Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-68
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "character.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;
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 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
740
741 /* Properties handled by iterators. */
742
743 static struct props it_props[] =
744 {
745 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
746 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
747 /* Handle `face' before `display' because some sub-properties of
748 `display' need to know the face. */
749 {&Qface, FACE_PROP_IDX, handle_face_prop},
750 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
751 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
752 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
753 {NULL, 0, NULL}
754 };
755
756 /* Value is the position described by X. If X is a marker, value is
757 the marker_position of X. Otherwise, value is X. */
758
759 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
760
761 /* Enumeration returned by some move_it_.* functions internally. */
762
763 enum move_it_result
764 {
765 /* Not used. Undefined value. */
766 MOVE_UNDEFINED,
767
768 /* Move ended at the requested buffer position or ZV. */
769 MOVE_POS_MATCH_OR_ZV,
770
771 /* Move ended at the requested X pixel position. */
772 MOVE_X_REACHED,
773
774 /* Move within a line ended at the end of a line that must be
775 continued. */
776 MOVE_LINE_CONTINUED,
777
778 /* Move within a line ended at the end of a line that would
779 be displayed truncated. */
780 MOVE_LINE_TRUNCATED,
781
782 /* Move within a line ended at a line end. */
783 MOVE_NEWLINE_OR_CR
784 };
785
786 /* This counter is used to clear the face cache every once in a while
787 in redisplay_internal. It is incremented for each redisplay.
788 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
789 cleared. */
790
791 #define CLEAR_FACE_CACHE_COUNT 500
792 static int clear_face_cache_count;
793
794 /* Similarly for the image cache. */
795
796 #ifdef HAVE_WINDOW_SYSTEM
797 #define CLEAR_IMAGE_CACHE_COUNT 101
798 static int clear_image_cache_count;
799 #endif
800
801 /* Record the previous terminal frame we displayed. */
802
803 static struct frame *previous_terminal_frame;
804
805 /* Non-zero while redisplay_internal is in progress. */
806
807 int redisplaying_p;
808
809 /* Non-zero means don't free realized faces. Bound while freeing
810 realized faces is dangerous because glyph matrices might still
811 reference them. */
812
813 int inhibit_free_realized_faces;
814 Lisp_Object Qinhibit_free_realized_faces;
815
816 /* If a string, XTread_socket generates an event to display that string.
817 (The display is done in read_char.) */
818
819 Lisp_Object help_echo_string;
820 Lisp_Object help_echo_window;
821 Lisp_Object help_echo_object;
822 int help_echo_pos;
823
824 /* Temporary variable for XTread_socket. */
825
826 Lisp_Object previous_help_echo_string;
827
828 /* Null glyph slice */
829
830 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
831
832 \f
833 /* Function prototypes. */
834
835 static void setup_for_ellipsis P_ ((struct it *, int));
836 static void mark_window_display_accurate_1 P_ ((struct window *, int));
837 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
838 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
839 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
840 static int redisplay_mode_lines P_ ((Lisp_Object, int));
841 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
842
843 #if 0
844 static int invisible_text_between_p P_ ((struct it *, int, int));
845 #endif
846
847 static void pint2str P_ ((char *, int, int));
848 static void pint2hrstr P_ ((char *, int, int));
849 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
850 struct text_pos));
851 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
852 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
853 static void store_mode_line_noprop_char P_ ((char));
854 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
855 static void x_consider_frame_title P_ ((Lisp_Object));
856 static void handle_stop P_ ((struct it *));
857 static int tool_bar_lines_needed P_ ((struct frame *));
858 static int single_display_spec_intangible_p P_ ((Lisp_Object));
859 static void ensure_echo_area_buffers P_ ((void));
860 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
861 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
862 static int with_echo_area_buffer P_ ((struct window *, int,
863 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
864 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static void clear_garbaged_frames P_ ((void));
866 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
868 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static int display_echo_area P_ ((struct window *));
870 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
872 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
873 static int string_char_and_length P_ ((const unsigned char *, int, int *));
874 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
875 struct text_pos));
876 static int compute_window_start_on_continuation_line P_ ((struct window *));
877 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
878 static void insert_left_trunc_glyphs P_ ((struct it *));
879 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
880 Lisp_Object));
881 static void extend_face_to_end_of_line P_ ((struct it *));
882 static int append_space_for_newline P_ ((struct it *, int));
883 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
884 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
885 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
886 static int trailing_whitespace_p P_ ((int));
887 static int message_log_check_duplicate P_ ((int, int, int, int));
888 static void push_it P_ ((struct it *));
889 static void pop_it P_ ((struct it *));
890 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
891 static void select_frame_for_redisplay P_ ((Lisp_Object));
892 static void redisplay_internal P_ ((int));
893 static int echo_area_display P_ ((int));
894 static void redisplay_windows P_ ((Lisp_Object));
895 static void redisplay_window P_ ((Lisp_Object, int));
896 static Lisp_Object redisplay_window_error ();
897 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
898 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
899 static void update_menu_bar P_ ((struct frame *, int));
900 static int try_window_reusing_current_matrix P_ ((struct window *));
901 static int try_window_id P_ ((struct window *));
902 static int display_line P_ ((struct it *));
903 static int display_mode_lines P_ ((struct window *));
904 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
905 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
906 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
907 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
908 static void display_menu_bar P_ ((struct window *));
909 static int display_count_lines P_ ((int, int, int, int, int *));
910 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
911 int, int, struct it *, int, int, int, int));
912 static void compute_line_metrics P_ ((struct it *));
913 static void run_redisplay_end_trigger_hook P_ ((struct it *));
914 static int get_overlay_strings P_ ((struct it *, int));
915 static void next_overlay_string P_ ((struct it *));
916 static void reseat P_ ((struct it *, struct text_pos, int));
917 static void reseat_1 P_ ((struct it *, struct text_pos, int));
918 static void back_to_previous_visible_line_start P_ ((struct it *));
919 void reseat_at_previous_visible_line_start P_ ((struct it *));
920 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
921 static int next_element_from_ellipsis P_ ((struct it *));
922 static int next_element_from_display_vector P_ ((struct it *));
923 static int next_element_from_string P_ ((struct it *));
924 static int next_element_from_c_string P_ ((struct it *));
925 static int next_element_from_buffer P_ ((struct it *));
926 static int next_element_from_composition P_ ((struct it *));
927 static int next_element_from_image P_ ((struct it *));
928 static int next_element_from_stretch P_ ((struct it *));
929 static void load_overlay_strings P_ ((struct it *, int));
930 static int init_from_display_pos P_ ((struct it *, struct window *,
931 struct display_pos *));
932 static void reseat_to_string P_ ((struct it *, unsigned char *,
933 Lisp_Object, int, int, int, int));
934 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
935 int, int, int));
936 void move_it_vertically_backward P_ ((struct it *, int));
937 static void init_to_row_start P_ ((struct it *, struct window *,
938 struct glyph_row *));
939 static int init_to_row_end P_ ((struct it *, struct window *,
940 struct glyph_row *));
941 static void back_to_previous_line_start P_ ((struct it *));
942 static int forward_to_next_line_start P_ ((struct it *, int *));
943 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
944 Lisp_Object, int));
945 static struct text_pos string_pos P_ ((int, Lisp_Object));
946 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
947 static int number_of_chars P_ ((unsigned char *, int));
948 static void compute_stop_pos P_ ((struct it *));
949 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
950 Lisp_Object));
951 static int face_before_or_after_it_pos P_ ((struct it *, int));
952 static int next_overlay_change P_ ((int));
953 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
954 Lisp_Object, struct text_pos *,
955 int));
956 static int underlying_face_id P_ ((struct it *));
957 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
958 struct window *));
959
960 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
961 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
962
963 #ifdef HAVE_WINDOW_SYSTEM
964
965 static void update_tool_bar P_ ((struct frame *, int));
966 static void build_desired_tool_bar_string P_ ((struct frame *f));
967 static int redisplay_tool_bar P_ ((struct frame *));
968 static void display_tool_bar_line P_ ((struct it *));
969 static void notice_overwritten_cursor P_ ((struct window *,
970 enum glyph_row_area,
971 int, int, int, int));
972
973
974
975 #endif /* HAVE_WINDOW_SYSTEM */
976
977 \f
978 /***********************************************************************
979 Window display dimensions
980 ***********************************************************************/
981
982 /* Return the bottom boundary y-position for text lines in window W.
983 This is the first y position at which a line cannot start.
984 It is relative to the top of the window.
985
986 This is the height of W minus the height of a mode line, if any. */
987
988 INLINE int
989 window_text_bottom_y (w)
990 struct window *w;
991 {
992 int height = WINDOW_TOTAL_HEIGHT (w);
993
994 if (WINDOW_WANTS_MODELINE_P (w))
995 height -= CURRENT_MODE_LINE_HEIGHT (w);
996 return height;
997 }
998
999 /* Return the pixel width of display area AREA of window W. AREA < 0
1000 means return the total width of W, not including fringes to
1001 the left and right of the window. */
1002
1003 INLINE int
1004 window_box_width (w, area)
1005 struct window *w;
1006 int area;
1007 {
1008 int cols = XFASTINT (w->total_cols);
1009 int pixels = 0;
1010
1011 if (!w->pseudo_window_p)
1012 {
1013 cols -= WINDOW_SCROLL_BAR_COLS (w);
1014
1015 if (area == TEXT_AREA)
1016 {
1017 if (INTEGERP (w->left_margin_cols))
1018 cols -= XFASTINT (w->left_margin_cols);
1019 if (INTEGERP (w->right_margin_cols))
1020 cols -= XFASTINT (w->right_margin_cols);
1021 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1022 }
1023 else if (area == LEFT_MARGIN_AREA)
1024 {
1025 cols = (INTEGERP (w->left_margin_cols)
1026 ? XFASTINT (w->left_margin_cols) : 0);
1027 pixels = 0;
1028 }
1029 else if (area == RIGHT_MARGIN_AREA)
1030 {
1031 cols = (INTEGERP (w->right_margin_cols)
1032 ? XFASTINT (w->right_margin_cols) : 0);
1033 pixels = 0;
1034 }
1035 }
1036
1037 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1038 }
1039
1040
1041 /* Return the pixel height of the display area of window W, not
1042 including mode lines of W, if any. */
1043
1044 INLINE int
1045 window_box_height (w)
1046 struct window *w;
1047 {
1048 struct frame *f = XFRAME (w->frame);
1049 int height = WINDOW_TOTAL_HEIGHT (w);
1050
1051 xassert (height >= 0);
1052
1053 /* Note: the code below that determines the mode-line/header-line
1054 height is essentially the same as that contained in the macro
1055 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1056 the appropriate glyph row has its `mode_line_p' flag set,
1057 and if it doesn't, uses estimate_mode_line_height instead. */
1058
1059 if (WINDOW_WANTS_MODELINE_P (w))
1060 {
1061 struct glyph_row *ml_row
1062 = (w->current_matrix && w->current_matrix->rows
1063 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1064 : 0);
1065 if (ml_row && ml_row->mode_line_p)
1066 height -= ml_row->height;
1067 else
1068 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1069 }
1070
1071 if (WINDOW_WANTS_HEADER_LINE_P (w))
1072 {
1073 struct glyph_row *hl_row
1074 = (w->current_matrix && w->current_matrix->rows
1075 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1076 : 0);
1077 if (hl_row && hl_row->mode_line_p)
1078 height -= hl_row->height;
1079 else
1080 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1081 }
1082
1083 /* With a very small font and a mode-line that's taller than
1084 default, we might end up with a negative height. */
1085 return max (0, height);
1086 }
1087
1088 /* Return the window-relative coordinate of the left edge of display
1089 area AREA of window W. AREA < 0 means return the left edge of the
1090 whole window, to the right of the left fringe of W. */
1091
1092 INLINE int
1093 window_box_left_offset (w, area)
1094 struct window *w;
1095 int area;
1096 {
1097 int x;
1098
1099 if (w->pseudo_window_p)
1100 return 0;
1101
1102 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1103
1104 if (area == TEXT_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA));
1107 else if (area == RIGHT_MARGIN_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA)
1110 + window_box_width (w, TEXT_AREA)
1111 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1112 ? 0
1113 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1114 else if (area == LEFT_MARGIN_AREA
1115 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1116 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1117
1118 return x;
1119 }
1120
1121
1122 /* Return the window-relative coordinate of the right edge of display
1123 area AREA of window W. AREA < 0 means return the left edge of the
1124 whole window, to the left of the right fringe of W. */
1125
1126 INLINE int
1127 window_box_right_offset (w, area)
1128 struct window *w;
1129 int area;
1130 {
1131 return window_box_left_offset (w, area) + window_box_width (w, area);
1132 }
1133
1134 /* Return the frame-relative coordinate of the left edge of display
1135 area AREA of window W. AREA < 0 means return the left edge of the
1136 whole window, to the right of the left fringe of W. */
1137
1138 INLINE int
1139 window_box_left (w, area)
1140 struct window *w;
1141 int area;
1142 {
1143 struct frame *f = XFRAME (w->frame);
1144 int x;
1145
1146 if (w->pseudo_window_p)
1147 return FRAME_INTERNAL_BORDER_WIDTH (f);
1148
1149 x = (WINDOW_LEFT_EDGE_X (w)
1150 + window_box_left_offset (w, area));
1151
1152 return x;
1153 }
1154
1155
1156 /* Return the frame-relative coordinate of the right edge of display
1157 area AREA of window W. AREA < 0 means return the left edge of the
1158 whole window, to the left of the right fringe of W. */
1159
1160 INLINE int
1161 window_box_right (w, area)
1162 struct window *w;
1163 int area;
1164 {
1165 return window_box_left (w, area) + window_box_width (w, area);
1166 }
1167
1168 /* Get the bounding box of the display area AREA of window W, without
1169 mode lines, in frame-relative coordinates. AREA < 0 means the
1170 whole window, not including the left and right fringes of
1171 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1172 coordinates of the upper-left corner of the box. Return in
1173 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1174
1175 INLINE void
1176 window_box (w, area, box_x, box_y, box_width, box_height)
1177 struct window *w;
1178 int area;
1179 int *box_x, *box_y, *box_width, *box_height;
1180 {
1181 if (box_width)
1182 *box_width = window_box_width (w, area);
1183 if (box_height)
1184 *box_height = window_box_height (w);
1185 if (box_x)
1186 *box_x = window_box_left (w, area);
1187 if (box_y)
1188 {
1189 *box_y = WINDOW_TOP_EDGE_Y (w);
1190 if (WINDOW_WANTS_HEADER_LINE_P (w))
1191 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1192 }
1193 }
1194
1195
1196 /* Get the bounding box of the display area AREA of window W, without
1197 mode lines. AREA < 0 means the whole window, not including the
1198 left and right fringe of the window. Return in *TOP_LEFT_X
1199 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1200 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1201 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1202 box. */
1203
1204 INLINE void
1205 window_box_edges (w, area, top_left_x, top_left_y,
1206 bottom_right_x, bottom_right_y)
1207 struct window *w;
1208 int area;
1209 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1210 {
1211 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1212 bottom_right_y);
1213 *bottom_right_x += *top_left_x;
1214 *bottom_right_y += *top_left_y;
1215 }
1216
1217
1218 \f
1219 /***********************************************************************
1220 Utilities
1221 ***********************************************************************/
1222
1223 /* Return the bottom y-position of the line the iterator IT is in.
1224 This can modify IT's settings. */
1225
1226 int
1227 line_bottom_y (it)
1228 struct it *it;
1229 {
1230 int line_height = it->max_ascent + it->max_descent;
1231 int line_top_y = it->current_y;
1232
1233 if (line_height == 0)
1234 {
1235 if (last_height)
1236 line_height = last_height;
1237 else if (IT_CHARPOS (*it) < ZV)
1238 {
1239 move_it_by_lines (it, 1, 1);
1240 line_height = (it->max_ascent || it->max_descent
1241 ? it->max_ascent + it->max_descent
1242 : last_height);
1243 }
1244 else
1245 {
1246 struct glyph_row *row = it->glyph_row;
1247
1248 /* Use the default character height. */
1249 it->glyph_row = NULL;
1250 it->what = IT_CHARACTER;
1251 it->c = ' ';
1252 it->len = 1;
1253 PRODUCE_GLYPHS (it);
1254 line_height = it->ascent + it->descent;
1255 it->glyph_row = row;
1256 }
1257 }
1258
1259 return line_top_y + line_height;
1260 }
1261
1262
1263 /* Return 1 if position CHARPOS is visible in window W.
1264 If visible, set *X and *Y to pixel coordinates of top left corner.
1265 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1266 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1267 and header-lines heights. */
1268
1269 int
1270 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1271 struct window *w;
1272 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1273 {
1274 struct it it;
1275 struct text_pos top;
1276 int visible_p = 0;
1277 struct buffer *old_buffer = NULL;
1278
1279 if (noninteractive)
1280 return visible_p;
1281
1282 if (XBUFFER (w->buffer) != current_buffer)
1283 {
1284 old_buffer = current_buffer;
1285 set_buffer_internal_1 (XBUFFER (w->buffer));
1286 }
1287
1288 SET_TEXT_POS_FROM_MARKER (top, w->start);
1289
1290 /* Compute exact mode line heights, if requested. */
1291 if (exact_mode_line_heights_p)
1292 {
1293 if (WINDOW_WANTS_MODELINE_P (w))
1294 current_mode_line_height
1295 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1296 current_buffer->mode_line_format);
1297
1298 if (WINDOW_WANTS_HEADER_LINE_P (w))
1299 current_header_line_height
1300 = display_mode_line (w, HEADER_LINE_FACE_ID,
1301 current_buffer->header_line_format);
1302 }
1303
1304 start_display (&it, w, top);
1305 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1306 MOVE_TO_POS | MOVE_TO_Y);
1307
1308 /* Note that we may overshoot because of invisible text. */
1309 if (IT_CHARPOS (it) >= charpos)
1310 {
1311 int top_x = it.current_x;
1312 int top_y = it.current_y;
1313 int bottom_y = (last_height = 0, line_bottom_y (&it));
1314 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1315
1316 if (top_y < window_top_y)
1317 visible_p = bottom_y > window_top_y;
1318 else if (top_y < it.last_visible_y)
1319 visible_p = 1;
1320 if (visible_p)
1321 {
1322 *x = top_x;
1323 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1324 *rtop = max (0, window_top_y - top_y);
1325 *rbot = max (0, bottom_y - it.last_visible_y);
1326 }
1327 }
1328 else
1329 {
1330 struct it it2;
1331
1332 it2 = it;
1333 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1334 move_it_by_lines (&it, 1, 0);
1335 if (charpos < IT_CHARPOS (it))
1336 {
1337 visible_p = 1;
1338 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1339 *x = it2.current_x;
1340 *y = it2.current_y + it2.max_ascent - it2.ascent;
1341 *rtop = max (0, -it2.current_y);
1342 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1343 - it.last_visible_y));
1344 }
1345 }
1346
1347 if (old_buffer)
1348 set_buffer_internal_1 (old_buffer);
1349
1350 current_header_line_height = current_mode_line_height = -1;
1351
1352 return visible_p;
1353 }
1354
1355
1356 /* Return the next character from STR which is MAXLEN bytes long.
1357 Return in *LEN the length of the character. This is like
1358 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1359 we find one, we return a `?', but with the length of the invalid
1360 character. */
1361
1362 static INLINE int
1363 string_char_and_length (str, maxlen, len)
1364 const unsigned char *str;
1365 int maxlen, *len;
1366 {
1367 int c;
1368
1369 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1370 if (!CHAR_VALID_P (c, 1))
1371 /* We may not change the length here because other places in Emacs
1372 don't use this function, i.e. they silently accept invalid
1373 characters. */
1374 c = '?';
1375
1376 return c;
1377 }
1378
1379
1380
1381 /* Given a position POS containing a valid character and byte position
1382 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1383
1384 static struct text_pos
1385 string_pos_nchars_ahead (pos, string, nchars)
1386 struct text_pos pos;
1387 Lisp_Object string;
1388 int nchars;
1389 {
1390 xassert (STRINGP (string) && nchars >= 0);
1391
1392 if (STRING_MULTIBYTE (string))
1393 {
1394 int rest = SBYTES (string) - BYTEPOS (pos);
1395 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1396 int len;
1397
1398 while (nchars--)
1399 {
1400 string_char_and_length (p, rest, &len);
1401 p += len, rest -= len;
1402 xassert (rest >= 0);
1403 CHARPOS (pos) += 1;
1404 BYTEPOS (pos) += len;
1405 }
1406 }
1407 else
1408 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1409
1410 return pos;
1411 }
1412
1413
1414 /* Value is the text position, i.e. character and byte position,
1415 for character position CHARPOS in STRING. */
1416
1417 static INLINE struct text_pos
1418 string_pos (charpos, string)
1419 int charpos;
1420 Lisp_Object string;
1421 {
1422 struct text_pos pos;
1423 xassert (STRINGP (string));
1424 xassert (charpos >= 0);
1425 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1426 return pos;
1427 }
1428
1429
1430 /* Value is a text position, i.e. character and byte position, for
1431 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1432 means recognize multibyte characters. */
1433
1434 static struct text_pos
1435 c_string_pos (charpos, s, multibyte_p)
1436 int charpos;
1437 unsigned char *s;
1438 int multibyte_p;
1439 {
1440 struct text_pos pos;
1441
1442 xassert (s != NULL);
1443 xassert (charpos >= 0);
1444
1445 if (multibyte_p)
1446 {
1447 int rest = strlen (s), len;
1448
1449 SET_TEXT_POS (pos, 0, 0);
1450 while (charpos--)
1451 {
1452 string_char_and_length (s, rest, &len);
1453 s += len, rest -= len;
1454 xassert (rest >= 0);
1455 CHARPOS (pos) += 1;
1456 BYTEPOS (pos) += len;
1457 }
1458 }
1459 else
1460 SET_TEXT_POS (pos, charpos, charpos);
1461
1462 return pos;
1463 }
1464
1465
1466 /* Value is the number of characters in C string S. MULTIBYTE_P
1467 non-zero means recognize multibyte characters. */
1468
1469 static int
1470 number_of_chars (s, multibyte_p)
1471 unsigned char *s;
1472 int multibyte_p;
1473 {
1474 int nchars;
1475
1476 if (multibyte_p)
1477 {
1478 int rest = strlen (s), len;
1479 unsigned char *p = (unsigned char *) s;
1480
1481 for (nchars = 0; rest > 0; ++nchars)
1482 {
1483 string_char_and_length (p, rest, &len);
1484 rest -= len, p += len;
1485 }
1486 }
1487 else
1488 nchars = strlen (s);
1489
1490 return nchars;
1491 }
1492
1493
1494 /* Compute byte position NEWPOS->bytepos corresponding to
1495 NEWPOS->charpos. POS is a known position in string STRING.
1496 NEWPOS->charpos must be >= POS.charpos. */
1497
1498 static void
1499 compute_string_pos (newpos, pos, string)
1500 struct text_pos *newpos, pos;
1501 Lisp_Object string;
1502 {
1503 xassert (STRINGP (string));
1504 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1505
1506 if (STRING_MULTIBYTE (string))
1507 *newpos = string_pos_nchars_ahead (pos, string,
1508 CHARPOS (*newpos) - CHARPOS (pos));
1509 else
1510 BYTEPOS (*newpos) = CHARPOS (*newpos);
1511 }
1512
1513 /* EXPORT:
1514 Return an estimation of the pixel height of mode or top lines on
1515 frame F. FACE_ID specifies what line's height to estimate. */
1516
1517 int
1518 estimate_mode_line_height (f, face_id)
1519 struct frame *f;
1520 enum face_id face_id;
1521 {
1522 #ifdef HAVE_WINDOW_SYSTEM
1523 if (FRAME_WINDOW_P (f))
1524 {
1525 int height = FONT_HEIGHT (FRAME_FONT (f));
1526
1527 /* This function is called so early when Emacs starts that the face
1528 cache and mode line face are not yet initialized. */
1529 if (FRAME_FACE_CACHE (f))
1530 {
1531 struct face *face = FACE_FROM_ID (f, face_id);
1532 if (face)
1533 {
1534 if (face->font)
1535 height = FONT_HEIGHT (face->font);
1536 if (face->box_line_width > 0)
1537 height += 2 * face->box_line_width;
1538 }
1539 }
1540
1541 return height;
1542 }
1543 #endif
1544
1545 return 1;
1546 }
1547
1548 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1549 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1550 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1551 not force the value into range. */
1552
1553 void
1554 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1555 FRAME_PTR f;
1556 register int pix_x, pix_y;
1557 int *x, *y;
1558 NativeRectangle *bounds;
1559 int noclip;
1560 {
1561
1562 #ifdef HAVE_WINDOW_SYSTEM
1563 if (FRAME_WINDOW_P (f))
1564 {
1565 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1566 even for negative values. */
1567 if (pix_x < 0)
1568 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1569 if (pix_y < 0)
1570 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1571
1572 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1573 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1574
1575 if (bounds)
1576 STORE_NATIVE_RECT (*bounds,
1577 FRAME_COL_TO_PIXEL_X (f, pix_x),
1578 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1579 FRAME_COLUMN_WIDTH (f) - 1,
1580 FRAME_LINE_HEIGHT (f) - 1);
1581
1582 if (!noclip)
1583 {
1584 if (pix_x < 0)
1585 pix_x = 0;
1586 else if (pix_x > FRAME_TOTAL_COLS (f))
1587 pix_x = FRAME_TOTAL_COLS (f);
1588
1589 if (pix_y < 0)
1590 pix_y = 0;
1591 else if (pix_y > FRAME_LINES (f))
1592 pix_y = FRAME_LINES (f);
1593 }
1594 }
1595 #endif
1596
1597 *x = pix_x;
1598 *y = pix_y;
1599 }
1600
1601
1602 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1603 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1604 can't tell the positions because W's display is not up to date,
1605 return 0. */
1606
1607 int
1608 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1609 struct window *w;
1610 int hpos, vpos;
1611 int *frame_x, *frame_y;
1612 {
1613 #ifdef HAVE_WINDOW_SYSTEM
1614 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1615 {
1616 int success_p;
1617
1618 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1619 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1620
1621 if (display_completed)
1622 {
1623 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1624 struct glyph *glyph = row->glyphs[TEXT_AREA];
1625 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1626
1627 hpos = row->x;
1628 vpos = row->y;
1629 while (glyph < end)
1630 {
1631 hpos += glyph->pixel_width;
1632 ++glyph;
1633 }
1634
1635 /* If first glyph is partially visible, its first visible position is still 0. */
1636 if (hpos < 0)
1637 hpos = 0;
1638
1639 success_p = 1;
1640 }
1641 else
1642 {
1643 hpos = vpos = 0;
1644 success_p = 0;
1645 }
1646
1647 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1648 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1649 return success_p;
1650 }
1651 #endif
1652
1653 *frame_x = hpos;
1654 *frame_y = vpos;
1655 return 1;
1656 }
1657
1658
1659 #ifdef HAVE_WINDOW_SYSTEM
1660
1661 /* Find the glyph under window-relative coordinates X/Y in window W.
1662 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1663 strings. Return in *HPOS and *VPOS the row and column number of
1664 the glyph found. Return in *AREA the glyph area containing X.
1665 Value is a pointer to the glyph found or null if X/Y is not on
1666 text, or we can't tell because W's current matrix is not up to
1667 date. */
1668
1669 static struct glyph *
1670 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1671 struct window *w;
1672 int x, y;
1673 int *hpos, *vpos, *dx, *dy, *area;
1674 {
1675 struct glyph *glyph, *end;
1676 struct glyph_row *row = NULL;
1677 int x0, i;
1678
1679 /* Find row containing Y. Give up if some row is not enabled. */
1680 for (i = 0; i < w->current_matrix->nrows; ++i)
1681 {
1682 row = MATRIX_ROW (w->current_matrix, i);
1683 if (!row->enabled_p)
1684 return NULL;
1685 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1686 break;
1687 }
1688
1689 *vpos = i;
1690 *hpos = 0;
1691
1692 /* Give up if Y is not in the window. */
1693 if (i == w->current_matrix->nrows)
1694 return NULL;
1695
1696 /* Get the glyph area containing X. */
1697 if (w->pseudo_window_p)
1698 {
1699 *area = TEXT_AREA;
1700 x0 = 0;
1701 }
1702 else
1703 {
1704 if (x < window_box_left_offset (w, TEXT_AREA))
1705 {
1706 *area = LEFT_MARGIN_AREA;
1707 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1708 }
1709 else if (x < window_box_right_offset (w, TEXT_AREA))
1710 {
1711 *area = TEXT_AREA;
1712 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1713 }
1714 else
1715 {
1716 *area = RIGHT_MARGIN_AREA;
1717 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1718 }
1719 }
1720
1721 /* Find glyph containing X. */
1722 glyph = row->glyphs[*area];
1723 end = glyph + row->used[*area];
1724 x -= x0;
1725 while (glyph < end && x >= glyph->pixel_width)
1726 {
1727 x -= glyph->pixel_width;
1728 ++glyph;
1729 }
1730
1731 if (glyph == end)
1732 return NULL;
1733
1734 if (dx)
1735 {
1736 *dx = x;
1737 *dy = y - (row->y + row->ascent - glyph->ascent);
1738 }
1739
1740 *hpos = glyph - row->glyphs[*area];
1741 return glyph;
1742 }
1743
1744
1745 /* EXPORT:
1746 Convert frame-relative x/y to coordinates relative to window W.
1747 Takes pseudo-windows into account. */
1748
1749 void
1750 frame_to_window_pixel_xy (w, x, y)
1751 struct window *w;
1752 int *x, *y;
1753 {
1754 if (w->pseudo_window_p)
1755 {
1756 /* A pseudo-window is always full-width, and starts at the
1757 left edge of the frame, plus a frame border. */
1758 struct frame *f = XFRAME (w->frame);
1759 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1760 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1761 }
1762 else
1763 {
1764 *x -= WINDOW_LEFT_EDGE_X (w);
1765 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1766 }
1767 }
1768
1769 /* EXPORT:
1770 Return in *R the clipping rectangle for glyph string S. */
1771
1772 void
1773 get_glyph_string_clip_rect (s, nr)
1774 struct glyph_string *s;
1775 NativeRectangle *nr;
1776 {
1777 XRectangle r;
1778
1779 if (s->row->full_width_p)
1780 {
1781 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1782 r.x = WINDOW_LEFT_EDGE_X (s->w);
1783 r.width = WINDOW_TOTAL_WIDTH (s->w);
1784
1785 /* Unless displaying a mode or menu bar line, which are always
1786 fully visible, clip to the visible part of the row. */
1787 if (s->w->pseudo_window_p)
1788 r.height = s->row->visible_height;
1789 else
1790 r.height = s->height;
1791 }
1792 else
1793 {
1794 /* This is a text line that may be partially visible. */
1795 r.x = window_box_left (s->w, s->area);
1796 r.width = window_box_width (s->w, s->area);
1797 r.height = s->row->visible_height;
1798 }
1799
1800 if (s->clip_head)
1801 if (r.x < s->clip_head->x)
1802 {
1803 if (r.width >= s->clip_head->x - r.x)
1804 r.width -= s->clip_head->x - r.x;
1805 else
1806 r.width = 0;
1807 r.x = s->clip_head->x;
1808 }
1809 if (s->clip_tail)
1810 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1811 {
1812 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1813 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1814 else
1815 r.width = 0;
1816 }
1817
1818 /* If S draws overlapping rows, it's sufficient to use the top and
1819 bottom of the window for clipping because this glyph string
1820 intentionally draws over other lines. */
1821 if (s->for_overlaps_p)
1822 {
1823 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1824 r.height = window_text_bottom_y (s->w) - r.y;
1825 }
1826 else
1827 {
1828 /* Don't use S->y for clipping because it doesn't take partially
1829 visible lines into account. For example, it can be negative for
1830 partially visible lines at the top of a window. */
1831 if (!s->row->full_width_p
1832 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1833 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1834 else
1835 r.y = max (0, s->row->y);
1836
1837 /* If drawing a tool-bar window, draw it over the internal border
1838 at the top of the window. */
1839 if (WINDOWP (s->f->tool_bar_window)
1840 && s->w == XWINDOW (s->f->tool_bar_window))
1841 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1842 }
1843
1844 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1845
1846 /* If drawing the cursor, don't let glyph draw outside its
1847 advertised boundaries. Cleartype does this under some circumstances. */
1848 if (s->hl == DRAW_CURSOR)
1849 {
1850 struct glyph *glyph = s->first_glyph;
1851 int height, max_y;
1852
1853 if (s->x > r.x)
1854 {
1855 r.width -= s->x - r.x;
1856 r.x = s->x;
1857 }
1858 r.width = min (r.width, glyph->pixel_width);
1859
1860 /* If r.y is below window bottom, ensure that we still see a cursor. */
1861 height = min (glyph->ascent + glyph->descent,
1862 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1863 max_y = window_text_bottom_y (s->w) - height;
1864 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1865 if (s->ybase - glyph->ascent > max_y)
1866 {
1867 r.y = max_y;
1868 r.height = height;
1869 }
1870 else
1871 {
1872 /* Don't draw cursor glyph taller than our actual glyph. */
1873 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1874 if (height < r.height)
1875 {
1876 max_y = r.y + r.height;
1877 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1878 r.height = min (max_y - r.y, height);
1879 }
1880 }
1881 }
1882
1883 #ifdef CONVERT_FROM_XRECT
1884 CONVERT_FROM_XRECT (r, *nr);
1885 #else
1886 *nr = r;
1887 #endif
1888 }
1889
1890
1891 /* EXPORT:
1892 Return the position and height of the phys cursor in window W.
1893 Set w->phys_cursor_width to width of phys cursor.
1894 */
1895
1896 int
1897 get_phys_cursor_geometry (w, row, glyph, heightp)
1898 struct window *w;
1899 struct glyph_row *row;
1900 struct glyph *glyph;
1901 int *heightp;
1902 {
1903 struct frame *f = XFRAME (WINDOW_FRAME (w));
1904 int y, wd, h, h0, y0;
1905
1906 /* Compute the width of the rectangle to draw. If on a stretch
1907 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1908 rectangle as wide as the glyph, but use a canonical character
1909 width instead. */
1910 wd = glyph->pixel_width - 1;
1911 #ifdef HAVE_NTGUI
1912 wd++; /* Why? */
1913 #endif
1914 if (glyph->type == STRETCH_GLYPH
1915 && !x_stretch_cursor_p)
1916 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1917 w->phys_cursor_width = wd;
1918
1919 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1920
1921 /* If y is below window bottom, ensure that we still see a cursor. */
1922 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1923
1924 h = max (h0, glyph->ascent + glyph->descent);
1925 h0 = min (h0, glyph->ascent + glyph->descent);
1926
1927 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1928 if (y < y0)
1929 {
1930 h = max (h - (y0 - y) + 1, h0);
1931 y = y0 - 1;
1932 }
1933 else
1934 {
1935 y0 = window_text_bottom_y (w) - h0;
1936 if (y > y0)
1937 {
1938 h += y - y0;
1939 y = y0;
1940 }
1941 }
1942
1943 *heightp = h - 1;
1944 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1945 }
1946
1947
1948 #endif /* HAVE_WINDOW_SYSTEM */
1949
1950 \f
1951 /***********************************************************************
1952 Lisp form evaluation
1953 ***********************************************************************/
1954
1955 /* Error handler for safe_eval and safe_call. */
1956
1957 static Lisp_Object
1958 safe_eval_handler (arg)
1959 Lisp_Object arg;
1960 {
1961 add_to_log ("Error during redisplay: %s", arg, Qnil);
1962 return Qnil;
1963 }
1964
1965
1966 /* Evaluate SEXPR and return the result, or nil if something went
1967 wrong. Prevent redisplay during the evaluation. */
1968
1969 Lisp_Object
1970 safe_eval (sexpr)
1971 Lisp_Object sexpr;
1972 {
1973 Lisp_Object val;
1974
1975 if (inhibit_eval_during_redisplay)
1976 val = Qnil;
1977 else
1978 {
1979 int count = SPECPDL_INDEX ();
1980 struct gcpro gcpro1;
1981
1982 GCPRO1 (sexpr);
1983 specbind (Qinhibit_redisplay, Qt);
1984 /* Use Qt to ensure debugger does not run,
1985 so there is no possibility of wanting to redisplay. */
1986 val = internal_condition_case_1 (Feval, sexpr, Qt,
1987 safe_eval_handler);
1988 UNGCPRO;
1989 val = unbind_to (count, val);
1990 }
1991
1992 return val;
1993 }
1994
1995
1996 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1997 Return the result, or nil if something went wrong. Prevent
1998 redisplay during the evaluation. */
1999
2000 Lisp_Object
2001 safe_call (nargs, args)
2002 int nargs;
2003 Lisp_Object *args;
2004 {
2005 Lisp_Object val;
2006
2007 if (inhibit_eval_during_redisplay)
2008 val = Qnil;
2009 else
2010 {
2011 int count = SPECPDL_INDEX ();
2012 struct gcpro gcpro1;
2013
2014 GCPRO1 (args[0]);
2015 gcpro1.nvars = nargs;
2016 specbind (Qinhibit_redisplay, Qt);
2017 /* Use Qt to ensure debugger does not run,
2018 so there is no possibility of wanting to redisplay. */
2019 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2020 safe_eval_handler);
2021 UNGCPRO;
2022 val = unbind_to (count, val);
2023 }
2024
2025 return val;
2026 }
2027
2028
2029 /* Call function FN with one argument ARG.
2030 Return the result, or nil if something went wrong. */
2031
2032 Lisp_Object
2033 safe_call1 (fn, arg)
2034 Lisp_Object fn, arg;
2035 {
2036 Lisp_Object args[2];
2037 args[0] = fn;
2038 args[1] = arg;
2039 return safe_call (2, args);
2040 }
2041
2042
2043 \f
2044 /***********************************************************************
2045 Debugging
2046 ***********************************************************************/
2047
2048 #if 0
2049
2050 /* Define CHECK_IT to perform sanity checks on iterators.
2051 This is for debugging. It is too slow to do unconditionally. */
2052
2053 static void
2054 check_it (it)
2055 struct it *it;
2056 {
2057 if (it->method == GET_FROM_STRING)
2058 {
2059 xassert (STRINGP (it->string));
2060 xassert (IT_STRING_CHARPOS (*it) >= 0);
2061 }
2062 else
2063 {
2064 xassert (IT_STRING_CHARPOS (*it) < 0);
2065 if (it->method == GET_FROM_BUFFER)
2066 {
2067 /* Check that character and byte positions agree. */
2068 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2069 }
2070 }
2071
2072 if (it->dpvec)
2073 xassert (it->current.dpvec_index >= 0);
2074 else
2075 xassert (it->current.dpvec_index < 0);
2076 }
2077
2078 #define CHECK_IT(IT) check_it ((IT))
2079
2080 #else /* not 0 */
2081
2082 #define CHECK_IT(IT) (void) 0
2083
2084 #endif /* not 0 */
2085
2086
2087 #if GLYPH_DEBUG
2088
2089 /* Check that the window end of window W is what we expect it
2090 to be---the last row in the current matrix displaying text. */
2091
2092 static void
2093 check_window_end (w)
2094 struct window *w;
2095 {
2096 if (!MINI_WINDOW_P (w)
2097 && !NILP (w->window_end_valid))
2098 {
2099 struct glyph_row *row;
2100 xassert ((row = MATRIX_ROW (w->current_matrix,
2101 XFASTINT (w->window_end_vpos)),
2102 !row->enabled_p
2103 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2104 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2105 }
2106 }
2107
2108 #define CHECK_WINDOW_END(W) check_window_end ((W))
2109
2110 #else /* not GLYPH_DEBUG */
2111
2112 #define CHECK_WINDOW_END(W) (void) 0
2113
2114 #endif /* not GLYPH_DEBUG */
2115
2116
2117 \f
2118 /***********************************************************************
2119 Iterator initialization
2120 ***********************************************************************/
2121
2122 /* Initialize IT for displaying current_buffer in window W, starting
2123 at character position CHARPOS. CHARPOS < 0 means that no buffer
2124 position is specified which is useful when the iterator is assigned
2125 a position later. BYTEPOS is the byte position corresponding to
2126 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2127
2128 If ROW is not null, calls to produce_glyphs with IT as parameter
2129 will produce glyphs in that row.
2130
2131 BASE_FACE_ID is the id of a base face to use. It must be one of
2132 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2133 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2134 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2135
2136 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2137 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2138 will be initialized to use the corresponding mode line glyph row of
2139 the desired matrix of W. */
2140
2141 void
2142 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2143 struct it *it;
2144 struct window *w;
2145 int charpos, bytepos;
2146 struct glyph_row *row;
2147 enum face_id base_face_id;
2148 {
2149 int highlight_region_p;
2150
2151 /* Some precondition checks. */
2152 xassert (w != NULL && it != NULL);
2153 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2154 && charpos <= ZV));
2155
2156 /* If face attributes have been changed since the last redisplay,
2157 free realized faces now because they depend on face definitions
2158 that might have changed. Don't free faces while there might be
2159 desired matrices pending which reference these faces. */
2160 if (face_change_count && !inhibit_free_realized_faces)
2161 {
2162 face_change_count = 0;
2163 free_all_realized_faces (Qnil);
2164 }
2165
2166 /* Use one of the mode line rows of W's desired matrix if
2167 appropriate. */
2168 if (row == NULL)
2169 {
2170 if (base_face_id == MODE_LINE_FACE_ID
2171 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2172 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2173 else if (base_face_id == HEADER_LINE_FACE_ID)
2174 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2175 }
2176
2177 /* Clear IT. */
2178 bzero (it, sizeof *it);
2179 it->current.overlay_string_index = -1;
2180 it->current.dpvec_index = -1;
2181 it->base_face_id = base_face_id;
2182 it->string = Qnil;
2183 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2184
2185 /* The window in which we iterate over current_buffer: */
2186 XSETWINDOW (it->window, w);
2187 it->w = w;
2188 it->f = XFRAME (w->frame);
2189
2190 /* Extra space between lines (on window systems only). */
2191 if (base_face_id == DEFAULT_FACE_ID
2192 && FRAME_WINDOW_P (it->f))
2193 {
2194 if (NATNUMP (current_buffer->extra_line_spacing))
2195 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2196 else if (FLOATP (current_buffer->extra_line_spacing))
2197 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2198 * FRAME_LINE_HEIGHT (it->f));
2199 else if (it->f->extra_line_spacing > 0)
2200 it->extra_line_spacing = it->f->extra_line_spacing;
2201 it->max_extra_line_spacing = 0;
2202 }
2203
2204 /* If realized faces have been removed, e.g. because of face
2205 attribute changes of named faces, recompute them. When running
2206 in batch mode, the face cache of Vterminal_frame is null. If
2207 we happen to get called, make a dummy face cache. */
2208 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2209 init_frame_faces (it->f);
2210 if (FRAME_FACE_CACHE (it->f)->used == 0)
2211 recompute_basic_faces (it->f);
2212
2213 /* Current value of the `slice', `space-width', and 'height' properties. */
2214 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2215 it->space_width = Qnil;
2216 it->font_height = Qnil;
2217 it->override_ascent = -1;
2218
2219 /* Are control characters displayed as `^C'? */
2220 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2221
2222 /* -1 means everything between a CR and the following line end
2223 is invisible. >0 means lines indented more than this value are
2224 invisible. */
2225 it->selective = (INTEGERP (current_buffer->selective_display)
2226 ? XFASTINT (current_buffer->selective_display)
2227 : (!NILP (current_buffer->selective_display)
2228 ? -1 : 0));
2229 it->selective_display_ellipsis_p
2230 = !NILP (current_buffer->selective_display_ellipses);
2231
2232 /* Display table to use. */
2233 it->dp = window_display_table (w);
2234
2235 /* Are multibyte characters enabled in current_buffer? */
2236 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2237
2238 /* Non-zero if we should highlight the region. */
2239 highlight_region_p
2240 = (!NILP (Vtransient_mark_mode)
2241 && !NILP (current_buffer->mark_active)
2242 && XMARKER (current_buffer->mark)->buffer != 0);
2243
2244 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2245 start and end of a visible region in window IT->w. Set both to
2246 -1 to indicate no region. */
2247 if (highlight_region_p
2248 /* Maybe highlight only in selected window. */
2249 && (/* Either show region everywhere. */
2250 highlight_nonselected_windows
2251 /* Or show region in the selected window. */
2252 || w == XWINDOW (selected_window)
2253 /* Or show the region if we are in the mini-buffer and W is
2254 the window the mini-buffer refers to. */
2255 || (MINI_WINDOW_P (XWINDOW (selected_window))
2256 && WINDOWP (minibuf_selected_window)
2257 && w == XWINDOW (minibuf_selected_window))))
2258 {
2259 int charpos = marker_position (current_buffer->mark);
2260 it->region_beg_charpos = min (PT, charpos);
2261 it->region_end_charpos = max (PT, charpos);
2262 }
2263 else
2264 it->region_beg_charpos = it->region_end_charpos = -1;
2265
2266 /* Get the position at which the redisplay_end_trigger hook should
2267 be run, if it is to be run at all. */
2268 if (MARKERP (w->redisplay_end_trigger)
2269 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2270 it->redisplay_end_trigger_charpos
2271 = marker_position (w->redisplay_end_trigger);
2272 else if (INTEGERP (w->redisplay_end_trigger))
2273 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2274
2275 /* Correct bogus values of tab_width. */
2276 it->tab_width = XINT (current_buffer->tab_width);
2277 if (it->tab_width <= 0 || it->tab_width > 1000)
2278 it->tab_width = 8;
2279
2280 /* Are lines in the display truncated? */
2281 it->truncate_lines_p
2282 = (base_face_id != DEFAULT_FACE_ID
2283 || XINT (it->w->hscroll)
2284 || (truncate_partial_width_windows
2285 && !WINDOW_FULL_WIDTH_P (it->w))
2286 || !NILP (current_buffer->truncate_lines));
2287
2288 /* Get dimensions of truncation and continuation glyphs. These are
2289 displayed as fringe bitmaps under X, so we don't need them for such
2290 frames. */
2291 if (!FRAME_WINDOW_P (it->f))
2292 {
2293 if (it->truncate_lines_p)
2294 {
2295 /* We will need the truncation glyph. */
2296 xassert (it->glyph_row == NULL);
2297 produce_special_glyphs (it, IT_TRUNCATION);
2298 it->truncation_pixel_width = it->pixel_width;
2299 }
2300 else
2301 {
2302 /* We will need the continuation glyph. */
2303 xassert (it->glyph_row == NULL);
2304 produce_special_glyphs (it, IT_CONTINUATION);
2305 it->continuation_pixel_width = it->pixel_width;
2306 }
2307
2308 /* Reset these values to zero because the produce_special_glyphs
2309 above has changed them. */
2310 it->pixel_width = it->ascent = it->descent = 0;
2311 it->phys_ascent = it->phys_descent = 0;
2312 }
2313
2314 /* Set this after getting the dimensions of truncation and
2315 continuation glyphs, so that we don't produce glyphs when calling
2316 produce_special_glyphs, above. */
2317 it->glyph_row = row;
2318 it->area = TEXT_AREA;
2319
2320 /* Get the dimensions of the display area. The display area
2321 consists of the visible window area plus a horizontally scrolled
2322 part to the left of the window. All x-values are relative to the
2323 start of this total display area. */
2324 if (base_face_id != DEFAULT_FACE_ID)
2325 {
2326 /* Mode lines, menu bar in terminal frames. */
2327 it->first_visible_x = 0;
2328 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2329 }
2330 else
2331 {
2332 it->first_visible_x
2333 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2334 it->last_visible_x = (it->first_visible_x
2335 + window_box_width (w, TEXT_AREA));
2336
2337 /* If we truncate lines, leave room for the truncator glyph(s) at
2338 the right margin. Otherwise, leave room for the continuation
2339 glyph(s). Truncation and continuation glyphs are not inserted
2340 for window-based redisplay. */
2341 if (!FRAME_WINDOW_P (it->f))
2342 {
2343 if (it->truncate_lines_p)
2344 it->last_visible_x -= it->truncation_pixel_width;
2345 else
2346 it->last_visible_x -= it->continuation_pixel_width;
2347 }
2348
2349 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2350 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2351 }
2352
2353 /* Leave room for a border glyph. */
2354 if (!FRAME_WINDOW_P (it->f)
2355 && !WINDOW_RIGHTMOST_P (it->w))
2356 it->last_visible_x -= 1;
2357
2358 it->last_visible_y = window_text_bottom_y (w);
2359
2360 /* For mode lines and alike, arrange for the first glyph having a
2361 left box line if the face specifies a box. */
2362 if (base_face_id != DEFAULT_FACE_ID)
2363 {
2364 struct face *face;
2365
2366 it->face_id = base_face_id;
2367
2368 /* If we have a boxed mode line, make the first character appear
2369 with a left box line. */
2370 face = FACE_FROM_ID (it->f, base_face_id);
2371 if (face->box != FACE_NO_BOX)
2372 it->start_of_box_run_p = 1;
2373 }
2374
2375 /* If a buffer position was specified, set the iterator there,
2376 getting overlays and face properties from that position. */
2377 if (charpos >= BUF_BEG (current_buffer))
2378 {
2379 it->end_charpos = ZV;
2380 it->face_id = -1;
2381 IT_CHARPOS (*it) = charpos;
2382
2383 /* Compute byte position if not specified. */
2384 if (bytepos < charpos)
2385 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2386 else
2387 IT_BYTEPOS (*it) = bytepos;
2388
2389 it->start = it->current;
2390
2391 /* Compute faces etc. */
2392 reseat (it, it->current.pos, 1);
2393 }
2394
2395 CHECK_IT (it);
2396 }
2397
2398
2399 /* Initialize IT for the display of window W with window start POS. */
2400
2401 void
2402 start_display (it, w, pos)
2403 struct it *it;
2404 struct window *w;
2405 struct text_pos pos;
2406 {
2407 struct glyph_row *row;
2408 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2409
2410 row = w->desired_matrix->rows + first_vpos;
2411 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2412 it->first_vpos = first_vpos;
2413
2414 if (!it->truncate_lines_p)
2415 {
2416 int start_at_line_beg_p;
2417 int first_y = it->current_y;
2418
2419 /* If window start is not at a line start, skip forward to POS to
2420 get the correct continuation lines width. */
2421 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2422 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2423 if (!start_at_line_beg_p)
2424 {
2425 int new_x;
2426
2427 reseat_at_previous_visible_line_start (it);
2428 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2429
2430 new_x = it->current_x + it->pixel_width;
2431
2432 /* If lines are continued, this line may end in the middle
2433 of a multi-glyph character (e.g. a control character
2434 displayed as \003, or in the middle of an overlay
2435 string). In this case move_it_to above will not have
2436 taken us to the start of the continuation line but to the
2437 end of the continued line. */
2438 if (it->current_x > 0
2439 && !it->truncate_lines_p /* Lines are continued. */
2440 && (/* And glyph doesn't fit on the line. */
2441 new_x > it->last_visible_x
2442 /* Or it fits exactly and we're on a window
2443 system frame. */
2444 || (new_x == it->last_visible_x
2445 && FRAME_WINDOW_P (it->f))))
2446 {
2447 if (it->current.dpvec_index >= 0
2448 || it->current.overlay_string_index >= 0)
2449 {
2450 set_iterator_to_next (it, 1);
2451 move_it_in_display_line_to (it, -1, -1, 0);
2452 }
2453
2454 it->continuation_lines_width += it->current_x;
2455 }
2456
2457 /* We're starting a new display line, not affected by the
2458 height of the continued line, so clear the appropriate
2459 fields in the iterator structure. */
2460 it->max_ascent = it->max_descent = 0;
2461 it->max_phys_ascent = it->max_phys_descent = 0;
2462
2463 it->current_y = first_y;
2464 it->vpos = 0;
2465 it->current_x = it->hpos = 0;
2466 }
2467 }
2468
2469 #if 0 /* Don't assert the following because start_display is sometimes
2470 called intentionally with a window start that is not at a
2471 line start. Please leave this code in as a comment. */
2472
2473 /* Window start should be on a line start, now. */
2474 xassert (it->continuation_lines_width
2475 || IT_CHARPOS (it) == BEGV
2476 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2477 #endif /* 0 */
2478 }
2479
2480
2481 /* Return 1 if POS is a position in ellipses displayed for invisible
2482 text. W is the window we display, for text property lookup. */
2483
2484 static int
2485 in_ellipses_for_invisible_text_p (pos, w)
2486 struct display_pos *pos;
2487 struct window *w;
2488 {
2489 Lisp_Object prop, window;
2490 int ellipses_p = 0;
2491 int charpos = CHARPOS (pos->pos);
2492
2493 /* If POS specifies a position in a display vector, this might
2494 be for an ellipsis displayed for invisible text. We won't
2495 get the iterator set up for delivering that ellipsis unless
2496 we make sure that it gets aware of the invisible text. */
2497 if (pos->dpvec_index >= 0
2498 && pos->overlay_string_index < 0
2499 && CHARPOS (pos->string_pos) < 0
2500 && charpos > BEGV
2501 && (XSETWINDOW (window, w),
2502 prop = Fget_char_property (make_number (charpos),
2503 Qinvisible, window),
2504 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2505 {
2506 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2507 window);
2508 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2509 }
2510
2511 return ellipses_p;
2512 }
2513
2514
2515 /* Initialize IT for stepping through current_buffer in window W,
2516 starting at position POS that includes overlay string and display
2517 vector/ control character translation position information. Value
2518 is zero if there are overlay strings with newlines at POS. */
2519
2520 static int
2521 init_from_display_pos (it, w, pos)
2522 struct it *it;
2523 struct window *w;
2524 struct display_pos *pos;
2525 {
2526 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2527 int i, overlay_strings_with_newlines = 0;
2528
2529 /* If POS specifies a position in a display vector, this might
2530 be for an ellipsis displayed for invisible text. We won't
2531 get the iterator set up for delivering that ellipsis unless
2532 we make sure that it gets aware of the invisible text. */
2533 if (in_ellipses_for_invisible_text_p (pos, w))
2534 {
2535 --charpos;
2536 bytepos = 0;
2537 }
2538
2539 /* Keep in mind: the call to reseat in init_iterator skips invisible
2540 text, so we might end up at a position different from POS. This
2541 is only a problem when POS is a row start after a newline and an
2542 overlay starts there with an after-string, and the overlay has an
2543 invisible property. Since we don't skip invisible text in
2544 display_line and elsewhere immediately after consuming the
2545 newline before the row start, such a POS will not be in a string,
2546 but the call to init_iterator below will move us to the
2547 after-string. */
2548 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2549
2550 /* This only scans the current chunk -- it should scan all chunks.
2551 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2552 to 16 in 22.1 to make this a lesser problem. */
2553 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2554 {
2555 const char *s = SDATA (it->overlay_strings[i]);
2556 const char *e = s + SBYTES (it->overlay_strings[i]);
2557
2558 while (s < e && *s != '\n')
2559 ++s;
2560
2561 if (s < e)
2562 {
2563 overlay_strings_with_newlines = 1;
2564 break;
2565 }
2566 }
2567
2568 /* If position is within an overlay string, set up IT to the right
2569 overlay string. */
2570 if (pos->overlay_string_index >= 0)
2571 {
2572 int relative_index;
2573
2574 /* If the first overlay string happens to have a `display'
2575 property for an image, the iterator will be set up for that
2576 image, and we have to undo that setup first before we can
2577 correct the overlay string index. */
2578 if (it->method == GET_FROM_IMAGE)
2579 pop_it (it);
2580
2581 /* We already have the first chunk of overlay strings in
2582 IT->overlay_strings. Load more until the one for
2583 pos->overlay_string_index is in IT->overlay_strings. */
2584 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2585 {
2586 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2587 it->current.overlay_string_index = 0;
2588 while (n--)
2589 {
2590 load_overlay_strings (it, 0);
2591 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2592 }
2593 }
2594
2595 it->current.overlay_string_index = pos->overlay_string_index;
2596 relative_index = (it->current.overlay_string_index
2597 % OVERLAY_STRING_CHUNK_SIZE);
2598 it->string = it->overlay_strings[relative_index];
2599 xassert (STRINGP (it->string));
2600 it->current.string_pos = pos->string_pos;
2601 it->method = GET_FROM_STRING;
2602 }
2603
2604 #if 0 /* This is bogus because POS not having an overlay string
2605 position does not mean it's after the string. Example: A
2606 line starting with a before-string and initialization of IT
2607 to the previous row's end position. */
2608 else if (it->current.overlay_string_index >= 0)
2609 {
2610 /* If POS says we're already after an overlay string ending at
2611 POS, make sure to pop the iterator because it will be in
2612 front of that overlay string. When POS is ZV, we've thereby
2613 also ``processed'' overlay strings at ZV. */
2614 while (it->sp)
2615 pop_it (it);
2616 it->current.overlay_string_index = -1;
2617 it->method = GET_FROM_BUFFER;
2618 if (CHARPOS (pos->pos) == ZV)
2619 it->overlay_strings_at_end_processed_p = 1;
2620 }
2621 #endif /* 0 */
2622
2623 if (CHARPOS (pos->string_pos) >= 0)
2624 {
2625 /* Recorded position is not in an overlay string, but in another
2626 string. This can only be a string from a `display' property.
2627 IT should already be filled with that string. */
2628 it->current.string_pos = pos->string_pos;
2629 xassert (STRINGP (it->string));
2630 }
2631
2632 /* Restore position in display vector translations, control
2633 character translations or ellipses. */
2634 if (pos->dpvec_index >= 0)
2635 {
2636 if (it->dpvec == NULL)
2637 get_next_display_element (it);
2638 xassert (it->dpvec && it->current.dpvec_index == 0);
2639 it->current.dpvec_index = pos->dpvec_index;
2640 }
2641
2642 CHECK_IT (it);
2643 return !overlay_strings_with_newlines;
2644 }
2645
2646
2647 /* Initialize IT for stepping through current_buffer in window W
2648 starting at ROW->start. */
2649
2650 static void
2651 init_to_row_start (it, w, row)
2652 struct it *it;
2653 struct window *w;
2654 struct glyph_row *row;
2655 {
2656 init_from_display_pos (it, w, &row->start);
2657 it->start = row->start;
2658 it->continuation_lines_width = row->continuation_lines_width;
2659 CHECK_IT (it);
2660 }
2661
2662
2663 /* Initialize IT for stepping through current_buffer in window W
2664 starting in the line following ROW, i.e. starting at ROW->end.
2665 Value is zero if there are overlay strings with newlines at ROW's
2666 end position. */
2667
2668 static int
2669 init_to_row_end (it, w, row)
2670 struct it *it;
2671 struct window *w;
2672 struct glyph_row *row;
2673 {
2674 int success = 0;
2675
2676 if (init_from_display_pos (it, w, &row->end))
2677 {
2678 if (row->continued_p)
2679 it->continuation_lines_width
2680 = row->continuation_lines_width + row->pixel_width;
2681 CHECK_IT (it);
2682 success = 1;
2683 }
2684
2685 return success;
2686 }
2687
2688
2689
2690 \f
2691 /***********************************************************************
2692 Text properties
2693 ***********************************************************************/
2694
2695 /* Called when IT reaches IT->stop_charpos. Handle text property and
2696 overlay changes. Set IT->stop_charpos to the next position where
2697 to stop. */
2698
2699 static void
2700 handle_stop (it)
2701 struct it *it;
2702 {
2703 enum prop_handled handled;
2704 int handle_overlay_change_p = 1;
2705 struct props *p;
2706
2707 it->dpvec = NULL;
2708 it->current.dpvec_index = -1;
2709
2710 /* Use face of preceding text for ellipsis (if invisible) */
2711 if (it->selective_display_ellipsis_p)
2712 it->saved_face_id = it->face_id;
2713
2714 do
2715 {
2716 handled = HANDLED_NORMALLY;
2717
2718 /* Call text property handlers. */
2719 for (p = it_props; p->handler; ++p)
2720 {
2721 handled = p->handler (it);
2722
2723 if (handled == HANDLED_RECOMPUTE_PROPS)
2724 break;
2725 else if (handled == HANDLED_RETURN)
2726 return;
2727 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2728 handle_overlay_change_p = 0;
2729 }
2730
2731 if (handled != HANDLED_RECOMPUTE_PROPS)
2732 {
2733 /* Don't check for overlay strings below when set to deliver
2734 characters from a display vector. */
2735 if (it->method == GET_FROM_DISPLAY_VECTOR)
2736 handle_overlay_change_p = 0;
2737
2738 /* Handle overlay changes. */
2739 if (handle_overlay_change_p)
2740 handled = handle_overlay_change (it);
2741
2742 /* Determine where to stop next. */
2743 if (handled == HANDLED_NORMALLY)
2744 compute_stop_pos (it);
2745 }
2746 }
2747 while (handled == HANDLED_RECOMPUTE_PROPS);
2748 }
2749
2750
2751 /* Compute IT->stop_charpos from text property and overlay change
2752 information for IT's current position. */
2753
2754 static void
2755 compute_stop_pos (it)
2756 struct it *it;
2757 {
2758 register INTERVAL iv, next_iv;
2759 Lisp_Object object, limit, position;
2760
2761 /* If nowhere else, stop at the end. */
2762 it->stop_charpos = it->end_charpos;
2763
2764 if (STRINGP (it->string))
2765 {
2766 /* Strings are usually short, so don't limit the search for
2767 properties. */
2768 object = it->string;
2769 limit = Qnil;
2770 position = make_number (IT_STRING_CHARPOS (*it));
2771 }
2772 else
2773 {
2774 int charpos;
2775
2776 /* If next overlay change is in front of the current stop pos
2777 (which is IT->end_charpos), stop there. Note: value of
2778 next_overlay_change is point-max if no overlay change
2779 follows. */
2780 charpos = next_overlay_change (IT_CHARPOS (*it));
2781 if (charpos < it->stop_charpos)
2782 it->stop_charpos = charpos;
2783
2784 /* If showing the region, we have to stop at the region
2785 start or end because the face might change there. */
2786 if (it->region_beg_charpos > 0)
2787 {
2788 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2789 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2790 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2791 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2792 }
2793
2794 /* Set up variables for computing the stop position from text
2795 property changes. */
2796 XSETBUFFER (object, current_buffer);
2797 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2798 position = make_number (IT_CHARPOS (*it));
2799
2800 }
2801
2802 /* Get the interval containing IT's position. Value is a null
2803 interval if there isn't such an interval. */
2804 iv = validate_interval_range (object, &position, &position, 0);
2805 if (!NULL_INTERVAL_P (iv))
2806 {
2807 Lisp_Object values_here[LAST_PROP_IDX];
2808 struct props *p;
2809
2810 /* Get properties here. */
2811 for (p = it_props; p->handler; ++p)
2812 values_here[p->idx] = textget (iv->plist, *p->name);
2813
2814 /* Look for an interval following iv that has different
2815 properties. */
2816 for (next_iv = next_interval (iv);
2817 (!NULL_INTERVAL_P (next_iv)
2818 && (NILP (limit)
2819 || XFASTINT (limit) > next_iv->position));
2820 next_iv = next_interval (next_iv))
2821 {
2822 for (p = it_props; p->handler; ++p)
2823 {
2824 Lisp_Object new_value;
2825
2826 new_value = textget (next_iv->plist, *p->name);
2827 if (!EQ (values_here[p->idx], new_value))
2828 break;
2829 }
2830
2831 if (p->handler)
2832 break;
2833 }
2834
2835 if (!NULL_INTERVAL_P (next_iv))
2836 {
2837 if (INTEGERP (limit)
2838 && next_iv->position >= XFASTINT (limit))
2839 /* No text property change up to limit. */
2840 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2841 else
2842 /* Text properties change in next_iv. */
2843 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2844 }
2845 }
2846
2847 xassert (STRINGP (it->string)
2848 || (it->stop_charpos >= BEGV
2849 && it->stop_charpos >= IT_CHARPOS (*it)));
2850 }
2851
2852
2853 /* Return the position of the next overlay change after POS in
2854 current_buffer. Value is point-max if no overlay change
2855 follows. This is like `next-overlay-change' but doesn't use
2856 xmalloc. */
2857
2858 static int
2859 next_overlay_change (pos)
2860 int pos;
2861 {
2862 int noverlays;
2863 int endpos;
2864 Lisp_Object *overlays;
2865 int i;
2866
2867 /* Get all overlays at the given position. */
2868 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2869
2870 /* If any of these overlays ends before endpos,
2871 use its ending point instead. */
2872 for (i = 0; i < noverlays; ++i)
2873 {
2874 Lisp_Object oend;
2875 int oendpos;
2876
2877 oend = OVERLAY_END (overlays[i]);
2878 oendpos = OVERLAY_POSITION (oend);
2879 endpos = min (endpos, oendpos);
2880 }
2881
2882 return endpos;
2883 }
2884
2885
2886 \f
2887 /***********************************************************************
2888 Fontification
2889 ***********************************************************************/
2890
2891 /* Handle changes in the `fontified' property of the current buffer by
2892 calling hook functions from Qfontification_functions to fontify
2893 regions of text. */
2894
2895 static enum prop_handled
2896 handle_fontified_prop (it)
2897 struct it *it;
2898 {
2899 Lisp_Object prop, pos;
2900 enum prop_handled handled = HANDLED_NORMALLY;
2901
2902 /* Get the value of the `fontified' property at IT's current buffer
2903 position. (The `fontified' property doesn't have a special
2904 meaning in strings.) If the value is nil, call functions from
2905 Qfontification_functions. */
2906 if (!STRINGP (it->string)
2907 && it->s == NULL
2908 && !NILP (Vfontification_functions)
2909 && !NILP (Vrun_hooks)
2910 && (pos = make_number (IT_CHARPOS (*it)),
2911 prop = Fget_char_property (pos, Qfontified, Qnil),
2912 NILP (prop)))
2913 {
2914 int count = SPECPDL_INDEX ();
2915 Lisp_Object val;
2916
2917 val = Vfontification_functions;
2918 specbind (Qfontification_functions, Qnil);
2919
2920 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2921 safe_call1 (val, pos);
2922 else
2923 {
2924 Lisp_Object globals, fn;
2925 struct gcpro gcpro1, gcpro2;
2926
2927 globals = Qnil;
2928 GCPRO2 (val, globals);
2929
2930 for (; CONSP (val); val = XCDR (val))
2931 {
2932 fn = XCAR (val);
2933
2934 if (EQ (fn, Qt))
2935 {
2936 /* A value of t indicates this hook has a local
2937 binding; it means to run the global binding too.
2938 In a global value, t should not occur. If it
2939 does, we must ignore it to avoid an endless
2940 loop. */
2941 for (globals = Fdefault_value (Qfontification_functions);
2942 CONSP (globals);
2943 globals = XCDR (globals))
2944 {
2945 fn = XCAR (globals);
2946 if (!EQ (fn, Qt))
2947 safe_call1 (fn, pos);
2948 }
2949 }
2950 else
2951 safe_call1 (fn, pos);
2952 }
2953
2954 UNGCPRO;
2955 }
2956
2957 unbind_to (count, Qnil);
2958
2959 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2960 something. This avoids an endless loop if they failed to
2961 fontify the text for which reason ever. */
2962 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2963 handled = HANDLED_RECOMPUTE_PROPS;
2964 }
2965
2966 return handled;
2967 }
2968
2969
2970 \f
2971 /***********************************************************************
2972 Faces
2973 ***********************************************************************/
2974
2975 /* Set up iterator IT from face properties at its current position.
2976 Called from handle_stop. */
2977
2978 static enum prop_handled
2979 handle_face_prop (it)
2980 struct it *it;
2981 {
2982 int new_face_id, next_stop;
2983
2984 if (!STRINGP (it->string))
2985 {
2986 new_face_id
2987 = face_at_buffer_position (it->w,
2988 IT_CHARPOS (*it),
2989 it->region_beg_charpos,
2990 it->region_end_charpos,
2991 &next_stop,
2992 (IT_CHARPOS (*it)
2993 + TEXT_PROP_DISTANCE_LIMIT),
2994 0);
2995
2996 /* Is this a start of a run of characters with box face?
2997 Caveat: this can be called for a freshly initialized
2998 iterator; face_id is -1 in this case. We know that the new
2999 face will not change until limit, i.e. if the new face has a
3000 box, all characters up to limit will have one. But, as
3001 usual, we don't know whether limit is really the end. */
3002 if (new_face_id != it->face_id)
3003 {
3004 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3005
3006 /* If new face has a box but old face has not, this is
3007 the start of a run of characters with box, i.e. it has
3008 a shadow on the left side. The value of face_id of the
3009 iterator will be -1 if this is the initial call that gets
3010 the face. In this case, we have to look in front of IT's
3011 position and see whether there is a face != new_face_id. */
3012 it->start_of_box_run_p
3013 = (new_face->box != FACE_NO_BOX
3014 && (it->face_id >= 0
3015 || IT_CHARPOS (*it) == BEG
3016 || new_face_id != face_before_it_pos (it)));
3017 it->face_box_p = new_face->box != FACE_NO_BOX;
3018 }
3019 }
3020 else
3021 {
3022 int base_face_id, bufpos;
3023
3024 if (it->current.overlay_string_index >= 0)
3025 bufpos = IT_CHARPOS (*it);
3026 else
3027 bufpos = 0;
3028
3029 /* For strings from a buffer, i.e. overlay strings or strings
3030 from a `display' property, use the face at IT's current
3031 buffer position as the base face to merge with, so that
3032 overlay strings appear in the same face as surrounding
3033 text, unless they specify their own faces. */
3034 base_face_id = underlying_face_id (it);
3035
3036 new_face_id = face_at_string_position (it->w,
3037 it->string,
3038 IT_STRING_CHARPOS (*it),
3039 bufpos,
3040 it->region_beg_charpos,
3041 it->region_end_charpos,
3042 &next_stop,
3043 base_face_id, 0);
3044
3045 #if 0 /* This shouldn't be neccessary. Let's check it. */
3046 /* If IT is used to display a mode line we would really like to
3047 use the mode line face instead of the frame's default face. */
3048 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3049 && new_face_id == DEFAULT_FACE_ID)
3050 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3051 #endif
3052
3053 /* Is this a start of a run of characters with box? Caveat:
3054 this can be called for a freshly allocated iterator; face_id
3055 is -1 is this case. We know that the new face will not
3056 change until the next check pos, i.e. if the new face has a
3057 box, all characters up to that position will have a
3058 box. But, as usual, we don't know whether that position
3059 is really the end. */
3060 if (new_face_id != it->face_id)
3061 {
3062 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3063 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3064
3065 /* If new face has a box but old face hasn't, this is the
3066 start of a run of characters with box, i.e. it has a
3067 shadow on the left side. */
3068 it->start_of_box_run_p
3069 = new_face->box && (old_face == NULL || !old_face->box);
3070 it->face_box_p = new_face->box != FACE_NO_BOX;
3071 }
3072 }
3073
3074 it->face_id = new_face_id;
3075 return HANDLED_NORMALLY;
3076 }
3077
3078
3079 /* Return the ID of the face ``underlying'' IT's current position,
3080 which is in a string. If the iterator is associated with a
3081 buffer, return the face at IT's current buffer position.
3082 Otherwise, use the iterator's base_face_id. */
3083
3084 static int
3085 underlying_face_id (it)
3086 struct it *it;
3087 {
3088 int face_id = it->base_face_id, i;
3089
3090 xassert (STRINGP (it->string));
3091
3092 for (i = it->sp - 1; i >= 0; --i)
3093 if (NILP (it->stack[i].string))
3094 face_id = it->stack[i].face_id;
3095
3096 return face_id;
3097 }
3098
3099
3100 /* Compute the face one character before or after the current position
3101 of IT. BEFORE_P non-zero means get the face in front of IT's
3102 position. Value is the id of the face. */
3103
3104 static int
3105 face_before_or_after_it_pos (it, before_p)
3106 struct it *it;
3107 int before_p;
3108 {
3109 int face_id, limit;
3110 int next_check_charpos;
3111 struct text_pos pos;
3112
3113 xassert (it->s == NULL);
3114
3115 if (STRINGP (it->string))
3116 {
3117 int bufpos, base_face_id;
3118
3119 /* No face change past the end of the string (for the case
3120 we are padding with spaces). No face change before the
3121 string start. */
3122 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3123 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3124 return it->face_id;
3125
3126 /* Set pos to the position before or after IT's current position. */
3127 if (before_p)
3128 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3129 else
3130 /* For composition, we must check the character after the
3131 composition. */
3132 pos = (it->what == IT_COMPOSITION
3133 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3134 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3135
3136 if (it->current.overlay_string_index >= 0)
3137 bufpos = IT_CHARPOS (*it);
3138 else
3139 bufpos = 0;
3140
3141 base_face_id = underlying_face_id (it);
3142
3143 /* Get the face for ASCII, or unibyte. */
3144 face_id = face_at_string_position (it->w,
3145 it->string,
3146 CHARPOS (pos),
3147 bufpos,
3148 it->region_beg_charpos,
3149 it->region_end_charpos,
3150 &next_check_charpos,
3151 base_face_id, 0);
3152
3153 /* Correct the face for charsets different from ASCII. Do it
3154 for the multibyte case only. The face returned above is
3155 suitable for unibyte text if IT->string is unibyte. */
3156 if (STRING_MULTIBYTE (it->string))
3157 {
3158 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3159 int rest = SBYTES (it->string) - BYTEPOS (pos);
3160 int c, len;
3161 struct face *face = FACE_FROM_ID (it->f, face_id);
3162
3163 c = string_char_and_length (p, rest, &len);
3164 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3165 }
3166 }
3167 else
3168 {
3169 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3170 || (IT_CHARPOS (*it) <= BEGV && before_p))
3171 return it->face_id;
3172
3173 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3174 pos = it->current.pos;
3175
3176 if (before_p)
3177 DEC_TEXT_POS (pos, it->multibyte_p);
3178 else
3179 {
3180 if (it->what == IT_COMPOSITION)
3181 /* For composition, we must check the position after the
3182 composition. */
3183 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3184 else
3185 INC_TEXT_POS (pos, it->multibyte_p);
3186 }
3187
3188 /* Determine face for CHARSET_ASCII, or unibyte. */
3189 face_id = face_at_buffer_position (it->w,
3190 CHARPOS (pos),
3191 it->region_beg_charpos,
3192 it->region_end_charpos,
3193 &next_check_charpos,
3194 limit, 0);
3195
3196 /* Correct the face for charsets different from ASCII. Do it
3197 for the multibyte case only. The face returned above is
3198 suitable for unibyte text if current_buffer is unibyte. */
3199 if (it->multibyte_p)
3200 {
3201 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3202 struct face *face = FACE_FROM_ID (it->f, face_id);
3203 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3204 }
3205 }
3206
3207 return face_id;
3208 }
3209
3210
3211 \f
3212 /***********************************************************************
3213 Invisible text
3214 ***********************************************************************/
3215
3216 /* Set up iterator IT from invisible properties at its current
3217 position. Called from handle_stop. */
3218
3219 static enum prop_handled
3220 handle_invisible_prop (it)
3221 struct it *it;
3222 {
3223 enum prop_handled handled = HANDLED_NORMALLY;
3224
3225 if (STRINGP (it->string))
3226 {
3227 extern Lisp_Object Qinvisible;
3228 Lisp_Object prop, end_charpos, limit, charpos;
3229
3230 /* Get the value of the invisible text property at the
3231 current position. Value will be nil if there is no such
3232 property. */
3233 charpos = make_number (IT_STRING_CHARPOS (*it));
3234 prop = Fget_text_property (charpos, Qinvisible, it->string);
3235
3236 if (!NILP (prop)
3237 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3238 {
3239 handled = HANDLED_RECOMPUTE_PROPS;
3240
3241 /* Get the position at which the next change of the
3242 invisible text property can be found in IT->string.
3243 Value will be nil if the property value is the same for
3244 all the rest of IT->string. */
3245 XSETINT (limit, SCHARS (it->string));
3246 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3247 it->string, limit);
3248
3249 /* Text at current position is invisible. The next
3250 change in the property is at position end_charpos.
3251 Move IT's current position to that position. */
3252 if (INTEGERP (end_charpos)
3253 && XFASTINT (end_charpos) < XFASTINT (limit))
3254 {
3255 struct text_pos old;
3256 old = it->current.string_pos;
3257 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3258 compute_string_pos (&it->current.string_pos, old, it->string);
3259 }
3260 else
3261 {
3262 /* The rest of the string is invisible. If this is an
3263 overlay string, proceed with the next overlay string
3264 or whatever comes and return a character from there. */
3265 if (it->current.overlay_string_index >= 0)
3266 {
3267 next_overlay_string (it);
3268 /* Don't check for overlay strings when we just
3269 finished processing them. */
3270 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3271 }
3272 else
3273 {
3274 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3275 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3276 }
3277 }
3278 }
3279 }
3280 else
3281 {
3282 int invis_p, newpos, next_stop, start_charpos;
3283 Lisp_Object pos, prop, overlay;
3284
3285 /* First of all, is there invisible text at this position? */
3286 start_charpos = IT_CHARPOS (*it);
3287 pos = make_number (IT_CHARPOS (*it));
3288 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3289 &overlay);
3290 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3291
3292 /* If we are on invisible text, skip over it. */
3293 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3294 {
3295 /* Record whether we have to display an ellipsis for the
3296 invisible text. */
3297 int display_ellipsis_p = invis_p == 2;
3298
3299 handled = HANDLED_RECOMPUTE_PROPS;
3300
3301 /* Loop skipping over invisible text. The loop is left at
3302 ZV or with IT on the first char being visible again. */
3303 do
3304 {
3305 /* Try to skip some invisible text. Return value is the
3306 position reached which can be equal to IT's position
3307 if there is nothing invisible here. This skips both
3308 over invisible text properties and overlays with
3309 invisible property. */
3310 newpos = skip_invisible (IT_CHARPOS (*it),
3311 &next_stop, ZV, it->window);
3312
3313 /* If we skipped nothing at all we weren't at invisible
3314 text in the first place. If everything to the end of
3315 the buffer was skipped, end the loop. */
3316 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3317 invis_p = 0;
3318 else
3319 {
3320 /* We skipped some characters but not necessarily
3321 all there are. Check if we ended up on visible
3322 text. Fget_char_property returns the property of
3323 the char before the given position, i.e. if we
3324 get invis_p = 0, this means that the char at
3325 newpos is visible. */
3326 pos = make_number (newpos);
3327 prop = Fget_char_property (pos, Qinvisible, it->window);
3328 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3329 }
3330
3331 /* If we ended up on invisible text, proceed to
3332 skip starting with next_stop. */
3333 if (invis_p)
3334 IT_CHARPOS (*it) = next_stop;
3335 }
3336 while (invis_p);
3337
3338 /* The position newpos is now either ZV or on visible text. */
3339 IT_CHARPOS (*it) = newpos;
3340 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3341
3342 /* If there are before-strings at the start of invisible
3343 text, and the text is invisible because of a text
3344 property, arrange to show before-strings because 20.x did
3345 it that way. (If the text is invisible because of an
3346 overlay property instead of a text property, this is
3347 already handled in the overlay code.) */
3348 if (NILP (overlay)
3349 && get_overlay_strings (it, start_charpos))
3350 {
3351 handled = HANDLED_RECOMPUTE_PROPS;
3352 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3353 }
3354 else if (display_ellipsis_p)
3355 setup_for_ellipsis (it, 0);
3356 }
3357 }
3358
3359 return handled;
3360 }
3361
3362
3363 /* Make iterator IT return `...' next.
3364 Replaces LEN characters from buffer. */
3365
3366 static void
3367 setup_for_ellipsis (it, len)
3368 struct it *it;
3369 int len;
3370 {
3371 /* Use the display table definition for `...'. Invalid glyphs
3372 will be handled by the method returning elements from dpvec. */
3373 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3374 {
3375 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3376 it->dpvec = v->contents;
3377 it->dpend = v->contents + v->size;
3378 }
3379 else
3380 {
3381 /* Default `...'. */
3382 it->dpvec = default_invis_vector;
3383 it->dpend = default_invis_vector + 3;
3384 }
3385
3386 it->dpvec_char_len = len;
3387 it->current.dpvec_index = 0;
3388 it->dpvec_face_id = -1;
3389
3390 /* Remember the current face id in case glyphs specify faces.
3391 IT's face is restored in set_iterator_to_next.
3392 saved_face_id was set to preceding char's face in handle_stop. */
3393 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3394 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3395
3396 it->method = GET_FROM_DISPLAY_VECTOR;
3397 it->ellipsis_p = 1;
3398 }
3399
3400
3401 \f
3402 /***********************************************************************
3403 'display' property
3404 ***********************************************************************/
3405
3406 /* Set up iterator IT from `display' property at its current position.
3407 Called from handle_stop.
3408 We return HANDLED_RETURN if some part of the display property
3409 overrides the display of the buffer text itself.
3410 Otherwise we return HANDLED_NORMALLY. */
3411
3412 static enum prop_handled
3413 handle_display_prop (it)
3414 struct it *it;
3415 {
3416 Lisp_Object prop, object;
3417 struct text_pos *position;
3418 /* Nonzero if some property replaces the display of the text itself. */
3419 int display_replaced_p = 0;
3420
3421 if (STRINGP (it->string))
3422 {
3423 object = it->string;
3424 position = &it->current.string_pos;
3425 }
3426 else
3427 {
3428 object = it->w->buffer;
3429 position = &it->current.pos;
3430 }
3431
3432 /* Reset those iterator values set from display property values. */
3433 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3434 it->space_width = Qnil;
3435 it->font_height = Qnil;
3436 it->voffset = 0;
3437
3438 /* We don't support recursive `display' properties, i.e. string
3439 values that have a string `display' property, that have a string
3440 `display' property etc. */
3441 if (!it->string_from_display_prop_p)
3442 it->area = TEXT_AREA;
3443
3444 prop = Fget_char_property (make_number (position->charpos),
3445 Qdisplay, object);
3446 if (NILP (prop))
3447 return HANDLED_NORMALLY;
3448
3449 if (CONSP (prop)
3450 /* Simple properties. */
3451 && !EQ (XCAR (prop), Qimage)
3452 && !EQ (XCAR (prop), Qspace)
3453 && !EQ (XCAR (prop), Qwhen)
3454 && !EQ (XCAR (prop), Qslice)
3455 && !EQ (XCAR (prop), Qspace_width)
3456 && !EQ (XCAR (prop), Qheight)
3457 && !EQ (XCAR (prop), Qraise)
3458 /* Marginal area specifications. */
3459 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3460 && !EQ (XCAR (prop), Qleft_fringe)
3461 && !EQ (XCAR (prop), Qright_fringe)
3462 && !NILP (XCAR (prop)))
3463 {
3464 for (; CONSP (prop); prop = XCDR (prop))
3465 {
3466 if (handle_single_display_spec (it, XCAR (prop), object,
3467 position, display_replaced_p))
3468 display_replaced_p = 1;
3469 }
3470 }
3471 else if (VECTORP (prop))
3472 {
3473 int i;
3474 for (i = 0; i < ASIZE (prop); ++i)
3475 if (handle_single_display_spec (it, AREF (prop, i), object,
3476 position, display_replaced_p))
3477 display_replaced_p = 1;
3478 }
3479 else
3480 {
3481 int ret = handle_single_display_spec (it, prop, object, position, 0);
3482 if (ret < 0) /* Replaced by "", i.e. nothing. */
3483 return HANDLED_RECOMPUTE_PROPS;
3484 if (ret)
3485 display_replaced_p = 1;
3486 }
3487
3488 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3489 }
3490
3491
3492 /* Value is the position of the end of the `display' property starting
3493 at START_POS in OBJECT. */
3494
3495 static struct text_pos
3496 display_prop_end (it, object, start_pos)
3497 struct it *it;
3498 Lisp_Object object;
3499 struct text_pos start_pos;
3500 {
3501 Lisp_Object end;
3502 struct text_pos end_pos;
3503
3504 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3505 Qdisplay, object, Qnil);
3506 CHARPOS (end_pos) = XFASTINT (end);
3507 if (STRINGP (object))
3508 compute_string_pos (&end_pos, start_pos, it->string);
3509 else
3510 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3511
3512 return end_pos;
3513 }
3514
3515
3516 /* Set up IT from a single `display' specification PROP. OBJECT
3517 is the object in which the `display' property was found. *POSITION
3518 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3519 means that we previously saw a display specification which already
3520 replaced text display with something else, for example an image;
3521 we ignore such properties after the first one has been processed.
3522
3523 If PROP is a `space' or `image' specification, and in some other
3524 cases too, set *POSITION to the position where the `display'
3525 property ends.
3526
3527 Value is non-zero if something was found which replaces the display
3528 of buffer or string text. Specifically, the value is -1 if that
3529 "something" is "nothing". */
3530
3531 static int
3532 handle_single_display_spec (it, spec, object, position,
3533 display_replaced_before_p)
3534 struct it *it;
3535 Lisp_Object spec;
3536 Lisp_Object object;
3537 struct text_pos *position;
3538 int display_replaced_before_p;
3539 {
3540 Lisp_Object form;
3541 Lisp_Object location, value;
3542 struct text_pos start_pos;
3543 int valid_p;
3544
3545 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3546 If the result is non-nil, use VALUE instead of SPEC. */
3547 form = Qt;
3548 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3549 {
3550 spec = XCDR (spec);
3551 if (!CONSP (spec))
3552 return 0;
3553 form = XCAR (spec);
3554 spec = XCDR (spec);
3555 }
3556
3557 if (!NILP (form) && !EQ (form, Qt))
3558 {
3559 int count = SPECPDL_INDEX ();
3560 struct gcpro gcpro1;
3561
3562 /* Bind `object' to the object having the `display' property, a
3563 buffer or string. Bind `position' to the position in the
3564 object where the property was found, and `buffer-position'
3565 to the current position in the buffer. */
3566 specbind (Qobject, object);
3567 specbind (Qposition, make_number (CHARPOS (*position)));
3568 specbind (Qbuffer_position,
3569 make_number (STRINGP (object)
3570 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3571 GCPRO1 (form);
3572 form = safe_eval (form);
3573 UNGCPRO;
3574 unbind_to (count, Qnil);
3575 }
3576
3577 if (NILP (form))
3578 return 0;
3579
3580 /* Handle `(height HEIGHT)' specifications. */
3581 if (CONSP (spec)
3582 && EQ (XCAR (spec), Qheight)
3583 && CONSP (XCDR (spec)))
3584 {
3585 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3586 return 0;
3587
3588 it->font_height = XCAR (XCDR (spec));
3589 if (!NILP (it->font_height))
3590 {
3591 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3592 int new_height = -1;
3593
3594 if (CONSP (it->font_height)
3595 && (EQ (XCAR (it->font_height), Qplus)
3596 || EQ (XCAR (it->font_height), Qminus))
3597 && CONSP (XCDR (it->font_height))
3598 && INTEGERP (XCAR (XCDR (it->font_height))))
3599 {
3600 /* `(+ N)' or `(- N)' where N is an integer. */
3601 int steps = XINT (XCAR (XCDR (it->font_height)));
3602 if (EQ (XCAR (it->font_height), Qplus))
3603 steps = - steps;
3604 it->face_id = smaller_face (it->f, it->face_id, steps);
3605 }
3606 else if (FUNCTIONP (it->font_height))
3607 {
3608 /* Call function with current height as argument.
3609 Value is the new height. */
3610 Lisp_Object height;
3611 height = safe_call1 (it->font_height,
3612 face->lface[LFACE_HEIGHT_INDEX]);
3613 if (NUMBERP (height))
3614 new_height = XFLOATINT (height);
3615 }
3616 else if (NUMBERP (it->font_height))
3617 {
3618 /* Value is a multiple of the canonical char height. */
3619 struct face *face;
3620
3621 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3622 new_height = (XFLOATINT (it->font_height)
3623 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3624 }
3625 else
3626 {
3627 /* Evaluate IT->font_height with `height' bound to the
3628 current specified height to get the new height. */
3629 int count = SPECPDL_INDEX ();
3630
3631 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3632 value = safe_eval (it->font_height);
3633 unbind_to (count, Qnil);
3634
3635 if (NUMBERP (value))
3636 new_height = XFLOATINT (value);
3637 }
3638
3639 if (new_height > 0)
3640 it->face_id = face_with_height (it->f, it->face_id, new_height);
3641 }
3642
3643 return 0;
3644 }
3645
3646 /* Handle `(space_width WIDTH)'. */
3647 if (CONSP (spec)
3648 && EQ (XCAR (spec), Qspace_width)
3649 && CONSP (XCDR (spec)))
3650 {
3651 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3652 return 0;
3653
3654 value = XCAR (XCDR (spec));
3655 if (NUMBERP (value) && XFLOATINT (value) > 0)
3656 it->space_width = value;
3657
3658 return 0;
3659 }
3660
3661 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3662 if (CONSP (spec)
3663 && EQ (XCAR (spec), Qslice))
3664 {
3665 Lisp_Object tem;
3666
3667 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3668 return 0;
3669
3670 if (tem = XCDR (spec), CONSP (tem))
3671 {
3672 it->slice.x = XCAR (tem);
3673 if (tem = XCDR (tem), CONSP (tem))
3674 {
3675 it->slice.y = XCAR (tem);
3676 if (tem = XCDR (tem), CONSP (tem))
3677 {
3678 it->slice.width = XCAR (tem);
3679 if (tem = XCDR (tem), CONSP (tem))
3680 it->slice.height = XCAR (tem);
3681 }
3682 }
3683 }
3684
3685 return 0;
3686 }
3687
3688 /* Handle `(raise FACTOR)'. */
3689 if (CONSP (spec)
3690 && EQ (XCAR (spec), Qraise)
3691 && CONSP (XCDR (spec)))
3692 {
3693 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3694 return 0;
3695
3696 #ifdef HAVE_WINDOW_SYSTEM
3697 value = XCAR (XCDR (spec));
3698 if (NUMBERP (value))
3699 {
3700 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3701 it->voffset = - (XFLOATINT (value)
3702 * (FONT_HEIGHT (face->font)));
3703 }
3704 #endif /* HAVE_WINDOW_SYSTEM */
3705
3706 return 0;
3707 }
3708
3709 /* Don't handle the other kinds of display specifications
3710 inside a string that we got from a `display' property. */
3711 if (it->string_from_display_prop_p)
3712 return 0;
3713
3714 /* Characters having this form of property are not displayed, so
3715 we have to find the end of the property. */
3716 start_pos = *position;
3717 *position = display_prop_end (it, object, start_pos);
3718 value = Qnil;
3719
3720 /* Stop the scan at that end position--we assume that all
3721 text properties change there. */
3722 it->stop_charpos = position->charpos;
3723
3724 /* Handle `(left-fringe BITMAP [FACE])'
3725 and `(right-fringe BITMAP [FACE])'. */
3726 if (CONSP (spec)
3727 && (EQ (XCAR (spec), Qleft_fringe)
3728 || EQ (XCAR (spec), Qright_fringe))
3729 && CONSP (XCDR (spec)))
3730 {
3731 int face_id = DEFAULT_FACE_ID;
3732 int fringe_bitmap;
3733
3734 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3735 /* If we return here, POSITION has been advanced
3736 across the text with this property. */
3737 return 0;
3738
3739 #ifdef HAVE_WINDOW_SYSTEM
3740 value = XCAR (XCDR (spec));
3741 if (!SYMBOLP (value)
3742 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3743 /* If we return here, POSITION has been advanced
3744 across the text with this property. */
3745 return 0;
3746
3747 if (CONSP (XCDR (XCDR (spec))))
3748 {
3749 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3750 int face_id2 = lookup_derived_face (it->f, face_name,
3751 FRINGE_FACE_ID, 0);
3752 if (face_id2 >= 0)
3753 face_id = face_id2;
3754 }
3755
3756 /* Save current settings of IT so that we can restore them
3757 when we are finished with the glyph property value. */
3758
3759 push_it (it);
3760
3761 it->area = TEXT_AREA;
3762 it->what = IT_IMAGE;
3763 it->image_id = -1; /* no image */
3764 it->position = start_pos;
3765 it->object = NILP (object) ? it->w->buffer : object;
3766 it->method = GET_FROM_IMAGE;
3767 it->face_id = face_id;
3768
3769 /* Say that we haven't consumed the characters with
3770 `display' property yet. The call to pop_it in
3771 set_iterator_to_next will clean this up. */
3772 *position = start_pos;
3773
3774 if (EQ (XCAR (spec), Qleft_fringe))
3775 {
3776 it->left_user_fringe_bitmap = fringe_bitmap;
3777 it->left_user_fringe_face_id = face_id;
3778 }
3779 else
3780 {
3781 it->right_user_fringe_bitmap = fringe_bitmap;
3782 it->right_user_fringe_face_id = face_id;
3783 }
3784 #endif /* HAVE_WINDOW_SYSTEM */
3785 return 1;
3786 }
3787
3788 /* Prepare to handle `((margin left-margin) ...)',
3789 `((margin right-margin) ...)' and `((margin nil) ...)'
3790 prefixes for display specifications. */
3791 location = Qunbound;
3792 if (CONSP (spec) && CONSP (XCAR (spec)))
3793 {
3794 Lisp_Object tem;
3795
3796 value = XCDR (spec);
3797 if (CONSP (value))
3798 value = XCAR (value);
3799
3800 tem = XCAR (spec);
3801 if (EQ (XCAR (tem), Qmargin)
3802 && (tem = XCDR (tem),
3803 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3804 (NILP (tem)
3805 || EQ (tem, Qleft_margin)
3806 || EQ (tem, Qright_margin))))
3807 location = tem;
3808 }
3809
3810 if (EQ (location, Qunbound))
3811 {
3812 location = Qnil;
3813 value = spec;
3814 }
3815
3816 /* After this point, VALUE is the property after any
3817 margin prefix has been stripped. It must be a string,
3818 an image specification, or `(space ...)'.
3819
3820 LOCATION specifies where to display: `left-margin',
3821 `right-margin' or nil. */
3822
3823 valid_p = (STRINGP (value)
3824 #ifdef HAVE_WINDOW_SYSTEM
3825 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3826 #endif /* not HAVE_WINDOW_SYSTEM */
3827 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3828
3829 if (valid_p && !display_replaced_before_p)
3830 {
3831 /* Save current settings of IT so that we can restore them
3832 when we are finished with the glyph property value. */
3833 push_it (it);
3834
3835 if (NILP (location))
3836 it->area = TEXT_AREA;
3837 else if (EQ (location, Qleft_margin))
3838 it->area = LEFT_MARGIN_AREA;
3839 else
3840 it->area = RIGHT_MARGIN_AREA;
3841
3842 if (STRINGP (value))
3843 {
3844 if (SCHARS (value) == 0)
3845 {
3846 pop_it (it);
3847 return -1; /* Replaced by "", i.e. nothing. */
3848 }
3849 it->string = value;
3850 it->multibyte_p = STRING_MULTIBYTE (it->string);
3851 it->current.overlay_string_index = -1;
3852 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3853 it->end_charpos = it->string_nchars = SCHARS (it->string);
3854 it->method = GET_FROM_STRING;
3855 it->stop_charpos = 0;
3856 it->string_from_display_prop_p = 1;
3857 /* Say that we haven't consumed the characters with
3858 `display' property yet. The call to pop_it in
3859 set_iterator_to_next will clean this up. */
3860 *position = start_pos;
3861 }
3862 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3863 {
3864 it->method = GET_FROM_STRETCH;
3865 it->object = value;
3866 it->current.pos = it->position = start_pos;
3867 }
3868 #ifdef HAVE_WINDOW_SYSTEM
3869 else
3870 {
3871 it->what = IT_IMAGE;
3872 it->image_id = lookup_image (it->f, value);
3873 it->position = start_pos;
3874 it->object = NILP (object) ? it->w->buffer : object;
3875 it->method = GET_FROM_IMAGE;
3876
3877 /* Say that we haven't consumed the characters with
3878 `display' property yet. The call to pop_it in
3879 set_iterator_to_next will clean this up. */
3880 *position = start_pos;
3881 }
3882 #endif /* HAVE_WINDOW_SYSTEM */
3883
3884 return 1;
3885 }
3886
3887 /* Invalid property or property not supported. Restore
3888 POSITION to what it was before. */
3889 *position = start_pos;
3890 return 0;
3891 }
3892
3893
3894 /* Check if SPEC is a display specification value whose text should be
3895 treated as intangible. */
3896
3897 static int
3898 single_display_spec_intangible_p (prop)
3899 Lisp_Object prop;
3900 {
3901 /* Skip over `when FORM'. */
3902 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3903 {
3904 prop = XCDR (prop);
3905 if (!CONSP (prop))
3906 return 0;
3907 prop = XCDR (prop);
3908 }
3909
3910 if (STRINGP (prop))
3911 return 1;
3912
3913 if (!CONSP (prop))
3914 return 0;
3915
3916 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3917 we don't need to treat text as intangible. */
3918 if (EQ (XCAR (prop), Qmargin))
3919 {
3920 prop = XCDR (prop);
3921 if (!CONSP (prop))
3922 return 0;
3923
3924 prop = XCDR (prop);
3925 if (!CONSP (prop)
3926 || EQ (XCAR (prop), Qleft_margin)
3927 || EQ (XCAR (prop), Qright_margin))
3928 return 0;
3929 }
3930
3931 return (CONSP (prop)
3932 && (EQ (XCAR (prop), Qimage)
3933 || EQ (XCAR (prop), Qspace)));
3934 }
3935
3936
3937 /* Check if PROP is a display property value whose text should be
3938 treated as intangible. */
3939
3940 int
3941 display_prop_intangible_p (prop)
3942 Lisp_Object prop;
3943 {
3944 if (CONSP (prop)
3945 && CONSP (XCAR (prop))
3946 && !EQ (Qmargin, XCAR (XCAR (prop))))
3947 {
3948 /* A list of sub-properties. */
3949 while (CONSP (prop))
3950 {
3951 if (single_display_spec_intangible_p (XCAR (prop)))
3952 return 1;
3953 prop = XCDR (prop);
3954 }
3955 }
3956 else if (VECTORP (prop))
3957 {
3958 /* A vector of sub-properties. */
3959 int i;
3960 for (i = 0; i < ASIZE (prop); ++i)
3961 if (single_display_spec_intangible_p (AREF (prop, i)))
3962 return 1;
3963 }
3964 else
3965 return single_display_spec_intangible_p (prop);
3966
3967 return 0;
3968 }
3969
3970
3971 /* Return 1 if PROP is a display sub-property value containing STRING. */
3972
3973 static int
3974 single_display_spec_string_p (prop, string)
3975 Lisp_Object prop, string;
3976 {
3977 if (EQ (string, prop))
3978 return 1;
3979
3980 /* Skip over `when FORM'. */
3981 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3982 {
3983 prop = XCDR (prop);
3984 if (!CONSP (prop))
3985 return 0;
3986 prop = XCDR (prop);
3987 }
3988
3989 if (CONSP (prop))
3990 /* Skip over `margin LOCATION'. */
3991 if (EQ (XCAR (prop), Qmargin))
3992 {
3993 prop = XCDR (prop);
3994 if (!CONSP (prop))
3995 return 0;
3996
3997 prop = XCDR (prop);
3998 if (!CONSP (prop))
3999 return 0;
4000 }
4001
4002 return CONSP (prop) && EQ (XCAR (prop), string);
4003 }
4004
4005
4006 /* Return 1 if STRING appears in the `display' property PROP. */
4007
4008 static int
4009 display_prop_string_p (prop, string)
4010 Lisp_Object prop, string;
4011 {
4012 if (CONSP (prop)
4013 && CONSP (XCAR (prop))
4014 && !EQ (Qmargin, XCAR (XCAR (prop))))
4015 {
4016 /* A list of sub-properties. */
4017 while (CONSP (prop))
4018 {
4019 if (single_display_spec_string_p (XCAR (prop), string))
4020 return 1;
4021 prop = XCDR (prop);
4022 }
4023 }
4024 else if (VECTORP (prop))
4025 {
4026 /* A vector of sub-properties. */
4027 int i;
4028 for (i = 0; i < ASIZE (prop); ++i)
4029 if (single_display_spec_string_p (AREF (prop, i), string))
4030 return 1;
4031 }
4032 else
4033 return single_display_spec_string_p (prop, string);
4034
4035 return 0;
4036 }
4037
4038
4039 /* Determine from which buffer position in W's buffer STRING comes
4040 from. AROUND_CHARPOS is an approximate position where it could
4041 be from. Value is the buffer position or 0 if it couldn't be
4042 determined.
4043
4044 W's buffer must be current.
4045
4046 This function is necessary because we don't record buffer positions
4047 in glyphs generated from strings (to keep struct glyph small).
4048 This function may only use code that doesn't eval because it is
4049 called asynchronously from note_mouse_highlight. */
4050
4051 int
4052 string_buffer_position (w, string, around_charpos)
4053 struct window *w;
4054 Lisp_Object string;
4055 int around_charpos;
4056 {
4057 Lisp_Object limit, prop, pos;
4058 const int MAX_DISTANCE = 1000;
4059 int found = 0;
4060
4061 pos = make_number (around_charpos);
4062 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4063 while (!found && !EQ (pos, limit))
4064 {
4065 prop = Fget_char_property (pos, Qdisplay, Qnil);
4066 if (!NILP (prop) && display_prop_string_p (prop, string))
4067 found = 1;
4068 else
4069 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4070 }
4071
4072 if (!found)
4073 {
4074 pos = make_number (around_charpos);
4075 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4076 while (!found && !EQ (pos, limit))
4077 {
4078 prop = Fget_char_property (pos, Qdisplay, Qnil);
4079 if (!NILP (prop) && display_prop_string_p (prop, string))
4080 found = 1;
4081 else
4082 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4083 limit);
4084 }
4085 }
4086
4087 return found ? XINT (pos) : 0;
4088 }
4089
4090
4091 \f
4092 /***********************************************************************
4093 `composition' property
4094 ***********************************************************************/
4095
4096 static enum prop_handled
4097 handle_auto_composed_prop (it)
4098 struct it *it;
4099 {
4100 enum prop_handled handled = HANDLED_NORMALLY;
4101
4102 if (FUNCTIONP (Vauto_composition_function))
4103 {
4104 Lisp_Object val;
4105 EMACS_INT pos, this_pos;
4106
4107 if (STRINGP (it->string))
4108 pos = IT_STRING_CHARPOS (*it);
4109 else
4110 pos = IT_CHARPOS (*it);
4111 this_pos = pos;
4112
4113 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
4114 if (! NILP (val))
4115 {
4116 Lisp_Object limit = Qnil, next;
4117
4118 /* As Fnext_single_char_property_change is very slow, we
4119 limit the search to the current line. */
4120 if (STRINGP (it->string))
4121 limit = make_number (SCHARS (it->string));
4122 else
4123 limit = make_number (find_next_newline_no_quit (pos, 1));
4124
4125 next = (Fnext_single_property_change
4126 (make_number (pos), Qauto_composed, it->string, limit));
4127 if (XINT (next) < XINT (limit))
4128 {
4129 /* The current point is auto-composed, but there exist
4130 characters not yet composed beyond the auto-composed
4131 region. There's a possiblity that the last
4132 characters in the region may be newly composed. */
4133 int charpos = XINT (next) - 1, bytepos, c;
4134
4135 if (STRINGP (it->string))
4136 {
4137 bytepos = string_char_to_byte (it->string, charpos);
4138 c = SDATA (it->string)[bytepos];
4139 }
4140 else
4141 {
4142 bytepos = CHAR_TO_BYTE (charpos);
4143 c = FETCH_BYTE (bytepos);
4144 }
4145 if (c != '\n')
4146 /* If the last character is not newline, it may be
4147 composed with the following characters. */
4148 val = Qnil, pos = charpos + 1;
4149 }
4150 }
4151 if (NILP (val))
4152 {
4153 int count = SPECPDL_INDEX ();
4154 Lisp_Object args[3];
4155
4156 args[0] = Vauto_composition_function;
4157 specbind (Qauto_composition_function, Qnil);
4158 args[1] = make_number (pos);
4159 args[2] = it->string;
4160 safe_call (3, args);
4161 unbind_to (count, Qnil);
4162
4163 if (this_pos == pos)
4164 {
4165 val = Fget_char_property (args[1], Qauto_composed, it->string);
4166 /* Return HANDLED_RECOMPUTE_PROPS only if function composed
4167 something. This avoids an endless loop if they failed to
4168 fontify the text for which reason ever. */
4169 if (! NILP (val))
4170 handled = HANDLED_RECOMPUTE_PROPS;
4171 }
4172 else
4173 handled = HANDLED_RECOMPUTE_PROPS;
4174 }
4175 }
4176
4177 return handled;
4178 }
4179
4180 /* Set up iterator IT from `composition' property at its current
4181 position. Called from handle_stop. */
4182
4183 static enum prop_handled
4184 handle_composition_prop (it)
4185 struct it *it;
4186 {
4187 Lisp_Object prop, string;
4188 EMACS_INT pos, pos_byte, start, end;
4189 enum prop_handled handled = HANDLED_NORMALLY;
4190
4191 if (STRINGP (it->string))
4192 {
4193 pos = IT_STRING_CHARPOS (*it);
4194 pos_byte = IT_STRING_BYTEPOS (*it);
4195 string = it->string;
4196 }
4197 else
4198 {
4199 pos = IT_CHARPOS (*it);
4200 pos_byte = IT_BYTEPOS (*it);
4201 string = Qnil;
4202 }
4203
4204 /* If there's a valid composition and point is not inside of the
4205 composition (in the case that the composition is from the current
4206 buffer), draw a glyph composed from the composition components. */
4207 if (find_composition (pos, -1, &start, &end, &prop, string)
4208 && COMPOSITION_VALID_P (start, end, prop)
4209 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4210 {
4211 int id;
4212
4213 if (start != pos)
4214 {
4215 if (STRINGP (it->string))
4216 pos_byte = string_char_to_byte (it->string, start);
4217 else
4218 pos_byte = CHAR_TO_BYTE (start);
4219 }
4220 id = get_composition_id (start, pos_byte, end - start, prop, string);
4221
4222 if (id >= 0)
4223 {
4224 it->method = GET_FROM_COMPOSITION;
4225 it->cmp_id = id;
4226 it->cmp_len = COMPOSITION_LENGTH (prop);
4227 /* For a terminal, draw only the first character of the
4228 components. */
4229 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4230 it->len = (STRINGP (it->string)
4231 ? string_char_to_byte (it->string, end)
4232 : CHAR_TO_BYTE (end)) - pos_byte;
4233 it->stop_charpos = end;
4234 handled = HANDLED_RETURN;
4235 }
4236 }
4237
4238 return handled;
4239 }
4240
4241
4242 \f
4243 /***********************************************************************
4244 Overlay strings
4245 ***********************************************************************/
4246
4247 /* The following structure is used to record overlay strings for
4248 later sorting in load_overlay_strings. */
4249
4250 struct overlay_entry
4251 {
4252 Lisp_Object overlay;
4253 Lisp_Object string;
4254 int priority;
4255 int after_string_p;
4256 };
4257
4258
4259 /* Set up iterator IT from overlay strings at its current position.
4260 Called from handle_stop. */
4261
4262 static enum prop_handled
4263 handle_overlay_change (it)
4264 struct it *it;
4265 {
4266 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4267 return HANDLED_RECOMPUTE_PROPS;
4268 else
4269 return HANDLED_NORMALLY;
4270 }
4271
4272
4273 /* Set up the next overlay string for delivery by IT, if there is an
4274 overlay string to deliver. Called by set_iterator_to_next when the
4275 end of the current overlay string is reached. If there are more
4276 overlay strings to display, IT->string and
4277 IT->current.overlay_string_index are set appropriately here.
4278 Otherwise IT->string is set to nil. */
4279
4280 static void
4281 next_overlay_string (it)
4282 struct it *it;
4283 {
4284 ++it->current.overlay_string_index;
4285 if (it->current.overlay_string_index == it->n_overlay_strings)
4286 {
4287 /* No more overlay strings. Restore IT's settings to what
4288 they were before overlay strings were processed, and
4289 continue to deliver from current_buffer. */
4290 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4291
4292 pop_it (it);
4293 xassert (it->stop_charpos >= BEGV
4294 && it->stop_charpos <= it->end_charpos);
4295 it->string = Qnil;
4296 it->current.overlay_string_index = -1;
4297 SET_TEXT_POS (it->current.string_pos, -1, -1);
4298 it->n_overlay_strings = 0;
4299 it->method = GET_FROM_BUFFER;
4300
4301 /* If we're at the end of the buffer, record that we have
4302 processed the overlay strings there already, so that
4303 next_element_from_buffer doesn't try it again. */
4304 if (IT_CHARPOS (*it) >= it->end_charpos)
4305 it->overlay_strings_at_end_processed_p = 1;
4306
4307 /* If we have to display `...' for invisible text, set
4308 the iterator up for that. */
4309 if (display_ellipsis_p)
4310 setup_for_ellipsis (it, 0);
4311 }
4312 else
4313 {
4314 /* There are more overlay strings to process. If
4315 IT->current.overlay_string_index has advanced to a position
4316 where we must load IT->overlay_strings with more strings, do
4317 it. */
4318 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4319
4320 if (it->current.overlay_string_index && i == 0)
4321 load_overlay_strings (it, 0);
4322
4323 /* Initialize IT to deliver display elements from the overlay
4324 string. */
4325 it->string = it->overlay_strings[i];
4326 it->multibyte_p = STRING_MULTIBYTE (it->string);
4327 SET_TEXT_POS (it->current.string_pos, 0, 0);
4328 it->method = GET_FROM_STRING;
4329 it->stop_charpos = 0;
4330 }
4331
4332 CHECK_IT (it);
4333 }
4334
4335
4336 /* Compare two overlay_entry structures E1 and E2. Used as a
4337 comparison function for qsort in load_overlay_strings. Overlay
4338 strings for the same position are sorted so that
4339
4340 1. All after-strings come in front of before-strings, except
4341 when they come from the same overlay.
4342
4343 2. Within after-strings, strings are sorted so that overlay strings
4344 from overlays with higher priorities come first.
4345
4346 2. Within before-strings, strings are sorted so that overlay
4347 strings from overlays with higher priorities come last.
4348
4349 Value is analogous to strcmp. */
4350
4351
4352 static int
4353 compare_overlay_entries (e1, e2)
4354 void *e1, *e2;
4355 {
4356 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4357 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4358 int result;
4359
4360 if (entry1->after_string_p != entry2->after_string_p)
4361 {
4362 /* Let after-strings appear in front of before-strings if
4363 they come from different overlays. */
4364 if (EQ (entry1->overlay, entry2->overlay))
4365 result = entry1->after_string_p ? 1 : -1;
4366 else
4367 result = entry1->after_string_p ? -1 : 1;
4368 }
4369 else if (entry1->after_string_p)
4370 /* After-strings sorted in order of decreasing priority. */
4371 result = entry2->priority - entry1->priority;
4372 else
4373 /* Before-strings sorted in order of increasing priority. */
4374 result = entry1->priority - entry2->priority;
4375
4376 return result;
4377 }
4378
4379
4380 /* Load the vector IT->overlay_strings with overlay strings from IT's
4381 current buffer position, or from CHARPOS if that is > 0. Set
4382 IT->n_overlays to the total number of overlay strings found.
4383
4384 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4385 a time. On entry into load_overlay_strings,
4386 IT->current.overlay_string_index gives the number of overlay
4387 strings that have already been loaded by previous calls to this
4388 function.
4389
4390 IT->add_overlay_start contains an additional overlay start
4391 position to consider for taking overlay strings from, if non-zero.
4392 This position comes into play when the overlay has an `invisible'
4393 property, and both before and after-strings. When we've skipped to
4394 the end of the overlay, because of its `invisible' property, we
4395 nevertheless want its before-string to appear.
4396 IT->add_overlay_start will contain the overlay start position
4397 in this case.
4398
4399 Overlay strings are sorted so that after-string strings come in
4400 front of before-string strings. Within before and after-strings,
4401 strings are sorted by overlay priority. See also function
4402 compare_overlay_entries. */
4403
4404 static void
4405 load_overlay_strings (it, charpos)
4406 struct it *it;
4407 int charpos;
4408 {
4409 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4410 Lisp_Object overlay, window, str, invisible;
4411 struct Lisp_Overlay *ov;
4412 int start, end;
4413 int size = 20;
4414 int n = 0, i, j, invis_p;
4415 struct overlay_entry *entries
4416 = (struct overlay_entry *) alloca (size * sizeof *entries);
4417
4418 if (charpos <= 0)
4419 charpos = IT_CHARPOS (*it);
4420
4421 /* Append the overlay string STRING of overlay OVERLAY to vector
4422 `entries' which has size `size' and currently contains `n'
4423 elements. AFTER_P non-zero means STRING is an after-string of
4424 OVERLAY. */
4425 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4426 do \
4427 { \
4428 Lisp_Object priority; \
4429 \
4430 if (n == size) \
4431 { \
4432 int new_size = 2 * size; \
4433 struct overlay_entry *old = entries; \
4434 entries = \
4435 (struct overlay_entry *) alloca (new_size \
4436 * sizeof *entries); \
4437 bcopy (old, entries, size * sizeof *entries); \
4438 size = new_size; \
4439 } \
4440 \
4441 entries[n].string = (STRING); \
4442 entries[n].overlay = (OVERLAY); \
4443 priority = Foverlay_get ((OVERLAY), Qpriority); \
4444 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4445 entries[n].after_string_p = (AFTER_P); \
4446 ++n; \
4447 } \
4448 while (0)
4449
4450 /* Process overlay before the overlay center. */
4451 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4452 {
4453 XSETMISC (overlay, ov);
4454 xassert (OVERLAYP (overlay));
4455 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4456 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4457
4458 if (end < charpos)
4459 break;
4460
4461 /* Skip this overlay if it doesn't start or end at IT's current
4462 position. */
4463 if (end != charpos && start != charpos)
4464 continue;
4465
4466 /* Skip this overlay if it doesn't apply to IT->w. */
4467 window = Foverlay_get (overlay, Qwindow);
4468 if (WINDOWP (window) && XWINDOW (window) != it->w)
4469 continue;
4470
4471 /* If the text ``under'' the overlay is invisible, both before-
4472 and after-strings from this overlay are visible; start and
4473 end position are indistinguishable. */
4474 invisible = Foverlay_get (overlay, Qinvisible);
4475 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4476
4477 /* If overlay has a non-empty before-string, record it. */
4478 if ((start == charpos || (end == charpos && invis_p))
4479 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4480 && SCHARS (str))
4481 RECORD_OVERLAY_STRING (overlay, str, 0);
4482
4483 /* If overlay has a non-empty after-string, record it. */
4484 if ((end == charpos || (start == charpos && invis_p))
4485 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4486 && SCHARS (str))
4487 RECORD_OVERLAY_STRING (overlay, str, 1);
4488 }
4489
4490 /* Process overlays after the overlay center. */
4491 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4492 {
4493 XSETMISC (overlay, ov);
4494 xassert (OVERLAYP (overlay));
4495 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4496 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4497
4498 if (start > charpos)
4499 break;
4500
4501 /* Skip this overlay if it doesn't start or end at IT's current
4502 position. */
4503 if (end != charpos && start != charpos)
4504 continue;
4505
4506 /* Skip this overlay if it doesn't apply to IT->w. */
4507 window = Foverlay_get (overlay, Qwindow);
4508 if (WINDOWP (window) && XWINDOW (window) != it->w)
4509 continue;
4510
4511 /* If the text ``under'' the overlay is invisible, it has a zero
4512 dimension, and both before- and after-strings apply. */
4513 invisible = Foverlay_get (overlay, Qinvisible);
4514 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4515
4516 /* If overlay has a non-empty before-string, record it. */
4517 if ((start == charpos || (end == charpos && invis_p))
4518 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4519 && SCHARS (str))
4520 RECORD_OVERLAY_STRING (overlay, str, 0);
4521
4522 /* If overlay has a non-empty after-string, record it. */
4523 if ((end == charpos || (start == charpos && invis_p))
4524 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4525 && SCHARS (str))
4526 RECORD_OVERLAY_STRING (overlay, str, 1);
4527 }
4528
4529 #undef RECORD_OVERLAY_STRING
4530
4531 /* Sort entries. */
4532 if (n > 1)
4533 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4534
4535 /* Record the total number of strings to process. */
4536 it->n_overlay_strings = n;
4537
4538 /* IT->current.overlay_string_index is the number of overlay strings
4539 that have already been consumed by IT. Copy some of the
4540 remaining overlay strings to IT->overlay_strings. */
4541 i = 0;
4542 j = it->current.overlay_string_index;
4543 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4544 it->overlay_strings[i++] = entries[j++].string;
4545
4546 CHECK_IT (it);
4547 }
4548
4549
4550 /* Get the first chunk of overlay strings at IT's current buffer
4551 position, or at CHARPOS if that is > 0. Value is non-zero if at
4552 least one overlay string was found. */
4553
4554 static int
4555 get_overlay_strings (it, charpos)
4556 struct it *it;
4557 int charpos;
4558 {
4559 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4560 process. This fills IT->overlay_strings with strings, and sets
4561 IT->n_overlay_strings to the total number of strings to process.
4562 IT->pos.overlay_string_index has to be set temporarily to zero
4563 because load_overlay_strings needs this; it must be set to -1
4564 when no overlay strings are found because a zero value would
4565 indicate a position in the first overlay string. */
4566 it->current.overlay_string_index = 0;
4567 load_overlay_strings (it, charpos);
4568
4569 /* If we found overlay strings, set up IT to deliver display
4570 elements from the first one. Otherwise set up IT to deliver
4571 from current_buffer. */
4572 if (it->n_overlay_strings)
4573 {
4574 /* Make sure we know settings in current_buffer, so that we can
4575 restore meaningful values when we're done with the overlay
4576 strings. */
4577 compute_stop_pos (it);
4578 xassert (it->face_id >= 0);
4579
4580 /* Save IT's settings. They are restored after all overlay
4581 strings have been processed. */
4582 xassert (it->sp == 0);
4583 push_it (it);
4584
4585 /* Set up IT to deliver display elements from the first overlay
4586 string. */
4587 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4588 it->string = it->overlay_strings[0];
4589 it->stop_charpos = 0;
4590 xassert (STRINGP (it->string));
4591 it->end_charpos = SCHARS (it->string);
4592 it->multibyte_p = STRING_MULTIBYTE (it->string);
4593 it->method = GET_FROM_STRING;
4594 }
4595 else
4596 {
4597 it->string = Qnil;
4598 it->current.overlay_string_index = -1;
4599 it->method = GET_FROM_BUFFER;
4600 }
4601
4602 CHECK_IT (it);
4603
4604 /* Value is non-zero if we found at least one overlay string. */
4605 return STRINGP (it->string);
4606 }
4607
4608
4609 \f
4610 /***********************************************************************
4611 Saving and restoring state
4612 ***********************************************************************/
4613
4614 /* Save current settings of IT on IT->stack. Called, for example,
4615 before setting up IT for an overlay string, to be able to restore
4616 IT's settings to what they were after the overlay string has been
4617 processed. */
4618
4619 static void
4620 push_it (it)
4621 struct it *it;
4622 {
4623 struct iterator_stack_entry *p;
4624
4625 xassert (it->sp < 2);
4626 p = it->stack + it->sp;
4627
4628 p->stop_charpos = it->stop_charpos;
4629 xassert (it->face_id >= 0);
4630 p->face_id = it->face_id;
4631 p->string = it->string;
4632 p->pos = it->current;
4633 p->end_charpos = it->end_charpos;
4634 p->string_nchars = it->string_nchars;
4635 p->area = it->area;
4636 p->multibyte_p = it->multibyte_p;
4637 p->slice = it->slice;
4638 p->space_width = it->space_width;
4639 p->font_height = it->font_height;
4640 p->voffset = it->voffset;
4641 p->string_from_display_prop_p = it->string_from_display_prop_p;
4642 p->display_ellipsis_p = 0;
4643 ++it->sp;
4644 }
4645
4646
4647 /* Restore IT's settings from IT->stack. Called, for example, when no
4648 more overlay strings must be processed, and we return to delivering
4649 display elements from a buffer, or when the end of a string from a
4650 `display' property is reached and we return to delivering display
4651 elements from an overlay string, or from a buffer. */
4652
4653 static void
4654 pop_it (it)
4655 struct it *it;
4656 {
4657 struct iterator_stack_entry *p;
4658
4659 xassert (it->sp > 0);
4660 --it->sp;
4661 p = it->stack + it->sp;
4662 it->stop_charpos = p->stop_charpos;
4663 it->face_id = p->face_id;
4664 it->string = p->string;
4665 it->current = p->pos;
4666 it->end_charpos = p->end_charpos;
4667 it->string_nchars = p->string_nchars;
4668 it->area = p->area;
4669 it->multibyte_p = p->multibyte_p;
4670 it->slice = p->slice;
4671 it->space_width = p->space_width;
4672 it->font_height = p->font_height;
4673 it->voffset = p->voffset;
4674 it->string_from_display_prop_p = p->string_from_display_prop_p;
4675 }
4676
4677
4678 \f
4679 /***********************************************************************
4680 Moving over lines
4681 ***********************************************************************/
4682
4683 /* Set IT's current position to the previous line start. */
4684
4685 static void
4686 back_to_previous_line_start (it)
4687 struct it *it;
4688 {
4689 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4690 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4691 }
4692
4693
4694 /* Move IT to the next line start.
4695
4696 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4697 we skipped over part of the text (as opposed to moving the iterator
4698 continuously over the text). Otherwise, don't change the value
4699 of *SKIPPED_P.
4700
4701 Newlines may come from buffer text, overlay strings, or strings
4702 displayed via the `display' property. That's the reason we can't
4703 simply use find_next_newline_no_quit.
4704
4705 Note that this function may not skip over invisible text that is so
4706 because of text properties and immediately follows a newline. If
4707 it would, function reseat_at_next_visible_line_start, when called
4708 from set_iterator_to_next, would effectively make invisible
4709 characters following a newline part of the wrong glyph row, which
4710 leads to wrong cursor motion. */
4711
4712 static int
4713 forward_to_next_line_start (it, skipped_p)
4714 struct it *it;
4715 int *skipped_p;
4716 {
4717 int old_selective, newline_found_p, n;
4718 const int MAX_NEWLINE_DISTANCE = 500;
4719
4720 /* If already on a newline, just consume it to avoid unintended
4721 skipping over invisible text below. */
4722 if (it->what == IT_CHARACTER
4723 && it->c == '\n'
4724 && CHARPOS (it->position) == IT_CHARPOS (*it))
4725 {
4726 set_iterator_to_next (it, 0);
4727 it->c = 0;
4728 return 1;
4729 }
4730
4731 /* Don't handle selective display in the following. It's (a)
4732 unnecessary because it's done by the caller, and (b) leads to an
4733 infinite recursion because next_element_from_ellipsis indirectly
4734 calls this function. */
4735 old_selective = it->selective;
4736 it->selective = 0;
4737
4738 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4739 from buffer text. */
4740 for (n = newline_found_p = 0;
4741 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4742 n += STRINGP (it->string) ? 0 : 1)
4743 {
4744 if (!get_next_display_element (it))
4745 return 0;
4746 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4747 set_iterator_to_next (it, 0);
4748 }
4749
4750 /* If we didn't find a newline near enough, see if we can use a
4751 short-cut. */
4752 if (!newline_found_p)
4753 {
4754 int start = IT_CHARPOS (*it);
4755 int limit = find_next_newline_no_quit (start, 1);
4756 Lisp_Object pos;
4757
4758 xassert (!STRINGP (it->string));
4759
4760 /* If there isn't any `display' property in sight, and no
4761 overlays, we can just use the position of the newline in
4762 buffer text. */
4763 if (it->stop_charpos >= limit
4764 || ((pos = Fnext_single_property_change (make_number (start),
4765 Qdisplay,
4766 Qnil, make_number (limit)),
4767 NILP (pos))
4768 && next_overlay_change (start) == ZV))
4769 {
4770 IT_CHARPOS (*it) = limit;
4771 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4772 *skipped_p = newline_found_p = 1;
4773 }
4774 else
4775 {
4776 while (get_next_display_element (it)
4777 && !newline_found_p)
4778 {
4779 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4780 set_iterator_to_next (it, 0);
4781 }
4782 }
4783 }
4784
4785 it->selective = old_selective;
4786 return newline_found_p;
4787 }
4788
4789
4790 /* Set IT's current position to the previous visible line start. Skip
4791 invisible text that is so either due to text properties or due to
4792 selective display. Caution: this does not change IT->current_x and
4793 IT->hpos. */
4794
4795 static void
4796 back_to_previous_visible_line_start (it)
4797 struct it *it;
4798 {
4799 while (IT_CHARPOS (*it) > BEGV)
4800 {
4801 back_to_previous_line_start (it);
4802 if (IT_CHARPOS (*it) <= BEGV)
4803 break;
4804
4805 /* If selective > 0, then lines indented more than that values
4806 are invisible. */
4807 if (it->selective > 0
4808 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4809 (double) it->selective)) /* iftc */
4810 continue;
4811
4812 /* Check the newline before point for invisibility. */
4813 {
4814 Lisp_Object prop;
4815 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4816 Qinvisible, it->window);
4817 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4818 continue;
4819 }
4820
4821 /* If newline has a display property that replaces the newline with something
4822 else (image or text), find start of overlay or interval and continue search
4823 from that point. */
4824 if (IT_CHARPOS (*it) > BEGV)
4825 {
4826 struct it it2 = *it;
4827 int pos;
4828 int beg, end;
4829 Lisp_Object val, overlay;
4830
4831 pos = --IT_CHARPOS (it2);
4832 --IT_BYTEPOS (it2);
4833 it2.sp = 0;
4834 if (handle_display_prop (&it2) == HANDLED_RETURN
4835 && !NILP (val = get_char_property_and_overlay
4836 (make_number (pos), Qdisplay, Qnil, &overlay))
4837 && (OVERLAYP (overlay)
4838 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4839 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4840 {
4841 if (beg < BEGV)
4842 beg = BEGV;
4843 IT_CHARPOS (*it) = beg;
4844 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4845 continue;
4846 }
4847 }
4848
4849 break;
4850 }
4851
4852 xassert (IT_CHARPOS (*it) >= BEGV);
4853 xassert (IT_CHARPOS (*it) == BEGV
4854 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4855 CHECK_IT (it);
4856 }
4857
4858
4859 /* Reseat iterator IT at the previous visible line start. Skip
4860 invisible text that is so either due to text properties or due to
4861 selective display. At the end, update IT's overlay information,
4862 face information etc. */
4863
4864 void
4865 reseat_at_previous_visible_line_start (it)
4866 struct it *it;
4867 {
4868 back_to_previous_visible_line_start (it);
4869 reseat (it, it->current.pos, 1);
4870 CHECK_IT (it);
4871 }
4872
4873
4874 /* Reseat iterator IT on the next visible line start in the current
4875 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4876 preceding the line start. Skip over invisible text that is so
4877 because of selective display. Compute faces, overlays etc at the
4878 new position. Note that this function does not skip over text that
4879 is invisible because of text properties. */
4880
4881 static void
4882 reseat_at_next_visible_line_start (it, on_newline_p)
4883 struct it *it;
4884 int on_newline_p;
4885 {
4886 int newline_found_p, skipped_p = 0;
4887
4888 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4889
4890 /* Skip over lines that are invisible because they are indented
4891 more than the value of IT->selective. */
4892 if (it->selective > 0)
4893 while (IT_CHARPOS (*it) < ZV
4894 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4895 (double) it->selective)) /* iftc */
4896 {
4897 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4898 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4899 }
4900
4901 /* Position on the newline if that's what's requested. */
4902 if (on_newline_p && newline_found_p)
4903 {
4904 if (STRINGP (it->string))
4905 {
4906 if (IT_STRING_CHARPOS (*it) > 0)
4907 {
4908 --IT_STRING_CHARPOS (*it);
4909 --IT_STRING_BYTEPOS (*it);
4910 }
4911 }
4912 else if (IT_CHARPOS (*it) > BEGV)
4913 {
4914 --IT_CHARPOS (*it);
4915 --IT_BYTEPOS (*it);
4916 reseat (it, it->current.pos, 0);
4917 }
4918 }
4919 else if (skipped_p)
4920 reseat (it, it->current.pos, 0);
4921
4922 CHECK_IT (it);
4923 }
4924
4925
4926 \f
4927 /***********************************************************************
4928 Changing an iterator's position
4929 ***********************************************************************/
4930
4931 /* Change IT's current position to POS in current_buffer. If FORCE_P
4932 is non-zero, always check for text properties at the new position.
4933 Otherwise, text properties are only looked up if POS >=
4934 IT->check_charpos of a property. */
4935
4936 static void
4937 reseat (it, pos, force_p)
4938 struct it *it;
4939 struct text_pos pos;
4940 int force_p;
4941 {
4942 int original_pos = IT_CHARPOS (*it);
4943
4944 reseat_1 (it, pos, 0);
4945
4946 /* Determine where to check text properties. Avoid doing it
4947 where possible because text property lookup is very expensive. */
4948 if (force_p
4949 || CHARPOS (pos) > it->stop_charpos
4950 || CHARPOS (pos) < original_pos)
4951 handle_stop (it);
4952
4953 CHECK_IT (it);
4954 }
4955
4956
4957 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4958 IT->stop_pos to POS, also. */
4959
4960 static void
4961 reseat_1 (it, pos, set_stop_p)
4962 struct it *it;
4963 struct text_pos pos;
4964 int set_stop_p;
4965 {
4966 /* Don't call this function when scanning a C string. */
4967 xassert (it->s == NULL);
4968
4969 /* POS must be a reasonable value. */
4970 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4971
4972 it->current.pos = it->position = pos;
4973 XSETBUFFER (it->object, current_buffer);
4974 it->end_charpos = ZV;
4975 it->dpvec = NULL;
4976 it->current.dpvec_index = -1;
4977 it->current.overlay_string_index = -1;
4978 IT_STRING_CHARPOS (*it) = -1;
4979 IT_STRING_BYTEPOS (*it) = -1;
4980 it->string = Qnil;
4981 it->method = GET_FROM_BUFFER;
4982 /* RMS: I added this to fix a bug in move_it_vertically_backward
4983 where it->area continued to relate to the starting point
4984 for the backward motion. Bug report from
4985 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4986 However, I am not sure whether reseat still does the right thing
4987 in general after this change. */
4988 it->area = TEXT_AREA;
4989 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4990 it->sp = 0;
4991 it->face_before_selective_p = 0;
4992
4993 if (set_stop_p)
4994 it->stop_charpos = CHARPOS (pos);
4995 }
4996
4997
4998 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4999 If S is non-null, it is a C string to iterate over. Otherwise,
5000 STRING gives a Lisp string to iterate over.
5001
5002 If PRECISION > 0, don't return more then PRECISION number of
5003 characters from the string.
5004
5005 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5006 characters have been returned. FIELD_WIDTH < 0 means an infinite
5007 field width.
5008
5009 MULTIBYTE = 0 means disable processing of multibyte characters,
5010 MULTIBYTE > 0 means enable it,
5011 MULTIBYTE < 0 means use IT->multibyte_p.
5012
5013 IT must be initialized via a prior call to init_iterator before
5014 calling this function. */
5015
5016 static void
5017 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5018 struct it *it;
5019 unsigned char *s;
5020 Lisp_Object string;
5021 int charpos;
5022 int precision, field_width, multibyte;
5023 {
5024 /* No region in strings. */
5025 it->region_beg_charpos = it->region_end_charpos = -1;
5026
5027 /* No text property checks performed by default, but see below. */
5028 it->stop_charpos = -1;
5029
5030 /* Set iterator position and end position. */
5031 bzero (&it->current, sizeof it->current);
5032 it->current.overlay_string_index = -1;
5033 it->current.dpvec_index = -1;
5034 xassert (charpos >= 0);
5035
5036 /* If STRING is specified, use its multibyteness, otherwise use the
5037 setting of MULTIBYTE, if specified. */
5038 if (multibyte >= 0)
5039 it->multibyte_p = multibyte > 0;
5040
5041 if (s == NULL)
5042 {
5043 xassert (STRINGP (string));
5044 it->string = string;
5045 it->s = NULL;
5046 it->end_charpos = it->string_nchars = SCHARS (string);
5047 it->method = GET_FROM_STRING;
5048 it->current.string_pos = string_pos (charpos, string);
5049 }
5050 else
5051 {
5052 it->s = s;
5053 it->string = Qnil;
5054
5055 /* Note that we use IT->current.pos, not it->current.string_pos,
5056 for displaying C strings. */
5057 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5058 if (it->multibyte_p)
5059 {
5060 it->current.pos = c_string_pos (charpos, s, 1);
5061 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5062 }
5063 else
5064 {
5065 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5066 it->end_charpos = it->string_nchars = strlen (s);
5067 }
5068
5069 it->method = GET_FROM_C_STRING;
5070 }
5071
5072 /* PRECISION > 0 means don't return more than PRECISION characters
5073 from the string. */
5074 if (precision > 0 && it->end_charpos - charpos > precision)
5075 it->end_charpos = it->string_nchars = charpos + precision;
5076
5077 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5078 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5079 FIELD_WIDTH < 0 means infinite field width. This is useful for
5080 padding with `-' at the end of a mode line. */
5081 if (field_width < 0)
5082 field_width = INFINITY;
5083 if (field_width > it->end_charpos - charpos)
5084 it->end_charpos = charpos + field_width;
5085
5086 /* Use the standard display table for displaying strings. */
5087 if (DISP_TABLE_P (Vstandard_display_table))
5088 it->dp = XCHAR_TABLE (Vstandard_display_table);
5089
5090 it->stop_charpos = charpos;
5091 CHECK_IT (it);
5092 }
5093
5094
5095 \f
5096 /***********************************************************************
5097 Iteration
5098 ***********************************************************************/
5099
5100 /* Map enum it_method value to corresponding next_element_from_* function. */
5101
5102 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5103 {
5104 next_element_from_buffer,
5105 next_element_from_display_vector,
5106 next_element_from_composition,
5107 next_element_from_string,
5108 next_element_from_c_string,
5109 next_element_from_image,
5110 next_element_from_stretch
5111 };
5112
5113
5114 /* Load IT's display element fields with information about the next
5115 display element from the current position of IT. Value is zero if
5116 end of buffer (or C string) is reached. */
5117
5118 int
5119 get_next_display_element (it)
5120 struct it *it;
5121 {
5122 /* Non-zero means that we found a display element. Zero means that
5123 we hit the end of what we iterate over. Performance note: the
5124 function pointer `method' used here turns out to be faster than
5125 using a sequence of if-statements. */
5126 int success_p;
5127
5128 get_next:
5129 success_p = (*get_next_element[it->method]) (it);
5130
5131 if (it->what == IT_CHARACTER)
5132 {
5133 /* Map via display table or translate control characters.
5134 IT->c, IT->len etc. have been set to the next character by
5135 the function call above. If we have a display table, and it
5136 contains an entry for IT->c, translate it. Don't do this if
5137 IT->c itself comes from a display table, otherwise we could
5138 end up in an infinite recursion. (An alternative could be to
5139 count the recursion depth of this function and signal an
5140 error when a certain maximum depth is reached.) Is it worth
5141 it? */
5142 if (success_p && it->dpvec == NULL)
5143 {
5144 Lisp_Object dv;
5145
5146 if (it->dp
5147 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5148 VECTORP (dv)))
5149 {
5150 struct Lisp_Vector *v = XVECTOR (dv);
5151
5152 /* Return the first character from the display table
5153 entry, if not empty. If empty, don't display the
5154 current character. */
5155 if (v->size)
5156 {
5157 it->dpvec_char_len = it->len;
5158 it->dpvec = v->contents;
5159 it->dpend = v->contents + v->size;
5160 it->current.dpvec_index = 0;
5161 it->dpvec_face_id = -1;
5162 it->saved_face_id = it->face_id;
5163 it->method = GET_FROM_DISPLAY_VECTOR;
5164 it->ellipsis_p = 0;
5165 }
5166 else
5167 {
5168 set_iterator_to_next (it, 0);
5169 }
5170 goto get_next;
5171 }
5172
5173 /* Translate control characters into `\003' or `^C' form.
5174 Control characters coming from a display table entry are
5175 currently not translated because we use IT->dpvec to hold
5176 the translation. This could easily be changed but I
5177 don't believe that it is worth doing.
5178
5179 If it->multibyte_p is nonzero, non-printable non-ASCII
5180 characters are also translated to octal form.
5181
5182 If it->multibyte_p is zero, eight-bit characters that
5183 don't have corresponding multibyte char code are also
5184 translated to octal form. */
5185 else if ((it->c < ' '
5186 ? (it->area != TEXT_AREA
5187 /* In mode line, treat \n, \t like other crl chars. */
5188 || (it->c != '\t'
5189 && it->glyph_row && it->glyph_row->mode_line_p)
5190 || (it->c != '\n' && it->c != '\t'))
5191 : (it->multibyte_p
5192 ? (!CHAR_PRINTABLE_P (it->c)
5193 || (!NILP (Vnobreak_char_display)
5194 && (it->c == 0xA0 /* NO-BREAK SPACE */
5195 || it->c == 0xAD /* SOFT HYPHEN */)))
5196 : (it->c >= 127
5197 && (! unibyte_display_via_language_environment
5198 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5199 {
5200 /* IT->c is a control character which must be displayed
5201 either as '\003' or as `^C' where the '\\' and '^'
5202 can be defined in the display table. Fill
5203 IT->ctl_chars with glyphs for what we have to
5204 display. Then, set IT->dpvec to these glyphs. */
5205 GLYPH g;
5206 int ctl_len;
5207 int face_id, lface_id = 0 ;
5208 GLYPH escape_glyph;
5209
5210 /* Handle control characters with ^. */
5211
5212 if (it->c < 128 && it->ctl_arrow_p)
5213 {
5214 g = '^'; /* default glyph for Control */
5215 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5216 if (it->dp
5217 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5218 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5219 {
5220 g = XINT (DISP_CTRL_GLYPH (it->dp));
5221 lface_id = FAST_GLYPH_FACE (g);
5222 }
5223 if (lface_id)
5224 {
5225 g = FAST_GLYPH_CHAR (g);
5226 face_id = merge_faces (it->f, Qt, lface_id,
5227 it->face_id);
5228 }
5229 else
5230 {
5231 /* Merge the escape-glyph face into the current face. */
5232 face_id = merge_faces (it->f, Qescape_glyph, 0,
5233 it->face_id);
5234 }
5235
5236 XSETINT (it->ctl_chars[0], g);
5237 g = it->c ^ 0100;
5238 XSETINT (it->ctl_chars[1], g);
5239 ctl_len = 2;
5240 goto display_control;
5241 }
5242
5243 /* Handle non-break space in the mode where it only gets
5244 highlighting. */
5245
5246 if (EQ (Vnobreak_char_display, Qt)
5247 && it->c == 0xA0)
5248 {
5249 /* Merge the no-break-space face into the current face. */
5250 face_id = merge_faces (it->f, Qnobreak_space, 0,
5251 it->face_id);
5252
5253 g = it->c = ' ';
5254 XSETINT (it->ctl_chars[0], g);
5255 ctl_len = 1;
5256 goto display_control;
5257 }
5258
5259 /* Handle sequences that start with the "escape glyph". */
5260
5261 /* the default escape glyph is \. */
5262 escape_glyph = '\\';
5263
5264 if (it->dp
5265 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5266 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5267 {
5268 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5269 lface_id = FAST_GLYPH_FACE (escape_glyph);
5270 }
5271 if (lface_id)
5272 {
5273 /* The display table specified a face.
5274 Merge it into face_id and also into escape_glyph. */
5275 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5276 face_id = merge_faces (it->f, Qt, lface_id,
5277 it->face_id);
5278 }
5279 else
5280 {
5281 /* Merge the escape-glyph face into the current face. */
5282 face_id = merge_faces (it->f, Qescape_glyph, 0,
5283 it->face_id);
5284 }
5285
5286 /* Handle soft hyphens in the mode where they only get
5287 highlighting. */
5288
5289 if (EQ (Vnobreak_char_display, Qt)
5290 && it->c == 0xAD)
5291 {
5292 g = it->c = '-';
5293 XSETINT (it->ctl_chars[0], g);
5294 ctl_len = 1;
5295 goto display_control;
5296 }
5297
5298 /* Handle non-break space and soft hyphen
5299 with the escape glyph. */
5300
5301 if (it->c == 0xA0 || it->c == 0xAD)
5302 {
5303 XSETINT (it->ctl_chars[0], escape_glyph);
5304 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5305 XSETINT (it->ctl_chars[1], g);
5306 ctl_len = 2;
5307 goto display_control;
5308 }
5309
5310 {
5311 unsigned char str[MAX_MULTIBYTE_LENGTH];
5312 int len;
5313 int i;
5314
5315 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5316 if (CHAR_BYTE8_P (it->c))
5317 {
5318 str[0] = CHAR_TO_BYTE8 (it->c);
5319 len = 1;
5320 }
5321 else if (it->c < 256)
5322 {
5323 str[0] = it->c;
5324 len = 1;
5325 }
5326 else
5327 {
5328 /* It's an invalid character, which shouldn't
5329 happen actually, but due to bugs it may
5330 happen. Let's print the char as is, there's
5331 not much meaningful we can do with it. */
5332 str[0] = it->c;
5333 str[1] = it->c >> 8;
5334 str[2] = it->c >> 16;
5335 str[3] = it->c >> 24;
5336 len = 4;
5337 }
5338
5339 for (i = 0; i < len; i++)
5340 {
5341 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5342 /* Insert three more glyphs into IT->ctl_chars for
5343 the octal display of the character. */
5344 g = ((str[i] >> 6) & 7) + '0';
5345 XSETINT (it->ctl_chars[i * 4 + 1], g);
5346 g = ((str[i] >> 3) & 7) + '0';
5347 XSETINT (it->ctl_chars[i * 4 + 2], g);
5348 g = (str[i] & 7) + '0';
5349 XSETINT (it->ctl_chars[i * 4 + 3], g);
5350 }
5351 ctl_len = len * 4;
5352 }
5353
5354 display_control:
5355 /* Set up IT->dpvec and return first character from it. */
5356 it->dpvec_char_len = it->len;
5357 it->dpvec = it->ctl_chars;
5358 it->dpend = it->dpvec + ctl_len;
5359 it->current.dpvec_index = 0;
5360 it->dpvec_face_id = face_id;
5361 it->saved_face_id = it->face_id;
5362 it->method = GET_FROM_DISPLAY_VECTOR;
5363 it->ellipsis_p = 0;
5364 goto get_next;
5365 }
5366 }
5367
5368 /* Adjust face id for a multibyte character. There are no
5369 multibyte character in unibyte text. */
5370 if (it->multibyte_p
5371 && success_p
5372 && FRAME_WINDOW_P (it->f))
5373 {
5374 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5375 int pos = (it->s ? -1
5376 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5377 : IT_CHARPOS (*it));
5378
5379 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5380 }
5381 }
5382
5383 /* Is this character the last one of a run of characters with
5384 box? If yes, set IT->end_of_box_run_p to 1. */
5385 if (it->face_box_p
5386 && it->s == NULL)
5387 {
5388 int face_id;
5389 struct face *face;
5390
5391 it->end_of_box_run_p
5392 = ((face_id = face_after_it_pos (it),
5393 face_id != it->face_id)
5394 && (face = FACE_FROM_ID (it->f, face_id),
5395 face->box == FACE_NO_BOX));
5396 }
5397
5398 /* Value is 0 if end of buffer or string reached. */
5399 return success_p;
5400 }
5401
5402
5403 /* Move IT to the next display element.
5404
5405 RESEAT_P non-zero means if called on a newline in buffer text,
5406 skip to the next visible line start.
5407
5408 Functions get_next_display_element and set_iterator_to_next are
5409 separate because I find this arrangement easier to handle than a
5410 get_next_display_element function that also increments IT's
5411 position. The way it is we can first look at an iterator's current
5412 display element, decide whether it fits on a line, and if it does,
5413 increment the iterator position. The other way around we probably
5414 would either need a flag indicating whether the iterator has to be
5415 incremented the next time, or we would have to implement a
5416 decrement position function which would not be easy to write. */
5417
5418 void
5419 set_iterator_to_next (it, reseat_p)
5420 struct it *it;
5421 int reseat_p;
5422 {
5423 /* Reset flags indicating start and end of a sequence of characters
5424 with box. Reset them at the start of this function because
5425 moving the iterator to a new position might set them. */
5426 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5427
5428 switch (it->method)
5429 {
5430 case GET_FROM_BUFFER:
5431 /* The current display element of IT is a character from
5432 current_buffer. Advance in the buffer, and maybe skip over
5433 invisible lines that are so because of selective display. */
5434 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5435 reseat_at_next_visible_line_start (it, 0);
5436 else
5437 {
5438 xassert (it->len != 0);
5439 IT_BYTEPOS (*it) += it->len;
5440 IT_CHARPOS (*it) += 1;
5441 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5442 }
5443 break;
5444
5445 case GET_FROM_COMPOSITION:
5446 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5447 if (STRINGP (it->string))
5448 {
5449 IT_STRING_BYTEPOS (*it) += it->len;
5450 IT_STRING_CHARPOS (*it) += it->cmp_len;
5451 it->method = GET_FROM_STRING;
5452 goto consider_string_end;
5453 }
5454 else
5455 {
5456 IT_BYTEPOS (*it) += it->len;
5457 IT_CHARPOS (*it) += it->cmp_len;
5458 it->method = GET_FROM_BUFFER;
5459 }
5460 break;
5461
5462 case GET_FROM_C_STRING:
5463 /* Current display element of IT is from a C string. */
5464 IT_BYTEPOS (*it) += it->len;
5465 IT_CHARPOS (*it) += 1;
5466 break;
5467
5468 case GET_FROM_DISPLAY_VECTOR:
5469 /* Current display element of IT is from a display table entry.
5470 Advance in the display table definition. Reset it to null if
5471 end reached, and continue with characters from buffers/
5472 strings. */
5473 ++it->current.dpvec_index;
5474
5475 /* Restore face of the iterator to what they were before the
5476 display vector entry (these entries may contain faces). */
5477 it->face_id = it->saved_face_id;
5478
5479 if (it->dpvec + it->current.dpvec_index == it->dpend)
5480 {
5481 if (it->s)
5482 it->method = GET_FROM_C_STRING;
5483 else if (STRINGP (it->string))
5484 it->method = GET_FROM_STRING;
5485 else
5486 it->method = GET_FROM_BUFFER;
5487
5488 it->dpvec = NULL;
5489 it->current.dpvec_index = -1;
5490
5491 /* Skip over characters which were displayed via IT->dpvec. */
5492 if (it->dpvec_char_len < 0)
5493 reseat_at_next_visible_line_start (it, 1);
5494 else if (it->dpvec_char_len > 0)
5495 {
5496 it->len = it->dpvec_char_len;
5497 set_iterator_to_next (it, reseat_p);
5498 }
5499
5500 /* Recheck faces after display vector */
5501 it->stop_charpos = IT_CHARPOS (*it);
5502 }
5503 break;
5504
5505 case GET_FROM_STRING:
5506 /* Current display element is a character from a Lisp string. */
5507 xassert (it->s == NULL && STRINGP (it->string));
5508 IT_STRING_BYTEPOS (*it) += it->len;
5509 IT_STRING_CHARPOS (*it) += 1;
5510
5511 consider_string_end:
5512
5513 if (it->current.overlay_string_index >= 0)
5514 {
5515 /* IT->string is an overlay string. Advance to the
5516 next, if there is one. */
5517 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5518 next_overlay_string (it);
5519 }
5520 else
5521 {
5522 /* IT->string is not an overlay string. If we reached
5523 its end, and there is something on IT->stack, proceed
5524 with what is on the stack. This can be either another
5525 string, this time an overlay string, or a buffer. */
5526 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5527 && it->sp > 0)
5528 {
5529 pop_it (it);
5530 if (STRINGP (it->string))
5531 goto consider_string_end;
5532 it->method = GET_FROM_BUFFER;
5533 }
5534 }
5535 break;
5536
5537 case GET_FROM_IMAGE:
5538 case GET_FROM_STRETCH:
5539 /* The position etc with which we have to proceed are on
5540 the stack. The position may be at the end of a string,
5541 if the `display' property takes up the whole string. */
5542 xassert (it->sp > 0);
5543 pop_it (it);
5544 it->image_id = 0;
5545 if (STRINGP (it->string))
5546 {
5547 it->method = GET_FROM_STRING;
5548 goto consider_string_end;
5549 }
5550 it->method = GET_FROM_BUFFER;
5551 break;
5552
5553 default:
5554 /* There are no other methods defined, so this should be a bug. */
5555 abort ();
5556 }
5557
5558 xassert (it->method != GET_FROM_STRING
5559 || (STRINGP (it->string)
5560 && IT_STRING_CHARPOS (*it) >= 0));
5561 }
5562
5563 /* Load IT's display element fields with information about the next
5564 display element which comes from a display table entry or from the
5565 result of translating a control character to one of the forms `^C'
5566 or `\003'.
5567
5568 IT->dpvec holds the glyphs to return as characters.
5569 IT->saved_face_id holds the face id before the display vector--
5570 it is restored into IT->face_idin set_iterator_to_next. */
5571
5572 static int
5573 next_element_from_display_vector (it)
5574 struct it *it;
5575 {
5576 /* Precondition. */
5577 xassert (it->dpvec && it->current.dpvec_index >= 0);
5578
5579 it->face_id = it->saved_face_id;
5580
5581 if (INTEGERP (*it->dpvec)
5582 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5583 {
5584 GLYPH g;
5585
5586 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5587 it->c = FAST_GLYPH_CHAR (g);
5588 it->len = CHAR_BYTES (it->c);
5589
5590 /* The entry may contain a face id to use. Such a face id is
5591 the id of a Lisp face, not a realized face. A face id of
5592 zero means no face is specified. */
5593 if (it->dpvec_face_id >= 0)
5594 it->face_id = it->dpvec_face_id;
5595 else
5596 {
5597 int lface_id = FAST_GLYPH_FACE (g);
5598 if (lface_id > 0)
5599 it->face_id = merge_faces (it->f, Qt, lface_id,
5600 it->saved_face_id);
5601 }
5602 }
5603 else
5604 /* Display table entry is invalid. Return a space. */
5605 it->c = ' ', it->len = 1;
5606
5607 /* Don't change position and object of the iterator here. They are
5608 still the values of the character that had this display table
5609 entry or was translated, and that's what we want. */
5610 it->what = IT_CHARACTER;
5611 return 1;
5612 }
5613
5614
5615 /* Load IT with the next display element from Lisp string IT->string.
5616 IT->current.string_pos is the current position within the string.
5617 If IT->current.overlay_string_index >= 0, the Lisp string is an
5618 overlay string. */
5619
5620 static int
5621 next_element_from_string (it)
5622 struct it *it;
5623 {
5624 struct text_pos position;
5625
5626 xassert (STRINGP (it->string));
5627 xassert (IT_STRING_CHARPOS (*it) >= 0);
5628 position = it->current.string_pos;
5629
5630 /* Time to check for invisible text? */
5631 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5632 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5633 {
5634 handle_stop (it);
5635
5636 /* Since a handler may have changed IT->method, we must
5637 recurse here. */
5638 return get_next_display_element (it);
5639 }
5640
5641 if (it->current.overlay_string_index >= 0)
5642 {
5643 /* Get the next character from an overlay string. In overlay
5644 strings, There is no field width or padding with spaces to
5645 do. */
5646 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5647 {
5648 it->what = IT_EOB;
5649 return 0;
5650 }
5651 else if (STRING_MULTIBYTE (it->string))
5652 {
5653 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5654 const unsigned char *s = (SDATA (it->string)
5655 + IT_STRING_BYTEPOS (*it));
5656 it->c = string_char_and_length (s, remaining, &it->len);
5657 }
5658 else
5659 {
5660 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5661 it->len = 1;
5662 }
5663 }
5664 else
5665 {
5666 /* Get the next character from a Lisp string that is not an
5667 overlay string. Such strings come from the mode line, for
5668 example. We may have to pad with spaces, or truncate the
5669 string. See also next_element_from_c_string. */
5670 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5671 {
5672 it->what = IT_EOB;
5673 return 0;
5674 }
5675 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5676 {
5677 /* Pad with spaces. */
5678 it->c = ' ', it->len = 1;
5679 CHARPOS (position) = BYTEPOS (position) = -1;
5680 }
5681 else if (STRING_MULTIBYTE (it->string))
5682 {
5683 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5684 const unsigned char *s = (SDATA (it->string)
5685 + IT_STRING_BYTEPOS (*it));
5686 it->c = string_char_and_length (s, maxlen, &it->len);
5687 }
5688 else
5689 {
5690 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5691 it->len = 1;
5692 }
5693 }
5694
5695 /* Record what we have and where it came from. Note that we store a
5696 buffer position in IT->position although it could arguably be a
5697 string position. */
5698 it->what = IT_CHARACTER;
5699 it->object = it->string;
5700 it->position = position;
5701 return 1;
5702 }
5703
5704
5705 /* Load IT with next display element from C string IT->s.
5706 IT->string_nchars is the maximum number of characters to return
5707 from the string. IT->end_charpos may be greater than
5708 IT->string_nchars when this function is called, in which case we
5709 may have to return padding spaces. Value is zero if end of string
5710 reached, including padding spaces. */
5711
5712 static int
5713 next_element_from_c_string (it)
5714 struct it *it;
5715 {
5716 int success_p = 1;
5717
5718 xassert (it->s);
5719 it->what = IT_CHARACTER;
5720 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5721 it->object = Qnil;
5722
5723 /* IT's position can be greater IT->string_nchars in case a field
5724 width or precision has been specified when the iterator was
5725 initialized. */
5726 if (IT_CHARPOS (*it) >= it->end_charpos)
5727 {
5728 /* End of the game. */
5729 it->what = IT_EOB;
5730 success_p = 0;
5731 }
5732 else if (IT_CHARPOS (*it) >= it->string_nchars)
5733 {
5734 /* Pad with spaces. */
5735 it->c = ' ', it->len = 1;
5736 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5737 }
5738 else if (it->multibyte_p)
5739 {
5740 /* Implementation note: The calls to strlen apparently aren't a
5741 performance problem because there is no noticeable performance
5742 difference between Emacs running in unibyte or multibyte mode. */
5743 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5744 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5745 maxlen, &it->len);
5746 }
5747 else
5748 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5749
5750 return success_p;
5751 }
5752
5753
5754 /* Set up IT to return characters from an ellipsis, if appropriate.
5755 The definition of the ellipsis glyphs may come from a display table
5756 entry. This function Fills IT with the first glyph from the
5757 ellipsis if an ellipsis is to be displayed. */
5758
5759 static int
5760 next_element_from_ellipsis (it)
5761 struct it *it;
5762 {
5763 if (it->selective_display_ellipsis_p)
5764 setup_for_ellipsis (it, it->len);
5765 else
5766 {
5767 /* The face at the current position may be different from the
5768 face we find after the invisible text. Remember what it
5769 was in IT->saved_face_id, and signal that it's there by
5770 setting face_before_selective_p. */
5771 it->saved_face_id = it->face_id;
5772 it->method = GET_FROM_BUFFER;
5773 reseat_at_next_visible_line_start (it, 1);
5774 it->face_before_selective_p = 1;
5775 }
5776
5777 return get_next_display_element (it);
5778 }
5779
5780
5781 /* Deliver an image display element. The iterator IT is already
5782 filled with image information (done in handle_display_prop). Value
5783 is always 1. */
5784
5785
5786 static int
5787 next_element_from_image (it)
5788 struct it *it;
5789 {
5790 it->what = IT_IMAGE;
5791 return 1;
5792 }
5793
5794
5795 /* Fill iterator IT with next display element from a stretch glyph
5796 property. IT->object is the value of the text property. Value is
5797 always 1. */
5798
5799 static int
5800 next_element_from_stretch (it)
5801 struct it *it;
5802 {
5803 it->what = IT_STRETCH;
5804 return 1;
5805 }
5806
5807
5808 /* Load IT with the next display element from current_buffer. Value
5809 is zero if end of buffer reached. IT->stop_charpos is the next
5810 position at which to stop and check for text properties or buffer
5811 end. */
5812
5813 static int
5814 next_element_from_buffer (it)
5815 struct it *it;
5816 {
5817 int success_p = 1;
5818
5819 /* Check this assumption, otherwise, we would never enter the
5820 if-statement, below. */
5821 xassert (IT_CHARPOS (*it) >= BEGV
5822 && IT_CHARPOS (*it) <= it->stop_charpos);
5823
5824 if (IT_CHARPOS (*it) >= it->stop_charpos)
5825 {
5826 if (IT_CHARPOS (*it) >= it->end_charpos)
5827 {
5828 int overlay_strings_follow_p;
5829
5830 /* End of the game, except when overlay strings follow that
5831 haven't been returned yet. */
5832 if (it->overlay_strings_at_end_processed_p)
5833 overlay_strings_follow_p = 0;
5834 else
5835 {
5836 it->overlay_strings_at_end_processed_p = 1;
5837 overlay_strings_follow_p = get_overlay_strings (it, 0);
5838 }
5839
5840 if (overlay_strings_follow_p)
5841 success_p = get_next_display_element (it);
5842 else
5843 {
5844 it->what = IT_EOB;
5845 it->position = it->current.pos;
5846 success_p = 0;
5847 }
5848 }
5849 else
5850 {
5851 handle_stop (it);
5852 return get_next_display_element (it);
5853 }
5854 }
5855 else
5856 {
5857 /* No face changes, overlays etc. in sight, so just return a
5858 character from current_buffer. */
5859 unsigned char *p;
5860
5861 /* Maybe run the redisplay end trigger hook. Performance note:
5862 This doesn't seem to cost measurable time. */
5863 if (it->redisplay_end_trigger_charpos
5864 && it->glyph_row
5865 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5866 run_redisplay_end_trigger_hook (it);
5867
5868 /* Get the next character, maybe multibyte. */
5869 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5870 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5871 {
5872 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5873 - IT_BYTEPOS (*it));
5874 it->c = string_char_and_length (p, maxlen, &it->len);
5875 }
5876 else
5877 it->c = *p, it->len = 1;
5878
5879 /* Record what we have and where it came from. */
5880 it->what = IT_CHARACTER;;
5881 it->object = it->w->buffer;
5882 it->position = it->current.pos;
5883
5884 /* Normally we return the character found above, except when we
5885 really want to return an ellipsis for selective display. */
5886 if (it->selective)
5887 {
5888 if (it->c == '\n')
5889 {
5890 /* A value of selective > 0 means hide lines indented more
5891 than that number of columns. */
5892 if (it->selective > 0
5893 && IT_CHARPOS (*it) + 1 < ZV
5894 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5895 IT_BYTEPOS (*it) + 1,
5896 (double) it->selective)) /* iftc */
5897 {
5898 success_p = next_element_from_ellipsis (it);
5899 it->dpvec_char_len = -1;
5900 }
5901 }
5902 else if (it->c == '\r' && it->selective == -1)
5903 {
5904 /* A value of selective == -1 means that everything from the
5905 CR to the end of the line is invisible, with maybe an
5906 ellipsis displayed for it. */
5907 success_p = next_element_from_ellipsis (it);
5908 it->dpvec_char_len = -1;
5909 }
5910 }
5911 }
5912
5913 /* Value is zero if end of buffer reached. */
5914 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5915 return success_p;
5916 }
5917
5918
5919 /* Run the redisplay end trigger hook for IT. */
5920
5921 static void
5922 run_redisplay_end_trigger_hook (it)
5923 struct it *it;
5924 {
5925 Lisp_Object args[3];
5926
5927 /* IT->glyph_row should be non-null, i.e. we should be actually
5928 displaying something, or otherwise we should not run the hook. */
5929 xassert (it->glyph_row);
5930
5931 /* Set up hook arguments. */
5932 args[0] = Qredisplay_end_trigger_functions;
5933 args[1] = it->window;
5934 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5935 it->redisplay_end_trigger_charpos = 0;
5936
5937 /* Since we are *trying* to run these functions, don't try to run
5938 them again, even if they get an error. */
5939 it->w->redisplay_end_trigger = Qnil;
5940 Frun_hook_with_args (3, args);
5941
5942 /* Notice if it changed the face of the character we are on. */
5943 handle_face_prop (it);
5944 }
5945
5946
5947 /* Deliver a composition display element. The iterator IT is already
5948 filled with composition information (done in
5949 handle_composition_prop). Value is always 1. */
5950
5951 static int
5952 next_element_from_composition (it)
5953 struct it *it;
5954 {
5955 it->what = IT_COMPOSITION;
5956 it->position = (STRINGP (it->string)
5957 ? it->current.string_pos
5958 : it->current.pos);
5959 return 1;
5960 }
5961
5962
5963 \f
5964 /***********************************************************************
5965 Moving an iterator without producing glyphs
5966 ***********************************************************************/
5967
5968 /* Move iterator IT to a specified buffer or X position within one
5969 line on the display without producing glyphs.
5970
5971 OP should be a bit mask including some or all of these bits:
5972 MOVE_TO_X: Stop on reaching x-position TO_X.
5973 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5974 Regardless of OP's value, stop in reaching the end of the display line.
5975
5976 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5977 This means, in particular, that TO_X includes window's horizontal
5978 scroll amount.
5979
5980 The return value has several possible values that
5981 say what condition caused the scan to stop:
5982
5983 MOVE_POS_MATCH_OR_ZV
5984 - when TO_POS or ZV was reached.
5985
5986 MOVE_X_REACHED
5987 -when TO_X was reached before TO_POS or ZV were reached.
5988
5989 MOVE_LINE_CONTINUED
5990 - when we reached the end of the display area and the line must
5991 be continued.
5992
5993 MOVE_LINE_TRUNCATED
5994 - when we reached the end of the display area and the line is
5995 truncated.
5996
5997 MOVE_NEWLINE_OR_CR
5998 - when we stopped at a line end, i.e. a newline or a CR and selective
5999 display is on. */
6000
6001 static enum move_it_result
6002 move_it_in_display_line_to (it, to_charpos, to_x, op)
6003 struct it *it;
6004 int to_charpos, to_x, op;
6005 {
6006 enum move_it_result result = MOVE_UNDEFINED;
6007 struct glyph_row *saved_glyph_row;
6008
6009 /* Don't produce glyphs in produce_glyphs. */
6010 saved_glyph_row = it->glyph_row;
6011 it->glyph_row = NULL;
6012
6013 #define BUFFER_POS_REACHED_P() \
6014 ((op & MOVE_TO_POS) != 0 \
6015 && BUFFERP (it->object) \
6016 && IT_CHARPOS (*it) >= to_charpos \
6017 && (it->method == GET_FROM_BUFFER \
6018 || (it->method == GET_FROM_DISPLAY_VECTOR \
6019 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6020
6021
6022 while (1)
6023 {
6024 int x, i, ascent = 0, descent = 0;
6025
6026 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6027 if ((op & MOVE_TO_POS) != 0
6028 && BUFFERP (it->object)
6029 && it->method == GET_FROM_BUFFER
6030 && IT_CHARPOS (*it) > to_charpos)
6031 {
6032 result = MOVE_POS_MATCH_OR_ZV;
6033 break;
6034 }
6035
6036 /* Stop when ZV reached.
6037 We used to stop here when TO_CHARPOS reached as well, but that is
6038 too soon if this glyph does not fit on this line. So we handle it
6039 explicitly below. */
6040 if (!get_next_display_element (it)
6041 || (it->truncate_lines_p
6042 && BUFFER_POS_REACHED_P ()))
6043 {
6044 result = MOVE_POS_MATCH_OR_ZV;
6045 break;
6046 }
6047
6048 /* The call to produce_glyphs will get the metrics of the
6049 display element IT is loaded with. We record in x the
6050 x-position before this display element in case it does not
6051 fit on the line. */
6052 x = it->current_x;
6053
6054 /* Remember the line height so far in case the next element doesn't
6055 fit on the line. */
6056 if (!it->truncate_lines_p)
6057 {
6058 ascent = it->max_ascent;
6059 descent = it->max_descent;
6060 }
6061
6062 PRODUCE_GLYPHS (it);
6063
6064 if (it->area != TEXT_AREA)
6065 {
6066 set_iterator_to_next (it, 1);
6067 continue;
6068 }
6069
6070 /* The number of glyphs we get back in IT->nglyphs will normally
6071 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6072 character on a terminal frame, or (iii) a line end. For the
6073 second case, IT->nglyphs - 1 padding glyphs will be present
6074 (on X frames, there is only one glyph produced for a
6075 composite character.
6076
6077 The behavior implemented below means, for continuation lines,
6078 that as many spaces of a TAB as fit on the current line are
6079 displayed there. For terminal frames, as many glyphs of a
6080 multi-glyph character are displayed in the current line, too.
6081 This is what the old redisplay code did, and we keep it that
6082 way. Under X, the whole shape of a complex character must
6083 fit on the line or it will be completely displayed in the
6084 next line.
6085
6086 Note that both for tabs and padding glyphs, all glyphs have
6087 the same width. */
6088 if (it->nglyphs)
6089 {
6090 /* More than one glyph or glyph doesn't fit on line. All
6091 glyphs have the same width. */
6092 int single_glyph_width = it->pixel_width / it->nglyphs;
6093 int new_x;
6094
6095 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6096 {
6097 new_x = x + single_glyph_width;
6098
6099 /* We want to leave anything reaching TO_X to the caller. */
6100 if ((op & MOVE_TO_X) && new_x > to_x)
6101 {
6102 if (BUFFER_POS_REACHED_P ())
6103 goto buffer_pos_reached;
6104 it->current_x = x;
6105 result = MOVE_X_REACHED;
6106 break;
6107 }
6108 else if (/* Lines are continued. */
6109 !it->truncate_lines_p
6110 && (/* And glyph doesn't fit on the line. */
6111 new_x > it->last_visible_x
6112 /* Or it fits exactly and we're on a window
6113 system frame. */
6114 || (new_x == it->last_visible_x
6115 && FRAME_WINDOW_P (it->f))))
6116 {
6117 if (/* IT->hpos == 0 means the very first glyph
6118 doesn't fit on the line, e.g. a wide image. */
6119 it->hpos == 0
6120 || (new_x == it->last_visible_x
6121 && FRAME_WINDOW_P (it->f)))
6122 {
6123 ++it->hpos;
6124 it->current_x = new_x;
6125 if (i == it->nglyphs - 1)
6126 {
6127 set_iterator_to_next (it, 1);
6128 #ifdef HAVE_WINDOW_SYSTEM
6129 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6130 {
6131 if (!get_next_display_element (it))
6132 {
6133 result = MOVE_POS_MATCH_OR_ZV;
6134 break;
6135 }
6136 if (BUFFER_POS_REACHED_P ())
6137 {
6138 if (ITERATOR_AT_END_OF_LINE_P (it))
6139 result = MOVE_POS_MATCH_OR_ZV;
6140 else
6141 result = MOVE_LINE_CONTINUED;
6142 break;
6143 }
6144 if (ITERATOR_AT_END_OF_LINE_P (it))
6145 {
6146 result = MOVE_NEWLINE_OR_CR;
6147 break;
6148 }
6149 }
6150 #endif /* HAVE_WINDOW_SYSTEM */
6151 }
6152 }
6153 else
6154 {
6155 it->current_x = x;
6156 it->max_ascent = ascent;
6157 it->max_descent = descent;
6158 }
6159
6160 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6161 IT_CHARPOS (*it)));
6162 result = MOVE_LINE_CONTINUED;
6163 break;
6164 }
6165 else if (BUFFER_POS_REACHED_P ())
6166 goto buffer_pos_reached;
6167 else if (new_x > it->first_visible_x)
6168 {
6169 /* Glyph is visible. Increment number of glyphs that
6170 would be displayed. */
6171 ++it->hpos;
6172 }
6173 else
6174 {
6175 /* Glyph is completely off the left margin of the display
6176 area. Nothing to do. */
6177 }
6178 }
6179
6180 if (result != MOVE_UNDEFINED)
6181 break;
6182 }
6183 else if (BUFFER_POS_REACHED_P ())
6184 {
6185 buffer_pos_reached:
6186 it->current_x = x;
6187 it->max_ascent = ascent;
6188 it->max_descent = descent;
6189 result = MOVE_POS_MATCH_OR_ZV;
6190 break;
6191 }
6192 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6193 {
6194 /* Stop when TO_X specified and reached. This check is
6195 necessary here because of lines consisting of a line end,
6196 only. The line end will not produce any glyphs and we
6197 would never get MOVE_X_REACHED. */
6198 xassert (it->nglyphs == 0);
6199 result = MOVE_X_REACHED;
6200 break;
6201 }
6202
6203 /* Is this a line end? If yes, we're done. */
6204 if (ITERATOR_AT_END_OF_LINE_P (it))
6205 {
6206 result = MOVE_NEWLINE_OR_CR;
6207 break;
6208 }
6209
6210 /* The current display element has been consumed. Advance
6211 to the next. */
6212 set_iterator_to_next (it, 1);
6213
6214 /* Stop if lines are truncated and IT's current x-position is
6215 past the right edge of the window now. */
6216 if (it->truncate_lines_p
6217 && it->current_x >= it->last_visible_x)
6218 {
6219 #ifdef HAVE_WINDOW_SYSTEM
6220 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6221 {
6222 if (!get_next_display_element (it)
6223 || BUFFER_POS_REACHED_P ())
6224 {
6225 result = MOVE_POS_MATCH_OR_ZV;
6226 break;
6227 }
6228 if (ITERATOR_AT_END_OF_LINE_P (it))
6229 {
6230 result = MOVE_NEWLINE_OR_CR;
6231 break;
6232 }
6233 }
6234 #endif /* HAVE_WINDOW_SYSTEM */
6235 result = MOVE_LINE_TRUNCATED;
6236 break;
6237 }
6238 }
6239
6240 #undef BUFFER_POS_REACHED_P
6241
6242 /* Restore the iterator settings altered at the beginning of this
6243 function. */
6244 it->glyph_row = saved_glyph_row;
6245 return result;
6246 }
6247
6248
6249 /* Move IT forward until it satisfies one or more of the criteria in
6250 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6251
6252 OP is a bit-mask that specifies where to stop, and in particular,
6253 which of those four position arguments makes a difference. See the
6254 description of enum move_operation_enum.
6255
6256 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6257 screen line, this function will set IT to the next position >
6258 TO_CHARPOS. */
6259
6260 void
6261 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6262 struct it *it;
6263 int to_charpos, to_x, to_y, to_vpos;
6264 int op;
6265 {
6266 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6267 int line_height;
6268 int reached = 0;
6269
6270 for (;;)
6271 {
6272 if (op & MOVE_TO_VPOS)
6273 {
6274 /* If no TO_CHARPOS and no TO_X specified, stop at the
6275 start of the line TO_VPOS. */
6276 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6277 {
6278 if (it->vpos == to_vpos)
6279 {
6280 reached = 1;
6281 break;
6282 }
6283 else
6284 skip = move_it_in_display_line_to (it, -1, -1, 0);
6285 }
6286 else
6287 {
6288 /* TO_VPOS >= 0 means stop at TO_X in the line at
6289 TO_VPOS, or at TO_POS, whichever comes first. */
6290 if (it->vpos == to_vpos)
6291 {
6292 reached = 2;
6293 break;
6294 }
6295
6296 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6297
6298 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6299 {
6300 reached = 3;
6301 break;
6302 }
6303 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6304 {
6305 /* We have reached TO_X but not in the line we want. */
6306 skip = move_it_in_display_line_to (it, to_charpos,
6307 -1, MOVE_TO_POS);
6308 if (skip == MOVE_POS_MATCH_OR_ZV)
6309 {
6310 reached = 4;
6311 break;
6312 }
6313 }
6314 }
6315 }
6316 else if (op & MOVE_TO_Y)
6317 {
6318 struct it it_backup;
6319
6320 /* TO_Y specified means stop at TO_X in the line containing
6321 TO_Y---or at TO_CHARPOS if this is reached first. The
6322 problem is that we can't really tell whether the line
6323 contains TO_Y before we have completely scanned it, and
6324 this may skip past TO_X. What we do is to first scan to
6325 TO_X.
6326
6327 If TO_X is not specified, use a TO_X of zero. The reason
6328 is to make the outcome of this function more predictable.
6329 If we didn't use TO_X == 0, we would stop at the end of
6330 the line which is probably not what a caller would expect
6331 to happen. */
6332 skip = move_it_in_display_line_to (it, to_charpos,
6333 ((op & MOVE_TO_X)
6334 ? to_x : 0),
6335 (MOVE_TO_X
6336 | (op & MOVE_TO_POS)));
6337
6338 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6339 if (skip == MOVE_POS_MATCH_OR_ZV)
6340 {
6341 reached = 5;
6342 break;
6343 }
6344
6345 /* If TO_X was reached, we would like to know whether TO_Y
6346 is in the line. This can only be said if we know the
6347 total line height which requires us to scan the rest of
6348 the line. */
6349 if (skip == MOVE_X_REACHED)
6350 {
6351 /* Wait! We can conclude that TO_Y is in the line if
6352 the already scanned glyphs make the line tall enough
6353 because further scanning doesn't make it shorter. */
6354 line_height = it->max_ascent + it->max_descent;
6355 if (to_y >= it->current_y
6356 && to_y < it->current_y + line_height)
6357 {
6358 reached = 6;
6359 break;
6360 }
6361 it_backup = *it;
6362 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6363 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6364 op & MOVE_TO_POS);
6365 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6366 }
6367
6368 /* Now, decide whether TO_Y is in this line. */
6369 line_height = it->max_ascent + it->max_descent;
6370 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6371
6372 if (to_y >= it->current_y
6373 && to_y < it->current_y + line_height)
6374 {
6375 if (skip == MOVE_X_REACHED)
6376 /* If TO_Y is in this line and TO_X was reached above,
6377 we scanned too far. We have to restore IT's settings
6378 to the ones before skipping. */
6379 *it = it_backup;
6380 reached = 6;
6381 }
6382 else if (skip == MOVE_X_REACHED)
6383 {
6384 skip = skip2;
6385 if (skip == MOVE_POS_MATCH_OR_ZV)
6386 reached = 7;
6387 }
6388
6389 if (reached)
6390 break;
6391 }
6392 else
6393 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6394
6395 switch (skip)
6396 {
6397 case MOVE_POS_MATCH_OR_ZV:
6398 reached = 8;
6399 goto out;
6400
6401 case MOVE_NEWLINE_OR_CR:
6402 set_iterator_to_next (it, 1);
6403 it->continuation_lines_width = 0;
6404 break;
6405
6406 case MOVE_LINE_TRUNCATED:
6407 it->continuation_lines_width = 0;
6408 reseat_at_next_visible_line_start (it, 0);
6409 if ((op & MOVE_TO_POS) != 0
6410 && IT_CHARPOS (*it) > to_charpos)
6411 {
6412 reached = 9;
6413 goto out;
6414 }
6415 break;
6416
6417 case MOVE_LINE_CONTINUED:
6418 it->continuation_lines_width += it->current_x;
6419 break;
6420
6421 default:
6422 abort ();
6423 }
6424
6425 /* Reset/increment for the next run. */
6426 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6427 it->current_x = it->hpos = 0;
6428 it->current_y += it->max_ascent + it->max_descent;
6429 ++it->vpos;
6430 last_height = it->max_ascent + it->max_descent;
6431 last_max_ascent = it->max_ascent;
6432 it->max_ascent = it->max_descent = 0;
6433 }
6434
6435 out:
6436
6437 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6438 }
6439
6440
6441 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6442
6443 If DY > 0, move IT backward at least that many pixels. DY = 0
6444 means move IT backward to the preceding line start or BEGV. This
6445 function may move over more than DY pixels if IT->current_y - DY
6446 ends up in the middle of a line; in this case IT->current_y will be
6447 set to the top of the line moved to. */
6448
6449 void
6450 move_it_vertically_backward (it, dy)
6451 struct it *it;
6452 int dy;
6453 {
6454 int nlines, h;
6455 struct it it2, it3;
6456 int start_pos;
6457
6458 move_further_back:
6459 xassert (dy >= 0);
6460
6461 start_pos = IT_CHARPOS (*it);
6462
6463 /* Estimate how many newlines we must move back. */
6464 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6465
6466 /* Set the iterator's position that many lines back. */
6467 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6468 back_to_previous_visible_line_start (it);
6469
6470 /* Reseat the iterator here. When moving backward, we don't want
6471 reseat to skip forward over invisible text, set up the iterator
6472 to deliver from overlay strings at the new position etc. So,
6473 use reseat_1 here. */
6474 reseat_1 (it, it->current.pos, 1);
6475
6476 /* We are now surely at a line start. */
6477 it->current_x = it->hpos = 0;
6478 it->continuation_lines_width = 0;
6479
6480 /* Move forward and see what y-distance we moved. First move to the
6481 start of the next line so that we get its height. We need this
6482 height to be able to tell whether we reached the specified
6483 y-distance. */
6484 it2 = *it;
6485 it2.max_ascent = it2.max_descent = 0;
6486 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6487 MOVE_TO_POS | MOVE_TO_VPOS);
6488 xassert (IT_CHARPOS (*it) >= BEGV);
6489 it3 = it2;
6490
6491 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6492 xassert (IT_CHARPOS (*it) >= BEGV);
6493 /* H is the actual vertical distance from the position in *IT
6494 and the starting position. */
6495 h = it2.current_y - it->current_y;
6496 /* NLINES is the distance in number of lines. */
6497 nlines = it2.vpos - it->vpos;
6498
6499 /* Correct IT's y and vpos position
6500 so that they are relative to the starting point. */
6501 it->vpos -= nlines;
6502 it->current_y -= h;
6503
6504 if (dy == 0)
6505 {
6506 /* DY == 0 means move to the start of the screen line. The
6507 value of nlines is > 0 if continuation lines were involved. */
6508 if (nlines > 0)
6509 move_it_by_lines (it, nlines, 1);
6510 #if 0
6511 /* I think this assert is bogus if buffer contains
6512 invisible text or images. KFS. */
6513 xassert (IT_CHARPOS (*it) <= start_pos);
6514 #endif
6515 }
6516 else
6517 {
6518 /* The y-position we try to reach, relative to *IT.
6519 Note that H has been subtracted in front of the if-statement. */
6520 int target_y = it->current_y + h - dy;
6521 int y0 = it3.current_y;
6522 int y1 = line_bottom_y (&it3);
6523 int line_height = y1 - y0;
6524
6525 /* If we did not reach target_y, try to move further backward if
6526 we can. If we moved too far backward, try to move forward. */
6527 if (target_y < it->current_y
6528 /* This is heuristic. In a window that's 3 lines high, with
6529 a line height of 13 pixels each, recentering with point
6530 on the bottom line will try to move -39/2 = 19 pixels
6531 backward. Try to avoid moving into the first line. */
6532 && (it->current_y - target_y
6533 > min (window_box_height (it->w), line_height * 2 / 3))
6534 && IT_CHARPOS (*it) > BEGV)
6535 {
6536 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6537 target_y - it->current_y));
6538 dy = it->current_y - target_y;
6539 goto move_further_back;
6540 }
6541 else if (target_y >= it->current_y + line_height
6542 && IT_CHARPOS (*it) < ZV)
6543 {
6544 /* Should move forward by at least one line, maybe more.
6545
6546 Note: Calling move_it_by_lines can be expensive on
6547 terminal frames, where compute_motion is used (via
6548 vmotion) to do the job, when there are very long lines
6549 and truncate-lines is nil. That's the reason for
6550 treating terminal frames specially here. */
6551
6552 if (!FRAME_WINDOW_P (it->f))
6553 move_it_vertically (it, target_y - (it->current_y + line_height));
6554 else
6555 {
6556 do
6557 {
6558 move_it_by_lines (it, 1, 1);
6559 }
6560 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6561 }
6562
6563 #if 0
6564 /* I think this assert is bogus if buffer contains
6565 invisible text or images. KFS. */
6566 xassert (IT_CHARPOS (*it) >= BEGV);
6567 #endif
6568 }
6569 }
6570 }
6571
6572
6573 /* Move IT by a specified amount of pixel lines DY. DY negative means
6574 move backwards. DY = 0 means move to start of screen line. At the
6575 end, IT will be on the start of a screen line. */
6576
6577 void
6578 move_it_vertically (it, dy)
6579 struct it *it;
6580 int dy;
6581 {
6582 if (dy <= 0)
6583 move_it_vertically_backward (it, -dy);
6584 else
6585 {
6586 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6587 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6588 MOVE_TO_POS | MOVE_TO_Y);
6589 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6590
6591 /* If buffer ends in ZV without a newline, move to the start of
6592 the line to satisfy the post-condition. */
6593 if (IT_CHARPOS (*it) == ZV
6594 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6595 move_it_by_lines (it, 0, 0);
6596 }
6597 }
6598
6599
6600 /* Move iterator IT past the end of the text line it is in. */
6601
6602 void
6603 move_it_past_eol (it)
6604 struct it *it;
6605 {
6606 enum move_it_result rc;
6607
6608 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6609 if (rc == MOVE_NEWLINE_OR_CR)
6610 set_iterator_to_next (it, 0);
6611 }
6612
6613
6614 #if 0 /* Currently not used. */
6615
6616 /* Return non-zero if some text between buffer positions START_CHARPOS
6617 and END_CHARPOS is invisible. IT->window is the window for text
6618 property lookup. */
6619
6620 static int
6621 invisible_text_between_p (it, start_charpos, end_charpos)
6622 struct it *it;
6623 int start_charpos, end_charpos;
6624 {
6625 Lisp_Object prop, limit;
6626 int invisible_found_p;
6627
6628 xassert (it != NULL && start_charpos <= end_charpos);
6629
6630 /* Is text at START invisible? */
6631 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6632 it->window);
6633 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6634 invisible_found_p = 1;
6635 else
6636 {
6637 limit = Fnext_single_char_property_change (make_number (start_charpos),
6638 Qinvisible, Qnil,
6639 make_number (end_charpos));
6640 invisible_found_p = XFASTINT (limit) < end_charpos;
6641 }
6642
6643 return invisible_found_p;
6644 }
6645
6646 #endif /* 0 */
6647
6648
6649 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6650 negative means move up. DVPOS == 0 means move to the start of the
6651 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6652 NEED_Y_P is zero, IT->current_y will be left unchanged.
6653
6654 Further optimization ideas: If we would know that IT->f doesn't use
6655 a face with proportional font, we could be faster for
6656 truncate-lines nil. */
6657
6658 void
6659 move_it_by_lines (it, dvpos, need_y_p)
6660 struct it *it;
6661 int dvpos, need_y_p;
6662 {
6663 struct position pos;
6664
6665 if (!FRAME_WINDOW_P (it->f))
6666 {
6667 struct text_pos textpos;
6668
6669 /* We can use vmotion on frames without proportional fonts. */
6670 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6671 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6672 reseat (it, textpos, 1);
6673 it->vpos += pos.vpos;
6674 it->current_y += pos.vpos;
6675 }
6676 else if (dvpos == 0)
6677 {
6678 /* DVPOS == 0 means move to the start of the screen line. */
6679 move_it_vertically_backward (it, 0);
6680 xassert (it->current_x == 0 && it->hpos == 0);
6681 /* Let next call to line_bottom_y calculate real line height */
6682 last_height = 0;
6683 }
6684 else if (dvpos > 0)
6685 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6686 else
6687 {
6688 struct it it2;
6689 int start_charpos, i;
6690
6691 /* Start at the beginning of the screen line containing IT's
6692 position. */
6693 move_it_vertically_backward (it, 0);
6694
6695 /* Go back -DVPOS visible lines and reseat the iterator there. */
6696 start_charpos = IT_CHARPOS (*it);
6697 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6698 back_to_previous_visible_line_start (it);
6699 reseat (it, it->current.pos, 1);
6700 it->current_x = it->hpos = 0;
6701
6702 /* Above call may have moved too far if continuation lines
6703 are involved. Scan forward and see if it did. */
6704 it2 = *it;
6705 it2.vpos = it2.current_y = 0;
6706 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6707 it->vpos -= it2.vpos;
6708 it->current_y -= it2.current_y;
6709 it->current_x = it->hpos = 0;
6710
6711 /* If we moved too far back, move IT some lines forward. */
6712 if (it2.vpos > -dvpos)
6713 {
6714 int delta = it2.vpos + dvpos;
6715 it2 = *it;
6716 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6717 /* Move back again if we got too far ahead. */
6718 if (IT_CHARPOS (*it) >= start_charpos)
6719 *it = it2;
6720 }
6721 }
6722 }
6723
6724 /* Return 1 if IT points into the middle of a display vector. */
6725
6726 int
6727 in_display_vector_p (it)
6728 struct it *it;
6729 {
6730 return (it->method == GET_FROM_DISPLAY_VECTOR
6731 && it->current.dpvec_index > 0
6732 && it->dpvec + it->current.dpvec_index != it->dpend);
6733 }
6734
6735 \f
6736 /***********************************************************************
6737 Messages
6738 ***********************************************************************/
6739
6740
6741 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6742 to *Messages*. */
6743
6744 void
6745 add_to_log (format, arg1, arg2)
6746 char *format;
6747 Lisp_Object arg1, arg2;
6748 {
6749 Lisp_Object args[3];
6750 Lisp_Object msg, fmt;
6751 char *buffer;
6752 int len;
6753 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6754 USE_SAFE_ALLOCA;
6755
6756 /* Do nothing if called asynchronously. Inserting text into
6757 a buffer may call after-change-functions and alike and
6758 that would means running Lisp asynchronously. */
6759 if (handling_signal)
6760 return;
6761
6762 fmt = msg = Qnil;
6763 GCPRO4 (fmt, msg, arg1, arg2);
6764
6765 args[0] = fmt = build_string (format);
6766 args[1] = arg1;
6767 args[2] = arg2;
6768 msg = Fformat (3, args);
6769
6770 len = SBYTES (msg) + 1;
6771 SAFE_ALLOCA (buffer, char *, len);
6772 bcopy (SDATA (msg), buffer, len);
6773
6774 message_dolog (buffer, len - 1, 1, 0);
6775 SAFE_FREE ();
6776
6777 UNGCPRO;
6778 }
6779
6780
6781 /* Output a newline in the *Messages* buffer if "needs" one. */
6782
6783 void
6784 message_log_maybe_newline ()
6785 {
6786 if (message_log_need_newline)
6787 message_dolog ("", 0, 1, 0);
6788 }
6789
6790
6791 /* Add a string M of length NBYTES to the message log, optionally
6792 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6793 nonzero, means interpret the contents of M as multibyte. This
6794 function calls low-level routines in order to bypass text property
6795 hooks, etc. which might not be safe to run. */
6796
6797 void
6798 message_dolog (m, nbytes, nlflag, multibyte)
6799 const char *m;
6800 int nbytes, nlflag, multibyte;
6801 {
6802 if (!NILP (Vmemory_full))
6803 return;
6804
6805 if (!NILP (Vmessage_log_max))
6806 {
6807 struct buffer *oldbuf;
6808 Lisp_Object oldpoint, oldbegv, oldzv;
6809 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6810 int point_at_end = 0;
6811 int zv_at_end = 0;
6812 Lisp_Object old_deactivate_mark, tem;
6813 struct gcpro gcpro1;
6814
6815 old_deactivate_mark = Vdeactivate_mark;
6816 oldbuf = current_buffer;
6817 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6818 current_buffer->undo_list = Qt;
6819
6820 oldpoint = message_dolog_marker1;
6821 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6822 oldbegv = message_dolog_marker2;
6823 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6824 oldzv = message_dolog_marker3;
6825 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6826 GCPRO1 (old_deactivate_mark);
6827
6828 if (PT == Z)
6829 point_at_end = 1;
6830 if (ZV == Z)
6831 zv_at_end = 1;
6832
6833 BEGV = BEG;
6834 BEGV_BYTE = BEG_BYTE;
6835 ZV = Z;
6836 ZV_BYTE = Z_BYTE;
6837 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6838
6839 /* Insert the string--maybe converting multibyte to single byte
6840 or vice versa, so that all the text fits the buffer. */
6841 if (multibyte
6842 && NILP (current_buffer->enable_multibyte_characters))
6843 {
6844 int i, c, char_bytes;
6845 unsigned char work[1];
6846
6847 /* Convert a multibyte string to single-byte
6848 for the *Message* buffer. */
6849 for (i = 0; i < nbytes; i += char_bytes)
6850 {
6851 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6852 work[0] = (ASCII_CHAR_P (c)
6853 ? c
6854 : multibyte_char_to_unibyte (c, Qnil));
6855 insert_1_both (work, 1, 1, 1, 0, 0);
6856 }
6857 }
6858 else if (! multibyte
6859 && ! NILP (current_buffer->enable_multibyte_characters))
6860 {
6861 int i, c, char_bytes;
6862 unsigned char *msg = (unsigned char *) m;
6863 unsigned char str[MAX_MULTIBYTE_LENGTH];
6864 /* Convert a single-byte string to multibyte
6865 for the *Message* buffer. */
6866 for (i = 0; i < nbytes; i++)
6867 {
6868 c = msg[i];
6869 c = unibyte_char_to_multibyte (c);
6870 char_bytes = CHAR_STRING (c, str);
6871 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6872 }
6873 }
6874 else if (nbytes)
6875 insert_1 (m, nbytes, 1, 0, 0);
6876
6877 if (nlflag)
6878 {
6879 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6880 insert_1 ("\n", 1, 1, 0, 0);
6881
6882 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6883 this_bol = PT;
6884 this_bol_byte = PT_BYTE;
6885
6886 /* See if this line duplicates the previous one.
6887 If so, combine duplicates. */
6888 if (this_bol > BEG)
6889 {
6890 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6891 prev_bol = PT;
6892 prev_bol_byte = PT_BYTE;
6893
6894 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6895 this_bol, this_bol_byte);
6896 if (dup)
6897 {
6898 del_range_both (prev_bol, prev_bol_byte,
6899 this_bol, this_bol_byte, 0);
6900 if (dup > 1)
6901 {
6902 char dupstr[40];
6903 int duplen;
6904
6905 /* If you change this format, don't forget to also
6906 change message_log_check_duplicate. */
6907 sprintf (dupstr, " [%d times]", dup);
6908 duplen = strlen (dupstr);
6909 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6910 insert_1 (dupstr, duplen, 1, 0, 1);
6911 }
6912 }
6913 }
6914
6915 /* If we have more than the desired maximum number of lines
6916 in the *Messages* buffer now, delete the oldest ones.
6917 This is safe because we don't have undo in this buffer. */
6918
6919 if (NATNUMP (Vmessage_log_max))
6920 {
6921 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6922 -XFASTINT (Vmessage_log_max) - 1, 0);
6923 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6924 }
6925 }
6926 BEGV = XMARKER (oldbegv)->charpos;
6927 BEGV_BYTE = marker_byte_position (oldbegv);
6928
6929 if (zv_at_end)
6930 {
6931 ZV = Z;
6932 ZV_BYTE = Z_BYTE;
6933 }
6934 else
6935 {
6936 ZV = XMARKER (oldzv)->charpos;
6937 ZV_BYTE = marker_byte_position (oldzv);
6938 }
6939
6940 if (point_at_end)
6941 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6942 else
6943 /* We can't do Fgoto_char (oldpoint) because it will run some
6944 Lisp code. */
6945 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6946 XMARKER (oldpoint)->bytepos);
6947
6948 UNGCPRO;
6949 unchain_marker (XMARKER (oldpoint));
6950 unchain_marker (XMARKER (oldbegv));
6951 unchain_marker (XMARKER (oldzv));
6952
6953 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6954 set_buffer_internal (oldbuf);
6955 if (NILP (tem))
6956 windows_or_buffers_changed = old_windows_or_buffers_changed;
6957 message_log_need_newline = !nlflag;
6958 Vdeactivate_mark = old_deactivate_mark;
6959 }
6960 }
6961
6962
6963 /* We are at the end of the buffer after just having inserted a newline.
6964 (Note: We depend on the fact we won't be crossing the gap.)
6965 Check to see if the most recent message looks a lot like the previous one.
6966 Return 0 if different, 1 if the new one should just replace it, or a
6967 value N > 1 if we should also append " [N times]". */
6968
6969 static int
6970 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6971 int prev_bol, this_bol;
6972 int prev_bol_byte, this_bol_byte;
6973 {
6974 int i;
6975 int len = Z_BYTE - 1 - this_bol_byte;
6976 int seen_dots = 0;
6977 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6978 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6979
6980 for (i = 0; i < len; i++)
6981 {
6982 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6983 seen_dots = 1;
6984 if (p1[i] != p2[i])
6985 return seen_dots;
6986 }
6987 p1 += len;
6988 if (*p1 == '\n')
6989 return 2;
6990 if (*p1++ == ' ' && *p1++ == '[')
6991 {
6992 int n = 0;
6993 while (*p1 >= '0' && *p1 <= '9')
6994 n = n * 10 + *p1++ - '0';
6995 if (strncmp (p1, " times]\n", 8) == 0)
6996 return n+1;
6997 }
6998 return 0;
6999 }
7000 \f
7001
7002 /* Display an echo area message M with a specified length of NBYTES
7003 bytes. The string may include null characters. If M is 0, clear
7004 out any existing message, and let the mini-buffer text show
7005 through.
7006
7007 The buffer M must continue to exist until after the echo area gets
7008 cleared or some other message gets displayed there. This means do
7009 not pass text that is stored in a Lisp string; do not pass text in
7010 a buffer that was alloca'd. */
7011
7012 void
7013 message2 (m, nbytes, multibyte)
7014 const char *m;
7015 int nbytes;
7016 int multibyte;
7017 {
7018 /* First flush out any partial line written with print. */
7019 message_log_maybe_newline ();
7020 if (m)
7021 message_dolog (m, nbytes, 1, multibyte);
7022 message2_nolog (m, nbytes, multibyte);
7023 }
7024
7025
7026 /* The non-logging counterpart of message2. */
7027
7028 void
7029 message2_nolog (m, nbytes, multibyte)
7030 const char *m;
7031 int nbytes, multibyte;
7032 {
7033 struct frame *sf = SELECTED_FRAME ();
7034 message_enable_multibyte = multibyte;
7035
7036 if (noninteractive)
7037 {
7038 if (noninteractive_need_newline)
7039 putc ('\n', stderr);
7040 noninteractive_need_newline = 0;
7041 if (m)
7042 fwrite (m, nbytes, 1, stderr);
7043 if (cursor_in_echo_area == 0)
7044 fprintf (stderr, "\n");
7045 fflush (stderr);
7046 }
7047 /* A null message buffer means that the frame hasn't really been
7048 initialized yet. Error messages get reported properly by
7049 cmd_error, so this must be just an informative message; toss it. */
7050 else if (INTERACTIVE
7051 && sf->glyphs_initialized_p
7052 && FRAME_MESSAGE_BUF (sf))
7053 {
7054 Lisp_Object mini_window;
7055 struct frame *f;
7056
7057 /* Get the frame containing the mini-buffer
7058 that the selected frame is using. */
7059 mini_window = FRAME_MINIBUF_WINDOW (sf);
7060 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7061
7062 FRAME_SAMPLE_VISIBILITY (f);
7063 if (FRAME_VISIBLE_P (sf)
7064 && ! FRAME_VISIBLE_P (f))
7065 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7066
7067 if (m)
7068 {
7069 set_message (m, Qnil, nbytes, multibyte);
7070 if (minibuffer_auto_raise)
7071 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7072 }
7073 else
7074 clear_message (1, 1);
7075
7076 do_pending_window_change (0);
7077 echo_area_display (1);
7078 do_pending_window_change (0);
7079 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7080 (*frame_up_to_date_hook) (f);
7081 }
7082 }
7083
7084
7085 /* Display an echo area message M with a specified length of NBYTES
7086 bytes. The string may include null characters. If M is not a
7087 string, clear out any existing message, and let the mini-buffer
7088 text show through.
7089
7090 This function cancels echoing. */
7091
7092 void
7093 message3 (m, nbytes, multibyte)
7094 Lisp_Object m;
7095 int nbytes;
7096 int multibyte;
7097 {
7098 struct gcpro gcpro1;
7099
7100 GCPRO1 (m);
7101 clear_message (1,1);
7102 cancel_echoing ();
7103
7104 /* First flush out any partial line written with print. */
7105 message_log_maybe_newline ();
7106 if (STRINGP (m))
7107 message_dolog (SDATA (m), nbytes, 1, multibyte);
7108 message3_nolog (m, nbytes, multibyte);
7109
7110 UNGCPRO;
7111 }
7112
7113
7114 /* The non-logging version of message3.
7115 This does not cancel echoing, because it is used for echoing.
7116 Perhaps we need to make a separate function for echoing
7117 and make this cancel echoing. */
7118
7119 void
7120 message3_nolog (m, nbytes, multibyte)
7121 Lisp_Object m;
7122 int nbytes, multibyte;
7123 {
7124 struct frame *sf = SELECTED_FRAME ();
7125 message_enable_multibyte = multibyte;
7126
7127 if (noninteractive)
7128 {
7129 if (noninteractive_need_newline)
7130 putc ('\n', stderr);
7131 noninteractive_need_newline = 0;
7132 if (STRINGP (m))
7133 fwrite (SDATA (m), nbytes, 1, stderr);
7134 if (cursor_in_echo_area == 0)
7135 fprintf (stderr, "\n");
7136 fflush (stderr);
7137 }
7138 /* A null message buffer means that the frame hasn't really been
7139 initialized yet. Error messages get reported properly by
7140 cmd_error, so this must be just an informative message; toss it. */
7141 else if (INTERACTIVE
7142 && sf->glyphs_initialized_p
7143 && FRAME_MESSAGE_BUF (sf))
7144 {
7145 Lisp_Object mini_window;
7146 Lisp_Object frame;
7147 struct frame *f;
7148
7149 /* Get the frame containing the mini-buffer
7150 that the selected frame is using. */
7151 mini_window = FRAME_MINIBUF_WINDOW (sf);
7152 frame = XWINDOW (mini_window)->frame;
7153 f = XFRAME (frame);
7154
7155 FRAME_SAMPLE_VISIBILITY (f);
7156 if (FRAME_VISIBLE_P (sf)
7157 && !FRAME_VISIBLE_P (f))
7158 Fmake_frame_visible (frame);
7159
7160 if (STRINGP (m) && SCHARS (m) > 0)
7161 {
7162 set_message (NULL, m, nbytes, multibyte);
7163 if (minibuffer_auto_raise)
7164 Fraise_frame (frame);
7165 }
7166 else
7167 clear_message (1, 1);
7168
7169 do_pending_window_change (0);
7170 echo_area_display (1);
7171 do_pending_window_change (0);
7172 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7173 (*frame_up_to_date_hook) (f);
7174 }
7175 }
7176
7177
7178 /* Display a null-terminated echo area message M. If M is 0, clear
7179 out any existing message, and let the mini-buffer text show through.
7180
7181 The buffer M must continue to exist until after the echo area gets
7182 cleared or some other message gets displayed there. Do not pass
7183 text that is stored in a Lisp string. Do not pass text in a buffer
7184 that was alloca'd. */
7185
7186 void
7187 message1 (m)
7188 char *m;
7189 {
7190 message2 (m, (m ? strlen (m) : 0), 0);
7191 }
7192
7193
7194 /* The non-logging counterpart of message1. */
7195
7196 void
7197 message1_nolog (m)
7198 char *m;
7199 {
7200 message2_nolog (m, (m ? strlen (m) : 0), 0);
7201 }
7202
7203 /* Display a message M which contains a single %s
7204 which gets replaced with STRING. */
7205
7206 void
7207 message_with_string (m, string, log)
7208 char *m;
7209 Lisp_Object string;
7210 int log;
7211 {
7212 CHECK_STRING (string);
7213
7214 if (noninteractive)
7215 {
7216 if (m)
7217 {
7218 if (noninteractive_need_newline)
7219 putc ('\n', stderr);
7220 noninteractive_need_newline = 0;
7221 fprintf (stderr, m, SDATA (string));
7222 if (cursor_in_echo_area == 0)
7223 fprintf (stderr, "\n");
7224 fflush (stderr);
7225 }
7226 }
7227 else if (INTERACTIVE)
7228 {
7229 /* The frame whose minibuffer we're going to display the message on.
7230 It may be larger than the selected frame, so we need
7231 to use its buffer, not the selected frame's buffer. */
7232 Lisp_Object mini_window;
7233 struct frame *f, *sf = SELECTED_FRAME ();
7234
7235 /* Get the frame containing the minibuffer
7236 that the selected frame is using. */
7237 mini_window = FRAME_MINIBUF_WINDOW (sf);
7238 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7239
7240 /* A null message buffer means that the frame hasn't really been
7241 initialized yet. Error messages get reported properly by
7242 cmd_error, so this must be just an informative message; toss it. */
7243 if (FRAME_MESSAGE_BUF (f))
7244 {
7245 Lisp_Object args[2], message;
7246 struct gcpro gcpro1, gcpro2;
7247
7248 args[0] = build_string (m);
7249 args[1] = message = string;
7250 GCPRO2 (args[0], message);
7251 gcpro1.nvars = 2;
7252
7253 message = Fformat (2, args);
7254
7255 if (log)
7256 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7257 else
7258 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7259
7260 UNGCPRO;
7261
7262 /* Print should start at the beginning of the message
7263 buffer next time. */
7264 message_buf_print = 0;
7265 }
7266 }
7267 }
7268
7269
7270 /* Dump an informative message to the minibuf. If M is 0, clear out
7271 any existing message, and let the mini-buffer text show through. */
7272
7273 /* VARARGS 1 */
7274 void
7275 message (m, a1, a2, a3)
7276 char *m;
7277 EMACS_INT a1, a2, a3;
7278 {
7279 if (noninteractive)
7280 {
7281 if (m)
7282 {
7283 if (noninteractive_need_newline)
7284 putc ('\n', stderr);
7285 noninteractive_need_newline = 0;
7286 fprintf (stderr, m, a1, a2, a3);
7287 if (cursor_in_echo_area == 0)
7288 fprintf (stderr, "\n");
7289 fflush (stderr);
7290 }
7291 }
7292 else if (INTERACTIVE)
7293 {
7294 /* The frame whose mini-buffer we're going to display the message
7295 on. It may be larger than the selected frame, so we need to
7296 use its buffer, not the selected frame's buffer. */
7297 Lisp_Object mini_window;
7298 struct frame *f, *sf = SELECTED_FRAME ();
7299
7300 /* Get the frame containing the mini-buffer
7301 that the selected frame is using. */
7302 mini_window = FRAME_MINIBUF_WINDOW (sf);
7303 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7304
7305 /* A null message buffer means that the frame hasn't really been
7306 initialized yet. Error messages get reported properly by
7307 cmd_error, so this must be just an informative message; toss
7308 it. */
7309 if (FRAME_MESSAGE_BUF (f))
7310 {
7311 if (m)
7312 {
7313 int len;
7314 #ifdef NO_ARG_ARRAY
7315 char *a[3];
7316 a[0] = (char *) a1;
7317 a[1] = (char *) a2;
7318 a[2] = (char *) a3;
7319
7320 len = doprnt (FRAME_MESSAGE_BUF (f),
7321 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7322 #else
7323 len = doprnt (FRAME_MESSAGE_BUF (f),
7324 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7325 (char **) &a1);
7326 #endif /* NO_ARG_ARRAY */
7327
7328 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7329 }
7330 else
7331 message1 (0);
7332
7333 /* Print should start at the beginning of the message
7334 buffer next time. */
7335 message_buf_print = 0;
7336 }
7337 }
7338 }
7339
7340
7341 /* The non-logging version of message. */
7342
7343 void
7344 message_nolog (m, a1, a2, a3)
7345 char *m;
7346 EMACS_INT a1, a2, a3;
7347 {
7348 Lisp_Object old_log_max;
7349 old_log_max = Vmessage_log_max;
7350 Vmessage_log_max = Qnil;
7351 message (m, a1, a2, a3);
7352 Vmessage_log_max = old_log_max;
7353 }
7354
7355
7356 /* Display the current message in the current mini-buffer. This is
7357 only called from error handlers in process.c, and is not time
7358 critical. */
7359
7360 void
7361 update_echo_area ()
7362 {
7363 if (!NILP (echo_area_buffer[0]))
7364 {
7365 Lisp_Object string;
7366 string = Fcurrent_message ();
7367 message3 (string, SBYTES (string),
7368 !NILP (current_buffer->enable_multibyte_characters));
7369 }
7370 }
7371
7372
7373 /* Make sure echo area buffers in `echo_buffers' are live.
7374 If they aren't, make new ones. */
7375
7376 static void
7377 ensure_echo_area_buffers ()
7378 {
7379 int i;
7380
7381 for (i = 0; i < 2; ++i)
7382 if (!BUFFERP (echo_buffer[i])
7383 || NILP (XBUFFER (echo_buffer[i])->name))
7384 {
7385 char name[30];
7386 Lisp_Object old_buffer;
7387 int j;
7388
7389 old_buffer = echo_buffer[i];
7390 sprintf (name, " *Echo Area %d*", i);
7391 echo_buffer[i] = Fget_buffer_create (build_string (name));
7392 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7393
7394 for (j = 0; j < 2; ++j)
7395 if (EQ (old_buffer, echo_area_buffer[j]))
7396 echo_area_buffer[j] = echo_buffer[i];
7397 }
7398 }
7399
7400
7401 /* Call FN with args A1..A4 with either the current or last displayed
7402 echo_area_buffer as current buffer.
7403
7404 WHICH zero means use the current message buffer
7405 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7406 from echo_buffer[] and clear it.
7407
7408 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7409 suitable buffer from echo_buffer[] and clear it.
7410
7411 Value is what FN returns. */
7412
7413 static int
7414 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7415 struct window *w;
7416 int which;
7417 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7418 EMACS_INT a1;
7419 Lisp_Object a2;
7420 EMACS_INT a3, a4;
7421 {
7422 Lisp_Object buffer;
7423 int this_one, the_other, clear_buffer_p, rc;
7424 int count = SPECPDL_INDEX ();
7425
7426 /* If buffers aren't live, make new ones. */
7427 ensure_echo_area_buffers ();
7428
7429 clear_buffer_p = 0;
7430
7431 if (which == 0)
7432 this_one = 0, the_other = 1;
7433 else if (which > 0)
7434 this_one = 1, the_other = 0;
7435
7436 /* Choose a suitable buffer from echo_buffer[] is we don't
7437 have one. */
7438 if (NILP (echo_area_buffer[this_one]))
7439 {
7440 echo_area_buffer[this_one]
7441 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7442 ? echo_buffer[the_other]
7443 : echo_buffer[this_one]);
7444 clear_buffer_p = 1;
7445 }
7446
7447 buffer = echo_area_buffer[this_one];
7448
7449 /* Don't get confused by reusing the buffer used for echoing
7450 for a different purpose. */
7451 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7452 cancel_echoing ();
7453
7454 record_unwind_protect (unwind_with_echo_area_buffer,
7455 with_echo_area_buffer_unwind_data (w));
7456
7457 /* Make the echo area buffer current. Note that for display
7458 purposes, it is not necessary that the displayed window's buffer
7459 == current_buffer, except for text property lookup. So, let's
7460 only set that buffer temporarily here without doing a full
7461 Fset_window_buffer. We must also change w->pointm, though,
7462 because otherwise an assertions in unshow_buffer fails, and Emacs
7463 aborts. */
7464 set_buffer_internal_1 (XBUFFER (buffer));
7465 if (w)
7466 {
7467 w->buffer = buffer;
7468 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7469 }
7470
7471 current_buffer->undo_list = Qt;
7472 current_buffer->read_only = Qnil;
7473 specbind (Qinhibit_read_only, Qt);
7474 specbind (Qinhibit_modification_hooks, Qt);
7475
7476 if (clear_buffer_p && Z > BEG)
7477 del_range (BEG, Z);
7478
7479 xassert (BEGV >= BEG);
7480 xassert (ZV <= Z && ZV >= BEGV);
7481
7482 rc = fn (a1, a2, a3, a4);
7483
7484 xassert (BEGV >= BEG);
7485 xassert (ZV <= Z && ZV >= BEGV);
7486
7487 unbind_to (count, Qnil);
7488 return rc;
7489 }
7490
7491
7492 /* Save state that should be preserved around the call to the function
7493 FN called in with_echo_area_buffer. */
7494
7495 static Lisp_Object
7496 with_echo_area_buffer_unwind_data (w)
7497 struct window *w;
7498 {
7499 int i = 0;
7500 Lisp_Object vector;
7501
7502 /* Reduce consing by keeping one vector in
7503 Vwith_echo_area_save_vector. */
7504 vector = Vwith_echo_area_save_vector;
7505 Vwith_echo_area_save_vector = Qnil;
7506
7507 if (NILP (vector))
7508 vector = Fmake_vector (make_number (7), Qnil);
7509
7510 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7511 AREF (vector, i) = Vdeactivate_mark, ++i;
7512 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7513
7514 if (w)
7515 {
7516 XSETWINDOW (AREF (vector, i), w); ++i;
7517 AREF (vector, i) = w->buffer; ++i;
7518 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7519 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7520 }
7521 else
7522 {
7523 int end = i + 4;
7524 for (; i < end; ++i)
7525 AREF (vector, i) = Qnil;
7526 }
7527
7528 xassert (i == ASIZE (vector));
7529 return vector;
7530 }
7531
7532
7533 /* Restore global state from VECTOR which was created by
7534 with_echo_area_buffer_unwind_data. */
7535
7536 static Lisp_Object
7537 unwind_with_echo_area_buffer (vector)
7538 Lisp_Object vector;
7539 {
7540 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7541 Vdeactivate_mark = AREF (vector, 1);
7542 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7543
7544 if (WINDOWP (AREF (vector, 3)))
7545 {
7546 struct window *w;
7547 Lisp_Object buffer, charpos, bytepos;
7548
7549 w = XWINDOW (AREF (vector, 3));
7550 buffer = AREF (vector, 4);
7551 charpos = AREF (vector, 5);
7552 bytepos = AREF (vector, 6);
7553
7554 w->buffer = buffer;
7555 set_marker_both (w->pointm, buffer,
7556 XFASTINT (charpos), XFASTINT (bytepos));
7557 }
7558
7559 Vwith_echo_area_save_vector = vector;
7560 return Qnil;
7561 }
7562
7563
7564 /* Set up the echo area for use by print functions. MULTIBYTE_P
7565 non-zero means we will print multibyte. */
7566
7567 void
7568 setup_echo_area_for_printing (multibyte_p)
7569 int multibyte_p;
7570 {
7571 /* If we can't find an echo area any more, exit. */
7572 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7573 Fkill_emacs (Qnil);
7574
7575 ensure_echo_area_buffers ();
7576
7577 if (!message_buf_print)
7578 {
7579 /* A message has been output since the last time we printed.
7580 Choose a fresh echo area buffer. */
7581 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7582 echo_area_buffer[0] = echo_buffer[1];
7583 else
7584 echo_area_buffer[0] = echo_buffer[0];
7585
7586 /* Switch to that buffer and clear it. */
7587 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7588 current_buffer->truncate_lines = Qnil;
7589
7590 if (Z > BEG)
7591 {
7592 int count = SPECPDL_INDEX ();
7593 specbind (Qinhibit_read_only, Qt);
7594 /* Note that undo recording is always disabled. */
7595 del_range (BEG, Z);
7596 unbind_to (count, Qnil);
7597 }
7598 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7599
7600 /* Set up the buffer for the multibyteness we need. */
7601 if (multibyte_p
7602 != !NILP (current_buffer->enable_multibyte_characters))
7603 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7604
7605 /* Raise the frame containing the echo area. */
7606 if (minibuffer_auto_raise)
7607 {
7608 struct frame *sf = SELECTED_FRAME ();
7609 Lisp_Object mini_window;
7610 mini_window = FRAME_MINIBUF_WINDOW (sf);
7611 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7612 }
7613
7614 message_log_maybe_newline ();
7615 message_buf_print = 1;
7616 }
7617 else
7618 {
7619 if (NILP (echo_area_buffer[0]))
7620 {
7621 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7622 echo_area_buffer[0] = echo_buffer[1];
7623 else
7624 echo_area_buffer[0] = echo_buffer[0];
7625 }
7626
7627 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7628 {
7629 /* Someone switched buffers between print requests. */
7630 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7631 current_buffer->truncate_lines = Qnil;
7632 }
7633 }
7634 }
7635
7636
7637 /* Display an echo area message in window W. Value is non-zero if W's
7638 height is changed. If display_last_displayed_message_p is
7639 non-zero, display the message that was last displayed, otherwise
7640 display the current message. */
7641
7642 static int
7643 display_echo_area (w)
7644 struct window *w;
7645 {
7646 int i, no_message_p, window_height_changed_p, count;
7647
7648 /* Temporarily disable garbage collections while displaying the echo
7649 area. This is done because a GC can print a message itself.
7650 That message would modify the echo area buffer's contents while a
7651 redisplay of the buffer is going on, and seriously confuse
7652 redisplay. */
7653 count = inhibit_garbage_collection ();
7654
7655 /* If there is no message, we must call display_echo_area_1
7656 nevertheless because it resizes the window. But we will have to
7657 reset the echo_area_buffer in question to nil at the end because
7658 with_echo_area_buffer will sets it to an empty buffer. */
7659 i = display_last_displayed_message_p ? 1 : 0;
7660 no_message_p = NILP (echo_area_buffer[i]);
7661
7662 window_height_changed_p
7663 = with_echo_area_buffer (w, display_last_displayed_message_p,
7664 display_echo_area_1,
7665 (EMACS_INT) w, Qnil, 0, 0);
7666
7667 if (no_message_p)
7668 echo_area_buffer[i] = Qnil;
7669
7670 unbind_to (count, Qnil);
7671 return window_height_changed_p;
7672 }
7673
7674
7675 /* Helper for display_echo_area. Display the current buffer which
7676 contains the current echo area message in window W, a mini-window,
7677 a pointer to which is passed in A1. A2..A4 are currently not used.
7678 Change the height of W so that all of the message is displayed.
7679 Value is non-zero if height of W was changed. */
7680
7681 static int
7682 display_echo_area_1 (a1, a2, a3, a4)
7683 EMACS_INT a1;
7684 Lisp_Object a2;
7685 EMACS_INT a3, a4;
7686 {
7687 struct window *w = (struct window *) a1;
7688 Lisp_Object window;
7689 struct text_pos start;
7690 int window_height_changed_p = 0;
7691
7692 /* Do this before displaying, so that we have a large enough glyph
7693 matrix for the display. */
7694 window_height_changed_p = resize_mini_window (w, 0);
7695
7696 /* Display. */
7697 clear_glyph_matrix (w->desired_matrix);
7698 XSETWINDOW (window, w);
7699 SET_TEXT_POS (start, BEG, BEG_BYTE);
7700 try_window (window, start, 0);
7701
7702 return window_height_changed_p;
7703 }
7704
7705
7706 /* Resize the echo area window to exactly the size needed for the
7707 currently displayed message, if there is one. If a mini-buffer
7708 is active, don't shrink it. */
7709
7710 void
7711 resize_echo_area_exactly ()
7712 {
7713 if (BUFFERP (echo_area_buffer[0])
7714 && WINDOWP (echo_area_window))
7715 {
7716 struct window *w = XWINDOW (echo_area_window);
7717 int resized_p;
7718 Lisp_Object resize_exactly;
7719
7720 if (minibuf_level == 0)
7721 resize_exactly = Qt;
7722 else
7723 resize_exactly = Qnil;
7724
7725 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7726 (EMACS_INT) w, resize_exactly, 0, 0);
7727 if (resized_p)
7728 {
7729 ++windows_or_buffers_changed;
7730 ++update_mode_lines;
7731 redisplay_internal (0);
7732 }
7733 }
7734 }
7735
7736
7737 /* Callback function for with_echo_area_buffer, when used from
7738 resize_echo_area_exactly. A1 contains a pointer to the window to
7739 resize, EXACTLY non-nil means resize the mini-window exactly to the
7740 size of the text displayed. A3 and A4 are not used. Value is what
7741 resize_mini_window returns. */
7742
7743 static int
7744 resize_mini_window_1 (a1, exactly, a3, a4)
7745 EMACS_INT a1;
7746 Lisp_Object exactly;
7747 EMACS_INT a3, a4;
7748 {
7749 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7750 }
7751
7752
7753 /* Resize mini-window W to fit the size of its contents. EXACT:P
7754 means size the window exactly to the size needed. Otherwise, it's
7755 only enlarged until W's buffer is empty. Value is non-zero if
7756 the window height has been changed. */
7757
7758 int
7759 resize_mini_window (w, exact_p)
7760 struct window *w;
7761 int exact_p;
7762 {
7763 struct frame *f = XFRAME (w->frame);
7764 int window_height_changed_p = 0;
7765
7766 xassert (MINI_WINDOW_P (w));
7767
7768 /* Don't resize windows while redisplaying a window; it would
7769 confuse redisplay functions when the size of the window they are
7770 displaying changes from under them. Such a resizing can happen,
7771 for instance, when which-func prints a long message while
7772 we are running fontification-functions. We're running these
7773 functions with safe_call which binds inhibit-redisplay to t. */
7774 if (!NILP (Vinhibit_redisplay))
7775 return 0;
7776
7777 /* Nil means don't try to resize. */
7778 if (NILP (Vresize_mini_windows)
7779 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7780 return 0;
7781
7782 if (!FRAME_MINIBUF_ONLY_P (f))
7783 {
7784 struct it it;
7785 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7786 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7787 int height, max_height;
7788 int unit = FRAME_LINE_HEIGHT (f);
7789 struct text_pos start;
7790 struct buffer *old_current_buffer = NULL;
7791
7792 if (current_buffer != XBUFFER (w->buffer))
7793 {
7794 old_current_buffer = current_buffer;
7795 set_buffer_internal (XBUFFER (w->buffer));
7796 }
7797
7798 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7799
7800 /* Compute the max. number of lines specified by the user. */
7801 if (FLOATP (Vmax_mini_window_height))
7802 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7803 else if (INTEGERP (Vmax_mini_window_height))
7804 max_height = XINT (Vmax_mini_window_height);
7805 else
7806 max_height = total_height / 4;
7807
7808 /* Correct that max. height if it's bogus. */
7809 max_height = max (1, max_height);
7810 max_height = min (total_height, max_height);
7811
7812 /* Find out the height of the text in the window. */
7813 if (it.truncate_lines_p)
7814 height = 1;
7815 else
7816 {
7817 last_height = 0;
7818 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7819 if (it.max_ascent == 0 && it.max_descent == 0)
7820 height = it.current_y + last_height;
7821 else
7822 height = it.current_y + it.max_ascent + it.max_descent;
7823 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7824 height = (height + unit - 1) / unit;
7825 }
7826
7827 /* Compute a suitable window start. */
7828 if (height > max_height)
7829 {
7830 height = max_height;
7831 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7832 move_it_vertically_backward (&it, (height - 1) * unit);
7833 start = it.current.pos;
7834 }
7835 else
7836 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7837 SET_MARKER_FROM_TEXT_POS (w->start, start);
7838
7839 if (EQ (Vresize_mini_windows, Qgrow_only))
7840 {
7841 /* Let it grow only, until we display an empty message, in which
7842 case the window shrinks again. */
7843 if (height > WINDOW_TOTAL_LINES (w))
7844 {
7845 int old_height = WINDOW_TOTAL_LINES (w);
7846 freeze_window_starts (f, 1);
7847 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7848 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7849 }
7850 else if (height < WINDOW_TOTAL_LINES (w)
7851 && (exact_p || BEGV == ZV))
7852 {
7853 int old_height = WINDOW_TOTAL_LINES (w);
7854 freeze_window_starts (f, 0);
7855 shrink_mini_window (w);
7856 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7857 }
7858 }
7859 else
7860 {
7861 /* Always resize to exact size needed. */
7862 if (height > WINDOW_TOTAL_LINES (w))
7863 {
7864 int old_height = WINDOW_TOTAL_LINES (w);
7865 freeze_window_starts (f, 1);
7866 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7867 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7868 }
7869 else if (height < WINDOW_TOTAL_LINES (w))
7870 {
7871 int old_height = WINDOW_TOTAL_LINES (w);
7872 freeze_window_starts (f, 0);
7873 shrink_mini_window (w);
7874
7875 if (height)
7876 {
7877 freeze_window_starts (f, 1);
7878 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7879 }
7880
7881 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7882 }
7883 }
7884
7885 if (old_current_buffer)
7886 set_buffer_internal (old_current_buffer);
7887 }
7888
7889 return window_height_changed_p;
7890 }
7891
7892
7893 /* Value is the current message, a string, or nil if there is no
7894 current message. */
7895
7896 Lisp_Object
7897 current_message ()
7898 {
7899 Lisp_Object msg;
7900
7901 if (NILP (echo_area_buffer[0]))
7902 msg = Qnil;
7903 else
7904 {
7905 with_echo_area_buffer (0, 0, current_message_1,
7906 (EMACS_INT) &msg, Qnil, 0, 0);
7907 if (NILP (msg))
7908 echo_area_buffer[0] = Qnil;
7909 }
7910
7911 return msg;
7912 }
7913
7914
7915 static int
7916 current_message_1 (a1, a2, a3, a4)
7917 EMACS_INT a1;
7918 Lisp_Object a2;
7919 EMACS_INT a3, a4;
7920 {
7921 Lisp_Object *msg = (Lisp_Object *) a1;
7922
7923 if (Z > BEG)
7924 *msg = make_buffer_string (BEG, Z, 1);
7925 else
7926 *msg = Qnil;
7927 return 0;
7928 }
7929
7930
7931 /* Push the current message on Vmessage_stack for later restauration
7932 by restore_message. Value is non-zero if the current message isn't
7933 empty. This is a relatively infrequent operation, so it's not
7934 worth optimizing. */
7935
7936 int
7937 push_message ()
7938 {
7939 Lisp_Object msg;
7940 msg = current_message ();
7941 Vmessage_stack = Fcons (msg, Vmessage_stack);
7942 return STRINGP (msg);
7943 }
7944
7945
7946 /* Restore message display from the top of Vmessage_stack. */
7947
7948 void
7949 restore_message ()
7950 {
7951 Lisp_Object msg;
7952
7953 xassert (CONSP (Vmessage_stack));
7954 msg = XCAR (Vmessage_stack);
7955 if (STRINGP (msg))
7956 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7957 else
7958 message3_nolog (msg, 0, 0);
7959 }
7960
7961
7962 /* Handler for record_unwind_protect calling pop_message. */
7963
7964 Lisp_Object
7965 pop_message_unwind (dummy)
7966 Lisp_Object dummy;
7967 {
7968 pop_message ();
7969 return Qnil;
7970 }
7971
7972 /* Pop the top-most entry off Vmessage_stack. */
7973
7974 void
7975 pop_message ()
7976 {
7977 xassert (CONSP (Vmessage_stack));
7978 Vmessage_stack = XCDR (Vmessage_stack);
7979 }
7980
7981
7982 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7983 exits. If the stack is not empty, we have a missing pop_message
7984 somewhere. */
7985
7986 void
7987 check_message_stack ()
7988 {
7989 if (!NILP (Vmessage_stack))
7990 abort ();
7991 }
7992
7993
7994 /* Truncate to NCHARS what will be displayed in the echo area the next
7995 time we display it---but don't redisplay it now. */
7996
7997 void
7998 truncate_echo_area (nchars)
7999 int nchars;
8000 {
8001 if (nchars == 0)
8002 echo_area_buffer[0] = Qnil;
8003 /* A null message buffer means that the frame hasn't really been
8004 initialized yet. Error messages get reported properly by
8005 cmd_error, so this must be just an informative message; toss it. */
8006 else if (!noninteractive
8007 && INTERACTIVE
8008 && !NILP (echo_area_buffer[0]))
8009 {
8010 struct frame *sf = SELECTED_FRAME ();
8011 if (FRAME_MESSAGE_BUF (sf))
8012 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8013 }
8014 }
8015
8016
8017 /* Helper function for truncate_echo_area. Truncate the current
8018 message to at most NCHARS characters. */
8019
8020 static int
8021 truncate_message_1 (nchars, a2, a3, a4)
8022 EMACS_INT nchars;
8023 Lisp_Object a2;
8024 EMACS_INT a3, a4;
8025 {
8026 if (BEG + nchars < Z)
8027 del_range (BEG + nchars, Z);
8028 if (Z == BEG)
8029 echo_area_buffer[0] = Qnil;
8030 return 0;
8031 }
8032
8033
8034 /* Set the current message to a substring of S or STRING.
8035
8036 If STRING is a Lisp string, set the message to the first NBYTES
8037 bytes from STRING. NBYTES zero means use the whole string. If
8038 STRING is multibyte, the message will be displayed multibyte.
8039
8040 If S is not null, set the message to the first LEN bytes of S. LEN
8041 zero means use the whole string. MULTIBYTE_P non-zero means S is
8042 multibyte. Display the message multibyte in that case. */
8043
8044 void
8045 set_message (s, string, nbytes, multibyte_p)
8046 const char *s;
8047 Lisp_Object string;
8048 int nbytes, multibyte_p;
8049 {
8050 message_enable_multibyte
8051 = ((s && multibyte_p)
8052 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8053
8054 with_echo_area_buffer (0, 0, set_message_1,
8055 (EMACS_INT) s, string, nbytes, multibyte_p);
8056 message_buf_print = 0;
8057 help_echo_showing_p = 0;
8058 }
8059
8060
8061 /* Helper function for set_message. Arguments have the same meaning
8062 as there, with A1 corresponding to S and A2 corresponding to STRING
8063 This function is called with the echo area buffer being
8064 current. */
8065
8066 static int
8067 set_message_1 (a1, a2, nbytes, multibyte_p)
8068 EMACS_INT a1;
8069 Lisp_Object a2;
8070 EMACS_INT nbytes, multibyte_p;
8071 {
8072 const char *s = (const char *) a1;
8073 Lisp_Object string = a2;
8074
8075 /* Change multibyteness of the echo buffer appropriately. */
8076 if (message_enable_multibyte
8077 != !NILP (current_buffer->enable_multibyte_characters))
8078 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8079
8080 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8081
8082 /* Insert new message at BEG. */
8083 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8084 Ferase_buffer ();
8085
8086 if (STRINGP (string))
8087 {
8088 int nchars;
8089
8090 if (nbytes == 0)
8091 nbytes = SBYTES (string);
8092 nchars = string_byte_to_char (string, nbytes);
8093
8094 /* This function takes care of single/multibyte conversion. We
8095 just have to ensure that the echo area buffer has the right
8096 setting of enable_multibyte_characters. */
8097 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8098 }
8099 else if (s)
8100 {
8101 if (nbytes == 0)
8102 nbytes = strlen (s);
8103
8104 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8105 {
8106 /* Convert from multi-byte to single-byte. */
8107 int i, c, n;
8108 unsigned char work[1];
8109
8110 /* Convert a multibyte string to single-byte. */
8111 for (i = 0; i < nbytes; i += n)
8112 {
8113 c = string_char_and_length (s + i, nbytes - i, &n);
8114 work[0] = (ASCII_CHAR_P (c)
8115 ? c
8116 : multibyte_char_to_unibyte (c, Qnil));
8117 insert_1_both (work, 1, 1, 1, 0, 0);
8118 }
8119 }
8120 else if (!multibyte_p
8121 && !NILP (current_buffer->enable_multibyte_characters))
8122 {
8123 /* Convert from single-byte to multi-byte. */
8124 int i, c, n;
8125 const unsigned char *msg = (const unsigned char *) s;
8126 unsigned char str[MAX_MULTIBYTE_LENGTH];
8127
8128 /* Convert a single-byte string to multibyte. */
8129 for (i = 0; i < nbytes; i++)
8130 {
8131 c = msg[i];
8132 c = unibyte_char_to_multibyte (c);
8133 n = CHAR_STRING (c, str);
8134 insert_1_both (str, 1, n, 1, 0, 0);
8135 }
8136 }
8137 else
8138 insert_1 (s, nbytes, 1, 0, 0);
8139 }
8140
8141 return 0;
8142 }
8143
8144
8145 /* Clear messages. CURRENT_P non-zero means clear the current
8146 message. LAST_DISPLAYED_P non-zero means clear the message
8147 last displayed. */
8148
8149 void
8150 clear_message (current_p, last_displayed_p)
8151 int current_p, last_displayed_p;
8152 {
8153 if (current_p)
8154 {
8155 echo_area_buffer[0] = Qnil;
8156 message_cleared_p = 1;
8157 }
8158
8159 if (last_displayed_p)
8160 echo_area_buffer[1] = Qnil;
8161
8162 message_buf_print = 0;
8163 }
8164
8165 /* Clear garbaged frames.
8166
8167 This function is used where the old redisplay called
8168 redraw_garbaged_frames which in turn called redraw_frame which in
8169 turn called clear_frame. The call to clear_frame was a source of
8170 flickering. I believe a clear_frame is not necessary. It should
8171 suffice in the new redisplay to invalidate all current matrices,
8172 and ensure a complete redisplay of all windows. */
8173
8174 static void
8175 clear_garbaged_frames ()
8176 {
8177 if (frame_garbaged)
8178 {
8179 Lisp_Object tail, frame;
8180 int changed_count = 0;
8181
8182 FOR_EACH_FRAME (tail, frame)
8183 {
8184 struct frame *f = XFRAME (frame);
8185
8186 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8187 {
8188 if (f->resized_p)
8189 {
8190 Fredraw_frame (frame);
8191 f->force_flush_display_p = 1;
8192 }
8193 clear_current_matrices (f);
8194 changed_count++;
8195 f->garbaged = 0;
8196 f->resized_p = 0;
8197 }
8198 }
8199
8200 frame_garbaged = 0;
8201 if (changed_count)
8202 ++windows_or_buffers_changed;
8203 }
8204 }
8205
8206
8207 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8208 is non-zero update selected_frame. Value is non-zero if the
8209 mini-windows height has been changed. */
8210
8211 static int
8212 echo_area_display (update_frame_p)
8213 int update_frame_p;
8214 {
8215 Lisp_Object mini_window;
8216 struct window *w;
8217 struct frame *f;
8218 int window_height_changed_p = 0;
8219 struct frame *sf = SELECTED_FRAME ();
8220
8221 mini_window = FRAME_MINIBUF_WINDOW (sf);
8222 w = XWINDOW (mini_window);
8223 f = XFRAME (WINDOW_FRAME (w));
8224
8225 /* Don't display if frame is invisible or not yet initialized. */
8226 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8227 return 0;
8228
8229 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8230 #ifndef MAC_OS8
8231 #ifdef HAVE_WINDOW_SYSTEM
8232 /* When Emacs starts, selected_frame may be a visible terminal
8233 frame, even if we run under a window system. If we let this
8234 through, a message would be displayed on the terminal. */
8235 if (EQ (selected_frame, Vterminal_frame)
8236 && !NILP (Vwindow_system))
8237 return 0;
8238 #endif /* HAVE_WINDOW_SYSTEM */
8239 #endif
8240
8241 /* Redraw garbaged frames. */
8242 if (frame_garbaged)
8243 clear_garbaged_frames ();
8244
8245 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8246 {
8247 echo_area_window = mini_window;
8248 window_height_changed_p = display_echo_area (w);
8249 w->must_be_updated_p = 1;
8250
8251 /* Update the display, unless called from redisplay_internal.
8252 Also don't update the screen during redisplay itself. The
8253 update will happen at the end of redisplay, and an update
8254 here could cause confusion. */
8255 if (update_frame_p && !redisplaying_p)
8256 {
8257 int n = 0;
8258
8259 /* If the display update has been interrupted by pending
8260 input, update mode lines in the frame. Due to the
8261 pending input, it might have been that redisplay hasn't
8262 been called, so that mode lines above the echo area are
8263 garbaged. This looks odd, so we prevent it here. */
8264 if (!display_completed)
8265 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8266
8267 if (window_height_changed_p
8268 /* Don't do this if Emacs is shutting down. Redisplay
8269 needs to run hooks. */
8270 && !NILP (Vrun_hooks))
8271 {
8272 /* Must update other windows. Likewise as in other
8273 cases, don't let this update be interrupted by
8274 pending input. */
8275 int count = SPECPDL_INDEX ();
8276 specbind (Qredisplay_dont_pause, Qt);
8277 windows_or_buffers_changed = 1;
8278 redisplay_internal (0);
8279 unbind_to (count, Qnil);
8280 }
8281 else if (FRAME_WINDOW_P (f) && n == 0)
8282 {
8283 /* Window configuration is the same as before.
8284 Can do with a display update of the echo area,
8285 unless we displayed some mode lines. */
8286 update_single_window (w, 1);
8287 rif->flush_display (f);
8288 }
8289 else
8290 update_frame (f, 1, 1);
8291
8292 /* If cursor is in the echo area, make sure that the next
8293 redisplay displays the minibuffer, so that the cursor will
8294 be replaced with what the minibuffer wants. */
8295 if (cursor_in_echo_area)
8296 ++windows_or_buffers_changed;
8297 }
8298 }
8299 else if (!EQ (mini_window, selected_window))
8300 windows_or_buffers_changed++;
8301
8302 /* The current message is now also the last one displayed. */
8303 echo_area_buffer[1] = echo_area_buffer[0];
8304
8305 /* Prevent redisplay optimization in redisplay_internal by resetting
8306 this_line_start_pos. This is done because the mini-buffer now
8307 displays the message instead of its buffer text. */
8308 if (EQ (mini_window, selected_window))
8309 CHARPOS (this_line_start_pos) = 0;
8310
8311 return window_height_changed_p;
8312 }
8313
8314
8315 \f
8316 /***********************************************************************
8317 Mode Lines and Frame Titles
8318 ***********************************************************************/
8319
8320 /* A buffer for constructing non-propertized mode-line strings and
8321 frame titles in it; allocated from the heap in init_xdisp and
8322 resized as needed in store_mode_line_noprop_char. */
8323
8324 static char *mode_line_noprop_buf;
8325
8326 /* The buffer's end, and a current output position in it. */
8327
8328 static char *mode_line_noprop_buf_end;
8329 static char *mode_line_noprop_ptr;
8330
8331 #define MODE_LINE_NOPROP_LEN(start) \
8332 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8333
8334 static enum {
8335 MODE_LINE_DISPLAY = 0,
8336 MODE_LINE_TITLE,
8337 MODE_LINE_NOPROP,
8338 MODE_LINE_STRING
8339 } mode_line_target;
8340
8341 /* Alist that caches the results of :propertize.
8342 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8343 static Lisp_Object mode_line_proptrans_alist;
8344
8345 /* List of strings making up the mode-line. */
8346 static Lisp_Object mode_line_string_list;
8347
8348 /* Base face property when building propertized mode line string. */
8349 static Lisp_Object mode_line_string_face;
8350 static Lisp_Object mode_line_string_face_prop;
8351
8352
8353 /* Unwind data for mode line strings */
8354
8355 static Lisp_Object Vmode_line_unwind_vector;
8356
8357 static Lisp_Object
8358 format_mode_line_unwind_data (obuf)
8359 struct buffer *obuf;
8360 {
8361 Lisp_Object vector;
8362
8363 /* Reduce consing by keeping one vector in
8364 Vwith_echo_area_save_vector. */
8365 vector = Vmode_line_unwind_vector;
8366 Vmode_line_unwind_vector = Qnil;
8367
8368 if (NILP (vector))
8369 vector = Fmake_vector (make_number (7), Qnil);
8370
8371 AREF (vector, 0) = make_number (mode_line_target);
8372 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8373 AREF (vector, 2) = mode_line_string_list;
8374 AREF (vector, 3) = mode_line_proptrans_alist;
8375 AREF (vector, 4) = mode_line_string_face;
8376 AREF (vector, 5) = mode_line_string_face_prop;
8377
8378 if (obuf)
8379 XSETBUFFER (AREF (vector, 6), obuf);
8380 else
8381 AREF (vector, 6) = Qnil;
8382
8383 return vector;
8384 }
8385
8386 static Lisp_Object
8387 unwind_format_mode_line (vector)
8388 Lisp_Object vector;
8389 {
8390 mode_line_target = XINT (AREF (vector, 0));
8391 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8392 mode_line_string_list = AREF (vector, 2);
8393 mode_line_proptrans_alist = AREF (vector, 3);
8394 mode_line_string_face = AREF (vector, 4);
8395 mode_line_string_face_prop = AREF (vector, 5);
8396
8397 if (!NILP (AREF (vector, 6)))
8398 {
8399 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8400 AREF (vector, 6) = Qnil;
8401 }
8402
8403 Vmode_line_unwind_vector = vector;
8404 return Qnil;
8405 }
8406
8407
8408 /* Store a single character C for the frame title in mode_line_noprop_buf.
8409 Re-allocate mode_line_noprop_buf if necessary. */
8410
8411 static void
8412 #ifdef PROTOTYPES
8413 store_mode_line_noprop_char (char c)
8414 #else
8415 store_mode_line_noprop_char (c)
8416 char c;
8417 #endif
8418 {
8419 /* If output position has reached the end of the allocated buffer,
8420 double the buffer's size. */
8421 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8422 {
8423 int len = MODE_LINE_NOPROP_LEN (0);
8424 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8425 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8426 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8427 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8428 }
8429
8430 *mode_line_noprop_ptr++ = c;
8431 }
8432
8433
8434 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8435 mode_line_noprop_ptr. STR is the string to store. Do not copy
8436 characters that yield more columns than PRECISION; PRECISION <= 0
8437 means copy the whole string. Pad with spaces until FIELD_WIDTH
8438 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8439 pad. Called from display_mode_element when it is used to build a
8440 frame title. */
8441
8442 static int
8443 store_mode_line_noprop (str, field_width, precision)
8444 const unsigned char *str;
8445 int field_width, precision;
8446 {
8447 int n = 0;
8448 int dummy, nbytes;
8449
8450 /* Copy at most PRECISION chars from STR. */
8451 nbytes = strlen (str);
8452 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8453 while (nbytes--)
8454 store_mode_line_noprop_char (*str++);
8455
8456 /* Fill up with spaces until FIELD_WIDTH reached. */
8457 while (field_width > 0
8458 && n < field_width)
8459 {
8460 store_mode_line_noprop_char (' ');
8461 ++n;
8462 }
8463
8464 return n;
8465 }
8466
8467 /***********************************************************************
8468 Frame Titles
8469 ***********************************************************************/
8470
8471 #ifdef HAVE_WINDOW_SYSTEM
8472
8473 /* Set the title of FRAME, if it has changed. The title format is
8474 Vicon_title_format if FRAME is iconified, otherwise it is
8475 frame_title_format. */
8476
8477 static void
8478 x_consider_frame_title (frame)
8479 Lisp_Object frame;
8480 {
8481 struct frame *f = XFRAME (frame);
8482
8483 if (FRAME_WINDOW_P (f)
8484 || FRAME_MINIBUF_ONLY_P (f)
8485 || f->explicit_name)
8486 {
8487 /* Do we have more than one visible frame on this X display? */
8488 Lisp_Object tail;
8489 Lisp_Object fmt;
8490 int title_start;
8491 char *title;
8492 int len;
8493 struct it it;
8494 int count = SPECPDL_INDEX ();
8495
8496 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8497 {
8498 Lisp_Object other_frame = XCAR (tail);
8499 struct frame *tf = XFRAME (other_frame);
8500
8501 if (tf != f
8502 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8503 && !FRAME_MINIBUF_ONLY_P (tf)
8504 && !EQ (other_frame, tip_frame)
8505 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8506 break;
8507 }
8508
8509 /* Set global variable indicating that multiple frames exist. */
8510 multiple_frames = CONSP (tail);
8511
8512 /* Switch to the buffer of selected window of the frame. Set up
8513 mode_line_target so that display_mode_element will output into
8514 mode_line_noprop_buf; then display the title. */
8515 record_unwind_protect (unwind_format_mode_line,
8516 format_mode_line_unwind_data (current_buffer));
8517
8518 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8519 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8520
8521 mode_line_target = MODE_LINE_TITLE;
8522 title_start = MODE_LINE_NOPROP_LEN (0);
8523 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8524 NULL, DEFAULT_FACE_ID);
8525 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8526 len = MODE_LINE_NOPROP_LEN (title_start);
8527 title = mode_line_noprop_buf + title_start;
8528 unbind_to (count, Qnil);
8529
8530 /* Set the title only if it's changed. This avoids consing in
8531 the common case where it hasn't. (If it turns out that we've
8532 already wasted too much time by walking through the list with
8533 display_mode_element, then we might need to optimize at a
8534 higher level than this.) */
8535 if (! STRINGP (f->name)
8536 || SBYTES (f->name) != len
8537 || bcmp (title, SDATA (f->name), len) != 0)
8538 x_implicitly_set_name (f, make_string (title, len), Qnil);
8539 }
8540 }
8541
8542 #endif /* not HAVE_WINDOW_SYSTEM */
8543
8544
8545
8546 \f
8547 /***********************************************************************
8548 Menu Bars
8549 ***********************************************************************/
8550
8551
8552 /* Prepare for redisplay by updating menu-bar item lists when
8553 appropriate. This can call eval. */
8554
8555 void
8556 prepare_menu_bars ()
8557 {
8558 int all_windows;
8559 struct gcpro gcpro1, gcpro2;
8560 struct frame *f;
8561 Lisp_Object tooltip_frame;
8562
8563 #ifdef HAVE_WINDOW_SYSTEM
8564 tooltip_frame = tip_frame;
8565 #else
8566 tooltip_frame = Qnil;
8567 #endif
8568
8569 /* Update all frame titles based on their buffer names, etc. We do
8570 this before the menu bars so that the buffer-menu will show the
8571 up-to-date frame titles. */
8572 #ifdef HAVE_WINDOW_SYSTEM
8573 if (windows_or_buffers_changed || update_mode_lines)
8574 {
8575 Lisp_Object tail, frame;
8576
8577 FOR_EACH_FRAME (tail, frame)
8578 {
8579 f = XFRAME (frame);
8580 if (!EQ (frame, tooltip_frame)
8581 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8582 x_consider_frame_title (frame);
8583 }
8584 }
8585 #endif /* HAVE_WINDOW_SYSTEM */
8586
8587 /* Update the menu bar item lists, if appropriate. This has to be
8588 done before any actual redisplay or generation of display lines. */
8589 all_windows = (update_mode_lines
8590 || buffer_shared > 1
8591 || windows_or_buffers_changed);
8592 if (all_windows)
8593 {
8594 Lisp_Object tail, frame;
8595 int count = SPECPDL_INDEX ();
8596
8597 record_unwind_save_match_data ();
8598
8599 FOR_EACH_FRAME (tail, frame)
8600 {
8601 f = XFRAME (frame);
8602
8603 /* Ignore tooltip frame. */
8604 if (EQ (frame, tooltip_frame))
8605 continue;
8606
8607 /* If a window on this frame changed size, report that to
8608 the user and clear the size-change flag. */
8609 if (FRAME_WINDOW_SIZES_CHANGED (f))
8610 {
8611 Lisp_Object functions;
8612
8613 /* Clear flag first in case we get an error below. */
8614 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8615 functions = Vwindow_size_change_functions;
8616 GCPRO2 (tail, functions);
8617
8618 while (CONSP (functions))
8619 {
8620 call1 (XCAR (functions), frame);
8621 functions = XCDR (functions);
8622 }
8623 UNGCPRO;
8624 }
8625
8626 GCPRO1 (tail);
8627 update_menu_bar (f, 0);
8628 #ifdef HAVE_WINDOW_SYSTEM
8629 update_tool_bar (f, 0);
8630 #endif
8631 UNGCPRO;
8632 }
8633
8634 unbind_to (count, Qnil);
8635 }
8636 else
8637 {
8638 struct frame *sf = SELECTED_FRAME ();
8639 update_menu_bar (sf, 1);
8640 #ifdef HAVE_WINDOW_SYSTEM
8641 update_tool_bar (sf, 1);
8642 #endif
8643 }
8644
8645 /* Motif needs this. See comment in xmenu.c. Turn it off when
8646 pending_menu_activation is not defined. */
8647 #ifdef USE_X_TOOLKIT
8648 pending_menu_activation = 0;
8649 #endif
8650 }
8651
8652
8653 /* Update the menu bar item list for frame F. This has to be done
8654 before we start to fill in any display lines, because it can call
8655 eval.
8656
8657 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8658
8659 static void
8660 update_menu_bar (f, save_match_data)
8661 struct frame *f;
8662 int save_match_data;
8663 {
8664 Lisp_Object window;
8665 register struct window *w;
8666
8667 /* If called recursively during a menu update, do nothing. This can
8668 happen when, for instance, an activate-menubar-hook causes a
8669 redisplay. */
8670 if (inhibit_menubar_update)
8671 return;
8672
8673 window = FRAME_SELECTED_WINDOW (f);
8674 w = XWINDOW (window);
8675
8676 #if 0 /* The if statement below this if statement used to include the
8677 condition !NILP (w->update_mode_line), rather than using
8678 update_mode_lines directly, and this if statement may have
8679 been added to make that condition work. Now the if
8680 statement below matches its comment, this isn't needed. */
8681 if (update_mode_lines)
8682 w->update_mode_line = Qt;
8683 #endif
8684
8685 if (FRAME_WINDOW_P (f)
8686 ?
8687 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8688 || defined (USE_GTK)
8689 FRAME_EXTERNAL_MENU_BAR (f)
8690 #else
8691 FRAME_MENU_BAR_LINES (f) > 0
8692 #endif
8693 : FRAME_MENU_BAR_LINES (f) > 0)
8694 {
8695 /* If the user has switched buffers or windows, we need to
8696 recompute to reflect the new bindings. But we'll
8697 recompute when update_mode_lines is set too; that means
8698 that people can use force-mode-line-update to request
8699 that the menu bar be recomputed. The adverse effect on
8700 the rest of the redisplay algorithm is about the same as
8701 windows_or_buffers_changed anyway. */
8702 if (windows_or_buffers_changed
8703 /* This used to test w->update_mode_line, but we believe
8704 there is no need to recompute the menu in that case. */
8705 || update_mode_lines
8706 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8707 < BUF_MODIFF (XBUFFER (w->buffer)))
8708 != !NILP (w->last_had_star))
8709 || ((!NILP (Vtransient_mark_mode)
8710 && !NILP (XBUFFER (w->buffer)->mark_active))
8711 != !NILP (w->region_showing)))
8712 {
8713 struct buffer *prev = current_buffer;
8714 int count = SPECPDL_INDEX ();
8715
8716 specbind (Qinhibit_menubar_update, Qt);
8717
8718 set_buffer_internal_1 (XBUFFER (w->buffer));
8719 if (save_match_data)
8720 record_unwind_save_match_data ();
8721 if (NILP (Voverriding_local_map_menu_flag))
8722 {
8723 specbind (Qoverriding_terminal_local_map, Qnil);
8724 specbind (Qoverriding_local_map, Qnil);
8725 }
8726
8727 /* Run the Lucid hook. */
8728 safe_run_hooks (Qactivate_menubar_hook);
8729
8730 /* If it has changed current-menubar from previous value,
8731 really recompute the menu-bar from the value. */
8732 if (! NILP (Vlucid_menu_bar_dirty_flag))
8733 call0 (Qrecompute_lucid_menubar);
8734
8735 safe_run_hooks (Qmenu_bar_update_hook);
8736 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8737
8738 /* Redisplay the menu bar in case we changed it. */
8739 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8740 || defined (USE_GTK)
8741 if (FRAME_WINDOW_P (f)
8742 #if defined (MAC_OS)
8743 /* All frames on Mac OS share the same menubar. So only the
8744 selected frame should be allowed to set it. */
8745 && f == SELECTED_FRAME ()
8746 #endif
8747 )
8748 set_frame_menubar (f, 0, 0);
8749 else
8750 /* On a terminal screen, the menu bar is an ordinary screen
8751 line, and this makes it get updated. */
8752 w->update_mode_line = Qt;
8753 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8754 /* In the non-toolkit version, the menu bar is an ordinary screen
8755 line, and this makes it get updated. */
8756 w->update_mode_line = Qt;
8757 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8758
8759 unbind_to (count, Qnil);
8760 set_buffer_internal_1 (prev);
8761 }
8762 }
8763 }
8764
8765
8766 \f
8767 /***********************************************************************
8768 Output Cursor
8769 ***********************************************************************/
8770
8771 #ifdef HAVE_WINDOW_SYSTEM
8772
8773 /* EXPORT:
8774 Nominal cursor position -- where to draw output.
8775 HPOS and VPOS are window relative glyph matrix coordinates.
8776 X and Y are window relative pixel coordinates. */
8777
8778 struct cursor_pos output_cursor;
8779
8780
8781 /* EXPORT:
8782 Set the global variable output_cursor to CURSOR. All cursor
8783 positions are relative to updated_window. */
8784
8785 void
8786 set_output_cursor (cursor)
8787 struct cursor_pos *cursor;
8788 {
8789 output_cursor.hpos = cursor->hpos;
8790 output_cursor.vpos = cursor->vpos;
8791 output_cursor.x = cursor->x;
8792 output_cursor.y = cursor->y;
8793 }
8794
8795
8796 /* EXPORT for RIF:
8797 Set a nominal cursor position.
8798
8799 HPOS and VPOS are column/row positions in a window glyph matrix. X
8800 and Y are window text area relative pixel positions.
8801
8802 If this is done during an update, updated_window will contain the
8803 window that is being updated and the position is the future output
8804 cursor position for that window. If updated_window is null, use
8805 selected_window and display the cursor at the given position. */
8806
8807 void
8808 x_cursor_to (vpos, hpos, y, x)
8809 int vpos, hpos, y, x;
8810 {
8811 struct window *w;
8812
8813 /* If updated_window is not set, work on selected_window. */
8814 if (updated_window)
8815 w = updated_window;
8816 else
8817 w = XWINDOW (selected_window);
8818
8819 /* Set the output cursor. */
8820 output_cursor.hpos = hpos;
8821 output_cursor.vpos = vpos;
8822 output_cursor.x = x;
8823 output_cursor.y = y;
8824
8825 /* If not called as part of an update, really display the cursor.
8826 This will also set the cursor position of W. */
8827 if (updated_window == NULL)
8828 {
8829 BLOCK_INPUT;
8830 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8831 if (rif->flush_display_optional)
8832 rif->flush_display_optional (SELECTED_FRAME ());
8833 UNBLOCK_INPUT;
8834 }
8835 }
8836
8837 #endif /* HAVE_WINDOW_SYSTEM */
8838
8839 \f
8840 /***********************************************************************
8841 Tool-bars
8842 ***********************************************************************/
8843
8844 #ifdef HAVE_WINDOW_SYSTEM
8845
8846 /* Where the mouse was last time we reported a mouse event. */
8847
8848 FRAME_PTR last_mouse_frame;
8849
8850 /* Tool-bar item index of the item on which a mouse button was pressed
8851 or -1. */
8852
8853 int last_tool_bar_item;
8854
8855
8856 /* Update the tool-bar item list for frame F. This has to be done
8857 before we start to fill in any display lines. Called from
8858 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8859 and restore it here. */
8860
8861 static void
8862 update_tool_bar (f, save_match_data)
8863 struct frame *f;
8864 int save_match_data;
8865 {
8866 #ifdef USE_GTK
8867 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8868 #else
8869 int do_update = WINDOWP (f->tool_bar_window)
8870 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8871 #endif
8872
8873 if (do_update)
8874 {
8875 Lisp_Object window;
8876 struct window *w;
8877
8878 window = FRAME_SELECTED_WINDOW (f);
8879 w = XWINDOW (window);
8880
8881 /* If the user has switched buffers or windows, we need to
8882 recompute to reflect the new bindings. But we'll
8883 recompute when update_mode_lines is set too; that means
8884 that people can use force-mode-line-update to request
8885 that the menu bar be recomputed. The adverse effect on
8886 the rest of the redisplay algorithm is about the same as
8887 windows_or_buffers_changed anyway. */
8888 if (windows_or_buffers_changed
8889 || !NILP (w->update_mode_line)
8890 || update_mode_lines
8891 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8892 < BUF_MODIFF (XBUFFER (w->buffer)))
8893 != !NILP (w->last_had_star))
8894 || ((!NILP (Vtransient_mark_mode)
8895 && !NILP (XBUFFER (w->buffer)->mark_active))
8896 != !NILP (w->region_showing)))
8897 {
8898 struct buffer *prev = current_buffer;
8899 int count = SPECPDL_INDEX ();
8900 Lisp_Object new_tool_bar;
8901 int new_n_tool_bar;
8902 struct gcpro gcpro1;
8903
8904 /* Set current_buffer to the buffer of the selected
8905 window of the frame, so that we get the right local
8906 keymaps. */
8907 set_buffer_internal_1 (XBUFFER (w->buffer));
8908
8909 /* Save match data, if we must. */
8910 if (save_match_data)
8911 record_unwind_save_match_data ();
8912
8913 /* Make sure that we don't accidentally use bogus keymaps. */
8914 if (NILP (Voverriding_local_map_menu_flag))
8915 {
8916 specbind (Qoverriding_terminal_local_map, Qnil);
8917 specbind (Qoverriding_local_map, Qnil);
8918 }
8919
8920 GCPRO1 (new_tool_bar);
8921
8922 /* Build desired tool-bar items from keymaps. */
8923 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8924 &new_n_tool_bar);
8925
8926 /* Redisplay the tool-bar if we changed it. */
8927 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8928 {
8929 /* Redisplay that happens asynchronously due to an expose event
8930 may access f->tool_bar_items. Make sure we update both
8931 variables within BLOCK_INPUT so no such event interrupts. */
8932 BLOCK_INPUT;
8933 f->tool_bar_items = new_tool_bar;
8934 f->n_tool_bar_items = new_n_tool_bar;
8935 w->update_mode_line = Qt;
8936 UNBLOCK_INPUT;
8937 }
8938
8939 UNGCPRO;
8940
8941 unbind_to (count, Qnil);
8942 set_buffer_internal_1 (prev);
8943 }
8944 }
8945 }
8946
8947
8948 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8949 F's desired tool-bar contents. F->tool_bar_items must have
8950 been set up previously by calling prepare_menu_bars. */
8951
8952 static void
8953 build_desired_tool_bar_string (f)
8954 struct frame *f;
8955 {
8956 int i, size, size_needed;
8957 struct gcpro gcpro1, gcpro2, gcpro3;
8958 Lisp_Object image, plist, props;
8959
8960 image = plist = props = Qnil;
8961 GCPRO3 (image, plist, props);
8962
8963 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8964 Otherwise, make a new string. */
8965
8966 /* The size of the string we might be able to reuse. */
8967 size = (STRINGP (f->desired_tool_bar_string)
8968 ? SCHARS (f->desired_tool_bar_string)
8969 : 0);
8970
8971 /* We need one space in the string for each image. */
8972 size_needed = f->n_tool_bar_items;
8973
8974 /* Reuse f->desired_tool_bar_string, if possible. */
8975 if (size < size_needed || NILP (f->desired_tool_bar_string))
8976 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8977 make_number (' '));
8978 else
8979 {
8980 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8981 Fremove_text_properties (make_number (0), make_number (size),
8982 props, f->desired_tool_bar_string);
8983 }
8984
8985 /* Put a `display' property on the string for the images to display,
8986 put a `menu_item' property on tool-bar items with a value that
8987 is the index of the item in F's tool-bar item vector. */
8988 for (i = 0; i < f->n_tool_bar_items; ++i)
8989 {
8990 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8991
8992 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8993 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8994 int hmargin, vmargin, relief, idx, end;
8995 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8996
8997 /* If image is a vector, choose the image according to the
8998 button state. */
8999 image = PROP (TOOL_BAR_ITEM_IMAGES);
9000 if (VECTORP (image))
9001 {
9002 if (enabled_p)
9003 idx = (selected_p
9004 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9005 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9006 else
9007 idx = (selected_p
9008 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9009 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9010
9011 xassert (ASIZE (image) >= idx);
9012 image = AREF (image, idx);
9013 }
9014 else
9015 idx = -1;
9016
9017 /* Ignore invalid image specifications. */
9018 if (!valid_image_p (image))
9019 continue;
9020
9021 /* Display the tool-bar button pressed, or depressed. */
9022 plist = Fcopy_sequence (XCDR (image));
9023
9024 /* Compute margin and relief to draw. */
9025 relief = (tool_bar_button_relief >= 0
9026 ? tool_bar_button_relief
9027 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9028 hmargin = vmargin = relief;
9029
9030 if (INTEGERP (Vtool_bar_button_margin)
9031 && XINT (Vtool_bar_button_margin) > 0)
9032 {
9033 hmargin += XFASTINT (Vtool_bar_button_margin);
9034 vmargin += XFASTINT (Vtool_bar_button_margin);
9035 }
9036 else if (CONSP (Vtool_bar_button_margin))
9037 {
9038 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9039 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9040 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9041
9042 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9043 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9044 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9045 }
9046
9047 if (auto_raise_tool_bar_buttons_p)
9048 {
9049 /* Add a `:relief' property to the image spec if the item is
9050 selected. */
9051 if (selected_p)
9052 {
9053 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9054 hmargin -= relief;
9055 vmargin -= relief;
9056 }
9057 }
9058 else
9059 {
9060 /* If image is selected, display it pressed, i.e. with a
9061 negative relief. If it's not selected, display it with a
9062 raised relief. */
9063 plist = Fplist_put (plist, QCrelief,
9064 (selected_p
9065 ? make_number (-relief)
9066 : make_number (relief)));
9067 hmargin -= relief;
9068 vmargin -= relief;
9069 }
9070
9071 /* Put a margin around the image. */
9072 if (hmargin || vmargin)
9073 {
9074 if (hmargin == vmargin)
9075 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9076 else
9077 plist = Fplist_put (plist, QCmargin,
9078 Fcons (make_number (hmargin),
9079 make_number (vmargin)));
9080 }
9081
9082 /* If button is not enabled, and we don't have special images
9083 for the disabled state, make the image appear disabled by
9084 applying an appropriate algorithm to it. */
9085 if (!enabled_p && idx < 0)
9086 plist = Fplist_put (plist, QCconversion, Qdisabled);
9087
9088 /* Put a `display' text property on the string for the image to
9089 display. Put a `menu-item' property on the string that gives
9090 the start of this item's properties in the tool-bar items
9091 vector. */
9092 image = Fcons (Qimage, plist);
9093 props = list4 (Qdisplay, image,
9094 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9095
9096 /* Let the last image hide all remaining spaces in the tool bar
9097 string. The string can be longer than needed when we reuse a
9098 previous string. */
9099 if (i + 1 == f->n_tool_bar_items)
9100 end = SCHARS (f->desired_tool_bar_string);
9101 else
9102 end = i + 1;
9103 Fadd_text_properties (make_number (i), make_number (end),
9104 props, f->desired_tool_bar_string);
9105 #undef PROP
9106 }
9107
9108 UNGCPRO;
9109 }
9110
9111
9112 /* Display one line of the tool-bar of frame IT->f. */
9113
9114 static void
9115 display_tool_bar_line (it)
9116 struct it *it;
9117 {
9118 struct glyph_row *row = it->glyph_row;
9119 int max_x = it->last_visible_x;
9120 struct glyph *last;
9121
9122 prepare_desired_row (row);
9123 row->y = it->current_y;
9124
9125 /* Note that this isn't made use of if the face hasn't a box,
9126 so there's no need to check the face here. */
9127 it->start_of_box_run_p = 1;
9128
9129 while (it->current_x < max_x)
9130 {
9131 int x_before, x, n_glyphs_before, i, nglyphs;
9132
9133 /* Get the next display element. */
9134 if (!get_next_display_element (it))
9135 break;
9136
9137 /* Produce glyphs. */
9138 x_before = it->current_x;
9139 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9140 PRODUCE_GLYPHS (it);
9141
9142 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9143 i = 0;
9144 x = x_before;
9145 while (i < nglyphs)
9146 {
9147 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9148
9149 if (x + glyph->pixel_width > max_x)
9150 {
9151 /* Glyph doesn't fit on line. */
9152 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9153 it->current_x = x;
9154 goto out;
9155 }
9156
9157 ++it->hpos;
9158 x += glyph->pixel_width;
9159 ++i;
9160 }
9161
9162 /* Stop at line ends. */
9163 if (ITERATOR_AT_END_OF_LINE_P (it))
9164 break;
9165
9166 set_iterator_to_next (it, 1);
9167 }
9168
9169 out:;
9170
9171 row->displays_text_p = row->used[TEXT_AREA] != 0;
9172 extend_face_to_end_of_line (it);
9173 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9174 last->right_box_line_p = 1;
9175 if (last == row->glyphs[TEXT_AREA])
9176 last->left_box_line_p = 1;
9177 compute_line_metrics (it);
9178
9179 /* If line is empty, make it occupy the rest of the tool-bar. */
9180 if (!row->displays_text_p)
9181 {
9182 row->height = row->phys_height = it->last_visible_y - row->y;
9183 row->ascent = row->phys_ascent = 0;
9184 row->extra_line_spacing = 0;
9185 }
9186
9187 row->full_width_p = 1;
9188 row->continued_p = 0;
9189 row->truncated_on_left_p = 0;
9190 row->truncated_on_right_p = 0;
9191
9192 it->current_x = it->hpos = 0;
9193 it->current_y += row->height;
9194 ++it->vpos;
9195 ++it->glyph_row;
9196 }
9197
9198
9199 /* Value is the number of screen lines needed to make all tool-bar
9200 items of frame F visible. */
9201
9202 static int
9203 tool_bar_lines_needed (f)
9204 struct frame *f;
9205 {
9206 struct window *w = XWINDOW (f->tool_bar_window);
9207 struct it it;
9208
9209 /* Initialize an iterator for iteration over
9210 F->desired_tool_bar_string in the tool-bar window of frame F. */
9211 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9212 it.first_visible_x = 0;
9213 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9214 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9215
9216 while (!ITERATOR_AT_END_P (&it))
9217 {
9218 it.glyph_row = w->desired_matrix->rows;
9219 clear_glyph_row (it.glyph_row);
9220 display_tool_bar_line (&it);
9221 }
9222
9223 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9224 }
9225
9226
9227 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9228 0, 1, 0,
9229 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9230 (frame)
9231 Lisp_Object frame;
9232 {
9233 struct frame *f;
9234 struct window *w;
9235 int nlines = 0;
9236
9237 if (NILP (frame))
9238 frame = selected_frame;
9239 else
9240 CHECK_FRAME (frame);
9241 f = XFRAME (frame);
9242
9243 if (WINDOWP (f->tool_bar_window)
9244 || (w = XWINDOW (f->tool_bar_window),
9245 WINDOW_TOTAL_LINES (w) > 0))
9246 {
9247 update_tool_bar (f, 1);
9248 if (f->n_tool_bar_items)
9249 {
9250 build_desired_tool_bar_string (f);
9251 nlines = tool_bar_lines_needed (f);
9252 }
9253 }
9254
9255 return make_number (nlines);
9256 }
9257
9258
9259 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9260 height should be changed. */
9261
9262 static int
9263 redisplay_tool_bar (f)
9264 struct frame *f;
9265 {
9266 struct window *w;
9267 struct it it;
9268 struct glyph_row *row;
9269 int change_height_p = 0;
9270
9271 #ifdef USE_GTK
9272 if (FRAME_EXTERNAL_TOOL_BAR (f))
9273 update_frame_tool_bar (f);
9274 return 0;
9275 #endif
9276
9277 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9278 do anything. This means you must start with tool-bar-lines
9279 non-zero to get the auto-sizing effect. Or in other words, you
9280 can turn off tool-bars by specifying tool-bar-lines zero. */
9281 if (!WINDOWP (f->tool_bar_window)
9282 || (w = XWINDOW (f->tool_bar_window),
9283 WINDOW_TOTAL_LINES (w) == 0))
9284 return 0;
9285
9286 /* Set up an iterator for the tool-bar window. */
9287 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9288 it.first_visible_x = 0;
9289 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9290 row = it.glyph_row;
9291
9292 /* Build a string that represents the contents of the tool-bar. */
9293 build_desired_tool_bar_string (f);
9294 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9295
9296 /* Display as many lines as needed to display all tool-bar items. */
9297 while (it.current_y < it.last_visible_y)
9298 display_tool_bar_line (&it);
9299
9300 /* It doesn't make much sense to try scrolling in the tool-bar
9301 window, so don't do it. */
9302 w->desired_matrix->no_scrolling_p = 1;
9303 w->must_be_updated_p = 1;
9304
9305 if (auto_resize_tool_bars_p)
9306 {
9307 int nlines;
9308
9309 /* If we couldn't display everything, change the tool-bar's
9310 height. */
9311 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9312 change_height_p = 1;
9313
9314 /* If there are blank lines at the end, except for a partially
9315 visible blank line at the end that is smaller than
9316 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9317 row = it.glyph_row - 1;
9318 if (!row->displays_text_p
9319 && row->height >= FRAME_LINE_HEIGHT (f))
9320 change_height_p = 1;
9321
9322 /* If row displays tool-bar items, but is partially visible,
9323 change the tool-bar's height. */
9324 if (row->displays_text_p
9325 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9326 change_height_p = 1;
9327
9328 /* Resize windows as needed by changing the `tool-bar-lines'
9329 frame parameter. */
9330 if (change_height_p
9331 && (nlines = tool_bar_lines_needed (f),
9332 nlines != WINDOW_TOTAL_LINES (w)))
9333 {
9334 extern Lisp_Object Qtool_bar_lines;
9335 Lisp_Object frame;
9336 int old_height = WINDOW_TOTAL_LINES (w);
9337
9338 XSETFRAME (frame, f);
9339 clear_glyph_matrix (w->desired_matrix);
9340 Fmodify_frame_parameters (frame,
9341 Fcons (Fcons (Qtool_bar_lines,
9342 make_number (nlines)),
9343 Qnil));
9344 if (WINDOW_TOTAL_LINES (w) != old_height)
9345 fonts_changed_p = 1;
9346 }
9347 }
9348
9349 return change_height_p;
9350 }
9351
9352
9353 /* Get information about the tool-bar item which is displayed in GLYPH
9354 on frame F. Return in *PROP_IDX the index where tool-bar item
9355 properties start in F->tool_bar_items. Value is zero if
9356 GLYPH doesn't display a tool-bar item. */
9357
9358 static int
9359 tool_bar_item_info (f, glyph, prop_idx)
9360 struct frame *f;
9361 struct glyph *glyph;
9362 int *prop_idx;
9363 {
9364 Lisp_Object prop;
9365 int success_p;
9366 int charpos;
9367
9368 /* This function can be called asynchronously, which means we must
9369 exclude any possibility that Fget_text_property signals an
9370 error. */
9371 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9372 charpos = max (0, charpos);
9373
9374 /* Get the text property `menu-item' at pos. The value of that
9375 property is the start index of this item's properties in
9376 F->tool_bar_items. */
9377 prop = Fget_text_property (make_number (charpos),
9378 Qmenu_item, f->current_tool_bar_string);
9379 if (INTEGERP (prop))
9380 {
9381 *prop_idx = XINT (prop);
9382 success_p = 1;
9383 }
9384 else
9385 success_p = 0;
9386
9387 return success_p;
9388 }
9389
9390 \f
9391 /* Get information about the tool-bar item at position X/Y on frame F.
9392 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9393 the current matrix of the tool-bar window of F, or NULL if not
9394 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9395 item in F->tool_bar_items. Value is
9396
9397 -1 if X/Y is not on a tool-bar item
9398 0 if X/Y is on the same item that was highlighted before.
9399 1 otherwise. */
9400
9401 static int
9402 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9403 struct frame *f;
9404 int x, y;
9405 struct glyph **glyph;
9406 int *hpos, *vpos, *prop_idx;
9407 {
9408 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9409 struct window *w = XWINDOW (f->tool_bar_window);
9410 int area;
9411
9412 /* Find the glyph under X/Y. */
9413 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9414 if (*glyph == NULL)
9415 return -1;
9416
9417 /* Get the start of this tool-bar item's properties in
9418 f->tool_bar_items. */
9419 if (!tool_bar_item_info (f, *glyph, prop_idx))
9420 return -1;
9421
9422 /* Is mouse on the highlighted item? */
9423 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9424 && *vpos >= dpyinfo->mouse_face_beg_row
9425 && *vpos <= dpyinfo->mouse_face_end_row
9426 && (*vpos > dpyinfo->mouse_face_beg_row
9427 || *hpos >= dpyinfo->mouse_face_beg_col)
9428 && (*vpos < dpyinfo->mouse_face_end_row
9429 || *hpos < dpyinfo->mouse_face_end_col
9430 || dpyinfo->mouse_face_past_end))
9431 return 0;
9432
9433 return 1;
9434 }
9435
9436
9437 /* EXPORT:
9438 Handle mouse button event on the tool-bar of frame F, at
9439 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9440 0 for button release. MODIFIERS is event modifiers for button
9441 release. */
9442
9443 void
9444 handle_tool_bar_click (f, x, y, down_p, modifiers)
9445 struct frame *f;
9446 int x, y, down_p;
9447 unsigned int modifiers;
9448 {
9449 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9450 struct window *w = XWINDOW (f->tool_bar_window);
9451 int hpos, vpos, prop_idx;
9452 struct glyph *glyph;
9453 Lisp_Object enabled_p;
9454
9455 /* If not on the highlighted tool-bar item, return. */
9456 frame_to_window_pixel_xy (w, &x, &y);
9457 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9458 return;
9459
9460 /* If item is disabled, do nothing. */
9461 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9462 if (NILP (enabled_p))
9463 return;
9464
9465 if (down_p)
9466 {
9467 /* Show item in pressed state. */
9468 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9469 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9470 last_tool_bar_item = prop_idx;
9471 }
9472 else
9473 {
9474 Lisp_Object key, frame;
9475 struct input_event event;
9476 EVENT_INIT (event);
9477
9478 /* Show item in released state. */
9479 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9480 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9481
9482 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9483
9484 XSETFRAME (frame, f);
9485 event.kind = TOOL_BAR_EVENT;
9486 event.frame_or_window = frame;
9487 event.arg = frame;
9488 kbd_buffer_store_event (&event);
9489
9490 event.kind = TOOL_BAR_EVENT;
9491 event.frame_or_window = frame;
9492 event.arg = key;
9493 event.modifiers = modifiers;
9494 kbd_buffer_store_event (&event);
9495 last_tool_bar_item = -1;
9496 }
9497 }
9498
9499
9500 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9501 tool-bar window-relative coordinates X/Y. Called from
9502 note_mouse_highlight. */
9503
9504 static void
9505 note_tool_bar_highlight (f, x, y)
9506 struct frame *f;
9507 int x, y;
9508 {
9509 Lisp_Object window = f->tool_bar_window;
9510 struct window *w = XWINDOW (window);
9511 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9512 int hpos, vpos;
9513 struct glyph *glyph;
9514 struct glyph_row *row;
9515 int i;
9516 Lisp_Object enabled_p;
9517 int prop_idx;
9518 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9519 int mouse_down_p, rc;
9520
9521 /* Function note_mouse_highlight is called with negative x(y
9522 values when mouse moves outside of the frame. */
9523 if (x <= 0 || y <= 0)
9524 {
9525 clear_mouse_face (dpyinfo);
9526 return;
9527 }
9528
9529 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9530 if (rc < 0)
9531 {
9532 /* Not on tool-bar item. */
9533 clear_mouse_face (dpyinfo);
9534 return;
9535 }
9536 else if (rc == 0)
9537 /* On same tool-bar item as before. */
9538 goto set_help_echo;
9539
9540 clear_mouse_face (dpyinfo);
9541
9542 /* Mouse is down, but on different tool-bar item? */
9543 mouse_down_p = (dpyinfo->grabbed
9544 && f == last_mouse_frame
9545 && FRAME_LIVE_P (f));
9546 if (mouse_down_p
9547 && last_tool_bar_item != prop_idx)
9548 return;
9549
9550 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9551 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9552
9553 /* If tool-bar item is not enabled, don't highlight it. */
9554 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9555 if (!NILP (enabled_p))
9556 {
9557 /* Compute the x-position of the glyph. In front and past the
9558 image is a space. We include this in the highlighted area. */
9559 row = MATRIX_ROW (w->current_matrix, vpos);
9560 for (i = x = 0; i < hpos; ++i)
9561 x += row->glyphs[TEXT_AREA][i].pixel_width;
9562
9563 /* Record this as the current active region. */
9564 dpyinfo->mouse_face_beg_col = hpos;
9565 dpyinfo->mouse_face_beg_row = vpos;
9566 dpyinfo->mouse_face_beg_x = x;
9567 dpyinfo->mouse_face_beg_y = row->y;
9568 dpyinfo->mouse_face_past_end = 0;
9569
9570 dpyinfo->mouse_face_end_col = hpos + 1;
9571 dpyinfo->mouse_face_end_row = vpos;
9572 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9573 dpyinfo->mouse_face_end_y = row->y;
9574 dpyinfo->mouse_face_window = window;
9575 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9576
9577 /* Display it as active. */
9578 show_mouse_face (dpyinfo, draw);
9579 dpyinfo->mouse_face_image_state = draw;
9580 }
9581
9582 set_help_echo:
9583
9584 /* Set help_echo_string to a help string to display for this tool-bar item.
9585 XTread_socket does the rest. */
9586 help_echo_object = help_echo_window = Qnil;
9587 help_echo_pos = -1;
9588 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9589 if (NILP (help_echo_string))
9590 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9591 }
9592
9593 #endif /* HAVE_WINDOW_SYSTEM */
9594
9595
9596 \f
9597 /************************************************************************
9598 Horizontal scrolling
9599 ************************************************************************/
9600
9601 static int hscroll_window_tree P_ ((Lisp_Object));
9602 static int hscroll_windows P_ ((Lisp_Object));
9603
9604 /* For all leaf windows in the window tree rooted at WINDOW, set their
9605 hscroll value so that PT is (i) visible in the window, and (ii) so
9606 that it is not within a certain margin at the window's left and
9607 right border. Value is non-zero if any window's hscroll has been
9608 changed. */
9609
9610 static int
9611 hscroll_window_tree (window)
9612 Lisp_Object window;
9613 {
9614 int hscrolled_p = 0;
9615 int hscroll_relative_p = FLOATP (Vhscroll_step);
9616 int hscroll_step_abs = 0;
9617 double hscroll_step_rel = 0;
9618
9619 if (hscroll_relative_p)
9620 {
9621 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9622 if (hscroll_step_rel < 0)
9623 {
9624 hscroll_relative_p = 0;
9625 hscroll_step_abs = 0;
9626 }
9627 }
9628 else if (INTEGERP (Vhscroll_step))
9629 {
9630 hscroll_step_abs = XINT (Vhscroll_step);
9631 if (hscroll_step_abs < 0)
9632 hscroll_step_abs = 0;
9633 }
9634 else
9635 hscroll_step_abs = 0;
9636
9637 while (WINDOWP (window))
9638 {
9639 struct window *w = XWINDOW (window);
9640
9641 if (WINDOWP (w->hchild))
9642 hscrolled_p |= hscroll_window_tree (w->hchild);
9643 else if (WINDOWP (w->vchild))
9644 hscrolled_p |= hscroll_window_tree (w->vchild);
9645 else if (w->cursor.vpos >= 0)
9646 {
9647 int h_margin;
9648 int text_area_width;
9649 struct glyph_row *current_cursor_row
9650 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9651 struct glyph_row *desired_cursor_row
9652 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9653 struct glyph_row *cursor_row
9654 = (desired_cursor_row->enabled_p
9655 ? desired_cursor_row
9656 : current_cursor_row);
9657
9658 text_area_width = window_box_width (w, TEXT_AREA);
9659
9660 /* Scroll when cursor is inside this scroll margin. */
9661 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9662
9663 if ((XFASTINT (w->hscroll)
9664 && w->cursor.x <= h_margin)
9665 || (cursor_row->enabled_p
9666 && cursor_row->truncated_on_right_p
9667 && (w->cursor.x >= text_area_width - h_margin)))
9668 {
9669 struct it it;
9670 int hscroll;
9671 struct buffer *saved_current_buffer;
9672 int pt;
9673 int wanted_x;
9674
9675 /* Find point in a display of infinite width. */
9676 saved_current_buffer = current_buffer;
9677 current_buffer = XBUFFER (w->buffer);
9678
9679 if (w == XWINDOW (selected_window))
9680 pt = BUF_PT (current_buffer);
9681 else
9682 {
9683 pt = marker_position (w->pointm);
9684 pt = max (BEGV, pt);
9685 pt = min (ZV, pt);
9686 }
9687
9688 /* Move iterator to pt starting at cursor_row->start in
9689 a line with infinite width. */
9690 init_to_row_start (&it, w, cursor_row);
9691 it.last_visible_x = INFINITY;
9692 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9693 current_buffer = saved_current_buffer;
9694
9695 /* Position cursor in window. */
9696 if (!hscroll_relative_p && hscroll_step_abs == 0)
9697 hscroll = max (0, (it.current_x
9698 - (ITERATOR_AT_END_OF_LINE_P (&it)
9699 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9700 : (text_area_width / 2))))
9701 / FRAME_COLUMN_WIDTH (it.f);
9702 else if (w->cursor.x >= text_area_width - h_margin)
9703 {
9704 if (hscroll_relative_p)
9705 wanted_x = text_area_width * (1 - hscroll_step_rel)
9706 - h_margin;
9707 else
9708 wanted_x = text_area_width
9709 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9710 - h_margin;
9711 hscroll
9712 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9713 }
9714 else
9715 {
9716 if (hscroll_relative_p)
9717 wanted_x = text_area_width * hscroll_step_rel
9718 + h_margin;
9719 else
9720 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9721 + h_margin;
9722 hscroll
9723 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9724 }
9725 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9726
9727 /* Don't call Fset_window_hscroll if value hasn't
9728 changed because it will prevent redisplay
9729 optimizations. */
9730 if (XFASTINT (w->hscroll) != hscroll)
9731 {
9732 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9733 w->hscroll = make_number (hscroll);
9734 hscrolled_p = 1;
9735 }
9736 }
9737 }
9738
9739 window = w->next;
9740 }
9741
9742 /* Value is non-zero if hscroll of any leaf window has been changed. */
9743 return hscrolled_p;
9744 }
9745
9746
9747 /* Set hscroll so that cursor is visible and not inside horizontal
9748 scroll margins for all windows in the tree rooted at WINDOW. See
9749 also hscroll_window_tree above. Value is non-zero if any window's
9750 hscroll has been changed. If it has, desired matrices on the frame
9751 of WINDOW are cleared. */
9752
9753 static int
9754 hscroll_windows (window)
9755 Lisp_Object window;
9756 {
9757 int hscrolled_p;
9758
9759 if (automatic_hscrolling_p)
9760 {
9761 hscrolled_p = hscroll_window_tree (window);
9762 if (hscrolled_p)
9763 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9764 }
9765 else
9766 hscrolled_p = 0;
9767 return hscrolled_p;
9768 }
9769
9770
9771 \f
9772 /************************************************************************
9773 Redisplay
9774 ************************************************************************/
9775
9776 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9777 to a non-zero value. This is sometimes handy to have in a debugger
9778 session. */
9779
9780 #if GLYPH_DEBUG
9781
9782 /* First and last unchanged row for try_window_id. */
9783
9784 int debug_first_unchanged_at_end_vpos;
9785 int debug_last_unchanged_at_beg_vpos;
9786
9787 /* Delta vpos and y. */
9788
9789 int debug_dvpos, debug_dy;
9790
9791 /* Delta in characters and bytes for try_window_id. */
9792
9793 int debug_delta, debug_delta_bytes;
9794
9795 /* Values of window_end_pos and window_end_vpos at the end of
9796 try_window_id. */
9797
9798 EMACS_INT debug_end_pos, debug_end_vpos;
9799
9800 /* Append a string to W->desired_matrix->method. FMT is a printf
9801 format string. A1...A9 are a supplement for a variable-length
9802 argument list. If trace_redisplay_p is non-zero also printf the
9803 resulting string to stderr. */
9804
9805 static void
9806 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9807 struct window *w;
9808 char *fmt;
9809 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9810 {
9811 char buffer[512];
9812 char *method = w->desired_matrix->method;
9813 int len = strlen (method);
9814 int size = sizeof w->desired_matrix->method;
9815 int remaining = size - len - 1;
9816
9817 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9818 if (len && remaining)
9819 {
9820 method[len] = '|';
9821 --remaining, ++len;
9822 }
9823
9824 strncpy (method + len, buffer, remaining);
9825
9826 if (trace_redisplay_p)
9827 fprintf (stderr, "%p (%s): %s\n",
9828 w,
9829 ((BUFFERP (w->buffer)
9830 && STRINGP (XBUFFER (w->buffer)->name))
9831 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9832 : "no buffer"),
9833 buffer);
9834 }
9835
9836 #endif /* GLYPH_DEBUG */
9837
9838
9839 /* Value is non-zero if all changes in window W, which displays
9840 current_buffer, are in the text between START and END. START is a
9841 buffer position, END is given as a distance from Z. Used in
9842 redisplay_internal for display optimization. */
9843
9844 static INLINE int
9845 text_outside_line_unchanged_p (w, start, end)
9846 struct window *w;
9847 int start, end;
9848 {
9849 int unchanged_p = 1;
9850
9851 /* If text or overlays have changed, see where. */
9852 if (XFASTINT (w->last_modified) < MODIFF
9853 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9854 {
9855 /* Gap in the line? */
9856 if (GPT < start || Z - GPT < end)
9857 unchanged_p = 0;
9858
9859 /* Changes start in front of the line, or end after it? */
9860 if (unchanged_p
9861 && (BEG_UNCHANGED < start - 1
9862 || END_UNCHANGED < end))
9863 unchanged_p = 0;
9864
9865 /* If selective display, can't optimize if changes start at the
9866 beginning of the line. */
9867 if (unchanged_p
9868 && INTEGERP (current_buffer->selective_display)
9869 && XINT (current_buffer->selective_display) > 0
9870 && (BEG_UNCHANGED < start || GPT <= start))
9871 unchanged_p = 0;
9872
9873 /* If there are overlays at the start or end of the line, these
9874 may have overlay strings with newlines in them. A change at
9875 START, for instance, may actually concern the display of such
9876 overlay strings as well, and they are displayed on different
9877 lines. So, quickly rule out this case. (For the future, it
9878 might be desirable to implement something more telling than
9879 just BEG/END_UNCHANGED.) */
9880 if (unchanged_p)
9881 {
9882 if (BEG + BEG_UNCHANGED == start
9883 && overlay_touches_p (start))
9884 unchanged_p = 0;
9885 if (END_UNCHANGED == end
9886 && overlay_touches_p (Z - end))
9887 unchanged_p = 0;
9888 }
9889 }
9890
9891 return unchanged_p;
9892 }
9893
9894
9895 /* Do a frame update, taking possible shortcuts into account. This is
9896 the main external entry point for redisplay.
9897
9898 If the last redisplay displayed an echo area message and that message
9899 is no longer requested, we clear the echo area or bring back the
9900 mini-buffer if that is in use. */
9901
9902 void
9903 redisplay ()
9904 {
9905 redisplay_internal (0);
9906 }
9907
9908
9909 static Lisp_Object
9910 overlay_arrow_string_or_property (var)
9911 Lisp_Object var;
9912 {
9913 Lisp_Object val;
9914
9915 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9916 return val;
9917
9918 return Voverlay_arrow_string;
9919 }
9920
9921 /* Return 1 if there are any overlay-arrows in current_buffer. */
9922 static int
9923 overlay_arrow_in_current_buffer_p ()
9924 {
9925 Lisp_Object vlist;
9926
9927 for (vlist = Voverlay_arrow_variable_list;
9928 CONSP (vlist);
9929 vlist = XCDR (vlist))
9930 {
9931 Lisp_Object var = XCAR (vlist);
9932 Lisp_Object val;
9933
9934 if (!SYMBOLP (var))
9935 continue;
9936 val = find_symbol_value (var);
9937 if (MARKERP (val)
9938 && current_buffer == XMARKER (val)->buffer)
9939 return 1;
9940 }
9941 return 0;
9942 }
9943
9944
9945 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9946 has changed. */
9947
9948 static int
9949 overlay_arrows_changed_p ()
9950 {
9951 Lisp_Object vlist;
9952
9953 for (vlist = Voverlay_arrow_variable_list;
9954 CONSP (vlist);
9955 vlist = XCDR (vlist))
9956 {
9957 Lisp_Object var = XCAR (vlist);
9958 Lisp_Object val, pstr;
9959
9960 if (!SYMBOLP (var))
9961 continue;
9962 val = find_symbol_value (var);
9963 if (!MARKERP (val))
9964 continue;
9965 if (! EQ (COERCE_MARKER (val),
9966 Fget (var, Qlast_arrow_position))
9967 || ! (pstr = overlay_arrow_string_or_property (var),
9968 EQ (pstr, Fget (var, Qlast_arrow_string))))
9969 return 1;
9970 }
9971 return 0;
9972 }
9973
9974 /* Mark overlay arrows to be updated on next redisplay. */
9975
9976 static void
9977 update_overlay_arrows (up_to_date)
9978 int up_to_date;
9979 {
9980 Lisp_Object vlist;
9981
9982 for (vlist = Voverlay_arrow_variable_list;
9983 CONSP (vlist);
9984 vlist = XCDR (vlist))
9985 {
9986 Lisp_Object var = XCAR (vlist);
9987
9988 if (!SYMBOLP (var))
9989 continue;
9990
9991 if (up_to_date > 0)
9992 {
9993 Lisp_Object val = find_symbol_value (var);
9994 Fput (var, Qlast_arrow_position,
9995 COERCE_MARKER (val));
9996 Fput (var, Qlast_arrow_string,
9997 overlay_arrow_string_or_property (var));
9998 }
9999 else if (up_to_date < 0
10000 || !NILP (Fget (var, Qlast_arrow_position)))
10001 {
10002 Fput (var, Qlast_arrow_position, Qt);
10003 Fput (var, Qlast_arrow_string, Qt);
10004 }
10005 }
10006 }
10007
10008
10009 /* Return overlay arrow string to display at row.
10010 Return integer (bitmap number) for arrow bitmap in left fringe.
10011 Return nil if no overlay arrow. */
10012
10013 static Lisp_Object
10014 overlay_arrow_at_row (it, row)
10015 struct it *it;
10016 struct glyph_row *row;
10017 {
10018 Lisp_Object vlist;
10019
10020 for (vlist = Voverlay_arrow_variable_list;
10021 CONSP (vlist);
10022 vlist = XCDR (vlist))
10023 {
10024 Lisp_Object var = XCAR (vlist);
10025 Lisp_Object val;
10026
10027 if (!SYMBOLP (var))
10028 continue;
10029
10030 val = find_symbol_value (var);
10031
10032 if (MARKERP (val)
10033 && current_buffer == XMARKER (val)->buffer
10034 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10035 {
10036 if (FRAME_WINDOW_P (it->f)
10037 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10038 {
10039 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10040 {
10041 int fringe_bitmap;
10042 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10043 return make_number (fringe_bitmap);
10044 }
10045 return make_number (-1); /* Use default arrow bitmap */
10046 }
10047 return overlay_arrow_string_or_property (var);
10048 }
10049 }
10050
10051 return Qnil;
10052 }
10053
10054 /* Return 1 if point moved out of or into a composition. Otherwise
10055 return 0. PREV_BUF and PREV_PT are the last point buffer and
10056 position. BUF and PT are the current point buffer and position. */
10057
10058 int
10059 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10060 struct buffer *prev_buf, *buf;
10061 int prev_pt, pt;
10062 {
10063 EMACS_INT start, end;
10064 Lisp_Object prop;
10065 Lisp_Object buffer;
10066
10067 XSETBUFFER (buffer, buf);
10068 /* Check a composition at the last point if point moved within the
10069 same buffer. */
10070 if (prev_buf == buf)
10071 {
10072 if (prev_pt == pt)
10073 /* Point didn't move. */
10074 return 0;
10075
10076 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10077 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10078 && COMPOSITION_VALID_P (start, end, prop)
10079 && start < prev_pt && end > prev_pt)
10080 /* The last point was within the composition. Return 1 iff
10081 point moved out of the composition. */
10082 return (pt <= start || pt >= end);
10083 }
10084
10085 /* Check a composition at the current point. */
10086 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10087 && find_composition (pt, -1, &start, &end, &prop, buffer)
10088 && COMPOSITION_VALID_P (start, end, prop)
10089 && start < pt && end > pt);
10090 }
10091
10092
10093 /* Reconsider the setting of B->clip_changed which is displayed
10094 in window W. */
10095
10096 static INLINE void
10097 reconsider_clip_changes (w, b)
10098 struct window *w;
10099 struct buffer *b;
10100 {
10101 if (b->clip_changed
10102 && !NILP (w->window_end_valid)
10103 && w->current_matrix->buffer == b
10104 && w->current_matrix->zv == BUF_ZV (b)
10105 && w->current_matrix->begv == BUF_BEGV (b))
10106 b->clip_changed = 0;
10107
10108 /* If display wasn't paused, and W is not a tool bar window, see if
10109 point has been moved into or out of a composition. In that case,
10110 we set b->clip_changed to 1 to force updating the screen. If
10111 b->clip_changed has already been set to 1, we can skip this
10112 check. */
10113 if (!b->clip_changed
10114 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10115 {
10116 int pt;
10117
10118 if (w == XWINDOW (selected_window))
10119 pt = BUF_PT (current_buffer);
10120 else
10121 pt = marker_position (w->pointm);
10122
10123 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10124 || pt != XINT (w->last_point))
10125 && check_point_in_composition (w->current_matrix->buffer,
10126 XINT (w->last_point),
10127 XBUFFER (w->buffer), pt))
10128 b->clip_changed = 1;
10129 }
10130 }
10131 \f
10132
10133 /* Select FRAME to forward the values of frame-local variables into C
10134 variables so that the redisplay routines can access those values
10135 directly. */
10136
10137 static void
10138 select_frame_for_redisplay (frame)
10139 Lisp_Object frame;
10140 {
10141 Lisp_Object tail, sym, val;
10142 Lisp_Object old = selected_frame;
10143
10144 selected_frame = frame;
10145
10146 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10147 if (CONSP (XCAR (tail))
10148 && (sym = XCAR (XCAR (tail)),
10149 SYMBOLP (sym))
10150 && (sym = indirect_variable (sym),
10151 val = SYMBOL_VALUE (sym),
10152 (BUFFER_LOCAL_VALUEP (val)
10153 || SOME_BUFFER_LOCAL_VALUEP (val)))
10154 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10155 Fsymbol_value (sym);
10156
10157 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10158 if (CONSP (XCAR (tail))
10159 && (sym = XCAR (XCAR (tail)),
10160 SYMBOLP (sym))
10161 && (sym = indirect_variable (sym),
10162 val = SYMBOL_VALUE (sym),
10163 (BUFFER_LOCAL_VALUEP (val)
10164 || SOME_BUFFER_LOCAL_VALUEP (val)))
10165 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10166 Fsymbol_value (sym);
10167 }
10168
10169
10170 #define STOP_POLLING \
10171 do { if (! polling_stopped_here) stop_polling (); \
10172 polling_stopped_here = 1; } while (0)
10173
10174 #define RESUME_POLLING \
10175 do { if (polling_stopped_here) start_polling (); \
10176 polling_stopped_here = 0; } while (0)
10177
10178
10179 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10180 response to any user action; therefore, we should preserve the echo
10181 area. (Actually, our caller does that job.) Perhaps in the future
10182 avoid recentering windows if it is not necessary; currently that
10183 causes some problems. */
10184
10185 static void
10186 redisplay_internal (preserve_echo_area)
10187 int preserve_echo_area;
10188 {
10189 struct window *w = XWINDOW (selected_window);
10190 struct frame *f = XFRAME (w->frame);
10191 int pause;
10192 int must_finish = 0;
10193 struct text_pos tlbufpos, tlendpos;
10194 int number_of_visible_frames;
10195 int count;
10196 struct frame *sf = SELECTED_FRAME ();
10197 int polling_stopped_here = 0;
10198
10199 /* Non-zero means redisplay has to consider all windows on all
10200 frames. Zero means, only selected_window is considered. */
10201 int consider_all_windows_p;
10202
10203 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10204
10205 /* No redisplay if running in batch mode or frame is not yet fully
10206 initialized, or redisplay is explicitly turned off by setting
10207 Vinhibit_redisplay. */
10208 if (noninteractive
10209 || !NILP (Vinhibit_redisplay)
10210 || !f->glyphs_initialized_p)
10211 return;
10212
10213 /* The flag redisplay_performed_directly_p is set by
10214 direct_output_for_insert when it already did the whole screen
10215 update necessary. */
10216 if (redisplay_performed_directly_p)
10217 {
10218 redisplay_performed_directly_p = 0;
10219 if (!hscroll_windows (selected_window))
10220 return;
10221 }
10222
10223 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10224 if (popup_activated ())
10225 return;
10226 #endif
10227
10228 /* I don't think this happens but let's be paranoid. */
10229 if (redisplaying_p)
10230 return;
10231
10232 /* Record a function that resets redisplaying_p to its old value
10233 when we leave this function. */
10234 count = SPECPDL_INDEX ();
10235 record_unwind_protect (unwind_redisplay,
10236 Fcons (make_number (redisplaying_p), selected_frame));
10237 ++redisplaying_p;
10238 specbind (Qinhibit_free_realized_faces, Qnil);
10239
10240 retry:
10241 pause = 0;
10242 reconsider_clip_changes (w, current_buffer);
10243
10244 /* If new fonts have been loaded that make a glyph matrix adjustment
10245 necessary, do it. */
10246 if (fonts_changed_p)
10247 {
10248 adjust_glyphs (NULL);
10249 ++windows_or_buffers_changed;
10250 fonts_changed_p = 0;
10251 }
10252
10253 /* If face_change_count is non-zero, init_iterator will free all
10254 realized faces, which includes the faces referenced from current
10255 matrices. So, we can't reuse current matrices in this case. */
10256 if (face_change_count)
10257 ++windows_or_buffers_changed;
10258
10259 if (! FRAME_WINDOW_P (sf)
10260 && previous_terminal_frame != sf)
10261 {
10262 /* Since frames on an ASCII terminal share the same display
10263 area, displaying a different frame means redisplay the whole
10264 thing. */
10265 windows_or_buffers_changed++;
10266 SET_FRAME_GARBAGED (sf);
10267 XSETFRAME (Vterminal_frame, sf);
10268 }
10269 previous_terminal_frame = sf;
10270
10271 /* Set the visible flags for all frames. Do this before checking
10272 for resized or garbaged frames; they want to know if their frames
10273 are visible. See the comment in frame.h for
10274 FRAME_SAMPLE_VISIBILITY. */
10275 {
10276 Lisp_Object tail, frame;
10277
10278 number_of_visible_frames = 0;
10279
10280 FOR_EACH_FRAME (tail, frame)
10281 {
10282 struct frame *f = XFRAME (frame);
10283
10284 FRAME_SAMPLE_VISIBILITY (f);
10285 if (FRAME_VISIBLE_P (f))
10286 ++number_of_visible_frames;
10287 clear_desired_matrices (f);
10288 }
10289 }
10290
10291 /* Notice any pending interrupt request to change frame size. */
10292 do_pending_window_change (1);
10293
10294 /* Clear frames marked as garbaged. */
10295 if (frame_garbaged)
10296 clear_garbaged_frames ();
10297
10298 /* Build menubar and tool-bar items. */
10299 prepare_menu_bars ();
10300
10301 if (windows_or_buffers_changed)
10302 update_mode_lines++;
10303
10304 /* Detect case that we need to write or remove a star in the mode line. */
10305 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10306 {
10307 w->update_mode_line = Qt;
10308 if (buffer_shared > 1)
10309 update_mode_lines++;
10310 }
10311
10312 /* If %c is in the mode line, update it if needed. */
10313 if (!NILP (w->column_number_displayed)
10314 /* This alternative quickly identifies a common case
10315 where no change is needed. */
10316 && !(PT == XFASTINT (w->last_point)
10317 && XFASTINT (w->last_modified) >= MODIFF
10318 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10319 && (XFASTINT (w->column_number_displayed)
10320 != (int) current_column ())) /* iftc */
10321 w->update_mode_line = Qt;
10322
10323 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10324
10325 /* The variable buffer_shared is set in redisplay_window and
10326 indicates that we redisplay a buffer in different windows. See
10327 there. */
10328 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10329 || cursor_type_changed);
10330
10331 /* If specs for an arrow have changed, do thorough redisplay
10332 to ensure we remove any arrow that should no longer exist. */
10333 if (overlay_arrows_changed_p ())
10334 consider_all_windows_p = windows_or_buffers_changed = 1;
10335
10336 /* Normally the message* functions will have already displayed and
10337 updated the echo area, but the frame may have been trashed, or
10338 the update may have been preempted, so display the echo area
10339 again here. Checking message_cleared_p captures the case that
10340 the echo area should be cleared. */
10341 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10342 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10343 || (message_cleared_p
10344 && minibuf_level == 0
10345 /* If the mini-window is currently selected, this means the
10346 echo-area doesn't show through. */
10347 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10348 {
10349 int window_height_changed_p = echo_area_display (0);
10350 must_finish = 1;
10351
10352 /* If we don't display the current message, don't clear the
10353 message_cleared_p flag, because, if we did, we wouldn't clear
10354 the echo area in the next redisplay which doesn't preserve
10355 the echo area. */
10356 if (!display_last_displayed_message_p)
10357 message_cleared_p = 0;
10358
10359 if (fonts_changed_p)
10360 goto retry;
10361 else if (window_height_changed_p)
10362 {
10363 consider_all_windows_p = 1;
10364 ++update_mode_lines;
10365 ++windows_or_buffers_changed;
10366
10367 /* If window configuration was changed, frames may have been
10368 marked garbaged. Clear them or we will experience
10369 surprises wrt scrolling. */
10370 if (frame_garbaged)
10371 clear_garbaged_frames ();
10372 }
10373 }
10374 else if (EQ (selected_window, minibuf_window)
10375 && (current_buffer->clip_changed
10376 || XFASTINT (w->last_modified) < MODIFF
10377 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10378 && resize_mini_window (w, 0))
10379 {
10380 /* Resized active mini-window to fit the size of what it is
10381 showing if its contents might have changed. */
10382 must_finish = 1;
10383 consider_all_windows_p = 1;
10384 ++windows_or_buffers_changed;
10385 ++update_mode_lines;
10386
10387 /* If window configuration was changed, frames may have been
10388 marked garbaged. Clear them or we will experience
10389 surprises wrt scrolling. */
10390 if (frame_garbaged)
10391 clear_garbaged_frames ();
10392 }
10393
10394
10395 /* If showing the region, and mark has changed, we must redisplay
10396 the whole window. The assignment to this_line_start_pos prevents
10397 the optimization directly below this if-statement. */
10398 if (((!NILP (Vtransient_mark_mode)
10399 && !NILP (XBUFFER (w->buffer)->mark_active))
10400 != !NILP (w->region_showing))
10401 || (!NILP (w->region_showing)
10402 && !EQ (w->region_showing,
10403 Fmarker_position (XBUFFER (w->buffer)->mark))))
10404 CHARPOS (this_line_start_pos) = 0;
10405
10406 /* Optimize the case that only the line containing the cursor in the
10407 selected window has changed. Variables starting with this_ are
10408 set in display_line and record information about the line
10409 containing the cursor. */
10410 tlbufpos = this_line_start_pos;
10411 tlendpos = this_line_end_pos;
10412 if (!consider_all_windows_p
10413 && CHARPOS (tlbufpos) > 0
10414 && NILP (w->update_mode_line)
10415 && !current_buffer->clip_changed
10416 && !current_buffer->prevent_redisplay_optimizations_p
10417 && FRAME_VISIBLE_P (XFRAME (w->frame))
10418 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10419 /* Make sure recorded data applies to current buffer, etc. */
10420 && this_line_buffer == current_buffer
10421 && current_buffer == XBUFFER (w->buffer)
10422 && NILP (w->force_start)
10423 && NILP (w->optional_new_start)
10424 /* Point must be on the line that we have info recorded about. */
10425 && PT >= CHARPOS (tlbufpos)
10426 && PT <= Z - CHARPOS (tlendpos)
10427 /* All text outside that line, including its final newline,
10428 must be unchanged */
10429 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10430 CHARPOS (tlendpos)))
10431 {
10432 if (CHARPOS (tlbufpos) > BEGV
10433 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10434 && (CHARPOS (tlbufpos) == ZV
10435 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10436 /* Former continuation line has disappeared by becoming empty */
10437 goto cancel;
10438 else if (XFASTINT (w->last_modified) < MODIFF
10439 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10440 || MINI_WINDOW_P (w))
10441 {
10442 /* We have to handle the case of continuation around a
10443 wide-column character (See the comment in indent.c around
10444 line 885).
10445
10446 For instance, in the following case:
10447
10448 -------- Insert --------
10449 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10450 J_I_ ==> J_I_ `^^' are cursors.
10451 ^^ ^^
10452 -------- --------
10453
10454 As we have to redraw the line above, we should goto cancel. */
10455
10456 struct it it;
10457 int line_height_before = this_line_pixel_height;
10458
10459 /* Note that start_display will handle the case that the
10460 line starting at tlbufpos is a continuation lines. */
10461 start_display (&it, w, tlbufpos);
10462
10463 /* Implementation note: It this still necessary? */
10464 if (it.current_x != this_line_start_x)
10465 goto cancel;
10466
10467 TRACE ((stderr, "trying display optimization 1\n"));
10468 w->cursor.vpos = -1;
10469 overlay_arrow_seen = 0;
10470 it.vpos = this_line_vpos;
10471 it.current_y = this_line_y;
10472 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10473 display_line (&it);
10474
10475 /* If line contains point, is not continued,
10476 and ends at same distance from eob as before, we win */
10477 if (w->cursor.vpos >= 0
10478 /* Line is not continued, otherwise this_line_start_pos
10479 would have been set to 0 in display_line. */
10480 && CHARPOS (this_line_start_pos)
10481 /* Line ends as before. */
10482 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10483 /* Line has same height as before. Otherwise other lines
10484 would have to be shifted up or down. */
10485 && this_line_pixel_height == line_height_before)
10486 {
10487 /* If this is not the window's last line, we must adjust
10488 the charstarts of the lines below. */
10489 if (it.current_y < it.last_visible_y)
10490 {
10491 struct glyph_row *row
10492 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10493 int delta, delta_bytes;
10494
10495 if (Z - CHARPOS (tlendpos) == ZV)
10496 {
10497 /* This line ends at end of (accessible part of)
10498 buffer. There is no newline to count. */
10499 delta = (Z
10500 - CHARPOS (tlendpos)
10501 - MATRIX_ROW_START_CHARPOS (row));
10502 delta_bytes = (Z_BYTE
10503 - BYTEPOS (tlendpos)
10504 - MATRIX_ROW_START_BYTEPOS (row));
10505 }
10506 else
10507 {
10508 /* This line ends in a newline. Must take
10509 account of the newline and the rest of the
10510 text that follows. */
10511 delta = (Z
10512 - CHARPOS (tlendpos)
10513 - MATRIX_ROW_START_CHARPOS (row));
10514 delta_bytes = (Z_BYTE
10515 - BYTEPOS (tlendpos)
10516 - MATRIX_ROW_START_BYTEPOS (row));
10517 }
10518
10519 increment_matrix_positions (w->current_matrix,
10520 this_line_vpos + 1,
10521 w->current_matrix->nrows,
10522 delta, delta_bytes);
10523 }
10524
10525 /* If this row displays text now but previously didn't,
10526 or vice versa, w->window_end_vpos may have to be
10527 adjusted. */
10528 if ((it.glyph_row - 1)->displays_text_p)
10529 {
10530 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10531 XSETINT (w->window_end_vpos, this_line_vpos);
10532 }
10533 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10534 && this_line_vpos > 0)
10535 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10536 w->window_end_valid = Qnil;
10537
10538 /* Update hint: No need to try to scroll in update_window. */
10539 w->desired_matrix->no_scrolling_p = 1;
10540
10541 #if GLYPH_DEBUG
10542 *w->desired_matrix->method = 0;
10543 debug_method_add (w, "optimization 1");
10544 #endif
10545 #ifdef HAVE_WINDOW_SYSTEM
10546 update_window_fringes (w, 0);
10547 #endif
10548 goto update;
10549 }
10550 else
10551 goto cancel;
10552 }
10553 else if (/* Cursor position hasn't changed. */
10554 PT == XFASTINT (w->last_point)
10555 /* Make sure the cursor was last displayed
10556 in this window. Otherwise we have to reposition it. */
10557 && 0 <= w->cursor.vpos
10558 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10559 {
10560 if (!must_finish)
10561 {
10562 do_pending_window_change (1);
10563
10564 /* We used to always goto end_of_redisplay here, but this
10565 isn't enough if we have a blinking cursor. */
10566 if (w->cursor_off_p == w->last_cursor_off_p)
10567 goto end_of_redisplay;
10568 }
10569 goto update;
10570 }
10571 /* If highlighting the region, or if the cursor is in the echo area,
10572 then we can't just move the cursor. */
10573 else if (! (!NILP (Vtransient_mark_mode)
10574 && !NILP (current_buffer->mark_active))
10575 && (EQ (selected_window, current_buffer->last_selected_window)
10576 || highlight_nonselected_windows)
10577 && NILP (w->region_showing)
10578 && NILP (Vshow_trailing_whitespace)
10579 && !cursor_in_echo_area)
10580 {
10581 struct it it;
10582 struct glyph_row *row;
10583
10584 /* Skip from tlbufpos to PT and see where it is. Note that
10585 PT may be in invisible text. If so, we will end at the
10586 next visible position. */
10587 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10588 NULL, DEFAULT_FACE_ID);
10589 it.current_x = this_line_start_x;
10590 it.current_y = this_line_y;
10591 it.vpos = this_line_vpos;
10592
10593 /* The call to move_it_to stops in front of PT, but
10594 moves over before-strings. */
10595 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10596
10597 if (it.vpos == this_line_vpos
10598 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10599 row->enabled_p))
10600 {
10601 xassert (this_line_vpos == it.vpos);
10602 xassert (this_line_y == it.current_y);
10603 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10604 #if GLYPH_DEBUG
10605 *w->desired_matrix->method = 0;
10606 debug_method_add (w, "optimization 3");
10607 #endif
10608 goto update;
10609 }
10610 else
10611 goto cancel;
10612 }
10613
10614 cancel:
10615 /* Text changed drastically or point moved off of line. */
10616 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10617 }
10618
10619 CHARPOS (this_line_start_pos) = 0;
10620 consider_all_windows_p |= buffer_shared > 1;
10621 ++clear_face_cache_count;
10622 #ifdef HAVE_WINDOW_SYSTEM
10623 ++clear_image_cache_count;
10624 #endif
10625
10626 /* Build desired matrices, and update the display. If
10627 consider_all_windows_p is non-zero, do it for all windows on all
10628 frames. Otherwise do it for selected_window, only. */
10629
10630 if (consider_all_windows_p)
10631 {
10632 Lisp_Object tail, frame;
10633 int i, n = 0, size = 50;
10634 struct frame **updated
10635 = (struct frame **) alloca (size * sizeof *updated);
10636
10637 /* Recompute # windows showing selected buffer. This will be
10638 incremented each time such a window is displayed. */
10639 buffer_shared = 0;
10640
10641 FOR_EACH_FRAME (tail, frame)
10642 {
10643 struct frame *f = XFRAME (frame);
10644
10645 if (FRAME_WINDOW_P (f) || f == sf)
10646 {
10647 if (! EQ (frame, selected_frame))
10648 /* Select the frame, for the sake of frame-local
10649 variables. */
10650 select_frame_for_redisplay (frame);
10651
10652 /* Mark all the scroll bars to be removed; we'll redeem
10653 the ones we want when we redisplay their windows. */
10654 if (condemn_scroll_bars_hook)
10655 condemn_scroll_bars_hook (f);
10656
10657 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10658 redisplay_windows (FRAME_ROOT_WINDOW (f));
10659
10660 /* Any scroll bars which redisplay_windows should have
10661 nuked should now go away. */
10662 if (judge_scroll_bars_hook)
10663 judge_scroll_bars_hook (f);
10664
10665 /* If fonts changed, display again. */
10666 /* ??? rms: I suspect it is a mistake to jump all the way
10667 back to retry here. It should just retry this frame. */
10668 if (fonts_changed_p)
10669 goto retry;
10670
10671 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10672 {
10673 /* See if we have to hscroll. */
10674 if (hscroll_windows (f->root_window))
10675 goto retry;
10676
10677 /* Prevent various kinds of signals during display
10678 update. stdio is not robust about handling
10679 signals, which can cause an apparent I/O
10680 error. */
10681 if (interrupt_input)
10682 unrequest_sigio ();
10683 STOP_POLLING;
10684
10685 /* Update the display. */
10686 set_window_update_flags (XWINDOW (f->root_window), 1);
10687 pause |= update_frame (f, 0, 0);
10688 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10689 if (pause)
10690 break;
10691 #endif
10692
10693 if (n == size)
10694 {
10695 int nbytes = size * sizeof *updated;
10696 struct frame **p = (struct frame **) alloca (2 * nbytes);
10697 bcopy (updated, p, nbytes);
10698 size *= 2;
10699 }
10700
10701 updated[n++] = f;
10702 }
10703 }
10704 }
10705
10706 if (!pause)
10707 {
10708 /* Do the mark_window_display_accurate after all windows have
10709 been redisplayed because this call resets flags in buffers
10710 which are needed for proper redisplay. */
10711 for (i = 0; i < n; ++i)
10712 {
10713 struct frame *f = updated[i];
10714 mark_window_display_accurate (f->root_window, 1);
10715 if (frame_up_to_date_hook)
10716 frame_up_to_date_hook (f);
10717 }
10718 }
10719 }
10720 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10721 {
10722 Lisp_Object mini_window;
10723 struct frame *mini_frame;
10724
10725 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10726 /* Use list_of_error, not Qerror, so that
10727 we catch only errors and don't run the debugger. */
10728 internal_condition_case_1 (redisplay_window_1, selected_window,
10729 list_of_error,
10730 redisplay_window_error);
10731
10732 /* Compare desired and current matrices, perform output. */
10733
10734 update:
10735 /* If fonts changed, display again. */
10736 if (fonts_changed_p)
10737 goto retry;
10738
10739 /* Prevent various kinds of signals during display update.
10740 stdio is not robust about handling signals,
10741 which can cause an apparent I/O error. */
10742 if (interrupt_input)
10743 unrequest_sigio ();
10744 STOP_POLLING;
10745
10746 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10747 {
10748 if (hscroll_windows (selected_window))
10749 goto retry;
10750
10751 XWINDOW (selected_window)->must_be_updated_p = 1;
10752 pause = update_frame (sf, 0, 0);
10753 }
10754
10755 /* We may have called echo_area_display at the top of this
10756 function. If the echo area is on another frame, that may
10757 have put text on a frame other than the selected one, so the
10758 above call to update_frame would not have caught it. Catch
10759 it here. */
10760 mini_window = FRAME_MINIBUF_WINDOW (sf);
10761 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10762
10763 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10764 {
10765 XWINDOW (mini_window)->must_be_updated_p = 1;
10766 pause |= update_frame (mini_frame, 0, 0);
10767 if (!pause && hscroll_windows (mini_window))
10768 goto retry;
10769 }
10770 }
10771
10772 /* If display was paused because of pending input, make sure we do a
10773 thorough update the next time. */
10774 if (pause)
10775 {
10776 /* Prevent the optimization at the beginning of
10777 redisplay_internal that tries a single-line update of the
10778 line containing the cursor in the selected window. */
10779 CHARPOS (this_line_start_pos) = 0;
10780
10781 /* Let the overlay arrow be updated the next time. */
10782 update_overlay_arrows (0);
10783
10784 /* If we pause after scrolling, some rows in the current
10785 matrices of some windows are not valid. */
10786 if (!WINDOW_FULL_WIDTH_P (w)
10787 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10788 update_mode_lines = 1;
10789 }
10790 else
10791 {
10792 if (!consider_all_windows_p)
10793 {
10794 /* This has already been done above if
10795 consider_all_windows_p is set. */
10796 mark_window_display_accurate_1 (w, 1);
10797
10798 /* Say overlay arrows are up to date. */
10799 update_overlay_arrows (1);
10800
10801 if (frame_up_to_date_hook != 0)
10802 frame_up_to_date_hook (sf);
10803 }
10804
10805 update_mode_lines = 0;
10806 windows_or_buffers_changed = 0;
10807 cursor_type_changed = 0;
10808 }
10809
10810 /* Start SIGIO interrupts coming again. Having them off during the
10811 code above makes it less likely one will discard output, but not
10812 impossible, since there might be stuff in the system buffer here.
10813 But it is much hairier to try to do anything about that. */
10814 if (interrupt_input)
10815 request_sigio ();
10816 RESUME_POLLING;
10817
10818 /* If a frame has become visible which was not before, redisplay
10819 again, so that we display it. Expose events for such a frame
10820 (which it gets when becoming visible) don't call the parts of
10821 redisplay constructing glyphs, so simply exposing a frame won't
10822 display anything in this case. So, we have to display these
10823 frames here explicitly. */
10824 if (!pause)
10825 {
10826 Lisp_Object tail, frame;
10827 int new_count = 0;
10828
10829 FOR_EACH_FRAME (tail, frame)
10830 {
10831 int this_is_visible = 0;
10832
10833 if (XFRAME (frame)->visible)
10834 this_is_visible = 1;
10835 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10836 if (XFRAME (frame)->visible)
10837 this_is_visible = 1;
10838
10839 if (this_is_visible)
10840 new_count++;
10841 }
10842
10843 if (new_count != number_of_visible_frames)
10844 windows_or_buffers_changed++;
10845 }
10846
10847 /* Change frame size now if a change is pending. */
10848 do_pending_window_change (1);
10849
10850 /* If we just did a pending size change, or have additional
10851 visible frames, redisplay again. */
10852 if (windows_or_buffers_changed && !pause)
10853 goto retry;
10854
10855 /* Clear the face cache eventually. */
10856 if (consider_all_windows_p)
10857 {
10858 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10859 {
10860 clear_face_cache (0);
10861 clear_face_cache_count = 0;
10862 }
10863 #ifdef HAVE_WINDOW_SYSTEM
10864 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10865 {
10866 Lisp_Object tail, frame;
10867 FOR_EACH_FRAME (tail, frame)
10868 {
10869 struct frame *f = XFRAME (frame);
10870 if (FRAME_WINDOW_P (f))
10871 clear_image_cache (f, 0);
10872 }
10873 clear_image_cache_count = 0;
10874 }
10875 #endif /* HAVE_WINDOW_SYSTEM */
10876 }
10877
10878 end_of_redisplay:
10879 unbind_to (count, Qnil);
10880 RESUME_POLLING;
10881 }
10882
10883
10884 /* Redisplay, but leave alone any recent echo area message unless
10885 another message has been requested in its place.
10886
10887 This is useful in situations where you need to redisplay but no
10888 user action has occurred, making it inappropriate for the message
10889 area to be cleared. See tracking_off and
10890 wait_reading_process_output for examples of these situations.
10891
10892 FROM_WHERE is an integer saying from where this function was
10893 called. This is useful for debugging. */
10894
10895 void
10896 redisplay_preserve_echo_area (from_where)
10897 int from_where;
10898 {
10899 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10900
10901 if (!NILP (echo_area_buffer[1]))
10902 {
10903 /* We have a previously displayed message, but no current
10904 message. Redisplay the previous message. */
10905 display_last_displayed_message_p = 1;
10906 redisplay_internal (1);
10907 display_last_displayed_message_p = 0;
10908 }
10909 else
10910 redisplay_internal (1);
10911
10912 if (rif != NULL && rif->flush_display_optional)
10913 rif->flush_display_optional (NULL);
10914 }
10915
10916
10917 /* Function registered with record_unwind_protect in
10918 redisplay_internal. Reset redisplaying_p to the value it had
10919 before redisplay_internal was called, and clear
10920 prevent_freeing_realized_faces_p. It also selects the previously
10921 selected frame. */
10922
10923 static Lisp_Object
10924 unwind_redisplay (val)
10925 Lisp_Object val;
10926 {
10927 Lisp_Object old_redisplaying_p, old_frame;
10928
10929 old_redisplaying_p = XCAR (val);
10930 redisplaying_p = XFASTINT (old_redisplaying_p);
10931 old_frame = XCDR (val);
10932 if (! EQ (old_frame, selected_frame))
10933 select_frame_for_redisplay (old_frame);
10934 return Qnil;
10935 }
10936
10937
10938 /* Mark the display of window W as accurate or inaccurate. If
10939 ACCURATE_P is non-zero mark display of W as accurate. If
10940 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10941 redisplay_internal is called. */
10942
10943 static void
10944 mark_window_display_accurate_1 (w, accurate_p)
10945 struct window *w;
10946 int accurate_p;
10947 {
10948 if (BUFFERP (w->buffer))
10949 {
10950 struct buffer *b = XBUFFER (w->buffer);
10951
10952 w->last_modified
10953 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10954 w->last_overlay_modified
10955 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10956 w->last_had_star
10957 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10958
10959 if (accurate_p)
10960 {
10961 b->clip_changed = 0;
10962 b->prevent_redisplay_optimizations_p = 0;
10963
10964 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10965 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10966 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10967 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10968
10969 w->current_matrix->buffer = b;
10970 w->current_matrix->begv = BUF_BEGV (b);
10971 w->current_matrix->zv = BUF_ZV (b);
10972
10973 w->last_cursor = w->cursor;
10974 w->last_cursor_off_p = w->cursor_off_p;
10975
10976 if (w == XWINDOW (selected_window))
10977 w->last_point = make_number (BUF_PT (b));
10978 else
10979 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10980 }
10981 }
10982
10983 if (accurate_p)
10984 {
10985 w->window_end_valid = w->buffer;
10986 #if 0 /* This is incorrect with variable-height lines. */
10987 xassert (XINT (w->window_end_vpos)
10988 < (WINDOW_TOTAL_LINES (w)
10989 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10990 #endif
10991 w->update_mode_line = Qnil;
10992 }
10993 }
10994
10995
10996 /* Mark the display of windows in the window tree rooted at WINDOW as
10997 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10998 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10999 be redisplayed the next time redisplay_internal is called. */
11000
11001 void
11002 mark_window_display_accurate (window, accurate_p)
11003 Lisp_Object window;
11004 int accurate_p;
11005 {
11006 struct window *w;
11007
11008 for (; !NILP (window); window = w->next)
11009 {
11010 w = XWINDOW (window);
11011 mark_window_display_accurate_1 (w, accurate_p);
11012
11013 if (!NILP (w->vchild))
11014 mark_window_display_accurate (w->vchild, accurate_p);
11015 if (!NILP (w->hchild))
11016 mark_window_display_accurate (w->hchild, accurate_p);
11017 }
11018
11019 if (accurate_p)
11020 {
11021 update_overlay_arrows (1);
11022 }
11023 else
11024 {
11025 /* Force a thorough redisplay the next time by setting
11026 last_arrow_position and last_arrow_string to t, which is
11027 unequal to any useful value of Voverlay_arrow_... */
11028 update_overlay_arrows (-1);
11029 }
11030 }
11031
11032
11033 /* Return value in display table DP (Lisp_Char_Table *) for character
11034 C. Since a display table doesn't have any parent, we don't have to
11035 follow parent. Do not call this function directly but use the
11036 macro DISP_CHAR_VECTOR. */
11037
11038 Lisp_Object
11039 disp_char_vector (dp, c)
11040 struct Lisp_Char_Table *dp;
11041 int c;
11042 {
11043 Lisp_Object val;
11044
11045 if (ASCII_CHAR_P (c))
11046 {
11047 val = dp->ascii;
11048 if (SUB_CHAR_TABLE_P (val))
11049 val = XSUB_CHAR_TABLE (val)->contents[c];
11050 }
11051 else
11052 {
11053 Lisp_Object table;
11054
11055 XSETCHAR_TABLE (table, dp);
11056 val = char_table_ref (table, c);
11057 }
11058 if (NILP (val))
11059 val = dp->defalt;
11060 return val;
11061 }
11062
11063
11064 \f
11065 /***********************************************************************
11066 Window Redisplay
11067 ***********************************************************************/
11068
11069 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11070
11071 static void
11072 redisplay_windows (window)
11073 Lisp_Object window;
11074 {
11075 while (!NILP (window))
11076 {
11077 struct window *w = XWINDOW (window);
11078
11079 if (!NILP (w->hchild))
11080 redisplay_windows (w->hchild);
11081 else if (!NILP (w->vchild))
11082 redisplay_windows (w->vchild);
11083 else
11084 {
11085 displayed_buffer = XBUFFER (w->buffer);
11086 /* Use list_of_error, not Qerror, so that
11087 we catch only errors and don't run the debugger. */
11088 internal_condition_case_1 (redisplay_window_0, window,
11089 list_of_error,
11090 redisplay_window_error);
11091 }
11092
11093 window = w->next;
11094 }
11095 }
11096
11097 static Lisp_Object
11098 redisplay_window_error ()
11099 {
11100 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11101 return Qnil;
11102 }
11103
11104 static Lisp_Object
11105 redisplay_window_0 (window)
11106 Lisp_Object window;
11107 {
11108 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11109 redisplay_window (window, 0);
11110 return Qnil;
11111 }
11112
11113 static Lisp_Object
11114 redisplay_window_1 (window)
11115 Lisp_Object window;
11116 {
11117 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11118 redisplay_window (window, 1);
11119 return Qnil;
11120 }
11121 \f
11122
11123 /* Increment GLYPH until it reaches END or CONDITION fails while
11124 adding (GLYPH)->pixel_width to X. */
11125
11126 #define SKIP_GLYPHS(glyph, end, x, condition) \
11127 do \
11128 { \
11129 (x) += (glyph)->pixel_width; \
11130 ++(glyph); \
11131 } \
11132 while ((glyph) < (end) && (condition))
11133
11134
11135 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11136 DELTA is the number of bytes by which positions recorded in ROW
11137 differ from current buffer positions. */
11138
11139 void
11140 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11141 struct window *w;
11142 struct glyph_row *row;
11143 struct glyph_matrix *matrix;
11144 int delta, delta_bytes, dy, dvpos;
11145 {
11146 struct glyph *glyph = row->glyphs[TEXT_AREA];
11147 struct glyph *end = glyph + row->used[TEXT_AREA];
11148 struct glyph *cursor = NULL;
11149 /* The first glyph that starts a sequence of glyphs from string. */
11150 struct glyph *string_start;
11151 /* The X coordinate of string_start. */
11152 int string_start_x;
11153 /* The last known character position. */
11154 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11155 /* The last known character position before string_start. */
11156 int string_before_pos;
11157 int x = row->x;
11158 int cursor_x = x;
11159 int cursor_from_overlay_pos = 0;
11160 int pt_old = PT - delta;
11161
11162 /* Skip over glyphs not having an object at the start of the row.
11163 These are special glyphs like truncation marks on terminal
11164 frames. */
11165 if (row->displays_text_p)
11166 while (glyph < end
11167 && INTEGERP (glyph->object)
11168 && glyph->charpos < 0)
11169 {
11170 x += glyph->pixel_width;
11171 ++glyph;
11172 }
11173
11174 string_start = NULL;
11175 while (glyph < end
11176 && !INTEGERP (glyph->object)
11177 && (!BUFFERP (glyph->object)
11178 || (last_pos = glyph->charpos) < pt_old))
11179 {
11180 if (! STRINGP (glyph->object))
11181 {
11182 string_start = NULL;
11183 x += glyph->pixel_width;
11184 ++glyph;
11185 if (cursor_from_overlay_pos
11186 && last_pos > cursor_from_overlay_pos)
11187 {
11188 cursor_from_overlay_pos = 0;
11189 cursor = 0;
11190 }
11191 }
11192 else
11193 {
11194 string_before_pos = last_pos;
11195 string_start = glyph;
11196 string_start_x = x;
11197 /* Skip all glyphs from string. */
11198 do
11199 {
11200 int pos;
11201 if ((cursor == NULL || glyph > cursor)
11202 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11203 Qcursor, (glyph)->object))
11204 && (pos = string_buffer_position (w, glyph->object,
11205 string_before_pos),
11206 (pos == 0 /* From overlay */
11207 || pos == pt_old)))
11208 {
11209 /* Estimate overlay buffer position from the buffer
11210 positions of the glyphs before and after the overlay.
11211 Add 1 to last_pos so that if point corresponds to the
11212 glyph right after the overlay, we still use a 'cursor'
11213 property found in that overlay. */
11214 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11215 cursor = glyph;
11216 cursor_x = x;
11217 }
11218 x += glyph->pixel_width;
11219 ++glyph;
11220 }
11221 while (glyph < end && STRINGP (glyph->object));
11222 }
11223 }
11224
11225 if (cursor != NULL)
11226 {
11227 glyph = cursor;
11228 x = cursor_x;
11229 }
11230 else if (row->ends_in_ellipsis_p && glyph == end)
11231 {
11232 /* Scan back over the ellipsis glyphs, decrementing positions. */
11233 while (glyph > row->glyphs[TEXT_AREA]
11234 && (glyph - 1)->charpos == last_pos)
11235 glyph--, x -= glyph->pixel_width;
11236 /* That loop always goes one position too far,
11237 including the glyph before the ellipsis.
11238 So scan forward over that one. */
11239 x += glyph->pixel_width;
11240 glyph++;
11241 }
11242 else if (string_start
11243 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11244 {
11245 /* We may have skipped over point because the previous glyphs
11246 are from string. As there's no easy way to know the
11247 character position of the current glyph, find the correct
11248 glyph on point by scanning from string_start again. */
11249 Lisp_Object limit;
11250 Lisp_Object string;
11251 int pos;
11252
11253 limit = make_number (pt_old + 1);
11254 end = glyph;
11255 glyph = string_start;
11256 x = string_start_x;
11257 string = glyph->object;
11258 pos = string_buffer_position (w, string, string_before_pos);
11259 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11260 because we always put cursor after overlay strings. */
11261 while (pos == 0 && glyph < end)
11262 {
11263 string = glyph->object;
11264 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11265 if (glyph < end)
11266 pos = string_buffer_position (w, glyph->object, string_before_pos);
11267 }
11268
11269 while (glyph < end)
11270 {
11271 pos = XINT (Fnext_single_char_property_change
11272 (make_number (pos), Qdisplay, Qnil, limit));
11273 if (pos > pt_old)
11274 break;
11275 /* Skip glyphs from the same string. */
11276 string = glyph->object;
11277 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11278 /* Skip glyphs from an overlay. */
11279 while (glyph < end
11280 && ! string_buffer_position (w, glyph->object, pos))
11281 {
11282 string = glyph->object;
11283 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11284 }
11285 }
11286 }
11287
11288 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11289 w->cursor.x = x;
11290 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11291 w->cursor.y = row->y + dy;
11292
11293 if (w == XWINDOW (selected_window))
11294 {
11295 if (!row->continued_p
11296 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11297 && row->x == 0)
11298 {
11299 this_line_buffer = XBUFFER (w->buffer);
11300
11301 CHARPOS (this_line_start_pos)
11302 = MATRIX_ROW_START_CHARPOS (row) + delta;
11303 BYTEPOS (this_line_start_pos)
11304 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11305
11306 CHARPOS (this_line_end_pos)
11307 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11308 BYTEPOS (this_line_end_pos)
11309 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11310
11311 this_line_y = w->cursor.y;
11312 this_line_pixel_height = row->height;
11313 this_line_vpos = w->cursor.vpos;
11314 this_line_start_x = row->x;
11315 }
11316 else
11317 CHARPOS (this_line_start_pos) = 0;
11318 }
11319 }
11320
11321
11322 /* Run window scroll functions, if any, for WINDOW with new window
11323 start STARTP. Sets the window start of WINDOW to that position.
11324
11325 We assume that the window's buffer is really current. */
11326
11327 static INLINE struct text_pos
11328 run_window_scroll_functions (window, startp)
11329 Lisp_Object window;
11330 struct text_pos startp;
11331 {
11332 struct window *w = XWINDOW (window);
11333 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11334
11335 if (current_buffer != XBUFFER (w->buffer))
11336 abort ();
11337
11338 if (!NILP (Vwindow_scroll_functions))
11339 {
11340 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11341 make_number (CHARPOS (startp)));
11342 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11343 /* In case the hook functions switch buffers. */
11344 if (current_buffer != XBUFFER (w->buffer))
11345 set_buffer_internal_1 (XBUFFER (w->buffer));
11346 }
11347
11348 return startp;
11349 }
11350
11351
11352 /* Make sure the line containing the cursor is fully visible.
11353 A value of 1 means there is nothing to be done.
11354 (Either the line is fully visible, or it cannot be made so,
11355 or we cannot tell.)
11356
11357 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11358 is higher than window.
11359
11360 A value of 0 means the caller should do scrolling
11361 as if point had gone off the screen. */
11362
11363 static int
11364 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11365 struct window *w;
11366 int force_p;
11367 {
11368 struct glyph_matrix *matrix;
11369 struct glyph_row *row;
11370 int window_height;
11371
11372 if (!make_cursor_line_fully_visible_p)
11373 return 1;
11374
11375 /* It's not always possible to find the cursor, e.g, when a window
11376 is full of overlay strings. Don't do anything in that case. */
11377 if (w->cursor.vpos < 0)
11378 return 1;
11379
11380 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11381 row = MATRIX_ROW (matrix, w->cursor.vpos);
11382
11383 /* If the cursor row is not partially visible, there's nothing to do. */
11384 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11385 return 1;
11386
11387 /* If the row the cursor is in is taller than the window's height,
11388 it's not clear what to do, so do nothing. */
11389 window_height = window_box_height (w);
11390 if (row->height >= window_height)
11391 {
11392 if (!force_p || w->vscroll)
11393 return 1;
11394 }
11395 return 0;
11396
11397 #if 0
11398 /* This code used to try to scroll the window just enough to make
11399 the line visible. It returned 0 to say that the caller should
11400 allocate larger glyph matrices. */
11401
11402 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11403 {
11404 int dy = row->height - row->visible_height;
11405 w->vscroll = 0;
11406 w->cursor.y += dy;
11407 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11408 }
11409 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11410 {
11411 int dy = - (row->height - row->visible_height);
11412 w->vscroll = dy;
11413 w->cursor.y += dy;
11414 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11415 }
11416
11417 /* When we change the cursor y-position of the selected window,
11418 change this_line_y as well so that the display optimization for
11419 the cursor line of the selected window in redisplay_internal uses
11420 the correct y-position. */
11421 if (w == XWINDOW (selected_window))
11422 this_line_y = w->cursor.y;
11423
11424 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11425 redisplay with larger matrices. */
11426 if (matrix->nrows < required_matrix_height (w))
11427 {
11428 fonts_changed_p = 1;
11429 return 0;
11430 }
11431
11432 return 1;
11433 #endif /* 0 */
11434 }
11435
11436
11437 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11438 non-zero means only WINDOW is redisplayed in redisplay_internal.
11439 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11440 in redisplay_window to bring a partially visible line into view in
11441 the case that only the cursor has moved.
11442
11443 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11444 last screen line's vertical height extends past the end of the screen.
11445
11446 Value is
11447
11448 1 if scrolling succeeded
11449
11450 0 if scrolling didn't find point.
11451
11452 -1 if new fonts have been loaded so that we must interrupt
11453 redisplay, adjust glyph matrices, and try again. */
11454
11455 enum
11456 {
11457 SCROLLING_SUCCESS,
11458 SCROLLING_FAILED,
11459 SCROLLING_NEED_LARGER_MATRICES
11460 };
11461
11462 static int
11463 try_scrolling (window, just_this_one_p, scroll_conservatively,
11464 scroll_step, temp_scroll_step, last_line_misfit)
11465 Lisp_Object window;
11466 int just_this_one_p;
11467 EMACS_INT scroll_conservatively, scroll_step;
11468 int temp_scroll_step;
11469 int last_line_misfit;
11470 {
11471 struct window *w = XWINDOW (window);
11472 struct frame *f = XFRAME (w->frame);
11473 struct text_pos scroll_margin_pos;
11474 struct text_pos pos;
11475 struct text_pos startp;
11476 struct it it;
11477 Lisp_Object window_end;
11478 int this_scroll_margin;
11479 int dy = 0;
11480 int scroll_max;
11481 int rc;
11482 int amount_to_scroll = 0;
11483 Lisp_Object aggressive;
11484 int height;
11485 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11486
11487 #if GLYPH_DEBUG
11488 debug_method_add (w, "try_scrolling");
11489 #endif
11490
11491 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11492
11493 /* Compute scroll margin height in pixels. We scroll when point is
11494 within this distance from the top or bottom of the window. */
11495 if (scroll_margin > 0)
11496 {
11497 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11498 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11499 }
11500 else
11501 this_scroll_margin = 0;
11502
11503 /* Force scroll_conservatively to have a reasonable value so it doesn't
11504 cause an overflow while computing how much to scroll. */
11505 if (scroll_conservatively)
11506 scroll_conservatively = min (scroll_conservatively,
11507 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11508
11509 /* Compute how much we should try to scroll maximally to bring point
11510 into view. */
11511 if (scroll_step || scroll_conservatively || temp_scroll_step)
11512 scroll_max = max (scroll_step,
11513 max (scroll_conservatively, temp_scroll_step));
11514 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11515 || NUMBERP (current_buffer->scroll_up_aggressively))
11516 /* We're trying to scroll because of aggressive scrolling
11517 but no scroll_step is set. Choose an arbitrary one. Maybe
11518 there should be a variable for this. */
11519 scroll_max = 10;
11520 else
11521 scroll_max = 0;
11522 scroll_max *= FRAME_LINE_HEIGHT (f);
11523
11524 /* Decide whether we have to scroll down. Start at the window end
11525 and move this_scroll_margin up to find the position of the scroll
11526 margin. */
11527 window_end = Fwindow_end (window, Qt);
11528
11529 too_near_end:
11530
11531 CHARPOS (scroll_margin_pos) = XINT (window_end);
11532 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11533
11534 if (this_scroll_margin || extra_scroll_margin_lines)
11535 {
11536 start_display (&it, w, scroll_margin_pos);
11537 if (this_scroll_margin)
11538 move_it_vertically_backward (&it, this_scroll_margin);
11539 if (extra_scroll_margin_lines)
11540 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11541 scroll_margin_pos = it.current.pos;
11542 }
11543
11544 if (PT >= CHARPOS (scroll_margin_pos))
11545 {
11546 int y0;
11547
11548 /* Point is in the scroll margin at the bottom of the window, or
11549 below. Compute a new window start that makes point visible. */
11550
11551 /* Compute the distance from the scroll margin to PT.
11552 Give up if the distance is greater than scroll_max. */
11553 start_display (&it, w, scroll_margin_pos);
11554 y0 = it.current_y;
11555 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11556 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11557
11558 /* To make point visible, we have to move the window start
11559 down so that the line the cursor is in is visible, which
11560 means we have to add in the height of the cursor line. */
11561 dy = line_bottom_y (&it) - y0;
11562
11563 if (dy > scroll_max)
11564 return SCROLLING_FAILED;
11565
11566 /* Move the window start down. If scrolling conservatively,
11567 move it just enough down to make point visible. If
11568 scroll_step is set, move it down by scroll_step. */
11569 start_display (&it, w, startp);
11570
11571 if (scroll_conservatively)
11572 /* Set AMOUNT_TO_SCROLL to at least one line,
11573 and at most scroll_conservatively lines. */
11574 amount_to_scroll
11575 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11576 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11577 else if (scroll_step || temp_scroll_step)
11578 amount_to_scroll = scroll_max;
11579 else
11580 {
11581 aggressive = current_buffer->scroll_up_aggressively;
11582 height = WINDOW_BOX_TEXT_HEIGHT (w);
11583 if (NUMBERP (aggressive))
11584 {
11585 double float_amount = XFLOATINT (aggressive) * height;
11586 amount_to_scroll = float_amount;
11587 if (amount_to_scroll == 0 && float_amount > 0)
11588 amount_to_scroll = 1;
11589 }
11590 }
11591
11592 if (amount_to_scroll <= 0)
11593 return SCROLLING_FAILED;
11594
11595 /* If moving by amount_to_scroll leaves STARTP unchanged,
11596 move it down one screen line. */
11597
11598 move_it_vertically (&it, amount_to_scroll);
11599 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11600 move_it_by_lines (&it, 1, 1);
11601 startp = it.current.pos;
11602 }
11603 else
11604 {
11605 /* See if point is inside the scroll margin at the top of the
11606 window. */
11607 scroll_margin_pos = startp;
11608 if (this_scroll_margin)
11609 {
11610 start_display (&it, w, startp);
11611 move_it_vertically (&it, this_scroll_margin);
11612 scroll_margin_pos = it.current.pos;
11613 }
11614
11615 if (PT < CHARPOS (scroll_margin_pos))
11616 {
11617 /* Point is in the scroll margin at the top of the window or
11618 above what is displayed in the window. */
11619 int y0;
11620
11621 /* Compute the vertical distance from PT to the scroll
11622 margin position. Give up if distance is greater than
11623 scroll_max. */
11624 SET_TEXT_POS (pos, PT, PT_BYTE);
11625 start_display (&it, w, pos);
11626 y0 = it.current_y;
11627 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11628 it.last_visible_y, -1,
11629 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11630 dy = it.current_y - y0;
11631 if (dy > scroll_max)
11632 return SCROLLING_FAILED;
11633
11634 /* Compute new window start. */
11635 start_display (&it, w, startp);
11636
11637 if (scroll_conservatively)
11638 amount_to_scroll
11639 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11640 else if (scroll_step || temp_scroll_step)
11641 amount_to_scroll = scroll_max;
11642 else
11643 {
11644 aggressive = current_buffer->scroll_down_aggressively;
11645 height = WINDOW_BOX_TEXT_HEIGHT (w);
11646 if (NUMBERP (aggressive))
11647 {
11648 double float_amount = XFLOATINT (aggressive) * height;
11649 amount_to_scroll = float_amount;
11650 if (amount_to_scroll == 0 && float_amount > 0)
11651 amount_to_scroll = 1;
11652 }
11653 }
11654
11655 if (amount_to_scroll <= 0)
11656 return SCROLLING_FAILED;
11657
11658 move_it_vertically_backward (&it, amount_to_scroll);
11659 startp = it.current.pos;
11660 }
11661 }
11662
11663 /* Run window scroll functions. */
11664 startp = run_window_scroll_functions (window, startp);
11665
11666 /* Display the window. Give up if new fonts are loaded, or if point
11667 doesn't appear. */
11668 if (!try_window (window, startp, 0))
11669 rc = SCROLLING_NEED_LARGER_MATRICES;
11670 else if (w->cursor.vpos < 0)
11671 {
11672 clear_glyph_matrix (w->desired_matrix);
11673 rc = SCROLLING_FAILED;
11674 }
11675 else
11676 {
11677 /* Maybe forget recorded base line for line number display. */
11678 if (!just_this_one_p
11679 || current_buffer->clip_changed
11680 || BEG_UNCHANGED < CHARPOS (startp))
11681 w->base_line_number = Qnil;
11682
11683 /* If cursor ends up on a partially visible line,
11684 treat that as being off the bottom of the screen. */
11685 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11686 {
11687 clear_glyph_matrix (w->desired_matrix);
11688 ++extra_scroll_margin_lines;
11689 goto too_near_end;
11690 }
11691 rc = SCROLLING_SUCCESS;
11692 }
11693
11694 return rc;
11695 }
11696
11697
11698 /* Compute a suitable window start for window W if display of W starts
11699 on a continuation line. Value is non-zero if a new window start
11700 was computed.
11701
11702 The new window start will be computed, based on W's width, starting
11703 from the start of the continued line. It is the start of the
11704 screen line with the minimum distance from the old start W->start. */
11705
11706 static int
11707 compute_window_start_on_continuation_line (w)
11708 struct window *w;
11709 {
11710 struct text_pos pos, start_pos;
11711 int window_start_changed_p = 0;
11712
11713 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11714
11715 /* If window start is on a continuation line... Window start may be
11716 < BEGV in case there's invisible text at the start of the
11717 buffer (M-x rmail, for example). */
11718 if (CHARPOS (start_pos) > BEGV
11719 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11720 {
11721 struct it it;
11722 struct glyph_row *row;
11723
11724 /* Handle the case that the window start is out of range. */
11725 if (CHARPOS (start_pos) < BEGV)
11726 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11727 else if (CHARPOS (start_pos) > ZV)
11728 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11729
11730 /* Find the start of the continued line. This should be fast
11731 because scan_buffer is fast (newline cache). */
11732 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11733 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11734 row, DEFAULT_FACE_ID);
11735 reseat_at_previous_visible_line_start (&it);
11736
11737 /* If the line start is "too far" away from the window start,
11738 say it takes too much time to compute a new window start. */
11739 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11740 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11741 {
11742 int min_distance, distance;
11743
11744 /* Move forward by display lines to find the new window
11745 start. If window width was enlarged, the new start can
11746 be expected to be > the old start. If window width was
11747 decreased, the new window start will be < the old start.
11748 So, we're looking for the display line start with the
11749 minimum distance from the old window start. */
11750 pos = it.current.pos;
11751 min_distance = INFINITY;
11752 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11753 distance < min_distance)
11754 {
11755 min_distance = distance;
11756 pos = it.current.pos;
11757 move_it_by_lines (&it, 1, 0);
11758 }
11759
11760 /* Set the window start there. */
11761 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11762 window_start_changed_p = 1;
11763 }
11764 }
11765
11766 return window_start_changed_p;
11767 }
11768
11769
11770 /* Try cursor movement in case text has not changed in window WINDOW,
11771 with window start STARTP. Value is
11772
11773 CURSOR_MOVEMENT_SUCCESS if successful
11774
11775 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11776
11777 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11778 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11779 we want to scroll as if scroll-step were set to 1. See the code.
11780
11781 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11782 which case we have to abort this redisplay, and adjust matrices
11783 first. */
11784
11785 enum
11786 {
11787 CURSOR_MOVEMENT_SUCCESS,
11788 CURSOR_MOVEMENT_CANNOT_BE_USED,
11789 CURSOR_MOVEMENT_MUST_SCROLL,
11790 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11791 };
11792
11793 static int
11794 try_cursor_movement (window, startp, scroll_step)
11795 Lisp_Object window;
11796 struct text_pos startp;
11797 int *scroll_step;
11798 {
11799 struct window *w = XWINDOW (window);
11800 struct frame *f = XFRAME (w->frame);
11801 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11802
11803 #if GLYPH_DEBUG
11804 if (inhibit_try_cursor_movement)
11805 return rc;
11806 #endif
11807
11808 /* Handle case where text has not changed, only point, and it has
11809 not moved off the frame. */
11810 if (/* Point may be in this window. */
11811 PT >= CHARPOS (startp)
11812 /* Selective display hasn't changed. */
11813 && !current_buffer->clip_changed
11814 /* Function force-mode-line-update is used to force a thorough
11815 redisplay. It sets either windows_or_buffers_changed or
11816 update_mode_lines. So don't take a shortcut here for these
11817 cases. */
11818 && !update_mode_lines
11819 && !windows_or_buffers_changed
11820 && !cursor_type_changed
11821 /* Can't use this case if highlighting a region. When a
11822 region exists, cursor movement has to do more than just
11823 set the cursor. */
11824 && !(!NILP (Vtransient_mark_mode)
11825 && !NILP (current_buffer->mark_active))
11826 && NILP (w->region_showing)
11827 && NILP (Vshow_trailing_whitespace)
11828 /* Right after splitting windows, last_point may be nil. */
11829 && INTEGERP (w->last_point)
11830 /* This code is not used for mini-buffer for the sake of the case
11831 of redisplaying to replace an echo area message; since in
11832 that case the mini-buffer contents per se are usually
11833 unchanged. This code is of no real use in the mini-buffer
11834 since the handling of this_line_start_pos, etc., in redisplay
11835 handles the same cases. */
11836 && !EQ (window, minibuf_window)
11837 /* When splitting windows or for new windows, it happens that
11838 redisplay is called with a nil window_end_vpos or one being
11839 larger than the window. This should really be fixed in
11840 window.c. I don't have this on my list, now, so we do
11841 approximately the same as the old redisplay code. --gerd. */
11842 && INTEGERP (w->window_end_vpos)
11843 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11844 && (FRAME_WINDOW_P (f)
11845 || !overlay_arrow_in_current_buffer_p ()))
11846 {
11847 int this_scroll_margin, top_scroll_margin;
11848 struct glyph_row *row = NULL;
11849
11850 #if GLYPH_DEBUG
11851 debug_method_add (w, "cursor movement");
11852 #endif
11853
11854 /* Scroll if point within this distance from the top or bottom
11855 of the window. This is a pixel value. */
11856 this_scroll_margin = max (0, scroll_margin);
11857 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11858 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11859
11860 top_scroll_margin = this_scroll_margin;
11861 if (WINDOW_WANTS_HEADER_LINE_P (w))
11862 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11863
11864 /* Start with the row the cursor was displayed during the last
11865 not paused redisplay. Give up if that row is not valid. */
11866 if (w->last_cursor.vpos < 0
11867 || w->last_cursor.vpos >= w->current_matrix->nrows)
11868 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11869 else
11870 {
11871 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11872 if (row->mode_line_p)
11873 ++row;
11874 if (!row->enabled_p)
11875 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11876 }
11877
11878 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11879 {
11880 int scroll_p = 0;
11881 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11882
11883 if (PT > XFASTINT (w->last_point))
11884 {
11885 /* Point has moved forward. */
11886 while (MATRIX_ROW_END_CHARPOS (row) < PT
11887 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11888 {
11889 xassert (row->enabled_p);
11890 ++row;
11891 }
11892
11893 /* The end position of a row equals the start position
11894 of the next row. If PT is there, we would rather
11895 display it in the next line. */
11896 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11897 && MATRIX_ROW_END_CHARPOS (row) == PT
11898 && !cursor_row_p (w, row))
11899 ++row;
11900
11901 /* If within the scroll margin, scroll. Note that
11902 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11903 the next line would be drawn, and that
11904 this_scroll_margin can be zero. */
11905 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11906 || PT > MATRIX_ROW_END_CHARPOS (row)
11907 /* Line is completely visible last line in window
11908 and PT is to be set in the next line. */
11909 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11910 && PT == MATRIX_ROW_END_CHARPOS (row)
11911 && !row->ends_at_zv_p
11912 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11913 scroll_p = 1;
11914 }
11915 else if (PT < XFASTINT (w->last_point))
11916 {
11917 /* Cursor has to be moved backward. Note that PT >=
11918 CHARPOS (startp) because of the outer if-statement. */
11919 while (!row->mode_line_p
11920 && (MATRIX_ROW_START_CHARPOS (row) > PT
11921 || (MATRIX_ROW_START_CHARPOS (row) == PT
11922 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11923 && (row->y > top_scroll_margin
11924 || CHARPOS (startp) == BEGV))
11925 {
11926 xassert (row->enabled_p);
11927 --row;
11928 }
11929
11930 /* Consider the following case: Window starts at BEGV,
11931 there is invisible, intangible text at BEGV, so that
11932 display starts at some point START > BEGV. It can
11933 happen that we are called with PT somewhere between
11934 BEGV and START. Try to handle that case. */
11935 if (row < w->current_matrix->rows
11936 || row->mode_line_p)
11937 {
11938 row = w->current_matrix->rows;
11939 if (row->mode_line_p)
11940 ++row;
11941 }
11942
11943 /* Due to newlines in overlay strings, we may have to
11944 skip forward over overlay strings. */
11945 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11946 && MATRIX_ROW_END_CHARPOS (row) == PT
11947 && !cursor_row_p (w, row))
11948 ++row;
11949
11950 /* If within the scroll margin, scroll. */
11951 if (row->y < top_scroll_margin
11952 && CHARPOS (startp) != BEGV)
11953 scroll_p = 1;
11954 }
11955 else
11956 {
11957 /* Cursor did not move. So don't scroll even if cursor line
11958 is partially visible, as it was so before. */
11959 rc = CURSOR_MOVEMENT_SUCCESS;
11960 }
11961
11962 if (PT < MATRIX_ROW_START_CHARPOS (row)
11963 || PT > MATRIX_ROW_END_CHARPOS (row))
11964 {
11965 /* if PT is not in the glyph row, give up. */
11966 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11967 }
11968 else if (rc != CURSOR_MOVEMENT_SUCCESS
11969 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11970 && make_cursor_line_fully_visible_p)
11971 {
11972 if (PT == MATRIX_ROW_END_CHARPOS (row)
11973 && !row->ends_at_zv_p
11974 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11975 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11976 else if (row->height > window_box_height (w))
11977 {
11978 /* If we end up in a partially visible line, let's
11979 make it fully visible, except when it's taller
11980 than the window, in which case we can't do much
11981 about it. */
11982 *scroll_step = 1;
11983 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11984 }
11985 else
11986 {
11987 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11988 if (!cursor_row_fully_visible_p (w, 0, 1))
11989 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11990 else
11991 rc = CURSOR_MOVEMENT_SUCCESS;
11992 }
11993 }
11994 else if (scroll_p)
11995 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11996 else
11997 {
11998 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11999 rc = CURSOR_MOVEMENT_SUCCESS;
12000 }
12001 }
12002 }
12003
12004 return rc;
12005 }
12006
12007 void
12008 set_vertical_scroll_bar (w)
12009 struct window *w;
12010 {
12011 int start, end, whole;
12012
12013 /* Calculate the start and end positions for the current window.
12014 At some point, it would be nice to choose between scrollbars
12015 which reflect the whole buffer size, with special markers
12016 indicating narrowing, and scrollbars which reflect only the
12017 visible region.
12018
12019 Note that mini-buffers sometimes aren't displaying any text. */
12020 if (!MINI_WINDOW_P (w)
12021 || (w == XWINDOW (minibuf_window)
12022 && NILP (echo_area_buffer[0])))
12023 {
12024 struct buffer *buf = XBUFFER (w->buffer);
12025 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12026 start = marker_position (w->start) - BUF_BEGV (buf);
12027 /* I don't think this is guaranteed to be right. For the
12028 moment, we'll pretend it is. */
12029 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12030
12031 if (end < start)
12032 end = start;
12033 if (whole < (end - start))
12034 whole = end - start;
12035 }
12036 else
12037 start = end = whole = 0;
12038
12039 /* Indicate what this scroll bar ought to be displaying now. */
12040 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12041 }
12042
12043
12044 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12045 selected_window is redisplayed.
12046
12047 We can return without actually redisplaying the window if
12048 fonts_changed_p is nonzero. In that case, redisplay_internal will
12049 retry. */
12050
12051 static void
12052 redisplay_window (window, just_this_one_p)
12053 Lisp_Object window;
12054 int just_this_one_p;
12055 {
12056 struct window *w = XWINDOW (window);
12057 struct frame *f = XFRAME (w->frame);
12058 struct buffer *buffer = XBUFFER (w->buffer);
12059 struct buffer *old = current_buffer;
12060 struct text_pos lpoint, opoint, startp;
12061 int update_mode_line;
12062 int tem;
12063 struct it it;
12064 /* Record it now because it's overwritten. */
12065 int current_matrix_up_to_date_p = 0;
12066 int used_current_matrix_p = 0;
12067 /* This is less strict than current_matrix_up_to_date_p.
12068 It indictes that the buffer contents and narrowing are unchanged. */
12069 int buffer_unchanged_p = 0;
12070 int temp_scroll_step = 0;
12071 int count = SPECPDL_INDEX ();
12072 int rc;
12073 int centering_position = -1;
12074 int last_line_misfit = 0;
12075
12076 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12077 opoint = lpoint;
12078
12079 /* W must be a leaf window here. */
12080 xassert (!NILP (w->buffer));
12081 #if GLYPH_DEBUG
12082 *w->desired_matrix->method = 0;
12083 #endif
12084
12085 specbind (Qinhibit_point_motion_hooks, Qt);
12086
12087 reconsider_clip_changes (w, buffer);
12088
12089 /* Has the mode line to be updated? */
12090 update_mode_line = (!NILP (w->update_mode_line)
12091 || update_mode_lines
12092 || buffer->clip_changed
12093 || buffer->prevent_redisplay_optimizations_p);
12094
12095 if (MINI_WINDOW_P (w))
12096 {
12097 if (w == XWINDOW (echo_area_window)
12098 && !NILP (echo_area_buffer[0]))
12099 {
12100 if (update_mode_line)
12101 /* We may have to update a tty frame's menu bar or a
12102 tool-bar. Example `M-x C-h C-h C-g'. */
12103 goto finish_menu_bars;
12104 else
12105 /* We've already displayed the echo area glyphs in this window. */
12106 goto finish_scroll_bars;
12107 }
12108 else if ((w != XWINDOW (minibuf_window)
12109 || minibuf_level == 0)
12110 /* When buffer is nonempty, redisplay window normally. */
12111 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12112 /* Quail displays non-mini buffers in minibuffer window.
12113 In that case, redisplay the window normally. */
12114 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12115 {
12116 /* W is a mini-buffer window, but it's not active, so clear
12117 it. */
12118 int yb = window_text_bottom_y (w);
12119 struct glyph_row *row;
12120 int y;
12121
12122 for (y = 0, row = w->desired_matrix->rows;
12123 y < yb;
12124 y += row->height, ++row)
12125 blank_row (w, row, y);
12126 goto finish_scroll_bars;
12127 }
12128
12129 clear_glyph_matrix (w->desired_matrix);
12130 }
12131
12132 /* Otherwise set up data on this window; select its buffer and point
12133 value. */
12134 /* Really select the buffer, for the sake of buffer-local
12135 variables. */
12136 set_buffer_internal_1 (XBUFFER (w->buffer));
12137 SET_TEXT_POS (opoint, PT, PT_BYTE);
12138
12139 current_matrix_up_to_date_p
12140 = (!NILP (w->window_end_valid)
12141 && !current_buffer->clip_changed
12142 && !current_buffer->prevent_redisplay_optimizations_p
12143 && XFASTINT (w->last_modified) >= MODIFF
12144 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12145
12146 buffer_unchanged_p
12147 = (!NILP (w->window_end_valid)
12148 && !current_buffer->clip_changed
12149 && XFASTINT (w->last_modified) >= MODIFF
12150 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12151
12152 /* When windows_or_buffers_changed is non-zero, we can't rely on
12153 the window end being valid, so set it to nil there. */
12154 if (windows_or_buffers_changed)
12155 {
12156 /* If window starts on a continuation line, maybe adjust the
12157 window start in case the window's width changed. */
12158 if (XMARKER (w->start)->buffer == current_buffer)
12159 compute_window_start_on_continuation_line (w);
12160
12161 w->window_end_valid = Qnil;
12162 }
12163
12164 /* Some sanity checks. */
12165 CHECK_WINDOW_END (w);
12166 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12167 abort ();
12168 if (BYTEPOS (opoint) < CHARPOS (opoint))
12169 abort ();
12170
12171 /* If %c is in mode line, update it if needed. */
12172 if (!NILP (w->column_number_displayed)
12173 /* This alternative quickly identifies a common case
12174 where no change is needed. */
12175 && !(PT == XFASTINT (w->last_point)
12176 && XFASTINT (w->last_modified) >= MODIFF
12177 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12178 && (XFASTINT (w->column_number_displayed)
12179 != (int) current_column ())) /* iftc */
12180 update_mode_line = 1;
12181
12182 /* Count number of windows showing the selected buffer. An indirect
12183 buffer counts as its base buffer. */
12184 if (!just_this_one_p)
12185 {
12186 struct buffer *current_base, *window_base;
12187 current_base = current_buffer;
12188 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12189 if (current_base->base_buffer)
12190 current_base = current_base->base_buffer;
12191 if (window_base->base_buffer)
12192 window_base = window_base->base_buffer;
12193 if (current_base == window_base)
12194 buffer_shared++;
12195 }
12196
12197 /* Point refers normally to the selected window. For any other
12198 window, set up appropriate value. */
12199 if (!EQ (window, selected_window))
12200 {
12201 int new_pt = XMARKER (w->pointm)->charpos;
12202 int new_pt_byte = marker_byte_position (w->pointm);
12203 if (new_pt < BEGV)
12204 {
12205 new_pt = BEGV;
12206 new_pt_byte = BEGV_BYTE;
12207 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12208 }
12209 else if (new_pt > (ZV - 1))
12210 {
12211 new_pt = ZV;
12212 new_pt_byte = ZV_BYTE;
12213 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12214 }
12215
12216 /* We don't use SET_PT so that the point-motion hooks don't run. */
12217 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12218 }
12219
12220 /* If any of the character widths specified in the display table
12221 have changed, invalidate the width run cache. It's true that
12222 this may be a bit late to catch such changes, but the rest of
12223 redisplay goes (non-fatally) haywire when the display table is
12224 changed, so why should we worry about doing any better? */
12225 if (current_buffer->width_run_cache)
12226 {
12227 struct Lisp_Char_Table *disptab = buffer_display_table ();
12228
12229 if (! disptab_matches_widthtab (disptab,
12230 XVECTOR (current_buffer->width_table)))
12231 {
12232 invalidate_region_cache (current_buffer,
12233 current_buffer->width_run_cache,
12234 BEG, Z);
12235 recompute_width_table (current_buffer, disptab);
12236 }
12237 }
12238
12239 /* If window-start is screwed up, choose a new one. */
12240 if (XMARKER (w->start)->buffer != current_buffer)
12241 goto recenter;
12242
12243 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12244
12245 /* If someone specified a new starting point but did not insist,
12246 check whether it can be used. */
12247 if (!NILP (w->optional_new_start)
12248 && CHARPOS (startp) >= BEGV
12249 && CHARPOS (startp) <= ZV)
12250 {
12251 w->optional_new_start = Qnil;
12252 start_display (&it, w, startp);
12253 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12254 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12255 if (IT_CHARPOS (it) == PT)
12256 w->force_start = Qt;
12257 /* IT may overshoot PT if text at PT is invisible. */
12258 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12259 w->force_start = Qt;
12260
12261
12262 }
12263
12264 /* Handle case where place to start displaying has been specified,
12265 unless the specified location is outside the accessible range. */
12266 if (!NILP (w->force_start)
12267 || w->frozen_window_start_p)
12268 {
12269 /* We set this later on if we have to adjust point. */
12270 int new_vpos = -1;
12271 int val;
12272
12273 w->force_start = Qnil;
12274 w->vscroll = 0;
12275 w->window_end_valid = Qnil;
12276
12277 /* Forget any recorded base line for line number display. */
12278 if (!buffer_unchanged_p)
12279 w->base_line_number = Qnil;
12280
12281 /* Redisplay the mode line. Select the buffer properly for that.
12282 Also, run the hook window-scroll-functions
12283 because we have scrolled. */
12284 /* Note, we do this after clearing force_start because
12285 if there's an error, it is better to forget about force_start
12286 than to get into an infinite loop calling the hook functions
12287 and having them get more errors. */
12288 if (!update_mode_line
12289 || ! NILP (Vwindow_scroll_functions))
12290 {
12291 update_mode_line = 1;
12292 w->update_mode_line = Qt;
12293 startp = run_window_scroll_functions (window, startp);
12294 }
12295
12296 w->last_modified = make_number (0);
12297 w->last_overlay_modified = make_number (0);
12298 if (CHARPOS (startp) < BEGV)
12299 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12300 else if (CHARPOS (startp) > ZV)
12301 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12302
12303 /* Redisplay, then check if cursor has been set during the
12304 redisplay. Give up if new fonts were loaded. */
12305 val = try_window (window, startp, 1);
12306 if (!val)
12307 {
12308 w->force_start = Qt;
12309 clear_glyph_matrix (w->desired_matrix);
12310 goto need_larger_matrices;
12311 }
12312 /* Point was outside the scroll margins. */
12313 if (val < 0)
12314 new_vpos = window_box_height (w) / 2;
12315
12316 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12317 {
12318 /* If point does not appear, try to move point so it does
12319 appear. The desired matrix has been built above, so we
12320 can use it here. */
12321 new_vpos = window_box_height (w) / 2;
12322 }
12323
12324 if (!cursor_row_fully_visible_p (w, 0, 0))
12325 {
12326 /* Point does appear, but on a line partly visible at end of window.
12327 Move it back to a fully-visible line. */
12328 new_vpos = window_box_height (w);
12329 }
12330
12331 /* If we need to move point for either of the above reasons,
12332 now actually do it. */
12333 if (new_vpos >= 0)
12334 {
12335 struct glyph_row *row;
12336
12337 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12338 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12339 ++row;
12340
12341 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12342 MATRIX_ROW_START_BYTEPOS (row));
12343
12344 if (w != XWINDOW (selected_window))
12345 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12346 else if (current_buffer == old)
12347 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12348
12349 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12350
12351 /* If we are highlighting the region, then we just changed
12352 the region, so redisplay to show it. */
12353 if (!NILP (Vtransient_mark_mode)
12354 && !NILP (current_buffer->mark_active))
12355 {
12356 clear_glyph_matrix (w->desired_matrix);
12357 if (!try_window (window, startp, 0))
12358 goto need_larger_matrices;
12359 }
12360 }
12361
12362 #if GLYPH_DEBUG
12363 debug_method_add (w, "forced window start");
12364 #endif
12365 goto done;
12366 }
12367
12368 /* Handle case where text has not changed, only point, and it has
12369 not moved off the frame, and we are not retrying after hscroll.
12370 (current_matrix_up_to_date_p is nonzero when retrying.) */
12371 if (current_matrix_up_to_date_p
12372 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12373 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12374 {
12375 switch (rc)
12376 {
12377 case CURSOR_MOVEMENT_SUCCESS:
12378 used_current_matrix_p = 1;
12379 goto done;
12380
12381 #if 0 /* try_cursor_movement never returns this value. */
12382 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12383 goto need_larger_matrices;
12384 #endif
12385
12386 case CURSOR_MOVEMENT_MUST_SCROLL:
12387 goto try_to_scroll;
12388
12389 default:
12390 abort ();
12391 }
12392 }
12393 /* If current starting point was originally the beginning of a line
12394 but no longer is, find a new starting point. */
12395 else if (!NILP (w->start_at_line_beg)
12396 && !(CHARPOS (startp) <= BEGV
12397 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12398 {
12399 #if GLYPH_DEBUG
12400 debug_method_add (w, "recenter 1");
12401 #endif
12402 goto recenter;
12403 }
12404
12405 /* Try scrolling with try_window_id. Value is > 0 if update has
12406 been done, it is -1 if we know that the same window start will
12407 not work. It is 0 if unsuccessful for some other reason. */
12408 else if ((tem = try_window_id (w)) != 0)
12409 {
12410 #if GLYPH_DEBUG
12411 debug_method_add (w, "try_window_id %d", tem);
12412 #endif
12413
12414 if (fonts_changed_p)
12415 goto need_larger_matrices;
12416 if (tem > 0)
12417 goto done;
12418
12419 /* Otherwise try_window_id has returned -1 which means that we
12420 don't want the alternative below this comment to execute. */
12421 }
12422 else if (CHARPOS (startp) >= BEGV
12423 && CHARPOS (startp) <= ZV
12424 && PT >= CHARPOS (startp)
12425 && (CHARPOS (startp) < ZV
12426 /* Avoid starting at end of buffer. */
12427 || CHARPOS (startp) == BEGV
12428 || (XFASTINT (w->last_modified) >= MODIFF
12429 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12430 {
12431 #if GLYPH_DEBUG
12432 debug_method_add (w, "same window start");
12433 #endif
12434
12435 /* Try to redisplay starting at same place as before.
12436 If point has not moved off frame, accept the results. */
12437 if (!current_matrix_up_to_date_p
12438 /* Don't use try_window_reusing_current_matrix in this case
12439 because a window scroll function can have changed the
12440 buffer. */
12441 || !NILP (Vwindow_scroll_functions)
12442 || MINI_WINDOW_P (w)
12443 || !(used_current_matrix_p
12444 = try_window_reusing_current_matrix (w)))
12445 {
12446 IF_DEBUG (debug_method_add (w, "1"));
12447 if (try_window (window, startp, 1) < 0)
12448 /* -1 means we need to scroll.
12449 0 means we need new matrices, but fonts_changed_p
12450 is set in that case, so we will detect it below. */
12451 goto try_to_scroll;
12452 }
12453
12454 if (fonts_changed_p)
12455 goto need_larger_matrices;
12456
12457 if (w->cursor.vpos >= 0)
12458 {
12459 if (!just_this_one_p
12460 || current_buffer->clip_changed
12461 || BEG_UNCHANGED < CHARPOS (startp))
12462 /* Forget any recorded base line for line number display. */
12463 w->base_line_number = Qnil;
12464
12465 if (!cursor_row_fully_visible_p (w, 1, 0))
12466 {
12467 clear_glyph_matrix (w->desired_matrix);
12468 last_line_misfit = 1;
12469 }
12470 /* Drop through and scroll. */
12471 else
12472 goto done;
12473 }
12474 else
12475 clear_glyph_matrix (w->desired_matrix);
12476 }
12477
12478 try_to_scroll:
12479
12480 w->last_modified = make_number (0);
12481 w->last_overlay_modified = make_number (0);
12482
12483 /* Redisplay the mode line. Select the buffer properly for that. */
12484 if (!update_mode_line)
12485 {
12486 update_mode_line = 1;
12487 w->update_mode_line = Qt;
12488 }
12489
12490 /* Try to scroll by specified few lines. */
12491 if ((scroll_conservatively
12492 || scroll_step
12493 || temp_scroll_step
12494 || NUMBERP (current_buffer->scroll_up_aggressively)
12495 || NUMBERP (current_buffer->scroll_down_aggressively))
12496 && !current_buffer->clip_changed
12497 && CHARPOS (startp) >= BEGV
12498 && CHARPOS (startp) <= ZV)
12499 {
12500 /* The function returns -1 if new fonts were loaded, 1 if
12501 successful, 0 if not successful. */
12502 int rc = try_scrolling (window, just_this_one_p,
12503 scroll_conservatively,
12504 scroll_step,
12505 temp_scroll_step, last_line_misfit);
12506 switch (rc)
12507 {
12508 case SCROLLING_SUCCESS:
12509 goto done;
12510
12511 case SCROLLING_NEED_LARGER_MATRICES:
12512 goto need_larger_matrices;
12513
12514 case SCROLLING_FAILED:
12515 break;
12516
12517 default:
12518 abort ();
12519 }
12520 }
12521
12522 /* Finally, just choose place to start which centers point */
12523
12524 recenter:
12525 if (centering_position < 0)
12526 centering_position = window_box_height (w) / 2;
12527
12528 #if GLYPH_DEBUG
12529 debug_method_add (w, "recenter");
12530 #endif
12531
12532 /* w->vscroll = 0; */
12533
12534 /* Forget any previously recorded base line for line number display. */
12535 if (!buffer_unchanged_p)
12536 w->base_line_number = Qnil;
12537
12538 /* Move backward half the height of the window. */
12539 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12540 it.current_y = it.last_visible_y;
12541 move_it_vertically_backward (&it, centering_position);
12542 xassert (IT_CHARPOS (it) >= BEGV);
12543
12544 /* The function move_it_vertically_backward may move over more
12545 than the specified y-distance. If it->w is small, e.g. a
12546 mini-buffer window, we may end up in front of the window's
12547 display area. Start displaying at the start of the line
12548 containing PT in this case. */
12549 if (it.current_y <= 0)
12550 {
12551 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12552 move_it_vertically_backward (&it, 0);
12553 #if 0
12554 /* I think this assert is bogus if buffer contains
12555 invisible text or images. KFS. */
12556 xassert (IT_CHARPOS (it) <= PT);
12557 #endif
12558 it.current_y = 0;
12559 }
12560
12561 it.current_x = it.hpos = 0;
12562
12563 /* Set startp here explicitly in case that helps avoid an infinite loop
12564 in case the window-scroll-functions functions get errors. */
12565 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12566
12567 /* Run scroll hooks. */
12568 startp = run_window_scroll_functions (window, it.current.pos);
12569
12570 /* Redisplay the window. */
12571 if (!current_matrix_up_to_date_p
12572 || windows_or_buffers_changed
12573 || cursor_type_changed
12574 /* Don't use try_window_reusing_current_matrix in this case
12575 because it can have changed the buffer. */
12576 || !NILP (Vwindow_scroll_functions)
12577 || !just_this_one_p
12578 || MINI_WINDOW_P (w)
12579 || !(used_current_matrix_p
12580 = try_window_reusing_current_matrix (w)))
12581 try_window (window, startp, 0);
12582
12583 /* If new fonts have been loaded (due to fontsets), give up. We
12584 have to start a new redisplay since we need to re-adjust glyph
12585 matrices. */
12586 if (fonts_changed_p)
12587 goto need_larger_matrices;
12588
12589 /* If cursor did not appear assume that the middle of the window is
12590 in the first line of the window. Do it again with the next line.
12591 (Imagine a window of height 100, displaying two lines of height
12592 60. Moving back 50 from it->last_visible_y will end in the first
12593 line.) */
12594 if (w->cursor.vpos < 0)
12595 {
12596 if (!NILP (w->window_end_valid)
12597 && PT >= Z - XFASTINT (w->window_end_pos))
12598 {
12599 clear_glyph_matrix (w->desired_matrix);
12600 move_it_by_lines (&it, 1, 0);
12601 try_window (window, it.current.pos, 0);
12602 }
12603 else if (PT < IT_CHARPOS (it))
12604 {
12605 clear_glyph_matrix (w->desired_matrix);
12606 move_it_by_lines (&it, -1, 0);
12607 try_window (window, it.current.pos, 0);
12608 }
12609 else
12610 {
12611 /* Not much we can do about it. */
12612 }
12613 }
12614
12615 /* Consider the following case: Window starts at BEGV, there is
12616 invisible, intangible text at BEGV, so that display starts at
12617 some point START > BEGV. It can happen that we are called with
12618 PT somewhere between BEGV and START. Try to handle that case. */
12619 if (w->cursor.vpos < 0)
12620 {
12621 struct glyph_row *row = w->current_matrix->rows;
12622 if (row->mode_line_p)
12623 ++row;
12624 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12625 }
12626
12627 if (!cursor_row_fully_visible_p (w, 0, 0))
12628 {
12629 /* If vscroll is enabled, disable it and try again. */
12630 if (w->vscroll)
12631 {
12632 w->vscroll = 0;
12633 clear_glyph_matrix (w->desired_matrix);
12634 goto recenter;
12635 }
12636
12637 /* If centering point failed to make the whole line visible,
12638 put point at the top instead. That has to make the whole line
12639 visible, if it can be done. */
12640 if (centering_position == 0)
12641 goto done;
12642
12643 clear_glyph_matrix (w->desired_matrix);
12644 centering_position = 0;
12645 goto recenter;
12646 }
12647
12648 done:
12649
12650 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12651 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12652 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12653 ? Qt : Qnil);
12654
12655 /* Display the mode line, if we must. */
12656 if ((update_mode_line
12657 /* If window not full width, must redo its mode line
12658 if (a) the window to its side is being redone and
12659 (b) we do a frame-based redisplay. This is a consequence
12660 of how inverted lines are drawn in frame-based redisplay. */
12661 || (!just_this_one_p
12662 && !FRAME_WINDOW_P (f)
12663 && !WINDOW_FULL_WIDTH_P (w))
12664 /* Line number to display. */
12665 || INTEGERP (w->base_line_pos)
12666 /* Column number is displayed and different from the one displayed. */
12667 || (!NILP (w->column_number_displayed)
12668 && (XFASTINT (w->column_number_displayed)
12669 != (int) current_column ()))) /* iftc */
12670 /* This means that the window has a mode line. */
12671 && (WINDOW_WANTS_MODELINE_P (w)
12672 || WINDOW_WANTS_HEADER_LINE_P (w)))
12673 {
12674 display_mode_lines (w);
12675
12676 /* If mode line height has changed, arrange for a thorough
12677 immediate redisplay using the correct mode line height. */
12678 if (WINDOW_WANTS_MODELINE_P (w)
12679 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12680 {
12681 fonts_changed_p = 1;
12682 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12683 = DESIRED_MODE_LINE_HEIGHT (w);
12684 }
12685
12686 /* If top line height has changed, arrange for a thorough
12687 immediate redisplay using the correct mode line height. */
12688 if (WINDOW_WANTS_HEADER_LINE_P (w)
12689 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12690 {
12691 fonts_changed_p = 1;
12692 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12693 = DESIRED_HEADER_LINE_HEIGHT (w);
12694 }
12695
12696 if (fonts_changed_p)
12697 goto need_larger_matrices;
12698 }
12699
12700 if (!line_number_displayed
12701 && !BUFFERP (w->base_line_pos))
12702 {
12703 w->base_line_pos = Qnil;
12704 w->base_line_number = Qnil;
12705 }
12706
12707 finish_menu_bars:
12708
12709 /* When we reach a frame's selected window, redo the frame's menu bar. */
12710 if (update_mode_line
12711 && EQ (FRAME_SELECTED_WINDOW (f), window))
12712 {
12713 int redisplay_menu_p = 0;
12714 int redisplay_tool_bar_p = 0;
12715
12716 if (FRAME_WINDOW_P (f))
12717 {
12718 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12719 || defined (USE_GTK)
12720 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12721 #else
12722 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12723 #endif
12724 }
12725 else
12726 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12727
12728 if (redisplay_menu_p)
12729 display_menu_bar (w);
12730
12731 #ifdef HAVE_WINDOW_SYSTEM
12732 #ifdef USE_GTK
12733 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12734 #else
12735 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12736 && (FRAME_TOOL_BAR_LINES (f) > 0
12737 || auto_resize_tool_bars_p);
12738
12739 #endif
12740
12741 if (redisplay_tool_bar_p)
12742 redisplay_tool_bar (f);
12743 #endif
12744 }
12745
12746 #ifdef HAVE_WINDOW_SYSTEM
12747 if (FRAME_WINDOW_P (f)
12748 && update_window_fringes (w, 0)
12749 && !just_this_one_p
12750 && (used_current_matrix_p || overlay_arrow_seen)
12751 && !w->pseudo_window_p)
12752 {
12753 update_begin (f);
12754 BLOCK_INPUT;
12755 if (draw_window_fringes (w, 1))
12756 x_draw_vertical_border (w);
12757 UNBLOCK_INPUT;
12758 update_end (f);
12759 }
12760 #endif /* HAVE_WINDOW_SYSTEM */
12761
12762 /* We go to this label, with fonts_changed_p nonzero,
12763 if it is necessary to try again using larger glyph matrices.
12764 We have to redeem the scroll bar even in this case,
12765 because the loop in redisplay_internal expects that. */
12766 need_larger_matrices:
12767 ;
12768 finish_scroll_bars:
12769
12770 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12771 {
12772 /* Set the thumb's position and size. */
12773 set_vertical_scroll_bar (w);
12774
12775 /* Note that we actually used the scroll bar attached to this
12776 window, so it shouldn't be deleted at the end of redisplay. */
12777 redeem_scroll_bar_hook (w);
12778 }
12779
12780 /* Restore current_buffer and value of point in it. */
12781 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12782 set_buffer_internal_1 (old);
12783 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12784
12785 unbind_to (count, Qnil);
12786 }
12787
12788
12789 /* Build the complete desired matrix of WINDOW with a window start
12790 buffer position POS.
12791
12792 Value is 1 if successful. It is zero if fonts were loaded during
12793 redisplay which makes re-adjusting glyph matrices necessary, and -1
12794 if point would appear in the scroll margins.
12795 (We check that only if CHECK_MARGINS is nonzero. */
12796
12797 int
12798 try_window (window, pos, check_margins)
12799 Lisp_Object window;
12800 struct text_pos pos;
12801 int check_margins;
12802 {
12803 struct window *w = XWINDOW (window);
12804 struct it it;
12805 struct glyph_row *last_text_row = NULL;
12806
12807 /* Make POS the new window start. */
12808 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12809
12810 /* Mark cursor position as unknown. No overlay arrow seen. */
12811 w->cursor.vpos = -1;
12812 overlay_arrow_seen = 0;
12813
12814 /* Initialize iterator and info to start at POS. */
12815 start_display (&it, w, pos);
12816
12817 /* Display all lines of W. */
12818 while (it.current_y < it.last_visible_y)
12819 {
12820 if (display_line (&it))
12821 last_text_row = it.glyph_row - 1;
12822 if (fonts_changed_p)
12823 return 0;
12824 }
12825
12826 /* Don't let the cursor end in the scroll margins. */
12827 if (check_margins)
12828 {
12829 int this_scroll_margin, cursor_height;
12830
12831 this_scroll_margin = max (0, scroll_margin);
12832 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12833 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
12834 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12835
12836 if ((w->cursor.y < this_scroll_margin
12837 && CHARPOS (pos) > BEGV)
12838 /* rms: considering make_cursor_line_fully_visible_p here
12839 seems to give wrong results. We don't want to recenter
12840 when the last line is partly visible, we want to allow
12841 that case to be handled in the usual way. */
12842 || (w->cursor.y + 1) > it.last_visible_y)
12843 {
12844 w->cursor.vpos = -1;
12845 clear_glyph_matrix (w->desired_matrix);
12846 return -1;
12847 }
12848 }
12849
12850 /* If bottom moved off end of frame, change mode line percentage. */
12851 if (XFASTINT (w->window_end_pos) <= 0
12852 && Z != IT_CHARPOS (it))
12853 w->update_mode_line = Qt;
12854
12855 /* Set window_end_pos to the offset of the last character displayed
12856 on the window from the end of current_buffer. Set
12857 window_end_vpos to its row number. */
12858 if (last_text_row)
12859 {
12860 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12861 w->window_end_bytepos
12862 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12863 w->window_end_pos
12864 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12865 w->window_end_vpos
12866 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12867 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12868 ->displays_text_p);
12869 }
12870 else
12871 {
12872 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12873 w->window_end_pos = make_number (Z - ZV);
12874 w->window_end_vpos = make_number (0);
12875 }
12876
12877 /* But that is not valid info until redisplay finishes. */
12878 w->window_end_valid = Qnil;
12879 return 1;
12880 }
12881
12882
12883 \f
12884 /************************************************************************
12885 Window redisplay reusing current matrix when buffer has not changed
12886 ************************************************************************/
12887
12888 /* Try redisplay of window W showing an unchanged buffer with a
12889 different window start than the last time it was displayed by
12890 reusing its current matrix. Value is non-zero if successful.
12891 W->start is the new window start. */
12892
12893 static int
12894 try_window_reusing_current_matrix (w)
12895 struct window *w;
12896 {
12897 struct frame *f = XFRAME (w->frame);
12898 struct glyph_row *row, *bottom_row;
12899 struct it it;
12900 struct run run;
12901 struct text_pos start, new_start;
12902 int nrows_scrolled, i;
12903 struct glyph_row *last_text_row;
12904 struct glyph_row *last_reused_text_row;
12905 struct glyph_row *start_row;
12906 int start_vpos, min_y, max_y;
12907
12908 #if GLYPH_DEBUG
12909 if (inhibit_try_window_reusing)
12910 return 0;
12911 #endif
12912
12913 if (/* This function doesn't handle terminal frames. */
12914 !FRAME_WINDOW_P (f)
12915 /* Don't try to reuse the display if windows have been split
12916 or such. */
12917 || windows_or_buffers_changed
12918 || cursor_type_changed)
12919 return 0;
12920
12921 /* Can't do this if region may have changed. */
12922 if ((!NILP (Vtransient_mark_mode)
12923 && !NILP (current_buffer->mark_active))
12924 || !NILP (w->region_showing)
12925 || !NILP (Vshow_trailing_whitespace))
12926 return 0;
12927
12928 /* If top-line visibility has changed, give up. */
12929 if (WINDOW_WANTS_HEADER_LINE_P (w)
12930 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12931 return 0;
12932
12933 /* Give up if old or new display is scrolled vertically. We could
12934 make this function handle this, but right now it doesn't. */
12935 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12936 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12937 return 0;
12938
12939 /* The variable new_start now holds the new window start. The old
12940 start `start' can be determined from the current matrix. */
12941 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12942 start = start_row->start.pos;
12943 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12944
12945 /* Clear the desired matrix for the display below. */
12946 clear_glyph_matrix (w->desired_matrix);
12947
12948 if (CHARPOS (new_start) <= CHARPOS (start))
12949 {
12950 int first_row_y;
12951
12952 /* Don't use this method if the display starts with an ellipsis
12953 displayed for invisible text. It's not easy to handle that case
12954 below, and it's certainly not worth the effort since this is
12955 not a frequent case. */
12956 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12957 return 0;
12958
12959 IF_DEBUG (debug_method_add (w, "twu1"));
12960
12961 /* Display up to a row that can be reused. The variable
12962 last_text_row is set to the last row displayed that displays
12963 text. Note that it.vpos == 0 if or if not there is a
12964 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12965 start_display (&it, w, new_start);
12966 first_row_y = it.current_y;
12967 w->cursor.vpos = -1;
12968 last_text_row = last_reused_text_row = NULL;
12969
12970 while (it.current_y < it.last_visible_y
12971 && !fonts_changed_p)
12972 {
12973 /* If we have reached into the characters in the START row,
12974 that means the line boundaries have changed. So we
12975 can't start copying with the row START. Maybe it will
12976 work to start copying with the following row. */
12977 while (IT_CHARPOS (it) > CHARPOS (start))
12978 {
12979 /* Advance to the next row as the "start". */
12980 start_row++;
12981 start = start_row->start.pos;
12982 /* If there are no more rows to try, or just one, give up. */
12983 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12984 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12985 || CHARPOS (start) == ZV)
12986 {
12987 clear_glyph_matrix (w->desired_matrix);
12988 return 0;
12989 }
12990
12991 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12992 }
12993 /* If we have reached alignment,
12994 we can copy the rest of the rows. */
12995 if (IT_CHARPOS (it) == CHARPOS (start))
12996 break;
12997
12998 if (display_line (&it))
12999 last_text_row = it.glyph_row - 1;
13000 }
13001
13002 /* A value of current_y < last_visible_y means that we stopped
13003 at the previous window start, which in turn means that we
13004 have at least one reusable row. */
13005 if (it.current_y < it.last_visible_y)
13006 {
13007 /* IT.vpos always starts from 0; it counts text lines. */
13008 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13009
13010 /* Find PT if not already found in the lines displayed. */
13011 if (w->cursor.vpos < 0)
13012 {
13013 int dy = it.current_y - start_row->y;
13014
13015 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13016 row = row_containing_pos (w, PT, row, NULL, dy);
13017 if (row)
13018 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13019 dy, nrows_scrolled);
13020 else
13021 {
13022 clear_glyph_matrix (w->desired_matrix);
13023 return 0;
13024 }
13025 }
13026
13027 /* Scroll the display. Do it before the current matrix is
13028 changed. The problem here is that update has not yet
13029 run, i.e. part of the current matrix is not up to date.
13030 scroll_run_hook will clear the cursor, and use the
13031 current matrix to get the height of the row the cursor is
13032 in. */
13033 run.current_y = start_row->y;
13034 run.desired_y = it.current_y;
13035 run.height = it.last_visible_y - it.current_y;
13036
13037 if (run.height > 0 && run.current_y != run.desired_y)
13038 {
13039 update_begin (f);
13040 rif->update_window_begin_hook (w);
13041 rif->clear_window_mouse_face (w);
13042 rif->scroll_run_hook (w, &run);
13043 rif->update_window_end_hook (w, 0, 0);
13044 update_end (f);
13045 }
13046
13047 /* Shift current matrix down by nrows_scrolled lines. */
13048 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13049 rotate_matrix (w->current_matrix,
13050 start_vpos,
13051 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13052 nrows_scrolled);
13053
13054 /* Disable lines that must be updated. */
13055 for (i = 0; i < it.vpos; ++i)
13056 (start_row + i)->enabled_p = 0;
13057
13058 /* Re-compute Y positions. */
13059 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13060 max_y = it.last_visible_y;
13061 for (row = start_row + nrows_scrolled;
13062 row < bottom_row;
13063 ++row)
13064 {
13065 row->y = it.current_y;
13066 row->visible_height = row->height;
13067
13068 if (row->y < min_y)
13069 row->visible_height -= min_y - row->y;
13070 if (row->y + row->height > max_y)
13071 row->visible_height -= row->y + row->height - max_y;
13072 row->redraw_fringe_bitmaps_p = 1;
13073
13074 it.current_y += row->height;
13075
13076 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13077 last_reused_text_row = row;
13078 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13079 break;
13080 }
13081
13082 /* Disable lines in the current matrix which are now
13083 below the window. */
13084 for (++row; row < bottom_row; ++row)
13085 row->enabled_p = 0;
13086 }
13087
13088 /* Update window_end_pos etc.; last_reused_text_row is the last
13089 reused row from the current matrix containing text, if any.
13090 The value of last_text_row is the last displayed line
13091 containing text. */
13092 if (last_reused_text_row)
13093 {
13094 w->window_end_bytepos
13095 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13096 w->window_end_pos
13097 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13098 w->window_end_vpos
13099 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13100 w->current_matrix));
13101 }
13102 else if (last_text_row)
13103 {
13104 w->window_end_bytepos
13105 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13106 w->window_end_pos
13107 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13108 w->window_end_vpos
13109 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13110 }
13111 else
13112 {
13113 /* This window must be completely empty. */
13114 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13115 w->window_end_pos = make_number (Z - ZV);
13116 w->window_end_vpos = make_number (0);
13117 }
13118 w->window_end_valid = Qnil;
13119
13120 /* Update hint: don't try scrolling again in update_window. */
13121 w->desired_matrix->no_scrolling_p = 1;
13122
13123 #if GLYPH_DEBUG
13124 debug_method_add (w, "try_window_reusing_current_matrix 1");
13125 #endif
13126 return 1;
13127 }
13128 else if (CHARPOS (new_start) > CHARPOS (start))
13129 {
13130 struct glyph_row *pt_row, *row;
13131 struct glyph_row *first_reusable_row;
13132 struct glyph_row *first_row_to_display;
13133 int dy;
13134 int yb = window_text_bottom_y (w);
13135
13136 /* Find the row starting at new_start, if there is one. Don't
13137 reuse a partially visible line at the end. */
13138 first_reusable_row = start_row;
13139 while (first_reusable_row->enabled_p
13140 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13141 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13142 < CHARPOS (new_start)))
13143 ++first_reusable_row;
13144
13145 /* Give up if there is no row to reuse. */
13146 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13147 || !first_reusable_row->enabled_p
13148 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13149 != CHARPOS (new_start)))
13150 return 0;
13151
13152 /* We can reuse fully visible rows beginning with
13153 first_reusable_row to the end of the window. Set
13154 first_row_to_display to the first row that cannot be reused.
13155 Set pt_row to the row containing point, if there is any. */
13156 pt_row = NULL;
13157 for (first_row_to_display = first_reusable_row;
13158 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13159 ++first_row_to_display)
13160 {
13161 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13162 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13163 pt_row = first_row_to_display;
13164 }
13165
13166 /* Start displaying at the start of first_row_to_display. */
13167 xassert (first_row_to_display->y < yb);
13168 init_to_row_start (&it, w, first_row_to_display);
13169
13170 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13171 - start_vpos);
13172 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13173 - nrows_scrolled);
13174 it.current_y = (first_row_to_display->y - first_reusable_row->y
13175 + WINDOW_HEADER_LINE_HEIGHT (w));
13176
13177 /* Display lines beginning with first_row_to_display in the
13178 desired matrix. Set last_text_row to the last row displayed
13179 that displays text. */
13180 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13181 if (pt_row == NULL)
13182 w->cursor.vpos = -1;
13183 last_text_row = NULL;
13184 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13185 if (display_line (&it))
13186 last_text_row = it.glyph_row - 1;
13187
13188 /* Give up If point isn't in a row displayed or reused. */
13189 if (w->cursor.vpos < 0)
13190 {
13191 clear_glyph_matrix (w->desired_matrix);
13192 return 0;
13193 }
13194
13195 /* If point is in a reused row, adjust y and vpos of the cursor
13196 position. */
13197 if (pt_row)
13198 {
13199 w->cursor.vpos -= nrows_scrolled;
13200 w->cursor.y -= first_reusable_row->y - start_row->y;
13201 }
13202
13203 /* Scroll the display. */
13204 run.current_y = first_reusable_row->y;
13205 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13206 run.height = it.last_visible_y - run.current_y;
13207 dy = run.current_y - run.desired_y;
13208
13209 if (run.height)
13210 {
13211 update_begin (f);
13212 rif->update_window_begin_hook (w);
13213 rif->clear_window_mouse_face (w);
13214 rif->scroll_run_hook (w, &run);
13215 rif->update_window_end_hook (w, 0, 0);
13216 update_end (f);
13217 }
13218
13219 /* Adjust Y positions of reused rows. */
13220 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13221 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13222 max_y = it.last_visible_y;
13223 for (row = first_reusable_row; row < first_row_to_display; ++row)
13224 {
13225 row->y -= dy;
13226 row->visible_height = row->height;
13227 if (row->y < min_y)
13228 row->visible_height -= min_y - row->y;
13229 if (row->y + row->height > max_y)
13230 row->visible_height -= row->y + row->height - max_y;
13231 row->redraw_fringe_bitmaps_p = 1;
13232 }
13233
13234 /* Scroll the current matrix. */
13235 xassert (nrows_scrolled > 0);
13236 rotate_matrix (w->current_matrix,
13237 start_vpos,
13238 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13239 -nrows_scrolled);
13240
13241 /* Disable rows not reused. */
13242 for (row -= nrows_scrolled; row < bottom_row; ++row)
13243 row->enabled_p = 0;
13244
13245 /* Point may have moved to a different line, so we cannot assume that
13246 the previous cursor position is valid; locate the correct row. */
13247 if (pt_row)
13248 {
13249 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13250 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13251 row++)
13252 {
13253 w->cursor.vpos++;
13254 w->cursor.y = row->y;
13255 }
13256 if (row < bottom_row)
13257 {
13258 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13259 while (glyph->charpos < PT)
13260 {
13261 w->cursor.hpos++;
13262 w->cursor.x += glyph->pixel_width;
13263 glyph++;
13264 }
13265 }
13266 }
13267
13268 /* Adjust window end. A null value of last_text_row means that
13269 the window end is in reused rows which in turn means that
13270 only its vpos can have changed. */
13271 if (last_text_row)
13272 {
13273 w->window_end_bytepos
13274 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13275 w->window_end_pos
13276 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13277 w->window_end_vpos
13278 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13279 }
13280 else
13281 {
13282 w->window_end_vpos
13283 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13284 }
13285
13286 w->window_end_valid = Qnil;
13287 w->desired_matrix->no_scrolling_p = 1;
13288
13289 #if GLYPH_DEBUG
13290 debug_method_add (w, "try_window_reusing_current_matrix 2");
13291 #endif
13292 return 1;
13293 }
13294
13295 return 0;
13296 }
13297
13298
13299 \f
13300 /************************************************************************
13301 Window redisplay reusing current matrix when buffer has changed
13302 ************************************************************************/
13303
13304 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13305 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13306 int *, int *));
13307 static struct glyph_row *
13308 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13309 struct glyph_row *));
13310
13311
13312 /* Return the last row in MATRIX displaying text. If row START is
13313 non-null, start searching with that row. IT gives the dimensions
13314 of the display. Value is null if matrix is empty; otherwise it is
13315 a pointer to the row found. */
13316
13317 static struct glyph_row *
13318 find_last_row_displaying_text (matrix, it, start)
13319 struct glyph_matrix *matrix;
13320 struct it *it;
13321 struct glyph_row *start;
13322 {
13323 struct glyph_row *row, *row_found;
13324
13325 /* Set row_found to the last row in IT->w's current matrix
13326 displaying text. The loop looks funny but think of partially
13327 visible lines. */
13328 row_found = NULL;
13329 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13330 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13331 {
13332 xassert (row->enabled_p);
13333 row_found = row;
13334 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13335 break;
13336 ++row;
13337 }
13338
13339 return row_found;
13340 }
13341
13342
13343 /* Return the last row in the current matrix of W that is not affected
13344 by changes at the start of current_buffer that occurred since W's
13345 current matrix was built. Value is null if no such row exists.
13346
13347 BEG_UNCHANGED us the number of characters unchanged at the start of
13348 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13349 first changed character in current_buffer. Characters at positions <
13350 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13351 when the current matrix was built. */
13352
13353 static struct glyph_row *
13354 find_last_unchanged_at_beg_row (w)
13355 struct window *w;
13356 {
13357 int first_changed_pos = BEG + BEG_UNCHANGED;
13358 struct glyph_row *row;
13359 struct glyph_row *row_found = NULL;
13360 int yb = window_text_bottom_y (w);
13361
13362 /* Find the last row displaying unchanged text. */
13363 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13364 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13365 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13366 {
13367 if (/* If row ends before first_changed_pos, it is unchanged,
13368 except in some case. */
13369 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13370 /* When row ends in ZV and we write at ZV it is not
13371 unchanged. */
13372 && !row->ends_at_zv_p
13373 /* When first_changed_pos is the end of a continued line,
13374 row is not unchanged because it may be no longer
13375 continued. */
13376 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13377 && (row->continued_p
13378 || row->exact_window_width_line_p)))
13379 row_found = row;
13380
13381 /* Stop if last visible row. */
13382 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13383 break;
13384
13385 ++row;
13386 }
13387
13388 return row_found;
13389 }
13390
13391
13392 /* Find the first glyph row in the current matrix of W that is not
13393 affected by changes at the end of current_buffer since the
13394 time W's current matrix was built.
13395
13396 Return in *DELTA the number of chars by which buffer positions in
13397 unchanged text at the end of current_buffer must be adjusted.
13398
13399 Return in *DELTA_BYTES the corresponding number of bytes.
13400
13401 Value is null if no such row exists, i.e. all rows are affected by
13402 changes. */
13403
13404 static struct glyph_row *
13405 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13406 struct window *w;
13407 int *delta, *delta_bytes;
13408 {
13409 struct glyph_row *row;
13410 struct glyph_row *row_found = NULL;
13411
13412 *delta = *delta_bytes = 0;
13413
13414 /* Display must not have been paused, otherwise the current matrix
13415 is not up to date. */
13416 if (NILP (w->window_end_valid))
13417 abort ();
13418
13419 /* A value of window_end_pos >= END_UNCHANGED means that the window
13420 end is in the range of changed text. If so, there is no
13421 unchanged row at the end of W's current matrix. */
13422 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13423 return NULL;
13424
13425 /* Set row to the last row in W's current matrix displaying text. */
13426 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13427
13428 /* If matrix is entirely empty, no unchanged row exists. */
13429 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13430 {
13431 /* The value of row is the last glyph row in the matrix having a
13432 meaningful buffer position in it. The end position of row
13433 corresponds to window_end_pos. This allows us to translate
13434 buffer positions in the current matrix to current buffer
13435 positions for characters not in changed text. */
13436 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13437 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13438 int last_unchanged_pos, last_unchanged_pos_old;
13439 struct glyph_row *first_text_row
13440 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13441
13442 *delta = Z - Z_old;
13443 *delta_bytes = Z_BYTE - Z_BYTE_old;
13444
13445 /* Set last_unchanged_pos to the buffer position of the last
13446 character in the buffer that has not been changed. Z is the
13447 index + 1 of the last character in current_buffer, i.e. by
13448 subtracting END_UNCHANGED we get the index of the last
13449 unchanged character, and we have to add BEG to get its buffer
13450 position. */
13451 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13452 last_unchanged_pos_old = last_unchanged_pos - *delta;
13453
13454 /* Search backward from ROW for a row displaying a line that
13455 starts at a minimum position >= last_unchanged_pos_old. */
13456 for (; row > first_text_row; --row)
13457 {
13458 /* This used to abort, but it can happen.
13459 It is ok to just stop the search instead here. KFS. */
13460 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13461 break;
13462
13463 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13464 row_found = row;
13465 }
13466 }
13467
13468 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13469 abort ();
13470
13471 return row_found;
13472 }
13473
13474
13475 /* Make sure that glyph rows in the current matrix of window W
13476 reference the same glyph memory as corresponding rows in the
13477 frame's frame matrix. This function is called after scrolling W's
13478 current matrix on a terminal frame in try_window_id and
13479 try_window_reusing_current_matrix. */
13480
13481 static void
13482 sync_frame_with_window_matrix_rows (w)
13483 struct window *w;
13484 {
13485 struct frame *f = XFRAME (w->frame);
13486 struct glyph_row *window_row, *window_row_end, *frame_row;
13487
13488 /* Preconditions: W must be a leaf window and full-width. Its frame
13489 must have a frame matrix. */
13490 xassert (NILP (w->hchild) && NILP (w->vchild));
13491 xassert (WINDOW_FULL_WIDTH_P (w));
13492 xassert (!FRAME_WINDOW_P (f));
13493
13494 /* If W is a full-width window, glyph pointers in W's current matrix
13495 have, by definition, to be the same as glyph pointers in the
13496 corresponding frame matrix. Note that frame matrices have no
13497 marginal areas (see build_frame_matrix). */
13498 window_row = w->current_matrix->rows;
13499 window_row_end = window_row + w->current_matrix->nrows;
13500 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13501 while (window_row < window_row_end)
13502 {
13503 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13504 struct glyph *end = window_row->glyphs[LAST_AREA];
13505
13506 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13507 frame_row->glyphs[TEXT_AREA] = start;
13508 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13509 frame_row->glyphs[LAST_AREA] = end;
13510
13511 /* Disable frame rows whose corresponding window rows have
13512 been disabled in try_window_id. */
13513 if (!window_row->enabled_p)
13514 frame_row->enabled_p = 0;
13515
13516 ++window_row, ++frame_row;
13517 }
13518 }
13519
13520
13521 /* Find the glyph row in window W containing CHARPOS. Consider all
13522 rows between START and END (not inclusive). END null means search
13523 all rows to the end of the display area of W. Value is the row
13524 containing CHARPOS or null. */
13525
13526 struct glyph_row *
13527 row_containing_pos (w, charpos, start, end, dy)
13528 struct window *w;
13529 int charpos;
13530 struct glyph_row *start, *end;
13531 int dy;
13532 {
13533 struct glyph_row *row = start;
13534 int last_y;
13535
13536 /* If we happen to start on a header-line, skip that. */
13537 if (row->mode_line_p)
13538 ++row;
13539
13540 if ((end && row >= end) || !row->enabled_p)
13541 return NULL;
13542
13543 last_y = window_text_bottom_y (w) - dy;
13544
13545 while (1)
13546 {
13547 /* Give up if we have gone too far. */
13548 if (end && row >= end)
13549 return NULL;
13550 /* This formerly returned if they were equal.
13551 I think that both quantities are of a "last plus one" type;
13552 if so, when they are equal, the row is within the screen. -- rms. */
13553 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13554 return NULL;
13555
13556 /* If it is in this row, return this row. */
13557 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13558 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13559 /* The end position of a row equals the start
13560 position of the next row. If CHARPOS is there, we
13561 would rather display it in the next line, except
13562 when this line ends in ZV. */
13563 && !row->ends_at_zv_p
13564 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13565 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13566 return row;
13567 ++row;
13568 }
13569 }
13570
13571
13572 /* Try to redisplay window W by reusing its existing display. W's
13573 current matrix must be up to date when this function is called,
13574 i.e. window_end_valid must not be nil.
13575
13576 Value is
13577
13578 1 if display has been updated
13579 0 if otherwise unsuccessful
13580 -1 if redisplay with same window start is known not to succeed
13581
13582 The following steps are performed:
13583
13584 1. Find the last row in the current matrix of W that is not
13585 affected by changes at the start of current_buffer. If no such row
13586 is found, give up.
13587
13588 2. Find the first row in W's current matrix that is not affected by
13589 changes at the end of current_buffer. Maybe there is no such row.
13590
13591 3. Display lines beginning with the row + 1 found in step 1 to the
13592 row found in step 2 or, if step 2 didn't find a row, to the end of
13593 the window.
13594
13595 4. If cursor is not known to appear on the window, give up.
13596
13597 5. If display stopped at the row found in step 2, scroll the
13598 display and current matrix as needed.
13599
13600 6. Maybe display some lines at the end of W, if we must. This can
13601 happen under various circumstances, like a partially visible line
13602 becoming fully visible, or because newly displayed lines are displayed
13603 in smaller font sizes.
13604
13605 7. Update W's window end information. */
13606
13607 static int
13608 try_window_id (w)
13609 struct window *w;
13610 {
13611 struct frame *f = XFRAME (w->frame);
13612 struct glyph_matrix *current_matrix = w->current_matrix;
13613 struct glyph_matrix *desired_matrix = w->desired_matrix;
13614 struct glyph_row *last_unchanged_at_beg_row;
13615 struct glyph_row *first_unchanged_at_end_row;
13616 struct glyph_row *row;
13617 struct glyph_row *bottom_row;
13618 int bottom_vpos;
13619 struct it it;
13620 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13621 struct text_pos start_pos;
13622 struct run run;
13623 int first_unchanged_at_end_vpos = 0;
13624 struct glyph_row *last_text_row, *last_text_row_at_end;
13625 struct text_pos start;
13626 int first_changed_charpos, last_changed_charpos;
13627
13628 #if GLYPH_DEBUG
13629 if (inhibit_try_window_id)
13630 return 0;
13631 #endif
13632
13633 /* This is handy for debugging. */
13634 #if 0
13635 #define GIVE_UP(X) \
13636 do { \
13637 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13638 return 0; \
13639 } while (0)
13640 #else
13641 #define GIVE_UP(X) return 0
13642 #endif
13643
13644 SET_TEXT_POS_FROM_MARKER (start, w->start);
13645
13646 /* Don't use this for mini-windows because these can show
13647 messages and mini-buffers, and we don't handle that here. */
13648 if (MINI_WINDOW_P (w))
13649 GIVE_UP (1);
13650
13651 /* This flag is used to prevent redisplay optimizations. */
13652 if (windows_or_buffers_changed || cursor_type_changed)
13653 GIVE_UP (2);
13654
13655 /* Verify that narrowing has not changed.
13656 Also verify that we were not told to prevent redisplay optimizations.
13657 It would be nice to further
13658 reduce the number of cases where this prevents try_window_id. */
13659 if (current_buffer->clip_changed
13660 || current_buffer->prevent_redisplay_optimizations_p)
13661 GIVE_UP (3);
13662
13663 /* Window must either use window-based redisplay or be full width. */
13664 if (!FRAME_WINDOW_P (f)
13665 && (!line_ins_del_ok
13666 || !WINDOW_FULL_WIDTH_P (w)))
13667 GIVE_UP (4);
13668
13669 /* Give up if point is not known NOT to appear in W. */
13670 if (PT < CHARPOS (start))
13671 GIVE_UP (5);
13672
13673 /* Another way to prevent redisplay optimizations. */
13674 if (XFASTINT (w->last_modified) == 0)
13675 GIVE_UP (6);
13676
13677 /* Verify that window is not hscrolled. */
13678 if (XFASTINT (w->hscroll) != 0)
13679 GIVE_UP (7);
13680
13681 /* Verify that display wasn't paused. */
13682 if (NILP (w->window_end_valid))
13683 GIVE_UP (8);
13684
13685 /* Can't use this if highlighting a region because a cursor movement
13686 will do more than just set the cursor. */
13687 if (!NILP (Vtransient_mark_mode)
13688 && !NILP (current_buffer->mark_active))
13689 GIVE_UP (9);
13690
13691 /* Likewise if highlighting trailing whitespace. */
13692 if (!NILP (Vshow_trailing_whitespace))
13693 GIVE_UP (11);
13694
13695 /* Likewise if showing a region. */
13696 if (!NILP (w->region_showing))
13697 GIVE_UP (10);
13698
13699 /* Can use this if overlay arrow position and or string have changed. */
13700 if (overlay_arrows_changed_p ())
13701 GIVE_UP (12);
13702
13703
13704 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13705 only if buffer has really changed. The reason is that the gap is
13706 initially at Z for freshly visited files. The code below would
13707 set end_unchanged to 0 in that case. */
13708 if (MODIFF > SAVE_MODIFF
13709 /* This seems to happen sometimes after saving a buffer. */
13710 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13711 {
13712 if (GPT - BEG < BEG_UNCHANGED)
13713 BEG_UNCHANGED = GPT - BEG;
13714 if (Z - GPT < END_UNCHANGED)
13715 END_UNCHANGED = Z - GPT;
13716 }
13717
13718 /* The position of the first and last character that has been changed. */
13719 first_changed_charpos = BEG + BEG_UNCHANGED;
13720 last_changed_charpos = Z - END_UNCHANGED;
13721
13722 /* If window starts after a line end, and the last change is in
13723 front of that newline, then changes don't affect the display.
13724 This case happens with stealth-fontification. Note that although
13725 the display is unchanged, glyph positions in the matrix have to
13726 be adjusted, of course. */
13727 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13728 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13729 && ((last_changed_charpos < CHARPOS (start)
13730 && CHARPOS (start) == BEGV)
13731 || (last_changed_charpos < CHARPOS (start) - 1
13732 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13733 {
13734 int Z_old, delta, Z_BYTE_old, delta_bytes;
13735 struct glyph_row *r0;
13736
13737 /* Compute how many chars/bytes have been added to or removed
13738 from the buffer. */
13739 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13740 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13741 delta = Z - Z_old;
13742 delta_bytes = Z_BYTE - Z_BYTE_old;
13743
13744 /* Give up if PT is not in the window. Note that it already has
13745 been checked at the start of try_window_id that PT is not in
13746 front of the window start. */
13747 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13748 GIVE_UP (13);
13749
13750 /* If window start is unchanged, we can reuse the whole matrix
13751 as is, after adjusting glyph positions. No need to compute
13752 the window end again, since its offset from Z hasn't changed. */
13753 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13754 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13755 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13756 /* PT must not be in a partially visible line. */
13757 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13758 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13759 {
13760 /* Adjust positions in the glyph matrix. */
13761 if (delta || delta_bytes)
13762 {
13763 struct glyph_row *r1
13764 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13765 increment_matrix_positions (w->current_matrix,
13766 MATRIX_ROW_VPOS (r0, current_matrix),
13767 MATRIX_ROW_VPOS (r1, current_matrix),
13768 delta, delta_bytes);
13769 }
13770
13771 /* Set the cursor. */
13772 row = row_containing_pos (w, PT, r0, NULL, 0);
13773 if (row)
13774 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13775 else
13776 abort ();
13777 return 1;
13778 }
13779 }
13780
13781 /* Handle the case that changes are all below what is displayed in
13782 the window, and that PT is in the window. This shortcut cannot
13783 be taken if ZV is visible in the window, and text has been added
13784 there that is visible in the window. */
13785 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13786 /* ZV is not visible in the window, or there are no
13787 changes at ZV, actually. */
13788 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13789 || first_changed_charpos == last_changed_charpos))
13790 {
13791 struct glyph_row *r0;
13792
13793 /* Give up if PT is not in the window. Note that it already has
13794 been checked at the start of try_window_id that PT is not in
13795 front of the window start. */
13796 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13797 GIVE_UP (14);
13798
13799 /* If window start is unchanged, we can reuse the whole matrix
13800 as is, without changing glyph positions since no text has
13801 been added/removed in front of the window end. */
13802 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13803 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13804 /* PT must not be in a partially visible line. */
13805 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13806 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13807 {
13808 /* We have to compute the window end anew since text
13809 can have been added/removed after it. */
13810 w->window_end_pos
13811 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13812 w->window_end_bytepos
13813 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13814
13815 /* Set the cursor. */
13816 row = row_containing_pos (w, PT, r0, NULL, 0);
13817 if (row)
13818 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13819 else
13820 abort ();
13821 return 2;
13822 }
13823 }
13824
13825 /* Give up if window start is in the changed area.
13826
13827 The condition used to read
13828
13829 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13830
13831 but why that was tested escapes me at the moment. */
13832 if (CHARPOS (start) >= first_changed_charpos
13833 && CHARPOS (start) <= last_changed_charpos)
13834 GIVE_UP (15);
13835
13836 /* Check that window start agrees with the start of the first glyph
13837 row in its current matrix. Check this after we know the window
13838 start is not in changed text, otherwise positions would not be
13839 comparable. */
13840 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13841 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13842 GIVE_UP (16);
13843
13844 /* Give up if the window ends in strings. Overlay strings
13845 at the end are difficult to handle, so don't try. */
13846 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13847 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13848 GIVE_UP (20);
13849
13850 /* Compute the position at which we have to start displaying new
13851 lines. Some of the lines at the top of the window might be
13852 reusable because they are not displaying changed text. Find the
13853 last row in W's current matrix not affected by changes at the
13854 start of current_buffer. Value is null if changes start in the
13855 first line of window. */
13856 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13857 if (last_unchanged_at_beg_row)
13858 {
13859 /* Avoid starting to display in the moddle of a character, a TAB
13860 for instance. This is easier than to set up the iterator
13861 exactly, and it's not a frequent case, so the additional
13862 effort wouldn't really pay off. */
13863 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13864 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13865 && last_unchanged_at_beg_row > w->current_matrix->rows)
13866 --last_unchanged_at_beg_row;
13867
13868 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13869 GIVE_UP (17);
13870
13871 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13872 GIVE_UP (18);
13873 start_pos = it.current.pos;
13874
13875 /* Start displaying new lines in the desired matrix at the same
13876 vpos we would use in the current matrix, i.e. below
13877 last_unchanged_at_beg_row. */
13878 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13879 current_matrix);
13880 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13881 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13882
13883 xassert (it.hpos == 0 && it.current_x == 0);
13884 }
13885 else
13886 {
13887 /* There are no reusable lines at the start of the window.
13888 Start displaying in the first text line. */
13889 start_display (&it, w, start);
13890 it.vpos = it.first_vpos;
13891 start_pos = it.current.pos;
13892 }
13893
13894 /* Find the first row that is not affected by changes at the end of
13895 the buffer. Value will be null if there is no unchanged row, in
13896 which case we must redisplay to the end of the window. delta
13897 will be set to the value by which buffer positions beginning with
13898 first_unchanged_at_end_row have to be adjusted due to text
13899 changes. */
13900 first_unchanged_at_end_row
13901 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13902 IF_DEBUG (debug_delta = delta);
13903 IF_DEBUG (debug_delta_bytes = delta_bytes);
13904
13905 /* Set stop_pos to the buffer position up to which we will have to
13906 display new lines. If first_unchanged_at_end_row != NULL, this
13907 is the buffer position of the start of the line displayed in that
13908 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13909 that we don't stop at a buffer position. */
13910 stop_pos = 0;
13911 if (first_unchanged_at_end_row)
13912 {
13913 xassert (last_unchanged_at_beg_row == NULL
13914 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13915
13916 /* If this is a continuation line, move forward to the next one
13917 that isn't. Changes in lines above affect this line.
13918 Caution: this may move first_unchanged_at_end_row to a row
13919 not displaying text. */
13920 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13921 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13922 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13923 < it.last_visible_y))
13924 ++first_unchanged_at_end_row;
13925
13926 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13927 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13928 >= it.last_visible_y))
13929 first_unchanged_at_end_row = NULL;
13930 else
13931 {
13932 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13933 + delta);
13934 first_unchanged_at_end_vpos
13935 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13936 xassert (stop_pos >= Z - END_UNCHANGED);
13937 }
13938 }
13939 else if (last_unchanged_at_beg_row == NULL)
13940 GIVE_UP (19);
13941
13942
13943 #if GLYPH_DEBUG
13944
13945 /* Either there is no unchanged row at the end, or the one we have
13946 now displays text. This is a necessary condition for the window
13947 end pos calculation at the end of this function. */
13948 xassert (first_unchanged_at_end_row == NULL
13949 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13950
13951 debug_last_unchanged_at_beg_vpos
13952 = (last_unchanged_at_beg_row
13953 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13954 : -1);
13955 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13956
13957 #endif /* GLYPH_DEBUG != 0 */
13958
13959
13960 /* Display new lines. Set last_text_row to the last new line
13961 displayed which has text on it, i.e. might end up as being the
13962 line where the window_end_vpos is. */
13963 w->cursor.vpos = -1;
13964 last_text_row = NULL;
13965 overlay_arrow_seen = 0;
13966 while (it.current_y < it.last_visible_y
13967 && !fonts_changed_p
13968 && (first_unchanged_at_end_row == NULL
13969 || IT_CHARPOS (it) < stop_pos))
13970 {
13971 if (display_line (&it))
13972 last_text_row = it.glyph_row - 1;
13973 }
13974
13975 if (fonts_changed_p)
13976 return -1;
13977
13978
13979 /* Compute differences in buffer positions, y-positions etc. for
13980 lines reused at the bottom of the window. Compute what we can
13981 scroll. */
13982 if (first_unchanged_at_end_row
13983 /* No lines reused because we displayed everything up to the
13984 bottom of the window. */
13985 && it.current_y < it.last_visible_y)
13986 {
13987 dvpos = (it.vpos
13988 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13989 current_matrix));
13990 dy = it.current_y - first_unchanged_at_end_row->y;
13991 run.current_y = first_unchanged_at_end_row->y;
13992 run.desired_y = run.current_y + dy;
13993 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13994 }
13995 else
13996 {
13997 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13998 first_unchanged_at_end_row = NULL;
13999 }
14000 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14001
14002
14003 /* Find the cursor if not already found. We have to decide whether
14004 PT will appear on this window (it sometimes doesn't, but this is
14005 not a very frequent case.) This decision has to be made before
14006 the current matrix is altered. A value of cursor.vpos < 0 means
14007 that PT is either in one of the lines beginning at
14008 first_unchanged_at_end_row or below the window. Don't care for
14009 lines that might be displayed later at the window end; as
14010 mentioned, this is not a frequent case. */
14011 if (w->cursor.vpos < 0)
14012 {
14013 /* Cursor in unchanged rows at the top? */
14014 if (PT < CHARPOS (start_pos)
14015 && last_unchanged_at_beg_row)
14016 {
14017 row = row_containing_pos (w, PT,
14018 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14019 last_unchanged_at_beg_row + 1, 0);
14020 if (row)
14021 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14022 }
14023
14024 /* Start from first_unchanged_at_end_row looking for PT. */
14025 else if (first_unchanged_at_end_row)
14026 {
14027 row = row_containing_pos (w, PT - delta,
14028 first_unchanged_at_end_row, NULL, 0);
14029 if (row)
14030 set_cursor_from_row (w, row, w->current_matrix, delta,
14031 delta_bytes, dy, dvpos);
14032 }
14033
14034 /* Give up if cursor was not found. */
14035 if (w->cursor.vpos < 0)
14036 {
14037 clear_glyph_matrix (w->desired_matrix);
14038 return -1;
14039 }
14040 }
14041
14042 /* Don't let the cursor end in the scroll margins. */
14043 {
14044 int this_scroll_margin, cursor_height;
14045
14046 this_scroll_margin = max (0, scroll_margin);
14047 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14048 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14049 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14050
14051 if ((w->cursor.y < this_scroll_margin
14052 && CHARPOS (start) > BEGV)
14053 /* Old redisplay didn't take scroll margin into account at the bottom,
14054 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14055 || (w->cursor.y + (make_cursor_line_fully_visible_p
14056 ? cursor_height + this_scroll_margin
14057 : 1)) > it.last_visible_y)
14058 {
14059 w->cursor.vpos = -1;
14060 clear_glyph_matrix (w->desired_matrix);
14061 return -1;
14062 }
14063 }
14064
14065 /* Scroll the display. Do it before changing the current matrix so
14066 that xterm.c doesn't get confused about where the cursor glyph is
14067 found. */
14068 if (dy && run.height)
14069 {
14070 update_begin (f);
14071
14072 if (FRAME_WINDOW_P (f))
14073 {
14074 rif->update_window_begin_hook (w);
14075 rif->clear_window_mouse_face (w);
14076 rif->scroll_run_hook (w, &run);
14077 rif->update_window_end_hook (w, 0, 0);
14078 }
14079 else
14080 {
14081 /* Terminal frame. In this case, dvpos gives the number of
14082 lines to scroll by; dvpos < 0 means scroll up. */
14083 int first_unchanged_at_end_vpos
14084 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14085 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14086 int end = (WINDOW_TOP_EDGE_LINE (w)
14087 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14088 + window_internal_height (w));
14089
14090 /* Perform the operation on the screen. */
14091 if (dvpos > 0)
14092 {
14093 /* Scroll last_unchanged_at_beg_row to the end of the
14094 window down dvpos lines. */
14095 set_terminal_window (end);
14096
14097 /* On dumb terminals delete dvpos lines at the end
14098 before inserting dvpos empty lines. */
14099 if (!scroll_region_ok)
14100 ins_del_lines (end - dvpos, -dvpos);
14101
14102 /* Insert dvpos empty lines in front of
14103 last_unchanged_at_beg_row. */
14104 ins_del_lines (from, dvpos);
14105 }
14106 else if (dvpos < 0)
14107 {
14108 /* Scroll up last_unchanged_at_beg_vpos to the end of
14109 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14110 set_terminal_window (end);
14111
14112 /* Delete dvpos lines in front of
14113 last_unchanged_at_beg_vpos. ins_del_lines will set
14114 the cursor to the given vpos and emit |dvpos| delete
14115 line sequences. */
14116 ins_del_lines (from + dvpos, dvpos);
14117
14118 /* On a dumb terminal insert dvpos empty lines at the
14119 end. */
14120 if (!scroll_region_ok)
14121 ins_del_lines (end + dvpos, -dvpos);
14122 }
14123
14124 set_terminal_window (0);
14125 }
14126
14127 update_end (f);
14128 }
14129
14130 /* Shift reused rows of the current matrix to the right position.
14131 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14132 text. */
14133 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14134 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14135 if (dvpos < 0)
14136 {
14137 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14138 bottom_vpos, dvpos);
14139 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14140 bottom_vpos, 0);
14141 }
14142 else if (dvpos > 0)
14143 {
14144 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14145 bottom_vpos, dvpos);
14146 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14147 first_unchanged_at_end_vpos + dvpos, 0);
14148 }
14149
14150 /* For frame-based redisplay, make sure that current frame and window
14151 matrix are in sync with respect to glyph memory. */
14152 if (!FRAME_WINDOW_P (f))
14153 sync_frame_with_window_matrix_rows (w);
14154
14155 /* Adjust buffer positions in reused rows. */
14156 if (delta)
14157 increment_matrix_positions (current_matrix,
14158 first_unchanged_at_end_vpos + dvpos,
14159 bottom_vpos, delta, delta_bytes);
14160
14161 /* Adjust Y positions. */
14162 if (dy)
14163 shift_glyph_matrix (w, current_matrix,
14164 first_unchanged_at_end_vpos + dvpos,
14165 bottom_vpos, dy);
14166
14167 if (first_unchanged_at_end_row)
14168 {
14169 first_unchanged_at_end_row += dvpos;
14170 if (first_unchanged_at_end_row->y >= it.last_visible_y
14171 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14172 first_unchanged_at_end_row = NULL;
14173 }
14174
14175 /* If scrolling up, there may be some lines to display at the end of
14176 the window. */
14177 last_text_row_at_end = NULL;
14178 if (dy < 0)
14179 {
14180 /* Scrolling up can leave for example a partially visible line
14181 at the end of the window to be redisplayed. */
14182 /* Set last_row to the glyph row in the current matrix where the
14183 window end line is found. It has been moved up or down in
14184 the matrix by dvpos. */
14185 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14186 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14187
14188 /* If last_row is the window end line, it should display text. */
14189 xassert (last_row->displays_text_p);
14190
14191 /* If window end line was partially visible before, begin
14192 displaying at that line. Otherwise begin displaying with the
14193 line following it. */
14194 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14195 {
14196 init_to_row_start (&it, w, last_row);
14197 it.vpos = last_vpos;
14198 it.current_y = last_row->y;
14199 }
14200 else
14201 {
14202 init_to_row_end (&it, w, last_row);
14203 it.vpos = 1 + last_vpos;
14204 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14205 ++last_row;
14206 }
14207
14208 /* We may start in a continuation line. If so, we have to
14209 get the right continuation_lines_width and current_x. */
14210 it.continuation_lines_width = last_row->continuation_lines_width;
14211 it.hpos = it.current_x = 0;
14212
14213 /* Display the rest of the lines at the window end. */
14214 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14215 while (it.current_y < it.last_visible_y
14216 && !fonts_changed_p)
14217 {
14218 /* Is it always sure that the display agrees with lines in
14219 the current matrix? I don't think so, so we mark rows
14220 displayed invalid in the current matrix by setting their
14221 enabled_p flag to zero. */
14222 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14223 if (display_line (&it))
14224 last_text_row_at_end = it.glyph_row - 1;
14225 }
14226 }
14227
14228 /* Update window_end_pos and window_end_vpos. */
14229 if (first_unchanged_at_end_row
14230 && !last_text_row_at_end)
14231 {
14232 /* Window end line if one of the preserved rows from the current
14233 matrix. Set row to the last row displaying text in current
14234 matrix starting at first_unchanged_at_end_row, after
14235 scrolling. */
14236 xassert (first_unchanged_at_end_row->displays_text_p);
14237 row = find_last_row_displaying_text (w->current_matrix, &it,
14238 first_unchanged_at_end_row);
14239 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14240
14241 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14242 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14243 w->window_end_vpos
14244 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14245 xassert (w->window_end_bytepos >= 0);
14246 IF_DEBUG (debug_method_add (w, "A"));
14247 }
14248 else if (last_text_row_at_end)
14249 {
14250 w->window_end_pos
14251 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14252 w->window_end_bytepos
14253 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14254 w->window_end_vpos
14255 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14256 xassert (w->window_end_bytepos >= 0);
14257 IF_DEBUG (debug_method_add (w, "B"));
14258 }
14259 else if (last_text_row)
14260 {
14261 /* We have displayed either to the end of the window or at the
14262 end of the window, i.e. the last row with text is to be found
14263 in the desired matrix. */
14264 w->window_end_pos
14265 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14266 w->window_end_bytepos
14267 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14268 w->window_end_vpos
14269 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14270 xassert (w->window_end_bytepos >= 0);
14271 }
14272 else if (first_unchanged_at_end_row == NULL
14273 && last_text_row == NULL
14274 && last_text_row_at_end == NULL)
14275 {
14276 /* Displayed to end of window, but no line containing text was
14277 displayed. Lines were deleted at the end of the window. */
14278 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14279 int vpos = XFASTINT (w->window_end_vpos);
14280 struct glyph_row *current_row = current_matrix->rows + vpos;
14281 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14282
14283 for (row = NULL;
14284 row == NULL && vpos >= first_vpos;
14285 --vpos, --current_row, --desired_row)
14286 {
14287 if (desired_row->enabled_p)
14288 {
14289 if (desired_row->displays_text_p)
14290 row = desired_row;
14291 }
14292 else if (current_row->displays_text_p)
14293 row = current_row;
14294 }
14295
14296 xassert (row != NULL);
14297 w->window_end_vpos = make_number (vpos + 1);
14298 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14299 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14300 xassert (w->window_end_bytepos >= 0);
14301 IF_DEBUG (debug_method_add (w, "C"));
14302 }
14303 else
14304 abort ();
14305
14306 #if 0 /* This leads to problems, for instance when the cursor is
14307 at ZV, and the cursor line displays no text. */
14308 /* Disable rows below what's displayed in the window. This makes
14309 debugging easier. */
14310 enable_glyph_matrix_rows (current_matrix,
14311 XFASTINT (w->window_end_vpos) + 1,
14312 bottom_vpos, 0);
14313 #endif
14314
14315 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14316 debug_end_vpos = XFASTINT (w->window_end_vpos));
14317
14318 /* Record that display has not been completed. */
14319 w->window_end_valid = Qnil;
14320 w->desired_matrix->no_scrolling_p = 1;
14321 return 3;
14322
14323 #undef GIVE_UP
14324 }
14325
14326
14327 \f
14328 /***********************************************************************
14329 More debugging support
14330 ***********************************************************************/
14331
14332 #if GLYPH_DEBUG
14333
14334 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14335 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14336 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14337
14338
14339 /* Dump the contents of glyph matrix MATRIX on stderr.
14340
14341 GLYPHS 0 means don't show glyph contents.
14342 GLYPHS 1 means show glyphs in short form
14343 GLYPHS > 1 means show glyphs in long form. */
14344
14345 void
14346 dump_glyph_matrix (matrix, glyphs)
14347 struct glyph_matrix *matrix;
14348 int glyphs;
14349 {
14350 int i;
14351 for (i = 0; i < matrix->nrows; ++i)
14352 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14353 }
14354
14355
14356 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14357 the glyph row and area where the glyph comes from. */
14358
14359 void
14360 dump_glyph (row, glyph, area)
14361 struct glyph_row *row;
14362 struct glyph *glyph;
14363 int area;
14364 {
14365 if (glyph->type == CHAR_GLYPH)
14366 {
14367 fprintf (stderr,
14368 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14369 glyph - row->glyphs[TEXT_AREA],
14370 'C',
14371 glyph->charpos,
14372 (BUFFERP (glyph->object)
14373 ? 'B'
14374 : (STRINGP (glyph->object)
14375 ? 'S'
14376 : '-')),
14377 glyph->pixel_width,
14378 glyph->u.ch,
14379 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14380 ? glyph->u.ch
14381 : '.'),
14382 glyph->face_id,
14383 glyph->left_box_line_p,
14384 glyph->right_box_line_p);
14385 }
14386 else if (glyph->type == STRETCH_GLYPH)
14387 {
14388 fprintf (stderr,
14389 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14390 glyph - row->glyphs[TEXT_AREA],
14391 'S',
14392 glyph->charpos,
14393 (BUFFERP (glyph->object)
14394 ? 'B'
14395 : (STRINGP (glyph->object)
14396 ? 'S'
14397 : '-')),
14398 glyph->pixel_width,
14399 0,
14400 '.',
14401 glyph->face_id,
14402 glyph->left_box_line_p,
14403 glyph->right_box_line_p);
14404 }
14405 else if (glyph->type == IMAGE_GLYPH)
14406 {
14407 fprintf (stderr,
14408 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14409 glyph - row->glyphs[TEXT_AREA],
14410 'I',
14411 glyph->charpos,
14412 (BUFFERP (glyph->object)
14413 ? 'B'
14414 : (STRINGP (glyph->object)
14415 ? 'S'
14416 : '-')),
14417 glyph->pixel_width,
14418 glyph->u.img_id,
14419 '.',
14420 glyph->face_id,
14421 glyph->left_box_line_p,
14422 glyph->right_box_line_p);
14423 }
14424 }
14425
14426
14427 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14428 GLYPHS 0 means don't show glyph contents.
14429 GLYPHS 1 means show glyphs in short form
14430 GLYPHS > 1 means show glyphs in long form. */
14431
14432 void
14433 dump_glyph_row (row, vpos, glyphs)
14434 struct glyph_row *row;
14435 int vpos, glyphs;
14436 {
14437 if (glyphs != 1)
14438 {
14439 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14440 fprintf (stderr, "======================================================================\n");
14441
14442 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14443 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14444 vpos,
14445 MATRIX_ROW_START_CHARPOS (row),
14446 MATRIX_ROW_END_CHARPOS (row),
14447 row->used[TEXT_AREA],
14448 row->contains_overlapping_glyphs_p,
14449 row->enabled_p,
14450 row->truncated_on_left_p,
14451 row->truncated_on_right_p,
14452 row->continued_p,
14453 MATRIX_ROW_CONTINUATION_LINE_P (row),
14454 row->displays_text_p,
14455 row->ends_at_zv_p,
14456 row->fill_line_p,
14457 row->ends_in_middle_of_char_p,
14458 row->starts_in_middle_of_char_p,
14459 row->mouse_face_p,
14460 row->x,
14461 row->y,
14462 row->pixel_width,
14463 row->height,
14464 row->visible_height,
14465 row->ascent,
14466 row->phys_ascent);
14467 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14468 row->end.overlay_string_index,
14469 row->continuation_lines_width);
14470 fprintf (stderr, "%9d %5d\n",
14471 CHARPOS (row->start.string_pos),
14472 CHARPOS (row->end.string_pos));
14473 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14474 row->end.dpvec_index);
14475 }
14476
14477 if (glyphs > 1)
14478 {
14479 int area;
14480
14481 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14482 {
14483 struct glyph *glyph = row->glyphs[area];
14484 struct glyph *glyph_end = glyph + row->used[area];
14485
14486 /* Glyph for a line end in text. */
14487 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14488 ++glyph_end;
14489
14490 if (glyph < glyph_end)
14491 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14492
14493 for (; glyph < glyph_end; ++glyph)
14494 dump_glyph (row, glyph, area);
14495 }
14496 }
14497 else if (glyphs == 1)
14498 {
14499 int area;
14500
14501 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14502 {
14503 char *s = (char *) alloca (row->used[area] + 1);
14504 int i;
14505
14506 for (i = 0; i < row->used[area]; ++i)
14507 {
14508 struct glyph *glyph = row->glyphs[area] + i;
14509 if (glyph->type == CHAR_GLYPH
14510 && glyph->u.ch < 0x80
14511 && glyph->u.ch >= ' ')
14512 s[i] = glyph->u.ch;
14513 else
14514 s[i] = '.';
14515 }
14516
14517 s[i] = '\0';
14518 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14519 }
14520 }
14521 }
14522
14523
14524 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14525 Sdump_glyph_matrix, 0, 1, "p",
14526 doc: /* Dump the current matrix of the selected window to stderr.
14527 Shows contents of glyph row structures. With non-nil
14528 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14529 glyphs in short form, otherwise show glyphs in long form. */)
14530 (glyphs)
14531 Lisp_Object glyphs;
14532 {
14533 struct window *w = XWINDOW (selected_window);
14534 struct buffer *buffer = XBUFFER (w->buffer);
14535
14536 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14537 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14538 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14539 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14540 fprintf (stderr, "=============================================\n");
14541 dump_glyph_matrix (w->current_matrix,
14542 NILP (glyphs) ? 0 : XINT (glyphs));
14543 return Qnil;
14544 }
14545
14546
14547 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14548 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14549 ()
14550 {
14551 struct frame *f = XFRAME (selected_frame);
14552 dump_glyph_matrix (f->current_matrix, 1);
14553 return Qnil;
14554 }
14555
14556
14557 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14558 doc: /* Dump glyph row ROW to stderr.
14559 GLYPH 0 means don't dump glyphs.
14560 GLYPH 1 means dump glyphs in short form.
14561 GLYPH > 1 or omitted means dump glyphs in long form. */)
14562 (row, glyphs)
14563 Lisp_Object row, glyphs;
14564 {
14565 struct glyph_matrix *matrix;
14566 int vpos;
14567
14568 CHECK_NUMBER (row);
14569 matrix = XWINDOW (selected_window)->current_matrix;
14570 vpos = XINT (row);
14571 if (vpos >= 0 && vpos < matrix->nrows)
14572 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14573 vpos,
14574 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14575 return Qnil;
14576 }
14577
14578
14579 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14580 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14581 GLYPH 0 means don't dump glyphs.
14582 GLYPH 1 means dump glyphs in short form.
14583 GLYPH > 1 or omitted means dump glyphs in long form. */)
14584 (row, glyphs)
14585 Lisp_Object row, glyphs;
14586 {
14587 struct frame *sf = SELECTED_FRAME ();
14588 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14589 int vpos;
14590
14591 CHECK_NUMBER (row);
14592 vpos = XINT (row);
14593 if (vpos >= 0 && vpos < m->nrows)
14594 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14595 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14596 return Qnil;
14597 }
14598
14599
14600 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14601 doc: /* Toggle tracing of redisplay.
14602 With ARG, turn tracing on if and only if ARG is positive. */)
14603 (arg)
14604 Lisp_Object arg;
14605 {
14606 if (NILP (arg))
14607 trace_redisplay_p = !trace_redisplay_p;
14608 else
14609 {
14610 arg = Fprefix_numeric_value (arg);
14611 trace_redisplay_p = XINT (arg) > 0;
14612 }
14613
14614 return Qnil;
14615 }
14616
14617
14618 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14619 doc: /* Like `format', but print result to stderr.
14620 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14621 (nargs, args)
14622 int nargs;
14623 Lisp_Object *args;
14624 {
14625 Lisp_Object s = Fformat (nargs, args);
14626 fprintf (stderr, "%s", SDATA (s));
14627 return Qnil;
14628 }
14629
14630 #endif /* GLYPH_DEBUG */
14631
14632
14633 \f
14634 /***********************************************************************
14635 Building Desired Matrix Rows
14636 ***********************************************************************/
14637
14638 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14639 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14640
14641 static struct glyph_row *
14642 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14643 struct window *w;
14644 Lisp_Object overlay_arrow_string;
14645 {
14646 struct frame *f = XFRAME (WINDOW_FRAME (w));
14647 struct buffer *buffer = XBUFFER (w->buffer);
14648 struct buffer *old = current_buffer;
14649 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14650 int arrow_len = SCHARS (overlay_arrow_string);
14651 const unsigned char *arrow_end = arrow_string + arrow_len;
14652 const unsigned char *p;
14653 struct it it;
14654 int multibyte_p;
14655 int n_glyphs_before;
14656
14657 set_buffer_temp (buffer);
14658 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14659 it.glyph_row->used[TEXT_AREA] = 0;
14660 SET_TEXT_POS (it.position, 0, 0);
14661
14662 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14663 p = arrow_string;
14664 while (p < arrow_end)
14665 {
14666 Lisp_Object face, ilisp;
14667
14668 /* Get the next character. */
14669 if (multibyte_p)
14670 it.c = string_char_and_length (p, arrow_len, &it.len);
14671 else
14672 it.c = *p, it.len = 1;
14673 p += it.len;
14674
14675 /* Get its face. */
14676 ilisp = make_number (p - arrow_string);
14677 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14678 it.face_id = compute_char_face (f, it.c, face);
14679
14680 /* Compute its width, get its glyphs. */
14681 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14682 SET_TEXT_POS (it.position, -1, -1);
14683 PRODUCE_GLYPHS (&it);
14684
14685 /* If this character doesn't fit any more in the line, we have
14686 to remove some glyphs. */
14687 if (it.current_x > it.last_visible_x)
14688 {
14689 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14690 break;
14691 }
14692 }
14693
14694 set_buffer_temp (old);
14695 return it.glyph_row;
14696 }
14697
14698
14699 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14700 glyphs are only inserted for terminal frames since we can't really
14701 win with truncation glyphs when partially visible glyphs are
14702 involved. Which glyphs to insert is determined by
14703 produce_special_glyphs. */
14704
14705 static void
14706 insert_left_trunc_glyphs (it)
14707 struct it *it;
14708 {
14709 struct it truncate_it;
14710 struct glyph *from, *end, *to, *toend;
14711
14712 xassert (!FRAME_WINDOW_P (it->f));
14713
14714 /* Get the truncation glyphs. */
14715 truncate_it = *it;
14716 truncate_it.current_x = 0;
14717 truncate_it.face_id = DEFAULT_FACE_ID;
14718 truncate_it.glyph_row = &scratch_glyph_row;
14719 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14720 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14721 truncate_it.object = make_number (0);
14722 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14723
14724 /* Overwrite glyphs from IT with truncation glyphs. */
14725 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14726 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14727 to = it->glyph_row->glyphs[TEXT_AREA];
14728 toend = to + it->glyph_row->used[TEXT_AREA];
14729
14730 while (from < end)
14731 *to++ = *from++;
14732
14733 /* There may be padding glyphs left over. Overwrite them too. */
14734 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14735 {
14736 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14737 while (from < end)
14738 *to++ = *from++;
14739 }
14740
14741 if (to > toend)
14742 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14743 }
14744
14745
14746 /* Compute the pixel height and width of IT->glyph_row.
14747
14748 Most of the time, ascent and height of a display line will be equal
14749 to the max_ascent and max_height values of the display iterator
14750 structure. This is not the case if
14751
14752 1. We hit ZV without displaying anything. In this case, max_ascent
14753 and max_height will be zero.
14754
14755 2. We have some glyphs that don't contribute to the line height.
14756 (The glyph row flag contributes_to_line_height_p is for future
14757 pixmap extensions).
14758
14759 The first case is easily covered by using default values because in
14760 these cases, the line height does not really matter, except that it
14761 must not be zero. */
14762
14763 static void
14764 compute_line_metrics (it)
14765 struct it *it;
14766 {
14767 struct glyph_row *row = it->glyph_row;
14768 int area, i;
14769
14770 if (FRAME_WINDOW_P (it->f))
14771 {
14772 int i, min_y, max_y;
14773
14774 /* The line may consist of one space only, that was added to
14775 place the cursor on it. If so, the row's height hasn't been
14776 computed yet. */
14777 if (row->height == 0)
14778 {
14779 if (it->max_ascent + it->max_descent == 0)
14780 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14781 row->ascent = it->max_ascent;
14782 row->height = it->max_ascent + it->max_descent;
14783 row->phys_ascent = it->max_phys_ascent;
14784 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14785 row->extra_line_spacing = it->max_extra_line_spacing;
14786 }
14787
14788 /* Compute the width of this line. */
14789 row->pixel_width = row->x;
14790 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14791 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14792
14793 xassert (row->pixel_width >= 0);
14794 xassert (row->ascent >= 0 && row->height > 0);
14795
14796 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14797 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14798
14799 /* If first line's physical ascent is larger than its logical
14800 ascent, use the physical ascent, and make the row taller.
14801 This makes accented characters fully visible. */
14802 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14803 && row->phys_ascent > row->ascent)
14804 {
14805 row->height += row->phys_ascent - row->ascent;
14806 row->ascent = row->phys_ascent;
14807 }
14808
14809 /* Compute how much of the line is visible. */
14810 row->visible_height = row->height;
14811
14812 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14813 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14814
14815 if (row->y < min_y)
14816 row->visible_height -= min_y - row->y;
14817 if (row->y + row->height > max_y)
14818 row->visible_height -= row->y + row->height - max_y;
14819 }
14820 else
14821 {
14822 row->pixel_width = row->used[TEXT_AREA];
14823 if (row->continued_p)
14824 row->pixel_width -= it->continuation_pixel_width;
14825 else if (row->truncated_on_right_p)
14826 row->pixel_width -= it->truncation_pixel_width;
14827 row->ascent = row->phys_ascent = 0;
14828 row->height = row->phys_height = row->visible_height = 1;
14829 row->extra_line_spacing = 0;
14830 }
14831
14832 /* Compute a hash code for this row. */
14833 row->hash = 0;
14834 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14835 for (i = 0; i < row->used[area]; ++i)
14836 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14837 + row->glyphs[area][i].u.val
14838 + row->glyphs[area][i].face_id
14839 + row->glyphs[area][i].padding_p
14840 + (row->glyphs[area][i].type << 2));
14841
14842 it->max_ascent = it->max_descent = 0;
14843 it->max_phys_ascent = it->max_phys_descent = 0;
14844 }
14845
14846
14847 /* Append one space to the glyph row of iterator IT if doing a
14848 window-based redisplay. The space has the same face as
14849 IT->face_id. Value is non-zero if a space was added.
14850
14851 This function is called to make sure that there is always one glyph
14852 at the end of a glyph row that the cursor can be set on under
14853 window-systems. (If there weren't such a glyph we would not know
14854 how wide and tall a box cursor should be displayed).
14855
14856 At the same time this space let's a nicely handle clearing to the
14857 end of the line if the row ends in italic text. */
14858
14859 static int
14860 append_space_for_newline (it, default_face_p)
14861 struct it *it;
14862 int default_face_p;
14863 {
14864 if (FRAME_WINDOW_P (it->f))
14865 {
14866 int n = it->glyph_row->used[TEXT_AREA];
14867
14868 if (it->glyph_row->glyphs[TEXT_AREA] + n
14869 < it->glyph_row->glyphs[1 + TEXT_AREA])
14870 {
14871 /* Save some values that must not be changed.
14872 Must save IT->c and IT->len because otherwise
14873 ITERATOR_AT_END_P wouldn't work anymore after
14874 append_space_for_newline has been called. */
14875 enum display_element_type saved_what = it->what;
14876 int saved_c = it->c, saved_len = it->len;
14877 int saved_x = it->current_x;
14878 int saved_face_id = it->face_id;
14879 struct text_pos saved_pos;
14880 Lisp_Object saved_object;
14881 struct face *face;
14882
14883 saved_object = it->object;
14884 saved_pos = it->position;
14885
14886 it->what = IT_CHARACTER;
14887 bzero (&it->position, sizeof it->position);
14888 it->object = make_number (0);
14889 it->c = ' ';
14890 it->len = 1;
14891
14892 if (default_face_p)
14893 it->face_id = DEFAULT_FACE_ID;
14894 else if (it->face_before_selective_p)
14895 it->face_id = it->saved_face_id;
14896 face = FACE_FROM_ID (it->f, it->face_id);
14897 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
14898
14899 PRODUCE_GLYPHS (it);
14900
14901 it->override_ascent = -1;
14902 it->constrain_row_ascent_descent_p = 0;
14903 it->current_x = saved_x;
14904 it->object = saved_object;
14905 it->position = saved_pos;
14906 it->what = saved_what;
14907 it->face_id = saved_face_id;
14908 it->len = saved_len;
14909 it->c = saved_c;
14910 return 1;
14911 }
14912 }
14913
14914 return 0;
14915 }
14916
14917
14918 /* Extend the face of the last glyph in the text area of IT->glyph_row
14919 to the end of the display line. Called from display_line.
14920 If the glyph row is empty, add a space glyph to it so that we
14921 know the face to draw. Set the glyph row flag fill_line_p. */
14922
14923 static void
14924 extend_face_to_end_of_line (it)
14925 struct it *it;
14926 {
14927 struct face *face;
14928 struct frame *f = it->f;
14929
14930 /* If line is already filled, do nothing. */
14931 if (it->current_x >= it->last_visible_x)
14932 return;
14933
14934 /* Face extension extends the background and box of IT->face_id
14935 to the end of the line. If the background equals the background
14936 of the frame, we don't have to do anything. */
14937 if (it->face_before_selective_p)
14938 face = FACE_FROM_ID (it->f, it->saved_face_id);
14939 else
14940 face = FACE_FROM_ID (f, it->face_id);
14941
14942 if (FRAME_WINDOW_P (f)
14943 && face->box == FACE_NO_BOX
14944 && face->background == FRAME_BACKGROUND_PIXEL (f)
14945 && !face->stipple)
14946 return;
14947
14948 /* Set the glyph row flag indicating that the face of the last glyph
14949 in the text area has to be drawn to the end of the text area. */
14950 it->glyph_row->fill_line_p = 1;
14951
14952 /* If current character of IT is not ASCII, make sure we have the
14953 ASCII face. This will be automatically undone the next time
14954 get_next_display_element returns a multibyte character. Note
14955 that the character will always be single byte in unibyte text. */
14956 if (!ASCII_CHAR_P (it->c))
14957 {
14958 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
14959 }
14960
14961 if (FRAME_WINDOW_P (f))
14962 {
14963 /* If the row is empty, add a space with the current face of IT,
14964 so that we know which face to draw. */
14965 if (it->glyph_row->used[TEXT_AREA] == 0)
14966 {
14967 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14968 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14969 it->glyph_row->used[TEXT_AREA] = 1;
14970 }
14971 }
14972 else
14973 {
14974 /* Save some values that must not be changed. */
14975 int saved_x = it->current_x;
14976 struct text_pos saved_pos;
14977 Lisp_Object saved_object;
14978 enum display_element_type saved_what = it->what;
14979 int saved_face_id = it->face_id;
14980
14981 saved_object = it->object;
14982 saved_pos = it->position;
14983
14984 it->what = IT_CHARACTER;
14985 bzero (&it->position, sizeof it->position);
14986 it->object = make_number (0);
14987 it->c = ' ';
14988 it->len = 1;
14989 it->face_id = face->id;
14990
14991 PRODUCE_GLYPHS (it);
14992
14993 while (it->current_x <= it->last_visible_x)
14994 PRODUCE_GLYPHS (it);
14995
14996 /* Don't count these blanks really. It would let us insert a left
14997 truncation glyph below and make us set the cursor on them, maybe. */
14998 it->current_x = saved_x;
14999 it->object = saved_object;
15000 it->position = saved_pos;
15001 it->what = saved_what;
15002 it->face_id = saved_face_id;
15003 }
15004 }
15005
15006
15007 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15008 trailing whitespace. */
15009
15010 static int
15011 trailing_whitespace_p (charpos)
15012 int charpos;
15013 {
15014 int bytepos = CHAR_TO_BYTE (charpos);
15015 int c = 0;
15016
15017 while (bytepos < ZV_BYTE
15018 && (c = FETCH_CHAR (bytepos),
15019 c == ' ' || c == '\t'))
15020 ++bytepos;
15021
15022 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15023 {
15024 if (bytepos != PT_BYTE)
15025 return 1;
15026 }
15027 return 0;
15028 }
15029
15030
15031 /* Highlight trailing whitespace, if any, in ROW. */
15032
15033 void
15034 highlight_trailing_whitespace (f, row)
15035 struct frame *f;
15036 struct glyph_row *row;
15037 {
15038 int used = row->used[TEXT_AREA];
15039
15040 if (used)
15041 {
15042 struct glyph *start = row->glyphs[TEXT_AREA];
15043 struct glyph *glyph = start + used - 1;
15044
15045 /* Skip over glyphs inserted to display the cursor at the
15046 end of a line, for extending the face of the last glyph
15047 to the end of the line on terminals, and for truncation
15048 and continuation glyphs. */
15049 while (glyph >= start
15050 && glyph->type == CHAR_GLYPH
15051 && INTEGERP (glyph->object))
15052 --glyph;
15053
15054 /* If last glyph is a space or stretch, and it's trailing
15055 whitespace, set the face of all trailing whitespace glyphs in
15056 IT->glyph_row to `trailing-whitespace'. */
15057 if (glyph >= start
15058 && BUFFERP (glyph->object)
15059 && (glyph->type == STRETCH_GLYPH
15060 || (glyph->type == CHAR_GLYPH
15061 && glyph->u.ch == ' '))
15062 && trailing_whitespace_p (glyph->charpos))
15063 {
15064 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
15065 if (face_id < 0)
15066 return;
15067
15068 while (glyph >= start
15069 && BUFFERP (glyph->object)
15070 && (glyph->type == STRETCH_GLYPH
15071 || (glyph->type == CHAR_GLYPH
15072 && glyph->u.ch == ' ')))
15073 (glyph--)->face_id = face_id;
15074 }
15075 }
15076 }
15077
15078
15079 /* Value is non-zero if glyph row ROW in window W should be
15080 used to hold the cursor. */
15081
15082 static int
15083 cursor_row_p (w, row)
15084 struct window *w;
15085 struct glyph_row *row;
15086 {
15087 int cursor_row_p = 1;
15088
15089 if (PT == MATRIX_ROW_END_CHARPOS (row))
15090 {
15091 /* If the row ends with a newline from a string, we don't want
15092 the cursor there (if the row is continued it doesn't end in a
15093 newline). */
15094 if (CHARPOS (row->end.string_pos) >= 0)
15095 cursor_row_p = row->continued_p;
15096 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15097 {
15098 /* If the row ends in middle of a real character,
15099 and the line is continued, we want the cursor here.
15100 That's because MATRIX_ROW_END_CHARPOS would equal
15101 PT if PT is before the character. */
15102 if (!row->ends_in_ellipsis_p)
15103 cursor_row_p = row->continued_p;
15104 else
15105 /* If the row ends in an ellipsis, then
15106 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15107 We want that position to be displayed after the ellipsis. */
15108 cursor_row_p = 0;
15109 }
15110 /* If the row ends at ZV, display the cursor at the end of that
15111 row instead of at the start of the row below. */
15112 else if (row->ends_at_zv_p)
15113 cursor_row_p = 1;
15114 else
15115 cursor_row_p = 0;
15116 }
15117
15118 return cursor_row_p;
15119 }
15120
15121
15122 /* Construct the glyph row IT->glyph_row in the desired matrix of
15123 IT->w from text at the current position of IT. See dispextern.h
15124 for an overview of struct it. Value is non-zero if
15125 IT->glyph_row displays text, as opposed to a line displaying ZV
15126 only. */
15127
15128 static int
15129 display_line (it)
15130 struct it *it;
15131 {
15132 struct glyph_row *row = it->glyph_row;
15133 Lisp_Object overlay_arrow_string;
15134
15135 /* We always start displaying at hpos zero even if hscrolled. */
15136 xassert (it->hpos == 0 && it->current_x == 0);
15137
15138 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15139 >= it->w->desired_matrix->nrows)
15140 {
15141 it->w->nrows_scale_factor++;
15142 fonts_changed_p = 1;
15143 return 0;
15144 }
15145
15146 /* Is IT->w showing the region? */
15147 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15148
15149 /* Clear the result glyph row and enable it. */
15150 prepare_desired_row (row);
15151
15152 row->y = it->current_y;
15153 row->start = it->start;
15154 row->continuation_lines_width = it->continuation_lines_width;
15155 row->displays_text_p = 1;
15156 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15157 it->starts_in_middle_of_char_p = 0;
15158
15159 /* Arrange the overlays nicely for our purposes. Usually, we call
15160 display_line on only one line at a time, in which case this
15161 can't really hurt too much, or we call it on lines which appear
15162 one after another in the buffer, in which case all calls to
15163 recenter_overlay_lists but the first will be pretty cheap. */
15164 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15165
15166 /* Move over display elements that are not visible because we are
15167 hscrolled. This may stop at an x-position < IT->first_visible_x
15168 if the first glyph is partially visible or if we hit a line end. */
15169 if (it->current_x < it->first_visible_x)
15170 {
15171 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15172 MOVE_TO_POS | MOVE_TO_X);
15173 }
15174
15175 /* Get the initial row height. This is either the height of the
15176 text hscrolled, if there is any, or zero. */
15177 row->ascent = it->max_ascent;
15178 row->height = it->max_ascent + it->max_descent;
15179 row->phys_ascent = it->max_phys_ascent;
15180 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15181 row->extra_line_spacing = it->max_extra_line_spacing;
15182
15183 /* Loop generating characters. The loop is left with IT on the next
15184 character to display. */
15185 while (1)
15186 {
15187 int n_glyphs_before, hpos_before, x_before;
15188 int x, i, nglyphs;
15189 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15190
15191 /* Retrieve the next thing to display. Value is zero if end of
15192 buffer reached. */
15193 if (!get_next_display_element (it))
15194 {
15195 /* Maybe add a space at the end of this line that is used to
15196 display the cursor there under X. Set the charpos of the
15197 first glyph of blank lines not corresponding to any text
15198 to -1. */
15199 #ifdef HAVE_WINDOW_SYSTEM
15200 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15201 row->exact_window_width_line_p = 1;
15202 else
15203 #endif /* HAVE_WINDOW_SYSTEM */
15204 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15205 || row->used[TEXT_AREA] == 0)
15206 {
15207 row->glyphs[TEXT_AREA]->charpos = -1;
15208 row->displays_text_p = 0;
15209
15210 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15211 && (!MINI_WINDOW_P (it->w)
15212 || (minibuf_level && EQ (it->window, minibuf_window))))
15213 row->indicate_empty_line_p = 1;
15214 }
15215
15216 it->continuation_lines_width = 0;
15217 row->ends_at_zv_p = 1;
15218 break;
15219 }
15220
15221 /* Now, get the metrics of what we want to display. This also
15222 generates glyphs in `row' (which is IT->glyph_row). */
15223 n_glyphs_before = row->used[TEXT_AREA];
15224 x = it->current_x;
15225
15226 /* Remember the line height so far in case the next element doesn't
15227 fit on the line. */
15228 if (!it->truncate_lines_p)
15229 {
15230 ascent = it->max_ascent;
15231 descent = it->max_descent;
15232 phys_ascent = it->max_phys_ascent;
15233 phys_descent = it->max_phys_descent;
15234 }
15235
15236 PRODUCE_GLYPHS (it);
15237
15238 /* If this display element was in marginal areas, continue with
15239 the next one. */
15240 if (it->area != TEXT_AREA)
15241 {
15242 row->ascent = max (row->ascent, it->max_ascent);
15243 row->height = max (row->height, it->max_ascent + it->max_descent);
15244 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15245 row->phys_height = max (row->phys_height,
15246 it->max_phys_ascent + it->max_phys_descent);
15247 row->extra_line_spacing = max (row->extra_line_spacing,
15248 it->max_extra_line_spacing);
15249 set_iterator_to_next (it, 1);
15250 continue;
15251 }
15252
15253 /* Does the display element fit on the line? If we truncate
15254 lines, we should draw past the right edge of the window. If
15255 we don't truncate, we want to stop so that we can display the
15256 continuation glyph before the right margin. If lines are
15257 continued, there are two possible strategies for characters
15258 resulting in more than 1 glyph (e.g. tabs): Display as many
15259 glyphs as possible in this line and leave the rest for the
15260 continuation line, or display the whole element in the next
15261 line. Original redisplay did the former, so we do it also. */
15262 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15263 hpos_before = it->hpos;
15264 x_before = x;
15265
15266 if (/* Not a newline. */
15267 nglyphs > 0
15268 /* Glyphs produced fit entirely in the line. */
15269 && it->current_x < it->last_visible_x)
15270 {
15271 it->hpos += nglyphs;
15272 row->ascent = max (row->ascent, it->max_ascent);
15273 row->height = max (row->height, it->max_ascent + it->max_descent);
15274 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15275 row->phys_height = max (row->phys_height,
15276 it->max_phys_ascent + it->max_phys_descent);
15277 row->extra_line_spacing = max (row->extra_line_spacing,
15278 it->max_extra_line_spacing);
15279 if (it->current_x - it->pixel_width < it->first_visible_x)
15280 row->x = x - it->first_visible_x;
15281 }
15282 else
15283 {
15284 int new_x;
15285 struct glyph *glyph;
15286
15287 for (i = 0; i < nglyphs; ++i, x = new_x)
15288 {
15289 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15290 new_x = x + glyph->pixel_width;
15291
15292 if (/* Lines are continued. */
15293 !it->truncate_lines_p
15294 && (/* Glyph doesn't fit on the line. */
15295 new_x > it->last_visible_x
15296 /* Or it fits exactly on a window system frame. */
15297 || (new_x == it->last_visible_x
15298 && FRAME_WINDOW_P (it->f))))
15299 {
15300 /* End of a continued line. */
15301
15302 if (it->hpos == 0
15303 || (new_x == it->last_visible_x
15304 && FRAME_WINDOW_P (it->f)))
15305 {
15306 /* Current glyph is the only one on the line or
15307 fits exactly on the line. We must continue
15308 the line because we can't draw the cursor
15309 after the glyph. */
15310 row->continued_p = 1;
15311 it->current_x = new_x;
15312 it->continuation_lines_width += new_x;
15313 ++it->hpos;
15314 if (i == nglyphs - 1)
15315 {
15316 set_iterator_to_next (it, 1);
15317 #ifdef HAVE_WINDOW_SYSTEM
15318 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15319 {
15320 if (!get_next_display_element (it))
15321 {
15322 row->exact_window_width_line_p = 1;
15323 it->continuation_lines_width = 0;
15324 row->continued_p = 0;
15325 row->ends_at_zv_p = 1;
15326 }
15327 else if (ITERATOR_AT_END_OF_LINE_P (it))
15328 {
15329 row->continued_p = 0;
15330 row->exact_window_width_line_p = 1;
15331 }
15332 }
15333 #endif /* HAVE_WINDOW_SYSTEM */
15334 }
15335 }
15336 else if (CHAR_GLYPH_PADDING_P (*glyph)
15337 && !FRAME_WINDOW_P (it->f))
15338 {
15339 /* A padding glyph that doesn't fit on this line.
15340 This means the whole character doesn't fit
15341 on the line. */
15342 row->used[TEXT_AREA] = n_glyphs_before;
15343
15344 /* Fill the rest of the row with continuation
15345 glyphs like in 20.x. */
15346 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15347 < row->glyphs[1 + TEXT_AREA])
15348 produce_special_glyphs (it, IT_CONTINUATION);
15349
15350 row->continued_p = 1;
15351 it->current_x = x_before;
15352 it->continuation_lines_width += x_before;
15353
15354 /* Restore the height to what it was before the
15355 element not fitting on the line. */
15356 it->max_ascent = ascent;
15357 it->max_descent = descent;
15358 it->max_phys_ascent = phys_ascent;
15359 it->max_phys_descent = phys_descent;
15360 }
15361 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15362 {
15363 /* A TAB that extends past the right edge of the
15364 window. This produces a single glyph on
15365 window system frames. We leave the glyph in
15366 this row and let it fill the row, but don't
15367 consume the TAB. */
15368 it->continuation_lines_width += it->last_visible_x;
15369 row->ends_in_middle_of_char_p = 1;
15370 row->continued_p = 1;
15371 glyph->pixel_width = it->last_visible_x - x;
15372 it->starts_in_middle_of_char_p = 1;
15373 }
15374 else
15375 {
15376 /* Something other than a TAB that draws past
15377 the right edge of the window. Restore
15378 positions to values before the element. */
15379 row->used[TEXT_AREA] = n_glyphs_before + i;
15380
15381 /* Display continuation glyphs. */
15382 if (!FRAME_WINDOW_P (it->f))
15383 produce_special_glyphs (it, IT_CONTINUATION);
15384 row->continued_p = 1;
15385
15386 it->continuation_lines_width += x;
15387
15388 if (nglyphs > 1 && i > 0)
15389 {
15390 row->ends_in_middle_of_char_p = 1;
15391 it->starts_in_middle_of_char_p = 1;
15392 }
15393
15394 /* Restore the height to what it was before the
15395 element not fitting on the line. */
15396 it->max_ascent = ascent;
15397 it->max_descent = descent;
15398 it->max_phys_ascent = phys_ascent;
15399 it->max_phys_descent = phys_descent;
15400 }
15401
15402 break;
15403 }
15404 else if (new_x > it->first_visible_x)
15405 {
15406 /* Increment number of glyphs actually displayed. */
15407 ++it->hpos;
15408
15409 if (x < it->first_visible_x)
15410 /* Glyph is partially visible, i.e. row starts at
15411 negative X position. */
15412 row->x = x - it->first_visible_x;
15413 }
15414 else
15415 {
15416 /* Glyph is completely off the left margin of the
15417 window. This should not happen because of the
15418 move_it_in_display_line at the start of this
15419 function, unless the text display area of the
15420 window is empty. */
15421 xassert (it->first_visible_x <= it->last_visible_x);
15422 }
15423 }
15424
15425 row->ascent = max (row->ascent, it->max_ascent);
15426 row->height = max (row->height, it->max_ascent + it->max_descent);
15427 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15428 row->phys_height = max (row->phys_height,
15429 it->max_phys_ascent + it->max_phys_descent);
15430 row->extra_line_spacing = max (row->extra_line_spacing,
15431 it->max_extra_line_spacing);
15432
15433 /* End of this display line if row is continued. */
15434 if (row->continued_p || row->ends_at_zv_p)
15435 break;
15436 }
15437
15438 at_end_of_line:
15439 /* Is this a line end? If yes, we're also done, after making
15440 sure that a non-default face is extended up to the right
15441 margin of the window. */
15442 if (ITERATOR_AT_END_OF_LINE_P (it))
15443 {
15444 int used_before = row->used[TEXT_AREA];
15445
15446 row->ends_in_newline_from_string_p = STRINGP (it->object);
15447
15448 #ifdef HAVE_WINDOW_SYSTEM
15449 /* Add a space at the end of the line that is used to
15450 display the cursor there. */
15451 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15452 append_space_for_newline (it, 0);
15453 #endif /* HAVE_WINDOW_SYSTEM */
15454
15455 /* Extend the face to the end of the line. */
15456 extend_face_to_end_of_line (it);
15457
15458 /* Make sure we have the position. */
15459 if (used_before == 0)
15460 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15461
15462 /* Consume the line end. This skips over invisible lines. */
15463 set_iterator_to_next (it, 1);
15464 it->continuation_lines_width = 0;
15465 break;
15466 }
15467
15468 /* Proceed with next display element. Note that this skips
15469 over lines invisible because of selective display. */
15470 set_iterator_to_next (it, 1);
15471
15472 /* If we truncate lines, we are done when the last displayed
15473 glyphs reach past the right margin of the window. */
15474 if (it->truncate_lines_p
15475 && (FRAME_WINDOW_P (it->f)
15476 ? (it->current_x >= it->last_visible_x)
15477 : (it->current_x > it->last_visible_x)))
15478 {
15479 /* Maybe add truncation glyphs. */
15480 if (!FRAME_WINDOW_P (it->f))
15481 {
15482 int i, n;
15483
15484 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15485 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15486 break;
15487
15488 for (n = row->used[TEXT_AREA]; i < n; ++i)
15489 {
15490 row->used[TEXT_AREA] = i;
15491 produce_special_glyphs (it, IT_TRUNCATION);
15492 }
15493 }
15494 #ifdef HAVE_WINDOW_SYSTEM
15495 else
15496 {
15497 /* Don't truncate if we can overflow newline into fringe. */
15498 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15499 {
15500 if (!get_next_display_element (it))
15501 {
15502 it->continuation_lines_width = 0;
15503 row->ends_at_zv_p = 1;
15504 row->exact_window_width_line_p = 1;
15505 break;
15506 }
15507 if (ITERATOR_AT_END_OF_LINE_P (it))
15508 {
15509 row->exact_window_width_line_p = 1;
15510 goto at_end_of_line;
15511 }
15512 }
15513 }
15514 #endif /* HAVE_WINDOW_SYSTEM */
15515
15516 row->truncated_on_right_p = 1;
15517 it->continuation_lines_width = 0;
15518 reseat_at_next_visible_line_start (it, 0);
15519 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15520 it->hpos = hpos_before;
15521 it->current_x = x_before;
15522 break;
15523 }
15524 }
15525
15526 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15527 at the left window margin. */
15528 if (it->first_visible_x
15529 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15530 {
15531 if (!FRAME_WINDOW_P (it->f))
15532 insert_left_trunc_glyphs (it);
15533 row->truncated_on_left_p = 1;
15534 }
15535
15536 /* If the start of this line is the overlay arrow-position, then
15537 mark this glyph row as the one containing the overlay arrow.
15538 This is clearly a mess with variable size fonts. It would be
15539 better to let it be displayed like cursors under X. */
15540 if ((row->displays_text_p || !overlay_arrow_seen)
15541 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15542 !NILP (overlay_arrow_string)))
15543 {
15544 /* Overlay arrow in window redisplay is a fringe bitmap. */
15545 if (STRINGP (overlay_arrow_string))
15546 {
15547 struct glyph_row *arrow_row
15548 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15549 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15550 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15551 struct glyph *p = row->glyphs[TEXT_AREA];
15552 struct glyph *p2, *end;
15553
15554 /* Copy the arrow glyphs. */
15555 while (glyph < arrow_end)
15556 *p++ = *glyph++;
15557
15558 /* Throw away padding glyphs. */
15559 p2 = p;
15560 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15561 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15562 ++p2;
15563 if (p2 > p)
15564 {
15565 while (p2 < end)
15566 *p++ = *p2++;
15567 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15568 }
15569 }
15570 else
15571 {
15572 xassert (INTEGERP (overlay_arrow_string));
15573 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15574 }
15575 overlay_arrow_seen = 1;
15576 }
15577
15578 /* Compute pixel dimensions of this line. */
15579 compute_line_metrics (it);
15580
15581 /* Remember the position at which this line ends. */
15582 row->end = it->current;
15583
15584 /* Record whether this row ends inside an ellipsis. */
15585 row->ends_in_ellipsis_p
15586 = (it->method == GET_FROM_DISPLAY_VECTOR
15587 && it->ellipsis_p);
15588
15589 /* Save fringe bitmaps in this row. */
15590 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15591 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15592 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15593 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15594
15595 it->left_user_fringe_bitmap = 0;
15596 it->left_user_fringe_face_id = 0;
15597 it->right_user_fringe_bitmap = 0;
15598 it->right_user_fringe_face_id = 0;
15599
15600 /* Maybe set the cursor. */
15601 if (it->w->cursor.vpos < 0
15602 && PT >= MATRIX_ROW_START_CHARPOS (row)
15603 && PT <= MATRIX_ROW_END_CHARPOS (row)
15604 && cursor_row_p (it->w, row))
15605 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15606
15607 /* Highlight trailing whitespace. */
15608 if (!NILP (Vshow_trailing_whitespace))
15609 highlight_trailing_whitespace (it->f, it->glyph_row);
15610
15611 /* Prepare for the next line. This line starts horizontally at (X
15612 HPOS) = (0 0). Vertical positions are incremented. As a
15613 convenience for the caller, IT->glyph_row is set to the next
15614 row to be used. */
15615 it->current_x = it->hpos = 0;
15616 it->current_y += row->height;
15617 ++it->vpos;
15618 ++it->glyph_row;
15619 it->start = it->current;
15620 return row->displays_text_p;
15621 }
15622
15623
15624 \f
15625 /***********************************************************************
15626 Menu Bar
15627 ***********************************************************************/
15628
15629 /* Redisplay the menu bar in the frame for window W.
15630
15631 The menu bar of X frames that don't have X toolkit support is
15632 displayed in a special window W->frame->menu_bar_window.
15633
15634 The menu bar of terminal frames is treated specially as far as
15635 glyph matrices are concerned. Menu bar lines are not part of
15636 windows, so the update is done directly on the frame matrix rows
15637 for the menu bar. */
15638
15639 static void
15640 display_menu_bar (w)
15641 struct window *w;
15642 {
15643 struct frame *f = XFRAME (WINDOW_FRAME (w));
15644 struct it it;
15645 Lisp_Object items;
15646 int i;
15647
15648 /* Don't do all this for graphical frames. */
15649 #ifdef HAVE_NTGUI
15650 if (!NILP (Vwindow_system))
15651 return;
15652 #endif
15653 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15654 if (FRAME_X_P (f))
15655 return;
15656 #endif
15657 #ifdef MAC_OS
15658 if (FRAME_MAC_P (f))
15659 return;
15660 #endif
15661
15662 #ifdef USE_X_TOOLKIT
15663 xassert (!FRAME_WINDOW_P (f));
15664 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15665 it.first_visible_x = 0;
15666 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15667 #else /* not USE_X_TOOLKIT */
15668 if (FRAME_WINDOW_P (f))
15669 {
15670 /* Menu bar lines are displayed in the desired matrix of the
15671 dummy window menu_bar_window. */
15672 struct window *menu_w;
15673 xassert (WINDOWP (f->menu_bar_window));
15674 menu_w = XWINDOW (f->menu_bar_window);
15675 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15676 MENU_FACE_ID);
15677 it.first_visible_x = 0;
15678 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15679 }
15680 else
15681 {
15682 /* This is a TTY frame, i.e. character hpos/vpos are used as
15683 pixel x/y. */
15684 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15685 MENU_FACE_ID);
15686 it.first_visible_x = 0;
15687 it.last_visible_x = FRAME_COLS (f);
15688 }
15689 #endif /* not USE_X_TOOLKIT */
15690
15691 if (! mode_line_inverse_video)
15692 /* Force the menu-bar to be displayed in the default face. */
15693 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15694
15695 /* Clear all rows of the menu bar. */
15696 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15697 {
15698 struct glyph_row *row = it.glyph_row + i;
15699 clear_glyph_row (row);
15700 row->enabled_p = 1;
15701 row->full_width_p = 1;
15702 }
15703
15704 /* Display all items of the menu bar. */
15705 items = FRAME_MENU_BAR_ITEMS (it.f);
15706 for (i = 0; i < XVECTOR (items)->size; i += 4)
15707 {
15708 Lisp_Object string;
15709
15710 /* Stop at nil string. */
15711 string = AREF (items, i + 1);
15712 if (NILP (string))
15713 break;
15714
15715 /* Remember where item was displayed. */
15716 AREF (items, i + 3) = make_number (it.hpos);
15717
15718 /* Display the item, pad with one space. */
15719 if (it.current_x < it.last_visible_x)
15720 display_string (NULL, string, Qnil, 0, 0, &it,
15721 SCHARS (string) + 1, 0, 0, -1);
15722 }
15723
15724 /* Fill out the line with spaces. */
15725 if (it.current_x < it.last_visible_x)
15726 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15727
15728 /* Compute the total height of the lines. */
15729 compute_line_metrics (&it);
15730 }
15731
15732
15733 \f
15734 /***********************************************************************
15735 Mode Line
15736 ***********************************************************************/
15737
15738 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15739 FORCE is non-zero, redisplay mode lines unconditionally.
15740 Otherwise, redisplay only mode lines that are garbaged. Value is
15741 the number of windows whose mode lines were redisplayed. */
15742
15743 static int
15744 redisplay_mode_lines (window, force)
15745 Lisp_Object window;
15746 int force;
15747 {
15748 int nwindows = 0;
15749
15750 while (!NILP (window))
15751 {
15752 struct window *w = XWINDOW (window);
15753
15754 if (WINDOWP (w->hchild))
15755 nwindows += redisplay_mode_lines (w->hchild, force);
15756 else if (WINDOWP (w->vchild))
15757 nwindows += redisplay_mode_lines (w->vchild, force);
15758 else if (force
15759 || FRAME_GARBAGED_P (XFRAME (w->frame))
15760 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15761 {
15762 struct text_pos lpoint;
15763 struct buffer *old = current_buffer;
15764
15765 /* Set the window's buffer for the mode line display. */
15766 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15767 set_buffer_internal_1 (XBUFFER (w->buffer));
15768
15769 /* Point refers normally to the selected window. For any
15770 other window, set up appropriate value. */
15771 if (!EQ (window, selected_window))
15772 {
15773 struct text_pos pt;
15774
15775 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15776 if (CHARPOS (pt) < BEGV)
15777 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15778 else if (CHARPOS (pt) > (ZV - 1))
15779 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15780 else
15781 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15782 }
15783
15784 /* Display mode lines. */
15785 clear_glyph_matrix (w->desired_matrix);
15786 if (display_mode_lines (w))
15787 {
15788 ++nwindows;
15789 w->must_be_updated_p = 1;
15790 }
15791
15792 /* Restore old settings. */
15793 set_buffer_internal_1 (old);
15794 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15795 }
15796
15797 window = w->next;
15798 }
15799
15800 return nwindows;
15801 }
15802
15803
15804 /* Display the mode and/or top line of window W. Value is the number
15805 of mode lines displayed. */
15806
15807 static int
15808 display_mode_lines (w)
15809 struct window *w;
15810 {
15811 Lisp_Object old_selected_window, old_selected_frame;
15812 int n = 0;
15813
15814 old_selected_frame = selected_frame;
15815 selected_frame = w->frame;
15816 old_selected_window = selected_window;
15817 XSETWINDOW (selected_window, w);
15818
15819 /* These will be set while the mode line specs are processed. */
15820 line_number_displayed = 0;
15821 w->column_number_displayed = Qnil;
15822
15823 if (WINDOW_WANTS_MODELINE_P (w))
15824 {
15825 struct window *sel_w = XWINDOW (old_selected_window);
15826
15827 /* Select mode line face based on the real selected window. */
15828 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15829 current_buffer->mode_line_format);
15830 ++n;
15831 }
15832
15833 if (WINDOW_WANTS_HEADER_LINE_P (w))
15834 {
15835 display_mode_line (w, HEADER_LINE_FACE_ID,
15836 current_buffer->header_line_format);
15837 ++n;
15838 }
15839
15840 selected_frame = old_selected_frame;
15841 selected_window = old_selected_window;
15842 return n;
15843 }
15844
15845
15846 /* Display mode or top line of window W. FACE_ID specifies which line
15847 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15848 FORMAT is the mode line format to display. Value is the pixel
15849 height of the mode line displayed. */
15850
15851 static int
15852 display_mode_line (w, face_id, format)
15853 struct window *w;
15854 enum face_id face_id;
15855 Lisp_Object format;
15856 {
15857 struct it it;
15858 struct face *face;
15859 int count = SPECPDL_INDEX ();
15860
15861 init_iterator (&it, w, -1, -1, NULL, face_id);
15862 prepare_desired_row (it.glyph_row);
15863
15864 it.glyph_row->mode_line_p = 1;
15865
15866 if (! mode_line_inverse_video)
15867 /* Force the mode-line to be displayed in the default face. */
15868 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15869
15870 record_unwind_protect (unwind_format_mode_line,
15871 format_mode_line_unwind_data (NULL));
15872
15873 mode_line_target = MODE_LINE_DISPLAY;
15874
15875 /* Temporarily make frame's keyboard the current kboard so that
15876 kboard-local variables in the mode_line_format will get the right
15877 values. */
15878 push_frame_kboard (it.f);
15879 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15880 pop_frame_kboard ();
15881
15882 unbind_to (count, Qnil);
15883
15884 /* Fill up with spaces. */
15885 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15886
15887 compute_line_metrics (&it);
15888 it.glyph_row->full_width_p = 1;
15889 it.glyph_row->continued_p = 0;
15890 it.glyph_row->truncated_on_left_p = 0;
15891 it.glyph_row->truncated_on_right_p = 0;
15892
15893 /* Make a 3D mode-line have a shadow at its right end. */
15894 face = FACE_FROM_ID (it.f, face_id);
15895 extend_face_to_end_of_line (&it);
15896 if (face->box != FACE_NO_BOX)
15897 {
15898 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15899 + it.glyph_row->used[TEXT_AREA] - 1);
15900 last->right_box_line_p = 1;
15901 }
15902
15903 return it.glyph_row->height;
15904 }
15905
15906 /* Contribute ELT to the mode line for window IT->w. How it
15907 translates into text depends on its data type.
15908
15909 IT describes the display environment in which we display, as usual.
15910
15911 DEPTH is the depth in recursion. It is used to prevent
15912 infinite recursion here.
15913
15914 FIELD_WIDTH is the number of characters the display of ELT should
15915 occupy in the mode line, and PRECISION is the maximum number of
15916 characters to display from ELT's representation. See
15917 display_string for details.
15918
15919 Returns the hpos of the end of the text generated by ELT.
15920
15921 PROPS is a property list to add to any string we encounter.
15922
15923 If RISKY is nonzero, remove (disregard) any properties in any string
15924 we encounter, and ignore :eval and :propertize.
15925
15926 The global variable `mode_line_target' determines whether the
15927 output is passed to `store_mode_line_noprop',
15928 `store_mode_line_string', or `display_string'. */
15929
15930 static int
15931 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15932 struct it *it;
15933 int depth;
15934 int field_width, precision;
15935 Lisp_Object elt, props;
15936 int risky;
15937 {
15938 int n = 0, field, prec;
15939 int literal = 0;
15940
15941 tail_recurse:
15942 if (depth > 100)
15943 elt = build_string ("*too-deep*");
15944
15945 depth++;
15946
15947 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15948 {
15949 case Lisp_String:
15950 {
15951 /* A string: output it and check for %-constructs within it. */
15952 unsigned char c;
15953 const unsigned char *this, *lisp_string;
15954
15955 if (!NILP (props) || risky)
15956 {
15957 Lisp_Object oprops, aelt;
15958 oprops = Ftext_properties_at (make_number (0), elt);
15959
15960 /* If the starting string's properties are not what
15961 we want, translate the string. Also, if the string
15962 is risky, do that anyway. */
15963
15964 if (NILP (Fequal (props, oprops)) || risky)
15965 {
15966 /* If the starting string has properties,
15967 merge the specified ones onto the existing ones. */
15968 if (! NILP (oprops) && !risky)
15969 {
15970 Lisp_Object tem;
15971
15972 oprops = Fcopy_sequence (oprops);
15973 tem = props;
15974 while (CONSP (tem))
15975 {
15976 oprops = Fplist_put (oprops, XCAR (tem),
15977 XCAR (XCDR (tem)));
15978 tem = XCDR (XCDR (tem));
15979 }
15980 props = oprops;
15981 }
15982
15983 aelt = Fassoc (elt, mode_line_proptrans_alist);
15984 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15985 {
15986 mode_line_proptrans_alist
15987 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15988 elt = XCAR (aelt);
15989 }
15990 else
15991 {
15992 Lisp_Object tem;
15993
15994 elt = Fcopy_sequence (elt);
15995 Fset_text_properties (make_number (0), Flength (elt),
15996 props, elt);
15997 /* Add this item to mode_line_proptrans_alist. */
15998 mode_line_proptrans_alist
15999 = Fcons (Fcons (elt, props),
16000 mode_line_proptrans_alist);
16001 /* Truncate mode_line_proptrans_alist
16002 to at most 50 elements. */
16003 tem = Fnthcdr (make_number (50),
16004 mode_line_proptrans_alist);
16005 if (! NILP (tem))
16006 XSETCDR (tem, Qnil);
16007 }
16008 }
16009 }
16010
16011 this = SDATA (elt);
16012 lisp_string = this;
16013
16014 if (literal)
16015 {
16016 prec = precision - n;
16017 switch (mode_line_target)
16018 {
16019 case MODE_LINE_NOPROP:
16020 case MODE_LINE_TITLE:
16021 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16022 break;
16023 case MODE_LINE_STRING:
16024 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16025 break;
16026 case MODE_LINE_DISPLAY:
16027 n += display_string (NULL, elt, Qnil, 0, 0, it,
16028 0, prec, 0, STRING_MULTIBYTE (elt));
16029 break;
16030 }
16031
16032 break;
16033 }
16034
16035 while ((precision <= 0 || n < precision)
16036 && *this
16037 && (mode_line_target != MODE_LINE_DISPLAY
16038 || it->current_x < it->last_visible_x))
16039 {
16040 const unsigned char *last = this;
16041
16042 /* Advance to end of string or next format specifier. */
16043 while ((c = *this++) != '\0' && c != '%')
16044 ;
16045
16046 if (this - 1 != last)
16047 {
16048 int nchars, nbytes;
16049
16050 /* Output to end of string or up to '%'. Field width
16051 is length of string. Don't output more than
16052 PRECISION allows us. */
16053 --this;
16054
16055 prec = c_string_width (last, this - last, precision - n,
16056 &nchars, &nbytes);
16057
16058 switch (mode_line_target)
16059 {
16060 case MODE_LINE_NOPROP:
16061 case MODE_LINE_TITLE:
16062 n += store_mode_line_noprop (last, 0, prec);
16063 break;
16064 case MODE_LINE_STRING:
16065 {
16066 int bytepos = last - lisp_string;
16067 int charpos = string_byte_to_char (elt, bytepos);
16068 int endpos = (precision <= 0
16069 ? string_byte_to_char (elt,
16070 this - lisp_string)
16071 : charpos + nchars);
16072
16073 n += store_mode_line_string (NULL,
16074 Fsubstring (elt, make_number (charpos),
16075 make_number (endpos)),
16076 0, 0, 0, Qnil);
16077 }
16078 break;
16079 case MODE_LINE_DISPLAY:
16080 {
16081 int bytepos = last - lisp_string;
16082 int charpos = string_byte_to_char (elt, bytepos);
16083 n += display_string (NULL, elt, Qnil, 0, charpos,
16084 it, 0, prec, 0,
16085 STRING_MULTIBYTE (elt));
16086 }
16087 break;
16088 }
16089 }
16090 else /* c == '%' */
16091 {
16092 const unsigned char *percent_position = this;
16093
16094 /* Get the specified minimum width. Zero means
16095 don't pad. */
16096 field = 0;
16097 while ((c = *this++) >= '0' && c <= '9')
16098 field = field * 10 + c - '0';
16099
16100 /* Don't pad beyond the total padding allowed. */
16101 if (field_width - n > 0 && field > field_width - n)
16102 field = field_width - n;
16103
16104 /* Note that either PRECISION <= 0 or N < PRECISION. */
16105 prec = precision - n;
16106
16107 if (c == 'M')
16108 n += display_mode_element (it, depth, field, prec,
16109 Vglobal_mode_string, props,
16110 risky);
16111 else if (c != 0)
16112 {
16113 int multibyte;
16114 int bytepos, charpos;
16115 unsigned char *spec;
16116
16117 bytepos = percent_position - lisp_string;
16118 charpos = (STRING_MULTIBYTE (elt)
16119 ? string_byte_to_char (elt, bytepos)
16120 : bytepos);
16121
16122 spec
16123 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16124
16125 switch (mode_line_target)
16126 {
16127 case MODE_LINE_NOPROP:
16128 case MODE_LINE_TITLE:
16129 n += store_mode_line_noprop (spec, field, prec);
16130 break;
16131 case MODE_LINE_STRING:
16132 {
16133 int len = strlen (spec);
16134 Lisp_Object tem = make_string (spec, len);
16135 props = Ftext_properties_at (make_number (charpos), elt);
16136 /* Should only keep face property in props */
16137 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16138 }
16139 break;
16140 case MODE_LINE_DISPLAY:
16141 {
16142 int nglyphs_before, nwritten;
16143
16144 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16145 nwritten = display_string (spec, Qnil, elt,
16146 charpos, 0, it,
16147 field, prec, 0,
16148 multibyte);
16149
16150 /* Assign to the glyphs written above the
16151 string where the `%x' came from, position
16152 of the `%'. */
16153 if (nwritten > 0)
16154 {
16155 struct glyph *glyph
16156 = (it->glyph_row->glyphs[TEXT_AREA]
16157 + nglyphs_before);
16158 int i;
16159
16160 for (i = 0; i < nwritten; ++i)
16161 {
16162 glyph[i].object = elt;
16163 glyph[i].charpos = charpos;
16164 }
16165
16166 n += nwritten;
16167 }
16168 }
16169 break;
16170 }
16171 }
16172 else /* c == 0 */
16173 break;
16174 }
16175 }
16176 }
16177 break;
16178
16179 case Lisp_Symbol:
16180 /* A symbol: process the value of the symbol recursively
16181 as if it appeared here directly. Avoid error if symbol void.
16182 Special case: if value of symbol is a string, output the string
16183 literally. */
16184 {
16185 register Lisp_Object tem;
16186
16187 /* If the variable is not marked as risky to set
16188 then its contents are risky to use. */
16189 if (NILP (Fget (elt, Qrisky_local_variable)))
16190 risky = 1;
16191
16192 tem = Fboundp (elt);
16193 if (!NILP (tem))
16194 {
16195 tem = Fsymbol_value (elt);
16196 /* If value is a string, output that string literally:
16197 don't check for % within it. */
16198 if (STRINGP (tem))
16199 literal = 1;
16200
16201 if (!EQ (tem, elt))
16202 {
16203 /* Give up right away for nil or t. */
16204 elt = tem;
16205 goto tail_recurse;
16206 }
16207 }
16208 }
16209 break;
16210
16211 case Lisp_Cons:
16212 {
16213 register Lisp_Object car, tem;
16214
16215 /* A cons cell: five distinct cases.
16216 If first element is :eval or :propertize, do something special.
16217 If first element is a string or a cons, process all the elements
16218 and effectively concatenate them.
16219 If first element is a negative number, truncate displaying cdr to
16220 at most that many characters. If positive, pad (with spaces)
16221 to at least that many characters.
16222 If first element is a symbol, process the cadr or caddr recursively
16223 according to whether the symbol's value is non-nil or nil. */
16224 car = XCAR (elt);
16225 if (EQ (car, QCeval))
16226 {
16227 /* An element of the form (:eval FORM) means evaluate FORM
16228 and use the result as mode line elements. */
16229
16230 if (risky)
16231 break;
16232
16233 if (CONSP (XCDR (elt)))
16234 {
16235 Lisp_Object spec;
16236 spec = safe_eval (XCAR (XCDR (elt)));
16237 n += display_mode_element (it, depth, field_width - n,
16238 precision - n, spec, props,
16239 risky);
16240 }
16241 }
16242 else if (EQ (car, QCpropertize))
16243 {
16244 /* An element of the form (:propertize ELT PROPS...)
16245 means display ELT but applying properties PROPS. */
16246
16247 if (risky)
16248 break;
16249
16250 if (CONSP (XCDR (elt)))
16251 n += display_mode_element (it, depth, field_width - n,
16252 precision - n, XCAR (XCDR (elt)),
16253 XCDR (XCDR (elt)), risky);
16254 }
16255 else if (SYMBOLP (car))
16256 {
16257 tem = Fboundp (car);
16258 elt = XCDR (elt);
16259 if (!CONSP (elt))
16260 goto invalid;
16261 /* elt is now the cdr, and we know it is a cons cell.
16262 Use its car if CAR has a non-nil value. */
16263 if (!NILP (tem))
16264 {
16265 tem = Fsymbol_value (car);
16266 if (!NILP (tem))
16267 {
16268 elt = XCAR (elt);
16269 goto tail_recurse;
16270 }
16271 }
16272 /* Symbol's value is nil (or symbol is unbound)
16273 Get the cddr of the original list
16274 and if possible find the caddr and use that. */
16275 elt = XCDR (elt);
16276 if (NILP (elt))
16277 break;
16278 else if (!CONSP (elt))
16279 goto invalid;
16280 elt = XCAR (elt);
16281 goto tail_recurse;
16282 }
16283 else if (INTEGERP (car))
16284 {
16285 register int lim = XINT (car);
16286 elt = XCDR (elt);
16287 if (lim < 0)
16288 {
16289 /* Negative int means reduce maximum width. */
16290 if (precision <= 0)
16291 precision = -lim;
16292 else
16293 precision = min (precision, -lim);
16294 }
16295 else if (lim > 0)
16296 {
16297 /* Padding specified. Don't let it be more than
16298 current maximum. */
16299 if (precision > 0)
16300 lim = min (precision, lim);
16301
16302 /* If that's more padding than already wanted, queue it.
16303 But don't reduce padding already specified even if
16304 that is beyond the current truncation point. */
16305 field_width = max (lim, field_width);
16306 }
16307 goto tail_recurse;
16308 }
16309 else if (STRINGP (car) || CONSP (car))
16310 {
16311 register int limit = 50;
16312 /* Limit is to protect against circular lists. */
16313 while (CONSP (elt)
16314 && --limit > 0
16315 && (precision <= 0 || n < precision))
16316 {
16317 n += display_mode_element (it, depth,
16318 /* Do padding only after the last
16319 element in the list. */
16320 (! CONSP (XCDR (elt))
16321 ? field_width - n
16322 : 0),
16323 precision - n, XCAR (elt),
16324 props, risky);
16325 elt = XCDR (elt);
16326 }
16327 }
16328 }
16329 break;
16330
16331 default:
16332 invalid:
16333 elt = build_string ("*invalid*");
16334 goto tail_recurse;
16335 }
16336
16337 /* Pad to FIELD_WIDTH. */
16338 if (field_width > 0 && n < field_width)
16339 {
16340 switch (mode_line_target)
16341 {
16342 case MODE_LINE_NOPROP:
16343 case MODE_LINE_TITLE:
16344 n += store_mode_line_noprop ("", field_width - n, 0);
16345 break;
16346 case MODE_LINE_STRING:
16347 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16348 break;
16349 case MODE_LINE_DISPLAY:
16350 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16351 0, 0, 0);
16352 break;
16353 }
16354 }
16355
16356 return n;
16357 }
16358
16359 /* Store a mode-line string element in mode_line_string_list.
16360
16361 If STRING is non-null, display that C string. Otherwise, the Lisp
16362 string LISP_STRING is displayed.
16363
16364 FIELD_WIDTH is the minimum number of output glyphs to produce.
16365 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16366 with spaces. FIELD_WIDTH <= 0 means don't pad.
16367
16368 PRECISION is the maximum number of characters to output from
16369 STRING. PRECISION <= 0 means don't truncate the string.
16370
16371 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16372 properties to the string.
16373
16374 PROPS are the properties to add to the string.
16375 The mode_line_string_face face property is always added to the string.
16376 */
16377
16378 static int
16379 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16380 char *string;
16381 Lisp_Object lisp_string;
16382 int copy_string;
16383 int field_width;
16384 int precision;
16385 Lisp_Object props;
16386 {
16387 int len;
16388 int n = 0;
16389
16390 if (string != NULL)
16391 {
16392 len = strlen (string);
16393 if (precision > 0 && len > precision)
16394 len = precision;
16395 lisp_string = make_string (string, len);
16396 if (NILP (props))
16397 props = mode_line_string_face_prop;
16398 else if (!NILP (mode_line_string_face))
16399 {
16400 Lisp_Object face = Fplist_get (props, Qface);
16401 props = Fcopy_sequence (props);
16402 if (NILP (face))
16403 face = mode_line_string_face;
16404 else
16405 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16406 props = Fplist_put (props, Qface, face);
16407 }
16408 Fadd_text_properties (make_number (0), make_number (len),
16409 props, lisp_string);
16410 }
16411 else
16412 {
16413 len = XFASTINT (Flength (lisp_string));
16414 if (precision > 0 && len > precision)
16415 {
16416 len = precision;
16417 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16418 precision = -1;
16419 }
16420 if (!NILP (mode_line_string_face))
16421 {
16422 Lisp_Object face;
16423 if (NILP (props))
16424 props = Ftext_properties_at (make_number (0), lisp_string);
16425 face = Fplist_get (props, Qface);
16426 if (NILP (face))
16427 face = mode_line_string_face;
16428 else
16429 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16430 props = Fcons (Qface, Fcons (face, Qnil));
16431 if (copy_string)
16432 lisp_string = Fcopy_sequence (lisp_string);
16433 }
16434 if (!NILP (props))
16435 Fadd_text_properties (make_number (0), make_number (len),
16436 props, lisp_string);
16437 }
16438
16439 if (len > 0)
16440 {
16441 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16442 n += len;
16443 }
16444
16445 if (field_width > len)
16446 {
16447 field_width -= len;
16448 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16449 if (!NILP (props))
16450 Fadd_text_properties (make_number (0), make_number (field_width),
16451 props, lisp_string);
16452 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16453 n += field_width;
16454 }
16455
16456 return n;
16457 }
16458
16459
16460 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16461 1, 4, 0,
16462 doc: /* Format a string out of a mode line format specification.
16463 First arg FORMAT specifies the mode line format (see `mode-line-format'
16464 for details) to use.
16465
16466 Optional second arg FACE specifies the face property to put
16467 on all characters for which no face is specified.
16468 t means whatever face the window's mode line currently uses
16469 \(either `mode-line' or `mode-line-inactive', depending).
16470 nil means the default is no face property.
16471 If FACE is an integer, the value string has no text properties.
16472
16473 Optional third and fourth args WINDOW and BUFFER specify the window
16474 and buffer to use as the context for the formatting (defaults
16475 are the selected window and the window's buffer). */)
16476 (format, face, window, buffer)
16477 Lisp_Object format, face, window, buffer;
16478 {
16479 struct it it;
16480 int len;
16481 struct window *w;
16482 struct buffer *old_buffer = NULL;
16483 int face_id = -1;
16484 int no_props = INTEGERP (face);
16485 int count = SPECPDL_INDEX ();
16486 Lisp_Object str;
16487 int string_start = 0;
16488
16489 if (NILP (window))
16490 window = selected_window;
16491 CHECK_WINDOW (window);
16492 w = XWINDOW (window);
16493
16494 if (NILP (buffer))
16495 buffer = w->buffer;
16496 CHECK_BUFFER (buffer);
16497
16498 if (NILP (format))
16499 return build_string ("");
16500
16501 if (no_props)
16502 face = Qnil;
16503
16504 if (!NILP (face))
16505 {
16506 if (EQ (face, Qt))
16507 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16508 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
16509 }
16510
16511 if (face_id < 0)
16512 face_id = DEFAULT_FACE_ID;
16513
16514 if (XBUFFER (buffer) != current_buffer)
16515 old_buffer = current_buffer;
16516
16517 record_unwind_protect (unwind_format_mode_line,
16518 format_mode_line_unwind_data (old_buffer));
16519
16520 if (old_buffer)
16521 set_buffer_internal_1 (XBUFFER (buffer));
16522
16523 init_iterator (&it, w, -1, -1, NULL, face_id);
16524
16525 if (no_props)
16526 {
16527 mode_line_target = MODE_LINE_NOPROP;
16528 mode_line_string_face_prop = Qnil;
16529 mode_line_string_list = Qnil;
16530 string_start = MODE_LINE_NOPROP_LEN (0);
16531 }
16532 else
16533 {
16534 mode_line_target = MODE_LINE_STRING;
16535 mode_line_string_list = Qnil;
16536 mode_line_string_face = face;
16537 mode_line_string_face_prop
16538 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16539 }
16540
16541 push_frame_kboard (it.f);
16542 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16543 pop_frame_kboard ();
16544
16545 if (no_props)
16546 {
16547 len = MODE_LINE_NOPROP_LEN (string_start);
16548 str = make_string (mode_line_noprop_buf + string_start, len);
16549 }
16550 else
16551 {
16552 mode_line_string_list = Fnreverse (mode_line_string_list);
16553 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16554 make_string ("", 0));
16555 }
16556
16557 unbind_to (count, Qnil);
16558 return str;
16559 }
16560
16561 /* Write a null-terminated, right justified decimal representation of
16562 the positive integer D to BUF using a minimal field width WIDTH. */
16563
16564 static void
16565 pint2str (buf, width, d)
16566 register char *buf;
16567 register int width;
16568 register int d;
16569 {
16570 register char *p = buf;
16571
16572 if (d <= 0)
16573 *p++ = '0';
16574 else
16575 {
16576 while (d > 0)
16577 {
16578 *p++ = d % 10 + '0';
16579 d /= 10;
16580 }
16581 }
16582
16583 for (width -= (int) (p - buf); width > 0; --width)
16584 *p++ = ' ';
16585 *p-- = '\0';
16586 while (p > buf)
16587 {
16588 d = *buf;
16589 *buf++ = *p;
16590 *p-- = d;
16591 }
16592 }
16593
16594 /* Write a null-terminated, right justified decimal and "human
16595 readable" representation of the nonnegative integer D to BUF using
16596 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16597
16598 static const char power_letter[] =
16599 {
16600 0, /* not used */
16601 'k', /* kilo */
16602 'M', /* mega */
16603 'G', /* giga */
16604 'T', /* tera */
16605 'P', /* peta */
16606 'E', /* exa */
16607 'Z', /* zetta */
16608 'Y' /* yotta */
16609 };
16610
16611 static void
16612 pint2hrstr (buf, width, d)
16613 char *buf;
16614 int width;
16615 int d;
16616 {
16617 /* We aim to represent the nonnegative integer D as
16618 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16619 int quotient = d;
16620 int remainder = 0;
16621 /* -1 means: do not use TENTHS. */
16622 int tenths = -1;
16623 int exponent = 0;
16624
16625 /* Length of QUOTIENT.TENTHS as a string. */
16626 int length;
16627
16628 char * psuffix;
16629 char * p;
16630
16631 if (1000 <= quotient)
16632 {
16633 /* Scale to the appropriate EXPONENT. */
16634 do
16635 {
16636 remainder = quotient % 1000;
16637 quotient /= 1000;
16638 exponent++;
16639 }
16640 while (1000 <= quotient);
16641
16642 /* Round to nearest and decide whether to use TENTHS or not. */
16643 if (quotient <= 9)
16644 {
16645 tenths = remainder / 100;
16646 if (50 <= remainder % 100)
16647 {
16648 if (tenths < 9)
16649 tenths++;
16650 else
16651 {
16652 quotient++;
16653 if (quotient == 10)
16654 tenths = -1;
16655 else
16656 tenths = 0;
16657 }
16658 }
16659 }
16660 else
16661 if (500 <= remainder)
16662 {
16663 if (quotient < 999)
16664 quotient++;
16665 else
16666 {
16667 quotient = 1;
16668 exponent++;
16669 tenths = 0;
16670 }
16671 }
16672 }
16673
16674 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16675 if (tenths == -1 && quotient <= 99)
16676 if (quotient <= 9)
16677 length = 1;
16678 else
16679 length = 2;
16680 else
16681 length = 3;
16682 p = psuffix = buf + max (width, length);
16683
16684 /* Print EXPONENT. */
16685 if (exponent)
16686 *psuffix++ = power_letter[exponent];
16687 *psuffix = '\0';
16688
16689 /* Print TENTHS. */
16690 if (tenths >= 0)
16691 {
16692 *--p = '0' + tenths;
16693 *--p = '.';
16694 }
16695
16696 /* Print QUOTIENT. */
16697 do
16698 {
16699 int digit = quotient % 10;
16700 *--p = '0' + digit;
16701 }
16702 while ((quotient /= 10) != 0);
16703
16704 /* Print leading spaces. */
16705 while (buf < p)
16706 *--p = ' ';
16707 }
16708
16709 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16710 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16711 type of CODING_SYSTEM. Return updated pointer into BUF. */
16712
16713 static unsigned char invalid_eol_type[] = "(*invalid*)";
16714
16715 static char *
16716 decode_mode_spec_coding (coding_system, buf, eol_flag)
16717 Lisp_Object coding_system;
16718 register char *buf;
16719 int eol_flag;
16720 {
16721 Lisp_Object val;
16722 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16723 const unsigned char *eol_str;
16724 int eol_str_len;
16725 /* The EOL conversion we are using. */
16726 Lisp_Object eoltype;
16727
16728 val = CODING_SYSTEM_SPEC (coding_system);
16729 eoltype = Qnil;
16730
16731 if (!VECTORP (val)) /* Not yet decided. */
16732 {
16733 if (multibyte)
16734 *buf++ = '-';
16735 if (eol_flag)
16736 eoltype = eol_mnemonic_undecided;
16737 /* Don't mention EOL conversion if it isn't decided. */
16738 }
16739 else
16740 {
16741 Lisp_Object attrs;
16742 Lisp_Object eolvalue;
16743
16744 attrs = AREF (val, 0);
16745 eolvalue = AREF (val, 2);
16746
16747 if (multibyte)
16748 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
16749
16750 if (eol_flag)
16751 {
16752 /* The EOL conversion that is normal on this system. */
16753
16754 if (NILP (eolvalue)) /* Not yet decided. */
16755 eoltype = eol_mnemonic_undecided;
16756 else if (VECTORP (eolvalue)) /* Not yet decided. */
16757 eoltype = eol_mnemonic_undecided;
16758 else /* eolvalue is Qunix, Qdos, or Qmac. */
16759 eoltype = (EQ (eolvalue, Qunix)
16760 ? eol_mnemonic_unix
16761 : (EQ (eolvalue, Qdos) == 1
16762 ? eol_mnemonic_dos : eol_mnemonic_mac));
16763 }
16764 }
16765
16766 if (eol_flag)
16767 {
16768 /* Mention the EOL conversion if it is not the usual one. */
16769 if (STRINGP (eoltype))
16770 {
16771 eol_str = SDATA (eoltype);
16772 eol_str_len = SBYTES (eoltype);
16773 }
16774 else if (CHARACTERP (eoltype))
16775 {
16776 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16777 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16778 eol_str = tmp;
16779 }
16780 else
16781 {
16782 eol_str = invalid_eol_type;
16783 eol_str_len = sizeof (invalid_eol_type) - 1;
16784 }
16785 bcopy (eol_str, buf, eol_str_len);
16786 buf += eol_str_len;
16787 }
16788
16789 return buf;
16790 }
16791
16792 /* Return a string for the output of a mode line %-spec for window W,
16793 generated by character C. PRECISION >= 0 means don't return a
16794 string longer than that value. FIELD_WIDTH > 0 means pad the
16795 string returned with spaces to that value. Return 1 in *MULTIBYTE
16796 if the result is multibyte text.
16797
16798 Note we operate on the current buffer for most purposes,
16799 the exception being w->base_line_pos. */
16800
16801 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16802
16803 static char *
16804 decode_mode_spec (w, c, field_width, precision, multibyte)
16805 struct window *w;
16806 register int c;
16807 int field_width, precision;
16808 int *multibyte;
16809 {
16810 Lisp_Object obj;
16811 struct frame *f = XFRAME (WINDOW_FRAME (w));
16812 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16813 struct buffer *b = current_buffer;
16814
16815 obj = Qnil;
16816 *multibyte = 0;
16817
16818 switch (c)
16819 {
16820 case '*':
16821 if (!NILP (b->read_only))
16822 return "%";
16823 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16824 return "*";
16825 return "-";
16826
16827 case '+':
16828 /* This differs from %* only for a modified read-only buffer. */
16829 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16830 return "*";
16831 if (!NILP (b->read_only))
16832 return "%";
16833 return "-";
16834
16835 case '&':
16836 /* This differs from %* in ignoring read-only-ness. */
16837 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16838 return "*";
16839 return "-";
16840
16841 case '%':
16842 return "%";
16843
16844 case '[':
16845 {
16846 int i;
16847 char *p;
16848
16849 if (command_loop_level > 5)
16850 return "[[[... ";
16851 p = decode_mode_spec_buf;
16852 for (i = 0; i < command_loop_level; i++)
16853 *p++ = '[';
16854 *p = 0;
16855 return decode_mode_spec_buf;
16856 }
16857
16858 case ']':
16859 {
16860 int i;
16861 char *p;
16862
16863 if (command_loop_level > 5)
16864 return " ...]]]";
16865 p = decode_mode_spec_buf;
16866 for (i = 0; i < command_loop_level; i++)
16867 *p++ = ']';
16868 *p = 0;
16869 return decode_mode_spec_buf;
16870 }
16871
16872 case '-':
16873 {
16874 register int i;
16875
16876 /* Let lots_of_dashes be a string of infinite length. */
16877 if (mode_line_target == MODE_LINE_NOPROP ||
16878 mode_line_target == MODE_LINE_STRING)
16879 return "--";
16880 if (field_width <= 0
16881 || field_width > sizeof (lots_of_dashes))
16882 {
16883 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16884 decode_mode_spec_buf[i] = '-';
16885 decode_mode_spec_buf[i] = '\0';
16886 return decode_mode_spec_buf;
16887 }
16888 else
16889 return lots_of_dashes;
16890 }
16891
16892 case 'b':
16893 obj = b->name;
16894 break;
16895
16896 case 'c':
16897 {
16898 int col = (int) current_column (); /* iftc */
16899 w->column_number_displayed = make_number (col);
16900 pint2str (decode_mode_spec_buf, field_width, col);
16901 return decode_mode_spec_buf;
16902 }
16903
16904 case 'F':
16905 /* %F displays the frame name. */
16906 if (!NILP (f->title))
16907 return (char *) SDATA (f->title);
16908 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16909 return (char *) SDATA (f->name);
16910 return "Emacs";
16911
16912 case 'f':
16913 obj = b->filename;
16914 break;
16915
16916 case 'i':
16917 {
16918 int size = ZV - BEGV;
16919 pint2str (decode_mode_spec_buf, field_width, size);
16920 return decode_mode_spec_buf;
16921 }
16922
16923 case 'I':
16924 {
16925 int size = ZV - BEGV;
16926 pint2hrstr (decode_mode_spec_buf, field_width, size);
16927 return decode_mode_spec_buf;
16928 }
16929
16930 case 'l':
16931 {
16932 int startpos = XMARKER (w->start)->charpos;
16933 int startpos_byte = marker_byte_position (w->start);
16934 int line, linepos, linepos_byte, topline;
16935 int nlines, junk;
16936 int height = WINDOW_TOTAL_LINES (w);
16937
16938 /* If we decided that this buffer isn't suitable for line numbers,
16939 don't forget that too fast. */
16940 if (EQ (w->base_line_pos, w->buffer))
16941 goto no_value;
16942 /* But do forget it, if the window shows a different buffer now. */
16943 else if (BUFFERP (w->base_line_pos))
16944 w->base_line_pos = Qnil;
16945
16946 /* If the buffer is very big, don't waste time. */
16947 if (INTEGERP (Vline_number_display_limit)
16948 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16949 {
16950 w->base_line_pos = Qnil;
16951 w->base_line_number = Qnil;
16952 goto no_value;
16953 }
16954
16955 if (!NILP (w->base_line_number)
16956 && !NILP (w->base_line_pos)
16957 && XFASTINT (w->base_line_pos) <= startpos)
16958 {
16959 line = XFASTINT (w->base_line_number);
16960 linepos = XFASTINT (w->base_line_pos);
16961 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16962 }
16963 else
16964 {
16965 line = 1;
16966 linepos = BUF_BEGV (b);
16967 linepos_byte = BUF_BEGV_BYTE (b);
16968 }
16969
16970 /* Count lines from base line to window start position. */
16971 nlines = display_count_lines (linepos, linepos_byte,
16972 startpos_byte,
16973 startpos, &junk);
16974
16975 topline = nlines + line;
16976
16977 /* Determine a new base line, if the old one is too close
16978 or too far away, or if we did not have one.
16979 "Too close" means it's plausible a scroll-down would
16980 go back past it. */
16981 if (startpos == BUF_BEGV (b))
16982 {
16983 w->base_line_number = make_number (topline);
16984 w->base_line_pos = make_number (BUF_BEGV (b));
16985 }
16986 else if (nlines < height + 25 || nlines > height * 3 + 50
16987 || linepos == BUF_BEGV (b))
16988 {
16989 int limit = BUF_BEGV (b);
16990 int limit_byte = BUF_BEGV_BYTE (b);
16991 int position;
16992 int distance = (height * 2 + 30) * line_number_display_limit_width;
16993
16994 if (startpos - distance > limit)
16995 {
16996 limit = startpos - distance;
16997 limit_byte = CHAR_TO_BYTE (limit);
16998 }
16999
17000 nlines = display_count_lines (startpos, startpos_byte,
17001 limit_byte,
17002 - (height * 2 + 30),
17003 &position);
17004 /* If we couldn't find the lines we wanted within
17005 line_number_display_limit_width chars per line,
17006 give up on line numbers for this window. */
17007 if (position == limit_byte && limit == startpos - distance)
17008 {
17009 w->base_line_pos = w->buffer;
17010 w->base_line_number = Qnil;
17011 goto no_value;
17012 }
17013
17014 w->base_line_number = make_number (topline - nlines);
17015 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17016 }
17017
17018 /* Now count lines from the start pos to point. */
17019 nlines = display_count_lines (startpos, startpos_byte,
17020 PT_BYTE, PT, &junk);
17021
17022 /* Record that we did display the line number. */
17023 line_number_displayed = 1;
17024
17025 /* Make the string to show. */
17026 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17027 return decode_mode_spec_buf;
17028 no_value:
17029 {
17030 char* p = decode_mode_spec_buf;
17031 int pad = field_width - 2;
17032 while (pad-- > 0)
17033 *p++ = ' ';
17034 *p++ = '?';
17035 *p++ = '?';
17036 *p = '\0';
17037 return decode_mode_spec_buf;
17038 }
17039 }
17040 break;
17041
17042 case 'm':
17043 obj = b->mode_name;
17044 break;
17045
17046 case 'n':
17047 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17048 return " Narrow";
17049 break;
17050
17051 case 'p':
17052 {
17053 int pos = marker_position (w->start);
17054 int total = BUF_ZV (b) - BUF_BEGV (b);
17055
17056 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17057 {
17058 if (pos <= BUF_BEGV (b))
17059 return "All";
17060 else
17061 return "Bottom";
17062 }
17063 else if (pos <= BUF_BEGV (b))
17064 return "Top";
17065 else
17066 {
17067 if (total > 1000000)
17068 /* Do it differently for a large value, to avoid overflow. */
17069 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17070 else
17071 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17072 /* We can't normally display a 3-digit number,
17073 so get us a 2-digit number that is close. */
17074 if (total == 100)
17075 total = 99;
17076 sprintf (decode_mode_spec_buf, "%2d%%", total);
17077 return decode_mode_spec_buf;
17078 }
17079 }
17080
17081 /* Display percentage of size above the bottom of the screen. */
17082 case 'P':
17083 {
17084 int toppos = marker_position (w->start);
17085 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17086 int total = BUF_ZV (b) - BUF_BEGV (b);
17087
17088 if (botpos >= BUF_ZV (b))
17089 {
17090 if (toppos <= BUF_BEGV (b))
17091 return "All";
17092 else
17093 return "Bottom";
17094 }
17095 else
17096 {
17097 if (total > 1000000)
17098 /* Do it differently for a large value, to avoid overflow. */
17099 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17100 else
17101 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17102 /* We can't normally display a 3-digit number,
17103 so get us a 2-digit number that is close. */
17104 if (total == 100)
17105 total = 99;
17106 if (toppos <= BUF_BEGV (b))
17107 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17108 else
17109 sprintf (decode_mode_spec_buf, "%2d%%", total);
17110 return decode_mode_spec_buf;
17111 }
17112 }
17113
17114 case 's':
17115 /* status of process */
17116 obj = Fget_buffer_process (Fcurrent_buffer ());
17117 if (NILP (obj))
17118 return "no process";
17119 #ifdef subprocesses
17120 obj = Fsymbol_name (Fprocess_status (obj));
17121 #endif
17122 break;
17123
17124 case 't': /* indicate TEXT or BINARY */
17125 #ifdef MODE_LINE_BINARY_TEXT
17126 return MODE_LINE_BINARY_TEXT (b);
17127 #else
17128 return "T";
17129 #endif
17130
17131 case 'z':
17132 /* coding-system (not including end-of-line format) */
17133 case 'Z':
17134 /* coding-system (including end-of-line type) */
17135 {
17136 int eol_flag = (c == 'Z');
17137 char *p = decode_mode_spec_buf;
17138
17139 if (! FRAME_WINDOW_P (f))
17140 {
17141 /* No need to mention EOL here--the terminal never needs
17142 to do EOL conversion. */
17143 p = decode_mode_spec_coding (CODING_ID_NAME (keyboard_coding.id),
17144 p, 0);
17145 p = decode_mode_spec_coding (CODING_ID_NAME (terminal_coding.id),
17146 p, 0);
17147 }
17148 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17149 p, eol_flag);
17150
17151 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17152 #ifdef subprocesses
17153 obj = Fget_buffer_process (Fcurrent_buffer ());
17154 if (PROCESSP (obj))
17155 {
17156 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17157 p, eol_flag);
17158 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17159 p, eol_flag);
17160 }
17161 #endif /* subprocesses */
17162 #endif /* 0 */
17163 *p = 0;
17164 return decode_mode_spec_buf;
17165 }
17166 }
17167
17168 if (STRINGP (obj))
17169 {
17170 *multibyte = STRING_MULTIBYTE (obj);
17171 return (char *) SDATA (obj);
17172 }
17173 else
17174 return "";
17175 }
17176
17177
17178 /* Count up to COUNT lines starting from START / START_BYTE.
17179 But don't go beyond LIMIT_BYTE.
17180 Return the number of lines thus found (always nonnegative).
17181
17182 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17183
17184 static int
17185 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17186 int start, start_byte, limit_byte, count;
17187 int *byte_pos_ptr;
17188 {
17189 register unsigned char *cursor;
17190 unsigned char *base;
17191
17192 register int ceiling;
17193 register unsigned char *ceiling_addr;
17194 int orig_count = count;
17195
17196 /* If we are not in selective display mode,
17197 check only for newlines. */
17198 int selective_display = (!NILP (current_buffer->selective_display)
17199 && !INTEGERP (current_buffer->selective_display));
17200
17201 if (count > 0)
17202 {
17203 while (start_byte < limit_byte)
17204 {
17205 ceiling = BUFFER_CEILING_OF (start_byte);
17206 ceiling = min (limit_byte - 1, ceiling);
17207 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17208 base = (cursor = BYTE_POS_ADDR (start_byte));
17209 while (1)
17210 {
17211 if (selective_display)
17212 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17213 ;
17214 else
17215 while (*cursor != '\n' && ++cursor != ceiling_addr)
17216 ;
17217
17218 if (cursor != ceiling_addr)
17219 {
17220 if (--count == 0)
17221 {
17222 start_byte += cursor - base + 1;
17223 *byte_pos_ptr = start_byte;
17224 return orig_count;
17225 }
17226 else
17227 if (++cursor == ceiling_addr)
17228 break;
17229 }
17230 else
17231 break;
17232 }
17233 start_byte += cursor - base;
17234 }
17235 }
17236 else
17237 {
17238 while (start_byte > limit_byte)
17239 {
17240 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17241 ceiling = max (limit_byte, ceiling);
17242 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17243 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17244 while (1)
17245 {
17246 if (selective_display)
17247 while (--cursor != ceiling_addr
17248 && *cursor != '\n' && *cursor != 015)
17249 ;
17250 else
17251 while (--cursor != ceiling_addr && *cursor != '\n')
17252 ;
17253
17254 if (cursor != ceiling_addr)
17255 {
17256 if (++count == 0)
17257 {
17258 start_byte += cursor - base + 1;
17259 *byte_pos_ptr = start_byte;
17260 /* When scanning backwards, we should
17261 not count the newline posterior to which we stop. */
17262 return - orig_count - 1;
17263 }
17264 }
17265 else
17266 break;
17267 }
17268 /* Here we add 1 to compensate for the last decrement
17269 of CURSOR, which took it past the valid range. */
17270 start_byte += cursor - base + 1;
17271 }
17272 }
17273
17274 *byte_pos_ptr = limit_byte;
17275
17276 if (count < 0)
17277 return - orig_count + count;
17278 return orig_count - count;
17279
17280 }
17281
17282
17283 \f
17284 /***********************************************************************
17285 Displaying strings
17286 ***********************************************************************/
17287
17288 /* Display a NUL-terminated string, starting with index START.
17289
17290 If STRING is non-null, display that C string. Otherwise, the Lisp
17291 string LISP_STRING is displayed.
17292
17293 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17294 FACE_STRING. Display STRING or LISP_STRING with the face at
17295 FACE_STRING_POS in FACE_STRING:
17296
17297 Display the string in the environment given by IT, but use the
17298 standard display table, temporarily.
17299
17300 FIELD_WIDTH is the minimum number of output glyphs to produce.
17301 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17302 with spaces. If STRING has more characters, more than FIELD_WIDTH
17303 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17304
17305 PRECISION is the maximum number of characters to output from
17306 STRING. PRECISION < 0 means don't truncate the string.
17307
17308 This is roughly equivalent to printf format specifiers:
17309
17310 FIELD_WIDTH PRECISION PRINTF
17311 ----------------------------------------
17312 -1 -1 %s
17313 -1 10 %.10s
17314 10 -1 %10s
17315 20 10 %20.10s
17316
17317 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17318 display them, and < 0 means obey the current buffer's value of
17319 enable_multibyte_characters.
17320
17321 Value is the number of glyphs produced. */
17322
17323 static int
17324 display_string (string, lisp_string, face_string, face_string_pos,
17325 start, it, field_width, precision, max_x, multibyte)
17326 unsigned char *string;
17327 Lisp_Object lisp_string;
17328 Lisp_Object face_string;
17329 int face_string_pos;
17330 int start;
17331 struct it *it;
17332 int field_width, precision, max_x;
17333 int multibyte;
17334 {
17335 int hpos_at_start = it->hpos;
17336 int saved_face_id = it->face_id;
17337 struct glyph_row *row = it->glyph_row;
17338
17339 /* Initialize the iterator IT for iteration over STRING beginning
17340 with index START. */
17341 reseat_to_string (it, string, lisp_string, start,
17342 precision, field_width, multibyte);
17343
17344 /* If displaying STRING, set up the face of the iterator
17345 from LISP_STRING, if that's given. */
17346 if (STRINGP (face_string))
17347 {
17348 int endptr;
17349 struct face *face;
17350
17351 it->face_id
17352 = face_at_string_position (it->w, face_string, face_string_pos,
17353 0, it->region_beg_charpos,
17354 it->region_end_charpos,
17355 &endptr, it->base_face_id, 0);
17356 face = FACE_FROM_ID (it->f, it->face_id);
17357 it->face_box_p = face->box != FACE_NO_BOX;
17358 }
17359
17360 /* Set max_x to the maximum allowed X position. Don't let it go
17361 beyond the right edge of the window. */
17362 if (max_x <= 0)
17363 max_x = it->last_visible_x;
17364 else
17365 max_x = min (max_x, it->last_visible_x);
17366
17367 /* Skip over display elements that are not visible. because IT->w is
17368 hscrolled. */
17369 if (it->current_x < it->first_visible_x)
17370 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17371 MOVE_TO_POS | MOVE_TO_X);
17372
17373 row->ascent = it->max_ascent;
17374 row->height = it->max_ascent + it->max_descent;
17375 row->phys_ascent = it->max_phys_ascent;
17376 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17377 row->extra_line_spacing = it->max_extra_line_spacing;
17378
17379 /* This condition is for the case that we are called with current_x
17380 past last_visible_x. */
17381 while (it->current_x < max_x)
17382 {
17383 int x_before, x, n_glyphs_before, i, nglyphs;
17384
17385 /* Get the next display element. */
17386 if (!get_next_display_element (it))
17387 break;
17388
17389 /* Produce glyphs. */
17390 x_before = it->current_x;
17391 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17392 PRODUCE_GLYPHS (it);
17393
17394 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17395 i = 0;
17396 x = x_before;
17397 while (i < nglyphs)
17398 {
17399 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17400
17401 if (!it->truncate_lines_p
17402 && x + glyph->pixel_width > max_x)
17403 {
17404 /* End of continued line or max_x reached. */
17405 if (CHAR_GLYPH_PADDING_P (*glyph))
17406 {
17407 /* A wide character is unbreakable. */
17408 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17409 it->current_x = x_before;
17410 }
17411 else
17412 {
17413 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17414 it->current_x = x;
17415 }
17416 break;
17417 }
17418 else if (x + glyph->pixel_width >= it->first_visible_x)
17419 {
17420 /* Glyph is at least partially visible. */
17421 ++it->hpos;
17422 if (x < it->first_visible_x)
17423 it->glyph_row->x = x - it->first_visible_x;
17424 }
17425 else
17426 {
17427 /* Glyph is off the left margin of the display area.
17428 Should not happen. */
17429 abort ();
17430 }
17431
17432 row->ascent = max (row->ascent, it->max_ascent);
17433 row->height = max (row->height, it->max_ascent + it->max_descent);
17434 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17435 row->phys_height = max (row->phys_height,
17436 it->max_phys_ascent + it->max_phys_descent);
17437 row->extra_line_spacing = max (row->extra_line_spacing,
17438 it->max_extra_line_spacing);
17439 x += glyph->pixel_width;
17440 ++i;
17441 }
17442
17443 /* Stop if max_x reached. */
17444 if (i < nglyphs)
17445 break;
17446
17447 /* Stop at line ends. */
17448 if (ITERATOR_AT_END_OF_LINE_P (it))
17449 {
17450 it->continuation_lines_width = 0;
17451 break;
17452 }
17453
17454 set_iterator_to_next (it, 1);
17455
17456 /* Stop if truncating at the right edge. */
17457 if (it->truncate_lines_p
17458 && it->current_x >= it->last_visible_x)
17459 {
17460 /* Add truncation mark, but don't do it if the line is
17461 truncated at a padding space. */
17462 if (IT_CHARPOS (*it) < it->string_nchars)
17463 {
17464 if (!FRAME_WINDOW_P (it->f))
17465 {
17466 int i, n;
17467
17468 if (it->current_x > it->last_visible_x)
17469 {
17470 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17471 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17472 break;
17473 for (n = row->used[TEXT_AREA]; i < n; ++i)
17474 {
17475 row->used[TEXT_AREA] = i;
17476 produce_special_glyphs (it, IT_TRUNCATION);
17477 }
17478 }
17479 produce_special_glyphs (it, IT_TRUNCATION);
17480 }
17481 it->glyph_row->truncated_on_right_p = 1;
17482 }
17483 break;
17484 }
17485 }
17486
17487 /* Maybe insert a truncation at the left. */
17488 if (it->first_visible_x
17489 && IT_CHARPOS (*it) > 0)
17490 {
17491 if (!FRAME_WINDOW_P (it->f))
17492 insert_left_trunc_glyphs (it);
17493 it->glyph_row->truncated_on_left_p = 1;
17494 }
17495
17496 it->face_id = saved_face_id;
17497
17498 /* Value is number of columns displayed. */
17499 return it->hpos - hpos_at_start;
17500 }
17501
17502
17503 \f
17504 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17505 appears as an element of LIST or as the car of an element of LIST.
17506 If PROPVAL is a list, compare each element against LIST in that
17507 way, and return 1/2 if any element of PROPVAL is found in LIST.
17508 Otherwise return 0. This function cannot quit.
17509 The return value is 2 if the text is invisible but with an ellipsis
17510 and 1 if it's invisible and without an ellipsis. */
17511
17512 int
17513 invisible_p (propval, list)
17514 register Lisp_Object propval;
17515 Lisp_Object list;
17516 {
17517 register Lisp_Object tail, proptail;
17518
17519 for (tail = list; CONSP (tail); tail = XCDR (tail))
17520 {
17521 register Lisp_Object tem;
17522 tem = XCAR (tail);
17523 if (EQ (propval, tem))
17524 return 1;
17525 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17526 return NILP (XCDR (tem)) ? 1 : 2;
17527 }
17528
17529 if (CONSP (propval))
17530 {
17531 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17532 {
17533 Lisp_Object propelt;
17534 propelt = XCAR (proptail);
17535 for (tail = list; CONSP (tail); tail = XCDR (tail))
17536 {
17537 register Lisp_Object tem;
17538 tem = XCAR (tail);
17539 if (EQ (propelt, tem))
17540 return 1;
17541 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17542 return NILP (XCDR (tem)) ? 1 : 2;
17543 }
17544 }
17545 }
17546
17547 return 0;
17548 }
17549
17550 /* Calculate a width or height in pixels from a specification using
17551 the following elements:
17552
17553 SPEC ::=
17554 NUM - a (fractional) multiple of the default font width/height
17555 (NUM) - specifies exactly NUM pixels
17556 UNIT - a fixed number of pixels, see below.
17557 ELEMENT - size of a display element in pixels, see below.
17558 (NUM . SPEC) - equals NUM * SPEC
17559 (+ SPEC SPEC ...) - add pixel values
17560 (- SPEC SPEC ...) - subtract pixel values
17561 (- SPEC) - negate pixel value
17562
17563 NUM ::=
17564 INT or FLOAT - a number constant
17565 SYMBOL - use symbol's (buffer local) variable binding.
17566
17567 UNIT ::=
17568 in - pixels per inch *)
17569 mm - pixels per 1/1000 meter *)
17570 cm - pixels per 1/100 meter *)
17571 width - width of current font in pixels.
17572 height - height of current font in pixels.
17573
17574 *) using the ratio(s) defined in display-pixels-per-inch.
17575
17576 ELEMENT ::=
17577
17578 left-fringe - left fringe width in pixels
17579 right-fringe - right fringe width in pixels
17580
17581 left-margin - left margin width in pixels
17582 right-margin - right margin width in pixels
17583
17584 scroll-bar - scroll-bar area width in pixels
17585
17586 Examples:
17587
17588 Pixels corresponding to 5 inches:
17589 (5 . in)
17590
17591 Total width of non-text areas on left side of window (if scroll-bar is on left):
17592 '(space :width (+ left-fringe left-margin scroll-bar))
17593
17594 Align to first text column (in header line):
17595 '(space :align-to 0)
17596
17597 Align to middle of text area minus half the width of variable `my-image'
17598 containing a loaded image:
17599 '(space :align-to (0.5 . (- text my-image)))
17600
17601 Width of left margin minus width of 1 character in the default font:
17602 '(space :width (- left-margin 1))
17603
17604 Width of left margin minus width of 2 characters in the current font:
17605 '(space :width (- left-margin (2 . width)))
17606
17607 Center 1 character over left-margin (in header line):
17608 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17609
17610 Different ways to express width of left fringe plus left margin minus one pixel:
17611 '(space :width (- (+ left-fringe left-margin) (1)))
17612 '(space :width (+ left-fringe left-margin (- (1))))
17613 '(space :width (+ left-fringe left-margin (-1)))
17614
17615 */
17616
17617 #define NUMVAL(X) \
17618 ((INTEGERP (X) || FLOATP (X)) \
17619 ? XFLOATINT (X) \
17620 : - 1)
17621
17622 int
17623 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17624 double *res;
17625 struct it *it;
17626 Lisp_Object prop;
17627 void *font;
17628 int width_p, *align_to;
17629 {
17630 double pixels;
17631
17632 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17633 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17634
17635 if (NILP (prop))
17636 return OK_PIXELS (0);
17637
17638 if (SYMBOLP (prop))
17639 {
17640 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17641 {
17642 char *unit = SDATA (SYMBOL_NAME (prop));
17643
17644 if (unit[0] == 'i' && unit[1] == 'n')
17645 pixels = 1.0;
17646 else if (unit[0] == 'm' && unit[1] == 'm')
17647 pixels = 25.4;
17648 else if (unit[0] == 'c' && unit[1] == 'm')
17649 pixels = 2.54;
17650 else
17651 pixels = 0;
17652 if (pixels > 0)
17653 {
17654 double ppi;
17655 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17656 || (CONSP (Vdisplay_pixels_per_inch)
17657 && (ppi = (width_p
17658 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17659 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17660 ppi > 0)))
17661 return OK_PIXELS (ppi / pixels);
17662
17663 return 0;
17664 }
17665 }
17666
17667 #ifdef HAVE_WINDOW_SYSTEM
17668 if (EQ (prop, Qheight))
17669 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17670 if (EQ (prop, Qwidth))
17671 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17672 #else
17673 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17674 return OK_PIXELS (1);
17675 #endif
17676
17677 if (EQ (prop, Qtext))
17678 return OK_PIXELS (width_p
17679 ? window_box_width (it->w, TEXT_AREA)
17680 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17681
17682 if (align_to && *align_to < 0)
17683 {
17684 *res = 0;
17685 if (EQ (prop, Qleft))
17686 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17687 if (EQ (prop, Qright))
17688 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17689 if (EQ (prop, Qcenter))
17690 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17691 + window_box_width (it->w, TEXT_AREA) / 2);
17692 if (EQ (prop, Qleft_fringe))
17693 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17694 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17695 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17696 if (EQ (prop, Qright_fringe))
17697 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17698 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17699 : window_box_right_offset (it->w, TEXT_AREA));
17700 if (EQ (prop, Qleft_margin))
17701 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17702 if (EQ (prop, Qright_margin))
17703 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17704 if (EQ (prop, Qscroll_bar))
17705 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17706 ? 0
17707 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17708 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17709 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17710 : 0)));
17711 }
17712 else
17713 {
17714 if (EQ (prop, Qleft_fringe))
17715 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17716 if (EQ (prop, Qright_fringe))
17717 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17718 if (EQ (prop, Qleft_margin))
17719 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17720 if (EQ (prop, Qright_margin))
17721 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17722 if (EQ (prop, Qscroll_bar))
17723 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17724 }
17725
17726 prop = Fbuffer_local_value (prop, it->w->buffer);
17727 }
17728
17729 if (INTEGERP (prop) || FLOATP (prop))
17730 {
17731 int base_unit = (width_p
17732 ? FRAME_COLUMN_WIDTH (it->f)
17733 : FRAME_LINE_HEIGHT (it->f));
17734 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17735 }
17736
17737 if (CONSP (prop))
17738 {
17739 Lisp_Object car = XCAR (prop);
17740 Lisp_Object cdr = XCDR (prop);
17741
17742 if (SYMBOLP (car))
17743 {
17744 #ifdef HAVE_WINDOW_SYSTEM
17745 if (valid_image_p (prop))
17746 {
17747 int id = lookup_image (it->f, prop);
17748 struct image *img = IMAGE_FROM_ID (it->f, id);
17749
17750 return OK_PIXELS (width_p ? img->width : img->height);
17751 }
17752 #endif
17753 if (EQ (car, Qplus) || EQ (car, Qminus))
17754 {
17755 int first = 1;
17756 double px;
17757
17758 pixels = 0;
17759 while (CONSP (cdr))
17760 {
17761 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17762 font, width_p, align_to))
17763 return 0;
17764 if (first)
17765 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17766 else
17767 pixels += px;
17768 cdr = XCDR (cdr);
17769 }
17770 if (EQ (car, Qminus))
17771 pixels = -pixels;
17772 return OK_PIXELS (pixels);
17773 }
17774
17775 car = Fbuffer_local_value (car, it->w->buffer);
17776 }
17777
17778 if (INTEGERP (car) || FLOATP (car))
17779 {
17780 double fact;
17781 pixels = XFLOATINT (car);
17782 if (NILP (cdr))
17783 return OK_PIXELS (pixels);
17784 if (calc_pixel_width_or_height (&fact, it, cdr,
17785 font, width_p, align_to))
17786 return OK_PIXELS (pixels * fact);
17787 return 0;
17788 }
17789
17790 return 0;
17791 }
17792
17793 return 0;
17794 }
17795
17796 \f
17797 /***********************************************************************
17798 Glyph Display
17799 ***********************************************************************/
17800
17801 #ifdef HAVE_WINDOW_SYSTEM
17802
17803 #if GLYPH_DEBUG
17804
17805 void
17806 dump_glyph_string (s)
17807 struct glyph_string *s;
17808 {
17809 fprintf (stderr, "glyph string\n");
17810 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17811 s->x, s->y, s->width, s->height);
17812 fprintf (stderr, " ybase = %d\n", s->ybase);
17813 fprintf (stderr, " hl = %d\n", s->hl);
17814 fprintf (stderr, " left overhang = %d, right = %d\n",
17815 s->left_overhang, s->right_overhang);
17816 fprintf (stderr, " nchars = %d\n", s->nchars);
17817 fprintf (stderr, " extends to end of line = %d\n",
17818 s->extends_to_end_of_line_p);
17819 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17820 fprintf (stderr, " bg width = %d\n", s->background_width);
17821 }
17822
17823 #endif /* GLYPH_DEBUG */
17824
17825 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17826 of XChar2b structures for S; it can't be allocated in
17827 init_glyph_string because it must be allocated via `alloca'. W
17828 is the window on which S is drawn. ROW and AREA are the glyph row
17829 and area within the row from which S is constructed. START is the
17830 index of the first glyph structure covered by S. HL is a
17831 face-override for drawing S. */
17832
17833 #ifdef HAVE_NTGUI
17834 #define OPTIONAL_HDC(hdc) hdc,
17835 #define DECLARE_HDC(hdc) HDC hdc;
17836 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17837 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17838 #endif
17839
17840 #ifndef OPTIONAL_HDC
17841 #define OPTIONAL_HDC(hdc)
17842 #define DECLARE_HDC(hdc)
17843 #define ALLOCATE_HDC(hdc, f)
17844 #define RELEASE_HDC(hdc, f)
17845 #endif
17846
17847 static void
17848 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17849 struct glyph_string *s;
17850 DECLARE_HDC (hdc)
17851 XChar2b *char2b;
17852 struct window *w;
17853 struct glyph_row *row;
17854 enum glyph_row_area area;
17855 int start;
17856 enum draw_glyphs_face hl;
17857 {
17858 bzero (s, sizeof *s);
17859 s->w = w;
17860 s->f = XFRAME (w->frame);
17861 #ifdef HAVE_NTGUI
17862 s->hdc = hdc;
17863 #endif
17864 s->display = FRAME_X_DISPLAY (s->f);
17865 s->window = FRAME_X_WINDOW (s->f);
17866 s->char2b = char2b;
17867 s->hl = hl;
17868 s->row = row;
17869 s->area = area;
17870 s->first_glyph = row->glyphs[area] + start;
17871 s->height = row->height;
17872 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17873
17874 /* Display the internal border below the tool-bar window. */
17875 if (WINDOWP (s->f->tool_bar_window)
17876 && s->w == XWINDOW (s->f->tool_bar_window))
17877 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17878
17879 s->ybase = s->y + row->ascent;
17880 }
17881
17882
17883 /* Append the list of glyph strings with head H and tail T to the list
17884 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17885
17886 static INLINE void
17887 append_glyph_string_lists (head, tail, h, t)
17888 struct glyph_string **head, **tail;
17889 struct glyph_string *h, *t;
17890 {
17891 if (h)
17892 {
17893 if (*head)
17894 (*tail)->next = h;
17895 else
17896 *head = h;
17897 h->prev = *tail;
17898 *tail = t;
17899 }
17900 }
17901
17902
17903 /* Prepend the list of glyph strings with head H and tail T to the
17904 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17905 result. */
17906
17907 static INLINE void
17908 prepend_glyph_string_lists (head, tail, h, t)
17909 struct glyph_string **head, **tail;
17910 struct glyph_string *h, *t;
17911 {
17912 if (h)
17913 {
17914 if (*head)
17915 (*head)->prev = t;
17916 else
17917 *tail = t;
17918 t->next = *head;
17919 *head = h;
17920 }
17921 }
17922
17923
17924 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17925 Set *HEAD and *TAIL to the resulting list. */
17926
17927 static INLINE void
17928 append_glyph_string (head, tail, s)
17929 struct glyph_string **head, **tail;
17930 struct glyph_string *s;
17931 {
17932 s->next = s->prev = NULL;
17933 append_glyph_string_lists (head, tail, s, s);
17934 }
17935
17936
17937 /* Get face and two-byte form of character glyph GLYPH on frame F.
17938 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17939 a pointer to a realized face that is ready for display. */
17940
17941 static INLINE struct face *
17942 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17943 struct frame *f;
17944 struct glyph *glyph;
17945 XChar2b *char2b;
17946 int *two_byte_p;
17947 {
17948 struct face *face;
17949
17950 xassert (glyph->type == CHAR_GLYPH);
17951 face = FACE_FROM_ID (f, glyph->face_id);
17952
17953 if (two_byte_p)
17954 *two_byte_p = 0;
17955
17956 if (!glyph->multibyte_p)
17957 {
17958 /* Unibyte case. We don't have to encode, but we have to make
17959 sure to use a face suitable for unibyte. */
17960 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17961 }
17962 else if (glyph->u.ch < 128
17963 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17964 {
17965 /* Case of ASCII in a face known to fit ASCII. */
17966 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17967 }
17968 else
17969 {
17970 struct font_info *font_info
17971 = FONT_INFO_FROM_ID (f, face->font_info_id);
17972 if (font_info)
17973 {
17974 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
17975 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
17976
17977 if (CHARSET_DIMENSION (charset) == 1)
17978 STORE_XCHAR2B (char2b, 0, code);
17979 else
17980 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
17981
17982 /* Maybe encode the character in *CHAR2B. */
17983 if (CHARSET_ID (charset) != charset_ascii)
17984 {
17985 glyph->font_type
17986 = rif->encode_char (glyph->u.ch, char2b, font_info, charset,
17987 two_byte_p);
17988 }
17989 }
17990 }
17991
17992 /* Make sure X resources of the face are allocated. */
17993 xassert (face != NULL);
17994 PREPARE_FACE_FOR_DISPLAY (f, face);
17995 return face;
17996 }
17997
17998
17999 /* Fill glyph string S with composition components specified by S->cmp.
18000
18001 FACES is an array of faces for all components of this composition.
18002 S->gidx is the index of the first component for S.
18003 OVERLAPS_P non-zero means S should draw the foreground only, and
18004 use its physical height for clipping.
18005
18006 Value is the index of a component not in S. */
18007
18008 static int
18009 fill_composite_glyph_string (s, faces, overlaps_p)
18010 struct glyph_string *s;
18011 struct face **faces;
18012 int overlaps_p;
18013 {
18014 int i;
18015
18016 xassert (s);
18017
18018 s->for_overlaps_p = overlaps_p;
18019
18020 s->face = faces[s->gidx];
18021 s->font = s->face->font;
18022 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18023
18024 /* For all glyphs of this composition, starting at the offset
18025 S->gidx, until we reach the end of the definition or encounter a
18026 glyph that requires the different face, add it to S. */
18027 ++s->nchars;
18028 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18029 ++s->nchars;
18030
18031 /* All glyph strings for the same composition has the same width,
18032 i.e. the width set for the first component of the composition. */
18033
18034 s->width = s->first_glyph->pixel_width;
18035
18036 /* If the specified font could not be loaded, use the frame's
18037 default font, but record the fact that we couldn't load it in
18038 the glyph string so that we can draw rectangles for the
18039 characters of the glyph string. */
18040 if (s->font == NULL)
18041 {
18042 s->font_not_found_p = 1;
18043 s->font = FRAME_FONT (s->f);
18044 }
18045
18046 /* Adjust base line for subscript/superscript text. */
18047 s->ybase += s->first_glyph->voffset;
18048
18049 xassert (s->face && s->face->gc);
18050
18051 /* This glyph string must always be drawn with 16-bit functions. */
18052 s->two_byte_p = 1;
18053
18054 return s->gidx + s->nchars;
18055 }
18056
18057
18058 /* Fill glyph string S from a sequence of character glyphs.
18059
18060 FACE_ID is the face id of the string. START is the index of the
18061 first glyph to consider, END is the index of the last + 1.
18062 OVERLAPS_P non-zero means S should draw the foreground only, and
18063 use its physical height for clipping.
18064
18065 Value is the index of the first glyph not in S. */
18066
18067 static int
18068 fill_glyph_string (s, face_id, start, end, overlaps_p)
18069 struct glyph_string *s;
18070 int face_id;
18071 int start, end, overlaps_p;
18072 {
18073 struct glyph *glyph, *last;
18074 int voffset;
18075 int glyph_not_available_p;
18076
18077 xassert (s->f == XFRAME (s->w->frame));
18078 xassert (s->nchars == 0);
18079 xassert (start >= 0 && end > start);
18080
18081 s->for_overlaps_p = overlaps_p,
18082 glyph = s->row->glyphs[s->area] + start;
18083 last = s->row->glyphs[s->area] + end;
18084 voffset = glyph->voffset;
18085
18086 glyph_not_available_p = glyph->glyph_not_available_p;
18087
18088 while (glyph < last
18089 && glyph->type == CHAR_GLYPH
18090 && glyph->voffset == voffset
18091 /* Same face id implies same font, nowadays. */
18092 && glyph->face_id == face_id
18093 && glyph->glyph_not_available_p == glyph_not_available_p)
18094 {
18095 int two_byte_p;
18096
18097 s->face = get_glyph_face_and_encoding (s->f, glyph,
18098 s->char2b + s->nchars,
18099 &two_byte_p);
18100 s->two_byte_p = two_byte_p;
18101 ++s->nchars;
18102 xassert (s->nchars <= end - start);
18103 s->width += glyph->pixel_width;
18104 ++glyph;
18105 }
18106
18107 s->font = s->face->font;
18108 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18109
18110 /* If the specified font could not be loaded, use the frame's font,
18111 but record the fact that we couldn't load it in
18112 S->font_not_found_p so that we can draw rectangles for the
18113 characters of the glyph string. */
18114 if (s->font == NULL || glyph_not_available_p)
18115 {
18116 s->font_not_found_p = 1;
18117 s->font = FRAME_FONT (s->f);
18118 }
18119
18120 /* Adjust base line for subscript/superscript text. */
18121 s->ybase += voffset;
18122
18123 xassert (s->face && s->face->gc);
18124 return glyph - s->row->glyphs[s->area];
18125 }
18126
18127
18128 /* Fill glyph string S from image glyph S->first_glyph. */
18129
18130 static void
18131 fill_image_glyph_string (s)
18132 struct glyph_string *s;
18133 {
18134 xassert (s->first_glyph->type == IMAGE_GLYPH);
18135 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18136 xassert (s->img);
18137 s->slice = s->first_glyph->slice;
18138 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18139 s->font = s->face->font;
18140 s->width = s->first_glyph->pixel_width;
18141
18142 /* Adjust base line for subscript/superscript text. */
18143 s->ybase += s->first_glyph->voffset;
18144 }
18145
18146
18147 /* Fill glyph string S from a sequence of stretch glyphs.
18148
18149 ROW is the glyph row in which the glyphs are found, AREA is the
18150 area within the row. START is the index of the first glyph to
18151 consider, END is the index of the last + 1.
18152
18153 Value is the index of the first glyph not in S. */
18154
18155 static int
18156 fill_stretch_glyph_string (s, row, area, start, end)
18157 struct glyph_string *s;
18158 struct glyph_row *row;
18159 enum glyph_row_area area;
18160 int start, end;
18161 {
18162 struct glyph *glyph, *last;
18163 int voffset, face_id;
18164
18165 xassert (s->first_glyph->type == STRETCH_GLYPH);
18166
18167 glyph = s->row->glyphs[s->area] + start;
18168 last = s->row->glyphs[s->area] + end;
18169 face_id = glyph->face_id;
18170 s->face = FACE_FROM_ID (s->f, face_id);
18171 s->font = s->face->font;
18172 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18173 s->width = glyph->pixel_width;
18174 voffset = glyph->voffset;
18175
18176 for (++glyph;
18177 (glyph < last
18178 && glyph->type == STRETCH_GLYPH
18179 && glyph->voffset == voffset
18180 && glyph->face_id == face_id);
18181 ++glyph)
18182 s->width += glyph->pixel_width;
18183
18184 /* Adjust base line for subscript/superscript text. */
18185 s->ybase += voffset;
18186
18187 /* The case that face->gc == 0 is handled when drawing the glyph
18188 string by calling PREPARE_FACE_FOR_DISPLAY. */
18189 xassert (s->face);
18190 return glyph - s->row->glyphs[s->area];
18191 }
18192
18193
18194 /* EXPORT for RIF:
18195 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18196 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18197 assumed to be zero. */
18198
18199 void
18200 x_get_glyph_overhangs (glyph, f, left, right)
18201 struct glyph *glyph;
18202 struct frame *f;
18203 int *left, *right;
18204 {
18205 *left = *right = 0;
18206
18207 if (glyph->type == CHAR_GLYPH)
18208 {
18209 XFontStruct *font;
18210 struct face *face;
18211 struct font_info *font_info;
18212 XChar2b char2b;
18213 XCharStruct *pcm;
18214
18215 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18216 font = face->font;
18217 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18218 if (font /* ++KFS: Should this be font_info ? */
18219 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18220 {
18221 if (pcm->rbearing > pcm->width)
18222 *right = pcm->rbearing - pcm->width;
18223 if (pcm->lbearing < 0)
18224 *left = -pcm->lbearing;
18225 }
18226 }
18227 else if (glyph->type == COMPOSITE_GLYPH)
18228 {
18229 struct composition *cmp = composition_table[glyph->u.cmp_id];
18230
18231 *right = cmp->rbearing - cmp->pixel_width;
18232 *left = - cmp->lbearing;
18233 }
18234 }
18235
18236
18237 /* Return the index of the first glyph preceding glyph string S that
18238 is overwritten by S because of S's left overhang. Value is -1
18239 if no glyphs are overwritten. */
18240
18241 static int
18242 left_overwritten (s)
18243 struct glyph_string *s;
18244 {
18245 int k;
18246
18247 if (s->left_overhang)
18248 {
18249 int x = 0, i;
18250 struct glyph *glyphs = s->row->glyphs[s->area];
18251 int first = s->first_glyph - glyphs;
18252
18253 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18254 x -= glyphs[i].pixel_width;
18255
18256 k = i + 1;
18257 }
18258 else
18259 k = -1;
18260
18261 return k;
18262 }
18263
18264
18265 /* Return the index of the first glyph preceding glyph string S that
18266 is overwriting S because of its right overhang. Value is -1 if no
18267 glyph in front of S overwrites S. */
18268
18269 static int
18270 left_overwriting (s)
18271 struct glyph_string *s;
18272 {
18273 int i, k, x;
18274 struct glyph *glyphs = s->row->glyphs[s->area];
18275 int first = s->first_glyph - glyphs;
18276
18277 k = -1;
18278 x = 0;
18279 for (i = first - 1; i >= 0; --i)
18280 {
18281 int left, right;
18282 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18283 if (x + right > 0)
18284 k = i;
18285 x -= glyphs[i].pixel_width;
18286 }
18287
18288 return k;
18289 }
18290
18291
18292 /* Return the index of the last glyph following glyph string S that is
18293 not overwritten by S because of S's right overhang. Value is -1 if
18294 no such glyph is found. */
18295
18296 static int
18297 right_overwritten (s)
18298 struct glyph_string *s;
18299 {
18300 int k = -1;
18301
18302 if (s->right_overhang)
18303 {
18304 int x = 0, i;
18305 struct glyph *glyphs = s->row->glyphs[s->area];
18306 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18307 int end = s->row->used[s->area];
18308
18309 for (i = first; i < end && s->right_overhang > x; ++i)
18310 x += glyphs[i].pixel_width;
18311
18312 k = i;
18313 }
18314
18315 return k;
18316 }
18317
18318
18319 /* Return the index of the last glyph following glyph string S that
18320 overwrites S because of its left overhang. Value is negative
18321 if no such glyph is found. */
18322
18323 static int
18324 right_overwriting (s)
18325 struct glyph_string *s;
18326 {
18327 int i, k, x;
18328 int end = s->row->used[s->area];
18329 struct glyph *glyphs = s->row->glyphs[s->area];
18330 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18331
18332 k = -1;
18333 x = 0;
18334 for (i = first; i < end; ++i)
18335 {
18336 int left, right;
18337 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18338 if (x - left < 0)
18339 k = i;
18340 x += glyphs[i].pixel_width;
18341 }
18342
18343 return k;
18344 }
18345
18346
18347 /* Get face and two-byte form of character C in face FACE_ID on frame
18348 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18349 means we want to display multibyte text. DISPLAY_P non-zero means
18350 make sure that X resources for the face returned are allocated.
18351 Value is a pointer to a realized face that is ready for display if
18352 DISPLAY_P is non-zero. */
18353
18354 static INLINE struct face *
18355 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18356 struct frame *f;
18357 int c, face_id;
18358 XChar2b *char2b;
18359 int multibyte_p, display_p;
18360 {
18361 struct face *face = FACE_FROM_ID (f, face_id);
18362
18363 if (!multibyte_p)
18364 {
18365 /* Unibyte case. We don't have to encode, but we have to make
18366 sure to use a face suitable for unibyte. */
18367 STORE_XCHAR2B (char2b, 0, c);
18368 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
18369 face = FACE_FROM_ID (f, face_id);
18370 }
18371 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18372 {
18373 /* Case of ASCII in a face known to fit ASCII. */
18374 STORE_XCHAR2B (char2b, 0, c);
18375 }
18376 else if (face->font != NULL)
18377 {
18378 struct font_info *font_info
18379 = FONT_INFO_FROM_ID (f, face->font_info_id);
18380 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
18381 unsigned code = ENCODE_CHAR (charset, c);
18382
18383 if (CHARSET_DIMENSION (charset) == 1)
18384 STORE_XCHAR2B (char2b, 0, code);
18385 else
18386 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
18387 /* Maybe encode the character in *CHAR2B. */
18388 rif->encode_char (c, char2b, font_info, charset, NULL);
18389 }
18390
18391 /* Make sure X resources of the face are allocated. */
18392 #ifdef HAVE_X_WINDOWS
18393 if (display_p)
18394 #endif
18395 {
18396 xassert (face != NULL);
18397 PREPARE_FACE_FOR_DISPLAY (f, face);
18398 }
18399
18400 return face;
18401 }
18402
18403
18404 /* Set background width of glyph string S. START is the index of the
18405 first glyph following S. LAST_X is the right-most x-position + 1
18406 in the drawing area. */
18407
18408 static INLINE void
18409 set_glyph_string_background_width (s, start, last_x)
18410 struct glyph_string *s;
18411 int start;
18412 int last_x;
18413 {
18414 /* If the face of this glyph string has to be drawn to the end of
18415 the drawing area, set S->extends_to_end_of_line_p. */
18416 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18417
18418 if (start == s->row->used[s->area]
18419 && s->area == TEXT_AREA
18420 && ((s->hl == DRAW_NORMAL_TEXT
18421 && (s->row->fill_line_p
18422 || s->face->background != default_face->background
18423 || s->face->stipple != default_face->stipple
18424 || s->row->mouse_face_p))
18425 || s->hl == DRAW_MOUSE_FACE
18426 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18427 && s->row->fill_line_p)))
18428 s->extends_to_end_of_line_p = 1;
18429
18430 /* If S extends its face to the end of the line, set its
18431 background_width to the distance to the right edge of the drawing
18432 area. */
18433 if (s->extends_to_end_of_line_p)
18434 s->background_width = last_x - s->x + 1;
18435 else
18436 s->background_width = s->width;
18437 }
18438
18439
18440 /* Compute overhangs and x-positions for glyph string S and its
18441 predecessors, or successors. X is the starting x-position for S.
18442 BACKWARD_P non-zero means process predecessors. */
18443
18444 static void
18445 compute_overhangs_and_x (s, x, backward_p)
18446 struct glyph_string *s;
18447 int x;
18448 int backward_p;
18449 {
18450 if (backward_p)
18451 {
18452 while (s)
18453 {
18454 if (rif->compute_glyph_string_overhangs)
18455 rif->compute_glyph_string_overhangs (s);
18456 x -= s->width;
18457 s->x = x;
18458 s = s->prev;
18459 }
18460 }
18461 else
18462 {
18463 while (s)
18464 {
18465 if (rif->compute_glyph_string_overhangs)
18466 rif->compute_glyph_string_overhangs (s);
18467 s->x = x;
18468 x += s->width;
18469 s = s->next;
18470 }
18471 }
18472 }
18473
18474
18475
18476 /* The following macros are only called from draw_glyphs below.
18477 They reference the following parameters of that function directly:
18478 `w', `row', `area', and `overlap_p'
18479 as well as the following local variables:
18480 `s', `f', and `hdc' (in W32) */
18481
18482 #ifdef HAVE_NTGUI
18483 /* On W32, silently add local `hdc' variable to argument list of
18484 init_glyph_string. */
18485 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18486 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18487 #else
18488 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18489 init_glyph_string (s, char2b, w, row, area, start, hl)
18490 #endif
18491
18492 /* Add a glyph string for a stretch glyph to the list of strings
18493 between HEAD and TAIL. START is the index of the stretch glyph in
18494 row area AREA of glyph row ROW. END is the index of the last glyph
18495 in that glyph row area. X is the current output position assigned
18496 to the new glyph string constructed. HL overrides that face of the
18497 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18498 is the right-most x-position of the drawing area. */
18499
18500 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18501 and below -- keep them on one line. */
18502 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18503 do \
18504 { \
18505 s = (struct glyph_string *) alloca (sizeof *s); \
18506 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18507 START = fill_stretch_glyph_string (s, row, area, START, END); \
18508 append_glyph_string (&HEAD, &TAIL, s); \
18509 s->x = (X); \
18510 } \
18511 while (0)
18512
18513
18514 /* Add a glyph string for an image glyph to the list of strings
18515 between HEAD and TAIL. START is the index of the image glyph in
18516 row area AREA of glyph row ROW. END is the index of the last glyph
18517 in that glyph row area. X is the current output position assigned
18518 to the new glyph string constructed. HL overrides that face of the
18519 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18520 is the right-most x-position of the drawing area. */
18521
18522 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18523 do \
18524 { \
18525 s = (struct glyph_string *) alloca (sizeof *s); \
18526 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18527 fill_image_glyph_string (s); \
18528 append_glyph_string (&HEAD, &TAIL, s); \
18529 ++START; \
18530 s->x = (X); \
18531 } \
18532 while (0)
18533
18534
18535 /* Add a glyph string for a sequence of character glyphs to the list
18536 of strings between HEAD and TAIL. START is the index of the first
18537 glyph in row area AREA of glyph row ROW that is part of the new
18538 glyph string. END is the index of the last glyph in that glyph row
18539 area. X is the current output position assigned to the new glyph
18540 string constructed. HL overrides that face of the glyph; e.g. it
18541 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18542 right-most x-position of the drawing area. */
18543
18544 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18545 do \
18546 { \
18547 int face_id; \
18548 XChar2b *char2b; \
18549 \
18550 face_id = (row)->glyphs[area][START].face_id; \
18551 \
18552 s = (struct glyph_string *) alloca (sizeof *s); \
18553 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18554 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18555 append_glyph_string (&HEAD, &TAIL, s); \
18556 s->x = (X); \
18557 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18558 } \
18559 while (0)
18560
18561
18562 /* Add a glyph string for a composite sequence to the list of strings
18563 between HEAD and TAIL. START is the index of the first glyph in
18564 row area AREA of glyph row ROW that is part of the new glyph
18565 string. END is the index of the last glyph in that glyph row area.
18566 X is the current output position assigned to the new glyph string
18567 constructed. HL overrides that face of the glyph; e.g. it is
18568 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18569 x-position of the drawing area. */
18570
18571 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18572 do { \
18573 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18574 int face_id = (row)->glyphs[area][START].face_id; \
18575 struct face *base_face = FACE_FROM_ID (f, face_id); \
18576 struct composition *cmp = composition_table[cmp_id]; \
18577 int glyph_len = cmp->glyph_len; \
18578 XChar2b *char2b; \
18579 struct face **faces; \
18580 struct glyph_string *first_s = NULL; \
18581 int n; \
18582 \
18583 base_face = base_face->ascii_face; \
18584 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18585 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18586 /* At first, fill in `char2b' and `faces'. */ \
18587 for (n = 0; n < glyph_len; n++) \
18588 { \
18589 int c = COMPOSITION_GLYPH (cmp, n); \
18590 int this_face_id = FACE_FOR_CHAR (f, base_face, c, -1, Qnil); \
18591 faces[n] = FACE_FROM_ID (f, this_face_id); \
18592 get_char_face_and_encoding (f, c, this_face_id, \
18593 char2b + n, 1, 1); \
18594 } \
18595 \
18596 /* Make glyph_strings for each glyph sequence that is drawable by \
18597 the same face, and append them to HEAD/TAIL. */ \
18598 for (n = 0; n < cmp->glyph_len;) \
18599 { \
18600 s = (struct glyph_string *) alloca (sizeof *s); \
18601 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18602 append_glyph_string (&(HEAD), &(TAIL), s); \
18603 s->cmp = cmp; \
18604 s->gidx = n; \
18605 s->x = (X); \
18606 \
18607 if (n == 0) \
18608 first_s = s; \
18609 \
18610 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18611 } \
18612 \
18613 ++START; \
18614 s = first_s; \
18615 } while (0)
18616
18617
18618 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18619 of AREA of glyph row ROW on window W between indices START and END.
18620 HL overrides the face for drawing glyph strings, e.g. it is
18621 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18622 x-positions of the drawing area.
18623
18624 This is an ugly monster macro construct because we must use alloca
18625 to allocate glyph strings (because draw_glyphs can be called
18626 asynchronously). */
18627
18628 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18629 do \
18630 { \
18631 HEAD = TAIL = NULL; \
18632 while (START < END) \
18633 { \
18634 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18635 switch (first_glyph->type) \
18636 { \
18637 case CHAR_GLYPH: \
18638 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18639 HL, X, LAST_X); \
18640 break; \
18641 \
18642 case COMPOSITE_GLYPH: \
18643 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18644 HL, X, LAST_X); \
18645 break; \
18646 \
18647 case STRETCH_GLYPH: \
18648 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18649 HL, X, LAST_X); \
18650 break; \
18651 \
18652 case IMAGE_GLYPH: \
18653 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18654 HL, X, LAST_X); \
18655 break; \
18656 \
18657 default: \
18658 abort (); \
18659 } \
18660 \
18661 if (s) \
18662 { \
18663 set_glyph_string_background_width (s, START, LAST_X); \
18664 (X) += s->width; \
18665 } \
18666 } \
18667 } \
18668 while (0)
18669
18670
18671 /* Draw glyphs between START and END in AREA of ROW on window W,
18672 starting at x-position X. X is relative to AREA in W. HL is a
18673 face-override with the following meaning:
18674
18675 DRAW_NORMAL_TEXT draw normally
18676 DRAW_CURSOR draw in cursor face
18677 DRAW_MOUSE_FACE draw in mouse face.
18678 DRAW_INVERSE_VIDEO draw in mode line face
18679 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18680 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18681
18682 If OVERLAPS_P is non-zero, draw only the foreground of characters
18683 and clip to the physical height of ROW.
18684
18685 Value is the x-position reached, relative to AREA of W. */
18686
18687 static int
18688 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18689 struct window *w;
18690 int x;
18691 struct glyph_row *row;
18692 enum glyph_row_area area;
18693 EMACS_INT start, end;
18694 enum draw_glyphs_face hl;
18695 int overlaps_p;
18696 {
18697 struct glyph_string *head, *tail;
18698 struct glyph_string *s;
18699 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18700 int last_x, area_width;
18701 int x_reached;
18702 int i, j;
18703 struct frame *f = XFRAME (WINDOW_FRAME (w));
18704 DECLARE_HDC (hdc);
18705
18706 ALLOCATE_HDC (hdc, f);
18707
18708 /* Let's rather be paranoid than getting a SEGV. */
18709 end = min (end, row->used[area]);
18710 start = max (0, start);
18711 start = min (end, start);
18712
18713 /* Translate X to frame coordinates. Set last_x to the right
18714 end of the drawing area. */
18715 if (row->full_width_p)
18716 {
18717 /* X is relative to the left edge of W, without scroll bars
18718 or fringes. */
18719 x += WINDOW_LEFT_EDGE_X (w);
18720 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18721 }
18722 else
18723 {
18724 int area_left = window_box_left (w, area);
18725 x += area_left;
18726 area_width = window_box_width (w, area);
18727 last_x = area_left + area_width;
18728 }
18729
18730 /* Build a doubly-linked list of glyph_string structures between
18731 head and tail from what we have to draw. Note that the macro
18732 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18733 the reason we use a separate variable `i'. */
18734 i = start;
18735 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18736 if (tail)
18737 x_reached = tail->x + tail->background_width;
18738 else
18739 x_reached = x;
18740
18741 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18742 the row, redraw some glyphs in front or following the glyph
18743 strings built above. */
18744 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18745 {
18746 int dummy_x = 0;
18747 struct glyph_string *h, *t;
18748
18749 /* Compute overhangs for all glyph strings. */
18750 if (rif->compute_glyph_string_overhangs)
18751 for (s = head; s; s = s->next)
18752 rif->compute_glyph_string_overhangs (s);
18753
18754 /* Prepend glyph strings for glyphs in front of the first glyph
18755 string that are overwritten because of the first glyph
18756 string's left overhang. The background of all strings
18757 prepended must be drawn because the first glyph string
18758 draws over it. */
18759 i = left_overwritten (head);
18760 if (i >= 0)
18761 {
18762 j = i;
18763 BUILD_GLYPH_STRINGS (j, start, h, t,
18764 DRAW_NORMAL_TEXT, dummy_x, last_x);
18765 start = i;
18766 compute_overhangs_and_x (t, head->x, 1);
18767 prepend_glyph_string_lists (&head, &tail, h, t);
18768 clip_head = head;
18769 }
18770
18771 /* Prepend glyph strings for glyphs in front of the first glyph
18772 string that overwrite that glyph string because of their
18773 right overhang. For these strings, only the foreground must
18774 be drawn, because it draws over the glyph string at `head'.
18775 The background must not be drawn because this would overwrite
18776 right overhangs of preceding glyphs for which no glyph
18777 strings exist. */
18778 i = left_overwriting (head);
18779 if (i >= 0)
18780 {
18781 clip_head = head;
18782 BUILD_GLYPH_STRINGS (i, start, h, t,
18783 DRAW_NORMAL_TEXT, dummy_x, last_x);
18784 for (s = h; s; s = s->next)
18785 s->background_filled_p = 1;
18786 compute_overhangs_and_x (t, head->x, 1);
18787 prepend_glyph_string_lists (&head, &tail, h, t);
18788 }
18789
18790 /* Append glyphs strings for glyphs following the last glyph
18791 string tail that are overwritten by tail. The background of
18792 these strings has to be drawn because tail's foreground draws
18793 over it. */
18794 i = right_overwritten (tail);
18795 if (i >= 0)
18796 {
18797 BUILD_GLYPH_STRINGS (end, i, h, t,
18798 DRAW_NORMAL_TEXT, x, last_x);
18799 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18800 append_glyph_string_lists (&head, &tail, h, t);
18801 clip_tail = tail;
18802 }
18803
18804 /* Append glyph strings for glyphs following the last glyph
18805 string tail that overwrite tail. The foreground of such
18806 glyphs has to be drawn because it writes into the background
18807 of tail. The background must not be drawn because it could
18808 paint over the foreground of following glyphs. */
18809 i = right_overwriting (tail);
18810 if (i >= 0)
18811 {
18812 clip_tail = tail;
18813 BUILD_GLYPH_STRINGS (end, i, h, t,
18814 DRAW_NORMAL_TEXT, x, last_x);
18815 for (s = h; s; s = s->next)
18816 s->background_filled_p = 1;
18817 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18818 append_glyph_string_lists (&head, &tail, h, t);
18819 }
18820 if (clip_head || clip_tail)
18821 for (s = head; s; s = s->next)
18822 {
18823 s->clip_head = clip_head;
18824 s->clip_tail = clip_tail;
18825 }
18826 }
18827
18828 /* Draw all strings. */
18829 for (s = head; s; s = s->next)
18830 rif->draw_glyph_string (s);
18831
18832 if (area == TEXT_AREA
18833 && !row->full_width_p
18834 /* When drawing overlapping rows, only the glyph strings'
18835 foreground is drawn, which doesn't erase a cursor
18836 completely. */
18837 && !overlaps_p)
18838 {
18839 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18840 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18841 : (tail ? tail->x + tail->background_width : x));
18842
18843 int text_left = window_box_left (w, TEXT_AREA);
18844 x0 -= text_left;
18845 x1 -= text_left;
18846
18847 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18848 row->y, MATRIX_ROW_BOTTOM_Y (row));
18849 }
18850
18851 /* Value is the x-position up to which drawn, relative to AREA of W.
18852 This doesn't include parts drawn because of overhangs. */
18853 if (row->full_width_p)
18854 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18855 else
18856 x_reached -= window_box_left (w, area);
18857
18858 RELEASE_HDC (hdc, f);
18859
18860 return x_reached;
18861 }
18862
18863 /* Expand row matrix if too narrow. Don't expand if area
18864 is not present. */
18865
18866 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18867 { \
18868 if (!fonts_changed_p \
18869 && (it->glyph_row->glyphs[area] \
18870 < it->glyph_row->glyphs[area + 1])) \
18871 { \
18872 it->w->ncols_scale_factor++; \
18873 fonts_changed_p = 1; \
18874 } \
18875 }
18876
18877 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18878 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18879
18880 static INLINE void
18881 append_glyph (it)
18882 struct it *it;
18883 {
18884 struct glyph *glyph;
18885 enum glyph_row_area area = it->area;
18886
18887 xassert (it->glyph_row);
18888 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18889
18890 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18891 if (glyph < it->glyph_row->glyphs[area + 1])
18892 {
18893 glyph->charpos = CHARPOS (it->position);
18894 glyph->object = it->object;
18895 glyph->pixel_width = it->pixel_width;
18896 glyph->ascent = it->ascent;
18897 glyph->descent = it->descent;
18898 glyph->voffset = it->voffset;
18899 glyph->type = CHAR_GLYPH;
18900 glyph->multibyte_p = it->multibyte_p;
18901 glyph->left_box_line_p = it->start_of_box_run_p;
18902 glyph->right_box_line_p = it->end_of_box_run_p;
18903 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18904 || it->phys_descent > it->descent);
18905 glyph->padding_p = 0;
18906 glyph->glyph_not_available_p = it->glyph_not_available_p;
18907 glyph->face_id = it->face_id;
18908 glyph->u.ch = it->char_to_display;
18909 glyph->slice = null_glyph_slice;
18910 glyph->font_type = FONT_TYPE_UNKNOWN;
18911 ++it->glyph_row->used[area];
18912 }
18913 else
18914 IT_EXPAND_MATRIX_WIDTH (it, area);
18915 }
18916
18917 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18918 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18919
18920 static INLINE void
18921 append_composite_glyph (it)
18922 struct it *it;
18923 {
18924 struct glyph *glyph;
18925 enum glyph_row_area area = it->area;
18926
18927 xassert (it->glyph_row);
18928
18929 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18930 if (glyph < it->glyph_row->glyphs[area + 1])
18931 {
18932 glyph->charpos = CHARPOS (it->position);
18933 glyph->object = it->object;
18934 glyph->pixel_width = it->pixel_width;
18935 glyph->ascent = it->ascent;
18936 glyph->descent = it->descent;
18937 glyph->voffset = it->voffset;
18938 glyph->type = COMPOSITE_GLYPH;
18939 glyph->multibyte_p = it->multibyte_p;
18940 glyph->left_box_line_p = it->start_of_box_run_p;
18941 glyph->right_box_line_p = it->end_of_box_run_p;
18942 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18943 || it->phys_descent > it->descent);
18944 glyph->padding_p = 0;
18945 glyph->glyph_not_available_p = 0;
18946 glyph->face_id = it->face_id;
18947 glyph->u.cmp_id = it->cmp_id;
18948 glyph->slice = null_glyph_slice;
18949 glyph->font_type = FONT_TYPE_UNKNOWN;
18950 ++it->glyph_row->used[area];
18951 }
18952 else
18953 IT_EXPAND_MATRIX_WIDTH (it, area);
18954 }
18955
18956
18957 /* Change IT->ascent and IT->height according to the setting of
18958 IT->voffset. */
18959
18960 static INLINE void
18961 take_vertical_position_into_account (it)
18962 struct it *it;
18963 {
18964 if (it->voffset)
18965 {
18966 if (it->voffset < 0)
18967 /* Increase the ascent so that we can display the text higher
18968 in the line. */
18969 it->ascent -= it->voffset;
18970 else
18971 /* Increase the descent so that we can display the text lower
18972 in the line. */
18973 it->descent += it->voffset;
18974 }
18975 }
18976
18977
18978 /* Produce glyphs/get display metrics for the image IT is loaded with.
18979 See the description of struct display_iterator in dispextern.h for
18980 an overview of struct display_iterator. */
18981
18982 static void
18983 produce_image_glyph (it)
18984 struct it *it;
18985 {
18986 struct image *img;
18987 struct face *face;
18988 int glyph_ascent;
18989 struct glyph_slice slice;
18990
18991 xassert (it->what == IT_IMAGE);
18992
18993 face = FACE_FROM_ID (it->f, it->face_id);
18994 xassert (face);
18995 /* Make sure X resources of the face is loaded. */
18996 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18997
18998 if (it->image_id < 0)
18999 {
19000 /* Fringe bitmap. */
19001 it->ascent = it->phys_ascent = 0;
19002 it->descent = it->phys_descent = 0;
19003 it->pixel_width = 0;
19004 it->nglyphs = 0;
19005 return;
19006 }
19007
19008 img = IMAGE_FROM_ID (it->f, it->image_id);
19009 xassert (img);
19010 /* Make sure X resources of the image is loaded. */
19011 prepare_image_for_display (it->f, img);
19012
19013 slice.x = slice.y = 0;
19014 slice.width = img->width;
19015 slice.height = img->height;
19016
19017 if (INTEGERP (it->slice.x))
19018 slice.x = XINT (it->slice.x);
19019 else if (FLOATP (it->slice.x))
19020 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19021
19022 if (INTEGERP (it->slice.y))
19023 slice.y = XINT (it->slice.y);
19024 else if (FLOATP (it->slice.y))
19025 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19026
19027 if (INTEGERP (it->slice.width))
19028 slice.width = XINT (it->slice.width);
19029 else if (FLOATP (it->slice.width))
19030 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19031
19032 if (INTEGERP (it->slice.height))
19033 slice.height = XINT (it->slice.height);
19034 else if (FLOATP (it->slice.height))
19035 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19036
19037 if (slice.x >= img->width)
19038 slice.x = img->width;
19039 if (slice.y >= img->height)
19040 slice.y = img->height;
19041 if (slice.x + slice.width >= img->width)
19042 slice.width = img->width - slice.x;
19043 if (slice.y + slice.height > img->height)
19044 slice.height = img->height - slice.y;
19045
19046 if (slice.width == 0 || slice.height == 0)
19047 return;
19048
19049 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19050
19051 it->descent = slice.height - glyph_ascent;
19052 if (slice.y == 0)
19053 it->descent += img->vmargin;
19054 if (slice.y + slice.height == img->height)
19055 it->descent += img->vmargin;
19056 it->phys_descent = it->descent;
19057
19058 it->pixel_width = slice.width;
19059 if (slice.x == 0)
19060 it->pixel_width += img->hmargin;
19061 if (slice.x + slice.width == img->width)
19062 it->pixel_width += img->hmargin;
19063
19064 /* It's quite possible for images to have an ascent greater than
19065 their height, so don't get confused in that case. */
19066 if (it->descent < 0)
19067 it->descent = 0;
19068
19069 #if 0 /* this breaks image tiling */
19070 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19071 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19072 if (face_ascent > it->ascent)
19073 it->ascent = it->phys_ascent = face_ascent;
19074 #endif
19075
19076 it->nglyphs = 1;
19077
19078 if (face->box != FACE_NO_BOX)
19079 {
19080 if (face->box_line_width > 0)
19081 {
19082 if (slice.y == 0)
19083 it->ascent += face->box_line_width;
19084 if (slice.y + slice.height == img->height)
19085 it->descent += face->box_line_width;
19086 }
19087
19088 if (it->start_of_box_run_p && slice.x == 0)
19089 it->pixel_width += abs (face->box_line_width);
19090 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19091 it->pixel_width += abs (face->box_line_width);
19092 }
19093
19094 take_vertical_position_into_account (it);
19095
19096 if (it->glyph_row)
19097 {
19098 struct glyph *glyph;
19099 enum glyph_row_area area = it->area;
19100
19101 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19102 if (glyph < it->glyph_row->glyphs[area + 1])
19103 {
19104 glyph->charpos = CHARPOS (it->position);
19105 glyph->object = it->object;
19106 glyph->pixel_width = it->pixel_width;
19107 glyph->ascent = glyph_ascent;
19108 glyph->descent = it->descent;
19109 glyph->voffset = it->voffset;
19110 glyph->type = IMAGE_GLYPH;
19111 glyph->multibyte_p = it->multibyte_p;
19112 glyph->left_box_line_p = it->start_of_box_run_p;
19113 glyph->right_box_line_p = it->end_of_box_run_p;
19114 glyph->overlaps_vertically_p = 0;
19115 glyph->padding_p = 0;
19116 glyph->glyph_not_available_p = 0;
19117 glyph->face_id = it->face_id;
19118 glyph->u.img_id = img->id;
19119 glyph->slice = slice;
19120 glyph->font_type = FONT_TYPE_UNKNOWN;
19121 ++it->glyph_row->used[area];
19122 }
19123 else
19124 IT_EXPAND_MATRIX_WIDTH (it, area);
19125 }
19126 }
19127
19128
19129 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19130 of the glyph, WIDTH and HEIGHT are the width and height of the
19131 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19132
19133 static void
19134 append_stretch_glyph (it, object, width, height, ascent)
19135 struct it *it;
19136 Lisp_Object object;
19137 int width, height;
19138 int ascent;
19139 {
19140 struct glyph *glyph;
19141 enum glyph_row_area area = it->area;
19142
19143 xassert (ascent >= 0 && ascent <= height);
19144
19145 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19146 if (glyph < it->glyph_row->glyphs[area + 1])
19147 {
19148 glyph->charpos = CHARPOS (it->position);
19149 glyph->object = object;
19150 glyph->pixel_width = width;
19151 glyph->ascent = ascent;
19152 glyph->descent = height - ascent;
19153 glyph->voffset = it->voffset;
19154 glyph->type = STRETCH_GLYPH;
19155 glyph->multibyte_p = it->multibyte_p;
19156 glyph->left_box_line_p = it->start_of_box_run_p;
19157 glyph->right_box_line_p = it->end_of_box_run_p;
19158 glyph->overlaps_vertically_p = 0;
19159 glyph->padding_p = 0;
19160 glyph->glyph_not_available_p = 0;
19161 glyph->face_id = it->face_id;
19162 glyph->u.stretch.ascent = ascent;
19163 glyph->u.stretch.height = height;
19164 glyph->slice = null_glyph_slice;
19165 glyph->font_type = FONT_TYPE_UNKNOWN;
19166 ++it->glyph_row->used[area];
19167 }
19168 else
19169 IT_EXPAND_MATRIX_WIDTH (it, area);
19170 }
19171
19172
19173 /* Produce a stretch glyph for iterator IT. IT->object is the value
19174 of the glyph property displayed. The value must be a list
19175 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19176 being recognized:
19177
19178 1. `:width WIDTH' specifies that the space should be WIDTH *
19179 canonical char width wide. WIDTH may be an integer or floating
19180 point number.
19181
19182 2. `:relative-width FACTOR' specifies that the width of the stretch
19183 should be computed from the width of the first character having the
19184 `glyph' property, and should be FACTOR times that width.
19185
19186 3. `:align-to HPOS' specifies that the space should be wide enough
19187 to reach HPOS, a value in canonical character units.
19188
19189 Exactly one of the above pairs must be present.
19190
19191 4. `:height HEIGHT' specifies that the height of the stretch produced
19192 should be HEIGHT, measured in canonical character units.
19193
19194 5. `:relative-height FACTOR' specifies that the height of the
19195 stretch should be FACTOR times the height of the characters having
19196 the glyph property.
19197
19198 Either none or exactly one of 4 or 5 must be present.
19199
19200 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19201 of the stretch should be used for the ascent of the stretch.
19202 ASCENT must be in the range 0 <= ASCENT <= 100. */
19203
19204 static void
19205 produce_stretch_glyph (it)
19206 struct it *it;
19207 {
19208 /* (space :width WIDTH :height HEIGHT ...) */
19209 Lisp_Object prop, plist;
19210 int width = 0, height = 0, align_to = -1;
19211 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19212 int ascent = 0;
19213 double tem;
19214 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19215 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19216
19217 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19218
19219 /* List should start with `space'. */
19220 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19221 plist = XCDR (it->object);
19222
19223 /* Compute the width of the stretch. */
19224 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19225 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19226 {
19227 /* Absolute width `:width WIDTH' specified and valid. */
19228 zero_width_ok_p = 1;
19229 width = (int)tem;
19230 }
19231 else if (prop = Fplist_get (plist, QCrelative_width),
19232 NUMVAL (prop) > 0)
19233 {
19234 /* Relative width `:relative-width FACTOR' specified and valid.
19235 Compute the width of the characters having the `glyph'
19236 property. */
19237 struct it it2;
19238 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19239
19240 it2 = *it;
19241 if (it->multibyte_p)
19242 {
19243 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19244 - IT_BYTEPOS (*it));
19245 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19246 }
19247 else
19248 it2.c = *p, it2.len = 1;
19249
19250 it2.glyph_row = NULL;
19251 it2.what = IT_CHARACTER;
19252 x_produce_glyphs (&it2);
19253 width = NUMVAL (prop) * it2.pixel_width;
19254 }
19255 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19256 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19257 {
19258 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19259 align_to = (align_to < 0
19260 ? 0
19261 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19262 else if (align_to < 0)
19263 align_to = window_box_left_offset (it->w, TEXT_AREA);
19264 width = max (0, (int)tem + align_to - it->current_x);
19265 zero_width_ok_p = 1;
19266 }
19267 else
19268 /* Nothing specified -> width defaults to canonical char width. */
19269 width = FRAME_COLUMN_WIDTH (it->f);
19270
19271 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19272 width = 1;
19273
19274 /* Compute height. */
19275 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19276 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19277 {
19278 height = (int)tem;
19279 zero_height_ok_p = 1;
19280 }
19281 else if (prop = Fplist_get (plist, QCrelative_height),
19282 NUMVAL (prop) > 0)
19283 height = FONT_HEIGHT (font) * NUMVAL (prop);
19284 else
19285 height = FONT_HEIGHT (font);
19286
19287 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19288 height = 1;
19289
19290 /* Compute percentage of height used for ascent. If
19291 `:ascent ASCENT' is present and valid, use that. Otherwise,
19292 derive the ascent from the font in use. */
19293 if (prop = Fplist_get (plist, QCascent),
19294 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19295 ascent = height * NUMVAL (prop) / 100.0;
19296 else if (!NILP (prop)
19297 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19298 ascent = min (max (0, (int)tem), height);
19299 else
19300 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19301
19302 if (width > 0 && height > 0 && it->glyph_row)
19303 {
19304 Lisp_Object object = it->stack[it->sp - 1].string;
19305 if (!STRINGP (object))
19306 object = it->w->buffer;
19307 append_stretch_glyph (it, object, width, height, ascent);
19308 }
19309
19310 it->pixel_width = width;
19311 it->ascent = it->phys_ascent = ascent;
19312 it->descent = it->phys_descent = height - it->ascent;
19313 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19314
19315 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19316 {
19317 if (face->box_line_width > 0)
19318 {
19319 it->ascent += face->box_line_width;
19320 it->descent += face->box_line_width;
19321 }
19322
19323 if (it->start_of_box_run_p)
19324 it->pixel_width += abs (face->box_line_width);
19325 if (it->end_of_box_run_p)
19326 it->pixel_width += abs (face->box_line_width);
19327 }
19328
19329 take_vertical_position_into_account (it);
19330 }
19331
19332 /* Get line-height and line-spacing property at point.
19333 If line-height has format (HEIGHT TOTAL), return TOTAL
19334 in TOTAL_HEIGHT. */
19335
19336 static Lisp_Object
19337 get_line_height_property (it, prop)
19338 struct it *it;
19339 Lisp_Object prop;
19340 {
19341 Lisp_Object position;
19342
19343 if (STRINGP (it->object))
19344 position = make_number (IT_STRING_CHARPOS (*it));
19345 else if (BUFFERP (it->object))
19346 position = make_number (IT_CHARPOS (*it));
19347 else
19348 return Qnil;
19349
19350 return Fget_char_property (position, prop, it->object);
19351 }
19352
19353 /* Calculate line-height and line-spacing properties.
19354 An integer value specifies explicit pixel value.
19355 A float value specifies relative value to current face height.
19356 A cons (float . face-name) specifies relative value to
19357 height of specified face font.
19358
19359 Returns height in pixels, or nil. */
19360
19361
19362 static Lisp_Object
19363 calc_line_height_property (it, val, font, boff, override)
19364 struct it *it;
19365 Lisp_Object val;
19366 XFontStruct *font;
19367 int boff, override;
19368 {
19369 Lisp_Object face_name = Qnil;
19370 int ascent, descent, height;
19371
19372 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19373 return val;
19374
19375 if (CONSP (val))
19376 {
19377 face_name = XCAR (val);
19378 val = XCDR (val);
19379 if (!NUMBERP (val))
19380 val = make_number (1);
19381 if (NILP (face_name))
19382 {
19383 height = it->ascent + it->descent;
19384 goto scale;
19385 }
19386 }
19387
19388 if (NILP (face_name))
19389 {
19390 font = FRAME_FONT (it->f);
19391 boff = FRAME_BASELINE_OFFSET (it->f);
19392 }
19393 else if (EQ (face_name, Qt))
19394 {
19395 override = 0;
19396 }
19397 else
19398 {
19399 int face_id;
19400 struct face *face;
19401 struct font_info *font_info;
19402
19403 face_id = lookup_named_face (it->f, face_name, 0);
19404 if (face_id < 0)
19405 return make_number (-1);
19406
19407 face = FACE_FROM_ID (it->f, face_id);
19408 font = face->font;
19409 if (font == NULL)
19410 return make_number (-1);
19411
19412 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19413 boff = font_info->baseline_offset;
19414 if (font_info->vertical_centering)
19415 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19416 }
19417
19418 ascent = FONT_BASE (font) + boff;
19419 descent = FONT_DESCENT (font) - boff;
19420
19421 if (override)
19422 {
19423 it->override_ascent = ascent;
19424 it->override_descent = descent;
19425 it->override_boff = boff;
19426 }
19427
19428 height = ascent + descent;
19429
19430 scale:
19431 if (FLOATP (val))
19432 height = (int)(XFLOAT_DATA (val) * height);
19433 else if (INTEGERP (val))
19434 height *= XINT (val);
19435
19436 return make_number (height);
19437 }
19438
19439
19440 /* RIF:
19441 Produce glyphs/get display metrics for the display element IT is
19442 loaded with. See the description of struct display_iterator in
19443 dispextern.h for an overview of struct display_iterator. */
19444
19445 void
19446 x_produce_glyphs (it)
19447 struct it *it;
19448 {
19449 int extra_line_spacing = it->extra_line_spacing;
19450
19451 it->glyph_not_available_p = 0;
19452
19453 if (it->what == IT_CHARACTER)
19454 {
19455 XChar2b char2b;
19456 XFontStruct *font;
19457 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19458 XCharStruct *pcm;
19459 int font_not_found_p;
19460 struct font_info *font_info;
19461 int boff; /* baseline offset */
19462 /* We may change it->multibyte_p upon unibyte<->multibyte
19463 conversion. So, save the current value now and restore it
19464 later.
19465
19466 Note: It seems that we don't have to record multibyte_p in
19467 struct glyph because the character code itself tells if or
19468 not the character is multibyte. Thus, in the future, we must
19469 consider eliminating the field `multibyte_p' in the struct
19470 glyph. */
19471 int saved_multibyte_p = it->multibyte_p;
19472
19473 /* Maybe translate single-byte characters to multibyte, or the
19474 other way. */
19475 it->char_to_display = it->c;
19476 if (!ASCII_BYTE_P (it->c)
19477 && ! it->multibyte_p)
19478 {
19479 if (SINGLE_BYTE_CHAR_P (it->c)
19480 && unibyte_display_via_language_environment)
19481 it->char_to_display = unibyte_char_to_multibyte (it->c);
19482 if (! SINGLE_BYTE_CHAR_P (it->c))
19483 {
19484 it->multibyte_p = 1;
19485 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
19486 -1, Qnil);
19487 face = FACE_FROM_ID (it->f, it->face_id);
19488 }
19489 }
19490
19491 /* Get font to use. Encode IT->char_to_display. */
19492 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19493 &char2b, it->multibyte_p, 0);
19494 font = face->font;
19495
19496 /* When no suitable font found, use the default font. */
19497 font_not_found_p = font == NULL;
19498 if (font_not_found_p)
19499 {
19500 font = FRAME_FONT (it->f);
19501 boff = FRAME_BASELINE_OFFSET (it->f);
19502 font_info = NULL;
19503 }
19504 else
19505 {
19506 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19507 boff = font_info->baseline_offset;
19508 if (font_info->vertical_centering)
19509 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19510 }
19511
19512 if (it->char_to_display >= ' '
19513 && (!it->multibyte_p || it->char_to_display < 128))
19514 {
19515 /* Either unibyte or ASCII. */
19516 int stretched_p;
19517
19518 it->nglyphs = 1;
19519
19520 pcm = rif->per_char_metric (font, &char2b,
19521 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19522
19523 if (it->override_ascent >= 0)
19524 {
19525 it->ascent = it->override_ascent;
19526 it->descent = it->override_descent;
19527 boff = it->override_boff;
19528 }
19529 else
19530 {
19531 it->ascent = FONT_BASE (font) + boff;
19532 it->descent = FONT_DESCENT (font) - boff;
19533 }
19534
19535 if (pcm)
19536 {
19537 it->phys_ascent = pcm->ascent + boff;
19538 it->phys_descent = pcm->descent - boff;
19539 it->pixel_width = pcm->width;
19540 }
19541 else
19542 {
19543 it->glyph_not_available_p = 1;
19544 it->phys_ascent = it->ascent;
19545 it->phys_descent = it->descent;
19546 it->pixel_width = FONT_WIDTH (font);
19547 }
19548
19549 if (it->constrain_row_ascent_descent_p)
19550 {
19551 if (it->descent > it->max_descent)
19552 {
19553 it->ascent += it->descent - it->max_descent;
19554 it->descent = it->max_descent;
19555 }
19556 if (it->ascent > it->max_ascent)
19557 {
19558 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19559 it->ascent = it->max_ascent;
19560 }
19561 it->phys_ascent = min (it->phys_ascent, it->ascent);
19562 it->phys_descent = min (it->phys_descent, it->descent);
19563 extra_line_spacing = 0;
19564 }
19565
19566 /* If this is a space inside a region of text with
19567 `space-width' property, change its width. */
19568 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19569 if (stretched_p)
19570 it->pixel_width *= XFLOATINT (it->space_width);
19571
19572 /* If face has a box, add the box thickness to the character
19573 height. If character has a box line to the left and/or
19574 right, add the box line width to the character's width. */
19575 if (face->box != FACE_NO_BOX)
19576 {
19577 int thick = face->box_line_width;
19578
19579 if (thick > 0)
19580 {
19581 it->ascent += thick;
19582 it->descent += thick;
19583 }
19584 else
19585 thick = -thick;
19586
19587 if (it->start_of_box_run_p)
19588 it->pixel_width += thick;
19589 if (it->end_of_box_run_p)
19590 it->pixel_width += thick;
19591 }
19592
19593 /* If face has an overline, add the height of the overline
19594 (1 pixel) and a 1 pixel margin to the character height. */
19595 if (face->overline_p)
19596 it->ascent += 2;
19597
19598 if (it->constrain_row_ascent_descent_p)
19599 {
19600 if (it->ascent > it->max_ascent)
19601 it->ascent = it->max_ascent;
19602 if (it->descent > it->max_descent)
19603 it->descent = it->max_descent;
19604 }
19605
19606 take_vertical_position_into_account (it);
19607
19608 /* If we have to actually produce glyphs, do it. */
19609 if (it->glyph_row)
19610 {
19611 if (stretched_p)
19612 {
19613 /* Translate a space with a `space-width' property
19614 into a stretch glyph. */
19615 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19616 / FONT_HEIGHT (font));
19617 append_stretch_glyph (it, it->object, it->pixel_width,
19618 it->ascent + it->descent, ascent);
19619 }
19620 else
19621 append_glyph (it);
19622
19623 /* If characters with lbearing or rbearing are displayed
19624 in this line, record that fact in a flag of the
19625 glyph row. This is used to optimize X output code. */
19626 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19627 it->glyph_row->contains_overlapping_glyphs_p = 1;
19628 }
19629 }
19630 else if (it->char_to_display == '\n')
19631 {
19632 /* A newline has no width but we need the height of the line.
19633 But if previous part of the line set a height, don't
19634 increase that height */
19635
19636 Lisp_Object height;
19637 Lisp_Object total_height = Qnil;
19638
19639 it->override_ascent = -1;
19640 it->pixel_width = 0;
19641 it->nglyphs = 0;
19642
19643 height = get_line_height_property(it, Qline_height);
19644 /* Split (line-height total-height) list */
19645 if (CONSP (height)
19646 && CONSP (XCDR (height))
19647 && NILP (XCDR (XCDR (height))))
19648 {
19649 total_height = XCAR (XCDR (height));
19650 height = XCAR (height);
19651 }
19652 height = calc_line_height_property(it, height, font, boff, 1);
19653
19654 if (it->override_ascent >= 0)
19655 {
19656 it->ascent = it->override_ascent;
19657 it->descent = it->override_descent;
19658 boff = it->override_boff;
19659 }
19660 else
19661 {
19662 it->ascent = FONT_BASE (font) + boff;
19663 it->descent = FONT_DESCENT (font) - boff;
19664 }
19665
19666 if (EQ (height, Qt))
19667 {
19668 if (it->descent > it->max_descent)
19669 {
19670 it->ascent += it->descent - it->max_descent;
19671 it->descent = it->max_descent;
19672 }
19673 if (it->ascent > it->max_ascent)
19674 {
19675 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19676 it->ascent = it->max_ascent;
19677 }
19678 it->phys_ascent = min (it->phys_ascent, it->ascent);
19679 it->phys_descent = min (it->phys_descent, it->descent);
19680 it->constrain_row_ascent_descent_p = 1;
19681 extra_line_spacing = 0;
19682 }
19683 else
19684 {
19685 Lisp_Object spacing;
19686
19687 it->phys_ascent = it->ascent;
19688 it->phys_descent = it->descent;
19689
19690 if ((it->max_ascent > 0 || it->max_descent > 0)
19691 && face->box != FACE_NO_BOX
19692 && face->box_line_width > 0)
19693 {
19694 it->ascent += face->box_line_width;
19695 it->descent += face->box_line_width;
19696 }
19697 if (!NILP (height)
19698 && XINT (height) > it->ascent + it->descent)
19699 it->ascent = XINT (height) - it->descent;
19700
19701 if (!NILP (total_height))
19702 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19703 else
19704 {
19705 spacing = get_line_height_property(it, Qline_spacing);
19706 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19707 }
19708 if (INTEGERP (spacing))
19709 {
19710 extra_line_spacing = XINT (spacing);
19711 if (!NILP (total_height))
19712 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19713 }
19714 }
19715 }
19716 else if (it->char_to_display == '\t')
19717 {
19718 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19719 int x = it->current_x + it->continuation_lines_width;
19720 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19721
19722 /* If the distance from the current position to the next tab
19723 stop is less than a space character width, use the
19724 tab stop after that. */
19725 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19726 next_tab_x += tab_width;
19727
19728 it->pixel_width = next_tab_x - x;
19729 it->nglyphs = 1;
19730 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19731 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19732
19733 if (it->glyph_row)
19734 {
19735 append_stretch_glyph (it, it->object, it->pixel_width,
19736 it->ascent + it->descent, it->ascent);
19737 }
19738 }
19739 else
19740 {
19741 /* A multi-byte character. Assume that the display width of the
19742 character is the width of the character multiplied by the
19743 width of the font. */
19744
19745 /* If we found a font, this font should give us the right
19746 metrics. If we didn't find a font, use the frame's
19747 default font and calculate the width of the character by
19748 multiplying the width of font by the width of the
19749 character. */
19750
19751 pcm = rif->per_char_metric (font, &char2b,
19752 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19753
19754 if (font_not_found_p || !pcm)
19755 {
19756 it->glyph_not_available_p = 1;
19757 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19758 * CHAR_WIDTH (it->char_to_display));
19759 it->phys_ascent = FONT_BASE (font) + boff;
19760 it->phys_descent = FONT_DESCENT (font) - boff;
19761 }
19762 else
19763 {
19764 it->pixel_width = pcm->width;
19765 it->phys_ascent = pcm->ascent + boff;
19766 it->phys_descent = pcm->descent - boff;
19767 if (it->glyph_row
19768 && (pcm->lbearing < 0
19769 || pcm->rbearing > pcm->width))
19770 it->glyph_row->contains_overlapping_glyphs_p = 1;
19771 }
19772 it->nglyphs = 1;
19773 it->ascent = FONT_BASE (font) + boff;
19774 it->descent = FONT_DESCENT (font) - boff;
19775 if (face->box != FACE_NO_BOX)
19776 {
19777 int thick = face->box_line_width;
19778
19779 if (thick > 0)
19780 {
19781 it->ascent += thick;
19782 it->descent += thick;
19783 }
19784 else
19785 thick = - thick;
19786
19787 if (it->start_of_box_run_p)
19788 it->pixel_width += thick;
19789 if (it->end_of_box_run_p)
19790 it->pixel_width += thick;
19791 }
19792
19793 /* If face has an overline, add the height of the overline
19794 (1 pixel) and a 1 pixel margin to the character height. */
19795 if (face->overline_p)
19796 it->ascent += 2;
19797
19798 take_vertical_position_into_account (it);
19799
19800 if (it->glyph_row)
19801 append_glyph (it);
19802 }
19803 it->multibyte_p = saved_multibyte_p;
19804 }
19805 else if (it->what == IT_COMPOSITION)
19806 {
19807 /* Note: A composition is represented as one glyph in the
19808 glyph matrix. There are no padding glyphs. */
19809 XChar2b char2b;
19810 XFontStruct *font;
19811 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19812 XCharStruct *pcm;
19813 int font_not_found_p;
19814 struct font_info *font_info;
19815 int boff; /* baseline offset */
19816 struct composition *cmp = composition_table[it->cmp_id];
19817 int pos;
19818
19819 /* Maybe translate single-byte characters to multibyte. */
19820 it->char_to_display = it->c;
19821 if (unibyte_display_via_language_environment
19822 && it->c >= 0200)
19823 {
19824 it->char_to_display = unibyte_char_to_multibyte (it->c);
19825 }
19826
19827 /* Get face and font to use. Encode IT->char_to_display. */
19828 pos = STRINGP (it->string) ? IT_STRING_CHARPOS (*it) : IT_CHARPOS (*it);
19829 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
19830 pos, it->string);
19831 face = FACE_FROM_ID (it->f, it->face_id);
19832 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19833 &char2b, it->multibyte_p, 0);
19834 font = face->font;
19835
19836 /* When no suitable font found, use the default font. */
19837 font_not_found_p = font == NULL;
19838 if (font_not_found_p)
19839 {
19840 font = FRAME_FONT (it->f);
19841 boff = FRAME_BASELINE_OFFSET (it->f);
19842 font_info = NULL;
19843 }
19844 else
19845 {
19846 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19847 boff = font_info->baseline_offset;
19848 if (font_info->vertical_centering)
19849 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19850 }
19851
19852 /* There are no padding glyphs, so there is only one glyph to
19853 produce for the composition. Important is that pixel_width,
19854 ascent and descent are the values of what is drawn by
19855 draw_glyphs (i.e. the values of the overall glyphs composed). */
19856 it->nglyphs = 1;
19857
19858 /* If we have not yet calculated pixel size data of glyphs of
19859 the composition for the current face font, calculate them
19860 now. Theoretically, we have to check all fonts for the
19861 glyphs, but that requires much time and memory space. So,
19862 here we check only the font of the first glyph. This leads
19863 to incorrect display, but it's very rare, and C-l (recenter)
19864 can correct the display anyway. */
19865 if (cmp->glyph_len == 0)
19866 {
19867 cmp->lbearing = cmp->rbearing = 0;
19868 cmp->pixel_width = cmp->ascent = cmp->descent = 0;
19869 }
19870 else if (cmp->font != (void *) font)
19871 {
19872 /* Ascent and descent of the font of the first character of
19873 this composition (adjusted by baseline offset). Ascent
19874 and descent of overall glyphs should not be less than
19875 them respectively. */
19876 int font_ascent = FONT_BASE (font) + boff;
19877 int font_descent = FONT_DESCENT (font) - boff;
19878 int font_height = FONT_HEIGHT (font);
19879 /* Bounding box of the overall glyphs. */
19880 int leftmost, rightmost, lowest, highest;
19881 int lbearing, rbearing;
19882 int i, width, ascent, descent;
19883
19884 cmp->font = (void *) font;
19885
19886 /* Initialize the bounding box. */
19887 if (font_info
19888 && (pcm = rif->per_char_metric (font, &char2b,
19889 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19890 {
19891 width = pcm->width;
19892 ascent = pcm->ascent;
19893 descent = pcm->descent;
19894 lbearing = pcm->lbearing;
19895 if (lbearing > 0)
19896 lbearing = 0;
19897 rbearing = pcm->rbearing;
19898 if (rbearing < width)
19899 rbearing = width;
19900 }
19901 else
19902 {
19903 width = FONT_WIDTH (font);
19904 ascent = FONT_BASE (font);
19905 descent = FONT_DESCENT (font);
19906 lbearing = 0;
19907 rbearing = width;
19908 }
19909
19910 rightmost = width;
19911 lowest = - descent + boff;
19912 highest = ascent + boff;
19913 leftmost = 0;
19914
19915 if (font_info
19916 && font_info->default_ascent
19917 && CHAR_TABLE_P (Vuse_default_ascent)
19918 && !NILP (Faref (Vuse_default_ascent,
19919 make_number (it->char_to_display))))
19920 highest = font_info->default_ascent + boff;
19921
19922 /* Draw the first glyph at the normal position. It may be
19923 shifted to right later if some other glyphs are drawn at
19924 the left. */
19925 cmp->offsets[0] = 0;
19926 cmp->offsets[1] = boff;
19927 cmp->lbearing = lbearing;
19928 cmp->rbearing = rbearing;
19929
19930 /* Set cmp->offsets for the remaining glyphs. */
19931 for (i = 1; i < cmp->glyph_len; i++)
19932 {
19933 int left, right, btm, top;
19934 int ch = COMPOSITION_GLYPH (cmp, i);
19935 int face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
19936
19937 face = FACE_FROM_ID (it->f, face_id);
19938 get_char_face_and_encoding (it->f, ch, face->id,
19939 &char2b, it->multibyte_p, 0);
19940 font = face->font;
19941 if (font == NULL)
19942 {
19943 font = FRAME_FONT (it->f);
19944 boff = FRAME_BASELINE_OFFSET (it->f);
19945 font_info = NULL;
19946 }
19947 else
19948 {
19949 font_info
19950 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19951 boff = font_info->baseline_offset;
19952 if (font_info->vertical_centering)
19953 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19954 }
19955
19956 if (font_info
19957 && (pcm = rif->per_char_metric (font, &char2b,
19958 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19959 {
19960 width = pcm->width;
19961 ascent = pcm->ascent;
19962 descent = pcm->descent;
19963 lbearing = pcm->lbearing;
19964 if (lbearing > 0)
19965 lbearing = 0;
19966 rbearing = pcm->rbearing;
19967 if (rbearing < width)
19968 rbearing = width;
19969 }
19970 else
19971 {
19972 width = FONT_WIDTH (font);
19973 ascent = 1;
19974 descent = 0;
19975 lbearing = 0;
19976 rbearing = width;
19977 }
19978
19979 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19980 {
19981 /* Relative composition with or without
19982 alternate chars. */
19983 left = (leftmost + rightmost - width) / 2;
19984 btm = - descent + boff;
19985 if (font_info && font_info->relative_compose
19986 && (! CHAR_TABLE_P (Vignore_relative_composition)
19987 || NILP (Faref (Vignore_relative_composition,
19988 make_number (ch)))))
19989 {
19990
19991 if (- descent >= font_info->relative_compose)
19992 /* One extra pixel between two glyphs. */
19993 btm = highest + 1;
19994 else if (ascent <= 0)
19995 /* One extra pixel between two glyphs. */
19996 btm = lowest - 1 - ascent - descent;
19997 }
19998 }
19999 else
20000 {
20001 /* A composition rule is specified by an integer
20002 value that encodes global and new reference
20003 points (GREF and NREF). GREF and NREF are
20004 specified by numbers as below:
20005
20006 0---1---2 -- ascent
20007 | |
20008 | |
20009 | |
20010 9--10--11 -- center
20011 | |
20012 ---3---4---5--- baseline
20013 | |
20014 6---7---8 -- descent
20015 */
20016 int rule = COMPOSITION_RULE (cmp, i);
20017 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
20018
20019 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
20020 grefx = gref % 3, nrefx = nref % 3;
20021 grefy = gref / 3, nrefy = nref / 3;
20022 if (xoff)
20023 xoff = font_height * (xoff - 128) / 256;
20024 if (yoff)
20025 yoff = font_height * (yoff - 128) / 256;
20026
20027 left = (leftmost
20028 + grefx * (rightmost - leftmost) / 2
20029 - nrefx * width / 2
20030 + xoff);
20031
20032 btm = ((grefy == 0 ? highest
20033 : grefy == 1 ? 0
20034 : grefy == 2 ? lowest
20035 : (highest + lowest) / 2)
20036 - (nrefy == 0 ? ascent + descent
20037 : nrefy == 1 ? descent - boff
20038 : nrefy == 2 ? 0
20039 : (ascent + descent) / 2)
20040 + yoff);
20041 }
20042
20043 cmp->offsets[i * 2] = left;
20044 cmp->offsets[i * 2 + 1] = btm + descent;
20045
20046 /* Update the bounding box of the overall glyphs. */
20047 if (width > 0)
20048 {
20049 right = left + width;
20050 if (left < leftmost)
20051 leftmost = left;
20052 if (right > rightmost)
20053 rightmost = right;
20054 }
20055 top = btm + descent + ascent;
20056 if (top > highest)
20057 highest = top;
20058 if (btm < lowest)
20059 lowest = btm;
20060
20061 if (cmp->lbearing > left + lbearing)
20062 cmp->lbearing = left + lbearing;
20063 if (cmp->rbearing < left + rbearing)
20064 cmp->rbearing = left + rbearing;
20065 }
20066
20067 /* If there are glyphs whose x-offsets are negative,
20068 shift all glyphs to the right and make all x-offsets
20069 non-negative. */
20070 if (leftmost < 0)
20071 {
20072 for (i = 0; i < cmp->glyph_len; i++)
20073 cmp->offsets[i * 2] -= leftmost;
20074 rightmost -= leftmost;
20075 cmp->lbearing -= leftmost;
20076 cmp->rbearing -= leftmost;
20077 }
20078
20079 cmp->pixel_width = rightmost;
20080 cmp->ascent = highest;
20081 cmp->descent = - lowest;
20082 if (cmp->ascent < font_ascent)
20083 cmp->ascent = font_ascent;
20084 if (cmp->descent < font_descent)
20085 cmp->descent = font_descent;
20086 }
20087
20088 if (it->glyph_row
20089 && (cmp->lbearing < 0
20090 || cmp->rbearing > cmp->pixel_width))
20091 it->glyph_row->contains_overlapping_glyphs_p = 1;
20092
20093 it->pixel_width = cmp->pixel_width;
20094 it->ascent = it->phys_ascent = cmp->ascent;
20095 it->descent = it->phys_descent = cmp->descent;
20096
20097 if (face->box != FACE_NO_BOX)
20098 {
20099 int thick = face->box_line_width;
20100
20101 if (thick > 0)
20102 {
20103 it->ascent += thick;
20104 it->descent += thick;
20105 }
20106 else
20107 thick = - thick;
20108
20109 if (it->start_of_box_run_p)
20110 it->pixel_width += thick;
20111 if (it->end_of_box_run_p)
20112 it->pixel_width += thick;
20113 }
20114
20115 /* If face has an overline, add the height of the overline
20116 (1 pixel) and a 1 pixel margin to the character height. */
20117 if (face->overline_p)
20118 it->ascent += 2;
20119
20120 take_vertical_position_into_account (it);
20121
20122 if (it->glyph_row)
20123 append_composite_glyph (it);
20124 }
20125 else if (it->what == IT_IMAGE)
20126 produce_image_glyph (it);
20127 else if (it->what == IT_STRETCH)
20128 produce_stretch_glyph (it);
20129
20130 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20131 because this isn't true for images with `:ascent 100'. */
20132 xassert (it->ascent >= 0 && it->descent >= 0);
20133 if (it->area == TEXT_AREA)
20134 it->current_x += it->pixel_width;
20135
20136 if (extra_line_spacing > 0)
20137 {
20138 it->descent += extra_line_spacing;
20139 if (extra_line_spacing > it->max_extra_line_spacing)
20140 it->max_extra_line_spacing = extra_line_spacing;
20141 }
20142
20143 it->max_ascent = max (it->max_ascent, it->ascent);
20144 it->max_descent = max (it->max_descent, it->descent);
20145 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20146 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20147 }
20148
20149 /* EXPORT for RIF:
20150 Output LEN glyphs starting at START at the nominal cursor position.
20151 Advance the nominal cursor over the text. The global variable
20152 updated_window contains the window being updated, updated_row is
20153 the glyph row being updated, and updated_area is the area of that
20154 row being updated. */
20155
20156 void
20157 x_write_glyphs (start, len)
20158 struct glyph *start;
20159 int len;
20160 {
20161 int x, hpos;
20162
20163 xassert (updated_window && updated_row);
20164 BLOCK_INPUT;
20165
20166 /* Write glyphs. */
20167
20168 hpos = start - updated_row->glyphs[updated_area];
20169 x = draw_glyphs (updated_window, output_cursor.x,
20170 updated_row, updated_area,
20171 hpos, hpos + len,
20172 DRAW_NORMAL_TEXT, 0);
20173
20174 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20175 if (updated_area == TEXT_AREA
20176 && updated_window->phys_cursor_on_p
20177 && updated_window->phys_cursor.vpos == output_cursor.vpos
20178 && updated_window->phys_cursor.hpos >= hpos
20179 && updated_window->phys_cursor.hpos < hpos + len)
20180 updated_window->phys_cursor_on_p = 0;
20181
20182 UNBLOCK_INPUT;
20183
20184 /* Advance the output cursor. */
20185 output_cursor.hpos += len;
20186 output_cursor.x = x;
20187 }
20188
20189
20190 /* EXPORT for RIF:
20191 Insert LEN glyphs from START at the nominal cursor position. */
20192
20193 void
20194 x_insert_glyphs (start, len)
20195 struct glyph *start;
20196 int len;
20197 {
20198 struct frame *f;
20199 struct window *w;
20200 int line_height, shift_by_width, shifted_region_width;
20201 struct glyph_row *row;
20202 struct glyph *glyph;
20203 int frame_x, frame_y;
20204 EMACS_INT hpos;
20205
20206 xassert (updated_window && updated_row);
20207 BLOCK_INPUT;
20208 w = updated_window;
20209 f = XFRAME (WINDOW_FRAME (w));
20210
20211 /* Get the height of the line we are in. */
20212 row = updated_row;
20213 line_height = row->height;
20214
20215 /* Get the width of the glyphs to insert. */
20216 shift_by_width = 0;
20217 for (glyph = start; glyph < start + len; ++glyph)
20218 shift_by_width += glyph->pixel_width;
20219
20220 /* Get the width of the region to shift right. */
20221 shifted_region_width = (window_box_width (w, updated_area)
20222 - output_cursor.x
20223 - shift_by_width);
20224
20225 /* Shift right. */
20226 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20227 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20228
20229 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20230 line_height, shift_by_width);
20231
20232 /* Write the glyphs. */
20233 hpos = start - row->glyphs[updated_area];
20234 draw_glyphs (w, output_cursor.x, row, updated_area,
20235 hpos, hpos + len,
20236 DRAW_NORMAL_TEXT, 0);
20237
20238 /* Advance the output cursor. */
20239 output_cursor.hpos += len;
20240 output_cursor.x += shift_by_width;
20241 UNBLOCK_INPUT;
20242 }
20243
20244
20245 /* EXPORT for RIF:
20246 Erase the current text line from the nominal cursor position
20247 (inclusive) to pixel column TO_X (exclusive). The idea is that
20248 everything from TO_X onward is already erased.
20249
20250 TO_X is a pixel position relative to updated_area of
20251 updated_window. TO_X == -1 means clear to the end of this area. */
20252
20253 void
20254 x_clear_end_of_line (to_x)
20255 int to_x;
20256 {
20257 struct frame *f;
20258 struct window *w = updated_window;
20259 int max_x, min_y, max_y;
20260 int from_x, from_y, to_y;
20261
20262 xassert (updated_window && updated_row);
20263 f = XFRAME (w->frame);
20264
20265 if (updated_row->full_width_p)
20266 max_x = WINDOW_TOTAL_WIDTH (w);
20267 else
20268 max_x = window_box_width (w, updated_area);
20269 max_y = window_text_bottom_y (w);
20270
20271 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20272 of window. For TO_X > 0, truncate to end of drawing area. */
20273 if (to_x == 0)
20274 return;
20275 else if (to_x < 0)
20276 to_x = max_x;
20277 else
20278 to_x = min (to_x, max_x);
20279
20280 to_y = min (max_y, output_cursor.y + updated_row->height);
20281
20282 /* Notice if the cursor will be cleared by this operation. */
20283 if (!updated_row->full_width_p)
20284 notice_overwritten_cursor (w, updated_area,
20285 output_cursor.x, -1,
20286 updated_row->y,
20287 MATRIX_ROW_BOTTOM_Y (updated_row));
20288
20289 from_x = output_cursor.x;
20290
20291 /* Translate to frame coordinates. */
20292 if (updated_row->full_width_p)
20293 {
20294 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20295 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20296 }
20297 else
20298 {
20299 int area_left = window_box_left (w, updated_area);
20300 from_x += area_left;
20301 to_x += area_left;
20302 }
20303
20304 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20305 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20306 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20307
20308 /* Prevent inadvertently clearing to end of the X window. */
20309 if (to_x > from_x && to_y > from_y)
20310 {
20311 BLOCK_INPUT;
20312 rif->clear_frame_area (f, from_x, from_y,
20313 to_x - from_x, to_y - from_y);
20314 UNBLOCK_INPUT;
20315 }
20316 }
20317
20318 #endif /* HAVE_WINDOW_SYSTEM */
20319
20320
20321 \f
20322 /***********************************************************************
20323 Cursor types
20324 ***********************************************************************/
20325
20326 /* Value is the internal representation of the specified cursor type
20327 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20328 of the bar cursor. */
20329
20330 static enum text_cursor_kinds
20331 get_specified_cursor_type (arg, width)
20332 Lisp_Object arg;
20333 int *width;
20334 {
20335 enum text_cursor_kinds type;
20336
20337 if (NILP (arg))
20338 return NO_CURSOR;
20339
20340 if (EQ (arg, Qbox))
20341 return FILLED_BOX_CURSOR;
20342
20343 if (EQ (arg, Qhollow))
20344 return HOLLOW_BOX_CURSOR;
20345
20346 if (EQ (arg, Qbar))
20347 {
20348 *width = 2;
20349 return BAR_CURSOR;
20350 }
20351
20352 if (CONSP (arg)
20353 && EQ (XCAR (arg), Qbar)
20354 && INTEGERP (XCDR (arg))
20355 && XINT (XCDR (arg)) >= 0)
20356 {
20357 *width = XINT (XCDR (arg));
20358 return BAR_CURSOR;
20359 }
20360
20361 if (EQ (arg, Qhbar))
20362 {
20363 *width = 2;
20364 return HBAR_CURSOR;
20365 }
20366
20367 if (CONSP (arg)
20368 && EQ (XCAR (arg), Qhbar)
20369 && INTEGERP (XCDR (arg))
20370 && XINT (XCDR (arg)) >= 0)
20371 {
20372 *width = XINT (XCDR (arg));
20373 return HBAR_CURSOR;
20374 }
20375
20376 /* Treat anything unknown as "hollow box cursor".
20377 It was bad to signal an error; people have trouble fixing
20378 .Xdefaults with Emacs, when it has something bad in it. */
20379 type = HOLLOW_BOX_CURSOR;
20380
20381 return type;
20382 }
20383
20384 /* Set the default cursor types for specified frame. */
20385 void
20386 set_frame_cursor_types (f, arg)
20387 struct frame *f;
20388 Lisp_Object arg;
20389 {
20390 int width;
20391 Lisp_Object tem;
20392
20393 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20394 FRAME_CURSOR_WIDTH (f) = width;
20395
20396 /* By default, set up the blink-off state depending on the on-state. */
20397
20398 tem = Fassoc (arg, Vblink_cursor_alist);
20399 if (!NILP (tem))
20400 {
20401 FRAME_BLINK_OFF_CURSOR (f)
20402 = get_specified_cursor_type (XCDR (tem), &width);
20403 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20404 }
20405 else
20406 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20407 }
20408
20409
20410 /* Return the cursor we want to be displayed in window W. Return
20411 width of bar/hbar cursor through WIDTH arg. Return with
20412 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20413 (i.e. if the `system caret' should track this cursor).
20414
20415 In a mini-buffer window, we want the cursor only to appear if we
20416 are reading input from this window. For the selected window, we
20417 want the cursor type given by the frame parameter or buffer local
20418 setting of cursor-type. If explicitly marked off, draw no cursor.
20419 In all other cases, we want a hollow box cursor. */
20420
20421 static enum text_cursor_kinds
20422 get_window_cursor_type (w, glyph, width, active_cursor)
20423 struct window *w;
20424 struct glyph *glyph;
20425 int *width;
20426 int *active_cursor;
20427 {
20428 struct frame *f = XFRAME (w->frame);
20429 struct buffer *b = XBUFFER (w->buffer);
20430 int cursor_type = DEFAULT_CURSOR;
20431 Lisp_Object alt_cursor;
20432 int non_selected = 0;
20433
20434 *active_cursor = 1;
20435
20436 /* Echo area */
20437 if (cursor_in_echo_area
20438 && FRAME_HAS_MINIBUF_P (f)
20439 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20440 {
20441 if (w == XWINDOW (echo_area_window))
20442 {
20443 *width = FRAME_CURSOR_WIDTH (f);
20444 return FRAME_DESIRED_CURSOR (f);
20445 }
20446
20447 *active_cursor = 0;
20448 non_selected = 1;
20449 }
20450
20451 /* Nonselected window or nonselected frame. */
20452 else if (w != XWINDOW (f->selected_window)
20453 #ifdef HAVE_WINDOW_SYSTEM
20454 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20455 #endif
20456 )
20457 {
20458 *active_cursor = 0;
20459
20460 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20461 return NO_CURSOR;
20462
20463 non_selected = 1;
20464 }
20465
20466 /* Never display a cursor in a window in which cursor-type is nil. */
20467 if (NILP (b->cursor_type))
20468 return NO_CURSOR;
20469
20470 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20471 if (non_selected)
20472 {
20473 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20474 return get_specified_cursor_type (alt_cursor, width);
20475 }
20476
20477 /* Get the normal cursor type for this window. */
20478 if (EQ (b->cursor_type, Qt))
20479 {
20480 cursor_type = FRAME_DESIRED_CURSOR (f);
20481 *width = FRAME_CURSOR_WIDTH (f);
20482 }
20483 else
20484 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20485
20486 /* Use normal cursor if not blinked off. */
20487 if (!w->cursor_off_p)
20488 {
20489 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20490 if (cursor_type == FILLED_BOX_CURSOR)
20491 cursor_type = HOLLOW_BOX_CURSOR;
20492 }
20493 return cursor_type;
20494 }
20495
20496 /* Cursor is blinked off, so determine how to "toggle" it. */
20497
20498 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20499 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20500 return get_specified_cursor_type (XCDR (alt_cursor), width);
20501
20502 /* Then see if frame has specified a specific blink off cursor type. */
20503 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20504 {
20505 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20506 return FRAME_BLINK_OFF_CURSOR (f);
20507 }
20508
20509 #if 0
20510 /* Some people liked having a permanently visible blinking cursor,
20511 while others had very strong opinions against it. So it was
20512 decided to remove it. KFS 2003-09-03 */
20513
20514 /* Finally perform built-in cursor blinking:
20515 filled box <-> hollow box
20516 wide [h]bar <-> narrow [h]bar
20517 narrow [h]bar <-> no cursor
20518 other type <-> no cursor */
20519
20520 if (cursor_type == FILLED_BOX_CURSOR)
20521 return HOLLOW_BOX_CURSOR;
20522
20523 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20524 {
20525 *width = 1;
20526 return cursor_type;
20527 }
20528 #endif
20529
20530 return NO_CURSOR;
20531 }
20532
20533
20534 #ifdef HAVE_WINDOW_SYSTEM
20535
20536 /* Notice when the text cursor of window W has been completely
20537 overwritten by a drawing operation that outputs glyphs in AREA
20538 starting at X0 and ending at X1 in the line starting at Y0 and
20539 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20540 the rest of the line after X0 has been written. Y coordinates
20541 are window-relative. */
20542
20543 static void
20544 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20545 struct window *w;
20546 enum glyph_row_area area;
20547 int x0, y0, x1, y1;
20548 {
20549 int cx0, cx1, cy0, cy1;
20550 struct glyph_row *row;
20551
20552 if (!w->phys_cursor_on_p)
20553 return;
20554 if (area != TEXT_AREA)
20555 return;
20556
20557 if (w->phys_cursor.vpos < 0
20558 || w->phys_cursor.vpos >= w->current_matrix->nrows
20559 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20560 !(row->enabled_p && row->displays_text_p)))
20561 return;
20562
20563 if (row->cursor_in_fringe_p)
20564 {
20565 row->cursor_in_fringe_p = 0;
20566 draw_fringe_bitmap (w, row, 0);
20567 w->phys_cursor_on_p = 0;
20568 return;
20569 }
20570
20571 cx0 = w->phys_cursor.x;
20572 cx1 = cx0 + w->phys_cursor_width;
20573 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20574 return;
20575
20576 /* The cursor image will be completely removed from the
20577 screen if the output area intersects the cursor area in
20578 y-direction. When we draw in [y0 y1[, and some part of
20579 the cursor is at y < y0, that part must have been drawn
20580 before. When scrolling, the cursor is erased before
20581 actually scrolling, so we don't come here. When not
20582 scrolling, the rows above the old cursor row must have
20583 changed, and in this case these rows must have written
20584 over the cursor image.
20585
20586 Likewise if part of the cursor is below y1, with the
20587 exception of the cursor being in the first blank row at
20588 the buffer and window end because update_text_area
20589 doesn't draw that row. (Except when it does, but
20590 that's handled in update_text_area.) */
20591
20592 cy0 = w->phys_cursor.y;
20593 cy1 = cy0 + w->phys_cursor_height;
20594 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20595 return;
20596
20597 w->phys_cursor_on_p = 0;
20598 }
20599
20600 #endif /* HAVE_WINDOW_SYSTEM */
20601
20602 \f
20603 /************************************************************************
20604 Mouse Face
20605 ************************************************************************/
20606
20607 #ifdef HAVE_WINDOW_SYSTEM
20608
20609 /* EXPORT for RIF:
20610 Fix the display of area AREA of overlapping row ROW in window W. */
20611
20612 void
20613 x_fix_overlapping_area (w, row, area)
20614 struct window *w;
20615 struct glyph_row *row;
20616 enum glyph_row_area area;
20617 {
20618 int i, x;
20619
20620 BLOCK_INPUT;
20621
20622 x = 0;
20623 for (i = 0; i < row->used[area];)
20624 {
20625 if (row->glyphs[area][i].overlaps_vertically_p)
20626 {
20627 int start = i, start_x = x;
20628
20629 do
20630 {
20631 x += row->glyphs[area][i].pixel_width;
20632 ++i;
20633 }
20634 while (i < row->used[area]
20635 && row->glyphs[area][i].overlaps_vertically_p);
20636
20637 draw_glyphs (w, start_x, row, area,
20638 start, i,
20639 DRAW_NORMAL_TEXT, 1);
20640 }
20641 else
20642 {
20643 x += row->glyphs[area][i].pixel_width;
20644 ++i;
20645 }
20646 }
20647
20648 UNBLOCK_INPUT;
20649 }
20650
20651
20652 /* EXPORT:
20653 Draw the cursor glyph of window W in glyph row ROW. See the
20654 comment of draw_glyphs for the meaning of HL. */
20655
20656 void
20657 draw_phys_cursor_glyph (w, row, hl)
20658 struct window *w;
20659 struct glyph_row *row;
20660 enum draw_glyphs_face hl;
20661 {
20662 /* If cursor hpos is out of bounds, don't draw garbage. This can
20663 happen in mini-buffer windows when switching between echo area
20664 glyphs and mini-buffer. */
20665 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20666 {
20667 int on_p = w->phys_cursor_on_p;
20668 int x1;
20669 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20670 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20671 hl, 0);
20672 w->phys_cursor_on_p = on_p;
20673
20674 if (hl == DRAW_CURSOR)
20675 w->phys_cursor_width = x1 - w->phys_cursor.x;
20676 /* When we erase the cursor, and ROW is overlapped by other
20677 rows, make sure that these overlapping parts of other rows
20678 are redrawn. */
20679 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20680 {
20681 if (row > w->current_matrix->rows
20682 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20683 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20684
20685 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20686 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20687 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20688 }
20689 }
20690 }
20691
20692
20693 /* EXPORT:
20694 Erase the image of a cursor of window W from the screen. */
20695
20696 void
20697 erase_phys_cursor (w)
20698 struct window *w;
20699 {
20700 struct frame *f = XFRAME (w->frame);
20701 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20702 int hpos = w->phys_cursor.hpos;
20703 int vpos = w->phys_cursor.vpos;
20704 int mouse_face_here_p = 0;
20705 struct glyph_matrix *active_glyphs = w->current_matrix;
20706 struct glyph_row *cursor_row;
20707 struct glyph *cursor_glyph;
20708 enum draw_glyphs_face hl;
20709
20710 /* No cursor displayed or row invalidated => nothing to do on the
20711 screen. */
20712 if (w->phys_cursor_type == NO_CURSOR)
20713 goto mark_cursor_off;
20714
20715 /* VPOS >= active_glyphs->nrows means that window has been resized.
20716 Don't bother to erase the cursor. */
20717 if (vpos >= active_glyphs->nrows)
20718 goto mark_cursor_off;
20719
20720 /* If row containing cursor is marked invalid, there is nothing we
20721 can do. */
20722 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20723 if (!cursor_row->enabled_p)
20724 goto mark_cursor_off;
20725
20726 /* If line spacing is > 0, old cursor may only be partially visible in
20727 window after split-window. So adjust visible height. */
20728 cursor_row->visible_height = min (cursor_row->visible_height,
20729 window_text_bottom_y (w) - cursor_row->y);
20730
20731 /* If row is completely invisible, don't attempt to delete a cursor which
20732 isn't there. This can happen if cursor is at top of a window, and
20733 we switch to a buffer with a header line in that window. */
20734 if (cursor_row->visible_height <= 0)
20735 goto mark_cursor_off;
20736
20737 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20738 if (cursor_row->cursor_in_fringe_p)
20739 {
20740 cursor_row->cursor_in_fringe_p = 0;
20741 draw_fringe_bitmap (w, cursor_row, 0);
20742 goto mark_cursor_off;
20743 }
20744
20745 /* This can happen when the new row is shorter than the old one.
20746 In this case, either draw_glyphs or clear_end_of_line
20747 should have cleared the cursor. Note that we wouldn't be
20748 able to erase the cursor in this case because we don't have a
20749 cursor glyph at hand. */
20750 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20751 goto mark_cursor_off;
20752
20753 /* If the cursor is in the mouse face area, redisplay that when
20754 we clear the cursor. */
20755 if (! NILP (dpyinfo->mouse_face_window)
20756 && w == XWINDOW (dpyinfo->mouse_face_window)
20757 && (vpos > dpyinfo->mouse_face_beg_row
20758 || (vpos == dpyinfo->mouse_face_beg_row
20759 && hpos >= dpyinfo->mouse_face_beg_col))
20760 && (vpos < dpyinfo->mouse_face_end_row
20761 || (vpos == dpyinfo->mouse_face_end_row
20762 && hpos < dpyinfo->mouse_face_end_col))
20763 /* Don't redraw the cursor's spot in mouse face if it is at the
20764 end of a line (on a newline). The cursor appears there, but
20765 mouse highlighting does not. */
20766 && cursor_row->used[TEXT_AREA] > hpos)
20767 mouse_face_here_p = 1;
20768
20769 /* Maybe clear the display under the cursor. */
20770 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20771 {
20772 int x, y;
20773 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20774 int width;
20775
20776 cursor_glyph = get_phys_cursor_glyph (w);
20777 if (cursor_glyph == NULL)
20778 goto mark_cursor_off;
20779
20780 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20781 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20782 width = min (cursor_glyph->pixel_width,
20783 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20784
20785 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20786 }
20787
20788 /* Erase the cursor by redrawing the character underneath it. */
20789 if (mouse_face_here_p)
20790 hl = DRAW_MOUSE_FACE;
20791 else
20792 hl = DRAW_NORMAL_TEXT;
20793 draw_phys_cursor_glyph (w, cursor_row, hl);
20794
20795 mark_cursor_off:
20796 w->phys_cursor_on_p = 0;
20797 w->phys_cursor_type = NO_CURSOR;
20798 }
20799
20800
20801 /* EXPORT:
20802 Display or clear cursor of window W. If ON is zero, clear the
20803 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20804 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20805
20806 void
20807 display_and_set_cursor (w, on, hpos, vpos, x, y)
20808 struct window *w;
20809 int on, hpos, vpos, x, y;
20810 {
20811 struct frame *f = XFRAME (w->frame);
20812 int new_cursor_type;
20813 int new_cursor_width;
20814 int active_cursor;
20815 struct glyph_row *glyph_row;
20816 struct glyph *glyph;
20817
20818 /* This is pointless on invisible frames, and dangerous on garbaged
20819 windows and frames; in the latter case, the frame or window may
20820 be in the midst of changing its size, and x and y may be off the
20821 window. */
20822 if (! FRAME_VISIBLE_P (f)
20823 || FRAME_GARBAGED_P (f)
20824 || vpos >= w->current_matrix->nrows
20825 || hpos >= w->current_matrix->matrix_w)
20826 return;
20827
20828 /* If cursor is off and we want it off, return quickly. */
20829 if (!on && !w->phys_cursor_on_p)
20830 return;
20831
20832 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20833 /* If cursor row is not enabled, we don't really know where to
20834 display the cursor. */
20835 if (!glyph_row->enabled_p)
20836 {
20837 w->phys_cursor_on_p = 0;
20838 return;
20839 }
20840
20841 glyph = NULL;
20842 if (!glyph_row->exact_window_width_line_p
20843 || hpos < glyph_row->used[TEXT_AREA])
20844 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20845
20846 xassert (interrupt_input_blocked);
20847
20848 /* Set new_cursor_type to the cursor we want to be displayed. */
20849 new_cursor_type = get_window_cursor_type (w, glyph,
20850 &new_cursor_width, &active_cursor);
20851
20852 /* If cursor is currently being shown and we don't want it to be or
20853 it is in the wrong place, or the cursor type is not what we want,
20854 erase it. */
20855 if (w->phys_cursor_on_p
20856 && (!on
20857 || w->phys_cursor.x != x
20858 || w->phys_cursor.y != y
20859 || new_cursor_type != w->phys_cursor_type
20860 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20861 && new_cursor_width != w->phys_cursor_width)))
20862 erase_phys_cursor (w);
20863
20864 /* Don't check phys_cursor_on_p here because that flag is only set
20865 to zero in some cases where we know that the cursor has been
20866 completely erased, to avoid the extra work of erasing the cursor
20867 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20868 still not be visible, or it has only been partly erased. */
20869 if (on)
20870 {
20871 w->phys_cursor_ascent = glyph_row->ascent;
20872 w->phys_cursor_height = glyph_row->height;
20873
20874 /* Set phys_cursor_.* before x_draw_.* is called because some
20875 of them may need the information. */
20876 w->phys_cursor.x = x;
20877 w->phys_cursor.y = glyph_row->y;
20878 w->phys_cursor.hpos = hpos;
20879 w->phys_cursor.vpos = vpos;
20880 }
20881
20882 rif->draw_window_cursor (w, glyph_row, x, y,
20883 new_cursor_type, new_cursor_width,
20884 on, active_cursor);
20885 }
20886
20887
20888 /* Switch the display of W's cursor on or off, according to the value
20889 of ON. */
20890
20891 static void
20892 update_window_cursor (w, on)
20893 struct window *w;
20894 int on;
20895 {
20896 /* Don't update cursor in windows whose frame is in the process
20897 of being deleted. */
20898 if (w->current_matrix)
20899 {
20900 BLOCK_INPUT;
20901 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20902 w->phys_cursor.x, w->phys_cursor.y);
20903 UNBLOCK_INPUT;
20904 }
20905 }
20906
20907
20908 /* Call update_window_cursor with parameter ON_P on all leaf windows
20909 in the window tree rooted at W. */
20910
20911 static void
20912 update_cursor_in_window_tree (w, on_p)
20913 struct window *w;
20914 int on_p;
20915 {
20916 while (w)
20917 {
20918 if (!NILP (w->hchild))
20919 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20920 else if (!NILP (w->vchild))
20921 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20922 else
20923 update_window_cursor (w, on_p);
20924
20925 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20926 }
20927 }
20928
20929
20930 /* EXPORT:
20931 Display the cursor on window W, or clear it, according to ON_P.
20932 Don't change the cursor's position. */
20933
20934 void
20935 x_update_cursor (f, on_p)
20936 struct frame *f;
20937 int on_p;
20938 {
20939 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20940 }
20941
20942
20943 /* EXPORT:
20944 Clear the cursor of window W to background color, and mark the
20945 cursor as not shown. This is used when the text where the cursor
20946 is is about to be rewritten. */
20947
20948 void
20949 x_clear_cursor (w)
20950 struct window *w;
20951 {
20952 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20953 update_window_cursor (w, 0);
20954 }
20955
20956
20957 /* EXPORT:
20958 Display the active region described by mouse_face_* according to DRAW. */
20959
20960 void
20961 show_mouse_face (dpyinfo, draw)
20962 Display_Info *dpyinfo;
20963 enum draw_glyphs_face draw;
20964 {
20965 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20966 struct frame *f = XFRAME (WINDOW_FRAME (w));
20967
20968 if (/* If window is in the process of being destroyed, don't bother
20969 to do anything. */
20970 w->current_matrix != NULL
20971 /* Don't update mouse highlight if hidden */
20972 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20973 /* Recognize when we are called to operate on rows that don't exist
20974 anymore. This can happen when a window is split. */
20975 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20976 {
20977 int phys_cursor_on_p = w->phys_cursor_on_p;
20978 struct glyph_row *row, *first, *last;
20979
20980 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20981 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20982
20983 for (row = first; row <= last && row->enabled_p; ++row)
20984 {
20985 int start_hpos, end_hpos, start_x;
20986
20987 /* For all but the first row, the highlight starts at column 0. */
20988 if (row == first)
20989 {
20990 start_hpos = dpyinfo->mouse_face_beg_col;
20991 start_x = dpyinfo->mouse_face_beg_x;
20992 }
20993 else
20994 {
20995 start_hpos = 0;
20996 start_x = 0;
20997 }
20998
20999 if (row == last)
21000 end_hpos = dpyinfo->mouse_face_end_col;
21001 else
21002 end_hpos = row->used[TEXT_AREA];
21003
21004 if (end_hpos > start_hpos)
21005 {
21006 draw_glyphs (w, start_x, row, TEXT_AREA,
21007 start_hpos, end_hpos,
21008 draw, 0);
21009
21010 row->mouse_face_p
21011 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21012 }
21013 }
21014
21015 /* When we've written over the cursor, arrange for it to
21016 be displayed again. */
21017 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21018 {
21019 BLOCK_INPUT;
21020 display_and_set_cursor (w, 1,
21021 w->phys_cursor.hpos, w->phys_cursor.vpos,
21022 w->phys_cursor.x, w->phys_cursor.y);
21023 UNBLOCK_INPUT;
21024 }
21025 }
21026
21027 /* Change the mouse cursor. */
21028 if (draw == DRAW_NORMAL_TEXT)
21029 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21030 else if (draw == DRAW_MOUSE_FACE)
21031 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21032 else
21033 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21034 }
21035
21036 /* EXPORT:
21037 Clear out the mouse-highlighted active region.
21038 Redraw it un-highlighted first. Value is non-zero if mouse
21039 face was actually drawn unhighlighted. */
21040
21041 int
21042 clear_mouse_face (dpyinfo)
21043 Display_Info *dpyinfo;
21044 {
21045 int cleared = 0;
21046
21047 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21048 {
21049 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21050 cleared = 1;
21051 }
21052
21053 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21054 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21055 dpyinfo->mouse_face_window = Qnil;
21056 dpyinfo->mouse_face_overlay = Qnil;
21057 return cleared;
21058 }
21059
21060
21061 /* EXPORT:
21062 Non-zero if physical cursor of window W is within mouse face. */
21063
21064 int
21065 cursor_in_mouse_face_p (w)
21066 struct window *w;
21067 {
21068 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21069 int in_mouse_face = 0;
21070
21071 if (WINDOWP (dpyinfo->mouse_face_window)
21072 && XWINDOW (dpyinfo->mouse_face_window) == w)
21073 {
21074 int hpos = w->phys_cursor.hpos;
21075 int vpos = w->phys_cursor.vpos;
21076
21077 if (vpos >= dpyinfo->mouse_face_beg_row
21078 && vpos <= dpyinfo->mouse_face_end_row
21079 && (vpos > dpyinfo->mouse_face_beg_row
21080 || hpos >= dpyinfo->mouse_face_beg_col)
21081 && (vpos < dpyinfo->mouse_face_end_row
21082 || hpos < dpyinfo->mouse_face_end_col
21083 || dpyinfo->mouse_face_past_end))
21084 in_mouse_face = 1;
21085 }
21086
21087 return in_mouse_face;
21088 }
21089
21090
21091
21092 \f
21093 /* Find the glyph matrix position of buffer position CHARPOS in window
21094 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21095 current glyphs must be up to date. If CHARPOS is above window
21096 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21097 of last line in W. In the row containing CHARPOS, stop before glyphs
21098 having STOP as object. */
21099
21100 #if 1 /* This is a version of fast_find_position that's more correct
21101 in the presence of hscrolling, for example. I didn't install
21102 it right away because the problem fixed is minor, it failed
21103 in 20.x as well, and I think it's too risky to install
21104 so near the release of 21.1. 2001-09-25 gerd. */
21105
21106 static int
21107 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21108 struct window *w;
21109 EMACS_INT charpos;
21110 int *hpos, *vpos, *x, *y;
21111 Lisp_Object stop;
21112 {
21113 struct glyph_row *row, *first;
21114 struct glyph *glyph, *end;
21115 int past_end = 0;
21116
21117 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21118 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21119 {
21120 *x = first->x;
21121 *y = first->y;
21122 *hpos = 0;
21123 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21124 return 1;
21125 }
21126
21127 row = row_containing_pos (w, charpos, first, NULL, 0);
21128 if (row == NULL)
21129 {
21130 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21131 past_end = 1;
21132 }
21133
21134 /* If whole rows or last part of a row came from a display overlay,
21135 row_containing_pos will skip over such rows because their end pos
21136 equals the start pos of the overlay or interval.
21137
21138 Move back if we have a STOP object and previous row's
21139 end glyph came from STOP. */
21140 if (!NILP (stop))
21141 {
21142 struct glyph_row *prev;
21143 while ((prev = row - 1, prev >= first)
21144 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21145 && prev->used[TEXT_AREA] > 0)
21146 {
21147 struct glyph *beg = prev->glyphs[TEXT_AREA];
21148 glyph = beg + prev->used[TEXT_AREA];
21149 while (--glyph >= beg
21150 && INTEGERP (glyph->object));
21151 if (glyph < beg
21152 || !EQ (stop, glyph->object))
21153 break;
21154 row = prev;
21155 }
21156 }
21157
21158 *x = row->x;
21159 *y = row->y;
21160 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21161
21162 glyph = row->glyphs[TEXT_AREA];
21163 end = glyph + row->used[TEXT_AREA];
21164
21165 /* Skip over glyphs not having an object at the start of the row.
21166 These are special glyphs like truncation marks on terminal
21167 frames. */
21168 if (row->displays_text_p)
21169 while (glyph < end
21170 && INTEGERP (glyph->object)
21171 && !EQ (stop, glyph->object)
21172 && glyph->charpos < 0)
21173 {
21174 *x += glyph->pixel_width;
21175 ++glyph;
21176 }
21177
21178 while (glyph < end
21179 && !INTEGERP (glyph->object)
21180 && !EQ (stop, glyph->object)
21181 && (!BUFFERP (glyph->object)
21182 || glyph->charpos < charpos))
21183 {
21184 *x += glyph->pixel_width;
21185 ++glyph;
21186 }
21187
21188 *hpos = glyph - row->glyphs[TEXT_AREA];
21189 return !past_end;
21190 }
21191
21192 #else /* not 1 */
21193
21194 static int
21195 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21196 struct window *w;
21197 EMACS_INT pos;
21198 int *hpos, *vpos, *x, *y;
21199 Lisp_Object stop;
21200 {
21201 int i;
21202 int lastcol;
21203 int maybe_next_line_p = 0;
21204 int line_start_position;
21205 int yb = window_text_bottom_y (w);
21206 struct glyph_row *row, *best_row;
21207 int row_vpos, best_row_vpos;
21208 int current_x;
21209
21210 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21211 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21212
21213 while (row->y < yb)
21214 {
21215 if (row->used[TEXT_AREA])
21216 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21217 else
21218 line_start_position = 0;
21219
21220 if (line_start_position > pos)
21221 break;
21222 /* If the position sought is the end of the buffer,
21223 don't include the blank lines at the bottom of the window. */
21224 else if (line_start_position == pos
21225 && pos == BUF_ZV (XBUFFER (w->buffer)))
21226 {
21227 maybe_next_line_p = 1;
21228 break;
21229 }
21230 else if (line_start_position > 0)
21231 {
21232 best_row = row;
21233 best_row_vpos = row_vpos;
21234 }
21235
21236 if (row->y + row->height >= yb)
21237 break;
21238
21239 ++row;
21240 ++row_vpos;
21241 }
21242
21243 /* Find the right column within BEST_ROW. */
21244 lastcol = 0;
21245 current_x = best_row->x;
21246 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21247 {
21248 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21249 int charpos = glyph->charpos;
21250
21251 if (BUFFERP (glyph->object))
21252 {
21253 if (charpos == pos)
21254 {
21255 *hpos = i;
21256 *vpos = best_row_vpos;
21257 *x = current_x;
21258 *y = best_row->y;
21259 return 1;
21260 }
21261 else if (charpos > pos)
21262 break;
21263 }
21264 else if (EQ (glyph->object, stop))
21265 break;
21266
21267 if (charpos > 0)
21268 lastcol = i;
21269 current_x += glyph->pixel_width;
21270 }
21271
21272 /* If we're looking for the end of the buffer,
21273 and we didn't find it in the line we scanned,
21274 use the start of the following line. */
21275 if (maybe_next_line_p)
21276 {
21277 ++best_row;
21278 ++best_row_vpos;
21279 lastcol = 0;
21280 current_x = best_row->x;
21281 }
21282
21283 *vpos = best_row_vpos;
21284 *hpos = lastcol + 1;
21285 *x = current_x;
21286 *y = best_row->y;
21287 return 0;
21288 }
21289
21290 #endif /* not 1 */
21291
21292
21293 /* Find the position of the glyph for position POS in OBJECT in
21294 window W's current matrix, and return in *X, *Y the pixel
21295 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21296
21297 RIGHT_P non-zero means return the position of the right edge of the
21298 glyph, RIGHT_P zero means return the left edge position.
21299
21300 If no glyph for POS exists in the matrix, return the position of
21301 the glyph with the next smaller position that is in the matrix, if
21302 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21303 exists in the matrix, return the position of the glyph with the
21304 next larger position in OBJECT.
21305
21306 Value is non-zero if a glyph was found. */
21307
21308 static int
21309 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21310 struct window *w;
21311 EMACS_INT pos;
21312 Lisp_Object object;
21313 int *hpos, *vpos, *x, *y;
21314 int right_p;
21315 {
21316 int yb = window_text_bottom_y (w);
21317 struct glyph_row *r;
21318 struct glyph *best_glyph = NULL;
21319 struct glyph_row *best_row = NULL;
21320 int best_x = 0;
21321
21322 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21323 r->enabled_p && r->y < yb;
21324 ++r)
21325 {
21326 struct glyph *g = r->glyphs[TEXT_AREA];
21327 struct glyph *e = g + r->used[TEXT_AREA];
21328 int gx;
21329
21330 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21331 if (EQ (g->object, object))
21332 {
21333 if (g->charpos == pos)
21334 {
21335 best_glyph = g;
21336 best_x = gx;
21337 best_row = r;
21338 goto found;
21339 }
21340 else if (best_glyph == NULL
21341 || ((abs (g->charpos - pos)
21342 < abs (best_glyph->charpos - pos))
21343 && (right_p
21344 ? g->charpos < pos
21345 : g->charpos > pos)))
21346 {
21347 best_glyph = g;
21348 best_x = gx;
21349 best_row = r;
21350 }
21351 }
21352 }
21353
21354 found:
21355
21356 if (best_glyph)
21357 {
21358 *x = best_x;
21359 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21360
21361 if (right_p)
21362 {
21363 *x += best_glyph->pixel_width;
21364 ++*hpos;
21365 }
21366
21367 *y = best_row->y;
21368 *vpos = best_row - w->current_matrix->rows;
21369 }
21370
21371 return best_glyph != NULL;
21372 }
21373
21374
21375 /* See if position X, Y is within a hot-spot of an image. */
21376
21377 static int
21378 on_hot_spot_p (hot_spot, x, y)
21379 Lisp_Object hot_spot;
21380 int x, y;
21381 {
21382 if (!CONSP (hot_spot))
21383 return 0;
21384
21385 if (EQ (XCAR (hot_spot), Qrect))
21386 {
21387 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21388 Lisp_Object rect = XCDR (hot_spot);
21389 Lisp_Object tem;
21390 if (!CONSP (rect))
21391 return 0;
21392 if (!CONSP (XCAR (rect)))
21393 return 0;
21394 if (!CONSP (XCDR (rect)))
21395 return 0;
21396 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21397 return 0;
21398 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21399 return 0;
21400 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21401 return 0;
21402 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21403 return 0;
21404 return 1;
21405 }
21406 else if (EQ (XCAR (hot_spot), Qcircle))
21407 {
21408 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21409 Lisp_Object circ = XCDR (hot_spot);
21410 Lisp_Object lr, lx0, ly0;
21411 if (CONSP (circ)
21412 && CONSP (XCAR (circ))
21413 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21414 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21415 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21416 {
21417 double r = XFLOATINT (lr);
21418 double dx = XINT (lx0) - x;
21419 double dy = XINT (ly0) - y;
21420 return (dx * dx + dy * dy <= r * r);
21421 }
21422 }
21423 else if (EQ (XCAR (hot_spot), Qpoly))
21424 {
21425 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21426 if (VECTORP (XCDR (hot_spot)))
21427 {
21428 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21429 Lisp_Object *poly = v->contents;
21430 int n = v->size;
21431 int i;
21432 int inside = 0;
21433 Lisp_Object lx, ly;
21434 int x0, y0;
21435
21436 /* Need an even number of coordinates, and at least 3 edges. */
21437 if (n < 6 || n & 1)
21438 return 0;
21439
21440 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21441 If count is odd, we are inside polygon. Pixels on edges
21442 may or may not be included depending on actual geometry of the
21443 polygon. */
21444 if ((lx = poly[n-2], !INTEGERP (lx))
21445 || (ly = poly[n-1], !INTEGERP (lx)))
21446 return 0;
21447 x0 = XINT (lx), y0 = XINT (ly);
21448 for (i = 0; i < n; i += 2)
21449 {
21450 int x1 = x0, y1 = y0;
21451 if ((lx = poly[i], !INTEGERP (lx))
21452 || (ly = poly[i+1], !INTEGERP (ly)))
21453 return 0;
21454 x0 = XINT (lx), y0 = XINT (ly);
21455
21456 /* Does this segment cross the X line? */
21457 if (x0 >= x)
21458 {
21459 if (x1 >= x)
21460 continue;
21461 }
21462 else if (x1 < x)
21463 continue;
21464 if (y > y0 && y > y1)
21465 continue;
21466 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21467 inside = !inside;
21468 }
21469 return inside;
21470 }
21471 }
21472 /* If we don't understand the format, pretend we're not in the hot-spot. */
21473 return 0;
21474 }
21475
21476 Lisp_Object
21477 find_hot_spot (map, x, y)
21478 Lisp_Object map;
21479 int x, y;
21480 {
21481 while (CONSP (map))
21482 {
21483 if (CONSP (XCAR (map))
21484 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21485 return XCAR (map);
21486 map = XCDR (map);
21487 }
21488
21489 return Qnil;
21490 }
21491
21492 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21493 3, 3, 0,
21494 doc: /* Lookup in image map MAP coordinates X and Y.
21495 An image map is an alist where each element has the format (AREA ID PLIST).
21496 An AREA is specified as either a rectangle, a circle, or a polygon:
21497 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21498 pixel coordinates of the upper left and bottom right corners.
21499 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21500 and the radius of the circle; r may be a float or integer.
21501 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21502 vector describes one corner in the polygon.
21503 Returns the alist element for the first matching AREA in MAP. */)
21504 (map, x, y)
21505 Lisp_Object map;
21506 Lisp_Object x, y;
21507 {
21508 if (NILP (map))
21509 return Qnil;
21510
21511 CHECK_NUMBER (x);
21512 CHECK_NUMBER (y);
21513
21514 return find_hot_spot (map, XINT (x), XINT (y));
21515 }
21516
21517
21518 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21519 static void
21520 define_frame_cursor1 (f, cursor, pointer)
21521 struct frame *f;
21522 Cursor cursor;
21523 Lisp_Object pointer;
21524 {
21525 /* Do not change cursor shape while dragging mouse. */
21526 if (!NILP (do_mouse_tracking))
21527 return;
21528
21529 if (!NILP (pointer))
21530 {
21531 if (EQ (pointer, Qarrow))
21532 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21533 else if (EQ (pointer, Qhand))
21534 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21535 else if (EQ (pointer, Qtext))
21536 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21537 else if (EQ (pointer, intern ("hdrag")))
21538 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21539 #ifdef HAVE_X_WINDOWS
21540 else if (EQ (pointer, intern ("vdrag")))
21541 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21542 #endif
21543 else if (EQ (pointer, intern ("hourglass")))
21544 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21545 else if (EQ (pointer, Qmodeline))
21546 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21547 else
21548 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21549 }
21550
21551 if (cursor != No_Cursor)
21552 rif->define_frame_cursor (f, cursor);
21553 }
21554
21555 /* Take proper action when mouse has moved to the mode or header line
21556 or marginal area AREA of window W, x-position X and y-position Y.
21557 X is relative to the start of the text display area of W, so the
21558 width of bitmap areas and scroll bars must be subtracted to get a
21559 position relative to the start of the mode line. */
21560
21561 static void
21562 note_mode_line_or_margin_highlight (window, x, y, area)
21563 Lisp_Object window;
21564 int x, y;
21565 enum window_part area;
21566 {
21567 struct window *w = XWINDOW (window);
21568 struct frame *f = XFRAME (w->frame);
21569 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21570 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21571 Lisp_Object pointer = Qnil;
21572 int charpos, dx, dy, width, height;
21573 Lisp_Object string, object = Qnil;
21574 Lisp_Object pos, help;
21575
21576 Lisp_Object mouse_face;
21577 int original_x_pixel = x;
21578 struct glyph * glyph = NULL;
21579 struct glyph_row *row;
21580
21581 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21582 {
21583 int x0;
21584 struct glyph *end;
21585
21586 string = mode_line_string (w, area, &x, &y, &charpos,
21587 &object, &dx, &dy, &width, &height);
21588
21589 row = (area == ON_MODE_LINE
21590 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21591 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21592
21593 /* Find glyph */
21594 if (row->mode_line_p && row->enabled_p)
21595 {
21596 glyph = row->glyphs[TEXT_AREA];
21597 end = glyph + row->used[TEXT_AREA];
21598
21599 for (x0 = original_x_pixel;
21600 glyph < end && x0 >= glyph->pixel_width;
21601 ++glyph)
21602 x0 -= glyph->pixel_width;
21603
21604 if (glyph >= end)
21605 glyph = NULL;
21606 }
21607 }
21608 else
21609 {
21610 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21611 string = marginal_area_string (w, area, &x, &y, &charpos,
21612 &object, &dx, &dy, &width, &height);
21613 }
21614
21615 help = Qnil;
21616
21617 if (IMAGEP (object))
21618 {
21619 Lisp_Object image_map, hotspot;
21620 if ((image_map = Fplist_get (XCDR (object), QCmap),
21621 !NILP (image_map))
21622 && (hotspot = find_hot_spot (image_map, dx, dy),
21623 CONSP (hotspot))
21624 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21625 {
21626 Lisp_Object area_id, plist;
21627
21628 area_id = XCAR (hotspot);
21629 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21630 If so, we could look for mouse-enter, mouse-leave
21631 properties in PLIST (and do something...). */
21632 hotspot = XCDR (hotspot);
21633 if (CONSP (hotspot)
21634 && (plist = XCAR (hotspot), CONSP (plist)))
21635 {
21636 pointer = Fplist_get (plist, Qpointer);
21637 if (NILP (pointer))
21638 pointer = Qhand;
21639 help = Fplist_get (plist, Qhelp_echo);
21640 if (!NILP (help))
21641 {
21642 help_echo_string = help;
21643 /* Is this correct? ++kfs */
21644 XSETWINDOW (help_echo_window, w);
21645 help_echo_object = w->buffer;
21646 help_echo_pos = charpos;
21647 }
21648 }
21649 }
21650 if (NILP (pointer))
21651 pointer = Fplist_get (XCDR (object), QCpointer);
21652 }
21653
21654 if (STRINGP (string))
21655 {
21656 pos = make_number (charpos);
21657 /* If we're on a string with `help-echo' text property, arrange
21658 for the help to be displayed. This is done by setting the
21659 global variable help_echo_string to the help string. */
21660 if (NILP (help))
21661 {
21662 help = Fget_text_property (pos, Qhelp_echo, string);
21663 if (!NILP (help))
21664 {
21665 help_echo_string = help;
21666 XSETWINDOW (help_echo_window, w);
21667 help_echo_object = string;
21668 help_echo_pos = charpos;
21669 }
21670 }
21671
21672 if (NILP (pointer))
21673 pointer = Fget_text_property (pos, Qpointer, string);
21674
21675 /* Change the mouse pointer according to what is under X/Y. */
21676 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21677 {
21678 Lisp_Object map;
21679 map = Fget_text_property (pos, Qlocal_map, string);
21680 if (!KEYMAPP (map))
21681 map = Fget_text_property (pos, Qkeymap, string);
21682 if (!KEYMAPP (map))
21683 cursor = dpyinfo->vertical_scroll_bar_cursor;
21684 }
21685
21686 /* Change the mouse face according to what is under X/Y. */
21687 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21688 if (!NILP (mouse_face)
21689 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21690 && glyph)
21691 {
21692 Lisp_Object b, e;
21693
21694 struct glyph * tmp_glyph;
21695
21696 int gpos;
21697 int gseq_length;
21698 int total_pixel_width;
21699 int ignore;
21700
21701 int vpos, hpos;
21702
21703 b = Fprevious_single_property_change (make_number (charpos + 1),
21704 Qmouse_face, string, Qnil);
21705 if (NILP (b))
21706 b = make_number (0);
21707
21708 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21709 if (NILP (e))
21710 e = make_number (SCHARS (string));
21711
21712 /* Calculate the position(glyph position: GPOS) of GLYPH in
21713 displayed string. GPOS is different from CHARPOS.
21714
21715 CHARPOS is the position of glyph in internal string
21716 object. A mode line string format has structures which
21717 is converted to a flatten by emacs lisp interpreter.
21718 The internal string is an element of the structures.
21719 The displayed string is the flatten string. */
21720 for (tmp_glyph = glyph - 1, gpos = 0;
21721 tmp_glyph->charpos >= XINT (b);
21722 tmp_glyph--, gpos++)
21723 {
21724 if (!EQ (tmp_glyph->object, glyph->object))
21725 break;
21726 }
21727
21728 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21729 displayed string holding GLYPH.
21730
21731 GSEQ_LENGTH is different from SCHARS (STRING).
21732 SCHARS (STRING) returns the length of the internal string. */
21733 for (tmp_glyph = glyph, gseq_length = gpos;
21734 tmp_glyph->charpos < XINT (e);
21735 tmp_glyph++, gseq_length++)
21736 {
21737 if (!EQ (tmp_glyph->object, glyph->object))
21738 break;
21739 }
21740
21741 total_pixel_width = 0;
21742 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21743 total_pixel_width += tmp_glyph->pixel_width;
21744
21745 /* Pre calculation of re-rendering position */
21746 vpos = (x - gpos);
21747 hpos = (area == ON_MODE_LINE
21748 ? (w->current_matrix)->nrows - 1
21749 : 0);
21750
21751 /* If the re-rendering position is included in the last
21752 re-rendering area, we should do nothing. */
21753 if ( EQ (window, dpyinfo->mouse_face_window)
21754 && dpyinfo->mouse_face_beg_col <= vpos
21755 && vpos < dpyinfo->mouse_face_end_col
21756 && dpyinfo->mouse_face_beg_row == hpos )
21757 return;
21758
21759 if (clear_mouse_face (dpyinfo))
21760 cursor = No_Cursor;
21761
21762 dpyinfo->mouse_face_beg_col = vpos;
21763 dpyinfo->mouse_face_beg_row = hpos;
21764
21765 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21766 dpyinfo->mouse_face_beg_y = 0;
21767
21768 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21769 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21770
21771 dpyinfo->mouse_face_end_x = 0;
21772 dpyinfo->mouse_face_end_y = 0;
21773
21774 dpyinfo->mouse_face_past_end = 0;
21775 dpyinfo->mouse_face_window = window;
21776
21777 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21778 charpos,
21779 0, 0, 0, &ignore,
21780 glyph->face_id, 1);
21781 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21782
21783 if (NILP (pointer))
21784 pointer = Qhand;
21785 }
21786 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21787 clear_mouse_face (dpyinfo);
21788 }
21789 define_frame_cursor1 (f, cursor, pointer);
21790 }
21791
21792
21793 /* EXPORT:
21794 Take proper action when the mouse has moved to position X, Y on
21795 frame F as regards highlighting characters that have mouse-face
21796 properties. Also de-highlighting chars where the mouse was before.
21797 X and Y can be negative or out of range. */
21798
21799 void
21800 note_mouse_highlight (f, x, y)
21801 struct frame *f;
21802 int x, y;
21803 {
21804 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21805 enum window_part part;
21806 Lisp_Object window;
21807 struct window *w;
21808 Cursor cursor = No_Cursor;
21809 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21810 struct buffer *b;
21811
21812 /* When a menu is active, don't highlight because this looks odd. */
21813 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21814 if (popup_activated ())
21815 return;
21816 #endif
21817
21818 if (NILP (Vmouse_highlight)
21819 || !f->glyphs_initialized_p)
21820 return;
21821
21822 dpyinfo->mouse_face_mouse_x = x;
21823 dpyinfo->mouse_face_mouse_y = y;
21824 dpyinfo->mouse_face_mouse_frame = f;
21825
21826 if (dpyinfo->mouse_face_defer)
21827 return;
21828
21829 if (gc_in_progress)
21830 {
21831 dpyinfo->mouse_face_deferred_gc = 1;
21832 return;
21833 }
21834
21835 /* Which window is that in? */
21836 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21837
21838 /* If we were displaying active text in another window, clear that.
21839 Also clear if we move out of text area in same window. */
21840 if (! EQ (window, dpyinfo->mouse_face_window)
21841 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21842 && !NILP (dpyinfo->mouse_face_window)))
21843 clear_mouse_face (dpyinfo);
21844
21845 /* Not on a window -> return. */
21846 if (!WINDOWP (window))
21847 return;
21848
21849 /* Reset help_echo_string. It will get recomputed below. */
21850 help_echo_string = Qnil;
21851
21852 /* Convert to window-relative pixel coordinates. */
21853 w = XWINDOW (window);
21854 frame_to_window_pixel_xy (w, &x, &y);
21855
21856 /* Handle tool-bar window differently since it doesn't display a
21857 buffer. */
21858 if (EQ (window, f->tool_bar_window))
21859 {
21860 note_tool_bar_highlight (f, x, y);
21861 return;
21862 }
21863
21864 /* Mouse is on the mode, header line or margin? */
21865 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21866 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21867 {
21868 note_mode_line_or_margin_highlight (window, x, y, part);
21869 return;
21870 }
21871
21872 if (part == ON_VERTICAL_BORDER)
21873 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21874 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21875 || part == ON_SCROLL_BAR)
21876 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21877 else
21878 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21879
21880 /* Are we in a window whose display is up to date?
21881 And verify the buffer's text has not changed. */
21882 b = XBUFFER (w->buffer);
21883 if (part == ON_TEXT
21884 && EQ (w->window_end_valid, w->buffer)
21885 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21886 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21887 {
21888 int hpos, vpos, pos, i, dx, dy, area;
21889 struct glyph *glyph;
21890 Lisp_Object object;
21891 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21892 Lisp_Object *overlay_vec = NULL;
21893 int noverlays;
21894 struct buffer *obuf;
21895 int obegv, ozv, same_region;
21896
21897 /* Find the glyph under X/Y. */
21898 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21899
21900 /* Look for :pointer property on image. */
21901 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21902 {
21903 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21904 if (img != NULL && IMAGEP (img->spec))
21905 {
21906 Lisp_Object image_map, hotspot;
21907 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21908 !NILP (image_map))
21909 && (hotspot = find_hot_spot (image_map,
21910 glyph->slice.x + dx,
21911 glyph->slice.y + dy),
21912 CONSP (hotspot))
21913 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21914 {
21915 Lisp_Object area_id, plist;
21916
21917 area_id = XCAR (hotspot);
21918 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21919 If so, we could look for mouse-enter, mouse-leave
21920 properties in PLIST (and do something...). */
21921 hotspot = XCDR (hotspot);
21922 if (CONSP (hotspot)
21923 && (plist = XCAR (hotspot), CONSP (plist)))
21924 {
21925 pointer = Fplist_get (plist, Qpointer);
21926 if (NILP (pointer))
21927 pointer = Qhand;
21928 help_echo_string = Fplist_get (plist, Qhelp_echo);
21929 if (!NILP (help_echo_string))
21930 {
21931 help_echo_window = window;
21932 help_echo_object = glyph->object;
21933 help_echo_pos = glyph->charpos;
21934 }
21935 }
21936 }
21937 if (NILP (pointer))
21938 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21939 }
21940 }
21941
21942 /* Clear mouse face if X/Y not over text. */
21943 if (glyph == NULL
21944 || area != TEXT_AREA
21945 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21946 {
21947 if (clear_mouse_face (dpyinfo))
21948 cursor = No_Cursor;
21949 if (NILP (pointer))
21950 {
21951 if (area != TEXT_AREA)
21952 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21953 else
21954 pointer = Vvoid_text_area_pointer;
21955 }
21956 goto set_cursor;
21957 }
21958
21959 pos = glyph->charpos;
21960 object = glyph->object;
21961 if (!STRINGP (object) && !BUFFERP (object))
21962 goto set_cursor;
21963
21964 /* If we get an out-of-range value, return now; avoid an error. */
21965 if (BUFFERP (object) && pos > BUF_Z (b))
21966 goto set_cursor;
21967
21968 /* Make the window's buffer temporarily current for
21969 overlays_at and compute_char_face. */
21970 obuf = current_buffer;
21971 current_buffer = b;
21972 obegv = BEGV;
21973 ozv = ZV;
21974 BEGV = BEG;
21975 ZV = Z;
21976
21977 /* Is this char mouse-active or does it have help-echo? */
21978 position = make_number (pos);
21979
21980 if (BUFFERP (object))
21981 {
21982 /* Put all the overlays we want in a vector in overlay_vec. */
21983 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21984 /* Sort overlays into increasing priority order. */
21985 noverlays = sort_overlays (overlay_vec, noverlays, w);
21986 }
21987 else
21988 noverlays = 0;
21989
21990 same_region = (EQ (window, dpyinfo->mouse_face_window)
21991 && vpos >= dpyinfo->mouse_face_beg_row
21992 && vpos <= dpyinfo->mouse_face_end_row
21993 && (vpos > dpyinfo->mouse_face_beg_row
21994 || hpos >= dpyinfo->mouse_face_beg_col)
21995 && (vpos < dpyinfo->mouse_face_end_row
21996 || hpos < dpyinfo->mouse_face_end_col
21997 || dpyinfo->mouse_face_past_end));
21998
21999 if (same_region)
22000 cursor = No_Cursor;
22001
22002 /* Check mouse-face highlighting. */
22003 if (! same_region
22004 /* If there exists an overlay with mouse-face overlapping
22005 the one we are currently highlighting, we have to
22006 check if we enter the overlapping overlay, and then
22007 highlight only that. */
22008 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22009 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22010 {
22011 /* Find the highest priority overlay that has a mouse-face
22012 property. */
22013 overlay = Qnil;
22014 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22015 {
22016 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22017 if (!NILP (mouse_face))
22018 overlay = overlay_vec[i];
22019 }
22020
22021 /* If we're actually highlighting the same overlay as
22022 before, there's no need to do that again. */
22023 if (!NILP (overlay)
22024 && EQ (overlay, dpyinfo->mouse_face_overlay))
22025 goto check_help_echo;
22026
22027 dpyinfo->mouse_face_overlay = overlay;
22028
22029 /* Clear the display of the old active region, if any. */
22030 if (clear_mouse_face (dpyinfo))
22031 cursor = No_Cursor;
22032
22033 /* If no overlay applies, get a text property. */
22034 if (NILP (overlay))
22035 mouse_face = Fget_text_property (position, Qmouse_face, object);
22036
22037 /* Handle the overlay case. */
22038 if (!NILP (overlay))
22039 {
22040 /* Find the range of text around this char that
22041 should be active. */
22042 Lisp_Object before, after;
22043 int ignore;
22044
22045 before = Foverlay_start (overlay);
22046 after = Foverlay_end (overlay);
22047 /* Record this as the current active region. */
22048 fast_find_position (w, XFASTINT (before),
22049 &dpyinfo->mouse_face_beg_col,
22050 &dpyinfo->mouse_face_beg_row,
22051 &dpyinfo->mouse_face_beg_x,
22052 &dpyinfo->mouse_face_beg_y, Qnil);
22053
22054 dpyinfo->mouse_face_past_end
22055 = !fast_find_position (w, XFASTINT (after),
22056 &dpyinfo->mouse_face_end_col,
22057 &dpyinfo->mouse_face_end_row,
22058 &dpyinfo->mouse_face_end_x,
22059 &dpyinfo->mouse_face_end_y, Qnil);
22060 dpyinfo->mouse_face_window = window;
22061
22062 dpyinfo->mouse_face_face_id
22063 = face_at_buffer_position (w, pos, 0, 0,
22064 &ignore, pos + 1,
22065 !dpyinfo->mouse_face_hidden);
22066
22067 /* Display it as active. */
22068 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22069 cursor = No_Cursor;
22070 }
22071 /* Handle the text property case. */
22072 else if (!NILP (mouse_face) && BUFFERP (object))
22073 {
22074 /* Find the range of text around this char that
22075 should be active. */
22076 Lisp_Object before, after, beginning, end;
22077 int ignore;
22078
22079 beginning = Fmarker_position (w->start);
22080 end = make_number (BUF_Z (XBUFFER (object))
22081 - XFASTINT (w->window_end_pos));
22082 before
22083 = Fprevious_single_property_change (make_number (pos + 1),
22084 Qmouse_face,
22085 object, beginning);
22086 after
22087 = Fnext_single_property_change (position, Qmouse_face,
22088 object, end);
22089
22090 /* Record this as the current active region. */
22091 fast_find_position (w, XFASTINT (before),
22092 &dpyinfo->mouse_face_beg_col,
22093 &dpyinfo->mouse_face_beg_row,
22094 &dpyinfo->mouse_face_beg_x,
22095 &dpyinfo->mouse_face_beg_y, Qnil);
22096 dpyinfo->mouse_face_past_end
22097 = !fast_find_position (w, XFASTINT (after),
22098 &dpyinfo->mouse_face_end_col,
22099 &dpyinfo->mouse_face_end_row,
22100 &dpyinfo->mouse_face_end_x,
22101 &dpyinfo->mouse_face_end_y, Qnil);
22102 dpyinfo->mouse_face_window = window;
22103
22104 if (BUFFERP (object))
22105 dpyinfo->mouse_face_face_id
22106 = face_at_buffer_position (w, pos, 0, 0,
22107 &ignore, pos + 1,
22108 !dpyinfo->mouse_face_hidden);
22109
22110 /* Display it as active. */
22111 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22112 cursor = No_Cursor;
22113 }
22114 else if (!NILP (mouse_face) && STRINGP (object))
22115 {
22116 Lisp_Object b, e;
22117 int ignore;
22118
22119 b = Fprevious_single_property_change (make_number (pos + 1),
22120 Qmouse_face,
22121 object, Qnil);
22122 e = Fnext_single_property_change (position, Qmouse_face,
22123 object, Qnil);
22124 if (NILP (b))
22125 b = make_number (0);
22126 if (NILP (e))
22127 e = make_number (SCHARS (object) - 1);
22128
22129 fast_find_string_pos (w, XINT (b), object,
22130 &dpyinfo->mouse_face_beg_col,
22131 &dpyinfo->mouse_face_beg_row,
22132 &dpyinfo->mouse_face_beg_x,
22133 &dpyinfo->mouse_face_beg_y, 0);
22134 fast_find_string_pos (w, XINT (e), object,
22135 &dpyinfo->mouse_face_end_col,
22136 &dpyinfo->mouse_face_end_row,
22137 &dpyinfo->mouse_face_end_x,
22138 &dpyinfo->mouse_face_end_y, 1);
22139 dpyinfo->mouse_face_past_end = 0;
22140 dpyinfo->mouse_face_window = window;
22141 dpyinfo->mouse_face_face_id
22142 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22143 glyph->face_id, 1);
22144 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22145 cursor = No_Cursor;
22146 }
22147 else if (STRINGP (object) && NILP (mouse_face))
22148 {
22149 /* A string which doesn't have mouse-face, but
22150 the text ``under'' it might have. */
22151 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22152 int start = MATRIX_ROW_START_CHARPOS (r);
22153
22154 pos = string_buffer_position (w, object, start);
22155 if (pos > 0)
22156 mouse_face = get_char_property_and_overlay (make_number (pos),
22157 Qmouse_face,
22158 w->buffer,
22159 &overlay);
22160 if (!NILP (mouse_face) && !NILP (overlay))
22161 {
22162 Lisp_Object before = Foverlay_start (overlay);
22163 Lisp_Object after = Foverlay_end (overlay);
22164 int ignore;
22165
22166 /* Note that we might not be able to find position
22167 BEFORE in the glyph matrix if the overlay is
22168 entirely covered by a `display' property. In
22169 this case, we overshoot. So let's stop in
22170 the glyph matrix before glyphs for OBJECT. */
22171 fast_find_position (w, XFASTINT (before),
22172 &dpyinfo->mouse_face_beg_col,
22173 &dpyinfo->mouse_face_beg_row,
22174 &dpyinfo->mouse_face_beg_x,
22175 &dpyinfo->mouse_face_beg_y,
22176 object);
22177
22178 dpyinfo->mouse_face_past_end
22179 = !fast_find_position (w, XFASTINT (after),
22180 &dpyinfo->mouse_face_end_col,
22181 &dpyinfo->mouse_face_end_row,
22182 &dpyinfo->mouse_face_end_x,
22183 &dpyinfo->mouse_face_end_y,
22184 Qnil);
22185 dpyinfo->mouse_face_window = window;
22186 dpyinfo->mouse_face_face_id
22187 = face_at_buffer_position (w, pos, 0, 0,
22188 &ignore, pos + 1,
22189 !dpyinfo->mouse_face_hidden);
22190
22191 /* Display it as active. */
22192 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22193 cursor = No_Cursor;
22194 }
22195 }
22196 }
22197
22198 check_help_echo:
22199
22200 /* Look for a `help-echo' property. */
22201 if (NILP (help_echo_string)) {
22202 Lisp_Object help, overlay;
22203
22204 /* Check overlays first. */
22205 help = overlay = Qnil;
22206 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22207 {
22208 overlay = overlay_vec[i];
22209 help = Foverlay_get (overlay, Qhelp_echo);
22210 }
22211
22212 if (!NILP (help))
22213 {
22214 help_echo_string = help;
22215 help_echo_window = window;
22216 help_echo_object = overlay;
22217 help_echo_pos = pos;
22218 }
22219 else
22220 {
22221 Lisp_Object object = glyph->object;
22222 int charpos = glyph->charpos;
22223
22224 /* Try text properties. */
22225 if (STRINGP (object)
22226 && charpos >= 0
22227 && charpos < SCHARS (object))
22228 {
22229 help = Fget_text_property (make_number (charpos),
22230 Qhelp_echo, object);
22231 if (NILP (help))
22232 {
22233 /* If the string itself doesn't specify a help-echo,
22234 see if the buffer text ``under'' it does. */
22235 struct glyph_row *r
22236 = MATRIX_ROW (w->current_matrix, vpos);
22237 int start = MATRIX_ROW_START_CHARPOS (r);
22238 int pos = string_buffer_position (w, object, start);
22239 if (pos > 0)
22240 {
22241 help = Fget_char_property (make_number (pos),
22242 Qhelp_echo, w->buffer);
22243 if (!NILP (help))
22244 {
22245 charpos = pos;
22246 object = w->buffer;
22247 }
22248 }
22249 }
22250 }
22251 else if (BUFFERP (object)
22252 && charpos >= BEGV
22253 && charpos < ZV)
22254 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22255 object);
22256
22257 if (!NILP (help))
22258 {
22259 help_echo_string = help;
22260 help_echo_window = window;
22261 help_echo_object = object;
22262 help_echo_pos = charpos;
22263 }
22264 }
22265 }
22266
22267 /* Look for a `pointer' property. */
22268 if (NILP (pointer))
22269 {
22270 /* Check overlays first. */
22271 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22272 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22273
22274 if (NILP (pointer))
22275 {
22276 Lisp_Object object = glyph->object;
22277 int charpos = glyph->charpos;
22278
22279 /* Try text properties. */
22280 if (STRINGP (object)
22281 && charpos >= 0
22282 && charpos < SCHARS (object))
22283 {
22284 pointer = Fget_text_property (make_number (charpos),
22285 Qpointer, object);
22286 if (NILP (pointer))
22287 {
22288 /* If the string itself doesn't specify a pointer,
22289 see if the buffer text ``under'' it does. */
22290 struct glyph_row *r
22291 = MATRIX_ROW (w->current_matrix, vpos);
22292 int start = MATRIX_ROW_START_CHARPOS (r);
22293 int pos = string_buffer_position (w, object, start);
22294 if (pos > 0)
22295 pointer = Fget_char_property (make_number (pos),
22296 Qpointer, w->buffer);
22297 }
22298 }
22299 else if (BUFFERP (object)
22300 && charpos >= BEGV
22301 && charpos < ZV)
22302 pointer = Fget_text_property (make_number (charpos),
22303 Qpointer, object);
22304 }
22305 }
22306
22307 BEGV = obegv;
22308 ZV = ozv;
22309 current_buffer = obuf;
22310 }
22311
22312 set_cursor:
22313
22314 define_frame_cursor1 (f, cursor, pointer);
22315 }
22316
22317
22318 /* EXPORT for RIF:
22319 Clear any mouse-face on window W. This function is part of the
22320 redisplay interface, and is called from try_window_id and similar
22321 functions to ensure the mouse-highlight is off. */
22322
22323 void
22324 x_clear_window_mouse_face (w)
22325 struct window *w;
22326 {
22327 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22328 Lisp_Object window;
22329
22330 BLOCK_INPUT;
22331 XSETWINDOW (window, w);
22332 if (EQ (window, dpyinfo->mouse_face_window))
22333 clear_mouse_face (dpyinfo);
22334 UNBLOCK_INPUT;
22335 }
22336
22337
22338 /* EXPORT:
22339 Just discard the mouse face information for frame F, if any.
22340 This is used when the size of F is changed. */
22341
22342 void
22343 cancel_mouse_face (f)
22344 struct frame *f;
22345 {
22346 Lisp_Object window;
22347 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22348
22349 window = dpyinfo->mouse_face_window;
22350 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22351 {
22352 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22353 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22354 dpyinfo->mouse_face_window = Qnil;
22355 }
22356 }
22357
22358
22359 #endif /* HAVE_WINDOW_SYSTEM */
22360
22361 \f
22362 /***********************************************************************
22363 Exposure Events
22364 ***********************************************************************/
22365
22366 #ifdef HAVE_WINDOW_SYSTEM
22367
22368 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22369 which intersects rectangle R. R is in window-relative coordinates. */
22370
22371 static void
22372 expose_area (w, row, r, area)
22373 struct window *w;
22374 struct glyph_row *row;
22375 XRectangle *r;
22376 enum glyph_row_area area;
22377 {
22378 struct glyph *first = row->glyphs[area];
22379 struct glyph *end = row->glyphs[area] + row->used[area];
22380 struct glyph *last;
22381 int first_x, start_x, x;
22382
22383 if (area == TEXT_AREA && row->fill_line_p)
22384 /* If row extends face to end of line write the whole line. */
22385 draw_glyphs (w, 0, row, area,
22386 0, row->used[area],
22387 DRAW_NORMAL_TEXT, 0);
22388 else
22389 {
22390 /* Set START_X to the window-relative start position for drawing glyphs of
22391 AREA. The first glyph of the text area can be partially visible.
22392 The first glyphs of other areas cannot. */
22393 start_x = window_box_left_offset (w, area);
22394 x = start_x;
22395 if (area == TEXT_AREA)
22396 x += row->x;
22397
22398 /* Find the first glyph that must be redrawn. */
22399 while (first < end
22400 && x + first->pixel_width < r->x)
22401 {
22402 x += first->pixel_width;
22403 ++first;
22404 }
22405
22406 /* Find the last one. */
22407 last = first;
22408 first_x = x;
22409 while (last < end
22410 && x < r->x + r->width)
22411 {
22412 x += last->pixel_width;
22413 ++last;
22414 }
22415
22416 /* Repaint. */
22417 if (last > first)
22418 draw_glyphs (w, first_x - start_x, row, area,
22419 first - row->glyphs[area], last - row->glyphs[area],
22420 DRAW_NORMAL_TEXT, 0);
22421 }
22422 }
22423
22424
22425 /* Redraw the parts of the glyph row ROW on window W intersecting
22426 rectangle R. R is in window-relative coordinates. Value is
22427 non-zero if mouse-face was overwritten. */
22428
22429 static int
22430 expose_line (w, row, r)
22431 struct window *w;
22432 struct glyph_row *row;
22433 XRectangle *r;
22434 {
22435 xassert (row->enabled_p);
22436
22437 if (row->mode_line_p || w->pseudo_window_p)
22438 draw_glyphs (w, 0, row, TEXT_AREA,
22439 0, row->used[TEXT_AREA],
22440 DRAW_NORMAL_TEXT, 0);
22441 else
22442 {
22443 if (row->used[LEFT_MARGIN_AREA])
22444 expose_area (w, row, r, LEFT_MARGIN_AREA);
22445 if (row->used[TEXT_AREA])
22446 expose_area (w, row, r, TEXT_AREA);
22447 if (row->used[RIGHT_MARGIN_AREA])
22448 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22449 draw_row_fringe_bitmaps (w, row);
22450 }
22451
22452 return row->mouse_face_p;
22453 }
22454
22455
22456 /* Redraw those parts of glyphs rows during expose event handling that
22457 overlap other rows. Redrawing of an exposed line writes over parts
22458 of lines overlapping that exposed line; this function fixes that.
22459
22460 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22461 row in W's current matrix that is exposed and overlaps other rows.
22462 LAST_OVERLAPPING_ROW is the last such row. */
22463
22464 static void
22465 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22466 struct window *w;
22467 struct glyph_row *first_overlapping_row;
22468 struct glyph_row *last_overlapping_row;
22469 {
22470 struct glyph_row *row;
22471
22472 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22473 if (row->overlapping_p)
22474 {
22475 xassert (row->enabled_p && !row->mode_line_p);
22476
22477 if (row->used[LEFT_MARGIN_AREA])
22478 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22479
22480 if (row->used[TEXT_AREA])
22481 x_fix_overlapping_area (w, row, TEXT_AREA);
22482
22483 if (row->used[RIGHT_MARGIN_AREA])
22484 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22485 }
22486 }
22487
22488
22489 /* Return non-zero if W's cursor intersects rectangle R. */
22490
22491 static int
22492 phys_cursor_in_rect_p (w, r)
22493 struct window *w;
22494 XRectangle *r;
22495 {
22496 XRectangle cr, result;
22497 struct glyph *cursor_glyph;
22498
22499 cursor_glyph = get_phys_cursor_glyph (w);
22500 if (cursor_glyph)
22501 {
22502 /* r is relative to W's box, but w->phys_cursor.x is relative
22503 to left edge of W's TEXT area. Adjust it. */
22504 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22505 cr.y = w->phys_cursor.y;
22506 cr.width = cursor_glyph->pixel_width;
22507 cr.height = w->phys_cursor_height;
22508 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22509 I assume the effect is the same -- and this is portable. */
22510 return x_intersect_rectangles (&cr, r, &result);
22511 }
22512 else
22513 return 0;
22514 }
22515
22516
22517 /* EXPORT:
22518 Draw a vertical window border to the right of window W if W doesn't
22519 have vertical scroll bars. */
22520
22521 void
22522 x_draw_vertical_border (w)
22523 struct window *w;
22524 {
22525 /* We could do better, if we knew what type of scroll-bar the adjacent
22526 windows (on either side) have... But we don't :-(
22527 However, I think this works ok. ++KFS 2003-04-25 */
22528
22529 /* Redraw borders between horizontally adjacent windows. Don't
22530 do it for frames with vertical scroll bars because either the
22531 right scroll bar of a window, or the left scroll bar of its
22532 neighbor will suffice as a border. */
22533 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22534 return;
22535
22536 if (!WINDOW_RIGHTMOST_P (w)
22537 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22538 {
22539 int x0, x1, y0, y1;
22540
22541 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22542 y1 -= 1;
22543
22544 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22545 x1 -= 1;
22546
22547 rif->draw_vertical_window_border (w, x1, y0, y1);
22548 }
22549 else if (!WINDOW_LEFTMOST_P (w)
22550 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22551 {
22552 int x0, x1, y0, y1;
22553
22554 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22555 y1 -= 1;
22556
22557 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22558 x0 -= 1;
22559
22560 rif->draw_vertical_window_border (w, x0, y0, y1);
22561 }
22562 }
22563
22564
22565 /* Redraw the part of window W intersection rectangle FR. Pixel
22566 coordinates in FR are frame-relative. Call this function with
22567 input blocked. Value is non-zero if the exposure overwrites
22568 mouse-face. */
22569
22570 static int
22571 expose_window (w, fr)
22572 struct window *w;
22573 XRectangle *fr;
22574 {
22575 struct frame *f = XFRAME (w->frame);
22576 XRectangle wr, r;
22577 int mouse_face_overwritten_p = 0;
22578
22579 /* If window is not yet fully initialized, do nothing. This can
22580 happen when toolkit scroll bars are used and a window is split.
22581 Reconfiguring the scroll bar will generate an expose for a newly
22582 created window. */
22583 if (w->current_matrix == NULL)
22584 return 0;
22585
22586 /* When we're currently updating the window, display and current
22587 matrix usually don't agree. Arrange for a thorough display
22588 later. */
22589 if (w == updated_window)
22590 {
22591 SET_FRAME_GARBAGED (f);
22592 return 0;
22593 }
22594
22595 /* Frame-relative pixel rectangle of W. */
22596 wr.x = WINDOW_LEFT_EDGE_X (w);
22597 wr.y = WINDOW_TOP_EDGE_Y (w);
22598 wr.width = WINDOW_TOTAL_WIDTH (w);
22599 wr.height = WINDOW_TOTAL_HEIGHT (w);
22600
22601 if (x_intersect_rectangles (fr, &wr, &r))
22602 {
22603 int yb = window_text_bottom_y (w);
22604 struct glyph_row *row;
22605 int cursor_cleared_p;
22606 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22607
22608 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22609 r.x, r.y, r.width, r.height));
22610
22611 /* Convert to window coordinates. */
22612 r.x -= WINDOW_LEFT_EDGE_X (w);
22613 r.y -= WINDOW_TOP_EDGE_Y (w);
22614
22615 /* Turn off the cursor. */
22616 if (!w->pseudo_window_p
22617 && phys_cursor_in_rect_p (w, &r))
22618 {
22619 x_clear_cursor (w);
22620 cursor_cleared_p = 1;
22621 }
22622 else
22623 cursor_cleared_p = 0;
22624
22625 /* Update lines intersecting rectangle R. */
22626 first_overlapping_row = last_overlapping_row = NULL;
22627 for (row = w->current_matrix->rows;
22628 row->enabled_p;
22629 ++row)
22630 {
22631 int y0 = row->y;
22632 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22633
22634 if ((y0 >= r.y && y0 < r.y + r.height)
22635 || (y1 > r.y && y1 < r.y + r.height)
22636 || (r.y >= y0 && r.y < y1)
22637 || (r.y + r.height > y0 && r.y + r.height < y1))
22638 {
22639 /* A header line may be overlapping, but there is no need
22640 to fix overlapping areas for them. KFS 2005-02-12 */
22641 if (row->overlapping_p && !row->mode_line_p)
22642 {
22643 if (first_overlapping_row == NULL)
22644 first_overlapping_row = row;
22645 last_overlapping_row = row;
22646 }
22647
22648 if (expose_line (w, row, &r))
22649 mouse_face_overwritten_p = 1;
22650 }
22651
22652 if (y1 >= yb)
22653 break;
22654 }
22655
22656 /* Display the mode line if there is one. */
22657 if (WINDOW_WANTS_MODELINE_P (w)
22658 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22659 row->enabled_p)
22660 && row->y < r.y + r.height)
22661 {
22662 if (expose_line (w, row, &r))
22663 mouse_face_overwritten_p = 1;
22664 }
22665
22666 if (!w->pseudo_window_p)
22667 {
22668 /* Fix the display of overlapping rows. */
22669 if (first_overlapping_row)
22670 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22671
22672 /* Draw border between windows. */
22673 x_draw_vertical_border (w);
22674
22675 /* Turn the cursor on again. */
22676 if (cursor_cleared_p)
22677 update_window_cursor (w, 1);
22678 }
22679 }
22680
22681 return mouse_face_overwritten_p;
22682 }
22683
22684
22685
22686 /* Redraw (parts) of all windows in the window tree rooted at W that
22687 intersect R. R contains frame pixel coordinates. Value is
22688 non-zero if the exposure overwrites mouse-face. */
22689
22690 static int
22691 expose_window_tree (w, r)
22692 struct window *w;
22693 XRectangle *r;
22694 {
22695 struct frame *f = XFRAME (w->frame);
22696 int mouse_face_overwritten_p = 0;
22697
22698 while (w && !FRAME_GARBAGED_P (f))
22699 {
22700 if (!NILP (w->hchild))
22701 mouse_face_overwritten_p
22702 |= expose_window_tree (XWINDOW (w->hchild), r);
22703 else if (!NILP (w->vchild))
22704 mouse_face_overwritten_p
22705 |= expose_window_tree (XWINDOW (w->vchild), r);
22706 else
22707 mouse_face_overwritten_p |= expose_window (w, r);
22708
22709 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22710 }
22711
22712 return mouse_face_overwritten_p;
22713 }
22714
22715
22716 /* EXPORT:
22717 Redisplay an exposed area of frame F. X and Y are the upper-left
22718 corner of the exposed rectangle. W and H are width and height of
22719 the exposed area. All are pixel values. W or H zero means redraw
22720 the entire frame. */
22721
22722 void
22723 expose_frame (f, x, y, w, h)
22724 struct frame *f;
22725 int x, y, w, h;
22726 {
22727 XRectangle r;
22728 int mouse_face_overwritten_p = 0;
22729
22730 TRACE ((stderr, "expose_frame "));
22731
22732 /* No need to redraw if frame will be redrawn soon. */
22733 if (FRAME_GARBAGED_P (f))
22734 {
22735 TRACE ((stderr, " garbaged\n"));
22736 return;
22737 }
22738
22739 /* If basic faces haven't been realized yet, there is no point in
22740 trying to redraw anything. This can happen when we get an expose
22741 event while Emacs is starting, e.g. by moving another window. */
22742 if (FRAME_FACE_CACHE (f) == NULL
22743 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22744 {
22745 TRACE ((stderr, " no faces\n"));
22746 return;
22747 }
22748
22749 if (w == 0 || h == 0)
22750 {
22751 r.x = r.y = 0;
22752 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22753 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22754 }
22755 else
22756 {
22757 r.x = x;
22758 r.y = y;
22759 r.width = w;
22760 r.height = h;
22761 }
22762
22763 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22764 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22765
22766 if (WINDOWP (f->tool_bar_window))
22767 mouse_face_overwritten_p
22768 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22769
22770 #ifdef HAVE_X_WINDOWS
22771 #ifndef MSDOS
22772 #ifndef USE_X_TOOLKIT
22773 if (WINDOWP (f->menu_bar_window))
22774 mouse_face_overwritten_p
22775 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22776 #endif /* not USE_X_TOOLKIT */
22777 #endif
22778 #endif
22779
22780 /* Some window managers support a focus-follows-mouse style with
22781 delayed raising of frames. Imagine a partially obscured frame,
22782 and moving the mouse into partially obscured mouse-face on that
22783 frame. The visible part of the mouse-face will be highlighted,
22784 then the WM raises the obscured frame. With at least one WM, KDE
22785 2.1, Emacs is not getting any event for the raising of the frame
22786 (even tried with SubstructureRedirectMask), only Expose events.
22787 These expose events will draw text normally, i.e. not
22788 highlighted. Which means we must redo the highlight here.
22789 Subsume it under ``we love X''. --gerd 2001-08-15 */
22790 /* Included in Windows version because Windows most likely does not
22791 do the right thing if any third party tool offers
22792 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22793 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22794 {
22795 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22796 if (f == dpyinfo->mouse_face_mouse_frame)
22797 {
22798 int x = dpyinfo->mouse_face_mouse_x;
22799 int y = dpyinfo->mouse_face_mouse_y;
22800 clear_mouse_face (dpyinfo);
22801 note_mouse_highlight (f, x, y);
22802 }
22803 }
22804 }
22805
22806
22807 /* EXPORT:
22808 Determine the intersection of two rectangles R1 and R2. Return
22809 the intersection in *RESULT. Value is non-zero if RESULT is not
22810 empty. */
22811
22812 int
22813 x_intersect_rectangles (r1, r2, result)
22814 XRectangle *r1, *r2, *result;
22815 {
22816 XRectangle *left, *right;
22817 XRectangle *upper, *lower;
22818 int intersection_p = 0;
22819
22820 /* Rearrange so that R1 is the left-most rectangle. */
22821 if (r1->x < r2->x)
22822 left = r1, right = r2;
22823 else
22824 left = r2, right = r1;
22825
22826 /* X0 of the intersection is right.x0, if this is inside R1,
22827 otherwise there is no intersection. */
22828 if (right->x <= left->x + left->width)
22829 {
22830 result->x = right->x;
22831
22832 /* The right end of the intersection is the minimum of the
22833 the right ends of left and right. */
22834 result->width = (min (left->x + left->width, right->x + right->width)
22835 - result->x);
22836
22837 /* Same game for Y. */
22838 if (r1->y < r2->y)
22839 upper = r1, lower = r2;
22840 else
22841 upper = r2, lower = r1;
22842
22843 /* The upper end of the intersection is lower.y0, if this is inside
22844 of upper. Otherwise, there is no intersection. */
22845 if (lower->y <= upper->y + upper->height)
22846 {
22847 result->y = lower->y;
22848
22849 /* The lower end of the intersection is the minimum of the lower
22850 ends of upper and lower. */
22851 result->height = (min (lower->y + lower->height,
22852 upper->y + upper->height)
22853 - result->y);
22854 intersection_p = 1;
22855 }
22856 }
22857
22858 return intersection_p;
22859 }
22860
22861 #endif /* HAVE_WINDOW_SYSTEM */
22862
22863 \f
22864 /***********************************************************************
22865 Initialization
22866 ***********************************************************************/
22867
22868 void
22869 syms_of_xdisp ()
22870 {
22871 Vwith_echo_area_save_vector = Qnil;
22872 staticpro (&Vwith_echo_area_save_vector);
22873
22874 Vmessage_stack = Qnil;
22875 staticpro (&Vmessage_stack);
22876
22877 Qinhibit_redisplay = intern ("inhibit-redisplay");
22878 staticpro (&Qinhibit_redisplay);
22879
22880 message_dolog_marker1 = Fmake_marker ();
22881 staticpro (&message_dolog_marker1);
22882 message_dolog_marker2 = Fmake_marker ();
22883 staticpro (&message_dolog_marker2);
22884 message_dolog_marker3 = Fmake_marker ();
22885 staticpro (&message_dolog_marker3);
22886
22887 #if GLYPH_DEBUG
22888 defsubr (&Sdump_frame_glyph_matrix);
22889 defsubr (&Sdump_glyph_matrix);
22890 defsubr (&Sdump_glyph_row);
22891 defsubr (&Sdump_tool_bar_row);
22892 defsubr (&Strace_redisplay);
22893 defsubr (&Strace_to_stderr);
22894 #endif
22895 #ifdef HAVE_WINDOW_SYSTEM
22896 defsubr (&Stool_bar_lines_needed);
22897 defsubr (&Slookup_image_map);
22898 #endif
22899 defsubr (&Sformat_mode_line);
22900
22901 staticpro (&Qmenu_bar_update_hook);
22902 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22903
22904 staticpro (&Qoverriding_terminal_local_map);
22905 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22906
22907 staticpro (&Qoverriding_local_map);
22908 Qoverriding_local_map = intern ("overriding-local-map");
22909
22910 staticpro (&Qwindow_scroll_functions);
22911 Qwindow_scroll_functions = intern ("window-scroll-functions");
22912
22913 staticpro (&Qredisplay_end_trigger_functions);
22914 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22915
22916 staticpro (&Qinhibit_point_motion_hooks);
22917 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22918
22919 QCdata = intern (":data");
22920 staticpro (&QCdata);
22921 Qdisplay = intern ("display");
22922 staticpro (&Qdisplay);
22923 Qspace_width = intern ("space-width");
22924 staticpro (&Qspace_width);
22925 Qraise = intern ("raise");
22926 staticpro (&Qraise);
22927 Qslice = intern ("slice");
22928 staticpro (&Qslice);
22929 Qspace = intern ("space");
22930 staticpro (&Qspace);
22931 Qmargin = intern ("margin");
22932 staticpro (&Qmargin);
22933 Qpointer = intern ("pointer");
22934 staticpro (&Qpointer);
22935 Qleft_margin = intern ("left-margin");
22936 staticpro (&Qleft_margin);
22937 Qright_margin = intern ("right-margin");
22938 staticpro (&Qright_margin);
22939 Qcenter = intern ("center");
22940 staticpro (&Qcenter);
22941 Qline_height = intern ("line-height");
22942 staticpro (&Qline_height);
22943 QCalign_to = intern (":align-to");
22944 staticpro (&QCalign_to);
22945 QCrelative_width = intern (":relative-width");
22946 staticpro (&QCrelative_width);
22947 QCrelative_height = intern (":relative-height");
22948 staticpro (&QCrelative_height);
22949 QCeval = intern (":eval");
22950 staticpro (&QCeval);
22951 QCpropertize = intern (":propertize");
22952 staticpro (&QCpropertize);
22953 QCfile = intern (":file");
22954 staticpro (&QCfile);
22955 Qfontified = intern ("fontified");
22956 staticpro (&Qfontified);
22957 Qfontification_functions = intern ("fontification-functions");
22958 staticpro (&Qfontification_functions);
22959 Qtrailing_whitespace = intern ("trailing-whitespace");
22960 staticpro (&Qtrailing_whitespace);
22961 Qescape_glyph = intern ("escape-glyph");
22962 staticpro (&Qescape_glyph);
22963 Qnobreak_space = intern ("nobreak-space");
22964 staticpro (&Qnobreak_space);
22965 Qimage = intern ("image");
22966 staticpro (&Qimage);
22967 QCmap = intern (":map");
22968 staticpro (&QCmap);
22969 QCpointer = intern (":pointer");
22970 staticpro (&QCpointer);
22971 Qrect = intern ("rect");
22972 staticpro (&Qrect);
22973 Qcircle = intern ("circle");
22974 staticpro (&Qcircle);
22975 Qpoly = intern ("poly");
22976 staticpro (&Qpoly);
22977 Qmessage_truncate_lines = intern ("message-truncate-lines");
22978 staticpro (&Qmessage_truncate_lines);
22979 Qgrow_only = intern ("grow-only");
22980 staticpro (&Qgrow_only);
22981 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22982 staticpro (&Qinhibit_menubar_update);
22983 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22984 staticpro (&Qinhibit_eval_during_redisplay);
22985 Qposition = intern ("position");
22986 staticpro (&Qposition);
22987 Qbuffer_position = intern ("buffer-position");
22988 staticpro (&Qbuffer_position);
22989 Qobject = intern ("object");
22990 staticpro (&Qobject);
22991 Qbar = intern ("bar");
22992 staticpro (&Qbar);
22993 Qhbar = intern ("hbar");
22994 staticpro (&Qhbar);
22995 Qbox = intern ("box");
22996 staticpro (&Qbox);
22997 Qhollow = intern ("hollow");
22998 staticpro (&Qhollow);
22999 Qhand = intern ("hand");
23000 staticpro (&Qhand);
23001 Qarrow = intern ("arrow");
23002 staticpro (&Qarrow);
23003 Qtext = intern ("text");
23004 staticpro (&Qtext);
23005 Qrisky_local_variable = intern ("risky-local-variable");
23006 staticpro (&Qrisky_local_variable);
23007 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23008 staticpro (&Qinhibit_free_realized_faces);
23009
23010 list_of_error = Fcons (Fcons (intern ("error"),
23011 Fcons (intern ("void-variable"), Qnil)),
23012 Qnil);
23013 staticpro (&list_of_error);
23014
23015 Qlast_arrow_position = intern ("last-arrow-position");
23016 staticpro (&Qlast_arrow_position);
23017 Qlast_arrow_string = intern ("last-arrow-string");
23018 staticpro (&Qlast_arrow_string);
23019
23020 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23021 staticpro (&Qoverlay_arrow_string);
23022 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23023 staticpro (&Qoverlay_arrow_bitmap);
23024
23025 echo_buffer[0] = echo_buffer[1] = Qnil;
23026 staticpro (&echo_buffer[0]);
23027 staticpro (&echo_buffer[1]);
23028
23029 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23030 staticpro (&echo_area_buffer[0]);
23031 staticpro (&echo_area_buffer[1]);
23032
23033 Vmessages_buffer_name = build_string ("*Messages*");
23034 staticpro (&Vmessages_buffer_name);
23035
23036 mode_line_proptrans_alist = Qnil;
23037 staticpro (&mode_line_proptrans_alist);
23038 mode_line_string_list = Qnil;
23039 staticpro (&mode_line_string_list);
23040 mode_line_string_face = Qnil;
23041 staticpro (&mode_line_string_face);
23042 mode_line_string_face_prop = Qnil;
23043 staticpro (&mode_line_string_face_prop);
23044 Vmode_line_unwind_vector = Qnil;
23045 staticpro (&Vmode_line_unwind_vector);
23046
23047 help_echo_string = Qnil;
23048 staticpro (&help_echo_string);
23049 help_echo_object = Qnil;
23050 staticpro (&help_echo_object);
23051 help_echo_window = Qnil;
23052 staticpro (&help_echo_window);
23053 previous_help_echo_string = Qnil;
23054 staticpro (&previous_help_echo_string);
23055 help_echo_pos = -1;
23056
23057 #ifdef HAVE_WINDOW_SYSTEM
23058 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23059 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23060 For example, if a block cursor is over a tab, it will be drawn as
23061 wide as that tab on the display. */);
23062 x_stretch_cursor_p = 0;
23063 #endif
23064
23065 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23066 doc: /* *Non-nil means highlight trailing whitespace.
23067 The face used for trailing whitespace is `trailing-whitespace'. */);
23068 Vshow_trailing_whitespace = Qnil;
23069
23070 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23071 doc: /* *Control highlighting of nobreak space and soft hyphen.
23072 A value of t means highlight the character itself (for nobreak space,
23073 use face `nobreak-space').
23074 A value of nil means no highlighting.
23075 Other values mean display the escape glyph followed by an ordinary
23076 space or ordinary hyphen. */);
23077 Vnobreak_char_display = Qt;
23078
23079 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23080 doc: /* *The pointer shape to show in void text areas.
23081 A value of nil means to show the text pointer. Other options are `arrow',
23082 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23083 Vvoid_text_area_pointer = Qarrow;
23084
23085 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23086 doc: /* Non-nil means don't actually do any redisplay.
23087 This is used for internal purposes. */);
23088 Vinhibit_redisplay = Qnil;
23089
23090 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23091 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23092 Vglobal_mode_string = Qnil;
23093
23094 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23095 doc: /* Marker for where to display an arrow on top of the buffer text.
23096 This must be the beginning of a line in order to work.
23097 See also `overlay-arrow-string'. */);
23098 Voverlay_arrow_position = Qnil;
23099
23100 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23101 doc: /* String to display as an arrow in non-window frames.
23102 See also `overlay-arrow-position'. */);
23103 Voverlay_arrow_string = build_string ("=>");
23104
23105 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23106 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23107 The symbols on this list are examined during redisplay to determine
23108 where to display overlay arrows. */);
23109 Voverlay_arrow_variable_list
23110 = Fcons (intern ("overlay-arrow-position"), Qnil);
23111
23112 DEFVAR_INT ("scroll-step", &scroll_step,
23113 doc: /* *The number of lines to try scrolling a window by when point moves out.
23114 If that fails to bring point back on frame, point is centered instead.
23115 If this is zero, point is always centered after it moves off frame.
23116 If you want scrolling to always be a line at a time, you should set
23117 `scroll-conservatively' to a large value rather than set this to 1. */);
23118
23119 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23120 doc: /* *Scroll up to this many lines, to bring point back on screen.
23121 A value of zero means to scroll the text to center point vertically
23122 in the window. */);
23123 scroll_conservatively = 0;
23124
23125 DEFVAR_INT ("scroll-margin", &scroll_margin,
23126 doc: /* *Number of lines of margin at the top and bottom of a window.
23127 Recenter the window whenever point gets within this many lines
23128 of the top or bottom of the window. */);
23129 scroll_margin = 0;
23130
23131 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23132 doc: /* Pixels per inch on current display.
23133 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23134 Vdisplay_pixels_per_inch = make_float (72.0);
23135
23136 #if GLYPH_DEBUG
23137 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23138 #endif
23139
23140 DEFVAR_BOOL ("truncate-partial-width-windows",
23141 &truncate_partial_width_windows,
23142 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23143 truncate_partial_width_windows = 1;
23144
23145 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23146 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23147 Any other value means to use the appropriate face, `mode-line',
23148 `header-line', or `menu' respectively. */);
23149 mode_line_inverse_video = 1;
23150
23151 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23152 doc: /* *Maximum buffer size for which line number should be displayed.
23153 If the buffer is bigger than this, the line number does not appear
23154 in the mode line. A value of nil means no limit. */);
23155 Vline_number_display_limit = Qnil;
23156
23157 DEFVAR_INT ("line-number-display-limit-width",
23158 &line_number_display_limit_width,
23159 doc: /* *Maximum line width (in characters) for line number display.
23160 If the average length of the lines near point is bigger than this, then the
23161 line number may be omitted from the mode line. */);
23162 line_number_display_limit_width = 200;
23163
23164 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23165 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23166 highlight_nonselected_windows = 0;
23167
23168 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23169 doc: /* Non-nil if more than one frame is visible on this display.
23170 Minibuffer-only frames don't count, but iconified frames do.
23171 This variable is not guaranteed to be accurate except while processing
23172 `frame-title-format' and `icon-title-format'. */);
23173
23174 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23175 doc: /* Template for displaying the title bar of visible frames.
23176 \(Assuming the window manager supports this feature.)
23177 This variable has the same structure as `mode-line-format' (which see),
23178 and is used only on frames for which no explicit name has been set
23179 \(see `modify-frame-parameters'). */);
23180
23181 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23182 doc: /* Template for displaying the title bar of an iconified frame.
23183 \(Assuming the window manager supports this feature.)
23184 This variable has the same structure as `mode-line-format' (which see),
23185 and is used only on frames for which no explicit name has been set
23186 \(see `modify-frame-parameters'). */);
23187 Vicon_title_format
23188 = Vframe_title_format
23189 = Fcons (intern ("multiple-frames"),
23190 Fcons (build_string ("%b"),
23191 Fcons (Fcons (empty_string,
23192 Fcons (intern ("invocation-name"),
23193 Fcons (build_string ("@"),
23194 Fcons (intern ("system-name"),
23195 Qnil)))),
23196 Qnil)));
23197
23198 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23199 doc: /* Maximum number of lines to keep in the message log buffer.
23200 If nil, disable message logging. If t, log messages but don't truncate
23201 the buffer when it becomes large. */);
23202 Vmessage_log_max = make_number (50);
23203
23204 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23205 doc: /* Functions called before redisplay, if window sizes have changed.
23206 The value should be a list of functions that take one argument.
23207 Just before redisplay, for each frame, if any of its windows have changed
23208 size since the last redisplay, or have been split or deleted,
23209 all the functions in the list are called, with the frame as argument. */);
23210 Vwindow_size_change_functions = Qnil;
23211
23212 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23213 doc: /* List of functions to call before redisplaying a window with scrolling.
23214 Each function is called with two arguments, the window
23215 and its new display-start position. Note that the value of `window-end'
23216 is not valid when these functions are called. */);
23217 Vwindow_scroll_functions = Qnil;
23218
23219 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23220 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23221 mouse_autoselect_window = 0;
23222
23223 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23224 doc: /* *Non-nil means automatically resize tool-bars.
23225 This increases a tool-bar's height if not all tool-bar items are visible.
23226 It decreases a tool-bar's height when it would display blank lines
23227 otherwise. */);
23228 auto_resize_tool_bars_p = 1;
23229
23230 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23231 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23232 auto_raise_tool_bar_buttons_p = 1;
23233
23234 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23235 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23236 make_cursor_line_fully_visible_p = 1;
23237
23238 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23239 doc: /* *Margin around tool-bar buttons in pixels.
23240 If an integer, use that for both horizontal and vertical margins.
23241 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23242 HORZ specifying the horizontal margin, and VERT specifying the
23243 vertical margin. */);
23244 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23245
23246 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23247 doc: /* *Relief thickness of tool-bar buttons. */);
23248 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23249
23250 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23251 doc: /* List of functions to call to fontify regions of text.
23252 Each function is called with one argument POS. Functions must
23253 fontify a region starting at POS in the current buffer, and give
23254 fontified regions the property `fontified'. */);
23255 Vfontification_functions = Qnil;
23256 Fmake_variable_buffer_local (Qfontification_functions);
23257
23258 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23259 &unibyte_display_via_language_environment,
23260 doc: /* *Non-nil means display unibyte text according to language environment.
23261 Specifically this means that unibyte non-ASCII characters
23262 are displayed by converting them to the equivalent multibyte characters
23263 according to the current language environment. As a result, they are
23264 displayed according to the current fontset. */);
23265 unibyte_display_via_language_environment = 0;
23266
23267 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23268 doc: /* *Maximum height for resizing mini-windows.
23269 If a float, it specifies a fraction of the mini-window frame's height.
23270 If an integer, it specifies a number of lines. */);
23271 Vmax_mini_window_height = make_float (0.25);
23272
23273 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23274 doc: /* *How to resize mini-windows.
23275 A value of nil means don't automatically resize mini-windows.
23276 A value of t means resize them to fit the text displayed in them.
23277 A value of `grow-only', the default, means let mini-windows grow
23278 only, until their display becomes empty, at which point the windows
23279 go back to their normal size. */);
23280 Vresize_mini_windows = Qgrow_only;
23281
23282 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23283 doc: /* Alist specifying how to blink the cursor off.
23284 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23285 `cursor-type' frame-parameter or variable equals ON-STATE,
23286 comparing using `equal', Emacs uses OFF-STATE to specify
23287 how to blink it off. */);
23288 Vblink_cursor_alist = Qnil;
23289
23290 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23291 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23292 automatic_hscrolling_p = 1;
23293
23294 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23295 doc: /* *How many columns away from the window edge point is allowed to get
23296 before automatic hscrolling will horizontally scroll the window. */);
23297 hscroll_margin = 5;
23298
23299 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23300 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23301 When point is less than `automatic-hscroll-margin' columns from the window
23302 edge, automatic hscrolling will scroll the window by the amount of columns
23303 determined by this variable. If its value is a positive integer, scroll that
23304 many columns. If it's a positive floating-point number, it specifies the
23305 fraction of the window's width to scroll. If it's nil or zero, point will be
23306 centered horizontally after the scroll. Any other value, including negative
23307 numbers, are treated as if the value were zero.
23308
23309 Automatic hscrolling always moves point outside the scroll margin, so if
23310 point was more than scroll step columns inside the margin, the window will
23311 scroll more than the value given by the scroll step.
23312
23313 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23314 and `scroll-right' overrides this variable's effect. */);
23315 Vhscroll_step = make_number (0);
23316
23317 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23318 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23319 Bind this around calls to `message' to let it take effect. */);
23320 message_truncate_lines = 0;
23321
23322 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23323 doc: /* Normal hook run to update the menu bar definitions.
23324 Redisplay runs this hook before it redisplays the menu bar.
23325 This is used to update submenus such as Buffers,
23326 whose contents depend on various data. */);
23327 Vmenu_bar_update_hook = Qnil;
23328
23329 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23330 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23331 inhibit_menubar_update = 0;
23332
23333 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23334 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23335 inhibit_eval_during_redisplay = 0;
23336
23337 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23338 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23339 inhibit_free_realized_faces = 0;
23340
23341 #if GLYPH_DEBUG
23342 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23343 doc: /* Inhibit try_window_id display optimization. */);
23344 inhibit_try_window_id = 0;
23345
23346 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23347 doc: /* Inhibit try_window_reusing display optimization. */);
23348 inhibit_try_window_reusing = 0;
23349
23350 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23351 doc: /* Inhibit try_cursor_movement display optimization. */);
23352 inhibit_try_cursor_movement = 0;
23353 #endif /* GLYPH_DEBUG */
23354 }
23355
23356
23357 /* Initialize this module when Emacs starts. */
23358
23359 void
23360 init_xdisp ()
23361 {
23362 Lisp_Object root_window;
23363 struct window *mini_w;
23364
23365 current_header_line_height = current_mode_line_height = -1;
23366
23367 CHARPOS (this_line_start_pos) = 0;
23368
23369 mini_w = XWINDOW (minibuf_window);
23370 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23371
23372 if (!noninteractive)
23373 {
23374 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23375 int i;
23376
23377 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23378 set_window_height (root_window,
23379 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23380 0);
23381 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23382 set_window_height (minibuf_window, 1, 0);
23383
23384 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23385 mini_w->total_cols = make_number (FRAME_COLS (f));
23386
23387 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23388 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23389 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23390
23391 /* The default ellipsis glyphs `...'. */
23392 for (i = 0; i < 3; ++i)
23393 default_invis_vector[i] = make_number ('.');
23394 }
23395
23396 {
23397 /* Allocate the buffer for frame titles.
23398 Also used for `format-mode-line'. */
23399 int size = 100;
23400 mode_line_noprop_buf = (char *) xmalloc (size);
23401 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23402 mode_line_noprop_ptr = mode_line_noprop_buf;
23403 mode_line_target = MODE_LINE_DISPLAY;
23404 }
23405
23406 help_echo_showing_p = 0;
23407 }
23408
23409
23410 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23411 (do not change this comment) */