]> code.delx.au - gnu-emacs/blob - src/xdisp.c
*** empty log message ***
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
203
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
207
208 #define INFINITY 10000000
209
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
215
216 extern int interrupt_input;
217 extern int command_loop_level;
218
219 extern Lisp_Object do_mouse_tracking;
220
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
223
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
232
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
242
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
248
249 Lisp_Object Qrisky_local_variable;
250
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
253
254 /* Functions called to fontify regions of text. */
255
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
258
259 /* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_autoselect_window;
262
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
265
266 int auto_raise_tool_bar_buttons_p;
267
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
269
270 int make_cursor_line_fully_visible_p;
271
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
275
276 Lisp_Object Vtool_bar_border;
277
278 /* Margin around tool bar buttons in pixels. */
279
280 Lisp_Object Vtool_bar_button_margin;
281
282 /* Thickness of shadow to draw around tool bar buttons. */
283
284 EMACS_INT tool_bar_button_relief;
285
286 /* Non-zero means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain. */
288
289 int auto_resize_tool_bars_p;
290
291 /* Non-zero means draw block and hollow cursor as wide as the glyph
292 under it. For example, if a block cursor is over a tab, it will be
293 drawn as wide as that tab on the display. */
294
295 int x_stretch_cursor_p;
296
297 /* Non-nil means don't actually do any redisplay. */
298
299 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
300
301 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
302
303 int inhibit_eval_during_redisplay;
304
305 /* Names of text properties relevant for redisplay. */
306
307 Lisp_Object Qdisplay;
308 extern Lisp_Object Qface, Qinvisible, Qwidth;
309
310 /* Symbols used in text property values. */
311
312 Lisp_Object Vdisplay_pixels_per_inch;
313 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
314 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
315 Lisp_Object Qslice;
316 Lisp_Object Qcenter;
317 Lisp_Object Qmargin, Qpointer;
318 Lisp_Object Qline_height;
319 extern Lisp_Object Qheight;
320 extern Lisp_Object QCwidth, QCheight, QCascent;
321 extern Lisp_Object Qscroll_bar;
322 extern Lisp_Object Qcursor;
323
324 /* Non-nil means highlight trailing whitespace. */
325
326 Lisp_Object Vshow_trailing_whitespace;
327
328 /* Non-nil means escape non-break space and hyphens. */
329
330 Lisp_Object Vnobreak_char_display;
331
332 #ifdef HAVE_WINDOW_SYSTEM
333 extern Lisp_Object Voverflow_newline_into_fringe;
334
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
337
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P (it->f) \
341 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
342 && it->current_x == it->last_visible_x)
343
344 #endif /* HAVE_WINDOW_SYSTEM */
345
346 /* Non-nil means show the text cursor in void text areas
347 i.e. in blank areas after eol and eob. This used to be
348 the default in 21.3. */
349
350 Lisp_Object Vvoid_text_area_pointer;
351
352 /* Name of the face used to highlight trailing whitespace. */
353
354 Lisp_Object Qtrailing_whitespace;
355
356 /* Name and number of the face used to highlight escape glyphs. */
357
358 Lisp_Object Qescape_glyph;
359
360 /* Name and number of the face used to highlight non-breaking spaces. */
361
362 Lisp_Object Qnobreak_space;
363
364 /* The symbol `image' which is the car of the lists used to represent
365 images in Lisp. */
366
367 Lisp_Object Qimage;
368
369 /* The image map types. */
370 Lisp_Object QCmap, QCpointer;
371 Lisp_Object Qrect, Qcircle, Qpoly;
372
373 /* Non-zero means print newline to stdout before next mini-buffer
374 message. */
375
376 int noninteractive_need_newline;
377
378 /* Non-zero means print newline to message log before next message. */
379
380 static int message_log_need_newline;
381
382 /* Three markers that message_dolog uses.
383 It could allocate them itself, but that causes trouble
384 in handling memory-full errors. */
385 static Lisp_Object message_dolog_marker1;
386 static Lisp_Object message_dolog_marker2;
387 static Lisp_Object message_dolog_marker3;
388 \f
389 /* The buffer position of the first character appearing entirely or
390 partially on the line of the selected window which contains the
391 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
392 redisplay optimization in redisplay_internal. */
393
394 static struct text_pos this_line_start_pos;
395
396 /* Number of characters past the end of the line above, including the
397 terminating newline. */
398
399 static struct text_pos this_line_end_pos;
400
401 /* The vertical positions and the height of this line. */
402
403 static int this_line_vpos;
404 static int this_line_y;
405 static int this_line_pixel_height;
406
407 /* X position at which this display line starts. Usually zero;
408 negative if first character is partially visible. */
409
410 static int this_line_start_x;
411
412 /* Buffer that this_line_.* variables are referring to. */
413
414 static struct buffer *this_line_buffer;
415
416 /* Nonzero means truncate lines in all windows less wide than the
417 frame. */
418
419 int truncate_partial_width_windows;
420
421 /* A flag to control how to display unibyte 8-bit character. */
422
423 int unibyte_display_via_language_environment;
424
425 /* Nonzero means we have more than one non-mini-buffer-only frame.
426 Not guaranteed to be accurate except while parsing
427 frame-title-format. */
428
429 int multiple_frames;
430
431 Lisp_Object Vglobal_mode_string;
432
433
434 /* List of variables (symbols) which hold markers for overlay arrows.
435 The symbols on this list are examined during redisplay to determine
436 where to display overlay arrows. */
437
438 Lisp_Object Voverlay_arrow_variable_list;
439
440 /* Marker for where to display an arrow on top of the buffer text. */
441
442 Lisp_Object Voverlay_arrow_position;
443
444 /* String to display for the arrow. Only used on terminal frames. */
445
446 Lisp_Object Voverlay_arrow_string;
447
448 /* Values of those variables at last redisplay are stored as
449 properties on `overlay-arrow-position' symbol. However, if
450 Voverlay_arrow_position is a marker, last-arrow-position is its
451 numerical position. */
452
453 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
454
455 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
456 properties on a symbol in overlay-arrow-variable-list. */
457
458 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
459
460 /* Like mode-line-format, but for the title bar on a visible frame. */
461
462 Lisp_Object Vframe_title_format;
463
464 /* Like mode-line-format, but for the title bar on an iconified frame. */
465
466 Lisp_Object Vicon_title_format;
467
468 /* List of functions to call when a window's size changes. These
469 functions get one arg, a frame on which one or more windows' sizes
470 have changed. */
471
472 static Lisp_Object Vwindow_size_change_functions;
473
474 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
475
476 /* Nonzero if an overlay arrow has been displayed in this window. */
477
478 static int overlay_arrow_seen;
479
480 /* Nonzero means highlight the region even in nonselected windows. */
481
482 int highlight_nonselected_windows;
483
484 /* If cursor motion alone moves point off frame, try scrolling this
485 many lines up or down if that will bring it back. */
486
487 static EMACS_INT scroll_step;
488
489 /* Nonzero means scroll just far enough to bring point back on the
490 screen, when appropriate. */
491
492 static EMACS_INT scroll_conservatively;
493
494 /* Recenter the window whenever point gets within this many lines of
495 the top or bottom of the window. This value is translated into a
496 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
497 that there is really a fixed pixel height scroll margin. */
498
499 EMACS_INT scroll_margin;
500
501 /* Number of windows showing the buffer of the selected window (or
502 another buffer with the same base buffer). keyboard.c refers to
503 this. */
504
505 int buffer_shared;
506
507 /* Vector containing glyphs for an ellipsis `...'. */
508
509 static Lisp_Object default_invis_vector[3];
510
511 /* Zero means display the mode-line/header-line/menu-bar in the default face
512 (this slightly odd definition is for compatibility with previous versions
513 of emacs), non-zero means display them using their respective faces.
514
515 This variable is deprecated. */
516
517 int mode_line_inverse_video;
518
519 /* Prompt to display in front of the mini-buffer contents. */
520
521 Lisp_Object minibuf_prompt;
522
523 /* Width of current mini-buffer prompt. Only set after display_line
524 of the line that contains the prompt. */
525
526 int minibuf_prompt_width;
527
528 /* This is the window where the echo area message was displayed. It
529 is always a mini-buffer window, but it may not be the same window
530 currently active as a mini-buffer. */
531
532 Lisp_Object echo_area_window;
533
534 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
535 pushes the current message and the value of
536 message_enable_multibyte on the stack, the function restore_message
537 pops the stack and displays MESSAGE again. */
538
539 Lisp_Object Vmessage_stack;
540
541 /* Nonzero means multibyte characters were enabled when the echo area
542 message was specified. */
543
544 int message_enable_multibyte;
545
546 /* Nonzero if we should redraw the mode lines on the next redisplay. */
547
548 int update_mode_lines;
549
550 /* Nonzero if window sizes or contents have changed since last
551 redisplay that finished. */
552
553 int windows_or_buffers_changed;
554
555 /* Nonzero means a frame's cursor type has been changed. */
556
557 int cursor_type_changed;
558
559 /* Nonzero after display_mode_line if %l was used and it displayed a
560 line number. */
561
562 int line_number_displayed;
563
564 /* Maximum buffer size for which to display line numbers. */
565
566 Lisp_Object Vline_number_display_limit;
567
568 /* Line width to consider when repositioning for line number display. */
569
570 static EMACS_INT line_number_display_limit_width;
571
572 /* Number of lines to keep in the message log buffer. t means
573 infinite. nil means don't log at all. */
574
575 Lisp_Object Vmessage_log_max;
576
577 /* The name of the *Messages* buffer, a string. */
578
579 static Lisp_Object Vmessages_buffer_name;
580
581 /* Index 0 is the buffer that holds the current (desired) echo area message,
582 or nil if none is desired right now.
583
584 Index 1 is the buffer that holds the previously displayed echo area message,
585 or nil to indicate no message. This is normally what's on the screen now.
586
587 These two can point to the same buffer. That happens when the last
588 message output by the user (or made by echoing) has been displayed. */
589
590 Lisp_Object echo_area_buffer[2];
591
592 /* Permanent pointers to the two buffers that are used for echo area
593 purposes. Once the two buffers are made, and their pointers are
594 placed here, these two slots remain unchanged unless those buffers
595 need to be created afresh. */
596
597 static Lisp_Object echo_buffer[2];
598
599 /* A vector saved used in with_area_buffer to reduce consing. */
600
601 static Lisp_Object Vwith_echo_area_save_vector;
602
603 /* Non-zero means display_echo_area should display the last echo area
604 message again. Set by redisplay_preserve_echo_area. */
605
606 static int display_last_displayed_message_p;
607
608 /* Nonzero if echo area is being used by print; zero if being used by
609 message. */
610
611 int message_buf_print;
612
613 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
614
615 Lisp_Object Qinhibit_menubar_update;
616 int inhibit_menubar_update;
617
618 /* Maximum height for resizing mini-windows. Either a float
619 specifying a fraction of the available height, or an integer
620 specifying a number of lines. */
621
622 Lisp_Object Vmax_mini_window_height;
623
624 /* Non-zero means messages should be displayed with truncated
625 lines instead of being continued. */
626
627 int message_truncate_lines;
628 Lisp_Object Qmessage_truncate_lines;
629
630 /* Set to 1 in clear_message to make redisplay_internal aware
631 of an emptied echo area. */
632
633 static int message_cleared_p;
634
635 /* How to blink the default frame cursor off. */
636 Lisp_Object Vblink_cursor_alist;
637
638 /* A scratch glyph row with contents used for generating truncation
639 glyphs. Also used in direct_output_for_insert. */
640
641 #define MAX_SCRATCH_GLYPHS 100
642 struct glyph_row scratch_glyph_row;
643 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
644
645 /* Ascent and height of the last line processed by move_it_to. */
646
647 static int last_max_ascent, last_height;
648
649 /* Non-zero if there's a help-echo in the echo area. */
650
651 int help_echo_showing_p;
652
653 /* If >= 0, computed, exact values of mode-line and header-line height
654 to use in the macros CURRENT_MODE_LINE_HEIGHT and
655 CURRENT_HEADER_LINE_HEIGHT. */
656
657 int current_mode_line_height, current_header_line_height;
658
659 /* The maximum distance to look ahead for text properties. Values
660 that are too small let us call compute_char_face and similar
661 functions too often which is expensive. Values that are too large
662 let us call compute_char_face and alike too often because we
663 might not be interested in text properties that far away. */
664
665 #define TEXT_PROP_DISTANCE_LIMIT 100
666
667 #if GLYPH_DEBUG
668
669 /* Variables to turn off display optimizations from Lisp. */
670
671 int inhibit_try_window_id, inhibit_try_window_reusing;
672 int inhibit_try_cursor_movement;
673
674 /* Non-zero means print traces of redisplay if compiled with
675 GLYPH_DEBUG != 0. */
676
677 int trace_redisplay_p;
678
679 #endif /* GLYPH_DEBUG */
680
681 #ifdef DEBUG_TRACE_MOVE
682 /* Non-zero means trace with TRACE_MOVE to stderr. */
683 int trace_move;
684
685 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
686 #else
687 #define TRACE_MOVE(x) (void) 0
688 #endif
689
690 /* Non-zero means automatically scroll windows horizontally to make
691 point visible. */
692
693 int automatic_hscrolling_p;
694
695 /* How close to the margin can point get before the window is scrolled
696 horizontally. */
697 EMACS_INT hscroll_margin;
698
699 /* How much to scroll horizontally when point is inside the above margin. */
700 Lisp_Object Vhscroll_step;
701
702 /* The variable `resize-mini-windows'. If nil, don't resize
703 mini-windows. If t, always resize them to fit the text they
704 display. If `grow-only', let mini-windows grow only until they
705 become empty. */
706
707 Lisp_Object Vresize_mini_windows;
708
709 /* Buffer being redisplayed -- for redisplay_window_error. */
710
711 struct buffer *displayed_buffer;
712
713 /* Value returned from text property handlers (see below). */
714
715 enum prop_handled
716 {
717 HANDLED_NORMALLY,
718 HANDLED_RECOMPUTE_PROPS,
719 HANDLED_OVERLAY_STRING_CONSUMED,
720 HANDLED_RETURN
721 };
722
723 /* A description of text properties that redisplay is interested
724 in. */
725
726 struct props
727 {
728 /* The name of the property. */
729 Lisp_Object *name;
730
731 /* A unique index for the property. */
732 enum prop_idx idx;
733
734 /* A handler function called to set up iterator IT from the property
735 at IT's current position. Value is used to steer handle_stop. */
736 enum prop_handled (*handler) P_ ((struct it *it));
737 };
738
739 static enum prop_handled handle_face_prop P_ ((struct it *));
740 static enum prop_handled handle_invisible_prop P_ ((struct it *));
741 static enum prop_handled handle_display_prop P_ ((struct it *));
742 static enum prop_handled handle_composition_prop P_ ((struct it *));
743 static enum prop_handled handle_overlay_change P_ ((struct it *));
744 static enum prop_handled handle_fontified_prop P_ ((struct it *));
745
746 /* Properties handled by iterators. */
747
748 static struct props it_props[] =
749 {
750 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
751 /* Handle `face' before `display' because some sub-properties of
752 `display' need to know the face. */
753 {&Qface, FACE_PROP_IDX, handle_face_prop},
754 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
755 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
756 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
757 {NULL, 0, NULL}
758 };
759
760 /* Value is the position described by X. If X is a marker, value is
761 the marker_position of X. Otherwise, value is X. */
762
763 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
764
765 /* Enumeration returned by some move_it_.* functions internally. */
766
767 enum move_it_result
768 {
769 /* Not used. Undefined value. */
770 MOVE_UNDEFINED,
771
772 /* Move ended at the requested buffer position or ZV. */
773 MOVE_POS_MATCH_OR_ZV,
774
775 /* Move ended at the requested X pixel position. */
776 MOVE_X_REACHED,
777
778 /* Move within a line ended at the end of a line that must be
779 continued. */
780 MOVE_LINE_CONTINUED,
781
782 /* Move within a line ended at the end of a line that would
783 be displayed truncated. */
784 MOVE_LINE_TRUNCATED,
785
786 /* Move within a line ended at a line end. */
787 MOVE_NEWLINE_OR_CR
788 };
789
790 /* This counter is used to clear the face cache every once in a while
791 in redisplay_internal. It is incremented for each redisplay.
792 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
793 cleared. */
794
795 #define CLEAR_FACE_CACHE_COUNT 500
796 static int clear_face_cache_count;
797
798 /* Similarly for the image cache. */
799
800 #ifdef HAVE_WINDOW_SYSTEM
801 #define CLEAR_IMAGE_CACHE_COUNT 101
802 static int clear_image_cache_count;
803 #endif
804
805 /* Record the previous terminal frame we displayed. */
806
807 static struct frame *previous_terminal_frame;
808
809 /* Non-zero while redisplay_internal is in progress. */
810
811 int redisplaying_p;
812
813 /* Non-zero means don't free realized faces. Bound while freeing
814 realized faces is dangerous because glyph matrices might still
815 reference them. */
816
817 int inhibit_free_realized_faces;
818 Lisp_Object Qinhibit_free_realized_faces;
819
820 /* If a string, XTread_socket generates an event to display that string.
821 (The display is done in read_char.) */
822
823 Lisp_Object help_echo_string;
824 Lisp_Object help_echo_window;
825 Lisp_Object help_echo_object;
826 int help_echo_pos;
827
828 /* Temporary variable for XTread_socket. */
829
830 Lisp_Object previous_help_echo_string;
831
832 /* Null glyph slice */
833
834 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
835
836 \f
837 /* Function prototypes. */
838
839 static void setup_for_ellipsis P_ ((struct it *, int));
840 static void mark_window_display_accurate_1 P_ ((struct window *, int));
841 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
842 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
843 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
844 static int redisplay_mode_lines P_ ((Lisp_Object, int));
845 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
846
847 #if 0
848 static int invisible_text_between_p P_ ((struct it *, int, int));
849 #endif
850
851 static void pint2str P_ ((char *, int, int));
852 static void pint2hrstr P_ ((char *, int, int));
853 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
854 struct text_pos));
855 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
856 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
857 static void store_mode_line_noprop_char P_ ((char));
858 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
859 static void x_consider_frame_title P_ ((Lisp_Object));
860 static void handle_stop P_ ((struct it *));
861 static int tool_bar_lines_needed P_ ((struct frame *, int *));
862 static int single_display_spec_intangible_p P_ ((Lisp_Object));
863 static void ensure_echo_area_buffers P_ ((void));
864 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
865 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
866 static int with_echo_area_buffer P_ ((struct window *, int,
867 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
868 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static void clear_garbaged_frames P_ ((void));
870 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
872 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
873 static int display_echo_area P_ ((struct window *));
874 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
875 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
876 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
877 static int string_char_and_length P_ ((const unsigned char *, int, int *));
878 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
879 struct text_pos));
880 static int compute_window_start_on_continuation_line P_ ((struct window *));
881 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
882 static void insert_left_trunc_glyphs P_ ((struct it *));
883 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
884 Lisp_Object));
885 static void extend_face_to_end_of_line P_ ((struct it *));
886 static int append_space_for_newline P_ ((struct it *, int));
887 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
888 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
889 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
890 static int trailing_whitespace_p P_ ((int));
891 static int message_log_check_duplicate P_ ((int, int, int, int));
892 static void push_it P_ ((struct it *));
893 static void pop_it P_ ((struct it *));
894 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
895 static void select_frame_for_redisplay P_ ((Lisp_Object));
896 static void redisplay_internal P_ ((int));
897 static int echo_area_display P_ ((int));
898 static void redisplay_windows P_ ((Lisp_Object));
899 static void redisplay_window P_ ((Lisp_Object, int));
900 static Lisp_Object redisplay_window_error ();
901 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
902 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
903 static void update_menu_bar P_ ((struct frame *, int));
904 static int try_window_reusing_current_matrix P_ ((struct window *));
905 static int try_window_id P_ ((struct window *));
906 static int display_line P_ ((struct it *));
907 static int display_mode_lines P_ ((struct window *));
908 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
909 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
910 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
911 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
912 static void display_menu_bar P_ ((struct window *));
913 static int display_count_lines P_ ((int, int, int, int, int *));
914 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
915 int, int, struct it *, int, int, int, int));
916 static void compute_line_metrics P_ ((struct it *));
917 static void run_redisplay_end_trigger_hook P_ ((struct it *));
918 static int get_overlay_strings P_ ((struct it *, int));
919 static int get_overlay_strings_1 P_ ((struct it *, int, int));
920 static void next_overlay_string P_ ((struct it *));
921 static void reseat P_ ((struct it *, struct text_pos, int));
922 static void reseat_1 P_ ((struct it *, struct text_pos, int));
923 static void back_to_previous_visible_line_start P_ ((struct it *));
924 void reseat_at_previous_visible_line_start P_ ((struct it *));
925 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
926 static int next_element_from_ellipsis P_ ((struct it *));
927 static int next_element_from_display_vector P_ ((struct it *));
928 static int next_element_from_string P_ ((struct it *));
929 static int next_element_from_c_string P_ ((struct it *));
930 static int next_element_from_buffer P_ ((struct it *));
931 static int next_element_from_composition P_ ((struct it *));
932 static int next_element_from_image P_ ((struct it *));
933 static int next_element_from_stretch P_ ((struct it *));
934 static void load_overlay_strings P_ ((struct it *, int));
935 static int init_from_display_pos P_ ((struct it *, struct window *,
936 struct display_pos *));
937 static void reseat_to_string P_ ((struct it *, unsigned char *,
938 Lisp_Object, int, int, int, int));
939 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
940 int, int, int));
941 void move_it_vertically_backward P_ ((struct it *, int));
942 static void init_to_row_start P_ ((struct it *, struct window *,
943 struct glyph_row *));
944 static int init_to_row_end P_ ((struct it *, struct window *,
945 struct glyph_row *));
946 static void back_to_previous_line_start P_ ((struct it *));
947 static int forward_to_next_line_start P_ ((struct it *, int *));
948 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
949 Lisp_Object, int));
950 static struct text_pos string_pos P_ ((int, Lisp_Object));
951 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
952 static int number_of_chars P_ ((unsigned char *, int));
953 static void compute_stop_pos P_ ((struct it *));
954 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
955 Lisp_Object));
956 static int face_before_or_after_it_pos P_ ((struct it *, int));
957 static int next_overlay_change P_ ((int));
958 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
959 Lisp_Object, struct text_pos *,
960 int));
961 static int underlying_face_id P_ ((struct it *));
962 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
963 struct window *));
964
965 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
966 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
967
968 #ifdef HAVE_WINDOW_SYSTEM
969
970 static void update_tool_bar P_ ((struct frame *, int));
971 static void build_desired_tool_bar_string P_ ((struct frame *f));
972 static int redisplay_tool_bar P_ ((struct frame *));
973 static void display_tool_bar_line P_ ((struct it *, int));
974 static void notice_overwritten_cursor P_ ((struct window *,
975 enum glyph_row_area,
976 int, int, int, int));
977
978
979
980 #endif /* HAVE_WINDOW_SYSTEM */
981
982 \f
983 /***********************************************************************
984 Window display dimensions
985 ***********************************************************************/
986
987 /* Return the bottom boundary y-position for text lines in window W.
988 This is the first y position at which a line cannot start.
989 It is relative to the top of the window.
990
991 This is the height of W minus the height of a mode line, if any. */
992
993 INLINE int
994 window_text_bottom_y (w)
995 struct window *w;
996 {
997 int height = WINDOW_TOTAL_HEIGHT (w);
998
999 if (WINDOW_WANTS_MODELINE_P (w))
1000 height -= CURRENT_MODE_LINE_HEIGHT (w);
1001 return height;
1002 }
1003
1004 /* Return the pixel width of display area AREA of window W. AREA < 0
1005 means return the total width of W, not including fringes to
1006 the left and right of the window. */
1007
1008 INLINE int
1009 window_box_width (w, area)
1010 struct window *w;
1011 int area;
1012 {
1013 int cols = XFASTINT (w->total_cols);
1014 int pixels = 0;
1015
1016 if (!w->pseudo_window_p)
1017 {
1018 cols -= WINDOW_SCROLL_BAR_COLS (w);
1019
1020 if (area == TEXT_AREA)
1021 {
1022 if (INTEGERP (w->left_margin_cols))
1023 cols -= XFASTINT (w->left_margin_cols);
1024 if (INTEGERP (w->right_margin_cols))
1025 cols -= XFASTINT (w->right_margin_cols);
1026 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1027 }
1028 else if (area == LEFT_MARGIN_AREA)
1029 {
1030 cols = (INTEGERP (w->left_margin_cols)
1031 ? XFASTINT (w->left_margin_cols) : 0);
1032 pixels = 0;
1033 }
1034 else if (area == RIGHT_MARGIN_AREA)
1035 {
1036 cols = (INTEGERP (w->right_margin_cols)
1037 ? XFASTINT (w->right_margin_cols) : 0);
1038 pixels = 0;
1039 }
1040 }
1041
1042 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1043 }
1044
1045
1046 /* Return the pixel height of the display area of window W, not
1047 including mode lines of W, if any. */
1048
1049 INLINE int
1050 window_box_height (w)
1051 struct window *w;
1052 {
1053 struct frame *f = XFRAME (w->frame);
1054 int height = WINDOW_TOTAL_HEIGHT (w);
1055
1056 xassert (height >= 0);
1057
1058 /* Note: the code below that determines the mode-line/header-line
1059 height is essentially the same as that contained in the macro
1060 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1061 the appropriate glyph row has its `mode_line_p' flag set,
1062 and if it doesn't, uses estimate_mode_line_height instead. */
1063
1064 if (WINDOW_WANTS_MODELINE_P (w))
1065 {
1066 struct glyph_row *ml_row
1067 = (w->current_matrix && w->current_matrix->rows
1068 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1069 : 0);
1070 if (ml_row && ml_row->mode_line_p)
1071 height -= ml_row->height;
1072 else
1073 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1074 }
1075
1076 if (WINDOW_WANTS_HEADER_LINE_P (w))
1077 {
1078 struct glyph_row *hl_row
1079 = (w->current_matrix && w->current_matrix->rows
1080 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1081 : 0);
1082 if (hl_row && hl_row->mode_line_p)
1083 height -= hl_row->height;
1084 else
1085 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1086 }
1087
1088 /* With a very small font and a mode-line that's taller than
1089 default, we might end up with a negative height. */
1090 return max (0, height);
1091 }
1092
1093 /* Return the window-relative coordinate of the left edge of display
1094 area AREA of window W. AREA < 0 means return the left edge of the
1095 whole window, to the right of the left fringe of W. */
1096
1097 INLINE int
1098 window_box_left_offset (w, area)
1099 struct window *w;
1100 int area;
1101 {
1102 int x;
1103
1104 if (w->pseudo_window_p)
1105 return 0;
1106
1107 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1108
1109 if (area == TEXT_AREA)
1110 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1111 + window_box_width (w, LEFT_MARGIN_AREA));
1112 else if (area == RIGHT_MARGIN_AREA)
1113 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1114 + window_box_width (w, LEFT_MARGIN_AREA)
1115 + window_box_width (w, TEXT_AREA)
1116 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1117 ? 0
1118 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1119 else if (area == LEFT_MARGIN_AREA
1120 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1121 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1122
1123 return x;
1124 }
1125
1126
1127 /* Return the window-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the left edge of the
1129 whole window, to the left of the right fringe of W. */
1130
1131 INLINE int
1132 window_box_right_offset (w, area)
1133 struct window *w;
1134 int area;
1135 {
1136 return window_box_left_offset (w, area) + window_box_width (w, area);
1137 }
1138
1139 /* Return the frame-relative coordinate of the left edge of display
1140 area AREA of window W. AREA < 0 means return the left edge of the
1141 whole window, to the right of the left fringe of W. */
1142
1143 INLINE int
1144 window_box_left (w, area)
1145 struct window *w;
1146 int area;
1147 {
1148 struct frame *f = XFRAME (w->frame);
1149 int x;
1150
1151 if (w->pseudo_window_p)
1152 return FRAME_INTERNAL_BORDER_WIDTH (f);
1153
1154 x = (WINDOW_LEFT_EDGE_X (w)
1155 + window_box_left_offset (w, area));
1156
1157 return x;
1158 }
1159
1160
1161 /* Return the frame-relative coordinate of the right edge of display
1162 area AREA of window W. AREA < 0 means return the left edge of the
1163 whole window, to the left of the right fringe of W. */
1164
1165 INLINE int
1166 window_box_right (w, area)
1167 struct window *w;
1168 int area;
1169 {
1170 return window_box_left (w, area) + window_box_width (w, area);
1171 }
1172
1173 /* Get the bounding box of the display area AREA of window W, without
1174 mode lines, in frame-relative coordinates. AREA < 0 means the
1175 whole window, not including the left and right fringes of
1176 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1177 coordinates of the upper-left corner of the box. Return in
1178 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1179
1180 INLINE void
1181 window_box (w, area, box_x, box_y, box_width, box_height)
1182 struct window *w;
1183 int area;
1184 int *box_x, *box_y, *box_width, *box_height;
1185 {
1186 if (box_width)
1187 *box_width = window_box_width (w, area);
1188 if (box_height)
1189 *box_height = window_box_height (w);
1190 if (box_x)
1191 *box_x = window_box_left (w, area);
1192 if (box_y)
1193 {
1194 *box_y = WINDOW_TOP_EDGE_Y (w);
1195 if (WINDOW_WANTS_HEADER_LINE_P (w))
1196 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1197 }
1198 }
1199
1200
1201 /* Get the bounding box of the display area AREA of window W, without
1202 mode lines. AREA < 0 means the whole window, not including the
1203 left and right fringe of the window. Return in *TOP_LEFT_X
1204 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1205 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1206 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1207 box. */
1208
1209 INLINE void
1210 window_box_edges (w, area, top_left_x, top_left_y,
1211 bottom_right_x, bottom_right_y)
1212 struct window *w;
1213 int area;
1214 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1215 {
1216 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1217 bottom_right_y);
1218 *bottom_right_x += *top_left_x;
1219 *bottom_right_y += *top_left_y;
1220 }
1221
1222
1223 \f
1224 /***********************************************************************
1225 Utilities
1226 ***********************************************************************/
1227
1228 /* Return the bottom y-position of the line the iterator IT is in.
1229 This can modify IT's settings. */
1230
1231 int
1232 line_bottom_y (it)
1233 struct it *it;
1234 {
1235 int line_height = it->max_ascent + it->max_descent;
1236 int line_top_y = it->current_y;
1237
1238 if (line_height == 0)
1239 {
1240 if (last_height)
1241 line_height = last_height;
1242 else if (IT_CHARPOS (*it) < ZV)
1243 {
1244 move_it_by_lines (it, 1, 1);
1245 line_height = (it->max_ascent || it->max_descent
1246 ? it->max_ascent + it->max_descent
1247 : last_height);
1248 }
1249 else
1250 {
1251 struct glyph_row *row = it->glyph_row;
1252
1253 /* Use the default character height. */
1254 it->glyph_row = NULL;
1255 it->what = IT_CHARACTER;
1256 it->c = ' ';
1257 it->len = 1;
1258 PRODUCE_GLYPHS (it);
1259 line_height = it->ascent + it->descent;
1260 it->glyph_row = row;
1261 }
1262 }
1263
1264 return line_top_y + line_height;
1265 }
1266
1267
1268 /* Return 1 if position CHARPOS is visible in window W.
1269 If visible, set *X and *Y to pixel coordinates of top left corner.
1270 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1271 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1272 and header-lines heights. */
1273
1274 int
1275 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1276 struct window *w;
1277 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1278 {
1279 struct it it;
1280 struct text_pos top;
1281 int visible_p = 0;
1282 struct buffer *old_buffer = NULL;
1283
1284 if (noninteractive)
1285 return visible_p;
1286
1287 if (XBUFFER (w->buffer) != current_buffer)
1288 {
1289 old_buffer = current_buffer;
1290 set_buffer_internal_1 (XBUFFER (w->buffer));
1291 }
1292
1293 SET_TEXT_POS_FROM_MARKER (top, w->start);
1294
1295 /* Compute exact mode line heights, if requested. */
1296 if (exact_mode_line_heights_p)
1297 {
1298 if (WINDOW_WANTS_MODELINE_P (w))
1299 current_mode_line_height
1300 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1301 current_buffer->mode_line_format);
1302
1303 if (WINDOW_WANTS_HEADER_LINE_P (w))
1304 current_header_line_height
1305 = display_mode_line (w, HEADER_LINE_FACE_ID,
1306 current_buffer->header_line_format);
1307 }
1308
1309 start_display (&it, w, top);
1310 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1311 MOVE_TO_POS | MOVE_TO_Y);
1312
1313 /* Note that we may overshoot because of invisible text. */
1314 if (IT_CHARPOS (it) >= charpos)
1315 {
1316 int top_x = it.current_x;
1317 int top_y = it.current_y;
1318 int bottom_y = (last_height = 0, line_bottom_y (&it));
1319 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1320
1321 if (top_y < window_top_y)
1322 visible_p = bottom_y > window_top_y;
1323 else if (top_y < it.last_visible_y)
1324 visible_p = 1;
1325 if (visible_p)
1326 {
1327 *x = top_x;
1328 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1329 *rtop = max (0, window_top_y - top_y);
1330 *rbot = max (0, bottom_y - it.last_visible_y);
1331 }
1332 }
1333 else
1334 {
1335 struct it it2;
1336
1337 it2 = it;
1338 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1339 move_it_by_lines (&it, 1, 0);
1340 if (charpos < IT_CHARPOS (it))
1341 {
1342 visible_p = 1;
1343 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1344 *x = it2.current_x;
1345 *y = it2.current_y + it2.max_ascent - it2.ascent;
1346 *rtop = max (0, -it2.current_y);
1347 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1348 - it.last_visible_y));
1349 }
1350 }
1351
1352 if (old_buffer)
1353 set_buffer_internal_1 (old_buffer);
1354
1355 current_header_line_height = current_mode_line_height = -1;
1356
1357 if (visible_p && XFASTINT (w->hscroll) > 0)
1358 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1359
1360 return visible_p;
1361 }
1362
1363
1364 /* Return the next character from STR which is MAXLEN bytes long.
1365 Return in *LEN the length of the character. This is like
1366 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1367 we find one, we return a `?', but with the length of the invalid
1368 character. */
1369
1370 static INLINE int
1371 string_char_and_length (str, maxlen, len)
1372 const unsigned char *str;
1373 int maxlen, *len;
1374 {
1375 int c;
1376
1377 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1378 if (!CHAR_VALID_P (c, 1))
1379 /* We may not change the length here because other places in Emacs
1380 don't use this function, i.e. they silently accept invalid
1381 characters. */
1382 c = '?';
1383
1384 return c;
1385 }
1386
1387
1388
1389 /* Given a position POS containing a valid character and byte position
1390 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1391
1392 static struct text_pos
1393 string_pos_nchars_ahead (pos, string, nchars)
1394 struct text_pos pos;
1395 Lisp_Object string;
1396 int nchars;
1397 {
1398 xassert (STRINGP (string) && nchars >= 0);
1399
1400 if (STRING_MULTIBYTE (string))
1401 {
1402 int rest = SBYTES (string) - BYTEPOS (pos);
1403 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1404 int len;
1405
1406 while (nchars--)
1407 {
1408 string_char_and_length (p, rest, &len);
1409 p += len, rest -= len;
1410 xassert (rest >= 0);
1411 CHARPOS (pos) += 1;
1412 BYTEPOS (pos) += len;
1413 }
1414 }
1415 else
1416 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1417
1418 return pos;
1419 }
1420
1421
1422 /* Value is the text position, i.e. character and byte position,
1423 for character position CHARPOS in STRING. */
1424
1425 static INLINE struct text_pos
1426 string_pos (charpos, string)
1427 int charpos;
1428 Lisp_Object string;
1429 {
1430 struct text_pos pos;
1431 xassert (STRINGP (string));
1432 xassert (charpos >= 0);
1433 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1434 return pos;
1435 }
1436
1437
1438 /* Value is a text position, i.e. character and byte position, for
1439 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1440 means recognize multibyte characters. */
1441
1442 static struct text_pos
1443 c_string_pos (charpos, s, multibyte_p)
1444 int charpos;
1445 unsigned char *s;
1446 int multibyte_p;
1447 {
1448 struct text_pos pos;
1449
1450 xassert (s != NULL);
1451 xassert (charpos >= 0);
1452
1453 if (multibyte_p)
1454 {
1455 int rest = strlen (s), len;
1456
1457 SET_TEXT_POS (pos, 0, 0);
1458 while (charpos--)
1459 {
1460 string_char_and_length (s, rest, &len);
1461 s += len, rest -= len;
1462 xassert (rest >= 0);
1463 CHARPOS (pos) += 1;
1464 BYTEPOS (pos) += len;
1465 }
1466 }
1467 else
1468 SET_TEXT_POS (pos, charpos, charpos);
1469
1470 return pos;
1471 }
1472
1473
1474 /* Value is the number of characters in C string S. MULTIBYTE_P
1475 non-zero means recognize multibyte characters. */
1476
1477 static int
1478 number_of_chars (s, multibyte_p)
1479 unsigned char *s;
1480 int multibyte_p;
1481 {
1482 int nchars;
1483
1484 if (multibyte_p)
1485 {
1486 int rest = strlen (s), len;
1487 unsigned char *p = (unsigned char *) s;
1488
1489 for (nchars = 0; rest > 0; ++nchars)
1490 {
1491 string_char_and_length (p, rest, &len);
1492 rest -= len, p += len;
1493 }
1494 }
1495 else
1496 nchars = strlen (s);
1497
1498 return nchars;
1499 }
1500
1501
1502 /* Compute byte position NEWPOS->bytepos corresponding to
1503 NEWPOS->charpos. POS is a known position in string STRING.
1504 NEWPOS->charpos must be >= POS.charpos. */
1505
1506 static void
1507 compute_string_pos (newpos, pos, string)
1508 struct text_pos *newpos, pos;
1509 Lisp_Object string;
1510 {
1511 xassert (STRINGP (string));
1512 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1513
1514 if (STRING_MULTIBYTE (string))
1515 *newpos = string_pos_nchars_ahead (pos, string,
1516 CHARPOS (*newpos) - CHARPOS (pos));
1517 else
1518 BYTEPOS (*newpos) = CHARPOS (*newpos);
1519 }
1520
1521 /* EXPORT:
1522 Return an estimation of the pixel height of mode or top lines on
1523 frame F. FACE_ID specifies what line's height to estimate. */
1524
1525 int
1526 estimate_mode_line_height (f, face_id)
1527 struct frame *f;
1528 enum face_id face_id;
1529 {
1530 #ifdef HAVE_WINDOW_SYSTEM
1531 if (FRAME_WINDOW_P (f))
1532 {
1533 int height = FONT_HEIGHT (FRAME_FONT (f));
1534
1535 /* This function is called so early when Emacs starts that the face
1536 cache and mode line face are not yet initialized. */
1537 if (FRAME_FACE_CACHE (f))
1538 {
1539 struct face *face = FACE_FROM_ID (f, face_id);
1540 if (face)
1541 {
1542 if (face->font)
1543 height = FONT_HEIGHT (face->font);
1544 if (face->box_line_width > 0)
1545 height += 2 * face->box_line_width;
1546 }
1547 }
1548
1549 return height;
1550 }
1551 #endif
1552
1553 return 1;
1554 }
1555
1556 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1557 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1558 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1559 not force the value into range. */
1560
1561 void
1562 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1563 FRAME_PTR f;
1564 register int pix_x, pix_y;
1565 int *x, *y;
1566 NativeRectangle *bounds;
1567 int noclip;
1568 {
1569
1570 #ifdef HAVE_WINDOW_SYSTEM
1571 if (FRAME_WINDOW_P (f))
1572 {
1573 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1574 even for negative values. */
1575 if (pix_x < 0)
1576 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1577 if (pix_y < 0)
1578 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1579
1580 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1581 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1582
1583 if (bounds)
1584 STORE_NATIVE_RECT (*bounds,
1585 FRAME_COL_TO_PIXEL_X (f, pix_x),
1586 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1587 FRAME_COLUMN_WIDTH (f) - 1,
1588 FRAME_LINE_HEIGHT (f) - 1);
1589
1590 if (!noclip)
1591 {
1592 if (pix_x < 0)
1593 pix_x = 0;
1594 else if (pix_x > FRAME_TOTAL_COLS (f))
1595 pix_x = FRAME_TOTAL_COLS (f);
1596
1597 if (pix_y < 0)
1598 pix_y = 0;
1599 else if (pix_y > FRAME_LINES (f))
1600 pix_y = FRAME_LINES (f);
1601 }
1602 }
1603 #endif
1604
1605 *x = pix_x;
1606 *y = pix_y;
1607 }
1608
1609
1610 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1611 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1612 can't tell the positions because W's display is not up to date,
1613 return 0. */
1614
1615 int
1616 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1617 struct window *w;
1618 int hpos, vpos;
1619 int *frame_x, *frame_y;
1620 {
1621 #ifdef HAVE_WINDOW_SYSTEM
1622 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1623 {
1624 int success_p;
1625
1626 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1627 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1628
1629 if (display_completed)
1630 {
1631 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1632 struct glyph *glyph = row->glyphs[TEXT_AREA];
1633 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1634
1635 hpos = row->x;
1636 vpos = row->y;
1637 while (glyph < end)
1638 {
1639 hpos += glyph->pixel_width;
1640 ++glyph;
1641 }
1642
1643 /* If first glyph is partially visible, its first visible position is still 0. */
1644 if (hpos < 0)
1645 hpos = 0;
1646
1647 success_p = 1;
1648 }
1649 else
1650 {
1651 hpos = vpos = 0;
1652 success_p = 0;
1653 }
1654
1655 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1656 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1657 return success_p;
1658 }
1659 #endif
1660
1661 *frame_x = hpos;
1662 *frame_y = vpos;
1663 return 1;
1664 }
1665
1666
1667 #ifdef HAVE_WINDOW_SYSTEM
1668
1669 /* Find the glyph under window-relative coordinates X/Y in window W.
1670 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1671 strings. Return in *HPOS and *VPOS the row and column number of
1672 the glyph found. Return in *AREA the glyph area containing X.
1673 Value is a pointer to the glyph found or null if X/Y is not on
1674 text, or we can't tell because W's current matrix is not up to
1675 date. */
1676
1677 static struct glyph *
1678 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1679 struct window *w;
1680 int x, y;
1681 int *hpos, *vpos, *dx, *dy, *area;
1682 {
1683 struct glyph *glyph, *end;
1684 struct glyph_row *row = NULL;
1685 int x0, i;
1686
1687 /* Find row containing Y. Give up if some row is not enabled. */
1688 for (i = 0; i < w->current_matrix->nrows; ++i)
1689 {
1690 row = MATRIX_ROW (w->current_matrix, i);
1691 if (!row->enabled_p)
1692 return NULL;
1693 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1694 break;
1695 }
1696
1697 *vpos = i;
1698 *hpos = 0;
1699
1700 /* Give up if Y is not in the window. */
1701 if (i == w->current_matrix->nrows)
1702 return NULL;
1703
1704 /* Get the glyph area containing X. */
1705 if (w->pseudo_window_p)
1706 {
1707 *area = TEXT_AREA;
1708 x0 = 0;
1709 }
1710 else
1711 {
1712 if (x < window_box_left_offset (w, TEXT_AREA))
1713 {
1714 *area = LEFT_MARGIN_AREA;
1715 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1716 }
1717 else if (x < window_box_right_offset (w, TEXT_AREA))
1718 {
1719 *area = TEXT_AREA;
1720 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1721 }
1722 else
1723 {
1724 *area = RIGHT_MARGIN_AREA;
1725 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1726 }
1727 }
1728
1729 /* Find glyph containing X. */
1730 glyph = row->glyphs[*area];
1731 end = glyph + row->used[*area];
1732 x -= x0;
1733 while (glyph < end && x >= glyph->pixel_width)
1734 {
1735 x -= glyph->pixel_width;
1736 ++glyph;
1737 }
1738
1739 if (glyph == end)
1740 return NULL;
1741
1742 if (dx)
1743 {
1744 *dx = x;
1745 *dy = y - (row->y + row->ascent - glyph->ascent);
1746 }
1747
1748 *hpos = glyph - row->glyphs[*area];
1749 return glyph;
1750 }
1751
1752
1753 /* EXPORT:
1754 Convert frame-relative x/y to coordinates relative to window W.
1755 Takes pseudo-windows into account. */
1756
1757 void
1758 frame_to_window_pixel_xy (w, x, y)
1759 struct window *w;
1760 int *x, *y;
1761 {
1762 if (w->pseudo_window_p)
1763 {
1764 /* A pseudo-window is always full-width, and starts at the
1765 left edge of the frame, plus a frame border. */
1766 struct frame *f = XFRAME (w->frame);
1767 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1768 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1769 }
1770 else
1771 {
1772 *x -= WINDOW_LEFT_EDGE_X (w);
1773 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1774 }
1775 }
1776
1777 /* EXPORT:
1778 Return in RECTS[] at most N clipping rectangles for glyph string S.
1779 Return the number of stored rectangles. */
1780
1781 int
1782 get_glyph_string_clip_rects (s, rects, n)
1783 struct glyph_string *s;
1784 NativeRectangle *rects;
1785 int n;
1786 {
1787 XRectangle r;
1788
1789 if (n <= 0)
1790 return 0;
1791
1792 if (s->row->full_width_p)
1793 {
1794 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1795 r.x = WINDOW_LEFT_EDGE_X (s->w);
1796 r.width = WINDOW_TOTAL_WIDTH (s->w);
1797
1798 /* Unless displaying a mode or menu bar line, which are always
1799 fully visible, clip to the visible part of the row. */
1800 if (s->w->pseudo_window_p)
1801 r.height = s->row->visible_height;
1802 else
1803 r.height = s->height;
1804 }
1805 else
1806 {
1807 /* This is a text line that may be partially visible. */
1808 r.x = window_box_left (s->w, s->area);
1809 r.width = window_box_width (s->w, s->area);
1810 r.height = s->row->visible_height;
1811 }
1812
1813 if (s->clip_head)
1814 if (r.x < s->clip_head->x)
1815 {
1816 if (r.width >= s->clip_head->x - r.x)
1817 r.width -= s->clip_head->x - r.x;
1818 else
1819 r.width = 0;
1820 r.x = s->clip_head->x;
1821 }
1822 if (s->clip_tail)
1823 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1824 {
1825 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1826 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1827 else
1828 r.width = 0;
1829 }
1830
1831 /* If S draws overlapping rows, it's sufficient to use the top and
1832 bottom of the window for clipping because this glyph string
1833 intentionally draws over other lines. */
1834 if (s->for_overlaps)
1835 {
1836 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1837 r.height = window_text_bottom_y (s->w) - r.y;
1838
1839 /* Alas, the above simple strategy does not work for the
1840 environments with anti-aliased text: if the same text is
1841 drawn onto the same place multiple times, it gets thicker.
1842 If the overlap we are processing is for the erased cursor, we
1843 take the intersection with the rectagle of the cursor. */
1844 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1845 {
1846 XRectangle rc, r_save = r;
1847
1848 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1849 rc.y = s->w->phys_cursor.y;
1850 rc.width = s->w->phys_cursor_width;
1851 rc.height = s->w->phys_cursor_height;
1852
1853 x_intersect_rectangles (&r_save, &rc, &r);
1854 }
1855 }
1856 else
1857 {
1858 /* Don't use S->y for clipping because it doesn't take partially
1859 visible lines into account. For example, it can be negative for
1860 partially visible lines at the top of a window. */
1861 if (!s->row->full_width_p
1862 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1863 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1864 else
1865 r.y = max (0, s->row->y);
1866
1867 /* If drawing a tool-bar window, draw it over the internal border
1868 at the top of the window. */
1869 if (WINDOWP (s->f->tool_bar_window)
1870 && s->w == XWINDOW (s->f->tool_bar_window))
1871 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1872 }
1873
1874 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1875
1876 /* If drawing the cursor, don't let glyph draw outside its
1877 advertised boundaries. Cleartype does this under some circumstances. */
1878 if (s->hl == DRAW_CURSOR)
1879 {
1880 struct glyph *glyph = s->first_glyph;
1881 int height, max_y;
1882
1883 if (s->x > r.x)
1884 {
1885 r.width -= s->x - r.x;
1886 r.x = s->x;
1887 }
1888 r.width = min (r.width, glyph->pixel_width);
1889
1890 /* If r.y is below window bottom, ensure that we still see a cursor. */
1891 height = min (glyph->ascent + glyph->descent,
1892 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1893 max_y = window_text_bottom_y (s->w) - height;
1894 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1895 if (s->ybase - glyph->ascent > max_y)
1896 {
1897 r.y = max_y;
1898 r.height = height;
1899 }
1900 else
1901 {
1902 /* Don't draw cursor glyph taller than our actual glyph. */
1903 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1904 if (height < r.height)
1905 {
1906 max_y = r.y + r.height;
1907 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1908 r.height = min (max_y - r.y, height);
1909 }
1910 }
1911 }
1912
1913 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1914 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1915 {
1916 #ifdef CONVERT_FROM_XRECT
1917 CONVERT_FROM_XRECT (r, *rects);
1918 #else
1919 *rects = r;
1920 #endif
1921 return 1;
1922 }
1923 else
1924 {
1925 /* If we are processing overlapping and allowed to return
1926 multiple clipping rectangles, we exclude the row of the glyph
1927 string from the clipping rectangle. This is to avoid drawing
1928 the same text on the environment with anti-aliasing. */
1929 #ifdef CONVERT_FROM_XRECT
1930 XRectangle rs[2];
1931 #else
1932 XRectangle *rs = rects;
1933 #endif
1934 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1935
1936 if (s->for_overlaps & OVERLAPS_PRED)
1937 {
1938 rs[i] = r;
1939 if (r.y + r.height > row_y)
1940 {
1941 if (r.y < row_y)
1942 rs[i].height = row_y - r.y;
1943 else
1944 rs[i].height = 0;
1945 }
1946 i++;
1947 }
1948 if (s->for_overlaps & OVERLAPS_SUCC)
1949 {
1950 rs[i] = r;
1951 if (r.y < row_y + s->row->visible_height)
1952 {
1953 if (r.y + r.height > row_y + s->row->visible_height)
1954 {
1955 rs[i].y = row_y + s->row->visible_height;
1956 rs[i].height = r.y + r.height - rs[i].y;
1957 }
1958 else
1959 rs[i].height = 0;
1960 }
1961 i++;
1962 }
1963
1964 n = i;
1965 #ifdef CONVERT_FROM_XRECT
1966 for (i = 0; i < n; i++)
1967 CONVERT_FROM_XRECT (rs[i], rects[i]);
1968 #endif
1969 return n;
1970 }
1971 }
1972
1973 /* EXPORT:
1974 Return in *NR the clipping rectangle for glyph string S. */
1975
1976 void
1977 get_glyph_string_clip_rect (s, nr)
1978 struct glyph_string *s;
1979 NativeRectangle *nr;
1980 {
1981 get_glyph_string_clip_rects (s, nr, 1);
1982 }
1983
1984
1985 /* EXPORT:
1986 Return the position and height of the phys cursor in window W.
1987 Set w->phys_cursor_width to width of phys cursor.
1988 */
1989
1990 void
1991 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
1992 struct window *w;
1993 struct glyph_row *row;
1994 struct glyph *glyph;
1995 int *xp, *yp, *heightp;
1996 {
1997 struct frame *f = XFRAME (WINDOW_FRAME (w));
1998 int x, y, wd, h, h0, y0;
1999
2000 /* Compute the width of the rectangle to draw. If on a stretch
2001 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2002 rectangle as wide as the glyph, but use a canonical character
2003 width instead. */
2004 wd = glyph->pixel_width - 1;
2005 #ifdef HAVE_NTGUI
2006 wd++; /* Why? */
2007 #endif
2008
2009 x = w->phys_cursor.x;
2010 if (x < 0)
2011 {
2012 wd += x;
2013 x = 0;
2014 }
2015
2016 if (glyph->type == STRETCH_GLYPH
2017 && !x_stretch_cursor_p)
2018 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2019 w->phys_cursor_width = wd;
2020
2021 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2022
2023 /* If y is below window bottom, ensure that we still see a cursor. */
2024 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2025
2026 h = max (h0, glyph->ascent + glyph->descent);
2027 h0 = min (h0, glyph->ascent + glyph->descent);
2028
2029 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2030 if (y < y0)
2031 {
2032 h = max (h - (y0 - y) + 1, h0);
2033 y = y0 - 1;
2034 }
2035 else
2036 {
2037 y0 = window_text_bottom_y (w) - h0;
2038 if (y > y0)
2039 {
2040 h += y - y0;
2041 y = y0;
2042 }
2043 }
2044
2045 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2046 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2047 *heightp = h;
2048 }
2049
2050 /*
2051 * Remember which glyph the mouse is over.
2052 */
2053
2054 void
2055 remember_mouse_glyph (f, gx, gy, rect)
2056 struct frame *f;
2057 int gx, gy;
2058 NativeRectangle *rect;
2059 {
2060 Lisp_Object window;
2061 struct window *w;
2062 struct glyph_row *r, *gr, *end_row;
2063 enum window_part part;
2064 enum glyph_row_area area;
2065 int x, y, width, height;
2066
2067 /* Try to determine frame pixel position and size of the glyph under
2068 frame pixel coordinates X/Y on frame F. */
2069
2070 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2071 if (NILP (window))
2072 {
2073 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2074 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2075 goto virtual_glyph;
2076 }
2077
2078 w = XWINDOW (window);
2079 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2080 height = WINDOW_FRAME_LINE_HEIGHT (w);
2081
2082 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2083 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2084
2085 if (w->pseudo_window_p)
2086 {
2087 area = TEXT_AREA;
2088 part = ON_MODE_LINE; /* Don't adjust margin. */
2089 goto text_glyph;
2090 }
2091
2092 switch (part)
2093 {
2094 case ON_LEFT_MARGIN:
2095 area = LEFT_MARGIN_AREA;
2096 goto text_glyph;
2097
2098 case ON_RIGHT_MARGIN:
2099 area = RIGHT_MARGIN_AREA;
2100 goto text_glyph;
2101
2102 case ON_HEADER_LINE:
2103 case ON_MODE_LINE:
2104 gr = (part == ON_HEADER_LINE
2105 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2106 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2107 gy = gr->y;
2108 area = TEXT_AREA;
2109 goto text_glyph_row_found;
2110
2111 case ON_TEXT:
2112 area = TEXT_AREA;
2113
2114 text_glyph:
2115 gr = 0; gy = 0;
2116 for (; r <= end_row && r->enabled_p; ++r)
2117 if (r->y + r->height > y)
2118 {
2119 gr = r; gy = r->y;
2120 break;
2121 }
2122
2123 text_glyph_row_found:
2124 if (gr && gy <= y)
2125 {
2126 struct glyph *g = gr->glyphs[area];
2127 struct glyph *end = g + gr->used[area];
2128
2129 height = gr->height;
2130 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2131 if (gx + g->pixel_width > x)
2132 break;
2133
2134 if (g < end)
2135 {
2136 if (g->type == IMAGE_GLYPH)
2137 {
2138 /* Don't remember when mouse is over image, as
2139 image may have hot-spots. */
2140 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2141 return;
2142 }
2143 width = g->pixel_width;
2144 }
2145 else
2146 {
2147 /* Use nominal char spacing at end of line. */
2148 x -= gx;
2149 gx += (x / width) * width;
2150 }
2151
2152 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2153 gx += window_box_left_offset (w, area);
2154 }
2155 else
2156 {
2157 /* Use nominal line height at end of window. */
2158 gx = (x / width) * width;
2159 y -= gy;
2160 gy += (y / height) * height;
2161 }
2162 break;
2163
2164 case ON_LEFT_FRINGE:
2165 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2166 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2167 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2168 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2169 goto row_glyph;
2170
2171 case ON_RIGHT_FRINGE:
2172 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2173 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2174 : window_box_right_offset (w, TEXT_AREA));
2175 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2176 goto row_glyph;
2177
2178 case ON_SCROLL_BAR:
2179 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2180 ? 0
2181 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2182 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2183 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2184 : 0)));
2185 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2186
2187 row_glyph:
2188 gr = 0, gy = 0;
2189 for (; r <= end_row && r->enabled_p; ++r)
2190 if (r->y + r->height > y)
2191 {
2192 gr = r; gy = r->y;
2193 break;
2194 }
2195
2196 if (gr && gy <= y)
2197 height = gr->height;
2198 else
2199 {
2200 /* Use nominal line height at end of window. */
2201 y -= gy;
2202 gy += (y / height) * height;
2203 }
2204 break;
2205
2206 default:
2207 ;
2208 virtual_glyph:
2209 /* If there is no glyph under the mouse, then we divide the screen
2210 into a grid of the smallest glyph in the frame, and use that
2211 as our "glyph". */
2212
2213 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2214 round down even for negative values. */
2215 if (gx < 0)
2216 gx -= width - 1;
2217 if (gy < 0)
2218 gy -= height - 1;
2219
2220 gx = (gx / width) * width;
2221 gy = (gy / height) * height;
2222
2223 goto store_rect;
2224 }
2225
2226 gx += WINDOW_LEFT_EDGE_X (w);
2227 gy += WINDOW_TOP_EDGE_Y (w);
2228
2229 store_rect:
2230 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2231
2232 /* Visible feedback for debugging. */
2233 #if 0
2234 #if HAVE_X_WINDOWS
2235 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2236 f->output_data.x->normal_gc,
2237 gx, gy, width, height);
2238 #endif
2239 #endif
2240 }
2241
2242
2243 #endif /* HAVE_WINDOW_SYSTEM */
2244
2245 \f
2246 /***********************************************************************
2247 Lisp form evaluation
2248 ***********************************************************************/
2249
2250 /* Error handler for safe_eval and safe_call. */
2251
2252 static Lisp_Object
2253 safe_eval_handler (arg)
2254 Lisp_Object arg;
2255 {
2256 add_to_log ("Error during redisplay: %s", arg, Qnil);
2257 return Qnil;
2258 }
2259
2260
2261 /* Evaluate SEXPR and return the result, or nil if something went
2262 wrong. Prevent redisplay during the evaluation. */
2263
2264 Lisp_Object
2265 safe_eval (sexpr)
2266 Lisp_Object sexpr;
2267 {
2268 Lisp_Object val;
2269
2270 if (inhibit_eval_during_redisplay)
2271 val = Qnil;
2272 else
2273 {
2274 int count = SPECPDL_INDEX ();
2275 struct gcpro gcpro1;
2276
2277 GCPRO1 (sexpr);
2278 specbind (Qinhibit_redisplay, Qt);
2279 /* Use Qt to ensure debugger does not run,
2280 so there is no possibility of wanting to redisplay. */
2281 val = internal_condition_case_1 (Feval, sexpr, Qt,
2282 safe_eval_handler);
2283 UNGCPRO;
2284 val = unbind_to (count, val);
2285 }
2286
2287 return val;
2288 }
2289
2290
2291 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2292 Return the result, or nil if something went wrong. Prevent
2293 redisplay during the evaluation. */
2294
2295 Lisp_Object
2296 safe_call (nargs, args)
2297 int nargs;
2298 Lisp_Object *args;
2299 {
2300 Lisp_Object val;
2301
2302 if (inhibit_eval_during_redisplay)
2303 val = Qnil;
2304 else
2305 {
2306 int count = SPECPDL_INDEX ();
2307 struct gcpro gcpro1;
2308
2309 GCPRO1 (args[0]);
2310 gcpro1.nvars = nargs;
2311 specbind (Qinhibit_redisplay, Qt);
2312 /* Use Qt to ensure debugger does not run,
2313 so there is no possibility of wanting to redisplay. */
2314 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2315 safe_eval_handler);
2316 UNGCPRO;
2317 val = unbind_to (count, val);
2318 }
2319
2320 return val;
2321 }
2322
2323
2324 /* Call function FN with one argument ARG.
2325 Return the result, or nil if something went wrong. */
2326
2327 Lisp_Object
2328 safe_call1 (fn, arg)
2329 Lisp_Object fn, arg;
2330 {
2331 Lisp_Object args[2];
2332 args[0] = fn;
2333 args[1] = arg;
2334 return safe_call (2, args);
2335 }
2336
2337
2338 \f
2339 /***********************************************************************
2340 Debugging
2341 ***********************************************************************/
2342
2343 #if 0
2344
2345 /* Define CHECK_IT to perform sanity checks on iterators.
2346 This is for debugging. It is too slow to do unconditionally. */
2347
2348 static void
2349 check_it (it)
2350 struct it *it;
2351 {
2352 if (it->method == GET_FROM_STRING)
2353 {
2354 xassert (STRINGP (it->string));
2355 xassert (IT_STRING_CHARPOS (*it) >= 0);
2356 }
2357 else
2358 {
2359 xassert (IT_STRING_CHARPOS (*it) < 0);
2360 if (it->method == GET_FROM_BUFFER)
2361 {
2362 /* Check that character and byte positions agree. */
2363 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2364 }
2365 }
2366
2367 if (it->dpvec)
2368 xassert (it->current.dpvec_index >= 0);
2369 else
2370 xassert (it->current.dpvec_index < 0);
2371 }
2372
2373 #define CHECK_IT(IT) check_it ((IT))
2374
2375 #else /* not 0 */
2376
2377 #define CHECK_IT(IT) (void) 0
2378
2379 #endif /* not 0 */
2380
2381
2382 #if GLYPH_DEBUG
2383
2384 /* Check that the window end of window W is what we expect it
2385 to be---the last row in the current matrix displaying text. */
2386
2387 static void
2388 check_window_end (w)
2389 struct window *w;
2390 {
2391 if (!MINI_WINDOW_P (w)
2392 && !NILP (w->window_end_valid))
2393 {
2394 struct glyph_row *row;
2395 xassert ((row = MATRIX_ROW (w->current_matrix,
2396 XFASTINT (w->window_end_vpos)),
2397 !row->enabled_p
2398 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2399 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2400 }
2401 }
2402
2403 #define CHECK_WINDOW_END(W) check_window_end ((W))
2404
2405 #else /* not GLYPH_DEBUG */
2406
2407 #define CHECK_WINDOW_END(W) (void) 0
2408
2409 #endif /* not GLYPH_DEBUG */
2410
2411
2412 \f
2413 /***********************************************************************
2414 Iterator initialization
2415 ***********************************************************************/
2416
2417 /* Initialize IT for displaying current_buffer in window W, starting
2418 at character position CHARPOS. CHARPOS < 0 means that no buffer
2419 position is specified which is useful when the iterator is assigned
2420 a position later. BYTEPOS is the byte position corresponding to
2421 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2422
2423 If ROW is not null, calls to produce_glyphs with IT as parameter
2424 will produce glyphs in that row.
2425
2426 BASE_FACE_ID is the id of a base face to use. It must be one of
2427 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2428 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2429 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2430
2431 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2432 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2433 will be initialized to use the corresponding mode line glyph row of
2434 the desired matrix of W. */
2435
2436 void
2437 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2438 struct it *it;
2439 struct window *w;
2440 int charpos, bytepos;
2441 struct glyph_row *row;
2442 enum face_id base_face_id;
2443 {
2444 int highlight_region_p;
2445
2446 /* Some precondition checks. */
2447 xassert (w != NULL && it != NULL);
2448 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2449 && charpos <= ZV));
2450
2451 /* If face attributes have been changed since the last redisplay,
2452 free realized faces now because they depend on face definitions
2453 that might have changed. Don't free faces while there might be
2454 desired matrices pending which reference these faces. */
2455 if (face_change_count && !inhibit_free_realized_faces)
2456 {
2457 face_change_count = 0;
2458 free_all_realized_faces (Qnil);
2459 }
2460
2461 /* Use one of the mode line rows of W's desired matrix if
2462 appropriate. */
2463 if (row == NULL)
2464 {
2465 if (base_face_id == MODE_LINE_FACE_ID
2466 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2467 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2468 else if (base_face_id == HEADER_LINE_FACE_ID)
2469 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2470 }
2471
2472 /* Clear IT. */
2473 bzero (it, sizeof *it);
2474 it->current.overlay_string_index = -1;
2475 it->current.dpvec_index = -1;
2476 it->base_face_id = base_face_id;
2477 it->string = Qnil;
2478 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2479
2480 /* The window in which we iterate over current_buffer: */
2481 XSETWINDOW (it->window, w);
2482 it->w = w;
2483 it->f = XFRAME (w->frame);
2484
2485 /* Extra space between lines (on window systems only). */
2486 if (base_face_id == DEFAULT_FACE_ID
2487 && FRAME_WINDOW_P (it->f))
2488 {
2489 if (NATNUMP (current_buffer->extra_line_spacing))
2490 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2491 else if (FLOATP (current_buffer->extra_line_spacing))
2492 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2493 * FRAME_LINE_HEIGHT (it->f));
2494 else if (it->f->extra_line_spacing > 0)
2495 it->extra_line_spacing = it->f->extra_line_spacing;
2496 it->max_extra_line_spacing = 0;
2497 }
2498
2499 /* If realized faces have been removed, e.g. because of face
2500 attribute changes of named faces, recompute them. When running
2501 in batch mode, the face cache of Vterminal_frame is null. If
2502 we happen to get called, make a dummy face cache. */
2503 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2504 init_frame_faces (it->f);
2505 if (FRAME_FACE_CACHE (it->f)->used == 0)
2506 recompute_basic_faces (it->f);
2507
2508 /* Current value of the `slice', `space-width', and 'height' properties. */
2509 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2510 it->space_width = Qnil;
2511 it->font_height = Qnil;
2512 it->override_ascent = -1;
2513
2514 /* Are control characters displayed as `^C'? */
2515 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2516
2517 /* -1 means everything between a CR and the following line end
2518 is invisible. >0 means lines indented more than this value are
2519 invisible. */
2520 it->selective = (INTEGERP (current_buffer->selective_display)
2521 ? XFASTINT (current_buffer->selective_display)
2522 : (!NILP (current_buffer->selective_display)
2523 ? -1 : 0));
2524 it->selective_display_ellipsis_p
2525 = !NILP (current_buffer->selective_display_ellipses);
2526
2527 /* Display table to use. */
2528 it->dp = window_display_table (w);
2529
2530 /* Are multibyte characters enabled in current_buffer? */
2531 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2532
2533 /* Non-zero if we should highlight the region. */
2534 highlight_region_p
2535 = (!NILP (Vtransient_mark_mode)
2536 && !NILP (current_buffer->mark_active)
2537 && XMARKER (current_buffer->mark)->buffer != 0);
2538
2539 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2540 start and end of a visible region in window IT->w. Set both to
2541 -1 to indicate no region. */
2542 if (highlight_region_p
2543 /* Maybe highlight only in selected window. */
2544 && (/* Either show region everywhere. */
2545 highlight_nonselected_windows
2546 /* Or show region in the selected window. */
2547 || w == XWINDOW (selected_window)
2548 /* Or show the region if we are in the mini-buffer and W is
2549 the window the mini-buffer refers to. */
2550 || (MINI_WINDOW_P (XWINDOW (selected_window))
2551 && WINDOWP (minibuf_selected_window)
2552 && w == XWINDOW (minibuf_selected_window))))
2553 {
2554 int charpos = marker_position (current_buffer->mark);
2555 it->region_beg_charpos = min (PT, charpos);
2556 it->region_end_charpos = max (PT, charpos);
2557 }
2558 else
2559 it->region_beg_charpos = it->region_end_charpos = -1;
2560
2561 /* Get the position at which the redisplay_end_trigger hook should
2562 be run, if it is to be run at all. */
2563 if (MARKERP (w->redisplay_end_trigger)
2564 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2565 it->redisplay_end_trigger_charpos
2566 = marker_position (w->redisplay_end_trigger);
2567 else if (INTEGERP (w->redisplay_end_trigger))
2568 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2569
2570 /* Correct bogus values of tab_width. */
2571 it->tab_width = XINT (current_buffer->tab_width);
2572 if (it->tab_width <= 0 || it->tab_width > 1000)
2573 it->tab_width = 8;
2574
2575 /* Are lines in the display truncated? */
2576 it->truncate_lines_p
2577 = (base_face_id != DEFAULT_FACE_ID
2578 || XINT (it->w->hscroll)
2579 || (truncate_partial_width_windows
2580 && !WINDOW_FULL_WIDTH_P (it->w))
2581 || !NILP (current_buffer->truncate_lines));
2582
2583 /* Get dimensions of truncation and continuation glyphs. These are
2584 displayed as fringe bitmaps under X, so we don't need them for such
2585 frames. */
2586 if (!FRAME_WINDOW_P (it->f))
2587 {
2588 if (it->truncate_lines_p)
2589 {
2590 /* We will need the truncation glyph. */
2591 xassert (it->glyph_row == NULL);
2592 produce_special_glyphs (it, IT_TRUNCATION);
2593 it->truncation_pixel_width = it->pixel_width;
2594 }
2595 else
2596 {
2597 /* We will need the continuation glyph. */
2598 xassert (it->glyph_row == NULL);
2599 produce_special_glyphs (it, IT_CONTINUATION);
2600 it->continuation_pixel_width = it->pixel_width;
2601 }
2602
2603 /* Reset these values to zero because the produce_special_glyphs
2604 above has changed them. */
2605 it->pixel_width = it->ascent = it->descent = 0;
2606 it->phys_ascent = it->phys_descent = 0;
2607 }
2608
2609 /* Set this after getting the dimensions of truncation and
2610 continuation glyphs, so that we don't produce glyphs when calling
2611 produce_special_glyphs, above. */
2612 it->glyph_row = row;
2613 it->area = TEXT_AREA;
2614
2615 /* Get the dimensions of the display area. The display area
2616 consists of the visible window area plus a horizontally scrolled
2617 part to the left of the window. All x-values are relative to the
2618 start of this total display area. */
2619 if (base_face_id != DEFAULT_FACE_ID)
2620 {
2621 /* Mode lines, menu bar in terminal frames. */
2622 it->first_visible_x = 0;
2623 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2624 }
2625 else
2626 {
2627 it->first_visible_x
2628 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2629 it->last_visible_x = (it->first_visible_x
2630 + window_box_width (w, TEXT_AREA));
2631
2632 /* If we truncate lines, leave room for the truncator glyph(s) at
2633 the right margin. Otherwise, leave room for the continuation
2634 glyph(s). Truncation and continuation glyphs are not inserted
2635 for window-based redisplay. */
2636 if (!FRAME_WINDOW_P (it->f))
2637 {
2638 if (it->truncate_lines_p)
2639 it->last_visible_x -= it->truncation_pixel_width;
2640 else
2641 it->last_visible_x -= it->continuation_pixel_width;
2642 }
2643
2644 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2645 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2646 }
2647
2648 /* Leave room for a border glyph. */
2649 if (!FRAME_WINDOW_P (it->f)
2650 && !WINDOW_RIGHTMOST_P (it->w))
2651 it->last_visible_x -= 1;
2652
2653 it->last_visible_y = window_text_bottom_y (w);
2654
2655 /* For mode lines and alike, arrange for the first glyph having a
2656 left box line if the face specifies a box. */
2657 if (base_face_id != DEFAULT_FACE_ID)
2658 {
2659 struct face *face;
2660
2661 it->face_id = base_face_id;
2662
2663 /* If we have a boxed mode line, make the first character appear
2664 with a left box line. */
2665 face = FACE_FROM_ID (it->f, base_face_id);
2666 if (face->box != FACE_NO_BOX)
2667 it->start_of_box_run_p = 1;
2668 }
2669
2670 /* If a buffer position was specified, set the iterator there,
2671 getting overlays and face properties from that position. */
2672 if (charpos >= BUF_BEG (current_buffer))
2673 {
2674 it->end_charpos = ZV;
2675 it->face_id = -1;
2676 IT_CHARPOS (*it) = charpos;
2677
2678 /* Compute byte position if not specified. */
2679 if (bytepos < charpos)
2680 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2681 else
2682 IT_BYTEPOS (*it) = bytepos;
2683
2684 it->start = it->current;
2685
2686 /* Compute faces etc. */
2687 reseat (it, it->current.pos, 1);
2688 }
2689
2690 CHECK_IT (it);
2691 }
2692
2693
2694 /* Initialize IT for the display of window W with window start POS. */
2695
2696 void
2697 start_display (it, w, pos)
2698 struct it *it;
2699 struct window *w;
2700 struct text_pos pos;
2701 {
2702 struct glyph_row *row;
2703 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2704
2705 row = w->desired_matrix->rows + first_vpos;
2706 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2707 it->first_vpos = first_vpos;
2708
2709 /* Don't reseat to previous visible line start if current start
2710 position is in a string or image. */
2711 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2712 {
2713 int start_at_line_beg_p;
2714 int first_y = it->current_y;
2715
2716 /* If window start is not at a line start, skip forward to POS to
2717 get the correct continuation lines width. */
2718 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2719 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2720 if (!start_at_line_beg_p)
2721 {
2722 int new_x;
2723
2724 reseat_at_previous_visible_line_start (it);
2725 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2726
2727 new_x = it->current_x + it->pixel_width;
2728
2729 /* If lines are continued, this line may end in the middle
2730 of a multi-glyph character (e.g. a control character
2731 displayed as \003, or in the middle of an overlay
2732 string). In this case move_it_to above will not have
2733 taken us to the start of the continuation line but to the
2734 end of the continued line. */
2735 if (it->current_x > 0
2736 && !it->truncate_lines_p /* Lines are continued. */
2737 && (/* And glyph doesn't fit on the line. */
2738 new_x > it->last_visible_x
2739 /* Or it fits exactly and we're on a window
2740 system frame. */
2741 || (new_x == it->last_visible_x
2742 && FRAME_WINDOW_P (it->f))))
2743 {
2744 if (it->current.dpvec_index >= 0
2745 || it->current.overlay_string_index >= 0)
2746 {
2747 set_iterator_to_next (it, 1);
2748 move_it_in_display_line_to (it, -1, -1, 0);
2749 }
2750
2751 it->continuation_lines_width += it->current_x;
2752 }
2753
2754 /* We're starting a new display line, not affected by the
2755 height of the continued line, so clear the appropriate
2756 fields in the iterator structure. */
2757 it->max_ascent = it->max_descent = 0;
2758 it->max_phys_ascent = it->max_phys_descent = 0;
2759
2760 it->current_y = first_y;
2761 it->vpos = 0;
2762 it->current_x = it->hpos = 0;
2763 }
2764 }
2765
2766 #if 0 /* Don't assert the following because start_display is sometimes
2767 called intentionally with a window start that is not at a
2768 line start. Please leave this code in as a comment. */
2769
2770 /* Window start should be on a line start, now. */
2771 xassert (it->continuation_lines_width
2772 || IT_CHARPOS (it) == BEGV
2773 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2774 #endif /* 0 */
2775 }
2776
2777
2778 /* Return 1 if POS is a position in ellipses displayed for invisible
2779 text. W is the window we display, for text property lookup. */
2780
2781 static int
2782 in_ellipses_for_invisible_text_p (pos, w)
2783 struct display_pos *pos;
2784 struct window *w;
2785 {
2786 Lisp_Object prop, window;
2787 int ellipses_p = 0;
2788 int charpos = CHARPOS (pos->pos);
2789
2790 /* If POS specifies a position in a display vector, this might
2791 be for an ellipsis displayed for invisible text. We won't
2792 get the iterator set up for delivering that ellipsis unless
2793 we make sure that it gets aware of the invisible text. */
2794 if (pos->dpvec_index >= 0
2795 && pos->overlay_string_index < 0
2796 && CHARPOS (pos->string_pos) < 0
2797 && charpos > BEGV
2798 && (XSETWINDOW (window, w),
2799 prop = Fget_char_property (make_number (charpos),
2800 Qinvisible, window),
2801 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2802 {
2803 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2804 window);
2805 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2806 }
2807
2808 return ellipses_p;
2809 }
2810
2811
2812 /* Initialize IT for stepping through current_buffer in window W,
2813 starting at position POS that includes overlay string and display
2814 vector/ control character translation position information. Value
2815 is zero if there are overlay strings with newlines at POS. */
2816
2817 static int
2818 init_from_display_pos (it, w, pos)
2819 struct it *it;
2820 struct window *w;
2821 struct display_pos *pos;
2822 {
2823 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2824 int i, overlay_strings_with_newlines = 0;
2825
2826 /* If POS specifies a position in a display vector, this might
2827 be for an ellipsis displayed for invisible text. We won't
2828 get the iterator set up for delivering that ellipsis unless
2829 we make sure that it gets aware of the invisible text. */
2830 if (in_ellipses_for_invisible_text_p (pos, w))
2831 {
2832 --charpos;
2833 bytepos = 0;
2834 }
2835
2836 /* Keep in mind: the call to reseat in init_iterator skips invisible
2837 text, so we might end up at a position different from POS. This
2838 is only a problem when POS is a row start after a newline and an
2839 overlay starts there with an after-string, and the overlay has an
2840 invisible property. Since we don't skip invisible text in
2841 display_line and elsewhere immediately after consuming the
2842 newline before the row start, such a POS will not be in a string,
2843 but the call to init_iterator below will move us to the
2844 after-string. */
2845 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2846
2847 /* This only scans the current chunk -- it should scan all chunks.
2848 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2849 to 16 in 22.1 to make this a lesser problem. */
2850 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2851 {
2852 const char *s = SDATA (it->overlay_strings[i]);
2853 const char *e = s + SBYTES (it->overlay_strings[i]);
2854
2855 while (s < e && *s != '\n')
2856 ++s;
2857
2858 if (s < e)
2859 {
2860 overlay_strings_with_newlines = 1;
2861 break;
2862 }
2863 }
2864
2865 /* If position is within an overlay string, set up IT to the right
2866 overlay string. */
2867 if (pos->overlay_string_index >= 0)
2868 {
2869 int relative_index;
2870
2871 /* If the first overlay string happens to have a `display'
2872 property for an image, the iterator will be set up for that
2873 image, and we have to undo that setup first before we can
2874 correct the overlay string index. */
2875 if (it->method == GET_FROM_IMAGE)
2876 pop_it (it);
2877
2878 /* We already have the first chunk of overlay strings in
2879 IT->overlay_strings. Load more until the one for
2880 pos->overlay_string_index is in IT->overlay_strings. */
2881 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2882 {
2883 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2884 it->current.overlay_string_index = 0;
2885 while (n--)
2886 {
2887 load_overlay_strings (it, 0);
2888 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2889 }
2890 }
2891
2892 it->current.overlay_string_index = pos->overlay_string_index;
2893 relative_index = (it->current.overlay_string_index
2894 % OVERLAY_STRING_CHUNK_SIZE);
2895 it->string = it->overlay_strings[relative_index];
2896 xassert (STRINGP (it->string));
2897 it->current.string_pos = pos->string_pos;
2898 it->method = GET_FROM_STRING;
2899 }
2900
2901 #if 0 /* This is bogus because POS not having an overlay string
2902 position does not mean it's after the string. Example: A
2903 line starting with a before-string and initialization of IT
2904 to the previous row's end position. */
2905 else if (it->current.overlay_string_index >= 0)
2906 {
2907 /* If POS says we're already after an overlay string ending at
2908 POS, make sure to pop the iterator because it will be in
2909 front of that overlay string. When POS is ZV, we've thereby
2910 also ``processed'' overlay strings at ZV. */
2911 while (it->sp)
2912 pop_it (it);
2913 xassert (it->current.overlay_string_index == -1);
2914 xassert (it->method == GET_FROM_BUFFER);
2915 if (CHARPOS (pos->pos) == ZV)
2916 it->overlay_strings_at_end_processed_p = 1;
2917 }
2918 #endif /* 0 */
2919
2920 if (CHARPOS (pos->string_pos) >= 0)
2921 {
2922 /* Recorded position is not in an overlay string, but in another
2923 string. This can only be a string from a `display' property.
2924 IT should already be filled with that string. */
2925 it->current.string_pos = pos->string_pos;
2926 xassert (STRINGP (it->string));
2927 }
2928
2929 /* Restore position in display vector translations, control
2930 character translations or ellipses. */
2931 if (pos->dpvec_index >= 0)
2932 {
2933 if (it->dpvec == NULL)
2934 get_next_display_element (it);
2935 xassert (it->dpvec && it->current.dpvec_index == 0);
2936 it->current.dpvec_index = pos->dpvec_index;
2937 }
2938
2939 CHECK_IT (it);
2940 return !overlay_strings_with_newlines;
2941 }
2942
2943
2944 /* Initialize IT for stepping through current_buffer in window W
2945 starting at ROW->start. */
2946
2947 static void
2948 init_to_row_start (it, w, row)
2949 struct it *it;
2950 struct window *w;
2951 struct glyph_row *row;
2952 {
2953 init_from_display_pos (it, w, &row->start);
2954 it->start = row->start;
2955 it->continuation_lines_width = row->continuation_lines_width;
2956 CHECK_IT (it);
2957 }
2958
2959
2960 /* Initialize IT for stepping through current_buffer in window W
2961 starting in the line following ROW, i.e. starting at ROW->end.
2962 Value is zero if there are overlay strings with newlines at ROW's
2963 end position. */
2964
2965 static int
2966 init_to_row_end (it, w, row)
2967 struct it *it;
2968 struct window *w;
2969 struct glyph_row *row;
2970 {
2971 int success = 0;
2972
2973 if (init_from_display_pos (it, w, &row->end))
2974 {
2975 if (row->continued_p)
2976 it->continuation_lines_width
2977 = row->continuation_lines_width + row->pixel_width;
2978 CHECK_IT (it);
2979 success = 1;
2980 }
2981
2982 return success;
2983 }
2984
2985
2986
2987 \f
2988 /***********************************************************************
2989 Text properties
2990 ***********************************************************************/
2991
2992 /* Called when IT reaches IT->stop_charpos. Handle text property and
2993 overlay changes. Set IT->stop_charpos to the next position where
2994 to stop. */
2995
2996 static void
2997 handle_stop (it)
2998 struct it *it;
2999 {
3000 enum prop_handled handled;
3001 int handle_overlay_change_p;
3002 struct props *p;
3003
3004 it->dpvec = NULL;
3005 it->current.dpvec_index = -1;
3006 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3007 it->ignore_overlay_strings_at_pos_p = 0;
3008
3009 /* Use face of preceding text for ellipsis (if invisible) */
3010 if (it->selective_display_ellipsis_p)
3011 it->saved_face_id = it->face_id;
3012
3013 do
3014 {
3015 handled = HANDLED_NORMALLY;
3016
3017 /* Call text property handlers. */
3018 for (p = it_props; p->handler; ++p)
3019 {
3020 handled = p->handler (it);
3021
3022 if (handled == HANDLED_RECOMPUTE_PROPS)
3023 break;
3024 else if (handled == HANDLED_RETURN)
3025 {
3026 /* We still want to show before and after strings from
3027 overlays even if the actual buffer text is replaced. */
3028 if (!handle_overlay_change_p || it->sp > 1)
3029 return;
3030 if (!get_overlay_strings_1 (it, 0, 0))
3031 return;
3032 it->ignore_overlay_strings_at_pos_p = 1;
3033 it->string_from_display_prop_p = 0;
3034 handle_overlay_change_p = 0;
3035 handled = HANDLED_RECOMPUTE_PROPS;
3036 break;
3037 }
3038 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3039 handle_overlay_change_p = 0;
3040 }
3041
3042 if (handled != HANDLED_RECOMPUTE_PROPS)
3043 {
3044 /* Don't check for overlay strings below when set to deliver
3045 characters from a display vector. */
3046 if (it->method == GET_FROM_DISPLAY_VECTOR)
3047 handle_overlay_change_p = 0;
3048
3049 /* Handle overlay changes. */
3050 if (handle_overlay_change_p)
3051 handled = handle_overlay_change (it);
3052
3053 /* Determine where to stop next. */
3054 if (handled == HANDLED_NORMALLY)
3055 compute_stop_pos (it);
3056 }
3057 }
3058 while (handled == HANDLED_RECOMPUTE_PROPS);
3059 }
3060
3061
3062 /* Compute IT->stop_charpos from text property and overlay change
3063 information for IT's current position. */
3064
3065 static void
3066 compute_stop_pos (it)
3067 struct it *it;
3068 {
3069 register INTERVAL iv, next_iv;
3070 Lisp_Object object, limit, position;
3071
3072 /* If nowhere else, stop at the end. */
3073 it->stop_charpos = it->end_charpos;
3074
3075 if (STRINGP (it->string))
3076 {
3077 /* Strings are usually short, so don't limit the search for
3078 properties. */
3079 object = it->string;
3080 limit = Qnil;
3081 position = make_number (IT_STRING_CHARPOS (*it));
3082 }
3083 else
3084 {
3085 int charpos;
3086
3087 /* If next overlay change is in front of the current stop pos
3088 (which is IT->end_charpos), stop there. Note: value of
3089 next_overlay_change is point-max if no overlay change
3090 follows. */
3091 charpos = next_overlay_change (IT_CHARPOS (*it));
3092 if (charpos < it->stop_charpos)
3093 it->stop_charpos = charpos;
3094
3095 /* If showing the region, we have to stop at the region
3096 start or end because the face might change there. */
3097 if (it->region_beg_charpos > 0)
3098 {
3099 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3100 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3101 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3102 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3103 }
3104
3105 /* Set up variables for computing the stop position from text
3106 property changes. */
3107 XSETBUFFER (object, current_buffer);
3108 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3109 position = make_number (IT_CHARPOS (*it));
3110
3111 }
3112
3113 /* Get the interval containing IT's position. Value is a null
3114 interval if there isn't such an interval. */
3115 iv = validate_interval_range (object, &position, &position, 0);
3116 if (!NULL_INTERVAL_P (iv))
3117 {
3118 Lisp_Object values_here[LAST_PROP_IDX];
3119 struct props *p;
3120
3121 /* Get properties here. */
3122 for (p = it_props; p->handler; ++p)
3123 values_here[p->idx] = textget (iv->plist, *p->name);
3124
3125 /* Look for an interval following iv that has different
3126 properties. */
3127 for (next_iv = next_interval (iv);
3128 (!NULL_INTERVAL_P (next_iv)
3129 && (NILP (limit)
3130 || XFASTINT (limit) > next_iv->position));
3131 next_iv = next_interval (next_iv))
3132 {
3133 for (p = it_props; p->handler; ++p)
3134 {
3135 Lisp_Object new_value;
3136
3137 new_value = textget (next_iv->plist, *p->name);
3138 if (!EQ (values_here[p->idx], new_value))
3139 break;
3140 }
3141
3142 if (p->handler)
3143 break;
3144 }
3145
3146 if (!NULL_INTERVAL_P (next_iv))
3147 {
3148 if (INTEGERP (limit)
3149 && next_iv->position >= XFASTINT (limit))
3150 /* No text property change up to limit. */
3151 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3152 else
3153 /* Text properties change in next_iv. */
3154 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3155 }
3156 }
3157
3158 xassert (STRINGP (it->string)
3159 || (it->stop_charpos >= BEGV
3160 && it->stop_charpos >= IT_CHARPOS (*it)));
3161 }
3162
3163
3164 /* Return the position of the next overlay change after POS in
3165 current_buffer. Value is point-max if no overlay change
3166 follows. This is like `next-overlay-change' but doesn't use
3167 xmalloc. */
3168
3169 static int
3170 next_overlay_change (pos)
3171 int pos;
3172 {
3173 int noverlays;
3174 int endpos;
3175 Lisp_Object *overlays;
3176 int i;
3177
3178 /* Get all overlays at the given position. */
3179 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3180
3181 /* If any of these overlays ends before endpos,
3182 use its ending point instead. */
3183 for (i = 0; i < noverlays; ++i)
3184 {
3185 Lisp_Object oend;
3186 int oendpos;
3187
3188 oend = OVERLAY_END (overlays[i]);
3189 oendpos = OVERLAY_POSITION (oend);
3190 endpos = min (endpos, oendpos);
3191 }
3192
3193 return endpos;
3194 }
3195
3196
3197 \f
3198 /***********************************************************************
3199 Fontification
3200 ***********************************************************************/
3201
3202 /* Handle changes in the `fontified' property of the current buffer by
3203 calling hook functions from Qfontification_functions to fontify
3204 regions of text. */
3205
3206 static enum prop_handled
3207 handle_fontified_prop (it)
3208 struct it *it;
3209 {
3210 Lisp_Object prop, pos;
3211 enum prop_handled handled = HANDLED_NORMALLY;
3212
3213 if (!NILP (Vmemory_full))
3214 return handled;
3215
3216 /* Get the value of the `fontified' property at IT's current buffer
3217 position. (The `fontified' property doesn't have a special
3218 meaning in strings.) If the value is nil, call functions from
3219 Qfontification_functions. */
3220 if (!STRINGP (it->string)
3221 && it->s == NULL
3222 && !NILP (Vfontification_functions)
3223 && !NILP (Vrun_hooks)
3224 && (pos = make_number (IT_CHARPOS (*it)),
3225 prop = Fget_char_property (pos, Qfontified, Qnil),
3226 NILP (prop)))
3227 {
3228 int count = SPECPDL_INDEX ();
3229 Lisp_Object val;
3230
3231 val = Vfontification_functions;
3232 specbind (Qfontification_functions, Qnil);
3233
3234 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3235 safe_call1 (val, pos);
3236 else
3237 {
3238 Lisp_Object globals, fn;
3239 struct gcpro gcpro1, gcpro2;
3240
3241 globals = Qnil;
3242 GCPRO2 (val, globals);
3243
3244 for (; CONSP (val); val = XCDR (val))
3245 {
3246 fn = XCAR (val);
3247
3248 if (EQ (fn, Qt))
3249 {
3250 /* A value of t indicates this hook has a local
3251 binding; it means to run the global binding too.
3252 In a global value, t should not occur. If it
3253 does, we must ignore it to avoid an endless
3254 loop. */
3255 for (globals = Fdefault_value (Qfontification_functions);
3256 CONSP (globals);
3257 globals = XCDR (globals))
3258 {
3259 fn = XCAR (globals);
3260 if (!EQ (fn, Qt))
3261 safe_call1 (fn, pos);
3262 }
3263 }
3264 else
3265 safe_call1 (fn, pos);
3266 }
3267
3268 UNGCPRO;
3269 }
3270
3271 unbind_to (count, Qnil);
3272
3273 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3274 something. This avoids an endless loop if they failed to
3275 fontify the text for which reason ever. */
3276 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3277 handled = HANDLED_RECOMPUTE_PROPS;
3278 }
3279
3280 return handled;
3281 }
3282
3283
3284 \f
3285 /***********************************************************************
3286 Faces
3287 ***********************************************************************/
3288
3289 /* Set up iterator IT from face properties at its current position.
3290 Called from handle_stop. */
3291
3292 static enum prop_handled
3293 handle_face_prop (it)
3294 struct it *it;
3295 {
3296 int new_face_id, next_stop;
3297
3298 if (!STRINGP (it->string))
3299 {
3300 new_face_id
3301 = face_at_buffer_position (it->w,
3302 IT_CHARPOS (*it),
3303 it->region_beg_charpos,
3304 it->region_end_charpos,
3305 &next_stop,
3306 (IT_CHARPOS (*it)
3307 + TEXT_PROP_DISTANCE_LIMIT),
3308 0);
3309
3310 /* Is this a start of a run of characters with box face?
3311 Caveat: this can be called for a freshly initialized
3312 iterator; face_id is -1 in this case. We know that the new
3313 face will not change until limit, i.e. if the new face has a
3314 box, all characters up to limit will have one. But, as
3315 usual, we don't know whether limit is really the end. */
3316 if (new_face_id != it->face_id)
3317 {
3318 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3319
3320 /* If new face has a box but old face has not, this is
3321 the start of a run of characters with box, i.e. it has
3322 a shadow on the left side. The value of face_id of the
3323 iterator will be -1 if this is the initial call that gets
3324 the face. In this case, we have to look in front of IT's
3325 position and see whether there is a face != new_face_id. */
3326 it->start_of_box_run_p
3327 = (new_face->box != FACE_NO_BOX
3328 && (it->face_id >= 0
3329 || IT_CHARPOS (*it) == BEG
3330 || new_face_id != face_before_it_pos (it)));
3331 it->face_box_p = new_face->box != FACE_NO_BOX;
3332 }
3333 }
3334 else
3335 {
3336 int base_face_id, bufpos;
3337
3338 if (it->current.overlay_string_index >= 0)
3339 bufpos = IT_CHARPOS (*it);
3340 else
3341 bufpos = 0;
3342
3343 /* For strings from a buffer, i.e. overlay strings or strings
3344 from a `display' property, use the face at IT's current
3345 buffer position as the base face to merge with, so that
3346 overlay strings appear in the same face as surrounding
3347 text, unless they specify their own faces. */
3348 base_face_id = underlying_face_id (it);
3349
3350 new_face_id = face_at_string_position (it->w,
3351 it->string,
3352 IT_STRING_CHARPOS (*it),
3353 bufpos,
3354 it->region_beg_charpos,
3355 it->region_end_charpos,
3356 &next_stop,
3357 base_face_id, 0);
3358
3359 #if 0 /* This shouldn't be neccessary. Let's check it. */
3360 /* If IT is used to display a mode line we would really like to
3361 use the mode line face instead of the frame's default face. */
3362 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3363 && new_face_id == DEFAULT_FACE_ID)
3364 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3365 #endif
3366
3367 /* Is this a start of a run of characters with box? Caveat:
3368 this can be called for a freshly allocated iterator; face_id
3369 is -1 is this case. We know that the new face will not
3370 change until the next check pos, i.e. if the new face has a
3371 box, all characters up to that position will have a
3372 box. But, as usual, we don't know whether that position
3373 is really the end. */
3374 if (new_face_id != it->face_id)
3375 {
3376 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3377 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3378
3379 /* If new face has a box but old face hasn't, this is the
3380 start of a run of characters with box, i.e. it has a
3381 shadow on the left side. */
3382 it->start_of_box_run_p
3383 = new_face->box && (old_face == NULL || !old_face->box);
3384 it->face_box_p = new_face->box != FACE_NO_BOX;
3385 }
3386 }
3387
3388 it->face_id = new_face_id;
3389 return HANDLED_NORMALLY;
3390 }
3391
3392
3393 /* Return the ID of the face ``underlying'' IT's current position,
3394 which is in a string. If the iterator is associated with a
3395 buffer, return the face at IT's current buffer position.
3396 Otherwise, use the iterator's base_face_id. */
3397
3398 static int
3399 underlying_face_id (it)
3400 struct it *it;
3401 {
3402 int face_id = it->base_face_id, i;
3403
3404 xassert (STRINGP (it->string));
3405
3406 for (i = it->sp - 1; i >= 0; --i)
3407 if (NILP (it->stack[i].string))
3408 face_id = it->stack[i].face_id;
3409
3410 return face_id;
3411 }
3412
3413
3414 /* Compute the face one character before or after the current position
3415 of IT. BEFORE_P non-zero means get the face in front of IT's
3416 position. Value is the id of the face. */
3417
3418 static int
3419 face_before_or_after_it_pos (it, before_p)
3420 struct it *it;
3421 int before_p;
3422 {
3423 int face_id, limit;
3424 int next_check_charpos;
3425 struct text_pos pos;
3426
3427 xassert (it->s == NULL);
3428
3429 if (STRINGP (it->string))
3430 {
3431 int bufpos, base_face_id;
3432
3433 /* No face change past the end of the string (for the case
3434 we are padding with spaces). No face change before the
3435 string start. */
3436 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3437 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3438 return it->face_id;
3439
3440 /* Set pos to the position before or after IT's current position. */
3441 if (before_p)
3442 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3443 else
3444 /* For composition, we must check the character after the
3445 composition. */
3446 pos = (it->what == IT_COMPOSITION
3447 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3448 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3449
3450 if (it->current.overlay_string_index >= 0)
3451 bufpos = IT_CHARPOS (*it);
3452 else
3453 bufpos = 0;
3454
3455 base_face_id = underlying_face_id (it);
3456
3457 /* Get the face for ASCII, or unibyte. */
3458 face_id = face_at_string_position (it->w,
3459 it->string,
3460 CHARPOS (pos),
3461 bufpos,
3462 it->region_beg_charpos,
3463 it->region_end_charpos,
3464 &next_check_charpos,
3465 base_face_id, 0);
3466
3467 /* Correct the face for charsets different from ASCII. Do it
3468 for the multibyte case only. The face returned above is
3469 suitable for unibyte text if IT->string is unibyte. */
3470 if (STRING_MULTIBYTE (it->string))
3471 {
3472 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3473 int rest = SBYTES (it->string) - BYTEPOS (pos);
3474 int c, len;
3475 struct face *face = FACE_FROM_ID (it->f, face_id);
3476
3477 c = string_char_and_length (p, rest, &len);
3478 face_id = FACE_FOR_CHAR (it->f, face, c);
3479 }
3480 }
3481 else
3482 {
3483 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3484 || (IT_CHARPOS (*it) <= BEGV && before_p))
3485 return it->face_id;
3486
3487 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3488 pos = it->current.pos;
3489
3490 if (before_p)
3491 DEC_TEXT_POS (pos, it->multibyte_p);
3492 else
3493 {
3494 if (it->what == IT_COMPOSITION)
3495 /* For composition, we must check the position after the
3496 composition. */
3497 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3498 else
3499 INC_TEXT_POS (pos, it->multibyte_p);
3500 }
3501
3502 /* Determine face for CHARSET_ASCII, or unibyte. */
3503 face_id = face_at_buffer_position (it->w,
3504 CHARPOS (pos),
3505 it->region_beg_charpos,
3506 it->region_end_charpos,
3507 &next_check_charpos,
3508 limit, 0);
3509
3510 /* Correct the face for charsets different from ASCII. Do it
3511 for the multibyte case only. The face returned above is
3512 suitable for unibyte text if current_buffer is unibyte. */
3513 if (it->multibyte_p)
3514 {
3515 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3516 struct face *face = FACE_FROM_ID (it->f, face_id);
3517 face_id = FACE_FOR_CHAR (it->f, face, c);
3518 }
3519 }
3520
3521 return face_id;
3522 }
3523
3524
3525 \f
3526 /***********************************************************************
3527 Invisible text
3528 ***********************************************************************/
3529
3530 /* Set up iterator IT from invisible properties at its current
3531 position. Called from handle_stop. */
3532
3533 static enum prop_handled
3534 handle_invisible_prop (it)
3535 struct it *it;
3536 {
3537 enum prop_handled handled = HANDLED_NORMALLY;
3538
3539 if (STRINGP (it->string))
3540 {
3541 extern Lisp_Object Qinvisible;
3542 Lisp_Object prop, end_charpos, limit, charpos;
3543
3544 /* Get the value of the invisible text property at the
3545 current position. Value will be nil if there is no such
3546 property. */
3547 charpos = make_number (IT_STRING_CHARPOS (*it));
3548 prop = Fget_text_property (charpos, Qinvisible, it->string);
3549
3550 if (!NILP (prop)
3551 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3552 {
3553 handled = HANDLED_RECOMPUTE_PROPS;
3554
3555 /* Get the position at which the next change of the
3556 invisible text property can be found in IT->string.
3557 Value will be nil if the property value is the same for
3558 all the rest of IT->string. */
3559 XSETINT (limit, SCHARS (it->string));
3560 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3561 it->string, limit);
3562
3563 /* Text at current position is invisible. The next
3564 change in the property is at position end_charpos.
3565 Move IT's current position to that position. */
3566 if (INTEGERP (end_charpos)
3567 && XFASTINT (end_charpos) < XFASTINT (limit))
3568 {
3569 struct text_pos old;
3570 old = it->current.string_pos;
3571 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3572 compute_string_pos (&it->current.string_pos, old, it->string);
3573 }
3574 else
3575 {
3576 /* The rest of the string is invisible. If this is an
3577 overlay string, proceed with the next overlay string
3578 or whatever comes and return a character from there. */
3579 if (it->current.overlay_string_index >= 0)
3580 {
3581 next_overlay_string (it);
3582 /* Don't check for overlay strings when we just
3583 finished processing them. */
3584 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3585 }
3586 else
3587 {
3588 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3589 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3590 }
3591 }
3592 }
3593 }
3594 else
3595 {
3596 int invis_p, newpos, next_stop, start_charpos;
3597 Lisp_Object pos, prop, overlay;
3598
3599 /* First of all, is there invisible text at this position? */
3600 start_charpos = IT_CHARPOS (*it);
3601 pos = make_number (IT_CHARPOS (*it));
3602 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3603 &overlay);
3604 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3605
3606 /* If we are on invisible text, skip over it. */
3607 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3608 {
3609 /* Record whether we have to display an ellipsis for the
3610 invisible text. */
3611 int display_ellipsis_p = invis_p == 2;
3612
3613 handled = HANDLED_RECOMPUTE_PROPS;
3614
3615 /* Loop skipping over invisible text. The loop is left at
3616 ZV or with IT on the first char being visible again. */
3617 do
3618 {
3619 /* Try to skip some invisible text. Return value is the
3620 position reached which can be equal to IT's position
3621 if there is nothing invisible here. This skips both
3622 over invisible text properties and overlays with
3623 invisible property. */
3624 newpos = skip_invisible (IT_CHARPOS (*it),
3625 &next_stop, ZV, it->window);
3626
3627 /* If we skipped nothing at all we weren't at invisible
3628 text in the first place. If everything to the end of
3629 the buffer was skipped, end the loop. */
3630 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3631 invis_p = 0;
3632 else
3633 {
3634 /* We skipped some characters but not necessarily
3635 all there are. Check if we ended up on visible
3636 text. Fget_char_property returns the property of
3637 the char before the given position, i.e. if we
3638 get invis_p = 0, this means that the char at
3639 newpos is visible. */
3640 pos = make_number (newpos);
3641 prop = Fget_char_property (pos, Qinvisible, it->window);
3642 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3643 }
3644
3645 /* If we ended up on invisible text, proceed to
3646 skip starting with next_stop. */
3647 if (invis_p)
3648 IT_CHARPOS (*it) = next_stop;
3649
3650 /* If there are adjacent invisible texts, don't lose the
3651 second one's ellipsis. */
3652 if (invis_p == 2)
3653 display_ellipsis_p = 1;
3654 }
3655 while (invis_p);
3656
3657 /* The position newpos is now either ZV or on visible text. */
3658 IT_CHARPOS (*it) = newpos;
3659 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3660
3661 /* If there are before-strings at the start of invisible
3662 text, and the text is invisible because of a text
3663 property, arrange to show before-strings because 20.x did
3664 it that way. (If the text is invisible because of an
3665 overlay property instead of a text property, this is
3666 already handled in the overlay code.) */
3667 if (NILP (overlay)
3668 && get_overlay_strings (it, start_charpos))
3669 {
3670 handled = HANDLED_RECOMPUTE_PROPS;
3671 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3672 }
3673 else if (display_ellipsis_p)
3674 {
3675 /* Make sure that the glyphs of the ellipsis will get
3676 correct `charpos' values. If we would not update
3677 it->position here, the glyphs would belong to the
3678 last visible character _before_ the invisible
3679 text, which confuses `set_cursor_from_row'.
3680
3681 We use the last invisible position instead of the
3682 first because this way the cursor is always drawn on
3683 the first "." of the ellipsis, whenever PT is inside
3684 the invisible text. Otherwise the cursor would be
3685 placed _after_ the ellipsis when the point is after the
3686 first invisible character. */
3687 if (!STRINGP (it->object))
3688 {
3689 it->position.charpos = IT_CHARPOS (*it) - 1;
3690 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3691 }
3692 setup_for_ellipsis (it, 0);
3693 }
3694 }
3695 }
3696
3697 return handled;
3698 }
3699
3700
3701 /* Make iterator IT return `...' next.
3702 Replaces LEN characters from buffer. */
3703
3704 static void
3705 setup_for_ellipsis (it, len)
3706 struct it *it;
3707 int len;
3708 {
3709 /* Use the display table definition for `...'. Invalid glyphs
3710 will be handled by the method returning elements from dpvec. */
3711 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3712 {
3713 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3714 it->dpvec = v->contents;
3715 it->dpend = v->contents + v->size;
3716 }
3717 else
3718 {
3719 /* Default `...'. */
3720 it->dpvec = default_invis_vector;
3721 it->dpend = default_invis_vector + 3;
3722 }
3723
3724 it->dpvec_char_len = len;
3725 it->current.dpvec_index = 0;
3726 it->dpvec_face_id = -1;
3727
3728 /* Remember the current face id in case glyphs specify faces.
3729 IT's face is restored in set_iterator_to_next.
3730 saved_face_id was set to preceding char's face in handle_stop. */
3731 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3732 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3733
3734 it->method = GET_FROM_DISPLAY_VECTOR;
3735 it->ellipsis_p = 1;
3736 }
3737
3738
3739 \f
3740 /***********************************************************************
3741 'display' property
3742 ***********************************************************************/
3743
3744 /* Set up iterator IT from `display' property at its current position.
3745 Called from handle_stop.
3746 We return HANDLED_RETURN if some part of the display property
3747 overrides the display of the buffer text itself.
3748 Otherwise we return HANDLED_NORMALLY. */
3749
3750 static enum prop_handled
3751 handle_display_prop (it)
3752 struct it *it;
3753 {
3754 Lisp_Object prop, object;
3755 struct text_pos *position;
3756 /* Nonzero if some property replaces the display of the text itself. */
3757 int display_replaced_p = 0;
3758
3759 if (STRINGP (it->string))
3760 {
3761 object = it->string;
3762 position = &it->current.string_pos;
3763 }
3764 else
3765 {
3766 XSETWINDOW (object, it->w);
3767 position = &it->current.pos;
3768 }
3769
3770 /* Reset those iterator values set from display property values. */
3771 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3772 it->space_width = Qnil;
3773 it->font_height = Qnil;
3774 it->voffset = 0;
3775
3776 /* We don't support recursive `display' properties, i.e. string
3777 values that have a string `display' property, that have a string
3778 `display' property etc. */
3779 if (!it->string_from_display_prop_p)
3780 it->area = TEXT_AREA;
3781
3782 prop = Fget_char_property (make_number (position->charpos),
3783 Qdisplay, object);
3784 if (NILP (prop))
3785 return HANDLED_NORMALLY;
3786
3787 if (!STRINGP (it->string))
3788 object = it->w->buffer;
3789
3790 if (CONSP (prop)
3791 /* Simple properties. */
3792 && !EQ (XCAR (prop), Qimage)
3793 && !EQ (XCAR (prop), Qspace)
3794 && !EQ (XCAR (prop), Qwhen)
3795 && !EQ (XCAR (prop), Qslice)
3796 && !EQ (XCAR (prop), Qspace_width)
3797 && !EQ (XCAR (prop), Qheight)
3798 && !EQ (XCAR (prop), Qraise)
3799 /* Marginal area specifications. */
3800 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3801 && !EQ (XCAR (prop), Qleft_fringe)
3802 && !EQ (XCAR (prop), Qright_fringe)
3803 && !NILP (XCAR (prop)))
3804 {
3805 for (; CONSP (prop); prop = XCDR (prop))
3806 {
3807 if (handle_single_display_spec (it, XCAR (prop), object,
3808 position, display_replaced_p))
3809 display_replaced_p = 1;
3810 }
3811 }
3812 else if (VECTORP (prop))
3813 {
3814 int i;
3815 for (i = 0; i < ASIZE (prop); ++i)
3816 if (handle_single_display_spec (it, AREF (prop, i), object,
3817 position, display_replaced_p))
3818 display_replaced_p = 1;
3819 }
3820 else
3821 {
3822 int ret = handle_single_display_spec (it, prop, object, position, 0);
3823 if (ret < 0) /* Replaced by "", i.e. nothing. */
3824 return HANDLED_RECOMPUTE_PROPS;
3825 if (ret)
3826 display_replaced_p = 1;
3827 }
3828
3829 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3830 }
3831
3832
3833 /* Value is the position of the end of the `display' property starting
3834 at START_POS in OBJECT. */
3835
3836 static struct text_pos
3837 display_prop_end (it, object, start_pos)
3838 struct it *it;
3839 Lisp_Object object;
3840 struct text_pos start_pos;
3841 {
3842 Lisp_Object end;
3843 struct text_pos end_pos;
3844
3845 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3846 Qdisplay, object, Qnil);
3847 CHARPOS (end_pos) = XFASTINT (end);
3848 if (STRINGP (object))
3849 compute_string_pos (&end_pos, start_pos, it->string);
3850 else
3851 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3852
3853 return end_pos;
3854 }
3855
3856
3857 /* Set up IT from a single `display' specification PROP. OBJECT
3858 is the object in which the `display' property was found. *POSITION
3859 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3860 means that we previously saw a display specification which already
3861 replaced text display with something else, for example an image;
3862 we ignore such properties after the first one has been processed.
3863
3864 If PROP is a `space' or `image' specification, and in some other
3865 cases too, set *POSITION to the position where the `display'
3866 property ends.
3867
3868 Value is non-zero if something was found which replaces the display
3869 of buffer or string text. Specifically, the value is -1 if that
3870 "something" is "nothing". */
3871
3872 static int
3873 handle_single_display_spec (it, spec, object, position,
3874 display_replaced_before_p)
3875 struct it *it;
3876 Lisp_Object spec;
3877 Lisp_Object object;
3878 struct text_pos *position;
3879 int display_replaced_before_p;
3880 {
3881 Lisp_Object form;
3882 Lisp_Object location, value;
3883 struct text_pos start_pos;
3884 int valid_p;
3885
3886 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3887 If the result is non-nil, use VALUE instead of SPEC. */
3888 form = Qt;
3889 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3890 {
3891 spec = XCDR (spec);
3892 if (!CONSP (spec))
3893 return 0;
3894 form = XCAR (spec);
3895 spec = XCDR (spec);
3896 }
3897
3898 if (!NILP (form) && !EQ (form, Qt))
3899 {
3900 int count = SPECPDL_INDEX ();
3901 struct gcpro gcpro1;
3902
3903 /* Bind `object' to the object having the `display' property, a
3904 buffer or string. Bind `position' to the position in the
3905 object where the property was found, and `buffer-position'
3906 to the current position in the buffer. */
3907 specbind (Qobject, object);
3908 specbind (Qposition, make_number (CHARPOS (*position)));
3909 specbind (Qbuffer_position,
3910 make_number (STRINGP (object)
3911 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3912 GCPRO1 (form);
3913 form = safe_eval (form);
3914 UNGCPRO;
3915 unbind_to (count, Qnil);
3916 }
3917
3918 if (NILP (form))
3919 return 0;
3920
3921 /* Handle `(height HEIGHT)' specifications. */
3922 if (CONSP (spec)
3923 && EQ (XCAR (spec), Qheight)
3924 && CONSP (XCDR (spec)))
3925 {
3926 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3927 return 0;
3928
3929 it->font_height = XCAR (XCDR (spec));
3930 if (!NILP (it->font_height))
3931 {
3932 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3933 int new_height = -1;
3934
3935 if (CONSP (it->font_height)
3936 && (EQ (XCAR (it->font_height), Qplus)
3937 || EQ (XCAR (it->font_height), Qminus))
3938 && CONSP (XCDR (it->font_height))
3939 && INTEGERP (XCAR (XCDR (it->font_height))))
3940 {
3941 /* `(+ N)' or `(- N)' where N is an integer. */
3942 int steps = XINT (XCAR (XCDR (it->font_height)));
3943 if (EQ (XCAR (it->font_height), Qplus))
3944 steps = - steps;
3945 it->face_id = smaller_face (it->f, it->face_id, steps);
3946 }
3947 else if (FUNCTIONP (it->font_height))
3948 {
3949 /* Call function with current height as argument.
3950 Value is the new height. */
3951 Lisp_Object height;
3952 height = safe_call1 (it->font_height,
3953 face->lface[LFACE_HEIGHT_INDEX]);
3954 if (NUMBERP (height))
3955 new_height = XFLOATINT (height);
3956 }
3957 else if (NUMBERP (it->font_height))
3958 {
3959 /* Value is a multiple of the canonical char height. */
3960 struct face *face;
3961
3962 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3963 new_height = (XFLOATINT (it->font_height)
3964 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3965 }
3966 else
3967 {
3968 /* Evaluate IT->font_height with `height' bound to the
3969 current specified height to get the new height. */
3970 int count = SPECPDL_INDEX ();
3971
3972 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3973 value = safe_eval (it->font_height);
3974 unbind_to (count, Qnil);
3975
3976 if (NUMBERP (value))
3977 new_height = XFLOATINT (value);
3978 }
3979
3980 if (new_height > 0)
3981 it->face_id = face_with_height (it->f, it->face_id, new_height);
3982 }
3983
3984 return 0;
3985 }
3986
3987 /* Handle `(space_width WIDTH)'. */
3988 if (CONSP (spec)
3989 && EQ (XCAR (spec), Qspace_width)
3990 && CONSP (XCDR (spec)))
3991 {
3992 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3993 return 0;
3994
3995 value = XCAR (XCDR (spec));
3996 if (NUMBERP (value) && XFLOATINT (value) > 0)
3997 it->space_width = value;
3998
3999 return 0;
4000 }
4001
4002 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4003 if (CONSP (spec)
4004 && EQ (XCAR (spec), Qslice))
4005 {
4006 Lisp_Object tem;
4007
4008 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4009 return 0;
4010
4011 if (tem = XCDR (spec), CONSP (tem))
4012 {
4013 it->slice.x = XCAR (tem);
4014 if (tem = XCDR (tem), CONSP (tem))
4015 {
4016 it->slice.y = XCAR (tem);
4017 if (tem = XCDR (tem), CONSP (tem))
4018 {
4019 it->slice.width = XCAR (tem);
4020 if (tem = XCDR (tem), CONSP (tem))
4021 it->slice.height = XCAR (tem);
4022 }
4023 }
4024 }
4025
4026 return 0;
4027 }
4028
4029 /* Handle `(raise FACTOR)'. */
4030 if (CONSP (spec)
4031 && EQ (XCAR (spec), Qraise)
4032 && CONSP (XCDR (spec)))
4033 {
4034 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4035 return 0;
4036
4037 #ifdef HAVE_WINDOW_SYSTEM
4038 value = XCAR (XCDR (spec));
4039 if (NUMBERP (value))
4040 {
4041 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4042 it->voffset = - (XFLOATINT (value)
4043 * (FONT_HEIGHT (face->font)));
4044 }
4045 #endif /* HAVE_WINDOW_SYSTEM */
4046
4047 return 0;
4048 }
4049
4050 /* Don't handle the other kinds of display specifications
4051 inside a string that we got from a `display' property. */
4052 if (it->string_from_display_prop_p)
4053 return 0;
4054
4055 /* Characters having this form of property are not displayed, so
4056 we have to find the end of the property. */
4057 start_pos = *position;
4058 *position = display_prop_end (it, object, start_pos);
4059 value = Qnil;
4060
4061 /* Stop the scan at that end position--we assume that all
4062 text properties change there. */
4063 it->stop_charpos = position->charpos;
4064
4065 /* Handle `(left-fringe BITMAP [FACE])'
4066 and `(right-fringe BITMAP [FACE])'. */
4067 if (CONSP (spec)
4068 && (EQ (XCAR (spec), Qleft_fringe)
4069 || EQ (XCAR (spec), Qright_fringe))
4070 && CONSP (XCDR (spec)))
4071 {
4072 int face_id = DEFAULT_FACE_ID;
4073 int fringe_bitmap;
4074
4075 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4076 /* If we return here, POSITION has been advanced
4077 across the text with this property. */
4078 return 0;
4079
4080 #ifdef HAVE_WINDOW_SYSTEM
4081 value = XCAR (XCDR (spec));
4082 if (!SYMBOLP (value)
4083 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4084 /* If we return here, POSITION has been advanced
4085 across the text with this property. */
4086 return 0;
4087
4088 if (CONSP (XCDR (XCDR (spec))))
4089 {
4090 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4091 int face_id2 = lookup_derived_face (it->f, face_name,
4092 'A', FRINGE_FACE_ID, 0);
4093 if (face_id2 >= 0)
4094 face_id = face_id2;
4095 }
4096
4097 /* Save current settings of IT so that we can restore them
4098 when we are finished with the glyph property value. */
4099
4100 push_it (it);
4101
4102 it->area = TEXT_AREA;
4103 it->what = IT_IMAGE;
4104 it->image_id = -1; /* no image */
4105 it->position = start_pos;
4106 it->object = NILP (object) ? it->w->buffer : object;
4107 it->method = GET_FROM_IMAGE;
4108 it->face_id = face_id;
4109
4110 /* Say that we haven't consumed the characters with
4111 `display' property yet. The call to pop_it in
4112 set_iterator_to_next will clean this up. */
4113 *position = start_pos;
4114
4115 if (EQ (XCAR (spec), Qleft_fringe))
4116 {
4117 it->left_user_fringe_bitmap = fringe_bitmap;
4118 it->left_user_fringe_face_id = face_id;
4119 }
4120 else
4121 {
4122 it->right_user_fringe_bitmap = fringe_bitmap;
4123 it->right_user_fringe_face_id = face_id;
4124 }
4125 #endif /* HAVE_WINDOW_SYSTEM */
4126 return 1;
4127 }
4128
4129 /* Prepare to handle `((margin left-margin) ...)',
4130 `((margin right-margin) ...)' and `((margin nil) ...)'
4131 prefixes for display specifications. */
4132 location = Qunbound;
4133 if (CONSP (spec) && CONSP (XCAR (spec)))
4134 {
4135 Lisp_Object tem;
4136
4137 value = XCDR (spec);
4138 if (CONSP (value))
4139 value = XCAR (value);
4140
4141 tem = XCAR (spec);
4142 if (EQ (XCAR (tem), Qmargin)
4143 && (tem = XCDR (tem),
4144 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4145 (NILP (tem)
4146 || EQ (tem, Qleft_margin)
4147 || EQ (tem, Qright_margin))))
4148 location = tem;
4149 }
4150
4151 if (EQ (location, Qunbound))
4152 {
4153 location = Qnil;
4154 value = spec;
4155 }
4156
4157 /* After this point, VALUE is the property after any
4158 margin prefix has been stripped. It must be a string,
4159 an image specification, or `(space ...)'.
4160
4161 LOCATION specifies where to display: `left-margin',
4162 `right-margin' or nil. */
4163
4164 valid_p = (STRINGP (value)
4165 #ifdef HAVE_WINDOW_SYSTEM
4166 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4167 #endif /* not HAVE_WINDOW_SYSTEM */
4168 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4169
4170 if (valid_p && !display_replaced_before_p)
4171 {
4172 /* Save current settings of IT so that we can restore them
4173 when we are finished with the glyph property value. */
4174 push_it (it);
4175
4176 if (NILP (location))
4177 it->area = TEXT_AREA;
4178 else if (EQ (location, Qleft_margin))
4179 it->area = LEFT_MARGIN_AREA;
4180 else
4181 it->area = RIGHT_MARGIN_AREA;
4182
4183 if (STRINGP (value))
4184 {
4185 if (SCHARS (value) == 0)
4186 {
4187 pop_it (it);
4188 return -1; /* Replaced by "", i.e. nothing. */
4189 }
4190 it->string = value;
4191 it->multibyte_p = STRING_MULTIBYTE (it->string);
4192 it->current.overlay_string_index = -1;
4193 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4194 it->end_charpos = it->string_nchars = SCHARS (it->string);
4195 it->method = GET_FROM_STRING;
4196 it->stop_charpos = 0;
4197 it->string_from_display_prop_p = 1;
4198 /* Say that we haven't consumed the characters with
4199 `display' property yet. The call to pop_it in
4200 set_iterator_to_next will clean this up. */
4201 *position = start_pos;
4202 }
4203 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4204 {
4205 it->method = GET_FROM_STRETCH;
4206 it->object = value;
4207 *position = it->position = start_pos;
4208 }
4209 #ifdef HAVE_WINDOW_SYSTEM
4210 else
4211 {
4212 it->what = IT_IMAGE;
4213 it->image_id = lookup_image (it->f, value);
4214 it->position = start_pos;
4215 it->object = NILP (object) ? it->w->buffer : object;
4216 it->method = GET_FROM_IMAGE;
4217
4218 /* Say that we haven't consumed the characters with
4219 `display' property yet. The call to pop_it in
4220 set_iterator_to_next will clean this up. */
4221 *position = start_pos;
4222 }
4223 #endif /* HAVE_WINDOW_SYSTEM */
4224
4225 return 1;
4226 }
4227
4228 /* Invalid property or property not supported. Restore
4229 POSITION to what it was before. */
4230 *position = start_pos;
4231 return 0;
4232 }
4233
4234
4235 /* Check if SPEC is a display specification value whose text should be
4236 treated as intangible. */
4237
4238 static int
4239 single_display_spec_intangible_p (prop)
4240 Lisp_Object prop;
4241 {
4242 /* Skip over `when FORM'. */
4243 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4244 {
4245 prop = XCDR (prop);
4246 if (!CONSP (prop))
4247 return 0;
4248 prop = XCDR (prop);
4249 }
4250
4251 if (STRINGP (prop))
4252 return 1;
4253
4254 if (!CONSP (prop))
4255 return 0;
4256
4257 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4258 we don't need to treat text as intangible. */
4259 if (EQ (XCAR (prop), Qmargin))
4260 {
4261 prop = XCDR (prop);
4262 if (!CONSP (prop))
4263 return 0;
4264
4265 prop = XCDR (prop);
4266 if (!CONSP (prop)
4267 || EQ (XCAR (prop), Qleft_margin)
4268 || EQ (XCAR (prop), Qright_margin))
4269 return 0;
4270 }
4271
4272 return (CONSP (prop)
4273 && (EQ (XCAR (prop), Qimage)
4274 || EQ (XCAR (prop), Qspace)));
4275 }
4276
4277
4278 /* Check if PROP is a display property value whose text should be
4279 treated as intangible. */
4280
4281 int
4282 display_prop_intangible_p (prop)
4283 Lisp_Object prop;
4284 {
4285 if (CONSP (prop)
4286 && CONSP (XCAR (prop))
4287 && !EQ (Qmargin, XCAR (XCAR (prop))))
4288 {
4289 /* A list of sub-properties. */
4290 while (CONSP (prop))
4291 {
4292 if (single_display_spec_intangible_p (XCAR (prop)))
4293 return 1;
4294 prop = XCDR (prop);
4295 }
4296 }
4297 else if (VECTORP (prop))
4298 {
4299 /* A vector of sub-properties. */
4300 int i;
4301 for (i = 0; i < ASIZE (prop); ++i)
4302 if (single_display_spec_intangible_p (AREF (prop, i)))
4303 return 1;
4304 }
4305 else
4306 return single_display_spec_intangible_p (prop);
4307
4308 return 0;
4309 }
4310
4311
4312 /* Return 1 if PROP is a display sub-property value containing STRING. */
4313
4314 static int
4315 single_display_spec_string_p (prop, string)
4316 Lisp_Object prop, string;
4317 {
4318 if (EQ (string, prop))
4319 return 1;
4320
4321 /* Skip over `when FORM'. */
4322 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4323 {
4324 prop = XCDR (prop);
4325 if (!CONSP (prop))
4326 return 0;
4327 prop = XCDR (prop);
4328 }
4329
4330 if (CONSP (prop))
4331 /* Skip over `margin LOCATION'. */
4332 if (EQ (XCAR (prop), Qmargin))
4333 {
4334 prop = XCDR (prop);
4335 if (!CONSP (prop))
4336 return 0;
4337
4338 prop = XCDR (prop);
4339 if (!CONSP (prop))
4340 return 0;
4341 }
4342
4343 return CONSP (prop) && EQ (XCAR (prop), string);
4344 }
4345
4346
4347 /* Return 1 if STRING appears in the `display' property PROP. */
4348
4349 static int
4350 display_prop_string_p (prop, string)
4351 Lisp_Object prop, string;
4352 {
4353 if (CONSP (prop)
4354 && CONSP (XCAR (prop))
4355 && !EQ (Qmargin, XCAR (XCAR (prop))))
4356 {
4357 /* A list of sub-properties. */
4358 while (CONSP (prop))
4359 {
4360 if (single_display_spec_string_p (XCAR (prop), string))
4361 return 1;
4362 prop = XCDR (prop);
4363 }
4364 }
4365 else if (VECTORP (prop))
4366 {
4367 /* A vector of sub-properties. */
4368 int i;
4369 for (i = 0; i < ASIZE (prop); ++i)
4370 if (single_display_spec_string_p (AREF (prop, i), string))
4371 return 1;
4372 }
4373 else
4374 return single_display_spec_string_p (prop, string);
4375
4376 return 0;
4377 }
4378
4379
4380 /* Determine from which buffer position in W's buffer STRING comes
4381 from. AROUND_CHARPOS is an approximate position where it could
4382 be from. Value is the buffer position or 0 if it couldn't be
4383 determined.
4384
4385 W's buffer must be current.
4386
4387 This function is necessary because we don't record buffer positions
4388 in glyphs generated from strings (to keep struct glyph small).
4389 This function may only use code that doesn't eval because it is
4390 called asynchronously from note_mouse_highlight. */
4391
4392 int
4393 string_buffer_position (w, string, around_charpos)
4394 struct window *w;
4395 Lisp_Object string;
4396 int around_charpos;
4397 {
4398 Lisp_Object limit, prop, pos;
4399 const int MAX_DISTANCE = 1000;
4400 int found = 0;
4401
4402 pos = make_number (around_charpos);
4403 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4404 while (!found && !EQ (pos, limit))
4405 {
4406 prop = Fget_char_property (pos, Qdisplay, Qnil);
4407 if (!NILP (prop) && display_prop_string_p (prop, string))
4408 found = 1;
4409 else
4410 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4411 }
4412
4413 if (!found)
4414 {
4415 pos = make_number (around_charpos);
4416 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4417 while (!found && !EQ (pos, limit))
4418 {
4419 prop = Fget_char_property (pos, Qdisplay, Qnil);
4420 if (!NILP (prop) && display_prop_string_p (prop, string))
4421 found = 1;
4422 else
4423 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4424 limit);
4425 }
4426 }
4427
4428 return found ? XINT (pos) : 0;
4429 }
4430
4431
4432 \f
4433 /***********************************************************************
4434 `composition' property
4435 ***********************************************************************/
4436
4437 /* Set up iterator IT from `composition' property at its current
4438 position. Called from handle_stop. */
4439
4440 static enum prop_handled
4441 handle_composition_prop (it)
4442 struct it *it;
4443 {
4444 Lisp_Object prop, string;
4445 int pos, pos_byte, end;
4446 enum prop_handled handled = HANDLED_NORMALLY;
4447
4448 if (STRINGP (it->string))
4449 {
4450 pos = IT_STRING_CHARPOS (*it);
4451 pos_byte = IT_STRING_BYTEPOS (*it);
4452 string = it->string;
4453 }
4454 else
4455 {
4456 pos = IT_CHARPOS (*it);
4457 pos_byte = IT_BYTEPOS (*it);
4458 string = Qnil;
4459 }
4460
4461 /* If there's a valid composition and point is not inside of the
4462 composition (in the case that the composition is from the current
4463 buffer), draw a glyph composed from the composition components. */
4464 if (find_composition (pos, -1, &pos, &end, &prop, string)
4465 && COMPOSITION_VALID_P (pos, end, prop)
4466 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4467 {
4468 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4469
4470 if (id >= 0)
4471 {
4472 struct composition *cmp = composition_table[id];
4473
4474 if (cmp->glyph_len == 0)
4475 {
4476 /* No glyph. */
4477 if (STRINGP (it->string))
4478 {
4479 IT_STRING_CHARPOS (*it) = end;
4480 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4481 end);
4482 }
4483 else
4484 {
4485 IT_CHARPOS (*it) = end;
4486 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4487 }
4488 return HANDLED_RECOMPUTE_PROPS;
4489 }
4490
4491 push_it (it);
4492 it->method = GET_FROM_COMPOSITION;
4493 it->cmp_id = id;
4494 it->cmp_len = COMPOSITION_LENGTH (prop);
4495 /* For a terminal, draw only the first character of the
4496 components. */
4497 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4498 it->len = (STRINGP (it->string)
4499 ? string_char_to_byte (it->string, end)
4500 : CHAR_TO_BYTE (end)) - pos_byte;
4501 it->stop_charpos = end;
4502 handled = HANDLED_RETURN;
4503 }
4504 }
4505
4506 return handled;
4507 }
4508
4509
4510 \f
4511 /***********************************************************************
4512 Overlay strings
4513 ***********************************************************************/
4514
4515 /* The following structure is used to record overlay strings for
4516 later sorting in load_overlay_strings. */
4517
4518 struct overlay_entry
4519 {
4520 Lisp_Object overlay;
4521 Lisp_Object string;
4522 int priority;
4523 int after_string_p;
4524 };
4525
4526
4527 /* Set up iterator IT from overlay strings at its current position.
4528 Called from handle_stop. */
4529
4530 static enum prop_handled
4531 handle_overlay_change (it)
4532 struct it *it;
4533 {
4534 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4535 return HANDLED_RECOMPUTE_PROPS;
4536 else
4537 return HANDLED_NORMALLY;
4538 }
4539
4540
4541 /* Set up the next overlay string for delivery by IT, if there is an
4542 overlay string to deliver. Called by set_iterator_to_next when the
4543 end of the current overlay string is reached. If there are more
4544 overlay strings to display, IT->string and
4545 IT->current.overlay_string_index are set appropriately here.
4546 Otherwise IT->string is set to nil. */
4547
4548 static void
4549 next_overlay_string (it)
4550 struct it *it;
4551 {
4552 ++it->current.overlay_string_index;
4553 if (it->current.overlay_string_index == it->n_overlay_strings)
4554 {
4555 /* No more overlay strings. Restore IT's settings to what
4556 they were before overlay strings were processed, and
4557 continue to deliver from current_buffer. */
4558 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4559
4560 pop_it (it);
4561 xassert (it->sp > 0
4562 || it->method == GET_FROM_COMPOSITION
4563 || (NILP (it->string)
4564 && it->method == GET_FROM_BUFFER
4565 && it->stop_charpos >= BEGV
4566 && it->stop_charpos <= it->end_charpos));
4567 it->current.overlay_string_index = -1;
4568 it->n_overlay_strings = 0;
4569
4570 /* If we're at the end of the buffer, record that we have
4571 processed the overlay strings there already, so that
4572 next_element_from_buffer doesn't try it again. */
4573 if (IT_CHARPOS (*it) >= it->end_charpos)
4574 it->overlay_strings_at_end_processed_p = 1;
4575
4576 /* If we have to display `...' for invisible text, set
4577 the iterator up for that. */
4578 if (display_ellipsis_p)
4579 setup_for_ellipsis (it, 0);
4580 }
4581 else
4582 {
4583 /* There are more overlay strings to process. If
4584 IT->current.overlay_string_index has advanced to a position
4585 where we must load IT->overlay_strings with more strings, do
4586 it. */
4587 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4588
4589 if (it->current.overlay_string_index && i == 0)
4590 load_overlay_strings (it, 0);
4591
4592 /* Initialize IT to deliver display elements from the overlay
4593 string. */
4594 it->string = it->overlay_strings[i];
4595 it->multibyte_p = STRING_MULTIBYTE (it->string);
4596 SET_TEXT_POS (it->current.string_pos, 0, 0);
4597 it->method = GET_FROM_STRING;
4598 it->stop_charpos = 0;
4599 }
4600
4601 CHECK_IT (it);
4602 }
4603
4604
4605 /* Compare two overlay_entry structures E1 and E2. Used as a
4606 comparison function for qsort in load_overlay_strings. Overlay
4607 strings for the same position are sorted so that
4608
4609 1. All after-strings come in front of before-strings, except
4610 when they come from the same overlay.
4611
4612 2. Within after-strings, strings are sorted so that overlay strings
4613 from overlays with higher priorities come first.
4614
4615 2. Within before-strings, strings are sorted so that overlay
4616 strings from overlays with higher priorities come last.
4617
4618 Value is analogous to strcmp. */
4619
4620
4621 static int
4622 compare_overlay_entries (e1, e2)
4623 void *e1, *e2;
4624 {
4625 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4626 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4627 int result;
4628
4629 if (entry1->after_string_p != entry2->after_string_p)
4630 {
4631 /* Let after-strings appear in front of before-strings if
4632 they come from different overlays. */
4633 if (EQ (entry1->overlay, entry2->overlay))
4634 result = entry1->after_string_p ? 1 : -1;
4635 else
4636 result = entry1->after_string_p ? -1 : 1;
4637 }
4638 else if (entry1->after_string_p)
4639 /* After-strings sorted in order of decreasing priority. */
4640 result = entry2->priority - entry1->priority;
4641 else
4642 /* Before-strings sorted in order of increasing priority. */
4643 result = entry1->priority - entry2->priority;
4644
4645 return result;
4646 }
4647
4648
4649 /* Load the vector IT->overlay_strings with overlay strings from IT's
4650 current buffer position, or from CHARPOS if that is > 0. Set
4651 IT->n_overlays to the total number of overlay strings found.
4652
4653 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4654 a time. On entry into load_overlay_strings,
4655 IT->current.overlay_string_index gives the number of overlay
4656 strings that have already been loaded by previous calls to this
4657 function.
4658
4659 IT->add_overlay_start contains an additional overlay start
4660 position to consider for taking overlay strings from, if non-zero.
4661 This position comes into play when the overlay has an `invisible'
4662 property, and both before and after-strings. When we've skipped to
4663 the end of the overlay, because of its `invisible' property, we
4664 nevertheless want its before-string to appear.
4665 IT->add_overlay_start will contain the overlay start position
4666 in this case.
4667
4668 Overlay strings are sorted so that after-string strings come in
4669 front of before-string strings. Within before and after-strings,
4670 strings are sorted by overlay priority. See also function
4671 compare_overlay_entries. */
4672
4673 static void
4674 load_overlay_strings (it, charpos)
4675 struct it *it;
4676 int charpos;
4677 {
4678 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4679 Lisp_Object overlay, window, str, invisible;
4680 struct Lisp_Overlay *ov;
4681 int start, end;
4682 int size = 20;
4683 int n = 0, i, j, invis_p;
4684 struct overlay_entry *entries
4685 = (struct overlay_entry *) alloca (size * sizeof *entries);
4686
4687 if (charpos <= 0)
4688 charpos = IT_CHARPOS (*it);
4689
4690 /* Append the overlay string STRING of overlay OVERLAY to vector
4691 `entries' which has size `size' and currently contains `n'
4692 elements. AFTER_P non-zero means STRING is an after-string of
4693 OVERLAY. */
4694 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4695 do \
4696 { \
4697 Lisp_Object priority; \
4698 \
4699 if (n == size) \
4700 { \
4701 int new_size = 2 * size; \
4702 struct overlay_entry *old = entries; \
4703 entries = \
4704 (struct overlay_entry *) alloca (new_size \
4705 * sizeof *entries); \
4706 bcopy (old, entries, size * sizeof *entries); \
4707 size = new_size; \
4708 } \
4709 \
4710 entries[n].string = (STRING); \
4711 entries[n].overlay = (OVERLAY); \
4712 priority = Foverlay_get ((OVERLAY), Qpriority); \
4713 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4714 entries[n].after_string_p = (AFTER_P); \
4715 ++n; \
4716 } \
4717 while (0)
4718
4719 /* Process overlay before the overlay center. */
4720 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4721 {
4722 XSETMISC (overlay, ov);
4723 xassert (OVERLAYP (overlay));
4724 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4725 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4726
4727 if (end < charpos)
4728 break;
4729
4730 /* Skip this overlay if it doesn't start or end at IT's current
4731 position. */
4732 if (end != charpos && start != charpos)
4733 continue;
4734
4735 /* Skip this overlay if it doesn't apply to IT->w. */
4736 window = Foverlay_get (overlay, Qwindow);
4737 if (WINDOWP (window) && XWINDOW (window) != it->w)
4738 continue;
4739
4740 /* If the text ``under'' the overlay is invisible, both before-
4741 and after-strings from this overlay are visible; start and
4742 end position are indistinguishable. */
4743 invisible = Foverlay_get (overlay, Qinvisible);
4744 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4745
4746 /* If overlay has a non-empty before-string, record it. */
4747 if ((start == charpos || (end == charpos && invis_p))
4748 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4749 && SCHARS (str))
4750 RECORD_OVERLAY_STRING (overlay, str, 0);
4751
4752 /* If overlay has a non-empty after-string, record it. */
4753 if ((end == charpos || (start == charpos && invis_p))
4754 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4755 && SCHARS (str))
4756 RECORD_OVERLAY_STRING (overlay, str, 1);
4757 }
4758
4759 /* Process overlays after the overlay center. */
4760 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4761 {
4762 XSETMISC (overlay, ov);
4763 xassert (OVERLAYP (overlay));
4764 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4765 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4766
4767 if (start > charpos)
4768 break;
4769
4770 /* Skip this overlay if it doesn't start or end at IT's current
4771 position. */
4772 if (end != charpos && start != charpos)
4773 continue;
4774
4775 /* Skip this overlay if it doesn't apply to IT->w. */
4776 window = Foverlay_get (overlay, Qwindow);
4777 if (WINDOWP (window) && XWINDOW (window) != it->w)
4778 continue;
4779
4780 /* If the text ``under'' the overlay is invisible, it has a zero
4781 dimension, and both before- and after-strings apply. */
4782 invisible = Foverlay_get (overlay, Qinvisible);
4783 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4784
4785 /* If overlay has a non-empty before-string, record it. */
4786 if ((start == charpos || (end == charpos && invis_p))
4787 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4788 && SCHARS (str))
4789 RECORD_OVERLAY_STRING (overlay, str, 0);
4790
4791 /* If overlay has a non-empty after-string, record it. */
4792 if ((end == charpos || (start == charpos && invis_p))
4793 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4794 && SCHARS (str))
4795 RECORD_OVERLAY_STRING (overlay, str, 1);
4796 }
4797
4798 #undef RECORD_OVERLAY_STRING
4799
4800 /* Sort entries. */
4801 if (n > 1)
4802 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4803
4804 /* Record the total number of strings to process. */
4805 it->n_overlay_strings = n;
4806
4807 /* IT->current.overlay_string_index is the number of overlay strings
4808 that have already been consumed by IT. Copy some of the
4809 remaining overlay strings to IT->overlay_strings. */
4810 i = 0;
4811 j = it->current.overlay_string_index;
4812 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4813 it->overlay_strings[i++] = entries[j++].string;
4814
4815 CHECK_IT (it);
4816 }
4817
4818
4819 /* Get the first chunk of overlay strings at IT's current buffer
4820 position, or at CHARPOS if that is > 0. Value is non-zero if at
4821 least one overlay string was found. */
4822
4823 static int
4824 get_overlay_strings_1 (it, charpos, compute_stop_p)
4825 struct it *it;
4826 int charpos;
4827 {
4828 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4829 process. This fills IT->overlay_strings with strings, and sets
4830 IT->n_overlay_strings to the total number of strings to process.
4831 IT->pos.overlay_string_index has to be set temporarily to zero
4832 because load_overlay_strings needs this; it must be set to -1
4833 when no overlay strings are found because a zero value would
4834 indicate a position in the first overlay string. */
4835 it->current.overlay_string_index = 0;
4836 load_overlay_strings (it, charpos);
4837
4838 /* If we found overlay strings, set up IT to deliver display
4839 elements from the first one. Otherwise set up IT to deliver
4840 from current_buffer. */
4841 if (it->n_overlay_strings)
4842 {
4843 /* Make sure we know settings in current_buffer, so that we can
4844 restore meaningful values when we're done with the overlay
4845 strings. */
4846 if (compute_stop_p)
4847 compute_stop_pos (it);
4848 xassert (it->face_id >= 0);
4849
4850 /* Save IT's settings. They are restored after all overlay
4851 strings have been processed. */
4852 xassert (!compute_stop_p || it->sp == 0);
4853 push_it (it);
4854
4855 /* Set up IT to deliver display elements from the first overlay
4856 string. */
4857 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4858 it->string = it->overlay_strings[0];
4859 it->stop_charpos = 0;
4860 xassert (STRINGP (it->string));
4861 it->end_charpos = SCHARS (it->string);
4862 it->multibyte_p = STRING_MULTIBYTE (it->string);
4863 it->method = GET_FROM_STRING;
4864 return 1;
4865 }
4866
4867 it->current.overlay_string_index = -1;
4868 return 0;
4869 }
4870
4871 static int
4872 get_overlay_strings (it, charpos)
4873 struct it *it;
4874 int charpos;
4875 {
4876 it->string = Qnil;
4877 it->method = GET_FROM_BUFFER;
4878
4879 (void) get_overlay_strings_1 (it, charpos, 1);
4880
4881 CHECK_IT (it);
4882
4883 /* Value is non-zero if we found at least one overlay string. */
4884 return STRINGP (it->string);
4885 }
4886
4887
4888 \f
4889 /***********************************************************************
4890 Saving and restoring state
4891 ***********************************************************************/
4892
4893 /* Save current settings of IT on IT->stack. Called, for example,
4894 before setting up IT for an overlay string, to be able to restore
4895 IT's settings to what they were after the overlay string has been
4896 processed. */
4897
4898 static void
4899 push_it (it)
4900 struct it *it;
4901 {
4902 struct iterator_stack_entry *p;
4903
4904 xassert (it->sp < IT_STACK_SIZE);
4905 p = it->stack + it->sp;
4906
4907 p->stop_charpos = it->stop_charpos;
4908 xassert (it->face_id >= 0);
4909 p->face_id = it->face_id;
4910 p->string = it->string;
4911 p->method = it->method;
4912 switch (p->method)
4913 {
4914 case GET_FROM_IMAGE:
4915 p->u.image.object = it->object;
4916 p->u.image.image_id = it->image_id;
4917 p->u.image.slice = it->slice;
4918 break;
4919 case GET_FROM_COMPOSITION:
4920 p->u.comp.object = it->object;
4921 p->u.comp.c = it->c;
4922 p->u.comp.len = it->len;
4923 p->u.comp.cmp_id = it->cmp_id;
4924 p->u.comp.cmp_len = it->cmp_len;
4925 break;
4926 case GET_FROM_STRETCH:
4927 p->u.stretch.object = it->object;
4928 break;
4929 }
4930 p->position = it->position;
4931 p->current = it->current;
4932 p->end_charpos = it->end_charpos;
4933 p->string_nchars = it->string_nchars;
4934 p->area = it->area;
4935 p->multibyte_p = it->multibyte_p;
4936 p->space_width = it->space_width;
4937 p->font_height = it->font_height;
4938 p->voffset = it->voffset;
4939 p->string_from_display_prop_p = it->string_from_display_prop_p;
4940 p->display_ellipsis_p = 0;
4941 ++it->sp;
4942 }
4943
4944
4945 /* Restore IT's settings from IT->stack. Called, for example, when no
4946 more overlay strings must be processed, and we return to delivering
4947 display elements from a buffer, or when the end of a string from a
4948 `display' property is reached and we return to delivering display
4949 elements from an overlay string, or from a buffer. */
4950
4951 static void
4952 pop_it (it)
4953 struct it *it;
4954 {
4955 struct iterator_stack_entry *p;
4956
4957 xassert (it->sp > 0);
4958 --it->sp;
4959 p = it->stack + it->sp;
4960 it->stop_charpos = p->stop_charpos;
4961 it->face_id = p->face_id;
4962 it->current = p->current;
4963 it->position = p->position;
4964 it->string = p->string;
4965 if (NILP (it->string))
4966 SET_TEXT_POS (it->current.string_pos, -1, -1);
4967 it->method = p->method;
4968 switch (it->method)
4969 {
4970 case GET_FROM_IMAGE:
4971 it->image_id = p->u.image.image_id;
4972 it->object = p->u.image.object;
4973 it->slice = p->u.image.slice;
4974 break;
4975 case GET_FROM_COMPOSITION:
4976 it->object = p->u.comp.object;
4977 it->c = p->u.comp.c;
4978 it->len = p->u.comp.len;
4979 it->cmp_id = p->u.comp.cmp_id;
4980 it->cmp_len = p->u.comp.cmp_len;
4981 break;
4982 case GET_FROM_STRETCH:
4983 it->object = p->u.comp.object;
4984 break;
4985 }
4986 it->end_charpos = p->end_charpos;
4987 it->string_nchars = p->string_nchars;
4988 it->area = p->area;
4989 it->multibyte_p = p->multibyte_p;
4990 it->space_width = p->space_width;
4991 it->font_height = p->font_height;
4992 it->voffset = p->voffset;
4993 it->string_from_display_prop_p = p->string_from_display_prop_p;
4994 }
4995
4996
4997 \f
4998 /***********************************************************************
4999 Moving over lines
5000 ***********************************************************************/
5001
5002 /* Set IT's current position to the previous line start. */
5003
5004 static void
5005 back_to_previous_line_start (it)
5006 struct it *it;
5007 {
5008 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5009 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5010 }
5011
5012
5013 /* Move IT to the next line start.
5014
5015 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5016 we skipped over part of the text (as opposed to moving the iterator
5017 continuously over the text). Otherwise, don't change the value
5018 of *SKIPPED_P.
5019
5020 Newlines may come from buffer text, overlay strings, or strings
5021 displayed via the `display' property. That's the reason we can't
5022 simply use find_next_newline_no_quit.
5023
5024 Note that this function may not skip over invisible text that is so
5025 because of text properties and immediately follows a newline. If
5026 it would, function reseat_at_next_visible_line_start, when called
5027 from set_iterator_to_next, would effectively make invisible
5028 characters following a newline part of the wrong glyph row, which
5029 leads to wrong cursor motion. */
5030
5031 static int
5032 forward_to_next_line_start (it, skipped_p)
5033 struct it *it;
5034 int *skipped_p;
5035 {
5036 int old_selective, newline_found_p, n;
5037 const int MAX_NEWLINE_DISTANCE = 500;
5038
5039 /* If already on a newline, just consume it to avoid unintended
5040 skipping over invisible text below. */
5041 if (it->what == IT_CHARACTER
5042 && it->c == '\n'
5043 && CHARPOS (it->position) == IT_CHARPOS (*it))
5044 {
5045 set_iterator_to_next (it, 0);
5046 it->c = 0;
5047 return 1;
5048 }
5049
5050 /* Don't handle selective display in the following. It's (a)
5051 unnecessary because it's done by the caller, and (b) leads to an
5052 infinite recursion because next_element_from_ellipsis indirectly
5053 calls this function. */
5054 old_selective = it->selective;
5055 it->selective = 0;
5056
5057 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5058 from buffer text. */
5059 for (n = newline_found_p = 0;
5060 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5061 n += STRINGP (it->string) ? 0 : 1)
5062 {
5063 if (!get_next_display_element (it))
5064 return 0;
5065 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5066 set_iterator_to_next (it, 0);
5067 }
5068
5069 /* If we didn't find a newline near enough, see if we can use a
5070 short-cut. */
5071 if (!newline_found_p)
5072 {
5073 int start = IT_CHARPOS (*it);
5074 int limit = find_next_newline_no_quit (start, 1);
5075 Lisp_Object pos;
5076
5077 xassert (!STRINGP (it->string));
5078
5079 /* If there isn't any `display' property in sight, and no
5080 overlays, we can just use the position of the newline in
5081 buffer text. */
5082 if (it->stop_charpos >= limit
5083 || ((pos = Fnext_single_property_change (make_number (start),
5084 Qdisplay,
5085 Qnil, make_number (limit)),
5086 NILP (pos))
5087 && next_overlay_change (start) == ZV))
5088 {
5089 IT_CHARPOS (*it) = limit;
5090 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5091 *skipped_p = newline_found_p = 1;
5092 }
5093 else
5094 {
5095 while (get_next_display_element (it)
5096 && !newline_found_p)
5097 {
5098 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5099 set_iterator_to_next (it, 0);
5100 }
5101 }
5102 }
5103
5104 it->selective = old_selective;
5105 return newline_found_p;
5106 }
5107
5108
5109 /* Set IT's current position to the previous visible line start. Skip
5110 invisible text that is so either due to text properties or due to
5111 selective display. Caution: this does not change IT->current_x and
5112 IT->hpos. */
5113
5114 static void
5115 back_to_previous_visible_line_start (it)
5116 struct it *it;
5117 {
5118 while (IT_CHARPOS (*it) > BEGV)
5119 {
5120 back_to_previous_line_start (it);
5121
5122 if (IT_CHARPOS (*it) <= BEGV)
5123 break;
5124
5125 /* If selective > 0, then lines indented more than that values
5126 are invisible. */
5127 if (it->selective > 0
5128 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5129 (double) it->selective)) /* iftc */
5130 continue;
5131
5132 /* Check the newline before point for invisibility. */
5133 {
5134 Lisp_Object prop;
5135 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5136 Qinvisible, it->window);
5137 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5138 continue;
5139 }
5140
5141 if (IT_CHARPOS (*it) <= BEGV)
5142 break;
5143
5144 {
5145 struct it it2;
5146 int pos;
5147 int beg, end;
5148 Lisp_Object val, overlay;
5149
5150 /* If newline is part of a composition, continue from start of composition */
5151 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5152 && beg < IT_CHARPOS (*it))
5153 goto replaced;
5154
5155 /* If newline is replaced by a display property, find start of overlay
5156 or interval and continue search from that point. */
5157 it2 = *it;
5158 pos = --IT_CHARPOS (it2);
5159 --IT_BYTEPOS (it2);
5160 it2.sp = 0;
5161 if (handle_display_prop (&it2) == HANDLED_RETURN
5162 && !NILP (val = get_char_property_and_overlay
5163 (make_number (pos), Qdisplay, Qnil, &overlay))
5164 && (OVERLAYP (overlay)
5165 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5166 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5167 goto replaced;
5168
5169 /* Newline is not replaced by anything -- so we are done. */
5170 break;
5171
5172 replaced:
5173 if (beg < BEGV)
5174 beg = BEGV;
5175 IT_CHARPOS (*it) = beg;
5176 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5177 }
5178 }
5179
5180 it->continuation_lines_width = 0;
5181
5182 xassert (IT_CHARPOS (*it) >= BEGV);
5183 xassert (IT_CHARPOS (*it) == BEGV
5184 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5185 CHECK_IT (it);
5186 }
5187
5188
5189 /* Reseat iterator IT at the previous visible line start. Skip
5190 invisible text that is so either due to text properties or due to
5191 selective display. At the end, update IT's overlay information,
5192 face information etc. */
5193
5194 void
5195 reseat_at_previous_visible_line_start (it)
5196 struct it *it;
5197 {
5198 back_to_previous_visible_line_start (it);
5199 reseat (it, it->current.pos, 1);
5200 CHECK_IT (it);
5201 }
5202
5203
5204 /* Reseat iterator IT on the next visible line start in the current
5205 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5206 preceding the line start. Skip over invisible text that is so
5207 because of selective display. Compute faces, overlays etc at the
5208 new position. Note that this function does not skip over text that
5209 is invisible because of text properties. */
5210
5211 static void
5212 reseat_at_next_visible_line_start (it, on_newline_p)
5213 struct it *it;
5214 int on_newline_p;
5215 {
5216 int newline_found_p, skipped_p = 0;
5217
5218 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5219
5220 /* Skip over lines that are invisible because they are indented
5221 more than the value of IT->selective. */
5222 if (it->selective > 0)
5223 while (IT_CHARPOS (*it) < ZV
5224 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5225 (double) it->selective)) /* iftc */
5226 {
5227 xassert (IT_BYTEPOS (*it) == BEGV
5228 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5229 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5230 }
5231
5232 /* Position on the newline if that's what's requested. */
5233 if (on_newline_p && newline_found_p)
5234 {
5235 if (STRINGP (it->string))
5236 {
5237 if (IT_STRING_CHARPOS (*it) > 0)
5238 {
5239 --IT_STRING_CHARPOS (*it);
5240 --IT_STRING_BYTEPOS (*it);
5241 }
5242 }
5243 else if (IT_CHARPOS (*it) > BEGV)
5244 {
5245 --IT_CHARPOS (*it);
5246 --IT_BYTEPOS (*it);
5247 reseat (it, it->current.pos, 0);
5248 }
5249 }
5250 else if (skipped_p)
5251 reseat (it, it->current.pos, 0);
5252
5253 CHECK_IT (it);
5254 }
5255
5256
5257 \f
5258 /***********************************************************************
5259 Changing an iterator's position
5260 ***********************************************************************/
5261
5262 /* Change IT's current position to POS in current_buffer. If FORCE_P
5263 is non-zero, always check for text properties at the new position.
5264 Otherwise, text properties are only looked up if POS >=
5265 IT->check_charpos of a property. */
5266
5267 static void
5268 reseat (it, pos, force_p)
5269 struct it *it;
5270 struct text_pos pos;
5271 int force_p;
5272 {
5273 int original_pos = IT_CHARPOS (*it);
5274
5275 reseat_1 (it, pos, 0);
5276
5277 /* Determine where to check text properties. Avoid doing it
5278 where possible because text property lookup is very expensive. */
5279 if (force_p
5280 || CHARPOS (pos) > it->stop_charpos
5281 || CHARPOS (pos) < original_pos)
5282 handle_stop (it);
5283
5284 CHECK_IT (it);
5285 }
5286
5287
5288 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5289 IT->stop_pos to POS, also. */
5290
5291 static void
5292 reseat_1 (it, pos, set_stop_p)
5293 struct it *it;
5294 struct text_pos pos;
5295 int set_stop_p;
5296 {
5297 /* Don't call this function when scanning a C string. */
5298 xassert (it->s == NULL);
5299
5300 /* POS must be a reasonable value. */
5301 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5302
5303 it->current.pos = it->position = pos;
5304 XSETBUFFER (it->object, current_buffer);
5305 it->end_charpos = ZV;
5306 it->dpvec = NULL;
5307 it->current.dpvec_index = -1;
5308 it->current.overlay_string_index = -1;
5309 IT_STRING_CHARPOS (*it) = -1;
5310 IT_STRING_BYTEPOS (*it) = -1;
5311 it->string = Qnil;
5312 it->method = GET_FROM_BUFFER;
5313 it->object = it->w->buffer;
5314 it->area = TEXT_AREA;
5315 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5316 it->sp = 0;
5317 it->string_from_display_prop_p = 0;
5318 it->face_before_selective_p = 0;
5319
5320 if (set_stop_p)
5321 it->stop_charpos = CHARPOS (pos);
5322 }
5323
5324
5325 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5326 If S is non-null, it is a C string to iterate over. Otherwise,
5327 STRING gives a Lisp string to iterate over.
5328
5329 If PRECISION > 0, don't return more then PRECISION number of
5330 characters from the string.
5331
5332 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5333 characters have been returned. FIELD_WIDTH < 0 means an infinite
5334 field width.
5335
5336 MULTIBYTE = 0 means disable processing of multibyte characters,
5337 MULTIBYTE > 0 means enable it,
5338 MULTIBYTE < 0 means use IT->multibyte_p.
5339
5340 IT must be initialized via a prior call to init_iterator before
5341 calling this function. */
5342
5343 static void
5344 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5345 struct it *it;
5346 unsigned char *s;
5347 Lisp_Object string;
5348 int charpos;
5349 int precision, field_width, multibyte;
5350 {
5351 /* No region in strings. */
5352 it->region_beg_charpos = it->region_end_charpos = -1;
5353
5354 /* No text property checks performed by default, but see below. */
5355 it->stop_charpos = -1;
5356
5357 /* Set iterator position and end position. */
5358 bzero (&it->current, sizeof it->current);
5359 it->current.overlay_string_index = -1;
5360 it->current.dpvec_index = -1;
5361 xassert (charpos >= 0);
5362
5363 /* If STRING is specified, use its multibyteness, otherwise use the
5364 setting of MULTIBYTE, if specified. */
5365 if (multibyte >= 0)
5366 it->multibyte_p = multibyte > 0;
5367
5368 if (s == NULL)
5369 {
5370 xassert (STRINGP (string));
5371 it->string = string;
5372 it->s = NULL;
5373 it->end_charpos = it->string_nchars = SCHARS (string);
5374 it->method = GET_FROM_STRING;
5375 it->current.string_pos = string_pos (charpos, string);
5376 }
5377 else
5378 {
5379 it->s = s;
5380 it->string = Qnil;
5381
5382 /* Note that we use IT->current.pos, not it->current.string_pos,
5383 for displaying C strings. */
5384 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5385 if (it->multibyte_p)
5386 {
5387 it->current.pos = c_string_pos (charpos, s, 1);
5388 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5389 }
5390 else
5391 {
5392 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5393 it->end_charpos = it->string_nchars = strlen (s);
5394 }
5395
5396 it->method = GET_FROM_C_STRING;
5397 }
5398
5399 /* PRECISION > 0 means don't return more than PRECISION characters
5400 from the string. */
5401 if (precision > 0 && it->end_charpos - charpos > precision)
5402 it->end_charpos = it->string_nchars = charpos + precision;
5403
5404 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5405 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5406 FIELD_WIDTH < 0 means infinite field width. This is useful for
5407 padding with `-' at the end of a mode line. */
5408 if (field_width < 0)
5409 field_width = INFINITY;
5410 if (field_width > it->end_charpos - charpos)
5411 it->end_charpos = charpos + field_width;
5412
5413 /* Use the standard display table for displaying strings. */
5414 if (DISP_TABLE_P (Vstandard_display_table))
5415 it->dp = XCHAR_TABLE (Vstandard_display_table);
5416
5417 it->stop_charpos = charpos;
5418 CHECK_IT (it);
5419 }
5420
5421
5422 \f
5423 /***********************************************************************
5424 Iteration
5425 ***********************************************************************/
5426
5427 /* Map enum it_method value to corresponding next_element_from_* function. */
5428
5429 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5430 {
5431 next_element_from_buffer,
5432 next_element_from_display_vector,
5433 next_element_from_composition,
5434 next_element_from_string,
5435 next_element_from_c_string,
5436 next_element_from_image,
5437 next_element_from_stretch
5438 };
5439
5440
5441 /* Load IT's display element fields with information about the next
5442 display element from the current position of IT. Value is zero if
5443 end of buffer (or C string) is reached. */
5444
5445 static struct frame *last_escape_glyph_frame = NULL;
5446 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5447 static int last_escape_glyph_merged_face_id = 0;
5448
5449 int
5450 get_next_display_element (it)
5451 struct it *it;
5452 {
5453 /* Non-zero means that we found a display element. Zero means that
5454 we hit the end of what we iterate over. Performance note: the
5455 function pointer `method' used here turns out to be faster than
5456 using a sequence of if-statements. */
5457 int success_p;
5458
5459 get_next:
5460 success_p = (*get_next_element[it->method]) (it);
5461
5462 if (it->what == IT_CHARACTER)
5463 {
5464 /* Map via display table or translate control characters.
5465 IT->c, IT->len etc. have been set to the next character by
5466 the function call above. If we have a display table, and it
5467 contains an entry for IT->c, translate it. Don't do this if
5468 IT->c itself comes from a display table, otherwise we could
5469 end up in an infinite recursion. (An alternative could be to
5470 count the recursion depth of this function and signal an
5471 error when a certain maximum depth is reached.) Is it worth
5472 it? */
5473 if (success_p && it->dpvec == NULL)
5474 {
5475 Lisp_Object dv;
5476
5477 if (it->dp
5478 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5479 VECTORP (dv)))
5480 {
5481 struct Lisp_Vector *v = XVECTOR (dv);
5482
5483 /* Return the first character from the display table
5484 entry, if not empty. If empty, don't display the
5485 current character. */
5486 if (v->size)
5487 {
5488 it->dpvec_char_len = it->len;
5489 it->dpvec = v->contents;
5490 it->dpend = v->contents + v->size;
5491 it->current.dpvec_index = 0;
5492 it->dpvec_face_id = -1;
5493 it->saved_face_id = it->face_id;
5494 it->method = GET_FROM_DISPLAY_VECTOR;
5495 it->ellipsis_p = 0;
5496 }
5497 else
5498 {
5499 set_iterator_to_next (it, 0);
5500 }
5501 goto get_next;
5502 }
5503
5504 /* Translate control characters into `\003' or `^C' form.
5505 Control characters coming from a display table entry are
5506 currently not translated because we use IT->dpvec to hold
5507 the translation. This could easily be changed but I
5508 don't believe that it is worth doing.
5509
5510 If it->multibyte_p is nonzero, eight-bit characters and
5511 non-printable multibyte characters are also translated to
5512 octal form.
5513
5514 If it->multibyte_p is zero, eight-bit characters that
5515 don't have corresponding multibyte char code are also
5516 translated to octal form. */
5517 else if ((it->c < ' '
5518 && (it->area != TEXT_AREA
5519 /* In mode line, treat \n like other crl chars. */
5520 || (it->c != '\t'
5521 && it->glyph_row && it->glyph_row->mode_line_p)
5522 || (it->c != '\n' && it->c != '\t')))
5523 || (it->multibyte_p
5524 ? ((it->c >= 127
5525 && it->len == 1)
5526 || !CHAR_PRINTABLE_P (it->c)
5527 || (!NILP (Vnobreak_char_display)
5528 && (it->c == 0x8a0 || it->c == 0x8ad
5529 || it->c == 0x920 || it->c == 0x92d
5530 || it->c == 0xe20 || it->c == 0xe2d
5531 || it->c == 0xf20 || it->c == 0xf2d)))
5532 : (it->c >= 127
5533 && (!unibyte_display_via_language_environment
5534 || it->c == unibyte_char_to_multibyte (it->c)))))
5535 {
5536 /* IT->c is a control character which must be displayed
5537 either as '\003' or as `^C' where the '\\' and '^'
5538 can be defined in the display table. Fill
5539 IT->ctl_chars with glyphs for what we have to
5540 display. Then, set IT->dpvec to these glyphs. */
5541 GLYPH g;
5542 int ctl_len;
5543 int face_id, lface_id = 0 ;
5544 GLYPH escape_glyph;
5545
5546 /* Handle control characters with ^. */
5547
5548 if (it->c < 128 && it->ctl_arrow_p)
5549 {
5550 g = '^'; /* default glyph for Control */
5551 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5552 if (it->dp
5553 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5554 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5555 {
5556 g = XINT (DISP_CTRL_GLYPH (it->dp));
5557 lface_id = FAST_GLYPH_FACE (g);
5558 }
5559 if (lface_id)
5560 {
5561 g = FAST_GLYPH_CHAR (g);
5562 face_id = merge_faces (it->f, Qt, lface_id,
5563 it->face_id);
5564 }
5565 else if (it->f == last_escape_glyph_frame
5566 && it->face_id == last_escape_glyph_face_id)
5567 {
5568 face_id = last_escape_glyph_merged_face_id;
5569 }
5570 else
5571 {
5572 /* Merge the escape-glyph face into the current face. */
5573 face_id = merge_faces (it->f, Qescape_glyph, 0,
5574 it->face_id);
5575 last_escape_glyph_frame = it->f;
5576 last_escape_glyph_face_id = it->face_id;
5577 last_escape_glyph_merged_face_id = face_id;
5578 }
5579
5580 XSETINT (it->ctl_chars[0], g);
5581 g = it->c ^ 0100;
5582 XSETINT (it->ctl_chars[1], g);
5583 ctl_len = 2;
5584 goto display_control;
5585 }
5586
5587 /* Handle non-break space in the mode where it only gets
5588 highlighting. */
5589
5590 if (EQ (Vnobreak_char_display, Qt)
5591 && (it->c == 0x8a0 || it->c == 0x920
5592 || it->c == 0xe20 || it->c == 0xf20))
5593 {
5594 /* Merge the no-break-space face into the current face. */
5595 face_id = merge_faces (it->f, Qnobreak_space, 0,
5596 it->face_id);
5597
5598 g = it->c = ' ';
5599 XSETINT (it->ctl_chars[0], g);
5600 ctl_len = 1;
5601 goto display_control;
5602 }
5603
5604 /* Handle sequences that start with the "escape glyph". */
5605
5606 /* the default escape glyph is \. */
5607 escape_glyph = '\\';
5608
5609 if (it->dp
5610 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5611 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5612 {
5613 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5614 lface_id = FAST_GLYPH_FACE (escape_glyph);
5615 }
5616 if (lface_id)
5617 {
5618 /* The display table specified a face.
5619 Merge it into face_id and also into escape_glyph. */
5620 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5621 face_id = merge_faces (it->f, Qt, lface_id,
5622 it->face_id);
5623 }
5624 else if (it->f == last_escape_glyph_frame
5625 && it->face_id == last_escape_glyph_face_id)
5626 {
5627 face_id = last_escape_glyph_merged_face_id;
5628 }
5629 else
5630 {
5631 /* Merge the escape-glyph face into the current face. */
5632 face_id = merge_faces (it->f, Qescape_glyph, 0,
5633 it->face_id);
5634 last_escape_glyph_frame = it->f;
5635 last_escape_glyph_face_id = it->face_id;
5636 last_escape_glyph_merged_face_id = face_id;
5637 }
5638
5639 /* Handle soft hyphens in the mode where they only get
5640 highlighting. */
5641
5642 if (EQ (Vnobreak_char_display, Qt)
5643 && (it->c == 0x8ad || it->c == 0x92d
5644 || it->c == 0xe2d || it->c == 0xf2d))
5645 {
5646 g = it->c = '-';
5647 XSETINT (it->ctl_chars[0], g);
5648 ctl_len = 1;
5649 goto display_control;
5650 }
5651
5652 /* Handle non-break space and soft hyphen
5653 with the escape glyph. */
5654
5655 if (it->c == 0x8a0 || it->c == 0x8ad
5656 || it->c == 0x920 || it->c == 0x92d
5657 || it->c == 0xe20 || it->c == 0xe2d
5658 || it->c == 0xf20 || it->c == 0xf2d)
5659 {
5660 XSETINT (it->ctl_chars[0], escape_glyph);
5661 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5662 XSETINT (it->ctl_chars[1], g);
5663 ctl_len = 2;
5664 goto display_control;
5665 }
5666
5667 {
5668 unsigned char str[MAX_MULTIBYTE_LENGTH];
5669 int len;
5670 int i;
5671
5672 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5673 if (SINGLE_BYTE_CHAR_P (it->c))
5674 str[0] = it->c, len = 1;
5675 else
5676 {
5677 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5678 if (len < 0)
5679 {
5680 /* It's an invalid character, which shouldn't
5681 happen actually, but due to bugs it may
5682 happen. Let's print the char as is, there's
5683 not much meaningful we can do with it. */
5684 str[0] = it->c;
5685 str[1] = it->c >> 8;
5686 str[2] = it->c >> 16;
5687 str[3] = it->c >> 24;
5688 len = 4;
5689 }
5690 }
5691
5692 for (i = 0; i < len; i++)
5693 {
5694 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5695 /* Insert three more glyphs into IT->ctl_chars for
5696 the octal display of the character. */
5697 g = ((str[i] >> 6) & 7) + '0';
5698 XSETINT (it->ctl_chars[i * 4 + 1], g);
5699 g = ((str[i] >> 3) & 7) + '0';
5700 XSETINT (it->ctl_chars[i * 4 + 2], g);
5701 g = (str[i] & 7) + '0';
5702 XSETINT (it->ctl_chars[i * 4 + 3], g);
5703 }
5704 ctl_len = len * 4;
5705 }
5706
5707 display_control:
5708 /* Set up IT->dpvec and return first character from it. */
5709 it->dpvec_char_len = it->len;
5710 it->dpvec = it->ctl_chars;
5711 it->dpend = it->dpvec + ctl_len;
5712 it->current.dpvec_index = 0;
5713 it->dpvec_face_id = face_id;
5714 it->saved_face_id = it->face_id;
5715 it->method = GET_FROM_DISPLAY_VECTOR;
5716 it->ellipsis_p = 0;
5717 goto get_next;
5718 }
5719 }
5720
5721 /* Adjust face id for a multibyte character. There are no
5722 multibyte character in unibyte text. */
5723 if (it->multibyte_p
5724 && success_p
5725 && FRAME_WINDOW_P (it->f))
5726 {
5727 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5728 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5729 }
5730 }
5731
5732 /* Is this character the last one of a run of characters with
5733 box? If yes, set IT->end_of_box_run_p to 1. */
5734 if (it->face_box_p
5735 && it->s == NULL)
5736 {
5737 int face_id;
5738 struct face *face;
5739
5740 it->end_of_box_run_p
5741 = ((face_id = face_after_it_pos (it),
5742 face_id != it->face_id)
5743 && (face = FACE_FROM_ID (it->f, face_id),
5744 face->box == FACE_NO_BOX));
5745 }
5746
5747 /* Value is 0 if end of buffer or string reached. */
5748 return success_p;
5749 }
5750
5751
5752 /* Move IT to the next display element.
5753
5754 RESEAT_P non-zero means if called on a newline in buffer text,
5755 skip to the next visible line start.
5756
5757 Functions get_next_display_element and set_iterator_to_next are
5758 separate because I find this arrangement easier to handle than a
5759 get_next_display_element function that also increments IT's
5760 position. The way it is we can first look at an iterator's current
5761 display element, decide whether it fits on a line, and if it does,
5762 increment the iterator position. The other way around we probably
5763 would either need a flag indicating whether the iterator has to be
5764 incremented the next time, or we would have to implement a
5765 decrement position function which would not be easy to write. */
5766
5767 void
5768 set_iterator_to_next (it, reseat_p)
5769 struct it *it;
5770 int reseat_p;
5771 {
5772 /* Reset flags indicating start and end of a sequence of characters
5773 with box. Reset them at the start of this function because
5774 moving the iterator to a new position might set them. */
5775 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5776
5777 switch (it->method)
5778 {
5779 case GET_FROM_BUFFER:
5780 /* The current display element of IT is a character from
5781 current_buffer. Advance in the buffer, and maybe skip over
5782 invisible lines that are so because of selective display. */
5783 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5784 reseat_at_next_visible_line_start (it, 0);
5785 else
5786 {
5787 xassert (it->len != 0);
5788 IT_BYTEPOS (*it) += it->len;
5789 IT_CHARPOS (*it) += 1;
5790 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5791 }
5792 break;
5793
5794 case GET_FROM_COMPOSITION:
5795 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5796 xassert (it->sp > 0);
5797 pop_it (it);
5798 if (it->method == GET_FROM_STRING)
5799 {
5800 IT_STRING_BYTEPOS (*it) += it->len;
5801 IT_STRING_CHARPOS (*it) += it->cmp_len;
5802 it->object = it->string;
5803 goto consider_string_end;
5804 }
5805 else if (it->method == GET_FROM_BUFFER)
5806 {
5807 IT_BYTEPOS (*it) += it->len;
5808 IT_CHARPOS (*it) += it->cmp_len;
5809 it->object = it->w->buffer;
5810 }
5811 break;
5812
5813 case GET_FROM_C_STRING:
5814 /* Current display element of IT is from a C string. */
5815 IT_BYTEPOS (*it) += it->len;
5816 IT_CHARPOS (*it) += 1;
5817 break;
5818
5819 case GET_FROM_DISPLAY_VECTOR:
5820 /* Current display element of IT is from a display table entry.
5821 Advance in the display table definition. Reset it to null if
5822 end reached, and continue with characters from buffers/
5823 strings. */
5824 ++it->current.dpvec_index;
5825
5826 /* Restore face of the iterator to what they were before the
5827 display vector entry (these entries may contain faces). */
5828 it->face_id = it->saved_face_id;
5829
5830 if (it->dpvec + it->current.dpvec_index == it->dpend)
5831 {
5832 int recheck_faces = it->ellipsis_p;
5833
5834 if (it->s)
5835 it->method = GET_FROM_C_STRING;
5836 else if (STRINGP (it->string))
5837 it->method = GET_FROM_STRING;
5838 else
5839 {
5840 it->method = GET_FROM_BUFFER;
5841 it->object = it->w->buffer;
5842 }
5843
5844 it->dpvec = NULL;
5845 it->current.dpvec_index = -1;
5846
5847 /* Skip over characters which were displayed via IT->dpvec. */
5848 if (it->dpvec_char_len < 0)
5849 reseat_at_next_visible_line_start (it, 1);
5850 else if (it->dpvec_char_len > 0)
5851 {
5852 if (it->method == GET_FROM_STRING
5853 && it->n_overlay_strings > 0)
5854 it->ignore_overlay_strings_at_pos_p = 1;
5855 it->len = it->dpvec_char_len;
5856 set_iterator_to_next (it, reseat_p);
5857 }
5858
5859 /* Maybe recheck faces after display vector */
5860 if (recheck_faces)
5861 it->stop_charpos = IT_CHARPOS (*it);
5862 }
5863 break;
5864
5865 case GET_FROM_STRING:
5866 /* Current display element is a character from a Lisp string. */
5867 xassert (it->s == NULL && STRINGP (it->string));
5868 IT_STRING_BYTEPOS (*it) += it->len;
5869 IT_STRING_CHARPOS (*it) += 1;
5870
5871 consider_string_end:
5872
5873 if (it->current.overlay_string_index >= 0)
5874 {
5875 /* IT->string is an overlay string. Advance to the
5876 next, if there is one. */
5877 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5878 next_overlay_string (it);
5879 }
5880 else
5881 {
5882 /* IT->string is not an overlay string. If we reached
5883 its end, and there is something on IT->stack, proceed
5884 with what is on the stack. This can be either another
5885 string, this time an overlay string, or a buffer. */
5886 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5887 && it->sp > 0)
5888 {
5889 pop_it (it);
5890 if (it->method == GET_FROM_STRING)
5891 goto consider_string_end;
5892 }
5893 }
5894 break;
5895
5896 case GET_FROM_IMAGE:
5897 case GET_FROM_STRETCH:
5898 /* The position etc with which we have to proceed are on
5899 the stack. The position may be at the end of a string,
5900 if the `display' property takes up the whole string. */
5901 xassert (it->sp > 0);
5902 pop_it (it);
5903 if (it->method == GET_FROM_STRING)
5904 goto consider_string_end;
5905 break;
5906
5907 default:
5908 /* There are no other methods defined, so this should be a bug. */
5909 abort ();
5910 }
5911
5912 xassert (it->method != GET_FROM_STRING
5913 || (STRINGP (it->string)
5914 && IT_STRING_CHARPOS (*it) >= 0));
5915 }
5916
5917 /* Load IT's display element fields with information about the next
5918 display element which comes from a display table entry or from the
5919 result of translating a control character to one of the forms `^C'
5920 or `\003'.
5921
5922 IT->dpvec holds the glyphs to return as characters.
5923 IT->saved_face_id holds the face id before the display vector--
5924 it is restored into IT->face_idin set_iterator_to_next. */
5925
5926 static int
5927 next_element_from_display_vector (it)
5928 struct it *it;
5929 {
5930 /* Precondition. */
5931 xassert (it->dpvec && it->current.dpvec_index >= 0);
5932
5933 it->face_id = it->saved_face_id;
5934
5935 if (INTEGERP (*it->dpvec)
5936 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5937 {
5938 GLYPH g;
5939
5940 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5941 it->c = FAST_GLYPH_CHAR (g);
5942 it->len = CHAR_BYTES (it->c);
5943
5944 /* The entry may contain a face id to use. Such a face id is
5945 the id of a Lisp face, not a realized face. A face id of
5946 zero means no face is specified. */
5947 if (it->dpvec_face_id >= 0)
5948 it->face_id = it->dpvec_face_id;
5949 else
5950 {
5951 int lface_id = FAST_GLYPH_FACE (g);
5952 if (lface_id > 0)
5953 it->face_id = merge_faces (it->f, Qt, lface_id,
5954 it->saved_face_id);
5955 }
5956 }
5957 else
5958 /* Display table entry is invalid. Return a space. */
5959 it->c = ' ', it->len = 1;
5960
5961 /* Don't change position and object of the iterator here. They are
5962 still the values of the character that had this display table
5963 entry or was translated, and that's what we want. */
5964 it->what = IT_CHARACTER;
5965 return 1;
5966 }
5967
5968
5969 /* Load IT with the next display element from Lisp string IT->string.
5970 IT->current.string_pos is the current position within the string.
5971 If IT->current.overlay_string_index >= 0, the Lisp string is an
5972 overlay string. */
5973
5974 static int
5975 next_element_from_string (it)
5976 struct it *it;
5977 {
5978 struct text_pos position;
5979
5980 xassert (STRINGP (it->string));
5981 xassert (IT_STRING_CHARPOS (*it) >= 0);
5982 position = it->current.string_pos;
5983
5984 /* Time to check for invisible text? */
5985 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5986 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5987 {
5988 handle_stop (it);
5989
5990 /* Since a handler may have changed IT->method, we must
5991 recurse here. */
5992 return get_next_display_element (it);
5993 }
5994
5995 if (it->current.overlay_string_index >= 0)
5996 {
5997 /* Get the next character from an overlay string. In overlay
5998 strings, There is no field width or padding with spaces to
5999 do. */
6000 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6001 {
6002 it->what = IT_EOB;
6003 return 0;
6004 }
6005 else if (STRING_MULTIBYTE (it->string))
6006 {
6007 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6008 const unsigned char *s = (SDATA (it->string)
6009 + IT_STRING_BYTEPOS (*it));
6010 it->c = string_char_and_length (s, remaining, &it->len);
6011 }
6012 else
6013 {
6014 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6015 it->len = 1;
6016 }
6017 }
6018 else
6019 {
6020 /* Get the next character from a Lisp string that is not an
6021 overlay string. Such strings come from the mode line, for
6022 example. We may have to pad with spaces, or truncate the
6023 string. See also next_element_from_c_string. */
6024 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6025 {
6026 it->what = IT_EOB;
6027 return 0;
6028 }
6029 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6030 {
6031 /* Pad with spaces. */
6032 it->c = ' ', it->len = 1;
6033 CHARPOS (position) = BYTEPOS (position) = -1;
6034 }
6035 else if (STRING_MULTIBYTE (it->string))
6036 {
6037 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6038 const unsigned char *s = (SDATA (it->string)
6039 + IT_STRING_BYTEPOS (*it));
6040 it->c = string_char_and_length (s, maxlen, &it->len);
6041 }
6042 else
6043 {
6044 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6045 it->len = 1;
6046 }
6047 }
6048
6049 /* Record what we have and where it came from. Note that we store a
6050 buffer position in IT->position although it could arguably be a
6051 string position. */
6052 it->what = IT_CHARACTER;
6053 it->object = it->string;
6054 it->position = position;
6055 return 1;
6056 }
6057
6058
6059 /* Load IT with next display element from C string IT->s.
6060 IT->string_nchars is the maximum number of characters to return
6061 from the string. IT->end_charpos may be greater than
6062 IT->string_nchars when this function is called, in which case we
6063 may have to return padding spaces. Value is zero if end of string
6064 reached, including padding spaces. */
6065
6066 static int
6067 next_element_from_c_string (it)
6068 struct it *it;
6069 {
6070 int success_p = 1;
6071
6072 xassert (it->s);
6073 it->what = IT_CHARACTER;
6074 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6075 it->object = Qnil;
6076
6077 /* IT's position can be greater IT->string_nchars in case a field
6078 width or precision has been specified when the iterator was
6079 initialized. */
6080 if (IT_CHARPOS (*it) >= it->end_charpos)
6081 {
6082 /* End of the game. */
6083 it->what = IT_EOB;
6084 success_p = 0;
6085 }
6086 else if (IT_CHARPOS (*it) >= it->string_nchars)
6087 {
6088 /* Pad with spaces. */
6089 it->c = ' ', it->len = 1;
6090 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6091 }
6092 else if (it->multibyte_p)
6093 {
6094 /* Implementation note: The calls to strlen apparently aren't a
6095 performance problem because there is no noticeable performance
6096 difference between Emacs running in unibyte or multibyte mode. */
6097 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6098 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6099 maxlen, &it->len);
6100 }
6101 else
6102 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6103
6104 return success_p;
6105 }
6106
6107
6108 /* Set up IT to return characters from an ellipsis, if appropriate.
6109 The definition of the ellipsis glyphs may come from a display table
6110 entry. This function Fills IT with the first glyph from the
6111 ellipsis if an ellipsis is to be displayed. */
6112
6113 static int
6114 next_element_from_ellipsis (it)
6115 struct it *it;
6116 {
6117 if (it->selective_display_ellipsis_p)
6118 setup_for_ellipsis (it, it->len);
6119 else
6120 {
6121 /* The face at the current position may be different from the
6122 face we find after the invisible text. Remember what it
6123 was in IT->saved_face_id, and signal that it's there by
6124 setting face_before_selective_p. */
6125 it->saved_face_id = it->face_id;
6126 it->method = GET_FROM_BUFFER;
6127 it->object = it->w->buffer;
6128 reseat_at_next_visible_line_start (it, 1);
6129 it->face_before_selective_p = 1;
6130 }
6131
6132 return get_next_display_element (it);
6133 }
6134
6135
6136 /* Deliver an image display element. The iterator IT is already
6137 filled with image information (done in handle_display_prop). Value
6138 is always 1. */
6139
6140
6141 static int
6142 next_element_from_image (it)
6143 struct it *it;
6144 {
6145 it->what = IT_IMAGE;
6146 return 1;
6147 }
6148
6149
6150 /* Fill iterator IT with next display element from a stretch glyph
6151 property. IT->object is the value of the text property. Value is
6152 always 1. */
6153
6154 static int
6155 next_element_from_stretch (it)
6156 struct it *it;
6157 {
6158 it->what = IT_STRETCH;
6159 return 1;
6160 }
6161
6162
6163 /* Load IT with the next display element from current_buffer. Value
6164 is zero if end of buffer reached. IT->stop_charpos is the next
6165 position at which to stop and check for text properties or buffer
6166 end. */
6167
6168 static int
6169 next_element_from_buffer (it)
6170 struct it *it;
6171 {
6172 int success_p = 1;
6173
6174 /* Check this assumption, otherwise, we would never enter the
6175 if-statement, below. */
6176 xassert (IT_CHARPOS (*it) >= BEGV
6177 && IT_CHARPOS (*it) <= it->stop_charpos);
6178
6179 if (IT_CHARPOS (*it) >= it->stop_charpos)
6180 {
6181 if (IT_CHARPOS (*it) >= it->end_charpos)
6182 {
6183 int overlay_strings_follow_p;
6184
6185 /* End of the game, except when overlay strings follow that
6186 haven't been returned yet. */
6187 if (it->overlay_strings_at_end_processed_p)
6188 overlay_strings_follow_p = 0;
6189 else
6190 {
6191 it->overlay_strings_at_end_processed_p = 1;
6192 overlay_strings_follow_p = get_overlay_strings (it, 0);
6193 }
6194
6195 if (overlay_strings_follow_p)
6196 success_p = get_next_display_element (it);
6197 else
6198 {
6199 it->what = IT_EOB;
6200 it->position = it->current.pos;
6201 success_p = 0;
6202 }
6203 }
6204 else
6205 {
6206 handle_stop (it);
6207 return get_next_display_element (it);
6208 }
6209 }
6210 else
6211 {
6212 /* No face changes, overlays etc. in sight, so just return a
6213 character from current_buffer. */
6214 unsigned char *p;
6215
6216 /* Maybe run the redisplay end trigger hook. Performance note:
6217 This doesn't seem to cost measurable time. */
6218 if (it->redisplay_end_trigger_charpos
6219 && it->glyph_row
6220 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6221 run_redisplay_end_trigger_hook (it);
6222
6223 /* Get the next character, maybe multibyte. */
6224 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6225 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6226 {
6227 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6228 - IT_BYTEPOS (*it));
6229 it->c = string_char_and_length (p, maxlen, &it->len);
6230 }
6231 else
6232 it->c = *p, it->len = 1;
6233
6234 /* Record what we have and where it came from. */
6235 it->what = IT_CHARACTER;;
6236 it->object = it->w->buffer;
6237 it->position = it->current.pos;
6238
6239 /* Normally we return the character found above, except when we
6240 really want to return an ellipsis for selective display. */
6241 if (it->selective)
6242 {
6243 if (it->c == '\n')
6244 {
6245 /* A value of selective > 0 means hide lines indented more
6246 than that number of columns. */
6247 if (it->selective > 0
6248 && IT_CHARPOS (*it) + 1 < ZV
6249 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6250 IT_BYTEPOS (*it) + 1,
6251 (double) it->selective)) /* iftc */
6252 {
6253 success_p = next_element_from_ellipsis (it);
6254 it->dpvec_char_len = -1;
6255 }
6256 }
6257 else if (it->c == '\r' && it->selective == -1)
6258 {
6259 /* A value of selective == -1 means that everything from the
6260 CR to the end of the line is invisible, with maybe an
6261 ellipsis displayed for it. */
6262 success_p = next_element_from_ellipsis (it);
6263 it->dpvec_char_len = -1;
6264 }
6265 }
6266 }
6267
6268 /* Value is zero if end of buffer reached. */
6269 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6270 return success_p;
6271 }
6272
6273
6274 /* Run the redisplay end trigger hook for IT. */
6275
6276 static void
6277 run_redisplay_end_trigger_hook (it)
6278 struct it *it;
6279 {
6280 Lisp_Object args[3];
6281
6282 /* IT->glyph_row should be non-null, i.e. we should be actually
6283 displaying something, or otherwise we should not run the hook. */
6284 xassert (it->glyph_row);
6285
6286 /* Set up hook arguments. */
6287 args[0] = Qredisplay_end_trigger_functions;
6288 args[1] = it->window;
6289 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6290 it->redisplay_end_trigger_charpos = 0;
6291
6292 /* Since we are *trying* to run these functions, don't try to run
6293 them again, even if they get an error. */
6294 it->w->redisplay_end_trigger = Qnil;
6295 Frun_hook_with_args (3, args);
6296
6297 /* Notice if it changed the face of the character we are on. */
6298 handle_face_prop (it);
6299 }
6300
6301
6302 /* Deliver a composition display element. The iterator IT is already
6303 filled with composition information (done in
6304 handle_composition_prop). Value is always 1. */
6305
6306 static int
6307 next_element_from_composition (it)
6308 struct it *it;
6309 {
6310 it->what = IT_COMPOSITION;
6311 it->position = (STRINGP (it->string)
6312 ? it->current.string_pos
6313 : it->current.pos);
6314 if (STRINGP (it->string))
6315 it->object = it->string;
6316 else
6317 it->object = it->w->buffer;
6318 return 1;
6319 }
6320
6321
6322 \f
6323 /***********************************************************************
6324 Moving an iterator without producing glyphs
6325 ***********************************************************************/
6326
6327 /* Check if iterator is at a position corresponding to a valid buffer
6328 position after some move_it_ call. */
6329
6330 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6331 ((it)->method == GET_FROM_STRING \
6332 ? IT_STRING_CHARPOS (*it) == 0 \
6333 : 1)
6334
6335
6336 /* Move iterator IT to a specified buffer or X position within one
6337 line on the display without producing glyphs.
6338
6339 OP should be a bit mask including some or all of these bits:
6340 MOVE_TO_X: Stop on reaching x-position TO_X.
6341 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6342 Regardless of OP's value, stop in reaching the end of the display line.
6343
6344 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6345 This means, in particular, that TO_X includes window's horizontal
6346 scroll amount.
6347
6348 The return value has several possible values that
6349 say what condition caused the scan to stop:
6350
6351 MOVE_POS_MATCH_OR_ZV
6352 - when TO_POS or ZV was reached.
6353
6354 MOVE_X_REACHED
6355 -when TO_X was reached before TO_POS or ZV were reached.
6356
6357 MOVE_LINE_CONTINUED
6358 - when we reached the end of the display area and the line must
6359 be continued.
6360
6361 MOVE_LINE_TRUNCATED
6362 - when we reached the end of the display area and the line is
6363 truncated.
6364
6365 MOVE_NEWLINE_OR_CR
6366 - when we stopped at a line end, i.e. a newline or a CR and selective
6367 display is on. */
6368
6369 static enum move_it_result
6370 move_it_in_display_line_to (it, to_charpos, to_x, op)
6371 struct it *it;
6372 int to_charpos, to_x, op;
6373 {
6374 enum move_it_result result = MOVE_UNDEFINED;
6375 struct glyph_row *saved_glyph_row;
6376
6377 /* Don't produce glyphs in produce_glyphs. */
6378 saved_glyph_row = it->glyph_row;
6379 it->glyph_row = NULL;
6380
6381 #define BUFFER_POS_REACHED_P() \
6382 ((op & MOVE_TO_POS) != 0 \
6383 && BUFFERP (it->object) \
6384 && IT_CHARPOS (*it) >= to_charpos \
6385 && (it->method == GET_FROM_BUFFER \
6386 || (it->method == GET_FROM_DISPLAY_VECTOR \
6387 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6388
6389
6390 while (1)
6391 {
6392 int x, i, ascent = 0, descent = 0;
6393
6394 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6395 if ((op & MOVE_TO_POS) != 0
6396 && BUFFERP (it->object)
6397 && it->method == GET_FROM_BUFFER
6398 && IT_CHARPOS (*it) > to_charpos)
6399 {
6400 result = MOVE_POS_MATCH_OR_ZV;
6401 break;
6402 }
6403
6404 /* Stop when ZV reached.
6405 We used to stop here when TO_CHARPOS reached as well, but that is
6406 too soon if this glyph does not fit on this line. So we handle it
6407 explicitly below. */
6408 if (!get_next_display_element (it)
6409 || (it->truncate_lines_p
6410 && BUFFER_POS_REACHED_P ()))
6411 {
6412 result = MOVE_POS_MATCH_OR_ZV;
6413 break;
6414 }
6415
6416 /* The call to produce_glyphs will get the metrics of the
6417 display element IT is loaded with. We record in x the
6418 x-position before this display element in case it does not
6419 fit on the line. */
6420 x = it->current_x;
6421
6422 /* Remember the line height so far in case the next element doesn't
6423 fit on the line. */
6424 if (!it->truncate_lines_p)
6425 {
6426 ascent = it->max_ascent;
6427 descent = it->max_descent;
6428 }
6429
6430 PRODUCE_GLYPHS (it);
6431
6432 if (it->area != TEXT_AREA)
6433 {
6434 set_iterator_to_next (it, 1);
6435 continue;
6436 }
6437
6438 /* The number of glyphs we get back in IT->nglyphs will normally
6439 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6440 character on a terminal frame, or (iii) a line end. For the
6441 second case, IT->nglyphs - 1 padding glyphs will be present
6442 (on X frames, there is only one glyph produced for a
6443 composite character.
6444
6445 The behavior implemented below means, for continuation lines,
6446 that as many spaces of a TAB as fit on the current line are
6447 displayed there. For terminal frames, as many glyphs of a
6448 multi-glyph character are displayed in the current line, too.
6449 This is what the old redisplay code did, and we keep it that
6450 way. Under X, the whole shape of a complex character must
6451 fit on the line or it will be completely displayed in the
6452 next line.
6453
6454 Note that both for tabs and padding glyphs, all glyphs have
6455 the same width. */
6456 if (it->nglyphs)
6457 {
6458 /* More than one glyph or glyph doesn't fit on line. All
6459 glyphs have the same width. */
6460 int single_glyph_width = it->pixel_width / it->nglyphs;
6461 int new_x;
6462 int x_before_this_char = x;
6463 int hpos_before_this_char = it->hpos;
6464
6465 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6466 {
6467 new_x = x + single_glyph_width;
6468
6469 /* We want to leave anything reaching TO_X to the caller. */
6470 if ((op & MOVE_TO_X) && new_x > to_x)
6471 {
6472 if (BUFFER_POS_REACHED_P ())
6473 goto buffer_pos_reached;
6474 it->current_x = x;
6475 result = MOVE_X_REACHED;
6476 break;
6477 }
6478 else if (/* Lines are continued. */
6479 !it->truncate_lines_p
6480 && (/* And glyph doesn't fit on the line. */
6481 new_x > it->last_visible_x
6482 /* Or it fits exactly and we're on a window
6483 system frame. */
6484 || (new_x == it->last_visible_x
6485 && FRAME_WINDOW_P (it->f))))
6486 {
6487 if (/* IT->hpos == 0 means the very first glyph
6488 doesn't fit on the line, e.g. a wide image. */
6489 it->hpos == 0
6490 || (new_x == it->last_visible_x
6491 && FRAME_WINDOW_P (it->f)))
6492 {
6493 ++it->hpos;
6494 it->current_x = new_x;
6495
6496 /* The character's last glyph just barely fits
6497 in this row. */
6498 if (i == it->nglyphs - 1)
6499 {
6500 /* If this is the destination position,
6501 return a position *before* it in this row,
6502 now that we know it fits in this row. */
6503 if (BUFFER_POS_REACHED_P ())
6504 {
6505 it->hpos = hpos_before_this_char;
6506 it->current_x = x_before_this_char;
6507 result = MOVE_POS_MATCH_OR_ZV;
6508 break;
6509 }
6510
6511 set_iterator_to_next (it, 1);
6512 #ifdef HAVE_WINDOW_SYSTEM
6513 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6514 {
6515 if (!get_next_display_element (it))
6516 {
6517 result = MOVE_POS_MATCH_OR_ZV;
6518 break;
6519 }
6520 if (BUFFER_POS_REACHED_P ())
6521 {
6522 if (ITERATOR_AT_END_OF_LINE_P (it))
6523 result = MOVE_POS_MATCH_OR_ZV;
6524 else
6525 result = MOVE_LINE_CONTINUED;
6526 break;
6527 }
6528 if (ITERATOR_AT_END_OF_LINE_P (it))
6529 {
6530 result = MOVE_NEWLINE_OR_CR;
6531 break;
6532 }
6533 }
6534 #endif /* HAVE_WINDOW_SYSTEM */
6535 }
6536 }
6537 else
6538 {
6539 it->current_x = x;
6540 it->max_ascent = ascent;
6541 it->max_descent = descent;
6542 }
6543
6544 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6545 IT_CHARPOS (*it)));
6546 result = MOVE_LINE_CONTINUED;
6547 break;
6548 }
6549 else if (BUFFER_POS_REACHED_P ())
6550 goto buffer_pos_reached;
6551 else if (new_x > it->first_visible_x)
6552 {
6553 /* Glyph is visible. Increment number of glyphs that
6554 would be displayed. */
6555 ++it->hpos;
6556 }
6557 else
6558 {
6559 /* Glyph is completely off the left margin of the display
6560 area. Nothing to do. */
6561 }
6562 }
6563
6564 if (result != MOVE_UNDEFINED)
6565 break;
6566 }
6567 else if (BUFFER_POS_REACHED_P ())
6568 {
6569 buffer_pos_reached:
6570 it->current_x = x;
6571 it->max_ascent = ascent;
6572 it->max_descent = descent;
6573 result = MOVE_POS_MATCH_OR_ZV;
6574 break;
6575 }
6576 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6577 {
6578 /* Stop when TO_X specified and reached. This check is
6579 necessary here because of lines consisting of a line end,
6580 only. The line end will not produce any glyphs and we
6581 would never get MOVE_X_REACHED. */
6582 xassert (it->nglyphs == 0);
6583 result = MOVE_X_REACHED;
6584 break;
6585 }
6586
6587 /* Is this a line end? If yes, we're done. */
6588 if (ITERATOR_AT_END_OF_LINE_P (it))
6589 {
6590 result = MOVE_NEWLINE_OR_CR;
6591 break;
6592 }
6593
6594 /* The current display element has been consumed. Advance
6595 to the next. */
6596 set_iterator_to_next (it, 1);
6597
6598 /* Stop if lines are truncated and IT's current x-position is
6599 past the right edge of the window now. */
6600 if (it->truncate_lines_p
6601 && it->current_x >= it->last_visible_x)
6602 {
6603 #ifdef HAVE_WINDOW_SYSTEM
6604 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6605 {
6606 if (!get_next_display_element (it)
6607 || BUFFER_POS_REACHED_P ())
6608 {
6609 result = MOVE_POS_MATCH_OR_ZV;
6610 break;
6611 }
6612 if (ITERATOR_AT_END_OF_LINE_P (it))
6613 {
6614 result = MOVE_NEWLINE_OR_CR;
6615 break;
6616 }
6617 }
6618 #endif /* HAVE_WINDOW_SYSTEM */
6619 result = MOVE_LINE_TRUNCATED;
6620 break;
6621 }
6622 }
6623
6624 #undef BUFFER_POS_REACHED_P
6625
6626 /* Restore the iterator settings altered at the beginning of this
6627 function. */
6628 it->glyph_row = saved_glyph_row;
6629 return result;
6630 }
6631
6632
6633 /* Move IT forward until it satisfies one or more of the criteria in
6634 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6635
6636 OP is a bit-mask that specifies where to stop, and in particular,
6637 which of those four position arguments makes a difference. See the
6638 description of enum move_operation_enum.
6639
6640 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6641 screen line, this function will set IT to the next position >
6642 TO_CHARPOS. */
6643
6644 void
6645 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6646 struct it *it;
6647 int to_charpos, to_x, to_y, to_vpos;
6648 int op;
6649 {
6650 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6651 int line_height;
6652 int reached = 0;
6653
6654 for (;;)
6655 {
6656 if (op & MOVE_TO_VPOS)
6657 {
6658 /* If no TO_CHARPOS and no TO_X specified, stop at the
6659 start of the line TO_VPOS. */
6660 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6661 {
6662 if (it->vpos == to_vpos)
6663 {
6664 reached = 1;
6665 break;
6666 }
6667 else
6668 skip = move_it_in_display_line_to (it, -1, -1, 0);
6669 }
6670 else
6671 {
6672 /* TO_VPOS >= 0 means stop at TO_X in the line at
6673 TO_VPOS, or at TO_POS, whichever comes first. */
6674 if (it->vpos == to_vpos)
6675 {
6676 reached = 2;
6677 break;
6678 }
6679
6680 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6681
6682 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6683 {
6684 reached = 3;
6685 break;
6686 }
6687 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6688 {
6689 /* We have reached TO_X but not in the line we want. */
6690 skip = move_it_in_display_line_to (it, to_charpos,
6691 -1, MOVE_TO_POS);
6692 if (skip == MOVE_POS_MATCH_OR_ZV)
6693 {
6694 reached = 4;
6695 break;
6696 }
6697 }
6698 }
6699 }
6700 else if (op & MOVE_TO_Y)
6701 {
6702 struct it it_backup;
6703
6704 /* TO_Y specified means stop at TO_X in the line containing
6705 TO_Y---or at TO_CHARPOS if this is reached first. The
6706 problem is that we can't really tell whether the line
6707 contains TO_Y before we have completely scanned it, and
6708 this may skip past TO_X. What we do is to first scan to
6709 TO_X.
6710
6711 If TO_X is not specified, use a TO_X of zero. The reason
6712 is to make the outcome of this function more predictable.
6713 If we didn't use TO_X == 0, we would stop at the end of
6714 the line which is probably not what a caller would expect
6715 to happen. */
6716 skip = move_it_in_display_line_to (it, to_charpos,
6717 ((op & MOVE_TO_X)
6718 ? to_x : 0),
6719 (MOVE_TO_X
6720 | (op & MOVE_TO_POS)));
6721
6722 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6723 if (skip == MOVE_POS_MATCH_OR_ZV)
6724 {
6725 reached = 5;
6726 break;
6727 }
6728
6729 /* If TO_X was reached, we would like to know whether TO_Y
6730 is in the line. This can only be said if we know the
6731 total line height which requires us to scan the rest of
6732 the line. */
6733 if (skip == MOVE_X_REACHED)
6734 {
6735 it_backup = *it;
6736 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6737 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6738 op & MOVE_TO_POS);
6739 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6740 }
6741
6742 /* Now, decide whether TO_Y is in this line. */
6743 line_height = it->max_ascent + it->max_descent;
6744 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6745
6746 if (to_y >= it->current_y
6747 && to_y < it->current_y + line_height)
6748 {
6749 if (skip == MOVE_X_REACHED)
6750 /* If TO_Y is in this line and TO_X was reached above,
6751 we scanned too far. We have to restore IT's settings
6752 to the ones before skipping. */
6753 *it = it_backup;
6754 reached = 6;
6755 }
6756 else if (skip == MOVE_X_REACHED)
6757 {
6758 skip = skip2;
6759 if (skip == MOVE_POS_MATCH_OR_ZV)
6760 reached = 7;
6761 }
6762
6763 if (reached)
6764 break;
6765 }
6766 else
6767 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6768
6769 switch (skip)
6770 {
6771 case MOVE_POS_MATCH_OR_ZV:
6772 reached = 8;
6773 goto out;
6774
6775 case MOVE_NEWLINE_OR_CR:
6776 set_iterator_to_next (it, 1);
6777 it->continuation_lines_width = 0;
6778 break;
6779
6780 case MOVE_LINE_TRUNCATED:
6781 it->continuation_lines_width = 0;
6782 reseat_at_next_visible_line_start (it, 0);
6783 if ((op & MOVE_TO_POS) != 0
6784 && IT_CHARPOS (*it) > to_charpos)
6785 {
6786 reached = 9;
6787 goto out;
6788 }
6789 break;
6790
6791 case MOVE_LINE_CONTINUED:
6792 it->continuation_lines_width += it->current_x;
6793 break;
6794
6795 default:
6796 abort ();
6797 }
6798
6799 /* Reset/increment for the next run. */
6800 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6801 it->current_x = it->hpos = 0;
6802 it->current_y += it->max_ascent + it->max_descent;
6803 ++it->vpos;
6804 last_height = it->max_ascent + it->max_descent;
6805 last_max_ascent = it->max_ascent;
6806 it->max_ascent = it->max_descent = 0;
6807 }
6808
6809 out:
6810
6811 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6812 }
6813
6814
6815 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6816
6817 If DY > 0, move IT backward at least that many pixels. DY = 0
6818 means move IT backward to the preceding line start or BEGV. This
6819 function may move over more than DY pixels if IT->current_y - DY
6820 ends up in the middle of a line; in this case IT->current_y will be
6821 set to the top of the line moved to. */
6822
6823 void
6824 move_it_vertically_backward (it, dy)
6825 struct it *it;
6826 int dy;
6827 {
6828 int nlines, h;
6829 struct it it2, it3;
6830 int start_pos;
6831
6832 move_further_back:
6833 xassert (dy >= 0);
6834
6835 start_pos = IT_CHARPOS (*it);
6836
6837 /* Estimate how many newlines we must move back. */
6838 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6839
6840 /* Set the iterator's position that many lines back. */
6841 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6842 back_to_previous_visible_line_start (it);
6843
6844 /* Reseat the iterator here. When moving backward, we don't want
6845 reseat to skip forward over invisible text, set up the iterator
6846 to deliver from overlay strings at the new position etc. So,
6847 use reseat_1 here. */
6848 reseat_1 (it, it->current.pos, 1);
6849
6850 /* We are now surely at a line start. */
6851 it->current_x = it->hpos = 0;
6852 it->continuation_lines_width = 0;
6853
6854 /* Move forward and see what y-distance we moved. First move to the
6855 start of the next line so that we get its height. We need this
6856 height to be able to tell whether we reached the specified
6857 y-distance. */
6858 it2 = *it;
6859 it2.max_ascent = it2.max_descent = 0;
6860 do
6861 {
6862 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6863 MOVE_TO_POS | MOVE_TO_VPOS);
6864 }
6865 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6866 xassert (IT_CHARPOS (*it) >= BEGV);
6867 it3 = it2;
6868
6869 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6870 xassert (IT_CHARPOS (*it) >= BEGV);
6871 /* H is the actual vertical distance from the position in *IT
6872 and the starting position. */
6873 h = it2.current_y - it->current_y;
6874 /* NLINES is the distance in number of lines. */
6875 nlines = it2.vpos - it->vpos;
6876
6877 /* Correct IT's y and vpos position
6878 so that they are relative to the starting point. */
6879 it->vpos -= nlines;
6880 it->current_y -= h;
6881
6882 if (dy == 0)
6883 {
6884 /* DY == 0 means move to the start of the screen line. The
6885 value of nlines is > 0 if continuation lines were involved. */
6886 if (nlines > 0)
6887 move_it_by_lines (it, nlines, 1);
6888 #if 0
6889 /* I think this assert is bogus if buffer contains
6890 invisible text or images. KFS. */
6891 xassert (IT_CHARPOS (*it) <= start_pos);
6892 #endif
6893 }
6894 else
6895 {
6896 /* The y-position we try to reach, relative to *IT.
6897 Note that H has been subtracted in front of the if-statement. */
6898 int target_y = it->current_y + h - dy;
6899 int y0 = it3.current_y;
6900 int y1 = line_bottom_y (&it3);
6901 int line_height = y1 - y0;
6902
6903 /* If we did not reach target_y, try to move further backward if
6904 we can. If we moved too far backward, try to move forward. */
6905 if (target_y < it->current_y
6906 /* This is heuristic. In a window that's 3 lines high, with
6907 a line height of 13 pixels each, recentering with point
6908 on the bottom line will try to move -39/2 = 19 pixels
6909 backward. Try to avoid moving into the first line. */
6910 && (it->current_y - target_y
6911 > min (window_box_height (it->w), line_height * 2 / 3))
6912 && IT_CHARPOS (*it) > BEGV)
6913 {
6914 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6915 target_y - it->current_y));
6916 dy = it->current_y - target_y;
6917 goto move_further_back;
6918 }
6919 else if (target_y >= it->current_y + line_height
6920 && IT_CHARPOS (*it) < ZV)
6921 {
6922 /* Should move forward by at least one line, maybe more.
6923
6924 Note: Calling move_it_by_lines can be expensive on
6925 terminal frames, where compute_motion is used (via
6926 vmotion) to do the job, when there are very long lines
6927 and truncate-lines is nil. That's the reason for
6928 treating terminal frames specially here. */
6929
6930 if (!FRAME_WINDOW_P (it->f))
6931 move_it_vertically (it, target_y - (it->current_y + line_height));
6932 else
6933 {
6934 do
6935 {
6936 move_it_by_lines (it, 1, 1);
6937 }
6938 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6939 }
6940
6941 #if 0
6942 /* I think this assert is bogus if buffer contains
6943 invisible text or images. KFS. */
6944 xassert (IT_CHARPOS (*it) >= BEGV);
6945 #endif
6946 }
6947 }
6948 }
6949
6950
6951 /* Move IT by a specified amount of pixel lines DY. DY negative means
6952 move backwards. DY = 0 means move to start of screen line. At the
6953 end, IT will be on the start of a screen line. */
6954
6955 void
6956 move_it_vertically (it, dy)
6957 struct it *it;
6958 int dy;
6959 {
6960 if (dy <= 0)
6961 move_it_vertically_backward (it, -dy);
6962 else
6963 {
6964 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6965 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6966 MOVE_TO_POS | MOVE_TO_Y);
6967 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6968
6969 /* If buffer ends in ZV without a newline, move to the start of
6970 the line to satisfy the post-condition. */
6971 if (IT_CHARPOS (*it) == ZV
6972 && ZV > BEGV
6973 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6974 move_it_by_lines (it, 0, 0);
6975 }
6976 }
6977
6978
6979 /* Move iterator IT past the end of the text line it is in. */
6980
6981 void
6982 move_it_past_eol (it)
6983 struct it *it;
6984 {
6985 enum move_it_result rc;
6986
6987 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6988 if (rc == MOVE_NEWLINE_OR_CR)
6989 set_iterator_to_next (it, 0);
6990 }
6991
6992
6993 #if 0 /* Currently not used. */
6994
6995 /* Return non-zero if some text between buffer positions START_CHARPOS
6996 and END_CHARPOS is invisible. IT->window is the window for text
6997 property lookup. */
6998
6999 static int
7000 invisible_text_between_p (it, start_charpos, end_charpos)
7001 struct it *it;
7002 int start_charpos, end_charpos;
7003 {
7004 Lisp_Object prop, limit;
7005 int invisible_found_p;
7006
7007 xassert (it != NULL && start_charpos <= end_charpos);
7008
7009 /* Is text at START invisible? */
7010 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7011 it->window);
7012 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7013 invisible_found_p = 1;
7014 else
7015 {
7016 limit = Fnext_single_char_property_change (make_number (start_charpos),
7017 Qinvisible, Qnil,
7018 make_number (end_charpos));
7019 invisible_found_p = XFASTINT (limit) < end_charpos;
7020 }
7021
7022 return invisible_found_p;
7023 }
7024
7025 #endif /* 0 */
7026
7027
7028 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7029 negative means move up. DVPOS == 0 means move to the start of the
7030 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7031 NEED_Y_P is zero, IT->current_y will be left unchanged.
7032
7033 Further optimization ideas: If we would know that IT->f doesn't use
7034 a face with proportional font, we could be faster for
7035 truncate-lines nil. */
7036
7037 void
7038 move_it_by_lines (it, dvpos, need_y_p)
7039 struct it *it;
7040 int dvpos, need_y_p;
7041 {
7042 struct position pos;
7043
7044 if (!FRAME_WINDOW_P (it->f))
7045 {
7046 struct text_pos textpos;
7047
7048 /* We can use vmotion on frames without proportional fonts. */
7049 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7050 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7051 reseat (it, textpos, 1);
7052 it->vpos += pos.vpos;
7053 it->current_y += pos.vpos;
7054 }
7055 else if (dvpos == 0)
7056 {
7057 /* DVPOS == 0 means move to the start of the screen line. */
7058 move_it_vertically_backward (it, 0);
7059 xassert (it->current_x == 0 && it->hpos == 0);
7060 /* Let next call to line_bottom_y calculate real line height */
7061 last_height = 0;
7062 }
7063 else if (dvpos > 0)
7064 {
7065 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7066 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7067 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7068 }
7069 else
7070 {
7071 struct it it2;
7072 int start_charpos, i;
7073
7074 /* Start at the beginning of the screen line containing IT's
7075 position. This may actually move vertically backwards,
7076 in case of overlays, so adjust dvpos accordingly. */
7077 dvpos += it->vpos;
7078 move_it_vertically_backward (it, 0);
7079 dvpos -= it->vpos;
7080
7081 /* Go back -DVPOS visible lines and reseat the iterator there. */
7082 start_charpos = IT_CHARPOS (*it);
7083 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7084 back_to_previous_visible_line_start (it);
7085 reseat (it, it->current.pos, 1);
7086
7087 /* Move further back if we end up in a string or an image. */
7088 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7089 {
7090 /* First try to move to start of display line. */
7091 dvpos += it->vpos;
7092 move_it_vertically_backward (it, 0);
7093 dvpos -= it->vpos;
7094 if (IT_POS_VALID_AFTER_MOVE_P (it))
7095 break;
7096 /* If start of line is still in string or image,
7097 move further back. */
7098 back_to_previous_visible_line_start (it);
7099 reseat (it, it->current.pos, 1);
7100 dvpos--;
7101 }
7102
7103 it->current_x = it->hpos = 0;
7104
7105 /* Above call may have moved too far if continuation lines
7106 are involved. Scan forward and see if it did. */
7107 it2 = *it;
7108 it2.vpos = it2.current_y = 0;
7109 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7110 it->vpos -= it2.vpos;
7111 it->current_y -= it2.current_y;
7112 it->current_x = it->hpos = 0;
7113
7114 /* If we moved too far back, move IT some lines forward. */
7115 if (it2.vpos > -dvpos)
7116 {
7117 int delta = it2.vpos + dvpos;
7118 it2 = *it;
7119 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7120 /* Move back again if we got too far ahead. */
7121 if (IT_CHARPOS (*it) >= start_charpos)
7122 *it = it2;
7123 }
7124 }
7125 }
7126
7127 /* Return 1 if IT points into the middle of a display vector. */
7128
7129 int
7130 in_display_vector_p (it)
7131 struct it *it;
7132 {
7133 return (it->method == GET_FROM_DISPLAY_VECTOR
7134 && it->current.dpvec_index > 0
7135 && it->dpvec + it->current.dpvec_index != it->dpend);
7136 }
7137
7138 \f
7139 /***********************************************************************
7140 Messages
7141 ***********************************************************************/
7142
7143
7144 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7145 to *Messages*. */
7146
7147 void
7148 add_to_log (format, arg1, arg2)
7149 char *format;
7150 Lisp_Object arg1, arg2;
7151 {
7152 Lisp_Object args[3];
7153 Lisp_Object msg, fmt;
7154 char *buffer;
7155 int len;
7156 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7157 USE_SAFE_ALLOCA;
7158
7159 /* Do nothing if called asynchronously. Inserting text into
7160 a buffer may call after-change-functions and alike and
7161 that would means running Lisp asynchronously. */
7162 if (handling_signal)
7163 return;
7164
7165 fmt = msg = Qnil;
7166 GCPRO4 (fmt, msg, arg1, arg2);
7167
7168 args[0] = fmt = build_string (format);
7169 args[1] = arg1;
7170 args[2] = arg2;
7171 msg = Fformat (3, args);
7172
7173 len = SBYTES (msg) + 1;
7174 SAFE_ALLOCA (buffer, char *, len);
7175 bcopy (SDATA (msg), buffer, len);
7176
7177 message_dolog (buffer, len - 1, 1, 0);
7178 SAFE_FREE ();
7179
7180 UNGCPRO;
7181 }
7182
7183
7184 /* Output a newline in the *Messages* buffer if "needs" one. */
7185
7186 void
7187 message_log_maybe_newline ()
7188 {
7189 if (message_log_need_newline)
7190 message_dolog ("", 0, 1, 0);
7191 }
7192
7193
7194 /* Add a string M of length NBYTES to the message log, optionally
7195 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7196 nonzero, means interpret the contents of M as multibyte. This
7197 function calls low-level routines in order to bypass text property
7198 hooks, etc. which might not be safe to run.
7199
7200 This may GC (insert may run before/after change hooks),
7201 so the buffer M must NOT point to a Lisp string. */
7202
7203 void
7204 message_dolog (m, nbytes, nlflag, multibyte)
7205 const char *m;
7206 int nbytes, nlflag, multibyte;
7207 {
7208 if (!NILP (Vmemory_full))
7209 return;
7210
7211 if (!NILP (Vmessage_log_max))
7212 {
7213 struct buffer *oldbuf;
7214 Lisp_Object oldpoint, oldbegv, oldzv;
7215 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7216 int point_at_end = 0;
7217 int zv_at_end = 0;
7218 Lisp_Object old_deactivate_mark, tem;
7219 struct gcpro gcpro1;
7220
7221 old_deactivate_mark = Vdeactivate_mark;
7222 oldbuf = current_buffer;
7223 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7224 current_buffer->undo_list = Qt;
7225
7226 oldpoint = message_dolog_marker1;
7227 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7228 oldbegv = message_dolog_marker2;
7229 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7230 oldzv = message_dolog_marker3;
7231 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7232 GCPRO1 (old_deactivate_mark);
7233
7234 if (PT == Z)
7235 point_at_end = 1;
7236 if (ZV == Z)
7237 zv_at_end = 1;
7238
7239 BEGV = BEG;
7240 BEGV_BYTE = BEG_BYTE;
7241 ZV = Z;
7242 ZV_BYTE = Z_BYTE;
7243 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7244
7245 /* Insert the string--maybe converting multibyte to single byte
7246 or vice versa, so that all the text fits the buffer. */
7247 if (multibyte
7248 && NILP (current_buffer->enable_multibyte_characters))
7249 {
7250 int i, c, char_bytes;
7251 unsigned char work[1];
7252
7253 /* Convert a multibyte string to single-byte
7254 for the *Message* buffer. */
7255 for (i = 0; i < nbytes; i += char_bytes)
7256 {
7257 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7258 work[0] = (SINGLE_BYTE_CHAR_P (c)
7259 ? c
7260 : multibyte_char_to_unibyte (c, Qnil));
7261 insert_1_both (work, 1, 1, 1, 0, 0);
7262 }
7263 }
7264 else if (! multibyte
7265 && ! NILP (current_buffer->enable_multibyte_characters))
7266 {
7267 int i, c, char_bytes;
7268 unsigned char *msg = (unsigned char *) m;
7269 unsigned char str[MAX_MULTIBYTE_LENGTH];
7270 /* Convert a single-byte string to multibyte
7271 for the *Message* buffer. */
7272 for (i = 0; i < nbytes; i++)
7273 {
7274 c = unibyte_char_to_multibyte (msg[i]);
7275 char_bytes = CHAR_STRING (c, str);
7276 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7277 }
7278 }
7279 else if (nbytes)
7280 insert_1 (m, nbytes, 1, 0, 0);
7281
7282 if (nlflag)
7283 {
7284 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7285 insert_1 ("\n", 1, 1, 0, 0);
7286
7287 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7288 this_bol = PT;
7289 this_bol_byte = PT_BYTE;
7290
7291 /* See if this line duplicates the previous one.
7292 If so, combine duplicates. */
7293 if (this_bol > BEG)
7294 {
7295 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7296 prev_bol = PT;
7297 prev_bol_byte = PT_BYTE;
7298
7299 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7300 this_bol, this_bol_byte);
7301 if (dup)
7302 {
7303 del_range_both (prev_bol, prev_bol_byte,
7304 this_bol, this_bol_byte, 0);
7305 if (dup > 1)
7306 {
7307 char dupstr[40];
7308 int duplen;
7309
7310 /* If you change this format, don't forget to also
7311 change message_log_check_duplicate. */
7312 sprintf (dupstr, " [%d times]", dup);
7313 duplen = strlen (dupstr);
7314 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7315 insert_1 (dupstr, duplen, 1, 0, 1);
7316 }
7317 }
7318 }
7319
7320 /* If we have more than the desired maximum number of lines
7321 in the *Messages* buffer now, delete the oldest ones.
7322 This is safe because we don't have undo in this buffer. */
7323
7324 if (NATNUMP (Vmessage_log_max))
7325 {
7326 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7327 -XFASTINT (Vmessage_log_max) - 1, 0);
7328 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7329 }
7330 }
7331 BEGV = XMARKER (oldbegv)->charpos;
7332 BEGV_BYTE = marker_byte_position (oldbegv);
7333
7334 if (zv_at_end)
7335 {
7336 ZV = Z;
7337 ZV_BYTE = Z_BYTE;
7338 }
7339 else
7340 {
7341 ZV = XMARKER (oldzv)->charpos;
7342 ZV_BYTE = marker_byte_position (oldzv);
7343 }
7344
7345 if (point_at_end)
7346 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7347 else
7348 /* We can't do Fgoto_char (oldpoint) because it will run some
7349 Lisp code. */
7350 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7351 XMARKER (oldpoint)->bytepos);
7352
7353 UNGCPRO;
7354 unchain_marker (XMARKER (oldpoint));
7355 unchain_marker (XMARKER (oldbegv));
7356 unchain_marker (XMARKER (oldzv));
7357
7358 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7359 set_buffer_internal (oldbuf);
7360 if (NILP (tem))
7361 windows_or_buffers_changed = old_windows_or_buffers_changed;
7362 message_log_need_newline = !nlflag;
7363 Vdeactivate_mark = old_deactivate_mark;
7364 }
7365 }
7366
7367
7368 /* We are at the end of the buffer after just having inserted a newline.
7369 (Note: We depend on the fact we won't be crossing the gap.)
7370 Check to see if the most recent message looks a lot like the previous one.
7371 Return 0 if different, 1 if the new one should just replace it, or a
7372 value N > 1 if we should also append " [N times]". */
7373
7374 static int
7375 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7376 int prev_bol, this_bol;
7377 int prev_bol_byte, this_bol_byte;
7378 {
7379 int i;
7380 int len = Z_BYTE - 1 - this_bol_byte;
7381 int seen_dots = 0;
7382 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7383 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7384
7385 for (i = 0; i < len; i++)
7386 {
7387 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7388 seen_dots = 1;
7389 if (p1[i] != p2[i])
7390 return seen_dots;
7391 }
7392 p1 += len;
7393 if (*p1 == '\n')
7394 return 2;
7395 if (*p1++ == ' ' && *p1++ == '[')
7396 {
7397 int n = 0;
7398 while (*p1 >= '0' && *p1 <= '9')
7399 n = n * 10 + *p1++ - '0';
7400 if (strncmp (p1, " times]\n", 8) == 0)
7401 return n+1;
7402 }
7403 return 0;
7404 }
7405 \f
7406
7407 /* Display an echo area message M with a specified length of NBYTES
7408 bytes. The string may include null characters. If M is 0, clear
7409 out any existing message, and let the mini-buffer text show
7410 through.
7411
7412 This may GC, so the buffer M must NOT point to a Lisp string. */
7413
7414 void
7415 message2 (m, nbytes, multibyte)
7416 const char *m;
7417 int nbytes;
7418 int multibyte;
7419 {
7420 /* First flush out any partial line written with print. */
7421 message_log_maybe_newline ();
7422 if (m)
7423 message_dolog (m, nbytes, 1, multibyte);
7424 message2_nolog (m, nbytes, multibyte);
7425 }
7426
7427
7428 /* The non-logging counterpart of message2. */
7429
7430 void
7431 message2_nolog (m, nbytes, multibyte)
7432 const char *m;
7433 int nbytes, multibyte;
7434 {
7435 struct frame *sf = SELECTED_FRAME ();
7436 message_enable_multibyte = multibyte;
7437
7438 if (noninteractive)
7439 {
7440 if (noninteractive_need_newline)
7441 putc ('\n', stderr);
7442 noninteractive_need_newline = 0;
7443 if (m)
7444 fwrite (m, nbytes, 1, stderr);
7445 if (cursor_in_echo_area == 0)
7446 fprintf (stderr, "\n");
7447 fflush (stderr);
7448 }
7449 /* A null message buffer means that the frame hasn't really been
7450 initialized yet. Error messages get reported properly by
7451 cmd_error, so this must be just an informative message; toss it. */
7452 else if (INTERACTIVE
7453 && sf->glyphs_initialized_p
7454 && FRAME_MESSAGE_BUF (sf))
7455 {
7456 Lisp_Object mini_window;
7457 struct frame *f;
7458
7459 /* Get the frame containing the mini-buffer
7460 that the selected frame is using. */
7461 mini_window = FRAME_MINIBUF_WINDOW (sf);
7462 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7463
7464 FRAME_SAMPLE_VISIBILITY (f);
7465 if (FRAME_VISIBLE_P (sf)
7466 && ! FRAME_VISIBLE_P (f))
7467 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7468
7469 if (m)
7470 {
7471 set_message (m, Qnil, nbytes, multibyte);
7472 if (minibuffer_auto_raise)
7473 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7474 }
7475 else
7476 clear_message (1, 1);
7477
7478 do_pending_window_change (0);
7479 echo_area_display (1);
7480 do_pending_window_change (0);
7481 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7482 (*frame_up_to_date_hook) (f);
7483 }
7484 }
7485
7486
7487 /* Display an echo area message M with a specified length of NBYTES
7488 bytes. The string may include null characters. If M is not a
7489 string, clear out any existing message, and let the mini-buffer
7490 text show through.
7491
7492 This function cancels echoing. */
7493
7494 void
7495 message3 (m, nbytes, multibyte)
7496 Lisp_Object m;
7497 int nbytes;
7498 int multibyte;
7499 {
7500 struct gcpro gcpro1;
7501
7502 GCPRO1 (m);
7503 clear_message (1,1);
7504 cancel_echoing ();
7505
7506 /* First flush out any partial line written with print. */
7507 message_log_maybe_newline ();
7508 if (STRINGP (m))
7509 {
7510 char *buffer;
7511 USE_SAFE_ALLOCA;
7512
7513 SAFE_ALLOCA (buffer, char *, nbytes);
7514 bcopy (SDATA (m), buffer, nbytes);
7515 message_dolog (buffer, nbytes, 1, multibyte);
7516 SAFE_FREE ();
7517 }
7518 message3_nolog (m, nbytes, multibyte);
7519
7520 UNGCPRO;
7521 }
7522
7523
7524 /* The non-logging version of message3.
7525 This does not cancel echoing, because it is used for echoing.
7526 Perhaps we need to make a separate function for echoing
7527 and make this cancel echoing. */
7528
7529 void
7530 message3_nolog (m, nbytes, multibyte)
7531 Lisp_Object m;
7532 int nbytes, multibyte;
7533 {
7534 struct frame *sf = SELECTED_FRAME ();
7535 message_enable_multibyte = multibyte;
7536
7537 if (noninteractive)
7538 {
7539 if (noninteractive_need_newline)
7540 putc ('\n', stderr);
7541 noninteractive_need_newline = 0;
7542 if (STRINGP (m))
7543 fwrite (SDATA (m), nbytes, 1, stderr);
7544 if (cursor_in_echo_area == 0)
7545 fprintf (stderr, "\n");
7546 fflush (stderr);
7547 }
7548 /* A null message buffer means that the frame hasn't really been
7549 initialized yet. Error messages get reported properly by
7550 cmd_error, so this must be just an informative message; toss it. */
7551 else if (INTERACTIVE
7552 && sf->glyphs_initialized_p
7553 && FRAME_MESSAGE_BUF (sf))
7554 {
7555 Lisp_Object mini_window;
7556 Lisp_Object frame;
7557 struct frame *f;
7558
7559 /* Get the frame containing the mini-buffer
7560 that the selected frame is using. */
7561 mini_window = FRAME_MINIBUF_WINDOW (sf);
7562 frame = XWINDOW (mini_window)->frame;
7563 f = XFRAME (frame);
7564
7565 FRAME_SAMPLE_VISIBILITY (f);
7566 if (FRAME_VISIBLE_P (sf)
7567 && !FRAME_VISIBLE_P (f))
7568 Fmake_frame_visible (frame);
7569
7570 if (STRINGP (m) && SCHARS (m) > 0)
7571 {
7572 set_message (NULL, m, nbytes, multibyte);
7573 if (minibuffer_auto_raise)
7574 Fraise_frame (frame);
7575 /* Assume we are not echoing.
7576 (If we are, echo_now will override this.) */
7577 echo_message_buffer = Qnil;
7578 }
7579 else
7580 clear_message (1, 1);
7581
7582 do_pending_window_change (0);
7583 echo_area_display (1);
7584 do_pending_window_change (0);
7585 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7586 (*frame_up_to_date_hook) (f);
7587 }
7588 }
7589
7590
7591 /* Display a null-terminated echo area message M. If M is 0, clear
7592 out any existing message, and let the mini-buffer text show through.
7593
7594 The buffer M must continue to exist until after the echo area gets
7595 cleared or some other message gets displayed there. Do not pass
7596 text that is stored in a Lisp string. Do not pass text in a buffer
7597 that was alloca'd. */
7598
7599 void
7600 message1 (m)
7601 char *m;
7602 {
7603 message2 (m, (m ? strlen (m) : 0), 0);
7604 }
7605
7606
7607 /* The non-logging counterpart of message1. */
7608
7609 void
7610 message1_nolog (m)
7611 char *m;
7612 {
7613 message2_nolog (m, (m ? strlen (m) : 0), 0);
7614 }
7615
7616 /* Display a message M which contains a single %s
7617 which gets replaced with STRING. */
7618
7619 void
7620 message_with_string (m, string, log)
7621 char *m;
7622 Lisp_Object string;
7623 int log;
7624 {
7625 CHECK_STRING (string);
7626
7627 if (noninteractive)
7628 {
7629 if (m)
7630 {
7631 if (noninteractive_need_newline)
7632 putc ('\n', stderr);
7633 noninteractive_need_newline = 0;
7634 fprintf (stderr, m, SDATA (string));
7635 if (cursor_in_echo_area == 0)
7636 fprintf (stderr, "\n");
7637 fflush (stderr);
7638 }
7639 }
7640 else if (INTERACTIVE)
7641 {
7642 /* The frame whose minibuffer we're going to display the message on.
7643 It may be larger than the selected frame, so we need
7644 to use its buffer, not the selected frame's buffer. */
7645 Lisp_Object mini_window;
7646 struct frame *f, *sf = SELECTED_FRAME ();
7647
7648 /* Get the frame containing the minibuffer
7649 that the selected frame is using. */
7650 mini_window = FRAME_MINIBUF_WINDOW (sf);
7651 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7652
7653 /* A null message buffer means that the frame hasn't really been
7654 initialized yet. Error messages get reported properly by
7655 cmd_error, so this must be just an informative message; toss it. */
7656 if (FRAME_MESSAGE_BUF (f))
7657 {
7658 Lisp_Object args[2], message;
7659 struct gcpro gcpro1, gcpro2;
7660
7661 args[0] = build_string (m);
7662 args[1] = message = string;
7663 GCPRO2 (args[0], message);
7664 gcpro1.nvars = 2;
7665
7666 message = Fformat (2, args);
7667
7668 if (log)
7669 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7670 else
7671 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7672
7673 UNGCPRO;
7674
7675 /* Print should start at the beginning of the message
7676 buffer next time. */
7677 message_buf_print = 0;
7678 }
7679 }
7680 }
7681
7682
7683 /* Dump an informative message to the minibuf. If M is 0, clear out
7684 any existing message, and let the mini-buffer text show through. */
7685
7686 /* VARARGS 1 */
7687 void
7688 message (m, a1, a2, a3)
7689 char *m;
7690 EMACS_INT a1, a2, a3;
7691 {
7692 if (noninteractive)
7693 {
7694 if (m)
7695 {
7696 if (noninteractive_need_newline)
7697 putc ('\n', stderr);
7698 noninteractive_need_newline = 0;
7699 fprintf (stderr, m, a1, a2, a3);
7700 if (cursor_in_echo_area == 0)
7701 fprintf (stderr, "\n");
7702 fflush (stderr);
7703 }
7704 }
7705 else if (INTERACTIVE)
7706 {
7707 /* The frame whose mini-buffer we're going to display the message
7708 on. It may be larger than the selected frame, so we need to
7709 use its buffer, not the selected frame's buffer. */
7710 Lisp_Object mini_window;
7711 struct frame *f, *sf = SELECTED_FRAME ();
7712
7713 /* Get the frame containing the mini-buffer
7714 that the selected frame is using. */
7715 mini_window = FRAME_MINIBUF_WINDOW (sf);
7716 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7717
7718 /* A null message buffer means that the frame hasn't really been
7719 initialized yet. Error messages get reported properly by
7720 cmd_error, so this must be just an informative message; toss
7721 it. */
7722 if (FRAME_MESSAGE_BUF (f))
7723 {
7724 if (m)
7725 {
7726 int len;
7727 #ifdef NO_ARG_ARRAY
7728 char *a[3];
7729 a[0] = (char *) a1;
7730 a[1] = (char *) a2;
7731 a[2] = (char *) a3;
7732
7733 len = doprnt (FRAME_MESSAGE_BUF (f),
7734 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7735 #else
7736 len = doprnt (FRAME_MESSAGE_BUF (f),
7737 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7738 (char **) &a1);
7739 #endif /* NO_ARG_ARRAY */
7740
7741 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7742 }
7743 else
7744 message1 (0);
7745
7746 /* Print should start at the beginning of the message
7747 buffer next time. */
7748 message_buf_print = 0;
7749 }
7750 }
7751 }
7752
7753
7754 /* The non-logging version of message. */
7755
7756 void
7757 message_nolog (m, a1, a2, a3)
7758 char *m;
7759 EMACS_INT a1, a2, a3;
7760 {
7761 Lisp_Object old_log_max;
7762 old_log_max = Vmessage_log_max;
7763 Vmessage_log_max = Qnil;
7764 message (m, a1, a2, a3);
7765 Vmessage_log_max = old_log_max;
7766 }
7767
7768
7769 /* Display the current message in the current mini-buffer. This is
7770 only called from error handlers in process.c, and is not time
7771 critical. */
7772
7773 void
7774 update_echo_area ()
7775 {
7776 if (!NILP (echo_area_buffer[0]))
7777 {
7778 Lisp_Object string;
7779 string = Fcurrent_message ();
7780 message3 (string, SBYTES (string),
7781 !NILP (current_buffer->enable_multibyte_characters));
7782 }
7783 }
7784
7785
7786 /* Make sure echo area buffers in `echo_buffers' are live.
7787 If they aren't, make new ones. */
7788
7789 static void
7790 ensure_echo_area_buffers ()
7791 {
7792 int i;
7793
7794 for (i = 0; i < 2; ++i)
7795 if (!BUFFERP (echo_buffer[i])
7796 || NILP (XBUFFER (echo_buffer[i])->name))
7797 {
7798 char name[30];
7799 Lisp_Object old_buffer;
7800 int j;
7801
7802 old_buffer = echo_buffer[i];
7803 sprintf (name, " *Echo Area %d*", i);
7804 echo_buffer[i] = Fget_buffer_create (build_string (name));
7805 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7806
7807 for (j = 0; j < 2; ++j)
7808 if (EQ (old_buffer, echo_area_buffer[j]))
7809 echo_area_buffer[j] = echo_buffer[i];
7810 }
7811 }
7812
7813
7814 /* Call FN with args A1..A4 with either the current or last displayed
7815 echo_area_buffer as current buffer.
7816
7817 WHICH zero means use the current message buffer
7818 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7819 from echo_buffer[] and clear it.
7820
7821 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7822 suitable buffer from echo_buffer[] and clear it.
7823
7824 Value is what FN returns. */
7825
7826 static int
7827 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7828 struct window *w;
7829 int which;
7830 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7831 EMACS_INT a1;
7832 Lisp_Object a2;
7833 EMACS_INT a3, a4;
7834 {
7835 Lisp_Object buffer;
7836 int this_one, the_other, clear_buffer_p, rc;
7837 int count = SPECPDL_INDEX ();
7838
7839 /* If buffers aren't live, make new ones. */
7840 ensure_echo_area_buffers ();
7841
7842 clear_buffer_p = 0;
7843
7844 if (which == 0)
7845 this_one = 0, the_other = 1;
7846 else if (which > 0)
7847 this_one = 1, the_other = 0;
7848
7849 /* Choose a suitable buffer from echo_buffer[] is we don't
7850 have one. */
7851 if (NILP (echo_area_buffer[this_one]))
7852 {
7853 echo_area_buffer[this_one]
7854 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7855 ? echo_buffer[the_other]
7856 : echo_buffer[this_one]);
7857 clear_buffer_p = 1;
7858 }
7859
7860 buffer = echo_area_buffer[this_one];
7861
7862 /* Don't get confused by reusing the buffer used for echoing
7863 for a different purpose. */
7864 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7865 cancel_echoing ();
7866
7867 record_unwind_protect (unwind_with_echo_area_buffer,
7868 with_echo_area_buffer_unwind_data (w));
7869
7870 /* Make the echo area buffer current. Note that for display
7871 purposes, it is not necessary that the displayed window's buffer
7872 == current_buffer, except for text property lookup. So, let's
7873 only set that buffer temporarily here without doing a full
7874 Fset_window_buffer. We must also change w->pointm, though,
7875 because otherwise an assertions in unshow_buffer fails, and Emacs
7876 aborts. */
7877 set_buffer_internal_1 (XBUFFER (buffer));
7878 if (w)
7879 {
7880 w->buffer = buffer;
7881 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7882 }
7883
7884 current_buffer->undo_list = Qt;
7885 current_buffer->read_only = Qnil;
7886 specbind (Qinhibit_read_only, Qt);
7887 specbind (Qinhibit_modification_hooks, Qt);
7888
7889 if (clear_buffer_p && Z > BEG)
7890 del_range (BEG, Z);
7891
7892 xassert (BEGV >= BEG);
7893 xassert (ZV <= Z && ZV >= BEGV);
7894
7895 rc = fn (a1, a2, a3, a4);
7896
7897 xassert (BEGV >= BEG);
7898 xassert (ZV <= Z && ZV >= BEGV);
7899
7900 unbind_to (count, Qnil);
7901 return rc;
7902 }
7903
7904
7905 /* Save state that should be preserved around the call to the function
7906 FN called in with_echo_area_buffer. */
7907
7908 static Lisp_Object
7909 with_echo_area_buffer_unwind_data (w)
7910 struct window *w;
7911 {
7912 int i = 0;
7913 Lisp_Object vector;
7914
7915 /* Reduce consing by keeping one vector in
7916 Vwith_echo_area_save_vector. */
7917 vector = Vwith_echo_area_save_vector;
7918 Vwith_echo_area_save_vector = Qnil;
7919
7920 if (NILP (vector))
7921 vector = Fmake_vector (make_number (7), Qnil);
7922
7923 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7924 AREF (vector, i) = Vdeactivate_mark, ++i;
7925 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7926
7927 if (w)
7928 {
7929 XSETWINDOW (AREF (vector, i), w); ++i;
7930 AREF (vector, i) = w->buffer; ++i;
7931 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7932 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7933 }
7934 else
7935 {
7936 int end = i + 4;
7937 for (; i < end; ++i)
7938 AREF (vector, i) = Qnil;
7939 }
7940
7941 xassert (i == ASIZE (vector));
7942 return vector;
7943 }
7944
7945
7946 /* Restore global state from VECTOR which was created by
7947 with_echo_area_buffer_unwind_data. */
7948
7949 static Lisp_Object
7950 unwind_with_echo_area_buffer (vector)
7951 Lisp_Object vector;
7952 {
7953 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7954 Vdeactivate_mark = AREF (vector, 1);
7955 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7956
7957 if (WINDOWP (AREF (vector, 3)))
7958 {
7959 struct window *w;
7960 Lisp_Object buffer, charpos, bytepos;
7961
7962 w = XWINDOW (AREF (vector, 3));
7963 buffer = AREF (vector, 4);
7964 charpos = AREF (vector, 5);
7965 bytepos = AREF (vector, 6);
7966
7967 w->buffer = buffer;
7968 set_marker_both (w->pointm, buffer,
7969 XFASTINT (charpos), XFASTINT (bytepos));
7970 }
7971
7972 Vwith_echo_area_save_vector = vector;
7973 return Qnil;
7974 }
7975
7976
7977 /* Set up the echo area for use by print functions. MULTIBYTE_P
7978 non-zero means we will print multibyte. */
7979
7980 void
7981 setup_echo_area_for_printing (multibyte_p)
7982 int multibyte_p;
7983 {
7984 /* If we can't find an echo area any more, exit. */
7985 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7986 Fkill_emacs (Qnil);
7987
7988 ensure_echo_area_buffers ();
7989
7990 if (!message_buf_print)
7991 {
7992 /* A message has been output since the last time we printed.
7993 Choose a fresh echo area buffer. */
7994 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7995 echo_area_buffer[0] = echo_buffer[1];
7996 else
7997 echo_area_buffer[0] = echo_buffer[0];
7998
7999 /* Switch to that buffer and clear it. */
8000 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8001 current_buffer->truncate_lines = Qnil;
8002
8003 if (Z > BEG)
8004 {
8005 int count = SPECPDL_INDEX ();
8006 specbind (Qinhibit_read_only, Qt);
8007 /* Note that undo recording is always disabled. */
8008 del_range (BEG, Z);
8009 unbind_to (count, Qnil);
8010 }
8011 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8012
8013 /* Set up the buffer for the multibyteness we need. */
8014 if (multibyte_p
8015 != !NILP (current_buffer->enable_multibyte_characters))
8016 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8017
8018 /* Raise the frame containing the echo area. */
8019 if (minibuffer_auto_raise)
8020 {
8021 struct frame *sf = SELECTED_FRAME ();
8022 Lisp_Object mini_window;
8023 mini_window = FRAME_MINIBUF_WINDOW (sf);
8024 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8025 }
8026
8027 message_log_maybe_newline ();
8028 message_buf_print = 1;
8029 }
8030 else
8031 {
8032 if (NILP (echo_area_buffer[0]))
8033 {
8034 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8035 echo_area_buffer[0] = echo_buffer[1];
8036 else
8037 echo_area_buffer[0] = echo_buffer[0];
8038 }
8039
8040 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8041 {
8042 /* Someone switched buffers between print requests. */
8043 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8044 current_buffer->truncate_lines = Qnil;
8045 }
8046 }
8047 }
8048
8049
8050 /* Display an echo area message in window W. Value is non-zero if W's
8051 height is changed. If display_last_displayed_message_p is
8052 non-zero, display the message that was last displayed, otherwise
8053 display the current message. */
8054
8055 static int
8056 display_echo_area (w)
8057 struct window *w;
8058 {
8059 int i, no_message_p, window_height_changed_p, count;
8060
8061 /* Temporarily disable garbage collections while displaying the echo
8062 area. This is done because a GC can print a message itself.
8063 That message would modify the echo area buffer's contents while a
8064 redisplay of the buffer is going on, and seriously confuse
8065 redisplay. */
8066 count = inhibit_garbage_collection ();
8067
8068 /* If there is no message, we must call display_echo_area_1
8069 nevertheless because it resizes the window. But we will have to
8070 reset the echo_area_buffer in question to nil at the end because
8071 with_echo_area_buffer will sets it to an empty buffer. */
8072 i = display_last_displayed_message_p ? 1 : 0;
8073 no_message_p = NILP (echo_area_buffer[i]);
8074
8075 window_height_changed_p
8076 = with_echo_area_buffer (w, display_last_displayed_message_p,
8077 display_echo_area_1,
8078 (EMACS_INT) w, Qnil, 0, 0);
8079
8080 if (no_message_p)
8081 echo_area_buffer[i] = Qnil;
8082
8083 unbind_to (count, Qnil);
8084 return window_height_changed_p;
8085 }
8086
8087
8088 /* Helper for display_echo_area. Display the current buffer which
8089 contains the current echo area message in window W, a mini-window,
8090 a pointer to which is passed in A1. A2..A4 are currently not used.
8091 Change the height of W so that all of the message is displayed.
8092 Value is non-zero if height of W was changed. */
8093
8094 static int
8095 display_echo_area_1 (a1, a2, a3, a4)
8096 EMACS_INT a1;
8097 Lisp_Object a2;
8098 EMACS_INT a3, a4;
8099 {
8100 struct window *w = (struct window *) a1;
8101 Lisp_Object window;
8102 struct text_pos start;
8103 int window_height_changed_p = 0;
8104
8105 /* Do this before displaying, so that we have a large enough glyph
8106 matrix for the display. If we can't get enough space for the
8107 whole text, display the last N lines. That works by setting w->start. */
8108 window_height_changed_p = resize_mini_window (w, 0);
8109
8110 /* Use the starting position chosen by resize_mini_window. */
8111 SET_TEXT_POS_FROM_MARKER (start, w->start);
8112
8113 /* Display. */
8114 clear_glyph_matrix (w->desired_matrix);
8115 XSETWINDOW (window, w);
8116 try_window (window, start, 0);
8117
8118 return window_height_changed_p;
8119 }
8120
8121
8122 /* Resize the echo area window to exactly the size needed for the
8123 currently displayed message, if there is one. If a mini-buffer
8124 is active, don't shrink it. */
8125
8126 void
8127 resize_echo_area_exactly ()
8128 {
8129 if (BUFFERP (echo_area_buffer[0])
8130 && WINDOWP (echo_area_window))
8131 {
8132 struct window *w = XWINDOW (echo_area_window);
8133 int resized_p;
8134 Lisp_Object resize_exactly;
8135
8136 if (minibuf_level == 0)
8137 resize_exactly = Qt;
8138 else
8139 resize_exactly = Qnil;
8140
8141 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8142 (EMACS_INT) w, resize_exactly, 0, 0);
8143 if (resized_p)
8144 {
8145 ++windows_or_buffers_changed;
8146 ++update_mode_lines;
8147 redisplay_internal (0);
8148 }
8149 }
8150 }
8151
8152
8153 /* Callback function for with_echo_area_buffer, when used from
8154 resize_echo_area_exactly. A1 contains a pointer to the window to
8155 resize, EXACTLY non-nil means resize the mini-window exactly to the
8156 size of the text displayed. A3 and A4 are not used. Value is what
8157 resize_mini_window returns. */
8158
8159 static int
8160 resize_mini_window_1 (a1, exactly, a3, a4)
8161 EMACS_INT a1;
8162 Lisp_Object exactly;
8163 EMACS_INT a3, a4;
8164 {
8165 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8166 }
8167
8168
8169 /* Resize mini-window W to fit the size of its contents. EXACT:P
8170 means size the window exactly to the size needed. Otherwise, it's
8171 only enlarged until W's buffer is empty.
8172
8173 Set W->start to the right place to begin display. If the whole
8174 contents fit, start at the beginning. Otherwise, start so as
8175 to make the end of the contents appear. This is particularly
8176 important for y-or-n-p, but seems desirable generally.
8177
8178 Value is non-zero if the window height has been changed. */
8179
8180 int
8181 resize_mini_window (w, exact_p)
8182 struct window *w;
8183 int exact_p;
8184 {
8185 struct frame *f = XFRAME (w->frame);
8186 int window_height_changed_p = 0;
8187
8188 xassert (MINI_WINDOW_P (w));
8189
8190 /* By default, start display at the beginning. */
8191 set_marker_both (w->start, w->buffer,
8192 BUF_BEGV (XBUFFER (w->buffer)),
8193 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8194
8195 /* Don't resize windows while redisplaying a window; it would
8196 confuse redisplay functions when the size of the window they are
8197 displaying changes from under them. Such a resizing can happen,
8198 for instance, when which-func prints a long message while
8199 we are running fontification-functions. We're running these
8200 functions with safe_call which binds inhibit-redisplay to t. */
8201 if (!NILP (Vinhibit_redisplay))
8202 return 0;
8203
8204 /* Nil means don't try to resize. */
8205 if (NILP (Vresize_mini_windows)
8206 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8207 return 0;
8208
8209 if (!FRAME_MINIBUF_ONLY_P (f))
8210 {
8211 struct it it;
8212 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8213 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8214 int height, max_height;
8215 int unit = FRAME_LINE_HEIGHT (f);
8216 struct text_pos start;
8217 struct buffer *old_current_buffer = NULL;
8218
8219 if (current_buffer != XBUFFER (w->buffer))
8220 {
8221 old_current_buffer = current_buffer;
8222 set_buffer_internal (XBUFFER (w->buffer));
8223 }
8224
8225 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8226
8227 /* Compute the max. number of lines specified by the user. */
8228 if (FLOATP (Vmax_mini_window_height))
8229 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8230 else if (INTEGERP (Vmax_mini_window_height))
8231 max_height = XINT (Vmax_mini_window_height);
8232 else
8233 max_height = total_height / 4;
8234
8235 /* Correct that max. height if it's bogus. */
8236 max_height = max (1, max_height);
8237 max_height = min (total_height, max_height);
8238
8239 /* Find out the height of the text in the window. */
8240 if (it.truncate_lines_p)
8241 height = 1;
8242 else
8243 {
8244 last_height = 0;
8245 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8246 if (it.max_ascent == 0 && it.max_descent == 0)
8247 height = it.current_y + last_height;
8248 else
8249 height = it.current_y + it.max_ascent + it.max_descent;
8250 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8251 height = (height + unit - 1) / unit;
8252 }
8253
8254 /* Compute a suitable window start. */
8255 if (height > max_height)
8256 {
8257 height = max_height;
8258 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8259 move_it_vertically_backward (&it, (height - 1) * unit);
8260 start = it.current.pos;
8261 }
8262 else
8263 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8264 SET_MARKER_FROM_TEXT_POS (w->start, start);
8265
8266 if (EQ (Vresize_mini_windows, Qgrow_only))
8267 {
8268 /* Let it grow only, until we display an empty message, in which
8269 case the window shrinks again. */
8270 if (height > WINDOW_TOTAL_LINES (w))
8271 {
8272 int old_height = WINDOW_TOTAL_LINES (w);
8273 freeze_window_starts (f, 1);
8274 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8275 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8276 }
8277 else if (height < WINDOW_TOTAL_LINES (w)
8278 && (exact_p || BEGV == ZV))
8279 {
8280 int old_height = WINDOW_TOTAL_LINES (w);
8281 freeze_window_starts (f, 0);
8282 shrink_mini_window (w);
8283 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8284 }
8285 }
8286 else
8287 {
8288 /* Always resize to exact size needed. */
8289 if (height > WINDOW_TOTAL_LINES (w))
8290 {
8291 int old_height = WINDOW_TOTAL_LINES (w);
8292 freeze_window_starts (f, 1);
8293 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8294 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8295 }
8296 else if (height < WINDOW_TOTAL_LINES (w))
8297 {
8298 int old_height = WINDOW_TOTAL_LINES (w);
8299 freeze_window_starts (f, 0);
8300 shrink_mini_window (w);
8301
8302 if (height)
8303 {
8304 freeze_window_starts (f, 1);
8305 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8306 }
8307
8308 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8309 }
8310 }
8311
8312 if (old_current_buffer)
8313 set_buffer_internal (old_current_buffer);
8314 }
8315
8316 return window_height_changed_p;
8317 }
8318
8319
8320 /* Value is the current message, a string, or nil if there is no
8321 current message. */
8322
8323 Lisp_Object
8324 current_message ()
8325 {
8326 Lisp_Object msg;
8327
8328 if (NILP (echo_area_buffer[0]))
8329 msg = Qnil;
8330 else
8331 {
8332 with_echo_area_buffer (0, 0, current_message_1,
8333 (EMACS_INT) &msg, Qnil, 0, 0);
8334 if (NILP (msg))
8335 echo_area_buffer[0] = Qnil;
8336 }
8337
8338 return msg;
8339 }
8340
8341
8342 static int
8343 current_message_1 (a1, a2, a3, a4)
8344 EMACS_INT a1;
8345 Lisp_Object a2;
8346 EMACS_INT a3, a4;
8347 {
8348 Lisp_Object *msg = (Lisp_Object *) a1;
8349
8350 if (Z > BEG)
8351 *msg = make_buffer_string (BEG, Z, 1);
8352 else
8353 *msg = Qnil;
8354 return 0;
8355 }
8356
8357
8358 /* Push the current message on Vmessage_stack for later restauration
8359 by restore_message. Value is non-zero if the current message isn't
8360 empty. This is a relatively infrequent operation, so it's not
8361 worth optimizing. */
8362
8363 int
8364 push_message ()
8365 {
8366 Lisp_Object msg;
8367 msg = current_message ();
8368 Vmessage_stack = Fcons (msg, Vmessage_stack);
8369 return STRINGP (msg);
8370 }
8371
8372
8373 /* Restore message display from the top of Vmessage_stack. */
8374
8375 void
8376 restore_message ()
8377 {
8378 Lisp_Object msg;
8379
8380 xassert (CONSP (Vmessage_stack));
8381 msg = XCAR (Vmessage_stack);
8382 if (STRINGP (msg))
8383 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8384 else
8385 message3_nolog (msg, 0, 0);
8386 }
8387
8388
8389 /* Handler for record_unwind_protect calling pop_message. */
8390
8391 Lisp_Object
8392 pop_message_unwind (dummy)
8393 Lisp_Object dummy;
8394 {
8395 pop_message ();
8396 return Qnil;
8397 }
8398
8399 /* Pop the top-most entry off Vmessage_stack. */
8400
8401 void
8402 pop_message ()
8403 {
8404 xassert (CONSP (Vmessage_stack));
8405 Vmessage_stack = XCDR (Vmessage_stack);
8406 }
8407
8408
8409 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8410 exits. If the stack is not empty, we have a missing pop_message
8411 somewhere. */
8412
8413 void
8414 check_message_stack ()
8415 {
8416 if (!NILP (Vmessage_stack))
8417 abort ();
8418 }
8419
8420
8421 /* Truncate to NCHARS what will be displayed in the echo area the next
8422 time we display it---but don't redisplay it now. */
8423
8424 void
8425 truncate_echo_area (nchars)
8426 int nchars;
8427 {
8428 if (nchars == 0)
8429 echo_area_buffer[0] = Qnil;
8430 /* A null message buffer means that the frame hasn't really been
8431 initialized yet. Error messages get reported properly by
8432 cmd_error, so this must be just an informative message; toss it. */
8433 else if (!noninteractive
8434 && INTERACTIVE
8435 && !NILP (echo_area_buffer[0]))
8436 {
8437 struct frame *sf = SELECTED_FRAME ();
8438 if (FRAME_MESSAGE_BUF (sf))
8439 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8440 }
8441 }
8442
8443
8444 /* Helper function for truncate_echo_area. Truncate the current
8445 message to at most NCHARS characters. */
8446
8447 static int
8448 truncate_message_1 (nchars, a2, a3, a4)
8449 EMACS_INT nchars;
8450 Lisp_Object a2;
8451 EMACS_INT a3, a4;
8452 {
8453 if (BEG + nchars < Z)
8454 del_range (BEG + nchars, Z);
8455 if (Z == BEG)
8456 echo_area_buffer[0] = Qnil;
8457 return 0;
8458 }
8459
8460
8461 /* Set the current message to a substring of S or STRING.
8462
8463 If STRING is a Lisp string, set the message to the first NBYTES
8464 bytes from STRING. NBYTES zero means use the whole string. If
8465 STRING is multibyte, the message will be displayed multibyte.
8466
8467 If S is not null, set the message to the first LEN bytes of S. LEN
8468 zero means use the whole string. MULTIBYTE_P non-zero means S is
8469 multibyte. Display the message multibyte in that case.
8470
8471 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8472 to t before calling set_message_1 (which calls insert).
8473 */
8474
8475 void
8476 set_message (s, string, nbytes, multibyte_p)
8477 const char *s;
8478 Lisp_Object string;
8479 int nbytes, multibyte_p;
8480 {
8481 message_enable_multibyte
8482 = ((s && multibyte_p)
8483 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8484
8485 with_echo_area_buffer (0, 0, set_message_1,
8486 (EMACS_INT) s, string, nbytes, multibyte_p);
8487 message_buf_print = 0;
8488 help_echo_showing_p = 0;
8489 }
8490
8491
8492 /* Helper function for set_message. Arguments have the same meaning
8493 as there, with A1 corresponding to S and A2 corresponding to STRING
8494 This function is called with the echo area buffer being
8495 current. */
8496
8497 static int
8498 set_message_1 (a1, a2, nbytes, multibyte_p)
8499 EMACS_INT a1;
8500 Lisp_Object a2;
8501 EMACS_INT nbytes, multibyte_p;
8502 {
8503 const char *s = (const char *) a1;
8504 Lisp_Object string = a2;
8505
8506 /* Change multibyteness of the echo buffer appropriately. */
8507 if (message_enable_multibyte
8508 != !NILP (current_buffer->enable_multibyte_characters))
8509 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8510
8511 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8512
8513 /* Insert new message at BEG. */
8514 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8515 Ferase_buffer ();
8516
8517 if (STRINGP (string))
8518 {
8519 int nchars;
8520
8521 if (nbytes == 0)
8522 nbytes = SBYTES (string);
8523 nchars = string_byte_to_char (string, nbytes);
8524
8525 /* This function takes care of single/multibyte conversion. We
8526 just have to ensure that the echo area buffer has the right
8527 setting of enable_multibyte_characters. */
8528 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8529 }
8530 else if (s)
8531 {
8532 if (nbytes == 0)
8533 nbytes = strlen (s);
8534
8535 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8536 {
8537 /* Convert from multi-byte to single-byte. */
8538 int i, c, n;
8539 unsigned char work[1];
8540
8541 /* Convert a multibyte string to single-byte. */
8542 for (i = 0; i < nbytes; i += n)
8543 {
8544 c = string_char_and_length (s + i, nbytes - i, &n);
8545 work[0] = (SINGLE_BYTE_CHAR_P (c)
8546 ? c
8547 : multibyte_char_to_unibyte (c, Qnil));
8548 insert_1_both (work, 1, 1, 1, 0, 0);
8549 }
8550 }
8551 else if (!multibyte_p
8552 && !NILP (current_buffer->enable_multibyte_characters))
8553 {
8554 /* Convert from single-byte to multi-byte. */
8555 int i, c, n;
8556 const unsigned char *msg = (const unsigned char *) s;
8557 unsigned char str[MAX_MULTIBYTE_LENGTH];
8558
8559 /* Convert a single-byte string to multibyte. */
8560 for (i = 0; i < nbytes; i++)
8561 {
8562 c = unibyte_char_to_multibyte (msg[i]);
8563 n = CHAR_STRING (c, str);
8564 insert_1_both (str, 1, n, 1, 0, 0);
8565 }
8566 }
8567 else
8568 insert_1 (s, nbytes, 1, 0, 0);
8569 }
8570
8571 return 0;
8572 }
8573
8574
8575 /* Clear messages. CURRENT_P non-zero means clear the current
8576 message. LAST_DISPLAYED_P non-zero means clear the message
8577 last displayed. */
8578
8579 void
8580 clear_message (current_p, last_displayed_p)
8581 int current_p, last_displayed_p;
8582 {
8583 if (current_p)
8584 {
8585 echo_area_buffer[0] = Qnil;
8586 message_cleared_p = 1;
8587 }
8588
8589 if (last_displayed_p)
8590 echo_area_buffer[1] = Qnil;
8591
8592 message_buf_print = 0;
8593 }
8594
8595 /* Clear garbaged frames.
8596
8597 This function is used where the old redisplay called
8598 redraw_garbaged_frames which in turn called redraw_frame which in
8599 turn called clear_frame. The call to clear_frame was a source of
8600 flickering. I believe a clear_frame is not necessary. It should
8601 suffice in the new redisplay to invalidate all current matrices,
8602 and ensure a complete redisplay of all windows. */
8603
8604 static void
8605 clear_garbaged_frames ()
8606 {
8607 if (frame_garbaged)
8608 {
8609 Lisp_Object tail, frame;
8610 int changed_count = 0;
8611
8612 FOR_EACH_FRAME (tail, frame)
8613 {
8614 struct frame *f = XFRAME (frame);
8615
8616 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8617 {
8618 if (f->resized_p)
8619 {
8620 Fredraw_frame (frame);
8621 f->force_flush_display_p = 1;
8622 }
8623 clear_current_matrices (f);
8624 changed_count++;
8625 f->garbaged = 0;
8626 f->resized_p = 0;
8627 }
8628 }
8629
8630 frame_garbaged = 0;
8631 if (changed_count)
8632 ++windows_or_buffers_changed;
8633 }
8634 }
8635
8636
8637 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8638 is non-zero update selected_frame. Value is non-zero if the
8639 mini-windows height has been changed. */
8640
8641 static int
8642 echo_area_display (update_frame_p)
8643 int update_frame_p;
8644 {
8645 Lisp_Object mini_window;
8646 struct window *w;
8647 struct frame *f;
8648 int window_height_changed_p = 0;
8649 struct frame *sf = SELECTED_FRAME ();
8650
8651 mini_window = FRAME_MINIBUF_WINDOW (sf);
8652 w = XWINDOW (mini_window);
8653 f = XFRAME (WINDOW_FRAME (w));
8654
8655 /* Don't display if frame is invisible or not yet initialized. */
8656 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8657 return 0;
8658
8659 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8660 #ifndef MAC_OS8
8661 #ifdef HAVE_WINDOW_SYSTEM
8662 /* When Emacs starts, selected_frame may be a visible terminal
8663 frame, even if we run under a window system. If we let this
8664 through, a message would be displayed on the terminal. */
8665 if (EQ (selected_frame, Vterminal_frame)
8666 && !NILP (Vwindow_system))
8667 return 0;
8668 #endif /* HAVE_WINDOW_SYSTEM */
8669 #endif
8670
8671 /* Redraw garbaged frames. */
8672 if (frame_garbaged)
8673 clear_garbaged_frames ();
8674
8675 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8676 {
8677 echo_area_window = mini_window;
8678 window_height_changed_p = display_echo_area (w);
8679 w->must_be_updated_p = 1;
8680
8681 /* Update the display, unless called from redisplay_internal.
8682 Also don't update the screen during redisplay itself. The
8683 update will happen at the end of redisplay, and an update
8684 here could cause confusion. */
8685 if (update_frame_p && !redisplaying_p)
8686 {
8687 int n = 0;
8688
8689 /* If the display update has been interrupted by pending
8690 input, update mode lines in the frame. Due to the
8691 pending input, it might have been that redisplay hasn't
8692 been called, so that mode lines above the echo area are
8693 garbaged. This looks odd, so we prevent it here. */
8694 if (!display_completed)
8695 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8696
8697 if (window_height_changed_p
8698 /* Don't do this if Emacs is shutting down. Redisplay
8699 needs to run hooks. */
8700 && !NILP (Vrun_hooks))
8701 {
8702 /* Must update other windows. Likewise as in other
8703 cases, don't let this update be interrupted by
8704 pending input. */
8705 int count = SPECPDL_INDEX ();
8706 specbind (Qredisplay_dont_pause, Qt);
8707 windows_or_buffers_changed = 1;
8708 redisplay_internal (0);
8709 unbind_to (count, Qnil);
8710 }
8711 else if (FRAME_WINDOW_P (f) && n == 0)
8712 {
8713 /* Window configuration is the same as before.
8714 Can do with a display update of the echo area,
8715 unless we displayed some mode lines. */
8716 update_single_window (w, 1);
8717 rif->flush_display (f);
8718 }
8719 else
8720 update_frame (f, 1, 1);
8721
8722 /* If cursor is in the echo area, make sure that the next
8723 redisplay displays the minibuffer, so that the cursor will
8724 be replaced with what the minibuffer wants. */
8725 if (cursor_in_echo_area)
8726 ++windows_or_buffers_changed;
8727 }
8728 }
8729 else if (!EQ (mini_window, selected_window))
8730 windows_or_buffers_changed++;
8731
8732 /* The current message is now also the last one displayed. */
8733 echo_area_buffer[1] = echo_area_buffer[0];
8734
8735 /* Prevent redisplay optimization in redisplay_internal by resetting
8736 this_line_start_pos. This is done because the mini-buffer now
8737 displays the message instead of its buffer text. */
8738 if (EQ (mini_window, selected_window))
8739 CHARPOS (this_line_start_pos) = 0;
8740
8741 return window_height_changed_p;
8742 }
8743
8744
8745 \f
8746 /***********************************************************************
8747 Mode Lines and Frame Titles
8748 ***********************************************************************/
8749
8750 /* A buffer for constructing non-propertized mode-line strings and
8751 frame titles in it; allocated from the heap in init_xdisp and
8752 resized as needed in store_mode_line_noprop_char. */
8753
8754 static char *mode_line_noprop_buf;
8755
8756 /* The buffer's end, and a current output position in it. */
8757
8758 static char *mode_line_noprop_buf_end;
8759 static char *mode_line_noprop_ptr;
8760
8761 #define MODE_LINE_NOPROP_LEN(start) \
8762 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8763
8764 static enum {
8765 MODE_LINE_DISPLAY = 0,
8766 MODE_LINE_TITLE,
8767 MODE_LINE_NOPROP,
8768 MODE_LINE_STRING
8769 } mode_line_target;
8770
8771 /* Alist that caches the results of :propertize.
8772 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8773 static Lisp_Object mode_line_proptrans_alist;
8774
8775 /* List of strings making up the mode-line. */
8776 static Lisp_Object mode_line_string_list;
8777
8778 /* Base face property when building propertized mode line string. */
8779 static Lisp_Object mode_line_string_face;
8780 static Lisp_Object mode_line_string_face_prop;
8781
8782
8783 /* Unwind data for mode line strings */
8784
8785 static Lisp_Object Vmode_line_unwind_vector;
8786
8787 static Lisp_Object
8788 format_mode_line_unwind_data (obuf, save_proptrans)
8789 struct buffer *obuf;
8790 {
8791 Lisp_Object vector;
8792
8793 /* Reduce consing by keeping one vector in
8794 Vwith_echo_area_save_vector. */
8795 vector = Vmode_line_unwind_vector;
8796 Vmode_line_unwind_vector = Qnil;
8797
8798 if (NILP (vector))
8799 vector = Fmake_vector (make_number (7), Qnil);
8800
8801 AREF (vector, 0) = make_number (mode_line_target);
8802 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8803 AREF (vector, 2) = mode_line_string_list;
8804 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8805 AREF (vector, 4) = mode_line_string_face;
8806 AREF (vector, 5) = mode_line_string_face_prop;
8807
8808 if (obuf)
8809 XSETBUFFER (AREF (vector, 6), obuf);
8810 else
8811 AREF (vector, 6) = Qnil;
8812
8813 return vector;
8814 }
8815
8816 static Lisp_Object
8817 unwind_format_mode_line (vector)
8818 Lisp_Object vector;
8819 {
8820 mode_line_target = XINT (AREF (vector, 0));
8821 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8822 mode_line_string_list = AREF (vector, 2);
8823 if (! EQ (AREF (vector, 3), Qt))
8824 mode_line_proptrans_alist = AREF (vector, 3);
8825 mode_line_string_face = AREF (vector, 4);
8826 mode_line_string_face_prop = AREF (vector, 5);
8827
8828 if (!NILP (AREF (vector, 6)))
8829 {
8830 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8831 AREF (vector, 6) = Qnil;
8832 }
8833
8834 Vmode_line_unwind_vector = vector;
8835 return Qnil;
8836 }
8837
8838
8839 /* Store a single character C for the frame title in mode_line_noprop_buf.
8840 Re-allocate mode_line_noprop_buf if necessary. */
8841
8842 static void
8843 #ifdef PROTOTYPES
8844 store_mode_line_noprop_char (char c)
8845 #else
8846 store_mode_line_noprop_char (c)
8847 char c;
8848 #endif
8849 {
8850 /* If output position has reached the end of the allocated buffer,
8851 double the buffer's size. */
8852 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8853 {
8854 int len = MODE_LINE_NOPROP_LEN (0);
8855 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8856 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8857 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8858 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8859 }
8860
8861 *mode_line_noprop_ptr++ = c;
8862 }
8863
8864
8865 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8866 mode_line_noprop_ptr. STR is the string to store. Do not copy
8867 characters that yield more columns than PRECISION; PRECISION <= 0
8868 means copy the whole string. Pad with spaces until FIELD_WIDTH
8869 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8870 pad. Called from display_mode_element when it is used to build a
8871 frame title. */
8872
8873 static int
8874 store_mode_line_noprop (str, field_width, precision)
8875 const unsigned char *str;
8876 int field_width, precision;
8877 {
8878 int n = 0;
8879 int dummy, nbytes;
8880
8881 /* Copy at most PRECISION chars from STR. */
8882 nbytes = strlen (str);
8883 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8884 while (nbytes--)
8885 store_mode_line_noprop_char (*str++);
8886
8887 /* Fill up with spaces until FIELD_WIDTH reached. */
8888 while (field_width > 0
8889 && n < field_width)
8890 {
8891 store_mode_line_noprop_char (' ');
8892 ++n;
8893 }
8894
8895 return n;
8896 }
8897
8898 /***********************************************************************
8899 Frame Titles
8900 ***********************************************************************/
8901
8902 #ifdef HAVE_WINDOW_SYSTEM
8903
8904 /* Set the title of FRAME, if it has changed. The title format is
8905 Vicon_title_format if FRAME is iconified, otherwise it is
8906 frame_title_format. */
8907
8908 static void
8909 x_consider_frame_title (frame)
8910 Lisp_Object frame;
8911 {
8912 struct frame *f = XFRAME (frame);
8913
8914 if (FRAME_WINDOW_P (f)
8915 || FRAME_MINIBUF_ONLY_P (f)
8916 || f->explicit_name)
8917 {
8918 /* Do we have more than one visible frame on this X display? */
8919 Lisp_Object tail;
8920 Lisp_Object fmt;
8921 int title_start;
8922 char *title;
8923 int len;
8924 struct it it;
8925 int count = SPECPDL_INDEX ();
8926
8927 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8928 {
8929 Lisp_Object other_frame = XCAR (tail);
8930 struct frame *tf = XFRAME (other_frame);
8931
8932 if (tf != f
8933 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8934 && !FRAME_MINIBUF_ONLY_P (tf)
8935 && !EQ (other_frame, tip_frame)
8936 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8937 break;
8938 }
8939
8940 /* Set global variable indicating that multiple frames exist. */
8941 multiple_frames = CONSP (tail);
8942
8943 /* Switch to the buffer of selected window of the frame. Set up
8944 mode_line_target so that display_mode_element will output into
8945 mode_line_noprop_buf; then display the title. */
8946 record_unwind_protect (unwind_format_mode_line,
8947 format_mode_line_unwind_data (current_buffer, 0));
8948
8949 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8950 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8951
8952 mode_line_target = MODE_LINE_TITLE;
8953 title_start = MODE_LINE_NOPROP_LEN (0);
8954 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8955 NULL, DEFAULT_FACE_ID);
8956 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8957 len = MODE_LINE_NOPROP_LEN (title_start);
8958 title = mode_line_noprop_buf + title_start;
8959 unbind_to (count, Qnil);
8960
8961 /* Set the title only if it's changed. This avoids consing in
8962 the common case where it hasn't. (If it turns out that we've
8963 already wasted too much time by walking through the list with
8964 display_mode_element, then we might need to optimize at a
8965 higher level than this.) */
8966 if (! STRINGP (f->name)
8967 || SBYTES (f->name) != len
8968 || bcmp (title, SDATA (f->name), len) != 0)
8969 x_implicitly_set_name (f, make_string (title, len), Qnil);
8970 }
8971 }
8972
8973 #endif /* not HAVE_WINDOW_SYSTEM */
8974
8975
8976
8977 \f
8978 /***********************************************************************
8979 Menu Bars
8980 ***********************************************************************/
8981
8982
8983 /* Prepare for redisplay by updating menu-bar item lists when
8984 appropriate. This can call eval. */
8985
8986 void
8987 prepare_menu_bars ()
8988 {
8989 int all_windows;
8990 struct gcpro gcpro1, gcpro2;
8991 struct frame *f;
8992 Lisp_Object tooltip_frame;
8993
8994 #ifdef HAVE_WINDOW_SYSTEM
8995 tooltip_frame = tip_frame;
8996 #else
8997 tooltip_frame = Qnil;
8998 #endif
8999
9000 /* Update all frame titles based on their buffer names, etc. We do
9001 this before the menu bars so that the buffer-menu will show the
9002 up-to-date frame titles. */
9003 #ifdef HAVE_WINDOW_SYSTEM
9004 if (windows_or_buffers_changed || update_mode_lines)
9005 {
9006 Lisp_Object tail, frame;
9007
9008 FOR_EACH_FRAME (tail, frame)
9009 {
9010 f = XFRAME (frame);
9011 if (!EQ (frame, tooltip_frame)
9012 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9013 x_consider_frame_title (frame);
9014 }
9015 }
9016 #endif /* HAVE_WINDOW_SYSTEM */
9017
9018 /* Update the menu bar item lists, if appropriate. This has to be
9019 done before any actual redisplay or generation of display lines. */
9020 all_windows = (update_mode_lines
9021 || buffer_shared > 1
9022 || windows_or_buffers_changed);
9023 if (all_windows)
9024 {
9025 Lisp_Object tail, frame;
9026 int count = SPECPDL_INDEX ();
9027
9028 record_unwind_save_match_data ();
9029
9030 FOR_EACH_FRAME (tail, frame)
9031 {
9032 f = XFRAME (frame);
9033
9034 /* Ignore tooltip frame. */
9035 if (EQ (frame, tooltip_frame))
9036 continue;
9037
9038 /* If a window on this frame changed size, report that to
9039 the user and clear the size-change flag. */
9040 if (FRAME_WINDOW_SIZES_CHANGED (f))
9041 {
9042 Lisp_Object functions;
9043
9044 /* Clear flag first in case we get an error below. */
9045 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9046 functions = Vwindow_size_change_functions;
9047 GCPRO2 (tail, functions);
9048
9049 while (CONSP (functions))
9050 {
9051 call1 (XCAR (functions), frame);
9052 functions = XCDR (functions);
9053 }
9054 UNGCPRO;
9055 }
9056
9057 GCPRO1 (tail);
9058 update_menu_bar (f, 0);
9059 #ifdef HAVE_WINDOW_SYSTEM
9060 update_tool_bar (f, 0);
9061 #ifdef MAC_OS
9062 mac_update_title_bar (f, 0);
9063 #endif
9064 #endif
9065 UNGCPRO;
9066 }
9067
9068 unbind_to (count, Qnil);
9069 }
9070 else
9071 {
9072 struct frame *sf = SELECTED_FRAME ();
9073 update_menu_bar (sf, 1);
9074 #ifdef HAVE_WINDOW_SYSTEM
9075 update_tool_bar (sf, 1);
9076 #ifdef MAC_OS
9077 mac_update_title_bar (sf, 1);
9078 #endif
9079 #endif
9080 }
9081
9082 /* Motif needs this. See comment in xmenu.c. Turn it off when
9083 pending_menu_activation is not defined. */
9084 #ifdef USE_X_TOOLKIT
9085 pending_menu_activation = 0;
9086 #endif
9087 }
9088
9089
9090 /* Update the menu bar item list for frame F. This has to be done
9091 before we start to fill in any display lines, because it can call
9092 eval.
9093
9094 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
9095
9096 static void
9097 update_menu_bar (f, save_match_data)
9098 struct frame *f;
9099 int save_match_data;
9100 {
9101 Lisp_Object window;
9102 register struct window *w;
9103
9104 /* If called recursively during a menu update, do nothing. This can
9105 happen when, for instance, an activate-menubar-hook causes a
9106 redisplay. */
9107 if (inhibit_menubar_update)
9108 return;
9109
9110 window = FRAME_SELECTED_WINDOW (f);
9111 w = XWINDOW (window);
9112
9113 #if 0 /* The if statement below this if statement used to include the
9114 condition !NILP (w->update_mode_line), rather than using
9115 update_mode_lines directly, and this if statement may have
9116 been added to make that condition work. Now the if
9117 statement below matches its comment, this isn't needed. */
9118 if (update_mode_lines)
9119 w->update_mode_line = Qt;
9120 #endif
9121
9122 if (FRAME_WINDOW_P (f)
9123 ?
9124 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9125 || defined (USE_GTK)
9126 FRAME_EXTERNAL_MENU_BAR (f)
9127 #else
9128 FRAME_MENU_BAR_LINES (f) > 0
9129 #endif
9130 : FRAME_MENU_BAR_LINES (f) > 0)
9131 {
9132 /* If the user has switched buffers or windows, we need to
9133 recompute to reflect the new bindings. But we'll
9134 recompute when update_mode_lines is set too; that means
9135 that people can use force-mode-line-update to request
9136 that the menu bar be recomputed. The adverse effect on
9137 the rest of the redisplay algorithm is about the same as
9138 windows_or_buffers_changed anyway. */
9139 if (windows_or_buffers_changed
9140 /* This used to test w->update_mode_line, but we believe
9141 there is no need to recompute the menu in that case. */
9142 || update_mode_lines
9143 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9144 < BUF_MODIFF (XBUFFER (w->buffer)))
9145 != !NILP (w->last_had_star))
9146 || ((!NILP (Vtransient_mark_mode)
9147 && !NILP (XBUFFER (w->buffer)->mark_active))
9148 != !NILP (w->region_showing)))
9149 {
9150 struct buffer *prev = current_buffer;
9151 int count = SPECPDL_INDEX ();
9152
9153 specbind (Qinhibit_menubar_update, Qt);
9154
9155 set_buffer_internal_1 (XBUFFER (w->buffer));
9156 if (save_match_data)
9157 record_unwind_save_match_data ();
9158 if (NILP (Voverriding_local_map_menu_flag))
9159 {
9160 specbind (Qoverriding_terminal_local_map, Qnil);
9161 specbind (Qoverriding_local_map, Qnil);
9162 }
9163
9164 /* Run the Lucid hook. */
9165 safe_run_hooks (Qactivate_menubar_hook);
9166
9167 /* If it has changed current-menubar from previous value,
9168 really recompute the menu-bar from the value. */
9169 if (! NILP (Vlucid_menu_bar_dirty_flag))
9170 call0 (Qrecompute_lucid_menubar);
9171
9172 safe_run_hooks (Qmenu_bar_update_hook);
9173 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9174
9175 /* Redisplay the menu bar in case we changed it. */
9176 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9177 || defined (USE_GTK)
9178 if (FRAME_WINDOW_P (f))
9179 {
9180 #ifdef MAC_OS
9181 /* All frames on Mac OS share the same menubar. So only
9182 the selected frame should be allowed to set it. */
9183 if (f == SELECTED_FRAME ())
9184 #endif
9185 set_frame_menubar (f, 0, 0);
9186 }
9187 else
9188 /* On a terminal screen, the menu bar is an ordinary screen
9189 line, and this makes it get updated. */
9190 w->update_mode_line = Qt;
9191 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9192 /* In the non-toolkit version, the menu bar is an ordinary screen
9193 line, and this makes it get updated. */
9194 w->update_mode_line = Qt;
9195 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9196
9197 unbind_to (count, Qnil);
9198 set_buffer_internal_1 (prev);
9199 }
9200 }
9201 }
9202
9203
9204 \f
9205 /***********************************************************************
9206 Output Cursor
9207 ***********************************************************************/
9208
9209 #ifdef HAVE_WINDOW_SYSTEM
9210
9211 /* EXPORT:
9212 Nominal cursor position -- where to draw output.
9213 HPOS and VPOS are window relative glyph matrix coordinates.
9214 X and Y are window relative pixel coordinates. */
9215
9216 struct cursor_pos output_cursor;
9217
9218
9219 /* EXPORT:
9220 Set the global variable output_cursor to CURSOR. All cursor
9221 positions are relative to updated_window. */
9222
9223 void
9224 set_output_cursor (cursor)
9225 struct cursor_pos *cursor;
9226 {
9227 output_cursor.hpos = cursor->hpos;
9228 output_cursor.vpos = cursor->vpos;
9229 output_cursor.x = cursor->x;
9230 output_cursor.y = cursor->y;
9231 }
9232
9233
9234 /* EXPORT for RIF:
9235 Set a nominal cursor position.
9236
9237 HPOS and VPOS are column/row positions in a window glyph matrix. X
9238 and Y are window text area relative pixel positions.
9239
9240 If this is done during an update, updated_window will contain the
9241 window that is being updated and the position is the future output
9242 cursor position for that window. If updated_window is null, use
9243 selected_window and display the cursor at the given position. */
9244
9245 void
9246 x_cursor_to (vpos, hpos, y, x)
9247 int vpos, hpos, y, x;
9248 {
9249 struct window *w;
9250
9251 /* If updated_window is not set, work on selected_window. */
9252 if (updated_window)
9253 w = updated_window;
9254 else
9255 w = XWINDOW (selected_window);
9256
9257 /* Set the output cursor. */
9258 output_cursor.hpos = hpos;
9259 output_cursor.vpos = vpos;
9260 output_cursor.x = x;
9261 output_cursor.y = y;
9262
9263 /* If not called as part of an update, really display the cursor.
9264 This will also set the cursor position of W. */
9265 if (updated_window == NULL)
9266 {
9267 BLOCK_INPUT;
9268 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9269 if (rif->flush_display_optional)
9270 rif->flush_display_optional (SELECTED_FRAME ());
9271 UNBLOCK_INPUT;
9272 }
9273 }
9274
9275 #endif /* HAVE_WINDOW_SYSTEM */
9276
9277 \f
9278 /***********************************************************************
9279 Tool-bars
9280 ***********************************************************************/
9281
9282 #ifdef HAVE_WINDOW_SYSTEM
9283
9284 /* Where the mouse was last time we reported a mouse event. */
9285
9286 FRAME_PTR last_mouse_frame;
9287
9288 /* Tool-bar item index of the item on which a mouse button was pressed
9289 or -1. */
9290
9291 int last_tool_bar_item;
9292
9293
9294 /* Update the tool-bar item list for frame F. This has to be done
9295 before we start to fill in any display lines. Called from
9296 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9297 and restore it here. */
9298
9299 static void
9300 update_tool_bar (f, save_match_data)
9301 struct frame *f;
9302 int save_match_data;
9303 {
9304 #ifdef USE_GTK
9305 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9306 #else
9307 int do_update = WINDOWP (f->tool_bar_window)
9308 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9309 #endif
9310
9311 if (do_update)
9312 {
9313 Lisp_Object window;
9314 struct window *w;
9315
9316 window = FRAME_SELECTED_WINDOW (f);
9317 w = XWINDOW (window);
9318
9319 /* If the user has switched buffers or windows, we need to
9320 recompute to reflect the new bindings. But we'll
9321 recompute when update_mode_lines is set too; that means
9322 that people can use force-mode-line-update to request
9323 that the menu bar be recomputed. The adverse effect on
9324 the rest of the redisplay algorithm is about the same as
9325 windows_or_buffers_changed anyway. */
9326 if (windows_or_buffers_changed
9327 || !NILP (w->update_mode_line)
9328 || update_mode_lines
9329 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9330 < BUF_MODIFF (XBUFFER (w->buffer)))
9331 != !NILP (w->last_had_star))
9332 || ((!NILP (Vtransient_mark_mode)
9333 && !NILP (XBUFFER (w->buffer)->mark_active))
9334 != !NILP (w->region_showing)))
9335 {
9336 struct buffer *prev = current_buffer;
9337 int count = SPECPDL_INDEX ();
9338 Lisp_Object new_tool_bar;
9339 int new_n_tool_bar;
9340 struct gcpro gcpro1;
9341
9342 /* Set current_buffer to the buffer of the selected
9343 window of the frame, so that we get the right local
9344 keymaps. */
9345 set_buffer_internal_1 (XBUFFER (w->buffer));
9346
9347 /* Save match data, if we must. */
9348 if (save_match_data)
9349 record_unwind_save_match_data ();
9350
9351 /* Make sure that we don't accidentally use bogus keymaps. */
9352 if (NILP (Voverriding_local_map_menu_flag))
9353 {
9354 specbind (Qoverriding_terminal_local_map, Qnil);
9355 specbind (Qoverriding_local_map, Qnil);
9356 }
9357
9358 GCPRO1 (new_tool_bar);
9359
9360 /* Build desired tool-bar items from keymaps. */
9361 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9362 &new_n_tool_bar);
9363
9364 /* Redisplay the tool-bar if we changed it. */
9365 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9366 {
9367 /* Redisplay that happens asynchronously due to an expose event
9368 may access f->tool_bar_items. Make sure we update both
9369 variables within BLOCK_INPUT so no such event interrupts. */
9370 BLOCK_INPUT;
9371 f->tool_bar_items = new_tool_bar;
9372 f->n_tool_bar_items = new_n_tool_bar;
9373 w->update_mode_line = Qt;
9374 UNBLOCK_INPUT;
9375 }
9376
9377 UNGCPRO;
9378
9379 unbind_to (count, Qnil);
9380 set_buffer_internal_1 (prev);
9381 }
9382 }
9383 }
9384
9385
9386 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9387 F's desired tool-bar contents. F->tool_bar_items must have
9388 been set up previously by calling prepare_menu_bars. */
9389
9390 static void
9391 build_desired_tool_bar_string (f)
9392 struct frame *f;
9393 {
9394 int i, size, size_needed;
9395 struct gcpro gcpro1, gcpro2, gcpro3;
9396 Lisp_Object image, plist, props;
9397
9398 image = plist = props = Qnil;
9399 GCPRO3 (image, plist, props);
9400
9401 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9402 Otherwise, make a new string. */
9403
9404 /* The size of the string we might be able to reuse. */
9405 size = (STRINGP (f->desired_tool_bar_string)
9406 ? SCHARS (f->desired_tool_bar_string)
9407 : 0);
9408
9409 /* We need one space in the string for each image. */
9410 size_needed = f->n_tool_bar_items;
9411
9412 /* Reuse f->desired_tool_bar_string, if possible. */
9413 if (size < size_needed || NILP (f->desired_tool_bar_string))
9414 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9415 make_number (' '));
9416 else
9417 {
9418 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9419 Fremove_text_properties (make_number (0), make_number (size),
9420 props, f->desired_tool_bar_string);
9421 }
9422
9423 /* Put a `display' property on the string for the images to display,
9424 put a `menu_item' property on tool-bar items with a value that
9425 is the index of the item in F's tool-bar item vector. */
9426 for (i = 0; i < f->n_tool_bar_items; ++i)
9427 {
9428 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9429
9430 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9431 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9432 int hmargin, vmargin, relief, idx, end;
9433 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9434
9435 /* If image is a vector, choose the image according to the
9436 button state. */
9437 image = PROP (TOOL_BAR_ITEM_IMAGES);
9438 if (VECTORP (image))
9439 {
9440 if (enabled_p)
9441 idx = (selected_p
9442 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9443 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9444 else
9445 idx = (selected_p
9446 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9447 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9448
9449 xassert (ASIZE (image) >= idx);
9450 image = AREF (image, idx);
9451 }
9452 else
9453 idx = -1;
9454
9455 /* Ignore invalid image specifications. */
9456 if (!valid_image_p (image))
9457 continue;
9458
9459 /* Display the tool-bar button pressed, or depressed. */
9460 plist = Fcopy_sequence (XCDR (image));
9461
9462 /* Compute margin and relief to draw. */
9463 relief = (tool_bar_button_relief >= 0
9464 ? tool_bar_button_relief
9465 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9466 hmargin = vmargin = relief;
9467
9468 if (INTEGERP (Vtool_bar_button_margin)
9469 && XINT (Vtool_bar_button_margin) > 0)
9470 {
9471 hmargin += XFASTINT (Vtool_bar_button_margin);
9472 vmargin += XFASTINT (Vtool_bar_button_margin);
9473 }
9474 else if (CONSP (Vtool_bar_button_margin))
9475 {
9476 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9477 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9478 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9479
9480 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9481 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9482 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9483 }
9484
9485 if (auto_raise_tool_bar_buttons_p)
9486 {
9487 /* Add a `:relief' property to the image spec if the item is
9488 selected. */
9489 if (selected_p)
9490 {
9491 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9492 hmargin -= relief;
9493 vmargin -= relief;
9494 }
9495 }
9496 else
9497 {
9498 /* If image is selected, display it pressed, i.e. with a
9499 negative relief. If it's not selected, display it with a
9500 raised relief. */
9501 plist = Fplist_put (plist, QCrelief,
9502 (selected_p
9503 ? make_number (-relief)
9504 : make_number (relief)));
9505 hmargin -= relief;
9506 vmargin -= relief;
9507 }
9508
9509 /* Put a margin around the image. */
9510 if (hmargin || vmargin)
9511 {
9512 if (hmargin == vmargin)
9513 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9514 else
9515 plist = Fplist_put (plist, QCmargin,
9516 Fcons (make_number (hmargin),
9517 make_number (vmargin)));
9518 }
9519
9520 /* If button is not enabled, and we don't have special images
9521 for the disabled state, make the image appear disabled by
9522 applying an appropriate algorithm to it. */
9523 if (!enabled_p && idx < 0)
9524 plist = Fplist_put (plist, QCconversion, Qdisabled);
9525
9526 /* Put a `display' text property on the string for the image to
9527 display. Put a `menu-item' property on the string that gives
9528 the start of this item's properties in the tool-bar items
9529 vector. */
9530 image = Fcons (Qimage, plist);
9531 props = list4 (Qdisplay, image,
9532 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9533
9534 /* Let the last image hide all remaining spaces in the tool bar
9535 string. The string can be longer than needed when we reuse a
9536 previous string. */
9537 if (i + 1 == f->n_tool_bar_items)
9538 end = SCHARS (f->desired_tool_bar_string);
9539 else
9540 end = i + 1;
9541 Fadd_text_properties (make_number (i), make_number (end),
9542 props, f->desired_tool_bar_string);
9543 #undef PROP
9544 }
9545
9546 UNGCPRO;
9547 }
9548
9549
9550 /* Display one line of the tool-bar of frame IT->f.
9551
9552 HEIGHT specifies the desired height of the tool-bar line.
9553 If the actual height of the glyph row is less than HEIGHT, the
9554 row's height is increased to HEIGHT, and the icons are centered
9555 vertically in the new height.
9556
9557 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9558 count a final empty row in case the tool-bar width exactly matches
9559 the window width.
9560 */
9561
9562 static void
9563 display_tool_bar_line (it, height)
9564 struct it *it;
9565 int height;
9566 {
9567 struct glyph_row *row = it->glyph_row;
9568 int max_x = it->last_visible_x;
9569 struct glyph *last;
9570
9571 prepare_desired_row (row);
9572 row->y = it->current_y;
9573
9574 /* Note that this isn't made use of if the face hasn't a box,
9575 so there's no need to check the face here. */
9576 it->start_of_box_run_p = 1;
9577
9578 while (it->current_x < max_x)
9579 {
9580 int x, n_glyphs_before, i, nglyphs;
9581 struct it it_before;
9582
9583 /* Get the next display element. */
9584 if (!get_next_display_element (it))
9585 {
9586 /* Don't count empty row if we are counting needed tool-bar lines. */
9587 if (height < 0 && !it->hpos)
9588 return;
9589 break;
9590 }
9591
9592 /* Produce glyphs. */
9593 n_glyphs_before = row->used[TEXT_AREA];
9594 it_before = *it;
9595
9596 PRODUCE_GLYPHS (it);
9597
9598 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9599 i = 0;
9600 x = it_before.current_x;
9601 while (i < nglyphs)
9602 {
9603 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9604
9605 if (x + glyph->pixel_width > max_x)
9606 {
9607 /* Glyph doesn't fit on line. Backtrack. */
9608 row->used[TEXT_AREA] = n_glyphs_before;
9609 *it = it_before;
9610 goto out;
9611 }
9612
9613 ++it->hpos;
9614 x += glyph->pixel_width;
9615 ++i;
9616 }
9617
9618 /* Stop at line ends. */
9619 if (ITERATOR_AT_END_OF_LINE_P (it))
9620 break;
9621
9622 set_iterator_to_next (it, 1);
9623 }
9624
9625 out:;
9626
9627 row->displays_text_p = row->used[TEXT_AREA] != 0;
9628 /* Use default face for the border below the tool bar. */
9629 if (!row->displays_text_p)
9630 it->face_id = DEFAULT_FACE_ID;
9631 extend_face_to_end_of_line (it);
9632 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9633 last->right_box_line_p = 1;
9634 if (last == row->glyphs[TEXT_AREA])
9635 last->left_box_line_p = 1;
9636
9637 /* Make line the desired height and center it vertically. */
9638 if ((height -= it->max_ascent + it->max_descent) > 0)
9639 {
9640 /* Don't add more than one line height. */
9641 height %= FRAME_LINE_HEIGHT (it->f);
9642 it->max_ascent += height / 2;
9643 it->max_descent += (height + 1) / 2;
9644 }
9645
9646 compute_line_metrics (it);
9647
9648 /* If line is empty, make it occupy the rest of the tool-bar. */
9649 if (!row->displays_text_p)
9650 {
9651 row->height = row->phys_height = it->last_visible_y - row->y;
9652 row->ascent = row->phys_ascent = 0;
9653 row->extra_line_spacing = 0;
9654 }
9655
9656 row->full_width_p = 1;
9657 row->continued_p = 0;
9658 row->truncated_on_left_p = 0;
9659 row->truncated_on_right_p = 0;
9660
9661 it->current_x = it->hpos = 0;
9662 it->current_y += row->height;
9663 ++it->vpos;
9664 ++it->glyph_row;
9665 }
9666
9667
9668 /* Value is the number of screen lines needed to make all tool-bar
9669 items of frame F visible. The number of actual rows needed is
9670 returned in *N_ROWS if non-NULL. */
9671
9672 static int
9673 tool_bar_lines_needed (f, n_rows)
9674 struct frame *f;
9675 int *n_rows;
9676 {
9677 struct window *w = XWINDOW (f->tool_bar_window);
9678 struct it it;
9679 struct glyph_row *temp_row = w->desired_matrix->rows;
9680
9681 /* Initialize an iterator for iteration over
9682 F->desired_tool_bar_string in the tool-bar window of frame F. */
9683 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9684 it.first_visible_x = 0;
9685 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9686 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9687
9688 while (!ITERATOR_AT_END_P (&it))
9689 {
9690 clear_glyph_row (temp_row);
9691 it.glyph_row = temp_row;
9692 display_tool_bar_line (&it, -1);
9693 }
9694 clear_glyph_row (temp_row);
9695
9696 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9697 if (n_rows)
9698 *n_rows = it.vpos > 0 ? it.vpos : -1;
9699
9700 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9701 }
9702
9703
9704 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9705 0, 1, 0,
9706 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9707 (frame)
9708 Lisp_Object frame;
9709 {
9710 struct frame *f;
9711 struct window *w;
9712 int nlines = 0;
9713
9714 if (NILP (frame))
9715 frame = selected_frame;
9716 else
9717 CHECK_FRAME (frame);
9718 f = XFRAME (frame);
9719
9720 if (WINDOWP (f->tool_bar_window)
9721 || (w = XWINDOW (f->tool_bar_window),
9722 WINDOW_TOTAL_LINES (w) > 0))
9723 {
9724 update_tool_bar (f, 1);
9725 if (f->n_tool_bar_items)
9726 {
9727 build_desired_tool_bar_string (f);
9728 nlines = tool_bar_lines_needed (f, NULL);
9729 }
9730 }
9731
9732 return make_number (nlines);
9733 }
9734
9735
9736 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9737 height should be changed. */
9738
9739 static int
9740 redisplay_tool_bar (f)
9741 struct frame *f;
9742 {
9743 struct window *w;
9744 struct it it;
9745 struct glyph_row *row;
9746 int change_height_p = 0;
9747
9748 #ifdef USE_GTK
9749 if (FRAME_EXTERNAL_TOOL_BAR (f))
9750 update_frame_tool_bar (f);
9751 return 0;
9752 #endif
9753
9754 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9755 do anything. This means you must start with tool-bar-lines
9756 non-zero to get the auto-sizing effect. Or in other words, you
9757 can turn off tool-bars by specifying tool-bar-lines zero. */
9758 if (!WINDOWP (f->tool_bar_window)
9759 || (w = XWINDOW (f->tool_bar_window),
9760 WINDOW_TOTAL_LINES (w) == 0))
9761 return 0;
9762
9763 /* Set up an iterator for the tool-bar window. */
9764 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9765 it.first_visible_x = 0;
9766 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9767 row = it.glyph_row;
9768
9769 /* Build a string that represents the contents of the tool-bar. */
9770 build_desired_tool_bar_string (f);
9771 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9772
9773 if (f->n_tool_bar_rows == 0)
9774 {
9775 int nlines;
9776
9777 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9778 nlines != WINDOW_TOTAL_LINES (w)))
9779 {
9780 extern Lisp_Object Qtool_bar_lines;
9781 Lisp_Object frame;
9782 int old_height = WINDOW_TOTAL_LINES (w);
9783
9784 XSETFRAME (frame, f);
9785 clear_glyph_matrix (w->desired_matrix);
9786 Fmodify_frame_parameters (frame,
9787 Fcons (Fcons (Qtool_bar_lines,
9788 make_number (nlines)),
9789 Qnil));
9790 if (WINDOW_TOTAL_LINES (w) != old_height)
9791 {
9792 fonts_changed_p = 1;
9793 return 1;
9794 }
9795 }
9796 }
9797
9798 /* Display as many lines as needed to display all tool-bar items. */
9799
9800 if (f->n_tool_bar_rows > 0)
9801 {
9802 int border, rows, height, extra;
9803
9804 if (INTEGERP (Vtool_bar_border))
9805 border = XINT (Vtool_bar_border);
9806 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9807 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9808 else if (EQ (Vtool_bar_border, Qborder_width))
9809 border = f->border_width;
9810 else
9811 border = 0;
9812 if (border < 0)
9813 border = 0;
9814
9815 rows = f->n_tool_bar_rows;
9816 height = max (1, (it.last_visible_y - border) / rows);
9817 extra = it.last_visible_y - border - height * rows;
9818
9819 while (it.current_y < it.last_visible_y)
9820 {
9821 int h = 0;
9822 if (extra > 0 && rows-- > 0)
9823 {
9824 h = (extra + rows - 1) / rows;
9825 extra -= h;
9826 }
9827 display_tool_bar_line (&it, height + h);
9828 }
9829 }
9830 else
9831 {
9832 while (it.current_y < it.last_visible_y)
9833 display_tool_bar_line (&it, 0);
9834 }
9835
9836 /* It doesn't make much sense to try scrolling in the tool-bar
9837 window, so don't do it. */
9838 w->desired_matrix->no_scrolling_p = 1;
9839 w->must_be_updated_p = 1;
9840
9841 if (auto_resize_tool_bars_p)
9842 {
9843 int nlines;
9844
9845 /* If we couldn't display everything, change the tool-bar's
9846 height. */
9847 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9848 change_height_p = 1;
9849
9850 /* If there are blank lines at the end, except for a partially
9851 visible blank line at the end that is smaller than
9852 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9853 row = it.glyph_row - 1;
9854 if (!row->displays_text_p
9855 && row->height >= FRAME_LINE_HEIGHT (f))
9856 change_height_p = 1;
9857
9858 /* If row displays tool-bar items, but is partially visible,
9859 change the tool-bar's height. */
9860 if (row->displays_text_p
9861 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9862 change_height_p = 1;
9863
9864 /* Resize windows as needed by changing the `tool-bar-lines'
9865 frame parameter. */
9866 if (change_height_p
9867 && (nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9868 nlines != WINDOW_TOTAL_LINES (w)))
9869 {
9870 extern Lisp_Object Qtool_bar_lines;
9871 Lisp_Object frame;
9872 int old_height = WINDOW_TOTAL_LINES (w);
9873
9874 XSETFRAME (frame, f);
9875 clear_glyph_matrix (w->desired_matrix);
9876 Fmodify_frame_parameters (frame,
9877 Fcons (Fcons (Qtool_bar_lines,
9878 make_number (nlines)),
9879 Qnil));
9880 if (WINDOW_TOTAL_LINES (w) != old_height)
9881 fonts_changed_p = 1;
9882 }
9883 }
9884
9885 return change_height_p;
9886 }
9887
9888
9889 /* Get information about the tool-bar item which is displayed in GLYPH
9890 on frame F. Return in *PROP_IDX the index where tool-bar item
9891 properties start in F->tool_bar_items. Value is zero if
9892 GLYPH doesn't display a tool-bar item. */
9893
9894 static int
9895 tool_bar_item_info (f, glyph, prop_idx)
9896 struct frame *f;
9897 struct glyph *glyph;
9898 int *prop_idx;
9899 {
9900 Lisp_Object prop;
9901 int success_p;
9902 int charpos;
9903
9904 /* This function can be called asynchronously, which means we must
9905 exclude any possibility that Fget_text_property signals an
9906 error. */
9907 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9908 charpos = max (0, charpos);
9909
9910 /* Get the text property `menu-item' at pos. The value of that
9911 property is the start index of this item's properties in
9912 F->tool_bar_items. */
9913 prop = Fget_text_property (make_number (charpos),
9914 Qmenu_item, f->current_tool_bar_string);
9915 if (INTEGERP (prop))
9916 {
9917 *prop_idx = XINT (prop);
9918 success_p = 1;
9919 }
9920 else
9921 success_p = 0;
9922
9923 return success_p;
9924 }
9925
9926 \f
9927 /* Get information about the tool-bar item at position X/Y on frame F.
9928 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9929 the current matrix of the tool-bar window of F, or NULL if not
9930 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9931 item in F->tool_bar_items. Value is
9932
9933 -1 if X/Y is not on a tool-bar item
9934 0 if X/Y is on the same item that was highlighted before.
9935 1 otherwise. */
9936
9937 static int
9938 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9939 struct frame *f;
9940 int x, y;
9941 struct glyph **glyph;
9942 int *hpos, *vpos, *prop_idx;
9943 {
9944 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9945 struct window *w = XWINDOW (f->tool_bar_window);
9946 int area;
9947
9948 /* Find the glyph under X/Y. */
9949 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9950 if (*glyph == NULL)
9951 return -1;
9952
9953 /* Get the start of this tool-bar item's properties in
9954 f->tool_bar_items. */
9955 if (!tool_bar_item_info (f, *glyph, prop_idx))
9956 return -1;
9957
9958 /* Is mouse on the highlighted item? */
9959 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9960 && *vpos >= dpyinfo->mouse_face_beg_row
9961 && *vpos <= dpyinfo->mouse_face_end_row
9962 && (*vpos > dpyinfo->mouse_face_beg_row
9963 || *hpos >= dpyinfo->mouse_face_beg_col)
9964 && (*vpos < dpyinfo->mouse_face_end_row
9965 || *hpos < dpyinfo->mouse_face_end_col
9966 || dpyinfo->mouse_face_past_end))
9967 return 0;
9968
9969 return 1;
9970 }
9971
9972
9973 /* EXPORT:
9974 Handle mouse button event on the tool-bar of frame F, at
9975 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9976 0 for button release. MODIFIERS is event modifiers for button
9977 release. */
9978
9979 void
9980 handle_tool_bar_click (f, x, y, down_p, modifiers)
9981 struct frame *f;
9982 int x, y, down_p;
9983 unsigned int modifiers;
9984 {
9985 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9986 struct window *w = XWINDOW (f->tool_bar_window);
9987 int hpos, vpos, prop_idx;
9988 struct glyph *glyph;
9989 Lisp_Object enabled_p;
9990
9991 /* If not on the highlighted tool-bar item, return. */
9992 frame_to_window_pixel_xy (w, &x, &y);
9993 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9994 return;
9995
9996 /* If item is disabled, do nothing. */
9997 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9998 if (NILP (enabled_p))
9999 return;
10000
10001 if (down_p)
10002 {
10003 /* Show item in pressed state. */
10004 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10005 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10006 last_tool_bar_item = prop_idx;
10007 }
10008 else
10009 {
10010 Lisp_Object key, frame;
10011 struct input_event event;
10012 EVENT_INIT (event);
10013
10014 /* Show item in released state. */
10015 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10016 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10017
10018 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10019
10020 XSETFRAME (frame, f);
10021 event.kind = TOOL_BAR_EVENT;
10022 event.frame_or_window = frame;
10023 event.arg = frame;
10024 kbd_buffer_store_event (&event);
10025
10026 event.kind = TOOL_BAR_EVENT;
10027 event.frame_or_window = frame;
10028 event.arg = key;
10029 event.modifiers = modifiers;
10030 kbd_buffer_store_event (&event);
10031 last_tool_bar_item = -1;
10032 }
10033 }
10034
10035
10036 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10037 tool-bar window-relative coordinates X/Y. Called from
10038 note_mouse_highlight. */
10039
10040 static void
10041 note_tool_bar_highlight (f, x, y)
10042 struct frame *f;
10043 int x, y;
10044 {
10045 Lisp_Object window = f->tool_bar_window;
10046 struct window *w = XWINDOW (window);
10047 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10048 int hpos, vpos;
10049 struct glyph *glyph;
10050 struct glyph_row *row;
10051 int i;
10052 Lisp_Object enabled_p;
10053 int prop_idx;
10054 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10055 int mouse_down_p, rc;
10056
10057 /* Function note_mouse_highlight is called with negative x(y
10058 values when mouse moves outside of the frame. */
10059 if (x <= 0 || y <= 0)
10060 {
10061 clear_mouse_face (dpyinfo);
10062 return;
10063 }
10064
10065 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10066 if (rc < 0)
10067 {
10068 /* Not on tool-bar item. */
10069 clear_mouse_face (dpyinfo);
10070 return;
10071 }
10072 else if (rc == 0)
10073 /* On same tool-bar item as before. */
10074 goto set_help_echo;
10075
10076 clear_mouse_face (dpyinfo);
10077
10078 /* Mouse is down, but on different tool-bar item? */
10079 mouse_down_p = (dpyinfo->grabbed
10080 && f == last_mouse_frame
10081 && FRAME_LIVE_P (f));
10082 if (mouse_down_p
10083 && last_tool_bar_item != prop_idx)
10084 return;
10085
10086 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10087 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10088
10089 /* If tool-bar item is not enabled, don't highlight it. */
10090 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10091 if (!NILP (enabled_p))
10092 {
10093 /* Compute the x-position of the glyph. In front and past the
10094 image is a space. We include this in the highlighted area. */
10095 row = MATRIX_ROW (w->current_matrix, vpos);
10096 for (i = x = 0; i < hpos; ++i)
10097 x += row->glyphs[TEXT_AREA][i].pixel_width;
10098
10099 /* Record this as the current active region. */
10100 dpyinfo->mouse_face_beg_col = hpos;
10101 dpyinfo->mouse_face_beg_row = vpos;
10102 dpyinfo->mouse_face_beg_x = x;
10103 dpyinfo->mouse_face_beg_y = row->y;
10104 dpyinfo->mouse_face_past_end = 0;
10105
10106 dpyinfo->mouse_face_end_col = hpos + 1;
10107 dpyinfo->mouse_face_end_row = vpos;
10108 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10109 dpyinfo->mouse_face_end_y = row->y;
10110 dpyinfo->mouse_face_window = window;
10111 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10112
10113 /* Display it as active. */
10114 show_mouse_face (dpyinfo, draw);
10115 dpyinfo->mouse_face_image_state = draw;
10116 }
10117
10118 set_help_echo:
10119
10120 /* Set help_echo_string to a help string to display for this tool-bar item.
10121 XTread_socket does the rest. */
10122 help_echo_object = help_echo_window = Qnil;
10123 help_echo_pos = -1;
10124 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10125 if (NILP (help_echo_string))
10126 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10127 }
10128
10129 #endif /* HAVE_WINDOW_SYSTEM */
10130
10131
10132 \f
10133 /************************************************************************
10134 Horizontal scrolling
10135 ************************************************************************/
10136
10137 static int hscroll_window_tree P_ ((Lisp_Object));
10138 static int hscroll_windows P_ ((Lisp_Object));
10139
10140 /* For all leaf windows in the window tree rooted at WINDOW, set their
10141 hscroll value so that PT is (i) visible in the window, and (ii) so
10142 that it is not within a certain margin at the window's left and
10143 right border. Value is non-zero if any window's hscroll has been
10144 changed. */
10145
10146 static int
10147 hscroll_window_tree (window)
10148 Lisp_Object window;
10149 {
10150 int hscrolled_p = 0;
10151 int hscroll_relative_p = FLOATP (Vhscroll_step);
10152 int hscroll_step_abs = 0;
10153 double hscroll_step_rel = 0;
10154
10155 if (hscroll_relative_p)
10156 {
10157 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10158 if (hscroll_step_rel < 0)
10159 {
10160 hscroll_relative_p = 0;
10161 hscroll_step_abs = 0;
10162 }
10163 }
10164 else if (INTEGERP (Vhscroll_step))
10165 {
10166 hscroll_step_abs = XINT (Vhscroll_step);
10167 if (hscroll_step_abs < 0)
10168 hscroll_step_abs = 0;
10169 }
10170 else
10171 hscroll_step_abs = 0;
10172
10173 while (WINDOWP (window))
10174 {
10175 struct window *w = XWINDOW (window);
10176
10177 if (WINDOWP (w->hchild))
10178 hscrolled_p |= hscroll_window_tree (w->hchild);
10179 else if (WINDOWP (w->vchild))
10180 hscrolled_p |= hscroll_window_tree (w->vchild);
10181 else if (w->cursor.vpos >= 0)
10182 {
10183 int h_margin;
10184 int text_area_width;
10185 struct glyph_row *current_cursor_row
10186 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10187 struct glyph_row *desired_cursor_row
10188 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10189 struct glyph_row *cursor_row
10190 = (desired_cursor_row->enabled_p
10191 ? desired_cursor_row
10192 : current_cursor_row);
10193
10194 text_area_width = window_box_width (w, TEXT_AREA);
10195
10196 /* Scroll when cursor is inside this scroll margin. */
10197 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10198
10199 if ((XFASTINT (w->hscroll)
10200 && w->cursor.x <= h_margin)
10201 || (cursor_row->enabled_p
10202 && cursor_row->truncated_on_right_p
10203 && (w->cursor.x >= text_area_width - h_margin)))
10204 {
10205 struct it it;
10206 int hscroll;
10207 struct buffer *saved_current_buffer;
10208 int pt;
10209 int wanted_x;
10210
10211 /* Find point in a display of infinite width. */
10212 saved_current_buffer = current_buffer;
10213 current_buffer = XBUFFER (w->buffer);
10214
10215 if (w == XWINDOW (selected_window))
10216 pt = BUF_PT (current_buffer);
10217 else
10218 {
10219 pt = marker_position (w->pointm);
10220 pt = max (BEGV, pt);
10221 pt = min (ZV, pt);
10222 }
10223
10224 /* Move iterator to pt starting at cursor_row->start in
10225 a line with infinite width. */
10226 init_to_row_start (&it, w, cursor_row);
10227 it.last_visible_x = INFINITY;
10228 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10229 current_buffer = saved_current_buffer;
10230
10231 /* Position cursor in window. */
10232 if (!hscroll_relative_p && hscroll_step_abs == 0)
10233 hscroll = max (0, (it.current_x
10234 - (ITERATOR_AT_END_OF_LINE_P (&it)
10235 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10236 : (text_area_width / 2))))
10237 / FRAME_COLUMN_WIDTH (it.f);
10238 else if (w->cursor.x >= text_area_width - h_margin)
10239 {
10240 if (hscroll_relative_p)
10241 wanted_x = text_area_width * (1 - hscroll_step_rel)
10242 - h_margin;
10243 else
10244 wanted_x = text_area_width
10245 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10246 - h_margin;
10247 hscroll
10248 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10249 }
10250 else
10251 {
10252 if (hscroll_relative_p)
10253 wanted_x = text_area_width * hscroll_step_rel
10254 + h_margin;
10255 else
10256 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10257 + h_margin;
10258 hscroll
10259 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10260 }
10261 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10262
10263 /* Don't call Fset_window_hscroll if value hasn't
10264 changed because it will prevent redisplay
10265 optimizations. */
10266 if (XFASTINT (w->hscroll) != hscroll)
10267 {
10268 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10269 w->hscroll = make_number (hscroll);
10270 hscrolled_p = 1;
10271 }
10272 }
10273 }
10274
10275 window = w->next;
10276 }
10277
10278 /* Value is non-zero if hscroll of any leaf window has been changed. */
10279 return hscrolled_p;
10280 }
10281
10282
10283 /* Set hscroll so that cursor is visible and not inside horizontal
10284 scroll margins for all windows in the tree rooted at WINDOW. See
10285 also hscroll_window_tree above. Value is non-zero if any window's
10286 hscroll has been changed. If it has, desired matrices on the frame
10287 of WINDOW are cleared. */
10288
10289 static int
10290 hscroll_windows (window)
10291 Lisp_Object window;
10292 {
10293 int hscrolled_p;
10294
10295 if (automatic_hscrolling_p)
10296 {
10297 hscrolled_p = hscroll_window_tree (window);
10298 if (hscrolled_p)
10299 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10300 }
10301 else
10302 hscrolled_p = 0;
10303 return hscrolled_p;
10304 }
10305
10306
10307 \f
10308 /************************************************************************
10309 Redisplay
10310 ************************************************************************/
10311
10312 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10313 to a non-zero value. This is sometimes handy to have in a debugger
10314 session. */
10315
10316 #if GLYPH_DEBUG
10317
10318 /* First and last unchanged row for try_window_id. */
10319
10320 int debug_first_unchanged_at_end_vpos;
10321 int debug_last_unchanged_at_beg_vpos;
10322
10323 /* Delta vpos and y. */
10324
10325 int debug_dvpos, debug_dy;
10326
10327 /* Delta in characters and bytes for try_window_id. */
10328
10329 int debug_delta, debug_delta_bytes;
10330
10331 /* Values of window_end_pos and window_end_vpos at the end of
10332 try_window_id. */
10333
10334 EMACS_INT debug_end_pos, debug_end_vpos;
10335
10336 /* Append a string to W->desired_matrix->method. FMT is a printf
10337 format string. A1...A9 are a supplement for a variable-length
10338 argument list. If trace_redisplay_p is non-zero also printf the
10339 resulting string to stderr. */
10340
10341 static void
10342 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10343 struct window *w;
10344 char *fmt;
10345 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10346 {
10347 char buffer[512];
10348 char *method = w->desired_matrix->method;
10349 int len = strlen (method);
10350 int size = sizeof w->desired_matrix->method;
10351 int remaining = size - len - 1;
10352
10353 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10354 if (len && remaining)
10355 {
10356 method[len] = '|';
10357 --remaining, ++len;
10358 }
10359
10360 strncpy (method + len, buffer, remaining);
10361
10362 if (trace_redisplay_p)
10363 fprintf (stderr, "%p (%s): %s\n",
10364 w,
10365 ((BUFFERP (w->buffer)
10366 && STRINGP (XBUFFER (w->buffer)->name))
10367 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10368 : "no buffer"),
10369 buffer);
10370 }
10371
10372 #endif /* GLYPH_DEBUG */
10373
10374
10375 /* Value is non-zero if all changes in window W, which displays
10376 current_buffer, are in the text between START and END. START is a
10377 buffer position, END is given as a distance from Z. Used in
10378 redisplay_internal for display optimization. */
10379
10380 static INLINE int
10381 text_outside_line_unchanged_p (w, start, end)
10382 struct window *w;
10383 int start, end;
10384 {
10385 int unchanged_p = 1;
10386
10387 /* If text or overlays have changed, see where. */
10388 if (XFASTINT (w->last_modified) < MODIFF
10389 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10390 {
10391 /* Gap in the line? */
10392 if (GPT < start || Z - GPT < end)
10393 unchanged_p = 0;
10394
10395 /* Changes start in front of the line, or end after it? */
10396 if (unchanged_p
10397 && (BEG_UNCHANGED < start - 1
10398 || END_UNCHANGED < end))
10399 unchanged_p = 0;
10400
10401 /* If selective display, can't optimize if changes start at the
10402 beginning of the line. */
10403 if (unchanged_p
10404 && INTEGERP (current_buffer->selective_display)
10405 && XINT (current_buffer->selective_display) > 0
10406 && (BEG_UNCHANGED < start || GPT <= start))
10407 unchanged_p = 0;
10408
10409 /* If there are overlays at the start or end of the line, these
10410 may have overlay strings with newlines in them. A change at
10411 START, for instance, may actually concern the display of such
10412 overlay strings as well, and they are displayed on different
10413 lines. So, quickly rule out this case. (For the future, it
10414 might be desirable to implement something more telling than
10415 just BEG/END_UNCHANGED.) */
10416 if (unchanged_p)
10417 {
10418 if (BEG + BEG_UNCHANGED == start
10419 && overlay_touches_p (start))
10420 unchanged_p = 0;
10421 if (END_UNCHANGED == end
10422 && overlay_touches_p (Z - end))
10423 unchanged_p = 0;
10424 }
10425 }
10426
10427 return unchanged_p;
10428 }
10429
10430
10431 /* Do a frame update, taking possible shortcuts into account. This is
10432 the main external entry point for redisplay.
10433
10434 If the last redisplay displayed an echo area message and that message
10435 is no longer requested, we clear the echo area or bring back the
10436 mini-buffer if that is in use. */
10437
10438 void
10439 redisplay ()
10440 {
10441 redisplay_internal (0);
10442 }
10443
10444
10445 static Lisp_Object
10446 overlay_arrow_string_or_property (var)
10447 Lisp_Object var;
10448 {
10449 Lisp_Object val;
10450
10451 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10452 return val;
10453
10454 return Voverlay_arrow_string;
10455 }
10456
10457 /* Return 1 if there are any overlay-arrows in current_buffer. */
10458 static int
10459 overlay_arrow_in_current_buffer_p ()
10460 {
10461 Lisp_Object vlist;
10462
10463 for (vlist = Voverlay_arrow_variable_list;
10464 CONSP (vlist);
10465 vlist = XCDR (vlist))
10466 {
10467 Lisp_Object var = XCAR (vlist);
10468 Lisp_Object val;
10469
10470 if (!SYMBOLP (var))
10471 continue;
10472 val = find_symbol_value (var);
10473 if (MARKERP (val)
10474 && current_buffer == XMARKER (val)->buffer)
10475 return 1;
10476 }
10477 return 0;
10478 }
10479
10480
10481 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10482 has changed. */
10483
10484 static int
10485 overlay_arrows_changed_p ()
10486 {
10487 Lisp_Object vlist;
10488
10489 for (vlist = Voverlay_arrow_variable_list;
10490 CONSP (vlist);
10491 vlist = XCDR (vlist))
10492 {
10493 Lisp_Object var = XCAR (vlist);
10494 Lisp_Object val, pstr;
10495
10496 if (!SYMBOLP (var))
10497 continue;
10498 val = find_symbol_value (var);
10499 if (!MARKERP (val))
10500 continue;
10501 if (! EQ (COERCE_MARKER (val),
10502 Fget (var, Qlast_arrow_position))
10503 || ! (pstr = overlay_arrow_string_or_property (var),
10504 EQ (pstr, Fget (var, Qlast_arrow_string))))
10505 return 1;
10506 }
10507 return 0;
10508 }
10509
10510 /* Mark overlay arrows to be updated on next redisplay. */
10511
10512 static void
10513 update_overlay_arrows (up_to_date)
10514 int up_to_date;
10515 {
10516 Lisp_Object vlist;
10517
10518 for (vlist = Voverlay_arrow_variable_list;
10519 CONSP (vlist);
10520 vlist = XCDR (vlist))
10521 {
10522 Lisp_Object var = XCAR (vlist);
10523
10524 if (!SYMBOLP (var))
10525 continue;
10526
10527 if (up_to_date > 0)
10528 {
10529 Lisp_Object val = find_symbol_value (var);
10530 Fput (var, Qlast_arrow_position,
10531 COERCE_MARKER (val));
10532 Fput (var, Qlast_arrow_string,
10533 overlay_arrow_string_or_property (var));
10534 }
10535 else if (up_to_date < 0
10536 || !NILP (Fget (var, Qlast_arrow_position)))
10537 {
10538 Fput (var, Qlast_arrow_position, Qt);
10539 Fput (var, Qlast_arrow_string, Qt);
10540 }
10541 }
10542 }
10543
10544
10545 /* Return overlay arrow string to display at row.
10546 Return integer (bitmap number) for arrow bitmap in left fringe.
10547 Return nil if no overlay arrow. */
10548
10549 static Lisp_Object
10550 overlay_arrow_at_row (it, row)
10551 struct it *it;
10552 struct glyph_row *row;
10553 {
10554 Lisp_Object vlist;
10555
10556 for (vlist = Voverlay_arrow_variable_list;
10557 CONSP (vlist);
10558 vlist = XCDR (vlist))
10559 {
10560 Lisp_Object var = XCAR (vlist);
10561 Lisp_Object val;
10562
10563 if (!SYMBOLP (var))
10564 continue;
10565
10566 val = find_symbol_value (var);
10567
10568 if (MARKERP (val)
10569 && current_buffer == XMARKER (val)->buffer
10570 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10571 {
10572 if (FRAME_WINDOW_P (it->f)
10573 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10574 {
10575 #ifdef HAVE_WINDOW_SYSTEM
10576 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10577 {
10578 int fringe_bitmap;
10579 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10580 return make_number (fringe_bitmap);
10581 }
10582 #endif
10583 return make_number (-1); /* Use default arrow bitmap */
10584 }
10585 return overlay_arrow_string_or_property (var);
10586 }
10587 }
10588
10589 return Qnil;
10590 }
10591
10592 /* Return 1 if point moved out of or into a composition. Otherwise
10593 return 0. PREV_BUF and PREV_PT are the last point buffer and
10594 position. BUF and PT are the current point buffer and position. */
10595
10596 int
10597 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10598 struct buffer *prev_buf, *buf;
10599 int prev_pt, pt;
10600 {
10601 int start, end;
10602 Lisp_Object prop;
10603 Lisp_Object buffer;
10604
10605 XSETBUFFER (buffer, buf);
10606 /* Check a composition at the last point if point moved within the
10607 same buffer. */
10608 if (prev_buf == buf)
10609 {
10610 if (prev_pt == pt)
10611 /* Point didn't move. */
10612 return 0;
10613
10614 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10615 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10616 && COMPOSITION_VALID_P (start, end, prop)
10617 && start < prev_pt && end > prev_pt)
10618 /* The last point was within the composition. Return 1 iff
10619 point moved out of the composition. */
10620 return (pt <= start || pt >= end);
10621 }
10622
10623 /* Check a composition at the current point. */
10624 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10625 && find_composition (pt, -1, &start, &end, &prop, buffer)
10626 && COMPOSITION_VALID_P (start, end, prop)
10627 && start < pt && end > pt);
10628 }
10629
10630
10631 /* Reconsider the setting of B->clip_changed which is displayed
10632 in window W. */
10633
10634 static INLINE void
10635 reconsider_clip_changes (w, b)
10636 struct window *w;
10637 struct buffer *b;
10638 {
10639 if (b->clip_changed
10640 && !NILP (w->window_end_valid)
10641 && w->current_matrix->buffer == b
10642 && w->current_matrix->zv == BUF_ZV (b)
10643 && w->current_matrix->begv == BUF_BEGV (b))
10644 b->clip_changed = 0;
10645
10646 /* If display wasn't paused, and W is not a tool bar window, see if
10647 point has been moved into or out of a composition. In that case,
10648 we set b->clip_changed to 1 to force updating the screen. If
10649 b->clip_changed has already been set to 1, we can skip this
10650 check. */
10651 if (!b->clip_changed
10652 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10653 {
10654 int pt;
10655
10656 if (w == XWINDOW (selected_window))
10657 pt = BUF_PT (current_buffer);
10658 else
10659 pt = marker_position (w->pointm);
10660
10661 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10662 || pt != XINT (w->last_point))
10663 && check_point_in_composition (w->current_matrix->buffer,
10664 XINT (w->last_point),
10665 XBUFFER (w->buffer), pt))
10666 b->clip_changed = 1;
10667 }
10668 }
10669 \f
10670
10671 /* Select FRAME to forward the values of frame-local variables into C
10672 variables so that the redisplay routines can access those values
10673 directly. */
10674
10675 static void
10676 select_frame_for_redisplay (frame)
10677 Lisp_Object frame;
10678 {
10679 Lisp_Object tail, sym, val;
10680 Lisp_Object old = selected_frame;
10681
10682 selected_frame = frame;
10683
10684 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10685 if (CONSP (XCAR (tail))
10686 && (sym = XCAR (XCAR (tail)),
10687 SYMBOLP (sym))
10688 && (sym = indirect_variable (sym),
10689 val = SYMBOL_VALUE (sym),
10690 (BUFFER_LOCAL_VALUEP (val)
10691 || SOME_BUFFER_LOCAL_VALUEP (val)))
10692 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10693 /* Use find_symbol_value rather than Fsymbol_value
10694 to avoid an error if it is void. */
10695 find_symbol_value (sym);
10696
10697 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10698 if (CONSP (XCAR (tail))
10699 && (sym = XCAR (XCAR (tail)),
10700 SYMBOLP (sym))
10701 && (sym = indirect_variable (sym),
10702 val = SYMBOL_VALUE (sym),
10703 (BUFFER_LOCAL_VALUEP (val)
10704 || SOME_BUFFER_LOCAL_VALUEP (val)))
10705 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10706 find_symbol_value (sym);
10707 }
10708
10709
10710 #define STOP_POLLING \
10711 do { if (! polling_stopped_here) stop_polling (); \
10712 polling_stopped_here = 1; } while (0)
10713
10714 #define RESUME_POLLING \
10715 do { if (polling_stopped_here) start_polling (); \
10716 polling_stopped_here = 0; } while (0)
10717
10718
10719 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10720 response to any user action; therefore, we should preserve the echo
10721 area. (Actually, our caller does that job.) Perhaps in the future
10722 avoid recentering windows if it is not necessary; currently that
10723 causes some problems. */
10724
10725 static void
10726 redisplay_internal (preserve_echo_area)
10727 int preserve_echo_area;
10728 {
10729 struct window *w = XWINDOW (selected_window);
10730 struct frame *f = XFRAME (w->frame);
10731 int pause;
10732 int must_finish = 0;
10733 struct text_pos tlbufpos, tlendpos;
10734 int number_of_visible_frames;
10735 int count;
10736 struct frame *sf = SELECTED_FRAME ();
10737 int polling_stopped_here = 0;
10738
10739 /* Non-zero means redisplay has to consider all windows on all
10740 frames. Zero means, only selected_window is considered. */
10741 int consider_all_windows_p;
10742
10743 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10744
10745 /* No redisplay if running in batch mode or frame is not yet fully
10746 initialized, or redisplay is explicitly turned off by setting
10747 Vinhibit_redisplay. */
10748 if (noninteractive
10749 || !NILP (Vinhibit_redisplay)
10750 || !f->glyphs_initialized_p)
10751 return;
10752
10753 /* The flag redisplay_performed_directly_p is set by
10754 direct_output_for_insert when it already did the whole screen
10755 update necessary. */
10756 if (redisplay_performed_directly_p)
10757 {
10758 redisplay_performed_directly_p = 0;
10759 if (!hscroll_windows (selected_window))
10760 return;
10761 }
10762
10763 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10764 if (popup_activated ())
10765 return;
10766 #endif
10767
10768 /* I don't think this happens but let's be paranoid. */
10769 if (redisplaying_p)
10770 return;
10771
10772 /* Record a function that resets redisplaying_p to its old value
10773 when we leave this function. */
10774 count = SPECPDL_INDEX ();
10775 record_unwind_protect (unwind_redisplay,
10776 Fcons (make_number (redisplaying_p), selected_frame));
10777 ++redisplaying_p;
10778 specbind (Qinhibit_free_realized_faces, Qnil);
10779
10780 {
10781 Lisp_Object tail, frame;
10782
10783 FOR_EACH_FRAME (tail, frame)
10784 {
10785 struct frame *f = XFRAME (frame);
10786 f->already_hscrolled_p = 0;
10787 }
10788 }
10789
10790 retry:
10791 pause = 0;
10792 reconsider_clip_changes (w, current_buffer);
10793 last_escape_glyph_frame = NULL;
10794 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10795
10796 /* If new fonts have been loaded that make a glyph matrix adjustment
10797 necessary, do it. */
10798 if (fonts_changed_p)
10799 {
10800 adjust_glyphs (NULL);
10801 ++windows_or_buffers_changed;
10802 fonts_changed_p = 0;
10803 }
10804
10805 /* If face_change_count is non-zero, init_iterator will free all
10806 realized faces, which includes the faces referenced from current
10807 matrices. So, we can't reuse current matrices in this case. */
10808 if (face_change_count)
10809 ++windows_or_buffers_changed;
10810
10811 if (! FRAME_WINDOW_P (sf)
10812 && previous_terminal_frame != sf)
10813 {
10814 /* Since frames on an ASCII terminal share the same display
10815 area, displaying a different frame means redisplay the whole
10816 thing. */
10817 windows_or_buffers_changed++;
10818 SET_FRAME_GARBAGED (sf);
10819 XSETFRAME (Vterminal_frame, sf);
10820 }
10821 previous_terminal_frame = sf;
10822
10823 /* Set the visible flags for all frames. Do this before checking
10824 for resized or garbaged frames; they want to know if their frames
10825 are visible. See the comment in frame.h for
10826 FRAME_SAMPLE_VISIBILITY. */
10827 {
10828 Lisp_Object tail, frame;
10829
10830 number_of_visible_frames = 0;
10831
10832 FOR_EACH_FRAME (tail, frame)
10833 {
10834 struct frame *f = XFRAME (frame);
10835
10836 FRAME_SAMPLE_VISIBILITY (f);
10837 if (FRAME_VISIBLE_P (f))
10838 ++number_of_visible_frames;
10839 clear_desired_matrices (f);
10840 }
10841 }
10842
10843 /* Notice any pending interrupt request to change frame size. */
10844 do_pending_window_change (1);
10845
10846 /* Clear frames marked as garbaged. */
10847 if (frame_garbaged)
10848 clear_garbaged_frames ();
10849
10850 /* Build menubar and tool-bar items. */
10851 if (NILP (Vmemory_full))
10852 prepare_menu_bars ();
10853
10854 if (windows_or_buffers_changed)
10855 update_mode_lines++;
10856
10857 /* Detect case that we need to write or remove a star in the mode line. */
10858 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10859 {
10860 w->update_mode_line = Qt;
10861 if (buffer_shared > 1)
10862 update_mode_lines++;
10863 }
10864
10865 /* If %c is in the mode line, update it if needed. */
10866 if (!NILP (w->column_number_displayed)
10867 /* This alternative quickly identifies a common case
10868 where no change is needed. */
10869 && !(PT == XFASTINT (w->last_point)
10870 && XFASTINT (w->last_modified) >= MODIFF
10871 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10872 && (XFASTINT (w->column_number_displayed)
10873 != (int) current_column ())) /* iftc */
10874 w->update_mode_line = Qt;
10875
10876 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10877
10878 /* The variable buffer_shared is set in redisplay_window and
10879 indicates that we redisplay a buffer in different windows. See
10880 there. */
10881 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10882 || cursor_type_changed);
10883
10884 /* If specs for an arrow have changed, do thorough redisplay
10885 to ensure we remove any arrow that should no longer exist. */
10886 if (overlay_arrows_changed_p ())
10887 consider_all_windows_p = windows_or_buffers_changed = 1;
10888
10889 /* Normally the message* functions will have already displayed and
10890 updated the echo area, but the frame may have been trashed, or
10891 the update may have been preempted, so display the echo area
10892 again here. Checking message_cleared_p captures the case that
10893 the echo area should be cleared. */
10894 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10895 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10896 || (message_cleared_p
10897 && minibuf_level == 0
10898 /* If the mini-window is currently selected, this means the
10899 echo-area doesn't show through. */
10900 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10901 {
10902 int window_height_changed_p = echo_area_display (0);
10903 must_finish = 1;
10904
10905 /* If we don't display the current message, don't clear the
10906 message_cleared_p flag, because, if we did, we wouldn't clear
10907 the echo area in the next redisplay which doesn't preserve
10908 the echo area. */
10909 if (!display_last_displayed_message_p)
10910 message_cleared_p = 0;
10911
10912 if (fonts_changed_p)
10913 goto retry;
10914 else if (window_height_changed_p)
10915 {
10916 consider_all_windows_p = 1;
10917 ++update_mode_lines;
10918 ++windows_or_buffers_changed;
10919
10920 /* If window configuration was changed, frames may have been
10921 marked garbaged. Clear them or we will experience
10922 surprises wrt scrolling. */
10923 if (frame_garbaged)
10924 clear_garbaged_frames ();
10925 }
10926 }
10927 else if (EQ (selected_window, minibuf_window)
10928 && (current_buffer->clip_changed
10929 || XFASTINT (w->last_modified) < MODIFF
10930 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10931 && resize_mini_window (w, 0))
10932 {
10933 /* Resized active mini-window to fit the size of what it is
10934 showing if its contents might have changed. */
10935 must_finish = 1;
10936 consider_all_windows_p = 1;
10937 ++windows_or_buffers_changed;
10938 ++update_mode_lines;
10939
10940 /* If window configuration was changed, frames may have been
10941 marked garbaged. Clear them or we will experience
10942 surprises wrt scrolling. */
10943 if (frame_garbaged)
10944 clear_garbaged_frames ();
10945 }
10946
10947
10948 /* If showing the region, and mark has changed, we must redisplay
10949 the whole window. The assignment to this_line_start_pos prevents
10950 the optimization directly below this if-statement. */
10951 if (((!NILP (Vtransient_mark_mode)
10952 && !NILP (XBUFFER (w->buffer)->mark_active))
10953 != !NILP (w->region_showing))
10954 || (!NILP (w->region_showing)
10955 && !EQ (w->region_showing,
10956 Fmarker_position (XBUFFER (w->buffer)->mark))))
10957 CHARPOS (this_line_start_pos) = 0;
10958
10959 /* Optimize the case that only the line containing the cursor in the
10960 selected window has changed. Variables starting with this_ are
10961 set in display_line and record information about the line
10962 containing the cursor. */
10963 tlbufpos = this_line_start_pos;
10964 tlendpos = this_line_end_pos;
10965 if (!consider_all_windows_p
10966 && CHARPOS (tlbufpos) > 0
10967 && NILP (w->update_mode_line)
10968 && !current_buffer->clip_changed
10969 && !current_buffer->prevent_redisplay_optimizations_p
10970 && FRAME_VISIBLE_P (XFRAME (w->frame))
10971 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10972 /* Make sure recorded data applies to current buffer, etc. */
10973 && this_line_buffer == current_buffer
10974 && current_buffer == XBUFFER (w->buffer)
10975 && NILP (w->force_start)
10976 && NILP (w->optional_new_start)
10977 /* Point must be on the line that we have info recorded about. */
10978 && PT >= CHARPOS (tlbufpos)
10979 && PT <= Z - CHARPOS (tlendpos)
10980 /* All text outside that line, including its final newline,
10981 must be unchanged */
10982 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10983 CHARPOS (tlendpos)))
10984 {
10985 if (CHARPOS (tlbufpos) > BEGV
10986 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10987 && (CHARPOS (tlbufpos) == ZV
10988 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10989 /* Former continuation line has disappeared by becoming empty */
10990 goto cancel;
10991 else if (XFASTINT (w->last_modified) < MODIFF
10992 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10993 || MINI_WINDOW_P (w))
10994 {
10995 /* We have to handle the case of continuation around a
10996 wide-column character (See the comment in indent.c around
10997 line 885).
10998
10999 For instance, in the following case:
11000
11001 -------- Insert --------
11002 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11003 J_I_ ==> J_I_ `^^' are cursors.
11004 ^^ ^^
11005 -------- --------
11006
11007 As we have to redraw the line above, we should goto cancel. */
11008
11009 struct it it;
11010 int line_height_before = this_line_pixel_height;
11011
11012 /* Note that start_display will handle the case that the
11013 line starting at tlbufpos is a continuation lines. */
11014 start_display (&it, w, tlbufpos);
11015
11016 /* Implementation note: It this still necessary? */
11017 if (it.current_x != this_line_start_x)
11018 goto cancel;
11019
11020 TRACE ((stderr, "trying display optimization 1\n"));
11021 w->cursor.vpos = -1;
11022 overlay_arrow_seen = 0;
11023 it.vpos = this_line_vpos;
11024 it.current_y = this_line_y;
11025 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11026 display_line (&it);
11027
11028 /* If line contains point, is not continued,
11029 and ends at same distance from eob as before, we win */
11030 if (w->cursor.vpos >= 0
11031 /* Line is not continued, otherwise this_line_start_pos
11032 would have been set to 0 in display_line. */
11033 && CHARPOS (this_line_start_pos)
11034 /* Line ends as before. */
11035 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11036 /* Line has same height as before. Otherwise other lines
11037 would have to be shifted up or down. */
11038 && this_line_pixel_height == line_height_before)
11039 {
11040 /* If this is not the window's last line, we must adjust
11041 the charstarts of the lines below. */
11042 if (it.current_y < it.last_visible_y)
11043 {
11044 struct glyph_row *row
11045 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11046 int delta, delta_bytes;
11047
11048 if (Z - CHARPOS (tlendpos) == ZV)
11049 {
11050 /* This line ends at end of (accessible part of)
11051 buffer. There is no newline to count. */
11052 delta = (Z
11053 - CHARPOS (tlendpos)
11054 - MATRIX_ROW_START_CHARPOS (row));
11055 delta_bytes = (Z_BYTE
11056 - BYTEPOS (tlendpos)
11057 - MATRIX_ROW_START_BYTEPOS (row));
11058 }
11059 else
11060 {
11061 /* This line ends in a newline. Must take
11062 account of the newline and the rest of the
11063 text that follows. */
11064 delta = (Z
11065 - CHARPOS (tlendpos)
11066 - MATRIX_ROW_START_CHARPOS (row));
11067 delta_bytes = (Z_BYTE
11068 - BYTEPOS (tlendpos)
11069 - MATRIX_ROW_START_BYTEPOS (row));
11070 }
11071
11072 increment_matrix_positions (w->current_matrix,
11073 this_line_vpos + 1,
11074 w->current_matrix->nrows,
11075 delta, delta_bytes);
11076 }
11077
11078 /* If this row displays text now but previously didn't,
11079 or vice versa, w->window_end_vpos may have to be
11080 adjusted. */
11081 if ((it.glyph_row - 1)->displays_text_p)
11082 {
11083 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11084 XSETINT (w->window_end_vpos, this_line_vpos);
11085 }
11086 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11087 && this_line_vpos > 0)
11088 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11089 w->window_end_valid = Qnil;
11090
11091 /* Update hint: No need to try to scroll in update_window. */
11092 w->desired_matrix->no_scrolling_p = 1;
11093
11094 #if GLYPH_DEBUG
11095 *w->desired_matrix->method = 0;
11096 debug_method_add (w, "optimization 1");
11097 #endif
11098 #ifdef HAVE_WINDOW_SYSTEM
11099 update_window_fringes (w, 0);
11100 #endif
11101 goto update;
11102 }
11103 else
11104 goto cancel;
11105 }
11106 else if (/* Cursor position hasn't changed. */
11107 PT == XFASTINT (w->last_point)
11108 /* Make sure the cursor was last displayed
11109 in this window. Otherwise we have to reposition it. */
11110 && 0 <= w->cursor.vpos
11111 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11112 {
11113 if (!must_finish)
11114 {
11115 do_pending_window_change (1);
11116
11117 /* We used to always goto end_of_redisplay here, but this
11118 isn't enough if we have a blinking cursor. */
11119 if (w->cursor_off_p == w->last_cursor_off_p)
11120 goto end_of_redisplay;
11121 }
11122 goto update;
11123 }
11124 /* If highlighting the region, or if the cursor is in the echo area,
11125 then we can't just move the cursor. */
11126 else if (! (!NILP (Vtransient_mark_mode)
11127 && !NILP (current_buffer->mark_active))
11128 && (EQ (selected_window, current_buffer->last_selected_window)
11129 || highlight_nonselected_windows)
11130 && NILP (w->region_showing)
11131 && NILP (Vshow_trailing_whitespace)
11132 && !cursor_in_echo_area)
11133 {
11134 struct it it;
11135 struct glyph_row *row;
11136
11137 /* Skip from tlbufpos to PT and see where it is. Note that
11138 PT may be in invisible text. If so, we will end at the
11139 next visible position. */
11140 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11141 NULL, DEFAULT_FACE_ID);
11142 it.current_x = this_line_start_x;
11143 it.current_y = this_line_y;
11144 it.vpos = this_line_vpos;
11145
11146 /* The call to move_it_to stops in front of PT, but
11147 moves over before-strings. */
11148 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11149
11150 if (it.vpos == this_line_vpos
11151 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11152 row->enabled_p))
11153 {
11154 xassert (this_line_vpos == it.vpos);
11155 xassert (this_line_y == it.current_y);
11156 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11157 #if GLYPH_DEBUG
11158 *w->desired_matrix->method = 0;
11159 debug_method_add (w, "optimization 3");
11160 #endif
11161 goto update;
11162 }
11163 else
11164 goto cancel;
11165 }
11166
11167 cancel:
11168 /* Text changed drastically or point moved off of line. */
11169 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11170 }
11171
11172 CHARPOS (this_line_start_pos) = 0;
11173 consider_all_windows_p |= buffer_shared > 1;
11174 ++clear_face_cache_count;
11175 #ifdef HAVE_WINDOW_SYSTEM
11176 ++clear_image_cache_count;
11177 #endif
11178
11179 /* Build desired matrices, and update the display. If
11180 consider_all_windows_p is non-zero, do it for all windows on all
11181 frames. Otherwise do it for selected_window, only. */
11182
11183 if (consider_all_windows_p)
11184 {
11185 Lisp_Object tail, frame;
11186
11187 FOR_EACH_FRAME (tail, frame)
11188 XFRAME (frame)->updated_p = 0;
11189
11190 /* Recompute # windows showing selected buffer. This will be
11191 incremented each time such a window is displayed. */
11192 buffer_shared = 0;
11193
11194 FOR_EACH_FRAME (tail, frame)
11195 {
11196 struct frame *f = XFRAME (frame);
11197
11198 if (FRAME_WINDOW_P (f) || f == sf)
11199 {
11200 if (! EQ (frame, selected_frame))
11201 /* Select the frame, for the sake of frame-local
11202 variables. */
11203 select_frame_for_redisplay (frame);
11204
11205 /* Mark all the scroll bars to be removed; we'll redeem
11206 the ones we want when we redisplay their windows. */
11207 if (condemn_scroll_bars_hook)
11208 condemn_scroll_bars_hook (f);
11209
11210 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11211 redisplay_windows (FRAME_ROOT_WINDOW (f));
11212
11213 /* Any scroll bars which redisplay_windows should have
11214 nuked should now go away. */
11215 if (judge_scroll_bars_hook)
11216 judge_scroll_bars_hook (f);
11217
11218 /* If fonts changed, display again. */
11219 /* ??? rms: I suspect it is a mistake to jump all the way
11220 back to retry here. It should just retry this frame. */
11221 if (fonts_changed_p)
11222 goto retry;
11223
11224 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11225 {
11226 /* See if we have to hscroll. */
11227 if (!f->already_hscrolled_p)
11228 {
11229 f->already_hscrolled_p = 1;
11230 if (hscroll_windows (f->root_window))
11231 goto retry;
11232 }
11233
11234 /* Prevent various kinds of signals during display
11235 update. stdio is not robust about handling
11236 signals, which can cause an apparent I/O
11237 error. */
11238 if (interrupt_input)
11239 unrequest_sigio ();
11240 STOP_POLLING;
11241
11242 /* Update the display. */
11243 set_window_update_flags (XWINDOW (f->root_window), 1);
11244 pause |= update_frame (f, 0, 0);
11245 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11246 if (pause)
11247 break;
11248 #endif
11249
11250 f->updated_p = 1;
11251 }
11252 }
11253 }
11254
11255 if (!pause)
11256 {
11257 /* Do the mark_window_display_accurate after all windows have
11258 been redisplayed because this call resets flags in buffers
11259 which are needed for proper redisplay. */
11260 FOR_EACH_FRAME (tail, frame)
11261 {
11262 struct frame *f = XFRAME (frame);
11263 if (f->updated_p)
11264 {
11265 mark_window_display_accurate (f->root_window, 1);
11266 if (frame_up_to_date_hook)
11267 frame_up_to_date_hook (f);
11268 }
11269 }
11270 }
11271 }
11272 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11273 {
11274 Lisp_Object mini_window;
11275 struct frame *mini_frame;
11276
11277 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11278 /* Use list_of_error, not Qerror, so that
11279 we catch only errors and don't run the debugger. */
11280 internal_condition_case_1 (redisplay_window_1, selected_window,
11281 list_of_error,
11282 redisplay_window_error);
11283
11284 /* Compare desired and current matrices, perform output. */
11285
11286 update:
11287 /* If fonts changed, display again. */
11288 if (fonts_changed_p)
11289 goto retry;
11290
11291 /* Prevent various kinds of signals during display update.
11292 stdio is not robust about handling signals,
11293 which can cause an apparent I/O error. */
11294 if (interrupt_input)
11295 unrequest_sigio ();
11296 STOP_POLLING;
11297
11298 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11299 {
11300 if (hscroll_windows (selected_window))
11301 goto retry;
11302
11303 XWINDOW (selected_window)->must_be_updated_p = 1;
11304 pause = update_frame (sf, 0, 0);
11305 }
11306
11307 /* We may have called echo_area_display at the top of this
11308 function. If the echo area is on another frame, that may
11309 have put text on a frame other than the selected one, so the
11310 above call to update_frame would not have caught it. Catch
11311 it here. */
11312 mini_window = FRAME_MINIBUF_WINDOW (sf);
11313 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11314
11315 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11316 {
11317 XWINDOW (mini_window)->must_be_updated_p = 1;
11318 pause |= update_frame (mini_frame, 0, 0);
11319 if (!pause && hscroll_windows (mini_window))
11320 goto retry;
11321 }
11322 }
11323
11324 /* If display was paused because of pending input, make sure we do a
11325 thorough update the next time. */
11326 if (pause)
11327 {
11328 /* Prevent the optimization at the beginning of
11329 redisplay_internal that tries a single-line update of the
11330 line containing the cursor in the selected window. */
11331 CHARPOS (this_line_start_pos) = 0;
11332
11333 /* Let the overlay arrow be updated the next time. */
11334 update_overlay_arrows (0);
11335
11336 /* If we pause after scrolling, some rows in the current
11337 matrices of some windows are not valid. */
11338 if (!WINDOW_FULL_WIDTH_P (w)
11339 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11340 update_mode_lines = 1;
11341 }
11342 else
11343 {
11344 if (!consider_all_windows_p)
11345 {
11346 /* This has already been done above if
11347 consider_all_windows_p is set. */
11348 mark_window_display_accurate_1 (w, 1);
11349
11350 /* Say overlay arrows are up to date. */
11351 update_overlay_arrows (1);
11352
11353 if (frame_up_to_date_hook != 0)
11354 frame_up_to_date_hook (sf);
11355 }
11356
11357 update_mode_lines = 0;
11358 windows_or_buffers_changed = 0;
11359 cursor_type_changed = 0;
11360 }
11361
11362 /* Start SIGIO interrupts coming again. Having them off during the
11363 code above makes it less likely one will discard output, but not
11364 impossible, since there might be stuff in the system buffer here.
11365 But it is much hairier to try to do anything about that. */
11366 if (interrupt_input)
11367 request_sigio ();
11368 RESUME_POLLING;
11369
11370 /* If a frame has become visible which was not before, redisplay
11371 again, so that we display it. Expose events for such a frame
11372 (which it gets when becoming visible) don't call the parts of
11373 redisplay constructing glyphs, so simply exposing a frame won't
11374 display anything in this case. So, we have to display these
11375 frames here explicitly. */
11376 if (!pause)
11377 {
11378 Lisp_Object tail, frame;
11379 int new_count = 0;
11380
11381 FOR_EACH_FRAME (tail, frame)
11382 {
11383 int this_is_visible = 0;
11384
11385 if (XFRAME (frame)->visible)
11386 this_is_visible = 1;
11387 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11388 if (XFRAME (frame)->visible)
11389 this_is_visible = 1;
11390
11391 if (this_is_visible)
11392 new_count++;
11393 }
11394
11395 if (new_count != number_of_visible_frames)
11396 windows_or_buffers_changed++;
11397 }
11398
11399 /* Change frame size now if a change is pending. */
11400 do_pending_window_change (1);
11401
11402 /* If we just did a pending size change, or have additional
11403 visible frames, redisplay again. */
11404 if (windows_or_buffers_changed && !pause)
11405 goto retry;
11406
11407 /* Clear the face cache eventually. */
11408 if (consider_all_windows_p)
11409 {
11410 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11411 {
11412 clear_face_cache (0);
11413 clear_face_cache_count = 0;
11414 }
11415 #ifdef HAVE_WINDOW_SYSTEM
11416 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11417 {
11418 Lisp_Object tail, frame;
11419 FOR_EACH_FRAME (tail, frame)
11420 {
11421 struct frame *f = XFRAME (frame);
11422 if (FRAME_WINDOW_P (f))
11423 clear_image_cache (f, 0);
11424 }
11425 clear_image_cache_count = 0;
11426 }
11427 #endif /* HAVE_WINDOW_SYSTEM */
11428 }
11429
11430 end_of_redisplay:
11431 unbind_to (count, Qnil);
11432 RESUME_POLLING;
11433 }
11434
11435
11436 /* Redisplay, but leave alone any recent echo area message unless
11437 another message has been requested in its place.
11438
11439 This is useful in situations where you need to redisplay but no
11440 user action has occurred, making it inappropriate for the message
11441 area to be cleared. See tracking_off and
11442 wait_reading_process_output for examples of these situations.
11443
11444 FROM_WHERE is an integer saying from where this function was
11445 called. This is useful for debugging. */
11446
11447 void
11448 redisplay_preserve_echo_area (from_where)
11449 int from_where;
11450 {
11451 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11452
11453 if (!NILP (echo_area_buffer[1]))
11454 {
11455 /* We have a previously displayed message, but no current
11456 message. Redisplay the previous message. */
11457 display_last_displayed_message_p = 1;
11458 redisplay_internal (1);
11459 display_last_displayed_message_p = 0;
11460 }
11461 else
11462 redisplay_internal (1);
11463
11464 if (rif != NULL && rif->flush_display_optional)
11465 rif->flush_display_optional (NULL);
11466 }
11467
11468
11469 /* Function registered with record_unwind_protect in
11470 redisplay_internal. Reset redisplaying_p to the value it had
11471 before redisplay_internal was called, and clear
11472 prevent_freeing_realized_faces_p. It also selects the previously
11473 selected frame. */
11474
11475 static Lisp_Object
11476 unwind_redisplay (val)
11477 Lisp_Object val;
11478 {
11479 Lisp_Object old_redisplaying_p, old_frame;
11480
11481 old_redisplaying_p = XCAR (val);
11482 redisplaying_p = XFASTINT (old_redisplaying_p);
11483 old_frame = XCDR (val);
11484 if (! EQ (old_frame, selected_frame))
11485 select_frame_for_redisplay (old_frame);
11486 return Qnil;
11487 }
11488
11489
11490 /* Mark the display of window W as accurate or inaccurate. If
11491 ACCURATE_P is non-zero mark display of W as accurate. If
11492 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11493 redisplay_internal is called. */
11494
11495 static void
11496 mark_window_display_accurate_1 (w, accurate_p)
11497 struct window *w;
11498 int accurate_p;
11499 {
11500 if (BUFFERP (w->buffer))
11501 {
11502 struct buffer *b = XBUFFER (w->buffer);
11503
11504 w->last_modified
11505 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11506 w->last_overlay_modified
11507 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11508 w->last_had_star
11509 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11510
11511 if (accurate_p)
11512 {
11513 b->clip_changed = 0;
11514 b->prevent_redisplay_optimizations_p = 0;
11515
11516 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11517 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11518 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11519 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11520
11521 w->current_matrix->buffer = b;
11522 w->current_matrix->begv = BUF_BEGV (b);
11523 w->current_matrix->zv = BUF_ZV (b);
11524
11525 w->last_cursor = w->cursor;
11526 w->last_cursor_off_p = w->cursor_off_p;
11527
11528 if (w == XWINDOW (selected_window))
11529 w->last_point = make_number (BUF_PT (b));
11530 else
11531 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11532 }
11533 }
11534
11535 if (accurate_p)
11536 {
11537 w->window_end_valid = w->buffer;
11538 #if 0 /* This is incorrect with variable-height lines. */
11539 xassert (XINT (w->window_end_vpos)
11540 < (WINDOW_TOTAL_LINES (w)
11541 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11542 #endif
11543 w->update_mode_line = Qnil;
11544 }
11545 }
11546
11547
11548 /* Mark the display of windows in the window tree rooted at WINDOW as
11549 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11550 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11551 be redisplayed the next time redisplay_internal is called. */
11552
11553 void
11554 mark_window_display_accurate (window, accurate_p)
11555 Lisp_Object window;
11556 int accurate_p;
11557 {
11558 struct window *w;
11559
11560 for (; !NILP (window); window = w->next)
11561 {
11562 w = XWINDOW (window);
11563 mark_window_display_accurate_1 (w, accurate_p);
11564
11565 if (!NILP (w->vchild))
11566 mark_window_display_accurate (w->vchild, accurate_p);
11567 if (!NILP (w->hchild))
11568 mark_window_display_accurate (w->hchild, accurate_p);
11569 }
11570
11571 if (accurate_p)
11572 {
11573 update_overlay_arrows (1);
11574 }
11575 else
11576 {
11577 /* Force a thorough redisplay the next time by setting
11578 last_arrow_position and last_arrow_string to t, which is
11579 unequal to any useful value of Voverlay_arrow_... */
11580 update_overlay_arrows (-1);
11581 }
11582 }
11583
11584
11585 /* Return value in display table DP (Lisp_Char_Table *) for character
11586 C. Since a display table doesn't have any parent, we don't have to
11587 follow parent. Do not call this function directly but use the
11588 macro DISP_CHAR_VECTOR. */
11589
11590 Lisp_Object
11591 disp_char_vector (dp, c)
11592 struct Lisp_Char_Table *dp;
11593 int c;
11594 {
11595 int code[4], i;
11596 Lisp_Object val;
11597
11598 if (SINGLE_BYTE_CHAR_P (c))
11599 return (dp->contents[c]);
11600
11601 SPLIT_CHAR (c, code[0], code[1], code[2]);
11602 if (code[1] < 32)
11603 code[1] = -1;
11604 else if (code[2] < 32)
11605 code[2] = -1;
11606
11607 /* Here, the possible range of code[0] (== charset ID) is
11608 128..max_charset. Since the top level char table contains data
11609 for multibyte characters after 256th element, we must increment
11610 code[0] by 128 to get a correct index. */
11611 code[0] += 128;
11612 code[3] = -1; /* anchor */
11613
11614 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11615 {
11616 val = dp->contents[code[i]];
11617 if (!SUB_CHAR_TABLE_P (val))
11618 return (NILP (val) ? dp->defalt : val);
11619 }
11620
11621 /* Here, val is a sub char table. We return the default value of
11622 it. */
11623 return (dp->defalt);
11624 }
11625
11626
11627 \f
11628 /***********************************************************************
11629 Window Redisplay
11630 ***********************************************************************/
11631
11632 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11633
11634 static void
11635 redisplay_windows (window)
11636 Lisp_Object window;
11637 {
11638 while (!NILP (window))
11639 {
11640 struct window *w = XWINDOW (window);
11641
11642 if (!NILP (w->hchild))
11643 redisplay_windows (w->hchild);
11644 else if (!NILP (w->vchild))
11645 redisplay_windows (w->vchild);
11646 else
11647 {
11648 displayed_buffer = XBUFFER (w->buffer);
11649 /* Use list_of_error, not Qerror, so that
11650 we catch only errors and don't run the debugger. */
11651 internal_condition_case_1 (redisplay_window_0, window,
11652 list_of_error,
11653 redisplay_window_error);
11654 }
11655
11656 window = w->next;
11657 }
11658 }
11659
11660 static Lisp_Object
11661 redisplay_window_error ()
11662 {
11663 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11664 return Qnil;
11665 }
11666
11667 static Lisp_Object
11668 redisplay_window_0 (window)
11669 Lisp_Object window;
11670 {
11671 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11672 redisplay_window (window, 0);
11673 return Qnil;
11674 }
11675
11676 static Lisp_Object
11677 redisplay_window_1 (window)
11678 Lisp_Object window;
11679 {
11680 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11681 redisplay_window (window, 1);
11682 return Qnil;
11683 }
11684 \f
11685
11686 /* Increment GLYPH until it reaches END or CONDITION fails while
11687 adding (GLYPH)->pixel_width to X. */
11688
11689 #define SKIP_GLYPHS(glyph, end, x, condition) \
11690 do \
11691 { \
11692 (x) += (glyph)->pixel_width; \
11693 ++(glyph); \
11694 } \
11695 while ((glyph) < (end) && (condition))
11696
11697
11698 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11699 DELTA is the number of bytes by which positions recorded in ROW
11700 differ from current buffer positions.
11701
11702 Return 0 if cursor is not on this row. 1 otherwise. */
11703
11704 int
11705 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11706 struct window *w;
11707 struct glyph_row *row;
11708 struct glyph_matrix *matrix;
11709 int delta, delta_bytes, dy, dvpos;
11710 {
11711 struct glyph *glyph = row->glyphs[TEXT_AREA];
11712 struct glyph *end = glyph + row->used[TEXT_AREA];
11713 struct glyph *cursor = NULL;
11714 /* The first glyph that starts a sequence of glyphs from string. */
11715 struct glyph *string_start;
11716 /* The X coordinate of string_start. */
11717 int string_start_x;
11718 /* The last known character position. */
11719 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11720 /* The last known character position before string_start. */
11721 int string_before_pos;
11722 int x = row->x;
11723 int cursor_x = x;
11724 int cursor_from_overlay_pos = 0;
11725 int pt_old = PT - delta;
11726
11727 /* Skip over glyphs not having an object at the start of the row.
11728 These are special glyphs like truncation marks on terminal
11729 frames. */
11730 if (row->displays_text_p)
11731 while (glyph < end
11732 && INTEGERP (glyph->object)
11733 && glyph->charpos < 0)
11734 {
11735 x += glyph->pixel_width;
11736 ++glyph;
11737 }
11738
11739 string_start = NULL;
11740 while (glyph < end
11741 && !INTEGERP (glyph->object)
11742 && (!BUFFERP (glyph->object)
11743 || (last_pos = glyph->charpos) < pt_old))
11744 {
11745 if (! STRINGP (glyph->object))
11746 {
11747 string_start = NULL;
11748 x += glyph->pixel_width;
11749 ++glyph;
11750 if (cursor_from_overlay_pos
11751 && last_pos >= cursor_from_overlay_pos)
11752 {
11753 cursor_from_overlay_pos = 0;
11754 cursor = 0;
11755 }
11756 }
11757 else
11758 {
11759 if (string_start == NULL)
11760 {
11761 string_before_pos = last_pos;
11762 string_start = glyph;
11763 string_start_x = x;
11764 }
11765 /* Skip all glyphs from string. */
11766 do
11767 {
11768 Lisp_Object cprop;
11769 int pos;
11770 if ((cursor == NULL || glyph > cursor)
11771 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11772 Qcursor, (glyph)->object),
11773 !NILP (cprop))
11774 && (pos = string_buffer_position (w, glyph->object,
11775 string_before_pos),
11776 (pos == 0 /* From overlay */
11777 || pos == pt_old)))
11778 {
11779 /* Estimate overlay buffer position from the buffer
11780 positions of the glyphs before and after the overlay.
11781 Add 1 to last_pos so that if point corresponds to the
11782 glyph right after the overlay, we still use a 'cursor'
11783 property found in that overlay. */
11784 cursor_from_overlay_pos = (pos ? 0 : last_pos
11785 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11786 cursor = glyph;
11787 cursor_x = x;
11788 }
11789 x += glyph->pixel_width;
11790 ++glyph;
11791 }
11792 while (glyph < end && EQ (glyph->object, string_start->object));
11793 }
11794 }
11795
11796 if (cursor != NULL)
11797 {
11798 glyph = cursor;
11799 x = cursor_x;
11800 }
11801 else if (row->ends_in_ellipsis_p && glyph == end)
11802 {
11803 /* Scan back over the ellipsis glyphs, decrementing positions. */
11804 while (glyph > row->glyphs[TEXT_AREA]
11805 && (glyph - 1)->charpos == last_pos)
11806 glyph--, x -= glyph->pixel_width;
11807 /* That loop always goes one position too far,
11808 including the glyph before the ellipsis.
11809 So scan forward over that one. */
11810 x += glyph->pixel_width;
11811 glyph++;
11812 }
11813 else if (string_start
11814 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11815 {
11816 /* We may have skipped over point because the previous glyphs
11817 are from string. As there's no easy way to know the
11818 character position of the current glyph, find the correct
11819 glyph on point by scanning from string_start again. */
11820 Lisp_Object limit;
11821 Lisp_Object string;
11822 struct glyph *stop = glyph;
11823 int pos;
11824
11825 limit = make_number (pt_old + 1);
11826 glyph = string_start;
11827 x = string_start_x;
11828 string = glyph->object;
11829 pos = string_buffer_position (w, string, string_before_pos);
11830 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11831 because we always put cursor after overlay strings. */
11832 while (pos == 0 && glyph < stop)
11833 {
11834 string = glyph->object;
11835 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11836 if (glyph < stop)
11837 pos = string_buffer_position (w, glyph->object, string_before_pos);
11838 }
11839
11840 while (glyph < stop)
11841 {
11842 pos = XINT (Fnext_single_char_property_change
11843 (make_number (pos), Qdisplay, Qnil, limit));
11844 if (pos > pt_old)
11845 break;
11846 /* Skip glyphs from the same string. */
11847 string = glyph->object;
11848 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11849 /* Skip glyphs from an overlay. */
11850 while (glyph < stop
11851 && ! string_buffer_position (w, glyph->object, pos))
11852 {
11853 string = glyph->object;
11854 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11855 }
11856 }
11857
11858 /* If we reached the end of the line, and end was from a string,
11859 cursor is not on this line. */
11860 if (glyph == end && row->continued_p)
11861 return 0;
11862 }
11863
11864 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11865 w->cursor.x = x;
11866 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11867 w->cursor.y = row->y + dy;
11868
11869 if (w == XWINDOW (selected_window))
11870 {
11871 if (!row->continued_p
11872 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11873 && row->x == 0)
11874 {
11875 this_line_buffer = XBUFFER (w->buffer);
11876
11877 CHARPOS (this_line_start_pos)
11878 = MATRIX_ROW_START_CHARPOS (row) + delta;
11879 BYTEPOS (this_line_start_pos)
11880 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11881
11882 CHARPOS (this_line_end_pos)
11883 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11884 BYTEPOS (this_line_end_pos)
11885 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11886
11887 this_line_y = w->cursor.y;
11888 this_line_pixel_height = row->height;
11889 this_line_vpos = w->cursor.vpos;
11890 this_line_start_x = row->x;
11891 }
11892 else
11893 CHARPOS (this_line_start_pos) = 0;
11894 }
11895
11896 return 1;
11897 }
11898
11899
11900 /* Run window scroll functions, if any, for WINDOW with new window
11901 start STARTP. Sets the window start of WINDOW to that position.
11902
11903 We assume that the window's buffer is really current. */
11904
11905 static INLINE struct text_pos
11906 run_window_scroll_functions (window, startp)
11907 Lisp_Object window;
11908 struct text_pos startp;
11909 {
11910 struct window *w = XWINDOW (window);
11911 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11912
11913 if (current_buffer != XBUFFER (w->buffer))
11914 abort ();
11915
11916 if (!NILP (Vwindow_scroll_functions))
11917 {
11918 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11919 make_number (CHARPOS (startp)));
11920 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11921 /* In case the hook functions switch buffers. */
11922 if (current_buffer != XBUFFER (w->buffer))
11923 set_buffer_internal_1 (XBUFFER (w->buffer));
11924 }
11925
11926 return startp;
11927 }
11928
11929
11930 /* Make sure the line containing the cursor is fully visible.
11931 A value of 1 means there is nothing to be done.
11932 (Either the line is fully visible, or it cannot be made so,
11933 or we cannot tell.)
11934
11935 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11936 is higher than window.
11937
11938 A value of 0 means the caller should do scrolling
11939 as if point had gone off the screen. */
11940
11941 static int
11942 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11943 struct window *w;
11944 int force_p;
11945 int current_matrix_p;
11946 {
11947 struct glyph_matrix *matrix;
11948 struct glyph_row *row;
11949 int window_height;
11950
11951 if (!make_cursor_line_fully_visible_p)
11952 return 1;
11953
11954 /* It's not always possible to find the cursor, e.g, when a window
11955 is full of overlay strings. Don't do anything in that case. */
11956 if (w->cursor.vpos < 0)
11957 return 1;
11958
11959 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11960 row = MATRIX_ROW (matrix, w->cursor.vpos);
11961
11962 /* If the cursor row is not partially visible, there's nothing to do. */
11963 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11964 return 1;
11965
11966 /* If the row the cursor is in is taller than the window's height,
11967 it's not clear what to do, so do nothing. */
11968 window_height = window_box_height (w);
11969 if (row->height >= window_height)
11970 {
11971 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11972 return 1;
11973 }
11974 return 0;
11975
11976 #if 0
11977 /* This code used to try to scroll the window just enough to make
11978 the line visible. It returned 0 to say that the caller should
11979 allocate larger glyph matrices. */
11980
11981 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11982 {
11983 int dy = row->height - row->visible_height;
11984 w->vscroll = 0;
11985 w->cursor.y += dy;
11986 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11987 }
11988 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11989 {
11990 int dy = - (row->height - row->visible_height);
11991 w->vscroll = dy;
11992 w->cursor.y += dy;
11993 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11994 }
11995
11996 /* When we change the cursor y-position of the selected window,
11997 change this_line_y as well so that the display optimization for
11998 the cursor line of the selected window in redisplay_internal uses
11999 the correct y-position. */
12000 if (w == XWINDOW (selected_window))
12001 this_line_y = w->cursor.y;
12002
12003 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12004 redisplay with larger matrices. */
12005 if (matrix->nrows < required_matrix_height (w))
12006 {
12007 fonts_changed_p = 1;
12008 return 0;
12009 }
12010
12011 return 1;
12012 #endif /* 0 */
12013 }
12014
12015
12016 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12017 non-zero means only WINDOW is redisplayed in redisplay_internal.
12018 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12019 in redisplay_window to bring a partially visible line into view in
12020 the case that only the cursor has moved.
12021
12022 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12023 last screen line's vertical height extends past the end of the screen.
12024
12025 Value is
12026
12027 1 if scrolling succeeded
12028
12029 0 if scrolling didn't find point.
12030
12031 -1 if new fonts have been loaded so that we must interrupt
12032 redisplay, adjust glyph matrices, and try again. */
12033
12034 enum
12035 {
12036 SCROLLING_SUCCESS,
12037 SCROLLING_FAILED,
12038 SCROLLING_NEED_LARGER_MATRICES
12039 };
12040
12041 static int
12042 try_scrolling (window, just_this_one_p, scroll_conservatively,
12043 scroll_step, temp_scroll_step, last_line_misfit)
12044 Lisp_Object window;
12045 int just_this_one_p;
12046 EMACS_INT scroll_conservatively, scroll_step;
12047 int temp_scroll_step;
12048 int last_line_misfit;
12049 {
12050 struct window *w = XWINDOW (window);
12051 struct frame *f = XFRAME (w->frame);
12052 struct text_pos scroll_margin_pos;
12053 struct text_pos pos;
12054 struct text_pos startp;
12055 struct it it;
12056 Lisp_Object window_end;
12057 int this_scroll_margin;
12058 int dy = 0;
12059 int scroll_max;
12060 int rc;
12061 int amount_to_scroll = 0;
12062 Lisp_Object aggressive;
12063 int height;
12064 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12065
12066 #if GLYPH_DEBUG
12067 debug_method_add (w, "try_scrolling");
12068 #endif
12069
12070 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12071
12072 /* Compute scroll margin height in pixels. We scroll when point is
12073 within this distance from the top or bottom of the window. */
12074 if (scroll_margin > 0)
12075 {
12076 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12077 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12078 }
12079 else
12080 this_scroll_margin = 0;
12081
12082 /* Force scroll_conservatively to have a reasonable value so it doesn't
12083 cause an overflow while computing how much to scroll. */
12084 if (scroll_conservatively)
12085 scroll_conservatively = min (scroll_conservatively,
12086 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12087
12088 /* Compute how much we should try to scroll maximally to bring point
12089 into view. */
12090 if (scroll_step || scroll_conservatively || temp_scroll_step)
12091 scroll_max = max (scroll_step,
12092 max (scroll_conservatively, temp_scroll_step));
12093 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12094 || NUMBERP (current_buffer->scroll_up_aggressively))
12095 /* We're trying to scroll because of aggressive scrolling
12096 but no scroll_step is set. Choose an arbitrary one. Maybe
12097 there should be a variable for this. */
12098 scroll_max = 10;
12099 else
12100 scroll_max = 0;
12101 scroll_max *= FRAME_LINE_HEIGHT (f);
12102
12103 /* Decide whether we have to scroll down. Start at the window end
12104 and move this_scroll_margin up to find the position of the scroll
12105 margin. */
12106 window_end = Fwindow_end (window, Qt);
12107
12108 too_near_end:
12109
12110 CHARPOS (scroll_margin_pos) = XINT (window_end);
12111 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12112
12113 if (this_scroll_margin || extra_scroll_margin_lines)
12114 {
12115 start_display (&it, w, scroll_margin_pos);
12116 if (this_scroll_margin)
12117 move_it_vertically_backward (&it, this_scroll_margin);
12118 if (extra_scroll_margin_lines)
12119 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12120 scroll_margin_pos = it.current.pos;
12121 }
12122
12123 if (PT >= CHARPOS (scroll_margin_pos))
12124 {
12125 int y0;
12126
12127 /* Point is in the scroll margin at the bottom of the window, or
12128 below. Compute a new window start that makes point visible. */
12129
12130 /* Compute the distance from the scroll margin to PT.
12131 Give up if the distance is greater than scroll_max. */
12132 start_display (&it, w, scroll_margin_pos);
12133 y0 = it.current_y;
12134 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12135 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12136
12137 /* To make point visible, we have to move the window start
12138 down so that the line the cursor is in is visible, which
12139 means we have to add in the height of the cursor line. */
12140 dy = line_bottom_y (&it) - y0;
12141
12142 if (dy > scroll_max)
12143 return SCROLLING_FAILED;
12144
12145 /* Move the window start down. If scrolling conservatively,
12146 move it just enough down to make point visible. If
12147 scroll_step is set, move it down by scroll_step. */
12148 start_display (&it, w, startp);
12149
12150 if (scroll_conservatively)
12151 /* Set AMOUNT_TO_SCROLL to at least one line,
12152 and at most scroll_conservatively lines. */
12153 amount_to_scroll
12154 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12155 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12156 else if (scroll_step || temp_scroll_step)
12157 amount_to_scroll = scroll_max;
12158 else
12159 {
12160 aggressive = current_buffer->scroll_up_aggressively;
12161 height = WINDOW_BOX_TEXT_HEIGHT (w);
12162 if (NUMBERP (aggressive))
12163 {
12164 double float_amount = XFLOATINT (aggressive) * height;
12165 amount_to_scroll = float_amount;
12166 if (amount_to_scroll == 0 && float_amount > 0)
12167 amount_to_scroll = 1;
12168 }
12169 }
12170
12171 if (amount_to_scroll <= 0)
12172 return SCROLLING_FAILED;
12173
12174 /* If moving by amount_to_scroll leaves STARTP unchanged,
12175 move it down one screen line. */
12176
12177 move_it_vertically (&it, amount_to_scroll);
12178 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12179 move_it_by_lines (&it, 1, 1);
12180 startp = it.current.pos;
12181 }
12182 else
12183 {
12184 /* See if point is inside the scroll margin at the top of the
12185 window. */
12186 scroll_margin_pos = startp;
12187 if (this_scroll_margin)
12188 {
12189 start_display (&it, w, startp);
12190 move_it_vertically (&it, this_scroll_margin);
12191 scroll_margin_pos = it.current.pos;
12192 }
12193
12194 if (PT < CHARPOS (scroll_margin_pos))
12195 {
12196 /* Point is in the scroll margin at the top of the window or
12197 above what is displayed in the window. */
12198 int y0;
12199
12200 /* Compute the vertical distance from PT to the scroll
12201 margin position. Give up if distance is greater than
12202 scroll_max. */
12203 SET_TEXT_POS (pos, PT, PT_BYTE);
12204 start_display (&it, w, pos);
12205 y0 = it.current_y;
12206 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12207 it.last_visible_y, -1,
12208 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12209 dy = it.current_y - y0;
12210 if (dy > scroll_max)
12211 return SCROLLING_FAILED;
12212
12213 /* Compute new window start. */
12214 start_display (&it, w, startp);
12215
12216 if (scroll_conservatively)
12217 amount_to_scroll
12218 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12219 else if (scroll_step || temp_scroll_step)
12220 amount_to_scroll = scroll_max;
12221 else
12222 {
12223 aggressive = current_buffer->scroll_down_aggressively;
12224 height = WINDOW_BOX_TEXT_HEIGHT (w);
12225 if (NUMBERP (aggressive))
12226 {
12227 double float_amount = XFLOATINT (aggressive) * height;
12228 amount_to_scroll = float_amount;
12229 if (amount_to_scroll == 0 && float_amount > 0)
12230 amount_to_scroll = 1;
12231 }
12232 }
12233
12234 if (amount_to_scroll <= 0)
12235 return SCROLLING_FAILED;
12236
12237 move_it_vertically_backward (&it, amount_to_scroll);
12238 startp = it.current.pos;
12239 }
12240 }
12241
12242 /* Run window scroll functions. */
12243 startp = run_window_scroll_functions (window, startp);
12244
12245 /* Display the window. Give up if new fonts are loaded, or if point
12246 doesn't appear. */
12247 if (!try_window (window, startp, 0))
12248 rc = SCROLLING_NEED_LARGER_MATRICES;
12249 else if (w->cursor.vpos < 0)
12250 {
12251 clear_glyph_matrix (w->desired_matrix);
12252 rc = SCROLLING_FAILED;
12253 }
12254 else
12255 {
12256 /* Maybe forget recorded base line for line number display. */
12257 if (!just_this_one_p
12258 || current_buffer->clip_changed
12259 || BEG_UNCHANGED < CHARPOS (startp))
12260 w->base_line_number = Qnil;
12261
12262 /* If cursor ends up on a partially visible line,
12263 treat that as being off the bottom of the screen. */
12264 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12265 {
12266 clear_glyph_matrix (w->desired_matrix);
12267 ++extra_scroll_margin_lines;
12268 goto too_near_end;
12269 }
12270 rc = SCROLLING_SUCCESS;
12271 }
12272
12273 return rc;
12274 }
12275
12276
12277 /* Compute a suitable window start for window W if display of W starts
12278 on a continuation line. Value is non-zero if a new window start
12279 was computed.
12280
12281 The new window start will be computed, based on W's width, starting
12282 from the start of the continued line. It is the start of the
12283 screen line with the minimum distance from the old start W->start. */
12284
12285 static int
12286 compute_window_start_on_continuation_line (w)
12287 struct window *w;
12288 {
12289 struct text_pos pos, start_pos;
12290 int window_start_changed_p = 0;
12291
12292 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12293
12294 /* If window start is on a continuation line... Window start may be
12295 < BEGV in case there's invisible text at the start of the
12296 buffer (M-x rmail, for example). */
12297 if (CHARPOS (start_pos) > BEGV
12298 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12299 {
12300 struct it it;
12301 struct glyph_row *row;
12302
12303 /* Handle the case that the window start is out of range. */
12304 if (CHARPOS (start_pos) < BEGV)
12305 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12306 else if (CHARPOS (start_pos) > ZV)
12307 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12308
12309 /* Find the start of the continued line. This should be fast
12310 because scan_buffer is fast (newline cache). */
12311 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12312 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12313 row, DEFAULT_FACE_ID);
12314 reseat_at_previous_visible_line_start (&it);
12315
12316 /* If the line start is "too far" away from the window start,
12317 say it takes too much time to compute a new window start. */
12318 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12319 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12320 {
12321 int min_distance, distance;
12322
12323 /* Move forward by display lines to find the new window
12324 start. If window width was enlarged, the new start can
12325 be expected to be > the old start. If window width was
12326 decreased, the new window start will be < the old start.
12327 So, we're looking for the display line start with the
12328 minimum distance from the old window start. */
12329 pos = it.current.pos;
12330 min_distance = INFINITY;
12331 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12332 distance < min_distance)
12333 {
12334 min_distance = distance;
12335 pos = it.current.pos;
12336 move_it_by_lines (&it, 1, 0);
12337 }
12338
12339 /* Set the window start there. */
12340 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12341 window_start_changed_p = 1;
12342 }
12343 }
12344
12345 return window_start_changed_p;
12346 }
12347
12348
12349 /* Try cursor movement in case text has not changed in window WINDOW,
12350 with window start STARTP. Value is
12351
12352 CURSOR_MOVEMENT_SUCCESS if successful
12353
12354 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12355
12356 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12357 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12358 we want to scroll as if scroll-step were set to 1. See the code.
12359
12360 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12361 which case we have to abort this redisplay, and adjust matrices
12362 first. */
12363
12364 enum
12365 {
12366 CURSOR_MOVEMENT_SUCCESS,
12367 CURSOR_MOVEMENT_CANNOT_BE_USED,
12368 CURSOR_MOVEMENT_MUST_SCROLL,
12369 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12370 };
12371
12372 static int
12373 try_cursor_movement (window, startp, scroll_step)
12374 Lisp_Object window;
12375 struct text_pos startp;
12376 int *scroll_step;
12377 {
12378 struct window *w = XWINDOW (window);
12379 struct frame *f = XFRAME (w->frame);
12380 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12381
12382 #if GLYPH_DEBUG
12383 if (inhibit_try_cursor_movement)
12384 return rc;
12385 #endif
12386
12387 /* Handle case where text has not changed, only point, and it has
12388 not moved off the frame. */
12389 if (/* Point may be in this window. */
12390 PT >= CHARPOS (startp)
12391 /* Selective display hasn't changed. */
12392 && !current_buffer->clip_changed
12393 /* Function force-mode-line-update is used to force a thorough
12394 redisplay. It sets either windows_or_buffers_changed or
12395 update_mode_lines. So don't take a shortcut here for these
12396 cases. */
12397 && !update_mode_lines
12398 && !windows_or_buffers_changed
12399 && !cursor_type_changed
12400 /* Can't use this case if highlighting a region. When a
12401 region exists, cursor movement has to do more than just
12402 set the cursor. */
12403 && !(!NILP (Vtransient_mark_mode)
12404 && !NILP (current_buffer->mark_active))
12405 && NILP (w->region_showing)
12406 && NILP (Vshow_trailing_whitespace)
12407 /* Right after splitting windows, last_point may be nil. */
12408 && INTEGERP (w->last_point)
12409 /* This code is not used for mini-buffer for the sake of the case
12410 of redisplaying to replace an echo area message; since in
12411 that case the mini-buffer contents per se are usually
12412 unchanged. This code is of no real use in the mini-buffer
12413 since the handling of this_line_start_pos, etc., in redisplay
12414 handles the same cases. */
12415 && !EQ (window, minibuf_window)
12416 /* When splitting windows or for new windows, it happens that
12417 redisplay is called with a nil window_end_vpos or one being
12418 larger than the window. This should really be fixed in
12419 window.c. I don't have this on my list, now, so we do
12420 approximately the same as the old redisplay code. --gerd. */
12421 && INTEGERP (w->window_end_vpos)
12422 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12423 && (FRAME_WINDOW_P (f)
12424 || !overlay_arrow_in_current_buffer_p ()))
12425 {
12426 int this_scroll_margin, top_scroll_margin;
12427 struct glyph_row *row = NULL;
12428
12429 #if GLYPH_DEBUG
12430 debug_method_add (w, "cursor movement");
12431 #endif
12432
12433 /* Scroll if point within this distance from the top or bottom
12434 of the window. This is a pixel value. */
12435 this_scroll_margin = max (0, scroll_margin);
12436 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12437 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12438
12439 top_scroll_margin = this_scroll_margin;
12440 if (WINDOW_WANTS_HEADER_LINE_P (w))
12441 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12442
12443 /* Start with the row the cursor was displayed during the last
12444 not paused redisplay. Give up if that row is not valid. */
12445 if (w->last_cursor.vpos < 0
12446 || w->last_cursor.vpos >= w->current_matrix->nrows)
12447 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12448 else
12449 {
12450 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12451 if (row->mode_line_p)
12452 ++row;
12453 if (!row->enabled_p)
12454 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12455 }
12456
12457 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12458 {
12459 int scroll_p = 0;
12460 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12461
12462 if (PT > XFASTINT (w->last_point))
12463 {
12464 /* Point has moved forward. */
12465 while (MATRIX_ROW_END_CHARPOS (row) < PT
12466 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12467 {
12468 xassert (row->enabled_p);
12469 ++row;
12470 }
12471
12472 /* The end position of a row equals the start position
12473 of the next row. If PT is there, we would rather
12474 display it in the next line. */
12475 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12476 && MATRIX_ROW_END_CHARPOS (row) == PT
12477 && !cursor_row_p (w, row))
12478 ++row;
12479
12480 /* If within the scroll margin, scroll. Note that
12481 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12482 the next line would be drawn, and that
12483 this_scroll_margin can be zero. */
12484 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12485 || PT > MATRIX_ROW_END_CHARPOS (row)
12486 /* Line is completely visible last line in window
12487 and PT is to be set in the next line. */
12488 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12489 && PT == MATRIX_ROW_END_CHARPOS (row)
12490 && !row->ends_at_zv_p
12491 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12492 scroll_p = 1;
12493 }
12494 else if (PT < XFASTINT (w->last_point))
12495 {
12496 /* Cursor has to be moved backward. Note that PT >=
12497 CHARPOS (startp) because of the outer if-statement. */
12498 while (!row->mode_line_p
12499 && (MATRIX_ROW_START_CHARPOS (row) > PT
12500 || (MATRIX_ROW_START_CHARPOS (row) == PT
12501 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12502 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12503 row > w->current_matrix->rows
12504 && (row-1)->ends_in_newline_from_string_p))))
12505 && (row->y > top_scroll_margin
12506 || CHARPOS (startp) == BEGV))
12507 {
12508 xassert (row->enabled_p);
12509 --row;
12510 }
12511
12512 /* Consider the following case: Window starts at BEGV,
12513 there is invisible, intangible text at BEGV, so that
12514 display starts at some point START > BEGV. It can
12515 happen that we are called with PT somewhere between
12516 BEGV and START. Try to handle that case. */
12517 if (row < w->current_matrix->rows
12518 || row->mode_line_p)
12519 {
12520 row = w->current_matrix->rows;
12521 if (row->mode_line_p)
12522 ++row;
12523 }
12524
12525 /* Due to newlines in overlay strings, we may have to
12526 skip forward over overlay strings. */
12527 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12528 && MATRIX_ROW_END_CHARPOS (row) == PT
12529 && !cursor_row_p (w, row))
12530 ++row;
12531
12532 /* If within the scroll margin, scroll. */
12533 if (row->y < top_scroll_margin
12534 && CHARPOS (startp) != BEGV)
12535 scroll_p = 1;
12536 }
12537 else
12538 {
12539 /* Cursor did not move. So don't scroll even if cursor line
12540 is partially visible, as it was so before. */
12541 rc = CURSOR_MOVEMENT_SUCCESS;
12542 }
12543
12544 if (PT < MATRIX_ROW_START_CHARPOS (row)
12545 || PT > MATRIX_ROW_END_CHARPOS (row))
12546 {
12547 /* if PT is not in the glyph row, give up. */
12548 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12549 }
12550 else if (rc != CURSOR_MOVEMENT_SUCCESS
12551 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12552 && make_cursor_line_fully_visible_p)
12553 {
12554 if (PT == MATRIX_ROW_END_CHARPOS (row)
12555 && !row->ends_at_zv_p
12556 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12557 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12558 else if (row->height > window_box_height (w))
12559 {
12560 /* If we end up in a partially visible line, let's
12561 make it fully visible, except when it's taller
12562 than the window, in which case we can't do much
12563 about it. */
12564 *scroll_step = 1;
12565 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12566 }
12567 else
12568 {
12569 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12570 if (!cursor_row_fully_visible_p (w, 0, 1))
12571 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12572 else
12573 rc = CURSOR_MOVEMENT_SUCCESS;
12574 }
12575 }
12576 else if (scroll_p)
12577 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12578 else
12579 {
12580 do
12581 {
12582 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12583 {
12584 rc = CURSOR_MOVEMENT_SUCCESS;
12585 break;
12586 }
12587 ++row;
12588 }
12589 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12590 && MATRIX_ROW_START_CHARPOS (row) == PT
12591 && cursor_row_p (w, row));
12592 }
12593 }
12594 }
12595
12596 return rc;
12597 }
12598
12599 void
12600 set_vertical_scroll_bar (w)
12601 struct window *w;
12602 {
12603 int start, end, whole;
12604
12605 /* Calculate the start and end positions for the current window.
12606 At some point, it would be nice to choose between scrollbars
12607 which reflect the whole buffer size, with special markers
12608 indicating narrowing, and scrollbars which reflect only the
12609 visible region.
12610
12611 Note that mini-buffers sometimes aren't displaying any text. */
12612 if (!MINI_WINDOW_P (w)
12613 || (w == XWINDOW (minibuf_window)
12614 && NILP (echo_area_buffer[0])))
12615 {
12616 struct buffer *buf = XBUFFER (w->buffer);
12617 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12618 start = marker_position (w->start) - BUF_BEGV (buf);
12619 /* I don't think this is guaranteed to be right. For the
12620 moment, we'll pretend it is. */
12621 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12622
12623 if (end < start)
12624 end = start;
12625 if (whole < (end - start))
12626 whole = end - start;
12627 }
12628 else
12629 start = end = whole = 0;
12630
12631 /* Indicate what this scroll bar ought to be displaying now. */
12632 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12633 }
12634
12635
12636 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12637 selected_window is redisplayed.
12638
12639 We can return without actually redisplaying the window if
12640 fonts_changed_p is nonzero. In that case, redisplay_internal will
12641 retry. */
12642
12643 static void
12644 redisplay_window (window, just_this_one_p)
12645 Lisp_Object window;
12646 int just_this_one_p;
12647 {
12648 struct window *w = XWINDOW (window);
12649 struct frame *f = XFRAME (w->frame);
12650 struct buffer *buffer = XBUFFER (w->buffer);
12651 struct buffer *old = current_buffer;
12652 struct text_pos lpoint, opoint, startp;
12653 int update_mode_line;
12654 int tem;
12655 struct it it;
12656 /* Record it now because it's overwritten. */
12657 int current_matrix_up_to_date_p = 0;
12658 int used_current_matrix_p = 0;
12659 /* This is less strict than current_matrix_up_to_date_p.
12660 It indictes that the buffer contents and narrowing are unchanged. */
12661 int buffer_unchanged_p = 0;
12662 int temp_scroll_step = 0;
12663 int count = SPECPDL_INDEX ();
12664 int rc;
12665 int centering_position = -1;
12666 int last_line_misfit = 0;
12667
12668 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12669 opoint = lpoint;
12670
12671 /* W must be a leaf window here. */
12672 xassert (!NILP (w->buffer));
12673 #if GLYPH_DEBUG
12674 *w->desired_matrix->method = 0;
12675 #endif
12676
12677 specbind (Qinhibit_point_motion_hooks, Qt);
12678
12679 reconsider_clip_changes (w, buffer);
12680
12681 /* Has the mode line to be updated? */
12682 update_mode_line = (!NILP (w->update_mode_line)
12683 || update_mode_lines
12684 || buffer->clip_changed
12685 || buffer->prevent_redisplay_optimizations_p);
12686
12687 if (MINI_WINDOW_P (w))
12688 {
12689 if (w == XWINDOW (echo_area_window)
12690 && !NILP (echo_area_buffer[0]))
12691 {
12692 if (update_mode_line)
12693 /* We may have to update a tty frame's menu bar or a
12694 tool-bar. Example `M-x C-h C-h C-g'. */
12695 goto finish_menu_bars;
12696 else
12697 /* We've already displayed the echo area glyphs in this window. */
12698 goto finish_scroll_bars;
12699 }
12700 else if ((w != XWINDOW (minibuf_window)
12701 || minibuf_level == 0)
12702 /* When buffer is nonempty, redisplay window normally. */
12703 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12704 /* Quail displays non-mini buffers in minibuffer window.
12705 In that case, redisplay the window normally. */
12706 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12707 {
12708 /* W is a mini-buffer window, but it's not active, so clear
12709 it. */
12710 int yb = window_text_bottom_y (w);
12711 struct glyph_row *row;
12712 int y;
12713
12714 for (y = 0, row = w->desired_matrix->rows;
12715 y < yb;
12716 y += row->height, ++row)
12717 blank_row (w, row, y);
12718 goto finish_scroll_bars;
12719 }
12720
12721 clear_glyph_matrix (w->desired_matrix);
12722 }
12723
12724 /* Otherwise set up data on this window; select its buffer and point
12725 value. */
12726 /* Really select the buffer, for the sake of buffer-local
12727 variables. */
12728 set_buffer_internal_1 (XBUFFER (w->buffer));
12729 SET_TEXT_POS (opoint, PT, PT_BYTE);
12730
12731 current_matrix_up_to_date_p
12732 = (!NILP (w->window_end_valid)
12733 && !current_buffer->clip_changed
12734 && !current_buffer->prevent_redisplay_optimizations_p
12735 && XFASTINT (w->last_modified) >= MODIFF
12736 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12737
12738 buffer_unchanged_p
12739 = (!NILP (w->window_end_valid)
12740 && !current_buffer->clip_changed
12741 && XFASTINT (w->last_modified) >= MODIFF
12742 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12743
12744 /* When windows_or_buffers_changed is non-zero, we can't rely on
12745 the window end being valid, so set it to nil there. */
12746 if (windows_or_buffers_changed)
12747 {
12748 /* If window starts on a continuation line, maybe adjust the
12749 window start in case the window's width changed. */
12750 if (XMARKER (w->start)->buffer == current_buffer)
12751 compute_window_start_on_continuation_line (w);
12752
12753 w->window_end_valid = Qnil;
12754 }
12755
12756 /* Some sanity checks. */
12757 CHECK_WINDOW_END (w);
12758 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12759 abort ();
12760 if (BYTEPOS (opoint) < CHARPOS (opoint))
12761 abort ();
12762
12763 /* If %c is in mode line, update it if needed. */
12764 if (!NILP (w->column_number_displayed)
12765 /* This alternative quickly identifies a common case
12766 where no change is needed. */
12767 && !(PT == XFASTINT (w->last_point)
12768 && XFASTINT (w->last_modified) >= MODIFF
12769 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12770 && (XFASTINT (w->column_number_displayed)
12771 != (int) current_column ())) /* iftc */
12772 update_mode_line = 1;
12773
12774 /* Count number of windows showing the selected buffer. An indirect
12775 buffer counts as its base buffer. */
12776 if (!just_this_one_p)
12777 {
12778 struct buffer *current_base, *window_base;
12779 current_base = current_buffer;
12780 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12781 if (current_base->base_buffer)
12782 current_base = current_base->base_buffer;
12783 if (window_base->base_buffer)
12784 window_base = window_base->base_buffer;
12785 if (current_base == window_base)
12786 buffer_shared++;
12787 }
12788
12789 /* Point refers normally to the selected window. For any other
12790 window, set up appropriate value. */
12791 if (!EQ (window, selected_window))
12792 {
12793 int new_pt = XMARKER (w->pointm)->charpos;
12794 int new_pt_byte = marker_byte_position (w->pointm);
12795 if (new_pt < BEGV)
12796 {
12797 new_pt = BEGV;
12798 new_pt_byte = BEGV_BYTE;
12799 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12800 }
12801 else if (new_pt > (ZV - 1))
12802 {
12803 new_pt = ZV;
12804 new_pt_byte = ZV_BYTE;
12805 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12806 }
12807
12808 /* We don't use SET_PT so that the point-motion hooks don't run. */
12809 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12810 }
12811
12812 /* If any of the character widths specified in the display table
12813 have changed, invalidate the width run cache. It's true that
12814 this may be a bit late to catch such changes, but the rest of
12815 redisplay goes (non-fatally) haywire when the display table is
12816 changed, so why should we worry about doing any better? */
12817 if (current_buffer->width_run_cache)
12818 {
12819 struct Lisp_Char_Table *disptab = buffer_display_table ();
12820
12821 if (! disptab_matches_widthtab (disptab,
12822 XVECTOR (current_buffer->width_table)))
12823 {
12824 invalidate_region_cache (current_buffer,
12825 current_buffer->width_run_cache,
12826 BEG, Z);
12827 recompute_width_table (current_buffer, disptab);
12828 }
12829 }
12830
12831 /* If window-start is screwed up, choose a new one. */
12832 if (XMARKER (w->start)->buffer != current_buffer)
12833 goto recenter;
12834
12835 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12836
12837 /* If someone specified a new starting point but did not insist,
12838 check whether it can be used. */
12839 if (!NILP (w->optional_new_start)
12840 && CHARPOS (startp) >= BEGV
12841 && CHARPOS (startp) <= ZV)
12842 {
12843 w->optional_new_start = Qnil;
12844 start_display (&it, w, startp);
12845 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12846 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12847 if (IT_CHARPOS (it) == PT)
12848 w->force_start = Qt;
12849 /* IT may overshoot PT if text at PT is invisible. */
12850 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12851 w->force_start = Qt;
12852 }
12853
12854 /* Handle case where place to start displaying has been specified,
12855 unless the specified location is outside the accessible range. */
12856 if (!NILP (w->force_start)
12857 || w->frozen_window_start_p)
12858 {
12859 /* We set this later on if we have to adjust point. */
12860 int new_vpos = -1;
12861 int val;
12862
12863 w->force_start = Qnil;
12864 w->vscroll = 0;
12865 w->window_end_valid = Qnil;
12866
12867 /* Forget any recorded base line for line number display. */
12868 if (!buffer_unchanged_p)
12869 w->base_line_number = Qnil;
12870
12871 /* Redisplay the mode line. Select the buffer properly for that.
12872 Also, run the hook window-scroll-functions
12873 because we have scrolled. */
12874 /* Note, we do this after clearing force_start because
12875 if there's an error, it is better to forget about force_start
12876 than to get into an infinite loop calling the hook functions
12877 and having them get more errors. */
12878 if (!update_mode_line
12879 || ! NILP (Vwindow_scroll_functions))
12880 {
12881 update_mode_line = 1;
12882 w->update_mode_line = Qt;
12883 startp = run_window_scroll_functions (window, startp);
12884 }
12885
12886 w->last_modified = make_number (0);
12887 w->last_overlay_modified = make_number (0);
12888 if (CHARPOS (startp) < BEGV)
12889 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12890 else if (CHARPOS (startp) > ZV)
12891 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12892
12893 /* Redisplay, then check if cursor has been set during the
12894 redisplay. Give up if new fonts were loaded. */
12895 val = try_window (window, startp, 1);
12896 if (!val)
12897 {
12898 w->force_start = Qt;
12899 clear_glyph_matrix (w->desired_matrix);
12900 goto need_larger_matrices;
12901 }
12902 /* Point was outside the scroll margins. */
12903 if (val < 0)
12904 new_vpos = window_box_height (w) / 2;
12905
12906 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12907 {
12908 /* If point does not appear, try to move point so it does
12909 appear. The desired matrix has been built above, so we
12910 can use it here. */
12911 new_vpos = window_box_height (w) / 2;
12912 }
12913
12914 if (!cursor_row_fully_visible_p (w, 0, 0))
12915 {
12916 /* Point does appear, but on a line partly visible at end of window.
12917 Move it back to a fully-visible line. */
12918 new_vpos = window_box_height (w);
12919 }
12920
12921 /* If we need to move point for either of the above reasons,
12922 now actually do it. */
12923 if (new_vpos >= 0)
12924 {
12925 struct glyph_row *row;
12926
12927 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12928 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12929 ++row;
12930
12931 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12932 MATRIX_ROW_START_BYTEPOS (row));
12933
12934 if (w != XWINDOW (selected_window))
12935 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12936 else if (current_buffer == old)
12937 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12938
12939 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12940
12941 /* If we are highlighting the region, then we just changed
12942 the region, so redisplay to show it. */
12943 if (!NILP (Vtransient_mark_mode)
12944 && !NILP (current_buffer->mark_active))
12945 {
12946 clear_glyph_matrix (w->desired_matrix);
12947 if (!try_window (window, startp, 0))
12948 goto need_larger_matrices;
12949 }
12950 }
12951
12952 #if GLYPH_DEBUG
12953 debug_method_add (w, "forced window start");
12954 #endif
12955 goto done;
12956 }
12957
12958 /* Handle case where text has not changed, only point, and it has
12959 not moved off the frame, and we are not retrying after hscroll.
12960 (current_matrix_up_to_date_p is nonzero when retrying.) */
12961 if (current_matrix_up_to_date_p
12962 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12963 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12964 {
12965 switch (rc)
12966 {
12967 case CURSOR_MOVEMENT_SUCCESS:
12968 used_current_matrix_p = 1;
12969 goto done;
12970
12971 #if 0 /* try_cursor_movement never returns this value. */
12972 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12973 goto need_larger_matrices;
12974 #endif
12975
12976 case CURSOR_MOVEMENT_MUST_SCROLL:
12977 goto try_to_scroll;
12978
12979 default:
12980 abort ();
12981 }
12982 }
12983 /* If current starting point was originally the beginning of a line
12984 but no longer is, find a new starting point. */
12985 else if (!NILP (w->start_at_line_beg)
12986 && !(CHARPOS (startp) <= BEGV
12987 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12988 {
12989 #if GLYPH_DEBUG
12990 debug_method_add (w, "recenter 1");
12991 #endif
12992 goto recenter;
12993 }
12994
12995 /* Try scrolling with try_window_id. Value is > 0 if update has
12996 been done, it is -1 if we know that the same window start will
12997 not work. It is 0 if unsuccessful for some other reason. */
12998 else if ((tem = try_window_id (w)) != 0)
12999 {
13000 #if GLYPH_DEBUG
13001 debug_method_add (w, "try_window_id %d", tem);
13002 #endif
13003
13004 if (fonts_changed_p)
13005 goto need_larger_matrices;
13006 if (tem > 0)
13007 goto done;
13008
13009 /* Otherwise try_window_id has returned -1 which means that we
13010 don't want the alternative below this comment to execute. */
13011 }
13012 else if (CHARPOS (startp) >= BEGV
13013 && CHARPOS (startp) <= ZV
13014 && PT >= CHARPOS (startp)
13015 && (CHARPOS (startp) < ZV
13016 /* Avoid starting at end of buffer. */
13017 || CHARPOS (startp) == BEGV
13018 || (XFASTINT (w->last_modified) >= MODIFF
13019 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13020 {
13021
13022 /* If first window line is a continuation line, and window start
13023 is inside the modified region, but the first change is before
13024 current window start, we must select a new window start.*/
13025 if (NILP (w->start_at_line_beg)
13026 && CHARPOS (startp) > BEGV)
13027 {
13028 /* Make sure beg_unchanged and end_unchanged are up to date.
13029 Do it only if buffer has really changed. This may or may
13030 not have been done by try_window_id (see which) already. */
13031 if (MODIFF > SAVE_MODIFF
13032 /* This seems to happen sometimes after saving a buffer. */
13033 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13034 {
13035 if (GPT - BEG < BEG_UNCHANGED)
13036 BEG_UNCHANGED = GPT - BEG;
13037 if (Z - GPT < END_UNCHANGED)
13038 END_UNCHANGED = Z - GPT;
13039 }
13040
13041 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
13042 && CHARPOS (startp) <= Z - END_UNCHANGED)
13043 {
13044 /* There doesn't seems to be a simple way to find a new
13045 window start that is near the old window start, so
13046 we just recenter. */
13047 goto recenter;
13048 }
13049 }
13050
13051 #if GLYPH_DEBUG
13052 debug_method_add (w, "same window start");
13053 #endif
13054
13055 /* Try to redisplay starting at same place as before.
13056 If point has not moved off frame, accept the results. */
13057 if (!current_matrix_up_to_date_p
13058 /* Don't use try_window_reusing_current_matrix in this case
13059 because a window scroll function can have changed the
13060 buffer. */
13061 || !NILP (Vwindow_scroll_functions)
13062 || MINI_WINDOW_P (w)
13063 || !(used_current_matrix_p
13064 = try_window_reusing_current_matrix (w)))
13065 {
13066 IF_DEBUG (debug_method_add (w, "1"));
13067 if (try_window (window, startp, 1) < 0)
13068 /* -1 means we need to scroll.
13069 0 means we need new matrices, but fonts_changed_p
13070 is set in that case, so we will detect it below. */
13071 goto try_to_scroll;
13072 }
13073
13074 if (fonts_changed_p)
13075 goto need_larger_matrices;
13076
13077 if (w->cursor.vpos >= 0)
13078 {
13079 if (!just_this_one_p
13080 || current_buffer->clip_changed
13081 || BEG_UNCHANGED < CHARPOS (startp))
13082 /* Forget any recorded base line for line number display. */
13083 w->base_line_number = Qnil;
13084
13085 if (!cursor_row_fully_visible_p (w, 1, 0))
13086 {
13087 clear_glyph_matrix (w->desired_matrix);
13088 last_line_misfit = 1;
13089 }
13090 /* Drop through and scroll. */
13091 else
13092 goto done;
13093 }
13094 else
13095 clear_glyph_matrix (w->desired_matrix);
13096 }
13097
13098 try_to_scroll:
13099
13100 w->last_modified = make_number (0);
13101 w->last_overlay_modified = make_number (0);
13102
13103 /* Redisplay the mode line. Select the buffer properly for that. */
13104 if (!update_mode_line)
13105 {
13106 update_mode_line = 1;
13107 w->update_mode_line = Qt;
13108 }
13109
13110 /* Try to scroll by specified few lines. */
13111 if ((scroll_conservatively
13112 || scroll_step
13113 || temp_scroll_step
13114 || NUMBERP (current_buffer->scroll_up_aggressively)
13115 || NUMBERP (current_buffer->scroll_down_aggressively))
13116 && !current_buffer->clip_changed
13117 && CHARPOS (startp) >= BEGV
13118 && CHARPOS (startp) <= ZV)
13119 {
13120 /* The function returns -1 if new fonts were loaded, 1 if
13121 successful, 0 if not successful. */
13122 int rc = try_scrolling (window, just_this_one_p,
13123 scroll_conservatively,
13124 scroll_step,
13125 temp_scroll_step, last_line_misfit);
13126 switch (rc)
13127 {
13128 case SCROLLING_SUCCESS:
13129 goto done;
13130
13131 case SCROLLING_NEED_LARGER_MATRICES:
13132 goto need_larger_matrices;
13133
13134 case SCROLLING_FAILED:
13135 break;
13136
13137 default:
13138 abort ();
13139 }
13140 }
13141
13142 /* Finally, just choose place to start which centers point */
13143
13144 recenter:
13145 if (centering_position < 0)
13146 centering_position = window_box_height (w) / 2;
13147
13148 #if GLYPH_DEBUG
13149 debug_method_add (w, "recenter");
13150 #endif
13151
13152 /* w->vscroll = 0; */
13153
13154 /* Forget any previously recorded base line for line number display. */
13155 if (!buffer_unchanged_p)
13156 w->base_line_number = Qnil;
13157
13158 /* Move backward half the height of the window. */
13159 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13160 it.current_y = it.last_visible_y;
13161 move_it_vertically_backward (&it, centering_position);
13162 xassert (IT_CHARPOS (it) >= BEGV);
13163
13164 /* The function move_it_vertically_backward may move over more
13165 than the specified y-distance. If it->w is small, e.g. a
13166 mini-buffer window, we may end up in front of the window's
13167 display area. Start displaying at the start of the line
13168 containing PT in this case. */
13169 if (it.current_y <= 0)
13170 {
13171 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13172 move_it_vertically_backward (&it, 0);
13173 #if 0
13174 /* I think this assert is bogus if buffer contains
13175 invisible text or images. KFS. */
13176 xassert (IT_CHARPOS (it) <= PT);
13177 #endif
13178 it.current_y = 0;
13179 }
13180
13181 it.current_x = it.hpos = 0;
13182
13183 /* Set startp here explicitly in case that helps avoid an infinite loop
13184 in case the window-scroll-functions functions get errors. */
13185 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13186
13187 /* Run scroll hooks. */
13188 startp = run_window_scroll_functions (window, it.current.pos);
13189
13190 /* Redisplay the window. */
13191 if (!current_matrix_up_to_date_p
13192 || windows_or_buffers_changed
13193 || cursor_type_changed
13194 /* Don't use try_window_reusing_current_matrix in this case
13195 because it can have changed the buffer. */
13196 || !NILP (Vwindow_scroll_functions)
13197 || !just_this_one_p
13198 || MINI_WINDOW_P (w)
13199 || !(used_current_matrix_p
13200 = try_window_reusing_current_matrix (w)))
13201 try_window (window, startp, 0);
13202
13203 /* If new fonts have been loaded (due to fontsets), give up. We
13204 have to start a new redisplay since we need to re-adjust glyph
13205 matrices. */
13206 if (fonts_changed_p)
13207 goto need_larger_matrices;
13208
13209 /* If cursor did not appear assume that the middle of the window is
13210 in the first line of the window. Do it again with the next line.
13211 (Imagine a window of height 100, displaying two lines of height
13212 60. Moving back 50 from it->last_visible_y will end in the first
13213 line.) */
13214 if (w->cursor.vpos < 0)
13215 {
13216 if (!NILP (w->window_end_valid)
13217 && PT >= Z - XFASTINT (w->window_end_pos))
13218 {
13219 clear_glyph_matrix (w->desired_matrix);
13220 move_it_by_lines (&it, 1, 0);
13221 try_window (window, it.current.pos, 0);
13222 }
13223 else if (PT < IT_CHARPOS (it))
13224 {
13225 clear_glyph_matrix (w->desired_matrix);
13226 move_it_by_lines (&it, -1, 0);
13227 try_window (window, it.current.pos, 0);
13228 }
13229 else
13230 {
13231 /* Not much we can do about it. */
13232 }
13233 }
13234
13235 /* Consider the following case: Window starts at BEGV, there is
13236 invisible, intangible text at BEGV, so that display starts at
13237 some point START > BEGV. It can happen that we are called with
13238 PT somewhere between BEGV and START. Try to handle that case. */
13239 if (w->cursor.vpos < 0)
13240 {
13241 struct glyph_row *row = w->current_matrix->rows;
13242 if (row->mode_line_p)
13243 ++row;
13244 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13245 }
13246
13247 if (!cursor_row_fully_visible_p (w, 0, 0))
13248 {
13249 /* If vscroll is enabled, disable it and try again. */
13250 if (w->vscroll)
13251 {
13252 w->vscroll = 0;
13253 clear_glyph_matrix (w->desired_matrix);
13254 goto recenter;
13255 }
13256
13257 /* If centering point failed to make the whole line visible,
13258 put point at the top instead. That has to make the whole line
13259 visible, if it can be done. */
13260 if (centering_position == 0)
13261 goto done;
13262
13263 clear_glyph_matrix (w->desired_matrix);
13264 centering_position = 0;
13265 goto recenter;
13266 }
13267
13268 done:
13269
13270 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13271 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13272 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13273 ? Qt : Qnil);
13274
13275 /* Display the mode line, if we must. */
13276 if ((update_mode_line
13277 /* If window not full width, must redo its mode line
13278 if (a) the window to its side is being redone and
13279 (b) we do a frame-based redisplay. This is a consequence
13280 of how inverted lines are drawn in frame-based redisplay. */
13281 || (!just_this_one_p
13282 && !FRAME_WINDOW_P (f)
13283 && !WINDOW_FULL_WIDTH_P (w))
13284 /* Line number to display. */
13285 || INTEGERP (w->base_line_pos)
13286 /* Column number is displayed and different from the one displayed. */
13287 || (!NILP (w->column_number_displayed)
13288 && (XFASTINT (w->column_number_displayed)
13289 != (int) current_column ()))) /* iftc */
13290 /* This means that the window has a mode line. */
13291 && (WINDOW_WANTS_MODELINE_P (w)
13292 || WINDOW_WANTS_HEADER_LINE_P (w)))
13293 {
13294 display_mode_lines (w);
13295
13296 /* If mode line height has changed, arrange for a thorough
13297 immediate redisplay using the correct mode line height. */
13298 if (WINDOW_WANTS_MODELINE_P (w)
13299 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13300 {
13301 fonts_changed_p = 1;
13302 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13303 = DESIRED_MODE_LINE_HEIGHT (w);
13304 }
13305
13306 /* If top line height has changed, arrange for a thorough
13307 immediate redisplay using the correct mode line height. */
13308 if (WINDOW_WANTS_HEADER_LINE_P (w)
13309 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13310 {
13311 fonts_changed_p = 1;
13312 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13313 = DESIRED_HEADER_LINE_HEIGHT (w);
13314 }
13315
13316 if (fonts_changed_p)
13317 goto need_larger_matrices;
13318 }
13319
13320 if (!line_number_displayed
13321 && !BUFFERP (w->base_line_pos))
13322 {
13323 w->base_line_pos = Qnil;
13324 w->base_line_number = Qnil;
13325 }
13326
13327 finish_menu_bars:
13328
13329 /* When we reach a frame's selected window, redo the frame's menu bar. */
13330 if (update_mode_line
13331 && EQ (FRAME_SELECTED_WINDOW (f), window))
13332 {
13333 int redisplay_menu_p = 0;
13334 int redisplay_tool_bar_p = 0;
13335
13336 if (FRAME_WINDOW_P (f))
13337 {
13338 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13339 || defined (USE_GTK)
13340 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13341 #else
13342 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13343 #endif
13344 }
13345 else
13346 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13347
13348 if (redisplay_menu_p)
13349 display_menu_bar (w);
13350
13351 #ifdef HAVE_WINDOW_SYSTEM
13352 #ifdef USE_GTK
13353 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13354 #else
13355 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13356 && (FRAME_TOOL_BAR_LINES (f) > 0
13357 || auto_resize_tool_bars_p);
13358
13359 #endif
13360
13361 if (redisplay_tool_bar_p)
13362 redisplay_tool_bar (f);
13363 #endif
13364 }
13365
13366 #ifdef HAVE_WINDOW_SYSTEM
13367 if (FRAME_WINDOW_P (f)
13368 && update_window_fringes (w, (just_this_one_p
13369 || (!used_current_matrix_p && !overlay_arrow_seen)
13370 || w->pseudo_window_p)))
13371 {
13372 update_begin (f);
13373 BLOCK_INPUT;
13374 if (draw_window_fringes (w, 1))
13375 x_draw_vertical_border (w);
13376 UNBLOCK_INPUT;
13377 update_end (f);
13378 }
13379 #endif /* HAVE_WINDOW_SYSTEM */
13380
13381 /* We go to this label, with fonts_changed_p nonzero,
13382 if it is necessary to try again using larger glyph matrices.
13383 We have to redeem the scroll bar even in this case,
13384 because the loop in redisplay_internal expects that. */
13385 need_larger_matrices:
13386 ;
13387 finish_scroll_bars:
13388
13389 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13390 {
13391 /* Set the thumb's position and size. */
13392 set_vertical_scroll_bar (w);
13393
13394 /* Note that we actually used the scroll bar attached to this
13395 window, so it shouldn't be deleted at the end of redisplay. */
13396 redeem_scroll_bar_hook (w);
13397 }
13398
13399 /* Restore current_buffer and value of point in it. */
13400 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13401 set_buffer_internal_1 (old);
13402 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13403
13404 unbind_to (count, Qnil);
13405 }
13406
13407
13408 /* Build the complete desired matrix of WINDOW with a window start
13409 buffer position POS.
13410
13411 Value is 1 if successful. It is zero if fonts were loaded during
13412 redisplay which makes re-adjusting glyph matrices necessary, and -1
13413 if point would appear in the scroll margins.
13414 (We check that only if CHECK_MARGINS is nonzero. */
13415
13416 int
13417 try_window (window, pos, check_margins)
13418 Lisp_Object window;
13419 struct text_pos pos;
13420 int check_margins;
13421 {
13422 struct window *w = XWINDOW (window);
13423 struct it it;
13424 struct glyph_row *last_text_row = NULL;
13425
13426 /* Make POS the new window start. */
13427 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13428
13429 /* Mark cursor position as unknown. No overlay arrow seen. */
13430 w->cursor.vpos = -1;
13431 overlay_arrow_seen = 0;
13432
13433 /* Initialize iterator and info to start at POS. */
13434 start_display (&it, w, pos);
13435
13436 /* Display all lines of W. */
13437 while (it.current_y < it.last_visible_y)
13438 {
13439 if (display_line (&it))
13440 last_text_row = it.glyph_row - 1;
13441 if (fonts_changed_p)
13442 return 0;
13443 }
13444
13445 /* Don't let the cursor end in the scroll margins. */
13446 if (check_margins
13447 && !MINI_WINDOW_P (w))
13448 {
13449 int this_scroll_margin;
13450
13451 this_scroll_margin = max (0, scroll_margin);
13452 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13453 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13454
13455 if ((w->cursor.y < this_scroll_margin
13456 && CHARPOS (pos) > BEGV
13457 && IT_CHARPOS (it) < ZV)
13458 /* rms: considering make_cursor_line_fully_visible_p here
13459 seems to give wrong results. We don't want to recenter
13460 when the last line is partly visible, we want to allow
13461 that case to be handled in the usual way. */
13462 || (w->cursor.y + 1) > it.last_visible_y)
13463 {
13464 w->cursor.vpos = -1;
13465 clear_glyph_matrix (w->desired_matrix);
13466 return -1;
13467 }
13468 }
13469
13470 /* If bottom moved off end of frame, change mode line percentage. */
13471 if (XFASTINT (w->window_end_pos) <= 0
13472 && Z != IT_CHARPOS (it))
13473 w->update_mode_line = Qt;
13474
13475 /* Set window_end_pos to the offset of the last character displayed
13476 on the window from the end of current_buffer. Set
13477 window_end_vpos to its row number. */
13478 if (last_text_row)
13479 {
13480 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13481 w->window_end_bytepos
13482 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13483 w->window_end_pos
13484 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13485 w->window_end_vpos
13486 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13487 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13488 ->displays_text_p);
13489 }
13490 else
13491 {
13492 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13493 w->window_end_pos = make_number (Z - ZV);
13494 w->window_end_vpos = make_number (0);
13495 }
13496
13497 /* But that is not valid info until redisplay finishes. */
13498 w->window_end_valid = Qnil;
13499 return 1;
13500 }
13501
13502
13503 \f
13504 /************************************************************************
13505 Window redisplay reusing current matrix when buffer has not changed
13506 ************************************************************************/
13507
13508 /* Try redisplay of window W showing an unchanged buffer with a
13509 different window start than the last time it was displayed by
13510 reusing its current matrix. Value is non-zero if successful.
13511 W->start is the new window start. */
13512
13513 static int
13514 try_window_reusing_current_matrix (w)
13515 struct window *w;
13516 {
13517 struct frame *f = XFRAME (w->frame);
13518 struct glyph_row *row, *bottom_row;
13519 struct it it;
13520 struct run run;
13521 struct text_pos start, new_start;
13522 int nrows_scrolled, i;
13523 struct glyph_row *last_text_row;
13524 struct glyph_row *last_reused_text_row;
13525 struct glyph_row *start_row;
13526 int start_vpos, min_y, max_y;
13527
13528 #if GLYPH_DEBUG
13529 if (inhibit_try_window_reusing)
13530 return 0;
13531 #endif
13532
13533 if (/* This function doesn't handle terminal frames. */
13534 !FRAME_WINDOW_P (f)
13535 /* Don't try to reuse the display if windows have been split
13536 or such. */
13537 || windows_or_buffers_changed
13538 || cursor_type_changed)
13539 return 0;
13540
13541 /* Can't do this if region may have changed. */
13542 if ((!NILP (Vtransient_mark_mode)
13543 && !NILP (current_buffer->mark_active))
13544 || !NILP (w->region_showing)
13545 || !NILP (Vshow_trailing_whitespace))
13546 return 0;
13547
13548 /* If top-line visibility has changed, give up. */
13549 if (WINDOW_WANTS_HEADER_LINE_P (w)
13550 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13551 return 0;
13552
13553 /* Give up if old or new display is scrolled vertically. We could
13554 make this function handle this, but right now it doesn't. */
13555 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13556 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13557 return 0;
13558
13559 /* The variable new_start now holds the new window start. The old
13560 start `start' can be determined from the current matrix. */
13561 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13562 start = start_row->start.pos;
13563 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13564
13565 /* Clear the desired matrix for the display below. */
13566 clear_glyph_matrix (w->desired_matrix);
13567
13568 if (CHARPOS (new_start) <= CHARPOS (start))
13569 {
13570 int first_row_y;
13571
13572 /* Don't use this method if the display starts with an ellipsis
13573 displayed for invisible text. It's not easy to handle that case
13574 below, and it's certainly not worth the effort since this is
13575 not a frequent case. */
13576 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13577 return 0;
13578
13579 IF_DEBUG (debug_method_add (w, "twu1"));
13580
13581 /* Display up to a row that can be reused. The variable
13582 last_text_row is set to the last row displayed that displays
13583 text. Note that it.vpos == 0 if or if not there is a
13584 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13585 start_display (&it, w, new_start);
13586 first_row_y = it.current_y;
13587 w->cursor.vpos = -1;
13588 last_text_row = last_reused_text_row = NULL;
13589
13590 while (it.current_y < it.last_visible_y
13591 && !fonts_changed_p)
13592 {
13593 /* If we have reached into the characters in the START row,
13594 that means the line boundaries have changed. So we
13595 can't start copying with the row START. Maybe it will
13596 work to start copying with the following row. */
13597 while (IT_CHARPOS (it) > CHARPOS (start))
13598 {
13599 /* Advance to the next row as the "start". */
13600 start_row++;
13601 start = start_row->start.pos;
13602 /* If there are no more rows to try, or just one, give up. */
13603 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13604 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13605 || CHARPOS (start) == ZV)
13606 {
13607 clear_glyph_matrix (w->desired_matrix);
13608 return 0;
13609 }
13610
13611 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13612 }
13613 /* If we have reached alignment,
13614 we can copy the rest of the rows. */
13615 if (IT_CHARPOS (it) == CHARPOS (start))
13616 break;
13617
13618 if (display_line (&it))
13619 last_text_row = it.glyph_row - 1;
13620 }
13621
13622 /* A value of current_y < last_visible_y means that we stopped
13623 at the previous window start, which in turn means that we
13624 have at least one reusable row. */
13625 if (it.current_y < it.last_visible_y)
13626 {
13627 /* IT.vpos always starts from 0; it counts text lines. */
13628 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13629
13630 /* Find PT if not already found in the lines displayed. */
13631 if (w->cursor.vpos < 0)
13632 {
13633 int dy = it.current_y - start_row->y;
13634
13635 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13636 row = row_containing_pos (w, PT, row, NULL, dy);
13637 if (row)
13638 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13639 dy, nrows_scrolled);
13640 else
13641 {
13642 clear_glyph_matrix (w->desired_matrix);
13643 return 0;
13644 }
13645 }
13646
13647 /* Scroll the display. Do it before the current matrix is
13648 changed. The problem here is that update has not yet
13649 run, i.e. part of the current matrix is not up to date.
13650 scroll_run_hook will clear the cursor, and use the
13651 current matrix to get the height of the row the cursor is
13652 in. */
13653 run.current_y = start_row->y;
13654 run.desired_y = it.current_y;
13655 run.height = it.last_visible_y - it.current_y;
13656
13657 if (run.height > 0 && run.current_y != run.desired_y)
13658 {
13659 update_begin (f);
13660 rif->update_window_begin_hook (w);
13661 rif->clear_window_mouse_face (w);
13662 rif->scroll_run_hook (w, &run);
13663 rif->update_window_end_hook (w, 0, 0);
13664 update_end (f);
13665 }
13666
13667 /* Shift current matrix down by nrows_scrolled lines. */
13668 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13669 rotate_matrix (w->current_matrix,
13670 start_vpos,
13671 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13672 nrows_scrolled);
13673
13674 /* Disable lines that must be updated. */
13675 for (i = 0; i < it.vpos; ++i)
13676 (start_row + i)->enabled_p = 0;
13677
13678 /* Re-compute Y positions. */
13679 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13680 max_y = it.last_visible_y;
13681 for (row = start_row + nrows_scrolled;
13682 row < bottom_row;
13683 ++row)
13684 {
13685 row->y = it.current_y;
13686 row->visible_height = row->height;
13687
13688 if (row->y < min_y)
13689 row->visible_height -= min_y - row->y;
13690 if (row->y + row->height > max_y)
13691 row->visible_height -= row->y + row->height - max_y;
13692 row->redraw_fringe_bitmaps_p = 1;
13693
13694 it.current_y += row->height;
13695
13696 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13697 last_reused_text_row = row;
13698 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13699 break;
13700 }
13701
13702 /* Disable lines in the current matrix which are now
13703 below the window. */
13704 for (++row; row < bottom_row; ++row)
13705 row->enabled_p = row->mode_line_p = 0;
13706 }
13707
13708 /* Update window_end_pos etc.; last_reused_text_row is the last
13709 reused row from the current matrix containing text, if any.
13710 The value of last_text_row is the last displayed line
13711 containing text. */
13712 if (last_reused_text_row)
13713 {
13714 w->window_end_bytepos
13715 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13716 w->window_end_pos
13717 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13718 w->window_end_vpos
13719 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13720 w->current_matrix));
13721 }
13722 else if (last_text_row)
13723 {
13724 w->window_end_bytepos
13725 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13726 w->window_end_pos
13727 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13728 w->window_end_vpos
13729 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13730 }
13731 else
13732 {
13733 /* This window must be completely empty. */
13734 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13735 w->window_end_pos = make_number (Z - ZV);
13736 w->window_end_vpos = make_number (0);
13737 }
13738 w->window_end_valid = Qnil;
13739
13740 /* Update hint: don't try scrolling again in update_window. */
13741 w->desired_matrix->no_scrolling_p = 1;
13742
13743 #if GLYPH_DEBUG
13744 debug_method_add (w, "try_window_reusing_current_matrix 1");
13745 #endif
13746 return 1;
13747 }
13748 else if (CHARPOS (new_start) > CHARPOS (start))
13749 {
13750 struct glyph_row *pt_row, *row;
13751 struct glyph_row *first_reusable_row;
13752 struct glyph_row *first_row_to_display;
13753 int dy;
13754 int yb = window_text_bottom_y (w);
13755
13756 /* Find the row starting at new_start, if there is one. Don't
13757 reuse a partially visible line at the end. */
13758 first_reusable_row = start_row;
13759 while (first_reusable_row->enabled_p
13760 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13761 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13762 < CHARPOS (new_start)))
13763 ++first_reusable_row;
13764
13765 /* Give up if there is no row to reuse. */
13766 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13767 || !first_reusable_row->enabled_p
13768 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13769 != CHARPOS (new_start)))
13770 return 0;
13771
13772 /* We can reuse fully visible rows beginning with
13773 first_reusable_row to the end of the window. Set
13774 first_row_to_display to the first row that cannot be reused.
13775 Set pt_row to the row containing point, if there is any. */
13776 pt_row = NULL;
13777 for (first_row_to_display = first_reusable_row;
13778 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13779 ++first_row_to_display)
13780 {
13781 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13782 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13783 pt_row = first_row_to_display;
13784 }
13785
13786 /* Start displaying at the start of first_row_to_display. */
13787 xassert (first_row_to_display->y < yb);
13788 init_to_row_start (&it, w, first_row_to_display);
13789
13790 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13791 - start_vpos);
13792 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13793 - nrows_scrolled);
13794 it.current_y = (first_row_to_display->y - first_reusable_row->y
13795 + WINDOW_HEADER_LINE_HEIGHT (w));
13796
13797 /* Display lines beginning with first_row_to_display in the
13798 desired matrix. Set last_text_row to the last row displayed
13799 that displays text. */
13800 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13801 if (pt_row == NULL)
13802 w->cursor.vpos = -1;
13803 last_text_row = NULL;
13804 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13805 if (display_line (&it))
13806 last_text_row = it.glyph_row - 1;
13807
13808 /* Give up If point isn't in a row displayed or reused. */
13809 if (w->cursor.vpos < 0)
13810 {
13811 clear_glyph_matrix (w->desired_matrix);
13812 return 0;
13813 }
13814
13815 /* If point is in a reused row, adjust y and vpos of the cursor
13816 position. */
13817 if (pt_row)
13818 {
13819 w->cursor.vpos -= nrows_scrolled;
13820 w->cursor.y -= first_reusable_row->y - start_row->y;
13821 }
13822
13823 /* Scroll the display. */
13824 run.current_y = first_reusable_row->y;
13825 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13826 run.height = it.last_visible_y - run.current_y;
13827 dy = run.current_y - run.desired_y;
13828
13829 if (run.height)
13830 {
13831 update_begin (f);
13832 rif->update_window_begin_hook (w);
13833 rif->clear_window_mouse_face (w);
13834 rif->scroll_run_hook (w, &run);
13835 rif->update_window_end_hook (w, 0, 0);
13836 update_end (f);
13837 }
13838
13839 /* Adjust Y positions of reused rows. */
13840 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13841 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13842 max_y = it.last_visible_y;
13843 for (row = first_reusable_row; row < first_row_to_display; ++row)
13844 {
13845 row->y -= dy;
13846 row->visible_height = row->height;
13847 if (row->y < min_y)
13848 row->visible_height -= min_y - row->y;
13849 if (row->y + row->height > max_y)
13850 row->visible_height -= row->y + row->height - max_y;
13851 row->redraw_fringe_bitmaps_p = 1;
13852 }
13853
13854 /* Scroll the current matrix. */
13855 xassert (nrows_scrolled > 0);
13856 rotate_matrix (w->current_matrix,
13857 start_vpos,
13858 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13859 -nrows_scrolled);
13860
13861 /* Disable rows not reused. */
13862 for (row -= nrows_scrolled; row < bottom_row; ++row)
13863 row->enabled_p = 0;
13864
13865 /* Point may have moved to a different line, so we cannot assume that
13866 the previous cursor position is valid; locate the correct row. */
13867 if (pt_row)
13868 {
13869 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13870 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13871 row++)
13872 {
13873 w->cursor.vpos++;
13874 w->cursor.y = row->y;
13875 }
13876 if (row < bottom_row)
13877 {
13878 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13879 while (glyph->charpos < PT)
13880 {
13881 w->cursor.hpos++;
13882 w->cursor.x += glyph->pixel_width;
13883 glyph++;
13884 }
13885 }
13886 }
13887
13888 /* Adjust window end. A null value of last_text_row means that
13889 the window end is in reused rows which in turn means that
13890 only its vpos can have changed. */
13891 if (last_text_row)
13892 {
13893 w->window_end_bytepos
13894 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13895 w->window_end_pos
13896 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13897 w->window_end_vpos
13898 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13899 }
13900 else
13901 {
13902 w->window_end_vpos
13903 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13904 }
13905
13906 w->window_end_valid = Qnil;
13907 w->desired_matrix->no_scrolling_p = 1;
13908
13909 #if GLYPH_DEBUG
13910 debug_method_add (w, "try_window_reusing_current_matrix 2");
13911 #endif
13912 return 1;
13913 }
13914
13915 return 0;
13916 }
13917
13918
13919 \f
13920 /************************************************************************
13921 Window redisplay reusing current matrix when buffer has changed
13922 ************************************************************************/
13923
13924 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13925 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13926 int *, int *));
13927 static struct glyph_row *
13928 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13929 struct glyph_row *));
13930
13931
13932 /* Return the last row in MATRIX displaying text. If row START is
13933 non-null, start searching with that row. IT gives the dimensions
13934 of the display. Value is null if matrix is empty; otherwise it is
13935 a pointer to the row found. */
13936
13937 static struct glyph_row *
13938 find_last_row_displaying_text (matrix, it, start)
13939 struct glyph_matrix *matrix;
13940 struct it *it;
13941 struct glyph_row *start;
13942 {
13943 struct glyph_row *row, *row_found;
13944
13945 /* Set row_found to the last row in IT->w's current matrix
13946 displaying text. The loop looks funny but think of partially
13947 visible lines. */
13948 row_found = NULL;
13949 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13950 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13951 {
13952 xassert (row->enabled_p);
13953 row_found = row;
13954 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13955 break;
13956 ++row;
13957 }
13958
13959 return row_found;
13960 }
13961
13962
13963 /* Return the last row in the current matrix of W that is not affected
13964 by changes at the start of current_buffer that occurred since W's
13965 current matrix was built. Value is null if no such row exists.
13966
13967 BEG_UNCHANGED us the number of characters unchanged at the start of
13968 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13969 first changed character in current_buffer. Characters at positions <
13970 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13971 when the current matrix was built. */
13972
13973 static struct glyph_row *
13974 find_last_unchanged_at_beg_row (w)
13975 struct window *w;
13976 {
13977 int first_changed_pos = BEG + BEG_UNCHANGED;
13978 struct glyph_row *row;
13979 struct glyph_row *row_found = NULL;
13980 int yb = window_text_bottom_y (w);
13981
13982 /* Find the last row displaying unchanged text. */
13983 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13984 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13985 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13986 {
13987 if (/* If row ends before first_changed_pos, it is unchanged,
13988 except in some case. */
13989 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13990 /* When row ends in ZV and we write at ZV it is not
13991 unchanged. */
13992 && !row->ends_at_zv_p
13993 /* When first_changed_pos is the end of a continued line,
13994 row is not unchanged because it may be no longer
13995 continued. */
13996 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13997 && (row->continued_p
13998 || row->exact_window_width_line_p)))
13999 row_found = row;
14000
14001 /* Stop if last visible row. */
14002 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14003 break;
14004
14005 ++row;
14006 }
14007
14008 return row_found;
14009 }
14010
14011
14012 /* Find the first glyph row in the current matrix of W that is not
14013 affected by changes at the end of current_buffer since the
14014 time W's current matrix was built.
14015
14016 Return in *DELTA the number of chars by which buffer positions in
14017 unchanged text at the end of current_buffer must be adjusted.
14018
14019 Return in *DELTA_BYTES the corresponding number of bytes.
14020
14021 Value is null if no such row exists, i.e. all rows are affected by
14022 changes. */
14023
14024 static struct glyph_row *
14025 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14026 struct window *w;
14027 int *delta, *delta_bytes;
14028 {
14029 struct glyph_row *row;
14030 struct glyph_row *row_found = NULL;
14031
14032 *delta = *delta_bytes = 0;
14033
14034 /* Display must not have been paused, otherwise the current matrix
14035 is not up to date. */
14036 if (NILP (w->window_end_valid))
14037 abort ();
14038
14039 /* A value of window_end_pos >= END_UNCHANGED means that the window
14040 end is in the range of changed text. If so, there is no
14041 unchanged row at the end of W's current matrix. */
14042 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14043 return NULL;
14044
14045 /* Set row to the last row in W's current matrix displaying text. */
14046 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14047
14048 /* If matrix is entirely empty, no unchanged row exists. */
14049 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14050 {
14051 /* The value of row is the last glyph row in the matrix having a
14052 meaningful buffer position in it. The end position of row
14053 corresponds to window_end_pos. This allows us to translate
14054 buffer positions in the current matrix to current buffer
14055 positions for characters not in changed text. */
14056 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14057 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14058 int last_unchanged_pos, last_unchanged_pos_old;
14059 struct glyph_row *first_text_row
14060 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14061
14062 *delta = Z - Z_old;
14063 *delta_bytes = Z_BYTE - Z_BYTE_old;
14064
14065 /* Set last_unchanged_pos to the buffer position of the last
14066 character in the buffer that has not been changed. Z is the
14067 index + 1 of the last character in current_buffer, i.e. by
14068 subtracting END_UNCHANGED we get the index of the last
14069 unchanged character, and we have to add BEG to get its buffer
14070 position. */
14071 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14072 last_unchanged_pos_old = last_unchanged_pos - *delta;
14073
14074 /* Search backward from ROW for a row displaying a line that
14075 starts at a minimum position >= last_unchanged_pos_old. */
14076 for (; row > first_text_row; --row)
14077 {
14078 /* This used to abort, but it can happen.
14079 It is ok to just stop the search instead here. KFS. */
14080 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14081 break;
14082
14083 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14084 row_found = row;
14085 }
14086 }
14087
14088 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14089 abort ();
14090
14091 return row_found;
14092 }
14093
14094
14095 /* Make sure that glyph rows in the current matrix of window W
14096 reference the same glyph memory as corresponding rows in the
14097 frame's frame matrix. This function is called after scrolling W's
14098 current matrix on a terminal frame in try_window_id and
14099 try_window_reusing_current_matrix. */
14100
14101 static void
14102 sync_frame_with_window_matrix_rows (w)
14103 struct window *w;
14104 {
14105 struct frame *f = XFRAME (w->frame);
14106 struct glyph_row *window_row, *window_row_end, *frame_row;
14107
14108 /* Preconditions: W must be a leaf window and full-width. Its frame
14109 must have a frame matrix. */
14110 xassert (NILP (w->hchild) && NILP (w->vchild));
14111 xassert (WINDOW_FULL_WIDTH_P (w));
14112 xassert (!FRAME_WINDOW_P (f));
14113
14114 /* If W is a full-width window, glyph pointers in W's current matrix
14115 have, by definition, to be the same as glyph pointers in the
14116 corresponding frame matrix. Note that frame matrices have no
14117 marginal areas (see build_frame_matrix). */
14118 window_row = w->current_matrix->rows;
14119 window_row_end = window_row + w->current_matrix->nrows;
14120 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14121 while (window_row < window_row_end)
14122 {
14123 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14124 struct glyph *end = window_row->glyphs[LAST_AREA];
14125
14126 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14127 frame_row->glyphs[TEXT_AREA] = start;
14128 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14129 frame_row->glyphs[LAST_AREA] = end;
14130
14131 /* Disable frame rows whose corresponding window rows have
14132 been disabled in try_window_id. */
14133 if (!window_row->enabled_p)
14134 frame_row->enabled_p = 0;
14135
14136 ++window_row, ++frame_row;
14137 }
14138 }
14139
14140
14141 /* Find the glyph row in window W containing CHARPOS. Consider all
14142 rows between START and END (not inclusive). END null means search
14143 all rows to the end of the display area of W. Value is the row
14144 containing CHARPOS or null. */
14145
14146 struct glyph_row *
14147 row_containing_pos (w, charpos, start, end, dy)
14148 struct window *w;
14149 int charpos;
14150 struct glyph_row *start, *end;
14151 int dy;
14152 {
14153 struct glyph_row *row = start;
14154 int last_y;
14155
14156 /* If we happen to start on a header-line, skip that. */
14157 if (row->mode_line_p)
14158 ++row;
14159
14160 if ((end && row >= end) || !row->enabled_p)
14161 return NULL;
14162
14163 last_y = window_text_bottom_y (w) - dy;
14164
14165 while (1)
14166 {
14167 /* Give up if we have gone too far. */
14168 if (end && row >= end)
14169 return NULL;
14170 /* This formerly returned if they were equal.
14171 I think that both quantities are of a "last plus one" type;
14172 if so, when they are equal, the row is within the screen. -- rms. */
14173 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14174 return NULL;
14175
14176 /* If it is in this row, return this row. */
14177 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14178 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14179 /* The end position of a row equals the start
14180 position of the next row. If CHARPOS is there, we
14181 would rather display it in the next line, except
14182 when this line ends in ZV. */
14183 && !row->ends_at_zv_p
14184 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14185 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14186 return row;
14187 ++row;
14188 }
14189 }
14190
14191
14192 /* Try to redisplay window W by reusing its existing display. W's
14193 current matrix must be up to date when this function is called,
14194 i.e. window_end_valid must not be nil.
14195
14196 Value is
14197
14198 1 if display has been updated
14199 0 if otherwise unsuccessful
14200 -1 if redisplay with same window start is known not to succeed
14201
14202 The following steps are performed:
14203
14204 1. Find the last row in the current matrix of W that is not
14205 affected by changes at the start of current_buffer. If no such row
14206 is found, give up.
14207
14208 2. Find the first row in W's current matrix that is not affected by
14209 changes at the end of current_buffer. Maybe there is no such row.
14210
14211 3. Display lines beginning with the row + 1 found in step 1 to the
14212 row found in step 2 or, if step 2 didn't find a row, to the end of
14213 the window.
14214
14215 4. If cursor is not known to appear on the window, give up.
14216
14217 5. If display stopped at the row found in step 2, scroll the
14218 display and current matrix as needed.
14219
14220 6. Maybe display some lines at the end of W, if we must. This can
14221 happen under various circumstances, like a partially visible line
14222 becoming fully visible, or because newly displayed lines are displayed
14223 in smaller font sizes.
14224
14225 7. Update W's window end information. */
14226
14227 static int
14228 try_window_id (w)
14229 struct window *w;
14230 {
14231 struct frame *f = XFRAME (w->frame);
14232 struct glyph_matrix *current_matrix = w->current_matrix;
14233 struct glyph_matrix *desired_matrix = w->desired_matrix;
14234 struct glyph_row *last_unchanged_at_beg_row;
14235 struct glyph_row *first_unchanged_at_end_row;
14236 struct glyph_row *row;
14237 struct glyph_row *bottom_row;
14238 int bottom_vpos;
14239 struct it it;
14240 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14241 struct text_pos start_pos;
14242 struct run run;
14243 int first_unchanged_at_end_vpos = 0;
14244 struct glyph_row *last_text_row, *last_text_row_at_end;
14245 struct text_pos start;
14246 int first_changed_charpos, last_changed_charpos;
14247
14248 #if GLYPH_DEBUG
14249 if (inhibit_try_window_id)
14250 return 0;
14251 #endif
14252
14253 /* This is handy for debugging. */
14254 #if 0
14255 #define GIVE_UP(X) \
14256 do { \
14257 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14258 return 0; \
14259 } while (0)
14260 #else
14261 #define GIVE_UP(X) return 0
14262 #endif
14263
14264 SET_TEXT_POS_FROM_MARKER (start, w->start);
14265
14266 /* Don't use this for mini-windows because these can show
14267 messages and mini-buffers, and we don't handle that here. */
14268 if (MINI_WINDOW_P (w))
14269 GIVE_UP (1);
14270
14271 /* This flag is used to prevent redisplay optimizations. */
14272 if (windows_or_buffers_changed || cursor_type_changed)
14273 GIVE_UP (2);
14274
14275 /* Verify that narrowing has not changed.
14276 Also verify that we were not told to prevent redisplay optimizations.
14277 It would be nice to further
14278 reduce the number of cases where this prevents try_window_id. */
14279 if (current_buffer->clip_changed
14280 || current_buffer->prevent_redisplay_optimizations_p)
14281 GIVE_UP (3);
14282
14283 /* Window must either use window-based redisplay or be full width. */
14284 if (!FRAME_WINDOW_P (f)
14285 && (!line_ins_del_ok
14286 || !WINDOW_FULL_WIDTH_P (w)))
14287 GIVE_UP (4);
14288
14289 /* Give up if point is not known NOT to appear in W. */
14290 if (PT < CHARPOS (start))
14291 GIVE_UP (5);
14292
14293 /* Another way to prevent redisplay optimizations. */
14294 if (XFASTINT (w->last_modified) == 0)
14295 GIVE_UP (6);
14296
14297 /* Verify that window is not hscrolled. */
14298 if (XFASTINT (w->hscroll) != 0)
14299 GIVE_UP (7);
14300
14301 /* Verify that display wasn't paused. */
14302 if (NILP (w->window_end_valid))
14303 GIVE_UP (8);
14304
14305 /* Can't use this if highlighting a region because a cursor movement
14306 will do more than just set the cursor. */
14307 if (!NILP (Vtransient_mark_mode)
14308 && !NILP (current_buffer->mark_active))
14309 GIVE_UP (9);
14310
14311 /* Likewise if highlighting trailing whitespace. */
14312 if (!NILP (Vshow_trailing_whitespace))
14313 GIVE_UP (11);
14314
14315 /* Likewise if showing a region. */
14316 if (!NILP (w->region_showing))
14317 GIVE_UP (10);
14318
14319 /* Can use this if overlay arrow position and or string have changed. */
14320 if (overlay_arrows_changed_p ())
14321 GIVE_UP (12);
14322
14323
14324 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14325 only if buffer has really changed. The reason is that the gap is
14326 initially at Z for freshly visited files. The code below would
14327 set end_unchanged to 0 in that case. */
14328 if (MODIFF > SAVE_MODIFF
14329 /* This seems to happen sometimes after saving a buffer. */
14330 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14331 {
14332 if (GPT - BEG < BEG_UNCHANGED)
14333 BEG_UNCHANGED = GPT - BEG;
14334 if (Z - GPT < END_UNCHANGED)
14335 END_UNCHANGED = Z - GPT;
14336 }
14337
14338 /* The position of the first and last character that has been changed. */
14339 first_changed_charpos = BEG + BEG_UNCHANGED;
14340 last_changed_charpos = Z - END_UNCHANGED;
14341
14342 /* If window starts after a line end, and the last change is in
14343 front of that newline, then changes don't affect the display.
14344 This case happens with stealth-fontification. Note that although
14345 the display is unchanged, glyph positions in the matrix have to
14346 be adjusted, of course. */
14347 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14348 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14349 && ((last_changed_charpos < CHARPOS (start)
14350 && CHARPOS (start) == BEGV)
14351 || (last_changed_charpos < CHARPOS (start) - 1
14352 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14353 {
14354 int Z_old, delta, Z_BYTE_old, delta_bytes;
14355 struct glyph_row *r0;
14356
14357 /* Compute how many chars/bytes have been added to or removed
14358 from the buffer. */
14359 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14360 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14361 delta = Z - Z_old;
14362 delta_bytes = Z_BYTE - Z_BYTE_old;
14363
14364 /* Give up if PT is not in the window. Note that it already has
14365 been checked at the start of try_window_id that PT is not in
14366 front of the window start. */
14367 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14368 GIVE_UP (13);
14369
14370 /* If window start is unchanged, we can reuse the whole matrix
14371 as is, after adjusting glyph positions. No need to compute
14372 the window end again, since its offset from Z hasn't changed. */
14373 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14374 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14375 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14376 /* PT must not be in a partially visible line. */
14377 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14378 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14379 {
14380 /* Adjust positions in the glyph matrix. */
14381 if (delta || delta_bytes)
14382 {
14383 struct glyph_row *r1
14384 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14385 increment_matrix_positions (w->current_matrix,
14386 MATRIX_ROW_VPOS (r0, current_matrix),
14387 MATRIX_ROW_VPOS (r1, current_matrix),
14388 delta, delta_bytes);
14389 }
14390
14391 /* Set the cursor. */
14392 row = row_containing_pos (w, PT, r0, NULL, 0);
14393 if (row)
14394 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14395 else
14396 abort ();
14397 return 1;
14398 }
14399 }
14400
14401 /* Handle the case that changes are all below what is displayed in
14402 the window, and that PT is in the window. This shortcut cannot
14403 be taken if ZV is visible in the window, and text has been added
14404 there that is visible in the window. */
14405 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14406 /* ZV is not visible in the window, or there are no
14407 changes at ZV, actually. */
14408 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14409 || first_changed_charpos == last_changed_charpos))
14410 {
14411 struct glyph_row *r0;
14412
14413 /* Give up if PT is not in the window. Note that it already has
14414 been checked at the start of try_window_id that PT is not in
14415 front of the window start. */
14416 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14417 GIVE_UP (14);
14418
14419 /* If window start is unchanged, we can reuse the whole matrix
14420 as is, without changing glyph positions since no text has
14421 been added/removed in front of the window end. */
14422 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14423 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14424 /* PT must not be in a partially visible line. */
14425 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14426 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14427 {
14428 /* We have to compute the window end anew since text
14429 can have been added/removed after it. */
14430 w->window_end_pos
14431 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14432 w->window_end_bytepos
14433 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14434
14435 /* Set the cursor. */
14436 row = row_containing_pos (w, PT, r0, NULL, 0);
14437 if (row)
14438 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14439 else
14440 abort ();
14441 return 2;
14442 }
14443 }
14444
14445 /* Give up if window start is in the changed area.
14446
14447 The condition used to read
14448
14449 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14450
14451 but why that was tested escapes me at the moment. */
14452 if (CHARPOS (start) >= first_changed_charpos
14453 && CHARPOS (start) <= last_changed_charpos)
14454 GIVE_UP (15);
14455
14456 /* Check that window start agrees with the start of the first glyph
14457 row in its current matrix. Check this after we know the window
14458 start is not in changed text, otherwise positions would not be
14459 comparable. */
14460 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14461 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14462 GIVE_UP (16);
14463
14464 /* Give up if the window ends in strings. Overlay strings
14465 at the end are difficult to handle, so don't try. */
14466 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14467 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14468 GIVE_UP (20);
14469
14470 /* Compute the position at which we have to start displaying new
14471 lines. Some of the lines at the top of the window might be
14472 reusable because they are not displaying changed text. Find the
14473 last row in W's current matrix not affected by changes at the
14474 start of current_buffer. Value is null if changes start in the
14475 first line of window. */
14476 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14477 if (last_unchanged_at_beg_row)
14478 {
14479 /* Avoid starting to display in the moddle of a character, a TAB
14480 for instance. This is easier than to set up the iterator
14481 exactly, and it's not a frequent case, so the additional
14482 effort wouldn't really pay off. */
14483 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14484 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14485 && last_unchanged_at_beg_row > w->current_matrix->rows)
14486 --last_unchanged_at_beg_row;
14487
14488 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14489 GIVE_UP (17);
14490
14491 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14492 GIVE_UP (18);
14493 start_pos = it.current.pos;
14494
14495 /* Start displaying new lines in the desired matrix at the same
14496 vpos we would use in the current matrix, i.e. below
14497 last_unchanged_at_beg_row. */
14498 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14499 current_matrix);
14500 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14501 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14502
14503 xassert (it.hpos == 0 && it.current_x == 0);
14504 }
14505 else
14506 {
14507 /* There are no reusable lines at the start of the window.
14508 Start displaying in the first text line. */
14509 start_display (&it, w, start);
14510 it.vpos = it.first_vpos;
14511 start_pos = it.current.pos;
14512 }
14513
14514 /* Find the first row that is not affected by changes at the end of
14515 the buffer. Value will be null if there is no unchanged row, in
14516 which case we must redisplay to the end of the window. delta
14517 will be set to the value by which buffer positions beginning with
14518 first_unchanged_at_end_row have to be adjusted due to text
14519 changes. */
14520 first_unchanged_at_end_row
14521 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14522 IF_DEBUG (debug_delta = delta);
14523 IF_DEBUG (debug_delta_bytes = delta_bytes);
14524
14525 /* Set stop_pos to the buffer position up to which we will have to
14526 display new lines. If first_unchanged_at_end_row != NULL, this
14527 is the buffer position of the start of the line displayed in that
14528 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14529 that we don't stop at a buffer position. */
14530 stop_pos = 0;
14531 if (first_unchanged_at_end_row)
14532 {
14533 xassert (last_unchanged_at_beg_row == NULL
14534 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14535
14536 /* If this is a continuation line, move forward to the next one
14537 that isn't. Changes in lines above affect this line.
14538 Caution: this may move first_unchanged_at_end_row to a row
14539 not displaying text. */
14540 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14541 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14542 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14543 < it.last_visible_y))
14544 ++first_unchanged_at_end_row;
14545
14546 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14547 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14548 >= it.last_visible_y))
14549 first_unchanged_at_end_row = NULL;
14550 else
14551 {
14552 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14553 + delta);
14554 first_unchanged_at_end_vpos
14555 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14556 xassert (stop_pos >= Z - END_UNCHANGED);
14557 }
14558 }
14559 else if (last_unchanged_at_beg_row == NULL)
14560 GIVE_UP (19);
14561
14562
14563 #if GLYPH_DEBUG
14564
14565 /* Either there is no unchanged row at the end, or the one we have
14566 now displays text. This is a necessary condition for the window
14567 end pos calculation at the end of this function. */
14568 xassert (first_unchanged_at_end_row == NULL
14569 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14570
14571 debug_last_unchanged_at_beg_vpos
14572 = (last_unchanged_at_beg_row
14573 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14574 : -1);
14575 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14576
14577 #endif /* GLYPH_DEBUG != 0 */
14578
14579
14580 /* Display new lines. Set last_text_row to the last new line
14581 displayed which has text on it, i.e. might end up as being the
14582 line where the window_end_vpos is. */
14583 w->cursor.vpos = -1;
14584 last_text_row = NULL;
14585 overlay_arrow_seen = 0;
14586 while (it.current_y < it.last_visible_y
14587 && !fonts_changed_p
14588 && (first_unchanged_at_end_row == NULL
14589 || IT_CHARPOS (it) < stop_pos))
14590 {
14591 if (display_line (&it))
14592 last_text_row = it.glyph_row - 1;
14593 }
14594
14595 if (fonts_changed_p)
14596 return -1;
14597
14598
14599 /* Compute differences in buffer positions, y-positions etc. for
14600 lines reused at the bottom of the window. Compute what we can
14601 scroll. */
14602 if (first_unchanged_at_end_row
14603 /* No lines reused because we displayed everything up to the
14604 bottom of the window. */
14605 && it.current_y < it.last_visible_y)
14606 {
14607 dvpos = (it.vpos
14608 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14609 current_matrix));
14610 dy = it.current_y - first_unchanged_at_end_row->y;
14611 run.current_y = first_unchanged_at_end_row->y;
14612 run.desired_y = run.current_y + dy;
14613 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14614 }
14615 else
14616 {
14617 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14618 first_unchanged_at_end_row = NULL;
14619 }
14620 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14621
14622
14623 /* Find the cursor if not already found. We have to decide whether
14624 PT will appear on this window (it sometimes doesn't, but this is
14625 not a very frequent case.) This decision has to be made before
14626 the current matrix is altered. A value of cursor.vpos < 0 means
14627 that PT is either in one of the lines beginning at
14628 first_unchanged_at_end_row or below the window. Don't care for
14629 lines that might be displayed later at the window end; as
14630 mentioned, this is not a frequent case. */
14631 if (w->cursor.vpos < 0)
14632 {
14633 /* Cursor in unchanged rows at the top? */
14634 if (PT < CHARPOS (start_pos)
14635 && last_unchanged_at_beg_row)
14636 {
14637 row = row_containing_pos (w, PT,
14638 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14639 last_unchanged_at_beg_row + 1, 0);
14640 if (row)
14641 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14642 }
14643
14644 /* Start from first_unchanged_at_end_row looking for PT. */
14645 else if (first_unchanged_at_end_row)
14646 {
14647 row = row_containing_pos (w, PT - delta,
14648 first_unchanged_at_end_row, NULL, 0);
14649 if (row)
14650 set_cursor_from_row (w, row, w->current_matrix, delta,
14651 delta_bytes, dy, dvpos);
14652 }
14653
14654 /* Give up if cursor was not found. */
14655 if (w->cursor.vpos < 0)
14656 {
14657 clear_glyph_matrix (w->desired_matrix);
14658 return -1;
14659 }
14660 }
14661
14662 /* Don't let the cursor end in the scroll margins. */
14663 {
14664 int this_scroll_margin, cursor_height;
14665
14666 this_scroll_margin = max (0, scroll_margin);
14667 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14668 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14669 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14670
14671 if ((w->cursor.y < this_scroll_margin
14672 && CHARPOS (start) > BEGV)
14673 /* Old redisplay didn't take scroll margin into account at the bottom,
14674 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14675 || (w->cursor.y + (make_cursor_line_fully_visible_p
14676 ? cursor_height + this_scroll_margin
14677 : 1)) > it.last_visible_y)
14678 {
14679 w->cursor.vpos = -1;
14680 clear_glyph_matrix (w->desired_matrix);
14681 return -1;
14682 }
14683 }
14684
14685 /* Scroll the display. Do it before changing the current matrix so
14686 that xterm.c doesn't get confused about where the cursor glyph is
14687 found. */
14688 if (dy && run.height)
14689 {
14690 update_begin (f);
14691
14692 if (FRAME_WINDOW_P (f))
14693 {
14694 rif->update_window_begin_hook (w);
14695 rif->clear_window_mouse_face (w);
14696 rif->scroll_run_hook (w, &run);
14697 rif->update_window_end_hook (w, 0, 0);
14698 }
14699 else
14700 {
14701 /* Terminal frame. In this case, dvpos gives the number of
14702 lines to scroll by; dvpos < 0 means scroll up. */
14703 int first_unchanged_at_end_vpos
14704 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14705 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14706 int end = (WINDOW_TOP_EDGE_LINE (w)
14707 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14708 + window_internal_height (w));
14709
14710 /* Perform the operation on the screen. */
14711 if (dvpos > 0)
14712 {
14713 /* Scroll last_unchanged_at_beg_row to the end of the
14714 window down dvpos lines. */
14715 set_terminal_window (end);
14716
14717 /* On dumb terminals delete dvpos lines at the end
14718 before inserting dvpos empty lines. */
14719 if (!scroll_region_ok)
14720 ins_del_lines (end - dvpos, -dvpos);
14721
14722 /* Insert dvpos empty lines in front of
14723 last_unchanged_at_beg_row. */
14724 ins_del_lines (from, dvpos);
14725 }
14726 else if (dvpos < 0)
14727 {
14728 /* Scroll up last_unchanged_at_beg_vpos to the end of
14729 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14730 set_terminal_window (end);
14731
14732 /* Delete dvpos lines in front of
14733 last_unchanged_at_beg_vpos. ins_del_lines will set
14734 the cursor to the given vpos and emit |dvpos| delete
14735 line sequences. */
14736 ins_del_lines (from + dvpos, dvpos);
14737
14738 /* On a dumb terminal insert dvpos empty lines at the
14739 end. */
14740 if (!scroll_region_ok)
14741 ins_del_lines (end + dvpos, -dvpos);
14742 }
14743
14744 set_terminal_window (0);
14745 }
14746
14747 update_end (f);
14748 }
14749
14750 /* Shift reused rows of the current matrix to the right position.
14751 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14752 text. */
14753 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14754 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14755 if (dvpos < 0)
14756 {
14757 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14758 bottom_vpos, dvpos);
14759 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14760 bottom_vpos, 0);
14761 }
14762 else if (dvpos > 0)
14763 {
14764 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14765 bottom_vpos, dvpos);
14766 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14767 first_unchanged_at_end_vpos + dvpos, 0);
14768 }
14769
14770 /* For frame-based redisplay, make sure that current frame and window
14771 matrix are in sync with respect to glyph memory. */
14772 if (!FRAME_WINDOW_P (f))
14773 sync_frame_with_window_matrix_rows (w);
14774
14775 /* Adjust buffer positions in reused rows. */
14776 if (delta)
14777 increment_matrix_positions (current_matrix,
14778 first_unchanged_at_end_vpos + dvpos,
14779 bottom_vpos, delta, delta_bytes);
14780
14781 /* Adjust Y positions. */
14782 if (dy)
14783 shift_glyph_matrix (w, current_matrix,
14784 first_unchanged_at_end_vpos + dvpos,
14785 bottom_vpos, dy);
14786
14787 if (first_unchanged_at_end_row)
14788 {
14789 first_unchanged_at_end_row += dvpos;
14790 if (first_unchanged_at_end_row->y >= it.last_visible_y
14791 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14792 first_unchanged_at_end_row = NULL;
14793 }
14794
14795 /* If scrolling up, there may be some lines to display at the end of
14796 the window. */
14797 last_text_row_at_end = NULL;
14798 if (dy < 0)
14799 {
14800 /* Scrolling up can leave for example a partially visible line
14801 at the end of the window to be redisplayed. */
14802 /* Set last_row to the glyph row in the current matrix where the
14803 window end line is found. It has been moved up or down in
14804 the matrix by dvpos. */
14805 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14806 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14807
14808 /* If last_row is the window end line, it should display text. */
14809 xassert (last_row->displays_text_p);
14810
14811 /* If window end line was partially visible before, begin
14812 displaying at that line. Otherwise begin displaying with the
14813 line following it. */
14814 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14815 {
14816 init_to_row_start (&it, w, last_row);
14817 it.vpos = last_vpos;
14818 it.current_y = last_row->y;
14819 }
14820 else
14821 {
14822 init_to_row_end (&it, w, last_row);
14823 it.vpos = 1 + last_vpos;
14824 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14825 ++last_row;
14826 }
14827
14828 /* We may start in a continuation line. If so, we have to
14829 get the right continuation_lines_width and current_x. */
14830 it.continuation_lines_width = last_row->continuation_lines_width;
14831 it.hpos = it.current_x = 0;
14832
14833 /* Display the rest of the lines at the window end. */
14834 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14835 while (it.current_y < it.last_visible_y
14836 && !fonts_changed_p)
14837 {
14838 /* Is it always sure that the display agrees with lines in
14839 the current matrix? I don't think so, so we mark rows
14840 displayed invalid in the current matrix by setting their
14841 enabled_p flag to zero. */
14842 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14843 if (display_line (&it))
14844 last_text_row_at_end = it.glyph_row - 1;
14845 }
14846 }
14847
14848 /* Update window_end_pos and window_end_vpos. */
14849 if (first_unchanged_at_end_row
14850 && !last_text_row_at_end)
14851 {
14852 /* Window end line if one of the preserved rows from the current
14853 matrix. Set row to the last row displaying text in current
14854 matrix starting at first_unchanged_at_end_row, after
14855 scrolling. */
14856 xassert (first_unchanged_at_end_row->displays_text_p);
14857 row = find_last_row_displaying_text (w->current_matrix, &it,
14858 first_unchanged_at_end_row);
14859 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14860
14861 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14862 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14863 w->window_end_vpos
14864 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14865 xassert (w->window_end_bytepos >= 0);
14866 IF_DEBUG (debug_method_add (w, "A"));
14867 }
14868 else if (last_text_row_at_end)
14869 {
14870 w->window_end_pos
14871 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14872 w->window_end_bytepos
14873 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14874 w->window_end_vpos
14875 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14876 xassert (w->window_end_bytepos >= 0);
14877 IF_DEBUG (debug_method_add (w, "B"));
14878 }
14879 else if (last_text_row)
14880 {
14881 /* We have displayed either to the end of the window or at the
14882 end of the window, i.e. the last row with text is to be found
14883 in the desired matrix. */
14884 w->window_end_pos
14885 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14886 w->window_end_bytepos
14887 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14888 w->window_end_vpos
14889 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14890 xassert (w->window_end_bytepos >= 0);
14891 }
14892 else if (first_unchanged_at_end_row == NULL
14893 && last_text_row == NULL
14894 && last_text_row_at_end == NULL)
14895 {
14896 /* Displayed to end of window, but no line containing text was
14897 displayed. Lines were deleted at the end of the window. */
14898 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14899 int vpos = XFASTINT (w->window_end_vpos);
14900 struct glyph_row *current_row = current_matrix->rows + vpos;
14901 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14902
14903 for (row = NULL;
14904 row == NULL && vpos >= first_vpos;
14905 --vpos, --current_row, --desired_row)
14906 {
14907 if (desired_row->enabled_p)
14908 {
14909 if (desired_row->displays_text_p)
14910 row = desired_row;
14911 }
14912 else if (current_row->displays_text_p)
14913 row = current_row;
14914 }
14915
14916 xassert (row != NULL);
14917 w->window_end_vpos = make_number (vpos + 1);
14918 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14919 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14920 xassert (w->window_end_bytepos >= 0);
14921 IF_DEBUG (debug_method_add (w, "C"));
14922 }
14923 else
14924 abort ();
14925
14926 #if 0 /* This leads to problems, for instance when the cursor is
14927 at ZV, and the cursor line displays no text. */
14928 /* Disable rows below what's displayed in the window. This makes
14929 debugging easier. */
14930 enable_glyph_matrix_rows (current_matrix,
14931 XFASTINT (w->window_end_vpos) + 1,
14932 bottom_vpos, 0);
14933 #endif
14934
14935 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14936 debug_end_vpos = XFASTINT (w->window_end_vpos));
14937
14938 /* Record that display has not been completed. */
14939 w->window_end_valid = Qnil;
14940 w->desired_matrix->no_scrolling_p = 1;
14941 return 3;
14942
14943 #undef GIVE_UP
14944 }
14945
14946
14947 \f
14948 /***********************************************************************
14949 More debugging support
14950 ***********************************************************************/
14951
14952 #if GLYPH_DEBUG
14953
14954 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14955 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14956 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14957
14958
14959 /* Dump the contents of glyph matrix MATRIX on stderr.
14960
14961 GLYPHS 0 means don't show glyph contents.
14962 GLYPHS 1 means show glyphs in short form
14963 GLYPHS > 1 means show glyphs in long form. */
14964
14965 void
14966 dump_glyph_matrix (matrix, glyphs)
14967 struct glyph_matrix *matrix;
14968 int glyphs;
14969 {
14970 int i;
14971 for (i = 0; i < matrix->nrows; ++i)
14972 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14973 }
14974
14975
14976 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14977 the glyph row and area where the glyph comes from. */
14978
14979 void
14980 dump_glyph (row, glyph, area)
14981 struct glyph_row *row;
14982 struct glyph *glyph;
14983 int area;
14984 {
14985 if (glyph->type == CHAR_GLYPH)
14986 {
14987 fprintf (stderr,
14988 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14989 glyph - row->glyphs[TEXT_AREA],
14990 'C',
14991 glyph->charpos,
14992 (BUFFERP (glyph->object)
14993 ? 'B'
14994 : (STRINGP (glyph->object)
14995 ? 'S'
14996 : '-')),
14997 glyph->pixel_width,
14998 glyph->u.ch,
14999 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15000 ? glyph->u.ch
15001 : '.'),
15002 glyph->face_id,
15003 glyph->left_box_line_p,
15004 glyph->right_box_line_p);
15005 }
15006 else if (glyph->type == STRETCH_GLYPH)
15007 {
15008 fprintf (stderr,
15009 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15010 glyph - row->glyphs[TEXT_AREA],
15011 'S',
15012 glyph->charpos,
15013 (BUFFERP (glyph->object)
15014 ? 'B'
15015 : (STRINGP (glyph->object)
15016 ? 'S'
15017 : '-')),
15018 glyph->pixel_width,
15019 0,
15020 '.',
15021 glyph->face_id,
15022 glyph->left_box_line_p,
15023 glyph->right_box_line_p);
15024 }
15025 else if (glyph->type == IMAGE_GLYPH)
15026 {
15027 fprintf (stderr,
15028 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15029 glyph - row->glyphs[TEXT_AREA],
15030 'I',
15031 glyph->charpos,
15032 (BUFFERP (glyph->object)
15033 ? 'B'
15034 : (STRINGP (glyph->object)
15035 ? 'S'
15036 : '-')),
15037 glyph->pixel_width,
15038 glyph->u.img_id,
15039 '.',
15040 glyph->face_id,
15041 glyph->left_box_line_p,
15042 glyph->right_box_line_p);
15043 }
15044 else if (glyph->type == COMPOSITE_GLYPH)
15045 {
15046 fprintf (stderr,
15047 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15048 glyph - row->glyphs[TEXT_AREA],
15049 '+',
15050 glyph->charpos,
15051 (BUFFERP (glyph->object)
15052 ? 'B'
15053 : (STRINGP (glyph->object)
15054 ? 'S'
15055 : '-')),
15056 glyph->pixel_width,
15057 glyph->u.cmp_id,
15058 '.',
15059 glyph->face_id,
15060 glyph->left_box_line_p,
15061 glyph->right_box_line_p);
15062 }
15063 }
15064
15065
15066 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15067 GLYPHS 0 means don't show glyph contents.
15068 GLYPHS 1 means show glyphs in short form
15069 GLYPHS > 1 means show glyphs in long form. */
15070
15071 void
15072 dump_glyph_row (row, vpos, glyphs)
15073 struct glyph_row *row;
15074 int vpos, glyphs;
15075 {
15076 if (glyphs != 1)
15077 {
15078 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15079 fprintf (stderr, "======================================================================\n");
15080
15081 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15082 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15083 vpos,
15084 MATRIX_ROW_START_CHARPOS (row),
15085 MATRIX_ROW_END_CHARPOS (row),
15086 row->used[TEXT_AREA],
15087 row->contains_overlapping_glyphs_p,
15088 row->enabled_p,
15089 row->truncated_on_left_p,
15090 row->truncated_on_right_p,
15091 row->continued_p,
15092 MATRIX_ROW_CONTINUATION_LINE_P (row),
15093 row->displays_text_p,
15094 row->ends_at_zv_p,
15095 row->fill_line_p,
15096 row->ends_in_middle_of_char_p,
15097 row->starts_in_middle_of_char_p,
15098 row->mouse_face_p,
15099 row->x,
15100 row->y,
15101 row->pixel_width,
15102 row->height,
15103 row->visible_height,
15104 row->ascent,
15105 row->phys_ascent);
15106 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15107 row->end.overlay_string_index,
15108 row->continuation_lines_width);
15109 fprintf (stderr, "%9d %5d\n",
15110 CHARPOS (row->start.string_pos),
15111 CHARPOS (row->end.string_pos));
15112 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15113 row->end.dpvec_index);
15114 }
15115
15116 if (glyphs > 1)
15117 {
15118 int area;
15119
15120 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15121 {
15122 struct glyph *glyph = row->glyphs[area];
15123 struct glyph *glyph_end = glyph + row->used[area];
15124
15125 /* Glyph for a line end in text. */
15126 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15127 ++glyph_end;
15128
15129 if (glyph < glyph_end)
15130 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15131
15132 for (; glyph < glyph_end; ++glyph)
15133 dump_glyph (row, glyph, area);
15134 }
15135 }
15136 else if (glyphs == 1)
15137 {
15138 int area;
15139
15140 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15141 {
15142 char *s = (char *) alloca (row->used[area] + 1);
15143 int i;
15144
15145 for (i = 0; i < row->used[area]; ++i)
15146 {
15147 struct glyph *glyph = row->glyphs[area] + i;
15148 if (glyph->type == CHAR_GLYPH
15149 && glyph->u.ch < 0x80
15150 && glyph->u.ch >= ' ')
15151 s[i] = glyph->u.ch;
15152 else
15153 s[i] = '.';
15154 }
15155
15156 s[i] = '\0';
15157 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15158 }
15159 }
15160 }
15161
15162
15163 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15164 Sdump_glyph_matrix, 0, 1, "p",
15165 doc: /* Dump the current matrix of the selected window to stderr.
15166 Shows contents of glyph row structures. With non-nil
15167 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15168 glyphs in short form, otherwise show glyphs in long form. */)
15169 (glyphs)
15170 Lisp_Object glyphs;
15171 {
15172 struct window *w = XWINDOW (selected_window);
15173 struct buffer *buffer = XBUFFER (w->buffer);
15174
15175 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15176 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15177 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15178 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15179 fprintf (stderr, "=============================================\n");
15180 dump_glyph_matrix (w->current_matrix,
15181 NILP (glyphs) ? 0 : XINT (glyphs));
15182 return Qnil;
15183 }
15184
15185
15186 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15187 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15188 ()
15189 {
15190 struct frame *f = XFRAME (selected_frame);
15191 dump_glyph_matrix (f->current_matrix, 1);
15192 return Qnil;
15193 }
15194
15195
15196 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15197 doc: /* Dump glyph row ROW to stderr.
15198 GLYPH 0 means don't dump glyphs.
15199 GLYPH 1 means dump glyphs in short form.
15200 GLYPH > 1 or omitted means dump glyphs in long form. */)
15201 (row, glyphs)
15202 Lisp_Object row, glyphs;
15203 {
15204 struct glyph_matrix *matrix;
15205 int vpos;
15206
15207 CHECK_NUMBER (row);
15208 matrix = XWINDOW (selected_window)->current_matrix;
15209 vpos = XINT (row);
15210 if (vpos >= 0 && vpos < matrix->nrows)
15211 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15212 vpos,
15213 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15214 return Qnil;
15215 }
15216
15217
15218 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15219 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15220 GLYPH 0 means don't dump glyphs.
15221 GLYPH 1 means dump glyphs in short form.
15222 GLYPH > 1 or omitted means dump glyphs in long form. */)
15223 (row, glyphs)
15224 Lisp_Object row, glyphs;
15225 {
15226 struct frame *sf = SELECTED_FRAME ();
15227 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15228 int vpos;
15229
15230 CHECK_NUMBER (row);
15231 vpos = XINT (row);
15232 if (vpos >= 0 && vpos < m->nrows)
15233 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15234 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15235 return Qnil;
15236 }
15237
15238
15239 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15240 doc: /* Toggle tracing of redisplay.
15241 With ARG, turn tracing on if and only if ARG is positive. */)
15242 (arg)
15243 Lisp_Object arg;
15244 {
15245 if (NILP (arg))
15246 trace_redisplay_p = !trace_redisplay_p;
15247 else
15248 {
15249 arg = Fprefix_numeric_value (arg);
15250 trace_redisplay_p = XINT (arg) > 0;
15251 }
15252
15253 return Qnil;
15254 }
15255
15256
15257 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15258 doc: /* Like `format', but print result to stderr.
15259 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15260 (nargs, args)
15261 int nargs;
15262 Lisp_Object *args;
15263 {
15264 Lisp_Object s = Fformat (nargs, args);
15265 fprintf (stderr, "%s", SDATA (s));
15266 return Qnil;
15267 }
15268
15269 #endif /* GLYPH_DEBUG */
15270
15271
15272 \f
15273 /***********************************************************************
15274 Building Desired Matrix Rows
15275 ***********************************************************************/
15276
15277 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15278 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15279
15280 static struct glyph_row *
15281 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15282 struct window *w;
15283 Lisp_Object overlay_arrow_string;
15284 {
15285 struct frame *f = XFRAME (WINDOW_FRAME (w));
15286 struct buffer *buffer = XBUFFER (w->buffer);
15287 struct buffer *old = current_buffer;
15288 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15289 int arrow_len = SCHARS (overlay_arrow_string);
15290 const unsigned char *arrow_end = arrow_string + arrow_len;
15291 const unsigned char *p;
15292 struct it it;
15293 int multibyte_p;
15294 int n_glyphs_before;
15295
15296 set_buffer_temp (buffer);
15297 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15298 it.glyph_row->used[TEXT_AREA] = 0;
15299 SET_TEXT_POS (it.position, 0, 0);
15300
15301 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15302 p = arrow_string;
15303 while (p < arrow_end)
15304 {
15305 Lisp_Object face, ilisp;
15306
15307 /* Get the next character. */
15308 if (multibyte_p)
15309 it.c = string_char_and_length (p, arrow_len, &it.len);
15310 else
15311 it.c = *p, it.len = 1;
15312 p += it.len;
15313
15314 /* Get its face. */
15315 ilisp = make_number (p - arrow_string);
15316 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15317 it.face_id = compute_char_face (f, it.c, face);
15318
15319 /* Compute its width, get its glyphs. */
15320 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15321 SET_TEXT_POS (it.position, -1, -1);
15322 PRODUCE_GLYPHS (&it);
15323
15324 /* If this character doesn't fit any more in the line, we have
15325 to remove some glyphs. */
15326 if (it.current_x > it.last_visible_x)
15327 {
15328 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15329 break;
15330 }
15331 }
15332
15333 set_buffer_temp (old);
15334 return it.glyph_row;
15335 }
15336
15337
15338 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15339 glyphs are only inserted for terminal frames since we can't really
15340 win with truncation glyphs when partially visible glyphs are
15341 involved. Which glyphs to insert is determined by
15342 produce_special_glyphs. */
15343
15344 static void
15345 insert_left_trunc_glyphs (it)
15346 struct it *it;
15347 {
15348 struct it truncate_it;
15349 struct glyph *from, *end, *to, *toend;
15350
15351 xassert (!FRAME_WINDOW_P (it->f));
15352
15353 /* Get the truncation glyphs. */
15354 truncate_it = *it;
15355 truncate_it.current_x = 0;
15356 truncate_it.face_id = DEFAULT_FACE_ID;
15357 truncate_it.glyph_row = &scratch_glyph_row;
15358 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15359 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15360 truncate_it.object = make_number (0);
15361 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15362
15363 /* Overwrite glyphs from IT with truncation glyphs. */
15364 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15365 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15366 to = it->glyph_row->glyphs[TEXT_AREA];
15367 toend = to + it->glyph_row->used[TEXT_AREA];
15368
15369 while (from < end)
15370 *to++ = *from++;
15371
15372 /* There may be padding glyphs left over. Overwrite them too. */
15373 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15374 {
15375 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15376 while (from < end)
15377 *to++ = *from++;
15378 }
15379
15380 if (to > toend)
15381 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15382 }
15383
15384
15385 /* Compute the pixel height and width of IT->glyph_row.
15386
15387 Most of the time, ascent and height of a display line will be equal
15388 to the max_ascent and max_height values of the display iterator
15389 structure. This is not the case if
15390
15391 1. We hit ZV without displaying anything. In this case, max_ascent
15392 and max_height will be zero.
15393
15394 2. We have some glyphs that don't contribute to the line height.
15395 (The glyph row flag contributes_to_line_height_p is for future
15396 pixmap extensions).
15397
15398 The first case is easily covered by using default values because in
15399 these cases, the line height does not really matter, except that it
15400 must not be zero. */
15401
15402 static void
15403 compute_line_metrics (it)
15404 struct it *it;
15405 {
15406 struct glyph_row *row = it->glyph_row;
15407 int area, i;
15408
15409 if (FRAME_WINDOW_P (it->f))
15410 {
15411 int i, min_y, max_y;
15412
15413 /* The line may consist of one space only, that was added to
15414 place the cursor on it. If so, the row's height hasn't been
15415 computed yet. */
15416 if (row->height == 0)
15417 {
15418 if (it->max_ascent + it->max_descent == 0)
15419 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15420 row->ascent = it->max_ascent;
15421 row->height = it->max_ascent + it->max_descent;
15422 row->phys_ascent = it->max_phys_ascent;
15423 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15424 row->extra_line_spacing = it->max_extra_line_spacing;
15425 }
15426
15427 /* Compute the width of this line. */
15428 row->pixel_width = row->x;
15429 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15430 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15431
15432 xassert (row->pixel_width >= 0);
15433 xassert (row->ascent >= 0 && row->height > 0);
15434
15435 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15436 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15437
15438 /* If first line's physical ascent is larger than its logical
15439 ascent, use the physical ascent, and make the row taller.
15440 This makes accented characters fully visible. */
15441 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15442 && row->phys_ascent > row->ascent)
15443 {
15444 row->height += row->phys_ascent - row->ascent;
15445 row->ascent = row->phys_ascent;
15446 }
15447
15448 /* Compute how much of the line is visible. */
15449 row->visible_height = row->height;
15450
15451 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15452 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15453
15454 if (row->y < min_y)
15455 row->visible_height -= min_y - row->y;
15456 if (row->y + row->height > max_y)
15457 row->visible_height -= row->y + row->height - max_y;
15458 }
15459 else
15460 {
15461 row->pixel_width = row->used[TEXT_AREA];
15462 if (row->continued_p)
15463 row->pixel_width -= it->continuation_pixel_width;
15464 else if (row->truncated_on_right_p)
15465 row->pixel_width -= it->truncation_pixel_width;
15466 row->ascent = row->phys_ascent = 0;
15467 row->height = row->phys_height = row->visible_height = 1;
15468 row->extra_line_spacing = 0;
15469 }
15470
15471 /* Compute a hash code for this row. */
15472 row->hash = 0;
15473 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15474 for (i = 0; i < row->used[area]; ++i)
15475 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15476 + row->glyphs[area][i].u.val
15477 + row->glyphs[area][i].face_id
15478 + row->glyphs[area][i].padding_p
15479 + (row->glyphs[area][i].type << 2));
15480
15481 it->max_ascent = it->max_descent = 0;
15482 it->max_phys_ascent = it->max_phys_descent = 0;
15483 }
15484
15485
15486 /* Append one space to the glyph row of iterator IT if doing a
15487 window-based redisplay. The space has the same face as
15488 IT->face_id. Value is non-zero if a space was added.
15489
15490 This function is called to make sure that there is always one glyph
15491 at the end of a glyph row that the cursor can be set on under
15492 window-systems. (If there weren't such a glyph we would not know
15493 how wide and tall a box cursor should be displayed).
15494
15495 At the same time this space let's a nicely handle clearing to the
15496 end of the line if the row ends in italic text. */
15497
15498 static int
15499 append_space_for_newline (it, default_face_p)
15500 struct it *it;
15501 int default_face_p;
15502 {
15503 if (FRAME_WINDOW_P (it->f))
15504 {
15505 int n = it->glyph_row->used[TEXT_AREA];
15506
15507 if (it->glyph_row->glyphs[TEXT_AREA] + n
15508 < it->glyph_row->glyphs[1 + TEXT_AREA])
15509 {
15510 /* Save some values that must not be changed.
15511 Must save IT->c and IT->len because otherwise
15512 ITERATOR_AT_END_P wouldn't work anymore after
15513 append_space_for_newline has been called. */
15514 enum display_element_type saved_what = it->what;
15515 int saved_c = it->c, saved_len = it->len;
15516 int saved_x = it->current_x;
15517 int saved_face_id = it->face_id;
15518 struct text_pos saved_pos;
15519 Lisp_Object saved_object;
15520 struct face *face;
15521
15522 saved_object = it->object;
15523 saved_pos = it->position;
15524
15525 it->what = IT_CHARACTER;
15526 bzero (&it->position, sizeof it->position);
15527 it->object = make_number (0);
15528 it->c = ' ';
15529 it->len = 1;
15530
15531 if (default_face_p)
15532 it->face_id = DEFAULT_FACE_ID;
15533 else if (it->face_before_selective_p)
15534 it->face_id = it->saved_face_id;
15535 face = FACE_FROM_ID (it->f, it->face_id);
15536 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15537
15538 PRODUCE_GLYPHS (it);
15539
15540 it->override_ascent = -1;
15541 it->constrain_row_ascent_descent_p = 0;
15542 it->current_x = saved_x;
15543 it->object = saved_object;
15544 it->position = saved_pos;
15545 it->what = saved_what;
15546 it->face_id = saved_face_id;
15547 it->len = saved_len;
15548 it->c = saved_c;
15549 return 1;
15550 }
15551 }
15552
15553 return 0;
15554 }
15555
15556
15557 /* Extend the face of the last glyph in the text area of IT->glyph_row
15558 to the end of the display line. Called from display_line.
15559 If the glyph row is empty, add a space glyph to it so that we
15560 know the face to draw. Set the glyph row flag fill_line_p. */
15561
15562 static void
15563 extend_face_to_end_of_line (it)
15564 struct it *it;
15565 {
15566 struct face *face;
15567 struct frame *f = it->f;
15568
15569 /* If line is already filled, do nothing. */
15570 if (it->current_x >= it->last_visible_x)
15571 return;
15572
15573 /* Face extension extends the background and box of IT->face_id
15574 to the end of the line. If the background equals the background
15575 of the frame, we don't have to do anything. */
15576 if (it->face_before_selective_p)
15577 face = FACE_FROM_ID (it->f, it->saved_face_id);
15578 else
15579 face = FACE_FROM_ID (f, it->face_id);
15580
15581 if (FRAME_WINDOW_P (f)
15582 && it->glyph_row->displays_text_p
15583 && face->box == FACE_NO_BOX
15584 && face->background == FRAME_BACKGROUND_PIXEL (f)
15585 && !face->stipple)
15586 return;
15587
15588 /* Set the glyph row flag indicating that the face of the last glyph
15589 in the text area has to be drawn to the end of the text area. */
15590 it->glyph_row->fill_line_p = 1;
15591
15592 /* If current character of IT is not ASCII, make sure we have the
15593 ASCII face. This will be automatically undone the next time
15594 get_next_display_element returns a multibyte character. Note
15595 that the character will always be single byte in unibyte text. */
15596 if (!SINGLE_BYTE_CHAR_P (it->c))
15597 {
15598 it->face_id = FACE_FOR_CHAR (f, face, 0);
15599 }
15600
15601 if (FRAME_WINDOW_P (f))
15602 {
15603 /* If the row is empty, add a space with the current face of IT,
15604 so that we know which face to draw. */
15605 if (it->glyph_row->used[TEXT_AREA] == 0)
15606 {
15607 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15608 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15609 it->glyph_row->used[TEXT_AREA] = 1;
15610 }
15611 }
15612 else
15613 {
15614 /* Save some values that must not be changed. */
15615 int saved_x = it->current_x;
15616 struct text_pos saved_pos;
15617 Lisp_Object saved_object;
15618 enum display_element_type saved_what = it->what;
15619 int saved_face_id = it->face_id;
15620
15621 saved_object = it->object;
15622 saved_pos = it->position;
15623
15624 it->what = IT_CHARACTER;
15625 bzero (&it->position, sizeof it->position);
15626 it->object = make_number (0);
15627 it->c = ' ';
15628 it->len = 1;
15629 it->face_id = face->id;
15630
15631 PRODUCE_GLYPHS (it);
15632
15633 while (it->current_x <= it->last_visible_x)
15634 PRODUCE_GLYPHS (it);
15635
15636 /* Don't count these blanks really. It would let us insert a left
15637 truncation glyph below and make us set the cursor on them, maybe. */
15638 it->current_x = saved_x;
15639 it->object = saved_object;
15640 it->position = saved_pos;
15641 it->what = saved_what;
15642 it->face_id = saved_face_id;
15643 }
15644 }
15645
15646
15647 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15648 trailing whitespace. */
15649
15650 static int
15651 trailing_whitespace_p (charpos)
15652 int charpos;
15653 {
15654 int bytepos = CHAR_TO_BYTE (charpos);
15655 int c = 0;
15656
15657 while (bytepos < ZV_BYTE
15658 && (c = FETCH_CHAR (bytepos),
15659 c == ' ' || c == '\t'))
15660 ++bytepos;
15661
15662 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15663 {
15664 if (bytepos != PT_BYTE)
15665 return 1;
15666 }
15667 return 0;
15668 }
15669
15670
15671 /* Highlight trailing whitespace, if any, in ROW. */
15672
15673 void
15674 highlight_trailing_whitespace (f, row)
15675 struct frame *f;
15676 struct glyph_row *row;
15677 {
15678 int used = row->used[TEXT_AREA];
15679
15680 if (used)
15681 {
15682 struct glyph *start = row->glyphs[TEXT_AREA];
15683 struct glyph *glyph = start + used - 1;
15684
15685 /* Skip over glyphs inserted to display the cursor at the
15686 end of a line, for extending the face of the last glyph
15687 to the end of the line on terminals, and for truncation
15688 and continuation glyphs. */
15689 while (glyph >= start
15690 && glyph->type == CHAR_GLYPH
15691 && INTEGERP (glyph->object))
15692 --glyph;
15693
15694 /* If last glyph is a space or stretch, and it's trailing
15695 whitespace, set the face of all trailing whitespace glyphs in
15696 IT->glyph_row to `trailing-whitespace'. */
15697 if (glyph >= start
15698 && BUFFERP (glyph->object)
15699 && (glyph->type == STRETCH_GLYPH
15700 || (glyph->type == CHAR_GLYPH
15701 && glyph->u.ch == ' '))
15702 && trailing_whitespace_p (glyph->charpos))
15703 {
15704 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15705 if (face_id < 0)
15706 return;
15707
15708 while (glyph >= start
15709 && BUFFERP (glyph->object)
15710 && (glyph->type == STRETCH_GLYPH
15711 || (glyph->type == CHAR_GLYPH
15712 && glyph->u.ch == ' ')))
15713 (glyph--)->face_id = face_id;
15714 }
15715 }
15716 }
15717
15718
15719 /* Value is non-zero if glyph row ROW in window W should be
15720 used to hold the cursor. */
15721
15722 static int
15723 cursor_row_p (w, row)
15724 struct window *w;
15725 struct glyph_row *row;
15726 {
15727 int cursor_row_p = 1;
15728
15729 if (PT == MATRIX_ROW_END_CHARPOS (row))
15730 {
15731 /* If the row ends with a newline from a string, we don't want
15732 the cursor there, but we still want it at the start of the
15733 string if the string starts in this row.
15734 If the row is continued it doesn't end in a newline. */
15735 if (CHARPOS (row->end.string_pos) >= 0)
15736 cursor_row_p = (row->continued_p
15737 || PT >= MATRIX_ROW_START_CHARPOS (row));
15738 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15739 {
15740 /* If the row ends in middle of a real character,
15741 and the line is continued, we want the cursor here.
15742 That's because MATRIX_ROW_END_CHARPOS would equal
15743 PT if PT is before the character. */
15744 if (!row->ends_in_ellipsis_p)
15745 cursor_row_p = row->continued_p;
15746 else
15747 /* If the row ends in an ellipsis, then
15748 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15749 We want that position to be displayed after the ellipsis. */
15750 cursor_row_p = 0;
15751 }
15752 /* If the row ends at ZV, display the cursor at the end of that
15753 row instead of at the start of the row below. */
15754 else if (row->ends_at_zv_p)
15755 cursor_row_p = 1;
15756 else
15757 cursor_row_p = 0;
15758 }
15759
15760 return cursor_row_p;
15761 }
15762
15763
15764 /* Construct the glyph row IT->glyph_row in the desired matrix of
15765 IT->w from text at the current position of IT. See dispextern.h
15766 for an overview of struct it. Value is non-zero if
15767 IT->glyph_row displays text, as opposed to a line displaying ZV
15768 only. */
15769
15770 static int
15771 display_line (it)
15772 struct it *it;
15773 {
15774 struct glyph_row *row = it->glyph_row;
15775 Lisp_Object overlay_arrow_string;
15776
15777 /* We always start displaying at hpos zero even if hscrolled. */
15778 xassert (it->hpos == 0 && it->current_x == 0);
15779
15780 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15781 >= it->w->desired_matrix->nrows)
15782 {
15783 it->w->nrows_scale_factor++;
15784 fonts_changed_p = 1;
15785 return 0;
15786 }
15787
15788 /* Is IT->w showing the region? */
15789 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15790
15791 /* Clear the result glyph row and enable it. */
15792 prepare_desired_row (row);
15793
15794 row->y = it->current_y;
15795 row->start = it->start;
15796 row->continuation_lines_width = it->continuation_lines_width;
15797 row->displays_text_p = 1;
15798 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15799 it->starts_in_middle_of_char_p = 0;
15800
15801 /* Arrange the overlays nicely for our purposes. Usually, we call
15802 display_line on only one line at a time, in which case this
15803 can't really hurt too much, or we call it on lines which appear
15804 one after another in the buffer, in which case all calls to
15805 recenter_overlay_lists but the first will be pretty cheap. */
15806 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15807
15808 /* Move over display elements that are not visible because we are
15809 hscrolled. This may stop at an x-position < IT->first_visible_x
15810 if the first glyph is partially visible or if we hit a line end. */
15811 if (it->current_x < it->first_visible_x)
15812 {
15813 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15814 MOVE_TO_POS | MOVE_TO_X);
15815 }
15816
15817 /* Get the initial row height. This is either the height of the
15818 text hscrolled, if there is any, or zero. */
15819 row->ascent = it->max_ascent;
15820 row->height = it->max_ascent + it->max_descent;
15821 row->phys_ascent = it->max_phys_ascent;
15822 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15823 row->extra_line_spacing = it->max_extra_line_spacing;
15824
15825 /* Loop generating characters. The loop is left with IT on the next
15826 character to display. */
15827 while (1)
15828 {
15829 int n_glyphs_before, hpos_before, x_before;
15830 int x, i, nglyphs;
15831 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15832
15833 /* Retrieve the next thing to display. Value is zero if end of
15834 buffer reached. */
15835 if (!get_next_display_element (it))
15836 {
15837 /* Maybe add a space at the end of this line that is used to
15838 display the cursor there under X. Set the charpos of the
15839 first glyph of blank lines not corresponding to any text
15840 to -1. */
15841 #ifdef HAVE_WINDOW_SYSTEM
15842 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15843 row->exact_window_width_line_p = 1;
15844 else
15845 #endif /* HAVE_WINDOW_SYSTEM */
15846 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15847 || row->used[TEXT_AREA] == 0)
15848 {
15849 row->glyphs[TEXT_AREA]->charpos = -1;
15850 row->displays_text_p = 0;
15851
15852 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15853 && (!MINI_WINDOW_P (it->w)
15854 || (minibuf_level && EQ (it->window, minibuf_window))))
15855 row->indicate_empty_line_p = 1;
15856 }
15857
15858 it->continuation_lines_width = 0;
15859 row->ends_at_zv_p = 1;
15860 break;
15861 }
15862
15863 /* Now, get the metrics of what we want to display. This also
15864 generates glyphs in `row' (which is IT->glyph_row). */
15865 n_glyphs_before = row->used[TEXT_AREA];
15866 x = it->current_x;
15867
15868 /* Remember the line height so far in case the next element doesn't
15869 fit on the line. */
15870 if (!it->truncate_lines_p)
15871 {
15872 ascent = it->max_ascent;
15873 descent = it->max_descent;
15874 phys_ascent = it->max_phys_ascent;
15875 phys_descent = it->max_phys_descent;
15876 }
15877
15878 PRODUCE_GLYPHS (it);
15879
15880 /* If this display element was in marginal areas, continue with
15881 the next one. */
15882 if (it->area != TEXT_AREA)
15883 {
15884 row->ascent = max (row->ascent, it->max_ascent);
15885 row->height = max (row->height, it->max_ascent + it->max_descent);
15886 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15887 row->phys_height = max (row->phys_height,
15888 it->max_phys_ascent + it->max_phys_descent);
15889 row->extra_line_spacing = max (row->extra_line_spacing,
15890 it->max_extra_line_spacing);
15891 set_iterator_to_next (it, 1);
15892 continue;
15893 }
15894
15895 /* Does the display element fit on the line? If we truncate
15896 lines, we should draw past the right edge of the window. If
15897 we don't truncate, we want to stop so that we can display the
15898 continuation glyph before the right margin. If lines are
15899 continued, there are two possible strategies for characters
15900 resulting in more than 1 glyph (e.g. tabs): Display as many
15901 glyphs as possible in this line and leave the rest for the
15902 continuation line, or display the whole element in the next
15903 line. Original redisplay did the former, so we do it also. */
15904 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15905 hpos_before = it->hpos;
15906 x_before = x;
15907
15908 if (/* Not a newline. */
15909 nglyphs > 0
15910 /* Glyphs produced fit entirely in the line. */
15911 && it->current_x < it->last_visible_x)
15912 {
15913 it->hpos += nglyphs;
15914 row->ascent = max (row->ascent, it->max_ascent);
15915 row->height = max (row->height, it->max_ascent + it->max_descent);
15916 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15917 row->phys_height = max (row->phys_height,
15918 it->max_phys_ascent + it->max_phys_descent);
15919 row->extra_line_spacing = max (row->extra_line_spacing,
15920 it->max_extra_line_spacing);
15921 if (it->current_x - it->pixel_width < it->first_visible_x)
15922 row->x = x - it->first_visible_x;
15923 }
15924 else
15925 {
15926 int new_x;
15927 struct glyph *glyph;
15928
15929 for (i = 0; i < nglyphs; ++i, x = new_x)
15930 {
15931 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15932 new_x = x + glyph->pixel_width;
15933
15934 if (/* Lines are continued. */
15935 !it->truncate_lines_p
15936 && (/* Glyph doesn't fit on the line. */
15937 new_x > it->last_visible_x
15938 /* Or it fits exactly on a window system frame. */
15939 || (new_x == it->last_visible_x
15940 && FRAME_WINDOW_P (it->f))))
15941 {
15942 /* End of a continued line. */
15943
15944 if (it->hpos == 0
15945 || (new_x == it->last_visible_x
15946 && FRAME_WINDOW_P (it->f)))
15947 {
15948 /* Current glyph is the only one on the line or
15949 fits exactly on the line. We must continue
15950 the line because we can't draw the cursor
15951 after the glyph. */
15952 row->continued_p = 1;
15953 it->current_x = new_x;
15954 it->continuation_lines_width += new_x;
15955 ++it->hpos;
15956 if (i == nglyphs - 1)
15957 {
15958 set_iterator_to_next (it, 1);
15959 #ifdef HAVE_WINDOW_SYSTEM
15960 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15961 {
15962 if (!get_next_display_element (it))
15963 {
15964 row->exact_window_width_line_p = 1;
15965 it->continuation_lines_width = 0;
15966 row->continued_p = 0;
15967 row->ends_at_zv_p = 1;
15968 }
15969 else if (ITERATOR_AT_END_OF_LINE_P (it))
15970 {
15971 row->continued_p = 0;
15972 row->exact_window_width_line_p = 1;
15973 }
15974 }
15975 #endif /* HAVE_WINDOW_SYSTEM */
15976 }
15977 }
15978 else if (CHAR_GLYPH_PADDING_P (*glyph)
15979 && !FRAME_WINDOW_P (it->f))
15980 {
15981 /* A padding glyph that doesn't fit on this line.
15982 This means the whole character doesn't fit
15983 on the line. */
15984 row->used[TEXT_AREA] = n_glyphs_before;
15985
15986 /* Fill the rest of the row with continuation
15987 glyphs like in 20.x. */
15988 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15989 < row->glyphs[1 + TEXT_AREA])
15990 produce_special_glyphs (it, IT_CONTINUATION);
15991
15992 row->continued_p = 1;
15993 it->current_x = x_before;
15994 it->continuation_lines_width += x_before;
15995
15996 /* Restore the height to what it was before the
15997 element not fitting on the line. */
15998 it->max_ascent = ascent;
15999 it->max_descent = descent;
16000 it->max_phys_ascent = phys_ascent;
16001 it->max_phys_descent = phys_descent;
16002 }
16003 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16004 {
16005 /* A TAB that extends past the right edge of the
16006 window. This produces a single glyph on
16007 window system frames. We leave the glyph in
16008 this row and let it fill the row, but don't
16009 consume the TAB. */
16010 it->continuation_lines_width += it->last_visible_x;
16011 row->ends_in_middle_of_char_p = 1;
16012 row->continued_p = 1;
16013 glyph->pixel_width = it->last_visible_x - x;
16014 it->starts_in_middle_of_char_p = 1;
16015 }
16016 else
16017 {
16018 /* Something other than a TAB that draws past
16019 the right edge of the window. Restore
16020 positions to values before the element. */
16021 row->used[TEXT_AREA] = n_glyphs_before + i;
16022
16023 /* Display continuation glyphs. */
16024 if (!FRAME_WINDOW_P (it->f))
16025 produce_special_glyphs (it, IT_CONTINUATION);
16026 row->continued_p = 1;
16027
16028 it->current_x = x_before;
16029 it->continuation_lines_width += x;
16030 extend_face_to_end_of_line (it);
16031
16032 if (nglyphs > 1 && i > 0)
16033 {
16034 row->ends_in_middle_of_char_p = 1;
16035 it->starts_in_middle_of_char_p = 1;
16036 }
16037
16038 /* Restore the height to what it was before the
16039 element not fitting on the line. */
16040 it->max_ascent = ascent;
16041 it->max_descent = descent;
16042 it->max_phys_ascent = phys_ascent;
16043 it->max_phys_descent = phys_descent;
16044 }
16045
16046 break;
16047 }
16048 else if (new_x > it->first_visible_x)
16049 {
16050 /* Increment number of glyphs actually displayed. */
16051 ++it->hpos;
16052
16053 if (x < it->first_visible_x)
16054 /* Glyph is partially visible, i.e. row starts at
16055 negative X position. */
16056 row->x = x - it->first_visible_x;
16057 }
16058 else
16059 {
16060 /* Glyph is completely off the left margin of the
16061 window. This should not happen because of the
16062 move_it_in_display_line at the start of this
16063 function, unless the text display area of the
16064 window is empty. */
16065 xassert (it->first_visible_x <= it->last_visible_x);
16066 }
16067 }
16068
16069 row->ascent = max (row->ascent, it->max_ascent);
16070 row->height = max (row->height, it->max_ascent + it->max_descent);
16071 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16072 row->phys_height = max (row->phys_height,
16073 it->max_phys_ascent + it->max_phys_descent);
16074 row->extra_line_spacing = max (row->extra_line_spacing,
16075 it->max_extra_line_spacing);
16076
16077 /* End of this display line if row is continued. */
16078 if (row->continued_p || row->ends_at_zv_p)
16079 break;
16080 }
16081
16082 at_end_of_line:
16083 /* Is this a line end? If yes, we're also done, after making
16084 sure that a non-default face is extended up to the right
16085 margin of the window. */
16086 if (ITERATOR_AT_END_OF_LINE_P (it))
16087 {
16088 int used_before = row->used[TEXT_AREA];
16089
16090 row->ends_in_newline_from_string_p = STRINGP (it->object);
16091
16092 #ifdef HAVE_WINDOW_SYSTEM
16093 /* Add a space at the end of the line that is used to
16094 display the cursor there. */
16095 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16096 append_space_for_newline (it, 0);
16097 #endif /* HAVE_WINDOW_SYSTEM */
16098
16099 /* Extend the face to the end of the line. */
16100 extend_face_to_end_of_line (it);
16101
16102 /* Make sure we have the position. */
16103 if (used_before == 0)
16104 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16105
16106 /* Consume the line end. This skips over invisible lines. */
16107 set_iterator_to_next (it, 1);
16108 it->continuation_lines_width = 0;
16109 break;
16110 }
16111
16112 /* Proceed with next display element. Note that this skips
16113 over lines invisible because of selective display. */
16114 set_iterator_to_next (it, 1);
16115
16116 /* If we truncate lines, we are done when the last displayed
16117 glyphs reach past the right margin of the window. */
16118 if (it->truncate_lines_p
16119 && (FRAME_WINDOW_P (it->f)
16120 ? (it->current_x >= it->last_visible_x)
16121 : (it->current_x > it->last_visible_x)))
16122 {
16123 /* Maybe add truncation glyphs. */
16124 if (!FRAME_WINDOW_P (it->f))
16125 {
16126 int i, n;
16127
16128 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16129 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16130 break;
16131
16132 for (n = row->used[TEXT_AREA]; i < n; ++i)
16133 {
16134 row->used[TEXT_AREA] = i;
16135 produce_special_glyphs (it, IT_TRUNCATION);
16136 }
16137 }
16138 #ifdef HAVE_WINDOW_SYSTEM
16139 else
16140 {
16141 /* Don't truncate if we can overflow newline into fringe. */
16142 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16143 {
16144 if (!get_next_display_element (it))
16145 {
16146 it->continuation_lines_width = 0;
16147 row->ends_at_zv_p = 1;
16148 row->exact_window_width_line_p = 1;
16149 break;
16150 }
16151 if (ITERATOR_AT_END_OF_LINE_P (it))
16152 {
16153 row->exact_window_width_line_p = 1;
16154 goto at_end_of_line;
16155 }
16156 }
16157 }
16158 #endif /* HAVE_WINDOW_SYSTEM */
16159
16160 row->truncated_on_right_p = 1;
16161 it->continuation_lines_width = 0;
16162 reseat_at_next_visible_line_start (it, 0);
16163 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16164 it->hpos = hpos_before;
16165 it->current_x = x_before;
16166 break;
16167 }
16168 }
16169
16170 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16171 at the left window margin. */
16172 if (it->first_visible_x
16173 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16174 {
16175 if (!FRAME_WINDOW_P (it->f))
16176 insert_left_trunc_glyphs (it);
16177 row->truncated_on_left_p = 1;
16178 }
16179
16180 /* If the start of this line is the overlay arrow-position, then
16181 mark this glyph row as the one containing the overlay arrow.
16182 This is clearly a mess with variable size fonts. It would be
16183 better to let it be displayed like cursors under X. */
16184 if ((row->displays_text_p || !overlay_arrow_seen)
16185 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16186 !NILP (overlay_arrow_string)))
16187 {
16188 /* Overlay arrow in window redisplay is a fringe bitmap. */
16189 if (STRINGP (overlay_arrow_string))
16190 {
16191 struct glyph_row *arrow_row
16192 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16193 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16194 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16195 struct glyph *p = row->glyphs[TEXT_AREA];
16196 struct glyph *p2, *end;
16197
16198 /* Copy the arrow glyphs. */
16199 while (glyph < arrow_end)
16200 *p++ = *glyph++;
16201
16202 /* Throw away padding glyphs. */
16203 p2 = p;
16204 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16205 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16206 ++p2;
16207 if (p2 > p)
16208 {
16209 while (p2 < end)
16210 *p++ = *p2++;
16211 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16212 }
16213 }
16214 else
16215 {
16216 xassert (INTEGERP (overlay_arrow_string));
16217 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16218 }
16219 overlay_arrow_seen = 1;
16220 }
16221
16222 /* Compute pixel dimensions of this line. */
16223 compute_line_metrics (it);
16224
16225 /* Remember the position at which this line ends. */
16226 row->end = it->current;
16227
16228 /* Record whether this row ends inside an ellipsis. */
16229 row->ends_in_ellipsis_p
16230 = (it->method == GET_FROM_DISPLAY_VECTOR
16231 && it->ellipsis_p);
16232
16233 /* Save fringe bitmaps in this row. */
16234 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16235 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16236 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16237 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16238
16239 it->left_user_fringe_bitmap = 0;
16240 it->left_user_fringe_face_id = 0;
16241 it->right_user_fringe_bitmap = 0;
16242 it->right_user_fringe_face_id = 0;
16243
16244 /* Maybe set the cursor. */
16245 if (it->w->cursor.vpos < 0
16246 && PT >= MATRIX_ROW_START_CHARPOS (row)
16247 && PT <= MATRIX_ROW_END_CHARPOS (row)
16248 && cursor_row_p (it->w, row))
16249 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16250
16251 /* Highlight trailing whitespace. */
16252 if (!NILP (Vshow_trailing_whitespace))
16253 highlight_trailing_whitespace (it->f, it->glyph_row);
16254
16255 /* Prepare for the next line. This line starts horizontally at (X
16256 HPOS) = (0 0). Vertical positions are incremented. As a
16257 convenience for the caller, IT->glyph_row is set to the next
16258 row to be used. */
16259 it->current_x = it->hpos = 0;
16260 it->current_y += row->height;
16261 ++it->vpos;
16262 ++it->glyph_row;
16263 it->start = it->current;
16264 return row->displays_text_p;
16265 }
16266
16267
16268 \f
16269 /***********************************************************************
16270 Menu Bar
16271 ***********************************************************************/
16272
16273 /* Redisplay the menu bar in the frame for window W.
16274
16275 The menu bar of X frames that don't have X toolkit support is
16276 displayed in a special window W->frame->menu_bar_window.
16277
16278 The menu bar of terminal frames is treated specially as far as
16279 glyph matrices are concerned. Menu bar lines are not part of
16280 windows, so the update is done directly on the frame matrix rows
16281 for the menu bar. */
16282
16283 static void
16284 display_menu_bar (w)
16285 struct window *w;
16286 {
16287 struct frame *f = XFRAME (WINDOW_FRAME (w));
16288 struct it it;
16289 Lisp_Object items;
16290 int i;
16291
16292 /* Don't do all this for graphical frames. */
16293 #ifdef HAVE_NTGUI
16294 if (!NILP (Vwindow_system))
16295 return;
16296 #endif
16297 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16298 if (FRAME_X_P (f))
16299 return;
16300 #endif
16301 #ifdef MAC_OS
16302 if (FRAME_MAC_P (f))
16303 return;
16304 #endif
16305
16306 #ifdef USE_X_TOOLKIT
16307 xassert (!FRAME_WINDOW_P (f));
16308 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16309 it.first_visible_x = 0;
16310 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16311 #else /* not USE_X_TOOLKIT */
16312 if (FRAME_WINDOW_P (f))
16313 {
16314 /* Menu bar lines are displayed in the desired matrix of the
16315 dummy window menu_bar_window. */
16316 struct window *menu_w;
16317 xassert (WINDOWP (f->menu_bar_window));
16318 menu_w = XWINDOW (f->menu_bar_window);
16319 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16320 MENU_FACE_ID);
16321 it.first_visible_x = 0;
16322 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16323 }
16324 else
16325 {
16326 /* This is a TTY frame, i.e. character hpos/vpos are used as
16327 pixel x/y. */
16328 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16329 MENU_FACE_ID);
16330 it.first_visible_x = 0;
16331 it.last_visible_x = FRAME_COLS (f);
16332 }
16333 #endif /* not USE_X_TOOLKIT */
16334
16335 if (! mode_line_inverse_video)
16336 /* Force the menu-bar to be displayed in the default face. */
16337 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16338
16339 /* Clear all rows of the menu bar. */
16340 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16341 {
16342 struct glyph_row *row = it.glyph_row + i;
16343 clear_glyph_row (row);
16344 row->enabled_p = 1;
16345 row->full_width_p = 1;
16346 }
16347
16348 /* Display all items of the menu bar. */
16349 items = FRAME_MENU_BAR_ITEMS (it.f);
16350 for (i = 0; i < XVECTOR (items)->size; i += 4)
16351 {
16352 Lisp_Object string;
16353
16354 /* Stop at nil string. */
16355 string = AREF (items, i + 1);
16356 if (NILP (string))
16357 break;
16358
16359 /* Remember where item was displayed. */
16360 AREF (items, i + 3) = make_number (it.hpos);
16361
16362 /* Display the item, pad with one space. */
16363 if (it.current_x < it.last_visible_x)
16364 display_string (NULL, string, Qnil, 0, 0, &it,
16365 SCHARS (string) + 1, 0, 0, -1);
16366 }
16367
16368 /* Fill out the line with spaces. */
16369 if (it.current_x < it.last_visible_x)
16370 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16371
16372 /* Compute the total height of the lines. */
16373 compute_line_metrics (&it);
16374 }
16375
16376
16377 \f
16378 /***********************************************************************
16379 Mode Line
16380 ***********************************************************************/
16381
16382 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16383 FORCE is non-zero, redisplay mode lines unconditionally.
16384 Otherwise, redisplay only mode lines that are garbaged. Value is
16385 the number of windows whose mode lines were redisplayed. */
16386
16387 static int
16388 redisplay_mode_lines (window, force)
16389 Lisp_Object window;
16390 int force;
16391 {
16392 int nwindows = 0;
16393
16394 while (!NILP (window))
16395 {
16396 struct window *w = XWINDOW (window);
16397
16398 if (WINDOWP (w->hchild))
16399 nwindows += redisplay_mode_lines (w->hchild, force);
16400 else if (WINDOWP (w->vchild))
16401 nwindows += redisplay_mode_lines (w->vchild, force);
16402 else if (force
16403 || FRAME_GARBAGED_P (XFRAME (w->frame))
16404 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16405 {
16406 struct text_pos lpoint;
16407 struct buffer *old = current_buffer;
16408
16409 /* Set the window's buffer for the mode line display. */
16410 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16411 set_buffer_internal_1 (XBUFFER (w->buffer));
16412
16413 /* Point refers normally to the selected window. For any
16414 other window, set up appropriate value. */
16415 if (!EQ (window, selected_window))
16416 {
16417 struct text_pos pt;
16418
16419 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16420 if (CHARPOS (pt) < BEGV)
16421 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16422 else if (CHARPOS (pt) > (ZV - 1))
16423 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16424 else
16425 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16426 }
16427
16428 /* Display mode lines. */
16429 clear_glyph_matrix (w->desired_matrix);
16430 if (display_mode_lines (w))
16431 {
16432 ++nwindows;
16433 w->must_be_updated_p = 1;
16434 }
16435
16436 /* Restore old settings. */
16437 set_buffer_internal_1 (old);
16438 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16439 }
16440
16441 window = w->next;
16442 }
16443
16444 return nwindows;
16445 }
16446
16447
16448 /* Display the mode and/or top line of window W. Value is the number
16449 of mode lines displayed. */
16450
16451 static int
16452 display_mode_lines (w)
16453 struct window *w;
16454 {
16455 Lisp_Object old_selected_window, old_selected_frame;
16456 int n = 0;
16457
16458 old_selected_frame = selected_frame;
16459 selected_frame = w->frame;
16460 old_selected_window = selected_window;
16461 XSETWINDOW (selected_window, w);
16462
16463 /* These will be set while the mode line specs are processed. */
16464 line_number_displayed = 0;
16465 w->column_number_displayed = Qnil;
16466
16467 if (WINDOW_WANTS_MODELINE_P (w))
16468 {
16469 struct window *sel_w = XWINDOW (old_selected_window);
16470
16471 /* Select mode line face based on the real selected window. */
16472 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16473 current_buffer->mode_line_format);
16474 ++n;
16475 }
16476
16477 if (WINDOW_WANTS_HEADER_LINE_P (w))
16478 {
16479 display_mode_line (w, HEADER_LINE_FACE_ID,
16480 current_buffer->header_line_format);
16481 ++n;
16482 }
16483
16484 selected_frame = old_selected_frame;
16485 selected_window = old_selected_window;
16486 return n;
16487 }
16488
16489
16490 /* Display mode or top line of window W. FACE_ID specifies which line
16491 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16492 FORMAT is the mode line format to display. Value is the pixel
16493 height of the mode line displayed. */
16494
16495 static int
16496 display_mode_line (w, face_id, format)
16497 struct window *w;
16498 enum face_id face_id;
16499 Lisp_Object format;
16500 {
16501 struct it it;
16502 struct face *face;
16503 int count = SPECPDL_INDEX ();
16504
16505 init_iterator (&it, w, -1, -1, NULL, face_id);
16506 prepare_desired_row (it.glyph_row);
16507
16508 it.glyph_row->mode_line_p = 1;
16509
16510 if (! mode_line_inverse_video)
16511 /* Force the mode-line to be displayed in the default face. */
16512 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16513
16514 record_unwind_protect (unwind_format_mode_line,
16515 format_mode_line_unwind_data (NULL, 0));
16516
16517 mode_line_target = MODE_LINE_DISPLAY;
16518
16519 /* Temporarily make frame's keyboard the current kboard so that
16520 kboard-local variables in the mode_line_format will get the right
16521 values. */
16522 push_frame_kboard (it.f);
16523 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16524 pop_frame_kboard ();
16525
16526 unbind_to (count, Qnil);
16527
16528 /* Fill up with spaces. */
16529 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16530
16531 compute_line_metrics (&it);
16532 it.glyph_row->full_width_p = 1;
16533 it.glyph_row->continued_p = 0;
16534 it.glyph_row->truncated_on_left_p = 0;
16535 it.glyph_row->truncated_on_right_p = 0;
16536
16537 /* Make a 3D mode-line have a shadow at its right end. */
16538 face = FACE_FROM_ID (it.f, face_id);
16539 extend_face_to_end_of_line (&it);
16540 if (face->box != FACE_NO_BOX)
16541 {
16542 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16543 + it.glyph_row->used[TEXT_AREA] - 1);
16544 last->right_box_line_p = 1;
16545 }
16546
16547 return it.glyph_row->height;
16548 }
16549
16550 /* Move element ELT in LIST to the front of LIST.
16551 Return the updated list. */
16552
16553 static Lisp_Object
16554 move_elt_to_front (elt, list)
16555 Lisp_Object elt, list;
16556 {
16557 register Lisp_Object tail, prev;
16558 register Lisp_Object tem;
16559
16560 tail = list;
16561 prev = Qnil;
16562 while (CONSP (tail))
16563 {
16564 tem = XCAR (tail);
16565
16566 if (EQ (elt, tem))
16567 {
16568 /* Splice out the link TAIL. */
16569 if (NILP (prev))
16570 list = XCDR (tail);
16571 else
16572 Fsetcdr (prev, XCDR (tail));
16573
16574 /* Now make it the first. */
16575 Fsetcdr (tail, list);
16576 return tail;
16577 }
16578 else
16579 prev = tail;
16580 tail = XCDR (tail);
16581 QUIT;
16582 }
16583
16584 /* Not found--return unchanged LIST. */
16585 return list;
16586 }
16587
16588 /* Contribute ELT to the mode line for window IT->w. How it
16589 translates into text depends on its data type.
16590
16591 IT describes the display environment in which we display, as usual.
16592
16593 DEPTH is the depth in recursion. It is used to prevent
16594 infinite recursion here.
16595
16596 FIELD_WIDTH is the number of characters the display of ELT should
16597 occupy in the mode line, and PRECISION is the maximum number of
16598 characters to display from ELT's representation. See
16599 display_string for details.
16600
16601 Returns the hpos of the end of the text generated by ELT.
16602
16603 PROPS is a property list to add to any string we encounter.
16604
16605 If RISKY is nonzero, remove (disregard) any properties in any string
16606 we encounter, and ignore :eval and :propertize.
16607
16608 The global variable `mode_line_target' determines whether the
16609 output is passed to `store_mode_line_noprop',
16610 `store_mode_line_string', or `display_string'. */
16611
16612 static int
16613 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16614 struct it *it;
16615 int depth;
16616 int field_width, precision;
16617 Lisp_Object elt, props;
16618 int risky;
16619 {
16620 int n = 0, field, prec;
16621 int literal = 0;
16622
16623 tail_recurse:
16624 if (depth > 100)
16625 elt = build_string ("*too-deep*");
16626
16627 depth++;
16628
16629 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16630 {
16631 case Lisp_String:
16632 {
16633 /* A string: output it and check for %-constructs within it. */
16634 unsigned char c;
16635 int offset = 0;
16636
16637 if (SCHARS (elt) > 0
16638 && (!NILP (props) || risky))
16639 {
16640 Lisp_Object oprops, aelt;
16641 oprops = Ftext_properties_at (make_number (0), elt);
16642
16643 /* If the starting string's properties are not what
16644 we want, translate the string. Also, if the string
16645 is risky, do that anyway. */
16646
16647 if (NILP (Fequal (props, oprops)) || risky)
16648 {
16649 /* If the starting string has properties,
16650 merge the specified ones onto the existing ones. */
16651 if (! NILP (oprops) && !risky)
16652 {
16653 Lisp_Object tem;
16654
16655 oprops = Fcopy_sequence (oprops);
16656 tem = props;
16657 while (CONSP (tem))
16658 {
16659 oprops = Fplist_put (oprops, XCAR (tem),
16660 XCAR (XCDR (tem)));
16661 tem = XCDR (XCDR (tem));
16662 }
16663 props = oprops;
16664 }
16665
16666 aelt = Fassoc (elt, mode_line_proptrans_alist);
16667 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16668 {
16669 /* AELT is what we want. Move it to the front
16670 without consing. */
16671 elt = XCAR (aelt);
16672 mode_line_proptrans_alist
16673 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16674 }
16675 else
16676 {
16677 Lisp_Object tem;
16678
16679 /* If AELT has the wrong props, it is useless.
16680 so get rid of it. */
16681 if (! NILP (aelt))
16682 mode_line_proptrans_alist
16683 = Fdelq (aelt, mode_line_proptrans_alist);
16684
16685 elt = Fcopy_sequence (elt);
16686 Fset_text_properties (make_number (0), Flength (elt),
16687 props, elt);
16688 /* Add this item to mode_line_proptrans_alist. */
16689 mode_line_proptrans_alist
16690 = Fcons (Fcons (elt, props),
16691 mode_line_proptrans_alist);
16692 /* Truncate mode_line_proptrans_alist
16693 to at most 50 elements. */
16694 tem = Fnthcdr (make_number (50),
16695 mode_line_proptrans_alist);
16696 if (! NILP (tem))
16697 XSETCDR (tem, Qnil);
16698 }
16699 }
16700 }
16701
16702 offset = 0;
16703
16704 if (literal)
16705 {
16706 prec = precision - n;
16707 switch (mode_line_target)
16708 {
16709 case MODE_LINE_NOPROP:
16710 case MODE_LINE_TITLE:
16711 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16712 break;
16713 case MODE_LINE_STRING:
16714 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16715 break;
16716 case MODE_LINE_DISPLAY:
16717 n += display_string (NULL, elt, Qnil, 0, 0, it,
16718 0, prec, 0, STRING_MULTIBYTE (elt));
16719 break;
16720 }
16721
16722 break;
16723 }
16724
16725 /* Handle the non-literal case. */
16726
16727 while ((precision <= 0 || n < precision)
16728 && SREF (elt, offset) != 0
16729 && (mode_line_target != MODE_LINE_DISPLAY
16730 || it->current_x < it->last_visible_x))
16731 {
16732 int last_offset = offset;
16733
16734 /* Advance to end of string or next format specifier. */
16735 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16736 ;
16737
16738 if (offset - 1 != last_offset)
16739 {
16740 int nchars, nbytes;
16741
16742 /* Output to end of string or up to '%'. Field width
16743 is length of string. Don't output more than
16744 PRECISION allows us. */
16745 offset--;
16746
16747 prec = c_string_width (SDATA (elt) + last_offset,
16748 offset - last_offset, precision - n,
16749 &nchars, &nbytes);
16750
16751 switch (mode_line_target)
16752 {
16753 case MODE_LINE_NOPROP:
16754 case MODE_LINE_TITLE:
16755 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16756 break;
16757 case MODE_LINE_STRING:
16758 {
16759 int bytepos = last_offset;
16760 int charpos = string_byte_to_char (elt, bytepos);
16761 int endpos = (precision <= 0
16762 ? string_byte_to_char (elt, offset)
16763 : charpos + nchars);
16764
16765 n += store_mode_line_string (NULL,
16766 Fsubstring (elt, make_number (charpos),
16767 make_number (endpos)),
16768 0, 0, 0, Qnil);
16769 }
16770 break;
16771 case MODE_LINE_DISPLAY:
16772 {
16773 int bytepos = last_offset;
16774 int charpos = string_byte_to_char (elt, bytepos);
16775
16776 if (precision <= 0)
16777 nchars = string_byte_to_char (elt, offset) - charpos;
16778 n += display_string (NULL, elt, Qnil, 0, charpos,
16779 it, 0, nchars, 0,
16780 STRING_MULTIBYTE (elt));
16781 }
16782 break;
16783 }
16784 }
16785 else /* c == '%' */
16786 {
16787 int percent_position = offset;
16788
16789 /* Get the specified minimum width. Zero means
16790 don't pad. */
16791 field = 0;
16792 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16793 field = field * 10 + c - '0';
16794
16795 /* Don't pad beyond the total padding allowed. */
16796 if (field_width - n > 0 && field > field_width - n)
16797 field = field_width - n;
16798
16799 /* Note that either PRECISION <= 0 or N < PRECISION. */
16800 prec = precision - n;
16801
16802 if (c == 'M')
16803 n += display_mode_element (it, depth, field, prec,
16804 Vglobal_mode_string, props,
16805 risky);
16806 else if (c != 0)
16807 {
16808 int multibyte;
16809 int bytepos, charpos;
16810 unsigned char *spec;
16811
16812 bytepos = percent_position;
16813 charpos = (STRING_MULTIBYTE (elt)
16814 ? string_byte_to_char (elt, bytepos)
16815 : bytepos);
16816
16817 spec
16818 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16819
16820 switch (mode_line_target)
16821 {
16822 case MODE_LINE_NOPROP:
16823 case MODE_LINE_TITLE:
16824 n += store_mode_line_noprop (spec, field, prec);
16825 break;
16826 case MODE_LINE_STRING:
16827 {
16828 int len = strlen (spec);
16829 Lisp_Object tem = make_string (spec, len);
16830 props = Ftext_properties_at (make_number (charpos), elt);
16831 /* Should only keep face property in props */
16832 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16833 }
16834 break;
16835 case MODE_LINE_DISPLAY:
16836 {
16837 int nglyphs_before, nwritten;
16838
16839 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16840 nwritten = display_string (spec, Qnil, elt,
16841 charpos, 0, it,
16842 field, prec, 0,
16843 multibyte);
16844
16845 /* Assign to the glyphs written above the
16846 string where the `%x' came from, position
16847 of the `%'. */
16848 if (nwritten > 0)
16849 {
16850 struct glyph *glyph
16851 = (it->glyph_row->glyphs[TEXT_AREA]
16852 + nglyphs_before);
16853 int i;
16854
16855 for (i = 0; i < nwritten; ++i)
16856 {
16857 glyph[i].object = elt;
16858 glyph[i].charpos = charpos;
16859 }
16860
16861 n += nwritten;
16862 }
16863 }
16864 break;
16865 }
16866 }
16867 else /* c == 0 */
16868 break;
16869 }
16870 }
16871 }
16872 break;
16873
16874 case Lisp_Symbol:
16875 /* A symbol: process the value of the symbol recursively
16876 as if it appeared here directly. Avoid error if symbol void.
16877 Special case: if value of symbol is a string, output the string
16878 literally. */
16879 {
16880 register Lisp_Object tem;
16881
16882 /* If the variable is not marked as risky to set
16883 then its contents are risky to use. */
16884 if (NILP (Fget (elt, Qrisky_local_variable)))
16885 risky = 1;
16886
16887 tem = Fboundp (elt);
16888 if (!NILP (tem))
16889 {
16890 tem = Fsymbol_value (elt);
16891 /* If value is a string, output that string literally:
16892 don't check for % within it. */
16893 if (STRINGP (tem))
16894 literal = 1;
16895
16896 if (!EQ (tem, elt))
16897 {
16898 /* Give up right away for nil or t. */
16899 elt = tem;
16900 goto tail_recurse;
16901 }
16902 }
16903 }
16904 break;
16905
16906 case Lisp_Cons:
16907 {
16908 register Lisp_Object car, tem;
16909
16910 /* A cons cell: five distinct cases.
16911 If first element is :eval or :propertize, do something special.
16912 If first element is a string or a cons, process all the elements
16913 and effectively concatenate them.
16914 If first element is a negative number, truncate displaying cdr to
16915 at most that many characters. If positive, pad (with spaces)
16916 to at least that many characters.
16917 If first element is a symbol, process the cadr or caddr recursively
16918 according to whether the symbol's value is non-nil or nil. */
16919 car = XCAR (elt);
16920 if (EQ (car, QCeval))
16921 {
16922 /* An element of the form (:eval FORM) means evaluate FORM
16923 and use the result as mode line elements. */
16924
16925 if (risky)
16926 break;
16927
16928 if (CONSP (XCDR (elt)))
16929 {
16930 Lisp_Object spec;
16931 spec = safe_eval (XCAR (XCDR (elt)));
16932 n += display_mode_element (it, depth, field_width - n,
16933 precision - n, spec, props,
16934 risky);
16935 }
16936 }
16937 else if (EQ (car, QCpropertize))
16938 {
16939 /* An element of the form (:propertize ELT PROPS...)
16940 means display ELT but applying properties PROPS. */
16941
16942 if (risky)
16943 break;
16944
16945 if (CONSP (XCDR (elt)))
16946 n += display_mode_element (it, depth, field_width - n,
16947 precision - n, XCAR (XCDR (elt)),
16948 XCDR (XCDR (elt)), risky);
16949 }
16950 else if (SYMBOLP (car))
16951 {
16952 tem = Fboundp (car);
16953 elt = XCDR (elt);
16954 if (!CONSP (elt))
16955 goto invalid;
16956 /* elt is now the cdr, and we know it is a cons cell.
16957 Use its car if CAR has a non-nil value. */
16958 if (!NILP (tem))
16959 {
16960 tem = Fsymbol_value (car);
16961 if (!NILP (tem))
16962 {
16963 elt = XCAR (elt);
16964 goto tail_recurse;
16965 }
16966 }
16967 /* Symbol's value is nil (or symbol is unbound)
16968 Get the cddr of the original list
16969 and if possible find the caddr and use that. */
16970 elt = XCDR (elt);
16971 if (NILP (elt))
16972 break;
16973 else if (!CONSP (elt))
16974 goto invalid;
16975 elt = XCAR (elt);
16976 goto tail_recurse;
16977 }
16978 else if (INTEGERP (car))
16979 {
16980 register int lim = XINT (car);
16981 elt = XCDR (elt);
16982 if (lim < 0)
16983 {
16984 /* Negative int means reduce maximum width. */
16985 if (precision <= 0)
16986 precision = -lim;
16987 else
16988 precision = min (precision, -lim);
16989 }
16990 else if (lim > 0)
16991 {
16992 /* Padding specified. Don't let it be more than
16993 current maximum. */
16994 if (precision > 0)
16995 lim = min (precision, lim);
16996
16997 /* If that's more padding than already wanted, queue it.
16998 But don't reduce padding already specified even if
16999 that is beyond the current truncation point. */
17000 field_width = max (lim, field_width);
17001 }
17002 goto tail_recurse;
17003 }
17004 else if (STRINGP (car) || CONSP (car))
17005 {
17006 register int limit = 50;
17007 /* Limit is to protect against circular lists. */
17008 while (CONSP (elt)
17009 && --limit > 0
17010 && (precision <= 0 || n < precision))
17011 {
17012 n += display_mode_element (it, depth,
17013 /* Do padding only after the last
17014 element in the list. */
17015 (! CONSP (XCDR (elt))
17016 ? field_width - n
17017 : 0),
17018 precision - n, XCAR (elt),
17019 props, risky);
17020 elt = XCDR (elt);
17021 }
17022 }
17023 }
17024 break;
17025
17026 default:
17027 invalid:
17028 elt = build_string ("*invalid*");
17029 goto tail_recurse;
17030 }
17031
17032 /* Pad to FIELD_WIDTH. */
17033 if (field_width > 0 && n < field_width)
17034 {
17035 switch (mode_line_target)
17036 {
17037 case MODE_LINE_NOPROP:
17038 case MODE_LINE_TITLE:
17039 n += store_mode_line_noprop ("", field_width - n, 0);
17040 break;
17041 case MODE_LINE_STRING:
17042 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17043 break;
17044 case MODE_LINE_DISPLAY:
17045 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17046 0, 0, 0);
17047 break;
17048 }
17049 }
17050
17051 return n;
17052 }
17053
17054 /* Store a mode-line string element in mode_line_string_list.
17055
17056 If STRING is non-null, display that C string. Otherwise, the Lisp
17057 string LISP_STRING is displayed.
17058
17059 FIELD_WIDTH is the minimum number of output glyphs to produce.
17060 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17061 with spaces. FIELD_WIDTH <= 0 means don't pad.
17062
17063 PRECISION is the maximum number of characters to output from
17064 STRING. PRECISION <= 0 means don't truncate the string.
17065
17066 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17067 properties to the string.
17068
17069 PROPS are the properties to add to the string.
17070 The mode_line_string_face face property is always added to the string.
17071 */
17072
17073 static int
17074 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17075 char *string;
17076 Lisp_Object lisp_string;
17077 int copy_string;
17078 int field_width;
17079 int precision;
17080 Lisp_Object props;
17081 {
17082 int len;
17083 int n = 0;
17084
17085 if (string != NULL)
17086 {
17087 len = strlen (string);
17088 if (precision > 0 && len > precision)
17089 len = precision;
17090 lisp_string = make_string (string, len);
17091 if (NILP (props))
17092 props = mode_line_string_face_prop;
17093 else if (!NILP (mode_line_string_face))
17094 {
17095 Lisp_Object face = Fplist_get (props, Qface);
17096 props = Fcopy_sequence (props);
17097 if (NILP (face))
17098 face = mode_line_string_face;
17099 else
17100 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17101 props = Fplist_put (props, Qface, face);
17102 }
17103 Fadd_text_properties (make_number (0), make_number (len),
17104 props, lisp_string);
17105 }
17106 else
17107 {
17108 len = XFASTINT (Flength (lisp_string));
17109 if (precision > 0 && len > precision)
17110 {
17111 len = precision;
17112 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17113 precision = -1;
17114 }
17115 if (!NILP (mode_line_string_face))
17116 {
17117 Lisp_Object face;
17118 if (NILP (props))
17119 props = Ftext_properties_at (make_number (0), lisp_string);
17120 face = Fplist_get (props, Qface);
17121 if (NILP (face))
17122 face = mode_line_string_face;
17123 else
17124 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17125 props = Fcons (Qface, Fcons (face, Qnil));
17126 if (copy_string)
17127 lisp_string = Fcopy_sequence (lisp_string);
17128 }
17129 if (!NILP (props))
17130 Fadd_text_properties (make_number (0), make_number (len),
17131 props, lisp_string);
17132 }
17133
17134 if (len > 0)
17135 {
17136 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17137 n += len;
17138 }
17139
17140 if (field_width > len)
17141 {
17142 field_width -= len;
17143 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17144 if (!NILP (props))
17145 Fadd_text_properties (make_number (0), make_number (field_width),
17146 props, lisp_string);
17147 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17148 n += field_width;
17149 }
17150
17151 return n;
17152 }
17153
17154
17155 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17156 1, 4, 0,
17157 doc: /* Format a string out of a mode line format specification.
17158 First arg FORMAT specifies the mode line format (see `mode-line-format'
17159 for details) to use.
17160
17161 Optional second arg FACE specifies the face property to put
17162 on all characters for which no face is specified.
17163 t means whatever face the window's mode line currently uses
17164 \(either `mode-line' or `mode-line-inactive', depending).
17165 nil means the default is no face property.
17166 If FACE is an integer, the value string has no text properties.
17167
17168 Optional third and fourth args WINDOW and BUFFER specify the window
17169 and buffer to use as the context for the formatting (defaults
17170 are the selected window and the window's buffer). */)
17171 (format, face, window, buffer)
17172 Lisp_Object format, face, window, buffer;
17173 {
17174 struct it it;
17175 int len;
17176 struct window *w;
17177 struct buffer *old_buffer = NULL;
17178 int face_id = -1;
17179 int no_props = INTEGERP (face);
17180 int count = SPECPDL_INDEX ();
17181 Lisp_Object str;
17182 int string_start = 0;
17183
17184 if (NILP (window))
17185 window = selected_window;
17186 CHECK_WINDOW (window);
17187 w = XWINDOW (window);
17188
17189 if (NILP (buffer))
17190 buffer = w->buffer;
17191 CHECK_BUFFER (buffer);
17192
17193 if (NILP (format))
17194 return build_string ("");
17195
17196 if (no_props)
17197 face = Qnil;
17198
17199 if (!NILP (face))
17200 {
17201 if (EQ (face, Qt))
17202 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17203 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17204 }
17205
17206 if (face_id < 0)
17207 face_id = DEFAULT_FACE_ID;
17208
17209 if (XBUFFER (buffer) != current_buffer)
17210 old_buffer = current_buffer;
17211
17212 /* Save things including mode_line_proptrans_alist,
17213 and set that to nil so that we don't alter the outer value. */
17214 record_unwind_protect (unwind_format_mode_line,
17215 format_mode_line_unwind_data (old_buffer, 1));
17216 mode_line_proptrans_alist = Qnil;
17217
17218 if (old_buffer)
17219 set_buffer_internal_1 (XBUFFER (buffer));
17220
17221 init_iterator (&it, w, -1, -1, NULL, face_id);
17222
17223 if (no_props)
17224 {
17225 mode_line_target = MODE_LINE_NOPROP;
17226 mode_line_string_face_prop = Qnil;
17227 mode_line_string_list = Qnil;
17228 string_start = MODE_LINE_NOPROP_LEN (0);
17229 }
17230 else
17231 {
17232 mode_line_target = MODE_LINE_STRING;
17233 mode_line_string_list = Qnil;
17234 mode_line_string_face = face;
17235 mode_line_string_face_prop
17236 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17237 }
17238
17239 push_frame_kboard (it.f);
17240 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17241 pop_frame_kboard ();
17242
17243 if (no_props)
17244 {
17245 len = MODE_LINE_NOPROP_LEN (string_start);
17246 str = make_string (mode_line_noprop_buf + string_start, len);
17247 }
17248 else
17249 {
17250 mode_line_string_list = Fnreverse (mode_line_string_list);
17251 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17252 make_string ("", 0));
17253 }
17254
17255 unbind_to (count, Qnil);
17256 return str;
17257 }
17258
17259 /* Write a null-terminated, right justified decimal representation of
17260 the positive integer D to BUF using a minimal field width WIDTH. */
17261
17262 static void
17263 pint2str (buf, width, d)
17264 register char *buf;
17265 register int width;
17266 register int d;
17267 {
17268 register char *p = buf;
17269
17270 if (d <= 0)
17271 *p++ = '0';
17272 else
17273 {
17274 while (d > 0)
17275 {
17276 *p++ = d % 10 + '0';
17277 d /= 10;
17278 }
17279 }
17280
17281 for (width -= (int) (p - buf); width > 0; --width)
17282 *p++ = ' ';
17283 *p-- = '\0';
17284 while (p > buf)
17285 {
17286 d = *buf;
17287 *buf++ = *p;
17288 *p-- = d;
17289 }
17290 }
17291
17292 /* Write a null-terminated, right justified decimal and "human
17293 readable" representation of the nonnegative integer D to BUF using
17294 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17295
17296 static const char power_letter[] =
17297 {
17298 0, /* not used */
17299 'k', /* kilo */
17300 'M', /* mega */
17301 'G', /* giga */
17302 'T', /* tera */
17303 'P', /* peta */
17304 'E', /* exa */
17305 'Z', /* zetta */
17306 'Y' /* yotta */
17307 };
17308
17309 static void
17310 pint2hrstr (buf, width, d)
17311 char *buf;
17312 int width;
17313 int d;
17314 {
17315 /* We aim to represent the nonnegative integer D as
17316 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17317 int quotient = d;
17318 int remainder = 0;
17319 /* -1 means: do not use TENTHS. */
17320 int tenths = -1;
17321 int exponent = 0;
17322
17323 /* Length of QUOTIENT.TENTHS as a string. */
17324 int length;
17325
17326 char * psuffix;
17327 char * p;
17328
17329 if (1000 <= quotient)
17330 {
17331 /* Scale to the appropriate EXPONENT. */
17332 do
17333 {
17334 remainder = quotient % 1000;
17335 quotient /= 1000;
17336 exponent++;
17337 }
17338 while (1000 <= quotient);
17339
17340 /* Round to nearest and decide whether to use TENTHS or not. */
17341 if (quotient <= 9)
17342 {
17343 tenths = remainder / 100;
17344 if (50 <= remainder % 100)
17345 {
17346 if (tenths < 9)
17347 tenths++;
17348 else
17349 {
17350 quotient++;
17351 if (quotient == 10)
17352 tenths = -1;
17353 else
17354 tenths = 0;
17355 }
17356 }
17357 }
17358 else
17359 if (500 <= remainder)
17360 {
17361 if (quotient < 999)
17362 quotient++;
17363 else
17364 {
17365 quotient = 1;
17366 exponent++;
17367 tenths = 0;
17368 }
17369 }
17370 }
17371
17372 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17373 if (tenths == -1 && quotient <= 99)
17374 if (quotient <= 9)
17375 length = 1;
17376 else
17377 length = 2;
17378 else
17379 length = 3;
17380 p = psuffix = buf + max (width, length);
17381
17382 /* Print EXPONENT. */
17383 if (exponent)
17384 *psuffix++ = power_letter[exponent];
17385 *psuffix = '\0';
17386
17387 /* Print TENTHS. */
17388 if (tenths >= 0)
17389 {
17390 *--p = '0' + tenths;
17391 *--p = '.';
17392 }
17393
17394 /* Print QUOTIENT. */
17395 do
17396 {
17397 int digit = quotient % 10;
17398 *--p = '0' + digit;
17399 }
17400 while ((quotient /= 10) != 0);
17401
17402 /* Print leading spaces. */
17403 while (buf < p)
17404 *--p = ' ';
17405 }
17406
17407 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17408 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17409 type of CODING_SYSTEM. Return updated pointer into BUF. */
17410
17411 static unsigned char invalid_eol_type[] = "(*invalid*)";
17412
17413 static char *
17414 decode_mode_spec_coding (coding_system, buf, eol_flag)
17415 Lisp_Object coding_system;
17416 register char *buf;
17417 int eol_flag;
17418 {
17419 Lisp_Object val;
17420 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17421 const unsigned char *eol_str;
17422 int eol_str_len;
17423 /* The EOL conversion we are using. */
17424 Lisp_Object eoltype;
17425
17426 val = Fget (coding_system, Qcoding_system);
17427 eoltype = Qnil;
17428
17429 if (!VECTORP (val)) /* Not yet decided. */
17430 {
17431 if (multibyte)
17432 *buf++ = '-';
17433 if (eol_flag)
17434 eoltype = eol_mnemonic_undecided;
17435 /* Don't mention EOL conversion if it isn't decided. */
17436 }
17437 else
17438 {
17439 Lisp_Object eolvalue;
17440
17441 eolvalue = Fget (coding_system, Qeol_type);
17442
17443 if (multibyte)
17444 *buf++ = XFASTINT (AREF (val, 1));
17445
17446 if (eol_flag)
17447 {
17448 /* The EOL conversion that is normal on this system. */
17449
17450 if (NILP (eolvalue)) /* Not yet decided. */
17451 eoltype = eol_mnemonic_undecided;
17452 else if (VECTORP (eolvalue)) /* Not yet decided. */
17453 eoltype = eol_mnemonic_undecided;
17454 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17455 eoltype = (XFASTINT (eolvalue) == 0
17456 ? eol_mnemonic_unix
17457 : (XFASTINT (eolvalue) == 1
17458 ? eol_mnemonic_dos : eol_mnemonic_mac));
17459 }
17460 }
17461
17462 if (eol_flag)
17463 {
17464 /* Mention the EOL conversion if it is not the usual one. */
17465 if (STRINGP (eoltype))
17466 {
17467 eol_str = SDATA (eoltype);
17468 eol_str_len = SBYTES (eoltype);
17469 }
17470 else if (INTEGERP (eoltype)
17471 && CHAR_VALID_P (XINT (eoltype), 0))
17472 {
17473 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17474 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17475 eol_str = tmp;
17476 }
17477 else
17478 {
17479 eol_str = invalid_eol_type;
17480 eol_str_len = sizeof (invalid_eol_type) - 1;
17481 }
17482 bcopy (eol_str, buf, eol_str_len);
17483 buf += eol_str_len;
17484 }
17485
17486 return buf;
17487 }
17488
17489 /* Return a string for the output of a mode line %-spec for window W,
17490 generated by character C. PRECISION >= 0 means don't return a
17491 string longer than that value. FIELD_WIDTH > 0 means pad the
17492 string returned with spaces to that value. Return 1 in *MULTIBYTE
17493 if the result is multibyte text.
17494
17495 Note we operate on the current buffer for most purposes,
17496 the exception being w->base_line_pos. */
17497
17498 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17499
17500 static char *
17501 decode_mode_spec (w, c, field_width, precision, multibyte)
17502 struct window *w;
17503 register int c;
17504 int field_width, precision;
17505 int *multibyte;
17506 {
17507 Lisp_Object obj;
17508 struct frame *f = XFRAME (WINDOW_FRAME (w));
17509 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17510 struct buffer *b = current_buffer;
17511
17512 obj = Qnil;
17513 *multibyte = 0;
17514
17515 switch (c)
17516 {
17517 case '*':
17518 if (!NILP (b->read_only))
17519 return "%";
17520 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17521 return "*";
17522 return "-";
17523
17524 case '+':
17525 /* This differs from %* only for a modified read-only buffer. */
17526 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17527 return "*";
17528 if (!NILP (b->read_only))
17529 return "%";
17530 return "-";
17531
17532 case '&':
17533 /* This differs from %* in ignoring read-only-ness. */
17534 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17535 return "*";
17536 return "-";
17537
17538 case '%':
17539 return "%";
17540
17541 case '[':
17542 {
17543 int i;
17544 char *p;
17545
17546 if (command_loop_level > 5)
17547 return "[[[... ";
17548 p = decode_mode_spec_buf;
17549 for (i = 0; i < command_loop_level; i++)
17550 *p++ = '[';
17551 *p = 0;
17552 return decode_mode_spec_buf;
17553 }
17554
17555 case ']':
17556 {
17557 int i;
17558 char *p;
17559
17560 if (command_loop_level > 5)
17561 return " ...]]]";
17562 p = decode_mode_spec_buf;
17563 for (i = 0; i < command_loop_level; i++)
17564 *p++ = ']';
17565 *p = 0;
17566 return decode_mode_spec_buf;
17567 }
17568
17569 case '-':
17570 {
17571 register int i;
17572
17573 /* Let lots_of_dashes be a string of infinite length. */
17574 if (mode_line_target == MODE_LINE_NOPROP ||
17575 mode_line_target == MODE_LINE_STRING)
17576 return "--";
17577 if (field_width <= 0
17578 || field_width > sizeof (lots_of_dashes))
17579 {
17580 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17581 decode_mode_spec_buf[i] = '-';
17582 decode_mode_spec_buf[i] = '\0';
17583 return decode_mode_spec_buf;
17584 }
17585 else
17586 return lots_of_dashes;
17587 }
17588
17589 case 'b':
17590 obj = b->name;
17591 break;
17592
17593 case 'c':
17594 {
17595 int col = (int) current_column (); /* iftc */
17596 w->column_number_displayed = make_number (col);
17597 pint2str (decode_mode_spec_buf, field_width, col);
17598 return decode_mode_spec_buf;
17599 }
17600
17601 case 'e':
17602 #ifndef SYSTEM_MALLOC
17603 {
17604 if (NILP (Vmemory_full))
17605 return "";
17606 else
17607 return "!MEM FULL! ";
17608 }
17609 #else
17610 return "";
17611 #endif
17612
17613 case 'F':
17614 /* %F displays the frame name. */
17615 if (!NILP (f->title))
17616 return (char *) SDATA (f->title);
17617 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17618 return (char *) SDATA (f->name);
17619 return "Emacs";
17620
17621 case 'f':
17622 obj = b->filename;
17623 break;
17624
17625 case 'i':
17626 {
17627 int size = ZV - BEGV;
17628 pint2str (decode_mode_spec_buf, field_width, size);
17629 return decode_mode_spec_buf;
17630 }
17631
17632 case 'I':
17633 {
17634 int size = ZV - BEGV;
17635 pint2hrstr (decode_mode_spec_buf, field_width, size);
17636 return decode_mode_spec_buf;
17637 }
17638
17639 case 'l':
17640 {
17641 int startpos = XMARKER (w->start)->charpos;
17642 int startpos_byte = marker_byte_position (w->start);
17643 int line, linepos, linepos_byte, topline;
17644 int nlines, junk;
17645 int height = WINDOW_TOTAL_LINES (w);
17646
17647 /* If we decided that this buffer isn't suitable for line numbers,
17648 don't forget that too fast. */
17649 if (EQ (w->base_line_pos, w->buffer))
17650 goto no_value;
17651 /* But do forget it, if the window shows a different buffer now. */
17652 else if (BUFFERP (w->base_line_pos))
17653 w->base_line_pos = Qnil;
17654
17655 /* If the buffer is very big, don't waste time. */
17656 if (INTEGERP (Vline_number_display_limit)
17657 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17658 {
17659 w->base_line_pos = Qnil;
17660 w->base_line_number = Qnil;
17661 goto no_value;
17662 }
17663
17664 if (!NILP (w->base_line_number)
17665 && !NILP (w->base_line_pos)
17666 && XFASTINT (w->base_line_pos) <= startpos)
17667 {
17668 line = XFASTINT (w->base_line_number);
17669 linepos = XFASTINT (w->base_line_pos);
17670 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17671 }
17672 else
17673 {
17674 line = 1;
17675 linepos = BUF_BEGV (b);
17676 linepos_byte = BUF_BEGV_BYTE (b);
17677 }
17678
17679 /* Count lines from base line to window start position. */
17680 nlines = display_count_lines (linepos, linepos_byte,
17681 startpos_byte,
17682 startpos, &junk);
17683
17684 topline = nlines + line;
17685
17686 /* Determine a new base line, if the old one is too close
17687 or too far away, or if we did not have one.
17688 "Too close" means it's plausible a scroll-down would
17689 go back past it. */
17690 if (startpos == BUF_BEGV (b))
17691 {
17692 w->base_line_number = make_number (topline);
17693 w->base_line_pos = make_number (BUF_BEGV (b));
17694 }
17695 else if (nlines < height + 25 || nlines > height * 3 + 50
17696 || linepos == BUF_BEGV (b))
17697 {
17698 int limit = BUF_BEGV (b);
17699 int limit_byte = BUF_BEGV_BYTE (b);
17700 int position;
17701 int distance = (height * 2 + 30) * line_number_display_limit_width;
17702
17703 if (startpos - distance > limit)
17704 {
17705 limit = startpos - distance;
17706 limit_byte = CHAR_TO_BYTE (limit);
17707 }
17708
17709 nlines = display_count_lines (startpos, startpos_byte,
17710 limit_byte,
17711 - (height * 2 + 30),
17712 &position);
17713 /* If we couldn't find the lines we wanted within
17714 line_number_display_limit_width chars per line,
17715 give up on line numbers for this window. */
17716 if (position == limit_byte && limit == startpos - distance)
17717 {
17718 w->base_line_pos = w->buffer;
17719 w->base_line_number = Qnil;
17720 goto no_value;
17721 }
17722
17723 w->base_line_number = make_number (topline - nlines);
17724 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17725 }
17726
17727 /* Now count lines from the start pos to point. */
17728 nlines = display_count_lines (startpos, startpos_byte,
17729 PT_BYTE, PT, &junk);
17730
17731 /* Record that we did display the line number. */
17732 line_number_displayed = 1;
17733
17734 /* Make the string to show. */
17735 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17736 return decode_mode_spec_buf;
17737 no_value:
17738 {
17739 char* p = decode_mode_spec_buf;
17740 int pad = field_width - 2;
17741 while (pad-- > 0)
17742 *p++ = ' ';
17743 *p++ = '?';
17744 *p++ = '?';
17745 *p = '\0';
17746 return decode_mode_spec_buf;
17747 }
17748 }
17749 break;
17750
17751 case 'm':
17752 obj = b->mode_name;
17753 break;
17754
17755 case 'n':
17756 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17757 return " Narrow";
17758 break;
17759
17760 case 'p':
17761 {
17762 int pos = marker_position (w->start);
17763 int total = BUF_ZV (b) - BUF_BEGV (b);
17764
17765 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17766 {
17767 if (pos <= BUF_BEGV (b))
17768 return "All";
17769 else
17770 return "Bottom";
17771 }
17772 else if (pos <= BUF_BEGV (b))
17773 return "Top";
17774 else
17775 {
17776 if (total > 1000000)
17777 /* Do it differently for a large value, to avoid overflow. */
17778 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17779 else
17780 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17781 /* We can't normally display a 3-digit number,
17782 so get us a 2-digit number that is close. */
17783 if (total == 100)
17784 total = 99;
17785 sprintf (decode_mode_spec_buf, "%2d%%", total);
17786 return decode_mode_spec_buf;
17787 }
17788 }
17789
17790 /* Display percentage of size above the bottom of the screen. */
17791 case 'P':
17792 {
17793 int toppos = marker_position (w->start);
17794 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17795 int total = BUF_ZV (b) - BUF_BEGV (b);
17796
17797 if (botpos >= BUF_ZV (b))
17798 {
17799 if (toppos <= BUF_BEGV (b))
17800 return "All";
17801 else
17802 return "Bottom";
17803 }
17804 else
17805 {
17806 if (total > 1000000)
17807 /* Do it differently for a large value, to avoid overflow. */
17808 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17809 else
17810 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17811 /* We can't normally display a 3-digit number,
17812 so get us a 2-digit number that is close. */
17813 if (total == 100)
17814 total = 99;
17815 if (toppos <= BUF_BEGV (b))
17816 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17817 else
17818 sprintf (decode_mode_spec_buf, "%2d%%", total);
17819 return decode_mode_spec_buf;
17820 }
17821 }
17822
17823 case 's':
17824 /* status of process */
17825 obj = Fget_buffer_process (Fcurrent_buffer ());
17826 if (NILP (obj))
17827 return "no process";
17828 #ifdef subprocesses
17829 obj = Fsymbol_name (Fprocess_status (obj));
17830 #endif
17831 break;
17832
17833 case 't': /* indicate TEXT or BINARY */
17834 #ifdef MODE_LINE_BINARY_TEXT
17835 return MODE_LINE_BINARY_TEXT (b);
17836 #else
17837 return "T";
17838 #endif
17839
17840 case 'z':
17841 /* coding-system (not including end-of-line format) */
17842 case 'Z':
17843 /* coding-system (including end-of-line type) */
17844 {
17845 int eol_flag = (c == 'Z');
17846 char *p = decode_mode_spec_buf;
17847
17848 if (! FRAME_WINDOW_P (f))
17849 {
17850 /* No need to mention EOL here--the terminal never needs
17851 to do EOL conversion. */
17852 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17853 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17854 }
17855 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17856 p, eol_flag);
17857
17858 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17859 #ifdef subprocesses
17860 obj = Fget_buffer_process (Fcurrent_buffer ());
17861 if (PROCESSP (obj))
17862 {
17863 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17864 p, eol_flag);
17865 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17866 p, eol_flag);
17867 }
17868 #endif /* subprocesses */
17869 #endif /* 0 */
17870 *p = 0;
17871 return decode_mode_spec_buf;
17872 }
17873 }
17874
17875 if (STRINGP (obj))
17876 {
17877 *multibyte = STRING_MULTIBYTE (obj);
17878 return (char *) SDATA (obj);
17879 }
17880 else
17881 return "";
17882 }
17883
17884
17885 /* Count up to COUNT lines starting from START / START_BYTE.
17886 But don't go beyond LIMIT_BYTE.
17887 Return the number of lines thus found (always nonnegative).
17888
17889 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17890
17891 static int
17892 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17893 int start, start_byte, limit_byte, count;
17894 int *byte_pos_ptr;
17895 {
17896 register unsigned char *cursor;
17897 unsigned char *base;
17898
17899 register int ceiling;
17900 register unsigned char *ceiling_addr;
17901 int orig_count = count;
17902
17903 /* If we are not in selective display mode,
17904 check only for newlines. */
17905 int selective_display = (!NILP (current_buffer->selective_display)
17906 && !INTEGERP (current_buffer->selective_display));
17907
17908 if (count > 0)
17909 {
17910 while (start_byte < limit_byte)
17911 {
17912 ceiling = BUFFER_CEILING_OF (start_byte);
17913 ceiling = min (limit_byte - 1, ceiling);
17914 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17915 base = (cursor = BYTE_POS_ADDR (start_byte));
17916 while (1)
17917 {
17918 if (selective_display)
17919 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17920 ;
17921 else
17922 while (*cursor != '\n' && ++cursor != ceiling_addr)
17923 ;
17924
17925 if (cursor != ceiling_addr)
17926 {
17927 if (--count == 0)
17928 {
17929 start_byte += cursor - base + 1;
17930 *byte_pos_ptr = start_byte;
17931 return orig_count;
17932 }
17933 else
17934 if (++cursor == ceiling_addr)
17935 break;
17936 }
17937 else
17938 break;
17939 }
17940 start_byte += cursor - base;
17941 }
17942 }
17943 else
17944 {
17945 while (start_byte > limit_byte)
17946 {
17947 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17948 ceiling = max (limit_byte, ceiling);
17949 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17950 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17951 while (1)
17952 {
17953 if (selective_display)
17954 while (--cursor != ceiling_addr
17955 && *cursor != '\n' && *cursor != 015)
17956 ;
17957 else
17958 while (--cursor != ceiling_addr && *cursor != '\n')
17959 ;
17960
17961 if (cursor != ceiling_addr)
17962 {
17963 if (++count == 0)
17964 {
17965 start_byte += cursor - base + 1;
17966 *byte_pos_ptr = start_byte;
17967 /* When scanning backwards, we should
17968 not count the newline posterior to which we stop. */
17969 return - orig_count - 1;
17970 }
17971 }
17972 else
17973 break;
17974 }
17975 /* Here we add 1 to compensate for the last decrement
17976 of CURSOR, which took it past the valid range. */
17977 start_byte += cursor - base + 1;
17978 }
17979 }
17980
17981 *byte_pos_ptr = limit_byte;
17982
17983 if (count < 0)
17984 return - orig_count + count;
17985 return orig_count - count;
17986
17987 }
17988
17989
17990 \f
17991 /***********************************************************************
17992 Displaying strings
17993 ***********************************************************************/
17994
17995 /* Display a NUL-terminated string, starting with index START.
17996
17997 If STRING is non-null, display that C string. Otherwise, the Lisp
17998 string LISP_STRING is displayed.
17999
18000 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18001 FACE_STRING. Display STRING or LISP_STRING with the face at
18002 FACE_STRING_POS in FACE_STRING:
18003
18004 Display the string in the environment given by IT, but use the
18005 standard display table, temporarily.
18006
18007 FIELD_WIDTH is the minimum number of output glyphs to produce.
18008 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18009 with spaces. If STRING has more characters, more than FIELD_WIDTH
18010 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18011
18012 PRECISION is the maximum number of characters to output from
18013 STRING. PRECISION < 0 means don't truncate the string.
18014
18015 This is roughly equivalent to printf format specifiers:
18016
18017 FIELD_WIDTH PRECISION PRINTF
18018 ----------------------------------------
18019 -1 -1 %s
18020 -1 10 %.10s
18021 10 -1 %10s
18022 20 10 %20.10s
18023
18024 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18025 display them, and < 0 means obey the current buffer's value of
18026 enable_multibyte_characters.
18027
18028 Value is the number of columns displayed. */
18029
18030 static int
18031 display_string (string, lisp_string, face_string, face_string_pos,
18032 start, it, field_width, precision, max_x, multibyte)
18033 unsigned char *string;
18034 Lisp_Object lisp_string;
18035 Lisp_Object face_string;
18036 int face_string_pos;
18037 int start;
18038 struct it *it;
18039 int field_width, precision, max_x;
18040 int multibyte;
18041 {
18042 int hpos_at_start = it->hpos;
18043 int saved_face_id = it->face_id;
18044 struct glyph_row *row = it->glyph_row;
18045
18046 /* Initialize the iterator IT for iteration over STRING beginning
18047 with index START. */
18048 reseat_to_string (it, string, lisp_string, start,
18049 precision, field_width, multibyte);
18050
18051 /* If displaying STRING, set up the face of the iterator
18052 from LISP_STRING, if that's given. */
18053 if (STRINGP (face_string))
18054 {
18055 int endptr;
18056 struct face *face;
18057
18058 it->face_id
18059 = face_at_string_position (it->w, face_string, face_string_pos,
18060 0, it->region_beg_charpos,
18061 it->region_end_charpos,
18062 &endptr, it->base_face_id, 0);
18063 face = FACE_FROM_ID (it->f, it->face_id);
18064 it->face_box_p = face->box != FACE_NO_BOX;
18065 }
18066
18067 /* Set max_x to the maximum allowed X position. Don't let it go
18068 beyond the right edge of the window. */
18069 if (max_x <= 0)
18070 max_x = it->last_visible_x;
18071 else
18072 max_x = min (max_x, it->last_visible_x);
18073
18074 /* Skip over display elements that are not visible. because IT->w is
18075 hscrolled. */
18076 if (it->current_x < it->first_visible_x)
18077 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18078 MOVE_TO_POS | MOVE_TO_X);
18079
18080 row->ascent = it->max_ascent;
18081 row->height = it->max_ascent + it->max_descent;
18082 row->phys_ascent = it->max_phys_ascent;
18083 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18084 row->extra_line_spacing = it->max_extra_line_spacing;
18085
18086 /* This condition is for the case that we are called with current_x
18087 past last_visible_x. */
18088 while (it->current_x < max_x)
18089 {
18090 int x_before, x, n_glyphs_before, i, nglyphs;
18091
18092 /* Get the next display element. */
18093 if (!get_next_display_element (it))
18094 break;
18095
18096 /* Produce glyphs. */
18097 x_before = it->current_x;
18098 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18099 PRODUCE_GLYPHS (it);
18100
18101 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18102 i = 0;
18103 x = x_before;
18104 while (i < nglyphs)
18105 {
18106 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18107
18108 if (!it->truncate_lines_p
18109 && x + glyph->pixel_width > max_x)
18110 {
18111 /* End of continued line or max_x reached. */
18112 if (CHAR_GLYPH_PADDING_P (*glyph))
18113 {
18114 /* A wide character is unbreakable. */
18115 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18116 it->current_x = x_before;
18117 }
18118 else
18119 {
18120 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18121 it->current_x = x;
18122 }
18123 break;
18124 }
18125 else if (x + glyph->pixel_width > it->first_visible_x)
18126 {
18127 /* Glyph is at least partially visible. */
18128 ++it->hpos;
18129 if (x < it->first_visible_x)
18130 it->glyph_row->x = x - it->first_visible_x;
18131 }
18132 else
18133 {
18134 /* Glyph is off the left margin of the display area.
18135 Should not happen. */
18136 abort ();
18137 }
18138
18139 row->ascent = max (row->ascent, it->max_ascent);
18140 row->height = max (row->height, it->max_ascent + it->max_descent);
18141 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18142 row->phys_height = max (row->phys_height,
18143 it->max_phys_ascent + it->max_phys_descent);
18144 row->extra_line_spacing = max (row->extra_line_spacing,
18145 it->max_extra_line_spacing);
18146 x += glyph->pixel_width;
18147 ++i;
18148 }
18149
18150 /* Stop if max_x reached. */
18151 if (i < nglyphs)
18152 break;
18153
18154 /* Stop at line ends. */
18155 if (ITERATOR_AT_END_OF_LINE_P (it))
18156 {
18157 it->continuation_lines_width = 0;
18158 break;
18159 }
18160
18161 set_iterator_to_next (it, 1);
18162
18163 /* Stop if truncating at the right edge. */
18164 if (it->truncate_lines_p
18165 && it->current_x >= it->last_visible_x)
18166 {
18167 /* Add truncation mark, but don't do it if the line is
18168 truncated at a padding space. */
18169 if (IT_CHARPOS (*it) < it->string_nchars)
18170 {
18171 if (!FRAME_WINDOW_P (it->f))
18172 {
18173 int i, n;
18174
18175 if (it->current_x > it->last_visible_x)
18176 {
18177 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18178 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18179 break;
18180 for (n = row->used[TEXT_AREA]; i < n; ++i)
18181 {
18182 row->used[TEXT_AREA] = i;
18183 produce_special_glyphs (it, IT_TRUNCATION);
18184 }
18185 }
18186 produce_special_glyphs (it, IT_TRUNCATION);
18187 }
18188 it->glyph_row->truncated_on_right_p = 1;
18189 }
18190 break;
18191 }
18192 }
18193
18194 /* Maybe insert a truncation at the left. */
18195 if (it->first_visible_x
18196 && IT_CHARPOS (*it) > 0)
18197 {
18198 if (!FRAME_WINDOW_P (it->f))
18199 insert_left_trunc_glyphs (it);
18200 it->glyph_row->truncated_on_left_p = 1;
18201 }
18202
18203 it->face_id = saved_face_id;
18204
18205 /* Value is number of columns displayed. */
18206 return it->hpos - hpos_at_start;
18207 }
18208
18209
18210 \f
18211 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18212 appears as an element of LIST or as the car of an element of LIST.
18213 If PROPVAL is a list, compare each element against LIST in that
18214 way, and return 1/2 if any element of PROPVAL is found in LIST.
18215 Otherwise return 0. This function cannot quit.
18216 The return value is 2 if the text is invisible but with an ellipsis
18217 and 1 if it's invisible and without an ellipsis. */
18218
18219 int
18220 invisible_p (propval, list)
18221 register Lisp_Object propval;
18222 Lisp_Object list;
18223 {
18224 register Lisp_Object tail, proptail;
18225
18226 for (tail = list; CONSP (tail); tail = XCDR (tail))
18227 {
18228 register Lisp_Object tem;
18229 tem = XCAR (tail);
18230 if (EQ (propval, tem))
18231 return 1;
18232 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18233 return NILP (XCDR (tem)) ? 1 : 2;
18234 }
18235
18236 if (CONSP (propval))
18237 {
18238 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18239 {
18240 Lisp_Object propelt;
18241 propelt = XCAR (proptail);
18242 for (tail = list; CONSP (tail); tail = XCDR (tail))
18243 {
18244 register Lisp_Object tem;
18245 tem = XCAR (tail);
18246 if (EQ (propelt, tem))
18247 return 1;
18248 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18249 return NILP (XCDR (tem)) ? 1 : 2;
18250 }
18251 }
18252 }
18253
18254 return 0;
18255 }
18256
18257 /* Calculate a width or height in pixels from a specification using
18258 the following elements:
18259
18260 SPEC ::=
18261 NUM - a (fractional) multiple of the default font width/height
18262 (NUM) - specifies exactly NUM pixels
18263 UNIT - a fixed number of pixels, see below.
18264 ELEMENT - size of a display element in pixels, see below.
18265 (NUM . SPEC) - equals NUM * SPEC
18266 (+ SPEC SPEC ...) - add pixel values
18267 (- SPEC SPEC ...) - subtract pixel values
18268 (- SPEC) - negate pixel value
18269
18270 NUM ::=
18271 INT or FLOAT - a number constant
18272 SYMBOL - use symbol's (buffer local) variable binding.
18273
18274 UNIT ::=
18275 in - pixels per inch *)
18276 mm - pixels per 1/1000 meter *)
18277 cm - pixels per 1/100 meter *)
18278 width - width of current font in pixels.
18279 height - height of current font in pixels.
18280
18281 *) using the ratio(s) defined in display-pixels-per-inch.
18282
18283 ELEMENT ::=
18284
18285 left-fringe - left fringe width in pixels
18286 right-fringe - right fringe width in pixels
18287
18288 left-margin - left margin width in pixels
18289 right-margin - right margin width in pixels
18290
18291 scroll-bar - scroll-bar area width in pixels
18292
18293 Examples:
18294
18295 Pixels corresponding to 5 inches:
18296 (5 . in)
18297
18298 Total width of non-text areas on left side of window (if scroll-bar is on left):
18299 '(space :width (+ left-fringe left-margin scroll-bar))
18300
18301 Align to first text column (in header line):
18302 '(space :align-to 0)
18303
18304 Align to middle of text area minus half the width of variable `my-image'
18305 containing a loaded image:
18306 '(space :align-to (0.5 . (- text my-image)))
18307
18308 Width of left margin minus width of 1 character in the default font:
18309 '(space :width (- left-margin 1))
18310
18311 Width of left margin minus width of 2 characters in the current font:
18312 '(space :width (- left-margin (2 . width)))
18313
18314 Center 1 character over left-margin (in header line):
18315 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18316
18317 Different ways to express width of left fringe plus left margin minus one pixel:
18318 '(space :width (- (+ left-fringe left-margin) (1)))
18319 '(space :width (+ left-fringe left-margin (- (1))))
18320 '(space :width (+ left-fringe left-margin (-1)))
18321
18322 */
18323
18324 #define NUMVAL(X) \
18325 ((INTEGERP (X) || FLOATP (X)) \
18326 ? XFLOATINT (X) \
18327 : - 1)
18328
18329 int
18330 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18331 double *res;
18332 struct it *it;
18333 Lisp_Object prop;
18334 void *font;
18335 int width_p, *align_to;
18336 {
18337 double pixels;
18338
18339 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18340 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18341
18342 if (NILP (prop))
18343 return OK_PIXELS (0);
18344
18345 if (SYMBOLP (prop))
18346 {
18347 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18348 {
18349 char *unit = SDATA (SYMBOL_NAME (prop));
18350
18351 if (unit[0] == 'i' && unit[1] == 'n')
18352 pixels = 1.0;
18353 else if (unit[0] == 'm' && unit[1] == 'm')
18354 pixels = 25.4;
18355 else if (unit[0] == 'c' && unit[1] == 'm')
18356 pixels = 2.54;
18357 else
18358 pixels = 0;
18359 if (pixels > 0)
18360 {
18361 double ppi;
18362 #ifdef HAVE_WINDOW_SYSTEM
18363 if (FRAME_WINDOW_P (it->f)
18364 && (ppi = (width_p
18365 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18366 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18367 ppi > 0))
18368 return OK_PIXELS (ppi / pixels);
18369 #endif
18370
18371 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18372 || (CONSP (Vdisplay_pixels_per_inch)
18373 && (ppi = (width_p
18374 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18375 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18376 ppi > 0)))
18377 return OK_PIXELS (ppi / pixels);
18378
18379 return 0;
18380 }
18381 }
18382
18383 #ifdef HAVE_WINDOW_SYSTEM
18384 if (EQ (prop, Qheight))
18385 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18386 if (EQ (prop, Qwidth))
18387 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18388 #else
18389 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18390 return OK_PIXELS (1);
18391 #endif
18392
18393 if (EQ (prop, Qtext))
18394 return OK_PIXELS (width_p
18395 ? window_box_width (it->w, TEXT_AREA)
18396 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18397
18398 if (align_to && *align_to < 0)
18399 {
18400 *res = 0;
18401 if (EQ (prop, Qleft))
18402 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18403 if (EQ (prop, Qright))
18404 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18405 if (EQ (prop, Qcenter))
18406 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18407 + window_box_width (it->w, TEXT_AREA) / 2);
18408 if (EQ (prop, Qleft_fringe))
18409 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18410 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18411 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18412 if (EQ (prop, Qright_fringe))
18413 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18414 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18415 : window_box_right_offset (it->w, TEXT_AREA));
18416 if (EQ (prop, Qleft_margin))
18417 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18418 if (EQ (prop, Qright_margin))
18419 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18420 if (EQ (prop, Qscroll_bar))
18421 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18422 ? 0
18423 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18424 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18425 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18426 : 0)));
18427 }
18428 else
18429 {
18430 if (EQ (prop, Qleft_fringe))
18431 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18432 if (EQ (prop, Qright_fringe))
18433 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18434 if (EQ (prop, Qleft_margin))
18435 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18436 if (EQ (prop, Qright_margin))
18437 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18438 if (EQ (prop, Qscroll_bar))
18439 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18440 }
18441
18442 prop = Fbuffer_local_value (prop, it->w->buffer);
18443 }
18444
18445 if (INTEGERP (prop) || FLOATP (prop))
18446 {
18447 int base_unit = (width_p
18448 ? FRAME_COLUMN_WIDTH (it->f)
18449 : FRAME_LINE_HEIGHT (it->f));
18450 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18451 }
18452
18453 if (CONSP (prop))
18454 {
18455 Lisp_Object car = XCAR (prop);
18456 Lisp_Object cdr = XCDR (prop);
18457
18458 if (SYMBOLP (car))
18459 {
18460 #ifdef HAVE_WINDOW_SYSTEM
18461 if (valid_image_p (prop))
18462 {
18463 int id = lookup_image (it->f, prop);
18464 struct image *img = IMAGE_FROM_ID (it->f, id);
18465
18466 return OK_PIXELS (width_p ? img->width : img->height);
18467 }
18468 #endif
18469 if (EQ (car, Qplus) || EQ (car, Qminus))
18470 {
18471 int first = 1;
18472 double px;
18473
18474 pixels = 0;
18475 while (CONSP (cdr))
18476 {
18477 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18478 font, width_p, align_to))
18479 return 0;
18480 if (first)
18481 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18482 else
18483 pixels += px;
18484 cdr = XCDR (cdr);
18485 }
18486 if (EQ (car, Qminus))
18487 pixels = -pixels;
18488 return OK_PIXELS (pixels);
18489 }
18490
18491 car = Fbuffer_local_value (car, it->w->buffer);
18492 }
18493
18494 if (INTEGERP (car) || FLOATP (car))
18495 {
18496 double fact;
18497 pixels = XFLOATINT (car);
18498 if (NILP (cdr))
18499 return OK_PIXELS (pixels);
18500 if (calc_pixel_width_or_height (&fact, it, cdr,
18501 font, width_p, align_to))
18502 return OK_PIXELS (pixels * fact);
18503 return 0;
18504 }
18505
18506 return 0;
18507 }
18508
18509 return 0;
18510 }
18511
18512 \f
18513 /***********************************************************************
18514 Glyph Display
18515 ***********************************************************************/
18516
18517 #ifdef HAVE_WINDOW_SYSTEM
18518
18519 #if GLYPH_DEBUG
18520
18521 void
18522 dump_glyph_string (s)
18523 struct glyph_string *s;
18524 {
18525 fprintf (stderr, "glyph string\n");
18526 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18527 s->x, s->y, s->width, s->height);
18528 fprintf (stderr, " ybase = %d\n", s->ybase);
18529 fprintf (stderr, " hl = %d\n", s->hl);
18530 fprintf (stderr, " left overhang = %d, right = %d\n",
18531 s->left_overhang, s->right_overhang);
18532 fprintf (stderr, " nchars = %d\n", s->nchars);
18533 fprintf (stderr, " extends to end of line = %d\n",
18534 s->extends_to_end_of_line_p);
18535 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18536 fprintf (stderr, " bg width = %d\n", s->background_width);
18537 }
18538
18539 #endif /* GLYPH_DEBUG */
18540
18541 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18542 of XChar2b structures for S; it can't be allocated in
18543 init_glyph_string because it must be allocated via `alloca'. W
18544 is the window on which S is drawn. ROW and AREA are the glyph row
18545 and area within the row from which S is constructed. START is the
18546 index of the first glyph structure covered by S. HL is a
18547 face-override for drawing S. */
18548
18549 #ifdef HAVE_NTGUI
18550 #define OPTIONAL_HDC(hdc) hdc,
18551 #define DECLARE_HDC(hdc) HDC hdc;
18552 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18553 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18554 #endif
18555
18556 #ifndef OPTIONAL_HDC
18557 #define OPTIONAL_HDC(hdc)
18558 #define DECLARE_HDC(hdc)
18559 #define ALLOCATE_HDC(hdc, f)
18560 #define RELEASE_HDC(hdc, f)
18561 #endif
18562
18563 static void
18564 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18565 struct glyph_string *s;
18566 DECLARE_HDC (hdc)
18567 XChar2b *char2b;
18568 struct window *w;
18569 struct glyph_row *row;
18570 enum glyph_row_area area;
18571 int start;
18572 enum draw_glyphs_face hl;
18573 {
18574 bzero (s, sizeof *s);
18575 s->w = w;
18576 s->f = XFRAME (w->frame);
18577 #ifdef HAVE_NTGUI
18578 s->hdc = hdc;
18579 #endif
18580 s->display = FRAME_X_DISPLAY (s->f);
18581 s->window = FRAME_X_WINDOW (s->f);
18582 s->char2b = char2b;
18583 s->hl = hl;
18584 s->row = row;
18585 s->area = area;
18586 s->first_glyph = row->glyphs[area] + start;
18587 s->height = row->height;
18588 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18589
18590 /* Display the internal border below the tool-bar window. */
18591 if (WINDOWP (s->f->tool_bar_window)
18592 && s->w == XWINDOW (s->f->tool_bar_window))
18593 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18594
18595 s->ybase = s->y + row->ascent;
18596 }
18597
18598
18599 /* Append the list of glyph strings with head H and tail T to the list
18600 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18601
18602 static INLINE void
18603 append_glyph_string_lists (head, tail, h, t)
18604 struct glyph_string **head, **tail;
18605 struct glyph_string *h, *t;
18606 {
18607 if (h)
18608 {
18609 if (*head)
18610 (*tail)->next = h;
18611 else
18612 *head = h;
18613 h->prev = *tail;
18614 *tail = t;
18615 }
18616 }
18617
18618
18619 /* Prepend the list of glyph strings with head H and tail T to the
18620 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18621 result. */
18622
18623 static INLINE void
18624 prepend_glyph_string_lists (head, tail, h, t)
18625 struct glyph_string **head, **tail;
18626 struct glyph_string *h, *t;
18627 {
18628 if (h)
18629 {
18630 if (*head)
18631 (*head)->prev = t;
18632 else
18633 *tail = t;
18634 t->next = *head;
18635 *head = h;
18636 }
18637 }
18638
18639
18640 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18641 Set *HEAD and *TAIL to the resulting list. */
18642
18643 static INLINE void
18644 append_glyph_string (head, tail, s)
18645 struct glyph_string **head, **tail;
18646 struct glyph_string *s;
18647 {
18648 s->next = s->prev = NULL;
18649 append_glyph_string_lists (head, tail, s, s);
18650 }
18651
18652
18653 /* Get face and two-byte form of character glyph GLYPH on frame F.
18654 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18655 a pointer to a realized face that is ready for display. */
18656
18657 static INLINE struct face *
18658 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18659 struct frame *f;
18660 struct glyph *glyph;
18661 XChar2b *char2b;
18662 int *two_byte_p;
18663 {
18664 struct face *face;
18665
18666 xassert (glyph->type == CHAR_GLYPH);
18667 face = FACE_FROM_ID (f, glyph->face_id);
18668
18669 if (two_byte_p)
18670 *two_byte_p = 0;
18671
18672 if (!glyph->multibyte_p)
18673 {
18674 /* Unibyte case. We don't have to encode, but we have to make
18675 sure to use a face suitable for unibyte. */
18676 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18677 }
18678 else if (glyph->u.ch < 128)
18679 {
18680 /* Case of ASCII in a face known to fit ASCII. */
18681 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18682 }
18683 else
18684 {
18685 int c1, c2, charset;
18686
18687 /* Split characters into bytes. If c2 is -1 afterwards, C is
18688 really a one-byte character so that byte1 is zero. */
18689 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18690 if (c2 > 0)
18691 STORE_XCHAR2B (char2b, c1, c2);
18692 else
18693 STORE_XCHAR2B (char2b, 0, c1);
18694
18695 /* Maybe encode the character in *CHAR2B. */
18696 if (charset != CHARSET_ASCII)
18697 {
18698 struct font_info *font_info
18699 = FONT_INFO_FROM_ID (f, face->font_info_id);
18700 if (font_info)
18701 glyph->font_type
18702 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18703 }
18704 }
18705
18706 /* Make sure X resources of the face are allocated. */
18707 xassert (face != NULL);
18708 PREPARE_FACE_FOR_DISPLAY (f, face);
18709 return face;
18710 }
18711
18712
18713 /* Fill glyph string S with composition components specified by S->cmp.
18714
18715 FACES is an array of faces for all components of this composition.
18716 S->gidx is the index of the first component for S.
18717
18718 OVERLAPS non-zero means S should draw the foreground only, and use
18719 its physical height for clipping. See also draw_glyphs.
18720
18721 Value is the index of a component not in S. */
18722
18723 static int
18724 fill_composite_glyph_string (s, faces, overlaps)
18725 struct glyph_string *s;
18726 struct face **faces;
18727 int overlaps;
18728 {
18729 int i;
18730
18731 xassert (s);
18732
18733 s->for_overlaps = overlaps;
18734
18735 s->face = faces[s->gidx];
18736 s->font = s->face->font;
18737 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18738
18739 /* For all glyphs of this composition, starting at the offset
18740 S->gidx, until we reach the end of the definition or encounter a
18741 glyph that requires the different face, add it to S. */
18742 ++s->nchars;
18743 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18744 ++s->nchars;
18745
18746 /* All glyph strings for the same composition has the same width,
18747 i.e. the width set for the first component of the composition. */
18748
18749 s->width = s->first_glyph->pixel_width;
18750
18751 /* If the specified font could not be loaded, use the frame's
18752 default font, but record the fact that we couldn't load it in
18753 the glyph string so that we can draw rectangles for the
18754 characters of the glyph string. */
18755 if (s->font == NULL)
18756 {
18757 s->font_not_found_p = 1;
18758 s->font = FRAME_FONT (s->f);
18759 }
18760
18761 /* Adjust base line for subscript/superscript text. */
18762 s->ybase += s->first_glyph->voffset;
18763
18764 xassert (s->face && s->face->gc);
18765
18766 /* This glyph string must always be drawn with 16-bit functions. */
18767 s->two_byte_p = 1;
18768
18769 return s->gidx + s->nchars;
18770 }
18771
18772
18773 /* Fill glyph string S from a sequence of character glyphs.
18774
18775 FACE_ID is the face id of the string. START is the index of the
18776 first glyph to consider, END is the index of the last + 1.
18777 OVERLAPS non-zero means S should draw the foreground only, and use
18778 its physical height for clipping. See also draw_glyphs.
18779
18780 Value is the index of the first glyph not in S. */
18781
18782 static int
18783 fill_glyph_string (s, face_id, start, end, overlaps)
18784 struct glyph_string *s;
18785 int face_id;
18786 int start, end, overlaps;
18787 {
18788 struct glyph *glyph, *last;
18789 int voffset;
18790 int glyph_not_available_p;
18791
18792 xassert (s->f == XFRAME (s->w->frame));
18793 xassert (s->nchars == 0);
18794 xassert (start >= 0 && end > start);
18795
18796 s->for_overlaps = overlaps,
18797 glyph = s->row->glyphs[s->area] + start;
18798 last = s->row->glyphs[s->area] + end;
18799 voffset = glyph->voffset;
18800
18801 glyph_not_available_p = glyph->glyph_not_available_p;
18802
18803 while (glyph < last
18804 && glyph->type == CHAR_GLYPH
18805 && glyph->voffset == voffset
18806 /* Same face id implies same font, nowadays. */
18807 && glyph->face_id == face_id
18808 && glyph->glyph_not_available_p == glyph_not_available_p)
18809 {
18810 int two_byte_p;
18811
18812 s->face = get_glyph_face_and_encoding (s->f, glyph,
18813 s->char2b + s->nchars,
18814 &two_byte_p);
18815 s->two_byte_p = two_byte_p;
18816 ++s->nchars;
18817 xassert (s->nchars <= end - start);
18818 s->width += glyph->pixel_width;
18819 ++glyph;
18820 }
18821
18822 s->font = s->face->font;
18823 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18824
18825 /* If the specified font could not be loaded, use the frame's font,
18826 but record the fact that we couldn't load it in
18827 S->font_not_found_p so that we can draw rectangles for the
18828 characters of the glyph string. */
18829 if (s->font == NULL || glyph_not_available_p)
18830 {
18831 s->font_not_found_p = 1;
18832 s->font = FRAME_FONT (s->f);
18833 }
18834
18835 /* Adjust base line for subscript/superscript text. */
18836 s->ybase += voffset;
18837
18838 xassert (s->face && s->face->gc);
18839 return glyph - s->row->glyphs[s->area];
18840 }
18841
18842
18843 /* Fill glyph string S from image glyph S->first_glyph. */
18844
18845 static void
18846 fill_image_glyph_string (s)
18847 struct glyph_string *s;
18848 {
18849 xassert (s->first_glyph->type == IMAGE_GLYPH);
18850 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18851 xassert (s->img);
18852 s->slice = s->first_glyph->slice;
18853 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18854 s->font = s->face->font;
18855 s->width = s->first_glyph->pixel_width;
18856
18857 /* Adjust base line for subscript/superscript text. */
18858 s->ybase += s->first_glyph->voffset;
18859 }
18860
18861
18862 /* Fill glyph string S from a sequence of stretch glyphs.
18863
18864 ROW is the glyph row in which the glyphs are found, AREA is the
18865 area within the row. START is the index of the first glyph to
18866 consider, END is the index of the last + 1.
18867
18868 Value is the index of the first glyph not in S. */
18869
18870 static int
18871 fill_stretch_glyph_string (s, row, area, start, end)
18872 struct glyph_string *s;
18873 struct glyph_row *row;
18874 enum glyph_row_area area;
18875 int start, end;
18876 {
18877 struct glyph *glyph, *last;
18878 int voffset, face_id;
18879
18880 xassert (s->first_glyph->type == STRETCH_GLYPH);
18881
18882 glyph = s->row->glyphs[s->area] + start;
18883 last = s->row->glyphs[s->area] + end;
18884 face_id = glyph->face_id;
18885 s->face = FACE_FROM_ID (s->f, face_id);
18886 s->font = s->face->font;
18887 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18888 s->width = glyph->pixel_width;
18889 s->nchars = 1;
18890 voffset = glyph->voffset;
18891
18892 for (++glyph;
18893 (glyph < last
18894 && glyph->type == STRETCH_GLYPH
18895 && glyph->voffset == voffset
18896 && glyph->face_id == face_id);
18897 ++glyph)
18898 s->width += glyph->pixel_width;
18899
18900 /* Adjust base line for subscript/superscript text. */
18901 s->ybase += voffset;
18902
18903 /* The case that face->gc == 0 is handled when drawing the glyph
18904 string by calling PREPARE_FACE_FOR_DISPLAY. */
18905 xassert (s->face);
18906 return glyph - s->row->glyphs[s->area];
18907 }
18908
18909
18910 /* EXPORT for RIF:
18911 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18912 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18913 assumed to be zero. */
18914
18915 void
18916 x_get_glyph_overhangs (glyph, f, left, right)
18917 struct glyph *glyph;
18918 struct frame *f;
18919 int *left, *right;
18920 {
18921 *left = *right = 0;
18922
18923 if (glyph->type == CHAR_GLYPH)
18924 {
18925 XFontStruct *font;
18926 struct face *face;
18927 struct font_info *font_info;
18928 XChar2b char2b;
18929 XCharStruct *pcm;
18930
18931 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18932 font = face->font;
18933 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18934 if (font /* ++KFS: Should this be font_info ? */
18935 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18936 {
18937 if (pcm->rbearing > pcm->width)
18938 *right = pcm->rbearing - pcm->width;
18939 if (pcm->lbearing < 0)
18940 *left = -pcm->lbearing;
18941 }
18942 }
18943 }
18944
18945
18946 /* Return the index of the first glyph preceding glyph string S that
18947 is overwritten by S because of S's left overhang. Value is -1
18948 if no glyphs are overwritten. */
18949
18950 static int
18951 left_overwritten (s)
18952 struct glyph_string *s;
18953 {
18954 int k;
18955
18956 if (s->left_overhang)
18957 {
18958 int x = 0, i;
18959 struct glyph *glyphs = s->row->glyphs[s->area];
18960 int first = s->first_glyph - glyphs;
18961
18962 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18963 x -= glyphs[i].pixel_width;
18964
18965 k = i + 1;
18966 }
18967 else
18968 k = -1;
18969
18970 return k;
18971 }
18972
18973
18974 /* Return the index of the first glyph preceding glyph string S that
18975 is overwriting S because of its right overhang. Value is -1 if no
18976 glyph in front of S overwrites S. */
18977
18978 static int
18979 left_overwriting (s)
18980 struct glyph_string *s;
18981 {
18982 int i, k, x;
18983 struct glyph *glyphs = s->row->glyphs[s->area];
18984 int first = s->first_glyph - glyphs;
18985
18986 k = -1;
18987 x = 0;
18988 for (i = first - 1; i >= 0; --i)
18989 {
18990 int left, right;
18991 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18992 if (x + right > 0)
18993 k = i;
18994 x -= glyphs[i].pixel_width;
18995 }
18996
18997 return k;
18998 }
18999
19000
19001 /* Return the index of the last glyph following glyph string S that is
19002 not overwritten by S because of S's right overhang. Value is -1 if
19003 no such glyph is found. */
19004
19005 static int
19006 right_overwritten (s)
19007 struct glyph_string *s;
19008 {
19009 int k = -1;
19010
19011 if (s->right_overhang)
19012 {
19013 int x = 0, i;
19014 struct glyph *glyphs = s->row->glyphs[s->area];
19015 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19016 int end = s->row->used[s->area];
19017
19018 for (i = first; i < end && s->right_overhang > x; ++i)
19019 x += glyphs[i].pixel_width;
19020
19021 k = i;
19022 }
19023
19024 return k;
19025 }
19026
19027
19028 /* Return the index of the last glyph following glyph string S that
19029 overwrites S because of its left overhang. Value is negative
19030 if no such glyph is found. */
19031
19032 static int
19033 right_overwriting (s)
19034 struct glyph_string *s;
19035 {
19036 int i, k, x;
19037 int end = s->row->used[s->area];
19038 struct glyph *glyphs = s->row->glyphs[s->area];
19039 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19040
19041 k = -1;
19042 x = 0;
19043 for (i = first; i < end; ++i)
19044 {
19045 int left, right;
19046 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19047 if (x - left < 0)
19048 k = i;
19049 x += glyphs[i].pixel_width;
19050 }
19051
19052 return k;
19053 }
19054
19055
19056 /* Get face and two-byte form of character C in face FACE_ID on frame
19057 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19058 means we want to display multibyte text. DISPLAY_P non-zero means
19059 make sure that X resources for the face returned are allocated.
19060 Value is a pointer to a realized face that is ready for display if
19061 DISPLAY_P is non-zero. */
19062
19063 static INLINE struct face *
19064 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19065 struct frame *f;
19066 int c, face_id;
19067 XChar2b *char2b;
19068 int multibyte_p, display_p;
19069 {
19070 struct face *face = FACE_FROM_ID (f, face_id);
19071
19072 if (!multibyte_p)
19073 {
19074 /* Unibyte case. We don't have to encode, but we have to make
19075 sure to use a face suitable for unibyte. */
19076 STORE_XCHAR2B (char2b, 0, c);
19077 face_id = FACE_FOR_CHAR (f, face, c);
19078 face = FACE_FROM_ID (f, face_id);
19079 }
19080 else if (c < 128)
19081 {
19082 /* Case of ASCII in a face known to fit ASCII. */
19083 STORE_XCHAR2B (char2b, 0, c);
19084 }
19085 else
19086 {
19087 int c1, c2, charset;
19088
19089 /* Split characters into bytes. If c2 is -1 afterwards, C is
19090 really a one-byte character so that byte1 is zero. */
19091 SPLIT_CHAR (c, charset, c1, c2);
19092 if (c2 > 0)
19093 STORE_XCHAR2B (char2b, c1, c2);
19094 else
19095 STORE_XCHAR2B (char2b, 0, c1);
19096
19097 /* Maybe encode the character in *CHAR2B. */
19098 if (face->font != NULL)
19099 {
19100 struct font_info *font_info
19101 = FONT_INFO_FROM_ID (f, face->font_info_id);
19102 if (font_info)
19103 rif->encode_char (c, char2b, font_info, 0);
19104 }
19105 }
19106
19107 /* Make sure X resources of the face are allocated. */
19108 #ifdef HAVE_X_WINDOWS
19109 if (display_p)
19110 #endif
19111 {
19112 xassert (face != NULL);
19113 PREPARE_FACE_FOR_DISPLAY (f, face);
19114 }
19115
19116 return face;
19117 }
19118
19119
19120 /* Set background width of glyph string S. START is the index of the
19121 first glyph following S. LAST_X is the right-most x-position + 1
19122 in the drawing area. */
19123
19124 static INLINE void
19125 set_glyph_string_background_width (s, start, last_x)
19126 struct glyph_string *s;
19127 int start;
19128 int last_x;
19129 {
19130 /* If the face of this glyph string has to be drawn to the end of
19131 the drawing area, set S->extends_to_end_of_line_p. */
19132
19133 if (start == s->row->used[s->area]
19134 && s->area == TEXT_AREA
19135 && ((s->row->fill_line_p
19136 && (s->hl == DRAW_NORMAL_TEXT
19137 || s->hl == DRAW_IMAGE_RAISED
19138 || s->hl == DRAW_IMAGE_SUNKEN))
19139 || s->hl == DRAW_MOUSE_FACE))
19140 s->extends_to_end_of_line_p = 1;
19141
19142 /* If S extends its face to the end of the line, set its
19143 background_width to the distance to the right edge of the drawing
19144 area. */
19145 if (s->extends_to_end_of_line_p)
19146 s->background_width = last_x - s->x + 1;
19147 else
19148 s->background_width = s->width;
19149 }
19150
19151
19152 /* Compute overhangs and x-positions for glyph string S and its
19153 predecessors, or successors. X is the starting x-position for S.
19154 BACKWARD_P non-zero means process predecessors. */
19155
19156 static void
19157 compute_overhangs_and_x (s, x, backward_p)
19158 struct glyph_string *s;
19159 int x;
19160 int backward_p;
19161 {
19162 if (backward_p)
19163 {
19164 while (s)
19165 {
19166 if (rif->compute_glyph_string_overhangs)
19167 rif->compute_glyph_string_overhangs (s);
19168 x -= s->width;
19169 s->x = x;
19170 s = s->prev;
19171 }
19172 }
19173 else
19174 {
19175 while (s)
19176 {
19177 if (rif->compute_glyph_string_overhangs)
19178 rif->compute_glyph_string_overhangs (s);
19179 s->x = x;
19180 x += s->width;
19181 s = s->next;
19182 }
19183 }
19184 }
19185
19186
19187
19188 /* The following macros are only called from draw_glyphs below.
19189 They reference the following parameters of that function directly:
19190 `w', `row', `area', and `overlap_p'
19191 as well as the following local variables:
19192 `s', `f', and `hdc' (in W32) */
19193
19194 #ifdef HAVE_NTGUI
19195 /* On W32, silently add local `hdc' variable to argument list of
19196 init_glyph_string. */
19197 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19198 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19199 #else
19200 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19201 init_glyph_string (s, char2b, w, row, area, start, hl)
19202 #endif
19203
19204 /* Add a glyph string for a stretch glyph to the list of strings
19205 between HEAD and TAIL. START is the index of the stretch glyph in
19206 row area AREA of glyph row ROW. END is the index of the last glyph
19207 in that glyph row area. X is the current output position assigned
19208 to the new glyph string constructed. HL overrides that face of the
19209 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19210 is the right-most x-position of the drawing area. */
19211
19212 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19213 and below -- keep them on one line. */
19214 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19215 do \
19216 { \
19217 s = (struct glyph_string *) alloca (sizeof *s); \
19218 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19219 START = fill_stretch_glyph_string (s, row, area, START, END); \
19220 append_glyph_string (&HEAD, &TAIL, s); \
19221 s->x = (X); \
19222 } \
19223 while (0)
19224
19225
19226 /* Add a glyph string for an image glyph to the list of strings
19227 between HEAD and TAIL. START is the index of the image glyph in
19228 row area AREA of glyph row ROW. END is the index of the last glyph
19229 in that glyph row area. X is the current output position assigned
19230 to the new glyph string constructed. HL overrides that face of the
19231 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19232 is the right-most x-position of the drawing area. */
19233
19234 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19235 do \
19236 { \
19237 s = (struct glyph_string *) alloca (sizeof *s); \
19238 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19239 fill_image_glyph_string (s); \
19240 append_glyph_string (&HEAD, &TAIL, s); \
19241 ++START; \
19242 s->x = (X); \
19243 } \
19244 while (0)
19245
19246
19247 /* Add a glyph string for a sequence of character glyphs to the list
19248 of strings between HEAD and TAIL. START is the index of the first
19249 glyph in row area AREA of glyph row ROW that is part of the new
19250 glyph string. END is the index of the last glyph in that glyph row
19251 area. X is the current output position assigned to the new glyph
19252 string constructed. HL overrides that face of the glyph; e.g. it
19253 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19254 right-most x-position of the drawing area. */
19255
19256 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19257 do \
19258 { \
19259 int c, face_id; \
19260 XChar2b *char2b; \
19261 \
19262 c = (row)->glyphs[area][START].u.ch; \
19263 face_id = (row)->glyphs[area][START].face_id; \
19264 \
19265 s = (struct glyph_string *) alloca (sizeof *s); \
19266 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19267 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19268 append_glyph_string (&HEAD, &TAIL, s); \
19269 s->x = (X); \
19270 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19271 } \
19272 while (0)
19273
19274
19275 /* Add a glyph string for a composite sequence to the list of strings
19276 between HEAD and TAIL. START is the index of the first glyph in
19277 row area AREA of glyph row ROW that is part of the new glyph
19278 string. END is the index of the last glyph in that glyph row area.
19279 X is the current output position assigned to the new glyph string
19280 constructed. HL overrides that face of the glyph; e.g. it is
19281 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19282 x-position of the drawing area. */
19283
19284 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19285 do { \
19286 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19287 int face_id = (row)->glyphs[area][START].face_id; \
19288 struct face *base_face = FACE_FROM_ID (f, face_id); \
19289 struct composition *cmp = composition_table[cmp_id]; \
19290 int glyph_len = cmp->glyph_len; \
19291 XChar2b *char2b; \
19292 struct face **faces; \
19293 struct glyph_string *first_s = NULL; \
19294 int n; \
19295 \
19296 base_face = base_face->ascii_face; \
19297 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19298 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19299 /* At first, fill in `char2b' and `faces'. */ \
19300 for (n = 0; n < glyph_len; n++) \
19301 { \
19302 int c = COMPOSITION_GLYPH (cmp, n); \
19303 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19304 faces[n] = FACE_FROM_ID (f, this_face_id); \
19305 get_char_face_and_encoding (f, c, this_face_id, \
19306 char2b + n, 1, 1); \
19307 } \
19308 \
19309 /* Make glyph_strings for each glyph sequence that is drawable by \
19310 the same face, and append them to HEAD/TAIL. */ \
19311 for (n = 0; n < cmp->glyph_len;) \
19312 { \
19313 s = (struct glyph_string *) alloca (sizeof *s); \
19314 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19315 append_glyph_string (&(HEAD), &(TAIL), s); \
19316 s->cmp = cmp; \
19317 s->gidx = n; \
19318 s->x = (X); \
19319 \
19320 if (n == 0) \
19321 first_s = s; \
19322 \
19323 n = fill_composite_glyph_string (s, faces, overlaps); \
19324 } \
19325 \
19326 ++START; \
19327 s = first_s; \
19328 } while (0)
19329
19330
19331 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19332 of AREA of glyph row ROW on window W between indices START and END.
19333 HL overrides the face for drawing glyph strings, e.g. it is
19334 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19335 x-positions of the drawing area.
19336
19337 This is an ugly monster macro construct because we must use alloca
19338 to allocate glyph strings (because draw_glyphs can be called
19339 asynchronously). */
19340
19341 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19342 do \
19343 { \
19344 HEAD = TAIL = NULL; \
19345 while (START < END) \
19346 { \
19347 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19348 switch (first_glyph->type) \
19349 { \
19350 case CHAR_GLYPH: \
19351 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19352 HL, X, LAST_X); \
19353 break; \
19354 \
19355 case COMPOSITE_GLYPH: \
19356 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19357 HL, X, LAST_X); \
19358 break; \
19359 \
19360 case STRETCH_GLYPH: \
19361 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19362 HL, X, LAST_X); \
19363 break; \
19364 \
19365 case IMAGE_GLYPH: \
19366 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19367 HL, X, LAST_X); \
19368 break; \
19369 \
19370 default: \
19371 abort (); \
19372 } \
19373 \
19374 set_glyph_string_background_width (s, START, LAST_X); \
19375 (X) += s->width; \
19376 } \
19377 } \
19378 while (0)
19379
19380
19381 /* Draw glyphs between START and END in AREA of ROW on window W,
19382 starting at x-position X. X is relative to AREA in W. HL is a
19383 face-override with the following meaning:
19384
19385 DRAW_NORMAL_TEXT draw normally
19386 DRAW_CURSOR draw in cursor face
19387 DRAW_MOUSE_FACE draw in mouse face.
19388 DRAW_INVERSE_VIDEO draw in mode line face
19389 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19390 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19391
19392 If OVERLAPS is non-zero, draw only the foreground of characters and
19393 clip to the physical height of ROW. Non-zero value also defines
19394 the overlapping part to be drawn:
19395
19396 OVERLAPS_PRED overlap with preceding rows
19397 OVERLAPS_SUCC overlap with succeeding rows
19398 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19399 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19400
19401 Value is the x-position reached, relative to AREA of W. */
19402
19403 static int
19404 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19405 struct window *w;
19406 int x;
19407 struct glyph_row *row;
19408 enum glyph_row_area area;
19409 int start, end;
19410 enum draw_glyphs_face hl;
19411 int overlaps;
19412 {
19413 struct glyph_string *head, *tail;
19414 struct glyph_string *s;
19415 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19416 int last_x, area_width;
19417 int x_reached;
19418 int i, j;
19419 struct frame *f = XFRAME (WINDOW_FRAME (w));
19420 DECLARE_HDC (hdc);
19421
19422 ALLOCATE_HDC (hdc, f);
19423
19424 /* Let's rather be paranoid than getting a SEGV. */
19425 end = min (end, row->used[area]);
19426 start = max (0, start);
19427 start = min (end, start);
19428
19429 /* Translate X to frame coordinates. Set last_x to the right
19430 end of the drawing area. */
19431 if (row->full_width_p)
19432 {
19433 /* X is relative to the left edge of W, without scroll bars
19434 or fringes. */
19435 x += WINDOW_LEFT_EDGE_X (w);
19436 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19437 }
19438 else
19439 {
19440 int area_left = window_box_left (w, area);
19441 x += area_left;
19442 area_width = window_box_width (w, area);
19443 last_x = area_left + area_width;
19444 }
19445
19446 /* Build a doubly-linked list of glyph_string structures between
19447 head and tail from what we have to draw. Note that the macro
19448 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19449 the reason we use a separate variable `i'. */
19450 i = start;
19451 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19452 if (tail)
19453 x_reached = tail->x + tail->background_width;
19454 else
19455 x_reached = x;
19456
19457 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19458 the row, redraw some glyphs in front or following the glyph
19459 strings built above. */
19460 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19461 {
19462 int dummy_x = 0;
19463 struct glyph_string *h, *t;
19464
19465 /* Compute overhangs for all glyph strings. */
19466 if (rif->compute_glyph_string_overhangs)
19467 for (s = head; s; s = s->next)
19468 rif->compute_glyph_string_overhangs (s);
19469
19470 /* Prepend glyph strings for glyphs in front of the first glyph
19471 string that are overwritten because of the first glyph
19472 string's left overhang. The background of all strings
19473 prepended must be drawn because the first glyph string
19474 draws over it. */
19475 i = left_overwritten (head);
19476 if (i >= 0)
19477 {
19478 j = i;
19479 BUILD_GLYPH_STRINGS (j, start, h, t,
19480 DRAW_NORMAL_TEXT, dummy_x, last_x);
19481 start = i;
19482 compute_overhangs_and_x (t, head->x, 1);
19483 prepend_glyph_string_lists (&head, &tail, h, t);
19484 clip_head = head;
19485 }
19486
19487 /* Prepend glyph strings for glyphs in front of the first glyph
19488 string that overwrite that glyph string because of their
19489 right overhang. For these strings, only the foreground must
19490 be drawn, because it draws over the glyph string at `head'.
19491 The background must not be drawn because this would overwrite
19492 right overhangs of preceding glyphs for which no glyph
19493 strings exist. */
19494 i = left_overwriting (head);
19495 if (i >= 0)
19496 {
19497 clip_head = head;
19498 BUILD_GLYPH_STRINGS (i, start, h, t,
19499 DRAW_NORMAL_TEXT, dummy_x, last_x);
19500 for (s = h; s; s = s->next)
19501 s->background_filled_p = 1;
19502 compute_overhangs_and_x (t, head->x, 1);
19503 prepend_glyph_string_lists (&head, &tail, h, t);
19504 }
19505
19506 /* Append glyphs strings for glyphs following the last glyph
19507 string tail that are overwritten by tail. The background of
19508 these strings has to be drawn because tail's foreground draws
19509 over it. */
19510 i = right_overwritten (tail);
19511 if (i >= 0)
19512 {
19513 BUILD_GLYPH_STRINGS (end, i, h, t,
19514 DRAW_NORMAL_TEXT, x, last_x);
19515 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19516 append_glyph_string_lists (&head, &tail, h, t);
19517 clip_tail = tail;
19518 }
19519
19520 /* Append glyph strings for glyphs following the last glyph
19521 string tail that overwrite tail. The foreground of such
19522 glyphs has to be drawn because it writes into the background
19523 of tail. The background must not be drawn because it could
19524 paint over the foreground of following glyphs. */
19525 i = right_overwriting (tail);
19526 if (i >= 0)
19527 {
19528 clip_tail = tail;
19529 BUILD_GLYPH_STRINGS (end, i, h, t,
19530 DRAW_NORMAL_TEXT, x, last_x);
19531 for (s = h; s; s = s->next)
19532 s->background_filled_p = 1;
19533 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19534 append_glyph_string_lists (&head, &tail, h, t);
19535 }
19536 if (clip_head || clip_tail)
19537 for (s = head; s; s = s->next)
19538 {
19539 s->clip_head = clip_head;
19540 s->clip_tail = clip_tail;
19541 }
19542 }
19543
19544 /* Draw all strings. */
19545 for (s = head; s; s = s->next)
19546 rif->draw_glyph_string (s);
19547
19548 if (area == TEXT_AREA
19549 && !row->full_width_p
19550 /* When drawing overlapping rows, only the glyph strings'
19551 foreground is drawn, which doesn't erase a cursor
19552 completely. */
19553 && !overlaps)
19554 {
19555 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19556 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19557 : (tail ? tail->x + tail->background_width : x));
19558
19559 int text_left = window_box_left (w, TEXT_AREA);
19560 x0 -= text_left;
19561 x1 -= text_left;
19562
19563 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19564 row->y, MATRIX_ROW_BOTTOM_Y (row));
19565 }
19566
19567 /* Value is the x-position up to which drawn, relative to AREA of W.
19568 This doesn't include parts drawn because of overhangs. */
19569 if (row->full_width_p)
19570 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19571 else
19572 x_reached -= window_box_left (w, area);
19573
19574 RELEASE_HDC (hdc, f);
19575
19576 return x_reached;
19577 }
19578
19579 /* Expand row matrix if too narrow. Don't expand if area
19580 is not present. */
19581
19582 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19583 { \
19584 if (!fonts_changed_p \
19585 && (it->glyph_row->glyphs[area] \
19586 < it->glyph_row->glyphs[area + 1])) \
19587 { \
19588 it->w->ncols_scale_factor++; \
19589 fonts_changed_p = 1; \
19590 } \
19591 }
19592
19593 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19594 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19595
19596 static INLINE void
19597 append_glyph (it)
19598 struct it *it;
19599 {
19600 struct glyph *glyph;
19601 enum glyph_row_area area = it->area;
19602
19603 xassert (it->glyph_row);
19604 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19605
19606 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19607 if (glyph < it->glyph_row->glyphs[area + 1])
19608 {
19609 glyph->charpos = CHARPOS (it->position);
19610 glyph->object = it->object;
19611 glyph->pixel_width = it->pixel_width;
19612 glyph->ascent = it->ascent;
19613 glyph->descent = it->descent;
19614 glyph->voffset = it->voffset;
19615 glyph->type = CHAR_GLYPH;
19616 glyph->multibyte_p = it->multibyte_p;
19617 glyph->left_box_line_p = it->start_of_box_run_p;
19618 glyph->right_box_line_p = it->end_of_box_run_p;
19619 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19620 || it->phys_descent > it->descent);
19621 glyph->padding_p = 0;
19622 glyph->glyph_not_available_p = it->glyph_not_available_p;
19623 glyph->face_id = it->face_id;
19624 glyph->u.ch = it->char_to_display;
19625 glyph->slice = null_glyph_slice;
19626 glyph->font_type = FONT_TYPE_UNKNOWN;
19627 ++it->glyph_row->used[area];
19628 }
19629 else
19630 IT_EXPAND_MATRIX_WIDTH (it, area);
19631 }
19632
19633 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19634 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19635
19636 static INLINE void
19637 append_composite_glyph (it)
19638 struct it *it;
19639 {
19640 struct glyph *glyph;
19641 enum glyph_row_area area = it->area;
19642
19643 xassert (it->glyph_row);
19644
19645 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19646 if (glyph < it->glyph_row->glyphs[area + 1])
19647 {
19648 glyph->charpos = CHARPOS (it->position);
19649 glyph->object = it->object;
19650 glyph->pixel_width = it->pixel_width;
19651 glyph->ascent = it->ascent;
19652 glyph->descent = it->descent;
19653 glyph->voffset = it->voffset;
19654 glyph->type = COMPOSITE_GLYPH;
19655 glyph->multibyte_p = it->multibyte_p;
19656 glyph->left_box_line_p = it->start_of_box_run_p;
19657 glyph->right_box_line_p = it->end_of_box_run_p;
19658 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19659 || it->phys_descent > it->descent);
19660 glyph->padding_p = 0;
19661 glyph->glyph_not_available_p = 0;
19662 glyph->face_id = it->face_id;
19663 glyph->u.cmp_id = it->cmp_id;
19664 glyph->slice = null_glyph_slice;
19665 glyph->font_type = FONT_TYPE_UNKNOWN;
19666 ++it->glyph_row->used[area];
19667 }
19668 else
19669 IT_EXPAND_MATRIX_WIDTH (it, area);
19670 }
19671
19672
19673 /* Change IT->ascent and IT->height according to the setting of
19674 IT->voffset. */
19675
19676 static INLINE void
19677 take_vertical_position_into_account (it)
19678 struct it *it;
19679 {
19680 if (it->voffset)
19681 {
19682 if (it->voffset < 0)
19683 /* Increase the ascent so that we can display the text higher
19684 in the line. */
19685 it->ascent -= it->voffset;
19686 else
19687 /* Increase the descent so that we can display the text lower
19688 in the line. */
19689 it->descent += it->voffset;
19690 }
19691 }
19692
19693
19694 /* Produce glyphs/get display metrics for the image IT is loaded with.
19695 See the description of struct display_iterator in dispextern.h for
19696 an overview of struct display_iterator. */
19697
19698 static void
19699 produce_image_glyph (it)
19700 struct it *it;
19701 {
19702 struct image *img;
19703 struct face *face;
19704 int glyph_ascent;
19705 struct glyph_slice slice;
19706
19707 xassert (it->what == IT_IMAGE);
19708
19709 face = FACE_FROM_ID (it->f, it->face_id);
19710 xassert (face);
19711 /* Make sure X resources of the face is loaded. */
19712 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19713
19714 if (it->image_id < 0)
19715 {
19716 /* Fringe bitmap. */
19717 it->ascent = it->phys_ascent = 0;
19718 it->descent = it->phys_descent = 0;
19719 it->pixel_width = 0;
19720 it->nglyphs = 0;
19721 return;
19722 }
19723
19724 img = IMAGE_FROM_ID (it->f, it->image_id);
19725 xassert (img);
19726 /* Make sure X resources of the image is loaded. */
19727 prepare_image_for_display (it->f, img);
19728
19729 slice.x = slice.y = 0;
19730 slice.width = img->width;
19731 slice.height = img->height;
19732
19733 if (INTEGERP (it->slice.x))
19734 slice.x = XINT (it->slice.x);
19735 else if (FLOATP (it->slice.x))
19736 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19737
19738 if (INTEGERP (it->slice.y))
19739 slice.y = XINT (it->slice.y);
19740 else if (FLOATP (it->slice.y))
19741 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19742
19743 if (INTEGERP (it->slice.width))
19744 slice.width = XINT (it->slice.width);
19745 else if (FLOATP (it->slice.width))
19746 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19747
19748 if (INTEGERP (it->slice.height))
19749 slice.height = XINT (it->slice.height);
19750 else if (FLOATP (it->slice.height))
19751 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19752
19753 if (slice.x >= img->width)
19754 slice.x = img->width;
19755 if (slice.y >= img->height)
19756 slice.y = img->height;
19757 if (slice.x + slice.width >= img->width)
19758 slice.width = img->width - slice.x;
19759 if (slice.y + slice.height > img->height)
19760 slice.height = img->height - slice.y;
19761
19762 if (slice.width == 0 || slice.height == 0)
19763 return;
19764
19765 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19766
19767 it->descent = slice.height - glyph_ascent;
19768 if (slice.y == 0)
19769 it->descent += img->vmargin;
19770 if (slice.y + slice.height == img->height)
19771 it->descent += img->vmargin;
19772 it->phys_descent = it->descent;
19773
19774 it->pixel_width = slice.width;
19775 if (slice.x == 0)
19776 it->pixel_width += img->hmargin;
19777 if (slice.x + slice.width == img->width)
19778 it->pixel_width += img->hmargin;
19779
19780 /* It's quite possible for images to have an ascent greater than
19781 their height, so don't get confused in that case. */
19782 if (it->descent < 0)
19783 it->descent = 0;
19784
19785 #if 0 /* this breaks image tiling */
19786 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19787 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19788 if (face_ascent > it->ascent)
19789 it->ascent = it->phys_ascent = face_ascent;
19790 #endif
19791
19792 it->nglyphs = 1;
19793
19794 if (face->box != FACE_NO_BOX)
19795 {
19796 if (face->box_line_width > 0)
19797 {
19798 if (slice.y == 0)
19799 it->ascent += face->box_line_width;
19800 if (slice.y + slice.height == img->height)
19801 it->descent += face->box_line_width;
19802 }
19803
19804 if (it->start_of_box_run_p && slice.x == 0)
19805 it->pixel_width += abs (face->box_line_width);
19806 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19807 it->pixel_width += abs (face->box_line_width);
19808 }
19809
19810 take_vertical_position_into_account (it);
19811
19812 if (it->glyph_row)
19813 {
19814 struct glyph *glyph;
19815 enum glyph_row_area area = it->area;
19816
19817 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19818 if (glyph < it->glyph_row->glyphs[area + 1])
19819 {
19820 glyph->charpos = CHARPOS (it->position);
19821 glyph->object = it->object;
19822 glyph->pixel_width = it->pixel_width;
19823 glyph->ascent = glyph_ascent;
19824 glyph->descent = it->descent;
19825 glyph->voffset = it->voffset;
19826 glyph->type = IMAGE_GLYPH;
19827 glyph->multibyte_p = it->multibyte_p;
19828 glyph->left_box_line_p = it->start_of_box_run_p;
19829 glyph->right_box_line_p = it->end_of_box_run_p;
19830 glyph->overlaps_vertically_p = 0;
19831 glyph->padding_p = 0;
19832 glyph->glyph_not_available_p = 0;
19833 glyph->face_id = it->face_id;
19834 glyph->u.img_id = img->id;
19835 glyph->slice = slice;
19836 glyph->font_type = FONT_TYPE_UNKNOWN;
19837 ++it->glyph_row->used[area];
19838 }
19839 else
19840 IT_EXPAND_MATRIX_WIDTH (it, area);
19841 }
19842 }
19843
19844
19845 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19846 of the glyph, WIDTH and HEIGHT are the width and height of the
19847 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19848
19849 static void
19850 append_stretch_glyph (it, object, width, height, ascent)
19851 struct it *it;
19852 Lisp_Object object;
19853 int width, height;
19854 int ascent;
19855 {
19856 struct glyph *glyph;
19857 enum glyph_row_area area = it->area;
19858
19859 xassert (ascent >= 0 && ascent <= height);
19860
19861 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19862 if (glyph < it->glyph_row->glyphs[area + 1])
19863 {
19864 glyph->charpos = CHARPOS (it->position);
19865 glyph->object = object;
19866 glyph->pixel_width = width;
19867 glyph->ascent = ascent;
19868 glyph->descent = height - ascent;
19869 glyph->voffset = it->voffset;
19870 glyph->type = STRETCH_GLYPH;
19871 glyph->multibyte_p = it->multibyte_p;
19872 glyph->left_box_line_p = it->start_of_box_run_p;
19873 glyph->right_box_line_p = it->end_of_box_run_p;
19874 glyph->overlaps_vertically_p = 0;
19875 glyph->padding_p = 0;
19876 glyph->glyph_not_available_p = 0;
19877 glyph->face_id = it->face_id;
19878 glyph->u.stretch.ascent = ascent;
19879 glyph->u.stretch.height = height;
19880 glyph->slice = null_glyph_slice;
19881 glyph->font_type = FONT_TYPE_UNKNOWN;
19882 ++it->glyph_row->used[area];
19883 }
19884 else
19885 IT_EXPAND_MATRIX_WIDTH (it, area);
19886 }
19887
19888
19889 /* Produce a stretch glyph for iterator IT. IT->object is the value
19890 of the glyph property displayed. The value must be a list
19891 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19892 being recognized:
19893
19894 1. `:width WIDTH' specifies that the space should be WIDTH *
19895 canonical char width wide. WIDTH may be an integer or floating
19896 point number.
19897
19898 2. `:relative-width FACTOR' specifies that the width of the stretch
19899 should be computed from the width of the first character having the
19900 `glyph' property, and should be FACTOR times that width.
19901
19902 3. `:align-to HPOS' specifies that the space should be wide enough
19903 to reach HPOS, a value in canonical character units.
19904
19905 Exactly one of the above pairs must be present.
19906
19907 4. `:height HEIGHT' specifies that the height of the stretch produced
19908 should be HEIGHT, measured in canonical character units.
19909
19910 5. `:relative-height FACTOR' specifies that the height of the
19911 stretch should be FACTOR times the height of the characters having
19912 the glyph property.
19913
19914 Either none or exactly one of 4 or 5 must be present.
19915
19916 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19917 of the stretch should be used for the ascent of the stretch.
19918 ASCENT must be in the range 0 <= ASCENT <= 100. */
19919
19920 static void
19921 produce_stretch_glyph (it)
19922 struct it *it;
19923 {
19924 /* (space :width WIDTH :height HEIGHT ...) */
19925 Lisp_Object prop, plist;
19926 int width = 0, height = 0, align_to = -1;
19927 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19928 int ascent = 0;
19929 double tem;
19930 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19931 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19932
19933 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19934
19935 /* List should start with `space'. */
19936 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19937 plist = XCDR (it->object);
19938
19939 /* Compute the width of the stretch. */
19940 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19941 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19942 {
19943 /* Absolute width `:width WIDTH' specified and valid. */
19944 zero_width_ok_p = 1;
19945 width = (int)tem;
19946 }
19947 else if (prop = Fplist_get (plist, QCrelative_width),
19948 NUMVAL (prop) > 0)
19949 {
19950 /* Relative width `:relative-width FACTOR' specified and valid.
19951 Compute the width of the characters having the `glyph'
19952 property. */
19953 struct it it2;
19954 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19955
19956 it2 = *it;
19957 if (it->multibyte_p)
19958 {
19959 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19960 - IT_BYTEPOS (*it));
19961 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19962 }
19963 else
19964 it2.c = *p, it2.len = 1;
19965
19966 it2.glyph_row = NULL;
19967 it2.what = IT_CHARACTER;
19968 x_produce_glyphs (&it2);
19969 width = NUMVAL (prop) * it2.pixel_width;
19970 }
19971 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19972 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19973 {
19974 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19975 align_to = (align_to < 0
19976 ? 0
19977 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19978 else if (align_to < 0)
19979 align_to = window_box_left_offset (it->w, TEXT_AREA);
19980 width = max (0, (int)tem + align_to - it->current_x);
19981 zero_width_ok_p = 1;
19982 }
19983 else
19984 /* Nothing specified -> width defaults to canonical char width. */
19985 width = FRAME_COLUMN_WIDTH (it->f);
19986
19987 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19988 width = 1;
19989
19990 /* Compute height. */
19991 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19992 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19993 {
19994 height = (int)tem;
19995 zero_height_ok_p = 1;
19996 }
19997 else if (prop = Fplist_get (plist, QCrelative_height),
19998 NUMVAL (prop) > 0)
19999 height = FONT_HEIGHT (font) * NUMVAL (prop);
20000 else
20001 height = FONT_HEIGHT (font);
20002
20003 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20004 height = 1;
20005
20006 /* Compute percentage of height used for ascent. If
20007 `:ascent ASCENT' is present and valid, use that. Otherwise,
20008 derive the ascent from the font in use. */
20009 if (prop = Fplist_get (plist, QCascent),
20010 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20011 ascent = height * NUMVAL (prop) / 100.0;
20012 else if (!NILP (prop)
20013 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20014 ascent = min (max (0, (int)tem), height);
20015 else
20016 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20017
20018 if (width > 0 && !it->truncate_lines_p
20019 && it->current_x + width > it->last_visible_x)
20020 width = it->last_visible_x - it->current_x - 1;
20021
20022 if (width > 0 && height > 0 && it->glyph_row)
20023 {
20024 Lisp_Object object = it->stack[it->sp - 1].string;
20025 if (!STRINGP (object))
20026 object = it->w->buffer;
20027 append_stretch_glyph (it, object, width, height, ascent);
20028 }
20029
20030 it->pixel_width = width;
20031 it->ascent = it->phys_ascent = ascent;
20032 it->descent = it->phys_descent = height - it->ascent;
20033 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20034
20035 take_vertical_position_into_account (it);
20036 }
20037
20038 /* Get line-height and line-spacing property at point.
20039 If line-height has format (HEIGHT TOTAL), return TOTAL
20040 in TOTAL_HEIGHT. */
20041
20042 static Lisp_Object
20043 get_line_height_property (it, prop)
20044 struct it *it;
20045 Lisp_Object prop;
20046 {
20047 Lisp_Object position;
20048
20049 if (STRINGP (it->object))
20050 position = make_number (IT_STRING_CHARPOS (*it));
20051 else if (BUFFERP (it->object))
20052 position = make_number (IT_CHARPOS (*it));
20053 else
20054 return Qnil;
20055
20056 return Fget_char_property (position, prop, it->object);
20057 }
20058
20059 /* Calculate line-height and line-spacing properties.
20060 An integer value specifies explicit pixel value.
20061 A float value specifies relative value to current face height.
20062 A cons (float . face-name) specifies relative value to
20063 height of specified face font.
20064
20065 Returns height in pixels, or nil. */
20066
20067
20068 static Lisp_Object
20069 calc_line_height_property (it, val, font, boff, override)
20070 struct it *it;
20071 Lisp_Object val;
20072 XFontStruct *font;
20073 int boff, override;
20074 {
20075 Lisp_Object face_name = Qnil;
20076 int ascent, descent, height;
20077
20078 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20079 return val;
20080
20081 if (CONSP (val))
20082 {
20083 face_name = XCAR (val);
20084 val = XCDR (val);
20085 if (!NUMBERP (val))
20086 val = make_number (1);
20087 if (NILP (face_name))
20088 {
20089 height = it->ascent + it->descent;
20090 goto scale;
20091 }
20092 }
20093
20094 if (NILP (face_name))
20095 {
20096 font = FRAME_FONT (it->f);
20097 boff = FRAME_BASELINE_OFFSET (it->f);
20098 }
20099 else if (EQ (face_name, Qt))
20100 {
20101 override = 0;
20102 }
20103 else
20104 {
20105 int face_id;
20106 struct face *face;
20107 struct font_info *font_info;
20108
20109 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20110 if (face_id < 0)
20111 return make_number (-1);
20112
20113 face = FACE_FROM_ID (it->f, face_id);
20114 font = face->font;
20115 if (font == NULL)
20116 return make_number (-1);
20117
20118 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20119 boff = font_info->baseline_offset;
20120 if (font_info->vertical_centering)
20121 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20122 }
20123
20124 ascent = FONT_BASE (font) + boff;
20125 descent = FONT_DESCENT (font) - boff;
20126
20127 if (override)
20128 {
20129 it->override_ascent = ascent;
20130 it->override_descent = descent;
20131 it->override_boff = boff;
20132 }
20133
20134 height = ascent + descent;
20135
20136 scale:
20137 if (FLOATP (val))
20138 height = (int)(XFLOAT_DATA (val) * height);
20139 else if (INTEGERP (val))
20140 height *= XINT (val);
20141
20142 return make_number (height);
20143 }
20144
20145
20146 /* RIF:
20147 Produce glyphs/get display metrics for the display element IT is
20148 loaded with. See the description of struct it in dispextern.h
20149 for an overview of struct it. */
20150
20151 void
20152 x_produce_glyphs (it)
20153 struct it *it;
20154 {
20155 int extra_line_spacing = it->extra_line_spacing;
20156
20157 it->glyph_not_available_p = 0;
20158
20159 if (it->what == IT_CHARACTER)
20160 {
20161 XChar2b char2b;
20162 XFontStruct *font;
20163 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20164 XCharStruct *pcm;
20165 int font_not_found_p;
20166 struct font_info *font_info;
20167 int boff; /* baseline offset */
20168 /* We may change it->multibyte_p upon unibyte<->multibyte
20169 conversion. So, save the current value now and restore it
20170 later.
20171
20172 Note: It seems that we don't have to record multibyte_p in
20173 struct glyph because the character code itself tells if or
20174 not the character is multibyte. Thus, in the future, we must
20175 consider eliminating the field `multibyte_p' in the struct
20176 glyph. */
20177 int saved_multibyte_p = it->multibyte_p;
20178
20179 /* Maybe translate single-byte characters to multibyte, or the
20180 other way. */
20181 it->char_to_display = it->c;
20182 if (!ASCII_BYTE_P (it->c))
20183 {
20184 if (unibyte_display_via_language_environment
20185 && SINGLE_BYTE_CHAR_P (it->c)
20186 && (it->c >= 0240
20187 || !NILP (Vnonascii_translation_table)))
20188 {
20189 it->char_to_display = unibyte_char_to_multibyte (it->c);
20190 it->multibyte_p = 1;
20191 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20192 face = FACE_FROM_ID (it->f, it->face_id);
20193 }
20194 else if (!SINGLE_BYTE_CHAR_P (it->c)
20195 && !it->multibyte_p)
20196 {
20197 it->multibyte_p = 1;
20198 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20199 face = FACE_FROM_ID (it->f, it->face_id);
20200 }
20201 }
20202
20203 /* Get font to use. Encode IT->char_to_display. */
20204 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20205 &char2b, it->multibyte_p, 0);
20206 font = face->font;
20207
20208 /* When no suitable font found, use the default font. */
20209 font_not_found_p = font == NULL;
20210 if (font_not_found_p)
20211 {
20212 font = FRAME_FONT (it->f);
20213 boff = FRAME_BASELINE_OFFSET (it->f);
20214 font_info = NULL;
20215 }
20216 else
20217 {
20218 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20219 boff = font_info->baseline_offset;
20220 if (font_info->vertical_centering)
20221 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20222 }
20223
20224 if (it->char_to_display >= ' '
20225 && (!it->multibyte_p || it->char_to_display < 128))
20226 {
20227 /* Either unibyte or ASCII. */
20228 int stretched_p;
20229
20230 it->nglyphs = 1;
20231
20232 pcm = rif->per_char_metric (font, &char2b,
20233 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20234
20235 if (it->override_ascent >= 0)
20236 {
20237 it->ascent = it->override_ascent;
20238 it->descent = it->override_descent;
20239 boff = it->override_boff;
20240 }
20241 else
20242 {
20243 it->ascent = FONT_BASE (font) + boff;
20244 it->descent = FONT_DESCENT (font) - boff;
20245 }
20246
20247 if (pcm)
20248 {
20249 it->phys_ascent = pcm->ascent + boff;
20250 it->phys_descent = pcm->descent - boff;
20251 it->pixel_width = pcm->width;
20252 }
20253 else
20254 {
20255 it->glyph_not_available_p = 1;
20256 it->phys_ascent = it->ascent;
20257 it->phys_descent = it->descent;
20258 it->pixel_width = FONT_WIDTH (font);
20259 }
20260
20261 if (it->constrain_row_ascent_descent_p)
20262 {
20263 if (it->descent > it->max_descent)
20264 {
20265 it->ascent += it->descent - it->max_descent;
20266 it->descent = it->max_descent;
20267 }
20268 if (it->ascent > it->max_ascent)
20269 {
20270 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20271 it->ascent = it->max_ascent;
20272 }
20273 it->phys_ascent = min (it->phys_ascent, it->ascent);
20274 it->phys_descent = min (it->phys_descent, it->descent);
20275 extra_line_spacing = 0;
20276 }
20277
20278 /* If this is a space inside a region of text with
20279 `space-width' property, change its width. */
20280 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20281 if (stretched_p)
20282 it->pixel_width *= XFLOATINT (it->space_width);
20283
20284 /* If face has a box, add the box thickness to the character
20285 height. If character has a box line to the left and/or
20286 right, add the box line width to the character's width. */
20287 if (face->box != FACE_NO_BOX)
20288 {
20289 int thick = face->box_line_width;
20290
20291 if (thick > 0)
20292 {
20293 it->ascent += thick;
20294 it->descent += thick;
20295 }
20296 else
20297 thick = -thick;
20298
20299 if (it->start_of_box_run_p)
20300 it->pixel_width += thick;
20301 if (it->end_of_box_run_p)
20302 it->pixel_width += thick;
20303 }
20304
20305 /* If face has an overline, add the height of the overline
20306 (1 pixel) and a 1 pixel margin to the character height. */
20307 if (face->overline_p)
20308 it->ascent += 2;
20309
20310 if (it->constrain_row_ascent_descent_p)
20311 {
20312 if (it->ascent > it->max_ascent)
20313 it->ascent = it->max_ascent;
20314 if (it->descent > it->max_descent)
20315 it->descent = it->max_descent;
20316 }
20317
20318 take_vertical_position_into_account (it);
20319
20320 /* If we have to actually produce glyphs, do it. */
20321 if (it->glyph_row)
20322 {
20323 if (stretched_p)
20324 {
20325 /* Translate a space with a `space-width' property
20326 into a stretch glyph. */
20327 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20328 / FONT_HEIGHT (font));
20329 append_stretch_glyph (it, it->object, it->pixel_width,
20330 it->ascent + it->descent, ascent);
20331 }
20332 else
20333 append_glyph (it);
20334
20335 /* If characters with lbearing or rbearing are displayed
20336 in this line, record that fact in a flag of the
20337 glyph row. This is used to optimize X output code. */
20338 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20339 it->glyph_row->contains_overlapping_glyphs_p = 1;
20340 }
20341 }
20342 else if (it->char_to_display == '\n')
20343 {
20344 /* A newline has no width but we need the height of the line.
20345 But if previous part of the line set a height, don't
20346 increase that height */
20347
20348 Lisp_Object height;
20349 Lisp_Object total_height = Qnil;
20350
20351 it->override_ascent = -1;
20352 it->pixel_width = 0;
20353 it->nglyphs = 0;
20354
20355 height = get_line_height_property(it, Qline_height);
20356 /* Split (line-height total-height) list */
20357 if (CONSP (height)
20358 && CONSP (XCDR (height))
20359 && NILP (XCDR (XCDR (height))))
20360 {
20361 total_height = XCAR (XCDR (height));
20362 height = XCAR (height);
20363 }
20364 height = calc_line_height_property(it, height, font, boff, 1);
20365
20366 if (it->override_ascent >= 0)
20367 {
20368 it->ascent = it->override_ascent;
20369 it->descent = it->override_descent;
20370 boff = it->override_boff;
20371 }
20372 else
20373 {
20374 it->ascent = FONT_BASE (font) + boff;
20375 it->descent = FONT_DESCENT (font) - boff;
20376 }
20377
20378 if (EQ (height, Qt))
20379 {
20380 if (it->descent > it->max_descent)
20381 {
20382 it->ascent += it->descent - it->max_descent;
20383 it->descent = it->max_descent;
20384 }
20385 if (it->ascent > it->max_ascent)
20386 {
20387 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20388 it->ascent = it->max_ascent;
20389 }
20390 it->phys_ascent = min (it->phys_ascent, it->ascent);
20391 it->phys_descent = min (it->phys_descent, it->descent);
20392 it->constrain_row_ascent_descent_p = 1;
20393 extra_line_spacing = 0;
20394 }
20395 else
20396 {
20397 Lisp_Object spacing;
20398
20399 it->phys_ascent = it->ascent;
20400 it->phys_descent = it->descent;
20401
20402 if ((it->max_ascent > 0 || it->max_descent > 0)
20403 && face->box != FACE_NO_BOX
20404 && face->box_line_width > 0)
20405 {
20406 it->ascent += face->box_line_width;
20407 it->descent += face->box_line_width;
20408 }
20409 if (!NILP (height)
20410 && XINT (height) > it->ascent + it->descent)
20411 it->ascent = XINT (height) - it->descent;
20412
20413 if (!NILP (total_height))
20414 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20415 else
20416 {
20417 spacing = get_line_height_property(it, Qline_spacing);
20418 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20419 }
20420 if (INTEGERP (spacing))
20421 {
20422 extra_line_spacing = XINT (spacing);
20423 if (!NILP (total_height))
20424 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20425 }
20426 }
20427 }
20428 else if (it->char_to_display == '\t')
20429 {
20430 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20431 int x = it->current_x + it->continuation_lines_width;
20432 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20433
20434 /* If the distance from the current position to the next tab
20435 stop is less than a space character width, use the
20436 tab stop after that. */
20437 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20438 next_tab_x += tab_width;
20439
20440 it->pixel_width = next_tab_x - x;
20441 it->nglyphs = 1;
20442 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20443 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20444
20445 if (it->glyph_row)
20446 {
20447 append_stretch_glyph (it, it->object, it->pixel_width,
20448 it->ascent + it->descent, it->ascent);
20449 }
20450 }
20451 else
20452 {
20453 /* A multi-byte character. Assume that the display width of the
20454 character is the width of the character multiplied by the
20455 width of the font. */
20456
20457 /* If we found a font, this font should give us the right
20458 metrics. If we didn't find a font, use the frame's
20459 default font and calculate the width of the character
20460 from the charset width; this is what old redisplay code
20461 did. */
20462
20463 pcm = rif->per_char_metric (font, &char2b,
20464 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20465
20466 if (font_not_found_p || !pcm)
20467 {
20468 int charset = CHAR_CHARSET (it->char_to_display);
20469
20470 it->glyph_not_available_p = 1;
20471 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20472 * CHARSET_WIDTH (charset));
20473 it->phys_ascent = FONT_BASE (font) + boff;
20474 it->phys_descent = FONT_DESCENT (font) - boff;
20475 }
20476 else
20477 {
20478 it->pixel_width = pcm->width;
20479 it->phys_ascent = pcm->ascent + boff;
20480 it->phys_descent = pcm->descent - boff;
20481 if (it->glyph_row
20482 && (pcm->lbearing < 0
20483 || pcm->rbearing > pcm->width))
20484 it->glyph_row->contains_overlapping_glyphs_p = 1;
20485 }
20486 it->nglyphs = 1;
20487 it->ascent = FONT_BASE (font) + boff;
20488 it->descent = FONT_DESCENT (font) - boff;
20489 if (face->box != FACE_NO_BOX)
20490 {
20491 int thick = face->box_line_width;
20492
20493 if (thick > 0)
20494 {
20495 it->ascent += thick;
20496 it->descent += thick;
20497 }
20498 else
20499 thick = - thick;
20500
20501 if (it->start_of_box_run_p)
20502 it->pixel_width += thick;
20503 if (it->end_of_box_run_p)
20504 it->pixel_width += thick;
20505 }
20506
20507 /* If face has an overline, add the height of the overline
20508 (1 pixel) and a 1 pixel margin to the character height. */
20509 if (face->overline_p)
20510 it->ascent += 2;
20511
20512 take_vertical_position_into_account (it);
20513
20514 if (it->glyph_row)
20515 append_glyph (it);
20516 }
20517 it->multibyte_p = saved_multibyte_p;
20518 }
20519 else if (it->what == IT_COMPOSITION)
20520 {
20521 /* Note: A composition is represented as one glyph in the
20522 glyph matrix. There are no padding glyphs. */
20523 XChar2b char2b;
20524 XFontStruct *font;
20525 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20526 XCharStruct *pcm;
20527 int font_not_found_p;
20528 struct font_info *font_info;
20529 int boff; /* baseline offset */
20530 struct composition *cmp = composition_table[it->cmp_id];
20531
20532 /* Maybe translate single-byte characters to multibyte. */
20533 it->char_to_display = it->c;
20534 if (unibyte_display_via_language_environment
20535 && SINGLE_BYTE_CHAR_P (it->c)
20536 && (it->c >= 0240
20537 || (it->c >= 0200
20538 && !NILP (Vnonascii_translation_table))))
20539 {
20540 it->char_to_display = unibyte_char_to_multibyte (it->c);
20541 }
20542
20543 /* Get face and font to use. Encode IT->char_to_display. */
20544 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20545 face = FACE_FROM_ID (it->f, it->face_id);
20546 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20547 &char2b, it->multibyte_p, 0);
20548 font = face->font;
20549
20550 /* When no suitable font found, use the default font. */
20551 font_not_found_p = font == NULL;
20552 if (font_not_found_p)
20553 {
20554 font = FRAME_FONT (it->f);
20555 boff = FRAME_BASELINE_OFFSET (it->f);
20556 font_info = NULL;
20557 }
20558 else
20559 {
20560 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20561 boff = font_info->baseline_offset;
20562 if (font_info->vertical_centering)
20563 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20564 }
20565
20566 /* There are no padding glyphs, so there is only one glyph to
20567 produce for the composition. Important is that pixel_width,
20568 ascent and descent are the values of what is drawn by
20569 draw_glyphs (i.e. the values of the overall glyphs composed). */
20570 it->nglyphs = 1;
20571
20572 /* If we have not yet calculated pixel size data of glyphs of
20573 the composition for the current face font, calculate them
20574 now. Theoretically, we have to check all fonts for the
20575 glyphs, but that requires much time and memory space. So,
20576 here we check only the font of the first glyph. This leads
20577 to incorrect display very rarely, and C-l (recenter) can
20578 correct the display anyway. */
20579 if (cmp->font != (void *) font)
20580 {
20581 /* Ascent and descent of the font of the first character of
20582 this composition (adjusted by baseline offset). Ascent
20583 and descent of overall glyphs should not be less than
20584 them respectively. */
20585 int font_ascent = FONT_BASE (font) + boff;
20586 int font_descent = FONT_DESCENT (font) - boff;
20587 /* Bounding box of the overall glyphs. */
20588 int leftmost, rightmost, lowest, highest;
20589 int i, width, ascent, descent;
20590
20591 cmp->font = (void *) font;
20592
20593 /* Initialize the bounding box. */
20594 if (font_info
20595 && (pcm = rif->per_char_metric (font, &char2b,
20596 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20597 {
20598 width = pcm->width;
20599 ascent = pcm->ascent;
20600 descent = pcm->descent;
20601 }
20602 else
20603 {
20604 width = FONT_WIDTH (font);
20605 ascent = FONT_BASE (font);
20606 descent = FONT_DESCENT (font);
20607 }
20608
20609 rightmost = width;
20610 lowest = - descent + boff;
20611 highest = ascent + boff;
20612 leftmost = 0;
20613
20614 if (font_info
20615 && font_info->default_ascent
20616 && CHAR_TABLE_P (Vuse_default_ascent)
20617 && !NILP (Faref (Vuse_default_ascent,
20618 make_number (it->char_to_display))))
20619 highest = font_info->default_ascent + boff;
20620
20621 /* Draw the first glyph at the normal position. It may be
20622 shifted to right later if some other glyphs are drawn at
20623 the left. */
20624 cmp->offsets[0] = 0;
20625 cmp->offsets[1] = boff;
20626
20627 /* Set cmp->offsets for the remaining glyphs. */
20628 for (i = 1; i < cmp->glyph_len; i++)
20629 {
20630 int left, right, btm, top;
20631 int ch = COMPOSITION_GLYPH (cmp, i);
20632 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20633
20634 face = FACE_FROM_ID (it->f, face_id);
20635 get_char_face_and_encoding (it->f, ch, face->id,
20636 &char2b, it->multibyte_p, 0);
20637 font = face->font;
20638 if (font == NULL)
20639 {
20640 font = FRAME_FONT (it->f);
20641 boff = FRAME_BASELINE_OFFSET (it->f);
20642 font_info = NULL;
20643 }
20644 else
20645 {
20646 font_info
20647 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20648 boff = font_info->baseline_offset;
20649 if (font_info->vertical_centering)
20650 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20651 }
20652
20653 if (font_info
20654 && (pcm = rif->per_char_metric (font, &char2b,
20655 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20656 {
20657 width = pcm->width;
20658 ascent = pcm->ascent;
20659 descent = pcm->descent;
20660 }
20661 else
20662 {
20663 width = FONT_WIDTH (font);
20664 ascent = 1;
20665 descent = 0;
20666 }
20667
20668 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20669 {
20670 /* Relative composition with or without
20671 alternate chars. */
20672 left = (leftmost + rightmost - width) / 2;
20673 btm = - descent + boff;
20674 if (font_info && font_info->relative_compose
20675 && (! CHAR_TABLE_P (Vignore_relative_composition)
20676 || NILP (Faref (Vignore_relative_composition,
20677 make_number (ch)))))
20678 {
20679
20680 if (- descent >= font_info->relative_compose)
20681 /* One extra pixel between two glyphs. */
20682 btm = highest + 1;
20683 else if (ascent <= 0)
20684 /* One extra pixel between two glyphs. */
20685 btm = lowest - 1 - ascent - descent;
20686 }
20687 }
20688 else
20689 {
20690 /* A composition rule is specified by an integer
20691 value that encodes global and new reference
20692 points (GREF and NREF). GREF and NREF are
20693 specified by numbers as below:
20694
20695 0---1---2 -- ascent
20696 | |
20697 | |
20698 | |
20699 9--10--11 -- center
20700 | |
20701 ---3---4---5--- baseline
20702 | |
20703 6---7---8 -- descent
20704 */
20705 int rule = COMPOSITION_RULE (cmp, i);
20706 int gref, nref, grefx, grefy, nrefx, nrefy;
20707
20708 COMPOSITION_DECODE_RULE (rule, gref, nref);
20709 grefx = gref % 3, nrefx = nref % 3;
20710 grefy = gref / 3, nrefy = nref / 3;
20711
20712 left = (leftmost
20713 + grefx * (rightmost - leftmost) / 2
20714 - nrefx * width / 2);
20715 btm = ((grefy == 0 ? highest
20716 : grefy == 1 ? 0
20717 : grefy == 2 ? lowest
20718 : (highest + lowest) / 2)
20719 - (nrefy == 0 ? ascent + descent
20720 : nrefy == 1 ? descent - boff
20721 : nrefy == 2 ? 0
20722 : (ascent + descent) / 2));
20723 }
20724
20725 cmp->offsets[i * 2] = left;
20726 cmp->offsets[i * 2 + 1] = btm + descent;
20727
20728 /* Update the bounding box of the overall glyphs. */
20729 right = left + width;
20730 top = btm + descent + ascent;
20731 if (left < leftmost)
20732 leftmost = left;
20733 if (right > rightmost)
20734 rightmost = right;
20735 if (top > highest)
20736 highest = top;
20737 if (btm < lowest)
20738 lowest = btm;
20739 }
20740
20741 /* If there are glyphs whose x-offsets are negative,
20742 shift all glyphs to the right and make all x-offsets
20743 non-negative. */
20744 if (leftmost < 0)
20745 {
20746 for (i = 0; i < cmp->glyph_len; i++)
20747 cmp->offsets[i * 2] -= leftmost;
20748 rightmost -= leftmost;
20749 }
20750
20751 cmp->pixel_width = rightmost;
20752 cmp->ascent = highest;
20753 cmp->descent = - lowest;
20754 if (cmp->ascent < font_ascent)
20755 cmp->ascent = font_ascent;
20756 if (cmp->descent < font_descent)
20757 cmp->descent = font_descent;
20758 }
20759
20760 it->pixel_width = cmp->pixel_width;
20761 it->ascent = it->phys_ascent = cmp->ascent;
20762 it->descent = it->phys_descent = cmp->descent;
20763
20764 if (face->box != FACE_NO_BOX)
20765 {
20766 int thick = face->box_line_width;
20767
20768 if (thick > 0)
20769 {
20770 it->ascent += thick;
20771 it->descent += thick;
20772 }
20773 else
20774 thick = - thick;
20775
20776 if (it->start_of_box_run_p)
20777 it->pixel_width += thick;
20778 if (it->end_of_box_run_p)
20779 it->pixel_width += thick;
20780 }
20781
20782 /* If face has an overline, add the height of the overline
20783 (1 pixel) and a 1 pixel margin to the character height. */
20784 if (face->overline_p)
20785 it->ascent += 2;
20786
20787 take_vertical_position_into_account (it);
20788
20789 if (it->glyph_row)
20790 append_composite_glyph (it);
20791 }
20792 else if (it->what == IT_IMAGE)
20793 produce_image_glyph (it);
20794 else if (it->what == IT_STRETCH)
20795 produce_stretch_glyph (it);
20796
20797 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20798 because this isn't true for images with `:ascent 100'. */
20799 xassert (it->ascent >= 0 && it->descent >= 0);
20800 if (it->area == TEXT_AREA)
20801 it->current_x += it->pixel_width;
20802
20803 if (extra_line_spacing > 0)
20804 {
20805 it->descent += extra_line_spacing;
20806 if (extra_line_spacing > it->max_extra_line_spacing)
20807 it->max_extra_line_spacing = extra_line_spacing;
20808 }
20809
20810 it->max_ascent = max (it->max_ascent, it->ascent);
20811 it->max_descent = max (it->max_descent, it->descent);
20812 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20813 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20814 }
20815
20816 /* EXPORT for RIF:
20817 Output LEN glyphs starting at START at the nominal cursor position.
20818 Advance the nominal cursor over the text. The global variable
20819 updated_window contains the window being updated, updated_row is
20820 the glyph row being updated, and updated_area is the area of that
20821 row being updated. */
20822
20823 void
20824 x_write_glyphs (start, len)
20825 struct glyph *start;
20826 int len;
20827 {
20828 int x, hpos;
20829
20830 xassert (updated_window && updated_row);
20831 BLOCK_INPUT;
20832
20833 /* Write glyphs. */
20834
20835 hpos = start - updated_row->glyphs[updated_area];
20836 x = draw_glyphs (updated_window, output_cursor.x,
20837 updated_row, updated_area,
20838 hpos, hpos + len,
20839 DRAW_NORMAL_TEXT, 0);
20840
20841 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20842 if (updated_area == TEXT_AREA
20843 && updated_window->phys_cursor_on_p
20844 && updated_window->phys_cursor.vpos == output_cursor.vpos
20845 && updated_window->phys_cursor.hpos >= hpos
20846 && updated_window->phys_cursor.hpos < hpos + len)
20847 updated_window->phys_cursor_on_p = 0;
20848
20849 UNBLOCK_INPUT;
20850
20851 /* Advance the output cursor. */
20852 output_cursor.hpos += len;
20853 output_cursor.x = x;
20854 }
20855
20856
20857 /* EXPORT for RIF:
20858 Insert LEN glyphs from START at the nominal cursor position. */
20859
20860 void
20861 x_insert_glyphs (start, len)
20862 struct glyph *start;
20863 int len;
20864 {
20865 struct frame *f;
20866 struct window *w;
20867 int line_height, shift_by_width, shifted_region_width;
20868 struct glyph_row *row;
20869 struct glyph *glyph;
20870 int frame_x, frame_y, hpos;
20871
20872 xassert (updated_window && updated_row);
20873 BLOCK_INPUT;
20874 w = updated_window;
20875 f = XFRAME (WINDOW_FRAME (w));
20876
20877 /* Get the height of the line we are in. */
20878 row = updated_row;
20879 line_height = row->height;
20880
20881 /* Get the width of the glyphs to insert. */
20882 shift_by_width = 0;
20883 for (glyph = start; glyph < start + len; ++glyph)
20884 shift_by_width += glyph->pixel_width;
20885
20886 /* Get the width of the region to shift right. */
20887 shifted_region_width = (window_box_width (w, updated_area)
20888 - output_cursor.x
20889 - shift_by_width);
20890
20891 /* Shift right. */
20892 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20893 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20894
20895 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20896 line_height, shift_by_width);
20897
20898 /* Write the glyphs. */
20899 hpos = start - row->glyphs[updated_area];
20900 draw_glyphs (w, output_cursor.x, row, updated_area,
20901 hpos, hpos + len,
20902 DRAW_NORMAL_TEXT, 0);
20903
20904 /* Advance the output cursor. */
20905 output_cursor.hpos += len;
20906 output_cursor.x += shift_by_width;
20907 UNBLOCK_INPUT;
20908 }
20909
20910
20911 /* EXPORT for RIF:
20912 Erase the current text line from the nominal cursor position
20913 (inclusive) to pixel column TO_X (exclusive). The idea is that
20914 everything from TO_X onward is already erased.
20915
20916 TO_X is a pixel position relative to updated_area of
20917 updated_window. TO_X == -1 means clear to the end of this area. */
20918
20919 void
20920 x_clear_end_of_line (to_x)
20921 int to_x;
20922 {
20923 struct frame *f;
20924 struct window *w = updated_window;
20925 int max_x, min_y, max_y;
20926 int from_x, from_y, to_y;
20927
20928 xassert (updated_window && updated_row);
20929 f = XFRAME (w->frame);
20930
20931 if (updated_row->full_width_p)
20932 max_x = WINDOW_TOTAL_WIDTH (w);
20933 else
20934 max_x = window_box_width (w, updated_area);
20935 max_y = window_text_bottom_y (w);
20936
20937 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20938 of window. For TO_X > 0, truncate to end of drawing area. */
20939 if (to_x == 0)
20940 return;
20941 else if (to_x < 0)
20942 to_x = max_x;
20943 else
20944 to_x = min (to_x, max_x);
20945
20946 to_y = min (max_y, output_cursor.y + updated_row->height);
20947
20948 /* Notice if the cursor will be cleared by this operation. */
20949 if (!updated_row->full_width_p)
20950 notice_overwritten_cursor (w, updated_area,
20951 output_cursor.x, -1,
20952 updated_row->y,
20953 MATRIX_ROW_BOTTOM_Y (updated_row));
20954
20955 from_x = output_cursor.x;
20956
20957 /* Translate to frame coordinates. */
20958 if (updated_row->full_width_p)
20959 {
20960 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20961 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20962 }
20963 else
20964 {
20965 int area_left = window_box_left (w, updated_area);
20966 from_x += area_left;
20967 to_x += area_left;
20968 }
20969
20970 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20971 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20972 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20973
20974 /* Prevent inadvertently clearing to end of the X window. */
20975 if (to_x > from_x && to_y > from_y)
20976 {
20977 BLOCK_INPUT;
20978 rif->clear_frame_area (f, from_x, from_y,
20979 to_x - from_x, to_y - from_y);
20980 UNBLOCK_INPUT;
20981 }
20982 }
20983
20984 #endif /* HAVE_WINDOW_SYSTEM */
20985
20986
20987 \f
20988 /***********************************************************************
20989 Cursor types
20990 ***********************************************************************/
20991
20992 /* Value is the internal representation of the specified cursor type
20993 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20994 of the bar cursor. */
20995
20996 static enum text_cursor_kinds
20997 get_specified_cursor_type (arg, width)
20998 Lisp_Object arg;
20999 int *width;
21000 {
21001 enum text_cursor_kinds type;
21002
21003 if (NILP (arg))
21004 return NO_CURSOR;
21005
21006 if (EQ (arg, Qbox))
21007 return FILLED_BOX_CURSOR;
21008
21009 if (EQ (arg, Qhollow))
21010 return HOLLOW_BOX_CURSOR;
21011
21012 if (EQ (arg, Qbar))
21013 {
21014 *width = 2;
21015 return BAR_CURSOR;
21016 }
21017
21018 if (CONSP (arg)
21019 && EQ (XCAR (arg), Qbar)
21020 && INTEGERP (XCDR (arg))
21021 && XINT (XCDR (arg)) >= 0)
21022 {
21023 *width = XINT (XCDR (arg));
21024 return BAR_CURSOR;
21025 }
21026
21027 if (EQ (arg, Qhbar))
21028 {
21029 *width = 2;
21030 return HBAR_CURSOR;
21031 }
21032
21033 if (CONSP (arg)
21034 && EQ (XCAR (arg), Qhbar)
21035 && INTEGERP (XCDR (arg))
21036 && XINT (XCDR (arg)) >= 0)
21037 {
21038 *width = XINT (XCDR (arg));
21039 return HBAR_CURSOR;
21040 }
21041
21042 /* Treat anything unknown as "hollow box cursor".
21043 It was bad to signal an error; people have trouble fixing
21044 .Xdefaults with Emacs, when it has something bad in it. */
21045 type = HOLLOW_BOX_CURSOR;
21046
21047 return type;
21048 }
21049
21050 /* Set the default cursor types for specified frame. */
21051 void
21052 set_frame_cursor_types (f, arg)
21053 struct frame *f;
21054 Lisp_Object arg;
21055 {
21056 int width;
21057 Lisp_Object tem;
21058
21059 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21060 FRAME_CURSOR_WIDTH (f) = width;
21061
21062 /* By default, set up the blink-off state depending on the on-state. */
21063
21064 tem = Fassoc (arg, Vblink_cursor_alist);
21065 if (!NILP (tem))
21066 {
21067 FRAME_BLINK_OFF_CURSOR (f)
21068 = get_specified_cursor_type (XCDR (tem), &width);
21069 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21070 }
21071 else
21072 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21073 }
21074
21075
21076 /* Return the cursor we want to be displayed in window W. Return
21077 width of bar/hbar cursor through WIDTH arg. Return with
21078 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21079 (i.e. if the `system caret' should track this cursor).
21080
21081 In a mini-buffer window, we want the cursor only to appear if we
21082 are reading input from this window. For the selected window, we
21083 want the cursor type given by the frame parameter or buffer local
21084 setting of cursor-type. If explicitly marked off, draw no cursor.
21085 In all other cases, we want a hollow box cursor. */
21086
21087 static enum text_cursor_kinds
21088 get_window_cursor_type (w, glyph, width, active_cursor)
21089 struct window *w;
21090 struct glyph *glyph;
21091 int *width;
21092 int *active_cursor;
21093 {
21094 struct frame *f = XFRAME (w->frame);
21095 struct buffer *b = XBUFFER (w->buffer);
21096 int cursor_type = DEFAULT_CURSOR;
21097 Lisp_Object alt_cursor;
21098 int non_selected = 0;
21099
21100 *active_cursor = 1;
21101
21102 /* Echo area */
21103 if (cursor_in_echo_area
21104 && FRAME_HAS_MINIBUF_P (f)
21105 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21106 {
21107 if (w == XWINDOW (echo_area_window))
21108 {
21109 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21110 {
21111 *width = FRAME_CURSOR_WIDTH (f);
21112 return FRAME_DESIRED_CURSOR (f);
21113 }
21114 else
21115 return get_specified_cursor_type (b->cursor_type, width);
21116 }
21117
21118 *active_cursor = 0;
21119 non_selected = 1;
21120 }
21121
21122 /* Nonselected window or nonselected frame. */
21123 else if (w != XWINDOW (f->selected_window)
21124 #ifdef HAVE_WINDOW_SYSTEM
21125 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21126 #endif
21127 )
21128 {
21129 *active_cursor = 0;
21130
21131 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21132 return NO_CURSOR;
21133
21134 non_selected = 1;
21135 }
21136
21137 /* Never display a cursor in a window in which cursor-type is nil. */
21138 if (NILP (b->cursor_type))
21139 return NO_CURSOR;
21140
21141 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21142 if (non_selected)
21143 {
21144 alt_cursor = b->cursor_in_non_selected_windows;
21145 return get_specified_cursor_type (alt_cursor, width);
21146 }
21147
21148 /* Get the normal cursor type for this window. */
21149 if (EQ (b->cursor_type, Qt))
21150 {
21151 cursor_type = FRAME_DESIRED_CURSOR (f);
21152 *width = FRAME_CURSOR_WIDTH (f);
21153 }
21154 else
21155 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21156
21157 /* Use normal cursor if not blinked off. */
21158 if (!w->cursor_off_p)
21159 {
21160 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
21161 if (cursor_type == FILLED_BOX_CURSOR)
21162 cursor_type = HOLLOW_BOX_CURSOR;
21163 }
21164 return cursor_type;
21165 }
21166
21167 /* Cursor is blinked off, so determine how to "toggle" it. */
21168
21169 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21170 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21171 return get_specified_cursor_type (XCDR (alt_cursor), width);
21172
21173 /* Then see if frame has specified a specific blink off cursor type. */
21174 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21175 {
21176 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21177 return FRAME_BLINK_OFF_CURSOR (f);
21178 }
21179
21180 #if 0
21181 /* Some people liked having a permanently visible blinking cursor,
21182 while others had very strong opinions against it. So it was
21183 decided to remove it. KFS 2003-09-03 */
21184
21185 /* Finally perform built-in cursor blinking:
21186 filled box <-> hollow box
21187 wide [h]bar <-> narrow [h]bar
21188 narrow [h]bar <-> no cursor
21189 other type <-> no cursor */
21190
21191 if (cursor_type == FILLED_BOX_CURSOR)
21192 return HOLLOW_BOX_CURSOR;
21193
21194 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21195 {
21196 *width = 1;
21197 return cursor_type;
21198 }
21199 #endif
21200
21201 return NO_CURSOR;
21202 }
21203
21204
21205 #ifdef HAVE_WINDOW_SYSTEM
21206
21207 /* Notice when the text cursor of window W has been completely
21208 overwritten by a drawing operation that outputs glyphs in AREA
21209 starting at X0 and ending at X1 in the line starting at Y0 and
21210 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21211 the rest of the line after X0 has been written. Y coordinates
21212 are window-relative. */
21213
21214 static void
21215 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21216 struct window *w;
21217 enum glyph_row_area area;
21218 int x0, y0, x1, y1;
21219 {
21220 int cx0, cx1, cy0, cy1;
21221 struct glyph_row *row;
21222
21223 if (!w->phys_cursor_on_p)
21224 return;
21225 if (area != TEXT_AREA)
21226 return;
21227
21228 if (w->phys_cursor.vpos < 0
21229 || w->phys_cursor.vpos >= w->current_matrix->nrows
21230 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21231 !(row->enabled_p && row->displays_text_p)))
21232 return;
21233
21234 if (row->cursor_in_fringe_p)
21235 {
21236 row->cursor_in_fringe_p = 0;
21237 draw_fringe_bitmap (w, row, 0);
21238 w->phys_cursor_on_p = 0;
21239 return;
21240 }
21241
21242 cx0 = w->phys_cursor.x;
21243 cx1 = cx0 + w->phys_cursor_width;
21244 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21245 return;
21246
21247 /* The cursor image will be completely removed from the
21248 screen if the output area intersects the cursor area in
21249 y-direction. When we draw in [y0 y1[, and some part of
21250 the cursor is at y < y0, that part must have been drawn
21251 before. When scrolling, the cursor is erased before
21252 actually scrolling, so we don't come here. When not
21253 scrolling, the rows above the old cursor row must have
21254 changed, and in this case these rows must have written
21255 over the cursor image.
21256
21257 Likewise if part of the cursor is below y1, with the
21258 exception of the cursor being in the first blank row at
21259 the buffer and window end because update_text_area
21260 doesn't draw that row. (Except when it does, but
21261 that's handled in update_text_area.) */
21262
21263 cy0 = w->phys_cursor.y;
21264 cy1 = cy0 + w->phys_cursor_height;
21265 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21266 return;
21267
21268 w->phys_cursor_on_p = 0;
21269 }
21270
21271 #endif /* HAVE_WINDOW_SYSTEM */
21272
21273 \f
21274 /************************************************************************
21275 Mouse Face
21276 ************************************************************************/
21277
21278 #ifdef HAVE_WINDOW_SYSTEM
21279
21280 /* EXPORT for RIF:
21281 Fix the display of area AREA of overlapping row ROW in window W
21282 with respect to the overlapping part OVERLAPS. */
21283
21284 void
21285 x_fix_overlapping_area (w, row, area, overlaps)
21286 struct window *w;
21287 struct glyph_row *row;
21288 enum glyph_row_area area;
21289 int overlaps;
21290 {
21291 int i, x;
21292
21293 BLOCK_INPUT;
21294
21295 x = 0;
21296 for (i = 0; i < row->used[area];)
21297 {
21298 if (row->glyphs[area][i].overlaps_vertically_p)
21299 {
21300 int start = i, start_x = x;
21301
21302 do
21303 {
21304 x += row->glyphs[area][i].pixel_width;
21305 ++i;
21306 }
21307 while (i < row->used[area]
21308 && row->glyphs[area][i].overlaps_vertically_p);
21309
21310 draw_glyphs (w, start_x, row, area,
21311 start, i,
21312 DRAW_NORMAL_TEXT, overlaps);
21313 }
21314 else
21315 {
21316 x += row->glyphs[area][i].pixel_width;
21317 ++i;
21318 }
21319 }
21320
21321 UNBLOCK_INPUT;
21322 }
21323
21324
21325 /* EXPORT:
21326 Draw the cursor glyph of window W in glyph row ROW. See the
21327 comment of draw_glyphs for the meaning of HL. */
21328
21329 void
21330 draw_phys_cursor_glyph (w, row, hl)
21331 struct window *w;
21332 struct glyph_row *row;
21333 enum draw_glyphs_face hl;
21334 {
21335 /* If cursor hpos is out of bounds, don't draw garbage. This can
21336 happen in mini-buffer windows when switching between echo area
21337 glyphs and mini-buffer. */
21338 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21339 {
21340 int on_p = w->phys_cursor_on_p;
21341 int x1;
21342 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21343 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21344 hl, 0);
21345 w->phys_cursor_on_p = on_p;
21346
21347 if (hl == DRAW_CURSOR)
21348 w->phys_cursor_width = x1 - w->phys_cursor.x;
21349 /* When we erase the cursor, and ROW is overlapped by other
21350 rows, make sure that these overlapping parts of other rows
21351 are redrawn. */
21352 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21353 {
21354 w->phys_cursor_width = x1 - w->phys_cursor.x;
21355
21356 if (row > w->current_matrix->rows
21357 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21358 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21359 OVERLAPS_ERASED_CURSOR);
21360
21361 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21362 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21363 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21364 OVERLAPS_ERASED_CURSOR);
21365 }
21366 }
21367 }
21368
21369
21370 /* EXPORT:
21371 Erase the image of a cursor of window W from the screen. */
21372
21373 void
21374 erase_phys_cursor (w)
21375 struct window *w;
21376 {
21377 struct frame *f = XFRAME (w->frame);
21378 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21379 int hpos = w->phys_cursor.hpos;
21380 int vpos = w->phys_cursor.vpos;
21381 int mouse_face_here_p = 0;
21382 struct glyph_matrix *active_glyphs = w->current_matrix;
21383 struct glyph_row *cursor_row;
21384 struct glyph *cursor_glyph;
21385 enum draw_glyphs_face hl;
21386
21387 /* No cursor displayed or row invalidated => nothing to do on the
21388 screen. */
21389 if (w->phys_cursor_type == NO_CURSOR)
21390 goto mark_cursor_off;
21391
21392 /* VPOS >= active_glyphs->nrows means that window has been resized.
21393 Don't bother to erase the cursor. */
21394 if (vpos >= active_glyphs->nrows)
21395 goto mark_cursor_off;
21396
21397 /* If row containing cursor is marked invalid, there is nothing we
21398 can do. */
21399 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21400 if (!cursor_row->enabled_p)
21401 goto mark_cursor_off;
21402
21403 /* If line spacing is > 0, old cursor may only be partially visible in
21404 window after split-window. So adjust visible height. */
21405 cursor_row->visible_height = min (cursor_row->visible_height,
21406 window_text_bottom_y (w) - cursor_row->y);
21407
21408 /* If row is completely invisible, don't attempt to delete a cursor which
21409 isn't there. This can happen if cursor is at top of a window, and
21410 we switch to a buffer with a header line in that window. */
21411 if (cursor_row->visible_height <= 0)
21412 goto mark_cursor_off;
21413
21414 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21415 if (cursor_row->cursor_in_fringe_p)
21416 {
21417 cursor_row->cursor_in_fringe_p = 0;
21418 draw_fringe_bitmap (w, cursor_row, 0);
21419 goto mark_cursor_off;
21420 }
21421
21422 /* This can happen when the new row is shorter than the old one.
21423 In this case, either draw_glyphs or clear_end_of_line
21424 should have cleared the cursor. Note that we wouldn't be
21425 able to erase the cursor in this case because we don't have a
21426 cursor glyph at hand. */
21427 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21428 goto mark_cursor_off;
21429
21430 /* If the cursor is in the mouse face area, redisplay that when
21431 we clear the cursor. */
21432 if (! NILP (dpyinfo->mouse_face_window)
21433 && w == XWINDOW (dpyinfo->mouse_face_window)
21434 && (vpos > dpyinfo->mouse_face_beg_row
21435 || (vpos == dpyinfo->mouse_face_beg_row
21436 && hpos >= dpyinfo->mouse_face_beg_col))
21437 && (vpos < dpyinfo->mouse_face_end_row
21438 || (vpos == dpyinfo->mouse_face_end_row
21439 && hpos < dpyinfo->mouse_face_end_col))
21440 /* Don't redraw the cursor's spot in mouse face if it is at the
21441 end of a line (on a newline). The cursor appears there, but
21442 mouse highlighting does not. */
21443 && cursor_row->used[TEXT_AREA] > hpos)
21444 mouse_face_here_p = 1;
21445
21446 /* Maybe clear the display under the cursor. */
21447 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21448 {
21449 int x, y, left_x;
21450 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21451 int width;
21452
21453 cursor_glyph = get_phys_cursor_glyph (w);
21454 if (cursor_glyph == NULL)
21455 goto mark_cursor_off;
21456
21457 width = cursor_glyph->pixel_width;
21458 left_x = window_box_left_offset (w, TEXT_AREA);
21459 x = w->phys_cursor.x;
21460 if (x < left_x)
21461 width -= left_x - x;
21462 width = min (width, window_box_width (w, TEXT_AREA) - x);
21463 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21464 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21465
21466 if (width > 0)
21467 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21468 }
21469
21470 /* Erase the cursor by redrawing the character underneath it. */
21471 if (mouse_face_here_p)
21472 hl = DRAW_MOUSE_FACE;
21473 else
21474 hl = DRAW_NORMAL_TEXT;
21475 draw_phys_cursor_glyph (w, cursor_row, hl);
21476
21477 mark_cursor_off:
21478 w->phys_cursor_on_p = 0;
21479 w->phys_cursor_type = NO_CURSOR;
21480 }
21481
21482
21483 /* EXPORT:
21484 Display or clear cursor of window W. If ON is zero, clear the
21485 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21486 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21487
21488 void
21489 display_and_set_cursor (w, on, hpos, vpos, x, y)
21490 struct window *w;
21491 int on, hpos, vpos, x, y;
21492 {
21493 struct frame *f = XFRAME (w->frame);
21494 int new_cursor_type;
21495 int new_cursor_width;
21496 int active_cursor;
21497 struct glyph_row *glyph_row;
21498 struct glyph *glyph;
21499
21500 /* This is pointless on invisible frames, and dangerous on garbaged
21501 windows and frames; in the latter case, the frame or window may
21502 be in the midst of changing its size, and x and y may be off the
21503 window. */
21504 if (! FRAME_VISIBLE_P (f)
21505 || FRAME_GARBAGED_P (f)
21506 || vpos >= w->current_matrix->nrows
21507 || hpos >= w->current_matrix->matrix_w)
21508 return;
21509
21510 /* If cursor is off and we want it off, return quickly. */
21511 if (!on && !w->phys_cursor_on_p)
21512 return;
21513
21514 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21515 /* If cursor row is not enabled, we don't really know where to
21516 display the cursor. */
21517 if (!glyph_row->enabled_p)
21518 {
21519 w->phys_cursor_on_p = 0;
21520 return;
21521 }
21522
21523 glyph = NULL;
21524 if (!glyph_row->exact_window_width_line_p
21525 || hpos < glyph_row->used[TEXT_AREA])
21526 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21527
21528 xassert (interrupt_input_blocked);
21529
21530 /* Set new_cursor_type to the cursor we want to be displayed. */
21531 new_cursor_type = get_window_cursor_type (w, glyph,
21532 &new_cursor_width, &active_cursor);
21533
21534 /* If cursor is currently being shown and we don't want it to be or
21535 it is in the wrong place, or the cursor type is not what we want,
21536 erase it. */
21537 if (w->phys_cursor_on_p
21538 && (!on
21539 || w->phys_cursor.x != x
21540 || w->phys_cursor.y != y
21541 || new_cursor_type != w->phys_cursor_type
21542 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21543 && new_cursor_width != w->phys_cursor_width)))
21544 erase_phys_cursor (w);
21545
21546 /* Don't check phys_cursor_on_p here because that flag is only set
21547 to zero in some cases where we know that the cursor has been
21548 completely erased, to avoid the extra work of erasing the cursor
21549 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21550 still not be visible, or it has only been partly erased. */
21551 if (on)
21552 {
21553 w->phys_cursor_ascent = glyph_row->ascent;
21554 w->phys_cursor_height = glyph_row->height;
21555
21556 /* Set phys_cursor_.* before x_draw_.* is called because some
21557 of them may need the information. */
21558 w->phys_cursor.x = x;
21559 w->phys_cursor.y = glyph_row->y;
21560 w->phys_cursor.hpos = hpos;
21561 w->phys_cursor.vpos = vpos;
21562 }
21563
21564 rif->draw_window_cursor (w, glyph_row, x, y,
21565 new_cursor_type, new_cursor_width,
21566 on, active_cursor);
21567 }
21568
21569
21570 /* Switch the display of W's cursor on or off, according to the value
21571 of ON. */
21572
21573 static void
21574 update_window_cursor (w, on)
21575 struct window *w;
21576 int on;
21577 {
21578 /* Don't update cursor in windows whose frame is in the process
21579 of being deleted. */
21580 if (w->current_matrix)
21581 {
21582 BLOCK_INPUT;
21583 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21584 w->phys_cursor.x, w->phys_cursor.y);
21585 UNBLOCK_INPUT;
21586 }
21587 }
21588
21589
21590 /* Call update_window_cursor with parameter ON_P on all leaf windows
21591 in the window tree rooted at W. */
21592
21593 static void
21594 update_cursor_in_window_tree (w, on_p)
21595 struct window *w;
21596 int on_p;
21597 {
21598 while (w)
21599 {
21600 if (!NILP (w->hchild))
21601 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21602 else if (!NILP (w->vchild))
21603 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21604 else
21605 update_window_cursor (w, on_p);
21606
21607 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21608 }
21609 }
21610
21611
21612 /* EXPORT:
21613 Display the cursor on window W, or clear it, according to ON_P.
21614 Don't change the cursor's position. */
21615
21616 void
21617 x_update_cursor (f, on_p)
21618 struct frame *f;
21619 int on_p;
21620 {
21621 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21622 }
21623
21624
21625 /* EXPORT:
21626 Clear the cursor of window W to background color, and mark the
21627 cursor as not shown. This is used when the text where the cursor
21628 is is about to be rewritten. */
21629
21630 void
21631 x_clear_cursor (w)
21632 struct window *w;
21633 {
21634 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21635 update_window_cursor (w, 0);
21636 }
21637
21638
21639 /* EXPORT:
21640 Display the active region described by mouse_face_* according to DRAW. */
21641
21642 void
21643 show_mouse_face (dpyinfo, draw)
21644 Display_Info *dpyinfo;
21645 enum draw_glyphs_face draw;
21646 {
21647 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21648 struct frame *f = XFRAME (WINDOW_FRAME (w));
21649
21650 if (/* If window is in the process of being destroyed, don't bother
21651 to do anything. */
21652 w->current_matrix != NULL
21653 /* Don't update mouse highlight if hidden */
21654 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21655 /* Recognize when we are called to operate on rows that don't exist
21656 anymore. This can happen when a window is split. */
21657 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21658 {
21659 int phys_cursor_on_p = w->phys_cursor_on_p;
21660 struct glyph_row *row, *first, *last;
21661
21662 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21663 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21664
21665 for (row = first; row <= last && row->enabled_p; ++row)
21666 {
21667 int start_hpos, end_hpos, start_x;
21668
21669 /* For all but the first row, the highlight starts at column 0. */
21670 if (row == first)
21671 {
21672 start_hpos = dpyinfo->mouse_face_beg_col;
21673 start_x = dpyinfo->mouse_face_beg_x;
21674 }
21675 else
21676 {
21677 start_hpos = 0;
21678 start_x = 0;
21679 }
21680
21681 if (row == last)
21682 end_hpos = dpyinfo->mouse_face_end_col;
21683 else
21684 {
21685 end_hpos = row->used[TEXT_AREA];
21686 if (draw == DRAW_NORMAL_TEXT)
21687 row->fill_line_p = 1; /* Clear to end of line */
21688 }
21689
21690 if (end_hpos > start_hpos)
21691 {
21692 draw_glyphs (w, start_x, row, TEXT_AREA,
21693 start_hpos, end_hpos,
21694 draw, 0);
21695
21696 row->mouse_face_p
21697 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21698 }
21699 }
21700
21701 /* When we've written over the cursor, arrange for it to
21702 be displayed again. */
21703 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21704 {
21705 BLOCK_INPUT;
21706 display_and_set_cursor (w, 1,
21707 w->phys_cursor.hpos, w->phys_cursor.vpos,
21708 w->phys_cursor.x, w->phys_cursor.y);
21709 UNBLOCK_INPUT;
21710 }
21711 }
21712
21713 /* Change the mouse cursor. */
21714 if (draw == DRAW_NORMAL_TEXT)
21715 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21716 else if (draw == DRAW_MOUSE_FACE)
21717 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21718 else
21719 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21720 }
21721
21722 /* EXPORT:
21723 Clear out the mouse-highlighted active region.
21724 Redraw it un-highlighted first. Value is non-zero if mouse
21725 face was actually drawn unhighlighted. */
21726
21727 int
21728 clear_mouse_face (dpyinfo)
21729 Display_Info *dpyinfo;
21730 {
21731 int cleared = 0;
21732
21733 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21734 {
21735 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21736 cleared = 1;
21737 }
21738
21739 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21740 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21741 dpyinfo->mouse_face_window = Qnil;
21742 dpyinfo->mouse_face_overlay = Qnil;
21743 return cleared;
21744 }
21745
21746
21747 /* EXPORT:
21748 Non-zero if physical cursor of window W is within mouse face. */
21749
21750 int
21751 cursor_in_mouse_face_p (w)
21752 struct window *w;
21753 {
21754 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21755 int in_mouse_face = 0;
21756
21757 if (WINDOWP (dpyinfo->mouse_face_window)
21758 && XWINDOW (dpyinfo->mouse_face_window) == w)
21759 {
21760 int hpos = w->phys_cursor.hpos;
21761 int vpos = w->phys_cursor.vpos;
21762
21763 if (vpos >= dpyinfo->mouse_face_beg_row
21764 && vpos <= dpyinfo->mouse_face_end_row
21765 && (vpos > dpyinfo->mouse_face_beg_row
21766 || hpos >= dpyinfo->mouse_face_beg_col)
21767 && (vpos < dpyinfo->mouse_face_end_row
21768 || hpos < dpyinfo->mouse_face_end_col
21769 || dpyinfo->mouse_face_past_end))
21770 in_mouse_face = 1;
21771 }
21772
21773 return in_mouse_face;
21774 }
21775
21776
21777
21778 \f
21779 /* Find the glyph matrix position of buffer position CHARPOS in window
21780 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21781 current glyphs must be up to date. If CHARPOS is above window
21782 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21783 of last line in W. In the row containing CHARPOS, stop before glyphs
21784 having STOP as object. */
21785
21786 #if 1 /* This is a version of fast_find_position that's more correct
21787 in the presence of hscrolling, for example. I didn't install
21788 it right away because the problem fixed is minor, it failed
21789 in 20.x as well, and I think it's too risky to install
21790 so near the release of 21.1. 2001-09-25 gerd. */
21791
21792 static int
21793 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21794 struct window *w;
21795 int charpos;
21796 int *hpos, *vpos, *x, *y;
21797 Lisp_Object stop;
21798 {
21799 struct glyph_row *row, *first;
21800 struct glyph *glyph, *end;
21801 int past_end = 0;
21802
21803 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21804 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21805 {
21806 *x = first->x;
21807 *y = first->y;
21808 *hpos = 0;
21809 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21810 return 1;
21811 }
21812
21813 row = row_containing_pos (w, charpos, first, NULL, 0);
21814 if (row == NULL)
21815 {
21816 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21817 past_end = 1;
21818 }
21819
21820 /* If whole rows or last part of a row came from a display overlay,
21821 row_containing_pos will skip over such rows because their end pos
21822 equals the start pos of the overlay or interval.
21823
21824 Move back if we have a STOP object and previous row's
21825 end glyph came from STOP. */
21826 if (!NILP (stop))
21827 {
21828 struct glyph_row *prev;
21829 while ((prev = row - 1, prev >= first)
21830 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21831 && prev->used[TEXT_AREA] > 0)
21832 {
21833 struct glyph *beg = prev->glyphs[TEXT_AREA];
21834 glyph = beg + prev->used[TEXT_AREA];
21835 while (--glyph >= beg
21836 && INTEGERP (glyph->object));
21837 if (glyph < beg
21838 || !EQ (stop, glyph->object))
21839 break;
21840 row = prev;
21841 }
21842 }
21843
21844 *x = row->x;
21845 *y = row->y;
21846 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21847
21848 glyph = row->glyphs[TEXT_AREA];
21849 end = glyph + row->used[TEXT_AREA];
21850
21851 /* Skip over glyphs not having an object at the start of the row.
21852 These are special glyphs like truncation marks on terminal
21853 frames. */
21854 if (row->displays_text_p)
21855 while (glyph < end
21856 && INTEGERP (glyph->object)
21857 && !EQ (stop, glyph->object)
21858 && glyph->charpos < 0)
21859 {
21860 *x += glyph->pixel_width;
21861 ++glyph;
21862 }
21863
21864 while (glyph < end
21865 && !INTEGERP (glyph->object)
21866 && !EQ (stop, glyph->object)
21867 && (!BUFFERP (glyph->object)
21868 || glyph->charpos < charpos))
21869 {
21870 *x += glyph->pixel_width;
21871 ++glyph;
21872 }
21873
21874 *hpos = glyph - row->glyphs[TEXT_AREA];
21875 return !past_end;
21876 }
21877
21878 #else /* not 1 */
21879
21880 static int
21881 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21882 struct window *w;
21883 int pos;
21884 int *hpos, *vpos, *x, *y;
21885 Lisp_Object stop;
21886 {
21887 int i;
21888 int lastcol;
21889 int maybe_next_line_p = 0;
21890 int line_start_position;
21891 int yb = window_text_bottom_y (w);
21892 struct glyph_row *row, *best_row;
21893 int row_vpos, best_row_vpos;
21894 int current_x;
21895
21896 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21897 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21898
21899 while (row->y < yb)
21900 {
21901 if (row->used[TEXT_AREA])
21902 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21903 else
21904 line_start_position = 0;
21905
21906 if (line_start_position > pos)
21907 break;
21908 /* If the position sought is the end of the buffer,
21909 don't include the blank lines at the bottom of the window. */
21910 else if (line_start_position == pos
21911 && pos == BUF_ZV (XBUFFER (w->buffer)))
21912 {
21913 maybe_next_line_p = 1;
21914 break;
21915 }
21916 else if (line_start_position > 0)
21917 {
21918 best_row = row;
21919 best_row_vpos = row_vpos;
21920 }
21921
21922 if (row->y + row->height >= yb)
21923 break;
21924
21925 ++row;
21926 ++row_vpos;
21927 }
21928
21929 /* Find the right column within BEST_ROW. */
21930 lastcol = 0;
21931 current_x = best_row->x;
21932 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21933 {
21934 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21935 int charpos = glyph->charpos;
21936
21937 if (BUFFERP (glyph->object))
21938 {
21939 if (charpos == pos)
21940 {
21941 *hpos = i;
21942 *vpos = best_row_vpos;
21943 *x = current_x;
21944 *y = best_row->y;
21945 return 1;
21946 }
21947 else if (charpos > pos)
21948 break;
21949 }
21950 else if (EQ (glyph->object, stop))
21951 break;
21952
21953 if (charpos > 0)
21954 lastcol = i;
21955 current_x += glyph->pixel_width;
21956 }
21957
21958 /* If we're looking for the end of the buffer,
21959 and we didn't find it in the line we scanned,
21960 use the start of the following line. */
21961 if (maybe_next_line_p)
21962 {
21963 ++best_row;
21964 ++best_row_vpos;
21965 lastcol = 0;
21966 current_x = best_row->x;
21967 }
21968
21969 *vpos = best_row_vpos;
21970 *hpos = lastcol + 1;
21971 *x = current_x;
21972 *y = best_row->y;
21973 return 0;
21974 }
21975
21976 #endif /* not 1 */
21977
21978
21979 /* Find the position of the glyph for position POS in OBJECT in
21980 window W's current matrix, and return in *X, *Y the pixel
21981 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21982
21983 RIGHT_P non-zero means return the position of the right edge of the
21984 glyph, RIGHT_P zero means return the left edge position.
21985
21986 If no glyph for POS exists in the matrix, return the position of
21987 the glyph with the next smaller position that is in the matrix, if
21988 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21989 exists in the matrix, return the position of the glyph with the
21990 next larger position in OBJECT.
21991
21992 Value is non-zero if a glyph was found. */
21993
21994 static int
21995 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21996 struct window *w;
21997 int pos;
21998 Lisp_Object object;
21999 int *hpos, *vpos, *x, *y;
22000 int right_p;
22001 {
22002 int yb = window_text_bottom_y (w);
22003 struct glyph_row *r;
22004 struct glyph *best_glyph = NULL;
22005 struct glyph_row *best_row = NULL;
22006 int best_x = 0;
22007
22008 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22009 r->enabled_p && r->y < yb;
22010 ++r)
22011 {
22012 struct glyph *g = r->glyphs[TEXT_AREA];
22013 struct glyph *e = g + r->used[TEXT_AREA];
22014 int gx;
22015
22016 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22017 if (EQ (g->object, object))
22018 {
22019 if (g->charpos == pos)
22020 {
22021 best_glyph = g;
22022 best_x = gx;
22023 best_row = r;
22024 goto found;
22025 }
22026 else if (best_glyph == NULL
22027 || ((abs (g->charpos - pos)
22028 < abs (best_glyph->charpos - pos))
22029 && (right_p
22030 ? g->charpos < pos
22031 : g->charpos > pos)))
22032 {
22033 best_glyph = g;
22034 best_x = gx;
22035 best_row = r;
22036 }
22037 }
22038 }
22039
22040 found:
22041
22042 if (best_glyph)
22043 {
22044 *x = best_x;
22045 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22046
22047 if (right_p)
22048 {
22049 *x += best_glyph->pixel_width;
22050 ++*hpos;
22051 }
22052
22053 *y = best_row->y;
22054 *vpos = best_row - w->current_matrix->rows;
22055 }
22056
22057 return best_glyph != NULL;
22058 }
22059
22060
22061 /* See if position X, Y is within a hot-spot of an image. */
22062
22063 static int
22064 on_hot_spot_p (hot_spot, x, y)
22065 Lisp_Object hot_spot;
22066 int x, y;
22067 {
22068 if (!CONSP (hot_spot))
22069 return 0;
22070
22071 if (EQ (XCAR (hot_spot), Qrect))
22072 {
22073 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22074 Lisp_Object rect = XCDR (hot_spot);
22075 Lisp_Object tem;
22076 if (!CONSP (rect))
22077 return 0;
22078 if (!CONSP (XCAR (rect)))
22079 return 0;
22080 if (!CONSP (XCDR (rect)))
22081 return 0;
22082 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22083 return 0;
22084 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22085 return 0;
22086 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22087 return 0;
22088 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22089 return 0;
22090 return 1;
22091 }
22092 else if (EQ (XCAR (hot_spot), Qcircle))
22093 {
22094 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22095 Lisp_Object circ = XCDR (hot_spot);
22096 Lisp_Object lr, lx0, ly0;
22097 if (CONSP (circ)
22098 && CONSP (XCAR (circ))
22099 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22100 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22101 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22102 {
22103 double r = XFLOATINT (lr);
22104 double dx = XINT (lx0) - x;
22105 double dy = XINT (ly0) - y;
22106 return (dx * dx + dy * dy <= r * r);
22107 }
22108 }
22109 else if (EQ (XCAR (hot_spot), Qpoly))
22110 {
22111 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22112 if (VECTORP (XCDR (hot_spot)))
22113 {
22114 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22115 Lisp_Object *poly = v->contents;
22116 int n = v->size;
22117 int i;
22118 int inside = 0;
22119 Lisp_Object lx, ly;
22120 int x0, y0;
22121
22122 /* Need an even number of coordinates, and at least 3 edges. */
22123 if (n < 6 || n & 1)
22124 return 0;
22125
22126 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22127 If count is odd, we are inside polygon. Pixels on edges
22128 may or may not be included depending on actual geometry of the
22129 polygon. */
22130 if ((lx = poly[n-2], !INTEGERP (lx))
22131 || (ly = poly[n-1], !INTEGERP (lx)))
22132 return 0;
22133 x0 = XINT (lx), y0 = XINT (ly);
22134 for (i = 0; i < n; i += 2)
22135 {
22136 int x1 = x0, y1 = y0;
22137 if ((lx = poly[i], !INTEGERP (lx))
22138 || (ly = poly[i+1], !INTEGERP (ly)))
22139 return 0;
22140 x0 = XINT (lx), y0 = XINT (ly);
22141
22142 /* Does this segment cross the X line? */
22143 if (x0 >= x)
22144 {
22145 if (x1 >= x)
22146 continue;
22147 }
22148 else if (x1 < x)
22149 continue;
22150 if (y > y0 && y > y1)
22151 continue;
22152 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22153 inside = !inside;
22154 }
22155 return inside;
22156 }
22157 }
22158 /* If we don't understand the format, pretend we're not in the hot-spot. */
22159 return 0;
22160 }
22161
22162 Lisp_Object
22163 find_hot_spot (map, x, y)
22164 Lisp_Object map;
22165 int x, y;
22166 {
22167 while (CONSP (map))
22168 {
22169 if (CONSP (XCAR (map))
22170 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22171 return XCAR (map);
22172 map = XCDR (map);
22173 }
22174
22175 return Qnil;
22176 }
22177
22178 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22179 3, 3, 0,
22180 doc: /* Lookup in image map MAP coordinates X and Y.
22181 An image map is an alist where each element has the format (AREA ID PLIST).
22182 An AREA is specified as either a rectangle, a circle, or a polygon:
22183 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22184 pixel coordinates of the upper left and bottom right corners.
22185 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22186 and the radius of the circle; r may be a float or integer.
22187 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22188 vector describes one corner in the polygon.
22189 Returns the alist element for the first matching AREA in MAP. */)
22190 (map, x, y)
22191 Lisp_Object map;
22192 Lisp_Object x, y;
22193 {
22194 if (NILP (map))
22195 return Qnil;
22196
22197 CHECK_NUMBER (x);
22198 CHECK_NUMBER (y);
22199
22200 return find_hot_spot (map, XINT (x), XINT (y));
22201 }
22202
22203
22204 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22205 static void
22206 define_frame_cursor1 (f, cursor, pointer)
22207 struct frame *f;
22208 Cursor cursor;
22209 Lisp_Object pointer;
22210 {
22211 /* Do not change cursor shape while dragging mouse. */
22212 if (!NILP (do_mouse_tracking))
22213 return;
22214
22215 if (!NILP (pointer))
22216 {
22217 if (EQ (pointer, Qarrow))
22218 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22219 else if (EQ (pointer, Qhand))
22220 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22221 else if (EQ (pointer, Qtext))
22222 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22223 else if (EQ (pointer, intern ("hdrag")))
22224 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22225 #ifdef HAVE_X_WINDOWS
22226 else if (EQ (pointer, intern ("vdrag")))
22227 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22228 #endif
22229 else if (EQ (pointer, intern ("hourglass")))
22230 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22231 else if (EQ (pointer, Qmodeline))
22232 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22233 else
22234 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22235 }
22236
22237 if (cursor != No_Cursor)
22238 rif->define_frame_cursor (f, cursor);
22239 }
22240
22241 /* Take proper action when mouse has moved to the mode or header line
22242 or marginal area AREA of window W, x-position X and y-position Y.
22243 X is relative to the start of the text display area of W, so the
22244 width of bitmap areas and scroll bars must be subtracted to get a
22245 position relative to the start of the mode line. */
22246
22247 static void
22248 note_mode_line_or_margin_highlight (window, x, y, area)
22249 Lisp_Object window;
22250 int x, y;
22251 enum window_part area;
22252 {
22253 struct window *w = XWINDOW (window);
22254 struct frame *f = XFRAME (w->frame);
22255 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22256 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22257 Lisp_Object pointer = Qnil;
22258 int charpos, dx, dy, width, height;
22259 Lisp_Object string, object = Qnil;
22260 Lisp_Object pos, help;
22261
22262 Lisp_Object mouse_face;
22263 int original_x_pixel = x;
22264 struct glyph * glyph = NULL;
22265 struct glyph_row *row;
22266
22267 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22268 {
22269 int x0;
22270 struct glyph *end;
22271
22272 string = mode_line_string (w, area, &x, &y, &charpos,
22273 &object, &dx, &dy, &width, &height);
22274
22275 row = (area == ON_MODE_LINE
22276 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22277 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22278
22279 /* Find glyph */
22280 if (row->mode_line_p && row->enabled_p)
22281 {
22282 glyph = row->glyphs[TEXT_AREA];
22283 end = glyph + row->used[TEXT_AREA];
22284
22285 for (x0 = original_x_pixel;
22286 glyph < end && x0 >= glyph->pixel_width;
22287 ++glyph)
22288 x0 -= glyph->pixel_width;
22289
22290 if (glyph >= end)
22291 glyph = NULL;
22292 }
22293 }
22294 else
22295 {
22296 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22297 string = marginal_area_string (w, area, &x, &y, &charpos,
22298 &object, &dx, &dy, &width, &height);
22299 }
22300
22301 help = Qnil;
22302
22303 if (IMAGEP (object))
22304 {
22305 Lisp_Object image_map, hotspot;
22306 if ((image_map = Fplist_get (XCDR (object), QCmap),
22307 !NILP (image_map))
22308 && (hotspot = find_hot_spot (image_map, dx, dy),
22309 CONSP (hotspot))
22310 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22311 {
22312 Lisp_Object area_id, plist;
22313
22314 area_id = XCAR (hotspot);
22315 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22316 If so, we could look for mouse-enter, mouse-leave
22317 properties in PLIST (and do something...). */
22318 hotspot = XCDR (hotspot);
22319 if (CONSP (hotspot)
22320 && (plist = XCAR (hotspot), CONSP (plist)))
22321 {
22322 pointer = Fplist_get (plist, Qpointer);
22323 if (NILP (pointer))
22324 pointer = Qhand;
22325 help = Fplist_get (plist, Qhelp_echo);
22326 if (!NILP (help))
22327 {
22328 help_echo_string = help;
22329 /* Is this correct? ++kfs */
22330 XSETWINDOW (help_echo_window, w);
22331 help_echo_object = w->buffer;
22332 help_echo_pos = charpos;
22333 }
22334 }
22335 }
22336 if (NILP (pointer))
22337 pointer = Fplist_get (XCDR (object), QCpointer);
22338 }
22339
22340 if (STRINGP (string))
22341 {
22342 pos = make_number (charpos);
22343 /* If we're on a string with `help-echo' text property, arrange
22344 for the help to be displayed. This is done by setting the
22345 global variable help_echo_string to the help string. */
22346 if (NILP (help))
22347 {
22348 help = Fget_text_property (pos, Qhelp_echo, string);
22349 if (!NILP (help))
22350 {
22351 help_echo_string = help;
22352 XSETWINDOW (help_echo_window, w);
22353 help_echo_object = string;
22354 help_echo_pos = charpos;
22355 }
22356 }
22357
22358 if (NILP (pointer))
22359 pointer = Fget_text_property (pos, Qpointer, string);
22360
22361 /* Change the mouse pointer according to what is under X/Y. */
22362 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22363 {
22364 Lisp_Object map;
22365 map = Fget_text_property (pos, Qlocal_map, string);
22366 if (!KEYMAPP (map))
22367 map = Fget_text_property (pos, Qkeymap, string);
22368 if (!KEYMAPP (map))
22369 cursor = dpyinfo->vertical_scroll_bar_cursor;
22370 }
22371
22372 /* Change the mouse face according to what is under X/Y. */
22373 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22374 if (!NILP (mouse_face)
22375 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22376 && glyph)
22377 {
22378 Lisp_Object b, e;
22379
22380 struct glyph * tmp_glyph;
22381
22382 int gpos;
22383 int gseq_length;
22384 int total_pixel_width;
22385 int ignore;
22386
22387 int vpos, hpos;
22388
22389 b = Fprevious_single_property_change (make_number (charpos + 1),
22390 Qmouse_face, string, Qnil);
22391 if (NILP (b))
22392 b = make_number (0);
22393
22394 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22395 if (NILP (e))
22396 e = make_number (SCHARS (string));
22397
22398 /* Calculate the position(glyph position: GPOS) of GLYPH in
22399 displayed string. GPOS is different from CHARPOS.
22400
22401 CHARPOS is the position of glyph in internal string
22402 object. A mode line string format has structures which
22403 is converted to a flatten by emacs lisp interpreter.
22404 The internal string is an element of the structures.
22405 The displayed string is the flatten string. */
22406 for (tmp_glyph = glyph - 1, gpos = 0;
22407 tmp_glyph->charpos >= XINT (b);
22408 tmp_glyph--, gpos++)
22409 {
22410 if (!EQ (tmp_glyph->object, glyph->object))
22411 break;
22412 }
22413
22414 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22415 displayed string holding GLYPH.
22416
22417 GSEQ_LENGTH is different from SCHARS (STRING).
22418 SCHARS (STRING) returns the length of the internal string. */
22419 for (tmp_glyph = glyph, gseq_length = gpos;
22420 tmp_glyph->charpos < XINT (e);
22421 tmp_glyph++, gseq_length++)
22422 {
22423 if (!EQ (tmp_glyph->object, glyph->object))
22424 break;
22425 }
22426
22427 total_pixel_width = 0;
22428 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22429 total_pixel_width += tmp_glyph->pixel_width;
22430
22431 /* Pre calculation of re-rendering position */
22432 vpos = (x - gpos);
22433 hpos = (area == ON_MODE_LINE
22434 ? (w->current_matrix)->nrows - 1
22435 : 0);
22436
22437 /* If the re-rendering position is included in the last
22438 re-rendering area, we should do nothing. */
22439 if ( EQ (window, dpyinfo->mouse_face_window)
22440 && dpyinfo->mouse_face_beg_col <= vpos
22441 && vpos < dpyinfo->mouse_face_end_col
22442 && dpyinfo->mouse_face_beg_row == hpos )
22443 return;
22444
22445 if (clear_mouse_face (dpyinfo))
22446 cursor = No_Cursor;
22447
22448 dpyinfo->mouse_face_beg_col = vpos;
22449 dpyinfo->mouse_face_beg_row = hpos;
22450
22451 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22452 dpyinfo->mouse_face_beg_y = 0;
22453
22454 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22455 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22456
22457 dpyinfo->mouse_face_end_x = 0;
22458 dpyinfo->mouse_face_end_y = 0;
22459
22460 dpyinfo->mouse_face_past_end = 0;
22461 dpyinfo->mouse_face_window = window;
22462
22463 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22464 charpos,
22465 0, 0, 0, &ignore,
22466 glyph->face_id, 1);
22467 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22468
22469 if (NILP (pointer))
22470 pointer = Qhand;
22471 }
22472 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22473 clear_mouse_face (dpyinfo);
22474 }
22475 define_frame_cursor1 (f, cursor, pointer);
22476 }
22477
22478
22479 /* EXPORT:
22480 Take proper action when the mouse has moved to position X, Y on
22481 frame F as regards highlighting characters that have mouse-face
22482 properties. Also de-highlighting chars where the mouse was before.
22483 X and Y can be negative or out of range. */
22484
22485 void
22486 note_mouse_highlight (f, x, y)
22487 struct frame *f;
22488 int x, y;
22489 {
22490 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22491 enum window_part part;
22492 Lisp_Object window;
22493 struct window *w;
22494 Cursor cursor = No_Cursor;
22495 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22496 struct buffer *b;
22497
22498 /* When a menu is active, don't highlight because this looks odd. */
22499 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22500 if (popup_activated ())
22501 return;
22502 #endif
22503
22504 if (NILP (Vmouse_highlight)
22505 || !f->glyphs_initialized_p)
22506 return;
22507
22508 dpyinfo->mouse_face_mouse_x = x;
22509 dpyinfo->mouse_face_mouse_y = y;
22510 dpyinfo->mouse_face_mouse_frame = f;
22511
22512 if (dpyinfo->mouse_face_defer)
22513 return;
22514
22515 if (gc_in_progress)
22516 {
22517 dpyinfo->mouse_face_deferred_gc = 1;
22518 return;
22519 }
22520
22521 /* Which window is that in? */
22522 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22523
22524 /* If we were displaying active text in another window, clear that.
22525 Also clear if we move out of text area in same window. */
22526 if (! EQ (window, dpyinfo->mouse_face_window)
22527 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22528 && !NILP (dpyinfo->mouse_face_window)))
22529 clear_mouse_face (dpyinfo);
22530
22531 /* Not on a window -> return. */
22532 if (!WINDOWP (window))
22533 return;
22534
22535 /* Reset help_echo_string. It will get recomputed below. */
22536 help_echo_string = Qnil;
22537
22538 /* Convert to window-relative pixel coordinates. */
22539 w = XWINDOW (window);
22540 frame_to_window_pixel_xy (w, &x, &y);
22541
22542 /* Handle tool-bar window differently since it doesn't display a
22543 buffer. */
22544 if (EQ (window, f->tool_bar_window))
22545 {
22546 note_tool_bar_highlight (f, x, y);
22547 return;
22548 }
22549
22550 /* Mouse is on the mode, header line or margin? */
22551 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22552 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22553 {
22554 note_mode_line_or_margin_highlight (window, x, y, part);
22555 return;
22556 }
22557
22558 if (part == ON_VERTICAL_BORDER)
22559 {
22560 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22561 help_echo_string = build_string ("drag-mouse-1: resize");
22562 }
22563 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22564 || part == ON_SCROLL_BAR)
22565 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22566 else
22567 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22568
22569 /* Are we in a window whose display is up to date?
22570 And verify the buffer's text has not changed. */
22571 b = XBUFFER (w->buffer);
22572 if (part == ON_TEXT
22573 && EQ (w->window_end_valid, w->buffer)
22574 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22575 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22576 {
22577 int hpos, vpos, pos, i, dx, dy, area;
22578 struct glyph *glyph;
22579 Lisp_Object object;
22580 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22581 Lisp_Object *overlay_vec = NULL;
22582 int noverlays;
22583 struct buffer *obuf;
22584 int obegv, ozv, same_region;
22585
22586 /* Find the glyph under X/Y. */
22587 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22588
22589 /* Look for :pointer property on image. */
22590 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22591 {
22592 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22593 if (img != NULL && IMAGEP (img->spec))
22594 {
22595 Lisp_Object image_map, hotspot;
22596 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22597 !NILP (image_map))
22598 && (hotspot = find_hot_spot (image_map,
22599 glyph->slice.x + dx,
22600 glyph->slice.y + dy),
22601 CONSP (hotspot))
22602 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22603 {
22604 Lisp_Object area_id, plist;
22605
22606 area_id = XCAR (hotspot);
22607 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22608 If so, we could look for mouse-enter, mouse-leave
22609 properties in PLIST (and do something...). */
22610 hotspot = XCDR (hotspot);
22611 if (CONSP (hotspot)
22612 && (plist = XCAR (hotspot), CONSP (plist)))
22613 {
22614 pointer = Fplist_get (plist, Qpointer);
22615 if (NILP (pointer))
22616 pointer = Qhand;
22617 help_echo_string = Fplist_get (plist, Qhelp_echo);
22618 if (!NILP (help_echo_string))
22619 {
22620 help_echo_window = window;
22621 help_echo_object = glyph->object;
22622 help_echo_pos = glyph->charpos;
22623 }
22624 }
22625 }
22626 if (NILP (pointer))
22627 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22628 }
22629 }
22630
22631 /* Clear mouse face if X/Y not over text. */
22632 if (glyph == NULL
22633 || area != TEXT_AREA
22634 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22635 {
22636 if (clear_mouse_face (dpyinfo))
22637 cursor = No_Cursor;
22638 if (NILP (pointer))
22639 {
22640 if (area != TEXT_AREA)
22641 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22642 else
22643 pointer = Vvoid_text_area_pointer;
22644 }
22645 goto set_cursor;
22646 }
22647
22648 pos = glyph->charpos;
22649 object = glyph->object;
22650 if (!STRINGP (object) && !BUFFERP (object))
22651 goto set_cursor;
22652
22653 /* If we get an out-of-range value, return now; avoid an error. */
22654 if (BUFFERP (object) && pos > BUF_Z (b))
22655 goto set_cursor;
22656
22657 /* Make the window's buffer temporarily current for
22658 overlays_at and compute_char_face. */
22659 obuf = current_buffer;
22660 current_buffer = b;
22661 obegv = BEGV;
22662 ozv = ZV;
22663 BEGV = BEG;
22664 ZV = Z;
22665
22666 /* Is this char mouse-active or does it have help-echo? */
22667 position = make_number (pos);
22668
22669 if (BUFFERP (object))
22670 {
22671 /* Put all the overlays we want in a vector in overlay_vec. */
22672 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22673 /* Sort overlays into increasing priority order. */
22674 noverlays = sort_overlays (overlay_vec, noverlays, w);
22675 }
22676 else
22677 noverlays = 0;
22678
22679 same_region = (EQ (window, dpyinfo->mouse_face_window)
22680 && vpos >= dpyinfo->mouse_face_beg_row
22681 && vpos <= dpyinfo->mouse_face_end_row
22682 && (vpos > dpyinfo->mouse_face_beg_row
22683 || hpos >= dpyinfo->mouse_face_beg_col)
22684 && (vpos < dpyinfo->mouse_face_end_row
22685 || hpos < dpyinfo->mouse_face_end_col
22686 || dpyinfo->mouse_face_past_end));
22687
22688 if (same_region)
22689 cursor = No_Cursor;
22690
22691 /* Check mouse-face highlighting. */
22692 if (! same_region
22693 /* If there exists an overlay with mouse-face overlapping
22694 the one we are currently highlighting, we have to
22695 check if we enter the overlapping overlay, and then
22696 highlight only that. */
22697 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22698 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22699 {
22700 /* Find the highest priority overlay that has a mouse-face
22701 property. */
22702 overlay = Qnil;
22703 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22704 {
22705 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22706 if (!NILP (mouse_face))
22707 overlay = overlay_vec[i];
22708 }
22709
22710 /* If we're actually highlighting the same overlay as
22711 before, there's no need to do that again. */
22712 if (!NILP (overlay)
22713 && EQ (overlay, dpyinfo->mouse_face_overlay))
22714 goto check_help_echo;
22715
22716 dpyinfo->mouse_face_overlay = overlay;
22717
22718 /* Clear the display of the old active region, if any. */
22719 if (clear_mouse_face (dpyinfo))
22720 cursor = No_Cursor;
22721
22722 /* If no overlay applies, get a text property. */
22723 if (NILP (overlay))
22724 mouse_face = Fget_text_property (position, Qmouse_face, object);
22725
22726 /* Handle the overlay case. */
22727 if (!NILP (overlay))
22728 {
22729 /* Find the range of text around this char that
22730 should be active. */
22731 Lisp_Object before, after;
22732 int ignore;
22733
22734 before = Foverlay_start (overlay);
22735 after = Foverlay_end (overlay);
22736 /* Record this as the current active region. */
22737 fast_find_position (w, XFASTINT (before),
22738 &dpyinfo->mouse_face_beg_col,
22739 &dpyinfo->mouse_face_beg_row,
22740 &dpyinfo->mouse_face_beg_x,
22741 &dpyinfo->mouse_face_beg_y, Qnil);
22742
22743 dpyinfo->mouse_face_past_end
22744 = !fast_find_position (w, XFASTINT (after),
22745 &dpyinfo->mouse_face_end_col,
22746 &dpyinfo->mouse_face_end_row,
22747 &dpyinfo->mouse_face_end_x,
22748 &dpyinfo->mouse_face_end_y, Qnil);
22749 dpyinfo->mouse_face_window = window;
22750
22751 dpyinfo->mouse_face_face_id
22752 = face_at_buffer_position (w, pos, 0, 0,
22753 &ignore, pos + 1,
22754 !dpyinfo->mouse_face_hidden);
22755
22756 /* Display it as active. */
22757 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22758 cursor = No_Cursor;
22759 }
22760 /* Handle the text property case. */
22761 else if (!NILP (mouse_face) && BUFFERP (object))
22762 {
22763 /* Find the range of text around this char that
22764 should be active. */
22765 Lisp_Object before, after, beginning, end;
22766 int ignore;
22767
22768 beginning = Fmarker_position (w->start);
22769 end = make_number (BUF_Z (XBUFFER (object))
22770 - XFASTINT (w->window_end_pos));
22771 before
22772 = Fprevious_single_property_change (make_number (pos + 1),
22773 Qmouse_face,
22774 object, beginning);
22775 after
22776 = Fnext_single_property_change (position, Qmouse_face,
22777 object, end);
22778
22779 /* Record this as the current active region. */
22780 fast_find_position (w, XFASTINT (before),
22781 &dpyinfo->mouse_face_beg_col,
22782 &dpyinfo->mouse_face_beg_row,
22783 &dpyinfo->mouse_face_beg_x,
22784 &dpyinfo->mouse_face_beg_y, Qnil);
22785 dpyinfo->mouse_face_past_end
22786 = !fast_find_position (w, XFASTINT (after),
22787 &dpyinfo->mouse_face_end_col,
22788 &dpyinfo->mouse_face_end_row,
22789 &dpyinfo->mouse_face_end_x,
22790 &dpyinfo->mouse_face_end_y, Qnil);
22791 dpyinfo->mouse_face_window = window;
22792
22793 if (BUFFERP (object))
22794 dpyinfo->mouse_face_face_id
22795 = face_at_buffer_position (w, pos, 0, 0,
22796 &ignore, pos + 1,
22797 !dpyinfo->mouse_face_hidden);
22798
22799 /* Display it as active. */
22800 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22801 cursor = No_Cursor;
22802 }
22803 else if (!NILP (mouse_face) && STRINGP (object))
22804 {
22805 Lisp_Object b, e;
22806 int ignore;
22807
22808 b = Fprevious_single_property_change (make_number (pos + 1),
22809 Qmouse_face,
22810 object, Qnil);
22811 e = Fnext_single_property_change (position, Qmouse_face,
22812 object, Qnil);
22813 if (NILP (b))
22814 b = make_number (0);
22815 if (NILP (e))
22816 e = make_number (SCHARS (object) - 1);
22817
22818 fast_find_string_pos (w, XINT (b), object,
22819 &dpyinfo->mouse_face_beg_col,
22820 &dpyinfo->mouse_face_beg_row,
22821 &dpyinfo->mouse_face_beg_x,
22822 &dpyinfo->mouse_face_beg_y, 0);
22823 fast_find_string_pos (w, XINT (e), object,
22824 &dpyinfo->mouse_face_end_col,
22825 &dpyinfo->mouse_face_end_row,
22826 &dpyinfo->mouse_face_end_x,
22827 &dpyinfo->mouse_face_end_y, 1);
22828 dpyinfo->mouse_face_past_end = 0;
22829 dpyinfo->mouse_face_window = window;
22830 dpyinfo->mouse_face_face_id
22831 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22832 glyph->face_id, 1);
22833 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22834 cursor = No_Cursor;
22835 }
22836 else if (STRINGP (object) && NILP (mouse_face))
22837 {
22838 /* A string which doesn't have mouse-face, but
22839 the text ``under'' it might have. */
22840 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22841 int start = MATRIX_ROW_START_CHARPOS (r);
22842
22843 pos = string_buffer_position (w, object, start);
22844 if (pos > 0)
22845 mouse_face = get_char_property_and_overlay (make_number (pos),
22846 Qmouse_face,
22847 w->buffer,
22848 &overlay);
22849 if (!NILP (mouse_face) && !NILP (overlay))
22850 {
22851 Lisp_Object before = Foverlay_start (overlay);
22852 Lisp_Object after = Foverlay_end (overlay);
22853 int ignore;
22854
22855 /* Note that we might not be able to find position
22856 BEFORE in the glyph matrix if the overlay is
22857 entirely covered by a `display' property. In
22858 this case, we overshoot. So let's stop in
22859 the glyph matrix before glyphs for OBJECT. */
22860 fast_find_position (w, XFASTINT (before),
22861 &dpyinfo->mouse_face_beg_col,
22862 &dpyinfo->mouse_face_beg_row,
22863 &dpyinfo->mouse_face_beg_x,
22864 &dpyinfo->mouse_face_beg_y,
22865 object);
22866
22867 dpyinfo->mouse_face_past_end
22868 = !fast_find_position (w, XFASTINT (after),
22869 &dpyinfo->mouse_face_end_col,
22870 &dpyinfo->mouse_face_end_row,
22871 &dpyinfo->mouse_face_end_x,
22872 &dpyinfo->mouse_face_end_y,
22873 Qnil);
22874 dpyinfo->mouse_face_window = window;
22875 dpyinfo->mouse_face_face_id
22876 = face_at_buffer_position (w, pos, 0, 0,
22877 &ignore, pos + 1,
22878 !dpyinfo->mouse_face_hidden);
22879
22880 /* Display it as active. */
22881 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22882 cursor = No_Cursor;
22883 }
22884 }
22885 }
22886
22887 check_help_echo:
22888
22889 /* Look for a `help-echo' property. */
22890 if (NILP (help_echo_string)) {
22891 Lisp_Object help, overlay;
22892
22893 /* Check overlays first. */
22894 help = overlay = Qnil;
22895 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22896 {
22897 overlay = overlay_vec[i];
22898 help = Foverlay_get (overlay, Qhelp_echo);
22899 }
22900
22901 if (!NILP (help))
22902 {
22903 help_echo_string = help;
22904 help_echo_window = window;
22905 help_echo_object = overlay;
22906 help_echo_pos = pos;
22907 }
22908 else
22909 {
22910 Lisp_Object object = glyph->object;
22911 int charpos = glyph->charpos;
22912
22913 /* Try text properties. */
22914 if (STRINGP (object)
22915 && charpos >= 0
22916 && charpos < SCHARS (object))
22917 {
22918 help = Fget_text_property (make_number (charpos),
22919 Qhelp_echo, object);
22920 if (NILP (help))
22921 {
22922 /* If the string itself doesn't specify a help-echo,
22923 see if the buffer text ``under'' it does. */
22924 struct glyph_row *r
22925 = MATRIX_ROW (w->current_matrix, vpos);
22926 int start = MATRIX_ROW_START_CHARPOS (r);
22927 int pos = string_buffer_position (w, object, start);
22928 if (pos > 0)
22929 {
22930 help = Fget_char_property (make_number (pos),
22931 Qhelp_echo, w->buffer);
22932 if (!NILP (help))
22933 {
22934 charpos = pos;
22935 object = w->buffer;
22936 }
22937 }
22938 }
22939 }
22940 else if (BUFFERP (object)
22941 && charpos >= BEGV
22942 && charpos < ZV)
22943 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22944 object);
22945
22946 if (!NILP (help))
22947 {
22948 help_echo_string = help;
22949 help_echo_window = window;
22950 help_echo_object = object;
22951 help_echo_pos = charpos;
22952 }
22953 }
22954 }
22955
22956 /* Look for a `pointer' property. */
22957 if (NILP (pointer))
22958 {
22959 /* Check overlays first. */
22960 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22961 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22962
22963 if (NILP (pointer))
22964 {
22965 Lisp_Object object = glyph->object;
22966 int charpos = glyph->charpos;
22967
22968 /* Try text properties. */
22969 if (STRINGP (object)
22970 && charpos >= 0
22971 && charpos < SCHARS (object))
22972 {
22973 pointer = Fget_text_property (make_number (charpos),
22974 Qpointer, object);
22975 if (NILP (pointer))
22976 {
22977 /* If the string itself doesn't specify a pointer,
22978 see if the buffer text ``under'' it does. */
22979 struct glyph_row *r
22980 = MATRIX_ROW (w->current_matrix, vpos);
22981 int start = MATRIX_ROW_START_CHARPOS (r);
22982 int pos = string_buffer_position (w, object, start);
22983 if (pos > 0)
22984 pointer = Fget_char_property (make_number (pos),
22985 Qpointer, w->buffer);
22986 }
22987 }
22988 else if (BUFFERP (object)
22989 && charpos >= BEGV
22990 && charpos < ZV)
22991 pointer = Fget_text_property (make_number (charpos),
22992 Qpointer, object);
22993 }
22994 }
22995
22996 BEGV = obegv;
22997 ZV = ozv;
22998 current_buffer = obuf;
22999 }
23000
23001 set_cursor:
23002
23003 define_frame_cursor1 (f, cursor, pointer);
23004 }
23005
23006
23007 /* EXPORT for RIF:
23008 Clear any mouse-face on window W. This function is part of the
23009 redisplay interface, and is called from try_window_id and similar
23010 functions to ensure the mouse-highlight is off. */
23011
23012 void
23013 x_clear_window_mouse_face (w)
23014 struct window *w;
23015 {
23016 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23017 Lisp_Object window;
23018
23019 BLOCK_INPUT;
23020 XSETWINDOW (window, w);
23021 if (EQ (window, dpyinfo->mouse_face_window))
23022 clear_mouse_face (dpyinfo);
23023 UNBLOCK_INPUT;
23024 }
23025
23026
23027 /* EXPORT:
23028 Just discard the mouse face information for frame F, if any.
23029 This is used when the size of F is changed. */
23030
23031 void
23032 cancel_mouse_face (f)
23033 struct frame *f;
23034 {
23035 Lisp_Object window;
23036 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23037
23038 window = dpyinfo->mouse_face_window;
23039 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23040 {
23041 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23042 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23043 dpyinfo->mouse_face_window = Qnil;
23044 }
23045 }
23046
23047
23048 #endif /* HAVE_WINDOW_SYSTEM */
23049
23050 \f
23051 /***********************************************************************
23052 Exposure Events
23053 ***********************************************************************/
23054
23055 #ifdef HAVE_WINDOW_SYSTEM
23056
23057 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23058 which intersects rectangle R. R is in window-relative coordinates. */
23059
23060 static void
23061 expose_area (w, row, r, area)
23062 struct window *w;
23063 struct glyph_row *row;
23064 XRectangle *r;
23065 enum glyph_row_area area;
23066 {
23067 struct glyph *first = row->glyphs[area];
23068 struct glyph *end = row->glyphs[area] + row->used[area];
23069 struct glyph *last;
23070 int first_x, start_x, x;
23071
23072 if (area == TEXT_AREA && row->fill_line_p)
23073 /* If row extends face to end of line write the whole line. */
23074 draw_glyphs (w, 0, row, area,
23075 0, row->used[area],
23076 DRAW_NORMAL_TEXT, 0);
23077 else
23078 {
23079 /* Set START_X to the window-relative start position for drawing glyphs of
23080 AREA. The first glyph of the text area can be partially visible.
23081 The first glyphs of other areas cannot. */
23082 start_x = window_box_left_offset (w, area);
23083 x = start_x;
23084 if (area == TEXT_AREA)
23085 x += row->x;
23086
23087 /* Find the first glyph that must be redrawn. */
23088 while (first < end
23089 && x + first->pixel_width < r->x)
23090 {
23091 x += first->pixel_width;
23092 ++first;
23093 }
23094
23095 /* Find the last one. */
23096 last = first;
23097 first_x = x;
23098 while (last < end
23099 && x < r->x + r->width)
23100 {
23101 x += last->pixel_width;
23102 ++last;
23103 }
23104
23105 /* Repaint. */
23106 if (last > first)
23107 draw_glyphs (w, first_x - start_x, row, area,
23108 first - row->glyphs[area], last - row->glyphs[area],
23109 DRAW_NORMAL_TEXT, 0);
23110 }
23111 }
23112
23113
23114 /* Redraw the parts of the glyph row ROW on window W intersecting
23115 rectangle R. R is in window-relative coordinates. Value is
23116 non-zero if mouse-face was overwritten. */
23117
23118 static int
23119 expose_line (w, row, r)
23120 struct window *w;
23121 struct glyph_row *row;
23122 XRectangle *r;
23123 {
23124 xassert (row->enabled_p);
23125
23126 if (row->mode_line_p || w->pseudo_window_p)
23127 draw_glyphs (w, 0, row, TEXT_AREA,
23128 0, row->used[TEXT_AREA],
23129 DRAW_NORMAL_TEXT, 0);
23130 else
23131 {
23132 if (row->used[LEFT_MARGIN_AREA])
23133 expose_area (w, row, r, LEFT_MARGIN_AREA);
23134 if (row->used[TEXT_AREA])
23135 expose_area (w, row, r, TEXT_AREA);
23136 if (row->used[RIGHT_MARGIN_AREA])
23137 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23138 draw_row_fringe_bitmaps (w, row);
23139 }
23140
23141 return row->mouse_face_p;
23142 }
23143
23144
23145 /* Redraw those parts of glyphs rows during expose event handling that
23146 overlap other rows. Redrawing of an exposed line writes over parts
23147 of lines overlapping that exposed line; this function fixes that.
23148
23149 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23150 row in W's current matrix that is exposed and overlaps other rows.
23151 LAST_OVERLAPPING_ROW is the last such row. */
23152
23153 static void
23154 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23155 struct window *w;
23156 struct glyph_row *first_overlapping_row;
23157 struct glyph_row *last_overlapping_row;
23158 {
23159 struct glyph_row *row;
23160
23161 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23162 if (row->overlapping_p)
23163 {
23164 xassert (row->enabled_p && !row->mode_line_p);
23165
23166 if (row->used[LEFT_MARGIN_AREA])
23167 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23168
23169 if (row->used[TEXT_AREA])
23170 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23171
23172 if (row->used[RIGHT_MARGIN_AREA])
23173 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23174 }
23175 }
23176
23177
23178 /* Return non-zero if W's cursor intersects rectangle R. */
23179
23180 static int
23181 phys_cursor_in_rect_p (w, r)
23182 struct window *w;
23183 XRectangle *r;
23184 {
23185 XRectangle cr, result;
23186 struct glyph *cursor_glyph;
23187
23188 cursor_glyph = get_phys_cursor_glyph (w);
23189 if (cursor_glyph)
23190 {
23191 /* r is relative to W's box, but w->phys_cursor.x is relative
23192 to left edge of W's TEXT area. Adjust it. */
23193 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23194 cr.y = w->phys_cursor.y;
23195 cr.width = cursor_glyph->pixel_width;
23196 cr.height = w->phys_cursor_height;
23197 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23198 I assume the effect is the same -- and this is portable. */
23199 return x_intersect_rectangles (&cr, r, &result);
23200 }
23201 else
23202 return 0;
23203 }
23204
23205
23206 /* EXPORT:
23207 Draw a vertical window border to the right of window W if W doesn't
23208 have vertical scroll bars. */
23209
23210 void
23211 x_draw_vertical_border (w)
23212 struct window *w;
23213 {
23214 /* We could do better, if we knew what type of scroll-bar the adjacent
23215 windows (on either side) have... But we don't :-(
23216 However, I think this works ok. ++KFS 2003-04-25 */
23217
23218 /* Redraw borders between horizontally adjacent windows. Don't
23219 do it for frames with vertical scroll bars because either the
23220 right scroll bar of a window, or the left scroll bar of its
23221 neighbor will suffice as a border. */
23222 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23223 return;
23224
23225 if (!WINDOW_RIGHTMOST_P (w)
23226 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23227 {
23228 int x0, x1, y0, y1;
23229
23230 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23231 y1 -= 1;
23232
23233 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23234 x1 -= 1;
23235
23236 rif->draw_vertical_window_border (w, x1, y0, y1);
23237 }
23238 else if (!WINDOW_LEFTMOST_P (w)
23239 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23240 {
23241 int x0, x1, y0, y1;
23242
23243 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23244 y1 -= 1;
23245
23246 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23247 x0 -= 1;
23248
23249 rif->draw_vertical_window_border (w, x0, y0, y1);
23250 }
23251 }
23252
23253
23254 /* Redraw the part of window W intersection rectangle FR. Pixel
23255 coordinates in FR are frame-relative. Call this function with
23256 input blocked. Value is non-zero if the exposure overwrites
23257 mouse-face. */
23258
23259 static int
23260 expose_window (w, fr)
23261 struct window *w;
23262 XRectangle *fr;
23263 {
23264 struct frame *f = XFRAME (w->frame);
23265 XRectangle wr, r;
23266 int mouse_face_overwritten_p = 0;
23267
23268 /* If window is not yet fully initialized, do nothing. This can
23269 happen when toolkit scroll bars are used and a window is split.
23270 Reconfiguring the scroll bar will generate an expose for a newly
23271 created window. */
23272 if (w->current_matrix == NULL)
23273 return 0;
23274
23275 /* When we're currently updating the window, display and current
23276 matrix usually don't agree. Arrange for a thorough display
23277 later. */
23278 if (w == updated_window)
23279 {
23280 SET_FRAME_GARBAGED (f);
23281 return 0;
23282 }
23283
23284 /* Frame-relative pixel rectangle of W. */
23285 wr.x = WINDOW_LEFT_EDGE_X (w);
23286 wr.y = WINDOW_TOP_EDGE_Y (w);
23287 wr.width = WINDOW_TOTAL_WIDTH (w);
23288 wr.height = WINDOW_TOTAL_HEIGHT (w);
23289
23290 if (x_intersect_rectangles (fr, &wr, &r))
23291 {
23292 int yb = window_text_bottom_y (w);
23293 struct glyph_row *row;
23294 int cursor_cleared_p;
23295 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23296
23297 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23298 r.x, r.y, r.width, r.height));
23299
23300 /* Convert to window coordinates. */
23301 r.x -= WINDOW_LEFT_EDGE_X (w);
23302 r.y -= WINDOW_TOP_EDGE_Y (w);
23303
23304 /* Turn off the cursor. */
23305 if (!w->pseudo_window_p
23306 && phys_cursor_in_rect_p (w, &r))
23307 {
23308 x_clear_cursor (w);
23309 cursor_cleared_p = 1;
23310 }
23311 else
23312 cursor_cleared_p = 0;
23313
23314 /* Update lines intersecting rectangle R. */
23315 first_overlapping_row = last_overlapping_row = NULL;
23316 for (row = w->current_matrix->rows;
23317 row->enabled_p;
23318 ++row)
23319 {
23320 int y0 = row->y;
23321 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23322
23323 if ((y0 >= r.y && y0 < r.y + r.height)
23324 || (y1 > r.y && y1 < r.y + r.height)
23325 || (r.y >= y0 && r.y < y1)
23326 || (r.y + r.height > y0 && r.y + r.height < y1))
23327 {
23328 /* A header line may be overlapping, but there is no need
23329 to fix overlapping areas for them. KFS 2005-02-12 */
23330 if (row->overlapping_p && !row->mode_line_p)
23331 {
23332 if (first_overlapping_row == NULL)
23333 first_overlapping_row = row;
23334 last_overlapping_row = row;
23335 }
23336
23337 if (expose_line (w, row, &r))
23338 mouse_face_overwritten_p = 1;
23339 }
23340
23341 if (y1 >= yb)
23342 break;
23343 }
23344
23345 /* Display the mode line if there is one. */
23346 if (WINDOW_WANTS_MODELINE_P (w)
23347 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23348 row->enabled_p)
23349 && row->y < r.y + r.height)
23350 {
23351 if (expose_line (w, row, &r))
23352 mouse_face_overwritten_p = 1;
23353 }
23354
23355 if (!w->pseudo_window_p)
23356 {
23357 /* Fix the display of overlapping rows. */
23358 if (first_overlapping_row)
23359 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23360
23361 /* Draw border between windows. */
23362 x_draw_vertical_border (w);
23363
23364 /* Turn the cursor on again. */
23365 if (cursor_cleared_p)
23366 update_window_cursor (w, 1);
23367 }
23368 }
23369
23370 return mouse_face_overwritten_p;
23371 }
23372
23373
23374
23375 /* Redraw (parts) of all windows in the window tree rooted at W that
23376 intersect R. R contains frame pixel coordinates. Value is
23377 non-zero if the exposure overwrites mouse-face. */
23378
23379 static int
23380 expose_window_tree (w, r)
23381 struct window *w;
23382 XRectangle *r;
23383 {
23384 struct frame *f = XFRAME (w->frame);
23385 int mouse_face_overwritten_p = 0;
23386
23387 while (w && !FRAME_GARBAGED_P (f))
23388 {
23389 if (!NILP (w->hchild))
23390 mouse_face_overwritten_p
23391 |= expose_window_tree (XWINDOW (w->hchild), r);
23392 else if (!NILP (w->vchild))
23393 mouse_face_overwritten_p
23394 |= expose_window_tree (XWINDOW (w->vchild), r);
23395 else
23396 mouse_face_overwritten_p |= expose_window (w, r);
23397
23398 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23399 }
23400
23401 return mouse_face_overwritten_p;
23402 }
23403
23404
23405 /* EXPORT:
23406 Redisplay an exposed area of frame F. X and Y are the upper-left
23407 corner of the exposed rectangle. W and H are width and height of
23408 the exposed area. All are pixel values. W or H zero means redraw
23409 the entire frame. */
23410
23411 void
23412 expose_frame (f, x, y, w, h)
23413 struct frame *f;
23414 int x, y, w, h;
23415 {
23416 XRectangle r;
23417 int mouse_face_overwritten_p = 0;
23418
23419 TRACE ((stderr, "expose_frame "));
23420
23421 /* No need to redraw if frame will be redrawn soon. */
23422 if (FRAME_GARBAGED_P (f))
23423 {
23424 TRACE ((stderr, " garbaged\n"));
23425 return;
23426 }
23427
23428 /* If basic faces haven't been realized yet, there is no point in
23429 trying to redraw anything. This can happen when we get an expose
23430 event while Emacs is starting, e.g. by moving another window. */
23431 if (FRAME_FACE_CACHE (f) == NULL
23432 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23433 {
23434 TRACE ((stderr, " no faces\n"));
23435 return;
23436 }
23437
23438 if (w == 0 || h == 0)
23439 {
23440 r.x = r.y = 0;
23441 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23442 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23443 }
23444 else
23445 {
23446 r.x = x;
23447 r.y = y;
23448 r.width = w;
23449 r.height = h;
23450 }
23451
23452 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23453 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23454
23455 if (WINDOWP (f->tool_bar_window))
23456 mouse_face_overwritten_p
23457 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23458
23459 #ifdef HAVE_X_WINDOWS
23460 #ifndef MSDOS
23461 #ifndef USE_X_TOOLKIT
23462 if (WINDOWP (f->menu_bar_window))
23463 mouse_face_overwritten_p
23464 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23465 #endif /* not USE_X_TOOLKIT */
23466 #endif
23467 #endif
23468
23469 /* Some window managers support a focus-follows-mouse style with
23470 delayed raising of frames. Imagine a partially obscured frame,
23471 and moving the mouse into partially obscured mouse-face on that
23472 frame. The visible part of the mouse-face will be highlighted,
23473 then the WM raises the obscured frame. With at least one WM, KDE
23474 2.1, Emacs is not getting any event for the raising of the frame
23475 (even tried with SubstructureRedirectMask), only Expose events.
23476 These expose events will draw text normally, i.e. not
23477 highlighted. Which means we must redo the highlight here.
23478 Subsume it under ``we love X''. --gerd 2001-08-15 */
23479 /* Included in Windows version because Windows most likely does not
23480 do the right thing if any third party tool offers
23481 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23482 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23483 {
23484 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23485 if (f == dpyinfo->mouse_face_mouse_frame)
23486 {
23487 int x = dpyinfo->mouse_face_mouse_x;
23488 int y = dpyinfo->mouse_face_mouse_y;
23489 clear_mouse_face (dpyinfo);
23490 note_mouse_highlight (f, x, y);
23491 }
23492 }
23493 }
23494
23495
23496 /* EXPORT:
23497 Determine the intersection of two rectangles R1 and R2. Return
23498 the intersection in *RESULT. Value is non-zero if RESULT is not
23499 empty. */
23500
23501 int
23502 x_intersect_rectangles (r1, r2, result)
23503 XRectangle *r1, *r2, *result;
23504 {
23505 XRectangle *left, *right;
23506 XRectangle *upper, *lower;
23507 int intersection_p = 0;
23508
23509 /* Rearrange so that R1 is the left-most rectangle. */
23510 if (r1->x < r2->x)
23511 left = r1, right = r2;
23512 else
23513 left = r2, right = r1;
23514
23515 /* X0 of the intersection is right.x0, if this is inside R1,
23516 otherwise there is no intersection. */
23517 if (right->x <= left->x + left->width)
23518 {
23519 result->x = right->x;
23520
23521 /* The right end of the intersection is the minimum of the
23522 the right ends of left and right. */
23523 result->width = (min (left->x + left->width, right->x + right->width)
23524 - result->x);
23525
23526 /* Same game for Y. */
23527 if (r1->y < r2->y)
23528 upper = r1, lower = r2;
23529 else
23530 upper = r2, lower = r1;
23531
23532 /* The upper end of the intersection is lower.y0, if this is inside
23533 of upper. Otherwise, there is no intersection. */
23534 if (lower->y <= upper->y + upper->height)
23535 {
23536 result->y = lower->y;
23537
23538 /* The lower end of the intersection is the minimum of the lower
23539 ends of upper and lower. */
23540 result->height = (min (lower->y + lower->height,
23541 upper->y + upper->height)
23542 - result->y);
23543 intersection_p = 1;
23544 }
23545 }
23546
23547 return intersection_p;
23548 }
23549
23550 #endif /* HAVE_WINDOW_SYSTEM */
23551
23552 \f
23553 /***********************************************************************
23554 Initialization
23555 ***********************************************************************/
23556
23557 void
23558 syms_of_xdisp ()
23559 {
23560 Vwith_echo_area_save_vector = Qnil;
23561 staticpro (&Vwith_echo_area_save_vector);
23562
23563 Vmessage_stack = Qnil;
23564 staticpro (&Vmessage_stack);
23565
23566 Qinhibit_redisplay = intern ("inhibit-redisplay");
23567 staticpro (&Qinhibit_redisplay);
23568
23569 message_dolog_marker1 = Fmake_marker ();
23570 staticpro (&message_dolog_marker1);
23571 message_dolog_marker2 = Fmake_marker ();
23572 staticpro (&message_dolog_marker2);
23573 message_dolog_marker3 = Fmake_marker ();
23574 staticpro (&message_dolog_marker3);
23575
23576 #if GLYPH_DEBUG
23577 defsubr (&Sdump_frame_glyph_matrix);
23578 defsubr (&Sdump_glyph_matrix);
23579 defsubr (&Sdump_glyph_row);
23580 defsubr (&Sdump_tool_bar_row);
23581 defsubr (&Strace_redisplay);
23582 defsubr (&Strace_to_stderr);
23583 #endif
23584 #ifdef HAVE_WINDOW_SYSTEM
23585 defsubr (&Stool_bar_lines_needed);
23586 defsubr (&Slookup_image_map);
23587 #endif
23588 defsubr (&Sformat_mode_line);
23589
23590 staticpro (&Qmenu_bar_update_hook);
23591 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23592
23593 staticpro (&Qoverriding_terminal_local_map);
23594 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23595
23596 staticpro (&Qoverriding_local_map);
23597 Qoverriding_local_map = intern ("overriding-local-map");
23598
23599 staticpro (&Qwindow_scroll_functions);
23600 Qwindow_scroll_functions = intern ("window-scroll-functions");
23601
23602 staticpro (&Qredisplay_end_trigger_functions);
23603 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23604
23605 staticpro (&Qinhibit_point_motion_hooks);
23606 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23607
23608 QCdata = intern (":data");
23609 staticpro (&QCdata);
23610 Qdisplay = intern ("display");
23611 staticpro (&Qdisplay);
23612 Qspace_width = intern ("space-width");
23613 staticpro (&Qspace_width);
23614 Qraise = intern ("raise");
23615 staticpro (&Qraise);
23616 Qslice = intern ("slice");
23617 staticpro (&Qslice);
23618 Qspace = intern ("space");
23619 staticpro (&Qspace);
23620 Qmargin = intern ("margin");
23621 staticpro (&Qmargin);
23622 Qpointer = intern ("pointer");
23623 staticpro (&Qpointer);
23624 Qleft_margin = intern ("left-margin");
23625 staticpro (&Qleft_margin);
23626 Qright_margin = intern ("right-margin");
23627 staticpro (&Qright_margin);
23628 Qcenter = intern ("center");
23629 staticpro (&Qcenter);
23630 Qline_height = intern ("line-height");
23631 staticpro (&Qline_height);
23632 QCalign_to = intern (":align-to");
23633 staticpro (&QCalign_to);
23634 QCrelative_width = intern (":relative-width");
23635 staticpro (&QCrelative_width);
23636 QCrelative_height = intern (":relative-height");
23637 staticpro (&QCrelative_height);
23638 QCeval = intern (":eval");
23639 staticpro (&QCeval);
23640 QCpropertize = intern (":propertize");
23641 staticpro (&QCpropertize);
23642 QCfile = intern (":file");
23643 staticpro (&QCfile);
23644 Qfontified = intern ("fontified");
23645 staticpro (&Qfontified);
23646 Qfontification_functions = intern ("fontification-functions");
23647 staticpro (&Qfontification_functions);
23648 Qtrailing_whitespace = intern ("trailing-whitespace");
23649 staticpro (&Qtrailing_whitespace);
23650 Qescape_glyph = intern ("escape-glyph");
23651 staticpro (&Qescape_glyph);
23652 Qnobreak_space = intern ("nobreak-space");
23653 staticpro (&Qnobreak_space);
23654 Qimage = intern ("image");
23655 staticpro (&Qimage);
23656 QCmap = intern (":map");
23657 staticpro (&QCmap);
23658 QCpointer = intern (":pointer");
23659 staticpro (&QCpointer);
23660 Qrect = intern ("rect");
23661 staticpro (&Qrect);
23662 Qcircle = intern ("circle");
23663 staticpro (&Qcircle);
23664 Qpoly = intern ("poly");
23665 staticpro (&Qpoly);
23666 Qmessage_truncate_lines = intern ("message-truncate-lines");
23667 staticpro (&Qmessage_truncate_lines);
23668 Qgrow_only = intern ("grow-only");
23669 staticpro (&Qgrow_only);
23670 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23671 staticpro (&Qinhibit_menubar_update);
23672 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23673 staticpro (&Qinhibit_eval_during_redisplay);
23674 Qposition = intern ("position");
23675 staticpro (&Qposition);
23676 Qbuffer_position = intern ("buffer-position");
23677 staticpro (&Qbuffer_position);
23678 Qobject = intern ("object");
23679 staticpro (&Qobject);
23680 Qbar = intern ("bar");
23681 staticpro (&Qbar);
23682 Qhbar = intern ("hbar");
23683 staticpro (&Qhbar);
23684 Qbox = intern ("box");
23685 staticpro (&Qbox);
23686 Qhollow = intern ("hollow");
23687 staticpro (&Qhollow);
23688 Qhand = intern ("hand");
23689 staticpro (&Qhand);
23690 Qarrow = intern ("arrow");
23691 staticpro (&Qarrow);
23692 Qtext = intern ("text");
23693 staticpro (&Qtext);
23694 Qrisky_local_variable = intern ("risky-local-variable");
23695 staticpro (&Qrisky_local_variable);
23696 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23697 staticpro (&Qinhibit_free_realized_faces);
23698
23699 list_of_error = Fcons (Fcons (intern ("error"),
23700 Fcons (intern ("void-variable"), Qnil)),
23701 Qnil);
23702 staticpro (&list_of_error);
23703
23704 Qlast_arrow_position = intern ("last-arrow-position");
23705 staticpro (&Qlast_arrow_position);
23706 Qlast_arrow_string = intern ("last-arrow-string");
23707 staticpro (&Qlast_arrow_string);
23708
23709 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23710 staticpro (&Qoverlay_arrow_string);
23711 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23712 staticpro (&Qoverlay_arrow_bitmap);
23713
23714 echo_buffer[0] = echo_buffer[1] = Qnil;
23715 staticpro (&echo_buffer[0]);
23716 staticpro (&echo_buffer[1]);
23717
23718 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23719 staticpro (&echo_area_buffer[0]);
23720 staticpro (&echo_area_buffer[1]);
23721
23722 Vmessages_buffer_name = build_string ("*Messages*");
23723 staticpro (&Vmessages_buffer_name);
23724
23725 mode_line_proptrans_alist = Qnil;
23726 staticpro (&mode_line_proptrans_alist);
23727 mode_line_string_list = Qnil;
23728 staticpro (&mode_line_string_list);
23729 mode_line_string_face = Qnil;
23730 staticpro (&mode_line_string_face);
23731 mode_line_string_face_prop = Qnil;
23732 staticpro (&mode_line_string_face_prop);
23733 Vmode_line_unwind_vector = Qnil;
23734 staticpro (&Vmode_line_unwind_vector);
23735
23736 help_echo_string = Qnil;
23737 staticpro (&help_echo_string);
23738 help_echo_object = Qnil;
23739 staticpro (&help_echo_object);
23740 help_echo_window = Qnil;
23741 staticpro (&help_echo_window);
23742 previous_help_echo_string = Qnil;
23743 staticpro (&previous_help_echo_string);
23744 help_echo_pos = -1;
23745
23746 #ifdef HAVE_WINDOW_SYSTEM
23747 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23748 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23749 For example, if a block cursor is over a tab, it will be drawn as
23750 wide as that tab on the display. */);
23751 x_stretch_cursor_p = 0;
23752 #endif
23753
23754 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23755 doc: /* *Non-nil means highlight trailing whitespace.
23756 The face used for trailing whitespace is `trailing-whitespace'. */);
23757 Vshow_trailing_whitespace = Qnil;
23758
23759 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23760 doc: /* *Control highlighting of nobreak space and soft hyphen.
23761 A value of t means highlight the character itself (for nobreak space,
23762 use face `nobreak-space').
23763 A value of nil means no highlighting.
23764 Other values mean display the escape glyph followed by an ordinary
23765 space or ordinary hyphen. */);
23766 Vnobreak_char_display = Qt;
23767
23768 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23769 doc: /* *The pointer shape to show in void text areas.
23770 A value of nil means to show the text pointer. Other options are `arrow',
23771 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23772 Vvoid_text_area_pointer = Qarrow;
23773
23774 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23775 doc: /* Non-nil means don't actually do any redisplay.
23776 This is used for internal purposes. */);
23777 Vinhibit_redisplay = Qnil;
23778
23779 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23780 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23781 Vglobal_mode_string = Qnil;
23782
23783 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23784 doc: /* Marker for where to display an arrow on top of the buffer text.
23785 This must be the beginning of a line in order to work.
23786 See also `overlay-arrow-string'. */);
23787 Voverlay_arrow_position = Qnil;
23788
23789 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23790 doc: /* String to display as an arrow in non-window frames.
23791 See also `overlay-arrow-position'. */);
23792 Voverlay_arrow_string = build_string ("=>");
23793
23794 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23795 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23796 The symbols on this list are examined during redisplay to determine
23797 where to display overlay arrows. */);
23798 Voverlay_arrow_variable_list
23799 = Fcons (intern ("overlay-arrow-position"), Qnil);
23800
23801 DEFVAR_INT ("scroll-step", &scroll_step,
23802 doc: /* *The number of lines to try scrolling a window by when point moves out.
23803 If that fails to bring point back on frame, point is centered instead.
23804 If this is zero, point is always centered after it moves off frame.
23805 If you want scrolling to always be a line at a time, you should set
23806 `scroll-conservatively' to a large value rather than set this to 1. */);
23807
23808 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23809 doc: /* *Scroll up to this many lines, to bring point back on screen.
23810 A value of zero means to scroll the text to center point vertically
23811 in the window. */);
23812 scroll_conservatively = 0;
23813
23814 DEFVAR_INT ("scroll-margin", &scroll_margin,
23815 doc: /* *Number of lines of margin at the top and bottom of a window.
23816 Recenter the window whenever point gets within this many lines
23817 of the top or bottom of the window. */);
23818 scroll_margin = 0;
23819
23820 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23821 doc: /* Pixels per inch value for non-window system displays.
23822 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23823 Vdisplay_pixels_per_inch = make_float (72.0);
23824
23825 #if GLYPH_DEBUG
23826 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23827 #endif
23828
23829 DEFVAR_BOOL ("truncate-partial-width-windows",
23830 &truncate_partial_width_windows,
23831 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23832 truncate_partial_width_windows = 1;
23833
23834 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23835 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23836 Any other value means to use the appropriate face, `mode-line',
23837 `header-line', or `menu' respectively. */);
23838 mode_line_inverse_video = 1;
23839
23840 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23841 doc: /* *Maximum buffer size for which line number should be displayed.
23842 If the buffer is bigger than this, the line number does not appear
23843 in the mode line. A value of nil means no limit. */);
23844 Vline_number_display_limit = Qnil;
23845
23846 DEFVAR_INT ("line-number-display-limit-width",
23847 &line_number_display_limit_width,
23848 doc: /* *Maximum line width (in characters) for line number display.
23849 If the average length of the lines near point is bigger than this, then the
23850 line number may be omitted from the mode line. */);
23851 line_number_display_limit_width = 200;
23852
23853 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23854 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23855 highlight_nonselected_windows = 0;
23856
23857 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23858 doc: /* Non-nil if more than one frame is visible on this display.
23859 Minibuffer-only frames don't count, but iconified frames do.
23860 This variable is not guaranteed to be accurate except while processing
23861 `frame-title-format' and `icon-title-format'. */);
23862
23863 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23864 doc: /* Template for displaying the title bar of visible frames.
23865 \(Assuming the window manager supports this feature.)
23866 This variable has the same structure as `mode-line-format' (which see),
23867 and is used only on frames for which no explicit name has been set
23868 \(see `modify-frame-parameters'). */);
23869
23870 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23871 doc: /* Template for displaying the title bar of an iconified frame.
23872 \(Assuming the window manager supports this feature.)
23873 This variable has the same structure as `mode-line-format' (which see),
23874 and is used only on frames for which no explicit name has been set
23875 \(see `modify-frame-parameters'). */);
23876 Vicon_title_format
23877 = Vframe_title_format
23878 = Fcons (intern ("multiple-frames"),
23879 Fcons (build_string ("%b"),
23880 Fcons (Fcons (empty_string,
23881 Fcons (intern ("invocation-name"),
23882 Fcons (build_string ("@"),
23883 Fcons (intern ("system-name"),
23884 Qnil)))),
23885 Qnil)));
23886
23887 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23888 doc: /* Maximum number of lines to keep in the message log buffer.
23889 If nil, disable message logging. If t, log messages but don't truncate
23890 the buffer when it becomes large. */);
23891 Vmessage_log_max = make_number (50);
23892
23893 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23894 doc: /* Functions called before redisplay, if window sizes have changed.
23895 The value should be a list of functions that take one argument.
23896 Just before redisplay, for each frame, if any of its windows have changed
23897 size since the last redisplay, or have been split or deleted,
23898 all the functions in the list are called, with the frame as argument. */);
23899 Vwindow_size_change_functions = Qnil;
23900
23901 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23902 doc: /* List of functions to call before redisplaying a window with scrolling.
23903 Each function is called with two arguments, the window
23904 and its new display-start position. Note that the value of `window-end'
23905 is not valid when these functions are called. */);
23906 Vwindow_scroll_functions = Qnil;
23907
23908 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23909 doc: /* Functions called when redisplay of a window reaches the end trigger.
23910 Each function is called with two arguments, the window and the end trigger value.
23911 See `set-window-redisplay-end-trigger'. */);
23912 Vredisplay_end_trigger_functions = Qnil;
23913
23914 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23915 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23916 mouse_autoselect_window = 0;
23917
23918 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23919 doc: /* *Non-nil means automatically resize tool-bars.
23920 This increases a tool-bar's height if not all tool-bar items are visible.
23921 It decreases a tool-bar's height when it would display blank lines
23922 otherwise. */);
23923 auto_resize_tool_bars_p = 1;
23924
23925 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23926 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23927 auto_raise_tool_bar_buttons_p = 1;
23928
23929 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23930 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23931 make_cursor_line_fully_visible_p = 1;
23932
23933 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
23934 doc: /* *Border below tool-bar in pixels.
23935 If an integer, use it as the height of the border.
23936 If it is one of `internal-border-width' or `border-width', use the
23937 value of the corresponding frame parameter.
23938 Otherwise, no border is added below the tool-bar. */);
23939 Vtool_bar_border = Qinternal_border_width;
23940
23941 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23942 doc: /* *Margin around tool-bar buttons in pixels.
23943 If an integer, use that for both horizontal and vertical margins.
23944 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23945 HORZ specifying the horizontal margin, and VERT specifying the
23946 vertical margin. */);
23947 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23948
23949 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23950 doc: /* *Relief thickness of tool-bar buttons. */);
23951 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23952
23953 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23954 doc: /* List of functions to call to fontify regions of text.
23955 Each function is called with one argument POS. Functions must
23956 fontify a region starting at POS in the current buffer, and give
23957 fontified regions the property `fontified'. */);
23958 Vfontification_functions = Qnil;
23959 Fmake_variable_buffer_local (Qfontification_functions);
23960
23961 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23962 &unibyte_display_via_language_environment,
23963 doc: /* *Non-nil means display unibyte text according to language environment.
23964 Specifically this means that unibyte non-ASCII characters
23965 are displayed by converting them to the equivalent multibyte characters
23966 according to the current language environment. As a result, they are
23967 displayed according to the current fontset. */);
23968 unibyte_display_via_language_environment = 0;
23969
23970 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23971 doc: /* *Maximum height for resizing mini-windows.
23972 If a float, it specifies a fraction of the mini-window frame's height.
23973 If an integer, it specifies a number of lines. */);
23974 Vmax_mini_window_height = make_float (0.25);
23975
23976 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23977 doc: /* *How to resize mini-windows.
23978 A value of nil means don't automatically resize mini-windows.
23979 A value of t means resize them to fit the text displayed in them.
23980 A value of `grow-only', the default, means let mini-windows grow
23981 only, until their display becomes empty, at which point the windows
23982 go back to their normal size. */);
23983 Vresize_mini_windows = Qgrow_only;
23984
23985 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23986 doc: /* Alist specifying how to blink the cursor off.
23987 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23988 `cursor-type' frame-parameter or variable equals ON-STATE,
23989 comparing using `equal', Emacs uses OFF-STATE to specify
23990 how to blink it off. ON-STATE and OFF-STATE are values for
23991 the `cursor-type' frame parameter.
23992
23993 If a frame's ON-STATE has no entry in this list,
23994 the frame's other specifications determine how to blink the cursor off. */);
23995 Vblink_cursor_alist = Qnil;
23996
23997 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23998 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23999 automatic_hscrolling_p = 1;
24000
24001 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24002 doc: /* *How many columns away from the window edge point is allowed to get
24003 before automatic hscrolling will horizontally scroll the window. */);
24004 hscroll_margin = 5;
24005
24006 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24007 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24008 When point is less than `hscroll-margin' columns from the window
24009 edge, automatic hscrolling will scroll the window by the amount of columns
24010 determined by this variable. If its value is a positive integer, scroll that
24011 many columns. If it's a positive floating-point number, it specifies the
24012 fraction of the window's width to scroll. If it's nil or zero, point will be
24013 centered horizontally after the scroll. Any other value, including negative
24014 numbers, are treated as if the value were zero.
24015
24016 Automatic hscrolling always moves point outside the scroll margin, so if
24017 point was more than scroll step columns inside the margin, the window will
24018 scroll more than the value given by the scroll step.
24019
24020 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24021 and `scroll-right' overrides this variable's effect. */);
24022 Vhscroll_step = make_number (0);
24023
24024 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24025 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24026 Bind this around calls to `message' to let it take effect. */);
24027 message_truncate_lines = 0;
24028
24029 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24030 doc: /* Normal hook run to update the menu bar definitions.
24031 Redisplay runs this hook before it redisplays the menu bar.
24032 This is used to update submenus such as Buffers,
24033 whose contents depend on various data. */);
24034 Vmenu_bar_update_hook = Qnil;
24035
24036 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24037 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24038 inhibit_menubar_update = 0;
24039
24040 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24041 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24042 inhibit_eval_during_redisplay = 0;
24043
24044 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24045 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24046 inhibit_free_realized_faces = 0;
24047
24048 #if GLYPH_DEBUG
24049 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24050 doc: /* Inhibit try_window_id display optimization. */);
24051 inhibit_try_window_id = 0;
24052
24053 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24054 doc: /* Inhibit try_window_reusing display optimization. */);
24055 inhibit_try_window_reusing = 0;
24056
24057 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24058 doc: /* Inhibit try_cursor_movement display optimization. */);
24059 inhibit_try_cursor_movement = 0;
24060 #endif /* GLYPH_DEBUG */
24061 }
24062
24063
24064 /* Initialize this module when Emacs starts. */
24065
24066 void
24067 init_xdisp ()
24068 {
24069 Lisp_Object root_window;
24070 struct window *mini_w;
24071
24072 current_header_line_height = current_mode_line_height = -1;
24073
24074 CHARPOS (this_line_start_pos) = 0;
24075
24076 mini_w = XWINDOW (minibuf_window);
24077 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24078
24079 if (!noninteractive)
24080 {
24081 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24082 int i;
24083
24084 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24085 set_window_height (root_window,
24086 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24087 0);
24088 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24089 set_window_height (minibuf_window, 1, 0);
24090
24091 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24092 mini_w->total_cols = make_number (FRAME_COLS (f));
24093
24094 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24095 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24096 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24097
24098 /* The default ellipsis glyphs `...'. */
24099 for (i = 0; i < 3; ++i)
24100 default_invis_vector[i] = make_number ('.');
24101 }
24102
24103 {
24104 /* Allocate the buffer for frame titles.
24105 Also used for `format-mode-line'. */
24106 int size = 100;
24107 mode_line_noprop_buf = (char *) xmalloc (size);
24108 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24109 mode_line_noprop_ptr = mode_line_noprop_buf;
24110 mode_line_target = MODE_LINE_DISPLAY;
24111 }
24112
24113 help_echo_showing_p = 0;
24114 }
24115
24116
24117 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24118 (do not change this comment) */