]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(update_menu_bar) [MAC_OS]: Don't set
[gnu-emacs] / 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 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 2, or (at your option)
11 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; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
203
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
207
208 #define INFINITY 10000000
209
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
215
216 extern int interrupt_input;
217 extern int command_loop_level;
218
219 extern Lisp_Object do_mouse_tracking;
220
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
223
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
232
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
242
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
248
249 Lisp_Object Qrisky_local_variable;
250
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
253
254 /* Functions called to fontify regions of text. */
255
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
258
259 /* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_autoselect_window;
262
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
265
266 int auto_raise_tool_bar_buttons_p;
267
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
269
270 int make_cursor_line_fully_visible_p;
271
272 /* Margin around tool bar buttons in pixels. */
273
274 Lisp_Object Vtool_bar_button_margin;
275
276 /* Thickness of shadow to draw around tool bar buttons. */
277
278 EMACS_INT tool_bar_button_relief;
279
280 /* Non-zero means automatically resize tool-bars so that all tool-bar
281 items are visible, and no blank lines remain. */
282
283 int auto_resize_tool_bars_p;
284
285 /* Non-zero means draw block and hollow cursor as wide as the glyph
286 under it. For example, if a block cursor is over a tab, it will be
287 drawn as wide as that tab on the display. */
288
289 int x_stretch_cursor_p;
290
291 /* Non-nil means don't actually do any redisplay. */
292
293 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
294
295 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
296
297 int inhibit_eval_during_redisplay;
298
299 /* Names of text properties relevant for redisplay. */
300
301 Lisp_Object Qdisplay;
302 extern Lisp_Object Qface, Qinvisible, Qwidth;
303
304 /* Symbols used in text property values. */
305
306 Lisp_Object Vdisplay_pixels_per_inch;
307 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
308 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
309 Lisp_Object Qslice;
310 Lisp_Object Qcenter;
311 Lisp_Object Qmargin, Qpointer;
312 Lisp_Object Qline_height;
313 extern Lisp_Object Qheight;
314 extern Lisp_Object QCwidth, QCheight, QCascent;
315 extern Lisp_Object Qscroll_bar;
316 extern Lisp_Object Qcursor;
317
318 /* Non-nil means highlight trailing whitespace. */
319
320 Lisp_Object Vshow_trailing_whitespace;
321
322 /* Non-nil means escape non-break space and hyphens. */
323
324 Lisp_Object Vnobreak_char_display;
325
326 #ifdef HAVE_WINDOW_SYSTEM
327 extern Lisp_Object Voverflow_newline_into_fringe;
328
329 /* Test if overflow newline into fringe. Called with iterator IT
330 at or past right window margin, and with IT->current_x set. */
331
332 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
333 (!NILP (Voverflow_newline_into_fringe) \
334 && FRAME_WINDOW_P (it->f) \
335 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
336 && it->current_x == it->last_visible_x)
337
338 #endif /* HAVE_WINDOW_SYSTEM */
339
340 /* Non-nil means show the text cursor in void text areas
341 i.e. in blank areas after eol and eob. This used to be
342 the default in 21.3. */
343
344 Lisp_Object Vvoid_text_area_pointer;
345
346 /* Name of the face used to highlight trailing whitespace. */
347
348 Lisp_Object Qtrailing_whitespace;
349
350 /* Name and number of the face used to highlight escape glyphs. */
351
352 Lisp_Object Qescape_glyph;
353
354 /* Name and number of the face used to highlight non-breaking spaces. */
355
356 Lisp_Object Qnobreak_space;
357
358 /* The symbol `image' which is the car of the lists used to represent
359 images in Lisp. */
360
361 Lisp_Object Qimage;
362
363 /* The image map types. */
364 Lisp_Object QCmap, QCpointer;
365 Lisp_Object Qrect, Qcircle, Qpoly;
366
367 /* Non-zero means print newline to stdout before next mini-buffer
368 message. */
369
370 int noninteractive_need_newline;
371
372 /* Non-zero means print newline to message log before next message. */
373
374 static int message_log_need_newline;
375
376 /* Three markers that message_dolog uses.
377 It could allocate them itself, but that causes trouble
378 in handling memory-full errors. */
379 static Lisp_Object message_dolog_marker1;
380 static Lisp_Object message_dolog_marker2;
381 static Lisp_Object message_dolog_marker3;
382 \f
383 /* The buffer position of the first character appearing entirely or
384 partially on the line of the selected window which contains the
385 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
386 redisplay optimization in redisplay_internal. */
387
388 static struct text_pos this_line_start_pos;
389
390 /* Number of characters past the end of the line above, including the
391 terminating newline. */
392
393 static struct text_pos this_line_end_pos;
394
395 /* The vertical positions and the height of this line. */
396
397 static int this_line_vpos;
398 static int this_line_y;
399 static int this_line_pixel_height;
400
401 /* X position at which this display line starts. Usually zero;
402 negative if first character is partially visible. */
403
404 static int this_line_start_x;
405
406 /* Buffer that this_line_.* variables are referring to. */
407
408 static struct buffer *this_line_buffer;
409
410 /* Nonzero means truncate lines in all windows less wide than the
411 frame. */
412
413 int truncate_partial_width_windows;
414
415 /* A flag to control how to display unibyte 8-bit character. */
416
417 int unibyte_display_via_language_environment;
418
419 /* Nonzero means we have more than one non-mini-buffer-only frame.
420 Not guaranteed to be accurate except while parsing
421 frame-title-format. */
422
423 int multiple_frames;
424
425 Lisp_Object Vglobal_mode_string;
426
427
428 /* List of variables (symbols) which hold markers for overlay arrows.
429 The symbols on this list are examined during redisplay to determine
430 where to display overlay arrows. */
431
432 Lisp_Object Voverlay_arrow_variable_list;
433
434 /* Marker for where to display an arrow on top of the buffer text. */
435
436 Lisp_Object Voverlay_arrow_position;
437
438 /* String to display for the arrow. Only used on terminal frames. */
439
440 Lisp_Object Voverlay_arrow_string;
441
442 /* Values of those variables at last redisplay are stored as
443 properties on `overlay-arrow-position' symbol. However, if
444 Voverlay_arrow_position is a marker, last-arrow-position is its
445 numerical position. */
446
447 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
448
449 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
450 properties on a symbol in overlay-arrow-variable-list. */
451
452 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
453
454 /* Like mode-line-format, but for the title bar on a visible frame. */
455
456 Lisp_Object Vframe_title_format;
457
458 /* Like mode-line-format, but for the title bar on an iconified frame. */
459
460 Lisp_Object Vicon_title_format;
461
462 /* List of functions to call when a window's size changes. These
463 functions get one arg, a frame on which one or more windows' sizes
464 have changed. */
465
466 static Lisp_Object Vwindow_size_change_functions;
467
468 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
469
470 /* Nonzero if an overlay arrow has been displayed in this window. */
471
472 static int overlay_arrow_seen;
473
474 /* Nonzero means highlight the region even in nonselected windows. */
475
476 int highlight_nonselected_windows;
477
478 /* If cursor motion alone moves point off frame, try scrolling this
479 many lines up or down if that will bring it back. */
480
481 static EMACS_INT scroll_step;
482
483 /* Nonzero means scroll just far enough to bring point back on the
484 screen, when appropriate. */
485
486 static EMACS_INT scroll_conservatively;
487
488 /* Recenter the window whenever point gets within this many lines of
489 the top or bottom of the window. This value is translated into a
490 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
491 that there is really a fixed pixel height scroll margin. */
492
493 EMACS_INT scroll_margin;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* Zero means display the mode-line/header-line/menu-bar in the default face
506 (this slightly odd definition is for compatibility with previous versions
507 of emacs), non-zero means display them using their respective faces.
508
509 This variable is deprecated. */
510
511 int mode_line_inverse_video;
512
513 /* Prompt to display in front of the mini-buffer contents. */
514
515 Lisp_Object minibuf_prompt;
516
517 /* Width of current mini-buffer prompt. Only set after display_line
518 of the line that contains the prompt. */
519
520 int minibuf_prompt_width;
521
522 /* This is the window where the echo area message was displayed. It
523 is always a mini-buffer window, but it may not be the same window
524 currently active as a mini-buffer. */
525
526 Lisp_Object echo_area_window;
527
528 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
529 pushes the current message and the value of
530 message_enable_multibyte on the stack, the function restore_message
531 pops the stack and displays MESSAGE again. */
532
533 Lisp_Object Vmessage_stack;
534
535 /* Nonzero means multibyte characters were enabled when the echo area
536 message was specified. */
537
538 int message_enable_multibyte;
539
540 /* Nonzero if we should redraw the mode lines on the next redisplay. */
541
542 int update_mode_lines;
543
544 /* Nonzero if window sizes or contents have changed since last
545 redisplay that finished. */
546
547 int windows_or_buffers_changed;
548
549 /* Nonzero means a frame's cursor type has been changed. */
550
551 int cursor_type_changed;
552
553 /* Nonzero after display_mode_line if %l was used and it displayed a
554 line number. */
555
556 int line_number_displayed;
557
558 /* Maximum buffer size for which to display line numbers. */
559
560 Lisp_Object Vline_number_display_limit;
561
562 /* Line width to consider when repositioning for line number display. */
563
564 static EMACS_INT line_number_display_limit_width;
565
566 /* Number of lines to keep in the message log buffer. t means
567 infinite. nil means don't log at all. */
568
569 Lisp_Object Vmessage_log_max;
570
571 /* The name of the *Messages* buffer, a string. */
572
573 static Lisp_Object Vmessages_buffer_name;
574
575 /* Index 0 is the buffer that holds the current (desired) echo area message,
576 or nil if none is desired right now.
577
578 Index 1 is the buffer that holds the previously displayed echo area message,
579 or nil to indicate no message. This is normally what's on the screen now.
580
581 These two can point to the same buffer. That happens when the last
582 message output by the user (or made by echoing) has been displayed. */
583
584 Lisp_Object echo_area_buffer[2];
585
586 /* Permanent pointers to the two buffers that are used for echo area
587 purposes. Once the two buffers are made, and their pointers are
588 placed here, these two slots remain unchanged unless those buffers
589 need to be created afresh. */
590
591 static Lisp_Object echo_buffer[2];
592
593 /* A vector saved used in with_area_buffer to reduce consing. */
594
595 static Lisp_Object Vwith_echo_area_save_vector;
596
597 /* Non-zero means display_echo_area should display the last echo area
598 message again. Set by redisplay_preserve_echo_area. */
599
600 static int display_last_displayed_message_p;
601
602 /* Nonzero if echo area is being used by print; zero if being used by
603 message. */
604
605 int message_buf_print;
606
607 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
608
609 Lisp_Object Qinhibit_menubar_update;
610 int inhibit_menubar_update;
611
612 /* Maximum height for resizing mini-windows. Either a float
613 specifying a fraction of the available height, or an integer
614 specifying a number of lines. */
615
616 Lisp_Object Vmax_mini_window_height;
617
618 /* Non-zero means messages should be displayed with truncated
619 lines instead of being continued. */
620
621 int message_truncate_lines;
622 Lisp_Object Qmessage_truncate_lines;
623
624 /* Set to 1 in clear_message to make redisplay_internal aware
625 of an emptied echo area. */
626
627 static int message_cleared_p;
628
629 /* How to blink the default frame cursor off. */
630 Lisp_Object Vblink_cursor_alist;
631
632 /* A scratch glyph row with contents used for generating truncation
633 glyphs. Also used in direct_output_for_insert. */
634
635 #define MAX_SCRATCH_GLYPHS 100
636 struct glyph_row scratch_glyph_row;
637 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
638
639 /* Ascent and height of the last line processed by move_it_to. */
640
641 static int last_max_ascent, last_height;
642
643 /* Non-zero if there's a help-echo in the echo area. */
644
645 int help_echo_showing_p;
646
647 /* If >= 0, computed, exact values of mode-line and header-line height
648 to use in the macros CURRENT_MODE_LINE_HEIGHT and
649 CURRENT_HEADER_LINE_HEIGHT. */
650
651 int current_mode_line_height, current_header_line_height;
652
653 /* The maximum distance to look ahead for text properties. Values
654 that are too small let us call compute_char_face and similar
655 functions too often which is expensive. Values that are too large
656 let us call compute_char_face and alike too often because we
657 might not be interested in text properties that far away. */
658
659 #define TEXT_PROP_DISTANCE_LIMIT 100
660
661 #if GLYPH_DEBUG
662
663 /* Variables to turn off display optimizations from Lisp. */
664
665 int inhibit_try_window_id, inhibit_try_window_reusing;
666 int inhibit_try_cursor_movement;
667
668 /* Non-zero means print traces of redisplay if compiled with
669 GLYPH_DEBUG != 0. */
670
671 int trace_redisplay_p;
672
673 #endif /* GLYPH_DEBUG */
674
675 #ifdef DEBUG_TRACE_MOVE
676 /* Non-zero means trace with TRACE_MOVE to stderr. */
677 int trace_move;
678
679 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
680 #else
681 #define TRACE_MOVE(x) (void) 0
682 #endif
683
684 /* Non-zero means automatically scroll windows horizontally to make
685 point visible. */
686
687 int automatic_hscrolling_p;
688
689 /* How close to the margin can point get before the window is scrolled
690 horizontally. */
691 EMACS_INT hscroll_margin;
692
693 /* How much to scroll horizontally when point is inside the above margin. */
694 Lisp_Object Vhscroll_step;
695
696 /* The variable `resize-mini-windows'. If nil, don't resize
697 mini-windows. If t, always resize them to fit the text they
698 display. If `grow-only', let mini-windows grow only until they
699 become empty. */
700
701 Lisp_Object Vresize_mini_windows;
702
703 /* Buffer being redisplayed -- for redisplay_window_error. */
704
705 struct buffer *displayed_buffer;
706
707 /* Value returned from text property handlers (see below). */
708
709 enum prop_handled
710 {
711 HANDLED_NORMALLY,
712 HANDLED_RECOMPUTE_PROPS,
713 HANDLED_OVERLAY_STRING_CONSUMED,
714 HANDLED_RETURN
715 };
716
717 /* A description of text properties that redisplay is interested
718 in. */
719
720 struct props
721 {
722 /* The name of the property. */
723 Lisp_Object *name;
724
725 /* A unique index for the property. */
726 enum prop_idx idx;
727
728 /* A handler function called to set up iterator IT from the property
729 at IT's current position. Value is used to steer handle_stop. */
730 enum prop_handled (*handler) P_ ((struct it *it));
731 };
732
733 static enum prop_handled handle_face_prop P_ ((struct it *));
734 static enum prop_handled handle_invisible_prop P_ ((struct it *));
735 static enum prop_handled handle_display_prop P_ ((struct it *));
736 static enum prop_handled handle_composition_prop P_ ((struct it *));
737 static enum prop_handled handle_overlay_change P_ ((struct it *));
738 static enum prop_handled handle_fontified_prop P_ ((struct it *));
739
740 /* Properties handled by iterators. */
741
742 static struct props it_props[] =
743 {
744 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
745 /* Handle `face' before `display' because some sub-properties of
746 `display' need to know the face. */
747 {&Qface, FACE_PROP_IDX, handle_face_prop},
748 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
749 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
750 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
751 {NULL, 0, NULL}
752 };
753
754 /* Value is the position described by X. If X is a marker, value is
755 the marker_position of X. Otherwise, value is X. */
756
757 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
758
759 /* Enumeration returned by some move_it_.* functions internally. */
760
761 enum move_it_result
762 {
763 /* Not used. Undefined value. */
764 MOVE_UNDEFINED,
765
766 /* Move ended at the requested buffer position or ZV. */
767 MOVE_POS_MATCH_OR_ZV,
768
769 /* Move ended at the requested X pixel position. */
770 MOVE_X_REACHED,
771
772 /* Move within a line ended at the end of a line that must be
773 continued. */
774 MOVE_LINE_CONTINUED,
775
776 /* Move within a line ended at the end of a line that would
777 be displayed truncated. */
778 MOVE_LINE_TRUNCATED,
779
780 /* Move within a line ended at a line end. */
781 MOVE_NEWLINE_OR_CR
782 };
783
784 /* This counter is used to clear the face cache every once in a while
785 in redisplay_internal. It is incremented for each redisplay.
786 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
787 cleared. */
788
789 #define CLEAR_FACE_CACHE_COUNT 500
790 static int clear_face_cache_count;
791
792 /* Similarly for the image cache. */
793
794 #ifdef HAVE_WINDOW_SYSTEM
795 #define CLEAR_IMAGE_CACHE_COUNT 101
796 static int clear_image_cache_count;
797 #endif
798
799 /* Record the previous terminal frame we displayed. */
800
801 static struct frame *previous_terminal_frame;
802
803 /* Non-zero while redisplay_internal is in progress. */
804
805 int redisplaying_p;
806
807 /* Non-zero means don't free realized faces. Bound while freeing
808 realized faces is dangerous because glyph matrices might still
809 reference them. */
810
811 int inhibit_free_realized_faces;
812 Lisp_Object Qinhibit_free_realized_faces;
813
814 /* If a string, XTread_socket generates an event to display that string.
815 (The display is done in read_char.) */
816
817 Lisp_Object help_echo_string;
818 Lisp_Object help_echo_window;
819 Lisp_Object help_echo_object;
820 int help_echo_pos;
821
822 /* Temporary variable for XTread_socket. */
823
824 Lisp_Object previous_help_echo_string;
825
826 /* Null glyph slice */
827
828 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
829
830 \f
831 /* Function prototypes. */
832
833 static void setup_for_ellipsis P_ ((struct it *, int));
834 static void mark_window_display_accurate_1 P_ ((struct window *, int));
835 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
836 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
837 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
838 static int redisplay_mode_lines P_ ((Lisp_Object, int));
839 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
840
841 #if 0
842 static int invisible_text_between_p P_ ((struct it *, int, int));
843 #endif
844
845 static void pint2str P_ ((char *, int, int));
846 static void pint2hrstr P_ ((char *, int, int));
847 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
848 struct text_pos));
849 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
850 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
851 static void store_mode_line_noprop_char P_ ((char));
852 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
853 static void x_consider_frame_title P_ ((Lisp_Object));
854 static void handle_stop P_ ((struct it *));
855 static int tool_bar_lines_needed P_ ((struct frame *));
856 static int single_display_spec_intangible_p P_ ((Lisp_Object));
857 static void ensure_echo_area_buffers P_ ((void));
858 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
859 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
860 static int with_echo_area_buffer P_ ((struct window *, int,
861 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
862 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
863 static void clear_garbaged_frames P_ ((void));
864 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
866 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static int display_echo_area P_ ((struct window *));
868 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
870 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
871 static int string_char_and_length P_ ((const unsigned char *, int, int *));
872 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
873 struct text_pos));
874 static int compute_window_start_on_continuation_line P_ ((struct window *));
875 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
876 static void insert_left_trunc_glyphs P_ ((struct it *));
877 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
878 Lisp_Object));
879 static void extend_face_to_end_of_line P_ ((struct it *));
880 static int append_space_for_newline P_ ((struct it *, int));
881 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
882 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
883 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
884 static int trailing_whitespace_p P_ ((int));
885 static int message_log_check_duplicate P_ ((int, int, int, int));
886 static void push_it P_ ((struct it *));
887 static void pop_it P_ ((struct it *));
888 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
889 static void select_frame_for_redisplay P_ ((Lisp_Object));
890 static void redisplay_internal P_ ((int));
891 static int echo_area_display P_ ((int));
892 static void redisplay_windows P_ ((Lisp_Object));
893 static void redisplay_window P_ ((Lisp_Object, int));
894 static Lisp_Object redisplay_window_error ();
895 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
896 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
897 static void update_menu_bar P_ ((struct frame *, int));
898 static int try_window_reusing_current_matrix P_ ((struct window *));
899 static int try_window_id P_ ((struct window *));
900 static int display_line P_ ((struct it *));
901 static int display_mode_lines P_ ((struct window *));
902 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
903 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
904 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
905 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
906 static void display_menu_bar P_ ((struct window *));
907 static int display_count_lines P_ ((int, int, int, int, int *));
908 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
909 int, int, struct it *, int, int, int, int));
910 static void compute_line_metrics P_ ((struct it *));
911 static void run_redisplay_end_trigger_hook P_ ((struct it *));
912 static int get_overlay_strings P_ ((struct it *, int));
913 static void next_overlay_string P_ ((struct it *));
914 static void reseat P_ ((struct it *, struct text_pos, int));
915 static void reseat_1 P_ ((struct it *, struct text_pos, int));
916 static void back_to_previous_visible_line_start P_ ((struct it *));
917 void reseat_at_previous_visible_line_start P_ ((struct it *));
918 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
919 static int next_element_from_ellipsis P_ ((struct it *));
920 static int next_element_from_display_vector P_ ((struct it *));
921 static int next_element_from_string P_ ((struct it *));
922 static int next_element_from_c_string P_ ((struct it *));
923 static int next_element_from_buffer P_ ((struct it *));
924 static int next_element_from_composition P_ ((struct it *));
925 static int next_element_from_image P_ ((struct it *));
926 static int next_element_from_stretch P_ ((struct it *));
927 static void load_overlay_strings P_ ((struct it *, int));
928 static int init_from_display_pos P_ ((struct it *, struct window *,
929 struct display_pos *));
930 static void reseat_to_string P_ ((struct it *, unsigned char *,
931 Lisp_Object, int, int, int, int));
932 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
933 int, int, int));
934 void move_it_vertically_backward P_ ((struct it *, int));
935 static void init_to_row_start P_ ((struct it *, struct window *,
936 struct glyph_row *));
937 static int init_to_row_end P_ ((struct it *, struct window *,
938 struct glyph_row *));
939 static void back_to_previous_line_start P_ ((struct it *));
940 static int forward_to_next_line_start P_ ((struct it *, int *));
941 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
942 Lisp_Object, int));
943 static struct text_pos string_pos P_ ((int, Lisp_Object));
944 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
945 static int number_of_chars P_ ((unsigned char *, int));
946 static void compute_stop_pos P_ ((struct it *));
947 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
948 Lisp_Object));
949 static int face_before_or_after_it_pos P_ ((struct it *, int));
950 static int next_overlay_change P_ ((int));
951 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
952 Lisp_Object, struct text_pos *,
953 int));
954 static int underlying_face_id P_ ((struct it *));
955 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
956 struct window *));
957
958 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
959 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
960
961 #ifdef HAVE_WINDOW_SYSTEM
962
963 static void update_tool_bar P_ ((struct frame *, int));
964 static void build_desired_tool_bar_string P_ ((struct frame *f));
965 static int redisplay_tool_bar P_ ((struct frame *));
966 static void display_tool_bar_line P_ ((struct it *));
967 static void notice_overwritten_cursor P_ ((struct window *,
968 enum glyph_row_area,
969 int, int, int, int));
970
971
972
973 #endif /* HAVE_WINDOW_SYSTEM */
974
975 \f
976 /***********************************************************************
977 Window display dimensions
978 ***********************************************************************/
979
980 /* Return the bottom boundary y-position for text lines in window W.
981 This is the first y position at which a line cannot start.
982 It is relative to the top of the window.
983
984 This is the height of W minus the height of a mode line, if any. */
985
986 INLINE int
987 window_text_bottom_y (w)
988 struct window *w;
989 {
990 int height = WINDOW_TOTAL_HEIGHT (w);
991
992 if (WINDOW_WANTS_MODELINE_P (w))
993 height -= CURRENT_MODE_LINE_HEIGHT (w);
994 return height;
995 }
996
997 /* Return the pixel width of display area AREA of window W. AREA < 0
998 means return the total width of W, not including fringes to
999 the left and right of the window. */
1000
1001 INLINE int
1002 window_box_width (w, area)
1003 struct window *w;
1004 int area;
1005 {
1006 int cols = XFASTINT (w->total_cols);
1007 int pixels = 0;
1008
1009 if (!w->pseudo_window_p)
1010 {
1011 cols -= WINDOW_SCROLL_BAR_COLS (w);
1012
1013 if (area == TEXT_AREA)
1014 {
1015 if (INTEGERP (w->left_margin_cols))
1016 cols -= XFASTINT (w->left_margin_cols);
1017 if (INTEGERP (w->right_margin_cols))
1018 cols -= XFASTINT (w->right_margin_cols);
1019 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1020 }
1021 else if (area == LEFT_MARGIN_AREA)
1022 {
1023 cols = (INTEGERP (w->left_margin_cols)
1024 ? XFASTINT (w->left_margin_cols) : 0);
1025 pixels = 0;
1026 }
1027 else if (area == RIGHT_MARGIN_AREA)
1028 {
1029 cols = (INTEGERP (w->right_margin_cols)
1030 ? XFASTINT (w->right_margin_cols) : 0);
1031 pixels = 0;
1032 }
1033 }
1034
1035 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1036 }
1037
1038
1039 /* Return the pixel height of the display area of window W, not
1040 including mode lines of W, if any. */
1041
1042 INLINE int
1043 window_box_height (w)
1044 struct window *w;
1045 {
1046 struct frame *f = XFRAME (w->frame);
1047 int height = WINDOW_TOTAL_HEIGHT (w);
1048
1049 xassert (height >= 0);
1050
1051 /* Note: the code below that determines the mode-line/header-line
1052 height is essentially the same as that contained in the macro
1053 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1054 the appropriate glyph row has its `mode_line_p' flag set,
1055 and if it doesn't, uses estimate_mode_line_height instead. */
1056
1057 if (WINDOW_WANTS_MODELINE_P (w))
1058 {
1059 struct glyph_row *ml_row
1060 = (w->current_matrix && w->current_matrix->rows
1061 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1062 : 0);
1063 if (ml_row && ml_row->mode_line_p)
1064 height -= ml_row->height;
1065 else
1066 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1067 }
1068
1069 if (WINDOW_WANTS_HEADER_LINE_P (w))
1070 {
1071 struct glyph_row *hl_row
1072 = (w->current_matrix && w->current_matrix->rows
1073 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1074 : 0);
1075 if (hl_row && hl_row->mode_line_p)
1076 height -= hl_row->height;
1077 else
1078 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1079 }
1080
1081 /* With a very small font and a mode-line that's taller than
1082 default, we might end up with a negative height. */
1083 return max (0, height);
1084 }
1085
1086 /* Return the window-relative coordinate of the left edge of display
1087 area AREA of window W. AREA < 0 means return the left edge of the
1088 whole window, to the right of the left fringe of W. */
1089
1090 INLINE int
1091 window_box_left_offset (w, area)
1092 struct window *w;
1093 int area;
1094 {
1095 int x;
1096
1097 if (w->pseudo_window_p)
1098 return 0;
1099
1100 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1101
1102 if (area == TEXT_AREA)
1103 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1104 + window_box_width (w, LEFT_MARGIN_AREA));
1105 else if (area == RIGHT_MARGIN_AREA)
1106 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1107 + window_box_width (w, LEFT_MARGIN_AREA)
1108 + window_box_width (w, TEXT_AREA)
1109 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1110 ? 0
1111 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1112 else if (area == LEFT_MARGIN_AREA
1113 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1114 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1115
1116 return x;
1117 }
1118
1119
1120 /* Return the window-relative coordinate of the right edge of display
1121 area AREA of window W. AREA < 0 means return the left edge of the
1122 whole window, to the left of the right fringe of W. */
1123
1124 INLINE int
1125 window_box_right_offset (w, area)
1126 struct window *w;
1127 int area;
1128 {
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. AREA < 0 means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1135
1136 INLINE int
1137 window_box_left (w, area)
1138 struct window *w;
1139 int area;
1140 {
1141 struct frame *f = XFRAME (w->frame);
1142 int x;
1143
1144 if (w->pseudo_window_p)
1145 return FRAME_INTERNAL_BORDER_WIDTH (f);
1146
1147 x = (WINDOW_LEFT_EDGE_X (w)
1148 + window_box_left_offset (w, area));
1149
1150 return x;
1151 }
1152
1153
1154 /* Return the frame-relative coordinate of the right edge of display
1155 area AREA of window W. AREA < 0 means return the left edge of the
1156 whole window, to the left of the right fringe of W. */
1157
1158 INLINE int
1159 window_box_right (w, area)
1160 struct window *w;
1161 int area;
1162 {
1163 return window_box_left (w, area) + window_box_width (w, area);
1164 }
1165
1166 /* Get the bounding box of the display area AREA of window W, without
1167 mode lines, in frame-relative coordinates. AREA < 0 means the
1168 whole window, not including the left and right fringes of
1169 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1170 coordinates of the upper-left corner of the box. Return in
1171 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1172
1173 INLINE void
1174 window_box (w, area, box_x, box_y, box_width, box_height)
1175 struct window *w;
1176 int area;
1177 int *box_x, *box_y, *box_width, *box_height;
1178 {
1179 if (box_width)
1180 *box_width = window_box_width (w, area);
1181 if (box_height)
1182 *box_height = window_box_height (w);
1183 if (box_x)
1184 *box_x = window_box_left (w, area);
1185 if (box_y)
1186 {
1187 *box_y = WINDOW_TOP_EDGE_Y (w);
1188 if (WINDOW_WANTS_HEADER_LINE_P (w))
1189 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1190 }
1191 }
1192
1193
1194 /* Get the bounding box of the display area AREA of window W, without
1195 mode lines. AREA < 0 means the whole window, not including the
1196 left and right fringe of the window. Return in *TOP_LEFT_X
1197 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1198 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1199 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1200 box. */
1201
1202 INLINE void
1203 window_box_edges (w, area, top_left_x, top_left_y,
1204 bottom_right_x, bottom_right_y)
1205 struct window *w;
1206 int area;
1207 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1208 {
1209 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1210 bottom_right_y);
1211 *bottom_right_x += *top_left_x;
1212 *bottom_right_y += *top_left_y;
1213 }
1214
1215
1216 \f
1217 /***********************************************************************
1218 Utilities
1219 ***********************************************************************/
1220
1221 /* Return the bottom y-position of the line the iterator IT is in.
1222 This can modify IT's settings. */
1223
1224 int
1225 line_bottom_y (it)
1226 struct it *it;
1227 {
1228 int line_height = it->max_ascent + it->max_descent;
1229 int line_top_y = it->current_y;
1230
1231 if (line_height == 0)
1232 {
1233 if (last_height)
1234 line_height = last_height;
1235 else if (IT_CHARPOS (*it) < ZV)
1236 {
1237 move_it_by_lines (it, 1, 1);
1238 line_height = (it->max_ascent || it->max_descent
1239 ? it->max_ascent + it->max_descent
1240 : last_height);
1241 }
1242 else
1243 {
1244 struct glyph_row *row = it->glyph_row;
1245
1246 /* Use the default character height. */
1247 it->glyph_row = NULL;
1248 it->what = IT_CHARACTER;
1249 it->c = ' ';
1250 it->len = 1;
1251 PRODUCE_GLYPHS (it);
1252 line_height = it->ascent + it->descent;
1253 it->glyph_row = row;
1254 }
1255 }
1256
1257 return line_top_y + line_height;
1258 }
1259
1260
1261 /* Return 1 if position CHARPOS is visible in window W.
1262 If visible, set *X and *Y to pixel coordinates of top left corner.
1263 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1264 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1265 and header-lines heights. */
1266
1267 int
1268 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1269 struct window *w;
1270 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1271 {
1272 struct it it;
1273 struct text_pos top;
1274 int visible_p = 0;
1275 struct buffer *old_buffer = NULL;
1276
1277 if (noninteractive)
1278 return visible_p;
1279
1280 if (XBUFFER (w->buffer) != current_buffer)
1281 {
1282 old_buffer = current_buffer;
1283 set_buffer_internal_1 (XBUFFER (w->buffer));
1284 }
1285
1286 SET_TEXT_POS_FROM_MARKER (top, w->start);
1287
1288 /* Compute exact mode line heights, if requested. */
1289 if (exact_mode_line_heights_p)
1290 {
1291 if (WINDOW_WANTS_MODELINE_P (w))
1292 current_mode_line_height
1293 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1294 current_buffer->mode_line_format);
1295
1296 if (WINDOW_WANTS_HEADER_LINE_P (w))
1297 current_header_line_height
1298 = display_mode_line (w, HEADER_LINE_FACE_ID,
1299 current_buffer->header_line_format);
1300 }
1301
1302 start_display (&it, w, top);
1303 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1304 MOVE_TO_POS | MOVE_TO_Y);
1305
1306 /* Note that we may overshoot because of invisible text. */
1307 if (IT_CHARPOS (it) >= charpos)
1308 {
1309 int top_x = it.current_x;
1310 int top_y = it.current_y;
1311 int bottom_y = (last_height = 0, line_bottom_y (&it));
1312 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1313
1314 if (top_y < window_top_y)
1315 visible_p = bottom_y > window_top_y;
1316 else if (top_y < it.last_visible_y)
1317 visible_p = 1;
1318 if (visible_p)
1319 {
1320 *x = top_x;
1321 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1322 *rtop = max (0, window_top_y - top_y);
1323 *rbot = max (0, bottom_y - it.last_visible_y);
1324 }
1325 }
1326 else
1327 {
1328 struct it it2;
1329
1330 it2 = it;
1331 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1332 move_it_by_lines (&it, 1, 0);
1333 if (charpos < IT_CHARPOS (it))
1334 {
1335 visible_p = 1;
1336 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1337 *x = it2.current_x;
1338 *y = it2.current_y + it2.max_ascent - it2.ascent;
1339 *rtop = max (0, -it2.current_y);
1340 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1341 - it.last_visible_y));
1342 }
1343 }
1344
1345 if (old_buffer)
1346 set_buffer_internal_1 (old_buffer);
1347
1348 current_header_line_height = current_mode_line_height = -1;
1349
1350 if (visible_p && XFASTINT (w->hscroll) > 0)
1351 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1352
1353 return visible_p;
1354 }
1355
1356
1357 /* Return the next character from STR which is MAXLEN bytes long.
1358 Return in *LEN the length of the character. This is like
1359 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1360 we find one, we return a `?', but with the length of the invalid
1361 character. */
1362
1363 static INLINE int
1364 string_char_and_length (str, maxlen, len)
1365 const unsigned char *str;
1366 int maxlen, *len;
1367 {
1368 int c;
1369
1370 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1371 if (!CHAR_VALID_P (c, 1))
1372 /* We may not change the length here because other places in Emacs
1373 don't use this function, i.e. they silently accept invalid
1374 characters. */
1375 c = '?';
1376
1377 return c;
1378 }
1379
1380
1381
1382 /* Given a position POS containing a valid character and byte position
1383 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1384
1385 static struct text_pos
1386 string_pos_nchars_ahead (pos, string, nchars)
1387 struct text_pos pos;
1388 Lisp_Object string;
1389 int nchars;
1390 {
1391 xassert (STRINGP (string) && nchars >= 0);
1392
1393 if (STRING_MULTIBYTE (string))
1394 {
1395 int rest = SBYTES (string) - BYTEPOS (pos);
1396 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1397 int len;
1398
1399 while (nchars--)
1400 {
1401 string_char_and_length (p, rest, &len);
1402 p += len, rest -= len;
1403 xassert (rest >= 0);
1404 CHARPOS (pos) += 1;
1405 BYTEPOS (pos) += len;
1406 }
1407 }
1408 else
1409 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1410
1411 return pos;
1412 }
1413
1414
1415 /* Value is the text position, i.e. character and byte position,
1416 for character position CHARPOS in STRING. */
1417
1418 static INLINE struct text_pos
1419 string_pos (charpos, string)
1420 int charpos;
1421 Lisp_Object string;
1422 {
1423 struct text_pos pos;
1424 xassert (STRINGP (string));
1425 xassert (charpos >= 0);
1426 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1427 return pos;
1428 }
1429
1430
1431 /* Value is a text position, i.e. character and byte position, for
1432 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1433 means recognize multibyte characters. */
1434
1435 static struct text_pos
1436 c_string_pos (charpos, s, multibyte_p)
1437 int charpos;
1438 unsigned char *s;
1439 int multibyte_p;
1440 {
1441 struct text_pos pos;
1442
1443 xassert (s != NULL);
1444 xassert (charpos >= 0);
1445
1446 if (multibyte_p)
1447 {
1448 int rest = strlen (s), len;
1449
1450 SET_TEXT_POS (pos, 0, 0);
1451 while (charpos--)
1452 {
1453 string_char_and_length (s, rest, &len);
1454 s += len, rest -= len;
1455 xassert (rest >= 0);
1456 CHARPOS (pos) += 1;
1457 BYTEPOS (pos) += len;
1458 }
1459 }
1460 else
1461 SET_TEXT_POS (pos, charpos, charpos);
1462
1463 return pos;
1464 }
1465
1466
1467 /* Value is the number of characters in C string S. MULTIBYTE_P
1468 non-zero means recognize multibyte characters. */
1469
1470 static int
1471 number_of_chars (s, multibyte_p)
1472 unsigned char *s;
1473 int multibyte_p;
1474 {
1475 int nchars;
1476
1477 if (multibyte_p)
1478 {
1479 int rest = strlen (s), len;
1480 unsigned char *p = (unsigned char *) s;
1481
1482 for (nchars = 0; rest > 0; ++nchars)
1483 {
1484 string_char_and_length (p, rest, &len);
1485 rest -= len, p += len;
1486 }
1487 }
1488 else
1489 nchars = strlen (s);
1490
1491 return nchars;
1492 }
1493
1494
1495 /* Compute byte position NEWPOS->bytepos corresponding to
1496 NEWPOS->charpos. POS is a known position in string STRING.
1497 NEWPOS->charpos must be >= POS.charpos. */
1498
1499 static void
1500 compute_string_pos (newpos, pos, string)
1501 struct text_pos *newpos, pos;
1502 Lisp_Object string;
1503 {
1504 xassert (STRINGP (string));
1505 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1506
1507 if (STRING_MULTIBYTE (string))
1508 *newpos = string_pos_nchars_ahead (pos, string,
1509 CHARPOS (*newpos) - CHARPOS (pos));
1510 else
1511 BYTEPOS (*newpos) = CHARPOS (*newpos);
1512 }
1513
1514 /* EXPORT:
1515 Return an estimation of the pixel height of mode or top lines on
1516 frame F. FACE_ID specifies what line's height to estimate. */
1517
1518 int
1519 estimate_mode_line_height (f, face_id)
1520 struct frame *f;
1521 enum face_id face_id;
1522 {
1523 #ifdef HAVE_WINDOW_SYSTEM
1524 if (FRAME_WINDOW_P (f))
1525 {
1526 int height = FONT_HEIGHT (FRAME_FONT (f));
1527
1528 /* This function is called so early when Emacs starts that the face
1529 cache and mode line face are not yet initialized. */
1530 if (FRAME_FACE_CACHE (f))
1531 {
1532 struct face *face = FACE_FROM_ID (f, face_id);
1533 if (face)
1534 {
1535 if (face->font)
1536 height = FONT_HEIGHT (face->font);
1537 if (face->box_line_width > 0)
1538 height += 2 * face->box_line_width;
1539 }
1540 }
1541
1542 return height;
1543 }
1544 #endif
1545
1546 return 1;
1547 }
1548
1549 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1550 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1551 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1552 not force the value into range. */
1553
1554 void
1555 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1556 FRAME_PTR f;
1557 register int pix_x, pix_y;
1558 int *x, *y;
1559 NativeRectangle *bounds;
1560 int noclip;
1561 {
1562
1563 #ifdef HAVE_WINDOW_SYSTEM
1564 if (FRAME_WINDOW_P (f))
1565 {
1566 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1567 even for negative values. */
1568 if (pix_x < 0)
1569 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1570 if (pix_y < 0)
1571 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1572
1573 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1574 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1575
1576 if (bounds)
1577 STORE_NATIVE_RECT (*bounds,
1578 FRAME_COL_TO_PIXEL_X (f, pix_x),
1579 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1580 FRAME_COLUMN_WIDTH (f) - 1,
1581 FRAME_LINE_HEIGHT (f) - 1);
1582
1583 if (!noclip)
1584 {
1585 if (pix_x < 0)
1586 pix_x = 0;
1587 else if (pix_x > FRAME_TOTAL_COLS (f))
1588 pix_x = FRAME_TOTAL_COLS (f);
1589
1590 if (pix_y < 0)
1591 pix_y = 0;
1592 else if (pix_y > FRAME_LINES (f))
1593 pix_y = FRAME_LINES (f);
1594 }
1595 }
1596 #endif
1597
1598 *x = pix_x;
1599 *y = pix_y;
1600 }
1601
1602
1603 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1604 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1605 can't tell the positions because W's display is not up to date,
1606 return 0. */
1607
1608 int
1609 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1610 struct window *w;
1611 int hpos, vpos;
1612 int *frame_x, *frame_y;
1613 {
1614 #ifdef HAVE_WINDOW_SYSTEM
1615 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1616 {
1617 int success_p;
1618
1619 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1620 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1621
1622 if (display_completed)
1623 {
1624 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1625 struct glyph *glyph = row->glyphs[TEXT_AREA];
1626 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1627
1628 hpos = row->x;
1629 vpos = row->y;
1630 while (glyph < end)
1631 {
1632 hpos += glyph->pixel_width;
1633 ++glyph;
1634 }
1635
1636 /* If first glyph is partially visible, its first visible position is still 0. */
1637 if (hpos < 0)
1638 hpos = 0;
1639
1640 success_p = 1;
1641 }
1642 else
1643 {
1644 hpos = vpos = 0;
1645 success_p = 0;
1646 }
1647
1648 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1649 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1650 return success_p;
1651 }
1652 #endif
1653
1654 *frame_x = hpos;
1655 *frame_y = vpos;
1656 return 1;
1657 }
1658
1659
1660 #ifdef HAVE_WINDOW_SYSTEM
1661
1662 /* Find the glyph under window-relative coordinates X/Y in window W.
1663 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1664 strings. Return in *HPOS and *VPOS the row and column number of
1665 the glyph found. Return in *AREA the glyph area containing X.
1666 Value is a pointer to the glyph found or null if X/Y is not on
1667 text, or we can't tell because W's current matrix is not up to
1668 date. */
1669
1670 static struct glyph *
1671 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1672 struct window *w;
1673 int x, y;
1674 int *hpos, *vpos, *dx, *dy, *area;
1675 {
1676 struct glyph *glyph, *end;
1677 struct glyph_row *row = NULL;
1678 int x0, i;
1679
1680 /* Find row containing Y. Give up if some row is not enabled. */
1681 for (i = 0; i < w->current_matrix->nrows; ++i)
1682 {
1683 row = MATRIX_ROW (w->current_matrix, i);
1684 if (!row->enabled_p)
1685 return NULL;
1686 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1687 break;
1688 }
1689
1690 *vpos = i;
1691 *hpos = 0;
1692
1693 /* Give up if Y is not in the window. */
1694 if (i == w->current_matrix->nrows)
1695 return NULL;
1696
1697 /* Get the glyph area containing X. */
1698 if (w->pseudo_window_p)
1699 {
1700 *area = TEXT_AREA;
1701 x0 = 0;
1702 }
1703 else
1704 {
1705 if (x < window_box_left_offset (w, TEXT_AREA))
1706 {
1707 *area = LEFT_MARGIN_AREA;
1708 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1709 }
1710 else if (x < window_box_right_offset (w, TEXT_AREA))
1711 {
1712 *area = TEXT_AREA;
1713 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1714 }
1715 else
1716 {
1717 *area = RIGHT_MARGIN_AREA;
1718 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1719 }
1720 }
1721
1722 /* Find glyph containing X. */
1723 glyph = row->glyphs[*area];
1724 end = glyph + row->used[*area];
1725 x -= x0;
1726 while (glyph < end && x >= glyph->pixel_width)
1727 {
1728 x -= glyph->pixel_width;
1729 ++glyph;
1730 }
1731
1732 if (glyph == end)
1733 return NULL;
1734
1735 if (dx)
1736 {
1737 *dx = x;
1738 *dy = y - (row->y + row->ascent - glyph->ascent);
1739 }
1740
1741 *hpos = glyph - row->glyphs[*area];
1742 return glyph;
1743 }
1744
1745
1746 /* EXPORT:
1747 Convert frame-relative x/y to coordinates relative to window W.
1748 Takes pseudo-windows into account. */
1749
1750 void
1751 frame_to_window_pixel_xy (w, x, y)
1752 struct window *w;
1753 int *x, *y;
1754 {
1755 if (w->pseudo_window_p)
1756 {
1757 /* A pseudo-window is always full-width, and starts at the
1758 left edge of the frame, plus a frame border. */
1759 struct frame *f = XFRAME (w->frame);
1760 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1761 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1762 }
1763 else
1764 {
1765 *x -= WINDOW_LEFT_EDGE_X (w);
1766 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1767 }
1768 }
1769
1770 /* EXPORT:
1771 Return in RECTS[] at most N clipping rectangles for glyph string S.
1772 Return the number of stored rectangles. */
1773
1774 int
1775 get_glyph_string_clip_rects (s, rects, n)
1776 struct glyph_string *s;
1777 NativeRectangle *rects;
1778 int n;
1779 {
1780 XRectangle r;
1781
1782 if (n <= 0)
1783 return 0;
1784
1785 if (s->row->full_width_p)
1786 {
1787 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1788 r.x = WINDOW_LEFT_EDGE_X (s->w);
1789 r.width = WINDOW_TOTAL_WIDTH (s->w);
1790
1791 /* Unless displaying a mode or menu bar line, which are always
1792 fully visible, clip to the visible part of the row. */
1793 if (s->w->pseudo_window_p)
1794 r.height = s->row->visible_height;
1795 else
1796 r.height = s->height;
1797 }
1798 else
1799 {
1800 /* This is a text line that may be partially visible. */
1801 r.x = window_box_left (s->w, s->area);
1802 r.width = window_box_width (s->w, s->area);
1803 r.height = s->row->visible_height;
1804 }
1805
1806 if (s->clip_head)
1807 if (r.x < s->clip_head->x)
1808 {
1809 if (r.width >= s->clip_head->x - r.x)
1810 r.width -= s->clip_head->x - r.x;
1811 else
1812 r.width = 0;
1813 r.x = s->clip_head->x;
1814 }
1815 if (s->clip_tail)
1816 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1817 {
1818 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1819 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1820 else
1821 r.width = 0;
1822 }
1823
1824 /* If S draws overlapping rows, it's sufficient to use the top and
1825 bottom of the window for clipping because this glyph string
1826 intentionally draws over other lines. */
1827 if (s->for_overlaps)
1828 {
1829 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1830 r.height = window_text_bottom_y (s->w) - r.y;
1831
1832 /* Alas, the above simple strategy does not work for the
1833 environments with anti-aliased text: if the same text is
1834 drawn onto the same place multiple times, it gets thicker.
1835 If the overlap we are processing is for the erased cursor, we
1836 take the intersection with the rectagle of the cursor. */
1837 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1838 {
1839 XRectangle rc, r_save = r;
1840
1841 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1842 rc.y = s->w->phys_cursor.y;
1843 rc.width = s->w->phys_cursor_width;
1844 rc.height = s->w->phys_cursor_height;
1845
1846 x_intersect_rectangles (&r_save, &rc, &r);
1847 }
1848 }
1849 else
1850 {
1851 /* Don't use S->y for clipping because it doesn't take partially
1852 visible lines into account. For example, it can be negative for
1853 partially visible lines at the top of a window. */
1854 if (!s->row->full_width_p
1855 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1856 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1857 else
1858 r.y = max (0, s->row->y);
1859
1860 /* If drawing a tool-bar window, draw it over the internal border
1861 at the top of the window. */
1862 if (WINDOWP (s->f->tool_bar_window)
1863 && s->w == XWINDOW (s->f->tool_bar_window))
1864 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1865 }
1866
1867 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1868
1869 /* If drawing the cursor, don't let glyph draw outside its
1870 advertised boundaries. Cleartype does this under some circumstances. */
1871 if (s->hl == DRAW_CURSOR)
1872 {
1873 struct glyph *glyph = s->first_glyph;
1874 int height, max_y;
1875
1876 if (s->x > r.x)
1877 {
1878 r.width -= s->x - r.x;
1879 r.x = s->x;
1880 }
1881 r.width = min (r.width, glyph->pixel_width);
1882
1883 /* If r.y is below window bottom, ensure that we still see a cursor. */
1884 height = min (glyph->ascent + glyph->descent,
1885 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1886 max_y = window_text_bottom_y (s->w) - height;
1887 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1888 if (s->ybase - glyph->ascent > max_y)
1889 {
1890 r.y = max_y;
1891 r.height = height;
1892 }
1893 else
1894 {
1895 /* Don't draw cursor glyph taller than our actual glyph. */
1896 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1897 if (height < r.height)
1898 {
1899 max_y = r.y + r.height;
1900 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1901 r.height = min (max_y - r.y, height);
1902 }
1903 }
1904 }
1905
1906 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1907 || (s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1)
1908 {
1909 #ifdef CONVERT_FROM_XRECT
1910 CONVERT_FROM_XRECT (r, *rects);
1911 #else
1912 *rects = r;
1913 #endif
1914 return 1;
1915 }
1916 else
1917 {
1918 /* If we are processing overlapping and allowed to return
1919 multiple clipping rectangles, we exclude the row of the glyph
1920 string from the clipping rectangle. This is to avoid drawing
1921 the same text on the environment with anti-aliasing. */
1922 #ifdef CONVERT_FROM_XRECT
1923 XRectangle rs[2];
1924 #else
1925 XRectangle *rs = rects;
1926 #endif
1927 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1928
1929 if (s->for_overlaps & OVERLAPS_PRED)
1930 {
1931 rs[i] = r;
1932 if (r.y + r.height > row_y)
1933 if (r.y < row_y)
1934 rs[i].height = row_y - r.y;
1935 else
1936 rs[i].height = 0;
1937 i++;
1938 }
1939 if (s->for_overlaps & OVERLAPS_SUCC)
1940 {
1941 rs[i] = r;
1942 if (r.y < row_y + s->row->visible_height)
1943 if (r.y + r.height > row_y + s->row->visible_height)
1944 {
1945 rs[i].y = row_y + s->row->visible_height;
1946 rs[i].height = r.y + r.height - rs[i].y;
1947 }
1948 else
1949 rs[i].height = 0;
1950 i++;
1951 }
1952
1953 n = i;
1954 #ifdef CONVERT_FROM_XRECT
1955 for (i = 0; i < n; i++)
1956 CONVERT_FROM_XRECT (rs[i], rects[i]);
1957 #endif
1958 return n;
1959 }
1960 }
1961
1962 /* EXPORT:
1963 Return in *NR the clipping rectangle for glyph string S. */
1964
1965 void
1966 get_glyph_string_clip_rect (s, nr)
1967 struct glyph_string *s;
1968 NativeRectangle *nr;
1969 {
1970 get_glyph_string_clip_rects (s, nr, 1);
1971 }
1972
1973
1974 /* EXPORT:
1975 Return the position and height of the phys cursor in window W.
1976 Set w->phys_cursor_width to width of phys cursor.
1977 */
1978
1979 int
1980 get_phys_cursor_geometry (w, row, glyph, heightp)
1981 struct window *w;
1982 struct glyph_row *row;
1983 struct glyph *glyph;
1984 int *heightp;
1985 {
1986 struct frame *f = XFRAME (WINDOW_FRAME (w));
1987 int y, wd, h, h0, y0;
1988
1989 /* Compute the width of the rectangle to draw. If on a stretch
1990 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1991 rectangle as wide as the glyph, but use a canonical character
1992 width instead. */
1993 wd = glyph->pixel_width - 1;
1994 #ifdef HAVE_NTGUI
1995 wd++; /* Why? */
1996 #endif
1997 if (glyph->type == STRETCH_GLYPH
1998 && !x_stretch_cursor_p)
1999 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2000 w->phys_cursor_width = wd;
2001
2002 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2003
2004 /* If y is below window bottom, ensure that we still see a cursor. */
2005 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2006
2007 h = max (h0, glyph->ascent + glyph->descent);
2008 h0 = min (h0, glyph->ascent + glyph->descent);
2009
2010 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2011 if (y < y0)
2012 {
2013 h = max (h - (y0 - y) + 1, h0);
2014 y = y0 - 1;
2015 }
2016 else
2017 {
2018 y0 = window_text_bottom_y (w) - h0;
2019 if (y > y0)
2020 {
2021 h += y - y0;
2022 y = y0;
2023 }
2024 }
2025
2026 *heightp = h - 1;
2027 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
2028 }
2029
2030 /*
2031 * Remember which glyph the mouse is over.
2032 */
2033
2034 void
2035 remember_mouse_glyph (f, gx, gy, rect)
2036 struct frame *f;
2037 int gx, gy;
2038 NativeRectangle *rect;
2039 {
2040 Lisp_Object window;
2041 struct window *w;
2042 struct glyph_row *r, *gr, *end_row;
2043 enum window_part part;
2044 enum glyph_row_area area;
2045 int x, y, width, height;
2046
2047 /* Try to determine frame pixel position and size of the glyph under
2048 frame pixel coordinates X/Y on frame F. */
2049
2050 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2051 if (NILP (window))
2052 {
2053 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2054 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2055 goto virtual_glyph;
2056 }
2057
2058 w = XWINDOW (window);
2059 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2060 height = WINDOW_FRAME_LINE_HEIGHT (w);
2061
2062 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2063 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2064
2065 if (w->pseudo_window_p)
2066 {
2067 area = TEXT_AREA;
2068 part = ON_MODE_LINE; /* Don't adjust margin. */
2069 goto text_glyph;
2070 }
2071
2072 switch (part)
2073 {
2074 case ON_LEFT_MARGIN:
2075 area = LEFT_MARGIN_AREA;
2076 goto text_glyph;
2077
2078 case ON_RIGHT_MARGIN:
2079 area = RIGHT_MARGIN_AREA;
2080 goto text_glyph;
2081
2082 case ON_HEADER_LINE:
2083 case ON_MODE_LINE:
2084 gr = (part == ON_HEADER_LINE
2085 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2086 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2087 gy = gr->y;
2088 area = TEXT_AREA;
2089 goto text_glyph_row_found;
2090
2091 case ON_TEXT:
2092 area = TEXT_AREA;
2093
2094 text_glyph:
2095 gr = 0; gy = 0;
2096 for (; r <= end_row && r->enabled_p; ++r)
2097 if (r->y + r->height > y)
2098 {
2099 gr = r; gy = r->y;
2100 break;
2101 }
2102
2103 text_glyph_row_found:
2104 if (gr && gy <= y)
2105 {
2106 struct glyph *g = gr->glyphs[area];
2107 struct glyph *end = g + gr->used[area];
2108
2109 height = gr->height;
2110 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2111 if (gx + g->pixel_width > x)
2112 break;
2113
2114 if (g < end)
2115 {
2116 if (g->type == IMAGE_GLYPH)
2117 {
2118 /* Don't remember when mouse is over image, as
2119 image may have hot-spots. */
2120 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2121 return;
2122 }
2123 width = g->pixel_width;
2124 }
2125 else
2126 {
2127 /* Use nominal char spacing at end of line. */
2128 x -= gx;
2129 gx += (x / width) * width;
2130 }
2131
2132 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2133 gx += window_box_left_offset (w, area);
2134 }
2135 else
2136 {
2137 /* Use nominal line height at end of window. */
2138 gx = (x / width) * width;
2139 y -= gy;
2140 gy += (y / height) * height;
2141 }
2142 break;
2143
2144 case ON_LEFT_FRINGE:
2145 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2146 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2147 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2148 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2149 goto row_glyph;
2150
2151 case ON_RIGHT_FRINGE:
2152 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2153 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2154 : window_box_right_offset (w, TEXT_AREA));
2155 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2156 goto row_glyph;
2157
2158 case ON_SCROLL_BAR:
2159 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2160 ? 0
2161 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2162 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2163 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2164 : 0)));
2165 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2166
2167 row_glyph:
2168 gr = 0, gy = 0;
2169 for (; r <= end_row && r->enabled_p; ++r)
2170 if (r->y + r->height > y)
2171 {
2172 gr = r; gy = r->y;
2173 break;
2174 }
2175
2176 if (gr && gy <= y)
2177 height = gr->height;
2178 else
2179 {
2180 /* Use nominal line height at end of window. */
2181 y -= gy;
2182 gy += (y / height) * height;
2183 }
2184 break;
2185
2186 default:
2187 ;
2188 virtual_glyph:
2189 /* If there is no glyph under the mouse, then we divide the screen
2190 into a grid of the smallest glyph in the frame, and use that
2191 as our "glyph". */
2192
2193 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2194 round down even for negative values. */
2195 if (gx < 0)
2196 gx -= width - 1;
2197 if (gy < 0)
2198 gy -= height - 1;
2199
2200 gx = (gx / width) * width;
2201 gy = (gy / height) * height;
2202
2203 goto store_rect;
2204 }
2205
2206 gx += WINDOW_LEFT_EDGE_X (w);
2207 gy += WINDOW_TOP_EDGE_Y (w);
2208
2209 store_rect:
2210 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2211
2212 /* Visible feedback for debugging. */
2213 #if 0
2214 #if HAVE_X_WINDOWS
2215 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2216 f->output_data.x->normal_gc,
2217 gx, gy, width, height);
2218 #endif
2219 #endif
2220 }
2221
2222
2223 #endif /* HAVE_WINDOW_SYSTEM */
2224
2225 \f
2226 /***********************************************************************
2227 Lisp form evaluation
2228 ***********************************************************************/
2229
2230 /* Error handler for safe_eval and safe_call. */
2231
2232 static Lisp_Object
2233 safe_eval_handler (arg)
2234 Lisp_Object arg;
2235 {
2236 add_to_log ("Error during redisplay: %s", arg, Qnil);
2237 return Qnil;
2238 }
2239
2240
2241 /* Evaluate SEXPR and return the result, or nil if something went
2242 wrong. Prevent redisplay during the evaluation. */
2243
2244 Lisp_Object
2245 safe_eval (sexpr)
2246 Lisp_Object sexpr;
2247 {
2248 Lisp_Object val;
2249
2250 if (inhibit_eval_during_redisplay)
2251 val = Qnil;
2252 else
2253 {
2254 int count = SPECPDL_INDEX ();
2255 struct gcpro gcpro1;
2256
2257 GCPRO1 (sexpr);
2258 specbind (Qinhibit_redisplay, Qt);
2259 /* Use Qt to ensure debugger does not run,
2260 so there is no possibility of wanting to redisplay. */
2261 val = internal_condition_case_1 (Feval, sexpr, Qt,
2262 safe_eval_handler);
2263 UNGCPRO;
2264 val = unbind_to (count, val);
2265 }
2266
2267 return val;
2268 }
2269
2270
2271 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2272 Return the result, or nil if something went wrong. Prevent
2273 redisplay during the evaluation. */
2274
2275 Lisp_Object
2276 safe_call (nargs, args)
2277 int nargs;
2278 Lisp_Object *args;
2279 {
2280 Lisp_Object val;
2281
2282 if (inhibit_eval_during_redisplay)
2283 val = Qnil;
2284 else
2285 {
2286 int count = SPECPDL_INDEX ();
2287 struct gcpro gcpro1;
2288
2289 GCPRO1 (args[0]);
2290 gcpro1.nvars = nargs;
2291 specbind (Qinhibit_redisplay, Qt);
2292 /* Use Qt to ensure debugger does not run,
2293 so there is no possibility of wanting to redisplay. */
2294 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2295 safe_eval_handler);
2296 UNGCPRO;
2297 val = unbind_to (count, val);
2298 }
2299
2300 return val;
2301 }
2302
2303
2304 /* Call function FN with one argument ARG.
2305 Return the result, or nil if something went wrong. */
2306
2307 Lisp_Object
2308 safe_call1 (fn, arg)
2309 Lisp_Object fn, arg;
2310 {
2311 Lisp_Object args[2];
2312 args[0] = fn;
2313 args[1] = arg;
2314 return safe_call (2, args);
2315 }
2316
2317
2318 \f
2319 /***********************************************************************
2320 Debugging
2321 ***********************************************************************/
2322
2323 #if 0
2324
2325 /* Define CHECK_IT to perform sanity checks on iterators.
2326 This is for debugging. It is too slow to do unconditionally. */
2327
2328 static void
2329 check_it (it)
2330 struct it *it;
2331 {
2332 if (it->method == GET_FROM_STRING)
2333 {
2334 xassert (STRINGP (it->string));
2335 xassert (IT_STRING_CHARPOS (*it) >= 0);
2336 }
2337 else
2338 {
2339 xassert (IT_STRING_CHARPOS (*it) < 0);
2340 if (it->method == GET_FROM_BUFFER)
2341 {
2342 /* Check that character and byte positions agree. */
2343 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2344 }
2345 }
2346
2347 if (it->dpvec)
2348 xassert (it->current.dpvec_index >= 0);
2349 else
2350 xassert (it->current.dpvec_index < 0);
2351 }
2352
2353 #define CHECK_IT(IT) check_it ((IT))
2354
2355 #else /* not 0 */
2356
2357 #define CHECK_IT(IT) (void) 0
2358
2359 #endif /* not 0 */
2360
2361
2362 #if GLYPH_DEBUG
2363
2364 /* Check that the window end of window W is what we expect it
2365 to be---the last row in the current matrix displaying text. */
2366
2367 static void
2368 check_window_end (w)
2369 struct window *w;
2370 {
2371 if (!MINI_WINDOW_P (w)
2372 && !NILP (w->window_end_valid))
2373 {
2374 struct glyph_row *row;
2375 xassert ((row = MATRIX_ROW (w->current_matrix,
2376 XFASTINT (w->window_end_vpos)),
2377 !row->enabled_p
2378 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2379 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2380 }
2381 }
2382
2383 #define CHECK_WINDOW_END(W) check_window_end ((W))
2384
2385 #else /* not GLYPH_DEBUG */
2386
2387 #define CHECK_WINDOW_END(W) (void) 0
2388
2389 #endif /* not GLYPH_DEBUG */
2390
2391
2392 \f
2393 /***********************************************************************
2394 Iterator initialization
2395 ***********************************************************************/
2396
2397 /* Initialize IT for displaying current_buffer in window W, starting
2398 at character position CHARPOS. CHARPOS < 0 means that no buffer
2399 position is specified which is useful when the iterator is assigned
2400 a position later. BYTEPOS is the byte position corresponding to
2401 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2402
2403 If ROW is not null, calls to produce_glyphs with IT as parameter
2404 will produce glyphs in that row.
2405
2406 BASE_FACE_ID is the id of a base face to use. It must be one of
2407 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2408 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2409 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2410
2411 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2412 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2413 will be initialized to use the corresponding mode line glyph row of
2414 the desired matrix of W. */
2415
2416 void
2417 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2418 struct it *it;
2419 struct window *w;
2420 int charpos, bytepos;
2421 struct glyph_row *row;
2422 enum face_id base_face_id;
2423 {
2424 int highlight_region_p;
2425
2426 /* Some precondition checks. */
2427 xassert (w != NULL && it != NULL);
2428 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2429 && charpos <= ZV));
2430
2431 /* If face attributes have been changed since the last redisplay,
2432 free realized faces now because they depend on face definitions
2433 that might have changed. Don't free faces while there might be
2434 desired matrices pending which reference these faces. */
2435 if (face_change_count && !inhibit_free_realized_faces)
2436 {
2437 face_change_count = 0;
2438 free_all_realized_faces (Qnil);
2439 }
2440
2441 /* Use one of the mode line rows of W's desired matrix if
2442 appropriate. */
2443 if (row == NULL)
2444 {
2445 if (base_face_id == MODE_LINE_FACE_ID
2446 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2447 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2448 else if (base_face_id == HEADER_LINE_FACE_ID)
2449 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2450 }
2451
2452 /* Clear IT. */
2453 bzero (it, sizeof *it);
2454 it->current.overlay_string_index = -1;
2455 it->current.dpvec_index = -1;
2456 it->base_face_id = base_face_id;
2457 it->string = Qnil;
2458 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2459
2460 /* The window in which we iterate over current_buffer: */
2461 XSETWINDOW (it->window, w);
2462 it->w = w;
2463 it->f = XFRAME (w->frame);
2464
2465 /* Extra space between lines (on window systems only). */
2466 if (base_face_id == DEFAULT_FACE_ID
2467 && FRAME_WINDOW_P (it->f))
2468 {
2469 if (NATNUMP (current_buffer->extra_line_spacing))
2470 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2471 else if (FLOATP (current_buffer->extra_line_spacing))
2472 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2473 * FRAME_LINE_HEIGHT (it->f));
2474 else if (it->f->extra_line_spacing > 0)
2475 it->extra_line_spacing = it->f->extra_line_spacing;
2476 it->max_extra_line_spacing = 0;
2477 }
2478
2479 /* If realized faces have been removed, e.g. because of face
2480 attribute changes of named faces, recompute them. When running
2481 in batch mode, the face cache of Vterminal_frame is null. If
2482 we happen to get called, make a dummy face cache. */
2483 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2484 init_frame_faces (it->f);
2485 if (FRAME_FACE_CACHE (it->f)->used == 0)
2486 recompute_basic_faces (it->f);
2487
2488 /* Current value of the `slice', `space-width', and 'height' properties. */
2489 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2490 it->space_width = Qnil;
2491 it->font_height = Qnil;
2492 it->override_ascent = -1;
2493
2494 /* Are control characters displayed as `^C'? */
2495 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2496
2497 /* -1 means everything between a CR and the following line end
2498 is invisible. >0 means lines indented more than this value are
2499 invisible. */
2500 it->selective = (INTEGERP (current_buffer->selective_display)
2501 ? XFASTINT (current_buffer->selective_display)
2502 : (!NILP (current_buffer->selective_display)
2503 ? -1 : 0));
2504 it->selective_display_ellipsis_p
2505 = !NILP (current_buffer->selective_display_ellipses);
2506
2507 /* Display table to use. */
2508 it->dp = window_display_table (w);
2509
2510 /* Are multibyte characters enabled in current_buffer? */
2511 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2512
2513 /* Non-zero if we should highlight the region. */
2514 highlight_region_p
2515 = (!NILP (Vtransient_mark_mode)
2516 && !NILP (current_buffer->mark_active)
2517 && XMARKER (current_buffer->mark)->buffer != 0);
2518
2519 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2520 start and end of a visible region in window IT->w. Set both to
2521 -1 to indicate no region. */
2522 if (highlight_region_p
2523 /* Maybe highlight only in selected window. */
2524 && (/* Either show region everywhere. */
2525 highlight_nonselected_windows
2526 /* Or show region in the selected window. */
2527 || w == XWINDOW (selected_window)
2528 /* Or show the region if we are in the mini-buffer and W is
2529 the window the mini-buffer refers to. */
2530 || (MINI_WINDOW_P (XWINDOW (selected_window))
2531 && WINDOWP (minibuf_selected_window)
2532 && w == XWINDOW (minibuf_selected_window))))
2533 {
2534 int charpos = marker_position (current_buffer->mark);
2535 it->region_beg_charpos = min (PT, charpos);
2536 it->region_end_charpos = max (PT, charpos);
2537 }
2538 else
2539 it->region_beg_charpos = it->region_end_charpos = -1;
2540
2541 /* Get the position at which the redisplay_end_trigger hook should
2542 be run, if it is to be run at all. */
2543 if (MARKERP (w->redisplay_end_trigger)
2544 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2545 it->redisplay_end_trigger_charpos
2546 = marker_position (w->redisplay_end_trigger);
2547 else if (INTEGERP (w->redisplay_end_trigger))
2548 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2549
2550 /* Correct bogus values of tab_width. */
2551 it->tab_width = XINT (current_buffer->tab_width);
2552 if (it->tab_width <= 0 || it->tab_width > 1000)
2553 it->tab_width = 8;
2554
2555 /* Are lines in the display truncated? */
2556 it->truncate_lines_p
2557 = (base_face_id != DEFAULT_FACE_ID
2558 || XINT (it->w->hscroll)
2559 || (truncate_partial_width_windows
2560 && !WINDOW_FULL_WIDTH_P (it->w))
2561 || !NILP (current_buffer->truncate_lines));
2562
2563 /* Get dimensions of truncation and continuation glyphs. These are
2564 displayed as fringe bitmaps under X, so we don't need them for such
2565 frames. */
2566 if (!FRAME_WINDOW_P (it->f))
2567 {
2568 if (it->truncate_lines_p)
2569 {
2570 /* We will need the truncation glyph. */
2571 xassert (it->glyph_row == NULL);
2572 produce_special_glyphs (it, IT_TRUNCATION);
2573 it->truncation_pixel_width = it->pixel_width;
2574 }
2575 else
2576 {
2577 /* We will need the continuation glyph. */
2578 xassert (it->glyph_row == NULL);
2579 produce_special_glyphs (it, IT_CONTINUATION);
2580 it->continuation_pixel_width = it->pixel_width;
2581 }
2582
2583 /* Reset these values to zero because the produce_special_glyphs
2584 above has changed them. */
2585 it->pixel_width = it->ascent = it->descent = 0;
2586 it->phys_ascent = it->phys_descent = 0;
2587 }
2588
2589 /* Set this after getting the dimensions of truncation and
2590 continuation glyphs, so that we don't produce glyphs when calling
2591 produce_special_glyphs, above. */
2592 it->glyph_row = row;
2593 it->area = TEXT_AREA;
2594
2595 /* Get the dimensions of the display area. The display area
2596 consists of the visible window area plus a horizontally scrolled
2597 part to the left of the window. All x-values are relative to the
2598 start of this total display area. */
2599 if (base_face_id != DEFAULT_FACE_ID)
2600 {
2601 /* Mode lines, menu bar in terminal frames. */
2602 it->first_visible_x = 0;
2603 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2604 }
2605 else
2606 {
2607 it->first_visible_x
2608 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2609 it->last_visible_x = (it->first_visible_x
2610 + window_box_width (w, TEXT_AREA));
2611
2612 /* If we truncate lines, leave room for the truncator glyph(s) at
2613 the right margin. Otherwise, leave room for the continuation
2614 glyph(s). Truncation and continuation glyphs are not inserted
2615 for window-based redisplay. */
2616 if (!FRAME_WINDOW_P (it->f))
2617 {
2618 if (it->truncate_lines_p)
2619 it->last_visible_x -= it->truncation_pixel_width;
2620 else
2621 it->last_visible_x -= it->continuation_pixel_width;
2622 }
2623
2624 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2625 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2626 }
2627
2628 /* Leave room for a border glyph. */
2629 if (!FRAME_WINDOW_P (it->f)
2630 && !WINDOW_RIGHTMOST_P (it->w))
2631 it->last_visible_x -= 1;
2632
2633 it->last_visible_y = window_text_bottom_y (w);
2634
2635 /* For mode lines and alike, arrange for the first glyph having a
2636 left box line if the face specifies a box. */
2637 if (base_face_id != DEFAULT_FACE_ID)
2638 {
2639 struct face *face;
2640
2641 it->face_id = base_face_id;
2642
2643 /* If we have a boxed mode line, make the first character appear
2644 with a left box line. */
2645 face = FACE_FROM_ID (it->f, base_face_id);
2646 if (face->box != FACE_NO_BOX)
2647 it->start_of_box_run_p = 1;
2648 }
2649
2650 /* If a buffer position was specified, set the iterator there,
2651 getting overlays and face properties from that position. */
2652 if (charpos >= BUF_BEG (current_buffer))
2653 {
2654 it->end_charpos = ZV;
2655 it->face_id = -1;
2656 IT_CHARPOS (*it) = charpos;
2657
2658 /* Compute byte position if not specified. */
2659 if (bytepos < charpos)
2660 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2661 else
2662 IT_BYTEPOS (*it) = bytepos;
2663
2664 it->start = it->current;
2665
2666 /* Compute faces etc. */
2667 reseat (it, it->current.pos, 1);
2668 }
2669
2670 CHECK_IT (it);
2671 }
2672
2673
2674 /* Initialize IT for the display of window W with window start POS. */
2675
2676 void
2677 start_display (it, w, pos)
2678 struct it *it;
2679 struct window *w;
2680 struct text_pos pos;
2681 {
2682 struct glyph_row *row;
2683 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2684
2685 row = w->desired_matrix->rows + first_vpos;
2686 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2687 it->first_vpos = first_vpos;
2688
2689 /* Don't reseat to previous visible line start if current start
2690 position is in a string or image. */
2691 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2692 {
2693 int start_at_line_beg_p;
2694 int first_y = it->current_y;
2695
2696 /* If window start is not at a line start, skip forward to POS to
2697 get the correct continuation lines width. */
2698 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2699 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2700 if (!start_at_line_beg_p)
2701 {
2702 int new_x;
2703
2704 reseat_at_previous_visible_line_start (it);
2705 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2706
2707 new_x = it->current_x + it->pixel_width;
2708
2709 /* If lines are continued, this line may end in the middle
2710 of a multi-glyph character (e.g. a control character
2711 displayed as \003, or in the middle of an overlay
2712 string). In this case move_it_to above will not have
2713 taken us to the start of the continuation line but to the
2714 end of the continued line. */
2715 if (it->current_x > 0
2716 && !it->truncate_lines_p /* Lines are continued. */
2717 && (/* And glyph doesn't fit on the line. */
2718 new_x > it->last_visible_x
2719 /* Or it fits exactly and we're on a window
2720 system frame. */
2721 || (new_x == it->last_visible_x
2722 && FRAME_WINDOW_P (it->f))))
2723 {
2724 if (it->current.dpvec_index >= 0
2725 || it->current.overlay_string_index >= 0)
2726 {
2727 set_iterator_to_next (it, 1);
2728 move_it_in_display_line_to (it, -1, -1, 0);
2729 }
2730
2731 it->continuation_lines_width += it->current_x;
2732 }
2733
2734 /* We're starting a new display line, not affected by the
2735 height of the continued line, so clear the appropriate
2736 fields in the iterator structure. */
2737 it->max_ascent = it->max_descent = 0;
2738 it->max_phys_ascent = it->max_phys_descent = 0;
2739
2740 it->current_y = first_y;
2741 it->vpos = 0;
2742 it->current_x = it->hpos = 0;
2743 }
2744 }
2745
2746 #if 0 /* Don't assert the following because start_display is sometimes
2747 called intentionally with a window start that is not at a
2748 line start. Please leave this code in as a comment. */
2749
2750 /* Window start should be on a line start, now. */
2751 xassert (it->continuation_lines_width
2752 || IT_CHARPOS (it) == BEGV
2753 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2754 #endif /* 0 */
2755 }
2756
2757
2758 /* Return 1 if POS is a position in ellipses displayed for invisible
2759 text. W is the window we display, for text property lookup. */
2760
2761 static int
2762 in_ellipses_for_invisible_text_p (pos, w)
2763 struct display_pos *pos;
2764 struct window *w;
2765 {
2766 Lisp_Object prop, window;
2767 int ellipses_p = 0;
2768 int charpos = CHARPOS (pos->pos);
2769
2770 /* If POS specifies a position in a display vector, this might
2771 be for an ellipsis displayed for invisible text. We won't
2772 get the iterator set up for delivering that ellipsis unless
2773 we make sure that it gets aware of the invisible text. */
2774 if (pos->dpvec_index >= 0
2775 && pos->overlay_string_index < 0
2776 && CHARPOS (pos->string_pos) < 0
2777 && charpos > BEGV
2778 && (XSETWINDOW (window, w),
2779 prop = Fget_char_property (make_number (charpos),
2780 Qinvisible, window),
2781 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2782 {
2783 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2784 window);
2785 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2786 }
2787
2788 return ellipses_p;
2789 }
2790
2791
2792 /* Initialize IT for stepping through current_buffer in window W,
2793 starting at position POS that includes overlay string and display
2794 vector/ control character translation position information. Value
2795 is zero if there are overlay strings with newlines at POS. */
2796
2797 static int
2798 init_from_display_pos (it, w, pos)
2799 struct it *it;
2800 struct window *w;
2801 struct display_pos *pos;
2802 {
2803 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2804 int i, overlay_strings_with_newlines = 0;
2805
2806 /* If POS specifies a position in a display vector, this might
2807 be for an ellipsis displayed for invisible text. We won't
2808 get the iterator set up for delivering that ellipsis unless
2809 we make sure that it gets aware of the invisible text. */
2810 if (in_ellipses_for_invisible_text_p (pos, w))
2811 {
2812 --charpos;
2813 bytepos = 0;
2814 }
2815
2816 /* Keep in mind: the call to reseat in init_iterator skips invisible
2817 text, so we might end up at a position different from POS. This
2818 is only a problem when POS is a row start after a newline and an
2819 overlay starts there with an after-string, and the overlay has an
2820 invisible property. Since we don't skip invisible text in
2821 display_line and elsewhere immediately after consuming the
2822 newline before the row start, such a POS will not be in a string,
2823 but the call to init_iterator below will move us to the
2824 after-string. */
2825 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2826
2827 /* This only scans the current chunk -- it should scan all chunks.
2828 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2829 to 16 in 22.1 to make this a lesser problem. */
2830 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2831 {
2832 const char *s = SDATA (it->overlay_strings[i]);
2833 const char *e = s + SBYTES (it->overlay_strings[i]);
2834
2835 while (s < e && *s != '\n')
2836 ++s;
2837
2838 if (s < e)
2839 {
2840 overlay_strings_with_newlines = 1;
2841 break;
2842 }
2843 }
2844
2845 /* If position is within an overlay string, set up IT to the right
2846 overlay string. */
2847 if (pos->overlay_string_index >= 0)
2848 {
2849 int relative_index;
2850
2851 /* If the first overlay string happens to have a `display'
2852 property for an image, the iterator will be set up for that
2853 image, and we have to undo that setup first before we can
2854 correct the overlay string index. */
2855 if (it->method == GET_FROM_IMAGE)
2856 pop_it (it);
2857
2858 /* We already have the first chunk of overlay strings in
2859 IT->overlay_strings. Load more until the one for
2860 pos->overlay_string_index is in IT->overlay_strings. */
2861 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2862 {
2863 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2864 it->current.overlay_string_index = 0;
2865 while (n--)
2866 {
2867 load_overlay_strings (it, 0);
2868 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2869 }
2870 }
2871
2872 it->current.overlay_string_index = pos->overlay_string_index;
2873 relative_index = (it->current.overlay_string_index
2874 % OVERLAY_STRING_CHUNK_SIZE);
2875 it->string = it->overlay_strings[relative_index];
2876 xassert (STRINGP (it->string));
2877 it->current.string_pos = pos->string_pos;
2878 it->method = GET_FROM_STRING;
2879 }
2880
2881 #if 0 /* This is bogus because POS not having an overlay string
2882 position does not mean it's after the string. Example: A
2883 line starting with a before-string and initialization of IT
2884 to the previous row's end position. */
2885 else if (it->current.overlay_string_index >= 0)
2886 {
2887 /* If POS says we're already after an overlay string ending at
2888 POS, make sure to pop the iterator because it will be in
2889 front of that overlay string. When POS is ZV, we've thereby
2890 also ``processed'' overlay strings at ZV. */
2891 while (it->sp)
2892 pop_it (it);
2893 it->current.overlay_string_index = -1;
2894 it->method = GET_FROM_BUFFER;
2895 if (CHARPOS (pos->pos) == ZV)
2896 it->overlay_strings_at_end_processed_p = 1;
2897 }
2898 #endif /* 0 */
2899
2900 if (CHARPOS (pos->string_pos) >= 0)
2901 {
2902 /* Recorded position is not in an overlay string, but in another
2903 string. This can only be a string from a `display' property.
2904 IT should already be filled with that string. */
2905 it->current.string_pos = pos->string_pos;
2906 xassert (STRINGP (it->string));
2907 }
2908
2909 /* Restore position in display vector translations, control
2910 character translations or ellipses. */
2911 if (pos->dpvec_index >= 0)
2912 {
2913 if (it->dpvec == NULL)
2914 get_next_display_element (it);
2915 xassert (it->dpvec && it->current.dpvec_index == 0);
2916 it->current.dpvec_index = pos->dpvec_index;
2917 }
2918
2919 CHECK_IT (it);
2920 return !overlay_strings_with_newlines;
2921 }
2922
2923
2924 /* Initialize IT for stepping through current_buffer in window W
2925 starting at ROW->start. */
2926
2927 static void
2928 init_to_row_start (it, w, row)
2929 struct it *it;
2930 struct window *w;
2931 struct glyph_row *row;
2932 {
2933 init_from_display_pos (it, w, &row->start);
2934 it->start = row->start;
2935 it->continuation_lines_width = row->continuation_lines_width;
2936 CHECK_IT (it);
2937 }
2938
2939
2940 /* Initialize IT for stepping through current_buffer in window W
2941 starting in the line following ROW, i.e. starting at ROW->end.
2942 Value is zero if there are overlay strings with newlines at ROW's
2943 end position. */
2944
2945 static int
2946 init_to_row_end (it, w, row)
2947 struct it *it;
2948 struct window *w;
2949 struct glyph_row *row;
2950 {
2951 int success = 0;
2952
2953 if (init_from_display_pos (it, w, &row->end))
2954 {
2955 if (row->continued_p)
2956 it->continuation_lines_width
2957 = row->continuation_lines_width + row->pixel_width;
2958 CHECK_IT (it);
2959 success = 1;
2960 }
2961
2962 return success;
2963 }
2964
2965
2966
2967 \f
2968 /***********************************************************************
2969 Text properties
2970 ***********************************************************************/
2971
2972 /* Called when IT reaches IT->stop_charpos. Handle text property and
2973 overlay changes. Set IT->stop_charpos to the next position where
2974 to stop. */
2975
2976 static void
2977 handle_stop (it)
2978 struct it *it;
2979 {
2980 enum prop_handled handled;
2981 int handle_overlay_change_p;
2982 struct props *p;
2983
2984 it->dpvec = NULL;
2985 it->current.dpvec_index = -1;
2986 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2987 it->ignore_overlay_strings_at_pos_p = 0;
2988
2989 /* Use face of preceding text for ellipsis (if invisible) */
2990 if (it->selective_display_ellipsis_p)
2991 it->saved_face_id = it->face_id;
2992
2993 do
2994 {
2995 handled = HANDLED_NORMALLY;
2996
2997 /* Call text property handlers. */
2998 for (p = it_props; p->handler; ++p)
2999 {
3000 handled = p->handler (it);
3001
3002 if (handled == HANDLED_RECOMPUTE_PROPS)
3003 break;
3004 else if (handled == HANDLED_RETURN)
3005 return;
3006 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3007 handle_overlay_change_p = 0;
3008 }
3009
3010 if (handled != HANDLED_RECOMPUTE_PROPS)
3011 {
3012 /* Don't check for overlay strings below when set to deliver
3013 characters from a display vector. */
3014 if (it->method == GET_FROM_DISPLAY_VECTOR)
3015 handle_overlay_change_p = 0;
3016
3017 /* Handle overlay changes. */
3018 if (handle_overlay_change_p)
3019 handled = handle_overlay_change (it);
3020
3021 /* Determine where to stop next. */
3022 if (handled == HANDLED_NORMALLY)
3023 compute_stop_pos (it);
3024 }
3025 }
3026 while (handled == HANDLED_RECOMPUTE_PROPS);
3027 }
3028
3029
3030 /* Compute IT->stop_charpos from text property and overlay change
3031 information for IT's current position. */
3032
3033 static void
3034 compute_stop_pos (it)
3035 struct it *it;
3036 {
3037 register INTERVAL iv, next_iv;
3038 Lisp_Object object, limit, position;
3039
3040 /* If nowhere else, stop at the end. */
3041 it->stop_charpos = it->end_charpos;
3042
3043 if (STRINGP (it->string))
3044 {
3045 /* Strings are usually short, so don't limit the search for
3046 properties. */
3047 object = it->string;
3048 limit = Qnil;
3049 position = make_number (IT_STRING_CHARPOS (*it));
3050 }
3051 else
3052 {
3053 int charpos;
3054
3055 /* If next overlay change is in front of the current stop pos
3056 (which is IT->end_charpos), stop there. Note: value of
3057 next_overlay_change is point-max if no overlay change
3058 follows. */
3059 charpos = next_overlay_change (IT_CHARPOS (*it));
3060 if (charpos < it->stop_charpos)
3061 it->stop_charpos = charpos;
3062
3063 /* If showing the region, we have to stop at the region
3064 start or end because the face might change there. */
3065 if (it->region_beg_charpos > 0)
3066 {
3067 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3068 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3069 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3070 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3071 }
3072
3073 /* Set up variables for computing the stop position from text
3074 property changes. */
3075 XSETBUFFER (object, current_buffer);
3076 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3077 position = make_number (IT_CHARPOS (*it));
3078
3079 }
3080
3081 /* Get the interval containing IT's position. Value is a null
3082 interval if there isn't such an interval. */
3083 iv = validate_interval_range (object, &position, &position, 0);
3084 if (!NULL_INTERVAL_P (iv))
3085 {
3086 Lisp_Object values_here[LAST_PROP_IDX];
3087 struct props *p;
3088
3089 /* Get properties here. */
3090 for (p = it_props; p->handler; ++p)
3091 values_here[p->idx] = textget (iv->plist, *p->name);
3092
3093 /* Look for an interval following iv that has different
3094 properties. */
3095 for (next_iv = next_interval (iv);
3096 (!NULL_INTERVAL_P (next_iv)
3097 && (NILP (limit)
3098 || XFASTINT (limit) > next_iv->position));
3099 next_iv = next_interval (next_iv))
3100 {
3101 for (p = it_props; p->handler; ++p)
3102 {
3103 Lisp_Object new_value;
3104
3105 new_value = textget (next_iv->plist, *p->name);
3106 if (!EQ (values_here[p->idx], new_value))
3107 break;
3108 }
3109
3110 if (p->handler)
3111 break;
3112 }
3113
3114 if (!NULL_INTERVAL_P (next_iv))
3115 {
3116 if (INTEGERP (limit)
3117 && next_iv->position >= XFASTINT (limit))
3118 /* No text property change up to limit. */
3119 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3120 else
3121 /* Text properties change in next_iv. */
3122 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3123 }
3124 }
3125
3126 xassert (STRINGP (it->string)
3127 || (it->stop_charpos >= BEGV
3128 && it->stop_charpos >= IT_CHARPOS (*it)));
3129 }
3130
3131
3132 /* Return the position of the next overlay change after POS in
3133 current_buffer. Value is point-max if no overlay change
3134 follows. This is like `next-overlay-change' but doesn't use
3135 xmalloc. */
3136
3137 static int
3138 next_overlay_change (pos)
3139 int pos;
3140 {
3141 int noverlays;
3142 int endpos;
3143 Lisp_Object *overlays;
3144 int i;
3145
3146 /* Get all overlays at the given position. */
3147 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3148
3149 /* If any of these overlays ends before endpos,
3150 use its ending point instead. */
3151 for (i = 0; i < noverlays; ++i)
3152 {
3153 Lisp_Object oend;
3154 int oendpos;
3155
3156 oend = OVERLAY_END (overlays[i]);
3157 oendpos = OVERLAY_POSITION (oend);
3158 endpos = min (endpos, oendpos);
3159 }
3160
3161 return endpos;
3162 }
3163
3164
3165 \f
3166 /***********************************************************************
3167 Fontification
3168 ***********************************************************************/
3169
3170 /* Handle changes in the `fontified' property of the current buffer by
3171 calling hook functions from Qfontification_functions to fontify
3172 regions of text. */
3173
3174 static enum prop_handled
3175 handle_fontified_prop (it)
3176 struct it *it;
3177 {
3178 Lisp_Object prop, pos;
3179 enum prop_handled handled = HANDLED_NORMALLY;
3180
3181 if (!NILP (Vmemory_full))
3182 return handled;
3183
3184 /* Get the value of the `fontified' property at IT's current buffer
3185 position. (The `fontified' property doesn't have a special
3186 meaning in strings.) If the value is nil, call functions from
3187 Qfontification_functions. */
3188 if (!STRINGP (it->string)
3189 && it->s == NULL
3190 && !NILP (Vfontification_functions)
3191 && !NILP (Vrun_hooks)
3192 && (pos = make_number (IT_CHARPOS (*it)),
3193 prop = Fget_char_property (pos, Qfontified, Qnil),
3194 NILP (prop)))
3195 {
3196 int count = SPECPDL_INDEX ();
3197 Lisp_Object val;
3198
3199 val = Vfontification_functions;
3200 specbind (Qfontification_functions, Qnil);
3201
3202 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3203 safe_call1 (val, pos);
3204 else
3205 {
3206 Lisp_Object globals, fn;
3207 struct gcpro gcpro1, gcpro2;
3208
3209 globals = Qnil;
3210 GCPRO2 (val, globals);
3211
3212 for (; CONSP (val); val = XCDR (val))
3213 {
3214 fn = XCAR (val);
3215
3216 if (EQ (fn, Qt))
3217 {
3218 /* A value of t indicates this hook has a local
3219 binding; it means to run the global binding too.
3220 In a global value, t should not occur. If it
3221 does, we must ignore it to avoid an endless
3222 loop. */
3223 for (globals = Fdefault_value (Qfontification_functions);
3224 CONSP (globals);
3225 globals = XCDR (globals))
3226 {
3227 fn = XCAR (globals);
3228 if (!EQ (fn, Qt))
3229 safe_call1 (fn, pos);
3230 }
3231 }
3232 else
3233 safe_call1 (fn, pos);
3234 }
3235
3236 UNGCPRO;
3237 }
3238
3239 unbind_to (count, Qnil);
3240
3241 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3242 something. This avoids an endless loop if they failed to
3243 fontify the text for which reason ever. */
3244 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3245 handled = HANDLED_RECOMPUTE_PROPS;
3246 }
3247
3248 return handled;
3249 }
3250
3251
3252 \f
3253 /***********************************************************************
3254 Faces
3255 ***********************************************************************/
3256
3257 /* Set up iterator IT from face properties at its current position.
3258 Called from handle_stop. */
3259
3260 static enum prop_handled
3261 handle_face_prop (it)
3262 struct it *it;
3263 {
3264 int new_face_id, next_stop;
3265
3266 if (!STRINGP (it->string))
3267 {
3268 new_face_id
3269 = face_at_buffer_position (it->w,
3270 IT_CHARPOS (*it),
3271 it->region_beg_charpos,
3272 it->region_end_charpos,
3273 &next_stop,
3274 (IT_CHARPOS (*it)
3275 + TEXT_PROP_DISTANCE_LIMIT),
3276 0);
3277
3278 /* Is this a start of a run of characters with box face?
3279 Caveat: this can be called for a freshly initialized
3280 iterator; face_id is -1 in this case. We know that the new
3281 face will not change until limit, i.e. if the new face has a
3282 box, all characters up to limit will have one. But, as
3283 usual, we don't know whether limit is really the end. */
3284 if (new_face_id != it->face_id)
3285 {
3286 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3287
3288 /* If new face has a box but old face has not, this is
3289 the start of a run of characters with box, i.e. it has
3290 a shadow on the left side. The value of face_id of the
3291 iterator will be -1 if this is the initial call that gets
3292 the face. In this case, we have to look in front of IT's
3293 position and see whether there is a face != new_face_id. */
3294 it->start_of_box_run_p
3295 = (new_face->box != FACE_NO_BOX
3296 && (it->face_id >= 0
3297 || IT_CHARPOS (*it) == BEG
3298 || new_face_id != face_before_it_pos (it)));
3299 it->face_box_p = new_face->box != FACE_NO_BOX;
3300 }
3301 }
3302 else
3303 {
3304 int base_face_id, bufpos;
3305
3306 if (it->current.overlay_string_index >= 0)
3307 bufpos = IT_CHARPOS (*it);
3308 else
3309 bufpos = 0;
3310
3311 /* For strings from a buffer, i.e. overlay strings or strings
3312 from a `display' property, use the face at IT's current
3313 buffer position as the base face to merge with, so that
3314 overlay strings appear in the same face as surrounding
3315 text, unless they specify their own faces. */
3316 base_face_id = underlying_face_id (it);
3317
3318 new_face_id = face_at_string_position (it->w,
3319 it->string,
3320 IT_STRING_CHARPOS (*it),
3321 bufpos,
3322 it->region_beg_charpos,
3323 it->region_end_charpos,
3324 &next_stop,
3325 base_face_id, 0);
3326
3327 #if 0 /* This shouldn't be neccessary. Let's check it. */
3328 /* If IT is used to display a mode line we would really like to
3329 use the mode line face instead of the frame's default face. */
3330 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3331 && new_face_id == DEFAULT_FACE_ID)
3332 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3333 #endif
3334
3335 /* Is this a start of a run of characters with box? Caveat:
3336 this can be called for a freshly allocated iterator; face_id
3337 is -1 is this case. We know that the new face will not
3338 change until the next check pos, i.e. if the new face has a
3339 box, all characters up to that position will have a
3340 box. But, as usual, we don't know whether that position
3341 is really the end. */
3342 if (new_face_id != it->face_id)
3343 {
3344 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3345 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3346
3347 /* If new face has a box but old face hasn't, this is the
3348 start of a run of characters with box, i.e. it has a
3349 shadow on the left side. */
3350 it->start_of_box_run_p
3351 = new_face->box && (old_face == NULL || !old_face->box);
3352 it->face_box_p = new_face->box != FACE_NO_BOX;
3353 }
3354 }
3355
3356 it->face_id = new_face_id;
3357 return HANDLED_NORMALLY;
3358 }
3359
3360
3361 /* Return the ID of the face ``underlying'' IT's current position,
3362 which is in a string. If the iterator is associated with a
3363 buffer, return the face at IT's current buffer position.
3364 Otherwise, use the iterator's base_face_id. */
3365
3366 static int
3367 underlying_face_id (it)
3368 struct it *it;
3369 {
3370 int face_id = it->base_face_id, i;
3371
3372 xassert (STRINGP (it->string));
3373
3374 for (i = it->sp - 1; i >= 0; --i)
3375 if (NILP (it->stack[i].string))
3376 face_id = it->stack[i].face_id;
3377
3378 return face_id;
3379 }
3380
3381
3382 /* Compute the face one character before or after the current position
3383 of IT. BEFORE_P non-zero means get the face in front of IT's
3384 position. Value is the id of the face. */
3385
3386 static int
3387 face_before_or_after_it_pos (it, before_p)
3388 struct it *it;
3389 int before_p;
3390 {
3391 int face_id, limit;
3392 int next_check_charpos;
3393 struct text_pos pos;
3394
3395 xassert (it->s == NULL);
3396
3397 if (STRINGP (it->string))
3398 {
3399 int bufpos, base_face_id;
3400
3401 /* No face change past the end of the string (for the case
3402 we are padding with spaces). No face change before the
3403 string start. */
3404 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3405 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3406 return it->face_id;
3407
3408 /* Set pos to the position before or after IT's current position. */
3409 if (before_p)
3410 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3411 else
3412 /* For composition, we must check the character after the
3413 composition. */
3414 pos = (it->what == IT_COMPOSITION
3415 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3416 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3417
3418 if (it->current.overlay_string_index >= 0)
3419 bufpos = IT_CHARPOS (*it);
3420 else
3421 bufpos = 0;
3422
3423 base_face_id = underlying_face_id (it);
3424
3425 /* Get the face for ASCII, or unibyte. */
3426 face_id = face_at_string_position (it->w,
3427 it->string,
3428 CHARPOS (pos),
3429 bufpos,
3430 it->region_beg_charpos,
3431 it->region_end_charpos,
3432 &next_check_charpos,
3433 base_face_id, 0);
3434
3435 /* Correct the face for charsets different from ASCII. Do it
3436 for the multibyte case only. The face returned above is
3437 suitable for unibyte text if IT->string is unibyte. */
3438 if (STRING_MULTIBYTE (it->string))
3439 {
3440 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3441 int rest = SBYTES (it->string) - BYTEPOS (pos);
3442 int c, len;
3443 struct face *face = FACE_FROM_ID (it->f, face_id);
3444
3445 c = string_char_and_length (p, rest, &len);
3446 face_id = FACE_FOR_CHAR (it->f, face, c);
3447 }
3448 }
3449 else
3450 {
3451 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3452 || (IT_CHARPOS (*it) <= BEGV && before_p))
3453 return it->face_id;
3454
3455 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3456 pos = it->current.pos;
3457
3458 if (before_p)
3459 DEC_TEXT_POS (pos, it->multibyte_p);
3460 else
3461 {
3462 if (it->what == IT_COMPOSITION)
3463 /* For composition, we must check the position after the
3464 composition. */
3465 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3466 else
3467 INC_TEXT_POS (pos, it->multibyte_p);
3468 }
3469
3470 /* Determine face for CHARSET_ASCII, or unibyte. */
3471 face_id = face_at_buffer_position (it->w,
3472 CHARPOS (pos),
3473 it->region_beg_charpos,
3474 it->region_end_charpos,
3475 &next_check_charpos,
3476 limit, 0);
3477
3478 /* Correct the face for charsets different from ASCII. Do it
3479 for the multibyte case only. The face returned above is
3480 suitable for unibyte text if current_buffer is unibyte. */
3481 if (it->multibyte_p)
3482 {
3483 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3484 struct face *face = FACE_FROM_ID (it->f, face_id);
3485 face_id = FACE_FOR_CHAR (it->f, face, c);
3486 }
3487 }
3488
3489 return face_id;
3490 }
3491
3492
3493 \f
3494 /***********************************************************************
3495 Invisible text
3496 ***********************************************************************/
3497
3498 /* Set up iterator IT from invisible properties at its current
3499 position. Called from handle_stop. */
3500
3501 static enum prop_handled
3502 handle_invisible_prop (it)
3503 struct it *it;
3504 {
3505 enum prop_handled handled = HANDLED_NORMALLY;
3506
3507 if (STRINGP (it->string))
3508 {
3509 extern Lisp_Object Qinvisible;
3510 Lisp_Object prop, end_charpos, limit, charpos;
3511
3512 /* Get the value of the invisible text property at the
3513 current position. Value will be nil if there is no such
3514 property. */
3515 charpos = make_number (IT_STRING_CHARPOS (*it));
3516 prop = Fget_text_property (charpos, Qinvisible, it->string);
3517
3518 if (!NILP (prop)
3519 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3520 {
3521 handled = HANDLED_RECOMPUTE_PROPS;
3522
3523 /* Get the position at which the next change of the
3524 invisible text property can be found in IT->string.
3525 Value will be nil if the property value is the same for
3526 all the rest of IT->string. */
3527 XSETINT (limit, SCHARS (it->string));
3528 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3529 it->string, limit);
3530
3531 /* Text at current position is invisible. The next
3532 change in the property is at position end_charpos.
3533 Move IT's current position to that position. */
3534 if (INTEGERP (end_charpos)
3535 && XFASTINT (end_charpos) < XFASTINT (limit))
3536 {
3537 struct text_pos old;
3538 old = it->current.string_pos;
3539 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3540 compute_string_pos (&it->current.string_pos, old, it->string);
3541 }
3542 else
3543 {
3544 /* The rest of the string is invisible. If this is an
3545 overlay string, proceed with the next overlay string
3546 or whatever comes and return a character from there. */
3547 if (it->current.overlay_string_index >= 0)
3548 {
3549 next_overlay_string (it);
3550 /* Don't check for overlay strings when we just
3551 finished processing them. */
3552 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3553 }
3554 else
3555 {
3556 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3557 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3558 }
3559 }
3560 }
3561 }
3562 else
3563 {
3564 int invis_p, newpos, next_stop, start_charpos;
3565 Lisp_Object pos, prop, overlay;
3566
3567 /* First of all, is there invisible text at this position? */
3568 start_charpos = IT_CHARPOS (*it);
3569 pos = make_number (IT_CHARPOS (*it));
3570 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3571 &overlay);
3572 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3573
3574 /* If we are on invisible text, skip over it. */
3575 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3576 {
3577 /* Record whether we have to display an ellipsis for the
3578 invisible text. */
3579 int display_ellipsis_p = invis_p == 2;
3580
3581 handled = HANDLED_RECOMPUTE_PROPS;
3582
3583 /* Loop skipping over invisible text. The loop is left at
3584 ZV or with IT on the first char being visible again. */
3585 do
3586 {
3587 /* Try to skip some invisible text. Return value is the
3588 position reached which can be equal to IT's position
3589 if there is nothing invisible here. This skips both
3590 over invisible text properties and overlays with
3591 invisible property. */
3592 newpos = skip_invisible (IT_CHARPOS (*it),
3593 &next_stop, ZV, it->window);
3594
3595 /* If we skipped nothing at all we weren't at invisible
3596 text in the first place. If everything to the end of
3597 the buffer was skipped, end the loop. */
3598 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3599 invis_p = 0;
3600 else
3601 {
3602 /* We skipped some characters but not necessarily
3603 all there are. Check if we ended up on visible
3604 text. Fget_char_property returns the property of
3605 the char before the given position, i.e. if we
3606 get invis_p = 0, this means that the char at
3607 newpos is visible. */
3608 pos = make_number (newpos);
3609 prop = Fget_char_property (pos, Qinvisible, it->window);
3610 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3611 }
3612
3613 /* If we ended up on invisible text, proceed to
3614 skip starting with next_stop. */
3615 if (invis_p)
3616 IT_CHARPOS (*it) = next_stop;
3617
3618 /* If there are adjacent invisible texts, don't lose the
3619 second one's ellipsis. */
3620 if (invis_p == 2)
3621 display_ellipsis_p = 1;
3622 }
3623 while (invis_p);
3624
3625 /* The position newpos is now either ZV or on visible text. */
3626 IT_CHARPOS (*it) = newpos;
3627 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3628
3629 /* If there are before-strings at the start of invisible
3630 text, and the text is invisible because of a text
3631 property, arrange to show before-strings because 20.x did
3632 it that way. (If the text is invisible because of an
3633 overlay property instead of a text property, this is
3634 already handled in the overlay code.) */
3635 if (NILP (overlay)
3636 && get_overlay_strings (it, start_charpos))
3637 {
3638 handled = HANDLED_RECOMPUTE_PROPS;
3639 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3640 }
3641 else if (display_ellipsis_p)
3642 {
3643 /* Make sure that the glyphs of the ellipsis will get
3644 correct `charpos' values. If we would not update
3645 it->position here, the glyphs would belong to the
3646 last visible character _before_ the invisible
3647 text, which confuses `set_cursor_from_row'.
3648
3649 We use the last invisible position instead of the
3650 first because this way the cursor is always drawn on
3651 the first "." of the ellipsis, whenever PT is inside
3652 the invisible text. Otherwise the cursor would be
3653 placed _after_ the ellipsis when the point is after the
3654 first invisible character. */
3655 it->position.charpos = IT_CHARPOS (*it) - 1;
3656 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3657 setup_for_ellipsis (it, 0);
3658 }
3659 }
3660 }
3661
3662 return handled;
3663 }
3664
3665
3666 /* Make iterator IT return `...' next.
3667 Replaces LEN characters from buffer. */
3668
3669 static void
3670 setup_for_ellipsis (it, len)
3671 struct it *it;
3672 int len;
3673 {
3674 /* Use the display table definition for `...'. Invalid glyphs
3675 will be handled by the method returning elements from dpvec. */
3676 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3677 {
3678 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3679 it->dpvec = v->contents;
3680 it->dpend = v->contents + v->size;
3681 }
3682 else
3683 {
3684 /* Default `...'. */
3685 it->dpvec = default_invis_vector;
3686 it->dpend = default_invis_vector + 3;
3687 }
3688
3689 it->dpvec_char_len = len;
3690 it->current.dpvec_index = 0;
3691 it->dpvec_face_id = -1;
3692
3693 /* Remember the current face id in case glyphs specify faces.
3694 IT's face is restored in set_iterator_to_next.
3695 saved_face_id was set to preceding char's face in handle_stop. */
3696 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3697 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3698
3699 it->method = GET_FROM_DISPLAY_VECTOR;
3700 it->ellipsis_p = 1;
3701 }
3702
3703
3704 \f
3705 /***********************************************************************
3706 'display' property
3707 ***********************************************************************/
3708
3709 /* Set up iterator IT from `display' property at its current position.
3710 Called from handle_stop.
3711 We return HANDLED_RETURN if some part of the display property
3712 overrides the display of the buffer text itself.
3713 Otherwise we return HANDLED_NORMALLY. */
3714
3715 static enum prop_handled
3716 handle_display_prop (it)
3717 struct it *it;
3718 {
3719 Lisp_Object prop, object;
3720 struct text_pos *position;
3721 /* Nonzero if some property replaces the display of the text itself. */
3722 int display_replaced_p = 0;
3723
3724 if (STRINGP (it->string))
3725 {
3726 object = it->string;
3727 position = &it->current.string_pos;
3728 }
3729 else
3730 {
3731 XSETWINDOW (object, it->w);
3732 position = &it->current.pos;
3733 }
3734
3735 /* Reset those iterator values set from display property values. */
3736 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3737 it->space_width = Qnil;
3738 it->font_height = Qnil;
3739 it->voffset = 0;
3740
3741 /* We don't support recursive `display' properties, i.e. string
3742 values that have a string `display' property, that have a string
3743 `display' property etc. */
3744 if (!it->string_from_display_prop_p)
3745 it->area = TEXT_AREA;
3746
3747 prop = Fget_char_property (make_number (position->charpos),
3748 Qdisplay, object);
3749 if (NILP (prop))
3750 return HANDLED_NORMALLY;
3751
3752 if (!STRINGP (it->string))
3753 object = it->w->buffer;
3754
3755 if (CONSP (prop)
3756 /* Simple properties. */
3757 && !EQ (XCAR (prop), Qimage)
3758 && !EQ (XCAR (prop), Qspace)
3759 && !EQ (XCAR (prop), Qwhen)
3760 && !EQ (XCAR (prop), Qslice)
3761 && !EQ (XCAR (prop), Qspace_width)
3762 && !EQ (XCAR (prop), Qheight)
3763 && !EQ (XCAR (prop), Qraise)
3764 /* Marginal area specifications. */
3765 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3766 && !EQ (XCAR (prop), Qleft_fringe)
3767 && !EQ (XCAR (prop), Qright_fringe)
3768 && !NILP (XCAR (prop)))
3769 {
3770 for (; CONSP (prop); prop = XCDR (prop))
3771 {
3772 if (handle_single_display_spec (it, XCAR (prop), object,
3773 position, display_replaced_p))
3774 display_replaced_p = 1;
3775 }
3776 }
3777 else if (VECTORP (prop))
3778 {
3779 int i;
3780 for (i = 0; i < ASIZE (prop); ++i)
3781 if (handle_single_display_spec (it, AREF (prop, i), object,
3782 position, display_replaced_p))
3783 display_replaced_p = 1;
3784 }
3785 else
3786 {
3787 int ret = handle_single_display_spec (it, prop, object, position, 0);
3788 if (ret < 0) /* Replaced by "", i.e. nothing. */
3789 return HANDLED_RECOMPUTE_PROPS;
3790 if (ret)
3791 display_replaced_p = 1;
3792 }
3793
3794 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3795 }
3796
3797
3798 /* Value is the position of the end of the `display' property starting
3799 at START_POS in OBJECT. */
3800
3801 static struct text_pos
3802 display_prop_end (it, object, start_pos)
3803 struct it *it;
3804 Lisp_Object object;
3805 struct text_pos start_pos;
3806 {
3807 Lisp_Object end;
3808 struct text_pos end_pos;
3809
3810 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3811 Qdisplay, object, Qnil);
3812 CHARPOS (end_pos) = XFASTINT (end);
3813 if (STRINGP (object))
3814 compute_string_pos (&end_pos, start_pos, it->string);
3815 else
3816 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3817
3818 return end_pos;
3819 }
3820
3821
3822 /* Set up IT from a single `display' specification PROP. OBJECT
3823 is the object in which the `display' property was found. *POSITION
3824 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3825 means that we previously saw a display specification which already
3826 replaced text display with something else, for example an image;
3827 we ignore such properties after the first one has been processed.
3828
3829 If PROP is a `space' or `image' specification, and in some other
3830 cases too, set *POSITION to the position where the `display'
3831 property ends.
3832
3833 Value is non-zero if something was found which replaces the display
3834 of buffer or string text. Specifically, the value is -1 if that
3835 "something" is "nothing". */
3836
3837 static int
3838 handle_single_display_spec (it, spec, object, position,
3839 display_replaced_before_p)
3840 struct it *it;
3841 Lisp_Object spec;
3842 Lisp_Object object;
3843 struct text_pos *position;
3844 int display_replaced_before_p;
3845 {
3846 Lisp_Object form;
3847 Lisp_Object location, value;
3848 struct text_pos start_pos;
3849 int valid_p;
3850
3851 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3852 If the result is non-nil, use VALUE instead of SPEC. */
3853 form = Qt;
3854 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3855 {
3856 spec = XCDR (spec);
3857 if (!CONSP (spec))
3858 return 0;
3859 form = XCAR (spec);
3860 spec = XCDR (spec);
3861 }
3862
3863 if (!NILP (form) && !EQ (form, Qt))
3864 {
3865 int count = SPECPDL_INDEX ();
3866 struct gcpro gcpro1;
3867
3868 /* Bind `object' to the object having the `display' property, a
3869 buffer or string. Bind `position' to the position in the
3870 object where the property was found, and `buffer-position'
3871 to the current position in the buffer. */
3872 specbind (Qobject, object);
3873 specbind (Qposition, make_number (CHARPOS (*position)));
3874 specbind (Qbuffer_position,
3875 make_number (STRINGP (object)
3876 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3877 GCPRO1 (form);
3878 form = safe_eval (form);
3879 UNGCPRO;
3880 unbind_to (count, Qnil);
3881 }
3882
3883 if (NILP (form))
3884 return 0;
3885
3886 /* Handle `(height HEIGHT)' specifications. */
3887 if (CONSP (spec)
3888 && EQ (XCAR (spec), Qheight)
3889 && CONSP (XCDR (spec)))
3890 {
3891 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3892 return 0;
3893
3894 it->font_height = XCAR (XCDR (spec));
3895 if (!NILP (it->font_height))
3896 {
3897 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3898 int new_height = -1;
3899
3900 if (CONSP (it->font_height)
3901 && (EQ (XCAR (it->font_height), Qplus)
3902 || EQ (XCAR (it->font_height), Qminus))
3903 && CONSP (XCDR (it->font_height))
3904 && INTEGERP (XCAR (XCDR (it->font_height))))
3905 {
3906 /* `(+ N)' or `(- N)' where N is an integer. */
3907 int steps = XINT (XCAR (XCDR (it->font_height)));
3908 if (EQ (XCAR (it->font_height), Qplus))
3909 steps = - steps;
3910 it->face_id = smaller_face (it->f, it->face_id, steps);
3911 }
3912 else if (FUNCTIONP (it->font_height))
3913 {
3914 /* Call function with current height as argument.
3915 Value is the new height. */
3916 Lisp_Object height;
3917 height = safe_call1 (it->font_height,
3918 face->lface[LFACE_HEIGHT_INDEX]);
3919 if (NUMBERP (height))
3920 new_height = XFLOATINT (height);
3921 }
3922 else if (NUMBERP (it->font_height))
3923 {
3924 /* Value is a multiple of the canonical char height. */
3925 struct face *face;
3926
3927 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3928 new_height = (XFLOATINT (it->font_height)
3929 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3930 }
3931 else
3932 {
3933 /* Evaluate IT->font_height with `height' bound to the
3934 current specified height to get the new height. */
3935 int count = SPECPDL_INDEX ();
3936
3937 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3938 value = safe_eval (it->font_height);
3939 unbind_to (count, Qnil);
3940
3941 if (NUMBERP (value))
3942 new_height = XFLOATINT (value);
3943 }
3944
3945 if (new_height > 0)
3946 it->face_id = face_with_height (it->f, it->face_id, new_height);
3947 }
3948
3949 return 0;
3950 }
3951
3952 /* Handle `(space_width WIDTH)'. */
3953 if (CONSP (spec)
3954 && EQ (XCAR (spec), Qspace_width)
3955 && CONSP (XCDR (spec)))
3956 {
3957 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3958 return 0;
3959
3960 value = XCAR (XCDR (spec));
3961 if (NUMBERP (value) && XFLOATINT (value) > 0)
3962 it->space_width = value;
3963
3964 return 0;
3965 }
3966
3967 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3968 if (CONSP (spec)
3969 && EQ (XCAR (spec), Qslice))
3970 {
3971 Lisp_Object tem;
3972
3973 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3974 return 0;
3975
3976 if (tem = XCDR (spec), CONSP (tem))
3977 {
3978 it->slice.x = XCAR (tem);
3979 if (tem = XCDR (tem), CONSP (tem))
3980 {
3981 it->slice.y = XCAR (tem);
3982 if (tem = XCDR (tem), CONSP (tem))
3983 {
3984 it->slice.width = XCAR (tem);
3985 if (tem = XCDR (tem), CONSP (tem))
3986 it->slice.height = XCAR (tem);
3987 }
3988 }
3989 }
3990
3991 return 0;
3992 }
3993
3994 /* Handle `(raise FACTOR)'. */
3995 if (CONSP (spec)
3996 && EQ (XCAR (spec), Qraise)
3997 && CONSP (XCDR (spec)))
3998 {
3999 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4000 return 0;
4001
4002 #ifdef HAVE_WINDOW_SYSTEM
4003 value = XCAR (XCDR (spec));
4004 if (NUMBERP (value))
4005 {
4006 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4007 it->voffset = - (XFLOATINT (value)
4008 * (FONT_HEIGHT (face->font)));
4009 }
4010 #endif /* HAVE_WINDOW_SYSTEM */
4011
4012 return 0;
4013 }
4014
4015 /* Don't handle the other kinds of display specifications
4016 inside a string that we got from a `display' property. */
4017 if (it->string_from_display_prop_p)
4018 return 0;
4019
4020 /* Characters having this form of property are not displayed, so
4021 we have to find the end of the property. */
4022 start_pos = *position;
4023 *position = display_prop_end (it, object, start_pos);
4024 value = Qnil;
4025
4026 /* Stop the scan at that end position--we assume that all
4027 text properties change there. */
4028 it->stop_charpos = position->charpos;
4029
4030 /* Handle `(left-fringe BITMAP [FACE])'
4031 and `(right-fringe BITMAP [FACE])'. */
4032 if (CONSP (spec)
4033 && (EQ (XCAR (spec), Qleft_fringe)
4034 || EQ (XCAR (spec), Qright_fringe))
4035 && CONSP (XCDR (spec)))
4036 {
4037 int face_id = DEFAULT_FACE_ID;
4038 int fringe_bitmap;
4039
4040 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4041 /* If we return here, POSITION has been advanced
4042 across the text with this property. */
4043 return 0;
4044
4045 #ifdef HAVE_WINDOW_SYSTEM
4046 value = XCAR (XCDR (spec));
4047 if (!SYMBOLP (value)
4048 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4049 /* If we return here, POSITION has been advanced
4050 across the text with this property. */
4051 return 0;
4052
4053 if (CONSP (XCDR (XCDR (spec))))
4054 {
4055 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4056 int face_id2 = lookup_derived_face (it->f, face_name,
4057 'A', FRINGE_FACE_ID, 0);
4058 if (face_id2 >= 0)
4059 face_id = face_id2;
4060 }
4061
4062 /* Save current settings of IT so that we can restore them
4063 when we are finished with the glyph property value. */
4064
4065 push_it (it);
4066
4067 it->area = TEXT_AREA;
4068 it->what = IT_IMAGE;
4069 it->image_id = -1; /* no image */
4070 it->position = start_pos;
4071 it->object = NILP (object) ? it->w->buffer : object;
4072 it->method = GET_FROM_IMAGE;
4073 it->face_id = face_id;
4074
4075 /* Say that we haven't consumed the characters with
4076 `display' property yet. The call to pop_it in
4077 set_iterator_to_next will clean this up. */
4078 *position = start_pos;
4079
4080 if (EQ (XCAR (spec), Qleft_fringe))
4081 {
4082 it->left_user_fringe_bitmap = fringe_bitmap;
4083 it->left_user_fringe_face_id = face_id;
4084 }
4085 else
4086 {
4087 it->right_user_fringe_bitmap = fringe_bitmap;
4088 it->right_user_fringe_face_id = face_id;
4089 }
4090 #endif /* HAVE_WINDOW_SYSTEM */
4091 return 1;
4092 }
4093
4094 /* Prepare to handle `((margin left-margin) ...)',
4095 `((margin right-margin) ...)' and `((margin nil) ...)'
4096 prefixes for display specifications. */
4097 location = Qunbound;
4098 if (CONSP (spec) && CONSP (XCAR (spec)))
4099 {
4100 Lisp_Object tem;
4101
4102 value = XCDR (spec);
4103 if (CONSP (value))
4104 value = XCAR (value);
4105
4106 tem = XCAR (spec);
4107 if (EQ (XCAR (tem), Qmargin)
4108 && (tem = XCDR (tem),
4109 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4110 (NILP (tem)
4111 || EQ (tem, Qleft_margin)
4112 || EQ (tem, Qright_margin))))
4113 location = tem;
4114 }
4115
4116 if (EQ (location, Qunbound))
4117 {
4118 location = Qnil;
4119 value = spec;
4120 }
4121
4122 /* After this point, VALUE is the property after any
4123 margin prefix has been stripped. It must be a string,
4124 an image specification, or `(space ...)'.
4125
4126 LOCATION specifies where to display: `left-margin',
4127 `right-margin' or nil. */
4128
4129 valid_p = (STRINGP (value)
4130 #ifdef HAVE_WINDOW_SYSTEM
4131 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4132 #endif /* not HAVE_WINDOW_SYSTEM */
4133 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4134
4135 if (valid_p && !display_replaced_before_p)
4136 {
4137 /* Save current settings of IT so that we can restore them
4138 when we are finished with the glyph property value. */
4139 push_it (it);
4140
4141 if (NILP (location))
4142 it->area = TEXT_AREA;
4143 else if (EQ (location, Qleft_margin))
4144 it->area = LEFT_MARGIN_AREA;
4145 else
4146 it->area = RIGHT_MARGIN_AREA;
4147
4148 if (STRINGP (value))
4149 {
4150 if (SCHARS (value) == 0)
4151 {
4152 pop_it (it);
4153 return -1; /* Replaced by "", i.e. nothing. */
4154 }
4155 it->string = value;
4156 it->multibyte_p = STRING_MULTIBYTE (it->string);
4157 it->current.overlay_string_index = -1;
4158 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4159 it->end_charpos = it->string_nchars = SCHARS (it->string);
4160 it->method = GET_FROM_STRING;
4161 it->stop_charpos = 0;
4162 it->string_from_display_prop_p = 1;
4163 /* Say that we haven't consumed the characters with
4164 `display' property yet. The call to pop_it in
4165 set_iterator_to_next will clean this up. */
4166 *position = start_pos;
4167 }
4168 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4169 {
4170 it->method = GET_FROM_STRETCH;
4171 it->object = value;
4172 *position = it->position = start_pos;
4173 }
4174 #ifdef HAVE_WINDOW_SYSTEM
4175 else
4176 {
4177 it->what = IT_IMAGE;
4178 it->image_id = lookup_image (it->f, value);
4179 it->position = start_pos;
4180 it->object = NILP (object) ? it->w->buffer : object;
4181 it->method = GET_FROM_IMAGE;
4182
4183 /* Say that we haven't consumed the characters with
4184 `display' property yet. The call to pop_it in
4185 set_iterator_to_next will clean this up. */
4186 *position = start_pos;
4187 }
4188 #endif /* HAVE_WINDOW_SYSTEM */
4189
4190 return 1;
4191 }
4192
4193 /* Invalid property or property not supported. Restore
4194 POSITION to what it was before. */
4195 *position = start_pos;
4196 return 0;
4197 }
4198
4199
4200 /* Check if SPEC is a display specification value whose text should be
4201 treated as intangible. */
4202
4203 static int
4204 single_display_spec_intangible_p (prop)
4205 Lisp_Object prop;
4206 {
4207 /* Skip over `when FORM'. */
4208 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4209 {
4210 prop = XCDR (prop);
4211 if (!CONSP (prop))
4212 return 0;
4213 prop = XCDR (prop);
4214 }
4215
4216 if (STRINGP (prop))
4217 return 1;
4218
4219 if (!CONSP (prop))
4220 return 0;
4221
4222 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4223 we don't need to treat text as intangible. */
4224 if (EQ (XCAR (prop), Qmargin))
4225 {
4226 prop = XCDR (prop);
4227 if (!CONSP (prop))
4228 return 0;
4229
4230 prop = XCDR (prop);
4231 if (!CONSP (prop)
4232 || EQ (XCAR (prop), Qleft_margin)
4233 || EQ (XCAR (prop), Qright_margin))
4234 return 0;
4235 }
4236
4237 return (CONSP (prop)
4238 && (EQ (XCAR (prop), Qimage)
4239 || EQ (XCAR (prop), Qspace)));
4240 }
4241
4242
4243 /* Check if PROP is a display property value whose text should be
4244 treated as intangible. */
4245
4246 int
4247 display_prop_intangible_p (prop)
4248 Lisp_Object prop;
4249 {
4250 if (CONSP (prop)
4251 && CONSP (XCAR (prop))
4252 && !EQ (Qmargin, XCAR (XCAR (prop))))
4253 {
4254 /* A list of sub-properties. */
4255 while (CONSP (prop))
4256 {
4257 if (single_display_spec_intangible_p (XCAR (prop)))
4258 return 1;
4259 prop = XCDR (prop);
4260 }
4261 }
4262 else if (VECTORP (prop))
4263 {
4264 /* A vector of sub-properties. */
4265 int i;
4266 for (i = 0; i < ASIZE (prop); ++i)
4267 if (single_display_spec_intangible_p (AREF (prop, i)))
4268 return 1;
4269 }
4270 else
4271 return single_display_spec_intangible_p (prop);
4272
4273 return 0;
4274 }
4275
4276
4277 /* Return 1 if PROP is a display sub-property value containing STRING. */
4278
4279 static int
4280 single_display_spec_string_p (prop, string)
4281 Lisp_Object prop, string;
4282 {
4283 if (EQ (string, prop))
4284 return 1;
4285
4286 /* Skip over `when FORM'. */
4287 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4288 {
4289 prop = XCDR (prop);
4290 if (!CONSP (prop))
4291 return 0;
4292 prop = XCDR (prop);
4293 }
4294
4295 if (CONSP (prop))
4296 /* Skip over `margin LOCATION'. */
4297 if (EQ (XCAR (prop), Qmargin))
4298 {
4299 prop = XCDR (prop);
4300 if (!CONSP (prop))
4301 return 0;
4302
4303 prop = XCDR (prop);
4304 if (!CONSP (prop))
4305 return 0;
4306 }
4307
4308 return CONSP (prop) && EQ (XCAR (prop), string);
4309 }
4310
4311
4312 /* Return 1 if STRING appears in the `display' property PROP. */
4313
4314 static int
4315 display_prop_string_p (prop, string)
4316 Lisp_Object prop, string;
4317 {
4318 if (CONSP (prop)
4319 && CONSP (XCAR (prop))
4320 && !EQ (Qmargin, XCAR (XCAR (prop))))
4321 {
4322 /* A list of sub-properties. */
4323 while (CONSP (prop))
4324 {
4325 if (single_display_spec_string_p (XCAR (prop), string))
4326 return 1;
4327 prop = XCDR (prop);
4328 }
4329 }
4330 else if (VECTORP (prop))
4331 {
4332 /* A vector of sub-properties. */
4333 int i;
4334 for (i = 0; i < ASIZE (prop); ++i)
4335 if (single_display_spec_string_p (AREF (prop, i), string))
4336 return 1;
4337 }
4338 else
4339 return single_display_spec_string_p (prop, string);
4340
4341 return 0;
4342 }
4343
4344
4345 /* Determine from which buffer position in W's buffer STRING comes
4346 from. AROUND_CHARPOS is an approximate position where it could
4347 be from. Value is the buffer position or 0 if it couldn't be
4348 determined.
4349
4350 W's buffer must be current.
4351
4352 This function is necessary because we don't record buffer positions
4353 in glyphs generated from strings (to keep struct glyph small).
4354 This function may only use code that doesn't eval because it is
4355 called asynchronously from note_mouse_highlight. */
4356
4357 int
4358 string_buffer_position (w, string, around_charpos)
4359 struct window *w;
4360 Lisp_Object string;
4361 int around_charpos;
4362 {
4363 Lisp_Object limit, prop, pos;
4364 const int MAX_DISTANCE = 1000;
4365 int found = 0;
4366
4367 pos = make_number (around_charpos);
4368 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4369 while (!found && !EQ (pos, limit))
4370 {
4371 prop = Fget_char_property (pos, Qdisplay, Qnil);
4372 if (!NILP (prop) && display_prop_string_p (prop, string))
4373 found = 1;
4374 else
4375 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4376 }
4377
4378 if (!found)
4379 {
4380 pos = make_number (around_charpos);
4381 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4382 while (!found && !EQ (pos, limit))
4383 {
4384 prop = Fget_char_property (pos, Qdisplay, Qnil);
4385 if (!NILP (prop) && display_prop_string_p (prop, string))
4386 found = 1;
4387 else
4388 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4389 limit);
4390 }
4391 }
4392
4393 return found ? XINT (pos) : 0;
4394 }
4395
4396
4397 \f
4398 /***********************************************************************
4399 `composition' property
4400 ***********************************************************************/
4401
4402 /* Set up iterator IT from `composition' property at its current
4403 position. Called from handle_stop. */
4404
4405 static enum prop_handled
4406 handle_composition_prop (it)
4407 struct it *it;
4408 {
4409 Lisp_Object prop, string;
4410 int pos, pos_byte, end;
4411 enum prop_handled handled = HANDLED_NORMALLY;
4412
4413 if (STRINGP (it->string))
4414 {
4415 pos = IT_STRING_CHARPOS (*it);
4416 pos_byte = IT_STRING_BYTEPOS (*it);
4417 string = it->string;
4418 }
4419 else
4420 {
4421 pos = IT_CHARPOS (*it);
4422 pos_byte = IT_BYTEPOS (*it);
4423 string = Qnil;
4424 }
4425
4426 /* If there's a valid composition and point is not inside of the
4427 composition (in the case that the composition is from the current
4428 buffer), draw a glyph composed from the composition components. */
4429 if (find_composition (pos, -1, &pos, &end, &prop, string)
4430 && COMPOSITION_VALID_P (pos, end, prop)
4431 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4432 {
4433 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4434
4435 if (id >= 0)
4436 {
4437 it->method = GET_FROM_COMPOSITION;
4438 it->cmp_id = id;
4439 it->cmp_len = COMPOSITION_LENGTH (prop);
4440 /* For a terminal, draw only the first character of the
4441 components. */
4442 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4443 it->len = (STRINGP (it->string)
4444 ? string_char_to_byte (it->string, end)
4445 : CHAR_TO_BYTE (end)) - pos_byte;
4446 it->stop_charpos = end;
4447 handled = HANDLED_RETURN;
4448 }
4449 }
4450
4451 return handled;
4452 }
4453
4454
4455 \f
4456 /***********************************************************************
4457 Overlay strings
4458 ***********************************************************************/
4459
4460 /* The following structure is used to record overlay strings for
4461 later sorting in load_overlay_strings. */
4462
4463 struct overlay_entry
4464 {
4465 Lisp_Object overlay;
4466 Lisp_Object string;
4467 int priority;
4468 int after_string_p;
4469 };
4470
4471
4472 /* Set up iterator IT from overlay strings at its current position.
4473 Called from handle_stop. */
4474
4475 static enum prop_handled
4476 handle_overlay_change (it)
4477 struct it *it;
4478 {
4479 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4480 return HANDLED_RECOMPUTE_PROPS;
4481 else
4482 return HANDLED_NORMALLY;
4483 }
4484
4485
4486 /* Set up the next overlay string for delivery by IT, if there is an
4487 overlay string to deliver. Called by set_iterator_to_next when the
4488 end of the current overlay string is reached. If there are more
4489 overlay strings to display, IT->string and
4490 IT->current.overlay_string_index are set appropriately here.
4491 Otherwise IT->string is set to nil. */
4492
4493 static void
4494 next_overlay_string (it)
4495 struct it *it;
4496 {
4497 ++it->current.overlay_string_index;
4498 if (it->current.overlay_string_index == it->n_overlay_strings)
4499 {
4500 /* No more overlay strings. Restore IT's settings to what
4501 they were before overlay strings were processed, and
4502 continue to deliver from current_buffer. */
4503 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4504
4505 pop_it (it);
4506 xassert (it->stop_charpos >= BEGV
4507 && it->stop_charpos <= it->end_charpos);
4508 it->string = Qnil;
4509 it->current.overlay_string_index = -1;
4510 SET_TEXT_POS (it->current.string_pos, -1, -1);
4511 it->n_overlay_strings = 0;
4512 it->method = GET_FROM_BUFFER;
4513
4514 /* If we're at the end of the buffer, record that we have
4515 processed the overlay strings there already, so that
4516 next_element_from_buffer doesn't try it again. */
4517 if (IT_CHARPOS (*it) >= it->end_charpos)
4518 it->overlay_strings_at_end_processed_p = 1;
4519
4520 /* If we have to display `...' for invisible text, set
4521 the iterator up for that. */
4522 if (display_ellipsis_p)
4523 setup_for_ellipsis (it, 0);
4524 }
4525 else
4526 {
4527 /* There are more overlay strings to process. If
4528 IT->current.overlay_string_index has advanced to a position
4529 where we must load IT->overlay_strings with more strings, do
4530 it. */
4531 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4532
4533 if (it->current.overlay_string_index && i == 0)
4534 load_overlay_strings (it, 0);
4535
4536 /* Initialize IT to deliver display elements from the overlay
4537 string. */
4538 it->string = it->overlay_strings[i];
4539 it->multibyte_p = STRING_MULTIBYTE (it->string);
4540 SET_TEXT_POS (it->current.string_pos, 0, 0);
4541 it->method = GET_FROM_STRING;
4542 it->stop_charpos = 0;
4543 }
4544
4545 CHECK_IT (it);
4546 }
4547
4548
4549 /* Compare two overlay_entry structures E1 and E2. Used as a
4550 comparison function for qsort in load_overlay_strings. Overlay
4551 strings for the same position are sorted so that
4552
4553 1. All after-strings come in front of before-strings, except
4554 when they come from the same overlay.
4555
4556 2. Within after-strings, strings are sorted so that overlay strings
4557 from overlays with higher priorities come first.
4558
4559 2. Within before-strings, strings are sorted so that overlay
4560 strings from overlays with higher priorities come last.
4561
4562 Value is analogous to strcmp. */
4563
4564
4565 static int
4566 compare_overlay_entries (e1, e2)
4567 void *e1, *e2;
4568 {
4569 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4570 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4571 int result;
4572
4573 if (entry1->after_string_p != entry2->after_string_p)
4574 {
4575 /* Let after-strings appear in front of before-strings if
4576 they come from different overlays. */
4577 if (EQ (entry1->overlay, entry2->overlay))
4578 result = entry1->after_string_p ? 1 : -1;
4579 else
4580 result = entry1->after_string_p ? -1 : 1;
4581 }
4582 else if (entry1->after_string_p)
4583 /* After-strings sorted in order of decreasing priority. */
4584 result = entry2->priority - entry1->priority;
4585 else
4586 /* Before-strings sorted in order of increasing priority. */
4587 result = entry1->priority - entry2->priority;
4588
4589 return result;
4590 }
4591
4592
4593 /* Load the vector IT->overlay_strings with overlay strings from IT's
4594 current buffer position, or from CHARPOS if that is > 0. Set
4595 IT->n_overlays to the total number of overlay strings found.
4596
4597 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4598 a time. On entry into load_overlay_strings,
4599 IT->current.overlay_string_index gives the number of overlay
4600 strings that have already been loaded by previous calls to this
4601 function.
4602
4603 IT->add_overlay_start contains an additional overlay start
4604 position to consider for taking overlay strings from, if non-zero.
4605 This position comes into play when the overlay has an `invisible'
4606 property, and both before and after-strings. When we've skipped to
4607 the end of the overlay, because of its `invisible' property, we
4608 nevertheless want its before-string to appear.
4609 IT->add_overlay_start will contain the overlay start position
4610 in this case.
4611
4612 Overlay strings are sorted so that after-string strings come in
4613 front of before-string strings. Within before and after-strings,
4614 strings are sorted by overlay priority. See also function
4615 compare_overlay_entries. */
4616
4617 static void
4618 load_overlay_strings (it, charpos)
4619 struct it *it;
4620 int charpos;
4621 {
4622 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4623 Lisp_Object overlay, window, str, invisible;
4624 struct Lisp_Overlay *ov;
4625 int start, end;
4626 int size = 20;
4627 int n = 0, i, j, invis_p;
4628 struct overlay_entry *entries
4629 = (struct overlay_entry *) alloca (size * sizeof *entries);
4630
4631 if (charpos <= 0)
4632 charpos = IT_CHARPOS (*it);
4633
4634 /* Append the overlay string STRING of overlay OVERLAY to vector
4635 `entries' which has size `size' and currently contains `n'
4636 elements. AFTER_P non-zero means STRING is an after-string of
4637 OVERLAY. */
4638 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4639 do \
4640 { \
4641 Lisp_Object priority; \
4642 \
4643 if (n == size) \
4644 { \
4645 int new_size = 2 * size; \
4646 struct overlay_entry *old = entries; \
4647 entries = \
4648 (struct overlay_entry *) alloca (new_size \
4649 * sizeof *entries); \
4650 bcopy (old, entries, size * sizeof *entries); \
4651 size = new_size; \
4652 } \
4653 \
4654 entries[n].string = (STRING); \
4655 entries[n].overlay = (OVERLAY); \
4656 priority = Foverlay_get ((OVERLAY), Qpriority); \
4657 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4658 entries[n].after_string_p = (AFTER_P); \
4659 ++n; \
4660 } \
4661 while (0)
4662
4663 /* Process overlay before the overlay center. */
4664 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4665 {
4666 XSETMISC (overlay, ov);
4667 xassert (OVERLAYP (overlay));
4668 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4669 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4670
4671 if (end < charpos)
4672 break;
4673
4674 /* Skip this overlay if it doesn't start or end at IT's current
4675 position. */
4676 if (end != charpos && start != charpos)
4677 continue;
4678
4679 /* Skip this overlay if it doesn't apply to IT->w. */
4680 window = Foverlay_get (overlay, Qwindow);
4681 if (WINDOWP (window) && XWINDOW (window) != it->w)
4682 continue;
4683
4684 /* If the text ``under'' the overlay is invisible, both before-
4685 and after-strings from this overlay are visible; start and
4686 end position are indistinguishable. */
4687 invisible = Foverlay_get (overlay, Qinvisible);
4688 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4689
4690 /* If overlay has a non-empty before-string, record it. */
4691 if ((start == charpos || (end == charpos && invis_p))
4692 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4693 && SCHARS (str))
4694 RECORD_OVERLAY_STRING (overlay, str, 0);
4695
4696 /* If overlay has a non-empty after-string, record it. */
4697 if ((end == charpos || (start == charpos && invis_p))
4698 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4699 && SCHARS (str))
4700 RECORD_OVERLAY_STRING (overlay, str, 1);
4701 }
4702
4703 /* Process overlays after the overlay center. */
4704 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4705 {
4706 XSETMISC (overlay, ov);
4707 xassert (OVERLAYP (overlay));
4708 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4709 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4710
4711 if (start > charpos)
4712 break;
4713
4714 /* Skip this overlay if it doesn't start or end at IT's current
4715 position. */
4716 if (end != charpos && start != charpos)
4717 continue;
4718
4719 /* Skip this overlay if it doesn't apply to IT->w. */
4720 window = Foverlay_get (overlay, Qwindow);
4721 if (WINDOWP (window) && XWINDOW (window) != it->w)
4722 continue;
4723
4724 /* If the text ``under'' the overlay is invisible, it has a zero
4725 dimension, and both before- and after-strings apply. */
4726 invisible = Foverlay_get (overlay, Qinvisible);
4727 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4728
4729 /* If overlay has a non-empty before-string, record it. */
4730 if ((start == charpos || (end == charpos && invis_p))
4731 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4732 && SCHARS (str))
4733 RECORD_OVERLAY_STRING (overlay, str, 0);
4734
4735 /* If overlay has a non-empty after-string, record it. */
4736 if ((end == charpos || (start == charpos && invis_p))
4737 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4738 && SCHARS (str))
4739 RECORD_OVERLAY_STRING (overlay, str, 1);
4740 }
4741
4742 #undef RECORD_OVERLAY_STRING
4743
4744 /* Sort entries. */
4745 if (n > 1)
4746 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4747
4748 /* Record the total number of strings to process. */
4749 it->n_overlay_strings = n;
4750
4751 /* IT->current.overlay_string_index is the number of overlay strings
4752 that have already been consumed by IT. Copy some of the
4753 remaining overlay strings to IT->overlay_strings. */
4754 i = 0;
4755 j = it->current.overlay_string_index;
4756 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4757 it->overlay_strings[i++] = entries[j++].string;
4758
4759 CHECK_IT (it);
4760 }
4761
4762
4763 /* Get the first chunk of overlay strings at IT's current buffer
4764 position, or at CHARPOS if that is > 0. Value is non-zero if at
4765 least one overlay string was found. */
4766
4767 static int
4768 get_overlay_strings (it, charpos)
4769 struct it *it;
4770 int charpos;
4771 {
4772 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4773 process. This fills IT->overlay_strings with strings, and sets
4774 IT->n_overlay_strings to the total number of strings to process.
4775 IT->pos.overlay_string_index has to be set temporarily to zero
4776 because load_overlay_strings needs this; it must be set to -1
4777 when no overlay strings are found because a zero value would
4778 indicate a position in the first overlay string. */
4779 it->current.overlay_string_index = 0;
4780 load_overlay_strings (it, charpos);
4781
4782 /* If we found overlay strings, set up IT to deliver display
4783 elements from the first one. Otherwise set up IT to deliver
4784 from current_buffer. */
4785 if (it->n_overlay_strings)
4786 {
4787 /* Make sure we know settings in current_buffer, so that we can
4788 restore meaningful values when we're done with the overlay
4789 strings. */
4790 compute_stop_pos (it);
4791 xassert (it->face_id >= 0);
4792
4793 /* Save IT's settings. They are restored after all overlay
4794 strings have been processed. */
4795 xassert (it->sp == 0);
4796 push_it (it);
4797
4798 /* Set up IT to deliver display elements from the first overlay
4799 string. */
4800 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4801 it->string = it->overlay_strings[0];
4802 it->stop_charpos = 0;
4803 xassert (STRINGP (it->string));
4804 it->end_charpos = SCHARS (it->string);
4805 it->multibyte_p = STRING_MULTIBYTE (it->string);
4806 it->method = GET_FROM_STRING;
4807 }
4808 else
4809 {
4810 it->string = Qnil;
4811 it->current.overlay_string_index = -1;
4812 it->method = GET_FROM_BUFFER;
4813 }
4814
4815 CHECK_IT (it);
4816
4817 /* Value is non-zero if we found at least one overlay string. */
4818 return STRINGP (it->string);
4819 }
4820
4821
4822 \f
4823 /***********************************************************************
4824 Saving and restoring state
4825 ***********************************************************************/
4826
4827 /* Save current settings of IT on IT->stack. Called, for example,
4828 before setting up IT for an overlay string, to be able to restore
4829 IT's settings to what they were after the overlay string has been
4830 processed. */
4831
4832 static void
4833 push_it (it)
4834 struct it *it;
4835 {
4836 struct iterator_stack_entry *p;
4837
4838 xassert (it->sp < 2);
4839 p = it->stack + it->sp;
4840
4841 p->stop_charpos = it->stop_charpos;
4842 xassert (it->face_id >= 0);
4843 p->face_id = it->face_id;
4844 p->string = it->string;
4845 p->pos = it->current;
4846 p->end_charpos = it->end_charpos;
4847 p->string_nchars = it->string_nchars;
4848 p->area = it->area;
4849 p->multibyte_p = it->multibyte_p;
4850 p->slice = it->slice;
4851 p->space_width = it->space_width;
4852 p->font_height = it->font_height;
4853 p->voffset = it->voffset;
4854 p->string_from_display_prop_p = it->string_from_display_prop_p;
4855 p->display_ellipsis_p = 0;
4856 ++it->sp;
4857 }
4858
4859
4860 /* Restore IT's settings from IT->stack. Called, for example, when no
4861 more overlay strings must be processed, and we return to delivering
4862 display elements from a buffer, or when the end of a string from a
4863 `display' property is reached and we return to delivering display
4864 elements from an overlay string, or from a buffer. */
4865
4866 static void
4867 pop_it (it)
4868 struct it *it;
4869 {
4870 struct iterator_stack_entry *p;
4871
4872 xassert (it->sp > 0);
4873 --it->sp;
4874 p = it->stack + it->sp;
4875 it->stop_charpos = p->stop_charpos;
4876 it->face_id = p->face_id;
4877 it->string = p->string;
4878 it->current = p->pos;
4879 it->end_charpos = p->end_charpos;
4880 it->string_nchars = p->string_nchars;
4881 it->area = p->area;
4882 it->multibyte_p = p->multibyte_p;
4883 it->slice = p->slice;
4884 it->space_width = p->space_width;
4885 it->font_height = p->font_height;
4886 it->voffset = p->voffset;
4887 it->string_from_display_prop_p = p->string_from_display_prop_p;
4888 }
4889
4890
4891 \f
4892 /***********************************************************************
4893 Moving over lines
4894 ***********************************************************************/
4895
4896 /* Set IT's current position to the previous line start. */
4897
4898 static void
4899 back_to_previous_line_start (it)
4900 struct it *it;
4901 {
4902 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4903 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4904 }
4905
4906
4907 /* Move IT to the next line start.
4908
4909 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4910 we skipped over part of the text (as opposed to moving the iterator
4911 continuously over the text). Otherwise, don't change the value
4912 of *SKIPPED_P.
4913
4914 Newlines may come from buffer text, overlay strings, or strings
4915 displayed via the `display' property. That's the reason we can't
4916 simply use find_next_newline_no_quit.
4917
4918 Note that this function may not skip over invisible text that is so
4919 because of text properties and immediately follows a newline. If
4920 it would, function reseat_at_next_visible_line_start, when called
4921 from set_iterator_to_next, would effectively make invisible
4922 characters following a newline part of the wrong glyph row, which
4923 leads to wrong cursor motion. */
4924
4925 static int
4926 forward_to_next_line_start (it, skipped_p)
4927 struct it *it;
4928 int *skipped_p;
4929 {
4930 int old_selective, newline_found_p, n;
4931 const int MAX_NEWLINE_DISTANCE = 500;
4932
4933 /* If already on a newline, just consume it to avoid unintended
4934 skipping over invisible text below. */
4935 if (it->what == IT_CHARACTER
4936 && it->c == '\n'
4937 && CHARPOS (it->position) == IT_CHARPOS (*it))
4938 {
4939 set_iterator_to_next (it, 0);
4940 it->c = 0;
4941 return 1;
4942 }
4943
4944 /* Don't handle selective display in the following. It's (a)
4945 unnecessary because it's done by the caller, and (b) leads to an
4946 infinite recursion because next_element_from_ellipsis indirectly
4947 calls this function. */
4948 old_selective = it->selective;
4949 it->selective = 0;
4950
4951 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4952 from buffer text. */
4953 for (n = newline_found_p = 0;
4954 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4955 n += STRINGP (it->string) ? 0 : 1)
4956 {
4957 if (!get_next_display_element (it))
4958 return 0;
4959 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4960 set_iterator_to_next (it, 0);
4961 }
4962
4963 /* If we didn't find a newline near enough, see if we can use a
4964 short-cut. */
4965 if (!newline_found_p)
4966 {
4967 int start = IT_CHARPOS (*it);
4968 int limit = find_next_newline_no_quit (start, 1);
4969 Lisp_Object pos;
4970
4971 xassert (!STRINGP (it->string));
4972
4973 /* If there isn't any `display' property in sight, and no
4974 overlays, we can just use the position of the newline in
4975 buffer text. */
4976 if (it->stop_charpos >= limit
4977 || ((pos = Fnext_single_property_change (make_number (start),
4978 Qdisplay,
4979 Qnil, make_number (limit)),
4980 NILP (pos))
4981 && next_overlay_change (start) == ZV))
4982 {
4983 IT_CHARPOS (*it) = limit;
4984 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4985 *skipped_p = newline_found_p = 1;
4986 }
4987 else
4988 {
4989 while (get_next_display_element (it)
4990 && !newline_found_p)
4991 {
4992 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4993 set_iterator_to_next (it, 0);
4994 }
4995 }
4996 }
4997
4998 it->selective = old_selective;
4999 return newline_found_p;
5000 }
5001
5002
5003 /* Set IT's current position to the previous visible line start. Skip
5004 invisible text that is so either due to text properties or due to
5005 selective display. Caution: this does not change IT->current_x and
5006 IT->hpos. */
5007
5008 static void
5009 back_to_previous_visible_line_start (it)
5010 struct it *it;
5011 {
5012 while (IT_CHARPOS (*it) > BEGV)
5013 {
5014 back_to_previous_line_start (it);
5015 if (IT_CHARPOS (*it) <= BEGV)
5016 break;
5017
5018 /* If selective > 0, then lines indented more than that values
5019 are invisible. */
5020 if (it->selective > 0
5021 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5022 (double) it->selective)) /* iftc */
5023 continue;
5024
5025 /* Check the newline before point for invisibility. */
5026 {
5027 Lisp_Object prop;
5028 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5029 Qinvisible, it->window);
5030 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5031 continue;
5032 }
5033
5034 /* If newline has a display property that replaces the newline with something
5035 else (image or text), find start of overlay or interval and continue search
5036 from that point. */
5037 if (IT_CHARPOS (*it) > BEGV)
5038 {
5039 struct it it2 = *it;
5040 int pos;
5041 int beg, end;
5042 Lisp_Object val, overlay;
5043
5044 pos = --IT_CHARPOS (it2);
5045 --IT_BYTEPOS (it2);
5046 it2.sp = 0;
5047 if (handle_display_prop (&it2) == HANDLED_RETURN
5048 && !NILP (val = get_char_property_and_overlay
5049 (make_number (pos), Qdisplay, Qnil, &overlay))
5050 && (OVERLAYP (overlay)
5051 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5052 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5053 {
5054 if (beg < BEGV)
5055 beg = BEGV;
5056 IT_CHARPOS (*it) = beg;
5057 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5058 continue;
5059 }
5060 }
5061
5062 break;
5063 }
5064
5065 xassert (IT_CHARPOS (*it) >= BEGV);
5066 xassert (IT_CHARPOS (*it) == BEGV
5067 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5068 CHECK_IT (it);
5069 }
5070
5071
5072 /* Reseat iterator IT at the previous visible line start. Skip
5073 invisible text that is so either due to text properties or due to
5074 selective display. At the end, update IT's overlay information,
5075 face information etc. */
5076
5077 void
5078 reseat_at_previous_visible_line_start (it)
5079 struct it *it;
5080 {
5081 back_to_previous_visible_line_start (it);
5082 reseat (it, it->current.pos, 1);
5083 CHECK_IT (it);
5084 }
5085
5086
5087 /* Reseat iterator IT on the next visible line start in the current
5088 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5089 preceding the line start. Skip over invisible text that is so
5090 because of selective display. Compute faces, overlays etc at the
5091 new position. Note that this function does not skip over text that
5092 is invisible because of text properties. */
5093
5094 static void
5095 reseat_at_next_visible_line_start (it, on_newline_p)
5096 struct it *it;
5097 int on_newline_p;
5098 {
5099 int newline_found_p, skipped_p = 0;
5100
5101 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5102
5103 /* Skip over lines that are invisible because they are indented
5104 more than the value of IT->selective. */
5105 if (it->selective > 0)
5106 while (IT_CHARPOS (*it) < ZV
5107 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5108 (double) it->selective)) /* iftc */
5109 {
5110 xassert (IT_BYTEPOS (*it) == BEGV
5111 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5112 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5113 }
5114
5115 /* Position on the newline if that's what's requested. */
5116 if (on_newline_p && newline_found_p)
5117 {
5118 if (STRINGP (it->string))
5119 {
5120 if (IT_STRING_CHARPOS (*it) > 0)
5121 {
5122 --IT_STRING_CHARPOS (*it);
5123 --IT_STRING_BYTEPOS (*it);
5124 }
5125 }
5126 else if (IT_CHARPOS (*it) > BEGV)
5127 {
5128 --IT_CHARPOS (*it);
5129 --IT_BYTEPOS (*it);
5130 reseat (it, it->current.pos, 0);
5131 }
5132 }
5133 else if (skipped_p)
5134 reseat (it, it->current.pos, 0);
5135
5136 CHECK_IT (it);
5137 }
5138
5139
5140 \f
5141 /***********************************************************************
5142 Changing an iterator's position
5143 ***********************************************************************/
5144
5145 /* Change IT's current position to POS in current_buffer. If FORCE_P
5146 is non-zero, always check for text properties at the new position.
5147 Otherwise, text properties are only looked up if POS >=
5148 IT->check_charpos of a property. */
5149
5150 static void
5151 reseat (it, pos, force_p)
5152 struct it *it;
5153 struct text_pos pos;
5154 int force_p;
5155 {
5156 int original_pos = IT_CHARPOS (*it);
5157
5158 reseat_1 (it, pos, 0);
5159
5160 /* Determine where to check text properties. Avoid doing it
5161 where possible because text property lookup is very expensive. */
5162 if (force_p
5163 || CHARPOS (pos) > it->stop_charpos
5164 || CHARPOS (pos) < original_pos)
5165 handle_stop (it);
5166
5167 CHECK_IT (it);
5168 }
5169
5170
5171 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5172 IT->stop_pos to POS, also. */
5173
5174 static void
5175 reseat_1 (it, pos, set_stop_p)
5176 struct it *it;
5177 struct text_pos pos;
5178 int set_stop_p;
5179 {
5180 /* Don't call this function when scanning a C string. */
5181 xassert (it->s == NULL);
5182
5183 /* POS must be a reasonable value. */
5184 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5185
5186 it->current.pos = it->position = pos;
5187 XSETBUFFER (it->object, current_buffer);
5188 it->end_charpos = ZV;
5189 it->dpvec = NULL;
5190 it->current.dpvec_index = -1;
5191 it->current.overlay_string_index = -1;
5192 IT_STRING_CHARPOS (*it) = -1;
5193 IT_STRING_BYTEPOS (*it) = -1;
5194 it->string = Qnil;
5195 it->method = GET_FROM_BUFFER;
5196 /* RMS: I added this to fix a bug in move_it_vertically_backward
5197 where it->area continued to relate to the starting point
5198 for the backward motion. Bug report from
5199 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5200 However, I am not sure whether reseat still does the right thing
5201 in general after this change. */
5202 it->area = TEXT_AREA;
5203 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5204 it->sp = 0;
5205 it->face_before_selective_p = 0;
5206
5207 if (set_stop_p)
5208 it->stop_charpos = CHARPOS (pos);
5209 }
5210
5211
5212 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5213 If S is non-null, it is a C string to iterate over. Otherwise,
5214 STRING gives a Lisp string to iterate over.
5215
5216 If PRECISION > 0, don't return more then PRECISION number of
5217 characters from the string.
5218
5219 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5220 characters have been returned. FIELD_WIDTH < 0 means an infinite
5221 field width.
5222
5223 MULTIBYTE = 0 means disable processing of multibyte characters,
5224 MULTIBYTE > 0 means enable it,
5225 MULTIBYTE < 0 means use IT->multibyte_p.
5226
5227 IT must be initialized via a prior call to init_iterator before
5228 calling this function. */
5229
5230 static void
5231 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5232 struct it *it;
5233 unsigned char *s;
5234 Lisp_Object string;
5235 int charpos;
5236 int precision, field_width, multibyte;
5237 {
5238 /* No region in strings. */
5239 it->region_beg_charpos = it->region_end_charpos = -1;
5240
5241 /* No text property checks performed by default, but see below. */
5242 it->stop_charpos = -1;
5243
5244 /* Set iterator position and end position. */
5245 bzero (&it->current, sizeof it->current);
5246 it->current.overlay_string_index = -1;
5247 it->current.dpvec_index = -1;
5248 xassert (charpos >= 0);
5249
5250 /* If STRING is specified, use its multibyteness, otherwise use the
5251 setting of MULTIBYTE, if specified. */
5252 if (multibyte >= 0)
5253 it->multibyte_p = multibyte > 0;
5254
5255 if (s == NULL)
5256 {
5257 xassert (STRINGP (string));
5258 it->string = string;
5259 it->s = NULL;
5260 it->end_charpos = it->string_nchars = SCHARS (string);
5261 it->method = GET_FROM_STRING;
5262 it->current.string_pos = string_pos (charpos, string);
5263 }
5264 else
5265 {
5266 it->s = s;
5267 it->string = Qnil;
5268
5269 /* Note that we use IT->current.pos, not it->current.string_pos,
5270 for displaying C strings. */
5271 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5272 if (it->multibyte_p)
5273 {
5274 it->current.pos = c_string_pos (charpos, s, 1);
5275 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5276 }
5277 else
5278 {
5279 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5280 it->end_charpos = it->string_nchars = strlen (s);
5281 }
5282
5283 it->method = GET_FROM_C_STRING;
5284 }
5285
5286 /* PRECISION > 0 means don't return more than PRECISION characters
5287 from the string. */
5288 if (precision > 0 && it->end_charpos - charpos > precision)
5289 it->end_charpos = it->string_nchars = charpos + precision;
5290
5291 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5292 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5293 FIELD_WIDTH < 0 means infinite field width. This is useful for
5294 padding with `-' at the end of a mode line. */
5295 if (field_width < 0)
5296 field_width = INFINITY;
5297 if (field_width > it->end_charpos - charpos)
5298 it->end_charpos = charpos + field_width;
5299
5300 /* Use the standard display table for displaying strings. */
5301 if (DISP_TABLE_P (Vstandard_display_table))
5302 it->dp = XCHAR_TABLE (Vstandard_display_table);
5303
5304 it->stop_charpos = charpos;
5305 CHECK_IT (it);
5306 }
5307
5308
5309 \f
5310 /***********************************************************************
5311 Iteration
5312 ***********************************************************************/
5313
5314 /* Map enum it_method value to corresponding next_element_from_* function. */
5315
5316 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5317 {
5318 next_element_from_buffer,
5319 next_element_from_display_vector,
5320 next_element_from_composition,
5321 next_element_from_string,
5322 next_element_from_c_string,
5323 next_element_from_image,
5324 next_element_from_stretch
5325 };
5326
5327
5328 /* Load IT's display element fields with information about the next
5329 display element from the current position of IT. Value is zero if
5330 end of buffer (or C string) is reached. */
5331
5332 static struct frame *last_escape_glyph_frame = NULL;
5333 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5334 static int last_escape_glyph_merged_face_id = 0;
5335
5336 int
5337 get_next_display_element (it)
5338 struct it *it;
5339 {
5340 /* Non-zero means that we found a display element. Zero means that
5341 we hit the end of what we iterate over. Performance note: the
5342 function pointer `method' used here turns out to be faster than
5343 using a sequence of if-statements. */
5344 int success_p;
5345
5346 get_next:
5347 success_p = (*get_next_element[it->method]) (it);
5348
5349 if (it->what == IT_CHARACTER)
5350 {
5351 /* Map via display table or translate control characters.
5352 IT->c, IT->len etc. have been set to the next character by
5353 the function call above. If we have a display table, and it
5354 contains an entry for IT->c, translate it. Don't do this if
5355 IT->c itself comes from a display table, otherwise we could
5356 end up in an infinite recursion. (An alternative could be to
5357 count the recursion depth of this function and signal an
5358 error when a certain maximum depth is reached.) Is it worth
5359 it? */
5360 if (success_p && it->dpvec == NULL)
5361 {
5362 Lisp_Object dv;
5363
5364 if (it->dp
5365 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5366 VECTORP (dv)))
5367 {
5368 struct Lisp_Vector *v = XVECTOR (dv);
5369
5370 /* Return the first character from the display table
5371 entry, if not empty. If empty, don't display the
5372 current character. */
5373 if (v->size)
5374 {
5375 it->dpvec_char_len = it->len;
5376 it->dpvec = v->contents;
5377 it->dpend = v->contents + v->size;
5378 it->current.dpvec_index = 0;
5379 it->dpvec_face_id = -1;
5380 it->saved_face_id = it->face_id;
5381 it->method = GET_FROM_DISPLAY_VECTOR;
5382 it->ellipsis_p = 0;
5383 }
5384 else
5385 {
5386 set_iterator_to_next (it, 0);
5387 }
5388 goto get_next;
5389 }
5390
5391 /* Translate control characters into `\003' or `^C' form.
5392 Control characters coming from a display table entry are
5393 currently not translated because we use IT->dpvec to hold
5394 the translation. This could easily be changed but I
5395 don't believe that it is worth doing.
5396
5397 If it->multibyte_p is nonzero, eight-bit characters and
5398 non-printable multibyte characters are also translated to
5399 octal form.
5400
5401 If it->multibyte_p is zero, eight-bit characters that
5402 don't have corresponding multibyte char code are also
5403 translated to octal form. */
5404 else if ((it->c < ' '
5405 && (it->area != TEXT_AREA
5406 /* In mode line, treat \n like other crl chars. */
5407 || (it->c != '\t'
5408 && it->glyph_row && it->glyph_row->mode_line_p)
5409 || (it->c != '\n' && it->c != '\t')))
5410 || (it->multibyte_p
5411 ? ((it->c >= 127
5412 && it->len == 1)
5413 || !CHAR_PRINTABLE_P (it->c)
5414 || (!NILP (Vnobreak_char_display)
5415 && (it->c == 0x8a0 || it->c == 0x8ad
5416 || it->c == 0x920 || it->c == 0x92d
5417 || it->c == 0xe20 || it->c == 0xe2d
5418 || it->c == 0xf20 || it->c == 0xf2d)))
5419 : (it->c >= 127
5420 && (!unibyte_display_via_language_environment
5421 || it->c == unibyte_char_to_multibyte (it->c)))))
5422 {
5423 /* IT->c is a control character which must be displayed
5424 either as '\003' or as `^C' where the '\\' and '^'
5425 can be defined in the display table. Fill
5426 IT->ctl_chars with glyphs for what we have to
5427 display. Then, set IT->dpvec to these glyphs. */
5428 GLYPH g;
5429 int ctl_len;
5430 int face_id, lface_id = 0 ;
5431 GLYPH escape_glyph;
5432
5433 /* Handle control characters with ^. */
5434
5435 if (it->c < 128 && it->ctl_arrow_p)
5436 {
5437 g = '^'; /* default glyph for Control */
5438 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5439 if (it->dp
5440 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5441 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5442 {
5443 g = XINT (DISP_CTRL_GLYPH (it->dp));
5444 lface_id = FAST_GLYPH_FACE (g);
5445 }
5446 if (lface_id)
5447 {
5448 g = FAST_GLYPH_CHAR (g);
5449 face_id = merge_faces (it->f, Qt, lface_id,
5450 it->face_id);
5451 }
5452 else if (it->f == last_escape_glyph_frame
5453 && it->face_id == last_escape_glyph_face_id)
5454 {
5455 face_id = last_escape_glyph_merged_face_id;
5456 }
5457 else
5458 {
5459 /* Merge the escape-glyph face into the current face. */
5460 face_id = merge_faces (it->f, Qescape_glyph, 0,
5461 it->face_id);
5462 last_escape_glyph_frame = it->f;
5463 last_escape_glyph_face_id = it->face_id;
5464 last_escape_glyph_merged_face_id = face_id;
5465 }
5466
5467 XSETINT (it->ctl_chars[0], g);
5468 g = it->c ^ 0100;
5469 XSETINT (it->ctl_chars[1], g);
5470 ctl_len = 2;
5471 goto display_control;
5472 }
5473
5474 /* Handle non-break space in the mode where it only gets
5475 highlighting. */
5476
5477 if (EQ (Vnobreak_char_display, Qt)
5478 && (it->c == 0x8a0 || it->c == 0x920
5479 || it->c == 0xe20 || it->c == 0xf20))
5480 {
5481 /* Merge the no-break-space face into the current face. */
5482 face_id = merge_faces (it->f, Qnobreak_space, 0,
5483 it->face_id);
5484
5485 g = it->c = ' ';
5486 XSETINT (it->ctl_chars[0], g);
5487 ctl_len = 1;
5488 goto display_control;
5489 }
5490
5491 /* Handle sequences that start with the "escape glyph". */
5492
5493 /* the default escape glyph is \. */
5494 escape_glyph = '\\';
5495
5496 if (it->dp
5497 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5498 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5499 {
5500 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5501 lface_id = FAST_GLYPH_FACE (escape_glyph);
5502 }
5503 if (lface_id)
5504 {
5505 /* The display table specified a face.
5506 Merge it into face_id and also into escape_glyph. */
5507 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5508 face_id = merge_faces (it->f, Qt, lface_id,
5509 it->face_id);
5510 }
5511 else if (it->f == last_escape_glyph_frame
5512 && it->face_id == last_escape_glyph_face_id)
5513 {
5514 face_id = last_escape_glyph_merged_face_id;
5515 }
5516 else
5517 {
5518 /* Merge the escape-glyph face into the current face. */
5519 face_id = merge_faces (it->f, Qescape_glyph, 0,
5520 it->face_id);
5521 last_escape_glyph_frame = it->f;
5522 last_escape_glyph_face_id = it->face_id;
5523 last_escape_glyph_merged_face_id = face_id;
5524 }
5525
5526 /* Handle soft hyphens in the mode where they only get
5527 highlighting. */
5528
5529 if (EQ (Vnobreak_char_display, Qt)
5530 && (it->c == 0x8ad || it->c == 0x92d
5531 || it->c == 0xe2d || it->c == 0xf2d))
5532 {
5533 g = it->c = '-';
5534 XSETINT (it->ctl_chars[0], g);
5535 ctl_len = 1;
5536 goto display_control;
5537 }
5538
5539 /* Handle non-break space and soft hyphen
5540 with the escape glyph. */
5541
5542 if (it->c == 0x8a0 || it->c == 0x8ad
5543 || it->c == 0x920 || it->c == 0x92d
5544 || it->c == 0xe20 || it->c == 0xe2d
5545 || it->c == 0xf20 || it->c == 0xf2d)
5546 {
5547 XSETINT (it->ctl_chars[0], escape_glyph);
5548 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5549 XSETINT (it->ctl_chars[1], g);
5550 ctl_len = 2;
5551 goto display_control;
5552 }
5553
5554 {
5555 unsigned char str[MAX_MULTIBYTE_LENGTH];
5556 int len;
5557 int i;
5558
5559 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5560 if (SINGLE_BYTE_CHAR_P (it->c))
5561 str[0] = it->c, len = 1;
5562 else
5563 {
5564 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5565 if (len < 0)
5566 {
5567 /* It's an invalid character, which shouldn't
5568 happen actually, but due to bugs it may
5569 happen. Let's print the char as is, there's
5570 not much meaningful we can do with it. */
5571 str[0] = it->c;
5572 str[1] = it->c >> 8;
5573 str[2] = it->c >> 16;
5574 str[3] = it->c >> 24;
5575 len = 4;
5576 }
5577 }
5578
5579 for (i = 0; i < len; i++)
5580 {
5581 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5582 /* Insert three more glyphs into IT->ctl_chars for
5583 the octal display of the character. */
5584 g = ((str[i] >> 6) & 7) + '0';
5585 XSETINT (it->ctl_chars[i * 4 + 1], g);
5586 g = ((str[i] >> 3) & 7) + '0';
5587 XSETINT (it->ctl_chars[i * 4 + 2], g);
5588 g = (str[i] & 7) + '0';
5589 XSETINT (it->ctl_chars[i * 4 + 3], g);
5590 }
5591 ctl_len = len * 4;
5592 }
5593
5594 display_control:
5595 /* Set up IT->dpvec and return first character from it. */
5596 it->dpvec_char_len = it->len;
5597 it->dpvec = it->ctl_chars;
5598 it->dpend = it->dpvec + ctl_len;
5599 it->current.dpvec_index = 0;
5600 it->dpvec_face_id = face_id;
5601 it->saved_face_id = it->face_id;
5602 it->method = GET_FROM_DISPLAY_VECTOR;
5603 it->ellipsis_p = 0;
5604 goto get_next;
5605 }
5606 }
5607
5608 /* Adjust face id for a multibyte character. There are no
5609 multibyte character in unibyte text. */
5610 if (it->multibyte_p
5611 && success_p
5612 && FRAME_WINDOW_P (it->f))
5613 {
5614 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5615 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5616 }
5617 }
5618
5619 /* Is this character the last one of a run of characters with
5620 box? If yes, set IT->end_of_box_run_p to 1. */
5621 if (it->face_box_p
5622 && it->s == NULL)
5623 {
5624 int face_id;
5625 struct face *face;
5626
5627 it->end_of_box_run_p
5628 = ((face_id = face_after_it_pos (it),
5629 face_id != it->face_id)
5630 && (face = FACE_FROM_ID (it->f, face_id),
5631 face->box == FACE_NO_BOX));
5632 }
5633
5634 /* Value is 0 if end of buffer or string reached. */
5635 return success_p;
5636 }
5637
5638
5639 /* Move IT to the next display element.
5640
5641 RESEAT_P non-zero means if called on a newline in buffer text,
5642 skip to the next visible line start.
5643
5644 Functions get_next_display_element and set_iterator_to_next are
5645 separate because I find this arrangement easier to handle than a
5646 get_next_display_element function that also increments IT's
5647 position. The way it is we can first look at an iterator's current
5648 display element, decide whether it fits on a line, and if it does,
5649 increment the iterator position. The other way around we probably
5650 would either need a flag indicating whether the iterator has to be
5651 incremented the next time, or we would have to implement a
5652 decrement position function which would not be easy to write. */
5653
5654 void
5655 set_iterator_to_next (it, reseat_p)
5656 struct it *it;
5657 int reseat_p;
5658 {
5659 /* Reset flags indicating start and end of a sequence of characters
5660 with box. Reset them at the start of this function because
5661 moving the iterator to a new position might set them. */
5662 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5663
5664 switch (it->method)
5665 {
5666 case GET_FROM_BUFFER:
5667 /* The current display element of IT is a character from
5668 current_buffer. Advance in the buffer, and maybe skip over
5669 invisible lines that are so because of selective display. */
5670 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5671 reseat_at_next_visible_line_start (it, 0);
5672 else
5673 {
5674 xassert (it->len != 0);
5675 IT_BYTEPOS (*it) += it->len;
5676 IT_CHARPOS (*it) += 1;
5677 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5678 }
5679 break;
5680
5681 case GET_FROM_COMPOSITION:
5682 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5683 if (STRINGP (it->string))
5684 {
5685 IT_STRING_BYTEPOS (*it) += it->len;
5686 IT_STRING_CHARPOS (*it) += it->cmp_len;
5687 it->method = GET_FROM_STRING;
5688 goto consider_string_end;
5689 }
5690 else
5691 {
5692 IT_BYTEPOS (*it) += it->len;
5693 IT_CHARPOS (*it) += it->cmp_len;
5694 it->method = GET_FROM_BUFFER;
5695 }
5696 break;
5697
5698 case GET_FROM_C_STRING:
5699 /* Current display element of IT is from a C string. */
5700 IT_BYTEPOS (*it) += it->len;
5701 IT_CHARPOS (*it) += 1;
5702 break;
5703
5704 case GET_FROM_DISPLAY_VECTOR:
5705 /* Current display element of IT is from a display table entry.
5706 Advance in the display table definition. Reset it to null if
5707 end reached, and continue with characters from buffers/
5708 strings. */
5709 ++it->current.dpvec_index;
5710
5711 /* Restore face of the iterator to what they were before the
5712 display vector entry (these entries may contain faces). */
5713 it->face_id = it->saved_face_id;
5714
5715 if (it->dpvec + it->current.dpvec_index == it->dpend)
5716 {
5717 int recheck_faces = it->ellipsis_p;
5718
5719 if (it->s)
5720 it->method = GET_FROM_C_STRING;
5721 else if (STRINGP (it->string))
5722 it->method = GET_FROM_STRING;
5723 else
5724 it->method = GET_FROM_BUFFER;
5725
5726 it->dpvec = NULL;
5727 it->current.dpvec_index = -1;
5728
5729 /* Skip over characters which were displayed via IT->dpvec. */
5730 if (it->dpvec_char_len < 0)
5731 reseat_at_next_visible_line_start (it, 1);
5732 else if (it->dpvec_char_len > 0)
5733 {
5734 if (it->method == GET_FROM_STRING
5735 && it->n_overlay_strings > 0)
5736 it->ignore_overlay_strings_at_pos_p = 1;
5737 it->len = it->dpvec_char_len;
5738 set_iterator_to_next (it, reseat_p);
5739 }
5740
5741 /* Maybe recheck faces after display vector */
5742 if (recheck_faces)
5743 it->stop_charpos = IT_CHARPOS (*it);
5744 }
5745 break;
5746
5747 case GET_FROM_STRING:
5748 /* Current display element is a character from a Lisp string. */
5749 xassert (it->s == NULL && STRINGP (it->string));
5750 IT_STRING_BYTEPOS (*it) += it->len;
5751 IT_STRING_CHARPOS (*it) += 1;
5752
5753 consider_string_end:
5754
5755 if (it->current.overlay_string_index >= 0)
5756 {
5757 /* IT->string is an overlay string. Advance to the
5758 next, if there is one. */
5759 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5760 next_overlay_string (it);
5761 }
5762 else
5763 {
5764 /* IT->string is not an overlay string. If we reached
5765 its end, and there is something on IT->stack, proceed
5766 with what is on the stack. This can be either another
5767 string, this time an overlay string, or a buffer. */
5768 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5769 && it->sp > 0)
5770 {
5771 pop_it (it);
5772 if (STRINGP (it->string))
5773 goto consider_string_end;
5774 it->method = GET_FROM_BUFFER;
5775 }
5776 }
5777 break;
5778
5779 case GET_FROM_IMAGE:
5780 case GET_FROM_STRETCH:
5781 /* The position etc with which we have to proceed are on
5782 the stack. The position may be at the end of a string,
5783 if the `display' property takes up the whole string. */
5784 xassert (it->sp > 0);
5785 pop_it (it);
5786 it->image_id = 0;
5787 if (STRINGP (it->string))
5788 {
5789 it->method = GET_FROM_STRING;
5790 goto consider_string_end;
5791 }
5792 it->method = GET_FROM_BUFFER;
5793 break;
5794
5795 default:
5796 /* There are no other methods defined, so this should be a bug. */
5797 abort ();
5798 }
5799
5800 xassert (it->method != GET_FROM_STRING
5801 || (STRINGP (it->string)
5802 && IT_STRING_CHARPOS (*it) >= 0));
5803 }
5804
5805 /* Load IT's display element fields with information about the next
5806 display element which comes from a display table entry or from the
5807 result of translating a control character to one of the forms `^C'
5808 or `\003'.
5809
5810 IT->dpvec holds the glyphs to return as characters.
5811 IT->saved_face_id holds the face id before the display vector--
5812 it is restored into IT->face_idin set_iterator_to_next. */
5813
5814 static int
5815 next_element_from_display_vector (it)
5816 struct it *it;
5817 {
5818 /* Precondition. */
5819 xassert (it->dpvec && it->current.dpvec_index >= 0);
5820
5821 it->face_id = it->saved_face_id;
5822
5823 if (INTEGERP (*it->dpvec)
5824 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5825 {
5826 GLYPH g;
5827
5828 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5829 it->c = FAST_GLYPH_CHAR (g);
5830 it->len = CHAR_BYTES (it->c);
5831
5832 /* The entry may contain a face id to use. Such a face id is
5833 the id of a Lisp face, not a realized face. A face id of
5834 zero means no face is specified. */
5835 if (it->dpvec_face_id >= 0)
5836 it->face_id = it->dpvec_face_id;
5837 else
5838 {
5839 int lface_id = FAST_GLYPH_FACE (g);
5840 if (lface_id > 0)
5841 it->face_id = merge_faces (it->f, Qt, lface_id,
5842 it->saved_face_id);
5843 }
5844 }
5845 else
5846 /* Display table entry is invalid. Return a space. */
5847 it->c = ' ', it->len = 1;
5848
5849 /* Don't change position and object of the iterator here. They are
5850 still the values of the character that had this display table
5851 entry or was translated, and that's what we want. */
5852 it->what = IT_CHARACTER;
5853 return 1;
5854 }
5855
5856
5857 /* Load IT with the next display element from Lisp string IT->string.
5858 IT->current.string_pos is the current position within the string.
5859 If IT->current.overlay_string_index >= 0, the Lisp string is an
5860 overlay string. */
5861
5862 static int
5863 next_element_from_string (it)
5864 struct it *it;
5865 {
5866 struct text_pos position;
5867
5868 xassert (STRINGP (it->string));
5869 xassert (IT_STRING_CHARPOS (*it) >= 0);
5870 position = it->current.string_pos;
5871
5872 /* Time to check for invisible text? */
5873 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5874 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5875 {
5876 handle_stop (it);
5877
5878 /* Since a handler may have changed IT->method, we must
5879 recurse here. */
5880 return get_next_display_element (it);
5881 }
5882
5883 if (it->current.overlay_string_index >= 0)
5884 {
5885 /* Get the next character from an overlay string. In overlay
5886 strings, There is no field width or padding with spaces to
5887 do. */
5888 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5889 {
5890 it->what = IT_EOB;
5891 return 0;
5892 }
5893 else if (STRING_MULTIBYTE (it->string))
5894 {
5895 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5896 const unsigned char *s = (SDATA (it->string)
5897 + IT_STRING_BYTEPOS (*it));
5898 it->c = string_char_and_length (s, remaining, &it->len);
5899 }
5900 else
5901 {
5902 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5903 it->len = 1;
5904 }
5905 }
5906 else
5907 {
5908 /* Get the next character from a Lisp string that is not an
5909 overlay string. Such strings come from the mode line, for
5910 example. We may have to pad with spaces, or truncate the
5911 string. See also next_element_from_c_string. */
5912 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5913 {
5914 it->what = IT_EOB;
5915 return 0;
5916 }
5917 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5918 {
5919 /* Pad with spaces. */
5920 it->c = ' ', it->len = 1;
5921 CHARPOS (position) = BYTEPOS (position) = -1;
5922 }
5923 else if (STRING_MULTIBYTE (it->string))
5924 {
5925 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5926 const unsigned char *s = (SDATA (it->string)
5927 + IT_STRING_BYTEPOS (*it));
5928 it->c = string_char_and_length (s, maxlen, &it->len);
5929 }
5930 else
5931 {
5932 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5933 it->len = 1;
5934 }
5935 }
5936
5937 /* Record what we have and where it came from. Note that we store a
5938 buffer position in IT->position although it could arguably be a
5939 string position. */
5940 it->what = IT_CHARACTER;
5941 it->object = it->string;
5942 it->position = position;
5943 return 1;
5944 }
5945
5946
5947 /* Load IT with next display element from C string IT->s.
5948 IT->string_nchars is the maximum number of characters to return
5949 from the string. IT->end_charpos may be greater than
5950 IT->string_nchars when this function is called, in which case we
5951 may have to return padding spaces. Value is zero if end of string
5952 reached, including padding spaces. */
5953
5954 static int
5955 next_element_from_c_string (it)
5956 struct it *it;
5957 {
5958 int success_p = 1;
5959
5960 xassert (it->s);
5961 it->what = IT_CHARACTER;
5962 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5963 it->object = Qnil;
5964
5965 /* IT's position can be greater IT->string_nchars in case a field
5966 width or precision has been specified when the iterator was
5967 initialized. */
5968 if (IT_CHARPOS (*it) >= it->end_charpos)
5969 {
5970 /* End of the game. */
5971 it->what = IT_EOB;
5972 success_p = 0;
5973 }
5974 else if (IT_CHARPOS (*it) >= it->string_nchars)
5975 {
5976 /* Pad with spaces. */
5977 it->c = ' ', it->len = 1;
5978 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5979 }
5980 else if (it->multibyte_p)
5981 {
5982 /* Implementation note: The calls to strlen apparently aren't a
5983 performance problem because there is no noticeable performance
5984 difference between Emacs running in unibyte or multibyte mode. */
5985 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5986 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5987 maxlen, &it->len);
5988 }
5989 else
5990 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5991
5992 return success_p;
5993 }
5994
5995
5996 /* Set up IT to return characters from an ellipsis, if appropriate.
5997 The definition of the ellipsis glyphs may come from a display table
5998 entry. This function Fills IT with the first glyph from the
5999 ellipsis if an ellipsis is to be displayed. */
6000
6001 static int
6002 next_element_from_ellipsis (it)
6003 struct it *it;
6004 {
6005 if (it->selective_display_ellipsis_p)
6006 setup_for_ellipsis (it, it->len);
6007 else
6008 {
6009 /* The face at the current position may be different from the
6010 face we find after the invisible text. Remember what it
6011 was in IT->saved_face_id, and signal that it's there by
6012 setting face_before_selective_p. */
6013 it->saved_face_id = it->face_id;
6014 it->method = GET_FROM_BUFFER;
6015 reseat_at_next_visible_line_start (it, 1);
6016 it->face_before_selective_p = 1;
6017 }
6018
6019 return get_next_display_element (it);
6020 }
6021
6022
6023 /* Deliver an image display element. The iterator IT is already
6024 filled with image information (done in handle_display_prop). Value
6025 is always 1. */
6026
6027
6028 static int
6029 next_element_from_image (it)
6030 struct it *it;
6031 {
6032 it->what = IT_IMAGE;
6033 return 1;
6034 }
6035
6036
6037 /* Fill iterator IT with next display element from a stretch glyph
6038 property. IT->object is the value of the text property. Value is
6039 always 1. */
6040
6041 static int
6042 next_element_from_stretch (it)
6043 struct it *it;
6044 {
6045 it->what = IT_STRETCH;
6046 return 1;
6047 }
6048
6049
6050 /* Load IT with the next display element from current_buffer. Value
6051 is zero if end of buffer reached. IT->stop_charpos is the next
6052 position at which to stop and check for text properties or buffer
6053 end. */
6054
6055 static int
6056 next_element_from_buffer (it)
6057 struct it *it;
6058 {
6059 int success_p = 1;
6060
6061 /* Check this assumption, otherwise, we would never enter the
6062 if-statement, below. */
6063 xassert (IT_CHARPOS (*it) >= BEGV
6064 && IT_CHARPOS (*it) <= it->stop_charpos);
6065
6066 if (IT_CHARPOS (*it) >= it->stop_charpos)
6067 {
6068 if (IT_CHARPOS (*it) >= it->end_charpos)
6069 {
6070 int overlay_strings_follow_p;
6071
6072 /* End of the game, except when overlay strings follow that
6073 haven't been returned yet. */
6074 if (it->overlay_strings_at_end_processed_p)
6075 overlay_strings_follow_p = 0;
6076 else
6077 {
6078 it->overlay_strings_at_end_processed_p = 1;
6079 overlay_strings_follow_p = get_overlay_strings (it, 0);
6080 }
6081
6082 if (overlay_strings_follow_p)
6083 success_p = get_next_display_element (it);
6084 else
6085 {
6086 it->what = IT_EOB;
6087 it->position = it->current.pos;
6088 success_p = 0;
6089 }
6090 }
6091 else
6092 {
6093 handle_stop (it);
6094 return get_next_display_element (it);
6095 }
6096 }
6097 else
6098 {
6099 /* No face changes, overlays etc. in sight, so just return a
6100 character from current_buffer. */
6101 unsigned char *p;
6102
6103 /* Maybe run the redisplay end trigger hook. Performance note:
6104 This doesn't seem to cost measurable time. */
6105 if (it->redisplay_end_trigger_charpos
6106 && it->glyph_row
6107 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6108 run_redisplay_end_trigger_hook (it);
6109
6110 /* Get the next character, maybe multibyte. */
6111 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6112 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6113 {
6114 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6115 - IT_BYTEPOS (*it));
6116 it->c = string_char_and_length (p, maxlen, &it->len);
6117 }
6118 else
6119 it->c = *p, it->len = 1;
6120
6121 /* Record what we have and where it came from. */
6122 it->what = IT_CHARACTER;;
6123 it->object = it->w->buffer;
6124 it->position = it->current.pos;
6125
6126 /* Normally we return the character found above, except when we
6127 really want to return an ellipsis for selective display. */
6128 if (it->selective)
6129 {
6130 if (it->c == '\n')
6131 {
6132 /* A value of selective > 0 means hide lines indented more
6133 than that number of columns. */
6134 if (it->selective > 0
6135 && IT_CHARPOS (*it) + 1 < ZV
6136 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6137 IT_BYTEPOS (*it) + 1,
6138 (double) it->selective)) /* iftc */
6139 {
6140 success_p = next_element_from_ellipsis (it);
6141 it->dpvec_char_len = -1;
6142 }
6143 }
6144 else if (it->c == '\r' && it->selective == -1)
6145 {
6146 /* A value of selective == -1 means that everything from the
6147 CR to the end of the line is invisible, with maybe an
6148 ellipsis displayed for it. */
6149 success_p = next_element_from_ellipsis (it);
6150 it->dpvec_char_len = -1;
6151 }
6152 }
6153 }
6154
6155 /* Value is zero if end of buffer reached. */
6156 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6157 return success_p;
6158 }
6159
6160
6161 /* Run the redisplay end trigger hook for IT. */
6162
6163 static void
6164 run_redisplay_end_trigger_hook (it)
6165 struct it *it;
6166 {
6167 Lisp_Object args[3];
6168
6169 /* IT->glyph_row should be non-null, i.e. we should be actually
6170 displaying something, or otherwise we should not run the hook. */
6171 xassert (it->glyph_row);
6172
6173 /* Set up hook arguments. */
6174 args[0] = Qredisplay_end_trigger_functions;
6175 args[1] = it->window;
6176 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6177 it->redisplay_end_trigger_charpos = 0;
6178
6179 /* Since we are *trying* to run these functions, don't try to run
6180 them again, even if they get an error. */
6181 it->w->redisplay_end_trigger = Qnil;
6182 Frun_hook_with_args (3, args);
6183
6184 /* Notice if it changed the face of the character we are on. */
6185 handle_face_prop (it);
6186 }
6187
6188
6189 /* Deliver a composition display element. The iterator IT is already
6190 filled with composition information (done in
6191 handle_composition_prop). Value is always 1. */
6192
6193 static int
6194 next_element_from_composition (it)
6195 struct it *it;
6196 {
6197 it->what = IT_COMPOSITION;
6198 it->position = (STRINGP (it->string)
6199 ? it->current.string_pos
6200 : it->current.pos);
6201 return 1;
6202 }
6203
6204
6205 \f
6206 /***********************************************************************
6207 Moving an iterator without producing glyphs
6208 ***********************************************************************/
6209
6210 /* Check if iterator is at a position corresponding to a valid buffer
6211 position after some move_it_ call. */
6212
6213 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6214 ((it)->method == GET_FROM_STRING \
6215 ? IT_STRING_CHARPOS (*it) == 0 \
6216 : 1)
6217
6218
6219 /* Move iterator IT to a specified buffer or X position within one
6220 line on the display without producing glyphs.
6221
6222 OP should be a bit mask including some or all of these bits:
6223 MOVE_TO_X: Stop on reaching x-position TO_X.
6224 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6225 Regardless of OP's value, stop in reaching the end of the display line.
6226
6227 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6228 This means, in particular, that TO_X includes window's horizontal
6229 scroll amount.
6230
6231 The return value has several possible values that
6232 say what condition caused the scan to stop:
6233
6234 MOVE_POS_MATCH_OR_ZV
6235 - when TO_POS or ZV was reached.
6236
6237 MOVE_X_REACHED
6238 -when TO_X was reached before TO_POS or ZV were reached.
6239
6240 MOVE_LINE_CONTINUED
6241 - when we reached the end of the display area and the line must
6242 be continued.
6243
6244 MOVE_LINE_TRUNCATED
6245 - when we reached the end of the display area and the line is
6246 truncated.
6247
6248 MOVE_NEWLINE_OR_CR
6249 - when we stopped at a line end, i.e. a newline or a CR and selective
6250 display is on. */
6251
6252 static enum move_it_result
6253 move_it_in_display_line_to (it, to_charpos, to_x, op)
6254 struct it *it;
6255 int to_charpos, to_x, op;
6256 {
6257 enum move_it_result result = MOVE_UNDEFINED;
6258 struct glyph_row *saved_glyph_row;
6259
6260 /* Don't produce glyphs in produce_glyphs. */
6261 saved_glyph_row = it->glyph_row;
6262 it->glyph_row = NULL;
6263
6264 #define BUFFER_POS_REACHED_P() \
6265 ((op & MOVE_TO_POS) != 0 \
6266 && BUFFERP (it->object) \
6267 && IT_CHARPOS (*it) >= to_charpos \
6268 && (it->method == GET_FROM_BUFFER \
6269 || (it->method == GET_FROM_DISPLAY_VECTOR \
6270 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6271
6272
6273 while (1)
6274 {
6275 int x, i, ascent = 0, descent = 0;
6276
6277 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6278 if ((op & MOVE_TO_POS) != 0
6279 && BUFFERP (it->object)
6280 && it->method == GET_FROM_BUFFER
6281 && IT_CHARPOS (*it) > to_charpos)
6282 {
6283 result = MOVE_POS_MATCH_OR_ZV;
6284 break;
6285 }
6286
6287 /* Stop when ZV reached.
6288 We used to stop here when TO_CHARPOS reached as well, but that is
6289 too soon if this glyph does not fit on this line. So we handle it
6290 explicitly below. */
6291 if (!get_next_display_element (it)
6292 || (it->truncate_lines_p
6293 && BUFFER_POS_REACHED_P ()))
6294 {
6295 result = MOVE_POS_MATCH_OR_ZV;
6296 break;
6297 }
6298
6299 /* The call to produce_glyphs will get the metrics of the
6300 display element IT is loaded with. We record in x the
6301 x-position before this display element in case it does not
6302 fit on the line. */
6303 x = it->current_x;
6304
6305 /* Remember the line height so far in case the next element doesn't
6306 fit on the line. */
6307 if (!it->truncate_lines_p)
6308 {
6309 ascent = it->max_ascent;
6310 descent = it->max_descent;
6311 }
6312
6313 PRODUCE_GLYPHS (it);
6314
6315 if (it->area != TEXT_AREA)
6316 {
6317 set_iterator_to_next (it, 1);
6318 continue;
6319 }
6320
6321 /* The number of glyphs we get back in IT->nglyphs will normally
6322 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6323 character on a terminal frame, or (iii) a line end. For the
6324 second case, IT->nglyphs - 1 padding glyphs will be present
6325 (on X frames, there is only one glyph produced for a
6326 composite character.
6327
6328 The behavior implemented below means, for continuation lines,
6329 that as many spaces of a TAB as fit on the current line are
6330 displayed there. For terminal frames, as many glyphs of a
6331 multi-glyph character are displayed in the current line, too.
6332 This is what the old redisplay code did, and we keep it that
6333 way. Under X, the whole shape of a complex character must
6334 fit on the line or it will be completely displayed in the
6335 next line.
6336
6337 Note that both for tabs and padding glyphs, all glyphs have
6338 the same width. */
6339 if (it->nglyphs)
6340 {
6341 /* More than one glyph or glyph doesn't fit on line. All
6342 glyphs have the same width. */
6343 int single_glyph_width = it->pixel_width / it->nglyphs;
6344 int new_x;
6345 int x_before_this_char = x;
6346 int hpos_before_this_char = it->hpos;
6347
6348 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6349 {
6350 new_x = x + single_glyph_width;
6351
6352 /* We want to leave anything reaching TO_X to the caller. */
6353 if ((op & MOVE_TO_X) && new_x > to_x)
6354 {
6355 if (BUFFER_POS_REACHED_P ())
6356 goto buffer_pos_reached;
6357 it->current_x = x;
6358 result = MOVE_X_REACHED;
6359 break;
6360 }
6361 else if (/* Lines are continued. */
6362 !it->truncate_lines_p
6363 && (/* And glyph doesn't fit on the line. */
6364 new_x > it->last_visible_x
6365 /* Or it fits exactly and we're on a window
6366 system frame. */
6367 || (new_x == it->last_visible_x
6368 && FRAME_WINDOW_P (it->f))))
6369 {
6370 if (/* IT->hpos == 0 means the very first glyph
6371 doesn't fit on the line, e.g. a wide image. */
6372 it->hpos == 0
6373 || (new_x == it->last_visible_x
6374 && FRAME_WINDOW_P (it->f)))
6375 {
6376 ++it->hpos;
6377 it->current_x = new_x;
6378
6379 /* The character's last glyph just barely fits
6380 in this row. */
6381 if (i == it->nglyphs - 1)
6382 {
6383 /* If this is the destination position,
6384 return a position *before* it in this row,
6385 now that we know it fits in this row. */
6386 if (BUFFER_POS_REACHED_P ())
6387 {
6388 it->hpos = hpos_before_this_char;
6389 it->current_x = x_before_this_char;
6390 result = MOVE_POS_MATCH_OR_ZV;
6391 break;
6392 }
6393
6394 set_iterator_to_next (it, 1);
6395 #ifdef HAVE_WINDOW_SYSTEM
6396 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6397 {
6398 if (!get_next_display_element (it))
6399 {
6400 result = MOVE_POS_MATCH_OR_ZV;
6401 break;
6402 }
6403 if (BUFFER_POS_REACHED_P ())
6404 {
6405 if (ITERATOR_AT_END_OF_LINE_P (it))
6406 result = MOVE_POS_MATCH_OR_ZV;
6407 else
6408 result = MOVE_LINE_CONTINUED;
6409 break;
6410 }
6411 if (ITERATOR_AT_END_OF_LINE_P (it))
6412 {
6413 result = MOVE_NEWLINE_OR_CR;
6414 break;
6415 }
6416 }
6417 #endif /* HAVE_WINDOW_SYSTEM */
6418 }
6419 }
6420 else
6421 {
6422 it->current_x = x;
6423 it->max_ascent = ascent;
6424 it->max_descent = descent;
6425 }
6426
6427 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6428 IT_CHARPOS (*it)));
6429 result = MOVE_LINE_CONTINUED;
6430 break;
6431 }
6432 else if (BUFFER_POS_REACHED_P ())
6433 goto buffer_pos_reached;
6434 else if (new_x > it->first_visible_x)
6435 {
6436 /* Glyph is visible. Increment number of glyphs that
6437 would be displayed. */
6438 ++it->hpos;
6439 }
6440 else
6441 {
6442 /* Glyph is completely off the left margin of the display
6443 area. Nothing to do. */
6444 }
6445 }
6446
6447 if (result != MOVE_UNDEFINED)
6448 break;
6449 }
6450 else if (BUFFER_POS_REACHED_P ())
6451 {
6452 buffer_pos_reached:
6453 it->current_x = x;
6454 it->max_ascent = ascent;
6455 it->max_descent = descent;
6456 result = MOVE_POS_MATCH_OR_ZV;
6457 break;
6458 }
6459 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6460 {
6461 /* Stop when TO_X specified and reached. This check is
6462 necessary here because of lines consisting of a line end,
6463 only. The line end will not produce any glyphs and we
6464 would never get MOVE_X_REACHED. */
6465 xassert (it->nglyphs == 0);
6466 result = MOVE_X_REACHED;
6467 break;
6468 }
6469
6470 /* Is this a line end? If yes, we're done. */
6471 if (ITERATOR_AT_END_OF_LINE_P (it))
6472 {
6473 result = MOVE_NEWLINE_OR_CR;
6474 break;
6475 }
6476
6477 /* The current display element has been consumed. Advance
6478 to the next. */
6479 set_iterator_to_next (it, 1);
6480
6481 /* Stop if lines are truncated and IT's current x-position is
6482 past the right edge of the window now. */
6483 if (it->truncate_lines_p
6484 && it->current_x >= it->last_visible_x)
6485 {
6486 #ifdef HAVE_WINDOW_SYSTEM
6487 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6488 {
6489 if (!get_next_display_element (it)
6490 || BUFFER_POS_REACHED_P ())
6491 {
6492 result = MOVE_POS_MATCH_OR_ZV;
6493 break;
6494 }
6495 if (ITERATOR_AT_END_OF_LINE_P (it))
6496 {
6497 result = MOVE_NEWLINE_OR_CR;
6498 break;
6499 }
6500 }
6501 #endif /* HAVE_WINDOW_SYSTEM */
6502 result = MOVE_LINE_TRUNCATED;
6503 break;
6504 }
6505 }
6506
6507 #undef BUFFER_POS_REACHED_P
6508
6509 /* Restore the iterator settings altered at the beginning of this
6510 function. */
6511 it->glyph_row = saved_glyph_row;
6512 return result;
6513 }
6514
6515
6516 /* Move IT forward until it satisfies one or more of the criteria in
6517 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6518
6519 OP is a bit-mask that specifies where to stop, and in particular,
6520 which of those four position arguments makes a difference. See the
6521 description of enum move_operation_enum.
6522
6523 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6524 screen line, this function will set IT to the next position >
6525 TO_CHARPOS. */
6526
6527 void
6528 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6529 struct it *it;
6530 int to_charpos, to_x, to_y, to_vpos;
6531 int op;
6532 {
6533 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6534 int line_height;
6535 int reached = 0;
6536
6537 for (;;)
6538 {
6539 if (op & MOVE_TO_VPOS)
6540 {
6541 /* If no TO_CHARPOS and no TO_X specified, stop at the
6542 start of the line TO_VPOS. */
6543 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6544 {
6545 if (it->vpos == to_vpos)
6546 {
6547 reached = 1;
6548 break;
6549 }
6550 else
6551 skip = move_it_in_display_line_to (it, -1, -1, 0);
6552 }
6553 else
6554 {
6555 /* TO_VPOS >= 0 means stop at TO_X in the line at
6556 TO_VPOS, or at TO_POS, whichever comes first. */
6557 if (it->vpos == to_vpos)
6558 {
6559 reached = 2;
6560 break;
6561 }
6562
6563 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6564
6565 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6566 {
6567 reached = 3;
6568 break;
6569 }
6570 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6571 {
6572 /* We have reached TO_X but not in the line we want. */
6573 skip = move_it_in_display_line_to (it, to_charpos,
6574 -1, MOVE_TO_POS);
6575 if (skip == MOVE_POS_MATCH_OR_ZV)
6576 {
6577 reached = 4;
6578 break;
6579 }
6580 }
6581 }
6582 }
6583 else if (op & MOVE_TO_Y)
6584 {
6585 struct it it_backup;
6586
6587 /* TO_Y specified means stop at TO_X in the line containing
6588 TO_Y---or at TO_CHARPOS if this is reached first. The
6589 problem is that we can't really tell whether the line
6590 contains TO_Y before we have completely scanned it, and
6591 this may skip past TO_X. What we do is to first scan to
6592 TO_X.
6593
6594 If TO_X is not specified, use a TO_X of zero. The reason
6595 is to make the outcome of this function more predictable.
6596 If we didn't use TO_X == 0, we would stop at the end of
6597 the line which is probably not what a caller would expect
6598 to happen. */
6599 skip = move_it_in_display_line_to (it, to_charpos,
6600 ((op & MOVE_TO_X)
6601 ? to_x : 0),
6602 (MOVE_TO_X
6603 | (op & MOVE_TO_POS)));
6604
6605 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6606 if (skip == MOVE_POS_MATCH_OR_ZV)
6607 {
6608 reached = 5;
6609 break;
6610 }
6611
6612 /* If TO_X was reached, we would like to know whether TO_Y
6613 is in the line. This can only be said if we know the
6614 total line height which requires us to scan the rest of
6615 the line. */
6616 if (skip == MOVE_X_REACHED)
6617 {
6618 it_backup = *it;
6619 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6620 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6621 op & MOVE_TO_POS);
6622 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6623 }
6624
6625 /* Now, decide whether TO_Y is in this line. */
6626 line_height = it->max_ascent + it->max_descent;
6627 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6628
6629 if (to_y >= it->current_y
6630 && to_y < it->current_y + line_height)
6631 {
6632 if (skip == MOVE_X_REACHED)
6633 /* If TO_Y is in this line and TO_X was reached above,
6634 we scanned too far. We have to restore IT's settings
6635 to the ones before skipping. */
6636 *it = it_backup;
6637 reached = 6;
6638 }
6639 else if (skip == MOVE_X_REACHED)
6640 {
6641 skip = skip2;
6642 if (skip == MOVE_POS_MATCH_OR_ZV)
6643 reached = 7;
6644 }
6645
6646 if (reached)
6647 break;
6648 }
6649 else
6650 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6651
6652 switch (skip)
6653 {
6654 case MOVE_POS_MATCH_OR_ZV:
6655 reached = 8;
6656 goto out;
6657
6658 case MOVE_NEWLINE_OR_CR:
6659 set_iterator_to_next (it, 1);
6660 it->continuation_lines_width = 0;
6661 break;
6662
6663 case MOVE_LINE_TRUNCATED:
6664 it->continuation_lines_width = 0;
6665 reseat_at_next_visible_line_start (it, 0);
6666 if ((op & MOVE_TO_POS) != 0
6667 && IT_CHARPOS (*it) > to_charpos)
6668 {
6669 reached = 9;
6670 goto out;
6671 }
6672 break;
6673
6674 case MOVE_LINE_CONTINUED:
6675 it->continuation_lines_width += it->current_x;
6676 break;
6677
6678 default:
6679 abort ();
6680 }
6681
6682 /* Reset/increment for the next run. */
6683 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6684 it->current_x = it->hpos = 0;
6685 it->current_y += it->max_ascent + it->max_descent;
6686 ++it->vpos;
6687 last_height = it->max_ascent + it->max_descent;
6688 last_max_ascent = it->max_ascent;
6689 it->max_ascent = it->max_descent = 0;
6690 }
6691
6692 out:
6693
6694 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6695 }
6696
6697
6698 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6699
6700 If DY > 0, move IT backward at least that many pixels. DY = 0
6701 means move IT backward to the preceding line start or BEGV. This
6702 function may move over more than DY pixels if IT->current_y - DY
6703 ends up in the middle of a line; in this case IT->current_y will be
6704 set to the top of the line moved to. */
6705
6706 void
6707 move_it_vertically_backward (it, dy)
6708 struct it *it;
6709 int dy;
6710 {
6711 int nlines, h;
6712 struct it it2, it3;
6713 int start_pos;
6714
6715 move_further_back:
6716 xassert (dy >= 0);
6717
6718 start_pos = IT_CHARPOS (*it);
6719
6720 /* Estimate how many newlines we must move back. */
6721 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6722
6723 /* Set the iterator's position that many lines back. */
6724 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6725 back_to_previous_visible_line_start (it);
6726
6727 /* Reseat the iterator here. When moving backward, we don't want
6728 reseat to skip forward over invisible text, set up the iterator
6729 to deliver from overlay strings at the new position etc. So,
6730 use reseat_1 here. */
6731 reseat_1 (it, it->current.pos, 1);
6732
6733 /* We are now surely at a line start. */
6734 it->current_x = it->hpos = 0;
6735 it->continuation_lines_width = 0;
6736
6737 /* Move forward and see what y-distance we moved. First move to the
6738 start of the next line so that we get its height. We need this
6739 height to be able to tell whether we reached the specified
6740 y-distance. */
6741 it2 = *it;
6742 it2.max_ascent = it2.max_descent = 0;
6743 do
6744 {
6745 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6746 MOVE_TO_POS | MOVE_TO_VPOS);
6747 }
6748 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6749 xassert (IT_CHARPOS (*it) >= BEGV);
6750 it3 = it2;
6751
6752 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6753 xassert (IT_CHARPOS (*it) >= BEGV);
6754 /* H is the actual vertical distance from the position in *IT
6755 and the starting position. */
6756 h = it2.current_y - it->current_y;
6757 /* NLINES is the distance in number of lines. */
6758 nlines = it2.vpos - it->vpos;
6759
6760 /* Correct IT's y and vpos position
6761 so that they are relative to the starting point. */
6762 it->vpos -= nlines;
6763 it->current_y -= h;
6764
6765 if (dy == 0)
6766 {
6767 /* DY == 0 means move to the start of the screen line. The
6768 value of nlines is > 0 if continuation lines were involved. */
6769 if (nlines > 0)
6770 move_it_by_lines (it, nlines, 1);
6771 #if 0
6772 /* I think this assert is bogus if buffer contains
6773 invisible text or images. KFS. */
6774 xassert (IT_CHARPOS (*it) <= start_pos);
6775 #endif
6776 }
6777 else
6778 {
6779 /* The y-position we try to reach, relative to *IT.
6780 Note that H has been subtracted in front of the if-statement. */
6781 int target_y = it->current_y + h - dy;
6782 int y0 = it3.current_y;
6783 int y1 = line_bottom_y (&it3);
6784 int line_height = y1 - y0;
6785
6786 /* If we did not reach target_y, try to move further backward if
6787 we can. If we moved too far backward, try to move forward. */
6788 if (target_y < it->current_y
6789 /* This is heuristic. In a window that's 3 lines high, with
6790 a line height of 13 pixels each, recentering with point
6791 on the bottom line will try to move -39/2 = 19 pixels
6792 backward. Try to avoid moving into the first line. */
6793 && (it->current_y - target_y
6794 > min (window_box_height (it->w), line_height * 2 / 3))
6795 && IT_CHARPOS (*it) > BEGV)
6796 {
6797 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6798 target_y - it->current_y));
6799 dy = it->current_y - target_y;
6800 goto move_further_back;
6801 }
6802 else if (target_y >= it->current_y + line_height
6803 && IT_CHARPOS (*it) < ZV)
6804 {
6805 /* Should move forward by at least one line, maybe more.
6806
6807 Note: Calling move_it_by_lines can be expensive on
6808 terminal frames, where compute_motion is used (via
6809 vmotion) to do the job, when there are very long lines
6810 and truncate-lines is nil. That's the reason for
6811 treating terminal frames specially here. */
6812
6813 if (!FRAME_WINDOW_P (it->f))
6814 move_it_vertically (it, target_y - (it->current_y + line_height));
6815 else
6816 {
6817 do
6818 {
6819 move_it_by_lines (it, 1, 1);
6820 }
6821 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6822 }
6823
6824 #if 0
6825 /* I think this assert is bogus if buffer contains
6826 invisible text or images. KFS. */
6827 xassert (IT_CHARPOS (*it) >= BEGV);
6828 #endif
6829 }
6830 }
6831 }
6832
6833
6834 /* Move IT by a specified amount of pixel lines DY. DY negative means
6835 move backwards. DY = 0 means move to start of screen line. At the
6836 end, IT will be on the start of a screen line. */
6837
6838 void
6839 move_it_vertically (it, dy)
6840 struct it *it;
6841 int dy;
6842 {
6843 if (dy <= 0)
6844 move_it_vertically_backward (it, -dy);
6845 else
6846 {
6847 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6848 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6849 MOVE_TO_POS | MOVE_TO_Y);
6850 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6851
6852 /* If buffer ends in ZV without a newline, move to the start of
6853 the line to satisfy the post-condition. */
6854 if (IT_CHARPOS (*it) == ZV
6855 && ZV > BEGV
6856 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6857 move_it_by_lines (it, 0, 0);
6858 }
6859 }
6860
6861
6862 /* Move iterator IT past the end of the text line it is in. */
6863
6864 void
6865 move_it_past_eol (it)
6866 struct it *it;
6867 {
6868 enum move_it_result rc;
6869
6870 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6871 if (rc == MOVE_NEWLINE_OR_CR)
6872 set_iterator_to_next (it, 0);
6873 }
6874
6875
6876 #if 0 /* Currently not used. */
6877
6878 /* Return non-zero if some text between buffer positions START_CHARPOS
6879 and END_CHARPOS is invisible. IT->window is the window for text
6880 property lookup. */
6881
6882 static int
6883 invisible_text_between_p (it, start_charpos, end_charpos)
6884 struct it *it;
6885 int start_charpos, end_charpos;
6886 {
6887 Lisp_Object prop, limit;
6888 int invisible_found_p;
6889
6890 xassert (it != NULL && start_charpos <= end_charpos);
6891
6892 /* Is text at START invisible? */
6893 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6894 it->window);
6895 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6896 invisible_found_p = 1;
6897 else
6898 {
6899 limit = Fnext_single_char_property_change (make_number (start_charpos),
6900 Qinvisible, Qnil,
6901 make_number (end_charpos));
6902 invisible_found_p = XFASTINT (limit) < end_charpos;
6903 }
6904
6905 return invisible_found_p;
6906 }
6907
6908 #endif /* 0 */
6909
6910
6911 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6912 negative means move up. DVPOS == 0 means move to the start of the
6913 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6914 NEED_Y_P is zero, IT->current_y will be left unchanged.
6915
6916 Further optimization ideas: If we would know that IT->f doesn't use
6917 a face with proportional font, we could be faster for
6918 truncate-lines nil. */
6919
6920 void
6921 move_it_by_lines (it, dvpos, need_y_p)
6922 struct it *it;
6923 int dvpos, need_y_p;
6924 {
6925 struct position pos;
6926
6927 if (!FRAME_WINDOW_P (it->f))
6928 {
6929 struct text_pos textpos;
6930
6931 /* We can use vmotion on frames without proportional fonts. */
6932 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6933 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6934 reseat (it, textpos, 1);
6935 it->vpos += pos.vpos;
6936 it->current_y += pos.vpos;
6937 }
6938 else if (dvpos == 0)
6939 {
6940 /* DVPOS == 0 means move to the start of the screen line. */
6941 move_it_vertically_backward (it, 0);
6942 xassert (it->current_x == 0 && it->hpos == 0);
6943 /* Let next call to line_bottom_y calculate real line height */
6944 last_height = 0;
6945 }
6946 else if (dvpos > 0)
6947 {
6948 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6949 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6950 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6951 }
6952 else
6953 {
6954 struct it it2;
6955 int start_charpos, i;
6956
6957 /* Start at the beginning of the screen line containing IT's
6958 position. This may actually move vertically backwards,
6959 in case of overlays, so adjust dvpos accordingly. */
6960 dvpos += it->vpos;
6961 move_it_vertically_backward (it, 0);
6962 dvpos -= it->vpos;
6963
6964 /* Go back -DVPOS visible lines and reseat the iterator there. */
6965 start_charpos = IT_CHARPOS (*it);
6966 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6967 back_to_previous_visible_line_start (it);
6968 reseat (it, it->current.pos, 1);
6969
6970 /* Move further back if we end up in a string or an image. */
6971 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6972 {
6973 /* First try to move to start of display line. */
6974 dvpos += it->vpos;
6975 move_it_vertically_backward (it, 0);
6976 dvpos -= it->vpos;
6977 if (IT_POS_VALID_AFTER_MOVE_P (it))
6978 break;
6979 /* If start of line is still in string or image,
6980 move further back. */
6981 back_to_previous_visible_line_start (it);
6982 reseat (it, it->current.pos, 1);
6983 dvpos--;
6984 }
6985
6986 it->current_x = it->hpos = 0;
6987
6988 /* Above call may have moved too far if continuation lines
6989 are involved. Scan forward and see if it did. */
6990 it2 = *it;
6991 it2.vpos = it2.current_y = 0;
6992 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6993 it->vpos -= it2.vpos;
6994 it->current_y -= it2.current_y;
6995 it->current_x = it->hpos = 0;
6996
6997 /* If we moved too far back, move IT some lines forward. */
6998 if (it2.vpos > -dvpos)
6999 {
7000 int delta = it2.vpos + dvpos;
7001 it2 = *it;
7002 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7003 /* Move back again if we got too far ahead. */
7004 if (IT_CHARPOS (*it) >= start_charpos)
7005 *it = it2;
7006 }
7007 }
7008 }
7009
7010 /* Return 1 if IT points into the middle of a display vector. */
7011
7012 int
7013 in_display_vector_p (it)
7014 struct it *it;
7015 {
7016 return (it->method == GET_FROM_DISPLAY_VECTOR
7017 && it->current.dpvec_index > 0
7018 && it->dpvec + it->current.dpvec_index != it->dpend);
7019 }
7020
7021 \f
7022 /***********************************************************************
7023 Messages
7024 ***********************************************************************/
7025
7026
7027 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7028 to *Messages*. */
7029
7030 void
7031 add_to_log (format, arg1, arg2)
7032 char *format;
7033 Lisp_Object arg1, arg2;
7034 {
7035 Lisp_Object args[3];
7036 Lisp_Object msg, fmt;
7037 char *buffer;
7038 int len;
7039 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7040 USE_SAFE_ALLOCA;
7041
7042 /* Do nothing if called asynchronously. Inserting text into
7043 a buffer may call after-change-functions and alike and
7044 that would means running Lisp asynchronously. */
7045 if (handling_signal)
7046 return;
7047
7048 fmt = msg = Qnil;
7049 GCPRO4 (fmt, msg, arg1, arg2);
7050
7051 args[0] = fmt = build_string (format);
7052 args[1] = arg1;
7053 args[2] = arg2;
7054 msg = Fformat (3, args);
7055
7056 len = SBYTES (msg) + 1;
7057 SAFE_ALLOCA (buffer, char *, len);
7058 bcopy (SDATA (msg), buffer, len);
7059
7060 message_dolog (buffer, len - 1, 1, 0);
7061 SAFE_FREE ();
7062
7063 UNGCPRO;
7064 }
7065
7066
7067 /* Output a newline in the *Messages* buffer if "needs" one. */
7068
7069 void
7070 message_log_maybe_newline ()
7071 {
7072 if (message_log_need_newline)
7073 message_dolog ("", 0, 1, 0);
7074 }
7075
7076
7077 /* Add a string M of length NBYTES to the message log, optionally
7078 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7079 nonzero, means interpret the contents of M as multibyte. This
7080 function calls low-level routines in order to bypass text property
7081 hooks, etc. which might not be safe to run.
7082
7083 This may GC (insert may run before/after change hooks),
7084 so the buffer M must NOT point to a Lisp string. */
7085
7086 void
7087 message_dolog (m, nbytes, nlflag, multibyte)
7088 const char *m;
7089 int nbytes, nlflag, multibyte;
7090 {
7091 if (!NILP (Vmemory_full))
7092 return;
7093
7094 if (!NILP (Vmessage_log_max))
7095 {
7096 struct buffer *oldbuf;
7097 Lisp_Object oldpoint, oldbegv, oldzv;
7098 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7099 int point_at_end = 0;
7100 int zv_at_end = 0;
7101 Lisp_Object old_deactivate_mark, tem;
7102 struct gcpro gcpro1;
7103
7104 old_deactivate_mark = Vdeactivate_mark;
7105 oldbuf = current_buffer;
7106 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7107 current_buffer->undo_list = Qt;
7108
7109 oldpoint = message_dolog_marker1;
7110 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7111 oldbegv = message_dolog_marker2;
7112 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7113 oldzv = message_dolog_marker3;
7114 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7115 GCPRO1 (old_deactivate_mark);
7116
7117 if (PT == Z)
7118 point_at_end = 1;
7119 if (ZV == Z)
7120 zv_at_end = 1;
7121
7122 BEGV = BEG;
7123 BEGV_BYTE = BEG_BYTE;
7124 ZV = Z;
7125 ZV_BYTE = Z_BYTE;
7126 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7127
7128 /* Insert the string--maybe converting multibyte to single byte
7129 or vice versa, so that all the text fits the buffer. */
7130 if (multibyte
7131 && NILP (current_buffer->enable_multibyte_characters))
7132 {
7133 int i, c, char_bytes;
7134 unsigned char work[1];
7135
7136 /* Convert a multibyte string to single-byte
7137 for the *Message* buffer. */
7138 for (i = 0; i < nbytes; i += char_bytes)
7139 {
7140 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7141 work[0] = (SINGLE_BYTE_CHAR_P (c)
7142 ? c
7143 : multibyte_char_to_unibyte (c, Qnil));
7144 insert_1_both (work, 1, 1, 1, 0, 0);
7145 }
7146 }
7147 else if (! multibyte
7148 && ! NILP (current_buffer->enable_multibyte_characters))
7149 {
7150 int i, c, char_bytes;
7151 unsigned char *msg = (unsigned char *) m;
7152 unsigned char str[MAX_MULTIBYTE_LENGTH];
7153 /* Convert a single-byte string to multibyte
7154 for the *Message* buffer. */
7155 for (i = 0; i < nbytes; i++)
7156 {
7157 c = unibyte_char_to_multibyte (msg[i]);
7158 char_bytes = CHAR_STRING (c, str);
7159 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7160 }
7161 }
7162 else if (nbytes)
7163 insert_1 (m, nbytes, 1, 0, 0);
7164
7165 if (nlflag)
7166 {
7167 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7168 insert_1 ("\n", 1, 1, 0, 0);
7169
7170 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7171 this_bol = PT;
7172 this_bol_byte = PT_BYTE;
7173
7174 /* See if this line duplicates the previous one.
7175 If so, combine duplicates. */
7176 if (this_bol > BEG)
7177 {
7178 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7179 prev_bol = PT;
7180 prev_bol_byte = PT_BYTE;
7181
7182 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7183 this_bol, this_bol_byte);
7184 if (dup)
7185 {
7186 del_range_both (prev_bol, prev_bol_byte,
7187 this_bol, this_bol_byte, 0);
7188 if (dup > 1)
7189 {
7190 char dupstr[40];
7191 int duplen;
7192
7193 /* If you change this format, don't forget to also
7194 change message_log_check_duplicate. */
7195 sprintf (dupstr, " [%d times]", dup);
7196 duplen = strlen (dupstr);
7197 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7198 insert_1 (dupstr, duplen, 1, 0, 1);
7199 }
7200 }
7201 }
7202
7203 /* If we have more than the desired maximum number of lines
7204 in the *Messages* buffer now, delete the oldest ones.
7205 This is safe because we don't have undo in this buffer. */
7206
7207 if (NATNUMP (Vmessage_log_max))
7208 {
7209 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7210 -XFASTINT (Vmessage_log_max) - 1, 0);
7211 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7212 }
7213 }
7214 BEGV = XMARKER (oldbegv)->charpos;
7215 BEGV_BYTE = marker_byte_position (oldbegv);
7216
7217 if (zv_at_end)
7218 {
7219 ZV = Z;
7220 ZV_BYTE = Z_BYTE;
7221 }
7222 else
7223 {
7224 ZV = XMARKER (oldzv)->charpos;
7225 ZV_BYTE = marker_byte_position (oldzv);
7226 }
7227
7228 if (point_at_end)
7229 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7230 else
7231 /* We can't do Fgoto_char (oldpoint) because it will run some
7232 Lisp code. */
7233 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7234 XMARKER (oldpoint)->bytepos);
7235
7236 UNGCPRO;
7237 unchain_marker (XMARKER (oldpoint));
7238 unchain_marker (XMARKER (oldbegv));
7239 unchain_marker (XMARKER (oldzv));
7240
7241 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7242 set_buffer_internal (oldbuf);
7243 if (NILP (tem))
7244 windows_or_buffers_changed = old_windows_or_buffers_changed;
7245 message_log_need_newline = !nlflag;
7246 Vdeactivate_mark = old_deactivate_mark;
7247 }
7248 }
7249
7250
7251 /* We are at the end of the buffer after just having inserted a newline.
7252 (Note: We depend on the fact we won't be crossing the gap.)
7253 Check to see if the most recent message looks a lot like the previous one.
7254 Return 0 if different, 1 if the new one should just replace it, or a
7255 value N > 1 if we should also append " [N times]". */
7256
7257 static int
7258 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7259 int prev_bol, this_bol;
7260 int prev_bol_byte, this_bol_byte;
7261 {
7262 int i;
7263 int len = Z_BYTE - 1 - this_bol_byte;
7264 int seen_dots = 0;
7265 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7266 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7267
7268 for (i = 0; i < len; i++)
7269 {
7270 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7271 seen_dots = 1;
7272 if (p1[i] != p2[i])
7273 return seen_dots;
7274 }
7275 p1 += len;
7276 if (*p1 == '\n')
7277 return 2;
7278 if (*p1++ == ' ' && *p1++ == '[')
7279 {
7280 int n = 0;
7281 while (*p1 >= '0' && *p1 <= '9')
7282 n = n * 10 + *p1++ - '0';
7283 if (strncmp (p1, " times]\n", 8) == 0)
7284 return n+1;
7285 }
7286 return 0;
7287 }
7288 \f
7289
7290 /* Display an echo area message M with a specified length of NBYTES
7291 bytes. The string may include null characters. If M is 0, clear
7292 out any existing message, and let the mini-buffer text show
7293 through.
7294
7295 This may GC, so the buffer M must NOT point to a Lisp string. */
7296
7297 void
7298 message2 (m, nbytes, multibyte)
7299 const char *m;
7300 int nbytes;
7301 int multibyte;
7302 {
7303 /* First flush out any partial line written with print. */
7304 message_log_maybe_newline ();
7305 if (m)
7306 message_dolog (m, nbytes, 1, multibyte);
7307 message2_nolog (m, nbytes, multibyte);
7308 }
7309
7310
7311 /* The non-logging counterpart of message2. */
7312
7313 void
7314 message2_nolog (m, nbytes, multibyte)
7315 const char *m;
7316 int nbytes, multibyte;
7317 {
7318 struct frame *sf = SELECTED_FRAME ();
7319 message_enable_multibyte = multibyte;
7320
7321 if (noninteractive)
7322 {
7323 if (noninteractive_need_newline)
7324 putc ('\n', stderr);
7325 noninteractive_need_newline = 0;
7326 if (m)
7327 fwrite (m, nbytes, 1, stderr);
7328 if (cursor_in_echo_area == 0)
7329 fprintf (stderr, "\n");
7330 fflush (stderr);
7331 }
7332 /* A null message buffer means that the frame hasn't really been
7333 initialized yet. Error messages get reported properly by
7334 cmd_error, so this must be just an informative message; toss it. */
7335 else if (INTERACTIVE
7336 && sf->glyphs_initialized_p
7337 && FRAME_MESSAGE_BUF (sf))
7338 {
7339 Lisp_Object mini_window;
7340 struct frame *f;
7341
7342 /* Get the frame containing the mini-buffer
7343 that the selected frame is using. */
7344 mini_window = FRAME_MINIBUF_WINDOW (sf);
7345 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7346
7347 FRAME_SAMPLE_VISIBILITY (f);
7348 if (FRAME_VISIBLE_P (sf)
7349 && ! FRAME_VISIBLE_P (f))
7350 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7351
7352 if (m)
7353 {
7354 set_message (m, Qnil, nbytes, multibyte);
7355 if (minibuffer_auto_raise)
7356 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7357 }
7358 else
7359 clear_message (1, 1);
7360
7361 do_pending_window_change (0);
7362 echo_area_display (1);
7363 do_pending_window_change (0);
7364 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7365 (*frame_up_to_date_hook) (f);
7366 }
7367 }
7368
7369
7370 /* Display an echo area message M with a specified length of NBYTES
7371 bytes. The string may include null characters. If M is not a
7372 string, clear out any existing message, and let the mini-buffer
7373 text show through.
7374
7375 This function cancels echoing. */
7376
7377 void
7378 message3 (m, nbytes, multibyte)
7379 Lisp_Object m;
7380 int nbytes;
7381 int multibyte;
7382 {
7383 struct gcpro gcpro1;
7384
7385 GCPRO1 (m);
7386 clear_message (1,1);
7387 cancel_echoing ();
7388
7389 /* First flush out any partial line written with print. */
7390 message_log_maybe_newline ();
7391 if (STRINGP (m))
7392 {
7393 char *buffer;
7394 USE_SAFE_ALLOCA;
7395
7396 SAFE_ALLOCA (buffer, char *, nbytes);
7397 bcopy (SDATA (m), buffer, nbytes);
7398 message_dolog (buffer, nbytes, 1, multibyte);
7399 SAFE_FREE ();
7400 }
7401 message3_nolog (m, nbytes, multibyte);
7402
7403 UNGCPRO;
7404 }
7405
7406
7407 /* The non-logging version of message3.
7408 This does not cancel echoing, because it is used for echoing.
7409 Perhaps we need to make a separate function for echoing
7410 and make this cancel echoing. */
7411
7412 void
7413 message3_nolog (m, nbytes, multibyte)
7414 Lisp_Object m;
7415 int nbytes, multibyte;
7416 {
7417 struct frame *sf = SELECTED_FRAME ();
7418 message_enable_multibyte = multibyte;
7419
7420 if (noninteractive)
7421 {
7422 if (noninteractive_need_newline)
7423 putc ('\n', stderr);
7424 noninteractive_need_newline = 0;
7425 if (STRINGP (m))
7426 fwrite (SDATA (m), nbytes, 1, stderr);
7427 if (cursor_in_echo_area == 0)
7428 fprintf (stderr, "\n");
7429 fflush (stderr);
7430 }
7431 /* A null message buffer means that the frame hasn't really been
7432 initialized yet. Error messages get reported properly by
7433 cmd_error, so this must be just an informative message; toss it. */
7434 else if (INTERACTIVE
7435 && sf->glyphs_initialized_p
7436 && FRAME_MESSAGE_BUF (sf))
7437 {
7438 Lisp_Object mini_window;
7439 Lisp_Object frame;
7440 struct frame *f;
7441
7442 /* Get the frame containing the mini-buffer
7443 that the selected frame is using. */
7444 mini_window = FRAME_MINIBUF_WINDOW (sf);
7445 frame = XWINDOW (mini_window)->frame;
7446 f = XFRAME (frame);
7447
7448 FRAME_SAMPLE_VISIBILITY (f);
7449 if (FRAME_VISIBLE_P (sf)
7450 && !FRAME_VISIBLE_P (f))
7451 Fmake_frame_visible (frame);
7452
7453 if (STRINGP (m) && SCHARS (m) > 0)
7454 {
7455 set_message (NULL, m, nbytes, multibyte);
7456 if (minibuffer_auto_raise)
7457 Fraise_frame (frame);
7458 /* Assume we are not echoing.
7459 (If we are, echo_now will override this.) */
7460 echo_message_buffer = Qnil;
7461 }
7462 else
7463 clear_message (1, 1);
7464
7465 do_pending_window_change (0);
7466 echo_area_display (1);
7467 do_pending_window_change (0);
7468 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7469 (*frame_up_to_date_hook) (f);
7470 }
7471 }
7472
7473
7474 /* Display a null-terminated echo area message M. If M is 0, clear
7475 out any existing message, and let the mini-buffer text show through.
7476
7477 The buffer M must continue to exist until after the echo area gets
7478 cleared or some other message gets displayed there. Do not pass
7479 text that is stored in a Lisp string. Do not pass text in a buffer
7480 that was alloca'd. */
7481
7482 void
7483 message1 (m)
7484 char *m;
7485 {
7486 message2 (m, (m ? strlen (m) : 0), 0);
7487 }
7488
7489
7490 /* The non-logging counterpart of message1. */
7491
7492 void
7493 message1_nolog (m)
7494 char *m;
7495 {
7496 message2_nolog (m, (m ? strlen (m) : 0), 0);
7497 }
7498
7499 /* Display a message M which contains a single %s
7500 which gets replaced with STRING. */
7501
7502 void
7503 message_with_string (m, string, log)
7504 char *m;
7505 Lisp_Object string;
7506 int log;
7507 {
7508 CHECK_STRING (string);
7509
7510 if (noninteractive)
7511 {
7512 if (m)
7513 {
7514 if (noninteractive_need_newline)
7515 putc ('\n', stderr);
7516 noninteractive_need_newline = 0;
7517 fprintf (stderr, m, SDATA (string));
7518 if (cursor_in_echo_area == 0)
7519 fprintf (stderr, "\n");
7520 fflush (stderr);
7521 }
7522 }
7523 else if (INTERACTIVE)
7524 {
7525 /* The frame whose minibuffer we're going to display the message on.
7526 It may be larger than the selected frame, so we need
7527 to use its buffer, not the selected frame's buffer. */
7528 Lisp_Object mini_window;
7529 struct frame *f, *sf = SELECTED_FRAME ();
7530
7531 /* Get the frame containing the minibuffer
7532 that the selected frame is using. */
7533 mini_window = FRAME_MINIBUF_WINDOW (sf);
7534 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7535
7536 /* A null message buffer means that the frame hasn't really been
7537 initialized yet. Error messages get reported properly by
7538 cmd_error, so this must be just an informative message; toss it. */
7539 if (FRAME_MESSAGE_BUF (f))
7540 {
7541 Lisp_Object args[2], message;
7542 struct gcpro gcpro1, gcpro2;
7543
7544 args[0] = build_string (m);
7545 args[1] = message = string;
7546 GCPRO2 (args[0], message);
7547 gcpro1.nvars = 2;
7548
7549 message = Fformat (2, args);
7550
7551 if (log)
7552 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7553 else
7554 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7555
7556 UNGCPRO;
7557
7558 /* Print should start at the beginning of the message
7559 buffer next time. */
7560 message_buf_print = 0;
7561 }
7562 }
7563 }
7564
7565
7566 /* Dump an informative message to the minibuf. If M is 0, clear out
7567 any existing message, and let the mini-buffer text show through. */
7568
7569 /* VARARGS 1 */
7570 void
7571 message (m, a1, a2, a3)
7572 char *m;
7573 EMACS_INT a1, a2, a3;
7574 {
7575 if (noninteractive)
7576 {
7577 if (m)
7578 {
7579 if (noninteractive_need_newline)
7580 putc ('\n', stderr);
7581 noninteractive_need_newline = 0;
7582 fprintf (stderr, m, a1, a2, a3);
7583 if (cursor_in_echo_area == 0)
7584 fprintf (stderr, "\n");
7585 fflush (stderr);
7586 }
7587 }
7588 else if (INTERACTIVE)
7589 {
7590 /* The frame whose mini-buffer we're going to display the message
7591 on. It may be larger than the selected frame, so we need to
7592 use its buffer, not the selected frame's buffer. */
7593 Lisp_Object mini_window;
7594 struct frame *f, *sf = SELECTED_FRAME ();
7595
7596 /* Get the frame containing the mini-buffer
7597 that the selected frame is using. */
7598 mini_window = FRAME_MINIBUF_WINDOW (sf);
7599 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7600
7601 /* A null message buffer means that the frame hasn't really been
7602 initialized yet. Error messages get reported properly by
7603 cmd_error, so this must be just an informative message; toss
7604 it. */
7605 if (FRAME_MESSAGE_BUF (f))
7606 {
7607 if (m)
7608 {
7609 int len;
7610 #ifdef NO_ARG_ARRAY
7611 char *a[3];
7612 a[0] = (char *) a1;
7613 a[1] = (char *) a2;
7614 a[2] = (char *) a3;
7615
7616 len = doprnt (FRAME_MESSAGE_BUF (f),
7617 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7618 #else
7619 len = doprnt (FRAME_MESSAGE_BUF (f),
7620 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7621 (char **) &a1);
7622 #endif /* NO_ARG_ARRAY */
7623
7624 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7625 }
7626 else
7627 message1 (0);
7628
7629 /* Print should start at the beginning of the message
7630 buffer next time. */
7631 message_buf_print = 0;
7632 }
7633 }
7634 }
7635
7636
7637 /* The non-logging version of message. */
7638
7639 void
7640 message_nolog (m, a1, a2, a3)
7641 char *m;
7642 EMACS_INT a1, a2, a3;
7643 {
7644 Lisp_Object old_log_max;
7645 old_log_max = Vmessage_log_max;
7646 Vmessage_log_max = Qnil;
7647 message (m, a1, a2, a3);
7648 Vmessage_log_max = old_log_max;
7649 }
7650
7651
7652 /* Display the current message in the current mini-buffer. This is
7653 only called from error handlers in process.c, and is not time
7654 critical. */
7655
7656 void
7657 update_echo_area ()
7658 {
7659 if (!NILP (echo_area_buffer[0]))
7660 {
7661 Lisp_Object string;
7662 string = Fcurrent_message ();
7663 message3 (string, SBYTES (string),
7664 !NILP (current_buffer->enable_multibyte_characters));
7665 }
7666 }
7667
7668
7669 /* Make sure echo area buffers in `echo_buffers' are live.
7670 If they aren't, make new ones. */
7671
7672 static void
7673 ensure_echo_area_buffers ()
7674 {
7675 int i;
7676
7677 for (i = 0; i < 2; ++i)
7678 if (!BUFFERP (echo_buffer[i])
7679 || NILP (XBUFFER (echo_buffer[i])->name))
7680 {
7681 char name[30];
7682 Lisp_Object old_buffer;
7683 int j;
7684
7685 old_buffer = echo_buffer[i];
7686 sprintf (name, " *Echo Area %d*", i);
7687 echo_buffer[i] = Fget_buffer_create (build_string (name));
7688 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7689
7690 for (j = 0; j < 2; ++j)
7691 if (EQ (old_buffer, echo_area_buffer[j]))
7692 echo_area_buffer[j] = echo_buffer[i];
7693 }
7694 }
7695
7696
7697 /* Call FN with args A1..A4 with either the current or last displayed
7698 echo_area_buffer as current buffer.
7699
7700 WHICH zero means use the current message buffer
7701 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7702 from echo_buffer[] and clear it.
7703
7704 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7705 suitable buffer from echo_buffer[] and clear it.
7706
7707 Value is what FN returns. */
7708
7709 static int
7710 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7711 struct window *w;
7712 int which;
7713 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7714 EMACS_INT a1;
7715 Lisp_Object a2;
7716 EMACS_INT a3, a4;
7717 {
7718 Lisp_Object buffer;
7719 int this_one, the_other, clear_buffer_p, rc;
7720 int count = SPECPDL_INDEX ();
7721
7722 /* If buffers aren't live, make new ones. */
7723 ensure_echo_area_buffers ();
7724
7725 clear_buffer_p = 0;
7726
7727 if (which == 0)
7728 this_one = 0, the_other = 1;
7729 else if (which > 0)
7730 this_one = 1, the_other = 0;
7731
7732 /* Choose a suitable buffer from echo_buffer[] is we don't
7733 have one. */
7734 if (NILP (echo_area_buffer[this_one]))
7735 {
7736 echo_area_buffer[this_one]
7737 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7738 ? echo_buffer[the_other]
7739 : echo_buffer[this_one]);
7740 clear_buffer_p = 1;
7741 }
7742
7743 buffer = echo_area_buffer[this_one];
7744
7745 /* Don't get confused by reusing the buffer used for echoing
7746 for a different purpose. */
7747 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7748 cancel_echoing ();
7749
7750 record_unwind_protect (unwind_with_echo_area_buffer,
7751 with_echo_area_buffer_unwind_data (w));
7752
7753 /* Make the echo area buffer current. Note that for display
7754 purposes, it is not necessary that the displayed window's buffer
7755 == current_buffer, except for text property lookup. So, let's
7756 only set that buffer temporarily here without doing a full
7757 Fset_window_buffer. We must also change w->pointm, though,
7758 because otherwise an assertions in unshow_buffer fails, and Emacs
7759 aborts. */
7760 set_buffer_internal_1 (XBUFFER (buffer));
7761 if (w)
7762 {
7763 w->buffer = buffer;
7764 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7765 }
7766
7767 current_buffer->undo_list = Qt;
7768 current_buffer->read_only = Qnil;
7769 specbind (Qinhibit_read_only, Qt);
7770 specbind (Qinhibit_modification_hooks, Qt);
7771
7772 if (clear_buffer_p && Z > BEG)
7773 del_range (BEG, Z);
7774
7775 xassert (BEGV >= BEG);
7776 xassert (ZV <= Z && ZV >= BEGV);
7777
7778 rc = fn (a1, a2, a3, a4);
7779
7780 xassert (BEGV >= BEG);
7781 xassert (ZV <= Z && ZV >= BEGV);
7782
7783 unbind_to (count, Qnil);
7784 return rc;
7785 }
7786
7787
7788 /* Save state that should be preserved around the call to the function
7789 FN called in with_echo_area_buffer. */
7790
7791 static Lisp_Object
7792 with_echo_area_buffer_unwind_data (w)
7793 struct window *w;
7794 {
7795 int i = 0;
7796 Lisp_Object vector;
7797
7798 /* Reduce consing by keeping one vector in
7799 Vwith_echo_area_save_vector. */
7800 vector = Vwith_echo_area_save_vector;
7801 Vwith_echo_area_save_vector = Qnil;
7802
7803 if (NILP (vector))
7804 vector = Fmake_vector (make_number (7), Qnil);
7805
7806 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7807 AREF (vector, i) = Vdeactivate_mark, ++i;
7808 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7809
7810 if (w)
7811 {
7812 XSETWINDOW (AREF (vector, i), w); ++i;
7813 AREF (vector, i) = w->buffer; ++i;
7814 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7815 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7816 }
7817 else
7818 {
7819 int end = i + 4;
7820 for (; i < end; ++i)
7821 AREF (vector, i) = Qnil;
7822 }
7823
7824 xassert (i == ASIZE (vector));
7825 return vector;
7826 }
7827
7828
7829 /* Restore global state from VECTOR which was created by
7830 with_echo_area_buffer_unwind_data. */
7831
7832 static Lisp_Object
7833 unwind_with_echo_area_buffer (vector)
7834 Lisp_Object vector;
7835 {
7836 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7837 Vdeactivate_mark = AREF (vector, 1);
7838 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7839
7840 if (WINDOWP (AREF (vector, 3)))
7841 {
7842 struct window *w;
7843 Lisp_Object buffer, charpos, bytepos;
7844
7845 w = XWINDOW (AREF (vector, 3));
7846 buffer = AREF (vector, 4);
7847 charpos = AREF (vector, 5);
7848 bytepos = AREF (vector, 6);
7849
7850 w->buffer = buffer;
7851 set_marker_both (w->pointm, buffer,
7852 XFASTINT (charpos), XFASTINT (bytepos));
7853 }
7854
7855 Vwith_echo_area_save_vector = vector;
7856 return Qnil;
7857 }
7858
7859
7860 /* Set up the echo area for use by print functions. MULTIBYTE_P
7861 non-zero means we will print multibyte. */
7862
7863 void
7864 setup_echo_area_for_printing (multibyte_p)
7865 int multibyte_p;
7866 {
7867 /* If we can't find an echo area any more, exit. */
7868 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7869 Fkill_emacs (Qnil);
7870
7871 ensure_echo_area_buffers ();
7872
7873 if (!message_buf_print)
7874 {
7875 /* A message has been output since the last time we printed.
7876 Choose a fresh echo area buffer. */
7877 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7878 echo_area_buffer[0] = echo_buffer[1];
7879 else
7880 echo_area_buffer[0] = echo_buffer[0];
7881
7882 /* Switch to that buffer and clear it. */
7883 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7884 current_buffer->truncate_lines = Qnil;
7885
7886 if (Z > BEG)
7887 {
7888 int count = SPECPDL_INDEX ();
7889 specbind (Qinhibit_read_only, Qt);
7890 /* Note that undo recording is always disabled. */
7891 del_range (BEG, Z);
7892 unbind_to (count, Qnil);
7893 }
7894 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7895
7896 /* Set up the buffer for the multibyteness we need. */
7897 if (multibyte_p
7898 != !NILP (current_buffer->enable_multibyte_characters))
7899 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7900
7901 /* Raise the frame containing the echo area. */
7902 if (minibuffer_auto_raise)
7903 {
7904 struct frame *sf = SELECTED_FRAME ();
7905 Lisp_Object mini_window;
7906 mini_window = FRAME_MINIBUF_WINDOW (sf);
7907 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7908 }
7909
7910 message_log_maybe_newline ();
7911 message_buf_print = 1;
7912 }
7913 else
7914 {
7915 if (NILP (echo_area_buffer[0]))
7916 {
7917 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7918 echo_area_buffer[0] = echo_buffer[1];
7919 else
7920 echo_area_buffer[0] = echo_buffer[0];
7921 }
7922
7923 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7924 {
7925 /* Someone switched buffers between print requests. */
7926 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7927 current_buffer->truncate_lines = Qnil;
7928 }
7929 }
7930 }
7931
7932
7933 /* Display an echo area message in window W. Value is non-zero if W's
7934 height is changed. If display_last_displayed_message_p is
7935 non-zero, display the message that was last displayed, otherwise
7936 display the current message. */
7937
7938 static int
7939 display_echo_area (w)
7940 struct window *w;
7941 {
7942 int i, no_message_p, window_height_changed_p, count;
7943
7944 /* Temporarily disable garbage collections while displaying the echo
7945 area. This is done because a GC can print a message itself.
7946 That message would modify the echo area buffer's contents while a
7947 redisplay of the buffer is going on, and seriously confuse
7948 redisplay. */
7949 count = inhibit_garbage_collection ();
7950
7951 /* If there is no message, we must call display_echo_area_1
7952 nevertheless because it resizes the window. But we will have to
7953 reset the echo_area_buffer in question to nil at the end because
7954 with_echo_area_buffer will sets it to an empty buffer. */
7955 i = display_last_displayed_message_p ? 1 : 0;
7956 no_message_p = NILP (echo_area_buffer[i]);
7957
7958 window_height_changed_p
7959 = with_echo_area_buffer (w, display_last_displayed_message_p,
7960 display_echo_area_1,
7961 (EMACS_INT) w, Qnil, 0, 0);
7962
7963 if (no_message_p)
7964 echo_area_buffer[i] = Qnil;
7965
7966 unbind_to (count, Qnil);
7967 return window_height_changed_p;
7968 }
7969
7970
7971 /* Helper for display_echo_area. Display the current buffer which
7972 contains the current echo area message in window W, a mini-window,
7973 a pointer to which is passed in A1. A2..A4 are currently not used.
7974 Change the height of W so that all of the message is displayed.
7975 Value is non-zero if height of W was changed. */
7976
7977 static int
7978 display_echo_area_1 (a1, a2, a3, a4)
7979 EMACS_INT a1;
7980 Lisp_Object a2;
7981 EMACS_INT a3, a4;
7982 {
7983 struct window *w = (struct window *) a1;
7984 Lisp_Object window;
7985 struct text_pos start;
7986 int window_height_changed_p = 0;
7987
7988 /* Do this before displaying, so that we have a large enough glyph
7989 matrix for the display. If we can't get enough space for the
7990 whole text, display the last N lines. That works by setting w->start. */
7991 window_height_changed_p = resize_mini_window (w, 0);
7992
7993 /* Use the starting position chosen by resize_mini_window. */
7994 SET_TEXT_POS_FROM_MARKER (start, w->start);
7995
7996 /* Display. */
7997 clear_glyph_matrix (w->desired_matrix);
7998 XSETWINDOW (window, w);
7999 try_window (window, start, 0);
8000
8001 return window_height_changed_p;
8002 }
8003
8004
8005 /* Resize the echo area window to exactly the size needed for the
8006 currently displayed message, if there is one. If a mini-buffer
8007 is active, don't shrink it. */
8008
8009 void
8010 resize_echo_area_exactly ()
8011 {
8012 if (BUFFERP (echo_area_buffer[0])
8013 && WINDOWP (echo_area_window))
8014 {
8015 struct window *w = XWINDOW (echo_area_window);
8016 int resized_p;
8017 Lisp_Object resize_exactly;
8018
8019 if (minibuf_level == 0)
8020 resize_exactly = Qt;
8021 else
8022 resize_exactly = Qnil;
8023
8024 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8025 (EMACS_INT) w, resize_exactly, 0, 0);
8026 if (resized_p)
8027 {
8028 ++windows_or_buffers_changed;
8029 ++update_mode_lines;
8030 redisplay_internal (0);
8031 }
8032 }
8033 }
8034
8035
8036 /* Callback function for with_echo_area_buffer, when used from
8037 resize_echo_area_exactly. A1 contains a pointer to the window to
8038 resize, EXACTLY non-nil means resize the mini-window exactly to the
8039 size of the text displayed. A3 and A4 are not used. Value is what
8040 resize_mini_window returns. */
8041
8042 static int
8043 resize_mini_window_1 (a1, exactly, a3, a4)
8044 EMACS_INT a1;
8045 Lisp_Object exactly;
8046 EMACS_INT a3, a4;
8047 {
8048 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8049 }
8050
8051
8052 /* Resize mini-window W to fit the size of its contents. EXACT:P
8053 means size the window exactly to the size needed. Otherwise, it's
8054 only enlarged until W's buffer is empty.
8055
8056 Set W->start to the right place to begin display. If the whole
8057 contents fit, start at the beginning. Otherwise, start so as
8058 to make the end of the contents appear. This is particularly
8059 important for y-or-n-p, but seems desirable generally.
8060
8061 Value is non-zero if the window height has been changed. */
8062
8063 int
8064 resize_mini_window (w, exact_p)
8065 struct window *w;
8066 int exact_p;
8067 {
8068 struct frame *f = XFRAME (w->frame);
8069 int window_height_changed_p = 0;
8070
8071 xassert (MINI_WINDOW_P (w));
8072
8073 /* By default, start display at the beginning. */
8074 set_marker_both (w->start, w->buffer,
8075 BUF_BEGV (XBUFFER (w->buffer)),
8076 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8077
8078 /* Don't resize windows while redisplaying a window; it would
8079 confuse redisplay functions when the size of the window they are
8080 displaying changes from under them. Such a resizing can happen,
8081 for instance, when which-func prints a long message while
8082 we are running fontification-functions. We're running these
8083 functions with safe_call which binds inhibit-redisplay to t. */
8084 if (!NILP (Vinhibit_redisplay))
8085 return 0;
8086
8087 /* Nil means don't try to resize. */
8088 if (NILP (Vresize_mini_windows)
8089 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8090 return 0;
8091
8092 if (!FRAME_MINIBUF_ONLY_P (f))
8093 {
8094 struct it it;
8095 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8096 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8097 int height, max_height;
8098 int unit = FRAME_LINE_HEIGHT (f);
8099 struct text_pos start;
8100 struct buffer *old_current_buffer = NULL;
8101
8102 if (current_buffer != XBUFFER (w->buffer))
8103 {
8104 old_current_buffer = current_buffer;
8105 set_buffer_internal (XBUFFER (w->buffer));
8106 }
8107
8108 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8109
8110 /* Compute the max. number of lines specified by the user. */
8111 if (FLOATP (Vmax_mini_window_height))
8112 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8113 else if (INTEGERP (Vmax_mini_window_height))
8114 max_height = XINT (Vmax_mini_window_height);
8115 else
8116 max_height = total_height / 4;
8117
8118 /* Correct that max. height if it's bogus. */
8119 max_height = max (1, max_height);
8120 max_height = min (total_height, max_height);
8121
8122 /* Find out the height of the text in the window. */
8123 if (it.truncate_lines_p)
8124 height = 1;
8125 else
8126 {
8127 last_height = 0;
8128 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8129 if (it.max_ascent == 0 && it.max_descent == 0)
8130 height = it.current_y + last_height;
8131 else
8132 height = it.current_y + it.max_ascent + it.max_descent;
8133 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8134 height = (height + unit - 1) / unit;
8135 }
8136
8137 /* Compute a suitable window start. */
8138 if (height > max_height)
8139 {
8140 height = max_height;
8141 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8142 move_it_vertically_backward (&it, (height - 1) * unit);
8143 start = it.current.pos;
8144 }
8145 else
8146 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8147 SET_MARKER_FROM_TEXT_POS (w->start, start);
8148
8149 if (EQ (Vresize_mini_windows, Qgrow_only))
8150 {
8151 /* Let it grow only, until we display an empty message, in which
8152 case the window shrinks again. */
8153 if (height > WINDOW_TOTAL_LINES (w))
8154 {
8155 int old_height = WINDOW_TOTAL_LINES (w);
8156 freeze_window_starts (f, 1);
8157 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8158 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8159 }
8160 else if (height < WINDOW_TOTAL_LINES (w)
8161 && (exact_p || BEGV == ZV))
8162 {
8163 int old_height = WINDOW_TOTAL_LINES (w);
8164 freeze_window_starts (f, 0);
8165 shrink_mini_window (w);
8166 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8167 }
8168 }
8169 else
8170 {
8171 /* Always resize to exact size needed. */
8172 if (height > WINDOW_TOTAL_LINES (w))
8173 {
8174 int old_height = WINDOW_TOTAL_LINES (w);
8175 freeze_window_starts (f, 1);
8176 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8177 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8178 }
8179 else if (height < WINDOW_TOTAL_LINES (w))
8180 {
8181 int old_height = WINDOW_TOTAL_LINES (w);
8182 freeze_window_starts (f, 0);
8183 shrink_mini_window (w);
8184
8185 if (height)
8186 {
8187 freeze_window_starts (f, 1);
8188 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8189 }
8190
8191 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8192 }
8193 }
8194
8195 if (old_current_buffer)
8196 set_buffer_internal (old_current_buffer);
8197 }
8198
8199 return window_height_changed_p;
8200 }
8201
8202
8203 /* Value is the current message, a string, or nil if there is no
8204 current message. */
8205
8206 Lisp_Object
8207 current_message ()
8208 {
8209 Lisp_Object msg;
8210
8211 if (NILP (echo_area_buffer[0]))
8212 msg = Qnil;
8213 else
8214 {
8215 with_echo_area_buffer (0, 0, current_message_1,
8216 (EMACS_INT) &msg, Qnil, 0, 0);
8217 if (NILP (msg))
8218 echo_area_buffer[0] = Qnil;
8219 }
8220
8221 return msg;
8222 }
8223
8224
8225 static int
8226 current_message_1 (a1, a2, a3, a4)
8227 EMACS_INT a1;
8228 Lisp_Object a2;
8229 EMACS_INT a3, a4;
8230 {
8231 Lisp_Object *msg = (Lisp_Object *) a1;
8232
8233 if (Z > BEG)
8234 *msg = make_buffer_string (BEG, Z, 1);
8235 else
8236 *msg = Qnil;
8237 return 0;
8238 }
8239
8240
8241 /* Push the current message on Vmessage_stack for later restauration
8242 by restore_message. Value is non-zero if the current message isn't
8243 empty. This is a relatively infrequent operation, so it's not
8244 worth optimizing. */
8245
8246 int
8247 push_message ()
8248 {
8249 Lisp_Object msg;
8250 msg = current_message ();
8251 Vmessage_stack = Fcons (msg, Vmessage_stack);
8252 return STRINGP (msg);
8253 }
8254
8255
8256 /* Restore message display from the top of Vmessage_stack. */
8257
8258 void
8259 restore_message ()
8260 {
8261 Lisp_Object msg;
8262
8263 xassert (CONSP (Vmessage_stack));
8264 msg = XCAR (Vmessage_stack);
8265 if (STRINGP (msg))
8266 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8267 else
8268 message3_nolog (msg, 0, 0);
8269 }
8270
8271
8272 /* Handler for record_unwind_protect calling pop_message. */
8273
8274 Lisp_Object
8275 pop_message_unwind (dummy)
8276 Lisp_Object dummy;
8277 {
8278 pop_message ();
8279 return Qnil;
8280 }
8281
8282 /* Pop the top-most entry off Vmessage_stack. */
8283
8284 void
8285 pop_message ()
8286 {
8287 xassert (CONSP (Vmessage_stack));
8288 Vmessage_stack = XCDR (Vmessage_stack);
8289 }
8290
8291
8292 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8293 exits. If the stack is not empty, we have a missing pop_message
8294 somewhere. */
8295
8296 void
8297 check_message_stack ()
8298 {
8299 if (!NILP (Vmessage_stack))
8300 abort ();
8301 }
8302
8303
8304 /* Truncate to NCHARS what will be displayed in the echo area the next
8305 time we display it---but don't redisplay it now. */
8306
8307 void
8308 truncate_echo_area (nchars)
8309 int nchars;
8310 {
8311 if (nchars == 0)
8312 echo_area_buffer[0] = Qnil;
8313 /* A null message buffer means that the frame hasn't really been
8314 initialized yet. Error messages get reported properly by
8315 cmd_error, so this must be just an informative message; toss it. */
8316 else if (!noninteractive
8317 && INTERACTIVE
8318 && !NILP (echo_area_buffer[0]))
8319 {
8320 struct frame *sf = SELECTED_FRAME ();
8321 if (FRAME_MESSAGE_BUF (sf))
8322 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8323 }
8324 }
8325
8326
8327 /* Helper function for truncate_echo_area. Truncate the current
8328 message to at most NCHARS characters. */
8329
8330 static int
8331 truncate_message_1 (nchars, a2, a3, a4)
8332 EMACS_INT nchars;
8333 Lisp_Object a2;
8334 EMACS_INT a3, a4;
8335 {
8336 if (BEG + nchars < Z)
8337 del_range (BEG + nchars, Z);
8338 if (Z == BEG)
8339 echo_area_buffer[0] = Qnil;
8340 return 0;
8341 }
8342
8343
8344 /* Set the current message to a substring of S or STRING.
8345
8346 If STRING is a Lisp string, set the message to the first NBYTES
8347 bytes from STRING. NBYTES zero means use the whole string. If
8348 STRING is multibyte, the message will be displayed multibyte.
8349
8350 If S is not null, set the message to the first LEN bytes of S. LEN
8351 zero means use the whole string. MULTIBYTE_P non-zero means S is
8352 multibyte. Display the message multibyte in that case.
8353
8354 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8355 to t before calling set_message_1 (which calls insert).
8356 */
8357
8358 void
8359 set_message (s, string, nbytes, multibyte_p)
8360 const char *s;
8361 Lisp_Object string;
8362 int nbytes, multibyte_p;
8363 {
8364 message_enable_multibyte
8365 = ((s && multibyte_p)
8366 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8367
8368 with_echo_area_buffer (0, 0, set_message_1,
8369 (EMACS_INT) s, string, nbytes, multibyte_p);
8370 message_buf_print = 0;
8371 help_echo_showing_p = 0;
8372 }
8373
8374
8375 /* Helper function for set_message. Arguments have the same meaning
8376 as there, with A1 corresponding to S and A2 corresponding to STRING
8377 This function is called with the echo area buffer being
8378 current. */
8379
8380 static int
8381 set_message_1 (a1, a2, nbytes, multibyte_p)
8382 EMACS_INT a1;
8383 Lisp_Object a2;
8384 EMACS_INT nbytes, multibyte_p;
8385 {
8386 const char *s = (const char *) a1;
8387 Lisp_Object string = a2;
8388
8389 /* Change multibyteness of the echo buffer appropriately. */
8390 if (message_enable_multibyte
8391 != !NILP (current_buffer->enable_multibyte_characters))
8392 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8393
8394 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8395
8396 /* Insert new message at BEG. */
8397 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8398 Ferase_buffer ();
8399
8400 if (STRINGP (string))
8401 {
8402 int nchars;
8403
8404 if (nbytes == 0)
8405 nbytes = SBYTES (string);
8406 nchars = string_byte_to_char (string, nbytes);
8407
8408 /* This function takes care of single/multibyte conversion. We
8409 just have to ensure that the echo area buffer has the right
8410 setting of enable_multibyte_characters. */
8411 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8412 }
8413 else if (s)
8414 {
8415 if (nbytes == 0)
8416 nbytes = strlen (s);
8417
8418 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8419 {
8420 /* Convert from multi-byte to single-byte. */
8421 int i, c, n;
8422 unsigned char work[1];
8423
8424 /* Convert a multibyte string to single-byte. */
8425 for (i = 0; i < nbytes; i += n)
8426 {
8427 c = string_char_and_length (s + i, nbytes - i, &n);
8428 work[0] = (SINGLE_BYTE_CHAR_P (c)
8429 ? c
8430 : multibyte_char_to_unibyte (c, Qnil));
8431 insert_1_both (work, 1, 1, 1, 0, 0);
8432 }
8433 }
8434 else if (!multibyte_p
8435 && !NILP (current_buffer->enable_multibyte_characters))
8436 {
8437 /* Convert from single-byte to multi-byte. */
8438 int i, c, n;
8439 const unsigned char *msg = (const unsigned char *) s;
8440 unsigned char str[MAX_MULTIBYTE_LENGTH];
8441
8442 /* Convert a single-byte string to multibyte. */
8443 for (i = 0; i < nbytes; i++)
8444 {
8445 c = unibyte_char_to_multibyte (msg[i]);
8446 n = CHAR_STRING (c, str);
8447 insert_1_both (str, 1, n, 1, 0, 0);
8448 }
8449 }
8450 else
8451 insert_1 (s, nbytes, 1, 0, 0);
8452 }
8453
8454 return 0;
8455 }
8456
8457
8458 /* Clear messages. CURRENT_P non-zero means clear the current
8459 message. LAST_DISPLAYED_P non-zero means clear the message
8460 last displayed. */
8461
8462 void
8463 clear_message (current_p, last_displayed_p)
8464 int current_p, last_displayed_p;
8465 {
8466 if (current_p)
8467 {
8468 echo_area_buffer[0] = Qnil;
8469 message_cleared_p = 1;
8470 }
8471
8472 if (last_displayed_p)
8473 echo_area_buffer[1] = Qnil;
8474
8475 message_buf_print = 0;
8476 }
8477
8478 /* Clear garbaged frames.
8479
8480 This function is used where the old redisplay called
8481 redraw_garbaged_frames which in turn called redraw_frame which in
8482 turn called clear_frame. The call to clear_frame was a source of
8483 flickering. I believe a clear_frame is not necessary. It should
8484 suffice in the new redisplay to invalidate all current matrices,
8485 and ensure a complete redisplay of all windows. */
8486
8487 static void
8488 clear_garbaged_frames ()
8489 {
8490 if (frame_garbaged)
8491 {
8492 Lisp_Object tail, frame;
8493 int changed_count = 0;
8494
8495 FOR_EACH_FRAME (tail, frame)
8496 {
8497 struct frame *f = XFRAME (frame);
8498
8499 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8500 {
8501 if (f->resized_p)
8502 {
8503 Fredraw_frame (frame);
8504 f->force_flush_display_p = 1;
8505 }
8506 clear_current_matrices (f);
8507 changed_count++;
8508 f->garbaged = 0;
8509 f->resized_p = 0;
8510 }
8511 }
8512
8513 frame_garbaged = 0;
8514 if (changed_count)
8515 ++windows_or_buffers_changed;
8516 }
8517 }
8518
8519
8520 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8521 is non-zero update selected_frame. Value is non-zero if the
8522 mini-windows height has been changed. */
8523
8524 static int
8525 echo_area_display (update_frame_p)
8526 int update_frame_p;
8527 {
8528 Lisp_Object mini_window;
8529 struct window *w;
8530 struct frame *f;
8531 int window_height_changed_p = 0;
8532 struct frame *sf = SELECTED_FRAME ();
8533
8534 mini_window = FRAME_MINIBUF_WINDOW (sf);
8535 w = XWINDOW (mini_window);
8536 f = XFRAME (WINDOW_FRAME (w));
8537
8538 /* Don't display if frame is invisible or not yet initialized. */
8539 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8540 return 0;
8541
8542 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8543 #ifndef MAC_OS8
8544 #ifdef HAVE_WINDOW_SYSTEM
8545 /* When Emacs starts, selected_frame may be a visible terminal
8546 frame, even if we run under a window system. If we let this
8547 through, a message would be displayed on the terminal. */
8548 if (EQ (selected_frame, Vterminal_frame)
8549 && !NILP (Vwindow_system))
8550 return 0;
8551 #endif /* HAVE_WINDOW_SYSTEM */
8552 #endif
8553
8554 /* Redraw garbaged frames. */
8555 if (frame_garbaged)
8556 clear_garbaged_frames ();
8557
8558 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8559 {
8560 echo_area_window = mini_window;
8561 window_height_changed_p = display_echo_area (w);
8562 w->must_be_updated_p = 1;
8563
8564 /* Update the display, unless called from redisplay_internal.
8565 Also don't update the screen during redisplay itself. The
8566 update will happen at the end of redisplay, and an update
8567 here could cause confusion. */
8568 if (update_frame_p && !redisplaying_p)
8569 {
8570 int n = 0;
8571
8572 /* If the display update has been interrupted by pending
8573 input, update mode lines in the frame. Due to the
8574 pending input, it might have been that redisplay hasn't
8575 been called, so that mode lines above the echo area are
8576 garbaged. This looks odd, so we prevent it here. */
8577 if (!display_completed)
8578 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8579
8580 if (window_height_changed_p
8581 /* Don't do this if Emacs is shutting down. Redisplay
8582 needs to run hooks. */
8583 && !NILP (Vrun_hooks))
8584 {
8585 /* Must update other windows. Likewise as in other
8586 cases, don't let this update be interrupted by
8587 pending input. */
8588 int count = SPECPDL_INDEX ();
8589 specbind (Qredisplay_dont_pause, Qt);
8590 windows_or_buffers_changed = 1;
8591 redisplay_internal (0);
8592 unbind_to (count, Qnil);
8593 }
8594 else if (FRAME_WINDOW_P (f) && n == 0)
8595 {
8596 /* Window configuration is the same as before.
8597 Can do with a display update of the echo area,
8598 unless we displayed some mode lines. */
8599 update_single_window (w, 1);
8600 rif->flush_display (f);
8601 }
8602 else
8603 update_frame (f, 1, 1);
8604
8605 /* If cursor is in the echo area, make sure that the next
8606 redisplay displays the minibuffer, so that the cursor will
8607 be replaced with what the minibuffer wants. */
8608 if (cursor_in_echo_area)
8609 ++windows_or_buffers_changed;
8610 }
8611 }
8612 else if (!EQ (mini_window, selected_window))
8613 windows_or_buffers_changed++;
8614
8615 /* The current message is now also the last one displayed. */
8616 echo_area_buffer[1] = echo_area_buffer[0];
8617
8618 /* Prevent redisplay optimization in redisplay_internal by resetting
8619 this_line_start_pos. This is done because the mini-buffer now
8620 displays the message instead of its buffer text. */
8621 if (EQ (mini_window, selected_window))
8622 CHARPOS (this_line_start_pos) = 0;
8623
8624 return window_height_changed_p;
8625 }
8626
8627
8628 \f
8629 /***********************************************************************
8630 Mode Lines and Frame Titles
8631 ***********************************************************************/
8632
8633 /* A buffer for constructing non-propertized mode-line strings and
8634 frame titles in it; allocated from the heap in init_xdisp and
8635 resized as needed in store_mode_line_noprop_char. */
8636
8637 static char *mode_line_noprop_buf;
8638
8639 /* The buffer's end, and a current output position in it. */
8640
8641 static char *mode_line_noprop_buf_end;
8642 static char *mode_line_noprop_ptr;
8643
8644 #define MODE_LINE_NOPROP_LEN(start) \
8645 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8646
8647 static enum {
8648 MODE_LINE_DISPLAY = 0,
8649 MODE_LINE_TITLE,
8650 MODE_LINE_NOPROP,
8651 MODE_LINE_STRING
8652 } mode_line_target;
8653
8654 /* Alist that caches the results of :propertize.
8655 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8656 static Lisp_Object mode_line_proptrans_alist;
8657
8658 /* List of strings making up the mode-line. */
8659 static Lisp_Object mode_line_string_list;
8660
8661 /* Base face property when building propertized mode line string. */
8662 static Lisp_Object mode_line_string_face;
8663 static Lisp_Object mode_line_string_face_prop;
8664
8665
8666 /* Unwind data for mode line strings */
8667
8668 static Lisp_Object Vmode_line_unwind_vector;
8669
8670 static Lisp_Object
8671 format_mode_line_unwind_data (obuf, save_proptrans)
8672 struct buffer *obuf;
8673 {
8674 Lisp_Object vector;
8675
8676 /* Reduce consing by keeping one vector in
8677 Vwith_echo_area_save_vector. */
8678 vector = Vmode_line_unwind_vector;
8679 Vmode_line_unwind_vector = Qnil;
8680
8681 if (NILP (vector))
8682 vector = Fmake_vector (make_number (7), Qnil);
8683
8684 AREF (vector, 0) = make_number (mode_line_target);
8685 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8686 AREF (vector, 2) = mode_line_string_list;
8687 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8688 AREF (vector, 4) = mode_line_string_face;
8689 AREF (vector, 5) = mode_line_string_face_prop;
8690
8691 if (obuf)
8692 XSETBUFFER (AREF (vector, 6), obuf);
8693 else
8694 AREF (vector, 6) = Qnil;
8695
8696 return vector;
8697 }
8698
8699 static Lisp_Object
8700 unwind_format_mode_line (vector)
8701 Lisp_Object vector;
8702 {
8703 mode_line_target = XINT (AREF (vector, 0));
8704 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8705 mode_line_string_list = AREF (vector, 2);
8706 if (! EQ (AREF (vector, 3), Qt))
8707 mode_line_proptrans_alist = AREF (vector, 3);
8708 mode_line_string_face = AREF (vector, 4);
8709 mode_line_string_face_prop = AREF (vector, 5);
8710
8711 if (!NILP (AREF (vector, 6)))
8712 {
8713 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8714 AREF (vector, 6) = Qnil;
8715 }
8716
8717 Vmode_line_unwind_vector = vector;
8718 return Qnil;
8719 }
8720
8721
8722 /* Store a single character C for the frame title in mode_line_noprop_buf.
8723 Re-allocate mode_line_noprop_buf if necessary. */
8724
8725 static void
8726 #ifdef PROTOTYPES
8727 store_mode_line_noprop_char (char c)
8728 #else
8729 store_mode_line_noprop_char (c)
8730 char c;
8731 #endif
8732 {
8733 /* If output position has reached the end of the allocated buffer,
8734 double the buffer's size. */
8735 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8736 {
8737 int len = MODE_LINE_NOPROP_LEN (0);
8738 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8739 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8740 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8741 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8742 }
8743
8744 *mode_line_noprop_ptr++ = c;
8745 }
8746
8747
8748 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8749 mode_line_noprop_ptr. STR is the string to store. Do not copy
8750 characters that yield more columns than PRECISION; PRECISION <= 0
8751 means copy the whole string. Pad with spaces until FIELD_WIDTH
8752 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8753 pad. Called from display_mode_element when it is used to build a
8754 frame title. */
8755
8756 static int
8757 store_mode_line_noprop (str, field_width, precision)
8758 const unsigned char *str;
8759 int field_width, precision;
8760 {
8761 int n = 0;
8762 int dummy, nbytes;
8763
8764 /* Copy at most PRECISION chars from STR. */
8765 nbytes = strlen (str);
8766 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8767 while (nbytes--)
8768 store_mode_line_noprop_char (*str++);
8769
8770 /* Fill up with spaces until FIELD_WIDTH reached. */
8771 while (field_width > 0
8772 && n < field_width)
8773 {
8774 store_mode_line_noprop_char (' ');
8775 ++n;
8776 }
8777
8778 return n;
8779 }
8780
8781 /***********************************************************************
8782 Frame Titles
8783 ***********************************************************************/
8784
8785 #ifdef HAVE_WINDOW_SYSTEM
8786
8787 /* Set the title of FRAME, if it has changed. The title format is
8788 Vicon_title_format if FRAME is iconified, otherwise it is
8789 frame_title_format. */
8790
8791 static void
8792 x_consider_frame_title (frame)
8793 Lisp_Object frame;
8794 {
8795 struct frame *f = XFRAME (frame);
8796
8797 if (FRAME_WINDOW_P (f)
8798 || FRAME_MINIBUF_ONLY_P (f)
8799 || f->explicit_name)
8800 {
8801 /* Do we have more than one visible frame on this X display? */
8802 Lisp_Object tail;
8803 Lisp_Object fmt;
8804 int title_start;
8805 char *title;
8806 int len;
8807 struct it it;
8808 int count = SPECPDL_INDEX ();
8809
8810 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8811 {
8812 Lisp_Object other_frame = XCAR (tail);
8813 struct frame *tf = XFRAME (other_frame);
8814
8815 if (tf != f
8816 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8817 && !FRAME_MINIBUF_ONLY_P (tf)
8818 && !EQ (other_frame, tip_frame)
8819 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8820 break;
8821 }
8822
8823 /* Set global variable indicating that multiple frames exist. */
8824 multiple_frames = CONSP (tail);
8825
8826 /* Switch to the buffer of selected window of the frame. Set up
8827 mode_line_target so that display_mode_element will output into
8828 mode_line_noprop_buf; then display the title. */
8829 record_unwind_protect (unwind_format_mode_line,
8830 format_mode_line_unwind_data (current_buffer, 0));
8831
8832 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8833 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8834
8835 mode_line_target = MODE_LINE_TITLE;
8836 title_start = MODE_LINE_NOPROP_LEN (0);
8837 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8838 NULL, DEFAULT_FACE_ID);
8839 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8840 len = MODE_LINE_NOPROP_LEN (title_start);
8841 title = mode_line_noprop_buf + title_start;
8842 unbind_to (count, Qnil);
8843
8844 /* Set the title only if it's changed. This avoids consing in
8845 the common case where it hasn't. (If it turns out that we've
8846 already wasted too much time by walking through the list with
8847 display_mode_element, then we might need to optimize at a
8848 higher level than this.) */
8849 if (! STRINGP (f->name)
8850 || SBYTES (f->name) != len
8851 || bcmp (title, SDATA (f->name), len) != 0)
8852 x_implicitly_set_name (f, make_string (title, len), Qnil);
8853 }
8854 }
8855
8856 #endif /* not HAVE_WINDOW_SYSTEM */
8857
8858
8859
8860 \f
8861 /***********************************************************************
8862 Menu Bars
8863 ***********************************************************************/
8864
8865
8866 /* Prepare for redisplay by updating menu-bar item lists when
8867 appropriate. This can call eval. */
8868
8869 void
8870 prepare_menu_bars ()
8871 {
8872 int all_windows;
8873 struct gcpro gcpro1, gcpro2;
8874 struct frame *f;
8875 Lisp_Object tooltip_frame;
8876
8877 #ifdef HAVE_WINDOW_SYSTEM
8878 tooltip_frame = tip_frame;
8879 #else
8880 tooltip_frame = Qnil;
8881 #endif
8882
8883 /* Update all frame titles based on their buffer names, etc. We do
8884 this before the menu bars so that the buffer-menu will show the
8885 up-to-date frame titles. */
8886 #ifdef HAVE_WINDOW_SYSTEM
8887 if (windows_or_buffers_changed || update_mode_lines)
8888 {
8889 Lisp_Object tail, frame;
8890
8891 FOR_EACH_FRAME (tail, frame)
8892 {
8893 f = XFRAME (frame);
8894 if (!EQ (frame, tooltip_frame)
8895 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8896 x_consider_frame_title (frame);
8897 }
8898 }
8899 #endif /* HAVE_WINDOW_SYSTEM */
8900
8901 /* Update the menu bar item lists, if appropriate. This has to be
8902 done before any actual redisplay or generation of display lines. */
8903 all_windows = (update_mode_lines
8904 || buffer_shared > 1
8905 || windows_or_buffers_changed);
8906 if (all_windows)
8907 {
8908 Lisp_Object tail, frame;
8909 int count = SPECPDL_INDEX ();
8910
8911 record_unwind_save_match_data ();
8912
8913 FOR_EACH_FRAME (tail, frame)
8914 {
8915 f = XFRAME (frame);
8916
8917 /* Ignore tooltip frame. */
8918 if (EQ (frame, tooltip_frame))
8919 continue;
8920
8921 /* If a window on this frame changed size, report that to
8922 the user and clear the size-change flag. */
8923 if (FRAME_WINDOW_SIZES_CHANGED (f))
8924 {
8925 Lisp_Object functions;
8926
8927 /* Clear flag first in case we get an error below. */
8928 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8929 functions = Vwindow_size_change_functions;
8930 GCPRO2 (tail, functions);
8931
8932 while (CONSP (functions))
8933 {
8934 call1 (XCAR (functions), frame);
8935 functions = XCDR (functions);
8936 }
8937 UNGCPRO;
8938 }
8939
8940 GCPRO1 (tail);
8941 update_menu_bar (f, 0);
8942 #ifdef HAVE_WINDOW_SYSTEM
8943 update_tool_bar (f, 0);
8944 #endif
8945 UNGCPRO;
8946 }
8947
8948 unbind_to (count, Qnil);
8949 }
8950 else
8951 {
8952 struct frame *sf = SELECTED_FRAME ();
8953 update_menu_bar (sf, 1);
8954 #ifdef HAVE_WINDOW_SYSTEM
8955 update_tool_bar (sf, 1);
8956 #endif
8957 }
8958
8959 /* Motif needs this. See comment in xmenu.c. Turn it off when
8960 pending_menu_activation is not defined. */
8961 #ifdef USE_X_TOOLKIT
8962 pending_menu_activation = 0;
8963 #endif
8964 }
8965
8966
8967 /* Update the menu bar item list for frame F. This has to be done
8968 before we start to fill in any display lines, because it can call
8969 eval.
8970
8971 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8972
8973 static void
8974 update_menu_bar (f, save_match_data)
8975 struct frame *f;
8976 int save_match_data;
8977 {
8978 Lisp_Object window;
8979 register struct window *w;
8980
8981 /* If called recursively during a menu update, do nothing. This can
8982 happen when, for instance, an activate-menubar-hook causes a
8983 redisplay. */
8984 if (inhibit_menubar_update)
8985 return;
8986
8987 window = FRAME_SELECTED_WINDOW (f);
8988 w = XWINDOW (window);
8989
8990 #if 0 /* The if statement below this if statement used to include the
8991 condition !NILP (w->update_mode_line), rather than using
8992 update_mode_lines directly, and this if statement may have
8993 been added to make that condition work. Now the if
8994 statement below matches its comment, this isn't needed. */
8995 if (update_mode_lines)
8996 w->update_mode_line = Qt;
8997 #endif
8998
8999 if (FRAME_WINDOW_P (f)
9000 ?
9001 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9002 || defined (USE_GTK)
9003 FRAME_EXTERNAL_MENU_BAR (f)
9004 #else
9005 FRAME_MENU_BAR_LINES (f) > 0
9006 #endif
9007 : FRAME_MENU_BAR_LINES (f) > 0)
9008 {
9009 /* If the user has switched buffers or windows, we need to
9010 recompute to reflect the new bindings. But we'll
9011 recompute when update_mode_lines is set too; that means
9012 that people can use force-mode-line-update to request
9013 that the menu bar be recomputed. The adverse effect on
9014 the rest of the redisplay algorithm is about the same as
9015 windows_or_buffers_changed anyway. */
9016 if (windows_or_buffers_changed
9017 /* This used to test w->update_mode_line, but we believe
9018 there is no need to recompute the menu in that case. */
9019 || update_mode_lines
9020 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9021 < BUF_MODIFF (XBUFFER (w->buffer)))
9022 != !NILP (w->last_had_star))
9023 || ((!NILP (Vtransient_mark_mode)
9024 && !NILP (XBUFFER (w->buffer)->mark_active))
9025 != !NILP (w->region_showing)))
9026 {
9027 struct buffer *prev = current_buffer;
9028 int count = SPECPDL_INDEX ();
9029
9030 specbind (Qinhibit_menubar_update, Qt);
9031
9032 set_buffer_internal_1 (XBUFFER (w->buffer));
9033 if (save_match_data)
9034 record_unwind_save_match_data ();
9035 if (NILP (Voverriding_local_map_menu_flag))
9036 {
9037 specbind (Qoverriding_terminal_local_map, Qnil);
9038 specbind (Qoverriding_local_map, Qnil);
9039 }
9040
9041 /* Run the Lucid hook. */
9042 safe_run_hooks (Qactivate_menubar_hook);
9043
9044 /* If it has changed current-menubar from previous value,
9045 really recompute the menu-bar from the value. */
9046 if (! NILP (Vlucid_menu_bar_dirty_flag))
9047 call0 (Qrecompute_lucid_menubar);
9048
9049 safe_run_hooks (Qmenu_bar_update_hook);
9050 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9051
9052 /* Redisplay the menu bar in case we changed it. */
9053 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9054 || defined (USE_GTK)
9055 if (FRAME_WINDOW_P (f))
9056 {
9057 #ifdef MAC_OS
9058 /* All frames on Mac OS share the same menubar. So only
9059 the selected frame should be allowed to set it. */
9060 if (f == SELECTED_FRAME ())
9061 #endif
9062 set_frame_menubar (f, 0, 0);
9063 }
9064 else
9065 /* On a terminal screen, the menu bar is an ordinary screen
9066 line, and this makes it get updated. */
9067 w->update_mode_line = Qt;
9068 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9069 /* In the non-toolkit version, the menu bar is an ordinary screen
9070 line, and this makes it get updated. */
9071 w->update_mode_line = Qt;
9072 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9073
9074 unbind_to (count, Qnil);
9075 set_buffer_internal_1 (prev);
9076 }
9077 }
9078 }
9079
9080
9081 \f
9082 /***********************************************************************
9083 Output Cursor
9084 ***********************************************************************/
9085
9086 #ifdef HAVE_WINDOW_SYSTEM
9087
9088 /* EXPORT:
9089 Nominal cursor position -- where to draw output.
9090 HPOS and VPOS are window relative glyph matrix coordinates.
9091 X and Y are window relative pixel coordinates. */
9092
9093 struct cursor_pos output_cursor;
9094
9095
9096 /* EXPORT:
9097 Set the global variable output_cursor to CURSOR. All cursor
9098 positions are relative to updated_window. */
9099
9100 void
9101 set_output_cursor (cursor)
9102 struct cursor_pos *cursor;
9103 {
9104 output_cursor.hpos = cursor->hpos;
9105 output_cursor.vpos = cursor->vpos;
9106 output_cursor.x = cursor->x;
9107 output_cursor.y = cursor->y;
9108 }
9109
9110
9111 /* EXPORT for RIF:
9112 Set a nominal cursor position.
9113
9114 HPOS and VPOS are column/row positions in a window glyph matrix. X
9115 and Y are window text area relative pixel positions.
9116
9117 If this is done during an update, updated_window will contain the
9118 window that is being updated and the position is the future output
9119 cursor position for that window. If updated_window is null, use
9120 selected_window and display the cursor at the given position. */
9121
9122 void
9123 x_cursor_to (vpos, hpos, y, x)
9124 int vpos, hpos, y, x;
9125 {
9126 struct window *w;
9127
9128 /* If updated_window is not set, work on selected_window. */
9129 if (updated_window)
9130 w = updated_window;
9131 else
9132 w = XWINDOW (selected_window);
9133
9134 /* Set the output cursor. */
9135 output_cursor.hpos = hpos;
9136 output_cursor.vpos = vpos;
9137 output_cursor.x = x;
9138 output_cursor.y = y;
9139
9140 /* If not called as part of an update, really display the cursor.
9141 This will also set the cursor position of W. */
9142 if (updated_window == NULL)
9143 {
9144 BLOCK_INPUT;
9145 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9146 if (rif->flush_display_optional)
9147 rif->flush_display_optional (SELECTED_FRAME ());
9148 UNBLOCK_INPUT;
9149 }
9150 }
9151
9152 #endif /* HAVE_WINDOW_SYSTEM */
9153
9154 \f
9155 /***********************************************************************
9156 Tool-bars
9157 ***********************************************************************/
9158
9159 #ifdef HAVE_WINDOW_SYSTEM
9160
9161 /* Where the mouse was last time we reported a mouse event. */
9162
9163 FRAME_PTR last_mouse_frame;
9164
9165 /* Tool-bar item index of the item on which a mouse button was pressed
9166 or -1. */
9167
9168 int last_tool_bar_item;
9169
9170
9171 /* Update the tool-bar item list for frame F. This has to be done
9172 before we start to fill in any display lines. Called from
9173 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9174 and restore it here. */
9175
9176 static void
9177 update_tool_bar (f, save_match_data)
9178 struct frame *f;
9179 int save_match_data;
9180 {
9181 #ifdef USE_GTK
9182 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9183 #else
9184 int do_update = WINDOWP (f->tool_bar_window)
9185 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9186 #endif
9187
9188 if (do_update)
9189 {
9190 Lisp_Object window;
9191 struct window *w;
9192
9193 window = FRAME_SELECTED_WINDOW (f);
9194 w = XWINDOW (window);
9195
9196 /* If the user has switched buffers or windows, we need to
9197 recompute to reflect the new bindings. But we'll
9198 recompute when update_mode_lines is set too; that means
9199 that people can use force-mode-line-update to request
9200 that the menu bar be recomputed. The adverse effect on
9201 the rest of the redisplay algorithm is about the same as
9202 windows_or_buffers_changed anyway. */
9203 if (windows_or_buffers_changed
9204 || !NILP (w->update_mode_line)
9205 || update_mode_lines
9206 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9207 < BUF_MODIFF (XBUFFER (w->buffer)))
9208 != !NILP (w->last_had_star))
9209 || ((!NILP (Vtransient_mark_mode)
9210 && !NILP (XBUFFER (w->buffer)->mark_active))
9211 != !NILP (w->region_showing)))
9212 {
9213 struct buffer *prev = current_buffer;
9214 int count = SPECPDL_INDEX ();
9215 Lisp_Object new_tool_bar;
9216 int new_n_tool_bar;
9217 struct gcpro gcpro1;
9218
9219 /* Set current_buffer to the buffer of the selected
9220 window of the frame, so that we get the right local
9221 keymaps. */
9222 set_buffer_internal_1 (XBUFFER (w->buffer));
9223
9224 /* Save match data, if we must. */
9225 if (save_match_data)
9226 record_unwind_save_match_data ();
9227
9228 /* Make sure that we don't accidentally use bogus keymaps. */
9229 if (NILP (Voverriding_local_map_menu_flag))
9230 {
9231 specbind (Qoverriding_terminal_local_map, Qnil);
9232 specbind (Qoverriding_local_map, Qnil);
9233 }
9234
9235 GCPRO1 (new_tool_bar);
9236
9237 /* Build desired tool-bar items from keymaps. */
9238 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9239 &new_n_tool_bar);
9240
9241 /* Redisplay the tool-bar if we changed it. */
9242 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9243 {
9244 /* Redisplay that happens asynchronously due to an expose event
9245 may access f->tool_bar_items. Make sure we update both
9246 variables within BLOCK_INPUT so no such event interrupts. */
9247 BLOCK_INPUT;
9248 f->tool_bar_items = new_tool_bar;
9249 f->n_tool_bar_items = new_n_tool_bar;
9250 w->update_mode_line = Qt;
9251 UNBLOCK_INPUT;
9252 }
9253
9254 UNGCPRO;
9255
9256 unbind_to (count, Qnil);
9257 set_buffer_internal_1 (prev);
9258 }
9259 }
9260 }
9261
9262
9263 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9264 F's desired tool-bar contents. F->tool_bar_items must have
9265 been set up previously by calling prepare_menu_bars. */
9266
9267 static void
9268 build_desired_tool_bar_string (f)
9269 struct frame *f;
9270 {
9271 int i, size, size_needed;
9272 struct gcpro gcpro1, gcpro2, gcpro3;
9273 Lisp_Object image, plist, props;
9274
9275 image = plist = props = Qnil;
9276 GCPRO3 (image, plist, props);
9277
9278 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9279 Otherwise, make a new string. */
9280
9281 /* The size of the string we might be able to reuse. */
9282 size = (STRINGP (f->desired_tool_bar_string)
9283 ? SCHARS (f->desired_tool_bar_string)
9284 : 0);
9285
9286 /* We need one space in the string for each image. */
9287 size_needed = f->n_tool_bar_items;
9288
9289 /* Reuse f->desired_tool_bar_string, if possible. */
9290 if (size < size_needed || NILP (f->desired_tool_bar_string))
9291 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9292 make_number (' '));
9293 else
9294 {
9295 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9296 Fremove_text_properties (make_number (0), make_number (size),
9297 props, f->desired_tool_bar_string);
9298 }
9299
9300 /* Put a `display' property on the string for the images to display,
9301 put a `menu_item' property on tool-bar items with a value that
9302 is the index of the item in F's tool-bar item vector. */
9303 for (i = 0; i < f->n_tool_bar_items; ++i)
9304 {
9305 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9306
9307 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9308 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9309 int hmargin, vmargin, relief, idx, end;
9310 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9311
9312 /* If image is a vector, choose the image according to the
9313 button state. */
9314 image = PROP (TOOL_BAR_ITEM_IMAGES);
9315 if (VECTORP (image))
9316 {
9317 if (enabled_p)
9318 idx = (selected_p
9319 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9320 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9321 else
9322 idx = (selected_p
9323 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9324 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9325
9326 xassert (ASIZE (image) >= idx);
9327 image = AREF (image, idx);
9328 }
9329 else
9330 idx = -1;
9331
9332 /* Ignore invalid image specifications. */
9333 if (!valid_image_p (image))
9334 continue;
9335
9336 /* Display the tool-bar button pressed, or depressed. */
9337 plist = Fcopy_sequence (XCDR (image));
9338
9339 /* Compute margin and relief to draw. */
9340 relief = (tool_bar_button_relief >= 0
9341 ? tool_bar_button_relief
9342 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9343 hmargin = vmargin = relief;
9344
9345 if (INTEGERP (Vtool_bar_button_margin)
9346 && XINT (Vtool_bar_button_margin) > 0)
9347 {
9348 hmargin += XFASTINT (Vtool_bar_button_margin);
9349 vmargin += XFASTINT (Vtool_bar_button_margin);
9350 }
9351 else if (CONSP (Vtool_bar_button_margin))
9352 {
9353 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9354 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9355 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9356
9357 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9358 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9359 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9360 }
9361
9362 if (auto_raise_tool_bar_buttons_p)
9363 {
9364 /* Add a `:relief' property to the image spec if the item is
9365 selected. */
9366 if (selected_p)
9367 {
9368 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9369 hmargin -= relief;
9370 vmargin -= relief;
9371 }
9372 }
9373 else
9374 {
9375 /* If image is selected, display it pressed, i.e. with a
9376 negative relief. If it's not selected, display it with a
9377 raised relief. */
9378 plist = Fplist_put (plist, QCrelief,
9379 (selected_p
9380 ? make_number (-relief)
9381 : make_number (relief)));
9382 hmargin -= relief;
9383 vmargin -= relief;
9384 }
9385
9386 /* Put a margin around the image. */
9387 if (hmargin || vmargin)
9388 {
9389 if (hmargin == vmargin)
9390 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9391 else
9392 plist = Fplist_put (plist, QCmargin,
9393 Fcons (make_number (hmargin),
9394 make_number (vmargin)));
9395 }
9396
9397 /* If button is not enabled, and we don't have special images
9398 for the disabled state, make the image appear disabled by
9399 applying an appropriate algorithm to it. */
9400 if (!enabled_p && idx < 0)
9401 plist = Fplist_put (plist, QCconversion, Qdisabled);
9402
9403 /* Put a `display' text property on the string for the image to
9404 display. Put a `menu-item' property on the string that gives
9405 the start of this item's properties in the tool-bar items
9406 vector. */
9407 image = Fcons (Qimage, plist);
9408 props = list4 (Qdisplay, image,
9409 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9410
9411 /* Let the last image hide all remaining spaces in the tool bar
9412 string. The string can be longer than needed when we reuse a
9413 previous string. */
9414 if (i + 1 == f->n_tool_bar_items)
9415 end = SCHARS (f->desired_tool_bar_string);
9416 else
9417 end = i + 1;
9418 Fadd_text_properties (make_number (i), make_number (end),
9419 props, f->desired_tool_bar_string);
9420 #undef PROP
9421 }
9422
9423 UNGCPRO;
9424 }
9425
9426
9427 /* Display one line of the tool-bar of frame IT->f. */
9428
9429 static void
9430 display_tool_bar_line (it)
9431 struct it *it;
9432 {
9433 struct glyph_row *row = it->glyph_row;
9434 int max_x = it->last_visible_x;
9435 struct glyph *last;
9436
9437 prepare_desired_row (row);
9438 row->y = it->current_y;
9439
9440 /* Note that this isn't made use of if the face hasn't a box,
9441 so there's no need to check the face here. */
9442 it->start_of_box_run_p = 1;
9443
9444 while (it->current_x < max_x)
9445 {
9446 int x_before, x, n_glyphs_before, i, nglyphs;
9447
9448 /* Get the next display element. */
9449 if (!get_next_display_element (it))
9450 break;
9451
9452 /* Produce glyphs. */
9453 x_before = it->current_x;
9454 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9455 PRODUCE_GLYPHS (it);
9456
9457 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9458 i = 0;
9459 x = x_before;
9460 while (i < nglyphs)
9461 {
9462 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9463
9464 if (x + glyph->pixel_width > max_x)
9465 {
9466 /* Glyph doesn't fit on line. */
9467 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9468 it->current_x = x;
9469 goto out;
9470 }
9471
9472 ++it->hpos;
9473 x += glyph->pixel_width;
9474 ++i;
9475 }
9476
9477 /* Stop at line ends. */
9478 if (ITERATOR_AT_END_OF_LINE_P (it))
9479 break;
9480
9481 set_iterator_to_next (it, 1);
9482 }
9483
9484 out:;
9485
9486 row->displays_text_p = row->used[TEXT_AREA] != 0;
9487 extend_face_to_end_of_line (it);
9488 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9489 last->right_box_line_p = 1;
9490 if (last == row->glyphs[TEXT_AREA])
9491 last->left_box_line_p = 1;
9492 compute_line_metrics (it);
9493
9494 /* If line is empty, make it occupy the rest of the tool-bar. */
9495 if (!row->displays_text_p)
9496 {
9497 row->height = row->phys_height = it->last_visible_y - row->y;
9498 row->ascent = row->phys_ascent = 0;
9499 row->extra_line_spacing = 0;
9500 }
9501
9502 row->full_width_p = 1;
9503 row->continued_p = 0;
9504 row->truncated_on_left_p = 0;
9505 row->truncated_on_right_p = 0;
9506
9507 it->current_x = it->hpos = 0;
9508 it->current_y += row->height;
9509 ++it->vpos;
9510 ++it->glyph_row;
9511 }
9512
9513
9514 /* Value is the number of screen lines needed to make all tool-bar
9515 items of frame F visible. */
9516
9517 static int
9518 tool_bar_lines_needed (f)
9519 struct frame *f;
9520 {
9521 struct window *w = XWINDOW (f->tool_bar_window);
9522 struct it it;
9523
9524 /* Initialize an iterator for iteration over
9525 F->desired_tool_bar_string in the tool-bar window of frame F. */
9526 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9527 it.first_visible_x = 0;
9528 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9529 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9530
9531 while (!ITERATOR_AT_END_P (&it))
9532 {
9533 it.glyph_row = w->desired_matrix->rows;
9534 clear_glyph_row (it.glyph_row);
9535 display_tool_bar_line (&it);
9536 }
9537
9538 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9539 }
9540
9541
9542 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9543 0, 1, 0,
9544 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9545 (frame)
9546 Lisp_Object frame;
9547 {
9548 struct frame *f;
9549 struct window *w;
9550 int nlines = 0;
9551
9552 if (NILP (frame))
9553 frame = selected_frame;
9554 else
9555 CHECK_FRAME (frame);
9556 f = XFRAME (frame);
9557
9558 if (WINDOWP (f->tool_bar_window)
9559 || (w = XWINDOW (f->tool_bar_window),
9560 WINDOW_TOTAL_LINES (w) > 0))
9561 {
9562 update_tool_bar (f, 1);
9563 if (f->n_tool_bar_items)
9564 {
9565 build_desired_tool_bar_string (f);
9566 nlines = tool_bar_lines_needed (f);
9567 }
9568 }
9569
9570 return make_number (nlines);
9571 }
9572
9573
9574 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9575 height should be changed. */
9576
9577 static int
9578 redisplay_tool_bar (f)
9579 struct frame *f;
9580 {
9581 struct window *w;
9582 struct it it;
9583 struct glyph_row *row;
9584 int change_height_p = 0;
9585
9586 #ifdef USE_GTK
9587 if (FRAME_EXTERNAL_TOOL_BAR (f))
9588 update_frame_tool_bar (f);
9589 return 0;
9590 #endif
9591
9592 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9593 do anything. This means you must start with tool-bar-lines
9594 non-zero to get the auto-sizing effect. Or in other words, you
9595 can turn off tool-bars by specifying tool-bar-lines zero. */
9596 if (!WINDOWP (f->tool_bar_window)
9597 || (w = XWINDOW (f->tool_bar_window),
9598 WINDOW_TOTAL_LINES (w) == 0))
9599 return 0;
9600
9601 /* Set up an iterator for the tool-bar window. */
9602 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9603 it.first_visible_x = 0;
9604 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9605 row = it.glyph_row;
9606
9607 /* Build a string that represents the contents of the tool-bar. */
9608 build_desired_tool_bar_string (f);
9609 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9610
9611 /* Display as many lines as needed to display all tool-bar items. */
9612 while (it.current_y < it.last_visible_y)
9613 display_tool_bar_line (&it);
9614
9615 /* It doesn't make much sense to try scrolling in the tool-bar
9616 window, so don't do it. */
9617 w->desired_matrix->no_scrolling_p = 1;
9618 w->must_be_updated_p = 1;
9619
9620 if (auto_resize_tool_bars_p)
9621 {
9622 int nlines;
9623
9624 /* If we couldn't display everything, change the tool-bar's
9625 height. */
9626 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9627 change_height_p = 1;
9628
9629 /* If there are blank lines at the end, except for a partially
9630 visible blank line at the end that is smaller than
9631 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9632 row = it.glyph_row - 1;
9633 if (!row->displays_text_p
9634 && row->height >= FRAME_LINE_HEIGHT (f))
9635 change_height_p = 1;
9636
9637 /* If row displays tool-bar items, but is partially visible,
9638 change the tool-bar's height. */
9639 if (row->displays_text_p
9640 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9641 change_height_p = 1;
9642
9643 /* Resize windows as needed by changing the `tool-bar-lines'
9644 frame parameter. */
9645 if (change_height_p
9646 && (nlines = tool_bar_lines_needed (f),
9647 nlines != WINDOW_TOTAL_LINES (w)))
9648 {
9649 extern Lisp_Object Qtool_bar_lines;
9650 Lisp_Object frame;
9651 int old_height = WINDOW_TOTAL_LINES (w);
9652
9653 XSETFRAME (frame, f);
9654 clear_glyph_matrix (w->desired_matrix);
9655 Fmodify_frame_parameters (frame,
9656 Fcons (Fcons (Qtool_bar_lines,
9657 make_number (nlines)),
9658 Qnil));
9659 if (WINDOW_TOTAL_LINES (w) != old_height)
9660 fonts_changed_p = 1;
9661 }
9662 }
9663
9664 return change_height_p;
9665 }
9666
9667
9668 /* Get information about the tool-bar item which is displayed in GLYPH
9669 on frame F. Return in *PROP_IDX the index where tool-bar item
9670 properties start in F->tool_bar_items. Value is zero if
9671 GLYPH doesn't display a tool-bar item. */
9672
9673 static int
9674 tool_bar_item_info (f, glyph, prop_idx)
9675 struct frame *f;
9676 struct glyph *glyph;
9677 int *prop_idx;
9678 {
9679 Lisp_Object prop;
9680 int success_p;
9681 int charpos;
9682
9683 /* This function can be called asynchronously, which means we must
9684 exclude any possibility that Fget_text_property signals an
9685 error. */
9686 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9687 charpos = max (0, charpos);
9688
9689 /* Get the text property `menu-item' at pos. The value of that
9690 property is the start index of this item's properties in
9691 F->tool_bar_items. */
9692 prop = Fget_text_property (make_number (charpos),
9693 Qmenu_item, f->current_tool_bar_string);
9694 if (INTEGERP (prop))
9695 {
9696 *prop_idx = XINT (prop);
9697 success_p = 1;
9698 }
9699 else
9700 success_p = 0;
9701
9702 return success_p;
9703 }
9704
9705 \f
9706 /* Get information about the tool-bar item at position X/Y on frame F.
9707 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9708 the current matrix of the tool-bar window of F, or NULL if not
9709 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9710 item in F->tool_bar_items. Value is
9711
9712 -1 if X/Y is not on a tool-bar item
9713 0 if X/Y is on the same item that was highlighted before.
9714 1 otherwise. */
9715
9716 static int
9717 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9718 struct frame *f;
9719 int x, y;
9720 struct glyph **glyph;
9721 int *hpos, *vpos, *prop_idx;
9722 {
9723 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9724 struct window *w = XWINDOW (f->tool_bar_window);
9725 int area;
9726
9727 /* Find the glyph under X/Y. */
9728 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9729 if (*glyph == NULL)
9730 return -1;
9731
9732 /* Get the start of this tool-bar item's properties in
9733 f->tool_bar_items. */
9734 if (!tool_bar_item_info (f, *glyph, prop_idx))
9735 return -1;
9736
9737 /* Is mouse on the highlighted item? */
9738 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9739 && *vpos >= dpyinfo->mouse_face_beg_row
9740 && *vpos <= dpyinfo->mouse_face_end_row
9741 && (*vpos > dpyinfo->mouse_face_beg_row
9742 || *hpos >= dpyinfo->mouse_face_beg_col)
9743 && (*vpos < dpyinfo->mouse_face_end_row
9744 || *hpos < dpyinfo->mouse_face_end_col
9745 || dpyinfo->mouse_face_past_end))
9746 return 0;
9747
9748 return 1;
9749 }
9750
9751
9752 /* EXPORT:
9753 Handle mouse button event on the tool-bar of frame F, at
9754 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9755 0 for button release. MODIFIERS is event modifiers for button
9756 release. */
9757
9758 void
9759 handle_tool_bar_click (f, x, y, down_p, modifiers)
9760 struct frame *f;
9761 int x, y, down_p;
9762 unsigned int modifiers;
9763 {
9764 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9765 struct window *w = XWINDOW (f->tool_bar_window);
9766 int hpos, vpos, prop_idx;
9767 struct glyph *glyph;
9768 Lisp_Object enabled_p;
9769
9770 /* If not on the highlighted tool-bar item, return. */
9771 frame_to_window_pixel_xy (w, &x, &y);
9772 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9773 return;
9774
9775 /* If item is disabled, do nothing. */
9776 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9777 if (NILP (enabled_p))
9778 return;
9779
9780 if (down_p)
9781 {
9782 /* Show item in pressed state. */
9783 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9784 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9785 last_tool_bar_item = prop_idx;
9786 }
9787 else
9788 {
9789 Lisp_Object key, frame;
9790 struct input_event event;
9791 EVENT_INIT (event);
9792
9793 /* Show item in released state. */
9794 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9795 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9796
9797 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9798
9799 XSETFRAME (frame, f);
9800 event.kind = TOOL_BAR_EVENT;
9801 event.frame_or_window = frame;
9802 event.arg = frame;
9803 kbd_buffer_store_event (&event);
9804
9805 event.kind = TOOL_BAR_EVENT;
9806 event.frame_or_window = frame;
9807 event.arg = key;
9808 event.modifiers = modifiers;
9809 kbd_buffer_store_event (&event);
9810 last_tool_bar_item = -1;
9811 }
9812 }
9813
9814
9815 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9816 tool-bar window-relative coordinates X/Y. Called from
9817 note_mouse_highlight. */
9818
9819 static void
9820 note_tool_bar_highlight (f, x, y)
9821 struct frame *f;
9822 int x, y;
9823 {
9824 Lisp_Object window = f->tool_bar_window;
9825 struct window *w = XWINDOW (window);
9826 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9827 int hpos, vpos;
9828 struct glyph *glyph;
9829 struct glyph_row *row;
9830 int i;
9831 Lisp_Object enabled_p;
9832 int prop_idx;
9833 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9834 int mouse_down_p, rc;
9835
9836 /* Function note_mouse_highlight is called with negative x(y
9837 values when mouse moves outside of the frame. */
9838 if (x <= 0 || y <= 0)
9839 {
9840 clear_mouse_face (dpyinfo);
9841 return;
9842 }
9843
9844 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9845 if (rc < 0)
9846 {
9847 /* Not on tool-bar item. */
9848 clear_mouse_face (dpyinfo);
9849 return;
9850 }
9851 else if (rc == 0)
9852 /* On same tool-bar item as before. */
9853 goto set_help_echo;
9854
9855 clear_mouse_face (dpyinfo);
9856
9857 /* Mouse is down, but on different tool-bar item? */
9858 mouse_down_p = (dpyinfo->grabbed
9859 && f == last_mouse_frame
9860 && FRAME_LIVE_P (f));
9861 if (mouse_down_p
9862 && last_tool_bar_item != prop_idx)
9863 return;
9864
9865 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9866 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9867
9868 /* If tool-bar item is not enabled, don't highlight it. */
9869 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9870 if (!NILP (enabled_p))
9871 {
9872 /* Compute the x-position of the glyph. In front and past the
9873 image is a space. We include this in the highlighted area. */
9874 row = MATRIX_ROW (w->current_matrix, vpos);
9875 for (i = x = 0; i < hpos; ++i)
9876 x += row->glyphs[TEXT_AREA][i].pixel_width;
9877
9878 /* Record this as the current active region. */
9879 dpyinfo->mouse_face_beg_col = hpos;
9880 dpyinfo->mouse_face_beg_row = vpos;
9881 dpyinfo->mouse_face_beg_x = x;
9882 dpyinfo->mouse_face_beg_y = row->y;
9883 dpyinfo->mouse_face_past_end = 0;
9884
9885 dpyinfo->mouse_face_end_col = hpos + 1;
9886 dpyinfo->mouse_face_end_row = vpos;
9887 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9888 dpyinfo->mouse_face_end_y = row->y;
9889 dpyinfo->mouse_face_window = window;
9890 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9891
9892 /* Display it as active. */
9893 show_mouse_face (dpyinfo, draw);
9894 dpyinfo->mouse_face_image_state = draw;
9895 }
9896
9897 set_help_echo:
9898
9899 /* Set help_echo_string to a help string to display for this tool-bar item.
9900 XTread_socket does the rest. */
9901 help_echo_object = help_echo_window = Qnil;
9902 help_echo_pos = -1;
9903 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9904 if (NILP (help_echo_string))
9905 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9906 }
9907
9908 #endif /* HAVE_WINDOW_SYSTEM */
9909
9910
9911 \f
9912 /************************************************************************
9913 Horizontal scrolling
9914 ************************************************************************/
9915
9916 static int hscroll_window_tree P_ ((Lisp_Object));
9917 static int hscroll_windows P_ ((Lisp_Object));
9918
9919 /* For all leaf windows in the window tree rooted at WINDOW, set their
9920 hscroll value so that PT is (i) visible in the window, and (ii) so
9921 that it is not within a certain margin at the window's left and
9922 right border. Value is non-zero if any window's hscroll has been
9923 changed. */
9924
9925 static int
9926 hscroll_window_tree (window)
9927 Lisp_Object window;
9928 {
9929 int hscrolled_p = 0;
9930 int hscroll_relative_p = FLOATP (Vhscroll_step);
9931 int hscroll_step_abs = 0;
9932 double hscroll_step_rel = 0;
9933
9934 if (hscroll_relative_p)
9935 {
9936 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9937 if (hscroll_step_rel < 0)
9938 {
9939 hscroll_relative_p = 0;
9940 hscroll_step_abs = 0;
9941 }
9942 }
9943 else if (INTEGERP (Vhscroll_step))
9944 {
9945 hscroll_step_abs = XINT (Vhscroll_step);
9946 if (hscroll_step_abs < 0)
9947 hscroll_step_abs = 0;
9948 }
9949 else
9950 hscroll_step_abs = 0;
9951
9952 while (WINDOWP (window))
9953 {
9954 struct window *w = XWINDOW (window);
9955
9956 if (WINDOWP (w->hchild))
9957 hscrolled_p |= hscroll_window_tree (w->hchild);
9958 else if (WINDOWP (w->vchild))
9959 hscrolled_p |= hscroll_window_tree (w->vchild);
9960 else if (w->cursor.vpos >= 0)
9961 {
9962 int h_margin;
9963 int text_area_width;
9964 struct glyph_row *current_cursor_row
9965 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9966 struct glyph_row *desired_cursor_row
9967 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9968 struct glyph_row *cursor_row
9969 = (desired_cursor_row->enabled_p
9970 ? desired_cursor_row
9971 : current_cursor_row);
9972
9973 text_area_width = window_box_width (w, TEXT_AREA);
9974
9975 /* Scroll when cursor is inside this scroll margin. */
9976 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9977
9978 if ((XFASTINT (w->hscroll)
9979 && w->cursor.x <= h_margin)
9980 || (cursor_row->enabled_p
9981 && cursor_row->truncated_on_right_p
9982 && (w->cursor.x >= text_area_width - h_margin)))
9983 {
9984 struct it it;
9985 int hscroll;
9986 struct buffer *saved_current_buffer;
9987 int pt;
9988 int wanted_x;
9989
9990 /* Find point in a display of infinite width. */
9991 saved_current_buffer = current_buffer;
9992 current_buffer = XBUFFER (w->buffer);
9993
9994 if (w == XWINDOW (selected_window))
9995 pt = BUF_PT (current_buffer);
9996 else
9997 {
9998 pt = marker_position (w->pointm);
9999 pt = max (BEGV, pt);
10000 pt = min (ZV, pt);
10001 }
10002
10003 /* Move iterator to pt starting at cursor_row->start in
10004 a line with infinite width. */
10005 init_to_row_start (&it, w, cursor_row);
10006 it.last_visible_x = INFINITY;
10007 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10008 current_buffer = saved_current_buffer;
10009
10010 /* Position cursor in window. */
10011 if (!hscroll_relative_p && hscroll_step_abs == 0)
10012 hscroll = max (0, (it.current_x
10013 - (ITERATOR_AT_END_OF_LINE_P (&it)
10014 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10015 : (text_area_width / 2))))
10016 / FRAME_COLUMN_WIDTH (it.f);
10017 else if (w->cursor.x >= text_area_width - h_margin)
10018 {
10019 if (hscroll_relative_p)
10020 wanted_x = text_area_width * (1 - hscroll_step_rel)
10021 - h_margin;
10022 else
10023 wanted_x = text_area_width
10024 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10025 - h_margin;
10026 hscroll
10027 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10028 }
10029 else
10030 {
10031 if (hscroll_relative_p)
10032 wanted_x = text_area_width * hscroll_step_rel
10033 + h_margin;
10034 else
10035 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10036 + h_margin;
10037 hscroll
10038 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10039 }
10040 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10041
10042 /* Don't call Fset_window_hscroll if value hasn't
10043 changed because it will prevent redisplay
10044 optimizations. */
10045 if (XFASTINT (w->hscroll) != hscroll)
10046 {
10047 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10048 w->hscroll = make_number (hscroll);
10049 hscrolled_p = 1;
10050 }
10051 }
10052 }
10053
10054 window = w->next;
10055 }
10056
10057 /* Value is non-zero if hscroll of any leaf window has been changed. */
10058 return hscrolled_p;
10059 }
10060
10061
10062 /* Set hscroll so that cursor is visible and not inside horizontal
10063 scroll margins for all windows in the tree rooted at WINDOW. See
10064 also hscroll_window_tree above. Value is non-zero if any window's
10065 hscroll has been changed. If it has, desired matrices on the frame
10066 of WINDOW are cleared. */
10067
10068 static int
10069 hscroll_windows (window)
10070 Lisp_Object window;
10071 {
10072 int hscrolled_p;
10073
10074 if (automatic_hscrolling_p)
10075 {
10076 hscrolled_p = hscroll_window_tree (window);
10077 if (hscrolled_p)
10078 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10079 }
10080 else
10081 hscrolled_p = 0;
10082 return hscrolled_p;
10083 }
10084
10085
10086 \f
10087 /************************************************************************
10088 Redisplay
10089 ************************************************************************/
10090
10091 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10092 to a non-zero value. This is sometimes handy to have in a debugger
10093 session. */
10094
10095 #if GLYPH_DEBUG
10096
10097 /* First and last unchanged row for try_window_id. */
10098
10099 int debug_first_unchanged_at_end_vpos;
10100 int debug_last_unchanged_at_beg_vpos;
10101
10102 /* Delta vpos and y. */
10103
10104 int debug_dvpos, debug_dy;
10105
10106 /* Delta in characters and bytes for try_window_id. */
10107
10108 int debug_delta, debug_delta_bytes;
10109
10110 /* Values of window_end_pos and window_end_vpos at the end of
10111 try_window_id. */
10112
10113 EMACS_INT debug_end_pos, debug_end_vpos;
10114
10115 /* Append a string to W->desired_matrix->method. FMT is a printf
10116 format string. A1...A9 are a supplement for a variable-length
10117 argument list. If trace_redisplay_p is non-zero also printf the
10118 resulting string to stderr. */
10119
10120 static void
10121 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10122 struct window *w;
10123 char *fmt;
10124 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10125 {
10126 char buffer[512];
10127 char *method = w->desired_matrix->method;
10128 int len = strlen (method);
10129 int size = sizeof w->desired_matrix->method;
10130 int remaining = size - len - 1;
10131
10132 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10133 if (len && remaining)
10134 {
10135 method[len] = '|';
10136 --remaining, ++len;
10137 }
10138
10139 strncpy (method + len, buffer, remaining);
10140
10141 if (trace_redisplay_p)
10142 fprintf (stderr, "%p (%s): %s\n",
10143 w,
10144 ((BUFFERP (w->buffer)
10145 && STRINGP (XBUFFER (w->buffer)->name))
10146 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10147 : "no buffer"),
10148 buffer);
10149 }
10150
10151 #endif /* GLYPH_DEBUG */
10152
10153
10154 /* Value is non-zero if all changes in window W, which displays
10155 current_buffer, are in the text between START and END. START is a
10156 buffer position, END is given as a distance from Z. Used in
10157 redisplay_internal for display optimization. */
10158
10159 static INLINE int
10160 text_outside_line_unchanged_p (w, start, end)
10161 struct window *w;
10162 int start, end;
10163 {
10164 int unchanged_p = 1;
10165
10166 /* If text or overlays have changed, see where. */
10167 if (XFASTINT (w->last_modified) < MODIFF
10168 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10169 {
10170 /* Gap in the line? */
10171 if (GPT < start || Z - GPT < end)
10172 unchanged_p = 0;
10173
10174 /* Changes start in front of the line, or end after it? */
10175 if (unchanged_p
10176 && (BEG_UNCHANGED < start - 1
10177 || END_UNCHANGED < end))
10178 unchanged_p = 0;
10179
10180 /* If selective display, can't optimize if changes start at the
10181 beginning of the line. */
10182 if (unchanged_p
10183 && INTEGERP (current_buffer->selective_display)
10184 && XINT (current_buffer->selective_display) > 0
10185 && (BEG_UNCHANGED < start || GPT <= start))
10186 unchanged_p = 0;
10187
10188 /* If there are overlays at the start or end of the line, these
10189 may have overlay strings with newlines in them. A change at
10190 START, for instance, may actually concern the display of such
10191 overlay strings as well, and they are displayed on different
10192 lines. So, quickly rule out this case. (For the future, it
10193 might be desirable to implement something more telling than
10194 just BEG/END_UNCHANGED.) */
10195 if (unchanged_p)
10196 {
10197 if (BEG + BEG_UNCHANGED == start
10198 && overlay_touches_p (start))
10199 unchanged_p = 0;
10200 if (END_UNCHANGED == end
10201 && overlay_touches_p (Z - end))
10202 unchanged_p = 0;
10203 }
10204 }
10205
10206 return unchanged_p;
10207 }
10208
10209
10210 /* Do a frame update, taking possible shortcuts into account. This is
10211 the main external entry point for redisplay.
10212
10213 If the last redisplay displayed an echo area message and that message
10214 is no longer requested, we clear the echo area or bring back the
10215 mini-buffer if that is in use. */
10216
10217 void
10218 redisplay ()
10219 {
10220 redisplay_internal (0);
10221 }
10222
10223
10224 static Lisp_Object
10225 overlay_arrow_string_or_property (var)
10226 Lisp_Object var;
10227 {
10228 Lisp_Object val;
10229
10230 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10231 return val;
10232
10233 return Voverlay_arrow_string;
10234 }
10235
10236 /* Return 1 if there are any overlay-arrows in current_buffer. */
10237 static int
10238 overlay_arrow_in_current_buffer_p ()
10239 {
10240 Lisp_Object vlist;
10241
10242 for (vlist = Voverlay_arrow_variable_list;
10243 CONSP (vlist);
10244 vlist = XCDR (vlist))
10245 {
10246 Lisp_Object var = XCAR (vlist);
10247 Lisp_Object val;
10248
10249 if (!SYMBOLP (var))
10250 continue;
10251 val = find_symbol_value (var);
10252 if (MARKERP (val)
10253 && current_buffer == XMARKER (val)->buffer)
10254 return 1;
10255 }
10256 return 0;
10257 }
10258
10259
10260 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10261 has changed. */
10262
10263 static int
10264 overlay_arrows_changed_p ()
10265 {
10266 Lisp_Object vlist;
10267
10268 for (vlist = Voverlay_arrow_variable_list;
10269 CONSP (vlist);
10270 vlist = XCDR (vlist))
10271 {
10272 Lisp_Object var = XCAR (vlist);
10273 Lisp_Object val, pstr;
10274
10275 if (!SYMBOLP (var))
10276 continue;
10277 val = find_symbol_value (var);
10278 if (!MARKERP (val))
10279 continue;
10280 if (! EQ (COERCE_MARKER (val),
10281 Fget (var, Qlast_arrow_position))
10282 || ! (pstr = overlay_arrow_string_or_property (var),
10283 EQ (pstr, Fget (var, Qlast_arrow_string))))
10284 return 1;
10285 }
10286 return 0;
10287 }
10288
10289 /* Mark overlay arrows to be updated on next redisplay. */
10290
10291 static void
10292 update_overlay_arrows (up_to_date)
10293 int up_to_date;
10294 {
10295 Lisp_Object vlist;
10296
10297 for (vlist = Voverlay_arrow_variable_list;
10298 CONSP (vlist);
10299 vlist = XCDR (vlist))
10300 {
10301 Lisp_Object var = XCAR (vlist);
10302
10303 if (!SYMBOLP (var))
10304 continue;
10305
10306 if (up_to_date > 0)
10307 {
10308 Lisp_Object val = find_symbol_value (var);
10309 Fput (var, Qlast_arrow_position,
10310 COERCE_MARKER (val));
10311 Fput (var, Qlast_arrow_string,
10312 overlay_arrow_string_or_property (var));
10313 }
10314 else if (up_to_date < 0
10315 || !NILP (Fget (var, Qlast_arrow_position)))
10316 {
10317 Fput (var, Qlast_arrow_position, Qt);
10318 Fput (var, Qlast_arrow_string, Qt);
10319 }
10320 }
10321 }
10322
10323
10324 /* Return overlay arrow string to display at row.
10325 Return integer (bitmap number) for arrow bitmap in left fringe.
10326 Return nil if no overlay arrow. */
10327
10328 static Lisp_Object
10329 overlay_arrow_at_row (it, row)
10330 struct it *it;
10331 struct glyph_row *row;
10332 {
10333 Lisp_Object vlist;
10334
10335 for (vlist = Voverlay_arrow_variable_list;
10336 CONSP (vlist);
10337 vlist = XCDR (vlist))
10338 {
10339 Lisp_Object var = XCAR (vlist);
10340 Lisp_Object val;
10341
10342 if (!SYMBOLP (var))
10343 continue;
10344
10345 val = find_symbol_value (var);
10346
10347 if (MARKERP (val)
10348 && current_buffer == XMARKER (val)->buffer
10349 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10350 {
10351 if (FRAME_WINDOW_P (it->f)
10352 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10353 {
10354 #ifdef HAVE_WINDOW_SYSTEM
10355 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10356 {
10357 int fringe_bitmap;
10358 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10359 return make_number (fringe_bitmap);
10360 }
10361 #endif
10362 return make_number (-1); /* Use default arrow bitmap */
10363 }
10364 return overlay_arrow_string_or_property (var);
10365 }
10366 }
10367
10368 return Qnil;
10369 }
10370
10371 /* Return 1 if point moved out of or into a composition. Otherwise
10372 return 0. PREV_BUF and PREV_PT are the last point buffer and
10373 position. BUF and PT are the current point buffer and position. */
10374
10375 int
10376 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10377 struct buffer *prev_buf, *buf;
10378 int prev_pt, pt;
10379 {
10380 int start, end;
10381 Lisp_Object prop;
10382 Lisp_Object buffer;
10383
10384 XSETBUFFER (buffer, buf);
10385 /* Check a composition at the last point if point moved within the
10386 same buffer. */
10387 if (prev_buf == buf)
10388 {
10389 if (prev_pt == pt)
10390 /* Point didn't move. */
10391 return 0;
10392
10393 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10394 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10395 && COMPOSITION_VALID_P (start, end, prop)
10396 && start < prev_pt && end > prev_pt)
10397 /* The last point was within the composition. Return 1 iff
10398 point moved out of the composition. */
10399 return (pt <= start || pt >= end);
10400 }
10401
10402 /* Check a composition at the current point. */
10403 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10404 && find_composition (pt, -1, &start, &end, &prop, buffer)
10405 && COMPOSITION_VALID_P (start, end, prop)
10406 && start < pt && end > pt);
10407 }
10408
10409
10410 /* Reconsider the setting of B->clip_changed which is displayed
10411 in window W. */
10412
10413 static INLINE void
10414 reconsider_clip_changes (w, b)
10415 struct window *w;
10416 struct buffer *b;
10417 {
10418 if (b->clip_changed
10419 && !NILP (w->window_end_valid)
10420 && w->current_matrix->buffer == b
10421 && w->current_matrix->zv == BUF_ZV (b)
10422 && w->current_matrix->begv == BUF_BEGV (b))
10423 b->clip_changed = 0;
10424
10425 /* If display wasn't paused, and W is not a tool bar window, see if
10426 point has been moved into or out of a composition. In that case,
10427 we set b->clip_changed to 1 to force updating the screen. If
10428 b->clip_changed has already been set to 1, we can skip this
10429 check. */
10430 if (!b->clip_changed
10431 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10432 {
10433 int pt;
10434
10435 if (w == XWINDOW (selected_window))
10436 pt = BUF_PT (current_buffer);
10437 else
10438 pt = marker_position (w->pointm);
10439
10440 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10441 || pt != XINT (w->last_point))
10442 && check_point_in_composition (w->current_matrix->buffer,
10443 XINT (w->last_point),
10444 XBUFFER (w->buffer), pt))
10445 b->clip_changed = 1;
10446 }
10447 }
10448 \f
10449
10450 /* Select FRAME to forward the values of frame-local variables into C
10451 variables so that the redisplay routines can access those values
10452 directly. */
10453
10454 static void
10455 select_frame_for_redisplay (frame)
10456 Lisp_Object frame;
10457 {
10458 Lisp_Object tail, sym, val;
10459 Lisp_Object old = selected_frame;
10460
10461 selected_frame = frame;
10462
10463 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10464 if (CONSP (XCAR (tail))
10465 && (sym = XCAR (XCAR (tail)),
10466 SYMBOLP (sym))
10467 && (sym = indirect_variable (sym),
10468 val = SYMBOL_VALUE (sym),
10469 (BUFFER_LOCAL_VALUEP (val)
10470 || SOME_BUFFER_LOCAL_VALUEP (val)))
10471 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10472 /* Use find_symbol_value rather than Fsymbol_value
10473 to avoid an error if it is void. */
10474 find_symbol_value (sym);
10475
10476 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10477 if (CONSP (XCAR (tail))
10478 && (sym = XCAR (XCAR (tail)),
10479 SYMBOLP (sym))
10480 && (sym = indirect_variable (sym),
10481 val = SYMBOL_VALUE (sym),
10482 (BUFFER_LOCAL_VALUEP (val)
10483 || SOME_BUFFER_LOCAL_VALUEP (val)))
10484 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10485 find_symbol_value (sym);
10486 }
10487
10488
10489 #define STOP_POLLING \
10490 do { if (! polling_stopped_here) stop_polling (); \
10491 polling_stopped_here = 1; } while (0)
10492
10493 #define RESUME_POLLING \
10494 do { if (polling_stopped_here) start_polling (); \
10495 polling_stopped_here = 0; } while (0)
10496
10497
10498 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10499 response to any user action; therefore, we should preserve the echo
10500 area. (Actually, our caller does that job.) Perhaps in the future
10501 avoid recentering windows if it is not necessary; currently that
10502 causes some problems. */
10503
10504 static void
10505 redisplay_internal (preserve_echo_area)
10506 int preserve_echo_area;
10507 {
10508 struct window *w = XWINDOW (selected_window);
10509 struct frame *f = XFRAME (w->frame);
10510 int pause;
10511 int must_finish = 0;
10512 struct text_pos tlbufpos, tlendpos;
10513 int number_of_visible_frames;
10514 int count;
10515 struct frame *sf = SELECTED_FRAME ();
10516 int polling_stopped_here = 0;
10517
10518 /* Non-zero means redisplay has to consider all windows on all
10519 frames. Zero means, only selected_window is considered. */
10520 int consider_all_windows_p;
10521
10522 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10523
10524 /* No redisplay if running in batch mode or frame is not yet fully
10525 initialized, or redisplay is explicitly turned off by setting
10526 Vinhibit_redisplay. */
10527 if (noninteractive
10528 || !NILP (Vinhibit_redisplay)
10529 || !f->glyphs_initialized_p)
10530 return;
10531
10532 /* The flag redisplay_performed_directly_p is set by
10533 direct_output_for_insert when it already did the whole screen
10534 update necessary. */
10535 if (redisplay_performed_directly_p)
10536 {
10537 redisplay_performed_directly_p = 0;
10538 if (!hscroll_windows (selected_window))
10539 return;
10540 }
10541
10542 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10543 if (popup_activated ())
10544 return;
10545 #endif
10546
10547 /* I don't think this happens but let's be paranoid. */
10548 if (redisplaying_p)
10549 return;
10550
10551 /* Record a function that resets redisplaying_p to its old value
10552 when we leave this function. */
10553 count = SPECPDL_INDEX ();
10554 record_unwind_protect (unwind_redisplay,
10555 Fcons (make_number (redisplaying_p), selected_frame));
10556 ++redisplaying_p;
10557 specbind (Qinhibit_free_realized_faces, Qnil);
10558
10559 {
10560 Lisp_Object tail, frame;
10561
10562 FOR_EACH_FRAME (tail, frame)
10563 {
10564 struct frame *f = XFRAME (frame);
10565 f->already_hscrolled_p = 0;
10566 }
10567 }
10568
10569 retry:
10570 pause = 0;
10571 reconsider_clip_changes (w, current_buffer);
10572 last_escape_glyph_frame = NULL;
10573 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10574
10575 /* If new fonts have been loaded that make a glyph matrix adjustment
10576 necessary, do it. */
10577 if (fonts_changed_p)
10578 {
10579 adjust_glyphs (NULL);
10580 ++windows_or_buffers_changed;
10581 fonts_changed_p = 0;
10582 }
10583
10584 /* If face_change_count is non-zero, init_iterator will free all
10585 realized faces, which includes the faces referenced from current
10586 matrices. So, we can't reuse current matrices in this case. */
10587 if (face_change_count)
10588 ++windows_or_buffers_changed;
10589
10590 if (! FRAME_WINDOW_P (sf)
10591 && previous_terminal_frame != sf)
10592 {
10593 /* Since frames on an ASCII terminal share the same display
10594 area, displaying a different frame means redisplay the whole
10595 thing. */
10596 windows_or_buffers_changed++;
10597 SET_FRAME_GARBAGED (sf);
10598 XSETFRAME (Vterminal_frame, sf);
10599 }
10600 previous_terminal_frame = sf;
10601
10602 /* Set the visible flags for all frames. Do this before checking
10603 for resized or garbaged frames; they want to know if their frames
10604 are visible. See the comment in frame.h for
10605 FRAME_SAMPLE_VISIBILITY. */
10606 {
10607 Lisp_Object tail, frame;
10608
10609 number_of_visible_frames = 0;
10610
10611 FOR_EACH_FRAME (tail, frame)
10612 {
10613 struct frame *f = XFRAME (frame);
10614
10615 FRAME_SAMPLE_VISIBILITY (f);
10616 if (FRAME_VISIBLE_P (f))
10617 ++number_of_visible_frames;
10618 clear_desired_matrices (f);
10619 }
10620 }
10621
10622 /* Notice any pending interrupt request to change frame size. */
10623 do_pending_window_change (1);
10624
10625 /* Clear frames marked as garbaged. */
10626 if (frame_garbaged)
10627 clear_garbaged_frames ();
10628
10629 /* Build menubar and tool-bar items. */
10630 if (NILP (Vmemory_full))
10631 prepare_menu_bars ();
10632
10633 if (windows_or_buffers_changed)
10634 update_mode_lines++;
10635
10636 /* Detect case that we need to write or remove a star in the mode line. */
10637 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10638 {
10639 w->update_mode_line = Qt;
10640 if (buffer_shared > 1)
10641 update_mode_lines++;
10642 }
10643
10644 /* If %c is in the mode line, update it if needed. */
10645 if (!NILP (w->column_number_displayed)
10646 /* This alternative quickly identifies a common case
10647 where no change is needed. */
10648 && !(PT == XFASTINT (w->last_point)
10649 && XFASTINT (w->last_modified) >= MODIFF
10650 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10651 && (XFASTINT (w->column_number_displayed)
10652 != (int) current_column ())) /* iftc */
10653 w->update_mode_line = Qt;
10654
10655 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10656
10657 /* The variable buffer_shared is set in redisplay_window and
10658 indicates that we redisplay a buffer in different windows. See
10659 there. */
10660 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10661 || cursor_type_changed);
10662
10663 /* If specs for an arrow have changed, do thorough redisplay
10664 to ensure we remove any arrow that should no longer exist. */
10665 if (overlay_arrows_changed_p ())
10666 consider_all_windows_p = windows_or_buffers_changed = 1;
10667
10668 /* Normally the message* functions will have already displayed and
10669 updated the echo area, but the frame may have been trashed, or
10670 the update may have been preempted, so display the echo area
10671 again here. Checking message_cleared_p captures the case that
10672 the echo area should be cleared. */
10673 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10674 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10675 || (message_cleared_p
10676 && minibuf_level == 0
10677 /* If the mini-window is currently selected, this means the
10678 echo-area doesn't show through. */
10679 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10680 {
10681 int window_height_changed_p = echo_area_display (0);
10682 must_finish = 1;
10683
10684 /* If we don't display the current message, don't clear the
10685 message_cleared_p flag, because, if we did, we wouldn't clear
10686 the echo area in the next redisplay which doesn't preserve
10687 the echo area. */
10688 if (!display_last_displayed_message_p)
10689 message_cleared_p = 0;
10690
10691 if (fonts_changed_p)
10692 goto retry;
10693 else if (window_height_changed_p)
10694 {
10695 consider_all_windows_p = 1;
10696 ++update_mode_lines;
10697 ++windows_or_buffers_changed;
10698
10699 /* If window configuration was changed, frames may have been
10700 marked garbaged. Clear them or we will experience
10701 surprises wrt scrolling. */
10702 if (frame_garbaged)
10703 clear_garbaged_frames ();
10704 }
10705 }
10706 else if (EQ (selected_window, minibuf_window)
10707 && (current_buffer->clip_changed
10708 || XFASTINT (w->last_modified) < MODIFF
10709 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10710 && resize_mini_window (w, 0))
10711 {
10712 /* Resized active mini-window to fit the size of what it is
10713 showing if its contents might have changed. */
10714 must_finish = 1;
10715 consider_all_windows_p = 1;
10716 ++windows_or_buffers_changed;
10717 ++update_mode_lines;
10718
10719 /* If window configuration was changed, frames may have been
10720 marked garbaged. Clear them or we will experience
10721 surprises wrt scrolling. */
10722 if (frame_garbaged)
10723 clear_garbaged_frames ();
10724 }
10725
10726
10727 /* If showing the region, and mark has changed, we must redisplay
10728 the whole window. The assignment to this_line_start_pos prevents
10729 the optimization directly below this if-statement. */
10730 if (((!NILP (Vtransient_mark_mode)
10731 && !NILP (XBUFFER (w->buffer)->mark_active))
10732 != !NILP (w->region_showing))
10733 || (!NILP (w->region_showing)
10734 && !EQ (w->region_showing,
10735 Fmarker_position (XBUFFER (w->buffer)->mark))))
10736 CHARPOS (this_line_start_pos) = 0;
10737
10738 /* Optimize the case that only the line containing the cursor in the
10739 selected window has changed. Variables starting with this_ are
10740 set in display_line and record information about the line
10741 containing the cursor. */
10742 tlbufpos = this_line_start_pos;
10743 tlendpos = this_line_end_pos;
10744 if (!consider_all_windows_p
10745 && CHARPOS (tlbufpos) > 0
10746 && NILP (w->update_mode_line)
10747 && !current_buffer->clip_changed
10748 && !current_buffer->prevent_redisplay_optimizations_p
10749 && FRAME_VISIBLE_P (XFRAME (w->frame))
10750 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10751 /* Make sure recorded data applies to current buffer, etc. */
10752 && this_line_buffer == current_buffer
10753 && current_buffer == XBUFFER (w->buffer)
10754 && NILP (w->force_start)
10755 && NILP (w->optional_new_start)
10756 /* Point must be on the line that we have info recorded about. */
10757 && PT >= CHARPOS (tlbufpos)
10758 && PT <= Z - CHARPOS (tlendpos)
10759 /* All text outside that line, including its final newline,
10760 must be unchanged */
10761 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10762 CHARPOS (tlendpos)))
10763 {
10764 if (CHARPOS (tlbufpos) > BEGV
10765 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10766 && (CHARPOS (tlbufpos) == ZV
10767 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10768 /* Former continuation line has disappeared by becoming empty */
10769 goto cancel;
10770 else if (XFASTINT (w->last_modified) < MODIFF
10771 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10772 || MINI_WINDOW_P (w))
10773 {
10774 /* We have to handle the case of continuation around a
10775 wide-column character (See the comment in indent.c around
10776 line 885).
10777
10778 For instance, in the following case:
10779
10780 -------- Insert --------
10781 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10782 J_I_ ==> J_I_ `^^' are cursors.
10783 ^^ ^^
10784 -------- --------
10785
10786 As we have to redraw the line above, we should goto cancel. */
10787
10788 struct it it;
10789 int line_height_before = this_line_pixel_height;
10790
10791 /* Note that start_display will handle the case that the
10792 line starting at tlbufpos is a continuation lines. */
10793 start_display (&it, w, tlbufpos);
10794
10795 /* Implementation note: It this still necessary? */
10796 if (it.current_x != this_line_start_x)
10797 goto cancel;
10798
10799 TRACE ((stderr, "trying display optimization 1\n"));
10800 w->cursor.vpos = -1;
10801 overlay_arrow_seen = 0;
10802 it.vpos = this_line_vpos;
10803 it.current_y = this_line_y;
10804 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10805 display_line (&it);
10806
10807 /* If line contains point, is not continued,
10808 and ends at same distance from eob as before, we win */
10809 if (w->cursor.vpos >= 0
10810 /* Line is not continued, otherwise this_line_start_pos
10811 would have been set to 0 in display_line. */
10812 && CHARPOS (this_line_start_pos)
10813 /* Line ends as before. */
10814 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10815 /* Line has same height as before. Otherwise other lines
10816 would have to be shifted up or down. */
10817 && this_line_pixel_height == line_height_before)
10818 {
10819 /* If this is not the window's last line, we must adjust
10820 the charstarts of the lines below. */
10821 if (it.current_y < it.last_visible_y)
10822 {
10823 struct glyph_row *row
10824 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10825 int delta, delta_bytes;
10826
10827 if (Z - CHARPOS (tlendpos) == ZV)
10828 {
10829 /* This line ends at end of (accessible part of)
10830 buffer. There is no newline to count. */
10831 delta = (Z
10832 - CHARPOS (tlendpos)
10833 - MATRIX_ROW_START_CHARPOS (row));
10834 delta_bytes = (Z_BYTE
10835 - BYTEPOS (tlendpos)
10836 - MATRIX_ROW_START_BYTEPOS (row));
10837 }
10838 else
10839 {
10840 /* This line ends in a newline. Must take
10841 account of the newline and the rest of the
10842 text that follows. */
10843 delta = (Z
10844 - CHARPOS (tlendpos)
10845 - MATRIX_ROW_START_CHARPOS (row));
10846 delta_bytes = (Z_BYTE
10847 - BYTEPOS (tlendpos)
10848 - MATRIX_ROW_START_BYTEPOS (row));
10849 }
10850
10851 increment_matrix_positions (w->current_matrix,
10852 this_line_vpos + 1,
10853 w->current_matrix->nrows,
10854 delta, delta_bytes);
10855 }
10856
10857 /* If this row displays text now but previously didn't,
10858 or vice versa, w->window_end_vpos may have to be
10859 adjusted. */
10860 if ((it.glyph_row - 1)->displays_text_p)
10861 {
10862 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10863 XSETINT (w->window_end_vpos, this_line_vpos);
10864 }
10865 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10866 && this_line_vpos > 0)
10867 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10868 w->window_end_valid = Qnil;
10869
10870 /* Update hint: No need to try to scroll in update_window. */
10871 w->desired_matrix->no_scrolling_p = 1;
10872
10873 #if GLYPH_DEBUG
10874 *w->desired_matrix->method = 0;
10875 debug_method_add (w, "optimization 1");
10876 #endif
10877 #ifdef HAVE_WINDOW_SYSTEM
10878 update_window_fringes (w, 0);
10879 #endif
10880 goto update;
10881 }
10882 else
10883 goto cancel;
10884 }
10885 else if (/* Cursor position hasn't changed. */
10886 PT == XFASTINT (w->last_point)
10887 /* Make sure the cursor was last displayed
10888 in this window. Otherwise we have to reposition it. */
10889 && 0 <= w->cursor.vpos
10890 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10891 {
10892 if (!must_finish)
10893 {
10894 do_pending_window_change (1);
10895
10896 /* We used to always goto end_of_redisplay here, but this
10897 isn't enough if we have a blinking cursor. */
10898 if (w->cursor_off_p == w->last_cursor_off_p)
10899 goto end_of_redisplay;
10900 }
10901 goto update;
10902 }
10903 /* If highlighting the region, or if the cursor is in the echo area,
10904 then we can't just move the cursor. */
10905 else if (! (!NILP (Vtransient_mark_mode)
10906 && !NILP (current_buffer->mark_active))
10907 && (EQ (selected_window, current_buffer->last_selected_window)
10908 || highlight_nonselected_windows)
10909 && NILP (w->region_showing)
10910 && NILP (Vshow_trailing_whitespace)
10911 && !cursor_in_echo_area)
10912 {
10913 struct it it;
10914 struct glyph_row *row;
10915
10916 /* Skip from tlbufpos to PT and see where it is. Note that
10917 PT may be in invisible text. If so, we will end at the
10918 next visible position. */
10919 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10920 NULL, DEFAULT_FACE_ID);
10921 it.current_x = this_line_start_x;
10922 it.current_y = this_line_y;
10923 it.vpos = this_line_vpos;
10924
10925 /* The call to move_it_to stops in front of PT, but
10926 moves over before-strings. */
10927 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10928
10929 if (it.vpos == this_line_vpos
10930 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10931 row->enabled_p))
10932 {
10933 xassert (this_line_vpos == it.vpos);
10934 xassert (this_line_y == it.current_y);
10935 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10936 #if GLYPH_DEBUG
10937 *w->desired_matrix->method = 0;
10938 debug_method_add (w, "optimization 3");
10939 #endif
10940 goto update;
10941 }
10942 else
10943 goto cancel;
10944 }
10945
10946 cancel:
10947 /* Text changed drastically or point moved off of line. */
10948 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10949 }
10950
10951 CHARPOS (this_line_start_pos) = 0;
10952 consider_all_windows_p |= buffer_shared > 1;
10953 ++clear_face_cache_count;
10954 #ifdef HAVE_WINDOW_SYSTEM
10955 ++clear_image_cache_count;
10956 #endif
10957
10958 /* Build desired matrices, and update the display. If
10959 consider_all_windows_p is non-zero, do it for all windows on all
10960 frames. Otherwise do it for selected_window, only. */
10961
10962 if (consider_all_windows_p)
10963 {
10964 Lisp_Object tail, frame;
10965
10966 FOR_EACH_FRAME (tail, frame)
10967 XFRAME (frame)->updated_p = 0;
10968
10969 /* Recompute # windows showing selected buffer. This will be
10970 incremented each time such a window is displayed. */
10971 buffer_shared = 0;
10972
10973 FOR_EACH_FRAME (tail, frame)
10974 {
10975 struct frame *f = XFRAME (frame);
10976
10977 if (FRAME_WINDOW_P (f) || f == sf)
10978 {
10979 if (! EQ (frame, selected_frame))
10980 /* Select the frame, for the sake of frame-local
10981 variables. */
10982 select_frame_for_redisplay (frame);
10983
10984 /* Mark all the scroll bars to be removed; we'll redeem
10985 the ones we want when we redisplay their windows. */
10986 if (condemn_scroll_bars_hook)
10987 condemn_scroll_bars_hook (f);
10988
10989 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10990 redisplay_windows (FRAME_ROOT_WINDOW (f));
10991
10992 /* Any scroll bars which redisplay_windows should have
10993 nuked should now go away. */
10994 if (judge_scroll_bars_hook)
10995 judge_scroll_bars_hook (f);
10996
10997 /* If fonts changed, display again. */
10998 /* ??? rms: I suspect it is a mistake to jump all the way
10999 back to retry here. It should just retry this frame. */
11000 if (fonts_changed_p)
11001 goto retry;
11002
11003 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11004 {
11005 /* See if we have to hscroll. */
11006 if (!f->already_hscrolled_p)
11007 {
11008 f->already_hscrolled_p = 1;
11009 if (hscroll_windows (f->root_window))
11010 goto retry;
11011 }
11012
11013 /* Prevent various kinds of signals during display
11014 update. stdio is not robust about handling
11015 signals, which can cause an apparent I/O
11016 error. */
11017 if (interrupt_input)
11018 unrequest_sigio ();
11019 STOP_POLLING;
11020
11021 /* Update the display. */
11022 set_window_update_flags (XWINDOW (f->root_window), 1);
11023 pause |= update_frame (f, 0, 0);
11024 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11025 if (pause)
11026 break;
11027 #endif
11028
11029 f->updated_p = 1;
11030 }
11031 }
11032 }
11033
11034 if (!pause)
11035 {
11036 /* Do the mark_window_display_accurate after all windows have
11037 been redisplayed because this call resets flags in buffers
11038 which are needed for proper redisplay. */
11039 FOR_EACH_FRAME (tail, frame)
11040 {
11041 struct frame *f = XFRAME (frame);
11042 if (f->updated_p)
11043 {
11044 mark_window_display_accurate (f->root_window, 1);
11045 if (frame_up_to_date_hook)
11046 frame_up_to_date_hook (f);
11047 }
11048 }
11049 }
11050 }
11051 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11052 {
11053 Lisp_Object mini_window;
11054 struct frame *mini_frame;
11055
11056 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11057 /* Use list_of_error, not Qerror, so that
11058 we catch only errors and don't run the debugger. */
11059 internal_condition_case_1 (redisplay_window_1, selected_window,
11060 list_of_error,
11061 redisplay_window_error);
11062
11063 /* Compare desired and current matrices, perform output. */
11064
11065 update:
11066 /* If fonts changed, display again. */
11067 if (fonts_changed_p)
11068 goto retry;
11069
11070 /* Prevent various kinds of signals during display update.
11071 stdio is not robust about handling signals,
11072 which can cause an apparent I/O error. */
11073 if (interrupt_input)
11074 unrequest_sigio ();
11075 STOP_POLLING;
11076
11077 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11078 {
11079 if (hscroll_windows (selected_window))
11080 goto retry;
11081
11082 XWINDOW (selected_window)->must_be_updated_p = 1;
11083 pause = update_frame (sf, 0, 0);
11084 }
11085
11086 /* We may have called echo_area_display at the top of this
11087 function. If the echo area is on another frame, that may
11088 have put text on a frame other than the selected one, so the
11089 above call to update_frame would not have caught it. Catch
11090 it here. */
11091 mini_window = FRAME_MINIBUF_WINDOW (sf);
11092 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11093
11094 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11095 {
11096 XWINDOW (mini_window)->must_be_updated_p = 1;
11097 pause |= update_frame (mini_frame, 0, 0);
11098 if (!pause && hscroll_windows (mini_window))
11099 goto retry;
11100 }
11101 }
11102
11103 /* If display was paused because of pending input, make sure we do a
11104 thorough update the next time. */
11105 if (pause)
11106 {
11107 /* Prevent the optimization at the beginning of
11108 redisplay_internal that tries a single-line update of the
11109 line containing the cursor in the selected window. */
11110 CHARPOS (this_line_start_pos) = 0;
11111
11112 /* Let the overlay arrow be updated the next time. */
11113 update_overlay_arrows (0);
11114
11115 /* If we pause after scrolling, some rows in the current
11116 matrices of some windows are not valid. */
11117 if (!WINDOW_FULL_WIDTH_P (w)
11118 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11119 update_mode_lines = 1;
11120 }
11121 else
11122 {
11123 if (!consider_all_windows_p)
11124 {
11125 /* This has already been done above if
11126 consider_all_windows_p is set. */
11127 mark_window_display_accurate_1 (w, 1);
11128
11129 /* Say overlay arrows are up to date. */
11130 update_overlay_arrows (1);
11131
11132 if (frame_up_to_date_hook != 0)
11133 frame_up_to_date_hook (sf);
11134 }
11135
11136 update_mode_lines = 0;
11137 windows_or_buffers_changed = 0;
11138 cursor_type_changed = 0;
11139 }
11140
11141 /* Start SIGIO interrupts coming again. Having them off during the
11142 code above makes it less likely one will discard output, but not
11143 impossible, since there might be stuff in the system buffer here.
11144 But it is much hairier to try to do anything about that. */
11145 if (interrupt_input)
11146 request_sigio ();
11147 RESUME_POLLING;
11148
11149 /* If a frame has become visible which was not before, redisplay
11150 again, so that we display it. Expose events for such a frame
11151 (which it gets when becoming visible) don't call the parts of
11152 redisplay constructing glyphs, so simply exposing a frame won't
11153 display anything in this case. So, we have to display these
11154 frames here explicitly. */
11155 if (!pause)
11156 {
11157 Lisp_Object tail, frame;
11158 int new_count = 0;
11159
11160 FOR_EACH_FRAME (tail, frame)
11161 {
11162 int this_is_visible = 0;
11163
11164 if (XFRAME (frame)->visible)
11165 this_is_visible = 1;
11166 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11167 if (XFRAME (frame)->visible)
11168 this_is_visible = 1;
11169
11170 if (this_is_visible)
11171 new_count++;
11172 }
11173
11174 if (new_count != number_of_visible_frames)
11175 windows_or_buffers_changed++;
11176 }
11177
11178 /* Change frame size now if a change is pending. */
11179 do_pending_window_change (1);
11180
11181 /* If we just did a pending size change, or have additional
11182 visible frames, redisplay again. */
11183 if (windows_or_buffers_changed && !pause)
11184 goto retry;
11185
11186 /* Clear the face cache eventually. */
11187 if (consider_all_windows_p)
11188 {
11189 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11190 {
11191 clear_face_cache (0);
11192 clear_face_cache_count = 0;
11193 }
11194 #ifdef HAVE_WINDOW_SYSTEM
11195 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11196 {
11197 Lisp_Object tail, frame;
11198 FOR_EACH_FRAME (tail, frame)
11199 {
11200 struct frame *f = XFRAME (frame);
11201 if (FRAME_WINDOW_P (f))
11202 clear_image_cache (f, 0);
11203 }
11204 clear_image_cache_count = 0;
11205 }
11206 #endif /* HAVE_WINDOW_SYSTEM */
11207 }
11208
11209 end_of_redisplay:
11210 unbind_to (count, Qnil);
11211 RESUME_POLLING;
11212 }
11213
11214
11215 /* Redisplay, but leave alone any recent echo area message unless
11216 another message has been requested in its place.
11217
11218 This is useful in situations where you need to redisplay but no
11219 user action has occurred, making it inappropriate for the message
11220 area to be cleared. See tracking_off and
11221 wait_reading_process_output for examples of these situations.
11222
11223 FROM_WHERE is an integer saying from where this function was
11224 called. This is useful for debugging. */
11225
11226 void
11227 redisplay_preserve_echo_area (from_where)
11228 int from_where;
11229 {
11230 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11231
11232 if (!NILP (echo_area_buffer[1]))
11233 {
11234 /* We have a previously displayed message, but no current
11235 message. Redisplay the previous message. */
11236 display_last_displayed_message_p = 1;
11237 redisplay_internal (1);
11238 display_last_displayed_message_p = 0;
11239 }
11240 else
11241 redisplay_internal (1);
11242
11243 if (rif != NULL && rif->flush_display_optional)
11244 rif->flush_display_optional (NULL);
11245 }
11246
11247
11248 /* Function registered with record_unwind_protect in
11249 redisplay_internal. Reset redisplaying_p to the value it had
11250 before redisplay_internal was called, and clear
11251 prevent_freeing_realized_faces_p. It also selects the previously
11252 selected frame. */
11253
11254 static Lisp_Object
11255 unwind_redisplay (val)
11256 Lisp_Object val;
11257 {
11258 Lisp_Object old_redisplaying_p, old_frame;
11259
11260 old_redisplaying_p = XCAR (val);
11261 redisplaying_p = XFASTINT (old_redisplaying_p);
11262 old_frame = XCDR (val);
11263 if (! EQ (old_frame, selected_frame))
11264 select_frame_for_redisplay (old_frame);
11265 return Qnil;
11266 }
11267
11268
11269 /* Mark the display of window W as accurate or inaccurate. If
11270 ACCURATE_P is non-zero mark display of W as accurate. If
11271 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11272 redisplay_internal is called. */
11273
11274 static void
11275 mark_window_display_accurate_1 (w, accurate_p)
11276 struct window *w;
11277 int accurate_p;
11278 {
11279 if (BUFFERP (w->buffer))
11280 {
11281 struct buffer *b = XBUFFER (w->buffer);
11282
11283 w->last_modified
11284 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11285 w->last_overlay_modified
11286 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11287 w->last_had_star
11288 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11289
11290 if (accurate_p)
11291 {
11292 b->clip_changed = 0;
11293 b->prevent_redisplay_optimizations_p = 0;
11294
11295 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11296 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11297 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11298 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11299
11300 w->current_matrix->buffer = b;
11301 w->current_matrix->begv = BUF_BEGV (b);
11302 w->current_matrix->zv = BUF_ZV (b);
11303
11304 w->last_cursor = w->cursor;
11305 w->last_cursor_off_p = w->cursor_off_p;
11306
11307 if (w == XWINDOW (selected_window))
11308 w->last_point = make_number (BUF_PT (b));
11309 else
11310 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11311 }
11312 }
11313
11314 if (accurate_p)
11315 {
11316 w->window_end_valid = w->buffer;
11317 #if 0 /* This is incorrect with variable-height lines. */
11318 xassert (XINT (w->window_end_vpos)
11319 < (WINDOW_TOTAL_LINES (w)
11320 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11321 #endif
11322 w->update_mode_line = Qnil;
11323 }
11324 }
11325
11326
11327 /* Mark the display of windows in the window tree rooted at WINDOW as
11328 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11329 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11330 be redisplayed the next time redisplay_internal is called. */
11331
11332 void
11333 mark_window_display_accurate (window, accurate_p)
11334 Lisp_Object window;
11335 int accurate_p;
11336 {
11337 struct window *w;
11338
11339 for (; !NILP (window); window = w->next)
11340 {
11341 w = XWINDOW (window);
11342 mark_window_display_accurate_1 (w, accurate_p);
11343
11344 if (!NILP (w->vchild))
11345 mark_window_display_accurate (w->vchild, accurate_p);
11346 if (!NILP (w->hchild))
11347 mark_window_display_accurate (w->hchild, accurate_p);
11348 }
11349
11350 if (accurate_p)
11351 {
11352 update_overlay_arrows (1);
11353 }
11354 else
11355 {
11356 /* Force a thorough redisplay the next time by setting
11357 last_arrow_position and last_arrow_string to t, which is
11358 unequal to any useful value of Voverlay_arrow_... */
11359 update_overlay_arrows (-1);
11360 }
11361 }
11362
11363
11364 /* Return value in display table DP (Lisp_Char_Table *) for character
11365 C. Since a display table doesn't have any parent, we don't have to
11366 follow parent. Do not call this function directly but use the
11367 macro DISP_CHAR_VECTOR. */
11368
11369 Lisp_Object
11370 disp_char_vector (dp, c)
11371 struct Lisp_Char_Table *dp;
11372 int c;
11373 {
11374 int code[4], i;
11375 Lisp_Object val;
11376
11377 if (SINGLE_BYTE_CHAR_P (c))
11378 return (dp->contents[c]);
11379
11380 SPLIT_CHAR (c, code[0], code[1], code[2]);
11381 if (code[1] < 32)
11382 code[1] = -1;
11383 else if (code[2] < 32)
11384 code[2] = -1;
11385
11386 /* Here, the possible range of code[0] (== charset ID) is
11387 128..max_charset. Since the top level char table contains data
11388 for multibyte characters after 256th element, we must increment
11389 code[0] by 128 to get a correct index. */
11390 code[0] += 128;
11391 code[3] = -1; /* anchor */
11392
11393 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11394 {
11395 val = dp->contents[code[i]];
11396 if (!SUB_CHAR_TABLE_P (val))
11397 return (NILP (val) ? dp->defalt : val);
11398 }
11399
11400 /* Here, val is a sub char table. We return the default value of
11401 it. */
11402 return (dp->defalt);
11403 }
11404
11405
11406 \f
11407 /***********************************************************************
11408 Window Redisplay
11409 ***********************************************************************/
11410
11411 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11412
11413 static void
11414 redisplay_windows (window)
11415 Lisp_Object window;
11416 {
11417 while (!NILP (window))
11418 {
11419 struct window *w = XWINDOW (window);
11420
11421 if (!NILP (w->hchild))
11422 redisplay_windows (w->hchild);
11423 else if (!NILP (w->vchild))
11424 redisplay_windows (w->vchild);
11425 else
11426 {
11427 displayed_buffer = XBUFFER (w->buffer);
11428 /* Use list_of_error, not Qerror, so that
11429 we catch only errors and don't run the debugger. */
11430 internal_condition_case_1 (redisplay_window_0, window,
11431 list_of_error,
11432 redisplay_window_error);
11433 }
11434
11435 window = w->next;
11436 }
11437 }
11438
11439 static Lisp_Object
11440 redisplay_window_error ()
11441 {
11442 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11443 return Qnil;
11444 }
11445
11446 static Lisp_Object
11447 redisplay_window_0 (window)
11448 Lisp_Object window;
11449 {
11450 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11451 redisplay_window (window, 0);
11452 return Qnil;
11453 }
11454
11455 static Lisp_Object
11456 redisplay_window_1 (window)
11457 Lisp_Object window;
11458 {
11459 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11460 redisplay_window (window, 1);
11461 return Qnil;
11462 }
11463 \f
11464
11465 /* Increment GLYPH until it reaches END or CONDITION fails while
11466 adding (GLYPH)->pixel_width to X. */
11467
11468 #define SKIP_GLYPHS(glyph, end, x, condition) \
11469 do \
11470 { \
11471 (x) += (glyph)->pixel_width; \
11472 ++(glyph); \
11473 } \
11474 while ((glyph) < (end) && (condition))
11475
11476
11477 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11478 DELTA is the number of bytes by which positions recorded in ROW
11479 differ from current buffer positions. */
11480
11481 void
11482 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11483 struct window *w;
11484 struct glyph_row *row;
11485 struct glyph_matrix *matrix;
11486 int delta, delta_bytes, dy, dvpos;
11487 {
11488 struct glyph *glyph = row->glyphs[TEXT_AREA];
11489 struct glyph *end = glyph + row->used[TEXT_AREA];
11490 struct glyph *cursor = NULL;
11491 /* The first glyph that starts a sequence of glyphs from string. */
11492 struct glyph *string_start;
11493 /* The X coordinate of string_start. */
11494 int string_start_x;
11495 /* The last known character position. */
11496 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11497 /* The last known character position before string_start. */
11498 int string_before_pos;
11499 int x = row->x;
11500 int cursor_x = x;
11501 int cursor_from_overlay_pos = 0;
11502 int pt_old = PT - delta;
11503
11504 /* Skip over glyphs not having an object at the start of the row.
11505 These are special glyphs like truncation marks on terminal
11506 frames. */
11507 if (row->displays_text_p)
11508 while (glyph < end
11509 && INTEGERP (glyph->object)
11510 && glyph->charpos < 0)
11511 {
11512 x += glyph->pixel_width;
11513 ++glyph;
11514 }
11515
11516 string_start = NULL;
11517 while (glyph < end
11518 && !INTEGERP (glyph->object)
11519 && (!BUFFERP (glyph->object)
11520 || (last_pos = glyph->charpos) < pt_old))
11521 {
11522 if (! STRINGP (glyph->object))
11523 {
11524 string_start = NULL;
11525 x += glyph->pixel_width;
11526 ++glyph;
11527 if (cursor_from_overlay_pos
11528 && last_pos > cursor_from_overlay_pos)
11529 {
11530 cursor_from_overlay_pos = 0;
11531 cursor = 0;
11532 }
11533 }
11534 else
11535 {
11536 string_before_pos = last_pos;
11537 string_start = glyph;
11538 string_start_x = x;
11539 /* Skip all glyphs from string. */
11540 do
11541 {
11542 int pos;
11543 if ((cursor == NULL || glyph > cursor)
11544 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11545 Qcursor, (glyph)->object))
11546 && (pos = string_buffer_position (w, glyph->object,
11547 string_before_pos),
11548 (pos == 0 /* From overlay */
11549 || pos == pt_old)))
11550 {
11551 /* Estimate overlay buffer position from the buffer
11552 positions of the glyphs before and after the overlay.
11553 Add 1 to last_pos so that if point corresponds to the
11554 glyph right after the overlay, we still use a 'cursor'
11555 property found in that overlay. */
11556 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11557 cursor = glyph;
11558 cursor_x = x;
11559 }
11560 x += glyph->pixel_width;
11561 ++glyph;
11562 }
11563 while (glyph < end && STRINGP (glyph->object));
11564 }
11565 }
11566
11567 if (cursor != NULL)
11568 {
11569 glyph = cursor;
11570 x = cursor_x;
11571 }
11572 else if (row->ends_in_ellipsis_p && glyph == end)
11573 {
11574 /* Scan back over the ellipsis glyphs, decrementing positions. */
11575 while (glyph > row->glyphs[TEXT_AREA]
11576 && (glyph - 1)->charpos == last_pos)
11577 glyph--, x -= glyph->pixel_width;
11578 /* That loop always goes one position too far,
11579 including the glyph before the ellipsis.
11580 So scan forward over that one. */
11581 x += glyph->pixel_width;
11582 glyph++;
11583 }
11584 else if (string_start
11585 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11586 {
11587 /* We may have skipped over point because the previous glyphs
11588 are from string. As there's no easy way to know the
11589 character position of the current glyph, find the correct
11590 glyph on point by scanning from string_start again. */
11591 Lisp_Object limit;
11592 Lisp_Object string;
11593 int pos;
11594
11595 limit = make_number (pt_old + 1);
11596 end = glyph;
11597 glyph = string_start;
11598 x = string_start_x;
11599 string = glyph->object;
11600 pos = string_buffer_position (w, string, string_before_pos);
11601 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11602 because we always put cursor after overlay strings. */
11603 while (pos == 0 && glyph < end)
11604 {
11605 string = glyph->object;
11606 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11607 if (glyph < end)
11608 pos = string_buffer_position (w, glyph->object, string_before_pos);
11609 }
11610
11611 while (glyph < end)
11612 {
11613 pos = XINT (Fnext_single_char_property_change
11614 (make_number (pos), Qdisplay, Qnil, limit));
11615 if (pos > pt_old)
11616 break;
11617 /* Skip glyphs from the same string. */
11618 string = glyph->object;
11619 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11620 /* Skip glyphs from an overlay. */
11621 while (glyph < end
11622 && ! string_buffer_position (w, glyph->object, pos))
11623 {
11624 string = glyph->object;
11625 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11626 }
11627 }
11628 }
11629
11630 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11631 w->cursor.x = x;
11632 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11633 w->cursor.y = row->y + dy;
11634
11635 if (w == XWINDOW (selected_window))
11636 {
11637 if (!row->continued_p
11638 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11639 && row->x == 0)
11640 {
11641 this_line_buffer = XBUFFER (w->buffer);
11642
11643 CHARPOS (this_line_start_pos)
11644 = MATRIX_ROW_START_CHARPOS (row) + delta;
11645 BYTEPOS (this_line_start_pos)
11646 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11647
11648 CHARPOS (this_line_end_pos)
11649 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11650 BYTEPOS (this_line_end_pos)
11651 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11652
11653 this_line_y = w->cursor.y;
11654 this_line_pixel_height = row->height;
11655 this_line_vpos = w->cursor.vpos;
11656 this_line_start_x = row->x;
11657 }
11658 else
11659 CHARPOS (this_line_start_pos) = 0;
11660 }
11661 }
11662
11663
11664 /* Run window scroll functions, if any, for WINDOW with new window
11665 start STARTP. Sets the window start of WINDOW to that position.
11666
11667 We assume that the window's buffer is really current. */
11668
11669 static INLINE struct text_pos
11670 run_window_scroll_functions (window, startp)
11671 Lisp_Object window;
11672 struct text_pos startp;
11673 {
11674 struct window *w = XWINDOW (window);
11675 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11676
11677 if (current_buffer != XBUFFER (w->buffer))
11678 abort ();
11679
11680 if (!NILP (Vwindow_scroll_functions))
11681 {
11682 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11683 make_number (CHARPOS (startp)));
11684 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11685 /* In case the hook functions switch buffers. */
11686 if (current_buffer != XBUFFER (w->buffer))
11687 set_buffer_internal_1 (XBUFFER (w->buffer));
11688 }
11689
11690 return startp;
11691 }
11692
11693
11694 /* Make sure the line containing the cursor is fully visible.
11695 A value of 1 means there is nothing to be done.
11696 (Either the line is fully visible, or it cannot be made so,
11697 or we cannot tell.)
11698
11699 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11700 is higher than window.
11701
11702 A value of 0 means the caller should do scrolling
11703 as if point had gone off the screen. */
11704
11705 static int
11706 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11707 struct window *w;
11708 int force_p;
11709 int current_matrix_p;
11710 {
11711 struct glyph_matrix *matrix;
11712 struct glyph_row *row;
11713 int window_height;
11714
11715 if (!make_cursor_line_fully_visible_p)
11716 return 1;
11717
11718 /* It's not always possible to find the cursor, e.g, when a window
11719 is full of overlay strings. Don't do anything in that case. */
11720 if (w->cursor.vpos < 0)
11721 return 1;
11722
11723 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11724 row = MATRIX_ROW (matrix, w->cursor.vpos);
11725
11726 /* If the cursor row is not partially visible, there's nothing to do. */
11727 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11728 return 1;
11729
11730 /* If the row the cursor is in is taller than the window's height,
11731 it's not clear what to do, so do nothing. */
11732 window_height = window_box_height (w);
11733 if (row->height >= window_height)
11734 {
11735 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11736 return 1;
11737 }
11738 return 0;
11739
11740 #if 0
11741 /* This code used to try to scroll the window just enough to make
11742 the line visible. It returned 0 to say that the caller should
11743 allocate larger glyph matrices. */
11744
11745 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11746 {
11747 int dy = row->height - row->visible_height;
11748 w->vscroll = 0;
11749 w->cursor.y += dy;
11750 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11751 }
11752 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11753 {
11754 int dy = - (row->height - row->visible_height);
11755 w->vscroll = dy;
11756 w->cursor.y += dy;
11757 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11758 }
11759
11760 /* When we change the cursor y-position of the selected window,
11761 change this_line_y as well so that the display optimization for
11762 the cursor line of the selected window in redisplay_internal uses
11763 the correct y-position. */
11764 if (w == XWINDOW (selected_window))
11765 this_line_y = w->cursor.y;
11766
11767 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11768 redisplay with larger matrices. */
11769 if (matrix->nrows < required_matrix_height (w))
11770 {
11771 fonts_changed_p = 1;
11772 return 0;
11773 }
11774
11775 return 1;
11776 #endif /* 0 */
11777 }
11778
11779
11780 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11781 non-zero means only WINDOW is redisplayed in redisplay_internal.
11782 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11783 in redisplay_window to bring a partially visible line into view in
11784 the case that only the cursor has moved.
11785
11786 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11787 last screen line's vertical height extends past the end of the screen.
11788
11789 Value is
11790
11791 1 if scrolling succeeded
11792
11793 0 if scrolling didn't find point.
11794
11795 -1 if new fonts have been loaded so that we must interrupt
11796 redisplay, adjust glyph matrices, and try again. */
11797
11798 enum
11799 {
11800 SCROLLING_SUCCESS,
11801 SCROLLING_FAILED,
11802 SCROLLING_NEED_LARGER_MATRICES
11803 };
11804
11805 static int
11806 try_scrolling (window, just_this_one_p, scroll_conservatively,
11807 scroll_step, temp_scroll_step, last_line_misfit)
11808 Lisp_Object window;
11809 int just_this_one_p;
11810 EMACS_INT scroll_conservatively, scroll_step;
11811 int temp_scroll_step;
11812 int last_line_misfit;
11813 {
11814 struct window *w = XWINDOW (window);
11815 struct frame *f = XFRAME (w->frame);
11816 struct text_pos scroll_margin_pos;
11817 struct text_pos pos;
11818 struct text_pos startp;
11819 struct it it;
11820 Lisp_Object window_end;
11821 int this_scroll_margin;
11822 int dy = 0;
11823 int scroll_max;
11824 int rc;
11825 int amount_to_scroll = 0;
11826 Lisp_Object aggressive;
11827 int height;
11828 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11829
11830 #if GLYPH_DEBUG
11831 debug_method_add (w, "try_scrolling");
11832 #endif
11833
11834 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11835
11836 /* Compute scroll margin height in pixels. We scroll when point is
11837 within this distance from the top or bottom of the window. */
11838 if (scroll_margin > 0)
11839 {
11840 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11841 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11842 }
11843 else
11844 this_scroll_margin = 0;
11845
11846 /* Force scroll_conservatively to have a reasonable value so it doesn't
11847 cause an overflow while computing how much to scroll. */
11848 if (scroll_conservatively)
11849 scroll_conservatively = min (scroll_conservatively,
11850 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11851
11852 /* Compute how much we should try to scroll maximally to bring point
11853 into view. */
11854 if (scroll_step || scroll_conservatively || temp_scroll_step)
11855 scroll_max = max (scroll_step,
11856 max (scroll_conservatively, temp_scroll_step));
11857 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11858 || NUMBERP (current_buffer->scroll_up_aggressively))
11859 /* We're trying to scroll because of aggressive scrolling
11860 but no scroll_step is set. Choose an arbitrary one. Maybe
11861 there should be a variable for this. */
11862 scroll_max = 10;
11863 else
11864 scroll_max = 0;
11865 scroll_max *= FRAME_LINE_HEIGHT (f);
11866
11867 /* Decide whether we have to scroll down. Start at the window end
11868 and move this_scroll_margin up to find the position of the scroll
11869 margin. */
11870 window_end = Fwindow_end (window, Qt);
11871
11872 too_near_end:
11873
11874 CHARPOS (scroll_margin_pos) = XINT (window_end);
11875 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11876
11877 if (this_scroll_margin || extra_scroll_margin_lines)
11878 {
11879 start_display (&it, w, scroll_margin_pos);
11880 if (this_scroll_margin)
11881 move_it_vertically_backward (&it, this_scroll_margin);
11882 if (extra_scroll_margin_lines)
11883 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11884 scroll_margin_pos = it.current.pos;
11885 }
11886
11887 if (PT >= CHARPOS (scroll_margin_pos))
11888 {
11889 int y0;
11890
11891 /* Point is in the scroll margin at the bottom of the window, or
11892 below. Compute a new window start that makes point visible. */
11893
11894 /* Compute the distance from the scroll margin to PT.
11895 Give up if the distance is greater than scroll_max. */
11896 start_display (&it, w, scroll_margin_pos);
11897 y0 = it.current_y;
11898 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11899 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11900
11901 /* To make point visible, we have to move the window start
11902 down so that the line the cursor is in is visible, which
11903 means we have to add in the height of the cursor line. */
11904 dy = line_bottom_y (&it) - y0;
11905
11906 if (dy > scroll_max)
11907 return SCROLLING_FAILED;
11908
11909 /* Move the window start down. If scrolling conservatively,
11910 move it just enough down to make point visible. If
11911 scroll_step is set, move it down by scroll_step. */
11912 start_display (&it, w, startp);
11913
11914 if (scroll_conservatively)
11915 /* Set AMOUNT_TO_SCROLL to at least one line,
11916 and at most scroll_conservatively lines. */
11917 amount_to_scroll
11918 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11919 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11920 else if (scroll_step || temp_scroll_step)
11921 amount_to_scroll = scroll_max;
11922 else
11923 {
11924 aggressive = current_buffer->scroll_up_aggressively;
11925 height = WINDOW_BOX_TEXT_HEIGHT (w);
11926 if (NUMBERP (aggressive))
11927 {
11928 double float_amount = XFLOATINT (aggressive) * height;
11929 amount_to_scroll = float_amount;
11930 if (amount_to_scroll == 0 && float_amount > 0)
11931 amount_to_scroll = 1;
11932 }
11933 }
11934
11935 if (amount_to_scroll <= 0)
11936 return SCROLLING_FAILED;
11937
11938 /* If moving by amount_to_scroll leaves STARTP unchanged,
11939 move it down one screen line. */
11940
11941 move_it_vertically (&it, amount_to_scroll);
11942 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11943 move_it_by_lines (&it, 1, 1);
11944 startp = it.current.pos;
11945 }
11946 else
11947 {
11948 /* See if point is inside the scroll margin at the top of the
11949 window. */
11950 scroll_margin_pos = startp;
11951 if (this_scroll_margin)
11952 {
11953 start_display (&it, w, startp);
11954 move_it_vertically (&it, this_scroll_margin);
11955 scroll_margin_pos = it.current.pos;
11956 }
11957
11958 if (PT < CHARPOS (scroll_margin_pos))
11959 {
11960 /* Point is in the scroll margin at the top of the window or
11961 above what is displayed in the window. */
11962 int y0;
11963
11964 /* Compute the vertical distance from PT to the scroll
11965 margin position. Give up if distance is greater than
11966 scroll_max. */
11967 SET_TEXT_POS (pos, PT, PT_BYTE);
11968 start_display (&it, w, pos);
11969 y0 = it.current_y;
11970 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11971 it.last_visible_y, -1,
11972 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11973 dy = it.current_y - y0;
11974 if (dy > scroll_max)
11975 return SCROLLING_FAILED;
11976
11977 /* Compute new window start. */
11978 start_display (&it, w, startp);
11979
11980 if (scroll_conservatively)
11981 amount_to_scroll
11982 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11983 else if (scroll_step || temp_scroll_step)
11984 amount_to_scroll = scroll_max;
11985 else
11986 {
11987 aggressive = current_buffer->scroll_down_aggressively;
11988 height = WINDOW_BOX_TEXT_HEIGHT (w);
11989 if (NUMBERP (aggressive))
11990 {
11991 double float_amount = XFLOATINT (aggressive) * height;
11992 amount_to_scroll = float_amount;
11993 if (amount_to_scroll == 0 && float_amount > 0)
11994 amount_to_scroll = 1;
11995 }
11996 }
11997
11998 if (amount_to_scroll <= 0)
11999 return SCROLLING_FAILED;
12000
12001 move_it_vertically_backward (&it, amount_to_scroll);
12002 startp = it.current.pos;
12003 }
12004 }
12005
12006 /* Run window scroll functions. */
12007 startp = run_window_scroll_functions (window, startp);
12008
12009 /* Display the window. Give up if new fonts are loaded, or if point
12010 doesn't appear. */
12011 if (!try_window (window, startp, 0))
12012 rc = SCROLLING_NEED_LARGER_MATRICES;
12013 else if (w->cursor.vpos < 0)
12014 {
12015 clear_glyph_matrix (w->desired_matrix);
12016 rc = SCROLLING_FAILED;
12017 }
12018 else
12019 {
12020 /* Maybe forget recorded base line for line number display. */
12021 if (!just_this_one_p
12022 || current_buffer->clip_changed
12023 || BEG_UNCHANGED < CHARPOS (startp))
12024 w->base_line_number = Qnil;
12025
12026 /* If cursor ends up on a partially visible line,
12027 treat that as being off the bottom of the screen. */
12028 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12029 {
12030 clear_glyph_matrix (w->desired_matrix);
12031 ++extra_scroll_margin_lines;
12032 goto too_near_end;
12033 }
12034 rc = SCROLLING_SUCCESS;
12035 }
12036
12037 return rc;
12038 }
12039
12040
12041 /* Compute a suitable window start for window W if display of W starts
12042 on a continuation line. Value is non-zero if a new window start
12043 was computed.
12044
12045 The new window start will be computed, based on W's width, starting
12046 from the start of the continued line. It is the start of the
12047 screen line with the minimum distance from the old start W->start. */
12048
12049 static int
12050 compute_window_start_on_continuation_line (w)
12051 struct window *w;
12052 {
12053 struct text_pos pos, start_pos;
12054 int window_start_changed_p = 0;
12055
12056 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12057
12058 /* If window start is on a continuation line... Window start may be
12059 < BEGV in case there's invisible text at the start of the
12060 buffer (M-x rmail, for example). */
12061 if (CHARPOS (start_pos) > BEGV
12062 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12063 {
12064 struct it it;
12065 struct glyph_row *row;
12066
12067 /* Handle the case that the window start is out of range. */
12068 if (CHARPOS (start_pos) < BEGV)
12069 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12070 else if (CHARPOS (start_pos) > ZV)
12071 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12072
12073 /* Find the start of the continued line. This should be fast
12074 because scan_buffer is fast (newline cache). */
12075 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12076 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12077 row, DEFAULT_FACE_ID);
12078 reseat_at_previous_visible_line_start (&it);
12079
12080 /* If the line start is "too far" away from the window start,
12081 say it takes too much time to compute a new window start. */
12082 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12083 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12084 {
12085 int min_distance, distance;
12086
12087 /* Move forward by display lines to find the new window
12088 start. If window width was enlarged, the new start can
12089 be expected to be > the old start. If window width was
12090 decreased, the new window start will be < the old start.
12091 So, we're looking for the display line start with the
12092 minimum distance from the old window start. */
12093 pos = it.current.pos;
12094 min_distance = INFINITY;
12095 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12096 distance < min_distance)
12097 {
12098 min_distance = distance;
12099 pos = it.current.pos;
12100 move_it_by_lines (&it, 1, 0);
12101 }
12102
12103 /* Set the window start there. */
12104 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12105 window_start_changed_p = 1;
12106 }
12107 }
12108
12109 return window_start_changed_p;
12110 }
12111
12112
12113 /* Try cursor movement in case text has not changed in window WINDOW,
12114 with window start STARTP. Value is
12115
12116 CURSOR_MOVEMENT_SUCCESS if successful
12117
12118 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12119
12120 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12121 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12122 we want to scroll as if scroll-step were set to 1. See the code.
12123
12124 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12125 which case we have to abort this redisplay, and adjust matrices
12126 first. */
12127
12128 enum
12129 {
12130 CURSOR_MOVEMENT_SUCCESS,
12131 CURSOR_MOVEMENT_CANNOT_BE_USED,
12132 CURSOR_MOVEMENT_MUST_SCROLL,
12133 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12134 };
12135
12136 static int
12137 try_cursor_movement (window, startp, scroll_step)
12138 Lisp_Object window;
12139 struct text_pos startp;
12140 int *scroll_step;
12141 {
12142 struct window *w = XWINDOW (window);
12143 struct frame *f = XFRAME (w->frame);
12144 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12145
12146 #if GLYPH_DEBUG
12147 if (inhibit_try_cursor_movement)
12148 return rc;
12149 #endif
12150
12151 /* Handle case where text has not changed, only point, and it has
12152 not moved off the frame. */
12153 if (/* Point may be in this window. */
12154 PT >= CHARPOS (startp)
12155 /* Selective display hasn't changed. */
12156 && !current_buffer->clip_changed
12157 /* Function force-mode-line-update is used to force a thorough
12158 redisplay. It sets either windows_or_buffers_changed or
12159 update_mode_lines. So don't take a shortcut here for these
12160 cases. */
12161 && !update_mode_lines
12162 && !windows_or_buffers_changed
12163 && !cursor_type_changed
12164 /* Can't use this case if highlighting a region. When a
12165 region exists, cursor movement has to do more than just
12166 set the cursor. */
12167 && !(!NILP (Vtransient_mark_mode)
12168 && !NILP (current_buffer->mark_active))
12169 && NILP (w->region_showing)
12170 && NILP (Vshow_trailing_whitespace)
12171 /* Right after splitting windows, last_point may be nil. */
12172 && INTEGERP (w->last_point)
12173 /* This code is not used for mini-buffer for the sake of the case
12174 of redisplaying to replace an echo area message; since in
12175 that case the mini-buffer contents per se are usually
12176 unchanged. This code is of no real use in the mini-buffer
12177 since the handling of this_line_start_pos, etc., in redisplay
12178 handles the same cases. */
12179 && !EQ (window, minibuf_window)
12180 /* When splitting windows or for new windows, it happens that
12181 redisplay is called with a nil window_end_vpos or one being
12182 larger than the window. This should really be fixed in
12183 window.c. I don't have this on my list, now, so we do
12184 approximately the same as the old redisplay code. --gerd. */
12185 && INTEGERP (w->window_end_vpos)
12186 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12187 && (FRAME_WINDOW_P (f)
12188 || !overlay_arrow_in_current_buffer_p ()))
12189 {
12190 int this_scroll_margin, top_scroll_margin;
12191 struct glyph_row *row = NULL;
12192
12193 #if GLYPH_DEBUG
12194 debug_method_add (w, "cursor movement");
12195 #endif
12196
12197 /* Scroll if point within this distance from the top or bottom
12198 of the window. This is a pixel value. */
12199 this_scroll_margin = max (0, scroll_margin);
12200 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12201 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12202
12203 top_scroll_margin = this_scroll_margin;
12204 if (WINDOW_WANTS_HEADER_LINE_P (w))
12205 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12206
12207 /* Start with the row the cursor was displayed during the last
12208 not paused redisplay. Give up if that row is not valid. */
12209 if (w->last_cursor.vpos < 0
12210 || w->last_cursor.vpos >= w->current_matrix->nrows)
12211 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12212 else
12213 {
12214 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12215 if (row->mode_line_p)
12216 ++row;
12217 if (!row->enabled_p)
12218 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12219 }
12220
12221 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12222 {
12223 int scroll_p = 0;
12224 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12225
12226 if (PT > XFASTINT (w->last_point))
12227 {
12228 /* Point has moved forward. */
12229 while (MATRIX_ROW_END_CHARPOS (row) < PT
12230 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12231 {
12232 xassert (row->enabled_p);
12233 ++row;
12234 }
12235
12236 /* The end position of a row equals the start position
12237 of the next row. If PT is there, we would rather
12238 display it in the next line. */
12239 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12240 && MATRIX_ROW_END_CHARPOS (row) == PT
12241 && !cursor_row_p (w, row))
12242 ++row;
12243
12244 /* If within the scroll margin, scroll. Note that
12245 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12246 the next line would be drawn, and that
12247 this_scroll_margin can be zero. */
12248 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12249 || PT > MATRIX_ROW_END_CHARPOS (row)
12250 /* Line is completely visible last line in window
12251 and PT is to be set in the next line. */
12252 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12253 && PT == MATRIX_ROW_END_CHARPOS (row)
12254 && !row->ends_at_zv_p
12255 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12256 scroll_p = 1;
12257 }
12258 else if (PT < XFASTINT (w->last_point))
12259 {
12260 /* Cursor has to be moved backward. Note that PT >=
12261 CHARPOS (startp) because of the outer if-statement. */
12262 while (!row->mode_line_p
12263 && (MATRIX_ROW_START_CHARPOS (row) > PT
12264 || (MATRIX_ROW_START_CHARPOS (row) == PT
12265 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12266 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12267 row > w->current_matrix->rows
12268 && (row-1)->ends_in_newline_from_string_p))))
12269 && (row->y > top_scroll_margin
12270 || CHARPOS (startp) == BEGV))
12271 {
12272 xassert (row->enabled_p);
12273 --row;
12274 }
12275
12276 /* Consider the following case: Window starts at BEGV,
12277 there is invisible, intangible text at BEGV, so that
12278 display starts at some point START > BEGV. It can
12279 happen that we are called with PT somewhere between
12280 BEGV and START. Try to handle that case. */
12281 if (row < w->current_matrix->rows
12282 || row->mode_line_p)
12283 {
12284 row = w->current_matrix->rows;
12285 if (row->mode_line_p)
12286 ++row;
12287 }
12288
12289 /* Due to newlines in overlay strings, we may have to
12290 skip forward over overlay strings. */
12291 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12292 && MATRIX_ROW_END_CHARPOS (row) == PT
12293 && !cursor_row_p (w, row))
12294 ++row;
12295
12296 /* If within the scroll margin, scroll. */
12297 if (row->y < top_scroll_margin
12298 && CHARPOS (startp) != BEGV)
12299 scroll_p = 1;
12300 }
12301 else
12302 {
12303 /* Cursor did not move. So don't scroll even if cursor line
12304 is partially visible, as it was so before. */
12305 rc = CURSOR_MOVEMENT_SUCCESS;
12306 }
12307
12308 if (PT < MATRIX_ROW_START_CHARPOS (row)
12309 || PT > MATRIX_ROW_END_CHARPOS (row))
12310 {
12311 /* if PT is not in the glyph row, give up. */
12312 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12313 }
12314 else if (rc != CURSOR_MOVEMENT_SUCCESS
12315 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12316 && make_cursor_line_fully_visible_p)
12317 {
12318 if (PT == MATRIX_ROW_END_CHARPOS (row)
12319 && !row->ends_at_zv_p
12320 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12321 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12322 else if (row->height > window_box_height (w))
12323 {
12324 /* If we end up in a partially visible line, let's
12325 make it fully visible, except when it's taller
12326 than the window, in which case we can't do much
12327 about it. */
12328 *scroll_step = 1;
12329 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12330 }
12331 else
12332 {
12333 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12334 if (!cursor_row_fully_visible_p (w, 0, 1))
12335 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12336 else
12337 rc = CURSOR_MOVEMENT_SUCCESS;
12338 }
12339 }
12340 else if (scroll_p)
12341 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12342 else
12343 {
12344 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12345 rc = CURSOR_MOVEMENT_SUCCESS;
12346 }
12347 }
12348 }
12349
12350 return rc;
12351 }
12352
12353 void
12354 set_vertical_scroll_bar (w)
12355 struct window *w;
12356 {
12357 int start, end, whole;
12358
12359 /* Calculate the start and end positions for the current window.
12360 At some point, it would be nice to choose between scrollbars
12361 which reflect the whole buffer size, with special markers
12362 indicating narrowing, and scrollbars which reflect only the
12363 visible region.
12364
12365 Note that mini-buffers sometimes aren't displaying any text. */
12366 if (!MINI_WINDOW_P (w)
12367 || (w == XWINDOW (minibuf_window)
12368 && NILP (echo_area_buffer[0])))
12369 {
12370 struct buffer *buf = XBUFFER (w->buffer);
12371 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12372 start = marker_position (w->start) - BUF_BEGV (buf);
12373 /* I don't think this is guaranteed to be right. For the
12374 moment, we'll pretend it is. */
12375 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12376
12377 if (end < start)
12378 end = start;
12379 if (whole < (end - start))
12380 whole = end - start;
12381 }
12382 else
12383 start = end = whole = 0;
12384
12385 /* Indicate what this scroll bar ought to be displaying now. */
12386 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12387 }
12388
12389
12390 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12391 selected_window is redisplayed.
12392
12393 We can return without actually redisplaying the window if
12394 fonts_changed_p is nonzero. In that case, redisplay_internal will
12395 retry. */
12396
12397 static void
12398 redisplay_window (window, just_this_one_p)
12399 Lisp_Object window;
12400 int just_this_one_p;
12401 {
12402 struct window *w = XWINDOW (window);
12403 struct frame *f = XFRAME (w->frame);
12404 struct buffer *buffer = XBUFFER (w->buffer);
12405 struct buffer *old = current_buffer;
12406 struct text_pos lpoint, opoint, startp;
12407 int update_mode_line;
12408 int tem;
12409 struct it it;
12410 /* Record it now because it's overwritten. */
12411 int current_matrix_up_to_date_p = 0;
12412 int used_current_matrix_p = 0;
12413 /* This is less strict than current_matrix_up_to_date_p.
12414 It indictes that the buffer contents and narrowing are unchanged. */
12415 int buffer_unchanged_p = 0;
12416 int temp_scroll_step = 0;
12417 int count = SPECPDL_INDEX ();
12418 int rc;
12419 int centering_position = -1;
12420 int last_line_misfit = 0;
12421
12422 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12423 opoint = lpoint;
12424
12425 /* W must be a leaf window here. */
12426 xassert (!NILP (w->buffer));
12427 #if GLYPH_DEBUG
12428 *w->desired_matrix->method = 0;
12429 #endif
12430
12431 specbind (Qinhibit_point_motion_hooks, Qt);
12432
12433 reconsider_clip_changes (w, buffer);
12434
12435 /* Has the mode line to be updated? */
12436 update_mode_line = (!NILP (w->update_mode_line)
12437 || update_mode_lines
12438 || buffer->clip_changed
12439 || buffer->prevent_redisplay_optimizations_p);
12440
12441 if (MINI_WINDOW_P (w))
12442 {
12443 if (w == XWINDOW (echo_area_window)
12444 && !NILP (echo_area_buffer[0]))
12445 {
12446 if (update_mode_line)
12447 /* We may have to update a tty frame's menu bar or a
12448 tool-bar. Example `M-x C-h C-h C-g'. */
12449 goto finish_menu_bars;
12450 else
12451 /* We've already displayed the echo area glyphs in this window. */
12452 goto finish_scroll_bars;
12453 }
12454 else if ((w != XWINDOW (minibuf_window)
12455 || minibuf_level == 0)
12456 /* When buffer is nonempty, redisplay window normally. */
12457 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12458 /* Quail displays non-mini buffers in minibuffer window.
12459 In that case, redisplay the window normally. */
12460 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12461 {
12462 /* W is a mini-buffer window, but it's not active, so clear
12463 it. */
12464 int yb = window_text_bottom_y (w);
12465 struct glyph_row *row;
12466 int y;
12467
12468 for (y = 0, row = w->desired_matrix->rows;
12469 y < yb;
12470 y += row->height, ++row)
12471 blank_row (w, row, y);
12472 goto finish_scroll_bars;
12473 }
12474
12475 clear_glyph_matrix (w->desired_matrix);
12476 }
12477
12478 /* Otherwise set up data on this window; select its buffer and point
12479 value. */
12480 /* Really select the buffer, for the sake of buffer-local
12481 variables. */
12482 set_buffer_internal_1 (XBUFFER (w->buffer));
12483 SET_TEXT_POS (opoint, PT, PT_BYTE);
12484
12485 current_matrix_up_to_date_p
12486 = (!NILP (w->window_end_valid)
12487 && !current_buffer->clip_changed
12488 && !current_buffer->prevent_redisplay_optimizations_p
12489 && XFASTINT (w->last_modified) >= MODIFF
12490 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12491
12492 buffer_unchanged_p
12493 = (!NILP (w->window_end_valid)
12494 && !current_buffer->clip_changed
12495 && XFASTINT (w->last_modified) >= MODIFF
12496 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12497
12498 /* When windows_or_buffers_changed is non-zero, we can't rely on
12499 the window end being valid, so set it to nil there. */
12500 if (windows_or_buffers_changed)
12501 {
12502 /* If window starts on a continuation line, maybe adjust the
12503 window start in case the window's width changed. */
12504 if (XMARKER (w->start)->buffer == current_buffer)
12505 compute_window_start_on_continuation_line (w);
12506
12507 w->window_end_valid = Qnil;
12508 }
12509
12510 /* Some sanity checks. */
12511 CHECK_WINDOW_END (w);
12512 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12513 abort ();
12514 if (BYTEPOS (opoint) < CHARPOS (opoint))
12515 abort ();
12516
12517 /* If %c is in mode line, update it if needed. */
12518 if (!NILP (w->column_number_displayed)
12519 /* This alternative quickly identifies a common case
12520 where no change is needed. */
12521 && !(PT == XFASTINT (w->last_point)
12522 && XFASTINT (w->last_modified) >= MODIFF
12523 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12524 && (XFASTINT (w->column_number_displayed)
12525 != (int) current_column ())) /* iftc */
12526 update_mode_line = 1;
12527
12528 /* Count number of windows showing the selected buffer. An indirect
12529 buffer counts as its base buffer. */
12530 if (!just_this_one_p)
12531 {
12532 struct buffer *current_base, *window_base;
12533 current_base = current_buffer;
12534 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12535 if (current_base->base_buffer)
12536 current_base = current_base->base_buffer;
12537 if (window_base->base_buffer)
12538 window_base = window_base->base_buffer;
12539 if (current_base == window_base)
12540 buffer_shared++;
12541 }
12542
12543 /* Point refers normally to the selected window. For any other
12544 window, set up appropriate value. */
12545 if (!EQ (window, selected_window))
12546 {
12547 int new_pt = XMARKER (w->pointm)->charpos;
12548 int new_pt_byte = marker_byte_position (w->pointm);
12549 if (new_pt < BEGV)
12550 {
12551 new_pt = BEGV;
12552 new_pt_byte = BEGV_BYTE;
12553 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12554 }
12555 else if (new_pt > (ZV - 1))
12556 {
12557 new_pt = ZV;
12558 new_pt_byte = ZV_BYTE;
12559 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12560 }
12561
12562 /* We don't use SET_PT so that the point-motion hooks don't run. */
12563 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12564 }
12565
12566 /* If any of the character widths specified in the display table
12567 have changed, invalidate the width run cache. It's true that
12568 this may be a bit late to catch such changes, but the rest of
12569 redisplay goes (non-fatally) haywire when the display table is
12570 changed, so why should we worry about doing any better? */
12571 if (current_buffer->width_run_cache)
12572 {
12573 struct Lisp_Char_Table *disptab = buffer_display_table ();
12574
12575 if (! disptab_matches_widthtab (disptab,
12576 XVECTOR (current_buffer->width_table)))
12577 {
12578 invalidate_region_cache (current_buffer,
12579 current_buffer->width_run_cache,
12580 BEG, Z);
12581 recompute_width_table (current_buffer, disptab);
12582 }
12583 }
12584
12585 /* If window-start is screwed up, choose a new one. */
12586 if (XMARKER (w->start)->buffer != current_buffer)
12587 goto recenter;
12588
12589 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12590
12591 /* If someone specified a new starting point but did not insist,
12592 check whether it can be used. */
12593 if (!NILP (w->optional_new_start)
12594 && CHARPOS (startp) >= BEGV
12595 && CHARPOS (startp) <= ZV)
12596 {
12597 w->optional_new_start = Qnil;
12598 start_display (&it, w, startp);
12599 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12600 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12601 if (IT_CHARPOS (it) == PT)
12602 w->force_start = Qt;
12603 /* IT may overshoot PT if text at PT is invisible. */
12604 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12605 w->force_start = Qt;
12606
12607
12608 }
12609
12610 /* Handle case where place to start displaying has been specified,
12611 unless the specified location is outside the accessible range. */
12612 if (!NILP (w->force_start)
12613 || w->frozen_window_start_p)
12614 {
12615 /* We set this later on if we have to adjust point. */
12616 int new_vpos = -1;
12617 int val;
12618
12619 w->force_start = Qnil;
12620 w->vscroll = 0;
12621 w->window_end_valid = Qnil;
12622
12623 /* Forget any recorded base line for line number display. */
12624 if (!buffer_unchanged_p)
12625 w->base_line_number = Qnil;
12626
12627 /* Redisplay the mode line. Select the buffer properly for that.
12628 Also, run the hook window-scroll-functions
12629 because we have scrolled. */
12630 /* Note, we do this after clearing force_start because
12631 if there's an error, it is better to forget about force_start
12632 than to get into an infinite loop calling the hook functions
12633 and having them get more errors. */
12634 if (!update_mode_line
12635 || ! NILP (Vwindow_scroll_functions))
12636 {
12637 update_mode_line = 1;
12638 w->update_mode_line = Qt;
12639 startp = run_window_scroll_functions (window, startp);
12640 }
12641
12642 w->last_modified = make_number (0);
12643 w->last_overlay_modified = make_number (0);
12644 if (CHARPOS (startp) < BEGV)
12645 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12646 else if (CHARPOS (startp) > ZV)
12647 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12648
12649 /* Redisplay, then check if cursor has been set during the
12650 redisplay. Give up if new fonts were loaded. */
12651 val = try_window (window, startp, 1);
12652 if (!val)
12653 {
12654 w->force_start = Qt;
12655 clear_glyph_matrix (w->desired_matrix);
12656 goto need_larger_matrices;
12657 }
12658 /* Point was outside the scroll margins. */
12659 if (val < 0)
12660 new_vpos = window_box_height (w) / 2;
12661
12662 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12663 {
12664 /* If point does not appear, try to move point so it does
12665 appear. The desired matrix has been built above, so we
12666 can use it here. */
12667 new_vpos = window_box_height (w) / 2;
12668 }
12669
12670 if (!cursor_row_fully_visible_p (w, 0, 0))
12671 {
12672 /* Point does appear, but on a line partly visible at end of window.
12673 Move it back to a fully-visible line. */
12674 new_vpos = window_box_height (w);
12675 }
12676
12677 /* If we need to move point for either of the above reasons,
12678 now actually do it. */
12679 if (new_vpos >= 0)
12680 {
12681 struct glyph_row *row;
12682
12683 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12684 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12685 ++row;
12686
12687 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12688 MATRIX_ROW_START_BYTEPOS (row));
12689
12690 if (w != XWINDOW (selected_window))
12691 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12692 else if (current_buffer == old)
12693 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12694
12695 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12696
12697 /* If we are highlighting the region, then we just changed
12698 the region, so redisplay to show it. */
12699 if (!NILP (Vtransient_mark_mode)
12700 && !NILP (current_buffer->mark_active))
12701 {
12702 clear_glyph_matrix (w->desired_matrix);
12703 if (!try_window (window, startp, 0))
12704 goto need_larger_matrices;
12705 }
12706 }
12707
12708 #if GLYPH_DEBUG
12709 debug_method_add (w, "forced window start");
12710 #endif
12711 goto done;
12712 }
12713
12714 /* Handle case where text has not changed, only point, and it has
12715 not moved off the frame, and we are not retrying after hscroll.
12716 (current_matrix_up_to_date_p is nonzero when retrying.) */
12717 if (current_matrix_up_to_date_p
12718 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12719 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12720 {
12721 switch (rc)
12722 {
12723 case CURSOR_MOVEMENT_SUCCESS:
12724 used_current_matrix_p = 1;
12725 goto done;
12726
12727 #if 0 /* try_cursor_movement never returns this value. */
12728 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12729 goto need_larger_matrices;
12730 #endif
12731
12732 case CURSOR_MOVEMENT_MUST_SCROLL:
12733 goto try_to_scroll;
12734
12735 default:
12736 abort ();
12737 }
12738 }
12739 /* If current starting point was originally the beginning of a line
12740 but no longer is, find a new starting point. */
12741 else if (!NILP (w->start_at_line_beg)
12742 && !(CHARPOS (startp) <= BEGV
12743 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12744 {
12745 #if GLYPH_DEBUG
12746 debug_method_add (w, "recenter 1");
12747 #endif
12748 goto recenter;
12749 }
12750
12751 /* Try scrolling with try_window_id. Value is > 0 if update has
12752 been done, it is -1 if we know that the same window start will
12753 not work. It is 0 if unsuccessful for some other reason. */
12754 else if ((tem = try_window_id (w)) != 0)
12755 {
12756 #if GLYPH_DEBUG
12757 debug_method_add (w, "try_window_id %d", tem);
12758 #endif
12759
12760 if (fonts_changed_p)
12761 goto need_larger_matrices;
12762 if (tem > 0)
12763 goto done;
12764
12765 /* Otherwise try_window_id has returned -1 which means that we
12766 don't want the alternative below this comment to execute. */
12767 }
12768 else if (CHARPOS (startp) >= BEGV
12769 && CHARPOS (startp) <= ZV
12770 && PT >= CHARPOS (startp)
12771 && (CHARPOS (startp) < ZV
12772 /* Avoid starting at end of buffer. */
12773 || CHARPOS (startp) == BEGV
12774 || (XFASTINT (w->last_modified) >= MODIFF
12775 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12776 {
12777 #if GLYPH_DEBUG
12778 debug_method_add (w, "same window start");
12779 #endif
12780
12781 /* Try to redisplay starting at same place as before.
12782 If point has not moved off frame, accept the results. */
12783 if (!current_matrix_up_to_date_p
12784 /* Don't use try_window_reusing_current_matrix in this case
12785 because a window scroll function can have changed the
12786 buffer. */
12787 || !NILP (Vwindow_scroll_functions)
12788 || MINI_WINDOW_P (w)
12789 || !(used_current_matrix_p
12790 = try_window_reusing_current_matrix (w)))
12791 {
12792 IF_DEBUG (debug_method_add (w, "1"));
12793 if (try_window (window, startp, 1) < 0)
12794 /* -1 means we need to scroll.
12795 0 means we need new matrices, but fonts_changed_p
12796 is set in that case, so we will detect it below. */
12797 goto try_to_scroll;
12798 }
12799
12800 if (fonts_changed_p)
12801 goto need_larger_matrices;
12802
12803 if (w->cursor.vpos >= 0)
12804 {
12805 if (!just_this_one_p
12806 || current_buffer->clip_changed
12807 || BEG_UNCHANGED < CHARPOS (startp))
12808 /* Forget any recorded base line for line number display. */
12809 w->base_line_number = Qnil;
12810
12811 if (!cursor_row_fully_visible_p (w, 1, 0))
12812 {
12813 clear_glyph_matrix (w->desired_matrix);
12814 last_line_misfit = 1;
12815 }
12816 /* Drop through and scroll. */
12817 else
12818 goto done;
12819 }
12820 else
12821 clear_glyph_matrix (w->desired_matrix);
12822 }
12823
12824 try_to_scroll:
12825
12826 w->last_modified = make_number (0);
12827 w->last_overlay_modified = make_number (0);
12828
12829 /* Redisplay the mode line. Select the buffer properly for that. */
12830 if (!update_mode_line)
12831 {
12832 update_mode_line = 1;
12833 w->update_mode_line = Qt;
12834 }
12835
12836 /* Try to scroll by specified few lines. */
12837 if ((scroll_conservatively
12838 || scroll_step
12839 || temp_scroll_step
12840 || NUMBERP (current_buffer->scroll_up_aggressively)
12841 || NUMBERP (current_buffer->scroll_down_aggressively))
12842 && !current_buffer->clip_changed
12843 && CHARPOS (startp) >= BEGV
12844 && CHARPOS (startp) <= ZV)
12845 {
12846 /* The function returns -1 if new fonts were loaded, 1 if
12847 successful, 0 if not successful. */
12848 int rc = try_scrolling (window, just_this_one_p,
12849 scroll_conservatively,
12850 scroll_step,
12851 temp_scroll_step, last_line_misfit);
12852 switch (rc)
12853 {
12854 case SCROLLING_SUCCESS:
12855 goto done;
12856
12857 case SCROLLING_NEED_LARGER_MATRICES:
12858 goto need_larger_matrices;
12859
12860 case SCROLLING_FAILED:
12861 break;
12862
12863 default:
12864 abort ();
12865 }
12866 }
12867
12868 /* Finally, just choose place to start which centers point */
12869
12870 recenter:
12871 if (centering_position < 0)
12872 centering_position = window_box_height (w) / 2;
12873
12874 #if GLYPH_DEBUG
12875 debug_method_add (w, "recenter");
12876 #endif
12877
12878 /* w->vscroll = 0; */
12879
12880 /* Forget any previously recorded base line for line number display. */
12881 if (!buffer_unchanged_p)
12882 w->base_line_number = Qnil;
12883
12884 /* Move backward half the height of the window. */
12885 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12886 it.current_y = it.last_visible_y;
12887 move_it_vertically_backward (&it, centering_position);
12888 xassert (IT_CHARPOS (it) >= BEGV);
12889
12890 /* The function move_it_vertically_backward may move over more
12891 than the specified y-distance. If it->w is small, e.g. a
12892 mini-buffer window, we may end up in front of the window's
12893 display area. Start displaying at the start of the line
12894 containing PT in this case. */
12895 if (it.current_y <= 0)
12896 {
12897 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12898 move_it_vertically_backward (&it, 0);
12899 #if 0
12900 /* I think this assert is bogus if buffer contains
12901 invisible text or images. KFS. */
12902 xassert (IT_CHARPOS (it) <= PT);
12903 #endif
12904 it.current_y = 0;
12905 }
12906
12907 it.current_x = it.hpos = 0;
12908
12909 /* Set startp here explicitly in case that helps avoid an infinite loop
12910 in case the window-scroll-functions functions get errors. */
12911 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12912
12913 /* Run scroll hooks. */
12914 startp = run_window_scroll_functions (window, it.current.pos);
12915
12916 /* Redisplay the window. */
12917 if (!current_matrix_up_to_date_p
12918 || windows_or_buffers_changed
12919 || cursor_type_changed
12920 /* Don't use try_window_reusing_current_matrix in this case
12921 because it can have changed the buffer. */
12922 || !NILP (Vwindow_scroll_functions)
12923 || !just_this_one_p
12924 || MINI_WINDOW_P (w)
12925 || !(used_current_matrix_p
12926 = try_window_reusing_current_matrix (w)))
12927 try_window (window, startp, 0);
12928
12929 /* If new fonts have been loaded (due to fontsets), give up. We
12930 have to start a new redisplay since we need to re-adjust glyph
12931 matrices. */
12932 if (fonts_changed_p)
12933 goto need_larger_matrices;
12934
12935 /* If cursor did not appear assume that the middle of the window is
12936 in the first line of the window. Do it again with the next line.
12937 (Imagine a window of height 100, displaying two lines of height
12938 60. Moving back 50 from it->last_visible_y will end in the first
12939 line.) */
12940 if (w->cursor.vpos < 0)
12941 {
12942 if (!NILP (w->window_end_valid)
12943 && PT >= Z - XFASTINT (w->window_end_pos))
12944 {
12945 clear_glyph_matrix (w->desired_matrix);
12946 move_it_by_lines (&it, 1, 0);
12947 try_window (window, it.current.pos, 0);
12948 }
12949 else if (PT < IT_CHARPOS (it))
12950 {
12951 clear_glyph_matrix (w->desired_matrix);
12952 move_it_by_lines (&it, -1, 0);
12953 try_window (window, it.current.pos, 0);
12954 }
12955 else
12956 {
12957 /* Not much we can do about it. */
12958 }
12959 }
12960
12961 /* Consider the following case: Window starts at BEGV, there is
12962 invisible, intangible text at BEGV, so that display starts at
12963 some point START > BEGV. It can happen that we are called with
12964 PT somewhere between BEGV and START. Try to handle that case. */
12965 if (w->cursor.vpos < 0)
12966 {
12967 struct glyph_row *row = w->current_matrix->rows;
12968 if (row->mode_line_p)
12969 ++row;
12970 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12971 }
12972
12973 if (!cursor_row_fully_visible_p (w, 0, 0))
12974 {
12975 /* If vscroll is enabled, disable it and try again. */
12976 if (w->vscroll)
12977 {
12978 w->vscroll = 0;
12979 clear_glyph_matrix (w->desired_matrix);
12980 goto recenter;
12981 }
12982
12983 /* If centering point failed to make the whole line visible,
12984 put point at the top instead. That has to make the whole line
12985 visible, if it can be done. */
12986 if (centering_position == 0)
12987 goto done;
12988
12989 clear_glyph_matrix (w->desired_matrix);
12990 centering_position = 0;
12991 goto recenter;
12992 }
12993
12994 done:
12995
12996 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12997 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12998 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12999 ? Qt : Qnil);
13000
13001 /* Display the mode line, if we must. */
13002 if ((update_mode_line
13003 /* If window not full width, must redo its mode line
13004 if (a) the window to its side is being redone and
13005 (b) we do a frame-based redisplay. This is a consequence
13006 of how inverted lines are drawn in frame-based redisplay. */
13007 || (!just_this_one_p
13008 && !FRAME_WINDOW_P (f)
13009 && !WINDOW_FULL_WIDTH_P (w))
13010 /* Line number to display. */
13011 || INTEGERP (w->base_line_pos)
13012 /* Column number is displayed and different from the one displayed. */
13013 || (!NILP (w->column_number_displayed)
13014 && (XFASTINT (w->column_number_displayed)
13015 != (int) current_column ()))) /* iftc */
13016 /* This means that the window has a mode line. */
13017 && (WINDOW_WANTS_MODELINE_P (w)
13018 || WINDOW_WANTS_HEADER_LINE_P (w)))
13019 {
13020 display_mode_lines (w);
13021
13022 /* If mode line height has changed, arrange for a thorough
13023 immediate redisplay using the correct mode line height. */
13024 if (WINDOW_WANTS_MODELINE_P (w)
13025 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13026 {
13027 fonts_changed_p = 1;
13028 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13029 = DESIRED_MODE_LINE_HEIGHT (w);
13030 }
13031
13032 /* If top line height has changed, arrange for a thorough
13033 immediate redisplay using the correct mode line height. */
13034 if (WINDOW_WANTS_HEADER_LINE_P (w)
13035 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13036 {
13037 fonts_changed_p = 1;
13038 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13039 = DESIRED_HEADER_LINE_HEIGHT (w);
13040 }
13041
13042 if (fonts_changed_p)
13043 goto need_larger_matrices;
13044 }
13045
13046 if (!line_number_displayed
13047 && !BUFFERP (w->base_line_pos))
13048 {
13049 w->base_line_pos = Qnil;
13050 w->base_line_number = Qnil;
13051 }
13052
13053 finish_menu_bars:
13054
13055 /* When we reach a frame's selected window, redo the frame's menu bar. */
13056 if (update_mode_line
13057 && EQ (FRAME_SELECTED_WINDOW (f), window))
13058 {
13059 int redisplay_menu_p = 0;
13060 int redisplay_tool_bar_p = 0;
13061
13062 if (FRAME_WINDOW_P (f))
13063 {
13064 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13065 || defined (USE_GTK)
13066 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13067 #else
13068 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13069 #endif
13070 }
13071 else
13072 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13073
13074 if (redisplay_menu_p)
13075 display_menu_bar (w);
13076
13077 #ifdef HAVE_WINDOW_SYSTEM
13078 #ifdef USE_GTK
13079 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13080 #else
13081 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13082 && (FRAME_TOOL_BAR_LINES (f) > 0
13083 || auto_resize_tool_bars_p);
13084
13085 #endif
13086
13087 if (redisplay_tool_bar_p)
13088 redisplay_tool_bar (f);
13089 #endif
13090 }
13091
13092 #ifdef HAVE_WINDOW_SYSTEM
13093 if (FRAME_WINDOW_P (f)
13094 && update_window_fringes (w, (just_this_one_p
13095 || (!used_current_matrix_p && !overlay_arrow_seen)
13096 || w->pseudo_window_p)))
13097 {
13098 update_begin (f);
13099 BLOCK_INPUT;
13100 if (draw_window_fringes (w, 1))
13101 x_draw_vertical_border (w);
13102 UNBLOCK_INPUT;
13103 update_end (f);
13104 }
13105 #endif /* HAVE_WINDOW_SYSTEM */
13106
13107 /* We go to this label, with fonts_changed_p nonzero,
13108 if it is necessary to try again using larger glyph matrices.
13109 We have to redeem the scroll bar even in this case,
13110 because the loop in redisplay_internal expects that. */
13111 need_larger_matrices:
13112 ;
13113 finish_scroll_bars:
13114
13115 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13116 {
13117 /* Set the thumb's position and size. */
13118 set_vertical_scroll_bar (w);
13119
13120 /* Note that we actually used the scroll bar attached to this
13121 window, so it shouldn't be deleted at the end of redisplay. */
13122 redeem_scroll_bar_hook (w);
13123 }
13124
13125 /* Restore current_buffer and value of point in it. */
13126 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13127 set_buffer_internal_1 (old);
13128 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13129
13130 unbind_to (count, Qnil);
13131 }
13132
13133
13134 /* Build the complete desired matrix of WINDOW with a window start
13135 buffer position POS.
13136
13137 Value is 1 if successful. It is zero if fonts were loaded during
13138 redisplay which makes re-adjusting glyph matrices necessary, and -1
13139 if point would appear in the scroll margins.
13140 (We check that only if CHECK_MARGINS is nonzero. */
13141
13142 int
13143 try_window (window, pos, check_margins)
13144 Lisp_Object window;
13145 struct text_pos pos;
13146 int check_margins;
13147 {
13148 struct window *w = XWINDOW (window);
13149 struct it it;
13150 struct glyph_row *last_text_row = NULL;
13151
13152 /* Make POS the new window start. */
13153 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13154
13155 /* Mark cursor position as unknown. No overlay arrow seen. */
13156 w->cursor.vpos = -1;
13157 overlay_arrow_seen = 0;
13158
13159 /* Initialize iterator and info to start at POS. */
13160 start_display (&it, w, pos);
13161
13162 /* Display all lines of W. */
13163 while (it.current_y < it.last_visible_y)
13164 {
13165 if (display_line (&it))
13166 last_text_row = it.glyph_row - 1;
13167 if (fonts_changed_p)
13168 return 0;
13169 }
13170
13171 /* Don't let the cursor end in the scroll margins. */
13172 if (check_margins
13173 && !MINI_WINDOW_P (w))
13174 {
13175 int this_scroll_margin;
13176
13177 this_scroll_margin = max (0, scroll_margin);
13178 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13179 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13180
13181 if ((w->cursor.y < this_scroll_margin
13182 && CHARPOS (pos) > BEGV
13183 && IT_CHARPOS (it) < ZV)
13184 /* rms: considering make_cursor_line_fully_visible_p here
13185 seems to give wrong results. We don't want to recenter
13186 when the last line is partly visible, we want to allow
13187 that case to be handled in the usual way. */
13188 || (w->cursor.y + 1) > it.last_visible_y)
13189 {
13190 w->cursor.vpos = -1;
13191 clear_glyph_matrix (w->desired_matrix);
13192 return -1;
13193 }
13194 }
13195
13196 /* If bottom moved off end of frame, change mode line percentage. */
13197 if (XFASTINT (w->window_end_pos) <= 0
13198 && Z != IT_CHARPOS (it))
13199 w->update_mode_line = Qt;
13200
13201 /* Set window_end_pos to the offset of the last character displayed
13202 on the window from the end of current_buffer. Set
13203 window_end_vpos to its row number. */
13204 if (last_text_row)
13205 {
13206 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13207 w->window_end_bytepos
13208 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13209 w->window_end_pos
13210 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13211 w->window_end_vpos
13212 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13213 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13214 ->displays_text_p);
13215 }
13216 else
13217 {
13218 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13219 w->window_end_pos = make_number (Z - ZV);
13220 w->window_end_vpos = make_number (0);
13221 }
13222
13223 /* But that is not valid info until redisplay finishes. */
13224 w->window_end_valid = Qnil;
13225 return 1;
13226 }
13227
13228
13229 \f
13230 /************************************************************************
13231 Window redisplay reusing current matrix when buffer has not changed
13232 ************************************************************************/
13233
13234 /* Try redisplay of window W showing an unchanged buffer with a
13235 different window start than the last time it was displayed by
13236 reusing its current matrix. Value is non-zero if successful.
13237 W->start is the new window start. */
13238
13239 static int
13240 try_window_reusing_current_matrix (w)
13241 struct window *w;
13242 {
13243 struct frame *f = XFRAME (w->frame);
13244 struct glyph_row *row, *bottom_row;
13245 struct it it;
13246 struct run run;
13247 struct text_pos start, new_start;
13248 int nrows_scrolled, i;
13249 struct glyph_row *last_text_row;
13250 struct glyph_row *last_reused_text_row;
13251 struct glyph_row *start_row;
13252 int start_vpos, min_y, max_y;
13253
13254 #if GLYPH_DEBUG
13255 if (inhibit_try_window_reusing)
13256 return 0;
13257 #endif
13258
13259 if (/* This function doesn't handle terminal frames. */
13260 !FRAME_WINDOW_P (f)
13261 /* Don't try to reuse the display if windows have been split
13262 or such. */
13263 || windows_or_buffers_changed
13264 || cursor_type_changed)
13265 return 0;
13266
13267 /* Can't do this if region may have changed. */
13268 if ((!NILP (Vtransient_mark_mode)
13269 && !NILP (current_buffer->mark_active))
13270 || !NILP (w->region_showing)
13271 || !NILP (Vshow_trailing_whitespace))
13272 return 0;
13273
13274 /* If top-line visibility has changed, give up. */
13275 if (WINDOW_WANTS_HEADER_LINE_P (w)
13276 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13277 return 0;
13278
13279 /* Give up if old or new display is scrolled vertically. We could
13280 make this function handle this, but right now it doesn't. */
13281 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13282 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13283 return 0;
13284
13285 /* The variable new_start now holds the new window start. The old
13286 start `start' can be determined from the current matrix. */
13287 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13288 start = start_row->start.pos;
13289 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13290
13291 /* Clear the desired matrix for the display below. */
13292 clear_glyph_matrix (w->desired_matrix);
13293
13294 if (CHARPOS (new_start) <= CHARPOS (start))
13295 {
13296 int first_row_y;
13297
13298 /* Don't use this method if the display starts with an ellipsis
13299 displayed for invisible text. It's not easy to handle that case
13300 below, and it's certainly not worth the effort since this is
13301 not a frequent case. */
13302 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13303 return 0;
13304
13305 IF_DEBUG (debug_method_add (w, "twu1"));
13306
13307 /* Display up to a row that can be reused. The variable
13308 last_text_row is set to the last row displayed that displays
13309 text. Note that it.vpos == 0 if or if not there is a
13310 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13311 start_display (&it, w, new_start);
13312 first_row_y = it.current_y;
13313 w->cursor.vpos = -1;
13314 last_text_row = last_reused_text_row = NULL;
13315
13316 while (it.current_y < it.last_visible_y
13317 && !fonts_changed_p)
13318 {
13319 /* If we have reached into the characters in the START row,
13320 that means the line boundaries have changed. So we
13321 can't start copying with the row START. Maybe it will
13322 work to start copying with the following row. */
13323 while (IT_CHARPOS (it) > CHARPOS (start))
13324 {
13325 /* Advance to the next row as the "start". */
13326 start_row++;
13327 start = start_row->start.pos;
13328 /* If there are no more rows to try, or just one, give up. */
13329 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13330 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13331 || CHARPOS (start) == ZV)
13332 {
13333 clear_glyph_matrix (w->desired_matrix);
13334 return 0;
13335 }
13336
13337 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13338 }
13339 /* If we have reached alignment,
13340 we can copy the rest of the rows. */
13341 if (IT_CHARPOS (it) == CHARPOS (start))
13342 break;
13343
13344 if (display_line (&it))
13345 last_text_row = it.glyph_row - 1;
13346 }
13347
13348 /* A value of current_y < last_visible_y means that we stopped
13349 at the previous window start, which in turn means that we
13350 have at least one reusable row. */
13351 if (it.current_y < it.last_visible_y)
13352 {
13353 /* IT.vpos always starts from 0; it counts text lines. */
13354 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13355
13356 /* Find PT if not already found in the lines displayed. */
13357 if (w->cursor.vpos < 0)
13358 {
13359 int dy = it.current_y - start_row->y;
13360
13361 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13362 row = row_containing_pos (w, PT, row, NULL, dy);
13363 if (row)
13364 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13365 dy, nrows_scrolled);
13366 else
13367 {
13368 clear_glyph_matrix (w->desired_matrix);
13369 return 0;
13370 }
13371 }
13372
13373 /* Scroll the display. Do it before the current matrix is
13374 changed. The problem here is that update has not yet
13375 run, i.e. part of the current matrix is not up to date.
13376 scroll_run_hook will clear the cursor, and use the
13377 current matrix to get the height of the row the cursor is
13378 in. */
13379 run.current_y = start_row->y;
13380 run.desired_y = it.current_y;
13381 run.height = it.last_visible_y - it.current_y;
13382
13383 if (run.height > 0 && run.current_y != run.desired_y)
13384 {
13385 update_begin (f);
13386 rif->update_window_begin_hook (w);
13387 rif->clear_window_mouse_face (w);
13388 rif->scroll_run_hook (w, &run);
13389 rif->update_window_end_hook (w, 0, 0);
13390 update_end (f);
13391 }
13392
13393 /* Shift current matrix down by nrows_scrolled lines. */
13394 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13395 rotate_matrix (w->current_matrix,
13396 start_vpos,
13397 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13398 nrows_scrolled);
13399
13400 /* Disable lines that must be updated. */
13401 for (i = 0; i < it.vpos; ++i)
13402 (start_row + i)->enabled_p = 0;
13403
13404 /* Re-compute Y positions. */
13405 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13406 max_y = it.last_visible_y;
13407 for (row = start_row + nrows_scrolled;
13408 row < bottom_row;
13409 ++row)
13410 {
13411 row->y = it.current_y;
13412 row->visible_height = row->height;
13413
13414 if (row->y < min_y)
13415 row->visible_height -= min_y - row->y;
13416 if (row->y + row->height > max_y)
13417 row->visible_height -= row->y + row->height - max_y;
13418 row->redraw_fringe_bitmaps_p = 1;
13419
13420 it.current_y += row->height;
13421
13422 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13423 last_reused_text_row = row;
13424 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13425 break;
13426 }
13427
13428 /* Disable lines in the current matrix which are now
13429 below the window. */
13430 for (++row; row < bottom_row; ++row)
13431 row->enabled_p = row->mode_line_p = 0;
13432 }
13433
13434 /* Update window_end_pos etc.; last_reused_text_row is the last
13435 reused row from the current matrix containing text, if any.
13436 The value of last_text_row is the last displayed line
13437 containing text. */
13438 if (last_reused_text_row)
13439 {
13440 w->window_end_bytepos
13441 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13442 w->window_end_pos
13443 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13444 w->window_end_vpos
13445 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13446 w->current_matrix));
13447 }
13448 else if (last_text_row)
13449 {
13450 w->window_end_bytepos
13451 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13452 w->window_end_pos
13453 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13454 w->window_end_vpos
13455 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13456 }
13457 else
13458 {
13459 /* This window must be completely empty. */
13460 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13461 w->window_end_pos = make_number (Z - ZV);
13462 w->window_end_vpos = make_number (0);
13463 }
13464 w->window_end_valid = Qnil;
13465
13466 /* Update hint: don't try scrolling again in update_window. */
13467 w->desired_matrix->no_scrolling_p = 1;
13468
13469 #if GLYPH_DEBUG
13470 debug_method_add (w, "try_window_reusing_current_matrix 1");
13471 #endif
13472 return 1;
13473 }
13474 else if (CHARPOS (new_start) > CHARPOS (start))
13475 {
13476 struct glyph_row *pt_row, *row;
13477 struct glyph_row *first_reusable_row;
13478 struct glyph_row *first_row_to_display;
13479 int dy;
13480 int yb = window_text_bottom_y (w);
13481
13482 /* Find the row starting at new_start, if there is one. Don't
13483 reuse a partially visible line at the end. */
13484 first_reusable_row = start_row;
13485 while (first_reusable_row->enabled_p
13486 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13487 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13488 < CHARPOS (new_start)))
13489 ++first_reusable_row;
13490
13491 /* Give up if there is no row to reuse. */
13492 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13493 || !first_reusable_row->enabled_p
13494 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13495 != CHARPOS (new_start)))
13496 return 0;
13497
13498 /* We can reuse fully visible rows beginning with
13499 first_reusable_row to the end of the window. Set
13500 first_row_to_display to the first row that cannot be reused.
13501 Set pt_row to the row containing point, if there is any. */
13502 pt_row = NULL;
13503 for (first_row_to_display = first_reusable_row;
13504 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13505 ++first_row_to_display)
13506 {
13507 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13508 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13509 pt_row = first_row_to_display;
13510 }
13511
13512 /* Start displaying at the start of first_row_to_display. */
13513 xassert (first_row_to_display->y < yb);
13514 init_to_row_start (&it, w, first_row_to_display);
13515
13516 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13517 - start_vpos);
13518 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13519 - nrows_scrolled);
13520 it.current_y = (first_row_to_display->y - first_reusable_row->y
13521 + WINDOW_HEADER_LINE_HEIGHT (w));
13522
13523 /* Display lines beginning with first_row_to_display in the
13524 desired matrix. Set last_text_row to the last row displayed
13525 that displays text. */
13526 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13527 if (pt_row == NULL)
13528 w->cursor.vpos = -1;
13529 last_text_row = NULL;
13530 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13531 if (display_line (&it))
13532 last_text_row = it.glyph_row - 1;
13533
13534 /* Give up If point isn't in a row displayed or reused. */
13535 if (w->cursor.vpos < 0)
13536 {
13537 clear_glyph_matrix (w->desired_matrix);
13538 return 0;
13539 }
13540
13541 /* If point is in a reused row, adjust y and vpos of the cursor
13542 position. */
13543 if (pt_row)
13544 {
13545 w->cursor.vpos -= nrows_scrolled;
13546 w->cursor.y -= first_reusable_row->y - start_row->y;
13547 }
13548
13549 /* Scroll the display. */
13550 run.current_y = first_reusable_row->y;
13551 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13552 run.height = it.last_visible_y - run.current_y;
13553 dy = run.current_y - run.desired_y;
13554
13555 if (run.height)
13556 {
13557 update_begin (f);
13558 rif->update_window_begin_hook (w);
13559 rif->clear_window_mouse_face (w);
13560 rif->scroll_run_hook (w, &run);
13561 rif->update_window_end_hook (w, 0, 0);
13562 update_end (f);
13563 }
13564
13565 /* Adjust Y positions of reused rows. */
13566 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13567 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13568 max_y = it.last_visible_y;
13569 for (row = first_reusable_row; row < first_row_to_display; ++row)
13570 {
13571 row->y -= dy;
13572 row->visible_height = row->height;
13573 if (row->y < min_y)
13574 row->visible_height -= min_y - row->y;
13575 if (row->y + row->height > max_y)
13576 row->visible_height -= row->y + row->height - max_y;
13577 row->redraw_fringe_bitmaps_p = 1;
13578 }
13579
13580 /* Scroll the current matrix. */
13581 xassert (nrows_scrolled > 0);
13582 rotate_matrix (w->current_matrix,
13583 start_vpos,
13584 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13585 -nrows_scrolled);
13586
13587 /* Disable rows not reused. */
13588 for (row -= nrows_scrolled; row < bottom_row; ++row)
13589 row->enabled_p = 0;
13590
13591 /* Point may have moved to a different line, so we cannot assume that
13592 the previous cursor position is valid; locate the correct row. */
13593 if (pt_row)
13594 {
13595 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13596 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13597 row++)
13598 {
13599 w->cursor.vpos++;
13600 w->cursor.y = row->y;
13601 }
13602 if (row < bottom_row)
13603 {
13604 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13605 while (glyph->charpos < PT)
13606 {
13607 w->cursor.hpos++;
13608 w->cursor.x += glyph->pixel_width;
13609 glyph++;
13610 }
13611 }
13612 }
13613
13614 /* Adjust window end. A null value of last_text_row means that
13615 the window end is in reused rows which in turn means that
13616 only its vpos can have changed. */
13617 if (last_text_row)
13618 {
13619 w->window_end_bytepos
13620 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13621 w->window_end_pos
13622 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13623 w->window_end_vpos
13624 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13625 }
13626 else
13627 {
13628 w->window_end_vpos
13629 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13630 }
13631
13632 w->window_end_valid = Qnil;
13633 w->desired_matrix->no_scrolling_p = 1;
13634
13635 #if GLYPH_DEBUG
13636 debug_method_add (w, "try_window_reusing_current_matrix 2");
13637 #endif
13638 return 1;
13639 }
13640
13641 return 0;
13642 }
13643
13644
13645 \f
13646 /************************************************************************
13647 Window redisplay reusing current matrix when buffer has changed
13648 ************************************************************************/
13649
13650 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13651 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13652 int *, int *));
13653 static struct glyph_row *
13654 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13655 struct glyph_row *));
13656
13657
13658 /* Return the last row in MATRIX displaying text. If row START is
13659 non-null, start searching with that row. IT gives the dimensions
13660 of the display. Value is null if matrix is empty; otherwise it is
13661 a pointer to the row found. */
13662
13663 static struct glyph_row *
13664 find_last_row_displaying_text (matrix, it, start)
13665 struct glyph_matrix *matrix;
13666 struct it *it;
13667 struct glyph_row *start;
13668 {
13669 struct glyph_row *row, *row_found;
13670
13671 /* Set row_found to the last row in IT->w's current matrix
13672 displaying text. The loop looks funny but think of partially
13673 visible lines. */
13674 row_found = NULL;
13675 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13676 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13677 {
13678 xassert (row->enabled_p);
13679 row_found = row;
13680 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13681 break;
13682 ++row;
13683 }
13684
13685 return row_found;
13686 }
13687
13688
13689 /* Return the last row in the current matrix of W that is not affected
13690 by changes at the start of current_buffer that occurred since W's
13691 current matrix was built. Value is null if no such row exists.
13692
13693 BEG_UNCHANGED us the number of characters unchanged at the start of
13694 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13695 first changed character in current_buffer. Characters at positions <
13696 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13697 when the current matrix was built. */
13698
13699 static struct glyph_row *
13700 find_last_unchanged_at_beg_row (w)
13701 struct window *w;
13702 {
13703 int first_changed_pos = BEG + BEG_UNCHANGED;
13704 struct glyph_row *row;
13705 struct glyph_row *row_found = NULL;
13706 int yb = window_text_bottom_y (w);
13707
13708 /* Find the last row displaying unchanged text. */
13709 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13710 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13711 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13712 {
13713 if (/* If row ends before first_changed_pos, it is unchanged,
13714 except in some case. */
13715 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13716 /* When row ends in ZV and we write at ZV it is not
13717 unchanged. */
13718 && !row->ends_at_zv_p
13719 /* When first_changed_pos is the end of a continued line,
13720 row is not unchanged because it may be no longer
13721 continued. */
13722 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13723 && (row->continued_p
13724 || row->exact_window_width_line_p)))
13725 row_found = row;
13726
13727 /* Stop if last visible row. */
13728 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13729 break;
13730
13731 ++row;
13732 }
13733
13734 return row_found;
13735 }
13736
13737
13738 /* Find the first glyph row in the current matrix of W that is not
13739 affected by changes at the end of current_buffer since the
13740 time W's current matrix was built.
13741
13742 Return in *DELTA the number of chars by which buffer positions in
13743 unchanged text at the end of current_buffer must be adjusted.
13744
13745 Return in *DELTA_BYTES the corresponding number of bytes.
13746
13747 Value is null if no such row exists, i.e. all rows are affected by
13748 changes. */
13749
13750 static struct glyph_row *
13751 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13752 struct window *w;
13753 int *delta, *delta_bytes;
13754 {
13755 struct glyph_row *row;
13756 struct glyph_row *row_found = NULL;
13757
13758 *delta = *delta_bytes = 0;
13759
13760 /* Display must not have been paused, otherwise the current matrix
13761 is not up to date. */
13762 if (NILP (w->window_end_valid))
13763 abort ();
13764
13765 /* A value of window_end_pos >= END_UNCHANGED means that the window
13766 end is in the range of changed text. If so, there is no
13767 unchanged row at the end of W's current matrix. */
13768 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13769 return NULL;
13770
13771 /* Set row to the last row in W's current matrix displaying text. */
13772 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13773
13774 /* If matrix is entirely empty, no unchanged row exists. */
13775 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13776 {
13777 /* The value of row is the last glyph row in the matrix having a
13778 meaningful buffer position in it. The end position of row
13779 corresponds to window_end_pos. This allows us to translate
13780 buffer positions in the current matrix to current buffer
13781 positions for characters not in changed text. */
13782 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13783 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13784 int last_unchanged_pos, last_unchanged_pos_old;
13785 struct glyph_row *first_text_row
13786 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13787
13788 *delta = Z - Z_old;
13789 *delta_bytes = Z_BYTE - Z_BYTE_old;
13790
13791 /* Set last_unchanged_pos to the buffer position of the last
13792 character in the buffer that has not been changed. Z is the
13793 index + 1 of the last character in current_buffer, i.e. by
13794 subtracting END_UNCHANGED we get the index of the last
13795 unchanged character, and we have to add BEG to get its buffer
13796 position. */
13797 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13798 last_unchanged_pos_old = last_unchanged_pos - *delta;
13799
13800 /* Search backward from ROW for a row displaying a line that
13801 starts at a minimum position >= last_unchanged_pos_old. */
13802 for (; row > first_text_row; --row)
13803 {
13804 /* This used to abort, but it can happen.
13805 It is ok to just stop the search instead here. KFS. */
13806 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13807 break;
13808
13809 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13810 row_found = row;
13811 }
13812 }
13813
13814 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13815 abort ();
13816
13817 return row_found;
13818 }
13819
13820
13821 /* Make sure that glyph rows in the current matrix of window W
13822 reference the same glyph memory as corresponding rows in the
13823 frame's frame matrix. This function is called after scrolling W's
13824 current matrix on a terminal frame in try_window_id and
13825 try_window_reusing_current_matrix. */
13826
13827 static void
13828 sync_frame_with_window_matrix_rows (w)
13829 struct window *w;
13830 {
13831 struct frame *f = XFRAME (w->frame);
13832 struct glyph_row *window_row, *window_row_end, *frame_row;
13833
13834 /* Preconditions: W must be a leaf window and full-width. Its frame
13835 must have a frame matrix. */
13836 xassert (NILP (w->hchild) && NILP (w->vchild));
13837 xassert (WINDOW_FULL_WIDTH_P (w));
13838 xassert (!FRAME_WINDOW_P (f));
13839
13840 /* If W is a full-width window, glyph pointers in W's current matrix
13841 have, by definition, to be the same as glyph pointers in the
13842 corresponding frame matrix. Note that frame matrices have no
13843 marginal areas (see build_frame_matrix). */
13844 window_row = w->current_matrix->rows;
13845 window_row_end = window_row + w->current_matrix->nrows;
13846 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13847 while (window_row < window_row_end)
13848 {
13849 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13850 struct glyph *end = window_row->glyphs[LAST_AREA];
13851
13852 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13853 frame_row->glyphs[TEXT_AREA] = start;
13854 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13855 frame_row->glyphs[LAST_AREA] = end;
13856
13857 /* Disable frame rows whose corresponding window rows have
13858 been disabled in try_window_id. */
13859 if (!window_row->enabled_p)
13860 frame_row->enabled_p = 0;
13861
13862 ++window_row, ++frame_row;
13863 }
13864 }
13865
13866
13867 /* Find the glyph row in window W containing CHARPOS. Consider all
13868 rows between START and END (not inclusive). END null means search
13869 all rows to the end of the display area of W. Value is the row
13870 containing CHARPOS or null. */
13871
13872 struct glyph_row *
13873 row_containing_pos (w, charpos, start, end, dy)
13874 struct window *w;
13875 int charpos;
13876 struct glyph_row *start, *end;
13877 int dy;
13878 {
13879 struct glyph_row *row = start;
13880 int last_y;
13881
13882 /* If we happen to start on a header-line, skip that. */
13883 if (row->mode_line_p)
13884 ++row;
13885
13886 if ((end && row >= end) || !row->enabled_p)
13887 return NULL;
13888
13889 last_y = window_text_bottom_y (w) - dy;
13890
13891 while (1)
13892 {
13893 /* Give up if we have gone too far. */
13894 if (end && row >= end)
13895 return NULL;
13896 /* This formerly returned if they were equal.
13897 I think that both quantities are of a "last plus one" type;
13898 if so, when they are equal, the row is within the screen. -- rms. */
13899 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13900 return NULL;
13901
13902 /* If it is in this row, return this row. */
13903 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13904 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13905 /* The end position of a row equals the start
13906 position of the next row. If CHARPOS is there, we
13907 would rather display it in the next line, except
13908 when this line ends in ZV. */
13909 && !row->ends_at_zv_p
13910 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13911 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13912 return row;
13913 ++row;
13914 }
13915 }
13916
13917
13918 /* Try to redisplay window W by reusing its existing display. W's
13919 current matrix must be up to date when this function is called,
13920 i.e. window_end_valid must not be nil.
13921
13922 Value is
13923
13924 1 if display has been updated
13925 0 if otherwise unsuccessful
13926 -1 if redisplay with same window start is known not to succeed
13927
13928 The following steps are performed:
13929
13930 1. Find the last row in the current matrix of W that is not
13931 affected by changes at the start of current_buffer. If no such row
13932 is found, give up.
13933
13934 2. Find the first row in W's current matrix that is not affected by
13935 changes at the end of current_buffer. Maybe there is no such row.
13936
13937 3. Display lines beginning with the row + 1 found in step 1 to the
13938 row found in step 2 or, if step 2 didn't find a row, to the end of
13939 the window.
13940
13941 4. If cursor is not known to appear on the window, give up.
13942
13943 5. If display stopped at the row found in step 2, scroll the
13944 display and current matrix as needed.
13945
13946 6. Maybe display some lines at the end of W, if we must. This can
13947 happen under various circumstances, like a partially visible line
13948 becoming fully visible, or because newly displayed lines are displayed
13949 in smaller font sizes.
13950
13951 7. Update W's window end information. */
13952
13953 static int
13954 try_window_id (w)
13955 struct window *w;
13956 {
13957 struct frame *f = XFRAME (w->frame);
13958 struct glyph_matrix *current_matrix = w->current_matrix;
13959 struct glyph_matrix *desired_matrix = w->desired_matrix;
13960 struct glyph_row *last_unchanged_at_beg_row;
13961 struct glyph_row *first_unchanged_at_end_row;
13962 struct glyph_row *row;
13963 struct glyph_row *bottom_row;
13964 int bottom_vpos;
13965 struct it it;
13966 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13967 struct text_pos start_pos;
13968 struct run run;
13969 int first_unchanged_at_end_vpos = 0;
13970 struct glyph_row *last_text_row, *last_text_row_at_end;
13971 struct text_pos start;
13972 int first_changed_charpos, last_changed_charpos;
13973
13974 #if GLYPH_DEBUG
13975 if (inhibit_try_window_id)
13976 return 0;
13977 #endif
13978
13979 /* This is handy for debugging. */
13980 #if 0
13981 #define GIVE_UP(X) \
13982 do { \
13983 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13984 return 0; \
13985 } while (0)
13986 #else
13987 #define GIVE_UP(X) return 0
13988 #endif
13989
13990 SET_TEXT_POS_FROM_MARKER (start, w->start);
13991
13992 /* Don't use this for mini-windows because these can show
13993 messages and mini-buffers, and we don't handle that here. */
13994 if (MINI_WINDOW_P (w))
13995 GIVE_UP (1);
13996
13997 /* This flag is used to prevent redisplay optimizations. */
13998 if (windows_or_buffers_changed || cursor_type_changed)
13999 GIVE_UP (2);
14000
14001 /* Verify that narrowing has not changed.
14002 Also verify that we were not told to prevent redisplay optimizations.
14003 It would be nice to further
14004 reduce the number of cases where this prevents try_window_id. */
14005 if (current_buffer->clip_changed
14006 || current_buffer->prevent_redisplay_optimizations_p)
14007 GIVE_UP (3);
14008
14009 /* Window must either use window-based redisplay or be full width. */
14010 if (!FRAME_WINDOW_P (f)
14011 && (!line_ins_del_ok
14012 || !WINDOW_FULL_WIDTH_P (w)))
14013 GIVE_UP (4);
14014
14015 /* Give up if point is not known NOT to appear in W. */
14016 if (PT < CHARPOS (start))
14017 GIVE_UP (5);
14018
14019 /* Another way to prevent redisplay optimizations. */
14020 if (XFASTINT (w->last_modified) == 0)
14021 GIVE_UP (6);
14022
14023 /* Verify that window is not hscrolled. */
14024 if (XFASTINT (w->hscroll) != 0)
14025 GIVE_UP (7);
14026
14027 /* Verify that display wasn't paused. */
14028 if (NILP (w->window_end_valid))
14029 GIVE_UP (8);
14030
14031 /* Can't use this if highlighting a region because a cursor movement
14032 will do more than just set the cursor. */
14033 if (!NILP (Vtransient_mark_mode)
14034 && !NILP (current_buffer->mark_active))
14035 GIVE_UP (9);
14036
14037 /* Likewise if highlighting trailing whitespace. */
14038 if (!NILP (Vshow_trailing_whitespace))
14039 GIVE_UP (11);
14040
14041 /* Likewise if showing a region. */
14042 if (!NILP (w->region_showing))
14043 GIVE_UP (10);
14044
14045 /* Can use this if overlay arrow position and or string have changed. */
14046 if (overlay_arrows_changed_p ())
14047 GIVE_UP (12);
14048
14049
14050 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14051 only if buffer has really changed. The reason is that the gap is
14052 initially at Z for freshly visited files. The code below would
14053 set end_unchanged to 0 in that case. */
14054 if (MODIFF > SAVE_MODIFF
14055 /* This seems to happen sometimes after saving a buffer. */
14056 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14057 {
14058 if (GPT - BEG < BEG_UNCHANGED)
14059 BEG_UNCHANGED = GPT - BEG;
14060 if (Z - GPT < END_UNCHANGED)
14061 END_UNCHANGED = Z - GPT;
14062 }
14063
14064 /* The position of the first and last character that has been changed. */
14065 first_changed_charpos = BEG + BEG_UNCHANGED;
14066 last_changed_charpos = Z - END_UNCHANGED;
14067
14068 /* If window starts after a line end, and the last change is in
14069 front of that newline, then changes don't affect the display.
14070 This case happens with stealth-fontification. Note that although
14071 the display is unchanged, glyph positions in the matrix have to
14072 be adjusted, of course. */
14073 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14074 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14075 && ((last_changed_charpos < CHARPOS (start)
14076 && CHARPOS (start) == BEGV)
14077 || (last_changed_charpos < CHARPOS (start) - 1
14078 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14079 {
14080 int Z_old, delta, Z_BYTE_old, delta_bytes;
14081 struct glyph_row *r0;
14082
14083 /* Compute how many chars/bytes have been added to or removed
14084 from the buffer. */
14085 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14086 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14087 delta = Z - Z_old;
14088 delta_bytes = Z_BYTE - Z_BYTE_old;
14089
14090 /* Give up if PT is not in the window. Note that it already has
14091 been checked at the start of try_window_id that PT is not in
14092 front of the window start. */
14093 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14094 GIVE_UP (13);
14095
14096 /* If window start is unchanged, we can reuse the whole matrix
14097 as is, after adjusting glyph positions. No need to compute
14098 the window end again, since its offset from Z hasn't changed. */
14099 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14100 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14101 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14102 /* PT must not be in a partially visible line. */
14103 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14104 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14105 {
14106 /* Adjust positions in the glyph matrix. */
14107 if (delta || delta_bytes)
14108 {
14109 struct glyph_row *r1
14110 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14111 increment_matrix_positions (w->current_matrix,
14112 MATRIX_ROW_VPOS (r0, current_matrix),
14113 MATRIX_ROW_VPOS (r1, current_matrix),
14114 delta, delta_bytes);
14115 }
14116
14117 /* Set the cursor. */
14118 row = row_containing_pos (w, PT, r0, NULL, 0);
14119 if (row)
14120 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14121 else
14122 abort ();
14123 return 1;
14124 }
14125 }
14126
14127 /* Handle the case that changes are all below what is displayed in
14128 the window, and that PT is in the window. This shortcut cannot
14129 be taken if ZV is visible in the window, and text has been added
14130 there that is visible in the window. */
14131 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14132 /* ZV is not visible in the window, or there are no
14133 changes at ZV, actually. */
14134 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14135 || first_changed_charpos == last_changed_charpos))
14136 {
14137 struct glyph_row *r0;
14138
14139 /* Give up if PT is not in the window. Note that it already has
14140 been checked at the start of try_window_id that PT is not in
14141 front of the window start. */
14142 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14143 GIVE_UP (14);
14144
14145 /* If window start is unchanged, we can reuse the whole matrix
14146 as is, without changing glyph positions since no text has
14147 been added/removed in front of the window end. */
14148 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14149 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14150 /* PT must not be in a partially visible line. */
14151 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14152 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14153 {
14154 /* We have to compute the window end anew since text
14155 can have been added/removed after it. */
14156 w->window_end_pos
14157 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14158 w->window_end_bytepos
14159 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14160
14161 /* Set the cursor. */
14162 row = row_containing_pos (w, PT, r0, NULL, 0);
14163 if (row)
14164 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14165 else
14166 abort ();
14167 return 2;
14168 }
14169 }
14170
14171 /* Give up if window start is in the changed area.
14172
14173 The condition used to read
14174
14175 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14176
14177 but why that was tested escapes me at the moment. */
14178 if (CHARPOS (start) >= first_changed_charpos
14179 && CHARPOS (start) <= last_changed_charpos)
14180 GIVE_UP (15);
14181
14182 /* Check that window start agrees with the start of the first glyph
14183 row in its current matrix. Check this after we know the window
14184 start is not in changed text, otherwise positions would not be
14185 comparable. */
14186 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14187 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14188 GIVE_UP (16);
14189
14190 /* Give up if the window ends in strings. Overlay strings
14191 at the end are difficult to handle, so don't try. */
14192 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14193 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14194 GIVE_UP (20);
14195
14196 /* Compute the position at which we have to start displaying new
14197 lines. Some of the lines at the top of the window might be
14198 reusable because they are not displaying changed text. Find the
14199 last row in W's current matrix not affected by changes at the
14200 start of current_buffer. Value is null if changes start in the
14201 first line of window. */
14202 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14203 if (last_unchanged_at_beg_row)
14204 {
14205 /* Avoid starting to display in the moddle of a character, a TAB
14206 for instance. This is easier than to set up the iterator
14207 exactly, and it's not a frequent case, so the additional
14208 effort wouldn't really pay off. */
14209 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14210 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14211 && last_unchanged_at_beg_row > w->current_matrix->rows)
14212 --last_unchanged_at_beg_row;
14213
14214 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14215 GIVE_UP (17);
14216
14217 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14218 GIVE_UP (18);
14219 start_pos = it.current.pos;
14220
14221 /* Start displaying new lines in the desired matrix at the same
14222 vpos we would use in the current matrix, i.e. below
14223 last_unchanged_at_beg_row. */
14224 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14225 current_matrix);
14226 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14227 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14228
14229 xassert (it.hpos == 0 && it.current_x == 0);
14230 }
14231 else
14232 {
14233 /* There are no reusable lines at the start of the window.
14234 Start displaying in the first text line. */
14235 start_display (&it, w, start);
14236 it.vpos = it.first_vpos;
14237 start_pos = it.current.pos;
14238 }
14239
14240 /* Find the first row that is not affected by changes at the end of
14241 the buffer. Value will be null if there is no unchanged row, in
14242 which case we must redisplay to the end of the window. delta
14243 will be set to the value by which buffer positions beginning with
14244 first_unchanged_at_end_row have to be adjusted due to text
14245 changes. */
14246 first_unchanged_at_end_row
14247 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14248 IF_DEBUG (debug_delta = delta);
14249 IF_DEBUG (debug_delta_bytes = delta_bytes);
14250
14251 /* Set stop_pos to the buffer position up to which we will have to
14252 display new lines. If first_unchanged_at_end_row != NULL, this
14253 is the buffer position of the start of the line displayed in that
14254 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14255 that we don't stop at a buffer position. */
14256 stop_pos = 0;
14257 if (first_unchanged_at_end_row)
14258 {
14259 xassert (last_unchanged_at_beg_row == NULL
14260 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14261
14262 /* If this is a continuation line, move forward to the next one
14263 that isn't. Changes in lines above affect this line.
14264 Caution: this may move first_unchanged_at_end_row to a row
14265 not displaying text. */
14266 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14267 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14268 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14269 < it.last_visible_y))
14270 ++first_unchanged_at_end_row;
14271
14272 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14273 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14274 >= it.last_visible_y))
14275 first_unchanged_at_end_row = NULL;
14276 else
14277 {
14278 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14279 + delta);
14280 first_unchanged_at_end_vpos
14281 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14282 xassert (stop_pos >= Z - END_UNCHANGED);
14283 }
14284 }
14285 else if (last_unchanged_at_beg_row == NULL)
14286 GIVE_UP (19);
14287
14288
14289 #if GLYPH_DEBUG
14290
14291 /* Either there is no unchanged row at the end, or the one we have
14292 now displays text. This is a necessary condition for the window
14293 end pos calculation at the end of this function. */
14294 xassert (first_unchanged_at_end_row == NULL
14295 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14296
14297 debug_last_unchanged_at_beg_vpos
14298 = (last_unchanged_at_beg_row
14299 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14300 : -1);
14301 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14302
14303 #endif /* GLYPH_DEBUG != 0 */
14304
14305
14306 /* Display new lines. Set last_text_row to the last new line
14307 displayed which has text on it, i.e. might end up as being the
14308 line where the window_end_vpos is. */
14309 w->cursor.vpos = -1;
14310 last_text_row = NULL;
14311 overlay_arrow_seen = 0;
14312 while (it.current_y < it.last_visible_y
14313 && !fonts_changed_p
14314 && (first_unchanged_at_end_row == NULL
14315 || IT_CHARPOS (it) < stop_pos))
14316 {
14317 if (display_line (&it))
14318 last_text_row = it.glyph_row - 1;
14319 }
14320
14321 if (fonts_changed_p)
14322 return -1;
14323
14324
14325 /* Compute differences in buffer positions, y-positions etc. for
14326 lines reused at the bottom of the window. Compute what we can
14327 scroll. */
14328 if (first_unchanged_at_end_row
14329 /* No lines reused because we displayed everything up to the
14330 bottom of the window. */
14331 && it.current_y < it.last_visible_y)
14332 {
14333 dvpos = (it.vpos
14334 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14335 current_matrix));
14336 dy = it.current_y - first_unchanged_at_end_row->y;
14337 run.current_y = first_unchanged_at_end_row->y;
14338 run.desired_y = run.current_y + dy;
14339 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14340 }
14341 else
14342 {
14343 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14344 first_unchanged_at_end_row = NULL;
14345 }
14346 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14347
14348
14349 /* Find the cursor if not already found. We have to decide whether
14350 PT will appear on this window (it sometimes doesn't, but this is
14351 not a very frequent case.) This decision has to be made before
14352 the current matrix is altered. A value of cursor.vpos < 0 means
14353 that PT is either in one of the lines beginning at
14354 first_unchanged_at_end_row or below the window. Don't care for
14355 lines that might be displayed later at the window end; as
14356 mentioned, this is not a frequent case. */
14357 if (w->cursor.vpos < 0)
14358 {
14359 /* Cursor in unchanged rows at the top? */
14360 if (PT < CHARPOS (start_pos)
14361 && last_unchanged_at_beg_row)
14362 {
14363 row = row_containing_pos (w, PT,
14364 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14365 last_unchanged_at_beg_row + 1, 0);
14366 if (row)
14367 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14368 }
14369
14370 /* Start from first_unchanged_at_end_row looking for PT. */
14371 else if (first_unchanged_at_end_row)
14372 {
14373 row = row_containing_pos (w, PT - delta,
14374 first_unchanged_at_end_row, NULL, 0);
14375 if (row)
14376 set_cursor_from_row (w, row, w->current_matrix, delta,
14377 delta_bytes, dy, dvpos);
14378 }
14379
14380 /* Give up if cursor was not found. */
14381 if (w->cursor.vpos < 0)
14382 {
14383 clear_glyph_matrix (w->desired_matrix);
14384 return -1;
14385 }
14386 }
14387
14388 /* Don't let the cursor end in the scroll margins. */
14389 {
14390 int this_scroll_margin, cursor_height;
14391
14392 this_scroll_margin = max (0, scroll_margin);
14393 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14394 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14395 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14396
14397 if ((w->cursor.y < this_scroll_margin
14398 && CHARPOS (start) > BEGV)
14399 /* Old redisplay didn't take scroll margin into account at the bottom,
14400 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14401 || (w->cursor.y + (make_cursor_line_fully_visible_p
14402 ? cursor_height + this_scroll_margin
14403 : 1)) > it.last_visible_y)
14404 {
14405 w->cursor.vpos = -1;
14406 clear_glyph_matrix (w->desired_matrix);
14407 return -1;
14408 }
14409 }
14410
14411 /* Scroll the display. Do it before changing the current matrix so
14412 that xterm.c doesn't get confused about where the cursor glyph is
14413 found. */
14414 if (dy && run.height)
14415 {
14416 update_begin (f);
14417
14418 if (FRAME_WINDOW_P (f))
14419 {
14420 rif->update_window_begin_hook (w);
14421 rif->clear_window_mouse_face (w);
14422 rif->scroll_run_hook (w, &run);
14423 rif->update_window_end_hook (w, 0, 0);
14424 }
14425 else
14426 {
14427 /* Terminal frame. In this case, dvpos gives the number of
14428 lines to scroll by; dvpos < 0 means scroll up. */
14429 int first_unchanged_at_end_vpos
14430 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14431 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14432 int end = (WINDOW_TOP_EDGE_LINE (w)
14433 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14434 + window_internal_height (w));
14435
14436 /* Perform the operation on the screen. */
14437 if (dvpos > 0)
14438 {
14439 /* Scroll last_unchanged_at_beg_row to the end of the
14440 window down dvpos lines. */
14441 set_terminal_window (end);
14442
14443 /* On dumb terminals delete dvpos lines at the end
14444 before inserting dvpos empty lines. */
14445 if (!scroll_region_ok)
14446 ins_del_lines (end - dvpos, -dvpos);
14447
14448 /* Insert dvpos empty lines in front of
14449 last_unchanged_at_beg_row. */
14450 ins_del_lines (from, dvpos);
14451 }
14452 else if (dvpos < 0)
14453 {
14454 /* Scroll up last_unchanged_at_beg_vpos to the end of
14455 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14456 set_terminal_window (end);
14457
14458 /* Delete dvpos lines in front of
14459 last_unchanged_at_beg_vpos. ins_del_lines will set
14460 the cursor to the given vpos and emit |dvpos| delete
14461 line sequences. */
14462 ins_del_lines (from + dvpos, dvpos);
14463
14464 /* On a dumb terminal insert dvpos empty lines at the
14465 end. */
14466 if (!scroll_region_ok)
14467 ins_del_lines (end + dvpos, -dvpos);
14468 }
14469
14470 set_terminal_window (0);
14471 }
14472
14473 update_end (f);
14474 }
14475
14476 /* Shift reused rows of the current matrix to the right position.
14477 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14478 text. */
14479 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14480 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14481 if (dvpos < 0)
14482 {
14483 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14484 bottom_vpos, dvpos);
14485 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14486 bottom_vpos, 0);
14487 }
14488 else if (dvpos > 0)
14489 {
14490 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14491 bottom_vpos, dvpos);
14492 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14493 first_unchanged_at_end_vpos + dvpos, 0);
14494 }
14495
14496 /* For frame-based redisplay, make sure that current frame and window
14497 matrix are in sync with respect to glyph memory. */
14498 if (!FRAME_WINDOW_P (f))
14499 sync_frame_with_window_matrix_rows (w);
14500
14501 /* Adjust buffer positions in reused rows. */
14502 if (delta)
14503 increment_matrix_positions (current_matrix,
14504 first_unchanged_at_end_vpos + dvpos,
14505 bottom_vpos, delta, delta_bytes);
14506
14507 /* Adjust Y positions. */
14508 if (dy)
14509 shift_glyph_matrix (w, current_matrix,
14510 first_unchanged_at_end_vpos + dvpos,
14511 bottom_vpos, dy);
14512
14513 if (first_unchanged_at_end_row)
14514 {
14515 first_unchanged_at_end_row += dvpos;
14516 if (first_unchanged_at_end_row->y >= it.last_visible_y
14517 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14518 first_unchanged_at_end_row = NULL;
14519 }
14520
14521 /* If scrolling up, there may be some lines to display at the end of
14522 the window. */
14523 last_text_row_at_end = NULL;
14524 if (dy < 0)
14525 {
14526 /* Scrolling up can leave for example a partially visible line
14527 at the end of the window to be redisplayed. */
14528 /* Set last_row to the glyph row in the current matrix where the
14529 window end line is found. It has been moved up or down in
14530 the matrix by dvpos. */
14531 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14532 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14533
14534 /* If last_row is the window end line, it should display text. */
14535 xassert (last_row->displays_text_p);
14536
14537 /* If window end line was partially visible before, begin
14538 displaying at that line. Otherwise begin displaying with the
14539 line following it. */
14540 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14541 {
14542 init_to_row_start (&it, w, last_row);
14543 it.vpos = last_vpos;
14544 it.current_y = last_row->y;
14545 }
14546 else
14547 {
14548 init_to_row_end (&it, w, last_row);
14549 it.vpos = 1 + last_vpos;
14550 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14551 ++last_row;
14552 }
14553
14554 /* We may start in a continuation line. If so, we have to
14555 get the right continuation_lines_width and current_x. */
14556 it.continuation_lines_width = last_row->continuation_lines_width;
14557 it.hpos = it.current_x = 0;
14558
14559 /* Display the rest of the lines at the window end. */
14560 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14561 while (it.current_y < it.last_visible_y
14562 && !fonts_changed_p)
14563 {
14564 /* Is it always sure that the display agrees with lines in
14565 the current matrix? I don't think so, so we mark rows
14566 displayed invalid in the current matrix by setting their
14567 enabled_p flag to zero. */
14568 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14569 if (display_line (&it))
14570 last_text_row_at_end = it.glyph_row - 1;
14571 }
14572 }
14573
14574 /* Update window_end_pos and window_end_vpos. */
14575 if (first_unchanged_at_end_row
14576 && !last_text_row_at_end)
14577 {
14578 /* Window end line if one of the preserved rows from the current
14579 matrix. Set row to the last row displaying text in current
14580 matrix starting at first_unchanged_at_end_row, after
14581 scrolling. */
14582 xassert (first_unchanged_at_end_row->displays_text_p);
14583 row = find_last_row_displaying_text (w->current_matrix, &it,
14584 first_unchanged_at_end_row);
14585 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14586
14587 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14588 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14589 w->window_end_vpos
14590 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14591 xassert (w->window_end_bytepos >= 0);
14592 IF_DEBUG (debug_method_add (w, "A"));
14593 }
14594 else if (last_text_row_at_end)
14595 {
14596 w->window_end_pos
14597 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14598 w->window_end_bytepos
14599 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14600 w->window_end_vpos
14601 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14602 xassert (w->window_end_bytepos >= 0);
14603 IF_DEBUG (debug_method_add (w, "B"));
14604 }
14605 else if (last_text_row)
14606 {
14607 /* We have displayed either to the end of the window or at the
14608 end of the window, i.e. the last row with text is to be found
14609 in the desired matrix. */
14610 w->window_end_pos
14611 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14612 w->window_end_bytepos
14613 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14614 w->window_end_vpos
14615 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14616 xassert (w->window_end_bytepos >= 0);
14617 }
14618 else if (first_unchanged_at_end_row == NULL
14619 && last_text_row == NULL
14620 && last_text_row_at_end == NULL)
14621 {
14622 /* Displayed to end of window, but no line containing text was
14623 displayed. Lines were deleted at the end of the window. */
14624 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14625 int vpos = XFASTINT (w->window_end_vpos);
14626 struct glyph_row *current_row = current_matrix->rows + vpos;
14627 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14628
14629 for (row = NULL;
14630 row == NULL && vpos >= first_vpos;
14631 --vpos, --current_row, --desired_row)
14632 {
14633 if (desired_row->enabled_p)
14634 {
14635 if (desired_row->displays_text_p)
14636 row = desired_row;
14637 }
14638 else if (current_row->displays_text_p)
14639 row = current_row;
14640 }
14641
14642 xassert (row != NULL);
14643 w->window_end_vpos = make_number (vpos + 1);
14644 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14645 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14646 xassert (w->window_end_bytepos >= 0);
14647 IF_DEBUG (debug_method_add (w, "C"));
14648 }
14649 else
14650 abort ();
14651
14652 #if 0 /* This leads to problems, for instance when the cursor is
14653 at ZV, and the cursor line displays no text. */
14654 /* Disable rows below what's displayed in the window. This makes
14655 debugging easier. */
14656 enable_glyph_matrix_rows (current_matrix,
14657 XFASTINT (w->window_end_vpos) + 1,
14658 bottom_vpos, 0);
14659 #endif
14660
14661 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14662 debug_end_vpos = XFASTINT (w->window_end_vpos));
14663
14664 /* Record that display has not been completed. */
14665 w->window_end_valid = Qnil;
14666 w->desired_matrix->no_scrolling_p = 1;
14667 return 3;
14668
14669 #undef GIVE_UP
14670 }
14671
14672
14673 \f
14674 /***********************************************************************
14675 More debugging support
14676 ***********************************************************************/
14677
14678 #if GLYPH_DEBUG
14679
14680 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14681 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14682 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14683
14684
14685 /* Dump the contents of glyph matrix MATRIX on stderr.
14686
14687 GLYPHS 0 means don't show glyph contents.
14688 GLYPHS 1 means show glyphs in short form
14689 GLYPHS > 1 means show glyphs in long form. */
14690
14691 void
14692 dump_glyph_matrix (matrix, glyphs)
14693 struct glyph_matrix *matrix;
14694 int glyphs;
14695 {
14696 int i;
14697 for (i = 0; i < matrix->nrows; ++i)
14698 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14699 }
14700
14701
14702 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14703 the glyph row and area where the glyph comes from. */
14704
14705 void
14706 dump_glyph (row, glyph, area)
14707 struct glyph_row *row;
14708 struct glyph *glyph;
14709 int area;
14710 {
14711 if (glyph->type == CHAR_GLYPH)
14712 {
14713 fprintf (stderr,
14714 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14715 glyph - row->glyphs[TEXT_AREA],
14716 'C',
14717 glyph->charpos,
14718 (BUFFERP (glyph->object)
14719 ? 'B'
14720 : (STRINGP (glyph->object)
14721 ? 'S'
14722 : '-')),
14723 glyph->pixel_width,
14724 glyph->u.ch,
14725 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14726 ? glyph->u.ch
14727 : '.'),
14728 glyph->face_id,
14729 glyph->left_box_line_p,
14730 glyph->right_box_line_p);
14731 }
14732 else if (glyph->type == STRETCH_GLYPH)
14733 {
14734 fprintf (stderr,
14735 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14736 glyph - row->glyphs[TEXT_AREA],
14737 'S',
14738 glyph->charpos,
14739 (BUFFERP (glyph->object)
14740 ? 'B'
14741 : (STRINGP (glyph->object)
14742 ? 'S'
14743 : '-')),
14744 glyph->pixel_width,
14745 0,
14746 '.',
14747 glyph->face_id,
14748 glyph->left_box_line_p,
14749 glyph->right_box_line_p);
14750 }
14751 else if (glyph->type == IMAGE_GLYPH)
14752 {
14753 fprintf (stderr,
14754 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14755 glyph - row->glyphs[TEXT_AREA],
14756 'I',
14757 glyph->charpos,
14758 (BUFFERP (glyph->object)
14759 ? 'B'
14760 : (STRINGP (glyph->object)
14761 ? 'S'
14762 : '-')),
14763 glyph->pixel_width,
14764 glyph->u.img_id,
14765 '.',
14766 glyph->face_id,
14767 glyph->left_box_line_p,
14768 glyph->right_box_line_p);
14769 }
14770 }
14771
14772
14773 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14774 GLYPHS 0 means don't show glyph contents.
14775 GLYPHS 1 means show glyphs in short form
14776 GLYPHS > 1 means show glyphs in long form. */
14777
14778 void
14779 dump_glyph_row (row, vpos, glyphs)
14780 struct glyph_row *row;
14781 int vpos, glyphs;
14782 {
14783 if (glyphs != 1)
14784 {
14785 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14786 fprintf (stderr, "======================================================================\n");
14787
14788 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14789 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14790 vpos,
14791 MATRIX_ROW_START_CHARPOS (row),
14792 MATRIX_ROW_END_CHARPOS (row),
14793 row->used[TEXT_AREA],
14794 row->contains_overlapping_glyphs_p,
14795 row->enabled_p,
14796 row->truncated_on_left_p,
14797 row->truncated_on_right_p,
14798 row->continued_p,
14799 MATRIX_ROW_CONTINUATION_LINE_P (row),
14800 row->displays_text_p,
14801 row->ends_at_zv_p,
14802 row->fill_line_p,
14803 row->ends_in_middle_of_char_p,
14804 row->starts_in_middle_of_char_p,
14805 row->mouse_face_p,
14806 row->x,
14807 row->y,
14808 row->pixel_width,
14809 row->height,
14810 row->visible_height,
14811 row->ascent,
14812 row->phys_ascent);
14813 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14814 row->end.overlay_string_index,
14815 row->continuation_lines_width);
14816 fprintf (stderr, "%9d %5d\n",
14817 CHARPOS (row->start.string_pos),
14818 CHARPOS (row->end.string_pos));
14819 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14820 row->end.dpvec_index);
14821 }
14822
14823 if (glyphs > 1)
14824 {
14825 int area;
14826
14827 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14828 {
14829 struct glyph *glyph = row->glyphs[area];
14830 struct glyph *glyph_end = glyph + row->used[area];
14831
14832 /* Glyph for a line end in text. */
14833 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14834 ++glyph_end;
14835
14836 if (glyph < glyph_end)
14837 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14838
14839 for (; glyph < glyph_end; ++glyph)
14840 dump_glyph (row, glyph, area);
14841 }
14842 }
14843 else if (glyphs == 1)
14844 {
14845 int area;
14846
14847 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14848 {
14849 char *s = (char *) alloca (row->used[area] + 1);
14850 int i;
14851
14852 for (i = 0; i < row->used[area]; ++i)
14853 {
14854 struct glyph *glyph = row->glyphs[area] + i;
14855 if (glyph->type == CHAR_GLYPH
14856 && glyph->u.ch < 0x80
14857 && glyph->u.ch >= ' ')
14858 s[i] = glyph->u.ch;
14859 else
14860 s[i] = '.';
14861 }
14862
14863 s[i] = '\0';
14864 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14865 }
14866 }
14867 }
14868
14869
14870 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14871 Sdump_glyph_matrix, 0, 1, "p",
14872 doc: /* Dump the current matrix of the selected window to stderr.
14873 Shows contents of glyph row structures. With non-nil
14874 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14875 glyphs in short form, otherwise show glyphs in long form. */)
14876 (glyphs)
14877 Lisp_Object glyphs;
14878 {
14879 struct window *w = XWINDOW (selected_window);
14880 struct buffer *buffer = XBUFFER (w->buffer);
14881
14882 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14883 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14884 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14885 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14886 fprintf (stderr, "=============================================\n");
14887 dump_glyph_matrix (w->current_matrix,
14888 NILP (glyphs) ? 0 : XINT (glyphs));
14889 return Qnil;
14890 }
14891
14892
14893 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14894 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14895 ()
14896 {
14897 struct frame *f = XFRAME (selected_frame);
14898 dump_glyph_matrix (f->current_matrix, 1);
14899 return Qnil;
14900 }
14901
14902
14903 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14904 doc: /* Dump glyph row ROW to stderr.
14905 GLYPH 0 means don't dump glyphs.
14906 GLYPH 1 means dump glyphs in short form.
14907 GLYPH > 1 or omitted means dump glyphs in long form. */)
14908 (row, glyphs)
14909 Lisp_Object row, glyphs;
14910 {
14911 struct glyph_matrix *matrix;
14912 int vpos;
14913
14914 CHECK_NUMBER (row);
14915 matrix = XWINDOW (selected_window)->current_matrix;
14916 vpos = XINT (row);
14917 if (vpos >= 0 && vpos < matrix->nrows)
14918 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14919 vpos,
14920 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14921 return Qnil;
14922 }
14923
14924
14925 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14926 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14927 GLYPH 0 means don't dump glyphs.
14928 GLYPH 1 means dump glyphs in short form.
14929 GLYPH > 1 or omitted means dump glyphs in long form. */)
14930 (row, glyphs)
14931 Lisp_Object row, glyphs;
14932 {
14933 struct frame *sf = SELECTED_FRAME ();
14934 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14935 int vpos;
14936
14937 CHECK_NUMBER (row);
14938 vpos = XINT (row);
14939 if (vpos >= 0 && vpos < m->nrows)
14940 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14941 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14942 return Qnil;
14943 }
14944
14945
14946 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14947 doc: /* Toggle tracing of redisplay.
14948 With ARG, turn tracing on if and only if ARG is positive. */)
14949 (arg)
14950 Lisp_Object arg;
14951 {
14952 if (NILP (arg))
14953 trace_redisplay_p = !trace_redisplay_p;
14954 else
14955 {
14956 arg = Fprefix_numeric_value (arg);
14957 trace_redisplay_p = XINT (arg) > 0;
14958 }
14959
14960 return Qnil;
14961 }
14962
14963
14964 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14965 doc: /* Like `format', but print result to stderr.
14966 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14967 (nargs, args)
14968 int nargs;
14969 Lisp_Object *args;
14970 {
14971 Lisp_Object s = Fformat (nargs, args);
14972 fprintf (stderr, "%s", SDATA (s));
14973 return Qnil;
14974 }
14975
14976 #endif /* GLYPH_DEBUG */
14977
14978
14979 \f
14980 /***********************************************************************
14981 Building Desired Matrix Rows
14982 ***********************************************************************/
14983
14984 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14985 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14986
14987 static struct glyph_row *
14988 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14989 struct window *w;
14990 Lisp_Object overlay_arrow_string;
14991 {
14992 struct frame *f = XFRAME (WINDOW_FRAME (w));
14993 struct buffer *buffer = XBUFFER (w->buffer);
14994 struct buffer *old = current_buffer;
14995 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14996 int arrow_len = SCHARS (overlay_arrow_string);
14997 const unsigned char *arrow_end = arrow_string + arrow_len;
14998 const unsigned char *p;
14999 struct it it;
15000 int multibyte_p;
15001 int n_glyphs_before;
15002
15003 set_buffer_temp (buffer);
15004 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15005 it.glyph_row->used[TEXT_AREA] = 0;
15006 SET_TEXT_POS (it.position, 0, 0);
15007
15008 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15009 p = arrow_string;
15010 while (p < arrow_end)
15011 {
15012 Lisp_Object face, ilisp;
15013
15014 /* Get the next character. */
15015 if (multibyte_p)
15016 it.c = string_char_and_length (p, arrow_len, &it.len);
15017 else
15018 it.c = *p, it.len = 1;
15019 p += it.len;
15020
15021 /* Get its face. */
15022 ilisp = make_number (p - arrow_string);
15023 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15024 it.face_id = compute_char_face (f, it.c, face);
15025
15026 /* Compute its width, get its glyphs. */
15027 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15028 SET_TEXT_POS (it.position, -1, -1);
15029 PRODUCE_GLYPHS (&it);
15030
15031 /* If this character doesn't fit any more in the line, we have
15032 to remove some glyphs. */
15033 if (it.current_x > it.last_visible_x)
15034 {
15035 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15036 break;
15037 }
15038 }
15039
15040 set_buffer_temp (old);
15041 return it.glyph_row;
15042 }
15043
15044
15045 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15046 glyphs are only inserted for terminal frames since we can't really
15047 win with truncation glyphs when partially visible glyphs are
15048 involved. Which glyphs to insert is determined by
15049 produce_special_glyphs. */
15050
15051 static void
15052 insert_left_trunc_glyphs (it)
15053 struct it *it;
15054 {
15055 struct it truncate_it;
15056 struct glyph *from, *end, *to, *toend;
15057
15058 xassert (!FRAME_WINDOW_P (it->f));
15059
15060 /* Get the truncation glyphs. */
15061 truncate_it = *it;
15062 truncate_it.current_x = 0;
15063 truncate_it.face_id = DEFAULT_FACE_ID;
15064 truncate_it.glyph_row = &scratch_glyph_row;
15065 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15066 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15067 truncate_it.object = make_number (0);
15068 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15069
15070 /* Overwrite glyphs from IT with truncation glyphs. */
15071 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15072 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15073 to = it->glyph_row->glyphs[TEXT_AREA];
15074 toend = to + it->glyph_row->used[TEXT_AREA];
15075
15076 while (from < end)
15077 *to++ = *from++;
15078
15079 /* There may be padding glyphs left over. Overwrite them too. */
15080 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15081 {
15082 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15083 while (from < end)
15084 *to++ = *from++;
15085 }
15086
15087 if (to > toend)
15088 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15089 }
15090
15091
15092 /* Compute the pixel height and width of IT->glyph_row.
15093
15094 Most of the time, ascent and height of a display line will be equal
15095 to the max_ascent and max_height values of the display iterator
15096 structure. This is not the case if
15097
15098 1. We hit ZV without displaying anything. In this case, max_ascent
15099 and max_height will be zero.
15100
15101 2. We have some glyphs that don't contribute to the line height.
15102 (The glyph row flag contributes_to_line_height_p is for future
15103 pixmap extensions).
15104
15105 The first case is easily covered by using default values because in
15106 these cases, the line height does not really matter, except that it
15107 must not be zero. */
15108
15109 static void
15110 compute_line_metrics (it)
15111 struct it *it;
15112 {
15113 struct glyph_row *row = it->glyph_row;
15114 int area, i;
15115
15116 if (FRAME_WINDOW_P (it->f))
15117 {
15118 int i, min_y, max_y;
15119
15120 /* The line may consist of one space only, that was added to
15121 place the cursor on it. If so, the row's height hasn't been
15122 computed yet. */
15123 if (row->height == 0)
15124 {
15125 if (it->max_ascent + it->max_descent == 0)
15126 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15127 row->ascent = it->max_ascent;
15128 row->height = it->max_ascent + it->max_descent;
15129 row->phys_ascent = it->max_phys_ascent;
15130 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15131 row->extra_line_spacing = it->max_extra_line_spacing;
15132 }
15133
15134 /* Compute the width of this line. */
15135 row->pixel_width = row->x;
15136 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15137 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15138
15139 xassert (row->pixel_width >= 0);
15140 xassert (row->ascent >= 0 && row->height > 0);
15141
15142 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15143 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15144
15145 /* If first line's physical ascent is larger than its logical
15146 ascent, use the physical ascent, and make the row taller.
15147 This makes accented characters fully visible. */
15148 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15149 && row->phys_ascent > row->ascent)
15150 {
15151 row->height += row->phys_ascent - row->ascent;
15152 row->ascent = row->phys_ascent;
15153 }
15154
15155 /* Compute how much of the line is visible. */
15156 row->visible_height = row->height;
15157
15158 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15159 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15160
15161 if (row->y < min_y)
15162 row->visible_height -= min_y - row->y;
15163 if (row->y + row->height > max_y)
15164 row->visible_height -= row->y + row->height - max_y;
15165 }
15166 else
15167 {
15168 row->pixel_width = row->used[TEXT_AREA];
15169 if (row->continued_p)
15170 row->pixel_width -= it->continuation_pixel_width;
15171 else if (row->truncated_on_right_p)
15172 row->pixel_width -= it->truncation_pixel_width;
15173 row->ascent = row->phys_ascent = 0;
15174 row->height = row->phys_height = row->visible_height = 1;
15175 row->extra_line_spacing = 0;
15176 }
15177
15178 /* Compute a hash code for this row. */
15179 row->hash = 0;
15180 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15181 for (i = 0; i < row->used[area]; ++i)
15182 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15183 + row->glyphs[area][i].u.val
15184 + row->glyphs[area][i].face_id
15185 + row->glyphs[area][i].padding_p
15186 + (row->glyphs[area][i].type << 2));
15187
15188 it->max_ascent = it->max_descent = 0;
15189 it->max_phys_ascent = it->max_phys_descent = 0;
15190 }
15191
15192
15193 /* Append one space to the glyph row of iterator IT if doing a
15194 window-based redisplay. The space has the same face as
15195 IT->face_id. Value is non-zero if a space was added.
15196
15197 This function is called to make sure that there is always one glyph
15198 at the end of a glyph row that the cursor can be set on under
15199 window-systems. (If there weren't such a glyph we would not know
15200 how wide and tall a box cursor should be displayed).
15201
15202 At the same time this space let's a nicely handle clearing to the
15203 end of the line if the row ends in italic text. */
15204
15205 static int
15206 append_space_for_newline (it, default_face_p)
15207 struct it *it;
15208 int default_face_p;
15209 {
15210 if (FRAME_WINDOW_P (it->f))
15211 {
15212 int n = it->glyph_row->used[TEXT_AREA];
15213
15214 if (it->glyph_row->glyphs[TEXT_AREA] + n
15215 < it->glyph_row->glyphs[1 + TEXT_AREA])
15216 {
15217 /* Save some values that must not be changed.
15218 Must save IT->c and IT->len because otherwise
15219 ITERATOR_AT_END_P wouldn't work anymore after
15220 append_space_for_newline has been called. */
15221 enum display_element_type saved_what = it->what;
15222 int saved_c = it->c, saved_len = it->len;
15223 int saved_x = it->current_x;
15224 int saved_face_id = it->face_id;
15225 struct text_pos saved_pos;
15226 Lisp_Object saved_object;
15227 struct face *face;
15228
15229 saved_object = it->object;
15230 saved_pos = it->position;
15231
15232 it->what = IT_CHARACTER;
15233 bzero (&it->position, sizeof it->position);
15234 it->object = make_number (0);
15235 it->c = ' ';
15236 it->len = 1;
15237
15238 if (default_face_p)
15239 it->face_id = DEFAULT_FACE_ID;
15240 else if (it->face_before_selective_p)
15241 it->face_id = it->saved_face_id;
15242 face = FACE_FROM_ID (it->f, it->face_id);
15243 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15244
15245 PRODUCE_GLYPHS (it);
15246
15247 it->override_ascent = -1;
15248 it->constrain_row_ascent_descent_p = 0;
15249 it->current_x = saved_x;
15250 it->object = saved_object;
15251 it->position = saved_pos;
15252 it->what = saved_what;
15253 it->face_id = saved_face_id;
15254 it->len = saved_len;
15255 it->c = saved_c;
15256 return 1;
15257 }
15258 }
15259
15260 return 0;
15261 }
15262
15263
15264 /* Extend the face of the last glyph in the text area of IT->glyph_row
15265 to the end of the display line. Called from display_line.
15266 If the glyph row is empty, add a space glyph to it so that we
15267 know the face to draw. Set the glyph row flag fill_line_p. */
15268
15269 static void
15270 extend_face_to_end_of_line (it)
15271 struct it *it;
15272 {
15273 struct face *face;
15274 struct frame *f = it->f;
15275
15276 /* If line is already filled, do nothing. */
15277 if (it->current_x >= it->last_visible_x)
15278 return;
15279
15280 /* Face extension extends the background and box of IT->face_id
15281 to the end of the line. If the background equals the background
15282 of the frame, we don't have to do anything. */
15283 if (it->face_before_selective_p)
15284 face = FACE_FROM_ID (it->f, it->saved_face_id);
15285 else
15286 face = FACE_FROM_ID (f, it->face_id);
15287
15288 if (FRAME_WINDOW_P (f)
15289 && face->box == FACE_NO_BOX
15290 && face->background == FRAME_BACKGROUND_PIXEL (f)
15291 && !face->stipple)
15292 return;
15293
15294 /* Set the glyph row flag indicating that the face of the last glyph
15295 in the text area has to be drawn to the end of the text area. */
15296 it->glyph_row->fill_line_p = 1;
15297
15298 /* If current character of IT is not ASCII, make sure we have the
15299 ASCII face. This will be automatically undone the next time
15300 get_next_display_element returns a multibyte character. Note
15301 that the character will always be single byte in unibyte text. */
15302 if (!SINGLE_BYTE_CHAR_P (it->c))
15303 {
15304 it->face_id = FACE_FOR_CHAR (f, face, 0);
15305 }
15306
15307 if (FRAME_WINDOW_P (f))
15308 {
15309 /* If the row is empty, add a space with the current face of IT,
15310 so that we know which face to draw. */
15311 if (it->glyph_row->used[TEXT_AREA] == 0)
15312 {
15313 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15314 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15315 it->glyph_row->used[TEXT_AREA] = 1;
15316 }
15317 }
15318 else
15319 {
15320 /* Save some values that must not be changed. */
15321 int saved_x = it->current_x;
15322 struct text_pos saved_pos;
15323 Lisp_Object saved_object;
15324 enum display_element_type saved_what = it->what;
15325 int saved_face_id = it->face_id;
15326
15327 saved_object = it->object;
15328 saved_pos = it->position;
15329
15330 it->what = IT_CHARACTER;
15331 bzero (&it->position, sizeof it->position);
15332 it->object = make_number (0);
15333 it->c = ' ';
15334 it->len = 1;
15335 it->face_id = face->id;
15336
15337 PRODUCE_GLYPHS (it);
15338
15339 while (it->current_x <= it->last_visible_x)
15340 PRODUCE_GLYPHS (it);
15341
15342 /* Don't count these blanks really. It would let us insert a left
15343 truncation glyph below and make us set the cursor on them, maybe. */
15344 it->current_x = saved_x;
15345 it->object = saved_object;
15346 it->position = saved_pos;
15347 it->what = saved_what;
15348 it->face_id = saved_face_id;
15349 }
15350 }
15351
15352
15353 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15354 trailing whitespace. */
15355
15356 static int
15357 trailing_whitespace_p (charpos)
15358 int charpos;
15359 {
15360 int bytepos = CHAR_TO_BYTE (charpos);
15361 int c = 0;
15362
15363 while (bytepos < ZV_BYTE
15364 && (c = FETCH_CHAR (bytepos),
15365 c == ' ' || c == '\t'))
15366 ++bytepos;
15367
15368 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15369 {
15370 if (bytepos != PT_BYTE)
15371 return 1;
15372 }
15373 return 0;
15374 }
15375
15376
15377 /* Highlight trailing whitespace, if any, in ROW. */
15378
15379 void
15380 highlight_trailing_whitespace (f, row)
15381 struct frame *f;
15382 struct glyph_row *row;
15383 {
15384 int used = row->used[TEXT_AREA];
15385
15386 if (used)
15387 {
15388 struct glyph *start = row->glyphs[TEXT_AREA];
15389 struct glyph *glyph = start + used - 1;
15390
15391 /* Skip over glyphs inserted to display the cursor at the
15392 end of a line, for extending the face of the last glyph
15393 to the end of the line on terminals, and for truncation
15394 and continuation glyphs. */
15395 while (glyph >= start
15396 && glyph->type == CHAR_GLYPH
15397 && INTEGERP (glyph->object))
15398 --glyph;
15399
15400 /* If last glyph is a space or stretch, and it's trailing
15401 whitespace, set the face of all trailing whitespace glyphs in
15402 IT->glyph_row to `trailing-whitespace'. */
15403 if (glyph >= start
15404 && BUFFERP (glyph->object)
15405 && (glyph->type == STRETCH_GLYPH
15406 || (glyph->type == CHAR_GLYPH
15407 && glyph->u.ch == ' '))
15408 && trailing_whitespace_p (glyph->charpos))
15409 {
15410 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15411 if (face_id < 0)
15412 return;
15413
15414 while (glyph >= start
15415 && BUFFERP (glyph->object)
15416 && (glyph->type == STRETCH_GLYPH
15417 || (glyph->type == CHAR_GLYPH
15418 && glyph->u.ch == ' ')))
15419 (glyph--)->face_id = face_id;
15420 }
15421 }
15422 }
15423
15424
15425 /* Value is non-zero if glyph row ROW in window W should be
15426 used to hold the cursor. */
15427
15428 static int
15429 cursor_row_p (w, row)
15430 struct window *w;
15431 struct glyph_row *row;
15432 {
15433 int cursor_row_p = 1;
15434
15435 if (PT == MATRIX_ROW_END_CHARPOS (row))
15436 {
15437 /* If the row ends with a newline from a string, we don't want
15438 the cursor there, but we still want it at the start of the
15439 string if the string starts in this row.
15440 If the row is continued it doesn't end in a newline. */
15441 if (CHARPOS (row->end.string_pos) >= 0)
15442 cursor_row_p = (row->continued_p
15443 || PT >= MATRIX_ROW_START_CHARPOS (row));
15444 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15445 {
15446 /* If the row ends in middle of a real character,
15447 and the line is continued, we want the cursor here.
15448 That's because MATRIX_ROW_END_CHARPOS would equal
15449 PT if PT is before the character. */
15450 if (!row->ends_in_ellipsis_p)
15451 cursor_row_p = row->continued_p;
15452 else
15453 /* If the row ends in an ellipsis, then
15454 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15455 We want that position to be displayed after the ellipsis. */
15456 cursor_row_p = 0;
15457 }
15458 /* If the row ends at ZV, display the cursor at the end of that
15459 row instead of at the start of the row below. */
15460 else if (row->ends_at_zv_p)
15461 cursor_row_p = 1;
15462 else
15463 cursor_row_p = 0;
15464 }
15465
15466 return cursor_row_p;
15467 }
15468
15469
15470 /* Construct the glyph row IT->glyph_row in the desired matrix of
15471 IT->w from text at the current position of IT. See dispextern.h
15472 for an overview of struct it. Value is non-zero if
15473 IT->glyph_row displays text, as opposed to a line displaying ZV
15474 only. */
15475
15476 static int
15477 display_line (it)
15478 struct it *it;
15479 {
15480 struct glyph_row *row = it->glyph_row;
15481 Lisp_Object overlay_arrow_string;
15482
15483 /* We always start displaying at hpos zero even if hscrolled. */
15484 xassert (it->hpos == 0 && it->current_x == 0);
15485
15486 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15487 >= it->w->desired_matrix->nrows)
15488 {
15489 it->w->nrows_scale_factor++;
15490 fonts_changed_p = 1;
15491 return 0;
15492 }
15493
15494 /* Is IT->w showing the region? */
15495 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15496
15497 /* Clear the result glyph row and enable it. */
15498 prepare_desired_row (row);
15499
15500 row->y = it->current_y;
15501 row->start = it->start;
15502 row->continuation_lines_width = it->continuation_lines_width;
15503 row->displays_text_p = 1;
15504 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15505 it->starts_in_middle_of_char_p = 0;
15506
15507 /* Arrange the overlays nicely for our purposes. Usually, we call
15508 display_line on only one line at a time, in which case this
15509 can't really hurt too much, or we call it on lines which appear
15510 one after another in the buffer, in which case all calls to
15511 recenter_overlay_lists but the first will be pretty cheap. */
15512 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15513
15514 /* Move over display elements that are not visible because we are
15515 hscrolled. This may stop at an x-position < IT->first_visible_x
15516 if the first glyph is partially visible or if we hit a line end. */
15517 if (it->current_x < it->first_visible_x)
15518 {
15519 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15520 MOVE_TO_POS | MOVE_TO_X);
15521 }
15522
15523 /* Get the initial row height. This is either the height of the
15524 text hscrolled, if there is any, or zero. */
15525 row->ascent = it->max_ascent;
15526 row->height = it->max_ascent + it->max_descent;
15527 row->phys_ascent = it->max_phys_ascent;
15528 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15529 row->extra_line_spacing = it->max_extra_line_spacing;
15530
15531 /* Loop generating characters. The loop is left with IT on the next
15532 character to display. */
15533 while (1)
15534 {
15535 int n_glyphs_before, hpos_before, x_before;
15536 int x, i, nglyphs;
15537 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15538
15539 /* Retrieve the next thing to display. Value is zero if end of
15540 buffer reached. */
15541 if (!get_next_display_element (it))
15542 {
15543 /* Maybe add a space at the end of this line that is used to
15544 display the cursor there under X. Set the charpos of the
15545 first glyph of blank lines not corresponding to any text
15546 to -1. */
15547 #ifdef HAVE_WINDOW_SYSTEM
15548 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15549 row->exact_window_width_line_p = 1;
15550 else
15551 #endif /* HAVE_WINDOW_SYSTEM */
15552 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15553 || row->used[TEXT_AREA] == 0)
15554 {
15555 row->glyphs[TEXT_AREA]->charpos = -1;
15556 row->displays_text_p = 0;
15557
15558 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15559 && (!MINI_WINDOW_P (it->w)
15560 || (minibuf_level && EQ (it->window, minibuf_window))))
15561 row->indicate_empty_line_p = 1;
15562 }
15563
15564 it->continuation_lines_width = 0;
15565 row->ends_at_zv_p = 1;
15566 break;
15567 }
15568
15569 /* Now, get the metrics of what we want to display. This also
15570 generates glyphs in `row' (which is IT->glyph_row). */
15571 n_glyphs_before = row->used[TEXT_AREA];
15572 x = it->current_x;
15573
15574 /* Remember the line height so far in case the next element doesn't
15575 fit on the line. */
15576 if (!it->truncate_lines_p)
15577 {
15578 ascent = it->max_ascent;
15579 descent = it->max_descent;
15580 phys_ascent = it->max_phys_ascent;
15581 phys_descent = it->max_phys_descent;
15582 }
15583
15584 PRODUCE_GLYPHS (it);
15585
15586 /* If this display element was in marginal areas, continue with
15587 the next one. */
15588 if (it->area != TEXT_AREA)
15589 {
15590 row->ascent = max (row->ascent, it->max_ascent);
15591 row->height = max (row->height, it->max_ascent + it->max_descent);
15592 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15593 row->phys_height = max (row->phys_height,
15594 it->max_phys_ascent + it->max_phys_descent);
15595 row->extra_line_spacing = max (row->extra_line_spacing,
15596 it->max_extra_line_spacing);
15597 set_iterator_to_next (it, 1);
15598 continue;
15599 }
15600
15601 /* Does the display element fit on the line? If we truncate
15602 lines, we should draw past the right edge of the window. If
15603 we don't truncate, we want to stop so that we can display the
15604 continuation glyph before the right margin. If lines are
15605 continued, there are two possible strategies for characters
15606 resulting in more than 1 glyph (e.g. tabs): Display as many
15607 glyphs as possible in this line and leave the rest for the
15608 continuation line, or display the whole element in the next
15609 line. Original redisplay did the former, so we do it also. */
15610 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15611 hpos_before = it->hpos;
15612 x_before = x;
15613
15614 if (/* Not a newline. */
15615 nglyphs > 0
15616 /* Glyphs produced fit entirely in the line. */
15617 && it->current_x < it->last_visible_x)
15618 {
15619 it->hpos += nglyphs;
15620 row->ascent = max (row->ascent, it->max_ascent);
15621 row->height = max (row->height, it->max_ascent + it->max_descent);
15622 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15623 row->phys_height = max (row->phys_height,
15624 it->max_phys_ascent + it->max_phys_descent);
15625 row->extra_line_spacing = max (row->extra_line_spacing,
15626 it->max_extra_line_spacing);
15627 if (it->current_x - it->pixel_width < it->first_visible_x)
15628 row->x = x - it->first_visible_x;
15629 }
15630 else
15631 {
15632 int new_x;
15633 struct glyph *glyph;
15634
15635 for (i = 0; i < nglyphs; ++i, x = new_x)
15636 {
15637 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15638 new_x = x + glyph->pixel_width;
15639
15640 if (/* Lines are continued. */
15641 !it->truncate_lines_p
15642 && (/* Glyph doesn't fit on the line. */
15643 new_x > it->last_visible_x
15644 /* Or it fits exactly on a window system frame. */
15645 || (new_x == it->last_visible_x
15646 && FRAME_WINDOW_P (it->f))))
15647 {
15648 /* End of a continued line. */
15649
15650 if (it->hpos == 0
15651 || (new_x == it->last_visible_x
15652 && FRAME_WINDOW_P (it->f)))
15653 {
15654 /* Current glyph is the only one on the line or
15655 fits exactly on the line. We must continue
15656 the line because we can't draw the cursor
15657 after the glyph. */
15658 row->continued_p = 1;
15659 it->current_x = new_x;
15660 it->continuation_lines_width += new_x;
15661 ++it->hpos;
15662 if (i == nglyphs - 1)
15663 {
15664 set_iterator_to_next (it, 1);
15665 #ifdef HAVE_WINDOW_SYSTEM
15666 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15667 {
15668 if (!get_next_display_element (it))
15669 {
15670 row->exact_window_width_line_p = 1;
15671 it->continuation_lines_width = 0;
15672 row->continued_p = 0;
15673 row->ends_at_zv_p = 1;
15674 }
15675 else if (ITERATOR_AT_END_OF_LINE_P (it))
15676 {
15677 row->continued_p = 0;
15678 row->exact_window_width_line_p = 1;
15679 }
15680 }
15681 #endif /* HAVE_WINDOW_SYSTEM */
15682 }
15683 }
15684 else if (CHAR_GLYPH_PADDING_P (*glyph)
15685 && !FRAME_WINDOW_P (it->f))
15686 {
15687 /* A padding glyph that doesn't fit on this line.
15688 This means the whole character doesn't fit
15689 on the line. */
15690 row->used[TEXT_AREA] = n_glyphs_before;
15691
15692 /* Fill the rest of the row with continuation
15693 glyphs like in 20.x. */
15694 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15695 < row->glyphs[1 + TEXT_AREA])
15696 produce_special_glyphs (it, IT_CONTINUATION);
15697
15698 row->continued_p = 1;
15699 it->current_x = x_before;
15700 it->continuation_lines_width += x_before;
15701
15702 /* Restore the height to what it was before the
15703 element not fitting on the line. */
15704 it->max_ascent = ascent;
15705 it->max_descent = descent;
15706 it->max_phys_ascent = phys_ascent;
15707 it->max_phys_descent = phys_descent;
15708 }
15709 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15710 {
15711 /* A TAB that extends past the right edge of the
15712 window. This produces a single glyph on
15713 window system frames. We leave the glyph in
15714 this row and let it fill the row, but don't
15715 consume the TAB. */
15716 it->continuation_lines_width += it->last_visible_x;
15717 row->ends_in_middle_of_char_p = 1;
15718 row->continued_p = 1;
15719 glyph->pixel_width = it->last_visible_x - x;
15720 it->starts_in_middle_of_char_p = 1;
15721 }
15722 else
15723 {
15724 /* Something other than a TAB that draws past
15725 the right edge of the window. Restore
15726 positions to values before the element. */
15727 row->used[TEXT_AREA] = n_glyphs_before + i;
15728
15729 /* Display continuation glyphs. */
15730 if (!FRAME_WINDOW_P (it->f))
15731 produce_special_glyphs (it, IT_CONTINUATION);
15732 row->continued_p = 1;
15733
15734 it->current_x = x_before;
15735 it->continuation_lines_width += x;
15736 extend_face_to_end_of_line (it);
15737
15738 if (nglyphs > 1 && i > 0)
15739 {
15740 row->ends_in_middle_of_char_p = 1;
15741 it->starts_in_middle_of_char_p = 1;
15742 }
15743
15744 /* Restore the height to what it was before the
15745 element not fitting on the line. */
15746 it->max_ascent = ascent;
15747 it->max_descent = descent;
15748 it->max_phys_ascent = phys_ascent;
15749 it->max_phys_descent = phys_descent;
15750 }
15751
15752 break;
15753 }
15754 else if (new_x > it->first_visible_x)
15755 {
15756 /* Increment number of glyphs actually displayed. */
15757 ++it->hpos;
15758
15759 if (x < it->first_visible_x)
15760 /* Glyph is partially visible, i.e. row starts at
15761 negative X position. */
15762 row->x = x - it->first_visible_x;
15763 }
15764 else
15765 {
15766 /* Glyph is completely off the left margin of the
15767 window. This should not happen because of the
15768 move_it_in_display_line at the start of this
15769 function, unless the text display area of the
15770 window is empty. */
15771 xassert (it->first_visible_x <= it->last_visible_x);
15772 }
15773 }
15774
15775 row->ascent = max (row->ascent, it->max_ascent);
15776 row->height = max (row->height, it->max_ascent + it->max_descent);
15777 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15778 row->phys_height = max (row->phys_height,
15779 it->max_phys_ascent + it->max_phys_descent);
15780 row->extra_line_spacing = max (row->extra_line_spacing,
15781 it->max_extra_line_spacing);
15782
15783 /* End of this display line if row is continued. */
15784 if (row->continued_p || row->ends_at_zv_p)
15785 break;
15786 }
15787
15788 at_end_of_line:
15789 /* Is this a line end? If yes, we're also done, after making
15790 sure that a non-default face is extended up to the right
15791 margin of the window. */
15792 if (ITERATOR_AT_END_OF_LINE_P (it))
15793 {
15794 int used_before = row->used[TEXT_AREA];
15795
15796 row->ends_in_newline_from_string_p = STRINGP (it->object);
15797
15798 #ifdef HAVE_WINDOW_SYSTEM
15799 /* Add a space at the end of the line that is used to
15800 display the cursor there. */
15801 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15802 append_space_for_newline (it, 0);
15803 #endif /* HAVE_WINDOW_SYSTEM */
15804
15805 /* Extend the face to the end of the line. */
15806 extend_face_to_end_of_line (it);
15807
15808 /* Make sure we have the position. */
15809 if (used_before == 0)
15810 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15811
15812 /* Consume the line end. This skips over invisible lines. */
15813 set_iterator_to_next (it, 1);
15814 it->continuation_lines_width = 0;
15815 break;
15816 }
15817
15818 /* Proceed with next display element. Note that this skips
15819 over lines invisible because of selective display. */
15820 set_iterator_to_next (it, 1);
15821
15822 /* If we truncate lines, we are done when the last displayed
15823 glyphs reach past the right margin of the window. */
15824 if (it->truncate_lines_p
15825 && (FRAME_WINDOW_P (it->f)
15826 ? (it->current_x >= it->last_visible_x)
15827 : (it->current_x > it->last_visible_x)))
15828 {
15829 /* Maybe add truncation glyphs. */
15830 if (!FRAME_WINDOW_P (it->f))
15831 {
15832 int i, n;
15833
15834 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15835 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15836 break;
15837
15838 for (n = row->used[TEXT_AREA]; i < n; ++i)
15839 {
15840 row->used[TEXT_AREA] = i;
15841 produce_special_glyphs (it, IT_TRUNCATION);
15842 }
15843 }
15844 #ifdef HAVE_WINDOW_SYSTEM
15845 else
15846 {
15847 /* Don't truncate if we can overflow newline into fringe. */
15848 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15849 {
15850 if (!get_next_display_element (it))
15851 {
15852 it->continuation_lines_width = 0;
15853 row->ends_at_zv_p = 1;
15854 row->exact_window_width_line_p = 1;
15855 break;
15856 }
15857 if (ITERATOR_AT_END_OF_LINE_P (it))
15858 {
15859 row->exact_window_width_line_p = 1;
15860 goto at_end_of_line;
15861 }
15862 }
15863 }
15864 #endif /* HAVE_WINDOW_SYSTEM */
15865
15866 row->truncated_on_right_p = 1;
15867 it->continuation_lines_width = 0;
15868 reseat_at_next_visible_line_start (it, 0);
15869 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15870 it->hpos = hpos_before;
15871 it->current_x = x_before;
15872 break;
15873 }
15874 }
15875
15876 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15877 at the left window margin. */
15878 if (it->first_visible_x
15879 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15880 {
15881 if (!FRAME_WINDOW_P (it->f))
15882 insert_left_trunc_glyphs (it);
15883 row->truncated_on_left_p = 1;
15884 }
15885
15886 /* If the start of this line is the overlay arrow-position, then
15887 mark this glyph row as the one containing the overlay arrow.
15888 This is clearly a mess with variable size fonts. It would be
15889 better to let it be displayed like cursors under X. */
15890 if ((row->displays_text_p || !overlay_arrow_seen)
15891 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15892 !NILP (overlay_arrow_string)))
15893 {
15894 /* Overlay arrow in window redisplay is a fringe bitmap. */
15895 if (STRINGP (overlay_arrow_string))
15896 {
15897 struct glyph_row *arrow_row
15898 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15899 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15900 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15901 struct glyph *p = row->glyphs[TEXT_AREA];
15902 struct glyph *p2, *end;
15903
15904 /* Copy the arrow glyphs. */
15905 while (glyph < arrow_end)
15906 *p++ = *glyph++;
15907
15908 /* Throw away padding glyphs. */
15909 p2 = p;
15910 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15911 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15912 ++p2;
15913 if (p2 > p)
15914 {
15915 while (p2 < end)
15916 *p++ = *p2++;
15917 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15918 }
15919 }
15920 else
15921 {
15922 xassert (INTEGERP (overlay_arrow_string));
15923 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15924 }
15925 overlay_arrow_seen = 1;
15926 }
15927
15928 /* Compute pixel dimensions of this line. */
15929 compute_line_metrics (it);
15930
15931 /* Remember the position at which this line ends. */
15932 row->end = it->current;
15933
15934 /* Record whether this row ends inside an ellipsis. */
15935 row->ends_in_ellipsis_p
15936 = (it->method == GET_FROM_DISPLAY_VECTOR
15937 && it->ellipsis_p);
15938
15939 /* Save fringe bitmaps in this row. */
15940 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15941 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15942 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15943 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15944
15945 it->left_user_fringe_bitmap = 0;
15946 it->left_user_fringe_face_id = 0;
15947 it->right_user_fringe_bitmap = 0;
15948 it->right_user_fringe_face_id = 0;
15949
15950 /* Maybe set the cursor. */
15951 if (it->w->cursor.vpos < 0
15952 && PT >= MATRIX_ROW_START_CHARPOS (row)
15953 && PT <= MATRIX_ROW_END_CHARPOS (row)
15954 && cursor_row_p (it->w, row))
15955 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15956
15957 /* Highlight trailing whitespace. */
15958 if (!NILP (Vshow_trailing_whitespace))
15959 highlight_trailing_whitespace (it->f, it->glyph_row);
15960
15961 /* Prepare for the next line. This line starts horizontally at (X
15962 HPOS) = (0 0). Vertical positions are incremented. As a
15963 convenience for the caller, IT->glyph_row is set to the next
15964 row to be used. */
15965 it->current_x = it->hpos = 0;
15966 it->current_y += row->height;
15967 ++it->vpos;
15968 ++it->glyph_row;
15969 it->start = it->current;
15970 return row->displays_text_p;
15971 }
15972
15973
15974 \f
15975 /***********************************************************************
15976 Menu Bar
15977 ***********************************************************************/
15978
15979 /* Redisplay the menu bar in the frame for window W.
15980
15981 The menu bar of X frames that don't have X toolkit support is
15982 displayed in a special window W->frame->menu_bar_window.
15983
15984 The menu bar of terminal frames is treated specially as far as
15985 glyph matrices are concerned. Menu bar lines are not part of
15986 windows, so the update is done directly on the frame matrix rows
15987 for the menu bar. */
15988
15989 static void
15990 display_menu_bar (w)
15991 struct window *w;
15992 {
15993 struct frame *f = XFRAME (WINDOW_FRAME (w));
15994 struct it it;
15995 Lisp_Object items;
15996 int i;
15997
15998 /* Don't do all this for graphical frames. */
15999 #ifdef HAVE_NTGUI
16000 if (!NILP (Vwindow_system))
16001 return;
16002 #endif
16003 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16004 if (FRAME_X_P (f))
16005 return;
16006 #endif
16007 #ifdef MAC_OS
16008 if (FRAME_MAC_P (f))
16009 return;
16010 #endif
16011
16012 #ifdef USE_X_TOOLKIT
16013 xassert (!FRAME_WINDOW_P (f));
16014 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16015 it.first_visible_x = 0;
16016 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16017 #else /* not USE_X_TOOLKIT */
16018 if (FRAME_WINDOW_P (f))
16019 {
16020 /* Menu bar lines are displayed in the desired matrix of the
16021 dummy window menu_bar_window. */
16022 struct window *menu_w;
16023 xassert (WINDOWP (f->menu_bar_window));
16024 menu_w = XWINDOW (f->menu_bar_window);
16025 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16026 MENU_FACE_ID);
16027 it.first_visible_x = 0;
16028 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16029 }
16030 else
16031 {
16032 /* This is a TTY frame, i.e. character hpos/vpos are used as
16033 pixel x/y. */
16034 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16035 MENU_FACE_ID);
16036 it.first_visible_x = 0;
16037 it.last_visible_x = FRAME_COLS (f);
16038 }
16039 #endif /* not USE_X_TOOLKIT */
16040
16041 if (! mode_line_inverse_video)
16042 /* Force the menu-bar to be displayed in the default face. */
16043 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16044
16045 /* Clear all rows of the menu bar. */
16046 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16047 {
16048 struct glyph_row *row = it.glyph_row + i;
16049 clear_glyph_row (row);
16050 row->enabled_p = 1;
16051 row->full_width_p = 1;
16052 }
16053
16054 /* Display all items of the menu bar. */
16055 items = FRAME_MENU_BAR_ITEMS (it.f);
16056 for (i = 0; i < XVECTOR (items)->size; i += 4)
16057 {
16058 Lisp_Object string;
16059
16060 /* Stop at nil string. */
16061 string = AREF (items, i + 1);
16062 if (NILP (string))
16063 break;
16064
16065 /* Remember where item was displayed. */
16066 AREF (items, i + 3) = make_number (it.hpos);
16067
16068 /* Display the item, pad with one space. */
16069 if (it.current_x < it.last_visible_x)
16070 display_string (NULL, string, Qnil, 0, 0, &it,
16071 SCHARS (string) + 1, 0, 0, -1);
16072 }
16073
16074 /* Fill out the line with spaces. */
16075 if (it.current_x < it.last_visible_x)
16076 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16077
16078 /* Compute the total height of the lines. */
16079 compute_line_metrics (&it);
16080 }
16081
16082
16083 \f
16084 /***********************************************************************
16085 Mode Line
16086 ***********************************************************************/
16087
16088 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16089 FORCE is non-zero, redisplay mode lines unconditionally.
16090 Otherwise, redisplay only mode lines that are garbaged. Value is
16091 the number of windows whose mode lines were redisplayed. */
16092
16093 static int
16094 redisplay_mode_lines (window, force)
16095 Lisp_Object window;
16096 int force;
16097 {
16098 int nwindows = 0;
16099
16100 while (!NILP (window))
16101 {
16102 struct window *w = XWINDOW (window);
16103
16104 if (WINDOWP (w->hchild))
16105 nwindows += redisplay_mode_lines (w->hchild, force);
16106 else if (WINDOWP (w->vchild))
16107 nwindows += redisplay_mode_lines (w->vchild, force);
16108 else if (force
16109 || FRAME_GARBAGED_P (XFRAME (w->frame))
16110 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16111 {
16112 struct text_pos lpoint;
16113 struct buffer *old = current_buffer;
16114
16115 /* Set the window's buffer for the mode line display. */
16116 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16117 set_buffer_internal_1 (XBUFFER (w->buffer));
16118
16119 /* Point refers normally to the selected window. For any
16120 other window, set up appropriate value. */
16121 if (!EQ (window, selected_window))
16122 {
16123 struct text_pos pt;
16124
16125 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16126 if (CHARPOS (pt) < BEGV)
16127 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16128 else if (CHARPOS (pt) > (ZV - 1))
16129 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16130 else
16131 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16132 }
16133
16134 /* Display mode lines. */
16135 clear_glyph_matrix (w->desired_matrix);
16136 if (display_mode_lines (w))
16137 {
16138 ++nwindows;
16139 w->must_be_updated_p = 1;
16140 }
16141
16142 /* Restore old settings. */
16143 set_buffer_internal_1 (old);
16144 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16145 }
16146
16147 window = w->next;
16148 }
16149
16150 return nwindows;
16151 }
16152
16153
16154 /* Display the mode and/or top line of window W. Value is the number
16155 of mode lines displayed. */
16156
16157 static int
16158 display_mode_lines (w)
16159 struct window *w;
16160 {
16161 Lisp_Object old_selected_window, old_selected_frame;
16162 int n = 0;
16163
16164 old_selected_frame = selected_frame;
16165 selected_frame = w->frame;
16166 old_selected_window = selected_window;
16167 XSETWINDOW (selected_window, w);
16168
16169 /* These will be set while the mode line specs are processed. */
16170 line_number_displayed = 0;
16171 w->column_number_displayed = Qnil;
16172
16173 if (WINDOW_WANTS_MODELINE_P (w))
16174 {
16175 struct window *sel_w = XWINDOW (old_selected_window);
16176
16177 /* Select mode line face based on the real selected window. */
16178 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16179 current_buffer->mode_line_format);
16180 ++n;
16181 }
16182
16183 if (WINDOW_WANTS_HEADER_LINE_P (w))
16184 {
16185 display_mode_line (w, HEADER_LINE_FACE_ID,
16186 current_buffer->header_line_format);
16187 ++n;
16188 }
16189
16190 selected_frame = old_selected_frame;
16191 selected_window = old_selected_window;
16192 return n;
16193 }
16194
16195
16196 /* Display mode or top line of window W. FACE_ID specifies which line
16197 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16198 FORMAT is the mode line format to display. Value is the pixel
16199 height of the mode line displayed. */
16200
16201 static int
16202 display_mode_line (w, face_id, format)
16203 struct window *w;
16204 enum face_id face_id;
16205 Lisp_Object format;
16206 {
16207 struct it it;
16208 struct face *face;
16209 int count = SPECPDL_INDEX ();
16210
16211 init_iterator (&it, w, -1, -1, NULL, face_id);
16212 prepare_desired_row (it.glyph_row);
16213
16214 it.glyph_row->mode_line_p = 1;
16215
16216 if (! mode_line_inverse_video)
16217 /* Force the mode-line to be displayed in the default face. */
16218 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16219
16220 record_unwind_protect (unwind_format_mode_line,
16221 format_mode_line_unwind_data (NULL, 0));
16222
16223 mode_line_target = MODE_LINE_DISPLAY;
16224
16225 /* Temporarily make frame's keyboard the current kboard so that
16226 kboard-local variables in the mode_line_format will get the right
16227 values. */
16228 push_frame_kboard (it.f);
16229 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16230 pop_frame_kboard ();
16231
16232 unbind_to (count, Qnil);
16233
16234 /* Fill up with spaces. */
16235 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16236
16237 compute_line_metrics (&it);
16238 it.glyph_row->full_width_p = 1;
16239 it.glyph_row->continued_p = 0;
16240 it.glyph_row->truncated_on_left_p = 0;
16241 it.glyph_row->truncated_on_right_p = 0;
16242
16243 /* Make a 3D mode-line have a shadow at its right end. */
16244 face = FACE_FROM_ID (it.f, face_id);
16245 extend_face_to_end_of_line (&it);
16246 if (face->box != FACE_NO_BOX)
16247 {
16248 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16249 + it.glyph_row->used[TEXT_AREA] - 1);
16250 last->right_box_line_p = 1;
16251 }
16252
16253 return it.glyph_row->height;
16254 }
16255
16256 /* Move element ELT in LIST to the front of LIST.
16257 Return the updated list. */
16258
16259 static Lisp_Object
16260 move_elt_to_front (elt, list)
16261 Lisp_Object elt, list;
16262 {
16263 register Lisp_Object tail, prev;
16264 register Lisp_Object tem;
16265
16266 tail = list;
16267 prev = Qnil;
16268 while (CONSP (tail))
16269 {
16270 tem = XCAR (tail);
16271
16272 if (EQ (elt, tem))
16273 {
16274 /* Splice out the link TAIL. */
16275 if (NILP (prev))
16276 list = XCDR (tail);
16277 else
16278 Fsetcdr (prev, XCDR (tail));
16279
16280 /* Now make it the first. */
16281 Fsetcdr (tail, list);
16282 return tail;
16283 }
16284 else
16285 prev = tail;
16286 tail = XCDR (tail);
16287 QUIT;
16288 }
16289
16290 /* Not found--return unchanged LIST. */
16291 return list;
16292 }
16293
16294 /* Contribute ELT to the mode line for window IT->w. How it
16295 translates into text depends on its data type.
16296
16297 IT describes the display environment in which we display, as usual.
16298
16299 DEPTH is the depth in recursion. It is used to prevent
16300 infinite recursion here.
16301
16302 FIELD_WIDTH is the number of characters the display of ELT should
16303 occupy in the mode line, and PRECISION is the maximum number of
16304 characters to display from ELT's representation. See
16305 display_string for details.
16306
16307 Returns the hpos of the end of the text generated by ELT.
16308
16309 PROPS is a property list to add to any string we encounter.
16310
16311 If RISKY is nonzero, remove (disregard) any properties in any string
16312 we encounter, and ignore :eval and :propertize.
16313
16314 The global variable `mode_line_target' determines whether the
16315 output is passed to `store_mode_line_noprop',
16316 `store_mode_line_string', or `display_string'. */
16317
16318 static int
16319 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16320 struct it *it;
16321 int depth;
16322 int field_width, precision;
16323 Lisp_Object elt, props;
16324 int risky;
16325 {
16326 int n = 0, field, prec;
16327 int literal = 0;
16328
16329 tail_recurse:
16330 if (depth > 100)
16331 elt = build_string ("*too-deep*");
16332
16333 depth++;
16334
16335 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16336 {
16337 case Lisp_String:
16338 {
16339 /* A string: output it and check for %-constructs within it. */
16340 unsigned char c;
16341 int offset = 0;
16342
16343 if (SCHARS (elt) > 0
16344 && (!NILP (props) || risky))
16345 {
16346 Lisp_Object oprops, aelt;
16347 oprops = Ftext_properties_at (make_number (0), elt);
16348
16349 /* If the starting string's properties are not what
16350 we want, translate the string. Also, if the string
16351 is risky, do that anyway. */
16352
16353 if (NILP (Fequal (props, oprops)) || risky)
16354 {
16355 /* If the starting string has properties,
16356 merge the specified ones onto the existing ones. */
16357 if (! NILP (oprops) && !risky)
16358 {
16359 Lisp_Object tem;
16360
16361 oprops = Fcopy_sequence (oprops);
16362 tem = props;
16363 while (CONSP (tem))
16364 {
16365 oprops = Fplist_put (oprops, XCAR (tem),
16366 XCAR (XCDR (tem)));
16367 tem = XCDR (XCDR (tem));
16368 }
16369 props = oprops;
16370 }
16371
16372 aelt = Fassoc (elt, mode_line_proptrans_alist);
16373 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16374 {
16375 /* AELT is what we want. Move it to the front
16376 without consing. */
16377 elt = XCAR (aelt);
16378 mode_line_proptrans_alist
16379 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16380 }
16381 else
16382 {
16383 Lisp_Object tem;
16384
16385 /* If AELT has the wrong props, it is useless.
16386 so get rid of it. */
16387 if (! NILP (aelt))
16388 mode_line_proptrans_alist
16389 = Fdelq (aelt, mode_line_proptrans_alist);
16390
16391 elt = Fcopy_sequence (elt);
16392 Fset_text_properties (make_number (0), Flength (elt),
16393 props, elt);
16394 /* Add this item to mode_line_proptrans_alist. */
16395 mode_line_proptrans_alist
16396 = Fcons (Fcons (elt, props),
16397 mode_line_proptrans_alist);
16398 /* Truncate mode_line_proptrans_alist
16399 to at most 50 elements. */
16400 tem = Fnthcdr (make_number (50),
16401 mode_line_proptrans_alist);
16402 if (! NILP (tem))
16403 XSETCDR (tem, Qnil);
16404 }
16405 }
16406 }
16407
16408 offset = 0;
16409
16410 if (literal)
16411 {
16412 prec = precision - n;
16413 switch (mode_line_target)
16414 {
16415 case MODE_LINE_NOPROP:
16416 case MODE_LINE_TITLE:
16417 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16418 break;
16419 case MODE_LINE_STRING:
16420 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16421 break;
16422 case MODE_LINE_DISPLAY:
16423 n += display_string (NULL, elt, Qnil, 0, 0, it,
16424 0, prec, 0, STRING_MULTIBYTE (elt));
16425 break;
16426 }
16427
16428 break;
16429 }
16430
16431 /* Handle the non-literal case. */
16432
16433 while ((precision <= 0 || n < precision)
16434 && SREF (elt, offset) != 0
16435 && (mode_line_target != MODE_LINE_DISPLAY
16436 || it->current_x < it->last_visible_x))
16437 {
16438 int last_offset = offset;
16439
16440 /* Advance to end of string or next format specifier. */
16441 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16442 ;
16443
16444 if (offset - 1 != last_offset)
16445 {
16446 int nchars, nbytes;
16447
16448 /* Output to end of string or up to '%'. Field width
16449 is length of string. Don't output more than
16450 PRECISION allows us. */
16451 offset--;
16452
16453 prec = c_string_width (SDATA (elt) + last_offset,
16454 offset - last_offset, precision - n,
16455 &nchars, &nbytes);
16456
16457 switch (mode_line_target)
16458 {
16459 case MODE_LINE_NOPROP:
16460 case MODE_LINE_TITLE:
16461 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16462 break;
16463 case MODE_LINE_STRING:
16464 {
16465 int bytepos = last_offset;
16466 int charpos = string_byte_to_char (elt, bytepos);
16467 int endpos = (precision <= 0
16468 ? string_byte_to_char (elt, offset)
16469 : charpos + nchars);
16470
16471 n += store_mode_line_string (NULL,
16472 Fsubstring (elt, make_number (charpos),
16473 make_number (endpos)),
16474 0, 0, 0, Qnil);
16475 }
16476 break;
16477 case MODE_LINE_DISPLAY:
16478 {
16479 int bytepos = last_offset;
16480 int charpos = string_byte_to_char (elt, bytepos);
16481
16482 if (precision <= 0)
16483 nchars = string_byte_to_char (elt, offset) - charpos;
16484 n += display_string (NULL, elt, Qnil, 0, charpos,
16485 it, 0, nchars, 0,
16486 STRING_MULTIBYTE (elt));
16487 }
16488 break;
16489 }
16490 }
16491 else /* c == '%' */
16492 {
16493 int percent_position = offset;
16494
16495 /* Get the specified minimum width. Zero means
16496 don't pad. */
16497 field = 0;
16498 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16499 field = field * 10 + c - '0';
16500
16501 /* Don't pad beyond the total padding allowed. */
16502 if (field_width - n > 0 && field > field_width - n)
16503 field = field_width - n;
16504
16505 /* Note that either PRECISION <= 0 or N < PRECISION. */
16506 prec = precision - n;
16507
16508 if (c == 'M')
16509 n += display_mode_element (it, depth, field, prec,
16510 Vglobal_mode_string, props,
16511 risky);
16512 else if (c != 0)
16513 {
16514 int multibyte;
16515 int bytepos, charpos;
16516 unsigned char *spec;
16517
16518 bytepos = percent_position;
16519 charpos = (STRING_MULTIBYTE (elt)
16520 ? string_byte_to_char (elt, bytepos)
16521 : bytepos);
16522
16523 spec
16524 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16525
16526 switch (mode_line_target)
16527 {
16528 case MODE_LINE_NOPROP:
16529 case MODE_LINE_TITLE:
16530 n += store_mode_line_noprop (spec, field, prec);
16531 break;
16532 case MODE_LINE_STRING:
16533 {
16534 int len = strlen (spec);
16535 Lisp_Object tem = make_string (spec, len);
16536 props = Ftext_properties_at (make_number (charpos), elt);
16537 /* Should only keep face property in props */
16538 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16539 }
16540 break;
16541 case MODE_LINE_DISPLAY:
16542 {
16543 int nglyphs_before, nwritten;
16544
16545 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16546 nwritten = display_string (spec, Qnil, elt,
16547 charpos, 0, it,
16548 field, prec, 0,
16549 multibyte);
16550
16551 /* Assign to the glyphs written above the
16552 string where the `%x' came from, position
16553 of the `%'. */
16554 if (nwritten > 0)
16555 {
16556 struct glyph *glyph
16557 = (it->glyph_row->glyphs[TEXT_AREA]
16558 + nglyphs_before);
16559 int i;
16560
16561 for (i = 0; i < nwritten; ++i)
16562 {
16563 glyph[i].object = elt;
16564 glyph[i].charpos = charpos;
16565 }
16566
16567 n += nwritten;
16568 }
16569 }
16570 break;
16571 }
16572 }
16573 else /* c == 0 */
16574 break;
16575 }
16576 }
16577 }
16578 break;
16579
16580 case Lisp_Symbol:
16581 /* A symbol: process the value of the symbol recursively
16582 as if it appeared here directly. Avoid error if symbol void.
16583 Special case: if value of symbol is a string, output the string
16584 literally. */
16585 {
16586 register Lisp_Object tem;
16587
16588 /* If the variable is not marked as risky to set
16589 then its contents are risky to use. */
16590 if (NILP (Fget (elt, Qrisky_local_variable)))
16591 risky = 1;
16592
16593 tem = Fboundp (elt);
16594 if (!NILP (tem))
16595 {
16596 tem = Fsymbol_value (elt);
16597 /* If value is a string, output that string literally:
16598 don't check for % within it. */
16599 if (STRINGP (tem))
16600 literal = 1;
16601
16602 if (!EQ (tem, elt))
16603 {
16604 /* Give up right away for nil or t. */
16605 elt = tem;
16606 goto tail_recurse;
16607 }
16608 }
16609 }
16610 break;
16611
16612 case Lisp_Cons:
16613 {
16614 register Lisp_Object car, tem;
16615
16616 /* A cons cell: five distinct cases.
16617 If first element is :eval or :propertize, do something special.
16618 If first element is a string or a cons, process all the elements
16619 and effectively concatenate them.
16620 If first element is a negative number, truncate displaying cdr to
16621 at most that many characters. If positive, pad (with spaces)
16622 to at least that many characters.
16623 If first element is a symbol, process the cadr or caddr recursively
16624 according to whether the symbol's value is non-nil or nil. */
16625 car = XCAR (elt);
16626 if (EQ (car, QCeval))
16627 {
16628 /* An element of the form (:eval FORM) means evaluate FORM
16629 and use the result as mode line elements. */
16630
16631 if (risky)
16632 break;
16633
16634 if (CONSP (XCDR (elt)))
16635 {
16636 Lisp_Object spec;
16637 spec = safe_eval (XCAR (XCDR (elt)));
16638 n += display_mode_element (it, depth, field_width - n,
16639 precision - n, spec, props,
16640 risky);
16641 }
16642 }
16643 else if (EQ (car, QCpropertize))
16644 {
16645 /* An element of the form (:propertize ELT PROPS...)
16646 means display ELT but applying properties PROPS. */
16647
16648 if (risky)
16649 break;
16650
16651 if (CONSP (XCDR (elt)))
16652 n += display_mode_element (it, depth, field_width - n,
16653 precision - n, XCAR (XCDR (elt)),
16654 XCDR (XCDR (elt)), risky);
16655 }
16656 else if (SYMBOLP (car))
16657 {
16658 tem = Fboundp (car);
16659 elt = XCDR (elt);
16660 if (!CONSP (elt))
16661 goto invalid;
16662 /* elt is now the cdr, and we know it is a cons cell.
16663 Use its car if CAR has a non-nil value. */
16664 if (!NILP (tem))
16665 {
16666 tem = Fsymbol_value (car);
16667 if (!NILP (tem))
16668 {
16669 elt = XCAR (elt);
16670 goto tail_recurse;
16671 }
16672 }
16673 /* Symbol's value is nil (or symbol is unbound)
16674 Get the cddr of the original list
16675 and if possible find the caddr and use that. */
16676 elt = XCDR (elt);
16677 if (NILP (elt))
16678 break;
16679 else if (!CONSP (elt))
16680 goto invalid;
16681 elt = XCAR (elt);
16682 goto tail_recurse;
16683 }
16684 else if (INTEGERP (car))
16685 {
16686 register int lim = XINT (car);
16687 elt = XCDR (elt);
16688 if (lim < 0)
16689 {
16690 /* Negative int means reduce maximum width. */
16691 if (precision <= 0)
16692 precision = -lim;
16693 else
16694 precision = min (precision, -lim);
16695 }
16696 else if (lim > 0)
16697 {
16698 /* Padding specified. Don't let it be more than
16699 current maximum. */
16700 if (precision > 0)
16701 lim = min (precision, lim);
16702
16703 /* If that's more padding than already wanted, queue it.
16704 But don't reduce padding already specified even if
16705 that is beyond the current truncation point. */
16706 field_width = max (lim, field_width);
16707 }
16708 goto tail_recurse;
16709 }
16710 else if (STRINGP (car) || CONSP (car))
16711 {
16712 register int limit = 50;
16713 /* Limit is to protect against circular lists. */
16714 while (CONSP (elt)
16715 && --limit > 0
16716 && (precision <= 0 || n < precision))
16717 {
16718 n += display_mode_element (it, depth,
16719 /* Do padding only after the last
16720 element in the list. */
16721 (! CONSP (XCDR (elt))
16722 ? field_width - n
16723 : 0),
16724 precision - n, XCAR (elt),
16725 props, risky);
16726 elt = XCDR (elt);
16727 }
16728 }
16729 }
16730 break;
16731
16732 default:
16733 invalid:
16734 elt = build_string ("*invalid*");
16735 goto tail_recurse;
16736 }
16737
16738 /* Pad to FIELD_WIDTH. */
16739 if (field_width > 0 && n < field_width)
16740 {
16741 switch (mode_line_target)
16742 {
16743 case MODE_LINE_NOPROP:
16744 case MODE_LINE_TITLE:
16745 n += store_mode_line_noprop ("", field_width - n, 0);
16746 break;
16747 case MODE_LINE_STRING:
16748 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16749 break;
16750 case MODE_LINE_DISPLAY:
16751 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16752 0, 0, 0);
16753 break;
16754 }
16755 }
16756
16757 return n;
16758 }
16759
16760 /* Store a mode-line string element in mode_line_string_list.
16761
16762 If STRING is non-null, display that C string. Otherwise, the Lisp
16763 string LISP_STRING is displayed.
16764
16765 FIELD_WIDTH is the minimum number of output glyphs to produce.
16766 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16767 with spaces. FIELD_WIDTH <= 0 means don't pad.
16768
16769 PRECISION is the maximum number of characters to output from
16770 STRING. PRECISION <= 0 means don't truncate the string.
16771
16772 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16773 properties to the string.
16774
16775 PROPS are the properties to add to the string.
16776 The mode_line_string_face face property is always added to the string.
16777 */
16778
16779 static int
16780 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16781 char *string;
16782 Lisp_Object lisp_string;
16783 int copy_string;
16784 int field_width;
16785 int precision;
16786 Lisp_Object props;
16787 {
16788 int len;
16789 int n = 0;
16790
16791 if (string != NULL)
16792 {
16793 len = strlen (string);
16794 if (precision > 0 && len > precision)
16795 len = precision;
16796 lisp_string = make_string (string, len);
16797 if (NILP (props))
16798 props = mode_line_string_face_prop;
16799 else if (!NILP (mode_line_string_face))
16800 {
16801 Lisp_Object face = Fplist_get (props, Qface);
16802 props = Fcopy_sequence (props);
16803 if (NILP (face))
16804 face = mode_line_string_face;
16805 else
16806 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16807 props = Fplist_put (props, Qface, face);
16808 }
16809 Fadd_text_properties (make_number (0), make_number (len),
16810 props, lisp_string);
16811 }
16812 else
16813 {
16814 len = XFASTINT (Flength (lisp_string));
16815 if (precision > 0 && len > precision)
16816 {
16817 len = precision;
16818 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16819 precision = -1;
16820 }
16821 if (!NILP (mode_line_string_face))
16822 {
16823 Lisp_Object face;
16824 if (NILP (props))
16825 props = Ftext_properties_at (make_number (0), lisp_string);
16826 face = Fplist_get (props, Qface);
16827 if (NILP (face))
16828 face = mode_line_string_face;
16829 else
16830 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16831 props = Fcons (Qface, Fcons (face, Qnil));
16832 if (copy_string)
16833 lisp_string = Fcopy_sequence (lisp_string);
16834 }
16835 if (!NILP (props))
16836 Fadd_text_properties (make_number (0), make_number (len),
16837 props, lisp_string);
16838 }
16839
16840 if (len > 0)
16841 {
16842 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16843 n += len;
16844 }
16845
16846 if (field_width > len)
16847 {
16848 field_width -= len;
16849 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16850 if (!NILP (props))
16851 Fadd_text_properties (make_number (0), make_number (field_width),
16852 props, lisp_string);
16853 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16854 n += field_width;
16855 }
16856
16857 return n;
16858 }
16859
16860
16861 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16862 1, 4, 0,
16863 doc: /* Format a string out of a mode line format specification.
16864 First arg FORMAT specifies the mode line format (see `mode-line-format'
16865 for details) to use.
16866
16867 Optional second arg FACE specifies the face property to put
16868 on all characters for which no face is specified.
16869 t means whatever face the window's mode line currently uses
16870 \(either `mode-line' or `mode-line-inactive', depending).
16871 nil means the default is no face property.
16872 If FACE is an integer, the value string has no text properties.
16873
16874 Optional third and fourth args WINDOW and BUFFER specify the window
16875 and buffer to use as the context for the formatting (defaults
16876 are the selected window and the window's buffer). */)
16877 (format, face, window, buffer)
16878 Lisp_Object format, face, window, buffer;
16879 {
16880 struct it it;
16881 int len;
16882 struct window *w;
16883 struct buffer *old_buffer = NULL;
16884 int face_id = -1;
16885 int no_props = INTEGERP (face);
16886 int count = SPECPDL_INDEX ();
16887 Lisp_Object str;
16888 int string_start = 0;
16889
16890 if (NILP (window))
16891 window = selected_window;
16892 CHECK_WINDOW (window);
16893 w = XWINDOW (window);
16894
16895 if (NILP (buffer))
16896 buffer = w->buffer;
16897 CHECK_BUFFER (buffer);
16898
16899 if (NILP (format))
16900 return build_string ("");
16901
16902 if (no_props)
16903 face = Qnil;
16904
16905 if (!NILP (face))
16906 {
16907 if (EQ (face, Qt))
16908 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16909 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16910 }
16911
16912 if (face_id < 0)
16913 face_id = DEFAULT_FACE_ID;
16914
16915 if (XBUFFER (buffer) != current_buffer)
16916 old_buffer = current_buffer;
16917
16918 /* Save things including mode_line_proptrans_alist,
16919 and set that to nil so that we don't alter the outer value. */
16920 record_unwind_protect (unwind_format_mode_line,
16921 format_mode_line_unwind_data (old_buffer, 1));
16922 mode_line_proptrans_alist = Qnil;
16923
16924 if (old_buffer)
16925 set_buffer_internal_1 (XBUFFER (buffer));
16926
16927 init_iterator (&it, w, -1, -1, NULL, face_id);
16928
16929 if (no_props)
16930 {
16931 mode_line_target = MODE_LINE_NOPROP;
16932 mode_line_string_face_prop = Qnil;
16933 mode_line_string_list = Qnil;
16934 string_start = MODE_LINE_NOPROP_LEN (0);
16935 }
16936 else
16937 {
16938 mode_line_target = MODE_LINE_STRING;
16939 mode_line_string_list = Qnil;
16940 mode_line_string_face = face;
16941 mode_line_string_face_prop
16942 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16943 }
16944
16945 push_frame_kboard (it.f);
16946 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16947 pop_frame_kboard ();
16948
16949 if (no_props)
16950 {
16951 len = MODE_LINE_NOPROP_LEN (string_start);
16952 str = make_string (mode_line_noprop_buf + string_start, len);
16953 }
16954 else
16955 {
16956 mode_line_string_list = Fnreverse (mode_line_string_list);
16957 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16958 make_string ("", 0));
16959 }
16960
16961 unbind_to (count, Qnil);
16962 return str;
16963 }
16964
16965 /* Write a null-terminated, right justified decimal representation of
16966 the positive integer D to BUF using a minimal field width WIDTH. */
16967
16968 static void
16969 pint2str (buf, width, d)
16970 register char *buf;
16971 register int width;
16972 register int d;
16973 {
16974 register char *p = buf;
16975
16976 if (d <= 0)
16977 *p++ = '0';
16978 else
16979 {
16980 while (d > 0)
16981 {
16982 *p++ = d % 10 + '0';
16983 d /= 10;
16984 }
16985 }
16986
16987 for (width -= (int) (p - buf); width > 0; --width)
16988 *p++ = ' ';
16989 *p-- = '\0';
16990 while (p > buf)
16991 {
16992 d = *buf;
16993 *buf++ = *p;
16994 *p-- = d;
16995 }
16996 }
16997
16998 /* Write a null-terminated, right justified decimal and "human
16999 readable" representation of the nonnegative integer D to BUF using
17000 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17001
17002 static const char power_letter[] =
17003 {
17004 0, /* not used */
17005 'k', /* kilo */
17006 'M', /* mega */
17007 'G', /* giga */
17008 'T', /* tera */
17009 'P', /* peta */
17010 'E', /* exa */
17011 'Z', /* zetta */
17012 'Y' /* yotta */
17013 };
17014
17015 static void
17016 pint2hrstr (buf, width, d)
17017 char *buf;
17018 int width;
17019 int d;
17020 {
17021 /* We aim to represent the nonnegative integer D as
17022 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17023 int quotient = d;
17024 int remainder = 0;
17025 /* -1 means: do not use TENTHS. */
17026 int tenths = -1;
17027 int exponent = 0;
17028
17029 /* Length of QUOTIENT.TENTHS as a string. */
17030 int length;
17031
17032 char * psuffix;
17033 char * p;
17034
17035 if (1000 <= quotient)
17036 {
17037 /* Scale to the appropriate EXPONENT. */
17038 do
17039 {
17040 remainder = quotient % 1000;
17041 quotient /= 1000;
17042 exponent++;
17043 }
17044 while (1000 <= quotient);
17045
17046 /* Round to nearest and decide whether to use TENTHS or not. */
17047 if (quotient <= 9)
17048 {
17049 tenths = remainder / 100;
17050 if (50 <= remainder % 100)
17051 {
17052 if (tenths < 9)
17053 tenths++;
17054 else
17055 {
17056 quotient++;
17057 if (quotient == 10)
17058 tenths = -1;
17059 else
17060 tenths = 0;
17061 }
17062 }
17063 }
17064 else
17065 if (500 <= remainder)
17066 {
17067 if (quotient < 999)
17068 quotient++;
17069 else
17070 {
17071 quotient = 1;
17072 exponent++;
17073 tenths = 0;
17074 }
17075 }
17076 }
17077
17078 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17079 if (tenths == -1 && quotient <= 99)
17080 if (quotient <= 9)
17081 length = 1;
17082 else
17083 length = 2;
17084 else
17085 length = 3;
17086 p = psuffix = buf + max (width, length);
17087
17088 /* Print EXPONENT. */
17089 if (exponent)
17090 *psuffix++ = power_letter[exponent];
17091 *psuffix = '\0';
17092
17093 /* Print TENTHS. */
17094 if (tenths >= 0)
17095 {
17096 *--p = '0' + tenths;
17097 *--p = '.';
17098 }
17099
17100 /* Print QUOTIENT. */
17101 do
17102 {
17103 int digit = quotient % 10;
17104 *--p = '0' + digit;
17105 }
17106 while ((quotient /= 10) != 0);
17107
17108 /* Print leading spaces. */
17109 while (buf < p)
17110 *--p = ' ';
17111 }
17112
17113 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17114 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17115 type of CODING_SYSTEM. Return updated pointer into BUF. */
17116
17117 static unsigned char invalid_eol_type[] = "(*invalid*)";
17118
17119 static char *
17120 decode_mode_spec_coding (coding_system, buf, eol_flag)
17121 Lisp_Object coding_system;
17122 register char *buf;
17123 int eol_flag;
17124 {
17125 Lisp_Object val;
17126 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17127 const unsigned char *eol_str;
17128 int eol_str_len;
17129 /* The EOL conversion we are using. */
17130 Lisp_Object eoltype;
17131
17132 val = Fget (coding_system, Qcoding_system);
17133 eoltype = Qnil;
17134
17135 if (!VECTORP (val)) /* Not yet decided. */
17136 {
17137 if (multibyte)
17138 *buf++ = '-';
17139 if (eol_flag)
17140 eoltype = eol_mnemonic_undecided;
17141 /* Don't mention EOL conversion if it isn't decided. */
17142 }
17143 else
17144 {
17145 Lisp_Object eolvalue;
17146
17147 eolvalue = Fget (coding_system, Qeol_type);
17148
17149 if (multibyte)
17150 *buf++ = XFASTINT (AREF (val, 1));
17151
17152 if (eol_flag)
17153 {
17154 /* The EOL conversion that is normal on this system. */
17155
17156 if (NILP (eolvalue)) /* Not yet decided. */
17157 eoltype = eol_mnemonic_undecided;
17158 else if (VECTORP (eolvalue)) /* Not yet decided. */
17159 eoltype = eol_mnemonic_undecided;
17160 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17161 eoltype = (XFASTINT (eolvalue) == 0
17162 ? eol_mnemonic_unix
17163 : (XFASTINT (eolvalue) == 1
17164 ? eol_mnemonic_dos : eol_mnemonic_mac));
17165 }
17166 }
17167
17168 if (eol_flag)
17169 {
17170 /* Mention the EOL conversion if it is not the usual one. */
17171 if (STRINGP (eoltype))
17172 {
17173 eol_str = SDATA (eoltype);
17174 eol_str_len = SBYTES (eoltype);
17175 }
17176 else if (INTEGERP (eoltype)
17177 && CHAR_VALID_P (XINT (eoltype), 0))
17178 {
17179 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17180 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17181 eol_str = tmp;
17182 }
17183 else
17184 {
17185 eol_str = invalid_eol_type;
17186 eol_str_len = sizeof (invalid_eol_type) - 1;
17187 }
17188 bcopy (eol_str, buf, eol_str_len);
17189 buf += eol_str_len;
17190 }
17191
17192 return buf;
17193 }
17194
17195 /* Return a string for the output of a mode line %-spec for window W,
17196 generated by character C. PRECISION >= 0 means don't return a
17197 string longer than that value. FIELD_WIDTH > 0 means pad the
17198 string returned with spaces to that value. Return 1 in *MULTIBYTE
17199 if the result is multibyte text.
17200
17201 Note we operate on the current buffer for most purposes,
17202 the exception being w->base_line_pos. */
17203
17204 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17205
17206 static char *
17207 decode_mode_spec (w, c, field_width, precision, multibyte)
17208 struct window *w;
17209 register int c;
17210 int field_width, precision;
17211 int *multibyte;
17212 {
17213 Lisp_Object obj;
17214 struct frame *f = XFRAME (WINDOW_FRAME (w));
17215 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17216 struct buffer *b = current_buffer;
17217
17218 obj = Qnil;
17219 *multibyte = 0;
17220
17221 switch (c)
17222 {
17223 case '*':
17224 if (!NILP (b->read_only))
17225 return "%";
17226 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17227 return "*";
17228 return "-";
17229
17230 case '+':
17231 /* This differs from %* only for a modified read-only buffer. */
17232 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17233 return "*";
17234 if (!NILP (b->read_only))
17235 return "%";
17236 return "-";
17237
17238 case '&':
17239 /* This differs from %* in ignoring read-only-ness. */
17240 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17241 return "*";
17242 return "-";
17243
17244 case '%':
17245 return "%";
17246
17247 case '[':
17248 {
17249 int i;
17250 char *p;
17251
17252 if (command_loop_level > 5)
17253 return "[[[... ";
17254 p = decode_mode_spec_buf;
17255 for (i = 0; i < command_loop_level; i++)
17256 *p++ = '[';
17257 *p = 0;
17258 return decode_mode_spec_buf;
17259 }
17260
17261 case ']':
17262 {
17263 int i;
17264 char *p;
17265
17266 if (command_loop_level > 5)
17267 return " ...]]]";
17268 p = decode_mode_spec_buf;
17269 for (i = 0; i < command_loop_level; i++)
17270 *p++ = ']';
17271 *p = 0;
17272 return decode_mode_spec_buf;
17273 }
17274
17275 case '-':
17276 {
17277 register int i;
17278
17279 /* Let lots_of_dashes be a string of infinite length. */
17280 if (mode_line_target == MODE_LINE_NOPROP ||
17281 mode_line_target == MODE_LINE_STRING)
17282 return "--";
17283 if (field_width <= 0
17284 || field_width > sizeof (lots_of_dashes))
17285 {
17286 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17287 decode_mode_spec_buf[i] = '-';
17288 decode_mode_spec_buf[i] = '\0';
17289 return decode_mode_spec_buf;
17290 }
17291 else
17292 return lots_of_dashes;
17293 }
17294
17295 case 'b':
17296 obj = b->name;
17297 break;
17298
17299 case 'c':
17300 {
17301 int col = (int) current_column (); /* iftc */
17302 w->column_number_displayed = make_number (col);
17303 pint2str (decode_mode_spec_buf, field_width, col);
17304 return decode_mode_spec_buf;
17305 }
17306
17307 case 'e':
17308 #ifndef SYSTEM_MALLOC
17309 {
17310 if (NILP (Vmemory_full))
17311 return "";
17312 else
17313 return "!MEM FULL! ";
17314 }
17315 #else
17316 return "";
17317 #endif
17318
17319 case 'F':
17320 /* %F displays the frame name. */
17321 if (!NILP (f->title))
17322 return (char *) SDATA (f->title);
17323 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17324 return (char *) SDATA (f->name);
17325 return "Emacs";
17326
17327 case 'f':
17328 obj = b->filename;
17329 break;
17330
17331 case 'i':
17332 {
17333 int size = ZV - BEGV;
17334 pint2str (decode_mode_spec_buf, field_width, size);
17335 return decode_mode_spec_buf;
17336 }
17337
17338 case 'I':
17339 {
17340 int size = ZV - BEGV;
17341 pint2hrstr (decode_mode_spec_buf, field_width, size);
17342 return decode_mode_spec_buf;
17343 }
17344
17345 case 'l':
17346 {
17347 int startpos = XMARKER (w->start)->charpos;
17348 int startpos_byte = marker_byte_position (w->start);
17349 int line, linepos, linepos_byte, topline;
17350 int nlines, junk;
17351 int height = WINDOW_TOTAL_LINES (w);
17352
17353 /* If we decided that this buffer isn't suitable for line numbers,
17354 don't forget that too fast. */
17355 if (EQ (w->base_line_pos, w->buffer))
17356 goto no_value;
17357 /* But do forget it, if the window shows a different buffer now. */
17358 else if (BUFFERP (w->base_line_pos))
17359 w->base_line_pos = Qnil;
17360
17361 /* If the buffer is very big, don't waste time. */
17362 if (INTEGERP (Vline_number_display_limit)
17363 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17364 {
17365 w->base_line_pos = Qnil;
17366 w->base_line_number = Qnil;
17367 goto no_value;
17368 }
17369
17370 if (!NILP (w->base_line_number)
17371 && !NILP (w->base_line_pos)
17372 && XFASTINT (w->base_line_pos) <= startpos)
17373 {
17374 line = XFASTINT (w->base_line_number);
17375 linepos = XFASTINT (w->base_line_pos);
17376 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17377 }
17378 else
17379 {
17380 line = 1;
17381 linepos = BUF_BEGV (b);
17382 linepos_byte = BUF_BEGV_BYTE (b);
17383 }
17384
17385 /* Count lines from base line to window start position. */
17386 nlines = display_count_lines (linepos, linepos_byte,
17387 startpos_byte,
17388 startpos, &junk);
17389
17390 topline = nlines + line;
17391
17392 /* Determine a new base line, if the old one is too close
17393 or too far away, or if we did not have one.
17394 "Too close" means it's plausible a scroll-down would
17395 go back past it. */
17396 if (startpos == BUF_BEGV (b))
17397 {
17398 w->base_line_number = make_number (topline);
17399 w->base_line_pos = make_number (BUF_BEGV (b));
17400 }
17401 else if (nlines < height + 25 || nlines > height * 3 + 50
17402 || linepos == BUF_BEGV (b))
17403 {
17404 int limit = BUF_BEGV (b);
17405 int limit_byte = BUF_BEGV_BYTE (b);
17406 int position;
17407 int distance = (height * 2 + 30) * line_number_display_limit_width;
17408
17409 if (startpos - distance > limit)
17410 {
17411 limit = startpos - distance;
17412 limit_byte = CHAR_TO_BYTE (limit);
17413 }
17414
17415 nlines = display_count_lines (startpos, startpos_byte,
17416 limit_byte,
17417 - (height * 2 + 30),
17418 &position);
17419 /* If we couldn't find the lines we wanted within
17420 line_number_display_limit_width chars per line,
17421 give up on line numbers for this window. */
17422 if (position == limit_byte && limit == startpos - distance)
17423 {
17424 w->base_line_pos = w->buffer;
17425 w->base_line_number = Qnil;
17426 goto no_value;
17427 }
17428
17429 w->base_line_number = make_number (topline - nlines);
17430 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17431 }
17432
17433 /* Now count lines from the start pos to point. */
17434 nlines = display_count_lines (startpos, startpos_byte,
17435 PT_BYTE, PT, &junk);
17436
17437 /* Record that we did display the line number. */
17438 line_number_displayed = 1;
17439
17440 /* Make the string to show. */
17441 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17442 return decode_mode_spec_buf;
17443 no_value:
17444 {
17445 char* p = decode_mode_spec_buf;
17446 int pad = field_width - 2;
17447 while (pad-- > 0)
17448 *p++ = ' ';
17449 *p++ = '?';
17450 *p++ = '?';
17451 *p = '\0';
17452 return decode_mode_spec_buf;
17453 }
17454 }
17455 break;
17456
17457 case 'm':
17458 obj = b->mode_name;
17459 break;
17460
17461 case 'n':
17462 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17463 return " Narrow";
17464 break;
17465
17466 case 'p':
17467 {
17468 int pos = marker_position (w->start);
17469 int total = BUF_ZV (b) - BUF_BEGV (b);
17470
17471 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17472 {
17473 if (pos <= BUF_BEGV (b))
17474 return "All";
17475 else
17476 return "Bottom";
17477 }
17478 else if (pos <= BUF_BEGV (b))
17479 return "Top";
17480 else
17481 {
17482 if (total > 1000000)
17483 /* Do it differently for a large value, to avoid overflow. */
17484 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17485 else
17486 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17487 /* We can't normally display a 3-digit number,
17488 so get us a 2-digit number that is close. */
17489 if (total == 100)
17490 total = 99;
17491 sprintf (decode_mode_spec_buf, "%2d%%", total);
17492 return decode_mode_spec_buf;
17493 }
17494 }
17495
17496 /* Display percentage of size above the bottom of the screen. */
17497 case 'P':
17498 {
17499 int toppos = marker_position (w->start);
17500 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17501 int total = BUF_ZV (b) - BUF_BEGV (b);
17502
17503 if (botpos >= BUF_ZV (b))
17504 {
17505 if (toppos <= BUF_BEGV (b))
17506 return "All";
17507 else
17508 return "Bottom";
17509 }
17510 else
17511 {
17512 if (total > 1000000)
17513 /* Do it differently for a large value, to avoid overflow. */
17514 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17515 else
17516 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17517 /* We can't normally display a 3-digit number,
17518 so get us a 2-digit number that is close. */
17519 if (total == 100)
17520 total = 99;
17521 if (toppos <= BUF_BEGV (b))
17522 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17523 else
17524 sprintf (decode_mode_spec_buf, "%2d%%", total);
17525 return decode_mode_spec_buf;
17526 }
17527 }
17528
17529 case 's':
17530 /* status of process */
17531 obj = Fget_buffer_process (Fcurrent_buffer ());
17532 if (NILP (obj))
17533 return "no process";
17534 #ifdef subprocesses
17535 obj = Fsymbol_name (Fprocess_status (obj));
17536 #endif
17537 break;
17538
17539 case 't': /* indicate TEXT or BINARY */
17540 #ifdef MODE_LINE_BINARY_TEXT
17541 return MODE_LINE_BINARY_TEXT (b);
17542 #else
17543 return "T";
17544 #endif
17545
17546 case 'z':
17547 /* coding-system (not including end-of-line format) */
17548 case 'Z':
17549 /* coding-system (including end-of-line type) */
17550 {
17551 int eol_flag = (c == 'Z');
17552 char *p = decode_mode_spec_buf;
17553
17554 if (! FRAME_WINDOW_P (f))
17555 {
17556 /* No need to mention EOL here--the terminal never needs
17557 to do EOL conversion. */
17558 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17559 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17560 }
17561 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17562 p, eol_flag);
17563
17564 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17565 #ifdef subprocesses
17566 obj = Fget_buffer_process (Fcurrent_buffer ());
17567 if (PROCESSP (obj))
17568 {
17569 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17570 p, eol_flag);
17571 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17572 p, eol_flag);
17573 }
17574 #endif /* subprocesses */
17575 #endif /* 0 */
17576 *p = 0;
17577 return decode_mode_spec_buf;
17578 }
17579 }
17580
17581 if (STRINGP (obj))
17582 {
17583 *multibyte = STRING_MULTIBYTE (obj);
17584 return (char *) SDATA (obj);
17585 }
17586 else
17587 return "";
17588 }
17589
17590
17591 /* Count up to COUNT lines starting from START / START_BYTE.
17592 But don't go beyond LIMIT_BYTE.
17593 Return the number of lines thus found (always nonnegative).
17594
17595 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17596
17597 static int
17598 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17599 int start, start_byte, limit_byte, count;
17600 int *byte_pos_ptr;
17601 {
17602 register unsigned char *cursor;
17603 unsigned char *base;
17604
17605 register int ceiling;
17606 register unsigned char *ceiling_addr;
17607 int orig_count = count;
17608
17609 /* If we are not in selective display mode,
17610 check only for newlines. */
17611 int selective_display = (!NILP (current_buffer->selective_display)
17612 && !INTEGERP (current_buffer->selective_display));
17613
17614 if (count > 0)
17615 {
17616 while (start_byte < limit_byte)
17617 {
17618 ceiling = BUFFER_CEILING_OF (start_byte);
17619 ceiling = min (limit_byte - 1, ceiling);
17620 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17621 base = (cursor = BYTE_POS_ADDR (start_byte));
17622 while (1)
17623 {
17624 if (selective_display)
17625 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17626 ;
17627 else
17628 while (*cursor != '\n' && ++cursor != ceiling_addr)
17629 ;
17630
17631 if (cursor != ceiling_addr)
17632 {
17633 if (--count == 0)
17634 {
17635 start_byte += cursor - base + 1;
17636 *byte_pos_ptr = start_byte;
17637 return orig_count;
17638 }
17639 else
17640 if (++cursor == ceiling_addr)
17641 break;
17642 }
17643 else
17644 break;
17645 }
17646 start_byte += cursor - base;
17647 }
17648 }
17649 else
17650 {
17651 while (start_byte > limit_byte)
17652 {
17653 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17654 ceiling = max (limit_byte, ceiling);
17655 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17656 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17657 while (1)
17658 {
17659 if (selective_display)
17660 while (--cursor != ceiling_addr
17661 && *cursor != '\n' && *cursor != 015)
17662 ;
17663 else
17664 while (--cursor != ceiling_addr && *cursor != '\n')
17665 ;
17666
17667 if (cursor != ceiling_addr)
17668 {
17669 if (++count == 0)
17670 {
17671 start_byte += cursor - base + 1;
17672 *byte_pos_ptr = start_byte;
17673 /* When scanning backwards, we should
17674 not count the newline posterior to which we stop. */
17675 return - orig_count - 1;
17676 }
17677 }
17678 else
17679 break;
17680 }
17681 /* Here we add 1 to compensate for the last decrement
17682 of CURSOR, which took it past the valid range. */
17683 start_byte += cursor - base + 1;
17684 }
17685 }
17686
17687 *byte_pos_ptr = limit_byte;
17688
17689 if (count < 0)
17690 return - orig_count + count;
17691 return orig_count - count;
17692
17693 }
17694
17695
17696 \f
17697 /***********************************************************************
17698 Displaying strings
17699 ***********************************************************************/
17700
17701 /* Display a NUL-terminated string, starting with index START.
17702
17703 If STRING is non-null, display that C string. Otherwise, the Lisp
17704 string LISP_STRING is displayed.
17705
17706 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17707 FACE_STRING. Display STRING or LISP_STRING with the face at
17708 FACE_STRING_POS in FACE_STRING:
17709
17710 Display the string in the environment given by IT, but use the
17711 standard display table, temporarily.
17712
17713 FIELD_WIDTH is the minimum number of output glyphs to produce.
17714 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17715 with spaces. If STRING has more characters, more than FIELD_WIDTH
17716 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17717
17718 PRECISION is the maximum number of characters to output from
17719 STRING. PRECISION < 0 means don't truncate the string.
17720
17721 This is roughly equivalent to printf format specifiers:
17722
17723 FIELD_WIDTH PRECISION PRINTF
17724 ----------------------------------------
17725 -1 -1 %s
17726 -1 10 %.10s
17727 10 -1 %10s
17728 20 10 %20.10s
17729
17730 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17731 display them, and < 0 means obey the current buffer's value of
17732 enable_multibyte_characters.
17733
17734 Value is the number of columns displayed. */
17735
17736 static int
17737 display_string (string, lisp_string, face_string, face_string_pos,
17738 start, it, field_width, precision, max_x, multibyte)
17739 unsigned char *string;
17740 Lisp_Object lisp_string;
17741 Lisp_Object face_string;
17742 int face_string_pos;
17743 int start;
17744 struct it *it;
17745 int field_width, precision, max_x;
17746 int multibyte;
17747 {
17748 int hpos_at_start = it->hpos;
17749 int saved_face_id = it->face_id;
17750 struct glyph_row *row = it->glyph_row;
17751
17752 /* Initialize the iterator IT for iteration over STRING beginning
17753 with index START. */
17754 reseat_to_string (it, string, lisp_string, start,
17755 precision, field_width, multibyte);
17756
17757 /* If displaying STRING, set up the face of the iterator
17758 from LISP_STRING, if that's given. */
17759 if (STRINGP (face_string))
17760 {
17761 int endptr;
17762 struct face *face;
17763
17764 it->face_id
17765 = face_at_string_position (it->w, face_string, face_string_pos,
17766 0, it->region_beg_charpos,
17767 it->region_end_charpos,
17768 &endptr, it->base_face_id, 0);
17769 face = FACE_FROM_ID (it->f, it->face_id);
17770 it->face_box_p = face->box != FACE_NO_BOX;
17771 }
17772
17773 /* Set max_x to the maximum allowed X position. Don't let it go
17774 beyond the right edge of the window. */
17775 if (max_x <= 0)
17776 max_x = it->last_visible_x;
17777 else
17778 max_x = min (max_x, it->last_visible_x);
17779
17780 /* Skip over display elements that are not visible. because IT->w is
17781 hscrolled. */
17782 if (it->current_x < it->first_visible_x)
17783 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17784 MOVE_TO_POS | MOVE_TO_X);
17785
17786 row->ascent = it->max_ascent;
17787 row->height = it->max_ascent + it->max_descent;
17788 row->phys_ascent = it->max_phys_ascent;
17789 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17790 row->extra_line_spacing = it->max_extra_line_spacing;
17791
17792 /* This condition is for the case that we are called with current_x
17793 past last_visible_x. */
17794 while (it->current_x < max_x)
17795 {
17796 int x_before, x, n_glyphs_before, i, nglyphs;
17797
17798 /* Get the next display element. */
17799 if (!get_next_display_element (it))
17800 break;
17801
17802 /* Produce glyphs. */
17803 x_before = it->current_x;
17804 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17805 PRODUCE_GLYPHS (it);
17806
17807 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17808 i = 0;
17809 x = x_before;
17810 while (i < nglyphs)
17811 {
17812 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17813
17814 if (!it->truncate_lines_p
17815 && x + glyph->pixel_width > max_x)
17816 {
17817 /* End of continued line or max_x reached. */
17818 if (CHAR_GLYPH_PADDING_P (*glyph))
17819 {
17820 /* A wide character is unbreakable. */
17821 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17822 it->current_x = x_before;
17823 }
17824 else
17825 {
17826 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17827 it->current_x = x;
17828 }
17829 break;
17830 }
17831 else if (x + glyph->pixel_width > it->first_visible_x)
17832 {
17833 /* Glyph is at least partially visible. */
17834 ++it->hpos;
17835 if (x < it->first_visible_x)
17836 it->glyph_row->x = x - it->first_visible_x;
17837 }
17838 else
17839 {
17840 /* Glyph is off the left margin of the display area.
17841 Should not happen. */
17842 abort ();
17843 }
17844
17845 row->ascent = max (row->ascent, it->max_ascent);
17846 row->height = max (row->height, it->max_ascent + it->max_descent);
17847 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17848 row->phys_height = max (row->phys_height,
17849 it->max_phys_ascent + it->max_phys_descent);
17850 row->extra_line_spacing = max (row->extra_line_spacing,
17851 it->max_extra_line_spacing);
17852 x += glyph->pixel_width;
17853 ++i;
17854 }
17855
17856 /* Stop if max_x reached. */
17857 if (i < nglyphs)
17858 break;
17859
17860 /* Stop at line ends. */
17861 if (ITERATOR_AT_END_OF_LINE_P (it))
17862 {
17863 it->continuation_lines_width = 0;
17864 break;
17865 }
17866
17867 set_iterator_to_next (it, 1);
17868
17869 /* Stop if truncating at the right edge. */
17870 if (it->truncate_lines_p
17871 && it->current_x >= it->last_visible_x)
17872 {
17873 /* Add truncation mark, but don't do it if the line is
17874 truncated at a padding space. */
17875 if (IT_CHARPOS (*it) < it->string_nchars)
17876 {
17877 if (!FRAME_WINDOW_P (it->f))
17878 {
17879 int i, n;
17880
17881 if (it->current_x > it->last_visible_x)
17882 {
17883 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17884 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17885 break;
17886 for (n = row->used[TEXT_AREA]; i < n; ++i)
17887 {
17888 row->used[TEXT_AREA] = i;
17889 produce_special_glyphs (it, IT_TRUNCATION);
17890 }
17891 }
17892 produce_special_glyphs (it, IT_TRUNCATION);
17893 }
17894 it->glyph_row->truncated_on_right_p = 1;
17895 }
17896 break;
17897 }
17898 }
17899
17900 /* Maybe insert a truncation at the left. */
17901 if (it->first_visible_x
17902 && IT_CHARPOS (*it) > 0)
17903 {
17904 if (!FRAME_WINDOW_P (it->f))
17905 insert_left_trunc_glyphs (it);
17906 it->glyph_row->truncated_on_left_p = 1;
17907 }
17908
17909 it->face_id = saved_face_id;
17910
17911 /* Value is number of columns displayed. */
17912 return it->hpos - hpos_at_start;
17913 }
17914
17915
17916 \f
17917 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17918 appears as an element of LIST or as the car of an element of LIST.
17919 If PROPVAL is a list, compare each element against LIST in that
17920 way, and return 1/2 if any element of PROPVAL is found in LIST.
17921 Otherwise return 0. This function cannot quit.
17922 The return value is 2 if the text is invisible but with an ellipsis
17923 and 1 if it's invisible and without an ellipsis. */
17924
17925 int
17926 invisible_p (propval, list)
17927 register Lisp_Object propval;
17928 Lisp_Object list;
17929 {
17930 register Lisp_Object tail, proptail;
17931
17932 for (tail = list; CONSP (tail); tail = XCDR (tail))
17933 {
17934 register Lisp_Object tem;
17935 tem = XCAR (tail);
17936 if (EQ (propval, tem))
17937 return 1;
17938 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17939 return NILP (XCDR (tem)) ? 1 : 2;
17940 }
17941
17942 if (CONSP (propval))
17943 {
17944 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17945 {
17946 Lisp_Object propelt;
17947 propelt = XCAR (proptail);
17948 for (tail = list; CONSP (tail); tail = XCDR (tail))
17949 {
17950 register Lisp_Object tem;
17951 tem = XCAR (tail);
17952 if (EQ (propelt, tem))
17953 return 1;
17954 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17955 return NILP (XCDR (tem)) ? 1 : 2;
17956 }
17957 }
17958 }
17959
17960 return 0;
17961 }
17962
17963 /* Calculate a width or height in pixels from a specification using
17964 the following elements:
17965
17966 SPEC ::=
17967 NUM - a (fractional) multiple of the default font width/height
17968 (NUM) - specifies exactly NUM pixels
17969 UNIT - a fixed number of pixels, see below.
17970 ELEMENT - size of a display element in pixels, see below.
17971 (NUM . SPEC) - equals NUM * SPEC
17972 (+ SPEC SPEC ...) - add pixel values
17973 (- SPEC SPEC ...) - subtract pixel values
17974 (- SPEC) - negate pixel value
17975
17976 NUM ::=
17977 INT or FLOAT - a number constant
17978 SYMBOL - use symbol's (buffer local) variable binding.
17979
17980 UNIT ::=
17981 in - pixels per inch *)
17982 mm - pixels per 1/1000 meter *)
17983 cm - pixels per 1/100 meter *)
17984 width - width of current font in pixels.
17985 height - height of current font in pixels.
17986
17987 *) using the ratio(s) defined in display-pixels-per-inch.
17988
17989 ELEMENT ::=
17990
17991 left-fringe - left fringe width in pixels
17992 right-fringe - right fringe width in pixels
17993
17994 left-margin - left margin width in pixels
17995 right-margin - right margin width in pixels
17996
17997 scroll-bar - scroll-bar area width in pixels
17998
17999 Examples:
18000
18001 Pixels corresponding to 5 inches:
18002 (5 . in)
18003
18004 Total width of non-text areas on left side of window (if scroll-bar is on left):
18005 '(space :width (+ left-fringe left-margin scroll-bar))
18006
18007 Align to first text column (in header line):
18008 '(space :align-to 0)
18009
18010 Align to middle of text area minus half the width of variable `my-image'
18011 containing a loaded image:
18012 '(space :align-to (0.5 . (- text my-image)))
18013
18014 Width of left margin minus width of 1 character in the default font:
18015 '(space :width (- left-margin 1))
18016
18017 Width of left margin minus width of 2 characters in the current font:
18018 '(space :width (- left-margin (2 . width)))
18019
18020 Center 1 character over left-margin (in header line):
18021 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18022
18023 Different ways to express width of left fringe plus left margin minus one pixel:
18024 '(space :width (- (+ left-fringe left-margin) (1)))
18025 '(space :width (+ left-fringe left-margin (- (1))))
18026 '(space :width (+ left-fringe left-margin (-1)))
18027
18028 */
18029
18030 #define NUMVAL(X) \
18031 ((INTEGERP (X) || FLOATP (X)) \
18032 ? XFLOATINT (X) \
18033 : - 1)
18034
18035 int
18036 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18037 double *res;
18038 struct it *it;
18039 Lisp_Object prop;
18040 void *font;
18041 int width_p, *align_to;
18042 {
18043 double pixels;
18044
18045 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18046 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18047
18048 if (NILP (prop))
18049 return OK_PIXELS (0);
18050
18051 if (SYMBOLP (prop))
18052 {
18053 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18054 {
18055 char *unit = SDATA (SYMBOL_NAME (prop));
18056
18057 if (unit[0] == 'i' && unit[1] == 'n')
18058 pixels = 1.0;
18059 else if (unit[0] == 'm' && unit[1] == 'm')
18060 pixels = 25.4;
18061 else if (unit[0] == 'c' && unit[1] == 'm')
18062 pixels = 2.54;
18063 else
18064 pixels = 0;
18065 if (pixels > 0)
18066 {
18067 double ppi;
18068 #ifdef HAVE_WINDOW_SYSTEM
18069 if (FRAME_WINDOW_P (it->f)
18070 && (ppi = (width_p
18071 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18072 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18073 ppi > 0))
18074 return OK_PIXELS (ppi / pixels);
18075 #endif
18076
18077 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18078 || (CONSP (Vdisplay_pixels_per_inch)
18079 && (ppi = (width_p
18080 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18081 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18082 ppi > 0)))
18083 return OK_PIXELS (ppi / pixels);
18084
18085 return 0;
18086 }
18087 }
18088
18089 #ifdef HAVE_WINDOW_SYSTEM
18090 if (EQ (prop, Qheight))
18091 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18092 if (EQ (prop, Qwidth))
18093 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18094 #else
18095 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18096 return OK_PIXELS (1);
18097 #endif
18098
18099 if (EQ (prop, Qtext))
18100 return OK_PIXELS (width_p
18101 ? window_box_width (it->w, TEXT_AREA)
18102 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18103
18104 if (align_to && *align_to < 0)
18105 {
18106 *res = 0;
18107 if (EQ (prop, Qleft))
18108 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18109 if (EQ (prop, Qright))
18110 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18111 if (EQ (prop, Qcenter))
18112 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18113 + window_box_width (it->w, TEXT_AREA) / 2);
18114 if (EQ (prop, Qleft_fringe))
18115 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18116 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18117 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18118 if (EQ (prop, Qright_fringe))
18119 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18120 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18121 : window_box_right_offset (it->w, TEXT_AREA));
18122 if (EQ (prop, Qleft_margin))
18123 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18124 if (EQ (prop, Qright_margin))
18125 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18126 if (EQ (prop, Qscroll_bar))
18127 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18128 ? 0
18129 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18130 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18131 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18132 : 0)));
18133 }
18134 else
18135 {
18136 if (EQ (prop, Qleft_fringe))
18137 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18138 if (EQ (prop, Qright_fringe))
18139 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18140 if (EQ (prop, Qleft_margin))
18141 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18142 if (EQ (prop, Qright_margin))
18143 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18144 if (EQ (prop, Qscroll_bar))
18145 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18146 }
18147
18148 prop = Fbuffer_local_value (prop, it->w->buffer);
18149 }
18150
18151 if (INTEGERP (prop) || FLOATP (prop))
18152 {
18153 int base_unit = (width_p
18154 ? FRAME_COLUMN_WIDTH (it->f)
18155 : FRAME_LINE_HEIGHT (it->f));
18156 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18157 }
18158
18159 if (CONSP (prop))
18160 {
18161 Lisp_Object car = XCAR (prop);
18162 Lisp_Object cdr = XCDR (prop);
18163
18164 if (SYMBOLP (car))
18165 {
18166 #ifdef HAVE_WINDOW_SYSTEM
18167 if (valid_image_p (prop))
18168 {
18169 int id = lookup_image (it->f, prop);
18170 struct image *img = IMAGE_FROM_ID (it->f, id);
18171
18172 return OK_PIXELS (width_p ? img->width : img->height);
18173 }
18174 #endif
18175 if (EQ (car, Qplus) || EQ (car, Qminus))
18176 {
18177 int first = 1;
18178 double px;
18179
18180 pixels = 0;
18181 while (CONSP (cdr))
18182 {
18183 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18184 font, width_p, align_to))
18185 return 0;
18186 if (first)
18187 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18188 else
18189 pixels += px;
18190 cdr = XCDR (cdr);
18191 }
18192 if (EQ (car, Qminus))
18193 pixels = -pixels;
18194 return OK_PIXELS (pixels);
18195 }
18196
18197 car = Fbuffer_local_value (car, it->w->buffer);
18198 }
18199
18200 if (INTEGERP (car) || FLOATP (car))
18201 {
18202 double fact;
18203 pixels = XFLOATINT (car);
18204 if (NILP (cdr))
18205 return OK_PIXELS (pixels);
18206 if (calc_pixel_width_or_height (&fact, it, cdr,
18207 font, width_p, align_to))
18208 return OK_PIXELS (pixels * fact);
18209 return 0;
18210 }
18211
18212 return 0;
18213 }
18214
18215 return 0;
18216 }
18217
18218 \f
18219 /***********************************************************************
18220 Glyph Display
18221 ***********************************************************************/
18222
18223 #ifdef HAVE_WINDOW_SYSTEM
18224
18225 #if GLYPH_DEBUG
18226
18227 void
18228 dump_glyph_string (s)
18229 struct glyph_string *s;
18230 {
18231 fprintf (stderr, "glyph string\n");
18232 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18233 s->x, s->y, s->width, s->height);
18234 fprintf (stderr, " ybase = %d\n", s->ybase);
18235 fprintf (stderr, " hl = %d\n", s->hl);
18236 fprintf (stderr, " left overhang = %d, right = %d\n",
18237 s->left_overhang, s->right_overhang);
18238 fprintf (stderr, " nchars = %d\n", s->nchars);
18239 fprintf (stderr, " extends to end of line = %d\n",
18240 s->extends_to_end_of_line_p);
18241 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18242 fprintf (stderr, " bg width = %d\n", s->background_width);
18243 }
18244
18245 #endif /* GLYPH_DEBUG */
18246
18247 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18248 of XChar2b structures for S; it can't be allocated in
18249 init_glyph_string because it must be allocated via `alloca'. W
18250 is the window on which S is drawn. ROW and AREA are the glyph row
18251 and area within the row from which S is constructed. START is the
18252 index of the first glyph structure covered by S. HL is a
18253 face-override for drawing S. */
18254
18255 #ifdef HAVE_NTGUI
18256 #define OPTIONAL_HDC(hdc) hdc,
18257 #define DECLARE_HDC(hdc) HDC hdc;
18258 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18259 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18260 #endif
18261
18262 #ifndef OPTIONAL_HDC
18263 #define OPTIONAL_HDC(hdc)
18264 #define DECLARE_HDC(hdc)
18265 #define ALLOCATE_HDC(hdc, f)
18266 #define RELEASE_HDC(hdc, f)
18267 #endif
18268
18269 static void
18270 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18271 struct glyph_string *s;
18272 DECLARE_HDC (hdc)
18273 XChar2b *char2b;
18274 struct window *w;
18275 struct glyph_row *row;
18276 enum glyph_row_area area;
18277 int start;
18278 enum draw_glyphs_face hl;
18279 {
18280 bzero (s, sizeof *s);
18281 s->w = w;
18282 s->f = XFRAME (w->frame);
18283 #ifdef HAVE_NTGUI
18284 s->hdc = hdc;
18285 #endif
18286 s->display = FRAME_X_DISPLAY (s->f);
18287 s->window = FRAME_X_WINDOW (s->f);
18288 s->char2b = char2b;
18289 s->hl = hl;
18290 s->row = row;
18291 s->area = area;
18292 s->first_glyph = row->glyphs[area] + start;
18293 s->height = row->height;
18294 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18295
18296 /* Display the internal border below the tool-bar window. */
18297 if (WINDOWP (s->f->tool_bar_window)
18298 && s->w == XWINDOW (s->f->tool_bar_window))
18299 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18300
18301 s->ybase = s->y + row->ascent;
18302 }
18303
18304
18305 /* Append the list of glyph strings with head H and tail T to the list
18306 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18307
18308 static INLINE void
18309 append_glyph_string_lists (head, tail, h, t)
18310 struct glyph_string **head, **tail;
18311 struct glyph_string *h, *t;
18312 {
18313 if (h)
18314 {
18315 if (*head)
18316 (*tail)->next = h;
18317 else
18318 *head = h;
18319 h->prev = *tail;
18320 *tail = t;
18321 }
18322 }
18323
18324
18325 /* Prepend the list of glyph strings with head H and tail T to the
18326 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18327 result. */
18328
18329 static INLINE void
18330 prepend_glyph_string_lists (head, tail, h, t)
18331 struct glyph_string **head, **tail;
18332 struct glyph_string *h, *t;
18333 {
18334 if (h)
18335 {
18336 if (*head)
18337 (*head)->prev = t;
18338 else
18339 *tail = t;
18340 t->next = *head;
18341 *head = h;
18342 }
18343 }
18344
18345
18346 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18347 Set *HEAD and *TAIL to the resulting list. */
18348
18349 static INLINE void
18350 append_glyph_string (head, tail, s)
18351 struct glyph_string **head, **tail;
18352 struct glyph_string *s;
18353 {
18354 s->next = s->prev = NULL;
18355 append_glyph_string_lists (head, tail, s, s);
18356 }
18357
18358
18359 /* Get face and two-byte form of character glyph GLYPH on frame F.
18360 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18361 a pointer to a realized face that is ready for display. */
18362
18363 static INLINE struct face *
18364 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18365 struct frame *f;
18366 struct glyph *glyph;
18367 XChar2b *char2b;
18368 int *two_byte_p;
18369 {
18370 struct face *face;
18371
18372 xassert (glyph->type == CHAR_GLYPH);
18373 face = FACE_FROM_ID (f, glyph->face_id);
18374
18375 if (two_byte_p)
18376 *two_byte_p = 0;
18377
18378 if (!glyph->multibyte_p)
18379 {
18380 /* Unibyte case. We don't have to encode, but we have to make
18381 sure to use a face suitable for unibyte. */
18382 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18383 }
18384 else if (glyph->u.ch < 128
18385 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
18386 {
18387 /* Case of ASCII in a face known to fit ASCII. */
18388 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18389 }
18390 else
18391 {
18392 int c1, c2, charset;
18393
18394 /* Split characters into bytes. If c2 is -1 afterwards, C is
18395 really a one-byte character so that byte1 is zero. */
18396 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18397 if (c2 > 0)
18398 STORE_XCHAR2B (char2b, c1, c2);
18399 else
18400 STORE_XCHAR2B (char2b, 0, c1);
18401
18402 /* Maybe encode the character in *CHAR2B. */
18403 if (charset != CHARSET_ASCII)
18404 {
18405 struct font_info *font_info
18406 = FONT_INFO_FROM_ID (f, face->font_info_id);
18407 if (font_info)
18408 glyph->font_type
18409 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18410 }
18411 }
18412
18413 /* Make sure X resources of the face are allocated. */
18414 xassert (face != NULL);
18415 PREPARE_FACE_FOR_DISPLAY (f, face);
18416 return face;
18417 }
18418
18419
18420 /* Fill glyph string S with composition components specified by S->cmp.
18421
18422 FACES is an array of faces for all components of this composition.
18423 S->gidx is the index of the first component for S.
18424
18425 OVERLAPS non-zero means S should draw the foreground only, and use
18426 its physical height for clipping. See also draw_glyphs.
18427
18428 Value is the index of a component not in S. */
18429
18430 static int
18431 fill_composite_glyph_string (s, faces, overlaps)
18432 struct glyph_string *s;
18433 struct face **faces;
18434 int overlaps;
18435 {
18436 int i;
18437
18438 xassert (s);
18439
18440 s->for_overlaps = overlaps;
18441
18442 s->face = faces[s->gidx];
18443 s->font = s->face->font;
18444 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18445
18446 /* For all glyphs of this composition, starting at the offset
18447 S->gidx, until we reach the end of the definition or encounter a
18448 glyph that requires the different face, add it to S. */
18449 ++s->nchars;
18450 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18451 ++s->nchars;
18452
18453 /* All glyph strings for the same composition has the same width,
18454 i.e. the width set for the first component of the composition. */
18455
18456 s->width = s->first_glyph->pixel_width;
18457
18458 /* If the specified font could not be loaded, use the frame's
18459 default font, but record the fact that we couldn't load it in
18460 the glyph string so that we can draw rectangles for the
18461 characters of the glyph string. */
18462 if (s->font == NULL)
18463 {
18464 s->font_not_found_p = 1;
18465 s->font = FRAME_FONT (s->f);
18466 }
18467
18468 /* Adjust base line for subscript/superscript text. */
18469 s->ybase += s->first_glyph->voffset;
18470
18471 xassert (s->face && s->face->gc);
18472
18473 /* This glyph string must always be drawn with 16-bit functions. */
18474 s->two_byte_p = 1;
18475
18476 return s->gidx + s->nchars;
18477 }
18478
18479
18480 /* Fill glyph string S from a sequence of character glyphs.
18481
18482 FACE_ID is the face id of the string. START is the index of the
18483 first glyph to consider, END is the index of the last + 1.
18484 OVERLAPS non-zero means S should draw the foreground only, and use
18485 its physical height for clipping. See also draw_glyphs.
18486
18487 Value is the index of the first glyph not in S. */
18488
18489 static int
18490 fill_glyph_string (s, face_id, start, end, overlaps)
18491 struct glyph_string *s;
18492 int face_id;
18493 int start, end, overlaps;
18494 {
18495 struct glyph *glyph, *last;
18496 int voffset;
18497 int glyph_not_available_p;
18498
18499 xassert (s->f == XFRAME (s->w->frame));
18500 xassert (s->nchars == 0);
18501 xassert (start >= 0 && end > start);
18502
18503 s->for_overlaps = overlaps,
18504 glyph = s->row->glyphs[s->area] + start;
18505 last = s->row->glyphs[s->area] + end;
18506 voffset = glyph->voffset;
18507
18508 glyph_not_available_p = glyph->glyph_not_available_p;
18509
18510 while (glyph < last
18511 && glyph->type == CHAR_GLYPH
18512 && glyph->voffset == voffset
18513 /* Same face id implies same font, nowadays. */
18514 && glyph->face_id == face_id
18515 && glyph->glyph_not_available_p == glyph_not_available_p)
18516 {
18517 int two_byte_p;
18518
18519 s->face = get_glyph_face_and_encoding (s->f, glyph,
18520 s->char2b + s->nchars,
18521 &two_byte_p);
18522 s->two_byte_p = two_byte_p;
18523 ++s->nchars;
18524 xassert (s->nchars <= end - start);
18525 s->width += glyph->pixel_width;
18526 ++glyph;
18527 }
18528
18529 s->font = s->face->font;
18530 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18531
18532 /* If the specified font could not be loaded, use the frame's font,
18533 but record the fact that we couldn't load it in
18534 S->font_not_found_p so that we can draw rectangles for the
18535 characters of the glyph string. */
18536 if (s->font == NULL || glyph_not_available_p)
18537 {
18538 s->font_not_found_p = 1;
18539 s->font = FRAME_FONT (s->f);
18540 }
18541
18542 /* Adjust base line for subscript/superscript text. */
18543 s->ybase += voffset;
18544
18545 xassert (s->face && s->face->gc);
18546 return glyph - s->row->glyphs[s->area];
18547 }
18548
18549
18550 /* Fill glyph string S from image glyph S->first_glyph. */
18551
18552 static void
18553 fill_image_glyph_string (s)
18554 struct glyph_string *s;
18555 {
18556 xassert (s->first_glyph->type == IMAGE_GLYPH);
18557 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18558 xassert (s->img);
18559 s->slice = s->first_glyph->slice;
18560 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18561 s->font = s->face->font;
18562 s->width = s->first_glyph->pixel_width;
18563
18564 /* Adjust base line for subscript/superscript text. */
18565 s->ybase += s->first_glyph->voffset;
18566 }
18567
18568
18569 /* Fill glyph string S from a sequence of stretch glyphs.
18570
18571 ROW is the glyph row in which the glyphs are found, AREA is the
18572 area within the row. START is the index of the first glyph to
18573 consider, END is the index of the last + 1.
18574
18575 Value is the index of the first glyph not in S. */
18576
18577 static int
18578 fill_stretch_glyph_string (s, row, area, start, end)
18579 struct glyph_string *s;
18580 struct glyph_row *row;
18581 enum glyph_row_area area;
18582 int start, end;
18583 {
18584 struct glyph *glyph, *last;
18585 int voffset, face_id;
18586
18587 xassert (s->first_glyph->type == STRETCH_GLYPH);
18588
18589 glyph = s->row->glyphs[s->area] + start;
18590 last = s->row->glyphs[s->area] + end;
18591 face_id = glyph->face_id;
18592 s->face = FACE_FROM_ID (s->f, face_id);
18593 s->font = s->face->font;
18594 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18595 s->width = glyph->pixel_width;
18596 voffset = glyph->voffset;
18597
18598 for (++glyph;
18599 (glyph < last
18600 && glyph->type == STRETCH_GLYPH
18601 && glyph->voffset == voffset
18602 && glyph->face_id == face_id);
18603 ++glyph)
18604 s->width += glyph->pixel_width;
18605
18606 /* Adjust base line for subscript/superscript text. */
18607 s->ybase += voffset;
18608
18609 /* The case that face->gc == 0 is handled when drawing the glyph
18610 string by calling PREPARE_FACE_FOR_DISPLAY. */
18611 xassert (s->face);
18612 return glyph - s->row->glyphs[s->area];
18613 }
18614
18615
18616 /* EXPORT for RIF:
18617 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18618 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18619 assumed to be zero. */
18620
18621 void
18622 x_get_glyph_overhangs (glyph, f, left, right)
18623 struct glyph *glyph;
18624 struct frame *f;
18625 int *left, *right;
18626 {
18627 *left = *right = 0;
18628
18629 if (glyph->type == CHAR_GLYPH)
18630 {
18631 XFontStruct *font;
18632 struct face *face;
18633 struct font_info *font_info;
18634 XChar2b char2b;
18635 XCharStruct *pcm;
18636
18637 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18638 font = face->font;
18639 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18640 if (font /* ++KFS: Should this be font_info ? */
18641 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18642 {
18643 if (pcm->rbearing > pcm->width)
18644 *right = pcm->rbearing - pcm->width;
18645 if (pcm->lbearing < 0)
18646 *left = -pcm->lbearing;
18647 }
18648 }
18649 }
18650
18651
18652 /* Return the index of the first glyph preceding glyph string S that
18653 is overwritten by S because of S's left overhang. Value is -1
18654 if no glyphs are overwritten. */
18655
18656 static int
18657 left_overwritten (s)
18658 struct glyph_string *s;
18659 {
18660 int k;
18661
18662 if (s->left_overhang)
18663 {
18664 int x = 0, i;
18665 struct glyph *glyphs = s->row->glyphs[s->area];
18666 int first = s->first_glyph - glyphs;
18667
18668 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18669 x -= glyphs[i].pixel_width;
18670
18671 k = i + 1;
18672 }
18673 else
18674 k = -1;
18675
18676 return k;
18677 }
18678
18679
18680 /* Return the index of the first glyph preceding glyph string S that
18681 is overwriting S because of its right overhang. Value is -1 if no
18682 glyph in front of S overwrites S. */
18683
18684 static int
18685 left_overwriting (s)
18686 struct glyph_string *s;
18687 {
18688 int i, k, x;
18689 struct glyph *glyphs = s->row->glyphs[s->area];
18690 int first = s->first_glyph - glyphs;
18691
18692 k = -1;
18693 x = 0;
18694 for (i = first - 1; i >= 0; --i)
18695 {
18696 int left, right;
18697 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18698 if (x + right > 0)
18699 k = i;
18700 x -= glyphs[i].pixel_width;
18701 }
18702
18703 return k;
18704 }
18705
18706
18707 /* Return the index of the last glyph following glyph string S that is
18708 not overwritten by S because of S's right overhang. Value is -1 if
18709 no such glyph is found. */
18710
18711 static int
18712 right_overwritten (s)
18713 struct glyph_string *s;
18714 {
18715 int k = -1;
18716
18717 if (s->right_overhang)
18718 {
18719 int x = 0, i;
18720 struct glyph *glyphs = s->row->glyphs[s->area];
18721 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18722 int end = s->row->used[s->area];
18723
18724 for (i = first; i < end && s->right_overhang > x; ++i)
18725 x += glyphs[i].pixel_width;
18726
18727 k = i;
18728 }
18729
18730 return k;
18731 }
18732
18733
18734 /* Return the index of the last glyph following glyph string S that
18735 overwrites S because of its left overhang. Value is negative
18736 if no such glyph is found. */
18737
18738 static int
18739 right_overwriting (s)
18740 struct glyph_string *s;
18741 {
18742 int i, k, x;
18743 int end = s->row->used[s->area];
18744 struct glyph *glyphs = s->row->glyphs[s->area];
18745 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18746
18747 k = -1;
18748 x = 0;
18749 for (i = first; i < end; ++i)
18750 {
18751 int left, right;
18752 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18753 if (x - left < 0)
18754 k = i;
18755 x += glyphs[i].pixel_width;
18756 }
18757
18758 return k;
18759 }
18760
18761
18762 /* Get face and two-byte form of character C in face FACE_ID on frame
18763 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18764 means we want to display multibyte text. DISPLAY_P non-zero means
18765 make sure that X resources for the face returned are allocated.
18766 Value is a pointer to a realized face that is ready for display if
18767 DISPLAY_P is non-zero. */
18768
18769 static INLINE struct face *
18770 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18771 struct frame *f;
18772 int c, face_id;
18773 XChar2b *char2b;
18774 int multibyte_p, display_p;
18775 {
18776 struct face *face = FACE_FROM_ID (f, face_id);
18777
18778 if (!multibyte_p)
18779 {
18780 /* Unibyte case. We don't have to encode, but we have to make
18781 sure to use a face suitable for unibyte. */
18782 STORE_XCHAR2B (char2b, 0, c);
18783 face_id = FACE_FOR_CHAR (f, face, c);
18784 face = FACE_FROM_ID (f, face_id);
18785 }
18786 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18787 {
18788 /* Case of ASCII in a face known to fit ASCII. */
18789 STORE_XCHAR2B (char2b, 0, c);
18790 }
18791 else
18792 {
18793 int c1, c2, charset;
18794
18795 /* Split characters into bytes. If c2 is -1 afterwards, C is
18796 really a one-byte character so that byte1 is zero. */
18797 SPLIT_CHAR (c, charset, c1, c2);
18798 if (c2 > 0)
18799 STORE_XCHAR2B (char2b, c1, c2);
18800 else
18801 STORE_XCHAR2B (char2b, 0, c1);
18802
18803 /* Maybe encode the character in *CHAR2B. */
18804 if (face->font != NULL)
18805 {
18806 struct font_info *font_info
18807 = FONT_INFO_FROM_ID (f, face->font_info_id);
18808 if (font_info)
18809 rif->encode_char (c, char2b, font_info, 0);
18810 }
18811 }
18812
18813 /* Make sure X resources of the face are allocated. */
18814 #ifdef HAVE_X_WINDOWS
18815 if (display_p)
18816 #endif
18817 {
18818 xassert (face != NULL);
18819 PREPARE_FACE_FOR_DISPLAY (f, face);
18820 }
18821
18822 return face;
18823 }
18824
18825
18826 /* Set background width of glyph string S. START is the index of the
18827 first glyph following S. LAST_X is the right-most x-position + 1
18828 in the drawing area. */
18829
18830 static INLINE void
18831 set_glyph_string_background_width (s, start, last_x)
18832 struct glyph_string *s;
18833 int start;
18834 int last_x;
18835 {
18836 /* If the face of this glyph string has to be drawn to the end of
18837 the drawing area, set S->extends_to_end_of_line_p. */
18838
18839 if (start == s->row->used[s->area]
18840 && s->area == TEXT_AREA
18841 && ((s->row->fill_line_p
18842 && (s->hl == DRAW_NORMAL_TEXT
18843 || s->hl == DRAW_IMAGE_RAISED
18844 || s->hl == DRAW_IMAGE_SUNKEN))
18845 || s->hl == DRAW_MOUSE_FACE))
18846 s->extends_to_end_of_line_p = 1;
18847
18848 /* If S extends its face to the end of the line, set its
18849 background_width to the distance to the right edge of the drawing
18850 area. */
18851 if (s->extends_to_end_of_line_p)
18852 s->background_width = last_x - s->x + 1;
18853 else
18854 s->background_width = s->width;
18855 }
18856
18857
18858 /* Compute overhangs and x-positions for glyph string S and its
18859 predecessors, or successors. X is the starting x-position for S.
18860 BACKWARD_P non-zero means process predecessors. */
18861
18862 static void
18863 compute_overhangs_and_x (s, x, backward_p)
18864 struct glyph_string *s;
18865 int x;
18866 int backward_p;
18867 {
18868 if (backward_p)
18869 {
18870 while (s)
18871 {
18872 if (rif->compute_glyph_string_overhangs)
18873 rif->compute_glyph_string_overhangs (s);
18874 x -= s->width;
18875 s->x = x;
18876 s = s->prev;
18877 }
18878 }
18879 else
18880 {
18881 while (s)
18882 {
18883 if (rif->compute_glyph_string_overhangs)
18884 rif->compute_glyph_string_overhangs (s);
18885 s->x = x;
18886 x += s->width;
18887 s = s->next;
18888 }
18889 }
18890 }
18891
18892
18893
18894 /* The following macros are only called from draw_glyphs below.
18895 They reference the following parameters of that function directly:
18896 `w', `row', `area', and `overlap_p'
18897 as well as the following local variables:
18898 `s', `f', and `hdc' (in W32) */
18899
18900 #ifdef HAVE_NTGUI
18901 /* On W32, silently add local `hdc' variable to argument list of
18902 init_glyph_string. */
18903 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18904 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18905 #else
18906 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18907 init_glyph_string (s, char2b, w, row, area, start, hl)
18908 #endif
18909
18910 /* Add a glyph string for a stretch glyph to the list of strings
18911 between HEAD and TAIL. START is the index of the stretch glyph in
18912 row area AREA of glyph row ROW. END is the index of the last glyph
18913 in that glyph row area. X is the current output position assigned
18914 to the new glyph string constructed. HL overrides that face of the
18915 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18916 is the right-most x-position of the drawing area. */
18917
18918 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18919 and below -- keep them on one line. */
18920 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18921 do \
18922 { \
18923 s = (struct glyph_string *) alloca (sizeof *s); \
18924 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18925 START = fill_stretch_glyph_string (s, row, area, START, END); \
18926 append_glyph_string (&HEAD, &TAIL, s); \
18927 s->x = (X); \
18928 } \
18929 while (0)
18930
18931
18932 /* Add a glyph string for an image glyph to the list of strings
18933 between HEAD and TAIL. START is the index of the image glyph in
18934 row area AREA of glyph row ROW. END is the index of the last glyph
18935 in that glyph row area. X is the current output position assigned
18936 to the new glyph string constructed. HL overrides that face of the
18937 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18938 is the right-most x-position of the drawing area. */
18939
18940 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18941 do \
18942 { \
18943 s = (struct glyph_string *) alloca (sizeof *s); \
18944 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18945 fill_image_glyph_string (s); \
18946 append_glyph_string (&HEAD, &TAIL, s); \
18947 ++START; \
18948 s->x = (X); \
18949 } \
18950 while (0)
18951
18952
18953 /* Add a glyph string for a sequence of character glyphs to the list
18954 of strings between HEAD and TAIL. START is the index of the first
18955 glyph in row area AREA of glyph row ROW that is part of the new
18956 glyph string. END is the index of the last glyph in that glyph row
18957 area. X is the current output position assigned to the new glyph
18958 string constructed. HL overrides that face of the glyph; e.g. it
18959 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18960 right-most x-position of the drawing area. */
18961
18962 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18963 do \
18964 { \
18965 int c, face_id; \
18966 XChar2b *char2b; \
18967 \
18968 c = (row)->glyphs[area][START].u.ch; \
18969 face_id = (row)->glyphs[area][START].face_id; \
18970 \
18971 s = (struct glyph_string *) alloca (sizeof *s); \
18972 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18973 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18974 append_glyph_string (&HEAD, &TAIL, s); \
18975 s->x = (X); \
18976 START = fill_glyph_string (s, face_id, START, END, overlaps); \
18977 } \
18978 while (0)
18979
18980
18981 /* Add a glyph string for a composite sequence to the list of strings
18982 between HEAD and TAIL. START is the index of the first glyph in
18983 row area AREA of glyph row ROW that is part of the new glyph
18984 string. END is the index of the last glyph in that glyph row area.
18985 X is the current output position assigned to the new glyph string
18986 constructed. HL overrides that face of the glyph; e.g. it is
18987 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18988 x-position of the drawing area. */
18989
18990 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18991 do { \
18992 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18993 int face_id = (row)->glyphs[area][START].face_id; \
18994 struct face *base_face = FACE_FROM_ID (f, face_id); \
18995 struct composition *cmp = composition_table[cmp_id]; \
18996 int glyph_len = cmp->glyph_len; \
18997 XChar2b *char2b; \
18998 struct face **faces; \
18999 struct glyph_string *first_s = NULL; \
19000 int n; \
19001 \
19002 base_face = base_face->ascii_face; \
19003 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19004 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19005 /* At first, fill in `char2b' and `faces'. */ \
19006 for (n = 0; n < glyph_len; n++) \
19007 { \
19008 int c = COMPOSITION_GLYPH (cmp, n); \
19009 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19010 faces[n] = FACE_FROM_ID (f, this_face_id); \
19011 get_char_face_and_encoding (f, c, this_face_id, \
19012 char2b + n, 1, 1); \
19013 } \
19014 \
19015 /* Make glyph_strings for each glyph sequence that is drawable by \
19016 the same face, and append them to HEAD/TAIL. */ \
19017 for (n = 0; n < cmp->glyph_len;) \
19018 { \
19019 s = (struct glyph_string *) alloca (sizeof *s); \
19020 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19021 append_glyph_string (&(HEAD), &(TAIL), s); \
19022 s->cmp = cmp; \
19023 s->gidx = n; \
19024 s->x = (X); \
19025 \
19026 if (n == 0) \
19027 first_s = s; \
19028 \
19029 n = fill_composite_glyph_string (s, faces, overlaps); \
19030 } \
19031 \
19032 ++START; \
19033 s = first_s; \
19034 } while (0)
19035
19036
19037 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19038 of AREA of glyph row ROW on window W between indices START and END.
19039 HL overrides the face for drawing glyph strings, e.g. it is
19040 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19041 x-positions of the drawing area.
19042
19043 This is an ugly monster macro construct because we must use alloca
19044 to allocate glyph strings (because draw_glyphs can be called
19045 asynchronously). */
19046
19047 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19048 do \
19049 { \
19050 HEAD = TAIL = NULL; \
19051 while (START < END) \
19052 { \
19053 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19054 switch (first_glyph->type) \
19055 { \
19056 case CHAR_GLYPH: \
19057 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19058 HL, X, LAST_X); \
19059 break; \
19060 \
19061 case COMPOSITE_GLYPH: \
19062 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19063 HL, X, LAST_X); \
19064 break; \
19065 \
19066 case STRETCH_GLYPH: \
19067 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19068 HL, X, LAST_X); \
19069 break; \
19070 \
19071 case IMAGE_GLYPH: \
19072 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19073 HL, X, LAST_X); \
19074 break; \
19075 \
19076 default: \
19077 abort (); \
19078 } \
19079 \
19080 set_glyph_string_background_width (s, START, LAST_X); \
19081 (X) += s->width; \
19082 } \
19083 } \
19084 while (0)
19085
19086
19087 /* Draw glyphs between START and END in AREA of ROW on window W,
19088 starting at x-position X. X is relative to AREA in W. HL is a
19089 face-override with the following meaning:
19090
19091 DRAW_NORMAL_TEXT draw normally
19092 DRAW_CURSOR draw in cursor face
19093 DRAW_MOUSE_FACE draw in mouse face.
19094 DRAW_INVERSE_VIDEO draw in mode line face
19095 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19096 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19097
19098 If OVERLAPS is non-zero, draw only the foreground of characters and
19099 clip to the physical height of ROW. Non-zero value also defines
19100 the overlapping part to be drawn:
19101
19102 OVERLAPS_PRED overlap with preceding rows
19103 OVERLAPS_SUCC overlap with succeeding rows
19104 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19105 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19106
19107 Value is the x-position reached, relative to AREA of W. */
19108
19109 static int
19110 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19111 struct window *w;
19112 int x;
19113 struct glyph_row *row;
19114 enum glyph_row_area area;
19115 int start, end;
19116 enum draw_glyphs_face hl;
19117 int overlaps;
19118 {
19119 struct glyph_string *head, *tail;
19120 struct glyph_string *s;
19121 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19122 int last_x, area_width;
19123 int x_reached;
19124 int i, j;
19125 struct frame *f = XFRAME (WINDOW_FRAME (w));
19126 DECLARE_HDC (hdc);
19127
19128 ALLOCATE_HDC (hdc, f);
19129
19130 /* Let's rather be paranoid than getting a SEGV. */
19131 end = min (end, row->used[area]);
19132 start = max (0, start);
19133 start = min (end, start);
19134
19135 /* Translate X to frame coordinates. Set last_x to the right
19136 end of the drawing area. */
19137 if (row->full_width_p)
19138 {
19139 /* X is relative to the left edge of W, without scroll bars
19140 or fringes. */
19141 x += WINDOW_LEFT_EDGE_X (w);
19142 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19143 }
19144 else
19145 {
19146 int area_left = window_box_left (w, area);
19147 x += area_left;
19148 area_width = window_box_width (w, area);
19149 last_x = area_left + area_width;
19150 }
19151
19152 /* Build a doubly-linked list of glyph_string structures between
19153 head and tail from what we have to draw. Note that the macro
19154 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19155 the reason we use a separate variable `i'. */
19156 i = start;
19157 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19158 if (tail)
19159 x_reached = tail->x + tail->background_width;
19160 else
19161 x_reached = x;
19162
19163 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19164 the row, redraw some glyphs in front or following the glyph
19165 strings built above. */
19166 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19167 {
19168 int dummy_x = 0;
19169 struct glyph_string *h, *t;
19170
19171 /* Compute overhangs for all glyph strings. */
19172 if (rif->compute_glyph_string_overhangs)
19173 for (s = head; s; s = s->next)
19174 rif->compute_glyph_string_overhangs (s);
19175
19176 /* Prepend glyph strings for glyphs in front of the first glyph
19177 string that are overwritten because of the first glyph
19178 string's left overhang. The background of all strings
19179 prepended must be drawn because the first glyph string
19180 draws over it. */
19181 i = left_overwritten (head);
19182 if (i >= 0)
19183 {
19184 j = i;
19185 BUILD_GLYPH_STRINGS (j, start, h, t,
19186 DRAW_NORMAL_TEXT, dummy_x, last_x);
19187 start = i;
19188 compute_overhangs_and_x (t, head->x, 1);
19189 prepend_glyph_string_lists (&head, &tail, h, t);
19190 clip_head = head;
19191 }
19192
19193 /* Prepend glyph strings for glyphs in front of the first glyph
19194 string that overwrite that glyph string because of their
19195 right overhang. For these strings, only the foreground must
19196 be drawn, because it draws over the glyph string at `head'.
19197 The background must not be drawn because this would overwrite
19198 right overhangs of preceding glyphs for which no glyph
19199 strings exist. */
19200 i = left_overwriting (head);
19201 if (i >= 0)
19202 {
19203 clip_head = head;
19204 BUILD_GLYPH_STRINGS (i, start, h, t,
19205 DRAW_NORMAL_TEXT, dummy_x, last_x);
19206 for (s = h; s; s = s->next)
19207 s->background_filled_p = 1;
19208 compute_overhangs_and_x (t, head->x, 1);
19209 prepend_glyph_string_lists (&head, &tail, h, t);
19210 }
19211
19212 /* Append glyphs strings for glyphs following the last glyph
19213 string tail that are overwritten by tail. The background of
19214 these strings has to be drawn because tail's foreground draws
19215 over it. */
19216 i = right_overwritten (tail);
19217 if (i >= 0)
19218 {
19219 BUILD_GLYPH_STRINGS (end, i, h, t,
19220 DRAW_NORMAL_TEXT, x, last_x);
19221 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19222 append_glyph_string_lists (&head, &tail, h, t);
19223 clip_tail = tail;
19224 }
19225
19226 /* Append glyph strings for glyphs following the last glyph
19227 string tail that overwrite tail. The foreground of such
19228 glyphs has to be drawn because it writes into the background
19229 of tail. The background must not be drawn because it could
19230 paint over the foreground of following glyphs. */
19231 i = right_overwriting (tail);
19232 if (i >= 0)
19233 {
19234 clip_tail = tail;
19235 BUILD_GLYPH_STRINGS (end, i, h, t,
19236 DRAW_NORMAL_TEXT, x, last_x);
19237 for (s = h; s; s = s->next)
19238 s->background_filled_p = 1;
19239 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19240 append_glyph_string_lists (&head, &tail, h, t);
19241 }
19242 if (clip_head || clip_tail)
19243 for (s = head; s; s = s->next)
19244 {
19245 s->clip_head = clip_head;
19246 s->clip_tail = clip_tail;
19247 }
19248 }
19249
19250 /* Draw all strings. */
19251 for (s = head; s; s = s->next)
19252 rif->draw_glyph_string (s);
19253
19254 if (area == TEXT_AREA
19255 && !row->full_width_p
19256 /* When drawing overlapping rows, only the glyph strings'
19257 foreground is drawn, which doesn't erase a cursor
19258 completely. */
19259 && !overlaps)
19260 {
19261 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19262 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19263 : (tail ? tail->x + tail->background_width : x));
19264
19265 int text_left = window_box_left (w, TEXT_AREA);
19266 x0 -= text_left;
19267 x1 -= text_left;
19268
19269 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19270 row->y, MATRIX_ROW_BOTTOM_Y (row));
19271 }
19272
19273 /* Value is the x-position up to which drawn, relative to AREA of W.
19274 This doesn't include parts drawn because of overhangs. */
19275 if (row->full_width_p)
19276 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19277 else
19278 x_reached -= window_box_left (w, area);
19279
19280 RELEASE_HDC (hdc, f);
19281
19282 return x_reached;
19283 }
19284
19285 /* Expand row matrix if too narrow. Don't expand if area
19286 is not present. */
19287
19288 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19289 { \
19290 if (!fonts_changed_p \
19291 && (it->glyph_row->glyphs[area] \
19292 < it->glyph_row->glyphs[area + 1])) \
19293 { \
19294 it->w->ncols_scale_factor++; \
19295 fonts_changed_p = 1; \
19296 } \
19297 }
19298
19299 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19300 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19301
19302 static INLINE void
19303 append_glyph (it)
19304 struct it *it;
19305 {
19306 struct glyph *glyph;
19307 enum glyph_row_area area = it->area;
19308
19309 xassert (it->glyph_row);
19310 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19311
19312 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19313 if (glyph < it->glyph_row->glyphs[area + 1])
19314 {
19315 glyph->charpos = CHARPOS (it->position);
19316 glyph->object = it->object;
19317 glyph->pixel_width = it->pixel_width;
19318 glyph->ascent = it->ascent;
19319 glyph->descent = it->descent;
19320 glyph->voffset = it->voffset;
19321 glyph->type = CHAR_GLYPH;
19322 glyph->multibyte_p = it->multibyte_p;
19323 glyph->left_box_line_p = it->start_of_box_run_p;
19324 glyph->right_box_line_p = it->end_of_box_run_p;
19325 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19326 || it->phys_descent > it->descent);
19327 glyph->padding_p = 0;
19328 glyph->glyph_not_available_p = it->glyph_not_available_p;
19329 glyph->face_id = it->face_id;
19330 glyph->u.ch = it->char_to_display;
19331 glyph->slice = null_glyph_slice;
19332 glyph->font_type = FONT_TYPE_UNKNOWN;
19333 ++it->glyph_row->used[area];
19334 }
19335 else
19336 IT_EXPAND_MATRIX_WIDTH (it, area);
19337 }
19338
19339 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19340 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19341
19342 static INLINE void
19343 append_composite_glyph (it)
19344 struct it *it;
19345 {
19346 struct glyph *glyph;
19347 enum glyph_row_area area = it->area;
19348
19349 xassert (it->glyph_row);
19350
19351 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19352 if (glyph < it->glyph_row->glyphs[area + 1])
19353 {
19354 glyph->charpos = CHARPOS (it->position);
19355 glyph->object = it->object;
19356 glyph->pixel_width = it->pixel_width;
19357 glyph->ascent = it->ascent;
19358 glyph->descent = it->descent;
19359 glyph->voffset = it->voffset;
19360 glyph->type = COMPOSITE_GLYPH;
19361 glyph->multibyte_p = it->multibyte_p;
19362 glyph->left_box_line_p = it->start_of_box_run_p;
19363 glyph->right_box_line_p = it->end_of_box_run_p;
19364 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19365 || it->phys_descent > it->descent);
19366 glyph->padding_p = 0;
19367 glyph->glyph_not_available_p = 0;
19368 glyph->face_id = it->face_id;
19369 glyph->u.cmp_id = it->cmp_id;
19370 glyph->slice = null_glyph_slice;
19371 glyph->font_type = FONT_TYPE_UNKNOWN;
19372 ++it->glyph_row->used[area];
19373 }
19374 else
19375 IT_EXPAND_MATRIX_WIDTH (it, area);
19376 }
19377
19378
19379 /* Change IT->ascent and IT->height according to the setting of
19380 IT->voffset. */
19381
19382 static INLINE void
19383 take_vertical_position_into_account (it)
19384 struct it *it;
19385 {
19386 if (it->voffset)
19387 {
19388 if (it->voffset < 0)
19389 /* Increase the ascent so that we can display the text higher
19390 in the line. */
19391 it->ascent -= it->voffset;
19392 else
19393 /* Increase the descent so that we can display the text lower
19394 in the line. */
19395 it->descent += it->voffset;
19396 }
19397 }
19398
19399
19400 /* Produce glyphs/get display metrics for the image IT is loaded with.
19401 See the description of struct display_iterator in dispextern.h for
19402 an overview of struct display_iterator. */
19403
19404 static void
19405 produce_image_glyph (it)
19406 struct it *it;
19407 {
19408 struct image *img;
19409 struct face *face;
19410 int glyph_ascent;
19411 struct glyph_slice slice;
19412
19413 xassert (it->what == IT_IMAGE);
19414
19415 face = FACE_FROM_ID (it->f, it->face_id);
19416 xassert (face);
19417 /* Make sure X resources of the face is loaded. */
19418 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19419
19420 if (it->image_id < 0)
19421 {
19422 /* Fringe bitmap. */
19423 it->ascent = it->phys_ascent = 0;
19424 it->descent = it->phys_descent = 0;
19425 it->pixel_width = 0;
19426 it->nglyphs = 0;
19427 return;
19428 }
19429
19430 img = IMAGE_FROM_ID (it->f, it->image_id);
19431 xassert (img);
19432 /* Make sure X resources of the image is loaded. */
19433 prepare_image_for_display (it->f, img);
19434
19435 slice.x = slice.y = 0;
19436 slice.width = img->width;
19437 slice.height = img->height;
19438
19439 if (INTEGERP (it->slice.x))
19440 slice.x = XINT (it->slice.x);
19441 else if (FLOATP (it->slice.x))
19442 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19443
19444 if (INTEGERP (it->slice.y))
19445 slice.y = XINT (it->slice.y);
19446 else if (FLOATP (it->slice.y))
19447 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19448
19449 if (INTEGERP (it->slice.width))
19450 slice.width = XINT (it->slice.width);
19451 else if (FLOATP (it->slice.width))
19452 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19453
19454 if (INTEGERP (it->slice.height))
19455 slice.height = XINT (it->slice.height);
19456 else if (FLOATP (it->slice.height))
19457 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19458
19459 if (slice.x >= img->width)
19460 slice.x = img->width;
19461 if (slice.y >= img->height)
19462 slice.y = img->height;
19463 if (slice.x + slice.width >= img->width)
19464 slice.width = img->width - slice.x;
19465 if (slice.y + slice.height > img->height)
19466 slice.height = img->height - slice.y;
19467
19468 if (slice.width == 0 || slice.height == 0)
19469 return;
19470
19471 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19472
19473 it->descent = slice.height - glyph_ascent;
19474 if (slice.y == 0)
19475 it->descent += img->vmargin;
19476 if (slice.y + slice.height == img->height)
19477 it->descent += img->vmargin;
19478 it->phys_descent = it->descent;
19479
19480 it->pixel_width = slice.width;
19481 if (slice.x == 0)
19482 it->pixel_width += img->hmargin;
19483 if (slice.x + slice.width == img->width)
19484 it->pixel_width += img->hmargin;
19485
19486 /* It's quite possible for images to have an ascent greater than
19487 their height, so don't get confused in that case. */
19488 if (it->descent < 0)
19489 it->descent = 0;
19490
19491 #if 0 /* this breaks image tiling */
19492 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19493 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19494 if (face_ascent > it->ascent)
19495 it->ascent = it->phys_ascent = face_ascent;
19496 #endif
19497
19498 it->nglyphs = 1;
19499
19500 if (face->box != FACE_NO_BOX)
19501 {
19502 if (face->box_line_width > 0)
19503 {
19504 if (slice.y == 0)
19505 it->ascent += face->box_line_width;
19506 if (slice.y + slice.height == img->height)
19507 it->descent += face->box_line_width;
19508 }
19509
19510 if (it->start_of_box_run_p && slice.x == 0)
19511 it->pixel_width += abs (face->box_line_width);
19512 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19513 it->pixel_width += abs (face->box_line_width);
19514 }
19515
19516 take_vertical_position_into_account (it);
19517
19518 if (it->glyph_row)
19519 {
19520 struct glyph *glyph;
19521 enum glyph_row_area area = it->area;
19522
19523 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19524 if (glyph < it->glyph_row->glyphs[area + 1])
19525 {
19526 glyph->charpos = CHARPOS (it->position);
19527 glyph->object = it->object;
19528 glyph->pixel_width = it->pixel_width;
19529 glyph->ascent = glyph_ascent;
19530 glyph->descent = it->descent;
19531 glyph->voffset = it->voffset;
19532 glyph->type = IMAGE_GLYPH;
19533 glyph->multibyte_p = it->multibyte_p;
19534 glyph->left_box_line_p = it->start_of_box_run_p;
19535 glyph->right_box_line_p = it->end_of_box_run_p;
19536 glyph->overlaps_vertically_p = 0;
19537 glyph->padding_p = 0;
19538 glyph->glyph_not_available_p = 0;
19539 glyph->face_id = it->face_id;
19540 glyph->u.img_id = img->id;
19541 glyph->slice = slice;
19542 glyph->font_type = FONT_TYPE_UNKNOWN;
19543 ++it->glyph_row->used[area];
19544 }
19545 else
19546 IT_EXPAND_MATRIX_WIDTH (it, area);
19547 }
19548 }
19549
19550
19551 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19552 of the glyph, WIDTH and HEIGHT are the width and height of the
19553 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19554
19555 static void
19556 append_stretch_glyph (it, object, width, height, ascent)
19557 struct it *it;
19558 Lisp_Object object;
19559 int width, height;
19560 int ascent;
19561 {
19562 struct glyph *glyph;
19563 enum glyph_row_area area = it->area;
19564
19565 xassert (ascent >= 0 && ascent <= height);
19566
19567 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19568 if (glyph < it->glyph_row->glyphs[area + 1])
19569 {
19570 glyph->charpos = CHARPOS (it->position);
19571 glyph->object = object;
19572 glyph->pixel_width = width;
19573 glyph->ascent = ascent;
19574 glyph->descent = height - ascent;
19575 glyph->voffset = it->voffset;
19576 glyph->type = STRETCH_GLYPH;
19577 glyph->multibyte_p = it->multibyte_p;
19578 glyph->left_box_line_p = it->start_of_box_run_p;
19579 glyph->right_box_line_p = it->end_of_box_run_p;
19580 glyph->overlaps_vertically_p = 0;
19581 glyph->padding_p = 0;
19582 glyph->glyph_not_available_p = 0;
19583 glyph->face_id = it->face_id;
19584 glyph->u.stretch.ascent = ascent;
19585 glyph->u.stretch.height = height;
19586 glyph->slice = null_glyph_slice;
19587 glyph->font_type = FONT_TYPE_UNKNOWN;
19588 ++it->glyph_row->used[area];
19589 }
19590 else
19591 IT_EXPAND_MATRIX_WIDTH (it, area);
19592 }
19593
19594
19595 /* Produce a stretch glyph for iterator IT. IT->object is the value
19596 of the glyph property displayed. The value must be a list
19597 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19598 being recognized:
19599
19600 1. `:width WIDTH' specifies that the space should be WIDTH *
19601 canonical char width wide. WIDTH may be an integer or floating
19602 point number.
19603
19604 2. `:relative-width FACTOR' specifies that the width of the stretch
19605 should be computed from the width of the first character having the
19606 `glyph' property, and should be FACTOR times that width.
19607
19608 3. `:align-to HPOS' specifies that the space should be wide enough
19609 to reach HPOS, a value in canonical character units.
19610
19611 Exactly one of the above pairs must be present.
19612
19613 4. `:height HEIGHT' specifies that the height of the stretch produced
19614 should be HEIGHT, measured in canonical character units.
19615
19616 5. `:relative-height FACTOR' specifies that the height of the
19617 stretch should be FACTOR times the height of the characters having
19618 the glyph property.
19619
19620 Either none or exactly one of 4 or 5 must be present.
19621
19622 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19623 of the stretch should be used for the ascent of the stretch.
19624 ASCENT must be in the range 0 <= ASCENT <= 100. */
19625
19626 static void
19627 produce_stretch_glyph (it)
19628 struct it *it;
19629 {
19630 /* (space :width WIDTH :height HEIGHT ...) */
19631 Lisp_Object prop, plist;
19632 int width = 0, height = 0, align_to = -1;
19633 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19634 int ascent = 0;
19635 double tem;
19636 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19637 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19638
19639 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19640
19641 /* List should start with `space'. */
19642 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19643 plist = XCDR (it->object);
19644
19645 /* Compute the width of the stretch. */
19646 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19647 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19648 {
19649 /* Absolute width `:width WIDTH' specified and valid. */
19650 zero_width_ok_p = 1;
19651 width = (int)tem;
19652 }
19653 else if (prop = Fplist_get (plist, QCrelative_width),
19654 NUMVAL (prop) > 0)
19655 {
19656 /* Relative width `:relative-width FACTOR' specified and valid.
19657 Compute the width of the characters having the `glyph'
19658 property. */
19659 struct it it2;
19660 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19661
19662 it2 = *it;
19663 if (it->multibyte_p)
19664 {
19665 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19666 - IT_BYTEPOS (*it));
19667 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19668 }
19669 else
19670 it2.c = *p, it2.len = 1;
19671
19672 it2.glyph_row = NULL;
19673 it2.what = IT_CHARACTER;
19674 x_produce_glyphs (&it2);
19675 width = NUMVAL (prop) * it2.pixel_width;
19676 }
19677 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19678 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19679 {
19680 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19681 align_to = (align_to < 0
19682 ? 0
19683 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19684 else if (align_to < 0)
19685 align_to = window_box_left_offset (it->w, TEXT_AREA);
19686 width = max (0, (int)tem + align_to - it->current_x);
19687 zero_width_ok_p = 1;
19688 }
19689 else
19690 /* Nothing specified -> width defaults to canonical char width. */
19691 width = FRAME_COLUMN_WIDTH (it->f);
19692
19693 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19694 width = 1;
19695
19696 /* Compute height. */
19697 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19698 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19699 {
19700 height = (int)tem;
19701 zero_height_ok_p = 1;
19702 }
19703 else if (prop = Fplist_get (plist, QCrelative_height),
19704 NUMVAL (prop) > 0)
19705 height = FONT_HEIGHT (font) * NUMVAL (prop);
19706 else
19707 height = FONT_HEIGHT (font);
19708
19709 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19710 height = 1;
19711
19712 /* Compute percentage of height used for ascent. If
19713 `:ascent ASCENT' is present and valid, use that. Otherwise,
19714 derive the ascent from the font in use. */
19715 if (prop = Fplist_get (plist, QCascent),
19716 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19717 ascent = height * NUMVAL (prop) / 100.0;
19718 else if (!NILP (prop)
19719 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19720 ascent = min (max (0, (int)tem), height);
19721 else
19722 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19723
19724 if (width > 0 && !it->truncate_lines_p
19725 && it->current_x + width > it->last_visible_x)
19726 width = it->last_visible_x - it->current_x - 1;
19727
19728 if (width > 0 && height > 0 && it->glyph_row)
19729 {
19730 Lisp_Object object = it->stack[it->sp - 1].string;
19731 if (!STRINGP (object))
19732 object = it->w->buffer;
19733 append_stretch_glyph (it, object, width, height, ascent);
19734 }
19735
19736 it->pixel_width = width;
19737 it->ascent = it->phys_ascent = ascent;
19738 it->descent = it->phys_descent = height - it->ascent;
19739 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19740
19741 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19742 {
19743 if (face->box_line_width > 0)
19744 {
19745 it->ascent += face->box_line_width;
19746 it->descent += face->box_line_width;
19747 }
19748
19749 if (it->start_of_box_run_p)
19750 it->pixel_width += abs (face->box_line_width);
19751 if (it->end_of_box_run_p)
19752 it->pixel_width += abs (face->box_line_width);
19753 }
19754
19755 take_vertical_position_into_account (it);
19756 }
19757
19758 /* Get line-height and line-spacing property at point.
19759 If line-height has format (HEIGHT TOTAL), return TOTAL
19760 in TOTAL_HEIGHT. */
19761
19762 static Lisp_Object
19763 get_line_height_property (it, prop)
19764 struct it *it;
19765 Lisp_Object prop;
19766 {
19767 Lisp_Object position;
19768
19769 if (STRINGP (it->object))
19770 position = make_number (IT_STRING_CHARPOS (*it));
19771 else if (BUFFERP (it->object))
19772 position = make_number (IT_CHARPOS (*it));
19773 else
19774 return Qnil;
19775
19776 return Fget_char_property (position, prop, it->object);
19777 }
19778
19779 /* Calculate line-height and line-spacing properties.
19780 An integer value specifies explicit pixel value.
19781 A float value specifies relative value to current face height.
19782 A cons (float . face-name) specifies relative value to
19783 height of specified face font.
19784
19785 Returns height in pixels, or nil. */
19786
19787
19788 static Lisp_Object
19789 calc_line_height_property (it, val, font, boff, override)
19790 struct it *it;
19791 Lisp_Object val;
19792 XFontStruct *font;
19793 int boff, override;
19794 {
19795 Lisp_Object face_name = Qnil;
19796 int ascent, descent, height;
19797
19798 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19799 return val;
19800
19801 if (CONSP (val))
19802 {
19803 face_name = XCAR (val);
19804 val = XCDR (val);
19805 if (!NUMBERP (val))
19806 val = make_number (1);
19807 if (NILP (face_name))
19808 {
19809 height = it->ascent + it->descent;
19810 goto scale;
19811 }
19812 }
19813
19814 if (NILP (face_name))
19815 {
19816 font = FRAME_FONT (it->f);
19817 boff = FRAME_BASELINE_OFFSET (it->f);
19818 }
19819 else if (EQ (face_name, Qt))
19820 {
19821 override = 0;
19822 }
19823 else
19824 {
19825 int face_id;
19826 struct face *face;
19827 struct font_info *font_info;
19828
19829 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19830 if (face_id < 0)
19831 return make_number (-1);
19832
19833 face = FACE_FROM_ID (it->f, face_id);
19834 font = face->font;
19835 if (font == NULL)
19836 return make_number (-1);
19837
19838 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19839 boff = font_info->baseline_offset;
19840 if (font_info->vertical_centering)
19841 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19842 }
19843
19844 ascent = FONT_BASE (font) + boff;
19845 descent = FONT_DESCENT (font) - boff;
19846
19847 if (override)
19848 {
19849 it->override_ascent = ascent;
19850 it->override_descent = descent;
19851 it->override_boff = boff;
19852 }
19853
19854 height = ascent + descent;
19855
19856 scale:
19857 if (FLOATP (val))
19858 height = (int)(XFLOAT_DATA (val) * height);
19859 else if (INTEGERP (val))
19860 height *= XINT (val);
19861
19862 return make_number (height);
19863 }
19864
19865
19866 /* RIF:
19867 Produce glyphs/get display metrics for the display element IT is
19868 loaded with. See the description of struct it in dispextern.h
19869 for an overview of struct it. */
19870
19871 void
19872 x_produce_glyphs (it)
19873 struct it *it;
19874 {
19875 int extra_line_spacing = it->extra_line_spacing;
19876
19877 it->glyph_not_available_p = 0;
19878
19879 if (it->what == IT_CHARACTER)
19880 {
19881 XChar2b char2b;
19882 XFontStruct *font;
19883 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19884 XCharStruct *pcm;
19885 int font_not_found_p;
19886 struct font_info *font_info;
19887 int boff; /* baseline offset */
19888 /* We may change it->multibyte_p upon unibyte<->multibyte
19889 conversion. So, save the current value now and restore it
19890 later.
19891
19892 Note: It seems that we don't have to record multibyte_p in
19893 struct glyph because the character code itself tells if or
19894 not the character is multibyte. Thus, in the future, we must
19895 consider eliminating the field `multibyte_p' in the struct
19896 glyph. */
19897 int saved_multibyte_p = it->multibyte_p;
19898
19899 /* Maybe translate single-byte characters to multibyte, or the
19900 other way. */
19901 it->char_to_display = it->c;
19902 if (!ASCII_BYTE_P (it->c))
19903 {
19904 if (unibyte_display_via_language_environment
19905 && SINGLE_BYTE_CHAR_P (it->c)
19906 && (it->c >= 0240
19907 || !NILP (Vnonascii_translation_table)))
19908 {
19909 it->char_to_display = unibyte_char_to_multibyte (it->c);
19910 it->multibyte_p = 1;
19911 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19912 face = FACE_FROM_ID (it->f, it->face_id);
19913 }
19914 else if (!SINGLE_BYTE_CHAR_P (it->c)
19915 && !it->multibyte_p)
19916 {
19917 it->multibyte_p = 1;
19918 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19919 face = FACE_FROM_ID (it->f, it->face_id);
19920 }
19921 }
19922
19923 /* Get font to use. Encode IT->char_to_display. */
19924 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19925 &char2b, it->multibyte_p, 0);
19926 font = face->font;
19927
19928 /* When no suitable font found, use the default font. */
19929 font_not_found_p = font == NULL;
19930 if (font_not_found_p)
19931 {
19932 font = FRAME_FONT (it->f);
19933 boff = FRAME_BASELINE_OFFSET (it->f);
19934 font_info = NULL;
19935 }
19936 else
19937 {
19938 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19939 boff = font_info->baseline_offset;
19940 if (font_info->vertical_centering)
19941 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19942 }
19943
19944 if (it->char_to_display >= ' '
19945 && (!it->multibyte_p || it->char_to_display < 128))
19946 {
19947 /* Either unibyte or ASCII. */
19948 int stretched_p;
19949
19950 it->nglyphs = 1;
19951
19952 pcm = rif->per_char_metric (font, &char2b,
19953 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19954
19955 if (it->override_ascent >= 0)
19956 {
19957 it->ascent = it->override_ascent;
19958 it->descent = it->override_descent;
19959 boff = it->override_boff;
19960 }
19961 else
19962 {
19963 it->ascent = FONT_BASE (font) + boff;
19964 it->descent = FONT_DESCENT (font) - boff;
19965 }
19966
19967 if (pcm)
19968 {
19969 it->phys_ascent = pcm->ascent + boff;
19970 it->phys_descent = pcm->descent - boff;
19971 it->pixel_width = pcm->width;
19972 }
19973 else
19974 {
19975 it->glyph_not_available_p = 1;
19976 it->phys_ascent = it->ascent;
19977 it->phys_descent = it->descent;
19978 it->pixel_width = FONT_WIDTH (font);
19979 }
19980
19981 if (it->constrain_row_ascent_descent_p)
19982 {
19983 if (it->descent > it->max_descent)
19984 {
19985 it->ascent += it->descent - it->max_descent;
19986 it->descent = it->max_descent;
19987 }
19988 if (it->ascent > it->max_ascent)
19989 {
19990 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19991 it->ascent = it->max_ascent;
19992 }
19993 it->phys_ascent = min (it->phys_ascent, it->ascent);
19994 it->phys_descent = min (it->phys_descent, it->descent);
19995 extra_line_spacing = 0;
19996 }
19997
19998 /* If this is a space inside a region of text with
19999 `space-width' property, change its width. */
20000 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20001 if (stretched_p)
20002 it->pixel_width *= XFLOATINT (it->space_width);
20003
20004 /* If face has a box, add the box thickness to the character
20005 height. If character has a box line to the left and/or
20006 right, add the box line width to the character's width. */
20007 if (face->box != FACE_NO_BOX)
20008 {
20009 int thick = face->box_line_width;
20010
20011 if (thick > 0)
20012 {
20013 it->ascent += thick;
20014 it->descent += thick;
20015 }
20016 else
20017 thick = -thick;
20018
20019 if (it->start_of_box_run_p)
20020 it->pixel_width += thick;
20021 if (it->end_of_box_run_p)
20022 it->pixel_width += thick;
20023 }
20024
20025 /* If face has an overline, add the height of the overline
20026 (1 pixel) and a 1 pixel margin to the character height. */
20027 if (face->overline_p)
20028 it->ascent += 2;
20029
20030 if (it->constrain_row_ascent_descent_p)
20031 {
20032 if (it->ascent > it->max_ascent)
20033 it->ascent = it->max_ascent;
20034 if (it->descent > it->max_descent)
20035 it->descent = it->max_descent;
20036 }
20037
20038 take_vertical_position_into_account (it);
20039
20040 /* If we have to actually produce glyphs, do it. */
20041 if (it->glyph_row)
20042 {
20043 if (stretched_p)
20044 {
20045 /* Translate a space with a `space-width' property
20046 into a stretch glyph. */
20047 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20048 / FONT_HEIGHT (font));
20049 append_stretch_glyph (it, it->object, it->pixel_width,
20050 it->ascent + it->descent, ascent);
20051 }
20052 else
20053 append_glyph (it);
20054
20055 /* If characters with lbearing or rbearing are displayed
20056 in this line, record that fact in a flag of the
20057 glyph row. This is used to optimize X output code. */
20058 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20059 it->glyph_row->contains_overlapping_glyphs_p = 1;
20060 }
20061 }
20062 else if (it->char_to_display == '\n')
20063 {
20064 /* A newline has no width but we need the height of the line.
20065 But if previous part of the line set a height, don't
20066 increase that height */
20067
20068 Lisp_Object height;
20069 Lisp_Object total_height = Qnil;
20070
20071 it->override_ascent = -1;
20072 it->pixel_width = 0;
20073 it->nglyphs = 0;
20074
20075 height = get_line_height_property(it, Qline_height);
20076 /* Split (line-height total-height) list */
20077 if (CONSP (height)
20078 && CONSP (XCDR (height))
20079 && NILP (XCDR (XCDR (height))))
20080 {
20081 total_height = XCAR (XCDR (height));
20082 height = XCAR (height);
20083 }
20084 height = calc_line_height_property(it, height, font, boff, 1);
20085
20086 if (it->override_ascent >= 0)
20087 {
20088 it->ascent = it->override_ascent;
20089 it->descent = it->override_descent;
20090 boff = it->override_boff;
20091 }
20092 else
20093 {
20094 it->ascent = FONT_BASE (font) + boff;
20095 it->descent = FONT_DESCENT (font) - boff;
20096 }
20097
20098 if (EQ (height, Qt))
20099 {
20100 if (it->descent > it->max_descent)
20101 {
20102 it->ascent += it->descent - it->max_descent;
20103 it->descent = it->max_descent;
20104 }
20105 if (it->ascent > it->max_ascent)
20106 {
20107 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20108 it->ascent = it->max_ascent;
20109 }
20110 it->phys_ascent = min (it->phys_ascent, it->ascent);
20111 it->phys_descent = min (it->phys_descent, it->descent);
20112 it->constrain_row_ascent_descent_p = 1;
20113 extra_line_spacing = 0;
20114 }
20115 else
20116 {
20117 Lisp_Object spacing;
20118
20119 it->phys_ascent = it->ascent;
20120 it->phys_descent = it->descent;
20121
20122 if ((it->max_ascent > 0 || it->max_descent > 0)
20123 && face->box != FACE_NO_BOX
20124 && face->box_line_width > 0)
20125 {
20126 it->ascent += face->box_line_width;
20127 it->descent += face->box_line_width;
20128 }
20129 if (!NILP (height)
20130 && XINT (height) > it->ascent + it->descent)
20131 it->ascent = XINT (height) - it->descent;
20132
20133 if (!NILP (total_height))
20134 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20135 else
20136 {
20137 spacing = get_line_height_property(it, Qline_spacing);
20138 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20139 }
20140 if (INTEGERP (spacing))
20141 {
20142 extra_line_spacing = XINT (spacing);
20143 if (!NILP (total_height))
20144 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20145 }
20146 }
20147 }
20148 else if (it->char_to_display == '\t')
20149 {
20150 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20151 int x = it->current_x + it->continuation_lines_width;
20152 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20153
20154 /* If the distance from the current position to the next tab
20155 stop is less than a space character width, use the
20156 tab stop after that. */
20157 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20158 next_tab_x += tab_width;
20159
20160 it->pixel_width = next_tab_x - x;
20161 it->nglyphs = 1;
20162 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20163 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20164
20165 if (it->glyph_row)
20166 {
20167 append_stretch_glyph (it, it->object, it->pixel_width,
20168 it->ascent + it->descent, it->ascent);
20169 }
20170 }
20171 else
20172 {
20173 /* A multi-byte character. Assume that the display width of the
20174 character is the width of the character multiplied by the
20175 width of the font. */
20176
20177 /* If we found a font, this font should give us the right
20178 metrics. If we didn't find a font, use the frame's
20179 default font and calculate the width of the character
20180 from the charset width; this is what old redisplay code
20181 did. */
20182
20183 pcm = rif->per_char_metric (font, &char2b,
20184 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20185
20186 if (font_not_found_p || !pcm)
20187 {
20188 int charset = CHAR_CHARSET (it->char_to_display);
20189
20190 it->glyph_not_available_p = 1;
20191 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20192 * CHARSET_WIDTH (charset));
20193 it->phys_ascent = FONT_BASE (font) + boff;
20194 it->phys_descent = FONT_DESCENT (font) - boff;
20195 }
20196 else
20197 {
20198 it->pixel_width = pcm->width;
20199 it->phys_ascent = pcm->ascent + boff;
20200 it->phys_descent = pcm->descent - boff;
20201 if (it->glyph_row
20202 && (pcm->lbearing < 0
20203 || pcm->rbearing > pcm->width))
20204 it->glyph_row->contains_overlapping_glyphs_p = 1;
20205 }
20206 it->nglyphs = 1;
20207 it->ascent = FONT_BASE (font) + boff;
20208 it->descent = FONT_DESCENT (font) - boff;
20209 if (face->box != FACE_NO_BOX)
20210 {
20211 int thick = face->box_line_width;
20212
20213 if (thick > 0)
20214 {
20215 it->ascent += thick;
20216 it->descent += thick;
20217 }
20218 else
20219 thick = - thick;
20220
20221 if (it->start_of_box_run_p)
20222 it->pixel_width += thick;
20223 if (it->end_of_box_run_p)
20224 it->pixel_width += thick;
20225 }
20226
20227 /* If face has an overline, add the height of the overline
20228 (1 pixel) and a 1 pixel margin to the character height. */
20229 if (face->overline_p)
20230 it->ascent += 2;
20231
20232 take_vertical_position_into_account (it);
20233
20234 if (it->glyph_row)
20235 append_glyph (it);
20236 }
20237 it->multibyte_p = saved_multibyte_p;
20238 }
20239 else if (it->what == IT_COMPOSITION)
20240 {
20241 /* Note: A composition is represented as one glyph in the
20242 glyph matrix. There are no padding glyphs. */
20243 XChar2b char2b;
20244 XFontStruct *font;
20245 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20246 XCharStruct *pcm;
20247 int font_not_found_p;
20248 struct font_info *font_info;
20249 int boff; /* baseline offset */
20250 struct composition *cmp = composition_table[it->cmp_id];
20251
20252 /* Maybe translate single-byte characters to multibyte. */
20253 it->char_to_display = it->c;
20254 if (unibyte_display_via_language_environment
20255 && SINGLE_BYTE_CHAR_P (it->c)
20256 && (it->c >= 0240
20257 || (it->c >= 0200
20258 && !NILP (Vnonascii_translation_table))))
20259 {
20260 it->char_to_display = unibyte_char_to_multibyte (it->c);
20261 }
20262
20263 /* Get face and font to use. Encode IT->char_to_display. */
20264 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20265 face = FACE_FROM_ID (it->f, it->face_id);
20266 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20267 &char2b, it->multibyte_p, 0);
20268 font = face->font;
20269
20270 /* When no suitable font found, use the default font. */
20271 font_not_found_p = font == NULL;
20272 if (font_not_found_p)
20273 {
20274 font = FRAME_FONT (it->f);
20275 boff = FRAME_BASELINE_OFFSET (it->f);
20276 font_info = NULL;
20277 }
20278 else
20279 {
20280 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20281 boff = font_info->baseline_offset;
20282 if (font_info->vertical_centering)
20283 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20284 }
20285
20286 /* There are no padding glyphs, so there is only one glyph to
20287 produce for the composition. Important is that pixel_width,
20288 ascent and descent are the values of what is drawn by
20289 draw_glyphs (i.e. the values of the overall glyphs composed). */
20290 it->nglyphs = 1;
20291
20292 /* If we have not yet calculated pixel size data of glyphs of
20293 the composition for the current face font, calculate them
20294 now. Theoretically, we have to check all fonts for the
20295 glyphs, but that requires much time and memory space. So,
20296 here we check only the font of the first glyph. This leads
20297 to incorrect display very rarely, and C-l (recenter) can
20298 correct the display anyway. */
20299 if (cmp->font != (void *) font)
20300 {
20301 /* Ascent and descent of the font of the first character of
20302 this composition (adjusted by baseline offset). Ascent
20303 and descent of overall glyphs should not be less than
20304 them respectively. */
20305 int font_ascent = FONT_BASE (font) + boff;
20306 int font_descent = FONT_DESCENT (font) - boff;
20307 /* Bounding box of the overall glyphs. */
20308 int leftmost, rightmost, lowest, highest;
20309 int i, width, ascent, descent;
20310
20311 cmp->font = (void *) font;
20312
20313 /* Initialize the bounding box. */
20314 if (font_info
20315 && (pcm = rif->per_char_metric (font, &char2b,
20316 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20317 {
20318 width = pcm->width;
20319 ascent = pcm->ascent;
20320 descent = pcm->descent;
20321 }
20322 else
20323 {
20324 width = FONT_WIDTH (font);
20325 ascent = FONT_BASE (font);
20326 descent = FONT_DESCENT (font);
20327 }
20328
20329 rightmost = width;
20330 lowest = - descent + boff;
20331 highest = ascent + boff;
20332 leftmost = 0;
20333
20334 if (font_info
20335 && font_info->default_ascent
20336 && CHAR_TABLE_P (Vuse_default_ascent)
20337 && !NILP (Faref (Vuse_default_ascent,
20338 make_number (it->char_to_display))))
20339 highest = font_info->default_ascent + boff;
20340
20341 /* Draw the first glyph at the normal position. It may be
20342 shifted to right later if some other glyphs are drawn at
20343 the left. */
20344 cmp->offsets[0] = 0;
20345 cmp->offsets[1] = boff;
20346
20347 /* Set cmp->offsets for the remaining glyphs. */
20348 for (i = 1; i < cmp->glyph_len; i++)
20349 {
20350 int left, right, btm, top;
20351 int ch = COMPOSITION_GLYPH (cmp, i);
20352 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20353
20354 face = FACE_FROM_ID (it->f, face_id);
20355 get_char_face_and_encoding (it->f, ch, face->id,
20356 &char2b, it->multibyte_p, 0);
20357 font = face->font;
20358 if (font == NULL)
20359 {
20360 font = FRAME_FONT (it->f);
20361 boff = FRAME_BASELINE_OFFSET (it->f);
20362 font_info = NULL;
20363 }
20364 else
20365 {
20366 font_info
20367 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20368 boff = font_info->baseline_offset;
20369 if (font_info->vertical_centering)
20370 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20371 }
20372
20373 if (font_info
20374 && (pcm = rif->per_char_metric (font, &char2b,
20375 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20376 {
20377 width = pcm->width;
20378 ascent = pcm->ascent;
20379 descent = pcm->descent;
20380 }
20381 else
20382 {
20383 width = FONT_WIDTH (font);
20384 ascent = 1;
20385 descent = 0;
20386 }
20387
20388 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20389 {
20390 /* Relative composition with or without
20391 alternate chars. */
20392 left = (leftmost + rightmost - width) / 2;
20393 btm = - descent + boff;
20394 if (font_info && font_info->relative_compose
20395 && (! CHAR_TABLE_P (Vignore_relative_composition)
20396 || NILP (Faref (Vignore_relative_composition,
20397 make_number (ch)))))
20398 {
20399
20400 if (- descent >= font_info->relative_compose)
20401 /* One extra pixel between two glyphs. */
20402 btm = highest + 1;
20403 else if (ascent <= 0)
20404 /* One extra pixel between two glyphs. */
20405 btm = lowest - 1 - ascent - descent;
20406 }
20407 }
20408 else
20409 {
20410 /* A composition rule is specified by an integer
20411 value that encodes global and new reference
20412 points (GREF and NREF). GREF and NREF are
20413 specified by numbers as below:
20414
20415 0---1---2 -- ascent
20416 | |
20417 | |
20418 | |
20419 9--10--11 -- center
20420 | |
20421 ---3---4---5--- baseline
20422 | |
20423 6---7---8 -- descent
20424 */
20425 int rule = COMPOSITION_RULE (cmp, i);
20426 int gref, nref, grefx, grefy, nrefx, nrefy;
20427
20428 COMPOSITION_DECODE_RULE (rule, gref, nref);
20429 grefx = gref % 3, nrefx = nref % 3;
20430 grefy = gref / 3, nrefy = nref / 3;
20431
20432 left = (leftmost
20433 + grefx * (rightmost - leftmost) / 2
20434 - nrefx * width / 2);
20435 btm = ((grefy == 0 ? highest
20436 : grefy == 1 ? 0
20437 : grefy == 2 ? lowest
20438 : (highest + lowest) / 2)
20439 - (nrefy == 0 ? ascent + descent
20440 : nrefy == 1 ? descent - boff
20441 : nrefy == 2 ? 0
20442 : (ascent + descent) / 2));
20443 }
20444
20445 cmp->offsets[i * 2] = left;
20446 cmp->offsets[i * 2 + 1] = btm + descent;
20447
20448 /* Update the bounding box of the overall glyphs. */
20449 right = left + width;
20450 top = btm + descent + ascent;
20451 if (left < leftmost)
20452 leftmost = left;
20453 if (right > rightmost)
20454 rightmost = right;
20455 if (top > highest)
20456 highest = top;
20457 if (btm < lowest)
20458 lowest = btm;
20459 }
20460
20461 /* If there are glyphs whose x-offsets are negative,
20462 shift all glyphs to the right and make all x-offsets
20463 non-negative. */
20464 if (leftmost < 0)
20465 {
20466 for (i = 0; i < cmp->glyph_len; i++)
20467 cmp->offsets[i * 2] -= leftmost;
20468 rightmost -= leftmost;
20469 }
20470
20471 cmp->pixel_width = rightmost;
20472 cmp->ascent = highest;
20473 cmp->descent = - lowest;
20474 if (cmp->ascent < font_ascent)
20475 cmp->ascent = font_ascent;
20476 if (cmp->descent < font_descent)
20477 cmp->descent = font_descent;
20478 }
20479
20480 it->pixel_width = cmp->pixel_width;
20481 it->ascent = it->phys_ascent = cmp->ascent;
20482 it->descent = it->phys_descent = cmp->descent;
20483
20484 if (face->box != FACE_NO_BOX)
20485 {
20486 int thick = face->box_line_width;
20487
20488 if (thick > 0)
20489 {
20490 it->ascent += thick;
20491 it->descent += thick;
20492 }
20493 else
20494 thick = - thick;
20495
20496 if (it->start_of_box_run_p)
20497 it->pixel_width += thick;
20498 if (it->end_of_box_run_p)
20499 it->pixel_width += thick;
20500 }
20501
20502 /* If face has an overline, add the height of the overline
20503 (1 pixel) and a 1 pixel margin to the character height. */
20504 if (face->overline_p)
20505 it->ascent += 2;
20506
20507 take_vertical_position_into_account (it);
20508
20509 if (it->glyph_row)
20510 append_composite_glyph (it);
20511 }
20512 else if (it->what == IT_IMAGE)
20513 produce_image_glyph (it);
20514 else if (it->what == IT_STRETCH)
20515 produce_stretch_glyph (it);
20516
20517 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20518 because this isn't true for images with `:ascent 100'. */
20519 xassert (it->ascent >= 0 && it->descent >= 0);
20520 if (it->area == TEXT_AREA)
20521 it->current_x += it->pixel_width;
20522
20523 if (extra_line_spacing > 0)
20524 {
20525 it->descent += extra_line_spacing;
20526 if (extra_line_spacing > it->max_extra_line_spacing)
20527 it->max_extra_line_spacing = extra_line_spacing;
20528 }
20529
20530 it->max_ascent = max (it->max_ascent, it->ascent);
20531 it->max_descent = max (it->max_descent, it->descent);
20532 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20533 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20534 }
20535
20536 /* EXPORT for RIF:
20537 Output LEN glyphs starting at START at the nominal cursor position.
20538 Advance the nominal cursor over the text. The global variable
20539 updated_window contains the window being updated, updated_row is
20540 the glyph row being updated, and updated_area is the area of that
20541 row being updated. */
20542
20543 void
20544 x_write_glyphs (start, len)
20545 struct glyph *start;
20546 int len;
20547 {
20548 int x, hpos;
20549
20550 xassert (updated_window && updated_row);
20551 BLOCK_INPUT;
20552
20553 /* Write glyphs. */
20554
20555 hpos = start - updated_row->glyphs[updated_area];
20556 x = draw_glyphs (updated_window, output_cursor.x,
20557 updated_row, updated_area,
20558 hpos, hpos + len,
20559 DRAW_NORMAL_TEXT, 0);
20560
20561 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20562 if (updated_area == TEXT_AREA
20563 && updated_window->phys_cursor_on_p
20564 && updated_window->phys_cursor.vpos == output_cursor.vpos
20565 && updated_window->phys_cursor.hpos >= hpos
20566 && updated_window->phys_cursor.hpos < hpos + len)
20567 updated_window->phys_cursor_on_p = 0;
20568
20569 UNBLOCK_INPUT;
20570
20571 /* Advance the output cursor. */
20572 output_cursor.hpos += len;
20573 output_cursor.x = x;
20574 }
20575
20576
20577 /* EXPORT for RIF:
20578 Insert LEN glyphs from START at the nominal cursor position. */
20579
20580 void
20581 x_insert_glyphs (start, len)
20582 struct glyph *start;
20583 int len;
20584 {
20585 struct frame *f;
20586 struct window *w;
20587 int line_height, shift_by_width, shifted_region_width;
20588 struct glyph_row *row;
20589 struct glyph *glyph;
20590 int frame_x, frame_y, hpos;
20591
20592 xassert (updated_window && updated_row);
20593 BLOCK_INPUT;
20594 w = updated_window;
20595 f = XFRAME (WINDOW_FRAME (w));
20596
20597 /* Get the height of the line we are in. */
20598 row = updated_row;
20599 line_height = row->height;
20600
20601 /* Get the width of the glyphs to insert. */
20602 shift_by_width = 0;
20603 for (glyph = start; glyph < start + len; ++glyph)
20604 shift_by_width += glyph->pixel_width;
20605
20606 /* Get the width of the region to shift right. */
20607 shifted_region_width = (window_box_width (w, updated_area)
20608 - output_cursor.x
20609 - shift_by_width);
20610
20611 /* Shift right. */
20612 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20613 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20614
20615 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20616 line_height, shift_by_width);
20617
20618 /* Write the glyphs. */
20619 hpos = start - row->glyphs[updated_area];
20620 draw_glyphs (w, output_cursor.x, row, updated_area,
20621 hpos, hpos + len,
20622 DRAW_NORMAL_TEXT, 0);
20623
20624 /* Advance the output cursor. */
20625 output_cursor.hpos += len;
20626 output_cursor.x += shift_by_width;
20627 UNBLOCK_INPUT;
20628 }
20629
20630
20631 /* EXPORT for RIF:
20632 Erase the current text line from the nominal cursor position
20633 (inclusive) to pixel column TO_X (exclusive). The idea is that
20634 everything from TO_X onward is already erased.
20635
20636 TO_X is a pixel position relative to updated_area of
20637 updated_window. TO_X == -1 means clear to the end of this area. */
20638
20639 void
20640 x_clear_end_of_line (to_x)
20641 int to_x;
20642 {
20643 struct frame *f;
20644 struct window *w = updated_window;
20645 int max_x, min_y, max_y;
20646 int from_x, from_y, to_y;
20647
20648 xassert (updated_window && updated_row);
20649 f = XFRAME (w->frame);
20650
20651 if (updated_row->full_width_p)
20652 max_x = WINDOW_TOTAL_WIDTH (w);
20653 else
20654 max_x = window_box_width (w, updated_area);
20655 max_y = window_text_bottom_y (w);
20656
20657 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20658 of window. For TO_X > 0, truncate to end of drawing area. */
20659 if (to_x == 0)
20660 return;
20661 else if (to_x < 0)
20662 to_x = max_x;
20663 else
20664 to_x = min (to_x, max_x);
20665
20666 to_y = min (max_y, output_cursor.y + updated_row->height);
20667
20668 /* Notice if the cursor will be cleared by this operation. */
20669 if (!updated_row->full_width_p)
20670 notice_overwritten_cursor (w, updated_area,
20671 output_cursor.x, -1,
20672 updated_row->y,
20673 MATRIX_ROW_BOTTOM_Y (updated_row));
20674
20675 from_x = output_cursor.x;
20676
20677 /* Translate to frame coordinates. */
20678 if (updated_row->full_width_p)
20679 {
20680 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20681 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20682 }
20683 else
20684 {
20685 int area_left = window_box_left (w, updated_area);
20686 from_x += area_left;
20687 to_x += area_left;
20688 }
20689
20690 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20691 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20692 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20693
20694 /* Prevent inadvertently clearing to end of the X window. */
20695 if (to_x > from_x && to_y > from_y)
20696 {
20697 BLOCK_INPUT;
20698 rif->clear_frame_area (f, from_x, from_y,
20699 to_x - from_x, to_y - from_y);
20700 UNBLOCK_INPUT;
20701 }
20702 }
20703
20704 #endif /* HAVE_WINDOW_SYSTEM */
20705
20706
20707 \f
20708 /***********************************************************************
20709 Cursor types
20710 ***********************************************************************/
20711
20712 /* Value is the internal representation of the specified cursor type
20713 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20714 of the bar cursor. */
20715
20716 static enum text_cursor_kinds
20717 get_specified_cursor_type (arg, width)
20718 Lisp_Object arg;
20719 int *width;
20720 {
20721 enum text_cursor_kinds type;
20722
20723 if (NILP (arg))
20724 return NO_CURSOR;
20725
20726 if (EQ (arg, Qbox))
20727 return FILLED_BOX_CURSOR;
20728
20729 if (EQ (arg, Qhollow))
20730 return HOLLOW_BOX_CURSOR;
20731
20732 if (EQ (arg, Qbar))
20733 {
20734 *width = 2;
20735 return BAR_CURSOR;
20736 }
20737
20738 if (CONSP (arg)
20739 && EQ (XCAR (arg), Qbar)
20740 && INTEGERP (XCDR (arg))
20741 && XINT (XCDR (arg)) >= 0)
20742 {
20743 *width = XINT (XCDR (arg));
20744 return BAR_CURSOR;
20745 }
20746
20747 if (EQ (arg, Qhbar))
20748 {
20749 *width = 2;
20750 return HBAR_CURSOR;
20751 }
20752
20753 if (CONSP (arg)
20754 && EQ (XCAR (arg), Qhbar)
20755 && INTEGERP (XCDR (arg))
20756 && XINT (XCDR (arg)) >= 0)
20757 {
20758 *width = XINT (XCDR (arg));
20759 return HBAR_CURSOR;
20760 }
20761
20762 /* Treat anything unknown as "hollow box cursor".
20763 It was bad to signal an error; people have trouble fixing
20764 .Xdefaults with Emacs, when it has something bad in it. */
20765 type = HOLLOW_BOX_CURSOR;
20766
20767 return type;
20768 }
20769
20770 /* Set the default cursor types for specified frame. */
20771 void
20772 set_frame_cursor_types (f, arg)
20773 struct frame *f;
20774 Lisp_Object arg;
20775 {
20776 int width;
20777 Lisp_Object tem;
20778
20779 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20780 FRAME_CURSOR_WIDTH (f) = width;
20781
20782 /* By default, set up the blink-off state depending on the on-state. */
20783
20784 tem = Fassoc (arg, Vblink_cursor_alist);
20785 if (!NILP (tem))
20786 {
20787 FRAME_BLINK_OFF_CURSOR (f)
20788 = get_specified_cursor_type (XCDR (tem), &width);
20789 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20790 }
20791 else
20792 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20793 }
20794
20795
20796 /* Return the cursor we want to be displayed in window W. Return
20797 width of bar/hbar cursor through WIDTH arg. Return with
20798 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20799 (i.e. if the `system caret' should track this cursor).
20800
20801 In a mini-buffer window, we want the cursor only to appear if we
20802 are reading input from this window. For the selected window, we
20803 want the cursor type given by the frame parameter or buffer local
20804 setting of cursor-type. If explicitly marked off, draw no cursor.
20805 In all other cases, we want a hollow box cursor. */
20806
20807 static enum text_cursor_kinds
20808 get_window_cursor_type (w, glyph, width, active_cursor)
20809 struct window *w;
20810 struct glyph *glyph;
20811 int *width;
20812 int *active_cursor;
20813 {
20814 struct frame *f = XFRAME (w->frame);
20815 struct buffer *b = XBUFFER (w->buffer);
20816 int cursor_type = DEFAULT_CURSOR;
20817 Lisp_Object alt_cursor;
20818 int non_selected = 0;
20819
20820 *active_cursor = 1;
20821
20822 /* Echo area */
20823 if (cursor_in_echo_area
20824 && FRAME_HAS_MINIBUF_P (f)
20825 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20826 {
20827 if (w == XWINDOW (echo_area_window))
20828 {
20829 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
20830 {
20831 *width = FRAME_CURSOR_WIDTH (f);
20832 return FRAME_DESIRED_CURSOR (f);
20833 }
20834 else
20835 return get_specified_cursor_type (b->cursor_type, width);
20836 }
20837
20838 *active_cursor = 0;
20839 non_selected = 1;
20840 }
20841
20842 /* Nonselected window or nonselected frame. */
20843 else if (w != XWINDOW (f->selected_window)
20844 #ifdef HAVE_WINDOW_SYSTEM
20845 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20846 #endif
20847 )
20848 {
20849 *active_cursor = 0;
20850
20851 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20852 return NO_CURSOR;
20853
20854 non_selected = 1;
20855 }
20856
20857 /* Never display a cursor in a window in which cursor-type is nil. */
20858 if (NILP (b->cursor_type))
20859 return NO_CURSOR;
20860
20861 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20862 if (non_selected)
20863 {
20864 alt_cursor = b->cursor_in_non_selected_windows;
20865 return get_specified_cursor_type (alt_cursor, width);
20866 }
20867
20868 /* Get the normal cursor type for this window. */
20869 if (EQ (b->cursor_type, Qt))
20870 {
20871 cursor_type = FRAME_DESIRED_CURSOR (f);
20872 *width = FRAME_CURSOR_WIDTH (f);
20873 }
20874 else
20875 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20876
20877 /* Use normal cursor if not blinked off. */
20878 if (!w->cursor_off_p)
20879 {
20880 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20881 if (cursor_type == FILLED_BOX_CURSOR)
20882 cursor_type = HOLLOW_BOX_CURSOR;
20883 }
20884 return cursor_type;
20885 }
20886
20887 /* Cursor is blinked off, so determine how to "toggle" it. */
20888
20889 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20890 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20891 return get_specified_cursor_type (XCDR (alt_cursor), width);
20892
20893 /* Then see if frame has specified a specific blink off cursor type. */
20894 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20895 {
20896 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20897 return FRAME_BLINK_OFF_CURSOR (f);
20898 }
20899
20900 #if 0
20901 /* Some people liked having a permanently visible blinking cursor,
20902 while others had very strong opinions against it. So it was
20903 decided to remove it. KFS 2003-09-03 */
20904
20905 /* Finally perform built-in cursor blinking:
20906 filled box <-> hollow box
20907 wide [h]bar <-> narrow [h]bar
20908 narrow [h]bar <-> no cursor
20909 other type <-> no cursor */
20910
20911 if (cursor_type == FILLED_BOX_CURSOR)
20912 return HOLLOW_BOX_CURSOR;
20913
20914 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20915 {
20916 *width = 1;
20917 return cursor_type;
20918 }
20919 #endif
20920
20921 return NO_CURSOR;
20922 }
20923
20924
20925 #ifdef HAVE_WINDOW_SYSTEM
20926
20927 /* Notice when the text cursor of window W has been completely
20928 overwritten by a drawing operation that outputs glyphs in AREA
20929 starting at X0 and ending at X1 in the line starting at Y0 and
20930 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20931 the rest of the line after X0 has been written. Y coordinates
20932 are window-relative. */
20933
20934 static void
20935 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20936 struct window *w;
20937 enum glyph_row_area area;
20938 int x0, y0, x1, y1;
20939 {
20940 int cx0, cx1, cy0, cy1;
20941 struct glyph_row *row;
20942
20943 if (!w->phys_cursor_on_p)
20944 return;
20945 if (area != TEXT_AREA)
20946 return;
20947
20948 if (w->phys_cursor.vpos < 0
20949 || w->phys_cursor.vpos >= w->current_matrix->nrows
20950 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20951 !(row->enabled_p && row->displays_text_p)))
20952 return;
20953
20954 if (row->cursor_in_fringe_p)
20955 {
20956 row->cursor_in_fringe_p = 0;
20957 draw_fringe_bitmap (w, row, 0);
20958 w->phys_cursor_on_p = 0;
20959 return;
20960 }
20961
20962 cx0 = w->phys_cursor.x;
20963 cx1 = cx0 + w->phys_cursor_width;
20964 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20965 return;
20966
20967 /* The cursor image will be completely removed from the
20968 screen if the output area intersects the cursor area in
20969 y-direction. When we draw in [y0 y1[, and some part of
20970 the cursor is at y < y0, that part must have been drawn
20971 before. When scrolling, the cursor is erased before
20972 actually scrolling, so we don't come here. When not
20973 scrolling, the rows above the old cursor row must have
20974 changed, and in this case these rows must have written
20975 over the cursor image.
20976
20977 Likewise if part of the cursor is below y1, with the
20978 exception of the cursor being in the first blank row at
20979 the buffer and window end because update_text_area
20980 doesn't draw that row. (Except when it does, but
20981 that's handled in update_text_area.) */
20982
20983 cy0 = w->phys_cursor.y;
20984 cy1 = cy0 + w->phys_cursor_height;
20985 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20986 return;
20987
20988 w->phys_cursor_on_p = 0;
20989 }
20990
20991 #endif /* HAVE_WINDOW_SYSTEM */
20992
20993 \f
20994 /************************************************************************
20995 Mouse Face
20996 ************************************************************************/
20997
20998 #ifdef HAVE_WINDOW_SYSTEM
20999
21000 /* EXPORT for RIF:
21001 Fix the display of area AREA of overlapping row ROW in window W
21002 with respect to the overlapping part OVERLAPS. */
21003
21004 void
21005 x_fix_overlapping_area (w, row, area, overlaps)
21006 struct window *w;
21007 struct glyph_row *row;
21008 enum glyph_row_area area;
21009 int overlaps;
21010 {
21011 int i, x;
21012
21013 BLOCK_INPUT;
21014
21015 x = 0;
21016 for (i = 0; i < row->used[area];)
21017 {
21018 if (row->glyphs[area][i].overlaps_vertically_p)
21019 {
21020 int start = i, start_x = x;
21021
21022 do
21023 {
21024 x += row->glyphs[area][i].pixel_width;
21025 ++i;
21026 }
21027 while (i < row->used[area]
21028 && row->glyphs[area][i].overlaps_vertically_p);
21029
21030 draw_glyphs (w, start_x, row, area,
21031 start, i,
21032 DRAW_NORMAL_TEXT, overlaps);
21033 }
21034 else
21035 {
21036 x += row->glyphs[area][i].pixel_width;
21037 ++i;
21038 }
21039 }
21040
21041 UNBLOCK_INPUT;
21042 }
21043
21044
21045 /* EXPORT:
21046 Draw the cursor glyph of window W in glyph row ROW. See the
21047 comment of draw_glyphs for the meaning of HL. */
21048
21049 void
21050 draw_phys_cursor_glyph (w, row, hl)
21051 struct window *w;
21052 struct glyph_row *row;
21053 enum draw_glyphs_face hl;
21054 {
21055 /* If cursor hpos is out of bounds, don't draw garbage. This can
21056 happen in mini-buffer windows when switching between echo area
21057 glyphs and mini-buffer. */
21058 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21059 {
21060 int on_p = w->phys_cursor_on_p;
21061 int x1;
21062 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21063 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21064 hl, 0);
21065 w->phys_cursor_on_p = on_p;
21066
21067 if (hl == DRAW_CURSOR)
21068 w->phys_cursor_width = x1 - w->phys_cursor.x;
21069 /* When we erase the cursor, and ROW is overlapped by other
21070 rows, make sure that these overlapping parts of other rows
21071 are redrawn. */
21072 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21073 {
21074 w->phys_cursor_width = x1 - w->phys_cursor.x;
21075
21076 if (row > w->current_matrix->rows
21077 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21078 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21079 OVERLAPS_ERASED_CURSOR);
21080
21081 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21082 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21083 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21084 OVERLAPS_ERASED_CURSOR);
21085 }
21086 }
21087 }
21088
21089
21090 /* EXPORT:
21091 Erase the image of a cursor of window W from the screen. */
21092
21093 void
21094 erase_phys_cursor (w)
21095 struct window *w;
21096 {
21097 struct frame *f = XFRAME (w->frame);
21098 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21099 int hpos = w->phys_cursor.hpos;
21100 int vpos = w->phys_cursor.vpos;
21101 int mouse_face_here_p = 0;
21102 struct glyph_matrix *active_glyphs = w->current_matrix;
21103 struct glyph_row *cursor_row;
21104 struct glyph *cursor_glyph;
21105 enum draw_glyphs_face hl;
21106
21107 /* No cursor displayed or row invalidated => nothing to do on the
21108 screen. */
21109 if (w->phys_cursor_type == NO_CURSOR)
21110 goto mark_cursor_off;
21111
21112 /* VPOS >= active_glyphs->nrows means that window has been resized.
21113 Don't bother to erase the cursor. */
21114 if (vpos >= active_glyphs->nrows)
21115 goto mark_cursor_off;
21116
21117 /* If row containing cursor is marked invalid, there is nothing we
21118 can do. */
21119 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21120 if (!cursor_row->enabled_p)
21121 goto mark_cursor_off;
21122
21123 /* If line spacing is > 0, old cursor may only be partially visible in
21124 window after split-window. So adjust visible height. */
21125 cursor_row->visible_height = min (cursor_row->visible_height,
21126 window_text_bottom_y (w) - cursor_row->y);
21127
21128 /* If row is completely invisible, don't attempt to delete a cursor which
21129 isn't there. This can happen if cursor is at top of a window, and
21130 we switch to a buffer with a header line in that window. */
21131 if (cursor_row->visible_height <= 0)
21132 goto mark_cursor_off;
21133
21134 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21135 if (cursor_row->cursor_in_fringe_p)
21136 {
21137 cursor_row->cursor_in_fringe_p = 0;
21138 draw_fringe_bitmap (w, cursor_row, 0);
21139 goto mark_cursor_off;
21140 }
21141
21142 /* This can happen when the new row is shorter than the old one.
21143 In this case, either draw_glyphs or clear_end_of_line
21144 should have cleared the cursor. Note that we wouldn't be
21145 able to erase the cursor in this case because we don't have a
21146 cursor glyph at hand. */
21147 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21148 goto mark_cursor_off;
21149
21150 /* If the cursor is in the mouse face area, redisplay that when
21151 we clear the cursor. */
21152 if (! NILP (dpyinfo->mouse_face_window)
21153 && w == XWINDOW (dpyinfo->mouse_face_window)
21154 && (vpos > dpyinfo->mouse_face_beg_row
21155 || (vpos == dpyinfo->mouse_face_beg_row
21156 && hpos >= dpyinfo->mouse_face_beg_col))
21157 && (vpos < dpyinfo->mouse_face_end_row
21158 || (vpos == dpyinfo->mouse_face_end_row
21159 && hpos < dpyinfo->mouse_face_end_col))
21160 /* Don't redraw the cursor's spot in mouse face if it is at the
21161 end of a line (on a newline). The cursor appears there, but
21162 mouse highlighting does not. */
21163 && cursor_row->used[TEXT_AREA] > hpos)
21164 mouse_face_here_p = 1;
21165
21166 /* Maybe clear the display under the cursor. */
21167 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21168 {
21169 int x, y;
21170 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21171 int width;
21172
21173 cursor_glyph = get_phys_cursor_glyph (w);
21174 if (cursor_glyph == NULL)
21175 goto mark_cursor_off;
21176
21177 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21178 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21179 width = min (cursor_glyph->pixel_width,
21180 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
21181
21182 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21183 }
21184
21185 /* Erase the cursor by redrawing the character underneath it. */
21186 if (mouse_face_here_p)
21187 hl = DRAW_MOUSE_FACE;
21188 else
21189 hl = DRAW_NORMAL_TEXT;
21190 draw_phys_cursor_glyph (w, cursor_row, hl);
21191
21192 mark_cursor_off:
21193 w->phys_cursor_on_p = 0;
21194 w->phys_cursor_type = NO_CURSOR;
21195 }
21196
21197
21198 /* EXPORT:
21199 Display or clear cursor of window W. If ON is zero, clear the
21200 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21201 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21202
21203 void
21204 display_and_set_cursor (w, on, hpos, vpos, x, y)
21205 struct window *w;
21206 int on, hpos, vpos, x, y;
21207 {
21208 struct frame *f = XFRAME (w->frame);
21209 int new_cursor_type;
21210 int new_cursor_width;
21211 int active_cursor;
21212 struct glyph_row *glyph_row;
21213 struct glyph *glyph;
21214
21215 /* This is pointless on invisible frames, and dangerous on garbaged
21216 windows and frames; in the latter case, the frame or window may
21217 be in the midst of changing its size, and x and y may be off the
21218 window. */
21219 if (! FRAME_VISIBLE_P (f)
21220 || FRAME_GARBAGED_P (f)
21221 || vpos >= w->current_matrix->nrows
21222 || hpos >= w->current_matrix->matrix_w)
21223 return;
21224
21225 /* If cursor is off and we want it off, return quickly. */
21226 if (!on && !w->phys_cursor_on_p)
21227 return;
21228
21229 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21230 /* If cursor row is not enabled, we don't really know where to
21231 display the cursor. */
21232 if (!glyph_row->enabled_p)
21233 {
21234 w->phys_cursor_on_p = 0;
21235 return;
21236 }
21237
21238 glyph = NULL;
21239 if (!glyph_row->exact_window_width_line_p
21240 || hpos < glyph_row->used[TEXT_AREA])
21241 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21242
21243 xassert (interrupt_input_blocked);
21244
21245 /* Set new_cursor_type to the cursor we want to be displayed. */
21246 new_cursor_type = get_window_cursor_type (w, glyph,
21247 &new_cursor_width, &active_cursor);
21248
21249 /* If cursor is currently being shown and we don't want it to be or
21250 it is in the wrong place, or the cursor type is not what we want,
21251 erase it. */
21252 if (w->phys_cursor_on_p
21253 && (!on
21254 || w->phys_cursor.x != x
21255 || w->phys_cursor.y != y
21256 || new_cursor_type != w->phys_cursor_type
21257 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21258 && new_cursor_width != w->phys_cursor_width)))
21259 erase_phys_cursor (w);
21260
21261 /* Don't check phys_cursor_on_p here because that flag is only set
21262 to zero in some cases where we know that the cursor has been
21263 completely erased, to avoid the extra work of erasing the cursor
21264 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21265 still not be visible, or it has only been partly erased. */
21266 if (on)
21267 {
21268 w->phys_cursor_ascent = glyph_row->ascent;
21269 w->phys_cursor_height = glyph_row->height;
21270
21271 /* Set phys_cursor_.* before x_draw_.* is called because some
21272 of them may need the information. */
21273 w->phys_cursor.x = x;
21274 w->phys_cursor.y = glyph_row->y;
21275 w->phys_cursor.hpos = hpos;
21276 w->phys_cursor.vpos = vpos;
21277 }
21278
21279 rif->draw_window_cursor (w, glyph_row, x, y,
21280 new_cursor_type, new_cursor_width,
21281 on, active_cursor);
21282 }
21283
21284
21285 /* Switch the display of W's cursor on or off, according to the value
21286 of ON. */
21287
21288 static void
21289 update_window_cursor (w, on)
21290 struct window *w;
21291 int on;
21292 {
21293 /* Don't update cursor in windows whose frame is in the process
21294 of being deleted. */
21295 if (w->current_matrix)
21296 {
21297 BLOCK_INPUT;
21298 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21299 w->phys_cursor.x, w->phys_cursor.y);
21300 UNBLOCK_INPUT;
21301 }
21302 }
21303
21304
21305 /* Call update_window_cursor with parameter ON_P on all leaf windows
21306 in the window tree rooted at W. */
21307
21308 static void
21309 update_cursor_in_window_tree (w, on_p)
21310 struct window *w;
21311 int on_p;
21312 {
21313 while (w)
21314 {
21315 if (!NILP (w->hchild))
21316 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21317 else if (!NILP (w->vchild))
21318 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21319 else
21320 update_window_cursor (w, on_p);
21321
21322 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21323 }
21324 }
21325
21326
21327 /* EXPORT:
21328 Display the cursor on window W, or clear it, according to ON_P.
21329 Don't change the cursor's position. */
21330
21331 void
21332 x_update_cursor (f, on_p)
21333 struct frame *f;
21334 int on_p;
21335 {
21336 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21337 }
21338
21339
21340 /* EXPORT:
21341 Clear the cursor of window W to background color, and mark the
21342 cursor as not shown. This is used when the text where the cursor
21343 is is about to be rewritten. */
21344
21345 void
21346 x_clear_cursor (w)
21347 struct window *w;
21348 {
21349 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21350 update_window_cursor (w, 0);
21351 }
21352
21353
21354 /* EXPORT:
21355 Display the active region described by mouse_face_* according to DRAW. */
21356
21357 void
21358 show_mouse_face (dpyinfo, draw)
21359 Display_Info *dpyinfo;
21360 enum draw_glyphs_face draw;
21361 {
21362 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21363 struct frame *f = XFRAME (WINDOW_FRAME (w));
21364
21365 if (/* If window is in the process of being destroyed, don't bother
21366 to do anything. */
21367 w->current_matrix != NULL
21368 /* Don't update mouse highlight if hidden */
21369 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21370 /* Recognize when we are called to operate on rows that don't exist
21371 anymore. This can happen when a window is split. */
21372 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21373 {
21374 int phys_cursor_on_p = w->phys_cursor_on_p;
21375 struct glyph_row *row, *first, *last;
21376
21377 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21378 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21379
21380 for (row = first; row <= last && row->enabled_p; ++row)
21381 {
21382 int start_hpos, end_hpos, start_x;
21383
21384 /* For all but the first row, the highlight starts at column 0. */
21385 if (row == first)
21386 {
21387 start_hpos = dpyinfo->mouse_face_beg_col;
21388 start_x = dpyinfo->mouse_face_beg_x;
21389 }
21390 else
21391 {
21392 start_hpos = 0;
21393 start_x = 0;
21394 }
21395
21396 if (row == last)
21397 end_hpos = dpyinfo->mouse_face_end_col;
21398 else
21399 {
21400 end_hpos = row->used[TEXT_AREA];
21401 if (draw == DRAW_NORMAL_TEXT)
21402 row->fill_line_p = 1; /* Clear to end of line */
21403 }
21404
21405 if (end_hpos > start_hpos)
21406 {
21407 draw_glyphs (w, start_x, row, TEXT_AREA,
21408 start_hpos, end_hpos,
21409 draw, 0);
21410
21411 row->mouse_face_p
21412 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21413 }
21414 }
21415
21416 /* When we've written over the cursor, arrange for it to
21417 be displayed again. */
21418 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21419 {
21420 BLOCK_INPUT;
21421 display_and_set_cursor (w, 1,
21422 w->phys_cursor.hpos, w->phys_cursor.vpos,
21423 w->phys_cursor.x, w->phys_cursor.y);
21424 UNBLOCK_INPUT;
21425 }
21426 }
21427
21428 /* Change the mouse cursor. */
21429 if (draw == DRAW_NORMAL_TEXT)
21430 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21431 else if (draw == DRAW_MOUSE_FACE)
21432 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21433 else
21434 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21435 }
21436
21437 /* EXPORT:
21438 Clear out the mouse-highlighted active region.
21439 Redraw it un-highlighted first. Value is non-zero if mouse
21440 face was actually drawn unhighlighted. */
21441
21442 int
21443 clear_mouse_face (dpyinfo)
21444 Display_Info *dpyinfo;
21445 {
21446 int cleared = 0;
21447
21448 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21449 {
21450 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21451 cleared = 1;
21452 }
21453
21454 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21455 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21456 dpyinfo->mouse_face_window = Qnil;
21457 dpyinfo->mouse_face_overlay = Qnil;
21458 return cleared;
21459 }
21460
21461
21462 /* EXPORT:
21463 Non-zero if physical cursor of window W is within mouse face. */
21464
21465 int
21466 cursor_in_mouse_face_p (w)
21467 struct window *w;
21468 {
21469 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21470 int in_mouse_face = 0;
21471
21472 if (WINDOWP (dpyinfo->mouse_face_window)
21473 && XWINDOW (dpyinfo->mouse_face_window) == w)
21474 {
21475 int hpos = w->phys_cursor.hpos;
21476 int vpos = w->phys_cursor.vpos;
21477
21478 if (vpos >= dpyinfo->mouse_face_beg_row
21479 && vpos <= dpyinfo->mouse_face_end_row
21480 && (vpos > dpyinfo->mouse_face_beg_row
21481 || hpos >= dpyinfo->mouse_face_beg_col)
21482 && (vpos < dpyinfo->mouse_face_end_row
21483 || hpos < dpyinfo->mouse_face_end_col
21484 || dpyinfo->mouse_face_past_end))
21485 in_mouse_face = 1;
21486 }
21487
21488 return in_mouse_face;
21489 }
21490
21491
21492
21493 \f
21494 /* Find the glyph matrix position of buffer position CHARPOS in window
21495 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21496 current glyphs must be up to date. If CHARPOS is above window
21497 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21498 of last line in W. In the row containing CHARPOS, stop before glyphs
21499 having STOP as object. */
21500
21501 #if 1 /* This is a version of fast_find_position that's more correct
21502 in the presence of hscrolling, for example. I didn't install
21503 it right away because the problem fixed is minor, it failed
21504 in 20.x as well, and I think it's too risky to install
21505 so near the release of 21.1. 2001-09-25 gerd. */
21506
21507 static int
21508 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21509 struct window *w;
21510 int charpos;
21511 int *hpos, *vpos, *x, *y;
21512 Lisp_Object stop;
21513 {
21514 struct glyph_row *row, *first;
21515 struct glyph *glyph, *end;
21516 int past_end = 0;
21517
21518 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21519 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21520 {
21521 *x = first->x;
21522 *y = first->y;
21523 *hpos = 0;
21524 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21525 return 1;
21526 }
21527
21528 row = row_containing_pos (w, charpos, first, NULL, 0);
21529 if (row == NULL)
21530 {
21531 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21532 past_end = 1;
21533 }
21534
21535 /* If whole rows or last part of a row came from a display overlay,
21536 row_containing_pos will skip over such rows because their end pos
21537 equals the start pos of the overlay or interval.
21538
21539 Move back if we have a STOP object and previous row's
21540 end glyph came from STOP. */
21541 if (!NILP (stop))
21542 {
21543 struct glyph_row *prev;
21544 while ((prev = row - 1, prev >= first)
21545 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21546 && prev->used[TEXT_AREA] > 0)
21547 {
21548 struct glyph *beg = prev->glyphs[TEXT_AREA];
21549 glyph = beg + prev->used[TEXT_AREA];
21550 while (--glyph >= beg
21551 && INTEGERP (glyph->object));
21552 if (glyph < beg
21553 || !EQ (stop, glyph->object))
21554 break;
21555 row = prev;
21556 }
21557 }
21558
21559 *x = row->x;
21560 *y = row->y;
21561 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21562
21563 glyph = row->glyphs[TEXT_AREA];
21564 end = glyph + row->used[TEXT_AREA];
21565
21566 /* Skip over glyphs not having an object at the start of the row.
21567 These are special glyphs like truncation marks on terminal
21568 frames. */
21569 if (row->displays_text_p)
21570 while (glyph < end
21571 && INTEGERP (glyph->object)
21572 && !EQ (stop, glyph->object)
21573 && glyph->charpos < 0)
21574 {
21575 *x += glyph->pixel_width;
21576 ++glyph;
21577 }
21578
21579 while (glyph < end
21580 && !INTEGERP (glyph->object)
21581 && !EQ (stop, glyph->object)
21582 && (!BUFFERP (glyph->object)
21583 || glyph->charpos < charpos))
21584 {
21585 *x += glyph->pixel_width;
21586 ++glyph;
21587 }
21588
21589 *hpos = glyph - row->glyphs[TEXT_AREA];
21590 return !past_end;
21591 }
21592
21593 #else /* not 1 */
21594
21595 static int
21596 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21597 struct window *w;
21598 int pos;
21599 int *hpos, *vpos, *x, *y;
21600 Lisp_Object stop;
21601 {
21602 int i;
21603 int lastcol;
21604 int maybe_next_line_p = 0;
21605 int line_start_position;
21606 int yb = window_text_bottom_y (w);
21607 struct glyph_row *row, *best_row;
21608 int row_vpos, best_row_vpos;
21609 int current_x;
21610
21611 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21612 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21613
21614 while (row->y < yb)
21615 {
21616 if (row->used[TEXT_AREA])
21617 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21618 else
21619 line_start_position = 0;
21620
21621 if (line_start_position > pos)
21622 break;
21623 /* If the position sought is the end of the buffer,
21624 don't include the blank lines at the bottom of the window. */
21625 else if (line_start_position == pos
21626 && pos == BUF_ZV (XBUFFER (w->buffer)))
21627 {
21628 maybe_next_line_p = 1;
21629 break;
21630 }
21631 else if (line_start_position > 0)
21632 {
21633 best_row = row;
21634 best_row_vpos = row_vpos;
21635 }
21636
21637 if (row->y + row->height >= yb)
21638 break;
21639
21640 ++row;
21641 ++row_vpos;
21642 }
21643
21644 /* Find the right column within BEST_ROW. */
21645 lastcol = 0;
21646 current_x = best_row->x;
21647 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21648 {
21649 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21650 int charpos = glyph->charpos;
21651
21652 if (BUFFERP (glyph->object))
21653 {
21654 if (charpos == pos)
21655 {
21656 *hpos = i;
21657 *vpos = best_row_vpos;
21658 *x = current_x;
21659 *y = best_row->y;
21660 return 1;
21661 }
21662 else if (charpos > pos)
21663 break;
21664 }
21665 else if (EQ (glyph->object, stop))
21666 break;
21667
21668 if (charpos > 0)
21669 lastcol = i;
21670 current_x += glyph->pixel_width;
21671 }
21672
21673 /* If we're looking for the end of the buffer,
21674 and we didn't find it in the line we scanned,
21675 use the start of the following line. */
21676 if (maybe_next_line_p)
21677 {
21678 ++best_row;
21679 ++best_row_vpos;
21680 lastcol = 0;
21681 current_x = best_row->x;
21682 }
21683
21684 *vpos = best_row_vpos;
21685 *hpos = lastcol + 1;
21686 *x = current_x;
21687 *y = best_row->y;
21688 return 0;
21689 }
21690
21691 #endif /* not 1 */
21692
21693
21694 /* Find the position of the glyph for position POS in OBJECT in
21695 window W's current matrix, and return in *X, *Y the pixel
21696 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21697
21698 RIGHT_P non-zero means return the position of the right edge of the
21699 glyph, RIGHT_P zero means return the left edge position.
21700
21701 If no glyph for POS exists in the matrix, return the position of
21702 the glyph with the next smaller position that is in the matrix, if
21703 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21704 exists in the matrix, return the position of the glyph with the
21705 next larger position in OBJECT.
21706
21707 Value is non-zero if a glyph was found. */
21708
21709 static int
21710 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21711 struct window *w;
21712 int pos;
21713 Lisp_Object object;
21714 int *hpos, *vpos, *x, *y;
21715 int right_p;
21716 {
21717 int yb = window_text_bottom_y (w);
21718 struct glyph_row *r;
21719 struct glyph *best_glyph = NULL;
21720 struct glyph_row *best_row = NULL;
21721 int best_x = 0;
21722
21723 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21724 r->enabled_p && r->y < yb;
21725 ++r)
21726 {
21727 struct glyph *g = r->glyphs[TEXT_AREA];
21728 struct glyph *e = g + r->used[TEXT_AREA];
21729 int gx;
21730
21731 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21732 if (EQ (g->object, object))
21733 {
21734 if (g->charpos == pos)
21735 {
21736 best_glyph = g;
21737 best_x = gx;
21738 best_row = r;
21739 goto found;
21740 }
21741 else if (best_glyph == NULL
21742 || ((abs (g->charpos - pos)
21743 < abs (best_glyph->charpos - pos))
21744 && (right_p
21745 ? g->charpos < pos
21746 : g->charpos > pos)))
21747 {
21748 best_glyph = g;
21749 best_x = gx;
21750 best_row = r;
21751 }
21752 }
21753 }
21754
21755 found:
21756
21757 if (best_glyph)
21758 {
21759 *x = best_x;
21760 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21761
21762 if (right_p)
21763 {
21764 *x += best_glyph->pixel_width;
21765 ++*hpos;
21766 }
21767
21768 *y = best_row->y;
21769 *vpos = best_row - w->current_matrix->rows;
21770 }
21771
21772 return best_glyph != NULL;
21773 }
21774
21775
21776 /* See if position X, Y is within a hot-spot of an image. */
21777
21778 static int
21779 on_hot_spot_p (hot_spot, x, y)
21780 Lisp_Object hot_spot;
21781 int x, y;
21782 {
21783 if (!CONSP (hot_spot))
21784 return 0;
21785
21786 if (EQ (XCAR (hot_spot), Qrect))
21787 {
21788 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21789 Lisp_Object rect = XCDR (hot_spot);
21790 Lisp_Object tem;
21791 if (!CONSP (rect))
21792 return 0;
21793 if (!CONSP (XCAR (rect)))
21794 return 0;
21795 if (!CONSP (XCDR (rect)))
21796 return 0;
21797 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21798 return 0;
21799 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21800 return 0;
21801 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21802 return 0;
21803 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21804 return 0;
21805 return 1;
21806 }
21807 else if (EQ (XCAR (hot_spot), Qcircle))
21808 {
21809 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21810 Lisp_Object circ = XCDR (hot_spot);
21811 Lisp_Object lr, lx0, ly0;
21812 if (CONSP (circ)
21813 && CONSP (XCAR (circ))
21814 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21815 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21816 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21817 {
21818 double r = XFLOATINT (lr);
21819 double dx = XINT (lx0) - x;
21820 double dy = XINT (ly0) - y;
21821 return (dx * dx + dy * dy <= r * r);
21822 }
21823 }
21824 else if (EQ (XCAR (hot_spot), Qpoly))
21825 {
21826 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21827 if (VECTORP (XCDR (hot_spot)))
21828 {
21829 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21830 Lisp_Object *poly = v->contents;
21831 int n = v->size;
21832 int i;
21833 int inside = 0;
21834 Lisp_Object lx, ly;
21835 int x0, y0;
21836
21837 /* Need an even number of coordinates, and at least 3 edges. */
21838 if (n < 6 || n & 1)
21839 return 0;
21840
21841 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21842 If count is odd, we are inside polygon. Pixels on edges
21843 may or may not be included depending on actual geometry of the
21844 polygon. */
21845 if ((lx = poly[n-2], !INTEGERP (lx))
21846 || (ly = poly[n-1], !INTEGERP (lx)))
21847 return 0;
21848 x0 = XINT (lx), y0 = XINT (ly);
21849 for (i = 0; i < n; i += 2)
21850 {
21851 int x1 = x0, y1 = y0;
21852 if ((lx = poly[i], !INTEGERP (lx))
21853 || (ly = poly[i+1], !INTEGERP (ly)))
21854 return 0;
21855 x0 = XINT (lx), y0 = XINT (ly);
21856
21857 /* Does this segment cross the X line? */
21858 if (x0 >= x)
21859 {
21860 if (x1 >= x)
21861 continue;
21862 }
21863 else if (x1 < x)
21864 continue;
21865 if (y > y0 && y > y1)
21866 continue;
21867 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21868 inside = !inside;
21869 }
21870 return inside;
21871 }
21872 }
21873 /* If we don't understand the format, pretend we're not in the hot-spot. */
21874 return 0;
21875 }
21876
21877 Lisp_Object
21878 find_hot_spot (map, x, y)
21879 Lisp_Object map;
21880 int x, y;
21881 {
21882 while (CONSP (map))
21883 {
21884 if (CONSP (XCAR (map))
21885 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21886 return XCAR (map);
21887 map = XCDR (map);
21888 }
21889
21890 return Qnil;
21891 }
21892
21893 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21894 3, 3, 0,
21895 doc: /* Lookup in image map MAP coordinates X and Y.
21896 An image map is an alist where each element has the format (AREA ID PLIST).
21897 An AREA is specified as either a rectangle, a circle, or a polygon:
21898 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21899 pixel coordinates of the upper left and bottom right corners.
21900 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21901 and the radius of the circle; r may be a float or integer.
21902 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21903 vector describes one corner in the polygon.
21904 Returns the alist element for the first matching AREA in MAP. */)
21905 (map, x, y)
21906 Lisp_Object map;
21907 Lisp_Object x, y;
21908 {
21909 if (NILP (map))
21910 return Qnil;
21911
21912 CHECK_NUMBER (x);
21913 CHECK_NUMBER (y);
21914
21915 return find_hot_spot (map, XINT (x), XINT (y));
21916 }
21917
21918
21919 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21920 static void
21921 define_frame_cursor1 (f, cursor, pointer)
21922 struct frame *f;
21923 Cursor cursor;
21924 Lisp_Object pointer;
21925 {
21926 /* Do not change cursor shape while dragging mouse. */
21927 if (!NILP (do_mouse_tracking))
21928 return;
21929
21930 if (!NILP (pointer))
21931 {
21932 if (EQ (pointer, Qarrow))
21933 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21934 else if (EQ (pointer, Qhand))
21935 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21936 else if (EQ (pointer, Qtext))
21937 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21938 else if (EQ (pointer, intern ("hdrag")))
21939 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21940 #ifdef HAVE_X_WINDOWS
21941 else if (EQ (pointer, intern ("vdrag")))
21942 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21943 #endif
21944 else if (EQ (pointer, intern ("hourglass")))
21945 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21946 else if (EQ (pointer, Qmodeline))
21947 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21948 else
21949 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21950 }
21951
21952 if (cursor != No_Cursor)
21953 rif->define_frame_cursor (f, cursor);
21954 }
21955
21956 /* Take proper action when mouse has moved to the mode or header line
21957 or marginal area AREA of window W, x-position X and y-position Y.
21958 X is relative to the start of the text display area of W, so the
21959 width of bitmap areas and scroll bars must be subtracted to get a
21960 position relative to the start of the mode line. */
21961
21962 static void
21963 note_mode_line_or_margin_highlight (window, x, y, area)
21964 Lisp_Object window;
21965 int x, y;
21966 enum window_part area;
21967 {
21968 struct window *w = XWINDOW (window);
21969 struct frame *f = XFRAME (w->frame);
21970 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21971 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21972 Lisp_Object pointer = Qnil;
21973 int charpos, dx, dy, width, height;
21974 Lisp_Object string, object = Qnil;
21975 Lisp_Object pos, help;
21976
21977 Lisp_Object mouse_face;
21978 int original_x_pixel = x;
21979 struct glyph * glyph = NULL;
21980 struct glyph_row *row;
21981
21982 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21983 {
21984 int x0;
21985 struct glyph *end;
21986
21987 string = mode_line_string (w, area, &x, &y, &charpos,
21988 &object, &dx, &dy, &width, &height);
21989
21990 row = (area == ON_MODE_LINE
21991 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21992 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21993
21994 /* Find glyph */
21995 if (row->mode_line_p && row->enabled_p)
21996 {
21997 glyph = row->glyphs[TEXT_AREA];
21998 end = glyph + row->used[TEXT_AREA];
21999
22000 for (x0 = original_x_pixel;
22001 glyph < end && x0 >= glyph->pixel_width;
22002 ++glyph)
22003 x0 -= glyph->pixel_width;
22004
22005 if (glyph >= end)
22006 glyph = NULL;
22007 }
22008 }
22009 else
22010 {
22011 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22012 string = marginal_area_string (w, area, &x, &y, &charpos,
22013 &object, &dx, &dy, &width, &height);
22014 }
22015
22016 help = Qnil;
22017
22018 if (IMAGEP (object))
22019 {
22020 Lisp_Object image_map, hotspot;
22021 if ((image_map = Fplist_get (XCDR (object), QCmap),
22022 !NILP (image_map))
22023 && (hotspot = find_hot_spot (image_map, dx, dy),
22024 CONSP (hotspot))
22025 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22026 {
22027 Lisp_Object area_id, plist;
22028
22029 area_id = XCAR (hotspot);
22030 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22031 If so, we could look for mouse-enter, mouse-leave
22032 properties in PLIST (and do something...). */
22033 hotspot = XCDR (hotspot);
22034 if (CONSP (hotspot)
22035 && (plist = XCAR (hotspot), CONSP (plist)))
22036 {
22037 pointer = Fplist_get (plist, Qpointer);
22038 if (NILP (pointer))
22039 pointer = Qhand;
22040 help = Fplist_get (plist, Qhelp_echo);
22041 if (!NILP (help))
22042 {
22043 help_echo_string = help;
22044 /* Is this correct? ++kfs */
22045 XSETWINDOW (help_echo_window, w);
22046 help_echo_object = w->buffer;
22047 help_echo_pos = charpos;
22048 }
22049 }
22050 }
22051 if (NILP (pointer))
22052 pointer = Fplist_get (XCDR (object), QCpointer);
22053 }
22054
22055 if (STRINGP (string))
22056 {
22057 pos = make_number (charpos);
22058 /* If we're on a string with `help-echo' text property, arrange
22059 for the help to be displayed. This is done by setting the
22060 global variable help_echo_string to the help string. */
22061 if (NILP (help))
22062 {
22063 help = Fget_text_property (pos, Qhelp_echo, string);
22064 if (!NILP (help))
22065 {
22066 help_echo_string = help;
22067 XSETWINDOW (help_echo_window, w);
22068 help_echo_object = string;
22069 help_echo_pos = charpos;
22070 }
22071 }
22072
22073 if (NILP (pointer))
22074 pointer = Fget_text_property (pos, Qpointer, string);
22075
22076 /* Change the mouse pointer according to what is under X/Y. */
22077 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22078 {
22079 Lisp_Object map;
22080 map = Fget_text_property (pos, Qlocal_map, string);
22081 if (!KEYMAPP (map))
22082 map = Fget_text_property (pos, Qkeymap, string);
22083 if (!KEYMAPP (map))
22084 cursor = dpyinfo->vertical_scroll_bar_cursor;
22085 }
22086
22087 /* Change the mouse face according to what is under X/Y. */
22088 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22089 if (!NILP (mouse_face)
22090 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22091 && glyph)
22092 {
22093 Lisp_Object b, e;
22094
22095 struct glyph * tmp_glyph;
22096
22097 int gpos;
22098 int gseq_length;
22099 int total_pixel_width;
22100 int ignore;
22101
22102 int vpos, hpos;
22103
22104 b = Fprevious_single_property_change (make_number (charpos + 1),
22105 Qmouse_face, string, Qnil);
22106 if (NILP (b))
22107 b = make_number (0);
22108
22109 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22110 if (NILP (e))
22111 e = make_number (SCHARS (string));
22112
22113 /* Calculate the position(glyph position: GPOS) of GLYPH in
22114 displayed string. GPOS is different from CHARPOS.
22115
22116 CHARPOS is the position of glyph in internal string
22117 object. A mode line string format has structures which
22118 is converted to a flatten by emacs lisp interpreter.
22119 The internal string is an element of the structures.
22120 The displayed string is the flatten string. */
22121 for (tmp_glyph = glyph - 1, gpos = 0;
22122 tmp_glyph->charpos >= XINT (b);
22123 tmp_glyph--, gpos++)
22124 {
22125 if (!EQ (tmp_glyph->object, glyph->object))
22126 break;
22127 }
22128
22129 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22130 displayed string holding GLYPH.
22131
22132 GSEQ_LENGTH is different from SCHARS (STRING).
22133 SCHARS (STRING) returns the length of the internal string. */
22134 for (tmp_glyph = glyph, gseq_length = gpos;
22135 tmp_glyph->charpos < XINT (e);
22136 tmp_glyph++, gseq_length++)
22137 {
22138 if (!EQ (tmp_glyph->object, glyph->object))
22139 break;
22140 }
22141
22142 total_pixel_width = 0;
22143 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22144 total_pixel_width += tmp_glyph->pixel_width;
22145
22146 /* Pre calculation of re-rendering position */
22147 vpos = (x - gpos);
22148 hpos = (area == ON_MODE_LINE
22149 ? (w->current_matrix)->nrows - 1
22150 : 0);
22151
22152 /* If the re-rendering position is included in the last
22153 re-rendering area, we should do nothing. */
22154 if ( EQ (window, dpyinfo->mouse_face_window)
22155 && dpyinfo->mouse_face_beg_col <= vpos
22156 && vpos < dpyinfo->mouse_face_end_col
22157 && dpyinfo->mouse_face_beg_row == hpos )
22158 return;
22159
22160 if (clear_mouse_face (dpyinfo))
22161 cursor = No_Cursor;
22162
22163 dpyinfo->mouse_face_beg_col = vpos;
22164 dpyinfo->mouse_face_beg_row = hpos;
22165
22166 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22167 dpyinfo->mouse_face_beg_y = 0;
22168
22169 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22170 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22171
22172 dpyinfo->mouse_face_end_x = 0;
22173 dpyinfo->mouse_face_end_y = 0;
22174
22175 dpyinfo->mouse_face_past_end = 0;
22176 dpyinfo->mouse_face_window = window;
22177
22178 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22179 charpos,
22180 0, 0, 0, &ignore,
22181 glyph->face_id, 1);
22182 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22183
22184 if (NILP (pointer))
22185 pointer = Qhand;
22186 }
22187 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22188 clear_mouse_face (dpyinfo);
22189 }
22190 define_frame_cursor1 (f, cursor, pointer);
22191 }
22192
22193
22194 /* EXPORT:
22195 Take proper action when the mouse has moved to position X, Y on
22196 frame F as regards highlighting characters that have mouse-face
22197 properties. Also de-highlighting chars where the mouse was before.
22198 X and Y can be negative or out of range. */
22199
22200 void
22201 note_mouse_highlight (f, x, y)
22202 struct frame *f;
22203 int x, y;
22204 {
22205 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22206 enum window_part part;
22207 Lisp_Object window;
22208 struct window *w;
22209 Cursor cursor = No_Cursor;
22210 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22211 struct buffer *b;
22212
22213 /* When a menu is active, don't highlight because this looks odd. */
22214 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22215 if (popup_activated ())
22216 return;
22217 #endif
22218
22219 if (NILP (Vmouse_highlight)
22220 || !f->glyphs_initialized_p)
22221 return;
22222
22223 dpyinfo->mouse_face_mouse_x = x;
22224 dpyinfo->mouse_face_mouse_y = y;
22225 dpyinfo->mouse_face_mouse_frame = f;
22226
22227 if (dpyinfo->mouse_face_defer)
22228 return;
22229
22230 if (gc_in_progress)
22231 {
22232 dpyinfo->mouse_face_deferred_gc = 1;
22233 return;
22234 }
22235
22236 /* Which window is that in? */
22237 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22238
22239 /* If we were displaying active text in another window, clear that.
22240 Also clear if we move out of text area in same window. */
22241 if (! EQ (window, dpyinfo->mouse_face_window)
22242 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22243 && !NILP (dpyinfo->mouse_face_window)))
22244 clear_mouse_face (dpyinfo);
22245
22246 /* Not on a window -> return. */
22247 if (!WINDOWP (window))
22248 return;
22249
22250 /* Reset help_echo_string. It will get recomputed below. */
22251 help_echo_string = Qnil;
22252
22253 /* Convert to window-relative pixel coordinates. */
22254 w = XWINDOW (window);
22255 frame_to_window_pixel_xy (w, &x, &y);
22256
22257 /* Handle tool-bar window differently since it doesn't display a
22258 buffer. */
22259 if (EQ (window, f->tool_bar_window))
22260 {
22261 note_tool_bar_highlight (f, x, y);
22262 return;
22263 }
22264
22265 /* Mouse is on the mode, header line or margin? */
22266 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22267 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22268 {
22269 note_mode_line_or_margin_highlight (window, x, y, part);
22270 return;
22271 }
22272
22273 if (part == ON_VERTICAL_BORDER)
22274 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22275 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22276 || part == ON_SCROLL_BAR)
22277 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22278 else
22279 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22280
22281 /* Are we in a window whose display is up to date?
22282 And verify the buffer's text has not changed. */
22283 b = XBUFFER (w->buffer);
22284 if (part == ON_TEXT
22285 && EQ (w->window_end_valid, w->buffer)
22286 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22287 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22288 {
22289 int hpos, vpos, pos, i, dx, dy, area;
22290 struct glyph *glyph;
22291 Lisp_Object object;
22292 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22293 Lisp_Object *overlay_vec = NULL;
22294 int noverlays;
22295 struct buffer *obuf;
22296 int obegv, ozv, same_region;
22297
22298 /* Find the glyph under X/Y. */
22299 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22300
22301 /* Look for :pointer property on image. */
22302 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22303 {
22304 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22305 if (img != NULL && IMAGEP (img->spec))
22306 {
22307 Lisp_Object image_map, hotspot;
22308 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22309 !NILP (image_map))
22310 && (hotspot = find_hot_spot (image_map,
22311 glyph->slice.x + dx,
22312 glyph->slice.y + dy),
22313 CONSP (hotspot))
22314 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22315 {
22316 Lisp_Object area_id, plist;
22317
22318 area_id = XCAR (hotspot);
22319 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22320 If so, we could look for mouse-enter, mouse-leave
22321 properties in PLIST (and do something...). */
22322 hotspot = XCDR (hotspot);
22323 if (CONSP (hotspot)
22324 && (plist = XCAR (hotspot), CONSP (plist)))
22325 {
22326 pointer = Fplist_get (plist, Qpointer);
22327 if (NILP (pointer))
22328 pointer = Qhand;
22329 help_echo_string = Fplist_get (plist, Qhelp_echo);
22330 if (!NILP (help_echo_string))
22331 {
22332 help_echo_window = window;
22333 help_echo_object = glyph->object;
22334 help_echo_pos = glyph->charpos;
22335 }
22336 }
22337 }
22338 if (NILP (pointer))
22339 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22340 }
22341 }
22342
22343 /* Clear mouse face if X/Y not over text. */
22344 if (glyph == NULL
22345 || area != TEXT_AREA
22346 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22347 {
22348 if (clear_mouse_face (dpyinfo))
22349 cursor = No_Cursor;
22350 if (NILP (pointer))
22351 {
22352 if (area != TEXT_AREA)
22353 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22354 else
22355 pointer = Vvoid_text_area_pointer;
22356 }
22357 goto set_cursor;
22358 }
22359
22360 pos = glyph->charpos;
22361 object = glyph->object;
22362 if (!STRINGP (object) && !BUFFERP (object))
22363 goto set_cursor;
22364
22365 /* If we get an out-of-range value, return now; avoid an error. */
22366 if (BUFFERP (object) && pos > BUF_Z (b))
22367 goto set_cursor;
22368
22369 /* Make the window's buffer temporarily current for
22370 overlays_at and compute_char_face. */
22371 obuf = current_buffer;
22372 current_buffer = b;
22373 obegv = BEGV;
22374 ozv = ZV;
22375 BEGV = BEG;
22376 ZV = Z;
22377
22378 /* Is this char mouse-active or does it have help-echo? */
22379 position = make_number (pos);
22380
22381 if (BUFFERP (object))
22382 {
22383 /* Put all the overlays we want in a vector in overlay_vec. */
22384 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22385 /* Sort overlays into increasing priority order. */
22386 noverlays = sort_overlays (overlay_vec, noverlays, w);
22387 }
22388 else
22389 noverlays = 0;
22390
22391 same_region = (EQ (window, dpyinfo->mouse_face_window)
22392 && vpos >= dpyinfo->mouse_face_beg_row
22393 && vpos <= dpyinfo->mouse_face_end_row
22394 && (vpos > dpyinfo->mouse_face_beg_row
22395 || hpos >= dpyinfo->mouse_face_beg_col)
22396 && (vpos < dpyinfo->mouse_face_end_row
22397 || hpos < dpyinfo->mouse_face_end_col
22398 || dpyinfo->mouse_face_past_end));
22399
22400 if (same_region)
22401 cursor = No_Cursor;
22402
22403 /* Check mouse-face highlighting. */
22404 if (! same_region
22405 /* If there exists an overlay with mouse-face overlapping
22406 the one we are currently highlighting, we have to
22407 check if we enter the overlapping overlay, and then
22408 highlight only that. */
22409 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22410 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22411 {
22412 /* Find the highest priority overlay that has a mouse-face
22413 property. */
22414 overlay = Qnil;
22415 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22416 {
22417 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22418 if (!NILP (mouse_face))
22419 overlay = overlay_vec[i];
22420 }
22421
22422 /* If we're actually highlighting the same overlay as
22423 before, there's no need to do that again. */
22424 if (!NILP (overlay)
22425 && EQ (overlay, dpyinfo->mouse_face_overlay))
22426 goto check_help_echo;
22427
22428 dpyinfo->mouse_face_overlay = overlay;
22429
22430 /* Clear the display of the old active region, if any. */
22431 if (clear_mouse_face (dpyinfo))
22432 cursor = No_Cursor;
22433
22434 /* If no overlay applies, get a text property. */
22435 if (NILP (overlay))
22436 mouse_face = Fget_text_property (position, Qmouse_face, object);
22437
22438 /* Handle the overlay case. */
22439 if (!NILP (overlay))
22440 {
22441 /* Find the range of text around this char that
22442 should be active. */
22443 Lisp_Object before, after;
22444 int ignore;
22445
22446 before = Foverlay_start (overlay);
22447 after = Foverlay_end (overlay);
22448 /* Record this as the current active region. */
22449 fast_find_position (w, XFASTINT (before),
22450 &dpyinfo->mouse_face_beg_col,
22451 &dpyinfo->mouse_face_beg_row,
22452 &dpyinfo->mouse_face_beg_x,
22453 &dpyinfo->mouse_face_beg_y, Qnil);
22454
22455 dpyinfo->mouse_face_past_end
22456 = !fast_find_position (w, XFASTINT (after),
22457 &dpyinfo->mouse_face_end_col,
22458 &dpyinfo->mouse_face_end_row,
22459 &dpyinfo->mouse_face_end_x,
22460 &dpyinfo->mouse_face_end_y, Qnil);
22461 dpyinfo->mouse_face_window = window;
22462
22463 dpyinfo->mouse_face_face_id
22464 = face_at_buffer_position (w, pos, 0, 0,
22465 &ignore, pos + 1,
22466 !dpyinfo->mouse_face_hidden);
22467
22468 /* Display it as active. */
22469 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22470 cursor = No_Cursor;
22471 }
22472 /* Handle the text property case. */
22473 else if (!NILP (mouse_face) && BUFFERP (object))
22474 {
22475 /* Find the range of text around this char that
22476 should be active. */
22477 Lisp_Object before, after, beginning, end;
22478 int ignore;
22479
22480 beginning = Fmarker_position (w->start);
22481 end = make_number (BUF_Z (XBUFFER (object))
22482 - XFASTINT (w->window_end_pos));
22483 before
22484 = Fprevious_single_property_change (make_number (pos + 1),
22485 Qmouse_face,
22486 object, beginning);
22487 after
22488 = Fnext_single_property_change (position, Qmouse_face,
22489 object, end);
22490
22491 /* Record this as the current active region. */
22492 fast_find_position (w, XFASTINT (before),
22493 &dpyinfo->mouse_face_beg_col,
22494 &dpyinfo->mouse_face_beg_row,
22495 &dpyinfo->mouse_face_beg_x,
22496 &dpyinfo->mouse_face_beg_y, Qnil);
22497 dpyinfo->mouse_face_past_end
22498 = !fast_find_position (w, XFASTINT (after),
22499 &dpyinfo->mouse_face_end_col,
22500 &dpyinfo->mouse_face_end_row,
22501 &dpyinfo->mouse_face_end_x,
22502 &dpyinfo->mouse_face_end_y, Qnil);
22503 dpyinfo->mouse_face_window = window;
22504
22505 if (BUFFERP (object))
22506 dpyinfo->mouse_face_face_id
22507 = face_at_buffer_position (w, pos, 0, 0,
22508 &ignore, pos + 1,
22509 !dpyinfo->mouse_face_hidden);
22510
22511 /* Display it as active. */
22512 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22513 cursor = No_Cursor;
22514 }
22515 else if (!NILP (mouse_face) && STRINGP (object))
22516 {
22517 Lisp_Object b, e;
22518 int ignore;
22519
22520 b = Fprevious_single_property_change (make_number (pos + 1),
22521 Qmouse_face,
22522 object, Qnil);
22523 e = Fnext_single_property_change (position, Qmouse_face,
22524 object, Qnil);
22525 if (NILP (b))
22526 b = make_number (0);
22527 if (NILP (e))
22528 e = make_number (SCHARS (object) - 1);
22529
22530 fast_find_string_pos (w, XINT (b), object,
22531 &dpyinfo->mouse_face_beg_col,
22532 &dpyinfo->mouse_face_beg_row,
22533 &dpyinfo->mouse_face_beg_x,
22534 &dpyinfo->mouse_face_beg_y, 0);
22535 fast_find_string_pos (w, XINT (e), object,
22536 &dpyinfo->mouse_face_end_col,
22537 &dpyinfo->mouse_face_end_row,
22538 &dpyinfo->mouse_face_end_x,
22539 &dpyinfo->mouse_face_end_y, 1);
22540 dpyinfo->mouse_face_past_end = 0;
22541 dpyinfo->mouse_face_window = window;
22542 dpyinfo->mouse_face_face_id
22543 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22544 glyph->face_id, 1);
22545 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22546 cursor = No_Cursor;
22547 }
22548 else if (STRINGP (object) && NILP (mouse_face))
22549 {
22550 /* A string which doesn't have mouse-face, but
22551 the text ``under'' it might have. */
22552 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22553 int start = MATRIX_ROW_START_CHARPOS (r);
22554
22555 pos = string_buffer_position (w, object, start);
22556 if (pos > 0)
22557 mouse_face = get_char_property_and_overlay (make_number (pos),
22558 Qmouse_face,
22559 w->buffer,
22560 &overlay);
22561 if (!NILP (mouse_face) && !NILP (overlay))
22562 {
22563 Lisp_Object before = Foverlay_start (overlay);
22564 Lisp_Object after = Foverlay_end (overlay);
22565 int ignore;
22566
22567 /* Note that we might not be able to find position
22568 BEFORE in the glyph matrix if the overlay is
22569 entirely covered by a `display' property. In
22570 this case, we overshoot. So let's stop in
22571 the glyph matrix before glyphs for OBJECT. */
22572 fast_find_position (w, XFASTINT (before),
22573 &dpyinfo->mouse_face_beg_col,
22574 &dpyinfo->mouse_face_beg_row,
22575 &dpyinfo->mouse_face_beg_x,
22576 &dpyinfo->mouse_face_beg_y,
22577 object);
22578
22579 dpyinfo->mouse_face_past_end
22580 = !fast_find_position (w, XFASTINT (after),
22581 &dpyinfo->mouse_face_end_col,
22582 &dpyinfo->mouse_face_end_row,
22583 &dpyinfo->mouse_face_end_x,
22584 &dpyinfo->mouse_face_end_y,
22585 Qnil);
22586 dpyinfo->mouse_face_window = window;
22587 dpyinfo->mouse_face_face_id
22588 = face_at_buffer_position (w, pos, 0, 0,
22589 &ignore, pos + 1,
22590 !dpyinfo->mouse_face_hidden);
22591
22592 /* Display it as active. */
22593 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22594 cursor = No_Cursor;
22595 }
22596 }
22597 }
22598
22599 check_help_echo:
22600
22601 /* Look for a `help-echo' property. */
22602 if (NILP (help_echo_string)) {
22603 Lisp_Object help, overlay;
22604
22605 /* Check overlays first. */
22606 help = overlay = Qnil;
22607 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22608 {
22609 overlay = overlay_vec[i];
22610 help = Foverlay_get (overlay, Qhelp_echo);
22611 }
22612
22613 if (!NILP (help))
22614 {
22615 help_echo_string = help;
22616 help_echo_window = window;
22617 help_echo_object = overlay;
22618 help_echo_pos = pos;
22619 }
22620 else
22621 {
22622 Lisp_Object object = glyph->object;
22623 int charpos = glyph->charpos;
22624
22625 /* Try text properties. */
22626 if (STRINGP (object)
22627 && charpos >= 0
22628 && charpos < SCHARS (object))
22629 {
22630 help = Fget_text_property (make_number (charpos),
22631 Qhelp_echo, object);
22632 if (NILP (help))
22633 {
22634 /* If the string itself doesn't specify a help-echo,
22635 see if the buffer text ``under'' it does. */
22636 struct glyph_row *r
22637 = MATRIX_ROW (w->current_matrix, vpos);
22638 int start = MATRIX_ROW_START_CHARPOS (r);
22639 int pos = string_buffer_position (w, object, start);
22640 if (pos > 0)
22641 {
22642 help = Fget_char_property (make_number (pos),
22643 Qhelp_echo, w->buffer);
22644 if (!NILP (help))
22645 {
22646 charpos = pos;
22647 object = w->buffer;
22648 }
22649 }
22650 }
22651 }
22652 else if (BUFFERP (object)
22653 && charpos >= BEGV
22654 && charpos < ZV)
22655 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22656 object);
22657
22658 if (!NILP (help))
22659 {
22660 help_echo_string = help;
22661 help_echo_window = window;
22662 help_echo_object = object;
22663 help_echo_pos = charpos;
22664 }
22665 }
22666 }
22667
22668 /* Look for a `pointer' property. */
22669 if (NILP (pointer))
22670 {
22671 /* Check overlays first. */
22672 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22673 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22674
22675 if (NILP (pointer))
22676 {
22677 Lisp_Object object = glyph->object;
22678 int charpos = glyph->charpos;
22679
22680 /* Try text properties. */
22681 if (STRINGP (object)
22682 && charpos >= 0
22683 && charpos < SCHARS (object))
22684 {
22685 pointer = Fget_text_property (make_number (charpos),
22686 Qpointer, object);
22687 if (NILP (pointer))
22688 {
22689 /* If the string itself doesn't specify a pointer,
22690 see if the buffer text ``under'' it does. */
22691 struct glyph_row *r
22692 = MATRIX_ROW (w->current_matrix, vpos);
22693 int start = MATRIX_ROW_START_CHARPOS (r);
22694 int pos = string_buffer_position (w, object, start);
22695 if (pos > 0)
22696 pointer = Fget_char_property (make_number (pos),
22697 Qpointer, w->buffer);
22698 }
22699 }
22700 else if (BUFFERP (object)
22701 && charpos >= BEGV
22702 && charpos < ZV)
22703 pointer = Fget_text_property (make_number (charpos),
22704 Qpointer, object);
22705 }
22706 }
22707
22708 BEGV = obegv;
22709 ZV = ozv;
22710 current_buffer = obuf;
22711 }
22712
22713 set_cursor:
22714
22715 define_frame_cursor1 (f, cursor, pointer);
22716 }
22717
22718
22719 /* EXPORT for RIF:
22720 Clear any mouse-face on window W. This function is part of the
22721 redisplay interface, and is called from try_window_id and similar
22722 functions to ensure the mouse-highlight is off. */
22723
22724 void
22725 x_clear_window_mouse_face (w)
22726 struct window *w;
22727 {
22728 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22729 Lisp_Object window;
22730
22731 BLOCK_INPUT;
22732 XSETWINDOW (window, w);
22733 if (EQ (window, dpyinfo->mouse_face_window))
22734 clear_mouse_face (dpyinfo);
22735 UNBLOCK_INPUT;
22736 }
22737
22738
22739 /* EXPORT:
22740 Just discard the mouse face information for frame F, if any.
22741 This is used when the size of F is changed. */
22742
22743 void
22744 cancel_mouse_face (f)
22745 struct frame *f;
22746 {
22747 Lisp_Object window;
22748 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22749
22750 window = dpyinfo->mouse_face_window;
22751 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22752 {
22753 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22754 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22755 dpyinfo->mouse_face_window = Qnil;
22756 }
22757 }
22758
22759
22760 #endif /* HAVE_WINDOW_SYSTEM */
22761
22762 \f
22763 /***********************************************************************
22764 Exposure Events
22765 ***********************************************************************/
22766
22767 #ifdef HAVE_WINDOW_SYSTEM
22768
22769 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22770 which intersects rectangle R. R is in window-relative coordinates. */
22771
22772 static void
22773 expose_area (w, row, r, area)
22774 struct window *w;
22775 struct glyph_row *row;
22776 XRectangle *r;
22777 enum glyph_row_area area;
22778 {
22779 struct glyph *first = row->glyphs[area];
22780 struct glyph *end = row->glyphs[area] + row->used[area];
22781 struct glyph *last;
22782 int first_x, start_x, x;
22783
22784 if (area == TEXT_AREA && row->fill_line_p)
22785 /* If row extends face to end of line write the whole line. */
22786 draw_glyphs (w, 0, row, area,
22787 0, row->used[area],
22788 DRAW_NORMAL_TEXT, 0);
22789 else
22790 {
22791 /* Set START_X to the window-relative start position for drawing glyphs of
22792 AREA. The first glyph of the text area can be partially visible.
22793 The first glyphs of other areas cannot. */
22794 start_x = window_box_left_offset (w, area);
22795 x = start_x;
22796 if (area == TEXT_AREA)
22797 x += row->x;
22798
22799 /* Find the first glyph that must be redrawn. */
22800 while (first < end
22801 && x + first->pixel_width < r->x)
22802 {
22803 x += first->pixel_width;
22804 ++first;
22805 }
22806
22807 /* Find the last one. */
22808 last = first;
22809 first_x = x;
22810 while (last < end
22811 && x < r->x + r->width)
22812 {
22813 x += last->pixel_width;
22814 ++last;
22815 }
22816
22817 /* Repaint. */
22818 if (last > first)
22819 draw_glyphs (w, first_x - start_x, row, area,
22820 first - row->glyphs[area], last - row->glyphs[area],
22821 DRAW_NORMAL_TEXT, 0);
22822 }
22823 }
22824
22825
22826 /* Redraw the parts of the glyph row ROW on window W intersecting
22827 rectangle R. R is in window-relative coordinates. Value is
22828 non-zero if mouse-face was overwritten. */
22829
22830 static int
22831 expose_line (w, row, r)
22832 struct window *w;
22833 struct glyph_row *row;
22834 XRectangle *r;
22835 {
22836 xassert (row->enabled_p);
22837
22838 if (row->mode_line_p || w->pseudo_window_p)
22839 draw_glyphs (w, 0, row, TEXT_AREA,
22840 0, row->used[TEXT_AREA],
22841 DRAW_NORMAL_TEXT, 0);
22842 else
22843 {
22844 if (row->used[LEFT_MARGIN_AREA])
22845 expose_area (w, row, r, LEFT_MARGIN_AREA);
22846 if (row->used[TEXT_AREA])
22847 expose_area (w, row, r, TEXT_AREA);
22848 if (row->used[RIGHT_MARGIN_AREA])
22849 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22850 draw_row_fringe_bitmaps (w, row);
22851 }
22852
22853 return row->mouse_face_p;
22854 }
22855
22856
22857 /* Redraw those parts of glyphs rows during expose event handling that
22858 overlap other rows. Redrawing of an exposed line writes over parts
22859 of lines overlapping that exposed line; this function fixes that.
22860
22861 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22862 row in W's current matrix that is exposed and overlaps other rows.
22863 LAST_OVERLAPPING_ROW is the last such row. */
22864
22865 static void
22866 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22867 struct window *w;
22868 struct glyph_row *first_overlapping_row;
22869 struct glyph_row *last_overlapping_row;
22870 {
22871 struct glyph_row *row;
22872
22873 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22874 if (row->overlapping_p)
22875 {
22876 xassert (row->enabled_p && !row->mode_line_p);
22877
22878 if (row->used[LEFT_MARGIN_AREA])
22879 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
22880
22881 if (row->used[TEXT_AREA])
22882 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
22883
22884 if (row->used[RIGHT_MARGIN_AREA])
22885 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
22886 }
22887 }
22888
22889
22890 /* Return non-zero if W's cursor intersects rectangle R. */
22891
22892 static int
22893 phys_cursor_in_rect_p (w, r)
22894 struct window *w;
22895 XRectangle *r;
22896 {
22897 XRectangle cr, result;
22898 struct glyph *cursor_glyph;
22899
22900 cursor_glyph = get_phys_cursor_glyph (w);
22901 if (cursor_glyph)
22902 {
22903 /* r is relative to W's box, but w->phys_cursor.x is relative
22904 to left edge of W's TEXT area. Adjust it. */
22905 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22906 cr.y = w->phys_cursor.y;
22907 cr.width = cursor_glyph->pixel_width;
22908 cr.height = w->phys_cursor_height;
22909 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22910 I assume the effect is the same -- and this is portable. */
22911 return x_intersect_rectangles (&cr, r, &result);
22912 }
22913 else
22914 return 0;
22915 }
22916
22917
22918 /* EXPORT:
22919 Draw a vertical window border to the right of window W if W doesn't
22920 have vertical scroll bars. */
22921
22922 void
22923 x_draw_vertical_border (w)
22924 struct window *w;
22925 {
22926 /* We could do better, if we knew what type of scroll-bar the adjacent
22927 windows (on either side) have... But we don't :-(
22928 However, I think this works ok. ++KFS 2003-04-25 */
22929
22930 /* Redraw borders between horizontally adjacent windows. Don't
22931 do it for frames with vertical scroll bars because either the
22932 right scroll bar of a window, or the left scroll bar of its
22933 neighbor will suffice as a border. */
22934 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22935 return;
22936
22937 if (!WINDOW_RIGHTMOST_P (w)
22938 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22939 {
22940 int x0, x1, y0, y1;
22941
22942 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22943 y1 -= 1;
22944
22945 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22946 x1 -= 1;
22947
22948 rif->draw_vertical_window_border (w, x1, y0, y1);
22949 }
22950 else if (!WINDOW_LEFTMOST_P (w)
22951 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22952 {
22953 int x0, x1, y0, y1;
22954
22955 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22956 y1 -= 1;
22957
22958 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22959 x0 -= 1;
22960
22961 rif->draw_vertical_window_border (w, x0, y0, y1);
22962 }
22963 }
22964
22965
22966 /* Redraw the part of window W intersection rectangle FR. Pixel
22967 coordinates in FR are frame-relative. Call this function with
22968 input blocked. Value is non-zero if the exposure overwrites
22969 mouse-face. */
22970
22971 static int
22972 expose_window (w, fr)
22973 struct window *w;
22974 XRectangle *fr;
22975 {
22976 struct frame *f = XFRAME (w->frame);
22977 XRectangle wr, r;
22978 int mouse_face_overwritten_p = 0;
22979
22980 /* If window is not yet fully initialized, do nothing. This can
22981 happen when toolkit scroll bars are used and a window is split.
22982 Reconfiguring the scroll bar will generate an expose for a newly
22983 created window. */
22984 if (w->current_matrix == NULL)
22985 return 0;
22986
22987 /* When we're currently updating the window, display and current
22988 matrix usually don't agree. Arrange for a thorough display
22989 later. */
22990 if (w == updated_window)
22991 {
22992 SET_FRAME_GARBAGED (f);
22993 return 0;
22994 }
22995
22996 /* Frame-relative pixel rectangle of W. */
22997 wr.x = WINDOW_LEFT_EDGE_X (w);
22998 wr.y = WINDOW_TOP_EDGE_Y (w);
22999 wr.width = WINDOW_TOTAL_WIDTH (w);
23000 wr.height = WINDOW_TOTAL_HEIGHT (w);
23001
23002 if (x_intersect_rectangles (fr, &wr, &r))
23003 {
23004 int yb = window_text_bottom_y (w);
23005 struct glyph_row *row;
23006 int cursor_cleared_p;
23007 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23008
23009 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23010 r.x, r.y, r.width, r.height));
23011
23012 /* Convert to window coordinates. */
23013 r.x -= WINDOW_LEFT_EDGE_X (w);
23014 r.y -= WINDOW_TOP_EDGE_Y (w);
23015
23016 /* Turn off the cursor. */
23017 if (!w->pseudo_window_p
23018 && phys_cursor_in_rect_p (w, &r))
23019 {
23020 x_clear_cursor (w);
23021 cursor_cleared_p = 1;
23022 }
23023 else
23024 cursor_cleared_p = 0;
23025
23026 /* Update lines intersecting rectangle R. */
23027 first_overlapping_row = last_overlapping_row = NULL;
23028 for (row = w->current_matrix->rows;
23029 row->enabled_p;
23030 ++row)
23031 {
23032 int y0 = row->y;
23033 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23034
23035 if ((y0 >= r.y && y0 < r.y + r.height)
23036 || (y1 > r.y && y1 < r.y + r.height)
23037 || (r.y >= y0 && r.y < y1)
23038 || (r.y + r.height > y0 && r.y + r.height < y1))
23039 {
23040 /* A header line may be overlapping, but there is no need
23041 to fix overlapping areas for them. KFS 2005-02-12 */
23042 if (row->overlapping_p && !row->mode_line_p)
23043 {
23044 if (first_overlapping_row == NULL)
23045 first_overlapping_row = row;
23046 last_overlapping_row = row;
23047 }
23048
23049 if (expose_line (w, row, &r))
23050 mouse_face_overwritten_p = 1;
23051 }
23052
23053 if (y1 >= yb)
23054 break;
23055 }
23056
23057 /* Display the mode line if there is one. */
23058 if (WINDOW_WANTS_MODELINE_P (w)
23059 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23060 row->enabled_p)
23061 && row->y < r.y + r.height)
23062 {
23063 if (expose_line (w, row, &r))
23064 mouse_face_overwritten_p = 1;
23065 }
23066
23067 if (!w->pseudo_window_p)
23068 {
23069 /* Fix the display of overlapping rows. */
23070 if (first_overlapping_row)
23071 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23072
23073 /* Draw border between windows. */
23074 x_draw_vertical_border (w);
23075
23076 /* Turn the cursor on again. */
23077 if (cursor_cleared_p)
23078 update_window_cursor (w, 1);
23079 }
23080 }
23081
23082 return mouse_face_overwritten_p;
23083 }
23084
23085
23086
23087 /* Redraw (parts) of all windows in the window tree rooted at W that
23088 intersect R. R contains frame pixel coordinates. Value is
23089 non-zero if the exposure overwrites mouse-face. */
23090
23091 static int
23092 expose_window_tree (w, r)
23093 struct window *w;
23094 XRectangle *r;
23095 {
23096 struct frame *f = XFRAME (w->frame);
23097 int mouse_face_overwritten_p = 0;
23098
23099 while (w && !FRAME_GARBAGED_P (f))
23100 {
23101 if (!NILP (w->hchild))
23102 mouse_face_overwritten_p
23103 |= expose_window_tree (XWINDOW (w->hchild), r);
23104 else if (!NILP (w->vchild))
23105 mouse_face_overwritten_p
23106 |= expose_window_tree (XWINDOW (w->vchild), r);
23107 else
23108 mouse_face_overwritten_p |= expose_window (w, r);
23109
23110 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23111 }
23112
23113 return mouse_face_overwritten_p;
23114 }
23115
23116
23117 /* EXPORT:
23118 Redisplay an exposed area of frame F. X and Y are the upper-left
23119 corner of the exposed rectangle. W and H are width and height of
23120 the exposed area. All are pixel values. W or H zero means redraw
23121 the entire frame. */
23122
23123 void
23124 expose_frame (f, x, y, w, h)
23125 struct frame *f;
23126 int x, y, w, h;
23127 {
23128 XRectangle r;
23129 int mouse_face_overwritten_p = 0;
23130
23131 TRACE ((stderr, "expose_frame "));
23132
23133 /* No need to redraw if frame will be redrawn soon. */
23134 if (FRAME_GARBAGED_P (f))
23135 {
23136 TRACE ((stderr, " garbaged\n"));
23137 return;
23138 }
23139
23140 /* If basic faces haven't been realized yet, there is no point in
23141 trying to redraw anything. This can happen when we get an expose
23142 event while Emacs is starting, e.g. by moving another window. */
23143 if (FRAME_FACE_CACHE (f) == NULL
23144 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23145 {
23146 TRACE ((stderr, " no faces\n"));
23147 return;
23148 }
23149
23150 if (w == 0 || h == 0)
23151 {
23152 r.x = r.y = 0;
23153 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23154 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23155 }
23156 else
23157 {
23158 r.x = x;
23159 r.y = y;
23160 r.width = w;
23161 r.height = h;
23162 }
23163
23164 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23165 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23166
23167 if (WINDOWP (f->tool_bar_window))
23168 mouse_face_overwritten_p
23169 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23170
23171 #ifdef HAVE_X_WINDOWS
23172 #ifndef MSDOS
23173 #ifndef USE_X_TOOLKIT
23174 if (WINDOWP (f->menu_bar_window))
23175 mouse_face_overwritten_p
23176 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23177 #endif /* not USE_X_TOOLKIT */
23178 #endif
23179 #endif
23180
23181 /* Some window managers support a focus-follows-mouse style with
23182 delayed raising of frames. Imagine a partially obscured frame,
23183 and moving the mouse into partially obscured mouse-face on that
23184 frame. The visible part of the mouse-face will be highlighted,
23185 then the WM raises the obscured frame. With at least one WM, KDE
23186 2.1, Emacs is not getting any event for the raising of the frame
23187 (even tried with SubstructureRedirectMask), only Expose events.
23188 These expose events will draw text normally, i.e. not
23189 highlighted. Which means we must redo the highlight here.
23190 Subsume it under ``we love X''. --gerd 2001-08-15 */
23191 /* Included in Windows version because Windows most likely does not
23192 do the right thing if any third party tool offers
23193 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23194 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23195 {
23196 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23197 if (f == dpyinfo->mouse_face_mouse_frame)
23198 {
23199 int x = dpyinfo->mouse_face_mouse_x;
23200 int y = dpyinfo->mouse_face_mouse_y;
23201 clear_mouse_face (dpyinfo);
23202 note_mouse_highlight (f, x, y);
23203 }
23204 }
23205 }
23206
23207
23208 /* EXPORT:
23209 Determine the intersection of two rectangles R1 and R2. Return
23210 the intersection in *RESULT. Value is non-zero if RESULT is not
23211 empty. */
23212
23213 int
23214 x_intersect_rectangles (r1, r2, result)
23215 XRectangle *r1, *r2, *result;
23216 {
23217 XRectangle *left, *right;
23218 XRectangle *upper, *lower;
23219 int intersection_p = 0;
23220
23221 /* Rearrange so that R1 is the left-most rectangle. */
23222 if (r1->x < r2->x)
23223 left = r1, right = r2;
23224 else
23225 left = r2, right = r1;
23226
23227 /* X0 of the intersection is right.x0, if this is inside R1,
23228 otherwise there is no intersection. */
23229 if (right->x <= left->x + left->width)
23230 {
23231 result->x = right->x;
23232
23233 /* The right end of the intersection is the minimum of the
23234 the right ends of left and right. */
23235 result->width = (min (left->x + left->width, right->x + right->width)
23236 - result->x);
23237
23238 /* Same game for Y. */
23239 if (r1->y < r2->y)
23240 upper = r1, lower = r2;
23241 else
23242 upper = r2, lower = r1;
23243
23244 /* The upper end of the intersection is lower.y0, if this is inside
23245 of upper. Otherwise, there is no intersection. */
23246 if (lower->y <= upper->y + upper->height)
23247 {
23248 result->y = lower->y;
23249
23250 /* The lower end of the intersection is the minimum of the lower
23251 ends of upper and lower. */
23252 result->height = (min (lower->y + lower->height,
23253 upper->y + upper->height)
23254 - result->y);
23255 intersection_p = 1;
23256 }
23257 }
23258
23259 return intersection_p;
23260 }
23261
23262 #endif /* HAVE_WINDOW_SYSTEM */
23263
23264 \f
23265 /***********************************************************************
23266 Initialization
23267 ***********************************************************************/
23268
23269 void
23270 syms_of_xdisp ()
23271 {
23272 Vwith_echo_area_save_vector = Qnil;
23273 staticpro (&Vwith_echo_area_save_vector);
23274
23275 Vmessage_stack = Qnil;
23276 staticpro (&Vmessage_stack);
23277
23278 Qinhibit_redisplay = intern ("inhibit-redisplay");
23279 staticpro (&Qinhibit_redisplay);
23280
23281 message_dolog_marker1 = Fmake_marker ();
23282 staticpro (&message_dolog_marker1);
23283 message_dolog_marker2 = Fmake_marker ();
23284 staticpro (&message_dolog_marker2);
23285 message_dolog_marker3 = Fmake_marker ();
23286 staticpro (&message_dolog_marker3);
23287
23288 #if GLYPH_DEBUG
23289 defsubr (&Sdump_frame_glyph_matrix);
23290 defsubr (&Sdump_glyph_matrix);
23291 defsubr (&Sdump_glyph_row);
23292 defsubr (&Sdump_tool_bar_row);
23293 defsubr (&Strace_redisplay);
23294 defsubr (&Strace_to_stderr);
23295 #endif
23296 #ifdef HAVE_WINDOW_SYSTEM
23297 defsubr (&Stool_bar_lines_needed);
23298 defsubr (&Slookup_image_map);
23299 #endif
23300 defsubr (&Sformat_mode_line);
23301
23302 staticpro (&Qmenu_bar_update_hook);
23303 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23304
23305 staticpro (&Qoverriding_terminal_local_map);
23306 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23307
23308 staticpro (&Qoverriding_local_map);
23309 Qoverriding_local_map = intern ("overriding-local-map");
23310
23311 staticpro (&Qwindow_scroll_functions);
23312 Qwindow_scroll_functions = intern ("window-scroll-functions");
23313
23314 staticpro (&Qredisplay_end_trigger_functions);
23315 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23316
23317 staticpro (&Qinhibit_point_motion_hooks);
23318 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23319
23320 QCdata = intern (":data");
23321 staticpro (&QCdata);
23322 Qdisplay = intern ("display");
23323 staticpro (&Qdisplay);
23324 Qspace_width = intern ("space-width");
23325 staticpro (&Qspace_width);
23326 Qraise = intern ("raise");
23327 staticpro (&Qraise);
23328 Qslice = intern ("slice");
23329 staticpro (&Qslice);
23330 Qspace = intern ("space");
23331 staticpro (&Qspace);
23332 Qmargin = intern ("margin");
23333 staticpro (&Qmargin);
23334 Qpointer = intern ("pointer");
23335 staticpro (&Qpointer);
23336 Qleft_margin = intern ("left-margin");
23337 staticpro (&Qleft_margin);
23338 Qright_margin = intern ("right-margin");
23339 staticpro (&Qright_margin);
23340 Qcenter = intern ("center");
23341 staticpro (&Qcenter);
23342 Qline_height = intern ("line-height");
23343 staticpro (&Qline_height);
23344 QCalign_to = intern (":align-to");
23345 staticpro (&QCalign_to);
23346 QCrelative_width = intern (":relative-width");
23347 staticpro (&QCrelative_width);
23348 QCrelative_height = intern (":relative-height");
23349 staticpro (&QCrelative_height);
23350 QCeval = intern (":eval");
23351 staticpro (&QCeval);
23352 QCpropertize = intern (":propertize");
23353 staticpro (&QCpropertize);
23354 QCfile = intern (":file");
23355 staticpro (&QCfile);
23356 Qfontified = intern ("fontified");
23357 staticpro (&Qfontified);
23358 Qfontification_functions = intern ("fontification-functions");
23359 staticpro (&Qfontification_functions);
23360 Qtrailing_whitespace = intern ("trailing-whitespace");
23361 staticpro (&Qtrailing_whitespace);
23362 Qescape_glyph = intern ("escape-glyph");
23363 staticpro (&Qescape_glyph);
23364 Qnobreak_space = intern ("nobreak-space");
23365 staticpro (&Qnobreak_space);
23366 Qimage = intern ("image");
23367 staticpro (&Qimage);
23368 QCmap = intern (":map");
23369 staticpro (&QCmap);
23370 QCpointer = intern (":pointer");
23371 staticpro (&QCpointer);
23372 Qrect = intern ("rect");
23373 staticpro (&Qrect);
23374 Qcircle = intern ("circle");
23375 staticpro (&Qcircle);
23376 Qpoly = intern ("poly");
23377 staticpro (&Qpoly);
23378 Qmessage_truncate_lines = intern ("message-truncate-lines");
23379 staticpro (&Qmessage_truncate_lines);
23380 Qgrow_only = intern ("grow-only");
23381 staticpro (&Qgrow_only);
23382 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23383 staticpro (&Qinhibit_menubar_update);
23384 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23385 staticpro (&Qinhibit_eval_during_redisplay);
23386 Qposition = intern ("position");
23387 staticpro (&Qposition);
23388 Qbuffer_position = intern ("buffer-position");
23389 staticpro (&Qbuffer_position);
23390 Qobject = intern ("object");
23391 staticpro (&Qobject);
23392 Qbar = intern ("bar");
23393 staticpro (&Qbar);
23394 Qhbar = intern ("hbar");
23395 staticpro (&Qhbar);
23396 Qbox = intern ("box");
23397 staticpro (&Qbox);
23398 Qhollow = intern ("hollow");
23399 staticpro (&Qhollow);
23400 Qhand = intern ("hand");
23401 staticpro (&Qhand);
23402 Qarrow = intern ("arrow");
23403 staticpro (&Qarrow);
23404 Qtext = intern ("text");
23405 staticpro (&Qtext);
23406 Qrisky_local_variable = intern ("risky-local-variable");
23407 staticpro (&Qrisky_local_variable);
23408 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23409 staticpro (&Qinhibit_free_realized_faces);
23410
23411 list_of_error = Fcons (Fcons (intern ("error"),
23412 Fcons (intern ("void-variable"), Qnil)),
23413 Qnil);
23414 staticpro (&list_of_error);
23415
23416 Qlast_arrow_position = intern ("last-arrow-position");
23417 staticpro (&Qlast_arrow_position);
23418 Qlast_arrow_string = intern ("last-arrow-string");
23419 staticpro (&Qlast_arrow_string);
23420
23421 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23422 staticpro (&Qoverlay_arrow_string);
23423 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23424 staticpro (&Qoverlay_arrow_bitmap);
23425
23426 echo_buffer[0] = echo_buffer[1] = Qnil;
23427 staticpro (&echo_buffer[0]);
23428 staticpro (&echo_buffer[1]);
23429
23430 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23431 staticpro (&echo_area_buffer[0]);
23432 staticpro (&echo_area_buffer[1]);
23433
23434 Vmessages_buffer_name = build_string ("*Messages*");
23435 staticpro (&Vmessages_buffer_name);
23436
23437 mode_line_proptrans_alist = Qnil;
23438 staticpro (&mode_line_proptrans_alist);
23439 mode_line_string_list = Qnil;
23440 staticpro (&mode_line_string_list);
23441 mode_line_string_face = Qnil;
23442 staticpro (&mode_line_string_face);
23443 mode_line_string_face_prop = Qnil;
23444 staticpro (&mode_line_string_face_prop);
23445 Vmode_line_unwind_vector = Qnil;
23446 staticpro (&Vmode_line_unwind_vector);
23447
23448 help_echo_string = Qnil;
23449 staticpro (&help_echo_string);
23450 help_echo_object = Qnil;
23451 staticpro (&help_echo_object);
23452 help_echo_window = Qnil;
23453 staticpro (&help_echo_window);
23454 previous_help_echo_string = Qnil;
23455 staticpro (&previous_help_echo_string);
23456 help_echo_pos = -1;
23457
23458 #ifdef HAVE_WINDOW_SYSTEM
23459 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23460 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23461 For example, if a block cursor is over a tab, it will be drawn as
23462 wide as that tab on the display. */);
23463 x_stretch_cursor_p = 0;
23464 #endif
23465
23466 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23467 doc: /* *Non-nil means highlight trailing whitespace.
23468 The face used for trailing whitespace is `trailing-whitespace'. */);
23469 Vshow_trailing_whitespace = Qnil;
23470
23471 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23472 doc: /* *Control highlighting of nobreak space and soft hyphen.
23473 A value of t means highlight the character itself (for nobreak space,
23474 use face `nobreak-space').
23475 A value of nil means no highlighting.
23476 Other values mean display the escape glyph followed by an ordinary
23477 space or ordinary hyphen. */);
23478 Vnobreak_char_display = Qt;
23479
23480 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23481 doc: /* *The pointer shape to show in void text areas.
23482 A value of nil means to show the text pointer. Other options are `arrow',
23483 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23484 Vvoid_text_area_pointer = Qarrow;
23485
23486 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23487 doc: /* Non-nil means don't actually do any redisplay.
23488 This is used for internal purposes. */);
23489 Vinhibit_redisplay = Qnil;
23490
23491 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23492 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23493 Vglobal_mode_string = Qnil;
23494
23495 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23496 doc: /* Marker for where to display an arrow on top of the buffer text.
23497 This must be the beginning of a line in order to work.
23498 See also `overlay-arrow-string'. */);
23499 Voverlay_arrow_position = Qnil;
23500
23501 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23502 doc: /* String to display as an arrow in non-window frames.
23503 See also `overlay-arrow-position'. */);
23504 Voverlay_arrow_string = build_string ("=>");
23505
23506 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23507 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23508 The symbols on this list are examined during redisplay to determine
23509 where to display overlay arrows. */);
23510 Voverlay_arrow_variable_list
23511 = Fcons (intern ("overlay-arrow-position"), Qnil);
23512
23513 DEFVAR_INT ("scroll-step", &scroll_step,
23514 doc: /* *The number of lines to try scrolling a window by when point moves out.
23515 If that fails to bring point back on frame, point is centered instead.
23516 If this is zero, point is always centered after it moves off frame.
23517 If you want scrolling to always be a line at a time, you should set
23518 `scroll-conservatively' to a large value rather than set this to 1. */);
23519
23520 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23521 doc: /* *Scroll up to this many lines, to bring point back on screen.
23522 A value of zero means to scroll the text to center point vertically
23523 in the window. */);
23524 scroll_conservatively = 0;
23525
23526 DEFVAR_INT ("scroll-margin", &scroll_margin,
23527 doc: /* *Number of lines of margin at the top and bottom of a window.
23528 Recenter the window whenever point gets within this many lines
23529 of the top or bottom of the window. */);
23530 scroll_margin = 0;
23531
23532 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23533 doc: /* Pixels per inch value for non-window system displays.
23534 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23535 Vdisplay_pixels_per_inch = make_float (72.0);
23536
23537 #if GLYPH_DEBUG
23538 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23539 #endif
23540
23541 DEFVAR_BOOL ("truncate-partial-width-windows",
23542 &truncate_partial_width_windows,
23543 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23544 truncate_partial_width_windows = 1;
23545
23546 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23547 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23548 Any other value means to use the appropriate face, `mode-line',
23549 `header-line', or `menu' respectively. */);
23550 mode_line_inverse_video = 1;
23551
23552 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23553 doc: /* *Maximum buffer size for which line number should be displayed.
23554 If the buffer is bigger than this, the line number does not appear
23555 in the mode line. A value of nil means no limit. */);
23556 Vline_number_display_limit = Qnil;
23557
23558 DEFVAR_INT ("line-number-display-limit-width",
23559 &line_number_display_limit_width,
23560 doc: /* *Maximum line width (in characters) for line number display.
23561 If the average length of the lines near point is bigger than this, then the
23562 line number may be omitted from the mode line. */);
23563 line_number_display_limit_width = 200;
23564
23565 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23566 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23567 highlight_nonselected_windows = 0;
23568
23569 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23570 doc: /* Non-nil if more than one frame is visible on this display.
23571 Minibuffer-only frames don't count, but iconified frames do.
23572 This variable is not guaranteed to be accurate except while processing
23573 `frame-title-format' and `icon-title-format'. */);
23574
23575 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23576 doc: /* Template for displaying the title bar of visible frames.
23577 \(Assuming the window manager supports this feature.)
23578 This variable has the same structure as `mode-line-format' (which see),
23579 and is used only on frames for which no explicit name has been set
23580 \(see `modify-frame-parameters'). */);
23581
23582 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23583 doc: /* Template for displaying the title bar of an iconified frame.
23584 \(Assuming the window manager supports this feature.)
23585 This variable has the same structure as `mode-line-format' (which see),
23586 and is used only on frames for which no explicit name has been set
23587 \(see `modify-frame-parameters'). */);
23588 Vicon_title_format
23589 = Vframe_title_format
23590 = Fcons (intern ("multiple-frames"),
23591 Fcons (build_string ("%b"),
23592 Fcons (Fcons (empty_string,
23593 Fcons (intern ("invocation-name"),
23594 Fcons (build_string ("@"),
23595 Fcons (intern ("system-name"),
23596 Qnil)))),
23597 Qnil)));
23598
23599 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23600 doc: /* Maximum number of lines to keep in the message log buffer.
23601 If nil, disable message logging. If t, log messages but don't truncate
23602 the buffer when it becomes large. */);
23603 Vmessage_log_max = make_number (50);
23604
23605 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23606 doc: /* Functions called before redisplay, if window sizes have changed.
23607 The value should be a list of functions that take one argument.
23608 Just before redisplay, for each frame, if any of its windows have changed
23609 size since the last redisplay, or have been split or deleted,
23610 all the functions in the list are called, with the frame as argument. */);
23611 Vwindow_size_change_functions = Qnil;
23612
23613 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23614 doc: /* List of functions to call before redisplaying a window with scrolling.
23615 Each function is called with two arguments, the window
23616 and its new display-start position. Note that the value of `window-end'
23617 is not valid when these functions are called. */);
23618 Vwindow_scroll_functions = Qnil;
23619
23620 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23621 doc: /* Functions called when redisplay of a window reaches the end trigger.
23622 Each function is called with two arguments, the window and the end trigger value.
23623 See `set-window-redisplay-end-trigger'. */);
23624 Vredisplay_end_trigger_functions = Qnil;
23625
23626 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23627 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23628 mouse_autoselect_window = 0;
23629
23630 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23631 doc: /* *Non-nil means automatically resize tool-bars.
23632 This increases a tool-bar's height if not all tool-bar items are visible.
23633 It decreases a tool-bar's height when it would display blank lines
23634 otherwise. */);
23635 auto_resize_tool_bars_p = 1;
23636
23637 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23638 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23639 auto_raise_tool_bar_buttons_p = 1;
23640
23641 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23642 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23643 make_cursor_line_fully_visible_p = 1;
23644
23645 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23646 doc: /* *Margin around tool-bar buttons in pixels.
23647 If an integer, use that for both horizontal and vertical margins.
23648 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23649 HORZ specifying the horizontal margin, and VERT specifying the
23650 vertical margin. */);
23651 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23652
23653 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23654 doc: /* *Relief thickness of tool-bar buttons. */);
23655 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23656
23657 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23658 doc: /* List of functions to call to fontify regions of text.
23659 Each function is called with one argument POS. Functions must
23660 fontify a region starting at POS in the current buffer, and give
23661 fontified regions the property `fontified'. */);
23662 Vfontification_functions = Qnil;
23663 Fmake_variable_buffer_local (Qfontification_functions);
23664
23665 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23666 &unibyte_display_via_language_environment,
23667 doc: /* *Non-nil means display unibyte text according to language environment.
23668 Specifically this means that unibyte non-ASCII characters
23669 are displayed by converting them to the equivalent multibyte characters
23670 according to the current language environment. As a result, they are
23671 displayed according to the current fontset. */);
23672 unibyte_display_via_language_environment = 0;
23673
23674 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23675 doc: /* *Maximum height for resizing mini-windows.
23676 If a float, it specifies a fraction of the mini-window frame's height.
23677 If an integer, it specifies a number of lines. */);
23678 Vmax_mini_window_height = make_float (0.25);
23679
23680 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23681 doc: /* *How to resize mini-windows.
23682 A value of nil means don't automatically resize mini-windows.
23683 A value of t means resize them to fit the text displayed in them.
23684 A value of `grow-only', the default, means let mini-windows grow
23685 only, until their display becomes empty, at which point the windows
23686 go back to their normal size. */);
23687 Vresize_mini_windows = Qgrow_only;
23688
23689 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23690 doc: /* Alist specifying how to blink the cursor off.
23691 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23692 `cursor-type' frame-parameter or variable equals ON-STATE,
23693 comparing using `equal', Emacs uses OFF-STATE to specify
23694 how to blink it off. ON-STATE and OFF-STATE are values for
23695 the `cursor-type' frame parameter.
23696
23697 If a frame's ON-STATE has no entry in this list,
23698 the frame's other specifications determine how to blink the cursor off. */);
23699 Vblink_cursor_alist = Qnil;
23700
23701 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23702 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23703 automatic_hscrolling_p = 1;
23704
23705 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23706 doc: /* *How many columns away from the window edge point is allowed to get
23707 before automatic hscrolling will horizontally scroll the window. */);
23708 hscroll_margin = 5;
23709
23710 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23711 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23712 When point is less than `automatic-hscroll-margin' columns from the window
23713 edge, automatic hscrolling will scroll the window by the amount of columns
23714 determined by this variable. If its value is a positive integer, scroll that
23715 many columns. If it's a positive floating-point number, it specifies the
23716 fraction of the window's width to scroll. If it's nil or zero, point will be
23717 centered horizontally after the scroll. Any other value, including negative
23718 numbers, are treated as if the value were zero.
23719
23720 Automatic hscrolling always moves point outside the scroll margin, so if
23721 point was more than scroll step columns inside the margin, the window will
23722 scroll more than the value given by the scroll step.
23723
23724 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23725 and `scroll-right' overrides this variable's effect. */);
23726 Vhscroll_step = make_number (0);
23727
23728 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23729 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23730 Bind this around calls to `message' to let it take effect. */);
23731 message_truncate_lines = 0;
23732
23733 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23734 doc: /* Normal hook run to update the menu bar definitions.
23735 Redisplay runs this hook before it redisplays the menu bar.
23736 This is used to update submenus such as Buffers,
23737 whose contents depend on various data. */);
23738 Vmenu_bar_update_hook = Qnil;
23739
23740 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23741 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23742 inhibit_menubar_update = 0;
23743
23744 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23745 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23746 inhibit_eval_during_redisplay = 0;
23747
23748 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23749 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23750 inhibit_free_realized_faces = 0;
23751
23752 #if GLYPH_DEBUG
23753 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23754 doc: /* Inhibit try_window_id display optimization. */);
23755 inhibit_try_window_id = 0;
23756
23757 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23758 doc: /* Inhibit try_window_reusing display optimization. */);
23759 inhibit_try_window_reusing = 0;
23760
23761 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23762 doc: /* Inhibit try_cursor_movement display optimization. */);
23763 inhibit_try_cursor_movement = 0;
23764 #endif /* GLYPH_DEBUG */
23765 }
23766
23767
23768 /* Initialize this module when Emacs starts. */
23769
23770 void
23771 init_xdisp ()
23772 {
23773 Lisp_Object root_window;
23774 struct window *mini_w;
23775
23776 current_header_line_height = current_mode_line_height = -1;
23777
23778 CHARPOS (this_line_start_pos) = 0;
23779
23780 mini_w = XWINDOW (minibuf_window);
23781 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23782
23783 if (!noninteractive)
23784 {
23785 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23786 int i;
23787
23788 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23789 set_window_height (root_window,
23790 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23791 0);
23792 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23793 set_window_height (minibuf_window, 1, 0);
23794
23795 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23796 mini_w->total_cols = make_number (FRAME_COLS (f));
23797
23798 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23799 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23800 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23801
23802 /* The default ellipsis glyphs `...'. */
23803 for (i = 0; i < 3; ++i)
23804 default_invis_vector[i] = make_number ('.');
23805 }
23806
23807 {
23808 /* Allocate the buffer for frame titles.
23809 Also used for `format-mode-line'. */
23810 int size = 100;
23811 mode_line_noprop_buf = (char *) xmalloc (size);
23812 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23813 mode_line_noprop_ptr = mode_line_noprop_buf;
23814 mode_line_target = MODE_LINE_DISPLAY;
23815 }
23816
23817 help_echo_showing_p = 0;
23818 }
23819
23820
23821 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23822 (do not change this comment) */