]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(add_menu_item): If unicode_append_menu returns an
[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 object = it->w->buffer;
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 (CONSP (prop)
3453 /* Simple properties. */
3454 && !EQ (XCAR (prop), Qimage)
3455 && !EQ (XCAR (prop), Qspace)
3456 && !EQ (XCAR (prop), Qwhen)
3457 && !EQ (XCAR (prop), Qslice)
3458 && !EQ (XCAR (prop), Qspace_width)
3459 && !EQ (XCAR (prop), Qheight)
3460 && !EQ (XCAR (prop), Qraise)
3461 /* Marginal area specifications. */
3462 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3463 && !EQ (XCAR (prop), Qleft_fringe)
3464 && !EQ (XCAR (prop), Qright_fringe)
3465 && !NILP (XCAR (prop)))
3466 {
3467 for (; CONSP (prop); prop = XCDR (prop))
3468 {
3469 if (handle_single_display_spec (it, XCAR (prop), object,
3470 position, display_replaced_p))
3471 display_replaced_p = 1;
3472 }
3473 }
3474 else if (VECTORP (prop))
3475 {
3476 int i;
3477 for (i = 0; i < ASIZE (prop); ++i)
3478 if (handle_single_display_spec (it, AREF (prop, i), object,
3479 position, display_replaced_p))
3480 display_replaced_p = 1;
3481 }
3482 else
3483 {
3484 int ret = handle_single_display_spec (it, prop, object, position, 0);
3485 if (ret < 0) /* Replaced by "", i.e. nothing. */
3486 return HANDLED_RECOMPUTE_PROPS;
3487 if (ret)
3488 display_replaced_p = 1;
3489 }
3490
3491 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3492 }
3493
3494
3495 /* Value is the position of the end of the `display' property starting
3496 at START_POS in OBJECT. */
3497
3498 static struct text_pos
3499 display_prop_end (it, object, start_pos)
3500 struct it *it;
3501 Lisp_Object object;
3502 struct text_pos start_pos;
3503 {
3504 Lisp_Object end;
3505 struct text_pos end_pos;
3506
3507 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3508 Qdisplay, object, Qnil);
3509 CHARPOS (end_pos) = XFASTINT (end);
3510 if (STRINGP (object))
3511 compute_string_pos (&end_pos, start_pos, it->string);
3512 else
3513 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3514
3515 return end_pos;
3516 }
3517
3518
3519 /* Set up IT from a single `display' specification PROP. OBJECT
3520 is the object in which the `display' property was found. *POSITION
3521 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3522 means that we previously saw a display specification which already
3523 replaced text display with something else, for example an image;
3524 we ignore such properties after the first one has been processed.
3525
3526 If PROP is a `space' or `image' specification, and in some other
3527 cases too, set *POSITION to the position where the `display'
3528 property ends.
3529
3530 Value is non-zero if something was found which replaces the display
3531 of buffer or string text. Specifically, the value is -1 if that
3532 "something" is "nothing". */
3533
3534 static int
3535 handle_single_display_spec (it, spec, object, position,
3536 display_replaced_before_p)
3537 struct it *it;
3538 Lisp_Object spec;
3539 Lisp_Object object;
3540 struct text_pos *position;
3541 int display_replaced_before_p;
3542 {
3543 Lisp_Object form;
3544 Lisp_Object location, value;
3545 struct text_pos start_pos;
3546 int valid_p;
3547
3548 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3549 If the result is non-nil, use VALUE instead of SPEC. */
3550 form = Qt;
3551 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3552 {
3553 spec = XCDR (spec);
3554 if (!CONSP (spec))
3555 return 0;
3556 form = XCAR (spec);
3557 spec = XCDR (spec);
3558 }
3559
3560 if (!NILP (form) && !EQ (form, Qt))
3561 {
3562 int count = SPECPDL_INDEX ();
3563 struct gcpro gcpro1;
3564
3565 /* Bind `object' to the object having the `display' property, a
3566 buffer or string. Bind `position' to the position in the
3567 object where the property was found, and `buffer-position'
3568 to the current position in the buffer. */
3569 specbind (Qobject, object);
3570 specbind (Qposition, make_number (CHARPOS (*position)));
3571 specbind (Qbuffer_position,
3572 make_number (STRINGP (object)
3573 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3574 GCPRO1 (form);
3575 form = safe_eval (form);
3576 UNGCPRO;
3577 unbind_to (count, Qnil);
3578 }
3579
3580 if (NILP (form))
3581 return 0;
3582
3583 /* Handle `(height HEIGHT)' specifications. */
3584 if (CONSP (spec)
3585 && EQ (XCAR (spec), Qheight)
3586 && CONSP (XCDR (spec)))
3587 {
3588 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3589 return 0;
3590
3591 it->font_height = XCAR (XCDR (spec));
3592 if (!NILP (it->font_height))
3593 {
3594 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3595 int new_height = -1;
3596
3597 if (CONSP (it->font_height)
3598 && (EQ (XCAR (it->font_height), Qplus)
3599 || EQ (XCAR (it->font_height), Qminus))
3600 && CONSP (XCDR (it->font_height))
3601 && INTEGERP (XCAR (XCDR (it->font_height))))
3602 {
3603 /* `(+ N)' or `(- N)' where N is an integer. */
3604 int steps = XINT (XCAR (XCDR (it->font_height)));
3605 if (EQ (XCAR (it->font_height), Qplus))
3606 steps = - steps;
3607 it->face_id = smaller_face (it->f, it->face_id, steps);
3608 }
3609 else if (FUNCTIONP (it->font_height))
3610 {
3611 /* Call function with current height as argument.
3612 Value is the new height. */
3613 Lisp_Object height;
3614 height = safe_call1 (it->font_height,
3615 face->lface[LFACE_HEIGHT_INDEX]);
3616 if (NUMBERP (height))
3617 new_height = XFLOATINT (height);
3618 }
3619 else if (NUMBERP (it->font_height))
3620 {
3621 /* Value is a multiple of the canonical char height. */
3622 struct face *face;
3623
3624 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3625 new_height = (XFLOATINT (it->font_height)
3626 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3627 }
3628 else
3629 {
3630 /* Evaluate IT->font_height with `height' bound to the
3631 current specified height to get the new height. */
3632 int count = SPECPDL_INDEX ();
3633
3634 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3635 value = safe_eval (it->font_height);
3636 unbind_to (count, Qnil);
3637
3638 if (NUMBERP (value))
3639 new_height = XFLOATINT (value);
3640 }
3641
3642 if (new_height > 0)
3643 it->face_id = face_with_height (it->f, it->face_id, new_height);
3644 }
3645
3646 return 0;
3647 }
3648
3649 /* Handle `(space_width WIDTH)'. */
3650 if (CONSP (spec)
3651 && EQ (XCAR (spec), Qspace_width)
3652 && CONSP (XCDR (spec)))
3653 {
3654 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3655 return 0;
3656
3657 value = XCAR (XCDR (spec));
3658 if (NUMBERP (value) && XFLOATINT (value) > 0)
3659 it->space_width = value;
3660
3661 return 0;
3662 }
3663
3664 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3665 if (CONSP (spec)
3666 && EQ (XCAR (spec), Qslice))
3667 {
3668 Lisp_Object tem;
3669
3670 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3671 return 0;
3672
3673 if (tem = XCDR (spec), CONSP (tem))
3674 {
3675 it->slice.x = XCAR (tem);
3676 if (tem = XCDR (tem), CONSP (tem))
3677 {
3678 it->slice.y = XCAR (tem);
3679 if (tem = XCDR (tem), CONSP (tem))
3680 {
3681 it->slice.width = XCAR (tem);
3682 if (tem = XCDR (tem), CONSP (tem))
3683 it->slice.height = XCAR (tem);
3684 }
3685 }
3686 }
3687
3688 return 0;
3689 }
3690
3691 /* Handle `(raise FACTOR)'. */
3692 if (CONSP (spec)
3693 && EQ (XCAR (spec), Qraise)
3694 && CONSP (XCDR (spec)))
3695 {
3696 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3697 return 0;
3698
3699 #ifdef HAVE_WINDOW_SYSTEM
3700 value = XCAR (XCDR (spec));
3701 if (NUMBERP (value))
3702 {
3703 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3704 it->voffset = - (XFLOATINT (value)
3705 * (FONT_HEIGHT (face->font)));
3706 }
3707 #endif /* HAVE_WINDOW_SYSTEM */
3708
3709 return 0;
3710 }
3711
3712 /* Don't handle the other kinds of display specifications
3713 inside a string that we got from a `display' property. */
3714 if (it->string_from_display_prop_p)
3715 return 0;
3716
3717 /* Characters having this form of property are not displayed, so
3718 we have to find the end of the property. */
3719 start_pos = *position;
3720 *position = display_prop_end (it, object, start_pos);
3721 value = Qnil;
3722
3723 /* Stop the scan at that end position--we assume that all
3724 text properties change there. */
3725 it->stop_charpos = position->charpos;
3726
3727 /* Handle `(left-fringe BITMAP [FACE])'
3728 and `(right-fringe BITMAP [FACE])'. */
3729 if (CONSP (spec)
3730 && (EQ (XCAR (spec), Qleft_fringe)
3731 || EQ (XCAR (spec), Qright_fringe))
3732 && CONSP (XCDR (spec)))
3733 {
3734 int face_id = DEFAULT_FACE_ID;
3735 int fringe_bitmap;
3736
3737 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3738 /* If we return here, POSITION has been advanced
3739 across the text with this property. */
3740 return 0;
3741
3742 #ifdef HAVE_WINDOW_SYSTEM
3743 value = XCAR (XCDR (spec));
3744 if (!SYMBOLP (value)
3745 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3746 /* If we return here, POSITION has been advanced
3747 across the text with this property. */
3748 return 0;
3749
3750 if (CONSP (XCDR (XCDR (spec))))
3751 {
3752 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3753 int face_id2 = lookup_derived_face (it->f, face_name,
3754 'A', FRINGE_FACE_ID, 0);
3755 if (face_id2 >= 0)
3756 face_id = face_id2;
3757 }
3758
3759 /* Save current settings of IT so that we can restore them
3760 when we are finished with the glyph property value. */
3761
3762 push_it (it);
3763
3764 it->area = TEXT_AREA;
3765 it->what = IT_IMAGE;
3766 it->image_id = -1; /* no image */
3767 it->position = start_pos;
3768 it->object = NILP (object) ? it->w->buffer : object;
3769 it->method = GET_FROM_IMAGE;
3770 it->face_id = face_id;
3771
3772 /* Say that we haven't consumed the characters with
3773 `display' property yet. The call to pop_it in
3774 set_iterator_to_next will clean this up. */
3775 *position = start_pos;
3776
3777 if (EQ (XCAR (spec), Qleft_fringe))
3778 {
3779 it->left_user_fringe_bitmap = fringe_bitmap;
3780 it->left_user_fringe_face_id = face_id;
3781 }
3782 else
3783 {
3784 it->right_user_fringe_bitmap = fringe_bitmap;
3785 it->right_user_fringe_face_id = face_id;
3786 }
3787 #endif /* HAVE_WINDOW_SYSTEM */
3788 return 1;
3789 }
3790
3791 /* Prepare to handle `((margin left-margin) ...)',
3792 `((margin right-margin) ...)' and `((margin nil) ...)'
3793 prefixes for display specifications. */
3794 location = Qunbound;
3795 if (CONSP (spec) && CONSP (XCAR (spec)))
3796 {
3797 Lisp_Object tem;
3798
3799 value = XCDR (spec);
3800 if (CONSP (value))
3801 value = XCAR (value);
3802
3803 tem = XCAR (spec);
3804 if (EQ (XCAR (tem), Qmargin)
3805 && (tem = XCDR (tem),
3806 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3807 (NILP (tem)
3808 || EQ (tem, Qleft_margin)
3809 || EQ (tem, Qright_margin))))
3810 location = tem;
3811 }
3812
3813 if (EQ (location, Qunbound))
3814 {
3815 location = Qnil;
3816 value = spec;
3817 }
3818
3819 /* After this point, VALUE is the property after any
3820 margin prefix has been stripped. It must be a string,
3821 an image specification, or `(space ...)'.
3822
3823 LOCATION specifies where to display: `left-margin',
3824 `right-margin' or nil. */
3825
3826 valid_p = (STRINGP (value)
3827 #ifdef HAVE_WINDOW_SYSTEM
3828 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3829 #endif /* not HAVE_WINDOW_SYSTEM */
3830 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3831
3832 if (valid_p && !display_replaced_before_p)
3833 {
3834 /* Save current settings of IT so that we can restore them
3835 when we are finished with the glyph property value. */
3836 push_it (it);
3837
3838 if (NILP (location))
3839 it->area = TEXT_AREA;
3840 else if (EQ (location, Qleft_margin))
3841 it->area = LEFT_MARGIN_AREA;
3842 else
3843 it->area = RIGHT_MARGIN_AREA;
3844
3845 if (STRINGP (value))
3846 {
3847 if (SCHARS (value) == 0)
3848 {
3849 pop_it (it);
3850 return -1; /* Replaced by "", i.e. nothing. */
3851 }
3852 it->string = value;
3853 it->multibyte_p = STRING_MULTIBYTE (it->string);
3854 it->current.overlay_string_index = -1;
3855 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3856 it->end_charpos = it->string_nchars = SCHARS (it->string);
3857 it->method = GET_FROM_STRING;
3858 it->stop_charpos = 0;
3859 it->string_from_display_prop_p = 1;
3860 /* Say that we haven't consumed the characters with
3861 `display' property yet. The call to pop_it in
3862 set_iterator_to_next will clean this up. */
3863 *position = start_pos;
3864 }
3865 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3866 {
3867 it->method = GET_FROM_STRETCH;
3868 it->object = value;
3869 it->current.pos = it->position = start_pos;
3870 }
3871 #ifdef HAVE_WINDOW_SYSTEM
3872 else
3873 {
3874 it->what = IT_IMAGE;
3875 it->image_id = lookup_image (it->f, value);
3876 it->position = start_pos;
3877 it->object = NILP (object) ? it->w->buffer : object;
3878 it->method = GET_FROM_IMAGE;
3879
3880 /* Say that we haven't consumed the characters with
3881 `display' property yet. The call to pop_it in
3882 set_iterator_to_next will clean this up. */
3883 *position = start_pos;
3884 }
3885 #endif /* HAVE_WINDOW_SYSTEM */
3886
3887 return 1;
3888 }
3889
3890 /* Invalid property or property not supported. Restore
3891 POSITION to what it was before. */
3892 *position = start_pos;
3893 return 0;
3894 }
3895
3896
3897 /* Check if SPEC is a display specification value whose text should be
3898 treated as intangible. */
3899
3900 static int
3901 single_display_spec_intangible_p (prop)
3902 Lisp_Object prop;
3903 {
3904 /* Skip over `when FORM'. */
3905 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3906 {
3907 prop = XCDR (prop);
3908 if (!CONSP (prop))
3909 return 0;
3910 prop = XCDR (prop);
3911 }
3912
3913 if (STRINGP (prop))
3914 return 1;
3915
3916 if (!CONSP (prop))
3917 return 0;
3918
3919 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3920 we don't need to treat text as intangible. */
3921 if (EQ (XCAR (prop), Qmargin))
3922 {
3923 prop = XCDR (prop);
3924 if (!CONSP (prop))
3925 return 0;
3926
3927 prop = XCDR (prop);
3928 if (!CONSP (prop)
3929 || EQ (XCAR (prop), Qleft_margin)
3930 || EQ (XCAR (prop), Qright_margin))
3931 return 0;
3932 }
3933
3934 return (CONSP (prop)
3935 && (EQ (XCAR (prop), Qimage)
3936 || EQ (XCAR (prop), Qspace)));
3937 }
3938
3939
3940 /* Check if PROP is a display property value whose text should be
3941 treated as intangible. */
3942
3943 int
3944 display_prop_intangible_p (prop)
3945 Lisp_Object prop;
3946 {
3947 if (CONSP (prop)
3948 && CONSP (XCAR (prop))
3949 && !EQ (Qmargin, XCAR (XCAR (prop))))
3950 {
3951 /* A list of sub-properties. */
3952 while (CONSP (prop))
3953 {
3954 if (single_display_spec_intangible_p (XCAR (prop)))
3955 return 1;
3956 prop = XCDR (prop);
3957 }
3958 }
3959 else if (VECTORP (prop))
3960 {
3961 /* A vector of sub-properties. */
3962 int i;
3963 for (i = 0; i < ASIZE (prop); ++i)
3964 if (single_display_spec_intangible_p (AREF (prop, i)))
3965 return 1;
3966 }
3967 else
3968 return single_display_spec_intangible_p (prop);
3969
3970 return 0;
3971 }
3972
3973
3974 /* Return 1 if PROP is a display sub-property value containing STRING. */
3975
3976 static int
3977 single_display_spec_string_p (prop, string)
3978 Lisp_Object prop, string;
3979 {
3980 if (EQ (string, prop))
3981 return 1;
3982
3983 /* Skip over `when FORM'. */
3984 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3985 {
3986 prop = XCDR (prop);
3987 if (!CONSP (prop))
3988 return 0;
3989 prop = XCDR (prop);
3990 }
3991
3992 if (CONSP (prop))
3993 /* Skip over `margin LOCATION'. */
3994 if (EQ (XCAR (prop), Qmargin))
3995 {
3996 prop = XCDR (prop);
3997 if (!CONSP (prop))
3998 return 0;
3999
4000 prop = XCDR (prop);
4001 if (!CONSP (prop))
4002 return 0;
4003 }
4004
4005 return CONSP (prop) && EQ (XCAR (prop), string);
4006 }
4007
4008
4009 /* Return 1 if STRING appears in the `display' property PROP. */
4010
4011 static int
4012 display_prop_string_p (prop, string)
4013 Lisp_Object prop, string;
4014 {
4015 if (CONSP (prop)
4016 && CONSP (XCAR (prop))
4017 && !EQ (Qmargin, XCAR (XCAR (prop))))
4018 {
4019 /* A list of sub-properties. */
4020 while (CONSP (prop))
4021 {
4022 if (single_display_spec_string_p (XCAR (prop), string))
4023 return 1;
4024 prop = XCDR (prop);
4025 }
4026 }
4027 else if (VECTORP (prop))
4028 {
4029 /* A vector of sub-properties. */
4030 int i;
4031 for (i = 0; i < ASIZE (prop); ++i)
4032 if (single_display_spec_string_p (AREF (prop, i), string))
4033 return 1;
4034 }
4035 else
4036 return single_display_spec_string_p (prop, string);
4037
4038 return 0;
4039 }
4040
4041
4042 /* Determine from which buffer position in W's buffer STRING comes
4043 from. AROUND_CHARPOS is an approximate position where it could
4044 be from. Value is the buffer position or 0 if it couldn't be
4045 determined.
4046
4047 W's buffer must be current.
4048
4049 This function is necessary because we don't record buffer positions
4050 in glyphs generated from strings (to keep struct glyph small).
4051 This function may only use code that doesn't eval because it is
4052 called asynchronously from note_mouse_highlight. */
4053
4054 int
4055 string_buffer_position (w, string, around_charpos)
4056 struct window *w;
4057 Lisp_Object string;
4058 int around_charpos;
4059 {
4060 Lisp_Object limit, prop, pos;
4061 const int MAX_DISTANCE = 1000;
4062 int found = 0;
4063
4064 pos = make_number (around_charpos);
4065 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4066 while (!found && !EQ (pos, limit))
4067 {
4068 prop = Fget_char_property (pos, Qdisplay, Qnil);
4069 if (!NILP (prop) && display_prop_string_p (prop, string))
4070 found = 1;
4071 else
4072 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4073 }
4074
4075 if (!found)
4076 {
4077 pos = make_number (around_charpos);
4078 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4079 while (!found && !EQ (pos, limit))
4080 {
4081 prop = Fget_char_property (pos, Qdisplay, Qnil);
4082 if (!NILP (prop) && display_prop_string_p (prop, string))
4083 found = 1;
4084 else
4085 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4086 limit);
4087 }
4088 }
4089
4090 return found ? XINT (pos) : 0;
4091 }
4092
4093
4094 \f
4095 /***********************************************************************
4096 `composition' property
4097 ***********************************************************************/
4098
4099 /* Set up iterator IT from `composition' property at its current
4100 position. Called from handle_stop. */
4101
4102 static enum prop_handled
4103 handle_composition_prop (it)
4104 struct it *it;
4105 {
4106 Lisp_Object prop, string;
4107 int pos, pos_byte, end;
4108 enum prop_handled handled = HANDLED_NORMALLY;
4109
4110 if (STRINGP (it->string))
4111 {
4112 pos = IT_STRING_CHARPOS (*it);
4113 pos_byte = IT_STRING_BYTEPOS (*it);
4114 string = it->string;
4115 }
4116 else
4117 {
4118 pos = IT_CHARPOS (*it);
4119 pos_byte = IT_BYTEPOS (*it);
4120 string = Qnil;
4121 }
4122
4123 /* If there's a valid composition and point is not inside of the
4124 composition (in the case that the composition is from the current
4125 buffer), draw a glyph composed from the composition components. */
4126 if (find_composition (pos, -1, &pos, &end, &prop, string)
4127 && COMPOSITION_VALID_P (pos, end, prop)
4128 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4129 {
4130 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4131
4132 if (id >= 0)
4133 {
4134 it->method = GET_FROM_COMPOSITION;
4135 it->cmp_id = id;
4136 it->cmp_len = COMPOSITION_LENGTH (prop);
4137 /* For a terminal, draw only the first character of the
4138 components. */
4139 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4140 it->len = (STRINGP (it->string)
4141 ? string_char_to_byte (it->string, end)
4142 : CHAR_TO_BYTE (end)) - pos_byte;
4143 it->stop_charpos = end;
4144 handled = HANDLED_RETURN;
4145 }
4146 }
4147
4148 return handled;
4149 }
4150
4151
4152 \f
4153 /***********************************************************************
4154 Overlay strings
4155 ***********************************************************************/
4156
4157 /* The following structure is used to record overlay strings for
4158 later sorting in load_overlay_strings. */
4159
4160 struct overlay_entry
4161 {
4162 Lisp_Object overlay;
4163 Lisp_Object string;
4164 int priority;
4165 int after_string_p;
4166 };
4167
4168
4169 /* Set up iterator IT from overlay strings at its current position.
4170 Called from handle_stop. */
4171
4172 static enum prop_handled
4173 handle_overlay_change (it)
4174 struct it *it;
4175 {
4176 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4177 return HANDLED_RECOMPUTE_PROPS;
4178 else
4179 return HANDLED_NORMALLY;
4180 }
4181
4182
4183 /* Set up the next overlay string for delivery by IT, if there is an
4184 overlay string to deliver. Called by set_iterator_to_next when the
4185 end of the current overlay string is reached. If there are more
4186 overlay strings to display, IT->string and
4187 IT->current.overlay_string_index are set appropriately here.
4188 Otherwise IT->string is set to nil. */
4189
4190 static void
4191 next_overlay_string (it)
4192 struct it *it;
4193 {
4194 ++it->current.overlay_string_index;
4195 if (it->current.overlay_string_index == it->n_overlay_strings)
4196 {
4197 /* No more overlay strings. Restore IT's settings to what
4198 they were before overlay strings were processed, and
4199 continue to deliver from current_buffer. */
4200 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4201
4202 pop_it (it);
4203 xassert (it->stop_charpos >= BEGV
4204 && it->stop_charpos <= it->end_charpos);
4205 it->string = Qnil;
4206 it->current.overlay_string_index = -1;
4207 SET_TEXT_POS (it->current.string_pos, -1, -1);
4208 it->n_overlay_strings = 0;
4209 it->method = GET_FROM_BUFFER;
4210
4211 /* If we're at the end of the buffer, record that we have
4212 processed the overlay strings there already, so that
4213 next_element_from_buffer doesn't try it again. */
4214 if (IT_CHARPOS (*it) >= it->end_charpos)
4215 it->overlay_strings_at_end_processed_p = 1;
4216
4217 /* If we have to display `...' for invisible text, set
4218 the iterator up for that. */
4219 if (display_ellipsis_p)
4220 setup_for_ellipsis (it, 0);
4221 }
4222 else
4223 {
4224 /* There are more overlay strings to process. If
4225 IT->current.overlay_string_index has advanced to a position
4226 where we must load IT->overlay_strings with more strings, do
4227 it. */
4228 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4229
4230 if (it->current.overlay_string_index && i == 0)
4231 load_overlay_strings (it, 0);
4232
4233 /* Initialize IT to deliver display elements from the overlay
4234 string. */
4235 it->string = it->overlay_strings[i];
4236 it->multibyte_p = STRING_MULTIBYTE (it->string);
4237 SET_TEXT_POS (it->current.string_pos, 0, 0);
4238 it->method = GET_FROM_STRING;
4239 it->stop_charpos = 0;
4240 }
4241
4242 CHECK_IT (it);
4243 }
4244
4245
4246 /* Compare two overlay_entry structures E1 and E2. Used as a
4247 comparison function for qsort in load_overlay_strings. Overlay
4248 strings for the same position are sorted so that
4249
4250 1. All after-strings come in front of before-strings, except
4251 when they come from the same overlay.
4252
4253 2. Within after-strings, strings are sorted so that overlay strings
4254 from overlays with higher priorities come first.
4255
4256 2. Within before-strings, strings are sorted so that overlay
4257 strings from overlays with higher priorities come last.
4258
4259 Value is analogous to strcmp. */
4260
4261
4262 static int
4263 compare_overlay_entries (e1, e2)
4264 void *e1, *e2;
4265 {
4266 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4267 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4268 int result;
4269
4270 if (entry1->after_string_p != entry2->after_string_p)
4271 {
4272 /* Let after-strings appear in front of before-strings if
4273 they come from different overlays. */
4274 if (EQ (entry1->overlay, entry2->overlay))
4275 result = entry1->after_string_p ? 1 : -1;
4276 else
4277 result = entry1->after_string_p ? -1 : 1;
4278 }
4279 else if (entry1->after_string_p)
4280 /* After-strings sorted in order of decreasing priority. */
4281 result = entry2->priority - entry1->priority;
4282 else
4283 /* Before-strings sorted in order of increasing priority. */
4284 result = entry1->priority - entry2->priority;
4285
4286 return result;
4287 }
4288
4289
4290 /* Load the vector IT->overlay_strings with overlay strings from IT's
4291 current buffer position, or from CHARPOS if that is > 0. Set
4292 IT->n_overlays to the total number of overlay strings found.
4293
4294 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4295 a time. On entry into load_overlay_strings,
4296 IT->current.overlay_string_index gives the number of overlay
4297 strings that have already been loaded by previous calls to this
4298 function.
4299
4300 IT->add_overlay_start contains an additional overlay start
4301 position to consider for taking overlay strings from, if non-zero.
4302 This position comes into play when the overlay has an `invisible'
4303 property, and both before and after-strings. When we've skipped to
4304 the end of the overlay, because of its `invisible' property, we
4305 nevertheless want its before-string to appear.
4306 IT->add_overlay_start will contain the overlay start position
4307 in this case.
4308
4309 Overlay strings are sorted so that after-string strings come in
4310 front of before-string strings. Within before and after-strings,
4311 strings are sorted by overlay priority. See also function
4312 compare_overlay_entries. */
4313
4314 static void
4315 load_overlay_strings (it, charpos)
4316 struct it *it;
4317 int charpos;
4318 {
4319 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4320 Lisp_Object overlay, window, str, invisible;
4321 struct Lisp_Overlay *ov;
4322 int start, end;
4323 int size = 20;
4324 int n = 0, i, j, invis_p;
4325 struct overlay_entry *entries
4326 = (struct overlay_entry *) alloca (size * sizeof *entries);
4327
4328 if (charpos <= 0)
4329 charpos = IT_CHARPOS (*it);
4330
4331 /* Append the overlay string STRING of overlay OVERLAY to vector
4332 `entries' which has size `size' and currently contains `n'
4333 elements. AFTER_P non-zero means STRING is an after-string of
4334 OVERLAY. */
4335 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4336 do \
4337 { \
4338 Lisp_Object priority; \
4339 \
4340 if (n == size) \
4341 { \
4342 int new_size = 2 * size; \
4343 struct overlay_entry *old = entries; \
4344 entries = \
4345 (struct overlay_entry *) alloca (new_size \
4346 * sizeof *entries); \
4347 bcopy (old, entries, size * sizeof *entries); \
4348 size = new_size; \
4349 } \
4350 \
4351 entries[n].string = (STRING); \
4352 entries[n].overlay = (OVERLAY); \
4353 priority = Foverlay_get ((OVERLAY), Qpriority); \
4354 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4355 entries[n].after_string_p = (AFTER_P); \
4356 ++n; \
4357 } \
4358 while (0)
4359
4360 /* Process overlay before the overlay center. */
4361 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4362 {
4363 XSETMISC (overlay, ov);
4364 xassert (OVERLAYP (overlay));
4365 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4366 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4367
4368 if (end < charpos)
4369 break;
4370
4371 /* Skip this overlay if it doesn't start or end at IT's current
4372 position. */
4373 if (end != charpos && start != charpos)
4374 continue;
4375
4376 /* Skip this overlay if it doesn't apply to IT->w. */
4377 window = Foverlay_get (overlay, Qwindow);
4378 if (WINDOWP (window) && XWINDOW (window) != it->w)
4379 continue;
4380
4381 /* If the text ``under'' the overlay is invisible, both before-
4382 and after-strings from this overlay are visible; start and
4383 end position are indistinguishable. */
4384 invisible = Foverlay_get (overlay, Qinvisible);
4385 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4386
4387 /* If overlay has a non-empty before-string, record it. */
4388 if ((start == charpos || (end == charpos && invis_p))
4389 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4390 && SCHARS (str))
4391 RECORD_OVERLAY_STRING (overlay, str, 0);
4392
4393 /* If overlay has a non-empty after-string, record it. */
4394 if ((end == charpos || (start == charpos && invis_p))
4395 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4396 && SCHARS (str))
4397 RECORD_OVERLAY_STRING (overlay, str, 1);
4398 }
4399
4400 /* Process overlays after the overlay center. */
4401 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4402 {
4403 XSETMISC (overlay, ov);
4404 xassert (OVERLAYP (overlay));
4405 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4406 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4407
4408 if (start > charpos)
4409 break;
4410
4411 /* Skip this overlay if it doesn't start or end at IT's current
4412 position. */
4413 if (end != charpos && start != charpos)
4414 continue;
4415
4416 /* Skip this overlay if it doesn't apply to IT->w. */
4417 window = Foverlay_get (overlay, Qwindow);
4418 if (WINDOWP (window) && XWINDOW (window) != it->w)
4419 continue;
4420
4421 /* If the text ``under'' the overlay is invisible, it has a zero
4422 dimension, and both before- and after-strings apply. */
4423 invisible = Foverlay_get (overlay, Qinvisible);
4424 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4425
4426 /* If overlay has a non-empty before-string, record it. */
4427 if ((start == charpos || (end == charpos && invis_p))
4428 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4429 && SCHARS (str))
4430 RECORD_OVERLAY_STRING (overlay, str, 0);
4431
4432 /* If overlay has a non-empty after-string, record it. */
4433 if ((end == charpos || (start == charpos && invis_p))
4434 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4435 && SCHARS (str))
4436 RECORD_OVERLAY_STRING (overlay, str, 1);
4437 }
4438
4439 #undef RECORD_OVERLAY_STRING
4440
4441 /* Sort entries. */
4442 if (n > 1)
4443 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4444
4445 /* Record the total number of strings to process. */
4446 it->n_overlay_strings = n;
4447
4448 /* IT->current.overlay_string_index is the number of overlay strings
4449 that have already been consumed by IT. Copy some of the
4450 remaining overlay strings to IT->overlay_strings. */
4451 i = 0;
4452 j = it->current.overlay_string_index;
4453 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4454 it->overlay_strings[i++] = entries[j++].string;
4455
4456 CHECK_IT (it);
4457 }
4458
4459
4460 /* Get the first chunk of overlay strings at IT's current buffer
4461 position, or at CHARPOS if that is > 0. Value is non-zero if at
4462 least one overlay string was found. */
4463
4464 static int
4465 get_overlay_strings (it, charpos)
4466 struct it *it;
4467 int charpos;
4468 {
4469 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4470 process. This fills IT->overlay_strings with strings, and sets
4471 IT->n_overlay_strings to the total number of strings to process.
4472 IT->pos.overlay_string_index has to be set temporarily to zero
4473 because load_overlay_strings needs this; it must be set to -1
4474 when no overlay strings are found because a zero value would
4475 indicate a position in the first overlay string. */
4476 it->current.overlay_string_index = 0;
4477 load_overlay_strings (it, charpos);
4478
4479 /* If we found overlay strings, set up IT to deliver display
4480 elements from the first one. Otherwise set up IT to deliver
4481 from current_buffer. */
4482 if (it->n_overlay_strings)
4483 {
4484 /* Make sure we know settings in current_buffer, so that we can
4485 restore meaningful values when we're done with the overlay
4486 strings. */
4487 compute_stop_pos (it);
4488 xassert (it->face_id >= 0);
4489
4490 /* Save IT's settings. They are restored after all overlay
4491 strings have been processed. */
4492 xassert (it->sp == 0);
4493 push_it (it);
4494
4495 /* Set up IT to deliver display elements from the first overlay
4496 string. */
4497 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4498 it->string = it->overlay_strings[0];
4499 it->stop_charpos = 0;
4500 xassert (STRINGP (it->string));
4501 it->end_charpos = SCHARS (it->string);
4502 it->multibyte_p = STRING_MULTIBYTE (it->string);
4503 it->method = GET_FROM_STRING;
4504 }
4505 else
4506 {
4507 it->string = Qnil;
4508 it->current.overlay_string_index = -1;
4509 it->method = GET_FROM_BUFFER;
4510 }
4511
4512 CHECK_IT (it);
4513
4514 /* Value is non-zero if we found at least one overlay string. */
4515 return STRINGP (it->string);
4516 }
4517
4518
4519 \f
4520 /***********************************************************************
4521 Saving and restoring state
4522 ***********************************************************************/
4523
4524 /* Save current settings of IT on IT->stack. Called, for example,
4525 before setting up IT for an overlay string, to be able to restore
4526 IT's settings to what they were after the overlay string has been
4527 processed. */
4528
4529 static void
4530 push_it (it)
4531 struct it *it;
4532 {
4533 struct iterator_stack_entry *p;
4534
4535 xassert (it->sp < 2);
4536 p = it->stack + it->sp;
4537
4538 p->stop_charpos = it->stop_charpos;
4539 xassert (it->face_id >= 0);
4540 p->face_id = it->face_id;
4541 p->string = it->string;
4542 p->pos = it->current;
4543 p->end_charpos = it->end_charpos;
4544 p->string_nchars = it->string_nchars;
4545 p->area = it->area;
4546 p->multibyte_p = it->multibyte_p;
4547 p->slice = it->slice;
4548 p->space_width = it->space_width;
4549 p->font_height = it->font_height;
4550 p->voffset = it->voffset;
4551 p->string_from_display_prop_p = it->string_from_display_prop_p;
4552 p->display_ellipsis_p = 0;
4553 ++it->sp;
4554 }
4555
4556
4557 /* Restore IT's settings from IT->stack. Called, for example, when no
4558 more overlay strings must be processed, and we return to delivering
4559 display elements from a buffer, or when the end of a string from a
4560 `display' property is reached and we return to delivering display
4561 elements from an overlay string, or from a buffer. */
4562
4563 static void
4564 pop_it (it)
4565 struct it *it;
4566 {
4567 struct iterator_stack_entry *p;
4568
4569 xassert (it->sp > 0);
4570 --it->sp;
4571 p = it->stack + it->sp;
4572 it->stop_charpos = p->stop_charpos;
4573 it->face_id = p->face_id;
4574 it->string = p->string;
4575 it->current = p->pos;
4576 it->end_charpos = p->end_charpos;
4577 it->string_nchars = p->string_nchars;
4578 it->area = p->area;
4579 it->multibyte_p = p->multibyte_p;
4580 it->slice = p->slice;
4581 it->space_width = p->space_width;
4582 it->font_height = p->font_height;
4583 it->voffset = p->voffset;
4584 it->string_from_display_prop_p = p->string_from_display_prop_p;
4585 }
4586
4587
4588 \f
4589 /***********************************************************************
4590 Moving over lines
4591 ***********************************************************************/
4592
4593 /* Set IT's current position to the previous line start. */
4594
4595 static void
4596 back_to_previous_line_start (it)
4597 struct it *it;
4598 {
4599 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4600 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4601 }
4602
4603
4604 /* Move IT to the next line start.
4605
4606 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4607 we skipped over part of the text (as opposed to moving the iterator
4608 continuously over the text). Otherwise, don't change the value
4609 of *SKIPPED_P.
4610
4611 Newlines may come from buffer text, overlay strings, or strings
4612 displayed via the `display' property. That's the reason we can't
4613 simply use find_next_newline_no_quit.
4614
4615 Note that this function may not skip over invisible text that is so
4616 because of text properties and immediately follows a newline. If
4617 it would, function reseat_at_next_visible_line_start, when called
4618 from set_iterator_to_next, would effectively make invisible
4619 characters following a newline part of the wrong glyph row, which
4620 leads to wrong cursor motion. */
4621
4622 static int
4623 forward_to_next_line_start (it, skipped_p)
4624 struct it *it;
4625 int *skipped_p;
4626 {
4627 int old_selective, newline_found_p, n;
4628 const int MAX_NEWLINE_DISTANCE = 500;
4629
4630 /* If already on a newline, just consume it to avoid unintended
4631 skipping over invisible text below. */
4632 if (it->what == IT_CHARACTER
4633 && it->c == '\n'
4634 && CHARPOS (it->position) == IT_CHARPOS (*it))
4635 {
4636 set_iterator_to_next (it, 0);
4637 it->c = 0;
4638 return 1;
4639 }
4640
4641 /* Don't handle selective display in the following. It's (a)
4642 unnecessary because it's done by the caller, and (b) leads to an
4643 infinite recursion because next_element_from_ellipsis indirectly
4644 calls this function. */
4645 old_selective = it->selective;
4646 it->selective = 0;
4647
4648 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4649 from buffer text. */
4650 for (n = newline_found_p = 0;
4651 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4652 n += STRINGP (it->string) ? 0 : 1)
4653 {
4654 if (!get_next_display_element (it))
4655 return 0;
4656 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4657 set_iterator_to_next (it, 0);
4658 }
4659
4660 /* If we didn't find a newline near enough, see if we can use a
4661 short-cut. */
4662 if (!newline_found_p)
4663 {
4664 int start = IT_CHARPOS (*it);
4665 int limit = find_next_newline_no_quit (start, 1);
4666 Lisp_Object pos;
4667
4668 xassert (!STRINGP (it->string));
4669
4670 /* If there isn't any `display' property in sight, and no
4671 overlays, we can just use the position of the newline in
4672 buffer text. */
4673 if (it->stop_charpos >= limit
4674 || ((pos = Fnext_single_property_change (make_number (start),
4675 Qdisplay,
4676 Qnil, make_number (limit)),
4677 NILP (pos))
4678 && next_overlay_change (start) == ZV))
4679 {
4680 IT_CHARPOS (*it) = limit;
4681 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4682 *skipped_p = newline_found_p = 1;
4683 }
4684 else
4685 {
4686 while (get_next_display_element (it)
4687 && !newline_found_p)
4688 {
4689 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4690 set_iterator_to_next (it, 0);
4691 }
4692 }
4693 }
4694
4695 it->selective = old_selective;
4696 return newline_found_p;
4697 }
4698
4699
4700 /* Set IT's current position to the previous visible line start. Skip
4701 invisible text that is so either due to text properties or due to
4702 selective display. Caution: this does not change IT->current_x and
4703 IT->hpos. */
4704
4705 static void
4706 back_to_previous_visible_line_start (it)
4707 struct it *it;
4708 {
4709 while (IT_CHARPOS (*it) > BEGV)
4710 {
4711 back_to_previous_line_start (it);
4712 if (IT_CHARPOS (*it) <= BEGV)
4713 break;
4714
4715 /* If selective > 0, then lines indented more than that values
4716 are invisible. */
4717 if (it->selective > 0
4718 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4719 (double) it->selective)) /* iftc */
4720 continue;
4721
4722 /* Check the newline before point for invisibility. */
4723 {
4724 Lisp_Object prop;
4725 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4726 Qinvisible, it->window);
4727 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4728 continue;
4729 }
4730
4731 /* If newline has a display property that replaces the newline with something
4732 else (image or text), find start of overlay or interval and continue search
4733 from that point. */
4734 if (IT_CHARPOS (*it) > BEGV)
4735 {
4736 struct it it2 = *it;
4737 int pos;
4738 int beg, end;
4739 Lisp_Object val, overlay;
4740
4741 pos = --IT_CHARPOS (it2);
4742 --IT_BYTEPOS (it2);
4743 it2.sp = 0;
4744 if (handle_display_prop (&it2) == HANDLED_RETURN
4745 && !NILP (val = get_char_property_and_overlay
4746 (make_number (pos), Qdisplay, Qnil, &overlay))
4747 && (OVERLAYP (overlay)
4748 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4749 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4750 {
4751 if (beg < BEGV)
4752 beg = BEGV;
4753 IT_CHARPOS (*it) = beg;
4754 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4755 continue;
4756 }
4757 }
4758
4759 break;
4760 }
4761
4762 xassert (IT_CHARPOS (*it) >= BEGV);
4763 xassert (IT_CHARPOS (*it) == BEGV
4764 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4765 CHECK_IT (it);
4766 }
4767
4768
4769 /* Reseat iterator IT at the previous visible line start. Skip
4770 invisible text that is so either due to text properties or due to
4771 selective display. At the end, update IT's overlay information,
4772 face information etc. */
4773
4774 void
4775 reseat_at_previous_visible_line_start (it)
4776 struct it *it;
4777 {
4778 back_to_previous_visible_line_start (it);
4779 reseat (it, it->current.pos, 1);
4780 CHECK_IT (it);
4781 }
4782
4783
4784 /* Reseat iterator IT on the next visible line start in the current
4785 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4786 preceding the line start. Skip over invisible text that is so
4787 because of selective display. Compute faces, overlays etc at the
4788 new position. Note that this function does not skip over text that
4789 is invisible because of text properties. */
4790
4791 static void
4792 reseat_at_next_visible_line_start (it, on_newline_p)
4793 struct it *it;
4794 int on_newline_p;
4795 {
4796 int newline_found_p, skipped_p = 0;
4797
4798 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4799
4800 /* Skip over lines that are invisible because they are indented
4801 more than the value of IT->selective. */
4802 if (it->selective > 0)
4803 while (IT_CHARPOS (*it) < ZV
4804 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4805 (double) it->selective)) /* iftc */
4806 {
4807 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4808 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4809 }
4810
4811 /* Position on the newline if that's what's requested. */
4812 if (on_newline_p && newline_found_p)
4813 {
4814 if (STRINGP (it->string))
4815 {
4816 if (IT_STRING_CHARPOS (*it) > 0)
4817 {
4818 --IT_STRING_CHARPOS (*it);
4819 --IT_STRING_BYTEPOS (*it);
4820 }
4821 }
4822 else if (IT_CHARPOS (*it) > BEGV)
4823 {
4824 --IT_CHARPOS (*it);
4825 --IT_BYTEPOS (*it);
4826 reseat (it, it->current.pos, 0);
4827 }
4828 }
4829 else if (skipped_p)
4830 reseat (it, it->current.pos, 0);
4831
4832 CHECK_IT (it);
4833 }
4834
4835
4836 \f
4837 /***********************************************************************
4838 Changing an iterator's position
4839 ***********************************************************************/
4840
4841 /* Change IT's current position to POS in current_buffer. If FORCE_P
4842 is non-zero, always check for text properties at the new position.
4843 Otherwise, text properties are only looked up if POS >=
4844 IT->check_charpos of a property. */
4845
4846 static void
4847 reseat (it, pos, force_p)
4848 struct it *it;
4849 struct text_pos pos;
4850 int force_p;
4851 {
4852 int original_pos = IT_CHARPOS (*it);
4853
4854 reseat_1 (it, pos, 0);
4855
4856 /* Determine where to check text properties. Avoid doing it
4857 where possible because text property lookup is very expensive. */
4858 if (force_p
4859 || CHARPOS (pos) > it->stop_charpos
4860 || CHARPOS (pos) < original_pos)
4861 handle_stop (it);
4862
4863 CHECK_IT (it);
4864 }
4865
4866
4867 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4868 IT->stop_pos to POS, also. */
4869
4870 static void
4871 reseat_1 (it, pos, set_stop_p)
4872 struct it *it;
4873 struct text_pos pos;
4874 int set_stop_p;
4875 {
4876 /* Don't call this function when scanning a C string. */
4877 xassert (it->s == NULL);
4878
4879 /* POS must be a reasonable value. */
4880 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4881
4882 it->current.pos = it->position = pos;
4883 XSETBUFFER (it->object, current_buffer);
4884 it->end_charpos = ZV;
4885 it->dpvec = NULL;
4886 it->current.dpvec_index = -1;
4887 it->current.overlay_string_index = -1;
4888 IT_STRING_CHARPOS (*it) = -1;
4889 IT_STRING_BYTEPOS (*it) = -1;
4890 it->string = Qnil;
4891 it->method = GET_FROM_BUFFER;
4892 /* RMS: I added this to fix a bug in move_it_vertically_backward
4893 where it->area continued to relate to the starting point
4894 for the backward motion. Bug report from
4895 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4896 However, I am not sure whether reseat still does the right thing
4897 in general after this change. */
4898 it->area = TEXT_AREA;
4899 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4900 it->sp = 0;
4901 it->face_before_selective_p = 0;
4902
4903 if (set_stop_p)
4904 it->stop_charpos = CHARPOS (pos);
4905 }
4906
4907
4908 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4909 If S is non-null, it is a C string to iterate over. Otherwise,
4910 STRING gives a Lisp string to iterate over.
4911
4912 If PRECISION > 0, don't return more then PRECISION number of
4913 characters from the string.
4914
4915 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4916 characters have been returned. FIELD_WIDTH < 0 means an infinite
4917 field width.
4918
4919 MULTIBYTE = 0 means disable processing of multibyte characters,
4920 MULTIBYTE > 0 means enable it,
4921 MULTIBYTE < 0 means use IT->multibyte_p.
4922
4923 IT must be initialized via a prior call to init_iterator before
4924 calling this function. */
4925
4926 static void
4927 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4928 struct it *it;
4929 unsigned char *s;
4930 Lisp_Object string;
4931 int charpos;
4932 int precision, field_width, multibyte;
4933 {
4934 /* No region in strings. */
4935 it->region_beg_charpos = it->region_end_charpos = -1;
4936
4937 /* No text property checks performed by default, but see below. */
4938 it->stop_charpos = -1;
4939
4940 /* Set iterator position and end position. */
4941 bzero (&it->current, sizeof it->current);
4942 it->current.overlay_string_index = -1;
4943 it->current.dpvec_index = -1;
4944 xassert (charpos >= 0);
4945
4946 /* If STRING is specified, use its multibyteness, otherwise use the
4947 setting of MULTIBYTE, if specified. */
4948 if (multibyte >= 0)
4949 it->multibyte_p = multibyte > 0;
4950
4951 if (s == NULL)
4952 {
4953 xassert (STRINGP (string));
4954 it->string = string;
4955 it->s = NULL;
4956 it->end_charpos = it->string_nchars = SCHARS (string);
4957 it->method = GET_FROM_STRING;
4958 it->current.string_pos = string_pos (charpos, string);
4959 }
4960 else
4961 {
4962 it->s = s;
4963 it->string = Qnil;
4964
4965 /* Note that we use IT->current.pos, not it->current.string_pos,
4966 for displaying C strings. */
4967 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4968 if (it->multibyte_p)
4969 {
4970 it->current.pos = c_string_pos (charpos, s, 1);
4971 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4972 }
4973 else
4974 {
4975 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4976 it->end_charpos = it->string_nchars = strlen (s);
4977 }
4978
4979 it->method = GET_FROM_C_STRING;
4980 }
4981
4982 /* PRECISION > 0 means don't return more than PRECISION characters
4983 from the string. */
4984 if (precision > 0 && it->end_charpos - charpos > precision)
4985 it->end_charpos = it->string_nchars = charpos + precision;
4986
4987 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4988 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4989 FIELD_WIDTH < 0 means infinite field width. This is useful for
4990 padding with `-' at the end of a mode line. */
4991 if (field_width < 0)
4992 field_width = INFINITY;
4993 if (field_width > it->end_charpos - charpos)
4994 it->end_charpos = charpos + field_width;
4995
4996 /* Use the standard display table for displaying strings. */
4997 if (DISP_TABLE_P (Vstandard_display_table))
4998 it->dp = XCHAR_TABLE (Vstandard_display_table);
4999
5000 it->stop_charpos = charpos;
5001 CHECK_IT (it);
5002 }
5003
5004
5005 \f
5006 /***********************************************************************
5007 Iteration
5008 ***********************************************************************/
5009
5010 /* Map enum it_method value to corresponding next_element_from_* function. */
5011
5012 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5013 {
5014 next_element_from_buffer,
5015 next_element_from_display_vector,
5016 next_element_from_composition,
5017 next_element_from_string,
5018 next_element_from_c_string,
5019 next_element_from_image,
5020 next_element_from_stretch
5021 };
5022
5023
5024 /* Load IT's display element fields with information about the next
5025 display element from the current position of IT. Value is zero if
5026 end of buffer (or C string) is reached. */
5027
5028 int
5029 get_next_display_element (it)
5030 struct it *it;
5031 {
5032 /* Non-zero means that we found a display element. Zero means that
5033 we hit the end of what we iterate over. Performance note: the
5034 function pointer `method' used here turns out to be faster than
5035 using a sequence of if-statements. */
5036 int success_p;
5037
5038 get_next:
5039 success_p = (*get_next_element[it->method]) (it);
5040
5041 if (it->what == IT_CHARACTER)
5042 {
5043 /* Map via display table or translate control characters.
5044 IT->c, IT->len etc. have been set to the next character by
5045 the function call above. If we have a display table, and it
5046 contains an entry for IT->c, translate it. Don't do this if
5047 IT->c itself comes from a display table, otherwise we could
5048 end up in an infinite recursion. (An alternative could be to
5049 count the recursion depth of this function and signal an
5050 error when a certain maximum depth is reached.) Is it worth
5051 it? */
5052 if (success_p && it->dpvec == NULL)
5053 {
5054 Lisp_Object dv;
5055
5056 if (it->dp
5057 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5058 VECTORP (dv)))
5059 {
5060 struct Lisp_Vector *v = XVECTOR (dv);
5061
5062 /* Return the first character from the display table
5063 entry, if not empty. If empty, don't display the
5064 current character. */
5065 if (v->size)
5066 {
5067 it->dpvec_char_len = it->len;
5068 it->dpvec = v->contents;
5069 it->dpend = v->contents + v->size;
5070 it->current.dpvec_index = 0;
5071 it->dpvec_face_id = -1;
5072 it->saved_face_id = it->face_id;
5073 it->method = GET_FROM_DISPLAY_VECTOR;
5074 it->ellipsis_p = 0;
5075 }
5076 else
5077 {
5078 set_iterator_to_next (it, 0);
5079 }
5080 goto get_next;
5081 }
5082
5083 /* Translate control characters into `\003' or `^C' form.
5084 Control characters coming from a display table entry are
5085 currently not translated because we use IT->dpvec to hold
5086 the translation. This could easily be changed but I
5087 don't believe that it is worth doing.
5088
5089 If it->multibyte_p is nonzero, eight-bit characters and
5090 non-printable multibyte characters are also translated to
5091 octal form.
5092
5093 If it->multibyte_p is zero, eight-bit characters that
5094 don't have corresponding multibyte char code are also
5095 translated to octal form. */
5096 else if ((it->c < ' '
5097 && (it->area != TEXT_AREA
5098 /* In mode line, treat \n like other crl chars. */
5099 || (it->c != '\t'
5100 && it->glyph_row && it->glyph_row->mode_line_p)
5101 || (it->c != '\n' && it->c != '\t')))
5102 || (it->multibyte_p
5103 ? ((it->c >= 127
5104 && it->len == 1)
5105 || !CHAR_PRINTABLE_P (it->c)
5106 || (!NILP (Vnobreak_char_display)
5107 && (it->c == 0x8a0 || it->c == 0x8ad
5108 || it->c == 0x920 || it->c == 0x92d
5109 || it->c == 0xe20 || it->c == 0xe2d
5110 || it->c == 0xf20 || it->c == 0xf2d)))
5111 : (it->c >= 127
5112 && (!unibyte_display_via_language_environment
5113 || it->c == unibyte_char_to_multibyte (it->c)))))
5114 {
5115 /* IT->c is a control character which must be displayed
5116 either as '\003' or as `^C' where the '\\' and '^'
5117 can be defined in the display table. Fill
5118 IT->ctl_chars with glyphs for what we have to
5119 display. Then, set IT->dpvec to these glyphs. */
5120 GLYPH g;
5121 int ctl_len;
5122 int face_id, lface_id = 0 ;
5123 GLYPH escape_glyph;
5124
5125 /* Handle control characters with ^. */
5126
5127 if (it->c < 128 && it->ctl_arrow_p)
5128 {
5129 g = '^'; /* default glyph for Control */
5130 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5131 if (it->dp
5132 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5133 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5134 {
5135 g = XINT (DISP_CTRL_GLYPH (it->dp));
5136 lface_id = FAST_GLYPH_FACE (g);
5137 }
5138 if (lface_id)
5139 {
5140 g = FAST_GLYPH_CHAR (g);
5141 face_id = merge_faces (it->f, Qt, lface_id,
5142 it->face_id);
5143 }
5144 else
5145 {
5146 /* Merge the escape-glyph face into the current face. */
5147 face_id = merge_faces (it->f, Qescape_glyph, 0,
5148 it->face_id);
5149 }
5150
5151 XSETINT (it->ctl_chars[0], g);
5152 g = it->c ^ 0100;
5153 XSETINT (it->ctl_chars[1], g);
5154 ctl_len = 2;
5155 goto display_control;
5156 }
5157
5158 /* Handle non-break space in the mode where it only gets
5159 highlighting. */
5160
5161 if (EQ (Vnobreak_char_display, Qt)
5162 && (it->c == 0x8a0 || it->c == 0x920
5163 || it->c == 0xe20 || it->c == 0xf20))
5164 {
5165 /* Merge the no-break-space face into the current face. */
5166 face_id = merge_faces (it->f, Qnobreak_space, 0,
5167 it->face_id);
5168
5169 g = it->c = ' ';
5170 XSETINT (it->ctl_chars[0], g);
5171 ctl_len = 1;
5172 goto display_control;
5173 }
5174
5175 /* Handle sequences that start with the "escape glyph". */
5176
5177 /* the default escape glyph is \. */
5178 escape_glyph = '\\';
5179
5180 if (it->dp
5181 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5182 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5183 {
5184 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5185 lface_id = FAST_GLYPH_FACE (escape_glyph);
5186 }
5187 if (lface_id)
5188 {
5189 /* The display table specified a face.
5190 Merge it into face_id and also into escape_glyph. */
5191 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5192 face_id = merge_faces (it->f, Qt, lface_id,
5193 it->face_id);
5194 }
5195 else
5196 {
5197 /* Merge the escape-glyph face into the current face. */
5198 face_id = merge_faces (it->f, Qescape_glyph, 0,
5199 it->face_id);
5200 }
5201
5202 /* Handle soft hyphens in the mode where they only get
5203 highlighting. */
5204
5205 if (EQ (Vnobreak_char_display, Qt)
5206 && (it->c == 0x8ad || it->c == 0x92d
5207 || it->c == 0xe2d || it->c == 0xf2d))
5208 {
5209 g = it->c = '-';
5210 XSETINT (it->ctl_chars[0], g);
5211 ctl_len = 1;
5212 goto display_control;
5213 }
5214
5215 /* Handle non-break space and soft hyphen
5216 with the escape glyph. */
5217
5218 if (it->c == 0x8a0 || it->c == 0x8ad
5219 || it->c == 0x920 || it->c == 0x92d
5220 || it->c == 0xe20 || it->c == 0xe2d
5221 || it->c == 0xf20 || it->c == 0xf2d)
5222 {
5223 XSETINT (it->ctl_chars[0], escape_glyph);
5224 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5225 XSETINT (it->ctl_chars[1], g);
5226 ctl_len = 2;
5227 goto display_control;
5228 }
5229
5230 {
5231 unsigned char str[MAX_MULTIBYTE_LENGTH];
5232 int len;
5233 int i;
5234
5235 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5236 if (SINGLE_BYTE_CHAR_P (it->c))
5237 str[0] = it->c, len = 1;
5238 else
5239 {
5240 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5241 if (len < 0)
5242 {
5243 /* It's an invalid character, which shouldn't
5244 happen actually, but due to bugs it may
5245 happen. Let's print the char as is, there's
5246 not much meaningful we can do with it. */
5247 str[0] = it->c;
5248 str[1] = it->c >> 8;
5249 str[2] = it->c >> 16;
5250 str[3] = it->c >> 24;
5251 len = 4;
5252 }
5253 }
5254
5255 for (i = 0; i < len; i++)
5256 {
5257 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5258 /* Insert three more glyphs into IT->ctl_chars for
5259 the octal display of the character. */
5260 g = ((str[i] >> 6) & 7) + '0';
5261 XSETINT (it->ctl_chars[i * 4 + 1], g);
5262 g = ((str[i] >> 3) & 7) + '0';
5263 XSETINT (it->ctl_chars[i * 4 + 2], g);
5264 g = (str[i] & 7) + '0';
5265 XSETINT (it->ctl_chars[i * 4 + 3], g);
5266 }
5267 ctl_len = len * 4;
5268 }
5269
5270 display_control:
5271 /* Set up IT->dpvec and return first character from it. */
5272 it->dpvec_char_len = it->len;
5273 it->dpvec = it->ctl_chars;
5274 it->dpend = it->dpvec + ctl_len;
5275 it->current.dpvec_index = 0;
5276 it->dpvec_face_id = face_id;
5277 it->saved_face_id = it->face_id;
5278 it->method = GET_FROM_DISPLAY_VECTOR;
5279 it->ellipsis_p = 0;
5280 goto get_next;
5281 }
5282 }
5283
5284 /* Adjust face id for a multibyte character. There are no
5285 multibyte character in unibyte text. */
5286 if (it->multibyte_p
5287 && success_p
5288 && FRAME_WINDOW_P (it->f))
5289 {
5290 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5291 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5292 }
5293 }
5294
5295 /* Is this character the last one of a run of characters with
5296 box? If yes, set IT->end_of_box_run_p to 1. */
5297 if (it->face_box_p
5298 && it->s == NULL)
5299 {
5300 int face_id;
5301 struct face *face;
5302
5303 it->end_of_box_run_p
5304 = ((face_id = face_after_it_pos (it),
5305 face_id != it->face_id)
5306 && (face = FACE_FROM_ID (it->f, face_id),
5307 face->box == FACE_NO_BOX));
5308 }
5309
5310 /* Value is 0 if end of buffer or string reached. */
5311 return success_p;
5312 }
5313
5314
5315 /* Move IT to the next display element.
5316
5317 RESEAT_P non-zero means if called on a newline in buffer text,
5318 skip to the next visible line start.
5319
5320 Functions get_next_display_element and set_iterator_to_next are
5321 separate because I find this arrangement easier to handle than a
5322 get_next_display_element function that also increments IT's
5323 position. The way it is we can first look at an iterator's current
5324 display element, decide whether it fits on a line, and if it does,
5325 increment the iterator position. The other way around we probably
5326 would either need a flag indicating whether the iterator has to be
5327 incremented the next time, or we would have to implement a
5328 decrement position function which would not be easy to write. */
5329
5330 void
5331 set_iterator_to_next (it, reseat_p)
5332 struct it *it;
5333 int reseat_p;
5334 {
5335 /* Reset flags indicating start and end of a sequence of characters
5336 with box. Reset them at the start of this function because
5337 moving the iterator to a new position might set them. */
5338 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5339
5340 switch (it->method)
5341 {
5342 case GET_FROM_BUFFER:
5343 /* The current display element of IT is a character from
5344 current_buffer. Advance in the buffer, and maybe skip over
5345 invisible lines that are so because of selective display. */
5346 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5347 reseat_at_next_visible_line_start (it, 0);
5348 else
5349 {
5350 xassert (it->len != 0);
5351 IT_BYTEPOS (*it) += it->len;
5352 IT_CHARPOS (*it) += 1;
5353 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5354 }
5355 break;
5356
5357 case GET_FROM_COMPOSITION:
5358 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5359 if (STRINGP (it->string))
5360 {
5361 IT_STRING_BYTEPOS (*it) += it->len;
5362 IT_STRING_CHARPOS (*it) += it->cmp_len;
5363 it->method = GET_FROM_STRING;
5364 goto consider_string_end;
5365 }
5366 else
5367 {
5368 IT_BYTEPOS (*it) += it->len;
5369 IT_CHARPOS (*it) += it->cmp_len;
5370 it->method = GET_FROM_BUFFER;
5371 }
5372 break;
5373
5374 case GET_FROM_C_STRING:
5375 /* Current display element of IT is from a C string. */
5376 IT_BYTEPOS (*it) += it->len;
5377 IT_CHARPOS (*it) += 1;
5378 break;
5379
5380 case GET_FROM_DISPLAY_VECTOR:
5381 /* Current display element of IT is from a display table entry.
5382 Advance in the display table definition. Reset it to null if
5383 end reached, and continue with characters from buffers/
5384 strings. */
5385 ++it->current.dpvec_index;
5386
5387 /* Restore face of the iterator to what they were before the
5388 display vector entry (these entries may contain faces). */
5389 it->face_id = it->saved_face_id;
5390
5391 if (it->dpvec + it->current.dpvec_index == it->dpend)
5392 {
5393 if (it->s)
5394 it->method = GET_FROM_C_STRING;
5395 else if (STRINGP (it->string))
5396 it->method = GET_FROM_STRING;
5397 else
5398 it->method = GET_FROM_BUFFER;
5399
5400 it->dpvec = NULL;
5401 it->current.dpvec_index = -1;
5402
5403 /* Skip over characters which were displayed via IT->dpvec. */
5404 if (it->dpvec_char_len < 0)
5405 reseat_at_next_visible_line_start (it, 1);
5406 else if (it->dpvec_char_len > 0)
5407 {
5408 it->len = it->dpvec_char_len;
5409 set_iterator_to_next (it, reseat_p);
5410 }
5411
5412 /* Recheck faces after display vector */
5413 it->stop_charpos = IT_CHARPOS (*it);
5414 }
5415 break;
5416
5417 case GET_FROM_STRING:
5418 /* Current display element is a character from a Lisp string. */
5419 xassert (it->s == NULL && STRINGP (it->string));
5420 IT_STRING_BYTEPOS (*it) += it->len;
5421 IT_STRING_CHARPOS (*it) += 1;
5422
5423 consider_string_end:
5424
5425 if (it->current.overlay_string_index >= 0)
5426 {
5427 /* IT->string is an overlay string. Advance to the
5428 next, if there is one. */
5429 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5430 next_overlay_string (it);
5431 }
5432 else
5433 {
5434 /* IT->string is not an overlay string. If we reached
5435 its end, and there is something on IT->stack, proceed
5436 with what is on the stack. This can be either another
5437 string, this time an overlay string, or a buffer. */
5438 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5439 && it->sp > 0)
5440 {
5441 pop_it (it);
5442 if (STRINGP (it->string))
5443 goto consider_string_end;
5444 it->method = GET_FROM_BUFFER;
5445 }
5446 }
5447 break;
5448
5449 case GET_FROM_IMAGE:
5450 case GET_FROM_STRETCH:
5451 /* The position etc with which we have to proceed are on
5452 the stack. The position may be at the end of a string,
5453 if the `display' property takes up the whole string. */
5454 xassert (it->sp > 0);
5455 pop_it (it);
5456 it->image_id = 0;
5457 if (STRINGP (it->string))
5458 {
5459 it->method = GET_FROM_STRING;
5460 goto consider_string_end;
5461 }
5462 it->method = GET_FROM_BUFFER;
5463 break;
5464
5465 default:
5466 /* There are no other methods defined, so this should be a bug. */
5467 abort ();
5468 }
5469
5470 xassert (it->method != GET_FROM_STRING
5471 || (STRINGP (it->string)
5472 && IT_STRING_CHARPOS (*it) >= 0));
5473 }
5474
5475 /* Load IT's display element fields with information about the next
5476 display element which comes from a display table entry or from the
5477 result of translating a control character to one of the forms `^C'
5478 or `\003'.
5479
5480 IT->dpvec holds the glyphs to return as characters.
5481 IT->saved_face_id holds the face id before the display vector--
5482 it is restored into IT->face_idin set_iterator_to_next. */
5483
5484 static int
5485 next_element_from_display_vector (it)
5486 struct it *it;
5487 {
5488 /* Precondition. */
5489 xassert (it->dpvec && it->current.dpvec_index >= 0);
5490
5491 it->face_id = it->saved_face_id;
5492
5493 if (INTEGERP (*it->dpvec)
5494 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5495 {
5496 GLYPH g;
5497
5498 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5499 it->c = FAST_GLYPH_CHAR (g);
5500 it->len = CHAR_BYTES (it->c);
5501
5502 /* The entry may contain a face id to use. Such a face id is
5503 the id of a Lisp face, not a realized face. A face id of
5504 zero means no face is specified. */
5505 if (it->dpvec_face_id >= 0)
5506 it->face_id = it->dpvec_face_id;
5507 else
5508 {
5509 int lface_id = FAST_GLYPH_FACE (g);
5510 if (lface_id > 0)
5511 it->face_id = merge_faces (it->f, Qt, lface_id,
5512 it->saved_face_id);
5513 }
5514 }
5515 else
5516 /* Display table entry is invalid. Return a space. */
5517 it->c = ' ', it->len = 1;
5518
5519 /* Don't change position and object of the iterator here. They are
5520 still the values of the character that had this display table
5521 entry or was translated, and that's what we want. */
5522 it->what = IT_CHARACTER;
5523 return 1;
5524 }
5525
5526
5527 /* Load IT with the next display element from Lisp string IT->string.
5528 IT->current.string_pos is the current position within the string.
5529 If IT->current.overlay_string_index >= 0, the Lisp string is an
5530 overlay string. */
5531
5532 static int
5533 next_element_from_string (it)
5534 struct it *it;
5535 {
5536 struct text_pos position;
5537
5538 xassert (STRINGP (it->string));
5539 xassert (IT_STRING_CHARPOS (*it) >= 0);
5540 position = it->current.string_pos;
5541
5542 /* Time to check for invisible text? */
5543 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5544 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5545 {
5546 handle_stop (it);
5547
5548 /* Since a handler may have changed IT->method, we must
5549 recurse here. */
5550 return get_next_display_element (it);
5551 }
5552
5553 if (it->current.overlay_string_index >= 0)
5554 {
5555 /* Get the next character from an overlay string. In overlay
5556 strings, There is no field width or padding with spaces to
5557 do. */
5558 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5559 {
5560 it->what = IT_EOB;
5561 return 0;
5562 }
5563 else if (STRING_MULTIBYTE (it->string))
5564 {
5565 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5566 const unsigned char *s = (SDATA (it->string)
5567 + IT_STRING_BYTEPOS (*it));
5568 it->c = string_char_and_length (s, remaining, &it->len);
5569 }
5570 else
5571 {
5572 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5573 it->len = 1;
5574 }
5575 }
5576 else
5577 {
5578 /* Get the next character from a Lisp string that is not an
5579 overlay string. Such strings come from the mode line, for
5580 example. We may have to pad with spaces, or truncate the
5581 string. See also next_element_from_c_string. */
5582 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5583 {
5584 it->what = IT_EOB;
5585 return 0;
5586 }
5587 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5588 {
5589 /* Pad with spaces. */
5590 it->c = ' ', it->len = 1;
5591 CHARPOS (position) = BYTEPOS (position) = -1;
5592 }
5593 else if (STRING_MULTIBYTE (it->string))
5594 {
5595 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5596 const unsigned char *s = (SDATA (it->string)
5597 + IT_STRING_BYTEPOS (*it));
5598 it->c = string_char_and_length (s, maxlen, &it->len);
5599 }
5600 else
5601 {
5602 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5603 it->len = 1;
5604 }
5605 }
5606
5607 /* Record what we have and where it came from. Note that we store a
5608 buffer position in IT->position although it could arguably be a
5609 string position. */
5610 it->what = IT_CHARACTER;
5611 it->object = it->string;
5612 it->position = position;
5613 return 1;
5614 }
5615
5616
5617 /* Load IT with next display element from C string IT->s.
5618 IT->string_nchars is the maximum number of characters to return
5619 from the string. IT->end_charpos may be greater than
5620 IT->string_nchars when this function is called, in which case we
5621 may have to return padding spaces. Value is zero if end of string
5622 reached, including padding spaces. */
5623
5624 static int
5625 next_element_from_c_string (it)
5626 struct it *it;
5627 {
5628 int success_p = 1;
5629
5630 xassert (it->s);
5631 it->what = IT_CHARACTER;
5632 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5633 it->object = Qnil;
5634
5635 /* IT's position can be greater IT->string_nchars in case a field
5636 width or precision has been specified when the iterator was
5637 initialized. */
5638 if (IT_CHARPOS (*it) >= it->end_charpos)
5639 {
5640 /* End of the game. */
5641 it->what = IT_EOB;
5642 success_p = 0;
5643 }
5644 else if (IT_CHARPOS (*it) >= it->string_nchars)
5645 {
5646 /* Pad with spaces. */
5647 it->c = ' ', it->len = 1;
5648 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5649 }
5650 else if (it->multibyte_p)
5651 {
5652 /* Implementation note: The calls to strlen apparently aren't a
5653 performance problem because there is no noticeable performance
5654 difference between Emacs running in unibyte or multibyte mode. */
5655 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5656 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5657 maxlen, &it->len);
5658 }
5659 else
5660 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5661
5662 return success_p;
5663 }
5664
5665
5666 /* Set up IT to return characters from an ellipsis, if appropriate.
5667 The definition of the ellipsis glyphs may come from a display table
5668 entry. This function Fills IT with the first glyph from the
5669 ellipsis if an ellipsis is to be displayed. */
5670
5671 static int
5672 next_element_from_ellipsis (it)
5673 struct it *it;
5674 {
5675 if (it->selective_display_ellipsis_p)
5676 setup_for_ellipsis (it, it->len);
5677 else
5678 {
5679 /* The face at the current position may be different from the
5680 face we find after the invisible text. Remember what it
5681 was in IT->saved_face_id, and signal that it's there by
5682 setting face_before_selective_p. */
5683 it->saved_face_id = it->face_id;
5684 it->method = GET_FROM_BUFFER;
5685 reseat_at_next_visible_line_start (it, 1);
5686 it->face_before_selective_p = 1;
5687 }
5688
5689 return get_next_display_element (it);
5690 }
5691
5692
5693 /* Deliver an image display element. The iterator IT is already
5694 filled with image information (done in handle_display_prop). Value
5695 is always 1. */
5696
5697
5698 static int
5699 next_element_from_image (it)
5700 struct it *it;
5701 {
5702 it->what = IT_IMAGE;
5703 return 1;
5704 }
5705
5706
5707 /* Fill iterator IT with next display element from a stretch glyph
5708 property. IT->object is the value of the text property. Value is
5709 always 1. */
5710
5711 static int
5712 next_element_from_stretch (it)
5713 struct it *it;
5714 {
5715 it->what = IT_STRETCH;
5716 return 1;
5717 }
5718
5719
5720 /* Load IT with the next display element from current_buffer. Value
5721 is zero if end of buffer reached. IT->stop_charpos is the next
5722 position at which to stop and check for text properties or buffer
5723 end. */
5724
5725 static int
5726 next_element_from_buffer (it)
5727 struct it *it;
5728 {
5729 int success_p = 1;
5730
5731 /* Check this assumption, otherwise, we would never enter the
5732 if-statement, below. */
5733 xassert (IT_CHARPOS (*it) >= BEGV
5734 && IT_CHARPOS (*it) <= it->stop_charpos);
5735
5736 if (IT_CHARPOS (*it) >= it->stop_charpos)
5737 {
5738 if (IT_CHARPOS (*it) >= it->end_charpos)
5739 {
5740 int overlay_strings_follow_p;
5741
5742 /* End of the game, except when overlay strings follow that
5743 haven't been returned yet. */
5744 if (it->overlay_strings_at_end_processed_p)
5745 overlay_strings_follow_p = 0;
5746 else
5747 {
5748 it->overlay_strings_at_end_processed_p = 1;
5749 overlay_strings_follow_p = get_overlay_strings (it, 0);
5750 }
5751
5752 if (overlay_strings_follow_p)
5753 success_p = get_next_display_element (it);
5754 else
5755 {
5756 it->what = IT_EOB;
5757 it->position = it->current.pos;
5758 success_p = 0;
5759 }
5760 }
5761 else
5762 {
5763 handle_stop (it);
5764 return get_next_display_element (it);
5765 }
5766 }
5767 else
5768 {
5769 /* No face changes, overlays etc. in sight, so just return a
5770 character from current_buffer. */
5771 unsigned char *p;
5772
5773 /* Maybe run the redisplay end trigger hook. Performance note:
5774 This doesn't seem to cost measurable time. */
5775 if (it->redisplay_end_trigger_charpos
5776 && it->glyph_row
5777 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5778 run_redisplay_end_trigger_hook (it);
5779
5780 /* Get the next character, maybe multibyte. */
5781 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5782 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5783 {
5784 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5785 - IT_BYTEPOS (*it));
5786 it->c = string_char_and_length (p, maxlen, &it->len);
5787 }
5788 else
5789 it->c = *p, it->len = 1;
5790
5791 /* Record what we have and where it came from. */
5792 it->what = IT_CHARACTER;;
5793 it->object = it->w->buffer;
5794 it->position = it->current.pos;
5795
5796 /* Normally we return the character found above, except when we
5797 really want to return an ellipsis for selective display. */
5798 if (it->selective)
5799 {
5800 if (it->c == '\n')
5801 {
5802 /* A value of selective > 0 means hide lines indented more
5803 than that number of columns. */
5804 if (it->selective > 0
5805 && IT_CHARPOS (*it) + 1 < ZV
5806 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5807 IT_BYTEPOS (*it) + 1,
5808 (double) it->selective)) /* iftc */
5809 {
5810 success_p = next_element_from_ellipsis (it);
5811 it->dpvec_char_len = -1;
5812 }
5813 }
5814 else if (it->c == '\r' && it->selective == -1)
5815 {
5816 /* A value of selective == -1 means that everything from the
5817 CR to the end of the line is invisible, with maybe an
5818 ellipsis displayed for it. */
5819 success_p = next_element_from_ellipsis (it);
5820 it->dpvec_char_len = -1;
5821 }
5822 }
5823 }
5824
5825 /* Value is zero if end of buffer reached. */
5826 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5827 return success_p;
5828 }
5829
5830
5831 /* Run the redisplay end trigger hook for IT. */
5832
5833 static void
5834 run_redisplay_end_trigger_hook (it)
5835 struct it *it;
5836 {
5837 Lisp_Object args[3];
5838
5839 /* IT->glyph_row should be non-null, i.e. we should be actually
5840 displaying something, or otherwise we should not run the hook. */
5841 xassert (it->glyph_row);
5842
5843 /* Set up hook arguments. */
5844 args[0] = Qredisplay_end_trigger_functions;
5845 args[1] = it->window;
5846 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5847 it->redisplay_end_trigger_charpos = 0;
5848
5849 /* Since we are *trying* to run these functions, don't try to run
5850 them again, even if they get an error. */
5851 it->w->redisplay_end_trigger = Qnil;
5852 Frun_hook_with_args (3, args);
5853
5854 /* Notice if it changed the face of the character we are on. */
5855 handle_face_prop (it);
5856 }
5857
5858
5859 /* Deliver a composition display element. The iterator IT is already
5860 filled with composition information (done in
5861 handle_composition_prop). Value is always 1. */
5862
5863 static int
5864 next_element_from_composition (it)
5865 struct it *it;
5866 {
5867 it->what = IT_COMPOSITION;
5868 it->position = (STRINGP (it->string)
5869 ? it->current.string_pos
5870 : it->current.pos);
5871 return 1;
5872 }
5873
5874
5875 \f
5876 /***********************************************************************
5877 Moving an iterator without producing glyphs
5878 ***********************************************************************/
5879
5880 /* Check if iterator is at a position corresponding to a valid buffer
5881 position after some move_it_ call. */
5882
5883 #define IT_POS_VALID_AFTER_MOVE_P(it) \
5884 ((it)->method == GET_FROM_STRING \
5885 ? IT_STRING_CHARPOS (*it) == 0 \
5886 : 1)
5887
5888
5889 /* Move iterator IT to a specified buffer or X position within one
5890 line on the display without producing glyphs.
5891
5892 OP should be a bit mask including some or all of these bits:
5893 MOVE_TO_X: Stop on reaching x-position TO_X.
5894 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5895 Regardless of OP's value, stop in reaching the end of the display line.
5896
5897 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5898 This means, in particular, that TO_X includes window's horizontal
5899 scroll amount.
5900
5901 The return value has several possible values that
5902 say what condition caused the scan to stop:
5903
5904 MOVE_POS_MATCH_OR_ZV
5905 - when TO_POS or ZV was reached.
5906
5907 MOVE_X_REACHED
5908 -when TO_X was reached before TO_POS or ZV were reached.
5909
5910 MOVE_LINE_CONTINUED
5911 - when we reached the end of the display area and the line must
5912 be continued.
5913
5914 MOVE_LINE_TRUNCATED
5915 - when we reached the end of the display area and the line is
5916 truncated.
5917
5918 MOVE_NEWLINE_OR_CR
5919 - when we stopped at a line end, i.e. a newline or a CR and selective
5920 display is on. */
5921
5922 static enum move_it_result
5923 move_it_in_display_line_to (it, to_charpos, to_x, op)
5924 struct it *it;
5925 int to_charpos, to_x, op;
5926 {
5927 enum move_it_result result = MOVE_UNDEFINED;
5928 struct glyph_row *saved_glyph_row;
5929
5930 /* Don't produce glyphs in produce_glyphs. */
5931 saved_glyph_row = it->glyph_row;
5932 it->glyph_row = NULL;
5933
5934 #define BUFFER_POS_REACHED_P() \
5935 ((op & MOVE_TO_POS) != 0 \
5936 && BUFFERP (it->object) \
5937 && IT_CHARPOS (*it) >= to_charpos \
5938 && (it->method == GET_FROM_BUFFER \
5939 || (it->method == GET_FROM_DISPLAY_VECTOR \
5940 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5941
5942
5943 while (1)
5944 {
5945 int x, i, ascent = 0, descent = 0;
5946
5947 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5948 if ((op & MOVE_TO_POS) != 0
5949 && BUFFERP (it->object)
5950 && it->method == GET_FROM_BUFFER
5951 && IT_CHARPOS (*it) > to_charpos)
5952 {
5953 result = MOVE_POS_MATCH_OR_ZV;
5954 break;
5955 }
5956
5957 /* Stop when ZV reached.
5958 We used to stop here when TO_CHARPOS reached as well, but that is
5959 too soon if this glyph does not fit on this line. So we handle it
5960 explicitly below. */
5961 if (!get_next_display_element (it)
5962 || (it->truncate_lines_p
5963 && BUFFER_POS_REACHED_P ()))
5964 {
5965 result = MOVE_POS_MATCH_OR_ZV;
5966 break;
5967 }
5968
5969 /* The call to produce_glyphs will get the metrics of the
5970 display element IT is loaded with. We record in x the
5971 x-position before this display element in case it does not
5972 fit on the line. */
5973 x = it->current_x;
5974
5975 /* Remember the line height so far in case the next element doesn't
5976 fit on the line. */
5977 if (!it->truncate_lines_p)
5978 {
5979 ascent = it->max_ascent;
5980 descent = it->max_descent;
5981 }
5982
5983 PRODUCE_GLYPHS (it);
5984
5985 if (it->area != TEXT_AREA)
5986 {
5987 set_iterator_to_next (it, 1);
5988 continue;
5989 }
5990
5991 /* The number of glyphs we get back in IT->nglyphs will normally
5992 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5993 character on a terminal frame, or (iii) a line end. For the
5994 second case, IT->nglyphs - 1 padding glyphs will be present
5995 (on X frames, there is only one glyph produced for a
5996 composite character.
5997
5998 The behavior implemented below means, for continuation lines,
5999 that as many spaces of a TAB as fit on the current line are
6000 displayed there. For terminal frames, as many glyphs of a
6001 multi-glyph character are displayed in the current line, too.
6002 This is what the old redisplay code did, and we keep it that
6003 way. Under X, the whole shape of a complex character must
6004 fit on the line or it will be completely displayed in the
6005 next line.
6006
6007 Note that both for tabs and padding glyphs, all glyphs have
6008 the same width. */
6009 if (it->nglyphs)
6010 {
6011 /* More than one glyph or glyph doesn't fit on line. All
6012 glyphs have the same width. */
6013 int single_glyph_width = it->pixel_width / it->nglyphs;
6014 int new_x;
6015
6016 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6017 {
6018 new_x = x + single_glyph_width;
6019
6020 /* We want to leave anything reaching TO_X to the caller. */
6021 if ((op & MOVE_TO_X) && new_x > to_x)
6022 {
6023 if (BUFFER_POS_REACHED_P ())
6024 goto buffer_pos_reached;
6025 it->current_x = x;
6026 result = MOVE_X_REACHED;
6027 break;
6028 }
6029 else if (/* Lines are continued. */
6030 !it->truncate_lines_p
6031 && (/* And glyph doesn't fit on the line. */
6032 new_x > it->last_visible_x
6033 /* Or it fits exactly and we're on a window
6034 system frame. */
6035 || (new_x == it->last_visible_x
6036 && FRAME_WINDOW_P (it->f))))
6037 {
6038 if (/* IT->hpos == 0 means the very first glyph
6039 doesn't fit on the line, e.g. a wide image. */
6040 it->hpos == 0
6041 || (new_x == it->last_visible_x
6042 && FRAME_WINDOW_P (it->f)))
6043 {
6044 ++it->hpos;
6045 it->current_x = new_x;
6046 if (i == it->nglyphs - 1)
6047 {
6048 set_iterator_to_next (it, 1);
6049 #ifdef HAVE_WINDOW_SYSTEM
6050 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6051 {
6052 if (!get_next_display_element (it))
6053 {
6054 result = MOVE_POS_MATCH_OR_ZV;
6055 break;
6056 }
6057 if (BUFFER_POS_REACHED_P ())
6058 {
6059 if (ITERATOR_AT_END_OF_LINE_P (it))
6060 result = MOVE_POS_MATCH_OR_ZV;
6061 else
6062 result = MOVE_LINE_CONTINUED;
6063 break;
6064 }
6065 if (ITERATOR_AT_END_OF_LINE_P (it))
6066 {
6067 result = MOVE_NEWLINE_OR_CR;
6068 break;
6069 }
6070 }
6071 #endif /* HAVE_WINDOW_SYSTEM */
6072 }
6073 }
6074 else
6075 {
6076 it->current_x = x;
6077 it->max_ascent = ascent;
6078 it->max_descent = descent;
6079 }
6080
6081 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6082 IT_CHARPOS (*it)));
6083 result = MOVE_LINE_CONTINUED;
6084 break;
6085 }
6086 else if (BUFFER_POS_REACHED_P ())
6087 goto buffer_pos_reached;
6088 else if (new_x > it->first_visible_x)
6089 {
6090 /* Glyph is visible. Increment number of glyphs that
6091 would be displayed. */
6092 ++it->hpos;
6093 }
6094 else
6095 {
6096 /* Glyph is completely off the left margin of the display
6097 area. Nothing to do. */
6098 }
6099 }
6100
6101 if (result != MOVE_UNDEFINED)
6102 break;
6103 }
6104 else if (BUFFER_POS_REACHED_P ())
6105 {
6106 buffer_pos_reached:
6107 it->current_x = x;
6108 it->max_ascent = ascent;
6109 it->max_descent = descent;
6110 result = MOVE_POS_MATCH_OR_ZV;
6111 break;
6112 }
6113 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6114 {
6115 /* Stop when TO_X specified and reached. This check is
6116 necessary here because of lines consisting of a line end,
6117 only. The line end will not produce any glyphs and we
6118 would never get MOVE_X_REACHED. */
6119 xassert (it->nglyphs == 0);
6120 result = MOVE_X_REACHED;
6121 break;
6122 }
6123
6124 /* Is this a line end? If yes, we're done. */
6125 if (ITERATOR_AT_END_OF_LINE_P (it))
6126 {
6127 result = MOVE_NEWLINE_OR_CR;
6128 break;
6129 }
6130
6131 /* The current display element has been consumed. Advance
6132 to the next. */
6133 set_iterator_to_next (it, 1);
6134
6135 /* Stop if lines are truncated and IT's current x-position is
6136 past the right edge of the window now. */
6137 if (it->truncate_lines_p
6138 && it->current_x >= it->last_visible_x)
6139 {
6140 #ifdef HAVE_WINDOW_SYSTEM
6141 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6142 {
6143 if (!get_next_display_element (it)
6144 || BUFFER_POS_REACHED_P ())
6145 {
6146 result = MOVE_POS_MATCH_OR_ZV;
6147 break;
6148 }
6149 if (ITERATOR_AT_END_OF_LINE_P (it))
6150 {
6151 result = MOVE_NEWLINE_OR_CR;
6152 break;
6153 }
6154 }
6155 #endif /* HAVE_WINDOW_SYSTEM */
6156 result = MOVE_LINE_TRUNCATED;
6157 break;
6158 }
6159 }
6160
6161 #undef BUFFER_POS_REACHED_P
6162
6163 /* Restore the iterator settings altered at the beginning of this
6164 function. */
6165 it->glyph_row = saved_glyph_row;
6166 return result;
6167 }
6168
6169
6170 /* Move IT forward until it satisfies one or more of the criteria in
6171 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6172
6173 OP is a bit-mask that specifies where to stop, and in particular,
6174 which of those four position arguments makes a difference. See the
6175 description of enum move_operation_enum.
6176
6177 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6178 screen line, this function will set IT to the next position >
6179 TO_CHARPOS. */
6180
6181 void
6182 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6183 struct it *it;
6184 int to_charpos, to_x, to_y, to_vpos;
6185 int op;
6186 {
6187 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6188 int line_height;
6189 int reached = 0;
6190
6191 for (;;)
6192 {
6193 if (op & MOVE_TO_VPOS)
6194 {
6195 /* If no TO_CHARPOS and no TO_X specified, stop at the
6196 start of the line TO_VPOS. */
6197 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6198 {
6199 if (it->vpos == to_vpos)
6200 {
6201 reached = 1;
6202 break;
6203 }
6204 else
6205 skip = move_it_in_display_line_to (it, -1, -1, 0);
6206 }
6207 else
6208 {
6209 /* TO_VPOS >= 0 means stop at TO_X in the line at
6210 TO_VPOS, or at TO_POS, whichever comes first. */
6211 if (it->vpos == to_vpos)
6212 {
6213 reached = 2;
6214 break;
6215 }
6216
6217 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6218
6219 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6220 {
6221 reached = 3;
6222 break;
6223 }
6224 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6225 {
6226 /* We have reached TO_X but not in the line we want. */
6227 skip = move_it_in_display_line_to (it, to_charpos,
6228 -1, MOVE_TO_POS);
6229 if (skip == MOVE_POS_MATCH_OR_ZV)
6230 {
6231 reached = 4;
6232 break;
6233 }
6234 }
6235 }
6236 }
6237 else if (op & MOVE_TO_Y)
6238 {
6239 struct it it_backup;
6240
6241 /* TO_Y specified means stop at TO_X in the line containing
6242 TO_Y---or at TO_CHARPOS if this is reached first. The
6243 problem is that we can't really tell whether the line
6244 contains TO_Y before we have completely scanned it, and
6245 this may skip past TO_X. What we do is to first scan to
6246 TO_X.
6247
6248 If TO_X is not specified, use a TO_X of zero. The reason
6249 is to make the outcome of this function more predictable.
6250 If we didn't use TO_X == 0, we would stop at the end of
6251 the line which is probably not what a caller would expect
6252 to happen. */
6253 skip = move_it_in_display_line_to (it, to_charpos,
6254 ((op & MOVE_TO_X)
6255 ? to_x : 0),
6256 (MOVE_TO_X
6257 | (op & MOVE_TO_POS)));
6258
6259 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6260 if (skip == MOVE_POS_MATCH_OR_ZV)
6261 {
6262 reached = 5;
6263 break;
6264 }
6265
6266 /* If TO_X was reached, we would like to know whether TO_Y
6267 is in the line. This can only be said if we know the
6268 total line height which requires us to scan the rest of
6269 the line. */
6270 if (skip == MOVE_X_REACHED)
6271 {
6272 it_backup = *it;
6273 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6274 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6275 op & MOVE_TO_POS);
6276 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6277 }
6278
6279 /* Now, decide whether TO_Y is in this line. */
6280 line_height = it->max_ascent + it->max_descent;
6281 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6282
6283 if (to_y >= it->current_y
6284 && to_y < it->current_y + line_height)
6285 {
6286 if (skip == MOVE_X_REACHED)
6287 /* If TO_Y is in this line and TO_X was reached above,
6288 we scanned too far. We have to restore IT's settings
6289 to the ones before skipping. */
6290 *it = it_backup;
6291 reached = 6;
6292 }
6293 else if (skip == MOVE_X_REACHED)
6294 {
6295 skip = skip2;
6296 if (skip == MOVE_POS_MATCH_OR_ZV)
6297 reached = 7;
6298 }
6299
6300 if (reached)
6301 break;
6302 }
6303 else
6304 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6305
6306 switch (skip)
6307 {
6308 case MOVE_POS_MATCH_OR_ZV:
6309 reached = 8;
6310 goto out;
6311
6312 case MOVE_NEWLINE_OR_CR:
6313 set_iterator_to_next (it, 1);
6314 it->continuation_lines_width = 0;
6315 break;
6316
6317 case MOVE_LINE_TRUNCATED:
6318 it->continuation_lines_width = 0;
6319 reseat_at_next_visible_line_start (it, 0);
6320 if ((op & MOVE_TO_POS) != 0
6321 && IT_CHARPOS (*it) > to_charpos)
6322 {
6323 reached = 9;
6324 goto out;
6325 }
6326 break;
6327
6328 case MOVE_LINE_CONTINUED:
6329 it->continuation_lines_width += it->current_x;
6330 break;
6331
6332 default:
6333 abort ();
6334 }
6335
6336 /* Reset/increment for the next run. */
6337 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6338 it->current_x = it->hpos = 0;
6339 it->current_y += it->max_ascent + it->max_descent;
6340 ++it->vpos;
6341 last_height = it->max_ascent + it->max_descent;
6342 last_max_ascent = it->max_ascent;
6343 it->max_ascent = it->max_descent = 0;
6344 }
6345
6346 out:
6347
6348 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6349 }
6350
6351
6352 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6353
6354 If DY > 0, move IT backward at least that many pixels. DY = 0
6355 means move IT backward to the preceding line start or BEGV. This
6356 function may move over more than DY pixels if IT->current_y - DY
6357 ends up in the middle of a line; in this case IT->current_y will be
6358 set to the top of the line moved to. */
6359
6360 void
6361 move_it_vertically_backward (it, dy)
6362 struct it *it;
6363 int dy;
6364 {
6365 int nlines, h;
6366 struct it it2, it3;
6367 int start_pos;
6368
6369 move_further_back:
6370 xassert (dy >= 0);
6371
6372 start_pos = IT_CHARPOS (*it);
6373
6374 /* Estimate how many newlines we must move back. */
6375 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6376
6377 /* Set the iterator's position that many lines back. */
6378 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6379 back_to_previous_visible_line_start (it);
6380
6381 /* Reseat the iterator here. When moving backward, we don't want
6382 reseat to skip forward over invisible text, set up the iterator
6383 to deliver from overlay strings at the new position etc. So,
6384 use reseat_1 here. */
6385 reseat_1 (it, it->current.pos, 1);
6386
6387 /* We are now surely at a line start. */
6388 it->current_x = it->hpos = 0;
6389 it->continuation_lines_width = 0;
6390
6391 /* Move forward and see what y-distance we moved. First move to the
6392 start of the next line so that we get its height. We need this
6393 height to be able to tell whether we reached the specified
6394 y-distance. */
6395 it2 = *it;
6396 it2.max_ascent = it2.max_descent = 0;
6397 do
6398 {
6399 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6400 MOVE_TO_POS | MOVE_TO_VPOS);
6401 }
6402 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6403 xassert (IT_CHARPOS (*it) >= BEGV);
6404 it3 = it2;
6405
6406 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6407 xassert (IT_CHARPOS (*it) >= BEGV);
6408 /* H is the actual vertical distance from the position in *IT
6409 and the starting position. */
6410 h = it2.current_y - it->current_y;
6411 /* NLINES is the distance in number of lines. */
6412 nlines = it2.vpos - it->vpos;
6413
6414 /* Correct IT's y and vpos position
6415 so that they are relative to the starting point. */
6416 it->vpos -= nlines;
6417 it->current_y -= h;
6418
6419 if (dy == 0)
6420 {
6421 /* DY == 0 means move to the start of the screen line. The
6422 value of nlines is > 0 if continuation lines were involved. */
6423 if (nlines > 0)
6424 move_it_by_lines (it, nlines, 1);
6425 #if 0
6426 /* I think this assert is bogus if buffer contains
6427 invisible text or images. KFS. */
6428 xassert (IT_CHARPOS (*it) <= start_pos);
6429 #endif
6430 }
6431 else
6432 {
6433 /* The y-position we try to reach, relative to *IT.
6434 Note that H has been subtracted in front of the if-statement. */
6435 int target_y = it->current_y + h - dy;
6436 int y0 = it3.current_y;
6437 int y1 = line_bottom_y (&it3);
6438 int line_height = y1 - y0;
6439
6440 /* If we did not reach target_y, try to move further backward if
6441 we can. If we moved too far backward, try to move forward. */
6442 if (target_y < it->current_y
6443 /* This is heuristic. In a window that's 3 lines high, with
6444 a line height of 13 pixels each, recentering with point
6445 on the bottom line will try to move -39/2 = 19 pixels
6446 backward. Try to avoid moving into the first line. */
6447 && (it->current_y - target_y
6448 > min (window_box_height (it->w), line_height * 2 / 3))
6449 && IT_CHARPOS (*it) > BEGV)
6450 {
6451 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6452 target_y - it->current_y));
6453 dy = it->current_y - target_y;
6454 goto move_further_back;
6455 }
6456 else if (target_y >= it->current_y + line_height
6457 && IT_CHARPOS (*it) < ZV)
6458 {
6459 /* Should move forward by at least one line, maybe more.
6460
6461 Note: Calling move_it_by_lines can be expensive on
6462 terminal frames, where compute_motion is used (via
6463 vmotion) to do the job, when there are very long lines
6464 and truncate-lines is nil. That's the reason for
6465 treating terminal frames specially here. */
6466
6467 if (!FRAME_WINDOW_P (it->f))
6468 move_it_vertically (it, target_y - (it->current_y + line_height));
6469 else
6470 {
6471 do
6472 {
6473 move_it_by_lines (it, 1, 1);
6474 }
6475 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6476 }
6477
6478 #if 0
6479 /* I think this assert is bogus if buffer contains
6480 invisible text or images. KFS. */
6481 xassert (IT_CHARPOS (*it) >= BEGV);
6482 #endif
6483 }
6484 }
6485 }
6486
6487
6488 /* Move IT by a specified amount of pixel lines DY. DY negative means
6489 move backwards. DY = 0 means move to start of screen line. At the
6490 end, IT will be on the start of a screen line. */
6491
6492 void
6493 move_it_vertically (it, dy)
6494 struct it *it;
6495 int dy;
6496 {
6497 if (dy <= 0)
6498 move_it_vertically_backward (it, -dy);
6499 else
6500 {
6501 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6502 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6503 MOVE_TO_POS | MOVE_TO_Y);
6504 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6505
6506 /* If buffer ends in ZV without a newline, move to the start of
6507 the line to satisfy the post-condition. */
6508 if (IT_CHARPOS (*it) == ZV
6509 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6510 move_it_by_lines (it, 0, 0);
6511 }
6512 }
6513
6514
6515 /* Move iterator IT past the end of the text line it is in. */
6516
6517 void
6518 move_it_past_eol (it)
6519 struct it *it;
6520 {
6521 enum move_it_result rc;
6522
6523 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6524 if (rc == MOVE_NEWLINE_OR_CR)
6525 set_iterator_to_next (it, 0);
6526 }
6527
6528
6529 #if 0 /* Currently not used. */
6530
6531 /* Return non-zero if some text between buffer positions START_CHARPOS
6532 and END_CHARPOS is invisible. IT->window is the window for text
6533 property lookup. */
6534
6535 static int
6536 invisible_text_between_p (it, start_charpos, end_charpos)
6537 struct it *it;
6538 int start_charpos, end_charpos;
6539 {
6540 Lisp_Object prop, limit;
6541 int invisible_found_p;
6542
6543 xassert (it != NULL && start_charpos <= end_charpos);
6544
6545 /* Is text at START invisible? */
6546 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6547 it->window);
6548 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6549 invisible_found_p = 1;
6550 else
6551 {
6552 limit = Fnext_single_char_property_change (make_number (start_charpos),
6553 Qinvisible, Qnil,
6554 make_number (end_charpos));
6555 invisible_found_p = XFASTINT (limit) < end_charpos;
6556 }
6557
6558 return invisible_found_p;
6559 }
6560
6561 #endif /* 0 */
6562
6563
6564 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6565 negative means move up. DVPOS == 0 means move to the start of the
6566 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6567 NEED_Y_P is zero, IT->current_y will be left unchanged.
6568
6569 Further optimization ideas: If we would know that IT->f doesn't use
6570 a face with proportional font, we could be faster for
6571 truncate-lines nil. */
6572
6573 void
6574 move_it_by_lines (it, dvpos, need_y_p)
6575 struct it *it;
6576 int dvpos, need_y_p;
6577 {
6578 struct position pos;
6579
6580 if (!FRAME_WINDOW_P (it->f))
6581 {
6582 struct text_pos textpos;
6583
6584 /* We can use vmotion on frames without proportional fonts. */
6585 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6586 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6587 reseat (it, textpos, 1);
6588 it->vpos += pos.vpos;
6589 it->current_y += pos.vpos;
6590 }
6591 else if (dvpos == 0)
6592 {
6593 /* DVPOS == 0 means move to the start of the screen line. */
6594 move_it_vertically_backward (it, 0);
6595 xassert (it->current_x == 0 && it->hpos == 0);
6596 /* Let next call to line_bottom_y calculate real line height */
6597 last_height = 0;
6598 }
6599 else if (dvpos > 0)
6600 {
6601 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6602 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6603 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6604 }
6605 else
6606 {
6607 struct it it2;
6608 int start_charpos, i;
6609
6610 /* Start at the beginning of the screen line containing IT's
6611 position. This may actually move vertically backwards,
6612 in case of overlays, so adjust dvpos accordingly. */
6613 dvpos += it->vpos;
6614 move_it_vertically_backward (it, 0);
6615 dvpos -= it->vpos;
6616
6617 /* Go back -DVPOS visible lines and reseat the iterator there. */
6618 start_charpos = IT_CHARPOS (*it);
6619 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6620 back_to_previous_visible_line_start (it);
6621 reseat (it, it->current.pos, 1);
6622
6623 /* Move further back if we end up in a string or an image. */
6624 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6625 {
6626 /* First try to move to start of display line. */
6627 dvpos += it->vpos;
6628 move_it_vertically_backward (it, 0);
6629 dvpos -= it->vpos;
6630 if (IT_POS_VALID_AFTER_MOVE_P (it))
6631 break;
6632 /* If start of line is still in string or image,
6633 move further back. */
6634 back_to_previous_visible_line_start (it);
6635 reseat (it, it->current.pos, 1);
6636 dvpos--;
6637 }
6638
6639 it->current_x = it->hpos = 0;
6640
6641 /* Above call may have moved too far if continuation lines
6642 are involved. Scan forward and see if it did. */
6643 it2 = *it;
6644 it2.vpos = it2.current_y = 0;
6645 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6646 it->vpos -= it2.vpos;
6647 it->current_y -= it2.current_y;
6648 it->current_x = it->hpos = 0;
6649
6650 /* If we moved too far back, move IT some lines forward. */
6651 if (it2.vpos > -dvpos)
6652 {
6653 int delta = it2.vpos + dvpos;
6654 it2 = *it;
6655 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6656 /* Move back again if we got too far ahead. */
6657 if (IT_CHARPOS (*it) >= start_charpos)
6658 *it = it2;
6659 }
6660 }
6661 }
6662
6663 /* Return 1 if IT points into the middle of a display vector. */
6664
6665 int
6666 in_display_vector_p (it)
6667 struct it *it;
6668 {
6669 return (it->method == GET_FROM_DISPLAY_VECTOR
6670 && it->current.dpvec_index > 0
6671 && it->dpvec + it->current.dpvec_index != it->dpend);
6672 }
6673
6674 \f
6675 /***********************************************************************
6676 Messages
6677 ***********************************************************************/
6678
6679
6680 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6681 to *Messages*. */
6682
6683 void
6684 add_to_log (format, arg1, arg2)
6685 char *format;
6686 Lisp_Object arg1, arg2;
6687 {
6688 Lisp_Object args[3];
6689 Lisp_Object msg, fmt;
6690 char *buffer;
6691 int len;
6692 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6693 USE_SAFE_ALLOCA;
6694
6695 /* Do nothing if called asynchronously. Inserting text into
6696 a buffer may call after-change-functions and alike and
6697 that would means running Lisp asynchronously. */
6698 if (handling_signal)
6699 return;
6700
6701 fmt = msg = Qnil;
6702 GCPRO4 (fmt, msg, arg1, arg2);
6703
6704 args[0] = fmt = build_string (format);
6705 args[1] = arg1;
6706 args[2] = arg2;
6707 msg = Fformat (3, args);
6708
6709 len = SBYTES (msg) + 1;
6710 SAFE_ALLOCA (buffer, char *, len);
6711 bcopy (SDATA (msg), buffer, len);
6712
6713 message_dolog (buffer, len - 1, 1, 0);
6714 SAFE_FREE ();
6715
6716 UNGCPRO;
6717 }
6718
6719
6720 /* Output a newline in the *Messages* buffer if "needs" one. */
6721
6722 void
6723 message_log_maybe_newline ()
6724 {
6725 if (message_log_need_newline)
6726 message_dolog ("", 0, 1, 0);
6727 }
6728
6729
6730 /* Add a string M of length NBYTES to the message log, optionally
6731 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6732 nonzero, means interpret the contents of M as multibyte. This
6733 function calls low-level routines in order to bypass text property
6734 hooks, etc. which might not be safe to run. */
6735
6736 void
6737 message_dolog (m, nbytes, nlflag, multibyte)
6738 const char *m;
6739 int nbytes, nlflag, multibyte;
6740 {
6741 if (!NILP (Vmemory_full))
6742 return;
6743
6744 if (!NILP (Vmessage_log_max))
6745 {
6746 struct buffer *oldbuf;
6747 Lisp_Object oldpoint, oldbegv, oldzv;
6748 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6749 int point_at_end = 0;
6750 int zv_at_end = 0;
6751 Lisp_Object old_deactivate_mark, tem;
6752 struct gcpro gcpro1;
6753
6754 old_deactivate_mark = Vdeactivate_mark;
6755 oldbuf = current_buffer;
6756 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6757 current_buffer->undo_list = Qt;
6758
6759 oldpoint = message_dolog_marker1;
6760 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6761 oldbegv = message_dolog_marker2;
6762 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6763 oldzv = message_dolog_marker3;
6764 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6765 GCPRO1 (old_deactivate_mark);
6766
6767 if (PT == Z)
6768 point_at_end = 1;
6769 if (ZV == Z)
6770 zv_at_end = 1;
6771
6772 BEGV = BEG;
6773 BEGV_BYTE = BEG_BYTE;
6774 ZV = Z;
6775 ZV_BYTE = Z_BYTE;
6776 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6777
6778 /* Insert the string--maybe converting multibyte to single byte
6779 or vice versa, so that all the text fits the buffer. */
6780 if (multibyte
6781 && NILP (current_buffer->enable_multibyte_characters))
6782 {
6783 int i, c, char_bytes;
6784 unsigned char work[1];
6785
6786 /* Convert a multibyte string to single-byte
6787 for the *Message* buffer. */
6788 for (i = 0; i < nbytes; i += char_bytes)
6789 {
6790 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6791 work[0] = (SINGLE_BYTE_CHAR_P (c)
6792 ? c
6793 : multibyte_char_to_unibyte (c, Qnil));
6794 insert_1_both (work, 1, 1, 1, 0, 0);
6795 }
6796 }
6797 else if (! multibyte
6798 && ! NILP (current_buffer->enable_multibyte_characters))
6799 {
6800 int i, c, char_bytes;
6801 unsigned char *msg = (unsigned char *) m;
6802 unsigned char str[MAX_MULTIBYTE_LENGTH];
6803 /* Convert a single-byte string to multibyte
6804 for the *Message* buffer. */
6805 for (i = 0; i < nbytes; i++)
6806 {
6807 c = unibyte_char_to_multibyte (msg[i]);
6808 char_bytes = CHAR_STRING (c, str);
6809 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6810 }
6811 }
6812 else if (nbytes)
6813 insert_1 (m, nbytes, 1, 0, 0);
6814
6815 if (nlflag)
6816 {
6817 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6818 insert_1 ("\n", 1, 1, 0, 0);
6819
6820 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6821 this_bol = PT;
6822 this_bol_byte = PT_BYTE;
6823
6824 /* See if this line duplicates the previous one.
6825 If so, combine duplicates. */
6826 if (this_bol > BEG)
6827 {
6828 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6829 prev_bol = PT;
6830 prev_bol_byte = PT_BYTE;
6831
6832 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6833 this_bol, this_bol_byte);
6834 if (dup)
6835 {
6836 del_range_both (prev_bol, prev_bol_byte,
6837 this_bol, this_bol_byte, 0);
6838 if (dup > 1)
6839 {
6840 char dupstr[40];
6841 int duplen;
6842
6843 /* If you change this format, don't forget to also
6844 change message_log_check_duplicate. */
6845 sprintf (dupstr, " [%d times]", dup);
6846 duplen = strlen (dupstr);
6847 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6848 insert_1 (dupstr, duplen, 1, 0, 1);
6849 }
6850 }
6851 }
6852
6853 /* If we have more than the desired maximum number of lines
6854 in the *Messages* buffer now, delete the oldest ones.
6855 This is safe because we don't have undo in this buffer. */
6856
6857 if (NATNUMP (Vmessage_log_max))
6858 {
6859 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6860 -XFASTINT (Vmessage_log_max) - 1, 0);
6861 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6862 }
6863 }
6864 BEGV = XMARKER (oldbegv)->charpos;
6865 BEGV_BYTE = marker_byte_position (oldbegv);
6866
6867 if (zv_at_end)
6868 {
6869 ZV = Z;
6870 ZV_BYTE = Z_BYTE;
6871 }
6872 else
6873 {
6874 ZV = XMARKER (oldzv)->charpos;
6875 ZV_BYTE = marker_byte_position (oldzv);
6876 }
6877
6878 if (point_at_end)
6879 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6880 else
6881 /* We can't do Fgoto_char (oldpoint) because it will run some
6882 Lisp code. */
6883 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6884 XMARKER (oldpoint)->bytepos);
6885
6886 UNGCPRO;
6887 unchain_marker (XMARKER (oldpoint));
6888 unchain_marker (XMARKER (oldbegv));
6889 unchain_marker (XMARKER (oldzv));
6890
6891 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6892 set_buffer_internal (oldbuf);
6893 if (NILP (tem))
6894 windows_or_buffers_changed = old_windows_or_buffers_changed;
6895 message_log_need_newline = !nlflag;
6896 Vdeactivate_mark = old_deactivate_mark;
6897 }
6898 }
6899
6900
6901 /* We are at the end of the buffer after just having inserted a newline.
6902 (Note: We depend on the fact we won't be crossing the gap.)
6903 Check to see if the most recent message looks a lot like the previous one.
6904 Return 0 if different, 1 if the new one should just replace it, or a
6905 value N > 1 if we should also append " [N times]". */
6906
6907 static int
6908 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6909 int prev_bol, this_bol;
6910 int prev_bol_byte, this_bol_byte;
6911 {
6912 int i;
6913 int len = Z_BYTE - 1 - this_bol_byte;
6914 int seen_dots = 0;
6915 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6916 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6917
6918 for (i = 0; i < len; i++)
6919 {
6920 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6921 seen_dots = 1;
6922 if (p1[i] != p2[i])
6923 return seen_dots;
6924 }
6925 p1 += len;
6926 if (*p1 == '\n')
6927 return 2;
6928 if (*p1++ == ' ' && *p1++ == '[')
6929 {
6930 int n = 0;
6931 while (*p1 >= '0' && *p1 <= '9')
6932 n = n * 10 + *p1++ - '0';
6933 if (strncmp (p1, " times]\n", 8) == 0)
6934 return n+1;
6935 }
6936 return 0;
6937 }
6938 \f
6939
6940 /* Display an echo area message M with a specified length of NBYTES
6941 bytes. The string may include null characters. If M is 0, clear
6942 out any existing message, and let the mini-buffer text show
6943 through.
6944
6945 The buffer M must continue to exist until after the echo area gets
6946 cleared or some other message gets displayed there. This means do
6947 not pass text that is stored in a Lisp string; do not pass text in
6948 a buffer that was alloca'd. */
6949
6950 void
6951 message2 (m, nbytes, multibyte)
6952 const char *m;
6953 int nbytes;
6954 int multibyte;
6955 {
6956 /* First flush out any partial line written with print. */
6957 message_log_maybe_newline ();
6958 if (m)
6959 message_dolog (m, nbytes, 1, multibyte);
6960 message2_nolog (m, nbytes, multibyte);
6961 }
6962
6963
6964 /* The non-logging counterpart of message2. */
6965
6966 void
6967 message2_nolog (m, nbytes, multibyte)
6968 const char *m;
6969 int nbytes, multibyte;
6970 {
6971 struct frame *sf = SELECTED_FRAME ();
6972 message_enable_multibyte = multibyte;
6973
6974 if (noninteractive)
6975 {
6976 if (noninteractive_need_newline)
6977 putc ('\n', stderr);
6978 noninteractive_need_newline = 0;
6979 if (m)
6980 fwrite (m, nbytes, 1, stderr);
6981 if (cursor_in_echo_area == 0)
6982 fprintf (stderr, "\n");
6983 fflush (stderr);
6984 }
6985 /* A null message buffer means that the frame hasn't really been
6986 initialized yet. Error messages get reported properly by
6987 cmd_error, so this must be just an informative message; toss it. */
6988 else if (INTERACTIVE
6989 && sf->glyphs_initialized_p
6990 && FRAME_MESSAGE_BUF (sf))
6991 {
6992 Lisp_Object mini_window;
6993 struct frame *f;
6994
6995 /* Get the frame containing the mini-buffer
6996 that the selected frame is using. */
6997 mini_window = FRAME_MINIBUF_WINDOW (sf);
6998 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6999
7000 FRAME_SAMPLE_VISIBILITY (f);
7001 if (FRAME_VISIBLE_P (sf)
7002 && ! FRAME_VISIBLE_P (f))
7003 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7004
7005 if (m)
7006 {
7007 set_message (m, Qnil, nbytes, multibyte);
7008 if (minibuffer_auto_raise)
7009 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7010 }
7011 else
7012 clear_message (1, 1);
7013
7014 do_pending_window_change (0);
7015 echo_area_display (1);
7016 do_pending_window_change (0);
7017 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7018 (*frame_up_to_date_hook) (f);
7019 }
7020 }
7021
7022
7023 /* Display an echo area message M with a specified length of NBYTES
7024 bytes. The string may include null characters. If M is not a
7025 string, clear out any existing message, and let the mini-buffer
7026 text show through.
7027
7028 This function cancels echoing. */
7029
7030 void
7031 message3 (m, nbytes, multibyte)
7032 Lisp_Object m;
7033 int nbytes;
7034 int multibyte;
7035 {
7036 struct gcpro gcpro1;
7037
7038 GCPRO1 (m);
7039 clear_message (1,1);
7040 cancel_echoing ();
7041
7042 /* First flush out any partial line written with print. */
7043 message_log_maybe_newline ();
7044 if (STRINGP (m))
7045 message_dolog (SDATA (m), nbytes, 1, multibyte);
7046 message3_nolog (m, nbytes, multibyte);
7047
7048 UNGCPRO;
7049 }
7050
7051
7052 /* The non-logging version of message3.
7053 This does not cancel echoing, because it is used for echoing.
7054 Perhaps we need to make a separate function for echoing
7055 and make this cancel echoing. */
7056
7057 void
7058 message3_nolog (m, nbytes, multibyte)
7059 Lisp_Object m;
7060 int nbytes, multibyte;
7061 {
7062 struct frame *sf = SELECTED_FRAME ();
7063 message_enable_multibyte = multibyte;
7064
7065 if (noninteractive)
7066 {
7067 if (noninteractive_need_newline)
7068 putc ('\n', stderr);
7069 noninteractive_need_newline = 0;
7070 if (STRINGP (m))
7071 fwrite (SDATA (m), nbytes, 1, stderr);
7072 if (cursor_in_echo_area == 0)
7073 fprintf (stderr, "\n");
7074 fflush (stderr);
7075 }
7076 /* A null message buffer means that the frame hasn't really been
7077 initialized yet. Error messages get reported properly by
7078 cmd_error, so this must be just an informative message; toss it. */
7079 else if (INTERACTIVE
7080 && sf->glyphs_initialized_p
7081 && FRAME_MESSAGE_BUF (sf))
7082 {
7083 Lisp_Object mini_window;
7084 Lisp_Object frame;
7085 struct frame *f;
7086
7087 /* Get the frame containing the mini-buffer
7088 that the selected frame is using. */
7089 mini_window = FRAME_MINIBUF_WINDOW (sf);
7090 frame = XWINDOW (mini_window)->frame;
7091 f = XFRAME (frame);
7092
7093 FRAME_SAMPLE_VISIBILITY (f);
7094 if (FRAME_VISIBLE_P (sf)
7095 && !FRAME_VISIBLE_P (f))
7096 Fmake_frame_visible (frame);
7097
7098 if (STRINGP (m) && SCHARS (m) > 0)
7099 {
7100 set_message (NULL, m, nbytes, multibyte);
7101 if (minibuffer_auto_raise)
7102 Fraise_frame (frame);
7103 /* Assume we are not echoing.
7104 (If we are, echo_now will override this.) */
7105 echo_message_buffer = Qnil;
7106 }
7107 else
7108 clear_message (1, 1);
7109
7110 do_pending_window_change (0);
7111 echo_area_display (1);
7112 do_pending_window_change (0);
7113 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7114 (*frame_up_to_date_hook) (f);
7115 }
7116 }
7117
7118
7119 /* Display a null-terminated echo area message M. If M is 0, clear
7120 out any existing message, and let the mini-buffer text show through.
7121
7122 The buffer M must continue to exist until after the echo area gets
7123 cleared or some other message gets displayed there. Do not pass
7124 text that is stored in a Lisp string. Do not pass text in a buffer
7125 that was alloca'd. */
7126
7127 void
7128 message1 (m)
7129 char *m;
7130 {
7131 message2 (m, (m ? strlen (m) : 0), 0);
7132 }
7133
7134
7135 /* The non-logging counterpart of message1. */
7136
7137 void
7138 message1_nolog (m)
7139 char *m;
7140 {
7141 message2_nolog (m, (m ? strlen (m) : 0), 0);
7142 }
7143
7144 /* Display a message M which contains a single %s
7145 which gets replaced with STRING. */
7146
7147 void
7148 message_with_string (m, string, log)
7149 char *m;
7150 Lisp_Object string;
7151 int log;
7152 {
7153 CHECK_STRING (string);
7154
7155 if (noninteractive)
7156 {
7157 if (m)
7158 {
7159 if (noninteractive_need_newline)
7160 putc ('\n', stderr);
7161 noninteractive_need_newline = 0;
7162 fprintf (stderr, m, SDATA (string));
7163 if (cursor_in_echo_area == 0)
7164 fprintf (stderr, "\n");
7165 fflush (stderr);
7166 }
7167 }
7168 else if (INTERACTIVE)
7169 {
7170 /* The frame whose minibuffer we're going to display the message on.
7171 It may be larger than the selected frame, so we need
7172 to use its buffer, not the selected frame's buffer. */
7173 Lisp_Object mini_window;
7174 struct frame *f, *sf = SELECTED_FRAME ();
7175
7176 /* Get the frame containing the minibuffer
7177 that the selected frame is using. */
7178 mini_window = FRAME_MINIBUF_WINDOW (sf);
7179 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7180
7181 /* A null message buffer means that the frame hasn't really been
7182 initialized yet. Error messages get reported properly by
7183 cmd_error, so this must be just an informative message; toss it. */
7184 if (FRAME_MESSAGE_BUF (f))
7185 {
7186 Lisp_Object args[2], message;
7187 struct gcpro gcpro1, gcpro2;
7188
7189 args[0] = build_string (m);
7190 args[1] = message = string;
7191 GCPRO2 (args[0], message);
7192 gcpro1.nvars = 2;
7193
7194 message = Fformat (2, args);
7195
7196 if (log)
7197 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7198 else
7199 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7200
7201 UNGCPRO;
7202
7203 /* Print should start at the beginning of the message
7204 buffer next time. */
7205 message_buf_print = 0;
7206 }
7207 }
7208 }
7209
7210
7211 /* Dump an informative message to the minibuf. If M is 0, clear out
7212 any existing message, and let the mini-buffer text show through. */
7213
7214 /* VARARGS 1 */
7215 void
7216 message (m, a1, a2, a3)
7217 char *m;
7218 EMACS_INT a1, a2, a3;
7219 {
7220 if (noninteractive)
7221 {
7222 if (m)
7223 {
7224 if (noninteractive_need_newline)
7225 putc ('\n', stderr);
7226 noninteractive_need_newline = 0;
7227 fprintf (stderr, m, a1, a2, a3);
7228 if (cursor_in_echo_area == 0)
7229 fprintf (stderr, "\n");
7230 fflush (stderr);
7231 }
7232 }
7233 else if (INTERACTIVE)
7234 {
7235 /* The frame whose mini-buffer we're going to display the message
7236 on. It may be larger than the selected frame, so we need to
7237 use its buffer, not the selected frame's buffer. */
7238 Lisp_Object mini_window;
7239 struct frame *f, *sf = SELECTED_FRAME ();
7240
7241 /* Get the frame containing the mini-buffer
7242 that the selected frame is using. */
7243 mini_window = FRAME_MINIBUF_WINDOW (sf);
7244 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7245
7246 /* A null message buffer means that the frame hasn't really been
7247 initialized yet. Error messages get reported properly by
7248 cmd_error, so this must be just an informative message; toss
7249 it. */
7250 if (FRAME_MESSAGE_BUF (f))
7251 {
7252 if (m)
7253 {
7254 int len;
7255 #ifdef NO_ARG_ARRAY
7256 char *a[3];
7257 a[0] = (char *) a1;
7258 a[1] = (char *) a2;
7259 a[2] = (char *) a3;
7260
7261 len = doprnt (FRAME_MESSAGE_BUF (f),
7262 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7263 #else
7264 len = doprnt (FRAME_MESSAGE_BUF (f),
7265 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7266 (char **) &a1);
7267 #endif /* NO_ARG_ARRAY */
7268
7269 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7270 }
7271 else
7272 message1 (0);
7273
7274 /* Print should start at the beginning of the message
7275 buffer next time. */
7276 message_buf_print = 0;
7277 }
7278 }
7279 }
7280
7281
7282 /* The non-logging version of message. */
7283
7284 void
7285 message_nolog (m, a1, a2, a3)
7286 char *m;
7287 EMACS_INT a1, a2, a3;
7288 {
7289 Lisp_Object old_log_max;
7290 old_log_max = Vmessage_log_max;
7291 Vmessage_log_max = Qnil;
7292 message (m, a1, a2, a3);
7293 Vmessage_log_max = old_log_max;
7294 }
7295
7296
7297 /* Display the current message in the current mini-buffer. This is
7298 only called from error handlers in process.c, and is not time
7299 critical. */
7300
7301 void
7302 update_echo_area ()
7303 {
7304 if (!NILP (echo_area_buffer[0]))
7305 {
7306 Lisp_Object string;
7307 string = Fcurrent_message ();
7308 message3 (string, SBYTES (string),
7309 !NILP (current_buffer->enable_multibyte_characters));
7310 }
7311 }
7312
7313
7314 /* Make sure echo area buffers in `echo_buffers' are live.
7315 If they aren't, make new ones. */
7316
7317 static void
7318 ensure_echo_area_buffers ()
7319 {
7320 int i;
7321
7322 for (i = 0; i < 2; ++i)
7323 if (!BUFFERP (echo_buffer[i])
7324 || NILP (XBUFFER (echo_buffer[i])->name))
7325 {
7326 char name[30];
7327 Lisp_Object old_buffer;
7328 int j;
7329
7330 old_buffer = echo_buffer[i];
7331 sprintf (name, " *Echo Area %d*", i);
7332 echo_buffer[i] = Fget_buffer_create (build_string (name));
7333 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7334
7335 for (j = 0; j < 2; ++j)
7336 if (EQ (old_buffer, echo_area_buffer[j]))
7337 echo_area_buffer[j] = echo_buffer[i];
7338 }
7339 }
7340
7341
7342 /* Call FN with args A1..A4 with either the current or last displayed
7343 echo_area_buffer as current buffer.
7344
7345 WHICH zero means use the current message buffer
7346 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7347 from echo_buffer[] and clear it.
7348
7349 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7350 suitable buffer from echo_buffer[] and clear it.
7351
7352 Value is what FN returns. */
7353
7354 static int
7355 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7356 struct window *w;
7357 int which;
7358 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7359 EMACS_INT a1;
7360 Lisp_Object a2;
7361 EMACS_INT a3, a4;
7362 {
7363 Lisp_Object buffer;
7364 int this_one, the_other, clear_buffer_p, rc;
7365 int count = SPECPDL_INDEX ();
7366
7367 /* If buffers aren't live, make new ones. */
7368 ensure_echo_area_buffers ();
7369
7370 clear_buffer_p = 0;
7371
7372 if (which == 0)
7373 this_one = 0, the_other = 1;
7374 else if (which > 0)
7375 this_one = 1, the_other = 0;
7376
7377 /* Choose a suitable buffer from echo_buffer[] is we don't
7378 have one. */
7379 if (NILP (echo_area_buffer[this_one]))
7380 {
7381 echo_area_buffer[this_one]
7382 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7383 ? echo_buffer[the_other]
7384 : echo_buffer[this_one]);
7385 clear_buffer_p = 1;
7386 }
7387
7388 buffer = echo_area_buffer[this_one];
7389
7390 /* Don't get confused by reusing the buffer used for echoing
7391 for a different purpose. */
7392 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7393 cancel_echoing ();
7394
7395 record_unwind_protect (unwind_with_echo_area_buffer,
7396 with_echo_area_buffer_unwind_data (w));
7397
7398 /* Make the echo area buffer current. Note that for display
7399 purposes, it is not necessary that the displayed window's buffer
7400 == current_buffer, except for text property lookup. So, let's
7401 only set that buffer temporarily here without doing a full
7402 Fset_window_buffer. We must also change w->pointm, though,
7403 because otherwise an assertions in unshow_buffer fails, and Emacs
7404 aborts. */
7405 set_buffer_internal_1 (XBUFFER (buffer));
7406 if (w)
7407 {
7408 w->buffer = buffer;
7409 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7410 }
7411
7412 current_buffer->undo_list = Qt;
7413 current_buffer->read_only = Qnil;
7414 specbind (Qinhibit_read_only, Qt);
7415 specbind (Qinhibit_modification_hooks, Qt);
7416
7417 if (clear_buffer_p && Z > BEG)
7418 del_range (BEG, Z);
7419
7420 xassert (BEGV >= BEG);
7421 xassert (ZV <= Z && ZV >= BEGV);
7422
7423 rc = fn (a1, a2, a3, a4);
7424
7425 xassert (BEGV >= BEG);
7426 xassert (ZV <= Z && ZV >= BEGV);
7427
7428 unbind_to (count, Qnil);
7429 return rc;
7430 }
7431
7432
7433 /* Save state that should be preserved around the call to the function
7434 FN called in with_echo_area_buffer. */
7435
7436 static Lisp_Object
7437 with_echo_area_buffer_unwind_data (w)
7438 struct window *w;
7439 {
7440 int i = 0;
7441 Lisp_Object vector;
7442
7443 /* Reduce consing by keeping one vector in
7444 Vwith_echo_area_save_vector. */
7445 vector = Vwith_echo_area_save_vector;
7446 Vwith_echo_area_save_vector = Qnil;
7447
7448 if (NILP (vector))
7449 vector = Fmake_vector (make_number (7), Qnil);
7450
7451 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7452 AREF (vector, i) = Vdeactivate_mark, ++i;
7453 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7454
7455 if (w)
7456 {
7457 XSETWINDOW (AREF (vector, i), w); ++i;
7458 AREF (vector, i) = w->buffer; ++i;
7459 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7460 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7461 }
7462 else
7463 {
7464 int end = i + 4;
7465 for (; i < end; ++i)
7466 AREF (vector, i) = Qnil;
7467 }
7468
7469 xassert (i == ASIZE (vector));
7470 return vector;
7471 }
7472
7473
7474 /* Restore global state from VECTOR which was created by
7475 with_echo_area_buffer_unwind_data. */
7476
7477 static Lisp_Object
7478 unwind_with_echo_area_buffer (vector)
7479 Lisp_Object vector;
7480 {
7481 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7482 Vdeactivate_mark = AREF (vector, 1);
7483 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7484
7485 if (WINDOWP (AREF (vector, 3)))
7486 {
7487 struct window *w;
7488 Lisp_Object buffer, charpos, bytepos;
7489
7490 w = XWINDOW (AREF (vector, 3));
7491 buffer = AREF (vector, 4);
7492 charpos = AREF (vector, 5);
7493 bytepos = AREF (vector, 6);
7494
7495 w->buffer = buffer;
7496 set_marker_both (w->pointm, buffer,
7497 XFASTINT (charpos), XFASTINT (bytepos));
7498 }
7499
7500 Vwith_echo_area_save_vector = vector;
7501 return Qnil;
7502 }
7503
7504
7505 /* Set up the echo area for use by print functions. MULTIBYTE_P
7506 non-zero means we will print multibyte. */
7507
7508 void
7509 setup_echo_area_for_printing (multibyte_p)
7510 int multibyte_p;
7511 {
7512 /* If we can't find an echo area any more, exit. */
7513 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7514 Fkill_emacs (Qnil);
7515
7516 ensure_echo_area_buffers ();
7517
7518 if (!message_buf_print)
7519 {
7520 /* A message has been output since the last time we printed.
7521 Choose a fresh echo area buffer. */
7522 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7523 echo_area_buffer[0] = echo_buffer[1];
7524 else
7525 echo_area_buffer[0] = echo_buffer[0];
7526
7527 /* Switch to that buffer and clear it. */
7528 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7529 current_buffer->truncate_lines = Qnil;
7530
7531 if (Z > BEG)
7532 {
7533 int count = SPECPDL_INDEX ();
7534 specbind (Qinhibit_read_only, Qt);
7535 /* Note that undo recording is always disabled. */
7536 del_range (BEG, Z);
7537 unbind_to (count, Qnil);
7538 }
7539 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7540
7541 /* Set up the buffer for the multibyteness we need. */
7542 if (multibyte_p
7543 != !NILP (current_buffer->enable_multibyte_characters))
7544 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7545
7546 /* Raise the frame containing the echo area. */
7547 if (minibuffer_auto_raise)
7548 {
7549 struct frame *sf = SELECTED_FRAME ();
7550 Lisp_Object mini_window;
7551 mini_window = FRAME_MINIBUF_WINDOW (sf);
7552 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7553 }
7554
7555 message_log_maybe_newline ();
7556 message_buf_print = 1;
7557 }
7558 else
7559 {
7560 if (NILP (echo_area_buffer[0]))
7561 {
7562 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7563 echo_area_buffer[0] = echo_buffer[1];
7564 else
7565 echo_area_buffer[0] = echo_buffer[0];
7566 }
7567
7568 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7569 {
7570 /* Someone switched buffers between print requests. */
7571 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7572 current_buffer->truncate_lines = Qnil;
7573 }
7574 }
7575 }
7576
7577
7578 /* Display an echo area message in window W. Value is non-zero if W's
7579 height is changed. If display_last_displayed_message_p is
7580 non-zero, display the message that was last displayed, otherwise
7581 display the current message. */
7582
7583 static int
7584 display_echo_area (w)
7585 struct window *w;
7586 {
7587 int i, no_message_p, window_height_changed_p, count;
7588
7589 /* Temporarily disable garbage collections while displaying the echo
7590 area. This is done because a GC can print a message itself.
7591 That message would modify the echo area buffer's contents while a
7592 redisplay of the buffer is going on, and seriously confuse
7593 redisplay. */
7594 count = inhibit_garbage_collection ();
7595
7596 /* If there is no message, we must call display_echo_area_1
7597 nevertheless because it resizes the window. But we will have to
7598 reset the echo_area_buffer in question to nil at the end because
7599 with_echo_area_buffer will sets it to an empty buffer. */
7600 i = display_last_displayed_message_p ? 1 : 0;
7601 no_message_p = NILP (echo_area_buffer[i]);
7602
7603 window_height_changed_p
7604 = with_echo_area_buffer (w, display_last_displayed_message_p,
7605 display_echo_area_1,
7606 (EMACS_INT) w, Qnil, 0, 0);
7607
7608 if (no_message_p)
7609 echo_area_buffer[i] = Qnil;
7610
7611 unbind_to (count, Qnil);
7612 return window_height_changed_p;
7613 }
7614
7615
7616 /* Helper for display_echo_area. Display the current buffer which
7617 contains the current echo area message in window W, a mini-window,
7618 a pointer to which is passed in A1. A2..A4 are currently not used.
7619 Change the height of W so that all of the message is displayed.
7620 Value is non-zero if height of W was changed. */
7621
7622 static int
7623 display_echo_area_1 (a1, a2, a3, a4)
7624 EMACS_INT a1;
7625 Lisp_Object a2;
7626 EMACS_INT a3, a4;
7627 {
7628 struct window *w = (struct window *) a1;
7629 Lisp_Object window;
7630 struct text_pos start;
7631 int window_height_changed_p = 0;
7632
7633 /* Do this before displaying, so that we have a large enough glyph
7634 matrix for the display. If we can't get enough space for the
7635 whole text, display the last N lines. That works by setting w->start. */
7636 window_height_changed_p = resize_mini_window (w, 0);
7637
7638 /* Use the starting position chosen by resize_mini_window. */
7639 SET_TEXT_POS_FROM_MARKER (start, w->start);
7640
7641 /* Display. */
7642 clear_glyph_matrix (w->desired_matrix);
7643 XSETWINDOW (window, w);
7644 try_window (window, start, 0);
7645
7646 return window_height_changed_p;
7647 }
7648
7649
7650 /* Resize the echo area window to exactly the size needed for the
7651 currently displayed message, if there is one. If a mini-buffer
7652 is active, don't shrink it. */
7653
7654 void
7655 resize_echo_area_exactly ()
7656 {
7657 if (BUFFERP (echo_area_buffer[0])
7658 && WINDOWP (echo_area_window))
7659 {
7660 struct window *w = XWINDOW (echo_area_window);
7661 int resized_p;
7662 Lisp_Object resize_exactly;
7663
7664 if (minibuf_level == 0)
7665 resize_exactly = Qt;
7666 else
7667 resize_exactly = Qnil;
7668
7669 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7670 (EMACS_INT) w, resize_exactly, 0, 0);
7671 if (resized_p)
7672 {
7673 ++windows_or_buffers_changed;
7674 ++update_mode_lines;
7675 redisplay_internal (0);
7676 }
7677 }
7678 }
7679
7680
7681 /* Callback function for with_echo_area_buffer, when used from
7682 resize_echo_area_exactly. A1 contains a pointer to the window to
7683 resize, EXACTLY non-nil means resize the mini-window exactly to the
7684 size of the text displayed. A3 and A4 are not used. Value is what
7685 resize_mini_window returns. */
7686
7687 static int
7688 resize_mini_window_1 (a1, exactly, a3, a4)
7689 EMACS_INT a1;
7690 Lisp_Object exactly;
7691 EMACS_INT a3, a4;
7692 {
7693 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7694 }
7695
7696
7697 /* Resize mini-window W to fit the size of its contents. EXACT:P
7698 means size the window exactly to the size needed. Otherwise, it's
7699 only enlarged until W's buffer is empty.
7700
7701 Set W->start to the right place to begin display. If the whole
7702 contents fit, start at the beginning. Otherwise, start so as
7703 to make the end of the contents appear. This is particularly
7704 important for y-or-n-p, but seems desirable generally.
7705
7706 Value is non-zero if the window height has been changed. */
7707
7708 int
7709 resize_mini_window (w, exact_p)
7710 struct window *w;
7711 int exact_p;
7712 {
7713 struct frame *f = XFRAME (w->frame);
7714 int window_height_changed_p = 0;
7715
7716 xassert (MINI_WINDOW_P (w));
7717
7718 /* By default, start display at the beginning. */
7719 set_marker_both (w->start, w->buffer,
7720 BUF_BEGV (XBUFFER (w->buffer)),
7721 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
7722
7723 /* Don't resize windows while redisplaying a window; it would
7724 confuse redisplay functions when the size of the window they are
7725 displaying changes from under them. Such a resizing can happen,
7726 for instance, when which-func prints a long message while
7727 we are running fontification-functions. We're running these
7728 functions with safe_call which binds inhibit-redisplay to t. */
7729 if (!NILP (Vinhibit_redisplay))
7730 return 0;
7731
7732 /* Nil means don't try to resize. */
7733 if (NILP (Vresize_mini_windows)
7734 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7735 return 0;
7736
7737 if (!FRAME_MINIBUF_ONLY_P (f))
7738 {
7739 struct it it;
7740 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7741 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7742 int height, max_height;
7743 int unit = FRAME_LINE_HEIGHT (f);
7744 struct text_pos start;
7745 struct buffer *old_current_buffer = NULL;
7746
7747 if (current_buffer != XBUFFER (w->buffer))
7748 {
7749 old_current_buffer = current_buffer;
7750 set_buffer_internal (XBUFFER (w->buffer));
7751 }
7752
7753 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7754
7755 /* Compute the max. number of lines specified by the user. */
7756 if (FLOATP (Vmax_mini_window_height))
7757 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7758 else if (INTEGERP (Vmax_mini_window_height))
7759 max_height = XINT (Vmax_mini_window_height);
7760 else
7761 max_height = total_height / 4;
7762
7763 /* Correct that max. height if it's bogus. */
7764 max_height = max (1, max_height);
7765 max_height = min (total_height, max_height);
7766
7767 /* Find out the height of the text in the window. */
7768 if (it.truncate_lines_p)
7769 height = 1;
7770 else
7771 {
7772 last_height = 0;
7773 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7774 if (it.max_ascent == 0 && it.max_descent == 0)
7775 height = it.current_y + last_height;
7776 else
7777 height = it.current_y + it.max_ascent + it.max_descent;
7778 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7779 height = (height + unit - 1) / unit;
7780 }
7781
7782 /* Compute a suitable window start. */
7783 if (height > max_height)
7784 {
7785 height = max_height;
7786 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
7787 move_it_vertically_backward (&it, (height - 1) * unit);
7788 start = it.current.pos;
7789 }
7790 else
7791 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7792 SET_MARKER_FROM_TEXT_POS (w->start, start);
7793
7794 if (EQ (Vresize_mini_windows, Qgrow_only))
7795 {
7796 /* Let it grow only, until we display an empty message, in which
7797 case the window shrinks again. */
7798 if (height > WINDOW_TOTAL_LINES (w))
7799 {
7800 int old_height = WINDOW_TOTAL_LINES (w);
7801 freeze_window_starts (f, 1);
7802 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7803 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7804 }
7805 else if (height < WINDOW_TOTAL_LINES (w)
7806 && (exact_p || BEGV == ZV))
7807 {
7808 int old_height = WINDOW_TOTAL_LINES (w);
7809 freeze_window_starts (f, 0);
7810 shrink_mini_window (w);
7811 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7812 }
7813 }
7814 else
7815 {
7816 /* Always resize to exact size needed. */
7817 if (height > WINDOW_TOTAL_LINES (w))
7818 {
7819 int old_height = WINDOW_TOTAL_LINES (w);
7820 freeze_window_starts (f, 1);
7821 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7822 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7823 }
7824 else if (height < WINDOW_TOTAL_LINES (w))
7825 {
7826 int old_height = WINDOW_TOTAL_LINES (w);
7827 freeze_window_starts (f, 0);
7828 shrink_mini_window (w);
7829
7830 if (height)
7831 {
7832 freeze_window_starts (f, 1);
7833 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7834 }
7835
7836 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7837 }
7838 }
7839
7840 if (old_current_buffer)
7841 set_buffer_internal (old_current_buffer);
7842 }
7843
7844 return window_height_changed_p;
7845 }
7846
7847
7848 /* Value is the current message, a string, or nil if there is no
7849 current message. */
7850
7851 Lisp_Object
7852 current_message ()
7853 {
7854 Lisp_Object msg;
7855
7856 if (NILP (echo_area_buffer[0]))
7857 msg = Qnil;
7858 else
7859 {
7860 with_echo_area_buffer (0, 0, current_message_1,
7861 (EMACS_INT) &msg, Qnil, 0, 0);
7862 if (NILP (msg))
7863 echo_area_buffer[0] = Qnil;
7864 }
7865
7866 return msg;
7867 }
7868
7869
7870 static int
7871 current_message_1 (a1, a2, a3, a4)
7872 EMACS_INT a1;
7873 Lisp_Object a2;
7874 EMACS_INT a3, a4;
7875 {
7876 Lisp_Object *msg = (Lisp_Object *) a1;
7877
7878 if (Z > BEG)
7879 *msg = make_buffer_string (BEG, Z, 1);
7880 else
7881 *msg = Qnil;
7882 return 0;
7883 }
7884
7885
7886 /* Push the current message on Vmessage_stack for later restauration
7887 by restore_message. Value is non-zero if the current message isn't
7888 empty. This is a relatively infrequent operation, so it's not
7889 worth optimizing. */
7890
7891 int
7892 push_message ()
7893 {
7894 Lisp_Object msg;
7895 msg = current_message ();
7896 Vmessage_stack = Fcons (msg, Vmessage_stack);
7897 return STRINGP (msg);
7898 }
7899
7900
7901 /* Restore message display from the top of Vmessage_stack. */
7902
7903 void
7904 restore_message ()
7905 {
7906 Lisp_Object msg;
7907
7908 xassert (CONSP (Vmessage_stack));
7909 msg = XCAR (Vmessage_stack);
7910 if (STRINGP (msg))
7911 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7912 else
7913 message3_nolog (msg, 0, 0);
7914 }
7915
7916
7917 /* Handler for record_unwind_protect calling pop_message. */
7918
7919 Lisp_Object
7920 pop_message_unwind (dummy)
7921 Lisp_Object dummy;
7922 {
7923 pop_message ();
7924 return Qnil;
7925 }
7926
7927 /* Pop the top-most entry off Vmessage_stack. */
7928
7929 void
7930 pop_message ()
7931 {
7932 xassert (CONSP (Vmessage_stack));
7933 Vmessage_stack = XCDR (Vmessage_stack);
7934 }
7935
7936
7937 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7938 exits. If the stack is not empty, we have a missing pop_message
7939 somewhere. */
7940
7941 void
7942 check_message_stack ()
7943 {
7944 if (!NILP (Vmessage_stack))
7945 abort ();
7946 }
7947
7948
7949 /* Truncate to NCHARS what will be displayed in the echo area the next
7950 time we display it---but don't redisplay it now. */
7951
7952 void
7953 truncate_echo_area (nchars)
7954 int nchars;
7955 {
7956 if (nchars == 0)
7957 echo_area_buffer[0] = Qnil;
7958 /* A null message buffer means that the frame hasn't really been
7959 initialized yet. Error messages get reported properly by
7960 cmd_error, so this must be just an informative message; toss it. */
7961 else if (!noninteractive
7962 && INTERACTIVE
7963 && !NILP (echo_area_buffer[0]))
7964 {
7965 struct frame *sf = SELECTED_FRAME ();
7966 if (FRAME_MESSAGE_BUF (sf))
7967 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7968 }
7969 }
7970
7971
7972 /* Helper function for truncate_echo_area. Truncate the current
7973 message to at most NCHARS characters. */
7974
7975 static int
7976 truncate_message_1 (nchars, a2, a3, a4)
7977 EMACS_INT nchars;
7978 Lisp_Object a2;
7979 EMACS_INT a3, a4;
7980 {
7981 if (BEG + nchars < Z)
7982 del_range (BEG + nchars, Z);
7983 if (Z == BEG)
7984 echo_area_buffer[0] = Qnil;
7985 return 0;
7986 }
7987
7988
7989 /* Set the current message to a substring of S or STRING.
7990
7991 If STRING is a Lisp string, set the message to the first NBYTES
7992 bytes from STRING. NBYTES zero means use the whole string. If
7993 STRING is multibyte, the message will be displayed multibyte.
7994
7995 If S is not null, set the message to the first LEN bytes of S. LEN
7996 zero means use the whole string. MULTIBYTE_P non-zero means S is
7997 multibyte. Display the message multibyte in that case. */
7998
7999 void
8000 set_message (s, string, nbytes, multibyte_p)
8001 const char *s;
8002 Lisp_Object string;
8003 int nbytes, multibyte_p;
8004 {
8005 message_enable_multibyte
8006 = ((s && multibyte_p)
8007 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8008
8009 with_echo_area_buffer (0, 0, set_message_1,
8010 (EMACS_INT) s, string, nbytes, multibyte_p);
8011 message_buf_print = 0;
8012 help_echo_showing_p = 0;
8013 }
8014
8015
8016 /* Helper function for set_message. Arguments have the same meaning
8017 as there, with A1 corresponding to S and A2 corresponding to STRING
8018 This function is called with the echo area buffer being
8019 current. */
8020
8021 static int
8022 set_message_1 (a1, a2, nbytes, multibyte_p)
8023 EMACS_INT a1;
8024 Lisp_Object a2;
8025 EMACS_INT nbytes, multibyte_p;
8026 {
8027 const char *s = (const char *) a1;
8028 Lisp_Object string = a2;
8029
8030 /* Change multibyteness of the echo buffer appropriately. */
8031 if (message_enable_multibyte
8032 != !NILP (current_buffer->enable_multibyte_characters))
8033 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8034
8035 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8036
8037 /* Insert new message at BEG. */
8038 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8039 Ferase_buffer ();
8040
8041 if (STRINGP (string))
8042 {
8043 int nchars;
8044
8045 if (nbytes == 0)
8046 nbytes = SBYTES (string);
8047 nchars = string_byte_to_char (string, nbytes);
8048
8049 /* This function takes care of single/multibyte conversion. We
8050 just have to ensure that the echo area buffer has the right
8051 setting of enable_multibyte_characters. */
8052 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8053 }
8054 else if (s)
8055 {
8056 if (nbytes == 0)
8057 nbytes = strlen (s);
8058
8059 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8060 {
8061 /* Convert from multi-byte to single-byte. */
8062 int i, c, n;
8063 unsigned char work[1];
8064
8065 /* Convert a multibyte string to single-byte. */
8066 for (i = 0; i < nbytes; i += n)
8067 {
8068 c = string_char_and_length (s + i, nbytes - i, &n);
8069 work[0] = (SINGLE_BYTE_CHAR_P (c)
8070 ? c
8071 : multibyte_char_to_unibyte (c, Qnil));
8072 insert_1_both (work, 1, 1, 1, 0, 0);
8073 }
8074 }
8075 else if (!multibyte_p
8076 && !NILP (current_buffer->enable_multibyte_characters))
8077 {
8078 /* Convert from single-byte to multi-byte. */
8079 int i, c, n;
8080 const unsigned char *msg = (const unsigned char *) s;
8081 unsigned char str[MAX_MULTIBYTE_LENGTH];
8082
8083 /* Convert a single-byte string to multibyte. */
8084 for (i = 0; i < nbytes; i++)
8085 {
8086 c = unibyte_char_to_multibyte (msg[i]);
8087 n = CHAR_STRING (c, str);
8088 insert_1_both (str, 1, n, 1, 0, 0);
8089 }
8090 }
8091 else
8092 insert_1 (s, nbytes, 1, 0, 0);
8093 }
8094
8095 return 0;
8096 }
8097
8098
8099 /* Clear messages. CURRENT_P non-zero means clear the current
8100 message. LAST_DISPLAYED_P non-zero means clear the message
8101 last displayed. */
8102
8103 void
8104 clear_message (current_p, last_displayed_p)
8105 int current_p, last_displayed_p;
8106 {
8107 if (current_p)
8108 {
8109 echo_area_buffer[0] = Qnil;
8110 message_cleared_p = 1;
8111 }
8112
8113 if (last_displayed_p)
8114 echo_area_buffer[1] = Qnil;
8115
8116 message_buf_print = 0;
8117 }
8118
8119 /* Clear garbaged frames.
8120
8121 This function is used where the old redisplay called
8122 redraw_garbaged_frames which in turn called redraw_frame which in
8123 turn called clear_frame. The call to clear_frame was a source of
8124 flickering. I believe a clear_frame is not necessary. It should
8125 suffice in the new redisplay to invalidate all current matrices,
8126 and ensure a complete redisplay of all windows. */
8127
8128 static void
8129 clear_garbaged_frames ()
8130 {
8131 if (frame_garbaged)
8132 {
8133 Lisp_Object tail, frame;
8134 int changed_count = 0;
8135
8136 FOR_EACH_FRAME (tail, frame)
8137 {
8138 struct frame *f = XFRAME (frame);
8139
8140 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8141 {
8142 if (f->resized_p)
8143 {
8144 Fredraw_frame (frame);
8145 f->force_flush_display_p = 1;
8146 }
8147 clear_current_matrices (f);
8148 changed_count++;
8149 f->garbaged = 0;
8150 f->resized_p = 0;
8151 }
8152 }
8153
8154 frame_garbaged = 0;
8155 if (changed_count)
8156 ++windows_or_buffers_changed;
8157 }
8158 }
8159
8160
8161 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8162 is non-zero update selected_frame. Value is non-zero if the
8163 mini-windows height has been changed. */
8164
8165 static int
8166 echo_area_display (update_frame_p)
8167 int update_frame_p;
8168 {
8169 Lisp_Object mini_window;
8170 struct window *w;
8171 struct frame *f;
8172 int window_height_changed_p = 0;
8173 struct frame *sf = SELECTED_FRAME ();
8174
8175 mini_window = FRAME_MINIBUF_WINDOW (sf);
8176 w = XWINDOW (mini_window);
8177 f = XFRAME (WINDOW_FRAME (w));
8178
8179 /* Don't display if frame is invisible or not yet initialized. */
8180 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8181 return 0;
8182
8183 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8184 #ifndef MAC_OS8
8185 #ifdef HAVE_WINDOW_SYSTEM
8186 /* When Emacs starts, selected_frame may be a visible terminal
8187 frame, even if we run under a window system. If we let this
8188 through, a message would be displayed on the terminal. */
8189 if (EQ (selected_frame, Vterminal_frame)
8190 && !NILP (Vwindow_system))
8191 return 0;
8192 #endif /* HAVE_WINDOW_SYSTEM */
8193 #endif
8194
8195 /* Redraw garbaged frames. */
8196 if (frame_garbaged)
8197 clear_garbaged_frames ();
8198
8199 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8200 {
8201 echo_area_window = mini_window;
8202 window_height_changed_p = display_echo_area (w);
8203 w->must_be_updated_p = 1;
8204
8205 /* Update the display, unless called from redisplay_internal.
8206 Also don't update the screen during redisplay itself. The
8207 update will happen at the end of redisplay, and an update
8208 here could cause confusion. */
8209 if (update_frame_p && !redisplaying_p)
8210 {
8211 int n = 0;
8212
8213 /* If the display update has been interrupted by pending
8214 input, update mode lines in the frame. Due to the
8215 pending input, it might have been that redisplay hasn't
8216 been called, so that mode lines above the echo area are
8217 garbaged. This looks odd, so we prevent it here. */
8218 if (!display_completed)
8219 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8220
8221 if (window_height_changed_p
8222 /* Don't do this if Emacs is shutting down. Redisplay
8223 needs to run hooks. */
8224 && !NILP (Vrun_hooks))
8225 {
8226 /* Must update other windows. Likewise as in other
8227 cases, don't let this update be interrupted by
8228 pending input. */
8229 int count = SPECPDL_INDEX ();
8230 specbind (Qredisplay_dont_pause, Qt);
8231 windows_or_buffers_changed = 1;
8232 redisplay_internal (0);
8233 unbind_to (count, Qnil);
8234 }
8235 else if (FRAME_WINDOW_P (f) && n == 0)
8236 {
8237 /* Window configuration is the same as before.
8238 Can do with a display update of the echo area,
8239 unless we displayed some mode lines. */
8240 update_single_window (w, 1);
8241 rif->flush_display (f);
8242 }
8243 else
8244 update_frame (f, 1, 1);
8245
8246 /* If cursor is in the echo area, make sure that the next
8247 redisplay displays the minibuffer, so that the cursor will
8248 be replaced with what the minibuffer wants. */
8249 if (cursor_in_echo_area)
8250 ++windows_or_buffers_changed;
8251 }
8252 }
8253 else if (!EQ (mini_window, selected_window))
8254 windows_or_buffers_changed++;
8255
8256 /* The current message is now also the last one displayed. */
8257 echo_area_buffer[1] = echo_area_buffer[0];
8258
8259 /* Prevent redisplay optimization in redisplay_internal by resetting
8260 this_line_start_pos. This is done because the mini-buffer now
8261 displays the message instead of its buffer text. */
8262 if (EQ (mini_window, selected_window))
8263 CHARPOS (this_line_start_pos) = 0;
8264
8265 return window_height_changed_p;
8266 }
8267
8268
8269 \f
8270 /***********************************************************************
8271 Mode Lines and Frame Titles
8272 ***********************************************************************/
8273
8274 /* A buffer for constructing non-propertized mode-line strings and
8275 frame titles in it; allocated from the heap in init_xdisp and
8276 resized as needed in store_mode_line_noprop_char. */
8277
8278 static char *mode_line_noprop_buf;
8279
8280 /* The buffer's end, and a current output position in it. */
8281
8282 static char *mode_line_noprop_buf_end;
8283 static char *mode_line_noprop_ptr;
8284
8285 #define MODE_LINE_NOPROP_LEN(start) \
8286 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8287
8288 static enum {
8289 MODE_LINE_DISPLAY = 0,
8290 MODE_LINE_TITLE,
8291 MODE_LINE_NOPROP,
8292 MODE_LINE_STRING
8293 } mode_line_target;
8294
8295 /* Alist that caches the results of :propertize.
8296 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8297 static Lisp_Object mode_line_proptrans_alist;
8298
8299 /* List of strings making up the mode-line. */
8300 static Lisp_Object mode_line_string_list;
8301
8302 /* Base face property when building propertized mode line string. */
8303 static Lisp_Object mode_line_string_face;
8304 static Lisp_Object mode_line_string_face_prop;
8305
8306
8307 /* Unwind data for mode line strings */
8308
8309 static Lisp_Object Vmode_line_unwind_vector;
8310
8311 static Lisp_Object
8312 format_mode_line_unwind_data (obuf)
8313 struct buffer *obuf;
8314 {
8315 Lisp_Object vector;
8316
8317 /* Reduce consing by keeping one vector in
8318 Vwith_echo_area_save_vector. */
8319 vector = Vmode_line_unwind_vector;
8320 Vmode_line_unwind_vector = Qnil;
8321
8322 if (NILP (vector))
8323 vector = Fmake_vector (make_number (7), Qnil);
8324
8325 AREF (vector, 0) = make_number (mode_line_target);
8326 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8327 AREF (vector, 2) = mode_line_string_list;
8328 AREF (vector, 3) = mode_line_proptrans_alist;
8329 AREF (vector, 4) = mode_line_string_face;
8330 AREF (vector, 5) = mode_line_string_face_prop;
8331
8332 if (obuf)
8333 XSETBUFFER (AREF (vector, 6), obuf);
8334 else
8335 AREF (vector, 6) = Qnil;
8336
8337 return vector;
8338 }
8339
8340 static Lisp_Object
8341 unwind_format_mode_line (vector)
8342 Lisp_Object vector;
8343 {
8344 mode_line_target = XINT (AREF (vector, 0));
8345 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8346 mode_line_string_list = AREF (vector, 2);
8347 mode_line_proptrans_alist = AREF (vector, 3);
8348 mode_line_string_face = AREF (vector, 4);
8349 mode_line_string_face_prop = AREF (vector, 5);
8350
8351 if (!NILP (AREF (vector, 6)))
8352 {
8353 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8354 AREF (vector, 6) = Qnil;
8355 }
8356
8357 Vmode_line_unwind_vector = vector;
8358 return Qnil;
8359 }
8360
8361
8362 /* Store a single character C for the frame title in mode_line_noprop_buf.
8363 Re-allocate mode_line_noprop_buf if necessary. */
8364
8365 static void
8366 #ifdef PROTOTYPES
8367 store_mode_line_noprop_char (char c)
8368 #else
8369 store_mode_line_noprop_char (c)
8370 char c;
8371 #endif
8372 {
8373 /* If output position has reached the end of the allocated buffer,
8374 double the buffer's size. */
8375 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8376 {
8377 int len = MODE_LINE_NOPROP_LEN (0);
8378 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8379 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8380 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8381 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8382 }
8383
8384 *mode_line_noprop_ptr++ = c;
8385 }
8386
8387
8388 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8389 mode_line_noprop_ptr. STR is the string to store. Do not copy
8390 characters that yield more columns than PRECISION; PRECISION <= 0
8391 means copy the whole string. Pad with spaces until FIELD_WIDTH
8392 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8393 pad. Called from display_mode_element when it is used to build a
8394 frame title. */
8395
8396 static int
8397 store_mode_line_noprop (str, field_width, precision)
8398 const unsigned char *str;
8399 int field_width, precision;
8400 {
8401 int n = 0;
8402 int dummy, nbytes;
8403
8404 /* Copy at most PRECISION chars from STR. */
8405 nbytes = strlen (str);
8406 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8407 while (nbytes--)
8408 store_mode_line_noprop_char (*str++);
8409
8410 /* Fill up with spaces until FIELD_WIDTH reached. */
8411 while (field_width > 0
8412 && n < field_width)
8413 {
8414 store_mode_line_noprop_char (' ');
8415 ++n;
8416 }
8417
8418 return n;
8419 }
8420
8421 /***********************************************************************
8422 Frame Titles
8423 ***********************************************************************/
8424
8425 #ifdef HAVE_WINDOW_SYSTEM
8426
8427 /* Set the title of FRAME, if it has changed. The title format is
8428 Vicon_title_format if FRAME is iconified, otherwise it is
8429 frame_title_format. */
8430
8431 static void
8432 x_consider_frame_title (frame)
8433 Lisp_Object frame;
8434 {
8435 struct frame *f = XFRAME (frame);
8436
8437 if (FRAME_WINDOW_P (f)
8438 || FRAME_MINIBUF_ONLY_P (f)
8439 || f->explicit_name)
8440 {
8441 /* Do we have more than one visible frame on this X display? */
8442 Lisp_Object tail;
8443 Lisp_Object fmt;
8444 int title_start;
8445 char *title;
8446 int len;
8447 struct it it;
8448 int count = SPECPDL_INDEX ();
8449
8450 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8451 {
8452 Lisp_Object other_frame = XCAR (tail);
8453 struct frame *tf = XFRAME (other_frame);
8454
8455 if (tf != f
8456 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8457 && !FRAME_MINIBUF_ONLY_P (tf)
8458 && !EQ (other_frame, tip_frame)
8459 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8460 break;
8461 }
8462
8463 /* Set global variable indicating that multiple frames exist. */
8464 multiple_frames = CONSP (tail);
8465
8466 /* Switch to the buffer of selected window of the frame. Set up
8467 mode_line_target so that display_mode_element will output into
8468 mode_line_noprop_buf; then display the title. */
8469 record_unwind_protect (unwind_format_mode_line,
8470 format_mode_line_unwind_data (current_buffer));
8471
8472 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8473 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8474
8475 mode_line_target = MODE_LINE_TITLE;
8476 title_start = MODE_LINE_NOPROP_LEN (0);
8477 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8478 NULL, DEFAULT_FACE_ID);
8479 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8480 len = MODE_LINE_NOPROP_LEN (title_start);
8481 title = mode_line_noprop_buf + title_start;
8482 unbind_to (count, Qnil);
8483
8484 /* Set the title only if it's changed. This avoids consing in
8485 the common case where it hasn't. (If it turns out that we've
8486 already wasted too much time by walking through the list with
8487 display_mode_element, then we might need to optimize at a
8488 higher level than this.) */
8489 if (! STRINGP (f->name)
8490 || SBYTES (f->name) != len
8491 || bcmp (title, SDATA (f->name), len) != 0)
8492 x_implicitly_set_name (f, make_string (title, len), Qnil);
8493 }
8494 }
8495
8496 #endif /* not HAVE_WINDOW_SYSTEM */
8497
8498
8499
8500 \f
8501 /***********************************************************************
8502 Menu Bars
8503 ***********************************************************************/
8504
8505
8506 /* Prepare for redisplay by updating menu-bar item lists when
8507 appropriate. This can call eval. */
8508
8509 void
8510 prepare_menu_bars ()
8511 {
8512 int all_windows;
8513 struct gcpro gcpro1, gcpro2;
8514 struct frame *f;
8515 Lisp_Object tooltip_frame;
8516
8517 #ifdef HAVE_WINDOW_SYSTEM
8518 tooltip_frame = tip_frame;
8519 #else
8520 tooltip_frame = Qnil;
8521 #endif
8522
8523 /* Update all frame titles based on their buffer names, etc. We do
8524 this before the menu bars so that the buffer-menu will show the
8525 up-to-date frame titles. */
8526 #ifdef HAVE_WINDOW_SYSTEM
8527 if (windows_or_buffers_changed || update_mode_lines)
8528 {
8529 Lisp_Object tail, frame;
8530
8531 FOR_EACH_FRAME (tail, frame)
8532 {
8533 f = XFRAME (frame);
8534 if (!EQ (frame, tooltip_frame)
8535 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8536 x_consider_frame_title (frame);
8537 }
8538 }
8539 #endif /* HAVE_WINDOW_SYSTEM */
8540
8541 /* Update the menu bar item lists, if appropriate. This has to be
8542 done before any actual redisplay or generation of display lines. */
8543 all_windows = (update_mode_lines
8544 || buffer_shared > 1
8545 || windows_or_buffers_changed);
8546 if (all_windows)
8547 {
8548 Lisp_Object tail, frame;
8549 int count = SPECPDL_INDEX ();
8550
8551 record_unwind_save_match_data ();
8552
8553 FOR_EACH_FRAME (tail, frame)
8554 {
8555 f = XFRAME (frame);
8556
8557 /* Ignore tooltip frame. */
8558 if (EQ (frame, tooltip_frame))
8559 continue;
8560
8561 /* If a window on this frame changed size, report that to
8562 the user and clear the size-change flag. */
8563 if (FRAME_WINDOW_SIZES_CHANGED (f))
8564 {
8565 Lisp_Object functions;
8566
8567 /* Clear flag first in case we get an error below. */
8568 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8569 functions = Vwindow_size_change_functions;
8570 GCPRO2 (tail, functions);
8571
8572 while (CONSP (functions))
8573 {
8574 call1 (XCAR (functions), frame);
8575 functions = XCDR (functions);
8576 }
8577 UNGCPRO;
8578 }
8579
8580 GCPRO1 (tail);
8581 update_menu_bar (f, 0);
8582 #ifdef HAVE_WINDOW_SYSTEM
8583 update_tool_bar (f, 0);
8584 #endif
8585 UNGCPRO;
8586 }
8587
8588 unbind_to (count, Qnil);
8589 }
8590 else
8591 {
8592 struct frame *sf = SELECTED_FRAME ();
8593 update_menu_bar (sf, 1);
8594 #ifdef HAVE_WINDOW_SYSTEM
8595 update_tool_bar (sf, 1);
8596 #endif
8597 }
8598
8599 /* Motif needs this. See comment in xmenu.c. Turn it off when
8600 pending_menu_activation is not defined. */
8601 #ifdef USE_X_TOOLKIT
8602 pending_menu_activation = 0;
8603 #endif
8604 }
8605
8606
8607 /* Update the menu bar item list for frame F. This has to be done
8608 before we start to fill in any display lines, because it can call
8609 eval.
8610
8611 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8612
8613 static void
8614 update_menu_bar (f, save_match_data)
8615 struct frame *f;
8616 int save_match_data;
8617 {
8618 Lisp_Object window;
8619 register struct window *w;
8620
8621 /* If called recursively during a menu update, do nothing. This can
8622 happen when, for instance, an activate-menubar-hook causes a
8623 redisplay. */
8624 if (inhibit_menubar_update)
8625 return;
8626
8627 window = FRAME_SELECTED_WINDOW (f);
8628 w = XWINDOW (window);
8629
8630 #if 0 /* The if statement below this if statement used to include the
8631 condition !NILP (w->update_mode_line), rather than using
8632 update_mode_lines directly, and this if statement may have
8633 been added to make that condition work. Now the if
8634 statement below matches its comment, this isn't needed. */
8635 if (update_mode_lines)
8636 w->update_mode_line = Qt;
8637 #endif
8638
8639 if (FRAME_WINDOW_P (f)
8640 ?
8641 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8642 || defined (USE_GTK)
8643 FRAME_EXTERNAL_MENU_BAR (f)
8644 #else
8645 FRAME_MENU_BAR_LINES (f) > 0
8646 #endif
8647 : FRAME_MENU_BAR_LINES (f) > 0)
8648 {
8649 /* If the user has switched buffers or windows, we need to
8650 recompute to reflect the new bindings. But we'll
8651 recompute when update_mode_lines is set too; that means
8652 that people can use force-mode-line-update to request
8653 that the menu bar be recomputed. The adverse effect on
8654 the rest of the redisplay algorithm is about the same as
8655 windows_or_buffers_changed anyway. */
8656 if (windows_or_buffers_changed
8657 /* This used to test w->update_mode_line, but we believe
8658 there is no need to recompute the menu in that case. */
8659 || update_mode_lines
8660 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8661 < BUF_MODIFF (XBUFFER (w->buffer)))
8662 != !NILP (w->last_had_star))
8663 || ((!NILP (Vtransient_mark_mode)
8664 && !NILP (XBUFFER (w->buffer)->mark_active))
8665 != !NILP (w->region_showing)))
8666 {
8667 struct buffer *prev = current_buffer;
8668 int count = SPECPDL_INDEX ();
8669
8670 specbind (Qinhibit_menubar_update, Qt);
8671
8672 set_buffer_internal_1 (XBUFFER (w->buffer));
8673 if (save_match_data)
8674 record_unwind_save_match_data ();
8675 if (NILP (Voverriding_local_map_menu_flag))
8676 {
8677 specbind (Qoverriding_terminal_local_map, Qnil);
8678 specbind (Qoverriding_local_map, Qnil);
8679 }
8680
8681 /* Run the Lucid hook. */
8682 safe_run_hooks (Qactivate_menubar_hook);
8683
8684 /* If it has changed current-menubar from previous value,
8685 really recompute the menu-bar from the value. */
8686 if (! NILP (Vlucid_menu_bar_dirty_flag))
8687 call0 (Qrecompute_lucid_menubar);
8688
8689 safe_run_hooks (Qmenu_bar_update_hook);
8690 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8691
8692 /* Redisplay the menu bar in case we changed it. */
8693 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8694 || defined (USE_GTK)
8695 if (FRAME_WINDOW_P (f)
8696 #if defined (MAC_OS)
8697 /* All frames on Mac OS share the same menubar. So only the
8698 selected frame should be allowed to set it. */
8699 && f == SELECTED_FRAME ()
8700 #endif
8701 )
8702 set_frame_menubar (f, 0, 0);
8703 else
8704 /* On a terminal screen, the menu bar is an ordinary screen
8705 line, and this makes it get updated. */
8706 w->update_mode_line = Qt;
8707 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8708 /* In the non-toolkit version, the menu bar is an ordinary screen
8709 line, and this makes it get updated. */
8710 w->update_mode_line = Qt;
8711 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8712
8713 unbind_to (count, Qnil);
8714 set_buffer_internal_1 (prev);
8715 }
8716 }
8717 }
8718
8719
8720 \f
8721 /***********************************************************************
8722 Output Cursor
8723 ***********************************************************************/
8724
8725 #ifdef HAVE_WINDOW_SYSTEM
8726
8727 /* EXPORT:
8728 Nominal cursor position -- where to draw output.
8729 HPOS and VPOS are window relative glyph matrix coordinates.
8730 X and Y are window relative pixel coordinates. */
8731
8732 struct cursor_pos output_cursor;
8733
8734
8735 /* EXPORT:
8736 Set the global variable output_cursor to CURSOR. All cursor
8737 positions are relative to updated_window. */
8738
8739 void
8740 set_output_cursor (cursor)
8741 struct cursor_pos *cursor;
8742 {
8743 output_cursor.hpos = cursor->hpos;
8744 output_cursor.vpos = cursor->vpos;
8745 output_cursor.x = cursor->x;
8746 output_cursor.y = cursor->y;
8747 }
8748
8749
8750 /* EXPORT for RIF:
8751 Set a nominal cursor position.
8752
8753 HPOS and VPOS are column/row positions in a window glyph matrix. X
8754 and Y are window text area relative pixel positions.
8755
8756 If this is done during an update, updated_window will contain the
8757 window that is being updated and the position is the future output
8758 cursor position for that window. If updated_window is null, use
8759 selected_window and display the cursor at the given position. */
8760
8761 void
8762 x_cursor_to (vpos, hpos, y, x)
8763 int vpos, hpos, y, x;
8764 {
8765 struct window *w;
8766
8767 /* If updated_window is not set, work on selected_window. */
8768 if (updated_window)
8769 w = updated_window;
8770 else
8771 w = XWINDOW (selected_window);
8772
8773 /* Set the output cursor. */
8774 output_cursor.hpos = hpos;
8775 output_cursor.vpos = vpos;
8776 output_cursor.x = x;
8777 output_cursor.y = y;
8778
8779 /* If not called as part of an update, really display the cursor.
8780 This will also set the cursor position of W. */
8781 if (updated_window == NULL)
8782 {
8783 BLOCK_INPUT;
8784 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8785 if (rif->flush_display_optional)
8786 rif->flush_display_optional (SELECTED_FRAME ());
8787 UNBLOCK_INPUT;
8788 }
8789 }
8790
8791 #endif /* HAVE_WINDOW_SYSTEM */
8792
8793 \f
8794 /***********************************************************************
8795 Tool-bars
8796 ***********************************************************************/
8797
8798 #ifdef HAVE_WINDOW_SYSTEM
8799
8800 /* Where the mouse was last time we reported a mouse event. */
8801
8802 FRAME_PTR last_mouse_frame;
8803
8804 /* Tool-bar item index of the item on which a mouse button was pressed
8805 or -1. */
8806
8807 int last_tool_bar_item;
8808
8809
8810 /* Update the tool-bar item list for frame F. This has to be done
8811 before we start to fill in any display lines. Called from
8812 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8813 and restore it here. */
8814
8815 static void
8816 update_tool_bar (f, save_match_data)
8817 struct frame *f;
8818 int save_match_data;
8819 {
8820 #ifdef USE_GTK
8821 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8822 #else
8823 int do_update = WINDOWP (f->tool_bar_window)
8824 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8825 #endif
8826
8827 if (do_update)
8828 {
8829 Lisp_Object window;
8830 struct window *w;
8831
8832 window = FRAME_SELECTED_WINDOW (f);
8833 w = XWINDOW (window);
8834
8835 /* If the user has switched buffers or windows, we need to
8836 recompute to reflect the new bindings. But we'll
8837 recompute when update_mode_lines is set too; that means
8838 that people can use force-mode-line-update to request
8839 that the menu bar be recomputed. The adverse effect on
8840 the rest of the redisplay algorithm is about the same as
8841 windows_or_buffers_changed anyway. */
8842 if (windows_or_buffers_changed
8843 || !NILP (w->update_mode_line)
8844 || update_mode_lines
8845 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8846 < BUF_MODIFF (XBUFFER (w->buffer)))
8847 != !NILP (w->last_had_star))
8848 || ((!NILP (Vtransient_mark_mode)
8849 && !NILP (XBUFFER (w->buffer)->mark_active))
8850 != !NILP (w->region_showing)))
8851 {
8852 struct buffer *prev = current_buffer;
8853 int count = SPECPDL_INDEX ();
8854 Lisp_Object new_tool_bar;
8855 int new_n_tool_bar;
8856 struct gcpro gcpro1;
8857
8858 /* Set current_buffer to the buffer of the selected
8859 window of the frame, so that we get the right local
8860 keymaps. */
8861 set_buffer_internal_1 (XBUFFER (w->buffer));
8862
8863 /* Save match data, if we must. */
8864 if (save_match_data)
8865 record_unwind_save_match_data ();
8866
8867 /* Make sure that we don't accidentally use bogus keymaps. */
8868 if (NILP (Voverriding_local_map_menu_flag))
8869 {
8870 specbind (Qoverriding_terminal_local_map, Qnil);
8871 specbind (Qoverriding_local_map, Qnil);
8872 }
8873
8874 GCPRO1 (new_tool_bar);
8875
8876 /* Build desired tool-bar items from keymaps. */
8877 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8878 &new_n_tool_bar);
8879
8880 /* Redisplay the tool-bar if we changed it. */
8881 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8882 {
8883 /* Redisplay that happens asynchronously due to an expose event
8884 may access f->tool_bar_items. Make sure we update both
8885 variables within BLOCK_INPUT so no such event interrupts. */
8886 BLOCK_INPUT;
8887 f->tool_bar_items = new_tool_bar;
8888 f->n_tool_bar_items = new_n_tool_bar;
8889 w->update_mode_line = Qt;
8890 UNBLOCK_INPUT;
8891 }
8892
8893 UNGCPRO;
8894
8895 unbind_to (count, Qnil);
8896 set_buffer_internal_1 (prev);
8897 }
8898 }
8899 }
8900
8901
8902 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8903 F's desired tool-bar contents. F->tool_bar_items must have
8904 been set up previously by calling prepare_menu_bars. */
8905
8906 static void
8907 build_desired_tool_bar_string (f)
8908 struct frame *f;
8909 {
8910 int i, size, size_needed;
8911 struct gcpro gcpro1, gcpro2, gcpro3;
8912 Lisp_Object image, plist, props;
8913
8914 image = plist = props = Qnil;
8915 GCPRO3 (image, plist, props);
8916
8917 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8918 Otherwise, make a new string. */
8919
8920 /* The size of the string we might be able to reuse. */
8921 size = (STRINGP (f->desired_tool_bar_string)
8922 ? SCHARS (f->desired_tool_bar_string)
8923 : 0);
8924
8925 /* We need one space in the string for each image. */
8926 size_needed = f->n_tool_bar_items;
8927
8928 /* Reuse f->desired_tool_bar_string, if possible. */
8929 if (size < size_needed || NILP (f->desired_tool_bar_string))
8930 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8931 make_number (' '));
8932 else
8933 {
8934 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8935 Fremove_text_properties (make_number (0), make_number (size),
8936 props, f->desired_tool_bar_string);
8937 }
8938
8939 /* Put a `display' property on the string for the images to display,
8940 put a `menu_item' property on tool-bar items with a value that
8941 is the index of the item in F's tool-bar item vector. */
8942 for (i = 0; i < f->n_tool_bar_items; ++i)
8943 {
8944 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8945
8946 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8947 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8948 int hmargin, vmargin, relief, idx, end;
8949 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8950
8951 /* If image is a vector, choose the image according to the
8952 button state. */
8953 image = PROP (TOOL_BAR_ITEM_IMAGES);
8954 if (VECTORP (image))
8955 {
8956 if (enabled_p)
8957 idx = (selected_p
8958 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8959 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8960 else
8961 idx = (selected_p
8962 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8963 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8964
8965 xassert (ASIZE (image) >= idx);
8966 image = AREF (image, idx);
8967 }
8968 else
8969 idx = -1;
8970
8971 /* Ignore invalid image specifications. */
8972 if (!valid_image_p (image))
8973 continue;
8974
8975 /* Display the tool-bar button pressed, or depressed. */
8976 plist = Fcopy_sequence (XCDR (image));
8977
8978 /* Compute margin and relief to draw. */
8979 relief = (tool_bar_button_relief >= 0
8980 ? tool_bar_button_relief
8981 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8982 hmargin = vmargin = relief;
8983
8984 if (INTEGERP (Vtool_bar_button_margin)
8985 && XINT (Vtool_bar_button_margin) > 0)
8986 {
8987 hmargin += XFASTINT (Vtool_bar_button_margin);
8988 vmargin += XFASTINT (Vtool_bar_button_margin);
8989 }
8990 else if (CONSP (Vtool_bar_button_margin))
8991 {
8992 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8993 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8994 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8995
8996 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8997 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8998 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8999 }
9000
9001 if (auto_raise_tool_bar_buttons_p)
9002 {
9003 /* Add a `:relief' property to the image spec if the item is
9004 selected. */
9005 if (selected_p)
9006 {
9007 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9008 hmargin -= relief;
9009 vmargin -= relief;
9010 }
9011 }
9012 else
9013 {
9014 /* If image is selected, display it pressed, i.e. with a
9015 negative relief. If it's not selected, display it with a
9016 raised relief. */
9017 plist = Fplist_put (plist, QCrelief,
9018 (selected_p
9019 ? make_number (-relief)
9020 : make_number (relief)));
9021 hmargin -= relief;
9022 vmargin -= relief;
9023 }
9024
9025 /* Put a margin around the image. */
9026 if (hmargin || vmargin)
9027 {
9028 if (hmargin == vmargin)
9029 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9030 else
9031 plist = Fplist_put (plist, QCmargin,
9032 Fcons (make_number (hmargin),
9033 make_number (vmargin)));
9034 }
9035
9036 /* If button is not enabled, and we don't have special images
9037 for the disabled state, make the image appear disabled by
9038 applying an appropriate algorithm to it. */
9039 if (!enabled_p && idx < 0)
9040 plist = Fplist_put (plist, QCconversion, Qdisabled);
9041
9042 /* Put a `display' text property on the string for the image to
9043 display. Put a `menu-item' property on the string that gives
9044 the start of this item's properties in the tool-bar items
9045 vector. */
9046 image = Fcons (Qimage, plist);
9047 props = list4 (Qdisplay, image,
9048 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9049
9050 /* Let the last image hide all remaining spaces in the tool bar
9051 string. The string can be longer than needed when we reuse a
9052 previous string. */
9053 if (i + 1 == f->n_tool_bar_items)
9054 end = SCHARS (f->desired_tool_bar_string);
9055 else
9056 end = i + 1;
9057 Fadd_text_properties (make_number (i), make_number (end),
9058 props, f->desired_tool_bar_string);
9059 #undef PROP
9060 }
9061
9062 UNGCPRO;
9063 }
9064
9065
9066 /* Display one line of the tool-bar of frame IT->f. */
9067
9068 static void
9069 display_tool_bar_line (it)
9070 struct it *it;
9071 {
9072 struct glyph_row *row = it->glyph_row;
9073 int max_x = it->last_visible_x;
9074 struct glyph *last;
9075
9076 prepare_desired_row (row);
9077 row->y = it->current_y;
9078
9079 /* Note that this isn't made use of if the face hasn't a box,
9080 so there's no need to check the face here. */
9081 it->start_of_box_run_p = 1;
9082
9083 while (it->current_x < max_x)
9084 {
9085 int x_before, x, n_glyphs_before, i, nglyphs;
9086
9087 /* Get the next display element. */
9088 if (!get_next_display_element (it))
9089 break;
9090
9091 /* Produce glyphs. */
9092 x_before = it->current_x;
9093 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9094 PRODUCE_GLYPHS (it);
9095
9096 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9097 i = 0;
9098 x = x_before;
9099 while (i < nglyphs)
9100 {
9101 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9102
9103 if (x + glyph->pixel_width > max_x)
9104 {
9105 /* Glyph doesn't fit on line. */
9106 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9107 it->current_x = x;
9108 goto out;
9109 }
9110
9111 ++it->hpos;
9112 x += glyph->pixel_width;
9113 ++i;
9114 }
9115
9116 /* Stop at line ends. */
9117 if (ITERATOR_AT_END_OF_LINE_P (it))
9118 break;
9119
9120 set_iterator_to_next (it, 1);
9121 }
9122
9123 out:;
9124
9125 row->displays_text_p = row->used[TEXT_AREA] != 0;
9126 extend_face_to_end_of_line (it);
9127 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9128 last->right_box_line_p = 1;
9129 if (last == row->glyphs[TEXT_AREA])
9130 last->left_box_line_p = 1;
9131 compute_line_metrics (it);
9132
9133 /* If line is empty, make it occupy the rest of the tool-bar. */
9134 if (!row->displays_text_p)
9135 {
9136 row->height = row->phys_height = it->last_visible_y - row->y;
9137 row->ascent = row->phys_ascent = 0;
9138 row->extra_line_spacing = 0;
9139 }
9140
9141 row->full_width_p = 1;
9142 row->continued_p = 0;
9143 row->truncated_on_left_p = 0;
9144 row->truncated_on_right_p = 0;
9145
9146 it->current_x = it->hpos = 0;
9147 it->current_y += row->height;
9148 ++it->vpos;
9149 ++it->glyph_row;
9150 }
9151
9152
9153 /* Value is the number of screen lines needed to make all tool-bar
9154 items of frame F visible. */
9155
9156 static int
9157 tool_bar_lines_needed (f)
9158 struct frame *f;
9159 {
9160 struct window *w = XWINDOW (f->tool_bar_window);
9161 struct it it;
9162
9163 /* Initialize an iterator for iteration over
9164 F->desired_tool_bar_string in the tool-bar window of frame F. */
9165 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9166 it.first_visible_x = 0;
9167 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9168 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9169
9170 while (!ITERATOR_AT_END_P (&it))
9171 {
9172 it.glyph_row = w->desired_matrix->rows;
9173 clear_glyph_row (it.glyph_row);
9174 display_tool_bar_line (&it);
9175 }
9176
9177 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9178 }
9179
9180
9181 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9182 0, 1, 0,
9183 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9184 (frame)
9185 Lisp_Object frame;
9186 {
9187 struct frame *f;
9188 struct window *w;
9189 int nlines = 0;
9190
9191 if (NILP (frame))
9192 frame = selected_frame;
9193 else
9194 CHECK_FRAME (frame);
9195 f = XFRAME (frame);
9196
9197 if (WINDOWP (f->tool_bar_window)
9198 || (w = XWINDOW (f->tool_bar_window),
9199 WINDOW_TOTAL_LINES (w) > 0))
9200 {
9201 update_tool_bar (f, 1);
9202 if (f->n_tool_bar_items)
9203 {
9204 build_desired_tool_bar_string (f);
9205 nlines = tool_bar_lines_needed (f);
9206 }
9207 }
9208
9209 return make_number (nlines);
9210 }
9211
9212
9213 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9214 height should be changed. */
9215
9216 static int
9217 redisplay_tool_bar (f)
9218 struct frame *f;
9219 {
9220 struct window *w;
9221 struct it it;
9222 struct glyph_row *row;
9223 int change_height_p = 0;
9224
9225 #ifdef USE_GTK
9226 if (FRAME_EXTERNAL_TOOL_BAR (f))
9227 update_frame_tool_bar (f);
9228 return 0;
9229 #endif
9230
9231 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9232 do anything. This means you must start with tool-bar-lines
9233 non-zero to get the auto-sizing effect. Or in other words, you
9234 can turn off tool-bars by specifying tool-bar-lines zero. */
9235 if (!WINDOWP (f->tool_bar_window)
9236 || (w = XWINDOW (f->tool_bar_window),
9237 WINDOW_TOTAL_LINES (w) == 0))
9238 return 0;
9239
9240 /* Set up an iterator for the tool-bar window. */
9241 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9242 it.first_visible_x = 0;
9243 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9244 row = it.glyph_row;
9245
9246 /* Build a string that represents the contents of the tool-bar. */
9247 build_desired_tool_bar_string (f);
9248 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9249
9250 /* Display as many lines as needed to display all tool-bar items. */
9251 while (it.current_y < it.last_visible_y)
9252 display_tool_bar_line (&it);
9253
9254 /* It doesn't make much sense to try scrolling in the tool-bar
9255 window, so don't do it. */
9256 w->desired_matrix->no_scrolling_p = 1;
9257 w->must_be_updated_p = 1;
9258
9259 if (auto_resize_tool_bars_p)
9260 {
9261 int nlines;
9262
9263 /* If we couldn't display everything, change the tool-bar's
9264 height. */
9265 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9266 change_height_p = 1;
9267
9268 /* If there are blank lines at the end, except for a partially
9269 visible blank line at the end that is smaller than
9270 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9271 row = it.glyph_row - 1;
9272 if (!row->displays_text_p
9273 && row->height >= FRAME_LINE_HEIGHT (f))
9274 change_height_p = 1;
9275
9276 /* If row displays tool-bar items, but is partially visible,
9277 change the tool-bar's height. */
9278 if (row->displays_text_p
9279 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9280 change_height_p = 1;
9281
9282 /* Resize windows as needed by changing the `tool-bar-lines'
9283 frame parameter. */
9284 if (change_height_p
9285 && (nlines = tool_bar_lines_needed (f),
9286 nlines != WINDOW_TOTAL_LINES (w)))
9287 {
9288 extern Lisp_Object Qtool_bar_lines;
9289 Lisp_Object frame;
9290 int old_height = WINDOW_TOTAL_LINES (w);
9291
9292 XSETFRAME (frame, f);
9293 clear_glyph_matrix (w->desired_matrix);
9294 Fmodify_frame_parameters (frame,
9295 Fcons (Fcons (Qtool_bar_lines,
9296 make_number (nlines)),
9297 Qnil));
9298 if (WINDOW_TOTAL_LINES (w) != old_height)
9299 fonts_changed_p = 1;
9300 }
9301 }
9302
9303 return change_height_p;
9304 }
9305
9306
9307 /* Get information about the tool-bar item which is displayed in GLYPH
9308 on frame F. Return in *PROP_IDX the index where tool-bar item
9309 properties start in F->tool_bar_items. Value is zero if
9310 GLYPH doesn't display a tool-bar item. */
9311
9312 static int
9313 tool_bar_item_info (f, glyph, prop_idx)
9314 struct frame *f;
9315 struct glyph *glyph;
9316 int *prop_idx;
9317 {
9318 Lisp_Object prop;
9319 int success_p;
9320 int charpos;
9321
9322 /* This function can be called asynchronously, which means we must
9323 exclude any possibility that Fget_text_property signals an
9324 error. */
9325 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9326 charpos = max (0, charpos);
9327
9328 /* Get the text property `menu-item' at pos. The value of that
9329 property is the start index of this item's properties in
9330 F->tool_bar_items. */
9331 prop = Fget_text_property (make_number (charpos),
9332 Qmenu_item, f->current_tool_bar_string);
9333 if (INTEGERP (prop))
9334 {
9335 *prop_idx = XINT (prop);
9336 success_p = 1;
9337 }
9338 else
9339 success_p = 0;
9340
9341 return success_p;
9342 }
9343
9344 \f
9345 /* Get information about the tool-bar item at position X/Y on frame F.
9346 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9347 the current matrix of the tool-bar window of F, or NULL if not
9348 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9349 item in F->tool_bar_items. Value is
9350
9351 -1 if X/Y is not on a tool-bar item
9352 0 if X/Y is on the same item that was highlighted before.
9353 1 otherwise. */
9354
9355 static int
9356 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9357 struct frame *f;
9358 int x, y;
9359 struct glyph **glyph;
9360 int *hpos, *vpos, *prop_idx;
9361 {
9362 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9363 struct window *w = XWINDOW (f->tool_bar_window);
9364 int area;
9365
9366 /* Find the glyph under X/Y. */
9367 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9368 if (*glyph == NULL)
9369 return -1;
9370
9371 /* Get the start of this tool-bar item's properties in
9372 f->tool_bar_items. */
9373 if (!tool_bar_item_info (f, *glyph, prop_idx))
9374 return -1;
9375
9376 /* Is mouse on the highlighted item? */
9377 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9378 && *vpos >= dpyinfo->mouse_face_beg_row
9379 && *vpos <= dpyinfo->mouse_face_end_row
9380 && (*vpos > dpyinfo->mouse_face_beg_row
9381 || *hpos >= dpyinfo->mouse_face_beg_col)
9382 && (*vpos < dpyinfo->mouse_face_end_row
9383 || *hpos < dpyinfo->mouse_face_end_col
9384 || dpyinfo->mouse_face_past_end))
9385 return 0;
9386
9387 return 1;
9388 }
9389
9390
9391 /* EXPORT:
9392 Handle mouse button event on the tool-bar of frame F, at
9393 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9394 0 for button release. MODIFIERS is event modifiers for button
9395 release. */
9396
9397 void
9398 handle_tool_bar_click (f, x, y, down_p, modifiers)
9399 struct frame *f;
9400 int x, y, down_p;
9401 unsigned int modifiers;
9402 {
9403 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9404 struct window *w = XWINDOW (f->tool_bar_window);
9405 int hpos, vpos, prop_idx;
9406 struct glyph *glyph;
9407 Lisp_Object enabled_p;
9408
9409 /* If not on the highlighted tool-bar item, return. */
9410 frame_to_window_pixel_xy (w, &x, &y);
9411 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9412 return;
9413
9414 /* If item is disabled, do nothing. */
9415 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9416 if (NILP (enabled_p))
9417 return;
9418
9419 if (down_p)
9420 {
9421 /* Show item in pressed state. */
9422 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9423 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9424 last_tool_bar_item = prop_idx;
9425 }
9426 else
9427 {
9428 Lisp_Object key, frame;
9429 struct input_event event;
9430 EVENT_INIT (event);
9431
9432 /* Show item in released state. */
9433 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9434 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9435
9436 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9437
9438 XSETFRAME (frame, f);
9439 event.kind = TOOL_BAR_EVENT;
9440 event.frame_or_window = frame;
9441 event.arg = frame;
9442 kbd_buffer_store_event (&event);
9443
9444 event.kind = TOOL_BAR_EVENT;
9445 event.frame_or_window = frame;
9446 event.arg = key;
9447 event.modifiers = modifiers;
9448 kbd_buffer_store_event (&event);
9449 last_tool_bar_item = -1;
9450 }
9451 }
9452
9453
9454 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9455 tool-bar window-relative coordinates X/Y. Called from
9456 note_mouse_highlight. */
9457
9458 static void
9459 note_tool_bar_highlight (f, x, y)
9460 struct frame *f;
9461 int x, y;
9462 {
9463 Lisp_Object window = f->tool_bar_window;
9464 struct window *w = XWINDOW (window);
9465 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9466 int hpos, vpos;
9467 struct glyph *glyph;
9468 struct glyph_row *row;
9469 int i;
9470 Lisp_Object enabled_p;
9471 int prop_idx;
9472 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9473 int mouse_down_p, rc;
9474
9475 /* Function note_mouse_highlight is called with negative x(y
9476 values when mouse moves outside of the frame. */
9477 if (x <= 0 || y <= 0)
9478 {
9479 clear_mouse_face (dpyinfo);
9480 return;
9481 }
9482
9483 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9484 if (rc < 0)
9485 {
9486 /* Not on tool-bar item. */
9487 clear_mouse_face (dpyinfo);
9488 return;
9489 }
9490 else if (rc == 0)
9491 /* On same tool-bar item as before. */
9492 goto set_help_echo;
9493
9494 clear_mouse_face (dpyinfo);
9495
9496 /* Mouse is down, but on different tool-bar item? */
9497 mouse_down_p = (dpyinfo->grabbed
9498 && f == last_mouse_frame
9499 && FRAME_LIVE_P (f));
9500 if (mouse_down_p
9501 && last_tool_bar_item != prop_idx)
9502 return;
9503
9504 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9505 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9506
9507 /* If tool-bar item is not enabled, don't highlight it. */
9508 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9509 if (!NILP (enabled_p))
9510 {
9511 /* Compute the x-position of the glyph. In front and past the
9512 image is a space. We include this in the highlighted area. */
9513 row = MATRIX_ROW (w->current_matrix, vpos);
9514 for (i = x = 0; i < hpos; ++i)
9515 x += row->glyphs[TEXT_AREA][i].pixel_width;
9516
9517 /* Record this as the current active region. */
9518 dpyinfo->mouse_face_beg_col = hpos;
9519 dpyinfo->mouse_face_beg_row = vpos;
9520 dpyinfo->mouse_face_beg_x = x;
9521 dpyinfo->mouse_face_beg_y = row->y;
9522 dpyinfo->mouse_face_past_end = 0;
9523
9524 dpyinfo->mouse_face_end_col = hpos + 1;
9525 dpyinfo->mouse_face_end_row = vpos;
9526 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9527 dpyinfo->mouse_face_end_y = row->y;
9528 dpyinfo->mouse_face_window = window;
9529 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9530
9531 /* Display it as active. */
9532 show_mouse_face (dpyinfo, draw);
9533 dpyinfo->mouse_face_image_state = draw;
9534 }
9535
9536 set_help_echo:
9537
9538 /* Set help_echo_string to a help string to display for this tool-bar item.
9539 XTread_socket does the rest. */
9540 help_echo_object = help_echo_window = Qnil;
9541 help_echo_pos = -1;
9542 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9543 if (NILP (help_echo_string))
9544 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9545 }
9546
9547 #endif /* HAVE_WINDOW_SYSTEM */
9548
9549
9550 \f
9551 /************************************************************************
9552 Horizontal scrolling
9553 ************************************************************************/
9554
9555 static int hscroll_window_tree P_ ((Lisp_Object));
9556 static int hscroll_windows P_ ((Lisp_Object));
9557
9558 /* For all leaf windows in the window tree rooted at WINDOW, set their
9559 hscroll value so that PT is (i) visible in the window, and (ii) so
9560 that it is not within a certain margin at the window's left and
9561 right border. Value is non-zero if any window's hscroll has been
9562 changed. */
9563
9564 static int
9565 hscroll_window_tree (window)
9566 Lisp_Object window;
9567 {
9568 int hscrolled_p = 0;
9569 int hscroll_relative_p = FLOATP (Vhscroll_step);
9570 int hscroll_step_abs = 0;
9571 double hscroll_step_rel = 0;
9572
9573 if (hscroll_relative_p)
9574 {
9575 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9576 if (hscroll_step_rel < 0)
9577 {
9578 hscroll_relative_p = 0;
9579 hscroll_step_abs = 0;
9580 }
9581 }
9582 else if (INTEGERP (Vhscroll_step))
9583 {
9584 hscroll_step_abs = XINT (Vhscroll_step);
9585 if (hscroll_step_abs < 0)
9586 hscroll_step_abs = 0;
9587 }
9588 else
9589 hscroll_step_abs = 0;
9590
9591 while (WINDOWP (window))
9592 {
9593 struct window *w = XWINDOW (window);
9594
9595 if (WINDOWP (w->hchild))
9596 hscrolled_p |= hscroll_window_tree (w->hchild);
9597 else if (WINDOWP (w->vchild))
9598 hscrolled_p |= hscroll_window_tree (w->vchild);
9599 else if (w->cursor.vpos >= 0)
9600 {
9601 int h_margin;
9602 int text_area_width;
9603 struct glyph_row *current_cursor_row
9604 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9605 struct glyph_row *desired_cursor_row
9606 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9607 struct glyph_row *cursor_row
9608 = (desired_cursor_row->enabled_p
9609 ? desired_cursor_row
9610 : current_cursor_row);
9611
9612 text_area_width = window_box_width (w, TEXT_AREA);
9613
9614 /* Scroll when cursor is inside this scroll margin. */
9615 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9616
9617 if ((XFASTINT (w->hscroll)
9618 && w->cursor.x <= h_margin)
9619 || (cursor_row->enabled_p
9620 && cursor_row->truncated_on_right_p
9621 && (w->cursor.x >= text_area_width - h_margin)))
9622 {
9623 struct it it;
9624 int hscroll;
9625 struct buffer *saved_current_buffer;
9626 int pt;
9627 int wanted_x;
9628
9629 /* Find point in a display of infinite width. */
9630 saved_current_buffer = current_buffer;
9631 current_buffer = XBUFFER (w->buffer);
9632
9633 if (w == XWINDOW (selected_window))
9634 pt = BUF_PT (current_buffer);
9635 else
9636 {
9637 pt = marker_position (w->pointm);
9638 pt = max (BEGV, pt);
9639 pt = min (ZV, pt);
9640 }
9641
9642 /* Move iterator to pt starting at cursor_row->start in
9643 a line with infinite width. */
9644 init_to_row_start (&it, w, cursor_row);
9645 it.last_visible_x = INFINITY;
9646 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9647 current_buffer = saved_current_buffer;
9648
9649 /* Position cursor in window. */
9650 if (!hscroll_relative_p && hscroll_step_abs == 0)
9651 hscroll = max (0, (it.current_x
9652 - (ITERATOR_AT_END_OF_LINE_P (&it)
9653 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9654 : (text_area_width / 2))))
9655 / FRAME_COLUMN_WIDTH (it.f);
9656 else if (w->cursor.x >= text_area_width - h_margin)
9657 {
9658 if (hscroll_relative_p)
9659 wanted_x = text_area_width * (1 - hscroll_step_rel)
9660 - h_margin;
9661 else
9662 wanted_x = text_area_width
9663 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9664 - h_margin;
9665 hscroll
9666 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9667 }
9668 else
9669 {
9670 if (hscroll_relative_p)
9671 wanted_x = text_area_width * hscroll_step_rel
9672 + h_margin;
9673 else
9674 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9675 + h_margin;
9676 hscroll
9677 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9678 }
9679 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9680
9681 /* Don't call Fset_window_hscroll if value hasn't
9682 changed because it will prevent redisplay
9683 optimizations. */
9684 if (XFASTINT (w->hscroll) != hscroll)
9685 {
9686 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9687 w->hscroll = make_number (hscroll);
9688 hscrolled_p = 1;
9689 }
9690 }
9691 }
9692
9693 window = w->next;
9694 }
9695
9696 /* Value is non-zero if hscroll of any leaf window has been changed. */
9697 return hscrolled_p;
9698 }
9699
9700
9701 /* Set hscroll so that cursor is visible and not inside horizontal
9702 scroll margins for all windows in the tree rooted at WINDOW. See
9703 also hscroll_window_tree above. Value is non-zero if any window's
9704 hscroll has been changed. If it has, desired matrices on the frame
9705 of WINDOW are cleared. */
9706
9707 static int
9708 hscroll_windows (window)
9709 Lisp_Object window;
9710 {
9711 int hscrolled_p;
9712
9713 if (automatic_hscrolling_p)
9714 {
9715 hscrolled_p = hscroll_window_tree (window);
9716 if (hscrolled_p)
9717 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9718 }
9719 else
9720 hscrolled_p = 0;
9721 return hscrolled_p;
9722 }
9723
9724
9725 \f
9726 /************************************************************************
9727 Redisplay
9728 ************************************************************************/
9729
9730 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9731 to a non-zero value. This is sometimes handy to have in a debugger
9732 session. */
9733
9734 #if GLYPH_DEBUG
9735
9736 /* First and last unchanged row for try_window_id. */
9737
9738 int debug_first_unchanged_at_end_vpos;
9739 int debug_last_unchanged_at_beg_vpos;
9740
9741 /* Delta vpos and y. */
9742
9743 int debug_dvpos, debug_dy;
9744
9745 /* Delta in characters and bytes for try_window_id. */
9746
9747 int debug_delta, debug_delta_bytes;
9748
9749 /* Values of window_end_pos and window_end_vpos at the end of
9750 try_window_id. */
9751
9752 EMACS_INT debug_end_pos, debug_end_vpos;
9753
9754 /* Append a string to W->desired_matrix->method. FMT is a printf
9755 format string. A1...A9 are a supplement for a variable-length
9756 argument list. If trace_redisplay_p is non-zero also printf the
9757 resulting string to stderr. */
9758
9759 static void
9760 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9761 struct window *w;
9762 char *fmt;
9763 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9764 {
9765 char buffer[512];
9766 char *method = w->desired_matrix->method;
9767 int len = strlen (method);
9768 int size = sizeof w->desired_matrix->method;
9769 int remaining = size - len - 1;
9770
9771 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9772 if (len && remaining)
9773 {
9774 method[len] = '|';
9775 --remaining, ++len;
9776 }
9777
9778 strncpy (method + len, buffer, remaining);
9779
9780 if (trace_redisplay_p)
9781 fprintf (stderr, "%p (%s): %s\n",
9782 w,
9783 ((BUFFERP (w->buffer)
9784 && STRINGP (XBUFFER (w->buffer)->name))
9785 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9786 : "no buffer"),
9787 buffer);
9788 }
9789
9790 #endif /* GLYPH_DEBUG */
9791
9792
9793 /* Value is non-zero if all changes in window W, which displays
9794 current_buffer, are in the text between START and END. START is a
9795 buffer position, END is given as a distance from Z. Used in
9796 redisplay_internal for display optimization. */
9797
9798 static INLINE int
9799 text_outside_line_unchanged_p (w, start, end)
9800 struct window *w;
9801 int start, end;
9802 {
9803 int unchanged_p = 1;
9804
9805 /* If text or overlays have changed, see where. */
9806 if (XFASTINT (w->last_modified) < MODIFF
9807 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9808 {
9809 /* Gap in the line? */
9810 if (GPT < start || Z - GPT < end)
9811 unchanged_p = 0;
9812
9813 /* Changes start in front of the line, or end after it? */
9814 if (unchanged_p
9815 && (BEG_UNCHANGED < start - 1
9816 || END_UNCHANGED < end))
9817 unchanged_p = 0;
9818
9819 /* If selective display, can't optimize if changes start at the
9820 beginning of the line. */
9821 if (unchanged_p
9822 && INTEGERP (current_buffer->selective_display)
9823 && XINT (current_buffer->selective_display) > 0
9824 && (BEG_UNCHANGED < start || GPT <= start))
9825 unchanged_p = 0;
9826
9827 /* If there are overlays at the start or end of the line, these
9828 may have overlay strings with newlines in them. A change at
9829 START, for instance, may actually concern the display of such
9830 overlay strings as well, and they are displayed on different
9831 lines. So, quickly rule out this case. (For the future, it
9832 might be desirable to implement something more telling than
9833 just BEG/END_UNCHANGED.) */
9834 if (unchanged_p)
9835 {
9836 if (BEG + BEG_UNCHANGED == start
9837 && overlay_touches_p (start))
9838 unchanged_p = 0;
9839 if (END_UNCHANGED == end
9840 && overlay_touches_p (Z - end))
9841 unchanged_p = 0;
9842 }
9843 }
9844
9845 return unchanged_p;
9846 }
9847
9848
9849 /* Do a frame update, taking possible shortcuts into account. This is
9850 the main external entry point for redisplay.
9851
9852 If the last redisplay displayed an echo area message and that message
9853 is no longer requested, we clear the echo area or bring back the
9854 mini-buffer if that is in use. */
9855
9856 void
9857 redisplay ()
9858 {
9859 redisplay_internal (0);
9860 }
9861
9862
9863 static Lisp_Object
9864 overlay_arrow_string_or_property (var)
9865 Lisp_Object var;
9866 {
9867 Lisp_Object val;
9868
9869 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9870 return val;
9871
9872 return Voverlay_arrow_string;
9873 }
9874
9875 /* Return 1 if there are any overlay-arrows in current_buffer. */
9876 static int
9877 overlay_arrow_in_current_buffer_p ()
9878 {
9879 Lisp_Object vlist;
9880
9881 for (vlist = Voverlay_arrow_variable_list;
9882 CONSP (vlist);
9883 vlist = XCDR (vlist))
9884 {
9885 Lisp_Object var = XCAR (vlist);
9886 Lisp_Object val;
9887
9888 if (!SYMBOLP (var))
9889 continue;
9890 val = find_symbol_value (var);
9891 if (MARKERP (val)
9892 && current_buffer == XMARKER (val)->buffer)
9893 return 1;
9894 }
9895 return 0;
9896 }
9897
9898
9899 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9900 has changed. */
9901
9902 static int
9903 overlay_arrows_changed_p ()
9904 {
9905 Lisp_Object vlist;
9906
9907 for (vlist = Voverlay_arrow_variable_list;
9908 CONSP (vlist);
9909 vlist = XCDR (vlist))
9910 {
9911 Lisp_Object var = XCAR (vlist);
9912 Lisp_Object val, pstr;
9913
9914 if (!SYMBOLP (var))
9915 continue;
9916 val = find_symbol_value (var);
9917 if (!MARKERP (val))
9918 continue;
9919 if (! EQ (COERCE_MARKER (val),
9920 Fget (var, Qlast_arrow_position))
9921 || ! (pstr = overlay_arrow_string_or_property (var),
9922 EQ (pstr, Fget (var, Qlast_arrow_string))))
9923 return 1;
9924 }
9925 return 0;
9926 }
9927
9928 /* Mark overlay arrows to be updated on next redisplay. */
9929
9930 static void
9931 update_overlay_arrows (up_to_date)
9932 int up_to_date;
9933 {
9934 Lisp_Object vlist;
9935
9936 for (vlist = Voverlay_arrow_variable_list;
9937 CONSP (vlist);
9938 vlist = XCDR (vlist))
9939 {
9940 Lisp_Object var = XCAR (vlist);
9941
9942 if (!SYMBOLP (var))
9943 continue;
9944
9945 if (up_to_date > 0)
9946 {
9947 Lisp_Object val = find_symbol_value (var);
9948 Fput (var, Qlast_arrow_position,
9949 COERCE_MARKER (val));
9950 Fput (var, Qlast_arrow_string,
9951 overlay_arrow_string_or_property (var));
9952 }
9953 else if (up_to_date < 0
9954 || !NILP (Fget (var, Qlast_arrow_position)))
9955 {
9956 Fput (var, Qlast_arrow_position, Qt);
9957 Fput (var, Qlast_arrow_string, Qt);
9958 }
9959 }
9960 }
9961
9962
9963 /* Return overlay arrow string to display at row.
9964 Return integer (bitmap number) for arrow bitmap in left fringe.
9965 Return nil if no overlay arrow. */
9966
9967 static Lisp_Object
9968 overlay_arrow_at_row (it, row)
9969 struct it *it;
9970 struct glyph_row *row;
9971 {
9972 Lisp_Object vlist;
9973
9974 for (vlist = Voverlay_arrow_variable_list;
9975 CONSP (vlist);
9976 vlist = XCDR (vlist))
9977 {
9978 Lisp_Object var = XCAR (vlist);
9979 Lisp_Object val;
9980
9981 if (!SYMBOLP (var))
9982 continue;
9983
9984 val = find_symbol_value (var);
9985
9986 if (MARKERP (val)
9987 && current_buffer == XMARKER (val)->buffer
9988 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9989 {
9990 if (FRAME_WINDOW_P (it->f)
9991 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9992 {
9993 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9994 {
9995 int fringe_bitmap;
9996 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9997 return make_number (fringe_bitmap);
9998 }
9999 return make_number (-1); /* Use default arrow bitmap */
10000 }
10001 return overlay_arrow_string_or_property (var);
10002 }
10003 }
10004
10005 return Qnil;
10006 }
10007
10008 /* Return 1 if point moved out of or into a composition. Otherwise
10009 return 0. PREV_BUF and PREV_PT are the last point buffer and
10010 position. BUF and PT are the current point buffer and position. */
10011
10012 int
10013 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10014 struct buffer *prev_buf, *buf;
10015 int prev_pt, pt;
10016 {
10017 int start, end;
10018 Lisp_Object prop;
10019 Lisp_Object buffer;
10020
10021 XSETBUFFER (buffer, buf);
10022 /* Check a composition at the last point if point moved within the
10023 same buffer. */
10024 if (prev_buf == buf)
10025 {
10026 if (prev_pt == pt)
10027 /* Point didn't move. */
10028 return 0;
10029
10030 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10031 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10032 && COMPOSITION_VALID_P (start, end, prop)
10033 && start < prev_pt && end > prev_pt)
10034 /* The last point was within the composition. Return 1 iff
10035 point moved out of the composition. */
10036 return (pt <= start || pt >= end);
10037 }
10038
10039 /* Check a composition at the current point. */
10040 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10041 && find_composition (pt, -1, &start, &end, &prop, buffer)
10042 && COMPOSITION_VALID_P (start, end, prop)
10043 && start < pt && end > pt);
10044 }
10045
10046
10047 /* Reconsider the setting of B->clip_changed which is displayed
10048 in window W. */
10049
10050 static INLINE void
10051 reconsider_clip_changes (w, b)
10052 struct window *w;
10053 struct buffer *b;
10054 {
10055 if (b->clip_changed
10056 && !NILP (w->window_end_valid)
10057 && w->current_matrix->buffer == b
10058 && w->current_matrix->zv == BUF_ZV (b)
10059 && w->current_matrix->begv == BUF_BEGV (b))
10060 b->clip_changed = 0;
10061
10062 /* If display wasn't paused, and W is not a tool bar window, see if
10063 point has been moved into or out of a composition. In that case,
10064 we set b->clip_changed to 1 to force updating the screen. If
10065 b->clip_changed has already been set to 1, we can skip this
10066 check. */
10067 if (!b->clip_changed
10068 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10069 {
10070 int pt;
10071
10072 if (w == XWINDOW (selected_window))
10073 pt = BUF_PT (current_buffer);
10074 else
10075 pt = marker_position (w->pointm);
10076
10077 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10078 || pt != XINT (w->last_point))
10079 && check_point_in_composition (w->current_matrix->buffer,
10080 XINT (w->last_point),
10081 XBUFFER (w->buffer), pt))
10082 b->clip_changed = 1;
10083 }
10084 }
10085 \f
10086
10087 /* Select FRAME to forward the values of frame-local variables into C
10088 variables so that the redisplay routines can access those values
10089 directly. */
10090
10091 static void
10092 select_frame_for_redisplay (frame)
10093 Lisp_Object frame;
10094 {
10095 Lisp_Object tail, sym, val;
10096 Lisp_Object old = selected_frame;
10097
10098 selected_frame = frame;
10099
10100 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10101 if (CONSP (XCAR (tail))
10102 && (sym = XCAR (XCAR (tail)),
10103 SYMBOLP (sym))
10104 && (sym = indirect_variable (sym),
10105 val = SYMBOL_VALUE (sym),
10106 (BUFFER_LOCAL_VALUEP (val)
10107 || SOME_BUFFER_LOCAL_VALUEP (val)))
10108 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10109 /* Use find_symbol_value rather than Fsymbol_value
10110 to avoid an error if it is void. */
10111 find_symbol_value (sym);
10112
10113 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10114 if (CONSP (XCAR (tail))
10115 && (sym = XCAR (XCAR (tail)),
10116 SYMBOLP (sym))
10117 && (sym = indirect_variable (sym),
10118 val = SYMBOL_VALUE (sym),
10119 (BUFFER_LOCAL_VALUEP (val)
10120 || SOME_BUFFER_LOCAL_VALUEP (val)))
10121 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10122 find_symbol_value (sym);
10123 }
10124
10125
10126 #define STOP_POLLING \
10127 do { if (! polling_stopped_here) stop_polling (); \
10128 polling_stopped_here = 1; } while (0)
10129
10130 #define RESUME_POLLING \
10131 do { if (polling_stopped_here) start_polling (); \
10132 polling_stopped_here = 0; } while (0)
10133
10134
10135 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10136 response to any user action; therefore, we should preserve the echo
10137 area. (Actually, our caller does that job.) Perhaps in the future
10138 avoid recentering windows if it is not necessary; currently that
10139 causes some problems. */
10140
10141 static void
10142 redisplay_internal (preserve_echo_area)
10143 int preserve_echo_area;
10144 {
10145 struct window *w = XWINDOW (selected_window);
10146 struct frame *f = XFRAME (w->frame);
10147 int pause;
10148 int must_finish = 0;
10149 struct text_pos tlbufpos, tlendpos;
10150 int number_of_visible_frames;
10151 int count;
10152 struct frame *sf = SELECTED_FRAME ();
10153 int polling_stopped_here = 0;
10154
10155 /* Non-zero means redisplay has to consider all windows on all
10156 frames. Zero means, only selected_window is considered. */
10157 int consider_all_windows_p;
10158
10159 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10160
10161 /* No redisplay if running in batch mode or frame is not yet fully
10162 initialized, or redisplay is explicitly turned off by setting
10163 Vinhibit_redisplay. */
10164 if (noninteractive
10165 || !NILP (Vinhibit_redisplay)
10166 || !f->glyphs_initialized_p)
10167 return;
10168
10169 /* The flag redisplay_performed_directly_p is set by
10170 direct_output_for_insert when it already did the whole screen
10171 update necessary. */
10172 if (redisplay_performed_directly_p)
10173 {
10174 redisplay_performed_directly_p = 0;
10175 if (!hscroll_windows (selected_window))
10176 return;
10177 }
10178
10179 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10180 if (popup_activated ())
10181 return;
10182 #endif
10183
10184 /* I don't think this happens but let's be paranoid. */
10185 if (redisplaying_p)
10186 return;
10187
10188 /* Record a function that resets redisplaying_p to its old value
10189 when we leave this function. */
10190 count = SPECPDL_INDEX ();
10191 record_unwind_protect (unwind_redisplay,
10192 Fcons (make_number (redisplaying_p), selected_frame));
10193 ++redisplaying_p;
10194 specbind (Qinhibit_free_realized_faces, Qnil);
10195
10196 {
10197 Lisp_Object tail, frame;
10198
10199 FOR_EACH_FRAME (tail, frame)
10200 {
10201 struct frame *f = XFRAME (frame);
10202 f->already_hscrolled_p = 0;
10203 }
10204 }
10205
10206 retry:
10207 pause = 0;
10208 reconsider_clip_changes (w, current_buffer);
10209
10210 /* If new fonts have been loaded that make a glyph matrix adjustment
10211 necessary, do it. */
10212 if (fonts_changed_p)
10213 {
10214 adjust_glyphs (NULL);
10215 ++windows_or_buffers_changed;
10216 fonts_changed_p = 0;
10217 }
10218
10219 /* If face_change_count is non-zero, init_iterator will free all
10220 realized faces, which includes the faces referenced from current
10221 matrices. So, we can't reuse current matrices in this case. */
10222 if (face_change_count)
10223 ++windows_or_buffers_changed;
10224
10225 if (! FRAME_WINDOW_P (sf)
10226 && previous_terminal_frame != sf)
10227 {
10228 /* Since frames on an ASCII terminal share the same display
10229 area, displaying a different frame means redisplay the whole
10230 thing. */
10231 windows_or_buffers_changed++;
10232 SET_FRAME_GARBAGED (sf);
10233 XSETFRAME (Vterminal_frame, sf);
10234 }
10235 previous_terminal_frame = sf;
10236
10237 /* Set the visible flags for all frames. Do this before checking
10238 for resized or garbaged frames; they want to know if their frames
10239 are visible. See the comment in frame.h for
10240 FRAME_SAMPLE_VISIBILITY. */
10241 {
10242 Lisp_Object tail, frame;
10243
10244 number_of_visible_frames = 0;
10245
10246 FOR_EACH_FRAME (tail, frame)
10247 {
10248 struct frame *f = XFRAME (frame);
10249
10250 FRAME_SAMPLE_VISIBILITY (f);
10251 if (FRAME_VISIBLE_P (f))
10252 ++number_of_visible_frames;
10253 clear_desired_matrices (f);
10254 }
10255 }
10256
10257 /* Notice any pending interrupt request to change frame size. */
10258 do_pending_window_change (1);
10259
10260 /* Clear frames marked as garbaged. */
10261 if (frame_garbaged)
10262 clear_garbaged_frames ();
10263
10264 /* Build menubar and tool-bar items. */
10265 prepare_menu_bars ();
10266
10267 if (windows_or_buffers_changed)
10268 update_mode_lines++;
10269
10270 /* Detect case that we need to write or remove a star in the mode line. */
10271 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10272 {
10273 w->update_mode_line = Qt;
10274 if (buffer_shared > 1)
10275 update_mode_lines++;
10276 }
10277
10278 /* If %c is in the mode line, update it if needed. */
10279 if (!NILP (w->column_number_displayed)
10280 /* This alternative quickly identifies a common case
10281 where no change is needed. */
10282 && !(PT == XFASTINT (w->last_point)
10283 && XFASTINT (w->last_modified) >= MODIFF
10284 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10285 && (XFASTINT (w->column_number_displayed)
10286 != (int) current_column ())) /* iftc */
10287 w->update_mode_line = Qt;
10288
10289 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10290
10291 /* The variable buffer_shared is set in redisplay_window and
10292 indicates that we redisplay a buffer in different windows. See
10293 there. */
10294 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10295 || cursor_type_changed);
10296
10297 /* If specs for an arrow have changed, do thorough redisplay
10298 to ensure we remove any arrow that should no longer exist. */
10299 if (overlay_arrows_changed_p ())
10300 consider_all_windows_p = windows_or_buffers_changed = 1;
10301
10302 /* Normally the message* functions will have already displayed and
10303 updated the echo area, but the frame may have been trashed, or
10304 the update may have been preempted, so display the echo area
10305 again here. Checking message_cleared_p captures the case that
10306 the echo area should be cleared. */
10307 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10308 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10309 || (message_cleared_p
10310 && minibuf_level == 0
10311 /* If the mini-window is currently selected, this means the
10312 echo-area doesn't show through. */
10313 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10314 {
10315 int window_height_changed_p = echo_area_display (0);
10316 must_finish = 1;
10317
10318 /* If we don't display the current message, don't clear the
10319 message_cleared_p flag, because, if we did, we wouldn't clear
10320 the echo area in the next redisplay which doesn't preserve
10321 the echo area. */
10322 if (!display_last_displayed_message_p)
10323 message_cleared_p = 0;
10324
10325 if (fonts_changed_p)
10326 goto retry;
10327 else if (window_height_changed_p)
10328 {
10329 consider_all_windows_p = 1;
10330 ++update_mode_lines;
10331 ++windows_or_buffers_changed;
10332
10333 /* If window configuration was changed, frames may have been
10334 marked garbaged. Clear them or we will experience
10335 surprises wrt scrolling. */
10336 if (frame_garbaged)
10337 clear_garbaged_frames ();
10338 }
10339 }
10340 else if (EQ (selected_window, minibuf_window)
10341 && (current_buffer->clip_changed
10342 || XFASTINT (w->last_modified) < MODIFF
10343 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10344 && resize_mini_window (w, 0))
10345 {
10346 /* Resized active mini-window to fit the size of what it is
10347 showing if its contents might have changed. */
10348 must_finish = 1;
10349 consider_all_windows_p = 1;
10350 ++windows_or_buffers_changed;
10351 ++update_mode_lines;
10352
10353 /* If window configuration was changed, frames may have been
10354 marked garbaged. Clear them or we will experience
10355 surprises wrt scrolling. */
10356 if (frame_garbaged)
10357 clear_garbaged_frames ();
10358 }
10359
10360
10361 /* If showing the region, and mark has changed, we must redisplay
10362 the whole window. The assignment to this_line_start_pos prevents
10363 the optimization directly below this if-statement. */
10364 if (((!NILP (Vtransient_mark_mode)
10365 && !NILP (XBUFFER (w->buffer)->mark_active))
10366 != !NILP (w->region_showing))
10367 || (!NILP (w->region_showing)
10368 && !EQ (w->region_showing,
10369 Fmarker_position (XBUFFER (w->buffer)->mark))))
10370 CHARPOS (this_line_start_pos) = 0;
10371
10372 /* Optimize the case that only the line containing the cursor in the
10373 selected window has changed. Variables starting with this_ are
10374 set in display_line and record information about the line
10375 containing the cursor. */
10376 tlbufpos = this_line_start_pos;
10377 tlendpos = this_line_end_pos;
10378 if (!consider_all_windows_p
10379 && CHARPOS (tlbufpos) > 0
10380 && NILP (w->update_mode_line)
10381 && !current_buffer->clip_changed
10382 && !current_buffer->prevent_redisplay_optimizations_p
10383 && FRAME_VISIBLE_P (XFRAME (w->frame))
10384 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10385 /* Make sure recorded data applies to current buffer, etc. */
10386 && this_line_buffer == current_buffer
10387 && current_buffer == XBUFFER (w->buffer)
10388 && NILP (w->force_start)
10389 && NILP (w->optional_new_start)
10390 /* Point must be on the line that we have info recorded about. */
10391 && PT >= CHARPOS (tlbufpos)
10392 && PT <= Z - CHARPOS (tlendpos)
10393 /* All text outside that line, including its final newline,
10394 must be unchanged */
10395 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10396 CHARPOS (tlendpos)))
10397 {
10398 if (CHARPOS (tlbufpos) > BEGV
10399 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10400 && (CHARPOS (tlbufpos) == ZV
10401 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10402 /* Former continuation line has disappeared by becoming empty */
10403 goto cancel;
10404 else if (XFASTINT (w->last_modified) < MODIFF
10405 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10406 || MINI_WINDOW_P (w))
10407 {
10408 /* We have to handle the case of continuation around a
10409 wide-column character (See the comment in indent.c around
10410 line 885).
10411
10412 For instance, in the following case:
10413
10414 -------- Insert --------
10415 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10416 J_I_ ==> J_I_ `^^' are cursors.
10417 ^^ ^^
10418 -------- --------
10419
10420 As we have to redraw the line above, we should goto cancel. */
10421
10422 struct it it;
10423 int line_height_before = this_line_pixel_height;
10424
10425 /* Note that start_display will handle the case that the
10426 line starting at tlbufpos is a continuation lines. */
10427 start_display (&it, w, tlbufpos);
10428
10429 /* Implementation note: It this still necessary? */
10430 if (it.current_x != this_line_start_x)
10431 goto cancel;
10432
10433 TRACE ((stderr, "trying display optimization 1\n"));
10434 w->cursor.vpos = -1;
10435 overlay_arrow_seen = 0;
10436 it.vpos = this_line_vpos;
10437 it.current_y = this_line_y;
10438 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10439 display_line (&it);
10440
10441 /* If line contains point, is not continued,
10442 and ends at same distance from eob as before, we win */
10443 if (w->cursor.vpos >= 0
10444 /* Line is not continued, otherwise this_line_start_pos
10445 would have been set to 0 in display_line. */
10446 && CHARPOS (this_line_start_pos)
10447 /* Line ends as before. */
10448 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10449 /* Line has same height as before. Otherwise other lines
10450 would have to be shifted up or down. */
10451 && this_line_pixel_height == line_height_before)
10452 {
10453 /* If this is not the window's last line, we must adjust
10454 the charstarts of the lines below. */
10455 if (it.current_y < it.last_visible_y)
10456 {
10457 struct glyph_row *row
10458 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10459 int delta, delta_bytes;
10460
10461 if (Z - CHARPOS (tlendpos) == ZV)
10462 {
10463 /* This line ends at end of (accessible part of)
10464 buffer. There is no newline to count. */
10465 delta = (Z
10466 - CHARPOS (tlendpos)
10467 - MATRIX_ROW_START_CHARPOS (row));
10468 delta_bytes = (Z_BYTE
10469 - BYTEPOS (tlendpos)
10470 - MATRIX_ROW_START_BYTEPOS (row));
10471 }
10472 else
10473 {
10474 /* This line ends in a newline. Must take
10475 account of the newline and the rest of the
10476 text that follows. */
10477 delta = (Z
10478 - CHARPOS (tlendpos)
10479 - MATRIX_ROW_START_CHARPOS (row));
10480 delta_bytes = (Z_BYTE
10481 - BYTEPOS (tlendpos)
10482 - MATRIX_ROW_START_BYTEPOS (row));
10483 }
10484
10485 increment_matrix_positions (w->current_matrix,
10486 this_line_vpos + 1,
10487 w->current_matrix->nrows,
10488 delta, delta_bytes);
10489 }
10490
10491 /* If this row displays text now but previously didn't,
10492 or vice versa, w->window_end_vpos may have to be
10493 adjusted. */
10494 if ((it.glyph_row - 1)->displays_text_p)
10495 {
10496 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10497 XSETINT (w->window_end_vpos, this_line_vpos);
10498 }
10499 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10500 && this_line_vpos > 0)
10501 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10502 w->window_end_valid = Qnil;
10503
10504 /* Update hint: No need to try to scroll in update_window. */
10505 w->desired_matrix->no_scrolling_p = 1;
10506
10507 #if GLYPH_DEBUG
10508 *w->desired_matrix->method = 0;
10509 debug_method_add (w, "optimization 1");
10510 #endif
10511 #ifdef HAVE_WINDOW_SYSTEM
10512 update_window_fringes (w, 0);
10513 #endif
10514 goto update;
10515 }
10516 else
10517 goto cancel;
10518 }
10519 else if (/* Cursor position hasn't changed. */
10520 PT == XFASTINT (w->last_point)
10521 /* Make sure the cursor was last displayed
10522 in this window. Otherwise we have to reposition it. */
10523 && 0 <= w->cursor.vpos
10524 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10525 {
10526 if (!must_finish)
10527 {
10528 do_pending_window_change (1);
10529
10530 /* We used to always goto end_of_redisplay here, but this
10531 isn't enough if we have a blinking cursor. */
10532 if (w->cursor_off_p == w->last_cursor_off_p)
10533 goto end_of_redisplay;
10534 }
10535 goto update;
10536 }
10537 /* If highlighting the region, or if the cursor is in the echo area,
10538 then we can't just move the cursor. */
10539 else if (! (!NILP (Vtransient_mark_mode)
10540 && !NILP (current_buffer->mark_active))
10541 && (EQ (selected_window, current_buffer->last_selected_window)
10542 || highlight_nonselected_windows)
10543 && NILP (w->region_showing)
10544 && NILP (Vshow_trailing_whitespace)
10545 && !cursor_in_echo_area)
10546 {
10547 struct it it;
10548 struct glyph_row *row;
10549
10550 /* Skip from tlbufpos to PT and see where it is. Note that
10551 PT may be in invisible text. If so, we will end at the
10552 next visible position. */
10553 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10554 NULL, DEFAULT_FACE_ID);
10555 it.current_x = this_line_start_x;
10556 it.current_y = this_line_y;
10557 it.vpos = this_line_vpos;
10558
10559 /* The call to move_it_to stops in front of PT, but
10560 moves over before-strings. */
10561 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10562
10563 if (it.vpos == this_line_vpos
10564 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10565 row->enabled_p))
10566 {
10567 xassert (this_line_vpos == it.vpos);
10568 xassert (this_line_y == it.current_y);
10569 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10570 #if GLYPH_DEBUG
10571 *w->desired_matrix->method = 0;
10572 debug_method_add (w, "optimization 3");
10573 #endif
10574 goto update;
10575 }
10576 else
10577 goto cancel;
10578 }
10579
10580 cancel:
10581 /* Text changed drastically or point moved off of line. */
10582 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10583 }
10584
10585 CHARPOS (this_line_start_pos) = 0;
10586 consider_all_windows_p |= buffer_shared > 1;
10587 ++clear_face_cache_count;
10588 #ifdef HAVE_WINDOW_SYSTEM
10589 ++clear_image_cache_count;
10590 #endif
10591
10592 /* Build desired matrices, and update the display. If
10593 consider_all_windows_p is non-zero, do it for all windows on all
10594 frames. Otherwise do it for selected_window, only. */
10595
10596 if (consider_all_windows_p)
10597 {
10598 Lisp_Object tail, frame;
10599 int i, n = 0, size = 50;
10600 struct frame **updated
10601 = (struct frame **) alloca (size * sizeof *updated);
10602
10603 /* Recompute # windows showing selected buffer. This will be
10604 incremented each time such a window is displayed. */
10605 buffer_shared = 0;
10606
10607 FOR_EACH_FRAME (tail, frame)
10608 {
10609 struct frame *f = XFRAME (frame);
10610
10611 if (FRAME_WINDOW_P (f) || f == sf)
10612 {
10613 if (! EQ (frame, selected_frame))
10614 /* Select the frame, for the sake of frame-local
10615 variables. */
10616 select_frame_for_redisplay (frame);
10617
10618 /* Mark all the scroll bars to be removed; we'll redeem
10619 the ones we want when we redisplay their windows. */
10620 if (condemn_scroll_bars_hook)
10621 condemn_scroll_bars_hook (f);
10622
10623 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10624 redisplay_windows (FRAME_ROOT_WINDOW (f));
10625
10626 /* Any scroll bars which redisplay_windows should have
10627 nuked should now go away. */
10628 if (judge_scroll_bars_hook)
10629 judge_scroll_bars_hook (f);
10630
10631 /* If fonts changed, display again. */
10632 /* ??? rms: I suspect it is a mistake to jump all the way
10633 back to retry here. It should just retry this frame. */
10634 if (fonts_changed_p)
10635 goto retry;
10636
10637 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10638 {
10639 /* See if we have to hscroll. */
10640 if (!f->already_hscrolled_p)
10641 {
10642 f->already_hscrolled_p = 1;
10643 if (hscroll_windows (f->root_window))
10644 goto retry;
10645 }
10646
10647 /* Prevent various kinds of signals during display
10648 update. stdio is not robust about handling
10649 signals, which can cause an apparent I/O
10650 error. */
10651 if (interrupt_input)
10652 unrequest_sigio ();
10653 STOP_POLLING;
10654
10655 /* Update the display. */
10656 set_window_update_flags (XWINDOW (f->root_window), 1);
10657 pause |= update_frame (f, 0, 0);
10658 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10659 if (pause)
10660 break;
10661 #endif
10662
10663 if (n == size)
10664 {
10665 int nbytes = size * sizeof *updated;
10666 struct frame **p = (struct frame **) alloca (2 * nbytes);
10667 bcopy (updated, p, nbytes);
10668 size *= 2;
10669 }
10670
10671 updated[n++] = f;
10672 }
10673 }
10674 }
10675
10676 if (!pause)
10677 {
10678 /* Do the mark_window_display_accurate after all windows have
10679 been redisplayed because this call resets flags in buffers
10680 which are needed for proper redisplay. */
10681 for (i = 0; i < n; ++i)
10682 {
10683 struct frame *f = updated[i];
10684 mark_window_display_accurate (f->root_window, 1);
10685 if (frame_up_to_date_hook)
10686 frame_up_to_date_hook (f);
10687 }
10688 }
10689 }
10690 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10691 {
10692 Lisp_Object mini_window;
10693 struct frame *mini_frame;
10694
10695 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10696 /* Use list_of_error, not Qerror, so that
10697 we catch only errors and don't run the debugger. */
10698 internal_condition_case_1 (redisplay_window_1, selected_window,
10699 list_of_error,
10700 redisplay_window_error);
10701
10702 /* Compare desired and current matrices, perform output. */
10703
10704 update:
10705 /* If fonts changed, display again. */
10706 if (fonts_changed_p)
10707 goto retry;
10708
10709 /* Prevent various kinds of signals during display update.
10710 stdio is not robust about handling signals,
10711 which can cause an apparent I/O error. */
10712 if (interrupt_input)
10713 unrequest_sigio ();
10714 STOP_POLLING;
10715
10716 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10717 {
10718 if (hscroll_windows (selected_window))
10719 goto retry;
10720
10721 XWINDOW (selected_window)->must_be_updated_p = 1;
10722 pause = update_frame (sf, 0, 0);
10723 }
10724
10725 /* We may have called echo_area_display at the top of this
10726 function. If the echo area is on another frame, that may
10727 have put text on a frame other than the selected one, so the
10728 above call to update_frame would not have caught it. Catch
10729 it here. */
10730 mini_window = FRAME_MINIBUF_WINDOW (sf);
10731 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10732
10733 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10734 {
10735 XWINDOW (mini_window)->must_be_updated_p = 1;
10736 pause |= update_frame (mini_frame, 0, 0);
10737 if (!pause && hscroll_windows (mini_window))
10738 goto retry;
10739 }
10740 }
10741
10742 /* If display was paused because of pending input, make sure we do a
10743 thorough update the next time. */
10744 if (pause)
10745 {
10746 /* Prevent the optimization at the beginning of
10747 redisplay_internal that tries a single-line update of the
10748 line containing the cursor in the selected window. */
10749 CHARPOS (this_line_start_pos) = 0;
10750
10751 /* Let the overlay arrow be updated the next time. */
10752 update_overlay_arrows (0);
10753
10754 /* If we pause after scrolling, some rows in the current
10755 matrices of some windows are not valid. */
10756 if (!WINDOW_FULL_WIDTH_P (w)
10757 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10758 update_mode_lines = 1;
10759 }
10760 else
10761 {
10762 if (!consider_all_windows_p)
10763 {
10764 /* This has already been done above if
10765 consider_all_windows_p is set. */
10766 mark_window_display_accurate_1 (w, 1);
10767
10768 /* Say overlay arrows are up to date. */
10769 update_overlay_arrows (1);
10770
10771 if (frame_up_to_date_hook != 0)
10772 frame_up_to_date_hook (sf);
10773 }
10774
10775 update_mode_lines = 0;
10776 windows_or_buffers_changed = 0;
10777 cursor_type_changed = 0;
10778 }
10779
10780 /* Start SIGIO interrupts coming again. Having them off during the
10781 code above makes it less likely one will discard output, but not
10782 impossible, since there might be stuff in the system buffer here.
10783 But it is much hairier to try to do anything about that. */
10784 if (interrupt_input)
10785 request_sigio ();
10786 RESUME_POLLING;
10787
10788 /* If a frame has become visible which was not before, redisplay
10789 again, so that we display it. Expose events for such a frame
10790 (which it gets when becoming visible) don't call the parts of
10791 redisplay constructing glyphs, so simply exposing a frame won't
10792 display anything in this case. So, we have to display these
10793 frames here explicitly. */
10794 if (!pause)
10795 {
10796 Lisp_Object tail, frame;
10797 int new_count = 0;
10798
10799 FOR_EACH_FRAME (tail, frame)
10800 {
10801 int this_is_visible = 0;
10802
10803 if (XFRAME (frame)->visible)
10804 this_is_visible = 1;
10805 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10806 if (XFRAME (frame)->visible)
10807 this_is_visible = 1;
10808
10809 if (this_is_visible)
10810 new_count++;
10811 }
10812
10813 if (new_count != number_of_visible_frames)
10814 windows_or_buffers_changed++;
10815 }
10816
10817 /* Change frame size now if a change is pending. */
10818 do_pending_window_change (1);
10819
10820 /* If we just did a pending size change, or have additional
10821 visible frames, redisplay again. */
10822 if (windows_or_buffers_changed && !pause)
10823 goto retry;
10824
10825 /* Clear the face cache eventually. */
10826 if (consider_all_windows_p)
10827 {
10828 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10829 {
10830 clear_face_cache (0);
10831 clear_face_cache_count = 0;
10832 }
10833 #ifdef HAVE_WINDOW_SYSTEM
10834 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10835 {
10836 Lisp_Object tail, frame;
10837 FOR_EACH_FRAME (tail, frame)
10838 {
10839 struct frame *f = XFRAME (frame);
10840 if (FRAME_WINDOW_P (f))
10841 clear_image_cache (f, 0);
10842 }
10843 clear_image_cache_count = 0;
10844 }
10845 #endif /* HAVE_WINDOW_SYSTEM */
10846 }
10847
10848 end_of_redisplay:
10849 unbind_to (count, Qnil);
10850 RESUME_POLLING;
10851 }
10852
10853
10854 /* Redisplay, but leave alone any recent echo area message unless
10855 another message has been requested in its place.
10856
10857 This is useful in situations where you need to redisplay but no
10858 user action has occurred, making it inappropriate for the message
10859 area to be cleared. See tracking_off and
10860 wait_reading_process_output for examples of these situations.
10861
10862 FROM_WHERE is an integer saying from where this function was
10863 called. This is useful for debugging. */
10864
10865 void
10866 redisplay_preserve_echo_area (from_where)
10867 int from_where;
10868 {
10869 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10870
10871 if (!NILP (echo_area_buffer[1]))
10872 {
10873 /* We have a previously displayed message, but no current
10874 message. Redisplay the previous message. */
10875 display_last_displayed_message_p = 1;
10876 redisplay_internal (1);
10877 display_last_displayed_message_p = 0;
10878 }
10879 else
10880 redisplay_internal (1);
10881
10882 if (rif != NULL && rif->flush_display_optional)
10883 rif->flush_display_optional (NULL);
10884 }
10885
10886
10887 /* Function registered with record_unwind_protect in
10888 redisplay_internal. Reset redisplaying_p to the value it had
10889 before redisplay_internal was called, and clear
10890 prevent_freeing_realized_faces_p. It also selects the previously
10891 selected frame. */
10892
10893 static Lisp_Object
10894 unwind_redisplay (val)
10895 Lisp_Object val;
10896 {
10897 Lisp_Object old_redisplaying_p, old_frame;
10898
10899 old_redisplaying_p = XCAR (val);
10900 redisplaying_p = XFASTINT (old_redisplaying_p);
10901 old_frame = XCDR (val);
10902 if (! EQ (old_frame, selected_frame))
10903 select_frame_for_redisplay (old_frame);
10904 return Qnil;
10905 }
10906
10907
10908 /* Mark the display of window W as accurate or inaccurate. If
10909 ACCURATE_P is non-zero mark display of W as accurate. If
10910 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10911 redisplay_internal is called. */
10912
10913 static void
10914 mark_window_display_accurate_1 (w, accurate_p)
10915 struct window *w;
10916 int accurate_p;
10917 {
10918 if (BUFFERP (w->buffer))
10919 {
10920 struct buffer *b = XBUFFER (w->buffer);
10921
10922 w->last_modified
10923 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10924 w->last_overlay_modified
10925 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10926 w->last_had_star
10927 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10928
10929 if (accurate_p)
10930 {
10931 b->clip_changed = 0;
10932 b->prevent_redisplay_optimizations_p = 0;
10933
10934 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10935 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10936 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10937 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10938
10939 w->current_matrix->buffer = b;
10940 w->current_matrix->begv = BUF_BEGV (b);
10941 w->current_matrix->zv = BUF_ZV (b);
10942
10943 w->last_cursor = w->cursor;
10944 w->last_cursor_off_p = w->cursor_off_p;
10945
10946 if (w == XWINDOW (selected_window))
10947 w->last_point = make_number (BUF_PT (b));
10948 else
10949 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10950 }
10951 }
10952
10953 if (accurate_p)
10954 {
10955 w->window_end_valid = w->buffer;
10956 #if 0 /* This is incorrect with variable-height lines. */
10957 xassert (XINT (w->window_end_vpos)
10958 < (WINDOW_TOTAL_LINES (w)
10959 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10960 #endif
10961 w->update_mode_line = Qnil;
10962 }
10963 }
10964
10965
10966 /* Mark the display of windows in the window tree rooted at WINDOW as
10967 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10968 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10969 be redisplayed the next time redisplay_internal is called. */
10970
10971 void
10972 mark_window_display_accurate (window, accurate_p)
10973 Lisp_Object window;
10974 int accurate_p;
10975 {
10976 struct window *w;
10977
10978 for (; !NILP (window); window = w->next)
10979 {
10980 w = XWINDOW (window);
10981 mark_window_display_accurate_1 (w, accurate_p);
10982
10983 if (!NILP (w->vchild))
10984 mark_window_display_accurate (w->vchild, accurate_p);
10985 if (!NILP (w->hchild))
10986 mark_window_display_accurate (w->hchild, accurate_p);
10987 }
10988
10989 if (accurate_p)
10990 {
10991 update_overlay_arrows (1);
10992 }
10993 else
10994 {
10995 /* Force a thorough redisplay the next time by setting
10996 last_arrow_position and last_arrow_string to t, which is
10997 unequal to any useful value of Voverlay_arrow_... */
10998 update_overlay_arrows (-1);
10999 }
11000 }
11001
11002
11003 /* Return value in display table DP (Lisp_Char_Table *) for character
11004 C. Since a display table doesn't have any parent, we don't have to
11005 follow parent. Do not call this function directly but use the
11006 macro DISP_CHAR_VECTOR. */
11007
11008 Lisp_Object
11009 disp_char_vector (dp, c)
11010 struct Lisp_Char_Table *dp;
11011 int c;
11012 {
11013 int code[4], i;
11014 Lisp_Object val;
11015
11016 if (SINGLE_BYTE_CHAR_P (c))
11017 return (dp->contents[c]);
11018
11019 SPLIT_CHAR (c, code[0], code[1], code[2]);
11020 if (code[1] < 32)
11021 code[1] = -1;
11022 else if (code[2] < 32)
11023 code[2] = -1;
11024
11025 /* Here, the possible range of code[0] (== charset ID) is
11026 128..max_charset. Since the top level char table contains data
11027 for multibyte characters after 256th element, we must increment
11028 code[0] by 128 to get a correct index. */
11029 code[0] += 128;
11030 code[3] = -1; /* anchor */
11031
11032 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11033 {
11034 val = dp->contents[code[i]];
11035 if (!SUB_CHAR_TABLE_P (val))
11036 return (NILP (val) ? dp->defalt : val);
11037 }
11038
11039 /* Here, val is a sub char table. We return the default value of
11040 it. */
11041 return (dp->defalt);
11042 }
11043
11044
11045 \f
11046 /***********************************************************************
11047 Window Redisplay
11048 ***********************************************************************/
11049
11050 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11051
11052 static void
11053 redisplay_windows (window)
11054 Lisp_Object window;
11055 {
11056 while (!NILP (window))
11057 {
11058 struct window *w = XWINDOW (window);
11059
11060 if (!NILP (w->hchild))
11061 redisplay_windows (w->hchild);
11062 else if (!NILP (w->vchild))
11063 redisplay_windows (w->vchild);
11064 else
11065 {
11066 displayed_buffer = XBUFFER (w->buffer);
11067 /* Use list_of_error, not Qerror, so that
11068 we catch only errors and don't run the debugger. */
11069 internal_condition_case_1 (redisplay_window_0, window,
11070 list_of_error,
11071 redisplay_window_error);
11072 }
11073
11074 window = w->next;
11075 }
11076 }
11077
11078 static Lisp_Object
11079 redisplay_window_error ()
11080 {
11081 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11082 return Qnil;
11083 }
11084
11085 static Lisp_Object
11086 redisplay_window_0 (window)
11087 Lisp_Object window;
11088 {
11089 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11090 redisplay_window (window, 0);
11091 return Qnil;
11092 }
11093
11094 static Lisp_Object
11095 redisplay_window_1 (window)
11096 Lisp_Object window;
11097 {
11098 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11099 redisplay_window (window, 1);
11100 return Qnil;
11101 }
11102 \f
11103
11104 /* Increment GLYPH until it reaches END or CONDITION fails while
11105 adding (GLYPH)->pixel_width to X. */
11106
11107 #define SKIP_GLYPHS(glyph, end, x, condition) \
11108 do \
11109 { \
11110 (x) += (glyph)->pixel_width; \
11111 ++(glyph); \
11112 } \
11113 while ((glyph) < (end) && (condition))
11114
11115
11116 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11117 DELTA is the number of bytes by which positions recorded in ROW
11118 differ from current buffer positions. */
11119
11120 void
11121 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11122 struct window *w;
11123 struct glyph_row *row;
11124 struct glyph_matrix *matrix;
11125 int delta, delta_bytes, dy, dvpos;
11126 {
11127 struct glyph *glyph = row->glyphs[TEXT_AREA];
11128 struct glyph *end = glyph + row->used[TEXT_AREA];
11129 struct glyph *cursor = NULL;
11130 /* The first glyph that starts a sequence of glyphs from string. */
11131 struct glyph *string_start;
11132 /* The X coordinate of string_start. */
11133 int string_start_x;
11134 /* The last known character position. */
11135 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11136 /* The last known character position before string_start. */
11137 int string_before_pos;
11138 int x = row->x;
11139 int cursor_x = x;
11140 int cursor_from_overlay_pos = 0;
11141 int pt_old = PT - delta;
11142
11143 /* Skip over glyphs not having an object at the start of the row.
11144 These are special glyphs like truncation marks on terminal
11145 frames. */
11146 if (row->displays_text_p)
11147 while (glyph < end
11148 && INTEGERP (glyph->object)
11149 && glyph->charpos < 0)
11150 {
11151 x += glyph->pixel_width;
11152 ++glyph;
11153 }
11154
11155 string_start = NULL;
11156 while (glyph < end
11157 && !INTEGERP (glyph->object)
11158 && (!BUFFERP (glyph->object)
11159 || (last_pos = glyph->charpos) < pt_old))
11160 {
11161 if (! STRINGP (glyph->object))
11162 {
11163 string_start = NULL;
11164 x += glyph->pixel_width;
11165 ++glyph;
11166 if (cursor_from_overlay_pos
11167 && last_pos > cursor_from_overlay_pos)
11168 {
11169 cursor_from_overlay_pos = 0;
11170 cursor = 0;
11171 }
11172 }
11173 else
11174 {
11175 string_before_pos = last_pos;
11176 string_start = glyph;
11177 string_start_x = x;
11178 /* Skip all glyphs from string. */
11179 do
11180 {
11181 int pos;
11182 if ((cursor == NULL || glyph > cursor)
11183 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11184 Qcursor, (glyph)->object))
11185 && (pos = string_buffer_position (w, glyph->object,
11186 string_before_pos),
11187 (pos == 0 /* From overlay */
11188 || pos == pt_old)))
11189 {
11190 /* Estimate overlay buffer position from the buffer
11191 positions of the glyphs before and after the overlay.
11192 Add 1 to last_pos so that if point corresponds to the
11193 glyph right after the overlay, we still use a 'cursor'
11194 property found in that overlay. */
11195 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11196 cursor = glyph;
11197 cursor_x = x;
11198 }
11199 x += glyph->pixel_width;
11200 ++glyph;
11201 }
11202 while (glyph < end && STRINGP (glyph->object));
11203 }
11204 }
11205
11206 if (cursor != NULL)
11207 {
11208 glyph = cursor;
11209 x = cursor_x;
11210 }
11211 else if (row->ends_in_ellipsis_p && glyph == end)
11212 {
11213 /* Scan back over the ellipsis glyphs, decrementing positions. */
11214 while (glyph > row->glyphs[TEXT_AREA]
11215 && (glyph - 1)->charpos == last_pos)
11216 glyph--, x -= glyph->pixel_width;
11217 /* That loop always goes one position too far,
11218 including the glyph before the ellipsis.
11219 So scan forward over that one. */
11220 x += glyph->pixel_width;
11221 glyph++;
11222 }
11223 else if (string_start
11224 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11225 {
11226 /* We may have skipped over point because the previous glyphs
11227 are from string. As there's no easy way to know the
11228 character position of the current glyph, find the correct
11229 glyph on point by scanning from string_start again. */
11230 Lisp_Object limit;
11231 Lisp_Object string;
11232 int pos;
11233
11234 limit = make_number (pt_old + 1);
11235 end = glyph;
11236 glyph = string_start;
11237 x = string_start_x;
11238 string = glyph->object;
11239 pos = string_buffer_position (w, string, string_before_pos);
11240 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11241 because we always put cursor after overlay strings. */
11242 while (pos == 0 && glyph < end)
11243 {
11244 string = glyph->object;
11245 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11246 if (glyph < end)
11247 pos = string_buffer_position (w, glyph->object, string_before_pos);
11248 }
11249
11250 while (glyph < end)
11251 {
11252 pos = XINT (Fnext_single_char_property_change
11253 (make_number (pos), Qdisplay, Qnil, limit));
11254 if (pos > pt_old)
11255 break;
11256 /* Skip glyphs from the same string. */
11257 string = glyph->object;
11258 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11259 /* Skip glyphs from an overlay. */
11260 while (glyph < end
11261 && ! string_buffer_position (w, glyph->object, pos))
11262 {
11263 string = glyph->object;
11264 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11265 }
11266 }
11267 }
11268
11269 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11270 w->cursor.x = x;
11271 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11272 w->cursor.y = row->y + dy;
11273
11274 if (w == XWINDOW (selected_window))
11275 {
11276 if (!row->continued_p
11277 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11278 && row->x == 0)
11279 {
11280 this_line_buffer = XBUFFER (w->buffer);
11281
11282 CHARPOS (this_line_start_pos)
11283 = MATRIX_ROW_START_CHARPOS (row) + delta;
11284 BYTEPOS (this_line_start_pos)
11285 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11286
11287 CHARPOS (this_line_end_pos)
11288 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11289 BYTEPOS (this_line_end_pos)
11290 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11291
11292 this_line_y = w->cursor.y;
11293 this_line_pixel_height = row->height;
11294 this_line_vpos = w->cursor.vpos;
11295 this_line_start_x = row->x;
11296 }
11297 else
11298 CHARPOS (this_line_start_pos) = 0;
11299 }
11300 }
11301
11302
11303 /* Run window scroll functions, if any, for WINDOW with new window
11304 start STARTP. Sets the window start of WINDOW to that position.
11305
11306 We assume that the window's buffer is really current. */
11307
11308 static INLINE struct text_pos
11309 run_window_scroll_functions (window, startp)
11310 Lisp_Object window;
11311 struct text_pos startp;
11312 {
11313 struct window *w = XWINDOW (window);
11314 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11315
11316 if (current_buffer != XBUFFER (w->buffer))
11317 abort ();
11318
11319 if (!NILP (Vwindow_scroll_functions))
11320 {
11321 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11322 make_number (CHARPOS (startp)));
11323 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11324 /* In case the hook functions switch buffers. */
11325 if (current_buffer != XBUFFER (w->buffer))
11326 set_buffer_internal_1 (XBUFFER (w->buffer));
11327 }
11328
11329 return startp;
11330 }
11331
11332
11333 /* Make sure the line containing the cursor is fully visible.
11334 A value of 1 means there is nothing to be done.
11335 (Either the line is fully visible, or it cannot be made so,
11336 or we cannot tell.)
11337
11338 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11339 is higher than window.
11340
11341 A value of 0 means the caller should do scrolling
11342 as if point had gone off the screen. */
11343
11344 static int
11345 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11346 struct window *w;
11347 int force_p;
11348 {
11349 struct glyph_matrix *matrix;
11350 struct glyph_row *row;
11351 int window_height;
11352
11353 if (!make_cursor_line_fully_visible_p)
11354 return 1;
11355
11356 /* It's not always possible to find the cursor, e.g, when a window
11357 is full of overlay strings. Don't do anything in that case. */
11358 if (w->cursor.vpos < 0)
11359 return 1;
11360
11361 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11362 row = MATRIX_ROW (matrix, w->cursor.vpos);
11363
11364 /* If the cursor row is not partially visible, there's nothing to do. */
11365 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11366 return 1;
11367
11368 /* If the row the cursor is in is taller than the window's height,
11369 it's not clear what to do, so do nothing. */
11370 window_height = window_box_height (w);
11371 if (row->height >= window_height)
11372 {
11373 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11374 return 1;
11375 }
11376 return 0;
11377
11378 #if 0
11379 /* This code used to try to scroll the window just enough to make
11380 the line visible. It returned 0 to say that the caller should
11381 allocate larger glyph matrices. */
11382
11383 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11384 {
11385 int dy = row->height - row->visible_height;
11386 w->vscroll = 0;
11387 w->cursor.y += dy;
11388 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11389 }
11390 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11391 {
11392 int dy = - (row->height - row->visible_height);
11393 w->vscroll = dy;
11394 w->cursor.y += dy;
11395 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11396 }
11397
11398 /* When we change the cursor y-position of the selected window,
11399 change this_line_y as well so that the display optimization for
11400 the cursor line of the selected window in redisplay_internal uses
11401 the correct y-position. */
11402 if (w == XWINDOW (selected_window))
11403 this_line_y = w->cursor.y;
11404
11405 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11406 redisplay with larger matrices. */
11407 if (matrix->nrows < required_matrix_height (w))
11408 {
11409 fonts_changed_p = 1;
11410 return 0;
11411 }
11412
11413 return 1;
11414 #endif /* 0 */
11415 }
11416
11417
11418 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11419 non-zero means only WINDOW is redisplayed in redisplay_internal.
11420 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11421 in redisplay_window to bring a partially visible line into view in
11422 the case that only the cursor has moved.
11423
11424 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11425 last screen line's vertical height extends past the end of the screen.
11426
11427 Value is
11428
11429 1 if scrolling succeeded
11430
11431 0 if scrolling didn't find point.
11432
11433 -1 if new fonts have been loaded so that we must interrupt
11434 redisplay, adjust glyph matrices, and try again. */
11435
11436 enum
11437 {
11438 SCROLLING_SUCCESS,
11439 SCROLLING_FAILED,
11440 SCROLLING_NEED_LARGER_MATRICES
11441 };
11442
11443 static int
11444 try_scrolling (window, just_this_one_p, scroll_conservatively,
11445 scroll_step, temp_scroll_step, last_line_misfit)
11446 Lisp_Object window;
11447 int just_this_one_p;
11448 EMACS_INT scroll_conservatively, scroll_step;
11449 int temp_scroll_step;
11450 int last_line_misfit;
11451 {
11452 struct window *w = XWINDOW (window);
11453 struct frame *f = XFRAME (w->frame);
11454 struct text_pos scroll_margin_pos;
11455 struct text_pos pos;
11456 struct text_pos startp;
11457 struct it it;
11458 Lisp_Object window_end;
11459 int this_scroll_margin;
11460 int dy = 0;
11461 int scroll_max;
11462 int rc;
11463 int amount_to_scroll = 0;
11464 Lisp_Object aggressive;
11465 int height;
11466 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11467
11468 #if GLYPH_DEBUG
11469 debug_method_add (w, "try_scrolling");
11470 #endif
11471
11472 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11473
11474 /* Compute scroll margin height in pixels. We scroll when point is
11475 within this distance from the top or bottom of the window. */
11476 if (scroll_margin > 0)
11477 {
11478 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11479 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11480 }
11481 else
11482 this_scroll_margin = 0;
11483
11484 /* Force scroll_conservatively to have a reasonable value so it doesn't
11485 cause an overflow while computing how much to scroll. */
11486 if (scroll_conservatively)
11487 scroll_conservatively = min (scroll_conservatively,
11488 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11489
11490 /* Compute how much we should try to scroll maximally to bring point
11491 into view. */
11492 if (scroll_step || scroll_conservatively || temp_scroll_step)
11493 scroll_max = max (scroll_step,
11494 max (scroll_conservatively, temp_scroll_step));
11495 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11496 || NUMBERP (current_buffer->scroll_up_aggressively))
11497 /* We're trying to scroll because of aggressive scrolling
11498 but no scroll_step is set. Choose an arbitrary one. Maybe
11499 there should be a variable for this. */
11500 scroll_max = 10;
11501 else
11502 scroll_max = 0;
11503 scroll_max *= FRAME_LINE_HEIGHT (f);
11504
11505 /* Decide whether we have to scroll down. Start at the window end
11506 and move this_scroll_margin up to find the position of the scroll
11507 margin. */
11508 window_end = Fwindow_end (window, Qt);
11509
11510 too_near_end:
11511
11512 CHARPOS (scroll_margin_pos) = XINT (window_end);
11513 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11514
11515 if (this_scroll_margin || extra_scroll_margin_lines)
11516 {
11517 start_display (&it, w, scroll_margin_pos);
11518 if (this_scroll_margin)
11519 move_it_vertically_backward (&it, this_scroll_margin);
11520 if (extra_scroll_margin_lines)
11521 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11522 scroll_margin_pos = it.current.pos;
11523 }
11524
11525 if (PT >= CHARPOS (scroll_margin_pos))
11526 {
11527 int y0;
11528
11529 /* Point is in the scroll margin at the bottom of the window, or
11530 below. Compute a new window start that makes point visible. */
11531
11532 /* Compute the distance from the scroll margin to PT.
11533 Give up if the distance is greater than scroll_max. */
11534 start_display (&it, w, scroll_margin_pos);
11535 y0 = it.current_y;
11536 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11537 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11538
11539 /* To make point visible, we have to move the window start
11540 down so that the line the cursor is in is visible, which
11541 means we have to add in the height of the cursor line. */
11542 dy = line_bottom_y (&it) - y0;
11543
11544 if (dy > scroll_max)
11545 return SCROLLING_FAILED;
11546
11547 /* Move the window start down. If scrolling conservatively,
11548 move it just enough down to make point visible. If
11549 scroll_step is set, move it down by scroll_step. */
11550 start_display (&it, w, startp);
11551
11552 if (scroll_conservatively)
11553 /* Set AMOUNT_TO_SCROLL to at least one line,
11554 and at most scroll_conservatively lines. */
11555 amount_to_scroll
11556 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11557 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11558 else if (scroll_step || temp_scroll_step)
11559 amount_to_scroll = scroll_max;
11560 else
11561 {
11562 aggressive = current_buffer->scroll_up_aggressively;
11563 height = WINDOW_BOX_TEXT_HEIGHT (w);
11564 if (NUMBERP (aggressive))
11565 {
11566 double float_amount = XFLOATINT (aggressive) * height;
11567 amount_to_scroll = float_amount;
11568 if (amount_to_scroll == 0 && float_amount > 0)
11569 amount_to_scroll = 1;
11570 }
11571 }
11572
11573 if (amount_to_scroll <= 0)
11574 return SCROLLING_FAILED;
11575
11576 /* If moving by amount_to_scroll leaves STARTP unchanged,
11577 move it down one screen line. */
11578
11579 move_it_vertically (&it, amount_to_scroll);
11580 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11581 move_it_by_lines (&it, 1, 1);
11582 startp = it.current.pos;
11583 }
11584 else
11585 {
11586 /* See if point is inside the scroll margin at the top of the
11587 window. */
11588 scroll_margin_pos = startp;
11589 if (this_scroll_margin)
11590 {
11591 start_display (&it, w, startp);
11592 move_it_vertically (&it, this_scroll_margin);
11593 scroll_margin_pos = it.current.pos;
11594 }
11595
11596 if (PT < CHARPOS (scroll_margin_pos))
11597 {
11598 /* Point is in the scroll margin at the top of the window or
11599 above what is displayed in the window. */
11600 int y0;
11601
11602 /* Compute the vertical distance from PT to the scroll
11603 margin position. Give up if distance is greater than
11604 scroll_max. */
11605 SET_TEXT_POS (pos, PT, PT_BYTE);
11606 start_display (&it, w, pos);
11607 y0 = it.current_y;
11608 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11609 it.last_visible_y, -1,
11610 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11611 dy = it.current_y - y0;
11612 if (dy > scroll_max)
11613 return SCROLLING_FAILED;
11614
11615 /* Compute new window start. */
11616 start_display (&it, w, startp);
11617
11618 if (scroll_conservatively)
11619 amount_to_scroll
11620 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11621 else if (scroll_step || temp_scroll_step)
11622 amount_to_scroll = scroll_max;
11623 else
11624 {
11625 aggressive = current_buffer->scroll_down_aggressively;
11626 height = WINDOW_BOX_TEXT_HEIGHT (w);
11627 if (NUMBERP (aggressive))
11628 {
11629 double float_amount = XFLOATINT (aggressive) * height;
11630 amount_to_scroll = float_amount;
11631 if (amount_to_scroll == 0 && float_amount > 0)
11632 amount_to_scroll = 1;
11633 }
11634 }
11635
11636 if (amount_to_scroll <= 0)
11637 return SCROLLING_FAILED;
11638
11639 move_it_vertically_backward (&it, amount_to_scroll);
11640 startp = it.current.pos;
11641 }
11642 }
11643
11644 /* Run window scroll functions. */
11645 startp = run_window_scroll_functions (window, startp);
11646
11647 /* Display the window. Give up if new fonts are loaded, or if point
11648 doesn't appear. */
11649 if (!try_window (window, startp, 0))
11650 rc = SCROLLING_NEED_LARGER_MATRICES;
11651 else if (w->cursor.vpos < 0)
11652 {
11653 clear_glyph_matrix (w->desired_matrix);
11654 rc = SCROLLING_FAILED;
11655 }
11656 else
11657 {
11658 /* Maybe forget recorded base line for line number display. */
11659 if (!just_this_one_p
11660 || current_buffer->clip_changed
11661 || BEG_UNCHANGED < CHARPOS (startp))
11662 w->base_line_number = Qnil;
11663
11664 /* If cursor ends up on a partially visible line,
11665 treat that as being off the bottom of the screen. */
11666 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11667 {
11668 clear_glyph_matrix (w->desired_matrix);
11669 ++extra_scroll_margin_lines;
11670 goto too_near_end;
11671 }
11672 rc = SCROLLING_SUCCESS;
11673 }
11674
11675 return rc;
11676 }
11677
11678
11679 /* Compute a suitable window start for window W if display of W starts
11680 on a continuation line. Value is non-zero if a new window start
11681 was computed.
11682
11683 The new window start will be computed, based on W's width, starting
11684 from the start of the continued line. It is the start of the
11685 screen line with the minimum distance from the old start W->start. */
11686
11687 static int
11688 compute_window_start_on_continuation_line (w)
11689 struct window *w;
11690 {
11691 struct text_pos pos, start_pos;
11692 int window_start_changed_p = 0;
11693
11694 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11695
11696 /* If window start is on a continuation line... Window start may be
11697 < BEGV in case there's invisible text at the start of the
11698 buffer (M-x rmail, for example). */
11699 if (CHARPOS (start_pos) > BEGV
11700 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11701 {
11702 struct it it;
11703 struct glyph_row *row;
11704
11705 /* Handle the case that the window start is out of range. */
11706 if (CHARPOS (start_pos) < BEGV)
11707 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11708 else if (CHARPOS (start_pos) > ZV)
11709 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11710
11711 /* Find the start of the continued line. This should be fast
11712 because scan_buffer is fast (newline cache). */
11713 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11714 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11715 row, DEFAULT_FACE_ID);
11716 reseat_at_previous_visible_line_start (&it);
11717
11718 /* If the line start is "too far" away from the window start,
11719 say it takes too much time to compute a new window start. */
11720 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11721 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11722 {
11723 int min_distance, distance;
11724
11725 /* Move forward by display lines to find the new window
11726 start. If window width was enlarged, the new start can
11727 be expected to be > the old start. If window width was
11728 decreased, the new window start will be < the old start.
11729 So, we're looking for the display line start with the
11730 minimum distance from the old window start. */
11731 pos = it.current.pos;
11732 min_distance = INFINITY;
11733 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11734 distance < min_distance)
11735 {
11736 min_distance = distance;
11737 pos = it.current.pos;
11738 move_it_by_lines (&it, 1, 0);
11739 }
11740
11741 /* Set the window start there. */
11742 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11743 window_start_changed_p = 1;
11744 }
11745 }
11746
11747 return window_start_changed_p;
11748 }
11749
11750
11751 /* Try cursor movement in case text has not changed in window WINDOW,
11752 with window start STARTP. Value is
11753
11754 CURSOR_MOVEMENT_SUCCESS if successful
11755
11756 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11757
11758 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11759 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11760 we want to scroll as if scroll-step were set to 1. See the code.
11761
11762 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11763 which case we have to abort this redisplay, and adjust matrices
11764 first. */
11765
11766 enum
11767 {
11768 CURSOR_MOVEMENT_SUCCESS,
11769 CURSOR_MOVEMENT_CANNOT_BE_USED,
11770 CURSOR_MOVEMENT_MUST_SCROLL,
11771 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11772 };
11773
11774 static int
11775 try_cursor_movement (window, startp, scroll_step)
11776 Lisp_Object window;
11777 struct text_pos startp;
11778 int *scroll_step;
11779 {
11780 struct window *w = XWINDOW (window);
11781 struct frame *f = XFRAME (w->frame);
11782 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11783
11784 #if GLYPH_DEBUG
11785 if (inhibit_try_cursor_movement)
11786 return rc;
11787 #endif
11788
11789 /* Handle case where text has not changed, only point, and it has
11790 not moved off the frame. */
11791 if (/* Point may be in this window. */
11792 PT >= CHARPOS (startp)
11793 /* Selective display hasn't changed. */
11794 && !current_buffer->clip_changed
11795 /* Function force-mode-line-update is used to force a thorough
11796 redisplay. It sets either windows_or_buffers_changed or
11797 update_mode_lines. So don't take a shortcut here for these
11798 cases. */
11799 && !update_mode_lines
11800 && !windows_or_buffers_changed
11801 && !cursor_type_changed
11802 /* Can't use this case if highlighting a region. When a
11803 region exists, cursor movement has to do more than just
11804 set the cursor. */
11805 && !(!NILP (Vtransient_mark_mode)
11806 && !NILP (current_buffer->mark_active))
11807 && NILP (w->region_showing)
11808 && NILP (Vshow_trailing_whitespace)
11809 /* Right after splitting windows, last_point may be nil. */
11810 && INTEGERP (w->last_point)
11811 /* This code is not used for mini-buffer for the sake of the case
11812 of redisplaying to replace an echo area message; since in
11813 that case the mini-buffer contents per se are usually
11814 unchanged. This code is of no real use in the mini-buffer
11815 since the handling of this_line_start_pos, etc., in redisplay
11816 handles the same cases. */
11817 && !EQ (window, minibuf_window)
11818 /* When splitting windows or for new windows, it happens that
11819 redisplay is called with a nil window_end_vpos or one being
11820 larger than the window. This should really be fixed in
11821 window.c. I don't have this on my list, now, so we do
11822 approximately the same as the old redisplay code. --gerd. */
11823 && INTEGERP (w->window_end_vpos)
11824 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11825 && (FRAME_WINDOW_P (f)
11826 || !overlay_arrow_in_current_buffer_p ()))
11827 {
11828 int this_scroll_margin, top_scroll_margin;
11829 struct glyph_row *row = NULL;
11830
11831 #if GLYPH_DEBUG
11832 debug_method_add (w, "cursor movement");
11833 #endif
11834
11835 /* Scroll if point within this distance from the top or bottom
11836 of the window. This is a pixel value. */
11837 this_scroll_margin = max (0, scroll_margin);
11838 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11839 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11840
11841 top_scroll_margin = this_scroll_margin;
11842 if (WINDOW_WANTS_HEADER_LINE_P (w))
11843 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11844
11845 /* Start with the row the cursor was displayed during the last
11846 not paused redisplay. Give up if that row is not valid. */
11847 if (w->last_cursor.vpos < 0
11848 || w->last_cursor.vpos >= w->current_matrix->nrows)
11849 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11850 else
11851 {
11852 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11853 if (row->mode_line_p)
11854 ++row;
11855 if (!row->enabled_p)
11856 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11857 }
11858
11859 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11860 {
11861 int scroll_p = 0;
11862 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11863
11864 if (PT > XFASTINT (w->last_point))
11865 {
11866 /* Point has moved forward. */
11867 while (MATRIX_ROW_END_CHARPOS (row) < PT
11868 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11869 {
11870 xassert (row->enabled_p);
11871 ++row;
11872 }
11873
11874 /* The end position of a row equals the start position
11875 of the next row. If PT is there, we would rather
11876 display it in the next line. */
11877 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11878 && MATRIX_ROW_END_CHARPOS (row) == PT
11879 && !cursor_row_p (w, row))
11880 ++row;
11881
11882 /* If within the scroll margin, scroll. Note that
11883 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11884 the next line would be drawn, and that
11885 this_scroll_margin can be zero. */
11886 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11887 || PT > MATRIX_ROW_END_CHARPOS (row)
11888 /* Line is completely visible last line in window
11889 and PT is to be set in the next line. */
11890 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11891 && PT == MATRIX_ROW_END_CHARPOS (row)
11892 && !row->ends_at_zv_p
11893 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11894 scroll_p = 1;
11895 }
11896 else if (PT < XFASTINT (w->last_point))
11897 {
11898 /* Cursor has to be moved backward. Note that PT >=
11899 CHARPOS (startp) because of the outer if-statement. */
11900 while (!row->mode_line_p
11901 && (MATRIX_ROW_START_CHARPOS (row) > PT
11902 || (MATRIX_ROW_START_CHARPOS (row) == PT
11903 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
11904 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
11905 row > w->current_matrix->rows
11906 && (row-1)->ends_in_newline_from_string_p))))
11907 && (row->y > top_scroll_margin
11908 || CHARPOS (startp) == BEGV))
11909 {
11910 xassert (row->enabled_p);
11911 --row;
11912 }
11913
11914 /* Consider the following case: Window starts at BEGV,
11915 there is invisible, intangible text at BEGV, so that
11916 display starts at some point START > BEGV. It can
11917 happen that we are called with PT somewhere between
11918 BEGV and START. Try to handle that case. */
11919 if (row < w->current_matrix->rows
11920 || row->mode_line_p)
11921 {
11922 row = w->current_matrix->rows;
11923 if (row->mode_line_p)
11924 ++row;
11925 }
11926
11927 /* Due to newlines in overlay strings, we may have to
11928 skip forward over overlay strings. */
11929 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11930 && MATRIX_ROW_END_CHARPOS (row) == PT
11931 && !cursor_row_p (w, row))
11932 ++row;
11933
11934 /* If within the scroll margin, scroll. */
11935 if (row->y < top_scroll_margin
11936 && CHARPOS (startp) != BEGV)
11937 scroll_p = 1;
11938 }
11939 else
11940 {
11941 /* Cursor did not move. So don't scroll even if cursor line
11942 is partially visible, as it was so before. */
11943 rc = CURSOR_MOVEMENT_SUCCESS;
11944 }
11945
11946 if (PT < MATRIX_ROW_START_CHARPOS (row)
11947 || PT > MATRIX_ROW_END_CHARPOS (row))
11948 {
11949 /* if PT is not in the glyph row, give up. */
11950 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11951 }
11952 else if (rc != CURSOR_MOVEMENT_SUCCESS
11953 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11954 && make_cursor_line_fully_visible_p)
11955 {
11956 if (PT == MATRIX_ROW_END_CHARPOS (row)
11957 && !row->ends_at_zv_p
11958 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11959 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11960 else if (row->height > window_box_height (w))
11961 {
11962 /* If we end up in a partially visible line, let's
11963 make it fully visible, except when it's taller
11964 than the window, in which case we can't do much
11965 about it. */
11966 *scroll_step = 1;
11967 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11968 }
11969 else
11970 {
11971 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11972 if (!cursor_row_fully_visible_p (w, 0, 1))
11973 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11974 else
11975 rc = CURSOR_MOVEMENT_SUCCESS;
11976 }
11977 }
11978 else if (scroll_p)
11979 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11980 else
11981 {
11982 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11983 rc = CURSOR_MOVEMENT_SUCCESS;
11984 }
11985 }
11986 }
11987
11988 return rc;
11989 }
11990
11991 void
11992 set_vertical_scroll_bar (w)
11993 struct window *w;
11994 {
11995 int start, end, whole;
11996
11997 /* Calculate the start and end positions for the current window.
11998 At some point, it would be nice to choose between scrollbars
11999 which reflect the whole buffer size, with special markers
12000 indicating narrowing, and scrollbars which reflect only the
12001 visible region.
12002
12003 Note that mini-buffers sometimes aren't displaying any text. */
12004 if (!MINI_WINDOW_P (w)
12005 || (w == XWINDOW (minibuf_window)
12006 && NILP (echo_area_buffer[0])))
12007 {
12008 struct buffer *buf = XBUFFER (w->buffer);
12009 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12010 start = marker_position (w->start) - BUF_BEGV (buf);
12011 /* I don't think this is guaranteed to be right. For the
12012 moment, we'll pretend it is. */
12013 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12014
12015 if (end < start)
12016 end = start;
12017 if (whole < (end - start))
12018 whole = end - start;
12019 }
12020 else
12021 start = end = whole = 0;
12022
12023 /* Indicate what this scroll bar ought to be displaying now. */
12024 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12025 }
12026
12027
12028 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12029 selected_window is redisplayed.
12030
12031 We can return without actually redisplaying the window if
12032 fonts_changed_p is nonzero. In that case, redisplay_internal will
12033 retry. */
12034
12035 static void
12036 redisplay_window (window, just_this_one_p)
12037 Lisp_Object window;
12038 int just_this_one_p;
12039 {
12040 struct window *w = XWINDOW (window);
12041 struct frame *f = XFRAME (w->frame);
12042 struct buffer *buffer = XBUFFER (w->buffer);
12043 struct buffer *old = current_buffer;
12044 struct text_pos lpoint, opoint, startp;
12045 int update_mode_line;
12046 int tem;
12047 struct it it;
12048 /* Record it now because it's overwritten. */
12049 int current_matrix_up_to_date_p = 0;
12050 int used_current_matrix_p = 0;
12051 /* This is less strict than current_matrix_up_to_date_p.
12052 It indictes that the buffer contents and narrowing are unchanged. */
12053 int buffer_unchanged_p = 0;
12054 int temp_scroll_step = 0;
12055 int count = SPECPDL_INDEX ();
12056 int rc;
12057 int centering_position = -1;
12058 int last_line_misfit = 0;
12059
12060 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12061 opoint = lpoint;
12062
12063 /* W must be a leaf window here. */
12064 xassert (!NILP (w->buffer));
12065 #if GLYPH_DEBUG
12066 *w->desired_matrix->method = 0;
12067 #endif
12068
12069 specbind (Qinhibit_point_motion_hooks, Qt);
12070
12071 reconsider_clip_changes (w, buffer);
12072
12073 /* Has the mode line to be updated? */
12074 update_mode_line = (!NILP (w->update_mode_line)
12075 || update_mode_lines
12076 || buffer->clip_changed
12077 || buffer->prevent_redisplay_optimizations_p);
12078
12079 if (MINI_WINDOW_P (w))
12080 {
12081 if (w == XWINDOW (echo_area_window)
12082 && !NILP (echo_area_buffer[0]))
12083 {
12084 if (update_mode_line)
12085 /* We may have to update a tty frame's menu bar or a
12086 tool-bar. Example `M-x C-h C-h C-g'. */
12087 goto finish_menu_bars;
12088 else
12089 /* We've already displayed the echo area glyphs in this window. */
12090 goto finish_scroll_bars;
12091 }
12092 else if ((w != XWINDOW (minibuf_window)
12093 || minibuf_level == 0)
12094 /* When buffer is nonempty, redisplay window normally. */
12095 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12096 /* Quail displays non-mini buffers in minibuffer window.
12097 In that case, redisplay the window normally. */
12098 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12099 {
12100 /* W is a mini-buffer window, but it's not active, so clear
12101 it. */
12102 int yb = window_text_bottom_y (w);
12103 struct glyph_row *row;
12104 int y;
12105
12106 for (y = 0, row = w->desired_matrix->rows;
12107 y < yb;
12108 y += row->height, ++row)
12109 blank_row (w, row, y);
12110 goto finish_scroll_bars;
12111 }
12112
12113 clear_glyph_matrix (w->desired_matrix);
12114 }
12115
12116 /* Otherwise set up data on this window; select its buffer and point
12117 value. */
12118 /* Really select the buffer, for the sake of buffer-local
12119 variables. */
12120 set_buffer_internal_1 (XBUFFER (w->buffer));
12121 SET_TEXT_POS (opoint, PT, PT_BYTE);
12122
12123 current_matrix_up_to_date_p
12124 = (!NILP (w->window_end_valid)
12125 && !current_buffer->clip_changed
12126 && !current_buffer->prevent_redisplay_optimizations_p
12127 && XFASTINT (w->last_modified) >= MODIFF
12128 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12129
12130 buffer_unchanged_p
12131 = (!NILP (w->window_end_valid)
12132 && !current_buffer->clip_changed
12133 && XFASTINT (w->last_modified) >= MODIFF
12134 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12135
12136 /* When windows_or_buffers_changed is non-zero, we can't rely on
12137 the window end being valid, so set it to nil there. */
12138 if (windows_or_buffers_changed)
12139 {
12140 /* If window starts on a continuation line, maybe adjust the
12141 window start in case the window's width changed. */
12142 if (XMARKER (w->start)->buffer == current_buffer)
12143 compute_window_start_on_continuation_line (w);
12144
12145 w->window_end_valid = Qnil;
12146 }
12147
12148 /* Some sanity checks. */
12149 CHECK_WINDOW_END (w);
12150 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12151 abort ();
12152 if (BYTEPOS (opoint) < CHARPOS (opoint))
12153 abort ();
12154
12155 /* If %c is in mode line, update it if needed. */
12156 if (!NILP (w->column_number_displayed)
12157 /* This alternative quickly identifies a common case
12158 where no change is needed. */
12159 && !(PT == XFASTINT (w->last_point)
12160 && XFASTINT (w->last_modified) >= MODIFF
12161 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12162 && (XFASTINT (w->column_number_displayed)
12163 != (int) current_column ())) /* iftc */
12164 update_mode_line = 1;
12165
12166 /* Count number of windows showing the selected buffer. An indirect
12167 buffer counts as its base buffer. */
12168 if (!just_this_one_p)
12169 {
12170 struct buffer *current_base, *window_base;
12171 current_base = current_buffer;
12172 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12173 if (current_base->base_buffer)
12174 current_base = current_base->base_buffer;
12175 if (window_base->base_buffer)
12176 window_base = window_base->base_buffer;
12177 if (current_base == window_base)
12178 buffer_shared++;
12179 }
12180
12181 /* Point refers normally to the selected window. For any other
12182 window, set up appropriate value. */
12183 if (!EQ (window, selected_window))
12184 {
12185 int new_pt = XMARKER (w->pointm)->charpos;
12186 int new_pt_byte = marker_byte_position (w->pointm);
12187 if (new_pt < BEGV)
12188 {
12189 new_pt = BEGV;
12190 new_pt_byte = BEGV_BYTE;
12191 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12192 }
12193 else if (new_pt > (ZV - 1))
12194 {
12195 new_pt = ZV;
12196 new_pt_byte = ZV_BYTE;
12197 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12198 }
12199
12200 /* We don't use SET_PT so that the point-motion hooks don't run. */
12201 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12202 }
12203
12204 /* If any of the character widths specified in the display table
12205 have changed, invalidate the width run cache. It's true that
12206 this may be a bit late to catch such changes, but the rest of
12207 redisplay goes (non-fatally) haywire when the display table is
12208 changed, so why should we worry about doing any better? */
12209 if (current_buffer->width_run_cache)
12210 {
12211 struct Lisp_Char_Table *disptab = buffer_display_table ();
12212
12213 if (! disptab_matches_widthtab (disptab,
12214 XVECTOR (current_buffer->width_table)))
12215 {
12216 invalidate_region_cache (current_buffer,
12217 current_buffer->width_run_cache,
12218 BEG, Z);
12219 recompute_width_table (current_buffer, disptab);
12220 }
12221 }
12222
12223 /* If window-start is screwed up, choose a new one. */
12224 if (XMARKER (w->start)->buffer != current_buffer)
12225 goto recenter;
12226
12227 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12228
12229 /* If someone specified a new starting point but did not insist,
12230 check whether it can be used. */
12231 if (!NILP (w->optional_new_start)
12232 && CHARPOS (startp) >= BEGV
12233 && CHARPOS (startp) <= ZV)
12234 {
12235 w->optional_new_start = Qnil;
12236 start_display (&it, w, startp);
12237 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12238 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12239 if (IT_CHARPOS (it) == PT)
12240 w->force_start = Qt;
12241 /* IT may overshoot PT if text at PT is invisible. */
12242 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12243 w->force_start = Qt;
12244
12245
12246 }
12247
12248 /* Handle case where place to start displaying has been specified,
12249 unless the specified location is outside the accessible range. */
12250 if (!NILP (w->force_start)
12251 || w->frozen_window_start_p)
12252 {
12253 /* We set this later on if we have to adjust point. */
12254 int new_vpos = -1;
12255 int val;
12256
12257 w->force_start = Qnil;
12258 w->vscroll = 0;
12259 w->window_end_valid = Qnil;
12260
12261 /* Forget any recorded base line for line number display. */
12262 if (!buffer_unchanged_p)
12263 w->base_line_number = Qnil;
12264
12265 /* Redisplay the mode line. Select the buffer properly for that.
12266 Also, run the hook window-scroll-functions
12267 because we have scrolled. */
12268 /* Note, we do this after clearing force_start because
12269 if there's an error, it is better to forget about force_start
12270 than to get into an infinite loop calling the hook functions
12271 and having them get more errors. */
12272 if (!update_mode_line
12273 || ! NILP (Vwindow_scroll_functions))
12274 {
12275 update_mode_line = 1;
12276 w->update_mode_line = Qt;
12277 startp = run_window_scroll_functions (window, startp);
12278 }
12279
12280 w->last_modified = make_number (0);
12281 w->last_overlay_modified = make_number (0);
12282 if (CHARPOS (startp) < BEGV)
12283 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12284 else if (CHARPOS (startp) > ZV)
12285 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12286
12287 /* Redisplay, then check if cursor has been set during the
12288 redisplay. Give up if new fonts were loaded. */
12289 val = try_window (window, startp, 1);
12290 if (!val)
12291 {
12292 w->force_start = Qt;
12293 clear_glyph_matrix (w->desired_matrix);
12294 goto need_larger_matrices;
12295 }
12296 /* Point was outside the scroll margins. */
12297 if (val < 0)
12298 new_vpos = window_box_height (w) / 2;
12299
12300 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12301 {
12302 /* If point does not appear, try to move point so it does
12303 appear. The desired matrix has been built above, so we
12304 can use it here. */
12305 new_vpos = window_box_height (w) / 2;
12306 }
12307
12308 if (!cursor_row_fully_visible_p (w, 0, 0))
12309 {
12310 /* Point does appear, but on a line partly visible at end of window.
12311 Move it back to a fully-visible line. */
12312 new_vpos = window_box_height (w);
12313 }
12314
12315 /* If we need to move point for either of the above reasons,
12316 now actually do it. */
12317 if (new_vpos >= 0)
12318 {
12319 struct glyph_row *row;
12320
12321 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12322 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12323 ++row;
12324
12325 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12326 MATRIX_ROW_START_BYTEPOS (row));
12327
12328 if (w != XWINDOW (selected_window))
12329 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12330 else if (current_buffer == old)
12331 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12332
12333 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12334
12335 /* If we are highlighting the region, then we just changed
12336 the region, so redisplay to show it. */
12337 if (!NILP (Vtransient_mark_mode)
12338 && !NILP (current_buffer->mark_active))
12339 {
12340 clear_glyph_matrix (w->desired_matrix);
12341 if (!try_window (window, startp, 0))
12342 goto need_larger_matrices;
12343 }
12344 }
12345
12346 #if GLYPH_DEBUG
12347 debug_method_add (w, "forced window start");
12348 #endif
12349 goto done;
12350 }
12351
12352 /* Handle case where text has not changed, only point, and it has
12353 not moved off the frame, and we are not retrying after hscroll.
12354 (current_matrix_up_to_date_p is nonzero when retrying.) */
12355 if (current_matrix_up_to_date_p
12356 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12357 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12358 {
12359 switch (rc)
12360 {
12361 case CURSOR_MOVEMENT_SUCCESS:
12362 used_current_matrix_p = 1;
12363 goto done;
12364
12365 #if 0 /* try_cursor_movement never returns this value. */
12366 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12367 goto need_larger_matrices;
12368 #endif
12369
12370 case CURSOR_MOVEMENT_MUST_SCROLL:
12371 goto try_to_scroll;
12372
12373 default:
12374 abort ();
12375 }
12376 }
12377 /* If current starting point was originally the beginning of a line
12378 but no longer is, find a new starting point. */
12379 else if (!NILP (w->start_at_line_beg)
12380 && !(CHARPOS (startp) <= BEGV
12381 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12382 {
12383 #if GLYPH_DEBUG
12384 debug_method_add (w, "recenter 1");
12385 #endif
12386 goto recenter;
12387 }
12388
12389 /* Try scrolling with try_window_id. Value is > 0 if update has
12390 been done, it is -1 if we know that the same window start will
12391 not work. It is 0 if unsuccessful for some other reason. */
12392 else if ((tem = try_window_id (w)) != 0)
12393 {
12394 #if GLYPH_DEBUG
12395 debug_method_add (w, "try_window_id %d", tem);
12396 #endif
12397
12398 if (fonts_changed_p)
12399 goto need_larger_matrices;
12400 if (tem > 0)
12401 goto done;
12402
12403 /* Otherwise try_window_id has returned -1 which means that we
12404 don't want the alternative below this comment to execute. */
12405 }
12406 else if (CHARPOS (startp) >= BEGV
12407 && CHARPOS (startp) <= ZV
12408 && PT >= CHARPOS (startp)
12409 && (CHARPOS (startp) < ZV
12410 /* Avoid starting at end of buffer. */
12411 || CHARPOS (startp) == BEGV
12412 || (XFASTINT (w->last_modified) >= MODIFF
12413 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12414 {
12415 #if GLYPH_DEBUG
12416 debug_method_add (w, "same window start");
12417 #endif
12418
12419 /* Try to redisplay starting at same place as before.
12420 If point has not moved off frame, accept the results. */
12421 if (!current_matrix_up_to_date_p
12422 /* Don't use try_window_reusing_current_matrix in this case
12423 because a window scroll function can have changed the
12424 buffer. */
12425 || !NILP (Vwindow_scroll_functions)
12426 || MINI_WINDOW_P (w)
12427 || !(used_current_matrix_p
12428 = try_window_reusing_current_matrix (w)))
12429 {
12430 IF_DEBUG (debug_method_add (w, "1"));
12431 if (try_window (window, startp, 1) < 0)
12432 /* -1 means we need to scroll.
12433 0 means we need new matrices, but fonts_changed_p
12434 is set in that case, so we will detect it below. */
12435 goto try_to_scroll;
12436 }
12437
12438 if (fonts_changed_p)
12439 goto need_larger_matrices;
12440
12441 if (w->cursor.vpos >= 0)
12442 {
12443 if (!just_this_one_p
12444 || current_buffer->clip_changed
12445 || BEG_UNCHANGED < CHARPOS (startp))
12446 /* Forget any recorded base line for line number display. */
12447 w->base_line_number = Qnil;
12448
12449 if (!cursor_row_fully_visible_p (w, 1, 0))
12450 {
12451 clear_glyph_matrix (w->desired_matrix);
12452 last_line_misfit = 1;
12453 }
12454 /* Drop through and scroll. */
12455 else
12456 goto done;
12457 }
12458 else
12459 clear_glyph_matrix (w->desired_matrix);
12460 }
12461
12462 try_to_scroll:
12463
12464 w->last_modified = make_number (0);
12465 w->last_overlay_modified = make_number (0);
12466
12467 /* Redisplay the mode line. Select the buffer properly for that. */
12468 if (!update_mode_line)
12469 {
12470 update_mode_line = 1;
12471 w->update_mode_line = Qt;
12472 }
12473
12474 /* Try to scroll by specified few lines. */
12475 if ((scroll_conservatively
12476 || scroll_step
12477 || temp_scroll_step
12478 || NUMBERP (current_buffer->scroll_up_aggressively)
12479 || NUMBERP (current_buffer->scroll_down_aggressively))
12480 && !current_buffer->clip_changed
12481 && CHARPOS (startp) >= BEGV
12482 && CHARPOS (startp) <= ZV)
12483 {
12484 /* The function returns -1 if new fonts were loaded, 1 if
12485 successful, 0 if not successful. */
12486 int rc = try_scrolling (window, just_this_one_p,
12487 scroll_conservatively,
12488 scroll_step,
12489 temp_scroll_step, last_line_misfit);
12490 switch (rc)
12491 {
12492 case SCROLLING_SUCCESS:
12493 goto done;
12494
12495 case SCROLLING_NEED_LARGER_MATRICES:
12496 goto need_larger_matrices;
12497
12498 case SCROLLING_FAILED:
12499 break;
12500
12501 default:
12502 abort ();
12503 }
12504 }
12505
12506 /* Finally, just choose place to start which centers point */
12507
12508 recenter:
12509 if (centering_position < 0)
12510 centering_position = window_box_height (w) / 2;
12511
12512 #if GLYPH_DEBUG
12513 debug_method_add (w, "recenter");
12514 #endif
12515
12516 /* w->vscroll = 0; */
12517
12518 /* Forget any previously recorded base line for line number display. */
12519 if (!buffer_unchanged_p)
12520 w->base_line_number = Qnil;
12521
12522 /* Move backward half the height of the window. */
12523 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12524 it.current_y = it.last_visible_y;
12525 move_it_vertically_backward (&it, centering_position);
12526 xassert (IT_CHARPOS (it) >= BEGV);
12527
12528 /* The function move_it_vertically_backward may move over more
12529 than the specified y-distance. If it->w is small, e.g. a
12530 mini-buffer window, we may end up in front of the window's
12531 display area. Start displaying at the start of the line
12532 containing PT in this case. */
12533 if (it.current_y <= 0)
12534 {
12535 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12536 move_it_vertically_backward (&it, 0);
12537 #if 0
12538 /* I think this assert is bogus if buffer contains
12539 invisible text or images. KFS. */
12540 xassert (IT_CHARPOS (it) <= PT);
12541 #endif
12542 it.current_y = 0;
12543 }
12544
12545 it.current_x = it.hpos = 0;
12546
12547 /* Set startp here explicitly in case that helps avoid an infinite loop
12548 in case the window-scroll-functions functions get errors. */
12549 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12550
12551 /* Run scroll hooks. */
12552 startp = run_window_scroll_functions (window, it.current.pos);
12553
12554 /* Redisplay the window. */
12555 if (!current_matrix_up_to_date_p
12556 || windows_or_buffers_changed
12557 || cursor_type_changed
12558 /* Don't use try_window_reusing_current_matrix in this case
12559 because it can have changed the buffer. */
12560 || !NILP (Vwindow_scroll_functions)
12561 || !just_this_one_p
12562 || MINI_WINDOW_P (w)
12563 || !(used_current_matrix_p
12564 = try_window_reusing_current_matrix (w)))
12565 try_window (window, startp, 0);
12566
12567 /* If new fonts have been loaded (due to fontsets), give up. We
12568 have to start a new redisplay since we need to re-adjust glyph
12569 matrices. */
12570 if (fonts_changed_p)
12571 goto need_larger_matrices;
12572
12573 /* If cursor did not appear assume that the middle of the window is
12574 in the first line of the window. Do it again with the next line.
12575 (Imagine a window of height 100, displaying two lines of height
12576 60. Moving back 50 from it->last_visible_y will end in the first
12577 line.) */
12578 if (w->cursor.vpos < 0)
12579 {
12580 if (!NILP (w->window_end_valid)
12581 && PT >= Z - XFASTINT (w->window_end_pos))
12582 {
12583 clear_glyph_matrix (w->desired_matrix);
12584 move_it_by_lines (&it, 1, 0);
12585 try_window (window, it.current.pos, 0);
12586 }
12587 else if (PT < IT_CHARPOS (it))
12588 {
12589 clear_glyph_matrix (w->desired_matrix);
12590 move_it_by_lines (&it, -1, 0);
12591 try_window (window, it.current.pos, 0);
12592 }
12593 else
12594 {
12595 /* Not much we can do about it. */
12596 }
12597 }
12598
12599 /* Consider the following case: Window starts at BEGV, there is
12600 invisible, intangible text at BEGV, so that display starts at
12601 some point START > BEGV. It can happen that we are called with
12602 PT somewhere between BEGV and START. Try to handle that case. */
12603 if (w->cursor.vpos < 0)
12604 {
12605 struct glyph_row *row = w->current_matrix->rows;
12606 if (row->mode_line_p)
12607 ++row;
12608 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12609 }
12610
12611 if (!cursor_row_fully_visible_p (w, 0, 0))
12612 {
12613 /* If vscroll is enabled, disable it and try again. */
12614 if (w->vscroll)
12615 {
12616 w->vscroll = 0;
12617 clear_glyph_matrix (w->desired_matrix);
12618 goto recenter;
12619 }
12620
12621 /* If centering point failed to make the whole line visible,
12622 put point at the top instead. That has to make the whole line
12623 visible, if it can be done. */
12624 if (centering_position == 0)
12625 goto done;
12626
12627 clear_glyph_matrix (w->desired_matrix);
12628 centering_position = 0;
12629 goto recenter;
12630 }
12631
12632 done:
12633
12634 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12635 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12636 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12637 ? Qt : Qnil);
12638
12639 /* Display the mode line, if we must. */
12640 if ((update_mode_line
12641 /* If window not full width, must redo its mode line
12642 if (a) the window to its side is being redone and
12643 (b) we do a frame-based redisplay. This is a consequence
12644 of how inverted lines are drawn in frame-based redisplay. */
12645 || (!just_this_one_p
12646 && !FRAME_WINDOW_P (f)
12647 && !WINDOW_FULL_WIDTH_P (w))
12648 /* Line number to display. */
12649 || INTEGERP (w->base_line_pos)
12650 /* Column number is displayed and different from the one displayed. */
12651 || (!NILP (w->column_number_displayed)
12652 && (XFASTINT (w->column_number_displayed)
12653 != (int) current_column ()))) /* iftc */
12654 /* This means that the window has a mode line. */
12655 && (WINDOW_WANTS_MODELINE_P (w)
12656 || WINDOW_WANTS_HEADER_LINE_P (w)))
12657 {
12658 display_mode_lines (w);
12659
12660 /* If mode line height has changed, arrange for a thorough
12661 immediate redisplay using the correct mode line height. */
12662 if (WINDOW_WANTS_MODELINE_P (w)
12663 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12664 {
12665 fonts_changed_p = 1;
12666 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12667 = DESIRED_MODE_LINE_HEIGHT (w);
12668 }
12669
12670 /* If top line height has changed, arrange for a thorough
12671 immediate redisplay using the correct mode line height. */
12672 if (WINDOW_WANTS_HEADER_LINE_P (w)
12673 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12674 {
12675 fonts_changed_p = 1;
12676 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12677 = DESIRED_HEADER_LINE_HEIGHT (w);
12678 }
12679
12680 if (fonts_changed_p)
12681 goto need_larger_matrices;
12682 }
12683
12684 if (!line_number_displayed
12685 && !BUFFERP (w->base_line_pos))
12686 {
12687 w->base_line_pos = Qnil;
12688 w->base_line_number = Qnil;
12689 }
12690
12691 finish_menu_bars:
12692
12693 /* When we reach a frame's selected window, redo the frame's menu bar. */
12694 if (update_mode_line
12695 && EQ (FRAME_SELECTED_WINDOW (f), window))
12696 {
12697 int redisplay_menu_p = 0;
12698 int redisplay_tool_bar_p = 0;
12699
12700 if (FRAME_WINDOW_P (f))
12701 {
12702 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12703 || defined (USE_GTK)
12704 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12705 #else
12706 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12707 #endif
12708 }
12709 else
12710 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12711
12712 if (redisplay_menu_p)
12713 display_menu_bar (w);
12714
12715 #ifdef HAVE_WINDOW_SYSTEM
12716 #ifdef USE_GTK
12717 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12718 #else
12719 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12720 && (FRAME_TOOL_BAR_LINES (f) > 0
12721 || auto_resize_tool_bars_p);
12722
12723 #endif
12724
12725 if (redisplay_tool_bar_p)
12726 redisplay_tool_bar (f);
12727 #endif
12728 }
12729
12730 #ifdef HAVE_WINDOW_SYSTEM
12731 if (FRAME_WINDOW_P (f)
12732 && update_window_fringes (w, (just_this_one_p
12733 || (!used_current_matrix_p && !overlay_arrow_seen)
12734 || w->pseudo_window_p)))
12735 {
12736 update_begin (f);
12737 BLOCK_INPUT;
12738 if (draw_window_fringes (w, 1))
12739 x_draw_vertical_border (w);
12740 UNBLOCK_INPUT;
12741 update_end (f);
12742 }
12743 #endif /* HAVE_WINDOW_SYSTEM */
12744
12745 /* We go to this label, with fonts_changed_p nonzero,
12746 if it is necessary to try again using larger glyph matrices.
12747 We have to redeem the scroll bar even in this case,
12748 because the loop in redisplay_internal expects that. */
12749 need_larger_matrices:
12750 ;
12751 finish_scroll_bars:
12752
12753 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12754 {
12755 /* Set the thumb's position and size. */
12756 set_vertical_scroll_bar (w);
12757
12758 /* Note that we actually used the scroll bar attached to this
12759 window, so it shouldn't be deleted at the end of redisplay. */
12760 redeem_scroll_bar_hook (w);
12761 }
12762
12763 /* Restore current_buffer and value of point in it. */
12764 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12765 set_buffer_internal_1 (old);
12766 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12767
12768 unbind_to (count, Qnil);
12769 }
12770
12771
12772 /* Build the complete desired matrix of WINDOW with a window start
12773 buffer position POS.
12774
12775 Value is 1 if successful. It is zero if fonts were loaded during
12776 redisplay which makes re-adjusting glyph matrices necessary, and -1
12777 if point would appear in the scroll margins.
12778 (We check that only if CHECK_MARGINS is nonzero. */
12779
12780 int
12781 try_window (window, pos, check_margins)
12782 Lisp_Object window;
12783 struct text_pos pos;
12784 int check_margins;
12785 {
12786 struct window *w = XWINDOW (window);
12787 struct it it;
12788 struct glyph_row *last_text_row = NULL;
12789
12790 /* Make POS the new window start. */
12791 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12792
12793 /* Mark cursor position as unknown. No overlay arrow seen. */
12794 w->cursor.vpos = -1;
12795 overlay_arrow_seen = 0;
12796
12797 /* Initialize iterator and info to start at POS. */
12798 start_display (&it, w, pos);
12799
12800 /* Display all lines of W. */
12801 while (it.current_y < it.last_visible_y)
12802 {
12803 if (display_line (&it))
12804 last_text_row = it.glyph_row - 1;
12805 if (fonts_changed_p)
12806 return 0;
12807 }
12808
12809 /* Don't let the cursor end in the scroll margins. */
12810 if (check_margins
12811 && !MINI_WINDOW_P (w))
12812 {
12813 int this_scroll_margin, cursor_height;
12814
12815 this_scroll_margin = max (0, scroll_margin);
12816 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12817 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
12818 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12819
12820 if ((w->cursor.y < this_scroll_margin
12821 && CHARPOS (pos) > BEGV)
12822 /* rms: considering make_cursor_line_fully_visible_p here
12823 seems to give wrong results. We don't want to recenter
12824 when the last line is partly visible, we want to allow
12825 that case to be handled in the usual way. */
12826 || (w->cursor.y + 1) > it.last_visible_y)
12827 {
12828 w->cursor.vpos = -1;
12829 clear_glyph_matrix (w->desired_matrix);
12830 return -1;
12831 }
12832 }
12833
12834 /* If bottom moved off end of frame, change mode line percentage. */
12835 if (XFASTINT (w->window_end_pos) <= 0
12836 && Z != IT_CHARPOS (it))
12837 w->update_mode_line = Qt;
12838
12839 /* Set window_end_pos to the offset of the last character displayed
12840 on the window from the end of current_buffer. Set
12841 window_end_vpos to its row number. */
12842 if (last_text_row)
12843 {
12844 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12845 w->window_end_bytepos
12846 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12847 w->window_end_pos
12848 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12849 w->window_end_vpos
12850 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12851 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12852 ->displays_text_p);
12853 }
12854 else
12855 {
12856 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12857 w->window_end_pos = make_number (Z - ZV);
12858 w->window_end_vpos = make_number (0);
12859 }
12860
12861 /* But that is not valid info until redisplay finishes. */
12862 w->window_end_valid = Qnil;
12863 return 1;
12864 }
12865
12866
12867 \f
12868 /************************************************************************
12869 Window redisplay reusing current matrix when buffer has not changed
12870 ************************************************************************/
12871
12872 /* Try redisplay of window W showing an unchanged buffer with a
12873 different window start than the last time it was displayed by
12874 reusing its current matrix. Value is non-zero if successful.
12875 W->start is the new window start. */
12876
12877 static int
12878 try_window_reusing_current_matrix (w)
12879 struct window *w;
12880 {
12881 struct frame *f = XFRAME (w->frame);
12882 struct glyph_row *row, *bottom_row;
12883 struct it it;
12884 struct run run;
12885 struct text_pos start, new_start;
12886 int nrows_scrolled, i;
12887 struct glyph_row *last_text_row;
12888 struct glyph_row *last_reused_text_row;
12889 struct glyph_row *start_row;
12890 int start_vpos, min_y, max_y;
12891
12892 #if GLYPH_DEBUG
12893 if (inhibit_try_window_reusing)
12894 return 0;
12895 #endif
12896
12897 if (/* This function doesn't handle terminal frames. */
12898 !FRAME_WINDOW_P (f)
12899 /* Don't try to reuse the display if windows have been split
12900 or such. */
12901 || windows_or_buffers_changed
12902 || cursor_type_changed)
12903 return 0;
12904
12905 /* Can't do this if region may have changed. */
12906 if ((!NILP (Vtransient_mark_mode)
12907 && !NILP (current_buffer->mark_active))
12908 || !NILP (w->region_showing)
12909 || !NILP (Vshow_trailing_whitespace))
12910 return 0;
12911
12912 /* If top-line visibility has changed, give up. */
12913 if (WINDOW_WANTS_HEADER_LINE_P (w)
12914 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12915 return 0;
12916
12917 /* Give up if old or new display is scrolled vertically. We could
12918 make this function handle this, but right now it doesn't. */
12919 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12920 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12921 return 0;
12922
12923 /* The variable new_start now holds the new window start. The old
12924 start `start' can be determined from the current matrix. */
12925 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12926 start = start_row->start.pos;
12927 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12928
12929 /* Clear the desired matrix for the display below. */
12930 clear_glyph_matrix (w->desired_matrix);
12931
12932 if (CHARPOS (new_start) <= CHARPOS (start))
12933 {
12934 int first_row_y;
12935
12936 /* Don't use this method if the display starts with an ellipsis
12937 displayed for invisible text. It's not easy to handle that case
12938 below, and it's certainly not worth the effort since this is
12939 not a frequent case. */
12940 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12941 return 0;
12942
12943 IF_DEBUG (debug_method_add (w, "twu1"));
12944
12945 /* Display up to a row that can be reused. The variable
12946 last_text_row is set to the last row displayed that displays
12947 text. Note that it.vpos == 0 if or if not there is a
12948 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12949 start_display (&it, w, new_start);
12950 first_row_y = it.current_y;
12951 w->cursor.vpos = -1;
12952 last_text_row = last_reused_text_row = NULL;
12953
12954 while (it.current_y < it.last_visible_y
12955 && !fonts_changed_p)
12956 {
12957 /* If we have reached into the characters in the START row,
12958 that means the line boundaries have changed. So we
12959 can't start copying with the row START. Maybe it will
12960 work to start copying with the following row. */
12961 while (IT_CHARPOS (it) > CHARPOS (start))
12962 {
12963 /* Advance to the next row as the "start". */
12964 start_row++;
12965 start = start_row->start.pos;
12966 /* If there are no more rows to try, or just one, give up. */
12967 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12968 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12969 || CHARPOS (start) == ZV)
12970 {
12971 clear_glyph_matrix (w->desired_matrix);
12972 return 0;
12973 }
12974
12975 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12976 }
12977 /* If we have reached alignment,
12978 we can copy the rest of the rows. */
12979 if (IT_CHARPOS (it) == CHARPOS (start))
12980 break;
12981
12982 if (display_line (&it))
12983 last_text_row = it.glyph_row - 1;
12984 }
12985
12986 /* A value of current_y < last_visible_y means that we stopped
12987 at the previous window start, which in turn means that we
12988 have at least one reusable row. */
12989 if (it.current_y < it.last_visible_y)
12990 {
12991 /* IT.vpos always starts from 0; it counts text lines. */
12992 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12993
12994 /* Find PT if not already found in the lines displayed. */
12995 if (w->cursor.vpos < 0)
12996 {
12997 int dy = it.current_y - start_row->y;
12998
12999 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13000 row = row_containing_pos (w, PT, row, NULL, dy);
13001 if (row)
13002 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13003 dy, nrows_scrolled);
13004 else
13005 {
13006 clear_glyph_matrix (w->desired_matrix);
13007 return 0;
13008 }
13009 }
13010
13011 /* Scroll the display. Do it before the current matrix is
13012 changed. The problem here is that update has not yet
13013 run, i.e. part of the current matrix is not up to date.
13014 scroll_run_hook will clear the cursor, and use the
13015 current matrix to get the height of the row the cursor is
13016 in. */
13017 run.current_y = start_row->y;
13018 run.desired_y = it.current_y;
13019 run.height = it.last_visible_y - it.current_y;
13020
13021 if (run.height > 0 && run.current_y != run.desired_y)
13022 {
13023 update_begin (f);
13024 rif->update_window_begin_hook (w);
13025 rif->clear_window_mouse_face (w);
13026 rif->scroll_run_hook (w, &run);
13027 rif->update_window_end_hook (w, 0, 0);
13028 update_end (f);
13029 }
13030
13031 /* Shift current matrix down by nrows_scrolled lines. */
13032 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13033 rotate_matrix (w->current_matrix,
13034 start_vpos,
13035 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13036 nrows_scrolled);
13037
13038 /* Disable lines that must be updated. */
13039 for (i = 0; i < it.vpos; ++i)
13040 (start_row + i)->enabled_p = 0;
13041
13042 /* Re-compute Y positions. */
13043 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13044 max_y = it.last_visible_y;
13045 for (row = start_row + nrows_scrolled;
13046 row < bottom_row;
13047 ++row)
13048 {
13049 row->y = it.current_y;
13050 row->visible_height = row->height;
13051
13052 if (row->y < min_y)
13053 row->visible_height -= min_y - row->y;
13054 if (row->y + row->height > max_y)
13055 row->visible_height -= row->y + row->height - max_y;
13056 row->redraw_fringe_bitmaps_p = 1;
13057
13058 it.current_y += row->height;
13059
13060 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13061 last_reused_text_row = row;
13062 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13063 break;
13064 }
13065
13066 /* Disable lines in the current matrix which are now
13067 below the window. */
13068 for (++row; row < bottom_row; ++row)
13069 row->enabled_p = 0;
13070 }
13071
13072 /* Update window_end_pos etc.; last_reused_text_row is the last
13073 reused row from the current matrix containing text, if any.
13074 The value of last_text_row is the last displayed line
13075 containing text. */
13076 if (last_reused_text_row)
13077 {
13078 w->window_end_bytepos
13079 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13080 w->window_end_pos
13081 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13082 w->window_end_vpos
13083 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13084 w->current_matrix));
13085 }
13086 else if (last_text_row)
13087 {
13088 w->window_end_bytepos
13089 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13090 w->window_end_pos
13091 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13092 w->window_end_vpos
13093 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13094 }
13095 else
13096 {
13097 /* This window must be completely empty. */
13098 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13099 w->window_end_pos = make_number (Z - ZV);
13100 w->window_end_vpos = make_number (0);
13101 }
13102 w->window_end_valid = Qnil;
13103
13104 /* Update hint: don't try scrolling again in update_window. */
13105 w->desired_matrix->no_scrolling_p = 1;
13106
13107 #if GLYPH_DEBUG
13108 debug_method_add (w, "try_window_reusing_current_matrix 1");
13109 #endif
13110 return 1;
13111 }
13112 else if (CHARPOS (new_start) > CHARPOS (start))
13113 {
13114 struct glyph_row *pt_row, *row;
13115 struct glyph_row *first_reusable_row;
13116 struct glyph_row *first_row_to_display;
13117 int dy;
13118 int yb = window_text_bottom_y (w);
13119
13120 /* Find the row starting at new_start, if there is one. Don't
13121 reuse a partially visible line at the end. */
13122 first_reusable_row = start_row;
13123 while (first_reusable_row->enabled_p
13124 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13125 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13126 < CHARPOS (new_start)))
13127 ++first_reusable_row;
13128
13129 /* Give up if there is no row to reuse. */
13130 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13131 || !first_reusable_row->enabled_p
13132 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13133 != CHARPOS (new_start)))
13134 return 0;
13135
13136 /* We can reuse fully visible rows beginning with
13137 first_reusable_row to the end of the window. Set
13138 first_row_to_display to the first row that cannot be reused.
13139 Set pt_row to the row containing point, if there is any. */
13140 pt_row = NULL;
13141 for (first_row_to_display = first_reusable_row;
13142 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13143 ++first_row_to_display)
13144 {
13145 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13146 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13147 pt_row = first_row_to_display;
13148 }
13149
13150 /* Start displaying at the start of first_row_to_display. */
13151 xassert (first_row_to_display->y < yb);
13152 init_to_row_start (&it, w, first_row_to_display);
13153
13154 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13155 - start_vpos);
13156 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13157 - nrows_scrolled);
13158 it.current_y = (first_row_to_display->y - first_reusable_row->y
13159 + WINDOW_HEADER_LINE_HEIGHT (w));
13160
13161 /* Display lines beginning with first_row_to_display in the
13162 desired matrix. Set last_text_row to the last row displayed
13163 that displays text. */
13164 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13165 if (pt_row == NULL)
13166 w->cursor.vpos = -1;
13167 last_text_row = NULL;
13168 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13169 if (display_line (&it))
13170 last_text_row = it.glyph_row - 1;
13171
13172 /* Give up If point isn't in a row displayed or reused. */
13173 if (w->cursor.vpos < 0)
13174 {
13175 clear_glyph_matrix (w->desired_matrix);
13176 return 0;
13177 }
13178
13179 /* If point is in a reused row, adjust y and vpos of the cursor
13180 position. */
13181 if (pt_row)
13182 {
13183 w->cursor.vpos -= nrows_scrolled;
13184 w->cursor.y -= first_reusable_row->y - start_row->y;
13185 }
13186
13187 /* Scroll the display. */
13188 run.current_y = first_reusable_row->y;
13189 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13190 run.height = it.last_visible_y - run.current_y;
13191 dy = run.current_y - run.desired_y;
13192
13193 if (run.height)
13194 {
13195 update_begin (f);
13196 rif->update_window_begin_hook (w);
13197 rif->clear_window_mouse_face (w);
13198 rif->scroll_run_hook (w, &run);
13199 rif->update_window_end_hook (w, 0, 0);
13200 update_end (f);
13201 }
13202
13203 /* Adjust Y positions of reused rows. */
13204 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13205 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13206 max_y = it.last_visible_y;
13207 for (row = first_reusable_row; row < first_row_to_display; ++row)
13208 {
13209 row->y -= dy;
13210 row->visible_height = row->height;
13211 if (row->y < min_y)
13212 row->visible_height -= min_y - row->y;
13213 if (row->y + row->height > max_y)
13214 row->visible_height -= row->y + row->height - max_y;
13215 row->redraw_fringe_bitmaps_p = 1;
13216 }
13217
13218 /* Scroll the current matrix. */
13219 xassert (nrows_scrolled > 0);
13220 rotate_matrix (w->current_matrix,
13221 start_vpos,
13222 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13223 -nrows_scrolled);
13224
13225 /* Disable rows not reused. */
13226 for (row -= nrows_scrolled; row < bottom_row; ++row)
13227 row->enabled_p = 0;
13228
13229 /* Point may have moved to a different line, so we cannot assume that
13230 the previous cursor position is valid; locate the correct row. */
13231 if (pt_row)
13232 {
13233 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13234 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13235 row++)
13236 {
13237 w->cursor.vpos++;
13238 w->cursor.y = row->y;
13239 }
13240 if (row < bottom_row)
13241 {
13242 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13243 while (glyph->charpos < PT)
13244 {
13245 w->cursor.hpos++;
13246 w->cursor.x += glyph->pixel_width;
13247 glyph++;
13248 }
13249 }
13250 }
13251
13252 /* Adjust window end. A null value of last_text_row means that
13253 the window end is in reused rows which in turn means that
13254 only its vpos can have changed. */
13255 if (last_text_row)
13256 {
13257 w->window_end_bytepos
13258 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13259 w->window_end_pos
13260 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13261 w->window_end_vpos
13262 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13263 }
13264 else
13265 {
13266 w->window_end_vpos
13267 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13268 }
13269
13270 w->window_end_valid = Qnil;
13271 w->desired_matrix->no_scrolling_p = 1;
13272
13273 #if GLYPH_DEBUG
13274 debug_method_add (w, "try_window_reusing_current_matrix 2");
13275 #endif
13276 return 1;
13277 }
13278
13279 return 0;
13280 }
13281
13282
13283 \f
13284 /************************************************************************
13285 Window redisplay reusing current matrix when buffer has changed
13286 ************************************************************************/
13287
13288 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13289 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13290 int *, int *));
13291 static struct glyph_row *
13292 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13293 struct glyph_row *));
13294
13295
13296 /* Return the last row in MATRIX displaying text. If row START is
13297 non-null, start searching with that row. IT gives the dimensions
13298 of the display. Value is null if matrix is empty; otherwise it is
13299 a pointer to the row found. */
13300
13301 static struct glyph_row *
13302 find_last_row_displaying_text (matrix, it, start)
13303 struct glyph_matrix *matrix;
13304 struct it *it;
13305 struct glyph_row *start;
13306 {
13307 struct glyph_row *row, *row_found;
13308
13309 /* Set row_found to the last row in IT->w's current matrix
13310 displaying text. The loop looks funny but think of partially
13311 visible lines. */
13312 row_found = NULL;
13313 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13314 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13315 {
13316 xassert (row->enabled_p);
13317 row_found = row;
13318 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13319 break;
13320 ++row;
13321 }
13322
13323 return row_found;
13324 }
13325
13326
13327 /* Return the last row in the current matrix of W that is not affected
13328 by changes at the start of current_buffer that occurred since W's
13329 current matrix was built. Value is null if no such row exists.
13330
13331 BEG_UNCHANGED us the number of characters unchanged at the start of
13332 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13333 first changed character in current_buffer. Characters at positions <
13334 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13335 when the current matrix was built. */
13336
13337 static struct glyph_row *
13338 find_last_unchanged_at_beg_row (w)
13339 struct window *w;
13340 {
13341 int first_changed_pos = BEG + BEG_UNCHANGED;
13342 struct glyph_row *row;
13343 struct glyph_row *row_found = NULL;
13344 int yb = window_text_bottom_y (w);
13345
13346 /* Find the last row displaying unchanged text. */
13347 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13348 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13349 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13350 {
13351 if (/* If row ends before first_changed_pos, it is unchanged,
13352 except in some case. */
13353 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13354 /* When row ends in ZV and we write at ZV it is not
13355 unchanged. */
13356 && !row->ends_at_zv_p
13357 /* When first_changed_pos is the end of a continued line,
13358 row is not unchanged because it may be no longer
13359 continued. */
13360 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13361 && (row->continued_p
13362 || row->exact_window_width_line_p)))
13363 row_found = row;
13364
13365 /* Stop if last visible row. */
13366 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13367 break;
13368
13369 ++row;
13370 }
13371
13372 return row_found;
13373 }
13374
13375
13376 /* Find the first glyph row in the current matrix of W that is not
13377 affected by changes at the end of current_buffer since the
13378 time W's current matrix was built.
13379
13380 Return in *DELTA the number of chars by which buffer positions in
13381 unchanged text at the end of current_buffer must be adjusted.
13382
13383 Return in *DELTA_BYTES the corresponding number of bytes.
13384
13385 Value is null if no such row exists, i.e. all rows are affected by
13386 changes. */
13387
13388 static struct glyph_row *
13389 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13390 struct window *w;
13391 int *delta, *delta_bytes;
13392 {
13393 struct glyph_row *row;
13394 struct glyph_row *row_found = NULL;
13395
13396 *delta = *delta_bytes = 0;
13397
13398 /* Display must not have been paused, otherwise the current matrix
13399 is not up to date. */
13400 if (NILP (w->window_end_valid))
13401 abort ();
13402
13403 /* A value of window_end_pos >= END_UNCHANGED means that the window
13404 end is in the range of changed text. If so, there is no
13405 unchanged row at the end of W's current matrix. */
13406 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13407 return NULL;
13408
13409 /* Set row to the last row in W's current matrix displaying text. */
13410 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13411
13412 /* If matrix is entirely empty, no unchanged row exists. */
13413 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13414 {
13415 /* The value of row is the last glyph row in the matrix having a
13416 meaningful buffer position in it. The end position of row
13417 corresponds to window_end_pos. This allows us to translate
13418 buffer positions in the current matrix to current buffer
13419 positions for characters not in changed text. */
13420 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13421 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13422 int last_unchanged_pos, last_unchanged_pos_old;
13423 struct glyph_row *first_text_row
13424 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13425
13426 *delta = Z - Z_old;
13427 *delta_bytes = Z_BYTE - Z_BYTE_old;
13428
13429 /* Set last_unchanged_pos to the buffer position of the last
13430 character in the buffer that has not been changed. Z is the
13431 index + 1 of the last character in current_buffer, i.e. by
13432 subtracting END_UNCHANGED we get the index of the last
13433 unchanged character, and we have to add BEG to get its buffer
13434 position. */
13435 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13436 last_unchanged_pos_old = last_unchanged_pos - *delta;
13437
13438 /* Search backward from ROW for a row displaying a line that
13439 starts at a minimum position >= last_unchanged_pos_old. */
13440 for (; row > first_text_row; --row)
13441 {
13442 /* This used to abort, but it can happen.
13443 It is ok to just stop the search instead here. KFS. */
13444 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13445 break;
13446
13447 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13448 row_found = row;
13449 }
13450 }
13451
13452 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13453 abort ();
13454
13455 return row_found;
13456 }
13457
13458
13459 /* Make sure that glyph rows in the current matrix of window W
13460 reference the same glyph memory as corresponding rows in the
13461 frame's frame matrix. This function is called after scrolling W's
13462 current matrix on a terminal frame in try_window_id and
13463 try_window_reusing_current_matrix. */
13464
13465 static void
13466 sync_frame_with_window_matrix_rows (w)
13467 struct window *w;
13468 {
13469 struct frame *f = XFRAME (w->frame);
13470 struct glyph_row *window_row, *window_row_end, *frame_row;
13471
13472 /* Preconditions: W must be a leaf window and full-width. Its frame
13473 must have a frame matrix. */
13474 xassert (NILP (w->hchild) && NILP (w->vchild));
13475 xassert (WINDOW_FULL_WIDTH_P (w));
13476 xassert (!FRAME_WINDOW_P (f));
13477
13478 /* If W is a full-width window, glyph pointers in W's current matrix
13479 have, by definition, to be the same as glyph pointers in the
13480 corresponding frame matrix. Note that frame matrices have no
13481 marginal areas (see build_frame_matrix). */
13482 window_row = w->current_matrix->rows;
13483 window_row_end = window_row + w->current_matrix->nrows;
13484 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13485 while (window_row < window_row_end)
13486 {
13487 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13488 struct glyph *end = window_row->glyphs[LAST_AREA];
13489
13490 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13491 frame_row->glyphs[TEXT_AREA] = start;
13492 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13493 frame_row->glyphs[LAST_AREA] = end;
13494
13495 /* Disable frame rows whose corresponding window rows have
13496 been disabled in try_window_id. */
13497 if (!window_row->enabled_p)
13498 frame_row->enabled_p = 0;
13499
13500 ++window_row, ++frame_row;
13501 }
13502 }
13503
13504
13505 /* Find the glyph row in window W containing CHARPOS. Consider all
13506 rows between START and END (not inclusive). END null means search
13507 all rows to the end of the display area of W. Value is the row
13508 containing CHARPOS or null. */
13509
13510 struct glyph_row *
13511 row_containing_pos (w, charpos, start, end, dy)
13512 struct window *w;
13513 int charpos;
13514 struct glyph_row *start, *end;
13515 int dy;
13516 {
13517 struct glyph_row *row = start;
13518 int last_y;
13519
13520 /* If we happen to start on a header-line, skip that. */
13521 if (row->mode_line_p)
13522 ++row;
13523
13524 if ((end && row >= end) || !row->enabled_p)
13525 return NULL;
13526
13527 last_y = window_text_bottom_y (w) - dy;
13528
13529 while (1)
13530 {
13531 /* Give up if we have gone too far. */
13532 if (end && row >= end)
13533 return NULL;
13534 /* This formerly returned if they were equal.
13535 I think that both quantities are of a "last plus one" type;
13536 if so, when they are equal, the row is within the screen. -- rms. */
13537 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13538 return NULL;
13539
13540 /* If it is in this row, return this row. */
13541 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13542 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13543 /* The end position of a row equals the start
13544 position of the next row. If CHARPOS is there, we
13545 would rather display it in the next line, except
13546 when this line ends in ZV. */
13547 && !row->ends_at_zv_p
13548 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13549 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13550 return row;
13551 ++row;
13552 }
13553 }
13554
13555
13556 /* Try to redisplay window W by reusing its existing display. W's
13557 current matrix must be up to date when this function is called,
13558 i.e. window_end_valid must not be nil.
13559
13560 Value is
13561
13562 1 if display has been updated
13563 0 if otherwise unsuccessful
13564 -1 if redisplay with same window start is known not to succeed
13565
13566 The following steps are performed:
13567
13568 1. Find the last row in the current matrix of W that is not
13569 affected by changes at the start of current_buffer. If no such row
13570 is found, give up.
13571
13572 2. Find the first row in W's current matrix that is not affected by
13573 changes at the end of current_buffer. Maybe there is no such row.
13574
13575 3. Display lines beginning with the row + 1 found in step 1 to the
13576 row found in step 2 or, if step 2 didn't find a row, to the end of
13577 the window.
13578
13579 4. If cursor is not known to appear on the window, give up.
13580
13581 5. If display stopped at the row found in step 2, scroll the
13582 display and current matrix as needed.
13583
13584 6. Maybe display some lines at the end of W, if we must. This can
13585 happen under various circumstances, like a partially visible line
13586 becoming fully visible, or because newly displayed lines are displayed
13587 in smaller font sizes.
13588
13589 7. Update W's window end information. */
13590
13591 static int
13592 try_window_id (w)
13593 struct window *w;
13594 {
13595 struct frame *f = XFRAME (w->frame);
13596 struct glyph_matrix *current_matrix = w->current_matrix;
13597 struct glyph_matrix *desired_matrix = w->desired_matrix;
13598 struct glyph_row *last_unchanged_at_beg_row;
13599 struct glyph_row *first_unchanged_at_end_row;
13600 struct glyph_row *row;
13601 struct glyph_row *bottom_row;
13602 int bottom_vpos;
13603 struct it it;
13604 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13605 struct text_pos start_pos;
13606 struct run run;
13607 int first_unchanged_at_end_vpos = 0;
13608 struct glyph_row *last_text_row, *last_text_row_at_end;
13609 struct text_pos start;
13610 int first_changed_charpos, last_changed_charpos;
13611
13612 #if GLYPH_DEBUG
13613 if (inhibit_try_window_id)
13614 return 0;
13615 #endif
13616
13617 /* This is handy for debugging. */
13618 #if 0
13619 #define GIVE_UP(X) \
13620 do { \
13621 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13622 return 0; \
13623 } while (0)
13624 #else
13625 #define GIVE_UP(X) return 0
13626 #endif
13627
13628 SET_TEXT_POS_FROM_MARKER (start, w->start);
13629
13630 /* Don't use this for mini-windows because these can show
13631 messages and mini-buffers, and we don't handle that here. */
13632 if (MINI_WINDOW_P (w))
13633 GIVE_UP (1);
13634
13635 /* This flag is used to prevent redisplay optimizations. */
13636 if (windows_or_buffers_changed || cursor_type_changed)
13637 GIVE_UP (2);
13638
13639 /* Verify that narrowing has not changed.
13640 Also verify that we were not told to prevent redisplay optimizations.
13641 It would be nice to further
13642 reduce the number of cases where this prevents try_window_id. */
13643 if (current_buffer->clip_changed
13644 || current_buffer->prevent_redisplay_optimizations_p)
13645 GIVE_UP (3);
13646
13647 /* Window must either use window-based redisplay or be full width. */
13648 if (!FRAME_WINDOW_P (f)
13649 && (!line_ins_del_ok
13650 || !WINDOW_FULL_WIDTH_P (w)))
13651 GIVE_UP (4);
13652
13653 /* Give up if point is not known NOT to appear in W. */
13654 if (PT < CHARPOS (start))
13655 GIVE_UP (5);
13656
13657 /* Another way to prevent redisplay optimizations. */
13658 if (XFASTINT (w->last_modified) == 0)
13659 GIVE_UP (6);
13660
13661 /* Verify that window is not hscrolled. */
13662 if (XFASTINT (w->hscroll) != 0)
13663 GIVE_UP (7);
13664
13665 /* Verify that display wasn't paused. */
13666 if (NILP (w->window_end_valid))
13667 GIVE_UP (8);
13668
13669 /* Can't use this if highlighting a region because a cursor movement
13670 will do more than just set the cursor. */
13671 if (!NILP (Vtransient_mark_mode)
13672 && !NILP (current_buffer->mark_active))
13673 GIVE_UP (9);
13674
13675 /* Likewise if highlighting trailing whitespace. */
13676 if (!NILP (Vshow_trailing_whitespace))
13677 GIVE_UP (11);
13678
13679 /* Likewise if showing a region. */
13680 if (!NILP (w->region_showing))
13681 GIVE_UP (10);
13682
13683 /* Can use this if overlay arrow position and or string have changed. */
13684 if (overlay_arrows_changed_p ())
13685 GIVE_UP (12);
13686
13687
13688 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13689 only if buffer has really changed. The reason is that the gap is
13690 initially at Z for freshly visited files. The code below would
13691 set end_unchanged to 0 in that case. */
13692 if (MODIFF > SAVE_MODIFF
13693 /* This seems to happen sometimes after saving a buffer. */
13694 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13695 {
13696 if (GPT - BEG < BEG_UNCHANGED)
13697 BEG_UNCHANGED = GPT - BEG;
13698 if (Z - GPT < END_UNCHANGED)
13699 END_UNCHANGED = Z - GPT;
13700 }
13701
13702 /* The position of the first and last character that has been changed. */
13703 first_changed_charpos = BEG + BEG_UNCHANGED;
13704 last_changed_charpos = Z - END_UNCHANGED;
13705
13706 /* If window starts after a line end, and the last change is in
13707 front of that newline, then changes don't affect the display.
13708 This case happens with stealth-fontification. Note that although
13709 the display is unchanged, glyph positions in the matrix have to
13710 be adjusted, of course. */
13711 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13712 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13713 && ((last_changed_charpos < CHARPOS (start)
13714 && CHARPOS (start) == BEGV)
13715 || (last_changed_charpos < CHARPOS (start) - 1
13716 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13717 {
13718 int Z_old, delta, Z_BYTE_old, delta_bytes;
13719 struct glyph_row *r0;
13720
13721 /* Compute how many chars/bytes have been added to or removed
13722 from the buffer. */
13723 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13724 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13725 delta = Z - Z_old;
13726 delta_bytes = Z_BYTE - Z_BYTE_old;
13727
13728 /* Give up if PT is not in the window. Note that it already has
13729 been checked at the start of try_window_id that PT is not in
13730 front of the window start. */
13731 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13732 GIVE_UP (13);
13733
13734 /* If window start is unchanged, we can reuse the whole matrix
13735 as is, after adjusting glyph positions. No need to compute
13736 the window end again, since its offset from Z hasn't changed. */
13737 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13738 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13739 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13740 /* PT must not be in a partially visible line. */
13741 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13742 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13743 {
13744 /* Adjust positions in the glyph matrix. */
13745 if (delta || delta_bytes)
13746 {
13747 struct glyph_row *r1
13748 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13749 increment_matrix_positions (w->current_matrix,
13750 MATRIX_ROW_VPOS (r0, current_matrix),
13751 MATRIX_ROW_VPOS (r1, current_matrix),
13752 delta, delta_bytes);
13753 }
13754
13755 /* Set the cursor. */
13756 row = row_containing_pos (w, PT, r0, NULL, 0);
13757 if (row)
13758 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13759 else
13760 abort ();
13761 return 1;
13762 }
13763 }
13764
13765 /* Handle the case that changes are all below what is displayed in
13766 the window, and that PT is in the window. This shortcut cannot
13767 be taken if ZV is visible in the window, and text has been added
13768 there that is visible in the window. */
13769 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13770 /* ZV is not visible in the window, or there are no
13771 changes at ZV, actually. */
13772 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13773 || first_changed_charpos == last_changed_charpos))
13774 {
13775 struct glyph_row *r0;
13776
13777 /* Give up if PT is not in the window. Note that it already has
13778 been checked at the start of try_window_id that PT is not in
13779 front of the window start. */
13780 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13781 GIVE_UP (14);
13782
13783 /* If window start is unchanged, we can reuse the whole matrix
13784 as is, without changing glyph positions since no text has
13785 been added/removed in front of the window end. */
13786 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13787 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13788 /* PT must not be in a partially visible line. */
13789 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13790 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13791 {
13792 /* We have to compute the window end anew since text
13793 can have been added/removed after it. */
13794 w->window_end_pos
13795 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13796 w->window_end_bytepos
13797 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13798
13799 /* Set the cursor. */
13800 row = row_containing_pos (w, PT, r0, NULL, 0);
13801 if (row)
13802 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13803 else
13804 abort ();
13805 return 2;
13806 }
13807 }
13808
13809 /* Give up if window start is in the changed area.
13810
13811 The condition used to read
13812
13813 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13814
13815 but why that was tested escapes me at the moment. */
13816 if (CHARPOS (start) >= first_changed_charpos
13817 && CHARPOS (start) <= last_changed_charpos)
13818 GIVE_UP (15);
13819
13820 /* Check that window start agrees with the start of the first glyph
13821 row in its current matrix. Check this after we know the window
13822 start is not in changed text, otherwise positions would not be
13823 comparable. */
13824 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13825 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13826 GIVE_UP (16);
13827
13828 /* Give up if the window ends in strings. Overlay strings
13829 at the end are difficult to handle, so don't try. */
13830 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13831 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13832 GIVE_UP (20);
13833
13834 /* Compute the position at which we have to start displaying new
13835 lines. Some of the lines at the top of the window might be
13836 reusable because they are not displaying changed text. Find the
13837 last row in W's current matrix not affected by changes at the
13838 start of current_buffer. Value is null if changes start in the
13839 first line of window. */
13840 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13841 if (last_unchanged_at_beg_row)
13842 {
13843 /* Avoid starting to display in the moddle of a character, a TAB
13844 for instance. This is easier than to set up the iterator
13845 exactly, and it's not a frequent case, so the additional
13846 effort wouldn't really pay off. */
13847 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13848 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13849 && last_unchanged_at_beg_row > w->current_matrix->rows)
13850 --last_unchanged_at_beg_row;
13851
13852 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13853 GIVE_UP (17);
13854
13855 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13856 GIVE_UP (18);
13857 start_pos = it.current.pos;
13858
13859 /* Start displaying new lines in the desired matrix at the same
13860 vpos we would use in the current matrix, i.e. below
13861 last_unchanged_at_beg_row. */
13862 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13863 current_matrix);
13864 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13865 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13866
13867 xassert (it.hpos == 0 && it.current_x == 0);
13868 }
13869 else
13870 {
13871 /* There are no reusable lines at the start of the window.
13872 Start displaying in the first text line. */
13873 start_display (&it, w, start);
13874 it.vpos = it.first_vpos;
13875 start_pos = it.current.pos;
13876 }
13877
13878 /* Find the first row that is not affected by changes at the end of
13879 the buffer. Value will be null if there is no unchanged row, in
13880 which case we must redisplay to the end of the window. delta
13881 will be set to the value by which buffer positions beginning with
13882 first_unchanged_at_end_row have to be adjusted due to text
13883 changes. */
13884 first_unchanged_at_end_row
13885 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13886 IF_DEBUG (debug_delta = delta);
13887 IF_DEBUG (debug_delta_bytes = delta_bytes);
13888
13889 /* Set stop_pos to the buffer position up to which we will have to
13890 display new lines. If first_unchanged_at_end_row != NULL, this
13891 is the buffer position of the start of the line displayed in that
13892 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13893 that we don't stop at a buffer position. */
13894 stop_pos = 0;
13895 if (first_unchanged_at_end_row)
13896 {
13897 xassert (last_unchanged_at_beg_row == NULL
13898 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13899
13900 /* If this is a continuation line, move forward to the next one
13901 that isn't. Changes in lines above affect this line.
13902 Caution: this may move first_unchanged_at_end_row to a row
13903 not displaying text. */
13904 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13905 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13906 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13907 < it.last_visible_y))
13908 ++first_unchanged_at_end_row;
13909
13910 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13911 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13912 >= it.last_visible_y))
13913 first_unchanged_at_end_row = NULL;
13914 else
13915 {
13916 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13917 + delta);
13918 first_unchanged_at_end_vpos
13919 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13920 xassert (stop_pos >= Z - END_UNCHANGED);
13921 }
13922 }
13923 else if (last_unchanged_at_beg_row == NULL)
13924 GIVE_UP (19);
13925
13926
13927 #if GLYPH_DEBUG
13928
13929 /* Either there is no unchanged row at the end, or the one we have
13930 now displays text. This is a necessary condition for the window
13931 end pos calculation at the end of this function. */
13932 xassert (first_unchanged_at_end_row == NULL
13933 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13934
13935 debug_last_unchanged_at_beg_vpos
13936 = (last_unchanged_at_beg_row
13937 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13938 : -1);
13939 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13940
13941 #endif /* GLYPH_DEBUG != 0 */
13942
13943
13944 /* Display new lines. Set last_text_row to the last new line
13945 displayed which has text on it, i.e. might end up as being the
13946 line where the window_end_vpos is. */
13947 w->cursor.vpos = -1;
13948 last_text_row = NULL;
13949 overlay_arrow_seen = 0;
13950 while (it.current_y < it.last_visible_y
13951 && !fonts_changed_p
13952 && (first_unchanged_at_end_row == NULL
13953 || IT_CHARPOS (it) < stop_pos))
13954 {
13955 if (display_line (&it))
13956 last_text_row = it.glyph_row - 1;
13957 }
13958
13959 if (fonts_changed_p)
13960 return -1;
13961
13962
13963 /* Compute differences in buffer positions, y-positions etc. for
13964 lines reused at the bottom of the window. Compute what we can
13965 scroll. */
13966 if (first_unchanged_at_end_row
13967 /* No lines reused because we displayed everything up to the
13968 bottom of the window. */
13969 && it.current_y < it.last_visible_y)
13970 {
13971 dvpos = (it.vpos
13972 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13973 current_matrix));
13974 dy = it.current_y - first_unchanged_at_end_row->y;
13975 run.current_y = first_unchanged_at_end_row->y;
13976 run.desired_y = run.current_y + dy;
13977 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13978 }
13979 else
13980 {
13981 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13982 first_unchanged_at_end_row = NULL;
13983 }
13984 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13985
13986
13987 /* Find the cursor if not already found. We have to decide whether
13988 PT will appear on this window (it sometimes doesn't, but this is
13989 not a very frequent case.) This decision has to be made before
13990 the current matrix is altered. A value of cursor.vpos < 0 means
13991 that PT is either in one of the lines beginning at
13992 first_unchanged_at_end_row or below the window. Don't care for
13993 lines that might be displayed later at the window end; as
13994 mentioned, this is not a frequent case. */
13995 if (w->cursor.vpos < 0)
13996 {
13997 /* Cursor in unchanged rows at the top? */
13998 if (PT < CHARPOS (start_pos)
13999 && last_unchanged_at_beg_row)
14000 {
14001 row = row_containing_pos (w, PT,
14002 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14003 last_unchanged_at_beg_row + 1, 0);
14004 if (row)
14005 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14006 }
14007
14008 /* Start from first_unchanged_at_end_row looking for PT. */
14009 else if (first_unchanged_at_end_row)
14010 {
14011 row = row_containing_pos (w, PT - delta,
14012 first_unchanged_at_end_row, NULL, 0);
14013 if (row)
14014 set_cursor_from_row (w, row, w->current_matrix, delta,
14015 delta_bytes, dy, dvpos);
14016 }
14017
14018 /* Give up if cursor was not found. */
14019 if (w->cursor.vpos < 0)
14020 {
14021 clear_glyph_matrix (w->desired_matrix);
14022 return -1;
14023 }
14024 }
14025
14026 /* Don't let the cursor end in the scroll margins. */
14027 {
14028 int this_scroll_margin, cursor_height;
14029
14030 this_scroll_margin = max (0, scroll_margin);
14031 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14032 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14033 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14034
14035 if ((w->cursor.y < this_scroll_margin
14036 && CHARPOS (start) > BEGV)
14037 /* Old redisplay didn't take scroll margin into account at the bottom,
14038 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14039 || (w->cursor.y + (make_cursor_line_fully_visible_p
14040 ? cursor_height + this_scroll_margin
14041 : 1)) > it.last_visible_y)
14042 {
14043 w->cursor.vpos = -1;
14044 clear_glyph_matrix (w->desired_matrix);
14045 return -1;
14046 }
14047 }
14048
14049 /* Scroll the display. Do it before changing the current matrix so
14050 that xterm.c doesn't get confused about where the cursor glyph is
14051 found. */
14052 if (dy && run.height)
14053 {
14054 update_begin (f);
14055
14056 if (FRAME_WINDOW_P (f))
14057 {
14058 rif->update_window_begin_hook (w);
14059 rif->clear_window_mouse_face (w);
14060 rif->scroll_run_hook (w, &run);
14061 rif->update_window_end_hook (w, 0, 0);
14062 }
14063 else
14064 {
14065 /* Terminal frame. In this case, dvpos gives the number of
14066 lines to scroll by; dvpos < 0 means scroll up. */
14067 int first_unchanged_at_end_vpos
14068 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14069 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14070 int end = (WINDOW_TOP_EDGE_LINE (w)
14071 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14072 + window_internal_height (w));
14073
14074 /* Perform the operation on the screen. */
14075 if (dvpos > 0)
14076 {
14077 /* Scroll last_unchanged_at_beg_row to the end of the
14078 window down dvpos lines. */
14079 set_terminal_window (end);
14080
14081 /* On dumb terminals delete dvpos lines at the end
14082 before inserting dvpos empty lines. */
14083 if (!scroll_region_ok)
14084 ins_del_lines (end - dvpos, -dvpos);
14085
14086 /* Insert dvpos empty lines in front of
14087 last_unchanged_at_beg_row. */
14088 ins_del_lines (from, dvpos);
14089 }
14090 else if (dvpos < 0)
14091 {
14092 /* Scroll up last_unchanged_at_beg_vpos to the end of
14093 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14094 set_terminal_window (end);
14095
14096 /* Delete dvpos lines in front of
14097 last_unchanged_at_beg_vpos. ins_del_lines will set
14098 the cursor to the given vpos and emit |dvpos| delete
14099 line sequences. */
14100 ins_del_lines (from + dvpos, dvpos);
14101
14102 /* On a dumb terminal insert dvpos empty lines at the
14103 end. */
14104 if (!scroll_region_ok)
14105 ins_del_lines (end + dvpos, -dvpos);
14106 }
14107
14108 set_terminal_window (0);
14109 }
14110
14111 update_end (f);
14112 }
14113
14114 /* Shift reused rows of the current matrix to the right position.
14115 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14116 text. */
14117 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14118 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14119 if (dvpos < 0)
14120 {
14121 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14122 bottom_vpos, dvpos);
14123 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14124 bottom_vpos, 0);
14125 }
14126 else if (dvpos > 0)
14127 {
14128 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14129 bottom_vpos, dvpos);
14130 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14131 first_unchanged_at_end_vpos + dvpos, 0);
14132 }
14133
14134 /* For frame-based redisplay, make sure that current frame and window
14135 matrix are in sync with respect to glyph memory. */
14136 if (!FRAME_WINDOW_P (f))
14137 sync_frame_with_window_matrix_rows (w);
14138
14139 /* Adjust buffer positions in reused rows. */
14140 if (delta)
14141 increment_matrix_positions (current_matrix,
14142 first_unchanged_at_end_vpos + dvpos,
14143 bottom_vpos, delta, delta_bytes);
14144
14145 /* Adjust Y positions. */
14146 if (dy)
14147 shift_glyph_matrix (w, current_matrix,
14148 first_unchanged_at_end_vpos + dvpos,
14149 bottom_vpos, dy);
14150
14151 if (first_unchanged_at_end_row)
14152 {
14153 first_unchanged_at_end_row += dvpos;
14154 if (first_unchanged_at_end_row->y >= it.last_visible_y
14155 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14156 first_unchanged_at_end_row = NULL;
14157 }
14158
14159 /* If scrolling up, there may be some lines to display at the end of
14160 the window. */
14161 last_text_row_at_end = NULL;
14162 if (dy < 0)
14163 {
14164 /* Scrolling up can leave for example a partially visible line
14165 at the end of the window to be redisplayed. */
14166 /* Set last_row to the glyph row in the current matrix where the
14167 window end line is found. It has been moved up or down in
14168 the matrix by dvpos. */
14169 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14170 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14171
14172 /* If last_row is the window end line, it should display text. */
14173 xassert (last_row->displays_text_p);
14174
14175 /* If window end line was partially visible before, begin
14176 displaying at that line. Otherwise begin displaying with the
14177 line following it. */
14178 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14179 {
14180 init_to_row_start (&it, w, last_row);
14181 it.vpos = last_vpos;
14182 it.current_y = last_row->y;
14183 }
14184 else
14185 {
14186 init_to_row_end (&it, w, last_row);
14187 it.vpos = 1 + last_vpos;
14188 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14189 ++last_row;
14190 }
14191
14192 /* We may start in a continuation line. If so, we have to
14193 get the right continuation_lines_width and current_x. */
14194 it.continuation_lines_width = last_row->continuation_lines_width;
14195 it.hpos = it.current_x = 0;
14196
14197 /* Display the rest of the lines at the window end. */
14198 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14199 while (it.current_y < it.last_visible_y
14200 && !fonts_changed_p)
14201 {
14202 /* Is it always sure that the display agrees with lines in
14203 the current matrix? I don't think so, so we mark rows
14204 displayed invalid in the current matrix by setting their
14205 enabled_p flag to zero. */
14206 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14207 if (display_line (&it))
14208 last_text_row_at_end = it.glyph_row - 1;
14209 }
14210 }
14211
14212 /* Update window_end_pos and window_end_vpos. */
14213 if (first_unchanged_at_end_row
14214 && !last_text_row_at_end)
14215 {
14216 /* Window end line if one of the preserved rows from the current
14217 matrix. Set row to the last row displaying text in current
14218 matrix starting at first_unchanged_at_end_row, after
14219 scrolling. */
14220 xassert (first_unchanged_at_end_row->displays_text_p);
14221 row = find_last_row_displaying_text (w->current_matrix, &it,
14222 first_unchanged_at_end_row);
14223 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14224
14225 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14226 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14227 w->window_end_vpos
14228 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14229 xassert (w->window_end_bytepos >= 0);
14230 IF_DEBUG (debug_method_add (w, "A"));
14231 }
14232 else if (last_text_row_at_end)
14233 {
14234 w->window_end_pos
14235 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14236 w->window_end_bytepos
14237 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14238 w->window_end_vpos
14239 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14240 xassert (w->window_end_bytepos >= 0);
14241 IF_DEBUG (debug_method_add (w, "B"));
14242 }
14243 else if (last_text_row)
14244 {
14245 /* We have displayed either to the end of the window or at the
14246 end of the window, i.e. the last row with text is to be found
14247 in the desired matrix. */
14248 w->window_end_pos
14249 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14250 w->window_end_bytepos
14251 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14252 w->window_end_vpos
14253 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14254 xassert (w->window_end_bytepos >= 0);
14255 }
14256 else if (first_unchanged_at_end_row == NULL
14257 && last_text_row == NULL
14258 && last_text_row_at_end == NULL)
14259 {
14260 /* Displayed to end of window, but no line containing text was
14261 displayed. Lines were deleted at the end of the window. */
14262 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14263 int vpos = XFASTINT (w->window_end_vpos);
14264 struct glyph_row *current_row = current_matrix->rows + vpos;
14265 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14266
14267 for (row = NULL;
14268 row == NULL && vpos >= first_vpos;
14269 --vpos, --current_row, --desired_row)
14270 {
14271 if (desired_row->enabled_p)
14272 {
14273 if (desired_row->displays_text_p)
14274 row = desired_row;
14275 }
14276 else if (current_row->displays_text_p)
14277 row = current_row;
14278 }
14279
14280 xassert (row != NULL);
14281 w->window_end_vpos = make_number (vpos + 1);
14282 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14283 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14284 xassert (w->window_end_bytepos >= 0);
14285 IF_DEBUG (debug_method_add (w, "C"));
14286 }
14287 else
14288 abort ();
14289
14290 #if 0 /* This leads to problems, for instance when the cursor is
14291 at ZV, and the cursor line displays no text. */
14292 /* Disable rows below what's displayed in the window. This makes
14293 debugging easier. */
14294 enable_glyph_matrix_rows (current_matrix,
14295 XFASTINT (w->window_end_vpos) + 1,
14296 bottom_vpos, 0);
14297 #endif
14298
14299 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14300 debug_end_vpos = XFASTINT (w->window_end_vpos));
14301
14302 /* Record that display has not been completed. */
14303 w->window_end_valid = Qnil;
14304 w->desired_matrix->no_scrolling_p = 1;
14305 return 3;
14306
14307 #undef GIVE_UP
14308 }
14309
14310
14311 \f
14312 /***********************************************************************
14313 More debugging support
14314 ***********************************************************************/
14315
14316 #if GLYPH_DEBUG
14317
14318 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14319 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14320 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14321
14322
14323 /* Dump the contents of glyph matrix MATRIX on stderr.
14324
14325 GLYPHS 0 means don't show glyph contents.
14326 GLYPHS 1 means show glyphs in short form
14327 GLYPHS > 1 means show glyphs in long form. */
14328
14329 void
14330 dump_glyph_matrix (matrix, glyphs)
14331 struct glyph_matrix *matrix;
14332 int glyphs;
14333 {
14334 int i;
14335 for (i = 0; i < matrix->nrows; ++i)
14336 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14337 }
14338
14339
14340 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14341 the glyph row and area where the glyph comes from. */
14342
14343 void
14344 dump_glyph (row, glyph, area)
14345 struct glyph_row *row;
14346 struct glyph *glyph;
14347 int area;
14348 {
14349 if (glyph->type == CHAR_GLYPH)
14350 {
14351 fprintf (stderr,
14352 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14353 glyph - row->glyphs[TEXT_AREA],
14354 'C',
14355 glyph->charpos,
14356 (BUFFERP (glyph->object)
14357 ? 'B'
14358 : (STRINGP (glyph->object)
14359 ? 'S'
14360 : '-')),
14361 glyph->pixel_width,
14362 glyph->u.ch,
14363 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14364 ? glyph->u.ch
14365 : '.'),
14366 glyph->face_id,
14367 glyph->left_box_line_p,
14368 glyph->right_box_line_p);
14369 }
14370 else if (glyph->type == STRETCH_GLYPH)
14371 {
14372 fprintf (stderr,
14373 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14374 glyph - row->glyphs[TEXT_AREA],
14375 'S',
14376 glyph->charpos,
14377 (BUFFERP (glyph->object)
14378 ? 'B'
14379 : (STRINGP (glyph->object)
14380 ? 'S'
14381 : '-')),
14382 glyph->pixel_width,
14383 0,
14384 '.',
14385 glyph->face_id,
14386 glyph->left_box_line_p,
14387 glyph->right_box_line_p);
14388 }
14389 else if (glyph->type == IMAGE_GLYPH)
14390 {
14391 fprintf (stderr,
14392 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14393 glyph - row->glyphs[TEXT_AREA],
14394 'I',
14395 glyph->charpos,
14396 (BUFFERP (glyph->object)
14397 ? 'B'
14398 : (STRINGP (glyph->object)
14399 ? 'S'
14400 : '-')),
14401 glyph->pixel_width,
14402 glyph->u.img_id,
14403 '.',
14404 glyph->face_id,
14405 glyph->left_box_line_p,
14406 glyph->right_box_line_p);
14407 }
14408 }
14409
14410
14411 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14412 GLYPHS 0 means don't show glyph contents.
14413 GLYPHS 1 means show glyphs in short form
14414 GLYPHS > 1 means show glyphs in long form. */
14415
14416 void
14417 dump_glyph_row (row, vpos, glyphs)
14418 struct glyph_row *row;
14419 int vpos, glyphs;
14420 {
14421 if (glyphs != 1)
14422 {
14423 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14424 fprintf (stderr, "======================================================================\n");
14425
14426 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14427 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14428 vpos,
14429 MATRIX_ROW_START_CHARPOS (row),
14430 MATRIX_ROW_END_CHARPOS (row),
14431 row->used[TEXT_AREA],
14432 row->contains_overlapping_glyphs_p,
14433 row->enabled_p,
14434 row->truncated_on_left_p,
14435 row->truncated_on_right_p,
14436 row->continued_p,
14437 MATRIX_ROW_CONTINUATION_LINE_P (row),
14438 row->displays_text_p,
14439 row->ends_at_zv_p,
14440 row->fill_line_p,
14441 row->ends_in_middle_of_char_p,
14442 row->starts_in_middle_of_char_p,
14443 row->mouse_face_p,
14444 row->x,
14445 row->y,
14446 row->pixel_width,
14447 row->height,
14448 row->visible_height,
14449 row->ascent,
14450 row->phys_ascent);
14451 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14452 row->end.overlay_string_index,
14453 row->continuation_lines_width);
14454 fprintf (stderr, "%9d %5d\n",
14455 CHARPOS (row->start.string_pos),
14456 CHARPOS (row->end.string_pos));
14457 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14458 row->end.dpvec_index);
14459 }
14460
14461 if (glyphs > 1)
14462 {
14463 int area;
14464
14465 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14466 {
14467 struct glyph *glyph = row->glyphs[area];
14468 struct glyph *glyph_end = glyph + row->used[area];
14469
14470 /* Glyph for a line end in text. */
14471 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14472 ++glyph_end;
14473
14474 if (glyph < glyph_end)
14475 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14476
14477 for (; glyph < glyph_end; ++glyph)
14478 dump_glyph (row, glyph, area);
14479 }
14480 }
14481 else if (glyphs == 1)
14482 {
14483 int area;
14484
14485 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14486 {
14487 char *s = (char *) alloca (row->used[area] + 1);
14488 int i;
14489
14490 for (i = 0; i < row->used[area]; ++i)
14491 {
14492 struct glyph *glyph = row->glyphs[area] + i;
14493 if (glyph->type == CHAR_GLYPH
14494 && glyph->u.ch < 0x80
14495 && glyph->u.ch >= ' ')
14496 s[i] = glyph->u.ch;
14497 else
14498 s[i] = '.';
14499 }
14500
14501 s[i] = '\0';
14502 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14503 }
14504 }
14505 }
14506
14507
14508 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14509 Sdump_glyph_matrix, 0, 1, "p",
14510 doc: /* Dump the current matrix of the selected window to stderr.
14511 Shows contents of glyph row structures. With non-nil
14512 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14513 glyphs in short form, otherwise show glyphs in long form. */)
14514 (glyphs)
14515 Lisp_Object glyphs;
14516 {
14517 struct window *w = XWINDOW (selected_window);
14518 struct buffer *buffer = XBUFFER (w->buffer);
14519
14520 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14521 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14522 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14523 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14524 fprintf (stderr, "=============================================\n");
14525 dump_glyph_matrix (w->current_matrix,
14526 NILP (glyphs) ? 0 : XINT (glyphs));
14527 return Qnil;
14528 }
14529
14530
14531 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14532 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14533 ()
14534 {
14535 struct frame *f = XFRAME (selected_frame);
14536 dump_glyph_matrix (f->current_matrix, 1);
14537 return Qnil;
14538 }
14539
14540
14541 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14542 doc: /* Dump glyph row ROW to stderr.
14543 GLYPH 0 means don't dump glyphs.
14544 GLYPH 1 means dump glyphs in short form.
14545 GLYPH > 1 or omitted means dump glyphs in long form. */)
14546 (row, glyphs)
14547 Lisp_Object row, glyphs;
14548 {
14549 struct glyph_matrix *matrix;
14550 int vpos;
14551
14552 CHECK_NUMBER (row);
14553 matrix = XWINDOW (selected_window)->current_matrix;
14554 vpos = XINT (row);
14555 if (vpos >= 0 && vpos < matrix->nrows)
14556 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14557 vpos,
14558 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14559 return Qnil;
14560 }
14561
14562
14563 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14564 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14565 GLYPH 0 means don't dump glyphs.
14566 GLYPH 1 means dump glyphs in short form.
14567 GLYPH > 1 or omitted means dump glyphs in long form. */)
14568 (row, glyphs)
14569 Lisp_Object row, glyphs;
14570 {
14571 struct frame *sf = SELECTED_FRAME ();
14572 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14573 int vpos;
14574
14575 CHECK_NUMBER (row);
14576 vpos = XINT (row);
14577 if (vpos >= 0 && vpos < m->nrows)
14578 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14579 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14580 return Qnil;
14581 }
14582
14583
14584 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14585 doc: /* Toggle tracing of redisplay.
14586 With ARG, turn tracing on if and only if ARG is positive. */)
14587 (arg)
14588 Lisp_Object arg;
14589 {
14590 if (NILP (arg))
14591 trace_redisplay_p = !trace_redisplay_p;
14592 else
14593 {
14594 arg = Fprefix_numeric_value (arg);
14595 trace_redisplay_p = XINT (arg) > 0;
14596 }
14597
14598 return Qnil;
14599 }
14600
14601
14602 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14603 doc: /* Like `format', but print result to stderr.
14604 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14605 (nargs, args)
14606 int nargs;
14607 Lisp_Object *args;
14608 {
14609 Lisp_Object s = Fformat (nargs, args);
14610 fprintf (stderr, "%s", SDATA (s));
14611 return Qnil;
14612 }
14613
14614 #endif /* GLYPH_DEBUG */
14615
14616
14617 \f
14618 /***********************************************************************
14619 Building Desired Matrix Rows
14620 ***********************************************************************/
14621
14622 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14623 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14624
14625 static struct glyph_row *
14626 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14627 struct window *w;
14628 Lisp_Object overlay_arrow_string;
14629 {
14630 struct frame *f = XFRAME (WINDOW_FRAME (w));
14631 struct buffer *buffer = XBUFFER (w->buffer);
14632 struct buffer *old = current_buffer;
14633 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14634 int arrow_len = SCHARS (overlay_arrow_string);
14635 const unsigned char *arrow_end = arrow_string + arrow_len;
14636 const unsigned char *p;
14637 struct it it;
14638 int multibyte_p;
14639 int n_glyphs_before;
14640
14641 set_buffer_temp (buffer);
14642 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14643 it.glyph_row->used[TEXT_AREA] = 0;
14644 SET_TEXT_POS (it.position, 0, 0);
14645
14646 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14647 p = arrow_string;
14648 while (p < arrow_end)
14649 {
14650 Lisp_Object face, ilisp;
14651
14652 /* Get the next character. */
14653 if (multibyte_p)
14654 it.c = string_char_and_length (p, arrow_len, &it.len);
14655 else
14656 it.c = *p, it.len = 1;
14657 p += it.len;
14658
14659 /* Get its face. */
14660 ilisp = make_number (p - arrow_string);
14661 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14662 it.face_id = compute_char_face (f, it.c, face);
14663
14664 /* Compute its width, get its glyphs. */
14665 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14666 SET_TEXT_POS (it.position, -1, -1);
14667 PRODUCE_GLYPHS (&it);
14668
14669 /* If this character doesn't fit any more in the line, we have
14670 to remove some glyphs. */
14671 if (it.current_x > it.last_visible_x)
14672 {
14673 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14674 break;
14675 }
14676 }
14677
14678 set_buffer_temp (old);
14679 return it.glyph_row;
14680 }
14681
14682
14683 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14684 glyphs are only inserted for terminal frames since we can't really
14685 win with truncation glyphs when partially visible glyphs are
14686 involved. Which glyphs to insert is determined by
14687 produce_special_glyphs. */
14688
14689 static void
14690 insert_left_trunc_glyphs (it)
14691 struct it *it;
14692 {
14693 struct it truncate_it;
14694 struct glyph *from, *end, *to, *toend;
14695
14696 xassert (!FRAME_WINDOW_P (it->f));
14697
14698 /* Get the truncation glyphs. */
14699 truncate_it = *it;
14700 truncate_it.current_x = 0;
14701 truncate_it.face_id = DEFAULT_FACE_ID;
14702 truncate_it.glyph_row = &scratch_glyph_row;
14703 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14704 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14705 truncate_it.object = make_number (0);
14706 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14707
14708 /* Overwrite glyphs from IT with truncation glyphs. */
14709 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14710 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14711 to = it->glyph_row->glyphs[TEXT_AREA];
14712 toend = to + it->glyph_row->used[TEXT_AREA];
14713
14714 while (from < end)
14715 *to++ = *from++;
14716
14717 /* There may be padding glyphs left over. Overwrite them too. */
14718 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14719 {
14720 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14721 while (from < end)
14722 *to++ = *from++;
14723 }
14724
14725 if (to > toend)
14726 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14727 }
14728
14729
14730 /* Compute the pixel height and width of IT->glyph_row.
14731
14732 Most of the time, ascent and height of a display line will be equal
14733 to the max_ascent and max_height values of the display iterator
14734 structure. This is not the case if
14735
14736 1. We hit ZV without displaying anything. In this case, max_ascent
14737 and max_height will be zero.
14738
14739 2. We have some glyphs that don't contribute to the line height.
14740 (The glyph row flag contributes_to_line_height_p is for future
14741 pixmap extensions).
14742
14743 The first case is easily covered by using default values because in
14744 these cases, the line height does not really matter, except that it
14745 must not be zero. */
14746
14747 static void
14748 compute_line_metrics (it)
14749 struct it *it;
14750 {
14751 struct glyph_row *row = it->glyph_row;
14752 int area, i;
14753
14754 if (FRAME_WINDOW_P (it->f))
14755 {
14756 int i, min_y, max_y;
14757
14758 /* The line may consist of one space only, that was added to
14759 place the cursor on it. If so, the row's height hasn't been
14760 computed yet. */
14761 if (row->height == 0)
14762 {
14763 if (it->max_ascent + it->max_descent == 0)
14764 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14765 row->ascent = it->max_ascent;
14766 row->height = it->max_ascent + it->max_descent;
14767 row->phys_ascent = it->max_phys_ascent;
14768 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14769 row->extra_line_spacing = it->max_extra_line_spacing;
14770 }
14771
14772 /* Compute the width of this line. */
14773 row->pixel_width = row->x;
14774 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14775 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14776
14777 xassert (row->pixel_width >= 0);
14778 xassert (row->ascent >= 0 && row->height > 0);
14779
14780 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14781 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14782
14783 /* If first line's physical ascent is larger than its logical
14784 ascent, use the physical ascent, and make the row taller.
14785 This makes accented characters fully visible. */
14786 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14787 && row->phys_ascent > row->ascent)
14788 {
14789 row->height += row->phys_ascent - row->ascent;
14790 row->ascent = row->phys_ascent;
14791 }
14792
14793 /* Compute how much of the line is visible. */
14794 row->visible_height = row->height;
14795
14796 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14797 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14798
14799 if (row->y < min_y)
14800 row->visible_height -= min_y - row->y;
14801 if (row->y + row->height > max_y)
14802 row->visible_height -= row->y + row->height - max_y;
14803 }
14804 else
14805 {
14806 row->pixel_width = row->used[TEXT_AREA];
14807 if (row->continued_p)
14808 row->pixel_width -= it->continuation_pixel_width;
14809 else if (row->truncated_on_right_p)
14810 row->pixel_width -= it->truncation_pixel_width;
14811 row->ascent = row->phys_ascent = 0;
14812 row->height = row->phys_height = row->visible_height = 1;
14813 row->extra_line_spacing = 0;
14814 }
14815
14816 /* Compute a hash code for this row. */
14817 row->hash = 0;
14818 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14819 for (i = 0; i < row->used[area]; ++i)
14820 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14821 + row->glyphs[area][i].u.val
14822 + row->glyphs[area][i].face_id
14823 + row->glyphs[area][i].padding_p
14824 + (row->glyphs[area][i].type << 2));
14825
14826 it->max_ascent = it->max_descent = 0;
14827 it->max_phys_ascent = it->max_phys_descent = 0;
14828 }
14829
14830
14831 /* Append one space to the glyph row of iterator IT if doing a
14832 window-based redisplay. The space has the same face as
14833 IT->face_id. Value is non-zero if a space was added.
14834
14835 This function is called to make sure that there is always one glyph
14836 at the end of a glyph row that the cursor can be set on under
14837 window-systems. (If there weren't such a glyph we would not know
14838 how wide and tall a box cursor should be displayed).
14839
14840 At the same time this space let's a nicely handle clearing to the
14841 end of the line if the row ends in italic text. */
14842
14843 static int
14844 append_space_for_newline (it, default_face_p)
14845 struct it *it;
14846 int default_face_p;
14847 {
14848 if (FRAME_WINDOW_P (it->f))
14849 {
14850 int n = it->glyph_row->used[TEXT_AREA];
14851
14852 if (it->glyph_row->glyphs[TEXT_AREA] + n
14853 < it->glyph_row->glyphs[1 + TEXT_AREA])
14854 {
14855 /* Save some values that must not be changed.
14856 Must save IT->c and IT->len because otherwise
14857 ITERATOR_AT_END_P wouldn't work anymore after
14858 append_space_for_newline has been called. */
14859 enum display_element_type saved_what = it->what;
14860 int saved_c = it->c, saved_len = it->len;
14861 int saved_x = it->current_x;
14862 int saved_face_id = it->face_id;
14863 struct text_pos saved_pos;
14864 Lisp_Object saved_object;
14865 struct face *face;
14866
14867 saved_object = it->object;
14868 saved_pos = it->position;
14869
14870 it->what = IT_CHARACTER;
14871 bzero (&it->position, sizeof it->position);
14872 it->object = make_number (0);
14873 it->c = ' ';
14874 it->len = 1;
14875
14876 if (default_face_p)
14877 it->face_id = DEFAULT_FACE_ID;
14878 else if (it->face_before_selective_p)
14879 it->face_id = it->saved_face_id;
14880 face = FACE_FROM_ID (it->f, it->face_id);
14881 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14882
14883 PRODUCE_GLYPHS (it);
14884
14885 it->override_ascent = -1;
14886 it->constrain_row_ascent_descent_p = 0;
14887 it->current_x = saved_x;
14888 it->object = saved_object;
14889 it->position = saved_pos;
14890 it->what = saved_what;
14891 it->face_id = saved_face_id;
14892 it->len = saved_len;
14893 it->c = saved_c;
14894 return 1;
14895 }
14896 }
14897
14898 return 0;
14899 }
14900
14901
14902 /* Extend the face of the last glyph in the text area of IT->glyph_row
14903 to the end of the display line. Called from display_line.
14904 If the glyph row is empty, add a space glyph to it so that we
14905 know the face to draw. Set the glyph row flag fill_line_p. */
14906
14907 static void
14908 extend_face_to_end_of_line (it)
14909 struct it *it;
14910 {
14911 struct face *face;
14912 struct frame *f = it->f;
14913
14914 /* If line is already filled, do nothing. */
14915 if (it->current_x >= it->last_visible_x)
14916 return;
14917
14918 /* Face extension extends the background and box of IT->face_id
14919 to the end of the line. If the background equals the background
14920 of the frame, we don't have to do anything. */
14921 if (it->face_before_selective_p)
14922 face = FACE_FROM_ID (it->f, it->saved_face_id);
14923 else
14924 face = FACE_FROM_ID (f, it->face_id);
14925
14926 if (FRAME_WINDOW_P (f)
14927 && face->box == FACE_NO_BOX
14928 && face->background == FRAME_BACKGROUND_PIXEL (f)
14929 && !face->stipple)
14930 return;
14931
14932 /* Set the glyph row flag indicating that the face of the last glyph
14933 in the text area has to be drawn to the end of the text area. */
14934 it->glyph_row->fill_line_p = 1;
14935
14936 /* If current character of IT is not ASCII, make sure we have the
14937 ASCII face. This will be automatically undone the next time
14938 get_next_display_element returns a multibyte character. Note
14939 that the character will always be single byte in unibyte text. */
14940 if (!SINGLE_BYTE_CHAR_P (it->c))
14941 {
14942 it->face_id = FACE_FOR_CHAR (f, face, 0);
14943 }
14944
14945 if (FRAME_WINDOW_P (f))
14946 {
14947 /* If the row is empty, add a space with the current face of IT,
14948 so that we know which face to draw. */
14949 if (it->glyph_row->used[TEXT_AREA] == 0)
14950 {
14951 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14952 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14953 it->glyph_row->used[TEXT_AREA] = 1;
14954 }
14955 }
14956 else
14957 {
14958 /* Save some values that must not be changed. */
14959 int saved_x = it->current_x;
14960 struct text_pos saved_pos;
14961 Lisp_Object saved_object;
14962 enum display_element_type saved_what = it->what;
14963 int saved_face_id = it->face_id;
14964
14965 saved_object = it->object;
14966 saved_pos = it->position;
14967
14968 it->what = IT_CHARACTER;
14969 bzero (&it->position, sizeof it->position);
14970 it->object = make_number (0);
14971 it->c = ' ';
14972 it->len = 1;
14973 it->face_id = face->id;
14974
14975 PRODUCE_GLYPHS (it);
14976
14977 while (it->current_x <= it->last_visible_x)
14978 PRODUCE_GLYPHS (it);
14979
14980 /* Don't count these blanks really. It would let us insert a left
14981 truncation glyph below and make us set the cursor on them, maybe. */
14982 it->current_x = saved_x;
14983 it->object = saved_object;
14984 it->position = saved_pos;
14985 it->what = saved_what;
14986 it->face_id = saved_face_id;
14987 }
14988 }
14989
14990
14991 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14992 trailing whitespace. */
14993
14994 static int
14995 trailing_whitespace_p (charpos)
14996 int charpos;
14997 {
14998 int bytepos = CHAR_TO_BYTE (charpos);
14999 int c = 0;
15000
15001 while (bytepos < ZV_BYTE
15002 && (c = FETCH_CHAR (bytepos),
15003 c == ' ' || c == '\t'))
15004 ++bytepos;
15005
15006 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15007 {
15008 if (bytepos != PT_BYTE)
15009 return 1;
15010 }
15011 return 0;
15012 }
15013
15014
15015 /* Highlight trailing whitespace, if any, in ROW. */
15016
15017 void
15018 highlight_trailing_whitespace (f, row)
15019 struct frame *f;
15020 struct glyph_row *row;
15021 {
15022 int used = row->used[TEXT_AREA];
15023
15024 if (used)
15025 {
15026 struct glyph *start = row->glyphs[TEXT_AREA];
15027 struct glyph *glyph = start + used - 1;
15028
15029 /* Skip over glyphs inserted to display the cursor at the
15030 end of a line, for extending the face of the last glyph
15031 to the end of the line on terminals, and for truncation
15032 and continuation glyphs. */
15033 while (glyph >= start
15034 && glyph->type == CHAR_GLYPH
15035 && INTEGERP (glyph->object))
15036 --glyph;
15037
15038 /* If last glyph is a space or stretch, and it's trailing
15039 whitespace, set the face of all trailing whitespace glyphs in
15040 IT->glyph_row to `trailing-whitespace'. */
15041 if (glyph >= start
15042 && BUFFERP (glyph->object)
15043 && (glyph->type == STRETCH_GLYPH
15044 || (glyph->type == CHAR_GLYPH
15045 && glyph->u.ch == ' '))
15046 && trailing_whitespace_p (glyph->charpos))
15047 {
15048 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15049 if (face_id < 0)
15050 return;
15051
15052 while (glyph >= start
15053 && BUFFERP (glyph->object)
15054 && (glyph->type == STRETCH_GLYPH
15055 || (glyph->type == CHAR_GLYPH
15056 && glyph->u.ch == ' ')))
15057 (glyph--)->face_id = face_id;
15058 }
15059 }
15060 }
15061
15062
15063 /* Value is non-zero if glyph row ROW in window W should be
15064 used to hold the cursor. */
15065
15066 static int
15067 cursor_row_p (w, row)
15068 struct window *w;
15069 struct glyph_row *row;
15070 {
15071 int cursor_row_p = 1;
15072
15073 if (PT == MATRIX_ROW_END_CHARPOS (row))
15074 {
15075 /* If the row ends with a newline from a string, we don't want
15076 the cursor there, but we still want it at the start of the
15077 string if the string starts in this row.
15078 If the row is continued it doesn't end in a newline. */
15079 if (CHARPOS (row->end.string_pos) >= 0)
15080 cursor_row_p = (row->continued_p
15081 || PT >= MATRIX_ROW_START_CHARPOS (row));
15082 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15083 {
15084 /* If the row ends in middle of a real character,
15085 and the line is continued, we want the cursor here.
15086 That's because MATRIX_ROW_END_CHARPOS would equal
15087 PT if PT is before the character. */
15088 if (!row->ends_in_ellipsis_p)
15089 cursor_row_p = row->continued_p;
15090 else
15091 /* If the row ends in an ellipsis, then
15092 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15093 We want that position to be displayed after the ellipsis. */
15094 cursor_row_p = 0;
15095 }
15096 /* If the row ends at ZV, display the cursor at the end of that
15097 row instead of at the start of the row below. */
15098 else if (row->ends_at_zv_p)
15099 cursor_row_p = 1;
15100 else
15101 cursor_row_p = 0;
15102 }
15103
15104 return cursor_row_p;
15105 }
15106
15107
15108 /* Construct the glyph row IT->glyph_row in the desired matrix of
15109 IT->w from text at the current position of IT. See dispextern.h
15110 for an overview of struct it. Value is non-zero if
15111 IT->glyph_row displays text, as opposed to a line displaying ZV
15112 only. */
15113
15114 static int
15115 display_line (it)
15116 struct it *it;
15117 {
15118 struct glyph_row *row = it->glyph_row;
15119 Lisp_Object overlay_arrow_string;
15120
15121 /* We always start displaying at hpos zero even if hscrolled. */
15122 xassert (it->hpos == 0 && it->current_x == 0);
15123
15124 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15125 >= it->w->desired_matrix->nrows)
15126 {
15127 it->w->nrows_scale_factor++;
15128 fonts_changed_p = 1;
15129 return 0;
15130 }
15131
15132 /* Is IT->w showing the region? */
15133 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15134
15135 /* Clear the result glyph row and enable it. */
15136 prepare_desired_row (row);
15137
15138 row->y = it->current_y;
15139 row->start = it->start;
15140 row->continuation_lines_width = it->continuation_lines_width;
15141 row->displays_text_p = 1;
15142 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15143 it->starts_in_middle_of_char_p = 0;
15144
15145 /* Arrange the overlays nicely for our purposes. Usually, we call
15146 display_line on only one line at a time, in which case this
15147 can't really hurt too much, or we call it on lines which appear
15148 one after another in the buffer, in which case all calls to
15149 recenter_overlay_lists but the first will be pretty cheap. */
15150 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15151
15152 /* Move over display elements that are not visible because we are
15153 hscrolled. This may stop at an x-position < IT->first_visible_x
15154 if the first glyph is partially visible or if we hit a line end. */
15155 if (it->current_x < it->first_visible_x)
15156 {
15157 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15158 MOVE_TO_POS | MOVE_TO_X);
15159 }
15160
15161 /* Get the initial row height. This is either the height of the
15162 text hscrolled, if there is any, or zero. */
15163 row->ascent = it->max_ascent;
15164 row->height = it->max_ascent + it->max_descent;
15165 row->phys_ascent = it->max_phys_ascent;
15166 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15167 row->extra_line_spacing = it->max_extra_line_spacing;
15168
15169 /* Loop generating characters. The loop is left with IT on the next
15170 character to display. */
15171 while (1)
15172 {
15173 int n_glyphs_before, hpos_before, x_before;
15174 int x, i, nglyphs;
15175 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15176
15177 /* Retrieve the next thing to display. Value is zero if end of
15178 buffer reached. */
15179 if (!get_next_display_element (it))
15180 {
15181 /* Maybe add a space at the end of this line that is used to
15182 display the cursor there under X. Set the charpos of the
15183 first glyph of blank lines not corresponding to any text
15184 to -1. */
15185 #ifdef HAVE_WINDOW_SYSTEM
15186 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15187 row->exact_window_width_line_p = 1;
15188 else
15189 #endif /* HAVE_WINDOW_SYSTEM */
15190 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15191 || row->used[TEXT_AREA] == 0)
15192 {
15193 row->glyphs[TEXT_AREA]->charpos = -1;
15194 row->displays_text_p = 0;
15195
15196 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15197 && (!MINI_WINDOW_P (it->w)
15198 || (minibuf_level && EQ (it->window, minibuf_window))))
15199 row->indicate_empty_line_p = 1;
15200 }
15201
15202 it->continuation_lines_width = 0;
15203 row->ends_at_zv_p = 1;
15204 break;
15205 }
15206
15207 /* Now, get the metrics of what we want to display. This also
15208 generates glyphs in `row' (which is IT->glyph_row). */
15209 n_glyphs_before = row->used[TEXT_AREA];
15210 x = it->current_x;
15211
15212 /* Remember the line height so far in case the next element doesn't
15213 fit on the line. */
15214 if (!it->truncate_lines_p)
15215 {
15216 ascent = it->max_ascent;
15217 descent = it->max_descent;
15218 phys_ascent = it->max_phys_ascent;
15219 phys_descent = it->max_phys_descent;
15220 }
15221
15222 PRODUCE_GLYPHS (it);
15223
15224 /* If this display element was in marginal areas, continue with
15225 the next one. */
15226 if (it->area != TEXT_AREA)
15227 {
15228 row->ascent = max (row->ascent, it->max_ascent);
15229 row->height = max (row->height, it->max_ascent + it->max_descent);
15230 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15231 row->phys_height = max (row->phys_height,
15232 it->max_phys_ascent + it->max_phys_descent);
15233 row->extra_line_spacing = max (row->extra_line_spacing,
15234 it->max_extra_line_spacing);
15235 set_iterator_to_next (it, 1);
15236 continue;
15237 }
15238
15239 /* Does the display element fit on the line? If we truncate
15240 lines, we should draw past the right edge of the window. If
15241 we don't truncate, we want to stop so that we can display the
15242 continuation glyph before the right margin. If lines are
15243 continued, there are two possible strategies for characters
15244 resulting in more than 1 glyph (e.g. tabs): Display as many
15245 glyphs as possible in this line and leave the rest for the
15246 continuation line, or display the whole element in the next
15247 line. Original redisplay did the former, so we do it also. */
15248 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15249 hpos_before = it->hpos;
15250 x_before = x;
15251
15252 if (/* Not a newline. */
15253 nglyphs > 0
15254 /* Glyphs produced fit entirely in the line. */
15255 && it->current_x < it->last_visible_x)
15256 {
15257 it->hpos += nglyphs;
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 if (it->current_x - it->pixel_width < it->first_visible_x)
15266 row->x = x - it->first_visible_x;
15267 }
15268 else
15269 {
15270 int new_x;
15271 struct glyph *glyph;
15272
15273 for (i = 0; i < nglyphs; ++i, x = new_x)
15274 {
15275 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15276 new_x = x + glyph->pixel_width;
15277
15278 if (/* Lines are continued. */
15279 !it->truncate_lines_p
15280 && (/* Glyph doesn't fit on the line. */
15281 new_x > it->last_visible_x
15282 /* Or it fits exactly on a window system frame. */
15283 || (new_x == it->last_visible_x
15284 && FRAME_WINDOW_P (it->f))))
15285 {
15286 /* End of a continued line. */
15287
15288 if (it->hpos == 0
15289 || (new_x == it->last_visible_x
15290 && FRAME_WINDOW_P (it->f)))
15291 {
15292 /* Current glyph is the only one on the line or
15293 fits exactly on the line. We must continue
15294 the line because we can't draw the cursor
15295 after the glyph. */
15296 row->continued_p = 1;
15297 it->current_x = new_x;
15298 it->continuation_lines_width += new_x;
15299 ++it->hpos;
15300 if (i == nglyphs - 1)
15301 {
15302 set_iterator_to_next (it, 1);
15303 #ifdef HAVE_WINDOW_SYSTEM
15304 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15305 {
15306 if (!get_next_display_element (it))
15307 {
15308 row->exact_window_width_line_p = 1;
15309 it->continuation_lines_width = 0;
15310 row->continued_p = 0;
15311 row->ends_at_zv_p = 1;
15312 }
15313 else if (ITERATOR_AT_END_OF_LINE_P (it))
15314 {
15315 row->continued_p = 0;
15316 row->exact_window_width_line_p = 1;
15317 }
15318 }
15319 #endif /* HAVE_WINDOW_SYSTEM */
15320 }
15321 }
15322 else if (CHAR_GLYPH_PADDING_P (*glyph)
15323 && !FRAME_WINDOW_P (it->f))
15324 {
15325 /* A padding glyph that doesn't fit on this line.
15326 This means the whole character doesn't fit
15327 on the line. */
15328 row->used[TEXT_AREA] = n_glyphs_before;
15329
15330 /* Fill the rest of the row with continuation
15331 glyphs like in 20.x. */
15332 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15333 < row->glyphs[1 + TEXT_AREA])
15334 produce_special_glyphs (it, IT_CONTINUATION);
15335
15336 row->continued_p = 1;
15337 it->current_x = x_before;
15338 it->continuation_lines_width += x_before;
15339
15340 /* Restore the height to what it was before the
15341 element not fitting on the line. */
15342 it->max_ascent = ascent;
15343 it->max_descent = descent;
15344 it->max_phys_ascent = phys_ascent;
15345 it->max_phys_descent = phys_descent;
15346 }
15347 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15348 {
15349 /* A TAB that extends past the right edge of the
15350 window. This produces a single glyph on
15351 window system frames. We leave the glyph in
15352 this row and let it fill the row, but don't
15353 consume the TAB. */
15354 it->continuation_lines_width += it->last_visible_x;
15355 row->ends_in_middle_of_char_p = 1;
15356 row->continued_p = 1;
15357 glyph->pixel_width = it->last_visible_x - x;
15358 it->starts_in_middle_of_char_p = 1;
15359 }
15360 else
15361 {
15362 /* Something other than a TAB that draws past
15363 the right edge of the window. Restore
15364 positions to values before the element. */
15365 row->used[TEXT_AREA] = n_glyphs_before + i;
15366
15367 /* Display continuation glyphs. */
15368 if (!FRAME_WINDOW_P (it->f))
15369 produce_special_glyphs (it, IT_CONTINUATION);
15370 row->continued_p = 1;
15371
15372 it->continuation_lines_width += x;
15373
15374 if (nglyphs > 1 && i > 0)
15375 {
15376 row->ends_in_middle_of_char_p = 1;
15377 it->starts_in_middle_of_char_p = 1;
15378 }
15379
15380 /* Restore the height to what it was before the
15381 element not fitting on the line. */
15382 it->max_ascent = ascent;
15383 it->max_descent = descent;
15384 it->max_phys_ascent = phys_ascent;
15385 it->max_phys_descent = phys_descent;
15386 }
15387
15388 break;
15389 }
15390 else if (new_x > it->first_visible_x)
15391 {
15392 /* Increment number of glyphs actually displayed. */
15393 ++it->hpos;
15394
15395 if (x < it->first_visible_x)
15396 /* Glyph is partially visible, i.e. row starts at
15397 negative X position. */
15398 row->x = x - it->first_visible_x;
15399 }
15400 else
15401 {
15402 /* Glyph is completely off the left margin of the
15403 window. This should not happen because of the
15404 move_it_in_display_line at the start of this
15405 function, unless the text display area of the
15406 window is empty. */
15407 xassert (it->first_visible_x <= it->last_visible_x);
15408 }
15409 }
15410
15411 row->ascent = max (row->ascent, it->max_ascent);
15412 row->height = max (row->height, it->max_ascent + it->max_descent);
15413 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15414 row->phys_height = max (row->phys_height,
15415 it->max_phys_ascent + it->max_phys_descent);
15416 row->extra_line_spacing = max (row->extra_line_spacing,
15417 it->max_extra_line_spacing);
15418
15419 /* End of this display line if row is continued. */
15420 if (row->continued_p || row->ends_at_zv_p)
15421 break;
15422 }
15423
15424 at_end_of_line:
15425 /* Is this a line end? If yes, we're also done, after making
15426 sure that a non-default face is extended up to the right
15427 margin of the window. */
15428 if (ITERATOR_AT_END_OF_LINE_P (it))
15429 {
15430 int used_before = row->used[TEXT_AREA];
15431
15432 row->ends_in_newline_from_string_p = STRINGP (it->object);
15433
15434 #ifdef HAVE_WINDOW_SYSTEM
15435 /* Add a space at the end of the line that is used to
15436 display the cursor there. */
15437 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15438 append_space_for_newline (it, 0);
15439 #endif /* HAVE_WINDOW_SYSTEM */
15440
15441 /* Extend the face to the end of the line. */
15442 extend_face_to_end_of_line (it);
15443
15444 /* Make sure we have the position. */
15445 if (used_before == 0)
15446 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15447
15448 /* Consume the line end. This skips over invisible lines. */
15449 set_iterator_to_next (it, 1);
15450 it->continuation_lines_width = 0;
15451 break;
15452 }
15453
15454 /* Proceed with next display element. Note that this skips
15455 over lines invisible because of selective display. */
15456 set_iterator_to_next (it, 1);
15457
15458 /* If we truncate lines, we are done when the last displayed
15459 glyphs reach past the right margin of the window. */
15460 if (it->truncate_lines_p
15461 && (FRAME_WINDOW_P (it->f)
15462 ? (it->current_x >= it->last_visible_x)
15463 : (it->current_x > it->last_visible_x)))
15464 {
15465 /* Maybe add truncation glyphs. */
15466 if (!FRAME_WINDOW_P (it->f))
15467 {
15468 int i, n;
15469
15470 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15471 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15472 break;
15473
15474 for (n = row->used[TEXT_AREA]; i < n; ++i)
15475 {
15476 row->used[TEXT_AREA] = i;
15477 produce_special_glyphs (it, IT_TRUNCATION);
15478 }
15479 }
15480 #ifdef HAVE_WINDOW_SYSTEM
15481 else
15482 {
15483 /* Don't truncate if we can overflow newline into fringe. */
15484 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15485 {
15486 if (!get_next_display_element (it))
15487 {
15488 it->continuation_lines_width = 0;
15489 row->ends_at_zv_p = 1;
15490 row->exact_window_width_line_p = 1;
15491 break;
15492 }
15493 if (ITERATOR_AT_END_OF_LINE_P (it))
15494 {
15495 row->exact_window_width_line_p = 1;
15496 goto at_end_of_line;
15497 }
15498 }
15499 }
15500 #endif /* HAVE_WINDOW_SYSTEM */
15501
15502 row->truncated_on_right_p = 1;
15503 it->continuation_lines_width = 0;
15504 reseat_at_next_visible_line_start (it, 0);
15505 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15506 it->hpos = hpos_before;
15507 it->current_x = x_before;
15508 break;
15509 }
15510 }
15511
15512 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15513 at the left window margin. */
15514 if (it->first_visible_x
15515 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15516 {
15517 if (!FRAME_WINDOW_P (it->f))
15518 insert_left_trunc_glyphs (it);
15519 row->truncated_on_left_p = 1;
15520 }
15521
15522 /* If the start of this line is the overlay arrow-position, then
15523 mark this glyph row as the one containing the overlay arrow.
15524 This is clearly a mess with variable size fonts. It would be
15525 better to let it be displayed like cursors under X. */
15526 if ((row->displays_text_p || !overlay_arrow_seen)
15527 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15528 !NILP (overlay_arrow_string)))
15529 {
15530 /* Overlay arrow in window redisplay is a fringe bitmap. */
15531 if (STRINGP (overlay_arrow_string))
15532 {
15533 struct glyph_row *arrow_row
15534 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15535 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15536 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15537 struct glyph *p = row->glyphs[TEXT_AREA];
15538 struct glyph *p2, *end;
15539
15540 /* Copy the arrow glyphs. */
15541 while (glyph < arrow_end)
15542 *p++ = *glyph++;
15543
15544 /* Throw away padding glyphs. */
15545 p2 = p;
15546 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15547 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15548 ++p2;
15549 if (p2 > p)
15550 {
15551 while (p2 < end)
15552 *p++ = *p2++;
15553 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15554 }
15555 }
15556 else
15557 {
15558 xassert (INTEGERP (overlay_arrow_string));
15559 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15560 }
15561 overlay_arrow_seen = 1;
15562 }
15563
15564 /* Compute pixel dimensions of this line. */
15565 compute_line_metrics (it);
15566
15567 /* Remember the position at which this line ends. */
15568 row->end = it->current;
15569
15570 /* Record whether this row ends inside an ellipsis. */
15571 row->ends_in_ellipsis_p
15572 = (it->method == GET_FROM_DISPLAY_VECTOR
15573 && it->ellipsis_p);
15574
15575 /* Save fringe bitmaps in this row. */
15576 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15577 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15578 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15579 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15580
15581 it->left_user_fringe_bitmap = 0;
15582 it->left_user_fringe_face_id = 0;
15583 it->right_user_fringe_bitmap = 0;
15584 it->right_user_fringe_face_id = 0;
15585
15586 /* Maybe set the cursor. */
15587 if (it->w->cursor.vpos < 0
15588 && PT >= MATRIX_ROW_START_CHARPOS (row)
15589 && PT <= MATRIX_ROW_END_CHARPOS (row)
15590 && cursor_row_p (it->w, row))
15591 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15592
15593 /* Highlight trailing whitespace. */
15594 if (!NILP (Vshow_trailing_whitespace))
15595 highlight_trailing_whitespace (it->f, it->glyph_row);
15596
15597 /* Prepare for the next line. This line starts horizontally at (X
15598 HPOS) = (0 0). Vertical positions are incremented. As a
15599 convenience for the caller, IT->glyph_row is set to the next
15600 row to be used. */
15601 it->current_x = it->hpos = 0;
15602 it->current_y += row->height;
15603 ++it->vpos;
15604 ++it->glyph_row;
15605 it->start = it->current;
15606 return row->displays_text_p;
15607 }
15608
15609
15610 \f
15611 /***********************************************************************
15612 Menu Bar
15613 ***********************************************************************/
15614
15615 /* Redisplay the menu bar in the frame for window W.
15616
15617 The menu bar of X frames that don't have X toolkit support is
15618 displayed in a special window W->frame->menu_bar_window.
15619
15620 The menu bar of terminal frames is treated specially as far as
15621 glyph matrices are concerned. Menu bar lines are not part of
15622 windows, so the update is done directly on the frame matrix rows
15623 for the menu bar. */
15624
15625 static void
15626 display_menu_bar (w)
15627 struct window *w;
15628 {
15629 struct frame *f = XFRAME (WINDOW_FRAME (w));
15630 struct it it;
15631 Lisp_Object items;
15632 int i;
15633
15634 /* Don't do all this for graphical frames. */
15635 #ifdef HAVE_NTGUI
15636 if (!NILP (Vwindow_system))
15637 return;
15638 #endif
15639 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15640 if (FRAME_X_P (f))
15641 return;
15642 #endif
15643 #ifdef MAC_OS
15644 if (FRAME_MAC_P (f))
15645 return;
15646 #endif
15647
15648 #ifdef USE_X_TOOLKIT
15649 xassert (!FRAME_WINDOW_P (f));
15650 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15651 it.first_visible_x = 0;
15652 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15653 #else /* not USE_X_TOOLKIT */
15654 if (FRAME_WINDOW_P (f))
15655 {
15656 /* Menu bar lines are displayed in the desired matrix of the
15657 dummy window menu_bar_window. */
15658 struct window *menu_w;
15659 xassert (WINDOWP (f->menu_bar_window));
15660 menu_w = XWINDOW (f->menu_bar_window);
15661 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15662 MENU_FACE_ID);
15663 it.first_visible_x = 0;
15664 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15665 }
15666 else
15667 {
15668 /* This is a TTY frame, i.e. character hpos/vpos are used as
15669 pixel x/y. */
15670 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15671 MENU_FACE_ID);
15672 it.first_visible_x = 0;
15673 it.last_visible_x = FRAME_COLS (f);
15674 }
15675 #endif /* not USE_X_TOOLKIT */
15676
15677 if (! mode_line_inverse_video)
15678 /* Force the menu-bar to be displayed in the default face. */
15679 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15680
15681 /* Clear all rows of the menu bar. */
15682 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15683 {
15684 struct glyph_row *row = it.glyph_row + i;
15685 clear_glyph_row (row);
15686 row->enabled_p = 1;
15687 row->full_width_p = 1;
15688 }
15689
15690 /* Display all items of the menu bar. */
15691 items = FRAME_MENU_BAR_ITEMS (it.f);
15692 for (i = 0; i < XVECTOR (items)->size; i += 4)
15693 {
15694 Lisp_Object string;
15695
15696 /* Stop at nil string. */
15697 string = AREF (items, i + 1);
15698 if (NILP (string))
15699 break;
15700
15701 /* Remember where item was displayed. */
15702 AREF (items, i + 3) = make_number (it.hpos);
15703
15704 /* Display the item, pad with one space. */
15705 if (it.current_x < it.last_visible_x)
15706 display_string (NULL, string, Qnil, 0, 0, &it,
15707 SCHARS (string) + 1, 0, 0, -1);
15708 }
15709
15710 /* Fill out the line with spaces. */
15711 if (it.current_x < it.last_visible_x)
15712 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15713
15714 /* Compute the total height of the lines. */
15715 compute_line_metrics (&it);
15716 }
15717
15718
15719 \f
15720 /***********************************************************************
15721 Mode Line
15722 ***********************************************************************/
15723
15724 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15725 FORCE is non-zero, redisplay mode lines unconditionally.
15726 Otherwise, redisplay only mode lines that are garbaged. Value is
15727 the number of windows whose mode lines were redisplayed. */
15728
15729 static int
15730 redisplay_mode_lines (window, force)
15731 Lisp_Object window;
15732 int force;
15733 {
15734 int nwindows = 0;
15735
15736 while (!NILP (window))
15737 {
15738 struct window *w = XWINDOW (window);
15739
15740 if (WINDOWP (w->hchild))
15741 nwindows += redisplay_mode_lines (w->hchild, force);
15742 else if (WINDOWP (w->vchild))
15743 nwindows += redisplay_mode_lines (w->vchild, force);
15744 else if (force
15745 || FRAME_GARBAGED_P (XFRAME (w->frame))
15746 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15747 {
15748 struct text_pos lpoint;
15749 struct buffer *old = current_buffer;
15750
15751 /* Set the window's buffer for the mode line display. */
15752 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15753 set_buffer_internal_1 (XBUFFER (w->buffer));
15754
15755 /* Point refers normally to the selected window. For any
15756 other window, set up appropriate value. */
15757 if (!EQ (window, selected_window))
15758 {
15759 struct text_pos pt;
15760
15761 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15762 if (CHARPOS (pt) < BEGV)
15763 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15764 else if (CHARPOS (pt) > (ZV - 1))
15765 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15766 else
15767 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15768 }
15769
15770 /* Display mode lines. */
15771 clear_glyph_matrix (w->desired_matrix);
15772 if (display_mode_lines (w))
15773 {
15774 ++nwindows;
15775 w->must_be_updated_p = 1;
15776 }
15777
15778 /* Restore old settings. */
15779 set_buffer_internal_1 (old);
15780 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15781 }
15782
15783 window = w->next;
15784 }
15785
15786 return nwindows;
15787 }
15788
15789
15790 /* Display the mode and/or top line of window W. Value is the number
15791 of mode lines displayed. */
15792
15793 static int
15794 display_mode_lines (w)
15795 struct window *w;
15796 {
15797 Lisp_Object old_selected_window, old_selected_frame;
15798 int n = 0;
15799
15800 old_selected_frame = selected_frame;
15801 selected_frame = w->frame;
15802 old_selected_window = selected_window;
15803 XSETWINDOW (selected_window, w);
15804
15805 /* These will be set while the mode line specs are processed. */
15806 line_number_displayed = 0;
15807 w->column_number_displayed = Qnil;
15808
15809 if (WINDOW_WANTS_MODELINE_P (w))
15810 {
15811 struct window *sel_w = XWINDOW (old_selected_window);
15812
15813 /* Select mode line face based on the real selected window. */
15814 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15815 current_buffer->mode_line_format);
15816 ++n;
15817 }
15818
15819 if (WINDOW_WANTS_HEADER_LINE_P (w))
15820 {
15821 display_mode_line (w, HEADER_LINE_FACE_ID,
15822 current_buffer->header_line_format);
15823 ++n;
15824 }
15825
15826 selected_frame = old_selected_frame;
15827 selected_window = old_selected_window;
15828 return n;
15829 }
15830
15831
15832 /* Display mode or top line of window W. FACE_ID specifies which line
15833 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15834 FORMAT is the mode line format to display. Value is the pixel
15835 height of the mode line displayed. */
15836
15837 static int
15838 display_mode_line (w, face_id, format)
15839 struct window *w;
15840 enum face_id face_id;
15841 Lisp_Object format;
15842 {
15843 struct it it;
15844 struct face *face;
15845 int count = SPECPDL_INDEX ();
15846
15847 init_iterator (&it, w, -1, -1, NULL, face_id);
15848 prepare_desired_row (it.glyph_row);
15849
15850 it.glyph_row->mode_line_p = 1;
15851
15852 if (! mode_line_inverse_video)
15853 /* Force the mode-line to be displayed in the default face. */
15854 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15855
15856 record_unwind_protect (unwind_format_mode_line,
15857 format_mode_line_unwind_data (NULL));
15858
15859 mode_line_target = MODE_LINE_DISPLAY;
15860
15861 /* Temporarily make frame's keyboard the current kboard so that
15862 kboard-local variables in the mode_line_format will get the right
15863 values. */
15864 push_frame_kboard (it.f);
15865 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15866 pop_frame_kboard ();
15867
15868 unbind_to (count, Qnil);
15869
15870 /* Fill up with spaces. */
15871 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15872
15873 compute_line_metrics (&it);
15874 it.glyph_row->full_width_p = 1;
15875 it.glyph_row->continued_p = 0;
15876 it.glyph_row->truncated_on_left_p = 0;
15877 it.glyph_row->truncated_on_right_p = 0;
15878
15879 /* Make a 3D mode-line have a shadow at its right end. */
15880 face = FACE_FROM_ID (it.f, face_id);
15881 extend_face_to_end_of_line (&it);
15882 if (face->box != FACE_NO_BOX)
15883 {
15884 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15885 + it.glyph_row->used[TEXT_AREA] - 1);
15886 last->right_box_line_p = 1;
15887 }
15888
15889 return it.glyph_row->height;
15890 }
15891
15892 /* Contribute ELT to the mode line for window IT->w. How it
15893 translates into text depends on its data type.
15894
15895 IT describes the display environment in which we display, as usual.
15896
15897 DEPTH is the depth in recursion. It is used to prevent
15898 infinite recursion here.
15899
15900 FIELD_WIDTH is the number of characters the display of ELT should
15901 occupy in the mode line, and PRECISION is the maximum number of
15902 characters to display from ELT's representation. See
15903 display_string for details.
15904
15905 Returns the hpos of the end of the text generated by ELT.
15906
15907 PROPS is a property list to add to any string we encounter.
15908
15909 If RISKY is nonzero, remove (disregard) any properties in any string
15910 we encounter, and ignore :eval and :propertize.
15911
15912 The global variable `mode_line_target' determines whether the
15913 output is passed to `store_mode_line_noprop',
15914 `store_mode_line_string', or `display_string'. */
15915
15916 static int
15917 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15918 struct it *it;
15919 int depth;
15920 int field_width, precision;
15921 Lisp_Object elt, props;
15922 int risky;
15923 {
15924 int n = 0, field, prec;
15925 int literal = 0;
15926
15927 tail_recurse:
15928 if (depth > 100)
15929 elt = build_string ("*too-deep*");
15930
15931 depth++;
15932
15933 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15934 {
15935 case Lisp_String:
15936 {
15937 /* A string: output it and check for %-constructs within it. */
15938 unsigned char c;
15939 const unsigned char *this, *lisp_string;
15940
15941 if (!NILP (props) || risky)
15942 {
15943 Lisp_Object oprops, aelt;
15944 oprops = Ftext_properties_at (make_number (0), elt);
15945
15946 /* If the starting string's properties are not what
15947 we want, translate the string. Also, if the string
15948 is risky, do that anyway. */
15949
15950 if (NILP (Fequal (props, oprops)) || risky)
15951 {
15952 /* If the starting string has properties,
15953 merge the specified ones onto the existing ones. */
15954 if (! NILP (oprops) && !risky)
15955 {
15956 Lisp_Object tem;
15957
15958 oprops = Fcopy_sequence (oprops);
15959 tem = props;
15960 while (CONSP (tem))
15961 {
15962 oprops = Fplist_put (oprops, XCAR (tem),
15963 XCAR (XCDR (tem)));
15964 tem = XCDR (XCDR (tem));
15965 }
15966 props = oprops;
15967 }
15968
15969 aelt = Fassoc (elt, mode_line_proptrans_alist);
15970 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15971 {
15972 mode_line_proptrans_alist
15973 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15974 elt = XCAR (aelt);
15975 }
15976 else
15977 {
15978 Lisp_Object tem;
15979
15980 elt = Fcopy_sequence (elt);
15981 Fset_text_properties (make_number (0), Flength (elt),
15982 props, elt);
15983 /* Add this item to mode_line_proptrans_alist. */
15984 mode_line_proptrans_alist
15985 = Fcons (Fcons (elt, props),
15986 mode_line_proptrans_alist);
15987 /* Truncate mode_line_proptrans_alist
15988 to at most 50 elements. */
15989 tem = Fnthcdr (make_number (50),
15990 mode_line_proptrans_alist);
15991 if (! NILP (tem))
15992 XSETCDR (tem, Qnil);
15993 }
15994 }
15995 }
15996
15997 this = SDATA (elt);
15998 lisp_string = this;
15999
16000 if (literal)
16001 {
16002 prec = precision - n;
16003 switch (mode_line_target)
16004 {
16005 case MODE_LINE_NOPROP:
16006 case MODE_LINE_TITLE:
16007 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16008 break;
16009 case MODE_LINE_STRING:
16010 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16011 break;
16012 case MODE_LINE_DISPLAY:
16013 n += display_string (NULL, elt, Qnil, 0, 0, it,
16014 0, prec, 0, STRING_MULTIBYTE (elt));
16015 break;
16016 }
16017
16018 break;
16019 }
16020
16021 while ((precision <= 0 || n < precision)
16022 && *this
16023 && (mode_line_target != MODE_LINE_DISPLAY
16024 || it->current_x < it->last_visible_x))
16025 {
16026 const unsigned char *last = this;
16027
16028 /* Advance to end of string or next format specifier. */
16029 while ((c = *this++) != '\0' && c != '%')
16030 ;
16031
16032 if (this - 1 != last)
16033 {
16034 int nchars, nbytes;
16035
16036 /* Output to end of string or up to '%'. Field width
16037 is length of string. Don't output more than
16038 PRECISION allows us. */
16039 --this;
16040
16041 prec = c_string_width (last, this - last, precision - n,
16042 &nchars, &nbytes);
16043
16044 switch (mode_line_target)
16045 {
16046 case MODE_LINE_NOPROP:
16047 case MODE_LINE_TITLE:
16048 n += store_mode_line_noprop (last, 0, prec);
16049 break;
16050 case MODE_LINE_STRING:
16051 {
16052 int bytepos = last - lisp_string;
16053 int charpos = string_byte_to_char (elt, bytepos);
16054 int endpos = (precision <= 0
16055 ? string_byte_to_char (elt,
16056 this - lisp_string)
16057 : charpos + nchars);
16058
16059 n += store_mode_line_string (NULL,
16060 Fsubstring (elt, make_number (charpos),
16061 make_number (endpos)),
16062 0, 0, 0, Qnil);
16063 }
16064 break;
16065 case MODE_LINE_DISPLAY:
16066 {
16067 int bytepos = last - lisp_string;
16068 int charpos = string_byte_to_char (elt, bytepos);
16069 n += display_string (NULL, elt, Qnil, 0, charpos,
16070 it, 0, prec, 0,
16071 STRING_MULTIBYTE (elt));
16072 }
16073 break;
16074 }
16075 }
16076 else /* c == '%' */
16077 {
16078 const unsigned char *percent_position = this;
16079
16080 /* Get the specified minimum width. Zero means
16081 don't pad. */
16082 field = 0;
16083 while ((c = *this++) >= '0' && c <= '9')
16084 field = field * 10 + c - '0';
16085
16086 /* Don't pad beyond the total padding allowed. */
16087 if (field_width - n > 0 && field > field_width - n)
16088 field = field_width - n;
16089
16090 /* Note that either PRECISION <= 0 or N < PRECISION. */
16091 prec = precision - n;
16092
16093 if (c == 'M')
16094 n += display_mode_element (it, depth, field, prec,
16095 Vglobal_mode_string, props,
16096 risky);
16097 else if (c != 0)
16098 {
16099 int multibyte;
16100 int bytepos, charpos;
16101 unsigned char *spec;
16102
16103 bytepos = percent_position - lisp_string;
16104 charpos = (STRING_MULTIBYTE (elt)
16105 ? string_byte_to_char (elt, bytepos)
16106 : bytepos);
16107
16108 spec
16109 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16110
16111 switch (mode_line_target)
16112 {
16113 case MODE_LINE_NOPROP:
16114 case MODE_LINE_TITLE:
16115 n += store_mode_line_noprop (spec, field, prec);
16116 break;
16117 case MODE_LINE_STRING:
16118 {
16119 int len = strlen (spec);
16120 Lisp_Object tem = make_string (spec, len);
16121 props = Ftext_properties_at (make_number (charpos), elt);
16122 /* Should only keep face property in props */
16123 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16124 }
16125 break;
16126 case MODE_LINE_DISPLAY:
16127 {
16128 int nglyphs_before, nwritten;
16129
16130 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16131 nwritten = display_string (spec, Qnil, elt,
16132 charpos, 0, it,
16133 field, prec, 0,
16134 multibyte);
16135
16136 /* Assign to the glyphs written above the
16137 string where the `%x' came from, position
16138 of the `%'. */
16139 if (nwritten > 0)
16140 {
16141 struct glyph *glyph
16142 = (it->glyph_row->glyphs[TEXT_AREA]
16143 + nglyphs_before);
16144 int i;
16145
16146 for (i = 0; i < nwritten; ++i)
16147 {
16148 glyph[i].object = elt;
16149 glyph[i].charpos = charpos;
16150 }
16151
16152 n += nwritten;
16153 }
16154 }
16155 break;
16156 }
16157 }
16158 else /* c == 0 */
16159 break;
16160 }
16161 }
16162 }
16163 break;
16164
16165 case Lisp_Symbol:
16166 /* A symbol: process the value of the symbol recursively
16167 as if it appeared here directly. Avoid error if symbol void.
16168 Special case: if value of symbol is a string, output the string
16169 literally. */
16170 {
16171 register Lisp_Object tem;
16172
16173 /* If the variable is not marked as risky to set
16174 then its contents are risky to use. */
16175 if (NILP (Fget (elt, Qrisky_local_variable)))
16176 risky = 1;
16177
16178 tem = Fboundp (elt);
16179 if (!NILP (tem))
16180 {
16181 tem = Fsymbol_value (elt);
16182 /* If value is a string, output that string literally:
16183 don't check for % within it. */
16184 if (STRINGP (tem))
16185 literal = 1;
16186
16187 if (!EQ (tem, elt))
16188 {
16189 /* Give up right away for nil or t. */
16190 elt = tem;
16191 goto tail_recurse;
16192 }
16193 }
16194 }
16195 break;
16196
16197 case Lisp_Cons:
16198 {
16199 register Lisp_Object car, tem;
16200
16201 /* A cons cell: five distinct cases.
16202 If first element is :eval or :propertize, do something special.
16203 If first element is a string or a cons, process all the elements
16204 and effectively concatenate them.
16205 If first element is a negative number, truncate displaying cdr to
16206 at most that many characters. If positive, pad (with spaces)
16207 to at least that many characters.
16208 If first element is a symbol, process the cadr or caddr recursively
16209 according to whether the symbol's value is non-nil or nil. */
16210 car = XCAR (elt);
16211 if (EQ (car, QCeval))
16212 {
16213 /* An element of the form (:eval FORM) means evaluate FORM
16214 and use the result as mode line elements. */
16215
16216 if (risky)
16217 break;
16218
16219 if (CONSP (XCDR (elt)))
16220 {
16221 Lisp_Object spec;
16222 spec = safe_eval (XCAR (XCDR (elt)));
16223 n += display_mode_element (it, depth, field_width - n,
16224 precision - n, spec, props,
16225 risky);
16226 }
16227 }
16228 else if (EQ (car, QCpropertize))
16229 {
16230 /* An element of the form (:propertize ELT PROPS...)
16231 means display ELT but applying properties PROPS. */
16232
16233 if (risky)
16234 break;
16235
16236 if (CONSP (XCDR (elt)))
16237 n += display_mode_element (it, depth, field_width - n,
16238 precision - n, XCAR (XCDR (elt)),
16239 XCDR (XCDR (elt)), risky);
16240 }
16241 else if (SYMBOLP (car))
16242 {
16243 tem = Fboundp (car);
16244 elt = XCDR (elt);
16245 if (!CONSP (elt))
16246 goto invalid;
16247 /* elt is now the cdr, and we know it is a cons cell.
16248 Use its car if CAR has a non-nil value. */
16249 if (!NILP (tem))
16250 {
16251 tem = Fsymbol_value (car);
16252 if (!NILP (tem))
16253 {
16254 elt = XCAR (elt);
16255 goto tail_recurse;
16256 }
16257 }
16258 /* Symbol's value is nil (or symbol is unbound)
16259 Get the cddr of the original list
16260 and if possible find the caddr and use that. */
16261 elt = XCDR (elt);
16262 if (NILP (elt))
16263 break;
16264 else if (!CONSP (elt))
16265 goto invalid;
16266 elt = XCAR (elt);
16267 goto tail_recurse;
16268 }
16269 else if (INTEGERP (car))
16270 {
16271 register int lim = XINT (car);
16272 elt = XCDR (elt);
16273 if (lim < 0)
16274 {
16275 /* Negative int means reduce maximum width. */
16276 if (precision <= 0)
16277 precision = -lim;
16278 else
16279 precision = min (precision, -lim);
16280 }
16281 else if (lim > 0)
16282 {
16283 /* Padding specified. Don't let it be more than
16284 current maximum. */
16285 if (precision > 0)
16286 lim = min (precision, lim);
16287
16288 /* If that's more padding than already wanted, queue it.
16289 But don't reduce padding already specified even if
16290 that is beyond the current truncation point. */
16291 field_width = max (lim, field_width);
16292 }
16293 goto tail_recurse;
16294 }
16295 else if (STRINGP (car) || CONSP (car))
16296 {
16297 register int limit = 50;
16298 /* Limit is to protect against circular lists. */
16299 while (CONSP (elt)
16300 && --limit > 0
16301 && (precision <= 0 || n < precision))
16302 {
16303 n += display_mode_element (it, depth,
16304 /* Do padding only after the last
16305 element in the list. */
16306 (! CONSP (XCDR (elt))
16307 ? field_width - n
16308 : 0),
16309 precision - n, XCAR (elt),
16310 props, risky);
16311 elt = XCDR (elt);
16312 }
16313 }
16314 }
16315 break;
16316
16317 default:
16318 invalid:
16319 elt = build_string ("*invalid*");
16320 goto tail_recurse;
16321 }
16322
16323 /* Pad to FIELD_WIDTH. */
16324 if (field_width > 0 && n < field_width)
16325 {
16326 switch (mode_line_target)
16327 {
16328 case MODE_LINE_NOPROP:
16329 case MODE_LINE_TITLE:
16330 n += store_mode_line_noprop ("", field_width - n, 0);
16331 break;
16332 case MODE_LINE_STRING:
16333 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16334 break;
16335 case MODE_LINE_DISPLAY:
16336 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16337 0, 0, 0);
16338 break;
16339 }
16340 }
16341
16342 return n;
16343 }
16344
16345 /* Store a mode-line string element in mode_line_string_list.
16346
16347 If STRING is non-null, display that C string. Otherwise, the Lisp
16348 string LISP_STRING is displayed.
16349
16350 FIELD_WIDTH is the minimum number of output glyphs to produce.
16351 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16352 with spaces. FIELD_WIDTH <= 0 means don't pad.
16353
16354 PRECISION is the maximum number of characters to output from
16355 STRING. PRECISION <= 0 means don't truncate the string.
16356
16357 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16358 properties to the string.
16359
16360 PROPS are the properties to add to the string.
16361 The mode_line_string_face face property is always added to the string.
16362 */
16363
16364 static int
16365 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16366 char *string;
16367 Lisp_Object lisp_string;
16368 int copy_string;
16369 int field_width;
16370 int precision;
16371 Lisp_Object props;
16372 {
16373 int len;
16374 int n = 0;
16375
16376 if (string != NULL)
16377 {
16378 len = strlen (string);
16379 if (precision > 0 && len > precision)
16380 len = precision;
16381 lisp_string = make_string (string, len);
16382 if (NILP (props))
16383 props = mode_line_string_face_prop;
16384 else if (!NILP (mode_line_string_face))
16385 {
16386 Lisp_Object face = Fplist_get (props, Qface);
16387 props = Fcopy_sequence (props);
16388 if (NILP (face))
16389 face = mode_line_string_face;
16390 else
16391 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16392 props = Fplist_put (props, Qface, face);
16393 }
16394 Fadd_text_properties (make_number (0), make_number (len),
16395 props, lisp_string);
16396 }
16397 else
16398 {
16399 len = XFASTINT (Flength (lisp_string));
16400 if (precision > 0 && len > precision)
16401 {
16402 len = precision;
16403 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16404 precision = -1;
16405 }
16406 if (!NILP (mode_line_string_face))
16407 {
16408 Lisp_Object face;
16409 if (NILP (props))
16410 props = Ftext_properties_at (make_number (0), lisp_string);
16411 face = Fplist_get (props, Qface);
16412 if (NILP (face))
16413 face = mode_line_string_face;
16414 else
16415 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16416 props = Fcons (Qface, Fcons (face, Qnil));
16417 if (copy_string)
16418 lisp_string = Fcopy_sequence (lisp_string);
16419 }
16420 if (!NILP (props))
16421 Fadd_text_properties (make_number (0), make_number (len),
16422 props, lisp_string);
16423 }
16424
16425 if (len > 0)
16426 {
16427 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16428 n += len;
16429 }
16430
16431 if (field_width > len)
16432 {
16433 field_width -= len;
16434 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16435 if (!NILP (props))
16436 Fadd_text_properties (make_number (0), make_number (field_width),
16437 props, lisp_string);
16438 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16439 n += field_width;
16440 }
16441
16442 return n;
16443 }
16444
16445
16446 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16447 1, 4, 0,
16448 doc: /* Format a string out of a mode line format specification.
16449 First arg FORMAT specifies the mode line format (see `mode-line-format'
16450 for details) to use.
16451
16452 Optional second arg FACE specifies the face property to put
16453 on all characters for which no face is specified.
16454 t means whatever face the window's mode line currently uses
16455 \(either `mode-line' or `mode-line-inactive', depending).
16456 nil means the default is no face property.
16457 If FACE is an integer, the value string has no text properties.
16458
16459 Optional third and fourth args WINDOW and BUFFER specify the window
16460 and buffer to use as the context for the formatting (defaults
16461 are the selected window and the window's buffer). */)
16462 (format, face, window, buffer)
16463 Lisp_Object format, face, window, buffer;
16464 {
16465 struct it it;
16466 int len;
16467 struct window *w;
16468 struct buffer *old_buffer = NULL;
16469 int face_id = -1;
16470 int no_props = INTEGERP (face);
16471 int count = SPECPDL_INDEX ();
16472 Lisp_Object str;
16473 int string_start = 0;
16474
16475 if (NILP (window))
16476 window = selected_window;
16477 CHECK_WINDOW (window);
16478 w = XWINDOW (window);
16479
16480 if (NILP (buffer))
16481 buffer = w->buffer;
16482 CHECK_BUFFER (buffer);
16483
16484 if (NILP (format))
16485 return build_string ("");
16486
16487 if (no_props)
16488 face = Qnil;
16489
16490 if (!NILP (face))
16491 {
16492 if (EQ (face, Qt))
16493 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16494 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16495 }
16496
16497 if (face_id < 0)
16498 face_id = DEFAULT_FACE_ID;
16499
16500 if (XBUFFER (buffer) != current_buffer)
16501 old_buffer = current_buffer;
16502
16503 record_unwind_protect (unwind_format_mode_line,
16504 format_mode_line_unwind_data (old_buffer));
16505
16506 if (old_buffer)
16507 set_buffer_internal_1 (XBUFFER (buffer));
16508
16509 init_iterator (&it, w, -1, -1, NULL, face_id);
16510
16511 if (no_props)
16512 {
16513 mode_line_target = MODE_LINE_NOPROP;
16514 mode_line_string_face_prop = Qnil;
16515 mode_line_string_list = Qnil;
16516 string_start = MODE_LINE_NOPROP_LEN (0);
16517 }
16518 else
16519 {
16520 mode_line_target = MODE_LINE_STRING;
16521 mode_line_string_list = Qnil;
16522 mode_line_string_face = face;
16523 mode_line_string_face_prop
16524 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16525 }
16526
16527 push_frame_kboard (it.f);
16528 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16529 pop_frame_kboard ();
16530
16531 if (no_props)
16532 {
16533 len = MODE_LINE_NOPROP_LEN (string_start);
16534 str = make_string (mode_line_noprop_buf + string_start, len);
16535 }
16536 else
16537 {
16538 mode_line_string_list = Fnreverse (mode_line_string_list);
16539 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16540 make_string ("", 0));
16541 }
16542
16543 unbind_to (count, Qnil);
16544 return str;
16545 }
16546
16547 /* Write a null-terminated, right justified decimal representation of
16548 the positive integer D to BUF using a minimal field width WIDTH. */
16549
16550 static void
16551 pint2str (buf, width, d)
16552 register char *buf;
16553 register int width;
16554 register int d;
16555 {
16556 register char *p = buf;
16557
16558 if (d <= 0)
16559 *p++ = '0';
16560 else
16561 {
16562 while (d > 0)
16563 {
16564 *p++ = d % 10 + '0';
16565 d /= 10;
16566 }
16567 }
16568
16569 for (width -= (int) (p - buf); width > 0; --width)
16570 *p++ = ' ';
16571 *p-- = '\0';
16572 while (p > buf)
16573 {
16574 d = *buf;
16575 *buf++ = *p;
16576 *p-- = d;
16577 }
16578 }
16579
16580 /* Write a null-terminated, right justified decimal and "human
16581 readable" representation of the nonnegative integer D to BUF using
16582 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16583
16584 static const char power_letter[] =
16585 {
16586 0, /* not used */
16587 'k', /* kilo */
16588 'M', /* mega */
16589 'G', /* giga */
16590 'T', /* tera */
16591 'P', /* peta */
16592 'E', /* exa */
16593 'Z', /* zetta */
16594 'Y' /* yotta */
16595 };
16596
16597 static void
16598 pint2hrstr (buf, width, d)
16599 char *buf;
16600 int width;
16601 int d;
16602 {
16603 /* We aim to represent the nonnegative integer D as
16604 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16605 int quotient = d;
16606 int remainder = 0;
16607 /* -1 means: do not use TENTHS. */
16608 int tenths = -1;
16609 int exponent = 0;
16610
16611 /* Length of QUOTIENT.TENTHS as a string. */
16612 int length;
16613
16614 char * psuffix;
16615 char * p;
16616
16617 if (1000 <= quotient)
16618 {
16619 /* Scale to the appropriate EXPONENT. */
16620 do
16621 {
16622 remainder = quotient % 1000;
16623 quotient /= 1000;
16624 exponent++;
16625 }
16626 while (1000 <= quotient);
16627
16628 /* Round to nearest and decide whether to use TENTHS or not. */
16629 if (quotient <= 9)
16630 {
16631 tenths = remainder / 100;
16632 if (50 <= remainder % 100)
16633 {
16634 if (tenths < 9)
16635 tenths++;
16636 else
16637 {
16638 quotient++;
16639 if (quotient == 10)
16640 tenths = -1;
16641 else
16642 tenths = 0;
16643 }
16644 }
16645 }
16646 else
16647 if (500 <= remainder)
16648 {
16649 if (quotient < 999)
16650 quotient++;
16651 else
16652 {
16653 quotient = 1;
16654 exponent++;
16655 tenths = 0;
16656 }
16657 }
16658 }
16659
16660 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16661 if (tenths == -1 && quotient <= 99)
16662 if (quotient <= 9)
16663 length = 1;
16664 else
16665 length = 2;
16666 else
16667 length = 3;
16668 p = psuffix = buf + max (width, length);
16669
16670 /* Print EXPONENT. */
16671 if (exponent)
16672 *psuffix++ = power_letter[exponent];
16673 *psuffix = '\0';
16674
16675 /* Print TENTHS. */
16676 if (tenths >= 0)
16677 {
16678 *--p = '0' + tenths;
16679 *--p = '.';
16680 }
16681
16682 /* Print QUOTIENT. */
16683 do
16684 {
16685 int digit = quotient % 10;
16686 *--p = '0' + digit;
16687 }
16688 while ((quotient /= 10) != 0);
16689
16690 /* Print leading spaces. */
16691 while (buf < p)
16692 *--p = ' ';
16693 }
16694
16695 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16696 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16697 type of CODING_SYSTEM. Return updated pointer into BUF. */
16698
16699 static unsigned char invalid_eol_type[] = "(*invalid*)";
16700
16701 static char *
16702 decode_mode_spec_coding (coding_system, buf, eol_flag)
16703 Lisp_Object coding_system;
16704 register char *buf;
16705 int eol_flag;
16706 {
16707 Lisp_Object val;
16708 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16709 const unsigned char *eol_str;
16710 int eol_str_len;
16711 /* The EOL conversion we are using. */
16712 Lisp_Object eoltype;
16713
16714 val = Fget (coding_system, Qcoding_system);
16715 eoltype = Qnil;
16716
16717 if (!VECTORP (val)) /* Not yet decided. */
16718 {
16719 if (multibyte)
16720 *buf++ = '-';
16721 if (eol_flag)
16722 eoltype = eol_mnemonic_undecided;
16723 /* Don't mention EOL conversion if it isn't decided. */
16724 }
16725 else
16726 {
16727 Lisp_Object eolvalue;
16728
16729 eolvalue = Fget (coding_system, Qeol_type);
16730
16731 if (multibyte)
16732 *buf++ = XFASTINT (AREF (val, 1));
16733
16734 if (eol_flag)
16735 {
16736 /* The EOL conversion that is normal on this system. */
16737
16738 if (NILP (eolvalue)) /* Not yet decided. */
16739 eoltype = eol_mnemonic_undecided;
16740 else if (VECTORP (eolvalue)) /* Not yet decided. */
16741 eoltype = eol_mnemonic_undecided;
16742 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16743 eoltype = (XFASTINT (eolvalue) == 0
16744 ? eol_mnemonic_unix
16745 : (XFASTINT (eolvalue) == 1
16746 ? eol_mnemonic_dos : eol_mnemonic_mac));
16747 }
16748 }
16749
16750 if (eol_flag)
16751 {
16752 /* Mention the EOL conversion if it is not the usual one. */
16753 if (STRINGP (eoltype))
16754 {
16755 eol_str = SDATA (eoltype);
16756 eol_str_len = SBYTES (eoltype);
16757 }
16758 else if (INTEGERP (eoltype)
16759 && CHAR_VALID_P (XINT (eoltype), 0))
16760 {
16761 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16762 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16763 eol_str = tmp;
16764 }
16765 else
16766 {
16767 eol_str = invalid_eol_type;
16768 eol_str_len = sizeof (invalid_eol_type) - 1;
16769 }
16770 bcopy (eol_str, buf, eol_str_len);
16771 buf += eol_str_len;
16772 }
16773
16774 return buf;
16775 }
16776
16777 /* Return a string for the output of a mode line %-spec for window W,
16778 generated by character C. PRECISION >= 0 means don't return a
16779 string longer than that value. FIELD_WIDTH > 0 means pad the
16780 string returned with spaces to that value. Return 1 in *MULTIBYTE
16781 if the result is multibyte text.
16782
16783 Note we operate on the current buffer for most purposes,
16784 the exception being w->base_line_pos. */
16785
16786 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16787
16788 static char *
16789 decode_mode_spec (w, c, field_width, precision, multibyte)
16790 struct window *w;
16791 register int c;
16792 int field_width, precision;
16793 int *multibyte;
16794 {
16795 Lisp_Object obj;
16796 struct frame *f = XFRAME (WINDOW_FRAME (w));
16797 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16798 struct buffer *b = current_buffer;
16799
16800 obj = Qnil;
16801 *multibyte = 0;
16802
16803 switch (c)
16804 {
16805 case '*':
16806 if (!NILP (b->read_only))
16807 return "%";
16808 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16809 return "*";
16810 return "-";
16811
16812 case '+':
16813 /* This differs from %* only for a modified read-only buffer. */
16814 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16815 return "*";
16816 if (!NILP (b->read_only))
16817 return "%";
16818 return "-";
16819
16820 case '&':
16821 /* This differs from %* in ignoring read-only-ness. */
16822 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16823 return "*";
16824 return "-";
16825
16826 case '%':
16827 return "%";
16828
16829 case '[':
16830 {
16831 int i;
16832 char *p;
16833
16834 if (command_loop_level > 5)
16835 return "[[[... ";
16836 p = decode_mode_spec_buf;
16837 for (i = 0; i < command_loop_level; i++)
16838 *p++ = '[';
16839 *p = 0;
16840 return decode_mode_spec_buf;
16841 }
16842
16843 case ']':
16844 {
16845 int i;
16846 char *p;
16847
16848 if (command_loop_level > 5)
16849 return " ...]]]";
16850 p = decode_mode_spec_buf;
16851 for (i = 0; i < command_loop_level; i++)
16852 *p++ = ']';
16853 *p = 0;
16854 return decode_mode_spec_buf;
16855 }
16856
16857 case '-':
16858 {
16859 register int i;
16860
16861 /* Let lots_of_dashes be a string of infinite length. */
16862 if (mode_line_target == MODE_LINE_NOPROP ||
16863 mode_line_target == MODE_LINE_STRING)
16864 return "--";
16865 if (field_width <= 0
16866 || field_width > sizeof (lots_of_dashes))
16867 {
16868 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16869 decode_mode_spec_buf[i] = '-';
16870 decode_mode_spec_buf[i] = '\0';
16871 return decode_mode_spec_buf;
16872 }
16873 else
16874 return lots_of_dashes;
16875 }
16876
16877 case 'b':
16878 obj = b->name;
16879 break;
16880
16881 case 'c':
16882 {
16883 int col = (int) current_column (); /* iftc */
16884 w->column_number_displayed = make_number (col);
16885 pint2str (decode_mode_spec_buf, field_width, col);
16886 return decode_mode_spec_buf;
16887 }
16888
16889 case 'F':
16890 /* %F displays the frame name. */
16891 if (!NILP (f->title))
16892 return (char *) SDATA (f->title);
16893 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16894 return (char *) SDATA (f->name);
16895 return "Emacs";
16896
16897 case 'f':
16898 obj = b->filename;
16899 break;
16900
16901 case 'i':
16902 {
16903 int size = ZV - BEGV;
16904 pint2str (decode_mode_spec_buf, field_width, size);
16905 return decode_mode_spec_buf;
16906 }
16907
16908 case 'I':
16909 {
16910 int size = ZV - BEGV;
16911 pint2hrstr (decode_mode_spec_buf, field_width, size);
16912 return decode_mode_spec_buf;
16913 }
16914
16915 case 'l':
16916 {
16917 int startpos = XMARKER (w->start)->charpos;
16918 int startpos_byte = marker_byte_position (w->start);
16919 int line, linepos, linepos_byte, topline;
16920 int nlines, junk;
16921 int height = WINDOW_TOTAL_LINES (w);
16922
16923 /* If we decided that this buffer isn't suitable for line numbers,
16924 don't forget that too fast. */
16925 if (EQ (w->base_line_pos, w->buffer))
16926 goto no_value;
16927 /* But do forget it, if the window shows a different buffer now. */
16928 else if (BUFFERP (w->base_line_pos))
16929 w->base_line_pos = Qnil;
16930
16931 /* If the buffer is very big, don't waste time. */
16932 if (INTEGERP (Vline_number_display_limit)
16933 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16934 {
16935 w->base_line_pos = Qnil;
16936 w->base_line_number = Qnil;
16937 goto no_value;
16938 }
16939
16940 if (!NILP (w->base_line_number)
16941 && !NILP (w->base_line_pos)
16942 && XFASTINT (w->base_line_pos) <= startpos)
16943 {
16944 line = XFASTINT (w->base_line_number);
16945 linepos = XFASTINT (w->base_line_pos);
16946 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16947 }
16948 else
16949 {
16950 line = 1;
16951 linepos = BUF_BEGV (b);
16952 linepos_byte = BUF_BEGV_BYTE (b);
16953 }
16954
16955 /* Count lines from base line to window start position. */
16956 nlines = display_count_lines (linepos, linepos_byte,
16957 startpos_byte,
16958 startpos, &junk);
16959
16960 topline = nlines + line;
16961
16962 /* Determine a new base line, if the old one is too close
16963 or too far away, or if we did not have one.
16964 "Too close" means it's plausible a scroll-down would
16965 go back past it. */
16966 if (startpos == BUF_BEGV (b))
16967 {
16968 w->base_line_number = make_number (topline);
16969 w->base_line_pos = make_number (BUF_BEGV (b));
16970 }
16971 else if (nlines < height + 25 || nlines > height * 3 + 50
16972 || linepos == BUF_BEGV (b))
16973 {
16974 int limit = BUF_BEGV (b);
16975 int limit_byte = BUF_BEGV_BYTE (b);
16976 int position;
16977 int distance = (height * 2 + 30) * line_number_display_limit_width;
16978
16979 if (startpos - distance > limit)
16980 {
16981 limit = startpos - distance;
16982 limit_byte = CHAR_TO_BYTE (limit);
16983 }
16984
16985 nlines = display_count_lines (startpos, startpos_byte,
16986 limit_byte,
16987 - (height * 2 + 30),
16988 &position);
16989 /* If we couldn't find the lines we wanted within
16990 line_number_display_limit_width chars per line,
16991 give up on line numbers for this window. */
16992 if (position == limit_byte && limit == startpos - distance)
16993 {
16994 w->base_line_pos = w->buffer;
16995 w->base_line_number = Qnil;
16996 goto no_value;
16997 }
16998
16999 w->base_line_number = make_number (topline - nlines);
17000 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17001 }
17002
17003 /* Now count lines from the start pos to point. */
17004 nlines = display_count_lines (startpos, startpos_byte,
17005 PT_BYTE, PT, &junk);
17006
17007 /* Record that we did display the line number. */
17008 line_number_displayed = 1;
17009
17010 /* Make the string to show. */
17011 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17012 return decode_mode_spec_buf;
17013 no_value:
17014 {
17015 char* p = decode_mode_spec_buf;
17016 int pad = field_width - 2;
17017 while (pad-- > 0)
17018 *p++ = ' ';
17019 *p++ = '?';
17020 *p++ = '?';
17021 *p = '\0';
17022 return decode_mode_spec_buf;
17023 }
17024 }
17025 break;
17026
17027 case 'm':
17028 obj = b->mode_name;
17029 break;
17030
17031 case 'n':
17032 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17033 return " Narrow";
17034 break;
17035
17036 case 'p':
17037 {
17038 int pos = marker_position (w->start);
17039 int total = BUF_ZV (b) - BUF_BEGV (b);
17040
17041 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17042 {
17043 if (pos <= BUF_BEGV (b))
17044 return "All";
17045 else
17046 return "Bottom";
17047 }
17048 else if (pos <= BUF_BEGV (b))
17049 return "Top";
17050 else
17051 {
17052 if (total > 1000000)
17053 /* Do it differently for a large value, to avoid overflow. */
17054 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17055 else
17056 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17057 /* We can't normally display a 3-digit number,
17058 so get us a 2-digit number that is close. */
17059 if (total == 100)
17060 total = 99;
17061 sprintf (decode_mode_spec_buf, "%2d%%", total);
17062 return decode_mode_spec_buf;
17063 }
17064 }
17065
17066 /* Display percentage of size above the bottom of the screen. */
17067 case 'P':
17068 {
17069 int toppos = marker_position (w->start);
17070 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17071 int total = BUF_ZV (b) - BUF_BEGV (b);
17072
17073 if (botpos >= BUF_ZV (b))
17074 {
17075 if (toppos <= BUF_BEGV (b))
17076 return "All";
17077 else
17078 return "Bottom";
17079 }
17080 else
17081 {
17082 if (total > 1000000)
17083 /* Do it differently for a large value, to avoid overflow. */
17084 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17085 else
17086 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17087 /* We can't normally display a 3-digit number,
17088 so get us a 2-digit number that is close. */
17089 if (total == 100)
17090 total = 99;
17091 if (toppos <= BUF_BEGV (b))
17092 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17093 else
17094 sprintf (decode_mode_spec_buf, "%2d%%", total);
17095 return decode_mode_spec_buf;
17096 }
17097 }
17098
17099 case 's':
17100 /* status of process */
17101 obj = Fget_buffer_process (Fcurrent_buffer ());
17102 if (NILP (obj))
17103 return "no process";
17104 #ifdef subprocesses
17105 obj = Fsymbol_name (Fprocess_status (obj));
17106 #endif
17107 break;
17108
17109 case 't': /* indicate TEXT or BINARY */
17110 #ifdef MODE_LINE_BINARY_TEXT
17111 return MODE_LINE_BINARY_TEXT (b);
17112 #else
17113 return "T";
17114 #endif
17115
17116 case 'z':
17117 /* coding-system (not including end-of-line format) */
17118 case 'Z':
17119 /* coding-system (including end-of-line type) */
17120 {
17121 int eol_flag = (c == 'Z');
17122 char *p = decode_mode_spec_buf;
17123
17124 if (! FRAME_WINDOW_P (f))
17125 {
17126 /* No need to mention EOL here--the terminal never needs
17127 to do EOL conversion. */
17128 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17129 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17130 }
17131 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17132 p, eol_flag);
17133
17134 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17135 #ifdef subprocesses
17136 obj = Fget_buffer_process (Fcurrent_buffer ());
17137 if (PROCESSP (obj))
17138 {
17139 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17140 p, eol_flag);
17141 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17142 p, eol_flag);
17143 }
17144 #endif /* subprocesses */
17145 #endif /* 0 */
17146 *p = 0;
17147 return decode_mode_spec_buf;
17148 }
17149 }
17150
17151 if (STRINGP (obj))
17152 {
17153 *multibyte = STRING_MULTIBYTE (obj);
17154 return (char *) SDATA (obj);
17155 }
17156 else
17157 return "";
17158 }
17159
17160
17161 /* Count up to COUNT lines starting from START / START_BYTE.
17162 But don't go beyond LIMIT_BYTE.
17163 Return the number of lines thus found (always nonnegative).
17164
17165 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17166
17167 static int
17168 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17169 int start, start_byte, limit_byte, count;
17170 int *byte_pos_ptr;
17171 {
17172 register unsigned char *cursor;
17173 unsigned char *base;
17174
17175 register int ceiling;
17176 register unsigned char *ceiling_addr;
17177 int orig_count = count;
17178
17179 /* If we are not in selective display mode,
17180 check only for newlines. */
17181 int selective_display = (!NILP (current_buffer->selective_display)
17182 && !INTEGERP (current_buffer->selective_display));
17183
17184 if (count > 0)
17185 {
17186 while (start_byte < limit_byte)
17187 {
17188 ceiling = BUFFER_CEILING_OF (start_byte);
17189 ceiling = min (limit_byte - 1, ceiling);
17190 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17191 base = (cursor = BYTE_POS_ADDR (start_byte));
17192 while (1)
17193 {
17194 if (selective_display)
17195 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17196 ;
17197 else
17198 while (*cursor != '\n' && ++cursor != ceiling_addr)
17199 ;
17200
17201 if (cursor != ceiling_addr)
17202 {
17203 if (--count == 0)
17204 {
17205 start_byte += cursor - base + 1;
17206 *byte_pos_ptr = start_byte;
17207 return orig_count;
17208 }
17209 else
17210 if (++cursor == ceiling_addr)
17211 break;
17212 }
17213 else
17214 break;
17215 }
17216 start_byte += cursor - base;
17217 }
17218 }
17219 else
17220 {
17221 while (start_byte > limit_byte)
17222 {
17223 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17224 ceiling = max (limit_byte, ceiling);
17225 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17226 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17227 while (1)
17228 {
17229 if (selective_display)
17230 while (--cursor != ceiling_addr
17231 && *cursor != '\n' && *cursor != 015)
17232 ;
17233 else
17234 while (--cursor != ceiling_addr && *cursor != '\n')
17235 ;
17236
17237 if (cursor != ceiling_addr)
17238 {
17239 if (++count == 0)
17240 {
17241 start_byte += cursor - base + 1;
17242 *byte_pos_ptr = start_byte;
17243 /* When scanning backwards, we should
17244 not count the newline posterior to which we stop. */
17245 return - orig_count - 1;
17246 }
17247 }
17248 else
17249 break;
17250 }
17251 /* Here we add 1 to compensate for the last decrement
17252 of CURSOR, which took it past the valid range. */
17253 start_byte += cursor - base + 1;
17254 }
17255 }
17256
17257 *byte_pos_ptr = limit_byte;
17258
17259 if (count < 0)
17260 return - orig_count + count;
17261 return orig_count - count;
17262
17263 }
17264
17265
17266 \f
17267 /***********************************************************************
17268 Displaying strings
17269 ***********************************************************************/
17270
17271 /* Display a NUL-terminated string, starting with index START.
17272
17273 If STRING is non-null, display that C string. Otherwise, the Lisp
17274 string LISP_STRING is displayed.
17275
17276 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17277 FACE_STRING. Display STRING or LISP_STRING with the face at
17278 FACE_STRING_POS in FACE_STRING:
17279
17280 Display the string in the environment given by IT, but use the
17281 standard display table, temporarily.
17282
17283 FIELD_WIDTH is the minimum number of output glyphs to produce.
17284 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17285 with spaces. If STRING has more characters, more than FIELD_WIDTH
17286 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17287
17288 PRECISION is the maximum number of characters to output from
17289 STRING. PRECISION < 0 means don't truncate the string.
17290
17291 This is roughly equivalent to printf format specifiers:
17292
17293 FIELD_WIDTH PRECISION PRINTF
17294 ----------------------------------------
17295 -1 -1 %s
17296 -1 10 %.10s
17297 10 -1 %10s
17298 20 10 %20.10s
17299
17300 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17301 display them, and < 0 means obey the current buffer's value of
17302 enable_multibyte_characters.
17303
17304 Value is the number of glyphs produced. */
17305
17306 static int
17307 display_string (string, lisp_string, face_string, face_string_pos,
17308 start, it, field_width, precision, max_x, multibyte)
17309 unsigned char *string;
17310 Lisp_Object lisp_string;
17311 Lisp_Object face_string;
17312 int face_string_pos;
17313 int start;
17314 struct it *it;
17315 int field_width, precision, max_x;
17316 int multibyte;
17317 {
17318 int hpos_at_start = it->hpos;
17319 int saved_face_id = it->face_id;
17320 struct glyph_row *row = it->glyph_row;
17321
17322 /* Initialize the iterator IT for iteration over STRING beginning
17323 with index START. */
17324 reseat_to_string (it, string, lisp_string, start,
17325 precision, field_width, multibyte);
17326
17327 /* If displaying STRING, set up the face of the iterator
17328 from LISP_STRING, if that's given. */
17329 if (STRINGP (face_string))
17330 {
17331 int endptr;
17332 struct face *face;
17333
17334 it->face_id
17335 = face_at_string_position (it->w, face_string, face_string_pos,
17336 0, it->region_beg_charpos,
17337 it->region_end_charpos,
17338 &endptr, it->base_face_id, 0);
17339 face = FACE_FROM_ID (it->f, it->face_id);
17340 it->face_box_p = face->box != FACE_NO_BOX;
17341 }
17342
17343 /* Set max_x to the maximum allowed X position. Don't let it go
17344 beyond the right edge of the window. */
17345 if (max_x <= 0)
17346 max_x = it->last_visible_x;
17347 else
17348 max_x = min (max_x, it->last_visible_x);
17349
17350 /* Skip over display elements that are not visible. because IT->w is
17351 hscrolled. */
17352 if (it->current_x < it->first_visible_x)
17353 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17354 MOVE_TO_POS | MOVE_TO_X);
17355
17356 row->ascent = it->max_ascent;
17357 row->height = it->max_ascent + it->max_descent;
17358 row->phys_ascent = it->max_phys_ascent;
17359 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17360 row->extra_line_spacing = it->max_extra_line_spacing;
17361
17362 /* This condition is for the case that we are called with current_x
17363 past last_visible_x. */
17364 while (it->current_x < max_x)
17365 {
17366 int x_before, x, n_glyphs_before, i, nglyphs;
17367
17368 /* Get the next display element. */
17369 if (!get_next_display_element (it))
17370 break;
17371
17372 /* Produce glyphs. */
17373 x_before = it->current_x;
17374 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17375 PRODUCE_GLYPHS (it);
17376
17377 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17378 i = 0;
17379 x = x_before;
17380 while (i < nglyphs)
17381 {
17382 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17383
17384 if (!it->truncate_lines_p
17385 && x + glyph->pixel_width > max_x)
17386 {
17387 /* End of continued line or max_x reached. */
17388 if (CHAR_GLYPH_PADDING_P (*glyph))
17389 {
17390 /* A wide character is unbreakable. */
17391 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17392 it->current_x = x_before;
17393 }
17394 else
17395 {
17396 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17397 it->current_x = x;
17398 }
17399 break;
17400 }
17401 else if (x + glyph->pixel_width > it->first_visible_x)
17402 {
17403 /* Glyph is at least partially visible. */
17404 ++it->hpos;
17405 if (x < it->first_visible_x)
17406 it->glyph_row->x = x - it->first_visible_x;
17407 }
17408 else
17409 {
17410 /* Glyph is off the left margin of the display area.
17411 Should not happen. */
17412 abort ();
17413 }
17414
17415 row->ascent = max (row->ascent, it->max_ascent);
17416 row->height = max (row->height, it->max_ascent + it->max_descent);
17417 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17418 row->phys_height = max (row->phys_height,
17419 it->max_phys_ascent + it->max_phys_descent);
17420 row->extra_line_spacing = max (row->extra_line_spacing,
17421 it->max_extra_line_spacing);
17422 x += glyph->pixel_width;
17423 ++i;
17424 }
17425
17426 /* Stop if max_x reached. */
17427 if (i < nglyphs)
17428 break;
17429
17430 /* Stop at line ends. */
17431 if (ITERATOR_AT_END_OF_LINE_P (it))
17432 {
17433 it->continuation_lines_width = 0;
17434 break;
17435 }
17436
17437 set_iterator_to_next (it, 1);
17438
17439 /* Stop if truncating at the right edge. */
17440 if (it->truncate_lines_p
17441 && it->current_x >= it->last_visible_x)
17442 {
17443 /* Add truncation mark, but don't do it if the line is
17444 truncated at a padding space. */
17445 if (IT_CHARPOS (*it) < it->string_nchars)
17446 {
17447 if (!FRAME_WINDOW_P (it->f))
17448 {
17449 int i, n;
17450
17451 if (it->current_x > it->last_visible_x)
17452 {
17453 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17454 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17455 break;
17456 for (n = row->used[TEXT_AREA]; i < n; ++i)
17457 {
17458 row->used[TEXT_AREA] = i;
17459 produce_special_glyphs (it, IT_TRUNCATION);
17460 }
17461 }
17462 produce_special_glyphs (it, IT_TRUNCATION);
17463 }
17464 it->glyph_row->truncated_on_right_p = 1;
17465 }
17466 break;
17467 }
17468 }
17469
17470 /* Maybe insert a truncation at the left. */
17471 if (it->first_visible_x
17472 && IT_CHARPOS (*it) > 0)
17473 {
17474 if (!FRAME_WINDOW_P (it->f))
17475 insert_left_trunc_glyphs (it);
17476 it->glyph_row->truncated_on_left_p = 1;
17477 }
17478
17479 it->face_id = saved_face_id;
17480
17481 /* Value is number of columns displayed. */
17482 return it->hpos - hpos_at_start;
17483 }
17484
17485
17486 \f
17487 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17488 appears as an element of LIST or as the car of an element of LIST.
17489 If PROPVAL is a list, compare each element against LIST in that
17490 way, and return 1/2 if any element of PROPVAL is found in LIST.
17491 Otherwise return 0. This function cannot quit.
17492 The return value is 2 if the text is invisible but with an ellipsis
17493 and 1 if it's invisible and without an ellipsis. */
17494
17495 int
17496 invisible_p (propval, list)
17497 register Lisp_Object propval;
17498 Lisp_Object list;
17499 {
17500 register Lisp_Object tail, proptail;
17501
17502 for (tail = list; CONSP (tail); tail = XCDR (tail))
17503 {
17504 register Lisp_Object tem;
17505 tem = XCAR (tail);
17506 if (EQ (propval, tem))
17507 return 1;
17508 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17509 return NILP (XCDR (tem)) ? 1 : 2;
17510 }
17511
17512 if (CONSP (propval))
17513 {
17514 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17515 {
17516 Lisp_Object propelt;
17517 propelt = XCAR (proptail);
17518 for (tail = list; CONSP (tail); tail = XCDR (tail))
17519 {
17520 register Lisp_Object tem;
17521 tem = XCAR (tail);
17522 if (EQ (propelt, tem))
17523 return 1;
17524 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17525 return NILP (XCDR (tem)) ? 1 : 2;
17526 }
17527 }
17528 }
17529
17530 return 0;
17531 }
17532
17533 /* Calculate a width or height in pixels from a specification using
17534 the following elements:
17535
17536 SPEC ::=
17537 NUM - a (fractional) multiple of the default font width/height
17538 (NUM) - specifies exactly NUM pixels
17539 UNIT - a fixed number of pixels, see below.
17540 ELEMENT - size of a display element in pixels, see below.
17541 (NUM . SPEC) - equals NUM * SPEC
17542 (+ SPEC SPEC ...) - add pixel values
17543 (- SPEC SPEC ...) - subtract pixel values
17544 (- SPEC) - negate pixel value
17545
17546 NUM ::=
17547 INT or FLOAT - a number constant
17548 SYMBOL - use symbol's (buffer local) variable binding.
17549
17550 UNIT ::=
17551 in - pixels per inch *)
17552 mm - pixels per 1/1000 meter *)
17553 cm - pixels per 1/100 meter *)
17554 width - width of current font in pixels.
17555 height - height of current font in pixels.
17556
17557 *) using the ratio(s) defined in display-pixels-per-inch.
17558
17559 ELEMENT ::=
17560
17561 left-fringe - left fringe width in pixels
17562 right-fringe - right fringe width in pixels
17563
17564 left-margin - left margin width in pixels
17565 right-margin - right margin width in pixels
17566
17567 scroll-bar - scroll-bar area width in pixels
17568
17569 Examples:
17570
17571 Pixels corresponding to 5 inches:
17572 (5 . in)
17573
17574 Total width of non-text areas on left side of window (if scroll-bar is on left):
17575 '(space :width (+ left-fringe left-margin scroll-bar))
17576
17577 Align to first text column (in header line):
17578 '(space :align-to 0)
17579
17580 Align to middle of text area minus half the width of variable `my-image'
17581 containing a loaded image:
17582 '(space :align-to (0.5 . (- text my-image)))
17583
17584 Width of left margin minus width of 1 character in the default font:
17585 '(space :width (- left-margin 1))
17586
17587 Width of left margin minus width of 2 characters in the current font:
17588 '(space :width (- left-margin (2 . width)))
17589
17590 Center 1 character over left-margin (in header line):
17591 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17592
17593 Different ways to express width of left fringe plus left margin minus one pixel:
17594 '(space :width (- (+ left-fringe left-margin) (1)))
17595 '(space :width (+ left-fringe left-margin (- (1))))
17596 '(space :width (+ left-fringe left-margin (-1)))
17597
17598 */
17599
17600 #define NUMVAL(X) \
17601 ((INTEGERP (X) || FLOATP (X)) \
17602 ? XFLOATINT (X) \
17603 : - 1)
17604
17605 int
17606 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17607 double *res;
17608 struct it *it;
17609 Lisp_Object prop;
17610 void *font;
17611 int width_p, *align_to;
17612 {
17613 double pixels;
17614
17615 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17616 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17617
17618 if (NILP (prop))
17619 return OK_PIXELS (0);
17620
17621 if (SYMBOLP (prop))
17622 {
17623 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17624 {
17625 char *unit = SDATA (SYMBOL_NAME (prop));
17626
17627 if (unit[0] == 'i' && unit[1] == 'n')
17628 pixels = 1.0;
17629 else if (unit[0] == 'm' && unit[1] == 'm')
17630 pixels = 25.4;
17631 else if (unit[0] == 'c' && unit[1] == 'm')
17632 pixels = 2.54;
17633 else
17634 pixels = 0;
17635 if (pixels > 0)
17636 {
17637 double ppi;
17638 #ifdef HAVE_WINDOW_SYSTEM
17639 if (FRAME_WINDOW_P (it->f)
17640 && (ppi = (width_p
17641 ? FRAME_X_DISPLAY_INFO (it->f)->resx
17642 : FRAME_X_DISPLAY_INFO (it->f)->resy),
17643 ppi > 0))
17644 return OK_PIXELS (ppi / pixels);
17645 #endif
17646
17647 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17648 || (CONSP (Vdisplay_pixels_per_inch)
17649 && (ppi = (width_p
17650 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17651 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17652 ppi > 0)))
17653 return OK_PIXELS (ppi / pixels);
17654
17655 return 0;
17656 }
17657 }
17658
17659 #ifdef HAVE_WINDOW_SYSTEM
17660 if (EQ (prop, Qheight))
17661 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17662 if (EQ (prop, Qwidth))
17663 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17664 #else
17665 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17666 return OK_PIXELS (1);
17667 #endif
17668
17669 if (EQ (prop, Qtext))
17670 return OK_PIXELS (width_p
17671 ? window_box_width (it->w, TEXT_AREA)
17672 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17673
17674 if (align_to && *align_to < 0)
17675 {
17676 *res = 0;
17677 if (EQ (prop, Qleft))
17678 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17679 if (EQ (prop, Qright))
17680 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17681 if (EQ (prop, Qcenter))
17682 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17683 + window_box_width (it->w, TEXT_AREA) / 2);
17684 if (EQ (prop, Qleft_fringe))
17685 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17686 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17687 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17688 if (EQ (prop, Qright_fringe))
17689 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17690 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17691 : window_box_right_offset (it->w, TEXT_AREA));
17692 if (EQ (prop, Qleft_margin))
17693 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17694 if (EQ (prop, Qright_margin))
17695 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17696 if (EQ (prop, Qscroll_bar))
17697 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17698 ? 0
17699 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17700 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17701 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17702 : 0)));
17703 }
17704 else
17705 {
17706 if (EQ (prop, Qleft_fringe))
17707 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17708 if (EQ (prop, Qright_fringe))
17709 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17710 if (EQ (prop, Qleft_margin))
17711 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17712 if (EQ (prop, Qright_margin))
17713 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17714 if (EQ (prop, Qscroll_bar))
17715 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17716 }
17717
17718 prop = Fbuffer_local_value (prop, it->w->buffer);
17719 }
17720
17721 if (INTEGERP (prop) || FLOATP (prop))
17722 {
17723 int base_unit = (width_p
17724 ? FRAME_COLUMN_WIDTH (it->f)
17725 : FRAME_LINE_HEIGHT (it->f));
17726 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17727 }
17728
17729 if (CONSP (prop))
17730 {
17731 Lisp_Object car = XCAR (prop);
17732 Lisp_Object cdr = XCDR (prop);
17733
17734 if (SYMBOLP (car))
17735 {
17736 #ifdef HAVE_WINDOW_SYSTEM
17737 if (valid_image_p (prop))
17738 {
17739 int id = lookup_image (it->f, prop);
17740 struct image *img = IMAGE_FROM_ID (it->f, id);
17741
17742 return OK_PIXELS (width_p ? img->width : img->height);
17743 }
17744 #endif
17745 if (EQ (car, Qplus) || EQ (car, Qminus))
17746 {
17747 int first = 1;
17748 double px;
17749
17750 pixels = 0;
17751 while (CONSP (cdr))
17752 {
17753 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17754 font, width_p, align_to))
17755 return 0;
17756 if (first)
17757 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17758 else
17759 pixels += px;
17760 cdr = XCDR (cdr);
17761 }
17762 if (EQ (car, Qminus))
17763 pixels = -pixels;
17764 return OK_PIXELS (pixels);
17765 }
17766
17767 car = Fbuffer_local_value (car, it->w->buffer);
17768 }
17769
17770 if (INTEGERP (car) || FLOATP (car))
17771 {
17772 double fact;
17773 pixels = XFLOATINT (car);
17774 if (NILP (cdr))
17775 return OK_PIXELS (pixels);
17776 if (calc_pixel_width_or_height (&fact, it, cdr,
17777 font, width_p, align_to))
17778 return OK_PIXELS (pixels * fact);
17779 return 0;
17780 }
17781
17782 return 0;
17783 }
17784
17785 return 0;
17786 }
17787
17788 \f
17789 /***********************************************************************
17790 Glyph Display
17791 ***********************************************************************/
17792
17793 #ifdef HAVE_WINDOW_SYSTEM
17794
17795 #if GLYPH_DEBUG
17796
17797 void
17798 dump_glyph_string (s)
17799 struct glyph_string *s;
17800 {
17801 fprintf (stderr, "glyph string\n");
17802 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17803 s->x, s->y, s->width, s->height);
17804 fprintf (stderr, " ybase = %d\n", s->ybase);
17805 fprintf (stderr, " hl = %d\n", s->hl);
17806 fprintf (stderr, " left overhang = %d, right = %d\n",
17807 s->left_overhang, s->right_overhang);
17808 fprintf (stderr, " nchars = %d\n", s->nchars);
17809 fprintf (stderr, " extends to end of line = %d\n",
17810 s->extends_to_end_of_line_p);
17811 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17812 fprintf (stderr, " bg width = %d\n", s->background_width);
17813 }
17814
17815 #endif /* GLYPH_DEBUG */
17816
17817 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17818 of XChar2b structures for S; it can't be allocated in
17819 init_glyph_string because it must be allocated via `alloca'. W
17820 is the window on which S is drawn. ROW and AREA are the glyph row
17821 and area within the row from which S is constructed. START is the
17822 index of the first glyph structure covered by S. HL is a
17823 face-override for drawing S. */
17824
17825 #ifdef HAVE_NTGUI
17826 #define OPTIONAL_HDC(hdc) hdc,
17827 #define DECLARE_HDC(hdc) HDC hdc;
17828 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17829 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17830 #endif
17831
17832 #ifndef OPTIONAL_HDC
17833 #define OPTIONAL_HDC(hdc)
17834 #define DECLARE_HDC(hdc)
17835 #define ALLOCATE_HDC(hdc, f)
17836 #define RELEASE_HDC(hdc, f)
17837 #endif
17838
17839 static void
17840 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17841 struct glyph_string *s;
17842 DECLARE_HDC (hdc)
17843 XChar2b *char2b;
17844 struct window *w;
17845 struct glyph_row *row;
17846 enum glyph_row_area area;
17847 int start;
17848 enum draw_glyphs_face hl;
17849 {
17850 bzero (s, sizeof *s);
17851 s->w = w;
17852 s->f = XFRAME (w->frame);
17853 #ifdef HAVE_NTGUI
17854 s->hdc = hdc;
17855 #endif
17856 s->display = FRAME_X_DISPLAY (s->f);
17857 s->window = FRAME_X_WINDOW (s->f);
17858 s->char2b = char2b;
17859 s->hl = hl;
17860 s->row = row;
17861 s->area = area;
17862 s->first_glyph = row->glyphs[area] + start;
17863 s->height = row->height;
17864 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17865
17866 /* Display the internal border below the tool-bar window. */
17867 if (WINDOWP (s->f->tool_bar_window)
17868 && s->w == XWINDOW (s->f->tool_bar_window))
17869 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17870
17871 s->ybase = s->y + row->ascent;
17872 }
17873
17874
17875 /* Append the list of glyph strings with head H and tail T to the list
17876 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17877
17878 static INLINE void
17879 append_glyph_string_lists (head, tail, h, t)
17880 struct glyph_string **head, **tail;
17881 struct glyph_string *h, *t;
17882 {
17883 if (h)
17884 {
17885 if (*head)
17886 (*tail)->next = h;
17887 else
17888 *head = h;
17889 h->prev = *tail;
17890 *tail = t;
17891 }
17892 }
17893
17894
17895 /* Prepend the list of glyph strings with head H and tail T to the
17896 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17897 result. */
17898
17899 static INLINE void
17900 prepend_glyph_string_lists (head, tail, h, t)
17901 struct glyph_string **head, **tail;
17902 struct glyph_string *h, *t;
17903 {
17904 if (h)
17905 {
17906 if (*head)
17907 (*head)->prev = t;
17908 else
17909 *tail = t;
17910 t->next = *head;
17911 *head = h;
17912 }
17913 }
17914
17915
17916 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17917 Set *HEAD and *TAIL to the resulting list. */
17918
17919 static INLINE void
17920 append_glyph_string (head, tail, s)
17921 struct glyph_string **head, **tail;
17922 struct glyph_string *s;
17923 {
17924 s->next = s->prev = NULL;
17925 append_glyph_string_lists (head, tail, s, s);
17926 }
17927
17928
17929 /* Get face and two-byte form of character glyph GLYPH on frame F.
17930 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17931 a pointer to a realized face that is ready for display. */
17932
17933 static INLINE struct face *
17934 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17935 struct frame *f;
17936 struct glyph *glyph;
17937 XChar2b *char2b;
17938 int *two_byte_p;
17939 {
17940 struct face *face;
17941
17942 xassert (glyph->type == CHAR_GLYPH);
17943 face = FACE_FROM_ID (f, glyph->face_id);
17944
17945 if (two_byte_p)
17946 *two_byte_p = 0;
17947
17948 if (!glyph->multibyte_p)
17949 {
17950 /* Unibyte case. We don't have to encode, but we have to make
17951 sure to use a face suitable for unibyte. */
17952 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17953 }
17954 else if (glyph->u.ch < 128
17955 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17956 {
17957 /* Case of ASCII in a face known to fit ASCII. */
17958 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17959 }
17960 else
17961 {
17962 int c1, c2, charset;
17963
17964 /* Split characters into bytes. If c2 is -1 afterwards, C is
17965 really a one-byte character so that byte1 is zero. */
17966 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17967 if (c2 > 0)
17968 STORE_XCHAR2B (char2b, c1, c2);
17969 else
17970 STORE_XCHAR2B (char2b, 0, c1);
17971
17972 /* Maybe encode the character in *CHAR2B. */
17973 if (charset != CHARSET_ASCII)
17974 {
17975 struct font_info *font_info
17976 = FONT_INFO_FROM_ID (f, face->font_info_id);
17977 if (font_info)
17978 glyph->font_type
17979 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17980 }
17981 }
17982
17983 /* Make sure X resources of the face are allocated. */
17984 xassert (face != NULL);
17985 PREPARE_FACE_FOR_DISPLAY (f, face);
17986 return face;
17987 }
17988
17989
17990 /* Fill glyph string S with composition components specified by S->cmp.
17991
17992 FACES is an array of faces for all components of this composition.
17993 S->gidx is the index of the first component for S.
17994 OVERLAPS_P non-zero means S should draw the foreground only, and
17995 use its physical height for clipping.
17996
17997 Value is the index of a component not in S. */
17998
17999 static int
18000 fill_composite_glyph_string (s, faces, overlaps_p)
18001 struct glyph_string *s;
18002 struct face **faces;
18003 int overlaps_p;
18004 {
18005 int i;
18006
18007 xassert (s);
18008
18009 s->for_overlaps_p = overlaps_p;
18010
18011 s->face = faces[s->gidx];
18012 s->font = s->face->font;
18013 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18014
18015 /* For all glyphs of this composition, starting at the offset
18016 S->gidx, until we reach the end of the definition or encounter a
18017 glyph that requires the different face, add it to S. */
18018 ++s->nchars;
18019 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18020 ++s->nchars;
18021
18022 /* All glyph strings for the same composition has the same width,
18023 i.e. the width set for the first component of the composition. */
18024
18025 s->width = s->first_glyph->pixel_width;
18026
18027 /* If the specified font could not be loaded, use the frame's
18028 default font, but record the fact that we couldn't load it in
18029 the glyph string so that we can draw rectangles for the
18030 characters of the glyph string. */
18031 if (s->font == NULL)
18032 {
18033 s->font_not_found_p = 1;
18034 s->font = FRAME_FONT (s->f);
18035 }
18036
18037 /* Adjust base line for subscript/superscript text. */
18038 s->ybase += s->first_glyph->voffset;
18039
18040 xassert (s->face && s->face->gc);
18041
18042 /* This glyph string must always be drawn with 16-bit functions. */
18043 s->two_byte_p = 1;
18044
18045 return s->gidx + s->nchars;
18046 }
18047
18048
18049 /* Fill glyph string S from a sequence of character glyphs.
18050
18051 FACE_ID is the face id of the string. START is the index of the
18052 first glyph to consider, END is the index of the last + 1.
18053 OVERLAPS_P non-zero means S should draw the foreground only, and
18054 use its physical height for clipping.
18055
18056 Value is the index of the first glyph not in S. */
18057
18058 static int
18059 fill_glyph_string (s, face_id, start, end, overlaps_p)
18060 struct glyph_string *s;
18061 int face_id;
18062 int start, end, overlaps_p;
18063 {
18064 struct glyph *glyph, *last;
18065 int voffset;
18066 int glyph_not_available_p;
18067
18068 xassert (s->f == XFRAME (s->w->frame));
18069 xassert (s->nchars == 0);
18070 xassert (start >= 0 && end > start);
18071
18072 s->for_overlaps_p = overlaps_p,
18073 glyph = s->row->glyphs[s->area] + start;
18074 last = s->row->glyphs[s->area] + end;
18075 voffset = glyph->voffset;
18076
18077 glyph_not_available_p = glyph->glyph_not_available_p;
18078
18079 while (glyph < last
18080 && glyph->type == CHAR_GLYPH
18081 && glyph->voffset == voffset
18082 /* Same face id implies same font, nowadays. */
18083 && glyph->face_id == face_id
18084 && glyph->glyph_not_available_p == glyph_not_available_p)
18085 {
18086 int two_byte_p;
18087
18088 s->face = get_glyph_face_and_encoding (s->f, glyph,
18089 s->char2b + s->nchars,
18090 &two_byte_p);
18091 s->two_byte_p = two_byte_p;
18092 ++s->nchars;
18093 xassert (s->nchars <= end - start);
18094 s->width += glyph->pixel_width;
18095 ++glyph;
18096 }
18097
18098 s->font = s->face->font;
18099 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18100
18101 /* If the specified font could not be loaded, use the frame's font,
18102 but record the fact that we couldn't load it in
18103 S->font_not_found_p so that we can draw rectangles for the
18104 characters of the glyph string. */
18105 if (s->font == NULL || glyph_not_available_p)
18106 {
18107 s->font_not_found_p = 1;
18108 s->font = FRAME_FONT (s->f);
18109 }
18110
18111 /* Adjust base line for subscript/superscript text. */
18112 s->ybase += voffset;
18113
18114 xassert (s->face && s->face->gc);
18115 return glyph - s->row->glyphs[s->area];
18116 }
18117
18118
18119 /* Fill glyph string S from image glyph S->first_glyph. */
18120
18121 static void
18122 fill_image_glyph_string (s)
18123 struct glyph_string *s;
18124 {
18125 xassert (s->first_glyph->type == IMAGE_GLYPH);
18126 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18127 xassert (s->img);
18128 s->slice = s->first_glyph->slice;
18129 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18130 s->font = s->face->font;
18131 s->width = s->first_glyph->pixel_width;
18132
18133 /* Adjust base line for subscript/superscript text. */
18134 s->ybase += s->first_glyph->voffset;
18135 }
18136
18137
18138 /* Fill glyph string S from a sequence of stretch glyphs.
18139
18140 ROW is the glyph row in which the glyphs are found, AREA is the
18141 area within the row. START is the index of the first glyph to
18142 consider, END is the index of the last + 1.
18143
18144 Value is the index of the first glyph not in S. */
18145
18146 static int
18147 fill_stretch_glyph_string (s, row, area, start, end)
18148 struct glyph_string *s;
18149 struct glyph_row *row;
18150 enum glyph_row_area area;
18151 int start, end;
18152 {
18153 struct glyph *glyph, *last;
18154 int voffset, face_id;
18155
18156 xassert (s->first_glyph->type == STRETCH_GLYPH);
18157
18158 glyph = s->row->glyphs[s->area] + start;
18159 last = s->row->glyphs[s->area] + end;
18160 face_id = glyph->face_id;
18161 s->face = FACE_FROM_ID (s->f, face_id);
18162 s->font = s->face->font;
18163 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18164 s->width = glyph->pixel_width;
18165 voffset = glyph->voffset;
18166
18167 for (++glyph;
18168 (glyph < last
18169 && glyph->type == STRETCH_GLYPH
18170 && glyph->voffset == voffset
18171 && glyph->face_id == face_id);
18172 ++glyph)
18173 s->width += glyph->pixel_width;
18174
18175 /* Adjust base line for subscript/superscript text. */
18176 s->ybase += voffset;
18177
18178 /* The case that face->gc == 0 is handled when drawing the glyph
18179 string by calling PREPARE_FACE_FOR_DISPLAY. */
18180 xassert (s->face);
18181 return glyph - s->row->glyphs[s->area];
18182 }
18183
18184
18185 /* EXPORT for RIF:
18186 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18187 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18188 assumed to be zero. */
18189
18190 void
18191 x_get_glyph_overhangs (glyph, f, left, right)
18192 struct glyph *glyph;
18193 struct frame *f;
18194 int *left, *right;
18195 {
18196 *left = *right = 0;
18197
18198 if (glyph->type == CHAR_GLYPH)
18199 {
18200 XFontStruct *font;
18201 struct face *face;
18202 struct font_info *font_info;
18203 XChar2b char2b;
18204 XCharStruct *pcm;
18205
18206 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18207 font = face->font;
18208 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18209 if (font /* ++KFS: Should this be font_info ? */
18210 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18211 {
18212 if (pcm->rbearing > pcm->width)
18213 *right = pcm->rbearing - pcm->width;
18214 if (pcm->lbearing < 0)
18215 *left = -pcm->lbearing;
18216 }
18217 }
18218 }
18219
18220
18221 /* Return the index of the first glyph preceding glyph string S that
18222 is overwritten by S because of S's left overhang. Value is -1
18223 if no glyphs are overwritten. */
18224
18225 static int
18226 left_overwritten (s)
18227 struct glyph_string *s;
18228 {
18229 int k;
18230
18231 if (s->left_overhang)
18232 {
18233 int x = 0, i;
18234 struct glyph *glyphs = s->row->glyphs[s->area];
18235 int first = s->first_glyph - glyphs;
18236
18237 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18238 x -= glyphs[i].pixel_width;
18239
18240 k = i + 1;
18241 }
18242 else
18243 k = -1;
18244
18245 return k;
18246 }
18247
18248
18249 /* Return the index of the first glyph preceding glyph string S that
18250 is overwriting S because of its right overhang. Value is -1 if no
18251 glyph in front of S overwrites S. */
18252
18253 static int
18254 left_overwriting (s)
18255 struct glyph_string *s;
18256 {
18257 int i, k, x;
18258 struct glyph *glyphs = s->row->glyphs[s->area];
18259 int first = s->first_glyph - glyphs;
18260
18261 k = -1;
18262 x = 0;
18263 for (i = first - 1; i >= 0; --i)
18264 {
18265 int left, right;
18266 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18267 if (x + right > 0)
18268 k = i;
18269 x -= glyphs[i].pixel_width;
18270 }
18271
18272 return k;
18273 }
18274
18275
18276 /* Return the index of the last glyph following glyph string S that is
18277 not overwritten by S because of S's right overhang. Value is -1 if
18278 no such glyph is found. */
18279
18280 static int
18281 right_overwritten (s)
18282 struct glyph_string *s;
18283 {
18284 int k = -1;
18285
18286 if (s->right_overhang)
18287 {
18288 int x = 0, i;
18289 struct glyph *glyphs = s->row->glyphs[s->area];
18290 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18291 int end = s->row->used[s->area];
18292
18293 for (i = first; i < end && s->right_overhang > x; ++i)
18294 x += glyphs[i].pixel_width;
18295
18296 k = i;
18297 }
18298
18299 return k;
18300 }
18301
18302
18303 /* Return the index of the last glyph following glyph string S that
18304 overwrites S because of its left overhang. Value is negative
18305 if no such glyph is found. */
18306
18307 static int
18308 right_overwriting (s)
18309 struct glyph_string *s;
18310 {
18311 int i, k, x;
18312 int end = s->row->used[s->area];
18313 struct glyph *glyphs = s->row->glyphs[s->area];
18314 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18315
18316 k = -1;
18317 x = 0;
18318 for (i = first; i < end; ++i)
18319 {
18320 int left, right;
18321 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18322 if (x - left < 0)
18323 k = i;
18324 x += glyphs[i].pixel_width;
18325 }
18326
18327 return k;
18328 }
18329
18330
18331 /* Get face and two-byte form of character C in face FACE_ID on frame
18332 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18333 means we want to display multibyte text. DISPLAY_P non-zero means
18334 make sure that X resources for the face returned are allocated.
18335 Value is a pointer to a realized face that is ready for display if
18336 DISPLAY_P is non-zero. */
18337
18338 static INLINE struct face *
18339 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18340 struct frame *f;
18341 int c, face_id;
18342 XChar2b *char2b;
18343 int multibyte_p, display_p;
18344 {
18345 struct face *face = FACE_FROM_ID (f, face_id);
18346
18347 if (!multibyte_p)
18348 {
18349 /* Unibyte case. We don't have to encode, but we have to make
18350 sure to use a face suitable for unibyte. */
18351 STORE_XCHAR2B (char2b, 0, c);
18352 face_id = FACE_FOR_CHAR (f, face, c);
18353 face = FACE_FROM_ID (f, face_id);
18354 }
18355 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18356 {
18357 /* Case of ASCII in a face known to fit ASCII. */
18358 STORE_XCHAR2B (char2b, 0, c);
18359 }
18360 else
18361 {
18362 int c1, c2, charset;
18363
18364 /* Split characters into bytes. If c2 is -1 afterwards, C is
18365 really a one-byte character so that byte1 is zero. */
18366 SPLIT_CHAR (c, charset, c1, c2);
18367 if (c2 > 0)
18368 STORE_XCHAR2B (char2b, c1, c2);
18369 else
18370 STORE_XCHAR2B (char2b, 0, c1);
18371
18372 /* Maybe encode the character in *CHAR2B. */
18373 if (face->font != NULL)
18374 {
18375 struct font_info *font_info
18376 = FONT_INFO_FROM_ID (f, face->font_info_id);
18377 if (font_info)
18378 rif->encode_char (c, char2b, font_info, 0);
18379 }
18380 }
18381
18382 /* Make sure X resources of the face are allocated. */
18383 #ifdef HAVE_X_WINDOWS
18384 if (display_p)
18385 #endif
18386 {
18387 xassert (face != NULL);
18388 PREPARE_FACE_FOR_DISPLAY (f, face);
18389 }
18390
18391 return face;
18392 }
18393
18394
18395 /* Set background width of glyph string S. START is the index of the
18396 first glyph following S. LAST_X is the right-most x-position + 1
18397 in the drawing area. */
18398
18399 static INLINE void
18400 set_glyph_string_background_width (s, start, last_x)
18401 struct glyph_string *s;
18402 int start;
18403 int last_x;
18404 {
18405 /* If the face of this glyph string has to be drawn to the end of
18406 the drawing area, set S->extends_to_end_of_line_p. */
18407 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18408
18409 if (start == s->row->used[s->area]
18410 && s->area == TEXT_AREA
18411 && ((s->hl == DRAW_NORMAL_TEXT
18412 && (s->row->fill_line_p
18413 || s->face->background != default_face->background
18414 || s->face->stipple != default_face->stipple
18415 || s->row->mouse_face_p))
18416 || s->hl == DRAW_MOUSE_FACE
18417 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18418 && s->row->fill_line_p)))
18419 s->extends_to_end_of_line_p = 1;
18420
18421 /* If S extends its face to the end of the line, set its
18422 background_width to the distance to the right edge of the drawing
18423 area. */
18424 if (s->extends_to_end_of_line_p)
18425 s->background_width = last_x - s->x + 1;
18426 else
18427 s->background_width = s->width;
18428 }
18429
18430
18431 /* Compute overhangs and x-positions for glyph string S and its
18432 predecessors, or successors. X is the starting x-position for S.
18433 BACKWARD_P non-zero means process predecessors. */
18434
18435 static void
18436 compute_overhangs_and_x (s, x, backward_p)
18437 struct glyph_string *s;
18438 int x;
18439 int backward_p;
18440 {
18441 if (backward_p)
18442 {
18443 while (s)
18444 {
18445 if (rif->compute_glyph_string_overhangs)
18446 rif->compute_glyph_string_overhangs (s);
18447 x -= s->width;
18448 s->x = x;
18449 s = s->prev;
18450 }
18451 }
18452 else
18453 {
18454 while (s)
18455 {
18456 if (rif->compute_glyph_string_overhangs)
18457 rif->compute_glyph_string_overhangs (s);
18458 s->x = x;
18459 x += s->width;
18460 s = s->next;
18461 }
18462 }
18463 }
18464
18465
18466
18467 /* The following macros are only called from draw_glyphs below.
18468 They reference the following parameters of that function directly:
18469 `w', `row', `area', and `overlap_p'
18470 as well as the following local variables:
18471 `s', `f', and `hdc' (in W32) */
18472
18473 #ifdef HAVE_NTGUI
18474 /* On W32, silently add local `hdc' variable to argument list of
18475 init_glyph_string. */
18476 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18477 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18478 #else
18479 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18480 init_glyph_string (s, char2b, w, row, area, start, hl)
18481 #endif
18482
18483 /* Add a glyph string for a stretch glyph to the list of strings
18484 between HEAD and TAIL. START is the index of the stretch glyph in
18485 row area AREA of glyph row ROW. END is the index of the last glyph
18486 in that glyph row area. X is the current output position assigned
18487 to the new glyph string constructed. HL overrides that face of the
18488 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18489 is the right-most x-position of the drawing area. */
18490
18491 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18492 and below -- keep them on one line. */
18493 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18494 do \
18495 { \
18496 s = (struct glyph_string *) alloca (sizeof *s); \
18497 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18498 START = fill_stretch_glyph_string (s, row, area, START, END); \
18499 append_glyph_string (&HEAD, &TAIL, s); \
18500 s->x = (X); \
18501 } \
18502 while (0)
18503
18504
18505 /* Add a glyph string for an image glyph to the list of strings
18506 between HEAD and TAIL. START is the index of the image glyph in
18507 row area AREA of glyph row ROW. END is the index of the last glyph
18508 in that glyph row area. X is the current output position assigned
18509 to the new glyph string constructed. HL overrides that face of the
18510 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18511 is the right-most x-position of the drawing area. */
18512
18513 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18514 do \
18515 { \
18516 s = (struct glyph_string *) alloca (sizeof *s); \
18517 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18518 fill_image_glyph_string (s); \
18519 append_glyph_string (&HEAD, &TAIL, s); \
18520 ++START; \
18521 s->x = (X); \
18522 } \
18523 while (0)
18524
18525
18526 /* Add a glyph string for a sequence of character glyphs to the list
18527 of strings between HEAD and TAIL. START is the index of the first
18528 glyph in row area AREA of glyph row ROW that is part of the new
18529 glyph string. END is the index of the last glyph in that glyph row
18530 area. X is the current output position assigned to the new glyph
18531 string constructed. HL overrides that face of the glyph; e.g. it
18532 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18533 right-most x-position of the drawing area. */
18534
18535 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18536 do \
18537 { \
18538 int c, face_id; \
18539 XChar2b *char2b; \
18540 \
18541 c = (row)->glyphs[area][START].u.ch; \
18542 face_id = (row)->glyphs[area][START].face_id; \
18543 \
18544 s = (struct glyph_string *) alloca (sizeof *s); \
18545 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18546 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18547 append_glyph_string (&HEAD, &TAIL, s); \
18548 s->x = (X); \
18549 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18550 } \
18551 while (0)
18552
18553
18554 /* Add a glyph string for a composite sequence to the list of strings
18555 between HEAD and TAIL. START is the index of the first glyph in
18556 row area AREA of glyph row ROW that is part of the new glyph
18557 string. END is the index of the last glyph in that glyph row area.
18558 X is the current output position assigned to the new glyph string
18559 constructed. HL overrides that face of the glyph; e.g. it is
18560 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18561 x-position of the drawing area. */
18562
18563 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18564 do { \
18565 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18566 int face_id = (row)->glyphs[area][START].face_id; \
18567 struct face *base_face = FACE_FROM_ID (f, face_id); \
18568 struct composition *cmp = composition_table[cmp_id]; \
18569 int glyph_len = cmp->glyph_len; \
18570 XChar2b *char2b; \
18571 struct face **faces; \
18572 struct glyph_string *first_s = NULL; \
18573 int n; \
18574 \
18575 base_face = base_face->ascii_face; \
18576 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18577 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18578 /* At first, fill in `char2b' and `faces'. */ \
18579 for (n = 0; n < glyph_len; n++) \
18580 { \
18581 int c = COMPOSITION_GLYPH (cmp, n); \
18582 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18583 faces[n] = FACE_FROM_ID (f, this_face_id); \
18584 get_char_face_and_encoding (f, c, this_face_id, \
18585 char2b + n, 1, 1); \
18586 } \
18587 \
18588 /* Make glyph_strings for each glyph sequence that is drawable by \
18589 the same face, and append them to HEAD/TAIL. */ \
18590 for (n = 0; n < cmp->glyph_len;) \
18591 { \
18592 s = (struct glyph_string *) alloca (sizeof *s); \
18593 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18594 append_glyph_string (&(HEAD), &(TAIL), s); \
18595 s->cmp = cmp; \
18596 s->gidx = n; \
18597 s->x = (X); \
18598 \
18599 if (n == 0) \
18600 first_s = s; \
18601 \
18602 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18603 } \
18604 \
18605 ++START; \
18606 s = first_s; \
18607 } while (0)
18608
18609
18610 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18611 of AREA of glyph row ROW on window W between indices START and END.
18612 HL overrides the face for drawing glyph strings, e.g. it is
18613 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18614 x-positions of the drawing area.
18615
18616 This is an ugly monster macro construct because we must use alloca
18617 to allocate glyph strings (because draw_glyphs can be called
18618 asynchronously). */
18619
18620 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18621 do \
18622 { \
18623 HEAD = TAIL = NULL; \
18624 while (START < END) \
18625 { \
18626 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18627 switch (first_glyph->type) \
18628 { \
18629 case CHAR_GLYPH: \
18630 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18631 HL, X, LAST_X); \
18632 break; \
18633 \
18634 case COMPOSITE_GLYPH: \
18635 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18636 HL, X, LAST_X); \
18637 break; \
18638 \
18639 case STRETCH_GLYPH: \
18640 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18641 HL, X, LAST_X); \
18642 break; \
18643 \
18644 case IMAGE_GLYPH: \
18645 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18646 HL, X, LAST_X); \
18647 break; \
18648 \
18649 default: \
18650 abort (); \
18651 } \
18652 \
18653 set_glyph_string_background_width (s, START, LAST_X); \
18654 (X) += s->width; \
18655 } \
18656 } \
18657 while (0)
18658
18659
18660 /* Draw glyphs between START and END in AREA of ROW on window W,
18661 starting at x-position X. X is relative to AREA in W. HL is a
18662 face-override with the following meaning:
18663
18664 DRAW_NORMAL_TEXT draw normally
18665 DRAW_CURSOR draw in cursor face
18666 DRAW_MOUSE_FACE draw in mouse face.
18667 DRAW_INVERSE_VIDEO draw in mode line face
18668 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18669 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18670
18671 If OVERLAPS_P is non-zero, draw only the foreground of characters
18672 and clip to the physical height of ROW.
18673
18674 Value is the x-position reached, relative to AREA of W. */
18675
18676 static int
18677 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18678 struct window *w;
18679 int x;
18680 struct glyph_row *row;
18681 enum glyph_row_area area;
18682 int start, end;
18683 enum draw_glyphs_face hl;
18684 int overlaps_p;
18685 {
18686 struct glyph_string *head, *tail;
18687 struct glyph_string *s;
18688 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18689 int last_x, area_width;
18690 int x_reached;
18691 int i, j;
18692 struct frame *f = XFRAME (WINDOW_FRAME (w));
18693 DECLARE_HDC (hdc);
18694
18695 ALLOCATE_HDC (hdc, f);
18696
18697 /* Let's rather be paranoid than getting a SEGV. */
18698 end = min (end, row->used[area]);
18699 start = max (0, start);
18700 start = min (end, start);
18701
18702 /* Translate X to frame coordinates. Set last_x to the right
18703 end of the drawing area. */
18704 if (row->full_width_p)
18705 {
18706 /* X is relative to the left edge of W, without scroll bars
18707 or fringes. */
18708 x += WINDOW_LEFT_EDGE_X (w);
18709 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18710 }
18711 else
18712 {
18713 int area_left = window_box_left (w, area);
18714 x += area_left;
18715 area_width = window_box_width (w, area);
18716 last_x = area_left + area_width;
18717 }
18718
18719 /* Build a doubly-linked list of glyph_string structures between
18720 head and tail from what we have to draw. Note that the macro
18721 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18722 the reason we use a separate variable `i'. */
18723 i = start;
18724 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18725 if (tail)
18726 x_reached = tail->x + tail->background_width;
18727 else
18728 x_reached = x;
18729
18730 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18731 the row, redraw some glyphs in front or following the glyph
18732 strings built above. */
18733 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18734 {
18735 int dummy_x = 0;
18736 struct glyph_string *h, *t;
18737
18738 /* Compute overhangs for all glyph strings. */
18739 if (rif->compute_glyph_string_overhangs)
18740 for (s = head; s; s = s->next)
18741 rif->compute_glyph_string_overhangs (s);
18742
18743 /* Prepend glyph strings for glyphs in front of the first glyph
18744 string that are overwritten because of the first glyph
18745 string's left overhang. The background of all strings
18746 prepended must be drawn because the first glyph string
18747 draws over it. */
18748 i = left_overwritten (head);
18749 if (i >= 0)
18750 {
18751 j = i;
18752 BUILD_GLYPH_STRINGS (j, start, h, t,
18753 DRAW_NORMAL_TEXT, dummy_x, last_x);
18754 start = i;
18755 compute_overhangs_and_x (t, head->x, 1);
18756 prepend_glyph_string_lists (&head, &tail, h, t);
18757 clip_head = head;
18758 }
18759
18760 /* Prepend glyph strings for glyphs in front of the first glyph
18761 string that overwrite that glyph string because of their
18762 right overhang. For these strings, only the foreground must
18763 be drawn, because it draws over the glyph string at `head'.
18764 The background must not be drawn because this would overwrite
18765 right overhangs of preceding glyphs for which no glyph
18766 strings exist. */
18767 i = left_overwriting (head);
18768 if (i >= 0)
18769 {
18770 clip_head = head;
18771 BUILD_GLYPH_STRINGS (i, start, h, t,
18772 DRAW_NORMAL_TEXT, dummy_x, last_x);
18773 for (s = h; s; s = s->next)
18774 s->background_filled_p = 1;
18775 compute_overhangs_and_x (t, head->x, 1);
18776 prepend_glyph_string_lists (&head, &tail, h, t);
18777 }
18778
18779 /* Append glyphs strings for glyphs following the last glyph
18780 string tail that are overwritten by tail. The background of
18781 these strings has to be drawn because tail's foreground draws
18782 over it. */
18783 i = right_overwritten (tail);
18784 if (i >= 0)
18785 {
18786 BUILD_GLYPH_STRINGS (end, i, h, t,
18787 DRAW_NORMAL_TEXT, x, last_x);
18788 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18789 append_glyph_string_lists (&head, &tail, h, t);
18790 clip_tail = tail;
18791 }
18792
18793 /* Append glyph strings for glyphs following the last glyph
18794 string tail that overwrite tail. The foreground of such
18795 glyphs has to be drawn because it writes into the background
18796 of tail. The background must not be drawn because it could
18797 paint over the foreground of following glyphs. */
18798 i = right_overwriting (tail);
18799 if (i >= 0)
18800 {
18801 clip_tail = tail;
18802 BUILD_GLYPH_STRINGS (end, i, h, t,
18803 DRAW_NORMAL_TEXT, x, last_x);
18804 for (s = h; s; s = s->next)
18805 s->background_filled_p = 1;
18806 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18807 append_glyph_string_lists (&head, &tail, h, t);
18808 }
18809 if (clip_head || clip_tail)
18810 for (s = head; s; s = s->next)
18811 {
18812 s->clip_head = clip_head;
18813 s->clip_tail = clip_tail;
18814 }
18815 }
18816
18817 /* Draw all strings. */
18818 for (s = head; s; s = s->next)
18819 rif->draw_glyph_string (s);
18820
18821 if (area == TEXT_AREA
18822 && !row->full_width_p
18823 /* When drawing overlapping rows, only the glyph strings'
18824 foreground is drawn, which doesn't erase a cursor
18825 completely. */
18826 && !overlaps_p)
18827 {
18828 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18829 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18830 : (tail ? tail->x + tail->background_width : x));
18831
18832 int text_left = window_box_left (w, TEXT_AREA);
18833 x0 -= text_left;
18834 x1 -= text_left;
18835
18836 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18837 row->y, MATRIX_ROW_BOTTOM_Y (row));
18838 }
18839
18840 /* Value is the x-position up to which drawn, relative to AREA of W.
18841 This doesn't include parts drawn because of overhangs. */
18842 if (row->full_width_p)
18843 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18844 else
18845 x_reached -= window_box_left (w, area);
18846
18847 RELEASE_HDC (hdc, f);
18848
18849 return x_reached;
18850 }
18851
18852 /* Expand row matrix if too narrow. Don't expand if area
18853 is not present. */
18854
18855 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18856 { \
18857 if (!fonts_changed_p \
18858 && (it->glyph_row->glyphs[area] \
18859 < it->glyph_row->glyphs[area + 1])) \
18860 { \
18861 it->w->ncols_scale_factor++; \
18862 fonts_changed_p = 1; \
18863 } \
18864 }
18865
18866 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18867 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18868
18869 static INLINE void
18870 append_glyph (it)
18871 struct it *it;
18872 {
18873 struct glyph *glyph;
18874 enum glyph_row_area area = it->area;
18875
18876 xassert (it->glyph_row);
18877 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18878
18879 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18880 if (glyph < it->glyph_row->glyphs[area + 1])
18881 {
18882 glyph->charpos = CHARPOS (it->position);
18883 glyph->object = it->object;
18884 glyph->pixel_width = it->pixel_width;
18885 glyph->ascent = it->ascent;
18886 glyph->descent = it->descent;
18887 glyph->voffset = it->voffset;
18888 glyph->type = CHAR_GLYPH;
18889 glyph->multibyte_p = it->multibyte_p;
18890 glyph->left_box_line_p = it->start_of_box_run_p;
18891 glyph->right_box_line_p = it->end_of_box_run_p;
18892 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18893 || it->phys_descent > it->descent);
18894 glyph->padding_p = 0;
18895 glyph->glyph_not_available_p = it->glyph_not_available_p;
18896 glyph->face_id = it->face_id;
18897 glyph->u.ch = it->char_to_display;
18898 glyph->slice = null_glyph_slice;
18899 glyph->font_type = FONT_TYPE_UNKNOWN;
18900 ++it->glyph_row->used[area];
18901 }
18902 else
18903 IT_EXPAND_MATRIX_WIDTH (it, area);
18904 }
18905
18906 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18907 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18908
18909 static INLINE void
18910 append_composite_glyph (it)
18911 struct it *it;
18912 {
18913 struct glyph *glyph;
18914 enum glyph_row_area area = it->area;
18915
18916 xassert (it->glyph_row);
18917
18918 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18919 if (glyph < it->glyph_row->glyphs[area + 1])
18920 {
18921 glyph->charpos = CHARPOS (it->position);
18922 glyph->object = it->object;
18923 glyph->pixel_width = it->pixel_width;
18924 glyph->ascent = it->ascent;
18925 glyph->descent = it->descent;
18926 glyph->voffset = it->voffset;
18927 glyph->type = COMPOSITE_GLYPH;
18928 glyph->multibyte_p = it->multibyte_p;
18929 glyph->left_box_line_p = it->start_of_box_run_p;
18930 glyph->right_box_line_p = it->end_of_box_run_p;
18931 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18932 || it->phys_descent > it->descent);
18933 glyph->padding_p = 0;
18934 glyph->glyph_not_available_p = 0;
18935 glyph->face_id = it->face_id;
18936 glyph->u.cmp_id = it->cmp_id;
18937 glyph->slice = null_glyph_slice;
18938 glyph->font_type = FONT_TYPE_UNKNOWN;
18939 ++it->glyph_row->used[area];
18940 }
18941 else
18942 IT_EXPAND_MATRIX_WIDTH (it, area);
18943 }
18944
18945
18946 /* Change IT->ascent and IT->height according to the setting of
18947 IT->voffset. */
18948
18949 static INLINE void
18950 take_vertical_position_into_account (it)
18951 struct it *it;
18952 {
18953 if (it->voffset)
18954 {
18955 if (it->voffset < 0)
18956 /* Increase the ascent so that we can display the text higher
18957 in the line. */
18958 it->ascent -= it->voffset;
18959 else
18960 /* Increase the descent so that we can display the text lower
18961 in the line. */
18962 it->descent += it->voffset;
18963 }
18964 }
18965
18966
18967 /* Produce glyphs/get display metrics for the image IT is loaded with.
18968 See the description of struct display_iterator in dispextern.h for
18969 an overview of struct display_iterator. */
18970
18971 static void
18972 produce_image_glyph (it)
18973 struct it *it;
18974 {
18975 struct image *img;
18976 struct face *face;
18977 int glyph_ascent;
18978 struct glyph_slice slice;
18979
18980 xassert (it->what == IT_IMAGE);
18981
18982 face = FACE_FROM_ID (it->f, it->face_id);
18983 xassert (face);
18984 /* Make sure X resources of the face is loaded. */
18985 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18986
18987 if (it->image_id < 0)
18988 {
18989 /* Fringe bitmap. */
18990 it->ascent = it->phys_ascent = 0;
18991 it->descent = it->phys_descent = 0;
18992 it->pixel_width = 0;
18993 it->nglyphs = 0;
18994 return;
18995 }
18996
18997 img = IMAGE_FROM_ID (it->f, it->image_id);
18998 xassert (img);
18999 /* Make sure X resources of the image is loaded. */
19000 prepare_image_for_display (it->f, img);
19001
19002 slice.x = slice.y = 0;
19003 slice.width = img->width;
19004 slice.height = img->height;
19005
19006 if (INTEGERP (it->slice.x))
19007 slice.x = XINT (it->slice.x);
19008 else if (FLOATP (it->slice.x))
19009 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19010
19011 if (INTEGERP (it->slice.y))
19012 slice.y = XINT (it->slice.y);
19013 else if (FLOATP (it->slice.y))
19014 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19015
19016 if (INTEGERP (it->slice.width))
19017 slice.width = XINT (it->slice.width);
19018 else if (FLOATP (it->slice.width))
19019 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19020
19021 if (INTEGERP (it->slice.height))
19022 slice.height = XINT (it->slice.height);
19023 else if (FLOATP (it->slice.height))
19024 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19025
19026 if (slice.x >= img->width)
19027 slice.x = img->width;
19028 if (slice.y >= img->height)
19029 slice.y = img->height;
19030 if (slice.x + slice.width >= img->width)
19031 slice.width = img->width - slice.x;
19032 if (slice.y + slice.height > img->height)
19033 slice.height = img->height - slice.y;
19034
19035 if (slice.width == 0 || slice.height == 0)
19036 return;
19037
19038 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19039
19040 it->descent = slice.height - glyph_ascent;
19041 if (slice.y == 0)
19042 it->descent += img->vmargin;
19043 if (slice.y + slice.height == img->height)
19044 it->descent += img->vmargin;
19045 it->phys_descent = it->descent;
19046
19047 it->pixel_width = slice.width;
19048 if (slice.x == 0)
19049 it->pixel_width += img->hmargin;
19050 if (slice.x + slice.width == img->width)
19051 it->pixel_width += img->hmargin;
19052
19053 /* It's quite possible for images to have an ascent greater than
19054 their height, so don't get confused in that case. */
19055 if (it->descent < 0)
19056 it->descent = 0;
19057
19058 #if 0 /* this breaks image tiling */
19059 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19060 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19061 if (face_ascent > it->ascent)
19062 it->ascent = it->phys_ascent = face_ascent;
19063 #endif
19064
19065 it->nglyphs = 1;
19066
19067 if (face->box != FACE_NO_BOX)
19068 {
19069 if (face->box_line_width > 0)
19070 {
19071 if (slice.y == 0)
19072 it->ascent += face->box_line_width;
19073 if (slice.y + slice.height == img->height)
19074 it->descent += face->box_line_width;
19075 }
19076
19077 if (it->start_of_box_run_p && slice.x == 0)
19078 it->pixel_width += abs (face->box_line_width);
19079 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19080 it->pixel_width += abs (face->box_line_width);
19081 }
19082
19083 take_vertical_position_into_account (it);
19084
19085 if (it->glyph_row)
19086 {
19087 struct glyph *glyph;
19088 enum glyph_row_area area = it->area;
19089
19090 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19091 if (glyph < it->glyph_row->glyphs[area + 1])
19092 {
19093 glyph->charpos = CHARPOS (it->position);
19094 glyph->object = it->object;
19095 glyph->pixel_width = it->pixel_width;
19096 glyph->ascent = glyph_ascent;
19097 glyph->descent = it->descent;
19098 glyph->voffset = it->voffset;
19099 glyph->type = IMAGE_GLYPH;
19100 glyph->multibyte_p = it->multibyte_p;
19101 glyph->left_box_line_p = it->start_of_box_run_p;
19102 glyph->right_box_line_p = it->end_of_box_run_p;
19103 glyph->overlaps_vertically_p = 0;
19104 glyph->padding_p = 0;
19105 glyph->glyph_not_available_p = 0;
19106 glyph->face_id = it->face_id;
19107 glyph->u.img_id = img->id;
19108 glyph->slice = slice;
19109 glyph->font_type = FONT_TYPE_UNKNOWN;
19110 ++it->glyph_row->used[area];
19111 }
19112 else
19113 IT_EXPAND_MATRIX_WIDTH (it, area);
19114 }
19115 }
19116
19117
19118 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19119 of the glyph, WIDTH and HEIGHT are the width and height of the
19120 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19121
19122 static void
19123 append_stretch_glyph (it, object, width, height, ascent)
19124 struct it *it;
19125 Lisp_Object object;
19126 int width, height;
19127 int ascent;
19128 {
19129 struct glyph *glyph;
19130 enum glyph_row_area area = it->area;
19131
19132 xassert (ascent >= 0 && ascent <= height);
19133
19134 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19135 if (glyph < it->glyph_row->glyphs[area + 1])
19136 {
19137 glyph->charpos = CHARPOS (it->position);
19138 glyph->object = object;
19139 glyph->pixel_width = width;
19140 glyph->ascent = ascent;
19141 glyph->descent = height - ascent;
19142 glyph->voffset = it->voffset;
19143 glyph->type = STRETCH_GLYPH;
19144 glyph->multibyte_p = it->multibyte_p;
19145 glyph->left_box_line_p = it->start_of_box_run_p;
19146 glyph->right_box_line_p = it->end_of_box_run_p;
19147 glyph->overlaps_vertically_p = 0;
19148 glyph->padding_p = 0;
19149 glyph->glyph_not_available_p = 0;
19150 glyph->face_id = it->face_id;
19151 glyph->u.stretch.ascent = ascent;
19152 glyph->u.stretch.height = height;
19153 glyph->slice = null_glyph_slice;
19154 glyph->font_type = FONT_TYPE_UNKNOWN;
19155 ++it->glyph_row->used[area];
19156 }
19157 else
19158 IT_EXPAND_MATRIX_WIDTH (it, area);
19159 }
19160
19161
19162 /* Produce a stretch glyph for iterator IT. IT->object is the value
19163 of the glyph property displayed. The value must be a list
19164 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19165 being recognized:
19166
19167 1. `:width WIDTH' specifies that the space should be WIDTH *
19168 canonical char width wide. WIDTH may be an integer or floating
19169 point number.
19170
19171 2. `:relative-width FACTOR' specifies that the width of the stretch
19172 should be computed from the width of the first character having the
19173 `glyph' property, and should be FACTOR times that width.
19174
19175 3. `:align-to HPOS' specifies that the space should be wide enough
19176 to reach HPOS, a value in canonical character units.
19177
19178 Exactly one of the above pairs must be present.
19179
19180 4. `:height HEIGHT' specifies that the height of the stretch produced
19181 should be HEIGHT, measured in canonical character units.
19182
19183 5. `:relative-height FACTOR' specifies that the height of the
19184 stretch should be FACTOR times the height of the characters having
19185 the glyph property.
19186
19187 Either none or exactly one of 4 or 5 must be present.
19188
19189 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19190 of the stretch should be used for the ascent of the stretch.
19191 ASCENT must be in the range 0 <= ASCENT <= 100. */
19192
19193 static void
19194 produce_stretch_glyph (it)
19195 struct it *it;
19196 {
19197 /* (space :width WIDTH :height HEIGHT ...) */
19198 Lisp_Object prop, plist;
19199 int width = 0, height = 0, align_to = -1;
19200 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19201 int ascent = 0;
19202 double tem;
19203 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19204 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19205
19206 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19207
19208 /* List should start with `space'. */
19209 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19210 plist = XCDR (it->object);
19211
19212 /* Compute the width of the stretch. */
19213 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19214 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19215 {
19216 /* Absolute width `:width WIDTH' specified and valid. */
19217 zero_width_ok_p = 1;
19218 width = (int)tem;
19219 }
19220 else if (prop = Fplist_get (plist, QCrelative_width),
19221 NUMVAL (prop) > 0)
19222 {
19223 /* Relative width `:relative-width FACTOR' specified and valid.
19224 Compute the width of the characters having the `glyph'
19225 property. */
19226 struct it it2;
19227 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19228
19229 it2 = *it;
19230 if (it->multibyte_p)
19231 {
19232 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19233 - IT_BYTEPOS (*it));
19234 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19235 }
19236 else
19237 it2.c = *p, it2.len = 1;
19238
19239 it2.glyph_row = NULL;
19240 it2.what = IT_CHARACTER;
19241 x_produce_glyphs (&it2);
19242 width = NUMVAL (prop) * it2.pixel_width;
19243 }
19244 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19245 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19246 {
19247 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19248 align_to = (align_to < 0
19249 ? 0
19250 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19251 else if (align_to < 0)
19252 align_to = window_box_left_offset (it->w, TEXT_AREA);
19253 width = max (0, (int)tem + align_to - it->current_x);
19254 zero_width_ok_p = 1;
19255 }
19256 else
19257 /* Nothing specified -> width defaults to canonical char width. */
19258 width = FRAME_COLUMN_WIDTH (it->f);
19259
19260 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19261 width = 1;
19262
19263 /* Compute height. */
19264 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19265 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19266 {
19267 height = (int)tem;
19268 zero_height_ok_p = 1;
19269 }
19270 else if (prop = Fplist_get (plist, QCrelative_height),
19271 NUMVAL (prop) > 0)
19272 height = FONT_HEIGHT (font) * NUMVAL (prop);
19273 else
19274 height = FONT_HEIGHT (font);
19275
19276 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19277 height = 1;
19278
19279 /* Compute percentage of height used for ascent. If
19280 `:ascent ASCENT' is present and valid, use that. Otherwise,
19281 derive the ascent from the font in use. */
19282 if (prop = Fplist_get (plist, QCascent),
19283 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19284 ascent = height * NUMVAL (prop) / 100.0;
19285 else if (!NILP (prop)
19286 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19287 ascent = min (max (0, (int)tem), height);
19288 else
19289 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19290
19291 if (width > 0 && height > 0 && it->glyph_row)
19292 {
19293 Lisp_Object object = it->stack[it->sp - 1].string;
19294 if (!STRINGP (object))
19295 object = it->w->buffer;
19296 append_stretch_glyph (it, object, width, height, ascent);
19297 }
19298
19299 it->pixel_width = width;
19300 it->ascent = it->phys_ascent = ascent;
19301 it->descent = it->phys_descent = height - it->ascent;
19302 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19303
19304 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19305 {
19306 if (face->box_line_width > 0)
19307 {
19308 it->ascent += face->box_line_width;
19309 it->descent += face->box_line_width;
19310 }
19311
19312 if (it->start_of_box_run_p)
19313 it->pixel_width += abs (face->box_line_width);
19314 if (it->end_of_box_run_p)
19315 it->pixel_width += abs (face->box_line_width);
19316 }
19317
19318 take_vertical_position_into_account (it);
19319 }
19320
19321 /* Get line-height and line-spacing property at point.
19322 If line-height has format (HEIGHT TOTAL), return TOTAL
19323 in TOTAL_HEIGHT. */
19324
19325 static Lisp_Object
19326 get_line_height_property (it, prop)
19327 struct it *it;
19328 Lisp_Object prop;
19329 {
19330 Lisp_Object position;
19331
19332 if (STRINGP (it->object))
19333 position = make_number (IT_STRING_CHARPOS (*it));
19334 else if (BUFFERP (it->object))
19335 position = make_number (IT_CHARPOS (*it));
19336 else
19337 return Qnil;
19338
19339 return Fget_char_property (position, prop, it->object);
19340 }
19341
19342 /* Calculate line-height and line-spacing properties.
19343 An integer value specifies explicit pixel value.
19344 A float value specifies relative value to current face height.
19345 A cons (float . face-name) specifies relative value to
19346 height of specified face font.
19347
19348 Returns height in pixels, or nil. */
19349
19350
19351 static Lisp_Object
19352 calc_line_height_property (it, val, font, boff, override)
19353 struct it *it;
19354 Lisp_Object val;
19355 XFontStruct *font;
19356 int boff, override;
19357 {
19358 Lisp_Object face_name = Qnil;
19359 int ascent, descent, height;
19360
19361 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19362 return val;
19363
19364 if (CONSP (val))
19365 {
19366 face_name = XCAR (val);
19367 val = XCDR (val);
19368 if (!NUMBERP (val))
19369 val = make_number (1);
19370 if (NILP (face_name))
19371 {
19372 height = it->ascent + it->descent;
19373 goto scale;
19374 }
19375 }
19376
19377 if (NILP (face_name))
19378 {
19379 font = FRAME_FONT (it->f);
19380 boff = FRAME_BASELINE_OFFSET (it->f);
19381 }
19382 else if (EQ (face_name, Qt))
19383 {
19384 override = 0;
19385 }
19386 else
19387 {
19388 int face_id;
19389 struct face *face;
19390 struct font_info *font_info;
19391
19392 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19393 if (face_id < 0)
19394 return make_number (-1);
19395
19396 face = FACE_FROM_ID (it->f, face_id);
19397 font = face->font;
19398 if (font == NULL)
19399 return make_number (-1);
19400
19401 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19402 boff = font_info->baseline_offset;
19403 if (font_info->vertical_centering)
19404 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19405 }
19406
19407 ascent = FONT_BASE (font) + boff;
19408 descent = FONT_DESCENT (font) - boff;
19409
19410 if (override)
19411 {
19412 it->override_ascent = ascent;
19413 it->override_descent = descent;
19414 it->override_boff = boff;
19415 }
19416
19417 height = ascent + descent;
19418
19419 scale:
19420 if (FLOATP (val))
19421 height = (int)(XFLOAT_DATA (val) * height);
19422 else if (INTEGERP (val))
19423 height *= XINT (val);
19424
19425 return make_number (height);
19426 }
19427
19428
19429 /* RIF:
19430 Produce glyphs/get display metrics for the display element IT is
19431 loaded with. See the description of struct display_iterator in
19432 dispextern.h for an overview of struct display_iterator. */
19433
19434 void
19435 x_produce_glyphs (it)
19436 struct it *it;
19437 {
19438 int extra_line_spacing = it->extra_line_spacing;
19439
19440 it->glyph_not_available_p = 0;
19441
19442 if (it->what == IT_CHARACTER)
19443 {
19444 XChar2b char2b;
19445 XFontStruct *font;
19446 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19447 XCharStruct *pcm;
19448 int font_not_found_p;
19449 struct font_info *font_info;
19450 int boff; /* baseline offset */
19451 /* We may change it->multibyte_p upon unibyte<->multibyte
19452 conversion. So, save the current value now and restore it
19453 later.
19454
19455 Note: It seems that we don't have to record multibyte_p in
19456 struct glyph because the character code itself tells if or
19457 not the character is multibyte. Thus, in the future, we must
19458 consider eliminating the field `multibyte_p' in the struct
19459 glyph. */
19460 int saved_multibyte_p = it->multibyte_p;
19461
19462 /* Maybe translate single-byte characters to multibyte, or the
19463 other way. */
19464 it->char_to_display = it->c;
19465 if (!ASCII_BYTE_P (it->c))
19466 {
19467 if (unibyte_display_via_language_environment
19468 && SINGLE_BYTE_CHAR_P (it->c)
19469 && (it->c >= 0240
19470 || !NILP (Vnonascii_translation_table)))
19471 {
19472 it->char_to_display = unibyte_char_to_multibyte (it->c);
19473 it->multibyte_p = 1;
19474 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19475 face = FACE_FROM_ID (it->f, it->face_id);
19476 }
19477 else if (!SINGLE_BYTE_CHAR_P (it->c)
19478 && !it->multibyte_p)
19479 {
19480 it->multibyte_p = 1;
19481 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19482 face = FACE_FROM_ID (it->f, it->face_id);
19483 }
19484 }
19485
19486 /* Get font to use. Encode IT->char_to_display. */
19487 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19488 &char2b, it->multibyte_p, 0);
19489 font = face->font;
19490
19491 /* When no suitable font found, use the default font. */
19492 font_not_found_p = font == NULL;
19493 if (font_not_found_p)
19494 {
19495 font = FRAME_FONT (it->f);
19496 boff = FRAME_BASELINE_OFFSET (it->f);
19497 font_info = NULL;
19498 }
19499 else
19500 {
19501 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19502 boff = font_info->baseline_offset;
19503 if (font_info->vertical_centering)
19504 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19505 }
19506
19507 if (it->char_to_display >= ' '
19508 && (!it->multibyte_p || it->char_to_display < 128))
19509 {
19510 /* Either unibyte or ASCII. */
19511 int stretched_p;
19512
19513 it->nglyphs = 1;
19514
19515 pcm = rif->per_char_metric (font, &char2b,
19516 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19517
19518 if (it->override_ascent >= 0)
19519 {
19520 it->ascent = it->override_ascent;
19521 it->descent = it->override_descent;
19522 boff = it->override_boff;
19523 }
19524 else
19525 {
19526 it->ascent = FONT_BASE (font) + boff;
19527 it->descent = FONT_DESCENT (font) - boff;
19528 }
19529
19530 if (pcm)
19531 {
19532 it->phys_ascent = pcm->ascent + boff;
19533 it->phys_descent = pcm->descent - boff;
19534 it->pixel_width = pcm->width;
19535 }
19536 else
19537 {
19538 it->glyph_not_available_p = 1;
19539 it->phys_ascent = it->ascent;
19540 it->phys_descent = it->descent;
19541 it->pixel_width = FONT_WIDTH (font);
19542 }
19543
19544 if (it->constrain_row_ascent_descent_p)
19545 {
19546 if (it->descent > it->max_descent)
19547 {
19548 it->ascent += it->descent - it->max_descent;
19549 it->descent = it->max_descent;
19550 }
19551 if (it->ascent > it->max_ascent)
19552 {
19553 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19554 it->ascent = it->max_ascent;
19555 }
19556 it->phys_ascent = min (it->phys_ascent, it->ascent);
19557 it->phys_descent = min (it->phys_descent, it->descent);
19558 extra_line_spacing = 0;
19559 }
19560
19561 /* If this is a space inside a region of text with
19562 `space-width' property, change its width. */
19563 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19564 if (stretched_p)
19565 it->pixel_width *= XFLOATINT (it->space_width);
19566
19567 /* If face has a box, add the box thickness to the character
19568 height. If character has a box line to the left and/or
19569 right, add the box line width to the character's width. */
19570 if (face->box != FACE_NO_BOX)
19571 {
19572 int thick = face->box_line_width;
19573
19574 if (thick > 0)
19575 {
19576 it->ascent += thick;
19577 it->descent += thick;
19578 }
19579 else
19580 thick = -thick;
19581
19582 if (it->start_of_box_run_p)
19583 it->pixel_width += thick;
19584 if (it->end_of_box_run_p)
19585 it->pixel_width += thick;
19586 }
19587
19588 /* If face has an overline, add the height of the overline
19589 (1 pixel) and a 1 pixel margin to the character height. */
19590 if (face->overline_p)
19591 it->ascent += 2;
19592
19593 if (it->constrain_row_ascent_descent_p)
19594 {
19595 if (it->ascent > it->max_ascent)
19596 it->ascent = it->max_ascent;
19597 if (it->descent > it->max_descent)
19598 it->descent = it->max_descent;
19599 }
19600
19601 take_vertical_position_into_account (it);
19602
19603 /* If we have to actually produce glyphs, do it. */
19604 if (it->glyph_row)
19605 {
19606 if (stretched_p)
19607 {
19608 /* Translate a space with a `space-width' property
19609 into a stretch glyph. */
19610 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19611 / FONT_HEIGHT (font));
19612 append_stretch_glyph (it, it->object, it->pixel_width,
19613 it->ascent + it->descent, ascent);
19614 }
19615 else
19616 append_glyph (it);
19617
19618 /* If characters with lbearing or rbearing are displayed
19619 in this line, record that fact in a flag of the
19620 glyph row. This is used to optimize X output code. */
19621 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19622 it->glyph_row->contains_overlapping_glyphs_p = 1;
19623 }
19624 }
19625 else if (it->char_to_display == '\n')
19626 {
19627 /* A newline has no width but we need the height of the line.
19628 But if previous part of the line set a height, don't
19629 increase that height */
19630
19631 Lisp_Object height;
19632 Lisp_Object total_height = Qnil;
19633
19634 it->override_ascent = -1;
19635 it->pixel_width = 0;
19636 it->nglyphs = 0;
19637
19638 height = get_line_height_property(it, Qline_height);
19639 /* Split (line-height total-height) list */
19640 if (CONSP (height)
19641 && CONSP (XCDR (height))
19642 && NILP (XCDR (XCDR (height))))
19643 {
19644 total_height = XCAR (XCDR (height));
19645 height = XCAR (height);
19646 }
19647 height = calc_line_height_property(it, height, font, boff, 1);
19648
19649 if (it->override_ascent >= 0)
19650 {
19651 it->ascent = it->override_ascent;
19652 it->descent = it->override_descent;
19653 boff = it->override_boff;
19654 }
19655 else
19656 {
19657 it->ascent = FONT_BASE (font) + boff;
19658 it->descent = FONT_DESCENT (font) - boff;
19659 }
19660
19661 if (EQ (height, Qt))
19662 {
19663 if (it->descent > it->max_descent)
19664 {
19665 it->ascent += it->descent - it->max_descent;
19666 it->descent = it->max_descent;
19667 }
19668 if (it->ascent > it->max_ascent)
19669 {
19670 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19671 it->ascent = it->max_ascent;
19672 }
19673 it->phys_ascent = min (it->phys_ascent, it->ascent);
19674 it->phys_descent = min (it->phys_descent, it->descent);
19675 it->constrain_row_ascent_descent_p = 1;
19676 extra_line_spacing = 0;
19677 }
19678 else
19679 {
19680 Lisp_Object spacing;
19681
19682 it->phys_ascent = it->ascent;
19683 it->phys_descent = it->descent;
19684
19685 if ((it->max_ascent > 0 || it->max_descent > 0)
19686 && face->box != FACE_NO_BOX
19687 && face->box_line_width > 0)
19688 {
19689 it->ascent += face->box_line_width;
19690 it->descent += face->box_line_width;
19691 }
19692 if (!NILP (height)
19693 && XINT (height) > it->ascent + it->descent)
19694 it->ascent = XINT (height) - it->descent;
19695
19696 if (!NILP (total_height))
19697 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19698 else
19699 {
19700 spacing = get_line_height_property(it, Qline_spacing);
19701 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19702 }
19703 if (INTEGERP (spacing))
19704 {
19705 extra_line_spacing = XINT (spacing);
19706 if (!NILP (total_height))
19707 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19708 }
19709 }
19710 }
19711 else if (it->char_to_display == '\t')
19712 {
19713 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19714 int x = it->current_x + it->continuation_lines_width;
19715 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19716
19717 /* If the distance from the current position to the next tab
19718 stop is less than a space character width, use the
19719 tab stop after that. */
19720 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19721 next_tab_x += tab_width;
19722
19723 it->pixel_width = next_tab_x - x;
19724 it->nglyphs = 1;
19725 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19726 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19727
19728 if (it->glyph_row)
19729 {
19730 append_stretch_glyph (it, it->object, it->pixel_width,
19731 it->ascent + it->descent, it->ascent);
19732 }
19733 }
19734 else
19735 {
19736 /* A multi-byte character. Assume that the display width of the
19737 character is the width of the character multiplied by the
19738 width of the font. */
19739
19740 /* If we found a font, this font should give us the right
19741 metrics. If we didn't find a font, use the frame's
19742 default font and calculate the width of the character
19743 from the charset width; this is what old redisplay code
19744 did. */
19745
19746 pcm = rif->per_char_metric (font, &char2b,
19747 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19748
19749 if (font_not_found_p || !pcm)
19750 {
19751 int charset = CHAR_CHARSET (it->char_to_display);
19752
19753 it->glyph_not_available_p = 1;
19754 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19755 * CHARSET_WIDTH (charset));
19756 it->phys_ascent = FONT_BASE (font) + boff;
19757 it->phys_descent = FONT_DESCENT (font) - boff;
19758 }
19759 else
19760 {
19761 it->pixel_width = pcm->width;
19762 it->phys_ascent = pcm->ascent + boff;
19763 it->phys_descent = pcm->descent - boff;
19764 if (it->glyph_row
19765 && (pcm->lbearing < 0
19766 || pcm->rbearing > pcm->width))
19767 it->glyph_row->contains_overlapping_glyphs_p = 1;
19768 }
19769 it->nglyphs = 1;
19770 it->ascent = FONT_BASE (font) + boff;
19771 it->descent = FONT_DESCENT (font) - boff;
19772 if (face->box != FACE_NO_BOX)
19773 {
19774 int thick = face->box_line_width;
19775
19776 if (thick > 0)
19777 {
19778 it->ascent += thick;
19779 it->descent += thick;
19780 }
19781 else
19782 thick = - thick;
19783
19784 if (it->start_of_box_run_p)
19785 it->pixel_width += thick;
19786 if (it->end_of_box_run_p)
19787 it->pixel_width += thick;
19788 }
19789
19790 /* If face has an overline, add the height of the overline
19791 (1 pixel) and a 1 pixel margin to the character height. */
19792 if (face->overline_p)
19793 it->ascent += 2;
19794
19795 take_vertical_position_into_account (it);
19796
19797 if (it->glyph_row)
19798 append_glyph (it);
19799 }
19800 it->multibyte_p = saved_multibyte_p;
19801 }
19802 else if (it->what == IT_COMPOSITION)
19803 {
19804 /* Note: A composition is represented as one glyph in the
19805 glyph matrix. There are no padding glyphs. */
19806 XChar2b char2b;
19807 XFontStruct *font;
19808 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19809 XCharStruct *pcm;
19810 int font_not_found_p;
19811 struct font_info *font_info;
19812 int boff; /* baseline offset */
19813 struct composition *cmp = composition_table[it->cmp_id];
19814
19815 /* Maybe translate single-byte characters to multibyte. */
19816 it->char_to_display = it->c;
19817 if (unibyte_display_via_language_environment
19818 && SINGLE_BYTE_CHAR_P (it->c)
19819 && (it->c >= 0240
19820 || (it->c >= 0200
19821 && !NILP (Vnonascii_translation_table))))
19822 {
19823 it->char_to_display = unibyte_char_to_multibyte (it->c);
19824 }
19825
19826 /* Get face and font to use. Encode IT->char_to_display. */
19827 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19828 face = FACE_FROM_ID (it->f, it->face_id);
19829 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19830 &char2b, it->multibyte_p, 0);
19831 font = face->font;
19832
19833 /* When no suitable font found, use the default font. */
19834 font_not_found_p = font == NULL;
19835 if (font_not_found_p)
19836 {
19837 font = FRAME_FONT (it->f);
19838 boff = FRAME_BASELINE_OFFSET (it->f);
19839 font_info = NULL;
19840 }
19841 else
19842 {
19843 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19844 boff = font_info->baseline_offset;
19845 if (font_info->vertical_centering)
19846 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19847 }
19848
19849 /* There are no padding glyphs, so there is only one glyph to
19850 produce for the composition. Important is that pixel_width,
19851 ascent and descent are the values of what is drawn by
19852 draw_glyphs (i.e. the values of the overall glyphs composed). */
19853 it->nglyphs = 1;
19854
19855 /* If we have not yet calculated pixel size data of glyphs of
19856 the composition for the current face font, calculate them
19857 now. Theoretically, we have to check all fonts for the
19858 glyphs, but that requires much time and memory space. So,
19859 here we check only the font of the first glyph. This leads
19860 to incorrect display very rarely, and C-l (recenter) can
19861 correct the display anyway. */
19862 if (cmp->font != (void *) font)
19863 {
19864 /* Ascent and descent of the font of the first character of
19865 this composition (adjusted by baseline offset). Ascent
19866 and descent of overall glyphs should not be less than
19867 them respectively. */
19868 int font_ascent = FONT_BASE (font) + boff;
19869 int font_descent = FONT_DESCENT (font) - boff;
19870 /* Bounding box of the overall glyphs. */
19871 int leftmost, rightmost, lowest, highest;
19872 int i, width, ascent, descent;
19873
19874 cmp->font = (void *) font;
19875
19876 /* Initialize the bounding box. */
19877 if (font_info
19878 && (pcm = rif->per_char_metric (font, &char2b,
19879 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19880 {
19881 width = pcm->width;
19882 ascent = pcm->ascent;
19883 descent = pcm->descent;
19884 }
19885 else
19886 {
19887 width = FONT_WIDTH (font);
19888 ascent = FONT_BASE (font);
19889 descent = FONT_DESCENT (font);
19890 }
19891
19892 rightmost = width;
19893 lowest = - descent + boff;
19894 highest = ascent + boff;
19895 leftmost = 0;
19896
19897 if (font_info
19898 && font_info->default_ascent
19899 && CHAR_TABLE_P (Vuse_default_ascent)
19900 && !NILP (Faref (Vuse_default_ascent,
19901 make_number (it->char_to_display))))
19902 highest = font_info->default_ascent + boff;
19903
19904 /* Draw the first glyph at the normal position. It may be
19905 shifted to right later if some other glyphs are drawn at
19906 the left. */
19907 cmp->offsets[0] = 0;
19908 cmp->offsets[1] = boff;
19909
19910 /* Set cmp->offsets for the remaining glyphs. */
19911 for (i = 1; i < cmp->glyph_len; i++)
19912 {
19913 int left, right, btm, top;
19914 int ch = COMPOSITION_GLYPH (cmp, i);
19915 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19916
19917 face = FACE_FROM_ID (it->f, face_id);
19918 get_char_face_and_encoding (it->f, ch, face->id,
19919 &char2b, it->multibyte_p, 0);
19920 font = face->font;
19921 if (font == NULL)
19922 {
19923 font = FRAME_FONT (it->f);
19924 boff = FRAME_BASELINE_OFFSET (it->f);
19925 font_info = NULL;
19926 }
19927 else
19928 {
19929 font_info
19930 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19931 boff = font_info->baseline_offset;
19932 if (font_info->vertical_centering)
19933 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19934 }
19935
19936 if (font_info
19937 && (pcm = rif->per_char_metric (font, &char2b,
19938 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19939 {
19940 width = pcm->width;
19941 ascent = pcm->ascent;
19942 descent = pcm->descent;
19943 }
19944 else
19945 {
19946 width = FONT_WIDTH (font);
19947 ascent = 1;
19948 descent = 0;
19949 }
19950
19951 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19952 {
19953 /* Relative composition with or without
19954 alternate chars. */
19955 left = (leftmost + rightmost - width) / 2;
19956 btm = - descent + boff;
19957 if (font_info && font_info->relative_compose
19958 && (! CHAR_TABLE_P (Vignore_relative_composition)
19959 || NILP (Faref (Vignore_relative_composition,
19960 make_number (ch)))))
19961 {
19962
19963 if (- descent >= font_info->relative_compose)
19964 /* One extra pixel between two glyphs. */
19965 btm = highest + 1;
19966 else if (ascent <= 0)
19967 /* One extra pixel between two glyphs. */
19968 btm = lowest - 1 - ascent - descent;
19969 }
19970 }
19971 else
19972 {
19973 /* A composition rule is specified by an integer
19974 value that encodes global and new reference
19975 points (GREF and NREF). GREF and NREF are
19976 specified by numbers as below:
19977
19978 0---1---2 -- ascent
19979 | |
19980 | |
19981 | |
19982 9--10--11 -- center
19983 | |
19984 ---3---4---5--- baseline
19985 | |
19986 6---7---8 -- descent
19987 */
19988 int rule = COMPOSITION_RULE (cmp, i);
19989 int gref, nref, grefx, grefy, nrefx, nrefy;
19990
19991 COMPOSITION_DECODE_RULE (rule, gref, nref);
19992 grefx = gref % 3, nrefx = nref % 3;
19993 grefy = gref / 3, nrefy = nref / 3;
19994
19995 left = (leftmost
19996 + grefx * (rightmost - leftmost) / 2
19997 - nrefx * width / 2);
19998 btm = ((grefy == 0 ? highest
19999 : grefy == 1 ? 0
20000 : grefy == 2 ? lowest
20001 : (highest + lowest) / 2)
20002 - (nrefy == 0 ? ascent + descent
20003 : nrefy == 1 ? descent - boff
20004 : nrefy == 2 ? 0
20005 : (ascent + descent) / 2));
20006 }
20007
20008 cmp->offsets[i * 2] = left;
20009 cmp->offsets[i * 2 + 1] = btm + descent;
20010
20011 /* Update the bounding box of the overall glyphs. */
20012 right = left + width;
20013 top = btm + descent + ascent;
20014 if (left < leftmost)
20015 leftmost = left;
20016 if (right > rightmost)
20017 rightmost = right;
20018 if (top > highest)
20019 highest = top;
20020 if (btm < lowest)
20021 lowest = btm;
20022 }
20023
20024 /* If there are glyphs whose x-offsets are negative,
20025 shift all glyphs to the right and make all x-offsets
20026 non-negative. */
20027 if (leftmost < 0)
20028 {
20029 for (i = 0; i < cmp->glyph_len; i++)
20030 cmp->offsets[i * 2] -= leftmost;
20031 rightmost -= leftmost;
20032 }
20033
20034 cmp->pixel_width = rightmost;
20035 cmp->ascent = highest;
20036 cmp->descent = - lowest;
20037 if (cmp->ascent < font_ascent)
20038 cmp->ascent = font_ascent;
20039 if (cmp->descent < font_descent)
20040 cmp->descent = font_descent;
20041 }
20042
20043 it->pixel_width = cmp->pixel_width;
20044 it->ascent = it->phys_ascent = cmp->ascent;
20045 it->descent = it->phys_descent = cmp->descent;
20046
20047 if (face->box != FACE_NO_BOX)
20048 {
20049 int thick = face->box_line_width;
20050
20051 if (thick > 0)
20052 {
20053 it->ascent += thick;
20054 it->descent += thick;
20055 }
20056 else
20057 thick = - thick;
20058
20059 if (it->start_of_box_run_p)
20060 it->pixel_width += thick;
20061 if (it->end_of_box_run_p)
20062 it->pixel_width += thick;
20063 }
20064
20065 /* If face has an overline, add the height of the overline
20066 (1 pixel) and a 1 pixel margin to the character height. */
20067 if (face->overline_p)
20068 it->ascent += 2;
20069
20070 take_vertical_position_into_account (it);
20071
20072 if (it->glyph_row)
20073 append_composite_glyph (it);
20074 }
20075 else if (it->what == IT_IMAGE)
20076 produce_image_glyph (it);
20077 else if (it->what == IT_STRETCH)
20078 produce_stretch_glyph (it);
20079
20080 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20081 because this isn't true for images with `:ascent 100'. */
20082 xassert (it->ascent >= 0 && it->descent >= 0);
20083 if (it->area == TEXT_AREA)
20084 it->current_x += it->pixel_width;
20085
20086 if (extra_line_spacing > 0)
20087 {
20088 it->descent += extra_line_spacing;
20089 if (extra_line_spacing > it->max_extra_line_spacing)
20090 it->max_extra_line_spacing = extra_line_spacing;
20091 }
20092
20093 it->max_ascent = max (it->max_ascent, it->ascent);
20094 it->max_descent = max (it->max_descent, it->descent);
20095 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20096 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20097 }
20098
20099 /* EXPORT for RIF:
20100 Output LEN glyphs starting at START at the nominal cursor position.
20101 Advance the nominal cursor over the text. The global variable
20102 updated_window contains the window being updated, updated_row is
20103 the glyph row being updated, and updated_area is the area of that
20104 row being updated. */
20105
20106 void
20107 x_write_glyphs (start, len)
20108 struct glyph *start;
20109 int len;
20110 {
20111 int x, hpos;
20112
20113 xassert (updated_window && updated_row);
20114 BLOCK_INPUT;
20115
20116 /* Write glyphs. */
20117
20118 hpos = start - updated_row->glyphs[updated_area];
20119 x = draw_glyphs (updated_window, output_cursor.x,
20120 updated_row, updated_area,
20121 hpos, hpos + len,
20122 DRAW_NORMAL_TEXT, 0);
20123
20124 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20125 if (updated_area == TEXT_AREA
20126 && updated_window->phys_cursor_on_p
20127 && updated_window->phys_cursor.vpos == output_cursor.vpos
20128 && updated_window->phys_cursor.hpos >= hpos
20129 && updated_window->phys_cursor.hpos < hpos + len)
20130 updated_window->phys_cursor_on_p = 0;
20131
20132 UNBLOCK_INPUT;
20133
20134 /* Advance the output cursor. */
20135 output_cursor.hpos += len;
20136 output_cursor.x = x;
20137 }
20138
20139
20140 /* EXPORT for RIF:
20141 Insert LEN glyphs from START at the nominal cursor position. */
20142
20143 void
20144 x_insert_glyphs (start, len)
20145 struct glyph *start;
20146 int len;
20147 {
20148 struct frame *f;
20149 struct window *w;
20150 int line_height, shift_by_width, shifted_region_width;
20151 struct glyph_row *row;
20152 struct glyph *glyph;
20153 int frame_x, frame_y, hpos;
20154
20155 xassert (updated_window && updated_row);
20156 BLOCK_INPUT;
20157 w = updated_window;
20158 f = XFRAME (WINDOW_FRAME (w));
20159
20160 /* Get the height of the line we are in. */
20161 row = updated_row;
20162 line_height = row->height;
20163
20164 /* Get the width of the glyphs to insert. */
20165 shift_by_width = 0;
20166 for (glyph = start; glyph < start + len; ++glyph)
20167 shift_by_width += glyph->pixel_width;
20168
20169 /* Get the width of the region to shift right. */
20170 shifted_region_width = (window_box_width (w, updated_area)
20171 - output_cursor.x
20172 - shift_by_width);
20173
20174 /* Shift right. */
20175 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20176 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20177
20178 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20179 line_height, shift_by_width);
20180
20181 /* Write the glyphs. */
20182 hpos = start - row->glyphs[updated_area];
20183 draw_glyphs (w, output_cursor.x, row, updated_area,
20184 hpos, hpos + len,
20185 DRAW_NORMAL_TEXT, 0);
20186
20187 /* Advance the output cursor. */
20188 output_cursor.hpos += len;
20189 output_cursor.x += shift_by_width;
20190 UNBLOCK_INPUT;
20191 }
20192
20193
20194 /* EXPORT for RIF:
20195 Erase the current text line from the nominal cursor position
20196 (inclusive) to pixel column TO_X (exclusive). The idea is that
20197 everything from TO_X onward is already erased.
20198
20199 TO_X is a pixel position relative to updated_area of
20200 updated_window. TO_X == -1 means clear to the end of this area. */
20201
20202 void
20203 x_clear_end_of_line (to_x)
20204 int to_x;
20205 {
20206 struct frame *f;
20207 struct window *w = updated_window;
20208 int max_x, min_y, max_y;
20209 int from_x, from_y, to_y;
20210
20211 xassert (updated_window && updated_row);
20212 f = XFRAME (w->frame);
20213
20214 if (updated_row->full_width_p)
20215 max_x = WINDOW_TOTAL_WIDTH (w);
20216 else
20217 max_x = window_box_width (w, updated_area);
20218 max_y = window_text_bottom_y (w);
20219
20220 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20221 of window. For TO_X > 0, truncate to end of drawing area. */
20222 if (to_x == 0)
20223 return;
20224 else if (to_x < 0)
20225 to_x = max_x;
20226 else
20227 to_x = min (to_x, max_x);
20228
20229 to_y = min (max_y, output_cursor.y + updated_row->height);
20230
20231 /* Notice if the cursor will be cleared by this operation. */
20232 if (!updated_row->full_width_p)
20233 notice_overwritten_cursor (w, updated_area,
20234 output_cursor.x, -1,
20235 updated_row->y,
20236 MATRIX_ROW_BOTTOM_Y (updated_row));
20237
20238 from_x = output_cursor.x;
20239
20240 /* Translate to frame coordinates. */
20241 if (updated_row->full_width_p)
20242 {
20243 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20244 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20245 }
20246 else
20247 {
20248 int area_left = window_box_left (w, updated_area);
20249 from_x += area_left;
20250 to_x += area_left;
20251 }
20252
20253 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20254 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20255 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20256
20257 /* Prevent inadvertently clearing to end of the X window. */
20258 if (to_x > from_x && to_y > from_y)
20259 {
20260 BLOCK_INPUT;
20261 rif->clear_frame_area (f, from_x, from_y,
20262 to_x - from_x, to_y - from_y);
20263 UNBLOCK_INPUT;
20264 }
20265 }
20266
20267 #endif /* HAVE_WINDOW_SYSTEM */
20268
20269
20270 \f
20271 /***********************************************************************
20272 Cursor types
20273 ***********************************************************************/
20274
20275 /* Value is the internal representation of the specified cursor type
20276 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20277 of the bar cursor. */
20278
20279 static enum text_cursor_kinds
20280 get_specified_cursor_type (arg, width)
20281 Lisp_Object arg;
20282 int *width;
20283 {
20284 enum text_cursor_kinds type;
20285
20286 if (NILP (arg))
20287 return NO_CURSOR;
20288
20289 if (EQ (arg, Qbox))
20290 return FILLED_BOX_CURSOR;
20291
20292 if (EQ (arg, Qhollow))
20293 return HOLLOW_BOX_CURSOR;
20294
20295 if (EQ (arg, Qbar))
20296 {
20297 *width = 2;
20298 return BAR_CURSOR;
20299 }
20300
20301 if (CONSP (arg)
20302 && EQ (XCAR (arg), Qbar)
20303 && INTEGERP (XCDR (arg))
20304 && XINT (XCDR (arg)) >= 0)
20305 {
20306 *width = XINT (XCDR (arg));
20307 return BAR_CURSOR;
20308 }
20309
20310 if (EQ (arg, Qhbar))
20311 {
20312 *width = 2;
20313 return HBAR_CURSOR;
20314 }
20315
20316 if (CONSP (arg)
20317 && EQ (XCAR (arg), Qhbar)
20318 && INTEGERP (XCDR (arg))
20319 && XINT (XCDR (arg)) >= 0)
20320 {
20321 *width = XINT (XCDR (arg));
20322 return HBAR_CURSOR;
20323 }
20324
20325 /* Treat anything unknown as "hollow box cursor".
20326 It was bad to signal an error; people have trouble fixing
20327 .Xdefaults with Emacs, when it has something bad in it. */
20328 type = HOLLOW_BOX_CURSOR;
20329
20330 return type;
20331 }
20332
20333 /* Set the default cursor types for specified frame. */
20334 void
20335 set_frame_cursor_types (f, arg)
20336 struct frame *f;
20337 Lisp_Object arg;
20338 {
20339 int width;
20340 Lisp_Object tem;
20341
20342 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20343 FRAME_CURSOR_WIDTH (f) = width;
20344
20345 /* By default, set up the blink-off state depending on the on-state. */
20346
20347 tem = Fassoc (arg, Vblink_cursor_alist);
20348 if (!NILP (tem))
20349 {
20350 FRAME_BLINK_OFF_CURSOR (f)
20351 = get_specified_cursor_type (XCDR (tem), &width);
20352 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20353 }
20354 else
20355 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20356 }
20357
20358
20359 /* Return the cursor we want to be displayed in window W. Return
20360 width of bar/hbar cursor through WIDTH arg. Return with
20361 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20362 (i.e. if the `system caret' should track this cursor).
20363
20364 In a mini-buffer window, we want the cursor only to appear if we
20365 are reading input from this window. For the selected window, we
20366 want the cursor type given by the frame parameter or buffer local
20367 setting of cursor-type. If explicitly marked off, draw no cursor.
20368 In all other cases, we want a hollow box cursor. */
20369
20370 static enum text_cursor_kinds
20371 get_window_cursor_type (w, glyph, width, active_cursor)
20372 struct window *w;
20373 struct glyph *glyph;
20374 int *width;
20375 int *active_cursor;
20376 {
20377 struct frame *f = XFRAME (w->frame);
20378 struct buffer *b = XBUFFER (w->buffer);
20379 int cursor_type = DEFAULT_CURSOR;
20380 Lisp_Object alt_cursor;
20381 int non_selected = 0;
20382
20383 *active_cursor = 1;
20384
20385 /* Echo area */
20386 if (cursor_in_echo_area
20387 && FRAME_HAS_MINIBUF_P (f)
20388 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20389 {
20390 if (w == XWINDOW (echo_area_window))
20391 {
20392 *width = FRAME_CURSOR_WIDTH (f);
20393 return FRAME_DESIRED_CURSOR (f);
20394 }
20395
20396 *active_cursor = 0;
20397 non_selected = 1;
20398 }
20399
20400 /* Nonselected window or nonselected frame. */
20401 else if (w != XWINDOW (f->selected_window)
20402 #ifdef HAVE_WINDOW_SYSTEM
20403 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20404 #endif
20405 )
20406 {
20407 *active_cursor = 0;
20408
20409 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20410 return NO_CURSOR;
20411
20412 non_selected = 1;
20413 }
20414
20415 /* Never display a cursor in a window in which cursor-type is nil. */
20416 if (NILP (b->cursor_type))
20417 return NO_CURSOR;
20418
20419 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20420 if (non_selected)
20421 {
20422 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20423 return get_specified_cursor_type (alt_cursor, width);
20424 }
20425
20426 /* Get the normal cursor type for this window. */
20427 if (EQ (b->cursor_type, Qt))
20428 {
20429 cursor_type = FRAME_DESIRED_CURSOR (f);
20430 *width = FRAME_CURSOR_WIDTH (f);
20431 }
20432 else
20433 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20434
20435 /* Use normal cursor if not blinked off. */
20436 if (!w->cursor_off_p)
20437 {
20438 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20439 if (cursor_type == FILLED_BOX_CURSOR)
20440 cursor_type = HOLLOW_BOX_CURSOR;
20441 }
20442 return cursor_type;
20443 }
20444
20445 /* Cursor is blinked off, so determine how to "toggle" it. */
20446
20447 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20448 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20449 return get_specified_cursor_type (XCDR (alt_cursor), width);
20450
20451 /* Then see if frame has specified a specific blink off cursor type. */
20452 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20453 {
20454 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20455 return FRAME_BLINK_OFF_CURSOR (f);
20456 }
20457
20458 #if 0
20459 /* Some people liked having a permanently visible blinking cursor,
20460 while others had very strong opinions against it. So it was
20461 decided to remove it. KFS 2003-09-03 */
20462
20463 /* Finally perform built-in cursor blinking:
20464 filled box <-> hollow box
20465 wide [h]bar <-> narrow [h]bar
20466 narrow [h]bar <-> no cursor
20467 other type <-> no cursor */
20468
20469 if (cursor_type == FILLED_BOX_CURSOR)
20470 return HOLLOW_BOX_CURSOR;
20471
20472 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20473 {
20474 *width = 1;
20475 return cursor_type;
20476 }
20477 #endif
20478
20479 return NO_CURSOR;
20480 }
20481
20482
20483 #ifdef HAVE_WINDOW_SYSTEM
20484
20485 /* Notice when the text cursor of window W has been completely
20486 overwritten by a drawing operation that outputs glyphs in AREA
20487 starting at X0 and ending at X1 in the line starting at Y0 and
20488 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20489 the rest of the line after X0 has been written. Y coordinates
20490 are window-relative. */
20491
20492 static void
20493 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20494 struct window *w;
20495 enum glyph_row_area area;
20496 int x0, y0, x1, y1;
20497 {
20498 int cx0, cx1, cy0, cy1;
20499 struct glyph_row *row;
20500
20501 if (!w->phys_cursor_on_p)
20502 return;
20503 if (area != TEXT_AREA)
20504 return;
20505
20506 if (w->phys_cursor.vpos < 0
20507 || w->phys_cursor.vpos >= w->current_matrix->nrows
20508 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20509 !(row->enabled_p && row->displays_text_p)))
20510 return;
20511
20512 if (row->cursor_in_fringe_p)
20513 {
20514 row->cursor_in_fringe_p = 0;
20515 draw_fringe_bitmap (w, row, 0);
20516 w->phys_cursor_on_p = 0;
20517 return;
20518 }
20519
20520 cx0 = w->phys_cursor.x;
20521 cx1 = cx0 + w->phys_cursor_width;
20522 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20523 return;
20524
20525 /* The cursor image will be completely removed from the
20526 screen if the output area intersects the cursor area in
20527 y-direction. When we draw in [y0 y1[, and some part of
20528 the cursor is at y < y0, that part must have been drawn
20529 before. When scrolling, the cursor is erased before
20530 actually scrolling, so we don't come here. When not
20531 scrolling, the rows above the old cursor row must have
20532 changed, and in this case these rows must have written
20533 over the cursor image.
20534
20535 Likewise if part of the cursor is below y1, with the
20536 exception of the cursor being in the first blank row at
20537 the buffer and window end because update_text_area
20538 doesn't draw that row. (Except when it does, but
20539 that's handled in update_text_area.) */
20540
20541 cy0 = w->phys_cursor.y;
20542 cy1 = cy0 + w->phys_cursor_height;
20543 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20544 return;
20545
20546 w->phys_cursor_on_p = 0;
20547 }
20548
20549 #endif /* HAVE_WINDOW_SYSTEM */
20550
20551 \f
20552 /************************************************************************
20553 Mouse Face
20554 ************************************************************************/
20555
20556 #ifdef HAVE_WINDOW_SYSTEM
20557
20558 /* EXPORT for RIF:
20559 Fix the display of area AREA of overlapping row ROW in window W. */
20560
20561 void
20562 x_fix_overlapping_area (w, row, area)
20563 struct window *w;
20564 struct glyph_row *row;
20565 enum glyph_row_area area;
20566 {
20567 int i, x;
20568
20569 BLOCK_INPUT;
20570
20571 x = 0;
20572 for (i = 0; i < row->used[area];)
20573 {
20574 if (row->glyphs[area][i].overlaps_vertically_p)
20575 {
20576 int start = i, start_x = x;
20577
20578 do
20579 {
20580 x += row->glyphs[area][i].pixel_width;
20581 ++i;
20582 }
20583 while (i < row->used[area]
20584 && row->glyphs[area][i].overlaps_vertically_p);
20585
20586 draw_glyphs (w, start_x, row, area,
20587 start, i,
20588 DRAW_NORMAL_TEXT, 1);
20589 }
20590 else
20591 {
20592 x += row->glyphs[area][i].pixel_width;
20593 ++i;
20594 }
20595 }
20596
20597 UNBLOCK_INPUT;
20598 }
20599
20600
20601 /* EXPORT:
20602 Draw the cursor glyph of window W in glyph row ROW. See the
20603 comment of draw_glyphs for the meaning of HL. */
20604
20605 void
20606 draw_phys_cursor_glyph (w, row, hl)
20607 struct window *w;
20608 struct glyph_row *row;
20609 enum draw_glyphs_face hl;
20610 {
20611 /* If cursor hpos is out of bounds, don't draw garbage. This can
20612 happen in mini-buffer windows when switching between echo area
20613 glyphs and mini-buffer. */
20614 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20615 {
20616 int on_p = w->phys_cursor_on_p;
20617 int x1;
20618 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20619 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20620 hl, 0);
20621 w->phys_cursor_on_p = on_p;
20622
20623 if (hl == DRAW_CURSOR)
20624 w->phys_cursor_width = x1 - w->phys_cursor.x;
20625 /* When we erase the cursor, and ROW is overlapped by other
20626 rows, make sure that these overlapping parts of other rows
20627 are redrawn. */
20628 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20629 {
20630 if (row > w->current_matrix->rows
20631 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20632 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20633
20634 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20635 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20636 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20637 }
20638 }
20639 }
20640
20641
20642 /* EXPORT:
20643 Erase the image of a cursor of window W from the screen. */
20644
20645 void
20646 erase_phys_cursor (w)
20647 struct window *w;
20648 {
20649 struct frame *f = XFRAME (w->frame);
20650 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20651 int hpos = w->phys_cursor.hpos;
20652 int vpos = w->phys_cursor.vpos;
20653 int mouse_face_here_p = 0;
20654 struct glyph_matrix *active_glyphs = w->current_matrix;
20655 struct glyph_row *cursor_row;
20656 struct glyph *cursor_glyph;
20657 enum draw_glyphs_face hl;
20658
20659 /* No cursor displayed or row invalidated => nothing to do on the
20660 screen. */
20661 if (w->phys_cursor_type == NO_CURSOR)
20662 goto mark_cursor_off;
20663
20664 /* VPOS >= active_glyphs->nrows means that window has been resized.
20665 Don't bother to erase the cursor. */
20666 if (vpos >= active_glyphs->nrows)
20667 goto mark_cursor_off;
20668
20669 /* If row containing cursor is marked invalid, there is nothing we
20670 can do. */
20671 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20672 if (!cursor_row->enabled_p)
20673 goto mark_cursor_off;
20674
20675 /* If line spacing is > 0, old cursor may only be partially visible in
20676 window after split-window. So adjust visible height. */
20677 cursor_row->visible_height = min (cursor_row->visible_height,
20678 window_text_bottom_y (w) - cursor_row->y);
20679
20680 /* If row is completely invisible, don't attempt to delete a cursor which
20681 isn't there. This can happen if cursor is at top of a window, and
20682 we switch to a buffer with a header line in that window. */
20683 if (cursor_row->visible_height <= 0)
20684 goto mark_cursor_off;
20685
20686 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20687 if (cursor_row->cursor_in_fringe_p)
20688 {
20689 cursor_row->cursor_in_fringe_p = 0;
20690 draw_fringe_bitmap (w, cursor_row, 0);
20691 goto mark_cursor_off;
20692 }
20693
20694 /* This can happen when the new row is shorter than the old one.
20695 In this case, either draw_glyphs or clear_end_of_line
20696 should have cleared the cursor. Note that we wouldn't be
20697 able to erase the cursor in this case because we don't have a
20698 cursor glyph at hand. */
20699 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20700 goto mark_cursor_off;
20701
20702 /* If the cursor is in the mouse face area, redisplay that when
20703 we clear the cursor. */
20704 if (! NILP (dpyinfo->mouse_face_window)
20705 && w == XWINDOW (dpyinfo->mouse_face_window)
20706 && (vpos > dpyinfo->mouse_face_beg_row
20707 || (vpos == dpyinfo->mouse_face_beg_row
20708 && hpos >= dpyinfo->mouse_face_beg_col))
20709 && (vpos < dpyinfo->mouse_face_end_row
20710 || (vpos == dpyinfo->mouse_face_end_row
20711 && hpos < dpyinfo->mouse_face_end_col))
20712 /* Don't redraw the cursor's spot in mouse face if it is at the
20713 end of a line (on a newline). The cursor appears there, but
20714 mouse highlighting does not. */
20715 && cursor_row->used[TEXT_AREA] > hpos)
20716 mouse_face_here_p = 1;
20717
20718 /* Maybe clear the display under the cursor. */
20719 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20720 {
20721 int x, y;
20722 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20723 int width;
20724
20725 cursor_glyph = get_phys_cursor_glyph (w);
20726 if (cursor_glyph == NULL)
20727 goto mark_cursor_off;
20728
20729 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20730 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20731 width = min (cursor_glyph->pixel_width,
20732 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20733
20734 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20735 }
20736
20737 /* Erase the cursor by redrawing the character underneath it. */
20738 if (mouse_face_here_p)
20739 hl = DRAW_MOUSE_FACE;
20740 else
20741 hl = DRAW_NORMAL_TEXT;
20742 draw_phys_cursor_glyph (w, cursor_row, hl);
20743
20744 mark_cursor_off:
20745 w->phys_cursor_on_p = 0;
20746 w->phys_cursor_type = NO_CURSOR;
20747 }
20748
20749
20750 /* EXPORT:
20751 Display or clear cursor of window W. If ON is zero, clear the
20752 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20753 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20754
20755 void
20756 display_and_set_cursor (w, on, hpos, vpos, x, y)
20757 struct window *w;
20758 int on, hpos, vpos, x, y;
20759 {
20760 struct frame *f = XFRAME (w->frame);
20761 int new_cursor_type;
20762 int new_cursor_width;
20763 int active_cursor;
20764 struct glyph_row *glyph_row;
20765 struct glyph *glyph;
20766
20767 /* This is pointless on invisible frames, and dangerous on garbaged
20768 windows and frames; in the latter case, the frame or window may
20769 be in the midst of changing its size, and x and y may be off the
20770 window. */
20771 if (! FRAME_VISIBLE_P (f)
20772 || FRAME_GARBAGED_P (f)
20773 || vpos >= w->current_matrix->nrows
20774 || hpos >= w->current_matrix->matrix_w)
20775 return;
20776
20777 /* If cursor is off and we want it off, return quickly. */
20778 if (!on && !w->phys_cursor_on_p)
20779 return;
20780
20781 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20782 /* If cursor row is not enabled, we don't really know where to
20783 display the cursor. */
20784 if (!glyph_row->enabled_p)
20785 {
20786 w->phys_cursor_on_p = 0;
20787 return;
20788 }
20789
20790 glyph = NULL;
20791 if (!glyph_row->exact_window_width_line_p
20792 || hpos < glyph_row->used[TEXT_AREA])
20793 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20794
20795 xassert (interrupt_input_blocked);
20796
20797 /* Set new_cursor_type to the cursor we want to be displayed. */
20798 new_cursor_type = get_window_cursor_type (w, glyph,
20799 &new_cursor_width, &active_cursor);
20800
20801 /* If cursor is currently being shown and we don't want it to be or
20802 it is in the wrong place, or the cursor type is not what we want,
20803 erase it. */
20804 if (w->phys_cursor_on_p
20805 && (!on
20806 || w->phys_cursor.x != x
20807 || w->phys_cursor.y != y
20808 || new_cursor_type != w->phys_cursor_type
20809 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20810 && new_cursor_width != w->phys_cursor_width)))
20811 erase_phys_cursor (w);
20812
20813 /* Don't check phys_cursor_on_p here because that flag is only set
20814 to zero in some cases where we know that the cursor has been
20815 completely erased, to avoid the extra work of erasing the cursor
20816 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20817 still not be visible, or it has only been partly erased. */
20818 if (on)
20819 {
20820 w->phys_cursor_ascent = glyph_row->ascent;
20821 w->phys_cursor_height = glyph_row->height;
20822
20823 /* Set phys_cursor_.* before x_draw_.* is called because some
20824 of them may need the information. */
20825 w->phys_cursor.x = x;
20826 w->phys_cursor.y = glyph_row->y;
20827 w->phys_cursor.hpos = hpos;
20828 w->phys_cursor.vpos = vpos;
20829 }
20830
20831 rif->draw_window_cursor (w, glyph_row, x, y,
20832 new_cursor_type, new_cursor_width,
20833 on, active_cursor);
20834 }
20835
20836
20837 /* Switch the display of W's cursor on or off, according to the value
20838 of ON. */
20839
20840 static void
20841 update_window_cursor (w, on)
20842 struct window *w;
20843 int on;
20844 {
20845 /* Don't update cursor in windows whose frame is in the process
20846 of being deleted. */
20847 if (w->current_matrix)
20848 {
20849 BLOCK_INPUT;
20850 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20851 w->phys_cursor.x, w->phys_cursor.y);
20852 UNBLOCK_INPUT;
20853 }
20854 }
20855
20856
20857 /* Call update_window_cursor with parameter ON_P on all leaf windows
20858 in the window tree rooted at W. */
20859
20860 static void
20861 update_cursor_in_window_tree (w, on_p)
20862 struct window *w;
20863 int on_p;
20864 {
20865 while (w)
20866 {
20867 if (!NILP (w->hchild))
20868 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20869 else if (!NILP (w->vchild))
20870 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20871 else
20872 update_window_cursor (w, on_p);
20873
20874 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20875 }
20876 }
20877
20878
20879 /* EXPORT:
20880 Display the cursor on window W, or clear it, according to ON_P.
20881 Don't change the cursor's position. */
20882
20883 void
20884 x_update_cursor (f, on_p)
20885 struct frame *f;
20886 int on_p;
20887 {
20888 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20889 }
20890
20891
20892 /* EXPORT:
20893 Clear the cursor of window W to background color, and mark the
20894 cursor as not shown. This is used when the text where the cursor
20895 is is about to be rewritten. */
20896
20897 void
20898 x_clear_cursor (w)
20899 struct window *w;
20900 {
20901 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20902 update_window_cursor (w, 0);
20903 }
20904
20905
20906 /* EXPORT:
20907 Display the active region described by mouse_face_* according to DRAW. */
20908
20909 void
20910 show_mouse_face (dpyinfo, draw)
20911 Display_Info *dpyinfo;
20912 enum draw_glyphs_face draw;
20913 {
20914 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20915 struct frame *f = XFRAME (WINDOW_FRAME (w));
20916
20917 if (/* If window is in the process of being destroyed, don't bother
20918 to do anything. */
20919 w->current_matrix != NULL
20920 /* Don't update mouse highlight if hidden */
20921 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20922 /* Recognize when we are called to operate on rows that don't exist
20923 anymore. This can happen when a window is split. */
20924 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20925 {
20926 int phys_cursor_on_p = w->phys_cursor_on_p;
20927 struct glyph_row *row, *first, *last;
20928
20929 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20930 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20931
20932 for (row = first; row <= last && row->enabled_p; ++row)
20933 {
20934 int start_hpos, end_hpos, start_x;
20935
20936 /* For all but the first row, the highlight starts at column 0. */
20937 if (row == first)
20938 {
20939 start_hpos = dpyinfo->mouse_face_beg_col;
20940 start_x = dpyinfo->mouse_face_beg_x;
20941 }
20942 else
20943 {
20944 start_hpos = 0;
20945 start_x = 0;
20946 }
20947
20948 if (row == last)
20949 end_hpos = dpyinfo->mouse_face_end_col;
20950 else
20951 end_hpos = row->used[TEXT_AREA];
20952
20953 if (end_hpos > start_hpos)
20954 {
20955 draw_glyphs (w, start_x, row, TEXT_AREA,
20956 start_hpos, end_hpos,
20957 draw, 0);
20958
20959 row->mouse_face_p
20960 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20961 }
20962 }
20963
20964 /* When we've written over the cursor, arrange for it to
20965 be displayed again. */
20966 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20967 {
20968 BLOCK_INPUT;
20969 display_and_set_cursor (w, 1,
20970 w->phys_cursor.hpos, w->phys_cursor.vpos,
20971 w->phys_cursor.x, w->phys_cursor.y);
20972 UNBLOCK_INPUT;
20973 }
20974 }
20975
20976 /* Change the mouse cursor. */
20977 if (draw == DRAW_NORMAL_TEXT)
20978 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20979 else if (draw == DRAW_MOUSE_FACE)
20980 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20981 else
20982 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20983 }
20984
20985 /* EXPORT:
20986 Clear out the mouse-highlighted active region.
20987 Redraw it un-highlighted first. Value is non-zero if mouse
20988 face was actually drawn unhighlighted. */
20989
20990 int
20991 clear_mouse_face (dpyinfo)
20992 Display_Info *dpyinfo;
20993 {
20994 int cleared = 0;
20995
20996 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20997 {
20998 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20999 cleared = 1;
21000 }
21001
21002 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21003 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21004 dpyinfo->mouse_face_window = Qnil;
21005 dpyinfo->mouse_face_overlay = Qnil;
21006 return cleared;
21007 }
21008
21009
21010 /* EXPORT:
21011 Non-zero if physical cursor of window W is within mouse face. */
21012
21013 int
21014 cursor_in_mouse_face_p (w)
21015 struct window *w;
21016 {
21017 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21018 int in_mouse_face = 0;
21019
21020 if (WINDOWP (dpyinfo->mouse_face_window)
21021 && XWINDOW (dpyinfo->mouse_face_window) == w)
21022 {
21023 int hpos = w->phys_cursor.hpos;
21024 int vpos = w->phys_cursor.vpos;
21025
21026 if (vpos >= dpyinfo->mouse_face_beg_row
21027 && vpos <= dpyinfo->mouse_face_end_row
21028 && (vpos > dpyinfo->mouse_face_beg_row
21029 || hpos >= dpyinfo->mouse_face_beg_col)
21030 && (vpos < dpyinfo->mouse_face_end_row
21031 || hpos < dpyinfo->mouse_face_end_col
21032 || dpyinfo->mouse_face_past_end))
21033 in_mouse_face = 1;
21034 }
21035
21036 return in_mouse_face;
21037 }
21038
21039
21040
21041 \f
21042 /* Find the glyph matrix position of buffer position CHARPOS in window
21043 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21044 current glyphs must be up to date. If CHARPOS is above window
21045 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21046 of last line in W. In the row containing CHARPOS, stop before glyphs
21047 having STOP as object. */
21048
21049 #if 1 /* This is a version of fast_find_position that's more correct
21050 in the presence of hscrolling, for example. I didn't install
21051 it right away because the problem fixed is minor, it failed
21052 in 20.x as well, and I think it's too risky to install
21053 so near the release of 21.1. 2001-09-25 gerd. */
21054
21055 static int
21056 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21057 struct window *w;
21058 int charpos;
21059 int *hpos, *vpos, *x, *y;
21060 Lisp_Object stop;
21061 {
21062 struct glyph_row *row, *first;
21063 struct glyph *glyph, *end;
21064 int past_end = 0;
21065
21066 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21067 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21068 {
21069 *x = first->x;
21070 *y = first->y;
21071 *hpos = 0;
21072 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21073 return 1;
21074 }
21075
21076 row = row_containing_pos (w, charpos, first, NULL, 0);
21077 if (row == NULL)
21078 {
21079 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21080 past_end = 1;
21081 }
21082
21083 /* If whole rows or last part of a row came from a display overlay,
21084 row_containing_pos will skip over such rows because their end pos
21085 equals the start pos of the overlay or interval.
21086
21087 Move back if we have a STOP object and previous row's
21088 end glyph came from STOP. */
21089 if (!NILP (stop))
21090 {
21091 struct glyph_row *prev;
21092 while ((prev = row - 1, prev >= first)
21093 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21094 && prev->used[TEXT_AREA] > 0)
21095 {
21096 struct glyph *beg = prev->glyphs[TEXT_AREA];
21097 glyph = beg + prev->used[TEXT_AREA];
21098 while (--glyph >= beg
21099 && INTEGERP (glyph->object));
21100 if (glyph < beg
21101 || !EQ (stop, glyph->object))
21102 break;
21103 row = prev;
21104 }
21105 }
21106
21107 *x = row->x;
21108 *y = row->y;
21109 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21110
21111 glyph = row->glyphs[TEXT_AREA];
21112 end = glyph + row->used[TEXT_AREA];
21113
21114 /* Skip over glyphs not having an object at the start of the row.
21115 These are special glyphs like truncation marks on terminal
21116 frames. */
21117 if (row->displays_text_p)
21118 while (glyph < end
21119 && INTEGERP (glyph->object)
21120 && !EQ (stop, glyph->object)
21121 && glyph->charpos < 0)
21122 {
21123 *x += glyph->pixel_width;
21124 ++glyph;
21125 }
21126
21127 while (glyph < end
21128 && !INTEGERP (glyph->object)
21129 && !EQ (stop, glyph->object)
21130 && (!BUFFERP (glyph->object)
21131 || glyph->charpos < charpos))
21132 {
21133 *x += glyph->pixel_width;
21134 ++glyph;
21135 }
21136
21137 *hpos = glyph - row->glyphs[TEXT_AREA];
21138 return !past_end;
21139 }
21140
21141 #else /* not 1 */
21142
21143 static int
21144 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21145 struct window *w;
21146 int pos;
21147 int *hpos, *vpos, *x, *y;
21148 Lisp_Object stop;
21149 {
21150 int i;
21151 int lastcol;
21152 int maybe_next_line_p = 0;
21153 int line_start_position;
21154 int yb = window_text_bottom_y (w);
21155 struct glyph_row *row, *best_row;
21156 int row_vpos, best_row_vpos;
21157 int current_x;
21158
21159 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21160 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21161
21162 while (row->y < yb)
21163 {
21164 if (row->used[TEXT_AREA])
21165 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21166 else
21167 line_start_position = 0;
21168
21169 if (line_start_position > pos)
21170 break;
21171 /* If the position sought is the end of the buffer,
21172 don't include the blank lines at the bottom of the window. */
21173 else if (line_start_position == pos
21174 && pos == BUF_ZV (XBUFFER (w->buffer)))
21175 {
21176 maybe_next_line_p = 1;
21177 break;
21178 }
21179 else if (line_start_position > 0)
21180 {
21181 best_row = row;
21182 best_row_vpos = row_vpos;
21183 }
21184
21185 if (row->y + row->height >= yb)
21186 break;
21187
21188 ++row;
21189 ++row_vpos;
21190 }
21191
21192 /* Find the right column within BEST_ROW. */
21193 lastcol = 0;
21194 current_x = best_row->x;
21195 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21196 {
21197 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21198 int charpos = glyph->charpos;
21199
21200 if (BUFFERP (glyph->object))
21201 {
21202 if (charpos == pos)
21203 {
21204 *hpos = i;
21205 *vpos = best_row_vpos;
21206 *x = current_x;
21207 *y = best_row->y;
21208 return 1;
21209 }
21210 else if (charpos > pos)
21211 break;
21212 }
21213 else if (EQ (glyph->object, stop))
21214 break;
21215
21216 if (charpos > 0)
21217 lastcol = i;
21218 current_x += glyph->pixel_width;
21219 }
21220
21221 /* If we're looking for the end of the buffer,
21222 and we didn't find it in the line we scanned,
21223 use the start of the following line. */
21224 if (maybe_next_line_p)
21225 {
21226 ++best_row;
21227 ++best_row_vpos;
21228 lastcol = 0;
21229 current_x = best_row->x;
21230 }
21231
21232 *vpos = best_row_vpos;
21233 *hpos = lastcol + 1;
21234 *x = current_x;
21235 *y = best_row->y;
21236 return 0;
21237 }
21238
21239 #endif /* not 1 */
21240
21241
21242 /* Find the position of the glyph for position POS in OBJECT in
21243 window W's current matrix, and return in *X, *Y the pixel
21244 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21245
21246 RIGHT_P non-zero means return the position of the right edge of the
21247 glyph, RIGHT_P zero means return the left edge position.
21248
21249 If no glyph for POS exists in the matrix, return the position of
21250 the glyph with the next smaller position that is in the matrix, if
21251 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21252 exists in the matrix, return the position of the glyph with the
21253 next larger position in OBJECT.
21254
21255 Value is non-zero if a glyph was found. */
21256
21257 static int
21258 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21259 struct window *w;
21260 int pos;
21261 Lisp_Object object;
21262 int *hpos, *vpos, *x, *y;
21263 int right_p;
21264 {
21265 int yb = window_text_bottom_y (w);
21266 struct glyph_row *r;
21267 struct glyph *best_glyph = NULL;
21268 struct glyph_row *best_row = NULL;
21269 int best_x = 0;
21270
21271 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21272 r->enabled_p && r->y < yb;
21273 ++r)
21274 {
21275 struct glyph *g = r->glyphs[TEXT_AREA];
21276 struct glyph *e = g + r->used[TEXT_AREA];
21277 int gx;
21278
21279 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21280 if (EQ (g->object, object))
21281 {
21282 if (g->charpos == pos)
21283 {
21284 best_glyph = g;
21285 best_x = gx;
21286 best_row = r;
21287 goto found;
21288 }
21289 else if (best_glyph == NULL
21290 || ((abs (g->charpos - pos)
21291 < abs (best_glyph->charpos - pos))
21292 && (right_p
21293 ? g->charpos < pos
21294 : g->charpos > pos)))
21295 {
21296 best_glyph = g;
21297 best_x = gx;
21298 best_row = r;
21299 }
21300 }
21301 }
21302
21303 found:
21304
21305 if (best_glyph)
21306 {
21307 *x = best_x;
21308 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21309
21310 if (right_p)
21311 {
21312 *x += best_glyph->pixel_width;
21313 ++*hpos;
21314 }
21315
21316 *y = best_row->y;
21317 *vpos = best_row - w->current_matrix->rows;
21318 }
21319
21320 return best_glyph != NULL;
21321 }
21322
21323
21324 /* See if position X, Y is within a hot-spot of an image. */
21325
21326 static int
21327 on_hot_spot_p (hot_spot, x, y)
21328 Lisp_Object hot_spot;
21329 int x, y;
21330 {
21331 if (!CONSP (hot_spot))
21332 return 0;
21333
21334 if (EQ (XCAR (hot_spot), Qrect))
21335 {
21336 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21337 Lisp_Object rect = XCDR (hot_spot);
21338 Lisp_Object tem;
21339 if (!CONSP (rect))
21340 return 0;
21341 if (!CONSP (XCAR (rect)))
21342 return 0;
21343 if (!CONSP (XCDR (rect)))
21344 return 0;
21345 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21346 return 0;
21347 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21348 return 0;
21349 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21350 return 0;
21351 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21352 return 0;
21353 return 1;
21354 }
21355 else if (EQ (XCAR (hot_spot), Qcircle))
21356 {
21357 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21358 Lisp_Object circ = XCDR (hot_spot);
21359 Lisp_Object lr, lx0, ly0;
21360 if (CONSP (circ)
21361 && CONSP (XCAR (circ))
21362 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21363 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21364 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21365 {
21366 double r = XFLOATINT (lr);
21367 double dx = XINT (lx0) - x;
21368 double dy = XINT (ly0) - y;
21369 return (dx * dx + dy * dy <= r * r);
21370 }
21371 }
21372 else if (EQ (XCAR (hot_spot), Qpoly))
21373 {
21374 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21375 if (VECTORP (XCDR (hot_spot)))
21376 {
21377 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21378 Lisp_Object *poly = v->contents;
21379 int n = v->size;
21380 int i;
21381 int inside = 0;
21382 Lisp_Object lx, ly;
21383 int x0, y0;
21384
21385 /* Need an even number of coordinates, and at least 3 edges. */
21386 if (n < 6 || n & 1)
21387 return 0;
21388
21389 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21390 If count is odd, we are inside polygon. Pixels on edges
21391 may or may not be included depending on actual geometry of the
21392 polygon. */
21393 if ((lx = poly[n-2], !INTEGERP (lx))
21394 || (ly = poly[n-1], !INTEGERP (lx)))
21395 return 0;
21396 x0 = XINT (lx), y0 = XINT (ly);
21397 for (i = 0; i < n; i += 2)
21398 {
21399 int x1 = x0, y1 = y0;
21400 if ((lx = poly[i], !INTEGERP (lx))
21401 || (ly = poly[i+1], !INTEGERP (ly)))
21402 return 0;
21403 x0 = XINT (lx), y0 = XINT (ly);
21404
21405 /* Does this segment cross the X line? */
21406 if (x0 >= x)
21407 {
21408 if (x1 >= x)
21409 continue;
21410 }
21411 else if (x1 < x)
21412 continue;
21413 if (y > y0 && y > y1)
21414 continue;
21415 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21416 inside = !inside;
21417 }
21418 return inside;
21419 }
21420 }
21421 /* If we don't understand the format, pretend we're not in the hot-spot. */
21422 return 0;
21423 }
21424
21425 Lisp_Object
21426 find_hot_spot (map, x, y)
21427 Lisp_Object map;
21428 int x, y;
21429 {
21430 while (CONSP (map))
21431 {
21432 if (CONSP (XCAR (map))
21433 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21434 return XCAR (map);
21435 map = XCDR (map);
21436 }
21437
21438 return Qnil;
21439 }
21440
21441 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21442 3, 3, 0,
21443 doc: /* Lookup in image map MAP coordinates X and Y.
21444 An image map is an alist where each element has the format (AREA ID PLIST).
21445 An AREA is specified as either a rectangle, a circle, or a polygon:
21446 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21447 pixel coordinates of the upper left and bottom right corners.
21448 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21449 and the radius of the circle; r may be a float or integer.
21450 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21451 vector describes one corner in the polygon.
21452 Returns the alist element for the first matching AREA in MAP. */)
21453 (map, x, y)
21454 Lisp_Object map;
21455 Lisp_Object x, y;
21456 {
21457 if (NILP (map))
21458 return Qnil;
21459
21460 CHECK_NUMBER (x);
21461 CHECK_NUMBER (y);
21462
21463 return find_hot_spot (map, XINT (x), XINT (y));
21464 }
21465
21466
21467 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21468 static void
21469 define_frame_cursor1 (f, cursor, pointer)
21470 struct frame *f;
21471 Cursor cursor;
21472 Lisp_Object pointer;
21473 {
21474 /* Do not change cursor shape while dragging mouse. */
21475 if (!NILP (do_mouse_tracking))
21476 return;
21477
21478 if (!NILP (pointer))
21479 {
21480 if (EQ (pointer, Qarrow))
21481 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21482 else if (EQ (pointer, Qhand))
21483 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21484 else if (EQ (pointer, Qtext))
21485 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21486 else if (EQ (pointer, intern ("hdrag")))
21487 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21488 #ifdef HAVE_X_WINDOWS
21489 else if (EQ (pointer, intern ("vdrag")))
21490 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21491 #endif
21492 else if (EQ (pointer, intern ("hourglass")))
21493 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21494 else if (EQ (pointer, Qmodeline))
21495 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21496 else
21497 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21498 }
21499
21500 if (cursor != No_Cursor)
21501 rif->define_frame_cursor (f, cursor);
21502 }
21503
21504 /* Take proper action when mouse has moved to the mode or header line
21505 or marginal area AREA of window W, x-position X and y-position Y.
21506 X is relative to the start of the text display area of W, so the
21507 width of bitmap areas and scroll bars must be subtracted to get a
21508 position relative to the start of the mode line. */
21509
21510 static void
21511 note_mode_line_or_margin_highlight (window, x, y, area)
21512 Lisp_Object window;
21513 int x, y;
21514 enum window_part area;
21515 {
21516 struct window *w = XWINDOW (window);
21517 struct frame *f = XFRAME (w->frame);
21518 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21519 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21520 Lisp_Object pointer = Qnil;
21521 int charpos, dx, dy, width, height;
21522 Lisp_Object string, object = Qnil;
21523 Lisp_Object pos, help;
21524
21525 Lisp_Object mouse_face;
21526 int original_x_pixel = x;
21527 struct glyph * glyph = NULL;
21528 struct glyph_row *row;
21529
21530 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21531 {
21532 int x0;
21533 struct glyph *end;
21534
21535 string = mode_line_string (w, area, &x, &y, &charpos,
21536 &object, &dx, &dy, &width, &height);
21537
21538 row = (area == ON_MODE_LINE
21539 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21540 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21541
21542 /* Find glyph */
21543 if (row->mode_line_p && row->enabled_p)
21544 {
21545 glyph = row->glyphs[TEXT_AREA];
21546 end = glyph + row->used[TEXT_AREA];
21547
21548 for (x0 = original_x_pixel;
21549 glyph < end && x0 >= glyph->pixel_width;
21550 ++glyph)
21551 x0 -= glyph->pixel_width;
21552
21553 if (glyph >= end)
21554 glyph = NULL;
21555 }
21556 }
21557 else
21558 {
21559 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21560 string = marginal_area_string (w, area, &x, &y, &charpos,
21561 &object, &dx, &dy, &width, &height);
21562 }
21563
21564 help = Qnil;
21565
21566 if (IMAGEP (object))
21567 {
21568 Lisp_Object image_map, hotspot;
21569 if ((image_map = Fplist_get (XCDR (object), QCmap),
21570 !NILP (image_map))
21571 && (hotspot = find_hot_spot (image_map, dx, dy),
21572 CONSP (hotspot))
21573 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21574 {
21575 Lisp_Object area_id, plist;
21576
21577 area_id = XCAR (hotspot);
21578 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21579 If so, we could look for mouse-enter, mouse-leave
21580 properties in PLIST (and do something...). */
21581 hotspot = XCDR (hotspot);
21582 if (CONSP (hotspot)
21583 && (plist = XCAR (hotspot), CONSP (plist)))
21584 {
21585 pointer = Fplist_get (plist, Qpointer);
21586 if (NILP (pointer))
21587 pointer = Qhand;
21588 help = Fplist_get (plist, Qhelp_echo);
21589 if (!NILP (help))
21590 {
21591 help_echo_string = help;
21592 /* Is this correct? ++kfs */
21593 XSETWINDOW (help_echo_window, w);
21594 help_echo_object = w->buffer;
21595 help_echo_pos = charpos;
21596 }
21597 }
21598 }
21599 if (NILP (pointer))
21600 pointer = Fplist_get (XCDR (object), QCpointer);
21601 }
21602
21603 if (STRINGP (string))
21604 {
21605 pos = make_number (charpos);
21606 /* If we're on a string with `help-echo' text property, arrange
21607 for the help to be displayed. This is done by setting the
21608 global variable help_echo_string to the help string. */
21609 if (NILP (help))
21610 {
21611 help = Fget_text_property (pos, Qhelp_echo, string);
21612 if (!NILP (help))
21613 {
21614 help_echo_string = help;
21615 XSETWINDOW (help_echo_window, w);
21616 help_echo_object = string;
21617 help_echo_pos = charpos;
21618 }
21619 }
21620
21621 if (NILP (pointer))
21622 pointer = Fget_text_property (pos, Qpointer, string);
21623
21624 /* Change the mouse pointer according to what is under X/Y. */
21625 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21626 {
21627 Lisp_Object map;
21628 map = Fget_text_property (pos, Qlocal_map, string);
21629 if (!KEYMAPP (map))
21630 map = Fget_text_property (pos, Qkeymap, string);
21631 if (!KEYMAPP (map))
21632 cursor = dpyinfo->vertical_scroll_bar_cursor;
21633 }
21634
21635 /* Change the mouse face according to what is under X/Y. */
21636 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21637 if (!NILP (mouse_face)
21638 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21639 && glyph)
21640 {
21641 Lisp_Object b, e;
21642
21643 struct glyph * tmp_glyph;
21644
21645 int gpos;
21646 int gseq_length;
21647 int total_pixel_width;
21648 int ignore;
21649
21650 int vpos, hpos;
21651
21652 b = Fprevious_single_property_change (make_number (charpos + 1),
21653 Qmouse_face, string, Qnil);
21654 if (NILP (b))
21655 b = make_number (0);
21656
21657 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21658 if (NILP (e))
21659 e = make_number (SCHARS (string));
21660
21661 /* Calculate the position(glyph position: GPOS) of GLYPH in
21662 displayed string. GPOS is different from CHARPOS.
21663
21664 CHARPOS is the position of glyph in internal string
21665 object. A mode line string format has structures which
21666 is converted to a flatten by emacs lisp interpreter.
21667 The internal string is an element of the structures.
21668 The displayed string is the flatten string. */
21669 for (tmp_glyph = glyph - 1, gpos = 0;
21670 tmp_glyph->charpos >= XINT (b);
21671 tmp_glyph--, gpos++)
21672 {
21673 if (!EQ (tmp_glyph->object, glyph->object))
21674 break;
21675 }
21676
21677 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21678 displayed string holding GLYPH.
21679
21680 GSEQ_LENGTH is different from SCHARS (STRING).
21681 SCHARS (STRING) returns the length of the internal string. */
21682 for (tmp_glyph = glyph, gseq_length = gpos;
21683 tmp_glyph->charpos < XINT (e);
21684 tmp_glyph++, gseq_length++)
21685 {
21686 if (!EQ (tmp_glyph->object, glyph->object))
21687 break;
21688 }
21689
21690 total_pixel_width = 0;
21691 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21692 total_pixel_width += tmp_glyph->pixel_width;
21693
21694 /* Pre calculation of re-rendering position */
21695 vpos = (x - gpos);
21696 hpos = (area == ON_MODE_LINE
21697 ? (w->current_matrix)->nrows - 1
21698 : 0);
21699
21700 /* If the re-rendering position is included in the last
21701 re-rendering area, we should do nothing. */
21702 if ( EQ (window, dpyinfo->mouse_face_window)
21703 && dpyinfo->mouse_face_beg_col <= vpos
21704 && vpos < dpyinfo->mouse_face_end_col
21705 && dpyinfo->mouse_face_beg_row == hpos )
21706 return;
21707
21708 if (clear_mouse_face (dpyinfo))
21709 cursor = No_Cursor;
21710
21711 dpyinfo->mouse_face_beg_col = vpos;
21712 dpyinfo->mouse_face_beg_row = hpos;
21713
21714 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21715 dpyinfo->mouse_face_beg_y = 0;
21716
21717 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21718 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21719
21720 dpyinfo->mouse_face_end_x = 0;
21721 dpyinfo->mouse_face_end_y = 0;
21722
21723 dpyinfo->mouse_face_past_end = 0;
21724 dpyinfo->mouse_face_window = window;
21725
21726 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21727 charpos,
21728 0, 0, 0, &ignore,
21729 glyph->face_id, 1);
21730 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21731
21732 if (NILP (pointer))
21733 pointer = Qhand;
21734 }
21735 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21736 clear_mouse_face (dpyinfo);
21737 }
21738 define_frame_cursor1 (f, cursor, pointer);
21739 }
21740
21741
21742 /* EXPORT:
21743 Take proper action when the mouse has moved to position X, Y on
21744 frame F as regards highlighting characters that have mouse-face
21745 properties. Also de-highlighting chars where the mouse was before.
21746 X and Y can be negative or out of range. */
21747
21748 void
21749 note_mouse_highlight (f, x, y)
21750 struct frame *f;
21751 int x, y;
21752 {
21753 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21754 enum window_part part;
21755 Lisp_Object window;
21756 struct window *w;
21757 Cursor cursor = No_Cursor;
21758 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21759 struct buffer *b;
21760
21761 /* When a menu is active, don't highlight because this looks odd. */
21762 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21763 if (popup_activated ())
21764 return;
21765 #endif
21766
21767 if (NILP (Vmouse_highlight)
21768 || !f->glyphs_initialized_p)
21769 return;
21770
21771 dpyinfo->mouse_face_mouse_x = x;
21772 dpyinfo->mouse_face_mouse_y = y;
21773 dpyinfo->mouse_face_mouse_frame = f;
21774
21775 if (dpyinfo->mouse_face_defer)
21776 return;
21777
21778 if (gc_in_progress)
21779 {
21780 dpyinfo->mouse_face_deferred_gc = 1;
21781 return;
21782 }
21783
21784 /* Which window is that in? */
21785 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21786
21787 /* If we were displaying active text in another window, clear that.
21788 Also clear if we move out of text area in same window. */
21789 if (! EQ (window, dpyinfo->mouse_face_window)
21790 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21791 && !NILP (dpyinfo->mouse_face_window)))
21792 clear_mouse_face (dpyinfo);
21793
21794 /* Not on a window -> return. */
21795 if (!WINDOWP (window))
21796 return;
21797
21798 /* Reset help_echo_string. It will get recomputed below. */
21799 help_echo_string = Qnil;
21800
21801 /* Convert to window-relative pixel coordinates. */
21802 w = XWINDOW (window);
21803 frame_to_window_pixel_xy (w, &x, &y);
21804
21805 /* Handle tool-bar window differently since it doesn't display a
21806 buffer. */
21807 if (EQ (window, f->tool_bar_window))
21808 {
21809 note_tool_bar_highlight (f, x, y);
21810 return;
21811 }
21812
21813 /* Mouse is on the mode, header line or margin? */
21814 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21815 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21816 {
21817 note_mode_line_or_margin_highlight (window, x, y, part);
21818 return;
21819 }
21820
21821 if (part == ON_VERTICAL_BORDER)
21822 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21823 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21824 || part == ON_SCROLL_BAR)
21825 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21826 else
21827 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21828
21829 /* Are we in a window whose display is up to date?
21830 And verify the buffer's text has not changed. */
21831 b = XBUFFER (w->buffer);
21832 if (part == ON_TEXT
21833 && EQ (w->window_end_valid, w->buffer)
21834 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21835 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21836 {
21837 int hpos, vpos, pos, i, dx, dy, area;
21838 struct glyph *glyph;
21839 Lisp_Object object;
21840 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21841 Lisp_Object *overlay_vec = NULL;
21842 int noverlays;
21843 struct buffer *obuf;
21844 int obegv, ozv, same_region;
21845
21846 /* Find the glyph under X/Y. */
21847 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21848
21849 /* Look for :pointer property on image. */
21850 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21851 {
21852 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21853 if (img != NULL && IMAGEP (img->spec))
21854 {
21855 Lisp_Object image_map, hotspot;
21856 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21857 !NILP (image_map))
21858 && (hotspot = find_hot_spot (image_map,
21859 glyph->slice.x + dx,
21860 glyph->slice.y + dy),
21861 CONSP (hotspot))
21862 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21863 {
21864 Lisp_Object area_id, plist;
21865
21866 area_id = XCAR (hotspot);
21867 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21868 If so, we could look for mouse-enter, mouse-leave
21869 properties in PLIST (and do something...). */
21870 hotspot = XCDR (hotspot);
21871 if (CONSP (hotspot)
21872 && (plist = XCAR (hotspot), CONSP (plist)))
21873 {
21874 pointer = Fplist_get (plist, Qpointer);
21875 if (NILP (pointer))
21876 pointer = Qhand;
21877 help_echo_string = Fplist_get (plist, Qhelp_echo);
21878 if (!NILP (help_echo_string))
21879 {
21880 help_echo_window = window;
21881 help_echo_object = glyph->object;
21882 help_echo_pos = glyph->charpos;
21883 }
21884 }
21885 }
21886 if (NILP (pointer))
21887 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21888 }
21889 }
21890
21891 /* Clear mouse face if X/Y not over text. */
21892 if (glyph == NULL
21893 || area != TEXT_AREA
21894 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21895 {
21896 if (clear_mouse_face (dpyinfo))
21897 cursor = No_Cursor;
21898 if (NILP (pointer))
21899 {
21900 if (area != TEXT_AREA)
21901 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21902 else
21903 pointer = Vvoid_text_area_pointer;
21904 }
21905 goto set_cursor;
21906 }
21907
21908 pos = glyph->charpos;
21909 object = glyph->object;
21910 if (!STRINGP (object) && !BUFFERP (object))
21911 goto set_cursor;
21912
21913 /* If we get an out-of-range value, return now; avoid an error. */
21914 if (BUFFERP (object) && pos > BUF_Z (b))
21915 goto set_cursor;
21916
21917 /* Make the window's buffer temporarily current for
21918 overlays_at and compute_char_face. */
21919 obuf = current_buffer;
21920 current_buffer = b;
21921 obegv = BEGV;
21922 ozv = ZV;
21923 BEGV = BEG;
21924 ZV = Z;
21925
21926 /* Is this char mouse-active or does it have help-echo? */
21927 position = make_number (pos);
21928
21929 if (BUFFERP (object))
21930 {
21931 /* Put all the overlays we want in a vector in overlay_vec. */
21932 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21933 /* Sort overlays into increasing priority order. */
21934 noverlays = sort_overlays (overlay_vec, noverlays, w);
21935 }
21936 else
21937 noverlays = 0;
21938
21939 same_region = (EQ (window, dpyinfo->mouse_face_window)
21940 && vpos >= dpyinfo->mouse_face_beg_row
21941 && vpos <= dpyinfo->mouse_face_end_row
21942 && (vpos > dpyinfo->mouse_face_beg_row
21943 || hpos >= dpyinfo->mouse_face_beg_col)
21944 && (vpos < dpyinfo->mouse_face_end_row
21945 || hpos < dpyinfo->mouse_face_end_col
21946 || dpyinfo->mouse_face_past_end));
21947
21948 if (same_region)
21949 cursor = No_Cursor;
21950
21951 /* Check mouse-face highlighting. */
21952 if (! same_region
21953 /* If there exists an overlay with mouse-face overlapping
21954 the one we are currently highlighting, we have to
21955 check if we enter the overlapping overlay, and then
21956 highlight only that. */
21957 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21958 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21959 {
21960 /* Find the highest priority overlay that has a mouse-face
21961 property. */
21962 overlay = Qnil;
21963 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21964 {
21965 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21966 if (!NILP (mouse_face))
21967 overlay = overlay_vec[i];
21968 }
21969
21970 /* If we're actually highlighting the same overlay as
21971 before, there's no need to do that again. */
21972 if (!NILP (overlay)
21973 && EQ (overlay, dpyinfo->mouse_face_overlay))
21974 goto check_help_echo;
21975
21976 dpyinfo->mouse_face_overlay = overlay;
21977
21978 /* Clear the display of the old active region, if any. */
21979 if (clear_mouse_face (dpyinfo))
21980 cursor = No_Cursor;
21981
21982 /* If no overlay applies, get a text property. */
21983 if (NILP (overlay))
21984 mouse_face = Fget_text_property (position, Qmouse_face, object);
21985
21986 /* Handle the overlay case. */
21987 if (!NILP (overlay))
21988 {
21989 /* Find the range of text around this char that
21990 should be active. */
21991 Lisp_Object before, after;
21992 int ignore;
21993
21994 before = Foverlay_start (overlay);
21995 after = Foverlay_end (overlay);
21996 /* Record this as the current active region. */
21997 fast_find_position (w, XFASTINT (before),
21998 &dpyinfo->mouse_face_beg_col,
21999 &dpyinfo->mouse_face_beg_row,
22000 &dpyinfo->mouse_face_beg_x,
22001 &dpyinfo->mouse_face_beg_y, Qnil);
22002
22003 dpyinfo->mouse_face_past_end
22004 = !fast_find_position (w, XFASTINT (after),
22005 &dpyinfo->mouse_face_end_col,
22006 &dpyinfo->mouse_face_end_row,
22007 &dpyinfo->mouse_face_end_x,
22008 &dpyinfo->mouse_face_end_y, Qnil);
22009 dpyinfo->mouse_face_window = window;
22010
22011 dpyinfo->mouse_face_face_id
22012 = face_at_buffer_position (w, pos, 0, 0,
22013 &ignore, pos + 1,
22014 !dpyinfo->mouse_face_hidden);
22015
22016 /* Display it as active. */
22017 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22018 cursor = No_Cursor;
22019 }
22020 /* Handle the text property case. */
22021 else if (!NILP (mouse_face) && BUFFERP (object))
22022 {
22023 /* Find the range of text around this char that
22024 should be active. */
22025 Lisp_Object before, after, beginning, end;
22026 int ignore;
22027
22028 beginning = Fmarker_position (w->start);
22029 end = make_number (BUF_Z (XBUFFER (object))
22030 - XFASTINT (w->window_end_pos));
22031 before
22032 = Fprevious_single_property_change (make_number (pos + 1),
22033 Qmouse_face,
22034 object, beginning);
22035 after
22036 = Fnext_single_property_change (position, Qmouse_face,
22037 object, end);
22038
22039 /* Record this as the current active region. */
22040 fast_find_position (w, XFASTINT (before),
22041 &dpyinfo->mouse_face_beg_col,
22042 &dpyinfo->mouse_face_beg_row,
22043 &dpyinfo->mouse_face_beg_x,
22044 &dpyinfo->mouse_face_beg_y, Qnil);
22045 dpyinfo->mouse_face_past_end
22046 = !fast_find_position (w, XFASTINT (after),
22047 &dpyinfo->mouse_face_end_col,
22048 &dpyinfo->mouse_face_end_row,
22049 &dpyinfo->mouse_face_end_x,
22050 &dpyinfo->mouse_face_end_y, Qnil);
22051 dpyinfo->mouse_face_window = window;
22052
22053 if (BUFFERP (object))
22054 dpyinfo->mouse_face_face_id
22055 = face_at_buffer_position (w, pos, 0, 0,
22056 &ignore, pos + 1,
22057 !dpyinfo->mouse_face_hidden);
22058
22059 /* Display it as active. */
22060 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22061 cursor = No_Cursor;
22062 }
22063 else if (!NILP (mouse_face) && STRINGP (object))
22064 {
22065 Lisp_Object b, e;
22066 int ignore;
22067
22068 b = Fprevious_single_property_change (make_number (pos + 1),
22069 Qmouse_face,
22070 object, Qnil);
22071 e = Fnext_single_property_change (position, Qmouse_face,
22072 object, Qnil);
22073 if (NILP (b))
22074 b = make_number (0);
22075 if (NILP (e))
22076 e = make_number (SCHARS (object) - 1);
22077
22078 fast_find_string_pos (w, XINT (b), object,
22079 &dpyinfo->mouse_face_beg_col,
22080 &dpyinfo->mouse_face_beg_row,
22081 &dpyinfo->mouse_face_beg_x,
22082 &dpyinfo->mouse_face_beg_y, 0);
22083 fast_find_string_pos (w, XINT (e), object,
22084 &dpyinfo->mouse_face_end_col,
22085 &dpyinfo->mouse_face_end_row,
22086 &dpyinfo->mouse_face_end_x,
22087 &dpyinfo->mouse_face_end_y, 1);
22088 dpyinfo->mouse_face_past_end = 0;
22089 dpyinfo->mouse_face_window = window;
22090 dpyinfo->mouse_face_face_id
22091 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22092 glyph->face_id, 1);
22093 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22094 cursor = No_Cursor;
22095 }
22096 else if (STRINGP (object) && NILP (mouse_face))
22097 {
22098 /* A string which doesn't have mouse-face, but
22099 the text ``under'' it might have. */
22100 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22101 int start = MATRIX_ROW_START_CHARPOS (r);
22102
22103 pos = string_buffer_position (w, object, start);
22104 if (pos > 0)
22105 mouse_face = get_char_property_and_overlay (make_number (pos),
22106 Qmouse_face,
22107 w->buffer,
22108 &overlay);
22109 if (!NILP (mouse_face) && !NILP (overlay))
22110 {
22111 Lisp_Object before = Foverlay_start (overlay);
22112 Lisp_Object after = Foverlay_end (overlay);
22113 int ignore;
22114
22115 /* Note that we might not be able to find position
22116 BEFORE in the glyph matrix if the overlay is
22117 entirely covered by a `display' property. In
22118 this case, we overshoot. So let's stop in
22119 the glyph matrix before glyphs for OBJECT. */
22120 fast_find_position (w, XFASTINT (before),
22121 &dpyinfo->mouse_face_beg_col,
22122 &dpyinfo->mouse_face_beg_row,
22123 &dpyinfo->mouse_face_beg_x,
22124 &dpyinfo->mouse_face_beg_y,
22125 object);
22126
22127 dpyinfo->mouse_face_past_end
22128 = !fast_find_position (w, XFASTINT (after),
22129 &dpyinfo->mouse_face_end_col,
22130 &dpyinfo->mouse_face_end_row,
22131 &dpyinfo->mouse_face_end_x,
22132 &dpyinfo->mouse_face_end_y,
22133 Qnil);
22134 dpyinfo->mouse_face_window = window;
22135 dpyinfo->mouse_face_face_id
22136 = face_at_buffer_position (w, pos, 0, 0,
22137 &ignore, pos + 1,
22138 !dpyinfo->mouse_face_hidden);
22139
22140 /* Display it as active. */
22141 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22142 cursor = No_Cursor;
22143 }
22144 }
22145 }
22146
22147 check_help_echo:
22148
22149 /* Look for a `help-echo' property. */
22150 if (NILP (help_echo_string)) {
22151 Lisp_Object help, overlay;
22152
22153 /* Check overlays first. */
22154 help = overlay = Qnil;
22155 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22156 {
22157 overlay = overlay_vec[i];
22158 help = Foverlay_get (overlay, Qhelp_echo);
22159 }
22160
22161 if (!NILP (help))
22162 {
22163 help_echo_string = help;
22164 help_echo_window = window;
22165 help_echo_object = overlay;
22166 help_echo_pos = pos;
22167 }
22168 else
22169 {
22170 Lisp_Object object = glyph->object;
22171 int charpos = glyph->charpos;
22172
22173 /* Try text properties. */
22174 if (STRINGP (object)
22175 && charpos >= 0
22176 && charpos < SCHARS (object))
22177 {
22178 help = Fget_text_property (make_number (charpos),
22179 Qhelp_echo, object);
22180 if (NILP (help))
22181 {
22182 /* If the string itself doesn't specify a help-echo,
22183 see if the buffer text ``under'' it does. */
22184 struct glyph_row *r
22185 = MATRIX_ROW (w->current_matrix, vpos);
22186 int start = MATRIX_ROW_START_CHARPOS (r);
22187 int pos = string_buffer_position (w, object, start);
22188 if (pos > 0)
22189 {
22190 help = Fget_char_property (make_number (pos),
22191 Qhelp_echo, w->buffer);
22192 if (!NILP (help))
22193 {
22194 charpos = pos;
22195 object = w->buffer;
22196 }
22197 }
22198 }
22199 }
22200 else if (BUFFERP (object)
22201 && charpos >= BEGV
22202 && charpos < ZV)
22203 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22204 object);
22205
22206 if (!NILP (help))
22207 {
22208 help_echo_string = help;
22209 help_echo_window = window;
22210 help_echo_object = object;
22211 help_echo_pos = charpos;
22212 }
22213 }
22214 }
22215
22216 /* Look for a `pointer' property. */
22217 if (NILP (pointer))
22218 {
22219 /* Check overlays first. */
22220 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22221 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22222
22223 if (NILP (pointer))
22224 {
22225 Lisp_Object object = glyph->object;
22226 int charpos = glyph->charpos;
22227
22228 /* Try text properties. */
22229 if (STRINGP (object)
22230 && charpos >= 0
22231 && charpos < SCHARS (object))
22232 {
22233 pointer = Fget_text_property (make_number (charpos),
22234 Qpointer, object);
22235 if (NILP (pointer))
22236 {
22237 /* If the string itself doesn't specify a pointer,
22238 see if the buffer text ``under'' it does. */
22239 struct glyph_row *r
22240 = MATRIX_ROW (w->current_matrix, vpos);
22241 int start = MATRIX_ROW_START_CHARPOS (r);
22242 int pos = string_buffer_position (w, object, start);
22243 if (pos > 0)
22244 pointer = Fget_char_property (make_number (pos),
22245 Qpointer, w->buffer);
22246 }
22247 }
22248 else if (BUFFERP (object)
22249 && charpos >= BEGV
22250 && charpos < ZV)
22251 pointer = Fget_text_property (make_number (charpos),
22252 Qpointer, object);
22253 }
22254 }
22255
22256 BEGV = obegv;
22257 ZV = ozv;
22258 current_buffer = obuf;
22259 }
22260
22261 set_cursor:
22262
22263 define_frame_cursor1 (f, cursor, pointer);
22264 }
22265
22266
22267 /* EXPORT for RIF:
22268 Clear any mouse-face on window W. This function is part of the
22269 redisplay interface, and is called from try_window_id and similar
22270 functions to ensure the mouse-highlight is off. */
22271
22272 void
22273 x_clear_window_mouse_face (w)
22274 struct window *w;
22275 {
22276 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22277 Lisp_Object window;
22278
22279 BLOCK_INPUT;
22280 XSETWINDOW (window, w);
22281 if (EQ (window, dpyinfo->mouse_face_window))
22282 clear_mouse_face (dpyinfo);
22283 UNBLOCK_INPUT;
22284 }
22285
22286
22287 /* EXPORT:
22288 Just discard the mouse face information for frame F, if any.
22289 This is used when the size of F is changed. */
22290
22291 void
22292 cancel_mouse_face (f)
22293 struct frame *f;
22294 {
22295 Lisp_Object window;
22296 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22297
22298 window = dpyinfo->mouse_face_window;
22299 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22300 {
22301 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22302 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22303 dpyinfo->mouse_face_window = Qnil;
22304 }
22305 }
22306
22307
22308 #endif /* HAVE_WINDOW_SYSTEM */
22309
22310 \f
22311 /***********************************************************************
22312 Exposure Events
22313 ***********************************************************************/
22314
22315 #ifdef HAVE_WINDOW_SYSTEM
22316
22317 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22318 which intersects rectangle R. R is in window-relative coordinates. */
22319
22320 static void
22321 expose_area (w, row, r, area)
22322 struct window *w;
22323 struct glyph_row *row;
22324 XRectangle *r;
22325 enum glyph_row_area area;
22326 {
22327 struct glyph *first = row->glyphs[area];
22328 struct glyph *end = row->glyphs[area] + row->used[area];
22329 struct glyph *last;
22330 int first_x, start_x, x;
22331
22332 if (area == TEXT_AREA && row->fill_line_p)
22333 /* If row extends face to end of line write the whole line. */
22334 draw_glyphs (w, 0, row, area,
22335 0, row->used[area],
22336 DRAW_NORMAL_TEXT, 0);
22337 else
22338 {
22339 /* Set START_X to the window-relative start position for drawing glyphs of
22340 AREA. The first glyph of the text area can be partially visible.
22341 The first glyphs of other areas cannot. */
22342 start_x = window_box_left_offset (w, area);
22343 x = start_x;
22344 if (area == TEXT_AREA)
22345 x += row->x;
22346
22347 /* Find the first glyph that must be redrawn. */
22348 while (first < end
22349 && x + first->pixel_width < r->x)
22350 {
22351 x += first->pixel_width;
22352 ++first;
22353 }
22354
22355 /* Find the last one. */
22356 last = first;
22357 first_x = x;
22358 while (last < end
22359 && x < r->x + r->width)
22360 {
22361 x += last->pixel_width;
22362 ++last;
22363 }
22364
22365 /* Repaint. */
22366 if (last > first)
22367 draw_glyphs (w, first_x - start_x, row, area,
22368 first - row->glyphs[area], last - row->glyphs[area],
22369 DRAW_NORMAL_TEXT, 0);
22370 }
22371 }
22372
22373
22374 /* Redraw the parts of the glyph row ROW on window W intersecting
22375 rectangle R. R is in window-relative coordinates. Value is
22376 non-zero if mouse-face was overwritten. */
22377
22378 static int
22379 expose_line (w, row, r)
22380 struct window *w;
22381 struct glyph_row *row;
22382 XRectangle *r;
22383 {
22384 xassert (row->enabled_p);
22385
22386 if (row->mode_line_p || w->pseudo_window_p)
22387 draw_glyphs (w, 0, row, TEXT_AREA,
22388 0, row->used[TEXT_AREA],
22389 DRAW_NORMAL_TEXT, 0);
22390 else
22391 {
22392 if (row->used[LEFT_MARGIN_AREA])
22393 expose_area (w, row, r, LEFT_MARGIN_AREA);
22394 if (row->used[TEXT_AREA])
22395 expose_area (w, row, r, TEXT_AREA);
22396 if (row->used[RIGHT_MARGIN_AREA])
22397 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22398 draw_row_fringe_bitmaps (w, row);
22399 }
22400
22401 return row->mouse_face_p;
22402 }
22403
22404
22405 /* Redraw those parts of glyphs rows during expose event handling that
22406 overlap other rows. Redrawing of an exposed line writes over parts
22407 of lines overlapping that exposed line; this function fixes that.
22408
22409 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22410 row in W's current matrix that is exposed and overlaps other rows.
22411 LAST_OVERLAPPING_ROW is the last such row. */
22412
22413 static void
22414 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22415 struct window *w;
22416 struct glyph_row *first_overlapping_row;
22417 struct glyph_row *last_overlapping_row;
22418 {
22419 struct glyph_row *row;
22420
22421 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22422 if (row->overlapping_p)
22423 {
22424 xassert (row->enabled_p && !row->mode_line_p);
22425
22426 if (row->used[LEFT_MARGIN_AREA])
22427 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22428
22429 if (row->used[TEXT_AREA])
22430 x_fix_overlapping_area (w, row, TEXT_AREA);
22431
22432 if (row->used[RIGHT_MARGIN_AREA])
22433 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22434 }
22435 }
22436
22437
22438 /* Return non-zero if W's cursor intersects rectangle R. */
22439
22440 static int
22441 phys_cursor_in_rect_p (w, r)
22442 struct window *w;
22443 XRectangle *r;
22444 {
22445 XRectangle cr, result;
22446 struct glyph *cursor_glyph;
22447
22448 cursor_glyph = get_phys_cursor_glyph (w);
22449 if (cursor_glyph)
22450 {
22451 /* r is relative to W's box, but w->phys_cursor.x is relative
22452 to left edge of W's TEXT area. Adjust it. */
22453 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22454 cr.y = w->phys_cursor.y;
22455 cr.width = cursor_glyph->pixel_width;
22456 cr.height = w->phys_cursor_height;
22457 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22458 I assume the effect is the same -- and this is portable. */
22459 return x_intersect_rectangles (&cr, r, &result);
22460 }
22461 else
22462 return 0;
22463 }
22464
22465
22466 /* EXPORT:
22467 Draw a vertical window border to the right of window W if W doesn't
22468 have vertical scroll bars. */
22469
22470 void
22471 x_draw_vertical_border (w)
22472 struct window *w;
22473 {
22474 /* We could do better, if we knew what type of scroll-bar the adjacent
22475 windows (on either side) have... But we don't :-(
22476 However, I think this works ok. ++KFS 2003-04-25 */
22477
22478 /* Redraw borders between horizontally adjacent windows. Don't
22479 do it for frames with vertical scroll bars because either the
22480 right scroll bar of a window, or the left scroll bar of its
22481 neighbor will suffice as a border. */
22482 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22483 return;
22484
22485 if (!WINDOW_RIGHTMOST_P (w)
22486 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22487 {
22488 int x0, x1, y0, y1;
22489
22490 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22491 y1 -= 1;
22492
22493 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22494 x1 -= 1;
22495
22496 rif->draw_vertical_window_border (w, x1, y0, y1);
22497 }
22498 else if (!WINDOW_LEFTMOST_P (w)
22499 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22500 {
22501 int x0, x1, y0, y1;
22502
22503 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22504 y1 -= 1;
22505
22506 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22507 x0 -= 1;
22508
22509 rif->draw_vertical_window_border (w, x0, y0, y1);
22510 }
22511 }
22512
22513
22514 /* Redraw the part of window W intersection rectangle FR. Pixel
22515 coordinates in FR are frame-relative. Call this function with
22516 input blocked. Value is non-zero if the exposure overwrites
22517 mouse-face. */
22518
22519 static int
22520 expose_window (w, fr)
22521 struct window *w;
22522 XRectangle *fr;
22523 {
22524 struct frame *f = XFRAME (w->frame);
22525 XRectangle wr, r;
22526 int mouse_face_overwritten_p = 0;
22527
22528 /* If window is not yet fully initialized, do nothing. This can
22529 happen when toolkit scroll bars are used and a window is split.
22530 Reconfiguring the scroll bar will generate an expose for a newly
22531 created window. */
22532 if (w->current_matrix == NULL)
22533 return 0;
22534
22535 /* When we're currently updating the window, display and current
22536 matrix usually don't agree. Arrange for a thorough display
22537 later. */
22538 if (w == updated_window)
22539 {
22540 SET_FRAME_GARBAGED (f);
22541 return 0;
22542 }
22543
22544 /* Frame-relative pixel rectangle of W. */
22545 wr.x = WINDOW_LEFT_EDGE_X (w);
22546 wr.y = WINDOW_TOP_EDGE_Y (w);
22547 wr.width = WINDOW_TOTAL_WIDTH (w);
22548 wr.height = WINDOW_TOTAL_HEIGHT (w);
22549
22550 if (x_intersect_rectangles (fr, &wr, &r))
22551 {
22552 int yb = window_text_bottom_y (w);
22553 struct glyph_row *row;
22554 int cursor_cleared_p;
22555 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22556
22557 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22558 r.x, r.y, r.width, r.height));
22559
22560 /* Convert to window coordinates. */
22561 r.x -= WINDOW_LEFT_EDGE_X (w);
22562 r.y -= WINDOW_TOP_EDGE_Y (w);
22563
22564 /* Turn off the cursor. */
22565 if (!w->pseudo_window_p
22566 && phys_cursor_in_rect_p (w, &r))
22567 {
22568 x_clear_cursor (w);
22569 cursor_cleared_p = 1;
22570 }
22571 else
22572 cursor_cleared_p = 0;
22573
22574 /* Update lines intersecting rectangle R. */
22575 first_overlapping_row = last_overlapping_row = NULL;
22576 for (row = w->current_matrix->rows;
22577 row->enabled_p;
22578 ++row)
22579 {
22580 int y0 = row->y;
22581 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22582
22583 if ((y0 >= r.y && y0 < r.y + r.height)
22584 || (y1 > r.y && y1 < r.y + r.height)
22585 || (r.y >= y0 && r.y < y1)
22586 || (r.y + r.height > y0 && r.y + r.height < y1))
22587 {
22588 /* A header line may be overlapping, but there is no need
22589 to fix overlapping areas for them. KFS 2005-02-12 */
22590 if (row->overlapping_p && !row->mode_line_p)
22591 {
22592 if (first_overlapping_row == NULL)
22593 first_overlapping_row = row;
22594 last_overlapping_row = row;
22595 }
22596
22597 if (expose_line (w, row, &r))
22598 mouse_face_overwritten_p = 1;
22599 }
22600
22601 if (y1 >= yb)
22602 break;
22603 }
22604
22605 /* Display the mode line if there is one. */
22606 if (WINDOW_WANTS_MODELINE_P (w)
22607 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22608 row->enabled_p)
22609 && row->y < r.y + r.height)
22610 {
22611 if (expose_line (w, row, &r))
22612 mouse_face_overwritten_p = 1;
22613 }
22614
22615 if (!w->pseudo_window_p)
22616 {
22617 /* Fix the display of overlapping rows. */
22618 if (first_overlapping_row)
22619 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22620
22621 /* Draw border between windows. */
22622 x_draw_vertical_border (w);
22623
22624 /* Turn the cursor on again. */
22625 if (cursor_cleared_p)
22626 update_window_cursor (w, 1);
22627 }
22628 }
22629
22630 return mouse_face_overwritten_p;
22631 }
22632
22633
22634
22635 /* Redraw (parts) of all windows in the window tree rooted at W that
22636 intersect R. R contains frame pixel coordinates. Value is
22637 non-zero if the exposure overwrites mouse-face. */
22638
22639 static int
22640 expose_window_tree (w, r)
22641 struct window *w;
22642 XRectangle *r;
22643 {
22644 struct frame *f = XFRAME (w->frame);
22645 int mouse_face_overwritten_p = 0;
22646
22647 while (w && !FRAME_GARBAGED_P (f))
22648 {
22649 if (!NILP (w->hchild))
22650 mouse_face_overwritten_p
22651 |= expose_window_tree (XWINDOW (w->hchild), r);
22652 else if (!NILP (w->vchild))
22653 mouse_face_overwritten_p
22654 |= expose_window_tree (XWINDOW (w->vchild), r);
22655 else
22656 mouse_face_overwritten_p |= expose_window (w, r);
22657
22658 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22659 }
22660
22661 return mouse_face_overwritten_p;
22662 }
22663
22664
22665 /* EXPORT:
22666 Redisplay an exposed area of frame F. X and Y are the upper-left
22667 corner of the exposed rectangle. W and H are width and height of
22668 the exposed area. All are pixel values. W or H zero means redraw
22669 the entire frame. */
22670
22671 void
22672 expose_frame (f, x, y, w, h)
22673 struct frame *f;
22674 int x, y, w, h;
22675 {
22676 XRectangle r;
22677 int mouse_face_overwritten_p = 0;
22678
22679 TRACE ((stderr, "expose_frame "));
22680
22681 /* No need to redraw if frame will be redrawn soon. */
22682 if (FRAME_GARBAGED_P (f))
22683 {
22684 TRACE ((stderr, " garbaged\n"));
22685 return;
22686 }
22687
22688 /* If basic faces haven't been realized yet, there is no point in
22689 trying to redraw anything. This can happen when we get an expose
22690 event while Emacs is starting, e.g. by moving another window. */
22691 if (FRAME_FACE_CACHE (f) == NULL
22692 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22693 {
22694 TRACE ((stderr, " no faces\n"));
22695 return;
22696 }
22697
22698 if (w == 0 || h == 0)
22699 {
22700 r.x = r.y = 0;
22701 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22702 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22703 }
22704 else
22705 {
22706 r.x = x;
22707 r.y = y;
22708 r.width = w;
22709 r.height = h;
22710 }
22711
22712 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22713 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22714
22715 if (WINDOWP (f->tool_bar_window))
22716 mouse_face_overwritten_p
22717 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22718
22719 #ifdef HAVE_X_WINDOWS
22720 #ifndef MSDOS
22721 #ifndef USE_X_TOOLKIT
22722 if (WINDOWP (f->menu_bar_window))
22723 mouse_face_overwritten_p
22724 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22725 #endif /* not USE_X_TOOLKIT */
22726 #endif
22727 #endif
22728
22729 /* Some window managers support a focus-follows-mouse style with
22730 delayed raising of frames. Imagine a partially obscured frame,
22731 and moving the mouse into partially obscured mouse-face on that
22732 frame. The visible part of the mouse-face will be highlighted,
22733 then the WM raises the obscured frame. With at least one WM, KDE
22734 2.1, Emacs is not getting any event for the raising of the frame
22735 (even tried with SubstructureRedirectMask), only Expose events.
22736 These expose events will draw text normally, i.e. not
22737 highlighted. Which means we must redo the highlight here.
22738 Subsume it under ``we love X''. --gerd 2001-08-15 */
22739 /* Included in Windows version because Windows most likely does not
22740 do the right thing if any third party tool offers
22741 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22742 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22743 {
22744 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22745 if (f == dpyinfo->mouse_face_mouse_frame)
22746 {
22747 int x = dpyinfo->mouse_face_mouse_x;
22748 int y = dpyinfo->mouse_face_mouse_y;
22749 clear_mouse_face (dpyinfo);
22750 note_mouse_highlight (f, x, y);
22751 }
22752 }
22753 }
22754
22755
22756 /* EXPORT:
22757 Determine the intersection of two rectangles R1 and R2. Return
22758 the intersection in *RESULT. Value is non-zero if RESULT is not
22759 empty. */
22760
22761 int
22762 x_intersect_rectangles (r1, r2, result)
22763 XRectangle *r1, *r2, *result;
22764 {
22765 XRectangle *left, *right;
22766 XRectangle *upper, *lower;
22767 int intersection_p = 0;
22768
22769 /* Rearrange so that R1 is the left-most rectangle. */
22770 if (r1->x < r2->x)
22771 left = r1, right = r2;
22772 else
22773 left = r2, right = r1;
22774
22775 /* X0 of the intersection is right.x0, if this is inside R1,
22776 otherwise there is no intersection. */
22777 if (right->x <= left->x + left->width)
22778 {
22779 result->x = right->x;
22780
22781 /* The right end of the intersection is the minimum of the
22782 the right ends of left and right. */
22783 result->width = (min (left->x + left->width, right->x + right->width)
22784 - result->x);
22785
22786 /* Same game for Y. */
22787 if (r1->y < r2->y)
22788 upper = r1, lower = r2;
22789 else
22790 upper = r2, lower = r1;
22791
22792 /* The upper end of the intersection is lower.y0, if this is inside
22793 of upper. Otherwise, there is no intersection. */
22794 if (lower->y <= upper->y + upper->height)
22795 {
22796 result->y = lower->y;
22797
22798 /* The lower end of the intersection is the minimum of the lower
22799 ends of upper and lower. */
22800 result->height = (min (lower->y + lower->height,
22801 upper->y + upper->height)
22802 - result->y);
22803 intersection_p = 1;
22804 }
22805 }
22806
22807 return intersection_p;
22808 }
22809
22810 #endif /* HAVE_WINDOW_SYSTEM */
22811
22812 \f
22813 /***********************************************************************
22814 Initialization
22815 ***********************************************************************/
22816
22817 void
22818 syms_of_xdisp ()
22819 {
22820 Vwith_echo_area_save_vector = Qnil;
22821 staticpro (&Vwith_echo_area_save_vector);
22822
22823 Vmessage_stack = Qnil;
22824 staticpro (&Vmessage_stack);
22825
22826 Qinhibit_redisplay = intern ("inhibit-redisplay");
22827 staticpro (&Qinhibit_redisplay);
22828
22829 message_dolog_marker1 = Fmake_marker ();
22830 staticpro (&message_dolog_marker1);
22831 message_dolog_marker2 = Fmake_marker ();
22832 staticpro (&message_dolog_marker2);
22833 message_dolog_marker3 = Fmake_marker ();
22834 staticpro (&message_dolog_marker3);
22835
22836 #if GLYPH_DEBUG
22837 defsubr (&Sdump_frame_glyph_matrix);
22838 defsubr (&Sdump_glyph_matrix);
22839 defsubr (&Sdump_glyph_row);
22840 defsubr (&Sdump_tool_bar_row);
22841 defsubr (&Strace_redisplay);
22842 defsubr (&Strace_to_stderr);
22843 #endif
22844 #ifdef HAVE_WINDOW_SYSTEM
22845 defsubr (&Stool_bar_lines_needed);
22846 defsubr (&Slookup_image_map);
22847 #endif
22848 defsubr (&Sformat_mode_line);
22849
22850 staticpro (&Qmenu_bar_update_hook);
22851 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22852
22853 staticpro (&Qoverriding_terminal_local_map);
22854 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22855
22856 staticpro (&Qoverriding_local_map);
22857 Qoverriding_local_map = intern ("overriding-local-map");
22858
22859 staticpro (&Qwindow_scroll_functions);
22860 Qwindow_scroll_functions = intern ("window-scroll-functions");
22861
22862 staticpro (&Qredisplay_end_trigger_functions);
22863 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22864
22865 staticpro (&Qinhibit_point_motion_hooks);
22866 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22867
22868 QCdata = intern (":data");
22869 staticpro (&QCdata);
22870 Qdisplay = intern ("display");
22871 staticpro (&Qdisplay);
22872 Qspace_width = intern ("space-width");
22873 staticpro (&Qspace_width);
22874 Qraise = intern ("raise");
22875 staticpro (&Qraise);
22876 Qslice = intern ("slice");
22877 staticpro (&Qslice);
22878 Qspace = intern ("space");
22879 staticpro (&Qspace);
22880 Qmargin = intern ("margin");
22881 staticpro (&Qmargin);
22882 Qpointer = intern ("pointer");
22883 staticpro (&Qpointer);
22884 Qleft_margin = intern ("left-margin");
22885 staticpro (&Qleft_margin);
22886 Qright_margin = intern ("right-margin");
22887 staticpro (&Qright_margin);
22888 Qcenter = intern ("center");
22889 staticpro (&Qcenter);
22890 Qline_height = intern ("line-height");
22891 staticpro (&Qline_height);
22892 QCalign_to = intern (":align-to");
22893 staticpro (&QCalign_to);
22894 QCrelative_width = intern (":relative-width");
22895 staticpro (&QCrelative_width);
22896 QCrelative_height = intern (":relative-height");
22897 staticpro (&QCrelative_height);
22898 QCeval = intern (":eval");
22899 staticpro (&QCeval);
22900 QCpropertize = intern (":propertize");
22901 staticpro (&QCpropertize);
22902 QCfile = intern (":file");
22903 staticpro (&QCfile);
22904 Qfontified = intern ("fontified");
22905 staticpro (&Qfontified);
22906 Qfontification_functions = intern ("fontification-functions");
22907 staticpro (&Qfontification_functions);
22908 Qtrailing_whitespace = intern ("trailing-whitespace");
22909 staticpro (&Qtrailing_whitespace);
22910 Qescape_glyph = intern ("escape-glyph");
22911 staticpro (&Qescape_glyph);
22912 Qnobreak_space = intern ("nobreak-space");
22913 staticpro (&Qnobreak_space);
22914 Qimage = intern ("image");
22915 staticpro (&Qimage);
22916 QCmap = intern (":map");
22917 staticpro (&QCmap);
22918 QCpointer = intern (":pointer");
22919 staticpro (&QCpointer);
22920 Qrect = intern ("rect");
22921 staticpro (&Qrect);
22922 Qcircle = intern ("circle");
22923 staticpro (&Qcircle);
22924 Qpoly = intern ("poly");
22925 staticpro (&Qpoly);
22926 Qmessage_truncate_lines = intern ("message-truncate-lines");
22927 staticpro (&Qmessage_truncate_lines);
22928 Qgrow_only = intern ("grow-only");
22929 staticpro (&Qgrow_only);
22930 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22931 staticpro (&Qinhibit_menubar_update);
22932 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22933 staticpro (&Qinhibit_eval_during_redisplay);
22934 Qposition = intern ("position");
22935 staticpro (&Qposition);
22936 Qbuffer_position = intern ("buffer-position");
22937 staticpro (&Qbuffer_position);
22938 Qobject = intern ("object");
22939 staticpro (&Qobject);
22940 Qbar = intern ("bar");
22941 staticpro (&Qbar);
22942 Qhbar = intern ("hbar");
22943 staticpro (&Qhbar);
22944 Qbox = intern ("box");
22945 staticpro (&Qbox);
22946 Qhollow = intern ("hollow");
22947 staticpro (&Qhollow);
22948 Qhand = intern ("hand");
22949 staticpro (&Qhand);
22950 Qarrow = intern ("arrow");
22951 staticpro (&Qarrow);
22952 Qtext = intern ("text");
22953 staticpro (&Qtext);
22954 Qrisky_local_variable = intern ("risky-local-variable");
22955 staticpro (&Qrisky_local_variable);
22956 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22957 staticpro (&Qinhibit_free_realized_faces);
22958
22959 list_of_error = Fcons (Fcons (intern ("error"),
22960 Fcons (intern ("void-variable"), Qnil)),
22961 Qnil);
22962 staticpro (&list_of_error);
22963
22964 Qlast_arrow_position = intern ("last-arrow-position");
22965 staticpro (&Qlast_arrow_position);
22966 Qlast_arrow_string = intern ("last-arrow-string");
22967 staticpro (&Qlast_arrow_string);
22968
22969 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22970 staticpro (&Qoverlay_arrow_string);
22971 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22972 staticpro (&Qoverlay_arrow_bitmap);
22973
22974 echo_buffer[0] = echo_buffer[1] = Qnil;
22975 staticpro (&echo_buffer[0]);
22976 staticpro (&echo_buffer[1]);
22977
22978 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22979 staticpro (&echo_area_buffer[0]);
22980 staticpro (&echo_area_buffer[1]);
22981
22982 Vmessages_buffer_name = build_string ("*Messages*");
22983 staticpro (&Vmessages_buffer_name);
22984
22985 mode_line_proptrans_alist = Qnil;
22986 staticpro (&mode_line_proptrans_alist);
22987 mode_line_string_list = Qnil;
22988 staticpro (&mode_line_string_list);
22989 mode_line_string_face = Qnil;
22990 staticpro (&mode_line_string_face);
22991 mode_line_string_face_prop = Qnil;
22992 staticpro (&mode_line_string_face_prop);
22993 Vmode_line_unwind_vector = Qnil;
22994 staticpro (&Vmode_line_unwind_vector);
22995
22996 help_echo_string = Qnil;
22997 staticpro (&help_echo_string);
22998 help_echo_object = Qnil;
22999 staticpro (&help_echo_object);
23000 help_echo_window = Qnil;
23001 staticpro (&help_echo_window);
23002 previous_help_echo_string = Qnil;
23003 staticpro (&previous_help_echo_string);
23004 help_echo_pos = -1;
23005
23006 #ifdef HAVE_WINDOW_SYSTEM
23007 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23008 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23009 For example, if a block cursor is over a tab, it will be drawn as
23010 wide as that tab on the display. */);
23011 x_stretch_cursor_p = 0;
23012 #endif
23013
23014 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23015 doc: /* *Non-nil means highlight trailing whitespace.
23016 The face used for trailing whitespace is `trailing-whitespace'. */);
23017 Vshow_trailing_whitespace = Qnil;
23018
23019 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23020 doc: /* *Control highlighting of nobreak space and soft hyphen.
23021 A value of t means highlight the character itself (for nobreak space,
23022 use face `nobreak-space').
23023 A value of nil means no highlighting.
23024 Other values mean display the escape glyph followed by an ordinary
23025 space or ordinary hyphen. */);
23026 Vnobreak_char_display = Qt;
23027
23028 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23029 doc: /* *The pointer shape to show in void text areas.
23030 A value of nil means to show the text pointer. Other options are `arrow',
23031 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23032 Vvoid_text_area_pointer = Qarrow;
23033
23034 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23035 doc: /* Non-nil means don't actually do any redisplay.
23036 This is used for internal purposes. */);
23037 Vinhibit_redisplay = Qnil;
23038
23039 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23040 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23041 Vglobal_mode_string = Qnil;
23042
23043 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23044 doc: /* Marker for where to display an arrow on top of the buffer text.
23045 This must be the beginning of a line in order to work.
23046 See also `overlay-arrow-string'. */);
23047 Voverlay_arrow_position = Qnil;
23048
23049 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23050 doc: /* String to display as an arrow in non-window frames.
23051 See also `overlay-arrow-position'. */);
23052 Voverlay_arrow_string = build_string ("=>");
23053
23054 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23055 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23056 The symbols on this list are examined during redisplay to determine
23057 where to display overlay arrows. */);
23058 Voverlay_arrow_variable_list
23059 = Fcons (intern ("overlay-arrow-position"), Qnil);
23060
23061 DEFVAR_INT ("scroll-step", &scroll_step,
23062 doc: /* *The number of lines to try scrolling a window by when point moves out.
23063 If that fails to bring point back on frame, point is centered instead.
23064 If this is zero, point is always centered after it moves off frame.
23065 If you want scrolling to always be a line at a time, you should set
23066 `scroll-conservatively' to a large value rather than set this to 1. */);
23067
23068 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23069 doc: /* *Scroll up to this many lines, to bring point back on screen.
23070 A value of zero means to scroll the text to center point vertically
23071 in the window. */);
23072 scroll_conservatively = 0;
23073
23074 DEFVAR_INT ("scroll-margin", &scroll_margin,
23075 doc: /* *Number of lines of margin at the top and bottom of a window.
23076 Recenter the window whenever point gets within this many lines
23077 of the top or bottom of the window. */);
23078 scroll_margin = 0;
23079
23080 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23081 doc: /* Pixels per inch value for non-window system displays.
23082 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23083 Vdisplay_pixels_per_inch = make_float (72.0);
23084
23085 #if GLYPH_DEBUG
23086 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23087 #endif
23088
23089 DEFVAR_BOOL ("truncate-partial-width-windows",
23090 &truncate_partial_width_windows,
23091 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23092 truncate_partial_width_windows = 1;
23093
23094 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23095 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23096 Any other value means to use the appropriate face, `mode-line',
23097 `header-line', or `menu' respectively. */);
23098 mode_line_inverse_video = 1;
23099
23100 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23101 doc: /* *Maximum buffer size for which line number should be displayed.
23102 If the buffer is bigger than this, the line number does not appear
23103 in the mode line. A value of nil means no limit. */);
23104 Vline_number_display_limit = Qnil;
23105
23106 DEFVAR_INT ("line-number-display-limit-width",
23107 &line_number_display_limit_width,
23108 doc: /* *Maximum line width (in characters) for line number display.
23109 If the average length of the lines near point is bigger than this, then the
23110 line number may be omitted from the mode line. */);
23111 line_number_display_limit_width = 200;
23112
23113 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23114 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23115 highlight_nonselected_windows = 0;
23116
23117 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23118 doc: /* Non-nil if more than one frame is visible on this display.
23119 Minibuffer-only frames don't count, but iconified frames do.
23120 This variable is not guaranteed to be accurate except while processing
23121 `frame-title-format' and `icon-title-format'. */);
23122
23123 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23124 doc: /* Template for displaying the title bar of visible frames.
23125 \(Assuming the window manager supports this feature.)
23126 This variable has the same structure as `mode-line-format' (which see),
23127 and is used only on frames for which no explicit name has been set
23128 \(see `modify-frame-parameters'). */);
23129
23130 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23131 doc: /* Template for displaying the title bar of an iconified frame.
23132 \(Assuming the window manager supports this feature.)
23133 This variable has the same structure as `mode-line-format' (which see),
23134 and is used only on frames for which no explicit name has been set
23135 \(see `modify-frame-parameters'). */);
23136 Vicon_title_format
23137 = Vframe_title_format
23138 = Fcons (intern ("multiple-frames"),
23139 Fcons (build_string ("%b"),
23140 Fcons (Fcons (empty_string,
23141 Fcons (intern ("invocation-name"),
23142 Fcons (build_string ("@"),
23143 Fcons (intern ("system-name"),
23144 Qnil)))),
23145 Qnil)));
23146
23147 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23148 doc: /* Maximum number of lines to keep in the message log buffer.
23149 If nil, disable message logging. If t, log messages but don't truncate
23150 the buffer when it becomes large. */);
23151 Vmessage_log_max = make_number (50);
23152
23153 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23154 doc: /* Functions called before redisplay, if window sizes have changed.
23155 The value should be a list of functions that take one argument.
23156 Just before redisplay, for each frame, if any of its windows have changed
23157 size since the last redisplay, or have been split or deleted,
23158 all the functions in the list are called, with the frame as argument. */);
23159 Vwindow_size_change_functions = Qnil;
23160
23161 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23162 doc: /* List of functions to call before redisplaying a window with scrolling.
23163 Each function is called with two arguments, the window
23164 and its new display-start position. Note that the value of `window-end'
23165 is not valid when these functions are called. */);
23166 Vwindow_scroll_functions = Qnil;
23167
23168 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23169 doc: /* Functions called when redisplay of a window reaches the end trigger.
23170 Each function is called with two arguments, the window and the end trigger value.
23171 See `set-window-redisplay-end-trigger'. */);
23172 Vredisplay_end_trigger_functions = Qnil;
23173
23174 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23175 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23176 mouse_autoselect_window = 0;
23177
23178 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23179 doc: /* *Non-nil means automatically resize tool-bars.
23180 This increases a tool-bar's height if not all tool-bar items are visible.
23181 It decreases a tool-bar's height when it would display blank lines
23182 otherwise. */);
23183 auto_resize_tool_bars_p = 1;
23184
23185 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23186 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23187 auto_raise_tool_bar_buttons_p = 1;
23188
23189 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23190 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23191 make_cursor_line_fully_visible_p = 1;
23192
23193 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23194 doc: /* *Margin around tool-bar buttons in pixels.
23195 If an integer, use that for both horizontal and vertical margins.
23196 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23197 HORZ specifying the horizontal margin, and VERT specifying the
23198 vertical margin. */);
23199 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23200
23201 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23202 doc: /* *Relief thickness of tool-bar buttons. */);
23203 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23204
23205 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23206 doc: /* List of functions to call to fontify regions of text.
23207 Each function is called with one argument POS. Functions must
23208 fontify a region starting at POS in the current buffer, and give
23209 fontified regions the property `fontified'. */);
23210 Vfontification_functions = Qnil;
23211 Fmake_variable_buffer_local (Qfontification_functions);
23212
23213 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23214 &unibyte_display_via_language_environment,
23215 doc: /* *Non-nil means display unibyte text according to language environment.
23216 Specifically this means that unibyte non-ASCII characters
23217 are displayed by converting them to the equivalent multibyte characters
23218 according to the current language environment. As a result, they are
23219 displayed according to the current fontset. */);
23220 unibyte_display_via_language_environment = 0;
23221
23222 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23223 doc: /* *Maximum height for resizing mini-windows.
23224 If a float, it specifies a fraction of the mini-window frame's height.
23225 If an integer, it specifies a number of lines. */);
23226 Vmax_mini_window_height = make_float (0.25);
23227
23228 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23229 doc: /* *How to resize mini-windows.
23230 A value of nil means don't automatically resize mini-windows.
23231 A value of t means resize them to fit the text displayed in them.
23232 A value of `grow-only', the default, means let mini-windows grow
23233 only, until their display becomes empty, at which point the windows
23234 go back to their normal size. */);
23235 Vresize_mini_windows = Qgrow_only;
23236
23237 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23238 doc: /* Alist specifying how to blink the cursor off.
23239 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23240 `cursor-type' frame-parameter or variable equals ON-STATE,
23241 comparing using `equal', Emacs uses OFF-STATE to specify
23242 how to blink it off. */);
23243 Vblink_cursor_alist = Qnil;
23244
23245 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23246 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23247 automatic_hscrolling_p = 1;
23248
23249 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23250 doc: /* *How many columns away from the window edge point is allowed to get
23251 before automatic hscrolling will horizontally scroll the window. */);
23252 hscroll_margin = 5;
23253
23254 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23255 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23256 When point is less than `automatic-hscroll-margin' columns from the window
23257 edge, automatic hscrolling will scroll the window by the amount of columns
23258 determined by this variable. If its value is a positive integer, scroll that
23259 many columns. If it's a positive floating-point number, it specifies the
23260 fraction of the window's width to scroll. If it's nil or zero, point will be
23261 centered horizontally after the scroll. Any other value, including negative
23262 numbers, are treated as if the value were zero.
23263
23264 Automatic hscrolling always moves point outside the scroll margin, so if
23265 point was more than scroll step columns inside the margin, the window will
23266 scroll more than the value given by the scroll step.
23267
23268 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23269 and `scroll-right' overrides this variable's effect. */);
23270 Vhscroll_step = make_number (0);
23271
23272 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23273 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23274 Bind this around calls to `message' to let it take effect. */);
23275 message_truncate_lines = 0;
23276
23277 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23278 doc: /* Normal hook run to update the menu bar definitions.
23279 Redisplay runs this hook before it redisplays the menu bar.
23280 This is used to update submenus such as Buffers,
23281 whose contents depend on various data. */);
23282 Vmenu_bar_update_hook = Qnil;
23283
23284 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23285 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23286 inhibit_menubar_update = 0;
23287
23288 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23289 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23290 inhibit_eval_during_redisplay = 0;
23291
23292 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23293 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23294 inhibit_free_realized_faces = 0;
23295
23296 #if GLYPH_DEBUG
23297 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23298 doc: /* Inhibit try_window_id display optimization. */);
23299 inhibit_try_window_id = 0;
23300
23301 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23302 doc: /* Inhibit try_window_reusing display optimization. */);
23303 inhibit_try_window_reusing = 0;
23304
23305 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23306 doc: /* Inhibit try_cursor_movement display optimization. */);
23307 inhibit_try_cursor_movement = 0;
23308 #endif /* GLYPH_DEBUG */
23309 }
23310
23311
23312 /* Initialize this module when Emacs starts. */
23313
23314 void
23315 init_xdisp ()
23316 {
23317 Lisp_Object root_window;
23318 struct window *mini_w;
23319
23320 current_header_line_height = current_mode_line_height = -1;
23321
23322 CHARPOS (this_line_start_pos) = 0;
23323
23324 mini_w = XWINDOW (minibuf_window);
23325 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23326
23327 if (!noninteractive)
23328 {
23329 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23330 int i;
23331
23332 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23333 set_window_height (root_window,
23334 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23335 0);
23336 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23337 set_window_height (minibuf_window, 1, 0);
23338
23339 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23340 mini_w->total_cols = make_number (FRAME_COLS (f));
23341
23342 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23343 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23344 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23345
23346 /* The default ellipsis glyphs `...'. */
23347 for (i = 0; i < 3; ++i)
23348 default_invis_vector[i] = make_number ('.');
23349 }
23350
23351 {
23352 /* Allocate the buffer for frame titles.
23353 Also used for `format-mode-line'. */
23354 int size = 100;
23355 mode_line_noprop_buf = (char *) xmalloc (size);
23356 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23357 mode_line_noprop_ptr = mode_line_noprop_buf;
23358 mode_line_target = MODE_LINE_DISPLAY;
23359 }
23360
23361 help_echo_showing_p = 0;
23362 }
23363
23364
23365 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23366 (do not change this comment) */