]> code.delx.au - gnu-emacs/blob - src/xdisp.c
* xlwmenu.c (find_next_selectable):
[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 This may GC (insert may run before/after change hooks),
6758 so the buffer M must NOT point to a Lisp string. */
6759
6760 void
6761 message_dolog (m, nbytes, nlflag, multibyte)
6762 const char *m;
6763 int nbytes, nlflag, multibyte;
6764 {
6765 if (!NILP (Vmemory_full))
6766 return;
6767
6768 if (!NILP (Vmessage_log_max))
6769 {
6770 struct buffer *oldbuf;
6771 Lisp_Object oldpoint, oldbegv, oldzv;
6772 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6773 int point_at_end = 0;
6774 int zv_at_end = 0;
6775 Lisp_Object old_deactivate_mark, tem;
6776 struct gcpro gcpro1;
6777
6778 old_deactivate_mark = Vdeactivate_mark;
6779 oldbuf = current_buffer;
6780 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6781 current_buffer->undo_list = Qt;
6782
6783 oldpoint = message_dolog_marker1;
6784 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6785 oldbegv = message_dolog_marker2;
6786 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6787 oldzv = message_dolog_marker3;
6788 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6789 GCPRO1 (old_deactivate_mark);
6790
6791 if (PT == Z)
6792 point_at_end = 1;
6793 if (ZV == Z)
6794 zv_at_end = 1;
6795
6796 BEGV = BEG;
6797 BEGV_BYTE = BEG_BYTE;
6798 ZV = Z;
6799 ZV_BYTE = Z_BYTE;
6800 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6801
6802 /* Insert the string--maybe converting multibyte to single byte
6803 or vice versa, so that all the text fits the buffer. */
6804 if (multibyte
6805 && NILP (current_buffer->enable_multibyte_characters))
6806 {
6807 int i, c, char_bytes;
6808 unsigned char work[1];
6809
6810 /* Convert a multibyte string to single-byte
6811 for the *Message* buffer. */
6812 for (i = 0; i < nbytes; i += char_bytes)
6813 {
6814 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6815 work[0] = (SINGLE_BYTE_CHAR_P (c)
6816 ? c
6817 : multibyte_char_to_unibyte (c, Qnil));
6818 insert_1_both (work, 1, 1, 1, 0, 0);
6819 }
6820 }
6821 else if (! multibyte
6822 && ! NILP (current_buffer->enable_multibyte_characters))
6823 {
6824 int i, c, char_bytes;
6825 unsigned char *msg = (unsigned char *) m;
6826 unsigned char str[MAX_MULTIBYTE_LENGTH];
6827 /* Convert a single-byte string to multibyte
6828 for the *Message* buffer. */
6829 for (i = 0; i < nbytes; i++)
6830 {
6831 c = unibyte_char_to_multibyte (msg[i]);
6832 char_bytes = CHAR_STRING (c, str);
6833 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6834 }
6835 }
6836 else if (nbytes)
6837 insert_1 (m, nbytes, 1, 0, 0);
6838
6839 if (nlflag)
6840 {
6841 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6842 insert_1 ("\n", 1, 1, 0, 0);
6843
6844 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6845 this_bol = PT;
6846 this_bol_byte = PT_BYTE;
6847
6848 /* See if this line duplicates the previous one.
6849 If so, combine duplicates. */
6850 if (this_bol > BEG)
6851 {
6852 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6853 prev_bol = PT;
6854 prev_bol_byte = PT_BYTE;
6855
6856 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6857 this_bol, this_bol_byte);
6858 if (dup)
6859 {
6860 del_range_both (prev_bol, prev_bol_byte,
6861 this_bol, this_bol_byte, 0);
6862 if (dup > 1)
6863 {
6864 char dupstr[40];
6865 int duplen;
6866
6867 /* If you change this format, don't forget to also
6868 change message_log_check_duplicate. */
6869 sprintf (dupstr, " [%d times]", dup);
6870 duplen = strlen (dupstr);
6871 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6872 insert_1 (dupstr, duplen, 1, 0, 1);
6873 }
6874 }
6875 }
6876
6877 /* If we have more than the desired maximum number of lines
6878 in the *Messages* buffer now, delete the oldest ones.
6879 This is safe because we don't have undo in this buffer. */
6880
6881 if (NATNUMP (Vmessage_log_max))
6882 {
6883 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6884 -XFASTINT (Vmessage_log_max) - 1, 0);
6885 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6886 }
6887 }
6888 BEGV = XMARKER (oldbegv)->charpos;
6889 BEGV_BYTE = marker_byte_position (oldbegv);
6890
6891 if (zv_at_end)
6892 {
6893 ZV = Z;
6894 ZV_BYTE = Z_BYTE;
6895 }
6896 else
6897 {
6898 ZV = XMARKER (oldzv)->charpos;
6899 ZV_BYTE = marker_byte_position (oldzv);
6900 }
6901
6902 if (point_at_end)
6903 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6904 else
6905 /* We can't do Fgoto_char (oldpoint) because it will run some
6906 Lisp code. */
6907 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6908 XMARKER (oldpoint)->bytepos);
6909
6910 UNGCPRO;
6911 unchain_marker (XMARKER (oldpoint));
6912 unchain_marker (XMARKER (oldbegv));
6913 unchain_marker (XMARKER (oldzv));
6914
6915 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6916 set_buffer_internal (oldbuf);
6917 if (NILP (tem))
6918 windows_or_buffers_changed = old_windows_or_buffers_changed;
6919 message_log_need_newline = !nlflag;
6920 Vdeactivate_mark = old_deactivate_mark;
6921 }
6922 }
6923
6924
6925 /* We are at the end of the buffer after just having inserted a newline.
6926 (Note: We depend on the fact we won't be crossing the gap.)
6927 Check to see if the most recent message looks a lot like the previous one.
6928 Return 0 if different, 1 if the new one should just replace it, or a
6929 value N > 1 if we should also append " [N times]". */
6930
6931 static int
6932 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6933 int prev_bol, this_bol;
6934 int prev_bol_byte, this_bol_byte;
6935 {
6936 int i;
6937 int len = Z_BYTE - 1 - this_bol_byte;
6938 int seen_dots = 0;
6939 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6940 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6941
6942 for (i = 0; i < len; i++)
6943 {
6944 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6945 seen_dots = 1;
6946 if (p1[i] != p2[i])
6947 return seen_dots;
6948 }
6949 p1 += len;
6950 if (*p1 == '\n')
6951 return 2;
6952 if (*p1++ == ' ' && *p1++ == '[')
6953 {
6954 int n = 0;
6955 while (*p1 >= '0' && *p1 <= '9')
6956 n = n * 10 + *p1++ - '0';
6957 if (strncmp (p1, " times]\n", 8) == 0)
6958 return n+1;
6959 }
6960 return 0;
6961 }
6962 \f
6963
6964 /* Display an echo area message M with a specified length of NBYTES
6965 bytes. The string may include null characters. If M is 0, clear
6966 out any existing message, and let the mini-buffer text show
6967 through.
6968
6969 This may GC, so the buffer M must NOT point to a Lisp string. */
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 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8029 to t before calling set_message_1 (which calls insert).
8030 */
8031
8032 void
8033 set_message (s, string, nbytes, multibyte_p)
8034 const char *s;
8035 Lisp_Object string;
8036 int nbytes, multibyte_p;
8037 {
8038 message_enable_multibyte
8039 = ((s && multibyte_p)
8040 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8041
8042 with_echo_area_buffer (0, 0, set_message_1,
8043 (EMACS_INT) s, string, nbytes, multibyte_p);
8044 message_buf_print = 0;
8045 help_echo_showing_p = 0;
8046 }
8047
8048
8049 /* Helper function for set_message. Arguments have the same meaning
8050 as there, with A1 corresponding to S and A2 corresponding to STRING
8051 This function is called with the echo area buffer being
8052 current. */
8053
8054 static int
8055 set_message_1 (a1, a2, nbytes, multibyte_p)
8056 EMACS_INT a1;
8057 Lisp_Object a2;
8058 EMACS_INT nbytes, multibyte_p;
8059 {
8060 const char *s = (const char *) a1;
8061 Lisp_Object string = a2;
8062
8063 /* Change multibyteness of the echo buffer appropriately. */
8064 if (message_enable_multibyte
8065 != !NILP (current_buffer->enable_multibyte_characters))
8066 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8067
8068 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8069
8070 /* Insert new message at BEG. */
8071 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8072 Ferase_buffer ();
8073
8074 if (STRINGP (string))
8075 {
8076 int nchars;
8077
8078 if (nbytes == 0)
8079 nbytes = SBYTES (string);
8080 nchars = string_byte_to_char (string, nbytes);
8081
8082 /* This function takes care of single/multibyte conversion. We
8083 just have to ensure that the echo area buffer has the right
8084 setting of enable_multibyte_characters. */
8085 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8086 }
8087 else if (s)
8088 {
8089 if (nbytes == 0)
8090 nbytes = strlen (s);
8091
8092 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8093 {
8094 /* Convert from multi-byte to single-byte. */
8095 int i, c, n;
8096 unsigned char work[1];
8097
8098 /* Convert a multibyte string to single-byte. */
8099 for (i = 0; i < nbytes; i += n)
8100 {
8101 c = string_char_and_length (s + i, nbytes - i, &n);
8102 work[0] = (SINGLE_BYTE_CHAR_P (c)
8103 ? c
8104 : multibyte_char_to_unibyte (c, Qnil));
8105 insert_1_both (work, 1, 1, 1, 0, 0);
8106 }
8107 }
8108 else if (!multibyte_p
8109 && !NILP (current_buffer->enable_multibyte_characters))
8110 {
8111 /* Convert from single-byte to multi-byte. */
8112 int i, c, n;
8113 const unsigned char *msg = (const unsigned char *) s;
8114 unsigned char str[MAX_MULTIBYTE_LENGTH];
8115
8116 /* Convert a single-byte string to multibyte. */
8117 for (i = 0; i < nbytes; i++)
8118 {
8119 c = unibyte_char_to_multibyte (msg[i]);
8120 n = CHAR_STRING (c, str);
8121 insert_1_both (str, 1, n, 1, 0, 0);
8122 }
8123 }
8124 else
8125 insert_1 (s, nbytes, 1, 0, 0);
8126 }
8127
8128 return 0;
8129 }
8130
8131
8132 /* Clear messages. CURRENT_P non-zero means clear the current
8133 message. LAST_DISPLAYED_P non-zero means clear the message
8134 last displayed. */
8135
8136 void
8137 clear_message (current_p, last_displayed_p)
8138 int current_p, last_displayed_p;
8139 {
8140 if (current_p)
8141 {
8142 echo_area_buffer[0] = Qnil;
8143 message_cleared_p = 1;
8144 }
8145
8146 if (last_displayed_p)
8147 echo_area_buffer[1] = Qnil;
8148
8149 message_buf_print = 0;
8150 }
8151
8152 /* Clear garbaged frames.
8153
8154 This function is used where the old redisplay called
8155 redraw_garbaged_frames which in turn called redraw_frame which in
8156 turn called clear_frame. The call to clear_frame was a source of
8157 flickering. I believe a clear_frame is not necessary. It should
8158 suffice in the new redisplay to invalidate all current matrices,
8159 and ensure a complete redisplay of all windows. */
8160
8161 static void
8162 clear_garbaged_frames ()
8163 {
8164 if (frame_garbaged)
8165 {
8166 Lisp_Object tail, frame;
8167 int changed_count = 0;
8168
8169 FOR_EACH_FRAME (tail, frame)
8170 {
8171 struct frame *f = XFRAME (frame);
8172
8173 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8174 {
8175 if (f->resized_p)
8176 {
8177 Fredraw_frame (frame);
8178 f->force_flush_display_p = 1;
8179 }
8180 clear_current_matrices (f);
8181 changed_count++;
8182 f->garbaged = 0;
8183 f->resized_p = 0;
8184 }
8185 }
8186
8187 frame_garbaged = 0;
8188 if (changed_count)
8189 ++windows_or_buffers_changed;
8190 }
8191 }
8192
8193
8194 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8195 is non-zero update selected_frame. Value is non-zero if the
8196 mini-windows height has been changed. */
8197
8198 static int
8199 echo_area_display (update_frame_p)
8200 int update_frame_p;
8201 {
8202 Lisp_Object mini_window;
8203 struct window *w;
8204 struct frame *f;
8205 int window_height_changed_p = 0;
8206 struct frame *sf = SELECTED_FRAME ();
8207
8208 mini_window = FRAME_MINIBUF_WINDOW (sf);
8209 w = XWINDOW (mini_window);
8210 f = XFRAME (WINDOW_FRAME (w));
8211
8212 /* Don't display if frame is invisible or not yet initialized. */
8213 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8214 return 0;
8215
8216 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8217 #ifndef MAC_OS8
8218 #ifdef HAVE_WINDOW_SYSTEM
8219 /* When Emacs starts, selected_frame may be a visible terminal
8220 frame, even if we run under a window system. If we let this
8221 through, a message would be displayed on the terminal. */
8222 if (EQ (selected_frame, Vterminal_frame)
8223 && !NILP (Vwindow_system))
8224 return 0;
8225 #endif /* HAVE_WINDOW_SYSTEM */
8226 #endif
8227
8228 /* Redraw garbaged frames. */
8229 if (frame_garbaged)
8230 clear_garbaged_frames ();
8231
8232 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8233 {
8234 echo_area_window = mini_window;
8235 window_height_changed_p = display_echo_area (w);
8236 w->must_be_updated_p = 1;
8237
8238 /* Update the display, unless called from redisplay_internal.
8239 Also don't update the screen during redisplay itself. The
8240 update will happen at the end of redisplay, and an update
8241 here could cause confusion. */
8242 if (update_frame_p && !redisplaying_p)
8243 {
8244 int n = 0;
8245
8246 /* If the display update has been interrupted by pending
8247 input, update mode lines in the frame. Due to the
8248 pending input, it might have been that redisplay hasn't
8249 been called, so that mode lines above the echo area are
8250 garbaged. This looks odd, so we prevent it here. */
8251 if (!display_completed)
8252 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8253
8254 if (window_height_changed_p
8255 /* Don't do this if Emacs is shutting down. Redisplay
8256 needs to run hooks. */
8257 && !NILP (Vrun_hooks))
8258 {
8259 /* Must update other windows. Likewise as in other
8260 cases, don't let this update be interrupted by
8261 pending input. */
8262 int count = SPECPDL_INDEX ();
8263 specbind (Qredisplay_dont_pause, Qt);
8264 windows_or_buffers_changed = 1;
8265 redisplay_internal (0);
8266 unbind_to (count, Qnil);
8267 }
8268 else if (FRAME_WINDOW_P (f) && n == 0)
8269 {
8270 /* Window configuration is the same as before.
8271 Can do with a display update of the echo area,
8272 unless we displayed some mode lines. */
8273 update_single_window (w, 1);
8274 rif->flush_display (f);
8275 }
8276 else
8277 update_frame (f, 1, 1);
8278
8279 /* If cursor is in the echo area, make sure that the next
8280 redisplay displays the minibuffer, so that the cursor will
8281 be replaced with what the minibuffer wants. */
8282 if (cursor_in_echo_area)
8283 ++windows_or_buffers_changed;
8284 }
8285 }
8286 else if (!EQ (mini_window, selected_window))
8287 windows_or_buffers_changed++;
8288
8289 /* The current message is now also the last one displayed. */
8290 echo_area_buffer[1] = echo_area_buffer[0];
8291
8292 /* Prevent redisplay optimization in redisplay_internal by resetting
8293 this_line_start_pos. This is done because the mini-buffer now
8294 displays the message instead of its buffer text. */
8295 if (EQ (mini_window, selected_window))
8296 CHARPOS (this_line_start_pos) = 0;
8297
8298 return window_height_changed_p;
8299 }
8300
8301
8302 \f
8303 /***********************************************************************
8304 Mode Lines and Frame Titles
8305 ***********************************************************************/
8306
8307 /* A buffer for constructing non-propertized mode-line strings and
8308 frame titles in it; allocated from the heap in init_xdisp and
8309 resized as needed in store_mode_line_noprop_char. */
8310
8311 static char *mode_line_noprop_buf;
8312
8313 /* The buffer's end, and a current output position in it. */
8314
8315 static char *mode_line_noprop_buf_end;
8316 static char *mode_line_noprop_ptr;
8317
8318 #define MODE_LINE_NOPROP_LEN(start) \
8319 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8320
8321 static enum {
8322 MODE_LINE_DISPLAY = 0,
8323 MODE_LINE_TITLE,
8324 MODE_LINE_NOPROP,
8325 MODE_LINE_STRING
8326 } mode_line_target;
8327
8328 /* Alist that caches the results of :propertize.
8329 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8330 static Lisp_Object mode_line_proptrans_alist;
8331
8332 /* List of strings making up the mode-line. */
8333 static Lisp_Object mode_line_string_list;
8334
8335 /* Base face property when building propertized mode line string. */
8336 static Lisp_Object mode_line_string_face;
8337 static Lisp_Object mode_line_string_face_prop;
8338
8339
8340 /* Unwind data for mode line strings */
8341
8342 static Lisp_Object Vmode_line_unwind_vector;
8343
8344 static Lisp_Object
8345 format_mode_line_unwind_data (obuf)
8346 struct buffer *obuf;
8347 {
8348 Lisp_Object vector;
8349
8350 /* Reduce consing by keeping one vector in
8351 Vwith_echo_area_save_vector. */
8352 vector = Vmode_line_unwind_vector;
8353 Vmode_line_unwind_vector = Qnil;
8354
8355 if (NILP (vector))
8356 vector = Fmake_vector (make_number (7), Qnil);
8357
8358 AREF (vector, 0) = make_number (mode_line_target);
8359 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8360 AREF (vector, 2) = mode_line_string_list;
8361 AREF (vector, 3) = mode_line_proptrans_alist;
8362 AREF (vector, 4) = mode_line_string_face;
8363 AREF (vector, 5) = mode_line_string_face_prop;
8364
8365 if (obuf)
8366 XSETBUFFER (AREF (vector, 6), obuf);
8367 else
8368 AREF (vector, 6) = Qnil;
8369
8370 return vector;
8371 }
8372
8373 static Lisp_Object
8374 unwind_format_mode_line (vector)
8375 Lisp_Object vector;
8376 {
8377 mode_line_target = XINT (AREF (vector, 0));
8378 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8379 mode_line_string_list = AREF (vector, 2);
8380 mode_line_proptrans_alist = AREF (vector, 3);
8381 mode_line_string_face = AREF (vector, 4);
8382 mode_line_string_face_prop = AREF (vector, 5);
8383
8384 if (!NILP (AREF (vector, 6)))
8385 {
8386 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8387 AREF (vector, 6) = Qnil;
8388 }
8389
8390 Vmode_line_unwind_vector = vector;
8391 return Qnil;
8392 }
8393
8394
8395 /* Store a single character C for the frame title in mode_line_noprop_buf.
8396 Re-allocate mode_line_noprop_buf if necessary. */
8397
8398 static void
8399 #ifdef PROTOTYPES
8400 store_mode_line_noprop_char (char c)
8401 #else
8402 store_mode_line_noprop_char (c)
8403 char c;
8404 #endif
8405 {
8406 /* If output position has reached the end of the allocated buffer,
8407 double the buffer's size. */
8408 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8409 {
8410 int len = MODE_LINE_NOPROP_LEN (0);
8411 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8412 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8413 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8414 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8415 }
8416
8417 *mode_line_noprop_ptr++ = c;
8418 }
8419
8420
8421 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8422 mode_line_noprop_ptr. STR is the string to store. Do not copy
8423 characters that yield more columns than PRECISION; PRECISION <= 0
8424 means copy the whole string. Pad with spaces until FIELD_WIDTH
8425 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8426 pad. Called from display_mode_element when it is used to build a
8427 frame title. */
8428
8429 static int
8430 store_mode_line_noprop (str, field_width, precision)
8431 const unsigned char *str;
8432 int field_width, precision;
8433 {
8434 int n = 0;
8435 int dummy, nbytes;
8436
8437 /* Copy at most PRECISION chars from STR. */
8438 nbytes = strlen (str);
8439 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8440 while (nbytes--)
8441 store_mode_line_noprop_char (*str++);
8442
8443 /* Fill up with spaces until FIELD_WIDTH reached. */
8444 while (field_width > 0
8445 && n < field_width)
8446 {
8447 store_mode_line_noprop_char (' ');
8448 ++n;
8449 }
8450
8451 return n;
8452 }
8453
8454 /***********************************************************************
8455 Frame Titles
8456 ***********************************************************************/
8457
8458 #ifdef HAVE_WINDOW_SYSTEM
8459
8460 /* Set the title of FRAME, if it has changed. The title format is
8461 Vicon_title_format if FRAME is iconified, otherwise it is
8462 frame_title_format. */
8463
8464 static void
8465 x_consider_frame_title (frame)
8466 Lisp_Object frame;
8467 {
8468 struct frame *f = XFRAME (frame);
8469
8470 if (FRAME_WINDOW_P (f)
8471 || FRAME_MINIBUF_ONLY_P (f)
8472 || f->explicit_name)
8473 {
8474 /* Do we have more than one visible frame on this X display? */
8475 Lisp_Object tail;
8476 Lisp_Object fmt;
8477 int title_start;
8478 char *title;
8479 int len;
8480 struct it it;
8481 int count = SPECPDL_INDEX ();
8482
8483 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8484 {
8485 Lisp_Object other_frame = XCAR (tail);
8486 struct frame *tf = XFRAME (other_frame);
8487
8488 if (tf != f
8489 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8490 && !FRAME_MINIBUF_ONLY_P (tf)
8491 && !EQ (other_frame, tip_frame)
8492 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8493 break;
8494 }
8495
8496 /* Set global variable indicating that multiple frames exist. */
8497 multiple_frames = CONSP (tail);
8498
8499 /* Switch to the buffer of selected window of the frame. Set up
8500 mode_line_target so that display_mode_element will output into
8501 mode_line_noprop_buf; then display the title. */
8502 record_unwind_protect (unwind_format_mode_line,
8503 format_mode_line_unwind_data (current_buffer));
8504
8505 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8506 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8507
8508 mode_line_target = MODE_LINE_TITLE;
8509 title_start = MODE_LINE_NOPROP_LEN (0);
8510 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8511 NULL, DEFAULT_FACE_ID);
8512 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8513 len = MODE_LINE_NOPROP_LEN (title_start);
8514 title = mode_line_noprop_buf + title_start;
8515 unbind_to (count, Qnil);
8516
8517 /* Set the title only if it's changed. This avoids consing in
8518 the common case where it hasn't. (If it turns out that we've
8519 already wasted too much time by walking through the list with
8520 display_mode_element, then we might need to optimize at a
8521 higher level than this.) */
8522 if (! STRINGP (f->name)
8523 || SBYTES (f->name) != len
8524 || bcmp (title, SDATA (f->name), len) != 0)
8525 x_implicitly_set_name (f, make_string (title, len), Qnil);
8526 }
8527 }
8528
8529 #endif /* not HAVE_WINDOW_SYSTEM */
8530
8531
8532
8533 \f
8534 /***********************************************************************
8535 Menu Bars
8536 ***********************************************************************/
8537
8538
8539 /* Prepare for redisplay by updating menu-bar item lists when
8540 appropriate. This can call eval. */
8541
8542 void
8543 prepare_menu_bars ()
8544 {
8545 int all_windows;
8546 struct gcpro gcpro1, gcpro2;
8547 struct frame *f;
8548 Lisp_Object tooltip_frame;
8549
8550 #ifdef HAVE_WINDOW_SYSTEM
8551 tooltip_frame = tip_frame;
8552 #else
8553 tooltip_frame = Qnil;
8554 #endif
8555
8556 /* Update all frame titles based on their buffer names, etc. We do
8557 this before the menu bars so that the buffer-menu will show the
8558 up-to-date frame titles. */
8559 #ifdef HAVE_WINDOW_SYSTEM
8560 if (windows_or_buffers_changed || update_mode_lines)
8561 {
8562 Lisp_Object tail, frame;
8563
8564 FOR_EACH_FRAME (tail, frame)
8565 {
8566 f = XFRAME (frame);
8567 if (!EQ (frame, tooltip_frame)
8568 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8569 x_consider_frame_title (frame);
8570 }
8571 }
8572 #endif /* HAVE_WINDOW_SYSTEM */
8573
8574 /* Update the menu bar item lists, if appropriate. This has to be
8575 done before any actual redisplay or generation of display lines. */
8576 all_windows = (update_mode_lines
8577 || buffer_shared > 1
8578 || windows_or_buffers_changed);
8579 if (all_windows)
8580 {
8581 Lisp_Object tail, frame;
8582 int count = SPECPDL_INDEX ();
8583
8584 record_unwind_save_match_data ();
8585
8586 FOR_EACH_FRAME (tail, frame)
8587 {
8588 f = XFRAME (frame);
8589
8590 /* Ignore tooltip frame. */
8591 if (EQ (frame, tooltip_frame))
8592 continue;
8593
8594 /* If a window on this frame changed size, report that to
8595 the user and clear the size-change flag. */
8596 if (FRAME_WINDOW_SIZES_CHANGED (f))
8597 {
8598 Lisp_Object functions;
8599
8600 /* Clear flag first in case we get an error below. */
8601 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8602 functions = Vwindow_size_change_functions;
8603 GCPRO2 (tail, functions);
8604
8605 while (CONSP (functions))
8606 {
8607 call1 (XCAR (functions), frame);
8608 functions = XCDR (functions);
8609 }
8610 UNGCPRO;
8611 }
8612
8613 GCPRO1 (tail);
8614 update_menu_bar (f, 0);
8615 #ifdef HAVE_WINDOW_SYSTEM
8616 update_tool_bar (f, 0);
8617 #endif
8618 UNGCPRO;
8619 }
8620
8621 unbind_to (count, Qnil);
8622 }
8623 else
8624 {
8625 struct frame *sf = SELECTED_FRAME ();
8626 update_menu_bar (sf, 1);
8627 #ifdef HAVE_WINDOW_SYSTEM
8628 update_tool_bar (sf, 1);
8629 #endif
8630 }
8631
8632 /* Motif needs this. See comment in xmenu.c. Turn it off when
8633 pending_menu_activation is not defined. */
8634 #ifdef USE_X_TOOLKIT
8635 pending_menu_activation = 0;
8636 #endif
8637 }
8638
8639
8640 /* Update the menu bar item list for frame F. This has to be done
8641 before we start to fill in any display lines, because it can call
8642 eval.
8643
8644 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8645
8646 static void
8647 update_menu_bar (f, save_match_data)
8648 struct frame *f;
8649 int save_match_data;
8650 {
8651 Lisp_Object window;
8652 register struct window *w;
8653
8654 /* If called recursively during a menu update, do nothing. This can
8655 happen when, for instance, an activate-menubar-hook causes a
8656 redisplay. */
8657 if (inhibit_menubar_update)
8658 return;
8659
8660 window = FRAME_SELECTED_WINDOW (f);
8661 w = XWINDOW (window);
8662
8663 #if 0 /* The if statement below this if statement used to include the
8664 condition !NILP (w->update_mode_line), rather than using
8665 update_mode_lines directly, and this if statement may have
8666 been added to make that condition work. Now the if
8667 statement below matches its comment, this isn't needed. */
8668 if (update_mode_lines)
8669 w->update_mode_line = Qt;
8670 #endif
8671
8672 if (FRAME_WINDOW_P (f)
8673 ?
8674 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8675 || defined (USE_GTK)
8676 FRAME_EXTERNAL_MENU_BAR (f)
8677 #else
8678 FRAME_MENU_BAR_LINES (f) > 0
8679 #endif
8680 : FRAME_MENU_BAR_LINES (f) > 0)
8681 {
8682 /* If the user has switched buffers or windows, we need to
8683 recompute to reflect the new bindings. But we'll
8684 recompute when update_mode_lines is set too; that means
8685 that people can use force-mode-line-update to request
8686 that the menu bar be recomputed. The adverse effect on
8687 the rest of the redisplay algorithm is about the same as
8688 windows_or_buffers_changed anyway. */
8689 if (windows_or_buffers_changed
8690 /* This used to test w->update_mode_line, but we believe
8691 there is no need to recompute the menu in that case. */
8692 || update_mode_lines
8693 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8694 < BUF_MODIFF (XBUFFER (w->buffer)))
8695 != !NILP (w->last_had_star))
8696 || ((!NILP (Vtransient_mark_mode)
8697 && !NILP (XBUFFER (w->buffer)->mark_active))
8698 != !NILP (w->region_showing)))
8699 {
8700 struct buffer *prev = current_buffer;
8701 int count = SPECPDL_INDEX ();
8702
8703 specbind (Qinhibit_menubar_update, Qt);
8704
8705 set_buffer_internal_1 (XBUFFER (w->buffer));
8706 if (save_match_data)
8707 record_unwind_save_match_data ();
8708 if (NILP (Voverriding_local_map_menu_flag))
8709 {
8710 specbind (Qoverriding_terminal_local_map, Qnil);
8711 specbind (Qoverriding_local_map, Qnil);
8712 }
8713
8714 /* Run the Lucid hook. */
8715 safe_run_hooks (Qactivate_menubar_hook);
8716
8717 /* If it has changed current-menubar from previous value,
8718 really recompute the menu-bar from the value. */
8719 if (! NILP (Vlucid_menu_bar_dirty_flag))
8720 call0 (Qrecompute_lucid_menubar);
8721
8722 safe_run_hooks (Qmenu_bar_update_hook);
8723 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8724
8725 /* Redisplay the menu bar in case we changed it. */
8726 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8727 || defined (USE_GTK)
8728 if (FRAME_WINDOW_P (f)
8729 #if defined (MAC_OS)
8730 /* All frames on Mac OS share the same menubar. So only the
8731 selected frame should be allowed to set it. */
8732 && f == SELECTED_FRAME ()
8733 #endif
8734 )
8735 set_frame_menubar (f, 0, 0);
8736 else
8737 /* On a terminal screen, the menu bar is an ordinary screen
8738 line, and this makes it get updated. */
8739 w->update_mode_line = Qt;
8740 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8741 /* In the non-toolkit version, the menu bar is an ordinary screen
8742 line, and this makes it get updated. */
8743 w->update_mode_line = Qt;
8744 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8745
8746 unbind_to (count, Qnil);
8747 set_buffer_internal_1 (prev);
8748 }
8749 }
8750 }
8751
8752
8753 \f
8754 /***********************************************************************
8755 Output Cursor
8756 ***********************************************************************/
8757
8758 #ifdef HAVE_WINDOW_SYSTEM
8759
8760 /* EXPORT:
8761 Nominal cursor position -- where to draw output.
8762 HPOS and VPOS are window relative glyph matrix coordinates.
8763 X and Y are window relative pixel coordinates. */
8764
8765 struct cursor_pos output_cursor;
8766
8767
8768 /* EXPORT:
8769 Set the global variable output_cursor to CURSOR. All cursor
8770 positions are relative to updated_window. */
8771
8772 void
8773 set_output_cursor (cursor)
8774 struct cursor_pos *cursor;
8775 {
8776 output_cursor.hpos = cursor->hpos;
8777 output_cursor.vpos = cursor->vpos;
8778 output_cursor.x = cursor->x;
8779 output_cursor.y = cursor->y;
8780 }
8781
8782
8783 /* EXPORT for RIF:
8784 Set a nominal cursor position.
8785
8786 HPOS and VPOS are column/row positions in a window glyph matrix. X
8787 and Y are window text area relative pixel positions.
8788
8789 If this is done during an update, updated_window will contain the
8790 window that is being updated and the position is the future output
8791 cursor position for that window. If updated_window is null, use
8792 selected_window and display the cursor at the given position. */
8793
8794 void
8795 x_cursor_to (vpos, hpos, y, x)
8796 int vpos, hpos, y, x;
8797 {
8798 struct window *w;
8799
8800 /* If updated_window is not set, work on selected_window. */
8801 if (updated_window)
8802 w = updated_window;
8803 else
8804 w = XWINDOW (selected_window);
8805
8806 /* Set the output cursor. */
8807 output_cursor.hpos = hpos;
8808 output_cursor.vpos = vpos;
8809 output_cursor.x = x;
8810 output_cursor.y = y;
8811
8812 /* If not called as part of an update, really display the cursor.
8813 This will also set the cursor position of W. */
8814 if (updated_window == NULL)
8815 {
8816 BLOCK_INPUT;
8817 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8818 if (rif->flush_display_optional)
8819 rif->flush_display_optional (SELECTED_FRAME ());
8820 UNBLOCK_INPUT;
8821 }
8822 }
8823
8824 #endif /* HAVE_WINDOW_SYSTEM */
8825
8826 \f
8827 /***********************************************************************
8828 Tool-bars
8829 ***********************************************************************/
8830
8831 #ifdef HAVE_WINDOW_SYSTEM
8832
8833 /* Where the mouse was last time we reported a mouse event. */
8834
8835 FRAME_PTR last_mouse_frame;
8836
8837 /* Tool-bar item index of the item on which a mouse button was pressed
8838 or -1. */
8839
8840 int last_tool_bar_item;
8841
8842
8843 /* Update the tool-bar item list for frame F. This has to be done
8844 before we start to fill in any display lines. Called from
8845 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8846 and restore it here. */
8847
8848 static void
8849 update_tool_bar (f, save_match_data)
8850 struct frame *f;
8851 int save_match_data;
8852 {
8853 #ifdef USE_GTK
8854 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8855 #else
8856 int do_update = WINDOWP (f->tool_bar_window)
8857 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8858 #endif
8859
8860 if (do_update)
8861 {
8862 Lisp_Object window;
8863 struct window *w;
8864
8865 window = FRAME_SELECTED_WINDOW (f);
8866 w = XWINDOW (window);
8867
8868 /* If the user has switched buffers or windows, we need to
8869 recompute to reflect the new bindings. But we'll
8870 recompute when update_mode_lines is set too; that means
8871 that people can use force-mode-line-update to request
8872 that the menu bar be recomputed. The adverse effect on
8873 the rest of the redisplay algorithm is about the same as
8874 windows_or_buffers_changed anyway. */
8875 if (windows_or_buffers_changed
8876 || !NILP (w->update_mode_line)
8877 || update_mode_lines
8878 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8879 < BUF_MODIFF (XBUFFER (w->buffer)))
8880 != !NILP (w->last_had_star))
8881 || ((!NILP (Vtransient_mark_mode)
8882 && !NILP (XBUFFER (w->buffer)->mark_active))
8883 != !NILP (w->region_showing)))
8884 {
8885 struct buffer *prev = current_buffer;
8886 int count = SPECPDL_INDEX ();
8887 Lisp_Object new_tool_bar;
8888 int new_n_tool_bar;
8889 struct gcpro gcpro1;
8890
8891 /* Set current_buffer to the buffer of the selected
8892 window of the frame, so that we get the right local
8893 keymaps. */
8894 set_buffer_internal_1 (XBUFFER (w->buffer));
8895
8896 /* Save match data, if we must. */
8897 if (save_match_data)
8898 record_unwind_save_match_data ();
8899
8900 /* Make sure that we don't accidentally use bogus keymaps. */
8901 if (NILP (Voverriding_local_map_menu_flag))
8902 {
8903 specbind (Qoverriding_terminal_local_map, Qnil);
8904 specbind (Qoverriding_local_map, Qnil);
8905 }
8906
8907 GCPRO1 (new_tool_bar);
8908
8909 /* Build desired tool-bar items from keymaps. */
8910 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8911 &new_n_tool_bar);
8912
8913 /* Redisplay the tool-bar if we changed it. */
8914 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8915 {
8916 /* Redisplay that happens asynchronously due to an expose event
8917 may access f->tool_bar_items. Make sure we update both
8918 variables within BLOCK_INPUT so no such event interrupts. */
8919 BLOCK_INPUT;
8920 f->tool_bar_items = new_tool_bar;
8921 f->n_tool_bar_items = new_n_tool_bar;
8922 w->update_mode_line = Qt;
8923 UNBLOCK_INPUT;
8924 }
8925
8926 UNGCPRO;
8927
8928 unbind_to (count, Qnil);
8929 set_buffer_internal_1 (prev);
8930 }
8931 }
8932 }
8933
8934
8935 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8936 F's desired tool-bar contents. F->tool_bar_items must have
8937 been set up previously by calling prepare_menu_bars. */
8938
8939 static void
8940 build_desired_tool_bar_string (f)
8941 struct frame *f;
8942 {
8943 int i, size, size_needed;
8944 struct gcpro gcpro1, gcpro2, gcpro3;
8945 Lisp_Object image, plist, props;
8946
8947 image = plist = props = Qnil;
8948 GCPRO3 (image, plist, props);
8949
8950 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8951 Otherwise, make a new string. */
8952
8953 /* The size of the string we might be able to reuse. */
8954 size = (STRINGP (f->desired_tool_bar_string)
8955 ? SCHARS (f->desired_tool_bar_string)
8956 : 0);
8957
8958 /* We need one space in the string for each image. */
8959 size_needed = f->n_tool_bar_items;
8960
8961 /* Reuse f->desired_tool_bar_string, if possible. */
8962 if (size < size_needed || NILP (f->desired_tool_bar_string))
8963 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8964 make_number (' '));
8965 else
8966 {
8967 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8968 Fremove_text_properties (make_number (0), make_number (size),
8969 props, f->desired_tool_bar_string);
8970 }
8971
8972 /* Put a `display' property on the string for the images to display,
8973 put a `menu_item' property on tool-bar items with a value that
8974 is the index of the item in F's tool-bar item vector. */
8975 for (i = 0; i < f->n_tool_bar_items; ++i)
8976 {
8977 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8978
8979 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8980 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8981 int hmargin, vmargin, relief, idx, end;
8982 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8983
8984 /* If image is a vector, choose the image according to the
8985 button state. */
8986 image = PROP (TOOL_BAR_ITEM_IMAGES);
8987 if (VECTORP (image))
8988 {
8989 if (enabled_p)
8990 idx = (selected_p
8991 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8992 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8993 else
8994 idx = (selected_p
8995 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8996 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8997
8998 xassert (ASIZE (image) >= idx);
8999 image = AREF (image, idx);
9000 }
9001 else
9002 idx = -1;
9003
9004 /* Ignore invalid image specifications. */
9005 if (!valid_image_p (image))
9006 continue;
9007
9008 /* Display the tool-bar button pressed, or depressed. */
9009 plist = Fcopy_sequence (XCDR (image));
9010
9011 /* Compute margin and relief to draw. */
9012 relief = (tool_bar_button_relief >= 0
9013 ? tool_bar_button_relief
9014 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9015 hmargin = vmargin = relief;
9016
9017 if (INTEGERP (Vtool_bar_button_margin)
9018 && XINT (Vtool_bar_button_margin) > 0)
9019 {
9020 hmargin += XFASTINT (Vtool_bar_button_margin);
9021 vmargin += XFASTINT (Vtool_bar_button_margin);
9022 }
9023 else if (CONSP (Vtool_bar_button_margin))
9024 {
9025 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9026 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9027 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9028
9029 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9030 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9031 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9032 }
9033
9034 if (auto_raise_tool_bar_buttons_p)
9035 {
9036 /* Add a `:relief' property to the image spec if the item is
9037 selected. */
9038 if (selected_p)
9039 {
9040 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9041 hmargin -= relief;
9042 vmargin -= relief;
9043 }
9044 }
9045 else
9046 {
9047 /* If image is selected, display it pressed, i.e. with a
9048 negative relief. If it's not selected, display it with a
9049 raised relief. */
9050 plist = Fplist_put (plist, QCrelief,
9051 (selected_p
9052 ? make_number (-relief)
9053 : make_number (relief)));
9054 hmargin -= relief;
9055 vmargin -= relief;
9056 }
9057
9058 /* Put a margin around the image. */
9059 if (hmargin || vmargin)
9060 {
9061 if (hmargin == vmargin)
9062 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9063 else
9064 plist = Fplist_put (plist, QCmargin,
9065 Fcons (make_number (hmargin),
9066 make_number (vmargin)));
9067 }
9068
9069 /* If button is not enabled, and we don't have special images
9070 for the disabled state, make the image appear disabled by
9071 applying an appropriate algorithm to it. */
9072 if (!enabled_p && idx < 0)
9073 plist = Fplist_put (plist, QCconversion, Qdisabled);
9074
9075 /* Put a `display' text property on the string for the image to
9076 display. Put a `menu-item' property on the string that gives
9077 the start of this item's properties in the tool-bar items
9078 vector. */
9079 image = Fcons (Qimage, plist);
9080 props = list4 (Qdisplay, image,
9081 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9082
9083 /* Let the last image hide all remaining spaces in the tool bar
9084 string. The string can be longer than needed when we reuse a
9085 previous string. */
9086 if (i + 1 == f->n_tool_bar_items)
9087 end = SCHARS (f->desired_tool_bar_string);
9088 else
9089 end = i + 1;
9090 Fadd_text_properties (make_number (i), make_number (end),
9091 props, f->desired_tool_bar_string);
9092 #undef PROP
9093 }
9094
9095 UNGCPRO;
9096 }
9097
9098
9099 /* Display one line of the tool-bar of frame IT->f. */
9100
9101 static void
9102 display_tool_bar_line (it)
9103 struct it *it;
9104 {
9105 struct glyph_row *row = it->glyph_row;
9106 int max_x = it->last_visible_x;
9107 struct glyph *last;
9108
9109 prepare_desired_row (row);
9110 row->y = it->current_y;
9111
9112 /* Note that this isn't made use of if the face hasn't a box,
9113 so there's no need to check the face here. */
9114 it->start_of_box_run_p = 1;
9115
9116 while (it->current_x < max_x)
9117 {
9118 int x_before, x, n_glyphs_before, i, nglyphs;
9119
9120 /* Get the next display element. */
9121 if (!get_next_display_element (it))
9122 break;
9123
9124 /* Produce glyphs. */
9125 x_before = it->current_x;
9126 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9127 PRODUCE_GLYPHS (it);
9128
9129 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9130 i = 0;
9131 x = x_before;
9132 while (i < nglyphs)
9133 {
9134 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9135
9136 if (x + glyph->pixel_width > max_x)
9137 {
9138 /* Glyph doesn't fit on line. */
9139 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9140 it->current_x = x;
9141 goto out;
9142 }
9143
9144 ++it->hpos;
9145 x += glyph->pixel_width;
9146 ++i;
9147 }
9148
9149 /* Stop at line ends. */
9150 if (ITERATOR_AT_END_OF_LINE_P (it))
9151 break;
9152
9153 set_iterator_to_next (it, 1);
9154 }
9155
9156 out:;
9157
9158 row->displays_text_p = row->used[TEXT_AREA] != 0;
9159 extend_face_to_end_of_line (it);
9160 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9161 last->right_box_line_p = 1;
9162 if (last == row->glyphs[TEXT_AREA])
9163 last->left_box_line_p = 1;
9164 compute_line_metrics (it);
9165
9166 /* If line is empty, make it occupy the rest of the tool-bar. */
9167 if (!row->displays_text_p)
9168 {
9169 row->height = row->phys_height = it->last_visible_y - row->y;
9170 row->ascent = row->phys_ascent = 0;
9171 row->extra_line_spacing = 0;
9172 }
9173
9174 row->full_width_p = 1;
9175 row->continued_p = 0;
9176 row->truncated_on_left_p = 0;
9177 row->truncated_on_right_p = 0;
9178
9179 it->current_x = it->hpos = 0;
9180 it->current_y += row->height;
9181 ++it->vpos;
9182 ++it->glyph_row;
9183 }
9184
9185
9186 /* Value is the number of screen lines needed to make all tool-bar
9187 items of frame F visible. */
9188
9189 static int
9190 tool_bar_lines_needed (f)
9191 struct frame *f;
9192 {
9193 struct window *w = XWINDOW (f->tool_bar_window);
9194 struct it it;
9195
9196 /* Initialize an iterator for iteration over
9197 F->desired_tool_bar_string in the tool-bar window of frame F. */
9198 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9199 it.first_visible_x = 0;
9200 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9201 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9202
9203 while (!ITERATOR_AT_END_P (&it))
9204 {
9205 it.glyph_row = w->desired_matrix->rows;
9206 clear_glyph_row (it.glyph_row);
9207 display_tool_bar_line (&it);
9208 }
9209
9210 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9211 }
9212
9213
9214 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9215 0, 1, 0,
9216 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9217 (frame)
9218 Lisp_Object frame;
9219 {
9220 struct frame *f;
9221 struct window *w;
9222 int nlines = 0;
9223
9224 if (NILP (frame))
9225 frame = selected_frame;
9226 else
9227 CHECK_FRAME (frame);
9228 f = XFRAME (frame);
9229
9230 if (WINDOWP (f->tool_bar_window)
9231 || (w = XWINDOW (f->tool_bar_window),
9232 WINDOW_TOTAL_LINES (w) > 0))
9233 {
9234 update_tool_bar (f, 1);
9235 if (f->n_tool_bar_items)
9236 {
9237 build_desired_tool_bar_string (f);
9238 nlines = tool_bar_lines_needed (f);
9239 }
9240 }
9241
9242 return make_number (nlines);
9243 }
9244
9245
9246 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9247 height should be changed. */
9248
9249 static int
9250 redisplay_tool_bar (f)
9251 struct frame *f;
9252 {
9253 struct window *w;
9254 struct it it;
9255 struct glyph_row *row;
9256 int change_height_p = 0;
9257
9258 #ifdef USE_GTK
9259 if (FRAME_EXTERNAL_TOOL_BAR (f))
9260 update_frame_tool_bar (f);
9261 return 0;
9262 #endif
9263
9264 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9265 do anything. This means you must start with tool-bar-lines
9266 non-zero to get the auto-sizing effect. Or in other words, you
9267 can turn off tool-bars by specifying tool-bar-lines zero. */
9268 if (!WINDOWP (f->tool_bar_window)
9269 || (w = XWINDOW (f->tool_bar_window),
9270 WINDOW_TOTAL_LINES (w) == 0))
9271 return 0;
9272
9273 /* Set up an iterator for the tool-bar window. */
9274 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9275 it.first_visible_x = 0;
9276 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9277 row = it.glyph_row;
9278
9279 /* Build a string that represents the contents of the tool-bar. */
9280 build_desired_tool_bar_string (f);
9281 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9282
9283 /* Display as many lines as needed to display all tool-bar items. */
9284 while (it.current_y < it.last_visible_y)
9285 display_tool_bar_line (&it);
9286
9287 /* It doesn't make much sense to try scrolling in the tool-bar
9288 window, so don't do it. */
9289 w->desired_matrix->no_scrolling_p = 1;
9290 w->must_be_updated_p = 1;
9291
9292 if (auto_resize_tool_bars_p)
9293 {
9294 int nlines;
9295
9296 /* If we couldn't display everything, change the tool-bar's
9297 height. */
9298 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9299 change_height_p = 1;
9300
9301 /* If there are blank lines at the end, except for a partially
9302 visible blank line at the end that is smaller than
9303 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9304 row = it.glyph_row - 1;
9305 if (!row->displays_text_p
9306 && row->height >= FRAME_LINE_HEIGHT (f))
9307 change_height_p = 1;
9308
9309 /* If row displays tool-bar items, but is partially visible,
9310 change the tool-bar's height. */
9311 if (row->displays_text_p
9312 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9313 change_height_p = 1;
9314
9315 /* Resize windows as needed by changing the `tool-bar-lines'
9316 frame parameter. */
9317 if (change_height_p
9318 && (nlines = tool_bar_lines_needed (f),
9319 nlines != WINDOW_TOTAL_LINES (w)))
9320 {
9321 extern Lisp_Object Qtool_bar_lines;
9322 Lisp_Object frame;
9323 int old_height = WINDOW_TOTAL_LINES (w);
9324
9325 XSETFRAME (frame, f);
9326 clear_glyph_matrix (w->desired_matrix);
9327 Fmodify_frame_parameters (frame,
9328 Fcons (Fcons (Qtool_bar_lines,
9329 make_number (nlines)),
9330 Qnil));
9331 if (WINDOW_TOTAL_LINES (w) != old_height)
9332 fonts_changed_p = 1;
9333 }
9334 }
9335
9336 return change_height_p;
9337 }
9338
9339
9340 /* Get information about the tool-bar item which is displayed in GLYPH
9341 on frame F. Return in *PROP_IDX the index where tool-bar item
9342 properties start in F->tool_bar_items. Value is zero if
9343 GLYPH doesn't display a tool-bar item. */
9344
9345 static int
9346 tool_bar_item_info (f, glyph, prop_idx)
9347 struct frame *f;
9348 struct glyph *glyph;
9349 int *prop_idx;
9350 {
9351 Lisp_Object prop;
9352 int success_p;
9353 int charpos;
9354
9355 /* This function can be called asynchronously, which means we must
9356 exclude any possibility that Fget_text_property signals an
9357 error. */
9358 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9359 charpos = max (0, charpos);
9360
9361 /* Get the text property `menu-item' at pos. The value of that
9362 property is the start index of this item's properties in
9363 F->tool_bar_items. */
9364 prop = Fget_text_property (make_number (charpos),
9365 Qmenu_item, f->current_tool_bar_string);
9366 if (INTEGERP (prop))
9367 {
9368 *prop_idx = XINT (prop);
9369 success_p = 1;
9370 }
9371 else
9372 success_p = 0;
9373
9374 return success_p;
9375 }
9376
9377 \f
9378 /* Get information about the tool-bar item at position X/Y on frame F.
9379 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9380 the current matrix of the tool-bar window of F, or NULL if not
9381 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9382 item in F->tool_bar_items. Value is
9383
9384 -1 if X/Y is not on a tool-bar item
9385 0 if X/Y is on the same item that was highlighted before.
9386 1 otherwise. */
9387
9388 static int
9389 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9390 struct frame *f;
9391 int x, y;
9392 struct glyph **glyph;
9393 int *hpos, *vpos, *prop_idx;
9394 {
9395 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9396 struct window *w = XWINDOW (f->tool_bar_window);
9397 int area;
9398
9399 /* Find the glyph under X/Y. */
9400 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9401 if (*glyph == NULL)
9402 return -1;
9403
9404 /* Get the start of this tool-bar item's properties in
9405 f->tool_bar_items. */
9406 if (!tool_bar_item_info (f, *glyph, prop_idx))
9407 return -1;
9408
9409 /* Is mouse on the highlighted item? */
9410 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9411 && *vpos >= dpyinfo->mouse_face_beg_row
9412 && *vpos <= dpyinfo->mouse_face_end_row
9413 && (*vpos > dpyinfo->mouse_face_beg_row
9414 || *hpos >= dpyinfo->mouse_face_beg_col)
9415 && (*vpos < dpyinfo->mouse_face_end_row
9416 || *hpos < dpyinfo->mouse_face_end_col
9417 || dpyinfo->mouse_face_past_end))
9418 return 0;
9419
9420 return 1;
9421 }
9422
9423
9424 /* EXPORT:
9425 Handle mouse button event on the tool-bar of frame F, at
9426 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9427 0 for button release. MODIFIERS is event modifiers for button
9428 release. */
9429
9430 void
9431 handle_tool_bar_click (f, x, y, down_p, modifiers)
9432 struct frame *f;
9433 int x, y, down_p;
9434 unsigned int modifiers;
9435 {
9436 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9437 struct window *w = XWINDOW (f->tool_bar_window);
9438 int hpos, vpos, prop_idx;
9439 struct glyph *glyph;
9440 Lisp_Object enabled_p;
9441
9442 /* If not on the highlighted tool-bar item, return. */
9443 frame_to_window_pixel_xy (w, &x, &y);
9444 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9445 return;
9446
9447 /* If item is disabled, do nothing. */
9448 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9449 if (NILP (enabled_p))
9450 return;
9451
9452 if (down_p)
9453 {
9454 /* Show item in pressed state. */
9455 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9456 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9457 last_tool_bar_item = prop_idx;
9458 }
9459 else
9460 {
9461 Lisp_Object key, frame;
9462 struct input_event event;
9463 EVENT_INIT (event);
9464
9465 /* Show item in released state. */
9466 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9467 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9468
9469 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9470
9471 XSETFRAME (frame, f);
9472 event.kind = TOOL_BAR_EVENT;
9473 event.frame_or_window = frame;
9474 event.arg = frame;
9475 kbd_buffer_store_event (&event);
9476
9477 event.kind = TOOL_BAR_EVENT;
9478 event.frame_or_window = frame;
9479 event.arg = key;
9480 event.modifiers = modifiers;
9481 kbd_buffer_store_event (&event);
9482 last_tool_bar_item = -1;
9483 }
9484 }
9485
9486
9487 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9488 tool-bar window-relative coordinates X/Y. Called from
9489 note_mouse_highlight. */
9490
9491 static void
9492 note_tool_bar_highlight (f, x, y)
9493 struct frame *f;
9494 int x, y;
9495 {
9496 Lisp_Object window = f->tool_bar_window;
9497 struct window *w = XWINDOW (window);
9498 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9499 int hpos, vpos;
9500 struct glyph *glyph;
9501 struct glyph_row *row;
9502 int i;
9503 Lisp_Object enabled_p;
9504 int prop_idx;
9505 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9506 int mouse_down_p, rc;
9507
9508 /* Function note_mouse_highlight is called with negative x(y
9509 values when mouse moves outside of the frame. */
9510 if (x <= 0 || y <= 0)
9511 {
9512 clear_mouse_face (dpyinfo);
9513 return;
9514 }
9515
9516 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9517 if (rc < 0)
9518 {
9519 /* Not on tool-bar item. */
9520 clear_mouse_face (dpyinfo);
9521 return;
9522 }
9523 else if (rc == 0)
9524 /* On same tool-bar item as before. */
9525 goto set_help_echo;
9526
9527 clear_mouse_face (dpyinfo);
9528
9529 /* Mouse is down, but on different tool-bar item? */
9530 mouse_down_p = (dpyinfo->grabbed
9531 && f == last_mouse_frame
9532 && FRAME_LIVE_P (f));
9533 if (mouse_down_p
9534 && last_tool_bar_item != prop_idx)
9535 return;
9536
9537 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9538 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9539
9540 /* If tool-bar item is not enabled, don't highlight it. */
9541 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9542 if (!NILP (enabled_p))
9543 {
9544 /* Compute the x-position of the glyph. In front and past the
9545 image is a space. We include this in the highlighted area. */
9546 row = MATRIX_ROW (w->current_matrix, vpos);
9547 for (i = x = 0; i < hpos; ++i)
9548 x += row->glyphs[TEXT_AREA][i].pixel_width;
9549
9550 /* Record this as the current active region. */
9551 dpyinfo->mouse_face_beg_col = hpos;
9552 dpyinfo->mouse_face_beg_row = vpos;
9553 dpyinfo->mouse_face_beg_x = x;
9554 dpyinfo->mouse_face_beg_y = row->y;
9555 dpyinfo->mouse_face_past_end = 0;
9556
9557 dpyinfo->mouse_face_end_col = hpos + 1;
9558 dpyinfo->mouse_face_end_row = vpos;
9559 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9560 dpyinfo->mouse_face_end_y = row->y;
9561 dpyinfo->mouse_face_window = window;
9562 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9563
9564 /* Display it as active. */
9565 show_mouse_face (dpyinfo, draw);
9566 dpyinfo->mouse_face_image_state = draw;
9567 }
9568
9569 set_help_echo:
9570
9571 /* Set help_echo_string to a help string to display for this tool-bar item.
9572 XTread_socket does the rest. */
9573 help_echo_object = help_echo_window = Qnil;
9574 help_echo_pos = -1;
9575 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9576 if (NILP (help_echo_string))
9577 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9578 }
9579
9580 #endif /* HAVE_WINDOW_SYSTEM */
9581
9582
9583 \f
9584 /************************************************************************
9585 Horizontal scrolling
9586 ************************************************************************/
9587
9588 static int hscroll_window_tree P_ ((Lisp_Object));
9589 static int hscroll_windows P_ ((Lisp_Object));
9590
9591 /* For all leaf windows in the window tree rooted at WINDOW, set their
9592 hscroll value so that PT is (i) visible in the window, and (ii) so
9593 that it is not within a certain margin at the window's left and
9594 right border. Value is non-zero if any window's hscroll has been
9595 changed. */
9596
9597 static int
9598 hscroll_window_tree (window)
9599 Lisp_Object window;
9600 {
9601 int hscrolled_p = 0;
9602 int hscroll_relative_p = FLOATP (Vhscroll_step);
9603 int hscroll_step_abs = 0;
9604 double hscroll_step_rel = 0;
9605
9606 if (hscroll_relative_p)
9607 {
9608 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9609 if (hscroll_step_rel < 0)
9610 {
9611 hscroll_relative_p = 0;
9612 hscroll_step_abs = 0;
9613 }
9614 }
9615 else if (INTEGERP (Vhscroll_step))
9616 {
9617 hscroll_step_abs = XINT (Vhscroll_step);
9618 if (hscroll_step_abs < 0)
9619 hscroll_step_abs = 0;
9620 }
9621 else
9622 hscroll_step_abs = 0;
9623
9624 while (WINDOWP (window))
9625 {
9626 struct window *w = XWINDOW (window);
9627
9628 if (WINDOWP (w->hchild))
9629 hscrolled_p |= hscroll_window_tree (w->hchild);
9630 else if (WINDOWP (w->vchild))
9631 hscrolled_p |= hscroll_window_tree (w->vchild);
9632 else if (w->cursor.vpos >= 0)
9633 {
9634 int h_margin;
9635 int text_area_width;
9636 struct glyph_row *current_cursor_row
9637 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9638 struct glyph_row *desired_cursor_row
9639 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9640 struct glyph_row *cursor_row
9641 = (desired_cursor_row->enabled_p
9642 ? desired_cursor_row
9643 : current_cursor_row);
9644
9645 text_area_width = window_box_width (w, TEXT_AREA);
9646
9647 /* Scroll when cursor is inside this scroll margin. */
9648 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9649
9650 if ((XFASTINT (w->hscroll)
9651 && w->cursor.x <= h_margin)
9652 || (cursor_row->enabled_p
9653 && cursor_row->truncated_on_right_p
9654 && (w->cursor.x >= text_area_width - h_margin)))
9655 {
9656 struct it it;
9657 int hscroll;
9658 struct buffer *saved_current_buffer;
9659 int pt;
9660 int wanted_x;
9661
9662 /* Find point in a display of infinite width. */
9663 saved_current_buffer = current_buffer;
9664 current_buffer = XBUFFER (w->buffer);
9665
9666 if (w == XWINDOW (selected_window))
9667 pt = BUF_PT (current_buffer);
9668 else
9669 {
9670 pt = marker_position (w->pointm);
9671 pt = max (BEGV, pt);
9672 pt = min (ZV, pt);
9673 }
9674
9675 /* Move iterator to pt starting at cursor_row->start in
9676 a line with infinite width. */
9677 init_to_row_start (&it, w, cursor_row);
9678 it.last_visible_x = INFINITY;
9679 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9680 current_buffer = saved_current_buffer;
9681
9682 /* Position cursor in window. */
9683 if (!hscroll_relative_p && hscroll_step_abs == 0)
9684 hscroll = max (0, (it.current_x
9685 - (ITERATOR_AT_END_OF_LINE_P (&it)
9686 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9687 : (text_area_width / 2))))
9688 / FRAME_COLUMN_WIDTH (it.f);
9689 else if (w->cursor.x >= text_area_width - h_margin)
9690 {
9691 if (hscroll_relative_p)
9692 wanted_x = text_area_width * (1 - hscroll_step_rel)
9693 - h_margin;
9694 else
9695 wanted_x = text_area_width
9696 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9697 - h_margin;
9698 hscroll
9699 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9700 }
9701 else
9702 {
9703 if (hscroll_relative_p)
9704 wanted_x = text_area_width * hscroll_step_rel
9705 + h_margin;
9706 else
9707 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9708 + h_margin;
9709 hscroll
9710 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9711 }
9712 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9713
9714 /* Don't call Fset_window_hscroll if value hasn't
9715 changed because it will prevent redisplay
9716 optimizations. */
9717 if (XFASTINT (w->hscroll) != hscroll)
9718 {
9719 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9720 w->hscroll = make_number (hscroll);
9721 hscrolled_p = 1;
9722 }
9723 }
9724 }
9725
9726 window = w->next;
9727 }
9728
9729 /* Value is non-zero if hscroll of any leaf window has been changed. */
9730 return hscrolled_p;
9731 }
9732
9733
9734 /* Set hscroll so that cursor is visible and not inside horizontal
9735 scroll margins for all windows in the tree rooted at WINDOW. See
9736 also hscroll_window_tree above. Value is non-zero if any window's
9737 hscroll has been changed. If it has, desired matrices on the frame
9738 of WINDOW are cleared. */
9739
9740 static int
9741 hscroll_windows (window)
9742 Lisp_Object window;
9743 {
9744 int hscrolled_p;
9745
9746 if (automatic_hscrolling_p)
9747 {
9748 hscrolled_p = hscroll_window_tree (window);
9749 if (hscrolled_p)
9750 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9751 }
9752 else
9753 hscrolled_p = 0;
9754 return hscrolled_p;
9755 }
9756
9757
9758 \f
9759 /************************************************************************
9760 Redisplay
9761 ************************************************************************/
9762
9763 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9764 to a non-zero value. This is sometimes handy to have in a debugger
9765 session. */
9766
9767 #if GLYPH_DEBUG
9768
9769 /* First and last unchanged row for try_window_id. */
9770
9771 int debug_first_unchanged_at_end_vpos;
9772 int debug_last_unchanged_at_beg_vpos;
9773
9774 /* Delta vpos and y. */
9775
9776 int debug_dvpos, debug_dy;
9777
9778 /* Delta in characters and bytes for try_window_id. */
9779
9780 int debug_delta, debug_delta_bytes;
9781
9782 /* Values of window_end_pos and window_end_vpos at the end of
9783 try_window_id. */
9784
9785 EMACS_INT debug_end_pos, debug_end_vpos;
9786
9787 /* Append a string to W->desired_matrix->method. FMT is a printf
9788 format string. A1...A9 are a supplement for a variable-length
9789 argument list. If trace_redisplay_p is non-zero also printf the
9790 resulting string to stderr. */
9791
9792 static void
9793 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9794 struct window *w;
9795 char *fmt;
9796 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9797 {
9798 char buffer[512];
9799 char *method = w->desired_matrix->method;
9800 int len = strlen (method);
9801 int size = sizeof w->desired_matrix->method;
9802 int remaining = size - len - 1;
9803
9804 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9805 if (len && remaining)
9806 {
9807 method[len] = '|';
9808 --remaining, ++len;
9809 }
9810
9811 strncpy (method + len, buffer, remaining);
9812
9813 if (trace_redisplay_p)
9814 fprintf (stderr, "%p (%s): %s\n",
9815 w,
9816 ((BUFFERP (w->buffer)
9817 && STRINGP (XBUFFER (w->buffer)->name))
9818 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9819 : "no buffer"),
9820 buffer);
9821 }
9822
9823 #endif /* GLYPH_DEBUG */
9824
9825
9826 /* Value is non-zero if all changes in window W, which displays
9827 current_buffer, are in the text between START and END. START is a
9828 buffer position, END is given as a distance from Z. Used in
9829 redisplay_internal for display optimization. */
9830
9831 static INLINE int
9832 text_outside_line_unchanged_p (w, start, end)
9833 struct window *w;
9834 int start, end;
9835 {
9836 int unchanged_p = 1;
9837
9838 /* If text or overlays have changed, see where. */
9839 if (XFASTINT (w->last_modified) < MODIFF
9840 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9841 {
9842 /* Gap in the line? */
9843 if (GPT < start || Z - GPT < end)
9844 unchanged_p = 0;
9845
9846 /* Changes start in front of the line, or end after it? */
9847 if (unchanged_p
9848 && (BEG_UNCHANGED < start - 1
9849 || END_UNCHANGED < end))
9850 unchanged_p = 0;
9851
9852 /* If selective display, can't optimize if changes start at the
9853 beginning of the line. */
9854 if (unchanged_p
9855 && INTEGERP (current_buffer->selective_display)
9856 && XINT (current_buffer->selective_display) > 0
9857 && (BEG_UNCHANGED < start || GPT <= start))
9858 unchanged_p = 0;
9859
9860 /* If there are overlays at the start or end of the line, these
9861 may have overlay strings with newlines in them. A change at
9862 START, for instance, may actually concern the display of such
9863 overlay strings as well, and they are displayed on different
9864 lines. So, quickly rule out this case. (For the future, it
9865 might be desirable to implement something more telling than
9866 just BEG/END_UNCHANGED.) */
9867 if (unchanged_p)
9868 {
9869 if (BEG + BEG_UNCHANGED == start
9870 && overlay_touches_p (start))
9871 unchanged_p = 0;
9872 if (END_UNCHANGED == end
9873 && overlay_touches_p (Z - end))
9874 unchanged_p = 0;
9875 }
9876 }
9877
9878 return unchanged_p;
9879 }
9880
9881
9882 /* Do a frame update, taking possible shortcuts into account. This is
9883 the main external entry point for redisplay.
9884
9885 If the last redisplay displayed an echo area message and that message
9886 is no longer requested, we clear the echo area or bring back the
9887 mini-buffer if that is in use. */
9888
9889 void
9890 redisplay ()
9891 {
9892 redisplay_internal (0);
9893 }
9894
9895
9896 static Lisp_Object
9897 overlay_arrow_string_or_property (var)
9898 Lisp_Object var;
9899 {
9900 Lisp_Object val;
9901
9902 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9903 return val;
9904
9905 return Voverlay_arrow_string;
9906 }
9907
9908 /* Return 1 if there are any overlay-arrows in current_buffer. */
9909 static int
9910 overlay_arrow_in_current_buffer_p ()
9911 {
9912 Lisp_Object vlist;
9913
9914 for (vlist = Voverlay_arrow_variable_list;
9915 CONSP (vlist);
9916 vlist = XCDR (vlist))
9917 {
9918 Lisp_Object var = XCAR (vlist);
9919 Lisp_Object val;
9920
9921 if (!SYMBOLP (var))
9922 continue;
9923 val = find_symbol_value (var);
9924 if (MARKERP (val)
9925 && current_buffer == XMARKER (val)->buffer)
9926 return 1;
9927 }
9928 return 0;
9929 }
9930
9931
9932 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9933 has changed. */
9934
9935 static int
9936 overlay_arrows_changed_p ()
9937 {
9938 Lisp_Object vlist;
9939
9940 for (vlist = Voverlay_arrow_variable_list;
9941 CONSP (vlist);
9942 vlist = XCDR (vlist))
9943 {
9944 Lisp_Object var = XCAR (vlist);
9945 Lisp_Object val, pstr;
9946
9947 if (!SYMBOLP (var))
9948 continue;
9949 val = find_symbol_value (var);
9950 if (!MARKERP (val))
9951 continue;
9952 if (! EQ (COERCE_MARKER (val),
9953 Fget (var, Qlast_arrow_position))
9954 || ! (pstr = overlay_arrow_string_or_property (var),
9955 EQ (pstr, Fget (var, Qlast_arrow_string))))
9956 return 1;
9957 }
9958 return 0;
9959 }
9960
9961 /* Mark overlay arrows to be updated on next redisplay. */
9962
9963 static void
9964 update_overlay_arrows (up_to_date)
9965 int up_to_date;
9966 {
9967 Lisp_Object vlist;
9968
9969 for (vlist = Voverlay_arrow_variable_list;
9970 CONSP (vlist);
9971 vlist = XCDR (vlist))
9972 {
9973 Lisp_Object var = XCAR (vlist);
9974
9975 if (!SYMBOLP (var))
9976 continue;
9977
9978 if (up_to_date > 0)
9979 {
9980 Lisp_Object val = find_symbol_value (var);
9981 Fput (var, Qlast_arrow_position,
9982 COERCE_MARKER (val));
9983 Fput (var, Qlast_arrow_string,
9984 overlay_arrow_string_or_property (var));
9985 }
9986 else if (up_to_date < 0
9987 || !NILP (Fget (var, Qlast_arrow_position)))
9988 {
9989 Fput (var, Qlast_arrow_position, Qt);
9990 Fput (var, Qlast_arrow_string, Qt);
9991 }
9992 }
9993 }
9994
9995
9996 /* Return overlay arrow string to display at row.
9997 Return integer (bitmap number) for arrow bitmap in left fringe.
9998 Return nil if no overlay arrow. */
9999
10000 static Lisp_Object
10001 overlay_arrow_at_row (it, row)
10002 struct it *it;
10003 struct glyph_row *row;
10004 {
10005 Lisp_Object vlist;
10006
10007 for (vlist = Voverlay_arrow_variable_list;
10008 CONSP (vlist);
10009 vlist = XCDR (vlist))
10010 {
10011 Lisp_Object var = XCAR (vlist);
10012 Lisp_Object val;
10013
10014 if (!SYMBOLP (var))
10015 continue;
10016
10017 val = find_symbol_value (var);
10018
10019 if (MARKERP (val)
10020 && current_buffer == XMARKER (val)->buffer
10021 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10022 {
10023 if (FRAME_WINDOW_P (it->f)
10024 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10025 {
10026 #ifdef HAVE_WINDOW_SYSTEM
10027 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10028 {
10029 int fringe_bitmap;
10030 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10031 return make_number (fringe_bitmap);
10032 }
10033 #endif
10034 return make_number (-1); /* Use default arrow bitmap */
10035 }
10036 return overlay_arrow_string_or_property (var);
10037 }
10038 }
10039
10040 return Qnil;
10041 }
10042
10043 /* Return 1 if point moved out of or into a composition. Otherwise
10044 return 0. PREV_BUF and PREV_PT are the last point buffer and
10045 position. BUF and PT are the current point buffer and position. */
10046
10047 int
10048 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10049 struct buffer *prev_buf, *buf;
10050 int prev_pt, pt;
10051 {
10052 int start, end;
10053 Lisp_Object prop;
10054 Lisp_Object buffer;
10055
10056 XSETBUFFER (buffer, buf);
10057 /* Check a composition at the last point if point moved within the
10058 same buffer. */
10059 if (prev_buf == buf)
10060 {
10061 if (prev_pt == pt)
10062 /* Point didn't move. */
10063 return 0;
10064
10065 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10066 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10067 && COMPOSITION_VALID_P (start, end, prop)
10068 && start < prev_pt && end > prev_pt)
10069 /* The last point was within the composition. Return 1 iff
10070 point moved out of the composition. */
10071 return (pt <= start || pt >= end);
10072 }
10073
10074 /* Check a composition at the current point. */
10075 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10076 && find_composition (pt, -1, &start, &end, &prop, buffer)
10077 && COMPOSITION_VALID_P (start, end, prop)
10078 && start < pt && end > pt);
10079 }
10080
10081
10082 /* Reconsider the setting of B->clip_changed which is displayed
10083 in window W. */
10084
10085 static INLINE void
10086 reconsider_clip_changes (w, b)
10087 struct window *w;
10088 struct buffer *b;
10089 {
10090 if (b->clip_changed
10091 && !NILP (w->window_end_valid)
10092 && w->current_matrix->buffer == b
10093 && w->current_matrix->zv == BUF_ZV (b)
10094 && w->current_matrix->begv == BUF_BEGV (b))
10095 b->clip_changed = 0;
10096
10097 /* If display wasn't paused, and W is not a tool bar window, see if
10098 point has been moved into or out of a composition. In that case,
10099 we set b->clip_changed to 1 to force updating the screen. If
10100 b->clip_changed has already been set to 1, we can skip this
10101 check. */
10102 if (!b->clip_changed
10103 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10104 {
10105 int pt;
10106
10107 if (w == XWINDOW (selected_window))
10108 pt = BUF_PT (current_buffer);
10109 else
10110 pt = marker_position (w->pointm);
10111
10112 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10113 || pt != XINT (w->last_point))
10114 && check_point_in_composition (w->current_matrix->buffer,
10115 XINT (w->last_point),
10116 XBUFFER (w->buffer), pt))
10117 b->clip_changed = 1;
10118 }
10119 }
10120 \f
10121
10122 /* Select FRAME to forward the values of frame-local variables into C
10123 variables so that the redisplay routines can access those values
10124 directly. */
10125
10126 static void
10127 select_frame_for_redisplay (frame)
10128 Lisp_Object frame;
10129 {
10130 Lisp_Object tail, sym, val;
10131 Lisp_Object old = selected_frame;
10132
10133 selected_frame = frame;
10134
10135 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10136 if (CONSP (XCAR (tail))
10137 && (sym = XCAR (XCAR (tail)),
10138 SYMBOLP (sym))
10139 && (sym = indirect_variable (sym),
10140 val = SYMBOL_VALUE (sym),
10141 (BUFFER_LOCAL_VALUEP (val)
10142 || SOME_BUFFER_LOCAL_VALUEP (val)))
10143 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10144 /* Use find_symbol_value rather than Fsymbol_value
10145 to avoid an error if it is void. */
10146 find_symbol_value (sym);
10147
10148 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10149 if (CONSP (XCAR (tail))
10150 && (sym = XCAR (XCAR (tail)),
10151 SYMBOLP (sym))
10152 && (sym = indirect_variable (sym),
10153 val = SYMBOL_VALUE (sym),
10154 (BUFFER_LOCAL_VALUEP (val)
10155 || SOME_BUFFER_LOCAL_VALUEP (val)))
10156 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10157 find_symbol_value (sym);
10158 }
10159
10160
10161 #define STOP_POLLING \
10162 do { if (! polling_stopped_here) stop_polling (); \
10163 polling_stopped_here = 1; } while (0)
10164
10165 #define RESUME_POLLING \
10166 do { if (polling_stopped_here) start_polling (); \
10167 polling_stopped_here = 0; } while (0)
10168
10169
10170 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10171 response to any user action; therefore, we should preserve the echo
10172 area. (Actually, our caller does that job.) Perhaps in the future
10173 avoid recentering windows if it is not necessary; currently that
10174 causes some problems. */
10175
10176 static void
10177 redisplay_internal (preserve_echo_area)
10178 int preserve_echo_area;
10179 {
10180 struct window *w = XWINDOW (selected_window);
10181 struct frame *f = XFRAME (w->frame);
10182 int pause;
10183 int must_finish = 0;
10184 struct text_pos tlbufpos, tlendpos;
10185 int number_of_visible_frames;
10186 int count;
10187 struct frame *sf = SELECTED_FRAME ();
10188 int polling_stopped_here = 0;
10189
10190 /* Non-zero means redisplay has to consider all windows on all
10191 frames. Zero means, only selected_window is considered. */
10192 int consider_all_windows_p;
10193
10194 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10195
10196 /* No redisplay if running in batch mode or frame is not yet fully
10197 initialized, or redisplay is explicitly turned off by setting
10198 Vinhibit_redisplay. */
10199 if (noninteractive
10200 || !NILP (Vinhibit_redisplay)
10201 || !f->glyphs_initialized_p)
10202 return;
10203
10204 /* The flag redisplay_performed_directly_p is set by
10205 direct_output_for_insert when it already did the whole screen
10206 update necessary. */
10207 if (redisplay_performed_directly_p)
10208 {
10209 redisplay_performed_directly_p = 0;
10210 if (!hscroll_windows (selected_window))
10211 return;
10212 }
10213
10214 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10215 if (popup_activated ())
10216 return;
10217 #endif
10218
10219 /* I don't think this happens but let's be paranoid. */
10220 if (redisplaying_p)
10221 return;
10222
10223 /* Record a function that resets redisplaying_p to its old value
10224 when we leave this function. */
10225 count = SPECPDL_INDEX ();
10226 record_unwind_protect (unwind_redisplay,
10227 Fcons (make_number (redisplaying_p), selected_frame));
10228 ++redisplaying_p;
10229 specbind (Qinhibit_free_realized_faces, Qnil);
10230
10231 {
10232 Lisp_Object tail, frame;
10233
10234 FOR_EACH_FRAME (tail, frame)
10235 {
10236 struct frame *f = XFRAME (frame);
10237 f->already_hscrolled_p = 0;
10238 }
10239 }
10240
10241 retry:
10242 pause = 0;
10243 reconsider_clip_changes (w, current_buffer);
10244
10245 /* If new fonts have been loaded that make a glyph matrix adjustment
10246 necessary, do it. */
10247 if (fonts_changed_p)
10248 {
10249 adjust_glyphs (NULL);
10250 ++windows_or_buffers_changed;
10251 fonts_changed_p = 0;
10252 }
10253
10254 /* If face_change_count is non-zero, init_iterator will free all
10255 realized faces, which includes the faces referenced from current
10256 matrices. So, we can't reuse current matrices in this case. */
10257 if (face_change_count)
10258 ++windows_or_buffers_changed;
10259
10260 if (! FRAME_WINDOW_P (sf)
10261 && previous_terminal_frame != sf)
10262 {
10263 /* Since frames on an ASCII terminal share the same display
10264 area, displaying a different frame means redisplay the whole
10265 thing. */
10266 windows_or_buffers_changed++;
10267 SET_FRAME_GARBAGED (sf);
10268 XSETFRAME (Vterminal_frame, sf);
10269 }
10270 previous_terminal_frame = sf;
10271
10272 /* Set the visible flags for all frames. Do this before checking
10273 for resized or garbaged frames; they want to know if their frames
10274 are visible. See the comment in frame.h for
10275 FRAME_SAMPLE_VISIBILITY. */
10276 {
10277 Lisp_Object tail, frame;
10278
10279 number_of_visible_frames = 0;
10280
10281 FOR_EACH_FRAME (tail, frame)
10282 {
10283 struct frame *f = XFRAME (frame);
10284
10285 FRAME_SAMPLE_VISIBILITY (f);
10286 if (FRAME_VISIBLE_P (f))
10287 ++number_of_visible_frames;
10288 clear_desired_matrices (f);
10289 }
10290 }
10291
10292 /* Notice any pending interrupt request to change frame size. */
10293 do_pending_window_change (1);
10294
10295 /* Clear frames marked as garbaged. */
10296 if (frame_garbaged)
10297 clear_garbaged_frames ();
10298
10299 /* Build menubar and tool-bar items. */
10300 prepare_menu_bars ();
10301
10302 if (windows_or_buffers_changed)
10303 update_mode_lines++;
10304
10305 /* Detect case that we need to write or remove a star in the mode line. */
10306 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10307 {
10308 w->update_mode_line = Qt;
10309 if (buffer_shared > 1)
10310 update_mode_lines++;
10311 }
10312
10313 /* If %c is in the mode line, update it if needed. */
10314 if (!NILP (w->column_number_displayed)
10315 /* This alternative quickly identifies a common case
10316 where no change is needed. */
10317 && !(PT == XFASTINT (w->last_point)
10318 && XFASTINT (w->last_modified) >= MODIFF
10319 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10320 && (XFASTINT (w->column_number_displayed)
10321 != (int) current_column ())) /* iftc */
10322 w->update_mode_line = Qt;
10323
10324 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10325
10326 /* The variable buffer_shared is set in redisplay_window and
10327 indicates that we redisplay a buffer in different windows. See
10328 there. */
10329 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10330 || cursor_type_changed);
10331
10332 /* If specs for an arrow have changed, do thorough redisplay
10333 to ensure we remove any arrow that should no longer exist. */
10334 if (overlay_arrows_changed_p ())
10335 consider_all_windows_p = windows_or_buffers_changed = 1;
10336
10337 /* Normally the message* functions will have already displayed and
10338 updated the echo area, but the frame may have been trashed, or
10339 the update may have been preempted, so display the echo area
10340 again here. Checking message_cleared_p captures the case that
10341 the echo area should be cleared. */
10342 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10343 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10344 || (message_cleared_p
10345 && minibuf_level == 0
10346 /* If the mini-window is currently selected, this means the
10347 echo-area doesn't show through. */
10348 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10349 {
10350 int window_height_changed_p = echo_area_display (0);
10351 must_finish = 1;
10352
10353 /* If we don't display the current message, don't clear the
10354 message_cleared_p flag, because, if we did, we wouldn't clear
10355 the echo area in the next redisplay which doesn't preserve
10356 the echo area. */
10357 if (!display_last_displayed_message_p)
10358 message_cleared_p = 0;
10359
10360 if (fonts_changed_p)
10361 goto retry;
10362 else if (window_height_changed_p)
10363 {
10364 consider_all_windows_p = 1;
10365 ++update_mode_lines;
10366 ++windows_or_buffers_changed;
10367
10368 /* If window configuration was changed, frames may have been
10369 marked garbaged. Clear them or we will experience
10370 surprises wrt scrolling. */
10371 if (frame_garbaged)
10372 clear_garbaged_frames ();
10373 }
10374 }
10375 else if (EQ (selected_window, minibuf_window)
10376 && (current_buffer->clip_changed
10377 || XFASTINT (w->last_modified) < MODIFF
10378 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10379 && resize_mini_window (w, 0))
10380 {
10381 /* Resized active mini-window to fit the size of what it is
10382 showing if its contents might have changed. */
10383 must_finish = 1;
10384 consider_all_windows_p = 1;
10385 ++windows_or_buffers_changed;
10386 ++update_mode_lines;
10387
10388 /* If window configuration was changed, frames may have been
10389 marked garbaged. Clear them or we will experience
10390 surprises wrt scrolling. */
10391 if (frame_garbaged)
10392 clear_garbaged_frames ();
10393 }
10394
10395
10396 /* If showing the region, and mark has changed, we must redisplay
10397 the whole window. The assignment to this_line_start_pos prevents
10398 the optimization directly below this if-statement. */
10399 if (((!NILP (Vtransient_mark_mode)
10400 && !NILP (XBUFFER (w->buffer)->mark_active))
10401 != !NILP (w->region_showing))
10402 || (!NILP (w->region_showing)
10403 && !EQ (w->region_showing,
10404 Fmarker_position (XBUFFER (w->buffer)->mark))))
10405 CHARPOS (this_line_start_pos) = 0;
10406
10407 /* Optimize the case that only the line containing the cursor in the
10408 selected window has changed. Variables starting with this_ are
10409 set in display_line and record information about the line
10410 containing the cursor. */
10411 tlbufpos = this_line_start_pos;
10412 tlendpos = this_line_end_pos;
10413 if (!consider_all_windows_p
10414 && CHARPOS (tlbufpos) > 0
10415 && NILP (w->update_mode_line)
10416 && !current_buffer->clip_changed
10417 && !current_buffer->prevent_redisplay_optimizations_p
10418 && FRAME_VISIBLE_P (XFRAME (w->frame))
10419 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10420 /* Make sure recorded data applies to current buffer, etc. */
10421 && this_line_buffer == current_buffer
10422 && current_buffer == XBUFFER (w->buffer)
10423 && NILP (w->force_start)
10424 && NILP (w->optional_new_start)
10425 /* Point must be on the line that we have info recorded about. */
10426 && PT >= CHARPOS (tlbufpos)
10427 && PT <= Z - CHARPOS (tlendpos)
10428 /* All text outside that line, including its final newline,
10429 must be unchanged */
10430 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10431 CHARPOS (tlendpos)))
10432 {
10433 if (CHARPOS (tlbufpos) > BEGV
10434 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10435 && (CHARPOS (tlbufpos) == ZV
10436 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10437 /* Former continuation line has disappeared by becoming empty */
10438 goto cancel;
10439 else if (XFASTINT (w->last_modified) < MODIFF
10440 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10441 || MINI_WINDOW_P (w))
10442 {
10443 /* We have to handle the case of continuation around a
10444 wide-column character (See the comment in indent.c around
10445 line 885).
10446
10447 For instance, in the following case:
10448
10449 -------- Insert --------
10450 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10451 J_I_ ==> J_I_ `^^' are cursors.
10452 ^^ ^^
10453 -------- --------
10454
10455 As we have to redraw the line above, we should goto cancel. */
10456
10457 struct it it;
10458 int line_height_before = this_line_pixel_height;
10459
10460 /* Note that start_display will handle the case that the
10461 line starting at tlbufpos is a continuation lines. */
10462 start_display (&it, w, tlbufpos);
10463
10464 /* Implementation note: It this still necessary? */
10465 if (it.current_x != this_line_start_x)
10466 goto cancel;
10467
10468 TRACE ((stderr, "trying display optimization 1\n"));
10469 w->cursor.vpos = -1;
10470 overlay_arrow_seen = 0;
10471 it.vpos = this_line_vpos;
10472 it.current_y = this_line_y;
10473 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10474 display_line (&it);
10475
10476 /* If line contains point, is not continued,
10477 and ends at same distance from eob as before, we win */
10478 if (w->cursor.vpos >= 0
10479 /* Line is not continued, otherwise this_line_start_pos
10480 would have been set to 0 in display_line. */
10481 && CHARPOS (this_line_start_pos)
10482 /* Line ends as before. */
10483 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10484 /* Line has same height as before. Otherwise other lines
10485 would have to be shifted up or down. */
10486 && this_line_pixel_height == line_height_before)
10487 {
10488 /* If this is not the window's last line, we must adjust
10489 the charstarts of the lines below. */
10490 if (it.current_y < it.last_visible_y)
10491 {
10492 struct glyph_row *row
10493 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10494 int delta, delta_bytes;
10495
10496 if (Z - CHARPOS (tlendpos) == ZV)
10497 {
10498 /* This line ends at end of (accessible part of)
10499 buffer. There is no newline to count. */
10500 delta = (Z
10501 - CHARPOS (tlendpos)
10502 - MATRIX_ROW_START_CHARPOS (row));
10503 delta_bytes = (Z_BYTE
10504 - BYTEPOS (tlendpos)
10505 - MATRIX_ROW_START_BYTEPOS (row));
10506 }
10507 else
10508 {
10509 /* This line ends in a newline. Must take
10510 account of the newline and the rest of the
10511 text that follows. */
10512 delta = (Z
10513 - CHARPOS (tlendpos)
10514 - MATRIX_ROW_START_CHARPOS (row));
10515 delta_bytes = (Z_BYTE
10516 - BYTEPOS (tlendpos)
10517 - MATRIX_ROW_START_BYTEPOS (row));
10518 }
10519
10520 increment_matrix_positions (w->current_matrix,
10521 this_line_vpos + 1,
10522 w->current_matrix->nrows,
10523 delta, delta_bytes);
10524 }
10525
10526 /* If this row displays text now but previously didn't,
10527 or vice versa, w->window_end_vpos may have to be
10528 adjusted. */
10529 if ((it.glyph_row - 1)->displays_text_p)
10530 {
10531 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10532 XSETINT (w->window_end_vpos, this_line_vpos);
10533 }
10534 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10535 && this_line_vpos > 0)
10536 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10537 w->window_end_valid = Qnil;
10538
10539 /* Update hint: No need to try to scroll in update_window. */
10540 w->desired_matrix->no_scrolling_p = 1;
10541
10542 #if GLYPH_DEBUG
10543 *w->desired_matrix->method = 0;
10544 debug_method_add (w, "optimization 1");
10545 #endif
10546 #ifdef HAVE_WINDOW_SYSTEM
10547 update_window_fringes (w, 0);
10548 #endif
10549 goto update;
10550 }
10551 else
10552 goto cancel;
10553 }
10554 else if (/* Cursor position hasn't changed. */
10555 PT == XFASTINT (w->last_point)
10556 /* Make sure the cursor was last displayed
10557 in this window. Otherwise we have to reposition it. */
10558 && 0 <= w->cursor.vpos
10559 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10560 {
10561 if (!must_finish)
10562 {
10563 do_pending_window_change (1);
10564
10565 /* We used to always goto end_of_redisplay here, but this
10566 isn't enough if we have a blinking cursor. */
10567 if (w->cursor_off_p == w->last_cursor_off_p)
10568 goto end_of_redisplay;
10569 }
10570 goto update;
10571 }
10572 /* If highlighting the region, or if the cursor is in the echo area,
10573 then we can't just move the cursor. */
10574 else if (! (!NILP (Vtransient_mark_mode)
10575 && !NILP (current_buffer->mark_active))
10576 && (EQ (selected_window, current_buffer->last_selected_window)
10577 || highlight_nonselected_windows)
10578 && NILP (w->region_showing)
10579 && NILP (Vshow_trailing_whitespace)
10580 && !cursor_in_echo_area)
10581 {
10582 struct it it;
10583 struct glyph_row *row;
10584
10585 /* Skip from tlbufpos to PT and see where it is. Note that
10586 PT may be in invisible text. If so, we will end at the
10587 next visible position. */
10588 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10589 NULL, DEFAULT_FACE_ID);
10590 it.current_x = this_line_start_x;
10591 it.current_y = this_line_y;
10592 it.vpos = this_line_vpos;
10593
10594 /* The call to move_it_to stops in front of PT, but
10595 moves over before-strings. */
10596 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10597
10598 if (it.vpos == this_line_vpos
10599 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10600 row->enabled_p))
10601 {
10602 xassert (this_line_vpos == it.vpos);
10603 xassert (this_line_y == it.current_y);
10604 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10605 #if GLYPH_DEBUG
10606 *w->desired_matrix->method = 0;
10607 debug_method_add (w, "optimization 3");
10608 #endif
10609 goto update;
10610 }
10611 else
10612 goto cancel;
10613 }
10614
10615 cancel:
10616 /* Text changed drastically or point moved off of line. */
10617 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10618 }
10619
10620 CHARPOS (this_line_start_pos) = 0;
10621 consider_all_windows_p |= buffer_shared > 1;
10622 ++clear_face_cache_count;
10623 #ifdef HAVE_WINDOW_SYSTEM
10624 ++clear_image_cache_count;
10625 #endif
10626
10627 /* Build desired matrices, and update the display. If
10628 consider_all_windows_p is non-zero, do it for all windows on all
10629 frames. Otherwise do it for selected_window, only. */
10630
10631 if (consider_all_windows_p)
10632 {
10633 Lisp_Object tail, frame;
10634
10635 FOR_EACH_FRAME (tail, frame)
10636 XFRAME (frame)->updated_p = 0;
10637
10638 /* Recompute # windows showing selected buffer. This will be
10639 incremented each time such a window is displayed. */
10640 buffer_shared = 0;
10641
10642 FOR_EACH_FRAME (tail, frame)
10643 {
10644 struct frame *f = XFRAME (frame);
10645
10646 if (FRAME_WINDOW_P (f) || f == sf)
10647 {
10648 if (! EQ (frame, selected_frame))
10649 /* Select the frame, for the sake of frame-local
10650 variables. */
10651 select_frame_for_redisplay (frame);
10652
10653 /* Mark all the scroll bars to be removed; we'll redeem
10654 the ones we want when we redisplay their windows. */
10655 if (condemn_scroll_bars_hook)
10656 condemn_scroll_bars_hook (f);
10657
10658 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10659 redisplay_windows (FRAME_ROOT_WINDOW (f));
10660
10661 /* Any scroll bars which redisplay_windows should have
10662 nuked should now go away. */
10663 if (judge_scroll_bars_hook)
10664 judge_scroll_bars_hook (f);
10665
10666 /* If fonts changed, display again. */
10667 /* ??? rms: I suspect it is a mistake to jump all the way
10668 back to retry here. It should just retry this frame. */
10669 if (fonts_changed_p)
10670 goto retry;
10671
10672 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10673 {
10674 /* See if we have to hscroll. */
10675 if (!f->already_hscrolled_p)
10676 {
10677 f->already_hscrolled_p = 1;
10678 if (hscroll_windows (f->root_window))
10679 goto retry;
10680 }
10681
10682 /* Prevent various kinds of signals during display
10683 update. stdio is not robust about handling
10684 signals, which can cause an apparent I/O
10685 error. */
10686 if (interrupt_input)
10687 unrequest_sigio ();
10688 STOP_POLLING;
10689
10690 /* Update the display. */
10691 set_window_update_flags (XWINDOW (f->root_window), 1);
10692 pause |= update_frame (f, 0, 0);
10693 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10694 if (pause)
10695 break;
10696 #endif
10697
10698 f->updated_p = 1;
10699 }
10700 }
10701 }
10702
10703 if (!pause)
10704 {
10705 /* Do the mark_window_display_accurate after all windows have
10706 been redisplayed because this call resets flags in buffers
10707 which are needed for proper redisplay. */
10708 FOR_EACH_FRAME (tail, frame)
10709 {
10710 struct frame *f = XFRAME (frame);
10711 if (f->updated_p)
10712 {
10713 mark_window_display_accurate (f->root_window, 1);
10714 if (frame_up_to_date_hook)
10715 frame_up_to_date_hook (f);
10716 }
10717 }
10718 }
10719 }
10720 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10721 {
10722 Lisp_Object mini_window;
10723 struct frame *mini_frame;
10724
10725 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10726 /* Use list_of_error, not Qerror, so that
10727 we catch only errors and don't run the debugger. */
10728 internal_condition_case_1 (redisplay_window_1, selected_window,
10729 list_of_error,
10730 redisplay_window_error);
10731
10732 /* Compare desired and current matrices, perform output. */
10733
10734 update:
10735 /* If fonts changed, display again. */
10736 if (fonts_changed_p)
10737 goto retry;
10738
10739 /* Prevent various kinds of signals during display update.
10740 stdio is not robust about handling signals,
10741 which can cause an apparent I/O error. */
10742 if (interrupt_input)
10743 unrequest_sigio ();
10744 STOP_POLLING;
10745
10746 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10747 {
10748 if (hscroll_windows (selected_window))
10749 goto retry;
10750
10751 XWINDOW (selected_window)->must_be_updated_p = 1;
10752 pause = update_frame (sf, 0, 0);
10753 }
10754
10755 /* We may have called echo_area_display at the top of this
10756 function. If the echo area is on another frame, that may
10757 have put text on a frame other than the selected one, so the
10758 above call to update_frame would not have caught it. Catch
10759 it here. */
10760 mini_window = FRAME_MINIBUF_WINDOW (sf);
10761 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10762
10763 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10764 {
10765 XWINDOW (mini_window)->must_be_updated_p = 1;
10766 pause |= update_frame (mini_frame, 0, 0);
10767 if (!pause && hscroll_windows (mini_window))
10768 goto retry;
10769 }
10770 }
10771
10772 /* If display was paused because of pending input, make sure we do a
10773 thorough update the next time. */
10774 if (pause)
10775 {
10776 /* Prevent the optimization at the beginning of
10777 redisplay_internal that tries a single-line update of the
10778 line containing the cursor in the selected window. */
10779 CHARPOS (this_line_start_pos) = 0;
10780
10781 /* Let the overlay arrow be updated the next time. */
10782 update_overlay_arrows (0);
10783
10784 /* If we pause after scrolling, some rows in the current
10785 matrices of some windows are not valid. */
10786 if (!WINDOW_FULL_WIDTH_P (w)
10787 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10788 update_mode_lines = 1;
10789 }
10790 else
10791 {
10792 if (!consider_all_windows_p)
10793 {
10794 /* This has already been done above if
10795 consider_all_windows_p is set. */
10796 mark_window_display_accurate_1 (w, 1);
10797
10798 /* Say overlay arrows are up to date. */
10799 update_overlay_arrows (1);
10800
10801 if (frame_up_to_date_hook != 0)
10802 frame_up_to_date_hook (sf);
10803 }
10804
10805 update_mode_lines = 0;
10806 windows_or_buffers_changed = 0;
10807 cursor_type_changed = 0;
10808 }
10809
10810 /* Start SIGIO interrupts coming again. Having them off during the
10811 code above makes it less likely one will discard output, but not
10812 impossible, since there might be stuff in the system buffer here.
10813 But it is much hairier to try to do anything about that. */
10814 if (interrupt_input)
10815 request_sigio ();
10816 RESUME_POLLING;
10817
10818 /* If a frame has become visible which was not before, redisplay
10819 again, so that we display it. Expose events for such a frame
10820 (which it gets when becoming visible) don't call the parts of
10821 redisplay constructing glyphs, so simply exposing a frame won't
10822 display anything in this case. So, we have to display these
10823 frames here explicitly. */
10824 if (!pause)
10825 {
10826 Lisp_Object tail, frame;
10827 int new_count = 0;
10828
10829 FOR_EACH_FRAME (tail, frame)
10830 {
10831 int this_is_visible = 0;
10832
10833 if (XFRAME (frame)->visible)
10834 this_is_visible = 1;
10835 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10836 if (XFRAME (frame)->visible)
10837 this_is_visible = 1;
10838
10839 if (this_is_visible)
10840 new_count++;
10841 }
10842
10843 if (new_count != number_of_visible_frames)
10844 windows_or_buffers_changed++;
10845 }
10846
10847 /* Change frame size now if a change is pending. */
10848 do_pending_window_change (1);
10849
10850 /* If we just did a pending size change, or have additional
10851 visible frames, redisplay again. */
10852 if (windows_or_buffers_changed && !pause)
10853 goto retry;
10854
10855 /* Clear the face cache eventually. */
10856 if (consider_all_windows_p)
10857 {
10858 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10859 {
10860 clear_face_cache (0);
10861 clear_face_cache_count = 0;
10862 }
10863 #ifdef HAVE_WINDOW_SYSTEM
10864 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10865 {
10866 Lisp_Object tail, frame;
10867 FOR_EACH_FRAME (tail, frame)
10868 {
10869 struct frame *f = XFRAME (frame);
10870 if (FRAME_WINDOW_P (f))
10871 clear_image_cache (f, 0);
10872 }
10873 clear_image_cache_count = 0;
10874 }
10875 #endif /* HAVE_WINDOW_SYSTEM */
10876 }
10877
10878 end_of_redisplay:
10879 unbind_to (count, Qnil);
10880 RESUME_POLLING;
10881 }
10882
10883
10884 /* Redisplay, but leave alone any recent echo area message unless
10885 another message has been requested in its place.
10886
10887 This is useful in situations where you need to redisplay but no
10888 user action has occurred, making it inappropriate for the message
10889 area to be cleared. See tracking_off and
10890 wait_reading_process_output for examples of these situations.
10891
10892 FROM_WHERE is an integer saying from where this function was
10893 called. This is useful for debugging. */
10894
10895 void
10896 redisplay_preserve_echo_area (from_where)
10897 int from_where;
10898 {
10899 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10900
10901 if (!NILP (echo_area_buffer[1]))
10902 {
10903 /* We have a previously displayed message, but no current
10904 message. Redisplay the previous message. */
10905 display_last_displayed_message_p = 1;
10906 redisplay_internal (1);
10907 display_last_displayed_message_p = 0;
10908 }
10909 else
10910 redisplay_internal (1);
10911
10912 if (rif != NULL && rif->flush_display_optional)
10913 rif->flush_display_optional (NULL);
10914 }
10915
10916
10917 /* Function registered with record_unwind_protect in
10918 redisplay_internal. Reset redisplaying_p to the value it had
10919 before redisplay_internal was called, and clear
10920 prevent_freeing_realized_faces_p. It also selects the previously
10921 selected frame. */
10922
10923 static Lisp_Object
10924 unwind_redisplay (val)
10925 Lisp_Object val;
10926 {
10927 Lisp_Object old_redisplaying_p, old_frame;
10928
10929 old_redisplaying_p = XCAR (val);
10930 redisplaying_p = XFASTINT (old_redisplaying_p);
10931 old_frame = XCDR (val);
10932 if (! EQ (old_frame, selected_frame))
10933 select_frame_for_redisplay (old_frame);
10934 return Qnil;
10935 }
10936
10937
10938 /* Mark the display of window W as accurate or inaccurate. If
10939 ACCURATE_P is non-zero mark display of W as accurate. If
10940 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10941 redisplay_internal is called. */
10942
10943 static void
10944 mark_window_display_accurate_1 (w, accurate_p)
10945 struct window *w;
10946 int accurate_p;
10947 {
10948 if (BUFFERP (w->buffer))
10949 {
10950 struct buffer *b = XBUFFER (w->buffer);
10951
10952 w->last_modified
10953 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10954 w->last_overlay_modified
10955 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10956 w->last_had_star
10957 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10958
10959 if (accurate_p)
10960 {
10961 b->clip_changed = 0;
10962 b->prevent_redisplay_optimizations_p = 0;
10963
10964 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10965 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10966 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10967 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10968
10969 w->current_matrix->buffer = b;
10970 w->current_matrix->begv = BUF_BEGV (b);
10971 w->current_matrix->zv = BUF_ZV (b);
10972
10973 w->last_cursor = w->cursor;
10974 w->last_cursor_off_p = w->cursor_off_p;
10975
10976 if (w == XWINDOW (selected_window))
10977 w->last_point = make_number (BUF_PT (b));
10978 else
10979 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10980 }
10981 }
10982
10983 if (accurate_p)
10984 {
10985 w->window_end_valid = w->buffer;
10986 #if 0 /* This is incorrect with variable-height lines. */
10987 xassert (XINT (w->window_end_vpos)
10988 < (WINDOW_TOTAL_LINES (w)
10989 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10990 #endif
10991 w->update_mode_line = Qnil;
10992 }
10993 }
10994
10995
10996 /* Mark the display of windows in the window tree rooted at WINDOW as
10997 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10998 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10999 be redisplayed the next time redisplay_internal is called. */
11000
11001 void
11002 mark_window_display_accurate (window, accurate_p)
11003 Lisp_Object window;
11004 int accurate_p;
11005 {
11006 struct window *w;
11007
11008 for (; !NILP (window); window = w->next)
11009 {
11010 w = XWINDOW (window);
11011 mark_window_display_accurate_1 (w, accurate_p);
11012
11013 if (!NILP (w->vchild))
11014 mark_window_display_accurate (w->vchild, accurate_p);
11015 if (!NILP (w->hchild))
11016 mark_window_display_accurate (w->hchild, accurate_p);
11017 }
11018
11019 if (accurate_p)
11020 {
11021 update_overlay_arrows (1);
11022 }
11023 else
11024 {
11025 /* Force a thorough redisplay the next time by setting
11026 last_arrow_position and last_arrow_string to t, which is
11027 unequal to any useful value of Voverlay_arrow_... */
11028 update_overlay_arrows (-1);
11029 }
11030 }
11031
11032
11033 /* Return value in display table DP (Lisp_Char_Table *) for character
11034 C. Since a display table doesn't have any parent, we don't have to
11035 follow parent. Do not call this function directly but use the
11036 macro DISP_CHAR_VECTOR. */
11037
11038 Lisp_Object
11039 disp_char_vector (dp, c)
11040 struct Lisp_Char_Table *dp;
11041 int c;
11042 {
11043 int code[4], i;
11044 Lisp_Object val;
11045
11046 if (SINGLE_BYTE_CHAR_P (c))
11047 return (dp->contents[c]);
11048
11049 SPLIT_CHAR (c, code[0], code[1], code[2]);
11050 if (code[1] < 32)
11051 code[1] = -1;
11052 else if (code[2] < 32)
11053 code[2] = -1;
11054
11055 /* Here, the possible range of code[0] (== charset ID) is
11056 128..max_charset. Since the top level char table contains data
11057 for multibyte characters after 256th element, we must increment
11058 code[0] by 128 to get a correct index. */
11059 code[0] += 128;
11060 code[3] = -1; /* anchor */
11061
11062 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11063 {
11064 val = dp->contents[code[i]];
11065 if (!SUB_CHAR_TABLE_P (val))
11066 return (NILP (val) ? dp->defalt : val);
11067 }
11068
11069 /* Here, val is a sub char table. We return the default value of
11070 it. */
11071 return (dp->defalt);
11072 }
11073
11074
11075 \f
11076 /***********************************************************************
11077 Window Redisplay
11078 ***********************************************************************/
11079
11080 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11081
11082 static void
11083 redisplay_windows (window)
11084 Lisp_Object window;
11085 {
11086 while (!NILP (window))
11087 {
11088 struct window *w = XWINDOW (window);
11089
11090 if (!NILP (w->hchild))
11091 redisplay_windows (w->hchild);
11092 else if (!NILP (w->vchild))
11093 redisplay_windows (w->vchild);
11094 else
11095 {
11096 displayed_buffer = XBUFFER (w->buffer);
11097 /* Use list_of_error, not Qerror, so that
11098 we catch only errors and don't run the debugger. */
11099 internal_condition_case_1 (redisplay_window_0, window,
11100 list_of_error,
11101 redisplay_window_error);
11102 }
11103
11104 window = w->next;
11105 }
11106 }
11107
11108 static Lisp_Object
11109 redisplay_window_error ()
11110 {
11111 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11112 return Qnil;
11113 }
11114
11115 static Lisp_Object
11116 redisplay_window_0 (window)
11117 Lisp_Object window;
11118 {
11119 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11120 redisplay_window (window, 0);
11121 return Qnil;
11122 }
11123
11124 static Lisp_Object
11125 redisplay_window_1 (window)
11126 Lisp_Object window;
11127 {
11128 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11129 redisplay_window (window, 1);
11130 return Qnil;
11131 }
11132 \f
11133
11134 /* Increment GLYPH until it reaches END or CONDITION fails while
11135 adding (GLYPH)->pixel_width to X. */
11136
11137 #define SKIP_GLYPHS(glyph, end, x, condition) \
11138 do \
11139 { \
11140 (x) += (glyph)->pixel_width; \
11141 ++(glyph); \
11142 } \
11143 while ((glyph) < (end) && (condition))
11144
11145
11146 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11147 DELTA is the number of bytes by which positions recorded in ROW
11148 differ from current buffer positions. */
11149
11150 void
11151 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11152 struct window *w;
11153 struct glyph_row *row;
11154 struct glyph_matrix *matrix;
11155 int delta, delta_bytes, dy, dvpos;
11156 {
11157 struct glyph *glyph = row->glyphs[TEXT_AREA];
11158 struct glyph *end = glyph + row->used[TEXT_AREA];
11159 struct glyph *cursor = NULL;
11160 /* The first glyph that starts a sequence of glyphs from string. */
11161 struct glyph *string_start;
11162 /* The X coordinate of string_start. */
11163 int string_start_x;
11164 /* The last known character position. */
11165 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11166 /* The last known character position before string_start. */
11167 int string_before_pos;
11168 int x = row->x;
11169 int cursor_x = x;
11170 int cursor_from_overlay_pos = 0;
11171 int pt_old = PT - delta;
11172
11173 /* Skip over glyphs not having an object at the start of the row.
11174 These are special glyphs like truncation marks on terminal
11175 frames. */
11176 if (row->displays_text_p)
11177 while (glyph < end
11178 && INTEGERP (glyph->object)
11179 && glyph->charpos < 0)
11180 {
11181 x += glyph->pixel_width;
11182 ++glyph;
11183 }
11184
11185 string_start = NULL;
11186 while (glyph < end
11187 && !INTEGERP (glyph->object)
11188 && (!BUFFERP (glyph->object)
11189 || (last_pos = glyph->charpos) < pt_old))
11190 {
11191 if (! STRINGP (glyph->object))
11192 {
11193 string_start = NULL;
11194 x += glyph->pixel_width;
11195 ++glyph;
11196 if (cursor_from_overlay_pos
11197 && last_pos > cursor_from_overlay_pos)
11198 {
11199 cursor_from_overlay_pos = 0;
11200 cursor = 0;
11201 }
11202 }
11203 else
11204 {
11205 string_before_pos = last_pos;
11206 string_start = glyph;
11207 string_start_x = x;
11208 /* Skip all glyphs from string. */
11209 do
11210 {
11211 int pos;
11212 if ((cursor == NULL || glyph > cursor)
11213 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11214 Qcursor, (glyph)->object))
11215 && (pos = string_buffer_position (w, glyph->object,
11216 string_before_pos),
11217 (pos == 0 /* From overlay */
11218 || pos == pt_old)))
11219 {
11220 /* Estimate overlay buffer position from the buffer
11221 positions of the glyphs before and after the overlay.
11222 Add 1 to last_pos so that if point corresponds to the
11223 glyph right after the overlay, we still use a 'cursor'
11224 property found in that overlay. */
11225 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11226 cursor = glyph;
11227 cursor_x = x;
11228 }
11229 x += glyph->pixel_width;
11230 ++glyph;
11231 }
11232 while (glyph < end && STRINGP (glyph->object));
11233 }
11234 }
11235
11236 if (cursor != NULL)
11237 {
11238 glyph = cursor;
11239 x = cursor_x;
11240 }
11241 else if (row->ends_in_ellipsis_p && glyph == end)
11242 {
11243 /* Scan back over the ellipsis glyphs, decrementing positions. */
11244 while (glyph > row->glyphs[TEXT_AREA]
11245 && (glyph - 1)->charpos == last_pos)
11246 glyph--, x -= glyph->pixel_width;
11247 /* That loop always goes one position too far,
11248 including the glyph before the ellipsis.
11249 So scan forward over that one. */
11250 x += glyph->pixel_width;
11251 glyph++;
11252 }
11253 else if (string_start
11254 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11255 {
11256 /* We may have skipped over point because the previous glyphs
11257 are from string. As there's no easy way to know the
11258 character position of the current glyph, find the correct
11259 glyph on point by scanning from string_start again. */
11260 Lisp_Object limit;
11261 Lisp_Object string;
11262 int pos;
11263
11264 limit = make_number (pt_old + 1);
11265 end = glyph;
11266 glyph = string_start;
11267 x = string_start_x;
11268 string = glyph->object;
11269 pos = string_buffer_position (w, string, string_before_pos);
11270 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11271 because we always put cursor after overlay strings. */
11272 while (pos == 0 && glyph < end)
11273 {
11274 string = glyph->object;
11275 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11276 if (glyph < end)
11277 pos = string_buffer_position (w, glyph->object, string_before_pos);
11278 }
11279
11280 while (glyph < end)
11281 {
11282 pos = XINT (Fnext_single_char_property_change
11283 (make_number (pos), Qdisplay, Qnil, limit));
11284 if (pos > pt_old)
11285 break;
11286 /* Skip glyphs from the same string. */
11287 string = glyph->object;
11288 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11289 /* Skip glyphs from an overlay. */
11290 while (glyph < end
11291 && ! string_buffer_position (w, glyph->object, pos))
11292 {
11293 string = glyph->object;
11294 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11295 }
11296 }
11297 }
11298
11299 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11300 w->cursor.x = x;
11301 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11302 w->cursor.y = row->y + dy;
11303
11304 if (w == XWINDOW (selected_window))
11305 {
11306 if (!row->continued_p
11307 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11308 && row->x == 0)
11309 {
11310 this_line_buffer = XBUFFER (w->buffer);
11311
11312 CHARPOS (this_line_start_pos)
11313 = MATRIX_ROW_START_CHARPOS (row) + delta;
11314 BYTEPOS (this_line_start_pos)
11315 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11316
11317 CHARPOS (this_line_end_pos)
11318 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11319 BYTEPOS (this_line_end_pos)
11320 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11321
11322 this_line_y = w->cursor.y;
11323 this_line_pixel_height = row->height;
11324 this_line_vpos = w->cursor.vpos;
11325 this_line_start_x = row->x;
11326 }
11327 else
11328 CHARPOS (this_line_start_pos) = 0;
11329 }
11330 }
11331
11332
11333 /* Run window scroll functions, if any, for WINDOW with new window
11334 start STARTP. Sets the window start of WINDOW to that position.
11335
11336 We assume that the window's buffer is really current. */
11337
11338 static INLINE struct text_pos
11339 run_window_scroll_functions (window, startp)
11340 Lisp_Object window;
11341 struct text_pos startp;
11342 {
11343 struct window *w = XWINDOW (window);
11344 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11345
11346 if (current_buffer != XBUFFER (w->buffer))
11347 abort ();
11348
11349 if (!NILP (Vwindow_scroll_functions))
11350 {
11351 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11352 make_number (CHARPOS (startp)));
11353 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11354 /* In case the hook functions switch buffers. */
11355 if (current_buffer != XBUFFER (w->buffer))
11356 set_buffer_internal_1 (XBUFFER (w->buffer));
11357 }
11358
11359 return startp;
11360 }
11361
11362
11363 /* Make sure the line containing the cursor is fully visible.
11364 A value of 1 means there is nothing to be done.
11365 (Either the line is fully visible, or it cannot be made so,
11366 or we cannot tell.)
11367
11368 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11369 is higher than window.
11370
11371 A value of 0 means the caller should do scrolling
11372 as if point had gone off the screen. */
11373
11374 static int
11375 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11376 struct window *w;
11377 int force_p;
11378 int current_matrix_p;
11379 {
11380 struct glyph_matrix *matrix;
11381 struct glyph_row *row;
11382 int window_height;
11383
11384 if (!make_cursor_line_fully_visible_p)
11385 return 1;
11386
11387 /* It's not always possible to find the cursor, e.g, when a window
11388 is full of overlay strings. Don't do anything in that case. */
11389 if (w->cursor.vpos < 0)
11390 return 1;
11391
11392 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11393 row = MATRIX_ROW (matrix, w->cursor.vpos);
11394
11395 /* If the cursor row is not partially visible, there's nothing to do. */
11396 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11397 return 1;
11398
11399 /* If the row the cursor is in is taller than the window's height,
11400 it's not clear what to do, so do nothing. */
11401 window_height = window_box_height (w);
11402 if (row->height >= window_height)
11403 {
11404 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11405 return 1;
11406 }
11407 return 0;
11408
11409 #if 0
11410 /* This code used to try to scroll the window just enough to make
11411 the line visible. It returned 0 to say that the caller should
11412 allocate larger glyph matrices. */
11413
11414 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11415 {
11416 int dy = row->height - row->visible_height;
11417 w->vscroll = 0;
11418 w->cursor.y += dy;
11419 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11420 }
11421 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11422 {
11423 int dy = - (row->height - row->visible_height);
11424 w->vscroll = dy;
11425 w->cursor.y += dy;
11426 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11427 }
11428
11429 /* When we change the cursor y-position of the selected window,
11430 change this_line_y as well so that the display optimization for
11431 the cursor line of the selected window in redisplay_internal uses
11432 the correct y-position. */
11433 if (w == XWINDOW (selected_window))
11434 this_line_y = w->cursor.y;
11435
11436 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11437 redisplay with larger matrices. */
11438 if (matrix->nrows < required_matrix_height (w))
11439 {
11440 fonts_changed_p = 1;
11441 return 0;
11442 }
11443
11444 return 1;
11445 #endif /* 0 */
11446 }
11447
11448
11449 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11450 non-zero means only WINDOW is redisplayed in redisplay_internal.
11451 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11452 in redisplay_window to bring a partially visible line into view in
11453 the case that only the cursor has moved.
11454
11455 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11456 last screen line's vertical height extends past the end of the screen.
11457
11458 Value is
11459
11460 1 if scrolling succeeded
11461
11462 0 if scrolling didn't find point.
11463
11464 -1 if new fonts have been loaded so that we must interrupt
11465 redisplay, adjust glyph matrices, and try again. */
11466
11467 enum
11468 {
11469 SCROLLING_SUCCESS,
11470 SCROLLING_FAILED,
11471 SCROLLING_NEED_LARGER_MATRICES
11472 };
11473
11474 static int
11475 try_scrolling (window, just_this_one_p, scroll_conservatively,
11476 scroll_step, temp_scroll_step, last_line_misfit)
11477 Lisp_Object window;
11478 int just_this_one_p;
11479 EMACS_INT scroll_conservatively, scroll_step;
11480 int temp_scroll_step;
11481 int last_line_misfit;
11482 {
11483 struct window *w = XWINDOW (window);
11484 struct frame *f = XFRAME (w->frame);
11485 struct text_pos scroll_margin_pos;
11486 struct text_pos pos;
11487 struct text_pos startp;
11488 struct it it;
11489 Lisp_Object window_end;
11490 int this_scroll_margin;
11491 int dy = 0;
11492 int scroll_max;
11493 int rc;
11494 int amount_to_scroll = 0;
11495 Lisp_Object aggressive;
11496 int height;
11497 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11498
11499 #if GLYPH_DEBUG
11500 debug_method_add (w, "try_scrolling");
11501 #endif
11502
11503 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11504
11505 /* Compute scroll margin height in pixels. We scroll when point is
11506 within this distance from the top or bottom of the window. */
11507 if (scroll_margin > 0)
11508 {
11509 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11510 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11511 }
11512 else
11513 this_scroll_margin = 0;
11514
11515 /* Force scroll_conservatively to have a reasonable value so it doesn't
11516 cause an overflow while computing how much to scroll. */
11517 if (scroll_conservatively)
11518 scroll_conservatively = min (scroll_conservatively,
11519 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11520
11521 /* Compute how much we should try to scroll maximally to bring point
11522 into view. */
11523 if (scroll_step || scroll_conservatively || temp_scroll_step)
11524 scroll_max = max (scroll_step,
11525 max (scroll_conservatively, temp_scroll_step));
11526 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11527 || NUMBERP (current_buffer->scroll_up_aggressively))
11528 /* We're trying to scroll because of aggressive scrolling
11529 but no scroll_step is set. Choose an arbitrary one. Maybe
11530 there should be a variable for this. */
11531 scroll_max = 10;
11532 else
11533 scroll_max = 0;
11534 scroll_max *= FRAME_LINE_HEIGHT (f);
11535
11536 /* Decide whether we have to scroll down. Start at the window end
11537 and move this_scroll_margin up to find the position of the scroll
11538 margin. */
11539 window_end = Fwindow_end (window, Qt);
11540
11541 too_near_end:
11542
11543 CHARPOS (scroll_margin_pos) = XINT (window_end);
11544 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11545
11546 if (this_scroll_margin || extra_scroll_margin_lines)
11547 {
11548 start_display (&it, w, scroll_margin_pos);
11549 if (this_scroll_margin)
11550 move_it_vertically_backward (&it, this_scroll_margin);
11551 if (extra_scroll_margin_lines)
11552 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11553 scroll_margin_pos = it.current.pos;
11554 }
11555
11556 if (PT >= CHARPOS (scroll_margin_pos))
11557 {
11558 int y0;
11559
11560 /* Point is in the scroll margin at the bottom of the window, or
11561 below. Compute a new window start that makes point visible. */
11562
11563 /* Compute the distance from the scroll margin to PT.
11564 Give up if the distance is greater than scroll_max. */
11565 start_display (&it, w, scroll_margin_pos);
11566 y0 = it.current_y;
11567 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11568 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11569
11570 /* To make point visible, we have to move the window start
11571 down so that the line the cursor is in is visible, which
11572 means we have to add in the height of the cursor line. */
11573 dy = line_bottom_y (&it) - y0;
11574
11575 if (dy > scroll_max)
11576 return SCROLLING_FAILED;
11577
11578 /* Move the window start down. If scrolling conservatively,
11579 move it just enough down to make point visible. If
11580 scroll_step is set, move it down by scroll_step. */
11581 start_display (&it, w, startp);
11582
11583 if (scroll_conservatively)
11584 /* Set AMOUNT_TO_SCROLL to at least one line,
11585 and at most scroll_conservatively lines. */
11586 amount_to_scroll
11587 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11588 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11589 else if (scroll_step || temp_scroll_step)
11590 amount_to_scroll = scroll_max;
11591 else
11592 {
11593 aggressive = current_buffer->scroll_up_aggressively;
11594 height = WINDOW_BOX_TEXT_HEIGHT (w);
11595 if (NUMBERP (aggressive))
11596 {
11597 double float_amount = XFLOATINT (aggressive) * height;
11598 amount_to_scroll = float_amount;
11599 if (amount_to_scroll == 0 && float_amount > 0)
11600 amount_to_scroll = 1;
11601 }
11602 }
11603
11604 if (amount_to_scroll <= 0)
11605 return SCROLLING_FAILED;
11606
11607 /* If moving by amount_to_scroll leaves STARTP unchanged,
11608 move it down one screen line. */
11609
11610 move_it_vertically (&it, amount_to_scroll);
11611 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11612 move_it_by_lines (&it, 1, 1);
11613 startp = it.current.pos;
11614 }
11615 else
11616 {
11617 /* See if point is inside the scroll margin at the top of the
11618 window. */
11619 scroll_margin_pos = startp;
11620 if (this_scroll_margin)
11621 {
11622 start_display (&it, w, startp);
11623 move_it_vertically (&it, this_scroll_margin);
11624 scroll_margin_pos = it.current.pos;
11625 }
11626
11627 if (PT < CHARPOS (scroll_margin_pos))
11628 {
11629 /* Point is in the scroll margin at the top of the window or
11630 above what is displayed in the window. */
11631 int y0;
11632
11633 /* Compute the vertical distance from PT to the scroll
11634 margin position. Give up if distance is greater than
11635 scroll_max. */
11636 SET_TEXT_POS (pos, PT, PT_BYTE);
11637 start_display (&it, w, pos);
11638 y0 = it.current_y;
11639 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11640 it.last_visible_y, -1,
11641 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11642 dy = it.current_y - y0;
11643 if (dy > scroll_max)
11644 return SCROLLING_FAILED;
11645
11646 /* Compute new window start. */
11647 start_display (&it, w, startp);
11648
11649 if (scroll_conservatively)
11650 amount_to_scroll
11651 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11652 else if (scroll_step || temp_scroll_step)
11653 amount_to_scroll = scroll_max;
11654 else
11655 {
11656 aggressive = current_buffer->scroll_down_aggressively;
11657 height = WINDOW_BOX_TEXT_HEIGHT (w);
11658 if (NUMBERP (aggressive))
11659 {
11660 double float_amount = XFLOATINT (aggressive) * height;
11661 amount_to_scroll = float_amount;
11662 if (amount_to_scroll == 0 && float_amount > 0)
11663 amount_to_scroll = 1;
11664 }
11665 }
11666
11667 if (amount_to_scroll <= 0)
11668 return SCROLLING_FAILED;
11669
11670 move_it_vertically_backward (&it, amount_to_scroll);
11671 startp = it.current.pos;
11672 }
11673 }
11674
11675 /* Run window scroll functions. */
11676 startp = run_window_scroll_functions (window, startp);
11677
11678 /* Display the window. Give up if new fonts are loaded, or if point
11679 doesn't appear. */
11680 if (!try_window (window, startp, 0))
11681 rc = SCROLLING_NEED_LARGER_MATRICES;
11682 else if (w->cursor.vpos < 0)
11683 {
11684 clear_glyph_matrix (w->desired_matrix);
11685 rc = SCROLLING_FAILED;
11686 }
11687 else
11688 {
11689 /* Maybe forget recorded base line for line number display. */
11690 if (!just_this_one_p
11691 || current_buffer->clip_changed
11692 || BEG_UNCHANGED < CHARPOS (startp))
11693 w->base_line_number = Qnil;
11694
11695 /* If cursor ends up on a partially visible line,
11696 treat that as being off the bottom of the screen. */
11697 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11698 {
11699 clear_glyph_matrix (w->desired_matrix);
11700 ++extra_scroll_margin_lines;
11701 goto too_near_end;
11702 }
11703 rc = SCROLLING_SUCCESS;
11704 }
11705
11706 return rc;
11707 }
11708
11709
11710 /* Compute a suitable window start for window W if display of W starts
11711 on a continuation line. Value is non-zero if a new window start
11712 was computed.
11713
11714 The new window start will be computed, based on W's width, starting
11715 from the start of the continued line. It is the start of the
11716 screen line with the minimum distance from the old start W->start. */
11717
11718 static int
11719 compute_window_start_on_continuation_line (w)
11720 struct window *w;
11721 {
11722 struct text_pos pos, start_pos;
11723 int window_start_changed_p = 0;
11724
11725 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11726
11727 /* If window start is on a continuation line... Window start may be
11728 < BEGV in case there's invisible text at the start of the
11729 buffer (M-x rmail, for example). */
11730 if (CHARPOS (start_pos) > BEGV
11731 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11732 {
11733 struct it it;
11734 struct glyph_row *row;
11735
11736 /* Handle the case that the window start is out of range. */
11737 if (CHARPOS (start_pos) < BEGV)
11738 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11739 else if (CHARPOS (start_pos) > ZV)
11740 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11741
11742 /* Find the start of the continued line. This should be fast
11743 because scan_buffer is fast (newline cache). */
11744 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11745 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11746 row, DEFAULT_FACE_ID);
11747 reseat_at_previous_visible_line_start (&it);
11748
11749 /* If the line start is "too far" away from the window start,
11750 say it takes too much time to compute a new window start. */
11751 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11752 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11753 {
11754 int min_distance, distance;
11755
11756 /* Move forward by display lines to find the new window
11757 start. If window width was enlarged, the new start can
11758 be expected to be > the old start. If window width was
11759 decreased, the new window start will be < the old start.
11760 So, we're looking for the display line start with the
11761 minimum distance from the old window start. */
11762 pos = it.current.pos;
11763 min_distance = INFINITY;
11764 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11765 distance < min_distance)
11766 {
11767 min_distance = distance;
11768 pos = it.current.pos;
11769 move_it_by_lines (&it, 1, 0);
11770 }
11771
11772 /* Set the window start there. */
11773 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11774 window_start_changed_p = 1;
11775 }
11776 }
11777
11778 return window_start_changed_p;
11779 }
11780
11781
11782 /* Try cursor movement in case text has not changed in window WINDOW,
11783 with window start STARTP. Value is
11784
11785 CURSOR_MOVEMENT_SUCCESS if successful
11786
11787 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11788
11789 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11790 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11791 we want to scroll as if scroll-step were set to 1. See the code.
11792
11793 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11794 which case we have to abort this redisplay, and adjust matrices
11795 first. */
11796
11797 enum
11798 {
11799 CURSOR_MOVEMENT_SUCCESS,
11800 CURSOR_MOVEMENT_CANNOT_BE_USED,
11801 CURSOR_MOVEMENT_MUST_SCROLL,
11802 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11803 };
11804
11805 static int
11806 try_cursor_movement (window, startp, scroll_step)
11807 Lisp_Object window;
11808 struct text_pos startp;
11809 int *scroll_step;
11810 {
11811 struct window *w = XWINDOW (window);
11812 struct frame *f = XFRAME (w->frame);
11813 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11814
11815 #if GLYPH_DEBUG
11816 if (inhibit_try_cursor_movement)
11817 return rc;
11818 #endif
11819
11820 /* Handle case where text has not changed, only point, and it has
11821 not moved off the frame. */
11822 if (/* Point may be in this window. */
11823 PT >= CHARPOS (startp)
11824 /* Selective display hasn't changed. */
11825 && !current_buffer->clip_changed
11826 /* Function force-mode-line-update is used to force a thorough
11827 redisplay. It sets either windows_or_buffers_changed or
11828 update_mode_lines. So don't take a shortcut here for these
11829 cases. */
11830 && !update_mode_lines
11831 && !windows_or_buffers_changed
11832 && !cursor_type_changed
11833 /* Can't use this case if highlighting a region. When a
11834 region exists, cursor movement has to do more than just
11835 set the cursor. */
11836 && !(!NILP (Vtransient_mark_mode)
11837 && !NILP (current_buffer->mark_active))
11838 && NILP (w->region_showing)
11839 && NILP (Vshow_trailing_whitespace)
11840 /* Right after splitting windows, last_point may be nil. */
11841 && INTEGERP (w->last_point)
11842 /* This code is not used for mini-buffer for the sake of the case
11843 of redisplaying to replace an echo area message; since in
11844 that case the mini-buffer contents per se are usually
11845 unchanged. This code is of no real use in the mini-buffer
11846 since the handling of this_line_start_pos, etc., in redisplay
11847 handles the same cases. */
11848 && !EQ (window, minibuf_window)
11849 /* When splitting windows or for new windows, it happens that
11850 redisplay is called with a nil window_end_vpos or one being
11851 larger than the window. This should really be fixed in
11852 window.c. I don't have this on my list, now, so we do
11853 approximately the same as the old redisplay code. --gerd. */
11854 && INTEGERP (w->window_end_vpos)
11855 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11856 && (FRAME_WINDOW_P (f)
11857 || !overlay_arrow_in_current_buffer_p ()))
11858 {
11859 int this_scroll_margin, top_scroll_margin;
11860 struct glyph_row *row = NULL;
11861
11862 #if GLYPH_DEBUG
11863 debug_method_add (w, "cursor movement");
11864 #endif
11865
11866 /* Scroll if point within this distance from the top or bottom
11867 of the window. This is a pixel value. */
11868 this_scroll_margin = max (0, scroll_margin);
11869 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11870 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11871
11872 top_scroll_margin = this_scroll_margin;
11873 if (WINDOW_WANTS_HEADER_LINE_P (w))
11874 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11875
11876 /* Start with the row the cursor was displayed during the last
11877 not paused redisplay. Give up if that row is not valid. */
11878 if (w->last_cursor.vpos < 0
11879 || w->last_cursor.vpos >= w->current_matrix->nrows)
11880 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11881 else
11882 {
11883 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11884 if (row->mode_line_p)
11885 ++row;
11886 if (!row->enabled_p)
11887 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11888 }
11889
11890 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11891 {
11892 int scroll_p = 0;
11893 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11894
11895 if (PT > XFASTINT (w->last_point))
11896 {
11897 /* Point has moved forward. */
11898 while (MATRIX_ROW_END_CHARPOS (row) < PT
11899 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11900 {
11901 xassert (row->enabled_p);
11902 ++row;
11903 }
11904
11905 /* The end position of a row equals the start position
11906 of the next row. If PT is there, we would rather
11907 display it in the next line. */
11908 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11909 && MATRIX_ROW_END_CHARPOS (row) == PT
11910 && !cursor_row_p (w, row))
11911 ++row;
11912
11913 /* If within the scroll margin, scroll. Note that
11914 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11915 the next line would be drawn, and that
11916 this_scroll_margin can be zero. */
11917 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11918 || PT > MATRIX_ROW_END_CHARPOS (row)
11919 /* Line is completely visible last line in window
11920 and PT is to be set in the next line. */
11921 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11922 && PT == MATRIX_ROW_END_CHARPOS (row)
11923 && !row->ends_at_zv_p
11924 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11925 scroll_p = 1;
11926 }
11927 else if (PT < XFASTINT (w->last_point))
11928 {
11929 /* Cursor has to be moved backward. Note that PT >=
11930 CHARPOS (startp) because of the outer if-statement. */
11931 while (!row->mode_line_p
11932 && (MATRIX_ROW_START_CHARPOS (row) > PT
11933 || (MATRIX_ROW_START_CHARPOS (row) == PT
11934 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
11935 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
11936 row > w->current_matrix->rows
11937 && (row-1)->ends_in_newline_from_string_p))))
11938 && (row->y > top_scroll_margin
11939 || CHARPOS (startp) == BEGV))
11940 {
11941 xassert (row->enabled_p);
11942 --row;
11943 }
11944
11945 /* Consider the following case: Window starts at BEGV,
11946 there is invisible, intangible text at BEGV, so that
11947 display starts at some point START > BEGV. It can
11948 happen that we are called with PT somewhere between
11949 BEGV and START. Try to handle that case. */
11950 if (row < w->current_matrix->rows
11951 || row->mode_line_p)
11952 {
11953 row = w->current_matrix->rows;
11954 if (row->mode_line_p)
11955 ++row;
11956 }
11957
11958 /* Due to newlines in overlay strings, we may have to
11959 skip forward over overlay strings. */
11960 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11961 && MATRIX_ROW_END_CHARPOS (row) == PT
11962 && !cursor_row_p (w, row))
11963 ++row;
11964
11965 /* If within the scroll margin, scroll. */
11966 if (row->y < top_scroll_margin
11967 && CHARPOS (startp) != BEGV)
11968 scroll_p = 1;
11969 }
11970 else
11971 {
11972 /* Cursor did not move. So don't scroll even if cursor line
11973 is partially visible, as it was so before. */
11974 rc = CURSOR_MOVEMENT_SUCCESS;
11975 }
11976
11977 if (PT < MATRIX_ROW_START_CHARPOS (row)
11978 || PT > MATRIX_ROW_END_CHARPOS (row))
11979 {
11980 /* if PT is not in the glyph row, give up. */
11981 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11982 }
11983 else if (rc != CURSOR_MOVEMENT_SUCCESS
11984 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11985 && make_cursor_line_fully_visible_p)
11986 {
11987 if (PT == MATRIX_ROW_END_CHARPOS (row)
11988 && !row->ends_at_zv_p
11989 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11990 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11991 else if (row->height > window_box_height (w))
11992 {
11993 /* If we end up in a partially visible line, let's
11994 make it fully visible, except when it's taller
11995 than the window, in which case we can't do much
11996 about it. */
11997 *scroll_step = 1;
11998 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11999 }
12000 else
12001 {
12002 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12003 if (!cursor_row_fully_visible_p (w, 0, 1))
12004 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12005 else
12006 rc = CURSOR_MOVEMENT_SUCCESS;
12007 }
12008 }
12009 else if (scroll_p)
12010 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12011 else
12012 {
12013 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12014 rc = CURSOR_MOVEMENT_SUCCESS;
12015 }
12016 }
12017 }
12018
12019 return rc;
12020 }
12021
12022 void
12023 set_vertical_scroll_bar (w)
12024 struct window *w;
12025 {
12026 int start, end, whole;
12027
12028 /* Calculate the start and end positions for the current window.
12029 At some point, it would be nice to choose between scrollbars
12030 which reflect the whole buffer size, with special markers
12031 indicating narrowing, and scrollbars which reflect only the
12032 visible region.
12033
12034 Note that mini-buffers sometimes aren't displaying any text. */
12035 if (!MINI_WINDOW_P (w)
12036 || (w == XWINDOW (minibuf_window)
12037 && NILP (echo_area_buffer[0])))
12038 {
12039 struct buffer *buf = XBUFFER (w->buffer);
12040 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12041 start = marker_position (w->start) - BUF_BEGV (buf);
12042 /* I don't think this is guaranteed to be right. For the
12043 moment, we'll pretend it is. */
12044 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12045
12046 if (end < start)
12047 end = start;
12048 if (whole < (end - start))
12049 whole = end - start;
12050 }
12051 else
12052 start = end = whole = 0;
12053
12054 /* Indicate what this scroll bar ought to be displaying now. */
12055 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12056 }
12057
12058
12059 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12060 selected_window is redisplayed.
12061
12062 We can return without actually redisplaying the window if
12063 fonts_changed_p is nonzero. In that case, redisplay_internal will
12064 retry. */
12065
12066 static void
12067 redisplay_window (window, just_this_one_p)
12068 Lisp_Object window;
12069 int just_this_one_p;
12070 {
12071 struct window *w = XWINDOW (window);
12072 struct frame *f = XFRAME (w->frame);
12073 struct buffer *buffer = XBUFFER (w->buffer);
12074 struct buffer *old = current_buffer;
12075 struct text_pos lpoint, opoint, startp;
12076 int update_mode_line;
12077 int tem;
12078 struct it it;
12079 /* Record it now because it's overwritten. */
12080 int current_matrix_up_to_date_p = 0;
12081 int used_current_matrix_p = 0;
12082 /* This is less strict than current_matrix_up_to_date_p.
12083 It indictes that the buffer contents and narrowing are unchanged. */
12084 int buffer_unchanged_p = 0;
12085 int temp_scroll_step = 0;
12086 int count = SPECPDL_INDEX ();
12087 int rc;
12088 int centering_position = -1;
12089 int last_line_misfit = 0;
12090
12091 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12092 opoint = lpoint;
12093
12094 /* W must be a leaf window here. */
12095 xassert (!NILP (w->buffer));
12096 #if GLYPH_DEBUG
12097 *w->desired_matrix->method = 0;
12098 #endif
12099
12100 specbind (Qinhibit_point_motion_hooks, Qt);
12101
12102 reconsider_clip_changes (w, buffer);
12103
12104 /* Has the mode line to be updated? */
12105 update_mode_line = (!NILP (w->update_mode_line)
12106 || update_mode_lines
12107 || buffer->clip_changed
12108 || buffer->prevent_redisplay_optimizations_p);
12109
12110 if (MINI_WINDOW_P (w))
12111 {
12112 if (w == XWINDOW (echo_area_window)
12113 && !NILP (echo_area_buffer[0]))
12114 {
12115 if (update_mode_line)
12116 /* We may have to update a tty frame's menu bar or a
12117 tool-bar. Example `M-x C-h C-h C-g'. */
12118 goto finish_menu_bars;
12119 else
12120 /* We've already displayed the echo area glyphs in this window. */
12121 goto finish_scroll_bars;
12122 }
12123 else if ((w != XWINDOW (minibuf_window)
12124 || minibuf_level == 0)
12125 /* When buffer is nonempty, redisplay window normally. */
12126 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12127 /* Quail displays non-mini buffers in minibuffer window.
12128 In that case, redisplay the window normally. */
12129 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12130 {
12131 /* W is a mini-buffer window, but it's not active, so clear
12132 it. */
12133 int yb = window_text_bottom_y (w);
12134 struct glyph_row *row;
12135 int y;
12136
12137 for (y = 0, row = w->desired_matrix->rows;
12138 y < yb;
12139 y += row->height, ++row)
12140 blank_row (w, row, y);
12141 goto finish_scroll_bars;
12142 }
12143
12144 clear_glyph_matrix (w->desired_matrix);
12145 }
12146
12147 /* Otherwise set up data on this window; select its buffer and point
12148 value. */
12149 /* Really select the buffer, for the sake of buffer-local
12150 variables. */
12151 set_buffer_internal_1 (XBUFFER (w->buffer));
12152 SET_TEXT_POS (opoint, PT, PT_BYTE);
12153
12154 current_matrix_up_to_date_p
12155 = (!NILP (w->window_end_valid)
12156 && !current_buffer->clip_changed
12157 && !current_buffer->prevent_redisplay_optimizations_p
12158 && XFASTINT (w->last_modified) >= MODIFF
12159 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12160
12161 buffer_unchanged_p
12162 = (!NILP (w->window_end_valid)
12163 && !current_buffer->clip_changed
12164 && XFASTINT (w->last_modified) >= MODIFF
12165 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12166
12167 /* When windows_or_buffers_changed is non-zero, we can't rely on
12168 the window end being valid, so set it to nil there. */
12169 if (windows_or_buffers_changed)
12170 {
12171 /* If window starts on a continuation line, maybe adjust the
12172 window start in case the window's width changed. */
12173 if (XMARKER (w->start)->buffer == current_buffer)
12174 compute_window_start_on_continuation_line (w);
12175
12176 w->window_end_valid = Qnil;
12177 }
12178
12179 /* Some sanity checks. */
12180 CHECK_WINDOW_END (w);
12181 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12182 abort ();
12183 if (BYTEPOS (opoint) < CHARPOS (opoint))
12184 abort ();
12185
12186 /* If %c is in mode line, update it if needed. */
12187 if (!NILP (w->column_number_displayed)
12188 /* This alternative quickly identifies a common case
12189 where no change is needed. */
12190 && !(PT == XFASTINT (w->last_point)
12191 && XFASTINT (w->last_modified) >= MODIFF
12192 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12193 && (XFASTINT (w->column_number_displayed)
12194 != (int) current_column ())) /* iftc */
12195 update_mode_line = 1;
12196
12197 /* Count number of windows showing the selected buffer. An indirect
12198 buffer counts as its base buffer. */
12199 if (!just_this_one_p)
12200 {
12201 struct buffer *current_base, *window_base;
12202 current_base = current_buffer;
12203 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12204 if (current_base->base_buffer)
12205 current_base = current_base->base_buffer;
12206 if (window_base->base_buffer)
12207 window_base = window_base->base_buffer;
12208 if (current_base == window_base)
12209 buffer_shared++;
12210 }
12211
12212 /* Point refers normally to the selected window. For any other
12213 window, set up appropriate value. */
12214 if (!EQ (window, selected_window))
12215 {
12216 int new_pt = XMARKER (w->pointm)->charpos;
12217 int new_pt_byte = marker_byte_position (w->pointm);
12218 if (new_pt < BEGV)
12219 {
12220 new_pt = BEGV;
12221 new_pt_byte = BEGV_BYTE;
12222 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12223 }
12224 else if (new_pt > (ZV - 1))
12225 {
12226 new_pt = ZV;
12227 new_pt_byte = ZV_BYTE;
12228 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12229 }
12230
12231 /* We don't use SET_PT so that the point-motion hooks don't run. */
12232 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12233 }
12234
12235 /* If any of the character widths specified in the display table
12236 have changed, invalidate the width run cache. It's true that
12237 this may be a bit late to catch such changes, but the rest of
12238 redisplay goes (non-fatally) haywire when the display table is
12239 changed, so why should we worry about doing any better? */
12240 if (current_buffer->width_run_cache)
12241 {
12242 struct Lisp_Char_Table *disptab = buffer_display_table ();
12243
12244 if (! disptab_matches_widthtab (disptab,
12245 XVECTOR (current_buffer->width_table)))
12246 {
12247 invalidate_region_cache (current_buffer,
12248 current_buffer->width_run_cache,
12249 BEG, Z);
12250 recompute_width_table (current_buffer, disptab);
12251 }
12252 }
12253
12254 /* If window-start is screwed up, choose a new one. */
12255 if (XMARKER (w->start)->buffer != current_buffer)
12256 goto recenter;
12257
12258 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12259
12260 /* If someone specified a new starting point but did not insist,
12261 check whether it can be used. */
12262 if (!NILP (w->optional_new_start)
12263 && CHARPOS (startp) >= BEGV
12264 && CHARPOS (startp) <= ZV)
12265 {
12266 w->optional_new_start = Qnil;
12267 start_display (&it, w, startp);
12268 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12269 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12270 if (IT_CHARPOS (it) == PT)
12271 w->force_start = Qt;
12272 /* IT may overshoot PT if text at PT is invisible. */
12273 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12274 w->force_start = Qt;
12275
12276
12277 }
12278
12279 /* Handle case where place to start displaying has been specified,
12280 unless the specified location is outside the accessible range. */
12281 if (!NILP (w->force_start)
12282 || w->frozen_window_start_p)
12283 {
12284 /* We set this later on if we have to adjust point. */
12285 int new_vpos = -1;
12286 int val;
12287
12288 w->force_start = Qnil;
12289 w->vscroll = 0;
12290 w->window_end_valid = Qnil;
12291
12292 /* Forget any recorded base line for line number display. */
12293 if (!buffer_unchanged_p)
12294 w->base_line_number = Qnil;
12295
12296 /* Redisplay the mode line. Select the buffer properly for that.
12297 Also, run the hook window-scroll-functions
12298 because we have scrolled. */
12299 /* Note, we do this after clearing force_start because
12300 if there's an error, it is better to forget about force_start
12301 than to get into an infinite loop calling the hook functions
12302 and having them get more errors. */
12303 if (!update_mode_line
12304 || ! NILP (Vwindow_scroll_functions))
12305 {
12306 update_mode_line = 1;
12307 w->update_mode_line = Qt;
12308 startp = run_window_scroll_functions (window, startp);
12309 }
12310
12311 w->last_modified = make_number (0);
12312 w->last_overlay_modified = make_number (0);
12313 if (CHARPOS (startp) < BEGV)
12314 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12315 else if (CHARPOS (startp) > ZV)
12316 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12317
12318 /* Redisplay, then check if cursor has been set during the
12319 redisplay. Give up if new fonts were loaded. */
12320 val = try_window (window, startp, 1);
12321 if (!val)
12322 {
12323 w->force_start = Qt;
12324 clear_glyph_matrix (w->desired_matrix);
12325 goto need_larger_matrices;
12326 }
12327 /* Point was outside the scroll margins. */
12328 if (val < 0)
12329 new_vpos = window_box_height (w) / 2;
12330
12331 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12332 {
12333 /* If point does not appear, try to move point so it does
12334 appear. The desired matrix has been built above, so we
12335 can use it here. */
12336 new_vpos = window_box_height (w) / 2;
12337 }
12338
12339 if (!cursor_row_fully_visible_p (w, 0, 0))
12340 {
12341 /* Point does appear, but on a line partly visible at end of window.
12342 Move it back to a fully-visible line. */
12343 new_vpos = window_box_height (w);
12344 }
12345
12346 /* If we need to move point for either of the above reasons,
12347 now actually do it. */
12348 if (new_vpos >= 0)
12349 {
12350 struct glyph_row *row;
12351
12352 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12353 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12354 ++row;
12355
12356 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12357 MATRIX_ROW_START_BYTEPOS (row));
12358
12359 if (w != XWINDOW (selected_window))
12360 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12361 else if (current_buffer == old)
12362 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12363
12364 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12365
12366 /* If we are highlighting the region, then we just changed
12367 the region, so redisplay to show it. */
12368 if (!NILP (Vtransient_mark_mode)
12369 && !NILP (current_buffer->mark_active))
12370 {
12371 clear_glyph_matrix (w->desired_matrix);
12372 if (!try_window (window, startp, 0))
12373 goto need_larger_matrices;
12374 }
12375 }
12376
12377 #if GLYPH_DEBUG
12378 debug_method_add (w, "forced window start");
12379 #endif
12380 goto done;
12381 }
12382
12383 /* Handle case where text has not changed, only point, and it has
12384 not moved off the frame, and we are not retrying after hscroll.
12385 (current_matrix_up_to_date_p is nonzero when retrying.) */
12386 if (current_matrix_up_to_date_p
12387 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12388 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12389 {
12390 switch (rc)
12391 {
12392 case CURSOR_MOVEMENT_SUCCESS:
12393 used_current_matrix_p = 1;
12394 goto done;
12395
12396 #if 0 /* try_cursor_movement never returns this value. */
12397 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12398 goto need_larger_matrices;
12399 #endif
12400
12401 case CURSOR_MOVEMENT_MUST_SCROLL:
12402 goto try_to_scroll;
12403
12404 default:
12405 abort ();
12406 }
12407 }
12408 /* If current starting point was originally the beginning of a line
12409 but no longer is, find a new starting point. */
12410 else if (!NILP (w->start_at_line_beg)
12411 && !(CHARPOS (startp) <= BEGV
12412 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12413 {
12414 #if GLYPH_DEBUG
12415 debug_method_add (w, "recenter 1");
12416 #endif
12417 goto recenter;
12418 }
12419
12420 /* Try scrolling with try_window_id. Value is > 0 if update has
12421 been done, it is -1 if we know that the same window start will
12422 not work. It is 0 if unsuccessful for some other reason. */
12423 else if ((tem = try_window_id (w)) != 0)
12424 {
12425 #if GLYPH_DEBUG
12426 debug_method_add (w, "try_window_id %d", tem);
12427 #endif
12428
12429 if (fonts_changed_p)
12430 goto need_larger_matrices;
12431 if (tem > 0)
12432 goto done;
12433
12434 /* Otherwise try_window_id has returned -1 which means that we
12435 don't want the alternative below this comment to execute. */
12436 }
12437 else if (CHARPOS (startp) >= BEGV
12438 && CHARPOS (startp) <= ZV
12439 && PT >= CHARPOS (startp)
12440 && (CHARPOS (startp) < ZV
12441 /* Avoid starting at end of buffer. */
12442 || CHARPOS (startp) == BEGV
12443 || (XFASTINT (w->last_modified) >= MODIFF
12444 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12445 {
12446 #if GLYPH_DEBUG
12447 debug_method_add (w, "same window start");
12448 #endif
12449
12450 /* Try to redisplay starting at same place as before.
12451 If point has not moved off frame, accept the results. */
12452 if (!current_matrix_up_to_date_p
12453 /* Don't use try_window_reusing_current_matrix in this case
12454 because a window scroll function can have changed the
12455 buffer. */
12456 || !NILP (Vwindow_scroll_functions)
12457 || MINI_WINDOW_P (w)
12458 || !(used_current_matrix_p
12459 = try_window_reusing_current_matrix (w)))
12460 {
12461 IF_DEBUG (debug_method_add (w, "1"));
12462 if (try_window (window, startp, 1) < 0)
12463 /* -1 means we need to scroll.
12464 0 means we need new matrices, but fonts_changed_p
12465 is set in that case, so we will detect it below. */
12466 goto try_to_scroll;
12467 }
12468
12469 if (fonts_changed_p)
12470 goto need_larger_matrices;
12471
12472 if (w->cursor.vpos >= 0)
12473 {
12474 if (!just_this_one_p
12475 || current_buffer->clip_changed
12476 || BEG_UNCHANGED < CHARPOS (startp))
12477 /* Forget any recorded base line for line number display. */
12478 w->base_line_number = Qnil;
12479
12480 if (!cursor_row_fully_visible_p (w, 1, 0))
12481 {
12482 clear_glyph_matrix (w->desired_matrix);
12483 last_line_misfit = 1;
12484 }
12485 /* Drop through and scroll. */
12486 else
12487 goto done;
12488 }
12489 else
12490 clear_glyph_matrix (w->desired_matrix);
12491 }
12492
12493 try_to_scroll:
12494
12495 w->last_modified = make_number (0);
12496 w->last_overlay_modified = make_number (0);
12497
12498 /* Redisplay the mode line. Select the buffer properly for that. */
12499 if (!update_mode_line)
12500 {
12501 update_mode_line = 1;
12502 w->update_mode_line = Qt;
12503 }
12504
12505 /* Try to scroll by specified few lines. */
12506 if ((scroll_conservatively
12507 || scroll_step
12508 || temp_scroll_step
12509 || NUMBERP (current_buffer->scroll_up_aggressively)
12510 || NUMBERP (current_buffer->scroll_down_aggressively))
12511 && !current_buffer->clip_changed
12512 && CHARPOS (startp) >= BEGV
12513 && CHARPOS (startp) <= ZV)
12514 {
12515 /* The function returns -1 if new fonts were loaded, 1 if
12516 successful, 0 if not successful. */
12517 int rc = try_scrolling (window, just_this_one_p,
12518 scroll_conservatively,
12519 scroll_step,
12520 temp_scroll_step, last_line_misfit);
12521 switch (rc)
12522 {
12523 case SCROLLING_SUCCESS:
12524 goto done;
12525
12526 case SCROLLING_NEED_LARGER_MATRICES:
12527 goto need_larger_matrices;
12528
12529 case SCROLLING_FAILED:
12530 break;
12531
12532 default:
12533 abort ();
12534 }
12535 }
12536
12537 /* Finally, just choose place to start which centers point */
12538
12539 recenter:
12540 if (centering_position < 0)
12541 centering_position = window_box_height (w) / 2;
12542
12543 #if GLYPH_DEBUG
12544 debug_method_add (w, "recenter");
12545 #endif
12546
12547 /* w->vscroll = 0; */
12548
12549 /* Forget any previously recorded base line for line number display. */
12550 if (!buffer_unchanged_p)
12551 w->base_line_number = Qnil;
12552
12553 /* Move backward half the height of the window. */
12554 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12555 it.current_y = it.last_visible_y;
12556 move_it_vertically_backward (&it, centering_position);
12557 xassert (IT_CHARPOS (it) >= BEGV);
12558
12559 /* The function move_it_vertically_backward may move over more
12560 than the specified y-distance. If it->w is small, e.g. a
12561 mini-buffer window, we may end up in front of the window's
12562 display area. Start displaying at the start of the line
12563 containing PT in this case. */
12564 if (it.current_y <= 0)
12565 {
12566 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12567 move_it_vertically_backward (&it, 0);
12568 #if 0
12569 /* I think this assert is bogus if buffer contains
12570 invisible text or images. KFS. */
12571 xassert (IT_CHARPOS (it) <= PT);
12572 #endif
12573 it.current_y = 0;
12574 }
12575
12576 it.current_x = it.hpos = 0;
12577
12578 /* Set startp here explicitly in case that helps avoid an infinite loop
12579 in case the window-scroll-functions functions get errors. */
12580 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12581
12582 /* Run scroll hooks. */
12583 startp = run_window_scroll_functions (window, it.current.pos);
12584
12585 /* Redisplay the window. */
12586 if (!current_matrix_up_to_date_p
12587 || windows_or_buffers_changed
12588 || cursor_type_changed
12589 /* Don't use try_window_reusing_current_matrix in this case
12590 because it can have changed the buffer. */
12591 || !NILP (Vwindow_scroll_functions)
12592 || !just_this_one_p
12593 || MINI_WINDOW_P (w)
12594 || !(used_current_matrix_p
12595 = try_window_reusing_current_matrix (w)))
12596 try_window (window, startp, 0);
12597
12598 /* If new fonts have been loaded (due to fontsets), give up. We
12599 have to start a new redisplay since we need to re-adjust glyph
12600 matrices. */
12601 if (fonts_changed_p)
12602 goto need_larger_matrices;
12603
12604 /* If cursor did not appear assume that the middle of the window is
12605 in the first line of the window. Do it again with the next line.
12606 (Imagine a window of height 100, displaying two lines of height
12607 60. Moving back 50 from it->last_visible_y will end in the first
12608 line.) */
12609 if (w->cursor.vpos < 0)
12610 {
12611 if (!NILP (w->window_end_valid)
12612 && PT >= Z - XFASTINT (w->window_end_pos))
12613 {
12614 clear_glyph_matrix (w->desired_matrix);
12615 move_it_by_lines (&it, 1, 0);
12616 try_window (window, it.current.pos, 0);
12617 }
12618 else if (PT < IT_CHARPOS (it))
12619 {
12620 clear_glyph_matrix (w->desired_matrix);
12621 move_it_by_lines (&it, -1, 0);
12622 try_window (window, it.current.pos, 0);
12623 }
12624 else
12625 {
12626 /* Not much we can do about it. */
12627 }
12628 }
12629
12630 /* Consider the following case: Window starts at BEGV, there is
12631 invisible, intangible text at BEGV, so that display starts at
12632 some point START > BEGV. It can happen that we are called with
12633 PT somewhere between BEGV and START. Try to handle that case. */
12634 if (w->cursor.vpos < 0)
12635 {
12636 struct glyph_row *row = w->current_matrix->rows;
12637 if (row->mode_line_p)
12638 ++row;
12639 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12640 }
12641
12642 if (!cursor_row_fully_visible_p (w, 0, 0))
12643 {
12644 /* If vscroll is enabled, disable it and try again. */
12645 if (w->vscroll)
12646 {
12647 w->vscroll = 0;
12648 clear_glyph_matrix (w->desired_matrix);
12649 goto recenter;
12650 }
12651
12652 /* If centering point failed to make the whole line visible,
12653 put point at the top instead. That has to make the whole line
12654 visible, if it can be done. */
12655 if (centering_position == 0)
12656 goto done;
12657
12658 clear_glyph_matrix (w->desired_matrix);
12659 centering_position = 0;
12660 goto recenter;
12661 }
12662
12663 done:
12664
12665 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12666 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12667 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12668 ? Qt : Qnil);
12669
12670 /* Display the mode line, if we must. */
12671 if ((update_mode_line
12672 /* If window not full width, must redo its mode line
12673 if (a) the window to its side is being redone and
12674 (b) we do a frame-based redisplay. This is a consequence
12675 of how inverted lines are drawn in frame-based redisplay. */
12676 || (!just_this_one_p
12677 && !FRAME_WINDOW_P (f)
12678 && !WINDOW_FULL_WIDTH_P (w))
12679 /* Line number to display. */
12680 || INTEGERP (w->base_line_pos)
12681 /* Column number is displayed and different from the one displayed. */
12682 || (!NILP (w->column_number_displayed)
12683 && (XFASTINT (w->column_number_displayed)
12684 != (int) current_column ()))) /* iftc */
12685 /* This means that the window has a mode line. */
12686 && (WINDOW_WANTS_MODELINE_P (w)
12687 || WINDOW_WANTS_HEADER_LINE_P (w)))
12688 {
12689 display_mode_lines (w);
12690
12691 /* If mode line height has changed, arrange for a thorough
12692 immediate redisplay using the correct mode line height. */
12693 if (WINDOW_WANTS_MODELINE_P (w)
12694 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12695 {
12696 fonts_changed_p = 1;
12697 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12698 = DESIRED_MODE_LINE_HEIGHT (w);
12699 }
12700
12701 /* If top line height has changed, arrange for a thorough
12702 immediate redisplay using the correct mode line height. */
12703 if (WINDOW_WANTS_HEADER_LINE_P (w)
12704 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12705 {
12706 fonts_changed_p = 1;
12707 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12708 = DESIRED_HEADER_LINE_HEIGHT (w);
12709 }
12710
12711 if (fonts_changed_p)
12712 goto need_larger_matrices;
12713 }
12714
12715 if (!line_number_displayed
12716 && !BUFFERP (w->base_line_pos))
12717 {
12718 w->base_line_pos = Qnil;
12719 w->base_line_number = Qnil;
12720 }
12721
12722 finish_menu_bars:
12723
12724 /* When we reach a frame's selected window, redo the frame's menu bar. */
12725 if (update_mode_line
12726 && EQ (FRAME_SELECTED_WINDOW (f), window))
12727 {
12728 int redisplay_menu_p = 0;
12729 int redisplay_tool_bar_p = 0;
12730
12731 if (FRAME_WINDOW_P (f))
12732 {
12733 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12734 || defined (USE_GTK)
12735 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12736 #else
12737 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12738 #endif
12739 }
12740 else
12741 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12742
12743 if (redisplay_menu_p)
12744 display_menu_bar (w);
12745
12746 #ifdef HAVE_WINDOW_SYSTEM
12747 #ifdef USE_GTK
12748 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12749 #else
12750 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12751 && (FRAME_TOOL_BAR_LINES (f) > 0
12752 || auto_resize_tool_bars_p);
12753
12754 #endif
12755
12756 if (redisplay_tool_bar_p)
12757 redisplay_tool_bar (f);
12758 #endif
12759 }
12760
12761 #ifdef HAVE_WINDOW_SYSTEM
12762 if (FRAME_WINDOW_P (f)
12763 && update_window_fringes (w, (just_this_one_p
12764 || (!used_current_matrix_p && !overlay_arrow_seen)
12765 || w->pseudo_window_p)))
12766 {
12767 update_begin (f);
12768 BLOCK_INPUT;
12769 if (draw_window_fringes (w, 1))
12770 x_draw_vertical_border (w);
12771 UNBLOCK_INPUT;
12772 update_end (f);
12773 }
12774 #endif /* HAVE_WINDOW_SYSTEM */
12775
12776 /* We go to this label, with fonts_changed_p nonzero,
12777 if it is necessary to try again using larger glyph matrices.
12778 We have to redeem the scroll bar even in this case,
12779 because the loop in redisplay_internal expects that. */
12780 need_larger_matrices:
12781 ;
12782 finish_scroll_bars:
12783
12784 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12785 {
12786 /* Set the thumb's position and size. */
12787 set_vertical_scroll_bar (w);
12788
12789 /* Note that we actually used the scroll bar attached to this
12790 window, so it shouldn't be deleted at the end of redisplay. */
12791 redeem_scroll_bar_hook (w);
12792 }
12793
12794 /* Restore current_buffer and value of point in it. */
12795 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12796 set_buffer_internal_1 (old);
12797 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12798
12799 unbind_to (count, Qnil);
12800 }
12801
12802
12803 /* Build the complete desired matrix of WINDOW with a window start
12804 buffer position POS.
12805
12806 Value is 1 if successful. It is zero if fonts were loaded during
12807 redisplay which makes re-adjusting glyph matrices necessary, and -1
12808 if point would appear in the scroll margins.
12809 (We check that only if CHECK_MARGINS is nonzero. */
12810
12811 int
12812 try_window (window, pos, check_margins)
12813 Lisp_Object window;
12814 struct text_pos pos;
12815 int check_margins;
12816 {
12817 struct window *w = XWINDOW (window);
12818 struct it it;
12819 struct glyph_row *last_text_row = NULL;
12820
12821 /* Make POS the new window start. */
12822 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12823
12824 /* Mark cursor position as unknown. No overlay arrow seen. */
12825 w->cursor.vpos = -1;
12826 overlay_arrow_seen = 0;
12827
12828 /* Initialize iterator and info to start at POS. */
12829 start_display (&it, w, pos);
12830
12831 /* Display all lines of W. */
12832 while (it.current_y < it.last_visible_y)
12833 {
12834 if (display_line (&it))
12835 last_text_row = it.glyph_row - 1;
12836 if (fonts_changed_p)
12837 return 0;
12838 }
12839
12840 /* Don't let the cursor end in the scroll margins. */
12841 if (check_margins
12842 && !MINI_WINDOW_P (w))
12843 {
12844 int this_scroll_margin;
12845
12846 this_scroll_margin = max (0, scroll_margin);
12847 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12848 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
12849
12850 if ((w->cursor.y < this_scroll_margin
12851 && CHARPOS (pos) > BEGV)
12852 /* rms: considering make_cursor_line_fully_visible_p here
12853 seems to give wrong results. We don't want to recenter
12854 when the last line is partly visible, we want to allow
12855 that case to be handled in the usual way. */
12856 || (w->cursor.y + 1) > it.last_visible_y)
12857 {
12858 w->cursor.vpos = -1;
12859 clear_glyph_matrix (w->desired_matrix);
12860 return -1;
12861 }
12862 }
12863
12864 /* If bottom moved off end of frame, change mode line percentage. */
12865 if (XFASTINT (w->window_end_pos) <= 0
12866 && Z != IT_CHARPOS (it))
12867 w->update_mode_line = Qt;
12868
12869 /* Set window_end_pos to the offset of the last character displayed
12870 on the window from the end of current_buffer. Set
12871 window_end_vpos to its row number. */
12872 if (last_text_row)
12873 {
12874 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12875 w->window_end_bytepos
12876 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12877 w->window_end_pos
12878 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12879 w->window_end_vpos
12880 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12881 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12882 ->displays_text_p);
12883 }
12884 else
12885 {
12886 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12887 w->window_end_pos = make_number (Z - ZV);
12888 w->window_end_vpos = make_number (0);
12889 }
12890
12891 /* But that is not valid info until redisplay finishes. */
12892 w->window_end_valid = Qnil;
12893 return 1;
12894 }
12895
12896
12897 \f
12898 /************************************************************************
12899 Window redisplay reusing current matrix when buffer has not changed
12900 ************************************************************************/
12901
12902 /* Try redisplay of window W showing an unchanged buffer with a
12903 different window start than the last time it was displayed by
12904 reusing its current matrix. Value is non-zero if successful.
12905 W->start is the new window start. */
12906
12907 static int
12908 try_window_reusing_current_matrix (w)
12909 struct window *w;
12910 {
12911 struct frame *f = XFRAME (w->frame);
12912 struct glyph_row *row, *bottom_row;
12913 struct it it;
12914 struct run run;
12915 struct text_pos start, new_start;
12916 int nrows_scrolled, i;
12917 struct glyph_row *last_text_row;
12918 struct glyph_row *last_reused_text_row;
12919 struct glyph_row *start_row;
12920 int start_vpos, min_y, max_y;
12921
12922 #if GLYPH_DEBUG
12923 if (inhibit_try_window_reusing)
12924 return 0;
12925 #endif
12926
12927 if (/* This function doesn't handle terminal frames. */
12928 !FRAME_WINDOW_P (f)
12929 /* Don't try to reuse the display if windows have been split
12930 or such. */
12931 || windows_or_buffers_changed
12932 || cursor_type_changed)
12933 return 0;
12934
12935 /* Can't do this if region may have changed. */
12936 if ((!NILP (Vtransient_mark_mode)
12937 && !NILP (current_buffer->mark_active))
12938 || !NILP (w->region_showing)
12939 || !NILP (Vshow_trailing_whitespace))
12940 return 0;
12941
12942 /* If top-line visibility has changed, give up. */
12943 if (WINDOW_WANTS_HEADER_LINE_P (w)
12944 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12945 return 0;
12946
12947 /* Give up if old or new display is scrolled vertically. We could
12948 make this function handle this, but right now it doesn't. */
12949 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12950 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12951 return 0;
12952
12953 /* The variable new_start now holds the new window start. The old
12954 start `start' can be determined from the current matrix. */
12955 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12956 start = start_row->start.pos;
12957 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12958
12959 /* Clear the desired matrix for the display below. */
12960 clear_glyph_matrix (w->desired_matrix);
12961
12962 if (CHARPOS (new_start) <= CHARPOS (start))
12963 {
12964 int first_row_y;
12965
12966 /* Don't use this method if the display starts with an ellipsis
12967 displayed for invisible text. It's not easy to handle that case
12968 below, and it's certainly not worth the effort since this is
12969 not a frequent case. */
12970 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12971 return 0;
12972
12973 IF_DEBUG (debug_method_add (w, "twu1"));
12974
12975 /* Display up to a row that can be reused. The variable
12976 last_text_row is set to the last row displayed that displays
12977 text. Note that it.vpos == 0 if or if not there is a
12978 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12979 start_display (&it, w, new_start);
12980 first_row_y = it.current_y;
12981 w->cursor.vpos = -1;
12982 last_text_row = last_reused_text_row = NULL;
12983
12984 while (it.current_y < it.last_visible_y
12985 && !fonts_changed_p)
12986 {
12987 /* If we have reached into the characters in the START row,
12988 that means the line boundaries have changed. So we
12989 can't start copying with the row START. Maybe it will
12990 work to start copying with the following row. */
12991 while (IT_CHARPOS (it) > CHARPOS (start))
12992 {
12993 /* Advance to the next row as the "start". */
12994 start_row++;
12995 start = start_row->start.pos;
12996 /* If there are no more rows to try, or just one, give up. */
12997 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12998 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12999 || CHARPOS (start) == ZV)
13000 {
13001 clear_glyph_matrix (w->desired_matrix);
13002 return 0;
13003 }
13004
13005 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13006 }
13007 /* If we have reached alignment,
13008 we can copy the rest of the rows. */
13009 if (IT_CHARPOS (it) == CHARPOS (start))
13010 break;
13011
13012 if (display_line (&it))
13013 last_text_row = it.glyph_row - 1;
13014 }
13015
13016 /* A value of current_y < last_visible_y means that we stopped
13017 at the previous window start, which in turn means that we
13018 have at least one reusable row. */
13019 if (it.current_y < it.last_visible_y)
13020 {
13021 /* IT.vpos always starts from 0; it counts text lines. */
13022 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13023
13024 /* Find PT if not already found in the lines displayed. */
13025 if (w->cursor.vpos < 0)
13026 {
13027 int dy = it.current_y - start_row->y;
13028
13029 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13030 row = row_containing_pos (w, PT, row, NULL, dy);
13031 if (row)
13032 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13033 dy, nrows_scrolled);
13034 else
13035 {
13036 clear_glyph_matrix (w->desired_matrix);
13037 return 0;
13038 }
13039 }
13040
13041 /* Scroll the display. Do it before the current matrix is
13042 changed. The problem here is that update has not yet
13043 run, i.e. part of the current matrix is not up to date.
13044 scroll_run_hook will clear the cursor, and use the
13045 current matrix to get the height of the row the cursor is
13046 in. */
13047 run.current_y = start_row->y;
13048 run.desired_y = it.current_y;
13049 run.height = it.last_visible_y - it.current_y;
13050
13051 if (run.height > 0 && run.current_y != run.desired_y)
13052 {
13053 update_begin (f);
13054 rif->update_window_begin_hook (w);
13055 rif->clear_window_mouse_face (w);
13056 rif->scroll_run_hook (w, &run);
13057 rif->update_window_end_hook (w, 0, 0);
13058 update_end (f);
13059 }
13060
13061 /* Shift current matrix down by nrows_scrolled lines. */
13062 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13063 rotate_matrix (w->current_matrix,
13064 start_vpos,
13065 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13066 nrows_scrolled);
13067
13068 /* Disable lines that must be updated. */
13069 for (i = 0; i < it.vpos; ++i)
13070 (start_row + i)->enabled_p = 0;
13071
13072 /* Re-compute Y positions. */
13073 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13074 max_y = it.last_visible_y;
13075 for (row = start_row + nrows_scrolled;
13076 row < bottom_row;
13077 ++row)
13078 {
13079 row->y = it.current_y;
13080 row->visible_height = row->height;
13081
13082 if (row->y < min_y)
13083 row->visible_height -= min_y - row->y;
13084 if (row->y + row->height > max_y)
13085 row->visible_height -= row->y + row->height - max_y;
13086 row->redraw_fringe_bitmaps_p = 1;
13087
13088 it.current_y += row->height;
13089
13090 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13091 last_reused_text_row = row;
13092 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13093 break;
13094 }
13095
13096 /* Disable lines in the current matrix which are now
13097 below the window. */
13098 for (++row; row < bottom_row; ++row)
13099 row->enabled_p = row->mode_line_p = 0;
13100 }
13101
13102 /* Update window_end_pos etc.; last_reused_text_row is the last
13103 reused row from the current matrix containing text, if any.
13104 The value of last_text_row is the last displayed line
13105 containing text. */
13106 if (last_reused_text_row)
13107 {
13108 w->window_end_bytepos
13109 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13110 w->window_end_pos
13111 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13112 w->window_end_vpos
13113 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13114 w->current_matrix));
13115 }
13116 else if (last_text_row)
13117 {
13118 w->window_end_bytepos
13119 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13120 w->window_end_pos
13121 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13122 w->window_end_vpos
13123 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13124 }
13125 else
13126 {
13127 /* This window must be completely empty. */
13128 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13129 w->window_end_pos = make_number (Z - ZV);
13130 w->window_end_vpos = make_number (0);
13131 }
13132 w->window_end_valid = Qnil;
13133
13134 /* Update hint: don't try scrolling again in update_window. */
13135 w->desired_matrix->no_scrolling_p = 1;
13136
13137 #if GLYPH_DEBUG
13138 debug_method_add (w, "try_window_reusing_current_matrix 1");
13139 #endif
13140 return 1;
13141 }
13142 else if (CHARPOS (new_start) > CHARPOS (start))
13143 {
13144 struct glyph_row *pt_row, *row;
13145 struct glyph_row *first_reusable_row;
13146 struct glyph_row *first_row_to_display;
13147 int dy;
13148 int yb = window_text_bottom_y (w);
13149
13150 /* Find the row starting at new_start, if there is one. Don't
13151 reuse a partially visible line at the end. */
13152 first_reusable_row = start_row;
13153 while (first_reusable_row->enabled_p
13154 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13155 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13156 < CHARPOS (new_start)))
13157 ++first_reusable_row;
13158
13159 /* Give up if there is no row to reuse. */
13160 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13161 || !first_reusable_row->enabled_p
13162 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13163 != CHARPOS (new_start)))
13164 return 0;
13165
13166 /* We can reuse fully visible rows beginning with
13167 first_reusable_row to the end of the window. Set
13168 first_row_to_display to the first row that cannot be reused.
13169 Set pt_row to the row containing point, if there is any. */
13170 pt_row = NULL;
13171 for (first_row_to_display = first_reusable_row;
13172 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13173 ++first_row_to_display)
13174 {
13175 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13176 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13177 pt_row = first_row_to_display;
13178 }
13179
13180 /* Start displaying at the start of first_row_to_display. */
13181 xassert (first_row_to_display->y < yb);
13182 init_to_row_start (&it, w, first_row_to_display);
13183
13184 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13185 - start_vpos);
13186 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13187 - nrows_scrolled);
13188 it.current_y = (first_row_to_display->y - first_reusable_row->y
13189 + WINDOW_HEADER_LINE_HEIGHT (w));
13190
13191 /* Display lines beginning with first_row_to_display in the
13192 desired matrix. Set last_text_row to the last row displayed
13193 that displays text. */
13194 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13195 if (pt_row == NULL)
13196 w->cursor.vpos = -1;
13197 last_text_row = NULL;
13198 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13199 if (display_line (&it))
13200 last_text_row = it.glyph_row - 1;
13201
13202 /* Give up If point isn't in a row displayed or reused. */
13203 if (w->cursor.vpos < 0)
13204 {
13205 clear_glyph_matrix (w->desired_matrix);
13206 return 0;
13207 }
13208
13209 /* If point is in a reused row, adjust y and vpos of the cursor
13210 position. */
13211 if (pt_row)
13212 {
13213 w->cursor.vpos -= nrows_scrolled;
13214 w->cursor.y -= first_reusable_row->y - start_row->y;
13215 }
13216
13217 /* Scroll the display. */
13218 run.current_y = first_reusable_row->y;
13219 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13220 run.height = it.last_visible_y - run.current_y;
13221 dy = run.current_y - run.desired_y;
13222
13223 if (run.height)
13224 {
13225 update_begin (f);
13226 rif->update_window_begin_hook (w);
13227 rif->clear_window_mouse_face (w);
13228 rif->scroll_run_hook (w, &run);
13229 rif->update_window_end_hook (w, 0, 0);
13230 update_end (f);
13231 }
13232
13233 /* Adjust Y positions of reused rows. */
13234 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13235 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13236 max_y = it.last_visible_y;
13237 for (row = first_reusable_row; row < first_row_to_display; ++row)
13238 {
13239 row->y -= dy;
13240 row->visible_height = row->height;
13241 if (row->y < min_y)
13242 row->visible_height -= min_y - row->y;
13243 if (row->y + row->height > max_y)
13244 row->visible_height -= row->y + row->height - max_y;
13245 row->redraw_fringe_bitmaps_p = 1;
13246 }
13247
13248 /* Scroll the current matrix. */
13249 xassert (nrows_scrolled > 0);
13250 rotate_matrix (w->current_matrix,
13251 start_vpos,
13252 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13253 -nrows_scrolled);
13254
13255 /* Disable rows not reused. */
13256 for (row -= nrows_scrolled; row < bottom_row; ++row)
13257 row->enabled_p = 0;
13258
13259 /* Point may have moved to a different line, so we cannot assume that
13260 the previous cursor position is valid; locate the correct row. */
13261 if (pt_row)
13262 {
13263 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13264 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13265 row++)
13266 {
13267 w->cursor.vpos++;
13268 w->cursor.y = row->y;
13269 }
13270 if (row < bottom_row)
13271 {
13272 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13273 while (glyph->charpos < PT)
13274 {
13275 w->cursor.hpos++;
13276 w->cursor.x += glyph->pixel_width;
13277 glyph++;
13278 }
13279 }
13280 }
13281
13282 /* Adjust window end. A null value of last_text_row means that
13283 the window end is in reused rows which in turn means that
13284 only its vpos can have changed. */
13285 if (last_text_row)
13286 {
13287 w->window_end_bytepos
13288 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13289 w->window_end_pos
13290 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13291 w->window_end_vpos
13292 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13293 }
13294 else
13295 {
13296 w->window_end_vpos
13297 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13298 }
13299
13300 w->window_end_valid = Qnil;
13301 w->desired_matrix->no_scrolling_p = 1;
13302
13303 #if GLYPH_DEBUG
13304 debug_method_add (w, "try_window_reusing_current_matrix 2");
13305 #endif
13306 return 1;
13307 }
13308
13309 return 0;
13310 }
13311
13312
13313 \f
13314 /************************************************************************
13315 Window redisplay reusing current matrix when buffer has changed
13316 ************************************************************************/
13317
13318 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13319 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13320 int *, int *));
13321 static struct glyph_row *
13322 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13323 struct glyph_row *));
13324
13325
13326 /* Return the last row in MATRIX displaying text. If row START is
13327 non-null, start searching with that row. IT gives the dimensions
13328 of the display. Value is null if matrix is empty; otherwise it is
13329 a pointer to the row found. */
13330
13331 static struct glyph_row *
13332 find_last_row_displaying_text (matrix, it, start)
13333 struct glyph_matrix *matrix;
13334 struct it *it;
13335 struct glyph_row *start;
13336 {
13337 struct glyph_row *row, *row_found;
13338
13339 /* Set row_found to the last row in IT->w's current matrix
13340 displaying text. The loop looks funny but think of partially
13341 visible lines. */
13342 row_found = NULL;
13343 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13344 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13345 {
13346 xassert (row->enabled_p);
13347 row_found = row;
13348 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13349 break;
13350 ++row;
13351 }
13352
13353 return row_found;
13354 }
13355
13356
13357 /* Return the last row in the current matrix of W that is not affected
13358 by changes at the start of current_buffer that occurred since W's
13359 current matrix was built. Value is null if no such row exists.
13360
13361 BEG_UNCHANGED us the number of characters unchanged at the start of
13362 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13363 first changed character in current_buffer. Characters at positions <
13364 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13365 when the current matrix was built. */
13366
13367 static struct glyph_row *
13368 find_last_unchanged_at_beg_row (w)
13369 struct window *w;
13370 {
13371 int first_changed_pos = BEG + BEG_UNCHANGED;
13372 struct glyph_row *row;
13373 struct glyph_row *row_found = NULL;
13374 int yb = window_text_bottom_y (w);
13375
13376 /* Find the last row displaying unchanged text. */
13377 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13378 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13379 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13380 {
13381 if (/* If row ends before first_changed_pos, it is unchanged,
13382 except in some case. */
13383 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13384 /* When row ends in ZV and we write at ZV it is not
13385 unchanged. */
13386 && !row->ends_at_zv_p
13387 /* When first_changed_pos is the end of a continued line,
13388 row is not unchanged because it may be no longer
13389 continued. */
13390 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13391 && (row->continued_p
13392 || row->exact_window_width_line_p)))
13393 row_found = row;
13394
13395 /* Stop if last visible row. */
13396 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13397 break;
13398
13399 ++row;
13400 }
13401
13402 return row_found;
13403 }
13404
13405
13406 /* Find the first glyph row in the current matrix of W that is not
13407 affected by changes at the end of current_buffer since the
13408 time W's current matrix was built.
13409
13410 Return in *DELTA the number of chars by which buffer positions in
13411 unchanged text at the end of current_buffer must be adjusted.
13412
13413 Return in *DELTA_BYTES the corresponding number of bytes.
13414
13415 Value is null if no such row exists, i.e. all rows are affected by
13416 changes. */
13417
13418 static struct glyph_row *
13419 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13420 struct window *w;
13421 int *delta, *delta_bytes;
13422 {
13423 struct glyph_row *row;
13424 struct glyph_row *row_found = NULL;
13425
13426 *delta = *delta_bytes = 0;
13427
13428 /* Display must not have been paused, otherwise the current matrix
13429 is not up to date. */
13430 if (NILP (w->window_end_valid))
13431 abort ();
13432
13433 /* A value of window_end_pos >= END_UNCHANGED means that the window
13434 end is in the range of changed text. If so, there is no
13435 unchanged row at the end of W's current matrix. */
13436 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13437 return NULL;
13438
13439 /* Set row to the last row in W's current matrix displaying text. */
13440 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13441
13442 /* If matrix is entirely empty, no unchanged row exists. */
13443 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13444 {
13445 /* The value of row is the last glyph row in the matrix having a
13446 meaningful buffer position in it. The end position of row
13447 corresponds to window_end_pos. This allows us to translate
13448 buffer positions in the current matrix to current buffer
13449 positions for characters not in changed text. */
13450 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13451 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13452 int last_unchanged_pos, last_unchanged_pos_old;
13453 struct glyph_row *first_text_row
13454 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13455
13456 *delta = Z - Z_old;
13457 *delta_bytes = Z_BYTE - Z_BYTE_old;
13458
13459 /* Set last_unchanged_pos to the buffer position of the last
13460 character in the buffer that has not been changed. Z is the
13461 index + 1 of the last character in current_buffer, i.e. by
13462 subtracting END_UNCHANGED we get the index of the last
13463 unchanged character, and we have to add BEG to get its buffer
13464 position. */
13465 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13466 last_unchanged_pos_old = last_unchanged_pos - *delta;
13467
13468 /* Search backward from ROW for a row displaying a line that
13469 starts at a minimum position >= last_unchanged_pos_old. */
13470 for (; row > first_text_row; --row)
13471 {
13472 /* This used to abort, but it can happen.
13473 It is ok to just stop the search instead here. KFS. */
13474 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13475 break;
13476
13477 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13478 row_found = row;
13479 }
13480 }
13481
13482 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13483 abort ();
13484
13485 return row_found;
13486 }
13487
13488
13489 /* Make sure that glyph rows in the current matrix of window W
13490 reference the same glyph memory as corresponding rows in the
13491 frame's frame matrix. This function is called after scrolling W's
13492 current matrix on a terminal frame in try_window_id and
13493 try_window_reusing_current_matrix. */
13494
13495 static void
13496 sync_frame_with_window_matrix_rows (w)
13497 struct window *w;
13498 {
13499 struct frame *f = XFRAME (w->frame);
13500 struct glyph_row *window_row, *window_row_end, *frame_row;
13501
13502 /* Preconditions: W must be a leaf window and full-width. Its frame
13503 must have a frame matrix. */
13504 xassert (NILP (w->hchild) && NILP (w->vchild));
13505 xassert (WINDOW_FULL_WIDTH_P (w));
13506 xassert (!FRAME_WINDOW_P (f));
13507
13508 /* If W is a full-width window, glyph pointers in W's current matrix
13509 have, by definition, to be the same as glyph pointers in the
13510 corresponding frame matrix. Note that frame matrices have no
13511 marginal areas (see build_frame_matrix). */
13512 window_row = w->current_matrix->rows;
13513 window_row_end = window_row + w->current_matrix->nrows;
13514 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13515 while (window_row < window_row_end)
13516 {
13517 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13518 struct glyph *end = window_row->glyphs[LAST_AREA];
13519
13520 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13521 frame_row->glyphs[TEXT_AREA] = start;
13522 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13523 frame_row->glyphs[LAST_AREA] = end;
13524
13525 /* Disable frame rows whose corresponding window rows have
13526 been disabled in try_window_id. */
13527 if (!window_row->enabled_p)
13528 frame_row->enabled_p = 0;
13529
13530 ++window_row, ++frame_row;
13531 }
13532 }
13533
13534
13535 /* Find the glyph row in window W containing CHARPOS. Consider all
13536 rows between START and END (not inclusive). END null means search
13537 all rows to the end of the display area of W. Value is the row
13538 containing CHARPOS or null. */
13539
13540 struct glyph_row *
13541 row_containing_pos (w, charpos, start, end, dy)
13542 struct window *w;
13543 int charpos;
13544 struct glyph_row *start, *end;
13545 int dy;
13546 {
13547 struct glyph_row *row = start;
13548 int last_y;
13549
13550 /* If we happen to start on a header-line, skip that. */
13551 if (row->mode_line_p)
13552 ++row;
13553
13554 if ((end && row >= end) || !row->enabled_p)
13555 return NULL;
13556
13557 last_y = window_text_bottom_y (w) - dy;
13558
13559 while (1)
13560 {
13561 /* Give up if we have gone too far. */
13562 if (end && row >= end)
13563 return NULL;
13564 /* This formerly returned if they were equal.
13565 I think that both quantities are of a "last plus one" type;
13566 if so, when they are equal, the row is within the screen. -- rms. */
13567 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13568 return NULL;
13569
13570 /* If it is in this row, return this row. */
13571 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13572 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13573 /* The end position of a row equals the start
13574 position of the next row. If CHARPOS is there, we
13575 would rather display it in the next line, except
13576 when this line ends in ZV. */
13577 && !row->ends_at_zv_p
13578 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13579 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13580 return row;
13581 ++row;
13582 }
13583 }
13584
13585
13586 /* Try to redisplay window W by reusing its existing display. W's
13587 current matrix must be up to date when this function is called,
13588 i.e. window_end_valid must not be nil.
13589
13590 Value is
13591
13592 1 if display has been updated
13593 0 if otherwise unsuccessful
13594 -1 if redisplay with same window start is known not to succeed
13595
13596 The following steps are performed:
13597
13598 1. Find the last row in the current matrix of W that is not
13599 affected by changes at the start of current_buffer. If no such row
13600 is found, give up.
13601
13602 2. Find the first row in W's current matrix that is not affected by
13603 changes at the end of current_buffer. Maybe there is no such row.
13604
13605 3. Display lines beginning with the row + 1 found in step 1 to the
13606 row found in step 2 or, if step 2 didn't find a row, to the end of
13607 the window.
13608
13609 4. If cursor is not known to appear on the window, give up.
13610
13611 5. If display stopped at the row found in step 2, scroll the
13612 display and current matrix as needed.
13613
13614 6. Maybe display some lines at the end of W, if we must. This can
13615 happen under various circumstances, like a partially visible line
13616 becoming fully visible, or because newly displayed lines are displayed
13617 in smaller font sizes.
13618
13619 7. Update W's window end information. */
13620
13621 static int
13622 try_window_id (w)
13623 struct window *w;
13624 {
13625 struct frame *f = XFRAME (w->frame);
13626 struct glyph_matrix *current_matrix = w->current_matrix;
13627 struct glyph_matrix *desired_matrix = w->desired_matrix;
13628 struct glyph_row *last_unchanged_at_beg_row;
13629 struct glyph_row *first_unchanged_at_end_row;
13630 struct glyph_row *row;
13631 struct glyph_row *bottom_row;
13632 int bottom_vpos;
13633 struct it it;
13634 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13635 struct text_pos start_pos;
13636 struct run run;
13637 int first_unchanged_at_end_vpos = 0;
13638 struct glyph_row *last_text_row, *last_text_row_at_end;
13639 struct text_pos start;
13640 int first_changed_charpos, last_changed_charpos;
13641
13642 #if GLYPH_DEBUG
13643 if (inhibit_try_window_id)
13644 return 0;
13645 #endif
13646
13647 /* This is handy for debugging. */
13648 #if 0
13649 #define GIVE_UP(X) \
13650 do { \
13651 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13652 return 0; \
13653 } while (0)
13654 #else
13655 #define GIVE_UP(X) return 0
13656 #endif
13657
13658 SET_TEXT_POS_FROM_MARKER (start, w->start);
13659
13660 /* Don't use this for mini-windows because these can show
13661 messages and mini-buffers, and we don't handle that here. */
13662 if (MINI_WINDOW_P (w))
13663 GIVE_UP (1);
13664
13665 /* This flag is used to prevent redisplay optimizations. */
13666 if (windows_or_buffers_changed || cursor_type_changed)
13667 GIVE_UP (2);
13668
13669 /* Verify that narrowing has not changed.
13670 Also verify that we were not told to prevent redisplay optimizations.
13671 It would be nice to further
13672 reduce the number of cases where this prevents try_window_id. */
13673 if (current_buffer->clip_changed
13674 || current_buffer->prevent_redisplay_optimizations_p)
13675 GIVE_UP (3);
13676
13677 /* Window must either use window-based redisplay or be full width. */
13678 if (!FRAME_WINDOW_P (f)
13679 && (!line_ins_del_ok
13680 || !WINDOW_FULL_WIDTH_P (w)))
13681 GIVE_UP (4);
13682
13683 /* Give up if point is not known NOT to appear in W. */
13684 if (PT < CHARPOS (start))
13685 GIVE_UP (5);
13686
13687 /* Another way to prevent redisplay optimizations. */
13688 if (XFASTINT (w->last_modified) == 0)
13689 GIVE_UP (6);
13690
13691 /* Verify that window is not hscrolled. */
13692 if (XFASTINT (w->hscroll) != 0)
13693 GIVE_UP (7);
13694
13695 /* Verify that display wasn't paused. */
13696 if (NILP (w->window_end_valid))
13697 GIVE_UP (8);
13698
13699 /* Can't use this if highlighting a region because a cursor movement
13700 will do more than just set the cursor. */
13701 if (!NILP (Vtransient_mark_mode)
13702 && !NILP (current_buffer->mark_active))
13703 GIVE_UP (9);
13704
13705 /* Likewise if highlighting trailing whitespace. */
13706 if (!NILP (Vshow_trailing_whitespace))
13707 GIVE_UP (11);
13708
13709 /* Likewise if showing a region. */
13710 if (!NILP (w->region_showing))
13711 GIVE_UP (10);
13712
13713 /* Can use this if overlay arrow position and or string have changed. */
13714 if (overlay_arrows_changed_p ())
13715 GIVE_UP (12);
13716
13717
13718 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13719 only if buffer has really changed. The reason is that the gap is
13720 initially at Z for freshly visited files. The code below would
13721 set end_unchanged to 0 in that case. */
13722 if (MODIFF > SAVE_MODIFF
13723 /* This seems to happen sometimes after saving a buffer. */
13724 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13725 {
13726 if (GPT - BEG < BEG_UNCHANGED)
13727 BEG_UNCHANGED = GPT - BEG;
13728 if (Z - GPT < END_UNCHANGED)
13729 END_UNCHANGED = Z - GPT;
13730 }
13731
13732 /* The position of the first and last character that has been changed. */
13733 first_changed_charpos = BEG + BEG_UNCHANGED;
13734 last_changed_charpos = Z - END_UNCHANGED;
13735
13736 /* If window starts after a line end, and the last change is in
13737 front of that newline, then changes don't affect the display.
13738 This case happens with stealth-fontification. Note that although
13739 the display is unchanged, glyph positions in the matrix have to
13740 be adjusted, of course. */
13741 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13742 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13743 && ((last_changed_charpos < CHARPOS (start)
13744 && CHARPOS (start) == BEGV)
13745 || (last_changed_charpos < CHARPOS (start) - 1
13746 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13747 {
13748 int Z_old, delta, Z_BYTE_old, delta_bytes;
13749 struct glyph_row *r0;
13750
13751 /* Compute how many chars/bytes have been added to or removed
13752 from the buffer. */
13753 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13754 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13755 delta = Z - Z_old;
13756 delta_bytes = Z_BYTE - Z_BYTE_old;
13757
13758 /* Give up if PT is not in the window. Note that it already has
13759 been checked at the start of try_window_id that PT is not in
13760 front of the window start. */
13761 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13762 GIVE_UP (13);
13763
13764 /* If window start is unchanged, we can reuse the whole matrix
13765 as is, after adjusting glyph positions. No need to compute
13766 the window end again, since its offset from Z hasn't changed. */
13767 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13768 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13769 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13770 /* PT must not be in a partially visible line. */
13771 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13772 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13773 {
13774 /* Adjust positions in the glyph matrix. */
13775 if (delta || delta_bytes)
13776 {
13777 struct glyph_row *r1
13778 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13779 increment_matrix_positions (w->current_matrix,
13780 MATRIX_ROW_VPOS (r0, current_matrix),
13781 MATRIX_ROW_VPOS (r1, current_matrix),
13782 delta, delta_bytes);
13783 }
13784
13785 /* Set the cursor. */
13786 row = row_containing_pos (w, PT, r0, NULL, 0);
13787 if (row)
13788 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13789 else
13790 abort ();
13791 return 1;
13792 }
13793 }
13794
13795 /* Handle the case that changes are all below what is displayed in
13796 the window, and that PT is in the window. This shortcut cannot
13797 be taken if ZV is visible in the window, and text has been added
13798 there that is visible in the window. */
13799 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13800 /* ZV is not visible in the window, or there are no
13801 changes at ZV, actually. */
13802 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13803 || first_changed_charpos == last_changed_charpos))
13804 {
13805 struct glyph_row *r0;
13806
13807 /* Give up if PT is not in the window. Note that it already has
13808 been checked at the start of try_window_id that PT is not in
13809 front of the window start. */
13810 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13811 GIVE_UP (14);
13812
13813 /* If window start is unchanged, we can reuse the whole matrix
13814 as is, without changing glyph positions since no text has
13815 been added/removed in front of the window end. */
13816 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13817 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13818 /* PT must not be in a partially visible line. */
13819 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13820 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13821 {
13822 /* We have to compute the window end anew since text
13823 can have been added/removed after it. */
13824 w->window_end_pos
13825 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13826 w->window_end_bytepos
13827 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13828
13829 /* Set the cursor. */
13830 row = row_containing_pos (w, PT, r0, NULL, 0);
13831 if (row)
13832 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13833 else
13834 abort ();
13835 return 2;
13836 }
13837 }
13838
13839 /* Give up if window start is in the changed area.
13840
13841 The condition used to read
13842
13843 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13844
13845 but why that was tested escapes me at the moment. */
13846 if (CHARPOS (start) >= first_changed_charpos
13847 && CHARPOS (start) <= last_changed_charpos)
13848 GIVE_UP (15);
13849
13850 /* Check that window start agrees with the start of the first glyph
13851 row in its current matrix. Check this after we know the window
13852 start is not in changed text, otherwise positions would not be
13853 comparable. */
13854 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13855 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13856 GIVE_UP (16);
13857
13858 /* Give up if the window ends in strings. Overlay strings
13859 at the end are difficult to handle, so don't try. */
13860 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13861 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13862 GIVE_UP (20);
13863
13864 /* Compute the position at which we have to start displaying new
13865 lines. Some of the lines at the top of the window might be
13866 reusable because they are not displaying changed text. Find the
13867 last row in W's current matrix not affected by changes at the
13868 start of current_buffer. Value is null if changes start in the
13869 first line of window. */
13870 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13871 if (last_unchanged_at_beg_row)
13872 {
13873 /* Avoid starting to display in the moddle of a character, a TAB
13874 for instance. This is easier than to set up the iterator
13875 exactly, and it's not a frequent case, so the additional
13876 effort wouldn't really pay off. */
13877 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13878 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13879 && last_unchanged_at_beg_row > w->current_matrix->rows)
13880 --last_unchanged_at_beg_row;
13881
13882 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13883 GIVE_UP (17);
13884
13885 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13886 GIVE_UP (18);
13887 start_pos = it.current.pos;
13888
13889 /* Start displaying new lines in the desired matrix at the same
13890 vpos we would use in the current matrix, i.e. below
13891 last_unchanged_at_beg_row. */
13892 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13893 current_matrix);
13894 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13895 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13896
13897 xassert (it.hpos == 0 && it.current_x == 0);
13898 }
13899 else
13900 {
13901 /* There are no reusable lines at the start of the window.
13902 Start displaying in the first text line. */
13903 start_display (&it, w, start);
13904 it.vpos = it.first_vpos;
13905 start_pos = it.current.pos;
13906 }
13907
13908 /* Find the first row that is not affected by changes at the end of
13909 the buffer. Value will be null if there is no unchanged row, in
13910 which case we must redisplay to the end of the window. delta
13911 will be set to the value by which buffer positions beginning with
13912 first_unchanged_at_end_row have to be adjusted due to text
13913 changes. */
13914 first_unchanged_at_end_row
13915 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13916 IF_DEBUG (debug_delta = delta);
13917 IF_DEBUG (debug_delta_bytes = delta_bytes);
13918
13919 /* Set stop_pos to the buffer position up to which we will have to
13920 display new lines. If first_unchanged_at_end_row != NULL, this
13921 is the buffer position of the start of the line displayed in that
13922 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13923 that we don't stop at a buffer position. */
13924 stop_pos = 0;
13925 if (first_unchanged_at_end_row)
13926 {
13927 xassert (last_unchanged_at_beg_row == NULL
13928 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13929
13930 /* If this is a continuation line, move forward to the next one
13931 that isn't. Changes in lines above affect this line.
13932 Caution: this may move first_unchanged_at_end_row to a row
13933 not displaying text. */
13934 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13935 && 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;
13939
13940 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13941 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13942 >= it.last_visible_y))
13943 first_unchanged_at_end_row = NULL;
13944 else
13945 {
13946 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13947 + delta);
13948 first_unchanged_at_end_vpos
13949 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13950 xassert (stop_pos >= Z - END_UNCHANGED);
13951 }
13952 }
13953 else if (last_unchanged_at_beg_row == NULL)
13954 GIVE_UP (19);
13955
13956
13957 #if GLYPH_DEBUG
13958
13959 /* Either there is no unchanged row at the end, or the one we have
13960 now displays text. This is a necessary condition for the window
13961 end pos calculation at the end of this function. */
13962 xassert (first_unchanged_at_end_row == NULL
13963 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13964
13965 debug_last_unchanged_at_beg_vpos
13966 = (last_unchanged_at_beg_row
13967 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13968 : -1);
13969 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13970
13971 #endif /* GLYPH_DEBUG != 0 */
13972
13973
13974 /* Display new lines. Set last_text_row to the last new line
13975 displayed which has text on it, i.e. might end up as being the
13976 line where the window_end_vpos is. */
13977 w->cursor.vpos = -1;
13978 last_text_row = NULL;
13979 overlay_arrow_seen = 0;
13980 while (it.current_y < it.last_visible_y
13981 && !fonts_changed_p
13982 && (first_unchanged_at_end_row == NULL
13983 || IT_CHARPOS (it) < stop_pos))
13984 {
13985 if (display_line (&it))
13986 last_text_row = it.glyph_row - 1;
13987 }
13988
13989 if (fonts_changed_p)
13990 return -1;
13991
13992
13993 /* Compute differences in buffer positions, y-positions etc. for
13994 lines reused at the bottom of the window. Compute what we can
13995 scroll. */
13996 if (first_unchanged_at_end_row
13997 /* No lines reused because we displayed everything up to the
13998 bottom of the window. */
13999 && it.current_y < it.last_visible_y)
14000 {
14001 dvpos = (it.vpos
14002 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14003 current_matrix));
14004 dy = it.current_y - first_unchanged_at_end_row->y;
14005 run.current_y = first_unchanged_at_end_row->y;
14006 run.desired_y = run.current_y + dy;
14007 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14008 }
14009 else
14010 {
14011 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14012 first_unchanged_at_end_row = NULL;
14013 }
14014 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14015
14016
14017 /* Find the cursor if not already found. We have to decide whether
14018 PT will appear on this window (it sometimes doesn't, but this is
14019 not a very frequent case.) This decision has to be made before
14020 the current matrix is altered. A value of cursor.vpos < 0 means
14021 that PT is either in one of the lines beginning at
14022 first_unchanged_at_end_row or below the window. Don't care for
14023 lines that might be displayed later at the window end; as
14024 mentioned, this is not a frequent case. */
14025 if (w->cursor.vpos < 0)
14026 {
14027 /* Cursor in unchanged rows at the top? */
14028 if (PT < CHARPOS (start_pos)
14029 && last_unchanged_at_beg_row)
14030 {
14031 row = row_containing_pos (w, PT,
14032 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14033 last_unchanged_at_beg_row + 1, 0);
14034 if (row)
14035 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14036 }
14037
14038 /* Start from first_unchanged_at_end_row looking for PT. */
14039 else if (first_unchanged_at_end_row)
14040 {
14041 row = row_containing_pos (w, PT - delta,
14042 first_unchanged_at_end_row, NULL, 0);
14043 if (row)
14044 set_cursor_from_row (w, row, w->current_matrix, delta,
14045 delta_bytes, dy, dvpos);
14046 }
14047
14048 /* Give up if cursor was not found. */
14049 if (w->cursor.vpos < 0)
14050 {
14051 clear_glyph_matrix (w->desired_matrix);
14052 return -1;
14053 }
14054 }
14055
14056 /* Don't let the cursor end in the scroll margins. */
14057 {
14058 int this_scroll_margin, cursor_height;
14059
14060 this_scroll_margin = max (0, scroll_margin);
14061 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14062 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14063 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14064
14065 if ((w->cursor.y < this_scroll_margin
14066 && CHARPOS (start) > BEGV)
14067 /* Old redisplay didn't take scroll margin into account at the bottom,
14068 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14069 || (w->cursor.y + (make_cursor_line_fully_visible_p
14070 ? cursor_height + this_scroll_margin
14071 : 1)) > it.last_visible_y)
14072 {
14073 w->cursor.vpos = -1;
14074 clear_glyph_matrix (w->desired_matrix);
14075 return -1;
14076 }
14077 }
14078
14079 /* Scroll the display. Do it before changing the current matrix so
14080 that xterm.c doesn't get confused about where the cursor glyph is
14081 found. */
14082 if (dy && run.height)
14083 {
14084 update_begin (f);
14085
14086 if (FRAME_WINDOW_P (f))
14087 {
14088 rif->update_window_begin_hook (w);
14089 rif->clear_window_mouse_face (w);
14090 rif->scroll_run_hook (w, &run);
14091 rif->update_window_end_hook (w, 0, 0);
14092 }
14093 else
14094 {
14095 /* Terminal frame. In this case, dvpos gives the number of
14096 lines to scroll by; dvpos < 0 means scroll up. */
14097 int first_unchanged_at_end_vpos
14098 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14099 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14100 int end = (WINDOW_TOP_EDGE_LINE (w)
14101 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14102 + window_internal_height (w));
14103
14104 /* Perform the operation on the screen. */
14105 if (dvpos > 0)
14106 {
14107 /* Scroll last_unchanged_at_beg_row to the end of the
14108 window down dvpos lines. */
14109 set_terminal_window (end);
14110
14111 /* On dumb terminals delete dvpos lines at the end
14112 before inserting dvpos empty lines. */
14113 if (!scroll_region_ok)
14114 ins_del_lines (end - dvpos, -dvpos);
14115
14116 /* Insert dvpos empty lines in front of
14117 last_unchanged_at_beg_row. */
14118 ins_del_lines (from, dvpos);
14119 }
14120 else if (dvpos < 0)
14121 {
14122 /* Scroll up last_unchanged_at_beg_vpos to the end of
14123 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14124 set_terminal_window (end);
14125
14126 /* Delete dvpos lines in front of
14127 last_unchanged_at_beg_vpos. ins_del_lines will set
14128 the cursor to the given vpos and emit |dvpos| delete
14129 line sequences. */
14130 ins_del_lines (from + dvpos, dvpos);
14131
14132 /* On a dumb terminal insert dvpos empty lines at the
14133 end. */
14134 if (!scroll_region_ok)
14135 ins_del_lines (end + dvpos, -dvpos);
14136 }
14137
14138 set_terminal_window (0);
14139 }
14140
14141 update_end (f);
14142 }
14143
14144 /* Shift reused rows of the current matrix to the right position.
14145 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14146 text. */
14147 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14148 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14149 if (dvpos < 0)
14150 {
14151 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14152 bottom_vpos, dvpos);
14153 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14154 bottom_vpos, 0);
14155 }
14156 else if (dvpos > 0)
14157 {
14158 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14159 bottom_vpos, dvpos);
14160 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14161 first_unchanged_at_end_vpos + dvpos, 0);
14162 }
14163
14164 /* For frame-based redisplay, make sure that current frame and window
14165 matrix are in sync with respect to glyph memory. */
14166 if (!FRAME_WINDOW_P (f))
14167 sync_frame_with_window_matrix_rows (w);
14168
14169 /* Adjust buffer positions in reused rows. */
14170 if (delta)
14171 increment_matrix_positions (current_matrix,
14172 first_unchanged_at_end_vpos + dvpos,
14173 bottom_vpos, delta, delta_bytes);
14174
14175 /* Adjust Y positions. */
14176 if (dy)
14177 shift_glyph_matrix (w, current_matrix,
14178 first_unchanged_at_end_vpos + dvpos,
14179 bottom_vpos, dy);
14180
14181 if (first_unchanged_at_end_row)
14182 {
14183 first_unchanged_at_end_row += dvpos;
14184 if (first_unchanged_at_end_row->y >= it.last_visible_y
14185 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14186 first_unchanged_at_end_row = NULL;
14187 }
14188
14189 /* If scrolling up, there may be some lines to display at the end of
14190 the window. */
14191 last_text_row_at_end = NULL;
14192 if (dy < 0)
14193 {
14194 /* Scrolling up can leave for example a partially visible line
14195 at the end of the window to be redisplayed. */
14196 /* Set last_row to the glyph row in the current matrix where the
14197 window end line is found. It has been moved up or down in
14198 the matrix by dvpos. */
14199 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14200 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14201
14202 /* If last_row is the window end line, it should display text. */
14203 xassert (last_row->displays_text_p);
14204
14205 /* If window end line was partially visible before, begin
14206 displaying at that line. Otherwise begin displaying with the
14207 line following it. */
14208 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14209 {
14210 init_to_row_start (&it, w, last_row);
14211 it.vpos = last_vpos;
14212 it.current_y = last_row->y;
14213 }
14214 else
14215 {
14216 init_to_row_end (&it, w, last_row);
14217 it.vpos = 1 + last_vpos;
14218 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14219 ++last_row;
14220 }
14221
14222 /* We may start in a continuation line. If so, we have to
14223 get the right continuation_lines_width and current_x. */
14224 it.continuation_lines_width = last_row->continuation_lines_width;
14225 it.hpos = it.current_x = 0;
14226
14227 /* Display the rest of the lines at the window end. */
14228 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14229 while (it.current_y < it.last_visible_y
14230 && !fonts_changed_p)
14231 {
14232 /* Is it always sure that the display agrees with lines in
14233 the current matrix? I don't think so, so we mark rows
14234 displayed invalid in the current matrix by setting their
14235 enabled_p flag to zero. */
14236 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14237 if (display_line (&it))
14238 last_text_row_at_end = it.glyph_row - 1;
14239 }
14240 }
14241
14242 /* Update window_end_pos and window_end_vpos. */
14243 if (first_unchanged_at_end_row
14244 && !last_text_row_at_end)
14245 {
14246 /* Window end line if one of the preserved rows from the current
14247 matrix. Set row to the last row displaying text in current
14248 matrix starting at first_unchanged_at_end_row, after
14249 scrolling. */
14250 xassert (first_unchanged_at_end_row->displays_text_p);
14251 row = find_last_row_displaying_text (w->current_matrix, &it,
14252 first_unchanged_at_end_row);
14253 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14254
14255 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14256 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14257 w->window_end_vpos
14258 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14259 xassert (w->window_end_bytepos >= 0);
14260 IF_DEBUG (debug_method_add (w, "A"));
14261 }
14262 else if (last_text_row_at_end)
14263 {
14264 w->window_end_pos
14265 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14266 w->window_end_bytepos
14267 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14268 w->window_end_vpos
14269 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14270 xassert (w->window_end_bytepos >= 0);
14271 IF_DEBUG (debug_method_add (w, "B"));
14272 }
14273 else if (last_text_row)
14274 {
14275 /* We have displayed either to the end of the window or at the
14276 end of the window, i.e. the last row with text is to be found
14277 in the desired matrix. */
14278 w->window_end_pos
14279 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14280 w->window_end_bytepos
14281 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14282 w->window_end_vpos
14283 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14284 xassert (w->window_end_bytepos >= 0);
14285 }
14286 else if (first_unchanged_at_end_row == NULL
14287 && last_text_row == NULL
14288 && last_text_row_at_end == NULL)
14289 {
14290 /* Displayed to end of window, but no line containing text was
14291 displayed. Lines were deleted at the end of the window. */
14292 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14293 int vpos = XFASTINT (w->window_end_vpos);
14294 struct glyph_row *current_row = current_matrix->rows + vpos;
14295 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14296
14297 for (row = NULL;
14298 row == NULL && vpos >= first_vpos;
14299 --vpos, --current_row, --desired_row)
14300 {
14301 if (desired_row->enabled_p)
14302 {
14303 if (desired_row->displays_text_p)
14304 row = desired_row;
14305 }
14306 else if (current_row->displays_text_p)
14307 row = current_row;
14308 }
14309
14310 xassert (row != NULL);
14311 w->window_end_vpos = make_number (vpos + 1);
14312 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14313 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14314 xassert (w->window_end_bytepos >= 0);
14315 IF_DEBUG (debug_method_add (w, "C"));
14316 }
14317 else
14318 abort ();
14319
14320 #if 0 /* This leads to problems, for instance when the cursor is
14321 at ZV, and the cursor line displays no text. */
14322 /* Disable rows below what's displayed in the window. This makes
14323 debugging easier. */
14324 enable_glyph_matrix_rows (current_matrix,
14325 XFASTINT (w->window_end_vpos) + 1,
14326 bottom_vpos, 0);
14327 #endif
14328
14329 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14330 debug_end_vpos = XFASTINT (w->window_end_vpos));
14331
14332 /* Record that display has not been completed. */
14333 w->window_end_valid = Qnil;
14334 w->desired_matrix->no_scrolling_p = 1;
14335 return 3;
14336
14337 #undef GIVE_UP
14338 }
14339
14340
14341 \f
14342 /***********************************************************************
14343 More debugging support
14344 ***********************************************************************/
14345
14346 #if GLYPH_DEBUG
14347
14348 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14349 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14350 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14351
14352
14353 /* Dump the contents of glyph matrix MATRIX on stderr.
14354
14355 GLYPHS 0 means don't show glyph contents.
14356 GLYPHS 1 means show glyphs in short form
14357 GLYPHS > 1 means show glyphs in long form. */
14358
14359 void
14360 dump_glyph_matrix (matrix, glyphs)
14361 struct glyph_matrix *matrix;
14362 int glyphs;
14363 {
14364 int i;
14365 for (i = 0; i < matrix->nrows; ++i)
14366 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14367 }
14368
14369
14370 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14371 the glyph row and area where the glyph comes from. */
14372
14373 void
14374 dump_glyph (row, glyph, area)
14375 struct glyph_row *row;
14376 struct glyph *glyph;
14377 int area;
14378 {
14379 if (glyph->type == CHAR_GLYPH)
14380 {
14381 fprintf (stderr,
14382 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14383 glyph - row->glyphs[TEXT_AREA],
14384 'C',
14385 glyph->charpos,
14386 (BUFFERP (glyph->object)
14387 ? 'B'
14388 : (STRINGP (glyph->object)
14389 ? 'S'
14390 : '-')),
14391 glyph->pixel_width,
14392 glyph->u.ch,
14393 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14394 ? glyph->u.ch
14395 : '.'),
14396 glyph->face_id,
14397 glyph->left_box_line_p,
14398 glyph->right_box_line_p);
14399 }
14400 else if (glyph->type == STRETCH_GLYPH)
14401 {
14402 fprintf (stderr,
14403 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14404 glyph - row->glyphs[TEXT_AREA],
14405 'S',
14406 glyph->charpos,
14407 (BUFFERP (glyph->object)
14408 ? 'B'
14409 : (STRINGP (glyph->object)
14410 ? 'S'
14411 : '-')),
14412 glyph->pixel_width,
14413 0,
14414 '.',
14415 glyph->face_id,
14416 glyph->left_box_line_p,
14417 glyph->right_box_line_p);
14418 }
14419 else if (glyph->type == IMAGE_GLYPH)
14420 {
14421 fprintf (stderr,
14422 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14423 glyph - row->glyphs[TEXT_AREA],
14424 'I',
14425 glyph->charpos,
14426 (BUFFERP (glyph->object)
14427 ? 'B'
14428 : (STRINGP (glyph->object)
14429 ? 'S'
14430 : '-')),
14431 glyph->pixel_width,
14432 glyph->u.img_id,
14433 '.',
14434 glyph->face_id,
14435 glyph->left_box_line_p,
14436 glyph->right_box_line_p);
14437 }
14438 }
14439
14440
14441 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14442 GLYPHS 0 means don't show glyph contents.
14443 GLYPHS 1 means show glyphs in short form
14444 GLYPHS > 1 means show glyphs in long form. */
14445
14446 void
14447 dump_glyph_row (row, vpos, glyphs)
14448 struct glyph_row *row;
14449 int vpos, glyphs;
14450 {
14451 if (glyphs != 1)
14452 {
14453 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14454 fprintf (stderr, "======================================================================\n");
14455
14456 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14457 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14458 vpos,
14459 MATRIX_ROW_START_CHARPOS (row),
14460 MATRIX_ROW_END_CHARPOS (row),
14461 row->used[TEXT_AREA],
14462 row->contains_overlapping_glyphs_p,
14463 row->enabled_p,
14464 row->truncated_on_left_p,
14465 row->truncated_on_right_p,
14466 row->continued_p,
14467 MATRIX_ROW_CONTINUATION_LINE_P (row),
14468 row->displays_text_p,
14469 row->ends_at_zv_p,
14470 row->fill_line_p,
14471 row->ends_in_middle_of_char_p,
14472 row->starts_in_middle_of_char_p,
14473 row->mouse_face_p,
14474 row->x,
14475 row->y,
14476 row->pixel_width,
14477 row->height,
14478 row->visible_height,
14479 row->ascent,
14480 row->phys_ascent);
14481 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14482 row->end.overlay_string_index,
14483 row->continuation_lines_width);
14484 fprintf (stderr, "%9d %5d\n",
14485 CHARPOS (row->start.string_pos),
14486 CHARPOS (row->end.string_pos));
14487 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14488 row->end.dpvec_index);
14489 }
14490
14491 if (glyphs > 1)
14492 {
14493 int area;
14494
14495 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14496 {
14497 struct glyph *glyph = row->glyphs[area];
14498 struct glyph *glyph_end = glyph + row->used[area];
14499
14500 /* Glyph for a line end in text. */
14501 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14502 ++glyph_end;
14503
14504 if (glyph < glyph_end)
14505 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14506
14507 for (; glyph < glyph_end; ++glyph)
14508 dump_glyph (row, glyph, area);
14509 }
14510 }
14511 else if (glyphs == 1)
14512 {
14513 int area;
14514
14515 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14516 {
14517 char *s = (char *) alloca (row->used[area] + 1);
14518 int i;
14519
14520 for (i = 0; i < row->used[area]; ++i)
14521 {
14522 struct glyph *glyph = row->glyphs[area] + i;
14523 if (glyph->type == CHAR_GLYPH
14524 && glyph->u.ch < 0x80
14525 && glyph->u.ch >= ' ')
14526 s[i] = glyph->u.ch;
14527 else
14528 s[i] = '.';
14529 }
14530
14531 s[i] = '\0';
14532 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14533 }
14534 }
14535 }
14536
14537
14538 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14539 Sdump_glyph_matrix, 0, 1, "p",
14540 doc: /* Dump the current matrix of the selected window to stderr.
14541 Shows contents of glyph row structures. With non-nil
14542 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14543 glyphs in short form, otherwise show glyphs in long form. */)
14544 (glyphs)
14545 Lisp_Object glyphs;
14546 {
14547 struct window *w = XWINDOW (selected_window);
14548 struct buffer *buffer = XBUFFER (w->buffer);
14549
14550 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14551 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14552 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14553 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14554 fprintf (stderr, "=============================================\n");
14555 dump_glyph_matrix (w->current_matrix,
14556 NILP (glyphs) ? 0 : XINT (glyphs));
14557 return Qnil;
14558 }
14559
14560
14561 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14562 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14563 ()
14564 {
14565 struct frame *f = XFRAME (selected_frame);
14566 dump_glyph_matrix (f->current_matrix, 1);
14567 return Qnil;
14568 }
14569
14570
14571 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14572 doc: /* Dump glyph row ROW to stderr.
14573 GLYPH 0 means don't dump glyphs.
14574 GLYPH 1 means dump glyphs in short form.
14575 GLYPH > 1 or omitted means dump glyphs in long form. */)
14576 (row, glyphs)
14577 Lisp_Object row, glyphs;
14578 {
14579 struct glyph_matrix *matrix;
14580 int vpos;
14581
14582 CHECK_NUMBER (row);
14583 matrix = XWINDOW (selected_window)->current_matrix;
14584 vpos = XINT (row);
14585 if (vpos >= 0 && vpos < matrix->nrows)
14586 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14587 vpos,
14588 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14589 return Qnil;
14590 }
14591
14592
14593 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14594 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14595 GLYPH 0 means don't dump glyphs.
14596 GLYPH 1 means dump glyphs in short form.
14597 GLYPH > 1 or omitted means dump glyphs in long form. */)
14598 (row, glyphs)
14599 Lisp_Object row, glyphs;
14600 {
14601 struct frame *sf = SELECTED_FRAME ();
14602 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14603 int vpos;
14604
14605 CHECK_NUMBER (row);
14606 vpos = XINT (row);
14607 if (vpos >= 0 && vpos < m->nrows)
14608 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14609 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14610 return Qnil;
14611 }
14612
14613
14614 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14615 doc: /* Toggle tracing of redisplay.
14616 With ARG, turn tracing on if and only if ARG is positive. */)
14617 (arg)
14618 Lisp_Object arg;
14619 {
14620 if (NILP (arg))
14621 trace_redisplay_p = !trace_redisplay_p;
14622 else
14623 {
14624 arg = Fprefix_numeric_value (arg);
14625 trace_redisplay_p = XINT (arg) > 0;
14626 }
14627
14628 return Qnil;
14629 }
14630
14631
14632 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14633 doc: /* Like `format', but print result to stderr.
14634 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14635 (nargs, args)
14636 int nargs;
14637 Lisp_Object *args;
14638 {
14639 Lisp_Object s = Fformat (nargs, args);
14640 fprintf (stderr, "%s", SDATA (s));
14641 return Qnil;
14642 }
14643
14644 #endif /* GLYPH_DEBUG */
14645
14646
14647 \f
14648 /***********************************************************************
14649 Building Desired Matrix Rows
14650 ***********************************************************************/
14651
14652 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14653 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14654
14655 static struct glyph_row *
14656 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14657 struct window *w;
14658 Lisp_Object overlay_arrow_string;
14659 {
14660 struct frame *f = XFRAME (WINDOW_FRAME (w));
14661 struct buffer *buffer = XBUFFER (w->buffer);
14662 struct buffer *old = current_buffer;
14663 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14664 int arrow_len = SCHARS (overlay_arrow_string);
14665 const unsigned char *arrow_end = arrow_string + arrow_len;
14666 const unsigned char *p;
14667 struct it it;
14668 int multibyte_p;
14669 int n_glyphs_before;
14670
14671 set_buffer_temp (buffer);
14672 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14673 it.glyph_row->used[TEXT_AREA] = 0;
14674 SET_TEXT_POS (it.position, 0, 0);
14675
14676 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14677 p = arrow_string;
14678 while (p < arrow_end)
14679 {
14680 Lisp_Object face, ilisp;
14681
14682 /* Get the next character. */
14683 if (multibyte_p)
14684 it.c = string_char_and_length (p, arrow_len, &it.len);
14685 else
14686 it.c = *p, it.len = 1;
14687 p += it.len;
14688
14689 /* Get its face. */
14690 ilisp = make_number (p - arrow_string);
14691 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14692 it.face_id = compute_char_face (f, it.c, face);
14693
14694 /* Compute its width, get its glyphs. */
14695 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14696 SET_TEXT_POS (it.position, -1, -1);
14697 PRODUCE_GLYPHS (&it);
14698
14699 /* If this character doesn't fit any more in the line, we have
14700 to remove some glyphs. */
14701 if (it.current_x > it.last_visible_x)
14702 {
14703 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14704 break;
14705 }
14706 }
14707
14708 set_buffer_temp (old);
14709 return it.glyph_row;
14710 }
14711
14712
14713 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14714 glyphs are only inserted for terminal frames since we can't really
14715 win with truncation glyphs when partially visible glyphs are
14716 involved. Which glyphs to insert is determined by
14717 produce_special_glyphs. */
14718
14719 static void
14720 insert_left_trunc_glyphs (it)
14721 struct it *it;
14722 {
14723 struct it truncate_it;
14724 struct glyph *from, *end, *to, *toend;
14725
14726 xassert (!FRAME_WINDOW_P (it->f));
14727
14728 /* Get the truncation glyphs. */
14729 truncate_it = *it;
14730 truncate_it.current_x = 0;
14731 truncate_it.face_id = DEFAULT_FACE_ID;
14732 truncate_it.glyph_row = &scratch_glyph_row;
14733 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14734 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14735 truncate_it.object = make_number (0);
14736 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14737
14738 /* Overwrite glyphs from IT with truncation glyphs. */
14739 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14740 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14741 to = it->glyph_row->glyphs[TEXT_AREA];
14742 toend = to + it->glyph_row->used[TEXT_AREA];
14743
14744 while (from < end)
14745 *to++ = *from++;
14746
14747 /* There may be padding glyphs left over. Overwrite them too. */
14748 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14749 {
14750 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14751 while (from < end)
14752 *to++ = *from++;
14753 }
14754
14755 if (to > toend)
14756 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14757 }
14758
14759
14760 /* Compute the pixel height and width of IT->glyph_row.
14761
14762 Most of the time, ascent and height of a display line will be equal
14763 to the max_ascent and max_height values of the display iterator
14764 structure. This is not the case if
14765
14766 1. We hit ZV without displaying anything. In this case, max_ascent
14767 and max_height will be zero.
14768
14769 2. We have some glyphs that don't contribute to the line height.
14770 (The glyph row flag contributes_to_line_height_p is for future
14771 pixmap extensions).
14772
14773 The first case is easily covered by using default values because in
14774 these cases, the line height does not really matter, except that it
14775 must not be zero. */
14776
14777 static void
14778 compute_line_metrics (it)
14779 struct it *it;
14780 {
14781 struct glyph_row *row = it->glyph_row;
14782 int area, i;
14783
14784 if (FRAME_WINDOW_P (it->f))
14785 {
14786 int i, min_y, max_y;
14787
14788 /* The line may consist of one space only, that was added to
14789 place the cursor on it. If so, the row's height hasn't been
14790 computed yet. */
14791 if (row->height == 0)
14792 {
14793 if (it->max_ascent + it->max_descent == 0)
14794 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14795 row->ascent = it->max_ascent;
14796 row->height = it->max_ascent + it->max_descent;
14797 row->phys_ascent = it->max_phys_ascent;
14798 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14799 row->extra_line_spacing = it->max_extra_line_spacing;
14800 }
14801
14802 /* Compute the width of this line. */
14803 row->pixel_width = row->x;
14804 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14805 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14806
14807 xassert (row->pixel_width >= 0);
14808 xassert (row->ascent >= 0 && row->height > 0);
14809
14810 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14811 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14812
14813 /* If first line's physical ascent is larger than its logical
14814 ascent, use the physical ascent, and make the row taller.
14815 This makes accented characters fully visible. */
14816 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14817 && row->phys_ascent > row->ascent)
14818 {
14819 row->height += row->phys_ascent - row->ascent;
14820 row->ascent = row->phys_ascent;
14821 }
14822
14823 /* Compute how much of the line is visible. */
14824 row->visible_height = row->height;
14825
14826 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14827 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14828
14829 if (row->y < min_y)
14830 row->visible_height -= min_y - row->y;
14831 if (row->y + row->height > max_y)
14832 row->visible_height -= row->y + row->height - max_y;
14833 }
14834 else
14835 {
14836 row->pixel_width = row->used[TEXT_AREA];
14837 if (row->continued_p)
14838 row->pixel_width -= it->continuation_pixel_width;
14839 else if (row->truncated_on_right_p)
14840 row->pixel_width -= it->truncation_pixel_width;
14841 row->ascent = row->phys_ascent = 0;
14842 row->height = row->phys_height = row->visible_height = 1;
14843 row->extra_line_spacing = 0;
14844 }
14845
14846 /* Compute a hash code for this row. */
14847 row->hash = 0;
14848 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14849 for (i = 0; i < row->used[area]; ++i)
14850 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14851 + row->glyphs[area][i].u.val
14852 + row->glyphs[area][i].face_id
14853 + row->glyphs[area][i].padding_p
14854 + (row->glyphs[area][i].type << 2));
14855
14856 it->max_ascent = it->max_descent = 0;
14857 it->max_phys_ascent = it->max_phys_descent = 0;
14858 }
14859
14860
14861 /* Append one space to the glyph row of iterator IT if doing a
14862 window-based redisplay. The space has the same face as
14863 IT->face_id. Value is non-zero if a space was added.
14864
14865 This function is called to make sure that there is always one glyph
14866 at the end of a glyph row that the cursor can be set on under
14867 window-systems. (If there weren't such a glyph we would not know
14868 how wide and tall a box cursor should be displayed).
14869
14870 At the same time this space let's a nicely handle clearing to the
14871 end of the line if the row ends in italic text. */
14872
14873 static int
14874 append_space_for_newline (it, default_face_p)
14875 struct it *it;
14876 int default_face_p;
14877 {
14878 if (FRAME_WINDOW_P (it->f))
14879 {
14880 int n = it->glyph_row->used[TEXT_AREA];
14881
14882 if (it->glyph_row->glyphs[TEXT_AREA] + n
14883 < it->glyph_row->glyphs[1 + TEXT_AREA])
14884 {
14885 /* Save some values that must not be changed.
14886 Must save IT->c and IT->len because otherwise
14887 ITERATOR_AT_END_P wouldn't work anymore after
14888 append_space_for_newline has been called. */
14889 enum display_element_type saved_what = it->what;
14890 int saved_c = it->c, saved_len = it->len;
14891 int saved_x = it->current_x;
14892 int saved_face_id = it->face_id;
14893 struct text_pos saved_pos;
14894 Lisp_Object saved_object;
14895 struct face *face;
14896
14897 saved_object = it->object;
14898 saved_pos = it->position;
14899
14900 it->what = IT_CHARACTER;
14901 bzero (&it->position, sizeof it->position);
14902 it->object = make_number (0);
14903 it->c = ' ';
14904 it->len = 1;
14905
14906 if (default_face_p)
14907 it->face_id = DEFAULT_FACE_ID;
14908 else if (it->face_before_selective_p)
14909 it->face_id = it->saved_face_id;
14910 face = FACE_FROM_ID (it->f, it->face_id);
14911 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14912
14913 PRODUCE_GLYPHS (it);
14914
14915 it->override_ascent = -1;
14916 it->constrain_row_ascent_descent_p = 0;
14917 it->current_x = saved_x;
14918 it->object = saved_object;
14919 it->position = saved_pos;
14920 it->what = saved_what;
14921 it->face_id = saved_face_id;
14922 it->len = saved_len;
14923 it->c = saved_c;
14924 return 1;
14925 }
14926 }
14927
14928 return 0;
14929 }
14930
14931
14932 /* Extend the face of the last glyph in the text area of IT->glyph_row
14933 to the end of the display line. Called from display_line.
14934 If the glyph row is empty, add a space glyph to it so that we
14935 know the face to draw. Set the glyph row flag fill_line_p. */
14936
14937 static void
14938 extend_face_to_end_of_line (it)
14939 struct it *it;
14940 {
14941 struct face *face;
14942 struct frame *f = it->f;
14943
14944 /* If line is already filled, do nothing. */
14945 if (it->current_x >= it->last_visible_x)
14946 return;
14947
14948 /* Face extension extends the background and box of IT->face_id
14949 to the end of the line. If the background equals the background
14950 of the frame, we don't have to do anything. */
14951 if (it->face_before_selective_p)
14952 face = FACE_FROM_ID (it->f, it->saved_face_id);
14953 else
14954 face = FACE_FROM_ID (f, it->face_id);
14955
14956 if (FRAME_WINDOW_P (f)
14957 && face->box == FACE_NO_BOX
14958 && face->background == FRAME_BACKGROUND_PIXEL (f)
14959 && !face->stipple)
14960 return;
14961
14962 /* Set the glyph row flag indicating that the face of the last glyph
14963 in the text area has to be drawn to the end of the text area. */
14964 it->glyph_row->fill_line_p = 1;
14965
14966 /* If current character of IT is not ASCII, make sure we have the
14967 ASCII face. This will be automatically undone the next time
14968 get_next_display_element returns a multibyte character. Note
14969 that the character will always be single byte in unibyte text. */
14970 if (!SINGLE_BYTE_CHAR_P (it->c))
14971 {
14972 it->face_id = FACE_FOR_CHAR (f, face, 0);
14973 }
14974
14975 if (FRAME_WINDOW_P (f))
14976 {
14977 /* If the row is empty, add a space with the current face of IT,
14978 so that we know which face to draw. */
14979 if (it->glyph_row->used[TEXT_AREA] == 0)
14980 {
14981 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14982 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14983 it->glyph_row->used[TEXT_AREA] = 1;
14984 }
14985 }
14986 else
14987 {
14988 /* Save some values that must not be changed. */
14989 int saved_x = it->current_x;
14990 struct text_pos saved_pos;
14991 Lisp_Object saved_object;
14992 enum display_element_type saved_what = it->what;
14993 int saved_face_id = it->face_id;
14994
14995 saved_object = it->object;
14996 saved_pos = it->position;
14997
14998 it->what = IT_CHARACTER;
14999 bzero (&it->position, sizeof it->position);
15000 it->object = make_number (0);
15001 it->c = ' ';
15002 it->len = 1;
15003 it->face_id = face->id;
15004
15005 PRODUCE_GLYPHS (it);
15006
15007 while (it->current_x <= it->last_visible_x)
15008 PRODUCE_GLYPHS (it);
15009
15010 /* Don't count these blanks really. It would let us insert a left
15011 truncation glyph below and make us set the cursor on them, maybe. */
15012 it->current_x = saved_x;
15013 it->object = saved_object;
15014 it->position = saved_pos;
15015 it->what = saved_what;
15016 it->face_id = saved_face_id;
15017 }
15018 }
15019
15020
15021 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15022 trailing whitespace. */
15023
15024 static int
15025 trailing_whitespace_p (charpos)
15026 int charpos;
15027 {
15028 int bytepos = CHAR_TO_BYTE (charpos);
15029 int c = 0;
15030
15031 while (bytepos < ZV_BYTE
15032 && (c = FETCH_CHAR (bytepos),
15033 c == ' ' || c == '\t'))
15034 ++bytepos;
15035
15036 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15037 {
15038 if (bytepos != PT_BYTE)
15039 return 1;
15040 }
15041 return 0;
15042 }
15043
15044
15045 /* Highlight trailing whitespace, if any, in ROW. */
15046
15047 void
15048 highlight_trailing_whitespace (f, row)
15049 struct frame *f;
15050 struct glyph_row *row;
15051 {
15052 int used = row->used[TEXT_AREA];
15053
15054 if (used)
15055 {
15056 struct glyph *start = row->glyphs[TEXT_AREA];
15057 struct glyph *glyph = start + used - 1;
15058
15059 /* Skip over glyphs inserted to display the cursor at the
15060 end of a line, for extending the face of the last glyph
15061 to the end of the line on terminals, and for truncation
15062 and continuation glyphs. */
15063 while (glyph >= start
15064 && glyph->type == CHAR_GLYPH
15065 && INTEGERP (glyph->object))
15066 --glyph;
15067
15068 /* If last glyph is a space or stretch, and it's trailing
15069 whitespace, set the face of all trailing whitespace glyphs in
15070 IT->glyph_row to `trailing-whitespace'. */
15071 if (glyph >= start
15072 && BUFFERP (glyph->object)
15073 && (glyph->type == STRETCH_GLYPH
15074 || (glyph->type == CHAR_GLYPH
15075 && glyph->u.ch == ' '))
15076 && trailing_whitespace_p (glyph->charpos))
15077 {
15078 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15079 if (face_id < 0)
15080 return;
15081
15082 while (glyph >= start
15083 && BUFFERP (glyph->object)
15084 && (glyph->type == STRETCH_GLYPH
15085 || (glyph->type == CHAR_GLYPH
15086 && glyph->u.ch == ' ')))
15087 (glyph--)->face_id = face_id;
15088 }
15089 }
15090 }
15091
15092
15093 /* Value is non-zero if glyph row ROW in window W should be
15094 used to hold the cursor. */
15095
15096 static int
15097 cursor_row_p (w, row)
15098 struct window *w;
15099 struct glyph_row *row;
15100 {
15101 int cursor_row_p = 1;
15102
15103 if (PT == MATRIX_ROW_END_CHARPOS (row))
15104 {
15105 /* If the row ends with a newline from a string, we don't want
15106 the cursor there, but we still want it at the start of the
15107 string if the string starts in this row.
15108 If the row is continued it doesn't end in a newline. */
15109 if (CHARPOS (row->end.string_pos) >= 0)
15110 cursor_row_p = (row->continued_p
15111 || PT >= MATRIX_ROW_START_CHARPOS (row));
15112 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15113 {
15114 /* If the row ends in middle of a real character,
15115 and the line is continued, we want the cursor here.
15116 That's because MATRIX_ROW_END_CHARPOS would equal
15117 PT if PT is before the character. */
15118 if (!row->ends_in_ellipsis_p)
15119 cursor_row_p = row->continued_p;
15120 else
15121 /* If the row ends in an ellipsis, then
15122 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15123 We want that position to be displayed after the ellipsis. */
15124 cursor_row_p = 0;
15125 }
15126 /* If the row ends at ZV, display the cursor at the end of that
15127 row instead of at the start of the row below. */
15128 else if (row->ends_at_zv_p)
15129 cursor_row_p = 1;
15130 else
15131 cursor_row_p = 0;
15132 }
15133
15134 return cursor_row_p;
15135 }
15136
15137
15138 /* Construct the glyph row IT->glyph_row in the desired matrix of
15139 IT->w from text at the current position of IT. See dispextern.h
15140 for an overview of struct it. Value is non-zero if
15141 IT->glyph_row displays text, as opposed to a line displaying ZV
15142 only. */
15143
15144 static int
15145 display_line (it)
15146 struct it *it;
15147 {
15148 struct glyph_row *row = it->glyph_row;
15149 Lisp_Object overlay_arrow_string;
15150
15151 /* We always start displaying at hpos zero even if hscrolled. */
15152 xassert (it->hpos == 0 && it->current_x == 0);
15153
15154 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15155 >= it->w->desired_matrix->nrows)
15156 {
15157 it->w->nrows_scale_factor++;
15158 fonts_changed_p = 1;
15159 return 0;
15160 }
15161
15162 /* Is IT->w showing the region? */
15163 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15164
15165 /* Clear the result glyph row and enable it. */
15166 prepare_desired_row (row);
15167
15168 row->y = it->current_y;
15169 row->start = it->start;
15170 row->continuation_lines_width = it->continuation_lines_width;
15171 row->displays_text_p = 1;
15172 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15173 it->starts_in_middle_of_char_p = 0;
15174
15175 /* Arrange the overlays nicely for our purposes. Usually, we call
15176 display_line on only one line at a time, in which case this
15177 can't really hurt too much, or we call it on lines which appear
15178 one after another in the buffer, in which case all calls to
15179 recenter_overlay_lists but the first will be pretty cheap. */
15180 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15181
15182 /* Move over display elements that are not visible because we are
15183 hscrolled. This may stop at an x-position < IT->first_visible_x
15184 if the first glyph is partially visible or if we hit a line end. */
15185 if (it->current_x < it->first_visible_x)
15186 {
15187 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15188 MOVE_TO_POS | MOVE_TO_X);
15189 }
15190
15191 /* Get the initial row height. This is either the height of the
15192 text hscrolled, if there is any, or zero. */
15193 row->ascent = it->max_ascent;
15194 row->height = it->max_ascent + it->max_descent;
15195 row->phys_ascent = it->max_phys_ascent;
15196 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15197 row->extra_line_spacing = it->max_extra_line_spacing;
15198
15199 /* Loop generating characters. The loop is left with IT on the next
15200 character to display. */
15201 while (1)
15202 {
15203 int n_glyphs_before, hpos_before, x_before;
15204 int x, i, nglyphs;
15205 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15206
15207 /* Retrieve the next thing to display. Value is zero if end of
15208 buffer reached. */
15209 if (!get_next_display_element (it))
15210 {
15211 /* Maybe add a space at the end of this line that is used to
15212 display the cursor there under X. Set the charpos of the
15213 first glyph of blank lines not corresponding to any text
15214 to -1. */
15215 #ifdef HAVE_WINDOW_SYSTEM
15216 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15217 row->exact_window_width_line_p = 1;
15218 else
15219 #endif /* HAVE_WINDOW_SYSTEM */
15220 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15221 || row->used[TEXT_AREA] == 0)
15222 {
15223 row->glyphs[TEXT_AREA]->charpos = -1;
15224 row->displays_text_p = 0;
15225
15226 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15227 && (!MINI_WINDOW_P (it->w)
15228 || (minibuf_level && EQ (it->window, minibuf_window))))
15229 row->indicate_empty_line_p = 1;
15230 }
15231
15232 it->continuation_lines_width = 0;
15233 row->ends_at_zv_p = 1;
15234 break;
15235 }
15236
15237 /* Now, get the metrics of what we want to display. This also
15238 generates glyphs in `row' (which is IT->glyph_row). */
15239 n_glyphs_before = row->used[TEXT_AREA];
15240 x = it->current_x;
15241
15242 /* Remember the line height so far in case the next element doesn't
15243 fit on the line. */
15244 if (!it->truncate_lines_p)
15245 {
15246 ascent = it->max_ascent;
15247 descent = it->max_descent;
15248 phys_ascent = it->max_phys_ascent;
15249 phys_descent = it->max_phys_descent;
15250 }
15251
15252 PRODUCE_GLYPHS (it);
15253
15254 /* If this display element was in marginal areas, continue with
15255 the next one. */
15256 if (it->area != TEXT_AREA)
15257 {
15258 row->ascent = max (row->ascent, it->max_ascent);
15259 row->height = max (row->height, it->max_ascent + it->max_descent);
15260 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15261 row->phys_height = max (row->phys_height,
15262 it->max_phys_ascent + it->max_phys_descent);
15263 row->extra_line_spacing = max (row->extra_line_spacing,
15264 it->max_extra_line_spacing);
15265 set_iterator_to_next (it, 1);
15266 continue;
15267 }
15268
15269 /* Does the display element fit on the line? If we truncate
15270 lines, we should draw past the right edge of the window. If
15271 we don't truncate, we want to stop so that we can display the
15272 continuation glyph before the right margin. If lines are
15273 continued, there are two possible strategies for characters
15274 resulting in more than 1 glyph (e.g. tabs): Display as many
15275 glyphs as possible in this line and leave the rest for the
15276 continuation line, or display the whole element in the next
15277 line. Original redisplay did the former, so we do it also. */
15278 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15279 hpos_before = it->hpos;
15280 x_before = x;
15281
15282 if (/* Not a newline. */
15283 nglyphs > 0
15284 /* Glyphs produced fit entirely in the line. */
15285 && it->current_x < it->last_visible_x)
15286 {
15287 it->hpos += nglyphs;
15288 row->ascent = max (row->ascent, it->max_ascent);
15289 row->height = max (row->height, it->max_ascent + it->max_descent);
15290 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15291 row->phys_height = max (row->phys_height,
15292 it->max_phys_ascent + it->max_phys_descent);
15293 row->extra_line_spacing = max (row->extra_line_spacing,
15294 it->max_extra_line_spacing);
15295 if (it->current_x - it->pixel_width < it->first_visible_x)
15296 row->x = x - it->first_visible_x;
15297 }
15298 else
15299 {
15300 int new_x;
15301 struct glyph *glyph;
15302
15303 for (i = 0; i < nglyphs; ++i, x = new_x)
15304 {
15305 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15306 new_x = x + glyph->pixel_width;
15307
15308 if (/* Lines are continued. */
15309 !it->truncate_lines_p
15310 && (/* Glyph doesn't fit on the line. */
15311 new_x > it->last_visible_x
15312 /* Or it fits exactly on a window system frame. */
15313 || (new_x == it->last_visible_x
15314 && FRAME_WINDOW_P (it->f))))
15315 {
15316 /* End of a continued line. */
15317
15318 if (it->hpos == 0
15319 || (new_x == it->last_visible_x
15320 && FRAME_WINDOW_P (it->f)))
15321 {
15322 /* Current glyph is the only one on the line or
15323 fits exactly on the line. We must continue
15324 the line because we can't draw the cursor
15325 after the glyph. */
15326 row->continued_p = 1;
15327 it->current_x = new_x;
15328 it->continuation_lines_width += new_x;
15329 ++it->hpos;
15330 if (i == nglyphs - 1)
15331 {
15332 set_iterator_to_next (it, 1);
15333 #ifdef HAVE_WINDOW_SYSTEM
15334 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15335 {
15336 if (!get_next_display_element (it))
15337 {
15338 row->exact_window_width_line_p = 1;
15339 it->continuation_lines_width = 0;
15340 row->continued_p = 0;
15341 row->ends_at_zv_p = 1;
15342 }
15343 else if (ITERATOR_AT_END_OF_LINE_P (it))
15344 {
15345 row->continued_p = 0;
15346 row->exact_window_width_line_p = 1;
15347 }
15348 }
15349 #endif /* HAVE_WINDOW_SYSTEM */
15350 }
15351 }
15352 else if (CHAR_GLYPH_PADDING_P (*glyph)
15353 && !FRAME_WINDOW_P (it->f))
15354 {
15355 /* A padding glyph that doesn't fit on this line.
15356 This means the whole character doesn't fit
15357 on the line. */
15358 row->used[TEXT_AREA] = n_glyphs_before;
15359
15360 /* Fill the rest of the row with continuation
15361 glyphs like in 20.x. */
15362 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15363 < row->glyphs[1 + TEXT_AREA])
15364 produce_special_glyphs (it, IT_CONTINUATION);
15365
15366 row->continued_p = 1;
15367 it->current_x = x_before;
15368 it->continuation_lines_width += x_before;
15369
15370 /* Restore the height to what it was before the
15371 element not fitting on the line. */
15372 it->max_ascent = ascent;
15373 it->max_descent = descent;
15374 it->max_phys_ascent = phys_ascent;
15375 it->max_phys_descent = phys_descent;
15376 }
15377 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15378 {
15379 /* A TAB that extends past the right edge of the
15380 window. This produces a single glyph on
15381 window system frames. We leave the glyph in
15382 this row and let it fill the row, but don't
15383 consume the TAB. */
15384 it->continuation_lines_width += it->last_visible_x;
15385 row->ends_in_middle_of_char_p = 1;
15386 row->continued_p = 1;
15387 glyph->pixel_width = it->last_visible_x - x;
15388 it->starts_in_middle_of_char_p = 1;
15389 }
15390 else
15391 {
15392 /* Something other than a TAB that draws past
15393 the right edge of the window. Restore
15394 positions to values before the element. */
15395 row->used[TEXT_AREA] = n_glyphs_before + i;
15396
15397 /* Display continuation glyphs. */
15398 if (!FRAME_WINDOW_P (it->f))
15399 produce_special_glyphs (it, IT_CONTINUATION);
15400 row->continued_p = 1;
15401
15402 it->continuation_lines_width += x;
15403
15404 if (nglyphs > 1 && i > 0)
15405 {
15406 row->ends_in_middle_of_char_p = 1;
15407 it->starts_in_middle_of_char_p = 1;
15408 }
15409
15410 /* Restore the height to what it was before the
15411 element not fitting on the line. */
15412 it->max_ascent = ascent;
15413 it->max_descent = descent;
15414 it->max_phys_ascent = phys_ascent;
15415 it->max_phys_descent = phys_descent;
15416 }
15417
15418 break;
15419 }
15420 else if (new_x > it->first_visible_x)
15421 {
15422 /* Increment number of glyphs actually displayed. */
15423 ++it->hpos;
15424
15425 if (x < it->first_visible_x)
15426 /* Glyph is partially visible, i.e. row starts at
15427 negative X position. */
15428 row->x = x - it->first_visible_x;
15429 }
15430 else
15431 {
15432 /* Glyph is completely off the left margin of the
15433 window. This should not happen because of the
15434 move_it_in_display_line at the start of this
15435 function, unless the text display area of the
15436 window is empty. */
15437 xassert (it->first_visible_x <= it->last_visible_x);
15438 }
15439 }
15440
15441 row->ascent = max (row->ascent, it->max_ascent);
15442 row->height = max (row->height, it->max_ascent + it->max_descent);
15443 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15444 row->phys_height = max (row->phys_height,
15445 it->max_phys_ascent + it->max_phys_descent);
15446 row->extra_line_spacing = max (row->extra_line_spacing,
15447 it->max_extra_line_spacing);
15448
15449 /* End of this display line if row is continued. */
15450 if (row->continued_p || row->ends_at_zv_p)
15451 break;
15452 }
15453
15454 at_end_of_line:
15455 /* Is this a line end? If yes, we're also done, after making
15456 sure that a non-default face is extended up to the right
15457 margin of the window. */
15458 if (ITERATOR_AT_END_OF_LINE_P (it))
15459 {
15460 int used_before = row->used[TEXT_AREA];
15461
15462 row->ends_in_newline_from_string_p = STRINGP (it->object);
15463
15464 #ifdef HAVE_WINDOW_SYSTEM
15465 /* Add a space at the end of the line that is used to
15466 display the cursor there. */
15467 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15468 append_space_for_newline (it, 0);
15469 #endif /* HAVE_WINDOW_SYSTEM */
15470
15471 /* Extend the face to the end of the line. */
15472 extend_face_to_end_of_line (it);
15473
15474 /* Make sure we have the position. */
15475 if (used_before == 0)
15476 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15477
15478 /* Consume the line end. This skips over invisible lines. */
15479 set_iterator_to_next (it, 1);
15480 it->continuation_lines_width = 0;
15481 break;
15482 }
15483
15484 /* Proceed with next display element. Note that this skips
15485 over lines invisible because of selective display. */
15486 set_iterator_to_next (it, 1);
15487
15488 /* If we truncate lines, we are done when the last displayed
15489 glyphs reach past the right margin of the window. */
15490 if (it->truncate_lines_p
15491 && (FRAME_WINDOW_P (it->f)
15492 ? (it->current_x >= it->last_visible_x)
15493 : (it->current_x > it->last_visible_x)))
15494 {
15495 /* Maybe add truncation glyphs. */
15496 if (!FRAME_WINDOW_P (it->f))
15497 {
15498 int i, n;
15499
15500 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15501 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15502 break;
15503
15504 for (n = row->used[TEXT_AREA]; i < n; ++i)
15505 {
15506 row->used[TEXT_AREA] = i;
15507 produce_special_glyphs (it, IT_TRUNCATION);
15508 }
15509 }
15510 #ifdef HAVE_WINDOW_SYSTEM
15511 else
15512 {
15513 /* Don't truncate if we can overflow newline into fringe. */
15514 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15515 {
15516 if (!get_next_display_element (it))
15517 {
15518 it->continuation_lines_width = 0;
15519 row->ends_at_zv_p = 1;
15520 row->exact_window_width_line_p = 1;
15521 break;
15522 }
15523 if (ITERATOR_AT_END_OF_LINE_P (it))
15524 {
15525 row->exact_window_width_line_p = 1;
15526 goto at_end_of_line;
15527 }
15528 }
15529 }
15530 #endif /* HAVE_WINDOW_SYSTEM */
15531
15532 row->truncated_on_right_p = 1;
15533 it->continuation_lines_width = 0;
15534 reseat_at_next_visible_line_start (it, 0);
15535 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15536 it->hpos = hpos_before;
15537 it->current_x = x_before;
15538 break;
15539 }
15540 }
15541
15542 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15543 at the left window margin. */
15544 if (it->first_visible_x
15545 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15546 {
15547 if (!FRAME_WINDOW_P (it->f))
15548 insert_left_trunc_glyphs (it);
15549 row->truncated_on_left_p = 1;
15550 }
15551
15552 /* If the start of this line is the overlay arrow-position, then
15553 mark this glyph row as the one containing the overlay arrow.
15554 This is clearly a mess with variable size fonts. It would be
15555 better to let it be displayed like cursors under X. */
15556 if ((row->displays_text_p || !overlay_arrow_seen)
15557 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15558 !NILP (overlay_arrow_string)))
15559 {
15560 /* Overlay arrow in window redisplay is a fringe bitmap. */
15561 if (STRINGP (overlay_arrow_string))
15562 {
15563 struct glyph_row *arrow_row
15564 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15565 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15566 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15567 struct glyph *p = row->glyphs[TEXT_AREA];
15568 struct glyph *p2, *end;
15569
15570 /* Copy the arrow glyphs. */
15571 while (glyph < arrow_end)
15572 *p++ = *glyph++;
15573
15574 /* Throw away padding glyphs. */
15575 p2 = p;
15576 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15577 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15578 ++p2;
15579 if (p2 > p)
15580 {
15581 while (p2 < end)
15582 *p++ = *p2++;
15583 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15584 }
15585 }
15586 else
15587 {
15588 xassert (INTEGERP (overlay_arrow_string));
15589 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15590 }
15591 overlay_arrow_seen = 1;
15592 }
15593
15594 /* Compute pixel dimensions of this line. */
15595 compute_line_metrics (it);
15596
15597 /* Remember the position at which this line ends. */
15598 row->end = it->current;
15599
15600 /* Record whether this row ends inside an ellipsis. */
15601 row->ends_in_ellipsis_p
15602 = (it->method == GET_FROM_DISPLAY_VECTOR
15603 && it->ellipsis_p);
15604
15605 /* Save fringe bitmaps in this row. */
15606 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15607 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15608 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15609 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15610
15611 it->left_user_fringe_bitmap = 0;
15612 it->left_user_fringe_face_id = 0;
15613 it->right_user_fringe_bitmap = 0;
15614 it->right_user_fringe_face_id = 0;
15615
15616 /* Maybe set the cursor. */
15617 if (it->w->cursor.vpos < 0
15618 && PT >= MATRIX_ROW_START_CHARPOS (row)
15619 && PT <= MATRIX_ROW_END_CHARPOS (row)
15620 && cursor_row_p (it->w, row))
15621 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15622
15623 /* Highlight trailing whitespace. */
15624 if (!NILP (Vshow_trailing_whitespace))
15625 highlight_trailing_whitespace (it->f, it->glyph_row);
15626
15627 /* Prepare for the next line. This line starts horizontally at (X
15628 HPOS) = (0 0). Vertical positions are incremented. As a
15629 convenience for the caller, IT->glyph_row is set to the next
15630 row to be used. */
15631 it->current_x = it->hpos = 0;
15632 it->current_y += row->height;
15633 ++it->vpos;
15634 ++it->glyph_row;
15635 it->start = it->current;
15636 return row->displays_text_p;
15637 }
15638
15639
15640 \f
15641 /***********************************************************************
15642 Menu Bar
15643 ***********************************************************************/
15644
15645 /* Redisplay the menu bar in the frame for window W.
15646
15647 The menu bar of X frames that don't have X toolkit support is
15648 displayed in a special window W->frame->menu_bar_window.
15649
15650 The menu bar of terminal frames is treated specially as far as
15651 glyph matrices are concerned. Menu bar lines are not part of
15652 windows, so the update is done directly on the frame matrix rows
15653 for the menu bar. */
15654
15655 static void
15656 display_menu_bar (w)
15657 struct window *w;
15658 {
15659 struct frame *f = XFRAME (WINDOW_FRAME (w));
15660 struct it it;
15661 Lisp_Object items;
15662 int i;
15663
15664 /* Don't do all this for graphical frames. */
15665 #ifdef HAVE_NTGUI
15666 if (!NILP (Vwindow_system))
15667 return;
15668 #endif
15669 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15670 if (FRAME_X_P (f))
15671 return;
15672 #endif
15673 #ifdef MAC_OS
15674 if (FRAME_MAC_P (f))
15675 return;
15676 #endif
15677
15678 #ifdef USE_X_TOOLKIT
15679 xassert (!FRAME_WINDOW_P (f));
15680 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15681 it.first_visible_x = 0;
15682 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15683 #else /* not USE_X_TOOLKIT */
15684 if (FRAME_WINDOW_P (f))
15685 {
15686 /* Menu bar lines are displayed in the desired matrix of the
15687 dummy window menu_bar_window. */
15688 struct window *menu_w;
15689 xassert (WINDOWP (f->menu_bar_window));
15690 menu_w = XWINDOW (f->menu_bar_window);
15691 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15692 MENU_FACE_ID);
15693 it.first_visible_x = 0;
15694 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15695 }
15696 else
15697 {
15698 /* This is a TTY frame, i.e. character hpos/vpos are used as
15699 pixel x/y. */
15700 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15701 MENU_FACE_ID);
15702 it.first_visible_x = 0;
15703 it.last_visible_x = FRAME_COLS (f);
15704 }
15705 #endif /* not USE_X_TOOLKIT */
15706
15707 if (! mode_line_inverse_video)
15708 /* Force the menu-bar to be displayed in the default face. */
15709 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15710
15711 /* Clear all rows of the menu bar. */
15712 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15713 {
15714 struct glyph_row *row = it.glyph_row + i;
15715 clear_glyph_row (row);
15716 row->enabled_p = 1;
15717 row->full_width_p = 1;
15718 }
15719
15720 /* Display all items of the menu bar. */
15721 items = FRAME_MENU_BAR_ITEMS (it.f);
15722 for (i = 0; i < XVECTOR (items)->size; i += 4)
15723 {
15724 Lisp_Object string;
15725
15726 /* Stop at nil string. */
15727 string = AREF (items, i + 1);
15728 if (NILP (string))
15729 break;
15730
15731 /* Remember where item was displayed. */
15732 AREF (items, i + 3) = make_number (it.hpos);
15733
15734 /* Display the item, pad with one space. */
15735 if (it.current_x < it.last_visible_x)
15736 display_string (NULL, string, Qnil, 0, 0, &it,
15737 SCHARS (string) + 1, 0, 0, -1);
15738 }
15739
15740 /* Fill out the line with spaces. */
15741 if (it.current_x < it.last_visible_x)
15742 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15743
15744 /* Compute the total height of the lines. */
15745 compute_line_metrics (&it);
15746 }
15747
15748
15749 \f
15750 /***********************************************************************
15751 Mode Line
15752 ***********************************************************************/
15753
15754 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15755 FORCE is non-zero, redisplay mode lines unconditionally.
15756 Otherwise, redisplay only mode lines that are garbaged. Value is
15757 the number of windows whose mode lines were redisplayed. */
15758
15759 static int
15760 redisplay_mode_lines (window, force)
15761 Lisp_Object window;
15762 int force;
15763 {
15764 int nwindows = 0;
15765
15766 while (!NILP (window))
15767 {
15768 struct window *w = XWINDOW (window);
15769
15770 if (WINDOWP (w->hchild))
15771 nwindows += redisplay_mode_lines (w->hchild, force);
15772 else if (WINDOWP (w->vchild))
15773 nwindows += redisplay_mode_lines (w->vchild, force);
15774 else if (force
15775 || FRAME_GARBAGED_P (XFRAME (w->frame))
15776 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15777 {
15778 struct text_pos lpoint;
15779 struct buffer *old = current_buffer;
15780
15781 /* Set the window's buffer for the mode line display. */
15782 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15783 set_buffer_internal_1 (XBUFFER (w->buffer));
15784
15785 /* Point refers normally to the selected window. For any
15786 other window, set up appropriate value. */
15787 if (!EQ (window, selected_window))
15788 {
15789 struct text_pos pt;
15790
15791 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15792 if (CHARPOS (pt) < BEGV)
15793 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15794 else if (CHARPOS (pt) > (ZV - 1))
15795 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15796 else
15797 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15798 }
15799
15800 /* Display mode lines. */
15801 clear_glyph_matrix (w->desired_matrix);
15802 if (display_mode_lines (w))
15803 {
15804 ++nwindows;
15805 w->must_be_updated_p = 1;
15806 }
15807
15808 /* Restore old settings. */
15809 set_buffer_internal_1 (old);
15810 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15811 }
15812
15813 window = w->next;
15814 }
15815
15816 return nwindows;
15817 }
15818
15819
15820 /* Display the mode and/or top line of window W. Value is the number
15821 of mode lines displayed. */
15822
15823 static int
15824 display_mode_lines (w)
15825 struct window *w;
15826 {
15827 Lisp_Object old_selected_window, old_selected_frame;
15828 int n = 0;
15829
15830 old_selected_frame = selected_frame;
15831 selected_frame = w->frame;
15832 old_selected_window = selected_window;
15833 XSETWINDOW (selected_window, w);
15834
15835 /* These will be set while the mode line specs are processed. */
15836 line_number_displayed = 0;
15837 w->column_number_displayed = Qnil;
15838
15839 if (WINDOW_WANTS_MODELINE_P (w))
15840 {
15841 struct window *sel_w = XWINDOW (old_selected_window);
15842
15843 /* Select mode line face based on the real selected window. */
15844 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15845 current_buffer->mode_line_format);
15846 ++n;
15847 }
15848
15849 if (WINDOW_WANTS_HEADER_LINE_P (w))
15850 {
15851 display_mode_line (w, HEADER_LINE_FACE_ID,
15852 current_buffer->header_line_format);
15853 ++n;
15854 }
15855
15856 selected_frame = old_selected_frame;
15857 selected_window = old_selected_window;
15858 return n;
15859 }
15860
15861
15862 /* Display mode or top line of window W. FACE_ID specifies which line
15863 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15864 FORMAT is the mode line format to display. Value is the pixel
15865 height of the mode line displayed. */
15866
15867 static int
15868 display_mode_line (w, face_id, format)
15869 struct window *w;
15870 enum face_id face_id;
15871 Lisp_Object format;
15872 {
15873 struct it it;
15874 struct face *face;
15875 int count = SPECPDL_INDEX ();
15876
15877 init_iterator (&it, w, -1, -1, NULL, face_id);
15878 prepare_desired_row (it.glyph_row);
15879
15880 it.glyph_row->mode_line_p = 1;
15881
15882 if (! mode_line_inverse_video)
15883 /* Force the mode-line to be displayed in the default face. */
15884 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15885
15886 record_unwind_protect (unwind_format_mode_line,
15887 format_mode_line_unwind_data (NULL));
15888
15889 mode_line_target = MODE_LINE_DISPLAY;
15890
15891 /* Temporarily make frame's keyboard the current kboard so that
15892 kboard-local variables in the mode_line_format will get the right
15893 values. */
15894 push_frame_kboard (it.f);
15895 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15896 pop_frame_kboard ();
15897
15898 unbind_to (count, Qnil);
15899
15900 /* Fill up with spaces. */
15901 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15902
15903 compute_line_metrics (&it);
15904 it.glyph_row->full_width_p = 1;
15905 it.glyph_row->continued_p = 0;
15906 it.glyph_row->truncated_on_left_p = 0;
15907 it.glyph_row->truncated_on_right_p = 0;
15908
15909 /* Make a 3D mode-line have a shadow at its right end. */
15910 face = FACE_FROM_ID (it.f, face_id);
15911 extend_face_to_end_of_line (&it);
15912 if (face->box != FACE_NO_BOX)
15913 {
15914 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15915 + it.glyph_row->used[TEXT_AREA] - 1);
15916 last->right_box_line_p = 1;
15917 }
15918
15919 return it.glyph_row->height;
15920 }
15921
15922 /* Contribute ELT to the mode line for window IT->w. How it
15923 translates into text depends on its data type.
15924
15925 IT describes the display environment in which we display, as usual.
15926
15927 DEPTH is the depth in recursion. It is used to prevent
15928 infinite recursion here.
15929
15930 FIELD_WIDTH is the number of characters the display of ELT should
15931 occupy in the mode line, and PRECISION is the maximum number of
15932 characters to display from ELT's representation. See
15933 display_string for details.
15934
15935 Returns the hpos of the end of the text generated by ELT.
15936
15937 PROPS is a property list to add to any string we encounter.
15938
15939 If RISKY is nonzero, remove (disregard) any properties in any string
15940 we encounter, and ignore :eval and :propertize.
15941
15942 The global variable `mode_line_target' determines whether the
15943 output is passed to `store_mode_line_noprop',
15944 `store_mode_line_string', or `display_string'. */
15945
15946 static int
15947 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15948 struct it *it;
15949 int depth;
15950 int field_width, precision;
15951 Lisp_Object elt, props;
15952 int risky;
15953 {
15954 int n = 0, field, prec;
15955 int literal = 0;
15956
15957 tail_recurse:
15958 if (depth > 100)
15959 elt = build_string ("*too-deep*");
15960
15961 depth++;
15962
15963 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15964 {
15965 case Lisp_String:
15966 {
15967 /* A string: output it and check for %-constructs within it. */
15968 unsigned char c;
15969 int offset = 0;
15970
15971 if (!NILP (props) || risky)
15972 {
15973 Lisp_Object oprops, aelt;
15974 oprops = Ftext_properties_at (make_number (0), elt);
15975
15976 /* If the starting string's properties are not what
15977 we want, translate the string. Also, if the string
15978 is risky, do that anyway. */
15979
15980 if (NILP (Fequal (props, oprops)) || risky)
15981 {
15982 /* If the starting string has properties,
15983 merge the specified ones onto the existing ones. */
15984 if (! NILP (oprops) && !risky)
15985 {
15986 Lisp_Object tem;
15987
15988 oprops = Fcopy_sequence (oprops);
15989 tem = props;
15990 while (CONSP (tem))
15991 {
15992 oprops = Fplist_put (oprops, XCAR (tem),
15993 XCAR (XCDR (tem)));
15994 tem = XCDR (XCDR (tem));
15995 }
15996 props = oprops;
15997 }
15998
15999 aelt = Fassoc (elt, mode_line_proptrans_alist);
16000 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16001 {
16002 mode_line_proptrans_alist
16003 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
16004 elt = XCAR (aelt);
16005 }
16006 else
16007 {
16008 Lisp_Object tem;
16009
16010 elt = Fcopy_sequence (elt);
16011 Fset_text_properties (make_number (0), Flength (elt),
16012 props, elt);
16013 /* Add this item to mode_line_proptrans_alist. */
16014 mode_line_proptrans_alist
16015 = Fcons (Fcons (elt, props),
16016 mode_line_proptrans_alist);
16017 /* Truncate mode_line_proptrans_alist
16018 to at most 50 elements. */
16019 tem = Fnthcdr (make_number (50),
16020 mode_line_proptrans_alist);
16021 if (! NILP (tem))
16022 XSETCDR (tem, Qnil);
16023 }
16024 }
16025 }
16026
16027 offset = 0;
16028
16029 if (literal)
16030 {
16031 prec = precision - n;
16032 switch (mode_line_target)
16033 {
16034 case MODE_LINE_NOPROP:
16035 case MODE_LINE_TITLE:
16036 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16037 break;
16038 case MODE_LINE_STRING:
16039 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16040 break;
16041 case MODE_LINE_DISPLAY:
16042 n += display_string (NULL, elt, Qnil, 0, 0, it,
16043 0, prec, 0, STRING_MULTIBYTE (elt));
16044 break;
16045 }
16046
16047 break;
16048 }
16049
16050 /* Handle the non-literal case. */
16051
16052 while ((precision <= 0 || n < precision)
16053 && SREF (elt, offset) != 0
16054 && (mode_line_target != MODE_LINE_DISPLAY
16055 || it->current_x < it->last_visible_x))
16056 {
16057 int last_offset = offset;
16058
16059 /* Advance to end of string or next format specifier. */
16060 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16061 ;
16062
16063 if (offset - 1 != last_offset)
16064 {
16065 int nchars, nbytes;
16066
16067 /* Output to end of string or up to '%'. Field width
16068 is length of string. Don't output more than
16069 PRECISION allows us. */
16070 offset--;
16071
16072 prec = c_string_width (SDATA (elt) + last_offset,
16073 offset - last_offset, precision - n,
16074 &nchars, &nbytes);
16075
16076 switch (mode_line_target)
16077 {
16078 case MODE_LINE_NOPROP:
16079 case MODE_LINE_TITLE:
16080 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16081 break;
16082 case MODE_LINE_STRING:
16083 {
16084 int bytepos = last_offset;
16085 int charpos = string_byte_to_char (elt, bytepos);
16086 int endpos = (precision <= 0
16087 ? string_byte_to_char (elt, offset)
16088 : charpos + nchars);
16089
16090 n += store_mode_line_string (NULL,
16091 Fsubstring (elt, make_number (charpos),
16092 make_number (endpos)),
16093 0, 0, 0, Qnil);
16094 }
16095 break;
16096 case MODE_LINE_DISPLAY:
16097 {
16098 int bytepos = last_offset;
16099 int charpos = string_byte_to_char (elt, bytepos);
16100 n += display_string (NULL, elt, Qnil, 0, charpos,
16101 it, 0, prec, 0,
16102 STRING_MULTIBYTE (elt));
16103 }
16104 break;
16105 }
16106 }
16107 else /* c == '%' */
16108 {
16109 int percent_position = offset;
16110
16111 /* Get the specified minimum width. Zero means
16112 don't pad. */
16113 field = 0;
16114 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16115 field = field * 10 + c - '0';
16116
16117 /* Don't pad beyond the total padding allowed. */
16118 if (field_width - n > 0 && field > field_width - n)
16119 field = field_width - n;
16120
16121 /* Note that either PRECISION <= 0 or N < PRECISION. */
16122 prec = precision - n;
16123
16124 if (c == 'M')
16125 n += display_mode_element (it, depth, field, prec,
16126 Vglobal_mode_string, props,
16127 risky);
16128 else if (c != 0)
16129 {
16130 int multibyte;
16131 int bytepos, charpos;
16132 unsigned char *spec;
16133
16134 bytepos = percent_position;
16135 charpos = (STRING_MULTIBYTE (elt)
16136 ? string_byte_to_char (elt, bytepos)
16137 : bytepos);
16138
16139 spec
16140 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16141
16142 switch (mode_line_target)
16143 {
16144 case MODE_LINE_NOPROP:
16145 case MODE_LINE_TITLE:
16146 n += store_mode_line_noprop (spec, field, prec);
16147 break;
16148 case MODE_LINE_STRING:
16149 {
16150 int len = strlen (spec);
16151 Lisp_Object tem = make_string (spec, len);
16152 props = Ftext_properties_at (make_number (charpos), elt);
16153 /* Should only keep face property in props */
16154 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16155 }
16156 break;
16157 case MODE_LINE_DISPLAY:
16158 {
16159 int nglyphs_before, nwritten;
16160
16161 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16162 nwritten = display_string (spec, Qnil, elt,
16163 charpos, 0, it,
16164 field, prec, 0,
16165 multibyte);
16166
16167 /* Assign to the glyphs written above the
16168 string where the `%x' came from, position
16169 of the `%'. */
16170 if (nwritten > 0)
16171 {
16172 struct glyph *glyph
16173 = (it->glyph_row->glyphs[TEXT_AREA]
16174 + nglyphs_before);
16175 int i;
16176
16177 for (i = 0; i < nwritten; ++i)
16178 {
16179 glyph[i].object = elt;
16180 glyph[i].charpos = charpos;
16181 }
16182
16183 n += nwritten;
16184 }
16185 }
16186 break;
16187 }
16188 }
16189 else /* c == 0 */
16190 break;
16191 }
16192 }
16193 }
16194 break;
16195
16196 case Lisp_Symbol:
16197 /* A symbol: process the value of the symbol recursively
16198 as if it appeared here directly. Avoid error if symbol void.
16199 Special case: if value of symbol is a string, output the string
16200 literally. */
16201 {
16202 register Lisp_Object tem;
16203
16204 /* If the variable is not marked as risky to set
16205 then its contents are risky to use. */
16206 if (NILP (Fget (elt, Qrisky_local_variable)))
16207 risky = 1;
16208
16209 tem = Fboundp (elt);
16210 if (!NILP (tem))
16211 {
16212 tem = Fsymbol_value (elt);
16213 /* If value is a string, output that string literally:
16214 don't check for % within it. */
16215 if (STRINGP (tem))
16216 literal = 1;
16217
16218 if (!EQ (tem, elt))
16219 {
16220 /* Give up right away for nil or t. */
16221 elt = tem;
16222 goto tail_recurse;
16223 }
16224 }
16225 }
16226 break;
16227
16228 case Lisp_Cons:
16229 {
16230 register Lisp_Object car, tem;
16231
16232 /* A cons cell: five distinct cases.
16233 If first element is :eval or :propertize, do something special.
16234 If first element is a string or a cons, process all the elements
16235 and effectively concatenate them.
16236 If first element is a negative number, truncate displaying cdr to
16237 at most that many characters. If positive, pad (with spaces)
16238 to at least that many characters.
16239 If first element is a symbol, process the cadr or caddr recursively
16240 according to whether the symbol's value is non-nil or nil. */
16241 car = XCAR (elt);
16242 if (EQ (car, QCeval))
16243 {
16244 /* An element of the form (:eval FORM) means evaluate FORM
16245 and use the result as mode line elements. */
16246
16247 if (risky)
16248 break;
16249
16250 if (CONSP (XCDR (elt)))
16251 {
16252 Lisp_Object spec;
16253 spec = safe_eval (XCAR (XCDR (elt)));
16254 n += display_mode_element (it, depth, field_width - n,
16255 precision - n, spec, props,
16256 risky);
16257 }
16258 }
16259 else if (EQ (car, QCpropertize))
16260 {
16261 /* An element of the form (:propertize ELT PROPS...)
16262 means display ELT but applying properties PROPS. */
16263
16264 if (risky)
16265 break;
16266
16267 if (CONSP (XCDR (elt)))
16268 n += display_mode_element (it, depth, field_width - n,
16269 precision - n, XCAR (XCDR (elt)),
16270 XCDR (XCDR (elt)), risky);
16271 }
16272 else if (SYMBOLP (car))
16273 {
16274 tem = Fboundp (car);
16275 elt = XCDR (elt);
16276 if (!CONSP (elt))
16277 goto invalid;
16278 /* elt is now the cdr, and we know it is a cons cell.
16279 Use its car if CAR has a non-nil value. */
16280 if (!NILP (tem))
16281 {
16282 tem = Fsymbol_value (car);
16283 if (!NILP (tem))
16284 {
16285 elt = XCAR (elt);
16286 goto tail_recurse;
16287 }
16288 }
16289 /* Symbol's value is nil (or symbol is unbound)
16290 Get the cddr of the original list
16291 and if possible find the caddr and use that. */
16292 elt = XCDR (elt);
16293 if (NILP (elt))
16294 break;
16295 else if (!CONSP (elt))
16296 goto invalid;
16297 elt = XCAR (elt);
16298 goto tail_recurse;
16299 }
16300 else if (INTEGERP (car))
16301 {
16302 register int lim = XINT (car);
16303 elt = XCDR (elt);
16304 if (lim < 0)
16305 {
16306 /* Negative int means reduce maximum width. */
16307 if (precision <= 0)
16308 precision = -lim;
16309 else
16310 precision = min (precision, -lim);
16311 }
16312 else if (lim > 0)
16313 {
16314 /* Padding specified. Don't let it be more than
16315 current maximum. */
16316 if (precision > 0)
16317 lim = min (precision, lim);
16318
16319 /* If that's more padding than already wanted, queue it.
16320 But don't reduce padding already specified even if
16321 that is beyond the current truncation point. */
16322 field_width = max (lim, field_width);
16323 }
16324 goto tail_recurse;
16325 }
16326 else if (STRINGP (car) || CONSP (car))
16327 {
16328 register int limit = 50;
16329 /* Limit is to protect against circular lists. */
16330 while (CONSP (elt)
16331 && --limit > 0
16332 && (precision <= 0 || n < precision))
16333 {
16334 n += display_mode_element (it, depth,
16335 /* Do padding only after the last
16336 element in the list. */
16337 (! CONSP (XCDR (elt))
16338 ? field_width - n
16339 : 0),
16340 precision - n, XCAR (elt),
16341 props, risky);
16342 elt = XCDR (elt);
16343 }
16344 }
16345 }
16346 break;
16347
16348 default:
16349 invalid:
16350 elt = build_string ("*invalid*");
16351 goto tail_recurse;
16352 }
16353
16354 /* Pad to FIELD_WIDTH. */
16355 if (field_width > 0 && n < field_width)
16356 {
16357 switch (mode_line_target)
16358 {
16359 case MODE_LINE_NOPROP:
16360 case MODE_LINE_TITLE:
16361 n += store_mode_line_noprop ("", field_width - n, 0);
16362 break;
16363 case MODE_LINE_STRING:
16364 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16365 break;
16366 case MODE_LINE_DISPLAY:
16367 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16368 0, 0, 0);
16369 break;
16370 }
16371 }
16372
16373 return n;
16374 }
16375
16376 /* Store a mode-line string element in mode_line_string_list.
16377
16378 If STRING is non-null, display that C string. Otherwise, the Lisp
16379 string LISP_STRING is displayed.
16380
16381 FIELD_WIDTH is the minimum number of output glyphs to produce.
16382 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16383 with spaces. FIELD_WIDTH <= 0 means don't pad.
16384
16385 PRECISION is the maximum number of characters to output from
16386 STRING. PRECISION <= 0 means don't truncate the string.
16387
16388 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16389 properties to the string.
16390
16391 PROPS are the properties to add to the string.
16392 The mode_line_string_face face property is always added to the string.
16393 */
16394
16395 static int
16396 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16397 char *string;
16398 Lisp_Object lisp_string;
16399 int copy_string;
16400 int field_width;
16401 int precision;
16402 Lisp_Object props;
16403 {
16404 int len;
16405 int n = 0;
16406
16407 if (string != NULL)
16408 {
16409 len = strlen (string);
16410 if (precision > 0 && len > precision)
16411 len = precision;
16412 lisp_string = make_string (string, len);
16413 if (NILP (props))
16414 props = mode_line_string_face_prop;
16415 else if (!NILP (mode_line_string_face))
16416 {
16417 Lisp_Object face = Fplist_get (props, Qface);
16418 props = Fcopy_sequence (props);
16419 if (NILP (face))
16420 face = mode_line_string_face;
16421 else
16422 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16423 props = Fplist_put (props, Qface, face);
16424 }
16425 Fadd_text_properties (make_number (0), make_number (len),
16426 props, lisp_string);
16427 }
16428 else
16429 {
16430 len = XFASTINT (Flength (lisp_string));
16431 if (precision > 0 && len > precision)
16432 {
16433 len = precision;
16434 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16435 precision = -1;
16436 }
16437 if (!NILP (mode_line_string_face))
16438 {
16439 Lisp_Object face;
16440 if (NILP (props))
16441 props = Ftext_properties_at (make_number (0), lisp_string);
16442 face = Fplist_get (props, Qface);
16443 if (NILP (face))
16444 face = mode_line_string_face;
16445 else
16446 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16447 props = Fcons (Qface, Fcons (face, Qnil));
16448 if (copy_string)
16449 lisp_string = Fcopy_sequence (lisp_string);
16450 }
16451 if (!NILP (props))
16452 Fadd_text_properties (make_number (0), make_number (len),
16453 props, lisp_string);
16454 }
16455
16456 if (len > 0)
16457 {
16458 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16459 n += len;
16460 }
16461
16462 if (field_width > len)
16463 {
16464 field_width -= len;
16465 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16466 if (!NILP (props))
16467 Fadd_text_properties (make_number (0), make_number (field_width),
16468 props, lisp_string);
16469 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16470 n += field_width;
16471 }
16472
16473 return n;
16474 }
16475
16476
16477 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16478 1, 4, 0,
16479 doc: /* Format a string out of a mode line format specification.
16480 First arg FORMAT specifies the mode line format (see `mode-line-format'
16481 for details) to use.
16482
16483 Optional second arg FACE specifies the face property to put
16484 on all characters for which no face is specified.
16485 t means whatever face the window's mode line currently uses
16486 \(either `mode-line' or `mode-line-inactive', depending).
16487 nil means the default is no face property.
16488 If FACE is an integer, the value string has no text properties.
16489
16490 Optional third and fourth args WINDOW and BUFFER specify the window
16491 and buffer to use as the context for the formatting (defaults
16492 are the selected window and the window's buffer). */)
16493 (format, face, window, buffer)
16494 Lisp_Object format, face, window, buffer;
16495 {
16496 struct it it;
16497 int len;
16498 struct window *w;
16499 struct buffer *old_buffer = NULL;
16500 int face_id = -1;
16501 int no_props = INTEGERP (face);
16502 int count = SPECPDL_INDEX ();
16503 Lisp_Object str;
16504 int string_start = 0;
16505
16506 if (NILP (window))
16507 window = selected_window;
16508 CHECK_WINDOW (window);
16509 w = XWINDOW (window);
16510
16511 if (NILP (buffer))
16512 buffer = w->buffer;
16513 CHECK_BUFFER (buffer);
16514
16515 if (NILP (format))
16516 return build_string ("");
16517
16518 if (no_props)
16519 face = Qnil;
16520
16521 if (!NILP (face))
16522 {
16523 if (EQ (face, Qt))
16524 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16525 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16526 }
16527
16528 if (face_id < 0)
16529 face_id = DEFAULT_FACE_ID;
16530
16531 if (XBUFFER (buffer) != current_buffer)
16532 old_buffer = current_buffer;
16533
16534 record_unwind_protect (unwind_format_mode_line,
16535 format_mode_line_unwind_data (old_buffer));
16536
16537 if (old_buffer)
16538 set_buffer_internal_1 (XBUFFER (buffer));
16539
16540 init_iterator (&it, w, -1, -1, NULL, face_id);
16541
16542 if (no_props)
16543 {
16544 mode_line_target = MODE_LINE_NOPROP;
16545 mode_line_string_face_prop = Qnil;
16546 mode_line_string_list = Qnil;
16547 string_start = MODE_LINE_NOPROP_LEN (0);
16548 }
16549 else
16550 {
16551 mode_line_target = MODE_LINE_STRING;
16552 mode_line_string_list = Qnil;
16553 mode_line_string_face = face;
16554 mode_line_string_face_prop
16555 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16556 }
16557
16558 push_frame_kboard (it.f);
16559 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16560 pop_frame_kboard ();
16561
16562 if (no_props)
16563 {
16564 len = MODE_LINE_NOPROP_LEN (string_start);
16565 str = make_string (mode_line_noprop_buf + string_start, len);
16566 }
16567 else
16568 {
16569 mode_line_string_list = Fnreverse (mode_line_string_list);
16570 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16571 make_string ("", 0));
16572 }
16573
16574 unbind_to (count, Qnil);
16575 return str;
16576 }
16577
16578 /* Write a null-terminated, right justified decimal representation of
16579 the positive integer D to BUF using a minimal field width WIDTH. */
16580
16581 static void
16582 pint2str (buf, width, d)
16583 register char *buf;
16584 register int width;
16585 register int d;
16586 {
16587 register char *p = buf;
16588
16589 if (d <= 0)
16590 *p++ = '0';
16591 else
16592 {
16593 while (d > 0)
16594 {
16595 *p++ = d % 10 + '0';
16596 d /= 10;
16597 }
16598 }
16599
16600 for (width -= (int) (p - buf); width > 0; --width)
16601 *p++ = ' ';
16602 *p-- = '\0';
16603 while (p > buf)
16604 {
16605 d = *buf;
16606 *buf++ = *p;
16607 *p-- = d;
16608 }
16609 }
16610
16611 /* Write a null-terminated, right justified decimal and "human
16612 readable" representation of the nonnegative integer D to BUF using
16613 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16614
16615 static const char power_letter[] =
16616 {
16617 0, /* not used */
16618 'k', /* kilo */
16619 'M', /* mega */
16620 'G', /* giga */
16621 'T', /* tera */
16622 'P', /* peta */
16623 'E', /* exa */
16624 'Z', /* zetta */
16625 'Y' /* yotta */
16626 };
16627
16628 static void
16629 pint2hrstr (buf, width, d)
16630 char *buf;
16631 int width;
16632 int d;
16633 {
16634 /* We aim to represent the nonnegative integer D as
16635 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16636 int quotient = d;
16637 int remainder = 0;
16638 /* -1 means: do not use TENTHS. */
16639 int tenths = -1;
16640 int exponent = 0;
16641
16642 /* Length of QUOTIENT.TENTHS as a string. */
16643 int length;
16644
16645 char * psuffix;
16646 char * p;
16647
16648 if (1000 <= quotient)
16649 {
16650 /* Scale to the appropriate EXPONENT. */
16651 do
16652 {
16653 remainder = quotient % 1000;
16654 quotient /= 1000;
16655 exponent++;
16656 }
16657 while (1000 <= quotient);
16658
16659 /* Round to nearest and decide whether to use TENTHS or not. */
16660 if (quotient <= 9)
16661 {
16662 tenths = remainder / 100;
16663 if (50 <= remainder % 100)
16664 {
16665 if (tenths < 9)
16666 tenths++;
16667 else
16668 {
16669 quotient++;
16670 if (quotient == 10)
16671 tenths = -1;
16672 else
16673 tenths = 0;
16674 }
16675 }
16676 }
16677 else
16678 if (500 <= remainder)
16679 {
16680 if (quotient < 999)
16681 quotient++;
16682 else
16683 {
16684 quotient = 1;
16685 exponent++;
16686 tenths = 0;
16687 }
16688 }
16689 }
16690
16691 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16692 if (tenths == -1 && quotient <= 99)
16693 if (quotient <= 9)
16694 length = 1;
16695 else
16696 length = 2;
16697 else
16698 length = 3;
16699 p = psuffix = buf + max (width, length);
16700
16701 /* Print EXPONENT. */
16702 if (exponent)
16703 *psuffix++ = power_letter[exponent];
16704 *psuffix = '\0';
16705
16706 /* Print TENTHS. */
16707 if (tenths >= 0)
16708 {
16709 *--p = '0' + tenths;
16710 *--p = '.';
16711 }
16712
16713 /* Print QUOTIENT. */
16714 do
16715 {
16716 int digit = quotient % 10;
16717 *--p = '0' + digit;
16718 }
16719 while ((quotient /= 10) != 0);
16720
16721 /* Print leading spaces. */
16722 while (buf < p)
16723 *--p = ' ';
16724 }
16725
16726 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16727 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16728 type of CODING_SYSTEM. Return updated pointer into BUF. */
16729
16730 static unsigned char invalid_eol_type[] = "(*invalid*)";
16731
16732 static char *
16733 decode_mode_spec_coding (coding_system, buf, eol_flag)
16734 Lisp_Object coding_system;
16735 register char *buf;
16736 int eol_flag;
16737 {
16738 Lisp_Object val;
16739 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16740 const unsigned char *eol_str;
16741 int eol_str_len;
16742 /* The EOL conversion we are using. */
16743 Lisp_Object eoltype;
16744
16745 val = Fget (coding_system, Qcoding_system);
16746 eoltype = Qnil;
16747
16748 if (!VECTORP (val)) /* Not yet decided. */
16749 {
16750 if (multibyte)
16751 *buf++ = '-';
16752 if (eol_flag)
16753 eoltype = eol_mnemonic_undecided;
16754 /* Don't mention EOL conversion if it isn't decided. */
16755 }
16756 else
16757 {
16758 Lisp_Object eolvalue;
16759
16760 eolvalue = Fget (coding_system, Qeol_type);
16761
16762 if (multibyte)
16763 *buf++ = XFASTINT (AREF (val, 1));
16764
16765 if (eol_flag)
16766 {
16767 /* The EOL conversion that is normal on this system. */
16768
16769 if (NILP (eolvalue)) /* Not yet decided. */
16770 eoltype = eol_mnemonic_undecided;
16771 else if (VECTORP (eolvalue)) /* Not yet decided. */
16772 eoltype = eol_mnemonic_undecided;
16773 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16774 eoltype = (XFASTINT (eolvalue) == 0
16775 ? eol_mnemonic_unix
16776 : (XFASTINT (eolvalue) == 1
16777 ? eol_mnemonic_dos : eol_mnemonic_mac));
16778 }
16779 }
16780
16781 if (eol_flag)
16782 {
16783 /* Mention the EOL conversion if it is not the usual one. */
16784 if (STRINGP (eoltype))
16785 {
16786 eol_str = SDATA (eoltype);
16787 eol_str_len = SBYTES (eoltype);
16788 }
16789 else if (INTEGERP (eoltype)
16790 && CHAR_VALID_P (XINT (eoltype), 0))
16791 {
16792 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16793 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16794 eol_str = tmp;
16795 }
16796 else
16797 {
16798 eol_str = invalid_eol_type;
16799 eol_str_len = sizeof (invalid_eol_type) - 1;
16800 }
16801 bcopy (eol_str, buf, eol_str_len);
16802 buf += eol_str_len;
16803 }
16804
16805 return buf;
16806 }
16807
16808 /* Return a string for the output of a mode line %-spec for window W,
16809 generated by character C. PRECISION >= 0 means don't return a
16810 string longer than that value. FIELD_WIDTH > 0 means pad the
16811 string returned with spaces to that value. Return 1 in *MULTIBYTE
16812 if the result is multibyte text.
16813
16814 Note we operate on the current buffer for most purposes,
16815 the exception being w->base_line_pos. */
16816
16817 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16818
16819 static char *
16820 decode_mode_spec (w, c, field_width, precision, multibyte)
16821 struct window *w;
16822 register int c;
16823 int field_width, precision;
16824 int *multibyte;
16825 {
16826 Lisp_Object obj;
16827 struct frame *f = XFRAME (WINDOW_FRAME (w));
16828 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16829 struct buffer *b = current_buffer;
16830
16831 obj = Qnil;
16832 *multibyte = 0;
16833
16834 switch (c)
16835 {
16836 case '*':
16837 if (!NILP (b->read_only))
16838 return "%";
16839 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16840 return "*";
16841 return "-";
16842
16843 case '+':
16844 /* This differs from %* only for a modified read-only buffer. */
16845 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16846 return "*";
16847 if (!NILP (b->read_only))
16848 return "%";
16849 return "-";
16850
16851 case '&':
16852 /* This differs from %* in ignoring read-only-ness. */
16853 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16854 return "*";
16855 return "-";
16856
16857 case '%':
16858 return "%";
16859
16860 case '[':
16861 {
16862 int i;
16863 char *p;
16864
16865 if (command_loop_level > 5)
16866 return "[[[... ";
16867 p = decode_mode_spec_buf;
16868 for (i = 0; i < command_loop_level; i++)
16869 *p++ = '[';
16870 *p = 0;
16871 return decode_mode_spec_buf;
16872 }
16873
16874 case ']':
16875 {
16876 int i;
16877 char *p;
16878
16879 if (command_loop_level > 5)
16880 return " ...]]]";
16881 p = decode_mode_spec_buf;
16882 for (i = 0; i < command_loop_level; i++)
16883 *p++ = ']';
16884 *p = 0;
16885 return decode_mode_spec_buf;
16886 }
16887
16888 case '-':
16889 {
16890 register int i;
16891
16892 /* Let lots_of_dashes be a string of infinite length. */
16893 if (mode_line_target == MODE_LINE_NOPROP ||
16894 mode_line_target == MODE_LINE_STRING)
16895 return "--";
16896 if (field_width <= 0
16897 || field_width > sizeof (lots_of_dashes))
16898 {
16899 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16900 decode_mode_spec_buf[i] = '-';
16901 decode_mode_spec_buf[i] = '\0';
16902 return decode_mode_spec_buf;
16903 }
16904 else
16905 return lots_of_dashes;
16906 }
16907
16908 case 'b':
16909 obj = b->name;
16910 break;
16911
16912 case 'c':
16913 {
16914 int col = (int) current_column (); /* iftc */
16915 w->column_number_displayed = make_number (col);
16916 pint2str (decode_mode_spec_buf, field_width, col);
16917 return decode_mode_spec_buf;
16918 }
16919
16920 case 'F':
16921 /* %F displays the frame name. */
16922 if (!NILP (f->title))
16923 return (char *) SDATA (f->title);
16924 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16925 return (char *) SDATA (f->name);
16926 return "Emacs";
16927
16928 case 'f':
16929 obj = b->filename;
16930 break;
16931
16932 case 'i':
16933 {
16934 int size = ZV - BEGV;
16935 pint2str (decode_mode_spec_buf, field_width, size);
16936 return decode_mode_spec_buf;
16937 }
16938
16939 case 'I':
16940 {
16941 int size = ZV - BEGV;
16942 pint2hrstr (decode_mode_spec_buf, field_width, size);
16943 return decode_mode_spec_buf;
16944 }
16945
16946 case 'l':
16947 {
16948 int startpos = XMARKER (w->start)->charpos;
16949 int startpos_byte = marker_byte_position (w->start);
16950 int line, linepos, linepos_byte, topline;
16951 int nlines, junk;
16952 int height = WINDOW_TOTAL_LINES (w);
16953
16954 /* If we decided that this buffer isn't suitable for line numbers,
16955 don't forget that too fast. */
16956 if (EQ (w->base_line_pos, w->buffer))
16957 goto no_value;
16958 /* But do forget it, if the window shows a different buffer now. */
16959 else if (BUFFERP (w->base_line_pos))
16960 w->base_line_pos = Qnil;
16961
16962 /* If the buffer is very big, don't waste time. */
16963 if (INTEGERP (Vline_number_display_limit)
16964 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16965 {
16966 w->base_line_pos = Qnil;
16967 w->base_line_number = Qnil;
16968 goto no_value;
16969 }
16970
16971 if (!NILP (w->base_line_number)
16972 && !NILP (w->base_line_pos)
16973 && XFASTINT (w->base_line_pos) <= startpos)
16974 {
16975 line = XFASTINT (w->base_line_number);
16976 linepos = XFASTINT (w->base_line_pos);
16977 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16978 }
16979 else
16980 {
16981 line = 1;
16982 linepos = BUF_BEGV (b);
16983 linepos_byte = BUF_BEGV_BYTE (b);
16984 }
16985
16986 /* Count lines from base line to window start position. */
16987 nlines = display_count_lines (linepos, linepos_byte,
16988 startpos_byte,
16989 startpos, &junk);
16990
16991 topline = nlines + line;
16992
16993 /* Determine a new base line, if the old one is too close
16994 or too far away, or if we did not have one.
16995 "Too close" means it's plausible a scroll-down would
16996 go back past it. */
16997 if (startpos == BUF_BEGV (b))
16998 {
16999 w->base_line_number = make_number (topline);
17000 w->base_line_pos = make_number (BUF_BEGV (b));
17001 }
17002 else if (nlines < height + 25 || nlines > height * 3 + 50
17003 || linepos == BUF_BEGV (b))
17004 {
17005 int limit = BUF_BEGV (b);
17006 int limit_byte = BUF_BEGV_BYTE (b);
17007 int position;
17008 int distance = (height * 2 + 30) * line_number_display_limit_width;
17009
17010 if (startpos - distance > limit)
17011 {
17012 limit = startpos - distance;
17013 limit_byte = CHAR_TO_BYTE (limit);
17014 }
17015
17016 nlines = display_count_lines (startpos, startpos_byte,
17017 limit_byte,
17018 - (height * 2 + 30),
17019 &position);
17020 /* If we couldn't find the lines we wanted within
17021 line_number_display_limit_width chars per line,
17022 give up on line numbers for this window. */
17023 if (position == limit_byte && limit == startpos - distance)
17024 {
17025 w->base_line_pos = w->buffer;
17026 w->base_line_number = Qnil;
17027 goto no_value;
17028 }
17029
17030 w->base_line_number = make_number (topline - nlines);
17031 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17032 }
17033
17034 /* Now count lines from the start pos to point. */
17035 nlines = display_count_lines (startpos, startpos_byte,
17036 PT_BYTE, PT, &junk);
17037
17038 /* Record that we did display the line number. */
17039 line_number_displayed = 1;
17040
17041 /* Make the string to show. */
17042 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17043 return decode_mode_spec_buf;
17044 no_value:
17045 {
17046 char* p = decode_mode_spec_buf;
17047 int pad = field_width - 2;
17048 while (pad-- > 0)
17049 *p++ = ' ';
17050 *p++ = '?';
17051 *p++ = '?';
17052 *p = '\0';
17053 return decode_mode_spec_buf;
17054 }
17055 }
17056 break;
17057
17058 case 'm':
17059 obj = b->mode_name;
17060 break;
17061
17062 case 'n':
17063 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17064 return " Narrow";
17065 break;
17066
17067 case 'p':
17068 {
17069 int pos = marker_position (w->start);
17070 int total = BUF_ZV (b) - BUF_BEGV (b);
17071
17072 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17073 {
17074 if (pos <= BUF_BEGV (b))
17075 return "All";
17076 else
17077 return "Bottom";
17078 }
17079 else if (pos <= BUF_BEGV (b))
17080 return "Top";
17081 else
17082 {
17083 if (total > 1000000)
17084 /* Do it differently for a large value, to avoid overflow. */
17085 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17086 else
17087 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17088 /* We can't normally display a 3-digit number,
17089 so get us a 2-digit number that is close. */
17090 if (total == 100)
17091 total = 99;
17092 sprintf (decode_mode_spec_buf, "%2d%%", total);
17093 return decode_mode_spec_buf;
17094 }
17095 }
17096
17097 /* Display percentage of size above the bottom of the screen. */
17098 case 'P':
17099 {
17100 int toppos = marker_position (w->start);
17101 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17102 int total = BUF_ZV (b) - BUF_BEGV (b);
17103
17104 if (botpos >= BUF_ZV (b))
17105 {
17106 if (toppos <= BUF_BEGV (b))
17107 return "All";
17108 else
17109 return "Bottom";
17110 }
17111 else
17112 {
17113 if (total > 1000000)
17114 /* Do it differently for a large value, to avoid overflow. */
17115 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17116 else
17117 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17118 /* We can't normally display a 3-digit number,
17119 so get us a 2-digit number that is close. */
17120 if (total == 100)
17121 total = 99;
17122 if (toppos <= BUF_BEGV (b))
17123 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17124 else
17125 sprintf (decode_mode_spec_buf, "%2d%%", total);
17126 return decode_mode_spec_buf;
17127 }
17128 }
17129
17130 case 's':
17131 /* status of process */
17132 obj = Fget_buffer_process (Fcurrent_buffer ());
17133 if (NILP (obj))
17134 return "no process";
17135 #ifdef subprocesses
17136 obj = Fsymbol_name (Fprocess_status (obj));
17137 #endif
17138 break;
17139
17140 case 't': /* indicate TEXT or BINARY */
17141 #ifdef MODE_LINE_BINARY_TEXT
17142 return MODE_LINE_BINARY_TEXT (b);
17143 #else
17144 return "T";
17145 #endif
17146
17147 case 'z':
17148 /* coding-system (not including end-of-line format) */
17149 case 'Z':
17150 /* coding-system (including end-of-line type) */
17151 {
17152 int eol_flag = (c == 'Z');
17153 char *p = decode_mode_spec_buf;
17154
17155 if (! FRAME_WINDOW_P (f))
17156 {
17157 /* No need to mention EOL here--the terminal never needs
17158 to do EOL conversion. */
17159 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17160 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17161 }
17162 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17163 p, eol_flag);
17164
17165 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17166 #ifdef subprocesses
17167 obj = Fget_buffer_process (Fcurrent_buffer ());
17168 if (PROCESSP (obj))
17169 {
17170 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17171 p, eol_flag);
17172 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17173 p, eol_flag);
17174 }
17175 #endif /* subprocesses */
17176 #endif /* 0 */
17177 *p = 0;
17178 return decode_mode_spec_buf;
17179 }
17180 }
17181
17182 if (STRINGP (obj))
17183 {
17184 *multibyte = STRING_MULTIBYTE (obj);
17185 return (char *) SDATA (obj);
17186 }
17187 else
17188 return "";
17189 }
17190
17191
17192 /* Count up to COUNT lines starting from START / START_BYTE.
17193 But don't go beyond LIMIT_BYTE.
17194 Return the number of lines thus found (always nonnegative).
17195
17196 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17197
17198 static int
17199 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17200 int start, start_byte, limit_byte, count;
17201 int *byte_pos_ptr;
17202 {
17203 register unsigned char *cursor;
17204 unsigned char *base;
17205
17206 register int ceiling;
17207 register unsigned char *ceiling_addr;
17208 int orig_count = count;
17209
17210 /* If we are not in selective display mode,
17211 check only for newlines. */
17212 int selective_display = (!NILP (current_buffer->selective_display)
17213 && !INTEGERP (current_buffer->selective_display));
17214
17215 if (count > 0)
17216 {
17217 while (start_byte < limit_byte)
17218 {
17219 ceiling = BUFFER_CEILING_OF (start_byte);
17220 ceiling = min (limit_byte - 1, ceiling);
17221 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17222 base = (cursor = BYTE_POS_ADDR (start_byte));
17223 while (1)
17224 {
17225 if (selective_display)
17226 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17227 ;
17228 else
17229 while (*cursor != '\n' && ++cursor != ceiling_addr)
17230 ;
17231
17232 if (cursor != ceiling_addr)
17233 {
17234 if (--count == 0)
17235 {
17236 start_byte += cursor - base + 1;
17237 *byte_pos_ptr = start_byte;
17238 return orig_count;
17239 }
17240 else
17241 if (++cursor == ceiling_addr)
17242 break;
17243 }
17244 else
17245 break;
17246 }
17247 start_byte += cursor - base;
17248 }
17249 }
17250 else
17251 {
17252 while (start_byte > limit_byte)
17253 {
17254 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17255 ceiling = max (limit_byte, ceiling);
17256 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17257 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17258 while (1)
17259 {
17260 if (selective_display)
17261 while (--cursor != ceiling_addr
17262 && *cursor != '\n' && *cursor != 015)
17263 ;
17264 else
17265 while (--cursor != ceiling_addr && *cursor != '\n')
17266 ;
17267
17268 if (cursor != ceiling_addr)
17269 {
17270 if (++count == 0)
17271 {
17272 start_byte += cursor - base + 1;
17273 *byte_pos_ptr = start_byte;
17274 /* When scanning backwards, we should
17275 not count the newline posterior to which we stop. */
17276 return - orig_count - 1;
17277 }
17278 }
17279 else
17280 break;
17281 }
17282 /* Here we add 1 to compensate for the last decrement
17283 of CURSOR, which took it past the valid range. */
17284 start_byte += cursor - base + 1;
17285 }
17286 }
17287
17288 *byte_pos_ptr = limit_byte;
17289
17290 if (count < 0)
17291 return - orig_count + count;
17292 return orig_count - count;
17293
17294 }
17295
17296
17297 \f
17298 /***********************************************************************
17299 Displaying strings
17300 ***********************************************************************/
17301
17302 /* Display a NUL-terminated string, starting with index START.
17303
17304 If STRING is non-null, display that C string. Otherwise, the Lisp
17305 string LISP_STRING is displayed.
17306
17307 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17308 FACE_STRING. Display STRING or LISP_STRING with the face at
17309 FACE_STRING_POS in FACE_STRING:
17310
17311 Display the string in the environment given by IT, but use the
17312 standard display table, temporarily.
17313
17314 FIELD_WIDTH is the minimum number of output glyphs to produce.
17315 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17316 with spaces. If STRING has more characters, more than FIELD_WIDTH
17317 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17318
17319 PRECISION is the maximum number of characters to output from
17320 STRING. PRECISION < 0 means don't truncate the string.
17321
17322 This is roughly equivalent to printf format specifiers:
17323
17324 FIELD_WIDTH PRECISION PRINTF
17325 ----------------------------------------
17326 -1 -1 %s
17327 -1 10 %.10s
17328 10 -1 %10s
17329 20 10 %20.10s
17330
17331 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17332 display them, and < 0 means obey the current buffer's value of
17333 enable_multibyte_characters.
17334
17335 Value is the number of glyphs produced. */
17336
17337 static int
17338 display_string (string, lisp_string, face_string, face_string_pos,
17339 start, it, field_width, precision, max_x, multibyte)
17340 unsigned char *string;
17341 Lisp_Object lisp_string;
17342 Lisp_Object face_string;
17343 int face_string_pos;
17344 int start;
17345 struct it *it;
17346 int field_width, precision, max_x;
17347 int multibyte;
17348 {
17349 int hpos_at_start = it->hpos;
17350 int saved_face_id = it->face_id;
17351 struct glyph_row *row = it->glyph_row;
17352
17353 /* Initialize the iterator IT for iteration over STRING beginning
17354 with index START. */
17355 reseat_to_string (it, string, lisp_string, start,
17356 precision, field_width, multibyte);
17357
17358 /* If displaying STRING, set up the face of the iterator
17359 from LISP_STRING, if that's given. */
17360 if (STRINGP (face_string))
17361 {
17362 int endptr;
17363 struct face *face;
17364
17365 it->face_id
17366 = face_at_string_position (it->w, face_string, face_string_pos,
17367 0, it->region_beg_charpos,
17368 it->region_end_charpos,
17369 &endptr, it->base_face_id, 0);
17370 face = FACE_FROM_ID (it->f, it->face_id);
17371 it->face_box_p = face->box != FACE_NO_BOX;
17372 }
17373
17374 /* Set max_x to the maximum allowed X position. Don't let it go
17375 beyond the right edge of the window. */
17376 if (max_x <= 0)
17377 max_x = it->last_visible_x;
17378 else
17379 max_x = min (max_x, it->last_visible_x);
17380
17381 /* Skip over display elements that are not visible. because IT->w is
17382 hscrolled. */
17383 if (it->current_x < it->first_visible_x)
17384 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17385 MOVE_TO_POS | MOVE_TO_X);
17386
17387 row->ascent = it->max_ascent;
17388 row->height = it->max_ascent + it->max_descent;
17389 row->phys_ascent = it->max_phys_ascent;
17390 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17391 row->extra_line_spacing = it->max_extra_line_spacing;
17392
17393 /* This condition is for the case that we are called with current_x
17394 past last_visible_x. */
17395 while (it->current_x < max_x)
17396 {
17397 int x_before, x, n_glyphs_before, i, nglyphs;
17398
17399 /* Get the next display element. */
17400 if (!get_next_display_element (it))
17401 break;
17402
17403 /* Produce glyphs. */
17404 x_before = it->current_x;
17405 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17406 PRODUCE_GLYPHS (it);
17407
17408 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17409 i = 0;
17410 x = x_before;
17411 while (i < nglyphs)
17412 {
17413 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17414
17415 if (!it->truncate_lines_p
17416 && x + glyph->pixel_width > max_x)
17417 {
17418 /* End of continued line or max_x reached. */
17419 if (CHAR_GLYPH_PADDING_P (*glyph))
17420 {
17421 /* A wide character is unbreakable. */
17422 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17423 it->current_x = x_before;
17424 }
17425 else
17426 {
17427 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17428 it->current_x = x;
17429 }
17430 break;
17431 }
17432 else if (x + glyph->pixel_width > it->first_visible_x)
17433 {
17434 /* Glyph is at least partially visible. */
17435 ++it->hpos;
17436 if (x < it->first_visible_x)
17437 it->glyph_row->x = x - it->first_visible_x;
17438 }
17439 else
17440 {
17441 /* Glyph is off the left margin of the display area.
17442 Should not happen. */
17443 abort ();
17444 }
17445
17446 row->ascent = max (row->ascent, it->max_ascent);
17447 row->height = max (row->height, it->max_ascent + it->max_descent);
17448 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17449 row->phys_height = max (row->phys_height,
17450 it->max_phys_ascent + it->max_phys_descent);
17451 row->extra_line_spacing = max (row->extra_line_spacing,
17452 it->max_extra_line_spacing);
17453 x += glyph->pixel_width;
17454 ++i;
17455 }
17456
17457 /* Stop if max_x reached. */
17458 if (i < nglyphs)
17459 break;
17460
17461 /* Stop at line ends. */
17462 if (ITERATOR_AT_END_OF_LINE_P (it))
17463 {
17464 it->continuation_lines_width = 0;
17465 break;
17466 }
17467
17468 set_iterator_to_next (it, 1);
17469
17470 /* Stop if truncating at the right edge. */
17471 if (it->truncate_lines_p
17472 && it->current_x >= it->last_visible_x)
17473 {
17474 /* Add truncation mark, but don't do it if the line is
17475 truncated at a padding space. */
17476 if (IT_CHARPOS (*it) < it->string_nchars)
17477 {
17478 if (!FRAME_WINDOW_P (it->f))
17479 {
17480 int i, n;
17481
17482 if (it->current_x > it->last_visible_x)
17483 {
17484 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17485 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17486 break;
17487 for (n = row->used[TEXT_AREA]; i < n; ++i)
17488 {
17489 row->used[TEXT_AREA] = i;
17490 produce_special_glyphs (it, IT_TRUNCATION);
17491 }
17492 }
17493 produce_special_glyphs (it, IT_TRUNCATION);
17494 }
17495 it->glyph_row->truncated_on_right_p = 1;
17496 }
17497 break;
17498 }
17499 }
17500
17501 /* Maybe insert a truncation at the left. */
17502 if (it->first_visible_x
17503 && IT_CHARPOS (*it) > 0)
17504 {
17505 if (!FRAME_WINDOW_P (it->f))
17506 insert_left_trunc_glyphs (it);
17507 it->glyph_row->truncated_on_left_p = 1;
17508 }
17509
17510 it->face_id = saved_face_id;
17511
17512 /* Value is number of columns displayed. */
17513 return it->hpos - hpos_at_start;
17514 }
17515
17516
17517 \f
17518 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17519 appears as an element of LIST or as the car of an element of LIST.
17520 If PROPVAL is a list, compare each element against LIST in that
17521 way, and return 1/2 if any element of PROPVAL is found in LIST.
17522 Otherwise return 0. This function cannot quit.
17523 The return value is 2 if the text is invisible but with an ellipsis
17524 and 1 if it's invisible and without an ellipsis. */
17525
17526 int
17527 invisible_p (propval, list)
17528 register Lisp_Object propval;
17529 Lisp_Object list;
17530 {
17531 register Lisp_Object tail, proptail;
17532
17533 for (tail = list; CONSP (tail); tail = XCDR (tail))
17534 {
17535 register Lisp_Object tem;
17536 tem = XCAR (tail);
17537 if (EQ (propval, tem))
17538 return 1;
17539 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17540 return NILP (XCDR (tem)) ? 1 : 2;
17541 }
17542
17543 if (CONSP (propval))
17544 {
17545 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17546 {
17547 Lisp_Object propelt;
17548 propelt = XCAR (proptail);
17549 for (tail = list; CONSP (tail); tail = XCDR (tail))
17550 {
17551 register Lisp_Object tem;
17552 tem = XCAR (tail);
17553 if (EQ (propelt, tem))
17554 return 1;
17555 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17556 return NILP (XCDR (tem)) ? 1 : 2;
17557 }
17558 }
17559 }
17560
17561 return 0;
17562 }
17563
17564 /* Calculate a width or height in pixels from a specification using
17565 the following elements:
17566
17567 SPEC ::=
17568 NUM - a (fractional) multiple of the default font width/height
17569 (NUM) - specifies exactly NUM pixels
17570 UNIT - a fixed number of pixels, see below.
17571 ELEMENT - size of a display element in pixels, see below.
17572 (NUM . SPEC) - equals NUM * SPEC
17573 (+ SPEC SPEC ...) - add pixel values
17574 (- SPEC SPEC ...) - subtract pixel values
17575 (- SPEC) - negate pixel value
17576
17577 NUM ::=
17578 INT or FLOAT - a number constant
17579 SYMBOL - use symbol's (buffer local) variable binding.
17580
17581 UNIT ::=
17582 in - pixels per inch *)
17583 mm - pixels per 1/1000 meter *)
17584 cm - pixels per 1/100 meter *)
17585 width - width of current font in pixels.
17586 height - height of current font in pixels.
17587
17588 *) using the ratio(s) defined in display-pixels-per-inch.
17589
17590 ELEMENT ::=
17591
17592 left-fringe - left fringe width in pixels
17593 right-fringe - right fringe width in pixels
17594
17595 left-margin - left margin width in pixels
17596 right-margin - right margin width in pixels
17597
17598 scroll-bar - scroll-bar area width in pixels
17599
17600 Examples:
17601
17602 Pixels corresponding to 5 inches:
17603 (5 . in)
17604
17605 Total width of non-text areas on left side of window (if scroll-bar is on left):
17606 '(space :width (+ left-fringe left-margin scroll-bar))
17607
17608 Align to first text column (in header line):
17609 '(space :align-to 0)
17610
17611 Align to middle of text area minus half the width of variable `my-image'
17612 containing a loaded image:
17613 '(space :align-to (0.5 . (- text my-image)))
17614
17615 Width of left margin minus width of 1 character in the default font:
17616 '(space :width (- left-margin 1))
17617
17618 Width of left margin minus width of 2 characters in the current font:
17619 '(space :width (- left-margin (2 . width)))
17620
17621 Center 1 character over left-margin (in header line):
17622 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17623
17624 Different ways to express width of left fringe plus left margin minus one pixel:
17625 '(space :width (- (+ left-fringe left-margin) (1)))
17626 '(space :width (+ left-fringe left-margin (- (1))))
17627 '(space :width (+ left-fringe left-margin (-1)))
17628
17629 */
17630
17631 #define NUMVAL(X) \
17632 ((INTEGERP (X) || FLOATP (X)) \
17633 ? XFLOATINT (X) \
17634 : - 1)
17635
17636 int
17637 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17638 double *res;
17639 struct it *it;
17640 Lisp_Object prop;
17641 void *font;
17642 int width_p, *align_to;
17643 {
17644 double pixels;
17645
17646 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17647 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17648
17649 if (NILP (prop))
17650 return OK_PIXELS (0);
17651
17652 if (SYMBOLP (prop))
17653 {
17654 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17655 {
17656 char *unit = SDATA (SYMBOL_NAME (prop));
17657
17658 if (unit[0] == 'i' && unit[1] == 'n')
17659 pixels = 1.0;
17660 else if (unit[0] == 'm' && unit[1] == 'm')
17661 pixels = 25.4;
17662 else if (unit[0] == 'c' && unit[1] == 'm')
17663 pixels = 2.54;
17664 else
17665 pixels = 0;
17666 if (pixels > 0)
17667 {
17668 double ppi;
17669 #ifdef HAVE_WINDOW_SYSTEM
17670 if (FRAME_WINDOW_P (it->f)
17671 && (ppi = (width_p
17672 ? FRAME_X_DISPLAY_INFO (it->f)->resx
17673 : FRAME_X_DISPLAY_INFO (it->f)->resy),
17674 ppi > 0))
17675 return OK_PIXELS (ppi / pixels);
17676 #endif
17677
17678 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17679 || (CONSP (Vdisplay_pixels_per_inch)
17680 && (ppi = (width_p
17681 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17682 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17683 ppi > 0)))
17684 return OK_PIXELS (ppi / pixels);
17685
17686 return 0;
17687 }
17688 }
17689
17690 #ifdef HAVE_WINDOW_SYSTEM
17691 if (EQ (prop, Qheight))
17692 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17693 if (EQ (prop, Qwidth))
17694 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17695 #else
17696 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17697 return OK_PIXELS (1);
17698 #endif
17699
17700 if (EQ (prop, Qtext))
17701 return OK_PIXELS (width_p
17702 ? window_box_width (it->w, TEXT_AREA)
17703 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17704
17705 if (align_to && *align_to < 0)
17706 {
17707 *res = 0;
17708 if (EQ (prop, Qleft))
17709 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17710 if (EQ (prop, Qright))
17711 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17712 if (EQ (prop, Qcenter))
17713 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17714 + window_box_width (it->w, TEXT_AREA) / 2);
17715 if (EQ (prop, Qleft_fringe))
17716 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17717 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17718 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17719 if (EQ (prop, Qright_fringe))
17720 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17721 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17722 : window_box_right_offset (it->w, TEXT_AREA));
17723 if (EQ (prop, Qleft_margin))
17724 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17725 if (EQ (prop, Qright_margin))
17726 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17727 if (EQ (prop, Qscroll_bar))
17728 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17729 ? 0
17730 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17731 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17732 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17733 : 0)));
17734 }
17735 else
17736 {
17737 if (EQ (prop, Qleft_fringe))
17738 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17739 if (EQ (prop, Qright_fringe))
17740 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17741 if (EQ (prop, Qleft_margin))
17742 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17743 if (EQ (prop, Qright_margin))
17744 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17745 if (EQ (prop, Qscroll_bar))
17746 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17747 }
17748
17749 prop = Fbuffer_local_value (prop, it->w->buffer);
17750 }
17751
17752 if (INTEGERP (prop) || FLOATP (prop))
17753 {
17754 int base_unit = (width_p
17755 ? FRAME_COLUMN_WIDTH (it->f)
17756 : FRAME_LINE_HEIGHT (it->f));
17757 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17758 }
17759
17760 if (CONSP (prop))
17761 {
17762 Lisp_Object car = XCAR (prop);
17763 Lisp_Object cdr = XCDR (prop);
17764
17765 if (SYMBOLP (car))
17766 {
17767 #ifdef HAVE_WINDOW_SYSTEM
17768 if (valid_image_p (prop))
17769 {
17770 int id = lookup_image (it->f, prop);
17771 struct image *img = IMAGE_FROM_ID (it->f, id);
17772
17773 return OK_PIXELS (width_p ? img->width : img->height);
17774 }
17775 #endif
17776 if (EQ (car, Qplus) || EQ (car, Qminus))
17777 {
17778 int first = 1;
17779 double px;
17780
17781 pixels = 0;
17782 while (CONSP (cdr))
17783 {
17784 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17785 font, width_p, align_to))
17786 return 0;
17787 if (first)
17788 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17789 else
17790 pixels += px;
17791 cdr = XCDR (cdr);
17792 }
17793 if (EQ (car, Qminus))
17794 pixels = -pixels;
17795 return OK_PIXELS (pixels);
17796 }
17797
17798 car = Fbuffer_local_value (car, it->w->buffer);
17799 }
17800
17801 if (INTEGERP (car) || FLOATP (car))
17802 {
17803 double fact;
17804 pixels = XFLOATINT (car);
17805 if (NILP (cdr))
17806 return OK_PIXELS (pixels);
17807 if (calc_pixel_width_or_height (&fact, it, cdr,
17808 font, width_p, align_to))
17809 return OK_PIXELS (pixels * fact);
17810 return 0;
17811 }
17812
17813 return 0;
17814 }
17815
17816 return 0;
17817 }
17818
17819 \f
17820 /***********************************************************************
17821 Glyph Display
17822 ***********************************************************************/
17823
17824 #ifdef HAVE_WINDOW_SYSTEM
17825
17826 #if GLYPH_DEBUG
17827
17828 void
17829 dump_glyph_string (s)
17830 struct glyph_string *s;
17831 {
17832 fprintf (stderr, "glyph string\n");
17833 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17834 s->x, s->y, s->width, s->height);
17835 fprintf (stderr, " ybase = %d\n", s->ybase);
17836 fprintf (stderr, " hl = %d\n", s->hl);
17837 fprintf (stderr, " left overhang = %d, right = %d\n",
17838 s->left_overhang, s->right_overhang);
17839 fprintf (stderr, " nchars = %d\n", s->nchars);
17840 fprintf (stderr, " extends to end of line = %d\n",
17841 s->extends_to_end_of_line_p);
17842 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17843 fprintf (stderr, " bg width = %d\n", s->background_width);
17844 }
17845
17846 #endif /* GLYPH_DEBUG */
17847
17848 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17849 of XChar2b structures for S; it can't be allocated in
17850 init_glyph_string because it must be allocated via `alloca'. W
17851 is the window on which S is drawn. ROW and AREA are the glyph row
17852 and area within the row from which S is constructed. START is the
17853 index of the first glyph structure covered by S. HL is a
17854 face-override for drawing S. */
17855
17856 #ifdef HAVE_NTGUI
17857 #define OPTIONAL_HDC(hdc) hdc,
17858 #define DECLARE_HDC(hdc) HDC hdc;
17859 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17860 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17861 #endif
17862
17863 #ifndef OPTIONAL_HDC
17864 #define OPTIONAL_HDC(hdc)
17865 #define DECLARE_HDC(hdc)
17866 #define ALLOCATE_HDC(hdc, f)
17867 #define RELEASE_HDC(hdc, f)
17868 #endif
17869
17870 static void
17871 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17872 struct glyph_string *s;
17873 DECLARE_HDC (hdc)
17874 XChar2b *char2b;
17875 struct window *w;
17876 struct glyph_row *row;
17877 enum glyph_row_area area;
17878 int start;
17879 enum draw_glyphs_face hl;
17880 {
17881 bzero (s, sizeof *s);
17882 s->w = w;
17883 s->f = XFRAME (w->frame);
17884 #ifdef HAVE_NTGUI
17885 s->hdc = hdc;
17886 #endif
17887 s->display = FRAME_X_DISPLAY (s->f);
17888 s->window = FRAME_X_WINDOW (s->f);
17889 s->char2b = char2b;
17890 s->hl = hl;
17891 s->row = row;
17892 s->area = area;
17893 s->first_glyph = row->glyphs[area] + start;
17894 s->height = row->height;
17895 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17896
17897 /* Display the internal border below the tool-bar window. */
17898 if (WINDOWP (s->f->tool_bar_window)
17899 && s->w == XWINDOW (s->f->tool_bar_window))
17900 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17901
17902 s->ybase = s->y + row->ascent;
17903 }
17904
17905
17906 /* Append the list of glyph strings with head H and tail T to the list
17907 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17908
17909 static INLINE void
17910 append_glyph_string_lists (head, tail, h, t)
17911 struct glyph_string **head, **tail;
17912 struct glyph_string *h, *t;
17913 {
17914 if (h)
17915 {
17916 if (*head)
17917 (*tail)->next = h;
17918 else
17919 *head = h;
17920 h->prev = *tail;
17921 *tail = t;
17922 }
17923 }
17924
17925
17926 /* Prepend the list of glyph strings with head H and tail T to the
17927 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17928 result. */
17929
17930 static INLINE void
17931 prepend_glyph_string_lists (head, tail, h, t)
17932 struct glyph_string **head, **tail;
17933 struct glyph_string *h, *t;
17934 {
17935 if (h)
17936 {
17937 if (*head)
17938 (*head)->prev = t;
17939 else
17940 *tail = t;
17941 t->next = *head;
17942 *head = h;
17943 }
17944 }
17945
17946
17947 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17948 Set *HEAD and *TAIL to the resulting list. */
17949
17950 static INLINE void
17951 append_glyph_string (head, tail, s)
17952 struct glyph_string **head, **tail;
17953 struct glyph_string *s;
17954 {
17955 s->next = s->prev = NULL;
17956 append_glyph_string_lists (head, tail, s, s);
17957 }
17958
17959
17960 /* Get face and two-byte form of character glyph GLYPH on frame F.
17961 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17962 a pointer to a realized face that is ready for display. */
17963
17964 static INLINE struct face *
17965 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17966 struct frame *f;
17967 struct glyph *glyph;
17968 XChar2b *char2b;
17969 int *two_byte_p;
17970 {
17971 struct face *face;
17972
17973 xassert (glyph->type == CHAR_GLYPH);
17974 face = FACE_FROM_ID (f, glyph->face_id);
17975
17976 if (two_byte_p)
17977 *two_byte_p = 0;
17978
17979 if (!glyph->multibyte_p)
17980 {
17981 /* Unibyte case. We don't have to encode, but we have to make
17982 sure to use a face suitable for unibyte. */
17983 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17984 }
17985 else if (glyph->u.ch < 128
17986 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17987 {
17988 /* Case of ASCII in a face known to fit ASCII. */
17989 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17990 }
17991 else
17992 {
17993 int c1, c2, charset;
17994
17995 /* Split characters into bytes. If c2 is -1 afterwards, C is
17996 really a one-byte character so that byte1 is zero. */
17997 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17998 if (c2 > 0)
17999 STORE_XCHAR2B (char2b, c1, c2);
18000 else
18001 STORE_XCHAR2B (char2b, 0, c1);
18002
18003 /* Maybe encode the character in *CHAR2B. */
18004 if (charset != CHARSET_ASCII)
18005 {
18006 struct font_info *font_info
18007 = FONT_INFO_FROM_ID (f, face->font_info_id);
18008 if (font_info)
18009 glyph->font_type
18010 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18011 }
18012 }
18013
18014 /* Make sure X resources of the face are allocated. */
18015 xassert (face != NULL);
18016 PREPARE_FACE_FOR_DISPLAY (f, face);
18017 return face;
18018 }
18019
18020
18021 /* Fill glyph string S with composition components specified by S->cmp.
18022
18023 FACES is an array of faces for all components of this composition.
18024 S->gidx is the index of the first component for S.
18025 OVERLAPS_P non-zero means S should draw the foreground only, and
18026 use its physical height for clipping.
18027
18028 Value is the index of a component not in S. */
18029
18030 static int
18031 fill_composite_glyph_string (s, faces, overlaps_p)
18032 struct glyph_string *s;
18033 struct face **faces;
18034 int overlaps_p;
18035 {
18036 int i;
18037
18038 xassert (s);
18039
18040 s->for_overlaps_p = overlaps_p;
18041
18042 s->face = faces[s->gidx];
18043 s->font = s->face->font;
18044 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18045
18046 /* For all glyphs of this composition, starting at the offset
18047 S->gidx, until we reach the end of the definition or encounter a
18048 glyph that requires the different face, add it to S. */
18049 ++s->nchars;
18050 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18051 ++s->nchars;
18052
18053 /* All glyph strings for the same composition has the same width,
18054 i.e. the width set for the first component of the composition. */
18055
18056 s->width = s->first_glyph->pixel_width;
18057
18058 /* If the specified font could not be loaded, use the frame's
18059 default font, but record the fact that we couldn't load it in
18060 the glyph string so that we can draw rectangles for the
18061 characters of the glyph string. */
18062 if (s->font == NULL)
18063 {
18064 s->font_not_found_p = 1;
18065 s->font = FRAME_FONT (s->f);
18066 }
18067
18068 /* Adjust base line for subscript/superscript text. */
18069 s->ybase += s->first_glyph->voffset;
18070
18071 xassert (s->face && s->face->gc);
18072
18073 /* This glyph string must always be drawn with 16-bit functions. */
18074 s->two_byte_p = 1;
18075
18076 return s->gidx + s->nchars;
18077 }
18078
18079
18080 /* Fill glyph string S from a sequence of character glyphs.
18081
18082 FACE_ID is the face id of the string. START is the index of the
18083 first glyph to consider, END is the index of the last + 1.
18084 OVERLAPS_P non-zero means S should draw the foreground only, and
18085 use its physical height for clipping.
18086
18087 Value is the index of the first glyph not in S. */
18088
18089 static int
18090 fill_glyph_string (s, face_id, start, end, overlaps_p)
18091 struct glyph_string *s;
18092 int face_id;
18093 int start, end, overlaps_p;
18094 {
18095 struct glyph *glyph, *last;
18096 int voffset;
18097 int glyph_not_available_p;
18098
18099 xassert (s->f == XFRAME (s->w->frame));
18100 xassert (s->nchars == 0);
18101 xassert (start >= 0 && end > start);
18102
18103 s->for_overlaps_p = overlaps_p,
18104 glyph = s->row->glyphs[s->area] + start;
18105 last = s->row->glyphs[s->area] + end;
18106 voffset = glyph->voffset;
18107
18108 glyph_not_available_p = glyph->glyph_not_available_p;
18109
18110 while (glyph < last
18111 && glyph->type == CHAR_GLYPH
18112 && glyph->voffset == voffset
18113 /* Same face id implies same font, nowadays. */
18114 && glyph->face_id == face_id
18115 && glyph->glyph_not_available_p == glyph_not_available_p)
18116 {
18117 int two_byte_p;
18118
18119 s->face = get_glyph_face_and_encoding (s->f, glyph,
18120 s->char2b + s->nchars,
18121 &two_byte_p);
18122 s->two_byte_p = two_byte_p;
18123 ++s->nchars;
18124 xassert (s->nchars <= end - start);
18125 s->width += glyph->pixel_width;
18126 ++glyph;
18127 }
18128
18129 s->font = s->face->font;
18130 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18131
18132 /* If the specified font could not be loaded, use the frame's font,
18133 but record the fact that we couldn't load it in
18134 S->font_not_found_p so that we can draw rectangles for the
18135 characters of the glyph string. */
18136 if (s->font == NULL || glyph_not_available_p)
18137 {
18138 s->font_not_found_p = 1;
18139 s->font = FRAME_FONT (s->f);
18140 }
18141
18142 /* Adjust base line for subscript/superscript text. */
18143 s->ybase += voffset;
18144
18145 xassert (s->face && s->face->gc);
18146 return glyph - s->row->glyphs[s->area];
18147 }
18148
18149
18150 /* Fill glyph string S from image glyph S->first_glyph. */
18151
18152 static void
18153 fill_image_glyph_string (s)
18154 struct glyph_string *s;
18155 {
18156 xassert (s->first_glyph->type == IMAGE_GLYPH);
18157 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18158 xassert (s->img);
18159 s->slice = s->first_glyph->slice;
18160 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18161 s->font = s->face->font;
18162 s->width = s->first_glyph->pixel_width;
18163
18164 /* Adjust base line for subscript/superscript text. */
18165 s->ybase += s->first_glyph->voffset;
18166 }
18167
18168
18169 /* Fill glyph string S from a sequence of stretch glyphs.
18170
18171 ROW is the glyph row in which the glyphs are found, AREA is the
18172 area within the row. START is the index of the first glyph to
18173 consider, END is the index of the last + 1.
18174
18175 Value is the index of the first glyph not in S. */
18176
18177 static int
18178 fill_stretch_glyph_string (s, row, area, start, end)
18179 struct glyph_string *s;
18180 struct glyph_row *row;
18181 enum glyph_row_area area;
18182 int start, end;
18183 {
18184 struct glyph *glyph, *last;
18185 int voffset, face_id;
18186
18187 xassert (s->first_glyph->type == STRETCH_GLYPH);
18188
18189 glyph = s->row->glyphs[s->area] + start;
18190 last = s->row->glyphs[s->area] + end;
18191 face_id = glyph->face_id;
18192 s->face = FACE_FROM_ID (s->f, face_id);
18193 s->font = s->face->font;
18194 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18195 s->width = glyph->pixel_width;
18196 voffset = glyph->voffset;
18197
18198 for (++glyph;
18199 (glyph < last
18200 && glyph->type == STRETCH_GLYPH
18201 && glyph->voffset == voffset
18202 && glyph->face_id == face_id);
18203 ++glyph)
18204 s->width += glyph->pixel_width;
18205
18206 /* Adjust base line for subscript/superscript text. */
18207 s->ybase += voffset;
18208
18209 /* The case that face->gc == 0 is handled when drawing the glyph
18210 string by calling PREPARE_FACE_FOR_DISPLAY. */
18211 xassert (s->face);
18212 return glyph - s->row->glyphs[s->area];
18213 }
18214
18215
18216 /* EXPORT for RIF:
18217 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18218 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18219 assumed to be zero. */
18220
18221 void
18222 x_get_glyph_overhangs (glyph, f, left, right)
18223 struct glyph *glyph;
18224 struct frame *f;
18225 int *left, *right;
18226 {
18227 *left = *right = 0;
18228
18229 if (glyph->type == CHAR_GLYPH)
18230 {
18231 XFontStruct *font;
18232 struct face *face;
18233 struct font_info *font_info;
18234 XChar2b char2b;
18235 XCharStruct *pcm;
18236
18237 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18238 font = face->font;
18239 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18240 if (font /* ++KFS: Should this be font_info ? */
18241 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18242 {
18243 if (pcm->rbearing > pcm->width)
18244 *right = pcm->rbearing - pcm->width;
18245 if (pcm->lbearing < 0)
18246 *left = -pcm->lbearing;
18247 }
18248 }
18249 }
18250
18251
18252 /* Return the index of the first glyph preceding glyph string S that
18253 is overwritten by S because of S's left overhang. Value is -1
18254 if no glyphs are overwritten. */
18255
18256 static int
18257 left_overwritten (s)
18258 struct glyph_string *s;
18259 {
18260 int k;
18261
18262 if (s->left_overhang)
18263 {
18264 int x = 0, i;
18265 struct glyph *glyphs = s->row->glyphs[s->area];
18266 int first = s->first_glyph - glyphs;
18267
18268 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18269 x -= glyphs[i].pixel_width;
18270
18271 k = i + 1;
18272 }
18273 else
18274 k = -1;
18275
18276 return k;
18277 }
18278
18279
18280 /* Return the index of the first glyph preceding glyph string S that
18281 is overwriting S because of its right overhang. Value is -1 if no
18282 glyph in front of S overwrites S. */
18283
18284 static int
18285 left_overwriting (s)
18286 struct glyph_string *s;
18287 {
18288 int i, k, x;
18289 struct glyph *glyphs = s->row->glyphs[s->area];
18290 int first = s->first_glyph - glyphs;
18291
18292 k = -1;
18293 x = 0;
18294 for (i = first - 1; i >= 0; --i)
18295 {
18296 int left, right;
18297 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18298 if (x + right > 0)
18299 k = i;
18300 x -= glyphs[i].pixel_width;
18301 }
18302
18303 return k;
18304 }
18305
18306
18307 /* Return the index of the last glyph following glyph string S that is
18308 not overwritten by S because of S's right overhang. Value is -1 if
18309 no such glyph is found. */
18310
18311 static int
18312 right_overwritten (s)
18313 struct glyph_string *s;
18314 {
18315 int k = -1;
18316
18317 if (s->right_overhang)
18318 {
18319 int x = 0, i;
18320 struct glyph *glyphs = s->row->glyphs[s->area];
18321 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18322 int end = s->row->used[s->area];
18323
18324 for (i = first; i < end && s->right_overhang > x; ++i)
18325 x += glyphs[i].pixel_width;
18326
18327 k = i;
18328 }
18329
18330 return k;
18331 }
18332
18333
18334 /* Return the index of the last glyph following glyph string S that
18335 overwrites S because of its left overhang. Value is negative
18336 if no such glyph is found. */
18337
18338 static int
18339 right_overwriting (s)
18340 struct glyph_string *s;
18341 {
18342 int i, k, x;
18343 int end = s->row->used[s->area];
18344 struct glyph *glyphs = s->row->glyphs[s->area];
18345 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18346
18347 k = -1;
18348 x = 0;
18349 for (i = first; i < end; ++i)
18350 {
18351 int left, right;
18352 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18353 if (x - left < 0)
18354 k = i;
18355 x += glyphs[i].pixel_width;
18356 }
18357
18358 return k;
18359 }
18360
18361
18362 /* Get face and two-byte form of character C in face FACE_ID on frame
18363 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18364 means we want to display multibyte text. DISPLAY_P non-zero means
18365 make sure that X resources for the face returned are allocated.
18366 Value is a pointer to a realized face that is ready for display if
18367 DISPLAY_P is non-zero. */
18368
18369 static INLINE struct face *
18370 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18371 struct frame *f;
18372 int c, face_id;
18373 XChar2b *char2b;
18374 int multibyte_p, display_p;
18375 {
18376 struct face *face = FACE_FROM_ID (f, face_id);
18377
18378 if (!multibyte_p)
18379 {
18380 /* Unibyte case. We don't have to encode, but we have to make
18381 sure to use a face suitable for unibyte. */
18382 STORE_XCHAR2B (char2b, 0, c);
18383 face_id = FACE_FOR_CHAR (f, face, c);
18384 face = FACE_FROM_ID (f, face_id);
18385 }
18386 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18387 {
18388 /* Case of ASCII in a face known to fit ASCII. */
18389 STORE_XCHAR2B (char2b, 0, c);
18390 }
18391 else
18392 {
18393 int c1, c2, charset;
18394
18395 /* Split characters into bytes. If c2 is -1 afterwards, C is
18396 really a one-byte character so that byte1 is zero. */
18397 SPLIT_CHAR (c, charset, c1, c2);
18398 if (c2 > 0)
18399 STORE_XCHAR2B (char2b, c1, c2);
18400 else
18401 STORE_XCHAR2B (char2b, 0, c1);
18402
18403 /* Maybe encode the character in *CHAR2B. */
18404 if (face->font != NULL)
18405 {
18406 struct font_info *font_info
18407 = FONT_INFO_FROM_ID (f, face->font_info_id);
18408 if (font_info)
18409 rif->encode_char (c, char2b, font_info, 0);
18410 }
18411 }
18412
18413 /* Make sure X resources of the face are allocated. */
18414 #ifdef HAVE_X_WINDOWS
18415 if (display_p)
18416 #endif
18417 {
18418 xassert (face != NULL);
18419 PREPARE_FACE_FOR_DISPLAY (f, face);
18420 }
18421
18422 return face;
18423 }
18424
18425
18426 /* Set background width of glyph string S. START is the index of the
18427 first glyph following S. LAST_X is the right-most x-position + 1
18428 in the drawing area. */
18429
18430 static INLINE void
18431 set_glyph_string_background_width (s, start, last_x)
18432 struct glyph_string *s;
18433 int start;
18434 int last_x;
18435 {
18436 /* If the face of this glyph string has to be drawn to the end of
18437 the drawing area, set S->extends_to_end_of_line_p. */
18438 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18439
18440 if (start == s->row->used[s->area]
18441 && s->area == TEXT_AREA
18442 && ((s->hl == DRAW_NORMAL_TEXT
18443 && (s->row->fill_line_p
18444 || s->face->background != default_face->background
18445 || s->face->stipple != default_face->stipple
18446 || s->row->mouse_face_p))
18447 || s->hl == DRAW_MOUSE_FACE
18448 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18449 && s->row->fill_line_p)))
18450 s->extends_to_end_of_line_p = 1;
18451
18452 /* If S extends its face to the end of the line, set its
18453 background_width to the distance to the right edge of the drawing
18454 area. */
18455 if (s->extends_to_end_of_line_p)
18456 s->background_width = last_x - s->x + 1;
18457 else
18458 s->background_width = s->width;
18459 }
18460
18461
18462 /* Compute overhangs and x-positions for glyph string S and its
18463 predecessors, or successors. X is the starting x-position for S.
18464 BACKWARD_P non-zero means process predecessors. */
18465
18466 static void
18467 compute_overhangs_and_x (s, x, backward_p)
18468 struct glyph_string *s;
18469 int x;
18470 int backward_p;
18471 {
18472 if (backward_p)
18473 {
18474 while (s)
18475 {
18476 if (rif->compute_glyph_string_overhangs)
18477 rif->compute_glyph_string_overhangs (s);
18478 x -= s->width;
18479 s->x = x;
18480 s = s->prev;
18481 }
18482 }
18483 else
18484 {
18485 while (s)
18486 {
18487 if (rif->compute_glyph_string_overhangs)
18488 rif->compute_glyph_string_overhangs (s);
18489 s->x = x;
18490 x += s->width;
18491 s = s->next;
18492 }
18493 }
18494 }
18495
18496
18497
18498 /* The following macros are only called from draw_glyphs below.
18499 They reference the following parameters of that function directly:
18500 `w', `row', `area', and `overlap_p'
18501 as well as the following local variables:
18502 `s', `f', and `hdc' (in W32) */
18503
18504 #ifdef HAVE_NTGUI
18505 /* On W32, silently add local `hdc' variable to argument list of
18506 init_glyph_string. */
18507 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18508 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18509 #else
18510 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18511 init_glyph_string (s, char2b, w, row, area, start, hl)
18512 #endif
18513
18514 /* Add a glyph string for a stretch glyph to the list of strings
18515 between HEAD and TAIL. START is the index of the stretch glyph in
18516 row area AREA of glyph row ROW. END is the index of the last glyph
18517 in that glyph row area. X is the current output position assigned
18518 to the new glyph string constructed. HL overrides that face of the
18519 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18520 is the right-most x-position of the drawing area. */
18521
18522 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18523 and below -- keep them on one line. */
18524 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18525 do \
18526 { \
18527 s = (struct glyph_string *) alloca (sizeof *s); \
18528 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18529 START = fill_stretch_glyph_string (s, row, area, START, END); \
18530 append_glyph_string (&HEAD, &TAIL, s); \
18531 s->x = (X); \
18532 } \
18533 while (0)
18534
18535
18536 /* Add a glyph string for an image glyph to the list of strings
18537 between HEAD and TAIL. START is the index of the image glyph in
18538 row area AREA of glyph row ROW. END is the index of the last glyph
18539 in that glyph row area. X is the current output position assigned
18540 to the new glyph string constructed. HL overrides that face of the
18541 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18542 is the right-most x-position of the drawing area. */
18543
18544 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18545 do \
18546 { \
18547 s = (struct glyph_string *) alloca (sizeof *s); \
18548 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18549 fill_image_glyph_string (s); \
18550 append_glyph_string (&HEAD, &TAIL, s); \
18551 ++START; \
18552 s->x = (X); \
18553 } \
18554 while (0)
18555
18556
18557 /* Add a glyph string for a sequence of character glyphs to the list
18558 of strings between HEAD and TAIL. START is the index of the first
18559 glyph in row area AREA of glyph row ROW that is part of the new
18560 glyph string. END is the index of the last glyph in that glyph row
18561 area. X is the current output position assigned to the new glyph
18562 string constructed. HL overrides that face of the glyph; e.g. it
18563 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18564 right-most x-position of the drawing area. */
18565
18566 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18567 do \
18568 { \
18569 int c, face_id; \
18570 XChar2b *char2b; \
18571 \
18572 c = (row)->glyphs[area][START].u.ch; \
18573 face_id = (row)->glyphs[area][START].face_id; \
18574 \
18575 s = (struct glyph_string *) alloca (sizeof *s); \
18576 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18577 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18578 append_glyph_string (&HEAD, &TAIL, s); \
18579 s->x = (X); \
18580 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18581 } \
18582 while (0)
18583
18584
18585 /* Add a glyph string for a composite sequence to the list of strings
18586 between HEAD and TAIL. START is the index of the first glyph in
18587 row area AREA of glyph row ROW that is part of the new glyph
18588 string. END is the index of the last glyph in that glyph row area.
18589 X is the current output position assigned to the new glyph string
18590 constructed. HL overrides that face of the glyph; e.g. it is
18591 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18592 x-position of the drawing area. */
18593
18594 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18595 do { \
18596 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18597 int face_id = (row)->glyphs[area][START].face_id; \
18598 struct face *base_face = FACE_FROM_ID (f, face_id); \
18599 struct composition *cmp = composition_table[cmp_id]; \
18600 int glyph_len = cmp->glyph_len; \
18601 XChar2b *char2b; \
18602 struct face **faces; \
18603 struct glyph_string *first_s = NULL; \
18604 int n; \
18605 \
18606 base_face = base_face->ascii_face; \
18607 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18608 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18609 /* At first, fill in `char2b' and `faces'. */ \
18610 for (n = 0; n < glyph_len; n++) \
18611 { \
18612 int c = COMPOSITION_GLYPH (cmp, n); \
18613 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18614 faces[n] = FACE_FROM_ID (f, this_face_id); \
18615 get_char_face_and_encoding (f, c, this_face_id, \
18616 char2b + n, 1, 1); \
18617 } \
18618 \
18619 /* Make glyph_strings for each glyph sequence that is drawable by \
18620 the same face, and append them to HEAD/TAIL. */ \
18621 for (n = 0; n < cmp->glyph_len;) \
18622 { \
18623 s = (struct glyph_string *) alloca (sizeof *s); \
18624 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18625 append_glyph_string (&(HEAD), &(TAIL), s); \
18626 s->cmp = cmp; \
18627 s->gidx = n; \
18628 s->x = (X); \
18629 \
18630 if (n == 0) \
18631 first_s = s; \
18632 \
18633 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18634 } \
18635 \
18636 ++START; \
18637 s = first_s; \
18638 } while (0)
18639
18640
18641 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18642 of AREA of glyph row ROW on window W between indices START and END.
18643 HL overrides the face for drawing glyph strings, e.g. it is
18644 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18645 x-positions of the drawing area.
18646
18647 This is an ugly monster macro construct because we must use alloca
18648 to allocate glyph strings (because draw_glyphs can be called
18649 asynchronously). */
18650
18651 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18652 do \
18653 { \
18654 HEAD = TAIL = NULL; \
18655 while (START < END) \
18656 { \
18657 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18658 switch (first_glyph->type) \
18659 { \
18660 case CHAR_GLYPH: \
18661 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18662 HL, X, LAST_X); \
18663 break; \
18664 \
18665 case COMPOSITE_GLYPH: \
18666 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18667 HL, X, LAST_X); \
18668 break; \
18669 \
18670 case STRETCH_GLYPH: \
18671 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18672 HL, X, LAST_X); \
18673 break; \
18674 \
18675 case IMAGE_GLYPH: \
18676 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18677 HL, X, LAST_X); \
18678 break; \
18679 \
18680 default: \
18681 abort (); \
18682 } \
18683 \
18684 set_glyph_string_background_width (s, START, LAST_X); \
18685 (X) += s->width; \
18686 } \
18687 } \
18688 while (0)
18689
18690
18691 /* Draw glyphs between START and END in AREA of ROW on window W,
18692 starting at x-position X. X is relative to AREA in W. HL is a
18693 face-override with the following meaning:
18694
18695 DRAW_NORMAL_TEXT draw normally
18696 DRAW_CURSOR draw in cursor face
18697 DRAW_MOUSE_FACE draw in mouse face.
18698 DRAW_INVERSE_VIDEO draw in mode line face
18699 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18700 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18701
18702 If OVERLAPS_P is non-zero, draw only the foreground of characters
18703 and clip to the physical height of ROW.
18704
18705 Value is the x-position reached, relative to AREA of W. */
18706
18707 static int
18708 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18709 struct window *w;
18710 int x;
18711 struct glyph_row *row;
18712 enum glyph_row_area area;
18713 int start, end;
18714 enum draw_glyphs_face hl;
18715 int overlaps_p;
18716 {
18717 struct glyph_string *head, *tail;
18718 struct glyph_string *s;
18719 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18720 int last_x, area_width;
18721 int x_reached;
18722 int i, j;
18723 struct frame *f = XFRAME (WINDOW_FRAME (w));
18724 DECLARE_HDC (hdc);
18725
18726 ALLOCATE_HDC (hdc, f);
18727
18728 /* Let's rather be paranoid than getting a SEGV. */
18729 end = min (end, row->used[area]);
18730 start = max (0, start);
18731 start = min (end, start);
18732
18733 /* Translate X to frame coordinates. Set last_x to the right
18734 end of the drawing area. */
18735 if (row->full_width_p)
18736 {
18737 /* X is relative to the left edge of W, without scroll bars
18738 or fringes. */
18739 x += WINDOW_LEFT_EDGE_X (w);
18740 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18741 }
18742 else
18743 {
18744 int area_left = window_box_left (w, area);
18745 x += area_left;
18746 area_width = window_box_width (w, area);
18747 last_x = area_left + area_width;
18748 }
18749
18750 /* Build a doubly-linked list of glyph_string structures between
18751 head and tail from what we have to draw. Note that the macro
18752 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18753 the reason we use a separate variable `i'. */
18754 i = start;
18755 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18756 if (tail)
18757 x_reached = tail->x + tail->background_width;
18758 else
18759 x_reached = x;
18760
18761 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18762 the row, redraw some glyphs in front or following the glyph
18763 strings built above. */
18764 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18765 {
18766 int dummy_x = 0;
18767 struct glyph_string *h, *t;
18768
18769 /* Compute overhangs for all glyph strings. */
18770 if (rif->compute_glyph_string_overhangs)
18771 for (s = head; s; s = s->next)
18772 rif->compute_glyph_string_overhangs (s);
18773
18774 /* Prepend glyph strings for glyphs in front of the first glyph
18775 string that are overwritten because of the first glyph
18776 string's left overhang. The background of all strings
18777 prepended must be drawn because the first glyph string
18778 draws over it. */
18779 i = left_overwritten (head);
18780 if (i >= 0)
18781 {
18782 j = i;
18783 BUILD_GLYPH_STRINGS (j, start, h, t,
18784 DRAW_NORMAL_TEXT, dummy_x, last_x);
18785 start = i;
18786 compute_overhangs_and_x (t, head->x, 1);
18787 prepend_glyph_string_lists (&head, &tail, h, t);
18788 clip_head = head;
18789 }
18790
18791 /* Prepend glyph strings for glyphs in front of the first glyph
18792 string that overwrite that glyph string because of their
18793 right overhang. For these strings, only the foreground must
18794 be drawn, because it draws over the glyph string at `head'.
18795 The background must not be drawn because this would overwrite
18796 right overhangs of preceding glyphs for which no glyph
18797 strings exist. */
18798 i = left_overwriting (head);
18799 if (i >= 0)
18800 {
18801 clip_head = head;
18802 BUILD_GLYPH_STRINGS (i, start, h, t,
18803 DRAW_NORMAL_TEXT, dummy_x, last_x);
18804 for (s = h; s; s = s->next)
18805 s->background_filled_p = 1;
18806 compute_overhangs_and_x (t, head->x, 1);
18807 prepend_glyph_string_lists (&head, &tail, h, t);
18808 }
18809
18810 /* Append glyphs strings for glyphs following the last glyph
18811 string tail that are overwritten by tail. The background of
18812 these strings has to be drawn because tail's foreground draws
18813 over it. */
18814 i = right_overwritten (tail);
18815 if (i >= 0)
18816 {
18817 BUILD_GLYPH_STRINGS (end, i, h, t,
18818 DRAW_NORMAL_TEXT, x, last_x);
18819 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18820 append_glyph_string_lists (&head, &tail, h, t);
18821 clip_tail = tail;
18822 }
18823
18824 /* Append glyph strings for glyphs following the last glyph
18825 string tail that overwrite tail. The foreground of such
18826 glyphs has to be drawn because it writes into the background
18827 of tail. The background must not be drawn because it could
18828 paint over the foreground of following glyphs. */
18829 i = right_overwriting (tail);
18830 if (i >= 0)
18831 {
18832 clip_tail = tail;
18833 BUILD_GLYPH_STRINGS (end, i, h, t,
18834 DRAW_NORMAL_TEXT, x, last_x);
18835 for (s = h; s; s = s->next)
18836 s->background_filled_p = 1;
18837 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18838 append_glyph_string_lists (&head, &tail, h, t);
18839 }
18840 if (clip_head || clip_tail)
18841 for (s = head; s; s = s->next)
18842 {
18843 s->clip_head = clip_head;
18844 s->clip_tail = clip_tail;
18845 }
18846 }
18847
18848 /* Draw all strings. */
18849 for (s = head; s; s = s->next)
18850 rif->draw_glyph_string (s);
18851
18852 if (area == TEXT_AREA
18853 && !row->full_width_p
18854 /* When drawing overlapping rows, only the glyph strings'
18855 foreground is drawn, which doesn't erase a cursor
18856 completely. */
18857 && !overlaps_p)
18858 {
18859 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18860 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18861 : (tail ? tail->x + tail->background_width : x));
18862
18863 int text_left = window_box_left (w, TEXT_AREA);
18864 x0 -= text_left;
18865 x1 -= text_left;
18866
18867 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18868 row->y, MATRIX_ROW_BOTTOM_Y (row));
18869 }
18870
18871 /* Value is the x-position up to which drawn, relative to AREA of W.
18872 This doesn't include parts drawn because of overhangs. */
18873 if (row->full_width_p)
18874 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18875 else
18876 x_reached -= window_box_left (w, area);
18877
18878 RELEASE_HDC (hdc, f);
18879
18880 return x_reached;
18881 }
18882
18883 /* Expand row matrix if too narrow. Don't expand if area
18884 is not present. */
18885
18886 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18887 { \
18888 if (!fonts_changed_p \
18889 && (it->glyph_row->glyphs[area] \
18890 < it->glyph_row->glyphs[area + 1])) \
18891 { \
18892 it->w->ncols_scale_factor++; \
18893 fonts_changed_p = 1; \
18894 } \
18895 }
18896
18897 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18898 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18899
18900 static INLINE void
18901 append_glyph (it)
18902 struct it *it;
18903 {
18904 struct glyph *glyph;
18905 enum glyph_row_area area = it->area;
18906
18907 xassert (it->glyph_row);
18908 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18909
18910 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18911 if (glyph < it->glyph_row->glyphs[area + 1])
18912 {
18913 glyph->charpos = CHARPOS (it->position);
18914 glyph->object = it->object;
18915 glyph->pixel_width = it->pixel_width;
18916 glyph->ascent = it->ascent;
18917 glyph->descent = it->descent;
18918 glyph->voffset = it->voffset;
18919 glyph->type = CHAR_GLYPH;
18920 glyph->multibyte_p = it->multibyte_p;
18921 glyph->left_box_line_p = it->start_of_box_run_p;
18922 glyph->right_box_line_p = it->end_of_box_run_p;
18923 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18924 || it->phys_descent > it->descent);
18925 glyph->padding_p = 0;
18926 glyph->glyph_not_available_p = it->glyph_not_available_p;
18927 glyph->face_id = it->face_id;
18928 glyph->u.ch = it->char_to_display;
18929 glyph->slice = null_glyph_slice;
18930 glyph->font_type = FONT_TYPE_UNKNOWN;
18931 ++it->glyph_row->used[area];
18932 }
18933 else
18934 IT_EXPAND_MATRIX_WIDTH (it, area);
18935 }
18936
18937 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18938 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18939
18940 static INLINE void
18941 append_composite_glyph (it)
18942 struct it *it;
18943 {
18944 struct glyph *glyph;
18945 enum glyph_row_area area = it->area;
18946
18947 xassert (it->glyph_row);
18948
18949 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18950 if (glyph < it->glyph_row->glyphs[area + 1])
18951 {
18952 glyph->charpos = CHARPOS (it->position);
18953 glyph->object = it->object;
18954 glyph->pixel_width = it->pixel_width;
18955 glyph->ascent = it->ascent;
18956 glyph->descent = it->descent;
18957 glyph->voffset = it->voffset;
18958 glyph->type = COMPOSITE_GLYPH;
18959 glyph->multibyte_p = it->multibyte_p;
18960 glyph->left_box_line_p = it->start_of_box_run_p;
18961 glyph->right_box_line_p = it->end_of_box_run_p;
18962 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18963 || it->phys_descent > it->descent);
18964 glyph->padding_p = 0;
18965 glyph->glyph_not_available_p = 0;
18966 glyph->face_id = it->face_id;
18967 glyph->u.cmp_id = it->cmp_id;
18968 glyph->slice = null_glyph_slice;
18969 glyph->font_type = FONT_TYPE_UNKNOWN;
18970 ++it->glyph_row->used[area];
18971 }
18972 else
18973 IT_EXPAND_MATRIX_WIDTH (it, area);
18974 }
18975
18976
18977 /* Change IT->ascent and IT->height according to the setting of
18978 IT->voffset. */
18979
18980 static INLINE void
18981 take_vertical_position_into_account (it)
18982 struct it *it;
18983 {
18984 if (it->voffset)
18985 {
18986 if (it->voffset < 0)
18987 /* Increase the ascent so that we can display the text higher
18988 in the line. */
18989 it->ascent -= it->voffset;
18990 else
18991 /* Increase the descent so that we can display the text lower
18992 in the line. */
18993 it->descent += it->voffset;
18994 }
18995 }
18996
18997
18998 /* Produce glyphs/get display metrics for the image IT is loaded with.
18999 See the description of struct display_iterator in dispextern.h for
19000 an overview of struct display_iterator. */
19001
19002 static void
19003 produce_image_glyph (it)
19004 struct it *it;
19005 {
19006 struct image *img;
19007 struct face *face;
19008 int glyph_ascent;
19009 struct glyph_slice slice;
19010
19011 xassert (it->what == IT_IMAGE);
19012
19013 face = FACE_FROM_ID (it->f, it->face_id);
19014 xassert (face);
19015 /* Make sure X resources of the face is loaded. */
19016 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19017
19018 if (it->image_id < 0)
19019 {
19020 /* Fringe bitmap. */
19021 it->ascent = it->phys_ascent = 0;
19022 it->descent = it->phys_descent = 0;
19023 it->pixel_width = 0;
19024 it->nglyphs = 0;
19025 return;
19026 }
19027
19028 img = IMAGE_FROM_ID (it->f, it->image_id);
19029 xassert (img);
19030 /* Make sure X resources of the image is loaded. */
19031 prepare_image_for_display (it->f, img);
19032
19033 slice.x = slice.y = 0;
19034 slice.width = img->width;
19035 slice.height = img->height;
19036
19037 if (INTEGERP (it->slice.x))
19038 slice.x = XINT (it->slice.x);
19039 else if (FLOATP (it->slice.x))
19040 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19041
19042 if (INTEGERP (it->slice.y))
19043 slice.y = XINT (it->slice.y);
19044 else if (FLOATP (it->slice.y))
19045 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19046
19047 if (INTEGERP (it->slice.width))
19048 slice.width = XINT (it->slice.width);
19049 else if (FLOATP (it->slice.width))
19050 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19051
19052 if (INTEGERP (it->slice.height))
19053 slice.height = XINT (it->slice.height);
19054 else if (FLOATP (it->slice.height))
19055 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19056
19057 if (slice.x >= img->width)
19058 slice.x = img->width;
19059 if (slice.y >= img->height)
19060 slice.y = img->height;
19061 if (slice.x + slice.width >= img->width)
19062 slice.width = img->width - slice.x;
19063 if (slice.y + slice.height > img->height)
19064 slice.height = img->height - slice.y;
19065
19066 if (slice.width == 0 || slice.height == 0)
19067 return;
19068
19069 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19070
19071 it->descent = slice.height - glyph_ascent;
19072 if (slice.y == 0)
19073 it->descent += img->vmargin;
19074 if (slice.y + slice.height == img->height)
19075 it->descent += img->vmargin;
19076 it->phys_descent = it->descent;
19077
19078 it->pixel_width = slice.width;
19079 if (slice.x == 0)
19080 it->pixel_width += img->hmargin;
19081 if (slice.x + slice.width == img->width)
19082 it->pixel_width += img->hmargin;
19083
19084 /* It's quite possible for images to have an ascent greater than
19085 their height, so don't get confused in that case. */
19086 if (it->descent < 0)
19087 it->descent = 0;
19088
19089 #if 0 /* this breaks image tiling */
19090 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19091 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19092 if (face_ascent > it->ascent)
19093 it->ascent = it->phys_ascent = face_ascent;
19094 #endif
19095
19096 it->nglyphs = 1;
19097
19098 if (face->box != FACE_NO_BOX)
19099 {
19100 if (face->box_line_width > 0)
19101 {
19102 if (slice.y == 0)
19103 it->ascent += face->box_line_width;
19104 if (slice.y + slice.height == img->height)
19105 it->descent += face->box_line_width;
19106 }
19107
19108 if (it->start_of_box_run_p && slice.x == 0)
19109 it->pixel_width += abs (face->box_line_width);
19110 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19111 it->pixel_width += abs (face->box_line_width);
19112 }
19113
19114 take_vertical_position_into_account (it);
19115
19116 if (it->glyph_row)
19117 {
19118 struct glyph *glyph;
19119 enum glyph_row_area area = it->area;
19120
19121 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19122 if (glyph < it->glyph_row->glyphs[area + 1])
19123 {
19124 glyph->charpos = CHARPOS (it->position);
19125 glyph->object = it->object;
19126 glyph->pixel_width = it->pixel_width;
19127 glyph->ascent = glyph_ascent;
19128 glyph->descent = it->descent;
19129 glyph->voffset = it->voffset;
19130 glyph->type = IMAGE_GLYPH;
19131 glyph->multibyte_p = it->multibyte_p;
19132 glyph->left_box_line_p = it->start_of_box_run_p;
19133 glyph->right_box_line_p = it->end_of_box_run_p;
19134 glyph->overlaps_vertically_p = 0;
19135 glyph->padding_p = 0;
19136 glyph->glyph_not_available_p = 0;
19137 glyph->face_id = it->face_id;
19138 glyph->u.img_id = img->id;
19139 glyph->slice = slice;
19140 glyph->font_type = FONT_TYPE_UNKNOWN;
19141 ++it->glyph_row->used[area];
19142 }
19143 else
19144 IT_EXPAND_MATRIX_WIDTH (it, area);
19145 }
19146 }
19147
19148
19149 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19150 of the glyph, WIDTH and HEIGHT are the width and height of the
19151 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19152
19153 static void
19154 append_stretch_glyph (it, object, width, height, ascent)
19155 struct it *it;
19156 Lisp_Object object;
19157 int width, height;
19158 int ascent;
19159 {
19160 struct glyph *glyph;
19161 enum glyph_row_area area = it->area;
19162
19163 xassert (ascent >= 0 && ascent <= height);
19164
19165 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19166 if (glyph < it->glyph_row->glyphs[area + 1])
19167 {
19168 glyph->charpos = CHARPOS (it->position);
19169 glyph->object = object;
19170 glyph->pixel_width = width;
19171 glyph->ascent = ascent;
19172 glyph->descent = height - ascent;
19173 glyph->voffset = it->voffset;
19174 glyph->type = STRETCH_GLYPH;
19175 glyph->multibyte_p = it->multibyte_p;
19176 glyph->left_box_line_p = it->start_of_box_run_p;
19177 glyph->right_box_line_p = it->end_of_box_run_p;
19178 glyph->overlaps_vertically_p = 0;
19179 glyph->padding_p = 0;
19180 glyph->glyph_not_available_p = 0;
19181 glyph->face_id = it->face_id;
19182 glyph->u.stretch.ascent = ascent;
19183 glyph->u.stretch.height = height;
19184 glyph->slice = null_glyph_slice;
19185 glyph->font_type = FONT_TYPE_UNKNOWN;
19186 ++it->glyph_row->used[area];
19187 }
19188 else
19189 IT_EXPAND_MATRIX_WIDTH (it, area);
19190 }
19191
19192
19193 /* Produce a stretch glyph for iterator IT. IT->object is the value
19194 of the glyph property displayed. The value must be a list
19195 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19196 being recognized:
19197
19198 1. `:width WIDTH' specifies that the space should be WIDTH *
19199 canonical char width wide. WIDTH may be an integer or floating
19200 point number.
19201
19202 2. `:relative-width FACTOR' specifies that the width of the stretch
19203 should be computed from the width of the first character having the
19204 `glyph' property, and should be FACTOR times that width.
19205
19206 3. `:align-to HPOS' specifies that the space should be wide enough
19207 to reach HPOS, a value in canonical character units.
19208
19209 Exactly one of the above pairs must be present.
19210
19211 4. `:height HEIGHT' specifies that the height of the stretch produced
19212 should be HEIGHT, measured in canonical character units.
19213
19214 5. `:relative-height FACTOR' specifies that the height of the
19215 stretch should be FACTOR times the height of the characters having
19216 the glyph property.
19217
19218 Either none or exactly one of 4 or 5 must be present.
19219
19220 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19221 of the stretch should be used for the ascent of the stretch.
19222 ASCENT must be in the range 0 <= ASCENT <= 100. */
19223
19224 static void
19225 produce_stretch_glyph (it)
19226 struct it *it;
19227 {
19228 /* (space :width WIDTH :height HEIGHT ...) */
19229 Lisp_Object prop, plist;
19230 int width = 0, height = 0, align_to = -1;
19231 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19232 int ascent = 0;
19233 double tem;
19234 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19235 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19236
19237 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19238
19239 /* List should start with `space'. */
19240 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19241 plist = XCDR (it->object);
19242
19243 /* Compute the width of the stretch. */
19244 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19245 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19246 {
19247 /* Absolute width `:width WIDTH' specified and valid. */
19248 zero_width_ok_p = 1;
19249 width = (int)tem;
19250 }
19251 else if (prop = Fplist_get (plist, QCrelative_width),
19252 NUMVAL (prop) > 0)
19253 {
19254 /* Relative width `:relative-width FACTOR' specified and valid.
19255 Compute the width of the characters having the `glyph'
19256 property. */
19257 struct it it2;
19258 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19259
19260 it2 = *it;
19261 if (it->multibyte_p)
19262 {
19263 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19264 - IT_BYTEPOS (*it));
19265 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19266 }
19267 else
19268 it2.c = *p, it2.len = 1;
19269
19270 it2.glyph_row = NULL;
19271 it2.what = IT_CHARACTER;
19272 x_produce_glyphs (&it2);
19273 width = NUMVAL (prop) * it2.pixel_width;
19274 }
19275 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19276 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19277 {
19278 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19279 align_to = (align_to < 0
19280 ? 0
19281 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19282 else if (align_to < 0)
19283 align_to = window_box_left_offset (it->w, TEXT_AREA);
19284 width = max (0, (int)tem + align_to - it->current_x);
19285 zero_width_ok_p = 1;
19286 }
19287 else
19288 /* Nothing specified -> width defaults to canonical char width. */
19289 width = FRAME_COLUMN_WIDTH (it->f);
19290
19291 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19292 width = 1;
19293
19294 /* Compute height. */
19295 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19296 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19297 {
19298 height = (int)tem;
19299 zero_height_ok_p = 1;
19300 }
19301 else if (prop = Fplist_get (plist, QCrelative_height),
19302 NUMVAL (prop) > 0)
19303 height = FONT_HEIGHT (font) * NUMVAL (prop);
19304 else
19305 height = FONT_HEIGHT (font);
19306
19307 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19308 height = 1;
19309
19310 /* Compute percentage of height used for ascent. If
19311 `:ascent ASCENT' is present and valid, use that. Otherwise,
19312 derive the ascent from the font in use. */
19313 if (prop = Fplist_get (plist, QCascent),
19314 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19315 ascent = height * NUMVAL (prop) / 100.0;
19316 else if (!NILP (prop)
19317 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19318 ascent = min (max (0, (int)tem), height);
19319 else
19320 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19321
19322 if (width > 0 && height > 0 && it->glyph_row)
19323 {
19324 Lisp_Object object = it->stack[it->sp - 1].string;
19325 if (!STRINGP (object))
19326 object = it->w->buffer;
19327 append_stretch_glyph (it, object, width, height, ascent);
19328 }
19329
19330 it->pixel_width = width;
19331 it->ascent = it->phys_ascent = ascent;
19332 it->descent = it->phys_descent = height - it->ascent;
19333 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19334
19335 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19336 {
19337 if (face->box_line_width > 0)
19338 {
19339 it->ascent += face->box_line_width;
19340 it->descent += face->box_line_width;
19341 }
19342
19343 if (it->start_of_box_run_p)
19344 it->pixel_width += abs (face->box_line_width);
19345 if (it->end_of_box_run_p)
19346 it->pixel_width += abs (face->box_line_width);
19347 }
19348
19349 take_vertical_position_into_account (it);
19350 }
19351
19352 /* Get line-height and line-spacing property at point.
19353 If line-height has format (HEIGHT TOTAL), return TOTAL
19354 in TOTAL_HEIGHT. */
19355
19356 static Lisp_Object
19357 get_line_height_property (it, prop)
19358 struct it *it;
19359 Lisp_Object prop;
19360 {
19361 Lisp_Object position;
19362
19363 if (STRINGP (it->object))
19364 position = make_number (IT_STRING_CHARPOS (*it));
19365 else if (BUFFERP (it->object))
19366 position = make_number (IT_CHARPOS (*it));
19367 else
19368 return Qnil;
19369
19370 return Fget_char_property (position, prop, it->object);
19371 }
19372
19373 /* Calculate line-height and line-spacing properties.
19374 An integer value specifies explicit pixel value.
19375 A float value specifies relative value to current face height.
19376 A cons (float . face-name) specifies relative value to
19377 height of specified face font.
19378
19379 Returns height in pixels, or nil. */
19380
19381
19382 static Lisp_Object
19383 calc_line_height_property (it, val, font, boff, override)
19384 struct it *it;
19385 Lisp_Object val;
19386 XFontStruct *font;
19387 int boff, override;
19388 {
19389 Lisp_Object face_name = Qnil;
19390 int ascent, descent, height;
19391
19392 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19393 return val;
19394
19395 if (CONSP (val))
19396 {
19397 face_name = XCAR (val);
19398 val = XCDR (val);
19399 if (!NUMBERP (val))
19400 val = make_number (1);
19401 if (NILP (face_name))
19402 {
19403 height = it->ascent + it->descent;
19404 goto scale;
19405 }
19406 }
19407
19408 if (NILP (face_name))
19409 {
19410 font = FRAME_FONT (it->f);
19411 boff = FRAME_BASELINE_OFFSET (it->f);
19412 }
19413 else if (EQ (face_name, Qt))
19414 {
19415 override = 0;
19416 }
19417 else
19418 {
19419 int face_id;
19420 struct face *face;
19421 struct font_info *font_info;
19422
19423 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19424 if (face_id < 0)
19425 return make_number (-1);
19426
19427 face = FACE_FROM_ID (it->f, face_id);
19428 font = face->font;
19429 if (font == NULL)
19430 return make_number (-1);
19431
19432 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19433 boff = font_info->baseline_offset;
19434 if (font_info->vertical_centering)
19435 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19436 }
19437
19438 ascent = FONT_BASE (font) + boff;
19439 descent = FONT_DESCENT (font) - boff;
19440
19441 if (override)
19442 {
19443 it->override_ascent = ascent;
19444 it->override_descent = descent;
19445 it->override_boff = boff;
19446 }
19447
19448 height = ascent + descent;
19449
19450 scale:
19451 if (FLOATP (val))
19452 height = (int)(XFLOAT_DATA (val) * height);
19453 else if (INTEGERP (val))
19454 height *= XINT (val);
19455
19456 return make_number (height);
19457 }
19458
19459
19460 /* RIF:
19461 Produce glyphs/get display metrics for the display element IT is
19462 loaded with. See the description of struct display_iterator in
19463 dispextern.h for an overview of struct display_iterator. */
19464
19465 void
19466 x_produce_glyphs (it)
19467 struct it *it;
19468 {
19469 int extra_line_spacing = it->extra_line_spacing;
19470
19471 it->glyph_not_available_p = 0;
19472
19473 if (it->what == IT_CHARACTER)
19474 {
19475 XChar2b char2b;
19476 XFontStruct *font;
19477 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19478 XCharStruct *pcm;
19479 int font_not_found_p;
19480 struct font_info *font_info;
19481 int boff; /* baseline offset */
19482 /* We may change it->multibyte_p upon unibyte<->multibyte
19483 conversion. So, save the current value now and restore it
19484 later.
19485
19486 Note: It seems that we don't have to record multibyte_p in
19487 struct glyph because the character code itself tells if or
19488 not the character is multibyte. Thus, in the future, we must
19489 consider eliminating the field `multibyte_p' in the struct
19490 glyph. */
19491 int saved_multibyte_p = it->multibyte_p;
19492
19493 /* Maybe translate single-byte characters to multibyte, or the
19494 other way. */
19495 it->char_to_display = it->c;
19496 if (!ASCII_BYTE_P (it->c))
19497 {
19498 if (unibyte_display_via_language_environment
19499 && SINGLE_BYTE_CHAR_P (it->c)
19500 && (it->c >= 0240
19501 || !NILP (Vnonascii_translation_table)))
19502 {
19503 it->char_to_display = unibyte_char_to_multibyte (it->c);
19504 it->multibyte_p = 1;
19505 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19506 face = FACE_FROM_ID (it->f, it->face_id);
19507 }
19508 else if (!SINGLE_BYTE_CHAR_P (it->c)
19509 && !it->multibyte_p)
19510 {
19511 it->multibyte_p = 1;
19512 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19513 face = FACE_FROM_ID (it->f, it->face_id);
19514 }
19515 }
19516
19517 /* Get font to use. Encode IT->char_to_display. */
19518 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19519 &char2b, it->multibyte_p, 0);
19520 font = face->font;
19521
19522 /* When no suitable font found, use the default font. */
19523 font_not_found_p = font == NULL;
19524 if (font_not_found_p)
19525 {
19526 font = FRAME_FONT (it->f);
19527 boff = FRAME_BASELINE_OFFSET (it->f);
19528 font_info = NULL;
19529 }
19530 else
19531 {
19532 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19533 boff = font_info->baseline_offset;
19534 if (font_info->vertical_centering)
19535 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19536 }
19537
19538 if (it->char_to_display >= ' '
19539 && (!it->multibyte_p || it->char_to_display < 128))
19540 {
19541 /* Either unibyte or ASCII. */
19542 int stretched_p;
19543
19544 it->nglyphs = 1;
19545
19546 pcm = rif->per_char_metric (font, &char2b,
19547 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19548
19549 if (it->override_ascent >= 0)
19550 {
19551 it->ascent = it->override_ascent;
19552 it->descent = it->override_descent;
19553 boff = it->override_boff;
19554 }
19555 else
19556 {
19557 it->ascent = FONT_BASE (font) + boff;
19558 it->descent = FONT_DESCENT (font) - boff;
19559 }
19560
19561 if (pcm)
19562 {
19563 it->phys_ascent = pcm->ascent + boff;
19564 it->phys_descent = pcm->descent - boff;
19565 it->pixel_width = pcm->width;
19566 }
19567 else
19568 {
19569 it->glyph_not_available_p = 1;
19570 it->phys_ascent = it->ascent;
19571 it->phys_descent = it->descent;
19572 it->pixel_width = FONT_WIDTH (font);
19573 }
19574
19575 if (it->constrain_row_ascent_descent_p)
19576 {
19577 if (it->descent > it->max_descent)
19578 {
19579 it->ascent += it->descent - it->max_descent;
19580 it->descent = it->max_descent;
19581 }
19582 if (it->ascent > it->max_ascent)
19583 {
19584 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19585 it->ascent = it->max_ascent;
19586 }
19587 it->phys_ascent = min (it->phys_ascent, it->ascent);
19588 it->phys_descent = min (it->phys_descent, it->descent);
19589 extra_line_spacing = 0;
19590 }
19591
19592 /* If this is a space inside a region of text with
19593 `space-width' property, change its width. */
19594 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19595 if (stretched_p)
19596 it->pixel_width *= XFLOATINT (it->space_width);
19597
19598 /* If face has a box, add the box thickness to the character
19599 height. If character has a box line to the left and/or
19600 right, add the box line width to the character's width. */
19601 if (face->box != FACE_NO_BOX)
19602 {
19603 int thick = face->box_line_width;
19604
19605 if (thick > 0)
19606 {
19607 it->ascent += thick;
19608 it->descent += thick;
19609 }
19610 else
19611 thick = -thick;
19612
19613 if (it->start_of_box_run_p)
19614 it->pixel_width += thick;
19615 if (it->end_of_box_run_p)
19616 it->pixel_width += thick;
19617 }
19618
19619 /* If face has an overline, add the height of the overline
19620 (1 pixel) and a 1 pixel margin to the character height. */
19621 if (face->overline_p)
19622 it->ascent += 2;
19623
19624 if (it->constrain_row_ascent_descent_p)
19625 {
19626 if (it->ascent > it->max_ascent)
19627 it->ascent = it->max_ascent;
19628 if (it->descent > it->max_descent)
19629 it->descent = it->max_descent;
19630 }
19631
19632 take_vertical_position_into_account (it);
19633
19634 /* If we have to actually produce glyphs, do it. */
19635 if (it->glyph_row)
19636 {
19637 if (stretched_p)
19638 {
19639 /* Translate a space with a `space-width' property
19640 into a stretch glyph. */
19641 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19642 / FONT_HEIGHT (font));
19643 append_stretch_glyph (it, it->object, it->pixel_width,
19644 it->ascent + it->descent, ascent);
19645 }
19646 else
19647 append_glyph (it);
19648
19649 /* If characters with lbearing or rbearing are displayed
19650 in this line, record that fact in a flag of the
19651 glyph row. This is used to optimize X output code. */
19652 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19653 it->glyph_row->contains_overlapping_glyphs_p = 1;
19654 }
19655 }
19656 else if (it->char_to_display == '\n')
19657 {
19658 /* A newline has no width but we need the height of the line.
19659 But if previous part of the line set a height, don't
19660 increase that height */
19661
19662 Lisp_Object height;
19663 Lisp_Object total_height = Qnil;
19664
19665 it->override_ascent = -1;
19666 it->pixel_width = 0;
19667 it->nglyphs = 0;
19668
19669 height = get_line_height_property(it, Qline_height);
19670 /* Split (line-height total-height) list */
19671 if (CONSP (height)
19672 && CONSP (XCDR (height))
19673 && NILP (XCDR (XCDR (height))))
19674 {
19675 total_height = XCAR (XCDR (height));
19676 height = XCAR (height);
19677 }
19678 height = calc_line_height_property(it, height, font, boff, 1);
19679
19680 if (it->override_ascent >= 0)
19681 {
19682 it->ascent = it->override_ascent;
19683 it->descent = it->override_descent;
19684 boff = it->override_boff;
19685 }
19686 else
19687 {
19688 it->ascent = FONT_BASE (font) + boff;
19689 it->descent = FONT_DESCENT (font) - boff;
19690 }
19691
19692 if (EQ (height, Qt))
19693 {
19694 if (it->descent > it->max_descent)
19695 {
19696 it->ascent += it->descent - it->max_descent;
19697 it->descent = it->max_descent;
19698 }
19699 if (it->ascent > it->max_ascent)
19700 {
19701 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19702 it->ascent = it->max_ascent;
19703 }
19704 it->phys_ascent = min (it->phys_ascent, it->ascent);
19705 it->phys_descent = min (it->phys_descent, it->descent);
19706 it->constrain_row_ascent_descent_p = 1;
19707 extra_line_spacing = 0;
19708 }
19709 else
19710 {
19711 Lisp_Object spacing;
19712
19713 it->phys_ascent = it->ascent;
19714 it->phys_descent = it->descent;
19715
19716 if ((it->max_ascent > 0 || it->max_descent > 0)
19717 && face->box != FACE_NO_BOX
19718 && face->box_line_width > 0)
19719 {
19720 it->ascent += face->box_line_width;
19721 it->descent += face->box_line_width;
19722 }
19723 if (!NILP (height)
19724 && XINT (height) > it->ascent + it->descent)
19725 it->ascent = XINT (height) - it->descent;
19726
19727 if (!NILP (total_height))
19728 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19729 else
19730 {
19731 spacing = get_line_height_property(it, Qline_spacing);
19732 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19733 }
19734 if (INTEGERP (spacing))
19735 {
19736 extra_line_spacing = XINT (spacing);
19737 if (!NILP (total_height))
19738 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19739 }
19740 }
19741 }
19742 else if (it->char_to_display == '\t')
19743 {
19744 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19745 int x = it->current_x + it->continuation_lines_width;
19746 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19747
19748 /* If the distance from the current position to the next tab
19749 stop is less than a space character width, use the
19750 tab stop after that. */
19751 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19752 next_tab_x += tab_width;
19753
19754 it->pixel_width = next_tab_x - x;
19755 it->nglyphs = 1;
19756 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19757 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19758
19759 if (it->glyph_row)
19760 {
19761 append_stretch_glyph (it, it->object, it->pixel_width,
19762 it->ascent + it->descent, it->ascent);
19763 }
19764 }
19765 else
19766 {
19767 /* A multi-byte character. Assume that the display width of the
19768 character is the width of the character multiplied by the
19769 width of the font. */
19770
19771 /* If we found a font, this font should give us the right
19772 metrics. If we didn't find a font, use the frame's
19773 default font and calculate the width of the character
19774 from the charset width; this is what old redisplay code
19775 did. */
19776
19777 pcm = rif->per_char_metric (font, &char2b,
19778 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19779
19780 if (font_not_found_p || !pcm)
19781 {
19782 int charset = CHAR_CHARSET (it->char_to_display);
19783
19784 it->glyph_not_available_p = 1;
19785 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19786 * CHARSET_WIDTH (charset));
19787 it->phys_ascent = FONT_BASE (font) + boff;
19788 it->phys_descent = FONT_DESCENT (font) - boff;
19789 }
19790 else
19791 {
19792 it->pixel_width = pcm->width;
19793 it->phys_ascent = pcm->ascent + boff;
19794 it->phys_descent = pcm->descent - boff;
19795 if (it->glyph_row
19796 && (pcm->lbearing < 0
19797 || pcm->rbearing > pcm->width))
19798 it->glyph_row->contains_overlapping_glyphs_p = 1;
19799 }
19800 it->nglyphs = 1;
19801 it->ascent = FONT_BASE (font) + boff;
19802 it->descent = FONT_DESCENT (font) - boff;
19803 if (face->box != FACE_NO_BOX)
19804 {
19805 int thick = face->box_line_width;
19806
19807 if (thick > 0)
19808 {
19809 it->ascent += thick;
19810 it->descent += thick;
19811 }
19812 else
19813 thick = - thick;
19814
19815 if (it->start_of_box_run_p)
19816 it->pixel_width += thick;
19817 if (it->end_of_box_run_p)
19818 it->pixel_width += thick;
19819 }
19820
19821 /* If face has an overline, add the height of the overline
19822 (1 pixel) and a 1 pixel margin to the character height. */
19823 if (face->overline_p)
19824 it->ascent += 2;
19825
19826 take_vertical_position_into_account (it);
19827
19828 if (it->glyph_row)
19829 append_glyph (it);
19830 }
19831 it->multibyte_p = saved_multibyte_p;
19832 }
19833 else if (it->what == IT_COMPOSITION)
19834 {
19835 /* Note: A composition is represented as one glyph in the
19836 glyph matrix. There are no padding glyphs. */
19837 XChar2b char2b;
19838 XFontStruct *font;
19839 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19840 XCharStruct *pcm;
19841 int font_not_found_p;
19842 struct font_info *font_info;
19843 int boff; /* baseline offset */
19844 struct composition *cmp = composition_table[it->cmp_id];
19845
19846 /* Maybe translate single-byte characters to multibyte. */
19847 it->char_to_display = it->c;
19848 if (unibyte_display_via_language_environment
19849 && SINGLE_BYTE_CHAR_P (it->c)
19850 && (it->c >= 0240
19851 || (it->c >= 0200
19852 && !NILP (Vnonascii_translation_table))))
19853 {
19854 it->char_to_display = unibyte_char_to_multibyte (it->c);
19855 }
19856
19857 /* Get face and font to use. Encode IT->char_to_display. */
19858 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19859 face = FACE_FROM_ID (it->f, it->face_id);
19860 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19861 &char2b, it->multibyte_p, 0);
19862 font = face->font;
19863
19864 /* When no suitable font found, use the default font. */
19865 font_not_found_p = font == NULL;
19866 if (font_not_found_p)
19867 {
19868 font = FRAME_FONT (it->f);
19869 boff = FRAME_BASELINE_OFFSET (it->f);
19870 font_info = NULL;
19871 }
19872 else
19873 {
19874 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19875 boff = font_info->baseline_offset;
19876 if (font_info->vertical_centering)
19877 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19878 }
19879
19880 /* There are no padding glyphs, so there is only one glyph to
19881 produce for the composition. Important is that pixel_width,
19882 ascent and descent are the values of what is drawn by
19883 draw_glyphs (i.e. the values of the overall glyphs composed). */
19884 it->nglyphs = 1;
19885
19886 /* If we have not yet calculated pixel size data of glyphs of
19887 the composition for the current face font, calculate them
19888 now. Theoretically, we have to check all fonts for the
19889 glyphs, but that requires much time and memory space. So,
19890 here we check only the font of the first glyph. This leads
19891 to incorrect display very rarely, and C-l (recenter) can
19892 correct the display anyway. */
19893 if (cmp->font != (void *) font)
19894 {
19895 /* Ascent and descent of the font of the first character of
19896 this composition (adjusted by baseline offset). Ascent
19897 and descent of overall glyphs should not be less than
19898 them respectively. */
19899 int font_ascent = FONT_BASE (font) + boff;
19900 int font_descent = FONT_DESCENT (font) - boff;
19901 /* Bounding box of the overall glyphs. */
19902 int leftmost, rightmost, lowest, highest;
19903 int i, width, ascent, descent;
19904
19905 cmp->font = (void *) font;
19906
19907 /* Initialize the bounding box. */
19908 if (font_info
19909 && (pcm = rif->per_char_metric (font, &char2b,
19910 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19911 {
19912 width = pcm->width;
19913 ascent = pcm->ascent;
19914 descent = pcm->descent;
19915 }
19916 else
19917 {
19918 width = FONT_WIDTH (font);
19919 ascent = FONT_BASE (font);
19920 descent = FONT_DESCENT (font);
19921 }
19922
19923 rightmost = width;
19924 lowest = - descent + boff;
19925 highest = ascent + boff;
19926 leftmost = 0;
19927
19928 if (font_info
19929 && font_info->default_ascent
19930 && CHAR_TABLE_P (Vuse_default_ascent)
19931 && !NILP (Faref (Vuse_default_ascent,
19932 make_number (it->char_to_display))))
19933 highest = font_info->default_ascent + boff;
19934
19935 /* Draw the first glyph at the normal position. It may be
19936 shifted to right later if some other glyphs are drawn at
19937 the left. */
19938 cmp->offsets[0] = 0;
19939 cmp->offsets[1] = boff;
19940
19941 /* Set cmp->offsets for the remaining glyphs. */
19942 for (i = 1; i < cmp->glyph_len; i++)
19943 {
19944 int left, right, btm, top;
19945 int ch = COMPOSITION_GLYPH (cmp, i);
19946 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19947
19948 face = FACE_FROM_ID (it->f, face_id);
19949 get_char_face_and_encoding (it->f, ch, face->id,
19950 &char2b, it->multibyte_p, 0);
19951 font = face->font;
19952 if (font == NULL)
19953 {
19954 font = FRAME_FONT (it->f);
19955 boff = FRAME_BASELINE_OFFSET (it->f);
19956 font_info = NULL;
19957 }
19958 else
19959 {
19960 font_info
19961 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19962 boff = font_info->baseline_offset;
19963 if (font_info->vertical_centering)
19964 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19965 }
19966
19967 if (font_info
19968 && (pcm = rif->per_char_metric (font, &char2b,
19969 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19970 {
19971 width = pcm->width;
19972 ascent = pcm->ascent;
19973 descent = pcm->descent;
19974 }
19975 else
19976 {
19977 width = FONT_WIDTH (font);
19978 ascent = 1;
19979 descent = 0;
19980 }
19981
19982 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19983 {
19984 /* Relative composition with or without
19985 alternate chars. */
19986 left = (leftmost + rightmost - width) / 2;
19987 btm = - descent + boff;
19988 if (font_info && font_info->relative_compose
19989 && (! CHAR_TABLE_P (Vignore_relative_composition)
19990 || NILP (Faref (Vignore_relative_composition,
19991 make_number (ch)))))
19992 {
19993
19994 if (- descent >= font_info->relative_compose)
19995 /* One extra pixel between two glyphs. */
19996 btm = highest + 1;
19997 else if (ascent <= 0)
19998 /* One extra pixel between two glyphs. */
19999 btm = lowest - 1 - ascent - descent;
20000 }
20001 }
20002 else
20003 {
20004 /* A composition rule is specified by an integer
20005 value that encodes global and new reference
20006 points (GREF and NREF). GREF and NREF are
20007 specified by numbers as below:
20008
20009 0---1---2 -- ascent
20010 | |
20011 | |
20012 | |
20013 9--10--11 -- center
20014 | |
20015 ---3---4---5--- baseline
20016 | |
20017 6---7---8 -- descent
20018 */
20019 int rule = COMPOSITION_RULE (cmp, i);
20020 int gref, nref, grefx, grefy, nrefx, nrefy;
20021
20022 COMPOSITION_DECODE_RULE (rule, gref, nref);
20023 grefx = gref % 3, nrefx = nref % 3;
20024 grefy = gref / 3, nrefy = nref / 3;
20025
20026 left = (leftmost
20027 + grefx * (rightmost - leftmost) / 2
20028 - nrefx * width / 2);
20029 btm = ((grefy == 0 ? highest
20030 : grefy == 1 ? 0
20031 : grefy == 2 ? lowest
20032 : (highest + lowest) / 2)
20033 - (nrefy == 0 ? ascent + descent
20034 : nrefy == 1 ? descent - boff
20035 : nrefy == 2 ? 0
20036 : (ascent + descent) / 2));
20037 }
20038
20039 cmp->offsets[i * 2] = left;
20040 cmp->offsets[i * 2 + 1] = btm + descent;
20041
20042 /* Update the bounding box of the overall glyphs. */
20043 right = left + width;
20044 top = btm + descent + ascent;
20045 if (left < leftmost)
20046 leftmost = left;
20047 if (right > rightmost)
20048 rightmost = right;
20049 if (top > highest)
20050 highest = top;
20051 if (btm < lowest)
20052 lowest = btm;
20053 }
20054
20055 /* If there are glyphs whose x-offsets are negative,
20056 shift all glyphs to the right and make all x-offsets
20057 non-negative. */
20058 if (leftmost < 0)
20059 {
20060 for (i = 0; i < cmp->glyph_len; i++)
20061 cmp->offsets[i * 2] -= leftmost;
20062 rightmost -= leftmost;
20063 }
20064
20065 cmp->pixel_width = rightmost;
20066 cmp->ascent = highest;
20067 cmp->descent = - lowest;
20068 if (cmp->ascent < font_ascent)
20069 cmp->ascent = font_ascent;
20070 if (cmp->descent < font_descent)
20071 cmp->descent = font_descent;
20072 }
20073
20074 it->pixel_width = cmp->pixel_width;
20075 it->ascent = it->phys_ascent = cmp->ascent;
20076 it->descent = it->phys_descent = cmp->descent;
20077
20078 if (face->box != FACE_NO_BOX)
20079 {
20080 int thick = face->box_line_width;
20081
20082 if (thick > 0)
20083 {
20084 it->ascent += thick;
20085 it->descent += thick;
20086 }
20087 else
20088 thick = - thick;
20089
20090 if (it->start_of_box_run_p)
20091 it->pixel_width += thick;
20092 if (it->end_of_box_run_p)
20093 it->pixel_width += thick;
20094 }
20095
20096 /* If face has an overline, add the height of the overline
20097 (1 pixel) and a 1 pixel margin to the character height. */
20098 if (face->overline_p)
20099 it->ascent += 2;
20100
20101 take_vertical_position_into_account (it);
20102
20103 if (it->glyph_row)
20104 append_composite_glyph (it);
20105 }
20106 else if (it->what == IT_IMAGE)
20107 produce_image_glyph (it);
20108 else if (it->what == IT_STRETCH)
20109 produce_stretch_glyph (it);
20110
20111 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20112 because this isn't true for images with `:ascent 100'. */
20113 xassert (it->ascent >= 0 && it->descent >= 0);
20114 if (it->area == TEXT_AREA)
20115 it->current_x += it->pixel_width;
20116
20117 if (extra_line_spacing > 0)
20118 {
20119 it->descent += extra_line_spacing;
20120 if (extra_line_spacing > it->max_extra_line_spacing)
20121 it->max_extra_line_spacing = extra_line_spacing;
20122 }
20123
20124 it->max_ascent = max (it->max_ascent, it->ascent);
20125 it->max_descent = max (it->max_descent, it->descent);
20126 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20127 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20128 }
20129
20130 /* EXPORT for RIF:
20131 Output LEN glyphs starting at START at the nominal cursor position.
20132 Advance the nominal cursor over the text. The global variable
20133 updated_window contains the window being updated, updated_row is
20134 the glyph row being updated, and updated_area is the area of that
20135 row being updated. */
20136
20137 void
20138 x_write_glyphs (start, len)
20139 struct glyph *start;
20140 int len;
20141 {
20142 int x, hpos;
20143
20144 xassert (updated_window && updated_row);
20145 BLOCK_INPUT;
20146
20147 /* Write glyphs. */
20148
20149 hpos = start - updated_row->glyphs[updated_area];
20150 x = draw_glyphs (updated_window, output_cursor.x,
20151 updated_row, updated_area,
20152 hpos, hpos + len,
20153 DRAW_NORMAL_TEXT, 0);
20154
20155 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20156 if (updated_area == TEXT_AREA
20157 && updated_window->phys_cursor_on_p
20158 && updated_window->phys_cursor.vpos == output_cursor.vpos
20159 && updated_window->phys_cursor.hpos >= hpos
20160 && updated_window->phys_cursor.hpos < hpos + len)
20161 updated_window->phys_cursor_on_p = 0;
20162
20163 UNBLOCK_INPUT;
20164
20165 /* Advance the output cursor. */
20166 output_cursor.hpos += len;
20167 output_cursor.x = x;
20168 }
20169
20170
20171 /* EXPORT for RIF:
20172 Insert LEN glyphs from START at the nominal cursor position. */
20173
20174 void
20175 x_insert_glyphs (start, len)
20176 struct glyph *start;
20177 int len;
20178 {
20179 struct frame *f;
20180 struct window *w;
20181 int line_height, shift_by_width, shifted_region_width;
20182 struct glyph_row *row;
20183 struct glyph *glyph;
20184 int frame_x, frame_y, hpos;
20185
20186 xassert (updated_window && updated_row);
20187 BLOCK_INPUT;
20188 w = updated_window;
20189 f = XFRAME (WINDOW_FRAME (w));
20190
20191 /* Get the height of the line we are in. */
20192 row = updated_row;
20193 line_height = row->height;
20194
20195 /* Get the width of the glyphs to insert. */
20196 shift_by_width = 0;
20197 for (glyph = start; glyph < start + len; ++glyph)
20198 shift_by_width += glyph->pixel_width;
20199
20200 /* Get the width of the region to shift right. */
20201 shifted_region_width = (window_box_width (w, updated_area)
20202 - output_cursor.x
20203 - shift_by_width);
20204
20205 /* Shift right. */
20206 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20207 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20208
20209 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20210 line_height, shift_by_width);
20211
20212 /* Write the glyphs. */
20213 hpos = start - row->glyphs[updated_area];
20214 draw_glyphs (w, output_cursor.x, row, updated_area,
20215 hpos, hpos + len,
20216 DRAW_NORMAL_TEXT, 0);
20217
20218 /* Advance the output cursor. */
20219 output_cursor.hpos += len;
20220 output_cursor.x += shift_by_width;
20221 UNBLOCK_INPUT;
20222 }
20223
20224
20225 /* EXPORT for RIF:
20226 Erase the current text line from the nominal cursor position
20227 (inclusive) to pixel column TO_X (exclusive). The idea is that
20228 everything from TO_X onward is already erased.
20229
20230 TO_X is a pixel position relative to updated_area of
20231 updated_window. TO_X == -1 means clear to the end of this area. */
20232
20233 void
20234 x_clear_end_of_line (to_x)
20235 int to_x;
20236 {
20237 struct frame *f;
20238 struct window *w = updated_window;
20239 int max_x, min_y, max_y;
20240 int from_x, from_y, to_y;
20241
20242 xassert (updated_window && updated_row);
20243 f = XFRAME (w->frame);
20244
20245 if (updated_row->full_width_p)
20246 max_x = WINDOW_TOTAL_WIDTH (w);
20247 else
20248 max_x = window_box_width (w, updated_area);
20249 max_y = window_text_bottom_y (w);
20250
20251 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20252 of window. For TO_X > 0, truncate to end of drawing area. */
20253 if (to_x == 0)
20254 return;
20255 else if (to_x < 0)
20256 to_x = max_x;
20257 else
20258 to_x = min (to_x, max_x);
20259
20260 to_y = min (max_y, output_cursor.y + updated_row->height);
20261
20262 /* Notice if the cursor will be cleared by this operation. */
20263 if (!updated_row->full_width_p)
20264 notice_overwritten_cursor (w, updated_area,
20265 output_cursor.x, -1,
20266 updated_row->y,
20267 MATRIX_ROW_BOTTOM_Y (updated_row));
20268
20269 from_x = output_cursor.x;
20270
20271 /* Translate to frame coordinates. */
20272 if (updated_row->full_width_p)
20273 {
20274 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20275 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20276 }
20277 else
20278 {
20279 int area_left = window_box_left (w, updated_area);
20280 from_x += area_left;
20281 to_x += area_left;
20282 }
20283
20284 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20285 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20286 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20287
20288 /* Prevent inadvertently clearing to end of the X window. */
20289 if (to_x > from_x && to_y > from_y)
20290 {
20291 BLOCK_INPUT;
20292 rif->clear_frame_area (f, from_x, from_y,
20293 to_x - from_x, to_y - from_y);
20294 UNBLOCK_INPUT;
20295 }
20296 }
20297
20298 #endif /* HAVE_WINDOW_SYSTEM */
20299
20300
20301 \f
20302 /***********************************************************************
20303 Cursor types
20304 ***********************************************************************/
20305
20306 /* Value is the internal representation of the specified cursor type
20307 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20308 of the bar cursor. */
20309
20310 static enum text_cursor_kinds
20311 get_specified_cursor_type (arg, width)
20312 Lisp_Object arg;
20313 int *width;
20314 {
20315 enum text_cursor_kinds type;
20316
20317 if (NILP (arg))
20318 return NO_CURSOR;
20319
20320 if (EQ (arg, Qbox))
20321 return FILLED_BOX_CURSOR;
20322
20323 if (EQ (arg, Qhollow))
20324 return HOLLOW_BOX_CURSOR;
20325
20326 if (EQ (arg, Qbar))
20327 {
20328 *width = 2;
20329 return BAR_CURSOR;
20330 }
20331
20332 if (CONSP (arg)
20333 && EQ (XCAR (arg), Qbar)
20334 && INTEGERP (XCDR (arg))
20335 && XINT (XCDR (arg)) >= 0)
20336 {
20337 *width = XINT (XCDR (arg));
20338 return BAR_CURSOR;
20339 }
20340
20341 if (EQ (arg, Qhbar))
20342 {
20343 *width = 2;
20344 return HBAR_CURSOR;
20345 }
20346
20347 if (CONSP (arg)
20348 && EQ (XCAR (arg), Qhbar)
20349 && INTEGERP (XCDR (arg))
20350 && XINT (XCDR (arg)) >= 0)
20351 {
20352 *width = XINT (XCDR (arg));
20353 return HBAR_CURSOR;
20354 }
20355
20356 /* Treat anything unknown as "hollow box cursor".
20357 It was bad to signal an error; people have trouble fixing
20358 .Xdefaults with Emacs, when it has something bad in it. */
20359 type = HOLLOW_BOX_CURSOR;
20360
20361 return type;
20362 }
20363
20364 /* Set the default cursor types for specified frame. */
20365 void
20366 set_frame_cursor_types (f, arg)
20367 struct frame *f;
20368 Lisp_Object arg;
20369 {
20370 int width;
20371 Lisp_Object tem;
20372
20373 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20374 FRAME_CURSOR_WIDTH (f) = width;
20375
20376 /* By default, set up the blink-off state depending on the on-state. */
20377
20378 tem = Fassoc (arg, Vblink_cursor_alist);
20379 if (!NILP (tem))
20380 {
20381 FRAME_BLINK_OFF_CURSOR (f)
20382 = get_specified_cursor_type (XCDR (tem), &width);
20383 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20384 }
20385 else
20386 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20387 }
20388
20389
20390 /* Return the cursor we want to be displayed in window W. Return
20391 width of bar/hbar cursor through WIDTH arg. Return with
20392 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20393 (i.e. if the `system caret' should track this cursor).
20394
20395 In a mini-buffer window, we want the cursor only to appear if we
20396 are reading input from this window. For the selected window, we
20397 want the cursor type given by the frame parameter or buffer local
20398 setting of cursor-type. If explicitly marked off, draw no cursor.
20399 In all other cases, we want a hollow box cursor. */
20400
20401 static enum text_cursor_kinds
20402 get_window_cursor_type (w, glyph, width, active_cursor)
20403 struct window *w;
20404 struct glyph *glyph;
20405 int *width;
20406 int *active_cursor;
20407 {
20408 struct frame *f = XFRAME (w->frame);
20409 struct buffer *b = XBUFFER (w->buffer);
20410 int cursor_type = DEFAULT_CURSOR;
20411 Lisp_Object alt_cursor;
20412 int non_selected = 0;
20413
20414 *active_cursor = 1;
20415
20416 /* Echo area */
20417 if (cursor_in_echo_area
20418 && FRAME_HAS_MINIBUF_P (f)
20419 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20420 {
20421 if (w == XWINDOW (echo_area_window))
20422 {
20423 *width = FRAME_CURSOR_WIDTH (f);
20424 return FRAME_DESIRED_CURSOR (f);
20425 }
20426
20427 *active_cursor = 0;
20428 non_selected = 1;
20429 }
20430
20431 /* Nonselected window or nonselected frame. */
20432 else if (w != XWINDOW (f->selected_window)
20433 #ifdef HAVE_WINDOW_SYSTEM
20434 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20435 #endif
20436 )
20437 {
20438 *active_cursor = 0;
20439
20440 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20441 return NO_CURSOR;
20442
20443 non_selected = 1;
20444 }
20445
20446 /* Never display a cursor in a window in which cursor-type is nil. */
20447 if (NILP (b->cursor_type))
20448 return NO_CURSOR;
20449
20450 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20451 if (non_selected)
20452 {
20453 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20454 return get_specified_cursor_type (alt_cursor, width);
20455 }
20456
20457 /* Get the normal cursor type for this window. */
20458 if (EQ (b->cursor_type, Qt))
20459 {
20460 cursor_type = FRAME_DESIRED_CURSOR (f);
20461 *width = FRAME_CURSOR_WIDTH (f);
20462 }
20463 else
20464 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20465
20466 /* Use normal cursor if not blinked off. */
20467 if (!w->cursor_off_p)
20468 {
20469 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20470 if (cursor_type == FILLED_BOX_CURSOR)
20471 cursor_type = HOLLOW_BOX_CURSOR;
20472 }
20473 return cursor_type;
20474 }
20475
20476 /* Cursor is blinked off, so determine how to "toggle" it. */
20477
20478 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20479 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20480 return get_specified_cursor_type (XCDR (alt_cursor), width);
20481
20482 /* Then see if frame has specified a specific blink off cursor type. */
20483 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20484 {
20485 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20486 return FRAME_BLINK_OFF_CURSOR (f);
20487 }
20488
20489 #if 0
20490 /* Some people liked having a permanently visible blinking cursor,
20491 while others had very strong opinions against it. So it was
20492 decided to remove it. KFS 2003-09-03 */
20493
20494 /* Finally perform built-in cursor blinking:
20495 filled box <-> hollow box
20496 wide [h]bar <-> narrow [h]bar
20497 narrow [h]bar <-> no cursor
20498 other type <-> no cursor */
20499
20500 if (cursor_type == FILLED_BOX_CURSOR)
20501 return HOLLOW_BOX_CURSOR;
20502
20503 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20504 {
20505 *width = 1;
20506 return cursor_type;
20507 }
20508 #endif
20509
20510 return NO_CURSOR;
20511 }
20512
20513
20514 #ifdef HAVE_WINDOW_SYSTEM
20515
20516 /* Notice when the text cursor of window W has been completely
20517 overwritten by a drawing operation that outputs glyphs in AREA
20518 starting at X0 and ending at X1 in the line starting at Y0 and
20519 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20520 the rest of the line after X0 has been written. Y coordinates
20521 are window-relative. */
20522
20523 static void
20524 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20525 struct window *w;
20526 enum glyph_row_area area;
20527 int x0, y0, x1, y1;
20528 {
20529 int cx0, cx1, cy0, cy1;
20530 struct glyph_row *row;
20531
20532 if (!w->phys_cursor_on_p)
20533 return;
20534 if (area != TEXT_AREA)
20535 return;
20536
20537 if (w->phys_cursor.vpos < 0
20538 || w->phys_cursor.vpos >= w->current_matrix->nrows
20539 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20540 !(row->enabled_p && row->displays_text_p)))
20541 return;
20542
20543 if (row->cursor_in_fringe_p)
20544 {
20545 row->cursor_in_fringe_p = 0;
20546 draw_fringe_bitmap (w, row, 0);
20547 w->phys_cursor_on_p = 0;
20548 return;
20549 }
20550
20551 cx0 = w->phys_cursor.x;
20552 cx1 = cx0 + w->phys_cursor_width;
20553 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20554 return;
20555
20556 /* The cursor image will be completely removed from the
20557 screen if the output area intersects the cursor area in
20558 y-direction. When we draw in [y0 y1[, and some part of
20559 the cursor is at y < y0, that part must have been drawn
20560 before. When scrolling, the cursor is erased before
20561 actually scrolling, so we don't come here. When not
20562 scrolling, the rows above the old cursor row must have
20563 changed, and in this case these rows must have written
20564 over the cursor image.
20565
20566 Likewise if part of the cursor is below y1, with the
20567 exception of the cursor being in the first blank row at
20568 the buffer and window end because update_text_area
20569 doesn't draw that row. (Except when it does, but
20570 that's handled in update_text_area.) */
20571
20572 cy0 = w->phys_cursor.y;
20573 cy1 = cy0 + w->phys_cursor_height;
20574 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20575 return;
20576
20577 w->phys_cursor_on_p = 0;
20578 }
20579
20580 #endif /* HAVE_WINDOW_SYSTEM */
20581
20582 \f
20583 /************************************************************************
20584 Mouse Face
20585 ************************************************************************/
20586
20587 #ifdef HAVE_WINDOW_SYSTEM
20588
20589 /* EXPORT for RIF:
20590 Fix the display of area AREA of overlapping row ROW in window W. */
20591
20592 void
20593 x_fix_overlapping_area (w, row, area)
20594 struct window *w;
20595 struct glyph_row *row;
20596 enum glyph_row_area area;
20597 {
20598 int i, x;
20599
20600 BLOCK_INPUT;
20601
20602 x = 0;
20603 for (i = 0; i < row->used[area];)
20604 {
20605 if (row->glyphs[area][i].overlaps_vertically_p)
20606 {
20607 int start = i, start_x = x;
20608
20609 do
20610 {
20611 x += row->glyphs[area][i].pixel_width;
20612 ++i;
20613 }
20614 while (i < row->used[area]
20615 && row->glyphs[area][i].overlaps_vertically_p);
20616
20617 draw_glyphs (w, start_x, row, area,
20618 start, i,
20619 DRAW_NORMAL_TEXT, 1);
20620 }
20621 else
20622 {
20623 x += row->glyphs[area][i].pixel_width;
20624 ++i;
20625 }
20626 }
20627
20628 UNBLOCK_INPUT;
20629 }
20630
20631
20632 /* EXPORT:
20633 Draw the cursor glyph of window W in glyph row ROW. See the
20634 comment of draw_glyphs for the meaning of HL. */
20635
20636 void
20637 draw_phys_cursor_glyph (w, row, hl)
20638 struct window *w;
20639 struct glyph_row *row;
20640 enum draw_glyphs_face hl;
20641 {
20642 /* If cursor hpos is out of bounds, don't draw garbage. This can
20643 happen in mini-buffer windows when switching between echo area
20644 glyphs and mini-buffer. */
20645 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20646 {
20647 int on_p = w->phys_cursor_on_p;
20648 int x1;
20649 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20650 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20651 hl, 0);
20652 w->phys_cursor_on_p = on_p;
20653
20654 if (hl == DRAW_CURSOR)
20655 w->phys_cursor_width = x1 - w->phys_cursor.x;
20656 /* When we erase the cursor, and ROW is overlapped by other
20657 rows, make sure that these overlapping parts of other rows
20658 are redrawn. */
20659 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20660 {
20661 if (row > w->current_matrix->rows
20662 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20663 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20664
20665 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20666 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20667 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20668 }
20669 }
20670 }
20671
20672
20673 /* EXPORT:
20674 Erase the image of a cursor of window W from the screen. */
20675
20676 void
20677 erase_phys_cursor (w)
20678 struct window *w;
20679 {
20680 struct frame *f = XFRAME (w->frame);
20681 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20682 int hpos = w->phys_cursor.hpos;
20683 int vpos = w->phys_cursor.vpos;
20684 int mouse_face_here_p = 0;
20685 struct glyph_matrix *active_glyphs = w->current_matrix;
20686 struct glyph_row *cursor_row;
20687 struct glyph *cursor_glyph;
20688 enum draw_glyphs_face hl;
20689
20690 /* No cursor displayed or row invalidated => nothing to do on the
20691 screen. */
20692 if (w->phys_cursor_type == NO_CURSOR)
20693 goto mark_cursor_off;
20694
20695 /* VPOS >= active_glyphs->nrows means that window has been resized.
20696 Don't bother to erase the cursor. */
20697 if (vpos >= active_glyphs->nrows)
20698 goto mark_cursor_off;
20699
20700 /* If row containing cursor is marked invalid, there is nothing we
20701 can do. */
20702 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20703 if (!cursor_row->enabled_p)
20704 goto mark_cursor_off;
20705
20706 /* If line spacing is > 0, old cursor may only be partially visible in
20707 window after split-window. So adjust visible height. */
20708 cursor_row->visible_height = min (cursor_row->visible_height,
20709 window_text_bottom_y (w) - cursor_row->y);
20710
20711 /* If row is completely invisible, don't attempt to delete a cursor which
20712 isn't there. This can happen if cursor is at top of a window, and
20713 we switch to a buffer with a header line in that window. */
20714 if (cursor_row->visible_height <= 0)
20715 goto mark_cursor_off;
20716
20717 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20718 if (cursor_row->cursor_in_fringe_p)
20719 {
20720 cursor_row->cursor_in_fringe_p = 0;
20721 draw_fringe_bitmap (w, cursor_row, 0);
20722 goto mark_cursor_off;
20723 }
20724
20725 /* This can happen when the new row is shorter than the old one.
20726 In this case, either draw_glyphs or clear_end_of_line
20727 should have cleared the cursor. Note that we wouldn't be
20728 able to erase the cursor in this case because we don't have a
20729 cursor glyph at hand. */
20730 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20731 goto mark_cursor_off;
20732
20733 /* If the cursor is in the mouse face area, redisplay that when
20734 we clear the cursor. */
20735 if (! NILP (dpyinfo->mouse_face_window)
20736 && w == XWINDOW (dpyinfo->mouse_face_window)
20737 && (vpos > dpyinfo->mouse_face_beg_row
20738 || (vpos == dpyinfo->mouse_face_beg_row
20739 && hpos >= dpyinfo->mouse_face_beg_col))
20740 && (vpos < dpyinfo->mouse_face_end_row
20741 || (vpos == dpyinfo->mouse_face_end_row
20742 && hpos < dpyinfo->mouse_face_end_col))
20743 /* Don't redraw the cursor's spot in mouse face if it is at the
20744 end of a line (on a newline). The cursor appears there, but
20745 mouse highlighting does not. */
20746 && cursor_row->used[TEXT_AREA] > hpos)
20747 mouse_face_here_p = 1;
20748
20749 /* Maybe clear the display under the cursor. */
20750 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20751 {
20752 int x, y;
20753 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20754 int width;
20755
20756 cursor_glyph = get_phys_cursor_glyph (w);
20757 if (cursor_glyph == NULL)
20758 goto mark_cursor_off;
20759
20760 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20761 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20762 width = min (cursor_glyph->pixel_width,
20763 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20764
20765 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20766 }
20767
20768 /* Erase the cursor by redrawing the character underneath it. */
20769 if (mouse_face_here_p)
20770 hl = DRAW_MOUSE_FACE;
20771 else
20772 hl = DRAW_NORMAL_TEXT;
20773 draw_phys_cursor_glyph (w, cursor_row, hl);
20774
20775 mark_cursor_off:
20776 w->phys_cursor_on_p = 0;
20777 w->phys_cursor_type = NO_CURSOR;
20778 }
20779
20780
20781 /* EXPORT:
20782 Display or clear cursor of window W. If ON is zero, clear the
20783 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20784 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20785
20786 void
20787 display_and_set_cursor (w, on, hpos, vpos, x, y)
20788 struct window *w;
20789 int on, hpos, vpos, x, y;
20790 {
20791 struct frame *f = XFRAME (w->frame);
20792 int new_cursor_type;
20793 int new_cursor_width;
20794 int active_cursor;
20795 struct glyph_row *glyph_row;
20796 struct glyph *glyph;
20797
20798 /* This is pointless on invisible frames, and dangerous on garbaged
20799 windows and frames; in the latter case, the frame or window may
20800 be in the midst of changing its size, and x and y may be off the
20801 window. */
20802 if (! FRAME_VISIBLE_P (f)
20803 || FRAME_GARBAGED_P (f)
20804 || vpos >= w->current_matrix->nrows
20805 || hpos >= w->current_matrix->matrix_w)
20806 return;
20807
20808 /* If cursor is off and we want it off, return quickly. */
20809 if (!on && !w->phys_cursor_on_p)
20810 return;
20811
20812 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20813 /* If cursor row is not enabled, we don't really know where to
20814 display the cursor. */
20815 if (!glyph_row->enabled_p)
20816 {
20817 w->phys_cursor_on_p = 0;
20818 return;
20819 }
20820
20821 glyph = NULL;
20822 if (!glyph_row->exact_window_width_line_p
20823 || hpos < glyph_row->used[TEXT_AREA])
20824 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20825
20826 xassert (interrupt_input_blocked);
20827
20828 /* Set new_cursor_type to the cursor we want to be displayed. */
20829 new_cursor_type = get_window_cursor_type (w, glyph,
20830 &new_cursor_width, &active_cursor);
20831
20832 /* If cursor is currently being shown and we don't want it to be or
20833 it is in the wrong place, or the cursor type is not what we want,
20834 erase it. */
20835 if (w->phys_cursor_on_p
20836 && (!on
20837 || w->phys_cursor.x != x
20838 || w->phys_cursor.y != y
20839 || new_cursor_type != w->phys_cursor_type
20840 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20841 && new_cursor_width != w->phys_cursor_width)))
20842 erase_phys_cursor (w);
20843
20844 /* Don't check phys_cursor_on_p here because that flag is only set
20845 to zero in some cases where we know that the cursor has been
20846 completely erased, to avoid the extra work of erasing the cursor
20847 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20848 still not be visible, or it has only been partly erased. */
20849 if (on)
20850 {
20851 w->phys_cursor_ascent = glyph_row->ascent;
20852 w->phys_cursor_height = glyph_row->height;
20853
20854 /* Set phys_cursor_.* before x_draw_.* is called because some
20855 of them may need the information. */
20856 w->phys_cursor.x = x;
20857 w->phys_cursor.y = glyph_row->y;
20858 w->phys_cursor.hpos = hpos;
20859 w->phys_cursor.vpos = vpos;
20860 }
20861
20862 rif->draw_window_cursor (w, glyph_row, x, y,
20863 new_cursor_type, new_cursor_width,
20864 on, active_cursor);
20865 }
20866
20867
20868 /* Switch the display of W's cursor on or off, according to the value
20869 of ON. */
20870
20871 static void
20872 update_window_cursor (w, on)
20873 struct window *w;
20874 int on;
20875 {
20876 /* Don't update cursor in windows whose frame is in the process
20877 of being deleted. */
20878 if (w->current_matrix)
20879 {
20880 BLOCK_INPUT;
20881 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20882 w->phys_cursor.x, w->phys_cursor.y);
20883 UNBLOCK_INPUT;
20884 }
20885 }
20886
20887
20888 /* Call update_window_cursor with parameter ON_P on all leaf windows
20889 in the window tree rooted at W. */
20890
20891 static void
20892 update_cursor_in_window_tree (w, on_p)
20893 struct window *w;
20894 int on_p;
20895 {
20896 while (w)
20897 {
20898 if (!NILP (w->hchild))
20899 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20900 else if (!NILP (w->vchild))
20901 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20902 else
20903 update_window_cursor (w, on_p);
20904
20905 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20906 }
20907 }
20908
20909
20910 /* EXPORT:
20911 Display the cursor on window W, or clear it, according to ON_P.
20912 Don't change the cursor's position. */
20913
20914 void
20915 x_update_cursor (f, on_p)
20916 struct frame *f;
20917 int on_p;
20918 {
20919 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20920 }
20921
20922
20923 /* EXPORT:
20924 Clear the cursor of window W to background color, and mark the
20925 cursor as not shown. This is used when the text where the cursor
20926 is is about to be rewritten. */
20927
20928 void
20929 x_clear_cursor (w)
20930 struct window *w;
20931 {
20932 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20933 update_window_cursor (w, 0);
20934 }
20935
20936
20937 /* EXPORT:
20938 Display the active region described by mouse_face_* according to DRAW. */
20939
20940 void
20941 show_mouse_face (dpyinfo, draw)
20942 Display_Info *dpyinfo;
20943 enum draw_glyphs_face draw;
20944 {
20945 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20946 struct frame *f = XFRAME (WINDOW_FRAME (w));
20947
20948 if (/* If window is in the process of being destroyed, don't bother
20949 to do anything. */
20950 w->current_matrix != NULL
20951 /* Don't update mouse highlight if hidden */
20952 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20953 /* Recognize when we are called to operate on rows that don't exist
20954 anymore. This can happen when a window is split. */
20955 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20956 {
20957 int phys_cursor_on_p = w->phys_cursor_on_p;
20958 struct glyph_row *row, *first, *last;
20959
20960 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20961 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20962
20963 for (row = first; row <= last && row->enabled_p; ++row)
20964 {
20965 int start_hpos, end_hpos, start_x;
20966
20967 /* For all but the first row, the highlight starts at column 0. */
20968 if (row == first)
20969 {
20970 start_hpos = dpyinfo->mouse_face_beg_col;
20971 start_x = dpyinfo->mouse_face_beg_x;
20972 }
20973 else
20974 {
20975 start_hpos = 0;
20976 start_x = 0;
20977 }
20978
20979 if (row == last)
20980 end_hpos = dpyinfo->mouse_face_end_col;
20981 else
20982 end_hpos = row->used[TEXT_AREA];
20983
20984 if (end_hpos > start_hpos)
20985 {
20986 draw_glyphs (w, start_x, row, TEXT_AREA,
20987 start_hpos, end_hpos,
20988 draw, 0);
20989
20990 row->mouse_face_p
20991 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20992 }
20993 }
20994
20995 /* When we've written over the cursor, arrange for it to
20996 be displayed again. */
20997 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20998 {
20999 BLOCK_INPUT;
21000 display_and_set_cursor (w, 1,
21001 w->phys_cursor.hpos, w->phys_cursor.vpos,
21002 w->phys_cursor.x, w->phys_cursor.y);
21003 UNBLOCK_INPUT;
21004 }
21005 }
21006
21007 /* Change the mouse cursor. */
21008 if (draw == DRAW_NORMAL_TEXT)
21009 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21010 else if (draw == DRAW_MOUSE_FACE)
21011 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21012 else
21013 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21014 }
21015
21016 /* EXPORT:
21017 Clear out the mouse-highlighted active region.
21018 Redraw it un-highlighted first. Value is non-zero if mouse
21019 face was actually drawn unhighlighted. */
21020
21021 int
21022 clear_mouse_face (dpyinfo)
21023 Display_Info *dpyinfo;
21024 {
21025 int cleared = 0;
21026
21027 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21028 {
21029 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21030 cleared = 1;
21031 }
21032
21033 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21034 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21035 dpyinfo->mouse_face_window = Qnil;
21036 dpyinfo->mouse_face_overlay = Qnil;
21037 return cleared;
21038 }
21039
21040
21041 /* EXPORT:
21042 Non-zero if physical cursor of window W is within mouse face. */
21043
21044 int
21045 cursor_in_mouse_face_p (w)
21046 struct window *w;
21047 {
21048 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21049 int in_mouse_face = 0;
21050
21051 if (WINDOWP (dpyinfo->mouse_face_window)
21052 && XWINDOW (dpyinfo->mouse_face_window) == w)
21053 {
21054 int hpos = w->phys_cursor.hpos;
21055 int vpos = w->phys_cursor.vpos;
21056
21057 if (vpos >= dpyinfo->mouse_face_beg_row
21058 && vpos <= dpyinfo->mouse_face_end_row
21059 && (vpos > dpyinfo->mouse_face_beg_row
21060 || hpos >= dpyinfo->mouse_face_beg_col)
21061 && (vpos < dpyinfo->mouse_face_end_row
21062 || hpos < dpyinfo->mouse_face_end_col
21063 || dpyinfo->mouse_face_past_end))
21064 in_mouse_face = 1;
21065 }
21066
21067 return in_mouse_face;
21068 }
21069
21070
21071
21072 \f
21073 /* Find the glyph matrix position of buffer position CHARPOS in window
21074 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21075 current glyphs must be up to date. If CHARPOS is above window
21076 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21077 of last line in W. In the row containing CHARPOS, stop before glyphs
21078 having STOP as object. */
21079
21080 #if 1 /* This is a version of fast_find_position that's more correct
21081 in the presence of hscrolling, for example. I didn't install
21082 it right away because the problem fixed is minor, it failed
21083 in 20.x as well, and I think it's too risky to install
21084 so near the release of 21.1. 2001-09-25 gerd. */
21085
21086 static int
21087 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21088 struct window *w;
21089 int charpos;
21090 int *hpos, *vpos, *x, *y;
21091 Lisp_Object stop;
21092 {
21093 struct glyph_row *row, *first;
21094 struct glyph *glyph, *end;
21095 int past_end = 0;
21096
21097 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21098 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21099 {
21100 *x = first->x;
21101 *y = first->y;
21102 *hpos = 0;
21103 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21104 return 1;
21105 }
21106
21107 row = row_containing_pos (w, charpos, first, NULL, 0);
21108 if (row == NULL)
21109 {
21110 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21111 past_end = 1;
21112 }
21113
21114 /* If whole rows or last part of a row came from a display overlay,
21115 row_containing_pos will skip over such rows because their end pos
21116 equals the start pos of the overlay or interval.
21117
21118 Move back if we have a STOP object and previous row's
21119 end glyph came from STOP. */
21120 if (!NILP (stop))
21121 {
21122 struct glyph_row *prev;
21123 while ((prev = row - 1, prev >= first)
21124 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21125 && prev->used[TEXT_AREA] > 0)
21126 {
21127 struct glyph *beg = prev->glyphs[TEXT_AREA];
21128 glyph = beg + prev->used[TEXT_AREA];
21129 while (--glyph >= beg
21130 && INTEGERP (glyph->object));
21131 if (glyph < beg
21132 || !EQ (stop, glyph->object))
21133 break;
21134 row = prev;
21135 }
21136 }
21137
21138 *x = row->x;
21139 *y = row->y;
21140 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21141
21142 glyph = row->glyphs[TEXT_AREA];
21143 end = glyph + row->used[TEXT_AREA];
21144
21145 /* Skip over glyphs not having an object at the start of the row.
21146 These are special glyphs like truncation marks on terminal
21147 frames. */
21148 if (row->displays_text_p)
21149 while (glyph < end
21150 && INTEGERP (glyph->object)
21151 && !EQ (stop, glyph->object)
21152 && glyph->charpos < 0)
21153 {
21154 *x += glyph->pixel_width;
21155 ++glyph;
21156 }
21157
21158 while (glyph < end
21159 && !INTEGERP (glyph->object)
21160 && !EQ (stop, glyph->object)
21161 && (!BUFFERP (glyph->object)
21162 || glyph->charpos < charpos))
21163 {
21164 *x += glyph->pixel_width;
21165 ++glyph;
21166 }
21167
21168 *hpos = glyph - row->glyphs[TEXT_AREA];
21169 return !past_end;
21170 }
21171
21172 #else /* not 1 */
21173
21174 static int
21175 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21176 struct window *w;
21177 int pos;
21178 int *hpos, *vpos, *x, *y;
21179 Lisp_Object stop;
21180 {
21181 int i;
21182 int lastcol;
21183 int maybe_next_line_p = 0;
21184 int line_start_position;
21185 int yb = window_text_bottom_y (w);
21186 struct glyph_row *row, *best_row;
21187 int row_vpos, best_row_vpos;
21188 int current_x;
21189
21190 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21191 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21192
21193 while (row->y < yb)
21194 {
21195 if (row->used[TEXT_AREA])
21196 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21197 else
21198 line_start_position = 0;
21199
21200 if (line_start_position > pos)
21201 break;
21202 /* If the position sought is the end of the buffer,
21203 don't include the blank lines at the bottom of the window. */
21204 else if (line_start_position == pos
21205 && pos == BUF_ZV (XBUFFER (w->buffer)))
21206 {
21207 maybe_next_line_p = 1;
21208 break;
21209 }
21210 else if (line_start_position > 0)
21211 {
21212 best_row = row;
21213 best_row_vpos = row_vpos;
21214 }
21215
21216 if (row->y + row->height >= yb)
21217 break;
21218
21219 ++row;
21220 ++row_vpos;
21221 }
21222
21223 /* Find the right column within BEST_ROW. */
21224 lastcol = 0;
21225 current_x = best_row->x;
21226 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21227 {
21228 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21229 int charpos = glyph->charpos;
21230
21231 if (BUFFERP (glyph->object))
21232 {
21233 if (charpos == pos)
21234 {
21235 *hpos = i;
21236 *vpos = best_row_vpos;
21237 *x = current_x;
21238 *y = best_row->y;
21239 return 1;
21240 }
21241 else if (charpos > pos)
21242 break;
21243 }
21244 else if (EQ (glyph->object, stop))
21245 break;
21246
21247 if (charpos > 0)
21248 lastcol = i;
21249 current_x += glyph->pixel_width;
21250 }
21251
21252 /* If we're looking for the end of the buffer,
21253 and we didn't find it in the line we scanned,
21254 use the start of the following line. */
21255 if (maybe_next_line_p)
21256 {
21257 ++best_row;
21258 ++best_row_vpos;
21259 lastcol = 0;
21260 current_x = best_row->x;
21261 }
21262
21263 *vpos = best_row_vpos;
21264 *hpos = lastcol + 1;
21265 *x = current_x;
21266 *y = best_row->y;
21267 return 0;
21268 }
21269
21270 #endif /* not 1 */
21271
21272
21273 /* Find the position of the glyph for position POS in OBJECT in
21274 window W's current matrix, and return in *X, *Y the pixel
21275 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21276
21277 RIGHT_P non-zero means return the position of the right edge of the
21278 glyph, RIGHT_P zero means return the left edge position.
21279
21280 If no glyph for POS exists in the matrix, return the position of
21281 the glyph with the next smaller position that is in the matrix, if
21282 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21283 exists in the matrix, return the position of the glyph with the
21284 next larger position in OBJECT.
21285
21286 Value is non-zero if a glyph was found. */
21287
21288 static int
21289 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21290 struct window *w;
21291 int pos;
21292 Lisp_Object object;
21293 int *hpos, *vpos, *x, *y;
21294 int right_p;
21295 {
21296 int yb = window_text_bottom_y (w);
21297 struct glyph_row *r;
21298 struct glyph *best_glyph = NULL;
21299 struct glyph_row *best_row = NULL;
21300 int best_x = 0;
21301
21302 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21303 r->enabled_p && r->y < yb;
21304 ++r)
21305 {
21306 struct glyph *g = r->glyphs[TEXT_AREA];
21307 struct glyph *e = g + r->used[TEXT_AREA];
21308 int gx;
21309
21310 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21311 if (EQ (g->object, object))
21312 {
21313 if (g->charpos == pos)
21314 {
21315 best_glyph = g;
21316 best_x = gx;
21317 best_row = r;
21318 goto found;
21319 }
21320 else if (best_glyph == NULL
21321 || ((abs (g->charpos - pos)
21322 < abs (best_glyph->charpos - pos))
21323 && (right_p
21324 ? g->charpos < pos
21325 : g->charpos > pos)))
21326 {
21327 best_glyph = g;
21328 best_x = gx;
21329 best_row = r;
21330 }
21331 }
21332 }
21333
21334 found:
21335
21336 if (best_glyph)
21337 {
21338 *x = best_x;
21339 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21340
21341 if (right_p)
21342 {
21343 *x += best_glyph->pixel_width;
21344 ++*hpos;
21345 }
21346
21347 *y = best_row->y;
21348 *vpos = best_row - w->current_matrix->rows;
21349 }
21350
21351 return best_glyph != NULL;
21352 }
21353
21354
21355 /* See if position X, Y is within a hot-spot of an image. */
21356
21357 static int
21358 on_hot_spot_p (hot_spot, x, y)
21359 Lisp_Object hot_spot;
21360 int x, y;
21361 {
21362 if (!CONSP (hot_spot))
21363 return 0;
21364
21365 if (EQ (XCAR (hot_spot), Qrect))
21366 {
21367 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21368 Lisp_Object rect = XCDR (hot_spot);
21369 Lisp_Object tem;
21370 if (!CONSP (rect))
21371 return 0;
21372 if (!CONSP (XCAR (rect)))
21373 return 0;
21374 if (!CONSP (XCDR (rect)))
21375 return 0;
21376 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21377 return 0;
21378 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21379 return 0;
21380 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21381 return 0;
21382 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21383 return 0;
21384 return 1;
21385 }
21386 else if (EQ (XCAR (hot_spot), Qcircle))
21387 {
21388 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21389 Lisp_Object circ = XCDR (hot_spot);
21390 Lisp_Object lr, lx0, ly0;
21391 if (CONSP (circ)
21392 && CONSP (XCAR (circ))
21393 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21394 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21395 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21396 {
21397 double r = XFLOATINT (lr);
21398 double dx = XINT (lx0) - x;
21399 double dy = XINT (ly0) - y;
21400 return (dx * dx + dy * dy <= r * r);
21401 }
21402 }
21403 else if (EQ (XCAR (hot_spot), Qpoly))
21404 {
21405 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21406 if (VECTORP (XCDR (hot_spot)))
21407 {
21408 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21409 Lisp_Object *poly = v->contents;
21410 int n = v->size;
21411 int i;
21412 int inside = 0;
21413 Lisp_Object lx, ly;
21414 int x0, y0;
21415
21416 /* Need an even number of coordinates, and at least 3 edges. */
21417 if (n < 6 || n & 1)
21418 return 0;
21419
21420 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21421 If count is odd, we are inside polygon. Pixels on edges
21422 may or may not be included depending on actual geometry of the
21423 polygon. */
21424 if ((lx = poly[n-2], !INTEGERP (lx))
21425 || (ly = poly[n-1], !INTEGERP (lx)))
21426 return 0;
21427 x0 = XINT (lx), y0 = XINT (ly);
21428 for (i = 0; i < n; i += 2)
21429 {
21430 int x1 = x0, y1 = y0;
21431 if ((lx = poly[i], !INTEGERP (lx))
21432 || (ly = poly[i+1], !INTEGERP (ly)))
21433 return 0;
21434 x0 = XINT (lx), y0 = XINT (ly);
21435
21436 /* Does this segment cross the X line? */
21437 if (x0 >= x)
21438 {
21439 if (x1 >= x)
21440 continue;
21441 }
21442 else if (x1 < x)
21443 continue;
21444 if (y > y0 && y > y1)
21445 continue;
21446 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21447 inside = !inside;
21448 }
21449 return inside;
21450 }
21451 }
21452 /* If we don't understand the format, pretend we're not in the hot-spot. */
21453 return 0;
21454 }
21455
21456 Lisp_Object
21457 find_hot_spot (map, x, y)
21458 Lisp_Object map;
21459 int x, y;
21460 {
21461 while (CONSP (map))
21462 {
21463 if (CONSP (XCAR (map))
21464 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21465 return XCAR (map);
21466 map = XCDR (map);
21467 }
21468
21469 return Qnil;
21470 }
21471
21472 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21473 3, 3, 0,
21474 doc: /* Lookup in image map MAP coordinates X and Y.
21475 An image map is an alist where each element has the format (AREA ID PLIST).
21476 An AREA is specified as either a rectangle, a circle, or a polygon:
21477 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21478 pixel coordinates of the upper left and bottom right corners.
21479 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21480 and the radius of the circle; r may be a float or integer.
21481 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21482 vector describes one corner in the polygon.
21483 Returns the alist element for the first matching AREA in MAP. */)
21484 (map, x, y)
21485 Lisp_Object map;
21486 Lisp_Object x, y;
21487 {
21488 if (NILP (map))
21489 return Qnil;
21490
21491 CHECK_NUMBER (x);
21492 CHECK_NUMBER (y);
21493
21494 return find_hot_spot (map, XINT (x), XINT (y));
21495 }
21496
21497
21498 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21499 static void
21500 define_frame_cursor1 (f, cursor, pointer)
21501 struct frame *f;
21502 Cursor cursor;
21503 Lisp_Object pointer;
21504 {
21505 /* Do not change cursor shape while dragging mouse. */
21506 if (!NILP (do_mouse_tracking))
21507 return;
21508
21509 if (!NILP (pointer))
21510 {
21511 if (EQ (pointer, Qarrow))
21512 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21513 else if (EQ (pointer, Qhand))
21514 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21515 else if (EQ (pointer, Qtext))
21516 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21517 else if (EQ (pointer, intern ("hdrag")))
21518 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21519 #ifdef HAVE_X_WINDOWS
21520 else if (EQ (pointer, intern ("vdrag")))
21521 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21522 #endif
21523 else if (EQ (pointer, intern ("hourglass")))
21524 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21525 else if (EQ (pointer, Qmodeline))
21526 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21527 else
21528 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21529 }
21530
21531 if (cursor != No_Cursor)
21532 rif->define_frame_cursor (f, cursor);
21533 }
21534
21535 /* Take proper action when mouse has moved to the mode or header line
21536 or marginal area AREA of window W, x-position X and y-position Y.
21537 X is relative to the start of the text display area of W, so the
21538 width of bitmap areas and scroll bars must be subtracted to get a
21539 position relative to the start of the mode line. */
21540
21541 static void
21542 note_mode_line_or_margin_highlight (window, x, y, area)
21543 Lisp_Object window;
21544 int x, y;
21545 enum window_part area;
21546 {
21547 struct window *w = XWINDOW (window);
21548 struct frame *f = XFRAME (w->frame);
21549 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21550 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21551 Lisp_Object pointer = Qnil;
21552 int charpos, dx, dy, width, height;
21553 Lisp_Object string, object = Qnil;
21554 Lisp_Object pos, help;
21555
21556 Lisp_Object mouse_face;
21557 int original_x_pixel = x;
21558 struct glyph * glyph = NULL;
21559 struct glyph_row *row;
21560
21561 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21562 {
21563 int x0;
21564 struct glyph *end;
21565
21566 string = mode_line_string (w, area, &x, &y, &charpos,
21567 &object, &dx, &dy, &width, &height);
21568
21569 row = (area == ON_MODE_LINE
21570 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21571 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21572
21573 /* Find glyph */
21574 if (row->mode_line_p && row->enabled_p)
21575 {
21576 glyph = row->glyphs[TEXT_AREA];
21577 end = glyph + row->used[TEXT_AREA];
21578
21579 for (x0 = original_x_pixel;
21580 glyph < end && x0 >= glyph->pixel_width;
21581 ++glyph)
21582 x0 -= glyph->pixel_width;
21583
21584 if (glyph >= end)
21585 glyph = NULL;
21586 }
21587 }
21588 else
21589 {
21590 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21591 string = marginal_area_string (w, area, &x, &y, &charpos,
21592 &object, &dx, &dy, &width, &height);
21593 }
21594
21595 help = Qnil;
21596
21597 if (IMAGEP (object))
21598 {
21599 Lisp_Object image_map, hotspot;
21600 if ((image_map = Fplist_get (XCDR (object), QCmap),
21601 !NILP (image_map))
21602 && (hotspot = find_hot_spot (image_map, dx, dy),
21603 CONSP (hotspot))
21604 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21605 {
21606 Lisp_Object area_id, plist;
21607
21608 area_id = XCAR (hotspot);
21609 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21610 If so, we could look for mouse-enter, mouse-leave
21611 properties in PLIST (and do something...). */
21612 hotspot = XCDR (hotspot);
21613 if (CONSP (hotspot)
21614 && (plist = XCAR (hotspot), CONSP (plist)))
21615 {
21616 pointer = Fplist_get (plist, Qpointer);
21617 if (NILP (pointer))
21618 pointer = Qhand;
21619 help = Fplist_get (plist, Qhelp_echo);
21620 if (!NILP (help))
21621 {
21622 help_echo_string = help;
21623 /* Is this correct? ++kfs */
21624 XSETWINDOW (help_echo_window, w);
21625 help_echo_object = w->buffer;
21626 help_echo_pos = charpos;
21627 }
21628 }
21629 }
21630 if (NILP (pointer))
21631 pointer = Fplist_get (XCDR (object), QCpointer);
21632 }
21633
21634 if (STRINGP (string))
21635 {
21636 pos = make_number (charpos);
21637 /* If we're on a string with `help-echo' text property, arrange
21638 for the help to be displayed. This is done by setting the
21639 global variable help_echo_string to the help string. */
21640 if (NILP (help))
21641 {
21642 help = Fget_text_property (pos, Qhelp_echo, string);
21643 if (!NILP (help))
21644 {
21645 help_echo_string = help;
21646 XSETWINDOW (help_echo_window, w);
21647 help_echo_object = string;
21648 help_echo_pos = charpos;
21649 }
21650 }
21651
21652 if (NILP (pointer))
21653 pointer = Fget_text_property (pos, Qpointer, string);
21654
21655 /* Change the mouse pointer according to what is under X/Y. */
21656 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21657 {
21658 Lisp_Object map;
21659 map = Fget_text_property (pos, Qlocal_map, string);
21660 if (!KEYMAPP (map))
21661 map = Fget_text_property (pos, Qkeymap, string);
21662 if (!KEYMAPP (map))
21663 cursor = dpyinfo->vertical_scroll_bar_cursor;
21664 }
21665
21666 /* Change the mouse face according to what is under X/Y. */
21667 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21668 if (!NILP (mouse_face)
21669 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21670 && glyph)
21671 {
21672 Lisp_Object b, e;
21673
21674 struct glyph * tmp_glyph;
21675
21676 int gpos;
21677 int gseq_length;
21678 int total_pixel_width;
21679 int ignore;
21680
21681 int vpos, hpos;
21682
21683 b = Fprevious_single_property_change (make_number (charpos + 1),
21684 Qmouse_face, string, Qnil);
21685 if (NILP (b))
21686 b = make_number (0);
21687
21688 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21689 if (NILP (e))
21690 e = make_number (SCHARS (string));
21691
21692 /* Calculate the position(glyph position: GPOS) of GLYPH in
21693 displayed string. GPOS is different from CHARPOS.
21694
21695 CHARPOS is the position of glyph in internal string
21696 object. A mode line string format has structures which
21697 is converted to a flatten by emacs lisp interpreter.
21698 The internal string is an element of the structures.
21699 The displayed string is the flatten string. */
21700 for (tmp_glyph = glyph - 1, gpos = 0;
21701 tmp_glyph->charpos >= XINT (b);
21702 tmp_glyph--, gpos++)
21703 {
21704 if (!EQ (tmp_glyph->object, glyph->object))
21705 break;
21706 }
21707
21708 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21709 displayed string holding GLYPH.
21710
21711 GSEQ_LENGTH is different from SCHARS (STRING).
21712 SCHARS (STRING) returns the length of the internal string. */
21713 for (tmp_glyph = glyph, gseq_length = gpos;
21714 tmp_glyph->charpos < XINT (e);
21715 tmp_glyph++, gseq_length++)
21716 {
21717 if (!EQ (tmp_glyph->object, glyph->object))
21718 break;
21719 }
21720
21721 total_pixel_width = 0;
21722 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21723 total_pixel_width += tmp_glyph->pixel_width;
21724
21725 /* Pre calculation of re-rendering position */
21726 vpos = (x - gpos);
21727 hpos = (area == ON_MODE_LINE
21728 ? (w->current_matrix)->nrows - 1
21729 : 0);
21730
21731 /* If the re-rendering position is included in the last
21732 re-rendering area, we should do nothing. */
21733 if ( EQ (window, dpyinfo->mouse_face_window)
21734 && dpyinfo->mouse_face_beg_col <= vpos
21735 && vpos < dpyinfo->mouse_face_end_col
21736 && dpyinfo->mouse_face_beg_row == hpos )
21737 return;
21738
21739 if (clear_mouse_face (dpyinfo))
21740 cursor = No_Cursor;
21741
21742 dpyinfo->mouse_face_beg_col = vpos;
21743 dpyinfo->mouse_face_beg_row = hpos;
21744
21745 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21746 dpyinfo->mouse_face_beg_y = 0;
21747
21748 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21749 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21750
21751 dpyinfo->mouse_face_end_x = 0;
21752 dpyinfo->mouse_face_end_y = 0;
21753
21754 dpyinfo->mouse_face_past_end = 0;
21755 dpyinfo->mouse_face_window = window;
21756
21757 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21758 charpos,
21759 0, 0, 0, &ignore,
21760 glyph->face_id, 1);
21761 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21762
21763 if (NILP (pointer))
21764 pointer = Qhand;
21765 }
21766 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21767 clear_mouse_face (dpyinfo);
21768 }
21769 define_frame_cursor1 (f, cursor, pointer);
21770 }
21771
21772
21773 /* EXPORT:
21774 Take proper action when the mouse has moved to position X, Y on
21775 frame F as regards highlighting characters that have mouse-face
21776 properties. Also de-highlighting chars where the mouse was before.
21777 X and Y can be negative or out of range. */
21778
21779 void
21780 note_mouse_highlight (f, x, y)
21781 struct frame *f;
21782 int x, y;
21783 {
21784 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21785 enum window_part part;
21786 Lisp_Object window;
21787 struct window *w;
21788 Cursor cursor = No_Cursor;
21789 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21790 struct buffer *b;
21791
21792 /* When a menu is active, don't highlight because this looks odd. */
21793 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21794 if (popup_activated ())
21795 return;
21796 #endif
21797
21798 if (NILP (Vmouse_highlight)
21799 || !f->glyphs_initialized_p)
21800 return;
21801
21802 dpyinfo->mouse_face_mouse_x = x;
21803 dpyinfo->mouse_face_mouse_y = y;
21804 dpyinfo->mouse_face_mouse_frame = f;
21805
21806 if (dpyinfo->mouse_face_defer)
21807 return;
21808
21809 if (gc_in_progress)
21810 {
21811 dpyinfo->mouse_face_deferred_gc = 1;
21812 return;
21813 }
21814
21815 /* Which window is that in? */
21816 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21817
21818 /* If we were displaying active text in another window, clear that.
21819 Also clear if we move out of text area in same window. */
21820 if (! EQ (window, dpyinfo->mouse_face_window)
21821 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21822 && !NILP (dpyinfo->mouse_face_window)))
21823 clear_mouse_face (dpyinfo);
21824
21825 /* Not on a window -> return. */
21826 if (!WINDOWP (window))
21827 return;
21828
21829 /* Reset help_echo_string. It will get recomputed below. */
21830 help_echo_string = Qnil;
21831
21832 /* Convert to window-relative pixel coordinates. */
21833 w = XWINDOW (window);
21834 frame_to_window_pixel_xy (w, &x, &y);
21835
21836 /* Handle tool-bar window differently since it doesn't display a
21837 buffer. */
21838 if (EQ (window, f->tool_bar_window))
21839 {
21840 note_tool_bar_highlight (f, x, y);
21841 return;
21842 }
21843
21844 /* Mouse is on the mode, header line or margin? */
21845 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21846 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21847 {
21848 note_mode_line_or_margin_highlight (window, x, y, part);
21849 return;
21850 }
21851
21852 if (part == ON_VERTICAL_BORDER)
21853 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21854 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21855 || part == ON_SCROLL_BAR)
21856 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21857 else
21858 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21859
21860 /* Are we in a window whose display is up to date?
21861 And verify the buffer's text has not changed. */
21862 b = XBUFFER (w->buffer);
21863 if (part == ON_TEXT
21864 && EQ (w->window_end_valid, w->buffer)
21865 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21866 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21867 {
21868 int hpos, vpos, pos, i, dx, dy, area;
21869 struct glyph *glyph;
21870 Lisp_Object object;
21871 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21872 Lisp_Object *overlay_vec = NULL;
21873 int noverlays;
21874 struct buffer *obuf;
21875 int obegv, ozv, same_region;
21876
21877 /* Find the glyph under X/Y. */
21878 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21879
21880 /* Look for :pointer property on image. */
21881 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21882 {
21883 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21884 if (img != NULL && IMAGEP (img->spec))
21885 {
21886 Lisp_Object image_map, hotspot;
21887 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21888 !NILP (image_map))
21889 && (hotspot = find_hot_spot (image_map,
21890 glyph->slice.x + dx,
21891 glyph->slice.y + dy),
21892 CONSP (hotspot))
21893 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21894 {
21895 Lisp_Object area_id, plist;
21896
21897 area_id = XCAR (hotspot);
21898 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21899 If so, we could look for mouse-enter, mouse-leave
21900 properties in PLIST (and do something...). */
21901 hotspot = XCDR (hotspot);
21902 if (CONSP (hotspot)
21903 && (plist = XCAR (hotspot), CONSP (plist)))
21904 {
21905 pointer = Fplist_get (plist, Qpointer);
21906 if (NILP (pointer))
21907 pointer = Qhand;
21908 help_echo_string = Fplist_get (plist, Qhelp_echo);
21909 if (!NILP (help_echo_string))
21910 {
21911 help_echo_window = window;
21912 help_echo_object = glyph->object;
21913 help_echo_pos = glyph->charpos;
21914 }
21915 }
21916 }
21917 if (NILP (pointer))
21918 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21919 }
21920 }
21921
21922 /* Clear mouse face if X/Y not over text. */
21923 if (glyph == NULL
21924 || area != TEXT_AREA
21925 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21926 {
21927 if (clear_mouse_face (dpyinfo))
21928 cursor = No_Cursor;
21929 if (NILP (pointer))
21930 {
21931 if (area != TEXT_AREA)
21932 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21933 else
21934 pointer = Vvoid_text_area_pointer;
21935 }
21936 goto set_cursor;
21937 }
21938
21939 pos = glyph->charpos;
21940 object = glyph->object;
21941 if (!STRINGP (object) && !BUFFERP (object))
21942 goto set_cursor;
21943
21944 /* If we get an out-of-range value, return now; avoid an error. */
21945 if (BUFFERP (object) && pos > BUF_Z (b))
21946 goto set_cursor;
21947
21948 /* Make the window's buffer temporarily current for
21949 overlays_at and compute_char_face. */
21950 obuf = current_buffer;
21951 current_buffer = b;
21952 obegv = BEGV;
21953 ozv = ZV;
21954 BEGV = BEG;
21955 ZV = Z;
21956
21957 /* Is this char mouse-active or does it have help-echo? */
21958 position = make_number (pos);
21959
21960 if (BUFFERP (object))
21961 {
21962 /* Put all the overlays we want in a vector in overlay_vec. */
21963 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21964 /* Sort overlays into increasing priority order. */
21965 noverlays = sort_overlays (overlay_vec, noverlays, w);
21966 }
21967 else
21968 noverlays = 0;
21969
21970 same_region = (EQ (window, dpyinfo->mouse_face_window)
21971 && vpos >= dpyinfo->mouse_face_beg_row
21972 && vpos <= dpyinfo->mouse_face_end_row
21973 && (vpos > dpyinfo->mouse_face_beg_row
21974 || hpos >= dpyinfo->mouse_face_beg_col)
21975 && (vpos < dpyinfo->mouse_face_end_row
21976 || hpos < dpyinfo->mouse_face_end_col
21977 || dpyinfo->mouse_face_past_end));
21978
21979 if (same_region)
21980 cursor = No_Cursor;
21981
21982 /* Check mouse-face highlighting. */
21983 if (! same_region
21984 /* If there exists an overlay with mouse-face overlapping
21985 the one we are currently highlighting, we have to
21986 check if we enter the overlapping overlay, and then
21987 highlight only that. */
21988 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21989 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21990 {
21991 /* Find the highest priority overlay that has a mouse-face
21992 property. */
21993 overlay = Qnil;
21994 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21995 {
21996 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21997 if (!NILP (mouse_face))
21998 overlay = overlay_vec[i];
21999 }
22000
22001 /* If we're actually highlighting the same overlay as
22002 before, there's no need to do that again. */
22003 if (!NILP (overlay)
22004 && EQ (overlay, dpyinfo->mouse_face_overlay))
22005 goto check_help_echo;
22006
22007 dpyinfo->mouse_face_overlay = overlay;
22008
22009 /* Clear the display of the old active region, if any. */
22010 if (clear_mouse_face (dpyinfo))
22011 cursor = No_Cursor;
22012
22013 /* If no overlay applies, get a text property. */
22014 if (NILP (overlay))
22015 mouse_face = Fget_text_property (position, Qmouse_face, object);
22016
22017 /* Handle the overlay case. */
22018 if (!NILP (overlay))
22019 {
22020 /* Find the range of text around this char that
22021 should be active. */
22022 Lisp_Object before, after;
22023 int ignore;
22024
22025 before = Foverlay_start (overlay);
22026 after = Foverlay_end (overlay);
22027 /* Record this as the current active region. */
22028 fast_find_position (w, XFASTINT (before),
22029 &dpyinfo->mouse_face_beg_col,
22030 &dpyinfo->mouse_face_beg_row,
22031 &dpyinfo->mouse_face_beg_x,
22032 &dpyinfo->mouse_face_beg_y, Qnil);
22033
22034 dpyinfo->mouse_face_past_end
22035 = !fast_find_position (w, XFASTINT (after),
22036 &dpyinfo->mouse_face_end_col,
22037 &dpyinfo->mouse_face_end_row,
22038 &dpyinfo->mouse_face_end_x,
22039 &dpyinfo->mouse_face_end_y, Qnil);
22040 dpyinfo->mouse_face_window = window;
22041
22042 dpyinfo->mouse_face_face_id
22043 = face_at_buffer_position (w, pos, 0, 0,
22044 &ignore, pos + 1,
22045 !dpyinfo->mouse_face_hidden);
22046
22047 /* Display it as active. */
22048 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22049 cursor = No_Cursor;
22050 }
22051 /* Handle the text property case. */
22052 else if (!NILP (mouse_face) && BUFFERP (object))
22053 {
22054 /* Find the range of text around this char that
22055 should be active. */
22056 Lisp_Object before, after, beginning, end;
22057 int ignore;
22058
22059 beginning = Fmarker_position (w->start);
22060 end = make_number (BUF_Z (XBUFFER (object))
22061 - XFASTINT (w->window_end_pos));
22062 before
22063 = Fprevious_single_property_change (make_number (pos + 1),
22064 Qmouse_face,
22065 object, beginning);
22066 after
22067 = Fnext_single_property_change (position, Qmouse_face,
22068 object, end);
22069
22070 /* Record this as the current active region. */
22071 fast_find_position (w, XFASTINT (before),
22072 &dpyinfo->mouse_face_beg_col,
22073 &dpyinfo->mouse_face_beg_row,
22074 &dpyinfo->mouse_face_beg_x,
22075 &dpyinfo->mouse_face_beg_y, Qnil);
22076 dpyinfo->mouse_face_past_end
22077 = !fast_find_position (w, XFASTINT (after),
22078 &dpyinfo->mouse_face_end_col,
22079 &dpyinfo->mouse_face_end_row,
22080 &dpyinfo->mouse_face_end_x,
22081 &dpyinfo->mouse_face_end_y, Qnil);
22082 dpyinfo->mouse_face_window = window;
22083
22084 if (BUFFERP (object))
22085 dpyinfo->mouse_face_face_id
22086 = face_at_buffer_position (w, pos, 0, 0,
22087 &ignore, pos + 1,
22088 !dpyinfo->mouse_face_hidden);
22089
22090 /* Display it as active. */
22091 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22092 cursor = No_Cursor;
22093 }
22094 else if (!NILP (mouse_face) && STRINGP (object))
22095 {
22096 Lisp_Object b, e;
22097 int ignore;
22098
22099 b = Fprevious_single_property_change (make_number (pos + 1),
22100 Qmouse_face,
22101 object, Qnil);
22102 e = Fnext_single_property_change (position, Qmouse_face,
22103 object, Qnil);
22104 if (NILP (b))
22105 b = make_number (0);
22106 if (NILP (e))
22107 e = make_number (SCHARS (object) - 1);
22108
22109 fast_find_string_pos (w, XINT (b), object,
22110 &dpyinfo->mouse_face_beg_col,
22111 &dpyinfo->mouse_face_beg_row,
22112 &dpyinfo->mouse_face_beg_x,
22113 &dpyinfo->mouse_face_beg_y, 0);
22114 fast_find_string_pos (w, XINT (e), object,
22115 &dpyinfo->mouse_face_end_col,
22116 &dpyinfo->mouse_face_end_row,
22117 &dpyinfo->mouse_face_end_x,
22118 &dpyinfo->mouse_face_end_y, 1);
22119 dpyinfo->mouse_face_past_end = 0;
22120 dpyinfo->mouse_face_window = window;
22121 dpyinfo->mouse_face_face_id
22122 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22123 glyph->face_id, 1);
22124 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22125 cursor = No_Cursor;
22126 }
22127 else if (STRINGP (object) && NILP (mouse_face))
22128 {
22129 /* A string which doesn't have mouse-face, but
22130 the text ``under'' it might have. */
22131 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22132 int start = MATRIX_ROW_START_CHARPOS (r);
22133
22134 pos = string_buffer_position (w, object, start);
22135 if (pos > 0)
22136 mouse_face = get_char_property_and_overlay (make_number (pos),
22137 Qmouse_face,
22138 w->buffer,
22139 &overlay);
22140 if (!NILP (mouse_face) && !NILP (overlay))
22141 {
22142 Lisp_Object before = Foverlay_start (overlay);
22143 Lisp_Object after = Foverlay_end (overlay);
22144 int ignore;
22145
22146 /* Note that we might not be able to find position
22147 BEFORE in the glyph matrix if the overlay is
22148 entirely covered by a `display' property. In
22149 this case, we overshoot. So let's stop in
22150 the glyph matrix before glyphs for OBJECT. */
22151 fast_find_position (w, XFASTINT (before),
22152 &dpyinfo->mouse_face_beg_col,
22153 &dpyinfo->mouse_face_beg_row,
22154 &dpyinfo->mouse_face_beg_x,
22155 &dpyinfo->mouse_face_beg_y,
22156 object);
22157
22158 dpyinfo->mouse_face_past_end
22159 = !fast_find_position (w, XFASTINT (after),
22160 &dpyinfo->mouse_face_end_col,
22161 &dpyinfo->mouse_face_end_row,
22162 &dpyinfo->mouse_face_end_x,
22163 &dpyinfo->mouse_face_end_y,
22164 Qnil);
22165 dpyinfo->mouse_face_window = window;
22166 dpyinfo->mouse_face_face_id
22167 = face_at_buffer_position (w, pos, 0, 0,
22168 &ignore, pos + 1,
22169 !dpyinfo->mouse_face_hidden);
22170
22171 /* Display it as active. */
22172 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22173 cursor = No_Cursor;
22174 }
22175 }
22176 }
22177
22178 check_help_echo:
22179
22180 /* Look for a `help-echo' property. */
22181 if (NILP (help_echo_string)) {
22182 Lisp_Object help, overlay;
22183
22184 /* Check overlays first. */
22185 help = overlay = Qnil;
22186 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22187 {
22188 overlay = overlay_vec[i];
22189 help = Foverlay_get (overlay, Qhelp_echo);
22190 }
22191
22192 if (!NILP (help))
22193 {
22194 help_echo_string = help;
22195 help_echo_window = window;
22196 help_echo_object = overlay;
22197 help_echo_pos = pos;
22198 }
22199 else
22200 {
22201 Lisp_Object object = glyph->object;
22202 int charpos = glyph->charpos;
22203
22204 /* Try text properties. */
22205 if (STRINGP (object)
22206 && charpos >= 0
22207 && charpos < SCHARS (object))
22208 {
22209 help = Fget_text_property (make_number (charpos),
22210 Qhelp_echo, object);
22211 if (NILP (help))
22212 {
22213 /* If the string itself doesn't specify a help-echo,
22214 see if the buffer text ``under'' it does. */
22215 struct glyph_row *r
22216 = MATRIX_ROW (w->current_matrix, vpos);
22217 int start = MATRIX_ROW_START_CHARPOS (r);
22218 int pos = string_buffer_position (w, object, start);
22219 if (pos > 0)
22220 {
22221 help = Fget_char_property (make_number (pos),
22222 Qhelp_echo, w->buffer);
22223 if (!NILP (help))
22224 {
22225 charpos = pos;
22226 object = w->buffer;
22227 }
22228 }
22229 }
22230 }
22231 else if (BUFFERP (object)
22232 && charpos >= BEGV
22233 && charpos < ZV)
22234 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22235 object);
22236
22237 if (!NILP (help))
22238 {
22239 help_echo_string = help;
22240 help_echo_window = window;
22241 help_echo_object = object;
22242 help_echo_pos = charpos;
22243 }
22244 }
22245 }
22246
22247 /* Look for a `pointer' property. */
22248 if (NILP (pointer))
22249 {
22250 /* Check overlays first. */
22251 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22252 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22253
22254 if (NILP (pointer))
22255 {
22256 Lisp_Object object = glyph->object;
22257 int charpos = glyph->charpos;
22258
22259 /* Try text properties. */
22260 if (STRINGP (object)
22261 && charpos >= 0
22262 && charpos < SCHARS (object))
22263 {
22264 pointer = Fget_text_property (make_number (charpos),
22265 Qpointer, object);
22266 if (NILP (pointer))
22267 {
22268 /* If the string itself doesn't specify a pointer,
22269 see if the buffer text ``under'' it does. */
22270 struct glyph_row *r
22271 = MATRIX_ROW (w->current_matrix, vpos);
22272 int start = MATRIX_ROW_START_CHARPOS (r);
22273 int pos = string_buffer_position (w, object, start);
22274 if (pos > 0)
22275 pointer = Fget_char_property (make_number (pos),
22276 Qpointer, w->buffer);
22277 }
22278 }
22279 else if (BUFFERP (object)
22280 && charpos >= BEGV
22281 && charpos < ZV)
22282 pointer = Fget_text_property (make_number (charpos),
22283 Qpointer, object);
22284 }
22285 }
22286
22287 BEGV = obegv;
22288 ZV = ozv;
22289 current_buffer = obuf;
22290 }
22291
22292 set_cursor:
22293
22294 define_frame_cursor1 (f, cursor, pointer);
22295 }
22296
22297
22298 /* EXPORT for RIF:
22299 Clear any mouse-face on window W. This function is part of the
22300 redisplay interface, and is called from try_window_id and similar
22301 functions to ensure the mouse-highlight is off. */
22302
22303 void
22304 x_clear_window_mouse_face (w)
22305 struct window *w;
22306 {
22307 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22308 Lisp_Object window;
22309
22310 BLOCK_INPUT;
22311 XSETWINDOW (window, w);
22312 if (EQ (window, dpyinfo->mouse_face_window))
22313 clear_mouse_face (dpyinfo);
22314 UNBLOCK_INPUT;
22315 }
22316
22317
22318 /* EXPORT:
22319 Just discard the mouse face information for frame F, if any.
22320 This is used when the size of F is changed. */
22321
22322 void
22323 cancel_mouse_face (f)
22324 struct frame *f;
22325 {
22326 Lisp_Object window;
22327 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22328
22329 window = dpyinfo->mouse_face_window;
22330 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22331 {
22332 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22333 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22334 dpyinfo->mouse_face_window = Qnil;
22335 }
22336 }
22337
22338
22339 #endif /* HAVE_WINDOW_SYSTEM */
22340
22341 \f
22342 /***********************************************************************
22343 Exposure Events
22344 ***********************************************************************/
22345
22346 #ifdef HAVE_WINDOW_SYSTEM
22347
22348 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22349 which intersects rectangle R. R is in window-relative coordinates. */
22350
22351 static void
22352 expose_area (w, row, r, area)
22353 struct window *w;
22354 struct glyph_row *row;
22355 XRectangle *r;
22356 enum glyph_row_area area;
22357 {
22358 struct glyph *first = row->glyphs[area];
22359 struct glyph *end = row->glyphs[area] + row->used[area];
22360 struct glyph *last;
22361 int first_x, start_x, x;
22362
22363 if (area == TEXT_AREA && row->fill_line_p)
22364 /* If row extends face to end of line write the whole line. */
22365 draw_glyphs (w, 0, row, area,
22366 0, row->used[area],
22367 DRAW_NORMAL_TEXT, 0);
22368 else
22369 {
22370 /* Set START_X to the window-relative start position for drawing glyphs of
22371 AREA. The first glyph of the text area can be partially visible.
22372 The first glyphs of other areas cannot. */
22373 start_x = window_box_left_offset (w, area);
22374 x = start_x;
22375 if (area == TEXT_AREA)
22376 x += row->x;
22377
22378 /* Find the first glyph that must be redrawn. */
22379 while (first < end
22380 && x + first->pixel_width < r->x)
22381 {
22382 x += first->pixel_width;
22383 ++first;
22384 }
22385
22386 /* Find the last one. */
22387 last = first;
22388 first_x = x;
22389 while (last < end
22390 && x < r->x + r->width)
22391 {
22392 x += last->pixel_width;
22393 ++last;
22394 }
22395
22396 /* Repaint. */
22397 if (last > first)
22398 draw_glyphs (w, first_x - start_x, row, area,
22399 first - row->glyphs[area], last - row->glyphs[area],
22400 DRAW_NORMAL_TEXT, 0);
22401 }
22402 }
22403
22404
22405 /* Redraw the parts of the glyph row ROW on window W intersecting
22406 rectangle R. R is in window-relative coordinates. Value is
22407 non-zero if mouse-face was overwritten. */
22408
22409 static int
22410 expose_line (w, row, r)
22411 struct window *w;
22412 struct glyph_row *row;
22413 XRectangle *r;
22414 {
22415 xassert (row->enabled_p);
22416
22417 if (row->mode_line_p || w->pseudo_window_p)
22418 draw_glyphs (w, 0, row, TEXT_AREA,
22419 0, row->used[TEXT_AREA],
22420 DRAW_NORMAL_TEXT, 0);
22421 else
22422 {
22423 if (row->used[LEFT_MARGIN_AREA])
22424 expose_area (w, row, r, LEFT_MARGIN_AREA);
22425 if (row->used[TEXT_AREA])
22426 expose_area (w, row, r, TEXT_AREA);
22427 if (row->used[RIGHT_MARGIN_AREA])
22428 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22429 draw_row_fringe_bitmaps (w, row);
22430 }
22431
22432 return row->mouse_face_p;
22433 }
22434
22435
22436 /* Redraw those parts of glyphs rows during expose event handling that
22437 overlap other rows. Redrawing of an exposed line writes over parts
22438 of lines overlapping that exposed line; this function fixes that.
22439
22440 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22441 row in W's current matrix that is exposed and overlaps other rows.
22442 LAST_OVERLAPPING_ROW is the last such row. */
22443
22444 static void
22445 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22446 struct window *w;
22447 struct glyph_row *first_overlapping_row;
22448 struct glyph_row *last_overlapping_row;
22449 {
22450 struct glyph_row *row;
22451
22452 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22453 if (row->overlapping_p)
22454 {
22455 xassert (row->enabled_p && !row->mode_line_p);
22456
22457 if (row->used[LEFT_MARGIN_AREA])
22458 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22459
22460 if (row->used[TEXT_AREA])
22461 x_fix_overlapping_area (w, row, TEXT_AREA);
22462
22463 if (row->used[RIGHT_MARGIN_AREA])
22464 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22465 }
22466 }
22467
22468
22469 /* Return non-zero if W's cursor intersects rectangle R. */
22470
22471 static int
22472 phys_cursor_in_rect_p (w, r)
22473 struct window *w;
22474 XRectangle *r;
22475 {
22476 XRectangle cr, result;
22477 struct glyph *cursor_glyph;
22478
22479 cursor_glyph = get_phys_cursor_glyph (w);
22480 if (cursor_glyph)
22481 {
22482 /* r is relative to W's box, but w->phys_cursor.x is relative
22483 to left edge of W's TEXT area. Adjust it. */
22484 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22485 cr.y = w->phys_cursor.y;
22486 cr.width = cursor_glyph->pixel_width;
22487 cr.height = w->phys_cursor_height;
22488 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22489 I assume the effect is the same -- and this is portable. */
22490 return x_intersect_rectangles (&cr, r, &result);
22491 }
22492 else
22493 return 0;
22494 }
22495
22496
22497 /* EXPORT:
22498 Draw a vertical window border to the right of window W if W doesn't
22499 have vertical scroll bars. */
22500
22501 void
22502 x_draw_vertical_border (w)
22503 struct window *w;
22504 {
22505 /* We could do better, if we knew what type of scroll-bar the adjacent
22506 windows (on either side) have... But we don't :-(
22507 However, I think this works ok. ++KFS 2003-04-25 */
22508
22509 /* Redraw borders between horizontally adjacent windows. Don't
22510 do it for frames with vertical scroll bars because either the
22511 right scroll bar of a window, or the left scroll bar of its
22512 neighbor will suffice as a border. */
22513 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22514 return;
22515
22516 if (!WINDOW_RIGHTMOST_P (w)
22517 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22518 {
22519 int x0, x1, y0, y1;
22520
22521 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22522 y1 -= 1;
22523
22524 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22525 x1 -= 1;
22526
22527 rif->draw_vertical_window_border (w, x1, y0, y1);
22528 }
22529 else if (!WINDOW_LEFTMOST_P (w)
22530 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22531 {
22532 int x0, x1, y0, y1;
22533
22534 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22535 y1 -= 1;
22536
22537 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22538 x0 -= 1;
22539
22540 rif->draw_vertical_window_border (w, x0, y0, y1);
22541 }
22542 }
22543
22544
22545 /* Redraw the part of window W intersection rectangle FR. Pixel
22546 coordinates in FR are frame-relative. Call this function with
22547 input blocked. Value is non-zero if the exposure overwrites
22548 mouse-face. */
22549
22550 static int
22551 expose_window (w, fr)
22552 struct window *w;
22553 XRectangle *fr;
22554 {
22555 struct frame *f = XFRAME (w->frame);
22556 XRectangle wr, r;
22557 int mouse_face_overwritten_p = 0;
22558
22559 /* If window is not yet fully initialized, do nothing. This can
22560 happen when toolkit scroll bars are used and a window is split.
22561 Reconfiguring the scroll bar will generate an expose for a newly
22562 created window. */
22563 if (w->current_matrix == NULL)
22564 return 0;
22565
22566 /* When we're currently updating the window, display and current
22567 matrix usually don't agree. Arrange for a thorough display
22568 later. */
22569 if (w == updated_window)
22570 {
22571 SET_FRAME_GARBAGED (f);
22572 return 0;
22573 }
22574
22575 /* Frame-relative pixel rectangle of W. */
22576 wr.x = WINDOW_LEFT_EDGE_X (w);
22577 wr.y = WINDOW_TOP_EDGE_Y (w);
22578 wr.width = WINDOW_TOTAL_WIDTH (w);
22579 wr.height = WINDOW_TOTAL_HEIGHT (w);
22580
22581 if (x_intersect_rectangles (fr, &wr, &r))
22582 {
22583 int yb = window_text_bottom_y (w);
22584 struct glyph_row *row;
22585 int cursor_cleared_p;
22586 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22587
22588 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22589 r.x, r.y, r.width, r.height));
22590
22591 /* Convert to window coordinates. */
22592 r.x -= WINDOW_LEFT_EDGE_X (w);
22593 r.y -= WINDOW_TOP_EDGE_Y (w);
22594
22595 /* Turn off the cursor. */
22596 if (!w->pseudo_window_p
22597 && phys_cursor_in_rect_p (w, &r))
22598 {
22599 x_clear_cursor (w);
22600 cursor_cleared_p = 1;
22601 }
22602 else
22603 cursor_cleared_p = 0;
22604
22605 /* Update lines intersecting rectangle R. */
22606 first_overlapping_row = last_overlapping_row = NULL;
22607 for (row = w->current_matrix->rows;
22608 row->enabled_p;
22609 ++row)
22610 {
22611 int y0 = row->y;
22612 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22613
22614 if ((y0 >= r.y && y0 < r.y + r.height)
22615 || (y1 > r.y && y1 < r.y + r.height)
22616 || (r.y >= y0 && r.y < y1)
22617 || (r.y + r.height > y0 && r.y + r.height < y1))
22618 {
22619 /* A header line may be overlapping, but there is no need
22620 to fix overlapping areas for them. KFS 2005-02-12 */
22621 if (row->overlapping_p && !row->mode_line_p)
22622 {
22623 if (first_overlapping_row == NULL)
22624 first_overlapping_row = row;
22625 last_overlapping_row = row;
22626 }
22627
22628 if (expose_line (w, row, &r))
22629 mouse_face_overwritten_p = 1;
22630 }
22631
22632 if (y1 >= yb)
22633 break;
22634 }
22635
22636 /* Display the mode line if there is one. */
22637 if (WINDOW_WANTS_MODELINE_P (w)
22638 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22639 row->enabled_p)
22640 && row->y < r.y + r.height)
22641 {
22642 if (expose_line (w, row, &r))
22643 mouse_face_overwritten_p = 1;
22644 }
22645
22646 if (!w->pseudo_window_p)
22647 {
22648 /* Fix the display of overlapping rows. */
22649 if (first_overlapping_row)
22650 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22651
22652 /* Draw border between windows. */
22653 x_draw_vertical_border (w);
22654
22655 /* Turn the cursor on again. */
22656 if (cursor_cleared_p)
22657 update_window_cursor (w, 1);
22658 }
22659 }
22660
22661 return mouse_face_overwritten_p;
22662 }
22663
22664
22665
22666 /* Redraw (parts) of all windows in the window tree rooted at W that
22667 intersect R. R contains frame pixel coordinates. Value is
22668 non-zero if the exposure overwrites mouse-face. */
22669
22670 static int
22671 expose_window_tree (w, r)
22672 struct window *w;
22673 XRectangle *r;
22674 {
22675 struct frame *f = XFRAME (w->frame);
22676 int mouse_face_overwritten_p = 0;
22677
22678 while (w && !FRAME_GARBAGED_P (f))
22679 {
22680 if (!NILP (w->hchild))
22681 mouse_face_overwritten_p
22682 |= expose_window_tree (XWINDOW (w->hchild), r);
22683 else if (!NILP (w->vchild))
22684 mouse_face_overwritten_p
22685 |= expose_window_tree (XWINDOW (w->vchild), r);
22686 else
22687 mouse_face_overwritten_p |= expose_window (w, r);
22688
22689 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22690 }
22691
22692 return mouse_face_overwritten_p;
22693 }
22694
22695
22696 /* EXPORT:
22697 Redisplay an exposed area of frame F. X and Y are the upper-left
22698 corner of the exposed rectangle. W and H are width and height of
22699 the exposed area. All are pixel values. W or H zero means redraw
22700 the entire frame. */
22701
22702 void
22703 expose_frame (f, x, y, w, h)
22704 struct frame *f;
22705 int x, y, w, h;
22706 {
22707 XRectangle r;
22708 int mouse_face_overwritten_p = 0;
22709
22710 TRACE ((stderr, "expose_frame "));
22711
22712 /* No need to redraw if frame will be redrawn soon. */
22713 if (FRAME_GARBAGED_P (f))
22714 {
22715 TRACE ((stderr, " garbaged\n"));
22716 return;
22717 }
22718
22719 /* If basic faces haven't been realized yet, there is no point in
22720 trying to redraw anything. This can happen when we get an expose
22721 event while Emacs is starting, e.g. by moving another window. */
22722 if (FRAME_FACE_CACHE (f) == NULL
22723 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22724 {
22725 TRACE ((stderr, " no faces\n"));
22726 return;
22727 }
22728
22729 if (w == 0 || h == 0)
22730 {
22731 r.x = r.y = 0;
22732 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22733 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22734 }
22735 else
22736 {
22737 r.x = x;
22738 r.y = y;
22739 r.width = w;
22740 r.height = h;
22741 }
22742
22743 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22744 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22745
22746 if (WINDOWP (f->tool_bar_window))
22747 mouse_face_overwritten_p
22748 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22749
22750 #ifdef HAVE_X_WINDOWS
22751 #ifndef MSDOS
22752 #ifndef USE_X_TOOLKIT
22753 if (WINDOWP (f->menu_bar_window))
22754 mouse_face_overwritten_p
22755 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22756 #endif /* not USE_X_TOOLKIT */
22757 #endif
22758 #endif
22759
22760 /* Some window managers support a focus-follows-mouse style with
22761 delayed raising of frames. Imagine a partially obscured frame,
22762 and moving the mouse into partially obscured mouse-face on that
22763 frame. The visible part of the mouse-face will be highlighted,
22764 then the WM raises the obscured frame. With at least one WM, KDE
22765 2.1, Emacs is not getting any event for the raising of the frame
22766 (even tried with SubstructureRedirectMask), only Expose events.
22767 These expose events will draw text normally, i.e. not
22768 highlighted. Which means we must redo the highlight here.
22769 Subsume it under ``we love X''. --gerd 2001-08-15 */
22770 /* Included in Windows version because Windows most likely does not
22771 do the right thing if any third party tool offers
22772 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22773 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22774 {
22775 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22776 if (f == dpyinfo->mouse_face_mouse_frame)
22777 {
22778 int x = dpyinfo->mouse_face_mouse_x;
22779 int y = dpyinfo->mouse_face_mouse_y;
22780 clear_mouse_face (dpyinfo);
22781 note_mouse_highlight (f, x, y);
22782 }
22783 }
22784 }
22785
22786
22787 /* EXPORT:
22788 Determine the intersection of two rectangles R1 and R2. Return
22789 the intersection in *RESULT. Value is non-zero if RESULT is not
22790 empty. */
22791
22792 int
22793 x_intersect_rectangles (r1, r2, result)
22794 XRectangle *r1, *r2, *result;
22795 {
22796 XRectangle *left, *right;
22797 XRectangle *upper, *lower;
22798 int intersection_p = 0;
22799
22800 /* Rearrange so that R1 is the left-most rectangle. */
22801 if (r1->x < r2->x)
22802 left = r1, right = r2;
22803 else
22804 left = r2, right = r1;
22805
22806 /* X0 of the intersection is right.x0, if this is inside R1,
22807 otherwise there is no intersection. */
22808 if (right->x <= left->x + left->width)
22809 {
22810 result->x = right->x;
22811
22812 /* The right end of the intersection is the minimum of the
22813 the right ends of left and right. */
22814 result->width = (min (left->x + left->width, right->x + right->width)
22815 - result->x);
22816
22817 /* Same game for Y. */
22818 if (r1->y < r2->y)
22819 upper = r1, lower = r2;
22820 else
22821 upper = r2, lower = r1;
22822
22823 /* The upper end of the intersection is lower.y0, if this is inside
22824 of upper. Otherwise, there is no intersection. */
22825 if (lower->y <= upper->y + upper->height)
22826 {
22827 result->y = lower->y;
22828
22829 /* The lower end of the intersection is the minimum of the lower
22830 ends of upper and lower. */
22831 result->height = (min (lower->y + lower->height,
22832 upper->y + upper->height)
22833 - result->y);
22834 intersection_p = 1;
22835 }
22836 }
22837
22838 return intersection_p;
22839 }
22840
22841 #endif /* HAVE_WINDOW_SYSTEM */
22842
22843 \f
22844 /***********************************************************************
22845 Initialization
22846 ***********************************************************************/
22847
22848 void
22849 syms_of_xdisp ()
22850 {
22851 Vwith_echo_area_save_vector = Qnil;
22852 staticpro (&Vwith_echo_area_save_vector);
22853
22854 Vmessage_stack = Qnil;
22855 staticpro (&Vmessage_stack);
22856
22857 Qinhibit_redisplay = intern ("inhibit-redisplay");
22858 staticpro (&Qinhibit_redisplay);
22859
22860 message_dolog_marker1 = Fmake_marker ();
22861 staticpro (&message_dolog_marker1);
22862 message_dolog_marker2 = Fmake_marker ();
22863 staticpro (&message_dolog_marker2);
22864 message_dolog_marker3 = Fmake_marker ();
22865 staticpro (&message_dolog_marker3);
22866
22867 #if GLYPH_DEBUG
22868 defsubr (&Sdump_frame_glyph_matrix);
22869 defsubr (&Sdump_glyph_matrix);
22870 defsubr (&Sdump_glyph_row);
22871 defsubr (&Sdump_tool_bar_row);
22872 defsubr (&Strace_redisplay);
22873 defsubr (&Strace_to_stderr);
22874 #endif
22875 #ifdef HAVE_WINDOW_SYSTEM
22876 defsubr (&Stool_bar_lines_needed);
22877 defsubr (&Slookup_image_map);
22878 #endif
22879 defsubr (&Sformat_mode_line);
22880
22881 staticpro (&Qmenu_bar_update_hook);
22882 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22883
22884 staticpro (&Qoverriding_terminal_local_map);
22885 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22886
22887 staticpro (&Qoverriding_local_map);
22888 Qoverriding_local_map = intern ("overriding-local-map");
22889
22890 staticpro (&Qwindow_scroll_functions);
22891 Qwindow_scroll_functions = intern ("window-scroll-functions");
22892
22893 staticpro (&Qredisplay_end_trigger_functions);
22894 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22895
22896 staticpro (&Qinhibit_point_motion_hooks);
22897 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22898
22899 QCdata = intern (":data");
22900 staticpro (&QCdata);
22901 Qdisplay = intern ("display");
22902 staticpro (&Qdisplay);
22903 Qspace_width = intern ("space-width");
22904 staticpro (&Qspace_width);
22905 Qraise = intern ("raise");
22906 staticpro (&Qraise);
22907 Qslice = intern ("slice");
22908 staticpro (&Qslice);
22909 Qspace = intern ("space");
22910 staticpro (&Qspace);
22911 Qmargin = intern ("margin");
22912 staticpro (&Qmargin);
22913 Qpointer = intern ("pointer");
22914 staticpro (&Qpointer);
22915 Qleft_margin = intern ("left-margin");
22916 staticpro (&Qleft_margin);
22917 Qright_margin = intern ("right-margin");
22918 staticpro (&Qright_margin);
22919 Qcenter = intern ("center");
22920 staticpro (&Qcenter);
22921 Qline_height = intern ("line-height");
22922 staticpro (&Qline_height);
22923 QCalign_to = intern (":align-to");
22924 staticpro (&QCalign_to);
22925 QCrelative_width = intern (":relative-width");
22926 staticpro (&QCrelative_width);
22927 QCrelative_height = intern (":relative-height");
22928 staticpro (&QCrelative_height);
22929 QCeval = intern (":eval");
22930 staticpro (&QCeval);
22931 QCpropertize = intern (":propertize");
22932 staticpro (&QCpropertize);
22933 QCfile = intern (":file");
22934 staticpro (&QCfile);
22935 Qfontified = intern ("fontified");
22936 staticpro (&Qfontified);
22937 Qfontification_functions = intern ("fontification-functions");
22938 staticpro (&Qfontification_functions);
22939 Qtrailing_whitespace = intern ("trailing-whitespace");
22940 staticpro (&Qtrailing_whitespace);
22941 Qescape_glyph = intern ("escape-glyph");
22942 staticpro (&Qescape_glyph);
22943 Qnobreak_space = intern ("nobreak-space");
22944 staticpro (&Qnobreak_space);
22945 Qimage = intern ("image");
22946 staticpro (&Qimage);
22947 QCmap = intern (":map");
22948 staticpro (&QCmap);
22949 QCpointer = intern (":pointer");
22950 staticpro (&QCpointer);
22951 Qrect = intern ("rect");
22952 staticpro (&Qrect);
22953 Qcircle = intern ("circle");
22954 staticpro (&Qcircle);
22955 Qpoly = intern ("poly");
22956 staticpro (&Qpoly);
22957 Qmessage_truncate_lines = intern ("message-truncate-lines");
22958 staticpro (&Qmessage_truncate_lines);
22959 Qgrow_only = intern ("grow-only");
22960 staticpro (&Qgrow_only);
22961 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22962 staticpro (&Qinhibit_menubar_update);
22963 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22964 staticpro (&Qinhibit_eval_during_redisplay);
22965 Qposition = intern ("position");
22966 staticpro (&Qposition);
22967 Qbuffer_position = intern ("buffer-position");
22968 staticpro (&Qbuffer_position);
22969 Qobject = intern ("object");
22970 staticpro (&Qobject);
22971 Qbar = intern ("bar");
22972 staticpro (&Qbar);
22973 Qhbar = intern ("hbar");
22974 staticpro (&Qhbar);
22975 Qbox = intern ("box");
22976 staticpro (&Qbox);
22977 Qhollow = intern ("hollow");
22978 staticpro (&Qhollow);
22979 Qhand = intern ("hand");
22980 staticpro (&Qhand);
22981 Qarrow = intern ("arrow");
22982 staticpro (&Qarrow);
22983 Qtext = intern ("text");
22984 staticpro (&Qtext);
22985 Qrisky_local_variable = intern ("risky-local-variable");
22986 staticpro (&Qrisky_local_variable);
22987 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22988 staticpro (&Qinhibit_free_realized_faces);
22989
22990 list_of_error = Fcons (Fcons (intern ("error"),
22991 Fcons (intern ("void-variable"), Qnil)),
22992 Qnil);
22993 staticpro (&list_of_error);
22994
22995 Qlast_arrow_position = intern ("last-arrow-position");
22996 staticpro (&Qlast_arrow_position);
22997 Qlast_arrow_string = intern ("last-arrow-string");
22998 staticpro (&Qlast_arrow_string);
22999
23000 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23001 staticpro (&Qoverlay_arrow_string);
23002 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23003 staticpro (&Qoverlay_arrow_bitmap);
23004
23005 echo_buffer[0] = echo_buffer[1] = Qnil;
23006 staticpro (&echo_buffer[0]);
23007 staticpro (&echo_buffer[1]);
23008
23009 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23010 staticpro (&echo_area_buffer[0]);
23011 staticpro (&echo_area_buffer[1]);
23012
23013 Vmessages_buffer_name = build_string ("*Messages*");
23014 staticpro (&Vmessages_buffer_name);
23015
23016 mode_line_proptrans_alist = Qnil;
23017 staticpro (&mode_line_proptrans_alist);
23018 mode_line_string_list = Qnil;
23019 staticpro (&mode_line_string_list);
23020 mode_line_string_face = Qnil;
23021 staticpro (&mode_line_string_face);
23022 mode_line_string_face_prop = Qnil;
23023 staticpro (&mode_line_string_face_prop);
23024 Vmode_line_unwind_vector = Qnil;
23025 staticpro (&Vmode_line_unwind_vector);
23026
23027 help_echo_string = Qnil;
23028 staticpro (&help_echo_string);
23029 help_echo_object = Qnil;
23030 staticpro (&help_echo_object);
23031 help_echo_window = Qnil;
23032 staticpro (&help_echo_window);
23033 previous_help_echo_string = Qnil;
23034 staticpro (&previous_help_echo_string);
23035 help_echo_pos = -1;
23036
23037 #ifdef HAVE_WINDOW_SYSTEM
23038 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23039 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23040 For example, if a block cursor is over a tab, it will be drawn as
23041 wide as that tab on the display. */);
23042 x_stretch_cursor_p = 0;
23043 #endif
23044
23045 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23046 doc: /* *Non-nil means highlight trailing whitespace.
23047 The face used for trailing whitespace is `trailing-whitespace'. */);
23048 Vshow_trailing_whitespace = Qnil;
23049
23050 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23051 doc: /* *Control highlighting of nobreak space and soft hyphen.
23052 A value of t means highlight the character itself (for nobreak space,
23053 use face `nobreak-space').
23054 A value of nil means no highlighting.
23055 Other values mean display the escape glyph followed by an ordinary
23056 space or ordinary hyphen. */);
23057 Vnobreak_char_display = Qt;
23058
23059 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23060 doc: /* *The pointer shape to show in void text areas.
23061 A value of nil means to show the text pointer. Other options are `arrow',
23062 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23063 Vvoid_text_area_pointer = Qarrow;
23064
23065 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23066 doc: /* Non-nil means don't actually do any redisplay.
23067 This is used for internal purposes. */);
23068 Vinhibit_redisplay = Qnil;
23069
23070 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23071 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23072 Vglobal_mode_string = Qnil;
23073
23074 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23075 doc: /* Marker for where to display an arrow on top of the buffer text.
23076 This must be the beginning of a line in order to work.
23077 See also `overlay-arrow-string'. */);
23078 Voverlay_arrow_position = Qnil;
23079
23080 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23081 doc: /* String to display as an arrow in non-window frames.
23082 See also `overlay-arrow-position'. */);
23083 Voverlay_arrow_string = build_string ("=>");
23084
23085 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23086 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23087 The symbols on this list are examined during redisplay to determine
23088 where to display overlay arrows. */);
23089 Voverlay_arrow_variable_list
23090 = Fcons (intern ("overlay-arrow-position"), Qnil);
23091
23092 DEFVAR_INT ("scroll-step", &scroll_step,
23093 doc: /* *The number of lines to try scrolling a window by when point moves out.
23094 If that fails to bring point back on frame, point is centered instead.
23095 If this is zero, point is always centered after it moves off frame.
23096 If you want scrolling to always be a line at a time, you should set
23097 `scroll-conservatively' to a large value rather than set this to 1. */);
23098
23099 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23100 doc: /* *Scroll up to this many lines, to bring point back on screen.
23101 A value of zero means to scroll the text to center point vertically
23102 in the window. */);
23103 scroll_conservatively = 0;
23104
23105 DEFVAR_INT ("scroll-margin", &scroll_margin,
23106 doc: /* *Number of lines of margin at the top and bottom of a window.
23107 Recenter the window whenever point gets within this many lines
23108 of the top or bottom of the window. */);
23109 scroll_margin = 0;
23110
23111 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23112 doc: /* Pixels per inch value for non-window system displays.
23113 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23114 Vdisplay_pixels_per_inch = make_float (72.0);
23115
23116 #if GLYPH_DEBUG
23117 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23118 #endif
23119
23120 DEFVAR_BOOL ("truncate-partial-width-windows",
23121 &truncate_partial_width_windows,
23122 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23123 truncate_partial_width_windows = 1;
23124
23125 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23126 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23127 Any other value means to use the appropriate face, `mode-line',
23128 `header-line', or `menu' respectively. */);
23129 mode_line_inverse_video = 1;
23130
23131 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23132 doc: /* *Maximum buffer size for which line number should be displayed.
23133 If the buffer is bigger than this, the line number does not appear
23134 in the mode line. A value of nil means no limit. */);
23135 Vline_number_display_limit = Qnil;
23136
23137 DEFVAR_INT ("line-number-display-limit-width",
23138 &line_number_display_limit_width,
23139 doc: /* *Maximum line width (in characters) for line number display.
23140 If the average length of the lines near point is bigger than this, then the
23141 line number may be omitted from the mode line. */);
23142 line_number_display_limit_width = 200;
23143
23144 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23145 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23146 highlight_nonselected_windows = 0;
23147
23148 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23149 doc: /* Non-nil if more than one frame is visible on this display.
23150 Minibuffer-only frames don't count, but iconified frames do.
23151 This variable is not guaranteed to be accurate except while processing
23152 `frame-title-format' and `icon-title-format'. */);
23153
23154 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23155 doc: /* Template for displaying the title bar of visible frames.
23156 \(Assuming the window manager supports this feature.)
23157 This variable has the same structure as `mode-line-format' (which see),
23158 and is used only on frames for which no explicit name has been set
23159 \(see `modify-frame-parameters'). */);
23160
23161 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23162 doc: /* Template for displaying the title bar of an iconified frame.
23163 \(Assuming the window manager supports this feature.)
23164 This variable has the same structure as `mode-line-format' (which see),
23165 and is used only on frames for which no explicit name has been set
23166 \(see `modify-frame-parameters'). */);
23167 Vicon_title_format
23168 = Vframe_title_format
23169 = Fcons (intern ("multiple-frames"),
23170 Fcons (build_string ("%b"),
23171 Fcons (Fcons (empty_string,
23172 Fcons (intern ("invocation-name"),
23173 Fcons (build_string ("@"),
23174 Fcons (intern ("system-name"),
23175 Qnil)))),
23176 Qnil)));
23177
23178 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23179 doc: /* Maximum number of lines to keep in the message log buffer.
23180 If nil, disable message logging. If t, log messages but don't truncate
23181 the buffer when it becomes large. */);
23182 Vmessage_log_max = make_number (50);
23183
23184 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23185 doc: /* Functions called before redisplay, if window sizes have changed.
23186 The value should be a list of functions that take one argument.
23187 Just before redisplay, for each frame, if any of its windows have changed
23188 size since the last redisplay, or have been split or deleted,
23189 all the functions in the list are called, with the frame as argument. */);
23190 Vwindow_size_change_functions = Qnil;
23191
23192 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23193 doc: /* List of functions to call before redisplaying a window with scrolling.
23194 Each function is called with two arguments, the window
23195 and its new display-start position. Note that the value of `window-end'
23196 is not valid when these functions are called. */);
23197 Vwindow_scroll_functions = Qnil;
23198
23199 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23200 doc: /* Functions called when redisplay of a window reaches the end trigger.
23201 Each function is called with two arguments, the window and the end trigger value.
23202 See `set-window-redisplay-end-trigger'. */);
23203 Vredisplay_end_trigger_functions = Qnil;
23204
23205 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23206 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23207 mouse_autoselect_window = 0;
23208
23209 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23210 doc: /* *Non-nil means automatically resize tool-bars.
23211 This increases a tool-bar's height if not all tool-bar items are visible.
23212 It decreases a tool-bar's height when it would display blank lines
23213 otherwise. */);
23214 auto_resize_tool_bars_p = 1;
23215
23216 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23217 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23218 auto_raise_tool_bar_buttons_p = 1;
23219
23220 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23221 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23222 make_cursor_line_fully_visible_p = 1;
23223
23224 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23225 doc: /* *Margin around tool-bar buttons in pixels.
23226 If an integer, use that for both horizontal and vertical margins.
23227 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23228 HORZ specifying the horizontal margin, and VERT specifying the
23229 vertical margin. */);
23230 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23231
23232 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23233 doc: /* *Relief thickness of tool-bar buttons. */);
23234 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23235
23236 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23237 doc: /* List of functions to call to fontify regions of text.
23238 Each function is called with one argument POS. Functions must
23239 fontify a region starting at POS in the current buffer, and give
23240 fontified regions the property `fontified'. */);
23241 Vfontification_functions = Qnil;
23242 Fmake_variable_buffer_local (Qfontification_functions);
23243
23244 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23245 &unibyte_display_via_language_environment,
23246 doc: /* *Non-nil means display unibyte text according to language environment.
23247 Specifically this means that unibyte non-ASCII characters
23248 are displayed by converting them to the equivalent multibyte characters
23249 according to the current language environment. As a result, they are
23250 displayed according to the current fontset. */);
23251 unibyte_display_via_language_environment = 0;
23252
23253 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23254 doc: /* *Maximum height for resizing mini-windows.
23255 If a float, it specifies a fraction of the mini-window frame's height.
23256 If an integer, it specifies a number of lines. */);
23257 Vmax_mini_window_height = make_float (0.25);
23258
23259 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23260 doc: /* *How to resize mini-windows.
23261 A value of nil means don't automatically resize mini-windows.
23262 A value of t means resize them to fit the text displayed in them.
23263 A value of `grow-only', the default, means let mini-windows grow
23264 only, until their display becomes empty, at which point the windows
23265 go back to their normal size. */);
23266 Vresize_mini_windows = Qgrow_only;
23267
23268 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23269 doc: /* Alist specifying how to blink the cursor off.
23270 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23271 `cursor-type' frame-parameter or variable equals ON-STATE,
23272 comparing using `equal', Emacs uses OFF-STATE to specify
23273 how to blink it off. */);
23274 Vblink_cursor_alist = Qnil;
23275
23276 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23277 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23278 automatic_hscrolling_p = 1;
23279
23280 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23281 doc: /* *How many columns away from the window edge point is allowed to get
23282 before automatic hscrolling will horizontally scroll the window. */);
23283 hscroll_margin = 5;
23284
23285 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23286 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23287 When point is less than `automatic-hscroll-margin' columns from the window
23288 edge, automatic hscrolling will scroll the window by the amount of columns
23289 determined by this variable. If its value is a positive integer, scroll that
23290 many columns. If it's a positive floating-point number, it specifies the
23291 fraction of the window's width to scroll. If it's nil or zero, point will be
23292 centered horizontally after the scroll. Any other value, including negative
23293 numbers, are treated as if the value were zero.
23294
23295 Automatic hscrolling always moves point outside the scroll margin, so if
23296 point was more than scroll step columns inside the margin, the window will
23297 scroll more than the value given by the scroll step.
23298
23299 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23300 and `scroll-right' overrides this variable's effect. */);
23301 Vhscroll_step = make_number (0);
23302
23303 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23304 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23305 Bind this around calls to `message' to let it take effect. */);
23306 message_truncate_lines = 0;
23307
23308 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23309 doc: /* Normal hook run to update the menu bar definitions.
23310 Redisplay runs this hook before it redisplays the menu bar.
23311 This is used to update submenus such as Buffers,
23312 whose contents depend on various data. */);
23313 Vmenu_bar_update_hook = Qnil;
23314
23315 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23316 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23317 inhibit_menubar_update = 0;
23318
23319 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23320 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23321 inhibit_eval_during_redisplay = 0;
23322
23323 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23324 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23325 inhibit_free_realized_faces = 0;
23326
23327 #if GLYPH_DEBUG
23328 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23329 doc: /* Inhibit try_window_id display optimization. */);
23330 inhibit_try_window_id = 0;
23331
23332 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23333 doc: /* Inhibit try_window_reusing display optimization. */);
23334 inhibit_try_window_reusing = 0;
23335
23336 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23337 doc: /* Inhibit try_cursor_movement display optimization. */);
23338 inhibit_try_cursor_movement = 0;
23339 #endif /* GLYPH_DEBUG */
23340 }
23341
23342
23343 /* Initialize this module when Emacs starts. */
23344
23345 void
23346 init_xdisp ()
23347 {
23348 Lisp_Object root_window;
23349 struct window *mini_w;
23350
23351 current_header_line_height = current_mode_line_height = -1;
23352
23353 CHARPOS (this_line_start_pos) = 0;
23354
23355 mini_w = XWINDOW (minibuf_window);
23356 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23357
23358 if (!noninteractive)
23359 {
23360 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23361 int i;
23362
23363 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23364 set_window_height (root_window,
23365 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23366 0);
23367 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23368 set_window_height (minibuf_window, 1, 0);
23369
23370 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23371 mini_w->total_cols = make_number (FRAME_COLS (f));
23372
23373 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23374 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23375 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23376
23377 /* The default ellipsis glyphs `...'. */
23378 for (i = 0; i < 3; ++i)
23379 default_invis_vector[i] = make_number ('.');
23380 }
23381
23382 {
23383 /* Allocate the buffer for frame titles.
23384 Also used for `format-mode-line'. */
23385 int size = 100;
23386 mode_line_noprop_buf = (char *) xmalloc (size);
23387 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23388 mode_line_noprop_ptr = mode_line_noprop_buf;
23389 mode_line_target = MODE_LINE_DISPLAY;
23390 }
23391
23392 help_echo_showing_p = 0;
23393 }
23394
23395
23396 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23397 (do not change this comment) */