]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(image_cache_refcount, dpyinfo_refcount) [GLYPH_DEBUG]: New variables.
[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 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);
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 *R the clipping rectangle for glyph string S. */
1772
1773 void
1774 get_glyph_string_clip_rect (s, nr)
1775 struct glyph_string *s;
1776 NativeRectangle *nr;
1777 {
1778 XRectangle r;
1779
1780 if (s->row->full_width_p)
1781 {
1782 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1783 r.x = WINDOW_LEFT_EDGE_X (s->w);
1784 r.width = WINDOW_TOTAL_WIDTH (s->w);
1785
1786 /* Unless displaying a mode or menu bar line, which are always
1787 fully visible, clip to the visible part of the row. */
1788 if (s->w->pseudo_window_p)
1789 r.height = s->row->visible_height;
1790 else
1791 r.height = s->height;
1792 }
1793 else
1794 {
1795 /* This is a text line that may be partially visible. */
1796 r.x = window_box_left (s->w, s->area);
1797 r.width = window_box_width (s->w, s->area);
1798 r.height = s->row->visible_height;
1799 }
1800
1801 if (s->clip_head)
1802 if (r.x < s->clip_head->x)
1803 {
1804 if (r.width >= s->clip_head->x - r.x)
1805 r.width -= s->clip_head->x - r.x;
1806 else
1807 r.width = 0;
1808 r.x = s->clip_head->x;
1809 }
1810 if (s->clip_tail)
1811 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1812 {
1813 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1814 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1815 else
1816 r.width = 0;
1817 }
1818
1819 /* If S draws overlapping rows, it's sufficient to use the top and
1820 bottom of the window for clipping because this glyph string
1821 intentionally draws over other lines. */
1822 if (s->for_overlaps_p)
1823 {
1824 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1825 r.height = window_text_bottom_y (s->w) - r.y;
1826 }
1827 else
1828 {
1829 /* Don't use S->y for clipping because it doesn't take partially
1830 visible lines into account. For example, it can be negative for
1831 partially visible lines at the top of a window. */
1832 if (!s->row->full_width_p
1833 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1834 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1835 else
1836 r.y = max (0, s->row->y);
1837
1838 /* If drawing a tool-bar window, draw it over the internal border
1839 at the top of the window. */
1840 if (WINDOWP (s->f->tool_bar_window)
1841 && s->w == XWINDOW (s->f->tool_bar_window))
1842 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1843 }
1844
1845 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1846
1847 /* If drawing the cursor, don't let glyph draw outside its
1848 advertised boundaries. Cleartype does this under some circumstances. */
1849 if (s->hl == DRAW_CURSOR)
1850 {
1851 struct glyph *glyph = s->first_glyph;
1852 int height, max_y;
1853
1854 if (s->x > r.x)
1855 {
1856 r.width -= s->x - r.x;
1857 r.x = s->x;
1858 }
1859 r.width = min (r.width, glyph->pixel_width);
1860
1861 /* If r.y is below window bottom, ensure that we still see a cursor. */
1862 height = min (glyph->ascent + glyph->descent,
1863 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1864 max_y = window_text_bottom_y (s->w) - height;
1865 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1866 if (s->ybase - glyph->ascent > max_y)
1867 {
1868 r.y = max_y;
1869 r.height = height;
1870 }
1871 else
1872 {
1873 /* Don't draw cursor glyph taller than our actual glyph. */
1874 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1875 if (height < r.height)
1876 {
1877 max_y = r.y + r.height;
1878 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1879 r.height = min (max_y - r.y, height);
1880 }
1881 }
1882 }
1883
1884 #ifdef CONVERT_FROM_XRECT
1885 CONVERT_FROM_XRECT (r, *nr);
1886 #else
1887 *nr = r;
1888 #endif
1889 }
1890
1891
1892 /* EXPORT:
1893 Return the position and height of the phys cursor in window W.
1894 Set w->phys_cursor_width to width of phys cursor.
1895 */
1896
1897 int
1898 get_phys_cursor_geometry (w, row, glyph, heightp)
1899 struct window *w;
1900 struct glyph_row *row;
1901 struct glyph *glyph;
1902 int *heightp;
1903 {
1904 struct frame *f = XFRAME (WINDOW_FRAME (w));
1905 int y, wd, h, h0, y0;
1906
1907 /* Compute the width of the rectangle to draw. If on a stretch
1908 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1909 rectangle as wide as the glyph, but use a canonical character
1910 width instead. */
1911 wd = glyph->pixel_width - 1;
1912 #ifdef HAVE_NTGUI
1913 wd++; /* Why? */
1914 #endif
1915 if (glyph->type == STRETCH_GLYPH
1916 && !x_stretch_cursor_p)
1917 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1918 w->phys_cursor_width = wd;
1919
1920 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1921
1922 /* If y is below window bottom, ensure that we still see a cursor. */
1923 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1924
1925 h = max (h0, glyph->ascent + glyph->descent);
1926 h0 = min (h0, glyph->ascent + glyph->descent);
1927
1928 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1929 if (y < y0)
1930 {
1931 h = max (h - (y0 - y) + 1, h0);
1932 y = y0 - 1;
1933 }
1934 else
1935 {
1936 y0 = window_text_bottom_y (w) - h0;
1937 if (y > y0)
1938 {
1939 h += y - y0;
1940 y = y0;
1941 }
1942 }
1943
1944 *heightp = h - 1;
1945 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1946 }
1947
1948
1949 #endif /* HAVE_WINDOW_SYSTEM */
1950
1951 \f
1952 /***********************************************************************
1953 Lisp form evaluation
1954 ***********************************************************************/
1955
1956 /* Error handler for safe_eval and safe_call. */
1957
1958 static Lisp_Object
1959 safe_eval_handler (arg)
1960 Lisp_Object arg;
1961 {
1962 add_to_log ("Error during redisplay: %s", arg, Qnil);
1963 return Qnil;
1964 }
1965
1966
1967 /* Evaluate SEXPR and return the result, or nil if something went
1968 wrong. Prevent redisplay during the evaluation. */
1969
1970 Lisp_Object
1971 safe_eval (sexpr)
1972 Lisp_Object sexpr;
1973 {
1974 Lisp_Object val;
1975
1976 if (inhibit_eval_during_redisplay)
1977 val = Qnil;
1978 else
1979 {
1980 int count = SPECPDL_INDEX ();
1981 struct gcpro gcpro1;
1982
1983 GCPRO1 (sexpr);
1984 specbind (Qinhibit_redisplay, Qt);
1985 /* Use Qt to ensure debugger does not run,
1986 so there is no possibility of wanting to redisplay. */
1987 val = internal_condition_case_1 (Feval, sexpr, Qt,
1988 safe_eval_handler);
1989 UNGCPRO;
1990 val = unbind_to (count, val);
1991 }
1992
1993 return val;
1994 }
1995
1996
1997 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1998 Return the result, or nil if something went wrong. Prevent
1999 redisplay during the evaluation. */
2000
2001 Lisp_Object
2002 safe_call (nargs, args)
2003 int nargs;
2004 Lisp_Object *args;
2005 {
2006 Lisp_Object val;
2007
2008 if (inhibit_eval_during_redisplay)
2009 val = Qnil;
2010 else
2011 {
2012 int count = SPECPDL_INDEX ();
2013 struct gcpro gcpro1;
2014
2015 GCPRO1 (args[0]);
2016 gcpro1.nvars = nargs;
2017 specbind (Qinhibit_redisplay, Qt);
2018 /* Use Qt to ensure debugger does not run,
2019 so there is no possibility of wanting to redisplay. */
2020 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2021 safe_eval_handler);
2022 UNGCPRO;
2023 val = unbind_to (count, val);
2024 }
2025
2026 return val;
2027 }
2028
2029
2030 /* Call function FN with one argument ARG.
2031 Return the result, or nil if something went wrong. */
2032
2033 Lisp_Object
2034 safe_call1 (fn, arg)
2035 Lisp_Object fn, arg;
2036 {
2037 Lisp_Object args[2];
2038 args[0] = fn;
2039 args[1] = arg;
2040 return safe_call (2, args);
2041 }
2042
2043
2044 \f
2045 /***********************************************************************
2046 Debugging
2047 ***********************************************************************/
2048
2049 #if 0
2050
2051 /* Define CHECK_IT to perform sanity checks on iterators.
2052 This is for debugging. It is too slow to do unconditionally. */
2053
2054 static void
2055 check_it (it)
2056 struct it *it;
2057 {
2058 if (it->method == GET_FROM_STRING)
2059 {
2060 xassert (STRINGP (it->string));
2061 xassert (IT_STRING_CHARPOS (*it) >= 0);
2062 }
2063 else
2064 {
2065 xassert (IT_STRING_CHARPOS (*it) < 0);
2066 if (it->method == GET_FROM_BUFFER)
2067 {
2068 /* Check that character and byte positions agree. */
2069 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2070 }
2071 }
2072
2073 if (it->dpvec)
2074 xassert (it->current.dpvec_index >= 0);
2075 else
2076 xassert (it->current.dpvec_index < 0);
2077 }
2078
2079 #define CHECK_IT(IT) check_it ((IT))
2080
2081 #else /* not 0 */
2082
2083 #define CHECK_IT(IT) (void) 0
2084
2085 #endif /* not 0 */
2086
2087
2088 #if GLYPH_DEBUG
2089
2090 /* Check that the window end of window W is what we expect it
2091 to be---the last row in the current matrix displaying text. */
2092
2093 static void
2094 check_window_end (w)
2095 struct window *w;
2096 {
2097 if (!MINI_WINDOW_P (w)
2098 && !NILP (w->window_end_valid))
2099 {
2100 struct glyph_row *row;
2101 xassert ((row = MATRIX_ROW (w->current_matrix,
2102 XFASTINT (w->window_end_vpos)),
2103 !row->enabled_p
2104 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2105 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2106 }
2107 }
2108
2109 #define CHECK_WINDOW_END(W) check_window_end ((W))
2110
2111 #else /* not GLYPH_DEBUG */
2112
2113 #define CHECK_WINDOW_END(W) (void) 0
2114
2115 #endif /* not GLYPH_DEBUG */
2116
2117
2118 \f
2119 /***********************************************************************
2120 Iterator initialization
2121 ***********************************************************************/
2122
2123 /* Initialize IT for displaying current_buffer in window W, starting
2124 at character position CHARPOS. CHARPOS < 0 means that no buffer
2125 position is specified which is useful when the iterator is assigned
2126 a position later. BYTEPOS is the byte position corresponding to
2127 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2128
2129 If ROW is not null, calls to produce_glyphs with IT as parameter
2130 will produce glyphs in that row.
2131
2132 BASE_FACE_ID is the id of a base face to use. It must be one of
2133 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2134 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2135 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2136
2137 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2138 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2139 will be initialized to use the corresponding mode line glyph row of
2140 the desired matrix of W. */
2141
2142 void
2143 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2144 struct it *it;
2145 struct window *w;
2146 int charpos, bytepos;
2147 struct glyph_row *row;
2148 enum face_id base_face_id;
2149 {
2150 int highlight_region_p;
2151
2152 /* Some precondition checks. */
2153 xassert (w != NULL && it != NULL);
2154 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2155 && charpos <= ZV));
2156
2157 /* If face attributes have been changed since the last redisplay,
2158 free realized faces now because they depend on face definitions
2159 that might have changed. Don't free faces while there might be
2160 desired matrices pending which reference these faces. */
2161 if (face_change_count && !inhibit_free_realized_faces)
2162 {
2163 face_change_count = 0;
2164 free_all_realized_faces (Qnil);
2165 }
2166
2167 /* Use one of the mode line rows of W's desired matrix if
2168 appropriate. */
2169 if (row == NULL)
2170 {
2171 if (base_face_id == MODE_LINE_FACE_ID
2172 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2173 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2174 else if (base_face_id == HEADER_LINE_FACE_ID)
2175 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2176 }
2177
2178 /* Clear IT. */
2179 bzero (it, sizeof *it);
2180 it->current.overlay_string_index = -1;
2181 it->current.dpvec_index = -1;
2182 it->base_face_id = base_face_id;
2183 it->string = Qnil;
2184 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2185
2186 /* The window in which we iterate over current_buffer: */
2187 XSETWINDOW (it->window, w);
2188 it->w = w;
2189 it->f = XFRAME (w->frame);
2190
2191 /* Extra space between lines (on window systems only). */
2192 if (base_face_id == DEFAULT_FACE_ID
2193 && FRAME_WINDOW_P (it->f))
2194 {
2195 if (NATNUMP (current_buffer->extra_line_spacing))
2196 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2197 else if (FLOATP (current_buffer->extra_line_spacing))
2198 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2199 * FRAME_LINE_HEIGHT (it->f));
2200 else if (it->f->extra_line_spacing > 0)
2201 it->extra_line_spacing = it->f->extra_line_spacing;
2202 it->max_extra_line_spacing = 0;
2203 }
2204
2205 /* If realized faces have been removed, e.g. because of face
2206 attribute changes of named faces, recompute them. When running
2207 in batch mode, the face cache of Vterminal_frame is null. If
2208 we happen to get called, make a dummy face cache. */
2209 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2210 init_frame_faces (it->f);
2211 if (FRAME_FACE_CACHE (it->f)->used == 0)
2212 recompute_basic_faces (it->f);
2213
2214 /* Current value of the `slice', `space-width', and 'height' properties. */
2215 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2216 it->space_width = Qnil;
2217 it->font_height = Qnil;
2218 it->override_ascent = -1;
2219
2220 /* Are control characters displayed as `^C'? */
2221 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2222
2223 /* -1 means everything between a CR and the following line end
2224 is invisible. >0 means lines indented more than this value are
2225 invisible. */
2226 it->selective = (INTEGERP (current_buffer->selective_display)
2227 ? XFASTINT (current_buffer->selective_display)
2228 : (!NILP (current_buffer->selective_display)
2229 ? -1 : 0));
2230 it->selective_display_ellipsis_p
2231 = !NILP (current_buffer->selective_display_ellipses);
2232
2233 /* Display table to use. */
2234 it->dp = window_display_table (w);
2235
2236 /* Are multibyte characters enabled in current_buffer? */
2237 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2238
2239 /* Non-zero if we should highlight the region. */
2240 highlight_region_p
2241 = (!NILP (Vtransient_mark_mode)
2242 && !NILP (current_buffer->mark_active)
2243 && XMARKER (current_buffer->mark)->buffer != 0);
2244
2245 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2246 start and end of a visible region in window IT->w. Set both to
2247 -1 to indicate no region. */
2248 if (highlight_region_p
2249 /* Maybe highlight only in selected window. */
2250 && (/* Either show region everywhere. */
2251 highlight_nonselected_windows
2252 /* Or show region in the selected window. */
2253 || w == XWINDOW (selected_window)
2254 /* Or show the region if we are in the mini-buffer and W is
2255 the window the mini-buffer refers to. */
2256 || (MINI_WINDOW_P (XWINDOW (selected_window))
2257 && WINDOWP (minibuf_selected_window)
2258 && w == XWINDOW (minibuf_selected_window))))
2259 {
2260 int charpos = marker_position (current_buffer->mark);
2261 it->region_beg_charpos = min (PT, charpos);
2262 it->region_end_charpos = max (PT, charpos);
2263 }
2264 else
2265 it->region_beg_charpos = it->region_end_charpos = -1;
2266
2267 /* Get the position at which the redisplay_end_trigger hook should
2268 be run, if it is to be run at all. */
2269 if (MARKERP (w->redisplay_end_trigger)
2270 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2271 it->redisplay_end_trigger_charpos
2272 = marker_position (w->redisplay_end_trigger);
2273 else if (INTEGERP (w->redisplay_end_trigger))
2274 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2275
2276 /* Correct bogus values of tab_width. */
2277 it->tab_width = XINT (current_buffer->tab_width);
2278 if (it->tab_width <= 0 || it->tab_width > 1000)
2279 it->tab_width = 8;
2280
2281 /* Are lines in the display truncated? */
2282 it->truncate_lines_p
2283 = (base_face_id != DEFAULT_FACE_ID
2284 || XINT (it->w->hscroll)
2285 || (truncate_partial_width_windows
2286 && !WINDOW_FULL_WIDTH_P (it->w))
2287 || !NILP (current_buffer->truncate_lines));
2288
2289 /* Get dimensions of truncation and continuation glyphs. These are
2290 displayed as fringe bitmaps under X, so we don't need them for such
2291 frames. */
2292 if (!FRAME_WINDOW_P (it->f))
2293 {
2294 if (it->truncate_lines_p)
2295 {
2296 /* We will need the truncation glyph. */
2297 xassert (it->glyph_row == NULL);
2298 produce_special_glyphs (it, IT_TRUNCATION);
2299 it->truncation_pixel_width = it->pixel_width;
2300 }
2301 else
2302 {
2303 /* We will need the continuation glyph. */
2304 xassert (it->glyph_row == NULL);
2305 produce_special_glyphs (it, IT_CONTINUATION);
2306 it->continuation_pixel_width = it->pixel_width;
2307 }
2308
2309 /* Reset these values to zero because the produce_special_glyphs
2310 above has changed them. */
2311 it->pixel_width = it->ascent = it->descent = 0;
2312 it->phys_ascent = it->phys_descent = 0;
2313 }
2314
2315 /* Set this after getting the dimensions of truncation and
2316 continuation glyphs, so that we don't produce glyphs when calling
2317 produce_special_glyphs, above. */
2318 it->glyph_row = row;
2319 it->area = TEXT_AREA;
2320
2321 /* Get the dimensions of the display area. The display area
2322 consists of the visible window area plus a horizontally scrolled
2323 part to the left of the window. All x-values are relative to the
2324 start of this total display area. */
2325 if (base_face_id != DEFAULT_FACE_ID)
2326 {
2327 /* Mode lines, menu bar in terminal frames. */
2328 it->first_visible_x = 0;
2329 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2330 }
2331 else
2332 {
2333 it->first_visible_x
2334 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2335 it->last_visible_x = (it->first_visible_x
2336 + window_box_width (w, TEXT_AREA));
2337
2338 /* If we truncate lines, leave room for the truncator glyph(s) at
2339 the right margin. Otherwise, leave room for the continuation
2340 glyph(s). Truncation and continuation glyphs are not inserted
2341 for window-based redisplay. */
2342 if (!FRAME_WINDOW_P (it->f))
2343 {
2344 if (it->truncate_lines_p)
2345 it->last_visible_x -= it->truncation_pixel_width;
2346 else
2347 it->last_visible_x -= it->continuation_pixel_width;
2348 }
2349
2350 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2351 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2352 }
2353
2354 /* Leave room for a border glyph. */
2355 if (!FRAME_WINDOW_P (it->f)
2356 && !WINDOW_RIGHTMOST_P (it->w))
2357 it->last_visible_x -= 1;
2358
2359 it->last_visible_y = window_text_bottom_y (w);
2360
2361 /* For mode lines and alike, arrange for the first glyph having a
2362 left box line if the face specifies a box. */
2363 if (base_face_id != DEFAULT_FACE_ID)
2364 {
2365 struct face *face;
2366
2367 it->face_id = base_face_id;
2368
2369 /* If we have a boxed mode line, make the first character appear
2370 with a left box line. */
2371 face = FACE_FROM_ID (it->f, base_face_id);
2372 if (face->box != FACE_NO_BOX)
2373 it->start_of_box_run_p = 1;
2374 }
2375
2376 /* If a buffer position was specified, set the iterator there,
2377 getting overlays and face properties from that position. */
2378 if (charpos >= BUF_BEG (current_buffer))
2379 {
2380 it->end_charpos = ZV;
2381 it->face_id = -1;
2382 IT_CHARPOS (*it) = charpos;
2383
2384 /* Compute byte position if not specified. */
2385 if (bytepos < charpos)
2386 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2387 else
2388 IT_BYTEPOS (*it) = bytepos;
2389
2390 it->start = it->current;
2391
2392 /* Compute faces etc. */
2393 reseat (it, it->current.pos, 1);
2394 }
2395
2396 CHECK_IT (it);
2397 }
2398
2399
2400 /* Initialize IT for the display of window W with window start POS. */
2401
2402 void
2403 start_display (it, w, pos)
2404 struct it *it;
2405 struct window *w;
2406 struct text_pos pos;
2407 {
2408 struct glyph_row *row;
2409 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2410
2411 row = w->desired_matrix->rows + first_vpos;
2412 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2413 it->first_vpos = first_vpos;
2414
2415 /* Don't reseat to previous visible line start if current start
2416 position is in a string or image. */
2417 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2418 {
2419 int start_at_line_beg_p;
2420 int first_y = it->current_y;
2421
2422 /* If window start is not at a line start, skip forward to POS to
2423 get the correct continuation lines width. */
2424 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2425 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2426 if (!start_at_line_beg_p)
2427 {
2428 int new_x;
2429
2430 reseat_at_previous_visible_line_start (it);
2431 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2432
2433 new_x = it->current_x + it->pixel_width;
2434
2435 /* If lines are continued, this line may end in the middle
2436 of a multi-glyph character (e.g. a control character
2437 displayed as \003, or in the middle of an overlay
2438 string). In this case move_it_to above will not have
2439 taken us to the start of the continuation line but to the
2440 end of the continued line. */
2441 if (it->current_x > 0
2442 && !it->truncate_lines_p /* Lines are continued. */
2443 && (/* And glyph doesn't fit on the line. */
2444 new_x > it->last_visible_x
2445 /* Or it fits exactly and we're on a window
2446 system frame. */
2447 || (new_x == it->last_visible_x
2448 && FRAME_WINDOW_P (it->f))))
2449 {
2450 if (it->current.dpvec_index >= 0
2451 || it->current.overlay_string_index >= 0)
2452 {
2453 set_iterator_to_next (it, 1);
2454 move_it_in_display_line_to (it, -1, -1, 0);
2455 }
2456
2457 it->continuation_lines_width += it->current_x;
2458 }
2459
2460 /* We're starting a new display line, not affected by the
2461 height of the continued line, so clear the appropriate
2462 fields in the iterator structure. */
2463 it->max_ascent = it->max_descent = 0;
2464 it->max_phys_ascent = it->max_phys_descent = 0;
2465
2466 it->current_y = first_y;
2467 it->vpos = 0;
2468 it->current_x = it->hpos = 0;
2469 }
2470 }
2471
2472 #if 0 /* Don't assert the following because start_display is sometimes
2473 called intentionally with a window start that is not at a
2474 line start. Please leave this code in as a comment. */
2475
2476 /* Window start should be on a line start, now. */
2477 xassert (it->continuation_lines_width
2478 || IT_CHARPOS (it) == BEGV
2479 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2480 #endif /* 0 */
2481 }
2482
2483
2484 /* Return 1 if POS is a position in ellipses displayed for invisible
2485 text. W is the window we display, for text property lookup. */
2486
2487 static int
2488 in_ellipses_for_invisible_text_p (pos, w)
2489 struct display_pos *pos;
2490 struct window *w;
2491 {
2492 Lisp_Object prop, window;
2493 int ellipses_p = 0;
2494 int charpos = CHARPOS (pos->pos);
2495
2496 /* If POS specifies a position in a display vector, this might
2497 be for an ellipsis displayed for invisible text. We won't
2498 get the iterator set up for delivering that ellipsis unless
2499 we make sure that it gets aware of the invisible text. */
2500 if (pos->dpvec_index >= 0
2501 && pos->overlay_string_index < 0
2502 && CHARPOS (pos->string_pos) < 0
2503 && charpos > BEGV
2504 && (XSETWINDOW (window, w),
2505 prop = Fget_char_property (make_number (charpos),
2506 Qinvisible, window),
2507 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2508 {
2509 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2510 window);
2511 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2512 }
2513
2514 return ellipses_p;
2515 }
2516
2517
2518 /* Initialize IT for stepping through current_buffer in window W,
2519 starting at position POS that includes overlay string and display
2520 vector/ control character translation position information. Value
2521 is zero if there are overlay strings with newlines at POS. */
2522
2523 static int
2524 init_from_display_pos (it, w, pos)
2525 struct it *it;
2526 struct window *w;
2527 struct display_pos *pos;
2528 {
2529 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2530 int i, overlay_strings_with_newlines = 0;
2531
2532 /* If POS specifies a position in a display vector, this might
2533 be for an ellipsis displayed for invisible text. We won't
2534 get the iterator set up for delivering that ellipsis unless
2535 we make sure that it gets aware of the invisible text. */
2536 if (in_ellipses_for_invisible_text_p (pos, w))
2537 {
2538 --charpos;
2539 bytepos = 0;
2540 }
2541
2542 /* Keep in mind: the call to reseat in init_iterator skips invisible
2543 text, so we might end up at a position different from POS. This
2544 is only a problem when POS is a row start after a newline and an
2545 overlay starts there with an after-string, and the overlay has an
2546 invisible property. Since we don't skip invisible text in
2547 display_line and elsewhere immediately after consuming the
2548 newline before the row start, such a POS will not be in a string,
2549 but the call to init_iterator below will move us to the
2550 after-string. */
2551 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2552
2553 /* This only scans the current chunk -- it should scan all chunks.
2554 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2555 to 16 in 22.1 to make this a lesser problem. */
2556 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2557 {
2558 const char *s = SDATA (it->overlay_strings[i]);
2559 const char *e = s + SBYTES (it->overlay_strings[i]);
2560
2561 while (s < e && *s != '\n')
2562 ++s;
2563
2564 if (s < e)
2565 {
2566 overlay_strings_with_newlines = 1;
2567 break;
2568 }
2569 }
2570
2571 /* If position is within an overlay string, set up IT to the right
2572 overlay string. */
2573 if (pos->overlay_string_index >= 0)
2574 {
2575 int relative_index;
2576
2577 /* If the first overlay string happens to have a `display'
2578 property for an image, the iterator will be set up for that
2579 image, and we have to undo that setup first before we can
2580 correct the overlay string index. */
2581 if (it->method == GET_FROM_IMAGE)
2582 pop_it (it);
2583
2584 /* We already have the first chunk of overlay strings in
2585 IT->overlay_strings. Load more until the one for
2586 pos->overlay_string_index is in IT->overlay_strings. */
2587 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2588 {
2589 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2590 it->current.overlay_string_index = 0;
2591 while (n--)
2592 {
2593 load_overlay_strings (it, 0);
2594 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2595 }
2596 }
2597
2598 it->current.overlay_string_index = pos->overlay_string_index;
2599 relative_index = (it->current.overlay_string_index
2600 % OVERLAY_STRING_CHUNK_SIZE);
2601 it->string = it->overlay_strings[relative_index];
2602 xassert (STRINGP (it->string));
2603 it->current.string_pos = pos->string_pos;
2604 it->method = GET_FROM_STRING;
2605 }
2606
2607 #if 0 /* This is bogus because POS not having an overlay string
2608 position does not mean it's after the string. Example: A
2609 line starting with a before-string and initialization of IT
2610 to the previous row's end position. */
2611 else if (it->current.overlay_string_index >= 0)
2612 {
2613 /* If POS says we're already after an overlay string ending at
2614 POS, make sure to pop the iterator because it will be in
2615 front of that overlay string. When POS is ZV, we've thereby
2616 also ``processed'' overlay strings at ZV. */
2617 while (it->sp)
2618 pop_it (it);
2619 it->current.overlay_string_index = -1;
2620 it->method = GET_FROM_BUFFER;
2621 if (CHARPOS (pos->pos) == ZV)
2622 it->overlay_strings_at_end_processed_p = 1;
2623 }
2624 #endif /* 0 */
2625
2626 if (CHARPOS (pos->string_pos) >= 0)
2627 {
2628 /* Recorded position is not in an overlay string, but in another
2629 string. This can only be a string from a `display' property.
2630 IT should already be filled with that string. */
2631 it->current.string_pos = pos->string_pos;
2632 xassert (STRINGP (it->string));
2633 }
2634
2635 /* Restore position in display vector translations, control
2636 character translations or ellipses. */
2637 if (pos->dpvec_index >= 0)
2638 {
2639 if (it->dpvec == NULL)
2640 get_next_display_element (it);
2641 xassert (it->dpvec && it->current.dpvec_index == 0);
2642 it->current.dpvec_index = pos->dpvec_index;
2643 }
2644
2645 CHECK_IT (it);
2646 return !overlay_strings_with_newlines;
2647 }
2648
2649
2650 /* Initialize IT for stepping through current_buffer in window W
2651 starting at ROW->start. */
2652
2653 static void
2654 init_to_row_start (it, w, row)
2655 struct it *it;
2656 struct window *w;
2657 struct glyph_row *row;
2658 {
2659 init_from_display_pos (it, w, &row->start);
2660 it->start = row->start;
2661 it->continuation_lines_width = row->continuation_lines_width;
2662 CHECK_IT (it);
2663 }
2664
2665
2666 /* Initialize IT for stepping through current_buffer in window W
2667 starting in the line following ROW, i.e. starting at ROW->end.
2668 Value is zero if there are overlay strings with newlines at ROW's
2669 end position. */
2670
2671 static int
2672 init_to_row_end (it, w, row)
2673 struct it *it;
2674 struct window *w;
2675 struct glyph_row *row;
2676 {
2677 int success = 0;
2678
2679 if (init_from_display_pos (it, w, &row->end))
2680 {
2681 if (row->continued_p)
2682 it->continuation_lines_width
2683 = row->continuation_lines_width + row->pixel_width;
2684 CHECK_IT (it);
2685 success = 1;
2686 }
2687
2688 return success;
2689 }
2690
2691
2692
2693 \f
2694 /***********************************************************************
2695 Text properties
2696 ***********************************************************************/
2697
2698 /* Called when IT reaches IT->stop_charpos. Handle text property and
2699 overlay changes. Set IT->stop_charpos to the next position where
2700 to stop. */
2701
2702 static void
2703 handle_stop (it)
2704 struct it *it;
2705 {
2706 enum prop_handled handled;
2707 int handle_overlay_change_p = 1;
2708 struct props *p;
2709
2710 it->dpvec = NULL;
2711 it->current.dpvec_index = -1;
2712
2713 /* Use face of preceding text for ellipsis (if invisible) */
2714 if (it->selective_display_ellipsis_p)
2715 it->saved_face_id = it->face_id;
2716
2717 do
2718 {
2719 handled = HANDLED_NORMALLY;
2720
2721 /* Call text property handlers. */
2722 for (p = it_props; p->handler; ++p)
2723 {
2724 handled = p->handler (it);
2725
2726 if (handled == HANDLED_RECOMPUTE_PROPS)
2727 break;
2728 else if (handled == HANDLED_RETURN)
2729 return;
2730 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2731 handle_overlay_change_p = 0;
2732 }
2733
2734 if (handled != HANDLED_RECOMPUTE_PROPS)
2735 {
2736 /* Don't check for overlay strings below when set to deliver
2737 characters from a display vector. */
2738 if (it->method == GET_FROM_DISPLAY_VECTOR)
2739 handle_overlay_change_p = 0;
2740
2741 /* Handle overlay changes. */
2742 if (handle_overlay_change_p)
2743 handled = handle_overlay_change (it);
2744
2745 /* Determine where to stop next. */
2746 if (handled == HANDLED_NORMALLY)
2747 compute_stop_pos (it);
2748 }
2749 }
2750 while (handled == HANDLED_RECOMPUTE_PROPS);
2751 }
2752
2753
2754 /* Compute IT->stop_charpos from text property and overlay change
2755 information for IT's current position. */
2756
2757 static void
2758 compute_stop_pos (it)
2759 struct it *it;
2760 {
2761 register INTERVAL iv, next_iv;
2762 Lisp_Object object, limit, position;
2763
2764 /* If nowhere else, stop at the end. */
2765 it->stop_charpos = it->end_charpos;
2766
2767 if (STRINGP (it->string))
2768 {
2769 /* Strings are usually short, so don't limit the search for
2770 properties. */
2771 object = it->string;
2772 limit = Qnil;
2773 position = make_number (IT_STRING_CHARPOS (*it));
2774 }
2775 else
2776 {
2777 int charpos;
2778
2779 /* If next overlay change is in front of the current stop pos
2780 (which is IT->end_charpos), stop there. Note: value of
2781 next_overlay_change is point-max if no overlay change
2782 follows. */
2783 charpos = next_overlay_change (IT_CHARPOS (*it));
2784 if (charpos < it->stop_charpos)
2785 it->stop_charpos = charpos;
2786
2787 /* If showing the region, we have to stop at the region
2788 start or end because the face might change there. */
2789 if (it->region_beg_charpos > 0)
2790 {
2791 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2792 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2793 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2794 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2795 }
2796
2797 /* Set up variables for computing the stop position from text
2798 property changes. */
2799 XSETBUFFER (object, current_buffer);
2800 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2801 position = make_number (IT_CHARPOS (*it));
2802
2803 }
2804
2805 /* Get the interval containing IT's position. Value is a null
2806 interval if there isn't such an interval. */
2807 iv = validate_interval_range (object, &position, &position, 0);
2808 if (!NULL_INTERVAL_P (iv))
2809 {
2810 Lisp_Object values_here[LAST_PROP_IDX];
2811 struct props *p;
2812
2813 /* Get properties here. */
2814 for (p = it_props; p->handler; ++p)
2815 values_here[p->idx] = textget (iv->plist, *p->name);
2816
2817 /* Look for an interval following iv that has different
2818 properties. */
2819 for (next_iv = next_interval (iv);
2820 (!NULL_INTERVAL_P (next_iv)
2821 && (NILP (limit)
2822 || XFASTINT (limit) > next_iv->position));
2823 next_iv = next_interval (next_iv))
2824 {
2825 for (p = it_props; p->handler; ++p)
2826 {
2827 Lisp_Object new_value;
2828
2829 new_value = textget (next_iv->plist, *p->name);
2830 if (!EQ (values_here[p->idx], new_value))
2831 break;
2832 }
2833
2834 if (p->handler)
2835 break;
2836 }
2837
2838 if (!NULL_INTERVAL_P (next_iv))
2839 {
2840 if (INTEGERP (limit)
2841 && next_iv->position >= XFASTINT (limit))
2842 /* No text property change up to limit. */
2843 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2844 else
2845 /* Text properties change in next_iv. */
2846 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2847 }
2848 }
2849
2850 xassert (STRINGP (it->string)
2851 || (it->stop_charpos >= BEGV
2852 && it->stop_charpos >= IT_CHARPOS (*it)));
2853 }
2854
2855
2856 /* Return the position of the next overlay change after POS in
2857 current_buffer. Value is point-max if no overlay change
2858 follows. This is like `next-overlay-change' but doesn't use
2859 xmalloc. */
2860
2861 static int
2862 next_overlay_change (pos)
2863 int pos;
2864 {
2865 int noverlays;
2866 int endpos;
2867 Lisp_Object *overlays;
2868 int i;
2869
2870 /* Get all overlays at the given position. */
2871 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2872
2873 /* If any of these overlays ends before endpos,
2874 use its ending point instead. */
2875 for (i = 0; i < noverlays; ++i)
2876 {
2877 Lisp_Object oend;
2878 int oendpos;
2879
2880 oend = OVERLAY_END (overlays[i]);
2881 oendpos = OVERLAY_POSITION (oend);
2882 endpos = min (endpos, oendpos);
2883 }
2884
2885 return endpos;
2886 }
2887
2888
2889 \f
2890 /***********************************************************************
2891 Fontification
2892 ***********************************************************************/
2893
2894 /* Handle changes in the `fontified' property of the current buffer by
2895 calling hook functions from Qfontification_functions to fontify
2896 regions of text. */
2897
2898 static enum prop_handled
2899 handle_fontified_prop (it)
2900 struct it *it;
2901 {
2902 Lisp_Object prop, pos;
2903 enum prop_handled handled = HANDLED_NORMALLY;
2904
2905 /* Get the value of the `fontified' property at IT's current buffer
2906 position. (The `fontified' property doesn't have a special
2907 meaning in strings.) If the value is nil, call functions from
2908 Qfontification_functions. */
2909 if (!STRINGP (it->string)
2910 && it->s == NULL
2911 && !NILP (Vfontification_functions)
2912 && !NILP (Vrun_hooks)
2913 && (pos = make_number (IT_CHARPOS (*it)),
2914 prop = Fget_char_property (pos, Qfontified, Qnil),
2915 NILP (prop)))
2916 {
2917 int count = SPECPDL_INDEX ();
2918 Lisp_Object val;
2919
2920 val = Vfontification_functions;
2921 specbind (Qfontification_functions, Qnil);
2922
2923 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2924 safe_call1 (val, pos);
2925 else
2926 {
2927 Lisp_Object globals, fn;
2928 struct gcpro gcpro1, gcpro2;
2929
2930 globals = Qnil;
2931 GCPRO2 (val, globals);
2932
2933 for (; CONSP (val); val = XCDR (val))
2934 {
2935 fn = XCAR (val);
2936
2937 if (EQ (fn, Qt))
2938 {
2939 /* A value of t indicates this hook has a local
2940 binding; it means to run the global binding too.
2941 In a global value, t should not occur. If it
2942 does, we must ignore it to avoid an endless
2943 loop. */
2944 for (globals = Fdefault_value (Qfontification_functions);
2945 CONSP (globals);
2946 globals = XCDR (globals))
2947 {
2948 fn = XCAR (globals);
2949 if (!EQ (fn, Qt))
2950 safe_call1 (fn, pos);
2951 }
2952 }
2953 else
2954 safe_call1 (fn, pos);
2955 }
2956
2957 UNGCPRO;
2958 }
2959
2960 unbind_to (count, Qnil);
2961
2962 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2963 something. This avoids an endless loop if they failed to
2964 fontify the text for which reason ever. */
2965 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2966 handled = HANDLED_RECOMPUTE_PROPS;
2967 }
2968
2969 return handled;
2970 }
2971
2972
2973 \f
2974 /***********************************************************************
2975 Faces
2976 ***********************************************************************/
2977
2978 /* Set up iterator IT from face properties at its current position.
2979 Called from handle_stop. */
2980
2981 static enum prop_handled
2982 handle_face_prop (it)
2983 struct it *it;
2984 {
2985 int new_face_id, next_stop;
2986
2987 if (!STRINGP (it->string))
2988 {
2989 new_face_id
2990 = face_at_buffer_position (it->w,
2991 IT_CHARPOS (*it),
2992 it->region_beg_charpos,
2993 it->region_end_charpos,
2994 &next_stop,
2995 (IT_CHARPOS (*it)
2996 + TEXT_PROP_DISTANCE_LIMIT),
2997 0);
2998
2999 /* Is this a start of a run of characters with box face?
3000 Caveat: this can be called for a freshly initialized
3001 iterator; face_id is -1 in this case. We know that the new
3002 face will not change until limit, i.e. if the new face has a
3003 box, all characters up to limit will have one. But, as
3004 usual, we don't know whether limit is really the end. */
3005 if (new_face_id != it->face_id)
3006 {
3007 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3008
3009 /* If new face has a box but old face has not, this is
3010 the start of a run of characters with box, i.e. it has
3011 a shadow on the left side. The value of face_id of the
3012 iterator will be -1 if this is the initial call that gets
3013 the face. In this case, we have to look in front of IT's
3014 position and see whether there is a face != new_face_id. */
3015 it->start_of_box_run_p
3016 = (new_face->box != FACE_NO_BOX
3017 && (it->face_id >= 0
3018 || IT_CHARPOS (*it) == BEG
3019 || new_face_id != face_before_it_pos (it)));
3020 it->face_box_p = new_face->box != FACE_NO_BOX;
3021 }
3022 }
3023 else
3024 {
3025 int base_face_id, bufpos;
3026
3027 if (it->current.overlay_string_index >= 0)
3028 bufpos = IT_CHARPOS (*it);
3029 else
3030 bufpos = 0;
3031
3032 /* For strings from a buffer, i.e. overlay strings or strings
3033 from a `display' property, use the face at IT's current
3034 buffer position as the base face to merge with, so that
3035 overlay strings appear in the same face as surrounding
3036 text, unless they specify their own faces. */
3037 base_face_id = underlying_face_id (it);
3038
3039 new_face_id = face_at_string_position (it->w,
3040 it->string,
3041 IT_STRING_CHARPOS (*it),
3042 bufpos,
3043 it->region_beg_charpos,
3044 it->region_end_charpos,
3045 &next_stop,
3046 base_face_id, 0);
3047
3048 #if 0 /* This shouldn't be neccessary. Let's check it. */
3049 /* If IT is used to display a mode line we would really like to
3050 use the mode line face instead of the frame's default face. */
3051 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3052 && new_face_id == DEFAULT_FACE_ID)
3053 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3054 #endif
3055
3056 /* Is this a start of a run of characters with box? Caveat:
3057 this can be called for a freshly allocated iterator; face_id
3058 is -1 is this case. We know that the new face will not
3059 change until the next check pos, i.e. if the new face has a
3060 box, all characters up to that position will have a
3061 box. But, as usual, we don't know whether that position
3062 is really the end. */
3063 if (new_face_id != it->face_id)
3064 {
3065 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3066 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3067
3068 /* If new face has a box but old face hasn't, this is the
3069 start of a run of characters with box, i.e. it has a
3070 shadow on the left side. */
3071 it->start_of_box_run_p
3072 = new_face->box && (old_face == NULL || !old_face->box);
3073 it->face_box_p = new_face->box != FACE_NO_BOX;
3074 }
3075 }
3076
3077 it->face_id = new_face_id;
3078 return HANDLED_NORMALLY;
3079 }
3080
3081
3082 /* Return the ID of the face ``underlying'' IT's current position,
3083 which is in a string. If the iterator is associated with a
3084 buffer, return the face at IT's current buffer position.
3085 Otherwise, use the iterator's base_face_id. */
3086
3087 static int
3088 underlying_face_id (it)
3089 struct it *it;
3090 {
3091 int face_id = it->base_face_id, i;
3092
3093 xassert (STRINGP (it->string));
3094
3095 for (i = it->sp - 1; i >= 0; --i)
3096 if (NILP (it->stack[i].string))
3097 face_id = it->stack[i].face_id;
3098
3099 return face_id;
3100 }
3101
3102
3103 /* Compute the face one character before or after the current position
3104 of IT. BEFORE_P non-zero means get the face in front of IT's
3105 position. Value is the id of the face. */
3106
3107 static int
3108 face_before_or_after_it_pos (it, before_p)
3109 struct it *it;
3110 int before_p;
3111 {
3112 int face_id, limit;
3113 int next_check_charpos;
3114 struct text_pos pos;
3115
3116 xassert (it->s == NULL);
3117
3118 if (STRINGP (it->string))
3119 {
3120 int bufpos, base_face_id;
3121
3122 /* No face change past the end of the string (for the case
3123 we are padding with spaces). No face change before the
3124 string start. */
3125 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3126 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3127 return it->face_id;
3128
3129 /* Set pos to the position before or after IT's current position. */
3130 if (before_p)
3131 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3132 else
3133 /* For composition, we must check the character after the
3134 composition. */
3135 pos = (it->what == IT_COMPOSITION
3136 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3137 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3138
3139 if (it->current.overlay_string_index >= 0)
3140 bufpos = IT_CHARPOS (*it);
3141 else
3142 bufpos = 0;
3143
3144 base_face_id = underlying_face_id (it);
3145
3146 /* Get the face for ASCII, or unibyte. */
3147 face_id = face_at_string_position (it->w,
3148 it->string,
3149 CHARPOS (pos),
3150 bufpos,
3151 it->region_beg_charpos,
3152 it->region_end_charpos,
3153 &next_check_charpos,
3154 base_face_id, 0);
3155
3156 /* Correct the face for charsets different from ASCII. Do it
3157 for the multibyte case only. The face returned above is
3158 suitable for unibyte text if IT->string is unibyte. */
3159 if (STRING_MULTIBYTE (it->string))
3160 {
3161 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3162 int rest = SBYTES (it->string) - BYTEPOS (pos);
3163 int c, len;
3164 struct face *face = FACE_FROM_ID (it->f, face_id);
3165
3166 c = string_char_and_length (p, rest, &len);
3167 face_id = FACE_FOR_CHAR (it->f, face, c);
3168 }
3169 }
3170 else
3171 {
3172 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3173 || (IT_CHARPOS (*it) <= BEGV && before_p))
3174 return it->face_id;
3175
3176 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3177 pos = it->current.pos;
3178
3179 if (before_p)
3180 DEC_TEXT_POS (pos, it->multibyte_p);
3181 else
3182 {
3183 if (it->what == IT_COMPOSITION)
3184 /* For composition, we must check the position after the
3185 composition. */
3186 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3187 else
3188 INC_TEXT_POS (pos, it->multibyte_p);
3189 }
3190
3191 /* Determine face for CHARSET_ASCII, or unibyte. */
3192 face_id = face_at_buffer_position (it->w,
3193 CHARPOS (pos),
3194 it->region_beg_charpos,
3195 it->region_end_charpos,
3196 &next_check_charpos,
3197 limit, 0);
3198
3199 /* Correct the face for charsets different from ASCII. Do it
3200 for the multibyte case only. The face returned above is
3201 suitable for unibyte text if current_buffer is unibyte. */
3202 if (it->multibyte_p)
3203 {
3204 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3205 struct face *face = FACE_FROM_ID (it->f, face_id);
3206 face_id = FACE_FOR_CHAR (it->f, face, c);
3207 }
3208 }
3209
3210 return face_id;
3211 }
3212
3213
3214 \f
3215 /***********************************************************************
3216 Invisible text
3217 ***********************************************************************/
3218
3219 /* Set up iterator IT from invisible properties at its current
3220 position. Called from handle_stop. */
3221
3222 static enum prop_handled
3223 handle_invisible_prop (it)
3224 struct it *it;
3225 {
3226 enum prop_handled handled = HANDLED_NORMALLY;
3227
3228 if (STRINGP (it->string))
3229 {
3230 extern Lisp_Object Qinvisible;
3231 Lisp_Object prop, end_charpos, limit, charpos;
3232
3233 /* Get the value of the invisible text property at the
3234 current position. Value will be nil if there is no such
3235 property. */
3236 charpos = make_number (IT_STRING_CHARPOS (*it));
3237 prop = Fget_text_property (charpos, Qinvisible, it->string);
3238
3239 if (!NILP (prop)
3240 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3241 {
3242 handled = HANDLED_RECOMPUTE_PROPS;
3243
3244 /* Get the position at which the next change of the
3245 invisible text property can be found in IT->string.
3246 Value will be nil if the property value is the same for
3247 all the rest of IT->string. */
3248 XSETINT (limit, SCHARS (it->string));
3249 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3250 it->string, limit);
3251
3252 /* Text at current position is invisible. The next
3253 change in the property is at position end_charpos.
3254 Move IT's current position to that position. */
3255 if (INTEGERP (end_charpos)
3256 && XFASTINT (end_charpos) < XFASTINT (limit))
3257 {
3258 struct text_pos old;
3259 old = it->current.string_pos;
3260 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3261 compute_string_pos (&it->current.string_pos, old, it->string);
3262 }
3263 else
3264 {
3265 /* The rest of the string is invisible. If this is an
3266 overlay string, proceed with the next overlay string
3267 or whatever comes and return a character from there. */
3268 if (it->current.overlay_string_index >= 0)
3269 {
3270 next_overlay_string (it);
3271 /* Don't check for overlay strings when we just
3272 finished processing them. */
3273 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3274 }
3275 else
3276 {
3277 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3278 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3279 }
3280 }
3281 }
3282 }
3283 else
3284 {
3285 int invis_p, newpos, next_stop, start_charpos;
3286 Lisp_Object pos, prop, overlay;
3287
3288 /* First of all, is there invisible text at this position? */
3289 start_charpos = IT_CHARPOS (*it);
3290 pos = make_number (IT_CHARPOS (*it));
3291 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3292 &overlay);
3293 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3294
3295 /* If we are on invisible text, skip over it. */
3296 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3297 {
3298 /* Record whether we have to display an ellipsis for the
3299 invisible text. */
3300 int display_ellipsis_p = invis_p == 2;
3301
3302 handled = HANDLED_RECOMPUTE_PROPS;
3303
3304 /* Loop skipping over invisible text. The loop is left at
3305 ZV or with IT on the first char being visible again. */
3306 do
3307 {
3308 /* Try to skip some invisible text. Return value is the
3309 position reached which can be equal to IT's position
3310 if there is nothing invisible here. This skips both
3311 over invisible text properties and overlays with
3312 invisible property. */
3313 newpos = skip_invisible (IT_CHARPOS (*it),
3314 &next_stop, ZV, it->window);
3315
3316 /* If we skipped nothing at all we weren't at invisible
3317 text in the first place. If everything to the end of
3318 the buffer was skipped, end the loop. */
3319 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3320 invis_p = 0;
3321 else
3322 {
3323 /* We skipped some characters but not necessarily
3324 all there are. Check if we ended up on visible
3325 text. Fget_char_property returns the property of
3326 the char before the given position, i.e. if we
3327 get invis_p = 0, this means that the char at
3328 newpos is visible. */
3329 pos = make_number (newpos);
3330 prop = Fget_char_property (pos, Qinvisible, it->window);
3331 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3332 }
3333
3334 /* If we ended up on invisible text, proceed to
3335 skip starting with next_stop. */
3336 if (invis_p)
3337 IT_CHARPOS (*it) = next_stop;
3338 }
3339 while (invis_p);
3340
3341 /* The position newpos is now either ZV or on visible text. */
3342 IT_CHARPOS (*it) = newpos;
3343 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3344
3345 /* If there are before-strings at the start of invisible
3346 text, and the text is invisible because of a text
3347 property, arrange to show before-strings because 20.x did
3348 it that way. (If the text is invisible because of an
3349 overlay property instead of a text property, this is
3350 already handled in the overlay code.) */
3351 if (NILP (overlay)
3352 && get_overlay_strings (it, start_charpos))
3353 {
3354 handled = HANDLED_RECOMPUTE_PROPS;
3355 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3356 }
3357 else if (display_ellipsis_p)
3358 setup_for_ellipsis (it, 0);
3359 }
3360 }
3361
3362 return handled;
3363 }
3364
3365
3366 /* Make iterator IT return `...' next.
3367 Replaces LEN characters from buffer. */
3368
3369 static void
3370 setup_for_ellipsis (it, len)
3371 struct it *it;
3372 int len;
3373 {
3374 /* Use the display table definition for `...'. Invalid glyphs
3375 will be handled by the method returning elements from dpvec. */
3376 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3377 {
3378 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3379 it->dpvec = v->contents;
3380 it->dpend = v->contents + v->size;
3381 }
3382 else
3383 {
3384 /* Default `...'. */
3385 it->dpvec = default_invis_vector;
3386 it->dpend = default_invis_vector + 3;
3387 }
3388
3389 it->dpvec_char_len = len;
3390 it->current.dpvec_index = 0;
3391 it->dpvec_face_id = -1;
3392
3393 /* Remember the current face id in case glyphs specify faces.
3394 IT's face is restored in set_iterator_to_next.
3395 saved_face_id was set to preceding char's face in handle_stop. */
3396 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3397 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3398
3399 it->method = GET_FROM_DISPLAY_VECTOR;
3400 it->ellipsis_p = 1;
3401 }
3402
3403
3404 \f
3405 /***********************************************************************
3406 'display' property
3407 ***********************************************************************/
3408
3409 /* Set up iterator IT from `display' property at its current position.
3410 Called from handle_stop.
3411 We return HANDLED_RETURN if some part of the display property
3412 overrides the display of the buffer text itself.
3413 Otherwise we return HANDLED_NORMALLY. */
3414
3415 static enum prop_handled
3416 handle_display_prop (it)
3417 struct it *it;
3418 {
3419 Lisp_Object prop, object;
3420 struct text_pos *position;
3421 /* Nonzero if some property replaces the display of the text itself. */
3422 int display_replaced_p = 0;
3423
3424 if (STRINGP (it->string))
3425 {
3426 object = it->string;
3427 position = &it->current.string_pos;
3428 }
3429 else
3430 {
3431 XSETWINDOW (object, it->w);
3432 position = &it->current.pos;
3433 }
3434
3435 /* Reset those iterator values set from display property values. */
3436 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3437 it->space_width = Qnil;
3438 it->font_height = Qnil;
3439 it->voffset = 0;
3440
3441 /* We don't support recursive `display' properties, i.e. string
3442 values that have a string `display' property, that have a string
3443 `display' property etc. */
3444 if (!it->string_from_display_prop_p)
3445 it->area = TEXT_AREA;
3446
3447 prop = Fget_char_property (make_number (position->charpos),
3448 Qdisplay, object);
3449 if (NILP (prop))
3450 return HANDLED_NORMALLY;
3451
3452 if (!STRINGP (it->string))
3453 object = it->w->buffer;
3454
3455 if (CONSP (prop)
3456 /* Simple properties. */
3457 && !EQ (XCAR (prop), Qimage)
3458 && !EQ (XCAR (prop), Qspace)
3459 && !EQ (XCAR (prop), Qwhen)
3460 && !EQ (XCAR (prop), Qslice)
3461 && !EQ (XCAR (prop), Qspace_width)
3462 && !EQ (XCAR (prop), Qheight)
3463 && !EQ (XCAR (prop), Qraise)
3464 /* Marginal area specifications. */
3465 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3466 && !EQ (XCAR (prop), Qleft_fringe)
3467 && !EQ (XCAR (prop), Qright_fringe)
3468 && !NILP (XCAR (prop)))
3469 {
3470 for (; CONSP (prop); prop = XCDR (prop))
3471 {
3472 if (handle_single_display_spec (it, XCAR (prop), object,
3473 position, display_replaced_p))
3474 display_replaced_p = 1;
3475 }
3476 }
3477 else if (VECTORP (prop))
3478 {
3479 int i;
3480 for (i = 0; i < ASIZE (prop); ++i)
3481 if (handle_single_display_spec (it, AREF (prop, i), object,
3482 position, display_replaced_p))
3483 display_replaced_p = 1;
3484 }
3485 else
3486 {
3487 int ret = handle_single_display_spec (it, prop, object, position, 0);
3488 if (ret < 0) /* Replaced by "", i.e. nothing. */
3489 return HANDLED_RECOMPUTE_PROPS;
3490 if (ret)
3491 display_replaced_p = 1;
3492 }
3493
3494 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3495 }
3496
3497
3498 /* Value is the position of the end of the `display' property starting
3499 at START_POS in OBJECT. */
3500
3501 static struct text_pos
3502 display_prop_end (it, object, start_pos)
3503 struct it *it;
3504 Lisp_Object object;
3505 struct text_pos start_pos;
3506 {
3507 Lisp_Object end;
3508 struct text_pos end_pos;
3509
3510 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3511 Qdisplay, object, Qnil);
3512 CHARPOS (end_pos) = XFASTINT (end);
3513 if (STRINGP (object))
3514 compute_string_pos (&end_pos, start_pos, it->string);
3515 else
3516 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3517
3518 return end_pos;
3519 }
3520
3521
3522 /* Set up IT from a single `display' specification PROP. OBJECT
3523 is the object in which the `display' property was found. *POSITION
3524 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3525 means that we previously saw a display specification which already
3526 replaced text display with something else, for example an image;
3527 we ignore such properties after the first one has been processed.
3528
3529 If PROP is a `space' or `image' specification, and in some other
3530 cases too, set *POSITION to the position where the `display'
3531 property ends.
3532
3533 Value is non-zero if something was found which replaces the display
3534 of buffer or string text. Specifically, the value is -1 if that
3535 "something" is "nothing". */
3536
3537 static int
3538 handle_single_display_spec (it, spec, object, position,
3539 display_replaced_before_p)
3540 struct it *it;
3541 Lisp_Object spec;
3542 Lisp_Object object;
3543 struct text_pos *position;
3544 int display_replaced_before_p;
3545 {
3546 Lisp_Object form;
3547 Lisp_Object location, value;
3548 struct text_pos start_pos;
3549 int valid_p;
3550
3551 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3552 If the result is non-nil, use VALUE instead of SPEC. */
3553 form = Qt;
3554 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3555 {
3556 spec = XCDR (spec);
3557 if (!CONSP (spec))
3558 return 0;
3559 form = XCAR (spec);
3560 spec = XCDR (spec);
3561 }
3562
3563 if (!NILP (form) && !EQ (form, Qt))
3564 {
3565 int count = SPECPDL_INDEX ();
3566 struct gcpro gcpro1;
3567
3568 /* Bind `object' to the object having the `display' property, a
3569 buffer or string. Bind `position' to the position in the
3570 object where the property was found, and `buffer-position'
3571 to the current position in the buffer. */
3572 specbind (Qobject, object);
3573 specbind (Qposition, make_number (CHARPOS (*position)));
3574 specbind (Qbuffer_position,
3575 make_number (STRINGP (object)
3576 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3577 GCPRO1 (form);
3578 form = safe_eval (form);
3579 UNGCPRO;
3580 unbind_to (count, Qnil);
3581 }
3582
3583 if (NILP (form))
3584 return 0;
3585
3586 /* Handle `(height HEIGHT)' specifications. */
3587 if (CONSP (spec)
3588 && EQ (XCAR (spec), Qheight)
3589 && CONSP (XCDR (spec)))
3590 {
3591 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3592 return 0;
3593
3594 it->font_height = XCAR (XCDR (spec));
3595 if (!NILP (it->font_height))
3596 {
3597 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3598 int new_height = -1;
3599
3600 if (CONSP (it->font_height)
3601 && (EQ (XCAR (it->font_height), Qplus)
3602 || EQ (XCAR (it->font_height), Qminus))
3603 && CONSP (XCDR (it->font_height))
3604 && INTEGERP (XCAR (XCDR (it->font_height))))
3605 {
3606 /* `(+ N)' or `(- N)' where N is an integer. */
3607 int steps = XINT (XCAR (XCDR (it->font_height)));
3608 if (EQ (XCAR (it->font_height), Qplus))
3609 steps = - steps;
3610 it->face_id = smaller_face (it->f, it->face_id, steps);
3611 }
3612 else if (FUNCTIONP (it->font_height))
3613 {
3614 /* Call function with current height as argument.
3615 Value is the new height. */
3616 Lisp_Object height;
3617 height = safe_call1 (it->font_height,
3618 face->lface[LFACE_HEIGHT_INDEX]);
3619 if (NUMBERP (height))
3620 new_height = XFLOATINT (height);
3621 }
3622 else if (NUMBERP (it->font_height))
3623 {
3624 /* Value is a multiple of the canonical char height. */
3625 struct face *face;
3626
3627 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3628 new_height = (XFLOATINT (it->font_height)
3629 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3630 }
3631 else
3632 {
3633 /* Evaluate IT->font_height with `height' bound to the
3634 current specified height to get the new height. */
3635 int count = SPECPDL_INDEX ();
3636
3637 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3638 value = safe_eval (it->font_height);
3639 unbind_to (count, Qnil);
3640
3641 if (NUMBERP (value))
3642 new_height = XFLOATINT (value);
3643 }
3644
3645 if (new_height > 0)
3646 it->face_id = face_with_height (it->f, it->face_id, new_height);
3647 }
3648
3649 return 0;
3650 }
3651
3652 /* Handle `(space_width WIDTH)'. */
3653 if (CONSP (spec)
3654 && EQ (XCAR (spec), Qspace_width)
3655 && CONSP (XCDR (spec)))
3656 {
3657 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3658 return 0;
3659
3660 value = XCAR (XCDR (spec));
3661 if (NUMBERP (value) && XFLOATINT (value) > 0)
3662 it->space_width = value;
3663
3664 return 0;
3665 }
3666
3667 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3668 if (CONSP (spec)
3669 && EQ (XCAR (spec), Qslice))
3670 {
3671 Lisp_Object tem;
3672
3673 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3674 return 0;
3675
3676 if (tem = XCDR (spec), CONSP (tem))
3677 {
3678 it->slice.x = XCAR (tem);
3679 if (tem = XCDR (tem), CONSP (tem))
3680 {
3681 it->slice.y = XCAR (tem);
3682 if (tem = XCDR (tem), CONSP (tem))
3683 {
3684 it->slice.width = XCAR (tem);
3685 if (tem = XCDR (tem), CONSP (tem))
3686 it->slice.height = XCAR (tem);
3687 }
3688 }
3689 }
3690
3691 return 0;
3692 }
3693
3694 /* Handle `(raise FACTOR)'. */
3695 if (CONSP (spec)
3696 && EQ (XCAR (spec), Qraise)
3697 && CONSP (XCDR (spec)))
3698 {
3699 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3700 return 0;
3701
3702 #ifdef HAVE_WINDOW_SYSTEM
3703 value = XCAR (XCDR (spec));
3704 if (NUMBERP (value))
3705 {
3706 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3707 it->voffset = - (XFLOATINT (value)
3708 * (FONT_HEIGHT (face->font)));
3709 }
3710 #endif /* HAVE_WINDOW_SYSTEM */
3711
3712 return 0;
3713 }
3714
3715 /* Don't handle the other kinds of display specifications
3716 inside a string that we got from a `display' property. */
3717 if (it->string_from_display_prop_p)
3718 return 0;
3719
3720 /* Characters having this form of property are not displayed, so
3721 we have to find the end of the property. */
3722 start_pos = *position;
3723 *position = display_prop_end (it, object, start_pos);
3724 value = Qnil;
3725
3726 /* Stop the scan at that end position--we assume that all
3727 text properties change there. */
3728 it->stop_charpos = position->charpos;
3729
3730 /* Handle `(left-fringe BITMAP [FACE])'
3731 and `(right-fringe BITMAP [FACE])'. */
3732 if (CONSP (spec)
3733 && (EQ (XCAR (spec), Qleft_fringe)
3734 || EQ (XCAR (spec), Qright_fringe))
3735 && CONSP (XCDR (spec)))
3736 {
3737 int face_id = DEFAULT_FACE_ID;
3738 int fringe_bitmap;
3739
3740 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3741 /* If we return here, POSITION has been advanced
3742 across the text with this property. */
3743 return 0;
3744
3745 #ifdef HAVE_WINDOW_SYSTEM
3746 value = XCAR (XCDR (spec));
3747 if (!SYMBOLP (value)
3748 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3749 /* If we return here, POSITION has been advanced
3750 across the text with this property. */
3751 return 0;
3752
3753 if (CONSP (XCDR (XCDR (spec))))
3754 {
3755 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3756 int face_id2 = lookup_derived_face (it->f, face_name,
3757 'A', FRINGE_FACE_ID, 0);
3758 if (face_id2 >= 0)
3759 face_id = face_id2;
3760 }
3761
3762 /* Save current settings of IT so that we can restore them
3763 when we are finished with the glyph property value. */
3764
3765 push_it (it);
3766
3767 it->area = TEXT_AREA;
3768 it->what = IT_IMAGE;
3769 it->image_id = -1; /* no image */
3770 it->position = start_pos;
3771 it->object = NILP (object) ? it->w->buffer : object;
3772 it->method = GET_FROM_IMAGE;
3773 it->face_id = face_id;
3774
3775 /* Say that we haven't consumed the characters with
3776 `display' property yet. The call to pop_it in
3777 set_iterator_to_next will clean this up. */
3778 *position = start_pos;
3779
3780 if (EQ (XCAR (spec), Qleft_fringe))
3781 {
3782 it->left_user_fringe_bitmap = fringe_bitmap;
3783 it->left_user_fringe_face_id = face_id;
3784 }
3785 else
3786 {
3787 it->right_user_fringe_bitmap = fringe_bitmap;
3788 it->right_user_fringe_face_id = face_id;
3789 }
3790 #endif /* HAVE_WINDOW_SYSTEM */
3791 return 1;
3792 }
3793
3794 /* Prepare to handle `((margin left-margin) ...)',
3795 `((margin right-margin) ...)' and `((margin nil) ...)'
3796 prefixes for display specifications. */
3797 location = Qunbound;
3798 if (CONSP (spec) && CONSP (XCAR (spec)))
3799 {
3800 Lisp_Object tem;
3801
3802 value = XCDR (spec);
3803 if (CONSP (value))
3804 value = XCAR (value);
3805
3806 tem = XCAR (spec);
3807 if (EQ (XCAR (tem), Qmargin)
3808 && (tem = XCDR (tem),
3809 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3810 (NILP (tem)
3811 || EQ (tem, Qleft_margin)
3812 || EQ (tem, Qright_margin))))
3813 location = tem;
3814 }
3815
3816 if (EQ (location, Qunbound))
3817 {
3818 location = Qnil;
3819 value = spec;
3820 }
3821
3822 /* After this point, VALUE is the property after any
3823 margin prefix has been stripped. It must be a string,
3824 an image specification, or `(space ...)'.
3825
3826 LOCATION specifies where to display: `left-margin',
3827 `right-margin' or nil. */
3828
3829 valid_p = (STRINGP (value)
3830 #ifdef HAVE_WINDOW_SYSTEM
3831 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3832 #endif /* not HAVE_WINDOW_SYSTEM */
3833 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3834
3835 if (valid_p && !display_replaced_before_p)
3836 {
3837 /* Save current settings of IT so that we can restore them
3838 when we are finished with the glyph property value. */
3839 push_it (it);
3840
3841 if (NILP (location))
3842 it->area = TEXT_AREA;
3843 else if (EQ (location, Qleft_margin))
3844 it->area = LEFT_MARGIN_AREA;
3845 else
3846 it->area = RIGHT_MARGIN_AREA;
3847
3848 if (STRINGP (value))
3849 {
3850 if (SCHARS (value) == 0)
3851 {
3852 pop_it (it);
3853 return -1; /* Replaced by "", i.e. nothing. */
3854 }
3855 it->string = value;
3856 it->multibyte_p = STRING_MULTIBYTE (it->string);
3857 it->current.overlay_string_index = -1;
3858 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3859 it->end_charpos = it->string_nchars = SCHARS (it->string);
3860 it->method = GET_FROM_STRING;
3861 it->stop_charpos = 0;
3862 it->string_from_display_prop_p = 1;
3863 /* Say that we haven't consumed the characters with
3864 `display' property yet. The call to pop_it in
3865 set_iterator_to_next will clean this up. */
3866 *position = start_pos;
3867 }
3868 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3869 {
3870 it->method = GET_FROM_STRETCH;
3871 it->object = value;
3872 it->current.pos = it->position = start_pos;
3873 }
3874 #ifdef HAVE_WINDOW_SYSTEM
3875 else
3876 {
3877 it->what = IT_IMAGE;
3878 it->image_id = lookup_image (it->f, value);
3879 it->position = start_pos;
3880 it->object = NILP (object) ? it->w->buffer : object;
3881 it->method = GET_FROM_IMAGE;
3882
3883 /* Say that we haven't consumed the characters with
3884 `display' property yet. The call to pop_it in
3885 set_iterator_to_next will clean this up. */
3886 *position = start_pos;
3887 }
3888 #endif /* HAVE_WINDOW_SYSTEM */
3889
3890 return 1;
3891 }
3892
3893 /* Invalid property or property not supported. Restore
3894 POSITION to what it was before. */
3895 *position = start_pos;
3896 return 0;
3897 }
3898
3899
3900 /* Check if SPEC is a display specification value whose text should be
3901 treated as intangible. */
3902
3903 static int
3904 single_display_spec_intangible_p (prop)
3905 Lisp_Object prop;
3906 {
3907 /* Skip over `when FORM'. */
3908 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3909 {
3910 prop = XCDR (prop);
3911 if (!CONSP (prop))
3912 return 0;
3913 prop = XCDR (prop);
3914 }
3915
3916 if (STRINGP (prop))
3917 return 1;
3918
3919 if (!CONSP (prop))
3920 return 0;
3921
3922 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3923 we don't need to treat text as intangible. */
3924 if (EQ (XCAR (prop), Qmargin))
3925 {
3926 prop = XCDR (prop);
3927 if (!CONSP (prop))
3928 return 0;
3929
3930 prop = XCDR (prop);
3931 if (!CONSP (prop)
3932 || EQ (XCAR (prop), Qleft_margin)
3933 || EQ (XCAR (prop), Qright_margin))
3934 return 0;
3935 }
3936
3937 return (CONSP (prop)
3938 && (EQ (XCAR (prop), Qimage)
3939 || EQ (XCAR (prop), Qspace)));
3940 }
3941
3942
3943 /* Check if PROP is a display property value whose text should be
3944 treated as intangible. */
3945
3946 int
3947 display_prop_intangible_p (prop)
3948 Lisp_Object prop;
3949 {
3950 if (CONSP (prop)
3951 && CONSP (XCAR (prop))
3952 && !EQ (Qmargin, XCAR (XCAR (prop))))
3953 {
3954 /* A list of sub-properties. */
3955 while (CONSP (prop))
3956 {
3957 if (single_display_spec_intangible_p (XCAR (prop)))
3958 return 1;
3959 prop = XCDR (prop);
3960 }
3961 }
3962 else if (VECTORP (prop))
3963 {
3964 /* A vector of sub-properties. */
3965 int i;
3966 for (i = 0; i < ASIZE (prop); ++i)
3967 if (single_display_spec_intangible_p (AREF (prop, i)))
3968 return 1;
3969 }
3970 else
3971 return single_display_spec_intangible_p (prop);
3972
3973 return 0;
3974 }
3975
3976
3977 /* Return 1 if PROP is a display sub-property value containing STRING. */
3978
3979 static int
3980 single_display_spec_string_p (prop, string)
3981 Lisp_Object prop, string;
3982 {
3983 if (EQ (string, prop))
3984 return 1;
3985
3986 /* Skip over `when FORM'. */
3987 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3988 {
3989 prop = XCDR (prop);
3990 if (!CONSP (prop))
3991 return 0;
3992 prop = XCDR (prop);
3993 }
3994
3995 if (CONSP (prop))
3996 /* Skip over `margin LOCATION'. */
3997 if (EQ (XCAR (prop), Qmargin))
3998 {
3999 prop = XCDR (prop);
4000 if (!CONSP (prop))
4001 return 0;
4002
4003 prop = XCDR (prop);
4004 if (!CONSP (prop))
4005 return 0;
4006 }
4007
4008 return CONSP (prop) && EQ (XCAR (prop), string);
4009 }
4010
4011
4012 /* Return 1 if STRING appears in the `display' property PROP. */
4013
4014 static int
4015 display_prop_string_p (prop, string)
4016 Lisp_Object prop, string;
4017 {
4018 if (CONSP (prop)
4019 && CONSP (XCAR (prop))
4020 && !EQ (Qmargin, XCAR (XCAR (prop))))
4021 {
4022 /* A list of sub-properties. */
4023 while (CONSP (prop))
4024 {
4025 if (single_display_spec_string_p (XCAR (prop), string))
4026 return 1;
4027 prop = XCDR (prop);
4028 }
4029 }
4030 else if (VECTORP (prop))
4031 {
4032 /* A vector of sub-properties. */
4033 int i;
4034 for (i = 0; i < ASIZE (prop); ++i)
4035 if (single_display_spec_string_p (AREF (prop, i), string))
4036 return 1;
4037 }
4038 else
4039 return single_display_spec_string_p (prop, string);
4040
4041 return 0;
4042 }
4043
4044
4045 /* Determine from which buffer position in W's buffer STRING comes
4046 from. AROUND_CHARPOS is an approximate position where it could
4047 be from. Value is the buffer position or 0 if it couldn't be
4048 determined.
4049
4050 W's buffer must be current.
4051
4052 This function is necessary because we don't record buffer positions
4053 in glyphs generated from strings (to keep struct glyph small).
4054 This function may only use code that doesn't eval because it is
4055 called asynchronously from note_mouse_highlight. */
4056
4057 int
4058 string_buffer_position (w, string, around_charpos)
4059 struct window *w;
4060 Lisp_Object string;
4061 int around_charpos;
4062 {
4063 Lisp_Object limit, prop, pos;
4064 const int MAX_DISTANCE = 1000;
4065 int found = 0;
4066
4067 pos = make_number (around_charpos);
4068 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4069 while (!found && !EQ (pos, limit))
4070 {
4071 prop = Fget_char_property (pos, Qdisplay, Qnil);
4072 if (!NILP (prop) && display_prop_string_p (prop, string))
4073 found = 1;
4074 else
4075 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4076 }
4077
4078 if (!found)
4079 {
4080 pos = make_number (around_charpos);
4081 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4082 while (!found && !EQ (pos, limit))
4083 {
4084 prop = Fget_char_property (pos, Qdisplay, Qnil);
4085 if (!NILP (prop) && display_prop_string_p (prop, string))
4086 found = 1;
4087 else
4088 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4089 limit);
4090 }
4091 }
4092
4093 return found ? XINT (pos) : 0;
4094 }
4095
4096
4097 \f
4098 /***********************************************************************
4099 `composition' property
4100 ***********************************************************************/
4101
4102 /* Set up iterator IT from `composition' property at its current
4103 position. Called from handle_stop. */
4104
4105 static enum prop_handled
4106 handle_composition_prop (it)
4107 struct it *it;
4108 {
4109 Lisp_Object prop, string;
4110 int pos, pos_byte, end;
4111 enum prop_handled handled = HANDLED_NORMALLY;
4112
4113 if (STRINGP (it->string))
4114 {
4115 pos = IT_STRING_CHARPOS (*it);
4116 pos_byte = IT_STRING_BYTEPOS (*it);
4117 string = it->string;
4118 }
4119 else
4120 {
4121 pos = IT_CHARPOS (*it);
4122 pos_byte = IT_BYTEPOS (*it);
4123 string = Qnil;
4124 }
4125
4126 /* If there's a valid composition and point is not inside of the
4127 composition (in the case that the composition is from the current
4128 buffer), draw a glyph composed from the composition components. */
4129 if (find_composition (pos, -1, &pos, &end, &prop, string)
4130 && COMPOSITION_VALID_P (pos, end, prop)
4131 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4132 {
4133 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4134
4135 if (id >= 0)
4136 {
4137 it->method = GET_FROM_COMPOSITION;
4138 it->cmp_id = id;
4139 it->cmp_len = COMPOSITION_LENGTH (prop);
4140 /* For a terminal, draw only the first character of the
4141 components. */
4142 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4143 it->len = (STRINGP (it->string)
4144 ? string_char_to_byte (it->string, end)
4145 : CHAR_TO_BYTE (end)) - pos_byte;
4146 it->stop_charpos = end;
4147 handled = HANDLED_RETURN;
4148 }
4149 }
4150
4151 return handled;
4152 }
4153
4154
4155 \f
4156 /***********************************************************************
4157 Overlay strings
4158 ***********************************************************************/
4159
4160 /* The following structure is used to record overlay strings for
4161 later sorting in load_overlay_strings. */
4162
4163 struct overlay_entry
4164 {
4165 Lisp_Object overlay;
4166 Lisp_Object string;
4167 int priority;
4168 int after_string_p;
4169 };
4170
4171
4172 /* Set up iterator IT from overlay strings at its current position.
4173 Called from handle_stop. */
4174
4175 static enum prop_handled
4176 handle_overlay_change (it)
4177 struct it *it;
4178 {
4179 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4180 return HANDLED_RECOMPUTE_PROPS;
4181 else
4182 return HANDLED_NORMALLY;
4183 }
4184
4185
4186 /* Set up the next overlay string for delivery by IT, if there is an
4187 overlay string to deliver. Called by set_iterator_to_next when the
4188 end of the current overlay string is reached. If there are more
4189 overlay strings to display, IT->string and
4190 IT->current.overlay_string_index are set appropriately here.
4191 Otherwise IT->string is set to nil. */
4192
4193 static void
4194 next_overlay_string (it)
4195 struct it *it;
4196 {
4197 ++it->current.overlay_string_index;
4198 if (it->current.overlay_string_index == it->n_overlay_strings)
4199 {
4200 /* No more overlay strings. Restore IT's settings to what
4201 they were before overlay strings were processed, and
4202 continue to deliver from current_buffer. */
4203 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4204
4205 pop_it (it);
4206 xassert (it->stop_charpos >= BEGV
4207 && it->stop_charpos <= it->end_charpos);
4208 it->string = Qnil;
4209 it->current.overlay_string_index = -1;
4210 SET_TEXT_POS (it->current.string_pos, -1, -1);
4211 it->n_overlay_strings = 0;
4212 it->method = GET_FROM_BUFFER;
4213
4214 /* If we're at the end of the buffer, record that we have
4215 processed the overlay strings there already, so that
4216 next_element_from_buffer doesn't try it again. */
4217 if (IT_CHARPOS (*it) >= it->end_charpos)
4218 it->overlay_strings_at_end_processed_p = 1;
4219
4220 /* If we have to display `...' for invisible text, set
4221 the iterator up for that. */
4222 if (display_ellipsis_p)
4223 setup_for_ellipsis (it, 0);
4224 }
4225 else
4226 {
4227 /* There are more overlay strings to process. If
4228 IT->current.overlay_string_index has advanced to a position
4229 where we must load IT->overlay_strings with more strings, do
4230 it. */
4231 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4232
4233 if (it->current.overlay_string_index && i == 0)
4234 load_overlay_strings (it, 0);
4235
4236 /* Initialize IT to deliver display elements from the overlay
4237 string. */
4238 it->string = it->overlay_strings[i];
4239 it->multibyte_p = STRING_MULTIBYTE (it->string);
4240 SET_TEXT_POS (it->current.string_pos, 0, 0);
4241 it->method = GET_FROM_STRING;
4242 it->stop_charpos = 0;
4243 }
4244
4245 CHECK_IT (it);
4246 }
4247
4248
4249 /* Compare two overlay_entry structures E1 and E2. Used as a
4250 comparison function for qsort in load_overlay_strings. Overlay
4251 strings for the same position are sorted so that
4252
4253 1. All after-strings come in front of before-strings, except
4254 when they come from the same overlay.
4255
4256 2. Within after-strings, strings are sorted so that overlay strings
4257 from overlays with higher priorities come first.
4258
4259 2. Within before-strings, strings are sorted so that overlay
4260 strings from overlays with higher priorities come last.
4261
4262 Value is analogous to strcmp. */
4263
4264
4265 static int
4266 compare_overlay_entries (e1, e2)
4267 void *e1, *e2;
4268 {
4269 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4270 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4271 int result;
4272
4273 if (entry1->after_string_p != entry2->after_string_p)
4274 {
4275 /* Let after-strings appear in front of before-strings if
4276 they come from different overlays. */
4277 if (EQ (entry1->overlay, entry2->overlay))
4278 result = entry1->after_string_p ? 1 : -1;
4279 else
4280 result = entry1->after_string_p ? -1 : 1;
4281 }
4282 else if (entry1->after_string_p)
4283 /* After-strings sorted in order of decreasing priority. */
4284 result = entry2->priority - entry1->priority;
4285 else
4286 /* Before-strings sorted in order of increasing priority. */
4287 result = entry1->priority - entry2->priority;
4288
4289 return result;
4290 }
4291
4292
4293 /* Load the vector IT->overlay_strings with overlay strings from IT's
4294 current buffer position, or from CHARPOS if that is > 0. Set
4295 IT->n_overlays to the total number of overlay strings found.
4296
4297 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4298 a time. On entry into load_overlay_strings,
4299 IT->current.overlay_string_index gives the number of overlay
4300 strings that have already been loaded by previous calls to this
4301 function.
4302
4303 IT->add_overlay_start contains an additional overlay start
4304 position to consider for taking overlay strings from, if non-zero.
4305 This position comes into play when the overlay has an `invisible'
4306 property, and both before and after-strings. When we've skipped to
4307 the end of the overlay, because of its `invisible' property, we
4308 nevertheless want its before-string to appear.
4309 IT->add_overlay_start will contain the overlay start position
4310 in this case.
4311
4312 Overlay strings are sorted so that after-string strings come in
4313 front of before-string strings. Within before and after-strings,
4314 strings are sorted by overlay priority. See also function
4315 compare_overlay_entries. */
4316
4317 static void
4318 load_overlay_strings (it, charpos)
4319 struct it *it;
4320 int charpos;
4321 {
4322 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4323 Lisp_Object overlay, window, str, invisible;
4324 struct Lisp_Overlay *ov;
4325 int start, end;
4326 int size = 20;
4327 int n = 0, i, j, invis_p;
4328 struct overlay_entry *entries
4329 = (struct overlay_entry *) alloca (size * sizeof *entries);
4330
4331 if (charpos <= 0)
4332 charpos = IT_CHARPOS (*it);
4333
4334 /* Append the overlay string STRING of overlay OVERLAY to vector
4335 `entries' which has size `size' and currently contains `n'
4336 elements. AFTER_P non-zero means STRING is an after-string of
4337 OVERLAY. */
4338 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4339 do \
4340 { \
4341 Lisp_Object priority; \
4342 \
4343 if (n == size) \
4344 { \
4345 int new_size = 2 * size; \
4346 struct overlay_entry *old = entries; \
4347 entries = \
4348 (struct overlay_entry *) alloca (new_size \
4349 * sizeof *entries); \
4350 bcopy (old, entries, size * sizeof *entries); \
4351 size = new_size; \
4352 } \
4353 \
4354 entries[n].string = (STRING); \
4355 entries[n].overlay = (OVERLAY); \
4356 priority = Foverlay_get ((OVERLAY), Qpriority); \
4357 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4358 entries[n].after_string_p = (AFTER_P); \
4359 ++n; \
4360 } \
4361 while (0)
4362
4363 /* Process overlay before the overlay center. */
4364 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4365 {
4366 XSETMISC (overlay, ov);
4367 xassert (OVERLAYP (overlay));
4368 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4369 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4370
4371 if (end < charpos)
4372 break;
4373
4374 /* Skip this overlay if it doesn't start or end at IT's current
4375 position. */
4376 if (end != charpos && start != charpos)
4377 continue;
4378
4379 /* Skip this overlay if it doesn't apply to IT->w. */
4380 window = Foverlay_get (overlay, Qwindow);
4381 if (WINDOWP (window) && XWINDOW (window) != it->w)
4382 continue;
4383
4384 /* If the text ``under'' the overlay is invisible, both before-
4385 and after-strings from this overlay are visible; start and
4386 end position are indistinguishable. */
4387 invisible = Foverlay_get (overlay, Qinvisible);
4388 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4389
4390 /* If overlay has a non-empty before-string, record it. */
4391 if ((start == charpos || (end == charpos && invis_p))
4392 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4393 && SCHARS (str))
4394 RECORD_OVERLAY_STRING (overlay, str, 0);
4395
4396 /* If overlay has a non-empty after-string, record it. */
4397 if ((end == charpos || (start == charpos && invis_p))
4398 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4399 && SCHARS (str))
4400 RECORD_OVERLAY_STRING (overlay, str, 1);
4401 }
4402
4403 /* Process overlays after the overlay center. */
4404 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4405 {
4406 XSETMISC (overlay, ov);
4407 xassert (OVERLAYP (overlay));
4408 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4409 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4410
4411 if (start > charpos)
4412 break;
4413
4414 /* Skip this overlay if it doesn't start or end at IT's current
4415 position. */
4416 if (end != charpos && start != charpos)
4417 continue;
4418
4419 /* Skip this overlay if it doesn't apply to IT->w. */
4420 window = Foverlay_get (overlay, Qwindow);
4421 if (WINDOWP (window) && XWINDOW (window) != it->w)
4422 continue;
4423
4424 /* If the text ``under'' the overlay is invisible, it has a zero
4425 dimension, and both before- and after-strings apply. */
4426 invisible = Foverlay_get (overlay, Qinvisible);
4427 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4428
4429 /* If overlay has a non-empty before-string, record it. */
4430 if ((start == charpos || (end == charpos && invis_p))
4431 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4432 && SCHARS (str))
4433 RECORD_OVERLAY_STRING (overlay, str, 0);
4434
4435 /* If overlay has a non-empty after-string, record it. */
4436 if ((end == charpos || (start == charpos && invis_p))
4437 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4438 && SCHARS (str))
4439 RECORD_OVERLAY_STRING (overlay, str, 1);
4440 }
4441
4442 #undef RECORD_OVERLAY_STRING
4443
4444 /* Sort entries. */
4445 if (n > 1)
4446 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4447
4448 /* Record the total number of strings to process. */
4449 it->n_overlay_strings = n;
4450
4451 /* IT->current.overlay_string_index is the number of overlay strings
4452 that have already been consumed by IT. Copy some of the
4453 remaining overlay strings to IT->overlay_strings. */
4454 i = 0;
4455 j = it->current.overlay_string_index;
4456 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4457 it->overlay_strings[i++] = entries[j++].string;
4458
4459 CHECK_IT (it);
4460 }
4461
4462
4463 /* Get the first chunk of overlay strings at IT's current buffer
4464 position, or at CHARPOS if that is > 0. Value is non-zero if at
4465 least one overlay string was found. */
4466
4467 static int
4468 get_overlay_strings (it, charpos)
4469 struct it *it;
4470 int charpos;
4471 {
4472 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4473 process. This fills IT->overlay_strings with strings, and sets
4474 IT->n_overlay_strings to the total number of strings to process.
4475 IT->pos.overlay_string_index has to be set temporarily to zero
4476 because load_overlay_strings needs this; it must be set to -1
4477 when no overlay strings are found because a zero value would
4478 indicate a position in the first overlay string. */
4479 it->current.overlay_string_index = 0;
4480 load_overlay_strings (it, charpos);
4481
4482 /* If we found overlay strings, set up IT to deliver display
4483 elements from the first one. Otherwise set up IT to deliver
4484 from current_buffer. */
4485 if (it->n_overlay_strings)
4486 {
4487 /* Make sure we know settings in current_buffer, so that we can
4488 restore meaningful values when we're done with the overlay
4489 strings. */
4490 compute_stop_pos (it);
4491 xassert (it->face_id >= 0);
4492
4493 /* Save IT's settings. They are restored after all overlay
4494 strings have been processed. */
4495 xassert (it->sp == 0);
4496 push_it (it);
4497
4498 /* Set up IT to deliver display elements from the first overlay
4499 string. */
4500 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4501 it->string = it->overlay_strings[0];
4502 it->stop_charpos = 0;
4503 xassert (STRINGP (it->string));
4504 it->end_charpos = SCHARS (it->string);
4505 it->multibyte_p = STRING_MULTIBYTE (it->string);
4506 it->method = GET_FROM_STRING;
4507 }
4508 else
4509 {
4510 it->string = Qnil;
4511 it->current.overlay_string_index = -1;
4512 it->method = GET_FROM_BUFFER;
4513 }
4514
4515 CHECK_IT (it);
4516
4517 /* Value is non-zero if we found at least one overlay string. */
4518 return STRINGP (it->string);
4519 }
4520
4521
4522 \f
4523 /***********************************************************************
4524 Saving and restoring state
4525 ***********************************************************************/
4526
4527 /* Save current settings of IT on IT->stack. Called, for example,
4528 before setting up IT for an overlay string, to be able to restore
4529 IT's settings to what they were after the overlay string has been
4530 processed. */
4531
4532 static void
4533 push_it (it)
4534 struct it *it;
4535 {
4536 struct iterator_stack_entry *p;
4537
4538 xassert (it->sp < 2);
4539 p = it->stack + it->sp;
4540
4541 p->stop_charpos = it->stop_charpos;
4542 xassert (it->face_id >= 0);
4543 p->face_id = it->face_id;
4544 p->string = it->string;
4545 p->pos = it->current;
4546 p->end_charpos = it->end_charpos;
4547 p->string_nchars = it->string_nchars;
4548 p->area = it->area;
4549 p->multibyte_p = it->multibyte_p;
4550 p->slice = it->slice;
4551 p->space_width = it->space_width;
4552 p->font_height = it->font_height;
4553 p->voffset = it->voffset;
4554 p->string_from_display_prop_p = it->string_from_display_prop_p;
4555 p->display_ellipsis_p = 0;
4556 ++it->sp;
4557 }
4558
4559
4560 /* Restore IT's settings from IT->stack. Called, for example, when no
4561 more overlay strings must be processed, and we return to delivering
4562 display elements from a buffer, or when the end of a string from a
4563 `display' property is reached and we return to delivering display
4564 elements from an overlay string, or from a buffer. */
4565
4566 static void
4567 pop_it (it)
4568 struct it *it;
4569 {
4570 struct iterator_stack_entry *p;
4571
4572 xassert (it->sp > 0);
4573 --it->sp;
4574 p = it->stack + it->sp;
4575 it->stop_charpos = p->stop_charpos;
4576 it->face_id = p->face_id;
4577 it->string = p->string;
4578 it->current = p->pos;
4579 it->end_charpos = p->end_charpos;
4580 it->string_nchars = p->string_nchars;
4581 it->area = p->area;
4582 it->multibyte_p = p->multibyte_p;
4583 it->slice = p->slice;
4584 it->space_width = p->space_width;
4585 it->font_height = p->font_height;
4586 it->voffset = p->voffset;
4587 it->string_from_display_prop_p = p->string_from_display_prop_p;
4588 }
4589
4590
4591 \f
4592 /***********************************************************************
4593 Moving over lines
4594 ***********************************************************************/
4595
4596 /* Set IT's current position to the previous line start. */
4597
4598 static void
4599 back_to_previous_line_start (it)
4600 struct it *it;
4601 {
4602 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4603 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4604 }
4605
4606
4607 /* Move IT to the next line start.
4608
4609 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4610 we skipped over part of the text (as opposed to moving the iterator
4611 continuously over the text). Otherwise, don't change the value
4612 of *SKIPPED_P.
4613
4614 Newlines may come from buffer text, overlay strings, or strings
4615 displayed via the `display' property. That's the reason we can't
4616 simply use find_next_newline_no_quit.
4617
4618 Note that this function may not skip over invisible text that is so
4619 because of text properties and immediately follows a newline. If
4620 it would, function reseat_at_next_visible_line_start, when called
4621 from set_iterator_to_next, would effectively make invisible
4622 characters following a newline part of the wrong glyph row, which
4623 leads to wrong cursor motion. */
4624
4625 static int
4626 forward_to_next_line_start (it, skipped_p)
4627 struct it *it;
4628 int *skipped_p;
4629 {
4630 int old_selective, newline_found_p, n;
4631 const int MAX_NEWLINE_DISTANCE = 500;
4632
4633 /* If already on a newline, just consume it to avoid unintended
4634 skipping over invisible text below. */
4635 if (it->what == IT_CHARACTER
4636 && it->c == '\n'
4637 && CHARPOS (it->position) == IT_CHARPOS (*it))
4638 {
4639 set_iterator_to_next (it, 0);
4640 it->c = 0;
4641 return 1;
4642 }
4643
4644 /* Don't handle selective display in the following. It's (a)
4645 unnecessary because it's done by the caller, and (b) leads to an
4646 infinite recursion because next_element_from_ellipsis indirectly
4647 calls this function. */
4648 old_selective = it->selective;
4649 it->selective = 0;
4650
4651 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4652 from buffer text. */
4653 for (n = newline_found_p = 0;
4654 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4655 n += STRINGP (it->string) ? 0 : 1)
4656 {
4657 if (!get_next_display_element (it))
4658 return 0;
4659 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4660 set_iterator_to_next (it, 0);
4661 }
4662
4663 /* If we didn't find a newline near enough, see if we can use a
4664 short-cut. */
4665 if (!newline_found_p)
4666 {
4667 int start = IT_CHARPOS (*it);
4668 int limit = find_next_newline_no_quit (start, 1);
4669 Lisp_Object pos;
4670
4671 xassert (!STRINGP (it->string));
4672
4673 /* If there isn't any `display' property in sight, and no
4674 overlays, we can just use the position of the newline in
4675 buffer text. */
4676 if (it->stop_charpos >= limit
4677 || ((pos = Fnext_single_property_change (make_number (start),
4678 Qdisplay,
4679 Qnil, make_number (limit)),
4680 NILP (pos))
4681 && next_overlay_change (start) == ZV))
4682 {
4683 IT_CHARPOS (*it) = limit;
4684 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4685 *skipped_p = newline_found_p = 1;
4686 }
4687 else
4688 {
4689 while (get_next_display_element (it)
4690 && !newline_found_p)
4691 {
4692 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4693 set_iterator_to_next (it, 0);
4694 }
4695 }
4696 }
4697
4698 it->selective = old_selective;
4699 return newline_found_p;
4700 }
4701
4702
4703 /* Set IT's current position to the previous visible line start. Skip
4704 invisible text that is so either due to text properties or due to
4705 selective display. Caution: this does not change IT->current_x and
4706 IT->hpos. */
4707
4708 static void
4709 back_to_previous_visible_line_start (it)
4710 struct it *it;
4711 {
4712 while (IT_CHARPOS (*it) > BEGV)
4713 {
4714 back_to_previous_line_start (it);
4715 if (IT_CHARPOS (*it) <= BEGV)
4716 break;
4717
4718 /* If selective > 0, then lines indented more than that values
4719 are invisible. */
4720 if (it->selective > 0
4721 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4722 (double) it->selective)) /* iftc */
4723 continue;
4724
4725 /* Check the newline before point for invisibility. */
4726 {
4727 Lisp_Object prop;
4728 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4729 Qinvisible, it->window);
4730 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4731 continue;
4732 }
4733
4734 /* If newline has a display property that replaces the newline with something
4735 else (image or text), find start of overlay or interval and continue search
4736 from that point. */
4737 if (IT_CHARPOS (*it) > BEGV)
4738 {
4739 struct it it2 = *it;
4740 int pos;
4741 int beg, end;
4742 Lisp_Object val, overlay;
4743
4744 pos = --IT_CHARPOS (it2);
4745 --IT_BYTEPOS (it2);
4746 it2.sp = 0;
4747 if (handle_display_prop (&it2) == HANDLED_RETURN
4748 && !NILP (val = get_char_property_and_overlay
4749 (make_number (pos), Qdisplay, Qnil, &overlay))
4750 && (OVERLAYP (overlay)
4751 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4752 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4753 {
4754 if (beg < BEGV)
4755 beg = BEGV;
4756 IT_CHARPOS (*it) = beg;
4757 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4758 continue;
4759 }
4760 }
4761
4762 break;
4763 }
4764
4765 xassert (IT_CHARPOS (*it) >= BEGV);
4766 xassert (IT_CHARPOS (*it) == BEGV
4767 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4768 CHECK_IT (it);
4769 }
4770
4771
4772 /* Reseat iterator IT at the previous visible line start. Skip
4773 invisible text that is so either due to text properties or due to
4774 selective display. At the end, update IT's overlay information,
4775 face information etc. */
4776
4777 void
4778 reseat_at_previous_visible_line_start (it)
4779 struct it *it;
4780 {
4781 back_to_previous_visible_line_start (it);
4782 reseat (it, it->current.pos, 1);
4783 CHECK_IT (it);
4784 }
4785
4786
4787 /* Reseat iterator IT on the next visible line start in the current
4788 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4789 preceding the line start. Skip over invisible text that is so
4790 because of selective display. Compute faces, overlays etc at the
4791 new position. Note that this function does not skip over text that
4792 is invisible because of text properties. */
4793
4794 static void
4795 reseat_at_next_visible_line_start (it, on_newline_p)
4796 struct it *it;
4797 int on_newline_p;
4798 {
4799 int newline_found_p, skipped_p = 0;
4800
4801 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4802
4803 /* Skip over lines that are invisible because they are indented
4804 more than the value of IT->selective. */
4805 if (it->selective > 0)
4806 while (IT_CHARPOS (*it) < ZV
4807 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4808 (double) it->selective)) /* iftc */
4809 {
4810 xassert (IT_BYTEPOS (*it) == BEGV
4811 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4812 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4813 }
4814
4815 /* Position on the newline if that's what's requested. */
4816 if (on_newline_p && newline_found_p)
4817 {
4818 if (STRINGP (it->string))
4819 {
4820 if (IT_STRING_CHARPOS (*it) > 0)
4821 {
4822 --IT_STRING_CHARPOS (*it);
4823 --IT_STRING_BYTEPOS (*it);
4824 }
4825 }
4826 else if (IT_CHARPOS (*it) > BEGV)
4827 {
4828 --IT_CHARPOS (*it);
4829 --IT_BYTEPOS (*it);
4830 reseat (it, it->current.pos, 0);
4831 }
4832 }
4833 else if (skipped_p)
4834 reseat (it, it->current.pos, 0);
4835
4836 CHECK_IT (it);
4837 }
4838
4839
4840 \f
4841 /***********************************************************************
4842 Changing an iterator's position
4843 ***********************************************************************/
4844
4845 /* Change IT's current position to POS in current_buffer. If FORCE_P
4846 is non-zero, always check for text properties at the new position.
4847 Otherwise, text properties are only looked up if POS >=
4848 IT->check_charpos of a property. */
4849
4850 static void
4851 reseat (it, pos, force_p)
4852 struct it *it;
4853 struct text_pos pos;
4854 int force_p;
4855 {
4856 int original_pos = IT_CHARPOS (*it);
4857
4858 reseat_1 (it, pos, 0);
4859
4860 /* Determine where to check text properties. Avoid doing it
4861 where possible because text property lookup is very expensive. */
4862 if (force_p
4863 || CHARPOS (pos) > it->stop_charpos
4864 || CHARPOS (pos) < original_pos)
4865 handle_stop (it);
4866
4867 CHECK_IT (it);
4868 }
4869
4870
4871 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4872 IT->stop_pos to POS, also. */
4873
4874 static void
4875 reseat_1 (it, pos, set_stop_p)
4876 struct it *it;
4877 struct text_pos pos;
4878 int set_stop_p;
4879 {
4880 /* Don't call this function when scanning a C string. */
4881 xassert (it->s == NULL);
4882
4883 /* POS must be a reasonable value. */
4884 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4885
4886 it->current.pos = it->position = pos;
4887 XSETBUFFER (it->object, current_buffer);
4888 it->end_charpos = ZV;
4889 it->dpvec = NULL;
4890 it->current.dpvec_index = -1;
4891 it->current.overlay_string_index = -1;
4892 IT_STRING_CHARPOS (*it) = -1;
4893 IT_STRING_BYTEPOS (*it) = -1;
4894 it->string = Qnil;
4895 it->method = GET_FROM_BUFFER;
4896 /* RMS: I added this to fix a bug in move_it_vertically_backward
4897 where it->area continued to relate to the starting point
4898 for the backward motion. Bug report from
4899 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4900 However, I am not sure whether reseat still does the right thing
4901 in general after this change. */
4902 it->area = TEXT_AREA;
4903 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4904 it->sp = 0;
4905 it->face_before_selective_p = 0;
4906
4907 if (set_stop_p)
4908 it->stop_charpos = CHARPOS (pos);
4909 }
4910
4911
4912 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4913 If S is non-null, it is a C string to iterate over. Otherwise,
4914 STRING gives a Lisp string to iterate over.
4915
4916 If PRECISION > 0, don't return more then PRECISION number of
4917 characters from the string.
4918
4919 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4920 characters have been returned. FIELD_WIDTH < 0 means an infinite
4921 field width.
4922
4923 MULTIBYTE = 0 means disable processing of multibyte characters,
4924 MULTIBYTE > 0 means enable it,
4925 MULTIBYTE < 0 means use IT->multibyte_p.
4926
4927 IT must be initialized via a prior call to init_iterator before
4928 calling this function. */
4929
4930 static void
4931 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4932 struct it *it;
4933 unsigned char *s;
4934 Lisp_Object string;
4935 int charpos;
4936 int precision, field_width, multibyte;
4937 {
4938 /* No region in strings. */
4939 it->region_beg_charpos = it->region_end_charpos = -1;
4940
4941 /* No text property checks performed by default, but see below. */
4942 it->stop_charpos = -1;
4943
4944 /* Set iterator position and end position. */
4945 bzero (&it->current, sizeof it->current);
4946 it->current.overlay_string_index = -1;
4947 it->current.dpvec_index = -1;
4948 xassert (charpos >= 0);
4949
4950 /* If STRING is specified, use its multibyteness, otherwise use the
4951 setting of MULTIBYTE, if specified. */
4952 if (multibyte >= 0)
4953 it->multibyte_p = multibyte > 0;
4954
4955 if (s == NULL)
4956 {
4957 xassert (STRINGP (string));
4958 it->string = string;
4959 it->s = NULL;
4960 it->end_charpos = it->string_nchars = SCHARS (string);
4961 it->method = GET_FROM_STRING;
4962 it->current.string_pos = string_pos (charpos, string);
4963 }
4964 else
4965 {
4966 it->s = s;
4967 it->string = Qnil;
4968
4969 /* Note that we use IT->current.pos, not it->current.string_pos,
4970 for displaying C strings. */
4971 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4972 if (it->multibyte_p)
4973 {
4974 it->current.pos = c_string_pos (charpos, s, 1);
4975 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4976 }
4977 else
4978 {
4979 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4980 it->end_charpos = it->string_nchars = strlen (s);
4981 }
4982
4983 it->method = GET_FROM_C_STRING;
4984 }
4985
4986 /* PRECISION > 0 means don't return more than PRECISION characters
4987 from the string. */
4988 if (precision > 0 && it->end_charpos - charpos > precision)
4989 it->end_charpos = it->string_nchars = charpos + precision;
4990
4991 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4992 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4993 FIELD_WIDTH < 0 means infinite field width. This is useful for
4994 padding with `-' at the end of a mode line. */
4995 if (field_width < 0)
4996 field_width = INFINITY;
4997 if (field_width > it->end_charpos - charpos)
4998 it->end_charpos = charpos + field_width;
4999
5000 /* Use the standard display table for displaying strings. */
5001 if (DISP_TABLE_P (Vstandard_display_table))
5002 it->dp = XCHAR_TABLE (Vstandard_display_table);
5003
5004 it->stop_charpos = charpos;
5005 CHECK_IT (it);
5006 }
5007
5008
5009 \f
5010 /***********************************************************************
5011 Iteration
5012 ***********************************************************************/
5013
5014 /* Map enum it_method value to corresponding next_element_from_* function. */
5015
5016 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5017 {
5018 next_element_from_buffer,
5019 next_element_from_display_vector,
5020 next_element_from_composition,
5021 next_element_from_string,
5022 next_element_from_c_string,
5023 next_element_from_image,
5024 next_element_from_stretch
5025 };
5026
5027
5028 /* Load IT's display element fields with information about the next
5029 display element from the current position of IT. Value is zero if
5030 end of buffer (or C string) is reached. */
5031
5032 int
5033 get_next_display_element (it)
5034 struct it *it;
5035 {
5036 /* Non-zero means that we found a display element. Zero means that
5037 we hit the end of what we iterate over. Performance note: the
5038 function pointer `method' used here turns out to be faster than
5039 using a sequence of if-statements. */
5040 int success_p;
5041
5042 get_next:
5043 success_p = (*get_next_element[it->method]) (it);
5044
5045 if (it->what == IT_CHARACTER)
5046 {
5047 /* Map via display table or translate control characters.
5048 IT->c, IT->len etc. have been set to the next character by
5049 the function call above. If we have a display table, and it
5050 contains an entry for IT->c, translate it. Don't do this if
5051 IT->c itself comes from a display table, otherwise we could
5052 end up in an infinite recursion. (An alternative could be to
5053 count the recursion depth of this function and signal an
5054 error when a certain maximum depth is reached.) Is it worth
5055 it? */
5056 if (success_p && it->dpvec == NULL)
5057 {
5058 Lisp_Object dv;
5059
5060 if (it->dp
5061 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5062 VECTORP (dv)))
5063 {
5064 struct Lisp_Vector *v = XVECTOR (dv);
5065
5066 /* Return the first character from the display table
5067 entry, if not empty. If empty, don't display the
5068 current character. */
5069 if (v->size)
5070 {
5071 it->dpvec_char_len = it->len;
5072 it->dpvec = v->contents;
5073 it->dpend = v->contents + v->size;
5074 it->current.dpvec_index = 0;
5075 it->dpvec_face_id = -1;
5076 it->saved_face_id = it->face_id;
5077 it->method = GET_FROM_DISPLAY_VECTOR;
5078 it->ellipsis_p = 0;
5079 }
5080 else
5081 {
5082 set_iterator_to_next (it, 0);
5083 }
5084 goto get_next;
5085 }
5086
5087 /* Translate control characters into `\003' or `^C' form.
5088 Control characters coming from a display table entry are
5089 currently not translated because we use IT->dpvec to hold
5090 the translation. This could easily be changed but I
5091 don't believe that it is worth doing.
5092
5093 If it->multibyte_p is nonzero, eight-bit characters and
5094 non-printable multibyte characters are also translated to
5095 octal form.
5096
5097 If it->multibyte_p is zero, eight-bit characters that
5098 don't have corresponding multibyte char code are also
5099 translated to octal form. */
5100 else if ((it->c < ' '
5101 && (it->area != TEXT_AREA
5102 /* In mode line, treat \n like other crl chars. */
5103 || (it->c != '\t'
5104 && it->glyph_row && it->glyph_row->mode_line_p)
5105 || (it->c != '\n' && it->c != '\t')))
5106 || (it->multibyte_p
5107 ? ((it->c >= 127
5108 && it->len == 1)
5109 || !CHAR_PRINTABLE_P (it->c)
5110 || (!NILP (Vnobreak_char_display)
5111 && (it->c == 0x8a0 || it->c == 0x8ad
5112 || it->c == 0x920 || it->c == 0x92d
5113 || it->c == 0xe20 || it->c == 0xe2d
5114 || it->c == 0xf20 || it->c == 0xf2d)))
5115 : (it->c >= 127
5116 && (!unibyte_display_via_language_environment
5117 || it->c == unibyte_char_to_multibyte (it->c)))))
5118 {
5119 /* IT->c is a control character which must be displayed
5120 either as '\003' or as `^C' where the '\\' and '^'
5121 can be defined in the display table. Fill
5122 IT->ctl_chars with glyphs for what we have to
5123 display. Then, set IT->dpvec to these glyphs. */
5124 GLYPH g;
5125 int ctl_len;
5126 int face_id, lface_id = 0 ;
5127 GLYPH escape_glyph;
5128
5129 /* Handle control characters with ^. */
5130
5131 if (it->c < 128 && it->ctl_arrow_p)
5132 {
5133 g = '^'; /* default glyph for Control */
5134 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5135 if (it->dp
5136 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5137 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5138 {
5139 g = XINT (DISP_CTRL_GLYPH (it->dp));
5140 lface_id = FAST_GLYPH_FACE (g);
5141 }
5142 if (lface_id)
5143 {
5144 g = FAST_GLYPH_CHAR (g);
5145 face_id = merge_faces (it->f, Qt, lface_id,
5146 it->face_id);
5147 }
5148 else
5149 {
5150 /* Merge the escape-glyph face into the current face. */
5151 face_id = merge_faces (it->f, Qescape_glyph, 0,
5152 it->face_id);
5153 }
5154
5155 XSETINT (it->ctl_chars[0], g);
5156 g = it->c ^ 0100;
5157 XSETINT (it->ctl_chars[1], g);
5158 ctl_len = 2;
5159 goto display_control;
5160 }
5161
5162 /* Handle non-break space in the mode where it only gets
5163 highlighting. */
5164
5165 if (EQ (Vnobreak_char_display, Qt)
5166 && (it->c == 0x8a0 || it->c == 0x920
5167 || it->c == 0xe20 || it->c == 0xf20))
5168 {
5169 /* Merge the no-break-space face into the current face. */
5170 face_id = merge_faces (it->f, Qnobreak_space, 0,
5171 it->face_id);
5172
5173 g = it->c = ' ';
5174 XSETINT (it->ctl_chars[0], g);
5175 ctl_len = 1;
5176 goto display_control;
5177 }
5178
5179 /* Handle sequences that start with the "escape glyph". */
5180
5181 /* the default escape glyph is \. */
5182 escape_glyph = '\\';
5183
5184 if (it->dp
5185 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5186 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5187 {
5188 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5189 lface_id = FAST_GLYPH_FACE (escape_glyph);
5190 }
5191 if (lface_id)
5192 {
5193 /* The display table specified a face.
5194 Merge it into face_id and also into escape_glyph. */
5195 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5196 face_id = merge_faces (it->f, Qt, lface_id,
5197 it->face_id);
5198 }
5199 else
5200 {
5201 /* Merge the escape-glyph face into the current face. */
5202 face_id = merge_faces (it->f, Qescape_glyph, 0,
5203 it->face_id);
5204 }
5205
5206 /* Handle soft hyphens in the mode where they only get
5207 highlighting. */
5208
5209 if (EQ (Vnobreak_char_display, Qt)
5210 && (it->c == 0x8ad || it->c == 0x92d
5211 || it->c == 0xe2d || it->c == 0xf2d))
5212 {
5213 g = it->c = '-';
5214 XSETINT (it->ctl_chars[0], g);
5215 ctl_len = 1;
5216 goto display_control;
5217 }
5218
5219 /* Handle non-break space and soft hyphen
5220 with the escape glyph. */
5221
5222 if (it->c == 0x8a0 || it->c == 0x8ad
5223 || it->c == 0x920 || it->c == 0x92d
5224 || it->c == 0xe20 || it->c == 0xe2d
5225 || it->c == 0xf20 || it->c == 0xf2d)
5226 {
5227 XSETINT (it->ctl_chars[0], escape_glyph);
5228 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5229 XSETINT (it->ctl_chars[1], g);
5230 ctl_len = 2;
5231 goto display_control;
5232 }
5233
5234 {
5235 unsigned char str[MAX_MULTIBYTE_LENGTH];
5236 int len;
5237 int i;
5238
5239 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5240 if (SINGLE_BYTE_CHAR_P (it->c))
5241 str[0] = it->c, len = 1;
5242 else
5243 {
5244 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5245 if (len < 0)
5246 {
5247 /* It's an invalid character, which shouldn't
5248 happen actually, but due to bugs it may
5249 happen. Let's print the char as is, there's
5250 not much meaningful we can do with it. */
5251 str[0] = it->c;
5252 str[1] = it->c >> 8;
5253 str[2] = it->c >> 16;
5254 str[3] = it->c >> 24;
5255 len = 4;
5256 }
5257 }
5258
5259 for (i = 0; i < len; i++)
5260 {
5261 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5262 /* Insert three more glyphs into IT->ctl_chars for
5263 the octal display of the character. */
5264 g = ((str[i] >> 6) & 7) + '0';
5265 XSETINT (it->ctl_chars[i * 4 + 1], g);
5266 g = ((str[i] >> 3) & 7) + '0';
5267 XSETINT (it->ctl_chars[i * 4 + 2], g);
5268 g = (str[i] & 7) + '0';
5269 XSETINT (it->ctl_chars[i * 4 + 3], g);
5270 }
5271 ctl_len = len * 4;
5272 }
5273
5274 display_control:
5275 /* Set up IT->dpvec and return first character from it. */
5276 it->dpvec_char_len = it->len;
5277 it->dpvec = it->ctl_chars;
5278 it->dpend = it->dpvec + ctl_len;
5279 it->current.dpvec_index = 0;
5280 it->dpvec_face_id = face_id;
5281 it->saved_face_id = it->face_id;
5282 it->method = GET_FROM_DISPLAY_VECTOR;
5283 it->ellipsis_p = 0;
5284 goto get_next;
5285 }
5286 }
5287
5288 /* Adjust face id for a multibyte character. There are no
5289 multibyte character in unibyte text. */
5290 if (it->multibyte_p
5291 && success_p
5292 && FRAME_WINDOW_P (it->f))
5293 {
5294 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5295 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5296 }
5297 }
5298
5299 /* Is this character the last one of a run of characters with
5300 box? If yes, set IT->end_of_box_run_p to 1. */
5301 if (it->face_box_p
5302 && it->s == NULL)
5303 {
5304 int face_id;
5305 struct face *face;
5306
5307 it->end_of_box_run_p
5308 = ((face_id = face_after_it_pos (it),
5309 face_id != it->face_id)
5310 && (face = FACE_FROM_ID (it->f, face_id),
5311 face->box == FACE_NO_BOX));
5312 }
5313
5314 /* Value is 0 if end of buffer or string reached. */
5315 return success_p;
5316 }
5317
5318
5319 /* Move IT to the next display element.
5320
5321 RESEAT_P non-zero means if called on a newline in buffer text,
5322 skip to the next visible line start.
5323
5324 Functions get_next_display_element and set_iterator_to_next are
5325 separate because I find this arrangement easier to handle than a
5326 get_next_display_element function that also increments IT's
5327 position. The way it is we can first look at an iterator's current
5328 display element, decide whether it fits on a line, and if it does,
5329 increment the iterator position. The other way around we probably
5330 would either need a flag indicating whether the iterator has to be
5331 incremented the next time, or we would have to implement a
5332 decrement position function which would not be easy to write. */
5333
5334 void
5335 set_iterator_to_next (it, reseat_p)
5336 struct it *it;
5337 int reseat_p;
5338 {
5339 /* Reset flags indicating start and end of a sequence of characters
5340 with box. Reset them at the start of this function because
5341 moving the iterator to a new position might set them. */
5342 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5343
5344 switch (it->method)
5345 {
5346 case GET_FROM_BUFFER:
5347 /* The current display element of IT is a character from
5348 current_buffer. Advance in the buffer, and maybe skip over
5349 invisible lines that are so because of selective display. */
5350 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5351 reseat_at_next_visible_line_start (it, 0);
5352 else
5353 {
5354 xassert (it->len != 0);
5355 IT_BYTEPOS (*it) += it->len;
5356 IT_CHARPOS (*it) += 1;
5357 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5358 }
5359 break;
5360
5361 case GET_FROM_COMPOSITION:
5362 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5363 if (STRINGP (it->string))
5364 {
5365 IT_STRING_BYTEPOS (*it) += it->len;
5366 IT_STRING_CHARPOS (*it) += it->cmp_len;
5367 it->method = GET_FROM_STRING;
5368 goto consider_string_end;
5369 }
5370 else
5371 {
5372 IT_BYTEPOS (*it) += it->len;
5373 IT_CHARPOS (*it) += it->cmp_len;
5374 it->method = GET_FROM_BUFFER;
5375 }
5376 break;
5377
5378 case GET_FROM_C_STRING:
5379 /* Current display element of IT is from a C string. */
5380 IT_BYTEPOS (*it) += it->len;
5381 IT_CHARPOS (*it) += 1;
5382 break;
5383
5384 case GET_FROM_DISPLAY_VECTOR:
5385 /* Current display element of IT is from a display table entry.
5386 Advance in the display table definition. Reset it to null if
5387 end reached, and continue with characters from buffers/
5388 strings. */
5389 ++it->current.dpvec_index;
5390
5391 /* Restore face of the iterator to what they were before the
5392 display vector entry (these entries may contain faces). */
5393 it->face_id = it->saved_face_id;
5394
5395 if (it->dpvec + it->current.dpvec_index == it->dpend)
5396 {
5397 if (it->s)
5398 it->method = GET_FROM_C_STRING;
5399 else if (STRINGP (it->string))
5400 it->method = GET_FROM_STRING;
5401 else
5402 it->method = GET_FROM_BUFFER;
5403
5404 it->dpvec = NULL;
5405 it->current.dpvec_index = -1;
5406
5407 /* Skip over characters which were displayed via IT->dpvec. */
5408 if (it->dpvec_char_len < 0)
5409 reseat_at_next_visible_line_start (it, 1);
5410 else if (it->dpvec_char_len > 0)
5411 {
5412 it->len = it->dpvec_char_len;
5413 set_iterator_to_next (it, reseat_p);
5414 }
5415
5416 /* Recheck faces after display vector */
5417 it->stop_charpos = IT_CHARPOS (*it);
5418 }
5419 break;
5420
5421 case GET_FROM_STRING:
5422 /* Current display element is a character from a Lisp string. */
5423 xassert (it->s == NULL && STRINGP (it->string));
5424 IT_STRING_BYTEPOS (*it) += it->len;
5425 IT_STRING_CHARPOS (*it) += 1;
5426
5427 consider_string_end:
5428
5429 if (it->current.overlay_string_index >= 0)
5430 {
5431 /* IT->string is an overlay string. Advance to the
5432 next, if there is one. */
5433 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5434 next_overlay_string (it);
5435 }
5436 else
5437 {
5438 /* IT->string is not an overlay string. If we reached
5439 its end, and there is something on IT->stack, proceed
5440 with what is on the stack. This can be either another
5441 string, this time an overlay string, or a buffer. */
5442 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5443 && it->sp > 0)
5444 {
5445 pop_it (it);
5446 if (STRINGP (it->string))
5447 goto consider_string_end;
5448 it->method = GET_FROM_BUFFER;
5449 }
5450 }
5451 break;
5452
5453 case GET_FROM_IMAGE:
5454 case GET_FROM_STRETCH:
5455 /* The position etc with which we have to proceed are on
5456 the stack. The position may be at the end of a string,
5457 if the `display' property takes up the whole string. */
5458 xassert (it->sp > 0);
5459 pop_it (it);
5460 it->image_id = 0;
5461 if (STRINGP (it->string))
5462 {
5463 it->method = GET_FROM_STRING;
5464 goto consider_string_end;
5465 }
5466 it->method = GET_FROM_BUFFER;
5467 break;
5468
5469 default:
5470 /* There are no other methods defined, so this should be a bug. */
5471 abort ();
5472 }
5473
5474 xassert (it->method != GET_FROM_STRING
5475 || (STRINGP (it->string)
5476 && IT_STRING_CHARPOS (*it) >= 0));
5477 }
5478
5479 /* Load IT's display element fields with information about the next
5480 display element which comes from a display table entry or from the
5481 result of translating a control character to one of the forms `^C'
5482 or `\003'.
5483
5484 IT->dpvec holds the glyphs to return as characters.
5485 IT->saved_face_id holds the face id before the display vector--
5486 it is restored into IT->face_idin set_iterator_to_next. */
5487
5488 static int
5489 next_element_from_display_vector (it)
5490 struct it *it;
5491 {
5492 /* Precondition. */
5493 xassert (it->dpvec && it->current.dpvec_index >= 0);
5494
5495 it->face_id = it->saved_face_id;
5496
5497 if (INTEGERP (*it->dpvec)
5498 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5499 {
5500 GLYPH g;
5501
5502 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5503 it->c = FAST_GLYPH_CHAR (g);
5504 it->len = CHAR_BYTES (it->c);
5505
5506 /* The entry may contain a face id to use. Such a face id is
5507 the id of a Lisp face, not a realized face. A face id of
5508 zero means no face is specified. */
5509 if (it->dpvec_face_id >= 0)
5510 it->face_id = it->dpvec_face_id;
5511 else
5512 {
5513 int lface_id = FAST_GLYPH_FACE (g);
5514 if (lface_id > 0)
5515 it->face_id = merge_faces (it->f, Qt, lface_id,
5516 it->saved_face_id);
5517 }
5518 }
5519 else
5520 /* Display table entry is invalid. Return a space. */
5521 it->c = ' ', it->len = 1;
5522
5523 /* Don't change position and object of the iterator here. They are
5524 still the values of the character that had this display table
5525 entry or was translated, and that's what we want. */
5526 it->what = IT_CHARACTER;
5527 return 1;
5528 }
5529
5530
5531 /* Load IT with the next display element from Lisp string IT->string.
5532 IT->current.string_pos is the current position within the string.
5533 If IT->current.overlay_string_index >= 0, the Lisp string is an
5534 overlay string. */
5535
5536 static int
5537 next_element_from_string (it)
5538 struct it *it;
5539 {
5540 struct text_pos position;
5541
5542 xassert (STRINGP (it->string));
5543 xassert (IT_STRING_CHARPOS (*it) >= 0);
5544 position = it->current.string_pos;
5545
5546 /* Time to check for invisible text? */
5547 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5548 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5549 {
5550 handle_stop (it);
5551
5552 /* Since a handler may have changed IT->method, we must
5553 recurse here. */
5554 return get_next_display_element (it);
5555 }
5556
5557 if (it->current.overlay_string_index >= 0)
5558 {
5559 /* Get the next character from an overlay string. In overlay
5560 strings, There is no field width or padding with spaces to
5561 do. */
5562 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5563 {
5564 it->what = IT_EOB;
5565 return 0;
5566 }
5567 else if (STRING_MULTIBYTE (it->string))
5568 {
5569 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5570 const unsigned char *s = (SDATA (it->string)
5571 + IT_STRING_BYTEPOS (*it));
5572 it->c = string_char_and_length (s, remaining, &it->len);
5573 }
5574 else
5575 {
5576 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5577 it->len = 1;
5578 }
5579 }
5580 else
5581 {
5582 /* Get the next character from a Lisp string that is not an
5583 overlay string. Such strings come from the mode line, for
5584 example. We may have to pad with spaces, or truncate the
5585 string. See also next_element_from_c_string. */
5586 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5587 {
5588 it->what = IT_EOB;
5589 return 0;
5590 }
5591 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5592 {
5593 /* Pad with spaces. */
5594 it->c = ' ', it->len = 1;
5595 CHARPOS (position) = BYTEPOS (position) = -1;
5596 }
5597 else if (STRING_MULTIBYTE (it->string))
5598 {
5599 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5600 const unsigned char *s = (SDATA (it->string)
5601 + IT_STRING_BYTEPOS (*it));
5602 it->c = string_char_and_length (s, maxlen, &it->len);
5603 }
5604 else
5605 {
5606 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5607 it->len = 1;
5608 }
5609 }
5610
5611 /* Record what we have and where it came from. Note that we store a
5612 buffer position in IT->position although it could arguably be a
5613 string position. */
5614 it->what = IT_CHARACTER;
5615 it->object = it->string;
5616 it->position = position;
5617 return 1;
5618 }
5619
5620
5621 /* Load IT with next display element from C string IT->s.
5622 IT->string_nchars is the maximum number of characters to return
5623 from the string. IT->end_charpos may be greater than
5624 IT->string_nchars when this function is called, in which case we
5625 may have to return padding spaces. Value is zero if end of string
5626 reached, including padding spaces. */
5627
5628 static int
5629 next_element_from_c_string (it)
5630 struct it *it;
5631 {
5632 int success_p = 1;
5633
5634 xassert (it->s);
5635 it->what = IT_CHARACTER;
5636 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5637 it->object = Qnil;
5638
5639 /* IT's position can be greater IT->string_nchars in case a field
5640 width or precision has been specified when the iterator was
5641 initialized. */
5642 if (IT_CHARPOS (*it) >= it->end_charpos)
5643 {
5644 /* End of the game. */
5645 it->what = IT_EOB;
5646 success_p = 0;
5647 }
5648 else if (IT_CHARPOS (*it) >= it->string_nchars)
5649 {
5650 /* Pad with spaces. */
5651 it->c = ' ', it->len = 1;
5652 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5653 }
5654 else if (it->multibyte_p)
5655 {
5656 /* Implementation note: The calls to strlen apparently aren't a
5657 performance problem because there is no noticeable performance
5658 difference between Emacs running in unibyte or multibyte mode. */
5659 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5660 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5661 maxlen, &it->len);
5662 }
5663 else
5664 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5665
5666 return success_p;
5667 }
5668
5669
5670 /* Set up IT to return characters from an ellipsis, if appropriate.
5671 The definition of the ellipsis glyphs may come from a display table
5672 entry. This function Fills IT with the first glyph from the
5673 ellipsis if an ellipsis is to be displayed. */
5674
5675 static int
5676 next_element_from_ellipsis (it)
5677 struct it *it;
5678 {
5679 if (it->selective_display_ellipsis_p)
5680 setup_for_ellipsis (it, it->len);
5681 else
5682 {
5683 /* The face at the current position may be different from the
5684 face we find after the invisible text. Remember what it
5685 was in IT->saved_face_id, and signal that it's there by
5686 setting face_before_selective_p. */
5687 it->saved_face_id = it->face_id;
5688 it->method = GET_FROM_BUFFER;
5689 reseat_at_next_visible_line_start (it, 1);
5690 it->face_before_selective_p = 1;
5691 }
5692
5693 return get_next_display_element (it);
5694 }
5695
5696
5697 /* Deliver an image display element. The iterator IT is already
5698 filled with image information (done in handle_display_prop). Value
5699 is always 1. */
5700
5701
5702 static int
5703 next_element_from_image (it)
5704 struct it *it;
5705 {
5706 it->what = IT_IMAGE;
5707 return 1;
5708 }
5709
5710
5711 /* Fill iterator IT with next display element from a stretch glyph
5712 property. IT->object is the value of the text property. Value is
5713 always 1. */
5714
5715 static int
5716 next_element_from_stretch (it)
5717 struct it *it;
5718 {
5719 it->what = IT_STRETCH;
5720 return 1;
5721 }
5722
5723
5724 /* Load IT with the next display element from current_buffer. Value
5725 is zero if end of buffer reached. IT->stop_charpos is the next
5726 position at which to stop and check for text properties or buffer
5727 end. */
5728
5729 static int
5730 next_element_from_buffer (it)
5731 struct it *it;
5732 {
5733 int success_p = 1;
5734
5735 /* Check this assumption, otherwise, we would never enter the
5736 if-statement, below. */
5737 xassert (IT_CHARPOS (*it) >= BEGV
5738 && IT_CHARPOS (*it) <= it->stop_charpos);
5739
5740 if (IT_CHARPOS (*it) >= it->stop_charpos)
5741 {
5742 if (IT_CHARPOS (*it) >= it->end_charpos)
5743 {
5744 int overlay_strings_follow_p;
5745
5746 /* End of the game, except when overlay strings follow that
5747 haven't been returned yet. */
5748 if (it->overlay_strings_at_end_processed_p)
5749 overlay_strings_follow_p = 0;
5750 else
5751 {
5752 it->overlay_strings_at_end_processed_p = 1;
5753 overlay_strings_follow_p = get_overlay_strings (it, 0);
5754 }
5755
5756 if (overlay_strings_follow_p)
5757 success_p = get_next_display_element (it);
5758 else
5759 {
5760 it->what = IT_EOB;
5761 it->position = it->current.pos;
5762 success_p = 0;
5763 }
5764 }
5765 else
5766 {
5767 handle_stop (it);
5768 return get_next_display_element (it);
5769 }
5770 }
5771 else
5772 {
5773 /* No face changes, overlays etc. in sight, so just return a
5774 character from current_buffer. */
5775 unsigned char *p;
5776
5777 /* Maybe run the redisplay end trigger hook. Performance note:
5778 This doesn't seem to cost measurable time. */
5779 if (it->redisplay_end_trigger_charpos
5780 && it->glyph_row
5781 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5782 run_redisplay_end_trigger_hook (it);
5783
5784 /* Get the next character, maybe multibyte. */
5785 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5786 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5787 {
5788 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5789 - IT_BYTEPOS (*it));
5790 it->c = string_char_and_length (p, maxlen, &it->len);
5791 }
5792 else
5793 it->c = *p, it->len = 1;
5794
5795 /* Record what we have and where it came from. */
5796 it->what = IT_CHARACTER;;
5797 it->object = it->w->buffer;
5798 it->position = it->current.pos;
5799
5800 /* Normally we return the character found above, except when we
5801 really want to return an ellipsis for selective display. */
5802 if (it->selective)
5803 {
5804 if (it->c == '\n')
5805 {
5806 /* A value of selective > 0 means hide lines indented more
5807 than that number of columns. */
5808 if (it->selective > 0
5809 && IT_CHARPOS (*it) + 1 < ZV
5810 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5811 IT_BYTEPOS (*it) + 1,
5812 (double) it->selective)) /* iftc */
5813 {
5814 success_p = next_element_from_ellipsis (it);
5815 it->dpvec_char_len = -1;
5816 }
5817 }
5818 else if (it->c == '\r' && it->selective == -1)
5819 {
5820 /* A value of selective == -1 means that everything from the
5821 CR to the end of the line is invisible, with maybe an
5822 ellipsis displayed for it. */
5823 success_p = next_element_from_ellipsis (it);
5824 it->dpvec_char_len = -1;
5825 }
5826 }
5827 }
5828
5829 /* Value is zero if end of buffer reached. */
5830 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5831 return success_p;
5832 }
5833
5834
5835 /* Run the redisplay end trigger hook for IT. */
5836
5837 static void
5838 run_redisplay_end_trigger_hook (it)
5839 struct it *it;
5840 {
5841 Lisp_Object args[3];
5842
5843 /* IT->glyph_row should be non-null, i.e. we should be actually
5844 displaying something, or otherwise we should not run the hook. */
5845 xassert (it->glyph_row);
5846
5847 /* Set up hook arguments. */
5848 args[0] = Qredisplay_end_trigger_functions;
5849 args[1] = it->window;
5850 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5851 it->redisplay_end_trigger_charpos = 0;
5852
5853 /* Since we are *trying* to run these functions, don't try to run
5854 them again, even if they get an error. */
5855 it->w->redisplay_end_trigger = Qnil;
5856 Frun_hook_with_args (3, args);
5857
5858 /* Notice if it changed the face of the character we are on. */
5859 handle_face_prop (it);
5860 }
5861
5862
5863 /* Deliver a composition display element. The iterator IT is already
5864 filled with composition information (done in
5865 handle_composition_prop). Value is always 1. */
5866
5867 static int
5868 next_element_from_composition (it)
5869 struct it *it;
5870 {
5871 it->what = IT_COMPOSITION;
5872 it->position = (STRINGP (it->string)
5873 ? it->current.string_pos
5874 : it->current.pos);
5875 return 1;
5876 }
5877
5878
5879 \f
5880 /***********************************************************************
5881 Moving an iterator without producing glyphs
5882 ***********************************************************************/
5883
5884 /* Check if iterator is at a position corresponding to a valid buffer
5885 position after some move_it_ call. */
5886
5887 #define IT_POS_VALID_AFTER_MOVE_P(it) \
5888 ((it)->method == GET_FROM_STRING \
5889 ? IT_STRING_CHARPOS (*it) == 0 \
5890 : 1)
5891
5892
5893 /* Move iterator IT to a specified buffer or X position within one
5894 line on the display without producing glyphs.
5895
5896 OP should be a bit mask including some or all of these bits:
5897 MOVE_TO_X: Stop on reaching x-position TO_X.
5898 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5899 Regardless of OP's value, stop in reaching the end of the display line.
5900
5901 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5902 This means, in particular, that TO_X includes window's horizontal
5903 scroll amount.
5904
5905 The return value has several possible values that
5906 say what condition caused the scan to stop:
5907
5908 MOVE_POS_MATCH_OR_ZV
5909 - when TO_POS or ZV was reached.
5910
5911 MOVE_X_REACHED
5912 -when TO_X was reached before TO_POS or ZV were reached.
5913
5914 MOVE_LINE_CONTINUED
5915 - when we reached the end of the display area and the line must
5916 be continued.
5917
5918 MOVE_LINE_TRUNCATED
5919 - when we reached the end of the display area and the line is
5920 truncated.
5921
5922 MOVE_NEWLINE_OR_CR
5923 - when we stopped at a line end, i.e. a newline or a CR and selective
5924 display is on. */
5925
5926 static enum move_it_result
5927 move_it_in_display_line_to (it, to_charpos, to_x, op)
5928 struct it *it;
5929 int to_charpos, to_x, op;
5930 {
5931 enum move_it_result result = MOVE_UNDEFINED;
5932 struct glyph_row *saved_glyph_row;
5933
5934 /* Don't produce glyphs in produce_glyphs. */
5935 saved_glyph_row = it->glyph_row;
5936 it->glyph_row = NULL;
5937
5938 #define BUFFER_POS_REACHED_P() \
5939 ((op & MOVE_TO_POS) != 0 \
5940 && BUFFERP (it->object) \
5941 && IT_CHARPOS (*it) >= to_charpos \
5942 && (it->method == GET_FROM_BUFFER \
5943 || (it->method == GET_FROM_DISPLAY_VECTOR \
5944 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5945
5946
5947 while (1)
5948 {
5949 int x, i, ascent = 0, descent = 0;
5950
5951 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5952 if ((op & MOVE_TO_POS) != 0
5953 && BUFFERP (it->object)
5954 && it->method == GET_FROM_BUFFER
5955 && IT_CHARPOS (*it) > to_charpos)
5956 {
5957 result = MOVE_POS_MATCH_OR_ZV;
5958 break;
5959 }
5960
5961 /* Stop when ZV reached.
5962 We used to stop here when TO_CHARPOS reached as well, but that is
5963 too soon if this glyph does not fit on this line. So we handle it
5964 explicitly below. */
5965 if (!get_next_display_element (it)
5966 || (it->truncate_lines_p
5967 && BUFFER_POS_REACHED_P ()))
5968 {
5969 result = MOVE_POS_MATCH_OR_ZV;
5970 break;
5971 }
5972
5973 /* The call to produce_glyphs will get the metrics of the
5974 display element IT is loaded with. We record in x the
5975 x-position before this display element in case it does not
5976 fit on the line. */
5977 x = it->current_x;
5978
5979 /* Remember the line height so far in case the next element doesn't
5980 fit on the line. */
5981 if (!it->truncate_lines_p)
5982 {
5983 ascent = it->max_ascent;
5984 descent = it->max_descent;
5985 }
5986
5987 PRODUCE_GLYPHS (it);
5988
5989 if (it->area != TEXT_AREA)
5990 {
5991 set_iterator_to_next (it, 1);
5992 continue;
5993 }
5994
5995 /* The number of glyphs we get back in IT->nglyphs will normally
5996 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5997 character on a terminal frame, or (iii) a line end. For the
5998 second case, IT->nglyphs - 1 padding glyphs will be present
5999 (on X frames, there is only one glyph produced for a
6000 composite character.
6001
6002 The behavior implemented below means, for continuation lines,
6003 that as many spaces of a TAB as fit on the current line are
6004 displayed there. For terminal frames, as many glyphs of a
6005 multi-glyph character are displayed in the current line, too.
6006 This is what the old redisplay code did, and we keep it that
6007 way. Under X, the whole shape of a complex character must
6008 fit on the line or it will be completely displayed in the
6009 next line.
6010
6011 Note that both for tabs and padding glyphs, all glyphs have
6012 the same width. */
6013 if (it->nglyphs)
6014 {
6015 /* More than one glyph or glyph doesn't fit on line. All
6016 glyphs have the same width. */
6017 int single_glyph_width = it->pixel_width / it->nglyphs;
6018 int new_x;
6019 int x_before_this_char = x;
6020 int hpos_before_this_char = it->hpos;
6021
6022 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6023 {
6024 new_x = x + single_glyph_width;
6025
6026 /* We want to leave anything reaching TO_X to the caller. */
6027 if ((op & MOVE_TO_X) && new_x > to_x)
6028 {
6029 if (BUFFER_POS_REACHED_P ())
6030 goto buffer_pos_reached;
6031 it->current_x = x;
6032 result = MOVE_X_REACHED;
6033 break;
6034 }
6035 else if (/* Lines are continued. */
6036 !it->truncate_lines_p
6037 && (/* And glyph doesn't fit on the line. */
6038 new_x > it->last_visible_x
6039 /* Or it fits exactly and we're on a window
6040 system frame. */
6041 || (new_x == it->last_visible_x
6042 && FRAME_WINDOW_P (it->f))))
6043 {
6044 if (/* IT->hpos == 0 means the very first glyph
6045 doesn't fit on the line, e.g. a wide image. */
6046 it->hpos == 0
6047 || (new_x == it->last_visible_x
6048 && FRAME_WINDOW_P (it->f)))
6049 {
6050 ++it->hpos;
6051 it->current_x = new_x;
6052
6053 /* The character's last glyph just barely fits
6054 in this row. */
6055 if (i == it->nglyphs - 1)
6056 {
6057 /* If this is the destination position,
6058 return a position *before* it in this row,
6059 now that we know it fits in this row. */
6060 if (BUFFER_POS_REACHED_P ())
6061 {
6062 it->hpos = hpos_before_this_char;
6063 it->current_x = x_before_this_char;
6064 result = MOVE_POS_MATCH_OR_ZV;
6065 break;
6066 }
6067
6068 set_iterator_to_next (it, 1);
6069 #ifdef HAVE_WINDOW_SYSTEM
6070 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6071 {
6072 if (!get_next_display_element (it))
6073 {
6074 result = MOVE_POS_MATCH_OR_ZV;
6075 break;
6076 }
6077 if (BUFFER_POS_REACHED_P ())
6078 {
6079 if (ITERATOR_AT_END_OF_LINE_P (it))
6080 result = MOVE_POS_MATCH_OR_ZV;
6081 else
6082 result = MOVE_LINE_CONTINUED;
6083 break;
6084 }
6085 if (ITERATOR_AT_END_OF_LINE_P (it))
6086 {
6087 result = MOVE_NEWLINE_OR_CR;
6088 break;
6089 }
6090 }
6091 #endif /* HAVE_WINDOW_SYSTEM */
6092 }
6093 }
6094 else
6095 {
6096 it->current_x = x;
6097 it->max_ascent = ascent;
6098 it->max_descent = descent;
6099 }
6100
6101 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6102 IT_CHARPOS (*it)));
6103 result = MOVE_LINE_CONTINUED;
6104 break;
6105 }
6106 else if (BUFFER_POS_REACHED_P ())
6107 goto buffer_pos_reached;
6108 else if (new_x > it->first_visible_x)
6109 {
6110 /* Glyph is visible. Increment number of glyphs that
6111 would be displayed. */
6112 ++it->hpos;
6113 }
6114 else
6115 {
6116 /* Glyph is completely off the left margin of the display
6117 area. Nothing to do. */
6118 }
6119 }
6120
6121 if (result != MOVE_UNDEFINED)
6122 break;
6123 }
6124 else if (BUFFER_POS_REACHED_P ())
6125 {
6126 buffer_pos_reached:
6127 it->current_x = x;
6128 it->max_ascent = ascent;
6129 it->max_descent = descent;
6130 result = MOVE_POS_MATCH_OR_ZV;
6131 break;
6132 }
6133 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6134 {
6135 /* Stop when TO_X specified and reached. This check is
6136 necessary here because of lines consisting of a line end,
6137 only. The line end will not produce any glyphs and we
6138 would never get MOVE_X_REACHED. */
6139 xassert (it->nglyphs == 0);
6140 result = MOVE_X_REACHED;
6141 break;
6142 }
6143
6144 /* Is this a line end? If yes, we're done. */
6145 if (ITERATOR_AT_END_OF_LINE_P (it))
6146 {
6147 result = MOVE_NEWLINE_OR_CR;
6148 break;
6149 }
6150
6151 /* The current display element has been consumed. Advance
6152 to the next. */
6153 set_iterator_to_next (it, 1);
6154
6155 /* Stop if lines are truncated and IT's current x-position is
6156 past the right edge of the window now. */
6157 if (it->truncate_lines_p
6158 && it->current_x >= it->last_visible_x)
6159 {
6160 #ifdef HAVE_WINDOW_SYSTEM
6161 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6162 {
6163 if (!get_next_display_element (it)
6164 || BUFFER_POS_REACHED_P ())
6165 {
6166 result = MOVE_POS_MATCH_OR_ZV;
6167 break;
6168 }
6169 if (ITERATOR_AT_END_OF_LINE_P (it))
6170 {
6171 result = MOVE_NEWLINE_OR_CR;
6172 break;
6173 }
6174 }
6175 #endif /* HAVE_WINDOW_SYSTEM */
6176 result = MOVE_LINE_TRUNCATED;
6177 break;
6178 }
6179 }
6180
6181 #undef BUFFER_POS_REACHED_P
6182
6183 /* Restore the iterator settings altered at the beginning of this
6184 function. */
6185 it->glyph_row = saved_glyph_row;
6186 return result;
6187 }
6188
6189
6190 /* Move IT forward until it satisfies one or more of the criteria in
6191 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6192
6193 OP is a bit-mask that specifies where to stop, and in particular,
6194 which of those four position arguments makes a difference. See the
6195 description of enum move_operation_enum.
6196
6197 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6198 screen line, this function will set IT to the next position >
6199 TO_CHARPOS. */
6200
6201 void
6202 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6203 struct it *it;
6204 int to_charpos, to_x, to_y, to_vpos;
6205 int op;
6206 {
6207 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6208 int line_height;
6209 int reached = 0;
6210
6211 for (;;)
6212 {
6213 if (op & MOVE_TO_VPOS)
6214 {
6215 /* If no TO_CHARPOS and no TO_X specified, stop at the
6216 start of the line TO_VPOS. */
6217 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6218 {
6219 if (it->vpos == to_vpos)
6220 {
6221 reached = 1;
6222 break;
6223 }
6224 else
6225 skip = move_it_in_display_line_to (it, -1, -1, 0);
6226 }
6227 else
6228 {
6229 /* TO_VPOS >= 0 means stop at TO_X in the line at
6230 TO_VPOS, or at TO_POS, whichever comes first. */
6231 if (it->vpos == to_vpos)
6232 {
6233 reached = 2;
6234 break;
6235 }
6236
6237 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6238
6239 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6240 {
6241 reached = 3;
6242 break;
6243 }
6244 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6245 {
6246 /* We have reached TO_X but not in the line we want. */
6247 skip = move_it_in_display_line_to (it, to_charpos,
6248 -1, MOVE_TO_POS);
6249 if (skip == MOVE_POS_MATCH_OR_ZV)
6250 {
6251 reached = 4;
6252 break;
6253 }
6254 }
6255 }
6256 }
6257 else if (op & MOVE_TO_Y)
6258 {
6259 struct it it_backup;
6260
6261 /* TO_Y specified means stop at TO_X in the line containing
6262 TO_Y---or at TO_CHARPOS if this is reached first. The
6263 problem is that we can't really tell whether the line
6264 contains TO_Y before we have completely scanned it, and
6265 this may skip past TO_X. What we do is to first scan to
6266 TO_X.
6267
6268 If TO_X is not specified, use a TO_X of zero. The reason
6269 is to make the outcome of this function more predictable.
6270 If we didn't use TO_X == 0, we would stop at the end of
6271 the line which is probably not what a caller would expect
6272 to happen. */
6273 skip = move_it_in_display_line_to (it, to_charpos,
6274 ((op & MOVE_TO_X)
6275 ? to_x : 0),
6276 (MOVE_TO_X
6277 | (op & MOVE_TO_POS)));
6278
6279 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6280 if (skip == MOVE_POS_MATCH_OR_ZV)
6281 {
6282 reached = 5;
6283 break;
6284 }
6285
6286 /* If TO_X was reached, we would like to know whether TO_Y
6287 is in the line. This can only be said if we know the
6288 total line height which requires us to scan the rest of
6289 the line. */
6290 if (skip == MOVE_X_REACHED)
6291 {
6292 it_backup = *it;
6293 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6294 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6295 op & MOVE_TO_POS);
6296 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6297 }
6298
6299 /* Now, decide whether TO_Y is in this line. */
6300 line_height = it->max_ascent + it->max_descent;
6301 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6302
6303 if (to_y >= it->current_y
6304 && to_y < it->current_y + line_height)
6305 {
6306 if (skip == MOVE_X_REACHED)
6307 /* If TO_Y is in this line and TO_X was reached above,
6308 we scanned too far. We have to restore IT's settings
6309 to the ones before skipping. */
6310 *it = it_backup;
6311 reached = 6;
6312 }
6313 else if (skip == MOVE_X_REACHED)
6314 {
6315 skip = skip2;
6316 if (skip == MOVE_POS_MATCH_OR_ZV)
6317 reached = 7;
6318 }
6319
6320 if (reached)
6321 break;
6322 }
6323 else
6324 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6325
6326 switch (skip)
6327 {
6328 case MOVE_POS_MATCH_OR_ZV:
6329 reached = 8;
6330 goto out;
6331
6332 case MOVE_NEWLINE_OR_CR:
6333 set_iterator_to_next (it, 1);
6334 it->continuation_lines_width = 0;
6335 break;
6336
6337 case MOVE_LINE_TRUNCATED:
6338 it->continuation_lines_width = 0;
6339 reseat_at_next_visible_line_start (it, 0);
6340 if ((op & MOVE_TO_POS) != 0
6341 && IT_CHARPOS (*it) > to_charpos)
6342 {
6343 reached = 9;
6344 goto out;
6345 }
6346 break;
6347
6348 case MOVE_LINE_CONTINUED:
6349 it->continuation_lines_width += it->current_x;
6350 break;
6351
6352 default:
6353 abort ();
6354 }
6355
6356 /* Reset/increment for the next run. */
6357 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6358 it->current_x = it->hpos = 0;
6359 it->current_y += it->max_ascent + it->max_descent;
6360 ++it->vpos;
6361 last_height = it->max_ascent + it->max_descent;
6362 last_max_ascent = it->max_ascent;
6363 it->max_ascent = it->max_descent = 0;
6364 }
6365
6366 out:
6367
6368 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6369 }
6370
6371
6372 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6373
6374 If DY > 0, move IT backward at least that many pixels. DY = 0
6375 means move IT backward to the preceding line start or BEGV. This
6376 function may move over more than DY pixels if IT->current_y - DY
6377 ends up in the middle of a line; in this case IT->current_y will be
6378 set to the top of the line moved to. */
6379
6380 void
6381 move_it_vertically_backward (it, dy)
6382 struct it *it;
6383 int dy;
6384 {
6385 int nlines, h;
6386 struct it it2, it3;
6387 int start_pos;
6388
6389 move_further_back:
6390 xassert (dy >= 0);
6391
6392 start_pos = IT_CHARPOS (*it);
6393
6394 /* Estimate how many newlines we must move back. */
6395 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6396
6397 /* Set the iterator's position that many lines back. */
6398 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6399 back_to_previous_visible_line_start (it);
6400
6401 /* Reseat the iterator here. When moving backward, we don't want
6402 reseat to skip forward over invisible text, set up the iterator
6403 to deliver from overlay strings at the new position etc. So,
6404 use reseat_1 here. */
6405 reseat_1 (it, it->current.pos, 1);
6406
6407 /* We are now surely at a line start. */
6408 it->current_x = it->hpos = 0;
6409 it->continuation_lines_width = 0;
6410
6411 /* Move forward and see what y-distance we moved. First move to the
6412 start of the next line so that we get its height. We need this
6413 height to be able to tell whether we reached the specified
6414 y-distance. */
6415 it2 = *it;
6416 it2.max_ascent = it2.max_descent = 0;
6417 do
6418 {
6419 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6420 MOVE_TO_POS | MOVE_TO_VPOS);
6421 }
6422 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6423 xassert (IT_CHARPOS (*it) >= BEGV);
6424 it3 = it2;
6425
6426 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6427 xassert (IT_CHARPOS (*it) >= BEGV);
6428 /* H is the actual vertical distance from the position in *IT
6429 and the starting position. */
6430 h = it2.current_y - it->current_y;
6431 /* NLINES is the distance in number of lines. */
6432 nlines = it2.vpos - it->vpos;
6433
6434 /* Correct IT's y and vpos position
6435 so that they are relative to the starting point. */
6436 it->vpos -= nlines;
6437 it->current_y -= h;
6438
6439 if (dy == 0)
6440 {
6441 /* DY == 0 means move to the start of the screen line. The
6442 value of nlines is > 0 if continuation lines were involved. */
6443 if (nlines > 0)
6444 move_it_by_lines (it, nlines, 1);
6445 #if 0
6446 /* I think this assert is bogus if buffer contains
6447 invisible text or images. KFS. */
6448 xassert (IT_CHARPOS (*it) <= start_pos);
6449 #endif
6450 }
6451 else
6452 {
6453 /* The y-position we try to reach, relative to *IT.
6454 Note that H has been subtracted in front of the if-statement. */
6455 int target_y = it->current_y + h - dy;
6456 int y0 = it3.current_y;
6457 int y1 = line_bottom_y (&it3);
6458 int line_height = y1 - y0;
6459
6460 /* If we did not reach target_y, try to move further backward if
6461 we can. If we moved too far backward, try to move forward. */
6462 if (target_y < it->current_y
6463 /* This is heuristic. In a window that's 3 lines high, with
6464 a line height of 13 pixels each, recentering with point
6465 on the bottom line will try to move -39/2 = 19 pixels
6466 backward. Try to avoid moving into the first line. */
6467 && (it->current_y - target_y
6468 > min (window_box_height (it->w), line_height * 2 / 3))
6469 && IT_CHARPOS (*it) > BEGV)
6470 {
6471 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6472 target_y - it->current_y));
6473 dy = it->current_y - target_y;
6474 goto move_further_back;
6475 }
6476 else if (target_y >= it->current_y + line_height
6477 && IT_CHARPOS (*it) < ZV)
6478 {
6479 /* Should move forward by at least one line, maybe more.
6480
6481 Note: Calling move_it_by_lines can be expensive on
6482 terminal frames, where compute_motion is used (via
6483 vmotion) to do the job, when there are very long lines
6484 and truncate-lines is nil. That's the reason for
6485 treating terminal frames specially here. */
6486
6487 if (!FRAME_WINDOW_P (it->f))
6488 move_it_vertically (it, target_y - (it->current_y + line_height));
6489 else
6490 {
6491 do
6492 {
6493 move_it_by_lines (it, 1, 1);
6494 }
6495 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6496 }
6497
6498 #if 0
6499 /* I think this assert is bogus if buffer contains
6500 invisible text or images. KFS. */
6501 xassert (IT_CHARPOS (*it) >= BEGV);
6502 #endif
6503 }
6504 }
6505 }
6506
6507
6508 /* Move IT by a specified amount of pixel lines DY. DY negative means
6509 move backwards. DY = 0 means move to start of screen line. At the
6510 end, IT will be on the start of a screen line. */
6511
6512 void
6513 move_it_vertically (it, dy)
6514 struct it *it;
6515 int dy;
6516 {
6517 if (dy <= 0)
6518 move_it_vertically_backward (it, -dy);
6519 else
6520 {
6521 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6522 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6523 MOVE_TO_POS | MOVE_TO_Y);
6524 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6525
6526 /* If buffer ends in ZV without a newline, move to the start of
6527 the line to satisfy the post-condition. */
6528 if (IT_CHARPOS (*it) == ZV
6529 && ZV > BEGV
6530 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6531 move_it_by_lines (it, 0, 0);
6532 }
6533 }
6534
6535
6536 /* Move iterator IT past the end of the text line it is in. */
6537
6538 void
6539 move_it_past_eol (it)
6540 struct it *it;
6541 {
6542 enum move_it_result rc;
6543
6544 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6545 if (rc == MOVE_NEWLINE_OR_CR)
6546 set_iterator_to_next (it, 0);
6547 }
6548
6549
6550 #if 0 /* Currently not used. */
6551
6552 /* Return non-zero if some text between buffer positions START_CHARPOS
6553 and END_CHARPOS is invisible. IT->window is the window for text
6554 property lookup. */
6555
6556 static int
6557 invisible_text_between_p (it, start_charpos, end_charpos)
6558 struct it *it;
6559 int start_charpos, end_charpos;
6560 {
6561 Lisp_Object prop, limit;
6562 int invisible_found_p;
6563
6564 xassert (it != NULL && start_charpos <= end_charpos);
6565
6566 /* Is text at START invisible? */
6567 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6568 it->window);
6569 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6570 invisible_found_p = 1;
6571 else
6572 {
6573 limit = Fnext_single_char_property_change (make_number (start_charpos),
6574 Qinvisible, Qnil,
6575 make_number (end_charpos));
6576 invisible_found_p = XFASTINT (limit) < end_charpos;
6577 }
6578
6579 return invisible_found_p;
6580 }
6581
6582 #endif /* 0 */
6583
6584
6585 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6586 negative means move up. DVPOS == 0 means move to the start of the
6587 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6588 NEED_Y_P is zero, IT->current_y will be left unchanged.
6589
6590 Further optimization ideas: If we would know that IT->f doesn't use
6591 a face with proportional font, we could be faster for
6592 truncate-lines nil. */
6593
6594 void
6595 move_it_by_lines (it, dvpos, need_y_p)
6596 struct it *it;
6597 int dvpos, need_y_p;
6598 {
6599 struct position pos;
6600
6601 if (!FRAME_WINDOW_P (it->f))
6602 {
6603 struct text_pos textpos;
6604
6605 /* We can use vmotion on frames without proportional fonts. */
6606 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6607 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6608 reseat (it, textpos, 1);
6609 it->vpos += pos.vpos;
6610 it->current_y += pos.vpos;
6611 }
6612 else if (dvpos == 0)
6613 {
6614 /* DVPOS == 0 means move to the start of the screen line. */
6615 move_it_vertically_backward (it, 0);
6616 xassert (it->current_x == 0 && it->hpos == 0);
6617 /* Let next call to line_bottom_y calculate real line height */
6618 last_height = 0;
6619 }
6620 else if (dvpos > 0)
6621 {
6622 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6623 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6624 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6625 }
6626 else
6627 {
6628 struct it it2;
6629 int start_charpos, i;
6630
6631 /* Start at the beginning of the screen line containing IT's
6632 position. This may actually move vertically backwards,
6633 in case of overlays, so adjust dvpos accordingly. */
6634 dvpos += it->vpos;
6635 move_it_vertically_backward (it, 0);
6636 dvpos -= it->vpos;
6637
6638 /* Go back -DVPOS visible lines and reseat the iterator there. */
6639 start_charpos = IT_CHARPOS (*it);
6640 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6641 back_to_previous_visible_line_start (it);
6642 reseat (it, it->current.pos, 1);
6643
6644 /* Move further back if we end up in a string or an image. */
6645 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6646 {
6647 /* First try to move to start of display line. */
6648 dvpos += it->vpos;
6649 move_it_vertically_backward (it, 0);
6650 dvpos -= it->vpos;
6651 if (IT_POS_VALID_AFTER_MOVE_P (it))
6652 break;
6653 /* If start of line is still in string or image,
6654 move further back. */
6655 back_to_previous_visible_line_start (it);
6656 reseat (it, it->current.pos, 1);
6657 dvpos--;
6658 }
6659
6660 it->current_x = it->hpos = 0;
6661
6662 /* Above call may have moved too far if continuation lines
6663 are involved. Scan forward and see if it did. */
6664 it2 = *it;
6665 it2.vpos = it2.current_y = 0;
6666 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6667 it->vpos -= it2.vpos;
6668 it->current_y -= it2.current_y;
6669 it->current_x = it->hpos = 0;
6670
6671 /* If we moved too far back, move IT some lines forward. */
6672 if (it2.vpos > -dvpos)
6673 {
6674 int delta = it2.vpos + dvpos;
6675 it2 = *it;
6676 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6677 /* Move back again if we got too far ahead. */
6678 if (IT_CHARPOS (*it) >= start_charpos)
6679 *it = it2;
6680 }
6681 }
6682 }
6683
6684 /* Return 1 if IT points into the middle of a display vector. */
6685
6686 int
6687 in_display_vector_p (it)
6688 struct it *it;
6689 {
6690 return (it->method == GET_FROM_DISPLAY_VECTOR
6691 && it->current.dpvec_index > 0
6692 && it->dpvec + it->current.dpvec_index != it->dpend);
6693 }
6694
6695 \f
6696 /***********************************************************************
6697 Messages
6698 ***********************************************************************/
6699
6700
6701 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6702 to *Messages*. */
6703
6704 void
6705 add_to_log (format, arg1, arg2)
6706 char *format;
6707 Lisp_Object arg1, arg2;
6708 {
6709 Lisp_Object args[3];
6710 Lisp_Object msg, fmt;
6711 char *buffer;
6712 int len;
6713 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6714 USE_SAFE_ALLOCA;
6715
6716 /* Do nothing if called asynchronously. Inserting text into
6717 a buffer may call after-change-functions and alike and
6718 that would means running Lisp asynchronously. */
6719 if (handling_signal)
6720 return;
6721
6722 fmt = msg = Qnil;
6723 GCPRO4 (fmt, msg, arg1, arg2);
6724
6725 args[0] = fmt = build_string (format);
6726 args[1] = arg1;
6727 args[2] = arg2;
6728 msg = Fformat (3, args);
6729
6730 len = SBYTES (msg) + 1;
6731 SAFE_ALLOCA (buffer, char *, len);
6732 bcopy (SDATA (msg), buffer, len);
6733
6734 message_dolog (buffer, len - 1, 1, 0);
6735 SAFE_FREE ();
6736
6737 UNGCPRO;
6738 }
6739
6740
6741 /* Output a newline in the *Messages* buffer if "needs" one. */
6742
6743 void
6744 message_log_maybe_newline ()
6745 {
6746 if (message_log_need_newline)
6747 message_dolog ("", 0, 1, 0);
6748 }
6749
6750
6751 /* Add a string M of length NBYTES to the message log, optionally
6752 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6753 nonzero, means interpret the contents of M as multibyte. This
6754 function calls low-level routines in order to bypass text property
6755 hooks, etc. which might not be safe to run. */
6756
6757 void
6758 message_dolog (m, nbytes, nlflag, multibyte)
6759 const char *m;
6760 int nbytes, nlflag, multibyte;
6761 {
6762 if (!NILP (Vmemory_full))
6763 return;
6764
6765 if (!NILP (Vmessage_log_max))
6766 {
6767 struct buffer *oldbuf;
6768 Lisp_Object oldpoint, oldbegv, oldzv;
6769 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6770 int point_at_end = 0;
6771 int zv_at_end = 0;
6772 Lisp_Object old_deactivate_mark, tem;
6773 struct gcpro gcpro1;
6774
6775 old_deactivate_mark = Vdeactivate_mark;
6776 oldbuf = current_buffer;
6777 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6778 current_buffer->undo_list = Qt;
6779
6780 oldpoint = message_dolog_marker1;
6781 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6782 oldbegv = message_dolog_marker2;
6783 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6784 oldzv = message_dolog_marker3;
6785 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6786 GCPRO1 (old_deactivate_mark);
6787
6788 if (PT == Z)
6789 point_at_end = 1;
6790 if (ZV == Z)
6791 zv_at_end = 1;
6792
6793 BEGV = BEG;
6794 BEGV_BYTE = BEG_BYTE;
6795 ZV = Z;
6796 ZV_BYTE = Z_BYTE;
6797 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6798
6799 /* Insert the string--maybe converting multibyte to single byte
6800 or vice versa, so that all the text fits the buffer. */
6801 if (multibyte
6802 && NILP (current_buffer->enable_multibyte_characters))
6803 {
6804 int i, c, char_bytes;
6805 unsigned char work[1];
6806
6807 /* Convert a multibyte string to single-byte
6808 for the *Message* buffer. */
6809 for (i = 0; i < nbytes; i += char_bytes)
6810 {
6811 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6812 work[0] = (SINGLE_BYTE_CHAR_P (c)
6813 ? c
6814 : multibyte_char_to_unibyte (c, Qnil));
6815 insert_1_both (work, 1, 1, 1, 0, 0);
6816 }
6817 }
6818 else if (! multibyte
6819 && ! NILP (current_buffer->enable_multibyte_characters))
6820 {
6821 int i, c, char_bytes;
6822 unsigned char *msg = (unsigned char *) m;
6823 unsigned char str[MAX_MULTIBYTE_LENGTH];
6824 /* Convert a single-byte string to multibyte
6825 for the *Message* buffer. */
6826 for (i = 0; i < nbytes; i++)
6827 {
6828 c = unibyte_char_to_multibyte (msg[i]);
6829 char_bytes = CHAR_STRING (c, str);
6830 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6831 }
6832 }
6833 else if (nbytes)
6834 insert_1 (m, nbytes, 1, 0, 0);
6835
6836 if (nlflag)
6837 {
6838 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6839 insert_1 ("\n", 1, 1, 0, 0);
6840
6841 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6842 this_bol = PT;
6843 this_bol_byte = PT_BYTE;
6844
6845 /* See if this line duplicates the previous one.
6846 If so, combine duplicates. */
6847 if (this_bol > BEG)
6848 {
6849 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6850 prev_bol = PT;
6851 prev_bol_byte = PT_BYTE;
6852
6853 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6854 this_bol, this_bol_byte);
6855 if (dup)
6856 {
6857 del_range_both (prev_bol, prev_bol_byte,
6858 this_bol, this_bol_byte, 0);
6859 if (dup > 1)
6860 {
6861 char dupstr[40];
6862 int duplen;
6863
6864 /* If you change this format, don't forget to also
6865 change message_log_check_duplicate. */
6866 sprintf (dupstr, " [%d times]", dup);
6867 duplen = strlen (dupstr);
6868 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6869 insert_1 (dupstr, duplen, 1, 0, 1);
6870 }
6871 }
6872 }
6873
6874 /* If we have more than the desired maximum number of lines
6875 in the *Messages* buffer now, delete the oldest ones.
6876 This is safe because we don't have undo in this buffer. */
6877
6878 if (NATNUMP (Vmessage_log_max))
6879 {
6880 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6881 -XFASTINT (Vmessage_log_max) - 1, 0);
6882 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6883 }
6884 }
6885 BEGV = XMARKER (oldbegv)->charpos;
6886 BEGV_BYTE = marker_byte_position (oldbegv);
6887
6888 if (zv_at_end)
6889 {
6890 ZV = Z;
6891 ZV_BYTE = Z_BYTE;
6892 }
6893 else
6894 {
6895 ZV = XMARKER (oldzv)->charpos;
6896 ZV_BYTE = marker_byte_position (oldzv);
6897 }
6898
6899 if (point_at_end)
6900 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6901 else
6902 /* We can't do Fgoto_char (oldpoint) because it will run some
6903 Lisp code. */
6904 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6905 XMARKER (oldpoint)->bytepos);
6906
6907 UNGCPRO;
6908 unchain_marker (XMARKER (oldpoint));
6909 unchain_marker (XMARKER (oldbegv));
6910 unchain_marker (XMARKER (oldzv));
6911
6912 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6913 set_buffer_internal (oldbuf);
6914 if (NILP (tem))
6915 windows_or_buffers_changed = old_windows_or_buffers_changed;
6916 message_log_need_newline = !nlflag;
6917 Vdeactivate_mark = old_deactivate_mark;
6918 }
6919 }
6920
6921
6922 /* We are at the end of the buffer after just having inserted a newline.
6923 (Note: We depend on the fact we won't be crossing the gap.)
6924 Check to see if the most recent message looks a lot like the previous one.
6925 Return 0 if different, 1 if the new one should just replace it, or a
6926 value N > 1 if we should also append " [N times]". */
6927
6928 static int
6929 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6930 int prev_bol, this_bol;
6931 int prev_bol_byte, this_bol_byte;
6932 {
6933 int i;
6934 int len = Z_BYTE - 1 - this_bol_byte;
6935 int seen_dots = 0;
6936 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6937 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6938
6939 for (i = 0; i < len; i++)
6940 {
6941 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6942 seen_dots = 1;
6943 if (p1[i] != p2[i])
6944 return seen_dots;
6945 }
6946 p1 += len;
6947 if (*p1 == '\n')
6948 return 2;
6949 if (*p1++ == ' ' && *p1++ == '[')
6950 {
6951 int n = 0;
6952 while (*p1 >= '0' && *p1 <= '9')
6953 n = n * 10 + *p1++ - '0';
6954 if (strncmp (p1, " times]\n", 8) == 0)
6955 return n+1;
6956 }
6957 return 0;
6958 }
6959 \f
6960
6961 /* Display an echo area message M with a specified length of NBYTES
6962 bytes. The string may include null characters. If M is 0, clear
6963 out any existing message, and let the mini-buffer text show
6964 through.
6965
6966 The buffer M must continue to exist until after the echo area gets
6967 cleared or some other message gets displayed there. This means do
6968 not pass text that is stored in a Lisp string; do not pass text in
6969 a buffer that was alloca'd. */
6970
6971 void
6972 message2 (m, nbytes, multibyte)
6973 const char *m;
6974 int nbytes;
6975 int multibyte;
6976 {
6977 /* First flush out any partial line written with print. */
6978 message_log_maybe_newline ();
6979 if (m)
6980 message_dolog (m, nbytes, 1, multibyte);
6981 message2_nolog (m, nbytes, multibyte);
6982 }
6983
6984
6985 /* The non-logging counterpart of message2. */
6986
6987 void
6988 message2_nolog (m, nbytes, multibyte)
6989 const char *m;
6990 int nbytes, multibyte;
6991 {
6992 struct frame *sf = SELECTED_FRAME ();
6993 message_enable_multibyte = multibyte;
6994
6995 if (noninteractive)
6996 {
6997 if (noninteractive_need_newline)
6998 putc ('\n', stderr);
6999 noninteractive_need_newline = 0;
7000 if (m)
7001 fwrite (m, nbytes, 1, stderr);
7002 if (cursor_in_echo_area == 0)
7003 fprintf (stderr, "\n");
7004 fflush (stderr);
7005 }
7006 /* A null message buffer means that the frame hasn't really been
7007 initialized yet. Error messages get reported properly by
7008 cmd_error, so this must be just an informative message; toss it. */
7009 else if (INTERACTIVE
7010 && sf->glyphs_initialized_p
7011 && FRAME_MESSAGE_BUF (sf))
7012 {
7013 Lisp_Object mini_window;
7014 struct frame *f;
7015
7016 /* Get the frame containing the mini-buffer
7017 that the selected frame is using. */
7018 mini_window = FRAME_MINIBUF_WINDOW (sf);
7019 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7020
7021 FRAME_SAMPLE_VISIBILITY (f);
7022 if (FRAME_VISIBLE_P (sf)
7023 && ! FRAME_VISIBLE_P (f))
7024 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7025
7026 if (m)
7027 {
7028 set_message (m, Qnil, nbytes, multibyte);
7029 if (minibuffer_auto_raise)
7030 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7031 }
7032 else
7033 clear_message (1, 1);
7034
7035 do_pending_window_change (0);
7036 echo_area_display (1);
7037 do_pending_window_change (0);
7038 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7039 (*frame_up_to_date_hook) (f);
7040 }
7041 }
7042
7043
7044 /* Display an echo area message M with a specified length of NBYTES
7045 bytes. The string may include null characters. If M is not a
7046 string, clear out any existing message, and let the mini-buffer
7047 text show through.
7048
7049 This function cancels echoing. */
7050
7051 void
7052 message3 (m, nbytes, multibyte)
7053 Lisp_Object m;
7054 int nbytes;
7055 int multibyte;
7056 {
7057 struct gcpro gcpro1;
7058
7059 GCPRO1 (m);
7060 clear_message (1,1);
7061 cancel_echoing ();
7062
7063 /* First flush out any partial line written with print. */
7064 message_log_maybe_newline ();
7065 if (STRINGP (m))
7066 {
7067 char *buffer;
7068 USE_SAFE_ALLOCA;
7069
7070 SAFE_ALLOCA (buffer, char *, nbytes);
7071 bcopy (SDATA (m), buffer, nbytes);
7072 message_dolog (buffer, nbytes, 1, multibyte);
7073 SAFE_FREE ();
7074 }
7075 message3_nolog (m, nbytes, multibyte);
7076
7077 UNGCPRO;
7078 }
7079
7080
7081 /* The non-logging version of message3.
7082 This does not cancel echoing, because it is used for echoing.
7083 Perhaps we need to make a separate function for echoing
7084 and make this cancel echoing. */
7085
7086 void
7087 message3_nolog (m, nbytes, multibyte)
7088 Lisp_Object m;
7089 int nbytes, multibyte;
7090 {
7091 struct frame *sf = SELECTED_FRAME ();
7092 message_enable_multibyte = multibyte;
7093
7094 if (noninteractive)
7095 {
7096 if (noninteractive_need_newline)
7097 putc ('\n', stderr);
7098 noninteractive_need_newline = 0;
7099 if (STRINGP (m))
7100 fwrite (SDATA (m), nbytes, 1, stderr);
7101 if (cursor_in_echo_area == 0)
7102 fprintf (stderr, "\n");
7103 fflush (stderr);
7104 }
7105 /* A null message buffer means that the frame hasn't really been
7106 initialized yet. Error messages get reported properly by
7107 cmd_error, so this must be just an informative message; toss it. */
7108 else if (INTERACTIVE
7109 && sf->glyphs_initialized_p
7110 && FRAME_MESSAGE_BUF (sf))
7111 {
7112 Lisp_Object mini_window;
7113 Lisp_Object frame;
7114 struct frame *f;
7115
7116 /* Get the frame containing the mini-buffer
7117 that the selected frame is using. */
7118 mini_window = FRAME_MINIBUF_WINDOW (sf);
7119 frame = XWINDOW (mini_window)->frame;
7120 f = XFRAME (frame);
7121
7122 FRAME_SAMPLE_VISIBILITY (f);
7123 if (FRAME_VISIBLE_P (sf)
7124 && !FRAME_VISIBLE_P (f))
7125 Fmake_frame_visible (frame);
7126
7127 if (STRINGP (m) && SCHARS (m) > 0)
7128 {
7129 set_message (NULL, m, nbytes, multibyte);
7130 if (minibuffer_auto_raise)
7131 Fraise_frame (frame);
7132 /* Assume we are not echoing.
7133 (If we are, echo_now will override this.) */
7134 echo_message_buffer = Qnil;
7135 }
7136 else
7137 clear_message (1, 1);
7138
7139 do_pending_window_change (0);
7140 echo_area_display (1);
7141 do_pending_window_change (0);
7142 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7143 (*frame_up_to_date_hook) (f);
7144 }
7145 }
7146
7147
7148 /* Display a null-terminated echo area message M. If M is 0, clear
7149 out any existing message, and let the mini-buffer text show through.
7150
7151 The buffer M must continue to exist until after the echo area gets
7152 cleared or some other message gets displayed there. Do not pass
7153 text that is stored in a Lisp string. Do not pass text in a buffer
7154 that was alloca'd. */
7155
7156 void
7157 message1 (m)
7158 char *m;
7159 {
7160 message2 (m, (m ? strlen (m) : 0), 0);
7161 }
7162
7163
7164 /* The non-logging counterpart of message1. */
7165
7166 void
7167 message1_nolog (m)
7168 char *m;
7169 {
7170 message2_nolog (m, (m ? strlen (m) : 0), 0);
7171 }
7172
7173 /* Display a message M which contains a single %s
7174 which gets replaced with STRING. */
7175
7176 void
7177 message_with_string (m, string, log)
7178 char *m;
7179 Lisp_Object string;
7180 int log;
7181 {
7182 CHECK_STRING (string);
7183
7184 if (noninteractive)
7185 {
7186 if (m)
7187 {
7188 if (noninteractive_need_newline)
7189 putc ('\n', stderr);
7190 noninteractive_need_newline = 0;
7191 fprintf (stderr, m, SDATA (string));
7192 if (cursor_in_echo_area == 0)
7193 fprintf (stderr, "\n");
7194 fflush (stderr);
7195 }
7196 }
7197 else if (INTERACTIVE)
7198 {
7199 /* The frame whose minibuffer we're going to display the message on.
7200 It may be larger than the selected frame, so we need
7201 to use its buffer, not the selected frame's buffer. */
7202 Lisp_Object mini_window;
7203 struct frame *f, *sf = SELECTED_FRAME ();
7204
7205 /* Get the frame containing the minibuffer
7206 that the selected frame is using. */
7207 mini_window = FRAME_MINIBUF_WINDOW (sf);
7208 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7209
7210 /* A null message buffer means that the frame hasn't really been
7211 initialized yet. Error messages get reported properly by
7212 cmd_error, so this must be just an informative message; toss it. */
7213 if (FRAME_MESSAGE_BUF (f))
7214 {
7215 Lisp_Object args[2], message;
7216 struct gcpro gcpro1, gcpro2;
7217
7218 args[0] = build_string (m);
7219 args[1] = message = string;
7220 GCPRO2 (args[0], message);
7221 gcpro1.nvars = 2;
7222
7223 message = Fformat (2, args);
7224
7225 if (log)
7226 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7227 else
7228 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7229
7230 UNGCPRO;
7231
7232 /* Print should start at the beginning of the message
7233 buffer next time. */
7234 message_buf_print = 0;
7235 }
7236 }
7237 }
7238
7239
7240 /* Dump an informative message to the minibuf. If M is 0, clear out
7241 any existing message, and let the mini-buffer text show through. */
7242
7243 /* VARARGS 1 */
7244 void
7245 message (m, a1, a2, a3)
7246 char *m;
7247 EMACS_INT a1, a2, a3;
7248 {
7249 if (noninteractive)
7250 {
7251 if (m)
7252 {
7253 if (noninteractive_need_newline)
7254 putc ('\n', stderr);
7255 noninteractive_need_newline = 0;
7256 fprintf (stderr, m, a1, a2, a3);
7257 if (cursor_in_echo_area == 0)
7258 fprintf (stderr, "\n");
7259 fflush (stderr);
7260 }
7261 }
7262 else if (INTERACTIVE)
7263 {
7264 /* The frame whose mini-buffer we're going to display the message
7265 on. It may be larger than the selected frame, so we need to
7266 use its buffer, not the selected frame's buffer. */
7267 Lisp_Object mini_window;
7268 struct frame *f, *sf = SELECTED_FRAME ();
7269
7270 /* Get the frame containing the mini-buffer
7271 that the selected frame is using. */
7272 mini_window = FRAME_MINIBUF_WINDOW (sf);
7273 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7274
7275 /* A null message buffer means that the frame hasn't really been
7276 initialized yet. Error messages get reported properly by
7277 cmd_error, so this must be just an informative message; toss
7278 it. */
7279 if (FRAME_MESSAGE_BUF (f))
7280 {
7281 if (m)
7282 {
7283 int len;
7284 #ifdef NO_ARG_ARRAY
7285 char *a[3];
7286 a[0] = (char *) a1;
7287 a[1] = (char *) a2;
7288 a[2] = (char *) a3;
7289
7290 len = doprnt (FRAME_MESSAGE_BUF (f),
7291 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7292 #else
7293 len = doprnt (FRAME_MESSAGE_BUF (f),
7294 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7295 (char **) &a1);
7296 #endif /* NO_ARG_ARRAY */
7297
7298 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7299 }
7300 else
7301 message1 (0);
7302
7303 /* Print should start at the beginning of the message
7304 buffer next time. */
7305 message_buf_print = 0;
7306 }
7307 }
7308 }
7309
7310
7311 /* The non-logging version of message. */
7312
7313 void
7314 message_nolog (m, a1, a2, a3)
7315 char *m;
7316 EMACS_INT a1, a2, a3;
7317 {
7318 Lisp_Object old_log_max;
7319 old_log_max = Vmessage_log_max;
7320 Vmessage_log_max = Qnil;
7321 message (m, a1, a2, a3);
7322 Vmessage_log_max = old_log_max;
7323 }
7324
7325
7326 /* Display the current message in the current mini-buffer. This is
7327 only called from error handlers in process.c, and is not time
7328 critical. */
7329
7330 void
7331 update_echo_area ()
7332 {
7333 if (!NILP (echo_area_buffer[0]))
7334 {
7335 Lisp_Object string;
7336 string = Fcurrent_message ();
7337 message3 (string, SBYTES (string),
7338 !NILP (current_buffer->enable_multibyte_characters));
7339 }
7340 }
7341
7342
7343 /* Make sure echo area buffers in `echo_buffers' are live.
7344 If they aren't, make new ones. */
7345
7346 static void
7347 ensure_echo_area_buffers ()
7348 {
7349 int i;
7350
7351 for (i = 0; i < 2; ++i)
7352 if (!BUFFERP (echo_buffer[i])
7353 || NILP (XBUFFER (echo_buffer[i])->name))
7354 {
7355 char name[30];
7356 Lisp_Object old_buffer;
7357 int j;
7358
7359 old_buffer = echo_buffer[i];
7360 sprintf (name, " *Echo Area %d*", i);
7361 echo_buffer[i] = Fget_buffer_create (build_string (name));
7362 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7363
7364 for (j = 0; j < 2; ++j)
7365 if (EQ (old_buffer, echo_area_buffer[j]))
7366 echo_area_buffer[j] = echo_buffer[i];
7367 }
7368 }
7369
7370
7371 /* Call FN with args A1..A4 with either the current or last displayed
7372 echo_area_buffer as current buffer.
7373
7374 WHICH zero means use the current message buffer
7375 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7376 from echo_buffer[] and clear it.
7377
7378 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7379 suitable buffer from echo_buffer[] and clear it.
7380
7381 Value is what FN returns. */
7382
7383 static int
7384 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7385 struct window *w;
7386 int which;
7387 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7388 EMACS_INT a1;
7389 Lisp_Object a2;
7390 EMACS_INT a3, a4;
7391 {
7392 Lisp_Object buffer;
7393 int this_one, the_other, clear_buffer_p, rc;
7394 int count = SPECPDL_INDEX ();
7395
7396 /* If buffers aren't live, make new ones. */
7397 ensure_echo_area_buffers ();
7398
7399 clear_buffer_p = 0;
7400
7401 if (which == 0)
7402 this_one = 0, the_other = 1;
7403 else if (which > 0)
7404 this_one = 1, the_other = 0;
7405
7406 /* Choose a suitable buffer from echo_buffer[] is we don't
7407 have one. */
7408 if (NILP (echo_area_buffer[this_one]))
7409 {
7410 echo_area_buffer[this_one]
7411 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7412 ? echo_buffer[the_other]
7413 : echo_buffer[this_one]);
7414 clear_buffer_p = 1;
7415 }
7416
7417 buffer = echo_area_buffer[this_one];
7418
7419 /* Don't get confused by reusing the buffer used for echoing
7420 for a different purpose. */
7421 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7422 cancel_echoing ();
7423
7424 record_unwind_protect (unwind_with_echo_area_buffer,
7425 with_echo_area_buffer_unwind_data (w));
7426
7427 /* Make the echo area buffer current. Note that for display
7428 purposes, it is not necessary that the displayed window's buffer
7429 == current_buffer, except for text property lookup. So, let's
7430 only set that buffer temporarily here without doing a full
7431 Fset_window_buffer. We must also change w->pointm, though,
7432 because otherwise an assertions in unshow_buffer fails, and Emacs
7433 aborts. */
7434 set_buffer_internal_1 (XBUFFER (buffer));
7435 if (w)
7436 {
7437 w->buffer = buffer;
7438 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7439 }
7440
7441 current_buffer->undo_list = Qt;
7442 current_buffer->read_only = Qnil;
7443 specbind (Qinhibit_read_only, Qt);
7444 specbind (Qinhibit_modification_hooks, Qt);
7445
7446 if (clear_buffer_p && Z > BEG)
7447 del_range (BEG, Z);
7448
7449 xassert (BEGV >= BEG);
7450 xassert (ZV <= Z && ZV >= BEGV);
7451
7452 rc = fn (a1, a2, a3, a4);
7453
7454 xassert (BEGV >= BEG);
7455 xassert (ZV <= Z && ZV >= BEGV);
7456
7457 unbind_to (count, Qnil);
7458 return rc;
7459 }
7460
7461
7462 /* Save state that should be preserved around the call to the function
7463 FN called in with_echo_area_buffer. */
7464
7465 static Lisp_Object
7466 with_echo_area_buffer_unwind_data (w)
7467 struct window *w;
7468 {
7469 int i = 0;
7470 Lisp_Object vector;
7471
7472 /* Reduce consing by keeping one vector in
7473 Vwith_echo_area_save_vector. */
7474 vector = Vwith_echo_area_save_vector;
7475 Vwith_echo_area_save_vector = Qnil;
7476
7477 if (NILP (vector))
7478 vector = Fmake_vector (make_number (7), Qnil);
7479
7480 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7481 AREF (vector, i) = Vdeactivate_mark, ++i;
7482 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7483
7484 if (w)
7485 {
7486 XSETWINDOW (AREF (vector, i), w); ++i;
7487 AREF (vector, i) = w->buffer; ++i;
7488 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7489 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7490 }
7491 else
7492 {
7493 int end = i + 4;
7494 for (; i < end; ++i)
7495 AREF (vector, i) = Qnil;
7496 }
7497
7498 xassert (i == ASIZE (vector));
7499 return vector;
7500 }
7501
7502
7503 /* Restore global state from VECTOR which was created by
7504 with_echo_area_buffer_unwind_data. */
7505
7506 static Lisp_Object
7507 unwind_with_echo_area_buffer (vector)
7508 Lisp_Object vector;
7509 {
7510 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7511 Vdeactivate_mark = AREF (vector, 1);
7512 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7513
7514 if (WINDOWP (AREF (vector, 3)))
7515 {
7516 struct window *w;
7517 Lisp_Object buffer, charpos, bytepos;
7518
7519 w = XWINDOW (AREF (vector, 3));
7520 buffer = AREF (vector, 4);
7521 charpos = AREF (vector, 5);
7522 bytepos = AREF (vector, 6);
7523
7524 w->buffer = buffer;
7525 set_marker_both (w->pointm, buffer,
7526 XFASTINT (charpos), XFASTINT (bytepos));
7527 }
7528
7529 Vwith_echo_area_save_vector = vector;
7530 return Qnil;
7531 }
7532
7533
7534 /* Set up the echo area for use by print functions. MULTIBYTE_P
7535 non-zero means we will print multibyte. */
7536
7537 void
7538 setup_echo_area_for_printing (multibyte_p)
7539 int multibyte_p;
7540 {
7541 /* If we can't find an echo area any more, exit. */
7542 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7543 Fkill_emacs (Qnil);
7544
7545 ensure_echo_area_buffers ();
7546
7547 if (!message_buf_print)
7548 {
7549 /* A message has been output since the last time we printed.
7550 Choose a fresh echo area buffer. */
7551 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7552 echo_area_buffer[0] = echo_buffer[1];
7553 else
7554 echo_area_buffer[0] = echo_buffer[0];
7555
7556 /* Switch to that buffer and clear it. */
7557 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7558 current_buffer->truncate_lines = Qnil;
7559
7560 if (Z > BEG)
7561 {
7562 int count = SPECPDL_INDEX ();
7563 specbind (Qinhibit_read_only, Qt);
7564 /* Note that undo recording is always disabled. */
7565 del_range (BEG, Z);
7566 unbind_to (count, Qnil);
7567 }
7568 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7569
7570 /* Set up the buffer for the multibyteness we need. */
7571 if (multibyte_p
7572 != !NILP (current_buffer->enable_multibyte_characters))
7573 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7574
7575 /* Raise the frame containing the echo area. */
7576 if (minibuffer_auto_raise)
7577 {
7578 struct frame *sf = SELECTED_FRAME ();
7579 Lisp_Object mini_window;
7580 mini_window = FRAME_MINIBUF_WINDOW (sf);
7581 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7582 }
7583
7584 message_log_maybe_newline ();
7585 message_buf_print = 1;
7586 }
7587 else
7588 {
7589 if (NILP (echo_area_buffer[0]))
7590 {
7591 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7592 echo_area_buffer[0] = echo_buffer[1];
7593 else
7594 echo_area_buffer[0] = echo_buffer[0];
7595 }
7596
7597 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7598 {
7599 /* Someone switched buffers between print requests. */
7600 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7601 current_buffer->truncate_lines = Qnil;
7602 }
7603 }
7604 }
7605
7606
7607 /* Display an echo area message in window W. Value is non-zero if W's
7608 height is changed. If display_last_displayed_message_p is
7609 non-zero, display the message that was last displayed, otherwise
7610 display the current message. */
7611
7612 static int
7613 display_echo_area (w)
7614 struct window *w;
7615 {
7616 int i, no_message_p, window_height_changed_p, count;
7617
7618 /* Temporarily disable garbage collections while displaying the echo
7619 area. This is done because a GC can print a message itself.
7620 That message would modify the echo area buffer's contents while a
7621 redisplay of the buffer is going on, and seriously confuse
7622 redisplay. */
7623 count = inhibit_garbage_collection ();
7624
7625 /* If there is no message, we must call display_echo_area_1
7626 nevertheless because it resizes the window. But we will have to
7627 reset the echo_area_buffer in question to nil at the end because
7628 with_echo_area_buffer will sets it to an empty buffer. */
7629 i = display_last_displayed_message_p ? 1 : 0;
7630 no_message_p = NILP (echo_area_buffer[i]);
7631
7632 window_height_changed_p
7633 = with_echo_area_buffer (w, display_last_displayed_message_p,
7634 display_echo_area_1,
7635 (EMACS_INT) w, Qnil, 0, 0);
7636
7637 if (no_message_p)
7638 echo_area_buffer[i] = Qnil;
7639
7640 unbind_to (count, Qnil);
7641 return window_height_changed_p;
7642 }
7643
7644
7645 /* Helper for display_echo_area. Display the current buffer which
7646 contains the current echo area message in window W, a mini-window,
7647 a pointer to which is passed in A1. A2..A4 are currently not used.
7648 Change the height of W so that all of the message is displayed.
7649 Value is non-zero if height of W was changed. */
7650
7651 static int
7652 display_echo_area_1 (a1, a2, a3, a4)
7653 EMACS_INT a1;
7654 Lisp_Object a2;
7655 EMACS_INT a3, a4;
7656 {
7657 struct window *w = (struct window *) a1;
7658 Lisp_Object window;
7659 struct text_pos start;
7660 int window_height_changed_p = 0;
7661
7662 /* Do this before displaying, so that we have a large enough glyph
7663 matrix for the display. If we can't get enough space for the
7664 whole text, display the last N lines. That works by setting w->start. */
7665 window_height_changed_p = resize_mini_window (w, 0);
7666
7667 /* Use the starting position chosen by resize_mini_window. */
7668 SET_TEXT_POS_FROM_MARKER (start, w->start);
7669
7670 /* Display. */
7671 clear_glyph_matrix (w->desired_matrix);
7672 XSETWINDOW (window, w);
7673 try_window (window, start, 0);
7674
7675 return window_height_changed_p;
7676 }
7677
7678
7679 /* Resize the echo area window to exactly the size needed for the
7680 currently displayed message, if there is one. If a mini-buffer
7681 is active, don't shrink it. */
7682
7683 void
7684 resize_echo_area_exactly ()
7685 {
7686 if (BUFFERP (echo_area_buffer[0])
7687 && WINDOWP (echo_area_window))
7688 {
7689 struct window *w = XWINDOW (echo_area_window);
7690 int resized_p;
7691 Lisp_Object resize_exactly;
7692
7693 if (minibuf_level == 0)
7694 resize_exactly = Qt;
7695 else
7696 resize_exactly = Qnil;
7697
7698 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7699 (EMACS_INT) w, resize_exactly, 0, 0);
7700 if (resized_p)
7701 {
7702 ++windows_or_buffers_changed;
7703 ++update_mode_lines;
7704 redisplay_internal (0);
7705 }
7706 }
7707 }
7708
7709
7710 /* Callback function for with_echo_area_buffer, when used from
7711 resize_echo_area_exactly. A1 contains a pointer to the window to
7712 resize, EXACTLY non-nil means resize the mini-window exactly to the
7713 size of the text displayed. A3 and A4 are not used. Value is what
7714 resize_mini_window returns. */
7715
7716 static int
7717 resize_mini_window_1 (a1, exactly, a3, a4)
7718 EMACS_INT a1;
7719 Lisp_Object exactly;
7720 EMACS_INT a3, a4;
7721 {
7722 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7723 }
7724
7725
7726 /* Resize mini-window W to fit the size of its contents. EXACT:P
7727 means size the window exactly to the size needed. Otherwise, it's
7728 only enlarged until W's buffer is empty.
7729
7730 Set W->start to the right place to begin display. If the whole
7731 contents fit, start at the beginning. Otherwise, start so as
7732 to make the end of the contents appear. This is particularly
7733 important for y-or-n-p, but seems desirable generally.
7734
7735 Value is non-zero if the window height has been changed. */
7736
7737 int
7738 resize_mini_window (w, exact_p)
7739 struct window *w;
7740 int exact_p;
7741 {
7742 struct frame *f = XFRAME (w->frame);
7743 int window_height_changed_p = 0;
7744
7745 xassert (MINI_WINDOW_P (w));
7746
7747 /* By default, start display at the beginning. */
7748 set_marker_both (w->start, w->buffer,
7749 BUF_BEGV (XBUFFER (w->buffer)),
7750 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
7751
7752 /* Don't resize windows while redisplaying a window; it would
7753 confuse redisplay functions when the size of the window they are
7754 displaying changes from under them. Such a resizing can happen,
7755 for instance, when which-func prints a long message while
7756 we are running fontification-functions. We're running these
7757 functions with safe_call which binds inhibit-redisplay to t. */
7758 if (!NILP (Vinhibit_redisplay))
7759 return 0;
7760
7761 /* Nil means don't try to resize. */
7762 if (NILP (Vresize_mini_windows)
7763 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7764 return 0;
7765
7766 if (!FRAME_MINIBUF_ONLY_P (f))
7767 {
7768 struct it it;
7769 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7770 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7771 int height, max_height;
7772 int unit = FRAME_LINE_HEIGHT (f);
7773 struct text_pos start;
7774 struct buffer *old_current_buffer = NULL;
7775
7776 if (current_buffer != XBUFFER (w->buffer))
7777 {
7778 old_current_buffer = current_buffer;
7779 set_buffer_internal (XBUFFER (w->buffer));
7780 }
7781
7782 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7783
7784 /* Compute the max. number of lines specified by the user. */
7785 if (FLOATP (Vmax_mini_window_height))
7786 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7787 else if (INTEGERP (Vmax_mini_window_height))
7788 max_height = XINT (Vmax_mini_window_height);
7789 else
7790 max_height = total_height / 4;
7791
7792 /* Correct that max. height if it's bogus. */
7793 max_height = max (1, max_height);
7794 max_height = min (total_height, max_height);
7795
7796 /* Find out the height of the text in the window. */
7797 if (it.truncate_lines_p)
7798 height = 1;
7799 else
7800 {
7801 last_height = 0;
7802 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7803 if (it.max_ascent == 0 && it.max_descent == 0)
7804 height = it.current_y + last_height;
7805 else
7806 height = it.current_y + it.max_ascent + it.max_descent;
7807 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7808 height = (height + unit - 1) / unit;
7809 }
7810
7811 /* Compute a suitable window start. */
7812 if (height > max_height)
7813 {
7814 height = max_height;
7815 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
7816 move_it_vertically_backward (&it, (height - 1) * unit);
7817 start = it.current.pos;
7818 }
7819 else
7820 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7821 SET_MARKER_FROM_TEXT_POS (w->start, start);
7822
7823 if (EQ (Vresize_mini_windows, Qgrow_only))
7824 {
7825 /* Let it grow only, until we display an empty message, in which
7826 case the window shrinks again. */
7827 if (height > WINDOW_TOTAL_LINES (w))
7828 {
7829 int old_height = WINDOW_TOTAL_LINES (w);
7830 freeze_window_starts (f, 1);
7831 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7832 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7833 }
7834 else if (height < WINDOW_TOTAL_LINES (w)
7835 && (exact_p || BEGV == ZV))
7836 {
7837 int old_height = WINDOW_TOTAL_LINES (w);
7838 freeze_window_starts (f, 0);
7839 shrink_mini_window (w);
7840 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7841 }
7842 }
7843 else
7844 {
7845 /* Always resize to exact size needed. */
7846 if (height > WINDOW_TOTAL_LINES (w))
7847 {
7848 int old_height = WINDOW_TOTAL_LINES (w);
7849 freeze_window_starts (f, 1);
7850 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7851 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7852 }
7853 else if (height < WINDOW_TOTAL_LINES (w))
7854 {
7855 int old_height = WINDOW_TOTAL_LINES (w);
7856 freeze_window_starts (f, 0);
7857 shrink_mini_window (w);
7858
7859 if (height)
7860 {
7861 freeze_window_starts (f, 1);
7862 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7863 }
7864
7865 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7866 }
7867 }
7868
7869 if (old_current_buffer)
7870 set_buffer_internal (old_current_buffer);
7871 }
7872
7873 return window_height_changed_p;
7874 }
7875
7876
7877 /* Value is the current message, a string, or nil if there is no
7878 current message. */
7879
7880 Lisp_Object
7881 current_message ()
7882 {
7883 Lisp_Object msg;
7884
7885 if (NILP (echo_area_buffer[0]))
7886 msg = Qnil;
7887 else
7888 {
7889 with_echo_area_buffer (0, 0, current_message_1,
7890 (EMACS_INT) &msg, Qnil, 0, 0);
7891 if (NILP (msg))
7892 echo_area_buffer[0] = Qnil;
7893 }
7894
7895 return msg;
7896 }
7897
7898
7899 static int
7900 current_message_1 (a1, a2, a3, a4)
7901 EMACS_INT a1;
7902 Lisp_Object a2;
7903 EMACS_INT a3, a4;
7904 {
7905 Lisp_Object *msg = (Lisp_Object *) a1;
7906
7907 if (Z > BEG)
7908 *msg = make_buffer_string (BEG, Z, 1);
7909 else
7910 *msg = Qnil;
7911 return 0;
7912 }
7913
7914
7915 /* Push the current message on Vmessage_stack for later restauration
7916 by restore_message. Value is non-zero if the current message isn't
7917 empty. This is a relatively infrequent operation, so it's not
7918 worth optimizing. */
7919
7920 int
7921 push_message ()
7922 {
7923 Lisp_Object msg;
7924 msg = current_message ();
7925 Vmessage_stack = Fcons (msg, Vmessage_stack);
7926 return STRINGP (msg);
7927 }
7928
7929
7930 /* Restore message display from the top of Vmessage_stack. */
7931
7932 void
7933 restore_message ()
7934 {
7935 Lisp_Object msg;
7936
7937 xassert (CONSP (Vmessage_stack));
7938 msg = XCAR (Vmessage_stack);
7939 if (STRINGP (msg))
7940 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7941 else
7942 message3_nolog (msg, 0, 0);
7943 }
7944
7945
7946 /* Handler for record_unwind_protect calling pop_message. */
7947
7948 Lisp_Object
7949 pop_message_unwind (dummy)
7950 Lisp_Object dummy;
7951 {
7952 pop_message ();
7953 return Qnil;
7954 }
7955
7956 /* Pop the top-most entry off Vmessage_stack. */
7957
7958 void
7959 pop_message ()
7960 {
7961 xassert (CONSP (Vmessage_stack));
7962 Vmessage_stack = XCDR (Vmessage_stack);
7963 }
7964
7965
7966 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7967 exits. If the stack is not empty, we have a missing pop_message
7968 somewhere. */
7969
7970 void
7971 check_message_stack ()
7972 {
7973 if (!NILP (Vmessage_stack))
7974 abort ();
7975 }
7976
7977
7978 /* Truncate to NCHARS what will be displayed in the echo area the next
7979 time we display it---but don't redisplay it now. */
7980
7981 void
7982 truncate_echo_area (nchars)
7983 int nchars;
7984 {
7985 if (nchars == 0)
7986 echo_area_buffer[0] = Qnil;
7987 /* A null message buffer means that the frame hasn't really been
7988 initialized yet. Error messages get reported properly by
7989 cmd_error, so this must be just an informative message; toss it. */
7990 else if (!noninteractive
7991 && INTERACTIVE
7992 && !NILP (echo_area_buffer[0]))
7993 {
7994 struct frame *sf = SELECTED_FRAME ();
7995 if (FRAME_MESSAGE_BUF (sf))
7996 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7997 }
7998 }
7999
8000
8001 /* Helper function for truncate_echo_area. Truncate the current
8002 message to at most NCHARS characters. */
8003
8004 static int
8005 truncate_message_1 (nchars, a2, a3, a4)
8006 EMACS_INT nchars;
8007 Lisp_Object a2;
8008 EMACS_INT a3, a4;
8009 {
8010 if (BEG + nchars < Z)
8011 del_range (BEG + nchars, Z);
8012 if (Z == BEG)
8013 echo_area_buffer[0] = Qnil;
8014 return 0;
8015 }
8016
8017
8018 /* Set the current message to a substring of S or STRING.
8019
8020 If STRING is a Lisp string, set the message to the first NBYTES
8021 bytes from STRING. NBYTES zero means use the whole string. If
8022 STRING is multibyte, the message will be displayed multibyte.
8023
8024 If S is not null, set the message to the first LEN bytes of S. LEN
8025 zero means use the whole string. MULTIBYTE_P non-zero means S is
8026 multibyte. Display the message multibyte in that case. */
8027
8028 void
8029 set_message (s, string, nbytes, multibyte_p)
8030 const char *s;
8031 Lisp_Object string;
8032 int nbytes, multibyte_p;
8033 {
8034 message_enable_multibyte
8035 = ((s && multibyte_p)
8036 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8037
8038 with_echo_area_buffer (0, 0, set_message_1,
8039 (EMACS_INT) s, string, nbytes, multibyte_p);
8040 message_buf_print = 0;
8041 help_echo_showing_p = 0;
8042 }
8043
8044
8045 /* Helper function for set_message. Arguments have the same meaning
8046 as there, with A1 corresponding to S and A2 corresponding to STRING
8047 This function is called with the echo area buffer being
8048 current. */
8049
8050 static int
8051 set_message_1 (a1, a2, nbytes, multibyte_p)
8052 EMACS_INT a1;
8053 Lisp_Object a2;
8054 EMACS_INT nbytes, multibyte_p;
8055 {
8056 const char *s = (const char *) a1;
8057 Lisp_Object string = a2;
8058
8059 /* Change multibyteness of the echo buffer appropriately. */
8060 if (message_enable_multibyte
8061 != !NILP (current_buffer->enable_multibyte_characters))
8062 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8063
8064 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8065
8066 /* Insert new message at BEG. */
8067 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8068 Ferase_buffer ();
8069
8070 if (STRINGP (string))
8071 {
8072 int nchars;
8073
8074 if (nbytes == 0)
8075 nbytes = SBYTES (string);
8076 nchars = string_byte_to_char (string, nbytes);
8077
8078 /* This function takes care of single/multibyte conversion. We
8079 just have to ensure that the echo area buffer has the right
8080 setting of enable_multibyte_characters. */
8081 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8082 }
8083 else if (s)
8084 {
8085 if (nbytes == 0)
8086 nbytes = strlen (s);
8087
8088 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8089 {
8090 /* Convert from multi-byte to single-byte. */
8091 int i, c, n;
8092 unsigned char work[1];
8093
8094 /* Convert a multibyte string to single-byte. */
8095 for (i = 0; i < nbytes; i += n)
8096 {
8097 c = string_char_and_length (s + i, nbytes - i, &n);
8098 work[0] = (SINGLE_BYTE_CHAR_P (c)
8099 ? c
8100 : multibyte_char_to_unibyte (c, Qnil));
8101 insert_1_both (work, 1, 1, 1, 0, 0);
8102 }
8103 }
8104 else if (!multibyte_p
8105 && !NILP (current_buffer->enable_multibyte_characters))
8106 {
8107 /* Convert from single-byte to multi-byte. */
8108 int i, c, n;
8109 const unsigned char *msg = (const unsigned char *) s;
8110 unsigned char str[MAX_MULTIBYTE_LENGTH];
8111
8112 /* Convert a single-byte string to multibyte. */
8113 for (i = 0; i < nbytes; i++)
8114 {
8115 c = unibyte_char_to_multibyte (msg[i]);
8116 n = CHAR_STRING (c, str);
8117 insert_1_both (str, 1, n, 1, 0, 0);
8118 }
8119 }
8120 else
8121 insert_1 (s, nbytes, 1, 0, 0);
8122 }
8123
8124 return 0;
8125 }
8126
8127
8128 /* Clear messages. CURRENT_P non-zero means clear the current
8129 message. LAST_DISPLAYED_P non-zero means clear the message
8130 last displayed. */
8131
8132 void
8133 clear_message (current_p, last_displayed_p)
8134 int current_p, last_displayed_p;
8135 {
8136 if (current_p)
8137 {
8138 echo_area_buffer[0] = Qnil;
8139 message_cleared_p = 1;
8140 }
8141
8142 if (last_displayed_p)
8143 echo_area_buffer[1] = Qnil;
8144
8145 message_buf_print = 0;
8146 }
8147
8148 /* Clear garbaged frames.
8149
8150 This function is used where the old redisplay called
8151 redraw_garbaged_frames which in turn called redraw_frame which in
8152 turn called clear_frame. The call to clear_frame was a source of
8153 flickering. I believe a clear_frame is not necessary. It should
8154 suffice in the new redisplay to invalidate all current matrices,
8155 and ensure a complete redisplay of all windows. */
8156
8157 static void
8158 clear_garbaged_frames ()
8159 {
8160 if (frame_garbaged)
8161 {
8162 Lisp_Object tail, frame;
8163 int changed_count = 0;
8164
8165 FOR_EACH_FRAME (tail, frame)
8166 {
8167 struct frame *f = XFRAME (frame);
8168
8169 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8170 {
8171 if (f->resized_p)
8172 {
8173 Fredraw_frame (frame);
8174 f->force_flush_display_p = 1;
8175 }
8176 clear_current_matrices (f);
8177 changed_count++;
8178 f->garbaged = 0;
8179 f->resized_p = 0;
8180 }
8181 }
8182
8183 frame_garbaged = 0;
8184 if (changed_count)
8185 ++windows_or_buffers_changed;
8186 }
8187 }
8188
8189
8190 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8191 is non-zero update selected_frame. Value is non-zero if the
8192 mini-windows height has been changed. */
8193
8194 static int
8195 echo_area_display (update_frame_p)
8196 int update_frame_p;
8197 {
8198 Lisp_Object mini_window;
8199 struct window *w;
8200 struct frame *f;
8201 int window_height_changed_p = 0;
8202 struct frame *sf = SELECTED_FRAME ();
8203
8204 mini_window = FRAME_MINIBUF_WINDOW (sf);
8205 w = XWINDOW (mini_window);
8206 f = XFRAME (WINDOW_FRAME (w));
8207
8208 /* Don't display if frame is invisible or not yet initialized. */
8209 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8210 return 0;
8211
8212 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8213 #ifndef MAC_OS8
8214 #ifdef HAVE_WINDOW_SYSTEM
8215 /* When Emacs starts, selected_frame may be a visible terminal
8216 frame, even if we run under a window system. If we let this
8217 through, a message would be displayed on the terminal. */
8218 if (EQ (selected_frame, Vterminal_frame)
8219 && !NILP (Vwindow_system))
8220 return 0;
8221 #endif /* HAVE_WINDOW_SYSTEM */
8222 #endif
8223
8224 /* Redraw garbaged frames. */
8225 if (frame_garbaged)
8226 clear_garbaged_frames ();
8227
8228 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8229 {
8230 echo_area_window = mini_window;
8231 window_height_changed_p = display_echo_area (w);
8232 w->must_be_updated_p = 1;
8233
8234 /* Update the display, unless called from redisplay_internal.
8235 Also don't update the screen during redisplay itself. The
8236 update will happen at the end of redisplay, and an update
8237 here could cause confusion. */
8238 if (update_frame_p && !redisplaying_p)
8239 {
8240 int n = 0;
8241
8242 /* If the display update has been interrupted by pending
8243 input, update mode lines in the frame. Due to the
8244 pending input, it might have been that redisplay hasn't
8245 been called, so that mode lines above the echo area are
8246 garbaged. This looks odd, so we prevent it here. */
8247 if (!display_completed)
8248 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8249
8250 if (window_height_changed_p
8251 /* Don't do this if Emacs is shutting down. Redisplay
8252 needs to run hooks. */
8253 && !NILP (Vrun_hooks))
8254 {
8255 /* Must update other windows. Likewise as in other
8256 cases, don't let this update be interrupted by
8257 pending input. */
8258 int count = SPECPDL_INDEX ();
8259 specbind (Qredisplay_dont_pause, Qt);
8260 windows_or_buffers_changed = 1;
8261 redisplay_internal (0);
8262 unbind_to (count, Qnil);
8263 }
8264 else if (FRAME_WINDOW_P (f) && n == 0)
8265 {
8266 /* Window configuration is the same as before.
8267 Can do with a display update of the echo area,
8268 unless we displayed some mode lines. */
8269 update_single_window (w, 1);
8270 rif->flush_display (f);
8271 }
8272 else
8273 update_frame (f, 1, 1);
8274
8275 /* If cursor is in the echo area, make sure that the next
8276 redisplay displays the minibuffer, so that the cursor will
8277 be replaced with what the minibuffer wants. */
8278 if (cursor_in_echo_area)
8279 ++windows_or_buffers_changed;
8280 }
8281 }
8282 else if (!EQ (mini_window, selected_window))
8283 windows_or_buffers_changed++;
8284
8285 /* The current message is now also the last one displayed. */
8286 echo_area_buffer[1] = echo_area_buffer[0];
8287
8288 /* Prevent redisplay optimization in redisplay_internal by resetting
8289 this_line_start_pos. This is done because the mini-buffer now
8290 displays the message instead of its buffer text. */
8291 if (EQ (mini_window, selected_window))
8292 CHARPOS (this_line_start_pos) = 0;
8293
8294 return window_height_changed_p;
8295 }
8296
8297
8298 \f
8299 /***********************************************************************
8300 Mode Lines and Frame Titles
8301 ***********************************************************************/
8302
8303 /* A buffer for constructing non-propertized mode-line strings and
8304 frame titles in it; allocated from the heap in init_xdisp and
8305 resized as needed in store_mode_line_noprop_char. */
8306
8307 static char *mode_line_noprop_buf;
8308
8309 /* The buffer's end, and a current output position in it. */
8310
8311 static char *mode_line_noprop_buf_end;
8312 static char *mode_line_noprop_ptr;
8313
8314 #define MODE_LINE_NOPROP_LEN(start) \
8315 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8316
8317 static enum {
8318 MODE_LINE_DISPLAY = 0,
8319 MODE_LINE_TITLE,
8320 MODE_LINE_NOPROP,
8321 MODE_LINE_STRING
8322 } mode_line_target;
8323
8324 /* Alist that caches the results of :propertize.
8325 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8326 static Lisp_Object mode_line_proptrans_alist;
8327
8328 /* List of strings making up the mode-line. */
8329 static Lisp_Object mode_line_string_list;
8330
8331 /* Base face property when building propertized mode line string. */
8332 static Lisp_Object mode_line_string_face;
8333 static Lisp_Object mode_line_string_face_prop;
8334
8335
8336 /* Unwind data for mode line strings */
8337
8338 static Lisp_Object Vmode_line_unwind_vector;
8339
8340 static Lisp_Object
8341 format_mode_line_unwind_data (obuf)
8342 struct buffer *obuf;
8343 {
8344 Lisp_Object vector;
8345
8346 /* Reduce consing by keeping one vector in
8347 Vwith_echo_area_save_vector. */
8348 vector = Vmode_line_unwind_vector;
8349 Vmode_line_unwind_vector = Qnil;
8350
8351 if (NILP (vector))
8352 vector = Fmake_vector (make_number (7), Qnil);
8353
8354 AREF (vector, 0) = make_number (mode_line_target);
8355 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8356 AREF (vector, 2) = mode_line_string_list;
8357 AREF (vector, 3) = mode_line_proptrans_alist;
8358 AREF (vector, 4) = mode_line_string_face;
8359 AREF (vector, 5) = mode_line_string_face_prop;
8360
8361 if (obuf)
8362 XSETBUFFER (AREF (vector, 6), obuf);
8363 else
8364 AREF (vector, 6) = Qnil;
8365
8366 return vector;
8367 }
8368
8369 static Lisp_Object
8370 unwind_format_mode_line (vector)
8371 Lisp_Object vector;
8372 {
8373 mode_line_target = XINT (AREF (vector, 0));
8374 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8375 mode_line_string_list = AREF (vector, 2);
8376 mode_line_proptrans_alist = AREF (vector, 3);
8377 mode_line_string_face = AREF (vector, 4);
8378 mode_line_string_face_prop = AREF (vector, 5);
8379
8380 if (!NILP (AREF (vector, 6)))
8381 {
8382 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8383 AREF (vector, 6) = Qnil;
8384 }
8385
8386 Vmode_line_unwind_vector = vector;
8387 return Qnil;
8388 }
8389
8390
8391 /* Store a single character C for the frame title in mode_line_noprop_buf.
8392 Re-allocate mode_line_noprop_buf if necessary. */
8393
8394 static void
8395 #ifdef PROTOTYPES
8396 store_mode_line_noprop_char (char c)
8397 #else
8398 store_mode_line_noprop_char (c)
8399 char c;
8400 #endif
8401 {
8402 /* If output position has reached the end of the allocated buffer,
8403 double the buffer's size. */
8404 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8405 {
8406 int len = MODE_LINE_NOPROP_LEN (0);
8407 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8408 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8409 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8410 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8411 }
8412
8413 *mode_line_noprop_ptr++ = c;
8414 }
8415
8416
8417 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8418 mode_line_noprop_ptr. STR is the string to store. Do not copy
8419 characters that yield more columns than PRECISION; PRECISION <= 0
8420 means copy the whole string. Pad with spaces until FIELD_WIDTH
8421 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8422 pad. Called from display_mode_element when it is used to build a
8423 frame title. */
8424
8425 static int
8426 store_mode_line_noprop (str, field_width, precision)
8427 const unsigned char *str;
8428 int field_width, precision;
8429 {
8430 int n = 0;
8431 int dummy, nbytes;
8432
8433 /* Copy at most PRECISION chars from STR. */
8434 nbytes = strlen (str);
8435 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8436 while (nbytes--)
8437 store_mode_line_noprop_char (*str++);
8438
8439 /* Fill up with spaces until FIELD_WIDTH reached. */
8440 while (field_width > 0
8441 && n < field_width)
8442 {
8443 store_mode_line_noprop_char (' ');
8444 ++n;
8445 }
8446
8447 return n;
8448 }
8449
8450 /***********************************************************************
8451 Frame Titles
8452 ***********************************************************************/
8453
8454 #ifdef HAVE_WINDOW_SYSTEM
8455
8456 /* Set the title of FRAME, if it has changed. The title format is
8457 Vicon_title_format if FRAME is iconified, otherwise it is
8458 frame_title_format. */
8459
8460 static void
8461 x_consider_frame_title (frame)
8462 Lisp_Object frame;
8463 {
8464 struct frame *f = XFRAME (frame);
8465
8466 if (FRAME_WINDOW_P (f)
8467 || FRAME_MINIBUF_ONLY_P (f)
8468 || f->explicit_name)
8469 {
8470 /* Do we have more than one visible frame on this X display? */
8471 Lisp_Object tail;
8472 Lisp_Object fmt;
8473 int title_start;
8474 char *title;
8475 int len;
8476 struct it it;
8477 int count = SPECPDL_INDEX ();
8478
8479 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8480 {
8481 Lisp_Object other_frame = XCAR (tail);
8482 struct frame *tf = XFRAME (other_frame);
8483
8484 if (tf != f
8485 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8486 && !FRAME_MINIBUF_ONLY_P (tf)
8487 && !EQ (other_frame, tip_frame)
8488 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8489 break;
8490 }
8491
8492 /* Set global variable indicating that multiple frames exist. */
8493 multiple_frames = CONSP (tail);
8494
8495 /* Switch to the buffer of selected window of the frame. Set up
8496 mode_line_target so that display_mode_element will output into
8497 mode_line_noprop_buf; then display the title. */
8498 record_unwind_protect (unwind_format_mode_line,
8499 format_mode_line_unwind_data (current_buffer));
8500
8501 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8502 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8503
8504 mode_line_target = MODE_LINE_TITLE;
8505 title_start = MODE_LINE_NOPROP_LEN (0);
8506 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8507 NULL, DEFAULT_FACE_ID);
8508 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8509 len = MODE_LINE_NOPROP_LEN (title_start);
8510 title = mode_line_noprop_buf + title_start;
8511 unbind_to (count, Qnil);
8512
8513 /* Set the title only if it's changed. This avoids consing in
8514 the common case where it hasn't. (If it turns out that we've
8515 already wasted too much time by walking through the list with
8516 display_mode_element, then we might need to optimize at a
8517 higher level than this.) */
8518 if (! STRINGP (f->name)
8519 || SBYTES (f->name) != len
8520 || bcmp (title, SDATA (f->name), len) != 0)
8521 x_implicitly_set_name (f, make_string (title, len), Qnil);
8522 }
8523 }
8524
8525 #endif /* not HAVE_WINDOW_SYSTEM */
8526
8527
8528
8529 \f
8530 /***********************************************************************
8531 Menu Bars
8532 ***********************************************************************/
8533
8534
8535 /* Prepare for redisplay by updating menu-bar item lists when
8536 appropriate. This can call eval. */
8537
8538 void
8539 prepare_menu_bars ()
8540 {
8541 int all_windows;
8542 struct gcpro gcpro1, gcpro2;
8543 struct frame *f;
8544 Lisp_Object tooltip_frame;
8545
8546 #ifdef HAVE_WINDOW_SYSTEM
8547 tooltip_frame = tip_frame;
8548 #else
8549 tooltip_frame = Qnil;
8550 #endif
8551
8552 /* Update all frame titles based on their buffer names, etc. We do
8553 this before the menu bars so that the buffer-menu will show the
8554 up-to-date frame titles. */
8555 #ifdef HAVE_WINDOW_SYSTEM
8556 if (windows_or_buffers_changed || update_mode_lines)
8557 {
8558 Lisp_Object tail, frame;
8559
8560 FOR_EACH_FRAME (tail, frame)
8561 {
8562 f = XFRAME (frame);
8563 if (!EQ (frame, tooltip_frame)
8564 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8565 x_consider_frame_title (frame);
8566 }
8567 }
8568 #endif /* HAVE_WINDOW_SYSTEM */
8569
8570 /* Update the menu bar item lists, if appropriate. This has to be
8571 done before any actual redisplay or generation of display lines. */
8572 all_windows = (update_mode_lines
8573 || buffer_shared > 1
8574 || windows_or_buffers_changed);
8575 if (all_windows)
8576 {
8577 Lisp_Object tail, frame;
8578 int count = SPECPDL_INDEX ();
8579
8580 record_unwind_save_match_data ();
8581
8582 FOR_EACH_FRAME (tail, frame)
8583 {
8584 f = XFRAME (frame);
8585
8586 /* Ignore tooltip frame. */
8587 if (EQ (frame, tooltip_frame))
8588 continue;
8589
8590 /* If a window on this frame changed size, report that to
8591 the user and clear the size-change flag. */
8592 if (FRAME_WINDOW_SIZES_CHANGED (f))
8593 {
8594 Lisp_Object functions;
8595
8596 /* Clear flag first in case we get an error below. */
8597 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8598 functions = Vwindow_size_change_functions;
8599 GCPRO2 (tail, functions);
8600
8601 while (CONSP (functions))
8602 {
8603 call1 (XCAR (functions), frame);
8604 functions = XCDR (functions);
8605 }
8606 UNGCPRO;
8607 }
8608
8609 GCPRO1 (tail);
8610 update_menu_bar (f, 0);
8611 #ifdef HAVE_WINDOW_SYSTEM
8612 update_tool_bar (f, 0);
8613 #endif
8614 UNGCPRO;
8615 }
8616
8617 unbind_to (count, Qnil);
8618 }
8619 else
8620 {
8621 struct frame *sf = SELECTED_FRAME ();
8622 update_menu_bar (sf, 1);
8623 #ifdef HAVE_WINDOW_SYSTEM
8624 update_tool_bar (sf, 1);
8625 #endif
8626 }
8627
8628 /* Motif needs this. See comment in xmenu.c. Turn it off when
8629 pending_menu_activation is not defined. */
8630 #ifdef USE_X_TOOLKIT
8631 pending_menu_activation = 0;
8632 #endif
8633 }
8634
8635
8636 /* Update the menu bar item list for frame F. This has to be done
8637 before we start to fill in any display lines, because it can call
8638 eval.
8639
8640 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8641
8642 static void
8643 update_menu_bar (f, save_match_data)
8644 struct frame *f;
8645 int save_match_data;
8646 {
8647 Lisp_Object window;
8648 register struct window *w;
8649
8650 /* If called recursively during a menu update, do nothing. This can
8651 happen when, for instance, an activate-menubar-hook causes a
8652 redisplay. */
8653 if (inhibit_menubar_update)
8654 return;
8655
8656 window = FRAME_SELECTED_WINDOW (f);
8657 w = XWINDOW (window);
8658
8659 #if 0 /* The if statement below this if statement used to include the
8660 condition !NILP (w->update_mode_line), rather than using
8661 update_mode_lines directly, and this if statement may have
8662 been added to make that condition work. Now the if
8663 statement below matches its comment, this isn't needed. */
8664 if (update_mode_lines)
8665 w->update_mode_line = Qt;
8666 #endif
8667
8668 if (FRAME_WINDOW_P (f)
8669 ?
8670 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8671 || defined (USE_GTK)
8672 FRAME_EXTERNAL_MENU_BAR (f)
8673 #else
8674 FRAME_MENU_BAR_LINES (f) > 0
8675 #endif
8676 : FRAME_MENU_BAR_LINES (f) > 0)
8677 {
8678 /* If the user has switched buffers or windows, we need to
8679 recompute to reflect the new bindings. But we'll
8680 recompute when update_mode_lines is set too; that means
8681 that people can use force-mode-line-update to request
8682 that the menu bar be recomputed. The adverse effect on
8683 the rest of the redisplay algorithm is about the same as
8684 windows_or_buffers_changed anyway. */
8685 if (windows_or_buffers_changed
8686 /* This used to test w->update_mode_line, but we believe
8687 there is no need to recompute the menu in that case. */
8688 || update_mode_lines
8689 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8690 < BUF_MODIFF (XBUFFER (w->buffer)))
8691 != !NILP (w->last_had_star))
8692 || ((!NILP (Vtransient_mark_mode)
8693 && !NILP (XBUFFER (w->buffer)->mark_active))
8694 != !NILP (w->region_showing)))
8695 {
8696 struct buffer *prev = current_buffer;
8697 int count = SPECPDL_INDEX ();
8698
8699 specbind (Qinhibit_menubar_update, Qt);
8700
8701 set_buffer_internal_1 (XBUFFER (w->buffer));
8702 if (save_match_data)
8703 record_unwind_save_match_data ();
8704 if (NILP (Voverriding_local_map_menu_flag))
8705 {
8706 specbind (Qoverriding_terminal_local_map, Qnil);
8707 specbind (Qoverriding_local_map, Qnil);
8708 }
8709
8710 /* Run the Lucid hook. */
8711 safe_run_hooks (Qactivate_menubar_hook);
8712
8713 /* If it has changed current-menubar from previous value,
8714 really recompute the menu-bar from the value. */
8715 if (! NILP (Vlucid_menu_bar_dirty_flag))
8716 call0 (Qrecompute_lucid_menubar);
8717
8718 safe_run_hooks (Qmenu_bar_update_hook);
8719 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8720
8721 /* Redisplay the menu bar in case we changed it. */
8722 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8723 || defined (USE_GTK)
8724 if (FRAME_WINDOW_P (f)
8725 #if defined (MAC_OS)
8726 /* All frames on Mac OS share the same menubar. So only the
8727 selected frame should be allowed to set it. */
8728 && f == SELECTED_FRAME ()
8729 #endif
8730 )
8731 set_frame_menubar (f, 0, 0);
8732 else
8733 /* On a terminal screen, the menu bar is an ordinary screen
8734 line, and this makes it get updated. */
8735 w->update_mode_line = Qt;
8736 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8737 /* In the non-toolkit version, the menu bar is an ordinary screen
8738 line, and this makes it get updated. */
8739 w->update_mode_line = Qt;
8740 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8741
8742 unbind_to (count, Qnil);
8743 set_buffer_internal_1 (prev);
8744 }
8745 }
8746 }
8747
8748
8749 \f
8750 /***********************************************************************
8751 Output Cursor
8752 ***********************************************************************/
8753
8754 #ifdef HAVE_WINDOW_SYSTEM
8755
8756 /* EXPORT:
8757 Nominal cursor position -- where to draw output.
8758 HPOS and VPOS are window relative glyph matrix coordinates.
8759 X and Y are window relative pixel coordinates. */
8760
8761 struct cursor_pos output_cursor;
8762
8763
8764 /* EXPORT:
8765 Set the global variable output_cursor to CURSOR. All cursor
8766 positions are relative to updated_window. */
8767
8768 void
8769 set_output_cursor (cursor)
8770 struct cursor_pos *cursor;
8771 {
8772 output_cursor.hpos = cursor->hpos;
8773 output_cursor.vpos = cursor->vpos;
8774 output_cursor.x = cursor->x;
8775 output_cursor.y = cursor->y;
8776 }
8777
8778
8779 /* EXPORT for RIF:
8780 Set a nominal cursor position.
8781
8782 HPOS and VPOS are column/row positions in a window glyph matrix. X
8783 and Y are window text area relative pixel positions.
8784
8785 If this is done during an update, updated_window will contain the
8786 window that is being updated and the position is the future output
8787 cursor position for that window. If updated_window is null, use
8788 selected_window and display the cursor at the given position. */
8789
8790 void
8791 x_cursor_to (vpos, hpos, y, x)
8792 int vpos, hpos, y, x;
8793 {
8794 struct window *w;
8795
8796 /* If updated_window is not set, work on selected_window. */
8797 if (updated_window)
8798 w = updated_window;
8799 else
8800 w = XWINDOW (selected_window);
8801
8802 /* Set the output cursor. */
8803 output_cursor.hpos = hpos;
8804 output_cursor.vpos = vpos;
8805 output_cursor.x = x;
8806 output_cursor.y = y;
8807
8808 /* If not called as part of an update, really display the cursor.
8809 This will also set the cursor position of W. */
8810 if (updated_window == NULL)
8811 {
8812 BLOCK_INPUT;
8813 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8814 if (rif->flush_display_optional)
8815 rif->flush_display_optional (SELECTED_FRAME ());
8816 UNBLOCK_INPUT;
8817 }
8818 }
8819
8820 #endif /* HAVE_WINDOW_SYSTEM */
8821
8822 \f
8823 /***********************************************************************
8824 Tool-bars
8825 ***********************************************************************/
8826
8827 #ifdef HAVE_WINDOW_SYSTEM
8828
8829 /* Where the mouse was last time we reported a mouse event. */
8830
8831 FRAME_PTR last_mouse_frame;
8832
8833 /* Tool-bar item index of the item on which a mouse button was pressed
8834 or -1. */
8835
8836 int last_tool_bar_item;
8837
8838
8839 /* Update the tool-bar item list for frame F. This has to be done
8840 before we start to fill in any display lines. Called from
8841 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8842 and restore it here. */
8843
8844 static void
8845 update_tool_bar (f, save_match_data)
8846 struct frame *f;
8847 int save_match_data;
8848 {
8849 #ifdef USE_GTK
8850 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8851 #else
8852 int do_update = WINDOWP (f->tool_bar_window)
8853 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8854 #endif
8855
8856 if (do_update)
8857 {
8858 Lisp_Object window;
8859 struct window *w;
8860
8861 window = FRAME_SELECTED_WINDOW (f);
8862 w = XWINDOW (window);
8863
8864 /* If the user has switched buffers or windows, we need to
8865 recompute to reflect the new bindings. But we'll
8866 recompute when update_mode_lines is set too; that means
8867 that people can use force-mode-line-update to request
8868 that the menu bar be recomputed. The adverse effect on
8869 the rest of the redisplay algorithm is about the same as
8870 windows_or_buffers_changed anyway. */
8871 if (windows_or_buffers_changed
8872 || !NILP (w->update_mode_line)
8873 || update_mode_lines
8874 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8875 < BUF_MODIFF (XBUFFER (w->buffer)))
8876 != !NILP (w->last_had_star))
8877 || ((!NILP (Vtransient_mark_mode)
8878 && !NILP (XBUFFER (w->buffer)->mark_active))
8879 != !NILP (w->region_showing)))
8880 {
8881 struct buffer *prev = current_buffer;
8882 int count = SPECPDL_INDEX ();
8883 Lisp_Object new_tool_bar;
8884 int new_n_tool_bar;
8885 struct gcpro gcpro1;
8886
8887 /* Set current_buffer to the buffer of the selected
8888 window of the frame, so that we get the right local
8889 keymaps. */
8890 set_buffer_internal_1 (XBUFFER (w->buffer));
8891
8892 /* Save match data, if we must. */
8893 if (save_match_data)
8894 record_unwind_save_match_data ();
8895
8896 /* Make sure that we don't accidentally use bogus keymaps. */
8897 if (NILP (Voverriding_local_map_menu_flag))
8898 {
8899 specbind (Qoverriding_terminal_local_map, Qnil);
8900 specbind (Qoverriding_local_map, Qnil);
8901 }
8902
8903 GCPRO1 (new_tool_bar);
8904
8905 /* Build desired tool-bar items from keymaps. */
8906 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8907 &new_n_tool_bar);
8908
8909 /* Redisplay the tool-bar if we changed it. */
8910 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8911 {
8912 /* Redisplay that happens asynchronously due to an expose event
8913 may access f->tool_bar_items. Make sure we update both
8914 variables within BLOCK_INPUT so no such event interrupts. */
8915 BLOCK_INPUT;
8916 f->tool_bar_items = new_tool_bar;
8917 f->n_tool_bar_items = new_n_tool_bar;
8918 w->update_mode_line = Qt;
8919 UNBLOCK_INPUT;
8920 }
8921
8922 UNGCPRO;
8923
8924 unbind_to (count, Qnil);
8925 set_buffer_internal_1 (prev);
8926 }
8927 }
8928 }
8929
8930
8931 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8932 F's desired tool-bar contents. F->tool_bar_items must have
8933 been set up previously by calling prepare_menu_bars. */
8934
8935 static void
8936 build_desired_tool_bar_string (f)
8937 struct frame *f;
8938 {
8939 int i, size, size_needed;
8940 struct gcpro gcpro1, gcpro2, gcpro3;
8941 Lisp_Object image, plist, props;
8942
8943 image = plist = props = Qnil;
8944 GCPRO3 (image, plist, props);
8945
8946 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8947 Otherwise, make a new string. */
8948
8949 /* The size of the string we might be able to reuse. */
8950 size = (STRINGP (f->desired_tool_bar_string)
8951 ? SCHARS (f->desired_tool_bar_string)
8952 : 0);
8953
8954 /* We need one space in the string for each image. */
8955 size_needed = f->n_tool_bar_items;
8956
8957 /* Reuse f->desired_tool_bar_string, if possible. */
8958 if (size < size_needed || NILP (f->desired_tool_bar_string))
8959 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8960 make_number (' '));
8961 else
8962 {
8963 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8964 Fremove_text_properties (make_number (0), make_number (size),
8965 props, f->desired_tool_bar_string);
8966 }
8967
8968 /* Put a `display' property on the string for the images to display,
8969 put a `menu_item' property on tool-bar items with a value that
8970 is the index of the item in F's tool-bar item vector. */
8971 for (i = 0; i < f->n_tool_bar_items; ++i)
8972 {
8973 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8974
8975 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8976 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8977 int hmargin, vmargin, relief, idx, end;
8978 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8979
8980 /* If image is a vector, choose the image according to the
8981 button state. */
8982 image = PROP (TOOL_BAR_ITEM_IMAGES);
8983 if (VECTORP (image))
8984 {
8985 if (enabled_p)
8986 idx = (selected_p
8987 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8988 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8989 else
8990 idx = (selected_p
8991 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8992 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8993
8994 xassert (ASIZE (image) >= idx);
8995 image = AREF (image, idx);
8996 }
8997 else
8998 idx = -1;
8999
9000 /* Ignore invalid image specifications. */
9001 if (!valid_image_p (image))
9002 continue;
9003
9004 /* Display the tool-bar button pressed, or depressed. */
9005 plist = Fcopy_sequence (XCDR (image));
9006
9007 /* Compute margin and relief to draw. */
9008 relief = (tool_bar_button_relief >= 0
9009 ? tool_bar_button_relief
9010 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9011 hmargin = vmargin = relief;
9012
9013 if (INTEGERP (Vtool_bar_button_margin)
9014 && XINT (Vtool_bar_button_margin) > 0)
9015 {
9016 hmargin += XFASTINT (Vtool_bar_button_margin);
9017 vmargin += XFASTINT (Vtool_bar_button_margin);
9018 }
9019 else if (CONSP (Vtool_bar_button_margin))
9020 {
9021 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9022 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9023 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9024
9025 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9026 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9027 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9028 }
9029
9030 if (auto_raise_tool_bar_buttons_p)
9031 {
9032 /* Add a `:relief' property to the image spec if the item is
9033 selected. */
9034 if (selected_p)
9035 {
9036 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9037 hmargin -= relief;
9038 vmargin -= relief;
9039 }
9040 }
9041 else
9042 {
9043 /* If image is selected, display it pressed, i.e. with a
9044 negative relief. If it's not selected, display it with a
9045 raised relief. */
9046 plist = Fplist_put (plist, QCrelief,
9047 (selected_p
9048 ? make_number (-relief)
9049 : make_number (relief)));
9050 hmargin -= relief;
9051 vmargin -= relief;
9052 }
9053
9054 /* Put a margin around the image. */
9055 if (hmargin || vmargin)
9056 {
9057 if (hmargin == vmargin)
9058 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9059 else
9060 plist = Fplist_put (plist, QCmargin,
9061 Fcons (make_number (hmargin),
9062 make_number (vmargin)));
9063 }
9064
9065 /* If button is not enabled, and we don't have special images
9066 for the disabled state, make the image appear disabled by
9067 applying an appropriate algorithm to it. */
9068 if (!enabled_p && idx < 0)
9069 plist = Fplist_put (plist, QCconversion, Qdisabled);
9070
9071 /* Put a `display' text property on the string for the image to
9072 display. Put a `menu-item' property on the string that gives
9073 the start of this item's properties in the tool-bar items
9074 vector. */
9075 image = Fcons (Qimage, plist);
9076 props = list4 (Qdisplay, image,
9077 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9078
9079 /* Let the last image hide all remaining spaces in the tool bar
9080 string. The string can be longer than needed when we reuse a
9081 previous string. */
9082 if (i + 1 == f->n_tool_bar_items)
9083 end = SCHARS (f->desired_tool_bar_string);
9084 else
9085 end = i + 1;
9086 Fadd_text_properties (make_number (i), make_number (end),
9087 props, f->desired_tool_bar_string);
9088 #undef PROP
9089 }
9090
9091 UNGCPRO;
9092 }
9093
9094
9095 /* Display one line of the tool-bar of frame IT->f. */
9096
9097 static void
9098 display_tool_bar_line (it)
9099 struct it *it;
9100 {
9101 struct glyph_row *row = it->glyph_row;
9102 int max_x = it->last_visible_x;
9103 struct glyph *last;
9104
9105 prepare_desired_row (row);
9106 row->y = it->current_y;
9107
9108 /* Note that this isn't made use of if the face hasn't a box,
9109 so there's no need to check the face here. */
9110 it->start_of_box_run_p = 1;
9111
9112 while (it->current_x < max_x)
9113 {
9114 int x_before, x, n_glyphs_before, i, nglyphs;
9115
9116 /* Get the next display element. */
9117 if (!get_next_display_element (it))
9118 break;
9119
9120 /* Produce glyphs. */
9121 x_before = it->current_x;
9122 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9123 PRODUCE_GLYPHS (it);
9124
9125 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9126 i = 0;
9127 x = x_before;
9128 while (i < nglyphs)
9129 {
9130 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9131
9132 if (x + glyph->pixel_width > max_x)
9133 {
9134 /* Glyph doesn't fit on line. */
9135 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9136 it->current_x = x;
9137 goto out;
9138 }
9139
9140 ++it->hpos;
9141 x += glyph->pixel_width;
9142 ++i;
9143 }
9144
9145 /* Stop at line ends. */
9146 if (ITERATOR_AT_END_OF_LINE_P (it))
9147 break;
9148
9149 set_iterator_to_next (it, 1);
9150 }
9151
9152 out:;
9153
9154 row->displays_text_p = row->used[TEXT_AREA] != 0;
9155 extend_face_to_end_of_line (it);
9156 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9157 last->right_box_line_p = 1;
9158 if (last == row->glyphs[TEXT_AREA])
9159 last->left_box_line_p = 1;
9160 compute_line_metrics (it);
9161
9162 /* If line is empty, make it occupy the rest of the tool-bar. */
9163 if (!row->displays_text_p)
9164 {
9165 row->height = row->phys_height = it->last_visible_y - row->y;
9166 row->ascent = row->phys_ascent = 0;
9167 row->extra_line_spacing = 0;
9168 }
9169
9170 row->full_width_p = 1;
9171 row->continued_p = 0;
9172 row->truncated_on_left_p = 0;
9173 row->truncated_on_right_p = 0;
9174
9175 it->current_x = it->hpos = 0;
9176 it->current_y += row->height;
9177 ++it->vpos;
9178 ++it->glyph_row;
9179 }
9180
9181
9182 /* Value is the number of screen lines needed to make all tool-bar
9183 items of frame F visible. */
9184
9185 static int
9186 tool_bar_lines_needed (f)
9187 struct frame *f;
9188 {
9189 struct window *w = XWINDOW (f->tool_bar_window);
9190 struct it it;
9191
9192 /* Initialize an iterator for iteration over
9193 F->desired_tool_bar_string in the tool-bar window of frame F. */
9194 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9195 it.first_visible_x = 0;
9196 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9197 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9198
9199 while (!ITERATOR_AT_END_P (&it))
9200 {
9201 it.glyph_row = w->desired_matrix->rows;
9202 clear_glyph_row (it.glyph_row);
9203 display_tool_bar_line (&it);
9204 }
9205
9206 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9207 }
9208
9209
9210 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9211 0, 1, 0,
9212 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9213 (frame)
9214 Lisp_Object frame;
9215 {
9216 struct frame *f;
9217 struct window *w;
9218 int nlines = 0;
9219
9220 if (NILP (frame))
9221 frame = selected_frame;
9222 else
9223 CHECK_FRAME (frame);
9224 f = XFRAME (frame);
9225
9226 if (WINDOWP (f->tool_bar_window)
9227 || (w = XWINDOW (f->tool_bar_window),
9228 WINDOW_TOTAL_LINES (w) > 0))
9229 {
9230 update_tool_bar (f, 1);
9231 if (f->n_tool_bar_items)
9232 {
9233 build_desired_tool_bar_string (f);
9234 nlines = tool_bar_lines_needed (f);
9235 }
9236 }
9237
9238 return make_number (nlines);
9239 }
9240
9241
9242 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9243 height should be changed. */
9244
9245 static int
9246 redisplay_tool_bar (f)
9247 struct frame *f;
9248 {
9249 struct window *w;
9250 struct it it;
9251 struct glyph_row *row;
9252 int change_height_p = 0;
9253
9254 #ifdef USE_GTK
9255 if (FRAME_EXTERNAL_TOOL_BAR (f))
9256 update_frame_tool_bar (f);
9257 return 0;
9258 #endif
9259
9260 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9261 do anything. This means you must start with tool-bar-lines
9262 non-zero to get the auto-sizing effect. Or in other words, you
9263 can turn off tool-bars by specifying tool-bar-lines zero. */
9264 if (!WINDOWP (f->tool_bar_window)
9265 || (w = XWINDOW (f->tool_bar_window),
9266 WINDOW_TOTAL_LINES (w) == 0))
9267 return 0;
9268
9269 /* Set up an iterator for the tool-bar window. */
9270 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9271 it.first_visible_x = 0;
9272 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9273 row = it.glyph_row;
9274
9275 /* Build a string that represents the contents of the tool-bar. */
9276 build_desired_tool_bar_string (f);
9277 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9278
9279 /* Display as many lines as needed to display all tool-bar items. */
9280 while (it.current_y < it.last_visible_y)
9281 display_tool_bar_line (&it);
9282
9283 /* It doesn't make much sense to try scrolling in the tool-bar
9284 window, so don't do it. */
9285 w->desired_matrix->no_scrolling_p = 1;
9286 w->must_be_updated_p = 1;
9287
9288 if (auto_resize_tool_bars_p)
9289 {
9290 int nlines;
9291
9292 /* If we couldn't display everything, change the tool-bar's
9293 height. */
9294 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9295 change_height_p = 1;
9296
9297 /* If there are blank lines at the end, except for a partially
9298 visible blank line at the end that is smaller than
9299 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9300 row = it.glyph_row - 1;
9301 if (!row->displays_text_p
9302 && row->height >= FRAME_LINE_HEIGHT (f))
9303 change_height_p = 1;
9304
9305 /* If row displays tool-bar items, but is partially visible,
9306 change the tool-bar's height. */
9307 if (row->displays_text_p
9308 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9309 change_height_p = 1;
9310
9311 /* Resize windows as needed by changing the `tool-bar-lines'
9312 frame parameter. */
9313 if (change_height_p
9314 && (nlines = tool_bar_lines_needed (f),
9315 nlines != WINDOW_TOTAL_LINES (w)))
9316 {
9317 extern Lisp_Object Qtool_bar_lines;
9318 Lisp_Object frame;
9319 int old_height = WINDOW_TOTAL_LINES (w);
9320
9321 XSETFRAME (frame, f);
9322 clear_glyph_matrix (w->desired_matrix);
9323 Fmodify_frame_parameters (frame,
9324 Fcons (Fcons (Qtool_bar_lines,
9325 make_number (nlines)),
9326 Qnil));
9327 if (WINDOW_TOTAL_LINES (w) != old_height)
9328 fonts_changed_p = 1;
9329 }
9330 }
9331
9332 return change_height_p;
9333 }
9334
9335
9336 /* Get information about the tool-bar item which is displayed in GLYPH
9337 on frame F. Return in *PROP_IDX the index where tool-bar item
9338 properties start in F->tool_bar_items. Value is zero if
9339 GLYPH doesn't display a tool-bar item. */
9340
9341 static int
9342 tool_bar_item_info (f, glyph, prop_idx)
9343 struct frame *f;
9344 struct glyph *glyph;
9345 int *prop_idx;
9346 {
9347 Lisp_Object prop;
9348 int success_p;
9349 int charpos;
9350
9351 /* This function can be called asynchronously, which means we must
9352 exclude any possibility that Fget_text_property signals an
9353 error. */
9354 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9355 charpos = max (0, charpos);
9356
9357 /* Get the text property `menu-item' at pos. The value of that
9358 property is the start index of this item's properties in
9359 F->tool_bar_items. */
9360 prop = Fget_text_property (make_number (charpos),
9361 Qmenu_item, f->current_tool_bar_string);
9362 if (INTEGERP (prop))
9363 {
9364 *prop_idx = XINT (prop);
9365 success_p = 1;
9366 }
9367 else
9368 success_p = 0;
9369
9370 return success_p;
9371 }
9372
9373 \f
9374 /* Get information about the tool-bar item at position X/Y on frame F.
9375 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9376 the current matrix of the tool-bar window of F, or NULL if not
9377 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9378 item in F->tool_bar_items. Value is
9379
9380 -1 if X/Y is not on a tool-bar item
9381 0 if X/Y is on the same item that was highlighted before.
9382 1 otherwise. */
9383
9384 static int
9385 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9386 struct frame *f;
9387 int x, y;
9388 struct glyph **glyph;
9389 int *hpos, *vpos, *prop_idx;
9390 {
9391 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9392 struct window *w = XWINDOW (f->tool_bar_window);
9393 int area;
9394
9395 /* Find the glyph under X/Y. */
9396 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9397 if (*glyph == NULL)
9398 return -1;
9399
9400 /* Get the start of this tool-bar item's properties in
9401 f->tool_bar_items. */
9402 if (!tool_bar_item_info (f, *glyph, prop_idx))
9403 return -1;
9404
9405 /* Is mouse on the highlighted item? */
9406 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9407 && *vpos >= dpyinfo->mouse_face_beg_row
9408 && *vpos <= dpyinfo->mouse_face_end_row
9409 && (*vpos > dpyinfo->mouse_face_beg_row
9410 || *hpos >= dpyinfo->mouse_face_beg_col)
9411 && (*vpos < dpyinfo->mouse_face_end_row
9412 || *hpos < dpyinfo->mouse_face_end_col
9413 || dpyinfo->mouse_face_past_end))
9414 return 0;
9415
9416 return 1;
9417 }
9418
9419
9420 /* EXPORT:
9421 Handle mouse button event on the tool-bar of frame F, at
9422 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9423 0 for button release. MODIFIERS is event modifiers for button
9424 release. */
9425
9426 void
9427 handle_tool_bar_click (f, x, y, down_p, modifiers)
9428 struct frame *f;
9429 int x, y, down_p;
9430 unsigned int modifiers;
9431 {
9432 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9433 struct window *w = XWINDOW (f->tool_bar_window);
9434 int hpos, vpos, prop_idx;
9435 struct glyph *glyph;
9436 Lisp_Object enabled_p;
9437
9438 /* If not on the highlighted tool-bar item, return. */
9439 frame_to_window_pixel_xy (w, &x, &y);
9440 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9441 return;
9442
9443 /* If item is disabled, do nothing. */
9444 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9445 if (NILP (enabled_p))
9446 return;
9447
9448 if (down_p)
9449 {
9450 /* Show item in pressed state. */
9451 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9452 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9453 last_tool_bar_item = prop_idx;
9454 }
9455 else
9456 {
9457 Lisp_Object key, frame;
9458 struct input_event event;
9459 EVENT_INIT (event);
9460
9461 /* Show item in released state. */
9462 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9463 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9464
9465 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9466
9467 XSETFRAME (frame, f);
9468 event.kind = TOOL_BAR_EVENT;
9469 event.frame_or_window = frame;
9470 event.arg = frame;
9471 kbd_buffer_store_event (&event);
9472
9473 event.kind = TOOL_BAR_EVENT;
9474 event.frame_or_window = frame;
9475 event.arg = key;
9476 event.modifiers = modifiers;
9477 kbd_buffer_store_event (&event);
9478 last_tool_bar_item = -1;
9479 }
9480 }
9481
9482
9483 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9484 tool-bar window-relative coordinates X/Y. Called from
9485 note_mouse_highlight. */
9486
9487 static void
9488 note_tool_bar_highlight (f, x, y)
9489 struct frame *f;
9490 int x, y;
9491 {
9492 Lisp_Object window = f->tool_bar_window;
9493 struct window *w = XWINDOW (window);
9494 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9495 int hpos, vpos;
9496 struct glyph *glyph;
9497 struct glyph_row *row;
9498 int i;
9499 Lisp_Object enabled_p;
9500 int prop_idx;
9501 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9502 int mouse_down_p, rc;
9503
9504 /* Function note_mouse_highlight is called with negative x(y
9505 values when mouse moves outside of the frame. */
9506 if (x <= 0 || y <= 0)
9507 {
9508 clear_mouse_face (dpyinfo);
9509 return;
9510 }
9511
9512 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9513 if (rc < 0)
9514 {
9515 /* Not on tool-bar item. */
9516 clear_mouse_face (dpyinfo);
9517 return;
9518 }
9519 else if (rc == 0)
9520 /* On same tool-bar item as before. */
9521 goto set_help_echo;
9522
9523 clear_mouse_face (dpyinfo);
9524
9525 /* Mouse is down, but on different tool-bar item? */
9526 mouse_down_p = (dpyinfo->grabbed
9527 && f == last_mouse_frame
9528 && FRAME_LIVE_P (f));
9529 if (mouse_down_p
9530 && last_tool_bar_item != prop_idx)
9531 return;
9532
9533 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9534 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9535
9536 /* If tool-bar item is not enabled, don't highlight it. */
9537 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9538 if (!NILP (enabled_p))
9539 {
9540 /* Compute the x-position of the glyph. In front and past the
9541 image is a space. We include this in the highlighted area. */
9542 row = MATRIX_ROW (w->current_matrix, vpos);
9543 for (i = x = 0; i < hpos; ++i)
9544 x += row->glyphs[TEXT_AREA][i].pixel_width;
9545
9546 /* Record this as the current active region. */
9547 dpyinfo->mouse_face_beg_col = hpos;
9548 dpyinfo->mouse_face_beg_row = vpos;
9549 dpyinfo->mouse_face_beg_x = x;
9550 dpyinfo->mouse_face_beg_y = row->y;
9551 dpyinfo->mouse_face_past_end = 0;
9552
9553 dpyinfo->mouse_face_end_col = hpos + 1;
9554 dpyinfo->mouse_face_end_row = vpos;
9555 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9556 dpyinfo->mouse_face_end_y = row->y;
9557 dpyinfo->mouse_face_window = window;
9558 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9559
9560 /* Display it as active. */
9561 show_mouse_face (dpyinfo, draw);
9562 dpyinfo->mouse_face_image_state = draw;
9563 }
9564
9565 set_help_echo:
9566
9567 /* Set help_echo_string to a help string to display for this tool-bar item.
9568 XTread_socket does the rest. */
9569 help_echo_object = help_echo_window = Qnil;
9570 help_echo_pos = -1;
9571 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9572 if (NILP (help_echo_string))
9573 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9574 }
9575
9576 #endif /* HAVE_WINDOW_SYSTEM */
9577
9578
9579 \f
9580 /************************************************************************
9581 Horizontal scrolling
9582 ************************************************************************/
9583
9584 static int hscroll_window_tree P_ ((Lisp_Object));
9585 static int hscroll_windows P_ ((Lisp_Object));
9586
9587 /* For all leaf windows in the window tree rooted at WINDOW, set their
9588 hscroll value so that PT is (i) visible in the window, and (ii) so
9589 that it is not within a certain margin at the window's left and
9590 right border. Value is non-zero if any window's hscroll has been
9591 changed. */
9592
9593 static int
9594 hscroll_window_tree (window)
9595 Lisp_Object window;
9596 {
9597 int hscrolled_p = 0;
9598 int hscroll_relative_p = FLOATP (Vhscroll_step);
9599 int hscroll_step_abs = 0;
9600 double hscroll_step_rel = 0;
9601
9602 if (hscroll_relative_p)
9603 {
9604 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9605 if (hscroll_step_rel < 0)
9606 {
9607 hscroll_relative_p = 0;
9608 hscroll_step_abs = 0;
9609 }
9610 }
9611 else if (INTEGERP (Vhscroll_step))
9612 {
9613 hscroll_step_abs = XINT (Vhscroll_step);
9614 if (hscroll_step_abs < 0)
9615 hscroll_step_abs = 0;
9616 }
9617 else
9618 hscroll_step_abs = 0;
9619
9620 while (WINDOWP (window))
9621 {
9622 struct window *w = XWINDOW (window);
9623
9624 if (WINDOWP (w->hchild))
9625 hscrolled_p |= hscroll_window_tree (w->hchild);
9626 else if (WINDOWP (w->vchild))
9627 hscrolled_p |= hscroll_window_tree (w->vchild);
9628 else if (w->cursor.vpos >= 0)
9629 {
9630 int h_margin;
9631 int text_area_width;
9632 struct glyph_row *current_cursor_row
9633 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9634 struct glyph_row *desired_cursor_row
9635 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9636 struct glyph_row *cursor_row
9637 = (desired_cursor_row->enabled_p
9638 ? desired_cursor_row
9639 : current_cursor_row);
9640
9641 text_area_width = window_box_width (w, TEXT_AREA);
9642
9643 /* Scroll when cursor is inside this scroll margin. */
9644 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9645
9646 if ((XFASTINT (w->hscroll)
9647 && w->cursor.x <= h_margin)
9648 || (cursor_row->enabled_p
9649 && cursor_row->truncated_on_right_p
9650 && (w->cursor.x >= text_area_width - h_margin)))
9651 {
9652 struct it it;
9653 int hscroll;
9654 struct buffer *saved_current_buffer;
9655 int pt;
9656 int wanted_x;
9657
9658 /* Find point in a display of infinite width. */
9659 saved_current_buffer = current_buffer;
9660 current_buffer = XBUFFER (w->buffer);
9661
9662 if (w == XWINDOW (selected_window))
9663 pt = BUF_PT (current_buffer);
9664 else
9665 {
9666 pt = marker_position (w->pointm);
9667 pt = max (BEGV, pt);
9668 pt = min (ZV, pt);
9669 }
9670
9671 /* Move iterator to pt starting at cursor_row->start in
9672 a line with infinite width. */
9673 init_to_row_start (&it, w, cursor_row);
9674 it.last_visible_x = INFINITY;
9675 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9676 current_buffer = saved_current_buffer;
9677
9678 /* Position cursor in window. */
9679 if (!hscroll_relative_p && hscroll_step_abs == 0)
9680 hscroll = max (0, (it.current_x
9681 - (ITERATOR_AT_END_OF_LINE_P (&it)
9682 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9683 : (text_area_width / 2))))
9684 / FRAME_COLUMN_WIDTH (it.f);
9685 else if (w->cursor.x >= text_area_width - h_margin)
9686 {
9687 if (hscroll_relative_p)
9688 wanted_x = text_area_width * (1 - hscroll_step_rel)
9689 - h_margin;
9690 else
9691 wanted_x = text_area_width
9692 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9693 - h_margin;
9694 hscroll
9695 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9696 }
9697 else
9698 {
9699 if (hscroll_relative_p)
9700 wanted_x = text_area_width * hscroll_step_rel
9701 + h_margin;
9702 else
9703 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9704 + h_margin;
9705 hscroll
9706 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9707 }
9708 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9709
9710 /* Don't call Fset_window_hscroll if value hasn't
9711 changed because it will prevent redisplay
9712 optimizations. */
9713 if (XFASTINT (w->hscroll) != hscroll)
9714 {
9715 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9716 w->hscroll = make_number (hscroll);
9717 hscrolled_p = 1;
9718 }
9719 }
9720 }
9721
9722 window = w->next;
9723 }
9724
9725 /* Value is non-zero if hscroll of any leaf window has been changed. */
9726 return hscrolled_p;
9727 }
9728
9729
9730 /* Set hscroll so that cursor is visible and not inside horizontal
9731 scroll margins for all windows in the tree rooted at WINDOW. See
9732 also hscroll_window_tree above. Value is non-zero if any window's
9733 hscroll has been changed. If it has, desired matrices on the frame
9734 of WINDOW are cleared. */
9735
9736 static int
9737 hscroll_windows (window)
9738 Lisp_Object window;
9739 {
9740 int hscrolled_p;
9741
9742 if (automatic_hscrolling_p)
9743 {
9744 hscrolled_p = hscroll_window_tree (window);
9745 if (hscrolled_p)
9746 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9747 }
9748 else
9749 hscrolled_p = 0;
9750 return hscrolled_p;
9751 }
9752
9753
9754 \f
9755 /************************************************************************
9756 Redisplay
9757 ************************************************************************/
9758
9759 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9760 to a non-zero value. This is sometimes handy to have in a debugger
9761 session. */
9762
9763 #if GLYPH_DEBUG
9764
9765 /* First and last unchanged row for try_window_id. */
9766
9767 int debug_first_unchanged_at_end_vpos;
9768 int debug_last_unchanged_at_beg_vpos;
9769
9770 /* Delta vpos and y. */
9771
9772 int debug_dvpos, debug_dy;
9773
9774 /* Delta in characters and bytes for try_window_id. */
9775
9776 int debug_delta, debug_delta_bytes;
9777
9778 /* Values of window_end_pos and window_end_vpos at the end of
9779 try_window_id. */
9780
9781 EMACS_INT debug_end_pos, debug_end_vpos;
9782
9783 /* Append a string to W->desired_matrix->method. FMT is a printf
9784 format string. A1...A9 are a supplement for a variable-length
9785 argument list. If trace_redisplay_p is non-zero also printf the
9786 resulting string to stderr. */
9787
9788 static void
9789 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9790 struct window *w;
9791 char *fmt;
9792 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9793 {
9794 char buffer[512];
9795 char *method = w->desired_matrix->method;
9796 int len = strlen (method);
9797 int size = sizeof w->desired_matrix->method;
9798 int remaining = size - len - 1;
9799
9800 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9801 if (len && remaining)
9802 {
9803 method[len] = '|';
9804 --remaining, ++len;
9805 }
9806
9807 strncpy (method + len, buffer, remaining);
9808
9809 if (trace_redisplay_p)
9810 fprintf (stderr, "%p (%s): %s\n",
9811 w,
9812 ((BUFFERP (w->buffer)
9813 && STRINGP (XBUFFER (w->buffer)->name))
9814 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9815 : "no buffer"),
9816 buffer);
9817 }
9818
9819 #endif /* GLYPH_DEBUG */
9820
9821
9822 /* Value is non-zero if all changes in window W, which displays
9823 current_buffer, are in the text between START and END. START is a
9824 buffer position, END is given as a distance from Z. Used in
9825 redisplay_internal for display optimization. */
9826
9827 static INLINE int
9828 text_outside_line_unchanged_p (w, start, end)
9829 struct window *w;
9830 int start, end;
9831 {
9832 int unchanged_p = 1;
9833
9834 /* If text or overlays have changed, see where. */
9835 if (XFASTINT (w->last_modified) < MODIFF
9836 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9837 {
9838 /* Gap in the line? */
9839 if (GPT < start || Z - GPT < end)
9840 unchanged_p = 0;
9841
9842 /* Changes start in front of the line, or end after it? */
9843 if (unchanged_p
9844 && (BEG_UNCHANGED < start - 1
9845 || END_UNCHANGED < end))
9846 unchanged_p = 0;
9847
9848 /* If selective display, can't optimize if changes start at the
9849 beginning of the line. */
9850 if (unchanged_p
9851 && INTEGERP (current_buffer->selective_display)
9852 && XINT (current_buffer->selective_display) > 0
9853 && (BEG_UNCHANGED < start || GPT <= start))
9854 unchanged_p = 0;
9855
9856 /* If there are overlays at the start or end of the line, these
9857 may have overlay strings with newlines in them. A change at
9858 START, for instance, may actually concern the display of such
9859 overlay strings as well, and they are displayed on different
9860 lines. So, quickly rule out this case. (For the future, it
9861 might be desirable to implement something more telling than
9862 just BEG/END_UNCHANGED.) */
9863 if (unchanged_p)
9864 {
9865 if (BEG + BEG_UNCHANGED == start
9866 && overlay_touches_p (start))
9867 unchanged_p = 0;
9868 if (END_UNCHANGED == end
9869 && overlay_touches_p (Z - end))
9870 unchanged_p = 0;
9871 }
9872 }
9873
9874 return unchanged_p;
9875 }
9876
9877
9878 /* Do a frame update, taking possible shortcuts into account. This is
9879 the main external entry point for redisplay.
9880
9881 If the last redisplay displayed an echo area message and that message
9882 is no longer requested, we clear the echo area or bring back the
9883 mini-buffer if that is in use. */
9884
9885 void
9886 redisplay ()
9887 {
9888 redisplay_internal (0);
9889 }
9890
9891
9892 static Lisp_Object
9893 overlay_arrow_string_or_property (var)
9894 Lisp_Object var;
9895 {
9896 Lisp_Object val;
9897
9898 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9899 return val;
9900
9901 return Voverlay_arrow_string;
9902 }
9903
9904 /* Return 1 if there are any overlay-arrows in current_buffer. */
9905 static int
9906 overlay_arrow_in_current_buffer_p ()
9907 {
9908 Lisp_Object vlist;
9909
9910 for (vlist = Voverlay_arrow_variable_list;
9911 CONSP (vlist);
9912 vlist = XCDR (vlist))
9913 {
9914 Lisp_Object var = XCAR (vlist);
9915 Lisp_Object val;
9916
9917 if (!SYMBOLP (var))
9918 continue;
9919 val = find_symbol_value (var);
9920 if (MARKERP (val)
9921 && current_buffer == XMARKER (val)->buffer)
9922 return 1;
9923 }
9924 return 0;
9925 }
9926
9927
9928 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9929 has changed. */
9930
9931 static int
9932 overlay_arrows_changed_p ()
9933 {
9934 Lisp_Object vlist;
9935
9936 for (vlist = Voverlay_arrow_variable_list;
9937 CONSP (vlist);
9938 vlist = XCDR (vlist))
9939 {
9940 Lisp_Object var = XCAR (vlist);
9941 Lisp_Object val, pstr;
9942
9943 if (!SYMBOLP (var))
9944 continue;
9945 val = find_symbol_value (var);
9946 if (!MARKERP (val))
9947 continue;
9948 if (! EQ (COERCE_MARKER (val),
9949 Fget (var, Qlast_arrow_position))
9950 || ! (pstr = overlay_arrow_string_or_property (var),
9951 EQ (pstr, Fget (var, Qlast_arrow_string))))
9952 return 1;
9953 }
9954 return 0;
9955 }
9956
9957 /* Mark overlay arrows to be updated on next redisplay. */
9958
9959 static void
9960 update_overlay_arrows (up_to_date)
9961 int up_to_date;
9962 {
9963 Lisp_Object vlist;
9964
9965 for (vlist = Voverlay_arrow_variable_list;
9966 CONSP (vlist);
9967 vlist = XCDR (vlist))
9968 {
9969 Lisp_Object var = XCAR (vlist);
9970
9971 if (!SYMBOLP (var))
9972 continue;
9973
9974 if (up_to_date > 0)
9975 {
9976 Lisp_Object val = find_symbol_value (var);
9977 Fput (var, Qlast_arrow_position,
9978 COERCE_MARKER (val));
9979 Fput (var, Qlast_arrow_string,
9980 overlay_arrow_string_or_property (var));
9981 }
9982 else if (up_to_date < 0
9983 || !NILP (Fget (var, Qlast_arrow_position)))
9984 {
9985 Fput (var, Qlast_arrow_position, Qt);
9986 Fput (var, Qlast_arrow_string, Qt);
9987 }
9988 }
9989 }
9990
9991
9992 /* Return overlay arrow string to display at row.
9993 Return integer (bitmap number) for arrow bitmap in left fringe.
9994 Return nil if no overlay arrow. */
9995
9996 static Lisp_Object
9997 overlay_arrow_at_row (it, row)
9998 struct it *it;
9999 struct glyph_row *row;
10000 {
10001 Lisp_Object vlist;
10002
10003 for (vlist = Voverlay_arrow_variable_list;
10004 CONSP (vlist);
10005 vlist = XCDR (vlist))
10006 {
10007 Lisp_Object var = XCAR (vlist);
10008 Lisp_Object val;
10009
10010 if (!SYMBOLP (var))
10011 continue;
10012
10013 val = find_symbol_value (var);
10014
10015 if (MARKERP (val)
10016 && current_buffer == XMARKER (val)->buffer
10017 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10018 {
10019 if (FRAME_WINDOW_P (it->f)
10020 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10021 {
10022 #ifdef HAVE_WINDOW_SYSTEM
10023 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10024 {
10025 int fringe_bitmap;
10026 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10027 return make_number (fringe_bitmap);
10028 }
10029 #endif
10030 return make_number (-1); /* Use default arrow bitmap */
10031 }
10032 return overlay_arrow_string_or_property (var);
10033 }
10034 }
10035
10036 return Qnil;
10037 }
10038
10039 /* Return 1 if point moved out of or into a composition. Otherwise
10040 return 0. PREV_BUF and PREV_PT are the last point buffer and
10041 position. BUF and PT are the current point buffer and position. */
10042
10043 int
10044 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10045 struct buffer *prev_buf, *buf;
10046 int prev_pt, pt;
10047 {
10048 int start, end;
10049 Lisp_Object prop;
10050 Lisp_Object buffer;
10051
10052 XSETBUFFER (buffer, buf);
10053 /* Check a composition at the last point if point moved within the
10054 same buffer. */
10055 if (prev_buf == buf)
10056 {
10057 if (prev_pt == pt)
10058 /* Point didn't move. */
10059 return 0;
10060
10061 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10062 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10063 && COMPOSITION_VALID_P (start, end, prop)
10064 && start < prev_pt && end > prev_pt)
10065 /* The last point was within the composition. Return 1 iff
10066 point moved out of the composition. */
10067 return (pt <= start || pt >= end);
10068 }
10069
10070 /* Check a composition at the current point. */
10071 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10072 && find_composition (pt, -1, &start, &end, &prop, buffer)
10073 && COMPOSITION_VALID_P (start, end, prop)
10074 && start < pt && end > pt);
10075 }
10076
10077
10078 /* Reconsider the setting of B->clip_changed which is displayed
10079 in window W. */
10080
10081 static INLINE void
10082 reconsider_clip_changes (w, b)
10083 struct window *w;
10084 struct buffer *b;
10085 {
10086 if (b->clip_changed
10087 && !NILP (w->window_end_valid)
10088 && w->current_matrix->buffer == b
10089 && w->current_matrix->zv == BUF_ZV (b)
10090 && w->current_matrix->begv == BUF_BEGV (b))
10091 b->clip_changed = 0;
10092
10093 /* If display wasn't paused, and W is not a tool bar window, see if
10094 point has been moved into or out of a composition. In that case,
10095 we set b->clip_changed to 1 to force updating the screen. If
10096 b->clip_changed has already been set to 1, we can skip this
10097 check. */
10098 if (!b->clip_changed
10099 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10100 {
10101 int pt;
10102
10103 if (w == XWINDOW (selected_window))
10104 pt = BUF_PT (current_buffer);
10105 else
10106 pt = marker_position (w->pointm);
10107
10108 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10109 || pt != XINT (w->last_point))
10110 && check_point_in_composition (w->current_matrix->buffer,
10111 XINT (w->last_point),
10112 XBUFFER (w->buffer), pt))
10113 b->clip_changed = 1;
10114 }
10115 }
10116 \f
10117
10118 /* Select FRAME to forward the values of frame-local variables into C
10119 variables so that the redisplay routines can access those values
10120 directly. */
10121
10122 static void
10123 select_frame_for_redisplay (frame)
10124 Lisp_Object frame;
10125 {
10126 Lisp_Object tail, sym, val;
10127 Lisp_Object old = selected_frame;
10128
10129 selected_frame = frame;
10130
10131 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10132 if (CONSP (XCAR (tail))
10133 && (sym = XCAR (XCAR (tail)),
10134 SYMBOLP (sym))
10135 && (sym = indirect_variable (sym),
10136 val = SYMBOL_VALUE (sym),
10137 (BUFFER_LOCAL_VALUEP (val)
10138 || SOME_BUFFER_LOCAL_VALUEP (val)))
10139 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10140 /* Use find_symbol_value rather than Fsymbol_value
10141 to avoid an error if it is void. */
10142 find_symbol_value (sym);
10143
10144 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10145 if (CONSP (XCAR (tail))
10146 && (sym = XCAR (XCAR (tail)),
10147 SYMBOLP (sym))
10148 && (sym = indirect_variable (sym),
10149 val = SYMBOL_VALUE (sym),
10150 (BUFFER_LOCAL_VALUEP (val)
10151 || SOME_BUFFER_LOCAL_VALUEP (val)))
10152 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10153 find_symbol_value (sym);
10154 }
10155
10156
10157 #define STOP_POLLING \
10158 do { if (! polling_stopped_here) stop_polling (); \
10159 polling_stopped_here = 1; } while (0)
10160
10161 #define RESUME_POLLING \
10162 do { if (polling_stopped_here) start_polling (); \
10163 polling_stopped_here = 0; } while (0)
10164
10165
10166 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10167 response to any user action; therefore, we should preserve the echo
10168 area. (Actually, our caller does that job.) Perhaps in the future
10169 avoid recentering windows if it is not necessary; currently that
10170 causes some problems. */
10171
10172 static void
10173 redisplay_internal (preserve_echo_area)
10174 int preserve_echo_area;
10175 {
10176 struct window *w = XWINDOW (selected_window);
10177 struct frame *f = XFRAME (w->frame);
10178 int pause;
10179 int must_finish = 0;
10180 struct text_pos tlbufpos, tlendpos;
10181 int number_of_visible_frames;
10182 int count;
10183 struct frame *sf = SELECTED_FRAME ();
10184 int polling_stopped_here = 0;
10185
10186 /* Non-zero means redisplay has to consider all windows on all
10187 frames. Zero means, only selected_window is considered. */
10188 int consider_all_windows_p;
10189
10190 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10191
10192 /* No redisplay if running in batch mode or frame is not yet fully
10193 initialized, or redisplay is explicitly turned off by setting
10194 Vinhibit_redisplay. */
10195 if (noninteractive
10196 || !NILP (Vinhibit_redisplay)
10197 || !f->glyphs_initialized_p)
10198 return;
10199
10200 /* The flag redisplay_performed_directly_p is set by
10201 direct_output_for_insert when it already did the whole screen
10202 update necessary. */
10203 if (redisplay_performed_directly_p)
10204 {
10205 redisplay_performed_directly_p = 0;
10206 if (!hscroll_windows (selected_window))
10207 return;
10208 }
10209
10210 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10211 if (popup_activated ())
10212 return;
10213 #endif
10214
10215 /* I don't think this happens but let's be paranoid. */
10216 if (redisplaying_p)
10217 return;
10218
10219 /* Record a function that resets redisplaying_p to its old value
10220 when we leave this function. */
10221 count = SPECPDL_INDEX ();
10222 record_unwind_protect (unwind_redisplay,
10223 Fcons (make_number (redisplaying_p), selected_frame));
10224 ++redisplaying_p;
10225 specbind (Qinhibit_free_realized_faces, Qnil);
10226
10227 {
10228 Lisp_Object tail, frame;
10229
10230 FOR_EACH_FRAME (tail, frame)
10231 {
10232 struct frame *f = XFRAME (frame);
10233 f->already_hscrolled_p = 0;
10234 }
10235 }
10236
10237 retry:
10238 pause = 0;
10239 reconsider_clip_changes (w, current_buffer);
10240
10241 /* If new fonts have been loaded that make a glyph matrix adjustment
10242 necessary, do it. */
10243 if (fonts_changed_p)
10244 {
10245 adjust_glyphs (NULL);
10246 ++windows_or_buffers_changed;
10247 fonts_changed_p = 0;
10248 }
10249
10250 /* If face_change_count is non-zero, init_iterator will free all
10251 realized faces, which includes the faces referenced from current
10252 matrices. So, we can't reuse current matrices in this case. */
10253 if (face_change_count)
10254 ++windows_or_buffers_changed;
10255
10256 if (! FRAME_WINDOW_P (sf)
10257 && previous_terminal_frame != sf)
10258 {
10259 /* Since frames on an ASCII terminal share the same display
10260 area, displaying a different frame means redisplay the whole
10261 thing. */
10262 windows_or_buffers_changed++;
10263 SET_FRAME_GARBAGED (sf);
10264 XSETFRAME (Vterminal_frame, sf);
10265 }
10266 previous_terminal_frame = sf;
10267
10268 /* Set the visible flags for all frames. Do this before checking
10269 for resized or garbaged frames; they want to know if their frames
10270 are visible. See the comment in frame.h for
10271 FRAME_SAMPLE_VISIBILITY. */
10272 {
10273 Lisp_Object tail, frame;
10274
10275 number_of_visible_frames = 0;
10276
10277 FOR_EACH_FRAME (tail, frame)
10278 {
10279 struct frame *f = XFRAME (frame);
10280
10281 FRAME_SAMPLE_VISIBILITY (f);
10282 if (FRAME_VISIBLE_P (f))
10283 ++number_of_visible_frames;
10284 clear_desired_matrices (f);
10285 }
10286 }
10287
10288 /* Notice any pending interrupt request to change frame size. */
10289 do_pending_window_change (1);
10290
10291 /* Clear frames marked as garbaged. */
10292 if (frame_garbaged)
10293 clear_garbaged_frames ();
10294
10295 /* Build menubar and tool-bar items. */
10296 prepare_menu_bars ();
10297
10298 if (windows_or_buffers_changed)
10299 update_mode_lines++;
10300
10301 /* Detect case that we need to write or remove a star in the mode line. */
10302 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10303 {
10304 w->update_mode_line = Qt;
10305 if (buffer_shared > 1)
10306 update_mode_lines++;
10307 }
10308
10309 /* If %c is in the mode line, update it if needed. */
10310 if (!NILP (w->column_number_displayed)
10311 /* This alternative quickly identifies a common case
10312 where no change is needed. */
10313 && !(PT == XFASTINT (w->last_point)
10314 && XFASTINT (w->last_modified) >= MODIFF
10315 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10316 && (XFASTINT (w->column_number_displayed)
10317 != (int) current_column ())) /* iftc */
10318 w->update_mode_line = Qt;
10319
10320 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10321
10322 /* The variable buffer_shared is set in redisplay_window and
10323 indicates that we redisplay a buffer in different windows. See
10324 there. */
10325 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10326 || cursor_type_changed);
10327
10328 /* If specs for an arrow have changed, do thorough redisplay
10329 to ensure we remove any arrow that should no longer exist. */
10330 if (overlay_arrows_changed_p ())
10331 consider_all_windows_p = windows_or_buffers_changed = 1;
10332
10333 /* Normally the message* functions will have already displayed and
10334 updated the echo area, but the frame may have been trashed, or
10335 the update may have been preempted, so display the echo area
10336 again here. Checking message_cleared_p captures the case that
10337 the echo area should be cleared. */
10338 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10339 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10340 || (message_cleared_p
10341 && minibuf_level == 0
10342 /* If the mini-window is currently selected, this means the
10343 echo-area doesn't show through. */
10344 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10345 {
10346 int window_height_changed_p = echo_area_display (0);
10347 must_finish = 1;
10348
10349 /* If we don't display the current message, don't clear the
10350 message_cleared_p flag, because, if we did, we wouldn't clear
10351 the echo area in the next redisplay which doesn't preserve
10352 the echo area. */
10353 if (!display_last_displayed_message_p)
10354 message_cleared_p = 0;
10355
10356 if (fonts_changed_p)
10357 goto retry;
10358 else if (window_height_changed_p)
10359 {
10360 consider_all_windows_p = 1;
10361 ++update_mode_lines;
10362 ++windows_or_buffers_changed;
10363
10364 /* If window configuration was changed, frames may have been
10365 marked garbaged. Clear them or we will experience
10366 surprises wrt scrolling. */
10367 if (frame_garbaged)
10368 clear_garbaged_frames ();
10369 }
10370 }
10371 else if (EQ (selected_window, minibuf_window)
10372 && (current_buffer->clip_changed
10373 || XFASTINT (w->last_modified) < MODIFF
10374 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10375 && resize_mini_window (w, 0))
10376 {
10377 /* Resized active mini-window to fit the size of what it is
10378 showing if its contents might have changed. */
10379 must_finish = 1;
10380 consider_all_windows_p = 1;
10381 ++windows_or_buffers_changed;
10382 ++update_mode_lines;
10383
10384 /* If window configuration was changed, frames may have been
10385 marked garbaged. Clear them or we will experience
10386 surprises wrt scrolling. */
10387 if (frame_garbaged)
10388 clear_garbaged_frames ();
10389 }
10390
10391
10392 /* If showing the region, and mark has changed, we must redisplay
10393 the whole window. The assignment to this_line_start_pos prevents
10394 the optimization directly below this if-statement. */
10395 if (((!NILP (Vtransient_mark_mode)
10396 && !NILP (XBUFFER (w->buffer)->mark_active))
10397 != !NILP (w->region_showing))
10398 || (!NILP (w->region_showing)
10399 && !EQ (w->region_showing,
10400 Fmarker_position (XBUFFER (w->buffer)->mark))))
10401 CHARPOS (this_line_start_pos) = 0;
10402
10403 /* Optimize the case that only the line containing the cursor in the
10404 selected window has changed. Variables starting with this_ are
10405 set in display_line and record information about the line
10406 containing the cursor. */
10407 tlbufpos = this_line_start_pos;
10408 tlendpos = this_line_end_pos;
10409 if (!consider_all_windows_p
10410 && CHARPOS (tlbufpos) > 0
10411 && NILP (w->update_mode_line)
10412 && !current_buffer->clip_changed
10413 && !current_buffer->prevent_redisplay_optimizations_p
10414 && FRAME_VISIBLE_P (XFRAME (w->frame))
10415 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10416 /* Make sure recorded data applies to current buffer, etc. */
10417 && this_line_buffer == current_buffer
10418 && current_buffer == XBUFFER (w->buffer)
10419 && NILP (w->force_start)
10420 && NILP (w->optional_new_start)
10421 /* Point must be on the line that we have info recorded about. */
10422 && PT >= CHARPOS (tlbufpos)
10423 && PT <= Z - CHARPOS (tlendpos)
10424 /* All text outside that line, including its final newline,
10425 must be unchanged */
10426 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10427 CHARPOS (tlendpos)))
10428 {
10429 if (CHARPOS (tlbufpos) > BEGV
10430 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10431 && (CHARPOS (tlbufpos) == ZV
10432 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10433 /* Former continuation line has disappeared by becoming empty */
10434 goto cancel;
10435 else if (XFASTINT (w->last_modified) < MODIFF
10436 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10437 || MINI_WINDOW_P (w))
10438 {
10439 /* We have to handle the case of continuation around a
10440 wide-column character (See the comment in indent.c around
10441 line 885).
10442
10443 For instance, in the following case:
10444
10445 -------- Insert --------
10446 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10447 J_I_ ==> J_I_ `^^' are cursors.
10448 ^^ ^^
10449 -------- --------
10450
10451 As we have to redraw the line above, we should goto cancel. */
10452
10453 struct it it;
10454 int line_height_before = this_line_pixel_height;
10455
10456 /* Note that start_display will handle the case that the
10457 line starting at tlbufpos is a continuation lines. */
10458 start_display (&it, w, tlbufpos);
10459
10460 /* Implementation note: It this still necessary? */
10461 if (it.current_x != this_line_start_x)
10462 goto cancel;
10463
10464 TRACE ((stderr, "trying display optimization 1\n"));
10465 w->cursor.vpos = -1;
10466 overlay_arrow_seen = 0;
10467 it.vpos = this_line_vpos;
10468 it.current_y = this_line_y;
10469 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10470 display_line (&it);
10471
10472 /* If line contains point, is not continued,
10473 and ends at same distance from eob as before, we win */
10474 if (w->cursor.vpos >= 0
10475 /* Line is not continued, otherwise this_line_start_pos
10476 would have been set to 0 in display_line. */
10477 && CHARPOS (this_line_start_pos)
10478 /* Line ends as before. */
10479 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10480 /* Line has same height as before. Otherwise other lines
10481 would have to be shifted up or down. */
10482 && this_line_pixel_height == line_height_before)
10483 {
10484 /* If this is not the window's last line, we must adjust
10485 the charstarts of the lines below. */
10486 if (it.current_y < it.last_visible_y)
10487 {
10488 struct glyph_row *row
10489 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10490 int delta, delta_bytes;
10491
10492 if (Z - CHARPOS (tlendpos) == ZV)
10493 {
10494 /* This line ends at end of (accessible part of)
10495 buffer. There is no newline to count. */
10496 delta = (Z
10497 - CHARPOS (tlendpos)
10498 - MATRIX_ROW_START_CHARPOS (row));
10499 delta_bytes = (Z_BYTE
10500 - BYTEPOS (tlendpos)
10501 - MATRIX_ROW_START_BYTEPOS (row));
10502 }
10503 else
10504 {
10505 /* This line ends in a newline. Must take
10506 account of the newline and the rest of the
10507 text that follows. */
10508 delta = (Z
10509 - CHARPOS (tlendpos)
10510 - MATRIX_ROW_START_CHARPOS (row));
10511 delta_bytes = (Z_BYTE
10512 - BYTEPOS (tlendpos)
10513 - MATRIX_ROW_START_BYTEPOS (row));
10514 }
10515
10516 increment_matrix_positions (w->current_matrix,
10517 this_line_vpos + 1,
10518 w->current_matrix->nrows,
10519 delta, delta_bytes);
10520 }
10521
10522 /* If this row displays text now but previously didn't,
10523 or vice versa, w->window_end_vpos may have to be
10524 adjusted. */
10525 if ((it.glyph_row - 1)->displays_text_p)
10526 {
10527 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10528 XSETINT (w->window_end_vpos, this_line_vpos);
10529 }
10530 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10531 && this_line_vpos > 0)
10532 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10533 w->window_end_valid = Qnil;
10534
10535 /* Update hint: No need to try to scroll in update_window. */
10536 w->desired_matrix->no_scrolling_p = 1;
10537
10538 #if GLYPH_DEBUG
10539 *w->desired_matrix->method = 0;
10540 debug_method_add (w, "optimization 1");
10541 #endif
10542 #ifdef HAVE_WINDOW_SYSTEM
10543 update_window_fringes (w, 0);
10544 #endif
10545 goto update;
10546 }
10547 else
10548 goto cancel;
10549 }
10550 else if (/* Cursor position hasn't changed. */
10551 PT == XFASTINT (w->last_point)
10552 /* Make sure the cursor was last displayed
10553 in this window. Otherwise we have to reposition it. */
10554 && 0 <= w->cursor.vpos
10555 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10556 {
10557 if (!must_finish)
10558 {
10559 do_pending_window_change (1);
10560
10561 /* We used to always goto end_of_redisplay here, but this
10562 isn't enough if we have a blinking cursor. */
10563 if (w->cursor_off_p == w->last_cursor_off_p)
10564 goto end_of_redisplay;
10565 }
10566 goto update;
10567 }
10568 /* If highlighting the region, or if the cursor is in the echo area,
10569 then we can't just move the cursor. */
10570 else if (! (!NILP (Vtransient_mark_mode)
10571 && !NILP (current_buffer->mark_active))
10572 && (EQ (selected_window, current_buffer->last_selected_window)
10573 || highlight_nonselected_windows)
10574 && NILP (w->region_showing)
10575 && NILP (Vshow_trailing_whitespace)
10576 && !cursor_in_echo_area)
10577 {
10578 struct it it;
10579 struct glyph_row *row;
10580
10581 /* Skip from tlbufpos to PT and see where it is. Note that
10582 PT may be in invisible text. If so, we will end at the
10583 next visible position. */
10584 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10585 NULL, DEFAULT_FACE_ID);
10586 it.current_x = this_line_start_x;
10587 it.current_y = this_line_y;
10588 it.vpos = this_line_vpos;
10589
10590 /* The call to move_it_to stops in front of PT, but
10591 moves over before-strings. */
10592 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10593
10594 if (it.vpos == this_line_vpos
10595 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10596 row->enabled_p))
10597 {
10598 xassert (this_line_vpos == it.vpos);
10599 xassert (this_line_y == it.current_y);
10600 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10601 #if GLYPH_DEBUG
10602 *w->desired_matrix->method = 0;
10603 debug_method_add (w, "optimization 3");
10604 #endif
10605 goto update;
10606 }
10607 else
10608 goto cancel;
10609 }
10610
10611 cancel:
10612 /* Text changed drastically or point moved off of line. */
10613 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10614 }
10615
10616 CHARPOS (this_line_start_pos) = 0;
10617 consider_all_windows_p |= buffer_shared > 1;
10618 ++clear_face_cache_count;
10619 #ifdef HAVE_WINDOW_SYSTEM
10620 ++clear_image_cache_count;
10621 #endif
10622
10623 /* Build desired matrices, and update the display. If
10624 consider_all_windows_p is non-zero, do it for all windows on all
10625 frames. Otherwise do it for selected_window, only. */
10626
10627 if (consider_all_windows_p)
10628 {
10629 Lisp_Object tail, frame;
10630
10631 FOR_EACH_FRAME (tail, frame)
10632 XFRAME (frame)->updated_p = 0;
10633
10634 /* Recompute # windows showing selected buffer. This will be
10635 incremented each time such a window is displayed. */
10636 buffer_shared = 0;
10637
10638 FOR_EACH_FRAME (tail, frame)
10639 {
10640 struct frame *f = XFRAME (frame);
10641
10642 if (FRAME_WINDOW_P (f) || f == sf)
10643 {
10644 if (! EQ (frame, selected_frame))
10645 /* Select the frame, for the sake of frame-local
10646 variables. */
10647 select_frame_for_redisplay (frame);
10648
10649 /* Mark all the scroll bars to be removed; we'll redeem
10650 the ones we want when we redisplay their windows. */
10651 if (condemn_scroll_bars_hook)
10652 condemn_scroll_bars_hook (f);
10653
10654 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10655 redisplay_windows (FRAME_ROOT_WINDOW (f));
10656
10657 /* Any scroll bars which redisplay_windows should have
10658 nuked should now go away. */
10659 if (judge_scroll_bars_hook)
10660 judge_scroll_bars_hook (f);
10661
10662 /* If fonts changed, display again. */
10663 /* ??? rms: I suspect it is a mistake to jump all the way
10664 back to retry here. It should just retry this frame. */
10665 if (fonts_changed_p)
10666 goto retry;
10667
10668 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10669 {
10670 /* See if we have to hscroll. */
10671 if (!f->already_hscrolled_p)
10672 {
10673 f->already_hscrolled_p = 1;
10674 if (hscroll_windows (f->root_window))
10675 goto retry;
10676 }
10677
10678 /* Prevent various kinds of signals during display
10679 update. stdio is not robust about handling
10680 signals, which can cause an apparent I/O
10681 error. */
10682 if (interrupt_input)
10683 unrequest_sigio ();
10684 STOP_POLLING;
10685
10686 /* Update the display. */
10687 set_window_update_flags (XWINDOW (f->root_window), 1);
10688 pause |= update_frame (f, 0, 0);
10689 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10690 if (pause)
10691 break;
10692 #endif
10693
10694 f->updated_p = 1;
10695 }
10696 }
10697 }
10698
10699 if (!pause)
10700 {
10701 /* Do the mark_window_display_accurate after all windows have
10702 been redisplayed because this call resets flags in buffers
10703 which are needed for proper redisplay. */
10704 FOR_EACH_FRAME (tail, frame)
10705 {
10706 struct frame *f = XFRAME (frame);
10707 if (f->updated_p)
10708 {
10709 mark_window_display_accurate (f->root_window, 1);
10710 if (frame_up_to_date_hook)
10711 frame_up_to_date_hook (f);
10712 }
10713 }
10714 }
10715 }
10716 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10717 {
10718 Lisp_Object mini_window;
10719 struct frame *mini_frame;
10720
10721 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10722 /* Use list_of_error, not Qerror, so that
10723 we catch only errors and don't run the debugger. */
10724 internal_condition_case_1 (redisplay_window_1, selected_window,
10725 list_of_error,
10726 redisplay_window_error);
10727
10728 /* Compare desired and current matrices, perform output. */
10729
10730 update:
10731 /* If fonts changed, display again. */
10732 if (fonts_changed_p)
10733 goto retry;
10734
10735 /* Prevent various kinds of signals during display update.
10736 stdio is not robust about handling signals,
10737 which can cause an apparent I/O error. */
10738 if (interrupt_input)
10739 unrequest_sigio ();
10740 STOP_POLLING;
10741
10742 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10743 {
10744 if (hscroll_windows (selected_window))
10745 goto retry;
10746
10747 XWINDOW (selected_window)->must_be_updated_p = 1;
10748 pause = update_frame (sf, 0, 0);
10749 }
10750
10751 /* We may have called echo_area_display at the top of this
10752 function. If the echo area is on another frame, that may
10753 have put text on a frame other than the selected one, so the
10754 above call to update_frame would not have caught it. Catch
10755 it here. */
10756 mini_window = FRAME_MINIBUF_WINDOW (sf);
10757 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10758
10759 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10760 {
10761 XWINDOW (mini_window)->must_be_updated_p = 1;
10762 pause |= update_frame (mini_frame, 0, 0);
10763 if (!pause && hscroll_windows (mini_window))
10764 goto retry;
10765 }
10766 }
10767
10768 /* If display was paused because of pending input, make sure we do a
10769 thorough update the next time. */
10770 if (pause)
10771 {
10772 /* Prevent the optimization at the beginning of
10773 redisplay_internal that tries a single-line update of the
10774 line containing the cursor in the selected window. */
10775 CHARPOS (this_line_start_pos) = 0;
10776
10777 /* Let the overlay arrow be updated the next time. */
10778 update_overlay_arrows (0);
10779
10780 /* If we pause after scrolling, some rows in the current
10781 matrices of some windows are not valid. */
10782 if (!WINDOW_FULL_WIDTH_P (w)
10783 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10784 update_mode_lines = 1;
10785 }
10786 else
10787 {
10788 if (!consider_all_windows_p)
10789 {
10790 /* This has already been done above if
10791 consider_all_windows_p is set. */
10792 mark_window_display_accurate_1 (w, 1);
10793
10794 /* Say overlay arrows are up to date. */
10795 update_overlay_arrows (1);
10796
10797 if (frame_up_to_date_hook != 0)
10798 frame_up_to_date_hook (sf);
10799 }
10800
10801 update_mode_lines = 0;
10802 windows_or_buffers_changed = 0;
10803 cursor_type_changed = 0;
10804 }
10805
10806 /* Start SIGIO interrupts coming again. Having them off during the
10807 code above makes it less likely one will discard output, but not
10808 impossible, since there might be stuff in the system buffer here.
10809 But it is much hairier to try to do anything about that. */
10810 if (interrupt_input)
10811 request_sigio ();
10812 RESUME_POLLING;
10813
10814 /* If a frame has become visible which was not before, redisplay
10815 again, so that we display it. Expose events for such a frame
10816 (which it gets when becoming visible) don't call the parts of
10817 redisplay constructing glyphs, so simply exposing a frame won't
10818 display anything in this case. So, we have to display these
10819 frames here explicitly. */
10820 if (!pause)
10821 {
10822 Lisp_Object tail, frame;
10823 int new_count = 0;
10824
10825 FOR_EACH_FRAME (tail, frame)
10826 {
10827 int this_is_visible = 0;
10828
10829 if (XFRAME (frame)->visible)
10830 this_is_visible = 1;
10831 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10832 if (XFRAME (frame)->visible)
10833 this_is_visible = 1;
10834
10835 if (this_is_visible)
10836 new_count++;
10837 }
10838
10839 if (new_count != number_of_visible_frames)
10840 windows_or_buffers_changed++;
10841 }
10842
10843 /* Change frame size now if a change is pending. */
10844 do_pending_window_change (1);
10845
10846 /* If we just did a pending size change, or have additional
10847 visible frames, redisplay again. */
10848 if (windows_or_buffers_changed && !pause)
10849 goto retry;
10850
10851 /* Clear the face cache eventually. */
10852 if (consider_all_windows_p)
10853 {
10854 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10855 {
10856 clear_face_cache (0);
10857 clear_face_cache_count = 0;
10858 }
10859 #ifdef HAVE_WINDOW_SYSTEM
10860 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10861 {
10862 Lisp_Object tail, frame;
10863 FOR_EACH_FRAME (tail, frame)
10864 {
10865 struct frame *f = XFRAME (frame);
10866 if (FRAME_WINDOW_P (f))
10867 clear_image_cache (f, 0);
10868 }
10869 clear_image_cache_count = 0;
10870 }
10871 #endif /* HAVE_WINDOW_SYSTEM */
10872 }
10873
10874 end_of_redisplay:
10875 unbind_to (count, Qnil);
10876 RESUME_POLLING;
10877 }
10878
10879
10880 /* Redisplay, but leave alone any recent echo area message unless
10881 another message has been requested in its place.
10882
10883 This is useful in situations where you need to redisplay but no
10884 user action has occurred, making it inappropriate for the message
10885 area to be cleared. See tracking_off and
10886 wait_reading_process_output for examples of these situations.
10887
10888 FROM_WHERE is an integer saying from where this function was
10889 called. This is useful for debugging. */
10890
10891 void
10892 redisplay_preserve_echo_area (from_where)
10893 int from_where;
10894 {
10895 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10896
10897 if (!NILP (echo_area_buffer[1]))
10898 {
10899 /* We have a previously displayed message, but no current
10900 message. Redisplay the previous message. */
10901 display_last_displayed_message_p = 1;
10902 redisplay_internal (1);
10903 display_last_displayed_message_p = 0;
10904 }
10905 else
10906 redisplay_internal (1);
10907
10908 if (rif != NULL && rif->flush_display_optional)
10909 rif->flush_display_optional (NULL);
10910 }
10911
10912
10913 /* Function registered with record_unwind_protect in
10914 redisplay_internal. Reset redisplaying_p to the value it had
10915 before redisplay_internal was called, and clear
10916 prevent_freeing_realized_faces_p. It also selects the previously
10917 selected frame. */
10918
10919 static Lisp_Object
10920 unwind_redisplay (val)
10921 Lisp_Object val;
10922 {
10923 Lisp_Object old_redisplaying_p, old_frame;
10924
10925 old_redisplaying_p = XCAR (val);
10926 redisplaying_p = XFASTINT (old_redisplaying_p);
10927 old_frame = XCDR (val);
10928 if (! EQ (old_frame, selected_frame))
10929 select_frame_for_redisplay (old_frame);
10930 return Qnil;
10931 }
10932
10933
10934 /* Mark the display of window W as accurate or inaccurate. If
10935 ACCURATE_P is non-zero mark display of W as accurate. If
10936 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10937 redisplay_internal is called. */
10938
10939 static void
10940 mark_window_display_accurate_1 (w, accurate_p)
10941 struct window *w;
10942 int accurate_p;
10943 {
10944 if (BUFFERP (w->buffer))
10945 {
10946 struct buffer *b = XBUFFER (w->buffer);
10947
10948 w->last_modified
10949 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10950 w->last_overlay_modified
10951 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10952 w->last_had_star
10953 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10954
10955 if (accurate_p)
10956 {
10957 b->clip_changed = 0;
10958 b->prevent_redisplay_optimizations_p = 0;
10959
10960 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10961 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10962 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10963 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10964
10965 w->current_matrix->buffer = b;
10966 w->current_matrix->begv = BUF_BEGV (b);
10967 w->current_matrix->zv = BUF_ZV (b);
10968
10969 w->last_cursor = w->cursor;
10970 w->last_cursor_off_p = w->cursor_off_p;
10971
10972 if (w == XWINDOW (selected_window))
10973 w->last_point = make_number (BUF_PT (b));
10974 else
10975 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10976 }
10977 }
10978
10979 if (accurate_p)
10980 {
10981 w->window_end_valid = w->buffer;
10982 #if 0 /* This is incorrect with variable-height lines. */
10983 xassert (XINT (w->window_end_vpos)
10984 < (WINDOW_TOTAL_LINES (w)
10985 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10986 #endif
10987 w->update_mode_line = Qnil;
10988 }
10989 }
10990
10991
10992 /* Mark the display of windows in the window tree rooted at WINDOW as
10993 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10994 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10995 be redisplayed the next time redisplay_internal is called. */
10996
10997 void
10998 mark_window_display_accurate (window, accurate_p)
10999 Lisp_Object window;
11000 int accurate_p;
11001 {
11002 struct window *w;
11003
11004 for (; !NILP (window); window = w->next)
11005 {
11006 w = XWINDOW (window);
11007 mark_window_display_accurate_1 (w, accurate_p);
11008
11009 if (!NILP (w->vchild))
11010 mark_window_display_accurate (w->vchild, accurate_p);
11011 if (!NILP (w->hchild))
11012 mark_window_display_accurate (w->hchild, accurate_p);
11013 }
11014
11015 if (accurate_p)
11016 {
11017 update_overlay_arrows (1);
11018 }
11019 else
11020 {
11021 /* Force a thorough redisplay the next time by setting
11022 last_arrow_position and last_arrow_string to t, which is
11023 unequal to any useful value of Voverlay_arrow_... */
11024 update_overlay_arrows (-1);
11025 }
11026 }
11027
11028
11029 /* Return value in display table DP (Lisp_Char_Table *) for character
11030 C. Since a display table doesn't have any parent, we don't have to
11031 follow parent. Do not call this function directly but use the
11032 macro DISP_CHAR_VECTOR. */
11033
11034 Lisp_Object
11035 disp_char_vector (dp, c)
11036 struct Lisp_Char_Table *dp;
11037 int c;
11038 {
11039 int code[4], i;
11040 Lisp_Object val;
11041
11042 if (SINGLE_BYTE_CHAR_P (c))
11043 return (dp->contents[c]);
11044
11045 SPLIT_CHAR (c, code[0], code[1], code[2]);
11046 if (code[1] < 32)
11047 code[1] = -1;
11048 else if (code[2] < 32)
11049 code[2] = -1;
11050
11051 /* Here, the possible range of code[0] (== charset ID) is
11052 128..max_charset. Since the top level char table contains data
11053 for multibyte characters after 256th element, we must increment
11054 code[0] by 128 to get a correct index. */
11055 code[0] += 128;
11056 code[3] = -1; /* anchor */
11057
11058 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11059 {
11060 val = dp->contents[code[i]];
11061 if (!SUB_CHAR_TABLE_P (val))
11062 return (NILP (val) ? dp->defalt : val);
11063 }
11064
11065 /* Here, val is a sub char table. We return the default value of
11066 it. */
11067 return (dp->defalt);
11068 }
11069
11070
11071 \f
11072 /***********************************************************************
11073 Window Redisplay
11074 ***********************************************************************/
11075
11076 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11077
11078 static void
11079 redisplay_windows (window)
11080 Lisp_Object window;
11081 {
11082 while (!NILP (window))
11083 {
11084 struct window *w = XWINDOW (window);
11085
11086 if (!NILP (w->hchild))
11087 redisplay_windows (w->hchild);
11088 else if (!NILP (w->vchild))
11089 redisplay_windows (w->vchild);
11090 else
11091 {
11092 displayed_buffer = XBUFFER (w->buffer);
11093 /* Use list_of_error, not Qerror, so that
11094 we catch only errors and don't run the debugger. */
11095 internal_condition_case_1 (redisplay_window_0, window,
11096 list_of_error,
11097 redisplay_window_error);
11098 }
11099
11100 window = w->next;
11101 }
11102 }
11103
11104 static Lisp_Object
11105 redisplay_window_error ()
11106 {
11107 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11108 return Qnil;
11109 }
11110
11111 static Lisp_Object
11112 redisplay_window_0 (window)
11113 Lisp_Object window;
11114 {
11115 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11116 redisplay_window (window, 0);
11117 return Qnil;
11118 }
11119
11120 static Lisp_Object
11121 redisplay_window_1 (window)
11122 Lisp_Object window;
11123 {
11124 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11125 redisplay_window (window, 1);
11126 return Qnil;
11127 }
11128 \f
11129
11130 /* Increment GLYPH until it reaches END or CONDITION fails while
11131 adding (GLYPH)->pixel_width to X. */
11132
11133 #define SKIP_GLYPHS(glyph, end, x, condition) \
11134 do \
11135 { \
11136 (x) += (glyph)->pixel_width; \
11137 ++(glyph); \
11138 } \
11139 while ((glyph) < (end) && (condition))
11140
11141
11142 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11143 DELTA is the number of bytes by which positions recorded in ROW
11144 differ from current buffer positions. */
11145
11146 void
11147 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11148 struct window *w;
11149 struct glyph_row *row;
11150 struct glyph_matrix *matrix;
11151 int delta, delta_bytes, dy, dvpos;
11152 {
11153 struct glyph *glyph = row->glyphs[TEXT_AREA];
11154 struct glyph *end = glyph + row->used[TEXT_AREA];
11155 struct glyph *cursor = NULL;
11156 /* The first glyph that starts a sequence of glyphs from string. */
11157 struct glyph *string_start;
11158 /* The X coordinate of string_start. */
11159 int string_start_x;
11160 /* The last known character position. */
11161 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11162 /* The last known character position before string_start. */
11163 int string_before_pos;
11164 int x = row->x;
11165 int cursor_x = x;
11166 int cursor_from_overlay_pos = 0;
11167 int pt_old = PT - delta;
11168
11169 /* Skip over glyphs not having an object at the start of the row.
11170 These are special glyphs like truncation marks on terminal
11171 frames. */
11172 if (row->displays_text_p)
11173 while (glyph < end
11174 && INTEGERP (glyph->object)
11175 && glyph->charpos < 0)
11176 {
11177 x += glyph->pixel_width;
11178 ++glyph;
11179 }
11180
11181 string_start = NULL;
11182 while (glyph < end
11183 && !INTEGERP (glyph->object)
11184 && (!BUFFERP (glyph->object)
11185 || (last_pos = glyph->charpos) < pt_old))
11186 {
11187 if (! STRINGP (glyph->object))
11188 {
11189 string_start = NULL;
11190 x += glyph->pixel_width;
11191 ++glyph;
11192 if (cursor_from_overlay_pos
11193 && last_pos > cursor_from_overlay_pos)
11194 {
11195 cursor_from_overlay_pos = 0;
11196 cursor = 0;
11197 }
11198 }
11199 else
11200 {
11201 string_before_pos = last_pos;
11202 string_start = glyph;
11203 string_start_x = x;
11204 /* Skip all glyphs from string. */
11205 do
11206 {
11207 int pos;
11208 if ((cursor == NULL || glyph > cursor)
11209 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11210 Qcursor, (glyph)->object))
11211 && (pos = string_buffer_position (w, glyph->object,
11212 string_before_pos),
11213 (pos == 0 /* From overlay */
11214 || pos == pt_old)))
11215 {
11216 /* Estimate overlay buffer position from the buffer
11217 positions of the glyphs before and after the overlay.
11218 Add 1 to last_pos so that if point corresponds to the
11219 glyph right after the overlay, we still use a 'cursor'
11220 property found in that overlay. */
11221 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11222 cursor = glyph;
11223 cursor_x = x;
11224 }
11225 x += glyph->pixel_width;
11226 ++glyph;
11227 }
11228 while (glyph < end && STRINGP (glyph->object));
11229 }
11230 }
11231
11232 if (cursor != NULL)
11233 {
11234 glyph = cursor;
11235 x = cursor_x;
11236 }
11237 else if (row->ends_in_ellipsis_p && glyph == end)
11238 {
11239 /* Scan back over the ellipsis glyphs, decrementing positions. */
11240 while (glyph > row->glyphs[TEXT_AREA]
11241 && (glyph - 1)->charpos == last_pos)
11242 glyph--, x -= glyph->pixel_width;
11243 /* That loop always goes one position too far,
11244 including the glyph before the ellipsis.
11245 So scan forward over that one. */
11246 x += glyph->pixel_width;
11247 glyph++;
11248 }
11249 else if (string_start
11250 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11251 {
11252 /* We may have skipped over point because the previous glyphs
11253 are from string. As there's no easy way to know the
11254 character position of the current glyph, find the correct
11255 glyph on point by scanning from string_start again. */
11256 Lisp_Object limit;
11257 Lisp_Object string;
11258 int pos;
11259
11260 limit = make_number (pt_old + 1);
11261 end = glyph;
11262 glyph = string_start;
11263 x = string_start_x;
11264 string = glyph->object;
11265 pos = string_buffer_position (w, string, string_before_pos);
11266 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11267 because we always put cursor after overlay strings. */
11268 while (pos == 0 && glyph < end)
11269 {
11270 string = glyph->object;
11271 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11272 if (glyph < end)
11273 pos = string_buffer_position (w, glyph->object, string_before_pos);
11274 }
11275
11276 while (glyph < end)
11277 {
11278 pos = XINT (Fnext_single_char_property_change
11279 (make_number (pos), Qdisplay, Qnil, limit));
11280 if (pos > pt_old)
11281 break;
11282 /* Skip glyphs from the same string. */
11283 string = glyph->object;
11284 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11285 /* Skip glyphs from an overlay. */
11286 while (glyph < end
11287 && ! string_buffer_position (w, glyph->object, pos))
11288 {
11289 string = glyph->object;
11290 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11291 }
11292 }
11293 }
11294
11295 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11296 w->cursor.x = x;
11297 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11298 w->cursor.y = row->y + dy;
11299
11300 if (w == XWINDOW (selected_window))
11301 {
11302 if (!row->continued_p
11303 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11304 && row->x == 0)
11305 {
11306 this_line_buffer = XBUFFER (w->buffer);
11307
11308 CHARPOS (this_line_start_pos)
11309 = MATRIX_ROW_START_CHARPOS (row) + delta;
11310 BYTEPOS (this_line_start_pos)
11311 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11312
11313 CHARPOS (this_line_end_pos)
11314 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11315 BYTEPOS (this_line_end_pos)
11316 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11317
11318 this_line_y = w->cursor.y;
11319 this_line_pixel_height = row->height;
11320 this_line_vpos = w->cursor.vpos;
11321 this_line_start_x = row->x;
11322 }
11323 else
11324 CHARPOS (this_line_start_pos) = 0;
11325 }
11326 }
11327
11328
11329 /* Run window scroll functions, if any, for WINDOW with new window
11330 start STARTP. Sets the window start of WINDOW to that position.
11331
11332 We assume that the window's buffer is really current. */
11333
11334 static INLINE struct text_pos
11335 run_window_scroll_functions (window, startp)
11336 Lisp_Object window;
11337 struct text_pos startp;
11338 {
11339 struct window *w = XWINDOW (window);
11340 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11341
11342 if (current_buffer != XBUFFER (w->buffer))
11343 abort ();
11344
11345 if (!NILP (Vwindow_scroll_functions))
11346 {
11347 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11348 make_number (CHARPOS (startp)));
11349 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11350 /* In case the hook functions switch buffers. */
11351 if (current_buffer != XBUFFER (w->buffer))
11352 set_buffer_internal_1 (XBUFFER (w->buffer));
11353 }
11354
11355 return startp;
11356 }
11357
11358
11359 /* Make sure the line containing the cursor is fully visible.
11360 A value of 1 means there is nothing to be done.
11361 (Either the line is fully visible, or it cannot be made so,
11362 or we cannot tell.)
11363
11364 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11365 is higher than window.
11366
11367 A value of 0 means the caller should do scrolling
11368 as if point had gone off the screen. */
11369
11370 static int
11371 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11372 struct window *w;
11373 int force_p;
11374 {
11375 struct glyph_matrix *matrix;
11376 struct glyph_row *row;
11377 int window_height;
11378
11379 if (!make_cursor_line_fully_visible_p)
11380 return 1;
11381
11382 /* It's not always possible to find the cursor, e.g, when a window
11383 is full of overlay strings. Don't do anything in that case. */
11384 if (w->cursor.vpos < 0)
11385 return 1;
11386
11387 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11388 row = MATRIX_ROW (matrix, w->cursor.vpos);
11389
11390 /* If the cursor row is not partially visible, there's nothing to do. */
11391 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11392 return 1;
11393
11394 /* If the row the cursor is in is taller than the window's height,
11395 it's not clear what to do, so do nothing. */
11396 window_height = window_box_height (w);
11397 if (row->height >= window_height)
11398 {
11399 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11400 return 1;
11401 }
11402 return 0;
11403
11404 #if 0
11405 /* This code used to try to scroll the window just enough to make
11406 the line visible. It returned 0 to say that the caller should
11407 allocate larger glyph matrices. */
11408
11409 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11410 {
11411 int dy = row->height - row->visible_height;
11412 w->vscroll = 0;
11413 w->cursor.y += dy;
11414 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11415 }
11416 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11417 {
11418 int dy = - (row->height - row->visible_height);
11419 w->vscroll = dy;
11420 w->cursor.y += dy;
11421 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11422 }
11423
11424 /* When we change the cursor y-position of the selected window,
11425 change this_line_y as well so that the display optimization for
11426 the cursor line of the selected window in redisplay_internal uses
11427 the correct y-position. */
11428 if (w == XWINDOW (selected_window))
11429 this_line_y = w->cursor.y;
11430
11431 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11432 redisplay with larger matrices. */
11433 if (matrix->nrows < required_matrix_height (w))
11434 {
11435 fonts_changed_p = 1;
11436 return 0;
11437 }
11438
11439 return 1;
11440 #endif /* 0 */
11441 }
11442
11443
11444 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11445 non-zero means only WINDOW is redisplayed in redisplay_internal.
11446 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11447 in redisplay_window to bring a partially visible line into view in
11448 the case that only the cursor has moved.
11449
11450 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11451 last screen line's vertical height extends past the end of the screen.
11452
11453 Value is
11454
11455 1 if scrolling succeeded
11456
11457 0 if scrolling didn't find point.
11458
11459 -1 if new fonts have been loaded so that we must interrupt
11460 redisplay, adjust glyph matrices, and try again. */
11461
11462 enum
11463 {
11464 SCROLLING_SUCCESS,
11465 SCROLLING_FAILED,
11466 SCROLLING_NEED_LARGER_MATRICES
11467 };
11468
11469 static int
11470 try_scrolling (window, just_this_one_p, scroll_conservatively,
11471 scroll_step, temp_scroll_step, last_line_misfit)
11472 Lisp_Object window;
11473 int just_this_one_p;
11474 EMACS_INT scroll_conservatively, scroll_step;
11475 int temp_scroll_step;
11476 int last_line_misfit;
11477 {
11478 struct window *w = XWINDOW (window);
11479 struct frame *f = XFRAME (w->frame);
11480 struct text_pos scroll_margin_pos;
11481 struct text_pos pos;
11482 struct text_pos startp;
11483 struct it it;
11484 Lisp_Object window_end;
11485 int this_scroll_margin;
11486 int dy = 0;
11487 int scroll_max;
11488 int rc;
11489 int amount_to_scroll = 0;
11490 Lisp_Object aggressive;
11491 int height;
11492 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11493
11494 #if GLYPH_DEBUG
11495 debug_method_add (w, "try_scrolling");
11496 #endif
11497
11498 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11499
11500 /* Compute scroll margin height in pixels. We scroll when point is
11501 within this distance from the top or bottom of the window. */
11502 if (scroll_margin > 0)
11503 {
11504 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11505 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11506 }
11507 else
11508 this_scroll_margin = 0;
11509
11510 /* Force scroll_conservatively to have a reasonable value so it doesn't
11511 cause an overflow while computing how much to scroll. */
11512 if (scroll_conservatively)
11513 scroll_conservatively = min (scroll_conservatively,
11514 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11515
11516 /* Compute how much we should try to scroll maximally to bring point
11517 into view. */
11518 if (scroll_step || scroll_conservatively || temp_scroll_step)
11519 scroll_max = max (scroll_step,
11520 max (scroll_conservatively, temp_scroll_step));
11521 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11522 || NUMBERP (current_buffer->scroll_up_aggressively))
11523 /* We're trying to scroll because of aggressive scrolling
11524 but no scroll_step is set. Choose an arbitrary one. Maybe
11525 there should be a variable for this. */
11526 scroll_max = 10;
11527 else
11528 scroll_max = 0;
11529 scroll_max *= FRAME_LINE_HEIGHT (f);
11530
11531 /* Decide whether we have to scroll down. Start at the window end
11532 and move this_scroll_margin up to find the position of the scroll
11533 margin. */
11534 window_end = Fwindow_end (window, Qt);
11535
11536 too_near_end:
11537
11538 CHARPOS (scroll_margin_pos) = XINT (window_end);
11539 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11540
11541 if (this_scroll_margin || extra_scroll_margin_lines)
11542 {
11543 start_display (&it, w, scroll_margin_pos);
11544 if (this_scroll_margin)
11545 move_it_vertically_backward (&it, this_scroll_margin);
11546 if (extra_scroll_margin_lines)
11547 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11548 scroll_margin_pos = it.current.pos;
11549 }
11550
11551 if (PT >= CHARPOS (scroll_margin_pos))
11552 {
11553 int y0;
11554
11555 /* Point is in the scroll margin at the bottom of the window, or
11556 below. Compute a new window start that makes point visible. */
11557
11558 /* Compute the distance from the scroll margin to PT.
11559 Give up if the distance is greater than scroll_max. */
11560 start_display (&it, w, scroll_margin_pos);
11561 y0 = it.current_y;
11562 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11563 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11564
11565 /* To make point visible, we have to move the window start
11566 down so that the line the cursor is in is visible, which
11567 means we have to add in the height of the cursor line. */
11568 dy = line_bottom_y (&it) - y0;
11569
11570 if (dy > scroll_max)
11571 return SCROLLING_FAILED;
11572
11573 /* Move the window start down. If scrolling conservatively,
11574 move it just enough down to make point visible. If
11575 scroll_step is set, move it down by scroll_step. */
11576 start_display (&it, w, startp);
11577
11578 if (scroll_conservatively)
11579 /* Set AMOUNT_TO_SCROLL to at least one line,
11580 and at most scroll_conservatively lines. */
11581 amount_to_scroll
11582 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11583 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11584 else if (scroll_step || temp_scroll_step)
11585 amount_to_scroll = scroll_max;
11586 else
11587 {
11588 aggressive = current_buffer->scroll_up_aggressively;
11589 height = WINDOW_BOX_TEXT_HEIGHT (w);
11590 if (NUMBERP (aggressive))
11591 {
11592 double float_amount = XFLOATINT (aggressive) * height;
11593 amount_to_scroll = float_amount;
11594 if (amount_to_scroll == 0 && float_amount > 0)
11595 amount_to_scroll = 1;
11596 }
11597 }
11598
11599 if (amount_to_scroll <= 0)
11600 return SCROLLING_FAILED;
11601
11602 /* If moving by amount_to_scroll leaves STARTP unchanged,
11603 move it down one screen line. */
11604
11605 move_it_vertically (&it, amount_to_scroll);
11606 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11607 move_it_by_lines (&it, 1, 1);
11608 startp = it.current.pos;
11609 }
11610 else
11611 {
11612 /* See if point is inside the scroll margin at the top of the
11613 window. */
11614 scroll_margin_pos = startp;
11615 if (this_scroll_margin)
11616 {
11617 start_display (&it, w, startp);
11618 move_it_vertically (&it, this_scroll_margin);
11619 scroll_margin_pos = it.current.pos;
11620 }
11621
11622 if (PT < CHARPOS (scroll_margin_pos))
11623 {
11624 /* Point is in the scroll margin at the top of the window or
11625 above what is displayed in the window. */
11626 int y0;
11627
11628 /* Compute the vertical distance from PT to the scroll
11629 margin position. Give up if distance is greater than
11630 scroll_max. */
11631 SET_TEXT_POS (pos, PT, PT_BYTE);
11632 start_display (&it, w, pos);
11633 y0 = it.current_y;
11634 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11635 it.last_visible_y, -1,
11636 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11637 dy = it.current_y - y0;
11638 if (dy > scroll_max)
11639 return SCROLLING_FAILED;
11640
11641 /* Compute new window start. */
11642 start_display (&it, w, startp);
11643
11644 if (scroll_conservatively)
11645 amount_to_scroll
11646 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11647 else if (scroll_step || temp_scroll_step)
11648 amount_to_scroll = scroll_max;
11649 else
11650 {
11651 aggressive = current_buffer->scroll_down_aggressively;
11652 height = WINDOW_BOX_TEXT_HEIGHT (w);
11653 if (NUMBERP (aggressive))
11654 {
11655 double float_amount = XFLOATINT (aggressive) * height;
11656 amount_to_scroll = float_amount;
11657 if (amount_to_scroll == 0 && float_amount > 0)
11658 amount_to_scroll = 1;
11659 }
11660 }
11661
11662 if (amount_to_scroll <= 0)
11663 return SCROLLING_FAILED;
11664
11665 move_it_vertically_backward (&it, amount_to_scroll);
11666 startp = it.current.pos;
11667 }
11668 }
11669
11670 /* Run window scroll functions. */
11671 startp = run_window_scroll_functions (window, startp);
11672
11673 /* Display the window. Give up if new fonts are loaded, or if point
11674 doesn't appear. */
11675 if (!try_window (window, startp, 0))
11676 rc = SCROLLING_NEED_LARGER_MATRICES;
11677 else if (w->cursor.vpos < 0)
11678 {
11679 clear_glyph_matrix (w->desired_matrix);
11680 rc = SCROLLING_FAILED;
11681 }
11682 else
11683 {
11684 /* Maybe forget recorded base line for line number display. */
11685 if (!just_this_one_p
11686 || current_buffer->clip_changed
11687 || BEG_UNCHANGED < CHARPOS (startp))
11688 w->base_line_number = Qnil;
11689
11690 /* If cursor ends up on a partially visible line,
11691 treat that as being off the bottom of the screen. */
11692 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11693 {
11694 clear_glyph_matrix (w->desired_matrix);
11695 ++extra_scroll_margin_lines;
11696 goto too_near_end;
11697 }
11698 rc = SCROLLING_SUCCESS;
11699 }
11700
11701 return rc;
11702 }
11703
11704
11705 /* Compute a suitable window start for window W if display of W starts
11706 on a continuation line. Value is non-zero if a new window start
11707 was computed.
11708
11709 The new window start will be computed, based on W's width, starting
11710 from the start of the continued line. It is the start of the
11711 screen line with the minimum distance from the old start W->start. */
11712
11713 static int
11714 compute_window_start_on_continuation_line (w)
11715 struct window *w;
11716 {
11717 struct text_pos pos, start_pos;
11718 int window_start_changed_p = 0;
11719
11720 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11721
11722 /* If window start is on a continuation line... Window start may be
11723 < BEGV in case there's invisible text at the start of the
11724 buffer (M-x rmail, for example). */
11725 if (CHARPOS (start_pos) > BEGV
11726 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11727 {
11728 struct it it;
11729 struct glyph_row *row;
11730
11731 /* Handle the case that the window start is out of range. */
11732 if (CHARPOS (start_pos) < BEGV)
11733 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11734 else if (CHARPOS (start_pos) > ZV)
11735 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11736
11737 /* Find the start of the continued line. This should be fast
11738 because scan_buffer is fast (newline cache). */
11739 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11740 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11741 row, DEFAULT_FACE_ID);
11742 reseat_at_previous_visible_line_start (&it);
11743
11744 /* If the line start is "too far" away from the window start,
11745 say it takes too much time to compute a new window start. */
11746 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11747 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11748 {
11749 int min_distance, distance;
11750
11751 /* Move forward by display lines to find the new window
11752 start. If window width was enlarged, the new start can
11753 be expected to be > the old start. If window width was
11754 decreased, the new window start will be < the old start.
11755 So, we're looking for the display line start with the
11756 minimum distance from the old window start. */
11757 pos = it.current.pos;
11758 min_distance = INFINITY;
11759 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11760 distance < min_distance)
11761 {
11762 min_distance = distance;
11763 pos = it.current.pos;
11764 move_it_by_lines (&it, 1, 0);
11765 }
11766
11767 /* Set the window start there. */
11768 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11769 window_start_changed_p = 1;
11770 }
11771 }
11772
11773 return window_start_changed_p;
11774 }
11775
11776
11777 /* Try cursor movement in case text has not changed in window WINDOW,
11778 with window start STARTP. Value is
11779
11780 CURSOR_MOVEMENT_SUCCESS if successful
11781
11782 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11783
11784 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11785 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11786 we want to scroll as if scroll-step were set to 1. See the code.
11787
11788 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11789 which case we have to abort this redisplay, and adjust matrices
11790 first. */
11791
11792 enum
11793 {
11794 CURSOR_MOVEMENT_SUCCESS,
11795 CURSOR_MOVEMENT_CANNOT_BE_USED,
11796 CURSOR_MOVEMENT_MUST_SCROLL,
11797 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11798 };
11799
11800 static int
11801 try_cursor_movement (window, startp, scroll_step)
11802 Lisp_Object window;
11803 struct text_pos startp;
11804 int *scroll_step;
11805 {
11806 struct window *w = XWINDOW (window);
11807 struct frame *f = XFRAME (w->frame);
11808 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11809
11810 #if GLYPH_DEBUG
11811 if (inhibit_try_cursor_movement)
11812 return rc;
11813 #endif
11814
11815 /* Handle case where text has not changed, only point, and it has
11816 not moved off the frame. */
11817 if (/* Point may be in this window. */
11818 PT >= CHARPOS (startp)
11819 /* Selective display hasn't changed. */
11820 && !current_buffer->clip_changed
11821 /* Function force-mode-line-update is used to force a thorough
11822 redisplay. It sets either windows_or_buffers_changed or
11823 update_mode_lines. So don't take a shortcut here for these
11824 cases. */
11825 && !update_mode_lines
11826 && !windows_or_buffers_changed
11827 && !cursor_type_changed
11828 /* Can't use this case if highlighting a region. When a
11829 region exists, cursor movement has to do more than just
11830 set the cursor. */
11831 && !(!NILP (Vtransient_mark_mode)
11832 && !NILP (current_buffer->mark_active))
11833 && NILP (w->region_showing)
11834 && NILP (Vshow_trailing_whitespace)
11835 /* Right after splitting windows, last_point may be nil. */
11836 && INTEGERP (w->last_point)
11837 /* This code is not used for mini-buffer for the sake of the case
11838 of redisplaying to replace an echo area message; since in
11839 that case the mini-buffer contents per se are usually
11840 unchanged. This code is of no real use in the mini-buffer
11841 since the handling of this_line_start_pos, etc., in redisplay
11842 handles the same cases. */
11843 && !EQ (window, minibuf_window)
11844 /* When splitting windows or for new windows, it happens that
11845 redisplay is called with a nil window_end_vpos or one being
11846 larger than the window. This should really be fixed in
11847 window.c. I don't have this on my list, now, so we do
11848 approximately the same as the old redisplay code. --gerd. */
11849 && INTEGERP (w->window_end_vpos)
11850 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11851 && (FRAME_WINDOW_P (f)
11852 || !overlay_arrow_in_current_buffer_p ()))
11853 {
11854 int this_scroll_margin, top_scroll_margin;
11855 struct glyph_row *row = NULL;
11856
11857 #if GLYPH_DEBUG
11858 debug_method_add (w, "cursor movement");
11859 #endif
11860
11861 /* Scroll if point within this distance from the top or bottom
11862 of the window. This is a pixel value. */
11863 this_scroll_margin = max (0, scroll_margin);
11864 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11865 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11866
11867 top_scroll_margin = this_scroll_margin;
11868 if (WINDOW_WANTS_HEADER_LINE_P (w))
11869 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11870
11871 /* Start with the row the cursor was displayed during the last
11872 not paused redisplay. Give up if that row is not valid. */
11873 if (w->last_cursor.vpos < 0
11874 || w->last_cursor.vpos >= w->current_matrix->nrows)
11875 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11876 else
11877 {
11878 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11879 if (row->mode_line_p)
11880 ++row;
11881 if (!row->enabled_p)
11882 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11883 }
11884
11885 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11886 {
11887 int scroll_p = 0;
11888 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11889
11890 if (PT > XFASTINT (w->last_point))
11891 {
11892 /* Point has moved forward. */
11893 while (MATRIX_ROW_END_CHARPOS (row) < PT
11894 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11895 {
11896 xassert (row->enabled_p);
11897 ++row;
11898 }
11899
11900 /* The end position of a row equals the start position
11901 of the next row. If PT is there, we would rather
11902 display it in the next line. */
11903 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11904 && MATRIX_ROW_END_CHARPOS (row) == PT
11905 && !cursor_row_p (w, row))
11906 ++row;
11907
11908 /* If within the scroll margin, scroll. Note that
11909 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11910 the next line would be drawn, and that
11911 this_scroll_margin can be zero. */
11912 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11913 || PT > MATRIX_ROW_END_CHARPOS (row)
11914 /* Line is completely visible last line in window
11915 and PT is to be set in the next line. */
11916 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11917 && PT == MATRIX_ROW_END_CHARPOS (row)
11918 && !row->ends_at_zv_p
11919 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11920 scroll_p = 1;
11921 }
11922 else if (PT < XFASTINT (w->last_point))
11923 {
11924 /* Cursor has to be moved backward. Note that PT >=
11925 CHARPOS (startp) because of the outer if-statement. */
11926 while (!row->mode_line_p
11927 && (MATRIX_ROW_START_CHARPOS (row) > PT
11928 || (MATRIX_ROW_START_CHARPOS (row) == PT
11929 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
11930 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
11931 row > w->current_matrix->rows
11932 && (row-1)->ends_in_newline_from_string_p))))
11933 && (row->y > top_scroll_margin
11934 || CHARPOS (startp) == BEGV))
11935 {
11936 xassert (row->enabled_p);
11937 --row;
11938 }
11939
11940 /* Consider the following case: Window starts at BEGV,
11941 there is invisible, intangible text at BEGV, so that
11942 display starts at some point START > BEGV. It can
11943 happen that we are called with PT somewhere between
11944 BEGV and START. Try to handle that case. */
11945 if (row < w->current_matrix->rows
11946 || row->mode_line_p)
11947 {
11948 row = w->current_matrix->rows;
11949 if (row->mode_line_p)
11950 ++row;
11951 }
11952
11953 /* Due to newlines in overlay strings, we may have to
11954 skip forward over overlay strings. */
11955 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11956 && MATRIX_ROW_END_CHARPOS (row) == PT
11957 && !cursor_row_p (w, row))
11958 ++row;
11959
11960 /* If within the scroll margin, scroll. */
11961 if (row->y < top_scroll_margin
11962 && CHARPOS (startp) != BEGV)
11963 scroll_p = 1;
11964 }
11965 else
11966 {
11967 /* Cursor did not move. So don't scroll even if cursor line
11968 is partially visible, as it was so before. */
11969 rc = CURSOR_MOVEMENT_SUCCESS;
11970 }
11971
11972 if (PT < MATRIX_ROW_START_CHARPOS (row)
11973 || PT > MATRIX_ROW_END_CHARPOS (row))
11974 {
11975 /* if PT is not in the glyph row, give up. */
11976 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11977 }
11978 else if (rc != CURSOR_MOVEMENT_SUCCESS
11979 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11980 && make_cursor_line_fully_visible_p)
11981 {
11982 if (PT == MATRIX_ROW_END_CHARPOS (row)
11983 && !row->ends_at_zv_p
11984 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11985 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11986 else if (row->height > window_box_height (w))
11987 {
11988 /* If we end up in a partially visible line, let's
11989 make it fully visible, except when it's taller
11990 than the window, in which case we can't do much
11991 about it. */
11992 *scroll_step = 1;
11993 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11994 }
11995 else
11996 {
11997 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11998 if (!cursor_row_fully_visible_p (w, 0, 1))
11999 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12000 else
12001 rc = CURSOR_MOVEMENT_SUCCESS;
12002 }
12003 }
12004 else if (scroll_p)
12005 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12006 else
12007 {
12008 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12009 rc = CURSOR_MOVEMENT_SUCCESS;
12010 }
12011 }
12012 }
12013
12014 return rc;
12015 }
12016
12017 void
12018 set_vertical_scroll_bar (w)
12019 struct window *w;
12020 {
12021 int start, end, whole;
12022
12023 /* Calculate the start and end positions for the current window.
12024 At some point, it would be nice to choose between scrollbars
12025 which reflect the whole buffer size, with special markers
12026 indicating narrowing, and scrollbars which reflect only the
12027 visible region.
12028
12029 Note that mini-buffers sometimes aren't displaying any text. */
12030 if (!MINI_WINDOW_P (w)
12031 || (w == XWINDOW (minibuf_window)
12032 && NILP (echo_area_buffer[0])))
12033 {
12034 struct buffer *buf = XBUFFER (w->buffer);
12035 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12036 start = marker_position (w->start) - BUF_BEGV (buf);
12037 /* I don't think this is guaranteed to be right. For the
12038 moment, we'll pretend it is. */
12039 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12040
12041 if (end < start)
12042 end = start;
12043 if (whole < (end - start))
12044 whole = end - start;
12045 }
12046 else
12047 start = end = whole = 0;
12048
12049 /* Indicate what this scroll bar ought to be displaying now. */
12050 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12051 }
12052
12053
12054 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12055 selected_window is redisplayed.
12056
12057 We can return without actually redisplaying the window if
12058 fonts_changed_p is nonzero. In that case, redisplay_internal will
12059 retry. */
12060
12061 static void
12062 redisplay_window (window, just_this_one_p)
12063 Lisp_Object window;
12064 int just_this_one_p;
12065 {
12066 struct window *w = XWINDOW (window);
12067 struct frame *f = XFRAME (w->frame);
12068 struct buffer *buffer = XBUFFER (w->buffer);
12069 struct buffer *old = current_buffer;
12070 struct text_pos lpoint, opoint, startp;
12071 int update_mode_line;
12072 int tem;
12073 struct it it;
12074 /* Record it now because it's overwritten. */
12075 int current_matrix_up_to_date_p = 0;
12076 int used_current_matrix_p = 0;
12077 /* This is less strict than current_matrix_up_to_date_p.
12078 It indictes that the buffer contents and narrowing are unchanged. */
12079 int buffer_unchanged_p = 0;
12080 int temp_scroll_step = 0;
12081 int count = SPECPDL_INDEX ();
12082 int rc;
12083 int centering_position = -1;
12084 int last_line_misfit = 0;
12085
12086 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12087 opoint = lpoint;
12088
12089 /* W must be a leaf window here. */
12090 xassert (!NILP (w->buffer));
12091 #if GLYPH_DEBUG
12092 *w->desired_matrix->method = 0;
12093 #endif
12094
12095 specbind (Qinhibit_point_motion_hooks, Qt);
12096
12097 reconsider_clip_changes (w, buffer);
12098
12099 /* Has the mode line to be updated? */
12100 update_mode_line = (!NILP (w->update_mode_line)
12101 || update_mode_lines
12102 || buffer->clip_changed
12103 || buffer->prevent_redisplay_optimizations_p);
12104
12105 if (MINI_WINDOW_P (w))
12106 {
12107 if (w == XWINDOW (echo_area_window)
12108 && !NILP (echo_area_buffer[0]))
12109 {
12110 if (update_mode_line)
12111 /* We may have to update a tty frame's menu bar or a
12112 tool-bar. Example `M-x C-h C-h C-g'. */
12113 goto finish_menu_bars;
12114 else
12115 /* We've already displayed the echo area glyphs in this window. */
12116 goto finish_scroll_bars;
12117 }
12118 else if ((w != XWINDOW (minibuf_window)
12119 || minibuf_level == 0)
12120 /* When buffer is nonempty, redisplay window normally. */
12121 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12122 /* Quail displays non-mini buffers in minibuffer window.
12123 In that case, redisplay the window normally. */
12124 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12125 {
12126 /* W is a mini-buffer window, but it's not active, so clear
12127 it. */
12128 int yb = window_text_bottom_y (w);
12129 struct glyph_row *row;
12130 int y;
12131
12132 for (y = 0, row = w->desired_matrix->rows;
12133 y < yb;
12134 y += row->height, ++row)
12135 blank_row (w, row, y);
12136 goto finish_scroll_bars;
12137 }
12138
12139 clear_glyph_matrix (w->desired_matrix);
12140 }
12141
12142 /* Otherwise set up data on this window; select its buffer and point
12143 value. */
12144 /* Really select the buffer, for the sake of buffer-local
12145 variables. */
12146 set_buffer_internal_1 (XBUFFER (w->buffer));
12147 SET_TEXT_POS (opoint, PT, PT_BYTE);
12148
12149 current_matrix_up_to_date_p
12150 = (!NILP (w->window_end_valid)
12151 && !current_buffer->clip_changed
12152 && !current_buffer->prevent_redisplay_optimizations_p
12153 && XFASTINT (w->last_modified) >= MODIFF
12154 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12155
12156 buffer_unchanged_p
12157 = (!NILP (w->window_end_valid)
12158 && !current_buffer->clip_changed
12159 && XFASTINT (w->last_modified) >= MODIFF
12160 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12161
12162 /* When windows_or_buffers_changed is non-zero, we can't rely on
12163 the window end being valid, so set it to nil there. */
12164 if (windows_or_buffers_changed)
12165 {
12166 /* If window starts on a continuation line, maybe adjust the
12167 window start in case the window's width changed. */
12168 if (XMARKER (w->start)->buffer == current_buffer)
12169 compute_window_start_on_continuation_line (w);
12170
12171 w->window_end_valid = Qnil;
12172 }
12173
12174 /* Some sanity checks. */
12175 CHECK_WINDOW_END (w);
12176 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12177 abort ();
12178 if (BYTEPOS (opoint) < CHARPOS (opoint))
12179 abort ();
12180
12181 /* If %c is in mode line, update it if needed. */
12182 if (!NILP (w->column_number_displayed)
12183 /* This alternative quickly identifies a common case
12184 where no change is needed. */
12185 && !(PT == XFASTINT (w->last_point)
12186 && XFASTINT (w->last_modified) >= MODIFF
12187 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12188 && (XFASTINT (w->column_number_displayed)
12189 != (int) current_column ())) /* iftc */
12190 update_mode_line = 1;
12191
12192 /* Count number of windows showing the selected buffer. An indirect
12193 buffer counts as its base buffer. */
12194 if (!just_this_one_p)
12195 {
12196 struct buffer *current_base, *window_base;
12197 current_base = current_buffer;
12198 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12199 if (current_base->base_buffer)
12200 current_base = current_base->base_buffer;
12201 if (window_base->base_buffer)
12202 window_base = window_base->base_buffer;
12203 if (current_base == window_base)
12204 buffer_shared++;
12205 }
12206
12207 /* Point refers normally to the selected window. For any other
12208 window, set up appropriate value. */
12209 if (!EQ (window, selected_window))
12210 {
12211 int new_pt = XMARKER (w->pointm)->charpos;
12212 int new_pt_byte = marker_byte_position (w->pointm);
12213 if (new_pt < BEGV)
12214 {
12215 new_pt = BEGV;
12216 new_pt_byte = BEGV_BYTE;
12217 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12218 }
12219 else if (new_pt > (ZV - 1))
12220 {
12221 new_pt = ZV;
12222 new_pt_byte = ZV_BYTE;
12223 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12224 }
12225
12226 /* We don't use SET_PT so that the point-motion hooks don't run. */
12227 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12228 }
12229
12230 /* If any of the character widths specified in the display table
12231 have changed, invalidate the width run cache. It's true that
12232 this may be a bit late to catch such changes, but the rest of
12233 redisplay goes (non-fatally) haywire when the display table is
12234 changed, so why should we worry about doing any better? */
12235 if (current_buffer->width_run_cache)
12236 {
12237 struct Lisp_Char_Table *disptab = buffer_display_table ();
12238
12239 if (! disptab_matches_widthtab (disptab,
12240 XVECTOR (current_buffer->width_table)))
12241 {
12242 invalidate_region_cache (current_buffer,
12243 current_buffer->width_run_cache,
12244 BEG, Z);
12245 recompute_width_table (current_buffer, disptab);
12246 }
12247 }
12248
12249 /* If window-start is screwed up, choose a new one. */
12250 if (XMARKER (w->start)->buffer != current_buffer)
12251 goto recenter;
12252
12253 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12254
12255 /* If someone specified a new starting point but did not insist,
12256 check whether it can be used. */
12257 if (!NILP (w->optional_new_start)
12258 && CHARPOS (startp) >= BEGV
12259 && CHARPOS (startp) <= ZV)
12260 {
12261 w->optional_new_start = Qnil;
12262 start_display (&it, w, startp);
12263 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12264 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12265 if (IT_CHARPOS (it) == PT)
12266 w->force_start = Qt;
12267 /* IT may overshoot PT if text at PT is invisible. */
12268 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12269 w->force_start = Qt;
12270
12271
12272 }
12273
12274 /* Handle case where place to start displaying has been specified,
12275 unless the specified location is outside the accessible range. */
12276 if (!NILP (w->force_start)
12277 || w->frozen_window_start_p)
12278 {
12279 /* We set this later on if we have to adjust point. */
12280 int new_vpos = -1;
12281 int val;
12282
12283 w->force_start = Qnil;
12284 w->vscroll = 0;
12285 w->window_end_valid = Qnil;
12286
12287 /* Forget any recorded base line for line number display. */
12288 if (!buffer_unchanged_p)
12289 w->base_line_number = Qnil;
12290
12291 /* Redisplay the mode line. Select the buffer properly for that.
12292 Also, run the hook window-scroll-functions
12293 because we have scrolled. */
12294 /* Note, we do this after clearing force_start because
12295 if there's an error, it is better to forget about force_start
12296 than to get into an infinite loop calling the hook functions
12297 and having them get more errors. */
12298 if (!update_mode_line
12299 || ! NILP (Vwindow_scroll_functions))
12300 {
12301 update_mode_line = 1;
12302 w->update_mode_line = Qt;
12303 startp = run_window_scroll_functions (window, startp);
12304 }
12305
12306 w->last_modified = make_number (0);
12307 w->last_overlay_modified = make_number (0);
12308 if (CHARPOS (startp) < BEGV)
12309 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12310 else if (CHARPOS (startp) > ZV)
12311 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12312
12313 /* Redisplay, then check if cursor has been set during the
12314 redisplay. Give up if new fonts were loaded. */
12315 val = try_window (window, startp, 1);
12316 if (!val)
12317 {
12318 w->force_start = Qt;
12319 clear_glyph_matrix (w->desired_matrix);
12320 goto need_larger_matrices;
12321 }
12322 /* Point was outside the scroll margins. */
12323 if (val < 0)
12324 new_vpos = window_box_height (w) / 2;
12325
12326 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12327 {
12328 /* If point does not appear, try to move point so it does
12329 appear. The desired matrix has been built above, so we
12330 can use it here. */
12331 new_vpos = window_box_height (w) / 2;
12332 }
12333
12334 if (!cursor_row_fully_visible_p (w, 0, 0))
12335 {
12336 /* Point does appear, but on a line partly visible at end of window.
12337 Move it back to a fully-visible line. */
12338 new_vpos = window_box_height (w);
12339 }
12340
12341 /* If we need to move point for either of the above reasons,
12342 now actually do it. */
12343 if (new_vpos >= 0)
12344 {
12345 struct glyph_row *row;
12346
12347 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12348 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12349 ++row;
12350
12351 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12352 MATRIX_ROW_START_BYTEPOS (row));
12353
12354 if (w != XWINDOW (selected_window))
12355 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12356 else if (current_buffer == old)
12357 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12358
12359 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12360
12361 /* If we are highlighting the region, then we just changed
12362 the region, so redisplay to show it. */
12363 if (!NILP (Vtransient_mark_mode)
12364 && !NILP (current_buffer->mark_active))
12365 {
12366 clear_glyph_matrix (w->desired_matrix);
12367 if (!try_window (window, startp, 0))
12368 goto need_larger_matrices;
12369 }
12370 }
12371
12372 #if GLYPH_DEBUG
12373 debug_method_add (w, "forced window start");
12374 #endif
12375 goto done;
12376 }
12377
12378 /* Handle case where text has not changed, only point, and it has
12379 not moved off the frame, and we are not retrying after hscroll.
12380 (current_matrix_up_to_date_p is nonzero when retrying.) */
12381 if (current_matrix_up_to_date_p
12382 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12383 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12384 {
12385 switch (rc)
12386 {
12387 case CURSOR_MOVEMENT_SUCCESS:
12388 used_current_matrix_p = 1;
12389 goto done;
12390
12391 #if 0 /* try_cursor_movement never returns this value. */
12392 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12393 goto need_larger_matrices;
12394 #endif
12395
12396 case CURSOR_MOVEMENT_MUST_SCROLL:
12397 goto try_to_scroll;
12398
12399 default:
12400 abort ();
12401 }
12402 }
12403 /* If current starting point was originally the beginning of a line
12404 but no longer is, find a new starting point. */
12405 else if (!NILP (w->start_at_line_beg)
12406 && !(CHARPOS (startp) <= BEGV
12407 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12408 {
12409 #if GLYPH_DEBUG
12410 debug_method_add (w, "recenter 1");
12411 #endif
12412 goto recenter;
12413 }
12414
12415 /* Try scrolling with try_window_id. Value is > 0 if update has
12416 been done, it is -1 if we know that the same window start will
12417 not work. It is 0 if unsuccessful for some other reason. */
12418 else if ((tem = try_window_id (w)) != 0)
12419 {
12420 #if GLYPH_DEBUG
12421 debug_method_add (w, "try_window_id %d", tem);
12422 #endif
12423
12424 if (fonts_changed_p)
12425 goto need_larger_matrices;
12426 if (tem > 0)
12427 goto done;
12428
12429 /* Otherwise try_window_id has returned -1 which means that we
12430 don't want the alternative below this comment to execute. */
12431 }
12432 else if (CHARPOS (startp) >= BEGV
12433 && CHARPOS (startp) <= ZV
12434 && PT >= CHARPOS (startp)
12435 && (CHARPOS (startp) < ZV
12436 /* Avoid starting at end of buffer. */
12437 || CHARPOS (startp) == BEGV
12438 || (XFASTINT (w->last_modified) >= MODIFF
12439 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12440 {
12441 #if GLYPH_DEBUG
12442 debug_method_add (w, "same window start");
12443 #endif
12444
12445 /* Try to redisplay starting at same place as before.
12446 If point has not moved off frame, accept the results. */
12447 if (!current_matrix_up_to_date_p
12448 /* Don't use try_window_reusing_current_matrix in this case
12449 because a window scroll function can have changed the
12450 buffer. */
12451 || !NILP (Vwindow_scroll_functions)
12452 || MINI_WINDOW_P (w)
12453 || !(used_current_matrix_p
12454 = try_window_reusing_current_matrix (w)))
12455 {
12456 IF_DEBUG (debug_method_add (w, "1"));
12457 if (try_window (window, startp, 1) < 0)
12458 /* -1 means we need to scroll.
12459 0 means we need new matrices, but fonts_changed_p
12460 is set in that case, so we will detect it below. */
12461 goto try_to_scroll;
12462 }
12463
12464 if (fonts_changed_p)
12465 goto need_larger_matrices;
12466
12467 if (w->cursor.vpos >= 0)
12468 {
12469 if (!just_this_one_p
12470 || current_buffer->clip_changed
12471 || BEG_UNCHANGED < CHARPOS (startp))
12472 /* Forget any recorded base line for line number display. */
12473 w->base_line_number = Qnil;
12474
12475 if (!cursor_row_fully_visible_p (w, 1, 0))
12476 {
12477 clear_glyph_matrix (w->desired_matrix);
12478 last_line_misfit = 1;
12479 }
12480 /* Drop through and scroll. */
12481 else
12482 goto done;
12483 }
12484 else
12485 clear_glyph_matrix (w->desired_matrix);
12486 }
12487
12488 try_to_scroll:
12489
12490 w->last_modified = make_number (0);
12491 w->last_overlay_modified = make_number (0);
12492
12493 /* Redisplay the mode line. Select the buffer properly for that. */
12494 if (!update_mode_line)
12495 {
12496 update_mode_line = 1;
12497 w->update_mode_line = Qt;
12498 }
12499
12500 /* Try to scroll by specified few lines. */
12501 if ((scroll_conservatively
12502 || scroll_step
12503 || temp_scroll_step
12504 || NUMBERP (current_buffer->scroll_up_aggressively)
12505 || NUMBERP (current_buffer->scroll_down_aggressively))
12506 && !current_buffer->clip_changed
12507 && CHARPOS (startp) >= BEGV
12508 && CHARPOS (startp) <= ZV)
12509 {
12510 /* The function returns -1 if new fonts were loaded, 1 if
12511 successful, 0 if not successful. */
12512 int rc = try_scrolling (window, just_this_one_p,
12513 scroll_conservatively,
12514 scroll_step,
12515 temp_scroll_step, last_line_misfit);
12516 switch (rc)
12517 {
12518 case SCROLLING_SUCCESS:
12519 goto done;
12520
12521 case SCROLLING_NEED_LARGER_MATRICES:
12522 goto need_larger_matrices;
12523
12524 case SCROLLING_FAILED:
12525 break;
12526
12527 default:
12528 abort ();
12529 }
12530 }
12531
12532 /* Finally, just choose place to start which centers point */
12533
12534 recenter:
12535 if (centering_position < 0)
12536 centering_position = window_box_height (w) / 2;
12537
12538 #if GLYPH_DEBUG
12539 debug_method_add (w, "recenter");
12540 #endif
12541
12542 /* w->vscroll = 0; */
12543
12544 /* Forget any previously recorded base line for line number display. */
12545 if (!buffer_unchanged_p)
12546 w->base_line_number = Qnil;
12547
12548 /* Move backward half the height of the window. */
12549 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12550 it.current_y = it.last_visible_y;
12551 move_it_vertically_backward (&it, centering_position);
12552 xassert (IT_CHARPOS (it) >= BEGV);
12553
12554 /* The function move_it_vertically_backward may move over more
12555 than the specified y-distance. If it->w is small, e.g. a
12556 mini-buffer window, we may end up in front of the window's
12557 display area. Start displaying at the start of the line
12558 containing PT in this case. */
12559 if (it.current_y <= 0)
12560 {
12561 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12562 move_it_vertically_backward (&it, 0);
12563 #if 0
12564 /* I think this assert is bogus if buffer contains
12565 invisible text or images. KFS. */
12566 xassert (IT_CHARPOS (it) <= PT);
12567 #endif
12568 it.current_y = 0;
12569 }
12570
12571 it.current_x = it.hpos = 0;
12572
12573 /* Set startp here explicitly in case that helps avoid an infinite loop
12574 in case the window-scroll-functions functions get errors. */
12575 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12576
12577 /* Run scroll hooks. */
12578 startp = run_window_scroll_functions (window, it.current.pos);
12579
12580 /* Redisplay the window. */
12581 if (!current_matrix_up_to_date_p
12582 || windows_or_buffers_changed
12583 || cursor_type_changed
12584 /* Don't use try_window_reusing_current_matrix in this case
12585 because it can have changed the buffer. */
12586 || !NILP (Vwindow_scroll_functions)
12587 || !just_this_one_p
12588 || MINI_WINDOW_P (w)
12589 || !(used_current_matrix_p
12590 = try_window_reusing_current_matrix (w)))
12591 try_window (window, startp, 0);
12592
12593 /* If new fonts have been loaded (due to fontsets), give up. We
12594 have to start a new redisplay since we need to re-adjust glyph
12595 matrices. */
12596 if (fonts_changed_p)
12597 goto need_larger_matrices;
12598
12599 /* If cursor did not appear assume that the middle of the window is
12600 in the first line of the window. Do it again with the next line.
12601 (Imagine a window of height 100, displaying two lines of height
12602 60. Moving back 50 from it->last_visible_y will end in the first
12603 line.) */
12604 if (w->cursor.vpos < 0)
12605 {
12606 if (!NILP (w->window_end_valid)
12607 && PT >= Z - XFASTINT (w->window_end_pos))
12608 {
12609 clear_glyph_matrix (w->desired_matrix);
12610 move_it_by_lines (&it, 1, 0);
12611 try_window (window, it.current.pos, 0);
12612 }
12613 else if (PT < IT_CHARPOS (it))
12614 {
12615 clear_glyph_matrix (w->desired_matrix);
12616 move_it_by_lines (&it, -1, 0);
12617 try_window (window, it.current.pos, 0);
12618 }
12619 else
12620 {
12621 /* Not much we can do about it. */
12622 }
12623 }
12624
12625 /* Consider the following case: Window starts at BEGV, there is
12626 invisible, intangible text at BEGV, so that display starts at
12627 some point START > BEGV. It can happen that we are called with
12628 PT somewhere between BEGV and START. Try to handle that case. */
12629 if (w->cursor.vpos < 0)
12630 {
12631 struct glyph_row *row = w->current_matrix->rows;
12632 if (row->mode_line_p)
12633 ++row;
12634 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12635 }
12636
12637 if (!cursor_row_fully_visible_p (w, 0, 0))
12638 {
12639 /* If vscroll is enabled, disable it and try again. */
12640 if (w->vscroll)
12641 {
12642 w->vscroll = 0;
12643 clear_glyph_matrix (w->desired_matrix);
12644 goto recenter;
12645 }
12646
12647 /* If centering point failed to make the whole line visible,
12648 put point at the top instead. That has to make the whole line
12649 visible, if it can be done. */
12650 if (centering_position == 0)
12651 goto done;
12652
12653 clear_glyph_matrix (w->desired_matrix);
12654 centering_position = 0;
12655 goto recenter;
12656 }
12657
12658 done:
12659
12660 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12661 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12662 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12663 ? Qt : Qnil);
12664
12665 /* Display the mode line, if we must. */
12666 if ((update_mode_line
12667 /* If window not full width, must redo its mode line
12668 if (a) the window to its side is being redone and
12669 (b) we do a frame-based redisplay. This is a consequence
12670 of how inverted lines are drawn in frame-based redisplay. */
12671 || (!just_this_one_p
12672 && !FRAME_WINDOW_P (f)
12673 && !WINDOW_FULL_WIDTH_P (w))
12674 /* Line number to display. */
12675 || INTEGERP (w->base_line_pos)
12676 /* Column number is displayed and different from the one displayed. */
12677 || (!NILP (w->column_number_displayed)
12678 && (XFASTINT (w->column_number_displayed)
12679 != (int) current_column ()))) /* iftc */
12680 /* This means that the window has a mode line. */
12681 && (WINDOW_WANTS_MODELINE_P (w)
12682 || WINDOW_WANTS_HEADER_LINE_P (w)))
12683 {
12684 display_mode_lines (w);
12685
12686 /* If mode line height has changed, arrange for a thorough
12687 immediate redisplay using the correct mode line height. */
12688 if (WINDOW_WANTS_MODELINE_P (w)
12689 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12690 {
12691 fonts_changed_p = 1;
12692 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12693 = DESIRED_MODE_LINE_HEIGHT (w);
12694 }
12695
12696 /* If top line height has changed, arrange for a thorough
12697 immediate redisplay using the correct mode line height. */
12698 if (WINDOW_WANTS_HEADER_LINE_P (w)
12699 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12700 {
12701 fonts_changed_p = 1;
12702 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12703 = DESIRED_HEADER_LINE_HEIGHT (w);
12704 }
12705
12706 if (fonts_changed_p)
12707 goto need_larger_matrices;
12708 }
12709
12710 if (!line_number_displayed
12711 && !BUFFERP (w->base_line_pos))
12712 {
12713 w->base_line_pos = Qnil;
12714 w->base_line_number = Qnil;
12715 }
12716
12717 finish_menu_bars:
12718
12719 /* When we reach a frame's selected window, redo the frame's menu bar. */
12720 if (update_mode_line
12721 && EQ (FRAME_SELECTED_WINDOW (f), window))
12722 {
12723 int redisplay_menu_p = 0;
12724 int redisplay_tool_bar_p = 0;
12725
12726 if (FRAME_WINDOW_P (f))
12727 {
12728 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12729 || defined (USE_GTK)
12730 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12731 #else
12732 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12733 #endif
12734 }
12735 else
12736 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12737
12738 if (redisplay_menu_p)
12739 display_menu_bar (w);
12740
12741 #ifdef HAVE_WINDOW_SYSTEM
12742 #ifdef USE_GTK
12743 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12744 #else
12745 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12746 && (FRAME_TOOL_BAR_LINES (f) > 0
12747 || auto_resize_tool_bars_p);
12748
12749 #endif
12750
12751 if (redisplay_tool_bar_p)
12752 redisplay_tool_bar (f);
12753 #endif
12754 }
12755
12756 #ifdef HAVE_WINDOW_SYSTEM
12757 if (FRAME_WINDOW_P (f)
12758 && update_window_fringes (w, (just_this_one_p
12759 || (!used_current_matrix_p && !overlay_arrow_seen)
12760 || w->pseudo_window_p)))
12761 {
12762 update_begin (f);
12763 BLOCK_INPUT;
12764 if (draw_window_fringes (w, 1))
12765 x_draw_vertical_border (w);
12766 UNBLOCK_INPUT;
12767 update_end (f);
12768 }
12769 #endif /* HAVE_WINDOW_SYSTEM */
12770
12771 /* We go to this label, with fonts_changed_p nonzero,
12772 if it is necessary to try again using larger glyph matrices.
12773 We have to redeem the scroll bar even in this case,
12774 because the loop in redisplay_internal expects that. */
12775 need_larger_matrices:
12776 ;
12777 finish_scroll_bars:
12778
12779 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12780 {
12781 /* Set the thumb's position and size. */
12782 set_vertical_scroll_bar (w);
12783
12784 /* Note that we actually used the scroll bar attached to this
12785 window, so it shouldn't be deleted at the end of redisplay. */
12786 redeem_scroll_bar_hook (w);
12787 }
12788
12789 /* Restore current_buffer and value of point in it. */
12790 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12791 set_buffer_internal_1 (old);
12792 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12793
12794 unbind_to (count, Qnil);
12795 }
12796
12797
12798 /* Build the complete desired matrix of WINDOW with a window start
12799 buffer position POS.
12800
12801 Value is 1 if successful. It is zero if fonts were loaded during
12802 redisplay which makes re-adjusting glyph matrices necessary, and -1
12803 if point would appear in the scroll margins.
12804 (We check that only if CHECK_MARGINS is nonzero. */
12805
12806 int
12807 try_window (window, pos, check_margins)
12808 Lisp_Object window;
12809 struct text_pos pos;
12810 int check_margins;
12811 {
12812 struct window *w = XWINDOW (window);
12813 struct it it;
12814 struct glyph_row *last_text_row = NULL;
12815
12816 /* Make POS the new window start. */
12817 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12818
12819 /* Mark cursor position as unknown. No overlay arrow seen. */
12820 w->cursor.vpos = -1;
12821 overlay_arrow_seen = 0;
12822
12823 /* Initialize iterator and info to start at POS. */
12824 start_display (&it, w, pos);
12825
12826 /* Display all lines of W. */
12827 while (it.current_y < it.last_visible_y)
12828 {
12829 if (display_line (&it))
12830 last_text_row = it.glyph_row - 1;
12831 if (fonts_changed_p)
12832 return 0;
12833 }
12834
12835 /* Don't let the cursor end in the scroll margins. */
12836 if (check_margins
12837 && !MINI_WINDOW_P (w))
12838 {
12839 int this_scroll_margin;
12840
12841 this_scroll_margin = max (0, scroll_margin);
12842 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12843 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
12844
12845 if ((w->cursor.y < this_scroll_margin
12846 && CHARPOS (pos) > BEGV)
12847 /* rms: considering make_cursor_line_fully_visible_p here
12848 seems to give wrong results. We don't want to recenter
12849 when the last line is partly visible, we want to allow
12850 that case to be handled in the usual way. */
12851 || (w->cursor.y + 1) > it.last_visible_y)
12852 {
12853 w->cursor.vpos = -1;
12854 clear_glyph_matrix (w->desired_matrix);
12855 return -1;
12856 }
12857 }
12858
12859 /* If bottom moved off end of frame, change mode line percentage. */
12860 if (XFASTINT (w->window_end_pos) <= 0
12861 && Z != IT_CHARPOS (it))
12862 w->update_mode_line = Qt;
12863
12864 /* Set window_end_pos to the offset of the last character displayed
12865 on the window from the end of current_buffer. Set
12866 window_end_vpos to its row number. */
12867 if (last_text_row)
12868 {
12869 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12870 w->window_end_bytepos
12871 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12872 w->window_end_pos
12873 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12874 w->window_end_vpos
12875 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12876 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12877 ->displays_text_p);
12878 }
12879 else
12880 {
12881 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12882 w->window_end_pos = make_number (Z - ZV);
12883 w->window_end_vpos = make_number (0);
12884 }
12885
12886 /* But that is not valid info until redisplay finishes. */
12887 w->window_end_valid = Qnil;
12888 return 1;
12889 }
12890
12891
12892 \f
12893 /************************************************************************
12894 Window redisplay reusing current matrix when buffer has not changed
12895 ************************************************************************/
12896
12897 /* Try redisplay of window W showing an unchanged buffer with a
12898 different window start than the last time it was displayed by
12899 reusing its current matrix. Value is non-zero if successful.
12900 W->start is the new window start. */
12901
12902 static int
12903 try_window_reusing_current_matrix (w)
12904 struct window *w;
12905 {
12906 struct frame *f = XFRAME (w->frame);
12907 struct glyph_row *row, *bottom_row;
12908 struct it it;
12909 struct run run;
12910 struct text_pos start, new_start;
12911 int nrows_scrolled, i;
12912 struct glyph_row *last_text_row;
12913 struct glyph_row *last_reused_text_row;
12914 struct glyph_row *start_row;
12915 int start_vpos, min_y, max_y;
12916
12917 #if GLYPH_DEBUG
12918 if (inhibit_try_window_reusing)
12919 return 0;
12920 #endif
12921
12922 if (/* This function doesn't handle terminal frames. */
12923 !FRAME_WINDOW_P (f)
12924 /* Don't try to reuse the display if windows have been split
12925 or such. */
12926 || windows_or_buffers_changed
12927 || cursor_type_changed)
12928 return 0;
12929
12930 /* Can't do this if region may have changed. */
12931 if ((!NILP (Vtransient_mark_mode)
12932 && !NILP (current_buffer->mark_active))
12933 || !NILP (w->region_showing)
12934 || !NILP (Vshow_trailing_whitespace))
12935 return 0;
12936
12937 /* If top-line visibility has changed, give up. */
12938 if (WINDOW_WANTS_HEADER_LINE_P (w)
12939 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12940 return 0;
12941
12942 /* Give up if old or new display is scrolled vertically. We could
12943 make this function handle this, but right now it doesn't. */
12944 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12945 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12946 return 0;
12947
12948 /* The variable new_start now holds the new window start. The old
12949 start `start' can be determined from the current matrix. */
12950 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12951 start = start_row->start.pos;
12952 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12953
12954 /* Clear the desired matrix for the display below. */
12955 clear_glyph_matrix (w->desired_matrix);
12956
12957 if (CHARPOS (new_start) <= CHARPOS (start))
12958 {
12959 int first_row_y;
12960
12961 /* Don't use this method if the display starts with an ellipsis
12962 displayed for invisible text. It's not easy to handle that case
12963 below, and it's certainly not worth the effort since this is
12964 not a frequent case. */
12965 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12966 return 0;
12967
12968 IF_DEBUG (debug_method_add (w, "twu1"));
12969
12970 /* Display up to a row that can be reused. The variable
12971 last_text_row is set to the last row displayed that displays
12972 text. Note that it.vpos == 0 if or if not there is a
12973 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12974 start_display (&it, w, new_start);
12975 first_row_y = it.current_y;
12976 w->cursor.vpos = -1;
12977 last_text_row = last_reused_text_row = NULL;
12978
12979 while (it.current_y < it.last_visible_y
12980 && !fonts_changed_p)
12981 {
12982 /* If we have reached into the characters in the START row,
12983 that means the line boundaries have changed. So we
12984 can't start copying with the row START. Maybe it will
12985 work to start copying with the following row. */
12986 while (IT_CHARPOS (it) > CHARPOS (start))
12987 {
12988 /* Advance to the next row as the "start". */
12989 start_row++;
12990 start = start_row->start.pos;
12991 /* If there are no more rows to try, or just one, give up. */
12992 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12993 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12994 || CHARPOS (start) == ZV)
12995 {
12996 clear_glyph_matrix (w->desired_matrix);
12997 return 0;
12998 }
12999
13000 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13001 }
13002 /* If we have reached alignment,
13003 we can copy the rest of the rows. */
13004 if (IT_CHARPOS (it) == CHARPOS (start))
13005 break;
13006
13007 if (display_line (&it))
13008 last_text_row = it.glyph_row - 1;
13009 }
13010
13011 /* A value of current_y < last_visible_y means that we stopped
13012 at the previous window start, which in turn means that we
13013 have at least one reusable row. */
13014 if (it.current_y < it.last_visible_y)
13015 {
13016 /* IT.vpos always starts from 0; it counts text lines. */
13017 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13018
13019 /* Find PT if not already found in the lines displayed. */
13020 if (w->cursor.vpos < 0)
13021 {
13022 int dy = it.current_y - start_row->y;
13023
13024 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13025 row = row_containing_pos (w, PT, row, NULL, dy);
13026 if (row)
13027 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13028 dy, nrows_scrolled);
13029 else
13030 {
13031 clear_glyph_matrix (w->desired_matrix);
13032 return 0;
13033 }
13034 }
13035
13036 /* Scroll the display. Do it before the current matrix is
13037 changed. The problem here is that update has not yet
13038 run, i.e. part of the current matrix is not up to date.
13039 scroll_run_hook will clear the cursor, and use the
13040 current matrix to get the height of the row the cursor is
13041 in. */
13042 run.current_y = start_row->y;
13043 run.desired_y = it.current_y;
13044 run.height = it.last_visible_y - it.current_y;
13045
13046 if (run.height > 0 && run.current_y != run.desired_y)
13047 {
13048 update_begin (f);
13049 rif->update_window_begin_hook (w);
13050 rif->clear_window_mouse_face (w);
13051 rif->scroll_run_hook (w, &run);
13052 rif->update_window_end_hook (w, 0, 0);
13053 update_end (f);
13054 }
13055
13056 /* Shift current matrix down by nrows_scrolled lines. */
13057 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13058 rotate_matrix (w->current_matrix,
13059 start_vpos,
13060 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13061 nrows_scrolled);
13062
13063 /* Disable lines that must be updated. */
13064 for (i = 0; i < it.vpos; ++i)
13065 (start_row + i)->enabled_p = 0;
13066
13067 /* Re-compute Y positions. */
13068 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13069 max_y = it.last_visible_y;
13070 for (row = start_row + nrows_scrolled;
13071 row < bottom_row;
13072 ++row)
13073 {
13074 row->y = it.current_y;
13075 row->visible_height = row->height;
13076
13077 if (row->y < min_y)
13078 row->visible_height -= min_y - row->y;
13079 if (row->y + row->height > max_y)
13080 row->visible_height -= row->y + row->height - max_y;
13081 row->redraw_fringe_bitmaps_p = 1;
13082
13083 it.current_y += row->height;
13084
13085 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13086 last_reused_text_row = row;
13087 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13088 break;
13089 }
13090
13091 /* Disable lines in the current matrix which are now
13092 below the window. */
13093 for (++row; row < bottom_row; ++row)
13094 row->enabled_p = row->mode_line_p = 0;
13095 }
13096
13097 /* Update window_end_pos etc.; last_reused_text_row is the last
13098 reused row from the current matrix containing text, if any.
13099 The value of last_text_row is the last displayed line
13100 containing text. */
13101 if (last_reused_text_row)
13102 {
13103 w->window_end_bytepos
13104 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13105 w->window_end_pos
13106 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13107 w->window_end_vpos
13108 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13109 w->current_matrix));
13110 }
13111 else if (last_text_row)
13112 {
13113 w->window_end_bytepos
13114 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13115 w->window_end_pos
13116 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13117 w->window_end_vpos
13118 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13119 }
13120 else
13121 {
13122 /* This window must be completely empty. */
13123 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13124 w->window_end_pos = make_number (Z - ZV);
13125 w->window_end_vpos = make_number (0);
13126 }
13127 w->window_end_valid = Qnil;
13128
13129 /* Update hint: don't try scrolling again in update_window. */
13130 w->desired_matrix->no_scrolling_p = 1;
13131
13132 #if GLYPH_DEBUG
13133 debug_method_add (w, "try_window_reusing_current_matrix 1");
13134 #endif
13135 return 1;
13136 }
13137 else if (CHARPOS (new_start) > CHARPOS (start))
13138 {
13139 struct glyph_row *pt_row, *row;
13140 struct glyph_row *first_reusable_row;
13141 struct glyph_row *first_row_to_display;
13142 int dy;
13143 int yb = window_text_bottom_y (w);
13144
13145 /* Find the row starting at new_start, if there is one. Don't
13146 reuse a partially visible line at the end. */
13147 first_reusable_row = start_row;
13148 while (first_reusable_row->enabled_p
13149 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13150 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13151 < CHARPOS (new_start)))
13152 ++first_reusable_row;
13153
13154 /* Give up if there is no row to reuse. */
13155 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13156 || !first_reusable_row->enabled_p
13157 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13158 != CHARPOS (new_start)))
13159 return 0;
13160
13161 /* We can reuse fully visible rows beginning with
13162 first_reusable_row to the end of the window. Set
13163 first_row_to_display to the first row that cannot be reused.
13164 Set pt_row to the row containing point, if there is any. */
13165 pt_row = NULL;
13166 for (first_row_to_display = first_reusable_row;
13167 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13168 ++first_row_to_display)
13169 {
13170 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13171 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13172 pt_row = first_row_to_display;
13173 }
13174
13175 /* Start displaying at the start of first_row_to_display. */
13176 xassert (first_row_to_display->y < yb);
13177 init_to_row_start (&it, w, first_row_to_display);
13178
13179 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13180 - start_vpos);
13181 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13182 - nrows_scrolled);
13183 it.current_y = (first_row_to_display->y - first_reusable_row->y
13184 + WINDOW_HEADER_LINE_HEIGHT (w));
13185
13186 /* Display lines beginning with first_row_to_display in the
13187 desired matrix. Set last_text_row to the last row displayed
13188 that displays text. */
13189 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13190 if (pt_row == NULL)
13191 w->cursor.vpos = -1;
13192 last_text_row = NULL;
13193 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13194 if (display_line (&it))
13195 last_text_row = it.glyph_row - 1;
13196
13197 /* Give up If point isn't in a row displayed or reused. */
13198 if (w->cursor.vpos < 0)
13199 {
13200 clear_glyph_matrix (w->desired_matrix);
13201 return 0;
13202 }
13203
13204 /* If point is in a reused row, adjust y and vpos of the cursor
13205 position. */
13206 if (pt_row)
13207 {
13208 w->cursor.vpos -= nrows_scrolled;
13209 w->cursor.y -= first_reusable_row->y - start_row->y;
13210 }
13211
13212 /* Scroll the display. */
13213 run.current_y = first_reusable_row->y;
13214 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13215 run.height = it.last_visible_y - run.current_y;
13216 dy = run.current_y - run.desired_y;
13217
13218 if (run.height)
13219 {
13220 update_begin (f);
13221 rif->update_window_begin_hook (w);
13222 rif->clear_window_mouse_face (w);
13223 rif->scroll_run_hook (w, &run);
13224 rif->update_window_end_hook (w, 0, 0);
13225 update_end (f);
13226 }
13227
13228 /* Adjust Y positions of reused rows. */
13229 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13230 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13231 max_y = it.last_visible_y;
13232 for (row = first_reusable_row; row < first_row_to_display; ++row)
13233 {
13234 row->y -= dy;
13235 row->visible_height = row->height;
13236 if (row->y < min_y)
13237 row->visible_height -= min_y - row->y;
13238 if (row->y + row->height > max_y)
13239 row->visible_height -= row->y + row->height - max_y;
13240 row->redraw_fringe_bitmaps_p = 1;
13241 }
13242
13243 /* Scroll the current matrix. */
13244 xassert (nrows_scrolled > 0);
13245 rotate_matrix (w->current_matrix,
13246 start_vpos,
13247 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13248 -nrows_scrolled);
13249
13250 /* Disable rows not reused. */
13251 for (row -= nrows_scrolled; row < bottom_row; ++row)
13252 row->enabled_p = 0;
13253
13254 /* Point may have moved to a different line, so we cannot assume that
13255 the previous cursor position is valid; locate the correct row. */
13256 if (pt_row)
13257 {
13258 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13259 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13260 row++)
13261 {
13262 w->cursor.vpos++;
13263 w->cursor.y = row->y;
13264 }
13265 if (row < bottom_row)
13266 {
13267 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13268 while (glyph->charpos < PT)
13269 {
13270 w->cursor.hpos++;
13271 w->cursor.x += glyph->pixel_width;
13272 glyph++;
13273 }
13274 }
13275 }
13276
13277 /* Adjust window end. A null value of last_text_row means that
13278 the window end is in reused rows which in turn means that
13279 only its vpos can have changed. */
13280 if (last_text_row)
13281 {
13282 w->window_end_bytepos
13283 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13284 w->window_end_pos
13285 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13286 w->window_end_vpos
13287 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13288 }
13289 else
13290 {
13291 w->window_end_vpos
13292 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13293 }
13294
13295 w->window_end_valid = Qnil;
13296 w->desired_matrix->no_scrolling_p = 1;
13297
13298 #if GLYPH_DEBUG
13299 debug_method_add (w, "try_window_reusing_current_matrix 2");
13300 #endif
13301 return 1;
13302 }
13303
13304 return 0;
13305 }
13306
13307
13308 \f
13309 /************************************************************************
13310 Window redisplay reusing current matrix when buffer has changed
13311 ************************************************************************/
13312
13313 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13314 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13315 int *, int *));
13316 static struct glyph_row *
13317 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13318 struct glyph_row *));
13319
13320
13321 /* Return the last row in MATRIX displaying text. If row START is
13322 non-null, start searching with that row. IT gives the dimensions
13323 of the display. Value is null if matrix is empty; otherwise it is
13324 a pointer to the row found. */
13325
13326 static struct glyph_row *
13327 find_last_row_displaying_text (matrix, it, start)
13328 struct glyph_matrix *matrix;
13329 struct it *it;
13330 struct glyph_row *start;
13331 {
13332 struct glyph_row *row, *row_found;
13333
13334 /* Set row_found to the last row in IT->w's current matrix
13335 displaying text. The loop looks funny but think of partially
13336 visible lines. */
13337 row_found = NULL;
13338 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13339 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13340 {
13341 xassert (row->enabled_p);
13342 row_found = row;
13343 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13344 break;
13345 ++row;
13346 }
13347
13348 return row_found;
13349 }
13350
13351
13352 /* Return the last row in the current matrix of W that is not affected
13353 by changes at the start of current_buffer that occurred since W's
13354 current matrix was built. Value is null if no such row exists.
13355
13356 BEG_UNCHANGED us the number of characters unchanged at the start of
13357 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13358 first changed character in current_buffer. Characters at positions <
13359 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13360 when the current matrix was built. */
13361
13362 static struct glyph_row *
13363 find_last_unchanged_at_beg_row (w)
13364 struct window *w;
13365 {
13366 int first_changed_pos = BEG + BEG_UNCHANGED;
13367 struct glyph_row *row;
13368 struct glyph_row *row_found = NULL;
13369 int yb = window_text_bottom_y (w);
13370
13371 /* Find the last row displaying unchanged text. */
13372 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13373 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13374 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13375 {
13376 if (/* If row ends before first_changed_pos, it is unchanged,
13377 except in some case. */
13378 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13379 /* When row ends in ZV and we write at ZV it is not
13380 unchanged. */
13381 && !row->ends_at_zv_p
13382 /* When first_changed_pos is the end of a continued line,
13383 row is not unchanged because it may be no longer
13384 continued. */
13385 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13386 && (row->continued_p
13387 || row->exact_window_width_line_p)))
13388 row_found = row;
13389
13390 /* Stop if last visible row. */
13391 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13392 break;
13393
13394 ++row;
13395 }
13396
13397 return row_found;
13398 }
13399
13400
13401 /* Find the first glyph row in the current matrix of W that is not
13402 affected by changes at the end of current_buffer since the
13403 time W's current matrix was built.
13404
13405 Return in *DELTA the number of chars by which buffer positions in
13406 unchanged text at the end of current_buffer must be adjusted.
13407
13408 Return in *DELTA_BYTES the corresponding number of bytes.
13409
13410 Value is null if no such row exists, i.e. all rows are affected by
13411 changes. */
13412
13413 static struct glyph_row *
13414 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13415 struct window *w;
13416 int *delta, *delta_bytes;
13417 {
13418 struct glyph_row *row;
13419 struct glyph_row *row_found = NULL;
13420
13421 *delta = *delta_bytes = 0;
13422
13423 /* Display must not have been paused, otherwise the current matrix
13424 is not up to date. */
13425 if (NILP (w->window_end_valid))
13426 abort ();
13427
13428 /* A value of window_end_pos >= END_UNCHANGED means that the window
13429 end is in the range of changed text. If so, there is no
13430 unchanged row at the end of W's current matrix. */
13431 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13432 return NULL;
13433
13434 /* Set row to the last row in W's current matrix displaying text. */
13435 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13436
13437 /* If matrix is entirely empty, no unchanged row exists. */
13438 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13439 {
13440 /* The value of row is the last glyph row in the matrix having a
13441 meaningful buffer position in it. The end position of row
13442 corresponds to window_end_pos. This allows us to translate
13443 buffer positions in the current matrix to current buffer
13444 positions for characters not in changed text. */
13445 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13446 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13447 int last_unchanged_pos, last_unchanged_pos_old;
13448 struct glyph_row *first_text_row
13449 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13450
13451 *delta = Z - Z_old;
13452 *delta_bytes = Z_BYTE - Z_BYTE_old;
13453
13454 /* Set last_unchanged_pos to the buffer position of the last
13455 character in the buffer that has not been changed. Z is the
13456 index + 1 of the last character in current_buffer, i.e. by
13457 subtracting END_UNCHANGED we get the index of the last
13458 unchanged character, and we have to add BEG to get its buffer
13459 position. */
13460 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13461 last_unchanged_pos_old = last_unchanged_pos - *delta;
13462
13463 /* Search backward from ROW for a row displaying a line that
13464 starts at a minimum position >= last_unchanged_pos_old. */
13465 for (; row > first_text_row; --row)
13466 {
13467 /* This used to abort, but it can happen.
13468 It is ok to just stop the search instead here. KFS. */
13469 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13470 break;
13471
13472 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13473 row_found = row;
13474 }
13475 }
13476
13477 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13478 abort ();
13479
13480 return row_found;
13481 }
13482
13483
13484 /* Make sure that glyph rows in the current matrix of window W
13485 reference the same glyph memory as corresponding rows in the
13486 frame's frame matrix. This function is called after scrolling W's
13487 current matrix on a terminal frame in try_window_id and
13488 try_window_reusing_current_matrix. */
13489
13490 static void
13491 sync_frame_with_window_matrix_rows (w)
13492 struct window *w;
13493 {
13494 struct frame *f = XFRAME (w->frame);
13495 struct glyph_row *window_row, *window_row_end, *frame_row;
13496
13497 /* Preconditions: W must be a leaf window and full-width. Its frame
13498 must have a frame matrix. */
13499 xassert (NILP (w->hchild) && NILP (w->vchild));
13500 xassert (WINDOW_FULL_WIDTH_P (w));
13501 xassert (!FRAME_WINDOW_P (f));
13502
13503 /* If W is a full-width window, glyph pointers in W's current matrix
13504 have, by definition, to be the same as glyph pointers in the
13505 corresponding frame matrix. Note that frame matrices have no
13506 marginal areas (see build_frame_matrix). */
13507 window_row = w->current_matrix->rows;
13508 window_row_end = window_row + w->current_matrix->nrows;
13509 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13510 while (window_row < window_row_end)
13511 {
13512 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13513 struct glyph *end = window_row->glyphs[LAST_AREA];
13514
13515 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13516 frame_row->glyphs[TEXT_AREA] = start;
13517 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13518 frame_row->glyphs[LAST_AREA] = end;
13519
13520 /* Disable frame rows whose corresponding window rows have
13521 been disabled in try_window_id. */
13522 if (!window_row->enabled_p)
13523 frame_row->enabled_p = 0;
13524
13525 ++window_row, ++frame_row;
13526 }
13527 }
13528
13529
13530 /* Find the glyph row in window W containing CHARPOS. Consider all
13531 rows between START and END (not inclusive). END null means search
13532 all rows to the end of the display area of W. Value is the row
13533 containing CHARPOS or null. */
13534
13535 struct glyph_row *
13536 row_containing_pos (w, charpos, start, end, dy)
13537 struct window *w;
13538 int charpos;
13539 struct glyph_row *start, *end;
13540 int dy;
13541 {
13542 struct glyph_row *row = start;
13543 int last_y;
13544
13545 /* If we happen to start on a header-line, skip that. */
13546 if (row->mode_line_p)
13547 ++row;
13548
13549 if ((end && row >= end) || !row->enabled_p)
13550 return NULL;
13551
13552 last_y = window_text_bottom_y (w) - dy;
13553
13554 while (1)
13555 {
13556 /* Give up if we have gone too far. */
13557 if (end && row >= end)
13558 return NULL;
13559 /* This formerly returned if they were equal.
13560 I think that both quantities are of a "last plus one" type;
13561 if so, when they are equal, the row is within the screen. -- rms. */
13562 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13563 return NULL;
13564
13565 /* If it is in this row, return this row. */
13566 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13567 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13568 /* The end position of a row equals the start
13569 position of the next row. If CHARPOS is there, we
13570 would rather display it in the next line, except
13571 when this line ends in ZV. */
13572 && !row->ends_at_zv_p
13573 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13574 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13575 return row;
13576 ++row;
13577 }
13578 }
13579
13580
13581 /* Try to redisplay window W by reusing its existing display. W's
13582 current matrix must be up to date when this function is called,
13583 i.e. window_end_valid must not be nil.
13584
13585 Value is
13586
13587 1 if display has been updated
13588 0 if otherwise unsuccessful
13589 -1 if redisplay with same window start is known not to succeed
13590
13591 The following steps are performed:
13592
13593 1. Find the last row in the current matrix of W that is not
13594 affected by changes at the start of current_buffer. If no such row
13595 is found, give up.
13596
13597 2. Find the first row in W's current matrix that is not affected by
13598 changes at the end of current_buffer. Maybe there is no such row.
13599
13600 3. Display lines beginning with the row + 1 found in step 1 to the
13601 row found in step 2 or, if step 2 didn't find a row, to the end of
13602 the window.
13603
13604 4. If cursor is not known to appear on the window, give up.
13605
13606 5. If display stopped at the row found in step 2, scroll the
13607 display and current matrix as needed.
13608
13609 6. Maybe display some lines at the end of W, if we must. This can
13610 happen under various circumstances, like a partially visible line
13611 becoming fully visible, or because newly displayed lines are displayed
13612 in smaller font sizes.
13613
13614 7. Update W's window end information. */
13615
13616 static int
13617 try_window_id (w)
13618 struct window *w;
13619 {
13620 struct frame *f = XFRAME (w->frame);
13621 struct glyph_matrix *current_matrix = w->current_matrix;
13622 struct glyph_matrix *desired_matrix = w->desired_matrix;
13623 struct glyph_row *last_unchanged_at_beg_row;
13624 struct glyph_row *first_unchanged_at_end_row;
13625 struct glyph_row *row;
13626 struct glyph_row *bottom_row;
13627 int bottom_vpos;
13628 struct it it;
13629 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13630 struct text_pos start_pos;
13631 struct run run;
13632 int first_unchanged_at_end_vpos = 0;
13633 struct glyph_row *last_text_row, *last_text_row_at_end;
13634 struct text_pos start;
13635 int first_changed_charpos, last_changed_charpos;
13636
13637 #if GLYPH_DEBUG
13638 if (inhibit_try_window_id)
13639 return 0;
13640 #endif
13641
13642 /* This is handy for debugging. */
13643 #if 0
13644 #define GIVE_UP(X) \
13645 do { \
13646 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13647 return 0; \
13648 } while (0)
13649 #else
13650 #define GIVE_UP(X) return 0
13651 #endif
13652
13653 SET_TEXT_POS_FROM_MARKER (start, w->start);
13654
13655 /* Don't use this for mini-windows because these can show
13656 messages and mini-buffers, and we don't handle that here. */
13657 if (MINI_WINDOW_P (w))
13658 GIVE_UP (1);
13659
13660 /* This flag is used to prevent redisplay optimizations. */
13661 if (windows_or_buffers_changed || cursor_type_changed)
13662 GIVE_UP (2);
13663
13664 /* Verify that narrowing has not changed.
13665 Also verify that we were not told to prevent redisplay optimizations.
13666 It would be nice to further
13667 reduce the number of cases where this prevents try_window_id. */
13668 if (current_buffer->clip_changed
13669 || current_buffer->prevent_redisplay_optimizations_p)
13670 GIVE_UP (3);
13671
13672 /* Window must either use window-based redisplay or be full width. */
13673 if (!FRAME_WINDOW_P (f)
13674 && (!line_ins_del_ok
13675 || !WINDOW_FULL_WIDTH_P (w)))
13676 GIVE_UP (4);
13677
13678 /* Give up if point is not known NOT to appear in W. */
13679 if (PT < CHARPOS (start))
13680 GIVE_UP (5);
13681
13682 /* Another way to prevent redisplay optimizations. */
13683 if (XFASTINT (w->last_modified) == 0)
13684 GIVE_UP (6);
13685
13686 /* Verify that window is not hscrolled. */
13687 if (XFASTINT (w->hscroll) != 0)
13688 GIVE_UP (7);
13689
13690 /* Verify that display wasn't paused. */
13691 if (NILP (w->window_end_valid))
13692 GIVE_UP (8);
13693
13694 /* Can't use this if highlighting a region because a cursor movement
13695 will do more than just set the cursor. */
13696 if (!NILP (Vtransient_mark_mode)
13697 && !NILP (current_buffer->mark_active))
13698 GIVE_UP (9);
13699
13700 /* Likewise if highlighting trailing whitespace. */
13701 if (!NILP (Vshow_trailing_whitespace))
13702 GIVE_UP (11);
13703
13704 /* Likewise if showing a region. */
13705 if (!NILP (w->region_showing))
13706 GIVE_UP (10);
13707
13708 /* Can use this if overlay arrow position and or string have changed. */
13709 if (overlay_arrows_changed_p ())
13710 GIVE_UP (12);
13711
13712
13713 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13714 only if buffer has really changed. The reason is that the gap is
13715 initially at Z for freshly visited files. The code below would
13716 set end_unchanged to 0 in that case. */
13717 if (MODIFF > SAVE_MODIFF
13718 /* This seems to happen sometimes after saving a buffer. */
13719 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13720 {
13721 if (GPT - BEG < BEG_UNCHANGED)
13722 BEG_UNCHANGED = GPT - BEG;
13723 if (Z - GPT < END_UNCHANGED)
13724 END_UNCHANGED = Z - GPT;
13725 }
13726
13727 /* The position of the first and last character that has been changed. */
13728 first_changed_charpos = BEG + BEG_UNCHANGED;
13729 last_changed_charpos = Z - END_UNCHANGED;
13730
13731 /* If window starts after a line end, and the last change is in
13732 front of that newline, then changes don't affect the display.
13733 This case happens with stealth-fontification. Note that although
13734 the display is unchanged, glyph positions in the matrix have to
13735 be adjusted, of course. */
13736 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13737 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13738 && ((last_changed_charpos < CHARPOS (start)
13739 && CHARPOS (start) == BEGV)
13740 || (last_changed_charpos < CHARPOS (start) - 1
13741 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13742 {
13743 int Z_old, delta, Z_BYTE_old, delta_bytes;
13744 struct glyph_row *r0;
13745
13746 /* Compute how many chars/bytes have been added to or removed
13747 from the buffer. */
13748 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13749 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13750 delta = Z - Z_old;
13751 delta_bytes = Z_BYTE - Z_BYTE_old;
13752
13753 /* Give up if PT is not in the window. Note that it already has
13754 been checked at the start of try_window_id that PT is not in
13755 front of the window start. */
13756 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13757 GIVE_UP (13);
13758
13759 /* If window start is unchanged, we can reuse the whole matrix
13760 as is, after adjusting glyph positions. No need to compute
13761 the window end again, since its offset from Z hasn't changed. */
13762 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13763 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13764 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13765 /* PT must not be in a partially visible line. */
13766 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13767 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13768 {
13769 /* Adjust positions in the glyph matrix. */
13770 if (delta || delta_bytes)
13771 {
13772 struct glyph_row *r1
13773 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13774 increment_matrix_positions (w->current_matrix,
13775 MATRIX_ROW_VPOS (r0, current_matrix),
13776 MATRIX_ROW_VPOS (r1, current_matrix),
13777 delta, delta_bytes);
13778 }
13779
13780 /* Set the cursor. */
13781 row = row_containing_pos (w, PT, r0, NULL, 0);
13782 if (row)
13783 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13784 else
13785 abort ();
13786 return 1;
13787 }
13788 }
13789
13790 /* Handle the case that changes are all below what is displayed in
13791 the window, and that PT is in the window. This shortcut cannot
13792 be taken if ZV is visible in the window, and text has been added
13793 there that is visible in the window. */
13794 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13795 /* ZV is not visible in the window, or there are no
13796 changes at ZV, actually. */
13797 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13798 || first_changed_charpos == last_changed_charpos))
13799 {
13800 struct glyph_row *r0;
13801
13802 /* Give up if PT is not in the window. Note that it already has
13803 been checked at the start of try_window_id that PT is not in
13804 front of the window start. */
13805 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13806 GIVE_UP (14);
13807
13808 /* If window start is unchanged, we can reuse the whole matrix
13809 as is, without changing glyph positions since no text has
13810 been added/removed in front of the window end. */
13811 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13812 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13813 /* PT must not be in a partially visible line. */
13814 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13815 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13816 {
13817 /* We have to compute the window end anew since text
13818 can have been added/removed after it. */
13819 w->window_end_pos
13820 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13821 w->window_end_bytepos
13822 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13823
13824 /* Set the cursor. */
13825 row = row_containing_pos (w, PT, r0, NULL, 0);
13826 if (row)
13827 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13828 else
13829 abort ();
13830 return 2;
13831 }
13832 }
13833
13834 /* Give up if window start is in the changed area.
13835
13836 The condition used to read
13837
13838 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13839
13840 but why that was tested escapes me at the moment. */
13841 if (CHARPOS (start) >= first_changed_charpos
13842 && CHARPOS (start) <= last_changed_charpos)
13843 GIVE_UP (15);
13844
13845 /* Check that window start agrees with the start of the first glyph
13846 row in its current matrix. Check this after we know the window
13847 start is not in changed text, otherwise positions would not be
13848 comparable. */
13849 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13850 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13851 GIVE_UP (16);
13852
13853 /* Give up if the window ends in strings. Overlay strings
13854 at the end are difficult to handle, so don't try. */
13855 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13856 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13857 GIVE_UP (20);
13858
13859 /* Compute the position at which we have to start displaying new
13860 lines. Some of the lines at the top of the window might be
13861 reusable because they are not displaying changed text. Find the
13862 last row in W's current matrix not affected by changes at the
13863 start of current_buffer. Value is null if changes start in the
13864 first line of window. */
13865 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13866 if (last_unchanged_at_beg_row)
13867 {
13868 /* Avoid starting to display in the moddle of a character, a TAB
13869 for instance. This is easier than to set up the iterator
13870 exactly, and it's not a frequent case, so the additional
13871 effort wouldn't really pay off. */
13872 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13873 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13874 && last_unchanged_at_beg_row > w->current_matrix->rows)
13875 --last_unchanged_at_beg_row;
13876
13877 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13878 GIVE_UP (17);
13879
13880 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13881 GIVE_UP (18);
13882 start_pos = it.current.pos;
13883
13884 /* Start displaying new lines in the desired matrix at the same
13885 vpos we would use in the current matrix, i.e. below
13886 last_unchanged_at_beg_row. */
13887 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13888 current_matrix);
13889 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13890 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13891
13892 xassert (it.hpos == 0 && it.current_x == 0);
13893 }
13894 else
13895 {
13896 /* There are no reusable lines at the start of the window.
13897 Start displaying in the first text line. */
13898 start_display (&it, w, start);
13899 it.vpos = it.first_vpos;
13900 start_pos = it.current.pos;
13901 }
13902
13903 /* Find the first row that is not affected by changes at the end of
13904 the buffer. Value will be null if there is no unchanged row, in
13905 which case we must redisplay to the end of the window. delta
13906 will be set to the value by which buffer positions beginning with
13907 first_unchanged_at_end_row have to be adjusted due to text
13908 changes. */
13909 first_unchanged_at_end_row
13910 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13911 IF_DEBUG (debug_delta = delta);
13912 IF_DEBUG (debug_delta_bytes = delta_bytes);
13913
13914 /* Set stop_pos to the buffer position up to which we will have to
13915 display new lines. If first_unchanged_at_end_row != NULL, this
13916 is the buffer position of the start of the line displayed in that
13917 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13918 that we don't stop at a buffer position. */
13919 stop_pos = 0;
13920 if (first_unchanged_at_end_row)
13921 {
13922 xassert (last_unchanged_at_beg_row == NULL
13923 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13924
13925 /* If this is a continuation line, move forward to the next one
13926 that isn't. Changes in lines above affect this line.
13927 Caution: this may move first_unchanged_at_end_row to a row
13928 not displaying text. */
13929 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13930 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13931 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13932 < it.last_visible_y))
13933 ++first_unchanged_at_end_row;
13934
13935 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13936 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13937 >= it.last_visible_y))
13938 first_unchanged_at_end_row = NULL;
13939 else
13940 {
13941 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13942 + delta);
13943 first_unchanged_at_end_vpos
13944 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13945 xassert (stop_pos >= Z - END_UNCHANGED);
13946 }
13947 }
13948 else if (last_unchanged_at_beg_row == NULL)
13949 GIVE_UP (19);
13950
13951
13952 #if GLYPH_DEBUG
13953
13954 /* Either there is no unchanged row at the end, or the one we have
13955 now displays text. This is a necessary condition for the window
13956 end pos calculation at the end of this function. */
13957 xassert (first_unchanged_at_end_row == NULL
13958 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13959
13960 debug_last_unchanged_at_beg_vpos
13961 = (last_unchanged_at_beg_row
13962 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13963 : -1);
13964 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13965
13966 #endif /* GLYPH_DEBUG != 0 */
13967
13968
13969 /* Display new lines. Set last_text_row to the last new line
13970 displayed which has text on it, i.e. might end up as being the
13971 line where the window_end_vpos is. */
13972 w->cursor.vpos = -1;
13973 last_text_row = NULL;
13974 overlay_arrow_seen = 0;
13975 while (it.current_y < it.last_visible_y
13976 && !fonts_changed_p
13977 && (first_unchanged_at_end_row == NULL
13978 || IT_CHARPOS (it) < stop_pos))
13979 {
13980 if (display_line (&it))
13981 last_text_row = it.glyph_row - 1;
13982 }
13983
13984 if (fonts_changed_p)
13985 return -1;
13986
13987
13988 /* Compute differences in buffer positions, y-positions etc. for
13989 lines reused at the bottom of the window. Compute what we can
13990 scroll. */
13991 if (first_unchanged_at_end_row
13992 /* No lines reused because we displayed everything up to the
13993 bottom of the window. */
13994 && it.current_y < it.last_visible_y)
13995 {
13996 dvpos = (it.vpos
13997 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13998 current_matrix));
13999 dy = it.current_y - first_unchanged_at_end_row->y;
14000 run.current_y = first_unchanged_at_end_row->y;
14001 run.desired_y = run.current_y + dy;
14002 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14003 }
14004 else
14005 {
14006 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14007 first_unchanged_at_end_row = NULL;
14008 }
14009 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14010
14011
14012 /* Find the cursor if not already found. We have to decide whether
14013 PT will appear on this window (it sometimes doesn't, but this is
14014 not a very frequent case.) This decision has to be made before
14015 the current matrix is altered. A value of cursor.vpos < 0 means
14016 that PT is either in one of the lines beginning at
14017 first_unchanged_at_end_row or below the window. Don't care for
14018 lines that might be displayed later at the window end; as
14019 mentioned, this is not a frequent case. */
14020 if (w->cursor.vpos < 0)
14021 {
14022 /* Cursor in unchanged rows at the top? */
14023 if (PT < CHARPOS (start_pos)
14024 && last_unchanged_at_beg_row)
14025 {
14026 row = row_containing_pos (w, PT,
14027 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14028 last_unchanged_at_beg_row + 1, 0);
14029 if (row)
14030 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14031 }
14032
14033 /* Start from first_unchanged_at_end_row looking for PT. */
14034 else if (first_unchanged_at_end_row)
14035 {
14036 row = row_containing_pos (w, PT - delta,
14037 first_unchanged_at_end_row, NULL, 0);
14038 if (row)
14039 set_cursor_from_row (w, row, w->current_matrix, delta,
14040 delta_bytes, dy, dvpos);
14041 }
14042
14043 /* Give up if cursor was not found. */
14044 if (w->cursor.vpos < 0)
14045 {
14046 clear_glyph_matrix (w->desired_matrix);
14047 return -1;
14048 }
14049 }
14050
14051 /* Don't let the cursor end in the scroll margins. */
14052 {
14053 int this_scroll_margin, cursor_height;
14054
14055 this_scroll_margin = max (0, scroll_margin);
14056 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14057 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14058 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14059
14060 if ((w->cursor.y < this_scroll_margin
14061 && CHARPOS (start) > BEGV)
14062 /* Old redisplay didn't take scroll margin into account at the bottom,
14063 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14064 || (w->cursor.y + (make_cursor_line_fully_visible_p
14065 ? cursor_height + this_scroll_margin
14066 : 1)) > it.last_visible_y)
14067 {
14068 w->cursor.vpos = -1;
14069 clear_glyph_matrix (w->desired_matrix);
14070 return -1;
14071 }
14072 }
14073
14074 /* Scroll the display. Do it before changing the current matrix so
14075 that xterm.c doesn't get confused about where the cursor glyph is
14076 found. */
14077 if (dy && run.height)
14078 {
14079 update_begin (f);
14080
14081 if (FRAME_WINDOW_P (f))
14082 {
14083 rif->update_window_begin_hook (w);
14084 rif->clear_window_mouse_face (w);
14085 rif->scroll_run_hook (w, &run);
14086 rif->update_window_end_hook (w, 0, 0);
14087 }
14088 else
14089 {
14090 /* Terminal frame. In this case, dvpos gives the number of
14091 lines to scroll by; dvpos < 0 means scroll up. */
14092 int first_unchanged_at_end_vpos
14093 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14094 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14095 int end = (WINDOW_TOP_EDGE_LINE (w)
14096 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14097 + window_internal_height (w));
14098
14099 /* Perform the operation on the screen. */
14100 if (dvpos > 0)
14101 {
14102 /* Scroll last_unchanged_at_beg_row to the end of the
14103 window down dvpos lines. */
14104 set_terminal_window (end);
14105
14106 /* On dumb terminals delete dvpos lines at the end
14107 before inserting dvpos empty lines. */
14108 if (!scroll_region_ok)
14109 ins_del_lines (end - dvpos, -dvpos);
14110
14111 /* Insert dvpos empty lines in front of
14112 last_unchanged_at_beg_row. */
14113 ins_del_lines (from, dvpos);
14114 }
14115 else if (dvpos < 0)
14116 {
14117 /* Scroll up last_unchanged_at_beg_vpos to the end of
14118 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14119 set_terminal_window (end);
14120
14121 /* Delete dvpos lines in front of
14122 last_unchanged_at_beg_vpos. ins_del_lines will set
14123 the cursor to the given vpos and emit |dvpos| delete
14124 line sequences. */
14125 ins_del_lines (from + dvpos, dvpos);
14126
14127 /* On a dumb terminal insert dvpos empty lines at the
14128 end. */
14129 if (!scroll_region_ok)
14130 ins_del_lines (end + dvpos, -dvpos);
14131 }
14132
14133 set_terminal_window (0);
14134 }
14135
14136 update_end (f);
14137 }
14138
14139 /* Shift reused rows of the current matrix to the right position.
14140 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14141 text. */
14142 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14143 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14144 if (dvpos < 0)
14145 {
14146 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14147 bottom_vpos, dvpos);
14148 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14149 bottom_vpos, 0);
14150 }
14151 else if (dvpos > 0)
14152 {
14153 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14154 bottom_vpos, dvpos);
14155 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14156 first_unchanged_at_end_vpos + dvpos, 0);
14157 }
14158
14159 /* For frame-based redisplay, make sure that current frame and window
14160 matrix are in sync with respect to glyph memory. */
14161 if (!FRAME_WINDOW_P (f))
14162 sync_frame_with_window_matrix_rows (w);
14163
14164 /* Adjust buffer positions in reused rows. */
14165 if (delta)
14166 increment_matrix_positions (current_matrix,
14167 first_unchanged_at_end_vpos + dvpos,
14168 bottom_vpos, delta, delta_bytes);
14169
14170 /* Adjust Y positions. */
14171 if (dy)
14172 shift_glyph_matrix (w, current_matrix,
14173 first_unchanged_at_end_vpos + dvpos,
14174 bottom_vpos, dy);
14175
14176 if (first_unchanged_at_end_row)
14177 {
14178 first_unchanged_at_end_row += dvpos;
14179 if (first_unchanged_at_end_row->y >= it.last_visible_y
14180 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14181 first_unchanged_at_end_row = NULL;
14182 }
14183
14184 /* If scrolling up, there may be some lines to display at the end of
14185 the window. */
14186 last_text_row_at_end = NULL;
14187 if (dy < 0)
14188 {
14189 /* Scrolling up can leave for example a partially visible line
14190 at the end of the window to be redisplayed. */
14191 /* Set last_row to the glyph row in the current matrix where the
14192 window end line is found. It has been moved up or down in
14193 the matrix by dvpos. */
14194 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14195 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14196
14197 /* If last_row is the window end line, it should display text. */
14198 xassert (last_row->displays_text_p);
14199
14200 /* If window end line was partially visible before, begin
14201 displaying at that line. Otherwise begin displaying with the
14202 line following it. */
14203 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14204 {
14205 init_to_row_start (&it, w, last_row);
14206 it.vpos = last_vpos;
14207 it.current_y = last_row->y;
14208 }
14209 else
14210 {
14211 init_to_row_end (&it, w, last_row);
14212 it.vpos = 1 + last_vpos;
14213 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14214 ++last_row;
14215 }
14216
14217 /* We may start in a continuation line. If so, we have to
14218 get the right continuation_lines_width and current_x. */
14219 it.continuation_lines_width = last_row->continuation_lines_width;
14220 it.hpos = it.current_x = 0;
14221
14222 /* Display the rest of the lines at the window end. */
14223 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14224 while (it.current_y < it.last_visible_y
14225 && !fonts_changed_p)
14226 {
14227 /* Is it always sure that the display agrees with lines in
14228 the current matrix? I don't think so, so we mark rows
14229 displayed invalid in the current matrix by setting their
14230 enabled_p flag to zero. */
14231 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14232 if (display_line (&it))
14233 last_text_row_at_end = it.glyph_row - 1;
14234 }
14235 }
14236
14237 /* Update window_end_pos and window_end_vpos. */
14238 if (first_unchanged_at_end_row
14239 && !last_text_row_at_end)
14240 {
14241 /* Window end line if one of the preserved rows from the current
14242 matrix. Set row to the last row displaying text in current
14243 matrix starting at first_unchanged_at_end_row, after
14244 scrolling. */
14245 xassert (first_unchanged_at_end_row->displays_text_p);
14246 row = find_last_row_displaying_text (w->current_matrix, &it,
14247 first_unchanged_at_end_row);
14248 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14249
14250 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14251 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14252 w->window_end_vpos
14253 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14254 xassert (w->window_end_bytepos >= 0);
14255 IF_DEBUG (debug_method_add (w, "A"));
14256 }
14257 else if (last_text_row_at_end)
14258 {
14259 w->window_end_pos
14260 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14261 w->window_end_bytepos
14262 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14263 w->window_end_vpos
14264 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14265 xassert (w->window_end_bytepos >= 0);
14266 IF_DEBUG (debug_method_add (w, "B"));
14267 }
14268 else if (last_text_row)
14269 {
14270 /* We have displayed either to the end of the window or at the
14271 end of the window, i.e. the last row with text is to be found
14272 in the desired matrix. */
14273 w->window_end_pos
14274 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14275 w->window_end_bytepos
14276 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14277 w->window_end_vpos
14278 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14279 xassert (w->window_end_bytepos >= 0);
14280 }
14281 else if (first_unchanged_at_end_row == NULL
14282 && last_text_row == NULL
14283 && last_text_row_at_end == NULL)
14284 {
14285 /* Displayed to end of window, but no line containing text was
14286 displayed. Lines were deleted at the end of the window. */
14287 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14288 int vpos = XFASTINT (w->window_end_vpos);
14289 struct glyph_row *current_row = current_matrix->rows + vpos;
14290 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14291
14292 for (row = NULL;
14293 row == NULL && vpos >= first_vpos;
14294 --vpos, --current_row, --desired_row)
14295 {
14296 if (desired_row->enabled_p)
14297 {
14298 if (desired_row->displays_text_p)
14299 row = desired_row;
14300 }
14301 else if (current_row->displays_text_p)
14302 row = current_row;
14303 }
14304
14305 xassert (row != NULL);
14306 w->window_end_vpos = make_number (vpos + 1);
14307 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14308 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14309 xassert (w->window_end_bytepos >= 0);
14310 IF_DEBUG (debug_method_add (w, "C"));
14311 }
14312 else
14313 abort ();
14314
14315 #if 0 /* This leads to problems, for instance when the cursor is
14316 at ZV, and the cursor line displays no text. */
14317 /* Disable rows below what's displayed in the window. This makes
14318 debugging easier. */
14319 enable_glyph_matrix_rows (current_matrix,
14320 XFASTINT (w->window_end_vpos) + 1,
14321 bottom_vpos, 0);
14322 #endif
14323
14324 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14325 debug_end_vpos = XFASTINT (w->window_end_vpos));
14326
14327 /* Record that display has not been completed. */
14328 w->window_end_valid = Qnil;
14329 w->desired_matrix->no_scrolling_p = 1;
14330 return 3;
14331
14332 #undef GIVE_UP
14333 }
14334
14335
14336 \f
14337 /***********************************************************************
14338 More debugging support
14339 ***********************************************************************/
14340
14341 #if GLYPH_DEBUG
14342
14343 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14344 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14345 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14346
14347
14348 /* Dump the contents of glyph matrix MATRIX on stderr.
14349
14350 GLYPHS 0 means don't show glyph contents.
14351 GLYPHS 1 means show glyphs in short form
14352 GLYPHS > 1 means show glyphs in long form. */
14353
14354 void
14355 dump_glyph_matrix (matrix, glyphs)
14356 struct glyph_matrix *matrix;
14357 int glyphs;
14358 {
14359 int i;
14360 for (i = 0; i < matrix->nrows; ++i)
14361 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14362 }
14363
14364
14365 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14366 the glyph row and area where the glyph comes from. */
14367
14368 void
14369 dump_glyph (row, glyph, area)
14370 struct glyph_row *row;
14371 struct glyph *glyph;
14372 int area;
14373 {
14374 if (glyph->type == CHAR_GLYPH)
14375 {
14376 fprintf (stderr,
14377 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14378 glyph - row->glyphs[TEXT_AREA],
14379 'C',
14380 glyph->charpos,
14381 (BUFFERP (glyph->object)
14382 ? 'B'
14383 : (STRINGP (glyph->object)
14384 ? 'S'
14385 : '-')),
14386 glyph->pixel_width,
14387 glyph->u.ch,
14388 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14389 ? glyph->u.ch
14390 : '.'),
14391 glyph->face_id,
14392 glyph->left_box_line_p,
14393 glyph->right_box_line_p);
14394 }
14395 else if (glyph->type == STRETCH_GLYPH)
14396 {
14397 fprintf (stderr,
14398 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14399 glyph - row->glyphs[TEXT_AREA],
14400 'S',
14401 glyph->charpos,
14402 (BUFFERP (glyph->object)
14403 ? 'B'
14404 : (STRINGP (glyph->object)
14405 ? 'S'
14406 : '-')),
14407 glyph->pixel_width,
14408 0,
14409 '.',
14410 glyph->face_id,
14411 glyph->left_box_line_p,
14412 glyph->right_box_line_p);
14413 }
14414 else if (glyph->type == IMAGE_GLYPH)
14415 {
14416 fprintf (stderr,
14417 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14418 glyph - row->glyphs[TEXT_AREA],
14419 'I',
14420 glyph->charpos,
14421 (BUFFERP (glyph->object)
14422 ? 'B'
14423 : (STRINGP (glyph->object)
14424 ? 'S'
14425 : '-')),
14426 glyph->pixel_width,
14427 glyph->u.img_id,
14428 '.',
14429 glyph->face_id,
14430 glyph->left_box_line_p,
14431 glyph->right_box_line_p);
14432 }
14433 }
14434
14435
14436 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14437 GLYPHS 0 means don't show glyph contents.
14438 GLYPHS 1 means show glyphs in short form
14439 GLYPHS > 1 means show glyphs in long form. */
14440
14441 void
14442 dump_glyph_row (row, vpos, glyphs)
14443 struct glyph_row *row;
14444 int vpos, glyphs;
14445 {
14446 if (glyphs != 1)
14447 {
14448 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14449 fprintf (stderr, "======================================================================\n");
14450
14451 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14452 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14453 vpos,
14454 MATRIX_ROW_START_CHARPOS (row),
14455 MATRIX_ROW_END_CHARPOS (row),
14456 row->used[TEXT_AREA],
14457 row->contains_overlapping_glyphs_p,
14458 row->enabled_p,
14459 row->truncated_on_left_p,
14460 row->truncated_on_right_p,
14461 row->continued_p,
14462 MATRIX_ROW_CONTINUATION_LINE_P (row),
14463 row->displays_text_p,
14464 row->ends_at_zv_p,
14465 row->fill_line_p,
14466 row->ends_in_middle_of_char_p,
14467 row->starts_in_middle_of_char_p,
14468 row->mouse_face_p,
14469 row->x,
14470 row->y,
14471 row->pixel_width,
14472 row->height,
14473 row->visible_height,
14474 row->ascent,
14475 row->phys_ascent);
14476 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14477 row->end.overlay_string_index,
14478 row->continuation_lines_width);
14479 fprintf (stderr, "%9d %5d\n",
14480 CHARPOS (row->start.string_pos),
14481 CHARPOS (row->end.string_pos));
14482 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14483 row->end.dpvec_index);
14484 }
14485
14486 if (glyphs > 1)
14487 {
14488 int area;
14489
14490 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14491 {
14492 struct glyph *glyph = row->glyphs[area];
14493 struct glyph *glyph_end = glyph + row->used[area];
14494
14495 /* Glyph for a line end in text. */
14496 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14497 ++glyph_end;
14498
14499 if (glyph < glyph_end)
14500 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14501
14502 for (; glyph < glyph_end; ++glyph)
14503 dump_glyph (row, glyph, area);
14504 }
14505 }
14506 else if (glyphs == 1)
14507 {
14508 int area;
14509
14510 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14511 {
14512 char *s = (char *) alloca (row->used[area] + 1);
14513 int i;
14514
14515 for (i = 0; i < row->used[area]; ++i)
14516 {
14517 struct glyph *glyph = row->glyphs[area] + i;
14518 if (glyph->type == CHAR_GLYPH
14519 && glyph->u.ch < 0x80
14520 && glyph->u.ch >= ' ')
14521 s[i] = glyph->u.ch;
14522 else
14523 s[i] = '.';
14524 }
14525
14526 s[i] = '\0';
14527 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14528 }
14529 }
14530 }
14531
14532
14533 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14534 Sdump_glyph_matrix, 0, 1, "p",
14535 doc: /* Dump the current matrix of the selected window to stderr.
14536 Shows contents of glyph row structures. With non-nil
14537 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14538 glyphs in short form, otherwise show glyphs in long form. */)
14539 (glyphs)
14540 Lisp_Object glyphs;
14541 {
14542 struct window *w = XWINDOW (selected_window);
14543 struct buffer *buffer = XBUFFER (w->buffer);
14544
14545 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14546 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14547 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14548 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14549 fprintf (stderr, "=============================================\n");
14550 dump_glyph_matrix (w->current_matrix,
14551 NILP (glyphs) ? 0 : XINT (glyphs));
14552 return Qnil;
14553 }
14554
14555
14556 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14557 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14558 ()
14559 {
14560 struct frame *f = XFRAME (selected_frame);
14561 dump_glyph_matrix (f->current_matrix, 1);
14562 return Qnil;
14563 }
14564
14565
14566 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14567 doc: /* Dump glyph row ROW to stderr.
14568 GLYPH 0 means don't dump glyphs.
14569 GLYPH 1 means dump glyphs in short form.
14570 GLYPH > 1 or omitted means dump glyphs in long form. */)
14571 (row, glyphs)
14572 Lisp_Object row, glyphs;
14573 {
14574 struct glyph_matrix *matrix;
14575 int vpos;
14576
14577 CHECK_NUMBER (row);
14578 matrix = XWINDOW (selected_window)->current_matrix;
14579 vpos = XINT (row);
14580 if (vpos >= 0 && vpos < matrix->nrows)
14581 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14582 vpos,
14583 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14584 return Qnil;
14585 }
14586
14587
14588 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14589 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14590 GLYPH 0 means don't dump glyphs.
14591 GLYPH 1 means dump glyphs in short form.
14592 GLYPH > 1 or omitted means dump glyphs in long form. */)
14593 (row, glyphs)
14594 Lisp_Object row, glyphs;
14595 {
14596 struct frame *sf = SELECTED_FRAME ();
14597 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14598 int vpos;
14599
14600 CHECK_NUMBER (row);
14601 vpos = XINT (row);
14602 if (vpos >= 0 && vpos < m->nrows)
14603 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14604 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14605 return Qnil;
14606 }
14607
14608
14609 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14610 doc: /* Toggle tracing of redisplay.
14611 With ARG, turn tracing on if and only if ARG is positive. */)
14612 (arg)
14613 Lisp_Object arg;
14614 {
14615 if (NILP (arg))
14616 trace_redisplay_p = !trace_redisplay_p;
14617 else
14618 {
14619 arg = Fprefix_numeric_value (arg);
14620 trace_redisplay_p = XINT (arg) > 0;
14621 }
14622
14623 return Qnil;
14624 }
14625
14626
14627 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14628 doc: /* Like `format', but print result to stderr.
14629 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14630 (nargs, args)
14631 int nargs;
14632 Lisp_Object *args;
14633 {
14634 Lisp_Object s = Fformat (nargs, args);
14635 fprintf (stderr, "%s", SDATA (s));
14636 return Qnil;
14637 }
14638
14639 #endif /* GLYPH_DEBUG */
14640
14641
14642 \f
14643 /***********************************************************************
14644 Building Desired Matrix Rows
14645 ***********************************************************************/
14646
14647 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14648 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14649
14650 static struct glyph_row *
14651 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14652 struct window *w;
14653 Lisp_Object overlay_arrow_string;
14654 {
14655 struct frame *f = XFRAME (WINDOW_FRAME (w));
14656 struct buffer *buffer = XBUFFER (w->buffer);
14657 struct buffer *old = current_buffer;
14658 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14659 int arrow_len = SCHARS (overlay_arrow_string);
14660 const unsigned char *arrow_end = arrow_string + arrow_len;
14661 const unsigned char *p;
14662 struct it it;
14663 int multibyte_p;
14664 int n_glyphs_before;
14665
14666 set_buffer_temp (buffer);
14667 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14668 it.glyph_row->used[TEXT_AREA] = 0;
14669 SET_TEXT_POS (it.position, 0, 0);
14670
14671 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14672 p = arrow_string;
14673 while (p < arrow_end)
14674 {
14675 Lisp_Object face, ilisp;
14676
14677 /* Get the next character. */
14678 if (multibyte_p)
14679 it.c = string_char_and_length (p, arrow_len, &it.len);
14680 else
14681 it.c = *p, it.len = 1;
14682 p += it.len;
14683
14684 /* Get its face. */
14685 ilisp = make_number (p - arrow_string);
14686 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14687 it.face_id = compute_char_face (f, it.c, face);
14688
14689 /* Compute its width, get its glyphs. */
14690 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14691 SET_TEXT_POS (it.position, -1, -1);
14692 PRODUCE_GLYPHS (&it);
14693
14694 /* If this character doesn't fit any more in the line, we have
14695 to remove some glyphs. */
14696 if (it.current_x > it.last_visible_x)
14697 {
14698 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14699 break;
14700 }
14701 }
14702
14703 set_buffer_temp (old);
14704 return it.glyph_row;
14705 }
14706
14707
14708 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14709 glyphs are only inserted for terminal frames since we can't really
14710 win with truncation glyphs when partially visible glyphs are
14711 involved. Which glyphs to insert is determined by
14712 produce_special_glyphs. */
14713
14714 static void
14715 insert_left_trunc_glyphs (it)
14716 struct it *it;
14717 {
14718 struct it truncate_it;
14719 struct glyph *from, *end, *to, *toend;
14720
14721 xassert (!FRAME_WINDOW_P (it->f));
14722
14723 /* Get the truncation glyphs. */
14724 truncate_it = *it;
14725 truncate_it.current_x = 0;
14726 truncate_it.face_id = DEFAULT_FACE_ID;
14727 truncate_it.glyph_row = &scratch_glyph_row;
14728 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14729 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14730 truncate_it.object = make_number (0);
14731 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14732
14733 /* Overwrite glyphs from IT with truncation glyphs. */
14734 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14735 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14736 to = it->glyph_row->glyphs[TEXT_AREA];
14737 toend = to + it->glyph_row->used[TEXT_AREA];
14738
14739 while (from < end)
14740 *to++ = *from++;
14741
14742 /* There may be padding glyphs left over. Overwrite them too. */
14743 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14744 {
14745 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14746 while (from < end)
14747 *to++ = *from++;
14748 }
14749
14750 if (to > toend)
14751 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14752 }
14753
14754
14755 /* Compute the pixel height and width of IT->glyph_row.
14756
14757 Most of the time, ascent and height of a display line will be equal
14758 to the max_ascent and max_height values of the display iterator
14759 structure. This is not the case if
14760
14761 1. We hit ZV without displaying anything. In this case, max_ascent
14762 and max_height will be zero.
14763
14764 2. We have some glyphs that don't contribute to the line height.
14765 (The glyph row flag contributes_to_line_height_p is for future
14766 pixmap extensions).
14767
14768 The first case is easily covered by using default values because in
14769 these cases, the line height does not really matter, except that it
14770 must not be zero. */
14771
14772 static void
14773 compute_line_metrics (it)
14774 struct it *it;
14775 {
14776 struct glyph_row *row = it->glyph_row;
14777 int area, i;
14778
14779 if (FRAME_WINDOW_P (it->f))
14780 {
14781 int i, min_y, max_y;
14782
14783 /* The line may consist of one space only, that was added to
14784 place the cursor on it. If so, the row's height hasn't been
14785 computed yet. */
14786 if (row->height == 0)
14787 {
14788 if (it->max_ascent + it->max_descent == 0)
14789 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14790 row->ascent = it->max_ascent;
14791 row->height = it->max_ascent + it->max_descent;
14792 row->phys_ascent = it->max_phys_ascent;
14793 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14794 row->extra_line_spacing = it->max_extra_line_spacing;
14795 }
14796
14797 /* Compute the width of this line. */
14798 row->pixel_width = row->x;
14799 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14800 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14801
14802 xassert (row->pixel_width >= 0);
14803 xassert (row->ascent >= 0 && row->height > 0);
14804
14805 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14806 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14807
14808 /* If first line's physical ascent is larger than its logical
14809 ascent, use the physical ascent, and make the row taller.
14810 This makes accented characters fully visible. */
14811 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14812 && row->phys_ascent > row->ascent)
14813 {
14814 row->height += row->phys_ascent - row->ascent;
14815 row->ascent = row->phys_ascent;
14816 }
14817
14818 /* Compute how much of the line is visible. */
14819 row->visible_height = row->height;
14820
14821 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14822 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14823
14824 if (row->y < min_y)
14825 row->visible_height -= min_y - row->y;
14826 if (row->y + row->height > max_y)
14827 row->visible_height -= row->y + row->height - max_y;
14828 }
14829 else
14830 {
14831 row->pixel_width = row->used[TEXT_AREA];
14832 if (row->continued_p)
14833 row->pixel_width -= it->continuation_pixel_width;
14834 else if (row->truncated_on_right_p)
14835 row->pixel_width -= it->truncation_pixel_width;
14836 row->ascent = row->phys_ascent = 0;
14837 row->height = row->phys_height = row->visible_height = 1;
14838 row->extra_line_spacing = 0;
14839 }
14840
14841 /* Compute a hash code for this row. */
14842 row->hash = 0;
14843 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14844 for (i = 0; i < row->used[area]; ++i)
14845 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14846 + row->glyphs[area][i].u.val
14847 + row->glyphs[area][i].face_id
14848 + row->glyphs[area][i].padding_p
14849 + (row->glyphs[area][i].type << 2));
14850
14851 it->max_ascent = it->max_descent = 0;
14852 it->max_phys_ascent = it->max_phys_descent = 0;
14853 }
14854
14855
14856 /* Append one space to the glyph row of iterator IT if doing a
14857 window-based redisplay. The space has the same face as
14858 IT->face_id. Value is non-zero if a space was added.
14859
14860 This function is called to make sure that there is always one glyph
14861 at the end of a glyph row that the cursor can be set on under
14862 window-systems. (If there weren't such a glyph we would not know
14863 how wide and tall a box cursor should be displayed).
14864
14865 At the same time this space let's a nicely handle clearing to the
14866 end of the line if the row ends in italic text. */
14867
14868 static int
14869 append_space_for_newline (it, default_face_p)
14870 struct it *it;
14871 int default_face_p;
14872 {
14873 if (FRAME_WINDOW_P (it->f))
14874 {
14875 int n = it->glyph_row->used[TEXT_AREA];
14876
14877 if (it->glyph_row->glyphs[TEXT_AREA] + n
14878 < it->glyph_row->glyphs[1 + TEXT_AREA])
14879 {
14880 /* Save some values that must not be changed.
14881 Must save IT->c and IT->len because otherwise
14882 ITERATOR_AT_END_P wouldn't work anymore after
14883 append_space_for_newline has been called. */
14884 enum display_element_type saved_what = it->what;
14885 int saved_c = it->c, saved_len = it->len;
14886 int saved_x = it->current_x;
14887 int saved_face_id = it->face_id;
14888 struct text_pos saved_pos;
14889 Lisp_Object saved_object;
14890 struct face *face;
14891
14892 saved_object = it->object;
14893 saved_pos = it->position;
14894
14895 it->what = IT_CHARACTER;
14896 bzero (&it->position, sizeof it->position);
14897 it->object = make_number (0);
14898 it->c = ' ';
14899 it->len = 1;
14900
14901 if (default_face_p)
14902 it->face_id = DEFAULT_FACE_ID;
14903 else if (it->face_before_selective_p)
14904 it->face_id = it->saved_face_id;
14905 face = FACE_FROM_ID (it->f, it->face_id);
14906 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14907
14908 PRODUCE_GLYPHS (it);
14909
14910 it->override_ascent = -1;
14911 it->constrain_row_ascent_descent_p = 0;
14912 it->current_x = saved_x;
14913 it->object = saved_object;
14914 it->position = saved_pos;
14915 it->what = saved_what;
14916 it->face_id = saved_face_id;
14917 it->len = saved_len;
14918 it->c = saved_c;
14919 return 1;
14920 }
14921 }
14922
14923 return 0;
14924 }
14925
14926
14927 /* Extend the face of the last glyph in the text area of IT->glyph_row
14928 to the end of the display line. Called from display_line.
14929 If the glyph row is empty, add a space glyph to it so that we
14930 know the face to draw. Set the glyph row flag fill_line_p. */
14931
14932 static void
14933 extend_face_to_end_of_line (it)
14934 struct it *it;
14935 {
14936 struct face *face;
14937 struct frame *f = it->f;
14938
14939 /* If line is already filled, do nothing. */
14940 if (it->current_x >= it->last_visible_x)
14941 return;
14942
14943 /* Face extension extends the background and box of IT->face_id
14944 to the end of the line. If the background equals the background
14945 of the frame, we don't have to do anything. */
14946 if (it->face_before_selective_p)
14947 face = FACE_FROM_ID (it->f, it->saved_face_id);
14948 else
14949 face = FACE_FROM_ID (f, it->face_id);
14950
14951 if (FRAME_WINDOW_P (f)
14952 && face->box == FACE_NO_BOX
14953 && face->background == FRAME_BACKGROUND_PIXEL (f)
14954 && !face->stipple)
14955 return;
14956
14957 /* Set the glyph row flag indicating that the face of the last glyph
14958 in the text area has to be drawn to the end of the text area. */
14959 it->glyph_row->fill_line_p = 1;
14960
14961 /* If current character of IT is not ASCII, make sure we have the
14962 ASCII face. This will be automatically undone the next time
14963 get_next_display_element returns a multibyte character. Note
14964 that the character will always be single byte in unibyte text. */
14965 if (!SINGLE_BYTE_CHAR_P (it->c))
14966 {
14967 it->face_id = FACE_FOR_CHAR (f, face, 0);
14968 }
14969
14970 if (FRAME_WINDOW_P (f))
14971 {
14972 /* If the row is empty, add a space with the current face of IT,
14973 so that we know which face to draw. */
14974 if (it->glyph_row->used[TEXT_AREA] == 0)
14975 {
14976 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14977 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14978 it->glyph_row->used[TEXT_AREA] = 1;
14979 }
14980 }
14981 else
14982 {
14983 /* Save some values that must not be changed. */
14984 int saved_x = it->current_x;
14985 struct text_pos saved_pos;
14986 Lisp_Object saved_object;
14987 enum display_element_type saved_what = it->what;
14988 int saved_face_id = it->face_id;
14989
14990 saved_object = it->object;
14991 saved_pos = it->position;
14992
14993 it->what = IT_CHARACTER;
14994 bzero (&it->position, sizeof it->position);
14995 it->object = make_number (0);
14996 it->c = ' ';
14997 it->len = 1;
14998 it->face_id = face->id;
14999
15000 PRODUCE_GLYPHS (it);
15001
15002 while (it->current_x <= it->last_visible_x)
15003 PRODUCE_GLYPHS (it);
15004
15005 /* Don't count these blanks really. It would let us insert a left
15006 truncation glyph below and make us set the cursor on them, maybe. */
15007 it->current_x = saved_x;
15008 it->object = saved_object;
15009 it->position = saved_pos;
15010 it->what = saved_what;
15011 it->face_id = saved_face_id;
15012 }
15013 }
15014
15015
15016 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15017 trailing whitespace. */
15018
15019 static int
15020 trailing_whitespace_p (charpos)
15021 int charpos;
15022 {
15023 int bytepos = CHAR_TO_BYTE (charpos);
15024 int c = 0;
15025
15026 while (bytepos < ZV_BYTE
15027 && (c = FETCH_CHAR (bytepos),
15028 c == ' ' || c == '\t'))
15029 ++bytepos;
15030
15031 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15032 {
15033 if (bytepos != PT_BYTE)
15034 return 1;
15035 }
15036 return 0;
15037 }
15038
15039
15040 /* Highlight trailing whitespace, if any, in ROW. */
15041
15042 void
15043 highlight_trailing_whitespace (f, row)
15044 struct frame *f;
15045 struct glyph_row *row;
15046 {
15047 int used = row->used[TEXT_AREA];
15048
15049 if (used)
15050 {
15051 struct glyph *start = row->glyphs[TEXT_AREA];
15052 struct glyph *glyph = start + used - 1;
15053
15054 /* Skip over glyphs inserted to display the cursor at the
15055 end of a line, for extending the face of the last glyph
15056 to the end of the line on terminals, and for truncation
15057 and continuation glyphs. */
15058 while (glyph >= start
15059 && glyph->type == CHAR_GLYPH
15060 && INTEGERP (glyph->object))
15061 --glyph;
15062
15063 /* If last glyph is a space or stretch, and it's trailing
15064 whitespace, set the face of all trailing whitespace glyphs in
15065 IT->glyph_row to `trailing-whitespace'. */
15066 if (glyph >= start
15067 && BUFFERP (glyph->object)
15068 && (glyph->type == STRETCH_GLYPH
15069 || (glyph->type == CHAR_GLYPH
15070 && glyph->u.ch == ' '))
15071 && trailing_whitespace_p (glyph->charpos))
15072 {
15073 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15074 if (face_id < 0)
15075 return;
15076
15077 while (glyph >= start
15078 && BUFFERP (glyph->object)
15079 && (glyph->type == STRETCH_GLYPH
15080 || (glyph->type == CHAR_GLYPH
15081 && glyph->u.ch == ' ')))
15082 (glyph--)->face_id = face_id;
15083 }
15084 }
15085 }
15086
15087
15088 /* Value is non-zero if glyph row ROW in window W should be
15089 used to hold the cursor. */
15090
15091 static int
15092 cursor_row_p (w, row)
15093 struct window *w;
15094 struct glyph_row *row;
15095 {
15096 int cursor_row_p = 1;
15097
15098 if (PT == MATRIX_ROW_END_CHARPOS (row))
15099 {
15100 /* If the row ends with a newline from a string, we don't want
15101 the cursor there, but we still want it at the start of the
15102 string if the string starts in this row.
15103 If the row is continued it doesn't end in a newline. */
15104 if (CHARPOS (row->end.string_pos) >= 0)
15105 cursor_row_p = (row->continued_p
15106 || PT >= MATRIX_ROW_START_CHARPOS (row));
15107 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15108 {
15109 /* If the row ends in middle of a real character,
15110 and the line is continued, we want the cursor here.
15111 That's because MATRIX_ROW_END_CHARPOS would equal
15112 PT if PT is before the character. */
15113 if (!row->ends_in_ellipsis_p)
15114 cursor_row_p = row->continued_p;
15115 else
15116 /* If the row ends in an ellipsis, then
15117 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15118 We want that position to be displayed after the ellipsis. */
15119 cursor_row_p = 0;
15120 }
15121 /* If the row ends at ZV, display the cursor at the end of that
15122 row instead of at the start of the row below. */
15123 else if (row->ends_at_zv_p)
15124 cursor_row_p = 1;
15125 else
15126 cursor_row_p = 0;
15127 }
15128
15129 return cursor_row_p;
15130 }
15131
15132
15133 /* Construct the glyph row IT->glyph_row in the desired matrix of
15134 IT->w from text at the current position of IT. See dispextern.h
15135 for an overview of struct it. Value is non-zero if
15136 IT->glyph_row displays text, as opposed to a line displaying ZV
15137 only. */
15138
15139 static int
15140 display_line (it)
15141 struct it *it;
15142 {
15143 struct glyph_row *row = it->glyph_row;
15144 Lisp_Object overlay_arrow_string;
15145
15146 /* We always start displaying at hpos zero even if hscrolled. */
15147 xassert (it->hpos == 0 && it->current_x == 0);
15148
15149 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15150 >= it->w->desired_matrix->nrows)
15151 {
15152 it->w->nrows_scale_factor++;
15153 fonts_changed_p = 1;
15154 return 0;
15155 }
15156
15157 /* Is IT->w showing the region? */
15158 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15159
15160 /* Clear the result glyph row and enable it. */
15161 prepare_desired_row (row);
15162
15163 row->y = it->current_y;
15164 row->start = it->start;
15165 row->continuation_lines_width = it->continuation_lines_width;
15166 row->displays_text_p = 1;
15167 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15168 it->starts_in_middle_of_char_p = 0;
15169
15170 /* Arrange the overlays nicely for our purposes. Usually, we call
15171 display_line on only one line at a time, in which case this
15172 can't really hurt too much, or we call it on lines which appear
15173 one after another in the buffer, in which case all calls to
15174 recenter_overlay_lists but the first will be pretty cheap. */
15175 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15176
15177 /* Move over display elements that are not visible because we are
15178 hscrolled. This may stop at an x-position < IT->first_visible_x
15179 if the first glyph is partially visible or if we hit a line end. */
15180 if (it->current_x < it->first_visible_x)
15181 {
15182 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15183 MOVE_TO_POS | MOVE_TO_X);
15184 }
15185
15186 /* Get the initial row height. This is either the height of the
15187 text hscrolled, if there is any, or zero. */
15188 row->ascent = it->max_ascent;
15189 row->height = it->max_ascent + it->max_descent;
15190 row->phys_ascent = it->max_phys_ascent;
15191 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15192 row->extra_line_spacing = it->max_extra_line_spacing;
15193
15194 /* Loop generating characters. The loop is left with IT on the next
15195 character to display. */
15196 while (1)
15197 {
15198 int n_glyphs_before, hpos_before, x_before;
15199 int x, i, nglyphs;
15200 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15201
15202 /* Retrieve the next thing to display. Value is zero if end of
15203 buffer reached. */
15204 if (!get_next_display_element (it))
15205 {
15206 /* Maybe add a space at the end of this line that is used to
15207 display the cursor there under X. Set the charpos of the
15208 first glyph of blank lines not corresponding to any text
15209 to -1. */
15210 #ifdef HAVE_WINDOW_SYSTEM
15211 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15212 row->exact_window_width_line_p = 1;
15213 else
15214 #endif /* HAVE_WINDOW_SYSTEM */
15215 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15216 || row->used[TEXT_AREA] == 0)
15217 {
15218 row->glyphs[TEXT_AREA]->charpos = -1;
15219 row->displays_text_p = 0;
15220
15221 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15222 && (!MINI_WINDOW_P (it->w)
15223 || (minibuf_level && EQ (it->window, minibuf_window))))
15224 row->indicate_empty_line_p = 1;
15225 }
15226
15227 it->continuation_lines_width = 0;
15228 row->ends_at_zv_p = 1;
15229 break;
15230 }
15231
15232 /* Now, get the metrics of what we want to display. This also
15233 generates glyphs in `row' (which is IT->glyph_row). */
15234 n_glyphs_before = row->used[TEXT_AREA];
15235 x = it->current_x;
15236
15237 /* Remember the line height so far in case the next element doesn't
15238 fit on the line. */
15239 if (!it->truncate_lines_p)
15240 {
15241 ascent = it->max_ascent;
15242 descent = it->max_descent;
15243 phys_ascent = it->max_phys_ascent;
15244 phys_descent = it->max_phys_descent;
15245 }
15246
15247 PRODUCE_GLYPHS (it);
15248
15249 /* If this display element was in marginal areas, continue with
15250 the next one. */
15251 if (it->area != TEXT_AREA)
15252 {
15253 row->ascent = max (row->ascent, it->max_ascent);
15254 row->height = max (row->height, it->max_ascent + it->max_descent);
15255 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15256 row->phys_height = max (row->phys_height,
15257 it->max_phys_ascent + it->max_phys_descent);
15258 row->extra_line_spacing = max (row->extra_line_spacing,
15259 it->max_extra_line_spacing);
15260 set_iterator_to_next (it, 1);
15261 continue;
15262 }
15263
15264 /* Does the display element fit on the line? If we truncate
15265 lines, we should draw past the right edge of the window. If
15266 we don't truncate, we want to stop so that we can display the
15267 continuation glyph before the right margin. If lines are
15268 continued, there are two possible strategies for characters
15269 resulting in more than 1 glyph (e.g. tabs): Display as many
15270 glyphs as possible in this line and leave the rest for the
15271 continuation line, or display the whole element in the next
15272 line. Original redisplay did the former, so we do it also. */
15273 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15274 hpos_before = it->hpos;
15275 x_before = x;
15276
15277 if (/* Not a newline. */
15278 nglyphs > 0
15279 /* Glyphs produced fit entirely in the line. */
15280 && it->current_x < it->last_visible_x)
15281 {
15282 it->hpos += nglyphs;
15283 row->ascent = max (row->ascent, it->max_ascent);
15284 row->height = max (row->height, it->max_ascent + it->max_descent);
15285 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15286 row->phys_height = max (row->phys_height,
15287 it->max_phys_ascent + it->max_phys_descent);
15288 row->extra_line_spacing = max (row->extra_line_spacing,
15289 it->max_extra_line_spacing);
15290 if (it->current_x - it->pixel_width < it->first_visible_x)
15291 row->x = x - it->first_visible_x;
15292 }
15293 else
15294 {
15295 int new_x;
15296 struct glyph *glyph;
15297
15298 for (i = 0; i < nglyphs; ++i, x = new_x)
15299 {
15300 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15301 new_x = x + glyph->pixel_width;
15302
15303 if (/* Lines are continued. */
15304 !it->truncate_lines_p
15305 && (/* Glyph doesn't fit on the line. */
15306 new_x > it->last_visible_x
15307 /* Or it fits exactly on a window system frame. */
15308 || (new_x == it->last_visible_x
15309 && FRAME_WINDOW_P (it->f))))
15310 {
15311 /* End of a continued line. */
15312
15313 if (it->hpos == 0
15314 || (new_x == it->last_visible_x
15315 && FRAME_WINDOW_P (it->f)))
15316 {
15317 /* Current glyph is the only one on the line or
15318 fits exactly on the line. We must continue
15319 the line because we can't draw the cursor
15320 after the glyph. */
15321 row->continued_p = 1;
15322 it->current_x = new_x;
15323 it->continuation_lines_width += new_x;
15324 ++it->hpos;
15325 if (i == nglyphs - 1)
15326 {
15327 set_iterator_to_next (it, 1);
15328 #ifdef HAVE_WINDOW_SYSTEM
15329 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15330 {
15331 if (!get_next_display_element (it))
15332 {
15333 row->exact_window_width_line_p = 1;
15334 it->continuation_lines_width = 0;
15335 row->continued_p = 0;
15336 row->ends_at_zv_p = 1;
15337 }
15338 else if (ITERATOR_AT_END_OF_LINE_P (it))
15339 {
15340 row->continued_p = 0;
15341 row->exact_window_width_line_p = 1;
15342 }
15343 }
15344 #endif /* HAVE_WINDOW_SYSTEM */
15345 }
15346 }
15347 else if (CHAR_GLYPH_PADDING_P (*glyph)
15348 && !FRAME_WINDOW_P (it->f))
15349 {
15350 /* A padding glyph that doesn't fit on this line.
15351 This means the whole character doesn't fit
15352 on the line. */
15353 row->used[TEXT_AREA] = n_glyphs_before;
15354
15355 /* Fill the rest of the row with continuation
15356 glyphs like in 20.x. */
15357 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15358 < row->glyphs[1 + TEXT_AREA])
15359 produce_special_glyphs (it, IT_CONTINUATION);
15360
15361 row->continued_p = 1;
15362 it->current_x = x_before;
15363 it->continuation_lines_width += x_before;
15364
15365 /* Restore the height to what it was before the
15366 element not fitting on the line. */
15367 it->max_ascent = ascent;
15368 it->max_descent = descent;
15369 it->max_phys_ascent = phys_ascent;
15370 it->max_phys_descent = phys_descent;
15371 }
15372 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15373 {
15374 /* A TAB that extends past the right edge of the
15375 window. This produces a single glyph on
15376 window system frames. We leave the glyph in
15377 this row and let it fill the row, but don't
15378 consume the TAB. */
15379 it->continuation_lines_width += it->last_visible_x;
15380 row->ends_in_middle_of_char_p = 1;
15381 row->continued_p = 1;
15382 glyph->pixel_width = it->last_visible_x - x;
15383 it->starts_in_middle_of_char_p = 1;
15384 }
15385 else
15386 {
15387 /* Something other than a TAB that draws past
15388 the right edge of the window. Restore
15389 positions to values before the element. */
15390 row->used[TEXT_AREA] = n_glyphs_before + i;
15391
15392 /* Display continuation glyphs. */
15393 if (!FRAME_WINDOW_P (it->f))
15394 produce_special_glyphs (it, IT_CONTINUATION);
15395 row->continued_p = 1;
15396
15397 it->continuation_lines_width += x;
15398
15399 if (nglyphs > 1 && i > 0)
15400 {
15401 row->ends_in_middle_of_char_p = 1;
15402 it->starts_in_middle_of_char_p = 1;
15403 }
15404
15405 /* Restore the height to what it was before the
15406 element not fitting on the line. */
15407 it->max_ascent = ascent;
15408 it->max_descent = descent;
15409 it->max_phys_ascent = phys_ascent;
15410 it->max_phys_descent = phys_descent;
15411 }
15412
15413 break;
15414 }
15415 else if (new_x > it->first_visible_x)
15416 {
15417 /* Increment number of glyphs actually displayed. */
15418 ++it->hpos;
15419
15420 if (x < it->first_visible_x)
15421 /* Glyph is partially visible, i.e. row starts at
15422 negative X position. */
15423 row->x = x - it->first_visible_x;
15424 }
15425 else
15426 {
15427 /* Glyph is completely off the left margin of the
15428 window. This should not happen because of the
15429 move_it_in_display_line at the start of this
15430 function, unless the text display area of the
15431 window is empty. */
15432 xassert (it->first_visible_x <= it->last_visible_x);
15433 }
15434 }
15435
15436 row->ascent = max (row->ascent, it->max_ascent);
15437 row->height = max (row->height, it->max_ascent + it->max_descent);
15438 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15439 row->phys_height = max (row->phys_height,
15440 it->max_phys_ascent + it->max_phys_descent);
15441 row->extra_line_spacing = max (row->extra_line_spacing,
15442 it->max_extra_line_spacing);
15443
15444 /* End of this display line if row is continued. */
15445 if (row->continued_p || row->ends_at_zv_p)
15446 break;
15447 }
15448
15449 at_end_of_line:
15450 /* Is this a line end? If yes, we're also done, after making
15451 sure that a non-default face is extended up to the right
15452 margin of the window. */
15453 if (ITERATOR_AT_END_OF_LINE_P (it))
15454 {
15455 int used_before = row->used[TEXT_AREA];
15456
15457 row->ends_in_newline_from_string_p = STRINGP (it->object);
15458
15459 #ifdef HAVE_WINDOW_SYSTEM
15460 /* Add a space at the end of the line that is used to
15461 display the cursor there. */
15462 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15463 append_space_for_newline (it, 0);
15464 #endif /* HAVE_WINDOW_SYSTEM */
15465
15466 /* Extend the face to the end of the line. */
15467 extend_face_to_end_of_line (it);
15468
15469 /* Make sure we have the position. */
15470 if (used_before == 0)
15471 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15472
15473 /* Consume the line end. This skips over invisible lines. */
15474 set_iterator_to_next (it, 1);
15475 it->continuation_lines_width = 0;
15476 break;
15477 }
15478
15479 /* Proceed with next display element. Note that this skips
15480 over lines invisible because of selective display. */
15481 set_iterator_to_next (it, 1);
15482
15483 /* If we truncate lines, we are done when the last displayed
15484 glyphs reach past the right margin of the window. */
15485 if (it->truncate_lines_p
15486 && (FRAME_WINDOW_P (it->f)
15487 ? (it->current_x >= it->last_visible_x)
15488 : (it->current_x > it->last_visible_x)))
15489 {
15490 /* Maybe add truncation glyphs. */
15491 if (!FRAME_WINDOW_P (it->f))
15492 {
15493 int i, n;
15494
15495 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15496 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15497 break;
15498
15499 for (n = row->used[TEXT_AREA]; i < n; ++i)
15500 {
15501 row->used[TEXT_AREA] = i;
15502 produce_special_glyphs (it, IT_TRUNCATION);
15503 }
15504 }
15505 #ifdef HAVE_WINDOW_SYSTEM
15506 else
15507 {
15508 /* Don't truncate if we can overflow newline into fringe. */
15509 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15510 {
15511 if (!get_next_display_element (it))
15512 {
15513 it->continuation_lines_width = 0;
15514 row->ends_at_zv_p = 1;
15515 row->exact_window_width_line_p = 1;
15516 break;
15517 }
15518 if (ITERATOR_AT_END_OF_LINE_P (it))
15519 {
15520 row->exact_window_width_line_p = 1;
15521 goto at_end_of_line;
15522 }
15523 }
15524 }
15525 #endif /* HAVE_WINDOW_SYSTEM */
15526
15527 row->truncated_on_right_p = 1;
15528 it->continuation_lines_width = 0;
15529 reseat_at_next_visible_line_start (it, 0);
15530 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15531 it->hpos = hpos_before;
15532 it->current_x = x_before;
15533 break;
15534 }
15535 }
15536
15537 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15538 at the left window margin. */
15539 if (it->first_visible_x
15540 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15541 {
15542 if (!FRAME_WINDOW_P (it->f))
15543 insert_left_trunc_glyphs (it);
15544 row->truncated_on_left_p = 1;
15545 }
15546
15547 /* If the start of this line is the overlay arrow-position, then
15548 mark this glyph row as the one containing the overlay arrow.
15549 This is clearly a mess with variable size fonts. It would be
15550 better to let it be displayed like cursors under X. */
15551 if ((row->displays_text_p || !overlay_arrow_seen)
15552 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15553 !NILP (overlay_arrow_string)))
15554 {
15555 /* Overlay arrow in window redisplay is a fringe bitmap. */
15556 if (STRINGP (overlay_arrow_string))
15557 {
15558 struct glyph_row *arrow_row
15559 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15560 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15561 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15562 struct glyph *p = row->glyphs[TEXT_AREA];
15563 struct glyph *p2, *end;
15564
15565 /* Copy the arrow glyphs. */
15566 while (glyph < arrow_end)
15567 *p++ = *glyph++;
15568
15569 /* Throw away padding glyphs. */
15570 p2 = p;
15571 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15572 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15573 ++p2;
15574 if (p2 > p)
15575 {
15576 while (p2 < end)
15577 *p++ = *p2++;
15578 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15579 }
15580 }
15581 else
15582 {
15583 xassert (INTEGERP (overlay_arrow_string));
15584 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15585 }
15586 overlay_arrow_seen = 1;
15587 }
15588
15589 /* Compute pixel dimensions of this line. */
15590 compute_line_metrics (it);
15591
15592 /* Remember the position at which this line ends. */
15593 row->end = it->current;
15594
15595 /* Record whether this row ends inside an ellipsis. */
15596 row->ends_in_ellipsis_p
15597 = (it->method == GET_FROM_DISPLAY_VECTOR
15598 && it->ellipsis_p);
15599
15600 /* Save fringe bitmaps in this row. */
15601 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15602 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15603 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15604 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15605
15606 it->left_user_fringe_bitmap = 0;
15607 it->left_user_fringe_face_id = 0;
15608 it->right_user_fringe_bitmap = 0;
15609 it->right_user_fringe_face_id = 0;
15610
15611 /* Maybe set the cursor. */
15612 if (it->w->cursor.vpos < 0
15613 && PT >= MATRIX_ROW_START_CHARPOS (row)
15614 && PT <= MATRIX_ROW_END_CHARPOS (row)
15615 && cursor_row_p (it->w, row))
15616 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15617
15618 /* Highlight trailing whitespace. */
15619 if (!NILP (Vshow_trailing_whitespace))
15620 highlight_trailing_whitespace (it->f, it->glyph_row);
15621
15622 /* Prepare for the next line. This line starts horizontally at (X
15623 HPOS) = (0 0). Vertical positions are incremented. As a
15624 convenience for the caller, IT->glyph_row is set to the next
15625 row to be used. */
15626 it->current_x = it->hpos = 0;
15627 it->current_y += row->height;
15628 ++it->vpos;
15629 ++it->glyph_row;
15630 it->start = it->current;
15631 return row->displays_text_p;
15632 }
15633
15634
15635 \f
15636 /***********************************************************************
15637 Menu Bar
15638 ***********************************************************************/
15639
15640 /* Redisplay the menu bar in the frame for window W.
15641
15642 The menu bar of X frames that don't have X toolkit support is
15643 displayed in a special window W->frame->menu_bar_window.
15644
15645 The menu bar of terminal frames is treated specially as far as
15646 glyph matrices are concerned. Menu bar lines are not part of
15647 windows, so the update is done directly on the frame matrix rows
15648 for the menu bar. */
15649
15650 static void
15651 display_menu_bar (w)
15652 struct window *w;
15653 {
15654 struct frame *f = XFRAME (WINDOW_FRAME (w));
15655 struct it it;
15656 Lisp_Object items;
15657 int i;
15658
15659 /* Don't do all this for graphical frames. */
15660 #ifdef HAVE_NTGUI
15661 if (!NILP (Vwindow_system))
15662 return;
15663 #endif
15664 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15665 if (FRAME_X_P (f))
15666 return;
15667 #endif
15668 #ifdef MAC_OS
15669 if (FRAME_MAC_P (f))
15670 return;
15671 #endif
15672
15673 #ifdef USE_X_TOOLKIT
15674 xassert (!FRAME_WINDOW_P (f));
15675 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15676 it.first_visible_x = 0;
15677 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15678 #else /* not USE_X_TOOLKIT */
15679 if (FRAME_WINDOW_P (f))
15680 {
15681 /* Menu bar lines are displayed in the desired matrix of the
15682 dummy window menu_bar_window. */
15683 struct window *menu_w;
15684 xassert (WINDOWP (f->menu_bar_window));
15685 menu_w = XWINDOW (f->menu_bar_window);
15686 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15687 MENU_FACE_ID);
15688 it.first_visible_x = 0;
15689 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15690 }
15691 else
15692 {
15693 /* This is a TTY frame, i.e. character hpos/vpos are used as
15694 pixel x/y. */
15695 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15696 MENU_FACE_ID);
15697 it.first_visible_x = 0;
15698 it.last_visible_x = FRAME_COLS (f);
15699 }
15700 #endif /* not USE_X_TOOLKIT */
15701
15702 if (! mode_line_inverse_video)
15703 /* Force the menu-bar to be displayed in the default face. */
15704 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15705
15706 /* Clear all rows of the menu bar. */
15707 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15708 {
15709 struct glyph_row *row = it.glyph_row + i;
15710 clear_glyph_row (row);
15711 row->enabled_p = 1;
15712 row->full_width_p = 1;
15713 }
15714
15715 /* Display all items of the menu bar. */
15716 items = FRAME_MENU_BAR_ITEMS (it.f);
15717 for (i = 0; i < XVECTOR (items)->size; i += 4)
15718 {
15719 Lisp_Object string;
15720
15721 /* Stop at nil string. */
15722 string = AREF (items, i + 1);
15723 if (NILP (string))
15724 break;
15725
15726 /* Remember where item was displayed. */
15727 AREF (items, i + 3) = make_number (it.hpos);
15728
15729 /* Display the item, pad with one space. */
15730 if (it.current_x < it.last_visible_x)
15731 display_string (NULL, string, Qnil, 0, 0, &it,
15732 SCHARS (string) + 1, 0, 0, -1);
15733 }
15734
15735 /* Fill out the line with spaces. */
15736 if (it.current_x < it.last_visible_x)
15737 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15738
15739 /* Compute the total height of the lines. */
15740 compute_line_metrics (&it);
15741 }
15742
15743
15744 \f
15745 /***********************************************************************
15746 Mode Line
15747 ***********************************************************************/
15748
15749 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15750 FORCE is non-zero, redisplay mode lines unconditionally.
15751 Otherwise, redisplay only mode lines that are garbaged. Value is
15752 the number of windows whose mode lines were redisplayed. */
15753
15754 static int
15755 redisplay_mode_lines (window, force)
15756 Lisp_Object window;
15757 int force;
15758 {
15759 int nwindows = 0;
15760
15761 while (!NILP (window))
15762 {
15763 struct window *w = XWINDOW (window);
15764
15765 if (WINDOWP (w->hchild))
15766 nwindows += redisplay_mode_lines (w->hchild, force);
15767 else if (WINDOWP (w->vchild))
15768 nwindows += redisplay_mode_lines (w->vchild, force);
15769 else if (force
15770 || FRAME_GARBAGED_P (XFRAME (w->frame))
15771 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15772 {
15773 struct text_pos lpoint;
15774 struct buffer *old = current_buffer;
15775
15776 /* Set the window's buffer for the mode line display. */
15777 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15778 set_buffer_internal_1 (XBUFFER (w->buffer));
15779
15780 /* Point refers normally to the selected window. For any
15781 other window, set up appropriate value. */
15782 if (!EQ (window, selected_window))
15783 {
15784 struct text_pos pt;
15785
15786 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15787 if (CHARPOS (pt) < BEGV)
15788 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15789 else if (CHARPOS (pt) > (ZV - 1))
15790 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15791 else
15792 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15793 }
15794
15795 /* Display mode lines. */
15796 clear_glyph_matrix (w->desired_matrix);
15797 if (display_mode_lines (w))
15798 {
15799 ++nwindows;
15800 w->must_be_updated_p = 1;
15801 }
15802
15803 /* Restore old settings. */
15804 set_buffer_internal_1 (old);
15805 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15806 }
15807
15808 window = w->next;
15809 }
15810
15811 return nwindows;
15812 }
15813
15814
15815 /* Display the mode and/or top line of window W. Value is the number
15816 of mode lines displayed. */
15817
15818 static int
15819 display_mode_lines (w)
15820 struct window *w;
15821 {
15822 Lisp_Object old_selected_window, old_selected_frame;
15823 int n = 0;
15824
15825 old_selected_frame = selected_frame;
15826 selected_frame = w->frame;
15827 old_selected_window = selected_window;
15828 XSETWINDOW (selected_window, w);
15829
15830 /* These will be set while the mode line specs are processed. */
15831 line_number_displayed = 0;
15832 w->column_number_displayed = Qnil;
15833
15834 if (WINDOW_WANTS_MODELINE_P (w))
15835 {
15836 struct window *sel_w = XWINDOW (old_selected_window);
15837
15838 /* Select mode line face based on the real selected window. */
15839 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15840 current_buffer->mode_line_format);
15841 ++n;
15842 }
15843
15844 if (WINDOW_WANTS_HEADER_LINE_P (w))
15845 {
15846 display_mode_line (w, HEADER_LINE_FACE_ID,
15847 current_buffer->header_line_format);
15848 ++n;
15849 }
15850
15851 selected_frame = old_selected_frame;
15852 selected_window = old_selected_window;
15853 return n;
15854 }
15855
15856
15857 /* Display mode or top line of window W. FACE_ID specifies which line
15858 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15859 FORMAT is the mode line format to display. Value is the pixel
15860 height of the mode line displayed. */
15861
15862 static int
15863 display_mode_line (w, face_id, format)
15864 struct window *w;
15865 enum face_id face_id;
15866 Lisp_Object format;
15867 {
15868 struct it it;
15869 struct face *face;
15870 int count = SPECPDL_INDEX ();
15871
15872 init_iterator (&it, w, -1, -1, NULL, face_id);
15873 prepare_desired_row (it.glyph_row);
15874
15875 it.glyph_row->mode_line_p = 1;
15876
15877 if (! mode_line_inverse_video)
15878 /* Force the mode-line to be displayed in the default face. */
15879 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15880
15881 record_unwind_protect (unwind_format_mode_line,
15882 format_mode_line_unwind_data (NULL));
15883
15884 mode_line_target = MODE_LINE_DISPLAY;
15885
15886 /* Temporarily make frame's keyboard the current kboard so that
15887 kboard-local variables in the mode_line_format will get the right
15888 values. */
15889 push_frame_kboard (it.f);
15890 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15891 pop_frame_kboard ();
15892
15893 unbind_to (count, Qnil);
15894
15895 /* Fill up with spaces. */
15896 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15897
15898 compute_line_metrics (&it);
15899 it.glyph_row->full_width_p = 1;
15900 it.glyph_row->continued_p = 0;
15901 it.glyph_row->truncated_on_left_p = 0;
15902 it.glyph_row->truncated_on_right_p = 0;
15903
15904 /* Make a 3D mode-line have a shadow at its right end. */
15905 face = FACE_FROM_ID (it.f, face_id);
15906 extend_face_to_end_of_line (&it);
15907 if (face->box != FACE_NO_BOX)
15908 {
15909 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15910 + it.glyph_row->used[TEXT_AREA] - 1);
15911 last->right_box_line_p = 1;
15912 }
15913
15914 return it.glyph_row->height;
15915 }
15916
15917 /* Contribute ELT to the mode line for window IT->w. How it
15918 translates into text depends on its data type.
15919
15920 IT describes the display environment in which we display, as usual.
15921
15922 DEPTH is the depth in recursion. It is used to prevent
15923 infinite recursion here.
15924
15925 FIELD_WIDTH is the number of characters the display of ELT should
15926 occupy in the mode line, and PRECISION is the maximum number of
15927 characters to display from ELT's representation. See
15928 display_string for details.
15929
15930 Returns the hpos of the end of the text generated by ELT.
15931
15932 PROPS is a property list to add to any string we encounter.
15933
15934 If RISKY is nonzero, remove (disregard) any properties in any string
15935 we encounter, and ignore :eval and :propertize.
15936
15937 The global variable `mode_line_target' determines whether the
15938 output is passed to `store_mode_line_noprop',
15939 `store_mode_line_string', or `display_string'. */
15940
15941 static int
15942 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15943 struct it *it;
15944 int depth;
15945 int field_width, precision;
15946 Lisp_Object elt, props;
15947 int risky;
15948 {
15949 int n = 0, field, prec;
15950 int literal = 0;
15951
15952 tail_recurse:
15953 if (depth > 100)
15954 elt = build_string ("*too-deep*");
15955
15956 depth++;
15957
15958 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15959 {
15960 case Lisp_String:
15961 {
15962 /* A string: output it and check for %-constructs within it. */
15963 unsigned char c;
15964 int offset = 0;
15965
15966 if (!NILP (props) || risky)
15967 {
15968 Lisp_Object oprops, aelt;
15969 oprops = Ftext_properties_at (make_number (0), elt);
15970
15971 /* If the starting string's properties are not what
15972 we want, translate the string. Also, if the string
15973 is risky, do that anyway. */
15974
15975 if (NILP (Fequal (props, oprops)) || risky)
15976 {
15977 /* If the starting string has properties,
15978 merge the specified ones onto the existing ones. */
15979 if (! NILP (oprops) && !risky)
15980 {
15981 Lisp_Object tem;
15982
15983 oprops = Fcopy_sequence (oprops);
15984 tem = props;
15985 while (CONSP (tem))
15986 {
15987 oprops = Fplist_put (oprops, XCAR (tem),
15988 XCAR (XCDR (tem)));
15989 tem = XCDR (XCDR (tem));
15990 }
15991 props = oprops;
15992 }
15993
15994 aelt = Fassoc (elt, mode_line_proptrans_alist);
15995 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15996 {
15997 mode_line_proptrans_alist
15998 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15999 elt = XCAR (aelt);
16000 }
16001 else
16002 {
16003 Lisp_Object tem;
16004
16005 elt = Fcopy_sequence (elt);
16006 Fset_text_properties (make_number (0), Flength (elt),
16007 props, elt);
16008 /* Add this item to mode_line_proptrans_alist. */
16009 mode_line_proptrans_alist
16010 = Fcons (Fcons (elt, props),
16011 mode_line_proptrans_alist);
16012 /* Truncate mode_line_proptrans_alist
16013 to at most 50 elements. */
16014 tem = Fnthcdr (make_number (50),
16015 mode_line_proptrans_alist);
16016 if (! NILP (tem))
16017 XSETCDR (tem, Qnil);
16018 }
16019 }
16020 }
16021
16022 offset = 0;
16023
16024 if (literal)
16025 {
16026 prec = precision - n;
16027 switch (mode_line_target)
16028 {
16029 case MODE_LINE_NOPROP:
16030 case MODE_LINE_TITLE:
16031 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16032 break;
16033 case MODE_LINE_STRING:
16034 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16035 break;
16036 case MODE_LINE_DISPLAY:
16037 n += display_string (NULL, elt, Qnil, 0, 0, it,
16038 0, prec, 0, STRING_MULTIBYTE (elt));
16039 break;
16040 }
16041
16042 break;
16043 }
16044
16045 /* Handle the non-literal case. */
16046
16047 while ((precision <= 0 || n < precision)
16048 && SREF (elt, offset) != 0
16049 && (mode_line_target != MODE_LINE_DISPLAY
16050 || it->current_x < it->last_visible_x))
16051 {
16052 int last_offset = offset;
16053
16054 /* Advance to end of string or next format specifier. */
16055 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16056 ;
16057
16058 if (offset - 1 != last_offset)
16059 {
16060 int nchars, nbytes;
16061
16062 /* Output to end of string or up to '%'. Field width
16063 is length of string. Don't output more than
16064 PRECISION allows us. */
16065 offset--;
16066
16067 prec = c_string_width (SDATA (elt) + last_offset,
16068 offset - last_offset, precision - n,
16069 &nchars, &nbytes);
16070
16071 switch (mode_line_target)
16072 {
16073 case MODE_LINE_NOPROP:
16074 case MODE_LINE_TITLE:
16075 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16076 break;
16077 case MODE_LINE_STRING:
16078 {
16079 int bytepos = last_offset;
16080 int charpos = string_byte_to_char (elt, bytepos);
16081 int endpos = (precision <= 0
16082 ? string_byte_to_char (elt, offset)
16083 : charpos + nchars);
16084
16085 n += store_mode_line_string (NULL,
16086 Fsubstring (elt, make_number (charpos),
16087 make_number (endpos)),
16088 0, 0, 0, Qnil);
16089 }
16090 break;
16091 case MODE_LINE_DISPLAY:
16092 {
16093 int bytepos = last_offset;
16094 int charpos = string_byte_to_char (elt, bytepos);
16095 n += display_string (NULL, elt, Qnil, 0, charpos,
16096 it, 0, prec, 0,
16097 STRING_MULTIBYTE (elt));
16098 }
16099 break;
16100 }
16101 }
16102 else /* c == '%' */
16103 {
16104 int percent_position = offset;
16105
16106 /* Get the specified minimum width. Zero means
16107 don't pad. */
16108 field = 0;
16109 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16110 field = field * 10 + c - '0';
16111
16112 /* Don't pad beyond the total padding allowed. */
16113 if (field_width - n > 0 && field > field_width - n)
16114 field = field_width - n;
16115
16116 /* Note that either PRECISION <= 0 or N < PRECISION. */
16117 prec = precision - n;
16118
16119 if (c == 'M')
16120 n += display_mode_element (it, depth, field, prec,
16121 Vglobal_mode_string, props,
16122 risky);
16123 else if (c != 0)
16124 {
16125 int multibyte;
16126 int bytepos, charpos;
16127 unsigned char *spec;
16128
16129 bytepos = percent_position;
16130 charpos = (STRING_MULTIBYTE (elt)
16131 ? string_byte_to_char (elt, bytepos)
16132 : bytepos);
16133
16134 spec
16135 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16136
16137 switch (mode_line_target)
16138 {
16139 case MODE_LINE_NOPROP:
16140 case MODE_LINE_TITLE:
16141 n += store_mode_line_noprop (spec, field, prec);
16142 break;
16143 case MODE_LINE_STRING:
16144 {
16145 int len = strlen (spec);
16146 Lisp_Object tem = make_string (spec, len);
16147 props = Ftext_properties_at (make_number (charpos), elt);
16148 /* Should only keep face property in props */
16149 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16150 }
16151 break;
16152 case MODE_LINE_DISPLAY:
16153 {
16154 int nglyphs_before, nwritten;
16155
16156 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16157 nwritten = display_string (spec, Qnil, elt,
16158 charpos, 0, it,
16159 field, prec, 0,
16160 multibyte);
16161
16162 /* Assign to the glyphs written above the
16163 string where the `%x' came from, position
16164 of the `%'. */
16165 if (nwritten > 0)
16166 {
16167 struct glyph *glyph
16168 = (it->glyph_row->glyphs[TEXT_AREA]
16169 + nglyphs_before);
16170 int i;
16171
16172 for (i = 0; i < nwritten; ++i)
16173 {
16174 glyph[i].object = elt;
16175 glyph[i].charpos = charpos;
16176 }
16177
16178 n += nwritten;
16179 }
16180 }
16181 break;
16182 }
16183 }
16184 else /* c == 0 */
16185 break;
16186 }
16187 }
16188 }
16189 break;
16190
16191 case Lisp_Symbol:
16192 /* A symbol: process the value of the symbol recursively
16193 as if it appeared here directly. Avoid error if symbol void.
16194 Special case: if value of symbol is a string, output the string
16195 literally. */
16196 {
16197 register Lisp_Object tem;
16198
16199 /* If the variable is not marked as risky to set
16200 then its contents are risky to use. */
16201 if (NILP (Fget (elt, Qrisky_local_variable)))
16202 risky = 1;
16203
16204 tem = Fboundp (elt);
16205 if (!NILP (tem))
16206 {
16207 tem = Fsymbol_value (elt);
16208 /* If value is a string, output that string literally:
16209 don't check for % within it. */
16210 if (STRINGP (tem))
16211 literal = 1;
16212
16213 if (!EQ (tem, elt))
16214 {
16215 /* Give up right away for nil or t. */
16216 elt = tem;
16217 goto tail_recurse;
16218 }
16219 }
16220 }
16221 break;
16222
16223 case Lisp_Cons:
16224 {
16225 register Lisp_Object car, tem;
16226
16227 /* A cons cell: five distinct cases.
16228 If first element is :eval or :propertize, do something special.
16229 If first element is a string or a cons, process all the elements
16230 and effectively concatenate them.
16231 If first element is a negative number, truncate displaying cdr to
16232 at most that many characters. If positive, pad (with spaces)
16233 to at least that many characters.
16234 If first element is a symbol, process the cadr or caddr recursively
16235 according to whether the symbol's value is non-nil or nil. */
16236 car = XCAR (elt);
16237 if (EQ (car, QCeval))
16238 {
16239 /* An element of the form (:eval FORM) means evaluate FORM
16240 and use the result as mode line elements. */
16241
16242 if (risky)
16243 break;
16244
16245 if (CONSP (XCDR (elt)))
16246 {
16247 Lisp_Object spec;
16248 spec = safe_eval (XCAR (XCDR (elt)));
16249 n += display_mode_element (it, depth, field_width - n,
16250 precision - n, spec, props,
16251 risky);
16252 }
16253 }
16254 else if (EQ (car, QCpropertize))
16255 {
16256 /* An element of the form (:propertize ELT PROPS...)
16257 means display ELT but applying properties PROPS. */
16258
16259 if (risky)
16260 break;
16261
16262 if (CONSP (XCDR (elt)))
16263 n += display_mode_element (it, depth, field_width - n,
16264 precision - n, XCAR (XCDR (elt)),
16265 XCDR (XCDR (elt)), risky);
16266 }
16267 else if (SYMBOLP (car))
16268 {
16269 tem = Fboundp (car);
16270 elt = XCDR (elt);
16271 if (!CONSP (elt))
16272 goto invalid;
16273 /* elt is now the cdr, and we know it is a cons cell.
16274 Use its car if CAR has a non-nil value. */
16275 if (!NILP (tem))
16276 {
16277 tem = Fsymbol_value (car);
16278 if (!NILP (tem))
16279 {
16280 elt = XCAR (elt);
16281 goto tail_recurse;
16282 }
16283 }
16284 /* Symbol's value is nil (or symbol is unbound)
16285 Get the cddr of the original list
16286 and if possible find the caddr and use that. */
16287 elt = XCDR (elt);
16288 if (NILP (elt))
16289 break;
16290 else if (!CONSP (elt))
16291 goto invalid;
16292 elt = XCAR (elt);
16293 goto tail_recurse;
16294 }
16295 else if (INTEGERP (car))
16296 {
16297 register int lim = XINT (car);
16298 elt = XCDR (elt);
16299 if (lim < 0)
16300 {
16301 /* Negative int means reduce maximum width. */
16302 if (precision <= 0)
16303 precision = -lim;
16304 else
16305 precision = min (precision, -lim);
16306 }
16307 else if (lim > 0)
16308 {
16309 /* Padding specified. Don't let it be more than
16310 current maximum. */
16311 if (precision > 0)
16312 lim = min (precision, lim);
16313
16314 /* If that's more padding than already wanted, queue it.
16315 But don't reduce padding already specified even if
16316 that is beyond the current truncation point. */
16317 field_width = max (lim, field_width);
16318 }
16319 goto tail_recurse;
16320 }
16321 else if (STRINGP (car) || CONSP (car))
16322 {
16323 register int limit = 50;
16324 /* Limit is to protect against circular lists. */
16325 while (CONSP (elt)
16326 && --limit > 0
16327 && (precision <= 0 || n < precision))
16328 {
16329 n += display_mode_element (it, depth,
16330 /* Do padding only after the last
16331 element in the list. */
16332 (! CONSP (XCDR (elt))
16333 ? field_width - n
16334 : 0),
16335 precision - n, XCAR (elt),
16336 props, risky);
16337 elt = XCDR (elt);
16338 }
16339 }
16340 }
16341 break;
16342
16343 default:
16344 invalid:
16345 elt = build_string ("*invalid*");
16346 goto tail_recurse;
16347 }
16348
16349 /* Pad to FIELD_WIDTH. */
16350 if (field_width > 0 && n < field_width)
16351 {
16352 switch (mode_line_target)
16353 {
16354 case MODE_LINE_NOPROP:
16355 case MODE_LINE_TITLE:
16356 n += store_mode_line_noprop ("", field_width - n, 0);
16357 break;
16358 case MODE_LINE_STRING:
16359 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16360 break;
16361 case MODE_LINE_DISPLAY:
16362 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16363 0, 0, 0);
16364 break;
16365 }
16366 }
16367
16368 return n;
16369 }
16370
16371 /* Store a mode-line string element in mode_line_string_list.
16372
16373 If STRING is non-null, display that C string. Otherwise, the Lisp
16374 string LISP_STRING is displayed.
16375
16376 FIELD_WIDTH is the minimum number of output glyphs to produce.
16377 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16378 with spaces. FIELD_WIDTH <= 0 means don't pad.
16379
16380 PRECISION is the maximum number of characters to output from
16381 STRING. PRECISION <= 0 means don't truncate the string.
16382
16383 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16384 properties to the string.
16385
16386 PROPS are the properties to add to the string.
16387 The mode_line_string_face face property is always added to the string.
16388 */
16389
16390 static int
16391 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16392 char *string;
16393 Lisp_Object lisp_string;
16394 int copy_string;
16395 int field_width;
16396 int precision;
16397 Lisp_Object props;
16398 {
16399 int len;
16400 int n = 0;
16401
16402 if (string != NULL)
16403 {
16404 len = strlen (string);
16405 if (precision > 0 && len > precision)
16406 len = precision;
16407 lisp_string = make_string (string, len);
16408 if (NILP (props))
16409 props = mode_line_string_face_prop;
16410 else if (!NILP (mode_line_string_face))
16411 {
16412 Lisp_Object face = Fplist_get (props, Qface);
16413 props = Fcopy_sequence (props);
16414 if (NILP (face))
16415 face = mode_line_string_face;
16416 else
16417 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16418 props = Fplist_put (props, Qface, face);
16419 }
16420 Fadd_text_properties (make_number (0), make_number (len),
16421 props, lisp_string);
16422 }
16423 else
16424 {
16425 len = XFASTINT (Flength (lisp_string));
16426 if (precision > 0 && len > precision)
16427 {
16428 len = precision;
16429 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16430 precision = -1;
16431 }
16432 if (!NILP (mode_line_string_face))
16433 {
16434 Lisp_Object face;
16435 if (NILP (props))
16436 props = Ftext_properties_at (make_number (0), lisp_string);
16437 face = Fplist_get (props, Qface);
16438 if (NILP (face))
16439 face = mode_line_string_face;
16440 else
16441 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16442 props = Fcons (Qface, Fcons (face, Qnil));
16443 if (copy_string)
16444 lisp_string = Fcopy_sequence (lisp_string);
16445 }
16446 if (!NILP (props))
16447 Fadd_text_properties (make_number (0), make_number (len),
16448 props, lisp_string);
16449 }
16450
16451 if (len > 0)
16452 {
16453 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16454 n += len;
16455 }
16456
16457 if (field_width > len)
16458 {
16459 field_width -= len;
16460 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16461 if (!NILP (props))
16462 Fadd_text_properties (make_number (0), make_number (field_width),
16463 props, lisp_string);
16464 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16465 n += field_width;
16466 }
16467
16468 return n;
16469 }
16470
16471
16472 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16473 1, 4, 0,
16474 doc: /* Format a string out of a mode line format specification.
16475 First arg FORMAT specifies the mode line format (see `mode-line-format'
16476 for details) to use.
16477
16478 Optional second arg FACE specifies the face property to put
16479 on all characters for which no face is specified.
16480 t means whatever face the window's mode line currently uses
16481 \(either `mode-line' or `mode-line-inactive', depending).
16482 nil means the default is no face property.
16483 If FACE is an integer, the value string has no text properties.
16484
16485 Optional third and fourth args WINDOW and BUFFER specify the window
16486 and buffer to use as the context for the formatting (defaults
16487 are the selected window and the window's buffer). */)
16488 (format, face, window, buffer)
16489 Lisp_Object format, face, window, buffer;
16490 {
16491 struct it it;
16492 int len;
16493 struct window *w;
16494 struct buffer *old_buffer = NULL;
16495 int face_id = -1;
16496 int no_props = INTEGERP (face);
16497 int count = SPECPDL_INDEX ();
16498 Lisp_Object str;
16499 int string_start = 0;
16500
16501 if (NILP (window))
16502 window = selected_window;
16503 CHECK_WINDOW (window);
16504 w = XWINDOW (window);
16505
16506 if (NILP (buffer))
16507 buffer = w->buffer;
16508 CHECK_BUFFER (buffer);
16509
16510 if (NILP (format))
16511 return build_string ("");
16512
16513 if (no_props)
16514 face = Qnil;
16515
16516 if (!NILP (face))
16517 {
16518 if (EQ (face, Qt))
16519 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16520 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16521 }
16522
16523 if (face_id < 0)
16524 face_id = DEFAULT_FACE_ID;
16525
16526 if (XBUFFER (buffer) != current_buffer)
16527 old_buffer = current_buffer;
16528
16529 record_unwind_protect (unwind_format_mode_line,
16530 format_mode_line_unwind_data (old_buffer));
16531
16532 if (old_buffer)
16533 set_buffer_internal_1 (XBUFFER (buffer));
16534
16535 init_iterator (&it, w, -1, -1, NULL, face_id);
16536
16537 if (no_props)
16538 {
16539 mode_line_target = MODE_LINE_NOPROP;
16540 mode_line_string_face_prop = Qnil;
16541 mode_line_string_list = Qnil;
16542 string_start = MODE_LINE_NOPROP_LEN (0);
16543 }
16544 else
16545 {
16546 mode_line_target = MODE_LINE_STRING;
16547 mode_line_string_list = Qnil;
16548 mode_line_string_face = face;
16549 mode_line_string_face_prop
16550 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16551 }
16552
16553 push_frame_kboard (it.f);
16554 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16555 pop_frame_kboard ();
16556
16557 if (no_props)
16558 {
16559 len = MODE_LINE_NOPROP_LEN (string_start);
16560 str = make_string (mode_line_noprop_buf + string_start, len);
16561 }
16562 else
16563 {
16564 mode_line_string_list = Fnreverse (mode_line_string_list);
16565 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16566 make_string ("", 0));
16567 }
16568
16569 unbind_to (count, Qnil);
16570 return str;
16571 }
16572
16573 /* Write a null-terminated, right justified decimal representation of
16574 the positive integer D to BUF using a minimal field width WIDTH. */
16575
16576 static void
16577 pint2str (buf, width, d)
16578 register char *buf;
16579 register int width;
16580 register int d;
16581 {
16582 register char *p = buf;
16583
16584 if (d <= 0)
16585 *p++ = '0';
16586 else
16587 {
16588 while (d > 0)
16589 {
16590 *p++ = d % 10 + '0';
16591 d /= 10;
16592 }
16593 }
16594
16595 for (width -= (int) (p - buf); width > 0; --width)
16596 *p++ = ' ';
16597 *p-- = '\0';
16598 while (p > buf)
16599 {
16600 d = *buf;
16601 *buf++ = *p;
16602 *p-- = d;
16603 }
16604 }
16605
16606 /* Write a null-terminated, right justified decimal and "human
16607 readable" representation of the nonnegative integer D to BUF using
16608 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16609
16610 static const char power_letter[] =
16611 {
16612 0, /* not used */
16613 'k', /* kilo */
16614 'M', /* mega */
16615 'G', /* giga */
16616 'T', /* tera */
16617 'P', /* peta */
16618 'E', /* exa */
16619 'Z', /* zetta */
16620 'Y' /* yotta */
16621 };
16622
16623 static void
16624 pint2hrstr (buf, width, d)
16625 char *buf;
16626 int width;
16627 int d;
16628 {
16629 /* We aim to represent the nonnegative integer D as
16630 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16631 int quotient = d;
16632 int remainder = 0;
16633 /* -1 means: do not use TENTHS. */
16634 int tenths = -1;
16635 int exponent = 0;
16636
16637 /* Length of QUOTIENT.TENTHS as a string. */
16638 int length;
16639
16640 char * psuffix;
16641 char * p;
16642
16643 if (1000 <= quotient)
16644 {
16645 /* Scale to the appropriate EXPONENT. */
16646 do
16647 {
16648 remainder = quotient % 1000;
16649 quotient /= 1000;
16650 exponent++;
16651 }
16652 while (1000 <= quotient);
16653
16654 /* Round to nearest and decide whether to use TENTHS or not. */
16655 if (quotient <= 9)
16656 {
16657 tenths = remainder / 100;
16658 if (50 <= remainder % 100)
16659 {
16660 if (tenths < 9)
16661 tenths++;
16662 else
16663 {
16664 quotient++;
16665 if (quotient == 10)
16666 tenths = -1;
16667 else
16668 tenths = 0;
16669 }
16670 }
16671 }
16672 else
16673 if (500 <= remainder)
16674 {
16675 if (quotient < 999)
16676 quotient++;
16677 else
16678 {
16679 quotient = 1;
16680 exponent++;
16681 tenths = 0;
16682 }
16683 }
16684 }
16685
16686 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16687 if (tenths == -1 && quotient <= 99)
16688 if (quotient <= 9)
16689 length = 1;
16690 else
16691 length = 2;
16692 else
16693 length = 3;
16694 p = psuffix = buf + max (width, length);
16695
16696 /* Print EXPONENT. */
16697 if (exponent)
16698 *psuffix++ = power_letter[exponent];
16699 *psuffix = '\0';
16700
16701 /* Print TENTHS. */
16702 if (tenths >= 0)
16703 {
16704 *--p = '0' + tenths;
16705 *--p = '.';
16706 }
16707
16708 /* Print QUOTIENT. */
16709 do
16710 {
16711 int digit = quotient % 10;
16712 *--p = '0' + digit;
16713 }
16714 while ((quotient /= 10) != 0);
16715
16716 /* Print leading spaces. */
16717 while (buf < p)
16718 *--p = ' ';
16719 }
16720
16721 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16722 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16723 type of CODING_SYSTEM. Return updated pointer into BUF. */
16724
16725 static unsigned char invalid_eol_type[] = "(*invalid*)";
16726
16727 static char *
16728 decode_mode_spec_coding (coding_system, buf, eol_flag)
16729 Lisp_Object coding_system;
16730 register char *buf;
16731 int eol_flag;
16732 {
16733 Lisp_Object val;
16734 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16735 const unsigned char *eol_str;
16736 int eol_str_len;
16737 /* The EOL conversion we are using. */
16738 Lisp_Object eoltype;
16739
16740 val = Fget (coding_system, Qcoding_system);
16741 eoltype = Qnil;
16742
16743 if (!VECTORP (val)) /* Not yet decided. */
16744 {
16745 if (multibyte)
16746 *buf++ = '-';
16747 if (eol_flag)
16748 eoltype = eol_mnemonic_undecided;
16749 /* Don't mention EOL conversion if it isn't decided. */
16750 }
16751 else
16752 {
16753 Lisp_Object eolvalue;
16754
16755 eolvalue = Fget (coding_system, Qeol_type);
16756
16757 if (multibyte)
16758 *buf++ = XFASTINT (AREF (val, 1));
16759
16760 if (eol_flag)
16761 {
16762 /* The EOL conversion that is normal on this system. */
16763
16764 if (NILP (eolvalue)) /* Not yet decided. */
16765 eoltype = eol_mnemonic_undecided;
16766 else if (VECTORP (eolvalue)) /* Not yet decided. */
16767 eoltype = eol_mnemonic_undecided;
16768 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16769 eoltype = (XFASTINT (eolvalue) == 0
16770 ? eol_mnemonic_unix
16771 : (XFASTINT (eolvalue) == 1
16772 ? eol_mnemonic_dos : eol_mnemonic_mac));
16773 }
16774 }
16775
16776 if (eol_flag)
16777 {
16778 /* Mention the EOL conversion if it is not the usual one. */
16779 if (STRINGP (eoltype))
16780 {
16781 eol_str = SDATA (eoltype);
16782 eol_str_len = SBYTES (eoltype);
16783 }
16784 else if (INTEGERP (eoltype)
16785 && CHAR_VALID_P (XINT (eoltype), 0))
16786 {
16787 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16788 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16789 eol_str = tmp;
16790 }
16791 else
16792 {
16793 eol_str = invalid_eol_type;
16794 eol_str_len = sizeof (invalid_eol_type) - 1;
16795 }
16796 bcopy (eol_str, buf, eol_str_len);
16797 buf += eol_str_len;
16798 }
16799
16800 return buf;
16801 }
16802
16803 /* Return a string for the output of a mode line %-spec for window W,
16804 generated by character C. PRECISION >= 0 means don't return a
16805 string longer than that value. FIELD_WIDTH > 0 means pad the
16806 string returned with spaces to that value. Return 1 in *MULTIBYTE
16807 if the result is multibyte text.
16808
16809 Note we operate on the current buffer for most purposes,
16810 the exception being w->base_line_pos. */
16811
16812 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16813
16814 static char *
16815 decode_mode_spec (w, c, field_width, precision, multibyte)
16816 struct window *w;
16817 register int c;
16818 int field_width, precision;
16819 int *multibyte;
16820 {
16821 Lisp_Object obj;
16822 struct frame *f = XFRAME (WINDOW_FRAME (w));
16823 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16824 struct buffer *b = current_buffer;
16825
16826 obj = Qnil;
16827 *multibyte = 0;
16828
16829 switch (c)
16830 {
16831 case '*':
16832 if (!NILP (b->read_only))
16833 return "%";
16834 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16835 return "*";
16836 return "-";
16837
16838 case '+':
16839 /* This differs from %* only for a modified read-only buffer. */
16840 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16841 return "*";
16842 if (!NILP (b->read_only))
16843 return "%";
16844 return "-";
16845
16846 case '&':
16847 /* This differs from %* in ignoring read-only-ness. */
16848 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16849 return "*";
16850 return "-";
16851
16852 case '%':
16853 return "%";
16854
16855 case '[':
16856 {
16857 int i;
16858 char *p;
16859
16860 if (command_loop_level > 5)
16861 return "[[[... ";
16862 p = decode_mode_spec_buf;
16863 for (i = 0; i < command_loop_level; i++)
16864 *p++ = '[';
16865 *p = 0;
16866 return decode_mode_spec_buf;
16867 }
16868
16869 case ']':
16870 {
16871 int i;
16872 char *p;
16873
16874 if (command_loop_level > 5)
16875 return " ...]]]";
16876 p = decode_mode_spec_buf;
16877 for (i = 0; i < command_loop_level; i++)
16878 *p++ = ']';
16879 *p = 0;
16880 return decode_mode_spec_buf;
16881 }
16882
16883 case '-':
16884 {
16885 register int i;
16886
16887 /* Let lots_of_dashes be a string of infinite length. */
16888 if (mode_line_target == MODE_LINE_NOPROP ||
16889 mode_line_target == MODE_LINE_STRING)
16890 return "--";
16891 if (field_width <= 0
16892 || field_width > sizeof (lots_of_dashes))
16893 {
16894 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16895 decode_mode_spec_buf[i] = '-';
16896 decode_mode_spec_buf[i] = '\0';
16897 return decode_mode_spec_buf;
16898 }
16899 else
16900 return lots_of_dashes;
16901 }
16902
16903 case 'b':
16904 obj = b->name;
16905 break;
16906
16907 case 'c':
16908 {
16909 int col = (int) current_column (); /* iftc */
16910 w->column_number_displayed = make_number (col);
16911 pint2str (decode_mode_spec_buf, field_width, col);
16912 return decode_mode_spec_buf;
16913 }
16914
16915 case 'F':
16916 /* %F displays the frame name. */
16917 if (!NILP (f->title))
16918 return (char *) SDATA (f->title);
16919 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16920 return (char *) SDATA (f->name);
16921 return "Emacs";
16922
16923 case 'f':
16924 obj = b->filename;
16925 break;
16926
16927 case 'i':
16928 {
16929 int size = ZV - BEGV;
16930 pint2str (decode_mode_spec_buf, field_width, size);
16931 return decode_mode_spec_buf;
16932 }
16933
16934 case 'I':
16935 {
16936 int size = ZV - BEGV;
16937 pint2hrstr (decode_mode_spec_buf, field_width, size);
16938 return decode_mode_spec_buf;
16939 }
16940
16941 case 'l':
16942 {
16943 int startpos = XMARKER (w->start)->charpos;
16944 int startpos_byte = marker_byte_position (w->start);
16945 int line, linepos, linepos_byte, topline;
16946 int nlines, junk;
16947 int height = WINDOW_TOTAL_LINES (w);
16948
16949 /* If we decided that this buffer isn't suitable for line numbers,
16950 don't forget that too fast. */
16951 if (EQ (w->base_line_pos, w->buffer))
16952 goto no_value;
16953 /* But do forget it, if the window shows a different buffer now. */
16954 else if (BUFFERP (w->base_line_pos))
16955 w->base_line_pos = Qnil;
16956
16957 /* If the buffer is very big, don't waste time. */
16958 if (INTEGERP (Vline_number_display_limit)
16959 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16960 {
16961 w->base_line_pos = Qnil;
16962 w->base_line_number = Qnil;
16963 goto no_value;
16964 }
16965
16966 if (!NILP (w->base_line_number)
16967 && !NILP (w->base_line_pos)
16968 && XFASTINT (w->base_line_pos) <= startpos)
16969 {
16970 line = XFASTINT (w->base_line_number);
16971 linepos = XFASTINT (w->base_line_pos);
16972 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16973 }
16974 else
16975 {
16976 line = 1;
16977 linepos = BUF_BEGV (b);
16978 linepos_byte = BUF_BEGV_BYTE (b);
16979 }
16980
16981 /* Count lines from base line to window start position. */
16982 nlines = display_count_lines (linepos, linepos_byte,
16983 startpos_byte,
16984 startpos, &junk);
16985
16986 topline = nlines + line;
16987
16988 /* Determine a new base line, if the old one is too close
16989 or too far away, or if we did not have one.
16990 "Too close" means it's plausible a scroll-down would
16991 go back past it. */
16992 if (startpos == BUF_BEGV (b))
16993 {
16994 w->base_line_number = make_number (topline);
16995 w->base_line_pos = make_number (BUF_BEGV (b));
16996 }
16997 else if (nlines < height + 25 || nlines > height * 3 + 50
16998 || linepos == BUF_BEGV (b))
16999 {
17000 int limit = BUF_BEGV (b);
17001 int limit_byte = BUF_BEGV_BYTE (b);
17002 int position;
17003 int distance = (height * 2 + 30) * line_number_display_limit_width;
17004
17005 if (startpos - distance > limit)
17006 {
17007 limit = startpos - distance;
17008 limit_byte = CHAR_TO_BYTE (limit);
17009 }
17010
17011 nlines = display_count_lines (startpos, startpos_byte,
17012 limit_byte,
17013 - (height * 2 + 30),
17014 &position);
17015 /* If we couldn't find the lines we wanted within
17016 line_number_display_limit_width chars per line,
17017 give up on line numbers for this window. */
17018 if (position == limit_byte && limit == startpos - distance)
17019 {
17020 w->base_line_pos = w->buffer;
17021 w->base_line_number = Qnil;
17022 goto no_value;
17023 }
17024
17025 w->base_line_number = make_number (topline - nlines);
17026 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17027 }
17028
17029 /* Now count lines from the start pos to point. */
17030 nlines = display_count_lines (startpos, startpos_byte,
17031 PT_BYTE, PT, &junk);
17032
17033 /* Record that we did display the line number. */
17034 line_number_displayed = 1;
17035
17036 /* Make the string to show. */
17037 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17038 return decode_mode_spec_buf;
17039 no_value:
17040 {
17041 char* p = decode_mode_spec_buf;
17042 int pad = field_width - 2;
17043 while (pad-- > 0)
17044 *p++ = ' ';
17045 *p++ = '?';
17046 *p++ = '?';
17047 *p = '\0';
17048 return decode_mode_spec_buf;
17049 }
17050 }
17051 break;
17052
17053 case 'm':
17054 obj = b->mode_name;
17055 break;
17056
17057 case 'n':
17058 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17059 return " Narrow";
17060 break;
17061
17062 case 'p':
17063 {
17064 int pos = marker_position (w->start);
17065 int total = BUF_ZV (b) - BUF_BEGV (b);
17066
17067 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17068 {
17069 if (pos <= BUF_BEGV (b))
17070 return "All";
17071 else
17072 return "Bottom";
17073 }
17074 else if (pos <= BUF_BEGV (b))
17075 return "Top";
17076 else
17077 {
17078 if (total > 1000000)
17079 /* Do it differently for a large value, to avoid overflow. */
17080 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17081 else
17082 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17083 /* We can't normally display a 3-digit number,
17084 so get us a 2-digit number that is close. */
17085 if (total == 100)
17086 total = 99;
17087 sprintf (decode_mode_spec_buf, "%2d%%", total);
17088 return decode_mode_spec_buf;
17089 }
17090 }
17091
17092 /* Display percentage of size above the bottom of the screen. */
17093 case 'P':
17094 {
17095 int toppos = marker_position (w->start);
17096 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17097 int total = BUF_ZV (b) - BUF_BEGV (b);
17098
17099 if (botpos >= BUF_ZV (b))
17100 {
17101 if (toppos <= BUF_BEGV (b))
17102 return "All";
17103 else
17104 return "Bottom";
17105 }
17106 else
17107 {
17108 if (total > 1000000)
17109 /* Do it differently for a large value, to avoid overflow. */
17110 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17111 else
17112 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17113 /* We can't normally display a 3-digit number,
17114 so get us a 2-digit number that is close. */
17115 if (total == 100)
17116 total = 99;
17117 if (toppos <= BUF_BEGV (b))
17118 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17119 else
17120 sprintf (decode_mode_spec_buf, "%2d%%", total);
17121 return decode_mode_spec_buf;
17122 }
17123 }
17124
17125 case 's':
17126 /* status of process */
17127 obj = Fget_buffer_process (Fcurrent_buffer ());
17128 if (NILP (obj))
17129 return "no process";
17130 #ifdef subprocesses
17131 obj = Fsymbol_name (Fprocess_status (obj));
17132 #endif
17133 break;
17134
17135 case 't': /* indicate TEXT or BINARY */
17136 #ifdef MODE_LINE_BINARY_TEXT
17137 return MODE_LINE_BINARY_TEXT (b);
17138 #else
17139 return "T";
17140 #endif
17141
17142 case 'z':
17143 /* coding-system (not including end-of-line format) */
17144 case 'Z':
17145 /* coding-system (including end-of-line type) */
17146 {
17147 int eol_flag = (c == 'Z');
17148 char *p = decode_mode_spec_buf;
17149
17150 if (! FRAME_WINDOW_P (f))
17151 {
17152 /* No need to mention EOL here--the terminal never needs
17153 to do EOL conversion. */
17154 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17155 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17156 }
17157 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17158 p, eol_flag);
17159
17160 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17161 #ifdef subprocesses
17162 obj = Fget_buffer_process (Fcurrent_buffer ());
17163 if (PROCESSP (obj))
17164 {
17165 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17166 p, eol_flag);
17167 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17168 p, eol_flag);
17169 }
17170 #endif /* subprocesses */
17171 #endif /* 0 */
17172 *p = 0;
17173 return decode_mode_spec_buf;
17174 }
17175 }
17176
17177 if (STRINGP (obj))
17178 {
17179 *multibyte = STRING_MULTIBYTE (obj);
17180 return (char *) SDATA (obj);
17181 }
17182 else
17183 return "";
17184 }
17185
17186
17187 /* Count up to COUNT lines starting from START / START_BYTE.
17188 But don't go beyond LIMIT_BYTE.
17189 Return the number of lines thus found (always nonnegative).
17190
17191 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17192
17193 static int
17194 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17195 int start, start_byte, limit_byte, count;
17196 int *byte_pos_ptr;
17197 {
17198 register unsigned char *cursor;
17199 unsigned char *base;
17200
17201 register int ceiling;
17202 register unsigned char *ceiling_addr;
17203 int orig_count = count;
17204
17205 /* If we are not in selective display mode,
17206 check only for newlines. */
17207 int selective_display = (!NILP (current_buffer->selective_display)
17208 && !INTEGERP (current_buffer->selective_display));
17209
17210 if (count > 0)
17211 {
17212 while (start_byte < limit_byte)
17213 {
17214 ceiling = BUFFER_CEILING_OF (start_byte);
17215 ceiling = min (limit_byte - 1, ceiling);
17216 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17217 base = (cursor = BYTE_POS_ADDR (start_byte));
17218 while (1)
17219 {
17220 if (selective_display)
17221 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17222 ;
17223 else
17224 while (*cursor != '\n' && ++cursor != ceiling_addr)
17225 ;
17226
17227 if (cursor != ceiling_addr)
17228 {
17229 if (--count == 0)
17230 {
17231 start_byte += cursor - base + 1;
17232 *byte_pos_ptr = start_byte;
17233 return orig_count;
17234 }
17235 else
17236 if (++cursor == ceiling_addr)
17237 break;
17238 }
17239 else
17240 break;
17241 }
17242 start_byte += cursor - base;
17243 }
17244 }
17245 else
17246 {
17247 while (start_byte > limit_byte)
17248 {
17249 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17250 ceiling = max (limit_byte, ceiling);
17251 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17252 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17253 while (1)
17254 {
17255 if (selective_display)
17256 while (--cursor != ceiling_addr
17257 && *cursor != '\n' && *cursor != 015)
17258 ;
17259 else
17260 while (--cursor != ceiling_addr && *cursor != '\n')
17261 ;
17262
17263 if (cursor != ceiling_addr)
17264 {
17265 if (++count == 0)
17266 {
17267 start_byte += cursor - base + 1;
17268 *byte_pos_ptr = start_byte;
17269 /* When scanning backwards, we should
17270 not count the newline posterior to which we stop. */
17271 return - orig_count - 1;
17272 }
17273 }
17274 else
17275 break;
17276 }
17277 /* Here we add 1 to compensate for the last decrement
17278 of CURSOR, which took it past the valid range. */
17279 start_byte += cursor - base + 1;
17280 }
17281 }
17282
17283 *byte_pos_ptr = limit_byte;
17284
17285 if (count < 0)
17286 return - orig_count + count;
17287 return orig_count - count;
17288
17289 }
17290
17291
17292 \f
17293 /***********************************************************************
17294 Displaying strings
17295 ***********************************************************************/
17296
17297 /* Display a NUL-terminated string, starting with index START.
17298
17299 If STRING is non-null, display that C string. Otherwise, the Lisp
17300 string LISP_STRING is displayed.
17301
17302 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17303 FACE_STRING. Display STRING or LISP_STRING with the face at
17304 FACE_STRING_POS in FACE_STRING:
17305
17306 Display the string in the environment given by IT, but use the
17307 standard display table, temporarily.
17308
17309 FIELD_WIDTH is the minimum number of output glyphs to produce.
17310 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17311 with spaces. If STRING has more characters, more than FIELD_WIDTH
17312 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17313
17314 PRECISION is the maximum number of characters to output from
17315 STRING. PRECISION < 0 means don't truncate the string.
17316
17317 This is roughly equivalent to printf format specifiers:
17318
17319 FIELD_WIDTH PRECISION PRINTF
17320 ----------------------------------------
17321 -1 -1 %s
17322 -1 10 %.10s
17323 10 -1 %10s
17324 20 10 %20.10s
17325
17326 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17327 display them, and < 0 means obey the current buffer's value of
17328 enable_multibyte_characters.
17329
17330 Value is the number of glyphs produced. */
17331
17332 static int
17333 display_string (string, lisp_string, face_string, face_string_pos,
17334 start, it, field_width, precision, max_x, multibyte)
17335 unsigned char *string;
17336 Lisp_Object lisp_string;
17337 Lisp_Object face_string;
17338 int face_string_pos;
17339 int start;
17340 struct it *it;
17341 int field_width, precision, max_x;
17342 int multibyte;
17343 {
17344 int hpos_at_start = it->hpos;
17345 int saved_face_id = it->face_id;
17346 struct glyph_row *row = it->glyph_row;
17347
17348 /* Initialize the iterator IT for iteration over STRING beginning
17349 with index START. */
17350 reseat_to_string (it, string, lisp_string, start,
17351 precision, field_width, multibyte);
17352
17353 /* If displaying STRING, set up the face of the iterator
17354 from LISP_STRING, if that's given. */
17355 if (STRINGP (face_string))
17356 {
17357 int endptr;
17358 struct face *face;
17359
17360 it->face_id
17361 = face_at_string_position (it->w, face_string, face_string_pos,
17362 0, it->region_beg_charpos,
17363 it->region_end_charpos,
17364 &endptr, it->base_face_id, 0);
17365 face = FACE_FROM_ID (it->f, it->face_id);
17366 it->face_box_p = face->box != FACE_NO_BOX;
17367 }
17368
17369 /* Set max_x to the maximum allowed X position. Don't let it go
17370 beyond the right edge of the window. */
17371 if (max_x <= 0)
17372 max_x = it->last_visible_x;
17373 else
17374 max_x = min (max_x, it->last_visible_x);
17375
17376 /* Skip over display elements that are not visible. because IT->w is
17377 hscrolled. */
17378 if (it->current_x < it->first_visible_x)
17379 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17380 MOVE_TO_POS | MOVE_TO_X);
17381
17382 row->ascent = it->max_ascent;
17383 row->height = it->max_ascent + it->max_descent;
17384 row->phys_ascent = it->max_phys_ascent;
17385 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17386 row->extra_line_spacing = it->max_extra_line_spacing;
17387
17388 /* This condition is for the case that we are called with current_x
17389 past last_visible_x. */
17390 while (it->current_x < max_x)
17391 {
17392 int x_before, x, n_glyphs_before, i, nglyphs;
17393
17394 /* Get the next display element. */
17395 if (!get_next_display_element (it))
17396 break;
17397
17398 /* Produce glyphs. */
17399 x_before = it->current_x;
17400 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17401 PRODUCE_GLYPHS (it);
17402
17403 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17404 i = 0;
17405 x = x_before;
17406 while (i < nglyphs)
17407 {
17408 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17409
17410 if (!it->truncate_lines_p
17411 && x + glyph->pixel_width > max_x)
17412 {
17413 /* End of continued line or max_x reached. */
17414 if (CHAR_GLYPH_PADDING_P (*glyph))
17415 {
17416 /* A wide character is unbreakable. */
17417 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17418 it->current_x = x_before;
17419 }
17420 else
17421 {
17422 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17423 it->current_x = x;
17424 }
17425 break;
17426 }
17427 else if (x + glyph->pixel_width > it->first_visible_x)
17428 {
17429 /* Glyph is at least partially visible. */
17430 ++it->hpos;
17431 if (x < it->first_visible_x)
17432 it->glyph_row->x = x - it->first_visible_x;
17433 }
17434 else
17435 {
17436 /* Glyph is off the left margin of the display area.
17437 Should not happen. */
17438 abort ();
17439 }
17440
17441 row->ascent = max (row->ascent, it->max_ascent);
17442 row->height = max (row->height, it->max_ascent + it->max_descent);
17443 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17444 row->phys_height = max (row->phys_height,
17445 it->max_phys_ascent + it->max_phys_descent);
17446 row->extra_line_spacing = max (row->extra_line_spacing,
17447 it->max_extra_line_spacing);
17448 x += glyph->pixel_width;
17449 ++i;
17450 }
17451
17452 /* Stop if max_x reached. */
17453 if (i < nglyphs)
17454 break;
17455
17456 /* Stop at line ends. */
17457 if (ITERATOR_AT_END_OF_LINE_P (it))
17458 {
17459 it->continuation_lines_width = 0;
17460 break;
17461 }
17462
17463 set_iterator_to_next (it, 1);
17464
17465 /* Stop if truncating at the right edge. */
17466 if (it->truncate_lines_p
17467 && it->current_x >= it->last_visible_x)
17468 {
17469 /* Add truncation mark, but don't do it if the line is
17470 truncated at a padding space. */
17471 if (IT_CHARPOS (*it) < it->string_nchars)
17472 {
17473 if (!FRAME_WINDOW_P (it->f))
17474 {
17475 int i, n;
17476
17477 if (it->current_x > it->last_visible_x)
17478 {
17479 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17480 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17481 break;
17482 for (n = row->used[TEXT_AREA]; i < n; ++i)
17483 {
17484 row->used[TEXT_AREA] = i;
17485 produce_special_glyphs (it, IT_TRUNCATION);
17486 }
17487 }
17488 produce_special_glyphs (it, IT_TRUNCATION);
17489 }
17490 it->glyph_row->truncated_on_right_p = 1;
17491 }
17492 break;
17493 }
17494 }
17495
17496 /* Maybe insert a truncation at the left. */
17497 if (it->first_visible_x
17498 && IT_CHARPOS (*it) > 0)
17499 {
17500 if (!FRAME_WINDOW_P (it->f))
17501 insert_left_trunc_glyphs (it);
17502 it->glyph_row->truncated_on_left_p = 1;
17503 }
17504
17505 it->face_id = saved_face_id;
17506
17507 /* Value is number of columns displayed. */
17508 return it->hpos - hpos_at_start;
17509 }
17510
17511
17512 \f
17513 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17514 appears as an element of LIST or as the car of an element of LIST.
17515 If PROPVAL is a list, compare each element against LIST in that
17516 way, and return 1/2 if any element of PROPVAL is found in LIST.
17517 Otherwise return 0. This function cannot quit.
17518 The return value is 2 if the text is invisible but with an ellipsis
17519 and 1 if it's invisible and without an ellipsis. */
17520
17521 int
17522 invisible_p (propval, list)
17523 register Lisp_Object propval;
17524 Lisp_Object list;
17525 {
17526 register Lisp_Object tail, proptail;
17527
17528 for (tail = list; CONSP (tail); tail = XCDR (tail))
17529 {
17530 register Lisp_Object tem;
17531 tem = XCAR (tail);
17532 if (EQ (propval, tem))
17533 return 1;
17534 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17535 return NILP (XCDR (tem)) ? 1 : 2;
17536 }
17537
17538 if (CONSP (propval))
17539 {
17540 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17541 {
17542 Lisp_Object propelt;
17543 propelt = XCAR (proptail);
17544 for (tail = list; CONSP (tail); tail = XCDR (tail))
17545 {
17546 register Lisp_Object tem;
17547 tem = XCAR (tail);
17548 if (EQ (propelt, tem))
17549 return 1;
17550 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17551 return NILP (XCDR (tem)) ? 1 : 2;
17552 }
17553 }
17554 }
17555
17556 return 0;
17557 }
17558
17559 /* Calculate a width or height in pixels from a specification using
17560 the following elements:
17561
17562 SPEC ::=
17563 NUM - a (fractional) multiple of the default font width/height
17564 (NUM) - specifies exactly NUM pixels
17565 UNIT - a fixed number of pixels, see below.
17566 ELEMENT - size of a display element in pixels, see below.
17567 (NUM . SPEC) - equals NUM * SPEC
17568 (+ SPEC SPEC ...) - add pixel values
17569 (- SPEC SPEC ...) - subtract pixel values
17570 (- SPEC) - negate pixel value
17571
17572 NUM ::=
17573 INT or FLOAT - a number constant
17574 SYMBOL - use symbol's (buffer local) variable binding.
17575
17576 UNIT ::=
17577 in - pixels per inch *)
17578 mm - pixels per 1/1000 meter *)
17579 cm - pixels per 1/100 meter *)
17580 width - width of current font in pixels.
17581 height - height of current font in pixels.
17582
17583 *) using the ratio(s) defined in display-pixels-per-inch.
17584
17585 ELEMENT ::=
17586
17587 left-fringe - left fringe width in pixels
17588 right-fringe - right fringe width in pixels
17589
17590 left-margin - left margin width in pixels
17591 right-margin - right margin width in pixels
17592
17593 scroll-bar - scroll-bar area width in pixels
17594
17595 Examples:
17596
17597 Pixels corresponding to 5 inches:
17598 (5 . in)
17599
17600 Total width of non-text areas on left side of window (if scroll-bar is on left):
17601 '(space :width (+ left-fringe left-margin scroll-bar))
17602
17603 Align to first text column (in header line):
17604 '(space :align-to 0)
17605
17606 Align to middle of text area minus half the width of variable `my-image'
17607 containing a loaded image:
17608 '(space :align-to (0.5 . (- text my-image)))
17609
17610 Width of left margin minus width of 1 character in the default font:
17611 '(space :width (- left-margin 1))
17612
17613 Width of left margin minus width of 2 characters in the current font:
17614 '(space :width (- left-margin (2 . width)))
17615
17616 Center 1 character over left-margin (in header line):
17617 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17618
17619 Different ways to express width of left fringe plus left margin minus one pixel:
17620 '(space :width (- (+ left-fringe left-margin) (1)))
17621 '(space :width (+ left-fringe left-margin (- (1))))
17622 '(space :width (+ left-fringe left-margin (-1)))
17623
17624 */
17625
17626 #define NUMVAL(X) \
17627 ((INTEGERP (X) || FLOATP (X)) \
17628 ? XFLOATINT (X) \
17629 : - 1)
17630
17631 int
17632 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17633 double *res;
17634 struct it *it;
17635 Lisp_Object prop;
17636 void *font;
17637 int width_p, *align_to;
17638 {
17639 double pixels;
17640
17641 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17642 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17643
17644 if (NILP (prop))
17645 return OK_PIXELS (0);
17646
17647 if (SYMBOLP (prop))
17648 {
17649 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17650 {
17651 char *unit = SDATA (SYMBOL_NAME (prop));
17652
17653 if (unit[0] == 'i' && unit[1] == 'n')
17654 pixels = 1.0;
17655 else if (unit[0] == 'm' && unit[1] == 'm')
17656 pixels = 25.4;
17657 else if (unit[0] == 'c' && unit[1] == 'm')
17658 pixels = 2.54;
17659 else
17660 pixels = 0;
17661 if (pixels > 0)
17662 {
17663 double ppi;
17664 #ifdef HAVE_WINDOW_SYSTEM
17665 if (FRAME_WINDOW_P (it->f)
17666 && (ppi = (width_p
17667 ? FRAME_X_DISPLAY_INFO (it->f)->resx
17668 : FRAME_X_DISPLAY_INFO (it->f)->resy),
17669 ppi > 0))
17670 return OK_PIXELS (ppi / pixels);
17671 #endif
17672
17673 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17674 || (CONSP (Vdisplay_pixels_per_inch)
17675 && (ppi = (width_p
17676 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17677 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17678 ppi > 0)))
17679 return OK_PIXELS (ppi / pixels);
17680
17681 return 0;
17682 }
17683 }
17684
17685 #ifdef HAVE_WINDOW_SYSTEM
17686 if (EQ (prop, Qheight))
17687 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17688 if (EQ (prop, Qwidth))
17689 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17690 #else
17691 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17692 return OK_PIXELS (1);
17693 #endif
17694
17695 if (EQ (prop, Qtext))
17696 return OK_PIXELS (width_p
17697 ? window_box_width (it->w, TEXT_AREA)
17698 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17699
17700 if (align_to && *align_to < 0)
17701 {
17702 *res = 0;
17703 if (EQ (prop, Qleft))
17704 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17705 if (EQ (prop, Qright))
17706 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17707 if (EQ (prop, Qcenter))
17708 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17709 + window_box_width (it->w, TEXT_AREA) / 2);
17710 if (EQ (prop, Qleft_fringe))
17711 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17712 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17713 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17714 if (EQ (prop, Qright_fringe))
17715 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17716 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17717 : window_box_right_offset (it->w, TEXT_AREA));
17718 if (EQ (prop, Qleft_margin))
17719 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17720 if (EQ (prop, Qright_margin))
17721 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17722 if (EQ (prop, Qscroll_bar))
17723 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17724 ? 0
17725 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17726 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17727 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17728 : 0)));
17729 }
17730 else
17731 {
17732 if (EQ (prop, Qleft_fringe))
17733 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17734 if (EQ (prop, Qright_fringe))
17735 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17736 if (EQ (prop, Qleft_margin))
17737 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17738 if (EQ (prop, Qright_margin))
17739 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17740 if (EQ (prop, Qscroll_bar))
17741 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17742 }
17743
17744 prop = Fbuffer_local_value (prop, it->w->buffer);
17745 }
17746
17747 if (INTEGERP (prop) || FLOATP (prop))
17748 {
17749 int base_unit = (width_p
17750 ? FRAME_COLUMN_WIDTH (it->f)
17751 : FRAME_LINE_HEIGHT (it->f));
17752 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17753 }
17754
17755 if (CONSP (prop))
17756 {
17757 Lisp_Object car = XCAR (prop);
17758 Lisp_Object cdr = XCDR (prop);
17759
17760 if (SYMBOLP (car))
17761 {
17762 #ifdef HAVE_WINDOW_SYSTEM
17763 if (valid_image_p (prop))
17764 {
17765 int id = lookup_image (it->f, prop);
17766 struct image *img = IMAGE_FROM_ID (it->f, id);
17767
17768 return OK_PIXELS (width_p ? img->width : img->height);
17769 }
17770 #endif
17771 if (EQ (car, Qplus) || EQ (car, Qminus))
17772 {
17773 int first = 1;
17774 double px;
17775
17776 pixels = 0;
17777 while (CONSP (cdr))
17778 {
17779 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17780 font, width_p, align_to))
17781 return 0;
17782 if (first)
17783 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17784 else
17785 pixels += px;
17786 cdr = XCDR (cdr);
17787 }
17788 if (EQ (car, Qminus))
17789 pixels = -pixels;
17790 return OK_PIXELS (pixels);
17791 }
17792
17793 car = Fbuffer_local_value (car, it->w->buffer);
17794 }
17795
17796 if (INTEGERP (car) || FLOATP (car))
17797 {
17798 double fact;
17799 pixels = XFLOATINT (car);
17800 if (NILP (cdr))
17801 return OK_PIXELS (pixels);
17802 if (calc_pixel_width_or_height (&fact, it, cdr,
17803 font, width_p, align_to))
17804 return OK_PIXELS (pixels * fact);
17805 return 0;
17806 }
17807
17808 return 0;
17809 }
17810
17811 return 0;
17812 }
17813
17814 \f
17815 /***********************************************************************
17816 Glyph Display
17817 ***********************************************************************/
17818
17819 #ifdef HAVE_WINDOW_SYSTEM
17820
17821 #if GLYPH_DEBUG
17822
17823 void
17824 dump_glyph_string (s)
17825 struct glyph_string *s;
17826 {
17827 fprintf (stderr, "glyph string\n");
17828 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17829 s->x, s->y, s->width, s->height);
17830 fprintf (stderr, " ybase = %d\n", s->ybase);
17831 fprintf (stderr, " hl = %d\n", s->hl);
17832 fprintf (stderr, " left overhang = %d, right = %d\n",
17833 s->left_overhang, s->right_overhang);
17834 fprintf (stderr, " nchars = %d\n", s->nchars);
17835 fprintf (stderr, " extends to end of line = %d\n",
17836 s->extends_to_end_of_line_p);
17837 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17838 fprintf (stderr, " bg width = %d\n", s->background_width);
17839 }
17840
17841 #endif /* GLYPH_DEBUG */
17842
17843 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17844 of XChar2b structures for S; it can't be allocated in
17845 init_glyph_string because it must be allocated via `alloca'. W
17846 is the window on which S is drawn. ROW and AREA are the glyph row
17847 and area within the row from which S is constructed. START is the
17848 index of the first glyph structure covered by S. HL is a
17849 face-override for drawing S. */
17850
17851 #ifdef HAVE_NTGUI
17852 #define OPTIONAL_HDC(hdc) hdc,
17853 #define DECLARE_HDC(hdc) HDC hdc;
17854 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17855 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17856 #endif
17857
17858 #ifndef OPTIONAL_HDC
17859 #define OPTIONAL_HDC(hdc)
17860 #define DECLARE_HDC(hdc)
17861 #define ALLOCATE_HDC(hdc, f)
17862 #define RELEASE_HDC(hdc, f)
17863 #endif
17864
17865 static void
17866 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17867 struct glyph_string *s;
17868 DECLARE_HDC (hdc)
17869 XChar2b *char2b;
17870 struct window *w;
17871 struct glyph_row *row;
17872 enum glyph_row_area area;
17873 int start;
17874 enum draw_glyphs_face hl;
17875 {
17876 bzero (s, sizeof *s);
17877 s->w = w;
17878 s->f = XFRAME (w->frame);
17879 #ifdef HAVE_NTGUI
17880 s->hdc = hdc;
17881 #endif
17882 s->display = FRAME_X_DISPLAY (s->f);
17883 s->window = FRAME_X_WINDOW (s->f);
17884 s->char2b = char2b;
17885 s->hl = hl;
17886 s->row = row;
17887 s->area = area;
17888 s->first_glyph = row->glyphs[area] + start;
17889 s->height = row->height;
17890 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17891
17892 /* Display the internal border below the tool-bar window. */
17893 if (WINDOWP (s->f->tool_bar_window)
17894 && s->w == XWINDOW (s->f->tool_bar_window))
17895 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17896
17897 s->ybase = s->y + row->ascent;
17898 }
17899
17900
17901 /* Append the list of glyph strings with head H and tail T to the list
17902 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17903
17904 static INLINE void
17905 append_glyph_string_lists (head, tail, h, t)
17906 struct glyph_string **head, **tail;
17907 struct glyph_string *h, *t;
17908 {
17909 if (h)
17910 {
17911 if (*head)
17912 (*tail)->next = h;
17913 else
17914 *head = h;
17915 h->prev = *tail;
17916 *tail = t;
17917 }
17918 }
17919
17920
17921 /* Prepend the list of glyph strings with head H and tail T to the
17922 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17923 result. */
17924
17925 static INLINE void
17926 prepend_glyph_string_lists (head, tail, h, t)
17927 struct glyph_string **head, **tail;
17928 struct glyph_string *h, *t;
17929 {
17930 if (h)
17931 {
17932 if (*head)
17933 (*head)->prev = t;
17934 else
17935 *tail = t;
17936 t->next = *head;
17937 *head = h;
17938 }
17939 }
17940
17941
17942 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17943 Set *HEAD and *TAIL to the resulting list. */
17944
17945 static INLINE void
17946 append_glyph_string (head, tail, s)
17947 struct glyph_string **head, **tail;
17948 struct glyph_string *s;
17949 {
17950 s->next = s->prev = NULL;
17951 append_glyph_string_lists (head, tail, s, s);
17952 }
17953
17954
17955 /* Get face and two-byte form of character glyph GLYPH on frame F.
17956 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17957 a pointer to a realized face that is ready for display. */
17958
17959 static INLINE struct face *
17960 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17961 struct frame *f;
17962 struct glyph *glyph;
17963 XChar2b *char2b;
17964 int *two_byte_p;
17965 {
17966 struct face *face;
17967
17968 xassert (glyph->type == CHAR_GLYPH);
17969 face = FACE_FROM_ID (f, glyph->face_id);
17970
17971 if (two_byte_p)
17972 *two_byte_p = 0;
17973
17974 if (!glyph->multibyte_p)
17975 {
17976 /* Unibyte case. We don't have to encode, but we have to make
17977 sure to use a face suitable for unibyte. */
17978 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17979 }
17980 else if (glyph->u.ch < 128
17981 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17982 {
17983 /* Case of ASCII in a face known to fit ASCII. */
17984 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17985 }
17986 else
17987 {
17988 int c1, c2, charset;
17989
17990 /* Split characters into bytes. If c2 is -1 afterwards, C is
17991 really a one-byte character so that byte1 is zero. */
17992 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17993 if (c2 > 0)
17994 STORE_XCHAR2B (char2b, c1, c2);
17995 else
17996 STORE_XCHAR2B (char2b, 0, c1);
17997
17998 /* Maybe encode the character in *CHAR2B. */
17999 if (charset != CHARSET_ASCII)
18000 {
18001 struct font_info *font_info
18002 = FONT_INFO_FROM_ID (f, face->font_info_id);
18003 if (font_info)
18004 glyph->font_type
18005 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18006 }
18007 }
18008
18009 /* Make sure X resources of the face are allocated. */
18010 xassert (face != NULL);
18011 PREPARE_FACE_FOR_DISPLAY (f, face);
18012 return face;
18013 }
18014
18015
18016 /* Fill glyph string S with composition components specified by S->cmp.
18017
18018 FACES is an array of faces for all components of this composition.
18019 S->gidx is the index of the first component for S.
18020 OVERLAPS_P non-zero means S should draw the foreground only, and
18021 use its physical height for clipping.
18022
18023 Value is the index of a component not in S. */
18024
18025 static int
18026 fill_composite_glyph_string (s, faces, overlaps_p)
18027 struct glyph_string *s;
18028 struct face **faces;
18029 int overlaps_p;
18030 {
18031 int i;
18032
18033 xassert (s);
18034
18035 s->for_overlaps_p = overlaps_p;
18036
18037 s->face = faces[s->gidx];
18038 s->font = s->face->font;
18039 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18040
18041 /* For all glyphs of this composition, starting at the offset
18042 S->gidx, until we reach the end of the definition or encounter a
18043 glyph that requires the different face, add it to S. */
18044 ++s->nchars;
18045 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18046 ++s->nchars;
18047
18048 /* All glyph strings for the same composition has the same width,
18049 i.e. the width set for the first component of the composition. */
18050
18051 s->width = s->first_glyph->pixel_width;
18052
18053 /* If the specified font could not be loaded, use the frame's
18054 default font, but record the fact that we couldn't load it in
18055 the glyph string so that we can draw rectangles for the
18056 characters of the glyph string. */
18057 if (s->font == NULL)
18058 {
18059 s->font_not_found_p = 1;
18060 s->font = FRAME_FONT (s->f);
18061 }
18062
18063 /* Adjust base line for subscript/superscript text. */
18064 s->ybase += s->first_glyph->voffset;
18065
18066 xassert (s->face && s->face->gc);
18067
18068 /* This glyph string must always be drawn with 16-bit functions. */
18069 s->two_byte_p = 1;
18070
18071 return s->gidx + s->nchars;
18072 }
18073
18074
18075 /* Fill glyph string S from a sequence of character glyphs.
18076
18077 FACE_ID is the face id of the string. START is the index of the
18078 first glyph to consider, END is the index of the last + 1.
18079 OVERLAPS_P non-zero means S should draw the foreground only, and
18080 use its physical height for clipping.
18081
18082 Value is the index of the first glyph not in S. */
18083
18084 static int
18085 fill_glyph_string (s, face_id, start, end, overlaps_p)
18086 struct glyph_string *s;
18087 int face_id;
18088 int start, end, overlaps_p;
18089 {
18090 struct glyph *glyph, *last;
18091 int voffset;
18092 int glyph_not_available_p;
18093
18094 xassert (s->f == XFRAME (s->w->frame));
18095 xassert (s->nchars == 0);
18096 xassert (start >= 0 && end > start);
18097
18098 s->for_overlaps_p = overlaps_p,
18099 glyph = s->row->glyphs[s->area] + start;
18100 last = s->row->glyphs[s->area] + end;
18101 voffset = glyph->voffset;
18102
18103 glyph_not_available_p = glyph->glyph_not_available_p;
18104
18105 while (glyph < last
18106 && glyph->type == CHAR_GLYPH
18107 && glyph->voffset == voffset
18108 /* Same face id implies same font, nowadays. */
18109 && glyph->face_id == face_id
18110 && glyph->glyph_not_available_p == glyph_not_available_p)
18111 {
18112 int two_byte_p;
18113
18114 s->face = get_glyph_face_and_encoding (s->f, glyph,
18115 s->char2b + s->nchars,
18116 &two_byte_p);
18117 s->two_byte_p = two_byte_p;
18118 ++s->nchars;
18119 xassert (s->nchars <= end - start);
18120 s->width += glyph->pixel_width;
18121 ++glyph;
18122 }
18123
18124 s->font = s->face->font;
18125 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18126
18127 /* If the specified font could not be loaded, use the frame's font,
18128 but record the fact that we couldn't load it in
18129 S->font_not_found_p so that we can draw rectangles for the
18130 characters of the glyph string. */
18131 if (s->font == NULL || glyph_not_available_p)
18132 {
18133 s->font_not_found_p = 1;
18134 s->font = FRAME_FONT (s->f);
18135 }
18136
18137 /* Adjust base line for subscript/superscript text. */
18138 s->ybase += voffset;
18139
18140 xassert (s->face && s->face->gc);
18141 return glyph - s->row->glyphs[s->area];
18142 }
18143
18144
18145 /* Fill glyph string S from image glyph S->first_glyph. */
18146
18147 static void
18148 fill_image_glyph_string (s)
18149 struct glyph_string *s;
18150 {
18151 xassert (s->first_glyph->type == IMAGE_GLYPH);
18152 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18153 xassert (s->img);
18154 s->slice = s->first_glyph->slice;
18155 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18156 s->font = s->face->font;
18157 s->width = s->first_glyph->pixel_width;
18158
18159 /* Adjust base line for subscript/superscript text. */
18160 s->ybase += s->first_glyph->voffset;
18161 }
18162
18163
18164 /* Fill glyph string S from a sequence of stretch glyphs.
18165
18166 ROW is the glyph row in which the glyphs are found, AREA is the
18167 area within the row. START is the index of the first glyph to
18168 consider, END is the index of the last + 1.
18169
18170 Value is the index of the first glyph not in S. */
18171
18172 static int
18173 fill_stretch_glyph_string (s, row, area, start, end)
18174 struct glyph_string *s;
18175 struct glyph_row *row;
18176 enum glyph_row_area area;
18177 int start, end;
18178 {
18179 struct glyph *glyph, *last;
18180 int voffset, face_id;
18181
18182 xassert (s->first_glyph->type == STRETCH_GLYPH);
18183
18184 glyph = s->row->glyphs[s->area] + start;
18185 last = s->row->glyphs[s->area] + end;
18186 face_id = glyph->face_id;
18187 s->face = FACE_FROM_ID (s->f, face_id);
18188 s->font = s->face->font;
18189 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18190 s->width = glyph->pixel_width;
18191 voffset = glyph->voffset;
18192
18193 for (++glyph;
18194 (glyph < last
18195 && glyph->type == STRETCH_GLYPH
18196 && glyph->voffset == voffset
18197 && glyph->face_id == face_id);
18198 ++glyph)
18199 s->width += glyph->pixel_width;
18200
18201 /* Adjust base line for subscript/superscript text. */
18202 s->ybase += voffset;
18203
18204 /* The case that face->gc == 0 is handled when drawing the glyph
18205 string by calling PREPARE_FACE_FOR_DISPLAY. */
18206 xassert (s->face);
18207 return glyph - s->row->glyphs[s->area];
18208 }
18209
18210
18211 /* EXPORT for RIF:
18212 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18213 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18214 assumed to be zero. */
18215
18216 void
18217 x_get_glyph_overhangs (glyph, f, left, right)
18218 struct glyph *glyph;
18219 struct frame *f;
18220 int *left, *right;
18221 {
18222 *left = *right = 0;
18223
18224 if (glyph->type == CHAR_GLYPH)
18225 {
18226 XFontStruct *font;
18227 struct face *face;
18228 struct font_info *font_info;
18229 XChar2b char2b;
18230 XCharStruct *pcm;
18231
18232 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18233 font = face->font;
18234 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18235 if (font /* ++KFS: Should this be font_info ? */
18236 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18237 {
18238 if (pcm->rbearing > pcm->width)
18239 *right = pcm->rbearing - pcm->width;
18240 if (pcm->lbearing < 0)
18241 *left = -pcm->lbearing;
18242 }
18243 }
18244 }
18245
18246
18247 /* Return the index of the first glyph preceding glyph string S that
18248 is overwritten by S because of S's left overhang. Value is -1
18249 if no glyphs are overwritten. */
18250
18251 static int
18252 left_overwritten (s)
18253 struct glyph_string *s;
18254 {
18255 int k;
18256
18257 if (s->left_overhang)
18258 {
18259 int x = 0, i;
18260 struct glyph *glyphs = s->row->glyphs[s->area];
18261 int first = s->first_glyph - glyphs;
18262
18263 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18264 x -= glyphs[i].pixel_width;
18265
18266 k = i + 1;
18267 }
18268 else
18269 k = -1;
18270
18271 return k;
18272 }
18273
18274
18275 /* Return the index of the first glyph preceding glyph string S that
18276 is overwriting S because of its right overhang. Value is -1 if no
18277 glyph in front of S overwrites S. */
18278
18279 static int
18280 left_overwriting (s)
18281 struct glyph_string *s;
18282 {
18283 int i, k, x;
18284 struct glyph *glyphs = s->row->glyphs[s->area];
18285 int first = s->first_glyph - glyphs;
18286
18287 k = -1;
18288 x = 0;
18289 for (i = first - 1; i >= 0; --i)
18290 {
18291 int left, right;
18292 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18293 if (x + right > 0)
18294 k = i;
18295 x -= glyphs[i].pixel_width;
18296 }
18297
18298 return k;
18299 }
18300
18301
18302 /* Return the index of the last glyph following glyph string S that is
18303 not overwritten by S because of S's right overhang. Value is -1 if
18304 no such glyph is found. */
18305
18306 static int
18307 right_overwritten (s)
18308 struct glyph_string *s;
18309 {
18310 int k = -1;
18311
18312 if (s->right_overhang)
18313 {
18314 int x = 0, i;
18315 struct glyph *glyphs = s->row->glyphs[s->area];
18316 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18317 int end = s->row->used[s->area];
18318
18319 for (i = first; i < end && s->right_overhang > x; ++i)
18320 x += glyphs[i].pixel_width;
18321
18322 k = i;
18323 }
18324
18325 return k;
18326 }
18327
18328
18329 /* Return the index of the last glyph following glyph string S that
18330 overwrites S because of its left overhang. Value is negative
18331 if no such glyph is found. */
18332
18333 static int
18334 right_overwriting (s)
18335 struct glyph_string *s;
18336 {
18337 int i, k, x;
18338 int end = s->row->used[s->area];
18339 struct glyph *glyphs = s->row->glyphs[s->area];
18340 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18341
18342 k = -1;
18343 x = 0;
18344 for (i = first; i < end; ++i)
18345 {
18346 int left, right;
18347 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18348 if (x - left < 0)
18349 k = i;
18350 x += glyphs[i].pixel_width;
18351 }
18352
18353 return k;
18354 }
18355
18356
18357 /* Get face and two-byte form of character C in face FACE_ID on frame
18358 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18359 means we want to display multibyte text. DISPLAY_P non-zero means
18360 make sure that X resources for the face returned are allocated.
18361 Value is a pointer to a realized face that is ready for display if
18362 DISPLAY_P is non-zero. */
18363
18364 static INLINE struct face *
18365 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18366 struct frame *f;
18367 int c, face_id;
18368 XChar2b *char2b;
18369 int multibyte_p, display_p;
18370 {
18371 struct face *face = FACE_FROM_ID (f, face_id);
18372
18373 if (!multibyte_p)
18374 {
18375 /* Unibyte case. We don't have to encode, but we have to make
18376 sure to use a face suitable for unibyte. */
18377 STORE_XCHAR2B (char2b, 0, c);
18378 face_id = FACE_FOR_CHAR (f, face, c);
18379 face = FACE_FROM_ID (f, face_id);
18380 }
18381 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18382 {
18383 /* Case of ASCII in a face known to fit ASCII. */
18384 STORE_XCHAR2B (char2b, 0, c);
18385 }
18386 else
18387 {
18388 int c1, c2, charset;
18389
18390 /* Split characters into bytes. If c2 is -1 afterwards, C is
18391 really a one-byte character so that byte1 is zero. */
18392 SPLIT_CHAR (c, charset, c1, c2);
18393 if (c2 > 0)
18394 STORE_XCHAR2B (char2b, c1, c2);
18395 else
18396 STORE_XCHAR2B (char2b, 0, c1);
18397
18398 /* Maybe encode the character in *CHAR2B. */
18399 if (face->font != NULL)
18400 {
18401 struct font_info *font_info
18402 = FONT_INFO_FROM_ID (f, face->font_info_id);
18403 if (font_info)
18404 rif->encode_char (c, char2b, font_info, 0);
18405 }
18406 }
18407
18408 /* Make sure X resources of the face are allocated. */
18409 #ifdef HAVE_X_WINDOWS
18410 if (display_p)
18411 #endif
18412 {
18413 xassert (face != NULL);
18414 PREPARE_FACE_FOR_DISPLAY (f, face);
18415 }
18416
18417 return face;
18418 }
18419
18420
18421 /* Set background width of glyph string S. START is the index of the
18422 first glyph following S. LAST_X is the right-most x-position + 1
18423 in the drawing area. */
18424
18425 static INLINE void
18426 set_glyph_string_background_width (s, start, last_x)
18427 struct glyph_string *s;
18428 int start;
18429 int last_x;
18430 {
18431 /* If the face of this glyph string has to be drawn to the end of
18432 the drawing area, set S->extends_to_end_of_line_p. */
18433 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18434
18435 if (start == s->row->used[s->area]
18436 && s->area == TEXT_AREA
18437 && ((s->hl == DRAW_NORMAL_TEXT
18438 && (s->row->fill_line_p
18439 || s->face->background != default_face->background
18440 || s->face->stipple != default_face->stipple
18441 || s->row->mouse_face_p))
18442 || s->hl == DRAW_MOUSE_FACE
18443 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18444 && s->row->fill_line_p)))
18445 s->extends_to_end_of_line_p = 1;
18446
18447 /* If S extends its face to the end of the line, set its
18448 background_width to the distance to the right edge of the drawing
18449 area. */
18450 if (s->extends_to_end_of_line_p)
18451 s->background_width = last_x - s->x + 1;
18452 else
18453 s->background_width = s->width;
18454 }
18455
18456
18457 /* Compute overhangs and x-positions for glyph string S and its
18458 predecessors, or successors. X is the starting x-position for S.
18459 BACKWARD_P non-zero means process predecessors. */
18460
18461 static void
18462 compute_overhangs_and_x (s, x, backward_p)
18463 struct glyph_string *s;
18464 int x;
18465 int backward_p;
18466 {
18467 if (backward_p)
18468 {
18469 while (s)
18470 {
18471 if (rif->compute_glyph_string_overhangs)
18472 rif->compute_glyph_string_overhangs (s);
18473 x -= s->width;
18474 s->x = x;
18475 s = s->prev;
18476 }
18477 }
18478 else
18479 {
18480 while (s)
18481 {
18482 if (rif->compute_glyph_string_overhangs)
18483 rif->compute_glyph_string_overhangs (s);
18484 s->x = x;
18485 x += s->width;
18486 s = s->next;
18487 }
18488 }
18489 }
18490
18491
18492
18493 /* The following macros are only called from draw_glyphs below.
18494 They reference the following parameters of that function directly:
18495 `w', `row', `area', and `overlap_p'
18496 as well as the following local variables:
18497 `s', `f', and `hdc' (in W32) */
18498
18499 #ifdef HAVE_NTGUI
18500 /* On W32, silently add local `hdc' variable to argument list of
18501 init_glyph_string. */
18502 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18503 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18504 #else
18505 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18506 init_glyph_string (s, char2b, w, row, area, start, hl)
18507 #endif
18508
18509 /* Add a glyph string for a stretch glyph to the list of strings
18510 between HEAD and TAIL. START is the index of the stretch glyph in
18511 row area AREA of glyph row ROW. END is the index of the last glyph
18512 in that glyph row area. X is the current output position assigned
18513 to the new glyph string constructed. HL overrides that face of the
18514 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18515 is the right-most x-position of the drawing area. */
18516
18517 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18518 and below -- keep them on one line. */
18519 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18520 do \
18521 { \
18522 s = (struct glyph_string *) alloca (sizeof *s); \
18523 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18524 START = fill_stretch_glyph_string (s, row, area, START, END); \
18525 append_glyph_string (&HEAD, &TAIL, s); \
18526 s->x = (X); \
18527 } \
18528 while (0)
18529
18530
18531 /* Add a glyph string for an image glyph to the list of strings
18532 between HEAD and TAIL. START is the index of the image glyph in
18533 row area AREA of glyph row ROW. END is the index of the last glyph
18534 in that glyph row area. X is the current output position assigned
18535 to the new glyph string constructed. HL overrides that face of the
18536 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18537 is the right-most x-position of the drawing area. */
18538
18539 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18540 do \
18541 { \
18542 s = (struct glyph_string *) alloca (sizeof *s); \
18543 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18544 fill_image_glyph_string (s); \
18545 append_glyph_string (&HEAD, &TAIL, s); \
18546 ++START; \
18547 s->x = (X); \
18548 } \
18549 while (0)
18550
18551
18552 /* Add a glyph string for a sequence of character glyphs to the list
18553 of strings between HEAD and TAIL. START is the index of the first
18554 glyph in row area AREA of glyph row ROW that is part of the new
18555 glyph string. END is the index of the last glyph in that glyph row
18556 area. X is the current output position assigned to the new glyph
18557 string constructed. HL overrides that face of the glyph; e.g. it
18558 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18559 right-most x-position of the drawing area. */
18560
18561 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18562 do \
18563 { \
18564 int c, face_id; \
18565 XChar2b *char2b; \
18566 \
18567 c = (row)->glyphs[area][START].u.ch; \
18568 face_id = (row)->glyphs[area][START].face_id; \
18569 \
18570 s = (struct glyph_string *) alloca (sizeof *s); \
18571 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18572 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18573 append_glyph_string (&HEAD, &TAIL, s); \
18574 s->x = (X); \
18575 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18576 } \
18577 while (0)
18578
18579
18580 /* Add a glyph string for a composite sequence to the list of strings
18581 between HEAD and TAIL. START is the index of the first glyph in
18582 row area AREA of glyph row ROW that is part of the new glyph
18583 string. END is the index of the last glyph in that glyph row area.
18584 X is the current output position assigned to the new glyph string
18585 constructed. HL overrides that face of the glyph; e.g. it is
18586 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18587 x-position of the drawing area. */
18588
18589 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18590 do { \
18591 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18592 int face_id = (row)->glyphs[area][START].face_id; \
18593 struct face *base_face = FACE_FROM_ID (f, face_id); \
18594 struct composition *cmp = composition_table[cmp_id]; \
18595 int glyph_len = cmp->glyph_len; \
18596 XChar2b *char2b; \
18597 struct face **faces; \
18598 struct glyph_string *first_s = NULL; \
18599 int n; \
18600 \
18601 base_face = base_face->ascii_face; \
18602 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18603 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18604 /* At first, fill in `char2b' and `faces'. */ \
18605 for (n = 0; n < glyph_len; n++) \
18606 { \
18607 int c = COMPOSITION_GLYPH (cmp, n); \
18608 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18609 faces[n] = FACE_FROM_ID (f, this_face_id); \
18610 get_char_face_and_encoding (f, c, this_face_id, \
18611 char2b + n, 1, 1); \
18612 } \
18613 \
18614 /* Make glyph_strings for each glyph sequence that is drawable by \
18615 the same face, and append them to HEAD/TAIL. */ \
18616 for (n = 0; n < cmp->glyph_len;) \
18617 { \
18618 s = (struct glyph_string *) alloca (sizeof *s); \
18619 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18620 append_glyph_string (&(HEAD), &(TAIL), s); \
18621 s->cmp = cmp; \
18622 s->gidx = n; \
18623 s->x = (X); \
18624 \
18625 if (n == 0) \
18626 first_s = s; \
18627 \
18628 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18629 } \
18630 \
18631 ++START; \
18632 s = first_s; \
18633 } while (0)
18634
18635
18636 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18637 of AREA of glyph row ROW on window W between indices START and END.
18638 HL overrides the face for drawing glyph strings, e.g. it is
18639 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18640 x-positions of the drawing area.
18641
18642 This is an ugly monster macro construct because we must use alloca
18643 to allocate glyph strings (because draw_glyphs can be called
18644 asynchronously). */
18645
18646 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18647 do \
18648 { \
18649 HEAD = TAIL = NULL; \
18650 while (START < END) \
18651 { \
18652 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18653 switch (first_glyph->type) \
18654 { \
18655 case CHAR_GLYPH: \
18656 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18657 HL, X, LAST_X); \
18658 break; \
18659 \
18660 case COMPOSITE_GLYPH: \
18661 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18662 HL, X, LAST_X); \
18663 break; \
18664 \
18665 case STRETCH_GLYPH: \
18666 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18667 HL, X, LAST_X); \
18668 break; \
18669 \
18670 case IMAGE_GLYPH: \
18671 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18672 HL, X, LAST_X); \
18673 break; \
18674 \
18675 default: \
18676 abort (); \
18677 } \
18678 \
18679 set_glyph_string_background_width (s, START, LAST_X); \
18680 (X) += s->width; \
18681 } \
18682 } \
18683 while (0)
18684
18685
18686 /* Draw glyphs between START and END in AREA of ROW on window W,
18687 starting at x-position X. X is relative to AREA in W. HL is a
18688 face-override with the following meaning:
18689
18690 DRAW_NORMAL_TEXT draw normally
18691 DRAW_CURSOR draw in cursor face
18692 DRAW_MOUSE_FACE draw in mouse face.
18693 DRAW_INVERSE_VIDEO draw in mode line face
18694 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18695 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18696
18697 If OVERLAPS_P is non-zero, draw only the foreground of characters
18698 and clip to the physical height of ROW.
18699
18700 Value is the x-position reached, relative to AREA of W. */
18701
18702 static int
18703 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18704 struct window *w;
18705 int x;
18706 struct glyph_row *row;
18707 enum glyph_row_area area;
18708 int start, end;
18709 enum draw_glyphs_face hl;
18710 int overlaps_p;
18711 {
18712 struct glyph_string *head, *tail;
18713 struct glyph_string *s;
18714 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18715 int last_x, area_width;
18716 int x_reached;
18717 int i, j;
18718 struct frame *f = XFRAME (WINDOW_FRAME (w));
18719 DECLARE_HDC (hdc);
18720
18721 ALLOCATE_HDC (hdc, f);
18722
18723 /* Let's rather be paranoid than getting a SEGV. */
18724 end = min (end, row->used[area]);
18725 start = max (0, start);
18726 start = min (end, start);
18727
18728 /* Translate X to frame coordinates. Set last_x to the right
18729 end of the drawing area. */
18730 if (row->full_width_p)
18731 {
18732 /* X is relative to the left edge of W, without scroll bars
18733 or fringes. */
18734 x += WINDOW_LEFT_EDGE_X (w);
18735 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18736 }
18737 else
18738 {
18739 int area_left = window_box_left (w, area);
18740 x += area_left;
18741 area_width = window_box_width (w, area);
18742 last_x = area_left + area_width;
18743 }
18744
18745 /* Build a doubly-linked list of glyph_string structures between
18746 head and tail from what we have to draw. Note that the macro
18747 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18748 the reason we use a separate variable `i'. */
18749 i = start;
18750 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18751 if (tail)
18752 x_reached = tail->x + tail->background_width;
18753 else
18754 x_reached = x;
18755
18756 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18757 the row, redraw some glyphs in front or following the glyph
18758 strings built above. */
18759 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18760 {
18761 int dummy_x = 0;
18762 struct glyph_string *h, *t;
18763
18764 /* Compute overhangs for all glyph strings. */
18765 if (rif->compute_glyph_string_overhangs)
18766 for (s = head; s; s = s->next)
18767 rif->compute_glyph_string_overhangs (s);
18768
18769 /* Prepend glyph strings for glyphs in front of the first glyph
18770 string that are overwritten because of the first glyph
18771 string's left overhang. The background of all strings
18772 prepended must be drawn because the first glyph string
18773 draws over it. */
18774 i = left_overwritten (head);
18775 if (i >= 0)
18776 {
18777 j = i;
18778 BUILD_GLYPH_STRINGS (j, start, h, t,
18779 DRAW_NORMAL_TEXT, dummy_x, last_x);
18780 start = i;
18781 compute_overhangs_and_x (t, head->x, 1);
18782 prepend_glyph_string_lists (&head, &tail, h, t);
18783 clip_head = head;
18784 }
18785
18786 /* Prepend glyph strings for glyphs in front of the first glyph
18787 string that overwrite that glyph string because of their
18788 right overhang. For these strings, only the foreground must
18789 be drawn, because it draws over the glyph string at `head'.
18790 The background must not be drawn because this would overwrite
18791 right overhangs of preceding glyphs for which no glyph
18792 strings exist. */
18793 i = left_overwriting (head);
18794 if (i >= 0)
18795 {
18796 clip_head = head;
18797 BUILD_GLYPH_STRINGS (i, start, h, t,
18798 DRAW_NORMAL_TEXT, dummy_x, last_x);
18799 for (s = h; s; s = s->next)
18800 s->background_filled_p = 1;
18801 compute_overhangs_and_x (t, head->x, 1);
18802 prepend_glyph_string_lists (&head, &tail, h, t);
18803 }
18804
18805 /* Append glyphs strings for glyphs following the last glyph
18806 string tail that are overwritten by tail. The background of
18807 these strings has to be drawn because tail's foreground draws
18808 over it. */
18809 i = right_overwritten (tail);
18810 if (i >= 0)
18811 {
18812 BUILD_GLYPH_STRINGS (end, i, h, t,
18813 DRAW_NORMAL_TEXT, x, last_x);
18814 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18815 append_glyph_string_lists (&head, &tail, h, t);
18816 clip_tail = tail;
18817 }
18818
18819 /* Append glyph strings for glyphs following the last glyph
18820 string tail that overwrite tail. The foreground of such
18821 glyphs has to be drawn because it writes into the background
18822 of tail. The background must not be drawn because it could
18823 paint over the foreground of following glyphs. */
18824 i = right_overwriting (tail);
18825 if (i >= 0)
18826 {
18827 clip_tail = tail;
18828 BUILD_GLYPH_STRINGS (end, i, h, t,
18829 DRAW_NORMAL_TEXT, x, last_x);
18830 for (s = h; s; s = s->next)
18831 s->background_filled_p = 1;
18832 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18833 append_glyph_string_lists (&head, &tail, h, t);
18834 }
18835 if (clip_head || clip_tail)
18836 for (s = head; s; s = s->next)
18837 {
18838 s->clip_head = clip_head;
18839 s->clip_tail = clip_tail;
18840 }
18841 }
18842
18843 /* Draw all strings. */
18844 for (s = head; s; s = s->next)
18845 rif->draw_glyph_string (s);
18846
18847 if (area == TEXT_AREA
18848 && !row->full_width_p
18849 /* When drawing overlapping rows, only the glyph strings'
18850 foreground is drawn, which doesn't erase a cursor
18851 completely. */
18852 && !overlaps_p)
18853 {
18854 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18855 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18856 : (tail ? tail->x + tail->background_width : x));
18857
18858 int text_left = window_box_left (w, TEXT_AREA);
18859 x0 -= text_left;
18860 x1 -= text_left;
18861
18862 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18863 row->y, MATRIX_ROW_BOTTOM_Y (row));
18864 }
18865
18866 /* Value is the x-position up to which drawn, relative to AREA of W.
18867 This doesn't include parts drawn because of overhangs. */
18868 if (row->full_width_p)
18869 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18870 else
18871 x_reached -= window_box_left (w, area);
18872
18873 RELEASE_HDC (hdc, f);
18874
18875 return x_reached;
18876 }
18877
18878 /* Expand row matrix if too narrow. Don't expand if area
18879 is not present. */
18880
18881 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18882 { \
18883 if (!fonts_changed_p \
18884 && (it->glyph_row->glyphs[area] \
18885 < it->glyph_row->glyphs[area + 1])) \
18886 { \
18887 it->w->ncols_scale_factor++; \
18888 fonts_changed_p = 1; \
18889 } \
18890 }
18891
18892 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18893 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18894
18895 static INLINE void
18896 append_glyph (it)
18897 struct it *it;
18898 {
18899 struct glyph *glyph;
18900 enum glyph_row_area area = it->area;
18901
18902 xassert (it->glyph_row);
18903 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18904
18905 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18906 if (glyph < it->glyph_row->glyphs[area + 1])
18907 {
18908 glyph->charpos = CHARPOS (it->position);
18909 glyph->object = it->object;
18910 glyph->pixel_width = it->pixel_width;
18911 glyph->ascent = it->ascent;
18912 glyph->descent = it->descent;
18913 glyph->voffset = it->voffset;
18914 glyph->type = CHAR_GLYPH;
18915 glyph->multibyte_p = it->multibyte_p;
18916 glyph->left_box_line_p = it->start_of_box_run_p;
18917 glyph->right_box_line_p = it->end_of_box_run_p;
18918 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18919 || it->phys_descent > it->descent);
18920 glyph->padding_p = 0;
18921 glyph->glyph_not_available_p = it->glyph_not_available_p;
18922 glyph->face_id = it->face_id;
18923 glyph->u.ch = it->char_to_display;
18924 glyph->slice = null_glyph_slice;
18925 glyph->font_type = FONT_TYPE_UNKNOWN;
18926 ++it->glyph_row->used[area];
18927 }
18928 else
18929 IT_EXPAND_MATRIX_WIDTH (it, area);
18930 }
18931
18932 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18933 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18934
18935 static INLINE void
18936 append_composite_glyph (it)
18937 struct it *it;
18938 {
18939 struct glyph *glyph;
18940 enum glyph_row_area area = it->area;
18941
18942 xassert (it->glyph_row);
18943
18944 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18945 if (glyph < it->glyph_row->glyphs[area + 1])
18946 {
18947 glyph->charpos = CHARPOS (it->position);
18948 glyph->object = it->object;
18949 glyph->pixel_width = it->pixel_width;
18950 glyph->ascent = it->ascent;
18951 glyph->descent = it->descent;
18952 glyph->voffset = it->voffset;
18953 glyph->type = COMPOSITE_GLYPH;
18954 glyph->multibyte_p = it->multibyte_p;
18955 glyph->left_box_line_p = it->start_of_box_run_p;
18956 glyph->right_box_line_p = it->end_of_box_run_p;
18957 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18958 || it->phys_descent > it->descent);
18959 glyph->padding_p = 0;
18960 glyph->glyph_not_available_p = 0;
18961 glyph->face_id = it->face_id;
18962 glyph->u.cmp_id = it->cmp_id;
18963 glyph->slice = null_glyph_slice;
18964 glyph->font_type = FONT_TYPE_UNKNOWN;
18965 ++it->glyph_row->used[area];
18966 }
18967 else
18968 IT_EXPAND_MATRIX_WIDTH (it, area);
18969 }
18970
18971
18972 /* Change IT->ascent and IT->height according to the setting of
18973 IT->voffset. */
18974
18975 static INLINE void
18976 take_vertical_position_into_account (it)
18977 struct it *it;
18978 {
18979 if (it->voffset)
18980 {
18981 if (it->voffset < 0)
18982 /* Increase the ascent so that we can display the text higher
18983 in the line. */
18984 it->ascent -= it->voffset;
18985 else
18986 /* Increase the descent so that we can display the text lower
18987 in the line. */
18988 it->descent += it->voffset;
18989 }
18990 }
18991
18992
18993 /* Produce glyphs/get display metrics for the image IT is loaded with.
18994 See the description of struct display_iterator in dispextern.h for
18995 an overview of struct display_iterator. */
18996
18997 static void
18998 produce_image_glyph (it)
18999 struct it *it;
19000 {
19001 struct image *img;
19002 struct face *face;
19003 int glyph_ascent;
19004 struct glyph_slice slice;
19005
19006 xassert (it->what == IT_IMAGE);
19007
19008 face = FACE_FROM_ID (it->f, it->face_id);
19009 xassert (face);
19010 /* Make sure X resources of the face is loaded. */
19011 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19012
19013 if (it->image_id < 0)
19014 {
19015 /* Fringe bitmap. */
19016 it->ascent = it->phys_ascent = 0;
19017 it->descent = it->phys_descent = 0;
19018 it->pixel_width = 0;
19019 it->nglyphs = 0;
19020 return;
19021 }
19022
19023 img = IMAGE_FROM_ID (it->f, it->image_id);
19024 xassert (img);
19025 /* Make sure X resources of the image is loaded. */
19026 prepare_image_for_display (it->f, img);
19027
19028 slice.x = slice.y = 0;
19029 slice.width = img->width;
19030 slice.height = img->height;
19031
19032 if (INTEGERP (it->slice.x))
19033 slice.x = XINT (it->slice.x);
19034 else if (FLOATP (it->slice.x))
19035 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19036
19037 if (INTEGERP (it->slice.y))
19038 slice.y = XINT (it->slice.y);
19039 else if (FLOATP (it->slice.y))
19040 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19041
19042 if (INTEGERP (it->slice.width))
19043 slice.width = XINT (it->slice.width);
19044 else if (FLOATP (it->slice.width))
19045 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19046
19047 if (INTEGERP (it->slice.height))
19048 slice.height = XINT (it->slice.height);
19049 else if (FLOATP (it->slice.height))
19050 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19051
19052 if (slice.x >= img->width)
19053 slice.x = img->width;
19054 if (slice.y >= img->height)
19055 slice.y = img->height;
19056 if (slice.x + slice.width >= img->width)
19057 slice.width = img->width - slice.x;
19058 if (slice.y + slice.height > img->height)
19059 slice.height = img->height - slice.y;
19060
19061 if (slice.width == 0 || slice.height == 0)
19062 return;
19063
19064 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19065
19066 it->descent = slice.height - glyph_ascent;
19067 if (slice.y == 0)
19068 it->descent += img->vmargin;
19069 if (slice.y + slice.height == img->height)
19070 it->descent += img->vmargin;
19071 it->phys_descent = it->descent;
19072
19073 it->pixel_width = slice.width;
19074 if (slice.x == 0)
19075 it->pixel_width += img->hmargin;
19076 if (slice.x + slice.width == img->width)
19077 it->pixel_width += img->hmargin;
19078
19079 /* It's quite possible for images to have an ascent greater than
19080 their height, so don't get confused in that case. */
19081 if (it->descent < 0)
19082 it->descent = 0;
19083
19084 #if 0 /* this breaks image tiling */
19085 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19086 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19087 if (face_ascent > it->ascent)
19088 it->ascent = it->phys_ascent = face_ascent;
19089 #endif
19090
19091 it->nglyphs = 1;
19092
19093 if (face->box != FACE_NO_BOX)
19094 {
19095 if (face->box_line_width > 0)
19096 {
19097 if (slice.y == 0)
19098 it->ascent += face->box_line_width;
19099 if (slice.y + slice.height == img->height)
19100 it->descent += face->box_line_width;
19101 }
19102
19103 if (it->start_of_box_run_p && slice.x == 0)
19104 it->pixel_width += abs (face->box_line_width);
19105 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19106 it->pixel_width += abs (face->box_line_width);
19107 }
19108
19109 take_vertical_position_into_account (it);
19110
19111 if (it->glyph_row)
19112 {
19113 struct glyph *glyph;
19114 enum glyph_row_area area = it->area;
19115
19116 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19117 if (glyph < it->glyph_row->glyphs[area + 1])
19118 {
19119 glyph->charpos = CHARPOS (it->position);
19120 glyph->object = it->object;
19121 glyph->pixel_width = it->pixel_width;
19122 glyph->ascent = glyph_ascent;
19123 glyph->descent = it->descent;
19124 glyph->voffset = it->voffset;
19125 glyph->type = IMAGE_GLYPH;
19126 glyph->multibyte_p = it->multibyte_p;
19127 glyph->left_box_line_p = it->start_of_box_run_p;
19128 glyph->right_box_line_p = it->end_of_box_run_p;
19129 glyph->overlaps_vertically_p = 0;
19130 glyph->padding_p = 0;
19131 glyph->glyph_not_available_p = 0;
19132 glyph->face_id = it->face_id;
19133 glyph->u.img_id = img->id;
19134 glyph->slice = slice;
19135 glyph->font_type = FONT_TYPE_UNKNOWN;
19136 ++it->glyph_row->used[area];
19137 }
19138 else
19139 IT_EXPAND_MATRIX_WIDTH (it, area);
19140 }
19141 }
19142
19143
19144 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19145 of the glyph, WIDTH and HEIGHT are the width and height of the
19146 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19147
19148 static void
19149 append_stretch_glyph (it, object, width, height, ascent)
19150 struct it *it;
19151 Lisp_Object object;
19152 int width, height;
19153 int ascent;
19154 {
19155 struct glyph *glyph;
19156 enum glyph_row_area area = it->area;
19157
19158 xassert (ascent >= 0 && ascent <= height);
19159
19160 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19161 if (glyph < it->glyph_row->glyphs[area + 1])
19162 {
19163 glyph->charpos = CHARPOS (it->position);
19164 glyph->object = object;
19165 glyph->pixel_width = width;
19166 glyph->ascent = ascent;
19167 glyph->descent = height - ascent;
19168 glyph->voffset = it->voffset;
19169 glyph->type = STRETCH_GLYPH;
19170 glyph->multibyte_p = it->multibyte_p;
19171 glyph->left_box_line_p = it->start_of_box_run_p;
19172 glyph->right_box_line_p = it->end_of_box_run_p;
19173 glyph->overlaps_vertically_p = 0;
19174 glyph->padding_p = 0;
19175 glyph->glyph_not_available_p = 0;
19176 glyph->face_id = it->face_id;
19177 glyph->u.stretch.ascent = ascent;
19178 glyph->u.stretch.height = height;
19179 glyph->slice = null_glyph_slice;
19180 glyph->font_type = FONT_TYPE_UNKNOWN;
19181 ++it->glyph_row->used[area];
19182 }
19183 else
19184 IT_EXPAND_MATRIX_WIDTH (it, area);
19185 }
19186
19187
19188 /* Produce a stretch glyph for iterator IT. IT->object is the value
19189 of the glyph property displayed. The value must be a list
19190 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19191 being recognized:
19192
19193 1. `:width WIDTH' specifies that the space should be WIDTH *
19194 canonical char width wide. WIDTH may be an integer or floating
19195 point number.
19196
19197 2. `:relative-width FACTOR' specifies that the width of the stretch
19198 should be computed from the width of the first character having the
19199 `glyph' property, and should be FACTOR times that width.
19200
19201 3. `:align-to HPOS' specifies that the space should be wide enough
19202 to reach HPOS, a value in canonical character units.
19203
19204 Exactly one of the above pairs must be present.
19205
19206 4. `:height HEIGHT' specifies that the height of the stretch produced
19207 should be HEIGHT, measured in canonical character units.
19208
19209 5. `:relative-height FACTOR' specifies that the height of the
19210 stretch should be FACTOR times the height of the characters having
19211 the glyph property.
19212
19213 Either none or exactly one of 4 or 5 must be present.
19214
19215 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19216 of the stretch should be used for the ascent of the stretch.
19217 ASCENT must be in the range 0 <= ASCENT <= 100. */
19218
19219 static void
19220 produce_stretch_glyph (it)
19221 struct it *it;
19222 {
19223 /* (space :width WIDTH :height HEIGHT ...) */
19224 Lisp_Object prop, plist;
19225 int width = 0, height = 0, align_to = -1;
19226 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19227 int ascent = 0;
19228 double tem;
19229 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19230 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19231
19232 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19233
19234 /* List should start with `space'. */
19235 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19236 plist = XCDR (it->object);
19237
19238 /* Compute the width of the stretch. */
19239 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19240 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19241 {
19242 /* Absolute width `:width WIDTH' specified and valid. */
19243 zero_width_ok_p = 1;
19244 width = (int)tem;
19245 }
19246 else if (prop = Fplist_get (plist, QCrelative_width),
19247 NUMVAL (prop) > 0)
19248 {
19249 /* Relative width `:relative-width FACTOR' specified and valid.
19250 Compute the width of the characters having the `glyph'
19251 property. */
19252 struct it it2;
19253 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19254
19255 it2 = *it;
19256 if (it->multibyte_p)
19257 {
19258 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19259 - IT_BYTEPOS (*it));
19260 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19261 }
19262 else
19263 it2.c = *p, it2.len = 1;
19264
19265 it2.glyph_row = NULL;
19266 it2.what = IT_CHARACTER;
19267 x_produce_glyphs (&it2);
19268 width = NUMVAL (prop) * it2.pixel_width;
19269 }
19270 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19271 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19272 {
19273 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19274 align_to = (align_to < 0
19275 ? 0
19276 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19277 else if (align_to < 0)
19278 align_to = window_box_left_offset (it->w, TEXT_AREA);
19279 width = max (0, (int)tem + align_to - it->current_x);
19280 zero_width_ok_p = 1;
19281 }
19282 else
19283 /* Nothing specified -> width defaults to canonical char width. */
19284 width = FRAME_COLUMN_WIDTH (it->f);
19285
19286 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19287 width = 1;
19288
19289 /* Compute height. */
19290 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19291 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19292 {
19293 height = (int)tem;
19294 zero_height_ok_p = 1;
19295 }
19296 else if (prop = Fplist_get (plist, QCrelative_height),
19297 NUMVAL (prop) > 0)
19298 height = FONT_HEIGHT (font) * NUMVAL (prop);
19299 else
19300 height = FONT_HEIGHT (font);
19301
19302 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19303 height = 1;
19304
19305 /* Compute percentage of height used for ascent. If
19306 `:ascent ASCENT' is present and valid, use that. Otherwise,
19307 derive the ascent from the font in use. */
19308 if (prop = Fplist_get (plist, QCascent),
19309 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19310 ascent = height * NUMVAL (prop) / 100.0;
19311 else if (!NILP (prop)
19312 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19313 ascent = min (max (0, (int)tem), height);
19314 else
19315 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19316
19317 if (width > 0 && height > 0 && it->glyph_row)
19318 {
19319 Lisp_Object object = it->stack[it->sp - 1].string;
19320 if (!STRINGP (object))
19321 object = it->w->buffer;
19322 append_stretch_glyph (it, object, width, height, ascent);
19323 }
19324
19325 it->pixel_width = width;
19326 it->ascent = it->phys_ascent = ascent;
19327 it->descent = it->phys_descent = height - it->ascent;
19328 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19329
19330 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19331 {
19332 if (face->box_line_width > 0)
19333 {
19334 it->ascent += face->box_line_width;
19335 it->descent += face->box_line_width;
19336 }
19337
19338 if (it->start_of_box_run_p)
19339 it->pixel_width += abs (face->box_line_width);
19340 if (it->end_of_box_run_p)
19341 it->pixel_width += abs (face->box_line_width);
19342 }
19343
19344 take_vertical_position_into_account (it);
19345 }
19346
19347 /* Get line-height and line-spacing property at point.
19348 If line-height has format (HEIGHT TOTAL), return TOTAL
19349 in TOTAL_HEIGHT. */
19350
19351 static Lisp_Object
19352 get_line_height_property (it, prop)
19353 struct it *it;
19354 Lisp_Object prop;
19355 {
19356 Lisp_Object position;
19357
19358 if (STRINGP (it->object))
19359 position = make_number (IT_STRING_CHARPOS (*it));
19360 else if (BUFFERP (it->object))
19361 position = make_number (IT_CHARPOS (*it));
19362 else
19363 return Qnil;
19364
19365 return Fget_char_property (position, prop, it->object);
19366 }
19367
19368 /* Calculate line-height and line-spacing properties.
19369 An integer value specifies explicit pixel value.
19370 A float value specifies relative value to current face height.
19371 A cons (float . face-name) specifies relative value to
19372 height of specified face font.
19373
19374 Returns height in pixels, or nil. */
19375
19376
19377 static Lisp_Object
19378 calc_line_height_property (it, val, font, boff, override)
19379 struct it *it;
19380 Lisp_Object val;
19381 XFontStruct *font;
19382 int boff, override;
19383 {
19384 Lisp_Object face_name = Qnil;
19385 int ascent, descent, height;
19386
19387 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19388 return val;
19389
19390 if (CONSP (val))
19391 {
19392 face_name = XCAR (val);
19393 val = XCDR (val);
19394 if (!NUMBERP (val))
19395 val = make_number (1);
19396 if (NILP (face_name))
19397 {
19398 height = it->ascent + it->descent;
19399 goto scale;
19400 }
19401 }
19402
19403 if (NILP (face_name))
19404 {
19405 font = FRAME_FONT (it->f);
19406 boff = FRAME_BASELINE_OFFSET (it->f);
19407 }
19408 else if (EQ (face_name, Qt))
19409 {
19410 override = 0;
19411 }
19412 else
19413 {
19414 int face_id;
19415 struct face *face;
19416 struct font_info *font_info;
19417
19418 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19419 if (face_id < 0)
19420 return make_number (-1);
19421
19422 face = FACE_FROM_ID (it->f, face_id);
19423 font = face->font;
19424 if (font == NULL)
19425 return make_number (-1);
19426
19427 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19428 boff = font_info->baseline_offset;
19429 if (font_info->vertical_centering)
19430 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19431 }
19432
19433 ascent = FONT_BASE (font) + boff;
19434 descent = FONT_DESCENT (font) - boff;
19435
19436 if (override)
19437 {
19438 it->override_ascent = ascent;
19439 it->override_descent = descent;
19440 it->override_boff = boff;
19441 }
19442
19443 height = ascent + descent;
19444
19445 scale:
19446 if (FLOATP (val))
19447 height = (int)(XFLOAT_DATA (val) * height);
19448 else if (INTEGERP (val))
19449 height *= XINT (val);
19450
19451 return make_number (height);
19452 }
19453
19454
19455 /* RIF:
19456 Produce glyphs/get display metrics for the display element IT is
19457 loaded with. See the description of struct display_iterator in
19458 dispextern.h for an overview of struct display_iterator. */
19459
19460 void
19461 x_produce_glyphs (it)
19462 struct it *it;
19463 {
19464 int extra_line_spacing = it->extra_line_spacing;
19465
19466 it->glyph_not_available_p = 0;
19467
19468 if (it->what == IT_CHARACTER)
19469 {
19470 XChar2b char2b;
19471 XFontStruct *font;
19472 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19473 XCharStruct *pcm;
19474 int font_not_found_p;
19475 struct font_info *font_info;
19476 int boff; /* baseline offset */
19477 /* We may change it->multibyte_p upon unibyte<->multibyte
19478 conversion. So, save the current value now and restore it
19479 later.
19480
19481 Note: It seems that we don't have to record multibyte_p in
19482 struct glyph because the character code itself tells if or
19483 not the character is multibyte. Thus, in the future, we must
19484 consider eliminating the field `multibyte_p' in the struct
19485 glyph. */
19486 int saved_multibyte_p = it->multibyte_p;
19487
19488 /* Maybe translate single-byte characters to multibyte, or the
19489 other way. */
19490 it->char_to_display = it->c;
19491 if (!ASCII_BYTE_P (it->c))
19492 {
19493 if (unibyte_display_via_language_environment
19494 && SINGLE_BYTE_CHAR_P (it->c)
19495 && (it->c >= 0240
19496 || !NILP (Vnonascii_translation_table)))
19497 {
19498 it->char_to_display = unibyte_char_to_multibyte (it->c);
19499 it->multibyte_p = 1;
19500 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19501 face = FACE_FROM_ID (it->f, it->face_id);
19502 }
19503 else if (!SINGLE_BYTE_CHAR_P (it->c)
19504 && !it->multibyte_p)
19505 {
19506 it->multibyte_p = 1;
19507 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19508 face = FACE_FROM_ID (it->f, it->face_id);
19509 }
19510 }
19511
19512 /* Get font to use. Encode IT->char_to_display. */
19513 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19514 &char2b, it->multibyte_p, 0);
19515 font = face->font;
19516
19517 /* When no suitable font found, use the default font. */
19518 font_not_found_p = font == NULL;
19519 if (font_not_found_p)
19520 {
19521 font = FRAME_FONT (it->f);
19522 boff = FRAME_BASELINE_OFFSET (it->f);
19523 font_info = NULL;
19524 }
19525 else
19526 {
19527 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19528 boff = font_info->baseline_offset;
19529 if (font_info->vertical_centering)
19530 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19531 }
19532
19533 if (it->char_to_display >= ' '
19534 && (!it->multibyte_p || it->char_to_display < 128))
19535 {
19536 /* Either unibyte or ASCII. */
19537 int stretched_p;
19538
19539 it->nglyphs = 1;
19540
19541 pcm = rif->per_char_metric (font, &char2b,
19542 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19543
19544 if (it->override_ascent >= 0)
19545 {
19546 it->ascent = it->override_ascent;
19547 it->descent = it->override_descent;
19548 boff = it->override_boff;
19549 }
19550 else
19551 {
19552 it->ascent = FONT_BASE (font) + boff;
19553 it->descent = FONT_DESCENT (font) - boff;
19554 }
19555
19556 if (pcm)
19557 {
19558 it->phys_ascent = pcm->ascent + boff;
19559 it->phys_descent = pcm->descent - boff;
19560 it->pixel_width = pcm->width;
19561 }
19562 else
19563 {
19564 it->glyph_not_available_p = 1;
19565 it->phys_ascent = it->ascent;
19566 it->phys_descent = it->descent;
19567 it->pixel_width = FONT_WIDTH (font);
19568 }
19569
19570 if (it->constrain_row_ascent_descent_p)
19571 {
19572 if (it->descent > it->max_descent)
19573 {
19574 it->ascent += it->descent - it->max_descent;
19575 it->descent = it->max_descent;
19576 }
19577 if (it->ascent > it->max_ascent)
19578 {
19579 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19580 it->ascent = it->max_ascent;
19581 }
19582 it->phys_ascent = min (it->phys_ascent, it->ascent);
19583 it->phys_descent = min (it->phys_descent, it->descent);
19584 extra_line_spacing = 0;
19585 }
19586
19587 /* If this is a space inside a region of text with
19588 `space-width' property, change its width. */
19589 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19590 if (stretched_p)
19591 it->pixel_width *= XFLOATINT (it->space_width);
19592
19593 /* If face has a box, add the box thickness to the character
19594 height. If character has a box line to the left and/or
19595 right, add the box line width to the character's width. */
19596 if (face->box != FACE_NO_BOX)
19597 {
19598 int thick = face->box_line_width;
19599
19600 if (thick > 0)
19601 {
19602 it->ascent += thick;
19603 it->descent += thick;
19604 }
19605 else
19606 thick = -thick;
19607
19608 if (it->start_of_box_run_p)
19609 it->pixel_width += thick;
19610 if (it->end_of_box_run_p)
19611 it->pixel_width += thick;
19612 }
19613
19614 /* If face has an overline, add the height of the overline
19615 (1 pixel) and a 1 pixel margin to the character height. */
19616 if (face->overline_p)
19617 it->ascent += 2;
19618
19619 if (it->constrain_row_ascent_descent_p)
19620 {
19621 if (it->ascent > it->max_ascent)
19622 it->ascent = it->max_ascent;
19623 if (it->descent > it->max_descent)
19624 it->descent = it->max_descent;
19625 }
19626
19627 take_vertical_position_into_account (it);
19628
19629 /* If we have to actually produce glyphs, do it. */
19630 if (it->glyph_row)
19631 {
19632 if (stretched_p)
19633 {
19634 /* Translate a space with a `space-width' property
19635 into a stretch glyph. */
19636 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19637 / FONT_HEIGHT (font));
19638 append_stretch_glyph (it, it->object, it->pixel_width,
19639 it->ascent + it->descent, ascent);
19640 }
19641 else
19642 append_glyph (it);
19643
19644 /* If characters with lbearing or rbearing are displayed
19645 in this line, record that fact in a flag of the
19646 glyph row. This is used to optimize X output code. */
19647 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19648 it->glyph_row->contains_overlapping_glyphs_p = 1;
19649 }
19650 }
19651 else if (it->char_to_display == '\n')
19652 {
19653 /* A newline has no width but we need the height of the line.
19654 But if previous part of the line set a height, don't
19655 increase that height */
19656
19657 Lisp_Object height;
19658 Lisp_Object total_height = Qnil;
19659
19660 it->override_ascent = -1;
19661 it->pixel_width = 0;
19662 it->nglyphs = 0;
19663
19664 height = get_line_height_property(it, Qline_height);
19665 /* Split (line-height total-height) list */
19666 if (CONSP (height)
19667 && CONSP (XCDR (height))
19668 && NILP (XCDR (XCDR (height))))
19669 {
19670 total_height = XCAR (XCDR (height));
19671 height = XCAR (height);
19672 }
19673 height = calc_line_height_property(it, height, font, boff, 1);
19674
19675 if (it->override_ascent >= 0)
19676 {
19677 it->ascent = it->override_ascent;
19678 it->descent = it->override_descent;
19679 boff = it->override_boff;
19680 }
19681 else
19682 {
19683 it->ascent = FONT_BASE (font) + boff;
19684 it->descent = FONT_DESCENT (font) - boff;
19685 }
19686
19687 if (EQ (height, Qt))
19688 {
19689 if (it->descent > it->max_descent)
19690 {
19691 it->ascent += it->descent - it->max_descent;
19692 it->descent = it->max_descent;
19693 }
19694 if (it->ascent > it->max_ascent)
19695 {
19696 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19697 it->ascent = it->max_ascent;
19698 }
19699 it->phys_ascent = min (it->phys_ascent, it->ascent);
19700 it->phys_descent = min (it->phys_descent, it->descent);
19701 it->constrain_row_ascent_descent_p = 1;
19702 extra_line_spacing = 0;
19703 }
19704 else
19705 {
19706 Lisp_Object spacing;
19707
19708 it->phys_ascent = it->ascent;
19709 it->phys_descent = it->descent;
19710
19711 if ((it->max_ascent > 0 || it->max_descent > 0)
19712 && face->box != FACE_NO_BOX
19713 && face->box_line_width > 0)
19714 {
19715 it->ascent += face->box_line_width;
19716 it->descent += face->box_line_width;
19717 }
19718 if (!NILP (height)
19719 && XINT (height) > it->ascent + it->descent)
19720 it->ascent = XINT (height) - it->descent;
19721
19722 if (!NILP (total_height))
19723 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19724 else
19725 {
19726 spacing = get_line_height_property(it, Qline_spacing);
19727 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19728 }
19729 if (INTEGERP (spacing))
19730 {
19731 extra_line_spacing = XINT (spacing);
19732 if (!NILP (total_height))
19733 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19734 }
19735 }
19736 }
19737 else if (it->char_to_display == '\t')
19738 {
19739 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19740 int x = it->current_x + it->continuation_lines_width;
19741 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19742
19743 /* If the distance from the current position to the next tab
19744 stop is less than a space character width, use the
19745 tab stop after that. */
19746 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19747 next_tab_x += tab_width;
19748
19749 it->pixel_width = next_tab_x - x;
19750 it->nglyphs = 1;
19751 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19752 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19753
19754 if (it->glyph_row)
19755 {
19756 append_stretch_glyph (it, it->object, it->pixel_width,
19757 it->ascent + it->descent, it->ascent);
19758 }
19759 }
19760 else
19761 {
19762 /* A multi-byte character. Assume that the display width of the
19763 character is the width of the character multiplied by the
19764 width of the font. */
19765
19766 /* If we found a font, this font should give us the right
19767 metrics. If we didn't find a font, use the frame's
19768 default font and calculate the width of the character
19769 from the charset width; this is what old redisplay code
19770 did. */
19771
19772 pcm = rif->per_char_metric (font, &char2b,
19773 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19774
19775 if (font_not_found_p || !pcm)
19776 {
19777 int charset = CHAR_CHARSET (it->char_to_display);
19778
19779 it->glyph_not_available_p = 1;
19780 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19781 * CHARSET_WIDTH (charset));
19782 it->phys_ascent = FONT_BASE (font) + boff;
19783 it->phys_descent = FONT_DESCENT (font) - boff;
19784 }
19785 else
19786 {
19787 it->pixel_width = pcm->width;
19788 it->phys_ascent = pcm->ascent + boff;
19789 it->phys_descent = pcm->descent - boff;
19790 if (it->glyph_row
19791 && (pcm->lbearing < 0
19792 || pcm->rbearing > pcm->width))
19793 it->glyph_row->contains_overlapping_glyphs_p = 1;
19794 }
19795 it->nglyphs = 1;
19796 it->ascent = FONT_BASE (font) + boff;
19797 it->descent = FONT_DESCENT (font) - boff;
19798 if (face->box != FACE_NO_BOX)
19799 {
19800 int thick = face->box_line_width;
19801
19802 if (thick > 0)
19803 {
19804 it->ascent += thick;
19805 it->descent += thick;
19806 }
19807 else
19808 thick = - thick;
19809
19810 if (it->start_of_box_run_p)
19811 it->pixel_width += thick;
19812 if (it->end_of_box_run_p)
19813 it->pixel_width += thick;
19814 }
19815
19816 /* If face has an overline, add the height of the overline
19817 (1 pixel) and a 1 pixel margin to the character height. */
19818 if (face->overline_p)
19819 it->ascent += 2;
19820
19821 take_vertical_position_into_account (it);
19822
19823 if (it->glyph_row)
19824 append_glyph (it);
19825 }
19826 it->multibyte_p = saved_multibyte_p;
19827 }
19828 else if (it->what == IT_COMPOSITION)
19829 {
19830 /* Note: A composition is represented as one glyph in the
19831 glyph matrix. There are no padding glyphs. */
19832 XChar2b char2b;
19833 XFontStruct *font;
19834 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19835 XCharStruct *pcm;
19836 int font_not_found_p;
19837 struct font_info *font_info;
19838 int boff; /* baseline offset */
19839 struct composition *cmp = composition_table[it->cmp_id];
19840
19841 /* Maybe translate single-byte characters to multibyte. */
19842 it->char_to_display = it->c;
19843 if (unibyte_display_via_language_environment
19844 && SINGLE_BYTE_CHAR_P (it->c)
19845 && (it->c >= 0240
19846 || (it->c >= 0200
19847 && !NILP (Vnonascii_translation_table))))
19848 {
19849 it->char_to_display = unibyte_char_to_multibyte (it->c);
19850 }
19851
19852 /* Get face and font to use. Encode IT->char_to_display. */
19853 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19854 face = FACE_FROM_ID (it->f, it->face_id);
19855 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19856 &char2b, it->multibyte_p, 0);
19857 font = face->font;
19858
19859 /* When no suitable font found, use the default font. */
19860 font_not_found_p = font == NULL;
19861 if (font_not_found_p)
19862 {
19863 font = FRAME_FONT (it->f);
19864 boff = FRAME_BASELINE_OFFSET (it->f);
19865 font_info = NULL;
19866 }
19867 else
19868 {
19869 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19870 boff = font_info->baseline_offset;
19871 if (font_info->vertical_centering)
19872 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19873 }
19874
19875 /* There are no padding glyphs, so there is only one glyph to
19876 produce for the composition. Important is that pixel_width,
19877 ascent and descent are the values of what is drawn by
19878 draw_glyphs (i.e. the values of the overall glyphs composed). */
19879 it->nglyphs = 1;
19880
19881 /* If we have not yet calculated pixel size data of glyphs of
19882 the composition for the current face font, calculate them
19883 now. Theoretically, we have to check all fonts for the
19884 glyphs, but that requires much time and memory space. So,
19885 here we check only the font of the first glyph. This leads
19886 to incorrect display very rarely, and C-l (recenter) can
19887 correct the display anyway. */
19888 if (cmp->font != (void *) font)
19889 {
19890 /* Ascent and descent of the font of the first character of
19891 this composition (adjusted by baseline offset). Ascent
19892 and descent of overall glyphs should not be less than
19893 them respectively. */
19894 int font_ascent = FONT_BASE (font) + boff;
19895 int font_descent = FONT_DESCENT (font) - boff;
19896 /* Bounding box of the overall glyphs. */
19897 int leftmost, rightmost, lowest, highest;
19898 int i, width, ascent, descent;
19899
19900 cmp->font = (void *) font;
19901
19902 /* Initialize the bounding box. */
19903 if (font_info
19904 && (pcm = rif->per_char_metric (font, &char2b,
19905 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19906 {
19907 width = pcm->width;
19908 ascent = pcm->ascent;
19909 descent = pcm->descent;
19910 }
19911 else
19912 {
19913 width = FONT_WIDTH (font);
19914 ascent = FONT_BASE (font);
19915 descent = FONT_DESCENT (font);
19916 }
19917
19918 rightmost = width;
19919 lowest = - descent + boff;
19920 highest = ascent + boff;
19921 leftmost = 0;
19922
19923 if (font_info
19924 && font_info->default_ascent
19925 && CHAR_TABLE_P (Vuse_default_ascent)
19926 && !NILP (Faref (Vuse_default_ascent,
19927 make_number (it->char_to_display))))
19928 highest = font_info->default_ascent + boff;
19929
19930 /* Draw the first glyph at the normal position. It may be
19931 shifted to right later if some other glyphs are drawn at
19932 the left. */
19933 cmp->offsets[0] = 0;
19934 cmp->offsets[1] = boff;
19935
19936 /* Set cmp->offsets for the remaining glyphs. */
19937 for (i = 1; i < cmp->glyph_len; i++)
19938 {
19939 int left, right, btm, top;
19940 int ch = COMPOSITION_GLYPH (cmp, i);
19941 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19942
19943 face = FACE_FROM_ID (it->f, face_id);
19944 get_char_face_and_encoding (it->f, ch, face->id,
19945 &char2b, it->multibyte_p, 0);
19946 font = face->font;
19947 if (font == NULL)
19948 {
19949 font = FRAME_FONT (it->f);
19950 boff = FRAME_BASELINE_OFFSET (it->f);
19951 font_info = NULL;
19952 }
19953 else
19954 {
19955 font_info
19956 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19957 boff = font_info->baseline_offset;
19958 if (font_info->vertical_centering)
19959 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19960 }
19961
19962 if (font_info
19963 && (pcm = rif->per_char_metric (font, &char2b,
19964 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19965 {
19966 width = pcm->width;
19967 ascent = pcm->ascent;
19968 descent = pcm->descent;
19969 }
19970 else
19971 {
19972 width = FONT_WIDTH (font);
19973 ascent = 1;
19974 descent = 0;
19975 }
19976
19977 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19978 {
19979 /* Relative composition with or without
19980 alternate chars. */
19981 left = (leftmost + rightmost - width) / 2;
19982 btm = - descent + boff;
19983 if (font_info && font_info->relative_compose
19984 && (! CHAR_TABLE_P (Vignore_relative_composition)
19985 || NILP (Faref (Vignore_relative_composition,
19986 make_number (ch)))))
19987 {
19988
19989 if (- descent >= font_info->relative_compose)
19990 /* One extra pixel between two glyphs. */
19991 btm = highest + 1;
19992 else if (ascent <= 0)
19993 /* One extra pixel between two glyphs. */
19994 btm = lowest - 1 - ascent - descent;
19995 }
19996 }
19997 else
19998 {
19999 /* A composition rule is specified by an integer
20000 value that encodes global and new reference
20001 points (GREF and NREF). GREF and NREF are
20002 specified by numbers as below:
20003
20004 0---1---2 -- ascent
20005 | |
20006 | |
20007 | |
20008 9--10--11 -- center
20009 | |
20010 ---3---4---5--- baseline
20011 | |
20012 6---7---8 -- descent
20013 */
20014 int rule = COMPOSITION_RULE (cmp, i);
20015 int gref, nref, grefx, grefy, nrefx, nrefy;
20016
20017 COMPOSITION_DECODE_RULE (rule, gref, nref);
20018 grefx = gref % 3, nrefx = nref % 3;
20019 grefy = gref / 3, nrefy = nref / 3;
20020
20021 left = (leftmost
20022 + grefx * (rightmost - leftmost) / 2
20023 - nrefx * width / 2);
20024 btm = ((grefy == 0 ? highest
20025 : grefy == 1 ? 0
20026 : grefy == 2 ? lowest
20027 : (highest + lowest) / 2)
20028 - (nrefy == 0 ? ascent + descent
20029 : nrefy == 1 ? descent - boff
20030 : nrefy == 2 ? 0
20031 : (ascent + descent) / 2));
20032 }
20033
20034 cmp->offsets[i * 2] = left;
20035 cmp->offsets[i * 2 + 1] = btm + descent;
20036
20037 /* Update the bounding box of the overall glyphs. */
20038 right = left + width;
20039 top = btm + descent + ascent;
20040 if (left < leftmost)
20041 leftmost = left;
20042 if (right > rightmost)
20043 rightmost = right;
20044 if (top > highest)
20045 highest = top;
20046 if (btm < lowest)
20047 lowest = btm;
20048 }
20049
20050 /* If there are glyphs whose x-offsets are negative,
20051 shift all glyphs to the right and make all x-offsets
20052 non-negative. */
20053 if (leftmost < 0)
20054 {
20055 for (i = 0; i < cmp->glyph_len; i++)
20056 cmp->offsets[i * 2] -= leftmost;
20057 rightmost -= leftmost;
20058 }
20059
20060 cmp->pixel_width = rightmost;
20061 cmp->ascent = highest;
20062 cmp->descent = - lowest;
20063 if (cmp->ascent < font_ascent)
20064 cmp->ascent = font_ascent;
20065 if (cmp->descent < font_descent)
20066 cmp->descent = font_descent;
20067 }
20068
20069 it->pixel_width = cmp->pixel_width;
20070 it->ascent = it->phys_ascent = cmp->ascent;
20071 it->descent = it->phys_descent = cmp->descent;
20072
20073 if (face->box != FACE_NO_BOX)
20074 {
20075 int thick = face->box_line_width;
20076
20077 if (thick > 0)
20078 {
20079 it->ascent += thick;
20080 it->descent += thick;
20081 }
20082 else
20083 thick = - thick;
20084
20085 if (it->start_of_box_run_p)
20086 it->pixel_width += thick;
20087 if (it->end_of_box_run_p)
20088 it->pixel_width += thick;
20089 }
20090
20091 /* If face has an overline, add the height of the overline
20092 (1 pixel) and a 1 pixel margin to the character height. */
20093 if (face->overline_p)
20094 it->ascent += 2;
20095
20096 take_vertical_position_into_account (it);
20097
20098 if (it->glyph_row)
20099 append_composite_glyph (it);
20100 }
20101 else if (it->what == IT_IMAGE)
20102 produce_image_glyph (it);
20103 else if (it->what == IT_STRETCH)
20104 produce_stretch_glyph (it);
20105
20106 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20107 because this isn't true for images with `:ascent 100'. */
20108 xassert (it->ascent >= 0 && it->descent >= 0);
20109 if (it->area == TEXT_AREA)
20110 it->current_x += it->pixel_width;
20111
20112 if (extra_line_spacing > 0)
20113 {
20114 it->descent += extra_line_spacing;
20115 if (extra_line_spacing > it->max_extra_line_spacing)
20116 it->max_extra_line_spacing = extra_line_spacing;
20117 }
20118
20119 it->max_ascent = max (it->max_ascent, it->ascent);
20120 it->max_descent = max (it->max_descent, it->descent);
20121 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20122 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20123 }
20124
20125 /* EXPORT for RIF:
20126 Output LEN glyphs starting at START at the nominal cursor position.
20127 Advance the nominal cursor over the text. The global variable
20128 updated_window contains the window being updated, updated_row is
20129 the glyph row being updated, and updated_area is the area of that
20130 row being updated. */
20131
20132 void
20133 x_write_glyphs (start, len)
20134 struct glyph *start;
20135 int len;
20136 {
20137 int x, hpos;
20138
20139 xassert (updated_window && updated_row);
20140 BLOCK_INPUT;
20141
20142 /* Write glyphs. */
20143
20144 hpos = start - updated_row->glyphs[updated_area];
20145 x = draw_glyphs (updated_window, output_cursor.x,
20146 updated_row, updated_area,
20147 hpos, hpos + len,
20148 DRAW_NORMAL_TEXT, 0);
20149
20150 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20151 if (updated_area == TEXT_AREA
20152 && updated_window->phys_cursor_on_p
20153 && updated_window->phys_cursor.vpos == output_cursor.vpos
20154 && updated_window->phys_cursor.hpos >= hpos
20155 && updated_window->phys_cursor.hpos < hpos + len)
20156 updated_window->phys_cursor_on_p = 0;
20157
20158 UNBLOCK_INPUT;
20159
20160 /* Advance the output cursor. */
20161 output_cursor.hpos += len;
20162 output_cursor.x = x;
20163 }
20164
20165
20166 /* EXPORT for RIF:
20167 Insert LEN glyphs from START at the nominal cursor position. */
20168
20169 void
20170 x_insert_glyphs (start, len)
20171 struct glyph *start;
20172 int len;
20173 {
20174 struct frame *f;
20175 struct window *w;
20176 int line_height, shift_by_width, shifted_region_width;
20177 struct glyph_row *row;
20178 struct glyph *glyph;
20179 int frame_x, frame_y, hpos;
20180
20181 xassert (updated_window && updated_row);
20182 BLOCK_INPUT;
20183 w = updated_window;
20184 f = XFRAME (WINDOW_FRAME (w));
20185
20186 /* Get the height of the line we are in. */
20187 row = updated_row;
20188 line_height = row->height;
20189
20190 /* Get the width of the glyphs to insert. */
20191 shift_by_width = 0;
20192 for (glyph = start; glyph < start + len; ++glyph)
20193 shift_by_width += glyph->pixel_width;
20194
20195 /* Get the width of the region to shift right. */
20196 shifted_region_width = (window_box_width (w, updated_area)
20197 - output_cursor.x
20198 - shift_by_width);
20199
20200 /* Shift right. */
20201 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20202 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20203
20204 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20205 line_height, shift_by_width);
20206
20207 /* Write the glyphs. */
20208 hpos = start - row->glyphs[updated_area];
20209 draw_glyphs (w, output_cursor.x, row, updated_area,
20210 hpos, hpos + len,
20211 DRAW_NORMAL_TEXT, 0);
20212
20213 /* Advance the output cursor. */
20214 output_cursor.hpos += len;
20215 output_cursor.x += shift_by_width;
20216 UNBLOCK_INPUT;
20217 }
20218
20219
20220 /* EXPORT for RIF:
20221 Erase the current text line from the nominal cursor position
20222 (inclusive) to pixel column TO_X (exclusive). The idea is that
20223 everything from TO_X onward is already erased.
20224
20225 TO_X is a pixel position relative to updated_area of
20226 updated_window. TO_X == -1 means clear to the end of this area. */
20227
20228 void
20229 x_clear_end_of_line (to_x)
20230 int to_x;
20231 {
20232 struct frame *f;
20233 struct window *w = updated_window;
20234 int max_x, min_y, max_y;
20235 int from_x, from_y, to_y;
20236
20237 xassert (updated_window && updated_row);
20238 f = XFRAME (w->frame);
20239
20240 if (updated_row->full_width_p)
20241 max_x = WINDOW_TOTAL_WIDTH (w);
20242 else
20243 max_x = window_box_width (w, updated_area);
20244 max_y = window_text_bottom_y (w);
20245
20246 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20247 of window. For TO_X > 0, truncate to end of drawing area. */
20248 if (to_x == 0)
20249 return;
20250 else if (to_x < 0)
20251 to_x = max_x;
20252 else
20253 to_x = min (to_x, max_x);
20254
20255 to_y = min (max_y, output_cursor.y + updated_row->height);
20256
20257 /* Notice if the cursor will be cleared by this operation. */
20258 if (!updated_row->full_width_p)
20259 notice_overwritten_cursor (w, updated_area,
20260 output_cursor.x, -1,
20261 updated_row->y,
20262 MATRIX_ROW_BOTTOM_Y (updated_row));
20263
20264 from_x = output_cursor.x;
20265
20266 /* Translate to frame coordinates. */
20267 if (updated_row->full_width_p)
20268 {
20269 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20270 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20271 }
20272 else
20273 {
20274 int area_left = window_box_left (w, updated_area);
20275 from_x += area_left;
20276 to_x += area_left;
20277 }
20278
20279 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20280 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20281 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20282
20283 /* Prevent inadvertently clearing to end of the X window. */
20284 if (to_x > from_x && to_y > from_y)
20285 {
20286 BLOCK_INPUT;
20287 rif->clear_frame_area (f, from_x, from_y,
20288 to_x - from_x, to_y - from_y);
20289 UNBLOCK_INPUT;
20290 }
20291 }
20292
20293 #endif /* HAVE_WINDOW_SYSTEM */
20294
20295
20296 \f
20297 /***********************************************************************
20298 Cursor types
20299 ***********************************************************************/
20300
20301 /* Value is the internal representation of the specified cursor type
20302 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20303 of the bar cursor. */
20304
20305 static enum text_cursor_kinds
20306 get_specified_cursor_type (arg, width)
20307 Lisp_Object arg;
20308 int *width;
20309 {
20310 enum text_cursor_kinds type;
20311
20312 if (NILP (arg))
20313 return NO_CURSOR;
20314
20315 if (EQ (arg, Qbox))
20316 return FILLED_BOX_CURSOR;
20317
20318 if (EQ (arg, Qhollow))
20319 return HOLLOW_BOX_CURSOR;
20320
20321 if (EQ (arg, Qbar))
20322 {
20323 *width = 2;
20324 return BAR_CURSOR;
20325 }
20326
20327 if (CONSP (arg)
20328 && EQ (XCAR (arg), Qbar)
20329 && INTEGERP (XCDR (arg))
20330 && XINT (XCDR (arg)) >= 0)
20331 {
20332 *width = XINT (XCDR (arg));
20333 return BAR_CURSOR;
20334 }
20335
20336 if (EQ (arg, Qhbar))
20337 {
20338 *width = 2;
20339 return HBAR_CURSOR;
20340 }
20341
20342 if (CONSP (arg)
20343 && EQ (XCAR (arg), Qhbar)
20344 && INTEGERP (XCDR (arg))
20345 && XINT (XCDR (arg)) >= 0)
20346 {
20347 *width = XINT (XCDR (arg));
20348 return HBAR_CURSOR;
20349 }
20350
20351 /* Treat anything unknown as "hollow box cursor".
20352 It was bad to signal an error; people have trouble fixing
20353 .Xdefaults with Emacs, when it has something bad in it. */
20354 type = HOLLOW_BOX_CURSOR;
20355
20356 return type;
20357 }
20358
20359 /* Set the default cursor types for specified frame. */
20360 void
20361 set_frame_cursor_types (f, arg)
20362 struct frame *f;
20363 Lisp_Object arg;
20364 {
20365 int width;
20366 Lisp_Object tem;
20367
20368 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20369 FRAME_CURSOR_WIDTH (f) = width;
20370
20371 /* By default, set up the blink-off state depending on the on-state. */
20372
20373 tem = Fassoc (arg, Vblink_cursor_alist);
20374 if (!NILP (tem))
20375 {
20376 FRAME_BLINK_OFF_CURSOR (f)
20377 = get_specified_cursor_type (XCDR (tem), &width);
20378 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20379 }
20380 else
20381 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20382 }
20383
20384
20385 /* Return the cursor we want to be displayed in window W. Return
20386 width of bar/hbar cursor through WIDTH arg. Return with
20387 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20388 (i.e. if the `system caret' should track this cursor).
20389
20390 In a mini-buffer window, we want the cursor only to appear if we
20391 are reading input from this window. For the selected window, we
20392 want the cursor type given by the frame parameter or buffer local
20393 setting of cursor-type. If explicitly marked off, draw no cursor.
20394 In all other cases, we want a hollow box cursor. */
20395
20396 static enum text_cursor_kinds
20397 get_window_cursor_type (w, glyph, width, active_cursor)
20398 struct window *w;
20399 struct glyph *glyph;
20400 int *width;
20401 int *active_cursor;
20402 {
20403 struct frame *f = XFRAME (w->frame);
20404 struct buffer *b = XBUFFER (w->buffer);
20405 int cursor_type = DEFAULT_CURSOR;
20406 Lisp_Object alt_cursor;
20407 int non_selected = 0;
20408
20409 *active_cursor = 1;
20410
20411 /* Echo area */
20412 if (cursor_in_echo_area
20413 && FRAME_HAS_MINIBUF_P (f)
20414 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20415 {
20416 if (w == XWINDOW (echo_area_window))
20417 {
20418 *width = FRAME_CURSOR_WIDTH (f);
20419 return FRAME_DESIRED_CURSOR (f);
20420 }
20421
20422 *active_cursor = 0;
20423 non_selected = 1;
20424 }
20425
20426 /* Nonselected window or nonselected frame. */
20427 else if (w != XWINDOW (f->selected_window)
20428 #ifdef HAVE_WINDOW_SYSTEM
20429 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20430 #endif
20431 )
20432 {
20433 *active_cursor = 0;
20434
20435 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20436 return NO_CURSOR;
20437
20438 non_selected = 1;
20439 }
20440
20441 /* Never display a cursor in a window in which cursor-type is nil. */
20442 if (NILP (b->cursor_type))
20443 return NO_CURSOR;
20444
20445 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20446 if (non_selected)
20447 {
20448 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20449 return get_specified_cursor_type (alt_cursor, width);
20450 }
20451
20452 /* Get the normal cursor type for this window. */
20453 if (EQ (b->cursor_type, Qt))
20454 {
20455 cursor_type = FRAME_DESIRED_CURSOR (f);
20456 *width = FRAME_CURSOR_WIDTH (f);
20457 }
20458 else
20459 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20460
20461 /* Use normal cursor if not blinked off. */
20462 if (!w->cursor_off_p)
20463 {
20464 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20465 if (cursor_type == FILLED_BOX_CURSOR)
20466 cursor_type = HOLLOW_BOX_CURSOR;
20467 }
20468 return cursor_type;
20469 }
20470
20471 /* Cursor is blinked off, so determine how to "toggle" it. */
20472
20473 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20474 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20475 return get_specified_cursor_type (XCDR (alt_cursor), width);
20476
20477 /* Then see if frame has specified a specific blink off cursor type. */
20478 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20479 {
20480 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20481 return FRAME_BLINK_OFF_CURSOR (f);
20482 }
20483
20484 #if 0
20485 /* Some people liked having a permanently visible blinking cursor,
20486 while others had very strong opinions against it. So it was
20487 decided to remove it. KFS 2003-09-03 */
20488
20489 /* Finally perform built-in cursor blinking:
20490 filled box <-> hollow box
20491 wide [h]bar <-> narrow [h]bar
20492 narrow [h]bar <-> no cursor
20493 other type <-> no cursor */
20494
20495 if (cursor_type == FILLED_BOX_CURSOR)
20496 return HOLLOW_BOX_CURSOR;
20497
20498 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20499 {
20500 *width = 1;
20501 return cursor_type;
20502 }
20503 #endif
20504
20505 return NO_CURSOR;
20506 }
20507
20508
20509 #ifdef HAVE_WINDOW_SYSTEM
20510
20511 /* Notice when the text cursor of window W has been completely
20512 overwritten by a drawing operation that outputs glyphs in AREA
20513 starting at X0 and ending at X1 in the line starting at Y0 and
20514 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20515 the rest of the line after X0 has been written. Y coordinates
20516 are window-relative. */
20517
20518 static void
20519 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20520 struct window *w;
20521 enum glyph_row_area area;
20522 int x0, y0, x1, y1;
20523 {
20524 int cx0, cx1, cy0, cy1;
20525 struct glyph_row *row;
20526
20527 if (!w->phys_cursor_on_p)
20528 return;
20529 if (area != TEXT_AREA)
20530 return;
20531
20532 if (w->phys_cursor.vpos < 0
20533 || w->phys_cursor.vpos >= w->current_matrix->nrows
20534 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20535 !(row->enabled_p && row->displays_text_p)))
20536 return;
20537
20538 if (row->cursor_in_fringe_p)
20539 {
20540 row->cursor_in_fringe_p = 0;
20541 draw_fringe_bitmap (w, row, 0);
20542 w->phys_cursor_on_p = 0;
20543 return;
20544 }
20545
20546 cx0 = w->phys_cursor.x;
20547 cx1 = cx0 + w->phys_cursor_width;
20548 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20549 return;
20550
20551 /* The cursor image will be completely removed from the
20552 screen if the output area intersects the cursor area in
20553 y-direction. When we draw in [y0 y1[, and some part of
20554 the cursor is at y < y0, that part must have been drawn
20555 before. When scrolling, the cursor is erased before
20556 actually scrolling, so we don't come here. When not
20557 scrolling, the rows above the old cursor row must have
20558 changed, and in this case these rows must have written
20559 over the cursor image.
20560
20561 Likewise if part of the cursor is below y1, with the
20562 exception of the cursor being in the first blank row at
20563 the buffer and window end because update_text_area
20564 doesn't draw that row. (Except when it does, but
20565 that's handled in update_text_area.) */
20566
20567 cy0 = w->phys_cursor.y;
20568 cy1 = cy0 + w->phys_cursor_height;
20569 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20570 return;
20571
20572 w->phys_cursor_on_p = 0;
20573 }
20574
20575 #endif /* HAVE_WINDOW_SYSTEM */
20576
20577 \f
20578 /************************************************************************
20579 Mouse Face
20580 ************************************************************************/
20581
20582 #ifdef HAVE_WINDOW_SYSTEM
20583
20584 /* EXPORT for RIF:
20585 Fix the display of area AREA of overlapping row ROW in window W. */
20586
20587 void
20588 x_fix_overlapping_area (w, row, area)
20589 struct window *w;
20590 struct glyph_row *row;
20591 enum glyph_row_area area;
20592 {
20593 int i, x;
20594
20595 BLOCK_INPUT;
20596
20597 x = 0;
20598 for (i = 0; i < row->used[area];)
20599 {
20600 if (row->glyphs[area][i].overlaps_vertically_p)
20601 {
20602 int start = i, start_x = x;
20603
20604 do
20605 {
20606 x += row->glyphs[area][i].pixel_width;
20607 ++i;
20608 }
20609 while (i < row->used[area]
20610 && row->glyphs[area][i].overlaps_vertically_p);
20611
20612 draw_glyphs (w, start_x, row, area,
20613 start, i,
20614 DRAW_NORMAL_TEXT, 1);
20615 }
20616 else
20617 {
20618 x += row->glyphs[area][i].pixel_width;
20619 ++i;
20620 }
20621 }
20622
20623 UNBLOCK_INPUT;
20624 }
20625
20626
20627 /* EXPORT:
20628 Draw the cursor glyph of window W in glyph row ROW. See the
20629 comment of draw_glyphs for the meaning of HL. */
20630
20631 void
20632 draw_phys_cursor_glyph (w, row, hl)
20633 struct window *w;
20634 struct glyph_row *row;
20635 enum draw_glyphs_face hl;
20636 {
20637 /* If cursor hpos is out of bounds, don't draw garbage. This can
20638 happen in mini-buffer windows when switching between echo area
20639 glyphs and mini-buffer. */
20640 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20641 {
20642 int on_p = w->phys_cursor_on_p;
20643 int x1;
20644 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20645 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20646 hl, 0);
20647 w->phys_cursor_on_p = on_p;
20648
20649 if (hl == DRAW_CURSOR)
20650 w->phys_cursor_width = x1 - w->phys_cursor.x;
20651 /* When we erase the cursor, and ROW is overlapped by other
20652 rows, make sure that these overlapping parts of other rows
20653 are redrawn. */
20654 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20655 {
20656 if (row > w->current_matrix->rows
20657 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20658 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20659
20660 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20661 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20662 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20663 }
20664 }
20665 }
20666
20667
20668 /* EXPORT:
20669 Erase the image of a cursor of window W from the screen. */
20670
20671 void
20672 erase_phys_cursor (w)
20673 struct window *w;
20674 {
20675 struct frame *f = XFRAME (w->frame);
20676 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20677 int hpos = w->phys_cursor.hpos;
20678 int vpos = w->phys_cursor.vpos;
20679 int mouse_face_here_p = 0;
20680 struct glyph_matrix *active_glyphs = w->current_matrix;
20681 struct glyph_row *cursor_row;
20682 struct glyph *cursor_glyph;
20683 enum draw_glyphs_face hl;
20684
20685 /* No cursor displayed or row invalidated => nothing to do on the
20686 screen. */
20687 if (w->phys_cursor_type == NO_CURSOR)
20688 goto mark_cursor_off;
20689
20690 /* VPOS >= active_glyphs->nrows means that window has been resized.
20691 Don't bother to erase the cursor. */
20692 if (vpos >= active_glyphs->nrows)
20693 goto mark_cursor_off;
20694
20695 /* If row containing cursor is marked invalid, there is nothing we
20696 can do. */
20697 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20698 if (!cursor_row->enabled_p)
20699 goto mark_cursor_off;
20700
20701 /* If line spacing is > 0, old cursor may only be partially visible in
20702 window after split-window. So adjust visible height. */
20703 cursor_row->visible_height = min (cursor_row->visible_height,
20704 window_text_bottom_y (w) - cursor_row->y);
20705
20706 /* If row is completely invisible, don't attempt to delete a cursor which
20707 isn't there. This can happen if cursor is at top of a window, and
20708 we switch to a buffer with a header line in that window. */
20709 if (cursor_row->visible_height <= 0)
20710 goto mark_cursor_off;
20711
20712 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20713 if (cursor_row->cursor_in_fringe_p)
20714 {
20715 cursor_row->cursor_in_fringe_p = 0;
20716 draw_fringe_bitmap (w, cursor_row, 0);
20717 goto mark_cursor_off;
20718 }
20719
20720 /* This can happen when the new row is shorter than the old one.
20721 In this case, either draw_glyphs or clear_end_of_line
20722 should have cleared the cursor. Note that we wouldn't be
20723 able to erase the cursor in this case because we don't have a
20724 cursor glyph at hand. */
20725 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20726 goto mark_cursor_off;
20727
20728 /* If the cursor is in the mouse face area, redisplay that when
20729 we clear the cursor. */
20730 if (! NILP (dpyinfo->mouse_face_window)
20731 && w == XWINDOW (dpyinfo->mouse_face_window)
20732 && (vpos > dpyinfo->mouse_face_beg_row
20733 || (vpos == dpyinfo->mouse_face_beg_row
20734 && hpos >= dpyinfo->mouse_face_beg_col))
20735 && (vpos < dpyinfo->mouse_face_end_row
20736 || (vpos == dpyinfo->mouse_face_end_row
20737 && hpos < dpyinfo->mouse_face_end_col))
20738 /* Don't redraw the cursor's spot in mouse face if it is at the
20739 end of a line (on a newline). The cursor appears there, but
20740 mouse highlighting does not. */
20741 && cursor_row->used[TEXT_AREA] > hpos)
20742 mouse_face_here_p = 1;
20743
20744 /* Maybe clear the display under the cursor. */
20745 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20746 {
20747 int x, y;
20748 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20749 int width;
20750
20751 cursor_glyph = get_phys_cursor_glyph (w);
20752 if (cursor_glyph == NULL)
20753 goto mark_cursor_off;
20754
20755 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20756 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20757 width = min (cursor_glyph->pixel_width,
20758 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20759
20760 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20761 }
20762
20763 /* Erase the cursor by redrawing the character underneath it. */
20764 if (mouse_face_here_p)
20765 hl = DRAW_MOUSE_FACE;
20766 else
20767 hl = DRAW_NORMAL_TEXT;
20768 draw_phys_cursor_glyph (w, cursor_row, hl);
20769
20770 mark_cursor_off:
20771 w->phys_cursor_on_p = 0;
20772 w->phys_cursor_type = NO_CURSOR;
20773 }
20774
20775
20776 /* EXPORT:
20777 Display or clear cursor of window W. If ON is zero, clear the
20778 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20779 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20780
20781 void
20782 display_and_set_cursor (w, on, hpos, vpos, x, y)
20783 struct window *w;
20784 int on, hpos, vpos, x, y;
20785 {
20786 struct frame *f = XFRAME (w->frame);
20787 int new_cursor_type;
20788 int new_cursor_width;
20789 int active_cursor;
20790 struct glyph_row *glyph_row;
20791 struct glyph *glyph;
20792
20793 /* This is pointless on invisible frames, and dangerous on garbaged
20794 windows and frames; in the latter case, the frame or window may
20795 be in the midst of changing its size, and x and y may be off the
20796 window. */
20797 if (! FRAME_VISIBLE_P (f)
20798 || FRAME_GARBAGED_P (f)
20799 || vpos >= w->current_matrix->nrows
20800 || hpos >= w->current_matrix->matrix_w)
20801 return;
20802
20803 /* If cursor is off and we want it off, return quickly. */
20804 if (!on && !w->phys_cursor_on_p)
20805 return;
20806
20807 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20808 /* If cursor row is not enabled, we don't really know where to
20809 display the cursor. */
20810 if (!glyph_row->enabled_p)
20811 {
20812 w->phys_cursor_on_p = 0;
20813 return;
20814 }
20815
20816 glyph = NULL;
20817 if (!glyph_row->exact_window_width_line_p
20818 || hpos < glyph_row->used[TEXT_AREA])
20819 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20820
20821 xassert (interrupt_input_blocked);
20822
20823 /* Set new_cursor_type to the cursor we want to be displayed. */
20824 new_cursor_type = get_window_cursor_type (w, glyph,
20825 &new_cursor_width, &active_cursor);
20826
20827 /* If cursor is currently being shown and we don't want it to be or
20828 it is in the wrong place, or the cursor type is not what we want,
20829 erase it. */
20830 if (w->phys_cursor_on_p
20831 && (!on
20832 || w->phys_cursor.x != x
20833 || w->phys_cursor.y != y
20834 || new_cursor_type != w->phys_cursor_type
20835 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20836 && new_cursor_width != w->phys_cursor_width)))
20837 erase_phys_cursor (w);
20838
20839 /* Don't check phys_cursor_on_p here because that flag is only set
20840 to zero in some cases where we know that the cursor has been
20841 completely erased, to avoid the extra work of erasing the cursor
20842 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20843 still not be visible, or it has only been partly erased. */
20844 if (on)
20845 {
20846 w->phys_cursor_ascent = glyph_row->ascent;
20847 w->phys_cursor_height = glyph_row->height;
20848
20849 /* Set phys_cursor_.* before x_draw_.* is called because some
20850 of them may need the information. */
20851 w->phys_cursor.x = x;
20852 w->phys_cursor.y = glyph_row->y;
20853 w->phys_cursor.hpos = hpos;
20854 w->phys_cursor.vpos = vpos;
20855 }
20856
20857 rif->draw_window_cursor (w, glyph_row, x, y,
20858 new_cursor_type, new_cursor_width,
20859 on, active_cursor);
20860 }
20861
20862
20863 /* Switch the display of W's cursor on or off, according to the value
20864 of ON. */
20865
20866 static void
20867 update_window_cursor (w, on)
20868 struct window *w;
20869 int on;
20870 {
20871 /* Don't update cursor in windows whose frame is in the process
20872 of being deleted. */
20873 if (w->current_matrix)
20874 {
20875 BLOCK_INPUT;
20876 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20877 w->phys_cursor.x, w->phys_cursor.y);
20878 UNBLOCK_INPUT;
20879 }
20880 }
20881
20882
20883 /* Call update_window_cursor with parameter ON_P on all leaf windows
20884 in the window tree rooted at W. */
20885
20886 static void
20887 update_cursor_in_window_tree (w, on_p)
20888 struct window *w;
20889 int on_p;
20890 {
20891 while (w)
20892 {
20893 if (!NILP (w->hchild))
20894 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20895 else if (!NILP (w->vchild))
20896 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20897 else
20898 update_window_cursor (w, on_p);
20899
20900 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20901 }
20902 }
20903
20904
20905 /* EXPORT:
20906 Display the cursor on window W, or clear it, according to ON_P.
20907 Don't change the cursor's position. */
20908
20909 void
20910 x_update_cursor (f, on_p)
20911 struct frame *f;
20912 int on_p;
20913 {
20914 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20915 }
20916
20917
20918 /* EXPORT:
20919 Clear the cursor of window W to background color, and mark the
20920 cursor as not shown. This is used when the text where the cursor
20921 is is about to be rewritten. */
20922
20923 void
20924 x_clear_cursor (w)
20925 struct window *w;
20926 {
20927 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20928 update_window_cursor (w, 0);
20929 }
20930
20931
20932 /* EXPORT:
20933 Display the active region described by mouse_face_* according to DRAW. */
20934
20935 void
20936 show_mouse_face (dpyinfo, draw)
20937 Display_Info *dpyinfo;
20938 enum draw_glyphs_face draw;
20939 {
20940 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20941 struct frame *f = XFRAME (WINDOW_FRAME (w));
20942
20943 if (/* If window is in the process of being destroyed, don't bother
20944 to do anything. */
20945 w->current_matrix != NULL
20946 /* Don't update mouse highlight if hidden */
20947 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20948 /* Recognize when we are called to operate on rows that don't exist
20949 anymore. This can happen when a window is split. */
20950 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20951 {
20952 int phys_cursor_on_p = w->phys_cursor_on_p;
20953 struct glyph_row *row, *first, *last;
20954
20955 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20956 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20957
20958 for (row = first; row <= last && row->enabled_p; ++row)
20959 {
20960 int start_hpos, end_hpos, start_x;
20961
20962 /* For all but the first row, the highlight starts at column 0. */
20963 if (row == first)
20964 {
20965 start_hpos = dpyinfo->mouse_face_beg_col;
20966 start_x = dpyinfo->mouse_face_beg_x;
20967 }
20968 else
20969 {
20970 start_hpos = 0;
20971 start_x = 0;
20972 }
20973
20974 if (row == last)
20975 end_hpos = dpyinfo->mouse_face_end_col;
20976 else
20977 end_hpos = row->used[TEXT_AREA];
20978
20979 if (end_hpos > start_hpos)
20980 {
20981 draw_glyphs (w, start_x, row, TEXT_AREA,
20982 start_hpos, end_hpos,
20983 draw, 0);
20984
20985 row->mouse_face_p
20986 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20987 }
20988 }
20989
20990 /* When we've written over the cursor, arrange for it to
20991 be displayed again. */
20992 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20993 {
20994 BLOCK_INPUT;
20995 display_and_set_cursor (w, 1,
20996 w->phys_cursor.hpos, w->phys_cursor.vpos,
20997 w->phys_cursor.x, w->phys_cursor.y);
20998 UNBLOCK_INPUT;
20999 }
21000 }
21001
21002 /* Change the mouse cursor. */
21003 if (draw == DRAW_NORMAL_TEXT)
21004 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21005 else if (draw == DRAW_MOUSE_FACE)
21006 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21007 else
21008 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21009 }
21010
21011 /* EXPORT:
21012 Clear out the mouse-highlighted active region.
21013 Redraw it un-highlighted first. Value is non-zero if mouse
21014 face was actually drawn unhighlighted. */
21015
21016 int
21017 clear_mouse_face (dpyinfo)
21018 Display_Info *dpyinfo;
21019 {
21020 int cleared = 0;
21021
21022 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21023 {
21024 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21025 cleared = 1;
21026 }
21027
21028 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21029 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21030 dpyinfo->mouse_face_window = Qnil;
21031 dpyinfo->mouse_face_overlay = Qnil;
21032 return cleared;
21033 }
21034
21035
21036 /* EXPORT:
21037 Non-zero if physical cursor of window W is within mouse face. */
21038
21039 int
21040 cursor_in_mouse_face_p (w)
21041 struct window *w;
21042 {
21043 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21044 int in_mouse_face = 0;
21045
21046 if (WINDOWP (dpyinfo->mouse_face_window)
21047 && XWINDOW (dpyinfo->mouse_face_window) == w)
21048 {
21049 int hpos = w->phys_cursor.hpos;
21050 int vpos = w->phys_cursor.vpos;
21051
21052 if (vpos >= dpyinfo->mouse_face_beg_row
21053 && vpos <= dpyinfo->mouse_face_end_row
21054 && (vpos > dpyinfo->mouse_face_beg_row
21055 || hpos >= dpyinfo->mouse_face_beg_col)
21056 && (vpos < dpyinfo->mouse_face_end_row
21057 || hpos < dpyinfo->mouse_face_end_col
21058 || dpyinfo->mouse_face_past_end))
21059 in_mouse_face = 1;
21060 }
21061
21062 return in_mouse_face;
21063 }
21064
21065
21066
21067 \f
21068 /* Find the glyph matrix position of buffer position CHARPOS in window
21069 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21070 current glyphs must be up to date. If CHARPOS is above window
21071 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21072 of last line in W. In the row containing CHARPOS, stop before glyphs
21073 having STOP as object. */
21074
21075 #if 1 /* This is a version of fast_find_position that's more correct
21076 in the presence of hscrolling, for example. I didn't install
21077 it right away because the problem fixed is minor, it failed
21078 in 20.x as well, and I think it's too risky to install
21079 so near the release of 21.1. 2001-09-25 gerd. */
21080
21081 static int
21082 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21083 struct window *w;
21084 int charpos;
21085 int *hpos, *vpos, *x, *y;
21086 Lisp_Object stop;
21087 {
21088 struct glyph_row *row, *first;
21089 struct glyph *glyph, *end;
21090 int past_end = 0;
21091
21092 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21093 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21094 {
21095 *x = first->x;
21096 *y = first->y;
21097 *hpos = 0;
21098 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21099 return 1;
21100 }
21101
21102 row = row_containing_pos (w, charpos, first, NULL, 0);
21103 if (row == NULL)
21104 {
21105 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21106 past_end = 1;
21107 }
21108
21109 /* If whole rows or last part of a row came from a display overlay,
21110 row_containing_pos will skip over such rows because their end pos
21111 equals the start pos of the overlay or interval.
21112
21113 Move back if we have a STOP object and previous row's
21114 end glyph came from STOP. */
21115 if (!NILP (stop))
21116 {
21117 struct glyph_row *prev;
21118 while ((prev = row - 1, prev >= first)
21119 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21120 && prev->used[TEXT_AREA] > 0)
21121 {
21122 struct glyph *beg = prev->glyphs[TEXT_AREA];
21123 glyph = beg + prev->used[TEXT_AREA];
21124 while (--glyph >= beg
21125 && INTEGERP (glyph->object));
21126 if (glyph < beg
21127 || !EQ (stop, glyph->object))
21128 break;
21129 row = prev;
21130 }
21131 }
21132
21133 *x = row->x;
21134 *y = row->y;
21135 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21136
21137 glyph = row->glyphs[TEXT_AREA];
21138 end = glyph + row->used[TEXT_AREA];
21139
21140 /* Skip over glyphs not having an object at the start of the row.
21141 These are special glyphs like truncation marks on terminal
21142 frames. */
21143 if (row->displays_text_p)
21144 while (glyph < end
21145 && INTEGERP (glyph->object)
21146 && !EQ (stop, glyph->object)
21147 && glyph->charpos < 0)
21148 {
21149 *x += glyph->pixel_width;
21150 ++glyph;
21151 }
21152
21153 while (glyph < end
21154 && !INTEGERP (glyph->object)
21155 && !EQ (stop, glyph->object)
21156 && (!BUFFERP (glyph->object)
21157 || glyph->charpos < charpos))
21158 {
21159 *x += glyph->pixel_width;
21160 ++glyph;
21161 }
21162
21163 *hpos = glyph - row->glyphs[TEXT_AREA];
21164 return !past_end;
21165 }
21166
21167 #else /* not 1 */
21168
21169 static int
21170 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21171 struct window *w;
21172 int pos;
21173 int *hpos, *vpos, *x, *y;
21174 Lisp_Object stop;
21175 {
21176 int i;
21177 int lastcol;
21178 int maybe_next_line_p = 0;
21179 int line_start_position;
21180 int yb = window_text_bottom_y (w);
21181 struct glyph_row *row, *best_row;
21182 int row_vpos, best_row_vpos;
21183 int current_x;
21184
21185 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21186 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21187
21188 while (row->y < yb)
21189 {
21190 if (row->used[TEXT_AREA])
21191 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21192 else
21193 line_start_position = 0;
21194
21195 if (line_start_position > pos)
21196 break;
21197 /* If the position sought is the end of the buffer,
21198 don't include the blank lines at the bottom of the window. */
21199 else if (line_start_position == pos
21200 && pos == BUF_ZV (XBUFFER (w->buffer)))
21201 {
21202 maybe_next_line_p = 1;
21203 break;
21204 }
21205 else if (line_start_position > 0)
21206 {
21207 best_row = row;
21208 best_row_vpos = row_vpos;
21209 }
21210
21211 if (row->y + row->height >= yb)
21212 break;
21213
21214 ++row;
21215 ++row_vpos;
21216 }
21217
21218 /* Find the right column within BEST_ROW. */
21219 lastcol = 0;
21220 current_x = best_row->x;
21221 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21222 {
21223 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21224 int charpos = glyph->charpos;
21225
21226 if (BUFFERP (glyph->object))
21227 {
21228 if (charpos == pos)
21229 {
21230 *hpos = i;
21231 *vpos = best_row_vpos;
21232 *x = current_x;
21233 *y = best_row->y;
21234 return 1;
21235 }
21236 else if (charpos > pos)
21237 break;
21238 }
21239 else if (EQ (glyph->object, stop))
21240 break;
21241
21242 if (charpos > 0)
21243 lastcol = i;
21244 current_x += glyph->pixel_width;
21245 }
21246
21247 /* If we're looking for the end of the buffer,
21248 and we didn't find it in the line we scanned,
21249 use the start of the following line. */
21250 if (maybe_next_line_p)
21251 {
21252 ++best_row;
21253 ++best_row_vpos;
21254 lastcol = 0;
21255 current_x = best_row->x;
21256 }
21257
21258 *vpos = best_row_vpos;
21259 *hpos = lastcol + 1;
21260 *x = current_x;
21261 *y = best_row->y;
21262 return 0;
21263 }
21264
21265 #endif /* not 1 */
21266
21267
21268 /* Find the position of the glyph for position POS in OBJECT in
21269 window W's current matrix, and return in *X, *Y the pixel
21270 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21271
21272 RIGHT_P non-zero means return the position of the right edge of the
21273 glyph, RIGHT_P zero means return the left edge position.
21274
21275 If no glyph for POS exists in the matrix, return the position of
21276 the glyph with the next smaller position that is in the matrix, if
21277 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21278 exists in the matrix, return the position of the glyph with the
21279 next larger position in OBJECT.
21280
21281 Value is non-zero if a glyph was found. */
21282
21283 static int
21284 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21285 struct window *w;
21286 int pos;
21287 Lisp_Object object;
21288 int *hpos, *vpos, *x, *y;
21289 int right_p;
21290 {
21291 int yb = window_text_bottom_y (w);
21292 struct glyph_row *r;
21293 struct glyph *best_glyph = NULL;
21294 struct glyph_row *best_row = NULL;
21295 int best_x = 0;
21296
21297 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21298 r->enabled_p && r->y < yb;
21299 ++r)
21300 {
21301 struct glyph *g = r->glyphs[TEXT_AREA];
21302 struct glyph *e = g + r->used[TEXT_AREA];
21303 int gx;
21304
21305 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21306 if (EQ (g->object, object))
21307 {
21308 if (g->charpos == pos)
21309 {
21310 best_glyph = g;
21311 best_x = gx;
21312 best_row = r;
21313 goto found;
21314 }
21315 else if (best_glyph == NULL
21316 || ((abs (g->charpos - pos)
21317 < abs (best_glyph->charpos - pos))
21318 && (right_p
21319 ? g->charpos < pos
21320 : g->charpos > pos)))
21321 {
21322 best_glyph = g;
21323 best_x = gx;
21324 best_row = r;
21325 }
21326 }
21327 }
21328
21329 found:
21330
21331 if (best_glyph)
21332 {
21333 *x = best_x;
21334 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21335
21336 if (right_p)
21337 {
21338 *x += best_glyph->pixel_width;
21339 ++*hpos;
21340 }
21341
21342 *y = best_row->y;
21343 *vpos = best_row - w->current_matrix->rows;
21344 }
21345
21346 return best_glyph != NULL;
21347 }
21348
21349
21350 /* See if position X, Y is within a hot-spot of an image. */
21351
21352 static int
21353 on_hot_spot_p (hot_spot, x, y)
21354 Lisp_Object hot_spot;
21355 int x, y;
21356 {
21357 if (!CONSP (hot_spot))
21358 return 0;
21359
21360 if (EQ (XCAR (hot_spot), Qrect))
21361 {
21362 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21363 Lisp_Object rect = XCDR (hot_spot);
21364 Lisp_Object tem;
21365 if (!CONSP (rect))
21366 return 0;
21367 if (!CONSP (XCAR (rect)))
21368 return 0;
21369 if (!CONSP (XCDR (rect)))
21370 return 0;
21371 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21372 return 0;
21373 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21374 return 0;
21375 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21376 return 0;
21377 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21378 return 0;
21379 return 1;
21380 }
21381 else if (EQ (XCAR (hot_spot), Qcircle))
21382 {
21383 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21384 Lisp_Object circ = XCDR (hot_spot);
21385 Lisp_Object lr, lx0, ly0;
21386 if (CONSP (circ)
21387 && CONSP (XCAR (circ))
21388 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21389 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21390 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21391 {
21392 double r = XFLOATINT (lr);
21393 double dx = XINT (lx0) - x;
21394 double dy = XINT (ly0) - y;
21395 return (dx * dx + dy * dy <= r * r);
21396 }
21397 }
21398 else if (EQ (XCAR (hot_spot), Qpoly))
21399 {
21400 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21401 if (VECTORP (XCDR (hot_spot)))
21402 {
21403 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21404 Lisp_Object *poly = v->contents;
21405 int n = v->size;
21406 int i;
21407 int inside = 0;
21408 Lisp_Object lx, ly;
21409 int x0, y0;
21410
21411 /* Need an even number of coordinates, and at least 3 edges. */
21412 if (n < 6 || n & 1)
21413 return 0;
21414
21415 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21416 If count is odd, we are inside polygon. Pixels on edges
21417 may or may not be included depending on actual geometry of the
21418 polygon. */
21419 if ((lx = poly[n-2], !INTEGERP (lx))
21420 || (ly = poly[n-1], !INTEGERP (lx)))
21421 return 0;
21422 x0 = XINT (lx), y0 = XINT (ly);
21423 for (i = 0; i < n; i += 2)
21424 {
21425 int x1 = x0, y1 = y0;
21426 if ((lx = poly[i], !INTEGERP (lx))
21427 || (ly = poly[i+1], !INTEGERP (ly)))
21428 return 0;
21429 x0 = XINT (lx), y0 = XINT (ly);
21430
21431 /* Does this segment cross the X line? */
21432 if (x0 >= x)
21433 {
21434 if (x1 >= x)
21435 continue;
21436 }
21437 else if (x1 < x)
21438 continue;
21439 if (y > y0 && y > y1)
21440 continue;
21441 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21442 inside = !inside;
21443 }
21444 return inside;
21445 }
21446 }
21447 /* If we don't understand the format, pretend we're not in the hot-spot. */
21448 return 0;
21449 }
21450
21451 Lisp_Object
21452 find_hot_spot (map, x, y)
21453 Lisp_Object map;
21454 int x, y;
21455 {
21456 while (CONSP (map))
21457 {
21458 if (CONSP (XCAR (map))
21459 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21460 return XCAR (map);
21461 map = XCDR (map);
21462 }
21463
21464 return Qnil;
21465 }
21466
21467 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21468 3, 3, 0,
21469 doc: /* Lookup in image map MAP coordinates X and Y.
21470 An image map is an alist where each element has the format (AREA ID PLIST).
21471 An AREA is specified as either a rectangle, a circle, or a polygon:
21472 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21473 pixel coordinates of the upper left and bottom right corners.
21474 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21475 and the radius of the circle; r may be a float or integer.
21476 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21477 vector describes one corner in the polygon.
21478 Returns the alist element for the first matching AREA in MAP. */)
21479 (map, x, y)
21480 Lisp_Object map;
21481 Lisp_Object x, y;
21482 {
21483 if (NILP (map))
21484 return Qnil;
21485
21486 CHECK_NUMBER (x);
21487 CHECK_NUMBER (y);
21488
21489 return find_hot_spot (map, XINT (x), XINT (y));
21490 }
21491
21492
21493 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21494 static void
21495 define_frame_cursor1 (f, cursor, pointer)
21496 struct frame *f;
21497 Cursor cursor;
21498 Lisp_Object pointer;
21499 {
21500 /* Do not change cursor shape while dragging mouse. */
21501 if (!NILP (do_mouse_tracking))
21502 return;
21503
21504 if (!NILP (pointer))
21505 {
21506 if (EQ (pointer, Qarrow))
21507 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21508 else if (EQ (pointer, Qhand))
21509 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21510 else if (EQ (pointer, Qtext))
21511 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21512 else if (EQ (pointer, intern ("hdrag")))
21513 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21514 #ifdef HAVE_X_WINDOWS
21515 else if (EQ (pointer, intern ("vdrag")))
21516 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21517 #endif
21518 else if (EQ (pointer, intern ("hourglass")))
21519 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21520 else if (EQ (pointer, Qmodeline))
21521 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21522 else
21523 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21524 }
21525
21526 if (cursor != No_Cursor)
21527 rif->define_frame_cursor (f, cursor);
21528 }
21529
21530 /* Take proper action when mouse has moved to the mode or header line
21531 or marginal area AREA of window W, x-position X and y-position Y.
21532 X is relative to the start of the text display area of W, so the
21533 width of bitmap areas and scroll bars must be subtracted to get a
21534 position relative to the start of the mode line. */
21535
21536 static void
21537 note_mode_line_or_margin_highlight (window, x, y, area)
21538 Lisp_Object window;
21539 int x, y;
21540 enum window_part area;
21541 {
21542 struct window *w = XWINDOW (window);
21543 struct frame *f = XFRAME (w->frame);
21544 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21545 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21546 Lisp_Object pointer = Qnil;
21547 int charpos, dx, dy, width, height;
21548 Lisp_Object string, object = Qnil;
21549 Lisp_Object pos, help;
21550
21551 Lisp_Object mouse_face;
21552 int original_x_pixel = x;
21553 struct glyph * glyph = NULL;
21554 struct glyph_row *row;
21555
21556 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21557 {
21558 int x0;
21559 struct glyph *end;
21560
21561 string = mode_line_string (w, area, &x, &y, &charpos,
21562 &object, &dx, &dy, &width, &height);
21563
21564 row = (area == ON_MODE_LINE
21565 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21566 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21567
21568 /* Find glyph */
21569 if (row->mode_line_p && row->enabled_p)
21570 {
21571 glyph = row->glyphs[TEXT_AREA];
21572 end = glyph + row->used[TEXT_AREA];
21573
21574 for (x0 = original_x_pixel;
21575 glyph < end && x0 >= glyph->pixel_width;
21576 ++glyph)
21577 x0 -= glyph->pixel_width;
21578
21579 if (glyph >= end)
21580 glyph = NULL;
21581 }
21582 }
21583 else
21584 {
21585 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21586 string = marginal_area_string (w, area, &x, &y, &charpos,
21587 &object, &dx, &dy, &width, &height);
21588 }
21589
21590 help = Qnil;
21591
21592 if (IMAGEP (object))
21593 {
21594 Lisp_Object image_map, hotspot;
21595 if ((image_map = Fplist_get (XCDR (object), QCmap),
21596 !NILP (image_map))
21597 && (hotspot = find_hot_spot (image_map, dx, dy),
21598 CONSP (hotspot))
21599 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21600 {
21601 Lisp_Object area_id, plist;
21602
21603 area_id = XCAR (hotspot);
21604 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21605 If so, we could look for mouse-enter, mouse-leave
21606 properties in PLIST (and do something...). */
21607 hotspot = XCDR (hotspot);
21608 if (CONSP (hotspot)
21609 && (plist = XCAR (hotspot), CONSP (plist)))
21610 {
21611 pointer = Fplist_get (plist, Qpointer);
21612 if (NILP (pointer))
21613 pointer = Qhand;
21614 help = Fplist_get (plist, Qhelp_echo);
21615 if (!NILP (help))
21616 {
21617 help_echo_string = help;
21618 /* Is this correct? ++kfs */
21619 XSETWINDOW (help_echo_window, w);
21620 help_echo_object = w->buffer;
21621 help_echo_pos = charpos;
21622 }
21623 }
21624 }
21625 if (NILP (pointer))
21626 pointer = Fplist_get (XCDR (object), QCpointer);
21627 }
21628
21629 if (STRINGP (string))
21630 {
21631 pos = make_number (charpos);
21632 /* If we're on a string with `help-echo' text property, arrange
21633 for the help to be displayed. This is done by setting the
21634 global variable help_echo_string to the help string. */
21635 if (NILP (help))
21636 {
21637 help = Fget_text_property (pos, Qhelp_echo, string);
21638 if (!NILP (help))
21639 {
21640 help_echo_string = help;
21641 XSETWINDOW (help_echo_window, w);
21642 help_echo_object = string;
21643 help_echo_pos = charpos;
21644 }
21645 }
21646
21647 if (NILP (pointer))
21648 pointer = Fget_text_property (pos, Qpointer, string);
21649
21650 /* Change the mouse pointer according to what is under X/Y. */
21651 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21652 {
21653 Lisp_Object map;
21654 map = Fget_text_property (pos, Qlocal_map, string);
21655 if (!KEYMAPP (map))
21656 map = Fget_text_property (pos, Qkeymap, string);
21657 if (!KEYMAPP (map))
21658 cursor = dpyinfo->vertical_scroll_bar_cursor;
21659 }
21660
21661 /* Change the mouse face according to what is under X/Y. */
21662 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21663 if (!NILP (mouse_face)
21664 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21665 && glyph)
21666 {
21667 Lisp_Object b, e;
21668
21669 struct glyph * tmp_glyph;
21670
21671 int gpos;
21672 int gseq_length;
21673 int total_pixel_width;
21674 int ignore;
21675
21676 int vpos, hpos;
21677
21678 b = Fprevious_single_property_change (make_number (charpos + 1),
21679 Qmouse_face, string, Qnil);
21680 if (NILP (b))
21681 b = make_number (0);
21682
21683 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21684 if (NILP (e))
21685 e = make_number (SCHARS (string));
21686
21687 /* Calculate the position(glyph position: GPOS) of GLYPH in
21688 displayed string. GPOS is different from CHARPOS.
21689
21690 CHARPOS is the position of glyph in internal string
21691 object. A mode line string format has structures which
21692 is converted to a flatten by emacs lisp interpreter.
21693 The internal string is an element of the structures.
21694 The displayed string is the flatten string. */
21695 for (tmp_glyph = glyph - 1, gpos = 0;
21696 tmp_glyph->charpos >= XINT (b);
21697 tmp_glyph--, gpos++)
21698 {
21699 if (!EQ (tmp_glyph->object, glyph->object))
21700 break;
21701 }
21702
21703 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21704 displayed string holding GLYPH.
21705
21706 GSEQ_LENGTH is different from SCHARS (STRING).
21707 SCHARS (STRING) returns the length of the internal string. */
21708 for (tmp_glyph = glyph, gseq_length = gpos;
21709 tmp_glyph->charpos < XINT (e);
21710 tmp_glyph++, gseq_length++)
21711 {
21712 if (!EQ (tmp_glyph->object, glyph->object))
21713 break;
21714 }
21715
21716 total_pixel_width = 0;
21717 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21718 total_pixel_width += tmp_glyph->pixel_width;
21719
21720 /* Pre calculation of re-rendering position */
21721 vpos = (x - gpos);
21722 hpos = (area == ON_MODE_LINE
21723 ? (w->current_matrix)->nrows - 1
21724 : 0);
21725
21726 /* If the re-rendering position is included in the last
21727 re-rendering area, we should do nothing. */
21728 if ( EQ (window, dpyinfo->mouse_face_window)
21729 && dpyinfo->mouse_face_beg_col <= vpos
21730 && vpos < dpyinfo->mouse_face_end_col
21731 && dpyinfo->mouse_face_beg_row == hpos )
21732 return;
21733
21734 if (clear_mouse_face (dpyinfo))
21735 cursor = No_Cursor;
21736
21737 dpyinfo->mouse_face_beg_col = vpos;
21738 dpyinfo->mouse_face_beg_row = hpos;
21739
21740 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21741 dpyinfo->mouse_face_beg_y = 0;
21742
21743 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21744 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21745
21746 dpyinfo->mouse_face_end_x = 0;
21747 dpyinfo->mouse_face_end_y = 0;
21748
21749 dpyinfo->mouse_face_past_end = 0;
21750 dpyinfo->mouse_face_window = window;
21751
21752 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21753 charpos,
21754 0, 0, 0, &ignore,
21755 glyph->face_id, 1);
21756 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21757
21758 if (NILP (pointer))
21759 pointer = Qhand;
21760 }
21761 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21762 clear_mouse_face (dpyinfo);
21763 }
21764 define_frame_cursor1 (f, cursor, pointer);
21765 }
21766
21767
21768 /* EXPORT:
21769 Take proper action when the mouse has moved to position X, Y on
21770 frame F as regards highlighting characters that have mouse-face
21771 properties. Also de-highlighting chars where the mouse was before.
21772 X and Y can be negative or out of range. */
21773
21774 void
21775 note_mouse_highlight (f, x, y)
21776 struct frame *f;
21777 int x, y;
21778 {
21779 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21780 enum window_part part;
21781 Lisp_Object window;
21782 struct window *w;
21783 Cursor cursor = No_Cursor;
21784 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21785 struct buffer *b;
21786
21787 /* When a menu is active, don't highlight because this looks odd. */
21788 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21789 if (popup_activated ())
21790 return;
21791 #endif
21792
21793 if (NILP (Vmouse_highlight)
21794 || !f->glyphs_initialized_p)
21795 return;
21796
21797 dpyinfo->mouse_face_mouse_x = x;
21798 dpyinfo->mouse_face_mouse_y = y;
21799 dpyinfo->mouse_face_mouse_frame = f;
21800
21801 if (dpyinfo->mouse_face_defer)
21802 return;
21803
21804 if (gc_in_progress)
21805 {
21806 dpyinfo->mouse_face_deferred_gc = 1;
21807 return;
21808 }
21809
21810 /* Which window is that in? */
21811 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21812
21813 /* If we were displaying active text in another window, clear that.
21814 Also clear if we move out of text area in same window. */
21815 if (! EQ (window, dpyinfo->mouse_face_window)
21816 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21817 && !NILP (dpyinfo->mouse_face_window)))
21818 clear_mouse_face (dpyinfo);
21819
21820 /* Not on a window -> return. */
21821 if (!WINDOWP (window))
21822 return;
21823
21824 /* Reset help_echo_string. It will get recomputed below. */
21825 help_echo_string = Qnil;
21826
21827 /* Convert to window-relative pixel coordinates. */
21828 w = XWINDOW (window);
21829 frame_to_window_pixel_xy (w, &x, &y);
21830
21831 /* Handle tool-bar window differently since it doesn't display a
21832 buffer. */
21833 if (EQ (window, f->tool_bar_window))
21834 {
21835 note_tool_bar_highlight (f, x, y);
21836 return;
21837 }
21838
21839 /* Mouse is on the mode, header line or margin? */
21840 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21841 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21842 {
21843 note_mode_line_or_margin_highlight (window, x, y, part);
21844 return;
21845 }
21846
21847 if (part == ON_VERTICAL_BORDER)
21848 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21849 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21850 || part == ON_SCROLL_BAR)
21851 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21852 else
21853 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21854
21855 /* Are we in a window whose display is up to date?
21856 And verify the buffer's text has not changed. */
21857 b = XBUFFER (w->buffer);
21858 if (part == ON_TEXT
21859 && EQ (w->window_end_valid, w->buffer)
21860 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21861 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21862 {
21863 int hpos, vpos, pos, i, dx, dy, area;
21864 struct glyph *glyph;
21865 Lisp_Object object;
21866 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21867 Lisp_Object *overlay_vec = NULL;
21868 int noverlays;
21869 struct buffer *obuf;
21870 int obegv, ozv, same_region;
21871
21872 /* Find the glyph under X/Y. */
21873 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21874
21875 /* Look for :pointer property on image. */
21876 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21877 {
21878 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21879 if (img != NULL && IMAGEP (img->spec))
21880 {
21881 Lisp_Object image_map, hotspot;
21882 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21883 !NILP (image_map))
21884 && (hotspot = find_hot_spot (image_map,
21885 glyph->slice.x + dx,
21886 glyph->slice.y + dy),
21887 CONSP (hotspot))
21888 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21889 {
21890 Lisp_Object area_id, plist;
21891
21892 area_id = XCAR (hotspot);
21893 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21894 If so, we could look for mouse-enter, mouse-leave
21895 properties in PLIST (and do something...). */
21896 hotspot = XCDR (hotspot);
21897 if (CONSP (hotspot)
21898 && (plist = XCAR (hotspot), CONSP (plist)))
21899 {
21900 pointer = Fplist_get (plist, Qpointer);
21901 if (NILP (pointer))
21902 pointer = Qhand;
21903 help_echo_string = Fplist_get (plist, Qhelp_echo);
21904 if (!NILP (help_echo_string))
21905 {
21906 help_echo_window = window;
21907 help_echo_object = glyph->object;
21908 help_echo_pos = glyph->charpos;
21909 }
21910 }
21911 }
21912 if (NILP (pointer))
21913 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21914 }
21915 }
21916
21917 /* Clear mouse face if X/Y not over text. */
21918 if (glyph == NULL
21919 || area != TEXT_AREA
21920 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21921 {
21922 if (clear_mouse_face (dpyinfo))
21923 cursor = No_Cursor;
21924 if (NILP (pointer))
21925 {
21926 if (area != TEXT_AREA)
21927 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21928 else
21929 pointer = Vvoid_text_area_pointer;
21930 }
21931 goto set_cursor;
21932 }
21933
21934 pos = glyph->charpos;
21935 object = glyph->object;
21936 if (!STRINGP (object) && !BUFFERP (object))
21937 goto set_cursor;
21938
21939 /* If we get an out-of-range value, return now; avoid an error. */
21940 if (BUFFERP (object) && pos > BUF_Z (b))
21941 goto set_cursor;
21942
21943 /* Make the window's buffer temporarily current for
21944 overlays_at and compute_char_face. */
21945 obuf = current_buffer;
21946 current_buffer = b;
21947 obegv = BEGV;
21948 ozv = ZV;
21949 BEGV = BEG;
21950 ZV = Z;
21951
21952 /* Is this char mouse-active or does it have help-echo? */
21953 position = make_number (pos);
21954
21955 if (BUFFERP (object))
21956 {
21957 /* Put all the overlays we want in a vector in overlay_vec. */
21958 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21959 /* Sort overlays into increasing priority order. */
21960 noverlays = sort_overlays (overlay_vec, noverlays, w);
21961 }
21962 else
21963 noverlays = 0;
21964
21965 same_region = (EQ (window, dpyinfo->mouse_face_window)
21966 && vpos >= dpyinfo->mouse_face_beg_row
21967 && vpos <= dpyinfo->mouse_face_end_row
21968 && (vpos > dpyinfo->mouse_face_beg_row
21969 || hpos >= dpyinfo->mouse_face_beg_col)
21970 && (vpos < dpyinfo->mouse_face_end_row
21971 || hpos < dpyinfo->mouse_face_end_col
21972 || dpyinfo->mouse_face_past_end));
21973
21974 if (same_region)
21975 cursor = No_Cursor;
21976
21977 /* Check mouse-face highlighting. */
21978 if (! same_region
21979 /* If there exists an overlay with mouse-face overlapping
21980 the one we are currently highlighting, we have to
21981 check if we enter the overlapping overlay, and then
21982 highlight only that. */
21983 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21984 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21985 {
21986 /* Find the highest priority overlay that has a mouse-face
21987 property. */
21988 overlay = Qnil;
21989 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21990 {
21991 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21992 if (!NILP (mouse_face))
21993 overlay = overlay_vec[i];
21994 }
21995
21996 /* If we're actually highlighting the same overlay as
21997 before, there's no need to do that again. */
21998 if (!NILP (overlay)
21999 && EQ (overlay, dpyinfo->mouse_face_overlay))
22000 goto check_help_echo;
22001
22002 dpyinfo->mouse_face_overlay = overlay;
22003
22004 /* Clear the display of the old active region, if any. */
22005 if (clear_mouse_face (dpyinfo))
22006 cursor = No_Cursor;
22007
22008 /* If no overlay applies, get a text property. */
22009 if (NILP (overlay))
22010 mouse_face = Fget_text_property (position, Qmouse_face, object);
22011
22012 /* Handle the overlay case. */
22013 if (!NILP (overlay))
22014 {
22015 /* Find the range of text around this char that
22016 should be active. */
22017 Lisp_Object before, after;
22018 int ignore;
22019
22020 before = Foverlay_start (overlay);
22021 after = Foverlay_end (overlay);
22022 /* Record this as the current active region. */
22023 fast_find_position (w, XFASTINT (before),
22024 &dpyinfo->mouse_face_beg_col,
22025 &dpyinfo->mouse_face_beg_row,
22026 &dpyinfo->mouse_face_beg_x,
22027 &dpyinfo->mouse_face_beg_y, Qnil);
22028
22029 dpyinfo->mouse_face_past_end
22030 = !fast_find_position (w, XFASTINT (after),
22031 &dpyinfo->mouse_face_end_col,
22032 &dpyinfo->mouse_face_end_row,
22033 &dpyinfo->mouse_face_end_x,
22034 &dpyinfo->mouse_face_end_y, Qnil);
22035 dpyinfo->mouse_face_window = window;
22036
22037 dpyinfo->mouse_face_face_id
22038 = face_at_buffer_position (w, pos, 0, 0,
22039 &ignore, pos + 1,
22040 !dpyinfo->mouse_face_hidden);
22041
22042 /* Display it as active. */
22043 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22044 cursor = No_Cursor;
22045 }
22046 /* Handle the text property case. */
22047 else if (!NILP (mouse_face) && BUFFERP (object))
22048 {
22049 /* Find the range of text around this char that
22050 should be active. */
22051 Lisp_Object before, after, beginning, end;
22052 int ignore;
22053
22054 beginning = Fmarker_position (w->start);
22055 end = make_number (BUF_Z (XBUFFER (object))
22056 - XFASTINT (w->window_end_pos));
22057 before
22058 = Fprevious_single_property_change (make_number (pos + 1),
22059 Qmouse_face,
22060 object, beginning);
22061 after
22062 = Fnext_single_property_change (position, Qmouse_face,
22063 object, end);
22064
22065 /* Record this as the current active region. */
22066 fast_find_position (w, XFASTINT (before),
22067 &dpyinfo->mouse_face_beg_col,
22068 &dpyinfo->mouse_face_beg_row,
22069 &dpyinfo->mouse_face_beg_x,
22070 &dpyinfo->mouse_face_beg_y, Qnil);
22071 dpyinfo->mouse_face_past_end
22072 = !fast_find_position (w, XFASTINT (after),
22073 &dpyinfo->mouse_face_end_col,
22074 &dpyinfo->mouse_face_end_row,
22075 &dpyinfo->mouse_face_end_x,
22076 &dpyinfo->mouse_face_end_y, Qnil);
22077 dpyinfo->mouse_face_window = window;
22078
22079 if (BUFFERP (object))
22080 dpyinfo->mouse_face_face_id
22081 = face_at_buffer_position (w, pos, 0, 0,
22082 &ignore, pos + 1,
22083 !dpyinfo->mouse_face_hidden);
22084
22085 /* Display it as active. */
22086 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22087 cursor = No_Cursor;
22088 }
22089 else if (!NILP (mouse_face) && STRINGP (object))
22090 {
22091 Lisp_Object b, e;
22092 int ignore;
22093
22094 b = Fprevious_single_property_change (make_number (pos + 1),
22095 Qmouse_face,
22096 object, Qnil);
22097 e = Fnext_single_property_change (position, Qmouse_face,
22098 object, Qnil);
22099 if (NILP (b))
22100 b = make_number (0);
22101 if (NILP (e))
22102 e = make_number (SCHARS (object) - 1);
22103
22104 fast_find_string_pos (w, XINT (b), object,
22105 &dpyinfo->mouse_face_beg_col,
22106 &dpyinfo->mouse_face_beg_row,
22107 &dpyinfo->mouse_face_beg_x,
22108 &dpyinfo->mouse_face_beg_y, 0);
22109 fast_find_string_pos (w, XINT (e), object,
22110 &dpyinfo->mouse_face_end_col,
22111 &dpyinfo->mouse_face_end_row,
22112 &dpyinfo->mouse_face_end_x,
22113 &dpyinfo->mouse_face_end_y, 1);
22114 dpyinfo->mouse_face_past_end = 0;
22115 dpyinfo->mouse_face_window = window;
22116 dpyinfo->mouse_face_face_id
22117 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22118 glyph->face_id, 1);
22119 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22120 cursor = No_Cursor;
22121 }
22122 else if (STRINGP (object) && NILP (mouse_face))
22123 {
22124 /* A string which doesn't have mouse-face, but
22125 the text ``under'' it might have. */
22126 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22127 int start = MATRIX_ROW_START_CHARPOS (r);
22128
22129 pos = string_buffer_position (w, object, start);
22130 if (pos > 0)
22131 mouse_face = get_char_property_and_overlay (make_number (pos),
22132 Qmouse_face,
22133 w->buffer,
22134 &overlay);
22135 if (!NILP (mouse_face) && !NILP (overlay))
22136 {
22137 Lisp_Object before = Foverlay_start (overlay);
22138 Lisp_Object after = Foverlay_end (overlay);
22139 int ignore;
22140
22141 /* Note that we might not be able to find position
22142 BEFORE in the glyph matrix if the overlay is
22143 entirely covered by a `display' property. In
22144 this case, we overshoot. So let's stop in
22145 the glyph matrix before glyphs for OBJECT. */
22146 fast_find_position (w, XFASTINT (before),
22147 &dpyinfo->mouse_face_beg_col,
22148 &dpyinfo->mouse_face_beg_row,
22149 &dpyinfo->mouse_face_beg_x,
22150 &dpyinfo->mouse_face_beg_y,
22151 object);
22152
22153 dpyinfo->mouse_face_past_end
22154 = !fast_find_position (w, XFASTINT (after),
22155 &dpyinfo->mouse_face_end_col,
22156 &dpyinfo->mouse_face_end_row,
22157 &dpyinfo->mouse_face_end_x,
22158 &dpyinfo->mouse_face_end_y,
22159 Qnil);
22160 dpyinfo->mouse_face_window = window;
22161 dpyinfo->mouse_face_face_id
22162 = face_at_buffer_position (w, pos, 0, 0,
22163 &ignore, pos + 1,
22164 !dpyinfo->mouse_face_hidden);
22165
22166 /* Display it as active. */
22167 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22168 cursor = No_Cursor;
22169 }
22170 }
22171 }
22172
22173 check_help_echo:
22174
22175 /* Look for a `help-echo' property. */
22176 if (NILP (help_echo_string)) {
22177 Lisp_Object help, overlay;
22178
22179 /* Check overlays first. */
22180 help = overlay = Qnil;
22181 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22182 {
22183 overlay = overlay_vec[i];
22184 help = Foverlay_get (overlay, Qhelp_echo);
22185 }
22186
22187 if (!NILP (help))
22188 {
22189 help_echo_string = help;
22190 help_echo_window = window;
22191 help_echo_object = overlay;
22192 help_echo_pos = pos;
22193 }
22194 else
22195 {
22196 Lisp_Object object = glyph->object;
22197 int charpos = glyph->charpos;
22198
22199 /* Try text properties. */
22200 if (STRINGP (object)
22201 && charpos >= 0
22202 && charpos < SCHARS (object))
22203 {
22204 help = Fget_text_property (make_number (charpos),
22205 Qhelp_echo, object);
22206 if (NILP (help))
22207 {
22208 /* If the string itself doesn't specify a help-echo,
22209 see if the buffer text ``under'' it does. */
22210 struct glyph_row *r
22211 = MATRIX_ROW (w->current_matrix, vpos);
22212 int start = MATRIX_ROW_START_CHARPOS (r);
22213 int pos = string_buffer_position (w, object, start);
22214 if (pos > 0)
22215 {
22216 help = Fget_char_property (make_number (pos),
22217 Qhelp_echo, w->buffer);
22218 if (!NILP (help))
22219 {
22220 charpos = pos;
22221 object = w->buffer;
22222 }
22223 }
22224 }
22225 }
22226 else if (BUFFERP (object)
22227 && charpos >= BEGV
22228 && charpos < ZV)
22229 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22230 object);
22231
22232 if (!NILP (help))
22233 {
22234 help_echo_string = help;
22235 help_echo_window = window;
22236 help_echo_object = object;
22237 help_echo_pos = charpos;
22238 }
22239 }
22240 }
22241
22242 /* Look for a `pointer' property. */
22243 if (NILP (pointer))
22244 {
22245 /* Check overlays first. */
22246 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22247 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22248
22249 if (NILP (pointer))
22250 {
22251 Lisp_Object object = glyph->object;
22252 int charpos = glyph->charpos;
22253
22254 /* Try text properties. */
22255 if (STRINGP (object)
22256 && charpos >= 0
22257 && charpos < SCHARS (object))
22258 {
22259 pointer = Fget_text_property (make_number (charpos),
22260 Qpointer, object);
22261 if (NILP (pointer))
22262 {
22263 /* If the string itself doesn't specify a pointer,
22264 see if the buffer text ``under'' it does. */
22265 struct glyph_row *r
22266 = MATRIX_ROW (w->current_matrix, vpos);
22267 int start = MATRIX_ROW_START_CHARPOS (r);
22268 int pos = string_buffer_position (w, object, start);
22269 if (pos > 0)
22270 pointer = Fget_char_property (make_number (pos),
22271 Qpointer, w->buffer);
22272 }
22273 }
22274 else if (BUFFERP (object)
22275 && charpos >= BEGV
22276 && charpos < ZV)
22277 pointer = Fget_text_property (make_number (charpos),
22278 Qpointer, object);
22279 }
22280 }
22281
22282 BEGV = obegv;
22283 ZV = ozv;
22284 current_buffer = obuf;
22285 }
22286
22287 set_cursor:
22288
22289 define_frame_cursor1 (f, cursor, pointer);
22290 }
22291
22292
22293 /* EXPORT for RIF:
22294 Clear any mouse-face on window W. This function is part of the
22295 redisplay interface, and is called from try_window_id and similar
22296 functions to ensure the mouse-highlight is off. */
22297
22298 void
22299 x_clear_window_mouse_face (w)
22300 struct window *w;
22301 {
22302 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22303 Lisp_Object window;
22304
22305 BLOCK_INPUT;
22306 XSETWINDOW (window, w);
22307 if (EQ (window, dpyinfo->mouse_face_window))
22308 clear_mouse_face (dpyinfo);
22309 UNBLOCK_INPUT;
22310 }
22311
22312
22313 /* EXPORT:
22314 Just discard the mouse face information for frame F, if any.
22315 This is used when the size of F is changed. */
22316
22317 void
22318 cancel_mouse_face (f)
22319 struct frame *f;
22320 {
22321 Lisp_Object window;
22322 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22323
22324 window = dpyinfo->mouse_face_window;
22325 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22326 {
22327 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22328 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22329 dpyinfo->mouse_face_window = Qnil;
22330 }
22331 }
22332
22333
22334 #endif /* HAVE_WINDOW_SYSTEM */
22335
22336 \f
22337 /***********************************************************************
22338 Exposure Events
22339 ***********************************************************************/
22340
22341 #ifdef HAVE_WINDOW_SYSTEM
22342
22343 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22344 which intersects rectangle R. R is in window-relative coordinates. */
22345
22346 static void
22347 expose_area (w, row, r, area)
22348 struct window *w;
22349 struct glyph_row *row;
22350 XRectangle *r;
22351 enum glyph_row_area area;
22352 {
22353 struct glyph *first = row->glyphs[area];
22354 struct glyph *end = row->glyphs[area] + row->used[area];
22355 struct glyph *last;
22356 int first_x, start_x, x;
22357
22358 if (area == TEXT_AREA && row->fill_line_p)
22359 /* If row extends face to end of line write the whole line. */
22360 draw_glyphs (w, 0, row, area,
22361 0, row->used[area],
22362 DRAW_NORMAL_TEXT, 0);
22363 else
22364 {
22365 /* Set START_X to the window-relative start position for drawing glyphs of
22366 AREA. The first glyph of the text area can be partially visible.
22367 The first glyphs of other areas cannot. */
22368 start_x = window_box_left_offset (w, area);
22369 x = start_x;
22370 if (area == TEXT_AREA)
22371 x += row->x;
22372
22373 /* Find the first glyph that must be redrawn. */
22374 while (first < end
22375 && x + first->pixel_width < r->x)
22376 {
22377 x += first->pixel_width;
22378 ++first;
22379 }
22380
22381 /* Find the last one. */
22382 last = first;
22383 first_x = x;
22384 while (last < end
22385 && x < r->x + r->width)
22386 {
22387 x += last->pixel_width;
22388 ++last;
22389 }
22390
22391 /* Repaint. */
22392 if (last > first)
22393 draw_glyphs (w, first_x - start_x, row, area,
22394 first - row->glyphs[area], last - row->glyphs[area],
22395 DRAW_NORMAL_TEXT, 0);
22396 }
22397 }
22398
22399
22400 /* Redraw the parts of the glyph row ROW on window W intersecting
22401 rectangle R. R is in window-relative coordinates. Value is
22402 non-zero if mouse-face was overwritten. */
22403
22404 static int
22405 expose_line (w, row, r)
22406 struct window *w;
22407 struct glyph_row *row;
22408 XRectangle *r;
22409 {
22410 xassert (row->enabled_p);
22411
22412 if (row->mode_line_p || w->pseudo_window_p)
22413 draw_glyphs (w, 0, row, TEXT_AREA,
22414 0, row->used[TEXT_AREA],
22415 DRAW_NORMAL_TEXT, 0);
22416 else
22417 {
22418 if (row->used[LEFT_MARGIN_AREA])
22419 expose_area (w, row, r, LEFT_MARGIN_AREA);
22420 if (row->used[TEXT_AREA])
22421 expose_area (w, row, r, TEXT_AREA);
22422 if (row->used[RIGHT_MARGIN_AREA])
22423 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22424 draw_row_fringe_bitmaps (w, row);
22425 }
22426
22427 return row->mouse_face_p;
22428 }
22429
22430
22431 /* Redraw those parts of glyphs rows during expose event handling that
22432 overlap other rows. Redrawing of an exposed line writes over parts
22433 of lines overlapping that exposed line; this function fixes that.
22434
22435 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22436 row in W's current matrix that is exposed and overlaps other rows.
22437 LAST_OVERLAPPING_ROW is the last such row. */
22438
22439 static void
22440 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22441 struct window *w;
22442 struct glyph_row *first_overlapping_row;
22443 struct glyph_row *last_overlapping_row;
22444 {
22445 struct glyph_row *row;
22446
22447 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22448 if (row->overlapping_p)
22449 {
22450 xassert (row->enabled_p && !row->mode_line_p);
22451
22452 if (row->used[LEFT_MARGIN_AREA])
22453 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22454
22455 if (row->used[TEXT_AREA])
22456 x_fix_overlapping_area (w, row, TEXT_AREA);
22457
22458 if (row->used[RIGHT_MARGIN_AREA])
22459 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22460 }
22461 }
22462
22463
22464 /* Return non-zero if W's cursor intersects rectangle R. */
22465
22466 static int
22467 phys_cursor_in_rect_p (w, r)
22468 struct window *w;
22469 XRectangle *r;
22470 {
22471 XRectangle cr, result;
22472 struct glyph *cursor_glyph;
22473
22474 cursor_glyph = get_phys_cursor_glyph (w);
22475 if (cursor_glyph)
22476 {
22477 /* r is relative to W's box, but w->phys_cursor.x is relative
22478 to left edge of W's TEXT area. Adjust it. */
22479 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22480 cr.y = w->phys_cursor.y;
22481 cr.width = cursor_glyph->pixel_width;
22482 cr.height = w->phys_cursor_height;
22483 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22484 I assume the effect is the same -- and this is portable. */
22485 return x_intersect_rectangles (&cr, r, &result);
22486 }
22487 else
22488 return 0;
22489 }
22490
22491
22492 /* EXPORT:
22493 Draw a vertical window border to the right of window W if W doesn't
22494 have vertical scroll bars. */
22495
22496 void
22497 x_draw_vertical_border (w)
22498 struct window *w;
22499 {
22500 /* We could do better, if we knew what type of scroll-bar the adjacent
22501 windows (on either side) have... But we don't :-(
22502 However, I think this works ok. ++KFS 2003-04-25 */
22503
22504 /* Redraw borders between horizontally adjacent windows. Don't
22505 do it for frames with vertical scroll bars because either the
22506 right scroll bar of a window, or the left scroll bar of its
22507 neighbor will suffice as a border. */
22508 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22509 return;
22510
22511 if (!WINDOW_RIGHTMOST_P (w)
22512 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22513 {
22514 int x0, x1, y0, y1;
22515
22516 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22517 y1 -= 1;
22518
22519 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22520 x1 -= 1;
22521
22522 rif->draw_vertical_window_border (w, x1, y0, y1);
22523 }
22524 else if (!WINDOW_LEFTMOST_P (w)
22525 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22526 {
22527 int x0, x1, y0, y1;
22528
22529 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22530 y1 -= 1;
22531
22532 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22533 x0 -= 1;
22534
22535 rif->draw_vertical_window_border (w, x0, y0, y1);
22536 }
22537 }
22538
22539
22540 /* Redraw the part of window W intersection rectangle FR. Pixel
22541 coordinates in FR are frame-relative. Call this function with
22542 input blocked. Value is non-zero if the exposure overwrites
22543 mouse-face. */
22544
22545 static int
22546 expose_window (w, fr)
22547 struct window *w;
22548 XRectangle *fr;
22549 {
22550 struct frame *f = XFRAME (w->frame);
22551 XRectangle wr, r;
22552 int mouse_face_overwritten_p = 0;
22553
22554 /* If window is not yet fully initialized, do nothing. This can
22555 happen when toolkit scroll bars are used and a window is split.
22556 Reconfiguring the scroll bar will generate an expose for a newly
22557 created window. */
22558 if (w->current_matrix == NULL)
22559 return 0;
22560
22561 /* When we're currently updating the window, display and current
22562 matrix usually don't agree. Arrange for a thorough display
22563 later. */
22564 if (w == updated_window)
22565 {
22566 SET_FRAME_GARBAGED (f);
22567 return 0;
22568 }
22569
22570 /* Frame-relative pixel rectangle of W. */
22571 wr.x = WINDOW_LEFT_EDGE_X (w);
22572 wr.y = WINDOW_TOP_EDGE_Y (w);
22573 wr.width = WINDOW_TOTAL_WIDTH (w);
22574 wr.height = WINDOW_TOTAL_HEIGHT (w);
22575
22576 if (x_intersect_rectangles (fr, &wr, &r))
22577 {
22578 int yb = window_text_bottom_y (w);
22579 struct glyph_row *row;
22580 int cursor_cleared_p;
22581 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22582
22583 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22584 r.x, r.y, r.width, r.height));
22585
22586 /* Convert to window coordinates. */
22587 r.x -= WINDOW_LEFT_EDGE_X (w);
22588 r.y -= WINDOW_TOP_EDGE_Y (w);
22589
22590 /* Turn off the cursor. */
22591 if (!w->pseudo_window_p
22592 && phys_cursor_in_rect_p (w, &r))
22593 {
22594 x_clear_cursor (w);
22595 cursor_cleared_p = 1;
22596 }
22597 else
22598 cursor_cleared_p = 0;
22599
22600 /* Update lines intersecting rectangle R. */
22601 first_overlapping_row = last_overlapping_row = NULL;
22602 for (row = w->current_matrix->rows;
22603 row->enabled_p;
22604 ++row)
22605 {
22606 int y0 = row->y;
22607 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22608
22609 if ((y0 >= r.y && y0 < r.y + r.height)
22610 || (y1 > r.y && y1 < r.y + r.height)
22611 || (r.y >= y0 && r.y < y1)
22612 || (r.y + r.height > y0 && r.y + r.height < y1))
22613 {
22614 /* A header line may be overlapping, but there is no need
22615 to fix overlapping areas for them. KFS 2005-02-12 */
22616 if (row->overlapping_p && !row->mode_line_p)
22617 {
22618 if (first_overlapping_row == NULL)
22619 first_overlapping_row = row;
22620 last_overlapping_row = row;
22621 }
22622
22623 if (expose_line (w, row, &r))
22624 mouse_face_overwritten_p = 1;
22625 }
22626
22627 if (y1 >= yb)
22628 break;
22629 }
22630
22631 /* Display the mode line if there is one. */
22632 if (WINDOW_WANTS_MODELINE_P (w)
22633 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22634 row->enabled_p)
22635 && row->y < r.y + r.height)
22636 {
22637 if (expose_line (w, row, &r))
22638 mouse_face_overwritten_p = 1;
22639 }
22640
22641 if (!w->pseudo_window_p)
22642 {
22643 /* Fix the display of overlapping rows. */
22644 if (first_overlapping_row)
22645 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22646
22647 /* Draw border between windows. */
22648 x_draw_vertical_border (w);
22649
22650 /* Turn the cursor on again. */
22651 if (cursor_cleared_p)
22652 update_window_cursor (w, 1);
22653 }
22654 }
22655
22656 return mouse_face_overwritten_p;
22657 }
22658
22659
22660
22661 /* Redraw (parts) of all windows in the window tree rooted at W that
22662 intersect R. R contains frame pixel coordinates. Value is
22663 non-zero if the exposure overwrites mouse-face. */
22664
22665 static int
22666 expose_window_tree (w, r)
22667 struct window *w;
22668 XRectangle *r;
22669 {
22670 struct frame *f = XFRAME (w->frame);
22671 int mouse_face_overwritten_p = 0;
22672
22673 while (w && !FRAME_GARBAGED_P (f))
22674 {
22675 if (!NILP (w->hchild))
22676 mouse_face_overwritten_p
22677 |= expose_window_tree (XWINDOW (w->hchild), r);
22678 else if (!NILP (w->vchild))
22679 mouse_face_overwritten_p
22680 |= expose_window_tree (XWINDOW (w->vchild), r);
22681 else
22682 mouse_face_overwritten_p |= expose_window (w, r);
22683
22684 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22685 }
22686
22687 return mouse_face_overwritten_p;
22688 }
22689
22690
22691 /* EXPORT:
22692 Redisplay an exposed area of frame F. X and Y are the upper-left
22693 corner of the exposed rectangle. W and H are width and height of
22694 the exposed area. All are pixel values. W or H zero means redraw
22695 the entire frame. */
22696
22697 void
22698 expose_frame (f, x, y, w, h)
22699 struct frame *f;
22700 int x, y, w, h;
22701 {
22702 XRectangle r;
22703 int mouse_face_overwritten_p = 0;
22704
22705 TRACE ((stderr, "expose_frame "));
22706
22707 /* No need to redraw if frame will be redrawn soon. */
22708 if (FRAME_GARBAGED_P (f))
22709 {
22710 TRACE ((stderr, " garbaged\n"));
22711 return;
22712 }
22713
22714 /* If basic faces haven't been realized yet, there is no point in
22715 trying to redraw anything. This can happen when we get an expose
22716 event while Emacs is starting, e.g. by moving another window. */
22717 if (FRAME_FACE_CACHE (f) == NULL
22718 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22719 {
22720 TRACE ((stderr, " no faces\n"));
22721 return;
22722 }
22723
22724 if (w == 0 || h == 0)
22725 {
22726 r.x = r.y = 0;
22727 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22728 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22729 }
22730 else
22731 {
22732 r.x = x;
22733 r.y = y;
22734 r.width = w;
22735 r.height = h;
22736 }
22737
22738 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22739 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22740
22741 if (WINDOWP (f->tool_bar_window))
22742 mouse_face_overwritten_p
22743 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22744
22745 #ifdef HAVE_X_WINDOWS
22746 #ifndef MSDOS
22747 #ifndef USE_X_TOOLKIT
22748 if (WINDOWP (f->menu_bar_window))
22749 mouse_face_overwritten_p
22750 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22751 #endif /* not USE_X_TOOLKIT */
22752 #endif
22753 #endif
22754
22755 /* Some window managers support a focus-follows-mouse style with
22756 delayed raising of frames. Imagine a partially obscured frame,
22757 and moving the mouse into partially obscured mouse-face on that
22758 frame. The visible part of the mouse-face will be highlighted,
22759 then the WM raises the obscured frame. With at least one WM, KDE
22760 2.1, Emacs is not getting any event for the raising of the frame
22761 (even tried with SubstructureRedirectMask), only Expose events.
22762 These expose events will draw text normally, i.e. not
22763 highlighted. Which means we must redo the highlight here.
22764 Subsume it under ``we love X''. --gerd 2001-08-15 */
22765 /* Included in Windows version because Windows most likely does not
22766 do the right thing if any third party tool offers
22767 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22768 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22769 {
22770 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22771 if (f == dpyinfo->mouse_face_mouse_frame)
22772 {
22773 int x = dpyinfo->mouse_face_mouse_x;
22774 int y = dpyinfo->mouse_face_mouse_y;
22775 clear_mouse_face (dpyinfo);
22776 note_mouse_highlight (f, x, y);
22777 }
22778 }
22779 }
22780
22781
22782 /* EXPORT:
22783 Determine the intersection of two rectangles R1 and R2. Return
22784 the intersection in *RESULT. Value is non-zero if RESULT is not
22785 empty. */
22786
22787 int
22788 x_intersect_rectangles (r1, r2, result)
22789 XRectangle *r1, *r2, *result;
22790 {
22791 XRectangle *left, *right;
22792 XRectangle *upper, *lower;
22793 int intersection_p = 0;
22794
22795 /* Rearrange so that R1 is the left-most rectangle. */
22796 if (r1->x < r2->x)
22797 left = r1, right = r2;
22798 else
22799 left = r2, right = r1;
22800
22801 /* X0 of the intersection is right.x0, if this is inside R1,
22802 otherwise there is no intersection. */
22803 if (right->x <= left->x + left->width)
22804 {
22805 result->x = right->x;
22806
22807 /* The right end of the intersection is the minimum of the
22808 the right ends of left and right. */
22809 result->width = (min (left->x + left->width, right->x + right->width)
22810 - result->x);
22811
22812 /* Same game for Y. */
22813 if (r1->y < r2->y)
22814 upper = r1, lower = r2;
22815 else
22816 upper = r2, lower = r1;
22817
22818 /* The upper end of the intersection is lower.y0, if this is inside
22819 of upper. Otherwise, there is no intersection. */
22820 if (lower->y <= upper->y + upper->height)
22821 {
22822 result->y = lower->y;
22823
22824 /* The lower end of the intersection is the minimum of the lower
22825 ends of upper and lower. */
22826 result->height = (min (lower->y + lower->height,
22827 upper->y + upper->height)
22828 - result->y);
22829 intersection_p = 1;
22830 }
22831 }
22832
22833 return intersection_p;
22834 }
22835
22836 #endif /* HAVE_WINDOW_SYSTEM */
22837
22838 \f
22839 /***********************************************************************
22840 Initialization
22841 ***********************************************************************/
22842
22843 void
22844 syms_of_xdisp ()
22845 {
22846 Vwith_echo_area_save_vector = Qnil;
22847 staticpro (&Vwith_echo_area_save_vector);
22848
22849 Vmessage_stack = Qnil;
22850 staticpro (&Vmessage_stack);
22851
22852 Qinhibit_redisplay = intern ("inhibit-redisplay");
22853 staticpro (&Qinhibit_redisplay);
22854
22855 message_dolog_marker1 = Fmake_marker ();
22856 staticpro (&message_dolog_marker1);
22857 message_dolog_marker2 = Fmake_marker ();
22858 staticpro (&message_dolog_marker2);
22859 message_dolog_marker3 = Fmake_marker ();
22860 staticpro (&message_dolog_marker3);
22861
22862 #if GLYPH_DEBUG
22863 defsubr (&Sdump_frame_glyph_matrix);
22864 defsubr (&Sdump_glyph_matrix);
22865 defsubr (&Sdump_glyph_row);
22866 defsubr (&Sdump_tool_bar_row);
22867 defsubr (&Strace_redisplay);
22868 defsubr (&Strace_to_stderr);
22869 #endif
22870 #ifdef HAVE_WINDOW_SYSTEM
22871 defsubr (&Stool_bar_lines_needed);
22872 defsubr (&Slookup_image_map);
22873 #endif
22874 defsubr (&Sformat_mode_line);
22875
22876 staticpro (&Qmenu_bar_update_hook);
22877 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22878
22879 staticpro (&Qoverriding_terminal_local_map);
22880 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22881
22882 staticpro (&Qoverriding_local_map);
22883 Qoverriding_local_map = intern ("overriding-local-map");
22884
22885 staticpro (&Qwindow_scroll_functions);
22886 Qwindow_scroll_functions = intern ("window-scroll-functions");
22887
22888 staticpro (&Qredisplay_end_trigger_functions);
22889 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22890
22891 staticpro (&Qinhibit_point_motion_hooks);
22892 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22893
22894 QCdata = intern (":data");
22895 staticpro (&QCdata);
22896 Qdisplay = intern ("display");
22897 staticpro (&Qdisplay);
22898 Qspace_width = intern ("space-width");
22899 staticpro (&Qspace_width);
22900 Qraise = intern ("raise");
22901 staticpro (&Qraise);
22902 Qslice = intern ("slice");
22903 staticpro (&Qslice);
22904 Qspace = intern ("space");
22905 staticpro (&Qspace);
22906 Qmargin = intern ("margin");
22907 staticpro (&Qmargin);
22908 Qpointer = intern ("pointer");
22909 staticpro (&Qpointer);
22910 Qleft_margin = intern ("left-margin");
22911 staticpro (&Qleft_margin);
22912 Qright_margin = intern ("right-margin");
22913 staticpro (&Qright_margin);
22914 Qcenter = intern ("center");
22915 staticpro (&Qcenter);
22916 Qline_height = intern ("line-height");
22917 staticpro (&Qline_height);
22918 QCalign_to = intern (":align-to");
22919 staticpro (&QCalign_to);
22920 QCrelative_width = intern (":relative-width");
22921 staticpro (&QCrelative_width);
22922 QCrelative_height = intern (":relative-height");
22923 staticpro (&QCrelative_height);
22924 QCeval = intern (":eval");
22925 staticpro (&QCeval);
22926 QCpropertize = intern (":propertize");
22927 staticpro (&QCpropertize);
22928 QCfile = intern (":file");
22929 staticpro (&QCfile);
22930 Qfontified = intern ("fontified");
22931 staticpro (&Qfontified);
22932 Qfontification_functions = intern ("fontification-functions");
22933 staticpro (&Qfontification_functions);
22934 Qtrailing_whitespace = intern ("trailing-whitespace");
22935 staticpro (&Qtrailing_whitespace);
22936 Qescape_glyph = intern ("escape-glyph");
22937 staticpro (&Qescape_glyph);
22938 Qnobreak_space = intern ("nobreak-space");
22939 staticpro (&Qnobreak_space);
22940 Qimage = intern ("image");
22941 staticpro (&Qimage);
22942 QCmap = intern (":map");
22943 staticpro (&QCmap);
22944 QCpointer = intern (":pointer");
22945 staticpro (&QCpointer);
22946 Qrect = intern ("rect");
22947 staticpro (&Qrect);
22948 Qcircle = intern ("circle");
22949 staticpro (&Qcircle);
22950 Qpoly = intern ("poly");
22951 staticpro (&Qpoly);
22952 Qmessage_truncate_lines = intern ("message-truncate-lines");
22953 staticpro (&Qmessage_truncate_lines);
22954 Qgrow_only = intern ("grow-only");
22955 staticpro (&Qgrow_only);
22956 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22957 staticpro (&Qinhibit_menubar_update);
22958 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22959 staticpro (&Qinhibit_eval_during_redisplay);
22960 Qposition = intern ("position");
22961 staticpro (&Qposition);
22962 Qbuffer_position = intern ("buffer-position");
22963 staticpro (&Qbuffer_position);
22964 Qobject = intern ("object");
22965 staticpro (&Qobject);
22966 Qbar = intern ("bar");
22967 staticpro (&Qbar);
22968 Qhbar = intern ("hbar");
22969 staticpro (&Qhbar);
22970 Qbox = intern ("box");
22971 staticpro (&Qbox);
22972 Qhollow = intern ("hollow");
22973 staticpro (&Qhollow);
22974 Qhand = intern ("hand");
22975 staticpro (&Qhand);
22976 Qarrow = intern ("arrow");
22977 staticpro (&Qarrow);
22978 Qtext = intern ("text");
22979 staticpro (&Qtext);
22980 Qrisky_local_variable = intern ("risky-local-variable");
22981 staticpro (&Qrisky_local_variable);
22982 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22983 staticpro (&Qinhibit_free_realized_faces);
22984
22985 list_of_error = Fcons (Fcons (intern ("error"),
22986 Fcons (intern ("void-variable"), Qnil)),
22987 Qnil);
22988 staticpro (&list_of_error);
22989
22990 Qlast_arrow_position = intern ("last-arrow-position");
22991 staticpro (&Qlast_arrow_position);
22992 Qlast_arrow_string = intern ("last-arrow-string");
22993 staticpro (&Qlast_arrow_string);
22994
22995 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22996 staticpro (&Qoverlay_arrow_string);
22997 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22998 staticpro (&Qoverlay_arrow_bitmap);
22999
23000 echo_buffer[0] = echo_buffer[1] = Qnil;
23001 staticpro (&echo_buffer[0]);
23002 staticpro (&echo_buffer[1]);
23003
23004 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23005 staticpro (&echo_area_buffer[0]);
23006 staticpro (&echo_area_buffer[1]);
23007
23008 Vmessages_buffer_name = build_string ("*Messages*");
23009 staticpro (&Vmessages_buffer_name);
23010
23011 mode_line_proptrans_alist = Qnil;
23012 staticpro (&mode_line_proptrans_alist);
23013 mode_line_string_list = Qnil;
23014 staticpro (&mode_line_string_list);
23015 mode_line_string_face = Qnil;
23016 staticpro (&mode_line_string_face);
23017 mode_line_string_face_prop = Qnil;
23018 staticpro (&mode_line_string_face_prop);
23019 Vmode_line_unwind_vector = Qnil;
23020 staticpro (&Vmode_line_unwind_vector);
23021
23022 help_echo_string = Qnil;
23023 staticpro (&help_echo_string);
23024 help_echo_object = Qnil;
23025 staticpro (&help_echo_object);
23026 help_echo_window = Qnil;
23027 staticpro (&help_echo_window);
23028 previous_help_echo_string = Qnil;
23029 staticpro (&previous_help_echo_string);
23030 help_echo_pos = -1;
23031
23032 #ifdef HAVE_WINDOW_SYSTEM
23033 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23034 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23035 For example, if a block cursor is over a tab, it will be drawn as
23036 wide as that tab on the display. */);
23037 x_stretch_cursor_p = 0;
23038 #endif
23039
23040 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23041 doc: /* *Non-nil means highlight trailing whitespace.
23042 The face used for trailing whitespace is `trailing-whitespace'. */);
23043 Vshow_trailing_whitespace = Qnil;
23044
23045 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23046 doc: /* *Control highlighting of nobreak space and soft hyphen.
23047 A value of t means highlight the character itself (for nobreak space,
23048 use face `nobreak-space').
23049 A value of nil means no highlighting.
23050 Other values mean display the escape glyph followed by an ordinary
23051 space or ordinary hyphen. */);
23052 Vnobreak_char_display = Qt;
23053
23054 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23055 doc: /* *The pointer shape to show in void text areas.
23056 A value of nil means to show the text pointer. Other options are `arrow',
23057 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23058 Vvoid_text_area_pointer = Qarrow;
23059
23060 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23061 doc: /* Non-nil means don't actually do any redisplay.
23062 This is used for internal purposes. */);
23063 Vinhibit_redisplay = Qnil;
23064
23065 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23066 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23067 Vglobal_mode_string = Qnil;
23068
23069 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23070 doc: /* Marker for where to display an arrow on top of the buffer text.
23071 This must be the beginning of a line in order to work.
23072 See also `overlay-arrow-string'. */);
23073 Voverlay_arrow_position = Qnil;
23074
23075 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23076 doc: /* String to display as an arrow in non-window frames.
23077 See also `overlay-arrow-position'. */);
23078 Voverlay_arrow_string = build_string ("=>");
23079
23080 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23081 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23082 The symbols on this list are examined during redisplay to determine
23083 where to display overlay arrows. */);
23084 Voverlay_arrow_variable_list
23085 = Fcons (intern ("overlay-arrow-position"), Qnil);
23086
23087 DEFVAR_INT ("scroll-step", &scroll_step,
23088 doc: /* *The number of lines to try scrolling a window by when point moves out.
23089 If that fails to bring point back on frame, point is centered instead.
23090 If this is zero, point is always centered after it moves off frame.
23091 If you want scrolling to always be a line at a time, you should set
23092 `scroll-conservatively' to a large value rather than set this to 1. */);
23093
23094 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23095 doc: /* *Scroll up to this many lines, to bring point back on screen.
23096 A value of zero means to scroll the text to center point vertically
23097 in the window. */);
23098 scroll_conservatively = 0;
23099
23100 DEFVAR_INT ("scroll-margin", &scroll_margin,
23101 doc: /* *Number of lines of margin at the top and bottom of a window.
23102 Recenter the window whenever point gets within this many lines
23103 of the top or bottom of the window. */);
23104 scroll_margin = 0;
23105
23106 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23107 doc: /* Pixels per inch value for non-window system displays.
23108 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23109 Vdisplay_pixels_per_inch = make_float (72.0);
23110
23111 #if GLYPH_DEBUG
23112 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23113 #endif
23114
23115 DEFVAR_BOOL ("truncate-partial-width-windows",
23116 &truncate_partial_width_windows,
23117 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23118 truncate_partial_width_windows = 1;
23119
23120 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23121 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23122 Any other value means to use the appropriate face, `mode-line',
23123 `header-line', or `menu' respectively. */);
23124 mode_line_inverse_video = 1;
23125
23126 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23127 doc: /* *Maximum buffer size for which line number should be displayed.
23128 If the buffer is bigger than this, the line number does not appear
23129 in the mode line. A value of nil means no limit. */);
23130 Vline_number_display_limit = Qnil;
23131
23132 DEFVAR_INT ("line-number-display-limit-width",
23133 &line_number_display_limit_width,
23134 doc: /* *Maximum line width (in characters) for line number display.
23135 If the average length of the lines near point is bigger than this, then the
23136 line number may be omitted from the mode line. */);
23137 line_number_display_limit_width = 200;
23138
23139 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23140 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23141 highlight_nonselected_windows = 0;
23142
23143 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23144 doc: /* Non-nil if more than one frame is visible on this display.
23145 Minibuffer-only frames don't count, but iconified frames do.
23146 This variable is not guaranteed to be accurate except while processing
23147 `frame-title-format' and `icon-title-format'. */);
23148
23149 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23150 doc: /* Template for displaying the title bar of visible frames.
23151 \(Assuming the window manager supports this feature.)
23152 This variable has the same structure as `mode-line-format' (which see),
23153 and is used only on frames for which no explicit name has been set
23154 \(see `modify-frame-parameters'). */);
23155
23156 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23157 doc: /* Template for displaying the title bar of an iconified frame.
23158 \(Assuming the window manager supports this feature.)
23159 This variable has the same structure as `mode-line-format' (which see),
23160 and is used only on frames for which no explicit name has been set
23161 \(see `modify-frame-parameters'). */);
23162 Vicon_title_format
23163 = Vframe_title_format
23164 = Fcons (intern ("multiple-frames"),
23165 Fcons (build_string ("%b"),
23166 Fcons (Fcons (empty_string,
23167 Fcons (intern ("invocation-name"),
23168 Fcons (build_string ("@"),
23169 Fcons (intern ("system-name"),
23170 Qnil)))),
23171 Qnil)));
23172
23173 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23174 doc: /* Maximum number of lines to keep in the message log buffer.
23175 If nil, disable message logging. If t, log messages but don't truncate
23176 the buffer when it becomes large. */);
23177 Vmessage_log_max = make_number (50);
23178
23179 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23180 doc: /* Functions called before redisplay, if window sizes have changed.
23181 The value should be a list of functions that take one argument.
23182 Just before redisplay, for each frame, if any of its windows have changed
23183 size since the last redisplay, or have been split or deleted,
23184 all the functions in the list are called, with the frame as argument. */);
23185 Vwindow_size_change_functions = Qnil;
23186
23187 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23188 doc: /* List of functions to call before redisplaying a window with scrolling.
23189 Each function is called with two arguments, the window
23190 and its new display-start position. Note that the value of `window-end'
23191 is not valid when these functions are called. */);
23192 Vwindow_scroll_functions = Qnil;
23193
23194 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23195 doc: /* Functions called when redisplay of a window reaches the end trigger.
23196 Each function is called with two arguments, the window and the end trigger value.
23197 See `set-window-redisplay-end-trigger'. */);
23198 Vredisplay_end_trigger_functions = Qnil;
23199
23200 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23201 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23202 mouse_autoselect_window = 0;
23203
23204 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23205 doc: /* *Non-nil means automatically resize tool-bars.
23206 This increases a tool-bar's height if not all tool-bar items are visible.
23207 It decreases a tool-bar's height when it would display blank lines
23208 otherwise. */);
23209 auto_resize_tool_bars_p = 1;
23210
23211 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23212 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23213 auto_raise_tool_bar_buttons_p = 1;
23214
23215 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23216 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23217 make_cursor_line_fully_visible_p = 1;
23218
23219 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23220 doc: /* *Margin around tool-bar buttons in pixels.
23221 If an integer, use that for both horizontal and vertical margins.
23222 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23223 HORZ specifying the horizontal margin, and VERT specifying the
23224 vertical margin. */);
23225 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23226
23227 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23228 doc: /* *Relief thickness of tool-bar buttons. */);
23229 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23230
23231 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23232 doc: /* List of functions to call to fontify regions of text.
23233 Each function is called with one argument POS. Functions must
23234 fontify a region starting at POS in the current buffer, and give
23235 fontified regions the property `fontified'. */);
23236 Vfontification_functions = Qnil;
23237 Fmake_variable_buffer_local (Qfontification_functions);
23238
23239 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23240 &unibyte_display_via_language_environment,
23241 doc: /* *Non-nil means display unibyte text according to language environment.
23242 Specifically this means that unibyte non-ASCII characters
23243 are displayed by converting them to the equivalent multibyte characters
23244 according to the current language environment. As a result, they are
23245 displayed according to the current fontset. */);
23246 unibyte_display_via_language_environment = 0;
23247
23248 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23249 doc: /* *Maximum height for resizing mini-windows.
23250 If a float, it specifies a fraction of the mini-window frame's height.
23251 If an integer, it specifies a number of lines. */);
23252 Vmax_mini_window_height = make_float (0.25);
23253
23254 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23255 doc: /* *How to resize mini-windows.
23256 A value of nil means don't automatically resize mini-windows.
23257 A value of t means resize them to fit the text displayed in them.
23258 A value of `grow-only', the default, means let mini-windows grow
23259 only, until their display becomes empty, at which point the windows
23260 go back to their normal size. */);
23261 Vresize_mini_windows = Qgrow_only;
23262
23263 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23264 doc: /* Alist specifying how to blink the cursor off.
23265 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23266 `cursor-type' frame-parameter or variable equals ON-STATE,
23267 comparing using `equal', Emacs uses OFF-STATE to specify
23268 how to blink it off. */);
23269 Vblink_cursor_alist = Qnil;
23270
23271 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23272 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23273 automatic_hscrolling_p = 1;
23274
23275 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23276 doc: /* *How many columns away from the window edge point is allowed to get
23277 before automatic hscrolling will horizontally scroll the window. */);
23278 hscroll_margin = 5;
23279
23280 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23281 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23282 When point is less than `automatic-hscroll-margin' columns from the window
23283 edge, automatic hscrolling will scroll the window by the amount of columns
23284 determined by this variable. If its value is a positive integer, scroll that
23285 many columns. If it's a positive floating-point number, it specifies the
23286 fraction of the window's width to scroll. If it's nil or zero, point will be
23287 centered horizontally after the scroll. Any other value, including negative
23288 numbers, are treated as if the value were zero.
23289
23290 Automatic hscrolling always moves point outside the scroll margin, so if
23291 point was more than scroll step columns inside the margin, the window will
23292 scroll more than the value given by the scroll step.
23293
23294 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23295 and `scroll-right' overrides this variable's effect. */);
23296 Vhscroll_step = make_number (0);
23297
23298 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23299 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23300 Bind this around calls to `message' to let it take effect. */);
23301 message_truncate_lines = 0;
23302
23303 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23304 doc: /* Normal hook run to update the menu bar definitions.
23305 Redisplay runs this hook before it redisplays the menu bar.
23306 This is used to update submenus such as Buffers,
23307 whose contents depend on various data. */);
23308 Vmenu_bar_update_hook = Qnil;
23309
23310 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23311 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23312 inhibit_menubar_update = 0;
23313
23314 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23315 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23316 inhibit_eval_during_redisplay = 0;
23317
23318 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23319 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23320 inhibit_free_realized_faces = 0;
23321
23322 #if GLYPH_DEBUG
23323 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23324 doc: /* Inhibit try_window_id display optimization. */);
23325 inhibit_try_window_id = 0;
23326
23327 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23328 doc: /* Inhibit try_window_reusing display optimization. */);
23329 inhibit_try_window_reusing = 0;
23330
23331 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23332 doc: /* Inhibit try_cursor_movement display optimization. */);
23333 inhibit_try_cursor_movement = 0;
23334 #endif /* GLYPH_DEBUG */
23335 }
23336
23337
23338 /* Initialize this module when Emacs starts. */
23339
23340 void
23341 init_xdisp ()
23342 {
23343 Lisp_Object root_window;
23344 struct window *mini_w;
23345
23346 current_header_line_height = current_mode_line_height = -1;
23347
23348 CHARPOS (this_line_start_pos) = 0;
23349
23350 mini_w = XWINDOW (minibuf_window);
23351 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23352
23353 if (!noninteractive)
23354 {
23355 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23356 int i;
23357
23358 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23359 set_window_height (root_window,
23360 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23361 0);
23362 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23363 set_window_height (minibuf_window, 1, 0);
23364
23365 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23366 mini_w->total_cols = make_number (FRAME_COLS (f));
23367
23368 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23369 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23370 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23371
23372 /* The default ellipsis glyphs `...'. */
23373 for (i = 0; i < 3; ++i)
23374 default_invis_vector[i] = make_number ('.');
23375 }
23376
23377 {
23378 /* Allocate the buffer for frame titles.
23379 Also used for `format-mode-line'. */
23380 int size = 100;
23381 mode_line_noprop_buf = (char *) xmalloc (size);
23382 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23383 mode_line_noprop_ptr = mode_line_noprop_buf;
23384 mode_line_target = MODE_LINE_DISPLAY;
23385 }
23386
23387 help_echo_showing_p = 0;
23388 }
23389
23390
23391 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23392 (do not change this comment) */