(display_tool_bar_line): Restore entire tool-bar geometry when
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
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 void next_overlay_string P_ ((struct it *));
920 static void reseat P_ ((struct it *, struct text_pos, int));
921 static void reseat_1 P_ ((struct it *, struct text_pos, int));
922 static void back_to_previous_visible_line_start P_ ((struct it *));
923 void reseat_at_previous_visible_line_start P_ ((struct it *));
924 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
925 static int next_element_from_ellipsis P_ ((struct it *));
926 static int next_element_from_display_vector P_ ((struct it *));
927 static int next_element_from_string P_ ((struct it *));
928 static int next_element_from_c_string P_ ((struct it *));
929 static int next_element_from_buffer P_ ((struct it *));
930 static int next_element_from_composition P_ ((struct it *));
931 static int next_element_from_image P_ ((struct it *));
932 static int next_element_from_stretch P_ ((struct it *));
933 static void load_overlay_strings P_ ((struct it *, int));
934 static int init_from_display_pos P_ ((struct it *, struct window *,
935 struct display_pos *));
936 static void reseat_to_string P_ ((struct it *, unsigned char *,
937 Lisp_Object, int, int, int, int));
938 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
939 int, int, int));
940 void move_it_vertically_backward P_ ((struct it *, int));
941 static void init_to_row_start P_ ((struct it *, struct window *,
942 struct glyph_row *));
943 static int init_to_row_end P_ ((struct it *, struct window *,
944 struct glyph_row *));
945 static void back_to_previous_line_start P_ ((struct it *));
946 static int forward_to_next_line_start P_ ((struct it *, int *));
947 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
948 Lisp_Object, int));
949 static struct text_pos string_pos P_ ((int, Lisp_Object));
950 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
951 static int number_of_chars P_ ((unsigned char *, int));
952 static void compute_stop_pos P_ ((struct it *));
953 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
954 Lisp_Object));
955 static int face_before_or_after_it_pos P_ ((struct it *, int));
956 static int next_overlay_change P_ ((int));
957 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
958 Lisp_Object, struct text_pos *,
959 int));
960 static int underlying_face_id P_ ((struct it *));
961 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
962 struct window *));
963
964 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
965 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
966
967 #ifdef HAVE_WINDOW_SYSTEM
968
969 static void update_tool_bar P_ ((struct frame *, int));
970 static void build_desired_tool_bar_string P_ ((struct frame *f));
971 static int redisplay_tool_bar P_ ((struct frame *));
972 static void display_tool_bar_line P_ ((struct it *, int));
973 static void notice_overwritten_cursor P_ ((struct window *,
974 enum glyph_row_area,
975 int, int, int, int));
976
977
978
979 #endif /* HAVE_WINDOW_SYSTEM */
980
981 \f
982 /***********************************************************************
983 Window display dimensions
984 ***********************************************************************/
985
986 /* Return the bottom boundary y-position for text lines in window W.
987 This is the first y position at which a line cannot start.
988 It is relative to the top of the window.
989
990 This is the height of W minus the height of a mode line, if any. */
991
992 INLINE int
993 window_text_bottom_y (w)
994 struct window *w;
995 {
996 int height = WINDOW_TOTAL_HEIGHT (w);
997
998 if (WINDOW_WANTS_MODELINE_P (w))
999 height -= CURRENT_MODE_LINE_HEIGHT (w);
1000 return height;
1001 }
1002
1003 /* Return the pixel width of display area AREA of window W. AREA < 0
1004 means return the total width of W, not including fringes to
1005 the left and right of the window. */
1006
1007 INLINE int
1008 window_box_width (w, area)
1009 struct window *w;
1010 int area;
1011 {
1012 int cols = XFASTINT (w->total_cols);
1013 int pixels = 0;
1014
1015 if (!w->pseudo_window_p)
1016 {
1017 cols -= WINDOW_SCROLL_BAR_COLS (w);
1018
1019 if (area == TEXT_AREA)
1020 {
1021 if (INTEGERP (w->left_margin_cols))
1022 cols -= XFASTINT (w->left_margin_cols);
1023 if (INTEGERP (w->right_margin_cols))
1024 cols -= XFASTINT (w->right_margin_cols);
1025 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1026 }
1027 else if (area == LEFT_MARGIN_AREA)
1028 {
1029 cols = (INTEGERP (w->left_margin_cols)
1030 ? XFASTINT (w->left_margin_cols) : 0);
1031 pixels = 0;
1032 }
1033 else if (area == RIGHT_MARGIN_AREA)
1034 {
1035 cols = (INTEGERP (w->right_margin_cols)
1036 ? XFASTINT (w->right_margin_cols) : 0);
1037 pixels = 0;
1038 }
1039 }
1040
1041 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1042 }
1043
1044
1045 /* Return the pixel height of the display area of window W, not
1046 including mode lines of W, if any. */
1047
1048 INLINE int
1049 window_box_height (w)
1050 struct window *w;
1051 {
1052 struct frame *f = XFRAME (w->frame);
1053 int height = WINDOW_TOTAL_HEIGHT (w);
1054
1055 xassert (height >= 0);
1056
1057 /* Note: the code below that determines the mode-line/header-line
1058 height is essentially the same as that contained in the macro
1059 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1060 the appropriate glyph row has its `mode_line_p' flag set,
1061 and if it doesn't, uses estimate_mode_line_height instead. */
1062
1063 if (WINDOW_WANTS_MODELINE_P (w))
1064 {
1065 struct glyph_row *ml_row
1066 = (w->current_matrix && w->current_matrix->rows
1067 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1068 : 0);
1069 if (ml_row && ml_row->mode_line_p)
1070 height -= ml_row->height;
1071 else
1072 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1073 }
1074
1075 if (WINDOW_WANTS_HEADER_LINE_P (w))
1076 {
1077 struct glyph_row *hl_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (hl_row && hl_row->mode_line_p)
1082 height -= hl_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1085 }
1086
1087 /* With a very small font and a mode-line that's taller than
1088 default, we might end up with a negative height. */
1089 return max (0, height);
1090 }
1091
1092 /* Return the window-relative coordinate of the left edge of display
1093 area AREA of window W. AREA < 0 means return the left edge of the
1094 whole window, to the right of the left fringe of W. */
1095
1096 INLINE int
1097 window_box_left_offset (w, area)
1098 struct window *w;
1099 int area;
1100 {
1101 int x;
1102
1103 if (w->pseudo_window_p)
1104 return 0;
1105
1106 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1107
1108 if (area == TEXT_AREA)
1109 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1110 + window_box_width (w, LEFT_MARGIN_AREA));
1111 else if (area == RIGHT_MARGIN_AREA)
1112 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1113 + window_box_width (w, LEFT_MARGIN_AREA)
1114 + window_box_width (w, TEXT_AREA)
1115 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1116 ? 0
1117 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1118 else if (area == LEFT_MARGIN_AREA
1119 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1120 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1121
1122 return x;
1123 }
1124
1125
1126 /* Return the window-relative coordinate of the right edge of display
1127 area AREA of window W. AREA < 0 means return the left edge of the
1128 whole window, to the left of the right fringe of W. */
1129
1130 INLINE int
1131 window_box_right_offset (w, area)
1132 struct window *w;
1133 int area;
1134 {
1135 return window_box_left_offset (w, area) + window_box_width (w, area);
1136 }
1137
1138 /* Return the frame-relative coordinate of the left edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the right of the left fringe of W. */
1141
1142 INLINE int
1143 window_box_left (w, area)
1144 struct window *w;
1145 int area;
1146 {
1147 struct frame *f = XFRAME (w->frame);
1148 int x;
1149
1150 if (w->pseudo_window_p)
1151 return FRAME_INTERNAL_BORDER_WIDTH (f);
1152
1153 x = (WINDOW_LEFT_EDGE_X (w)
1154 + window_box_left_offset (w, area));
1155
1156 return x;
1157 }
1158
1159
1160 /* Return the frame-relative coordinate of the right edge of display
1161 area AREA of window W. AREA < 0 means return the left edge of the
1162 whole window, to the left of the right fringe of W. */
1163
1164 INLINE int
1165 window_box_right (w, area)
1166 struct window *w;
1167 int area;
1168 {
1169 return window_box_left (w, area) + window_box_width (w, area);
1170 }
1171
1172 /* Get the bounding box of the display area AREA of window W, without
1173 mode lines, in frame-relative coordinates. AREA < 0 means the
1174 whole window, not including the left and right fringes of
1175 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1176 coordinates of the upper-left corner of the box. Return in
1177 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1178
1179 INLINE void
1180 window_box (w, area, box_x, box_y, box_width, box_height)
1181 struct window *w;
1182 int area;
1183 int *box_x, *box_y, *box_width, *box_height;
1184 {
1185 if (box_width)
1186 *box_width = window_box_width (w, area);
1187 if (box_height)
1188 *box_height = window_box_height (w);
1189 if (box_x)
1190 *box_x = window_box_left (w, area);
1191 if (box_y)
1192 {
1193 *box_y = WINDOW_TOP_EDGE_Y (w);
1194 if (WINDOW_WANTS_HEADER_LINE_P (w))
1195 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1196 }
1197 }
1198
1199
1200 /* Get the bounding box of the display area AREA of window W, without
1201 mode lines. AREA < 0 means the whole window, not including the
1202 left and right fringe of the window. Return in *TOP_LEFT_X
1203 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1204 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1205 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1206 box. */
1207
1208 INLINE void
1209 window_box_edges (w, area, top_left_x, top_left_y,
1210 bottom_right_x, bottom_right_y)
1211 struct window *w;
1212 int area;
1213 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1214 {
1215 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1216 bottom_right_y);
1217 *bottom_right_x += *top_left_x;
1218 *bottom_right_y += *top_left_y;
1219 }
1220
1221
1222 \f
1223 /***********************************************************************
1224 Utilities
1225 ***********************************************************************/
1226
1227 /* Return the bottom y-position of the line the iterator IT is in.
1228 This can modify IT's settings. */
1229
1230 int
1231 line_bottom_y (it)
1232 struct it *it;
1233 {
1234 int line_height = it->max_ascent + it->max_descent;
1235 int line_top_y = it->current_y;
1236
1237 if (line_height == 0)
1238 {
1239 if (last_height)
1240 line_height = last_height;
1241 else if (IT_CHARPOS (*it) < ZV)
1242 {
1243 move_it_by_lines (it, 1, 1);
1244 line_height = (it->max_ascent || it->max_descent
1245 ? it->max_ascent + it->max_descent
1246 : last_height);
1247 }
1248 else
1249 {
1250 struct glyph_row *row = it->glyph_row;
1251
1252 /* Use the default character height. */
1253 it->glyph_row = NULL;
1254 it->what = IT_CHARACTER;
1255 it->c = ' ';
1256 it->len = 1;
1257 PRODUCE_GLYPHS (it);
1258 line_height = it->ascent + it->descent;
1259 it->glyph_row = row;
1260 }
1261 }
1262
1263 return line_top_y + line_height;
1264 }
1265
1266
1267 /* Return 1 if position CHARPOS is visible in window W.
1268 If visible, set *X and *Y to pixel coordinates of top left corner.
1269 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1270 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1271 and header-lines heights. */
1272
1273 int
1274 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1275 struct window *w;
1276 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1277 {
1278 struct it it;
1279 struct text_pos top;
1280 int visible_p = 0;
1281 struct buffer *old_buffer = NULL;
1282
1283 if (noninteractive)
1284 return visible_p;
1285
1286 if (XBUFFER (w->buffer) != current_buffer)
1287 {
1288 old_buffer = current_buffer;
1289 set_buffer_internal_1 (XBUFFER (w->buffer));
1290 }
1291
1292 SET_TEXT_POS_FROM_MARKER (top, w->start);
1293
1294 /* Compute exact mode line heights, if requested. */
1295 if (exact_mode_line_heights_p)
1296 {
1297 if (WINDOW_WANTS_MODELINE_P (w))
1298 current_mode_line_height
1299 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1300 current_buffer->mode_line_format);
1301
1302 if (WINDOW_WANTS_HEADER_LINE_P (w))
1303 current_header_line_height
1304 = display_mode_line (w, HEADER_LINE_FACE_ID,
1305 current_buffer->header_line_format);
1306 }
1307
1308 start_display (&it, w, top);
1309 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1310 MOVE_TO_POS | MOVE_TO_Y);
1311
1312 /* Note that we may overshoot because of invisible text. */
1313 if (IT_CHARPOS (it) >= charpos)
1314 {
1315 int top_x = it.current_x;
1316 int top_y = it.current_y;
1317 int bottom_y = (last_height = 0, line_bottom_y (&it));
1318 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1319
1320 if (top_y < window_top_y)
1321 visible_p = bottom_y > window_top_y;
1322 else if (top_y < it.last_visible_y)
1323 visible_p = 1;
1324 if (visible_p)
1325 {
1326 *x = top_x;
1327 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1328 *rtop = max (0, window_top_y - top_y);
1329 *rbot = max (0, bottom_y - it.last_visible_y);
1330 }
1331 }
1332 else
1333 {
1334 struct it it2;
1335
1336 it2 = it;
1337 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1338 move_it_by_lines (&it, 1, 0);
1339 if (charpos < IT_CHARPOS (it))
1340 {
1341 visible_p = 1;
1342 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1343 *x = it2.current_x;
1344 *y = it2.current_y + it2.max_ascent - it2.ascent;
1345 *rtop = max (0, -it2.current_y);
1346 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1347 - it.last_visible_y));
1348 }
1349 }
1350
1351 if (old_buffer)
1352 set_buffer_internal_1 (old_buffer);
1353
1354 current_header_line_height = current_mode_line_height = -1;
1355
1356 if (visible_p && XFASTINT (w->hscroll) > 0)
1357 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1358
1359 return visible_p;
1360 }
1361
1362
1363 /* Return the next character from STR which is MAXLEN bytes long.
1364 Return in *LEN the length of the character. This is like
1365 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1366 we find one, we return a `?', but with the length of the invalid
1367 character. */
1368
1369 static INLINE int
1370 string_char_and_length (str, maxlen, len)
1371 const unsigned char *str;
1372 int maxlen, *len;
1373 {
1374 int c;
1375
1376 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1377 if (!CHAR_VALID_P (c, 1))
1378 /* We may not change the length here because other places in Emacs
1379 don't use this function, i.e. they silently accept invalid
1380 characters. */
1381 c = '?';
1382
1383 return c;
1384 }
1385
1386
1387
1388 /* Given a position POS containing a valid character and byte position
1389 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1390
1391 static struct text_pos
1392 string_pos_nchars_ahead (pos, string, nchars)
1393 struct text_pos pos;
1394 Lisp_Object string;
1395 int nchars;
1396 {
1397 xassert (STRINGP (string) && nchars >= 0);
1398
1399 if (STRING_MULTIBYTE (string))
1400 {
1401 int rest = SBYTES (string) - BYTEPOS (pos);
1402 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1403 int len;
1404
1405 while (nchars--)
1406 {
1407 string_char_and_length (p, rest, &len);
1408 p += len, rest -= len;
1409 xassert (rest >= 0);
1410 CHARPOS (pos) += 1;
1411 BYTEPOS (pos) += len;
1412 }
1413 }
1414 else
1415 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1416
1417 return pos;
1418 }
1419
1420
1421 /* Value is the text position, i.e. character and byte position,
1422 for character position CHARPOS in STRING. */
1423
1424 static INLINE struct text_pos
1425 string_pos (charpos, string)
1426 int charpos;
1427 Lisp_Object string;
1428 {
1429 struct text_pos pos;
1430 xassert (STRINGP (string));
1431 xassert (charpos >= 0);
1432 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1433 return pos;
1434 }
1435
1436
1437 /* Value is a text position, i.e. character and byte position, for
1438 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1439 means recognize multibyte characters. */
1440
1441 static struct text_pos
1442 c_string_pos (charpos, s, multibyte_p)
1443 int charpos;
1444 unsigned char *s;
1445 int multibyte_p;
1446 {
1447 struct text_pos pos;
1448
1449 xassert (s != NULL);
1450 xassert (charpos >= 0);
1451
1452 if (multibyte_p)
1453 {
1454 int rest = strlen (s), len;
1455
1456 SET_TEXT_POS (pos, 0, 0);
1457 while (charpos--)
1458 {
1459 string_char_and_length (s, rest, &len);
1460 s += len, rest -= len;
1461 xassert (rest >= 0);
1462 CHARPOS (pos) += 1;
1463 BYTEPOS (pos) += len;
1464 }
1465 }
1466 else
1467 SET_TEXT_POS (pos, charpos, charpos);
1468
1469 return pos;
1470 }
1471
1472
1473 /* Value is the number of characters in C string S. MULTIBYTE_P
1474 non-zero means recognize multibyte characters. */
1475
1476 static int
1477 number_of_chars (s, multibyte_p)
1478 unsigned char *s;
1479 int multibyte_p;
1480 {
1481 int nchars;
1482
1483 if (multibyte_p)
1484 {
1485 int rest = strlen (s), len;
1486 unsigned char *p = (unsigned char *) s;
1487
1488 for (nchars = 0; rest > 0; ++nchars)
1489 {
1490 string_char_and_length (p, rest, &len);
1491 rest -= len, p += len;
1492 }
1493 }
1494 else
1495 nchars = strlen (s);
1496
1497 return nchars;
1498 }
1499
1500
1501 /* Compute byte position NEWPOS->bytepos corresponding to
1502 NEWPOS->charpos. POS is a known position in string STRING.
1503 NEWPOS->charpos must be >= POS.charpos. */
1504
1505 static void
1506 compute_string_pos (newpos, pos, string)
1507 struct text_pos *newpos, pos;
1508 Lisp_Object string;
1509 {
1510 xassert (STRINGP (string));
1511 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1512
1513 if (STRING_MULTIBYTE (string))
1514 *newpos = string_pos_nchars_ahead (pos, string,
1515 CHARPOS (*newpos) - CHARPOS (pos));
1516 else
1517 BYTEPOS (*newpos) = CHARPOS (*newpos);
1518 }
1519
1520 /* EXPORT:
1521 Return an estimation of the pixel height of mode or top lines on
1522 frame F. FACE_ID specifies what line's height to estimate. */
1523
1524 int
1525 estimate_mode_line_height (f, face_id)
1526 struct frame *f;
1527 enum face_id face_id;
1528 {
1529 #ifdef HAVE_WINDOW_SYSTEM
1530 if (FRAME_WINDOW_P (f))
1531 {
1532 int height = FONT_HEIGHT (FRAME_FONT (f));
1533
1534 /* This function is called so early when Emacs starts that the face
1535 cache and mode line face are not yet initialized. */
1536 if (FRAME_FACE_CACHE (f))
1537 {
1538 struct face *face = FACE_FROM_ID (f, face_id);
1539 if (face)
1540 {
1541 if (face->font)
1542 height = FONT_HEIGHT (face->font);
1543 if (face->box_line_width > 0)
1544 height += 2 * face->box_line_width;
1545 }
1546 }
1547
1548 return height;
1549 }
1550 #endif
1551
1552 return 1;
1553 }
1554
1555 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1556 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1557 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1558 not force the value into range. */
1559
1560 void
1561 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1562 FRAME_PTR f;
1563 register int pix_x, pix_y;
1564 int *x, *y;
1565 NativeRectangle *bounds;
1566 int noclip;
1567 {
1568
1569 #ifdef HAVE_WINDOW_SYSTEM
1570 if (FRAME_WINDOW_P (f))
1571 {
1572 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1573 even for negative values. */
1574 if (pix_x < 0)
1575 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1576 if (pix_y < 0)
1577 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1578
1579 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1580 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1581
1582 if (bounds)
1583 STORE_NATIVE_RECT (*bounds,
1584 FRAME_COL_TO_PIXEL_X (f, pix_x),
1585 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1586 FRAME_COLUMN_WIDTH (f) - 1,
1587 FRAME_LINE_HEIGHT (f) - 1);
1588
1589 if (!noclip)
1590 {
1591 if (pix_x < 0)
1592 pix_x = 0;
1593 else if (pix_x > FRAME_TOTAL_COLS (f))
1594 pix_x = FRAME_TOTAL_COLS (f);
1595
1596 if (pix_y < 0)
1597 pix_y = 0;
1598 else if (pix_y > FRAME_LINES (f))
1599 pix_y = FRAME_LINES (f);
1600 }
1601 }
1602 #endif
1603
1604 *x = pix_x;
1605 *y = pix_y;
1606 }
1607
1608
1609 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1610 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1611 can't tell the positions because W's display is not up to date,
1612 return 0. */
1613
1614 int
1615 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1616 struct window *w;
1617 int hpos, vpos;
1618 int *frame_x, *frame_y;
1619 {
1620 #ifdef HAVE_WINDOW_SYSTEM
1621 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1622 {
1623 int success_p;
1624
1625 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1626 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1627
1628 if (display_completed)
1629 {
1630 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1631 struct glyph *glyph = row->glyphs[TEXT_AREA];
1632 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1633
1634 hpos = row->x;
1635 vpos = row->y;
1636 while (glyph < end)
1637 {
1638 hpos += glyph->pixel_width;
1639 ++glyph;
1640 }
1641
1642 /* If first glyph is partially visible, its first visible position is still 0. */
1643 if (hpos < 0)
1644 hpos = 0;
1645
1646 success_p = 1;
1647 }
1648 else
1649 {
1650 hpos = vpos = 0;
1651 success_p = 0;
1652 }
1653
1654 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1655 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1656 return success_p;
1657 }
1658 #endif
1659
1660 *frame_x = hpos;
1661 *frame_y = vpos;
1662 return 1;
1663 }
1664
1665
1666 #ifdef HAVE_WINDOW_SYSTEM
1667
1668 /* Find the glyph under window-relative coordinates X/Y in window W.
1669 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1670 strings. Return in *HPOS and *VPOS the row and column number of
1671 the glyph found. Return in *AREA the glyph area containing X.
1672 Value is a pointer to the glyph found or null if X/Y is not on
1673 text, or we can't tell because W's current matrix is not up to
1674 date. */
1675
1676 static struct glyph *
1677 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1678 struct window *w;
1679 int x, y;
1680 int *hpos, *vpos, *dx, *dy, *area;
1681 {
1682 struct glyph *glyph, *end;
1683 struct glyph_row *row = NULL;
1684 int x0, i;
1685
1686 /* Find row containing Y. Give up if some row is not enabled. */
1687 for (i = 0; i < w->current_matrix->nrows; ++i)
1688 {
1689 row = MATRIX_ROW (w->current_matrix, i);
1690 if (!row->enabled_p)
1691 return NULL;
1692 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1693 break;
1694 }
1695
1696 *vpos = i;
1697 *hpos = 0;
1698
1699 /* Give up if Y is not in the window. */
1700 if (i == w->current_matrix->nrows)
1701 return NULL;
1702
1703 /* Get the glyph area containing X. */
1704 if (w->pseudo_window_p)
1705 {
1706 *area = TEXT_AREA;
1707 x0 = 0;
1708 }
1709 else
1710 {
1711 if (x < window_box_left_offset (w, TEXT_AREA))
1712 {
1713 *area = LEFT_MARGIN_AREA;
1714 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1715 }
1716 else if (x < window_box_right_offset (w, TEXT_AREA))
1717 {
1718 *area = TEXT_AREA;
1719 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1720 }
1721 else
1722 {
1723 *area = RIGHT_MARGIN_AREA;
1724 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1725 }
1726 }
1727
1728 /* Find glyph containing X. */
1729 glyph = row->glyphs[*area];
1730 end = glyph + row->used[*area];
1731 x -= x0;
1732 while (glyph < end && x >= glyph->pixel_width)
1733 {
1734 x -= glyph->pixel_width;
1735 ++glyph;
1736 }
1737
1738 if (glyph == end)
1739 return NULL;
1740
1741 if (dx)
1742 {
1743 *dx = x;
1744 *dy = y - (row->y + row->ascent - glyph->ascent);
1745 }
1746
1747 *hpos = glyph - row->glyphs[*area];
1748 return glyph;
1749 }
1750
1751
1752 /* EXPORT:
1753 Convert frame-relative x/y to coordinates relative to window W.
1754 Takes pseudo-windows into account. */
1755
1756 void
1757 frame_to_window_pixel_xy (w, x, y)
1758 struct window *w;
1759 int *x, *y;
1760 {
1761 if (w->pseudo_window_p)
1762 {
1763 /* A pseudo-window is always full-width, and starts at the
1764 left edge of the frame, plus a frame border. */
1765 struct frame *f = XFRAME (w->frame);
1766 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1767 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1768 }
1769 else
1770 {
1771 *x -= WINDOW_LEFT_EDGE_X (w);
1772 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1773 }
1774 }
1775
1776 /* EXPORT:
1777 Return in RECTS[] at most N clipping rectangles for glyph string S.
1778 Return the number of stored rectangles. */
1779
1780 int
1781 get_glyph_string_clip_rects (s, rects, n)
1782 struct glyph_string *s;
1783 NativeRectangle *rects;
1784 int n;
1785 {
1786 XRectangle r;
1787
1788 if (n <= 0)
1789 return 0;
1790
1791 if (s->row->full_width_p)
1792 {
1793 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1794 r.x = WINDOW_LEFT_EDGE_X (s->w);
1795 r.width = WINDOW_TOTAL_WIDTH (s->w);
1796
1797 /* Unless displaying a mode or menu bar line, which are always
1798 fully visible, clip to the visible part of the row. */
1799 if (s->w->pseudo_window_p)
1800 r.height = s->row->visible_height;
1801 else
1802 r.height = s->height;
1803 }
1804 else
1805 {
1806 /* This is a text line that may be partially visible. */
1807 r.x = window_box_left (s->w, s->area);
1808 r.width = window_box_width (s->w, s->area);
1809 r.height = s->row->visible_height;
1810 }
1811
1812 if (s->clip_head)
1813 if (r.x < s->clip_head->x)
1814 {
1815 if (r.width >= s->clip_head->x - r.x)
1816 r.width -= s->clip_head->x - r.x;
1817 else
1818 r.width = 0;
1819 r.x = s->clip_head->x;
1820 }
1821 if (s->clip_tail)
1822 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1823 {
1824 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1825 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1826 else
1827 r.width = 0;
1828 }
1829
1830 /* If S draws overlapping rows, it's sufficient to use the top and
1831 bottom of the window for clipping because this glyph string
1832 intentionally draws over other lines. */
1833 if (s->for_overlaps)
1834 {
1835 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1836 r.height = window_text_bottom_y (s->w) - r.y;
1837
1838 /* Alas, the above simple strategy does not work for the
1839 environments with anti-aliased text: if the same text is
1840 drawn onto the same place multiple times, it gets thicker.
1841 If the overlap we are processing is for the erased cursor, we
1842 take the intersection with the rectagle of the cursor. */
1843 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1844 {
1845 XRectangle rc, r_save = r;
1846
1847 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1848 rc.y = s->w->phys_cursor.y;
1849 rc.width = s->w->phys_cursor_width;
1850 rc.height = s->w->phys_cursor_height;
1851
1852 x_intersect_rectangles (&r_save, &rc, &r);
1853 }
1854 }
1855 else
1856 {
1857 /* Don't use S->y for clipping because it doesn't take partially
1858 visible lines into account. For example, it can be negative for
1859 partially visible lines at the top of a window. */
1860 if (!s->row->full_width_p
1861 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1862 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1863 else
1864 r.y = max (0, s->row->y);
1865
1866 /* If drawing a tool-bar window, draw it over the internal border
1867 at the top of the window. */
1868 if (WINDOWP (s->f->tool_bar_window)
1869 && s->w == XWINDOW (s->f->tool_bar_window))
1870 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1871 }
1872
1873 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1874
1875 /* If drawing the cursor, don't let glyph draw outside its
1876 advertised boundaries. Cleartype does this under some circumstances. */
1877 if (s->hl == DRAW_CURSOR)
1878 {
1879 struct glyph *glyph = s->first_glyph;
1880 int height, max_y;
1881
1882 if (s->x > r.x)
1883 {
1884 r.width -= s->x - r.x;
1885 r.x = s->x;
1886 }
1887 r.width = min (r.width, glyph->pixel_width);
1888
1889 /* If r.y is below window bottom, ensure that we still see a cursor. */
1890 height = min (glyph->ascent + glyph->descent,
1891 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1892 max_y = window_text_bottom_y (s->w) - height;
1893 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1894 if (s->ybase - glyph->ascent > max_y)
1895 {
1896 r.y = max_y;
1897 r.height = height;
1898 }
1899 else
1900 {
1901 /* Don't draw cursor glyph taller than our actual glyph. */
1902 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1903 if (height < r.height)
1904 {
1905 max_y = r.y + r.height;
1906 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1907 r.height = min (max_y - r.y, height);
1908 }
1909 }
1910 }
1911
1912 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1913 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1914 {
1915 #ifdef CONVERT_FROM_XRECT
1916 CONVERT_FROM_XRECT (r, *rects);
1917 #else
1918 *rects = r;
1919 #endif
1920 return 1;
1921 }
1922 else
1923 {
1924 /* If we are processing overlapping and allowed to return
1925 multiple clipping rectangles, we exclude the row of the glyph
1926 string from the clipping rectangle. This is to avoid drawing
1927 the same text on the environment with anti-aliasing. */
1928 #ifdef CONVERT_FROM_XRECT
1929 XRectangle rs[2];
1930 #else
1931 XRectangle *rs = rects;
1932 #endif
1933 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1934
1935 if (s->for_overlaps & OVERLAPS_PRED)
1936 {
1937 rs[i] = r;
1938 if (r.y + r.height > row_y)
1939 {
1940 if (r.y < row_y)
1941 rs[i].height = row_y - r.y;
1942 else
1943 rs[i].height = 0;
1944 }
1945 i++;
1946 }
1947 if (s->for_overlaps & OVERLAPS_SUCC)
1948 {
1949 rs[i] = r;
1950 if (r.y < row_y + s->row->visible_height)
1951 {
1952 if (r.y + r.height > row_y + s->row->visible_height)
1953 {
1954 rs[i].y = row_y + s->row->visible_height;
1955 rs[i].height = r.y + r.height - rs[i].y;
1956 }
1957 else
1958 rs[i].height = 0;
1959 }
1960 i++;
1961 }
1962
1963 n = i;
1964 #ifdef CONVERT_FROM_XRECT
1965 for (i = 0; i < n; i++)
1966 CONVERT_FROM_XRECT (rs[i], rects[i]);
1967 #endif
1968 return n;
1969 }
1970 }
1971
1972 /* EXPORT:
1973 Return in *NR the clipping rectangle for glyph string S. */
1974
1975 void
1976 get_glyph_string_clip_rect (s, nr)
1977 struct glyph_string *s;
1978 NativeRectangle *nr;
1979 {
1980 get_glyph_string_clip_rects (s, nr, 1);
1981 }
1982
1983
1984 /* EXPORT:
1985 Return the position and height of the phys cursor in window W.
1986 Set w->phys_cursor_width to width of phys cursor.
1987 */
1988
1989 int
1990 get_phys_cursor_geometry (w, row, glyph, heightp)
1991 struct window *w;
1992 struct glyph_row *row;
1993 struct glyph *glyph;
1994 int *heightp;
1995 {
1996 struct frame *f = XFRAME (WINDOW_FRAME (w));
1997 int y, wd, h, h0, y0;
1998
1999 /* Compute the width of the rectangle to draw. If on a stretch
2000 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2001 rectangle as wide as the glyph, but use a canonical character
2002 width instead. */
2003 wd = glyph->pixel_width - 1;
2004 #ifdef HAVE_NTGUI
2005 wd++; /* Why? */
2006 #endif
2007 if (glyph->type == STRETCH_GLYPH
2008 && !x_stretch_cursor_p)
2009 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2010 w->phys_cursor_width = wd;
2011
2012 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2013
2014 /* If y is below window bottom, ensure that we still see a cursor. */
2015 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2016
2017 h = max (h0, glyph->ascent + glyph->descent);
2018 h0 = min (h0, glyph->ascent + glyph->descent);
2019
2020 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2021 if (y < y0)
2022 {
2023 h = max (h - (y0 - y) + 1, h0);
2024 y = y0 - 1;
2025 }
2026 else
2027 {
2028 y0 = window_text_bottom_y (w) - h0;
2029 if (y > y0)
2030 {
2031 h += y - y0;
2032 y = y0;
2033 }
2034 }
2035
2036 *heightp = h;
2037 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
2038 }
2039
2040 /*
2041 * Remember which glyph the mouse is over.
2042 */
2043
2044 void
2045 remember_mouse_glyph (f, gx, gy, rect)
2046 struct frame *f;
2047 int gx, gy;
2048 NativeRectangle *rect;
2049 {
2050 Lisp_Object window;
2051 struct window *w;
2052 struct glyph_row *r, *gr, *end_row;
2053 enum window_part part;
2054 enum glyph_row_area area;
2055 int x, y, width, height;
2056
2057 /* Try to determine frame pixel position and size of the glyph under
2058 frame pixel coordinates X/Y on frame F. */
2059
2060 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2061 if (NILP (window))
2062 {
2063 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2064 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2065 goto virtual_glyph;
2066 }
2067
2068 w = XWINDOW (window);
2069 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2070 height = WINDOW_FRAME_LINE_HEIGHT (w);
2071
2072 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2073 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2074
2075 if (w->pseudo_window_p)
2076 {
2077 area = TEXT_AREA;
2078 part = ON_MODE_LINE; /* Don't adjust margin. */
2079 goto text_glyph;
2080 }
2081
2082 switch (part)
2083 {
2084 case ON_LEFT_MARGIN:
2085 area = LEFT_MARGIN_AREA;
2086 goto text_glyph;
2087
2088 case ON_RIGHT_MARGIN:
2089 area = RIGHT_MARGIN_AREA;
2090 goto text_glyph;
2091
2092 case ON_HEADER_LINE:
2093 case ON_MODE_LINE:
2094 gr = (part == ON_HEADER_LINE
2095 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2096 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2097 gy = gr->y;
2098 area = TEXT_AREA;
2099 goto text_glyph_row_found;
2100
2101 case ON_TEXT:
2102 area = TEXT_AREA;
2103
2104 text_glyph:
2105 gr = 0; gy = 0;
2106 for (; r <= end_row && r->enabled_p; ++r)
2107 if (r->y + r->height > y)
2108 {
2109 gr = r; gy = r->y;
2110 break;
2111 }
2112
2113 text_glyph_row_found:
2114 if (gr && gy <= y)
2115 {
2116 struct glyph *g = gr->glyphs[area];
2117 struct glyph *end = g + gr->used[area];
2118
2119 height = gr->height;
2120 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2121 if (gx + g->pixel_width > x)
2122 break;
2123
2124 if (g < end)
2125 {
2126 if (g->type == IMAGE_GLYPH)
2127 {
2128 /* Don't remember when mouse is over image, as
2129 image may have hot-spots. */
2130 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2131 return;
2132 }
2133 width = g->pixel_width;
2134 }
2135 else
2136 {
2137 /* Use nominal char spacing at end of line. */
2138 x -= gx;
2139 gx += (x / width) * width;
2140 }
2141
2142 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2143 gx += window_box_left_offset (w, area);
2144 }
2145 else
2146 {
2147 /* Use nominal line height at end of window. */
2148 gx = (x / width) * width;
2149 y -= gy;
2150 gy += (y / height) * height;
2151 }
2152 break;
2153
2154 case ON_LEFT_FRINGE:
2155 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2156 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2157 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2158 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2159 goto row_glyph;
2160
2161 case ON_RIGHT_FRINGE:
2162 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2163 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2164 : window_box_right_offset (w, TEXT_AREA));
2165 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2166 goto row_glyph;
2167
2168 case ON_SCROLL_BAR:
2169 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2170 ? 0
2171 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2172 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2173 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2174 : 0)));
2175 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2176
2177 row_glyph:
2178 gr = 0, gy = 0;
2179 for (; r <= end_row && r->enabled_p; ++r)
2180 if (r->y + r->height > y)
2181 {
2182 gr = r; gy = r->y;
2183 break;
2184 }
2185
2186 if (gr && gy <= y)
2187 height = gr->height;
2188 else
2189 {
2190 /* Use nominal line height at end of window. */
2191 y -= gy;
2192 gy += (y / height) * height;
2193 }
2194 break;
2195
2196 default:
2197 ;
2198 virtual_glyph:
2199 /* If there is no glyph under the mouse, then we divide the screen
2200 into a grid of the smallest glyph in the frame, and use that
2201 as our "glyph". */
2202
2203 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2204 round down even for negative values. */
2205 if (gx < 0)
2206 gx -= width - 1;
2207 if (gy < 0)
2208 gy -= height - 1;
2209
2210 gx = (gx / width) * width;
2211 gy = (gy / height) * height;
2212
2213 goto store_rect;
2214 }
2215
2216 gx += WINDOW_LEFT_EDGE_X (w);
2217 gy += WINDOW_TOP_EDGE_Y (w);
2218
2219 store_rect:
2220 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2221
2222 /* Visible feedback for debugging. */
2223 #if 0
2224 #if HAVE_X_WINDOWS
2225 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2226 f->output_data.x->normal_gc,
2227 gx, gy, width, height);
2228 #endif
2229 #endif
2230 }
2231
2232
2233 #endif /* HAVE_WINDOW_SYSTEM */
2234
2235 \f
2236 /***********************************************************************
2237 Lisp form evaluation
2238 ***********************************************************************/
2239
2240 /* Error handler for safe_eval and safe_call. */
2241
2242 static Lisp_Object
2243 safe_eval_handler (arg)
2244 Lisp_Object arg;
2245 {
2246 add_to_log ("Error during redisplay: %s", arg, Qnil);
2247 return Qnil;
2248 }
2249
2250
2251 /* Evaluate SEXPR and return the result, or nil if something went
2252 wrong. Prevent redisplay during the evaluation. */
2253
2254 Lisp_Object
2255 safe_eval (sexpr)
2256 Lisp_Object sexpr;
2257 {
2258 Lisp_Object val;
2259
2260 if (inhibit_eval_during_redisplay)
2261 val = Qnil;
2262 else
2263 {
2264 int count = SPECPDL_INDEX ();
2265 struct gcpro gcpro1;
2266
2267 GCPRO1 (sexpr);
2268 specbind (Qinhibit_redisplay, Qt);
2269 /* Use Qt to ensure debugger does not run,
2270 so there is no possibility of wanting to redisplay. */
2271 val = internal_condition_case_1 (Feval, sexpr, Qt,
2272 safe_eval_handler);
2273 UNGCPRO;
2274 val = unbind_to (count, val);
2275 }
2276
2277 return val;
2278 }
2279
2280
2281 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2282 Return the result, or nil if something went wrong. Prevent
2283 redisplay during the evaluation. */
2284
2285 Lisp_Object
2286 safe_call (nargs, args)
2287 int nargs;
2288 Lisp_Object *args;
2289 {
2290 Lisp_Object val;
2291
2292 if (inhibit_eval_during_redisplay)
2293 val = Qnil;
2294 else
2295 {
2296 int count = SPECPDL_INDEX ();
2297 struct gcpro gcpro1;
2298
2299 GCPRO1 (args[0]);
2300 gcpro1.nvars = nargs;
2301 specbind (Qinhibit_redisplay, Qt);
2302 /* Use Qt to ensure debugger does not run,
2303 so there is no possibility of wanting to redisplay. */
2304 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2305 safe_eval_handler);
2306 UNGCPRO;
2307 val = unbind_to (count, val);
2308 }
2309
2310 return val;
2311 }
2312
2313
2314 /* Call function FN with one argument ARG.
2315 Return the result, or nil if something went wrong. */
2316
2317 Lisp_Object
2318 safe_call1 (fn, arg)
2319 Lisp_Object fn, arg;
2320 {
2321 Lisp_Object args[2];
2322 args[0] = fn;
2323 args[1] = arg;
2324 return safe_call (2, args);
2325 }
2326
2327
2328 \f
2329 /***********************************************************************
2330 Debugging
2331 ***********************************************************************/
2332
2333 #if 0
2334
2335 /* Define CHECK_IT to perform sanity checks on iterators.
2336 This is for debugging. It is too slow to do unconditionally. */
2337
2338 static void
2339 check_it (it)
2340 struct it *it;
2341 {
2342 if (it->method == GET_FROM_STRING)
2343 {
2344 xassert (STRINGP (it->string));
2345 xassert (IT_STRING_CHARPOS (*it) >= 0);
2346 }
2347 else
2348 {
2349 xassert (IT_STRING_CHARPOS (*it) < 0);
2350 if (it->method == GET_FROM_BUFFER)
2351 {
2352 /* Check that character and byte positions agree. */
2353 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2354 }
2355 }
2356
2357 if (it->dpvec)
2358 xassert (it->current.dpvec_index >= 0);
2359 else
2360 xassert (it->current.dpvec_index < 0);
2361 }
2362
2363 #define CHECK_IT(IT) check_it ((IT))
2364
2365 #else /* not 0 */
2366
2367 #define CHECK_IT(IT) (void) 0
2368
2369 #endif /* not 0 */
2370
2371
2372 #if GLYPH_DEBUG
2373
2374 /* Check that the window end of window W is what we expect it
2375 to be---the last row in the current matrix displaying text. */
2376
2377 static void
2378 check_window_end (w)
2379 struct window *w;
2380 {
2381 if (!MINI_WINDOW_P (w)
2382 && !NILP (w->window_end_valid))
2383 {
2384 struct glyph_row *row;
2385 xassert ((row = MATRIX_ROW (w->current_matrix,
2386 XFASTINT (w->window_end_vpos)),
2387 !row->enabled_p
2388 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2389 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2390 }
2391 }
2392
2393 #define CHECK_WINDOW_END(W) check_window_end ((W))
2394
2395 #else /* not GLYPH_DEBUG */
2396
2397 #define CHECK_WINDOW_END(W) (void) 0
2398
2399 #endif /* not GLYPH_DEBUG */
2400
2401
2402 \f
2403 /***********************************************************************
2404 Iterator initialization
2405 ***********************************************************************/
2406
2407 /* Initialize IT for displaying current_buffer in window W, starting
2408 at character position CHARPOS. CHARPOS < 0 means that no buffer
2409 position is specified which is useful when the iterator is assigned
2410 a position later. BYTEPOS is the byte position corresponding to
2411 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2412
2413 If ROW is not null, calls to produce_glyphs with IT as parameter
2414 will produce glyphs in that row.
2415
2416 BASE_FACE_ID is the id of a base face to use. It must be one of
2417 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2418 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2419 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2420
2421 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2422 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2423 will be initialized to use the corresponding mode line glyph row of
2424 the desired matrix of W. */
2425
2426 void
2427 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2428 struct it *it;
2429 struct window *w;
2430 int charpos, bytepos;
2431 struct glyph_row *row;
2432 enum face_id base_face_id;
2433 {
2434 int highlight_region_p;
2435
2436 /* Some precondition checks. */
2437 xassert (w != NULL && it != NULL);
2438 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2439 && charpos <= ZV));
2440
2441 /* If face attributes have been changed since the last redisplay,
2442 free realized faces now because they depend on face definitions
2443 that might have changed. Don't free faces while there might be
2444 desired matrices pending which reference these faces. */
2445 if (face_change_count && !inhibit_free_realized_faces)
2446 {
2447 face_change_count = 0;
2448 free_all_realized_faces (Qnil);
2449 }
2450
2451 /* Use one of the mode line rows of W's desired matrix if
2452 appropriate. */
2453 if (row == NULL)
2454 {
2455 if (base_face_id == MODE_LINE_FACE_ID
2456 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2457 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2458 else if (base_face_id == HEADER_LINE_FACE_ID)
2459 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2460 }
2461
2462 /* Clear IT. */
2463 bzero (it, sizeof *it);
2464 it->current.overlay_string_index = -1;
2465 it->current.dpvec_index = -1;
2466 it->base_face_id = base_face_id;
2467 it->string = Qnil;
2468 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2469
2470 /* The window in which we iterate over current_buffer: */
2471 XSETWINDOW (it->window, w);
2472 it->w = w;
2473 it->f = XFRAME (w->frame);
2474
2475 /* Extra space between lines (on window systems only). */
2476 if (base_face_id == DEFAULT_FACE_ID
2477 && FRAME_WINDOW_P (it->f))
2478 {
2479 if (NATNUMP (current_buffer->extra_line_spacing))
2480 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2481 else if (FLOATP (current_buffer->extra_line_spacing))
2482 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2483 * FRAME_LINE_HEIGHT (it->f));
2484 else if (it->f->extra_line_spacing > 0)
2485 it->extra_line_spacing = it->f->extra_line_spacing;
2486 it->max_extra_line_spacing = 0;
2487 }
2488
2489 /* If realized faces have been removed, e.g. because of face
2490 attribute changes of named faces, recompute them. When running
2491 in batch mode, the face cache of Vterminal_frame is null. If
2492 we happen to get called, make a dummy face cache. */
2493 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2494 init_frame_faces (it->f);
2495 if (FRAME_FACE_CACHE (it->f)->used == 0)
2496 recompute_basic_faces (it->f);
2497
2498 /* Current value of the `slice', `space-width', and 'height' properties. */
2499 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2500 it->space_width = Qnil;
2501 it->font_height = Qnil;
2502 it->override_ascent = -1;
2503
2504 /* Are control characters displayed as `^C'? */
2505 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2506
2507 /* -1 means everything between a CR and the following line end
2508 is invisible. >0 means lines indented more than this value are
2509 invisible. */
2510 it->selective = (INTEGERP (current_buffer->selective_display)
2511 ? XFASTINT (current_buffer->selective_display)
2512 : (!NILP (current_buffer->selective_display)
2513 ? -1 : 0));
2514 it->selective_display_ellipsis_p
2515 = !NILP (current_buffer->selective_display_ellipses);
2516
2517 /* Display table to use. */
2518 it->dp = window_display_table (w);
2519
2520 /* Are multibyte characters enabled in current_buffer? */
2521 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2522
2523 /* Non-zero if we should highlight the region. */
2524 highlight_region_p
2525 = (!NILP (Vtransient_mark_mode)
2526 && !NILP (current_buffer->mark_active)
2527 && XMARKER (current_buffer->mark)->buffer != 0);
2528
2529 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2530 start and end of a visible region in window IT->w. Set both to
2531 -1 to indicate no region. */
2532 if (highlight_region_p
2533 /* Maybe highlight only in selected window. */
2534 && (/* Either show region everywhere. */
2535 highlight_nonselected_windows
2536 /* Or show region in the selected window. */
2537 || w == XWINDOW (selected_window)
2538 /* Or show the region if we are in the mini-buffer and W is
2539 the window the mini-buffer refers to. */
2540 || (MINI_WINDOW_P (XWINDOW (selected_window))
2541 && WINDOWP (minibuf_selected_window)
2542 && w == XWINDOW (minibuf_selected_window))))
2543 {
2544 int charpos = marker_position (current_buffer->mark);
2545 it->region_beg_charpos = min (PT, charpos);
2546 it->region_end_charpos = max (PT, charpos);
2547 }
2548 else
2549 it->region_beg_charpos = it->region_end_charpos = -1;
2550
2551 /* Get the position at which the redisplay_end_trigger hook should
2552 be run, if it is to be run at all. */
2553 if (MARKERP (w->redisplay_end_trigger)
2554 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2555 it->redisplay_end_trigger_charpos
2556 = marker_position (w->redisplay_end_trigger);
2557 else if (INTEGERP (w->redisplay_end_trigger))
2558 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2559
2560 /* Correct bogus values of tab_width. */
2561 it->tab_width = XINT (current_buffer->tab_width);
2562 if (it->tab_width <= 0 || it->tab_width > 1000)
2563 it->tab_width = 8;
2564
2565 /* Are lines in the display truncated? */
2566 it->truncate_lines_p
2567 = (base_face_id != DEFAULT_FACE_ID
2568 || XINT (it->w->hscroll)
2569 || (truncate_partial_width_windows
2570 && !WINDOW_FULL_WIDTH_P (it->w))
2571 || !NILP (current_buffer->truncate_lines));
2572
2573 /* Get dimensions of truncation and continuation glyphs. These are
2574 displayed as fringe bitmaps under X, so we don't need them for such
2575 frames. */
2576 if (!FRAME_WINDOW_P (it->f))
2577 {
2578 if (it->truncate_lines_p)
2579 {
2580 /* We will need the truncation glyph. */
2581 xassert (it->glyph_row == NULL);
2582 produce_special_glyphs (it, IT_TRUNCATION);
2583 it->truncation_pixel_width = it->pixel_width;
2584 }
2585 else
2586 {
2587 /* We will need the continuation glyph. */
2588 xassert (it->glyph_row == NULL);
2589 produce_special_glyphs (it, IT_CONTINUATION);
2590 it->continuation_pixel_width = it->pixel_width;
2591 }
2592
2593 /* Reset these values to zero because the produce_special_glyphs
2594 above has changed them. */
2595 it->pixel_width = it->ascent = it->descent = 0;
2596 it->phys_ascent = it->phys_descent = 0;
2597 }
2598
2599 /* Set this after getting the dimensions of truncation and
2600 continuation glyphs, so that we don't produce glyphs when calling
2601 produce_special_glyphs, above. */
2602 it->glyph_row = row;
2603 it->area = TEXT_AREA;
2604
2605 /* Get the dimensions of the display area. The display area
2606 consists of the visible window area plus a horizontally scrolled
2607 part to the left of the window. All x-values are relative to the
2608 start of this total display area. */
2609 if (base_face_id != DEFAULT_FACE_ID)
2610 {
2611 /* Mode lines, menu bar in terminal frames. */
2612 it->first_visible_x = 0;
2613 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2614 }
2615 else
2616 {
2617 it->first_visible_x
2618 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2619 it->last_visible_x = (it->first_visible_x
2620 + window_box_width (w, TEXT_AREA));
2621
2622 /* If we truncate lines, leave room for the truncator glyph(s) at
2623 the right margin. Otherwise, leave room for the continuation
2624 glyph(s). Truncation and continuation glyphs are not inserted
2625 for window-based redisplay. */
2626 if (!FRAME_WINDOW_P (it->f))
2627 {
2628 if (it->truncate_lines_p)
2629 it->last_visible_x -= it->truncation_pixel_width;
2630 else
2631 it->last_visible_x -= it->continuation_pixel_width;
2632 }
2633
2634 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2635 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2636 }
2637
2638 /* Leave room for a border glyph. */
2639 if (!FRAME_WINDOW_P (it->f)
2640 && !WINDOW_RIGHTMOST_P (it->w))
2641 it->last_visible_x -= 1;
2642
2643 it->last_visible_y = window_text_bottom_y (w);
2644
2645 /* For mode lines and alike, arrange for the first glyph having a
2646 left box line if the face specifies a box. */
2647 if (base_face_id != DEFAULT_FACE_ID)
2648 {
2649 struct face *face;
2650
2651 it->face_id = base_face_id;
2652
2653 /* If we have a boxed mode line, make the first character appear
2654 with a left box line. */
2655 face = FACE_FROM_ID (it->f, base_face_id);
2656 if (face->box != FACE_NO_BOX)
2657 it->start_of_box_run_p = 1;
2658 }
2659
2660 /* If a buffer position was specified, set the iterator there,
2661 getting overlays and face properties from that position. */
2662 if (charpos >= BUF_BEG (current_buffer))
2663 {
2664 it->end_charpos = ZV;
2665 it->face_id = -1;
2666 IT_CHARPOS (*it) = charpos;
2667
2668 /* Compute byte position if not specified. */
2669 if (bytepos < charpos)
2670 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2671 else
2672 IT_BYTEPOS (*it) = bytepos;
2673
2674 it->start = it->current;
2675
2676 /* Compute faces etc. */
2677 reseat (it, it->current.pos, 1);
2678 }
2679
2680 CHECK_IT (it);
2681 }
2682
2683
2684 /* Initialize IT for the display of window W with window start POS. */
2685
2686 void
2687 start_display (it, w, pos)
2688 struct it *it;
2689 struct window *w;
2690 struct text_pos pos;
2691 {
2692 struct glyph_row *row;
2693 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2694
2695 row = w->desired_matrix->rows + first_vpos;
2696 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2697 it->first_vpos = first_vpos;
2698
2699 /* Don't reseat to previous visible line start if current start
2700 position is in a string or image. */
2701 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2702 {
2703 int start_at_line_beg_p;
2704 int first_y = it->current_y;
2705
2706 /* If window start is not at a line start, skip forward to POS to
2707 get the correct continuation lines width. */
2708 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2709 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2710 if (!start_at_line_beg_p)
2711 {
2712 int new_x;
2713
2714 reseat_at_previous_visible_line_start (it);
2715 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2716
2717 new_x = it->current_x + it->pixel_width;
2718
2719 /* If lines are continued, this line may end in the middle
2720 of a multi-glyph character (e.g. a control character
2721 displayed as \003, or in the middle of an overlay
2722 string). In this case move_it_to above will not have
2723 taken us to the start of the continuation line but to the
2724 end of the continued line. */
2725 if (it->current_x > 0
2726 && !it->truncate_lines_p /* Lines are continued. */
2727 && (/* And glyph doesn't fit on the line. */
2728 new_x > it->last_visible_x
2729 /* Or it fits exactly and we're on a window
2730 system frame. */
2731 || (new_x == it->last_visible_x
2732 && FRAME_WINDOW_P (it->f))))
2733 {
2734 if (it->current.dpvec_index >= 0
2735 || it->current.overlay_string_index >= 0)
2736 {
2737 set_iterator_to_next (it, 1);
2738 move_it_in_display_line_to (it, -1, -1, 0);
2739 }
2740
2741 it->continuation_lines_width += it->current_x;
2742 }
2743
2744 /* We're starting a new display line, not affected by the
2745 height of the continued line, so clear the appropriate
2746 fields in the iterator structure. */
2747 it->max_ascent = it->max_descent = 0;
2748 it->max_phys_ascent = it->max_phys_descent = 0;
2749
2750 it->current_y = first_y;
2751 it->vpos = 0;
2752 it->current_x = it->hpos = 0;
2753 }
2754 }
2755
2756 #if 0 /* Don't assert the following because start_display is sometimes
2757 called intentionally with a window start that is not at a
2758 line start. Please leave this code in as a comment. */
2759
2760 /* Window start should be on a line start, now. */
2761 xassert (it->continuation_lines_width
2762 || IT_CHARPOS (it) == BEGV
2763 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2764 #endif /* 0 */
2765 }
2766
2767
2768 /* Return 1 if POS is a position in ellipses displayed for invisible
2769 text. W is the window we display, for text property lookup. */
2770
2771 static int
2772 in_ellipses_for_invisible_text_p (pos, w)
2773 struct display_pos *pos;
2774 struct window *w;
2775 {
2776 Lisp_Object prop, window;
2777 int ellipses_p = 0;
2778 int charpos = CHARPOS (pos->pos);
2779
2780 /* If POS specifies a position in a display vector, this might
2781 be for an ellipsis displayed for invisible text. We won't
2782 get the iterator set up for delivering that ellipsis unless
2783 we make sure that it gets aware of the invisible text. */
2784 if (pos->dpvec_index >= 0
2785 && pos->overlay_string_index < 0
2786 && CHARPOS (pos->string_pos) < 0
2787 && charpos > BEGV
2788 && (XSETWINDOW (window, w),
2789 prop = Fget_char_property (make_number (charpos),
2790 Qinvisible, window),
2791 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2792 {
2793 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2794 window);
2795 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2796 }
2797
2798 return ellipses_p;
2799 }
2800
2801
2802 /* Initialize IT for stepping through current_buffer in window W,
2803 starting at position POS that includes overlay string and display
2804 vector/ control character translation position information. Value
2805 is zero if there are overlay strings with newlines at POS. */
2806
2807 static int
2808 init_from_display_pos (it, w, pos)
2809 struct it *it;
2810 struct window *w;
2811 struct display_pos *pos;
2812 {
2813 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2814 int i, overlay_strings_with_newlines = 0;
2815
2816 /* If POS specifies a position in a display vector, this might
2817 be for an ellipsis displayed for invisible text. We won't
2818 get the iterator set up for delivering that ellipsis unless
2819 we make sure that it gets aware of the invisible text. */
2820 if (in_ellipses_for_invisible_text_p (pos, w))
2821 {
2822 --charpos;
2823 bytepos = 0;
2824 }
2825
2826 /* Keep in mind: the call to reseat in init_iterator skips invisible
2827 text, so we might end up at a position different from POS. This
2828 is only a problem when POS is a row start after a newline and an
2829 overlay starts there with an after-string, and the overlay has an
2830 invisible property. Since we don't skip invisible text in
2831 display_line and elsewhere immediately after consuming the
2832 newline before the row start, such a POS will not be in a string,
2833 but the call to init_iterator below will move us to the
2834 after-string. */
2835 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2836
2837 /* This only scans the current chunk -- it should scan all chunks.
2838 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2839 to 16 in 22.1 to make this a lesser problem. */
2840 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2841 {
2842 const char *s = SDATA (it->overlay_strings[i]);
2843 const char *e = s + SBYTES (it->overlay_strings[i]);
2844
2845 while (s < e && *s != '\n')
2846 ++s;
2847
2848 if (s < e)
2849 {
2850 overlay_strings_with_newlines = 1;
2851 break;
2852 }
2853 }
2854
2855 /* If position is within an overlay string, set up IT to the right
2856 overlay string. */
2857 if (pos->overlay_string_index >= 0)
2858 {
2859 int relative_index;
2860
2861 /* If the first overlay string happens to have a `display'
2862 property for an image, the iterator will be set up for that
2863 image, and we have to undo that setup first before we can
2864 correct the overlay string index. */
2865 if (it->method == GET_FROM_IMAGE)
2866 pop_it (it);
2867
2868 /* We already have the first chunk of overlay strings in
2869 IT->overlay_strings. Load more until the one for
2870 pos->overlay_string_index is in IT->overlay_strings. */
2871 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2872 {
2873 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2874 it->current.overlay_string_index = 0;
2875 while (n--)
2876 {
2877 load_overlay_strings (it, 0);
2878 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2879 }
2880 }
2881
2882 it->current.overlay_string_index = pos->overlay_string_index;
2883 relative_index = (it->current.overlay_string_index
2884 % OVERLAY_STRING_CHUNK_SIZE);
2885 it->string = it->overlay_strings[relative_index];
2886 xassert (STRINGP (it->string));
2887 it->current.string_pos = pos->string_pos;
2888 it->method = GET_FROM_STRING;
2889 }
2890
2891 #if 0 /* This is bogus because POS not having an overlay string
2892 position does not mean it's after the string. Example: A
2893 line starting with a before-string and initialization of IT
2894 to the previous row's end position. */
2895 else if (it->current.overlay_string_index >= 0)
2896 {
2897 /* If POS says we're already after an overlay string ending at
2898 POS, make sure to pop the iterator because it will be in
2899 front of that overlay string. When POS is ZV, we've thereby
2900 also ``processed'' overlay strings at ZV. */
2901 while (it->sp)
2902 pop_it (it);
2903 it->current.overlay_string_index = -1;
2904 it->method = GET_FROM_BUFFER;
2905 if (CHARPOS (pos->pos) == ZV)
2906 it->overlay_strings_at_end_processed_p = 1;
2907 }
2908 #endif /* 0 */
2909
2910 if (CHARPOS (pos->string_pos) >= 0)
2911 {
2912 /* Recorded position is not in an overlay string, but in another
2913 string. This can only be a string from a `display' property.
2914 IT should already be filled with that string. */
2915 it->current.string_pos = pos->string_pos;
2916 xassert (STRINGP (it->string));
2917 }
2918
2919 /* Restore position in display vector translations, control
2920 character translations or ellipses. */
2921 if (pos->dpvec_index >= 0)
2922 {
2923 if (it->dpvec == NULL)
2924 get_next_display_element (it);
2925 xassert (it->dpvec && it->current.dpvec_index == 0);
2926 it->current.dpvec_index = pos->dpvec_index;
2927 }
2928
2929 CHECK_IT (it);
2930 return !overlay_strings_with_newlines;
2931 }
2932
2933
2934 /* Initialize IT for stepping through current_buffer in window W
2935 starting at ROW->start. */
2936
2937 static void
2938 init_to_row_start (it, w, row)
2939 struct it *it;
2940 struct window *w;
2941 struct glyph_row *row;
2942 {
2943 init_from_display_pos (it, w, &row->start);
2944 it->start = row->start;
2945 it->continuation_lines_width = row->continuation_lines_width;
2946 CHECK_IT (it);
2947 }
2948
2949
2950 /* Initialize IT for stepping through current_buffer in window W
2951 starting in the line following ROW, i.e. starting at ROW->end.
2952 Value is zero if there are overlay strings with newlines at ROW's
2953 end position. */
2954
2955 static int
2956 init_to_row_end (it, w, row)
2957 struct it *it;
2958 struct window *w;
2959 struct glyph_row *row;
2960 {
2961 int success = 0;
2962
2963 if (init_from_display_pos (it, w, &row->end))
2964 {
2965 if (row->continued_p)
2966 it->continuation_lines_width
2967 = row->continuation_lines_width + row->pixel_width;
2968 CHECK_IT (it);
2969 success = 1;
2970 }
2971
2972 return success;
2973 }
2974
2975
2976
2977 \f
2978 /***********************************************************************
2979 Text properties
2980 ***********************************************************************/
2981
2982 /* Called when IT reaches IT->stop_charpos. Handle text property and
2983 overlay changes. Set IT->stop_charpos to the next position where
2984 to stop. */
2985
2986 static void
2987 handle_stop (it)
2988 struct it *it;
2989 {
2990 enum prop_handled handled;
2991 int handle_overlay_change_p;
2992 struct props *p;
2993
2994 it->dpvec = NULL;
2995 it->current.dpvec_index = -1;
2996 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2997 it->ignore_overlay_strings_at_pos_p = 0;
2998
2999 /* Use face of preceding text for ellipsis (if invisible) */
3000 if (it->selective_display_ellipsis_p)
3001 it->saved_face_id = it->face_id;
3002
3003 do
3004 {
3005 handled = HANDLED_NORMALLY;
3006
3007 /* Call text property handlers. */
3008 for (p = it_props; p->handler; ++p)
3009 {
3010 handled = p->handler (it);
3011
3012 if (handled == HANDLED_RECOMPUTE_PROPS)
3013 break;
3014 else if (handled == HANDLED_RETURN)
3015 return;
3016 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3017 handle_overlay_change_p = 0;
3018 }
3019
3020 if (handled != HANDLED_RECOMPUTE_PROPS)
3021 {
3022 /* Don't check for overlay strings below when set to deliver
3023 characters from a display vector. */
3024 if (it->method == GET_FROM_DISPLAY_VECTOR)
3025 handle_overlay_change_p = 0;
3026
3027 /* Handle overlay changes. */
3028 if (handle_overlay_change_p)
3029 handled = handle_overlay_change (it);
3030
3031 /* Determine where to stop next. */
3032 if (handled == HANDLED_NORMALLY)
3033 compute_stop_pos (it);
3034 }
3035 }
3036 while (handled == HANDLED_RECOMPUTE_PROPS);
3037 }
3038
3039
3040 /* Compute IT->stop_charpos from text property and overlay change
3041 information for IT's current position. */
3042
3043 static void
3044 compute_stop_pos (it)
3045 struct it *it;
3046 {
3047 register INTERVAL iv, next_iv;
3048 Lisp_Object object, limit, position;
3049
3050 /* If nowhere else, stop at the end. */
3051 it->stop_charpos = it->end_charpos;
3052
3053 if (STRINGP (it->string))
3054 {
3055 /* Strings are usually short, so don't limit the search for
3056 properties. */
3057 object = it->string;
3058 limit = Qnil;
3059 position = make_number (IT_STRING_CHARPOS (*it));
3060 }
3061 else
3062 {
3063 int charpos;
3064
3065 /* If next overlay change is in front of the current stop pos
3066 (which is IT->end_charpos), stop there. Note: value of
3067 next_overlay_change is point-max if no overlay change
3068 follows. */
3069 charpos = next_overlay_change (IT_CHARPOS (*it));
3070 if (charpos < it->stop_charpos)
3071 it->stop_charpos = charpos;
3072
3073 /* If showing the region, we have to stop at the region
3074 start or end because the face might change there. */
3075 if (it->region_beg_charpos > 0)
3076 {
3077 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3078 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3079 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3080 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3081 }
3082
3083 /* Set up variables for computing the stop position from text
3084 property changes. */
3085 XSETBUFFER (object, current_buffer);
3086 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3087 position = make_number (IT_CHARPOS (*it));
3088
3089 }
3090
3091 /* Get the interval containing IT's position. Value is a null
3092 interval if there isn't such an interval. */
3093 iv = validate_interval_range (object, &position, &position, 0);
3094 if (!NULL_INTERVAL_P (iv))
3095 {
3096 Lisp_Object values_here[LAST_PROP_IDX];
3097 struct props *p;
3098
3099 /* Get properties here. */
3100 for (p = it_props; p->handler; ++p)
3101 values_here[p->idx] = textget (iv->plist, *p->name);
3102
3103 /* Look for an interval following iv that has different
3104 properties. */
3105 for (next_iv = next_interval (iv);
3106 (!NULL_INTERVAL_P (next_iv)
3107 && (NILP (limit)
3108 || XFASTINT (limit) > next_iv->position));
3109 next_iv = next_interval (next_iv))
3110 {
3111 for (p = it_props; p->handler; ++p)
3112 {
3113 Lisp_Object new_value;
3114
3115 new_value = textget (next_iv->plist, *p->name);
3116 if (!EQ (values_here[p->idx], new_value))
3117 break;
3118 }
3119
3120 if (p->handler)
3121 break;
3122 }
3123
3124 if (!NULL_INTERVAL_P (next_iv))
3125 {
3126 if (INTEGERP (limit)
3127 && next_iv->position >= XFASTINT (limit))
3128 /* No text property change up to limit. */
3129 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3130 else
3131 /* Text properties change in next_iv. */
3132 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3133 }
3134 }
3135
3136 xassert (STRINGP (it->string)
3137 || (it->stop_charpos >= BEGV
3138 && it->stop_charpos >= IT_CHARPOS (*it)));
3139 }
3140
3141
3142 /* Return the position of the next overlay change after POS in
3143 current_buffer. Value is point-max if no overlay change
3144 follows. This is like `next-overlay-change' but doesn't use
3145 xmalloc. */
3146
3147 static int
3148 next_overlay_change (pos)
3149 int pos;
3150 {
3151 int noverlays;
3152 int endpos;
3153 Lisp_Object *overlays;
3154 int i;
3155
3156 /* Get all overlays at the given position. */
3157 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3158
3159 /* If any of these overlays ends before endpos,
3160 use its ending point instead. */
3161 for (i = 0; i < noverlays; ++i)
3162 {
3163 Lisp_Object oend;
3164 int oendpos;
3165
3166 oend = OVERLAY_END (overlays[i]);
3167 oendpos = OVERLAY_POSITION (oend);
3168 endpos = min (endpos, oendpos);
3169 }
3170
3171 return endpos;
3172 }
3173
3174
3175 \f
3176 /***********************************************************************
3177 Fontification
3178 ***********************************************************************/
3179
3180 /* Handle changes in the `fontified' property of the current buffer by
3181 calling hook functions from Qfontification_functions to fontify
3182 regions of text. */
3183
3184 static enum prop_handled
3185 handle_fontified_prop (it)
3186 struct it *it;
3187 {
3188 Lisp_Object prop, pos;
3189 enum prop_handled handled = HANDLED_NORMALLY;
3190
3191 if (!NILP (Vmemory_full))
3192 return handled;
3193
3194 /* Get the value of the `fontified' property at IT's current buffer
3195 position. (The `fontified' property doesn't have a special
3196 meaning in strings.) If the value is nil, call functions from
3197 Qfontification_functions. */
3198 if (!STRINGP (it->string)
3199 && it->s == NULL
3200 && !NILP (Vfontification_functions)
3201 && !NILP (Vrun_hooks)
3202 && (pos = make_number (IT_CHARPOS (*it)),
3203 prop = Fget_char_property (pos, Qfontified, Qnil),
3204 NILP (prop)))
3205 {
3206 int count = SPECPDL_INDEX ();
3207 Lisp_Object val;
3208
3209 val = Vfontification_functions;
3210 specbind (Qfontification_functions, Qnil);
3211
3212 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3213 safe_call1 (val, pos);
3214 else
3215 {
3216 Lisp_Object globals, fn;
3217 struct gcpro gcpro1, gcpro2;
3218
3219 globals = Qnil;
3220 GCPRO2 (val, globals);
3221
3222 for (; CONSP (val); val = XCDR (val))
3223 {
3224 fn = XCAR (val);
3225
3226 if (EQ (fn, Qt))
3227 {
3228 /* A value of t indicates this hook has a local
3229 binding; it means to run the global binding too.
3230 In a global value, t should not occur. If it
3231 does, we must ignore it to avoid an endless
3232 loop. */
3233 for (globals = Fdefault_value (Qfontification_functions);
3234 CONSP (globals);
3235 globals = XCDR (globals))
3236 {
3237 fn = XCAR (globals);
3238 if (!EQ (fn, Qt))
3239 safe_call1 (fn, pos);
3240 }
3241 }
3242 else
3243 safe_call1 (fn, pos);
3244 }
3245
3246 UNGCPRO;
3247 }
3248
3249 unbind_to (count, Qnil);
3250
3251 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3252 something. This avoids an endless loop if they failed to
3253 fontify the text for which reason ever. */
3254 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3255 handled = HANDLED_RECOMPUTE_PROPS;
3256 }
3257
3258 return handled;
3259 }
3260
3261
3262 \f
3263 /***********************************************************************
3264 Faces
3265 ***********************************************************************/
3266
3267 /* Set up iterator IT from face properties at its current position.
3268 Called from handle_stop. */
3269
3270 static enum prop_handled
3271 handle_face_prop (it)
3272 struct it *it;
3273 {
3274 int new_face_id, next_stop;
3275
3276 if (!STRINGP (it->string))
3277 {
3278 new_face_id
3279 = face_at_buffer_position (it->w,
3280 IT_CHARPOS (*it),
3281 it->region_beg_charpos,
3282 it->region_end_charpos,
3283 &next_stop,
3284 (IT_CHARPOS (*it)
3285 + TEXT_PROP_DISTANCE_LIMIT),
3286 0);
3287
3288 /* Is this a start of a run of characters with box face?
3289 Caveat: this can be called for a freshly initialized
3290 iterator; face_id is -1 in this case. We know that the new
3291 face will not change until limit, i.e. if the new face has a
3292 box, all characters up to limit will have one. But, as
3293 usual, we don't know whether limit is really the end. */
3294 if (new_face_id != it->face_id)
3295 {
3296 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3297
3298 /* If new face has a box but old face has not, this is
3299 the start of a run of characters with box, i.e. it has
3300 a shadow on the left side. The value of face_id of the
3301 iterator will be -1 if this is the initial call that gets
3302 the face. In this case, we have to look in front of IT's
3303 position and see whether there is a face != new_face_id. */
3304 it->start_of_box_run_p
3305 = (new_face->box != FACE_NO_BOX
3306 && (it->face_id >= 0
3307 || IT_CHARPOS (*it) == BEG
3308 || new_face_id != face_before_it_pos (it)));
3309 it->face_box_p = new_face->box != FACE_NO_BOX;
3310 }
3311 }
3312 else
3313 {
3314 int base_face_id, bufpos;
3315
3316 if (it->current.overlay_string_index >= 0)
3317 bufpos = IT_CHARPOS (*it);
3318 else
3319 bufpos = 0;
3320
3321 /* For strings from a buffer, i.e. overlay strings or strings
3322 from a `display' property, use the face at IT's current
3323 buffer position as the base face to merge with, so that
3324 overlay strings appear in the same face as surrounding
3325 text, unless they specify their own faces. */
3326 base_face_id = underlying_face_id (it);
3327
3328 new_face_id = face_at_string_position (it->w,
3329 it->string,
3330 IT_STRING_CHARPOS (*it),
3331 bufpos,
3332 it->region_beg_charpos,
3333 it->region_end_charpos,
3334 &next_stop,
3335 base_face_id, 0);
3336
3337 #if 0 /* This shouldn't be neccessary. Let's check it. */
3338 /* If IT is used to display a mode line we would really like to
3339 use the mode line face instead of the frame's default face. */
3340 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3341 && new_face_id == DEFAULT_FACE_ID)
3342 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3343 #endif
3344
3345 /* Is this a start of a run of characters with box? Caveat:
3346 this can be called for a freshly allocated iterator; face_id
3347 is -1 is this case. We know that the new face will not
3348 change until the next check pos, i.e. if the new face has a
3349 box, all characters up to that position will have a
3350 box. But, as usual, we don't know whether that position
3351 is really the end. */
3352 if (new_face_id != it->face_id)
3353 {
3354 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3355 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3356
3357 /* If new face has a box but old face hasn't, this is the
3358 start of a run of characters with box, i.e. it has a
3359 shadow on the left side. */
3360 it->start_of_box_run_p
3361 = new_face->box && (old_face == NULL || !old_face->box);
3362 it->face_box_p = new_face->box != FACE_NO_BOX;
3363 }
3364 }
3365
3366 it->face_id = new_face_id;
3367 return HANDLED_NORMALLY;
3368 }
3369
3370
3371 /* Return the ID of the face ``underlying'' IT's current position,
3372 which is in a string. If the iterator is associated with a
3373 buffer, return the face at IT's current buffer position.
3374 Otherwise, use the iterator's base_face_id. */
3375
3376 static int
3377 underlying_face_id (it)
3378 struct it *it;
3379 {
3380 int face_id = it->base_face_id, i;
3381
3382 xassert (STRINGP (it->string));
3383
3384 for (i = it->sp - 1; i >= 0; --i)
3385 if (NILP (it->stack[i].string))
3386 face_id = it->stack[i].face_id;
3387
3388 return face_id;
3389 }
3390
3391
3392 /* Compute the face one character before or after the current position
3393 of IT. BEFORE_P non-zero means get the face in front of IT's
3394 position. Value is the id of the face. */
3395
3396 static int
3397 face_before_or_after_it_pos (it, before_p)
3398 struct it *it;
3399 int before_p;
3400 {
3401 int face_id, limit;
3402 int next_check_charpos;
3403 struct text_pos pos;
3404
3405 xassert (it->s == NULL);
3406
3407 if (STRINGP (it->string))
3408 {
3409 int bufpos, base_face_id;
3410
3411 /* No face change past the end of the string (for the case
3412 we are padding with spaces). No face change before the
3413 string start. */
3414 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3415 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3416 return it->face_id;
3417
3418 /* Set pos to the position before or after IT's current position. */
3419 if (before_p)
3420 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3421 else
3422 /* For composition, we must check the character after the
3423 composition. */
3424 pos = (it->what == IT_COMPOSITION
3425 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3426 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3427
3428 if (it->current.overlay_string_index >= 0)
3429 bufpos = IT_CHARPOS (*it);
3430 else
3431 bufpos = 0;
3432
3433 base_face_id = underlying_face_id (it);
3434
3435 /* Get the face for ASCII, or unibyte. */
3436 face_id = face_at_string_position (it->w,
3437 it->string,
3438 CHARPOS (pos),
3439 bufpos,
3440 it->region_beg_charpos,
3441 it->region_end_charpos,
3442 &next_check_charpos,
3443 base_face_id, 0);
3444
3445 /* Correct the face for charsets different from ASCII. Do it
3446 for the multibyte case only. The face returned above is
3447 suitable for unibyte text if IT->string is unibyte. */
3448 if (STRING_MULTIBYTE (it->string))
3449 {
3450 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3451 int rest = SBYTES (it->string) - BYTEPOS (pos);
3452 int c, len;
3453 struct face *face = FACE_FROM_ID (it->f, face_id);
3454
3455 c = string_char_and_length (p, rest, &len);
3456 face_id = FACE_FOR_CHAR (it->f, face, c);
3457 }
3458 }
3459 else
3460 {
3461 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3462 || (IT_CHARPOS (*it) <= BEGV && before_p))
3463 return it->face_id;
3464
3465 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3466 pos = it->current.pos;
3467
3468 if (before_p)
3469 DEC_TEXT_POS (pos, it->multibyte_p);
3470 else
3471 {
3472 if (it->what == IT_COMPOSITION)
3473 /* For composition, we must check the position after the
3474 composition. */
3475 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3476 else
3477 INC_TEXT_POS (pos, it->multibyte_p);
3478 }
3479
3480 /* Determine face for CHARSET_ASCII, or unibyte. */
3481 face_id = face_at_buffer_position (it->w,
3482 CHARPOS (pos),
3483 it->region_beg_charpos,
3484 it->region_end_charpos,
3485 &next_check_charpos,
3486 limit, 0);
3487
3488 /* Correct the face for charsets different from ASCII. Do it
3489 for the multibyte case only. The face returned above is
3490 suitable for unibyte text if current_buffer is unibyte. */
3491 if (it->multibyte_p)
3492 {
3493 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3494 struct face *face = FACE_FROM_ID (it->f, face_id);
3495 face_id = FACE_FOR_CHAR (it->f, face, c);
3496 }
3497 }
3498
3499 return face_id;
3500 }
3501
3502
3503 \f
3504 /***********************************************************************
3505 Invisible text
3506 ***********************************************************************/
3507
3508 /* Set up iterator IT from invisible properties at its current
3509 position. Called from handle_stop. */
3510
3511 static enum prop_handled
3512 handle_invisible_prop (it)
3513 struct it *it;
3514 {
3515 enum prop_handled handled = HANDLED_NORMALLY;
3516
3517 if (STRINGP (it->string))
3518 {
3519 extern Lisp_Object Qinvisible;
3520 Lisp_Object prop, end_charpos, limit, charpos;
3521
3522 /* Get the value of the invisible text property at the
3523 current position. Value will be nil if there is no such
3524 property. */
3525 charpos = make_number (IT_STRING_CHARPOS (*it));
3526 prop = Fget_text_property (charpos, Qinvisible, it->string);
3527
3528 if (!NILP (prop)
3529 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3530 {
3531 handled = HANDLED_RECOMPUTE_PROPS;
3532
3533 /* Get the position at which the next change of the
3534 invisible text property can be found in IT->string.
3535 Value will be nil if the property value is the same for
3536 all the rest of IT->string. */
3537 XSETINT (limit, SCHARS (it->string));
3538 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3539 it->string, limit);
3540
3541 /* Text at current position is invisible. The next
3542 change in the property is at position end_charpos.
3543 Move IT's current position to that position. */
3544 if (INTEGERP (end_charpos)
3545 && XFASTINT (end_charpos) < XFASTINT (limit))
3546 {
3547 struct text_pos old;
3548 old = it->current.string_pos;
3549 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3550 compute_string_pos (&it->current.string_pos, old, it->string);
3551 }
3552 else
3553 {
3554 /* The rest of the string is invisible. If this is an
3555 overlay string, proceed with the next overlay string
3556 or whatever comes and return a character from there. */
3557 if (it->current.overlay_string_index >= 0)
3558 {
3559 next_overlay_string (it);
3560 /* Don't check for overlay strings when we just
3561 finished processing them. */
3562 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3563 }
3564 else
3565 {
3566 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3567 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3568 }
3569 }
3570 }
3571 }
3572 else
3573 {
3574 int invis_p, newpos, next_stop, start_charpos;
3575 Lisp_Object pos, prop, overlay;
3576
3577 /* First of all, is there invisible text at this position? */
3578 start_charpos = IT_CHARPOS (*it);
3579 pos = make_number (IT_CHARPOS (*it));
3580 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3581 &overlay);
3582 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3583
3584 /* If we are on invisible text, skip over it. */
3585 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3586 {
3587 /* Record whether we have to display an ellipsis for the
3588 invisible text. */
3589 int display_ellipsis_p = invis_p == 2;
3590
3591 handled = HANDLED_RECOMPUTE_PROPS;
3592
3593 /* Loop skipping over invisible text. The loop is left at
3594 ZV or with IT on the first char being visible again. */
3595 do
3596 {
3597 /* Try to skip some invisible text. Return value is the
3598 position reached which can be equal to IT's position
3599 if there is nothing invisible here. This skips both
3600 over invisible text properties and overlays with
3601 invisible property. */
3602 newpos = skip_invisible (IT_CHARPOS (*it),
3603 &next_stop, ZV, it->window);
3604
3605 /* If we skipped nothing at all we weren't at invisible
3606 text in the first place. If everything to the end of
3607 the buffer was skipped, end the loop. */
3608 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3609 invis_p = 0;
3610 else
3611 {
3612 /* We skipped some characters but not necessarily
3613 all there are. Check if we ended up on visible
3614 text. Fget_char_property returns the property of
3615 the char before the given position, i.e. if we
3616 get invis_p = 0, this means that the char at
3617 newpos is visible. */
3618 pos = make_number (newpos);
3619 prop = Fget_char_property (pos, Qinvisible, it->window);
3620 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3621 }
3622
3623 /* If we ended up on invisible text, proceed to
3624 skip starting with next_stop. */
3625 if (invis_p)
3626 IT_CHARPOS (*it) = next_stop;
3627
3628 /* If there are adjacent invisible texts, don't lose the
3629 second one's ellipsis. */
3630 if (invis_p == 2)
3631 display_ellipsis_p = 1;
3632 }
3633 while (invis_p);
3634
3635 /* The position newpos is now either ZV or on visible text. */
3636 IT_CHARPOS (*it) = newpos;
3637 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3638
3639 /* If there are before-strings at the start of invisible
3640 text, and the text is invisible because of a text
3641 property, arrange to show before-strings because 20.x did
3642 it that way. (If the text is invisible because of an
3643 overlay property instead of a text property, this is
3644 already handled in the overlay code.) */
3645 if (NILP (overlay)
3646 && get_overlay_strings (it, start_charpos))
3647 {
3648 handled = HANDLED_RECOMPUTE_PROPS;
3649 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3650 }
3651 else if (display_ellipsis_p)
3652 {
3653 /* Make sure that the glyphs of the ellipsis will get
3654 correct `charpos' values. If we would not update
3655 it->position here, the glyphs would belong to the
3656 last visible character _before_ the invisible
3657 text, which confuses `set_cursor_from_row'.
3658
3659 We use the last invisible position instead of the
3660 first because this way the cursor is always drawn on
3661 the first "." of the ellipsis, whenever PT is inside
3662 the invisible text. Otherwise the cursor would be
3663 placed _after_ the ellipsis when the point is after the
3664 first invisible character. */
3665 if (!STRINGP (it->object))
3666 {
3667 it->position.charpos = IT_CHARPOS (*it) - 1;
3668 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3669 }
3670 setup_for_ellipsis (it, 0);
3671 }
3672 }
3673 }
3674
3675 return handled;
3676 }
3677
3678
3679 /* Make iterator IT return `...' next.
3680 Replaces LEN characters from buffer. */
3681
3682 static void
3683 setup_for_ellipsis (it, len)
3684 struct it *it;
3685 int len;
3686 {
3687 /* Use the display table definition for `...'. Invalid glyphs
3688 will be handled by the method returning elements from dpvec. */
3689 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3690 {
3691 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3692 it->dpvec = v->contents;
3693 it->dpend = v->contents + v->size;
3694 }
3695 else
3696 {
3697 /* Default `...'. */
3698 it->dpvec = default_invis_vector;
3699 it->dpend = default_invis_vector + 3;
3700 }
3701
3702 it->dpvec_char_len = len;
3703 it->current.dpvec_index = 0;
3704 it->dpvec_face_id = -1;
3705
3706 /* Remember the current face id in case glyphs specify faces.
3707 IT's face is restored in set_iterator_to_next.
3708 saved_face_id was set to preceding char's face in handle_stop. */
3709 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3710 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3711
3712 it->method = GET_FROM_DISPLAY_VECTOR;
3713 it->ellipsis_p = 1;
3714 }
3715
3716
3717 \f
3718 /***********************************************************************
3719 'display' property
3720 ***********************************************************************/
3721
3722 /* Set up iterator IT from `display' property at its current position.
3723 Called from handle_stop.
3724 We return HANDLED_RETURN if some part of the display property
3725 overrides the display of the buffer text itself.
3726 Otherwise we return HANDLED_NORMALLY. */
3727
3728 static enum prop_handled
3729 handle_display_prop (it)
3730 struct it *it;
3731 {
3732 Lisp_Object prop, object;
3733 struct text_pos *position;
3734 /* Nonzero if some property replaces the display of the text itself. */
3735 int display_replaced_p = 0;
3736
3737 if (STRINGP (it->string))
3738 {
3739 object = it->string;
3740 position = &it->current.string_pos;
3741 }
3742 else
3743 {
3744 XSETWINDOW (object, it->w);
3745 position = &it->current.pos;
3746 }
3747
3748 /* Reset those iterator values set from display property values. */
3749 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3750 it->space_width = Qnil;
3751 it->font_height = Qnil;
3752 it->voffset = 0;
3753
3754 /* We don't support recursive `display' properties, i.e. string
3755 values that have a string `display' property, that have a string
3756 `display' property etc. */
3757 if (!it->string_from_display_prop_p)
3758 it->area = TEXT_AREA;
3759
3760 prop = Fget_char_property (make_number (position->charpos),
3761 Qdisplay, object);
3762 if (NILP (prop))
3763 return HANDLED_NORMALLY;
3764
3765 if (!STRINGP (it->string))
3766 object = it->w->buffer;
3767
3768 if (CONSP (prop)
3769 /* Simple properties. */
3770 && !EQ (XCAR (prop), Qimage)
3771 && !EQ (XCAR (prop), Qspace)
3772 && !EQ (XCAR (prop), Qwhen)
3773 && !EQ (XCAR (prop), Qslice)
3774 && !EQ (XCAR (prop), Qspace_width)
3775 && !EQ (XCAR (prop), Qheight)
3776 && !EQ (XCAR (prop), Qraise)
3777 /* Marginal area specifications. */
3778 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3779 && !EQ (XCAR (prop), Qleft_fringe)
3780 && !EQ (XCAR (prop), Qright_fringe)
3781 && !NILP (XCAR (prop)))
3782 {
3783 for (; CONSP (prop); prop = XCDR (prop))
3784 {
3785 if (handle_single_display_spec (it, XCAR (prop), object,
3786 position, display_replaced_p))
3787 display_replaced_p = 1;
3788 }
3789 }
3790 else if (VECTORP (prop))
3791 {
3792 int i;
3793 for (i = 0; i < ASIZE (prop); ++i)
3794 if (handle_single_display_spec (it, AREF (prop, i), object,
3795 position, display_replaced_p))
3796 display_replaced_p = 1;
3797 }
3798 else
3799 {
3800 int ret = handle_single_display_spec (it, prop, object, position, 0);
3801 if (ret < 0) /* Replaced by "", i.e. nothing. */
3802 return HANDLED_RECOMPUTE_PROPS;
3803 if (ret)
3804 display_replaced_p = 1;
3805 }
3806
3807 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3808 }
3809
3810
3811 /* Value is the position of the end of the `display' property starting
3812 at START_POS in OBJECT. */
3813
3814 static struct text_pos
3815 display_prop_end (it, object, start_pos)
3816 struct it *it;
3817 Lisp_Object object;
3818 struct text_pos start_pos;
3819 {
3820 Lisp_Object end;
3821 struct text_pos end_pos;
3822
3823 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3824 Qdisplay, object, Qnil);
3825 CHARPOS (end_pos) = XFASTINT (end);
3826 if (STRINGP (object))
3827 compute_string_pos (&end_pos, start_pos, it->string);
3828 else
3829 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3830
3831 return end_pos;
3832 }
3833
3834
3835 /* Set up IT from a single `display' specification PROP. OBJECT
3836 is the object in which the `display' property was found. *POSITION
3837 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3838 means that we previously saw a display specification which already
3839 replaced text display with something else, for example an image;
3840 we ignore such properties after the first one has been processed.
3841
3842 If PROP is a `space' or `image' specification, and in some other
3843 cases too, set *POSITION to the position where the `display'
3844 property ends.
3845
3846 Value is non-zero if something was found which replaces the display
3847 of buffer or string text. Specifically, the value is -1 if that
3848 "something" is "nothing". */
3849
3850 static int
3851 handle_single_display_spec (it, spec, object, position,
3852 display_replaced_before_p)
3853 struct it *it;
3854 Lisp_Object spec;
3855 Lisp_Object object;
3856 struct text_pos *position;
3857 int display_replaced_before_p;
3858 {
3859 Lisp_Object form;
3860 Lisp_Object location, value;
3861 struct text_pos start_pos;
3862 int valid_p;
3863
3864 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3865 If the result is non-nil, use VALUE instead of SPEC. */
3866 form = Qt;
3867 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3868 {
3869 spec = XCDR (spec);
3870 if (!CONSP (spec))
3871 return 0;
3872 form = XCAR (spec);
3873 spec = XCDR (spec);
3874 }
3875
3876 if (!NILP (form) && !EQ (form, Qt))
3877 {
3878 int count = SPECPDL_INDEX ();
3879 struct gcpro gcpro1;
3880
3881 /* Bind `object' to the object having the `display' property, a
3882 buffer or string. Bind `position' to the position in the
3883 object where the property was found, and `buffer-position'
3884 to the current position in the buffer. */
3885 specbind (Qobject, object);
3886 specbind (Qposition, make_number (CHARPOS (*position)));
3887 specbind (Qbuffer_position,
3888 make_number (STRINGP (object)
3889 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3890 GCPRO1 (form);
3891 form = safe_eval (form);
3892 UNGCPRO;
3893 unbind_to (count, Qnil);
3894 }
3895
3896 if (NILP (form))
3897 return 0;
3898
3899 /* Handle `(height HEIGHT)' specifications. */
3900 if (CONSP (spec)
3901 && EQ (XCAR (spec), Qheight)
3902 && CONSP (XCDR (spec)))
3903 {
3904 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3905 return 0;
3906
3907 it->font_height = XCAR (XCDR (spec));
3908 if (!NILP (it->font_height))
3909 {
3910 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3911 int new_height = -1;
3912
3913 if (CONSP (it->font_height)
3914 && (EQ (XCAR (it->font_height), Qplus)
3915 || EQ (XCAR (it->font_height), Qminus))
3916 && CONSP (XCDR (it->font_height))
3917 && INTEGERP (XCAR (XCDR (it->font_height))))
3918 {
3919 /* `(+ N)' or `(- N)' where N is an integer. */
3920 int steps = XINT (XCAR (XCDR (it->font_height)));
3921 if (EQ (XCAR (it->font_height), Qplus))
3922 steps = - steps;
3923 it->face_id = smaller_face (it->f, it->face_id, steps);
3924 }
3925 else if (FUNCTIONP (it->font_height))
3926 {
3927 /* Call function with current height as argument.
3928 Value is the new height. */
3929 Lisp_Object height;
3930 height = safe_call1 (it->font_height,
3931 face->lface[LFACE_HEIGHT_INDEX]);
3932 if (NUMBERP (height))
3933 new_height = XFLOATINT (height);
3934 }
3935 else if (NUMBERP (it->font_height))
3936 {
3937 /* Value is a multiple of the canonical char height. */
3938 struct face *face;
3939
3940 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3941 new_height = (XFLOATINT (it->font_height)
3942 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3943 }
3944 else
3945 {
3946 /* Evaluate IT->font_height with `height' bound to the
3947 current specified height to get the new height. */
3948 int count = SPECPDL_INDEX ();
3949
3950 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3951 value = safe_eval (it->font_height);
3952 unbind_to (count, Qnil);
3953
3954 if (NUMBERP (value))
3955 new_height = XFLOATINT (value);
3956 }
3957
3958 if (new_height > 0)
3959 it->face_id = face_with_height (it->f, it->face_id, new_height);
3960 }
3961
3962 return 0;
3963 }
3964
3965 /* Handle `(space_width WIDTH)'. */
3966 if (CONSP (spec)
3967 && EQ (XCAR (spec), Qspace_width)
3968 && CONSP (XCDR (spec)))
3969 {
3970 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3971 return 0;
3972
3973 value = XCAR (XCDR (spec));
3974 if (NUMBERP (value) && XFLOATINT (value) > 0)
3975 it->space_width = value;
3976
3977 return 0;
3978 }
3979
3980 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3981 if (CONSP (spec)
3982 && EQ (XCAR (spec), Qslice))
3983 {
3984 Lisp_Object tem;
3985
3986 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3987 return 0;
3988
3989 if (tem = XCDR (spec), CONSP (tem))
3990 {
3991 it->slice.x = XCAR (tem);
3992 if (tem = XCDR (tem), CONSP (tem))
3993 {
3994 it->slice.y = XCAR (tem);
3995 if (tem = XCDR (tem), CONSP (tem))
3996 {
3997 it->slice.width = XCAR (tem);
3998 if (tem = XCDR (tem), CONSP (tem))
3999 it->slice.height = XCAR (tem);
4000 }
4001 }
4002 }
4003
4004 return 0;
4005 }
4006
4007 /* Handle `(raise FACTOR)'. */
4008 if (CONSP (spec)
4009 && EQ (XCAR (spec), Qraise)
4010 && CONSP (XCDR (spec)))
4011 {
4012 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4013 return 0;
4014
4015 #ifdef HAVE_WINDOW_SYSTEM
4016 value = XCAR (XCDR (spec));
4017 if (NUMBERP (value))
4018 {
4019 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4020 it->voffset = - (XFLOATINT (value)
4021 * (FONT_HEIGHT (face->font)));
4022 }
4023 #endif /* HAVE_WINDOW_SYSTEM */
4024
4025 return 0;
4026 }
4027
4028 /* Don't handle the other kinds of display specifications
4029 inside a string that we got from a `display' property. */
4030 if (it->string_from_display_prop_p)
4031 return 0;
4032
4033 /* Characters having this form of property are not displayed, so
4034 we have to find the end of the property. */
4035 start_pos = *position;
4036 *position = display_prop_end (it, object, start_pos);
4037 value = Qnil;
4038
4039 /* Stop the scan at that end position--we assume that all
4040 text properties change there. */
4041 it->stop_charpos = position->charpos;
4042
4043 /* Handle `(left-fringe BITMAP [FACE])'
4044 and `(right-fringe BITMAP [FACE])'. */
4045 if (CONSP (spec)
4046 && (EQ (XCAR (spec), Qleft_fringe)
4047 || EQ (XCAR (spec), Qright_fringe))
4048 && CONSP (XCDR (spec)))
4049 {
4050 int face_id = DEFAULT_FACE_ID;
4051 int fringe_bitmap;
4052
4053 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4054 /* If we return here, POSITION has been advanced
4055 across the text with this property. */
4056 return 0;
4057
4058 #ifdef HAVE_WINDOW_SYSTEM
4059 value = XCAR (XCDR (spec));
4060 if (!SYMBOLP (value)
4061 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4062 /* If we return here, POSITION has been advanced
4063 across the text with this property. */
4064 return 0;
4065
4066 if (CONSP (XCDR (XCDR (spec))))
4067 {
4068 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4069 int face_id2 = lookup_derived_face (it->f, face_name,
4070 'A', FRINGE_FACE_ID, 0);
4071 if (face_id2 >= 0)
4072 face_id = face_id2;
4073 }
4074
4075 /* Save current settings of IT so that we can restore them
4076 when we are finished with the glyph property value. */
4077
4078 push_it (it);
4079
4080 it->area = TEXT_AREA;
4081 it->what = IT_IMAGE;
4082 it->image_id = -1; /* no image */
4083 it->position = start_pos;
4084 it->object = NILP (object) ? it->w->buffer : object;
4085 it->method = GET_FROM_IMAGE;
4086 it->face_id = face_id;
4087
4088 /* Say that we haven't consumed the characters with
4089 `display' property yet. The call to pop_it in
4090 set_iterator_to_next will clean this up. */
4091 *position = start_pos;
4092
4093 if (EQ (XCAR (spec), Qleft_fringe))
4094 {
4095 it->left_user_fringe_bitmap = fringe_bitmap;
4096 it->left_user_fringe_face_id = face_id;
4097 }
4098 else
4099 {
4100 it->right_user_fringe_bitmap = fringe_bitmap;
4101 it->right_user_fringe_face_id = face_id;
4102 }
4103 #endif /* HAVE_WINDOW_SYSTEM */
4104 return 1;
4105 }
4106
4107 /* Prepare to handle `((margin left-margin) ...)',
4108 `((margin right-margin) ...)' and `((margin nil) ...)'
4109 prefixes for display specifications. */
4110 location = Qunbound;
4111 if (CONSP (spec) && CONSP (XCAR (spec)))
4112 {
4113 Lisp_Object tem;
4114
4115 value = XCDR (spec);
4116 if (CONSP (value))
4117 value = XCAR (value);
4118
4119 tem = XCAR (spec);
4120 if (EQ (XCAR (tem), Qmargin)
4121 && (tem = XCDR (tem),
4122 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4123 (NILP (tem)
4124 || EQ (tem, Qleft_margin)
4125 || EQ (tem, Qright_margin))))
4126 location = tem;
4127 }
4128
4129 if (EQ (location, Qunbound))
4130 {
4131 location = Qnil;
4132 value = spec;
4133 }
4134
4135 /* After this point, VALUE is the property after any
4136 margin prefix has been stripped. It must be a string,
4137 an image specification, or `(space ...)'.
4138
4139 LOCATION specifies where to display: `left-margin',
4140 `right-margin' or nil. */
4141
4142 valid_p = (STRINGP (value)
4143 #ifdef HAVE_WINDOW_SYSTEM
4144 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4145 #endif /* not HAVE_WINDOW_SYSTEM */
4146 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4147
4148 if (valid_p && !display_replaced_before_p)
4149 {
4150 /* Save current settings of IT so that we can restore them
4151 when we are finished with the glyph property value. */
4152 push_it (it);
4153
4154 if (NILP (location))
4155 it->area = TEXT_AREA;
4156 else if (EQ (location, Qleft_margin))
4157 it->area = LEFT_MARGIN_AREA;
4158 else
4159 it->area = RIGHT_MARGIN_AREA;
4160
4161 if (STRINGP (value))
4162 {
4163 if (SCHARS (value) == 0)
4164 {
4165 pop_it (it);
4166 return -1; /* Replaced by "", i.e. nothing. */
4167 }
4168 it->string = value;
4169 it->multibyte_p = STRING_MULTIBYTE (it->string);
4170 it->current.overlay_string_index = -1;
4171 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4172 it->end_charpos = it->string_nchars = SCHARS (it->string);
4173 it->method = GET_FROM_STRING;
4174 it->stop_charpos = 0;
4175 it->string_from_display_prop_p = 1;
4176 /* Say that we haven't consumed the characters with
4177 `display' property yet. The call to pop_it in
4178 set_iterator_to_next will clean this up. */
4179 *position = start_pos;
4180 }
4181 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4182 {
4183 it->method = GET_FROM_STRETCH;
4184 it->object = value;
4185 *position = it->position = start_pos;
4186 }
4187 #ifdef HAVE_WINDOW_SYSTEM
4188 else
4189 {
4190 it->what = IT_IMAGE;
4191 it->image_id = lookup_image (it->f, value);
4192 it->position = start_pos;
4193 it->object = NILP (object) ? it->w->buffer : object;
4194 it->method = GET_FROM_IMAGE;
4195
4196 /* Say that we haven't consumed the characters with
4197 `display' property yet. The call to pop_it in
4198 set_iterator_to_next will clean this up. */
4199 *position = start_pos;
4200 }
4201 #endif /* HAVE_WINDOW_SYSTEM */
4202
4203 return 1;
4204 }
4205
4206 /* Invalid property or property not supported. Restore
4207 POSITION to what it was before. */
4208 *position = start_pos;
4209 return 0;
4210 }
4211
4212
4213 /* Check if SPEC is a display specification value whose text should be
4214 treated as intangible. */
4215
4216 static int
4217 single_display_spec_intangible_p (prop)
4218 Lisp_Object prop;
4219 {
4220 /* Skip over `when FORM'. */
4221 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4222 {
4223 prop = XCDR (prop);
4224 if (!CONSP (prop))
4225 return 0;
4226 prop = XCDR (prop);
4227 }
4228
4229 if (STRINGP (prop))
4230 return 1;
4231
4232 if (!CONSP (prop))
4233 return 0;
4234
4235 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4236 we don't need to treat text as intangible. */
4237 if (EQ (XCAR (prop), Qmargin))
4238 {
4239 prop = XCDR (prop);
4240 if (!CONSP (prop))
4241 return 0;
4242
4243 prop = XCDR (prop);
4244 if (!CONSP (prop)
4245 || EQ (XCAR (prop), Qleft_margin)
4246 || EQ (XCAR (prop), Qright_margin))
4247 return 0;
4248 }
4249
4250 return (CONSP (prop)
4251 && (EQ (XCAR (prop), Qimage)
4252 || EQ (XCAR (prop), Qspace)));
4253 }
4254
4255
4256 /* Check if PROP is a display property value whose text should be
4257 treated as intangible. */
4258
4259 int
4260 display_prop_intangible_p (prop)
4261 Lisp_Object prop;
4262 {
4263 if (CONSP (prop)
4264 && CONSP (XCAR (prop))
4265 && !EQ (Qmargin, XCAR (XCAR (prop))))
4266 {
4267 /* A list of sub-properties. */
4268 while (CONSP (prop))
4269 {
4270 if (single_display_spec_intangible_p (XCAR (prop)))
4271 return 1;
4272 prop = XCDR (prop);
4273 }
4274 }
4275 else if (VECTORP (prop))
4276 {
4277 /* A vector of sub-properties. */
4278 int i;
4279 for (i = 0; i < ASIZE (prop); ++i)
4280 if (single_display_spec_intangible_p (AREF (prop, i)))
4281 return 1;
4282 }
4283 else
4284 return single_display_spec_intangible_p (prop);
4285
4286 return 0;
4287 }
4288
4289
4290 /* Return 1 if PROP is a display sub-property value containing STRING. */
4291
4292 static int
4293 single_display_spec_string_p (prop, string)
4294 Lisp_Object prop, string;
4295 {
4296 if (EQ (string, prop))
4297 return 1;
4298
4299 /* Skip over `when FORM'. */
4300 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4301 {
4302 prop = XCDR (prop);
4303 if (!CONSP (prop))
4304 return 0;
4305 prop = XCDR (prop);
4306 }
4307
4308 if (CONSP (prop))
4309 /* Skip over `margin LOCATION'. */
4310 if (EQ (XCAR (prop), Qmargin))
4311 {
4312 prop = XCDR (prop);
4313 if (!CONSP (prop))
4314 return 0;
4315
4316 prop = XCDR (prop);
4317 if (!CONSP (prop))
4318 return 0;
4319 }
4320
4321 return CONSP (prop) && EQ (XCAR (prop), string);
4322 }
4323
4324
4325 /* Return 1 if STRING appears in the `display' property PROP. */
4326
4327 static int
4328 display_prop_string_p (prop, string)
4329 Lisp_Object prop, string;
4330 {
4331 if (CONSP (prop)
4332 && CONSP (XCAR (prop))
4333 && !EQ (Qmargin, XCAR (XCAR (prop))))
4334 {
4335 /* A list of sub-properties. */
4336 while (CONSP (prop))
4337 {
4338 if (single_display_spec_string_p (XCAR (prop), string))
4339 return 1;
4340 prop = XCDR (prop);
4341 }
4342 }
4343 else if (VECTORP (prop))
4344 {
4345 /* A vector of sub-properties. */
4346 int i;
4347 for (i = 0; i < ASIZE (prop); ++i)
4348 if (single_display_spec_string_p (AREF (prop, i), string))
4349 return 1;
4350 }
4351 else
4352 return single_display_spec_string_p (prop, string);
4353
4354 return 0;
4355 }
4356
4357
4358 /* Determine from which buffer position in W's buffer STRING comes
4359 from. AROUND_CHARPOS is an approximate position where it could
4360 be from. Value is the buffer position or 0 if it couldn't be
4361 determined.
4362
4363 W's buffer must be current.
4364
4365 This function is necessary because we don't record buffer positions
4366 in glyphs generated from strings (to keep struct glyph small).
4367 This function may only use code that doesn't eval because it is
4368 called asynchronously from note_mouse_highlight. */
4369
4370 int
4371 string_buffer_position (w, string, around_charpos)
4372 struct window *w;
4373 Lisp_Object string;
4374 int around_charpos;
4375 {
4376 Lisp_Object limit, prop, pos;
4377 const int MAX_DISTANCE = 1000;
4378 int found = 0;
4379
4380 pos = make_number (around_charpos);
4381 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4382 while (!found && !EQ (pos, limit))
4383 {
4384 prop = Fget_char_property (pos, Qdisplay, Qnil);
4385 if (!NILP (prop) && display_prop_string_p (prop, string))
4386 found = 1;
4387 else
4388 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4389 }
4390
4391 if (!found)
4392 {
4393 pos = make_number (around_charpos);
4394 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4395 while (!found && !EQ (pos, limit))
4396 {
4397 prop = Fget_char_property (pos, Qdisplay, Qnil);
4398 if (!NILP (prop) && display_prop_string_p (prop, string))
4399 found = 1;
4400 else
4401 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4402 limit);
4403 }
4404 }
4405
4406 return found ? XINT (pos) : 0;
4407 }
4408
4409
4410 \f
4411 /***********************************************************************
4412 `composition' property
4413 ***********************************************************************/
4414
4415 /* Set up iterator IT from `composition' property at its current
4416 position. Called from handle_stop. */
4417
4418 static enum prop_handled
4419 handle_composition_prop (it)
4420 struct it *it;
4421 {
4422 Lisp_Object prop, string;
4423 int pos, pos_byte, end;
4424 enum prop_handled handled = HANDLED_NORMALLY;
4425
4426 if (STRINGP (it->string))
4427 {
4428 pos = IT_STRING_CHARPOS (*it);
4429 pos_byte = IT_STRING_BYTEPOS (*it);
4430 string = it->string;
4431 }
4432 else
4433 {
4434 pos = IT_CHARPOS (*it);
4435 pos_byte = IT_BYTEPOS (*it);
4436 string = Qnil;
4437 }
4438
4439 /* If there's a valid composition and point is not inside of the
4440 composition (in the case that the composition is from the current
4441 buffer), draw a glyph composed from the composition components. */
4442 if (find_composition (pos, -1, &pos, &end, &prop, string)
4443 && COMPOSITION_VALID_P (pos, end, prop)
4444 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4445 {
4446 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4447
4448 if (id >= 0)
4449 {
4450 struct composition *cmp = composition_table[id];
4451
4452 if (cmp->glyph_len == 0)
4453 {
4454 /* No glyph. */
4455 if (STRINGP (it->string))
4456 {
4457 IT_STRING_CHARPOS (*it) = end;
4458 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4459 end);
4460 }
4461 else
4462 {
4463 IT_CHARPOS (*it) = end;
4464 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4465 }
4466 return HANDLED_RECOMPUTE_PROPS;
4467 }
4468 it->method = GET_FROM_COMPOSITION;
4469 it->cmp_id = id;
4470 it->cmp_len = COMPOSITION_LENGTH (prop);
4471 /* For a terminal, draw only the first character of the
4472 components. */
4473 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4474 it->len = (STRINGP (it->string)
4475 ? string_char_to_byte (it->string, end)
4476 : CHAR_TO_BYTE (end)) - pos_byte;
4477 it->stop_charpos = end;
4478 handled = HANDLED_RETURN;
4479 }
4480 }
4481
4482 return handled;
4483 }
4484
4485
4486 \f
4487 /***********************************************************************
4488 Overlay strings
4489 ***********************************************************************/
4490
4491 /* The following structure is used to record overlay strings for
4492 later sorting in load_overlay_strings. */
4493
4494 struct overlay_entry
4495 {
4496 Lisp_Object overlay;
4497 Lisp_Object string;
4498 int priority;
4499 int after_string_p;
4500 };
4501
4502
4503 /* Set up iterator IT from overlay strings at its current position.
4504 Called from handle_stop. */
4505
4506 static enum prop_handled
4507 handle_overlay_change (it)
4508 struct it *it;
4509 {
4510 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4511 return HANDLED_RECOMPUTE_PROPS;
4512 else
4513 return HANDLED_NORMALLY;
4514 }
4515
4516
4517 /* Set up the next overlay string for delivery by IT, if there is an
4518 overlay string to deliver. Called by set_iterator_to_next when the
4519 end of the current overlay string is reached. If there are more
4520 overlay strings to display, IT->string and
4521 IT->current.overlay_string_index are set appropriately here.
4522 Otherwise IT->string is set to nil. */
4523
4524 static void
4525 next_overlay_string (it)
4526 struct it *it;
4527 {
4528 ++it->current.overlay_string_index;
4529 if (it->current.overlay_string_index == it->n_overlay_strings)
4530 {
4531 /* No more overlay strings. Restore IT's settings to what
4532 they were before overlay strings were processed, and
4533 continue to deliver from current_buffer. */
4534 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4535
4536 pop_it (it);
4537 xassert (it->stop_charpos >= BEGV
4538 && it->stop_charpos <= it->end_charpos);
4539 it->string = Qnil;
4540 it->current.overlay_string_index = -1;
4541 SET_TEXT_POS (it->current.string_pos, -1, -1);
4542 it->n_overlay_strings = 0;
4543 it->method = GET_FROM_BUFFER;
4544
4545 /* If we're at the end of the buffer, record that we have
4546 processed the overlay strings there already, so that
4547 next_element_from_buffer doesn't try it again. */
4548 if (IT_CHARPOS (*it) >= it->end_charpos)
4549 it->overlay_strings_at_end_processed_p = 1;
4550
4551 /* If we have to display `...' for invisible text, set
4552 the iterator up for that. */
4553 if (display_ellipsis_p)
4554 setup_for_ellipsis (it, 0);
4555 }
4556 else
4557 {
4558 /* There are more overlay strings to process. If
4559 IT->current.overlay_string_index has advanced to a position
4560 where we must load IT->overlay_strings with more strings, do
4561 it. */
4562 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4563
4564 if (it->current.overlay_string_index && i == 0)
4565 load_overlay_strings (it, 0);
4566
4567 /* Initialize IT to deliver display elements from the overlay
4568 string. */
4569 it->string = it->overlay_strings[i];
4570 it->multibyte_p = STRING_MULTIBYTE (it->string);
4571 SET_TEXT_POS (it->current.string_pos, 0, 0);
4572 it->method = GET_FROM_STRING;
4573 it->stop_charpos = 0;
4574 }
4575
4576 CHECK_IT (it);
4577 }
4578
4579
4580 /* Compare two overlay_entry structures E1 and E2. Used as a
4581 comparison function for qsort in load_overlay_strings. Overlay
4582 strings for the same position are sorted so that
4583
4584 1. All after-strings come in front of before-strings, except
4585 when they come from the same overlay.
4586
4587 2. Within after-strings, strings are sorted so that overlay strings
4588 from overlays with higher priorities come first.
4589
4590 2. Within before-strings, strings are sorted so that overlay
4591 strings from overlays with higher priorities come last.
4592
4593 Value is analogous to strcmp. */
4594
4595
4596 static int
4597 compare_overlay_entries (e1, e2)
4598 void *e1, *e2;
4599 {
4600 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4601 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4602 int result;
4603
4604 if (entry1->after_string_p != entry2->after_string_p)
4605 {
4606 /* Let after-strings appear in front of before-strings if
4607 they come from different overlays. */
4608 if (EQ (entry1->overlay, entry2->overlay))
4609 result = entry1->after_string_p ? 1 : -1;
4610 else
4611 result = entry1->after_string_p ? -1 : 1;
4612 }
4613 else if (entry1->after_string_p)
4614 /* After-strings sorted in order of decreasing priority. */
4615 result = entry2->priority - entry1->priority;
4616 else
4617 /* Before-strings sorted in order of increasing priority. */
4618 result = entry1->priority - entry2->priority;
4619
4620 return result;
4621 }
4622
4623
4624 /* Load the vector IT->overlay_strings with overlay strings from IT's
4625 current buffer position, or from CHARPOS if that is > 0. Set
4626 IT->n_overlays to the total number of overlay strings found.
4627
4628 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4629 a time. On entry into load_overlay_strings,
4630 IT->current.overlay_string_index gives the number of overlay
4631 strings that have already been loaded by previous calls to this
4632 function.
4633
4634 IT->add_overlay_start contains an additional overlay start
4635 position to consider for taking overlay strings from, if non-zero.
4636 This position comes into play when the overlay has an `invisible'
4637 property, and both before and after-strings. When we've skipped to
4638 the end of the overlay, because of its `invisible' property, we
4639 nevertheless want its before-string to appear.
4640 IT->add_overlay_start will contain the overlay start position
4641 in this case.
4642
4643 Overlay strings are sorted so that after-string strings come in
4644 front of before-string strings. Within before and after-strings,
4645 strings are sorted by overlay priority. See also function
4646 compare_overlay_entries. */
4647
4648 static void
4649 load_overlay_strings (it, charpos)
4650 struct it *it;
4651 int charpos;
4652 {
4653 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4654 Lisp_Object overlay, window, str, invisible;
4655 struct Lisp_Overlay *ov;
4656 int start, end;
4657 int size = 20;
4658 int n = 0, i, j, invis_p;
4659 struct overlay_entry *entries
4660 = (struct overlay_entry *) alloca (size * sizeof *entries);
4661
4662 if (charpos <= 0)
4663 charpos = IT_CHARPOS (*it);
4664
4665 /* Append the overlay string STRING of overlay OVERLAY to vector
4666 `entries' which has size `size' and currently contains `n'
4667 elements. AFTER_P non-zero means STRING is an after-string of
4668 OVERLAY. */
4669 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4670 do \
4671 { \
4672 Lisp_Object priority; \
4673 \
4674 if (n == size) \
4675 { \
4676 int new_size = 2 * size; \
4677 struct overlay_entry *old = entries; \
4678 entries = \
4679 (struct overlay_entry *) alloca (new_size \
4680 * sizeof *entries); \
4681 bcopy (old, entries, size * sizeof *entries); \
4682 size = new_size; \
4683 } \
4684 \
4685 entries[n].string = (STRING); \
4686 entries[n].overlay = (OVERLAY); \
4687 priority = Foverlay_get ((OVERLAY), Qpriority); \
4688 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4689 entries[n].after_string_p = (AFTER_P); \
4690 ++n; \
4691 } \
4692 while (0)
4693
4694 /* Process overlay before the overlay center. */
4695 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4696 {
4697 XSETMISC (overlay, ov);
4698 xassert (OVERLAYP (overlay));
4699 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4700 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4701
4702 if (end < charpos)
4703 break;
4704
4705 /* Skip this overlay if it doesn't start or end at IT's current
4706 position. */
4707 if (end != charpos && start != charpos)
4708 continue;
4709
4710 /* Skip this overlay if it doesn't apply to IT->w. */
4711 window = Foverlay_get (overlay, Qwindow);
4712 if (WINDOWP (window) && XWINDOW (window) != it->w)
4713 continue;
4714
4715 /* If the text ``under'' the overlay is invisible, both before-
4716 and after-strings from this overlay are visible; start and
4717 end position are indistinguishable. */
4718 invisible = Foverlay_get (overlay, Qinvisible);
4719 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4720
4721 /* If overlay has a non-empty before-string, record it. */
4722 if ((start == charpos || (end == charpos && invis_p))
4723 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4724 && SCHARS (str))
4725 RECORD_OVERLAY_STRING (overlay, str, 0);
4726
4727 /* If overlay has a non-empty after-string, record it. */
4728 if ((end == charpos || (start == charpos && invis_p))
4729 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4730 && SCHARS (str))
4731 RECORD_OVERLAY_STRING (overlay, str, 1);
4732 }
4733
4734 /* Process overlays after the overlay center. */
4735 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4736 {
4737 XSETMISC (overlay, ov);
4738 xassert (OVERLAYP (overlay));
4739 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4740 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4741
4742 if (start > charpos)
4743 break;
4744
4745 /* Skip this overlay if it doesn't start or end at IT's current
4746 position. */
4747 if (end != charpos && start != charpos)
4748 continue;
4749
4750 /* Skip this overlay if it doesn't apply to IT->w. */
4751 window = Foverlay_get (overlay, Qwindow);
4752 if (WINDOWP (window) && XWINDOW (window) != it->w)
4753 continue;
4754
4755 /* If the text ``under'' the overlay is invisible, it has a zero
4756 dimension, and both before- and after-strings apply. */
4757 invisible = Foverlay_get (overlay, Qinvisible);
4758 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4759
4760 /* If overlay has a non-empty before-string, record it. */
4761 if ((start == charpos || (end == charpos && invis_p))
4762 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4763 && SCHARS (str))
4764 RECORD_OVERLAY_STRING (overlay, str, 0);
4765
4766 /* If overlay has a non-empty after-string, record it. */
4767 if ((end == charpos || (start == charpos && invis_p))
4768 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4769 && SCHARS (str))
4770 RECORD_OVERLAY_STRING (overlay, str, 1);
4771 }
4772
4773 #undef RECORD_OVERLAY_STRING
4774
4775 /* Sort entries. */
4776 if (n > 1)
4777 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4778
4779 /* Record the total number of strings to process. */
4780 it->n_overlay_strings = n;
4781
4782 /* IT->current.overlay_string_index is the number of overlay strings
4783 that have already been consumed by IT. Copy some of the
4784 remaining overlay strings to IT->overlay_strings. */
4785 i = 0;
4786 j = it->current.overlay_string_index;
4787 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4788 it->overlay_strings[i++] = entries[j++].string;
4789
4790 CHECK_IT (it);
4791 }
4792
4793
4794 /* Get the first chunk of overlay strings at IT's current buffer
4795 position, or at CHARPOS if that is > 0. Value is non-zero if at
4796 least one overlay string was found. */
4797
4798 static int
4799 get_overlay_strings (it, charpos)
4800 struct it *it;
4801 int charpos;
4802 {
4803 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4804 process. This fills IT->overlay_strings with strings, and sets
4805 IT->n_overlay_strings to the total number of strings to process.
4806 IT->pos.overlay_string_index has to be set temporarily to zero
4807 because load_overlay_strings needs this; it must be set to -1
4808 when no overlay strings are found because a zero value would
4809 indicate a position in the first overlay string. */
4810 it->current.overlay_string_index = 0;
4811 load_overlay_strings (it, charpos);
4812
4813 /* If we found overlay strings, set up IT to deliver display
4814 elements from the first one. Otherwise set up IT to deliver
4815 from current_buffer. */
4816 if (it->n_overlay_strings)
4817 {
4818 /* Make sure we know settings in current_buffer, so that we can
4819 restore meaningful values when we're done with the overlay
4820 strings. */
4821 compute_stop_pos (it);
4822 xassert (it->face_id >= 0);
4823
4824 /* Save IT's settings. They are restored after all overlay
4825 strings have been processed. */
4826 xassert (it->sp == 0);
4827 push_it (it);
4828
4829 /* Set up IT to deliver display elements from the first overlay
4830 string. */
4831 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4832 it->string = it->overlay_strings[0];
4833 it->stop_charpos = 0;
4834 xassert (STRINGP (it->string));
4835 it->end_charpos = SCHARS (it->string);
4836 it->multibyte_p = STRING_MULTIBYTE (it->string);
4837 it->method = GET_FROM_STRING;
4838 }
4839 else
4840 {
4841 it->string = Qnil;
4842 it->current.overlay_string_index = -1;
4843 it->method = GET_FROM_BUFFER;
4844 }
4845
4846 CHECK_IT (it);
4847
4848 /* Value is non-zero if we found at least one overlay string. */
4849 return STRINGP (it->string);
4850 }
4851
4852
4853 \f
4854 /***********************************************************************
4855 Saving and restoring state
4856 ***********************************************************************/
4857
4858 /* Save current settings of IT on IT->stack. Called, for example,
4859 before setting up IT for an overlay string, to be able to restore
4860 IT's settings to what they were after the overlay string has been
4861 processed. */
4862
4863 static void
4864 push_it (it)
4865 struct it *it;
4866 {
4867 struct iterator_stack_entry *p;
4868
4869 xassert (it->sp < 2);
4870 p = it->stack + it->sp;
4871
4872 p->stop_charpos = it->stop_charpos;
4873 xassert (it->face_id >= 0);
4874 p->face_id = it->face_id;
4875 p->string = it->string;
4876 p->pos = it->current;
4877 p->end_charpos = it->end_charpos;
4878 p->string_nchars = it->string_nchars;
4879 p->area = it->area;
4880 p->multibyte_p = it->multibyte_p;
4881 p->slice = it->slice;
4882 p->space_width = it->space_width;
4883 p->font_height = it->font_height;
4884 p->voffset = it->voffset;
4885 p->string_from_display_prop_p = it->string_from_display_prop_p;
4886 p->display_ellipsis_p = 0;
4887 ++it->sp;
4888 }
4889
4890
4891 /* Restore IT's settings from IT->stack. Called, for example, when no
4892 more overlay strings must be processed, and we return to delivering
4893 display elements from a buffer, or when the end of a string from a
4894 `display' property is reached and we return to delivering display
4895 elements from an overlay string, or from a buffer. */
4896
4897 static void
4898 pop_it (it)
4899 struct it *it;
4900 {
4901 struct iterator_stack_entry *p;
4902
4903 xassert (it->sp > 0);
4904 --it->sp;
4905 p = it->stack + it->sp;
4906 it->stop_charpos = p->stop_charpos;
4907 it->face_id = p->face_id;
4908 it->string = p->string;
4909 it->current = p->pos;
4910 it->end_charpos = p->end_charpos;
4911 it->string_nchars = p->string_nchars;
4912 it->area = p->area;
4913 it->multibyte_p = p->multibyte_p;
4914 it->slice = p->slice;
4915 it->space_width = p->space_width;
4916 it->font_height = p->font_height;
4917 it->voffset = p->voffset;
4918 it->string_from_display_prop_p = p->string_from_display_prop_p;
4919 }
4920
4921
4922 \f
4923 /***********************************************************************
4924 Moving over lines
4925 ***********************************************************************/
4926
4927 /* Set IT's current position to the previous line start. */
4928
4929 static void
4930 back_to_previous_line_start (it)
4931 struct it *it;
4932 {
4933 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4934 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4935 }
4936
4937
4938 /* Move IT to the next line start.
4939
4940 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4941 we skipped over part of the text (as opposed to moving the iterator
4942 continuously over the text). Otherwise, don't change the value
4943 of *SKIPPED_P.
4944
4945 Newlines may come from buffer text, overlay strings, or strings
4946 displayed via the `display' property. That's the reason we can't
4947 simply use find_next_newline_no_quit.
4948
4949 Note that this function may not skip over invisible text that is so
4950 because of text properties and immediately follows a newline. If
4951 it would, function reseat_at_next_visible_line_start, when called
4952 from set_iterator_to_next, would effectively make invisible
4953 characters following a newline part of the wrong glyph row, which
4954 leads to wrong cursor motion. */
4955
4956 static int
4957 forward_to_next_line_start (it, skipped_p)
4958 struct it *it;
4959 int *skipped_p;
4960 {
4961 int old_selective, newline_found_p, n;
4962 const int MAX_NEWLINE_DISTANCE = 500;
4963
4964 /* If already on a newline, just consume it to avoid unintended
4965 skipping over invisible text below. */
4966 if (it->what == IT_CHARACTER
4967 && it->c == '\n'
4968 && CHARPOS (it->position) == IT_CHARPOS (*it))
4969 {
4970 set_iterator_to_next (it, 0);
4971 it->c = 0;
4972 return 1;
4973 }
4974
4975 /* Don't handle selective display in the following. It's (a)
4976 unnecessary because it's done by the caller, and (b) leads to an
4977 infinite recursion because next_element_from_ellipsis indirectly
4978 calls this function. */
4979 old_selective = it->selective;
4980 it->selective = 0;
4981
4982 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4983 from buffer text. */
4984 for (n = newline_found_p = 0;
4985 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4986 n += STRINGP (it->string) ? 0 : 1)
4987 {
4988 if (!get_next_display_element (it))
4989 return 0;
4990 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4991 set_iterator_to_next (it, 0);
4992 }
4993
4994 /* If we didn't find a newline near enough, see if we can use a
4995 short-cut. */
4996 if (!newline_found_p)
4997 {
4998 int start = IT_CHARPOS (*it);
4999 int limit = find_next_newline_no_quit (start, 1);
5000 Lisp_Object pos;
5001
5002 xassert (!STRINGP (it->string));
5003
5004 /* If there isn't any `display' property in sight, and no
5005 overlays, we can just use the position of the newline in
5006 buffer text. */
5007 if (it->stop_charpos >= limit
5008 || ((pos = Fnext_single_property_change (make_number (start),
5009 Qdisplay,
5010 Qnil, make_number (limit)),
5011 NILP (pos))
5012 && next_overlay_change (start) == ZV))
5013 {
5014 IT_CHARPOS (*it) = limit;
5015 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5016 *skipped_p = newline_found_p = 1;
5017 }
5018 else
5019 {
5020 while (get_next_display_element (it)
5021 && !newline_found_p)
5022 {
5023 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5024 set_iterator_to_next (it, 0);
5025 }
5026 }
5027 }
5028
5029 it->selective = old_selective;
5030 return newline_found_p;
5031 }
5032
5033
5034 /* Set IT's current position to the previous visible line start. Skip
5035 invisible text that is so either due to text properties or due to
5036 selective display. Caution: this does not change IT->current_x and
5037 IT->hpos. */
5038
5039 static void
5040 back_to_previous_visible_line_start (it)
5041 struct it *it;
5042 {
5043 while (IT_CHARPOS (*it) > BEGV)
5044 {
5045 back_to_previous_line_start (it);
5046 if (IT_CHARPOS (*it) <= BEGV)
5047 break;
5048
5049 /* If selective > 0, then lines indented more than that values
5050 are invisible. */
5051 if (it->selective > 0
5052 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5053 (double) it->selective)) /* iftc */
5054 continue;
5055
5056 /* Check the newline before point for invisibility. */
5057 {
5058 Lisp_Object prop;
5059 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5060 Qinvisible, it->window);
5061 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5062 continue;
5063 }
5064
5065 /* If newline has a display property that replaces the newline with something
5066 else (image or text), find start of overlay or interval and continue search
5067 from that point. */
5068 if (IT_CHARPOS (*it) > BEGV)
5069 {
5070 struct it it2 = *it;
5071 int pos;
5072 int beg, end;
5073 Lisp_Object val, overlay;
5074
5075 pos = --IT_CHARPOS (it2);
5076 --IT_BYTEPOS (it2);
5077 it2.sp = 0;
5078 if (handle_display_prop (&it2) == HANDLED_RETURN
5079 && !NILP (val = get_char_property_and_overlay
5080 (make_number (pos), Qdisplay, Qnil, &overlay))
5081 && (OVERLAYP (overlay)
5082 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5083 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5084 {
5085 if (beg < BEGV)
5086 beg = BEGV;
5087 IT_CHARPOS (*it) = beg;
5088 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5089 continue;
5090 }
5091 }
5092
5093 break;
5094 }
5095
5096 xassert (IT_CHARPOS (*it) >= BEGV);
5097 xassert (IT_CHARPOS (*it) == BEGV
5098 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5099 CHECK_IT (it);
5100 }
5101
5102
5103 /* Reseat iterator IT at the previous visible line start. Skip
5104 invisible text that is so either due to text properties or due to
5105 selective display. At the end, update IT's overlay information,
5106 face information etc. */
5107
5108 void
5109 reseat_at_previous_visible_line_start (it)
5110 struct it *it;
5111 {
5112 back_to_previous_visible_line_start (it);
5113 reseat (it, it->current.pos, 1);
5114 CHECK_IT (it);
5115 }
5116
5117
5118 /* Reseat iterator IT on the next visible line start in the current
5119 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5120 preceding the line start. Skip over invisible text that is so
5121 because of selective display. Compute faces, overlays etc at the
5122 new position. Note that this function does not skip over text that
5123 is invisible because of text properties. */
5124
5125 static void
5126 reseat_at_next_visible_line_start (it, on_newline_p)
5127 struct it *it;
5128 int on_newline_p;
5129 {
5130 int newline_found_p, skipped_p = 0;
5131
5132 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5133
5134 /* Skip over lines that are invisible because they are indented
5135 more than the value of IT->selective. */
5136 if (it->selective > 0)
5137 while (IT_CHARPOS (*it) < ZV
5138 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5139 (double) it->selective)) /* iftc */
5140 {
5141 xassert (IT_BYTEPOS (*it) == BEGV
5142 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5143 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5144 }
5145
5146 /* Position on the newline if that's what's requested. */
5147 if (on_newline_p && newline_found_p)
5148 {
5149 if (STRINGP (it->string))
5150 {
5151 if (IT_STRING_CHARPOS (*it) > 0)
5152 {
5153 --IT_STRING_CHARPOS (*it);
5154 --IT_STRING_BYTEPOS (*it);
5155 }
5156 }
5157 else if (IT_CHARPOS (*it) > BEGV)
5158 {
5159 --IT_CHARPOS (*it);
5160 --IT_BYTEPOS (*it);
5161 reseat (it, it->current.pos, 0);
5162 }
5163 }
5164 else if (skipped_p)
5165 reseat (it, it->current.pos, 0);
5166
5167 CHECK_IT (it);
5168 }
5169
5170
5171 \f
5172 /***********************************************************************
5173 Changing an iterator's position
5174 ***********************************************************************/
5175
5176 /* Change IT's current position to POS in current_buffer. If FORCE_P
5177 is non-zero, always check for text properties at the new position.
5178 Otherwise, text properties are only looked up if POS >=
5179 IT->check_charpos of a property. */
5180
5181 static void
5182 reseat (it, pos, force_p)
5183 struct it *it;
5184 struct text_pos pos;
5185 int force_p;
5186 {
5187 int original_pos = IT_CHARPOS (*it);
5188
5189 reseat_1 (it, pos, 0);
5190
5191 /* Determine where to check text properties. Avoid doing it
5192 where possible because text property lookup is very expensive. */
5193 if (force_p
5194 || CHARPOS (pos) > it->stop_charpos
5195 || CHARPOS (pos) < original_pos)
5196 handle_stop (it);
5197
5198 CHECK_IT (it);
5199 }
5200
5201
5202 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5203 IT->stop_pos to POS, also. */
5204
5205 static void
5206 reseat_1 (it, pos, set_stop_p)
5207 struct it *it;
5208 struct text_pos pos;
5209 int set_stop_p;
5210 {
5211 /* Don't call this function when scanning a C string. */
5212 xassert (it->s == NULL);
5213
5214 /* POS must be a reasonable value. */
5215 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5216
5217 it->current.pos = it->position = pos;
5218 XSETBUFFER (it->object, current_buffer);
5219 it->end_charpos = ZV;
5220 it->dpvec = NULL;
5221 it->current.dpvec_index = -1;
5222 it->current.overlay_string_index = -1;
5223 IT_STRING_CHARPOS (*it) = -1;
5224 IT_STRING_BYTEPOS (*it) = -1;
5225 it->string = Qnil;
5226 it->method = GET_FROM_BUFFER;
5227 /* RMS: I added this to fix a bug in move_it_vertically_backward
5228 where it->area continued to relate to the starting point
5229 for the backward motion. Bug report from
5230 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5231 However, I am not sure whether reseat still does the right thing
5232 in general after this change. */
5233 it->area = TEXT_AREA;
5234 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5235 it->sp = 0;
5236 it->face_before_selective_p = 0;
5237
5238 if (set_stop_p)
5239 it->stop_charpos = CHARPOS (pos);
5240 }
5241
5242
5243 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5244 If S is non-null, it is a C string to iterate over. Otherwise,
5245 STRING gives a Lisp string to iterate over.
5246
5247 If PRECISION > 0, don't return more then PRECISION number of
5248 characters from the string.
5249
5250 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5251 characters have been returned. FIELD_WIDTH < 0 means an infinite
5252 field width.
5253
5254 MULTIBYTE = 0 means disable processing of multibyte characters,
5255 MULTIBYTE > 0 means enable it,
5256 MULTIBYTE < 0 means use IT->multibyte_p.
5257
5258 IT must be initialized via a prior call to init_iterator before
5259 calling this function. */
5260
5261 static void
5262 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5263 struct it *it;
5264 unsigned char *s;
5265 Lisp_Object string;
5266 int charpos;
5267 int precision, field_width, multibyte;
5268 {
5269 /* No region in strings. */
5270 it->region_beg_charpos = it->region_end_charpos = -1;
5271
5272 /* No text property checks performed by default, but see below. */
5273 it->stop_charpos = -1;
5274
5275 /* Set iterator position and end position. */
5276 bzero (&it->current, sizeof it->current);
5277 it->current.overlay_string_index = -1;
5278 it->current.dpvec_index = -1;
5279 xassert (charpos >= 0);
5280
5281 /* If STRING is specified, use its multibyteness, otherwise use the
5282 setting of MULTIBYTE, if specified. */
5283 if (multibyte >= 0)
5284 it->multibyte_p = multibyte > 0;
5285
5286 if (s == NULL)
5287 {
5288 xassert (STRINGP (string));
5289 it->string = string;
5290 it->s = NULL;
5291 it->end_charpos = it->string_nchars = SCHARS (string);
5292 it->method = GET_FROM_STRING;
5293 it->current.string_pos = string_pos (charpos, string);
5294 }
5295 else
5296 {
5297 it->s = s;
5298 it->string = Qnil;
5299
5300 /* Note that we use IT->current.pos, not it->current.string_pos,
5301 for displaying C strings. */
5302 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5303 if (it->multibyte_p)
5304 {
5305 it->current.pos = c_string_pos (charpos, s, 1);
5306 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5307 }
5308 else
5309 {
5310 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5311 it->end_charpos = it->string_nchars = strlen (s);
5312 }
5313
5314 it->method = GET_FROM_C_STRING;
5315 }
5316
5317 /* PRECISION > 0 means don't return more than PRECISION characters
5318 from the string. */
5319 if (precision > 0 && it->end_charpos - charpos > precision)
5320 it->end_charpos = it->string_nchars = charpos + precision;
5321
5322 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5323 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5324 FIELD_WIDTH < 0 means infinite field width. This is useful for
5325 padding with `-' at the end of a mode line. */
5326 if (field_width < 0)
5327 field_width = INFINITY;
5328 if (field_width > it->end_charpos - charpos)
5329 it->end_charpos = charpos + field_width;
5330
5331 /* Use the standard display table for displaying strings. */
5332 if (DISP_TABLE_P (Vstandard_display_table))
5333 it->dp = XCHAR_TABLE (Vstandard_display_table);
5334
5335 it->stop_charpos = charpos;
5336 CHECK_IT (it);
5337 }
5338
5339
5340 \f
5341 /***********************************************************************
5342 Iteration
5343 ***********************************************************************/
5344
5345 /* Map enum it_method value to corresponding next_element_from_* function. */
5346
5347 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5348 {
5349 next_element_from_buffer,
5350 next_element_from_display_vector,
5351 next_element_from_composition,
5352 next_element_from_string,
5353 next_element_from_c_string,
5354 next_element_from_image,
5355 next_element_from_stretch
5356 };
5357
5358
5359 /* Load IT's display element fields with information about the next
5360 display element from the current position of IT. Value is zero if
5361 end of buffer (or C string) is reached. */
5362
5363 static struct frame *last_escape_glyph_frame = NULL;
5364 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5365 static int last_escape_glyph_merged_face_id = 0;
5366
5367 int
5368 get_next_display_element (it)
5369 struct it *it;
5370 {
5371 /* Non-zero means that we found a display element. Zero means that
5372 we hit the end of what we iterate over. Performance note: the
5373 function pointer `method' used here turns out to be faster than
5374 using a sequence of if-statements. */
5375 int success_p;
5376
5377 get_next:
5378 success_p = (*get_next_element[it->method]) (it);
5379
5380 if (it->what == IT_CHARACTER)
5381 {
5382 /* Map via display table or translate control characters.
5383 IT->c, IT->len etc. have been set to the next character by
5384 the function call above. If we have a display table, and it
5385 contains an entry for IT->c, translate it. Don't do this if
5386 IT->c itself comes from a display table, otherwise we could
5387 end up in an infinite recursion. (An alternative could be to
5388 count the recursion depth of this function and signal an
5389 error when a certain maximum depth is reached.) Is it worth
5390 it? */
5391 if (success_p && it->dpvec == NULL)
5392 {
5393 Lisp_Object dv;
5394
5395 if (it->dp
5396 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5397 VECTORP (dv)))
5398 {
5399 struct Lisp_Vector *v = XVECTOR (dv);
5400
5401 /* Return the first character from the display table
5402 entry, if not empty. If empty, don't display the
5403 current character. */
5404 if (v->size)
5405 {
5406 it->dpvec_char_len = it->len;
5407 it->dpvec = v->contents;
5408 it->dpend = v->contents + v->size;
5409 it->current.dpvec_index = 0;
5410 it->dpvec_face_id = -1;
5411 it->saved_face_id = it->face_id;
5412 it->method = GET_FROM_DISPLAY_VECTOR;
5413 it->ellipsis_p = 0;
5414 }
5415 else
5416 {
5417 set_iterator_to_next (it, 0);
5418 }
5419 goto get_next;
5420 }
5421
5422 /* Translate control characters into `\003' or `^C' form.
5423 Control characters coming from a display table entry are
5424 currently not translated because we use IT->dpvec to hold
5425 the translation. This could easily be changed but I
5426 don't believe that it is worth doing.
5427
5428 If it->multibyte_p is nonzero, eight-bit characters and
5429 non-printable multibyte characters are also translated to
5430 octal form.
5431
5432 If it->multibyte_p is zero, eight-bit characters that
5433 don't have corresponding multibyte char code are also
5434 translated to octal form. */
5435 else if ((it->c < ' '
5436 && (it->area != TEXT_AREA
5437 /* In mode line, treat \n like other crl chars. */
5438 || (it->c != '\t'
5439 && it->glyph_row && it->glyph_row->mode_line_p)
5440 || (it->c != '\n' && it->c != '\t')))
5441 || (it->multibyte_p
5442 ? ((it->c >= 127
5443 && it->len == 1)
5444 || !CHAR_PRINTABLE_P (it->c)
5445 || (!NILP (Vnobreak_char_display)
5446 && (it->c == 0x8a0 || it->c == 0x8ad
5447 || it->c == 0x920 || it->c == 0x92d
5448 || it->c == 0xe20 || it->c == 0xe2d
5449 || it->c == 0xf20 || it->c == 0xf2d)))
5450 : (it->c >= 127
5451 && (!unibyte_display_via_language_environment
5452 || it->c == unibyte_char_to_multibyte (it->c)))))
5453 {
5454 /* IT->c is a control character which must be displayed
5455 either as '\003' or as `^C' where the '\\' and '^'
5456 can be defined in the display table. Fill
5457 IT->ctl_chars with glyphs for what we have to
5458 display. Then, set IT->dpvec to these glyphs. */
5459 GLYPH g;
5460 int ctl_len;
5461 int face_id, lface_id = 0 ;
5462 GLYPH escape_glyph;
5463
5464 /* Handle control characters with ^. */
5465
5466 if (it->c < 128 && it->ctl_arrow_p)
5467 {
5468 g = '^'; /* default glyph for Control */
5469 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5470 if (it->dp
5471 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5472 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5473 {
5474 g = XINT (DISP_CTRL_GLYPH (it->dp));
5475 lface_id = FAST_GLYPH_FACE (g);
5476 }
5477 if (lface_id)
5478 {
5479 g = FAST_GLYPH_CHAR (g);
5480 face_id = merge_faces (it->f, Qt, lface_id,
5481 it->face_id);
5482 }
5483 else if (it->f == last_escape_glyph_frame
5484 && it->face_id == last_escape_glyph_face_id)
5485 {
5486 face_id = last_escape_glyph_merged_face_id;
5487 }
5488 else
5489 {
5490 /* Merge the escape-glyph face into the current face. */
5491 face_id = merge_faces (it->f, Qescape_glyph, 0,
5492 it->face_id);
5493 last_escape_glyph_frame = it->f;
5494 last_escape_glyph_face_id = it->face_id;
5495 last_escape_glyph_merged_face_id = face_id;
5496 }
5497
5498 XSETINT (it->ctl_chars[0], g);
5499 g = it->c ^ 0100;
5500 XSETINT (it->ctl_chars[1], g);
5501 ctl_len = 2;
5502 goto display_control;
5503 }
5504
5505 /* Handle non-break space in the mode where it only gets
5506 highlighting. */
5507
5508 if (EQ (Vnobreak_char_display, Qt)
5509 && (it->c == 0x8a0 || it->c == 0x920
5510 || it->c == 0xe20 || it->c == 0xf20))
5511 {
5512 /* Merge the no-break-space face into the current face. */
5513 face_id = merge_faces (it->f, Qnobreak_space, 0,
5514 it->face_id);
5515
5516 g = it->c = ' ';
5517 XSETINT (it->ctl_chars[0], g);
5518 ctl_len = 1;
5519 goto display_control;
5520 }
5521
5522 /* Handle sequences that start with the "escape glyph". */
5523
5524 /* the default escape glyph is \. */
5525 escape_glyph = '\\';
5526
5527 if (it->dp
5528 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5529 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5530 {
5531 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5532 lface_id = FAST_GLYPH_FACE (escape_glyph);
5533 }
5534 if (lface_id)
5535 {
5536 /* The display table specified a face.
5537 Merge it into face_id and also into escape_glyph. */
5538 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5539 face_id = merge_faces (it->f, Qt, lface_id,
5540 it->face_id);
5541 }
5542 else if (it->f == last_escape_glyph_frame
5543 && it->face_id == last_escape_glyph_face_id)
5544 {
5545 face_id = last_escape_glyph_merged_face_id;
5546 }
5547 else
5548 {
5549 /* Merge the escape-glyph face into the current face. */
5550 face_id = merge_faces (it->f, Qescape_glyph, 0,
5551 it->face_id);
5552 last_escape_glyph_frame = it->f;
5553 last_escape_glyph_face_id = it->face_id;
5554 last_escape_glyph_merged_face_id = face_id;
5555 }
5556
5557 /* Handle soft hyphens in the mode where they only get
5558 highlighting. */
5559
5560 if (EQ (Vnobreak_char_display, Qt)
5561 && (it->c == 0x8ad || it->c == 0x92d
5562 || it->c == 0xe2d || it->c == 0xf2d))
5563 {
5564 g = it->c = '-';
5565 XSETINT (it->ctl_chars[0], g);
5566 ctl_len = 1;
5567 goto display_control;
5568 }
5569
5570 /* Handle non-break space and soft hyphen
5571 with the escape glyph. */
5572
5573 if (it->c == 0x8a0 || it->c == 0x8ad
5574 || it->c == 0x920 || it->c == 0x92d
5575 || it->c == 0xe20 || it->c == 0xe2d
5576 || it->c == 0xf20 || it->c == 0xf2d)
5577 {
5578 XSETINT (it->ctl_chars[0], escape_glyph);
5579 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5580 XSETINT (it->ctl_chars[1], g);
5581 ctl_len = 2;
5582 goto display_control;
5583 }
5584
5585 {
5586 unsigned char str[MAX_MULTIBYTE_LENGTH];
5587 int len;
5588 int i;
5589
5590 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5591 if (SINGLE_BYTE_CHAR_P (it->c))
5592 str[0] = it->c, len = 1;
5593 else
5594 {
5595 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5596 if (len < 0)
5597 {
5598 /* It's an invalid character, which shouldn't
5599 happen actually, but due to bugs it may
5600 happen. Let's print the char as is, there's
5601 not much meaningful we can do with it. */
5602 str[0] = it->c;
5603 str[1] = it->c >> 8;
5604 str[2] = it->c >> 16;
5605 str[3] = it->c >> 24;
5606 len = 4;
5607 }
5608 }
5609
5610 for (i = 0; i < len; i++)
5611 {
5612 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5613 /* Insert three more glyphs into IT->ctl_chars for
5614 the octal display of the character. */
5615 g = ((str[i] >> 6) & 7) + '0';
5616 XSETINT (it->ctl_chars[i * 4 + 1], g);
5617 g = ((str[i] >> 3) & 7) + '0';
5618 XSETINT (it->ctl_chars[i * 4 + 2], g);
5619 g = (str[i] & 7) + '0';
5620 XSETINT (it->ctl_chars[i * 4 + 3], g);
5621 }
5622 ctl_len = len * 4;
5623 }
5624
5625 display_control:
5626 /* Set up IT->dpvec and return first character from it. */
5627 it->dpvec_char_len = it->len;
5628 it->dpvec = it->ctl_chars;
5629 it->dpend = it->dpvec + ctl_len;
5630 it->current.dpvec_index = 0;
5631 it->dpvec_face_id = face_id;
5632 it->saved_face_id = it->face_id;
5633 it->method = GET_FROM_DISPLAY_VECTOR;
5634 it->ellipsis_p = 0;
5635 goto get_next;
5636 }
5637 }
5638
5639 /* Adjust face id for a multibyte character. There are no
5640 multibyte character in unibyte text. */
5641 if (it->multibyte_p
5642 && success_p
5643 && FRAME_WINDOW_P (it->f))
5644 {
5645 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5646 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5647 }
5648 }
5649
5650 /* Is this character the last one of a run of characters with
5651 box? If yes, set IT->end_of_box_run_p to 1. */
5652 if (it->face_box_p
5653 && it->s == NULL)
5654 {
5655 int face_id;
5656 struct face *face;
5657
5658 it->end_of_box_run_p
5659 = ((face_id = face_after_it_pos (it),
5660 face_id != it->face_id)
5661 && (face = FACE_FROM_ID (it->f, face_id),
5662 face->box == FACE_NO_BOX));
5663 }
5664
5665 /* Value is 0 if end of buffer or string reached. */
5666 return success_p;
5667 }
5668
5669
5670 /* Move IT to the next display element.
5671
5672 RESEAT_P non-zero means if called on a newline in buffer text,
5673 skip to the next visible line start.
5674
5675 Functions get_next_display_element and set_iterator_to_next are
5676 separate because I find this arrangement easier to handle than a
5677 get_next_display_element function that also increments IT's
5678 position. The way it is we can first look at an iterator's current
5679 display element, decide whether it fits on a line, and if it does,
5680 increment the iterator position. The other way around we probably
5681 would either need a flag indicating whether the iterator has to be
5682 incremented the next time, or we would have to implement a
5683 decrement position function which would not be easy to write. */
5684
5685 void
5686 set_iterator_to_next (it, reseat_p)
5687 struct it *it;
5688 int reseat_p;
5689 {
5690 /* Reset flags indicating start and end of a sequence of characters
5691 with box. Reset them at the start of this function because
5692 moving the iterator to a new position might set them. */
5693 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5694
5695 switch (it->method)
5696 {
5697 case GET_FROM_BUFFER:
5698 /* The current display element of IT is a character from
5699 current_buffer. Advance in the buffer, and maybe skip over
5700 invisible lines that are so because of selective display. */
5701 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5702 reseat_at_next_visible_line_start (it, 0);
5703 else
5704 {
5705 xassert (it->len != 0);
5706 IT_BYTEPOS (*it) += it->len;
5707 IT_CHARPOS (*it) += 1;
5708 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5709 }
5710 break;
5711
5712 case GET_FROM_COMPOSITION:
5713 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5714 if (STRINGP (it->string))
5715 {
5716 IT_STRING_BYTEPOS (*it) += it->len;
5717 IT_STRING_CHARPOS (*it) += it->cmp_len;
5718 it->method = GET_FROM_STRING;
5719 goto consider_string_end;
5720 }
5721 else
5722 {
5723 IT_BYTEPOS (*it) += it->len;
5724 IT_CHARPOS (*it) += it->cmp_len;
5725 it->method = GET_FROM_BUFFER;
5726 }
5727 break;
5728
5729 case GET_FROM_C_STRING:
5730 /* Current display element of IT is from a C string. */
5731 IT_BYTEPOS (*it) += it->len;
5732 IT_CHARPOS (*it) += 1;
5733 break;
5734
5735 case GET_FROM_DISPLAY_VECTOR:
5736 /* Current display element of IT is from a display table entry.
5737 Advance in the display table definition. Reset it to null if
5738 end reached, and continue with characters from buffers/
5739 strings. */
5740 ++it->current.dpvec_index;
5741
5742 /* Restore face of the iterator to what they were before the
5743 display vector entry (these entries may contain faces). */
5744 it->face_id = it->saved_face_id;
5745
5746 if (it->dpvec + it->current.dpvec_index == it->dpend)
5747 {
5748 int recheck_faces = it->ellipsis_p;
5749
5750 if (it->s)
5751 it->method = GET_FROM_C_STRING;
5752 else if (STRINGP (it->string))
5753 it->method = GET_FROM_STRING;
5754 else
5755 it->method = GET_FROM_BUFFER;
5756
5757 it->dpvec = NULL;
5758 it->current.dpvec_index = -1;
5759
5760 /* Skip over characters which were displayed via IT->dpvec. */
5761 if (it->dpvec_char_len < 0)
5762 reseat_at_next_visible_line_start (it, 1);
5763 else if (it->dpvec_char_len > 0)
5764 {
5765 if (it->method == GET_FROM_STRING
5766 && it->n_overlay_strings > 0)
5767 it->ignore_overlay_strings_at_pos_p = 1;
5768 it->len = it->dpvec_char_len;
5769 set_iterator_to_next (it, reseat_p);
5770 }
5771
5772 /* Maybe recheck faces after display vector */
5773 if (recheck_faces)
5774 it->stop_charpos = IT_CHARPOS (*it);
5775 }
5776 break;
5777
5778 case GET_FROM_STRING:
5779 /* Current display element is a character from a Lisp string. */
5780 xassert (it->s == NULL && STRINGP (it->string));
5781 IT_STRING_BYTEPOS (*it) += it->len;
5782 IT_STRING_CHARPOS (*it) += 1;
5783
5784 consider_string_end:
5785
5786 if (it->current.overlay_string_index >= 0)
5787 {
5788 /* IT->string is an overlay string. Advance to the
5789 next, if there is one. */
5790 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5791 next_overlay_string (it);
5792 }
5793 else
5794 {
5795 /* IT->string is not an overlay string. If we reached
5796 its end, and there is something on IT->stack, proceed
5797 with what is on the stack. This can be either another
5798 string, this time an overlay string, or a buffer. */
5799 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5800 && it->sp > 0)
5801 {
5802 pop_it (it);
5803 if (STRINGP (it->string))
5804 goto consider_string_end;
5805 it->method = GET_FROM_BUFFER;
5806 }
5807 }
5808 break;
5809
5810 case GET_FROM_IMAGE:
5811 case GET_FROM_STRETCH:
5812 /* The position etc with which we have to proceed are on
5813 the stack. The position may be at the end of a string,
5814 if the `display' property takes up the whole string. */
5815 xassert (it->sp > 0);
5816 pop_it (it);
5817 it->image_id = 0;
5818 if (STRINGP (it->string))
5819 {
5820 it->method = GET_FROM_STRING;
5821 goto consider_string_end;
5822 }
5823 it->method = GET_FROM_BUFFER;
5824 break;
5825
5826 default:
5827 /* There are no other methods defined, so this should be a bug. */
5828 abort ();
5829 }
5830
5831 xassert (it->method != GET_FROM_STRING
5832 || (STRINGP (it->string)
5833 && IT_STRING_CHARPOS (*it) >= 0));
5834 }
5835
5836 /* Load IT's display element fields with information about the next
5837 display element which comes from a display table entry or from the
5838 result of translating a control character to one of the forms `^C'
5839 or `\003'.
5840
5841 IT->dpvec holds the glyphs to return as characters.
5842 IT->saved_face_id holds the face id before the display vector--
5843 it is restored into IT->face_idin set_iterator_to_next. */
5844
5845 static int
5846 next_element_from_display_vector (it)
5847 struct it *it;
5848 {
5849 /* Precondition. */
5850 xassert (it->dpvec && it->current.dpvec_index >= 0);
5851
5852 it->face_id = it->saved_face_id;
5853
5854 if (INTEGERP (*it->dpvec)
5855 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5856 {
5857 GLYPH g;
5858
5859 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5860 it->c = FAST_GLYPH_CHAR (g);
5861 it->len = CHAR_BYTES (it->c);
5862
5863 /* The entry may contain a face id to use. Such a face id is
5864 the id of a Lisp face, not a realized face. A face id of
5865 zero means no face is specified. */
5866 if (it->dpvec_face_id >= 0)
5867 it->face_id = it->dpvec_face_id;
5868 else
5869 {
5870 int lface_id = FAST_GLYPH_FACE (g);
5871 if (lface_id > 0)
5872 it->face_id = merge_faces (it->f, Qt, lface_id,
5873 it->saved_face_id);
5874 }
5875 }
5876 else
5877 /* Display table entry is invalid. Return a space. */
5878 it->c = ' ', it->len = 1;
5879
5880 /* Don't change position and object of the iterator here. They are
5881 still the values of the character that had this display table
5882 entry or was translated, and that's what we want. */
5883 it->what = IT_CHARACTER;
5884 return 1;
5885 }
5886
5887
5888 /* Load IT with the next display element from Lisp string IT->string.
5889 IT->current.string_pos is the current position within the string.
5890 If IT->current.overlay_string_index >= 0, the Lisp string is an
5891 overlay string. */
5892
5893 static int
5894 next_element_from_string (it)
5895 struct it *it;
5896 {
5897 struct text_pos position;
5898
5899 xassert (STRINGP (it->string));
5900 xassert (IT_STRING_CHARPOS (*it) >= 0);
5901 position = it->current.string_pos;
5902
5903 /* Time to check for invisible text? */
5904 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5905 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5906 {
5907 handle_stop (it);
5908
5909 /* Since a handler may have changed IT->method, we must
5910 recurse here. */
5911 return get_next_display_element (it);
5912 }
5913
5914 if (it->current.overlay_string_index >= 0)
5915 {
5916 /* Get the next character from an overlay string. In overlay
5917 strings, There is no field width or padding with spaces to
5918 do. */
5919 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5920 {
5921 it->what = IT_EOB;
5922 return 0;
5923 }
5924 else if (STRING_MULTIBYTE (it->string))
5925 {
5926 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5927 const unsigned char *s = (SDATA (it->string)
5928 + IT_STRING_BYTEPOS (*it));
5929 it->c = string_char_and_length (s, remaining, &it->len);
5930 }
5931 else
5932 {
5933 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5934 it->len = 1;
5935 }
5936 }
5937 else
5938 {
5939 /* Get the next character from a Lisp string that is not an
5940 overlay string. Such strings come from the mode line, for
5941 example. We may have to pad with spaces, or truncate the
5942 string. See also next_element_from_c_string. */
5943 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5944 {
5945 it->what = IT_EOB;
5946 return 0;
5947 }
5948 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5949 {
5950 /* Pad with spaces. */
5951 it->c = ' ', it->len = 1;
5952 CHARPOS (position) = BYTEPOS (position) = -1;
5953 }
5954 else if (STRING_MULTIBYTE (it->string))
5955 {
5956 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5957 const unsigned char *s = (SDATA (it->string)
5958 + IT_STRING_BYTEPOS (*it));
5959 it->c = string_char_and_length (s, maxlen, &it->len);
5960 }
5961 else
5962 {
5963 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5964 it->len = 1;
5965 }
5966 }
5967
5968 /* Record what we have and where it came from. Note that we store a
5969 buffer position in IT->position although it could arguably be a
5970 string position. */
5971 it->what = IT_CHARACTER;
5972 it->object = it->string;
5973 it->position = position;
5974 return 1;
5975 }
5976
5977
5978 /* Load IT with next display element from C string IT->s.
5979 IT->string_nchars is the maximum number of characters to return
5980 from the string. IT->end_charpos may be greater than
5981 IT->string_nchars when this function is called, in which case we
5982 may have to return padding spaces. Value is zero if end of string
5983 reached, including padding spaces. */
5984
5985 static int
5986 next_element_from_c_string (it)
5987 struct it *it;
5988 {
5989 int success_p = 1;
5990
5991 xassert (it->s);
5992 it->what = IT_CHARACTER;
5993 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5994 it->object = Qnil;
5995
5996 /* IT's position can be greater IT->string_nchars in case a field
5997 width or precision has been specified when the iterator was
5998 initialized. */
5999 if (IT_CHARPOS (*it) >= it->end_charpos)
6000 {
6001 /* End of the game. */
6002 it->what = IT_EOB;
6003 success_p = 0;
6004 }
6005 else if (IT_CHARPOS (*it) >= it->string_nchars)
6006 {
6007 /* Pad with spaces. */
6008 it->c = ' ', it->len = 1;
6009 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6010 }
6011 else if (it->multibyte_p)
6012 {
6013 /* Implementation note: The calls to strlen apparently aren't a
6014 performance problem because there is no noticeable performance
6015 difference between Emacs running in unibyte or multibyte mode. */
6016 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6017 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6018 maxlen, &it->len);
6019 }
6020 else
6021 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6022
6023 return success_p;
6024 }
6025
6026
6027 /* Set up IT to return characters from an ellipsis, if appropriate.
6028 The definition of the ellipsis glyphs may come from a display table
6029 entry. This function Fills IT with the first glyph from the
6030 ellipsis if an ellipsis is to be displayed. */
6031
6032 static int
6033 next_element_from_ellipsis (it)
6034 struct it *it;
6035 {
6036 if (it->selective_display_ellipsis_p)
6037 setup_for_ellipsis (it, it->len);
6038 else
6039 {
6040 /* The face at the current position may be different from the
6041 face we find after the invisible text. Remember what it
6042 was in IT->saved_face_id, and signal that it's there by
6043 setting face_before_selective_p. */
6044 it->saved_face_id = it->face_id;
6045 it->method = GET_FROM_BUFFER;
6046 reseat_at_next_visible_line_start (it, 1);
6047 it->face_before_selective_p = 1;
6048 }
6049
6050 return get_next_display_element (it);
6051 }
6052
6053
6054 /* Deliver an image display element. The iterator IT is already
6055 filled with image information (done in handle_display_prop). Value
6056 is always 1. */
6057
6058
6059 static int
6060 next_element_from_image (it)
6061 struct it *it;
6062 {
6063 it->what = IT_IMAGE;
6064 return 1;
6065 }
6066
6067
6068 /* Fill iterator IT with next display element from a stretch glyph
6069 property. IT->object is the value of the text property. Value is
6070 always 1. */
6071
6072 static int
6073 next_element_from_stretch (it)
6074 struct it *it;
6075 {
6076 it->what = IT_STRETCH;
6077 return 1;
6078 }
6079
6080
6081 /* Load IT with the next display element from current_buffer. Value
6082 is zero if end of buffer reached. IT->stop_charpos is the next
6083 position at which to stop and check for text properties or buffer
6084 end. */
6085
6086 static int
6087 next_element_from_buffer (it)
6088 struct it *it;
6089 {
6090 int success_p = 1;
6091
6092 /* Check this assumption, otherwise, we would never enter the
6093 if-statement, below. */
6094 xassert (IT_CHARPOS (*it) >= BEGV
6095 && IT_CHARPOS (*it) <= it->stop_charpos);
6096
6097 if (IT_CHARPOS (*it) >= it->stop_charpos)
6098 {
6099 if (IT_CHARPOS (*it) >= it->end_charpos)
6100 {
6101 int overlay_strings_follow_p;
6102
6103 /* End of the game, except when overlay strings follow that
6104 haven't been returned yet. */
6105 if (it->overlay_strings_at_end_processed_p)
6106 overlay_strings_follow_p = 0;
6107 else
6108 {
6109 it->overlay_strings_at_end_processed_p = 1;
6110 overlay_strings_follow_p = get_overlay_strings (it, 0);
6111 }
6112
6113 if (overlay_strings_follow_p)
6114 success_p = get_next_display_element (it);
6115 else
6116 {
6117 it->what = IT_EOB;
6118 it->position = it->current.pos;
6119 success_p = 0;
6120 }
6121 }
6122 else
6123 {
6124 handle_stop (it);
6125 return get_next_display_element (it);
6126 }
6127 }
6128 else
6129 {
6130 /* No face changes, overlays etc. in sight, so just return a
6131 character from current_buffer. */
6132 unsigned char *p;
6133
6134 /* Maybe run the redisplay end trigger hook. Performance note:
6135 This doesn't seem to cost measurable time. */
6136 if (it->redisplay_end_trigger_charpos
6137 && it->glyph_row
6138 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6139 run_redisplay_end_trigger_hook (it);
6140
6141 /* Get the next character, maybe multibyte. */
6142 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6143 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6144 {
6145 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6146 - IT_BYTEPOS (*it));
6147 it->c = string_char_and_length (p, maxlen, &it->len);
6148 }
6149 else
6150 it->c = *p, it->len = 1;
6151
6152 /* Record what we have and where it came from. */
6153 it->what = IT_CHARACTER;;
6154 it->object = it->w->buffer;
6155 it->position = it->current.pos;
6156
6157 /* Normally we return the character found above, except when we
6158 really want to return an ellipsis for selective display. */
6159 if (it->selective)
6160 {
6161 if (it->c == '\n')
6162 {
6163 /* A value of selective > 0 means hide lines indented more
6164 than that number of columns. */
6165 if (it->selective > 0
6166 && IT_CHARPOS (*it) + 1 < ZV
6167 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6168 IT_BYTEPOS (*it) + 1,
6169 (double) it->selective)) /* iftc */
6170 {
6171 success_p = next_element_from_ellipsis (it);
6172 it->dpvec_char_len = -1;
6173 }
6174 }
6175 else if (it->c == '\r' && it->selective == -1)
6176 {
6177 /* A value of selective == -1 means that everything from the
6178 CR to the end of the line is invisible, with maybe an
6179 ellipsis displayed for it. */
6180 success_p = next_element_from_ellipsis (it);
6181 it->dpvec_char_len = -1;
6182 }
6183 }
6184 }
6185
6186 /* Value is zero if end of buffer reached. */
6187 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6188 return success_p;
6189 }
6190
6191
6192 /* Run the redisplay end trigger hook for IT. */
6193
6194 static void
6195 run_redisplay_end_trigger_hook (it)
6196 struct it *it;
6197 {
6198 Lisp_Object args[3];
6199
6200 /* IT->glyph_row should be non-null, i.e. we should be actually
6201 displaying something, or otherwise we should not run the hook. */
6202 xassert (it->glyph_row);
6203
6204 /* Set up hook arguments. */
6205 args[0] = Qredisplay_end_trigger_functions;
6206 args[1] = it->window;
6207 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6208 it->redisplay_end_trigger_charpos = 0;
6209
6210 /* Since we are *trying* to run these functions, don't try to run
6211 them again, even if they get an error. */
6212 it->w->redisplay_end_trigger = Qnil;
6213 Frun_hook_with_args (3, args);
6214
6215 /* Notice if it changed the face of the character we are on. */
6216 handle_face_prop (it);
6217 }
6218
6219
6220 /* Deliver a composition display element. The iterator IT is already
6221 filled with composition information (done in
6222 handle_composition_prop). Value is always 1. */
6223
6224 static int
6225 next_element_from_composition (it)
6226 struct it *it;
6227 {
6228 it->what = IT_COMPOSITION;
6229 it->position = (STRINGP (it->string)
6230 ? it->current.string_pos
6231 : it->current.pos);
6232 return 1;
6233 }
6234
6235
6236 \f
6237 /***********************************************************************
6238 Moving an iterator without producing glyphs
6239 ***********************************************************************/
6240
6241 /* Check if iterator is at a position corresponding to a valid buffer
6242 position after some move_it_ call. */
6243
6244 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6245 ((it)->method == GET_FROM_STRING \
6246 ? IT_STRING_CHARPOS (*it) == 0 \
6247 : 1)
6248
6249
6250 /* Move iterator IT to a specified buffer or X position within one
6251 line on the display without producing glyphs.
6252
6253 OP should be a bit mask including some or all of these bits:
6254 MOVE_TO_X: Stop on reaching x-position TO_X.
6255 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6256 Regardless of OP's value, stop in reaching the end of the display line.
6257
6258 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6259 This means, in particular, that TO_X includes window's horizontal
6260 scroll amount.
6261
6262 The return value has several possible values that
6263 say what condition caused the scan to stop:
6264
6265 MOVE_POS_MATCH_OR_ZV
6266 - when TO_POS or ZV was reached.
6267
6268 MOVE_X_REACHED
6269 -when TO_X was reached before TO_POS or ZV were reached.
6270
6271 MOVE_LINE_CONTINUED
6272 - when we reached the end of the display area and the line must
6273 be continued.
6274
6275 MOVE_LINE_TRUNCATED
6276 - when we reached the end of the display area and the line is
6277 truncated.
6278
6279 MOVE_NEWLINE_OR_CR
6280 - when we stopped at a line end, i.e. a newline or a CR and selective
6281 display is on. */
6282
6283 static enum move_it_result
6284 move_it_in_display_line_to (it, to_charpos, to_x, op)
6285 struct it *it;
6286 int to_charpos, to_x, op;
6287 {
6288 enum move_it_result result = MOVE_UNDEFINED;
6289 struct glyph_row *saved_glyph_row;
6290
6291 /* Don't produce glyphs in produce_glyphs. */
6292 saved_glyph_row = it->glyph_row;
6293 it->glyph_row = NULL;
6294
6295 #define BUFFER_POS_REACHED_P() \
6296 ((op & MOVE_TO_POS) != 0 \
6297 && BUFFERP (it->object) \
6298 && IT_CHARPOS (*it) >= to_charpos \
6299 && (it->method == GET_FROM_BUFFER \
6300 || (it->method == GET_FROM_DISPLAY_VECTOR \
6301 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6302
6303
6304 while (1)
6305 {
6306 int x, i, ascent = 0, descent = 0;
6307
6308 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6309 if ((op & MOVE_TO_POS) != 0
6310 && BUFFERP (it->object)
6311 && it->method == GET_FROM_BUFFER
6312 && IT_CHARPOS (*it) > to_charpos)
6313 {
6314 result = MOVE_POS_MATCH_OR_ZV;
6315 break;
6316 }
6317
6318 /* Stop when ZV reached.
6319 We used to stop here when TO_CHARPOS reached as well, but that is
6320 too soon if this glyph does not fit on this line. So we handle it
6321 explicitly below. */
6322 if (!get_next_display_element (it)
6323 || (it->truncate_lines_p
6324 && BUFFER_POS_REACHED_P ()))
6325 {
6326 result = MOVE_POS_MATCH_OR_ZV;
6327 break;
6328 }
6329
6330 /* The call to produce_glyphs will get the metrics of the
6331 display element IT is loaded with. We record in x the
6332 x-position before this display element in case it does not
6333 fit on the line. */
6334 x = it->current_x;
6335
6336 /* Remember the line height so far in case the next element doesn't
6337 fit on the line. */
6338 if (!it->truncate_lines_p)
6339 {
6340 ascent = it->max_ascent;
6341 descent = it->max_descent;
6342 }
6343
6344 PRODUCE_GLYPHS (it);
6345
6346 if (it->area != TEXT_AREA)
6347 {
6348 set_iterator_to_next (it, 1);
6349 continue;
6350 }
6351
6352 /* The number of glyphs we get back in IT->nglyphs will normally
6353 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6354 character on a terminal frame, or (iii) a line end. For the
6355 second case, IT->nglyphs - 1 padding glyphs will be present
6356 (on X frames, there is only one glyph produced for a
6357 composite character.
6358
6359 The behavior implemented below means, for continuation lines,
6360 that as many spaces of a TAB as fit on the current line are
6361 displayed there. For terminal frames, as many glyphs of a
6362 multi-glyph character are displayed in the current line, too.
6363 This is what the old redisplay code did, and we keep it that
6364 way. Under X, the whole shape of a complex character must
6365 fit on the line or it will be completely displayed in the
6366 next line.
6367
6368 Note that both for tabs and padding glyphs, all glyphs have
6369 the same width. */
6370 if (it->nglyphs)
6371 {
6372 /* More than one glyph or glyph doesn't fit on line. All
6373 glyphs have the same width. */
6374 int single_glyph_width = it->pixel_width / it->nglyphs;
6375 int new_x;
6376 int x_before_this_char = x;
6377 int hpos_before_this_char = it->hpos;
6378
6379 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6380 {
6381 new_x = x + single_glyph_width;
6382
6383 /* We want to leave anything reaching TO_X to the caller. */
6384 if ((op & MOVE_TO_X) && new_x > to_x)
6385 {
6386 if (BUFFER_POS_REACHED_P ())
6387 goto buffer_pos_reached;
6388 it->current_x = x;
6389 result = MOVE_X_REACHED;
6390 break;
6391 }
6392 else if (/* Lines are continued. */
6393 !it->truncate_lines_p
6394 && (/* And glyph doesn't fit on the line. */
6395 new_x > it->last_visible_x
6396 /* Or it fits exactly and we're on a window
6397 system frame. */
6398 || (new_x == it->last_visible_x
6399 && FRAME_WINDOW_P (it->f))))
6400 {
6401 if (/* IT->hpos == 0 means the very first glyph
6402 doesn't fit on the line, e.g. a wide image. */
6403 it->hpos == 0
6404 || (new_x == it->last_visible_x
6405 && FRAME_WINDOW_P (it->f)))
6406 {
6407 ++it->hpos;
6408 it->current_x = new_x;
6409
6410 /* The character's last glyph just barely fits
6411 in this row. */
6412 if (i == it->nglyphs - 1)
6413 {
6414 /* If this is the destination position,
6415 return a position *before* it in this row,
6416 now that we know it fits in this row. */
6417 if (BUFFER_POS_REACHED_P ())
6418 {
6419 it->hpos = hpos_before_this_char;
6420 it->current_x = x_before_this_char;
6421 result = MOVE_POS_MATCH_OR_ZV;
6422 break;
6423 }
6424
6425 set_iterator_to_next (it, 1);
6426 #ifdef HAVE_WINDOW_SYSTEM
6427 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6428 {
6429 if (!get_next_display_element (it))
6430 {
6431 result = MOVE_POS_MATCH_OR_ZV;
6432 break;
6433 }
6434 if (BUFFER_POS_REACHED_P ())
6435 {
6436 if (ITERATOR_AT_END_OF_LINE_P (it))
6437 result = MOVE_POS_MATCH_OR_ZV;
6438 else
6439 result = MOVE_LINE_CONTINUED;
6440 break;
6441 }
6442 if (ITERATOR_AT_END_OF_LINE_P (it))
6443 {
6444 result = MOVE_NEWLINE_OR_CR;
6445 break;
6446 }
6447 }
6448 #endif /* HAVE_WINDOW_SYSTEM */
6449 }
6450 }
6451 else
6452 {
6453 it->current_x = x;
6454 it->max_ascent = ascent;
6455 it->max_descent = descent;
6456 }
6457
6458 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6459 IT_CHARPOS (*it)));
6460 result = MOVE_LINE_CONTINUED;
6461 break;
6462 }
6463 else if (BUFFER_POS_REACHED_P ())
6464 goto buffer_pos_reached;
6465 else if (new_x > it->first_visible_x)
6466 {
6467 /* Glyph is visible. Increment number of glyphs that
6468 would be displayed. */
6469 ++it->hpos;
6470 }
6471 else
6472 {
6473 /* Glyph is completely off the left margin of the display
6474 area. Nothing to do. */
6475 }
6476 }
6477
6478 if (result != MOVE_UNDEFINED)
6479 break;
6480 }
6481 else if (BUFFER_POS_REACHED_P ())
6482 {
6483 buffer_pos_reached:
6484 it->current_x = x;
6485 it->max_ascent = ascent;
6486 it->max_descent = descent;
6487 result = MOVE_POS_MATCH_OR_ZV;
6488 break;
6489 }
6490 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6491 {
6492 /* Stop when TO_X specified and reached. This check is
6493 necessary here because of lines consisting of a line end,
6494 only. The line end will not produce any glyphs and we
6495 would never get MOVE_X_REACHED. */
6496 xassert (it->nglyphs == 0);
6497 result = MOVE_X_REACHED;
6498 break;
6499 }
6500
6501 /* Is this a line end? If yes, we're done. */
6502 if (ITERATOR_AT_END_OF_LINE_P (it))
6503 {
6504 result = MOVE_NEWLINE_OR_CR;
6505 break;
6506 }
6507
6508 /* The current display element has been consumed. Advance
6509 to the next. */
6510 set_iterator_to_next (it, 1);
6511
6512 /* Stop if lines are truncated and IT's current x-position is
6513 past the right edge of the window now. */
6514 if (it->truncate_lines_p
6515 && it->current_x >= it->last_visible_x)
6516 {
6517 #ifdef HAVE_WINDOW_SYSTEM
6518 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6519 {
6520 if (!get_next_display_element (it)
6521 || BUFFER_POS_REACHED_P ())
6522 {
6523 result = MOVE_POS_MATCH_OR_ZV;
6524 break;
6525 }
6526 if (ITERATOR_AT_END_OF_LINE_P (it))
6527 {
6528 result = MOVE_NEWLINE_OR_CR;
6529 break;
6530 }
6531 }
6532 #endif /* HAVE_WINDOW_SYSTEM */
6533 result = MOVE_LINE_TRUNCATED;
6534 break;
6535 }
6536 }
6537
6538 #undef BUFFER_POS_REACHED_P
6539
6540 /* Restore the iterator settings altered at the beginning of this
6541 function. */
6542 it->glyph_row = saved_glyph_row;
6543 return result;
6544 }
6545
6546
6547 /* Move IT forward until it satisfies one or more of the criteria in
6548 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6549
6550 OP is a bit-mask that specifies where to stop, and in particular,
6551 which of those four position arguments makes a difference. See the
6552 description of enum move_operation_enum.
6553
6554 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6555 screen line, this function will set IT to the next position >
6556 TO_CHARPOS. */
6557
6558 void
6559 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6560 struct it *it;
6561 int to_charpos, to_x, to_y, to_vpos;
6562 int op;
6563 {
6564 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6565 int line_height;
6566 int reached = 0;
6567
6568 for (;;)
6569 {
6570 if (op & MOVE_TO_VPOS)
6571 {
6572 /* If no TO_CHARPOS and no TO_X specified, stop at the
6573 start of the line TO_VPOS. */
6574 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6575 {
6576 if (it->vpos == to_vpos)
6577 {
6578 reached = 1;
6579 break;
6580 }
6581 else
6582 skip = move_it_in_display_line_to (it, -1, -1, 0);
6583 }
6584 else
6585 {
6586 /* TO_VPOS >= 0 means stop at TO_X in the line at
6587 TO_VPOS, or at TO_POS, whichever comes first. */
6588 if (it->vpos == to_vpos)
6589 {
6590 reached = 2;
6591 break;
6592 }
6593
6594 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6595
6596 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6597 {
6598 reached = 3;
6599 break;
6600 }
6601 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6602 {
6603 /* We have reached TO_X but not in the line we want. */
6604 skip = move_it_in_display_line_to (it, to_charpos,
6605 -1, MOVE_TO_POS);
6606 if (skip == MOVE_POS_MATCH_OR_ZV)
6607 {
6608 reached = 4;
6609 break;
6610 }
6611 }
6612 }
6613 }
6614 else if (op & MOVE_TO_Y)
6615 {
6616 struct it it_backup;
6617
6618 /* TO_Y specified means stop at TO_X in the line containing
6619 TO_Y---or at TO_CHARPOS if this is reached first. The
6620 problem is that we can't really tell whether the line
6621 contains TO_Y before we have completely scanned it, and
6622 this may skip past TO_X. What we do is to first scan to
6623 TO_X.
6624
6625 If TO_X is not specified, use a TO_X of zero. The reason
6626 is to make the outcome of this function more predictable.
6627 If we didn't use TO_X == 0, we would stop at the end of
6628 the line which is probably not what a caller would expect
6629 to happen. */
6630 skip = move_it_in_display_line_to (it, to_charpos,
6631 ((op & MOVE_TO_X)
6632 ? to_x : 0),
6633 (MOVE_TO_X
6634 | (op & MOVE_TO_POS)));
6635
6636 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6637 if (skip == MOVE_POS_MATCH_OR_ZV)
6638 {
6639 reached = 5;
6640 break;
6641 }
6642
6643 /* If TO_X was reached, we would like to know whether TO_Y
6644 is in the line. This can only be said if we know the
6645 total line height which requires us to scan the rest of
6646 the line. */
6647 if (skip == MOVE_X_REACHED)
6648 {
6649 it_backup = *it;
6650 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6651 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6652 op & MOVE_TO_POS);
6653 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6654 }
6655
6656 /* Now, decide whether TO_Y is in this line. */
6657 line_height = it->max_ascent + it->max_descent;
6658 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6659
6660 if (to_y >= it->current_y
6661 && to_y < it->current_y + line_height)
6662 {
6663 if (skip == MOVE_X_REACHED)
6664 /* If TO_Y is in this line and TO_X was reached above,
6665 we scanned too far. We have to restore IT's settings
6666 to the ones before skipping. */
6667 *it = it_backup;
6668 reached = 6;
6669 }
6670 else if (skip == MOVE_X_REACHED)
6671 {
6672 skip = skip2;
6673 if (skip == MOVE_POS_MATCH_OR_ZV)
6674 reached = 7;
6675 }
6676
6677 if (reached)
6678 break;
6679 }
6680 else
6681 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6682
6683 switch (skip)
6684 {
6685 case MOVE_POS_MATCH_OR_ZV:
6686 reached = 8;
6687 goto out;
6688
6689 case MOVE_NEWLINE_OR_CR:
6690 set_iterator_to_next (it, 1);
6691 it->continuation_lines_width = 0;
6692 break;
6693
6694 case MOVE_LINE_TRUNCATED:
6695 it->continuation_lines_width = 0;
6696 reseat_at_next_visible_line_start (it, 0);
6697 if ((op & MOVE_TO_POS) != 0
6698 && IT_CHARPOS (*it) > to_charpos)
6699 {
6700 reached = 9;
6701 goto out;
6702 }
6703 break;
6704
6705 case MOVE_LINE_CONTINUED:
6706 it->continuation_lines_width += it->current_x;
6707 break;
6708
6709 default:
6710 abort ();
6711 }
6712
6713 /* Reset/increment for the next run. */
6714 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6715 it->current_x = it->hpos = 0;
6716 it->current_y += it->max_ascent + it->max_descent;
6717 ++it->vpos;
6718 last_height = it->max_ascent + it->max_descent;
6719 last_max_ascent = it->max_ascent;
6720 it->max_ascent = it->max_descent = 0;
6721 }
6722
6723 out:
6724
6725 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6726 }
6727
6728
6729 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6730
6731 If DY > 0, move IT backward at least that many pixels. DY = 0
6732 means move IT backward to the preceding line start or BEGV. This
6733 function may move over more than DY pixels if IT->current_y - DY
6734 ends up in the middle of a line; in this case IT->current_y will be
6735 set to the top of the line moved to. */
6736
6737 void
6738 move_it_vertically_backward (it, dy)
6739 struct it *it;
6740 int dy;
6741 {
6742 int nlines, h;
6743 struct it it2, it3;
6744 int start_pos;
6745
6746 move_further_back:
6747 xassert (dy >= 0);
6748
6749 start_pos = IT_CHARPOS (*it);
6750
6751 /* Estimate how many newlines we must move back. */
6752 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6753
6754 /* Set the iterator's position that many lines back. */
6755 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6756 back_to_previous_visible_line_start (it);
6757
6758 /* Reseat the iterator here. When moving backward, we don't want
6759 reseat to skip forward over invisible text, set up the iterator
6760 to deliver from overlay strings at the new position etc. So,
6761 use reseat_1 here. */
6762 reseat_1 (it, it->current.pos, 1);
6763
6764 /* We are now surely at a line start. */
6765 it->current_x = it->hpos = 0;
6766 it->continuation_lines_width = 0;
6767
6768 /* Move forward and see what y-distance we moved. First move to the
6769 start of the next line so that we get its height. We need this
6770 height to be able to tell whether we reached the specified
6771 y-distance. */
6772 it2 = *it;
6773 it2.max_ascent = it2.max_descent = 0;
6774 do
6775 {
6776 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6777 MOVE_TO_POS | MOVE_TO_VPOS);
6778 }
6779 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6780 xassert (IT_CHARPOS (*it) >= BEGV);
6781 it3 = it2;
6782
6783 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6784 xassert (IT_CHARPOS (*it) >= BEGV);
6785 /* H is the actual vertical distance from the position in *IT
6786 and the starting position. */
6787 h = it2.current_y - it->current_y;
6788 /* NLINES is the distance in number of lines. */
6789 nlines = it2.vpos - it->vpos;
6790
6791 /* Correct IT's y and vpos position
6792 so that they are relative to the starting point. */
6793 it->vpos -= nlines;
6794 it->current_y -= h;
6795
6796 if (dy == 0)
6797 {
6798 /* DY == 0 means move to the start of the screen line. The
6799 value of nlines is > 0 if continuation lines were involved. */
6800 if (nlines > 0)
6801 move_it_by_lines (it, nlines, 1);
6802 #if 0
6803 /* I think this assert is bogus if buffer contains
6804 invisible text or images. KFS. */
6805 xassert (IT_CHARPOS (*it) <= start_pos);
6806 #endif
6807 }
6808 else
6809 {
6810 /* The y-position we try to reach, relative to *IT.
6811 Note that H has been subtracted in front of the if-statement. */
6812 int target_y = it->current_y + h - dy;
6813 int y0 = it3.current_y;
6814 int y1 = line_bottom_y (&it3);
6815 int line_height = y1 - y0;
6816
6817 /* If we did not reach target_y, try to move further backward if
6818 we can. If we moved too far backward, try to move forward. */
6819 if (target_y < it->current_y
6820 /* This is heuristic. In a window that's 3 lines high, with
6821 a line height of 13 pixels each, recentering with point
6822 on the bottom line will try to move -39/2 = 19 pixels
6823 backward. Try to avoid moving into the first line. */
6824 && (it->current_y - target_y
6825 > min (window_box_height (it->w), line_height * 2 / 3))
6826 && IT_CHARPOS (*it) > BEGV)
6827 {
6828 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6829 target_y - it->current_y));
6830 dy = it->current_y - target_y;
6831 goto move_further_back;
6832 }
6833 else if (target_y >= it->current_y + line_height
6834 && IT_CHARPOS (*it) < ZV)
6835 {
6836 /* Should move forward by at least one line, maybe more.
6837
6838 Note: Calling move_it_by_lines can be expensive on
6839 terminal frames, where compute_motion is used (via
6840 vmotion) to do the job, when there are very long lines
6841 and truncate-lines is nil. That's the reason for
6842 treating terminal frames specially here. */
6843
6844 if (!FRAME_WINDOW_P (it->f))
6845 move_it_vertically (it, target_y - (it->current_y + line_height));
6846 else
6847 {
6848 do
6849 {
6850 move_it_by_lines (it, 1, 1);
6851 }
6852 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6853 }
6854
6855 #if 0
6856 /* I think this assert is bogus if buffer contains
6857 invisible text or images. KFS. */
6858 xassert (IT_CHARPOS (*it) >= BEGV);
6859 #endif
6860 }
6861 }
6862 }
6863
6864
6865 /* Move IT by a specified amount of pixel lines DY. DY negative means
6866 move backwards. DY = 0 means move to start of screen line. At the
6867 end, IT will be on the start of a screen line. */
6868
6869 void
6870 move_it_vertically (it, dy)
6871 struct it *it;
6872 int dy;
6873 {
6874 if (dy <= 0)
6875 move_it_vertically_backward (it, -dy);
6876 else
6877 {
6878 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6879 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6880 MOVE_TO_POS | MOVE_TO_Y);
6881 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6882
6883 /* If buffer ends in ZV without a newline, move to the start of
6884 the line to satisfy the post-condition. */
6885 if (IT_CHARPOS (*it) == ZV
6886 && ZV > BEGV
6887 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6888 move_it_by_lines (it, 0, 0);
6889 }
6890 }
6891
6892
6893 /* Move iterator IT past the end of the text line it is in. */
6894
6895 void
6896 move_it_past_eol (it)
6897 struct it *it;
6898 {
6899 enum move_it_result rc;
6900
6901 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6902 if (rc == MOVE_NEWLINE_OR_CR)
6903 set_iterator_to_next (it, 0);
6904 }
6905
6906
6907 #if 0 /* Currently not used. */
6908
6909 /* Return non-zero if some text between buffer positions START_CHARPOS
6910 and END_CHARPOS is invisible. IT->window is the window for text
6911 property lookup. */
6912
6913 static int
6914 invisible_text_between_p (it, start_charpos, end_charpos)
6915 struct it *it;
6916 int start_charpos, end_charpos;
6917 {
6918 Lisp_Object prop, limit;
6919 int invisible_found_p;
6920
6921 xassert (it != NULL && start_charpos <= end_charpos);
6922
6923 /* Is text at START invisible? */
6924 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6925 it->window);
6926 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6927 invisible_found_p = 1;
6928 else
6929 {
6930 limit = Fnext_single_char_property_change (make_number (start_charpos),
6931 Qinvisible, Qnil,
6932 make_number (end_charpos));
6933 invisible_found_p = XFASTINT (limit) < end_charpos;
6934 }
6935
6936 return invisible_found_p;
6937 }
6938
6939 #endif /* 0 */
6940
6941
6942 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6943 negative means move up. DVPOS == 0 means move to the start of the
6944 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6945 NEED_Y_P is zero, IT->current_y will be left unchanged.
6946
6947 Further optimization ideas: If we would know that IT->f doesn't use
6948 a face with proportional font, we could be faster for
6949 truncate-lines nil. */
6950
6951 void
6952 move_it_by_lines (it, dvpos, need_y_p)
6953 struct it *it;
6954 int dvpos, need_y_p;
6955 {
6956 struct position pos;
6957
6958 if (!FRAME_WINDOW_P (it->f))
6959 {
6960 struct text_pos textpos;
6961
6962 /* We can use vmotion on frames without proportional fonts. */
6963 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6964 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6965 reseat (it, textpos, 1);
6966 it->vpos += pos.vpos;
6967 it->current_y += pos.vpos;
6968 }
6969 else if (dvpos == 0)
6970 {
6971 /* DVPOS == 0 means move to the start of the screen line. */
6972 move_it_vertically_backward (it, 0);
6973 xassert (it->current_x == 0 && it->hpos == 0);
6974 /* Let next call to line_bottom_y calculate real line height */
6975 last_height = 0;
6976 }
6977 else if (dvpos > 0)
6978 {
6979 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6980 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6981 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6982 }
6983 else
6984 {
6985 struct it it2;
6986 int start_charpos, i;
6987
6988 /* Start at the beginning of the screen line containing IT's
6989 position. This may actually move vertically backwards,
6990 in case of overlays, so adjust dvpos accordingly. */
6991 dvpos += it->vpos;
6992 move_it_vertically_backward (it, 0);
6993 dvpos -= it->vpos;
6994
6995 /* Go back -DVPOS visible lines and reseat the iterator there. */
6996 start_charpos = IT_CHARPOS (*it);
6997 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6998 back_to_previous_visible_line_start (it);
6999 reseat (it, it->current.pos, 1);
7000
7001 /* Move further back if we end up in a string or an image. */
7002 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7003 {
7004 /* First try to move to start of display line. */
7005 dvpos += it->vpos;
7006 move_it_vertically_backward (it, 0);
7007 dvpos -= it->vpos;
7008 if (IT_POS_VALID_AFTER_MOVE_P (it))
7009 break;
7010 /* If start of line is still in string or image,
7011 move further back. */
7012 back_to_previous_visible_line_start (it);
7013 reseat (it, it->current.pos, 1);
7014 dvpos--;
7015 }
7016
7017 it->current_x = it->hpos = 0;
7018
7019 /* Above call may have moved too far if continuation lines
7020 are involved. Scan forward and see if it did. */
7021 it2 = *it;
7022 it2.vpos = it2.current_y = 0;
7023 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7024 it->vpos -= it2.vpos;
7025 it->current_y -= it2.current_y;
7026 it->current_x = it->hpos = 0;
7027
7028 /* If we moved too far back, move IT some lines forward. */
7029 if (it2.vpos > -dvpos)
7030 {
7031 int delta = it2.vpos + dvpos;
7032 it2 = *it;
7033 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7034 /* Move back again if we got too far ahead. */
7035 if (IT_CHARPOS (*it) >= start_charpos)
7036 *it = it2;
7037 }
7038 }
7039 }
7040
7041 /* Return 1 if IT points into the middle of a display vector. */
7042
7043 int
7044 in_display_vector_p (it)
7045 struct it *it;
7046 {
7047 return (it->method == GET_FROM_DISPLAY_VECTOR
7048 && it->current.dpvec_index > 0
7049 && it->dpvec + it->current.dpvec_index != it->dpend);
7050 }
7051
7052 \f
7053 /***********************************************************************
7054 Messages
7055 ***********************************************************************/
7056
7057
7058 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7059 to *Messages*. */
7060
7061 void
7062 add_to_log (format, arg1, arg2)
7063 char *format;
7064 Lisp_Object arg1, arg2;
7065 {
7066 Lisp_Object args[3];
7067 Lisp_Object msg, fmt;
7068 char *buffer;
7069 int len;
7070 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7071 USE_SAFE_ALLOCA;
7072
7073 /* Do nothing if called asynchronously. Inserting text into
7074 a buffer may call after-change-functions and alike and
7075 that would means running Lisp asynchronously. */
7076 if (handling_signal)
7077 return;
7078
7079 fmt = msg = Qnil;
7080 GCPRO4 (fmt, msg, arg1, arg2);
7081
7082 args[0] = fmt = build_string (format);
7083 args[1] = arg1;
7084 args[2] = arg2;
7085 msg = Fformat (3, args);
7086
7087 len = SBYTES (msg) + 1;
7088 SAFE_ALLOCA (buffer, char *, len);
7089 bcopy (SDATA (msg), buffer, len);
7090
7091 message_dolog (buffer, len - 1, 1, 0);
7092 SAFE_FREE ();
7093
7094 UNGCPRO;
7095 }
7096
7097
7098 /* Output a newline in the *Messages* buffer if "needs" one. */
7099
7100 void
7101 message_log_maybe_newline ()
7102 {
7103 if (message_log_need_newline)
7104 message_dolog ("", 0, 1, 0);
7105 }
7106
7107
7108 /* Add a string M of length NBYTES to the message log, optionally
7109 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7110 nonzero, means interpret the contents of M as multibyte. This
7111 function calls low-level routines in order to bypass text property
7112 hooks, etc. which might not be safe to run.
7113
7114 This may GC (insert may run before/after change hooks),
7115 so the buffer M must NOT point to a Lisp string. */
7116
7117 void
7118 message_dolog (m, nbytes, nlflag, multibyte)
7119 const char *m;
7120 int nbytes, nlflag, multibyte;
7121 {
7122 if (!NILP (Vmemory_full))
7123 return;
7124
7125 if (!NILP (Vmessage_log_max))
7126 {
7127 struct buffer *oldbuf;
7128 Lisp_Object oldpoint, oldbegv, oldzv;
7129 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7130 int point_at_end = 0;
7131 int zv_at_end = 0;
7132 Lisp_Object old_deactivate_mark, tem;
7133 struct gcpro gcpro1;
7134
7135 old_deactivate_mark = Vdeactivate_mark;
7136 oldbuf = current_buffer;
7137 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7138 current_buffer->undo_list = Qt;
7139
7140 oldpoint = message_dolog_marker1;
7141 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7142 oldbegv = message_dolog_marker2;
7143 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7144 oldzv = message_dolog_marker3;
7145 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7146 GCPRO1 (old_deactivate_mark);
7147
7148 if (PT == Z)
7149 point_at_end = 1;
7150 if (ZV == Z)
7151 zv_at_end = 1;
7152
7153 BEGV = BEG;
7154 BEGV_BYTE = BEG_BYTE;
7155 ZV = Z;
7156 ZV_BYTE = Z_BYTE;
7157 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7158
7159 /* Insert the string--maybe converting multibyte to single byte
7160 or vice versa, so that all the text fits the buffer. */
7161 if (multibyte
7162 && NILP (current_buffer->enable_multibyte_characters))
7163 {
7164 int i, c, char_bytes;
7165 unsigned char work[1];
7166
7167 /* Convert a multibyte string to single-byte
7168 for the *Message* buffer. */
7169 for (i = 0; i < nbytes; i += char_bytes)
7170 {
7171 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7172 work[0] = (SINGLE_BYTE_CHAR_P (c)
7173 ? c
7174 : multibyte_char_to_unibyte (c, Qnil));
7175 insert_1_both (work, 1, 1, 1, 0, 0);
7176 }
7177 }
7178 else if (! multibyte
7179 && ! NILP (current_buffer->enable_multibyte_characters))
7180 {
7181 int i, c, char_bytes;
7182 unsigned char *msg = (unsigned char *) m;
7183 unsigned char str[MAX_MULTIBYTE_LENGTH];
7184 /* Convert a single-byte string to multibyte
7185 for the *Message* buffer. */
7186 for (i = 0; i < nbytes; i++)
7187 {
7188 c = unibyte_char_to_multibyte (msg[i]);
7189 char_bytes = CHAR_STRING (c, str);
7190 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7191 }
7192 }
7193 else if (nbytes)
7194 insert_1 (m, nbytes, 1, 0, 0);
7195
7196 if (nlflag)
7197 {
7198 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7199 insert_1 ("\n", 1, 1, 0, 0);
7200
7201 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7202 this_bol = PT;
7203 this_bol_byte = PT_BYTE;
7204
7205 /* See if this line duplicates the previous one.
7206 If so, combine duplicates. */
7207 if (this_bol > BEG)
7208 {
7209 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7210 prev_bol = PT;
7211 prev_bol_byte = PT_BYTE;
7212
7213 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7214 this_bol, this_bol_byte);
7215 if (dup)
7216 {
7217 del_range_both (prev_bol, prev_bol_byte,
7218 this_bol, this_bol_byte, 0);
7219 if (dup > 1)
7220 {
7221 char dupstr[40];
7222 int duplen;
7223
7224 /* If you change this format, don't forget to also
7225 change message_log_check_duplicate. */
7226 sprintf (dupstr, " [%d times]", dup);
7227 duplen = strlen (dupstr);
7228 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7229 insert_1 (dupstr, duplen, 1, 0, 1);
7230 }
7231 }
7232 }
7233
7234 /* If we have more than the desired maximum number of lines
7235 in the *Messages* buffer now, delete the oldest ones.
7236 This is safe because we don't have undo in this buffer. */
7237
7238 if (NATNUMP (Vmessage_log_max))
7239 {
7240 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7241 -XFASTINT (Vmessage_log_max) - 1, 0);
7242 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7243 }
7244 }
7245 BEGV = XMARKER (oldbegv)->charpos;
7246 BEGV_BYTE = marker_byte_position (oldbegv);
7247
7248 if (zv_at_end)
7249 {
7250 ZV = Z;
7251 ZV_BYTE = Z_BYTE;
7252 }
7253 else
7254 {
7255 ZV = XMARKER (oldzv)->charpos;
7256 ZV_BYTE = marker_byte_position (oldzv);
7257 }
7258
7259 if (point_at_end)
7260 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7261 else
7262 /* We can't do Fgoto_char (oldpoint) because it will run some
7263 Lisp code. */
7264 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7265 XMARKER (oldpoint)->bytepos);
7266
7267 UNGCPRO;
7268 unchain_marker (XMARKER (oldpoint));
7269 unchain_marker (XMARKER (oldbegv));
7270 unchain_marker (XMARKER (oldzv));
7271
7272 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7273 set_buffer_internal (oldbuf);
7274 if (NILP (tem))
7275 windows_or_buffers_changed = old_windows_or_buffers_changed;
7276 message_log_need_newline = !nlflag;
7277 Vdeactivate_mark = old_deactivate_mark;
7278 }
7279 }
7280
7281
7282 /* We are at the end of the buffer after just having inserted a newline.
7283 (Note: We depend on the fact we won't be crossing the gap.)
7284 Check to see if the most recent message looks a lot like the previous one.
7285 Return 0 if different, 1 if the new one should just replace it, or a
7286 value N > 1 if we should also append " [N times]". */
7287
7288 static int
7289 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7290 int prev_bol, this_bol;
7291 int prev_bol_byte, this_bol_byte;
7292 {
7293 int i;
7294 int len = Z_BYTE - 1 - this_bol_byte;
7295 int seen_dots = 0;
7296 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7297 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7298
7299 for (i = 0; i < len; i++)
7300 {
7301 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7302 seen_dots = 1;
7303 if (p1[i] != p2[i])
7304 return seen_dots;
7305 }
7306 p1 += len;
7307 if (*p1 == '\n')
7308 return 2;
7309 if (*p1++ == ' ' && *p1++ == '[')
7310 {
7311 int n = 0;
7312 while (*p1 >= '0' && *p1 <= '9')
7313 n = n * 10 + *p1++ - '0';
7314 if (strncmp (p1, " times]\n", 8) == 0)
7315 return n+1;
7316 }
7317 return 0;
7318 }
7319 \f
7320
7321 /* Display an echo area message M with a specified length of NBYTES
7322 bytes. The string may include null characters. If M is 0, clear
7323 out any existing message, and let the mini-buffer text show
7324 through.
7325
7326 This may GC, so the buffer M must NOT point to a Lisp string. */
7327
7328 void
7329 message2 (m, nbytes, multibyte)
7330 const char *m;
7331 int nbytes;
7332 int multibyte;
7333 {
7334 /* First flush out any partial line written with print. */
7335 message_log_maybe_newline ();
7336 if (m)
7337 message_dolog (m, nbytes, 1, multibyte);
7338 message2_nolog (m, nbytes, multibyte);
7339 }
7340
7341
7342 /* The non-logging counterpart of message2. */
7343
7344 void
7345 message2_nolog (m, nbytes, multibyte)
7346 const char *m;
7347 int nbytes, multibyte;
7348 {
7349 struct frame *sf = SELECTED_FRAME ();
7350 message_enable_multibyte = multibyte;
7351
7352 if (noninteractive)
7353 {
7354 if (noninteractive_need_newline)
7355 putc ('\n', stderr);
7356 noninteractive_need_newline = 0;
7357 if (m)
7358 fwrite (m, nbytes, 1, stderr);
7359 if (cursor_in_echo_area == 0)
7360 fprintf (stderr, "\n");
7361 fflush (stderr);
7362 }
7363 /* A null message buffer means that the frame hasn't really been
7364 initialized yet. Error messages get reported properly by
7365 cmd_error, so this must be just an informative message; toss it. */
7366 else if (INTERACTIVE
7367 && sf->glyphs_initialized_p
7368 && FRAME_MESSAGE_BUF (sf))
7369 {
7370 Lisp_Object mini_window;
7371 struct frame *f;
7372
7373 /* Get the frame containing the mini-buffer
7374 that the selected frame is using. */
7375 mini_window = FRAME_MINIBUF_WINDOW (sf);
7376 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7377
7378 FRAME_SAMPLE_VISIBILITY (f);
7379 if (FRAME_VISIBLE_P (sf)
7380 && ! FRAME_VISIBLE_P (f))
7381 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7382
7383 if (m)
7384 {
7385 set_message (m, Qnil, nbytes, multibyte);
7386 if (minibuffer_auto_raise)
7387 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7388 }
7389 else
7390 clear_message (1, 1);
7391
7392 do_pending_window_change (0);
7393 echo_area_display (1);
7394 do_pending_window_change (0);
7395 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7396 (*frame_up_to_date_hook) (f);
7397 }
7398 }
7399
7400
7401 /* Display an echo area message M with a specified length of NBYTES
7402 bytes. The string may include null characters. If M is not a
7403 string, clear out any existing message, and let the mini-buffer
7404 text show through.
7405
7406 This function cancels echoing. */
7407
7408 void
7409 message3 (m, nbytes, multibyte)
7410 Lisp_Object m;
7411 int nbytes;
7412 int multibyte;
7413 {
7414 struct gcpro gcpro1;
7415
7416 GCPRO1 (m);
7417 clear_message (1,1);
7418 cancel_echoing ();
7419
7420 /* First flush out any partial line written with print. */
7421 message_log_maybe_newline ();
7422 if (STRINGP (m))
7423 {
7424 char *buffer;
7425 USE_SAFE_ALLOCA;
7426
7427 SAFE_ALLOCA (buffer, char *, nbytes);
7428 bcopy (SDATA (m), buffer, nbytes);
7429 message_dolog (buffer, nbytes, 1, multibyte);
7430 SAFE_FREE ();
7431 }
7432 message3_nolog (m, nbytes, multibyte);
7433
7434 UNGCPRO;
7435 }
7436
7437
7438 /* The non-logging version of message3.
7439 This does not cancel echoing, because it is used for echoing.
7440 Perhaps we need to make a separate function for echoing
7441 and make this cancel echoing. */
7442
7443 void
7444 message3_nolog (m, nbytes, multibyte)
7445 Lisp_Object m;
7446 int nbytes, multibyte;
7447 {
7448 struct frame *sf = SELECTED_FRAME ();
7449 message_enable_multibyte = multibyte;
7450
7451 if (noninteractive)
7452 {
7453 if (noninteractive_need_newline)
7454 putc ('\n', stderr);
7455 noninteractive_need_newline = 0;
7456 if (STRINGP (m))
7457 fwrite (SDATA (m), nbytes, 1, stderr);
7458 if (cursor_in_echo_area == 0)
7459 fprintf (stderr, "\n");
7460 fflush (stderr);
7461 }
7462 /* A null message buffer means that the frame hasn't really been
7463 initialized yet. Error messages get reported properly by
7464 cmd_error, so this must be just an informative message; toss it. */
7465 else if (INTERACTIVE
7466 && sf->glyphs_initialized_p
7467 && FRAME_MESSAGE_BUF (sf))
7468 {
7469 Lisp_Object mini_window;
7470 Lisp_Object frame;
7471 struct frame *f;
7472
7473 /* Get the frame containing the mini-buffer
7474 that the selected frame is using. */
7475 mini_window = FRAME_MINIBUF_WINDOW (sf);
7476 frame = XWINDOW (mini_window)->frame;
7477 f = XFRAME (frame);
7478
7479 FRAME_SAMPLE_VISIBILITY (f);
7480 if (FRAME_VISIBLE_P (sf)
7481 && !FRAME_VISIBLE_P (f))
7482 Fmake_frame_visible (frame);
7483
7484 if (STRINGP (m) && SCHARS (m) > 0)
7485 {
7486 set_message (NULL, m, nbytes, multibyte);
7487 if (minibuffer_auto_raise)
7488 Fraise_frame (frame);
7489 /* Assume we are not echoing.
7490 (If we are, echo_now will override this.) */
7491 echo_message_buffer = Qnil;
7492 }
7493 else
7494 clear_message (1, 1);
7495
7496 do_pending_window_change (0);
7497 echo_area_display (1);
7498 do_pending_window_change (0);
7499 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7500 (*frame_up_to_date_hook) (f);
7501 }
7502 }
7503
7504
7505 /* Display a null-terminated echo area message M. If M is 0, clear
7506 out any existing message, and let the mini-buffer text show through.
7507
7508 The buffer M must continue to exist until after the echo area gets
7509 cleared or some other message gets displayed there. Do not pass
7510 text that is stored in a Lisp string. Do not pass text in a buffer
7511 that was alloca'd. */
7512
7513 void
7514 message1 (m)
7515 char *m;
7516 {
7517 message2 (m, (m ? strlen (m) : 0), 0);
7518 }
7519
7520
7521 /* The non-logging counterpart of message1. */
7522
7523 void
7524 message1_nolog (m)
7525 char *m;
7526 {
7527 message2_nolog (m, (m ? strlen (m) : 0), 0);
7528 }
7529
7530 /* Display a message M which contains a single %s
7531 which gets replaced with STRING. */
7532
7533 void
7534 message_with_string (m, string, log)
7535 char *m;
7536 Lisp_Object string;
7537 int log;
7538 {
7539 CHECK_STRING (string);
7540
7541 if (noninteractive)
7542 {
7543 if (m)
7544 {
7545 if (noninteractive_need_newline)
7546 putc ('\n', stderr);
7547 noninteractive_need_newline = 0;
7548 fprintf (stderr, m, SDATA (string));
7549 if (cursor_in_echo_area == 0)
7550 fprintf (stderr, "\n");
7551 fflush (stderr);
7552 }
7553 }
7554 else if (INTERACTIVE)
7555 {
7556 /* The frame whose minibuffer we're going to display the message on.
7557 It may be larger than the selected frame, so we need
7558 to use its buffer, not the selected frame's buffer. */
7559 Lisp_Object mini_window;
7560 struct frame *f, *sf = SELECTED_FRAME ();
7561
7562 /* Get the frame containing the minibuffer
7563 that the selected frame is using. */
7564 mini_window = FRAME_MINIBUF_WINDOW (sf);
7565 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7566
7567 /* A null message buffer means that the frame hasn't really been
7568 initialized yet. Error messages get reported properly by
7569 cmd_error, so this must be just an informative message; toss it. */
7570 if (FRAME_MESSAGE_BUF (f))
7571 {
7572 Lisp_Object args[2], message;
7573 struct gcpro gcpro1, gcpro2;
7574
7575 args[0] = build_string (m);
7576 args[1] = message = string;
7577 GCPRO2 (args[0], message);
7578 gcpro1.nvars = 2;
7579
7580 message = Fformat (2, args);
7581
7582 if (log)
7583 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7584 else
7585 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7586
7587 UNGCPRO;
7588
7589 /* Print should start at the beginning of the message
7590 buffer next time. */
7591 message_buf_print = 0;
7592 }
7593 }
7594 }
7595
7596
7597 /* Dump an informative message to the minibuf. If M is 0, clear out
7598 any existing message, and let the mini-buffer text show through. */
7599
7600 /* VARARGS 1 */
7601 void
7602 message (m, a1, a2, a3)
7603 char *m;
7604 EMACS_INT a1, a2, a3;
7605 {
7606 if (noninteractive)
7607 {
7608 if (m)
7609 {
7610 if (noninteractive_need_newline)
7611 putc ('\n', stderr);
7612 noninteractive_need_newline = 0;
7613 fprintf (stderr, m, a1, a2, a3);
7614 if (cursor_in_echo_area == 0)
7615 fprintf (stderr, "\n");
7616 fflush (stderr);
7617 }
7618 }
7619 else if (INTERACTIVE)
7620 {
7621 /* The frame whose mini-buffer we're going to display the message
7622 on. It may be larger than the selected frame, so we need to
7623 use its buffer, not the selected frame's buffer. */
7624 Lisp_Object mini_window;
7625 struct frame *f, *sf = SELECTED_FRAME ();
7626
7627 /* Get the frame containing the mini-buffer
7628 that the selected frame is using. */
7629 mini_window = FRAME_MINIBUF_WINDOW (sf);
7630 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7631
7632 /* A null message buffer means that the frame hasn't really been
7633 initialized yet. Error messages get reported properly by
7634 cmd_error, so this must be just an informative message; toss
7635 it. */
7636 if (FRAME_MESSAGE_BUF (f))
7637 {
7638 if (m)
7639 {
7640 int len;
7641 #ifdef NO_ARG_ARRAY
7642 char *a[3];
7643 a[0] = (char *) a1;
7644 a[1] = (char *) a2;
7645 a[2] = (char *) a3;
7646
7647 len = doprnt (FRAME_MESSAGE_BUF (f),
7648 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7649 #else
7650 len = doprnt (FRAME_MESSAGE_BUF (f),
7651 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7652 (char **) &a1);
7653 #endif /* NO_ARG_ARRAY */
7654
7655 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7656 }
7657 else
7658 message1 (0);
7659
7660 /* Print should start at the beginning of the message
7661 buffer next time. */
7662 message_buf_print = 0;
7663 }
7664 }
7665 }
7666
7667
7668 /* The non-logging version of message. */
7669
7670 void
7671 message_nolog (m, a1, a2, a3)
7672 char *m;
7673 EMACS_INT a1, a2, a3;
7674 {
7675 Lisp_Object old_log_max;
7676 old_log_max = Vmessage_log_max;
7677 Vmessage_log_max = Qnil;
7678 message (m, a1, a2, a3);
7679 Vmessage_log_max = old_log_max;
7680 }
7681
7682
7683 /* Display the current message in the current mini-buffer. This is
7684 only called from error handlers in process.c, and is not time
7685 critical. */
7686
7687 void
7688 update_echo_area ()
7689 {
7690 if (!NILP (echo_area_buffer[0]))
7691 {
7692 Lisp_Object string;
7693 string = Fcurrent_message ();
7694 message3 (string, SBYTES (string),
7695 !NILP (current_buffer->enable_multibyte_characters));
7696 }
7697 }
7698
7699
7700 /* Make sure echo area buffers in `echo_buffers' are live.
7701 If they aren't, make new ones. */
7702
7703 static void
7704 ensure_echo_area_buffers ()
7705 {
7706 int i;
7707
7708 for (i = 0; i < 2; ++i)
7709 if (!BUFFERP (echo_buffer[i])
7710 || NILP (XBUFFER (echo_buffer[i])->name))
7711 {
7712 char name[30];
7713 Lisp_Object old_buffer;
7714 int j;
7715
7716 old_buffer = echo_buffer[i];
7717 sprintf (name, " *Echo Area %d*", i);
7718 echo_buffer[i] = Fget_buffer_create (build_string (name));
7719 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7720
7721 for (j = 0; j < 2; ++j)
7722 if (EQ (old_buffer, echo_area_buffer[j]))
7723 echo_area_buffer[j] = echo_buffer[i];
7724 }
7725 }
7726
7727
7728 /* Call FN with args A1..A4 with either the current or last displayed
7729 echo_area_buffer as current buffer.
7730
7731 WHICH zero means use the current message buffer
7732 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7733 from echo_buffer[] and clear it.
7734
7735 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7736 suitable buffer from echo_buffer[] and clear it.
7737
7738 Value is what FN returns. */
7739
7740 static int
7741 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7742 struct window *w;
7743 int which;
7744 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7745 EMACS_INT a1;
7746 Lisp_Object a2;
7747 EMACS_INT a3, a4;
7748 {
7749 Lisp_Object buffer;
7750 int this_one, the_other, clear_buffer_p, rc;
7751 int count = SPECPDL_INDEX ();
7752
7753 /* If buffers aren't live, make new ones. */
7754 ensure_echo_area_buffers ();
7755
7756 clear_buffer_p = 0;
7757
7758 if (which == 0)
7759 this_one = 0, the_other = 1;
7760 else if (which > 0)
7761 this_one = 1, the_other = 0;
7762
7763 /* Choose a suitable buffer from echo_buffer[] is we don't
7764 have one. */
7765 if (NILP (echo_area_buffer[this_one]))
7766 {
7767 echo_area_buffer[this_one]
7768 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7769 ? echo_buffer[the_other]
7770 : echo_buffer[this_one]);
7771 clear_buffer_p = 1;
7772 }
7773
7774 buffer = echo_area_buffer[this_one];
7775
7776 /* Don't get confused by reusing the buffer used for echoing
7777 for a different purpose. */
7778 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7779 cancel_echoing ();
7780
7781 record_unwind_protect (unwind_with_echo_area_buffer,
7782 with_echo_area_buffer_unwind_data (w));
7783
7784 /* Make the echo area buffer current. Note that for display
7785 purposes, it is not necessary that the displayed window's buffer
7786 == current_buffer, except for text property lookup. So, let's
7787 only set that buffer temporarily here without doing a full
7788 Fset_window_buffer. We must also change w->pointm, though,
7789 because otherwise an assertions in unshow_buffer fails, and Emacs
7790 aborts. */
7791 set_buffer_internal_1 (XBUFFER (buffer));
7792 if (w)
7793 {
7794 w->buffer = buffer;
7795 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7796 }
7797
7798 current_buffer->undo_list = Qt;
7799 current_buffer->read_only = Qnil;
7800 specbind (Qinhibit_read_only, Qt);
7801 specbind (Qinhibit_modification_hooks, Qt);
7802
7803 if (clear_buffer_p && Z > BEG)
7804 del_range (BEG, Z);
7805
7806 xassert (BEGV >= BEG);
7807 xassert (ZV <= Z && ZV >= BEGV);
7808
7809 rc = fn (a1, a2, a3, a4);
7810
7811 xassert (BEGV >= BEG);
7812 xassert (ZV <= Z && ZV >= BEGV);
7813
7814 unbind_to (count, Qnil);
7815 return rc;
7816 }
7817
7818
7819 /* Save state that should be preserved around the call to the function
7820 FN called in with_echo_area_buffer. */
7821
7822 static Lisp_Object
7823 with_echo_area_buffer_unwind_data (w)
7824 struct window *w;
7825 {
7826 int i = 0;
7827 Lisp_Object vector;
7828
7829 /* Reduce consing by keeping one vector in
7830 Vwith_echo_area_save_vector. */
7831 vector = Vwith_echo_area_save_vector;
7832 Vwith_echo_area_save_vector = Qnil;
7833
7834 if (NILP (vector))
7835 vector = Fmake_vector (make_number (7), Qnil);
7836
7837 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7838 AREF (vector, i) = Vdeactivate_mark, ++i;
7839 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7840
7841 if (w)
7842 {
7843 XSETWINDOW (AREF (vector, i), w); ++i;
7844 AREF (vector, i) = w->buffer; ++i;
7845 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7846 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7847 }
7848 else
7849 {
7850 int end = i + 4;
7851 for (; i < end; ++i)
7852 AREF (vector, i) = Qnil;
7853 }
7854
7855 xassert (i == ASIZE (vector));
7856 return vector;
7857 }
7858
7859
7860 /* Restore global state from VECTOR which was created by
7861 with_echo_area_buffer_unwind_data. */
7862
7863 static Lisp_Object
7864 unwind_with_echo_area_buffer (vector)
7865 Lisp_Object vector;
7866 {
7867 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7868 Vdeactivate_mark = AREF (vector, 1);
7869 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7870
7871 if (WINDOWP (AREF (vector, 3)))
7872 {
7873 struct window *w;
7874 Lisp_Object buffer, charpos, bytepos;
7875
7876 w = XWINDOW (AREF (vector, 3));
7877 buffer = AREF (vector, 4);
7878 charpos = AREF (vector, 5);
7879 bytepos = AREF (vector, 6);
7880
7881 w->buffer = buffer;
7882 set_marker_both (w->pointm, buffer,
7883 XFASTINT (charpos), XFASTINT (bytepos));
7884 }
7885
7886 Vwith_echo_area_save_vector = vector;
7887 return Qnil;
7888 }
7889
7890
7891 /* Set up the echo area for use by print functions. MULTIBYTE_P
7892 non-zero means we will print multibyte. */
7893
7894 void
7895 setup_echo_area_for_printing (multibyte_p)
7896 int multibyte_p;
7897 {
7898 /* If we can't find an echo area any more, exit. */
7899 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7900 Fkill_emacs (Qnil);
7901
7902 ensure_echo_area_buffers ();
7903
7904 if (!message_buf_print)
7905 {
7906 /* A message has been output since the last time we printed.
7907 Choose a fresh echo area buffer. */
7908 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7909 echo_area_buffer[0] = echo_buffer[1];
7910 else
7911 echo_area_buffer[0] = echo_buffer[0];
7912
7913 /* Switch to that buffer and clear it. */
7914 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7915 current_buffer->truncate_lines = Qnil;
7916
7917 if (Z > BEG)
7918 {
7919 int count = SPECPDL_INDEX ();
7920 specbind (Qinhibit_read_only, Qt);
7921 /* Note that undo recording is always disabled. */
7922 del_range (BEG, Z);
7923 unbind_to (count, Qnil);
7924 }
7925 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7926
7927 /* Set up the buffer for the multibyteness we need. */
7928 if (multibyte_p
7929 != !NILP (current_buffer->enable_multibyte_characters))
7930 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7931
7932 /* Raise the frame containing the echo area. */
7933 if (minibuffer_auto_raise)
7934 {
7935 struct frame *sf = SELECTED_FRAME ();
7936 Lisp_Object mini_window;
7937 mini_window = FRAME_MINIBUF_WINDOW (sf);
7938 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7939 }
7940
7941 message_log_maybe_newline ();
7942 message_buf_print = 1;
7943 }
7944 else
7945 {
7946 if (NILP (echo_area_buffer[0]))
7947 {
7948 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7949 echo_area_buffer[0] = echo_buffer[1];
7950 else
7951 echo_area_buffer[0] = echo_buffer[0];
7952 }
7953
7954 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7955 {
7956 /* Someone switched buffers between print requests. */
7957 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7958 current_buffer->truncate_lines = Qnil;
7959 }
7960 }
7961 }
7962
7963
7964 /* Display an echo area message in window W. Value is non-zero if W's
7965 height is changed. If display_last_displayed_message_p is
7966 non-zero, display the message that was last displayed, otherwise
7967 display the current message. */
7968
7969 static int
7970 display_echo_area (w)
7971 struct window *w;
7972 {
7973 int i, no_message_p, window_height_changed_p, count;
7974
7975 /* Temporarily disable garbage collections while displaying the echo
7976 area. This is done because a GC can print a message itself.
7977 That message would modify the echo area buffer's contents while a
7978 redisplay of the buffer is going on, and seriously confuse
7979 redisplay. */
7980 count = inhibit_garbage_collection ();
7981
7982 /* If there is no message, we must call display_echo_area_1
7983 nevertheless because it resizes the window. But we will have to
7984 reset the echo_area_buffer in question to nil at the end because
7985 with_echo_area_buffer will sets it to an empty buffer. */
7986 i = display_last_displayed_message_p ? 1 : 0;
7987 no_message_p = NILP (echo_area_buffer[i]);
7988
7989 window_height_changed_p
7990 = with_echo_area_buffer (w, display_last_displayed_message_p,
7991 display_echo_area_1,
7992 (EMACS_INT) w, Qnil, 0, 0);
7993
7994 if (no_message_p)
7995 echo_area_buffer[i] = Qnil;
7996
7997 unbind_to (count, Qnil);
7998 return window_height_changed_p;
7999 }
8000
8001
8002 /* Helper for display_echo_area. Display the current buffer which
8003 contains the current echo area message in window W, a mini-window,
8004 a pointer to which is passed in A1. A2..A4 are currently not used.
8005 Change the height of W so that all of the message is displayed.
8006 Value is non-zero if height of W was changed. */
8007
8008 static int
8009 display_echo_area_1 (a1, a2, a3, a4)
8010 EMACS_INT a1;
8011 Lisp_Object a2;
8012 EMACS_INT a3, a4;
8013 {
8014 struct window *w = (struct window *) a1;
8015 Lisp_Object window;
8016 struct text_pos start;
8017 int window_height_changed_p = 0;
8018
8019 /* Do this before displaying, so that we have a large enough glyph
8020 matrix for the display. If we can't get enough space for the
8021 whole text, display the last N lines. That works by setting w->start. */
8022 window_height_changed_p = resize_mini_window (w, 0);
8023
8024 /* Use the starting position chosen by resize_mini_window. */
8025 SET_TEXT_POS_FROM_MARKER (start, w->start);
8026
8027 /* Display. */
8028 clear_glyph_matrix (w->desired_matrix);
8029 XSETWINDOW (window, w);
8030 try_window (window, start, 0);
8031
8032 return window_height_changed_p;
8033 }
8034
8035
8036 /* Resize the echo area window to exactly the size needed for the
8037 currently displayed message, if there is one. If a mini-buffer
8038 is active, don't shrink it. */
8039
8040 void
8041 resize_echo_area_exactly ()
8042 {
8043 if (BUFFERP (echo_area_buffer[0])
8044 && WINDOWP (echo_area_window))
8045 {
8046 struct window *w = XWINDOW (echo_area_window);
8047 int resized_p;
8048 Lisp_Object resize_exactly;
8049
8050 if (minibuf_level == 0)
8051 resize_exactly = Qt;
8052 else
8053 resize_exactly = Qnil;
8054
8055 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8056 (EMACS_INT) w, resize_exactly, 0, 0);
8057 if (resized_p)
8058 {
8059 ++windows_or_buffers_changed;
8060 ++update_mode_lines;
8061 redisplay_internal (0);
8062 }
8063 }
8064 }
8065
8066
8067 /* Callback function for with_echo_area_buffer, when used from
8068 resize_echo_area_exactly. A1 contains a pointer to the window to
8069 resize, EXACTLY non-nil means resize the mini-window exactly to the
8070 size of the text displayed. A3 and A4 are not used. Value is what
8071 resize_mini_window returns. */
8072
8073 static int
8074 resize_mini_window_1 (a1, exactly, a3, a4)
8075 EMACS_INT a1;
8076 Lisp_Object exactly;
8077 EMACS_INT a3, a4;
8078 {
8079 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8080 }
8081
8082
8083 /* Resize mini-window W to fit the size of its contents. EXACT:P
8084 means size the window exactly to the size needed. Otherwise, it's
8085 only enlarged until W's buffer is empty.
8086
8087 Set W->start to the right place to begin display. If the whole
8088 contents fit, start at the beginning. Otherwise, start so as
8089 to make the end of the contents appear. This is particularly
8090 important for y-or-n-p, but seems desirable generally.
8091
8092 Value is non-zero if the window height has been changed. */
8093
8094 int
8095 resize_mini_window (w, exact_p)
8096 struct window *w;
8097 int exact_p;
8098 {
8099 struct frame *f = XFRAME (w->frame);
8100 int window_height_changed_p = 0;
8101
8102 xassert (MINI_WINDOW_P (w));
8103
8104 /* By default, start display at the beginning. */
8105 set_marker_both (w->start, w->buffer,
8106 BUF_BEGV (XBUFFER (w->buffer)),
8107 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8108
8109 /* Don't resize windows while redisplaying a window; it would
8110 confuse redisplay functions when the size of the window they are
8111 displaying changes from under them. Such a resizing can happen,
8112 for instance, when which-func prints a long message while
8113 we are running fontification-functions. We're running these
8114 functions with safe_call which binds inhibit-redisplay to t. */
8115 if (!NILP (Vinhibit_redisplay))
8116 return 0;
8117
8118 /* Nil means don't try to resize. */
8119 if (NILP (Vresize_mini_windows)
8120 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8121 return 0;
8122
8123 if (!FRAME_MINIBUF_ONLY_P (f))
8124 {
8125 struct it it;
8126 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8127 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8128 int height, max_height;
8129 int unit = FRAME_LINE_HEIGHT (f);
8130 struct text_pos start;
8131 struct buffer *old_current_buffer = NULL;
8132
8133 if (current_buffer != XBUFFER (w->buffer))
8134 {
8135 old_current_buffer = current_buffer;
8136 set_buffer_internal (XBUFFER (w->buffer));
8137 }
8138
8139 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8140
8141 /* Compute the max. number of lines specified by the user. */
8142 if (FLOATP (Vmax_mini_window_height))
8143 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8144 else if (INTEGERP (Vmax_mini_window_height))
8145 max_height = XINT (Vmax_mini_window_height);
8146 else
8147 max_height = total_height / 4;
8148
8149 /* Correct that max. height if it's bogus. */
8150 max_height = max (1, max_height);
8151 max_height = min (total_height, max_height);
8152
8153 /* Find out the height of the text in the window. */
8154 if (it.truncate_lines_p)
8155 height = 1;
8156 else
8157 {
8158 last_height = 0;
8159 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8160 if (it.max_ascent == 0 && it.max_descent == 0)
8161 height = it.current_y + last_height;
8162 else
8163 height = it.current_y + it.max_ascent + it.max_descent;
8164 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8165 height = (height + unit - 1) / unit;
8166 }
8167
8168 /* Compute a suitable window start. */
8169 if (height > max_height)
8170 {
8171 height = max_height;
8172 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8173 move_it_vertically_backward (&it, (height - 1) * unit);
8174 start = it.current.pos;
8175 }
8176 else
8177 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8178 SET_MARKER_FROM_TEXT_POS (w->start, start);
8179
8180 if (EQ (Vresize_mini_windows, Qgrow_only))
8181 {
8182 /* Let it grow only, until we display an empty message, in which
8183 case the window shrinks again. */
8184 if (height > WINDOW_TOTAL_LINES (w))
8185 {
8186 int old_height = WINDOW_TOTAL_LINES (w);
8187 freeze_window_starts (f, 1);
8188 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8189 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8190 }
8191 else if (height < WINDOW_TOTAL_LINES (w)
8192 && (exact_p || BEGV == ZV))
8193 {
8194 int old_height = WINDOW_TOTAL_LINES (w);
8195 freeze_window_starts (f, 0);
8196 shrink_mini_window (w);
8197 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8198 }
8199 }
8200 else
8201 {
8202 /* Always resize to exact size needed. */
8203 if (height > WINDOW_TOTAL_LINES (w))
8204 {
8205 int old_height = WINDOW_TOTAL_LINES (w);
8206 freeze_window_starts (f, 1);
8207 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8208 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8209 }
8210 else if (height < WINDOW_TOTAL_LINES (w))
8211 {
8212 int old_height = WINDOW_TOTAL_LINES (w);
8213 freeze_window_starts (f, 0);
8214 shrink_mini_window (w);
8215
8216 if (height)
8217 {
8218 freeze_window_starts (f, 1);
8219 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8220 }
8221
8222 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8223 }
8224 }
8225
8226 if (old_current_buffer)
8227 set_buffer_internal (old_current_buffer);
8228 }
8229
8230 return window_height_changed_p;
8231 }
8232
8233
8234 /* Value is the current message, a string, or nil if there is no
8235 current message. */
8236
8237 Lisp_Object
8238 current_message ()
8239 {
8240 Lisp_Object msg;
8241
8242 if (NILP (echo_area_buffer[0]))
8243 msg = Qnil;
8244 else
8245 {
8246 with_echo_area_buffer (0, 0, current_message_1,
8247 (EMACS_INT) &msg, Qnil, 0, 0);
8248 if (NILP (msg))
8249 echo_area_buffer[0] = Qnil;
8250 }
8251
8252 return msg;
8253 }
8254
8255
8256 static int
8257 current_message_1 (a1, a2, a3, a4)
8258 EMACS_INT a1;
8259 Lisp_Object a2;
8260 EMACS_INT a3, a4;
8261 {
8262 Lisp_Object *msg = (Lisp_Object *) a1;
8263
8264 if (Z > BEG)
8265 *msg = make_buffer_string (BEG, Z, 1);
8266 else
8267 *msg = Qnil;
8268 return 0;
8269 }
8270
8271
8272 /* Push the current message on Vmessage_stack for later restauration
8273 by restore_message. Value is non-zero if the current message isn't
8274 empty. This is a relatively infrequent operation, so it's not
8275 worth optimizing. */
8276
8277 int
8278 push_message ()
8279 {
8280 Lisp_Object msg;
8281 msg = current_message ();
8282 Vmessage_stack = Fcons (msg, Vmessage_stack);
8283 return STRINGP (msg);
8284 }
8285
8286
8287 /* Restore message display from the top of Vmessage_stack. */
8288
8289 void
8290 restore_message ()
8291 {
8292 Lisp_Object msg;
8293
8294 xassert (CONSP (Vmessage_stack));
8295 msg = XCAR (Vmessage_stack);
8296 if (STRINGP (msg))
8297 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8298 else
8299 message3_nolog (msg, 0, 0);
8300 }
8301
8302
8303 /* Handler for record_unwind_protect calling pop_message. */
8304
8305 Lisp_Object
8306 pop_message_unwind (dummy)
8307 Lisp_Object dummy;
8308 {
8309 pop_message ();
8310 return Qnil;
8311 }
8312
8313 /* Pop the top-most entry off Vmessage_stack. */
8314
8315 void
8316 pop_message ()
8317 {
8318 xassert (CONSP (Vmessage_stack));
8319 Vmessage_stack = XCDR (Vmessage_stack);
8320 }
8321
8322
8323 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8324 exits. If the stack is not empty, we have a missing pop_message
8325 somewhere. */
8326
8327 void
8328 check_message_stack ()
8329 {
8330 if (!NILP (Vmessage_stack))
8331 abort ();
8332 }
8333
8334
8335 /* Truncate to NCHARS what will be displayed in the echo area the next
8336 time we display it---but don't redisplay it now. */
8337
8338 void
8339 truncate_echo_area (nchars)
8340 int nchars;
8341 {
8342 if (nchars == 0)
8343 echo_area_buffer[0] = Qnil;
8344 /* A null message buffer means that the frame hasn't really been
8345 initialized yet. Error messages get reported properly by
8346 cmd_error, so this must be just an informative message; toss it. */
8347 else if (!noninteractive
8348 && INTERACTIVE
8349 && !NILP (echo_area_buffer[0]))
8350 {
8351 struct frame *sf = SELECTED_FRAME ();
8352 if (FRAME_MESSAGE_BUF (sf))
8353 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8354 }
8355 }
8356
8357
8358 /* Helper function for truncate_echo_area. Truncate the current
8359 message to at most NCHARS characters. */
8360
8361 static int
8362 truncate_message_1 (nchars, a2, a3, a4)
8363 EMACS_INT nchars;
8364 Lisp_Object a2;
8365 EMACS_INT a3, a4;
8366 {
8367 if (BEG + nchars < Z)
8368 del_range (BEG + nchars, Z);
8369 if (Z == BEG)
8370 echo_area_buffer[0] = Qnil;
8371 return 0;
8372 }
8373
8374
8375 /* Set the current message to a substring of S or STRING.
8376
8377 If STRING is a Lisp string, set the message to the first NBYTES
8378 bytes from STRING. NBYTES zero means use the whole string. If
8379 STRING is multibyte, the message will be displayed multibyte.
8380
8381 If S is not null, set the message to the first LEN bytes of S. LEN
8382 zero means use the whole string. MULTIBYTE_P non-zero means S is
8383 multibyte. Display the message multibyte in that case.
8384
8385 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8386 to t before calling set_message_1 (which calls insert).
8387 */
8388
8389 void
8390 set_message (s, string, nbytes, multibyte_p)
8391 const char *s;
8392 Lisp_Object string;
8393 int nbytes, multibyte_p;
8394 {
8395 message_enable_multibyte
8396 = ((s && multibyte_p)
8397 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8398
8399 with_echo_area_buffer (0, 0, set_message_1,
8400 (EMACS_INT) s, string, nbytes, multibyte_p);
8401 message_buf_print = 0;
8402 help_echo_showing_p = 0;
8403 }
8404
8405
8406 /* Helper function for set_message. Arguments have the same meaning
8407 as there, with A1 corresponding to S and A2 corresponding to STRING
8408 This function is called with the echo area buffer being
8409 current. */
8410
8411 static int
8412 set_message_1 (a1, a2, nbytes, multibyte_p)
8413 EMACS_INT a1;
8414 Lisp_Object a2;
8415 EMACS_INT nbytes, multibyte_p;
8416 {
8417 const char *s = (const char *) a1;
8418 Lisp_Object string = a2;
8419
8420 /* Change multibyteness of the echo buffer appropriately. */
8421 if (message_enable_multibyte
8422 != !NILP (current_buffer->enable_multibyte_characters))
8423 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8424
8425 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8426
8427 /* Insert new message at BEG. */
8428 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8429 Ferase_buffer ();
8430
8431 if (STRINGP (string))
8432 {
8433 int nchars;
8434
8435 if (nbytes == 0)
8436 nbytes = SBYTES (string);
8437 nchars = string_byte_to_char (string, nbytes);
8438
8439 /* This function takes care of single/multibyte conversion. We
8440 just have to ensure that the echo area buffer has the right
8441 setting of enable_multibyte_characters. */
8442 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8443 }
8444 else if (s)
8445 {
8446 if (nbytes == 0)
8447 nbytes = strlen (s);
8448
8449 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8450 {
8451 /* Convert from multi-byte to single-byte. */
8452 int i, c, n;
8453 unsigned char work[1];
8454
8455 /* Convert a multibyte string to single-byte. */
8456 for (i = 0; i < nbytes; i += n)
8457 {
8458 c = string_char_and_length (s + i, nbytes - i, &n);
8459 work[0] = (SINGLE_BYTE_CHAR_P (c)
8460 ? c
8461 : multibyte_char_to_unibyte (c, Qnil));
8462 insert_1_both (work, 1, 1, 1, 0, 0);
8463 }
8464 }
8465 else if (!multibyte_p
8466 && !NILP (current_buffer->enable_multibyte_characters))
8467 {
8468 /* Convert from single-byte to multi-byte. */
8469 int i, c, n;
8470 const unsigned char *msg = (const unsigned char *) s;
8471 unsigned char str[MAX_MULTIBYTE_LENGTH];
8472
8473 /* Convert a single-byte string to multibyte. */
8474 for (i = 0; i < nbytes; i++)
8475 {
8476 c = unibyte_char_to_multibyte (msg[i]);
8477 n = CHAR_STRING (c, str);
8478 insert_1_both (str, 1, n, 1, 0, 0);
8479 }
8480 }
8481 else
8482 insert_1 (s, nbytes, 1, 0, 0);
8483 }
8484
8485 return 0;
8486 }
8487
8488
8489 /* Clear messages. CURRENT_P non-zero means clear the current
8490 message. LAST_DISPLAYED_P non-zero means clear the message
8491 last displayed. */
8492
8493 void
8494 clear_message (current_p, last_displayed_p)
8495 int current_p, last_displayed_p;
8496 {
8497 if (current_p)
8498 {
8499 echo_area_buffer[0] = Qnil;
8500 message_cleared_p = 1;
8501 }
8502
8503 if (last_displayed_p)
8504 echo_area_buffer[1] = Qnil;
8505
8506 message_buf_print = 0;
8507 }
8508
8509 /* Clear garbaged frames.
8510
8511 This function is used where the old redisplay called
8512 redraw_garbaged_frames which in turn called redraw_frame which in
8513 turn called clear_frame. The call to clear_frame was a source of
8514 flickering. I believe a clear_frame is not necessary. It should
8515 suffice in the new redisplay to invalidate all current matrices,
8516 and ensure a complete redisplay of all windows. */
8517
8518 static void
8519 clear_garbaged_frames ()
8520 {
8521 if (frame_garbaged)
8522 {
8523 Lisp_Object tail, frame;
8524 int changed_count = 0;
8525
8526 FOR_EACH_FRAME (tail, frame)
8527 {
8528 struct frame *f = XFRAME (frame);
8529
8530 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8531 {
8532 if (f->resized_p)
8533 {
8534 Fredraw_frame (frame);
8535 f->force_flush_display_p = 1;
8536 }
8537 clear_current_matrices (f);
8538 changed_count++;
8539 f->garbaged = 0;
8540 f->resized_p = 0;
8541 }
8542 }
8543
8544 frame_garbaged = 0;
8545 if (changed_count)
8546 ++windows_or_buffers_changed;
8547 }
8548 }
8549
8550
8551 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8552 is non-zero update selected_frame. Value is non-zero if the
8553 mini-windows height has been changed. */
8554
8555 static int
8556 echo_area_display (update_frame_p)
8557 int update_frame_p;
8558 {
8559 Lisp_Object mini_window;
8560 struct window *w;
8561 struct frame *f;
8562 int window_height_changed_p = 0;
8563 struct frame *sf = SELECTED_FRAME ();
8564
8565 mini_window = FRAME_MINIBUF_WINDOW (sf);
8566 w = XWINDOW (mini_window);
8567 f = XFRAME (WINDOW_FRAME (w));
8568
8569 /* Don't display if frame is invisible or not yet initialized. */
8570 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8571 return 0;
8572
8573 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8574 #ifndef MAC_OS8
8575 #ifdef HAVE_WINDOW_SYSTEM
8576 /* When Emacs starts, selected_frame may be a visible terminal
8577 frame, even if we run under a window system. If we let this
8578 through, a message would be displayed on the terminal. */
8579 if (EQ (selected_frame, Vterminal_frame)
8580 && !NILP (Vwindow_system))
8581 return 0;
8582 #endif /* HAVE_WINDOW_SYSTEM */
8583 #endif
8584
8585 /* Redraw garbaged frames. */
8586 if (frame_garbaged)
8587 clear_garbaged_frames ();
8588
8589 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8590 {
8591 echo_area_window = mini_window;
8592 window_height_changed_p = display_echo_area (w);
8593 w->must_be_updated_p = 1;
8594
8595 /* Update the display, unless called from redisplay_internal.
8596 Also don't update the screen during redisplay itself. The
8597 update will happen at the end of redisplay, and an update
8598 here could cause confusion. */
8599 if (update_frame_p && !redisplaying_p)
8600 {
8601 int n = 0;
8602
8603 /* If the display update has been interrupted by pending
8604 input, update mode lines in the frame. Due to the
8605 pending input, it might have been that redisplay hasn't
8606 been called, so that mode lines above the echo area are
8607 garbaged. This looks odd, so we prevent it here. */
8608 if (!display_completed)
8609 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8610
8611 if (window_height_changed_p
8612 /* Don't do this if Emacs is shutting down. Redisplay
8613 needs to run hooks. */
8614 && !NILP (Vrun_hooks))
8615 {
8616 /* Must update other windows. Likewise as in other
8617 cases, don't let this update be interrupted by
8618 pending input. */
8619 int count = SPECPDL_INDEX ();
8620 specbind (Qredisplay_dont_pause, Qt);
8621 windows_or_buffers_changed = 1;
8622 redisplay_internal (0);
8623 unbind_to (count, Qnil);
8624 }
8625 else if (FRAME_WINDOW_P (f) && n == 0)
8626 {
8627 /* Window configuration is the same as before.
8628 Can do with a display update of the echo area,
8629 unless we displayed some mode lines. */
8630 update_single_window (w, 1);
8631 rif->flush_display (f);
8632 }
8633 else
8634 update_frame (f, 1, 1);
8635
8636 /* If cursor is in the echo area, make sure that the next
8637 redisplay displays the minibuffer, so that the cursor will
8638 be replaced with what the minibuffer wants. */
8639 if (cursor_in_echo_area)
8640 ++windows_or_buffers_changed;
8641 }
8642 }
8643 else if (!EQ (mini_window, selected_window))
8644 windows_or_buffers_changed++;
8645
8646 /* The current message is now also the last one displayed. */
8647 echo_area_buffer[1] = echo_area_buffer[0];
8648
8649 /* Prevent redisplay optimization in redisplay_internal by resetting
8650 this_line_start_pos. This is done because the mini-buffer now
8651 displays the message instead of its buffer text. */
8652 if (EQ (mini_window, selected_window))
8653 CHARPOS (this_line_start_pos) = 0;
8654
8655 return window_height_changed_p;
8656 }
8657
8658
8659 \f
8660 /***********************************************************************
8661 Mode Lines and Frame Titles
8662 ***********************************************************************/
8663
8664 /* A buffer for constructing non-propertized mode-line strings and
8665 frame titles in it; allocated from the heap in init_xdisp and
8666 resized as needed in store_mode_line_noprop_char. */
8667
8668 static char *mode_line_noprop_buf;
8669
8670 /* The buffer's end, and a current output position in it. */
8671
8672 static char *mode_line_noprop_buf_end;
8673 static char *mode_line_noprop_ptr;
8674
8675 #define MODE_LINE_NOPROP_LEN(start) \
8676 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8677
8678 static enum {
8679 MODE_LINE_DISPLAY = 0,
8680 MODE_LINE_TITLE,
8681 MODE_LINE_NOPROP,
8682 MODE_LINE_STRING
8683 } mode_line_target;
8684
8685 /* Alist that caches the results of :propertize.
8686 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8687 static Lisp_Object mode_line_proptrans_alist;
8688
8689 /* List of strings making up the mode-line. */
8690 static Lisp_Object mode_line_string_list;
8691
8692 /* Base face property when building propertized mode line string. */
8693 static Lisp_Object mode_line_string_face;
8694 static Lisp_Object mode_line_string_face_prop;
8695
8696
8697 /* Unwind data for mode line strings */
8698
8699 static Lisp_Object Vmode_line_unwind_vector;
8700
8701 static Lisp_Object
8702 format_mode_line_unwind_data (obuf, save_proptrans)
8703 struct buffer *obuf;
8704 {
8705 Lisp_Object vector;
8706
8707 /* Reduce consing by keeping one vector in
8708 Vwith_echo_area_save_vector. */
8709 vector = Vmode_line_unwind_vector;
8710 Vmode_line_unwind_vector = Qnil;
8711
8712 if (NILP (vector))
8713 vector = Fmake_vector (make_number (7), Qnil);
8714
8715 AREF (vector, 0) = make_number (mode_line_target);
8716 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8717 AREF (vector, 2) = mode_line_string_list;
8718 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8719 AREF (vector, 4) = mode_line_string_face;
8720 AREF (vector, 5) = mode_line_string_face_prop;
8721
8722 if (obuf)
8723 XSETBUFFER (AREF (vector, 6), obuf);
8724 else
8725 AREF (vector, 6) = Qnil;
8726
8727 return vector;
8728 }
8729
8730 static Lisp_Object
8731 unwind_format_mode_line (vector)
8732 Lisp_Object vector;
8733 {
8734 mode_line_target = XINT (AREF (vector, 0));
8735 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8736 mode_line_string_list = AREF (vector, 2);
8737 if (! EQ (AREF (vector, 3), Qt))
8738 mode_line_proptrans_alist = AREF (vector, 3);
8739 mode_line_string_face = AREF (vector, 4);
8740 mode_line_string_face_prop = AREF (vector, 5);
8741
8742 if (!NILP (AREF (vector, 6)))
8743 {
8744 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8745 AREF (vector, 6) = Qnil;
8746 }
8747
8748 Vmode_line_unwind_vector = vector;
8749 return Qnil;
8750 }
8751
8752
8753 /* Store a single character C for the frame title in mode_line_noprop_buf.
8754 Re-allocate mode_line_noprop_buf if necessary. */
8755
8756 static void
8757 #ifdef PROTOTYPES
8758 store_mode_line_noprop_char (char c)
8759 #else
8760 store_mode_line_noprop_char (c)
8761 char c;
8762 #endif
8763 {
8764 /* If output position has reached the end of the allocated buffer,
8765 double the buffer's size. */
8766 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8767 {
8768 int len = MODE_LINE_NOPROP_LEN (0);
8769 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8770 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8771 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8772 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8773 }
8774
8775 *mode_line_noprop_ptr++ = c;
8776 }
8777
8778
8779 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8780 mode_line_noprop_ptr. STR is the string to store. Do not copy
8781 characters that yield more columns than PRECISION; PRECISION <= 0
8782 means copy the whole string. Pad with spaces until FIELD_WIDTH
8783 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8784 pad. Called from display_mode_element when it is used to build a
8785 frame title. */
8786
8787 static int
8788 store_mode_line_noprop (str, field_width, precision)
8789 const unsigned char *str;
8790 int field_width, precision;
8791 {
8792 int n = 0;
8793 int dummy, nbytes;
8794
8795 /* Copy at most PRECISION chars from STR. */
8796 nbytes = strlen (str);
8797 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8798 while (nbytes--)
8799 store_mode_line_noprop_char (*str++);
8800
8801 /* Fill up with spaces until FIELD_WIDTH reached. */
8802 while (field_width > 0
8803 && n < field_width)
8804 {
8805 store_mode_line_noprop_char (' ');
8806 ++n;
8807 }
8808
8809 return n;
8810 }
8811
8812 /***********************************************************************
8813 Frame Titles
8814 ***********************************************************************/
8815
8816 #ifdef HAVE_WINDOW_SYSTEM
8817
8818 /* Set the title of FRAME, if it has changed. The title format is
8819 Vicon_title_format if FRAME is iconified, otherwise it is
8820 frame_title_format. */
8821
8822 static void
8823 x_consider_frame_title (frame)
8824 Lisp_Object frame;
8825 {
8826 struct frame *f = XFRAME (frame);
8827
8828 if (FRAME_WINDOW_P (f)
8829 || FRAME_MINIBUF_ONLY_P (f)
8830 || f->explicit_name)
8831 {
8832 /* Do we have more than one visible frame on this X display? */
8833 Lisp_Object tail;
8834 Lisp_Object fmt;
8835 int title_start;
8836 char *title;
8837 int len;
8838 struct it it;
8839 int count = SPECPDL_INDEX ();
8840
8841 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8842 {
8843 Lisp_Object other_frame = XCAR (tail);
8844 struct frame *tf = XFRAME (other_frame);
8845
8846 if (tf != f
8847 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8848 && !FRAME_MINIBUF_ONLY_P (tf)
8849 && !EQ (other_frame, tip_frame)
8850 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8851 break;
8852 }
8853
8854 /* Set global variable indicating that multiple frames exist. */
8855 multiple_frames = CONSP (tail);
8856
8857 /* Switch to the buffer of selected window of the frame. Set up
8858 mode_line_target so that display_mode_element will output into
8859 mode_line_noprop_buf; then display the title. */
8860 record_unwind_protect (unwind_format_mode_line,
8861 format_mode_line_unwind_data (current_buffer, 0));
8862
8863 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8864 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8865
8866 mode_line_target = MODE_LINE_TITLE;
8867 title_start = MODE_LINE_NOPROP_LEN (0);
8868 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8869 NULL, DEFAULT_FACE_ID);
8870 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8871 len = MODE_LINE_NOPROP_LEN (title_start);
8872 title = mode_line_noprop_buf + title_start;
8873 unbind_to (count, Qnil);
8874
8875 /* Set the title only if it's changed. This avoids consing in
8876 the common case where it hasn't. (If it turns out that we've
8877 already wasted too much time by walking through the list with
8878 display_mode_element, then we might need to optimize at a
8879 higher level than this.) */
8880 if (! STRINGP (f->name)
8881 || SBYTES (f->name) != len
8882 || bcmp (title, SDATA (f->name), len) != 0)
8883 x_implicitly_set_name (f, make_string (title, len), Qnil);
8884 }
8885 }
8886
8887 #endif /* not HAVE_WINDOW_SYSTEM */
8888
8889
8890
8891 \f
8892 /***********************************************************************
8893 Menu Bars
8894 ***********************************************************************/
8895
8896
8897 /* Prepare for redisplay by updating menu-bar item lists when
8898 appropriate. This can call eval. */
8899
8900 void
8901 prepare_menu_bars ()
8902 {
8903 int all_windows;
8904 struct gcpro gcpro1, gcpro2;
8905 struct frame *f;
8906 Lisp_Object tooltip_frame;
8907
8908 #ifdef HAVE_WINDOW_SYSTEM
8909 tooltip_frame = tip_frame;
8910 #else
8911 tooltip_frame = Qnil;
8912 #endif
8913
8914 /* Update all frame titles based on their buffer names, etc. We do
8915 this before the menu bars so that the buffer-menu will show the
8916 up-to-date frame titles. */
8917 #ifdef HAVE_WINDOW_SYSTEM
8918 if (windows_or_buffers_changed || update_mode_lines)
8919 {
8920 Lisp_Object tail, frame;
8921
8922 FOR_EACH_FRAME (tail, frame)
8923 {
8924 f = XFRAME (frame);
8925 if (!EQ (frame, tooltip_frame)
8926 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8927 x_consider_frame_title (frame);
8928 }
8929 }
8930 #endif /* HAVE_WINDOW_SYSTEM */
8931
8932 /* Update the menu bar item lists, if appropriate. This has to be
8933 done before any actual redisplay or generation of display lines. */
8934 all_windows = (update_mode_lines
8935 || buffer_shared > 1
8936 || windows_or_buffers_changed);
8937 if (all_windows)
8938 {
8939 Lisp_Object tail, frame;
8940 int count = SPECPDL_INDEX ();
8941
8942 record_unwind_save_match_data ();
8943
8944 FOR_EACH_FRAME (tail, frame)
8945 {
8946 f = XFRAME (frame);
8947
8948 /* Ignore tooltip frame. */
8949 if (EQ (frame, tooltip_frame))
8950 continue;
8951
8952 /* If a window on this frame changed size, report that to
8953 the user and clear the size-change flag. */
8954 if (FRAME_WINDOW_SIZES_CHANGED (f))
8955 {
8956 Lisp_Object functions;
8957
8958 /* Clear flag first in case we get an error below. */
8959 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8960 functions = Vwindow_size_change_functions;
8961 GCPRO2 (tail, functions);
8962
8963 while (CONSP (functions))
8964 {
8965 call1 (XCAR (functions), frame);
8966 functions = XCDR (functions);
8967 }
8968 UNGCPRO;
8969 }
8970
8971 GCPRO1 (tail);
8972 update_menu_bar (f, 0);
8973 #ifdef HAVE_WINDOW_SYSTEM
8974 update_tool_bar (f, 0);
8975 #ifdef MAC_OS
8976 mac_update_title_bar (f, 0);
8977 #endif
8978 #endif
8979 UNGCPRO;
8980 }
8981
8982 unbind_to (count, Qnil);
8983 }
8984 else
8985 {
8986 struct frame *sf = SELECTED_FRAME ();
8987 update_menu_bar (sf, 1);
8988 #ifdef HAVE_WINDOW_SYSTEM
8989 update_tool_bar (sf, 1);
8990 #ifdef MAC_OS
8991 mac_update_title_bar (sf, 1);
8992 #endif
8993 #endif
8994 }
8995
8996 /* Motif needs this. See comment in xmenu.c. Turn it off when
8997 pending_menu_activation is not defined. */
8998 #ifdef USE_X_TOOLKIT
8999 pending_menu_activation = 0;
9000 #endif
9001 }
9002
9003
9004 /* Update the menu bar item list for frame F. This has to be done
9005 before we start to fill in any display lines, because it can call
9006 eval.
9007
9008 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
9009
9010 static void
9011 update_menu_bar (f, save_match_data)
9012 struct frame *f;
9013 int save_match_data;
9014 {
9015 Lisp_Object window;
9016 register struct window *w;
9017
9018 /* If called recursively during a menu update, do nothing. This can
9019 happen when, for instance, an activate-menubar-hook causes a
9020 redisplay. */
9021 if (inhibit_menubar_update)
9022 return;
9023
9024 window = FRAME_SELECTED_WINDOW (f);
9025 w = XWINDOW (window);
9026
9027 #if 0 /* The if statement below this if statement used to include the
9028 condition !NILP (w->update_mode_line), rather than using
9029 update_mode_lines directly, and this if statement may have
9030 been added to make that condition work. Now the if
9031 statement below matches its comment, this isn't needed. */
9032 if (update_mode_lines)
9033 w->update_mode_line = Qt;
9034 #endif
9035
9036 if (FRAME_WINDOW_P (f)
9037 ?
9038 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9039 || defined (USE_GTK)
9040 FRAME_EXTERNAL_MENU_BAR (f)
9041 #else
9042 FRAME_MENU_BAR_LINES (f) > 0
9043 #endif
9044 : FRAME_MENU_BAR_LINES (f) > 0)
9045 {
9046 /* If the user has switched buffers or windows, we need to
9047 recompute to reflect the new bindings. But we'll
9048 recompute when update_mode_lines is set too; that means
9049 that people can use force-mode-line-update to request
9050 that the menu bar be recomputed. The adverse effect on
9051 the rest of the redisplay algorithm is about the same as
9052 windows_or_buffers_changed anyway. */
9053 if (windows_or_buffers_changed
9054 /* This used to test w->update_mode_line, but we believe
9055 there is no need to recompute the menu in that case. */
9056 || update_mode_lines
9057 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9058 < BUF_MODIFF (XBUFFER (w->buffer)))
9059 != !NILP (w->last_had_star))
9060 || ((!NILP (Vtransient_mark_mode)
9061 && !NILP (XBUFFER (w->buffer)->mark_active))
9062 != !NILP (w->region_showing)))
9063 {
9064 struct buffer *prev = current_buffer;
9065 int count = SPECPDL_INDEX ();
9066
9067 specbind (Qinhibit_menubar_update, Qt);
9068
9069 set_buffer_internal_1 (XBUFFER (w->buffer));
9070 if (save_match_data)
9071 record_unwind_save_match_data ();
9072 if (NILP (Voverriding_local_map_menu_flag))
9073 {
9074 specbind (Qoverriding_terminal_local_map, Qnil);
9075 specbind (Qoverriding_local_map, Qnil);
9076 }
9077
9078 /* Run the Lucid hook. */
9079 safe_run_hooks (Qactivate_menubar_hook);
9080
9081 /* If it has changed current-menubar from previous value,
9082 really recompute the menu-bar from the value. */
9083 if (! NILP (Vlucid_menu_bar_dirty_flag))
9084 call0 (Qrecompute_lucid_menubar);
9085
9086 safe_run_hooks (Qmenu_bar_update_hook);
9087 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9088
9089 /* Redisplay the menu bar in case we changed it. */
9090 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9091 || defined (USE_GTK)
9092 if (FRAME_WINDOW_P (f))
9093 {
9094 #ifdef MAC_OS
9095 /* All frames on Mac OS share the same menubar. So only
9096 the selected frame should be allowed to set it. */
9097 if (f == SELECTED_FRAME ())
9098 #endif
9099 set_frame_menubar (f, 0, 0);
9100 }
9101 else
9102 /* On a terminal screen, the menu bar is an ordinary screen
9103 line, and this makes it get updated. */
9104 w->update_mode_line = Qt;
9105 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9106 /* In the non-toolkit version, the menu bar is an ordinary screen
9107 line, and this makes it get updated. */
9108 w->update_mode_line = Qt;
9109 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9110
9111 unbind_to (count, Qnil);
9112 set_buffer_internal_1 (prev);
9113 }
9114 }
9115 }
9116
9117
9118 \f
9119 /***********************************************************************
9120 Output Cursor
9121 ***********************************************************************/
9122
9123 #ifdef HAVE_WINDOW_SYSTEM
9124
9125 /* EXPORT:
9126 Nominal cursor position -- where to draw output.
9127 HPOS and VPOS are window relative glyph matrix coordinates.
9128 X and Y are window relative pixel coordinates. */
9129
9130 struct cursor_pos output_cursor;
9131
9132
9133 /* EXPORT:
9134 Set the global variable output_cursor to CURSOR. All cursor
9135 positions are relative to updated_window. */
9136
9137 void
9138 set_output_cursor (cursor)
9139 struct cursor_pos *cursor;
9140 {
9141 output_cursor.hpos = cursor->hpos;
9142 output_cursor.vpos = cursor->vpos;
9143 output_cursor.x = cursor->x;
9144 output_cursor.y = cursor->y;
9145 }
9146
9147
9148 /* EXPORT for RIF:
9149 Set a nominal cursor position.
9150
9151 HPOS and VPOS are column/row positions in a window glyph matrix. X
9152 and Y are window text area relative pixel positions.
9153
9154 If this is done during an update, updated_window will contain the
9155 window that is being updated and the position is the future output
9156 cursor position for that window. If updated_window is null, use
9157 selected_window and display the cursor at the given position. */
9158
9159 void
9160 x_cursor_to (vpos, hpos, y, x)
9161 int vpos, hpos, y, x;
9162 {
9163 struct window *w;
9164
9165 /* If updated_window is not set, work on selected_window. */
9166 if (updated_window)
9167 w = updated_window;
9168 else
9169 w = XWINDOW (selected_window);
9170
9171 /* Set the output cursor. */
9172 output_cursor.hpos = hpos;
9173 output_cursor.vpos = vpos;
9174 output_cursor.x = x;
9175 output_cursor.y = y;
9176
9177 /* If not called as part of an update, really display the cursor.
9178 This will also set the cursor position of W. */
9179 if (updated_window == NULL)
9180 {
9181 BLOCK_INPUT;
9182 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9183 if (rif->flush_display_optional)
9184 rif->flush_display_optional (SELECTED_FRAME ());
9185 UNBLOCK_INPUT;
9186 }
9187 }
9188
9189 #endif /* HAVE_WINDOW_SYSTEM */
9190
9191 \f
9192 /***********************************************************************
9193 Tool-bars
9194 ***********************************************************************/
9195
9196 #ifdef HAVE_WINDOW_SYSTEM
9197
9198 /* Where the mouse was last time we reported a mouse event. */
9199
9200 FRAME_PTR last_mouse_frame;
9201
9202 /* Tool-bar item index of the item on which a mouse button was pressed
9203 or -1. */
9204
9205 int last_tool_bar_item;
9206
9207
9208 /* Update the tool-bar item list for frame F. This has to be done
9209 before we start to fill in any display lines. Called from
9210 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9211 and restore it here. */
9212
9213 static void
9214 update_tool_bar (f, save_match_data)
9215 struct frame *f;
9216 int save_match_data;
9217 {
9218 #ifdef USE_GTK
9219 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9220 #else
9221 int do_update = WINDOWP (f->tool_bar_window)
9222 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9223 #endif
9224
9225 if (do_update)
9226 {
9227 Lisp_Object window;
9228 struct window *w;
9229
9230 window = FRAME_SELECTED_WINDOW (f);
9231 w = XWINDOW (window);
9232
9233 /* If the user has switched buffers or windows, we need to
9234 recompute to reflect the new bindings. But we'll
9235 recompute when update_mode_lines is set too; that means
9236 that people can use force-mode-line-update to request
9237 that the menu bar be recomputed. The adverse effect on
9238 the rest of the redisplay algorithm is about the same as
9239 windows_or_buffers_changed anyway. */
9240 if (windows_or_buffers_changed
9241 || !NILP (w->update_mode_line)
9242 || update_mode_lines
9243 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9244 < BUF_MODIFF (XBUFFER (w->buffer)))
9245 != !NILP (w->last_had_star))
9246 || ((!NILP (Vtransient_mark_mode)
9247 && !NILP (XBUFFER (w->buffer)->mark_active))
9248 != !NILP (w->region_showing)))
9249 {
9250 struct buffer *prev = current_buffer;
9251 int count = SPECPDL_INDEX ();
9252 Lisp_Object new_tool_bar;
9253 int new_n_tool_bar;
9254 struct gcpro gcpro1;
9255
9256 /* Set current_buffer to the buffer of the selected
9257 window of the frame, so that we get the right local
9258 keymaps. */
9259 set_buffer_internal_1 (XBUFFER (w->buffer));
9260
9261 /* Save match data, if we must. */
9262 if (save_match_data)
9263 record_unwind_save_match_data ();
9264
9265 /* Make sure that we don't accidentally use bogus keymaps. */
9266 if (NILP (Voverriding_local_map_menu_flag))
9267 {
9268 specbind (Qoverriding_terminal_local_map, Qnil);
9269 specbind (Qoverriding_local_map, Qnil);
9270 }
9271
9272 GCPRO1 (new_tool_bar);
9273
9274 /* Build desired tool-bar items from keymaps. */
9275 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9276 &new_n_tool_bar);
9277
9278 /* Redisplay the tool-bar if we changed it. */
9279 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9280 {
9281 /* Redisplay that happens asynchronously due to an expose event
9282 may access f->tool_bar_items. Make sure we update both
9283 variables within BLOCK_INPUT so no such event interrupts. */
9284 BLOCK_INPUT;
9285 f->tool_bar_items = new_tool_bar;
9286 f->n_tool_bar_items = new_n_tool_bar;
9287 w->update_mode_line = Qt;
9288 UNBLOCK_INPUT;
9289 }
9290
9291 UNGCPRO;
9292
9293 unbind_to (count, Qnil);
9294 set_buffer_internal_1 (prev);
9295 }
9296 }
9297 }
9298
9299
9300 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9301 F's desired tool-bar contents. F->tool_bar_items must have
9302 been set up previously by calling prepare_menu_bars. */
9303
9304 static void
9305 build_desired_tool_bar_string (f)
9306 struct frame *f;
9307 {
9308 int i, size, size_needed;
9309 struct gcpro gcpro1, gcpro2, gcpro3;
9310 Lisp_Object image, plist, props;
9311
9312 image = plist = props = Qnil;
9313 GCPRO3 (image, plist, props);
9314
9315 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9316 Otherwise, make a new string. */
9317
9318 /* The size of the string we might be able to reuse. */
9319 size = (STRINGP (f->desired_tool_bar_string)
9320 ? SCHARS (f->desired_tool_bar_string)
9321 : 0);
9322
9323 /* We need one space in the string for each image. */
9324 size_needed = f->n_tool_bar_items;
9325
9326 /* Reuse f->desired_tool_bar_string, if possible. */
9327 if (size < size_needed || NILP (f->desired_tool_bar_string))
9328 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9329 make_number (' '));
9330 else
9331 {
9332 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9333 Fremove_text_properties (make_number (0), make_number (size),
9334 props, f->desired_tool_bar_string);
9335 }
9336
9337 /* Put a `display' property on the string for the images to display,
9338 put a `menu_item' property on tool-bar items with a value that
9339 is the index of the item in F's tool-bar item vector. */
9340 for (i = 0; i < f->n_tool_bar_items; ++i)
9341 {
9342 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9343
9344 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9345 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9346 int hmargin, vmargin, relief, idx, end;
9347 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9348
9349 /* If image is a vector, choose the image according to the
9350 button state. */
9351 image = PROP (TOOL_BAR_ITEM_IMAGES);
9352 if (VECTORP (image))
9353 {
9354 if (enabled_p)
9355 idx = (selected_p
9356 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9357 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9358 else
9359 idx = (selected_p
9360 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9361 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9362
9363 xassert (ASIZE (image) >= idx);
9364 image = AREF (image, idx);
9365 }
9366 else
9367 idx = -1;
9368
9369 /* Ignore invalid image specifications. */
9370 if (!valid_image_p (image))
9371 continue;
9372
9373 /* Display the tool-bar button pressed, or depressed. */
9374 plist = Fcopy_sequence (XCDR (image));
9375
9376 /* Compute margin and relief to draw. */
9377 relief = (tool_bar_button_relief >= 0
9378 ? tool_bar_button_relief
9379 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9380 hmargin = vmargin = relief;
9381
9382 if (INTEGERP (Vtool_bar_button_margin)
9383 && XINT (Vtool_bar_button_margin) > 0)
9384 {
9385 hmargin += XFASTINT (Vtool_bar_button_margin);
9386 vmargin += XFASTINT (Vtool_bar_button_margin);
9387 }
9388 else if (CONSP (Vtool_bar_button_margin))
9389 {
9390 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9391 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9392 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9393
9394 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9395 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9396 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9397 }
9398
9399 if (auto_raise_tool_bar_buttons_p)
9400 {
9401 /* Add a `:relief' property to the image spec if the item is
9402 selected. */
9403 if (selected_p)
9404 {
9405 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9406 hmargin -= relief;
9407 vmargin -= relief;
9408 }
9409 }
9410 else
9411 {
9412 /* If image is selected, display it pressed, i.e. with a
9413 negative relief. If it's not selected, display it with a
9414 raised relief. */
9415 plist = Fplist_put (plist, QCrelief,
9416 (selected_p
9417 ? make_number (-relief)
9418 : make_number (relief)));
9419 hmargin -= relief;
9420 vmargin -= relief;
9421 }
9422
9423 /* Put a margin around the image. */
9424 if (hmargin || vmargin)
9425 {
9426 if (hmargin == vmargin)
9427 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9428 else
9429 plist = Fplist_put (plist, QCmargin,
9430 Fcons (make_number (hmargin),
9431 make_number (vmargin)));
9432 }
9433
9434 /* If button is not enabled, and we don't have special images
9435 for the disabled state, make the image appear disabled by
9436 applying an appropriate algorithm to it. */
9437 if (!enabled_p && idx < 0)
9438 plist = Fplist_put (plist, QCconversion, Qdisabled);
9439
9440 /* Put a `display' text property on the string for the image to
9441 display. Put a `menu-item' property on the string that gives
9442 the start of this item's properties in the tool-bar items
9443 vector. */
9444 image = Fcons (Qimage, plist);
9445 props = list4 (Qdisplay, image,
9446 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9447
9448 /* Let the last image hide all remaining spaces in the tool bar
9449 string. The string can be longer than needed when we reuse a
9450 previous string. */
9451 if (i + 1 == f->n_tool_bar_items)
9452 end = SCHARS (f->desired_tool_bar_string);
9453 else
9454 end = i + 1;
9455 Fadd_text_properties (make_number (i), make_number (end),
9456 props, f->desired_tool_bar_string);
9457 #undef PROP
9458 }
9459
9460 UNGCPRO;
9461 }
9462
9463
9464 /* Display one line of the tool-bar of frame IT->f.
9465
9466 HEIGHT specifies the desired height of the tool-bar line.
9467 If the actual height of the glyph row is less than HEIGHT, the
9468 row's height is increased to HEIGHT, and the icons are centered
9469 vertically in the new height.
9470
9471 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9472 count a final empty row in case the tool-bar width exactly matches
9473 the window width.
9474 */
9475
9476 static void
9477 display_tool_bar_line (it, height)
9478 struct it *it;
9479 int height;
9480 {
9481 struct glyph_row *row = it->glyph_row;
9482 int max_x = it->last_visible_x;
9483 struct glyph *last;
9484
9485 prepare_desired_row (row);
9486 row->y = it->current_y;
9487
9488 /* Note that this isn't made use of if the face hasn't a box,
9489 so there's no need to check the face here. */
9490 it->start_of_box_run_p = 1;
9491
9492 while (it->current_x < max_x)
9493 {
9494 int x_before, x, n_glyphs_before, i, nglyphs;
9495 struct it it_before;
9496
9497 /* Get the next display element. */
9498 if (!get_next_display_element (it))
9499 {
9500 /* Don't count empty row if we are counting needed tool-bar lines. */
9501 if (height < 0 && !it->hpos)
9502 return;
9503 break;
9504 }
9505
9506 /* Produce glyphs. */
9507 x_before = it->current_x;
9508 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9509 it_before = *it;
9510 PRODUCE_GLYPHS (it);
9511
9512 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9513 i = 0;
9514 x = x_before;
9515 while (i < nglyphs)
9516 {
9517 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9518
9519 if (x + glyph->pixel_width > max_x)
9520 {
9521 /* Glyph doesn't fit on line. Backtrack. */
9522 row->used[TEXT_AREA] = n_glyphs_before;
9523 *it = it_before;
9524 goto out;
9525 }
9526
9527 ++it->hpos;
9528 x += glyph->pixel_width;
9529 ++i;
9530 }
9531
9532 /* Stop at line ends. */
9533 if (ITERATOR_AT_END_OF_LINE_P (it))
9534 break;
9535
9536 set_iterator_to_next (it, 1);
9537 }
9538
9539 out:;
9540
9541 row->displays_text_p = row->used[TEXT_AREA] != 0;
9542 /* Use default face for the border below the tool bar. */
9543 if (!row->displays_text_p)
9544 it->face_id = DEFAULT_FACE_ID;
9545 extend_face_to_end_of_line (it);
9546 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9547 last->right_box_line_p = 1;
9548 if (last == row->glyphs[TEXT_AREA])
9549 last->left_box_line_p = 1;
9550
9551 /* Make line the desired height and center it vertically. */
9552 if ((height -= it->max_ascent + it->max_descent) > 0)
9553 {
9554 /* Don't add more than one line height. */
9555 height %= FRAME_LINE_HEIGHT (it->f);
9556 it->max_ascent += height / 2;
9557 it->max_descent += (height + 1) / 2;
9558 }
9559
9560 compute_line_metrics (it);
9561
9562 /* If line is empty, make it occupy the rest of the tool-bar. */
9563 if (!row->displays_text_p)
9564 {
9565 row->height = row->phys_height = it->last_visible_y - row->y;
9566 row->ascent = row->phys_ascent = 0;
9567 row->extra_line_spacing = 0;
9568 }
9569
9570 row->full_width_p = 1;
9571 row->continued_p = 0;
9572 row->truncated_on_left_p = 0;
9573 row->truncated_on_right_p = 0;
9574
9575 it->current_x = it->hpos = 0;
9576 it->current_y += row->height;
9577 ++it->vpos;
9578 ++it->glyph_row;
9579 }
9580
9581
9582 /* Value is the number of screen lines needed to make all tool-bar
9583 items of frame F visible. The number of actual rows needed is
9584 returned in *N_ROWS if non-NULL. */
9585
9586 static int
9587 tool_bar_lines_needed (f, n_rows)
9588 struct frame *f;
9589 int *n_rows;
9590 {
9591 struct window *w = XWINDOW (f->tool_bar_window);
9592 struct it it;
9593 struct glyph_row *temp_row = w->desired_matrix->rows;
9594
9595 /* Initialize an iterator for iteration over
9596 F->desired_tool_bar_string in the tool-bar window of frame F. */
9597 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9598 it.first_visible_x = 0;
9599 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9600 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9601
9602 while (!ITERATOR_AT_END_P (&it))
9603 {
9604 clear_glyph_row (temp_row);
9605 it.glyph_row = temp_row;
9606 display_tool_bar_line (&it, -1);
9607 }
9608 clear_glyph_row (temp_row);
9609
9610 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9611 if (n_rows)
9612 *n_rows = it.vpos > 0 ? it.vpos : -1;
9613
9614 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9615 }
9616
9617
9618 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9619 0, 1, 0,
9620 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9621 (frame)
9622 Lisp_Object frame;
9623 {
9624 struct frame *f;
9625 struct window *w;
9626 int nlines = 0;
9627
9628 if (NILP (frame))
9629 frame = selected_frame;
9630 else
9631 CHECK_FRAME (frame);
9632 f = XFRAME (frame);
9633
9634 if (WINDOWP (f->tool_bar_window)
9635 || (w = XWINDOW (f->tool_bar_window),
9636 WINDOW_TOTAL_LINES (w) > 0))
9637 {
9638 update_tool_bar (f, 1);
9639 if (f->n_tool_bar_items)
9640 {
9641 build_desired_tool_bar_string (f);
9642 nlines = tool_bar_lines_needed (f, NULL);
9643 }
9644 }
9645
9646 return make_number (nlines);
9647 }
9648
9649
9650 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9651 height should be changed. */
9652
9653 static int
9654 redisplay_tool_bar (f)
9655 struct frame *f;
9656 {
9657 struct window *w;
9658 struct it it;
9659 struct glyph_row *row;
9660 int change_height_p = 0;
9661
9662 #ifdef USE_GTK
9663 if (FRAME_EXTERNAL_TOOL_BAR (f))
9664 update_frame_tool_bar (f);
9665 return 0;
9666 #endif
9667
9668 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9669 do anything. This means you must start with tool-bar-lines
9670 non-zero to get the auto-sizing effect. Or in other words, you
9671 can turn off tool-bars by specifying tool-bar-lines zero. */
9672 if (!WINDOWP (f->tool_bar_window)
9673 || (w = XWINDOW (f->tool_bar_window),
9674 WINDOW_TOTAL_LINES (w) == 0))
9675 return 0;
9676
9677 /* Set up an iterator for the tool-bar window. */
9678 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9679 it.first_visible_x = 0;
9680 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9681 row = it.glyph_row;
9682
9683 /* Build a string that represents the contents of the tool-bar. */
9684 build_desired_tool_bar_string (f);
9685 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9686
9687 if (f->n_tool_bar_rows == 0)
9688 {
9689 int nlines;
9690
9691 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9692 nlines != WINDOW_TOTAL_LINES (w)))
9693 {
9694 extern Lisp_Object Qtool_bar_lines;
9695 Lisp_Object frame;
9696 int old_height = WINDOW_TOTAL_LINES (w);
9697
9698 XSETFRAME (frame, f);
9699 clear_glyph_matrix (w->desired_matrix);
9700 Fmodify_frame_parameters (frame,
9701 Fcons (Fcons (Qtool_bar_lines,
9702 make_number (nlines)),
9703 Qnil));
9704 if (WINDOW_TOTAL_LINES (w) != old_height)
9705 {
9706 fonts_changed_p = 1;
9707 return 1;
9708 }
9709 }
9710 }
9711
9712 /* Display as many lines as needed to display all tool-bar items. */
9713
9714 if (f->n_tool_bar_rows > 0)
9715 {
9716 int border, rows, height, extra;
9717
9718 if (INTEGERP (Vtool_bar_border))
9719 border = XINT (Vtool_bar_border);
9720 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9721 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9722 else if (EQ (Vtool_bar_border, Qborder_width))
9723 border = f->border_width;
9724 else
9725 border = 0;
9726 if (border < 0)
9727 border = 0;
9728
9729 rows = f->n_tool_bar_rows;
9730 height = max (1, (it.last_visible_y - border) / rows);
9731 extra = it.last_visible_y - border - height * rows;
9732
9733 while (it.current_y < it.last_visible_y)
9734 {
9735 int h = 0;
9736 if (extra > 0 && rows-- > 0)
9737 {
9738 h = (extra + rows - 1) / rows;
9739 extra -= h;
9740 }
9741 display_tool_bar_line (&it, height + h);
9742 }
9743 }
9744 else
9745 {
9746 while (it.current_y < it.last_visible_y)
9747 display_tool_bar_line (&it, 0);
9748 }
9749
9750 /* It doesn't make much sense to try scrolling in the tool-bar
9751 window, so don't do it. */
9752 w->desired_matrix->no_scrolling_p = 1;
9753 w->must_be_updated_p = 1;
9754
9755 if (auto_resize_tool_bars_p)
9756 {
9757 int nlines;
9758
9759 /* If we couldn't display everything, change the tool-bar's
9760 height. */
9761 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9762 change_height_p = 1;
9763
9764 /* If there are blank lines at the end, except for a partially
9765 visible blank line at the end that is smaller than
9766 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9767 row = it.glyph_row - 1;
9768 if (!row->displays_text_p
9769 && row->height >= FRAME_LINE_HEIGHT (f))
9770 change_height_p = 1;
9771
9772 /* If row displays tool-bar items, but is partially visible,
9773 change the tool-bar's height. */
9774 if (row->displays_text_p
9775 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9776 change_height_p = 1;
9777
9778 /* Resize windows as needed by changing the `tool-bar-lines'
9779 frame parameter. */
9780 if (change_height_p
9781 && (nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9782 nlines != WINDOW_TOTAL_LINES (w)))
9783 {
9784 extern Lisp_Object Qtool_bar_lines;
9785 Lisp_Object frame;
9786 int old_height = WINDOW_TOTAL_LINES (w);
9787
9788 XSETFRAME (frame, f);
9789 clear_glyph_matrix (w->desired_matrix);
9790 Fmodify_frame_parameters (frame,
9791 Fcons (Fcons (Qtool_bar_lines,
9792 make_number (nlines)),
9793 Qnil));
9794 if (WINDOW_TOTAL_LINES (w) != old_height)
9795 fonts_changed_p = 1;
9796 }
9797 }
9798
9799 return change_height_p;
9800 }
9801
9802
9803 /* Get information about the tool-bar item which is displayed in GLYPH
9804 on frame F. Return in *PROP_IDX the index where tool-bar item
9805 properties start in F->tool_bar_items. Value is zero if
9806 GLYPH doesn't display a tool-bar item. */
9807
9808 static int
9809 tool_bar_item_info (f, glyph, prop_idx)
9810 struct frame *f;
9811 struct glyph *glyph;
9812 int *prop_idx;
9813 {
9814 Lisp_Object prop;
9815 int success_p;
9816 int charpos;
9817
9818 /* This function can be called asynchronously, which means we must
9819 exclude any possibility that Fget_text_property signals an
9820 error. */
9821 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9822 charpos = max (0, charpos);
9823
9824 /* Get the text property `menu-item' at pos. The value of that
9825 property is the start index of this item's properties in
9826 F->tool_bar_items. */
9827 prop = Fget_text_property (make_number (charpos),
9828 Qmenu_item, f->current_tool_bar_string);
9829 if (INTEGERP (prop))
9830 {
9831 *prop_idx = XINT (prop);
9832 success_p = 1;
9833 }
9834 else
9835 success_p = 0;
9836
9837 return success_p;
9838 }
9839
9840 \f
9841 /* Get information about the tool-bar item at position X/Y on frame F.
9842 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9843 the current matrix of the tool-bar window of F, or NULL if not
9844 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9845 item in F->tool_bar_items. Value is
9846
9847 -1 if X/Y is not on a tool-bar item
9848 0 if X/Y is on the same item that was highlighted before.
9849 1 otherwise. */
9850
9851 static int
9852 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9853 struct frame *f;
9854 int x, y;
9855 struct glyph **glyph;
9856 int *hpos, *vpos, *prop_idx;
9857 {
9858 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9859 struct window *w = XWINDOW (f->tool_bar_window);
9860 int area;
9861
9862 /* Find the glyph under X/Y. */
9863 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9864 if (*glyph == NULL)
9865 return -1;
9866
9867 /* Get the start of this tool-bar item's properties in
9868 f->tool_bar_items. */
9869 if (!tool_bar_item_info (f, *glyph, prop_idx))
9870 return -1;
9871
9872 /* Is mouse on the highlighted item? */
9873 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9874 && *vpos >= dpyinfo->mouse_face_beg_row
9875 && *vpos <= dpyinfo->mouse_face_end_row
9876 && (*vpos > dpyinfo->mouse_face_beg_row
9877 || *hpos >= dpyinfo->mouse_face_beg_col)
9878 && (*vpos < dpyinfo->mouse_face_end_row
9879 || *hpos < dpyinfo->mouse_face_end_col
9880 || dpyinfo->mouse_face_past_end))
9881 return 0;
9882
9883 return 1;
9884 }
9885
9886
9887 /* EXPORT:
9888 Handle mouse button event on the tool-bar of frame F, at
9889 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9890 0 for button release. MODIFIERS is event modifiers for button
9891 release. */
9892
9893 void
9894 handle_tool_bar_click (f, x, y, down_p, modifiers)
9895 struct frame *f;
9896 int x, y, down_p;
9897 unsigned int modifiers;
9898 {
9899 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9900 struct window *w = XWINDOW (f->tool_bar_window);
9901 int hpos, vpos, prop_idx;
9902 struct glyph *glyph;
9903 Lisp_Object enabled_p;
9904
9905 /* If not on the highlighted tool-bar item, return. */
9906 frame_to_window_pixel_xy (w, &x, &y);
9907 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9908 return;
9909
9910 /* If item is disabled, do nothing. */
9911 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9912 if (NILP (enabled_p))
9913 return;
9914
9915 if (down_p)
9916 {
9917 /* Show item in pressed state. */
9918 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9919 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9920 last_tool_bar_item = prop_idx;
9921 }
9922 else
9923 {
9924 Lisp_Object key, frame;
9925 struct input_event event;
9926 EVENT_INIT (event);
9927
9928 /* Show item in released state. */
9929 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9930 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9931
9932 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9933
9934 XSETFRAME (frame, f);
9935 event.kind = TOOL_BAR_EVENT;
9936 event.frame_or_window = frame;
9937 event.arg = frame;
9938 kbd_buffer_store_event (&event);
9939
9940 event.kind = TOOL_BAR_EVENT;
9941 event.frame_or_window = frame;
9942 event.arg = key;
9943 event.modifiers = modifiers;
9944 kbd_buffer_store_event (&event);
9945 last_tool_bar_item = -1;
9946 }
9947 }
9948
9949
9950 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9951 tool-bar window-relative coordinates X/Y. Called from
9952 note_mouse_highlight. */
9953
9954 static void
9955 note_tool_bar_highlight (f, x, y)
9956 struct frame *f;
9957 int x, y;
9958 {
9959 Lisp_Object window = f->tool_bar_window;
9960 struct window *w = XWINDOW (window);
9961 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9962 int hpos, vpos;
9963 struct glyph *glyph;
9964 struct glyph_row *row;
9965 int i;
9966 Lisp_Object enabled_p;
9967 int prop_idx;
9968 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9969 int mouse_down_p, rc;
9970
9971 /* Function note_mouse_highlight is called with negative x(y
9972 values when mouse moves outside of the frame. */
9973 if (x <= 0 || y <= 0)
9974 {
9975 clear_mouse_face (dpyinfo);
9976 return;
9977 }
9978
9979 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9980 if (rc < 0)
9981 {
9982 /* Not on tool-bar item. */
9983 clear_mouse_face (dpyinfo);
9984 return;
9985 }
9986 else if (rc == 0)
9987 /* On same tool-bar item as before. */
9988 goto set_help_echo;
9989
9990 clear_mouse_face (dpyinfo);
9991
9992 /* Mouse is down, but on different tool-bar item? */
9993 mouse_down_p = (dpyinfo->grabbed
9994 && f == last_mouse_frame
9995 && FRAME_LIVE_P (f));
9996 if (mouse_down_p
9997 && last_tool_bar_item != prop_idx)
9998 return;
9999
10000 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10001 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10002
10003 /* If tool-bar item is not enabled, don't highlight it. */
10004 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10005 if (!NILP (enabled_p))
10006 {
10007 /* Compute the x-position of the glyph. In front and past the
10008 image is a space. We include this in the highlighted area. */
10009 row = MATRIX_ROW (w->current_matrix, vpos);
10010 for (i = x = 0; i < hpos; ++i)
10011 x += row->glyphs[TEXT_AREA][i].pixel_width;
10012
10013 /* Record this as the current active region. */
10014 dpyinfo->mouse_face_beg_col = hpos;
10015 dpyinfo->mouse_face_beg_row = vpos;
10016 dpyinfo->mouse_face_beg_x = x;
10017 dpyinfo->mouse_face_beg_y = row->y;
10018 dpyinfo->mouse_face_past_end = 0;
10019
10020 dpyinfo->mouse_face_end_col = hpos + 1;
10021 dpyinfo->mouse_face_end_row = vpos;
10022 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10023 dpyinfo->mouse_face_end_y = row->y;
10024 dpyinfo->mouse_face_window = window;
10025 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10026
10027 /* Display it as active. */
10028 show_mouse_face (dpyinfo, draw);
10029 dpyinfo->mouse_face_image_state = draw;
10030 }
10031
10032 set_help_echo:
10033
10034 /* Set help_echo_string to a help string to display for this tool-bar item.
10035 XTread_socket does the rest. */
10036 help_echo_object = help_echo_window = Qnil;
10037 help_echo_pos = -1;
10038 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10039 if (NILP (help_echo_string))
10040 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10041 }
10042
10043 #endif /* HAVE_WINDOW_SYSTEM */
10044
10045
10046 \f
10047 /************************************************************************
10048 Horizontal scrolling
10049 ************************************************************************/
10050
10051 static int hscroll_window_tree P_ ((Lisp_Object));
10052 static int hscroll_windows P_ ((Lisp_Object));
10053
10054 /* For all leaf windows in the window tree rooted at WINDOW, set their
10055 hscroll value so that PT is (i) visible in the window, and (ii) so
10056 that it is not within a certain margin at the window's left and
10057 right border. Value is non-zero if any window's hscroll has been
10058 changed. */
10059
10060 static int
10061 hscroll_window_tree (window)
10062 Lisp_Object window;
10063 {
10064 int hscrolled_p = 0;
10065 int hscroll_relative_p = FLOATP (Vhscroll_step);
10066 int hscroll_step_abs = 0;
10067 double hscroll_step_rel = 0;
10068
10069 if (hscroll_relative_p)
10070 {
10071 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10072 if (hscroll_step_rel < 0)
10073 {
10074 hscroll_relative_p = 0;
10075 hscroll_step_abs = 0;
10076 }
10077 }
10078 else if (INTEGERP (Vhscroll_step))
10079 {
10080 hscroll_step_abs = XINT (Vhscroll_step);
10081 if (hscroll_step_abs < 0)
10082 hscroll_step_abs = 0;
10083 }
10084 else
10085 hscroll_step_abs = 0;
10086
10087 while (WINDOWP (window))
10088 {
10089 struct window *w = XWINDOW (window);
10090
10091 if (WINDOWP (w->hchild))
10092 hscrolled_p |= hscroll_window_tree (w->hchild);
10093 else if (WINDOWP (w->vchild))
10094 hscrolled_p |= hscroll_window_tree (w->vchild);
10095 else if (w->cursor.vpos >= 0)
10096 {
10097 int h_margin;
10098 int text_area_width;
10099 struct glyph_row *current_cursor_row
10100 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10101 struct glyph_row *desired_cursor_row
10102 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10103 struct glyph_row *cursor_row
10104 = (desired_cursor_row->enabled_p
10105 ? desired_cursor_row
10106 : current_cursor_row);
10107
10108 text_area_width = window_box_width (w, TEXT_AREA);
10109
10110 /* Scroll when cursor is inside this scroll margin. */
10111 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10112
10113 if ((XFASTINT (w->hscroll)
10114 && w->cursor.x <= h_margin)
10115 || (cursor_row->enabled_p
10116 && cursor_row->truncated_on_right_p
10117 && (w->cursor.x >= text_area_width - h_margin)))
10118 {
10119 struct it it;
10120 int hscroll;
10121 struct buffer *saved_current_buffer;
10122 int pt;
10123 int wanted_x;
10124
10125 /* Find point in a display of infinite width. */
10126 saved_current_buffer = current_buffer;
10127 current_buffer = XBUFFER (w->buffer);
10128
10129 if (w == XWINDOW (selected_window))
10130 pt = BUF_PT (current_buffer);
10131 else
10132 {
10133 pt = marker_position (w->pointm);
10134 pt = max (BEGV, pt);
10135 pt = min (ZV, pt);
10136 }
10137
10138 /* Move iterator to pt starting at cursor_row->start in
10139 a line with infinite width. */
10140 init_to_row_start (&it, w, cursor_row);
10141 it.last_visible_x = INFINITY;
10142 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10143 current_buffer = saved_current_buffer;
10144
10145 /* Position cursor in window. */
10146 if (!hscroll_relative_p && hscroll_step_abs == 0)
10147 hscroll = max (0, (it.current_x
10148 - (ITERATOR_AT_END_OF_LINE_P (&it)
10149 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10150 : (text_area_width / 2))))
10151 / FRAME_COLUMN_WIDTH (it.f);
10152 else if (w->cursor.x >= text_area_width - h_margin)
10153 {
10154 if (hscroll_relative_p)
10155 wanted_x = text_area_width * (1 - hscroll_step_rel)
10156 - h_margin;
10157 else
10158 wanted_x = text_area_width
10159 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10160 - h_margin;
10161 hscroll
10162 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10163 }
10164 else
10165 {
10166 if (hscroll_relative_p)
10167 wanted_x = text_area_width * hscroll_step_rel
10168 + h_margin;
10169 else
10170 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10171 + h_margin;
10172 hscroll
10173 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10174 }
10175 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10176
10177 /* Don't call Fset_window_hscroll if value hasn't
10178 changed because it will prevent redisplay
10179 optimizations. */
10180 if (XFASTINT (w->hscroll) != hscroll)
10181 {
10182 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10183 w->hscroll = make_number (hscroll);
10184 hscrolled_p = 1;
10185 }
10186 }
10187 }
10188
10189 window = w->next;
10190 }
10191
10192 /* Value is non-zero if hscroll of any leaf window has been changed. */
10193 return hscrolled_p;
10194 }
10195
10196
10197 /* Set hscroll so that cursor is visible and not inside horizontal
10198 scroll margins for all windows in the tree rooted at WINDOW. See
10199 also hscroll_window_tree above. Value is non-zero if any window's
10200 hscroll has been changed. If it has, desired matrices on the frame
10201 of WINDOW are cleared. */
10202
10203 static int
10204 hscroll_windows (window)
10205 Lisp_Object window;
10206 {
10207 int hscrolled_p;
10208
10209 if (automatic_hscrolling_p)
10210 {
10211 hscrolled_p = hscroll_window_tree (window);
10212 if (hscrolled_p)
10213 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10214 }
10215 else
10216 hscrolled_p = 0;
10217 return hscrolled_p;
10218 }
10219
10220
10221 \f
10222 /************************************************************************
10223 Redisplay
10224 ************************************************************************/
10225
10226 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10227 to a non-zero value. This is sometimes handy to have in a debugger
10228 session. */
10229
10230 #if GLYPH_DEBUG
10231
10232 /* First and last unchanged row for try_window_id. */
10233
10234 int debug_first_unchanged_at_end_vpos;
10235 int debug_last_unchanged_at_beg_vpos;
10236
10237 /* Delta vpos and y. */
10238
10239 int debug_dvpos, debug_dy;
10240
10241 /* Delta in characters and bytes for try_window_id. */
10242
10243 int debug_delta, debug_delta_bytes;
10244
10245 /* Values of window_end_pos and window_end_vpos at the end of
10246 try_window_id. */
10247
10248 EMACS_INT debug_end_pos, debug_end_vpos;
10249
10250 /* Append a string to W->desired_matrix->method. FMT is a printf
10251 format string. A1...A9 are a supplement for a variable-length
10252 argument list. If trace_redisplay_p is non-zero also printf the
10253 resulting string to stderr. */
10254
10255 static void
10256 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10257 struct window *w;
10258 char *fmt;
10259 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10260 {
10261 char buffer[512];
10262 char *method = w->desired_matrix->method;
10263 int len = strlen (method);
10264 int size = sizeof w->desired_matrix->method;
10265 int remaining = size - len - 1;
10266
10267 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10268 if (len && remaining)
10269 {
10270 method[len] = '|';
10271 --remaining, ++len;
10272 }
10273
10274 strncpy (method + len, buffer, remaining);
10275
10276 if (trace_redisplay_p)
10277 fprintf (stderr, "%p (%s): %s\n",
10278 w,
10279 ((BUFFERP (w->buffer)
10280 && STRINGP (XBUFFER (w->buffer)->name))
10281 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10282 : "no buffer"),
10283 buffer);
10284 }
10285
10286 #endif /* GLYPH_DEBUG */
10287
10288
10289 /* Value is non-zero if all changes in window W, which displays
10290 current_buffer, are in the text between START and END. START is a
10291 buffer position, END is given as a distance from Z. Used in
10292 redisplay_internal for display optimization. */
10293
10294 static INLINE int
10295 text_outside_line_unchanged_p (w, start, end)
10296 struct window *w;
10297 int start, end;
10298 {
10299 int unchanged_p = 1;
10300
10301 /* If text or overlays have changed, see where. */
10302 if (XFASTINT (w->last_modified) < MODIFF
10303 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10304 {
10305 /* Gap in the line? */
10306 if (GPT < start || Z - GPT < end)
10307 unchanged_p = 0;
10308
10309 /* Changes start in front of the line, or end after it? */
10310 if (unchanged_p
10311 && (BEG_UNCHANGED < start - 1
10312 || END_UNCHANGED < end))
10313 unchanged_p = 0;
10314
10315 /* If selective display, can't optimize if changes start at the
10316 beginning of the line. */
10317 if (unchanged_p
10318 && INTEGERP (current_buffer->selective_display)
10319 && XINT (current_buffer->selective_display) > 0
10320 && (BEG_UNCHANGED < start || GPT <= start))
10321 unchanged_p = 0;
10322
10323 /* If there are overlays at the start or end of the line, these
10324 may have overlay strings with newlines in them. A change at
10325 START, for instance, may actually concern the display of such
10326 overlay strings as well, and they are displayed on different
10327 lines. So, quickly rule out this case. (For the future, it
10328 might be desirable to implement something more telling than
10329 just BEG/END_UNCHANGED.) */
10330 if (unchanged_p)
10331 {
10332 if (BEG + BEG_UNCHANGED == start
10333 && overlay_touches_p (start))
10334 unchanged_p = 0;
10335 if (END_UNCHANGED == end
10336 && overlay_touches_p (Z - end))
10337 unchanged_p = 0;
10338 }
10339 }
10340
10341 return unchanged_p;
10342 }
10343
10344
10345 /* Do a frame update, taking possible shortcuts into account. This is
10346 the main external entry point for redisplay.
10347
10348 If the last redisplay displayed an echo area message and that message
10349 is no longer requested, we clear the echo area or bring back the
10350 mini-buffer if that is in use. */
10351
10352 void
10353 redisplay ()
10354 {
10355 redisplay_internal (0);
10356 }
10357
10358
10359 static Lisp_Object
10360 overlay_arrow_string_or_property (var)
10361 Lisp_Object var;
10362 {
10363 Lisp_Object val;
10364
10365 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10366 return val;
10367
10368 return Voverlay_arrow_string;
10369 }
10370
10371 /* Return 1 if there are any overlay-arrows in current_buffer. */
10372 static int
10373 overlay_arrow_in_current_buffer_p ()
10374 {
10375 Lisp_Object vlist;
10376
10377 for (vlist = Voverlay_arrow_variable_list;
10378 CONSP (vlist);
10379 vlist = XCDR (vlist))
10380 {
10381 Lisp_Object var = XCAR (vlist);
10382 Lisp_Object val;
10383
10384 if (!SYMBOLP (var))
10385 continue;
10386 val = find_symbol_value (var);
10387 if (MARKERP (val)
10388 && current_buffer == XMARKER (val)->buffer)
10389 return 1;
10390 }
10391 return 0;
10392 }
10393
10394
10395 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10396 has changed. */
10397
10398 static int
10399 overlay_arrows_changed_p ()
10400 {
10401 Lisp_Object vlist;
10402
10403 for (vlist = Voverlay_arrow_variable_list;
10404 CONSP (vlist);
10405 vlist = XCDR (vlist))
10406 {
10407 Lisp_Object var = XCAR (vlist);
10408 Lisp_Object val, pstr;
10409
10410 if (!SYMBOLP (var))
10411 continue;
10412 val = find_symbol_value (var);
10413 if (!MARKERP (val))
10414 continue;
10415 if (! EQ (COERCE_MARKER (val),
10416 Fget (var, Qlast_arrow_position))
10417 || ! (pstr = overlay_arrow_string_or_property (var),
10418 EQ (pstr, Fget (var, Qlast_arrow_string))))
10419 return 1;
10420 }
10421 return 0;
10422 }
10423
10424 /* Mark overlay arrows to be updated on next redisplay. */
10425
10426 static void
10427 update_overlay_arrows (up_to_date)
10428 int up_to_date;
10429 {
10430 Lisp_Object vlist;
10431
10432 for (vlist = Voverlay_arrow_variable_list;
10433 CONSP (vlist);
10434 vlist = XCDR (vlist))
10435 {
10436 Lisp_Object var = XCAR (vlist);
10437
10438 if (!SYMBOLP (var))
10439 continue;
10440
10441 if (up_to_date > 0)
10442 {
10443 Lisp_Object val = find_symbol_value (var);
10444 Fput (var, Qlast_arrow_position,
10445 COERCE_MARKER (val));
10446 Fput (var, Qlast_arrow_string,
10447 overlay_arrow_string_or_property (var));
10448 }
10449 else if (up_to_date < 0
10450 || !NILP (Fget (var, Qlast_arrow_position)))
10451 {
10452 Fput (var, Qlast_arrow_position, Qt);
10453 Fput (var, Qlast_arrow_string, Qt);
10454 }
10455 }
10456 }
10457
10458
10459 /* Return overlay arrow string to display at row.
10460 Return integer (bitmap number) for arrow bitmap in left fringe.
10461 Return nil if no overlay arrow. */
10462
10463 static Lisp_Object
10464 overlay_arrow_at_row (it, row)
10465 struct it *it;
10466 struct glyph_row *row;
10467 {
10468 Lisp_Object vlist;
10469
10470 for (vlist = Voverlay_arrow_variable_list;
10471 CONSP (vlist);
10472 vlist = XCDR (vlist))
10473 {
10474 Lisp_Object var = XCAR (vlist);
10475 Lisp_Object val;
10476
10477 if (!SYMBOLP (var))
10478 continue;
10479
10480 val = find_symbol_value (var);
10481
10482 if (MARKERP (val)
10483 && current_buffer == XMARKER (val)->buffer
10484 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10485 {
10486 if (FRAME_WINDOW_P (it->f)
10487 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10488 {
10489 #ifdef HAVE_WINDOW_SYSTEM
10490 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10491 {
10492 int fringe_bitmap;
10493 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10494 return make_number (fringe_bitmap);
10495 }
10496 #endif
10497 return make_number (-1); /* Use default arrow bitmap */
10498 }
10499 return overlay_arrow_string_or_property (var);
10500 }
10501 }
10502
10503 return Qnil;
10504 }
10505
10506 /* Return 1 if point moved out of or into a composition. Otherwise
10507 return 0. PREV_BUF and PREV_PT are the last point buffer and
10508 position. BUF and PT are the current point buffer and position. */
10509
10510 int
10511 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10512 struct buffer *prev_buf, *buf;
10513 int prev_pt, pt;
10514 {
10515 int start, end;
10516 Lisp_Object prop;
10517 Lisp_Object buffer;
10518
10519 XSETBUFFER (buffer, buf);
10520 /* Check a composition at the last point if point moved within the
10521 same buffer. */
10522 if (prev_buf == buf)
10523 {
10524 if (prev_pt == pt)
10525 /* Point didn't move. */
10526 return 0;
10527
10528 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10529 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10530 && COMPOSITION_VALID_P (start, end, prop)
10531 && start < prev_pt && end > prev_pt)
10532 /* The last point was within the composition. Return 1 iff
10533 point moved out of the composition. */
10534 return (pt <= start || pt >= end);
10535 }
10536
10537 /* Check a composition at the current point. */
10538 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10539 && find_composition (pt, -1, &start, &end, &prop, buffer)
10540 && COMPOSITION_VALID_P (start, end, prop)
10541 && start < pt && end > pt);
10542 }
10543
10544
10545 /* Reconsider the setting of B->clip_changed which is displayed
10546 in window W. */
10547
10548 static INLINE void
10549 reconsider_clip_changes (w, b)
10550 struct window *w;
10551 struct buffer *b;
10552 {
10553 if (b->clip_changed
10554 && !NILP (w->window_end_valid)
10555 && w->current_matrix->buffer == b
10556 && w->current_matrix->zv == BUF_ZV (b)
10557 && w->current_matrix->begv == BUF_BEGV (b))
10558 b->clip_changed = 0;
10559
10560 /* If display wasn't paused, and W is not a tool bar window, see if
10561 point has been moved into or out of a composition. In that case,
10562 we set b->clip_changed to 1 to force updating the screen. If
10563 b->clip_changed has already been set to 1, we can skip this
10564 check. */
10565 if (!b->clip_changed
10566 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10567 {
10568 int pt;
10569
10570 if (w == XWINDOW (selected_window))
10571 pt = BUF_PT (current_buffer);
10572 else
10573 pt = marker_position (w->pointm);
10574
10575 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10576 || pt != XINT (w->last_point))
10577 && check_point_in_composition (w->current_matrix->buffer,
10578 XINT (w->last_point),
10579 XBUFFER (w->buffer), pt))
10580 b->clip_changed = 1;
10581 }
10582 }
10583 \f
10584
10585 /* Select FRAME to forward the values of frame-local variables into C
10586 variables so that the redisplay routines can access those values
10587 directly. */
10588
10589 static void
10590 select_frame_for_redisplay (frame)
10591 Lisp_Object frame;
10592 {
10593 Lisp_Object tail, sym, val;
10594 Lisp_Object old = selected_frame;
10595
10596 selected_frame = frame;
10597
10598 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10599 if (CONSP (XCAR (tail))
10600 && (sym = XCAR (XCAR (tail)),
10601 SYMBOLP (sym))
10602 && (sym = indirect_variable (sym),
10603 val = SYMBOL_VALUE (sym),
10604 (BUFFER_LOCAL_VALUEP (val)
10605 || SOME_BUFFER_LOCAL_VALUEP (val)))
10606 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10607 /* Use find_symbol_value rather than Fsymbol_value
10608 to avoid an error if it is void. */
10609 find_symbol_value (sym);
10610
10611 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10612 if (CONSP (XCAR (tail))
10613 && (sym = XCAR (XCAR (tail)),
10614 SYMBOLP (sym))
10615 && (sym = indirect_variable (sym),
10616 val = SYMBOL_VALUE (sym),
10617 (BUFFER_LOCAL_VALUEP (val)
10618 || SOME_BUFFER_LOCAL_VALUEP (val)))
10619 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10620 find_symbol_value (sym);
10621 }
10622
10623
10624 #define STOP_POLLING \
10625 do { if (! polling_stopped_here) stop_polling (); \
10626 polling_stopped_here = 1; } while (0)
10627
10628 #define RESUME_POLLING \
10629 do { if (polling_stopped_here) start_polling (); \
10630 polling_stopped_here = 0; } while (0)
10631
10632
10633 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10634 response to any user action; therefore, we should preserve the echo
10635 area. (Actually, our caller does that job.) Perhaps in the future
10636 avoid recentering windows if it is not necessary; currently that
10637 causes some problems. */
10638
10639 static void
10640 redisplay_internal (preserve_echo_area)
10641 int preserve_echo_area;
10642 {
10643 struct window *w = XWINDOW (selected_window);
10644 struct frame *f = XFRAME (w->frame);
10645 int pause;
10646 int must_finish = 0;
10647 struct text_pos tlbufpos, tlendpos;
10648 int number_of_visible_frames;
10649 int count;
10650 struct frame *sf = SELECTED_FRAME ();
10651 int polling_stopped_here = 0;
10652
10653 /* Non-zero means redisplay has to consider all windows on all
10654 frames. Zero means, only selected_window is considered. */
10655 int consider_all_windows_p;
10656
10657 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10658
10659 /* No redisplay if running in batch mode or frame is not yet fully
10660 initialized, or redisplay is explicitly turned off by setting
10661 Vinhibit_redisplay. */
10662 if (noninteractive
10663 || !NILP (Vinhibit_redisplay)
10664 || !f->glyphs_initialized_p)
10665 return;
10666
10667 /* The flag redisplay_performed_directly_p is set by
10668 direct_output_for_insert when it already did the whole screen
10669 update necessary. */
10670 if (redisplay_performed_directly_p)
10671 {
10672 redisplay_performed_directly_p = 0;
10673 if (!hscroll_windows (selected_window))
10674 return;
10675 }
10676
10677 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10678 if (popup_activated ())
10679 return;
10680 #endif
10681
10682 /* I don't think this happens but let's be paranoid. */
10683 if (redisplaying_p)
10684 return;
10685
10686 /* Record a function that resets redisplaying_p to its old value
10687 when we leave this function. */
10688 count = SPECPDL_INDEX ();
10689 record_unwind_protect (unwind_redisplay,
10690 Fcons (make_number (redisplaying_p), selected_frame));
10691 ++redisplaying_p;
10692 specbind (Qinhibit_free_realized_faces, Qnil);
10693
10694 {
10695 Lisp_Object tail, frame;
10696
10697 FOR_EACH_FRAME (tail, frame)
10698 {
10699 struct frame *f = XFRAME (frame);
10700 f->already_hscrolled_p = 0;
10701 }
10702 }
10703
10704 retry:
10705 pause = 0;
10706 reconsider_clip_changes (w, current_buffer);
10707 last_escape_glyph_frame = NULL;
10708 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10709
10710 /* If new fonts have been loaded that make a glyph matrix adjustment
10711 necessary, do it. */
10712 if (fonts_changed_p)
10713 {
10714 adjust_glyphs (NULL);
10715 ++windows_or_buffers_changed;
10716 fonts_changed_p = 0;
10717 }
10718
10719 /* If face_change_count is non-zero, init_iterator will free all
10720 realized faces, which includes the faces referenced from current
10721 matrices. So, we can't reuse current matrices in this case. */
10722 if (face_change_count)
10723 ++windows_or_buffers_changed;
10724
10725 if (! FRAME_WINDOW_P (sf)
10726 && previous_terminal_frame != sf)
10727 {
10728 /* Since frames on an ASCII terminal share the same display
10729 area, displaying a different frame means redisplay the whole
10730 thing. */
10731 windows_or_buffers_changed++;
10732 SET_FRAME_GARBAGED (sf);
10733 XSETFRAME (Vterminal_frame, sf);
10734 }
10735 previous_terminal_frame = sf;
10736
10737 /* Set the visible flags for all frames. Do this before checking
10738 for resized or garbaged frames; they want to know if their frames
10739 are visible. See the comment in frame.h for
10740 FRAME_SAMPLE_VISIBILITY. */
10741 {
10742 Lisp_Object tail, frame;
10743
10744 number_of_visible_frames = 0;
10745
10746 FOR_EACH_FRAME (tail, frame)
10747 {
10748 struct frame *f = XFRAME (frame);
10749
10750 FRAME_SAMPLE_VISIBILITY (f);
10751 if (FRAME_VISIBLE_P (f))
10752 ++number_of_visible_frames;
10753 clear_desired_matrices (f);
10754 }
10755 }
10756
10757 /* Notice any pending interrupt request to change frame size. */
10758 do_pending_window_change (1);
10759
10760 /* Clear frames marked as garbaged. */
10761 if (frame_garbaged)
10762 clear_garbaged_frames ();
10763
10764 /* Build menubar and tool-bar items. */
10765 if (NILP (Vmemory_full))
10766 prepare_menu_bars ();
10767
10768 if (windows_or_buffers_changed)
10769 update_mode_lines++;
10770
10771 /* Detect case that we need to write or remove a star in the mode line. */
10772 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10773 {
10774 w->update_mode_line = Qt;
10775 if (buffer_shared > 1)
10776 update_mode_lines++;
10777 }
10778
10779 /* If %c is in the mode line, update it if needed. */
10780 if (!NILP (w->column_number_displayed)
10781 /* This alternative quickly identifies a common case
10782 where no change is needed. */
10783 && !(PT == XFASTINT (w->last_point)
10784 && XFASTINT (w->last_modified) >= MODIFF
10785 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10786 && (XFASTINT (w->column_number_displayed)
10787 != (int) current_column ())) /* iftc */
10788 w->update_mode_line = Qt;
10789
10790 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10791
10792 /* The variable buffer_shared is set in redisplay_window and
10793 indicates that we redisplay a buffer in different windows. See
10794 there. */
10795 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10796 || cursor_type_changed);
10797
10798 /* If specs for an arrow have changed, do thorough redisplay
10799 to ensure we remove any arrow that should no longer exist. */
10800 if (overlay_arrows_changed_p ())
10801 consider_all_windows_p = windows_or_buffers_changed = 1;
10802
10803 /* Normally the message* functions will have already displayed and
10804 updated the echo area, but the frame may have been trashed, or
10805 the update may have been preempted, so display the echo area
10806 again here. Checking message_cleared_p captures the case that
10807 the echo area should be cleared. */
10808 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10809 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10810 || (message_cleared_p
10811 && minibuf_level == 0
10812 /* If the mini-window is currently selected, this means the
10813 echo-area doesn't show through. */
10814 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10815 {
10816 int window_height_changed_p = echo_area_display (0);
10817 must_finish = 1;
10818
10819 /* If we don't display the current message, don't clear the
10820 message_cleared_p flag, because, if we did, we wouldn't clear
10821 the echo area in the next redisplay which doesn't preserve
10822 the echo area. */
10823 if (!display_last_displayed_message_p)
10824 message_cleared_p = 0;
10825
10826 if (fonts_changed_p)
10827 goto retry;
10828 else if (window_height_changed_p)
10829 {
10830 consider_all_windows_p = 1;
10831 ++update_mode_lines;
10832 ++windows_or_buffers_changed;
10833
10834 /* If window configuration was changed, frames may have been
10835 marked garbaged. Clear them or we will experience
10836 surprises wrt scrolling. */
10837 if (frame_garbaged)
10838 clear_garbaged_frames ();
10839 }
10840 }
10841 else if (EQ (selected_window, minibuf_window)
10842 && (current_buffer->clip_changed
10843 || XFASTINT (w->last_modified) < MODIFF
10844 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10845 && resize_mini_window (w, 0))
10846 {
10847 /* Resized active mini-window to fit the size of what it is
10848 showing if its contents might have changed. */
10849 must_finish = 1;
10850 consider_all_windows_p = 1;
10851 ++windows_or_buffers_changed;
10852 ++update_mode_lines;
10853
10854 /* If window configuration was changed, frames may have been
10855 marked garbaged. Clear them or we will experience
10856 surprises wrt scrolling. */
10857 if (frame_garbaged)
10858 clear_garbaged_frames ();
10859 }
10860
10861
10862 /* If showing the region, and mark has changed, we must redisplay
10863 the whole window. The assignment to this_line_start_pos prevents
10864 the optimization directly below this if-statement. */
10865 if (((!NILP (Vtransient_mark_mode)
10866 && !NILP (XBUFFER (w->buffer)->mark_active))
10867 != !NILP (w->region_showing))
10868 || (!NILP (w->region_showing)
10869 && !EQ (w->region_showing,
10870 Fmarker_position (XBUFFER (w->buffer)->mark))))
10871 CHARPOS (this_line_start_pos) = 0;
10872
10873 /* Optimize the case that only the line containing the cursor in the
10874 selected window has changed. Variables starting with this_ are
10875 set in display_line and record information about the line
10876 containing the cursor. */
10877 tlbufpos = this_line_start_pos;
10878 tlendpos = this_line_end_pos;
10879 if (!consider_all_windows_p
10880 && CHARPOS (tlbufpos) > 0
10881 && NILP (w->update_mode_line)
10882 && !current_buffer->clip_changed
10883 && !current_buffer->prevent_redisplay_optimizations_p
10884 && FRAME_VISIBLE_P (XFRAME (w->frame))
10885 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10886 /* Make sure recorded data applies to current buffer, etc. */
10887 && this_line_buffer == current_buffer
10888 && current_buffer == XBUFFER (w->buffer)
10889 && NILP (w->force_start)
10890 && NILP (w->optional_new_start)
10891 /* Point must be on the line that we have info recorded about. */
10892 && PT >= CHARPOS (tlbufpos)
10893 && PT <= Z - CHARPOS (tlendpos)
10894 /* All text outside that line, including its final newline,
10895 must be unchanged */
10896 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10897 CHARPOS (tlendpos)))
10898 {
10899 if (CHARPOS (tlbufpos) > BEGV
10900 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10901 && (CHARPOS (tlbufpos) == ZV
10902 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10903 /* Former continuation line has disappeared by becoming empty */
10904 goto cancel;
10905 else if (XFASTINT (w->last_modified) < MODIFF
10906 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10907 || MINI_WINDOW_P (w))
10908 {
10909 /* We have to handle the case of continuation around a
10910 wide-column character (See the comment in indent.c around
10911 line 885).
10912
10913 For instance, in the following case:
10914
10915 -------- Insert --------
10916 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10917 J_I_ ==> J_I_ `^^' are cursors.
10918 ^^ ^^
10919 -------- --------
10920
10921 As we have to redraw the line above, we should goto cancel. */
10922
10923 struct it it;
10924 int line_height_before = this_line_pixel_height;
10925
10926 /* Note that start_display will handle the case that the
10927 line starting at tlbufpos is a continuation lines. */
10928 start_display (&it, w, tlbufpos);
10929
10930 /* Implementation note: It this still necessary? */
10931 if (it.current_x != this_line_start_x)
10932 goto cancel;
10933
10934 TRACE ((stderr, "trying display optimization 1\n"));
10935 w->cursor.vpos = -1;
10936 overlay_arrow_seen = 0;
10937 it.vpos = this_line_vpos;
10938 it.current_y = this_line_y;
10939 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10940 display_line (&it);
10941
10942 /* If line contains point, is not continued,
10943 and ends at same distance from eob as before, we win */
10944 if (w->cursor.vpos >= 0
10945 /* Line is not continued, otherwise this_line_start_pos
10946 would have been set to 0 in display_line. */
10947 && CHARPOS (this_line_start_pos)
10948 /* Line ends as before. */
10949 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10950 /* Line has same height as before. Otherwise other lines
10951 would have to be shifted up or down. */
10952 && this_line_pixel_height == line_height_before)
10953 {
10954 /* If this is not the window's last line, we must adjust
10955 the charstarts of the lines below. */
10956 if (it.current_y < it.last_visible_y)
10957 {
10958 struct glyph_row *row
10959 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10960 int delta, delta_bytes;
10961
10962 if (Z - CHARPOS (tlendpos) == ZV)
10963 {
10964 /* This line ends at end of (accessible part of)
10965 buffer. There is no newline to count. */
10966 delta = (Z
10967 - CHARPOS (tlendpos)
10968 - MATRIX_ROW_START_CHARPOS (row));
10969 delta_bytes = (Z_BYTE
10970 - BYTEPOS (tlendpos)
10971 - MATRIX_ROW_START_BYTEPOS (row));
10972 }
10973 else
10974 {
10975 /* This line ends in a newline. Must take
10976 account of the newline and the rest of the
10977 text that follows. */
10978 delta = (Z
10979 - CHARPOS (tlendpos)
10980 - MATRIX_ROW_START_CHARPOS (row));
10981 delta_bytes = (Z_BYTE
10982 - BYTEPOS (tlendpos)
10983 - MATRIX_ROW_START_BYTEPOS (row));
10984 }
10985
10986 increment_matrix_positions (w->current_matrix,
10987 this_line_vpos + 1,
10988 w->current_matrix->nrows,
10989 delta, delta_bytes);
10990 }
10991
10992 /* If this row displays text now but previously didn't,
10993 or vice versa, w->window_end_vpos may have to be
10994 adjusted. */
10995 if ((it.glyph_row - 1)->displays_text_p)
10996 {
10997 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10998 XSETINT (w->window_end_vpos, this_line_vpos);
10999 }
11000 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11001 && this_line_vpos > 0)
11002 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11003 w->window_end_valid = Qnil;
11004
11005 /* Update hint: No need to try to scroll in update_window. */
11006 w->desired_matrix->no_scrolling_p = 1;
11007
11008 #if GLYPH_DEBUG
11009 *w->desired_matrix->method = 0;
11010 debug_method_add (w, "optimization 1");
11011 #endif
11012 #ifdef HAVE_WINDOW_SYSTEM
11013 update_window_fringes (w, 0);
11014 #endif
11015 goto update;
11016 }
11017 else
11018 goto cancel;
11019 }
11020 else if (/* Cursor position hasn't changed. */
11021 PT == XFASTINT (w->last_point)
11022 /* Make sure the cursor was last displayed
11023 in this window. Otherwise we have to reposition it. */
11024 && 0 <= w->cursor.vpos
11025 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11026 {
11027 if (!must_finish)
11028 {
11029 do_pending_window_change (1);
11030
11031 /* We used to always goto end_of_redisplay here, but this
11032 isn't enough if we have a blinking cursor. */
11033 if (w->cursor_off_p == w->last_cursor_off_p)
11034 goto end_of_redisplay;
11035 }
11036 goto update;
11037 }
11038 /* If highlighting the region, or if the cursor is in the echo area,
11039 then we can't just move the cursor. */
11040 else if (! (!NILP (Vtransient_mark_mode)
11041 && !NILP (current_buffer->mark_active))
11042 && (EQ (selected_window, current_buffer->last_selected_window)
11043 || highlight_nonselected_windows)
11044 && NILP (w->region_showing)
11045 && NILP (Vshow_trailing_whitespace)
11046 && !cursor_in_echo_area)
11047 {
11048 struct it it;
11049 struct glyph_row *row;
11050
11051 /* Skip from tlbufpos to PT and see where it is. Note that
11052 PT may be in invisible text. If so, we will end at the
11053 next visible position. */
11054 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11055 NULL, DEFAULT_FACE_ID);
11056 it.current_x = this_line_start_x;
11057 it.current_y = this_line_y;
11058 it.vpos = this_line_vpos;
11059
11060 /* The call to move_it_to stops in front of PT, but
11061 moves over before-strings. */
11062 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11063
11064 if (it.vpos == this_line_vpos
11065 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11066 row->enabled_p))
11067 {
11068 xassert (this_line_vpos == it.vpos);
11069 xassert (this_line_y == it.current_y);
11070 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11071 #if GLYPH_DEBUG
11072 *w->desired_matrix->method = 0;
11073 debug_method_add (w, "optimization 3");
11074 #endif
11075 goto update;
11076 }
11077 else
11078 goto cancel;
11079 }
11080
11081 cancel:
11082 /* Text changed drastically or point moved off of line. */
11083 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11084 }
11085
11086 CHARPOS (this_line_start_pos) = 0;
11087 consider_all_windows_p |= buffer_shared > 1;
11088 ++clear_face_cache_count;
11089 #ifdef HAVE_WINDOW_SYSTEM
11090 ++clear_image_cache_count;
11091 #endif
11092
11093 /* Build desired matrices, and update the display. If
11094 consider_all_windows_p is non-zero, do it for all windows on all
11095 frames. Otherwise do it for selected_window, only. */
11096
11097 if (consider_all_windows_p)
11098 {
11099 Lisp_Object tail, frame;
11100
11101 FOR_EACH_FRAME (tail, frame)
11102 XFRAME (frame)->updated_p = 0;
11103
11104 /* Recompute # windows showing selected buffer. This will be
11105 incremented each time such a window is displayed. */
11106 buffer_shared = 0;
11107
11108 FOR_EACH_FRAME (tail, frame)
11109 {
11110 struct frame *f = XFRAME (frame);
11111
11112 if (FRAME_WINDOW_P (f) || f == sf)
11113 {
11114 if (! EQ (frame, selected_frame))
11115 /* Select the frame, for the sake of frame-local
11116 variables. */
11117 select_frame_for_redisplay (frame);
11118
11119 /* Mark all the scroll bars to be removed; we'll redeem
11120 the ones we want when we redisplay their windows. */
11121 if (condemn_scroll_bars_hook)
11122 condemn_scroll_bars_hook (f);
11123
11124 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11125 redisplay_windows (FRAME_ROOT_WINDOW (f));
11126
11127 /* Any scroll bars which redisplay_windows should have
11128 nuked should now go away. */
11129 if (judge_scroll_bars_hook)
11130 judge_scroll_bars_hook (f);
11131
11132 /* If fonts changed, display again. */
11133 /* ??? rms: I suspect it is a mistake to jump all the way
11134 back to retry here. It should just retry this frame. */
11135 if (fonts_changed_p)
11136 goto retry;
11137
11138 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11139 {
11140 /* See if we have to hscroll. */
11141 if (!f->already_hscrolled_p)
11142 {
11143 f->already_hscrolled_p = 1;
11144 if (hscroll_windows (f->root_window))
11145 goto retry;
11146 }
11147
11148 /* Prevent various kinds of signals during display
11149 update. stdio is not robust about handling
11150 signals, which can cause an apparent I/O
11151 error. */
11152 if (interrupt_input)
11153 unrequest_sigio ();
11154 STOP_POLLING;
11155
11156 /* Update the display. */
11157 set_window_update_flags (XWINDOW (f->root_window), 1);
11158 pause |= update_frame (f, 0, 0);
11159 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11160 if (pause)
11161 break;
11162 #endif
11163
11164 f->updated_p = 1;
11165 }
11166 }
11167 }
11168
11169 if (!pause)
11170 {
11171 /* Do the mark_window_display_accurate after all windows have
11172 been redisplayed because this call resets flags in buffers
11173 which are needed for proper redisplay. */
11174 FOR_EACH_FRAME (tail, frame)
11175 {
11176 struct frame *f = XFRAME (frame);
11177 if (f->updated_p)
11178 {
11179 mark_window_display_accurate (f->root_window, 1);
11180 if (frame_up_to_date_hook)
11181 frame_up_to_date_hook (f);
11182 }
11183 }
11184 }
11185 }
11186 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11187 {
11188 Lisp_Object mini_window;
11189 struct frame *mini_frame;
11190
11191 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11192 /* Use list_of_error, not Qerror, so that
11193 we catch only errors and don't run the debugger. */
11194 internal_condition_case_1 (redisplay_window_1, selected_window,
11195 list_of_error,
11196 redisplay_window_error);
11197
11198 /* Compare desired and current matrices, perform output. */
11199
11200 update:
11201 /* If fonts changed, display again. */
11202 if (fonts_changed_p)
11203 goto retry;
11204
11205 /* Prevent various kinds of signals during display update.
11206 stdio is not robust about handling signals,
11207 which can cause an apparent I/O error. */
11208 if (interrupt_input)
11209 unrequest_sigio ();
11210 STOP_POLLING;
11211
11212 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11213 {
11214 if (hscroll_windows (selected_window))
11215 goto retry;
11216
11217 XWINDOW (selected_window)->must_be_updated_p = 1;
11218 pause = update_frame (sf, 0, 0);
11219 }
11220
11221 /* We may have called echo_area_display at the top of this
11222 function. If the echo area is on another frame, that may
11223 have put text on a frame other than the selected one, so the
11224 above call to update_frame would not have caught it. Catch
11225 it here. */
11226 mini_window = FRAME_MINIBUF_WINDOW (sf);
11227 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11228
11229 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11230 {
11231 XWINDOW (mini_window)->must_be_updated_p = 1;
11232 pause |= update_frame (mini_frame, 0, 0);
11233 if (!pause && hscroll_windows (mini_window))
11234 goto retry;
11235 }
11236 }
11237
11238 /* If display was paused because of pending input, make sure we do a
11239 thorough update the next time. */
11240 if (pause)
11241 {
11242 /* Prevent the optimization at the beginning of
11243 redisplay_internal that tries a single-line update of the
11244 line containing the cursor in the selected window. */
11245 CHARPOS (this_line_start_pos) = 0;
11246
11247 /* Let the overlay arrow be updated the next time. */
11248 update_overlay_arrows (0);
11249
11250 /* If we pause after scrolling, some rows in the current
11251 matrices of some windows are not valid. */
11252 if (!WINDOW_FULL_WIDTH_P (w)
11253 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11254 update_mode_lines = 1;
11255 }
11256 else
11257 {
11258 if (!consider_all_windows_p)
11259 {
11260 /* This has already been done above if
11261 consider_all_windows_p is set. */
11262 mark_window_display_accurate_1 (w, 1);
11263
11264 /* Say overlay arrows are up to date. */
11265 update_overlay_arrows (1);
11266
11267 if (frame_up_to_date_hook != 0)
11268 frame_up_to_date_hook (sf);
11269 }
11270
11271 update_mode_lines = 0;
11272 windows_or_buffers_changed = 0;
11273 cursor_type_changed = 0;
11274 }
11275
11276 /* Start SIGIO interrupts coming again. Having them off during the
11277 code above makes it less likely one will discard output, but not
11278 impossible, since there might be stuff in the system buffer here.
11279 But it is much hairier to try to do anything about that. */
11280 if (interrupt_input)
11281 request_sigio ();
11282 RESUME_POLLING;
11283
11284 /* If a frame has become visible which was not before, redisplay
11285 again, so that we display it. Expose events for such a frame
11286 (which it gets when becoming visible) don't call the parts of
11287 redisplay constructing glyphs, so simply exposing a frame won't
11288 display anything in this case. So, we have to display these
11289 frames here explicitly. */
11290 if (!pause)
11291 {
11292 Lisp_Object tail, frame;
11293 int new_count = 0;
11294
11295 FOR_EACH_FRAME (tail, frame)
11296 {
11297 int this_is_visible = 0;
11298
11299 if (XFRAME (frame)->visible)
11300 this_is_visible = 1;
11301 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11302 if (XFRAME (frame)->visible)
11303 this_is_visible = 1;
11304
11305 if (this_is_visible)
11306 new_count++;
11307 }
11308
11309 if (new_count != number_of_visible_frames)
11310 windows_or_buffers_changed++;
11311 }
11312
11313 /* Change frame size now if a change is pending. */
11314 do_pending_window_change (1);
11315
11316 /* If we just did a pending size change, or have additional
11317 visible frames, redisplay again. */
11318 if (windows_or_buffers_changed && !pause)
11319 goto retry;
11320
11321 /* Clear the face cache eventually. */
11322 if (consider_all_windows_p)
11323 {
11324 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11325 {
11326 clear_face_cache (0);
11327 clear_face_cache_count = 0;
11328 }
11329 #ifdef HAVE_WINDOW_SYSTEM
11330 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11331 {
11332 Lisp_Object tail, frame;
11333 FOR_EACH_FRAME (tail, frame)
11334 {
11335 struct frame *f = XFRAME (frame);
11336 if (FRAME_WINDOW_P (f))
11337 clear_image_cache (f, 0);
11338 }
11339 clear_image_cache_count = 0;
11340 }
11341 #endif /* HAVE_WINDOW_SYSTEM */
11342 }
11343
11344 end_of_redisplay:
11345 unbind_to (count, Qnil);
11346 RESUME_POLLING;
11347 }
11348
11349
11350 /* Redisplay, but leave alone any recent echo area message unless
11351 another message has been requested in its place.
11352
11353 This is useful in situations where you need to redisplay but no
11354 user action has occurred, making it inappropriate for the message
11355 area to be cleared. See tracking_off and
11356 wait_reading_process_output for examples of these situations.
11357
11358 FROM_WHERE is an integer saying from where this function was
11359 called. This is useful for debugging. */
11360
11361 void
11362 redisplay_preserve_echo_area (from_where)
11363 int from_where;
11364 {
11365 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11366
11367 if (!NILP (echo_area_buffer[1]))
11368 {
11369 /* We have a previously displayed message, but no current
11370 message. Redisplay the previous message. */
11371 display_last_displayed_message_p = 1;
11372 redisplay_internal (1);
11373 display_last_displayed_message_p = 0;
11374 }
11375 else
11376 redisplay_internal (1);
11377
11378 if (rif != NULL && rif->flush_display_optional)
11379 rif->flush_display_optional (NULL);
11380 }
11381
11382
11383 /* Function registered with record_unwind_protect in
11384 redisplay_internal. Reset redisplaying_p to the value it had
11385 before redisplay_internal was called, and clear
11386 prevent_freeing_realized_faces_p. It also selects the previously
11387 selected frame. */
11388
11389 static Lisp_Object
11390 unwind_redisplay (val)
11391 Lisp_Object val;
11392 {
11393 Lisp_Object old_redisplaying_p, old_frame;
11394
11395 old_redisplaying_p = XCAR (val);
11396 redisplaying_p = XFASTINT (old_redisplaying_p);
11397 old_frame = XCDR (val);
11398 if (! EQ (old_frame, selected_frame))
11399 select_frame_for_redisplay (old_frame);
11400 return Qnil;
11401 }
11402
11403
11404 /* Mark the display of window W as accurate or inaccurate. If
11405 ACCURATE_P is non-zero mark display of W as accurate. If
11406 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11407 redisplay_internal is called. */
11408
11409 static void
11410 mark_window_display_accurate_1 (w, accurate_p)
11411 struct window *w;
11412 int accurate_p;
11413 {
11414 if (BUFFERP (w->buffer))
11415 {
11416 struct buffer *b = XBUFFER (w->buffer);
11417
11418 w->last_modified
11419 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11420 w->last_overlay_modified
11421 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11422 w->last_had_star
11423 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11424
11425 if (accurate_p)
11426 {
11427 b->clip_changed = 0;
11428 b->prevent_redisplay_optimizations_p = 0;
11429
11430 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11431 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11432 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11433 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11434
11435 w->current_matrix->buffer = b;
11436 w->current_matrix->begv = BUF_BEGV (b);
11437 w->current_matrix->zv = BUF_ZV (b);
11438
11439 w->last_cursor = w->cursor;
11440 w->last_cursor_off_p = w->cursor_off_p;
11441
11442 if (w == XWINDOW (selected_window))
11443 w->last_point = make_number (BUF_PT (b));
11444 else
11445 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11446 }
11447 }
11448
11449 if (accurate_p)
11450 {
11451 w->window_end_valid = w->buffer;
11452 #if 0 /* This is incorrect with variable-height lines. */
11453 xassert (XINT (w->window_end_vpos)
11454 < (WINDOW_TOTAL_LINES (w)
11455 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11456 #endif
11457 w->update_mode_line = Qnil;
11458 }
11459 }
11460
11461
11462 /* Mark the display of windows in the window tree rooted at WINDOW as
11463 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11464 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11465 be redisplayed the next time redisplay_internal is called. */
11466
11467 void
11468 mark_window_display_accurate (window, accurate_p)
11469 Lisp_Object window;
11470 int accurate_p;
11471 {
11472 struct window *w;
11473
11474 for (; !NILP (window); window = w->next)
11475 {
11476 w = XWINDOW (window);
11477 mark_window_display_accurate_1 (w, accurate_p);
11478
11479 if (!NILP (w->vchild))
11480 mark_window_display_accurate (w->vchild, accurate_p);
11481 if (!NILP (w->hchild))
11482 mark_window_display_accurate (w->hchild, accurate_p);
11483 }
11484
11485 if (accurate_p)
11486 {
11487 update_overlay_arrows (1);
11488 }
11489 else
11490 {
11491 /* Force a thorough redisplay the next time by setting
11492 last_arrow_position and last_arrow_string to t, which is
11493 unequal to any useful value of Voverlay_arrow_... */
11494 update_overlay_arrows (-1);
11495 }
11496 }
11497
11498
11499 /* Return value in display table DP (Lisp_Char_Table *) for character
11500 C. Since a display table doesn't have any parent, we don't have to
11501 follow parent. Do not call this function directly but use the
11502 macro DISP_CHAR_VECTOR. */
11503
11504 Lisp_Object
11505 disp_char_vector (dp, c)
11506 struct Lisp_Char_Table *dp;
11507 int c;
11508 {
11509 int code[4], i;
11510 Lisp_Object val;
11511
11512 if (SINGLE_BYTE_CHAR_P (c))
11513 return (dp->contents[c]);
11514
11515 SPLIT_CHAR (c, code[0], code[1], code[2]);
11516 if (code[1] < 32)
11517 code[1] = -1;
11518 else if (code[2] < 32)
11519 code[2] = -1;
11520
11521 /* Here, the possible range of code[0] (== charset ID) is
11522 128..max_charset. Since the top level char table contains data
11523 for multibyte characters after 256th element, we must increment
11524 code[0] by 128 to get a correct index. */
11525 code[0] += 128;
11526 code[3] = -1; /* anchor */
11527
11528 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11529 {
11530 val = dp->contents[code[i]];
11531 if (!SUB_CHAR_TABLE_P (val))
11532 return (NILP (val) ? dp->defalt : val);
11533 }
11534
11535 /* Here, val is a sub char table. We return the default value of
11536 it. */
11537 return (dp->defalt);
11538 }
11539
11540
11541 \f
11542 /***********************************************************************
11543 Window Redisplay
11544 ***********************************************************************/
11545
11546 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11547
11548 static void
11549 redisplay_windows (window)
11550 Lisp_Object window;
11551 {
11552 while (!NILP (window))
11553 {
11554 struct window *w = XWINDOW (window);
11555
11556 if (!NILP (w->hchild))
11557 redisplay_windows (w->hchild);
11558 else if (!NILP (w->vchild))
11559 redisplay_windows (w->vchild);
11560 else
11561 {
11562 displayed_buffer = XBUFFER (w->buffer);
11563 /* Use list_of_error, not Qerror, so that
11564 we catch only errors and don't run the debugger. */
11565 internal_condition_case_1 (redisplay_window_0, window,
11566 list_of_error,
11567 redisplay_window_error);
11568 }
11569
11570 window = w->next;
11571 }
11572 }
11573
11574 static Lisp_Object
11575 redisplay_window_error ()
11576 {
11577 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11578 return Qnil;
11579 }
11580
11581 static Lisp_Object
11582 redisplay_window_0 (window)
11583 Lisp_Object window;
11584 {
11585 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11586 redisplay_window (window, 0);
11587 return Qnil;
11588 }
11589
11590 static Lisp_Object
11591 redisplay_window_1 (window)
11592 Lisp_Object window;
11593 {
11594 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11595 redisplay_window (window, 1);
11596 return Qnil;
11597 }
11598 \f
11599
11600 /* Increment GLYPH until it reaches END or CONDITION fails while
11601 adding (GLYPH)->pixel_width to X. */
11602
11603 #define SKIP_GLYPHS(glyph, end, x, condition) \
11604 do \
11605 { \
11606 (x) += (glyph)->pixel_width; \
11607 ++(glyph); \
11608 } \
11609 while ((glyph) < (end) && (condition))
11610
11611
11612 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11613 DELTA is the number of bytes by which positions recorded in ROW
11614 differ from current buffer positions. */
11615
11616 void
11617 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11618 struct window *w;
11619 struct glyph_row *row;
11620 struct glyph_matrix *matrix;
11621 int delta, delta_bytes, dy, dvpos;
11622 {
11623 struct glyph *glyph = row->glyphs[TEXT_AREA];
11624 struct glyph *end = glyph + row->used[TEXT_AREA];
11625 struct glyph *cursor = NULL;
11626 /* The first glyph that starts a sequence of glyphs from string. */
11627 struct glyph *string_start;
11628 /* The X coordinate of string_start. */
11629 int string_start_x;
11630 /* The last known character position. */
11631 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11632 /* The last known character position before string_start. */
11633 int string_before_pos;
11634 int x = row->x;
11635 int cursor_x = x;
11636 int cursor_from_overlay_pos = 0;
11637 int pt_old = PT - delta;
11638
11639 /* Skip over glyphs not having an object at the start of the row.
11640 These are special glyphs like truncation marks on terminal
11641 frames. */
11642 if (row->displays_text_p)
11643 while (glyph < end
11644 && INTEGERP (glyph->object)
11645 && glyph->charpos < 0)
11646 {
11647 x += glyph->pixel_width;
11648 ++glyph;
11649 }
11650
11651 string_start = NULL;
11652 while (glyph < end
11653 && !INTEGERP (glyph->object)
11654 && (!BUFFERP (glyph->object)
11655 || (last_pos = glyph->charpos) < pt_old))
11656 {
11657 if (! STRINGP (glyph->object))
11658 {
11659 string_start = NULL;
11660 x += glyph->pixel_width;
11661 ++glyph;
11662 if (cursor_from_overlay_pos
11663 && last_pos >= cursor_from_overlay_pos)
11664 {
11665 cursor_from_overlay_pos = 0;
11666 cursor = 0;
11667 }
11668 }
11669 else
11670 {
11671 string_before_pos = last_pos;
11672 string_start = glyph;
11673 string_start_x = x;
11674 /* Skip all glyphs from string. */
11675 do
11676 {
11677 Lisp_Object cprop;
11678 int pos;
11679 if ((cursor == NULL || glyph > cursor)
11680 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11681 Qcursor, (glyph)->object),
11682 !NILP (cprop))
11683 && (pos = string_buffer_position (w, glyph->object,
11684 string_before_pos),
11685 (pos == 0 /* From overlay */
11686 || pos == pt_old)))
11687 {
11688 /* Estimate overlay buffer position from the buffer
11689 positions of the glyphs before and after the overlay.
11690 Add 1 to last_pos so that if point corresponds to the
11691 glyph right after the overlay, we still use a 'cursor'
11692 property found in that overlay. */
11693 cursor_from_overlay_pos = (pos ? 0 : last_pos
11694 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11695 cursor = glyph;
11696 cursor_x = x;
11697 }
11698 x += glyph->pixel_width;
11699 ++glyph;
11700 }
11701 while (glyph < end && EQ (glyph->object, string_start->object));
11702 }
11703 }
11704
11705 if (cursor != NULL)
11706 {
11707 glyph = cursor;
11708 x = cursor_x;
11709 }
11710 else if (row->ends_in_ellipsis_p && glyph == end)
11711 {
11712 /* Scan back over the ellipsis glyphs, decrementing positions. */
11713 while (glyph > row->glyphs[TEXT_AREA]
11714 && (glyph - 1)->charpos == last_pos)
11715 glyph--, x -= glyph->pixel_width;
11716 /* That loop always goes one position too far,
11717 including the glyph before the ellipsis.
11718 So scan forward over that one. */
11719 x += glyph->pixel_width;
11720 glyph++;
11721 }
11722 else if (string_start
11723 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11724 {
11725 /* We may have skipped over point because the previous glyphs
11726 are from string. As there's no easy way to know the
11727 character position of the current glyph, find the correct
11728 glyph on point by scanning from string_start again. */
11729 Lisp_Object limit;
11730 Lisp_Object string;
11731 int pos;
11732
11733 limit = make_number (pt_old + 1);
11734 end = glyph;
11735 glyph = string_start;
11736 x = string_start_x;
11737 string = glyph->object;
11738 pos = string_buffer_position (w, string, string_before_pos);
11739 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11740 because we always put cursor after overlay strings. */
11741 while (pos == 0 && glyph < end)
11742 {
11743 string = glyph->object;
11744 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11745 if (glyph < end)
11746 pos = string_buffer_position (w, glyph->object, string_before_pos);
11747 }
11748
11749 while (glyph < end)
11750 {
11751 pos = XINT (Fnext_single_char_property_change
11752 (make_number (pos), Qdisplay, Qnil, limit));
11753 if (pos > pt_old)
11754 break;
11755 /* Skip glyphs from the same string. */
11756 string = glyph->object;
11757 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11758 /* Skip glyphs from an overlay. */
11759 while (glyph < end
11760 && ! string_buffer_position (w, glyph->object, pos))
11761 {
11762 string = glyph->object;
11763 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11764 }
11765 }
11766 }
11767
11768 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11769 w->cursor.x = x;
11770 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11771 w->cursor.y = row->y + dy;
11772
11773 if (w == XWINDOW (selected_window))
11774 {
11775 if (!row->continued_p
11776 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11777 && row->x == 0)
11778 {
11779 this_line_buffer = XBUFFER (w->buffer);
11780
11781 CHARPOS (this_line_start_pos)
11782 = MATRIX_ROW_START_CHARPOS (row) + delta;
11783 BYTEPOS (this_line_start_pos)
11784 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11785
11786 CHARPOS (this_line_end_pos)
11787 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11788 BYTEPOS (this_line_end_pos)
11789 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11790
11791 this_line_y = w->cursor.y;
11792 this_line_pixel_height = row->height;
11793 this_line_vpos = w->cursor.vpos;
11794 this_line_start_x = row->x;
11795 }
11796 else
11797 CHARPOS (this_line_start_pos) = 0;
11798 }
11799 }
11800
11801
11802 /* Run window scroll functions, if any, for WINDOW with new window
11803 start STARTP. Sets the window start of WINDOW to that position.
11804
11805 We assume that the window's buffer is really current. */
11806
11807 static INLINE struct text_pos
11808 run_window_scroll_functions (window, startp)
11809 Lisp_Object window;
11810 struct text_pos startp;
11811 {
11812 struct window *w = XWINDOW (window);
11813 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11814
11815 if (current_buffer != XBUFFER (w->buffer))
11816 abort ();
11817
11818 if (!NILP (Vwindow_scroll_functions))
11819 {
11820 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11821 make_number (CHARPOS (startp)));
11822 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11823 /* In case the hook functions switch buffers. */
11824 if (current_buffer != XBUFFER (w->buffer))
11825 set_buffer_internal_1 (XBUFFER (w->buffer));
11826 }
11827
11828 return startp;
11829 }
11830
11831
11832 /* Make sure the line containing the cursor is fully visible.
11833 A value of 1 means there is nothing to be done.
11834 (Either the line is fully visible, or it cannot be made so,
11835 or we cannot tell.)
11836
11837 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11838 is higher than window.
11839
11840 A value of 0 means the caller should do scrolling
11841 as if point had gone off the screen. */
11842
11843 static int
11844 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11845 struct window *w;
11846 int force_p;
11847 int current_matrix_p;
11848 {
11849 struct glyph_matrix *matrix;
11850 struct glyph_row *row;
11851 int window_height;
11852
11853 if (!make_cursor_line_fully_visible_p)
11854 return 1;
11855
11856 /* It's not always possible to find the cursor, e.g, when a window
11857 is full of overlay strings. Don't do anything in that case. */
11858 if (w->cursor.vpos < 0)
11859 return 1;
11860
11861 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11862 row = MATRIX_ROW (matrix, w->cursor.vpos);
11863
11864 /* If the cursor row is not partially visible, there's nothing to do. */
11865 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11866 return 1;
11867
11868 /* If the row the cursor is in is taller than the window's height,
11869 it's not clear what to do, so do nothing. */
11870 window_height = window_box_height (w);
11871 if (row->height >= window_height)
11872 {
11873 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11874 return 1;
11875 }
11876 return 0;
11877
11878 #if 0
11879 /* This code used to try to scroll the window just enough to make
11880 the line visible. It returned 0 to say that the caller should
11881 allocate larger glyph matrices. */
11882
11883 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11884 {
11885 int dy = row->height - row->visible_height;
11886 w->vscroll = 0;
11887 w->cursor.y += dy;
11888 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11889 }
11890 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11891 {
11892 int dy = - (row->height - row->visible_height);
11893 w->vscroll = dy;
11894 w->cursor.y += dy;
11895 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11896 }
11897
11898 /* When we change the cursor y-position of the selected window,
11899 change this_line_y as well so that the display optimization for
11900 the cursor line of the selected window in redisplay_internal uses
11901 the correct y-position. */
11902 if (w == XWINDOW (selected_window))
11903 this_line_y = w->cursor.y;
11904
11905 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11906 redisplay with larger matrices. */
11907 if (matrix->nrows < required_matrix_height (w))
11908 {
11909 fonts_changed_p = 1;
11910 return 0;
11911 }
11912
11913 return 1;
11914 #endif /* 0 */
11915 }
11916
11917
11918 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11919 non-zero means only WINDOW is redisplayed in redisplay_internal.
11920 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11921 in redisplay_window to bring a partially visible line into view in
11922 the case that only the cursor has moved.
11923
11924 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11925 last screen line's vertical height extends past the end of the screen.
11926
11927 Value is
11928
11929 1 if scrolling succeeded
11930
11931 0 if scrolling didn't find point.
11932
11933 -1 if new fonts have been loaded so that we must interrupt
11934 redisplay, adjust glyph matrices, and try again. */
11935
11936 enum
11937 {
11938 SCROLLING_SUCCESS,
11939 SCROLLING_FAILED,
11940 SCROLLING_NEED_LARGER_MATRICES
11941 };
11942
11943 static int
11944 try_scrolling (window, just_this_one_p, scroll_conservatively,
11945 scroll_step, temp_scroll_step, last_line_misfit)
11946 Lisp_Object window;
11947 int just_this_one_p;
11948 EMACS_INT scroll_conservatively, scroll_step;
11949 int temp_scroll_step;
11950 int last_line_misfit;
11951 {
11952 struct window *w = XWINDOW (window);
11953 struct frame *f = XFRAME (w->frame);
11954 struct text_pos scroll_margin_pos;
11955 struct text_pos pos;
11956 struct text_pos startp;
11957 struct it it;
11958 Lisp_Object window_end;
11959 int this_scroll_margin;
11960 int dy = 0;
11961 int scroll_max;
11962 int rc;
11963 int amount_to_scroll = 0;
11964 Lisp_Object aggressive;
11965 int height;
11966 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11967
11968 #if GLYPH_DEBUG
11969 debug_method_add (w, "try_scrolling");
11970 #endif
11971
11972 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11973
11974 /* Compute scroll margin height in pixels. We scroll when point is
11975 within this distance from the top or bottom of the window. */
11976 if (scroll_margin > 0)
11977 {
11978 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11979 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11980 }
11981 else
11982 this_scroll_margin = 0;
11983
11984 /* Force scroll_conservatively to have a reasonable value so it doesn't
11985 cause an overflow while computing how much to scroll. */
11986 if (scroll_conservatively)
11987 scroll_conservatively = min (scroll_conservatively,
11988 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11989
11990 /* Compute how much we should try to scroll maximally to bring point
11991 into view. */
11992 if (scroll_step || scroll_conservatively || temp_scroll_step)
11993 scroll_max = max (scroll_step,
11994 max (scroll_conservatively, temp_scroll_step));
11995 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11996 || NUMBERP (current_buffer->scroll_up_aggressively))
11997 /* We're trying to scroll because of aggressive scrolling
11998 but no scroll_step is set. Choose an arbitrary one. Maybe
11999 there should be a variable for this. */
12000 scroll_max = 10;
12001 else
12002 scroll_max = 0;
12003 scroll_max *= FRAME_LINE_HEIGHT (f);
12004
12005 /* Decide whether we have to scroll down. Start at the window end
12006 and move this_scroll_margin up to find the position of the scroll
12007 margin. */
12008 window_end = Fwindow_end (window, Qt);
12009
12010 too_near_end:
12011
12012 CHARPOS (scroll_margin_pos) = XINT (window_end);
12013 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12014
12015 if (this_scroll_margin || extra_scroll_margin_lines)
12016 {
12017 start_display (&it, w, scroll_margin_pos);
12018 if (this_scroll_margin)
12019 move_it_vertically_backward (&it, this_scroll_margin);
12020 if (extra_scroll_margin_lines)
12021 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12022 scroll_margin_pos = it.current.pos;
12023 }
12024
12025 if (PT >= CHARPOS (scroll_margin_pos))
12026 {
12027 int y0;
12028
12029 /* Point is in the scroll margin at the bottom of the window, or
12030 below. Compute a new window start that makes point visible. */
12031
12032 /* Compute the distance from the scroll margin to PT.
12033 Give up if the distance is greater than scroll_max. */
12034 start_display (&it, w, scroll_margin_pos);
12035 y0 = it.current_y;
12036 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12037 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12038
12039 /* To make point visible, we have to move the window start
12040 down so that the line the cursor is in is visible, which
12041 means we have to add in the height of the cursor line. */
12042 dy = line_bottom_y (&it) - y0;
12043
12044 if (dy > scroll_max)
12045 return SCROLLING_FAILED;
12046
12047 /* Move the window start down. If scrolling conservatively,
12048 move it just enough down to make point visible. If
12049 scroll_step is set, move it down by scroll_step. */
12050 start_display (&it, w, startp);
12051
12052 if (scroll_conservatively)
12053 /* Set AMOUNT_TO_SCROLL to at least one line,
12054 and at most scroll_conservatively lines. */
12055 amount_to_scroll
12056 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12057 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12058 else if (scroll_step || temp_scroll_step)
12059 amount_to_scroll = scroll_max;
12060 else
12061 {
12062 aggressive = current_buffer->scroll_up_aggressively;
12063 height = WINDOW_BOX_TEXT_HEIGHT (w);
12064 if (NUMBERP (aggressive))
12065 {
12066 double float_amount = XFLOATINT (aggressive) * height;
12067 amount_to_scroll = float_amount;
12068 if (amount_to_scroll == 0 && float_amount > 0)
12069 amount_to_scroll = 1;
12070 }
12071 }
12072
12073 if (amount_to_scroll <= 0)
12074 return SCROLLING_FAILED;
12075
12076 /* If moving by amount_to_scroll leaves STARTP unchanged,
12077 move it down one screen line. */
12078
12079 move_it_vertically (&it, amount_to_scroll);
12080 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12081 move_it_by_lines (&it, 1, 1);
12082 startp = it.current.pos;
12083 }
12084 else
12085 {
12086 /* See if point is inside the scroll margin at the top of the
12087 window. */
12088 scroll_margin_pos = startp;
12089 if (this_scroll_margin)
12090 {
12091 start_display (&it, w, startp);
12092 move_it_vertically (&it, this_scroll_margin);
12093 scroll_margin_pos = it.current.pos;
12094 }
12095
12096 if (PT < CHARPOS (scroll_margin_pos))
12097 {
12098 /* Point is in the scroll margin at the top of the window or
12099 above what is displayed in the window. */
12100 int y0;
12101
12102 /* Compute the vertical distance from PT to the scroll
12103 margin position. Give up if distance is greater than
12104 scroll_max. */
12105 SET_TEXT_POS (pos, PT, PT_BYTE);
12106 start_display (&it, w, pos);
12107 y0 = it.current_y;
12108 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12109 it.last_visible_y, -1,
12110 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12111 dy = it.current_y - y0;
12112 if (dy > scroll_max)
12113 return SCROLLING_FAILED;
12114
12115 /* Compute new window start. */
12116 start_display (&it, w, startp);
12117
12118 if (scroll_conservatively)
12119 amount_to_scroll
12120 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12121 else if (scroll_step || temp_scroll_step)
12122 amount_to_scroll = scroll_max;
12123 else
12124 {
12125 aggressive = current_buffer->scroll_down_aggressively;
12126 height = WINDOW_BOX_TEXT_HEIGHT (w);
12127 if (NUMBERP (aggressive))
12128 {
12129 double float_amount = XFLOATINT (aggressive) * height;
12130 amount_to_scroll = float_amount;
12131 if (amount_to_scroll == 0 && float_amount > 0)
12132 amount_to_scroll = 1;
12133 }
12134 }
12135
12136 if (amount_to_scroll <= 0)
12137 return SCROLLING_FAILED;
12138
12139 move_it_vertically_backward (&it, amount_to_scroll);
12140 startp = it.current.pos;
12141 }
12142 }
12143
12144 /* Run window scroll functions. */
12145 startp = run_window_scroll_functions (window, startp);
12146
12147 /* Display the window. Give up if new fonts are loaded, or if point
12148 doesn't appear. */
12149 if (!try_window (window, startp, 0))
12150 rc = SCROLLING_NEED_LARGER_MATRICES;
12151 else if (w->cursor.vpos < 0)
12152 {
12153 clear_glyph_matrix (w->desired_matrix);
12154 rc = SCROLLING_FAILED;
12155 }
12156 else
12157 {
12158 /* Maybe forget recorded base line for line number display. */
12159 if (!just_this_one_p
12160 || current_buffer->clip_changed
12161 || BEG_UNCHANGED < CHARPOS (startp))
12162 w->base_line_number = Qnil;
12163
12164 /* If cursor ends up on a partially visible line,
12165 treat that as being off the bottom of the screen. */
12166 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12167 {
12168 clear_glyph_matrix (w->desired_matrix);
12169 ++extra_scroll_margin_lines;
12170 goto too_near_end;
12171 }
12172 rc = SCROLLING_SUCCESS;
12173 }
12174
12175 return rc;
12176 }
12177
12178
12179 /* Compute a suitable window start for window W if display of W starts
12180 on a continuation line. Value is non-zero if a new window start
12181 was computed.
12182
12183 The new window start will be computed, based on W's width, starting
12184 from the start of the continued line. It is the start of the
12185 screen line with the minimum distance from the old start W->start. */
12186
12187 static int
12188 compute_window_start_on_continuation_line (w)
12189 struct window *w;
12190 {
12191 struct text_pos pos, start_pos;
12192 int window_start_changed_p = 0;
12193
12194 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12195
12196 /* If window start is on a continuation line... Window start may be
12197 < BEGV in case there's invisible text at the start of the
12198 buffer (M-x rmail, for example). */
12199 if (CHARPOS (start_pos) > BEGV
12200 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12201 {
12202 struct it it;
12203 struct glyph_row *row;
12204
12205 /* Handle the case that the window start is out of range. */
12206 if (CHARPOS (start_pos) < BEGV)
12207 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12208 else if (CHARPOS (start_pos) > ZV)
12209 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12210
12211 /* Find the start of the continued line. This should be fast
12212 because scan_buffer is fast (newline cache). */
12213 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12214 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12215 row, DEFAULT_FACE_ID);
12216 reseat_at_previous_visible_line_start (&it);
12217
12218 /* If the line start is "too far" away from the window start,
12219 say it takes too much time to compute a new window start. */
12220 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12221 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12222 {
12223 int min_distance, distance;
12224
12225 /* Move forward by display lines to find the new window
12226 start. If window width was enlarged, the new start can
12227 be expected to be > the old start. If window width was
12228 decreased, the new window start will be < the old start.
12229 So, we're looking for the display line start with the
12230 minimum distance from the old window start. */
12231 pos = it.current.pos;
12232 min_distance = INFINITY;
12233 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12234 distance < min_distance)
12235 {
12236 min_distance = distance;
12237 pos = it.current.pos;
12238 move_it_by_lines (&it, 1, 0);
12239 }
12240
12241 /* Set the window start there. */
12242 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12243 window_start_changed_p = 1;
12244 }
12245 }
12246
12247 return window_start_changed_p;
12248 }
12249
12250
12251 /* Try cursor movement in case text has not changed in window WINDOW,
12252 with window start STARTP. Value is
12253
12254 CURSOR_MOVEMENT_SUCCESS if successful
12255
12256 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12257
12258 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12259 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12260 we want to scroll as if scroll-step were set to 1. See the code.
12261
12262 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12263 which case we have to abort this redisplay, and adjust matrices
12264 first. */
12265
12266 enum
12267 {
12268 CURSOR_MOVEMENT_SUCCESS,
12269 CURSOR_MOVEMENT_CANNOT_BE_USED,
12270 CURSOR_MOVEMENT_MUST_SCROLL,
12271 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12272 };
12273
12274 static int
12275 try_cursor_movement (window, startp, scroll_step)
12276 Lisp_Object window;
12277 struct text_pos startp;
12278 int *scroll_step;
12279 {
12280 struct window *w = XWINDOW (window);
12281 struct frame *f = XFRAME (w->frame);
12282 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12283
12284 #if GLYPH_DEBUG
12285 if (inhibit_try_cursor_movement)
12286 return rc;
12287 #endif
12288
12289 /* Handle case where text has not changed, only point, and it has
12290 not moved off the frame. */
12291 if (/* Point may be in this window. */
12292 PT >= CHARPOS (startp)
12293 /* Selective display hasn't changed. */
12294 && !current_buffer->clip_changed
12295 /* Function force-mode-line-update is used to force a thorough
12296 redisplay. It sets either windows_or_buffers_changed or
12297 update_mode_lines. So don't take a shortcut here for these
12298 cases. */
12299 && !update_mode_lines
12300 && !windows_or_buffers_changed
12301 && !cursor_type_changed
12302 /* Can't use this case if highlighting a region. When a
12303 region exists, cursor movement has to do more than just
12304 set the cursor. */
12305 && !(!NILP (Vtransient_mark_mode)
12306 && !NILP (current_buffer->mark_active))
12307 && NILP (w->region_showing)
12308 && NILP (Vshow_trailing_whitespace)
12309 /* Right after splitting windows, last_point may be nil. */
12310 && INTEGERP (w->last_point)
12311 /* This code is not used for mini-buffer for the sake of the case
12312 of redisplaying to replace an echo area message; since in
12313 that case the mini-buffer contents per se are usually
12314 unchanged. This code is of no real use in the mini-buffer
12315 since the handling of this_line_start_pos, etc., in redisplay
12316 handles the same cases. */
12317 && !EQ (window, minibuf_window)
12318 /* When splitting windows or for new windows, it happens that
12319 redisplay is called with a nil window_end_vpos or one being
12320 larger than the window. This should really be fixed in
12321 window.c. I don't have this on my list, now, so we do
12322 approximately the same as the old redisplay code. --gerd. */
12323 && INTEGERP (w->window_end_vpos)
12324 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12325 && (FRAME_WINDOW_P (f)
12326 || !overlay_arrow_in_current_buffer_p ()))
12327 {
12328 int this_scroll_margin, top_scroll_margin;
12329 struct glyph_row *row = NULL;
12330
12331 #if GLYPH_DEBUG
12332 debug_method_add (w, "cursor movement");
12333 #endif
12334
12335 /* Scroll if point within this distance from the top or bottom
12336 of the window. This is a pixel value. */
12337 this_scroll_margin = max (0, scroll_margin);
12338 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12339 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12340
12341 top_scroll_margin = this_scroll_margin;
12342 if (WINDOW_WANTS_HEADER_LINE_P (w))
12343 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12344
12345 /* Start with the row the cursor was displayed during the last
12346 not paused redisplay. Give up if that row is not valid. */
12347 if (w->last_cursor.vpos < 0
12348 || w->last_cursor.vpos >= w->current_matrix->nrows)
12349 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12350 else
12351 {
12352 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12353 if (row->mode_line_p)
12354 ++row;
12355 if (!row->enabled_p)
12356 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12357 }
12358
12359 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12360 {
12361 int scroll_p = 0;
12362 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12363
12364 if (PT > XFASTINT (w->last_point))
12365 {
12366 /* Point has moved forward. */
12367 while (MATRIX_ROW_END_CHARPOS (row) < PT
12368 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12369 {
12370 xassert (row->enabled_p);
12371 ++row;
12372 }
12373
12374 /* The end position of a row equals the start position
12375 of the next row. If PT is there, we would rather
12376 display it in the next line. */
12377 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12378 && MATRIX_ROW_END_CHARPOS (row) == PT
12379 && !cursor_row_p (w, row))
12380 ++row;
12381
12382 /* If within the scroll margin, scroll. Note that
12383 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12384 the next line would be drawn, and that
12385 this_scroll_margin can be zero. */
12386 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12387 || PT > MATRIX_ROW_END_CHARPOS (row)
12388 /* Line is completely visible last line in window
12389 and PT is to be set in the next line. */
12390 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12391 && PT == MATRIX_ROW_END_CHARPOS (row)
12392 && !row->ends_at_zv_p
12393 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12394 scroll_p = 1;
12395 }
12396 else if (PT < XFASTINT (w->last_point))
12397 {
12398 /* Cursor has to be moved backward. Note that PT >=
12399 CHARPOS (startp) because of the outer if-statement. */
12400 while (!row->mode_line_p
12401 && (MATRIX_ROW_START_CHARPOS (row) > PT
12402 || (MATRIX_ROW_START_CHARPOS (row) == PT
12403 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12404 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12405 row > w->current_matrix->rows
12406 && (row-1)->ends_in_newline_from_string_p))))
12407 && (row->y > top_scroll_margin
12408 || CHARPOS (startp) == BEGV))
12409 {
12410 xassert (row->enabled_p);
12411 --row;
12412 }
12413
12414 /* Consider the following case: Window starts at BEGV,
12415 there is invisible, intangible text at BEGV, so that
12416 display starts at some point START > BEGV. It can
12417 happen that we are called with PT somewhere between
12418 BEGV and START. Try to handle that case. */
12419 if (row < w->current_matrix->rows
12420 || row->mode_line_p)
12421 {
12422 row = w->current_matrix->rows;
12423 if (row->mode_line_p)
12424 ++row;
12425 }
12426
12427 /* Due to newlines in overlay strings, we may have to
12428 skip forward over overlay strings. */
12429 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12430 && MATRIX_ROW_END_CHARPOS (row) == PT
12431 && !cursor_row_p (w, row))
12432 ++row;
12433
12434 /* If within the scroll margin, scroll. */
12435 if (row->y < top_scroll_margin
12436 && CHARPOS (startp) != BEGV)
12437 scroll_p = 1;
12438 }
12439 else
12440 {
12441 /* Cursor did not move. So don't scroll even if cursor line
12442 is partially visible, as it was so before. */
12443 rc = CURSOR_MOVEMENT_SUCCESS;
12444 }
12445
12446 if (PT < MATRIX_ROW_START_CHARPOS (row)
12447 || PT > MATRIX_ROW_END_CHARPOS (row))
12448 {
12449 /* if PT is not in the glyph row, give up. */
12450 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12451 }
12452 else if (rc != CURSOR_MOVEMENT_SUCCESS
12453 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12454 && make_cursor_line_fully_visible_p)
12455 {
12456 if (PT == MATRIX_ROW_END_CHARPOS (row)
12457 && !row->ends_at_zv_p
12458 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12459 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12460 else if (row->height > window_box_height (w))
12461 {
12462 /* If we end up in a partially visible line, let's
12463 make it fully visible, except when it's taller
12464 than the window, in which case we can't do much
12465 about it. */
12466 *scroll_step = 1;
12467 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12468 }
12469 else
12470 {
12471 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12472 if (!cursor_row_fully_visible_p (w, 0, 1))
12473 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12474 else
12475 rc = CURSOR_MOVEMENT_SUCCESS;
12476 }
12477 }
12478 else if (scroll_p)
12479 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12480 else
12481 {
12482 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12483 rc = CURSOR_MOVEMENT_SUCCESS;
12484 }
12485 }
12486 }
12487
12488 return rc;
12489 }
12490
12491 void
12492 set_vertical_scroll_bar (w)
12493 struct window *w;
12494 {
12495 int start, end, whole;
12496
12497 /* Calculate the start and end positions for the current window.
12498 At some point, it would be nice to choose between scrollbars
12499 which reflect the whole buffer size, with special markers
12500 indicating narrowing, and scrollbars which reflect only the
12501 visible region.
12502
12503 Note that mini-buffers sometimes aren't displaying any text. */
12504 if (!MINI_WINDOW_P (w)
12505 || (w == XWINDOW (minibuf_window)
12506 && NILP (echo_area_buffer[0])))
12507 {
12508 struct buffer *buf = XBUFFER (w->buffer);
12509 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12510 start = marker_position (w->start) - BUF_BEGV (buf);
12511 /* I don't think this is guaranteed to be right. For the
12512 moment, we'll pretend it is. */
12513 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12514
12515 if (end < start)
12516 end = start;
12517 if (whole < (end - start))
12518 whole = end - start;
12519 }
12520 else
12521 start = end = whole = 0;
12522
12523 /* Indicate what this scroll bar ought to be displaying now. */
12524 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12525 }
12526
12527
12528 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12529 selected_window is redisplayed.
12530
12531 We can return without actually redisplaying the window if
12532 fonts_changed_p is nonzero. In that case, redisplay_internal will
12533 retry. */
12534
12535 static void
12536 redisplay_window (window, just_this_one_p)
12537 Lisp_Object window;
12538 int just_this_one_p;
12539 {
12540 struct window *w = XWINDOW (window);
12541 struct frame *f = XFRAME (w->frame);
12542 struct buffer *buffer = XBUFFER (w->buffer);
12543 struct buffer *old = current_buffer;
12544 struct text_pos lpoint, opoint, startp;
12545 int update_mode_line;
12546 int tem;
12547 struct it it;
12548 /* Record it now because it's overwritten. */
12549 int current_matrix_up_to_date_p = 0;
12550 int used_current_matrix_p = 0;
12551 /* This is less strict than current_matrix_up_to_date_p.
12552 It indictes that the buffer contents and narrowing are unchanged. */
12553 int buffer_unchanged_p = 0;
12554 int temp_scroll_step = 0;
12555 int count = SPECPDL_INDEX ();
12556 int rc;
12557 int centering_position = -1;
12558 int last_line_misfit = 0;
12559
12560 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12561 opoint = lpoint;
12562
12563 /* W must be a leaf window here. */
12564 xassert (!NILP (w->buffer));
12565 #if GLYPH_DEBUG
12566 *w->desired_matrix->method = 0;
12567 #endif
12568
12569 specbind (Qinhibit_point_motion_hooks, Qt);
12570
12571 reconsider_clip_changes (w, buffer);
12572
12573 /* Has the mode line to be updated? */
12574 update_mode_line = (!NILP (w->update_mode_line)
12575 || update_mode_lines
12576 || buffer->clip_changed
12577 || buffer->prevent_redisplay_optimizations_p);
12578
12579 if (MINI_WINDOW_P (w))
12580 {
12581 if (w == XWINDOW (echo_area_window)
12582 && !NILP (echo_area_buffer[0]))
12583 {
12584 if (update_mode_line)
12585 /* We may have to update a tty frame's menu bar or a
12586 tool-bar. Example `M-x C-h C-h C-g'. */
12587 goto finish_menu_bars;
12588 else
12589 /* We've already displayed the echo area glyphs in this window. */
12590 goto finish_scroll_bars;
12591 }
12592 else if ((w != XWINDOW (minibuf_window)
12593 || minibuf_level == 0)
12594 /* When buffer is nonempty, redisplay window normally. */
12595 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12596 /* Quail displays non-mini buffers in minibuffer window.
12597 In that case, redisplay the window normally. */
12598 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12599 {
12600 /* W is a mini-buffer window, but it's not active, so clear
12601 it. */
12602 int yb = window_text_bottom_y (w);
12603 struct glyph_row *row;
12604 int y;
12605
12606 for (y = 0, row = w->desired_matrix->rows;
12607 y < yb;
12608 y += row->height, ++row)
12609 blank_row (w, row, y);
12610 goto finish_scroll_bars;
12611 }
12612
12613 clear_glyph_matrix (w->desired_matrix);
12614 }
12615
12616 /* Otherwise set up data on this window; select its buffer and point
12617 value. */
12618 /* Really select the buffer, for the sake of buffer-local
12619 variables. */
12620 set_buffer_internal_1 (XBUFFER (w->buffer));
12621 SET_TEXT_POS (opoint, PT, PT_BYTE);
12622
12623 current_matrix_up_to_date_p
12624 = (!NILP (w->window_end_valid)
12625 && !current_buffer->clip_changed
12626 && !current_buffer->prevent_redisplay_optimizations_p
12627 && XFASTINT (w->last_modified) >= MODIFF
12628 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12629
12630 buffer_unchanged_p
12631 = (!NILP (w->window_end_valid)
12632 && !current_buffer->clip_changed
12633 && XFASTINT (w->last_modified) >= MODIFF
12634 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12635
12636 /* When windows_or_buffers_changed is non-zero, we can't rely on
12637 the window end being valid, so set it to nil there. */
12638 if (windows_or_buffers_changed)
12639 {
12640 /* If window starts on a continuation line, maybe adjust the
12641 window start in case the window's width changed. */
12642 if (XMARKER (w->start)->buffer == current_buffer)
12643 compute_window_start_on_continuation_line (w);
12644
12645 w->window_end_valid = Qnil;
12646 }
12647
12648 /* Some sanity checks. */
12649 CHECK_WINDOW_END (w);
12650 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12651 abort ();
12652 if (BYTEPOS (opoint) < CHARPOS (opoint))
12653 abort ();
12654
12655 /* If %c is in mode line, update it if needed. */
12656 if (!NILP (w->column_number_displayed)
12657 /* This alternative quickly identifies a common case
12658 where no change is needed. */
12659 && !(PT == XFASTINT (w->last_point)
12660 && XFASTINT (w->last_modified) >= MODIFF
12661 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12662 && (XFASTINT (w->column_number_displayed)
12663 != (int) current_column ())) /* iftc */
12664 update_mode_line = 1;
12665
12666 /* Count number of windows showing the selected buffer. An indirect
12667 buffer counts as its base buffer. */
12668 if (!just_this_one_p)
12669 {
12670 struct buffer *current_base, *window_base;
12671 current_base = current_buffer;
12672 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12673 if (current_base->base_buffer)
12674 current_base = current_base->base_buffer;
12675 if (window_base->base_buffer)
12676 window_base = window_base->base_buffer;
12677 if (current_base == window_base)
12678 buffer_shared++;
12679 }
12680
12681 /* Point refers normally to the selected window. For any other
12682 window, set up appropriate value. */
12683 if (!EQ (window, selected_window))
12684 {
12685 int new_pt = XMARKER (w->pointm)->charpos;
12686 int new_pt_byte = marker_byte_position (w->pointm);
12687 if (new_pt < BEGV)
12688 {
12689 new_pt = BEGV;
12690 new_pt_byte = BEGV_BYTE;
12691 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12692 }
12693 else if (new_pt > (ZV - 1))
12694 {
12695 new_pt = ZV;
12696 new_pt_byte = ZV_BYTE;
12697 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12698 }
12699
12700 /* We don't use SET_PT so that the point-motion hooks don't run. */
12701 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12702 }
12703
12704 /* If any of the character widths specified in the display table
12705 have changed, invalidate the width run cache. It's true that
12706 this may be a bit late to catch such changes, but the rest of
12707 redisplay goes (non-fatally) haywire when the display table is
12708 changed, so why should we worry about doing any better? */
12709 if (current_buffer->width_run_cache)
12710 {
12711 struct Lisp_Char_Table *disptab = buffer_display_table ();
12712
12713 if (! disptab_matches_widthtab (disptab,
12714 XVECTOR (current_buffer->width_table)))
12715 {
12716 invalidate_region_cache (current_buffer,
12717 current_buffer->width_run_cache,
12718 BEG, Z);
12719 recompute_width_table (current_buffer, disptab);
12720 }
12721 }
12722
12723 /* If window-start is screwed up, choose a new one. */
12724 if (XMARKER (w->start)->buffer != current_buffer)
12725 goto recenter;
12726
12727 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12728
12729 /* If someone specified a new starting point but did not insist,
12730 check whether it can be used. */
12731 if (!NILP (w->optional_new_start)
12732 && CHARPOS (startp) >= BEGV
12733 && CHARPOS (startp) <= ZV)
12734 {
12735 w->optional_new_start = Qnil;
12736 start_display (&it, w, startp);
12737 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12738 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12739 if (IT_CHARPOS (it) == PT)
12740 w->force_start = Qt;
12741 /* IT may overshoot PT if text at PT is invisible. */
12742 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12743 w->force_start = Qt;
12744 }
12745
12746 /* Handle case where place to start displaying has been specified,
12747 unless the specified location is outside the accessible range. */
12748 if (!NILP (w->force_start)
12749 || w->frozen_window_start_p)
12750 {
12751 /* We set this later on if we have to adjust point. */
12752 int new_vpos = -1;
12753 int val;
12754
12755 w->force_start = Qnil;
12756 w->vscroll = 0;
12757 w->window_end_valid = Qnil;
12758
12759 /* Forget any recorded base line for line number display. */
12760 if (!buffer_unchanged_p)
12761 w->base_line_number = Qnil;
12762
12763 /* Redisplay the mode line. Select the buffer properly for that.
12764 Also, run the hook window-scroll-functions
12765 because we have scrolled. */
12766 /* Note, we do this after clearing force_start because
12767 if there's an error, it is better to forget about force_start
12768 than to get into an infinite loop calling the hook functions
12769 and having them get more errors. */
12770 if (!update_mode_line
12771 || ! NILP (Vwindow_scroll_functions))
12772 {
12773 update_mode_line = 1;
12774 w->update_mode_line = Qt;
12775 startp = run_window_scroll_functions (window, startp);
12776 }
12777
12778 w->last_modified = make_number (0);
12779 w->last_overlay_modified = make_number (0);
12780 if (CHARPOS (startp) < BEGV)
12781 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12782 else if (CHARPOS (startp) > ZV)
12783 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12784
12785 /* Redisplay, then check if cursor has been set during the
12786 redisplay. Give up if new fonts were loaded. */
12787 val = try_window (window, startp, 1);
12788 if (!val)
12789 {
12790 w->force_start = Qt;
12791 clear_glyph_matrix (w->desired_matrix);
12792 goto need_larger_matrices;
12793 }
12794 /* Point was outside the scroll margins. */
12795 if (val < 0)
12796 new_vpos = window_box_height (w) / 2;
12797
12798 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12799 {
12800 /* If point does not appear, try to move point so it does
12801 appear. The desired matrix has been built above, so we
12802 can use it here. */
12803 new_vpos = window_box_height (w) / 2;
12804 }
12805
12806 if (!cursor_row_fully_visible_p (w, 0, 0))
12807 {
12808 /* Point does appear, but on a line partly visible at end of window.
12809 Move it back to a fully-visible line. */
12810 new_vpos = window_box_height (w);
12811 }
12812
12813 /* If we need to move point for either of the above reasons,
12814 now actually do it. */
12815 if (new_vpos >= 0)
12816 {
12817 struct glyph_row *row;
12818
12819 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12820 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12821 ++row;
12822
12823 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12824 MATRIX_ROW_START_BYTEPOS (row));
12825
12826 if (w != XWINDOW (selected_window))
12827 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12828 else if (current_buffer == old)
12829 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12830
12831 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12832
12833 /* If we are highlighting the region, then we just changed
12834 the region, so redisplay to show it. */
12835 if (!NILP (Vtransient_mark_mode)
12836 && !NILP (current_buffer->mark_active))
12837 {
12838 clear_glyph_matrix (w->desired_matrix);
12839 if (!try_window (window, startp, 0))
12840 goto need_larger_matrices;
12841 }
12842 }
12843
12844 #if GLYPH_DEBUG
12845 debug_method_add (w, "forced window start");
12846 #endif
12847 goto done;
12848 }
12849
12850 /* Handle case where text has not changed, only point, and it has
12851 not moved off the frame, and we are not retrying after hscroll.
12852 (current_matrix_up_to_date_p is nonzero when retrying.) */
12853 if (current_matrix_up_to_date_p
12854 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12855 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12856 {
12857 switch (rc)
12858 {
12859 case CURSOR_MOVEMENT_SUCCESS:
12860 used_current_matrix_p = 1;
12861 goto done;
12862
12863 #if 0 /* try_cursor_movement never returns this value. */
12864 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12865 goto need_larger_matrices;
12866 #endif
12867
12868 case CURSOR_MOVEMENT_MUST_SCROLL:
12869 goto try_to_scroll;
12870
12871 default:
12872 abort ();
12873 }
12874 }
12875 /* If current starting point was originally the beginning of a line
12876 but no longer is, find a new starting point. */
12877 else if (!NILP (w->start_at_line_beg)
12878 && !(CHARPOS (startp) <= BEGV
12879 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12880 {
12881 #if GLYPH_DEBUG
12882 debug_method_add (w, "recenter 1");
12883 #endif
12884 goto recenter;
12885 }
12886
12887 /* Try scrolling with try_window_id. Value is > 0 if update has
12888 been done, it is -1 if we know that the same window start will
12889 not work. It is 0 if unsuccessful for some other reason. */
12890 else if ((tem = try_window_id (w)) != 0)
12891 {
12892 #if GLYPH_DEBUG
12893 debug_method_add (w, "try_window_id %d", tem);
12894 #endif
12895
12896 if (fonts_changed_p)
12897 goto need_larger_matrices;
12898 if (tem > 0)
12899 goto done;
12900
12901 /* Otherwise try_window_id has returned -1 which means that we
12902 don't want the alternative below this comment to execute. */
12903 }
12904 else if (CHARPOS (startp) >= BEGV
12905 && CHARPOS (startp) <= ZV
12906 && PT >= CHARPOS (startp)
12907 && (CHARPOS (startp) < ZV
12908 /* Avoid starting at end of buffer. */
12909 || CHARPOS (startp) == BEGV
12910 || (XFASTINT (w->last_modified) >= MODIFF
12911 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12912 {
12913
12914 /* If first window line is a continuation line, and window start
12915 is inside the modified region, but the first change is before
12916 current window start, we must select a new window start.*/
12917 if (NILP (w->start_at_line_beg)
12918 && CHARPOS (startp) > BEGV)
12919 {
12920 /* Make sure beg_unchanged and end_unchanged are up to date.
12921 Do it only if buffer has really changed. This may or may
12922 not have been done by try_window_id (see which) already. */
12923 if (MODIFF > SAVE_MODIFF
12924 /* This seems to happen sometimes after saving a buffer. */
12925 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12926 {
12927 if (GPT - BEG < BEG_UNCHANGED)
12928 BEG_UNCHANGED = GPT - BEG;
12929 if (Z - GPT < END_UNCHANGED)
12930 END_UNCHANGED = Z - GPT;
12931 }
12932
12933 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
12934 && CHARPOS (startp) <= Z - END_UNCHANGED)
12935 {
12936 /* There doesn't seems to be a simple way to find a new
12937 window start that is near the old window start, so
12938 we just recenter. */
12939 goto recenter;
12940 }
12941 }
12942
12943 #if GLYPH_DEBUG
12944 debug_method_add (w, "same window start");
12945 #endif
12946
12947 /* Try to redisplay starting at same place as before.
12948 If point has not moved off frame, accept the results. */
12949 if (!current_matrix_up_to_date_p
12950 /* Don't use try_window_reusing_current_matrix in this case
12951 because a window scroll function can have changed the
12952 buffer. */
12953 || !NILP (Vwindow_scroll_functions)
12954 || MINI_WINDOW_P (w)
12955 || !(used_current_matrix_p
12956 = try_window_reusing_current_matrix (w)))
12957 {
12958 IF_DEBUG (debug_method_add (w, "1"));
12959 if (try_window (window, startp, 1) < 0)
12960 /* -1 means we need to scroll.
12961 0 means we need new matrices, but fonts_changed_p
12962 is set in that case, so we will detect it below. */
12963 goto try_to_scroll;
12964 }
12965
12966 if (fonts_changed_p)
12967 goto need_larger_matrices;
12968
12969 if (w->cursor.vpos >= 0)
12970 {
12971 if (!just_this_one_p
12972 || current_buffer->clip_changed
12973 || BEG_UNCHANGED < CHARPOS (startp))
12974 /* Forget any recorded base line for line number display. */
12975 w->base_line_number = Qnil;
12976
12977 if (!cursor_row_fully_visible_p (w, 1, 0))
12978 {
12979 clear_glyph_matrix (w->desired_matrix);
12980 last_line_misfit = 1;
12981 }
12982 /* Drop through and scroll. */
12983 else
12984 goto done;
12985 }
12986 else
12987 clear_glyph_matrix (w->desired_matrix);
12988 }
12989
12990 try_to_scroll:
12991
12992 w->last_modified = make_number (0);
12993 w->last_overlay_modified = make_number (0);
12994
12995 /* Redisplay the mode line. Select the buffer properly for that. */
12996 if (!update_mode_line)
12997 {
12998 update_mode_line = 1;
12999 w->update_mode_line = Qt;
13000 }
13001
13002 /* Try to scroll by specified few lines. */
13003 if ((scroll_conservatively
13004 || scroll_step
13005 || temp_scroll_step
13006 || NUMBERP (current_buffer->scroll_up_aggressively)
13007 || NUMBERP (current_buffer->scroll_down_aggressively))
13008 && !current_buffer->clip_changed
13009 && CHARPOS (startp) >= BEGV
13010 && CHARPOS (startp) <= ZV)
13011 {
13012 /* The function returns -1 if new fonts were loaded, 1 if
13013 successful, 0 if not successful. */
13014 int rc = try_scrolling (window, just_this_one_p,
13015 scroll_conservatively,
13016 scroll_step,
13017 temp_scroll_step, last_line_misfit);
13018 switch (rc)
13019 {
13020 case SCROLLING_SUCCESS:
13021 goto done;
13022
13023 case SCROLLING_NEED_LARGER_MATRICES:
13024 goto need_larger_matrices;
13025
13026 case SCROLLING_FAILED:
13027 break;
13028
13029 default:
13030 abort ();
13031 }
13032 }
13033
13034 /* Finally, just choose place to start which centers point */
13035
13036 recenter:
13037 if (centering_position < 0)
13038 centering_position = window_box_height (w) / 2;
13039
13040 #if GLYPH_DEBUG
13041 debug_method_add (w, "recenter");
13042 #endif
13043
13044 /* w->vscroll = 0; */
13045
13046 /* Forget any previously recorded base line for line number display. */
13047 if (!buffer_unchanged_p)
13048 w->base_line_number = Qnil;
13049
13050 /* Move backward half the height of the window. */
13051 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13052 it.current_y = it.last_visible_y;
13053 move_it_vertically_backward (&it, centering_position);
13054 xassert (IT_CHARPOS (it) >= BEGV);
13055
13056 /* The function move_it_vertically_backward may move over more
13057 than the specified y-distance. If it->w is small, e.g. a
13058 mini-buffer window, we may end up in front of the window's
13059 display area. Start displaying at the start of the line
13060 containing PT in this case. */
13061 if (it.current_y <= 0)
13062 {
13063 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13064 move_it_vertically_backward (&it, 0);
13065 #if 0
13066 /* I think this assert is bogus if buffer contains
13067 invisible text or images. KFS. */
13068 xassert (IT_CHARPOS (it) <= PT);
13069 #endif
13070 it.current_y = 0;
13071 }
13072
13073 it.current_x = it.hpos = 0;
13074
13075 /* Set startp here explicitly in case that helps avoid an infinite loop
13076 in case the window-scroll-functions functions get errors. */
13077 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13078
13079 /* Run scroll hooks. */
13080 startp = run_window_scroll_functions (window, it.current.pos);
13081
13082 /* Redisplay the window. */
13083 if (!current_matrix_up_to_date_p
13084 || windows_or_buffers_changed
13085 || cursor_type_changed
13086 /* Don't use try_window_reusing_current_matrix in this case
13087 because it can have changed the buffer. */
13088 || !NILP (Vwindow_scroll_functions)
13089 || !just_this_one_p
13090 || MINI_WINDOW_P (w)
13091 || !(used_current_matrix_p
13092 = try_window_reusing_current_matrix (w)))
13093 try_window (window, startp, 0);
13094
13095 /* If new fonts have been loaded (due to fontsets), give up. We
13096 have to start a new redisplay since we need to re-adjust glyph
13097 matrices. */
13098 if (fonts_changed_p)
13099 goto need_larger_matrices;
13100
13101 /* If cursor did not appear assume that the middle of the window is
13102 in the first line of the window. Do it again with the next line.
13103 (Imagine a window of height 100, displaying two lines of height
13104 60. Moving back 50 from it->last_visible_y will end in the first
13105 line.) */
13106 if (w->cursor.vpos < 0)
13107 {
13108 if (!NILP (w->window_end_valid)
13109 && PT >= Z - XFASTINT (w->window_end_pos))
13110 {
13111 clear_glyph_matrix (w->desired_matrix);
13112 move_it_by_lines (&it, 1, 0);
13113 try_window (window, it.current.pos, 0);
13114 }
13115 else if (PT < IT_CHARPOS (it))
13116 {
13117 clear_glyph_matrix (w->desired_matrix);
13118 move_it_by_lines (&it, -1, 0);
13119 try_window (window, it.current.pos, 0);
13120 }
13121 else
13122 {
13123 /* Not much we can do about it. */
13124 }
13125 }
13126
13127 /* Consider the following case: Window starts at BEGV, there is
13128 invisible, intangible text at BEGV, so that display starts at
13129 some point START > BEGV. It can happen that we are called with
13130 PT somewhere between BEGV and START. Try to handle that case. */
13131 if (w->cursor.vpos < 0)
13132 {
13133 struct glyph_row *row = w->current_matrix->rows;
13134 if (row->mode_line_p)
13135 ++row;
13136 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13137 }
13138
13139 if (!cursor_row_fully_visible_p (w, 0, 0))
13140 {
13141 /* If vscroll is enabled, disable it and try again. */
13142 if (w->vscroll)
13143 {
13144 w->vscroll = 0;
13145 clear_glyph_matrix (w->desired_matrix);
13146 goto recenter;
13147 }
13148
13149 /* If centering point failed to make the whole line visible,
13150 put point at the top instead. That has to make the whole line
13151 visible, if it can be done. */
13152 if (centering_position == 0)
13153 goto done;
13154
13155 clear_glyph_matrix (w->desired_matrix);
13156 centering_position = 0;
13157 goto recenter;
13158 }
13159
13160 done:
13161
13162 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13163 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13164 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13165 ? Qt : Qnil);
13166
13167 /* Display the mode line, if we must. */
13168 if ((update_mode_line
13169 /* If window not full width, must redo its mode line
13170 if (a) the window to its side is being redone and
13171 (b) we do a frame-based redisplay. This is a consequence
13172 of how inverted lines are drawn in frame-based redisplay. */
13173 || (!just_this_one_p
13174 && !FRAME_WINDOW_P (f)
13175 && !WINDOW_FULL_WIDTH_P (w))
13176 /* Line number to display. */
13177 || INTEGERP (w->base_line_pos)
13178 /* Column number is displayed and different from the one displayed. */
13179 || (!NILP (w->column_number_displayed)
13180 && (XFASTINT (w->column_number_displayed)
13181 != (int) current_column ()))) /* iftc */
13182 /* This means that the window has a mode line. */
13183 && (WINDOW_WANTS_MODELINE_P (w)
13184 || WINDOW_WANTS_HEADER_LINE_P (w)))
13185 {
13186 display_mode_lines (w);
13187
13188 /* If mode line height has changed, arrange for a thorough
13189 immediate redisplay using the correct mode line height. */
13190 if (WINDOW_WANTS_MODELINE_P (w)
13191 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13192 {
13193 fonts_changed_p = 1;
13194 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13195 = DESIRED_MODE_LINE_HEIGHT (w);
13196 }
13197
13198 /* If top line height has changed, arrange for a thorough
13199 immediate redisplay using the correct mode line height. */
13200 if (WINDOW_WANTS_HEADER_LINE_P (w)
13201 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13202 {
13203 fonts_changed_p = 1;
13204 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13205 = DESIRED_HEADER_LINE_HEIGHT (w);
13206 }
13207
13208 if (fonts_changed_p)
13209 goto need_larger_matrices;
13210 }
13211
13212 if (!line_number_displayed
13213 && !BUFFERP (w->base_line_pos))
13214 {
13215 w->base_line_pos = Qnil;
13216 w->base_line_number = Qnil;
13217 }
13218
13219 finish_menu_bars:
13220
13221 /* When we reach a frame's selected window, redo the frame's menu bar. */
13222 if (update_mode_line
13223 && EQ (FRAME_SELECTED_WINDOW (f), window))
13224 {
13225 int redisplay_menu_p = 0;
13226 int redisplay_tool_bar_p = 0;
13227
13228 if (FRAME_WINDOW_P (f))
13229 {
13230 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13231 || defined (USE_GTK)
13232 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13233 #else
13234 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13235 #endif
13236 }
13237 else
13238 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13239
13240 if (redisplay_menu_p)
13241 display_menu_bar (w);
13242
13243 #ifdef HAVE_WINDOW_SYSTEM
13244 #ifdef USE_GTK
13245 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13246 #else
13247 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13248 && (FRAME_TOOL_BAR_LINES (f) > 0
13249 || auto_resize_tool_bars_p);
13250
13251 #endif
13252
13253 if (redisplay_tool_bar_p)
13254 redisplay_tool_bar (f);
13255 #endif
13256 }
13257
13258 #ifdef HAVE_WINDOW_SYSTEM
13259 if (FRAME_WINDOW_P (f)
13260 && update_window_fringes (w, (just_this_one_p
13261 || (!used_current_matrix_p && !overlay_arrow_seen)
13262 || w->pseudo_window_p)))
13263 {
13264 update_begin (f);
13265 BLOCK_INPUT;
13266 if (draw_window_fringes (w, 1))
13267 x_draw_vertical_border (w);
13268 UNBLOCK_INPUT;
13269 update_end (f);
13270 }
13271 #endif /* HAVE_WINDOW_SYSTEM */
13272
13273 /* We go to this label, with fonts_changed_p nonzero,
13274 if it is necessary to try again using larger glyph matrices.
13275 We have to redeem the scroll bar even in this case,
13276 because the loop in redisplay_internal expects that. */
13277 need_larger_matrices:
13278 ;
13279 finish_scroll_bars:
13280
13281 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13282 {
13283 /* Set the thumb's position and size. */
13284 set_vertical_scroll_bar (w);
13285
13286 /* Note that we actually used the scroll bar attached to this
13287 window, so it shouldn't be deleted at the end of redisplay. */
13288 redeem_scroll_bar_hook (w);
13289 }
13290
13291 /* Restore current_buffer and value of point in it. */
13292 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13293 set_buffer_internal_1 (old);
13294 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13295
13296 unbind_to (count, Qnil);
13297 }
13298
13299
13300 /* Build the complete desired matrix of WINDOW with a window start
13301 buffer position POS.
13302
13303 Value is 1 if successful. It is zero if fonts were loaded during
13304 redisplay which makes re-adjusting glyph matrices necessary, and -1
13305 if point would appear in the scroll margins.
13306 (We check that only if CHECK_MARGINS is nonzero. */
13307
13308 int
13309 try_window (window, pos, check_margins)
13310 Lisp_Object window;
13311 struct text_pos pos;
13312 int check_margins;
13313 {
13314 struct window *w = XWINDOW (window);
13315 struct it it;
13316 struct glyph_row *last_text_row = NULL;
13317
13318 /* Make POS the new window start. */
13319 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13320
13321 /* Mark cursor position as unknown. No overlay arrow seen. */
13322 w->cursor.vpos = -1;
13323 overlay_arrow_seen = 0;
13324
13325 /* Initialize iterator and info to start at POS. */
13326 start_display (&it, w, pos);
13327
13328 /* Display all lines of W. */
13329 while (it.current_y < it.last_visible_y)
13330 {
13331 if (display_line (&it))
13332 last_text_row = it.glyph_row - 1;
13333 if (fonts_changed_p)
13334 return 0;
13335 }
13336
13337 /* Don't let the cursor end in the scroll margins. */
13338 if (check_margins
13339 && !MINI_WINDOW_P (w))
13340 {
13341 int this_scroll_margin;
13342
13343 this_scroll_margin = max (0, scroll_margin);
13344 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13345 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13346
13347 if ((w->cursor.y < this_scroll_margin
13348 && CHARPOS (pos) > BEGV
13349 && IT_CHARPOS (it) < ZV)
13350 /* rms: considering make_cursor_line_fully_visible_p here
13351 seems to give wrong results. We don't want to recenter
13352 when the last line is partly visible, we want to allow
13353 that case to be handled in the usual way. */
13354 || (w->cursor.y + 1) > it.last_visible_y)
13355 {
13356 w->cursor.vpos = -1;
13357 clear_glyph_matrix (w->desired_matrix);
13358 return -1;
13359 }
13360 }
13361
13362 /* If bottom moved off end of frame, change mode line percentage. */
13363 if (XFASTINT (w->window_end_pos) <= 0
13364 && Z != IT_CHARPOS (it))
13365 w->update_mode_line = Qt;
13366
13367 /* Set window_end_pos to the offset of the last character displayed
13368 on the window from the end of current_buffer. Set
13369 window_end_vpos to its row number. */
13370 if (last_text_row)
13371 {
13372 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13373 w->window_end_bytepos
13374 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13375 w->window_end_pos
13376 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13377 w->window_end_vpos
13378 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13379 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13380 ->displays_text_p);
13381 }
13382 else
13383 {
13384 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13385 w->window_end_pos = make_number (Z - ZV);
13386 w->window_end_vpos = make_number (0);
13387 }
13388
13389 /* But that is not valid info until redisplay finishes. */
13390 w->window_end_valid = Qnil;
13391 return 1;
13392 }
13393
13394
13395 \f
13396 /************************************************************************
13397 Window redisplay reusing current matrix when buffer has not changed
13398 ************************************************************************/
13399
13400 /* Try redisplay of window W showing an unchanged buffer with a
13401 different window start than the last time it was displayed by
13402 reusing its current matrix. Value is non-zero if successful.
13403 W->start is the new window start. */
13404
13405 static int
13406 try_window_reusing_current_matrix (w)
13407 struct window *w;
13408 {
13409 struct frame *f = XFRAME (w->frame);
13410 struct glyph_row *row, *bottom_row;
13411 struct it it;
13412 struct run run;
13413 struct text_pos start, new_start;
13414 int nrows_scrolled, i;
13415 struct glyph_row *last_text_row;
13416 struct glyph_row *last_reused_text_row;
13417 struct glyph_row *start_row;
13418 int start_vpos, min_y, max_y;
13419
13420 #if GLYPH_DEBUG
13421 if (inhibit_try_window_reusing)
13422 return 0;
13423 #endif
13424
13425 if (/* This function doesn't handle terminal frames. */
13426 !FRAME_WINDOW_P (f)
13427 /* Don't try to reuse the display if windows have been split
13428 or such. */
13429 || windows_or_buffers_changed
13430 || cursor_type_changed)
13431 return 0;
13432
13433 /* Can't do this if region may have changed. */
13434 if ((!NILP (Vtransient_mark_mode)
13435 && !NILP (current_buffer->mark_active))
13436 || !NILP (w->region_showing)
13437 || !NILP (Vshow_trailing_whitespace))
13438 return 0;
13439
13440 /* If top-line visibility has changed, give up. */
13441 if (WINDOW_WANTS_HEADER_LINE_P (w)
13442 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13443 return 0;
13444
13445 /* Give up if old or new display is scrolled vertically. We could
13446 make this function handle this, but right now it doesn't. */
13447 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13448 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13449 return 0;
13450
13451 /* The variable new_start now holds the new window start. The old
13452 start `start' can be determined from the current matrix. */
13453 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13454 start = start_row->start.pos;
13455 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13456
13457 /* Clear the desired matrix for the display below. */
13458 clear_glyph_matrix (w->desired_matrix);
13459
13460 if (CHARPOS (new_start) <= CHARPOS (start))
13461 {
13462 int first_row_y;
13463
13464 /* Don't use this method if the display starts with an ellipsis
13465 displayed for invisible text. It's not easy to handle that case
13466 below, and it's certainly not worth the effort since this is
13467 not a frequent case. */
13468 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13469 return 0;
13470
13471 IF_DEBUG (debug_method_add (w, "twu1"));
13472
13473 /* Display up to a row that can be reused. The variable
13474 last_text_row is set to the last row displayed that displays
13475 text. Note that it.vpos == 0 if or if not there is a
13476 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13477 start_display (&it, w, new_start);
13478 first_row_y = it.current_y;
13479 w->cursor.vpos = -1;
13480 last_text_row = last_reused_text_row = NULL;
13481
13482 while (it.current_y < it.last_visible_y
13483 && !fonts_changed_p)
13484 {
13485 /* If we have reached into the characters in the START row,
13486 that means the line boundaries have changed. So we
13487 can't start copying with the row START. Maybe it will
13488 work to start copying with the following row. */
13489 while (IT_CHARPOS (it) > CHARPOS (start))
13490 {
13491 /* Advance to the next row as the "start". */
13492 start_row++;
13493 start = start_row->start.pos;
13494 /* If there are no more rows to try, or just one, give up. */
13495 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13496 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13497 || CHARPOS (start) == ZV)
13498 {
13499 clear_glyph_matrix (w->desired_matrix);
13500 return 0;
13501 }
13502
13503 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13504 }
13505 /* If we have reached alignment,
13506 we can copy the rest of the rows. */
13507 if (IT_CHARPOS (it) == CHARPOS (start))
13508 break;
13509
13510 if (display_line (&it))
13511 last_text_row = it.glyph_row - 1;
13512 }
13513
13514 /* A value of current_y < last_visible_y means that we stopped
13515 at the previous window start, which in turn means that we
13516 have at least one reusable row. */
13517 if (it.current_y < it.last_visible_y)
13518 {
13519 /* IT.vpos always starts from 0; it counts text lines. */
13520 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13521
13522 /* Find PT if not already found in the lines displayed. */
13523 if (w->cursor.vpos < 0)
13524 {
13525 int dy = it.current_y - start_row->y;
13526
13527 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13528 row = row_containing_pos (w, PT, row, NULL, dy);
13529 if (row)
13530 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13531 dy, nrows_scrolled);
13532 else
13533 {
13534 clear_glyph_matrix (w->desired_matrix);
13535 return 0;
13536 }
13537 }
13538
13539 /* Scroll the display. Do it before the current matrix is
13540 changed. The problem here is that update has not yet
13541 run, i.e. part of the current matrix is not up to date.
13542 scroll_run_hook will clear the cursor, and use the
13543 current matrix to get the height of the row the cursor is
13544 in. */
13545 run.current_y = start_row->y;
13546 run.desired_y = it.current_y;
13547 run.height = it.last_visible_y - it.current_y;
13548
13549 if (run.height > 0 && run.current_y != run.desired_y)
13550 {
13551 update_begin (f);
13552 rif->update_window_begin_hook (w);
13553 rif->clear_window_mouse_face (w);
13554 rif->scroll_run_hook (w, &run);
13555 rif->update_window_end_hook (w, 0, 0);
13556 update_end (f);
13557 }
13558
13559 /* Shift current matrix down by nrows_scrolled lines. */
13560 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13561 rotate_matrix (w->current_matrix,
13562 start_vpos,
13563 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13564 nrows_scrolled);
13565
13566 /* Disable lines that must be updated. */
13567 for (i = 0; i < it.vpos; ++i)
13568 (start_row + i)->enabled_p = 0;
13569
13570 /* Re-compute Y positions. */
13571 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13572 max_y = it.last_visible_y;
13573 for (row = start_row + nrows_scrolled;
13574 row < bottom_row;
13575 ++row)
13576 {
13577 row->y = it.current_y;
13578 row->visible_height = row->height;
13579
13580 if (row->y < min_y)
13581 row->visible_height -= min_y - row->y;
13582 if (row->y + row->height > max_y)
13583 row->visible_height -= row->y + row->height - max_y;
13584 row->redraw_fringe_bitmaps_p = 1;
13585
13586 it.current_y += row->height;
13587
13588 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13589 last_reused_text_row = row;
13590 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13591 break;
13592 }
13593
13594 /* Disable lines in the current matrix which are now
13595 below the window. */
13596 for (++row; row < bottom_row; ++row)
13597 row->enabled_p = row->mode_line_p = 0;
13598 }
13599
13600 /* Update window_end_pos etc.; last_reused_text_row is the last
13601 reused row from the current matrix containing text, if any.
13602 The value of last_text_row is the last displayed line
13603 containing text. */
13604 if (last_reused_text_row)
13605 {
13606 w->window_end_bytepos
13607 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13608 w->window_end_pos
13609 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13610 w->window_end_vpos
13611 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13612 w->current_matrix));
13613 }
13614 else if (last_text_row)
13615 {
13616 w->window_end_bytepos
13617 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13618 w->window_end_pos
13619 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13620 w->window_end_vpos
13621 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13622 }
13623 else
13624 {
13625 /* This window must be completely empty. */
13626 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13627 w->window_end_pos = make_number (Z - ZV);
13628 w->window_end_vpos = make_number (0);
13629 }
13630 w->window_end_valid = Qnil;
13631
13632 /* Update hint: don't try scrolling again in update_window. */
13633 w->desired_matrix->no_scrolling_p = 1;
13634
13635 #if GLYPH_DEBUG
13636 debug_method_add (w, "try_window_reusing_current_matrix 1");
13637 #endif
13638 return 1;
13639 }
13640 else if (CHARPOS (new_start) > CHARPOS (start))
13641 {
13642 struct glyph_row *pt_row, *row;
13643 struct glyph_row *first_reusable_row;
13644 struct glyph_row *first_row_to_display;
13645 int dy;
13646 int yb = window_text_bottom_y (w);
13647
13648 /* Find the row starting at new_start, if there is one. Don't
13649 reuse a partially visible line at the end. */
13650 first_reusable_row = start_row;
13651 while (first_reusable_row->enabled_p
13652 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13653 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13654 < CHARPOS (new_start)))
13655 ++first_reusable_row;
13656
13657 /* Give up if there is no row to reuse. */
13658 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13659 || !first_reusable_row->enabled_p
13660 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13661 != CHARPOS (new_start)))
13662 return 0;
13663
13664 /* We can reuse fully visible rows beginning with
13665 first_reusable_row to the end of the window. Set
13666 first_row_to_display to the first row that cannot be reused.
13667 Set pt_row to the row containing point, if there is any. */
13668 pt_row = NULL;
13669 for (first_row_to_display = first_reusable_row;
13670 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13671 ++first_row_to_display)
13672 {
13673 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13674 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13675 pt_row = first_row_to_display;
13676 }
13677
13678 /* Start displaying at the start of first_row_to_display. */
13679 xassert (first_row_to_display->y < yb);
13680 init_to_row_start (&it, w, first_row_to_display);
13681
13682 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13683 - start_vpos);
13684 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13685 - nrows_scrolled);
13686 it.current_y = (first_row_to_display->y - first_reusable_row->y
13687 + WINDOW_HEADER_LINE_HEIGHT (w));
13688
13689 /* Display lines beginning with first_row_to_display in the
13690 desired matrix. Set last_text_row to the last row displayed
13691 that displays text. */
13692 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13693 if (pt_row == NULL)
13694 w->cursor.vpos = -1;
13695 last_text_row = NULL;
13696 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13697 if (display_line (&it))
13698 last_text_row = it.glyph_row - 1;
13699
13700 /* Give up If point isn't in a row displayed or reused. */
13701 if (w->cursor.vpos < 0)
13702 {
13703 clear_glyph_matrix (w->desired_matrix);
13704 return 0;
13705 }
13706
13707 /* If point is in a reused row, adjust y and vpos of the cursor
13708 position. */
13709 if (pt_row)
13710 {
13711 w->cursor.vpos -= nrows_scrolled;
13712 w->cursor.y -= first_reusable_row->y - start_row->y;
13713 }
13714
13715 /* Scroll the display. */
13716 run.current_y = first_reusable_row->y;
13717 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13718 run.height = it.last_visible_y - run.current_y;
13719 dy = run.current_y - run.desired_y;
13720
13721 if (run.height)
13722 {
13723 update_begin (f);
13724 rif->update_window_begin_hook (w);
13725 rif->clear_window_mouse_face (w);
13726 rif->scroll_run_hook (w, &run);
13727 rif->update_window_end_hook (w, 0, 0);
13728 update_end (f);
13729 }
13730
13731 /* Adjust Y positions of reused rows. */
13732 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13733 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13734 max_y = it.last_visible_y;
13735 for (row = first_reusable_row; row < first_row_to_display; ++row)
13736 {
13737 row->y -= dy;
13738 row->visible_height = row->height;
13739 if (row->y < min_y)
13740 row->visible_height -= min_y - row->y;
13741 if (row->y + row->height > max_y)
13742 row->visible_height -= row->y + row->height - max_y;
13743 row->redraw_fringe_bitmaps_p = 1;
13744 }
13745
13746 /* Scroll the current matrix. */
13747 xassert (nrows_scrolled > 0);
13748 rotate_matrix (w->current_matrix,
13749 start_vpos,
13750 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13751 -nrows_scrolled);
13752
13753 /* Disable rows not reused. */
13754 for (row -= nrows_scrolled; row < bottom_row; ++row)
13755 row->enabled_p = 0;
13756
13757 /* Point may have moved to a different line, so we cannot assume that
13758 the previous cursor position is valid; locate the correct row. */
13759 if (pt_row)
13760 {
13761 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13762 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13763 row++)
13764 {
13765 w->cursor.vpos++;
13766 w->cursor.y = row->y;
13767 }
13768 if (row < bottom_row)
13769 {
13770 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13771 while (glyph->charpos < PT)
13772 {
13773 w->cursor.hpos++;
13774 w->cursor.x += glyph->pixel_width;
13775 glyph++;
13776 }
13777 }
13778 }
13779
13780 /* Adjust window end. A null value of last_text_row means that
13781 the window end is in reused rows which in turn means that
13782 only its vpos can have changed. */
13783 if (last_text_row)
13784 {
13785 w->window_end_bytepos
13786 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13787 w->window_end_pos
13788 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13789 w->window_end_vpos
13790 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13791 }
13792 else
13793 {
13794 w->window_end_vpos
13795 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13796 }
13797
13798 w->window_end_valid = Qnil;
13799 w->desired_matrix->no_scrolling_p = 1;
13800
13801 #if GLYPH_DEBUG
13802 debug_method_add (w, "try_window_reusing_current_matrix 2");
13803 #endif
13804 return 1;
13805 }
13806
13807 return 0;
13808 }
13809
13810
13811 \f
13812 /************************************************************************
13813 Window redisplay reusing current matrix when buffer has changed
13814 ************************************************************************/
13815
13816 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13817 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13818 int *, int *));
13819 static struct glyph_row *
13820 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13821 struct glyph_row *));
13822
13823
13824 /* Return the last row in MATRIX displaying text. If row START is
13825 non-null, start searching with that row. IT gives the dimensions
13826 of the display. Value is null if matrix is empty; otherwise it is
13827 a pointer to the row found. */
13828
13829 static struct glyph_row *
13830 find_last_row_displaying_text (matrix, it, start)
13831 struct glyph_matrix *matrix;
13832 struct it *it;
13833 struct glyph_row *start;
13834 {
13835 struct glyph_row *row, *row_found;
13836
13837 /* Set row_found to the last row in IT->w's current matrix
13838 displaying text. The loop looks funny but think of partially
13839 visible lines. */
13840 row_found = NULL;
13841 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13842 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13843 {
13844 xassert (row->enabled_p);
13845 row_found = row;
13846 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13847 break;
13848 ++row;
13849 }
13850
13851 return row_found;
13852 }
13853
13854
13855 /* Return the last row in the current matrix of W that is not affected
13856 by changes at the start of current_buffer that occurred since W's
13857 current matrix was built. Value is null if no such row exists.
13858
13859 BEG_UNCHANGED us the number of characters unchanged at the start of
13860 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13861 first changed character in current_buffer. Characters at positions <
13862 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13863 when the current matrix was built. */
13864
13865 static struct glyph_row *
13866 find_last_unchanged_at_beg_row (w)
13867 struct window *w;
13868 {
13869 int first_changed_pos = BEG + BEG_UNCHANGED;
13870 struct glyph_row *row;
13871 struct glyph_row *row_found = NULL;
13872 int yb = window_text_bottom_y (w);
13873
13874 /* Find the last row displaying unchanged text. */
13875 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13876 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13877 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13878 {
13879 if (/* If row ends before first_changed_pos, it is unchanged,
13880 except in some case. */
13881 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13882 /* When row ends in ZV and we write at ZV it is not
13883 unchanged. */
13884 && !row->ends_at_zv_p
13885 /* When first_changed_pos is the end of a continued line,
13886 row is not unchanged because it may be no longer
13887 continued. */
13888 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13889 && (row->continued_p
13890 || row->exact_window_width_line_p)))
13891 row_found = row;
13892
13893 /* Stop if last visible row. */
13894 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13895 break;
13896
13897 ++row;
13898 }
13899
13900 return row_found;
13901 }
13902
13903
13904 /* Find the first glyph row in the current matrix of W that is not
13905 affected by changes at the end of current_buffer since the
13906 time W's current matrix was built.
13907
13908 Return in *DELTA the number of chars by which buffer positions in
13909 unchanged text at the end of current_buffer must be adjusted.
13910
13911 Return in *DELTA_BYTES the corresponding number of bytes.
13912
13913 Value is null if no such row exists, i.e. all rows are affected by
13914 changes. */
13915
13916 static struct glyph_row *
13917 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13918 struct window *w;
13919 int *delta, *delta_bytes;
13920 {
13921 struct glyph_row *row;
13922 struct glyph_row *row_found = NULL;
13923
13924 *delta = *delta_bytes = 0;
13925
13926 /* Display must not have been paused, otherwise the current matrix
13927 is not up to date. */
13928 if (NILP (w->window_end_valid))
13929 abort ();
13930
13931 /* A value of window_end_pos >= END_UNCHANGED means that the window
13932 end is in the range of changed text. If so, there is no
13933 unchanged row at the end of W's current matrix. */
13934 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13935 return NULL;
13936
13937 /* Set row to the last row in W's current matrix displaying text. */
13938 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13939
13940 /* If matrix is entirely empty, no unchanged row exists. */
13941 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13942 {
13943 /* The value of row is the last glyph row in the matrix having a
13944 meaningful buffer position in it. The end position of row
13945 corresponds to window_end_pos. This allows us to translate
13946 buffer positions in the current matrix to current buffer
13947 positions for characters not in changed text. */
13948 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13949 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13950 int last_unchanged_pos, last_unchanged_pos_old;
13951 struct glyph_row *first_text_row
13952 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13953
13954 *delta = Z - Z_old;
13955 *delta_bytes = Z_BYTE - Z_BYTE_old;
13956
13957 /* Set last_unchanged_pos to the buffer position of the last
13958 character in the buffer that has not been changed. Z is the
13959 index + 1 of the last character in current_buffer, i.e. by
13960 subtracting END_UNCHANGED we get the index of the last
13961 unchanged character, and we have to add BEG to get its buffer
13962 position. */
13963 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13964 last_unchanged_pos_old = last_unchanged_pos - *delta;
13965
13966 /* Search backward from ROW for a row displaying a line that
13967 starts at a minimum position >= last_unchanged_pos_old. */
13968 for (; row > first_text_row; --row)
13969 {
13970 /* This used to abort, but it can happen.
13971 It is ok to just stop the search instead here. KFS. */
13972 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13973 break;
13974
13975 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13976 row_found = row;
13977 }
13978 }
13979
13980 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13981 abort ();
13982
13983 return row_found;
13984 }
13985
13986
13987 /* Make sure that glyph rows in the current matrix of window W
13988 reference the same glyph memory as corresponding rows in the
13989 frame's frame matrix. This function is called after scrolling W's
13990 current matrix on a terminal frame in try_window_id and
13991 try_window_reusing_current_matrix. */
13992
13993 static void
13994 sync_frame_with_window_matrix_rows (w)
13995 struct window *w;
13996 {
13997 struct frame *f = XFRAME (w->frame);
13998 struct glyph_row *window_row, *window_row_end, *frame_row;
13999
14000 /* Preconditions: W must be a leaf window and full-width. Its frame
14001 must have a frame matrix. */
14002 xassert (NILP (w->hchild) && NILP (w->vchild));
14003 xassert (WINDOW_FULL_WIDTH_P (w));
14004 xassert (!FRAME_WINDOW_P (f));
14005
14006 /* If W is a full-width window, glyph pointers in W's current matrix
14007 have, by definition, to be the same as glyph pointers in the
14008 corresponding frame matrix. Note that frame matrices have no
14009 marginal areas (see build_frame_matrix). */
14010 window_row = w->current_matrix->rows;
14011 window_row_end = window_row + w->current_matrix->nrows;
14012 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14013 while (window_row < window_row_end)
14014 {
14015 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14016 struct glyph *end = window_row->glyphs[LAST_AREA];
14017
14018 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14019 frame_row->glyphs[TEXT_AREA] = start;
14020 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14021 frame_row->glyphs[LAST_AREA] = end;
14022
14023 /* Disable frame rows whose corresponding window rows have
14024 been disabled in try_window_id. */
14025 if (!window_row->enabled_p)
14026 frame_row->enabled_p = 0;
14027
14028 ++window_row, ++frame_row;
14029 }
14030 }
14031
14032
14033 /* Find the glyph row in window W containing CHARPOS. Consider all
14034 rows between START and END (not inclusive). END null means search
14035 all rows to the end of the display area of W. Value is the row
14036 containing CHARPOS or null. */
14037
14038 struct glyph_row *
14039 row_containing_pos (w, charpos, start, end, dy)
14040 struct window *w;
14041 int charpos;
14042 struct glyph_row *start, *end;
14043 int dy;
14044 {
14045 struct glyph_row *row = start;
14046 int last_y;
14047
14048 /* If we happen to start on a header-line, skip that. */
14049 if (row->mode_line_p)
14050 ++row;
14051
14052 if ((end && row >= end) || !row->enabled_p)
14053 return NULL;
14054
14055 last_y = window_text_bottom_y (w) - dy;
14056
14057 while (1)
14058 {
14059 /* Give up if we have gone too far. */
14060 if (end && row >= end)
14061 return NULL;
14062 /* This formerly returned if they were equal.
14063 I think that both quantities are of a "last plus one" type;
14064 if so, when they are equal, the row is within the screen. -- rms. */
14065 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14066 return NULL;
14067
14068 /* If it is in this row, return this row. */
14069 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14070 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14071 /* The end position of a row equals the start
14072 position of the next row. If CHARPOS is there, we
14073 would rather display it in the next line, except
14074 when this line ends in ZV. */
14075 && !row->ends_at_zv_p
14076 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14077 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14078 return row;
14079 ++row;
14080 }
14081 }
14082
14083
14084 /* Try to redisplay window W by reusing its existing display. W's
14085 current matrix must be up to date when this function is called,
14086 i.e. window_end_valid must not be nil.
14087
14088 Value is
14089
14090 1 if display has been updated
14091 0 if otherwise unsuccessful
14092 -1 if redisplay with same window start is known not to succeed
14093
14094 The following steps are performed:
14095
14096 1. Find the last row in the current matrix of W that is not
14097 affected by changes at the start of current_buffer. If no such row
14098 is found, give up.
14099
14100 2. Find the first row in W's current matrix that is not affected by
14101 changes at the end of current_buffer. Maybe there is no such row.
14102
14103 3. Display lines beginning with the row + 1 found in step 1 to the
14104 row found in step 2 or, if step 2 didn't find a row, to the end of
14105 the window.
14106
14107 4. If cursor is not known to appear on the window, give up.
14108
14109 5. If display stopped at the row found in step 2, scroll the
14110 display and current matrix as needed.
14111
14112 6. Maybe display some lines at the end of W, if we must. This can
14113 happen under various circumstances, like a partially visible line
14114 becoming fully visible, or because newly displayed lines are displayed
14115 in smaller font sizes.
14116
14117 7. Update W's window end information. */
14118
14119 static int
14120 try_window_id (w)
14121 struct window *w;
14122 {
14123 struct frame *f = XFRAME (w->frame);
14124 struct glyph_matrix *current_matrix = w->current_matrix;
14125 struct glyph_matrix *desired_matrix = w->desired_matrix;
14126 struct glyph_row *last_unchanged_at_beg_row;
14127 struct glyph_row *first_unchanged_at_end_row;
14128 struct glyph_row *row;
14129 struct glyph_row *bottom_row;
14130 int bottom_vpos;
14131 struct it it;
14132 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14133 struct text_pos start_pos;
14134 struct run run;
14135 int first_unchanged_at_end_vpos = 0;
14136 struct glyph_row *last_text_row, *last_text_row_at_end;
14137 struct text_pos start;
14138 int first_changed_charpos, last_changed_charpos;
14139
14140 #if GLYPH_DEBUG
14141 if (inhibit_try_window_id)
14142 return 0;
14143 #endif
14144
14145 /* This is handy for debugging. */
14146 #if 0
14147 #define GIVE_UP(X) \
14148 do { \
14149 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14150 return 0; \
14151 } while (0)
14152 #else
14153 #define GIVE_UP(X) return 0
14154 #endif
14155
14156 SET_TEXT_POS_FROM_MARKER (start, w->start);
14157
14158 /* Don't use this for mini-windows because these can show
14159 messages and mini-buffers, and we don't handle that here. */
14160 if (MINI_WINDOW_P (w))
14161 GIVE_UP (1);
14162
14163 /* This flag is used to prevent redisplay optimizations. */
14164 if (windows_or_buffers_changed || cursor_type_changed)
14165 GIVE_UP (2);
14166
14167 /* Verify that narrowing has not changed.
14168 Also verify that we were not told to prevent redisplay optimizations.
14169 It would be nice to further
14170 reduce the number of cases where this prevents try_window_id. */
14171 if (current_buffer->clip_changed
14172 || current_buffer->prevent_redisplay_optimizations_p)
14173 GIVE_UP (3);
14174
14175 /* Window must either use window-based redisplay or be full width. */
14176 if (!FRAME_WINDOW_P (f)
14177 && (!line_ins_del_ok
14178 || !WINDOW_FULL_WIDTH_P (w)))
14179 GIVE_UP (4);
14180
14181 /* Give up if point is not known NOT to appear in W. */
14182 if (PT < CHARPOS (start))
14183 GIVE_UP (5);
14184
14185 /* Another way to prevent redisplay optimizations. */
14186 if (XFASTINT (w->last_modified) == 0)
14187 GIVE_UP (6);
14188
14189 /* Verify that window is not hscrolled. */
14190 if (XFASTINT (w->hscroll) != 0)
14191 GIVE_UP (7);
14192
14193 /* Verify that display wasn't paused. */
14194 if (NILP (w->window_end_valid))
14195 GIVE_UP (8);
14196
14197 /* Can't use this if highlighting a region because a cursor movement
14198 will do more than just set the cursor. */
14199 if (!NILP (Vtransient_mark_mode)
14200 && !NILP (current_buffer->mark_active))
14201 GIVE_UP (9);
14202
14203 /* Likewise if highlighting trailing whitespace. */
14204 if (!NILP (Vshow_trailing_whitespace))
14205 GIVE_UP (11);
14206
14207 /* Likewise if showing a region. */
14208 if (!NILP (w->region_showing))
14209 GIVE_UP (10);
14210
14211 /* Can use this if overlay arrow position and or string have changed. */
14212 if (overlay_arrows_changed_p ())
14213 GIVE_UP (12);
14214
14215
14216 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14217 only if buffer has really changed. The reason is that the gap is
14218 initially at Z for freshly visited files. The code below would
14219 set end_unchanged to 0 in that case. */
14220 if (MODIFF > SAVE_MODIFF
14221 /* This seems to happen sometimes after saving a buffer. */
14222 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14223 {
14224 if (GPT - BEG < BEG_UNCHANGED)
14225 BEG_UNCHANGED = GPT - BEG;
14226 if (Z - GPT < END_UNCHANGED)
14227 END_UNCHANGED = Z - GPT;
14228 }
14229
14230 /* The position of the first and last character that has been changed. */
14231 first_changed_charpos = BEG + BEG_UNCHANGED;
14232 last_changed_charpos = Z - END_UNCHANGED;
14233
14234 /* If window starts after a line end, and the last change is in
14235 front of that newline, then changes don't affect the display.
14236 This case happens with stealth-fontification. Note that although
14237 the display is unchanged, glyph positions in the matrix have to
14238 be adjusted, of course. */
14239 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14240 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14241 && ((last_changed_charpos < CHARPOS (start)
14242 && CHARPOS (start) == BEGV)
14243 || (last_changed_charpos < CHARPOS (start) - 1
14244 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14245 {
14246 int Z_old, delta, Z_BYTE_old, delta_bytes;
14247 struct glyph_row *r0;
14248
14249 /* Compute how many chars/bytes have been added to or removed
14250 from the buffer. */
14251 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14252 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14253 delta = Z - Z_old;
14254 delta_bytes = Z_BYTE - Z_BYTE_old;
14255
14256 /* Give up if PT is not in the window. Note that it already has
14257 been checked at the start of try_window_id that PT is not in
14258 front of the window start. */
14259 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14260 GIVE_UP (13);
14261
14262 /* If window start is unchanged, we can reuse the whole matrix
14263 as is, after adjusting glyph positions. No need to compute
14264 the window end again, since its offset from Z hasn't changed. */
14265 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14266 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14267 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14268 /* PT must not be in a partially visible line. */
14269 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14270 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14271 {
14272 /* Adjust positions in the glyph matrix. */
14273 if (delta || delta_bytes)
14274 {
14275 struct glyph_row *r1
14276 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14277 increment_matrix_positions (w->current_matrix,
14278 MATRIX_ROW_VPOS (r0, current_matrix),
14279 MATRIX_ROW_VPOS (r1, current_matrix),
14280 delta, delta_bytes);
14281 }
14282
14283 /* Set the cursor. */
14284 row = row_containing_pos (w, PT, r0, NULL, 0);
14285 if (row)
14286 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14287 else
14288 abort ();
14289 return 1;
14290 }
14291 }
14292
14293 /* Handle the case that changes are all below what is displayed in
14294 the window, and that PT is in the window. This shortcut cannot
14295 be taken if ZV is visible in the window, and text has been added
14296 there that is visible in the window. */
14297 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14298 /* ZV is not visible in the window, or there are no
14299 changes at ZV, actually. */
14300 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14301 || first_changed_charpos == last_changed_charpos))
14302 {
14303 struct glyph_row *r0;
14304
14305 /* Give up if PT is not in the window. Note that it already has
14306 been checked at the start of try_window_id that PT is not in
14307 front of the window start. */
14308 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14309 GIVE_UP (14);
14310
14311 /* If window start is unchanged, we can reuse the whole matrix
14312 as is, without changing glyph positions since no text has
14313 been added/removed in front of the window end. */
14314 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14315 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14316 /* PT must not be in a partially visible line. */
14317 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14318 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14319 {
14320 /* We have to compute the window end anew since text
14321 can have been added/removed after it. */
14322 w->window_end_pos
14323 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14324 w->window_end_bytepos
14325 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14326
14327 /* Set the cursor. */
14328 row = row_containing_pos (w, PT, r0, NULL, 0);
14329 if (row)
14330 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14331 else
14332 abort ();
14333 return 2;
14334 }
14335 }
14336
14337 /* Give up if window start is in the changed area.
14338
14339 The condition used to read
14340
14341 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14342
14343 but why that was tested escapes me at the moment. */
14344 if (CHARPOS (start) >= first_changed_charpos
14345 && CHARPOS (start) <= last_changed_charpos)
14346 GIVE_UP (15);
14347
14348 /* Check that window start agrees with the start of the first glyph
14349 row in its current matrix. Check this after we know the window
14350 start is not in changed text, otherwise positions would not be
14351 comparable. */
14352 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14353 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14354 GIVE_UP (16);
14355
14356 /* Give up if the window ends in strings. Overlay strings
14357 at the end are difficult to handle, so don't try. */
14358 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14359 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14360 GIVE_UP (20);
14361
14362 /* Compute the position at which we have to start displaying new
14363 lines. Some of the lines at the top of the window might be
14364 reusable because they are not displaying changed text. Find the
14365 last row in W's current matrix not affected by changes at the
14366 start of current_buffer. Value is null if changes start in the
14367 first line of window. */
14368 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14369 if (last_unchanged_at_beg_row)
14370 {
14371 /* Avoid starting to display in the moddle of a character, a TAB
14372 for instance. This is easier than to set up the iterator
14373 exactly, and it's not a frequent case, so the additional
14374 effort wouldn't really pay off. */
14375 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14376 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14377 && last_unchanged_at_beg_row > w->current_matrix->rows)
14378 --last_unchanged_at_beg_row;
14379
14380 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14381 GIVE_UP (17);
14382
14383 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14384 GIVE_UP (18);
14385 start_pos = it.current.pos;
14386
14387 /* Start displaying new lines in the desired matrix at the same
14388 vpos we would use in the current matrix, i.e. below
14389 last_unchanged_at_beg_row. */
14390 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14391 current_matrix);
14392 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14393 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14394
14395 xassert (it.hpos == 0 && it.current_x == 0);
14396 }
14397 else
14398 {
14399 /* There are no reusable lines at the start of the window.
14400 Start displaying in the first text line. */
14401 start_display (&it, w, start);
14402 it.vpos = it.first_vpos;
14403 start_pos = it.current.pos;
14404 }
14405
14406 /* Find the first row that is not affected by changes at the end of
14407 the buffer. Value will be null if there is no unchanged row, in
14408 which case we must redisplay to the end of the window. delta
14409 will be set to the value by which buffer positions beginning with
14410 first_unchanged_at_end_row have to be adjusted due to text
14411 changes. */
14412 first_unchanged_at_end_row
14413 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14414 IF_DEBUG (debug_delta = delta);
14415 IF_DEBUG (debug_delta_bytes = delta_bytes);
14416
14417 /* Set stop_pos to the buffer position up to which we will have to
14418 display new lines. If first_unchanged_at_end_row != NULL, this
14419 is the buffer position of the start of the line displayed in that
14420 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14421 that we don't stop at a buffer position. */
14422 stop_pos = 0;
14423 if (first_unchanged_at_end_row)
14424 {
14425 xassert (last_unchanged_at_beg_row == NULL
14426 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14427
14428 /* If this is a continuation line, move forward to the next one
14429 that isn't. Changes in lines above affect this line.
14430 Caution: this may move first_unchanged_at_end_row to a row
14431 not displaying text. */
14432 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14433 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14434 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14435 < it.last_visible_y))
14436 ++first_unchanged_at_end_row;
14437
14438 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14439 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14440 >= it.last_visible_y))
14441 first_unchanged_at_end_row = NULL;
14442 else
14443 {
14444 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14445 + delta);
14446 first_unchanged_at_end_vpos
14447 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14448 xassert (stop_pos >= Z - END_UNCHANGED);
14449 }
14450 }
14451 else if (last_unchanged_at_beg_row == NULL)
14452 GIVE_UP (19);
14453
14454
14455 #if GLYPH_DEBUG
14456
14457 /* Either there is no unchanged row at the end, or the one we have
14458 now displays text. This is a necessary condition for the window
14459 end pos calculation at the end of this function. */
14460 xassert (first_unchanged_at_end_row == NULL
14461 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14462
14463 debug_last_unchanged_at_beg_vpos
14464 = (last_unchanged_at_beg_row
14465 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14466 : -1);
14467 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14468
14469 #endif /* GLYPH_DEBUG != 0 */
14470
14471
14472 /* Display new lines. Set last_text_row to the last new line
14473 displayed which has text on it, i.e. might end up as being the
14474 line where the window_end_vpos is. */
14475 w->cursor.vpos = -1;
14476 last_text_row = NULL;
14477 overlay_arrow_seen = 0;
14478 while (it.current_y < it.last_visible_y
14479 && !fonts_changed_p
14480 && (first_unchanged_at_end_row == NULL
14481 || IT_CHARPOS (it) < stop_pos))
14482 {
14483 if (display_line (&it))
14484 last_text_row = it.glyph_row - 1;
14485 }
14486
14487 if (fonts_changed_p)
14488 return -1;
14489
14490
14491 /* Compute differences in buffer positions, y-positions etc. for
14492 lines reused at the bottom of the window. Compute what we can
14493 scroll. */
14494 if (first_unchanged_at_end_row
14495 /* No lines reused because we displayed everything up to the
14496 bottom of the window. */
14497 && it.current_y < it.last_visible_y)
14498 {
14499 dvpos = (it.vpos
14500 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14501 current_matrix));
14502 dy = it.current_y - first_unchanged_at_end_row->y;
14503 run.current_y = first_unchanged_at_end_row->y;
14504 run.desired_y = run.current_y + dy;
14505 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14506 }
14507 else
14508 {
14509 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14510 first_unchanged_at_end_row = NULL;
14511 }
14512 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14513
14514
14515 /* Find the cursor if not already found. We have to decide whether
14516 PT will appear on this window (it sometimes doesn't, but this is
14517 not a very frequent case.) This decision has to be made before
14518 the current matrix is altered. A value of cursor.vpos < 0 means
14519 that PT is either in one of the lines beginning at
14520 first_unchanged_at_end_row or below the window. Don't care for
14521 lines that might be displayed later at the window end; as
14522 mentioned, this is not a frequent case. */
14523 if (w->cursor.vpos < 0)
14524 {
14525 /* Cursor in unchanged rows at the top? */
14526 if (PT < CHARPOS (start_pos)
14527 && last_unchanged_at_beg_row)
14528 {
14529 row = row_containing_pos (w, PT,
14530 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14531 last_unchanged_at_beg_row + 1, 0);
14532 if (row)
14533 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14534 }
14535
14536 /* Start from first_unchanged_at_end_row looking for PT. */
14537 else if (first_unchanged_at_end_row)
14538 {
14539 row = row_containing_pos (w, PT - delta,
14540 first_unchanged_at_end_row, NULL, 0);
14541 if (row)
14542 set_cursor_from_row (w, row, w->current_matrix, delta,
14543 delta_bytes, dy, dvpos);
14544 }
14545
14546 /* Give up if cursor was not found. */
14547 if (w->cursor.vpos < 0)
14548 {
14549 clear_glyph_matrix (w->desired_matrix);
14550 return -1;
14551 }
14552 }
14553
14554 /* Don't let the cursor end in the scroll margins. */
14555 {
14556 int this_scroll_margin, cursor_height;
14557
14558 this_scroll_margin = max (0, scroll_margin);
14559 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14560 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14561 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14562
14563 if ((w->cursor.y < this_scroll_margin
14564 && CHARPOS (start) > BEGV)
14565 /* Old redisplay didn't take scroll margin into account at the bottom,
14566 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14567 || (w->cursor.y + (make_cursor_line_fully_visible_p
14568 ? cursor_height + this_scroll_margin
14569 : 1)) > it.last_visible_y)
14570 {
14571 w->cursor.vpos = -1;
14572 clear_glyph_matrix (w->desired_matrix);
14573 return -1;
14574 }
14575 }
14576
14577 /* Scroll the display. Do it before changing the current matrix so
14578 that xterm.c doesn't get confused about where the cursor glyph is
14579 found. */
14580 if (dy && run.height)
14581 {
14582 update_begin (f);
14583
14584 if (FRAME_WINDOW_P (f))
14585 {
14586 rif->update_window_begin_hook (w);
14587 rif->clear_window_mouse_face (w);
14588 rif->scroll_run_hook (w, &run);
14589 rif->update_window_end_hook (w, 0, 0);
14590 }
14591 else
14592 {
14593 /* Terminal frame. In this case, dvpos gives the number of
14594 lines to scroll by; dvpos < 0 means scroll up. */
14595 int first_unchanged_at_end_vpos
14596 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14597 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14598 int end = (WINDOW_TOP_EDGE_LINE (w)
14599 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14600 + window_internal_height (w));
14601
14602 /* Perform the operation on the screen. */
14603 if (dvpos > 0)
14604 {
14605 /* Scroll last_unchanged_at_beg_row to the end of the
14606 window down dvpos lines. */
14607 set_terminal_window (end);
14608
14609 /* On dumb terminals delete dvpos lines at the end
14610 before inserting dvpos empty lines. */
14611 if (!scroll_region_ok)
14612 ins_del_lines (end - dvpos, -dvpos);
14613
14614 /* Insert dvpos empty lines in front of
14615 last_unchanged_at_beg_row. */
14616 ins_del_lines (from, dvpos);
14617 }
14618 else if (dvpos < 0)
14619 {
14620 /* Scroll up last_unchanged_at_beg_vpos to the end of
14621 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14622 set_terminal_window (end);
14623
14624 /* Delete dvpos lines in front of
14625 last_unchanged_at_beg_vpos. ins_del_lines will set
14626 the cursor to the given vpos and emit |dvpos| delete
14627 line sequences. */
14628 ins_del_lines (from + dvpos, dvpos);
14629
14630 /* On a dumb terminal insert dvpos empty lines at the
14631 end. */
14632 if (!scroll_region_ok)
14633 ins_del_lines (end + dvpos, -dvpos);
14634 }
14635
14636 set_terminal_window (0);
14637 }
14638
14639 update_end (f);
14640 }
14641
14642 /* Shift reused rows of the current matrix to the right position.
14643 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14644 text. */
14645 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14646 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14647 if (dvpos < 0)
14648 {
14649 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14650 bottom_vpos, dvpos);
14651 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14652 bottom_vpos, 0);
14653 }
14654 else if (dvpos > 0)
14655 {
14656 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14657 bottom_vpos, dvpos);
14658 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14659 first_unchanged_at_end_vpos + dvpos, 0);
14660 }
14661
14662 /* For frame-based redisplay, make sure that current frame and window
14663 matrix are in sync with respect to glyph memory. */
14664 if (!FRAME_WINDOW_P (f))
14665 sync_frame_with_window_matrix_rows (w);
14666
14667 /* Adjust buffer positions in reused rows. */
14668 if (delta)
14669 increment_matrix_positions (current_matrix,
14670 first_unchanged_at_end_vpos + dvpos,
14671 bottom_vpos, delta, delta_bytes);
14672
14673 /* Adjust Y positions. */
14674 if (dy)
14675 shift_glyph_matrix (w, current_matrix,
14676 first_unchanged_at_end_vpos + dvpos,
14677 bottom_vpos, dy);
14678
14679 if (first_unchanged_at_end_row)
14680 {
14681 first_unchanged_at_end_row += dvpos;
14682 if (first_unchanged_at_end_row->y >= it.last_visible_y
14683 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14684 first_unchanged_at_end_row = NULL;
14685 }
14686
14687 /* If scrolling up, there may be some lines to display at the end of
14688 the window. */
14689 last_text_row_at_end = NULL;
14690 if (dy < 0)
14691 {
14692 /* Scrolling up can leave for example a partially visible line
14693 at the end of the window to be redisplayed. */
14694 /* Set last_row to the glyph row in the current matrix where the
14695 window end line is found. It has been moved up or down in
14696 the matrix by dvpos. */
14697 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14698 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14699
14700 /* If last_row is the window end line, it should display text. */
14701 xassert (last_row->displays_text_p);
14702
14703 /* If window end line was partially visible before, begin
14704 displaying at that line. Otherwise begin displaying with the
14705 line following it. */
14706 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14707 {
14708 init_to_row_start (&it, w, last_row);
14709 it.vpos = last_vpos;
14710 it.current_y = last_row->y;
14711 }
14712 else
14713 {
14714 init_to_row_end (&it, w, last_row);
14715 it.vpos = 1 + last_vpos;
14716 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14717 ++last_row;
14718 }
14719
14720 /* We may start in a continuation line. If so, we have to
14721 get the right continuation_lines_width and current_x. */
14722 it.continuation_lines_width = last_row->continuation_lines_width;
14723 it.hpos = it.current_x = 0;
14724
14725 /* Display the rest of the lines at the window end. */
14726 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14727 while (it.current_y < it.last_visible_y
14728 && !fonts_changed_p)
14729 {
14730 /* Is it always sure that the display agrees with lines in
14731 the current matrix? I don't think so, so we mark rows
14732 displayed invalid in the current matrix by setting their
14733 enabled_p flag to zero. */
14734 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14735 if (display_line (&it))
14736 last_text_row_at_end = it.glyph_row - 1;
14737 }
14738 }
14739
14740 /* Update window_end_pos and window_end_vpos. */
14741 if (first_unchanged_at_end_row
14742 && !last_text_row_at_end)
14743 {
14744 /* Window end line if one of the preserved rows from the current
14745 matrix. Set row to the last row displaying text in current
14746 matrix starting at first_unchanged_at_end_row, after
14747 scrolling. */
14748 xassert (first_unchanged_at_end_row->displays_text_p);
14749 row = find_last_row_displaying_text (w->current_matrix, &it,
14750 first_unchanged_at_end_row);
14751 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14752
14753 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14754 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14755 w->window_end_vpos
14756 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14757 xassert (w->window_end_bytepos >= 0);
14758 IF_DEBUG (debug_method_add (w, "A"));
14759 }
14760 else if (last_text_row_at_end)
14761 {
14762 w->window_end_pos
14763 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14764 w->window_end_bytepos
14765 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14766 w->window_end_vpos
14767 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14768 xassert (w->window_end_bytepos >= 0);
14769 IF_DEBUG (debug_method_add (w, "B"));
14770 }
14771 else if (last_text_row)
14772 {
14773 /* We have displayed either to the end of the window or at the
14774 end of the window, i.e. the last row with text is to be found
14775 in the desired matrix. */
14776 w->window_end_pos
14777 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14778 w->window_end_bytepos
14779 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14780 w->window_end_vpos
14781 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14782 xassert (w->window_end_bytepos >= 0);
14783 }
14784 else if (first_unchanged_at_end_row == NULL
14785 && last_text_row == NULL
14786 && last_text_row_at_end == NULL)
14787 {
14788 /* Displayed to end of window, but no line containing text was
14789 displayed. Lines were deleted at the end of the window. */
14790 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14791 int vpos = XFASTINT (w->window_end_vpos);
14792 struct glyph_row *current_row = current_matrix->rows + vpos;
14793 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14794
14795 for (row = NULL;
14796 row == NULL && vpos >= first_vpos;
14797 --vpos, --current_row, --desired_row)
14798 {
14799 if (desired_row->enabled_p)
14800 {
14801 if (desired_row->displays_text_p)
14802 row = desired_row;
14803 }
14804 else if (current_row->displays_text_p)
14805 row = current_row;
14806 }
14807
14808 xassert (row != NULL);
14809 w->window_end_vpos = make_number (vpos + 1);
14810 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14811 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14812 xassert (w->window_end_bytepos >= 0);
14813 IF_DEBUG (debug_method_add (w, "C"));
14814 }
14815 else
14816 abort ();
14817
14818 #if 0 /* This leads to problems, for instance when the cursor is
14819 at ZV, and the cursor line displays no text. */
14820 /* Disable rows below what's displayed in the window. This makes
14821 debugging easier. */
14822 enable_glyph_matrix_rows (current_matrix,
14823 XFASTINT (w->window_end_vpos) + 1,
14824 bottom_vpos, 0);
14825 #endif
14826
14827 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14828 debug_end_vpos = XFASTINT (w->window_end_vpos));
14829
14830 /* Record that display has not been completed. */
14831 w->window_end_valid = Qnil;
14832 w->desired_matrix->no_scrolling_p = 1;
14833 return 3;
14834
14835 #undef GIVE_UP
14836 }
14837
14838
14839 \f
14840 /***********************************************************************
14841 More debugging support
14842 ***********************************************************************/
14843
14844 #if GLYPH_DEBUG
14845
14846 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14847 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14848 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14849
14850
14851 /* Dump the contents of glyph matrix MATRIX on stderr.
14852
14853 GLYPHS 0 means don't show glyph contents.
14854 GLYPHS 1 means show glyphs in short form
14855 GLYPHS > 1 means show glyphs in long form. */
14856
14857 void
14858 dump_glyph_matrix (matrix, glyphs)
14859 struct glyph_matrix *matrix;
14860 int glyphs;
14861 {
14862 int i;
14863 for (i = 0; i < matrix->nrows; ++i)
14864 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14865 }
14866
14867
14868 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14869 the glyph row and area where the glyph comes from. */
14870
14871 void
14872 dump_glyph (row, glyph, area)
14873 struct glyph_row *row;
14874 struct glyph *glyph;
14875 int area;
14876 {
14877 if (glyph->type == CHAR_GLYPH)
14878 {
14879 fprintf (stderr,
14880 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14881 glyph - row->glyphs[TEXT_AREA],
14882 'C',
14883 glyph->charpos,
14884 (BUFFERP (glyph->object)
14885 ? 'B'
14886 : (STRINGP (glyph->object)
14887 ? 'S'
14888 : '-')),
14889 glyph->pixel_width,
14890 glyph->u.ch,
14891 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14892 ? glyph->u.ch
14893 : '.'),
14894 glyph->face_id,
14895 glyph->left_box_line_p,
14896 glyph->right_box_line_p);
14897 }
14898 else if (glyph->type == STRETCH_GLYPH)
14899 {
14900 fprintf (stderr,
14901 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14902 glyph - row->glyphs[TEXT_AREA],
14903 'S',
14904 glyph->charpos,
14905 (BUFFERP (glyph->object)
14906 ? 'B'
14907 : (STRINGP (glyph->object)
14908 ? 'S'
14909 : '-')),
14910 glyph->pixel_width,
14911 0,
14912 '.',
14913 glyph->face_id,
14914 glyph->left_box_line_p,
14915 glyph->right_box_line_p);
14916 }
14917 else if (glyph->type == IMAGE_GLYPH)
14918 {
14919 fprintf (stderr,
14920 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14921 glyph - row->glyphs[TEXT_AREA],
14922 'I',
14923 glyph->charpos,
14924 (BUFFERP (glyph->object)
14925 ? 'B'
14926 : (STRINGP (glyph->object)
14927 ? 'S'
14928 : '-')),
14929 glyph->pixel_width,
14930 glyph->u.img_id,
14931 '.',
14932 glyph->face_id,
14933 glyph->left_box_line_p,
14934 glyph->right_box_line_p);
14935 }
14936 }
14937
14938
14939 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14940 GLYPHS 0 means don't show glyph contents.
14941 GLYPHS 1 means show glyphs in short form
14942 GLYPHS > 1 means show glyphs in long form. */
14943
14944 void
14945 dump_glyph_row (row, vpos, glyphs)
14946 struct glyph_row *row;
14947 int vpos, glyphs;
14948 {
14949 if (glyphs != 1)
14950 {
14951 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
14952 fprintf (stderr, "======================================================================\n");
14953
14954 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14955 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14956 vpos,
14957 MATRIX_ROW_START_CHARPOS (row),
14958 MATRIX_ROW_END_CHARPOS (row),
14959 row->used[TEXT_AREA],
14960 row->contains_overlapping_glyphs_p,
14961 row->enabled_p,
14962 row->truncated_on_left_p,
14963 row->truncated_on_right_p,
14964 row->continued_p,
14965 MATRIX_ROW_CONTINUATION_LINE_P (row),
14966 row->displays_text_p,
14967 row->ends_at_zv_p,
14968 row->fill_line_p,
14969 row->ends_in_middle_of_char_p,
14970 row->starts_in_middle_of_char_p,
14971 row->mouse_face_p,
14972 row->x,
14973 row->y,
14974 row->pixel_width,
14975 row->height,
14976 row->visible_height,
14977 row->ascent,
14978 row->phys_ascent);
14979 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14980 row->end.overlay_string_index,
14981 row->continuation_lines_width);
14982 fprintf (stderr, "%9d %5d\n",
14983 CHARPOS (row->start.string_pos),
14984 CHARPOS (row->end.string_pos));
14985 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14986 row->end.dpvec_index);
14987 }
14988
14989 if (glyphs > 1)
14990 {
14991 int area;
14992
14993 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14994 {
14995 struct glyph *glyph = row->glyphs[area];
14996 struct glyph *glyph_end = glyph + row->used[area];
14997
14998 /* Glyph for a line end in text. */
14999 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15000 ++glyph_end;
15001
15002 if (glyph < glyph_end)
15003 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15004
15005 for (; glyph < glyph_end; ++glyph)
15006 dump_glyph (row, glyph, area);
15007 }
15008 }
15009 else if (glyphs == 1)
15010 {
15011 int area;
15012
15013 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15014 {
15015 char *s = (char *) alloca (row->used[area] + 1);
15016 int i;
15017
15018 for (i = 0; i < row->used[area]; ++i)
15019 {
15020 struct glyph *glyph = row->glyphs[area] + i;
15021 if (glyph->type == CHAR_GLYPH
15022 && glyph->u.ch < 0x80
15023 && glyph->u.ch >= ' ')
15024 s[i] = glyph->u.ch;
15025 else
15026 s[i] = '.';
15027 }
15028
15029 s[i] = '\0';
15030 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15031 }
15032 }
15033 }
15034
15035
15036 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15037 Sdump_glyph_matrix, 0, 1, "p",
15038 doc: /* Dump the current matrix of the selected window to stderr.
15039 Shows contents of glyph row structures. With non-nil
15040 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15041 glyphs in short form, otherwise show glyphs in long form. */)
15042 (glyphs)
15043 Lisp_Object glyphs;
15044 {
15045 struct window *w = XWINDOW (selected_window);
15046 struct buffer *buffer = XBUFFER (w->buffer);
15047
15048 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15049 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15050 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15051 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15052 fprintf (stderr, "=============================================\n");
15053 dump_glyph_matrix (w->current_matrix,
15054 NILP (glyphs) ? 0 : XINT (glyphs));
15055 return Qnil;
15056 }
15057
15058
15059 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15060 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15061 ()
15062 {
15063 struct frame *f = XFRAME (selected_frame);
15064 dump_glyph_matrix (f->current_matrix, 1);
15065 return Qnil;
15066 }
15067
15068
15069 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15070 doc: /* Dump glyph row ROW to stderr.
15071 GLYPH 0 means don't dump glyphs.
15072 GLYPH 1 means dump glyphs in short form.
15073 GLYPH > 1 or omitted means dump glyphs in long form. */)
15074 (row, glyphs)
15075 Lisp_Object row, glyphs;
15076 {
15077 struct glyph_matrix *matrix;
15078 int vpos;
15079
15080 CHECK_NUMBER (row);
15081 matrix = XWINDOW (selected_window)->current_matrix;
15082 vpos = XINT (row);
15083 if (vpos >= 0 && vpos < matrix->nrows)
15084 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15085 vpos,
15086 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15087 return Qnil;
15088 }
15089
15090
15091 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15092 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15093 GLYPH 0 means don't dump glyphs.
15094 GLYPH 1 means dump glyphs in short form.
15095 GLYPH > 1 or omitted means dump glyphs in long form. */)
15096 (row, glyphs)
15097 Lisp_Object row, glyphs;
15098 {
15099 struct frame *sf = SELECTED_FRAME ();
15100 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15101 int vpos;
15102
15103 CHECK_NUMBER (row);
15104 vpos = XINT (row);
15105 if (vpos >= 0 && vpos < m->nrows)
15106 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15107 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15108 return Qnil;
15109 }
15110
15111
15112 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15113 doc: /* Toggle tracing of redisplay.
15114 With ARG, turn tracing on if and only if ARG is positive. */)
15115 (arg)
15116 Lisp_Object arg;
15117 {
15118 if (NILP (arg))
15119 trace_redisplay_p = !trace_redisplay_p;
15120 else
15121 {
15122 arg = Fprefix_numeric_value (arg);
15123 trace_redisplay_p = XINT (arg) > 0;
15124 }
15125
15126 return Qnil;
15127 }
15128
15129
15130 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15131 doc: /* Like `format', but print result to stderr.
15132 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15133 (nargs, args)
15134 int nargs;
15135 Lisp_Object *args;
15136 {
15137 Lisp_Object s = Fformat (nargs, args);
15138 fprintf (stderr, "%s", SDATA (s));
15139 return Qnil;
15140 }
15141
15142 #endif /* GLYPH_DEBUG */
15143
15144
15145 \f
15146 /***********************************************************************
15147 Building Desired Matrix Rows
15148 ***********************************************************************/
15149
15150 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15151 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15152
15153 static struct glyph_row *
15154 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15155 struct window *w;
15156 Lisp_Object overlay_arrow_string;
15157 {
15158 struct frame *f = XFRAME (WINDOW_FRAME (w));
15159 struct buffer *buffer = XBUFFER (w->buffer);
15160 struct buffer *old = current_buffer;
15161 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15162 int arrow_len = SCHARS (overlay_arrow_string);
15163 const unsigned char *arrow_end = arrow_string + arrow_len;
15164 const unsigned char *p;
15165 struct it it;
15166 int multibyte_p;
15167 int n_glyphs_before;
15168
15169 set_buffer_temp (buffer);
15170 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15171 it.glyph_row->used[TEXT_AREA] = 0;
15172 SET_TEXT_POS (it.position, 0, 0);
15173
15174 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15175 p = arrow_string;
15176 while (p < arrow_end)
15177 {
15178 Lisp_Object face, ilisp;
15179
15180 /* Get the next character. */
15181 if (multibyte_p)
15182 it.c = string_char_and_length (p, arrow_len, &it.len);
15183 else
15184 it.c = *p, it.len = 1;
15185 p += it.len;
15186
15187 /* Get its face. */
15188 ilisp = make_number (p - arrow_string);
15189 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15190 it.face_id = compute_char_face (f, it.c, face);
15191
15192 /* Compute its width, get its glyphs. */
15193 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15194 SET_TEXT_POS (it.position, -1, -1);
15195 PRODUCE_GLYPHS (&it);
15196
15197 /* If this character doesn't fit any more in the line, we have
15198 to remove some glyphs. */
15199 if (it.current_x > it.last_visible_x)
15200 {
15201 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15202 break;
15203 }
15204 }
15205
15206 set_buffer_temp (old);
15207 return it.glyph_row;
15208 }
15209
15210
15211 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15212 glyphs are only inserted for terminal frames since we can't really
15213 win with truncation glyphs when partially visible glyphs are
15214 involved. Which glyphs to insert is determined by
15215 produce_special_glyphs. */
15216
15217 static void
15218 insert_left_trunc_glyphs (it)
15219 struct it *it;
15220 {
15221 struct it truncate_it;
15222 struct glyph *from, *end, *to, *toend;
15223
15224 xassert (!FRAME_WINDOW_P (it->f));
15225
15226 /* Get the truncation glyphs. */
15227 truncate_it = *it;
15228 truncate_it.current_x = 0;
15229 truncate_it.face_id = DEFAULT_FACE_ID;
15230 truncate_it.glyph_row = &scratch_glyph_row;
15231 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15232 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15233 truncate_it.object = make_number (0);
15234 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15235
15236 /* Overwrite glyphs from IT with truncation glyphs. */
15237 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15238 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15239 to = it->glyph_row->glyphs[TEXT_AREA];
15240 toend = to + it->glyph_row->used[TEXT_AREA];
15241
15242 while (from < end)
15243 *to++ = *from++;
15244
15245 /* There may be padding glyphs left over. Overwrite them too. */
15246 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15247 {
15248 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15249 while (from < end)
15250 *to++ = *from++;
15251 }
15252
15253 if (to > toend)
15254 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15255 }
15256
15257
15258 /* Compute the pixel height and width of IT->glyph_row.
15259
15260 Most of the time, ascent and height of a display line will be equal
15261 to the max_ascent and max_height values of the display iterator
15262 structure. This is not the case if
15263
15264 1. We hit ZV without displaying anything. In this case, max_ascent
15265 and max_height will be zero.
15266
15267 2. We have some glyphs that don't contribute to the line height.
15268 (The glyph row flag contributes_to_line_height_p is for future
15269 pixmap extensions).
15270
15271 The first case is easily covered by using default values because in
15272 these cases, the line height does not really matter, except that it
15273 must not be zero. */
15274
15275 static void
15276 compute_line_metrics (it)
15277 struct it *it;
15278 {
15279 struct glyph_row *row = it->glyph_row;
15280 int area, i;
15281
15282 if (FRAME_WINDOW_P (it->f))
15283 {
15284 int i, min_y, max_y;
15285
15286 /* The line may consist of one space only, that was added to
15287 place the cursor on it. If so, the row's height hasn't been
15288 computed yet. */
15289 if (row->height == 0)
15290 {
15291 if (it->max_ascent + it->max_descent == 0)
15292 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15293 row->ascent = it->max_ascent;
15294 row->height = it->max_ascent + it->max_descent;
15295 row->phys_ascent = it->max_phys_ascent;
15296 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15297 row->extra_line_spacing = it->max_extra_line_spacing;
15298 }
15299
15300 /* Compute the width of this line. */
15301 row->pixel_width = row->x;
15302 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15303 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15304
15305 xassert (row->pixel_width >= 0);
15306 xassert (row->ascent >= 0 && row->height > 0);
15307
15308 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15309 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15310
15311 /* If first line's physical ascent is larger than its logical
15312 ascent, use the physical ascent, and make the row taller.
15313 This makes accented characters fully visible. */
15314 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15315 && row->phys_ascent > row->ascent)
15316 {
15317 row->height += row->phys_ascent - row->ascent;
15318 row->ascent = row->phys_ascent;
15319 }
15320
15321 /* Compute how much of the line is visible. */
15322 row->visible_height = row->height;
15323
15324 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15325 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15326
15327 if (row->y < min_y)
15328 row->visible_height -= min_y - row->y;
15329 if (row->y + row->height > max_y)
15330 row->visible_height -= row->y + row->height - max_y;
15331 }
15332 else
15333 {
15334 row->pixel_width = row->used[TEXT_AREA];
15335 if (row->continued_p)
15336 row->pixel_width -= it->continuation_pixel_width;
15337 else if (row->truncated_on_right_p)
15338 row->pixel_width -= it->truncation_pixel_width;
15339 row->ascent = row->phys_ascent = 0;
15340 row->height = row->phys_height = row->visible_height = 1;
15341 row->extra_line_spacing = 0;
15342 }
15343
15344 /* Compute a hash code for this row. */
15345 row->hash = 0;
15346 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15347 for (i = 0; i < row->used[area]; ++i)
15348 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15349 + row->glyphs[area][i].u.val
15350 + row->glyphs[area][i].face_id
15351 + row->glyphs[area][i].padding_p
15352 + (row->glyphs[area][i].type << 2));
15353
15354 it->max_ascent = it->max_descent = 0;
15355 it->max_phys_ascent = it->max_phys_descent = 0;
15356 }
15357
15358
15359 /* Append one space to the glyph row of iterator IT if doing a
15360 window-based redisplay. The space has the same face as
15361 IT->face_id. Value is non-zero if a space was added.
15362
15363 This function is called to make sure that there is always one glyph
15364 at the end of a glyph row that the cursor can be set on under
15365 window-systems. (If there weren't such a glyph we would not know
15366 how wide and tall a box cursor should be displayed).
15367
15368 At the same time this space let's a nicely handle clearing to the
15369 end of the line if the row ends in italic text. */
15370
15371 static int
15372 append_space_for_newline (it, default_face_p)
15373 struct it *it;
15374 int default_face_p;
15375 {
15376 if (FRAME_WINDOW_P (it->f))
15377 {
15378 int n = it->glyph_row->used[TEXT_AREA];
15379
15380 if (it->glyph_row->glyphs[TEXT_AREA] + n
15381 < it->glyph_row->glyphs[1 + TEXT_AREA])
15382 {
15383 /* Save some values that must not be changed.
15384 Must save IT->c and IT->len because otherwise
15385 ITERATOR_AT_END_P wouldn't work anymore after
15386 append_space_for_newline has been called. */
15387 enum display_element_type saved_what = it->what;
15388 int saved_c = it->c, saved_len = it->len;
15389 int saved_x = it->current_x;
15390 int saved_face_id = it->face_id;
15391 struct text_pos saved_pos;
15392 Lisp_Object saved_object;
15393 struct face *face;
15394
15395 saved_object = it->object;
15396 saved_pos = it->position;
15397
15398 it->what = IT_CHARACTER;
15399 bzero (&it->position, sizeof it->position);
15400 it->object = make_number (0);
15401 it->c = ' ';
15402 it->len = 1;
15403
15404 if (default_face_p)
15405 it->face_id = DEFAULT_FACE_ID;
15406 else if (it->face_before_selective_p)
15407 it->face_id = it->saved_face_id;
15408 face = FACE_FROM_ID (it->f, it->face_id);
15409 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15410
15411 PRODUCE_GLYPHS (it);
15412
15413 it->override_ascent = -1;
15414 it->constrain_row_ascent_descent_p = 0;
15415 it->current_x = saved_x;
15416 it->object = saved_object;
15417 it->position = saved_pos;
15418 it->what = saved_what;
15419 it->face_id = saved_face_id;
15420 it->len = saved_len;
15421 it->c = saved_c;
15422 return 1;
15423 }
15424 }
15425
15426 return 0;
15427 }
15428
15429
15430 /* Extend the face of the last glyph in the text area of IT->glyph_row
15431 to the end of the display line. Called from display_line.
15432 If the glyph row is empty, add a space glyph to it so that we
15433 know the face to draw. Set the glyph row flag fill_line_p. */
15434
15435 static void
15436 extend_face_to_end_of_line (it)
15437 struct it *it;
15438 {
15439 struct face *face;
15440 struct frame *f = it->f;
15441
15442 /* If line is already filled, do nothing. */
15443 if (it->current_x >= it->last_visible_x)
15444 return;
15445
15446 /* Face extension extends the background and box of IT->face_id
15447 to the end of the line. If the background equals the background
15448 of the frame, we don't have to do anything. */
15449 if (it->face_before_selective_p)
15450 face = FACE_FROM_ID (it->f, it->saved_face_id);
15451 else
15452 face = FACE_FROM_ID (f, it->face_id);
15453
15454 if (FRAME_WINDOW_P (f)
15455 && it->glyph_row->displays_text_p
15456 && face->box == FACE_NO_BOX
15457 && face->background == FRAME_BACKGROUND_PIXEL (f)
15458 && !face->stipple)
15459 return;
15460
15461 /* Set the glyph row flag indicating that the face of the last glyph
15462 in the text area has to be drawn to the end of the text area. */
15463 it->glyph_row->fill_line_p = 1;
15464
15465 /* If current character of IT is not ASCII, make sure we have the
15466 ASCII face. This will be automatically undone the next time
15467 get_next_display_element returns a multibyte character. Note
15468 that the character will always be single byte in unibyte text. */
15469 if (!SINGLE_BYTE_CHAR_P (it->c))
15470 {
15471 it->face_id = FACE_FOR_CHAR (f, face, 0);
15472 }
15473
15474 if (FRAME_WINDOW_P (f))
15475 {
15476 /* If the row is empty, add a space with the current face of IT,
15477 so that we know which face to draw. */
15478 if (it->glyph_row->used[TEXT_AREA] == 0)
15479 {
15480 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15481 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15482 it->glyph_row->used[TEXT_AREA] = 1;
15483 }
15484 }
15485 else
15486 {
15487 /* Save some values that must not be changed. */
15488 int saved_x = it->current_x;
15489 struct text_pos saved_pos;
15490 Lisp_Object saved_object;
15491 enum display_element_type saved_what = it->what;
15492 int saved_face_id = it->face_id;
15493
15494 saved_object = it->object;
15495 saved_pos = it->position;
15496
15497 it->what = IT_CHARACTER;
15498 bzero (&it->position, sizeof it->position);
15499 it->object = make_number (0);
15500 it->c = ' ';
15501 it->len = 1;
15502 it->face_id = face->id;
15503
15504 PRODUCE_GLYPHS (it);
15505
15506 while (it->current_x <= it->last_visible_x)
15507 PRODUCE_GLYPHS (it);
15508
15509 /* Don't count these blanks really. It would let us insert a left
15510 truncation glyph below and make us set the cursor on them, maybe. */
15511 it->current_x = saved_x;
15512 it->object = saved_object;
15513 it->position = saved_pos;
15514 it->what = saved_what;
15515 it->face_id = saved_face_id;
15516 }
15517 }
15518
15519
15520 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15521 trailing whitespace. */
15522
15523 static int
15524 trailing_whitespace_p (charpos)
15525 int charpos;
15526 {
15527 int bytepos = CHAR_TO_BYTE (charpos);
15528 int c = 0;
15529
15530 while (bytepos < ZV_BYTE
15531 && (c = FETCH_CHAR (bytepos),
15532 c == ' ' || c == '\t'))
15533 ++bytepos;
15534
15535 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15536 {
15537 if (bytepos != PT_BYTE)
15538 return 1;
15539 }
15540 return 0;
15541 }
15542
15543
15544 /* Highlight trailing whitespace, if any, in ROW. */
15545
15546 void
15547 highlight_trailing_whitespace (f, row)
15548 struct frame *f;
15549 struct glyph_row *row;
15550 {
15551 int used = row->used[TEXT_AREA];
15552
15553 if (used)
15554 {
15555 struct glyph *start = row->glyphs[TEXT_AREA];
15556 struct glyph *glyph = start + used - 1;
15557
15558 /* Skip over glyphs inserted to display the cursor at the
15559 end of a line, for extending the face of the last glyph
15560 to the end of the line on terminals, and for truncation
15561 and continuation glyphs. */
15562 while (glyph >= start
15563 && glyph->type == CHAR_GLYPH
15564 && INTEGERP (glyph->object))
15565 --glyph;
15566
15567 /* If last glyph is a space or stretch, and it's trailing
15568 whitespace, set the face of all trailing whitespace glyphs in
15569 IT->glyph_row to `trailing-whitespace'. */
15570 if (glyph >= start
15571 && BUFFERP (glyph->object)
15572 && (glyph->type == STRETCH_GLYPH
15573 || (glyph->type == CHAR_GLYPH
15574 && glyph->u.ch == ' '))
15575 && trailing_whitespace_p (glyph->charpos))
15576 {
15577 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15578 if (face_id < 0)
15579 return;
15580
15581 while (glyph >= start
15582 && BUFFERP (glyph->object)
15583 && (glyph->type == STRETCH_GLYPH
15584 || (glyph->type == CHAR_GLYPH
15585 && glyph->u.ch == ' ')))
15586 (glyph--)->face_id = face_id;
15587 }
15588 }
15589 }
15590
15591
15592 /* Value is non-zero if glyph row ROW in window W should be
15593 used to hold the cursor. */
15594
15595 static int
15596 cursor_row_p (w, row)
15597 struct window *w;
15598 struct glyph_row *row;
15599 {
15600 int cursor_row_p = 1;
15601
15602 if (PT == MATRIX_ROW_END_CHARPOS (row))
15603 {
15604 /* If the row ends with a newline from a string, we don't want
15605 the cursor there, but we still want it at the start of the
15606 string if the string starts in this row.
15607 If the row is continued it doesn't end in a newline. */
15608 if (CHARPOS (row->end.string_pos) >= 0)
15609 cursor_row_p = (row->continued_p
15610 || PT >= MATRIX_ROW_START_CHARPOS (row));
15611 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15612 {
15613 /* If the row ends in middle of a real character,
15614 and the line is continued, we want the cursor here.
15615 That's because MATRIX_ROW_END_CHARPOS would equal
15616 PT if PT is before the character. */
15617 if (!row->ends_in_ellipsis_p)
15618 cursor_row_p = row->continued_p;
15619 else
15620 /* If the row ends in an ellipsis, then
15621 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15622 We want that position to be displayed after the ellipsis. */
15623 cursor_row_p = 0;
15624 }
15625 /* If the row ends at ZV, display the cursor at the end of that
15626 row instead of at the start of the row below. */
15627 else if (row->ends_at_zv_p)
15628 cursor_row_p = 1;
15629 else
15630 cursor_row_p = 0;
15631 }
15632
15633 return cursor_row_p;
15634 }
15635
15636
15637 /* Construct the glyph row IT->glyph_row in the desired matrix of
15638 IT->w from text at the current position of IT. See dispextern.h
15639 for an overview of struct it. Value is non-zero if
15640 IT->glyph_row displays text, as opposed to a line displaying ZV
15641 only. */
15642
15643 static int
15644 display_line (it)
15645 struct it *it;
15646 {
15647 struct glyph_row *row = it->glyph_row;
15648 Lisp_Object overlay_arrow_string;
15649
15650 /* We always start displaying at hpos zero even if hscrolled. */
15651 xassert (it->hpos == 0 && it->current_x == 0);
15652
15653 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15654 >= it->w->desired_matrix->nrows)
15655 {
15656 it->w->nrows_scale_factor++;
15657 fonts_changed_p = 1;
15658 return 0;
15659 }
15660
15661 /* Is IT->w showing the region? */
15662 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15663
15664 /* Clear the result glyph row and enable it. */
15665 prepare_desired_row (row);
15666
15667 row->y = it->current_y;
15668 row->start = it->start;
15669 row->continuation_lines_width = it->continuation_lines_width;
15670 row->displays_text_p = 1;
15671 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15672 it->starts_in_middle_of_char_p = 0;
15673
15674 /* Arrange the overlays nicely for our purposes. Usually, we call
15675 display_line on only one line at a time, in which case this
15676 can't really hurt too much, or we call it on lines which appear
15677 one after another in the buffer, in which case all calls to
15678 recenter_overlay_lists but the first will be pretty cheap. */
15679 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15680
15681 /* Move over display elements that are not visible because we are
15682 hscrolled. This may stop at an x-position < IT->first_visible_x
15683 if the first glyph is partially visible or if we hit a line end. */
15684 if (it->current_x < it->first_visible_x)
15685 {
15686 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15687 MOVE_TO_POS | MOVE_TO_X);
15688 }
15689
15690 /* Get the initial row height. This is either the height of the
15691 text hscrolled, if there is any, or zero. */
15692 row->ascent = it->max_ascent;
15693 row->height = it->max_ascent + it->max_descent;
15694 row->phys_ascent = it->max_phys_ascent;
15695 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15696 row->extra_line_spacing = it->max_extra_line_spacing;
15697
15698 /* Loop generating characters. The loop is left with IT on the next
15699 character to display. */
15700 while (1)
15701 {
15702 int n_glyphs_before, hpos_before, x_before;
15703 int x, i, nglyphs;
15704 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15705
15706 /* Retrieve the next thing to display. Value is zero if end of
15707 buffer reached. */
15708 if (!get_next_display_element (it))
15709 {
15710 /* Maybe add a space at the end of this line that is used to
15711 display the cursor there under X. Set the charpos of the
15712 first glyph of blank lines not corresponding to any text
15713 to -1. */
15714 #ifdef HAVE_WINDOW_SYSTEM
15715 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15716 row->exact_window_width_line_p = 1;
15717 else
15718 #endif /* HAVE_WINDOW_SYSTEM */
15719 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15720 || row->used[TEXT_AREA] == 0)
15721 {
15722 row->glyphs[TEXT_AREA]->charpos = -1;
15723 row->displays_text_p = 0;
15724
15725 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15726 && (!MINI_WINDOW_P (it->w)
15727 || (minibuf_level && EQ (it->window, minibuf_window))))
15728 row->indicate_empty_line_p = 1;
15729 }
15730
15731 it->continuation_lines_width = 0;
15732 row->ends_at_zv_p = 1;
15733 break;
15734 }
15735
15736 /* Now, get the metrics of what we want to display. This also
15737 generates glyphs in `row' (which is IT->glyph_row). */
15738 n_glyphs_before = row->used[TEXT_AREA];
15739 x = it->current_x;
15740
15741 /* Remember the line height so far in case the next element doesn't
15742 fit on the line. */
15743 if (!it->truncate_lines_p)
15744 {
15745 ascent = it->max_ascent;
15746 descent = it->max_descent;
15747 phys_ascent = it->max_phys_ascent;
15748 phys_descent = it->max_phys_descent;
15749 }
15750
15751 PRODUCE_GLYPHS (it);
15752
15753 /* If this display element was in marginal areas, continue with
15754 the next one. */
15755 if (it->area != TEXT_AREA)
15756 {
15757 row->ascent = max (row->ascent, it->max_ascent);
15758 row->height = max (row->height, it->max_ascent + it->max_descent);
15759 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15760 row->phys_height = max (row->phys_height,
15761 it->max_phys_ascent + it->max_phys_descent);
15762 row->extra_line_spacing = max (row->extra_line_spacing,
15763 it->max_extra_line_spacing);
15764 set_iterator_to_next (it, 1);
15765 continue;
15766 }
15767
15768 /* Does the display element fit on the line? If we truncate
15769 lines, we should draw past the right edge of the window. If
15770 we don't truncate, we want to stop so that we can display the
15771 continuation glyph before the right margin. If lines are
15772 continued, there are two possible strategies for characters
15773 resulting in more than 1 glyph (e.g. tabs): Display as many
15774 glyphs as possible in this line and leave the rest for the
15775 continuation line, or display the whole element in the next
15776 line. Original redisplay did the former, so we do it also. */
15777 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15778 hpos_before = it->hpos;
15779 x_before = x;
15780
15781 if (/* Not a newline. */
15782 nglyphs > 0
15783 /* Glyphs produced fit entirely in the line. */
15784 && it->current_x < it->last_visible_x)
15785 {
15786 it->hpos += nglyphs;
15787 row->ascent = max (row->ascent, it->max_ascent);
15788 row->height = max (row->height, it->max_ascent + it->max_descent);
15789 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15790 row->phys_height = max (row->phys_height,
15791 it->max_phys_ascent + it->max_phys_descent);
15792 row->extra_line_spacing = max (row->extra_line_spacing,
15793 it->max_extra_line_spacing);
15794 if (it->current_x - it->pixel_width < it->first_visible_x)
15795 row->x = x - it->first_visible_x;
15796 }
15797 else
15798 {
15799 int new_x;
15800 struct glyph *glyph;
15801
15802 for (i = 0; i < nglyphs; ++i, x = new_x)
15803 {
15804 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15805 new_x = x + glyph->pixel_width;
15806
15807 if (/* Lines are continued. */
15808 !it->truncate_lines_p
15809 && (/* Glyph doesn't fit on the line. */
15810 new_x > it->last_visible_x
15811 /* Or it fits exactly on a window system frame. */
15812 || (new_x == it->last_visible_x
15813 && FRAME_WINDOW_P (it->f))))
15814 {
15815 /* End of a continued line. */
15816
15817 if (it->hpos == 0
15818 || (new_x == it->last_visible_x
15819 && FRAME_WINDOW_P (it->f)))
15820 {
15821 /* Current glyph is the only one on the line or
15822 fits exactly on the line. We must continue
15823 the line because we can't draw the cursor
15824 after the glyph. */
15825 row->continued_p = 1;
15826 it->current_x = new_x;
15827 it->continuation_lines_width += new_x;
15828 ++it->hpos;
15829 if (i == nglyphs - 1)
15830 {
15831 set_iterator_to_next (it, 1);
15832 #ifdef HAVE_WINDOW_SYSTEM
15833 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15834 {
15835 if (!get_next_display_element (it))
15836 {
15837 row->exact_window_width_line_p = 1;
15838 it->continuation_lines_width = 0;
15839 row->continued_p = 0;
15840 row->ends_at_zv_p = 1;
15841 }
15842 else if (ITERATOR_AT_END_OF_LINE_P (it))
15843 {
15844 row->continued_p = 0;
15845 row->exact_window_width_line_p = 1;
15846 }
15847 }
15848 #endif /* HAVE_WINDOW_SYSTEM */
15849 }
15850 }
15851 else if (CHAR_GLYPH_PADDING_P (*glyph)
15852 && !FRAME_WINDOW_P (it->f))
15853 {
15854 /* A padding glyph that doesn't fit on this line.
15855 This means the whole character doesn't fit
15856 on the line. */
15857 row->used[TEXT_AREA] = n_glyphs_before;
15858
15859 /* Fill the rest of the row with continuation
15860 glyphs like in 20.x. */
15861 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15862 < row->glyphs[1 + TEXT_AREA])
15863 produce_special_glyphs (it, IT_CONTINUATION);
15864
15865 row->continued_p = 1;
15866 it->current_x = x_before;
15867 it->continuation_lines_width += x_before;
15868
15869 /* Restore the height to what it was before the
15870 element not fitting on the line. */
15871 it->max_ascent = ascent;
15872 it->max_descent = descent;
15873 it->max_phys_ascent = phys_ascent;
15874 it->max_phys_descent = phys_descent;
15875 }
15876 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15877 {
15878 /* A TAB that extends past the right edge of the
15879 window. This produces a single glyph on
15880 window system frames. We leave the glyph in
15881 this row and let it fill the row, but don't
15882 consume the TAB. */
15883 it->continuation_lines_width += it->last_visible_x;
15884 row->ends_in_middle_of_char_p = 1;
15885 row->continued_p = 1;
15886 glyph->pixel_width = it->last_visible_x - x;
15887 it->starts_in_middle_of_char_p = 1;
15888 }
15889 else
15890 {
15891 /* Something other than a TAB that draws past
15892 the right edge of the window. Restore
15893 positions to values before the element. */
15894 row->used[TEXT_AREA] = n_glyphs_before + i;
15895
15896 /* Display continuation glyphs. */
15897 if (!FRAME_WINDOW_P (it->f))
15898 produce_special_glyphs (it, IT_CONTINUATION);
15899 row->continued_p = 1;
15900
15901 it->current_x = x_before;
15902 it->continuation_lines_width += x;
15903 extend_face_to_end_of_line (it);
15904
15905 if (nglyphs > 1 && i > 0)
15906 {
15907 row->ends_in_middle_of_char_p = 1;
15908 it->starts_in_middle_of_char_p = 1;
15909 }
15910
15911 /* Restore the height to what it was before the
15912 element not fitting on the line. */
15913 it->max_ascent = ascent;
15914 it->max_descent = descent;
15915 it->max_phys_ascent = phys_ascent;
15916 it->max_phys_descent = phys_descent;
15917 }
15918
15919 break;
15920 }
15921 else if (new_x > it->first_visible_x)
15922 {
15923 /* Increment number of glyphs actually displayed. */
15924 ++it->hpos;
15925
15926 if (x < it->first_visible_x)
15927 /* Glyph is partially visible, i.e. row starts at
15928 negative X position. */
15929 row->x = x - it->first_visible_x;
15930 }
15931 else
15932 {
15933 /* Glyph is completely off the left margin of the
15934 window. This should not happen because of the
15935 move_it_in_display_line at the start of this
15936 function, unless the text display area of the
15937 window is empty. */
15938 xassert (it->first_visible_x <= it->last_visible_x);
15939 }
15940 }
15941
15942 row->ascent = max (row->ascent, it->max_ascent);
15943 row->height = max (row->height, it->max_ascent + it->max_descent);
15944 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15945 row->phys_height = max (row->phys_height,
15946 it->max_phys_ascent + it->max_phys_descent);
15947 row->extra_line_spacing = max (row->extra_line_spacing,
15948 it->max_extra_line_spacing);
15949
15950 /* End of this display line if row is continued. */
15951 if (row->continued_p || row->ends_at_zv_p)
15952 break;
15953 }
15954
15955 at_end_of_line:
15956 /* Is this a line end? If yes, we're also done, after making
15957 sure that a non-default face is extended up to the right
15958 margin of the window. */
15959 if (ITERATOR_AT_END_OF_LINE_P (it))
15960 {
15961 int used_before = row->used[TEXT_AREA];
15962
15963 row->ends_in_newline_from_string_p = STRINGP (it->object);
15964
15965 #ifdef HAVE_WINDOW_SYSTEM
15966 /* Add a space at the end of the line that is used to
15967 display the cursor there. */
15968 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15969 append_space_for_newline (it, 0);
15970 #endif /* HAVE_WINDOW_SYSTEM */
15971
15972 /* Extend the face to the end of the line. */
15973 extend_face_to_end_of_line (it);
15974
15975 /* Make sure we have the position. */
15976 if (used_before == 0)
15977 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15978
15979 /* Consume the line end. This skips over invisible lines. */
15980 set_iterator_to_next (it, 1);
15981 it->continuation_lines_width = 0;
15982 break;
15983 }
15984
15985 /* Proceed with next display element. Note that this skips
15986 over lines invisible because of selective display. */
15987 set_iterator_to_next (it, 1);
15988
15989 /* If we truncate lines, we are done when the last displayed
15990 glyphs reach past the right margin of the window. */
15991 if (it->truncate_lines_p
15992 && (FRAME_WINDOW_P (it->f)
15993 ? (it->current_x >= it->last_visible_x)
15994 : (it->current_x > it->last_visible_x)))
15995 {
15996 /* Maybe add truncation glyphs. */
15997 if (!FRAME_WINDOW_P (it->f))
15998 {
15999 int i, n;
16000
16001 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16002 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16003 break;
16004
16005 for (n = row->used[TEXT_AREA]; i < n; ++i)
16006 {
16007 row->used[TEXT_AREA] = i;
16008 produce_special_glyphs (it, IT_TRUNCATION);
16009 }
16010 }
16011 #ifdef HAVE_WINDOW_SYSTEM
16012 else
16013 {
16014 /* Don't truncate if we can overflow newline into fringe. */
16015 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16016 {
16017 if (!get_next_display_element (it))
16018 {
16019 it->continuation_lines_width = 0;
16020 row->ends_at_zv_p = 1;
16021 row->exact_window_width_line_p = 1;
16022 break;
16023 }
16024 if (ITERATOR_AT_END_OF_LINE_P (it))
16025 {
16026 row->exact_window_width_line_p = 1;
16027 goto at_end_of_line;
16028 }
16029 }
16030 }
16031 #endif /* HAVE_WINDOW_SYSTEM */
16032
16033 row->truncated_on_right_p = 1;
16034 it->continuation_lines_width = 0;
16035 reseat_at_next_visible_line_start (it, 0);
16036 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16037 it->hpos = hpos_before;
16038 it->current_x = x_before;
16039 break;
16040 }
16041 }
16042
16043 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16044 at the left window margin. */
16045 if (it->first_visible_x
16046 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16047 {
16048 if (!FRAME_WINDOW_P (it->f))
16049 insert_left_trunc_glyphs (it);
16050 row->truncated_on_left_p = 1;
16051 }
16052
16053 /* If the start of this line is the overlay arrow-position, then
16054 mark this glyph row as the one containing the overlay arrow.
16055 This is clearly a mess with variable size fonts. It would be
16056 better to let it be displayed like cursors under X. */
16057 if ((row->displays_text_p || !overlay_arrow_seen)
16058 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16059 !NILP (overlay_arrow_string)))
16060 {
16061 /* Overlay arrow in window redisplay is a fringe bitmap. */
16062 if (STRINGP (overlay_arrow_string))
16063 {
16064 struct glyph_row *arrow_row
16065 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16066 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16067 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16068 struct glyph *p = row->glyphs[TEXT_AREA];
16069 struct glyph *p2, *end;
16070
16071 /* Copy the arrow glyphs. */
16072 while (glyph < arrow_end)
16073 *p++ = *glyph++;
16074
16075 /* Throw away padding glyphs. */
16076 p2 = p;
16077 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16078 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16079 ++p2;
16080 if (p2 > p)
16081 {
16082 while (p2 < end)
16083 *p++ = *p2++;
16084 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16085 }
16086 }
16087 else
16088 {
16089 xassert (INTEGERP (overlay_arrow_string));
16090 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16091 }
16092 overlay_arrow_seen = 1;
16093 }
16094
16095 /* Compute pixel dimensions of this line. */
16096 compute_line_metrics (it);
16097
16098 /* Remember the position at which this line ends. */
16099 row->end = it->current;
16100
16101 /* Record whether this row ends inside an ellipsis. */
16102 row->ends_in_ellipsis_p
16103 = (it->method == GET_FROM_DISPLAY_VECTOR
16104 && it->ellipsis_p);
16105
16106 /* Save fringe bitmaps in this row. */
16107 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16108 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16109 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16110 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16111
16112 it->left_user_fringe_bitmap = 0;
16113 it->left_user_fringe_face_id = 0;
16114 it->right_user_fringe_bitmap = 0;
16115 it->right_user_fringe_face_id = 0;
16116
16117 /* Maybe set the cursor. */
16118 if (it->w->cursor.vpos < 0
16119 && PT >= MATRIX_ROW_START_CHARPOS (row)
16120 && PT <= MATRIX_ROW_END_CHARPOS (row)
16121 && cursor_row_p (it->w, row))
16122 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16123
16124 /* Highlight trailing whitespace. */
16125 if (!NILP (Vshow_trailing_whitespace))
16126 highlight_trailing_whitespace (it->f, it->glyph_row);
16127
16128 /* Prepare for the next line. This line starts horizontally at (X
16129 HPOS) = (0 0). Vertical positions are incremented. As a
16130 convenience for the caller, IT->glyph_row is set to the next
16131 row to be used. */
16132 it->current_x = it->hpos = 0;
16133 it->current_y += row->height;
16134 ++it->vpos;
16135 ++it->glyph_row;
16136 it->start = it->current;
16137 return row->displays_text_p;
16138 }
16139
16140
16141 \f
16142 /***********************************************************************
16143 Menu Bar
16144 ***********************************************************************/
16145
16146 /* Redisplay the menu bar in the frame for window W.
16147
16148 The menu bar of X frames that don't have X toolkit support is
16149 displayed in a special window W->frame->menu_bar_window.
16150
16151 The menu bar of terminal frames is treated specially as far as
16152 glyph matrices are concerned. Menu bar lines are not part of
16153 windows, so the update is done directly on the frame matrix rows
16154 for the menu bar. */
16155
16156 static void
16157 display_menu_bar (w)
16158 struct window *w;
16159 {
16160 struct frame *f = XFRAME (WINDOW_FRAME (w));
16161 struct it it;
16162 Lisp_Object items;
16163 int i;
16164
16165 /* Don't do all this for graphical frames. */
16166 #ifdef HAVE_NTGUI
16167 if (!NILP (Vwindow_system))
16168 return;
16169 #endif
16170 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16171 if (FRAME_X_P (f))
16172 return;
16173 #endif
16174 #ifdef MAC_OS
16175 if (FRAME_MAC_P (f))
16176 return;
16177 #endif
16178
16179 #ifdef USE_X_TOOLKIT
16180 xassert (!FRAME_WINDOW_P (f));
16181 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16182 it.first_visible_x = 0;
16183 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16184 #else /* not USE_X_TOOLKIT */
16185 if (FRAME_WINDOW_P (f))
16186 {
16187 /* Menu bar lines are displayed in the desired matrix of the
16188 dummy window menu_bar_window. */
16189 struct window *menu_w;
16190 xassert (WINDOWP (f->menu_bar_window));
16191 menu_w = XWINDOW (f->menu_bar_window);
16192 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16193 MENU_FACE_ID);
16194 it.first_visible_x = 0;
16195 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16196 }
16197 else
16198 {
16199 /* This is a TTY frame, i.e. character hpos/vpos are used as
16200 pixel x/y. */
16201 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16202 MENU_FACE_ID);
16203 it.first_visible_x = 0;
16204 it.last_visible_x = FRAME_COLS (f);
16205 }
16206 #endif /* not USE_X_TOOLKIT */
16207
16208 if (! mode_line_inverse_video)
16209 /* Force the menu-bar to be displayed in the default face. */
16210 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16211
16212 /* Clear all rows of the menu bar. */
16213 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16214 {
16215 struct glyph_row *row = it.glyph_row + i;
16216 clear_glyph_row (row);
16217 row->enabled_p = 1;
16218 row->full_width_p = 1;
16219 }
16220
16221 /* Display all items of the menu bar. */
16222 items = FRAME_MENU_BAR_ITEMS (it.f);
16223 for (i = 0; i < XVECTOR (items)->size; i += 4)
16224 {
16225 Lisp_Object string;
16226
16227 /* Stop at nil string. */
16228 string = AREF (items, i + 1);
16229 if (NILP (string))
16230 break;
16231
16232 /* Remember where item was displayed. */
16233 AREF (items, i + 3) = make_number (it.hpos);
16234
16235 /* Display the item, pad with one space. */
16236 if (it.current_x < it.last_visible_x)
16237 display_string (NULL, string, Qnil, 0, 0, &it,
16238 SCHARS (string) + 1, 0, 0, -1);
16239 }
16240
16241 /* Fill out the line with spaces. */
16242 if (it.current_x < it.last_visible_x)
16243 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16244
16245 /* Compute the total height of the lines. */
16246 compute_line_metrics (&it);
16247 }
16248
16249
16250 \f
16251 /***********************************************************************
16252 Mode Line
16253 ***********************************************************************/
16254
16255 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16256 FORCE is non-zero, redisplay mode lines unconditionally.
16257 Otherwise, redisplay only mode lines that are garbaged. Value is
16258 the number of windows whose mode lines were redisplayed. */
16259
16260 static int
16261 redisplay_mode_lines (window, force)
16262 Lisp_Object window;
16263 int force;
16264 {
16265 int nwindows = 0;
16266
16267 while (!NILP (window))
16268 {
16269 struct window *w = XWINDOW (window);
16270
16271 if (WINDOWP (w->hchild))
16272 nwindows += redisplay_mode_lines (w->hchild, force);
16273 else if (WINDOWP (w->vchild))
16274 nwindows += redisplay_mode_lines (w->vchild, force);
16275 else if (force
16276 || FRAME_GARBAGED_P (XFRAME (w->frame))
16277 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16278 {
16279 struct text_pos lpoint;
16280 struct buffer *old = current_buffer;
16281
16282 /* Set the window's buffer for the mode line display. */
16283 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16284 set_buffer_internal_1 (XBUFFER (w->buffer));
16285
16286 /* Point refers normally to the selected window. For any
16287 other window, set up appropriate value. */
16288 if (!EQ (window, selected_window))
16289 {
16290 struct text_pos pt;
16291
16292 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16293 if (CHARPOS (pt) < BEGV)
16294 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16295 else if (CHARPOS (pt) > (ZV - 1))
16296 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16297 else
16298 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16299 }
16300
16301 /* Display mode lines. */
16302 clear_glyph_matrix (w->desired_matrix);
16303 if (display_mode_lines (w))
16304 {
16305 ++nwindows;
16306 w->must_be_updated_p = 1;
16307 }
16308
16309 /* Restore old settings. */
16310 set_buffer_internal_1 (old);
16311 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16312 }
16313
16314 window = w->next;
16315 }
16316
16317 return nwindows;
16318 }
16319
16320
16321 /* Display the mode and/or top line of window W. Value is the number
16322 of mode lines displayed. */
16323
16324 static int
16325 display_mode_lines (w)
16326 struct window *w;
16327 {
16328 Lisp_Object old_selected_window, old_selected_frame;
16329 int n = 0;
16330
16331 old_selected_frame = selected_frame;
16332 selected_frame = w->frame;
16333 old_selected_window = selected_window;
16334 XSETWINDOW (selected_window, w);
16335
16336 /* These will be set while the mode line specs are processed. */
16337 line_number_displayed = 0;
16338 w->column_number_displayed = Qnil;
16339
16340 if (WINDOW_WANTS_MODELINE_P (w))
16341 {
16342 struct window *sel_w = XWINDOW (old_selected_window);
16343
16344 /* Select mode line face based on the real selected window. */
16345 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16346 current_buffer->mode_line_format);
16347 ++n;
16348 }
16349
16350 if (WINDOW_WANTS_HEADER_LINE_P (w))
16351 {
16352 display_mode_line (w, HEADER_LINE_FACE_ID,
16353 current_buffer->header_line_format);
16354 ++n;
16355 }
16356
16357 selected_frame = old_selected_frame;
16358 selected_window = old_selected_window;
16359 return n;
16360 }
16361
16362
16363 /* Display mode or top line of window W. FACE_ID specifies which line
16364 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16365 FORMAT is the mode line format to display. Value is the pixel
16366 height of the mode line displayed. */
16367
16368 static int
16369 display_mode_line (w, face_id, format)
16370 struct window *w;
16371 enum face_id face_id;
16372 Lisp_Object format;
16373 {
16374 struct it it;
16375 struct face *face;
16376 int count = SPECPDL_INDEX ();
16377
16378 init_iterator (&it, w, -1, -1, NULL, face_id);
16379 prepare_desired_row (it.glyph_row);
16380
16381 it.glyph_row->mode_line_p = 1;
16382
16383 if (! mode_line_inverse_video)
16384 /* Force the mode-line to be displayed in the default face. */
16385 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16386
16387 record_unwind_protect (unwind_format_mode_line,
16388 format_mode_line_unwind_data (NULL, 0));
16389
16390 mode_line_target = MODE_LINE_DISPLAY;
16391
16392 /* Temporarily make frame's keyboard the current kboard so that
16393 kboard-local variables in the mode_line_format will get the right
16394 values. */
16395 push_frame_kboard (it.f);
16396 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16397 pop_frame_kboard ();
16398
16399 unbind_to (count, Qnil);
16400
16401 /* Fill up with spaces. */
16402 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16403
16404 compute_line_metrics (&it);
16405 it.glyph_row->full_width_p = 1;
16406 it.glyph_row->continued_p = 0;
16407 it.glyph_row->truncated_on_left_p = 0;
16408 it.glyph_row->truncated_on_right_p = 0;
16409
16410 /* Make a 3D mode-line have a shadow at its right end. */
16411 face = FACE_FROM_ID (it.f, face_id);
16412 extend_face_to_end_of_line (&it);
16413 if (face->box != FACE_NO_BOX)
16414 {
16415 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16416 + it.glyph_row->used[TEXT_AREA] - 1);
16417 last->right_box_line_p = 1;
16418 }
16419
16420 return it.glyph_row->height;
16421 }
16422
16423 /* Move element ELT in LIST to the front of LIST.
16424 Return the updated list. */
16425
16426 static Lisp_Object
16427 move_elt_to_front (elt, list)
16428 Lisp_Object elt, list;
16429 {
16430 register Lisp_Object tail, prev;
16431 register Lisp_Object tem;
16432
16433 tail = list;
16434 prev = Qnil;
16435 while (CONSP (tail))
16436 {
16437 tem = XCAR (tail);
16438
16439 if (EQ (elt, tem))
16440 {
16441 /* Splice out the link TAIL. */
16442 if (NILP (prev))
16443 list = XCDR (tail);
16444 else
16445 Fsetcdr (prev, XCDR (tail));
16446
16447 /* Now make it the first. */
16448 Fsetcdr (tail, list);
16449 return tail;
16450 }
16451 else
16452 prev = tail;
16453 tail = XCDR (tail);
16454 QUIT;
16455 }
16456
16457 /* Not found--return unchanged LIST. */
16458 return list;
16459 }
16460
16461 /* Contribute ELT to the mode line for window IT->w. How it
16462 translates into text depends on its data type.
16463
16464 IT describes the display environment in which we display, as usual.
16465
16466 DEPTH is the depth in recursion. It is used to prevent
16467 infinite recursion here.
16468
16469 FIELD_WIDTH is the number of characters the display of ELT should
16470 occupy in the mode line, and PRECISION is the maximum number of
16471 characters to display from ELT's representation. See
16472 display_string for details.
16473
16474 Returns the hpos of the end of the text generated by ELT.
16475
16476 PROPS is a property list to add to any string we encounter.
16477
16478 If RISKY is nonzero, remove (disregard) any properties in any string
16479 we encounter, and ignore :eval and :propertize.
16480
16481 The global variable `mode_line_target' determines whether the
16482 output is passed to `store_mode_line_noprop',
16483 `store_mode_line_string', or `display_string'. */
16484
16485 static int
16486 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16487 struct it *it;
16488 int depth;
16489 int field_width, precision;
16490 Lisp_Object elt, props;
16491 int risky;
16492 {
16493 int n = 0, field, prec;
16494 int literal = 0;
16495
16496 tail_recurse:
16497 if (depth > 100)
16498 elt = build_string ("*too-deep*");
16499
16500 depth++;
16501
16502 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16503 {
16504 case Lisp_String:
16505 {
16506 /* A string: output it and check for %-constructs within it. */
16507 unsigned char c;
16508 int offset = 0;
16509
16510 if (SCHARS (elt) > 0
16511 && (!NILP (props) || risky))
16512 {
16513 Lisp_Object oprops, aelt;
16514 oprops = Ftext_properties_at (make_number (0), elt);
16515
16516 /* If the starting string's properties are not what
16517 we want, translate the string. Also, if the string
16518 is risky, do that anyway. */
16519
16520 if (NILP (Fequal (props, oprops)) || risky)
16521 {
16522 /* If the starting string has properties,
16523 merge the specified ones onto the existing ones. */
16524 if (! NILP (oprops) && !risky)
16525 {
16526 Lisp_Object tem;
16527
16528 oprops = Fcopy_sequence (oprops);
16529 tem = props;
16530 while (CONSP (tem))
16531 {
16532 oprops = Fplist_put (oprops, XCAR (tem),
16533 XCAR (XCDR (tem)));
16534 tem = XCDR (XCDR (tem));
16535 }
16536 props = oprops;
16537 }
16538
16539 aelt = Fassoc (elt, mode_line_proptrans_alist);
16540 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16541 {
16542 /* AELT is what we want. Move it to the front
16543 without consing. */
16544 elt = XCAR (aelt);
16545 mode_line_proptrans_alist
16546 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16547 }
16548 else
16549 {
16550 Lisp_Object tem;
16551
16552 /* If AELT has the wrong props, it is useless.
16553 so get rid of it. */
16554 if (! NILP (aelt))
16555 mode_line_proptrans_alist
16556 = Fdelq (aelt, mode_line_proptrans_alist);
16557
16558 elt = Fcopy_sequence (elt);
16559 Fset_text_properties (make_number (0), Flength (elt),
16560 props, elt);
16561 /* Add this item to mode_line_proptrans_alist. */
16562 mode_line_proptrans_alist
16563 = Fcons (Fcons (elt, props),
16564 mode_line_proptrans_alist);
16565 /* Truncate mode_line_proptrans_alist
16566 to at most 50 elements. */
16567 tem = Fnthcdr (make_number (50),
16568 mode_line_proptrans_alist);
16569 if (! NILP (tem))
16570 XSETCDR (tem, Qnil);
16571 }
16572 }
16573 }
16574
16575 offset = 0;
16576
16577 if (literal)
16578 {
16579 prec = precision - n;
16580 switch (mode_line_target)
16581 {
16582 case MODE_LINE_NOPROP:
16583 case MODE_LINE_TITLE:
16584 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16585 break;
16586 case MODE_LINE_STRING:
16587 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16588 break;
16589 case MODE_LINE_DISPLAY:
16590 n += display_string (NULL, elt, Qnil, 0, 0, it,
16591 0, prec, 0, STRING_MULTIBYTE (elt));
16592 break;
16593 }
16594
16595 break;
16596 }
16597
16598 /* Handle the non-literal case. */
16599
16600 while ((precision <= 0 || n < precision)
16601 && SREF (elt, offset) != 0
16602 && (mode_line_target != MODE_LINE_DISPLAY
16603 || it->current_x < it->last_visible_x))
16604 {
16605 int last_offset = offset;
16606
16607 /* Advance to end of string or next format specifier. */
16608 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16609 ;
16610
16611 if (offset - 1 != last_offset)
16612 {
16613 int nchars, nbytes;
16614
16615 /* Output to end of string or up to '%'. Field width
16616 is length of string. Don't output more than
16617 PRECISION allows us. */
16618 offset--;
16619
16620 prec = c_string_width (SDATA (elt) + last_offset,
16621 offset - last_offset, precision - n,
16622 &nchars, &nbytes);
16623
16624 switch (mode_line_target)
16625 {
16626 case MODE_LINE_NOPROP:
16627 case MODE_LINE_TITLE:
16628 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16629 break;
16630 case MODE_LINE_STRING:
16631 {
16632 int bytepos = last_offset;
16633 int charpos = string_byte_to_char (elt, bytepos);
16634 int endpos = (precision <= 0
16635 ? string_byte_to_char (elt, offset)
16636 : charpos + nchars);
16637
16638 n += store_mode_line_string (NULL,
16639 Fsubstring (elt, make_number (charpos),
16640 make_number (endpos)),
16641 0, 0, 0, Qnil);
16642 }
16643 break;
16644 case MODE_LINE_DISPLAY:
16645 {
16646 int bytepos = last_offset;
16647 int charpos = string_byte_to_char (elt, bytepos);
16648
16649 if (precision <= 0)
16650 nchars = string_byte_to_char (elt, offset) - charpos;
16651 n += display_string (NULL, elt, Qnil, 0, charpos,
16652 it, 0, nchars, 0,
16653 STRING_MULTIBYTE (elt));
16654 }
16655 break;
16656 }
16657 }
16658 else /* c == '%' */
16659 {
16660 int percent_position = offset;
16661
16662 /* Get the specified minimum width. Zero means
16663 don't pad. */
16664 field = 0;
16665 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16666 field = field * 10 + c - '0';
16667
16668 /* Don't pad beyond the total padding allowed. */
16669 if (field_width - n > 0 && field > field_width - n)
16670 field = field_width - n;
16671
16672 /* Note that either PRECISION <= 0 or N < PRECISION. */
16673 prec = precision - n;
16674
16675 if (c == 'M')
16676 n += display_mode_element (it, depth, field, prec,
16677 Vglobal_mode_string, props,
16678 risky);
16679 else if (c != 0)
16680 {
16681 int multibyte;
16682 int bytepos, charpos;
16683 unsigned char *spec;
16684
16685 bytepos = percent_position;
16686 charpos = (STRING_MULTIBYTE (elt)
16687 ? string_byte_to_char (elt, bytepos)
16688 : bytepos);
16689
16690 spec
16691 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16692
16693 switch (mode_line_target)
16694 {
16695 case MODE_LINE_NOPROP:
16696 case MODE_LINE_TITLE:
16697 n += store_mode_line_noprop (spec, field, prec);
16698 break;
16699 case MODE_LINE_STRING:
16700 {
16701 int len = strlen (spec);
16702 Lisp_Object tem = make_string (spec, len);
16703 props = Ftext_properties_at (make_number (charpos), elt);
16704 /* Should only keep face property in props */
16705 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16706 }
16707 break;
16708 case MODE_LINE_DISPLAY:
16709 {
16710 int nglyphs_before, nwritten;
16711
16712 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16713 nwritten = display_string (spec, Qnil, elt,
16714 charpos, 0, it,
16715 field, prec, 0,
16716 multibyte);
16717
16718 /* Assign to the glyphs written above the
16719 string where the `%x' came from, position
16720 of the `%'. */
16721 if (nwritten > 0)
16722 {
16723 struct glyph *glyph
16724 = (it->glyph_row->glyphs[TEXT_AREA]
16725 + nglyphs_before);
16726 int i;
16727
16728 for (i = 0; i < nwritten; ++i)
16729 {
16730 glyph[i].object = elt;
16731 glyph[i].charpos = charpos;
16732 }
16733
16734 n += nwritten;
16735 }
16736 }
16737 break;
16738 }
16739 }
16740 else /* c == 0 */
16741 break;
16742 }
16743 }
16744 }
16745 break;
16746
16747 case Lisp_Symbol:
16748 /* A symbol: process the value of the symbol recursively
16749 as if it appeared here directly. Avoid error if symbol void.
16750 Special case: if value of symbol is a string, output the string
16751 literally. */
16752 {
16753 register Lisp_Object tem;
16754
16755 /* If the variable is not marked as risky to set
16756 then its contents are risky to use. */
16757 if (NILP (Fget (elt, Qrisky_local_variable)))
16758 risky = 1;
16759
16760 tem = Fboundp (elt);
16761 if (!NILP (tem))
16762 {
16763 tem = Fsymbol_value (elt);
16764 /* If value is a string, output that string literally:
16765 don't check for % within it. */
16766 if (STRINGP (tem))
16767 literal = 1;
16768
16769 if (!EQ (tem, elt))
16770 {
16771 /* Give up right away for nil or t. */
16772 elt = tem;
16773 goto tail_recurse;
16774 }
16775 }
16776 }
16777 break;
16778
16779 case Lisp_Cons:
16780 {
16781 register Lisp_Object car, tem;
16782
16783 /* A cons cell: five distinct cases.
16784 If first element is :eval or :propertize, do something special.
16785 If first element is a string or a cons, process all the elements
16786 and effectively concatenate them.
16787 If first element is a negative number, truncate displaying cdr to
16788 at most that many characters. If positive, pad (with spaces)
16789 to at least that many characters.
16790 If first element is a symbol, process the cadr or caddr recursively
16791 according to whether the symbol's value is non-nil or nil. */
16792 car = XCAR (elt);
16793 if (EQ (car, QCeval))
16794 {
16795 /* An element of the form (:eval FORM) means evaluate FORM
16796 and use the result as mode line elements. */
16797
16798 if (risky)
16799 break;
16800
16801 if (CONSP (XCDR (elt)))
16802 {
16803 Lisp_Object spec;
16804 spec = safe_eval (XCAR (XCDR (elt)));
16805 n += display_mode_element (it, depth, field_width - n,
16806 precision - n, spec, props,
16807 risky);
16808 }
16809 }
16810 else if (EQ (car, QCpropertize))
16811 {
16812 /* An element of the form (:propertize ELT PROPS...)
16813 means display ELT but applying properties PROPS. */
16814
16815 if (risky)
16816 break;
16817
16818 if (CONSP (XCDR (elt)))
16819 n += display_mode_element (it, depth, field_width - n,
16820 precision - n, XCAR (XCDR (elt)),
16821 XCDR (XCDR (elt)), risky);
16822 }
16823 else if (SYMBOLP (car))
16824 {
16825 tem = Fboundp (car);
16826 elt = XCDR (elt);
16827 if (!CONSP (elt))
16828 goto invalid;
16829 /* elt is now the cdr, and we know it is a cons cell.
16830 Use its car if CAR has a non-nil value. */
16831 if (!NILP (tem))
16832 {
16833 tem = Fsymbol_value (car);
16834 if (!NILP (tem))
16835 {
16836 elt = XCAR (elt);
16837 goto tail_recurse;
16838 }
16839 }
16840 /* Symbol's value is nil (or symbol is unbound)
16841 Get the cddr of the original list
16842 and if possible find the caddr and use that. */
16843 elt = XCDR (elt);
16844 if (NILP (elt))
16845 break;
16846 else if (!CONSP (elt))
16847 goto invalid;
16848 elt = XCAR (elt);
16849 goto tail_recurse;
16850 }
16851 else if (INTEGERP (car))
16852 {
16853 register int lim = XINT (car);
16854 elt = XCDR (elt);
16855 if (lim < 0)
16856 {
16857 /* Negative int means reduce maximum width. */
16858 if (precision <= 0)
16859 precision = -lim;
16860 else
16861 precision = min (precision, -lim);
16862 }
16863 else if (lim > 0)
16864 {
16865 /* Padding specified. Don't let it be more than
16866 current maximum. */
16867 if (precision > 0)
16868 lim = min (precision, lim);
16869
16870 /* If that's more padding than already wanted, queue it.
16871 But don't reduce padding already specified even if
16872 that is beyond the current truncation point. */
16873 field_width = max (lim, field_width);
16874 }
16875 goto tail_recurse;
16876 }
16877 else if (STRINGP (car) || CONSP (car))
16878 {
16879 register int limit = 50;
16880 /* Limit is to protect against circular lists. */
16881 while (CONSP (elt)
16882 && --limit > 0
16883 && (precision <= 0 || n < precision))
16884 {
16885 n += display_mode_element (it, depth,
16886 /* Do padding only after the last
16887 element in the list. */
16888 (! CONSP (XCDR (elt))
16889 ? field_width - n
16890 : 0),
16891 precision - n, XCAR (elt),
16892 props, risky);
16893 elt = XCDR (elt);
16894 }
16895 }
16896 }
16897 break;
16898
16899 default:
16900 invalid:
16901 elt = build_string ("*invalid*");
16902 goto tail_recurse;
16903 }
16904
16905 /* Pad to FIELD_WIDTH. */
16906 if (field_width > 0 && n < field_width)
16907 {
16908 switch (mode_line_target)
16909 {
16910 case MODE_LINE_NOPROP:
16911 case MODE_LINE_TITLE:
16912 n += store_mode_line_noprop ("", field_width - n, 0);
16913 break;
16914 case MODE_LINE_STRING:
16915 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16916 break;
16917 case MODE_LINE_DISPLAY:
16918 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16919 0, 0, 0);
16920 break;
16921 }
16922 }
16923
16924 return n;
16925 }
16926
16927 /* Store a mode-line string element in mode_line_string_list.
16928
16929 If STRING is non-null, display that C string. Otherwise, the Lisp
16930 string LISP_STRING is displayed.
16931
16932 FIELD_WIDTH is the minimum number of output glyphs to produce.
16933 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16934 with spaces. FIELD_WIDTH <= 0 means don't pad.
16935
16936 PRECISION is the maximum number of characters to output from
16937 STRING. PRECISION <= 0 means don't truncate the string.
16938
16939 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16940 properties to the string.
16941
16942 PROPS are the properties to add to the string.
16943 The mode_line_string_face face property is always added to the string.
16944 */
16945
16946 static int
16947 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16948 char *string;
16949 Lisp_Object lisp_string;
16950 int copy_string;
16951 int field_width;
16952 int precision;
16953 Lisp_Object props;
16954 {
16955 int len;
16956 int n = 0;
16957
16958 if (string != NULL)
16959 {
16960 len = strlen (string);
16961 if (precision > 0 && len > precision)
16962 len = precision;
16963 lisp_string = make_string (string, len);
16964 if (NILP (props))
16965 props = mode_line_string_face_prop;
16966 else if (!NILP (mode_line_string_face))
16967 {
16968 Lisp_Object face = Fplist_get (props, Qface);
16969 props = Fcopy_sequence (props);
16970 if (NILP (face))
16971 face = mode_line_string_face;
16972 else
16973 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16974 props = Fplist_put (props, Qface, face);
16975 }
16976 Fadd_text_properties (make_number (0), make_number (len),
16977 props, lisp_string);
16978 }
16979 else
16980 {
16981 len = XFASTINT (Flength (lisp_string));
16982 if (precision > 0 && len > precision)
16983 {
16984 len = precision;
16985 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16986 precision = -1;
16987 }
16988 if (!NILP (mode_line_string_face))
16989 {
16990 Lisp_Object face;
16991 if (NILP (props))
16992 props = Ftext_properties_at (make_number (0), lisp_string);
16993 face = Fplist_get (props, Qface);
16994 if (NILP (face))
16995 face = mode_line_string_face;
16996 else
16997 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16998 props = Fcons (Qface, Fcons (face, Qnil));
16999 if (copy_string)
17000 lisp_string = Fcopy_sequence (lisp_string);
17001 }
17002 if (!NILP (props))
17003 Fadd_text_properties (make_number (0), make_number (len),
17004 props, lisp_string);
17005 }
17006
17007 if (len > 0)
17008 {
17009 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17010 n += len;
17011 }
17012
17013 if (field_width > len)
17014 {
17015 field_width -= len;
17016 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17017 if (!NILP (props))
17018 Fadd_text_properties (make_number (0), make_number (field_width),
17019 props, lisp_string);
17020 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17021 n += field_width;
17022 }
17023
17024 return n;
17025 }
17026
17027
17028 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17029 1, 4, 0,
17030 doc: /* Format a string out of a mode line format specification.
17031 First arg FORMAT specifies the mode line format (see `mode-line-format'
17032 for details) to use.
17033
17034 Optional second arg FACE specifies the face property to put
17035 on all characters for which no face is specified.
17036 t means whatever face the window's mode line currently uses
17037 \(either `mode-line' or `mode-line-inactive', depending).
17038 nil means the default is no face property.
17039 If FACE is an integer, the value string has no text properties.
17040
17041 Optional third and fourth args WINDOW and BUFFER specify the window
17042 and buffer to use as the context for the formatting (defaults
17043 are the selected window and the window's buffer). */)
17044 (format, face, window, buffer)
17045 Lisp_Object format, face, window, buffer;
17046 {
17047 struct it it;
17048 int len;
17049 struct window *w;
17050 struct buffer *old_buffer = NULL;
17051 int face_id = -1;
17052 int no_props = INTEGERP (face);
17053 int count = SPECPDL_INDEX ();
17054 Lisp_Object str;
17055 int string_start = 0;
17056
17057 if (NILP (window))
17058 window = selected_window;
17059 CHECK_WINDOW (window);
17060 w = XWINDOW (window);
17061
17062 if (NILP (buffer))
17063 buffer = w->buffer;
17064 CHECK_BUFFER (buffer);
17065
17066 if (NILP (format))
17067 return build_string ("");
17068
17069 if (no_props)
17070 face = Qnil;
17071
17072 if (!NILP (face))
17073 {
17074 if (EQ (face, Qt))
17075 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17076 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17077 }
17078
17079 if (face_id < 0)
17080 face_id = DEFAULT_FACE_ID;
17081
17082 if (XBUFFER (buffer) != current_buffer)
17083 old_buffer = current_buffer;
17084
17085 /* Save things including mode_line_proptrans_alist,
17086 and set that to nil so that we don't alter the outer value. */
17087 record_unwind_protect (unwind_format_mode_line,
17088 format_mode_line_unwind_data (old_buffer, 1));
17089 mode_line_proptrans_alist = Qnil;
17090
17091 if (old_buffer)
17092 set_buffer_internal_1 (XBUFFER (buffer));
17093
17094 init_iterator (&it, w, -1, -1, NULL, face_id);
17095
17096 if (no_props)
17097 {
17098 mode_line_target = MODE_LINE_NOPROP;
17099 mode_line_string_face_prop = Qnil;
17100 mode_line_string_list = Qnil;
17101 string_start = MODE_LINE_NOPROP_LEN (0);
17102 }
17103 else
17104 {
17105 mode_line_target = MODE_LINE_STRING;
17106 mode_line_string_list = Qnil;
17107 mode_line_string_face = face;
17108 mode_line_string_face_prop
17109 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17110 }
17111
17112 push_frame_kboard (it.f);
17113 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17114 pop_frame_kboard ();
17115
17116 if (no_props)
17117 {
17118 len = MODE_LINE_NOPROP_LEN (string_start);
17119 str = make_string (mode_line_noprop_buf + string_start, len);
17120 }
17121 else
17122 {
17123 mode_line_string_list = Fnreverse (mode_line_string_list);
17124 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17125 make_string ("", 0));
17126 }
17127
17128 unbind_to (count, Qnil);
17129 return str;
17130 }
17131
17132 /* Write a null-terminated, right justified decimal representation of
17133 the positive integer D to BUF using a minimal field width WIDTH. */
17134
17135 static void
17136 pint2str (buf, width, d)
17137 register char *buf;
17138 register int width;
17139 register int d;
17140 {
17141 register char *p = buf;
17142
17143 if (d <= 0)
17144 *p++ = '0';
17145 else
17146 {
17147 while (d > 0)
17148 {
17149 *p++ = d % 10 + '0';
17150 d /= 10;
17151 }
17152 }
17153
17154 for (width -= (int) (p - buf); width > 0; --width)
17155 *p++ = ' ';
17156 *p-- = '\0';
17157 while (p > buf)
17158 {
17159 d = *buf;
17160 *buf++ = *p;
17161 *p-- = d;
17162 }
17163 }
17164
17165 /* Write a null-terminated, right justified decimal and "human
17166 readable" representation of the nonnegative integer D to BUF using
17167 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17168
17169 static const char power_letter[] =
17170 {
17171 0, /* not used */
17172 'k', /* kilo */
17173 'M', /* mega */
17174 'G', /* giga */
17175 'T', /* tera */
17176 'P', /* peta */
17177 'E', /* exa */
17178 'Z', /* zetta */
17179 'Y' /* yotta */
17180 };
17181
17182 static void
17183 pint2hrstr (buf, width, d)
17184 char *buf;
17185 int width;
17186 int d;
17187 {
17188 /* We aim to represent the nonnegative integer D as
17189 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17190 int quotient = d;
17191 int remainder = 0;
17192 /* -1 means: do not use TENTHS. */
17193 int tenths = -1;
17194 int exponent = 0;
17195
17196 /* Length of QUOTIENT.TENTHS as a string. */
17197 int length;
17198
17199 char * psuffix;
17200 char * p;
17201
17202 if (1000 <= quotient)
17203 {
17204 /* Scale to the appropriate EXPONENT. */
17205 do
17206 {
17207 remainder = quotient % 1000;
17208 quotient /= 1000;
17209 exponent++;
17210 }
17211 while (1000 <= quotient);
17212
17213 /* Round to nearest and decide whether to use TENTHS or not. */
17214 if (quotient <= 9)
17215 {
17216 tenths = remainder / 100;
17217 if (50 <= remainder % 100)
17218 {
17219 if (tenths < 9)
17220 tenths++;
17221 else
17222 {
17223 quotient++;
17224 if (quotient == 10)
17225 tenths = -1;
17226 else
17227 tenths = 0;
17228 }
17229 }
17230 }
17231 else
17232 if (500 <= remainder)
17233 {
17234 if (quotient < 999)
17235 quotient++;
17236 else
17237 {
17238 quotient = 1;
17239 exponent++;
17240 tenths = 0;
17241 }
17242 }
17243 }
17244
17245 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17246 if (tenths == -1 && quotient <= 99)
17247 if (quotient <= 9)
17248 length = 1;
17249 else
17250 length = 2;
17251 else
17252 length = 3;
17253 p = psuffix = buf + max (width, length);
17254
17255 /* Print EXPONENT. */
17256 if (exponent)
17257 *psuffix++ = power_letter[exponent];
17258 *psuffix = '\0';
17259
17260 /* Print TENTHS. */
17261 if (tenths >= 0)
17262 {
17263 *--p = '0' + tenths;
17264 *--p = '.';
17265 }
17266
17267 /* Print QUOTIENT. */
17268 do
17269 {
17270 int digit = quotient % 10;
17271 *--p = '0' + digit;
17272 }
17273 while ((quotient /= 10) != 0);
17274
17275 /* Print leading spaces. */
17276 while (buf < p)
17277 *--p = ' ';
17278 }
17279
17280 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17281 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17282 type of CODING_SYSTEM. Return updated pointer into BUF. */
17283
17284 static unsigned char invalid_eol_type[] = "(*invalid*)";
17285
17286 static char *
17287 decode_mode_spec_coding (coding_system, buf, eol_flag)
17288 Lisp_Object coding_system;
17289 register char *buf;
17290 int eol_flag;
17291 {
17292 Lisp_Object val;
17293 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17294 const unsigned char *eol_str;
17295 int eol_str_len;
17296 /* The EOL conversion we are using. */
17297 Lisp_Object eoltype;
17298
17299 val = Fget (coding_system, Qcoding_system);
17300 eoltype = Qnil;
17301
17302 if (!VECTORP (val)) /* Not yet decided. */
17303 {
17304 if (multibyte)
17305 *buf++ = '-';
17306 if (eol_flag)
17307 eoltype = eol_mnemonic_undecided;
17308 /* Don't mention EOL conversion if it isn't decided. */
17309 }
17310 else
17311 {
17312 Lisp_Object eolvalue;
17313
17314 eolvalue = Fget (coding_system, Qeol_type);
17315
17316 if (multibyte)
17317 *buf++ = XFASTINT (AREF (val, 1));
17318
17319 if (eol_flag)
17320 {
17321 /* The EOL conversion that is normal on this system. */
17322
17323 if (NILP (eolvalue)) /* Not yet decided. */
17324 eoltype = eol_mnemonic_undecided;
17325 else if (VECTORP (eolvalue)) /* Not yet decided. */
17326 eoltype = eol_mnemonic_undecided;
17327 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17328 eoltype = (XFASTINT (eolvalue) == 0
17329 ? eol_mnemonic_unix
17330 : (XFASTINT (eolvalue) == 1
17331 ? eol_mnemonic_dos : eol_mnemonic_mac));
17332 }
17333 }
17334
17335 if (eol_flag)
17336 {
17337 /* Mention the EOL conversion if it is not the usual one. */
17338 if (STRINGP (eoltype))
17339 {
17340 eol_str = SDATA (eoltype);
17341 eol_str_len = SBYTES (eoltype);
17342 }
17343 else if (INTEGERP (eoltype)
17344 && CHAR_VALID_P (XINT (eoltype), 0))
17345 {
17346 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17347 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17348 eol_str = tmp;
17349 }
17350 else
17351 {
17352 eol_str = invalid_eol_type;
17353 eol_str_len = sizeof (invalid_eol_type) - 1;
17354 }
17355 bcopy (eol_str, buf, eol_str_len);
17356 buf += eol_str_len;
17357 }
17358
17359 return buf;
17360 }
17361
17362 /* Return a string for the output of a mode line %-spec for window W,
17363 generated by character C. PRECISION >= 0 means don't return a
17364 string longer than that value. FIELD_WIDTH > 0 means pad the
17365 string returned with spaces to that value. Return 1 in *MULTIBYTE
17366 if the result is multibyte text.
17367
17368 Note we operate on the current buffer for most purposes,
17369 the exception being w->base_line_pos. */
17370
17371 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17372
17373 static char *
17374 decode_mode_spec (w, c, field_width, precision, multibyte)
17375 struct window *w;
17376 register int c;
17377 int field_width, precision;
17378 int *multibyte;
17379 {
17380 Lisp_Object obj;
17381 struct frame *f = XFRAME (WINDOW_FRAME (w));
17382 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17383 struct buffer *b = current_buffer;
17384
17385 obj = Qnil;
17386 *multibyte = 0;
17387
17388 switch (c)
17389 {
17390 case '*':
17391 if (!NILP (b->read_only))
17392 return "%";
17393 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17394 return "*";
17395 return "-";
17396
17397 case '+':
17398 /* This differs from %* only for a modified read-only buffer. */
17399 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17400 return "*";
17401 if (!NILP (b->read_only))
17402 return "%";
17403 return "-";
17404
17405 case '&':
17406 /* This differs from %* in ignoring read-only-ness. */
17407 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17408 return "*";
17409 return "-";
17410
17411 case '%':
17412 return "%";
17413
17414 case '[':
17415 {
17416 int i;
17417 char *p;
17418
17419 if (command_loop_level > 5)
17420 return "[[[... ";
17421 p = decode_mode_spec_buf;
17422 for (i = 0; i < command_loop_level; i++)
17423 *p++ = '[';
17424 *p = 0;
17425 return decode_mode_spec_buf;
17426 }
17427
17428 case ']':
17429 {
17430 int i;
17431 char *p;
17432
17433 if (command_loop_level > 5)
17434 return " ...]]]";
17435 p = decode_mode_spec_buf;
17436 for (i = 0; i < command_loop_level; i++)
17437 *p++ = ']';
17438 *p = 0;
17439 return decode_mode_spec_buf;
17440 }
17441
17442 case '-':
17443 {
17444 register int i;
17445
17446 /* Let lots_of_dashes be a string of infinite length. */
17447 if (mode_line_target == MODE_LINE_NOPROP ||
17448 mode_line_target == MODE_LINE_STRING)
17449 return "--";
17450 if (field_width <= 0
17451 || field_width > sizeof (lots_of_dashes))
17452 {
17453 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17454 decode_mode_spec_buf[i] = '-';
17455 decode_mode_spec_buf[i] = '\0';
17456 return decode_mode_spec_buf;
17457 }
17458 else
17459 return lots_of_dashes;
17460 }
17461
17462 case 'b':
17463 obj = b->name;
17464 break;
17465
17466 case 'c':
17467 {
17468 int col = (int) current_column (); /* iftc */
17469 w->column_number_displayed = make_number (col);
17470 pint2str (decode_mode_spec_buf, field_width, col);
17471 return decode_mode_spec_buf;
17472 }
17473
17474 case 'e':
17475 #ifndef SYSTEM_MALLOC
17476 {
17477 if (NILP (Vmemory_full))
17478 return "";
17479 else
17480 return "!MEM FULL! ";
17481 }
17482 #else
17483 return "";
17484 #endif
17485
17486 case 'F':
17487 /* %F displays the frame name. */
17488 if (!NILP (f->title))
17489 return (char *) SDATA (f->title);
17490 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17491 return (char *) SDATA (f->name);
17492 return "Emacs";
17493
17494 case 'f':
17495 obj = b->filename;
17496 break;
17497
17498 case 'i':
17499 {
17500 int size = ZV - BEGV;
17501 pint2str (decode_mode_spec_buf, field_width, size);
17502 return decode_mode_spec_buf;
17503 }
17504
17505 case 'I':
17506 {
17507 int size = ZV - BEGV;
17508 pint2hrstr (decode_mode_spec_buf, field_width, size);
17509 return decode_mode_spec_buf;
17510 }
17511
17512 case 'l':
17513 {
17514 int startpos = XMARKER (w->start)->charpos;
17515 int startpos_byte = marker_byte_position (w->start);
17516 int line, linepos, linepos_byte, topline;
17517 int nlines, junk;
17518 int height = WINDOW_TOTAL_LINES (w);
17519
17520 /* If we decided that this buffer isn't suitable for line numbers,
17521 don't forget that too fast. */
17522 if (EQ (w->base_line_pos, w->buffer))
17523 goto no_value;
17524 /* But do forget it, if the window shows a different buffer now. */
17525 else if (BUFFERP (w->base_line_pos))
17526 w->base_line_pos = Qnil;
17527
17528 /* If the buffer is very big, don't waste time. */
17529 if (INTEGERP (Vline_number_display_limit)
17530 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17531 {
17532 w->base_line_pos = Qnil;
17533 w->base_line_number = Qnil;
17534 goto no_value;
17535 }
17536
17537 if (!NILP (w->base_line_number)
17538 && !NILP (w->base_line_pos)
17539 && XFASTINT (w->base_line_pos) <= startpos)
17540 {
17541 line = XFASTINT (w->base_line_number);
17542 linepos = XFASTINT (w->base_line_pos);
17543 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17544 }
17545 else
17546 {
17547 line = 1;
17548 linepos = BUF_BEGV (b);
17549 linepos_byte = BUF_BEGV_BYTE (b);
17550 }
17551
17552 /* Count lines from base line to window start position. */
17553 nlines = display_count_lines (linepos, linepos_byte,
17554 startpos_byte,
17555 startpos, &junk);
17556
17557 topline = nlines + line;
17558
17559 /* Determine a new base line, if the old one is too close
17560 or too far away, or if we did not have one.
17561 "Too close" means it's plausible a scroll-down would
17562 go back past it. */
17563 if (startpos == BUF_BEGV (b))
17564 {
17565 w->base_line_number = make_number (topline);
17566 w->base_line_pos = make_number (BUF_BEGV (b));
17567 }
17568 else if (nlines < height + 25 || nlines > height * 3 + 50
17569 || linepos == BUF_BEGV (b))
17570 {
17571 int limit = BUF_BEGV (b);
17572 int limit_byte = BUF_BEGV_BYTE (b);
17573 int position;
17574 int distance = (height * 2 + 30) * line_number_display_limit_width;
17575
17576 if (startpos - distance > limit)
17577 {
17578 limit = startpos - distance;
17579 limit_byte = CHAR_TO_BYTE (limit);
17580 }
17581
17582 nlines = display_count_lines (startpos, startpos_byte,
17583 limit_byte,
17584 - (height * 2 + 30),
17585 &position);
17586 /* If we couldn't find the lines we wanted within
17587 line_number_display_limit_width chars per line,
17588 give up on line numbers for this window. */
17589 if (position == limit_byte && limit == startpos - distance)
17590 {
17591 w->base_line_pos = w->buffer;
17592 w->base_line_number = Qnil;
17593 goto no_value;
17594 }
17595
17596 w->base_line_number = make_number (topline - nlines);
17597 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17598 }
17599
17600 /* Now count lines from the start pos to point. */
17601 nlines = display_count_lines (startpos, startpos_byte,
17602 PT_BYTE, PT, &junk);
17603
17604 /* Record that we did display the line number. */
17605 line_number_displayed = 1;
17606
17607 /* Make the string to show. */
17608 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17609 return decode_mode_spec_buf;
17610 no_value:
17611 {
17612 char* p = decode_mode_spec_buf;
17613 int pad = field_width - 2;
17614 while (pad-- > 0)
17615 *p++ = ' ';
17616 *p++ = '?';
17617 *p++ = '?';
17618 *p = '\0';
17619 return decode_mode_spec_buf;
17620 }
17621 }
17622 break;
17623
17624 case 'm':
17625 obj = b->mode_name;
17626 break;
17627
17628 case 'n':
17629 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17630 return " Narrow";
17631 break;
17632
17633 case 'p':
17634 {
17635 int pos = marker_position (w->start);
17636 int total = BUF_ZV (b) - BUF_BEGV (b);
17637
17638 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17639 {
17640 if (pos <= BUF_BEGV (b))
17641 return "All";
17642 else
17643 return "Bottom";
17644 }
17645 else if (pos <= BUF_BEGV (b))
17646 return "Top";
17647 else
17648 {
17649 if (total > 1000000)
17650 /* Do it differently for a large value, to avoid overflow. */
17651 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17652 else
17653 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17654 /* We can't normally display a 3-digit number,
17655 so get us a 2-digit number that is close. */
17656 if (total == 100)
17657 total = 99;
17658 sprintf (decode_mode_spec_buf, "%2d%%", total);
17659 return decode_mode_spec_buf;
17660 }
17661 }
17662
17663 /* Display percentage of size above the bottom of the screen. */
17664 case 'P':
17665 {
17666 int toppos = marker_position (w->start);
17667 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17668 int total = BUF_ZV (b) - BUF_BEGV (b);
17669
17670 if (botpos >= BUF_ZV (b))
17671 {
17672 if (toppos <= BUF_BEGV (b))
17673 return "All";
17674 else
17675 return "Bottom";
17676 }
17677 else
17678 {
17679 if (total > 1000000)
17680 /* Do it differently for a large value, to avoid overflow. */
17681 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17682 else
17683 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17684 /* We can't normally display a 3-digit number,
17685 so get us a 2-digit number that is close. */
17686 if (total == 100)
17687 total = 99;
17688 if (toppos <= BUF_BEGV (b))
17689 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17690 else
17691 sprintf (decode_mode_spec_buf, "%2d%%", total);
17692 return decode_mode_spec_buf;
17693 }
17694 }
17695
17696 case 's':
17697 /* status of process */
17698 obj = Fget_buffer_process (Fcurrent_buffer ());
17699 if (NILP (obj))
17700 return "no process";
17701 #ifdef subprocesses
17702 obj = Fsymbol_name (Fprocess_status (obj));
17703 #endif
17704 break;
17705
17706 case 't': /* indicate TEXT or BINARY */
17707 #ifdef MODE_LINE_BINARY_TEXT
17708 return MODE_LINE_BINARY_TEXT (b);
17709 #else
17710 return "T";
17711 #endif
17712
17713 case 'z':
17714 /* coding-system (not including end-of-line format) */
17715 case 'Z':
17716 /* coding-system (including end-of-line type) */
17717 {
17718 int eol_flag = (c == 'Z');
17719 char *p = decode_mode_spec_buf;
17720
17721 if (! FRAME_WINDOW_P (f))
17722 {
17723 /* No need to mention EOL here--the terminal never needs
17724 to do EOL conversion. */
17725 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17726 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17727 }
17728 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17729 p, eol_flag);
17730
17731 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17732 #ifdef subprocesses
17733 obj = Fget_buffer_process (Fcurrent_buffer ());
17734 if (PROCESSP (obj))
17735 {
17736 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17737 p, eol_flag);
17738 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17739 p, eol_flag);
17740 }
17741 #endif /* subprocesses */
17742 #endif /* 0 */
17743 *p = 0;
17744 return decode_mode_spec_buf;
17745 }
17746 }
17747
17748 if (STRINGP (obj))
17749 {
17750 *multibyte = STRING_MULTIBYTE (obj);
17751 return (char *) SDATA (obj);
17752 }
17753 else
17754 return "";
17755 }
17756
17757
17758 /* Count up to COUNT lines starting from START / START_BYTE.
17759 But don't go beyond LIMIT_BYTE.
17760 Return the number of lines thus found (always nonnegative).
17761
17762 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17763
17764 static int
17765 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17766 int start, start_byte, limit_byte, count;
17767 int *byte_pos_ptr;
17768 {
17769 register unsigned char *cursor;
17770 unsigned char *base;
17771
17772 register int ceiling;
17773 register unsigned char *ceiling_addr;
17774 int orig_count = count;
17775
17776 /* If we are not in selective display mode,
17777 check only for newlines. */
17778 int selective_display = (!NILP (current_buffer->selective_display)
17779 && !INTEGERP (current_buffer->selective_display));
17780
17781 if (count > 0)
17782 {
17783 while (start_byte < limit_byte)
17784 {
17785 ceiling = BUFFER_CEILING_OF (start_byte);
17786 ceiling = min (limit_byte - 1, ceiling);
17787 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17788 base = (cursor = BYTE_POS_ADDR (start_byte));
17789 while (1)
17790 {
17791 if (selective_display)
17792 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17793 ;
17794 else
17795 while (*cursor != '\n' && ++cursor != ceiling_addr)
17796 ;
17797
17798 if (cursor != ceiling_addr)
17799 {
17800 if (--count == 0)
17801 {
17802 start_byte += cursor - base + 1;
17803 *byte_pos_ptr = start_byte;
17804 return orig_count;
17805 }
17806 else
17807 if (++cursor == ceiling_addr)
17808 break;
17809 }
17810 else
17811 break;
17812 }
17813 start_byte += cursor - base;
17814 }
17815 }
17816 else
17817 {
17818 while (start_byte > limit_byte)
17819 {
17820 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17821 ceiling = max (limit_byte, ceiling);
17822 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17823 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17824 while (1)
17825 {
17826 if (selective_display)
17827 while (--cursor != ceiling_addr
17828 && *cursor != '\n' && *cursor != 015)
17829 ;
17830 else
17831 while (--cursor != ceiling_addr && *cursor != '\n')
17832 ;
17833
17834 if (cursor != ceiling_addr)
17835 {
17836 if (++count == 0)
17837 {
17838 start_byte += cursor - base + 1;
17839 *byte_pos_ptr = start_byte;
17840 /* When scanning backwards, we should
17841 not count the newline posterior to which we stop. */
17842 return - orig_count - 1;
17843 }
17844 }
17845 else
17846 break;
17847 }
17848 /* Here we add 1 to compensate for the last decrement
17849 of CURSOR, which took it past the valid range. */
17850 start_byte += cursor - base + 1;
17851 }
17852 }
17853
17854 *byte_pos_ptr = limit_byte;
17855
17856 if (count < 0)
17857 return - orig_count + count;
17858 return orig_count - count;
17859
17860 }
17861
17862
17863 \f
17864 /***********************************************************************
17865 Displaying strings
17866 ***********************************************************************/
17867
17868 /* Display a NUL-terminated string, starting with index START.
17869
17870 If STRING is non-null, display that C string. Otherwise, the Lisp
17871 string LISP_STRING is displayed.
17872
17873 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17874 FACE_STRING. Display STRING or LISP_STRING with the face at
17875 FACE_STRING_POS in FACE_STRING:
17876
17877 Display the string in the environment given by IT, but use the
17878 standard display table, temporarily.
17879
17880 FIELD_WIDTH is the minimum number of output glyphs to produce.
17881 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17882 with spaces. If STRING has more characters, more than FIELD_WIDTH
17883 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17884
17885 PRECISION is the maximum number of characters to output from
17886 STRING. PRECISION < 0 means don't truncate the string.
17887
17888 This is roughly equivalent to printf format specifiers:
17889
17890 FIELD_WIDTH PRECISION PRINTF
17891 ----------------------------------------
17892 -1 -1 %s
17893 -1 10 %.10s
17894 10 -1 %10s
17895 20 10 %20.10s
17896
17897 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17898 display them, and < 0 means obey the current buffer's value of
17899 enable_multibyte_characters.
17900
17901 Value is the number of columns displayed. */
17902
17903 static int
17904 display_string (string, lisp_string, face_string, face_string_pos,
17905 start, it, field_width, precision, max_x, multibyte)
17906 unsigned char *string;
17907 Lisp_Object lisp_string;
17908 Lisp_Object face_string;
17909 int face_string_pos;
17910 int start;
17911 struct it *it;
17912 int field_width, precision, max_x;
17913 int multibyte;
17914 {
17915 int hpos_at_start = it->hpos;
17916 int saved_face_id = it->face_id;
17917 struct glyph_row *row = it->glyph_row;
17918
17919 /* Initialize the iterator IT for iteration over STRING beginning
17920 with index START. */
17921 reseat_to_string (it, string, lisp_string, start,
17922 precision, field_width, multibyte);
17923
17924 /* If displaying STRING, set up the face of the iterator
17925 from LISP_STRING, if that's given. */
17926 if (STRINGP (face_string))
17927 {
17928 int endptr;
17929 struct face *face;
17930
17931 it->face_id
17932 = face_at_string_position (it->w, face_string, face_string_pos,
17933 0, it->region_beg_charpos,
17934 it->region_end_charpos,
17935 &endptr, it->base_face_id, 0);
17936 face = FACE_FROM_ID (it->f, it->face_id);
17937 it->face_box_p = face->box != FACE_NO_BOX;
17938 }
17939
17940 /* Set max_x to the maximum allowed X position. Don't let it go
17941 beyond the right edge of the window. */
17942 if (max_x <= 0)
17943 max_x = it->last_visible_x;
17944 else
17945 max_x = min (max_x, it->last_visible_x);
17946
17947 /* Skip over display elements that are not visible. because IT->w is
17948 hscrolled. */
17949 if (it->current_x < it->first_visible_x)
17950 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17951 MOVE_TO_POS | MOVE_TO_X);
17952
17953 row->ascent = it->max_ascent;
17954 row->height = it->max_ascent + it->max_descent;
17955 row->phys_ascent = it->max_phys_ascent;
17956 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17957 row->extra_line_spacing = it->max_extra_line_spacing;
17958
17959 /* This condition is for the case that we are called with current_x
17960 past last_visible_x. */
17961 while (it->current_x < max_x)
17962 {
17963 int x_before, x, n_glyphs_before, i, nglyphs;
17964
17965 /* Get the next display element. */
17966 if (!get_next_display_element (it))
17967 break;
17968
17969 /* Produce glyphs. */
17970 x_before = it->current_x;
17971 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17972 PRODUCE_GLYPHS (it);
17973
17974 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17975 i = 0;
17976 x = x_before;
17977 while (i < nglyphs)
17978 {
17979 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17980
17981 if (!it->truncate_lines_p
17982 && x + glyph->pixel_width > max_x)
17983 {
17984 /* End of continued line or max_x reached. */
17985 if (CHAR_GLYPH_PADDING_P (*glyph))
17986 {
17987 /* A wide character is unbreakable. */
17988 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17989 it->current_x = x_before;
17990 }
17991 else
17992 {
17993 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17994 it->current_x = x;
17995 }
17996 break;
17997 }
17998 else if (x + glyph->pixel_width > it->first_visible_x)
17999 {
18000 /* Glyph is at least partially visible. */
18001 ++it->hpos;
18002 if (x < it->first_visible_x)
18003 it->glyph_row->x = x - it->first_visible_x;
18004 }
18005 else
18006 {
18007 /* Glyph is off the left margin of the display area.
18008 Should not happen. */
18009 abort ();
18010 }
18011
18012 row->ascent = max (row->ascent, it->max_ascent);
18013 row->height = max (row->height, it->max_ascent + it->max_descent);
18014 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18015 row->phys_height = max (row->phys_height,
18016 it->max_phys_ascent + it->max_phys_descent);
18017 row->extra_line_spacing = max (row->extra_line_spacing,
18018 it->max_extra_line_spacing);
18019 x += glyph->pixel_width;
18020 ++i;
18021 }
18022
18023 /* Stop if max_x reached. */
18024 if (i < nglyphs)
18025 break;
18026
18027 /* Stop at line ends. */
18028 if (ITERATOR_AT_END_OF_LINE_P (it))
18029 {
18030 it->continuation_lines_width = 0;
18031 break;
18032 }
18033
18034 set_iterator_to_next (it, 1);
18035
18036 /* Stop if truncating at the right edge. */
18037 if (it->truncate_lines_p
18038 && it->current_x >= it->last_visible_x)
18039 {
18040 /* Add truncation mark, but don't do it if the line is
18041 truncated at a padding space. */
18042 if (IT_CHARPOS (*it) < it->string_nchars)
18043 {
18044 if (!FRAME_WINDOW_P (it->f))
18045 {
18046 int i, n;
18047
18048 if (it->current_x > it->last_visible_x)
18049 {
18050 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18051 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18052 break;
18053 for (n = row->used[TEXT_AREA]; i < n; ++i)
18054 {
18055 row->used[TEXT_AREA] = i;
18056 produce_special_glyphs (it, IT_TRUNCATION);
18057 }
18058 }
18059 produce_special_glyphs (it, IT_TRUNCATION);
18060 }
18061 it->glyph_row->truncated_on_right_p = 1;
18062 }
18063 break;
18064 }
18065 }
18066
18067 /* Maybe insert a truncation at the left. */
18068 if (it->first_visible_x
18069 && IT_CHARPOS (*it) > 0)
18070 {
18071 if (!FRAME_WINDOW_P (it->f))
18072 insert_left_trunc_glyphs (it);
18073 it->glyph_row->truncated_on_left_p = 1;
18074 }
18075
18076 it->face_id = saved_face_id;
18077
18078 /* Value is number of columns displayed. */
18079 return it->hpos - hpos_at_start;
18080 }
18081
18082
18083 \f
18084 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18085 appears as an element of LIST or as the car of an element of LIST.
18086 If PROPVAL is a list, compare each element against LIST in that
18087 way, and return 1/2 if any element of PROPVAL is found in LIST.
18088 Otherwise return 0. This function cannot quit.
18089 The return value is 2 if the text is invisible but with an ellipsis
18090 and 1 if it's invisible and without an ellipsis. */
18091
18092 int
18093 invisible_p (propval, list)
18094 register Lisp_Object propval;
18095 Lisp_Object list;
18096 {
18097 register Lisp_Object tail, proptail;
18098
18099 for (tail = list; CONSP (tail); tail = XCDR (tail))
18100 {
18101 register Lisp_Object tem;
18102 tem = XCAR (tail);
18103 if (EQ (propval, tem))
18104 return 1;
18105 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18106 return NILP (XCDR (tem)) ? 1 : 2;
18107 }
18108
18109 if (CONSP (propval))
18110 {
18111 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18112 {
18113 Lisp_Object propelt;
18114 propelt = XCAR (proptail);
18115 for (tail = list; CONSP (tail); tail = XCDR (tail))
18116 {
18117 register Lisp_Object tem;
18118 tem = XCAR (tail);
18119 if (EQ (propelt, tem))
18120 return 1;
18121 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18122 return NILP (XCDR (tem)) ? 1 : 2;
18123 }
18124 }
18125 }
18126
18127 return 0;
18128 }
18129
18130 /* Calculate a width or height in pixels from a specification using
18131 the following elements:
18132
18133 SPEC ::=
18134 NUM - a (fractional) multiple of the default font width/height
18135 (NUM) - specifies exactly NUM pixels
18136 UNIT - a fixed number of pixels, see below.
18137 ELEMENT - size of a display element in pixels, see below.
18138 (NUM . SPEC) - equals NUM * SPEC
18139 (+ SPEC SPEC ...) - add pixel values
18140 (- SPEC SPEC ...) - subtract pixel values
18141 (- SPEC) - negate pixel value
18142
18143 NUM ::=
18144 INT or FLOAT - a number constant
18145 SYMBOL - use symbol's (buffer local) variable binding.
18146
18147 UNIT ::=
18148 in - pixels per inch *)
18149 mm - pixels per 1/1000 meter *)
18150 cm - pixels per 1/100 meter *)
18151 width - width of current font in pixels.
18152 height - height of current font in pixels.
18153
18154 *) using the ratio(s) defined in display-pixels-per-inch.
18155
18156 ELEMENT ::=
18157
18158 left-fringe - left fringe width in pixels
18159 right-fringe - right fringe width in pixels
18160
18161 left-margin - left margin width in pixels
18162 right-margin - right margin width in pixels
18163
18164 scroll-bar - scroll-bar area width in pixels
18165
18166 Examples:
18167
18168 Pixels corresponding to 5 inches:
18169 (5 . in)
18170
18171 Total width of non-text areas on left side of window (if scroll-bar is on left):
18172 '(space :width (+ left-fringe left-margin scroll-bar))
18173
18174 Align to first text column (in header line):
18175 '(space :align-to 0)
18176
18177 Align to middle of text area minus half the width of variable `my-image'
18178 containing a loaded image:
18179 '(space :align-to (0.5 . (- text my-image)))
18180
18181 Width of left margin minus width of 1 character in the default font:
18182 '(space :width (- left-margin 1))
18183
18184 Width of left margin minus width of 2 characters in the current font:
18185 '(space :width (- left-margin (2 . width)))
18186
18187 Center 1 character over left-margin (in header line):
18188 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18189
18190 Different ways to express width of left fringe plus left margin minus one pixel:
18191 '(space :width (- (+ left-fringe left-margin) (1)))
18192 '(space :width (+ left-fringe left-margin (- (1))))
18193 '(space :width (+ left-fringe left-margin (-1)))
18194
18195 */
18196
18197 #define NUMVAL(X) \
18198 ((INTEGERP (X) || FLOATP (X)) \
18199 ? XFLOATINT (X) \
18200 : - 1)
18201
18202 int
18203 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18204 double *res;
18205 struct it *it;
18206 Lisp_Object prop;
18207 void *font;
18208 int width_p, *align_to;
18209 {
18210 double pixels;
18211
18212 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18213 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18214
18215 if (NILP (prop))
18216 return OK_PIXELS (0);
18217
18218 if (SYMBOLP (prop))
18219 {
18220 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18221 {
18222 char *unit = SDATA (SYMBOL_NAME (prop));
18223
18224 if (unit[0] == 'i' && unit[1] == 'n')
18225 pixels = 1.0;
18226 else if (unit[0] == 'm' && unit[1] == 'm')
18227 pixels = 25.4;
18228 else if (unit[0] == 'c' && unit[1] == 'm')
18229 pixels = 2.54;
18230 else
18231 pixels = 0;
18232 if (pixels > 0)
18233 {
18234 double ppi;
18235 #ifdef HAVE_WINDOW_SYSTEM
18236 if (FRAME_WINDOW_P (it->f)
18237 && (ppi = (width_p
18238 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18239 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18240 ppi > 0))
18241 return OK_PIXELS (ppi / pixels);
18242 #endif
18243
18244 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18245 || (CONSP (Vdisplay_pixels_per_inch)
18246 && (ppi = (width_p
18247 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18248 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18249 ppi > 0)))
18250 return OK_PIXELS (ppi / pixels);
18251
18252 return 0;
18253 }
18254 }
18255
18256 #ifdef HAVE_WINDOW_SYSTEM
18257 if (EQ (prop, Qheight))
18258 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18259 if (EQ (prop, Qwidth))
18260 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18261 #else
18262 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18263 return OK_PIXELS (1);
18264 #endif
18265
18266 if (EQ (prop, Qtext))
18267 return OK_PIXELS (width_p
18268 ? window_box_width (it->w, TEXT_AREA)
18269 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18270
18271 if (align_to && *align_to < 0)
18272 {
18273 *res = 0;
18274 if (EQ (prop, Qleft))
18275 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18276 if (EQ (prop, Qright))
18277 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18278 if (EQ (prop, Qcenter))
18279 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18280 + window_box_width (it->w, TEXT_AREA) / 2);
18281 if (EQ (prop, Qleft_fringe))
18282 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18283 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18284 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18285 if (EQ (prop, Qright_fringe))
18286 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18287 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18288 : window_box_right_offset (it->w, TEXT_AREA));
18289 if (EQ (prop, Qleft_margin))
18290 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18291 if (EQ (prop, Qright_margin))
18292 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18293 if (EQ (prop, Qscroll_bar))
18294 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18295 ? 0
18296 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18297 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18298 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18299 : 0)));
18300 }
18301 else
18302 {
18303 if (EQ (prop, Qleft_fringe))
18304 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18305 if (EQ (prop, Qright_fringe))
18306 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18307 if (EQ (prop, Qleft_margin))
18308 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18309 if (EQ (prop, Qright_margin))
18310 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18311 if (EQ (prop, Qscroll_bar))
18312 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18313 }
18314
18315 prop = Fbuffer_local_value (prop, it->w->buffer);
18316 }
18317
18318 if (INTEGERP (prop) || FLOATP (prop))
18319 {
18320 int base_unit = (width_p
18321 ? FRAME_COLUMN_WIDTH (it->f)
18322 : FRAME_LINE_HEIGHT (it->f));
18323 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18324 }
18325
18326 if (CONSP (prop))
18327 {
18328 Lisp_Object car = XCAR (prop);
18329 Lisp_Object cdr = XCDR (prop);
18330
18331 if (SYMBOLP (car))
18332 {
18333 #ifdef HAVE_WINDOW_SYSTEM
18334 if (valid_image_p (prop))
18335 {
18336 int id = lookup_image (it->f, prop);
18337 struct image *img = IMAGE_FROM_ID (it->f, id);
18338
18339 return OK_PIXELS (width_p ? img->width : img->height);
18340 }
18341 #endif
18342 if (EQ (car, Qplus) || EQ (car, Qminus))
18343 {
18344 int first = 1;
18345 double px;
18346
18347 pixels = 0;
18348 while (CONSP (cdr))
18349 {
18350 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18351 font, width_p, align_to))
18352 return 0;
18353 if (first)
18354 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18355 else
18356 pixels += px;
18357 cdr = XCDR (cdr);
18358 }
18359 if (EQ (car, Qminus))
18360 pixels = -pixels;
18361 return OK_PIXELS (pixels);
18362 }
18363
18364 car = Fbuffer_local_value (car, it->w->buffer);
18365 }
18366
18367 if (INTEGERP (car) || FLOATP (car))
18368 {
18369 double fact;
18370 pixels = XFLOATINT (car);
18371 if (NILP (cdr))
18372 return OK_PIXELS (pixels);
18373 if (calc_pixel_width_or_height (&fact, it, cdr,
18374 font, width_p, align_to))
18375 return OK_PIXELS (pixels * fact);
18376 return 0;
18377 }
18378
18379 return 0;
18380 }
18381
18382 return 0;
18383 }
18384
18385 \f
18386 /***********************************************************************
18387 Glyph Display
18388 ***********************************************************************/
18389
18390 #ifdef HAVE_WINDOW_SYSTEM
18391
18392 #if GLYPH_DEBUG
18393
18394 void
18395 dump_glyph_string (s)
18396 struct glyph_string *s;
18397 {
18398 fprintf (stderr, "glyph string\n");
18399 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18400 s->x, s->y, s->width, s->height);
18401 fprintf (stderr, " ybase = %d\n", s->ybase);
18402 fprintf (stderr, " hl = %d\n", s->hl);
18403 fprintf (stderr, " left overhang = %d, right = %d\n",
18404 s->left_overhang, s->right_overhang);
18405 fprintf (stderr, " nchars = %d\n", s->nchars);
18406 fprintf (stderr, " extends to end of line = %d\n",
18407 s->extends_to_end_of_line_p);
18408 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18409 fprintf (stderr, " bg width = %d\n", s->background_width);
18410 }
18411
18412 #endif /* GLYPH_DEBUG */
18413
18414 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18415 of XChar2b structures for S; it can't be allocated in
18416 init_glyph_string because it must be allocated via `alloca'. W
18417 is the window on which S is drawn. ROW and AREA are the glyph row
18418 and area within the row from which S is constructed. START is the
18419 index of the first glyph structure covered by S. HL is a
18420 face-override for drawing S. */
18421
18422 #ifdef HAVE_NTGUI
18423 #define OPTIONAL_HDC(hdc) hdc,
18424 #define DECLARE_HDC(hdc) HDC hdc;
18425 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18426 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18427 #endif
18428
18429 #ifndef OPTIONAL_HDC
18430 #define OPTIONAL_HDC(hdc)
18431 #define DECLARE_HDC(hdc)
18432 #define ALLOCATE_HDC(hdc, f)
18433 #define RELEASE_HDC(hdc, f)
18434 #endif
18435
18436 static void
18437 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18438 struct glyph_string *s;
18439 DECLARE_HDC (hdc)
18440 XChar2b *char2b;
18441 struct window *w;
18442 struct glyph_row *row;
18443 enum glyph_row_area area;
18444 int start;
18445 enum draw_glyphs_face hl;
18446 {
18447 bzero (s, sizeof *s);
18448 s->w = w;
18449 s->f = XFRAME (w->frame);
18450 #ifdef HAVE_NTGUI
18451 s->hdc = hdc;
18452 #endif
18453 s->display = FRAME_X_DISPLAY (s->f);
18454 s->window = FRAME_X_WINDOW (s->f);
18455 s->char2b = char2b;
18456 s->hl = hl;
18457 s->row = row;
18458 s->area = area;
18459 s->first_glyph = row->glyphs[area] + start;
18460 s->height = row->height;
18461 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18462
18463 /* Display the internal border below the tool-bar window. */
18464 if (WINDOWP (s->f->tool_bar_window)
18465 && s->w == XWINDOW (s->f->tool_bar_window))
18466 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18467
18468 s->ybase = s->y + row->ascent;
18469 }
18470
18471
18472 /* Append the list of glyph strings with head H and tail T to the list
18473 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18474
18475 static INLINE void
18476 append_glyph_string_lists (head, tail, h, t)
18477 struct glyph_string **head, **tail;
18478 struct glyph_string *h, *t;
18479 {
18480 if (h)
18481 {
18482 if (*head)
18483 (*tail)->next = h;
18484 else
18485 *head = h;
18486 h->prev = *tail;
18487 *tail = t;
18488 }
18489 }
18490
18491
18492 /* Prepend the list of glyph strings with head H and tail T to the
18493 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18494 result. */
18495
18496 static INLINE void
18497 prepend_glyph_string_lists (head, tail, h, t)
18498 struct glyph_string **head, **tail;
18499 struct glyph_string *h, *t;
18500 {
18501 if (h)
18502 {
18503 if (*head)
18504 (*head)->prev = t;
18505 else
18506 *tail = t;
18507 t->next = *head;
18508 *head = h;
18509 }
18510 }
18511
18512
18513 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18514 Set *HEAD and *TAIL to the resulting list. */
18515
18516 static INLINE void
18517 append_glyph_string (head, tail, s)
18518 struct glyph_string **head, **tail;
18519 struct glyph_string *s;
18520 {
18521 s->next = s->prev = NULL;
18522 append_glyph_string_lists (head, tail, s, s);
18523 }
18524
18525
18526 /* Get face and two-byte form of character glyph GLYPH on frame F.
18527 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18528 a pointer to a realized face that is ready for display. */
18529
18530 static INLINE struct face *
18531 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18532 struct frame *f;
18533 struct glyph *glyph;
18534 XChar2b *char2b;
18535 int *two_byte_p;
18536 {
18537 struct face *face;
18538
18539 xassert (glyph->type == CHAR_GLYPH);
18540 face = FACE_FROM_ID (f, glyph->face_id);
18541
18542 if (two_byte_p)
18543 *two_byte_p = 0;
18544
18545 if (!glyph->multibyte_p)
18546 {
18547 /* Unibyte case. We don't have to encode, but we have to make
18548 sure to use a face suitable for unibyte. */
18549 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18550 }
18551 else if (glyph->u.ch < 128)
18552 {
18553 /* Case of ASCII in a face known to fit ASCII. */
18554 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18555 }
18556 else
18557 {
18558 int c1, c2, charset;
18559
18560 /* Split characters into bytes. If c2 is -1 afterwards, C is
18561 really a one-byte character so that byte1 is zero. */
18562 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18563 if (c2 > 0)
18564 STORE_XCHAR2B (char2b, c1, c2);
18565 else
18566 STORE_XCHAR2B (char2b, 0, c1);
18567
18568 /* Maybe encode the character in *CHAR2B. */
18569 if (charset != CHARSET_ASCII)
18570 {
18571 struct font_info *font_info
18572 = FONT_INFO_FROM_ID (f, face->font_info_id);
18573 if (font_info)
18574 glyph->font_type
18575 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18576 }
18577 }
18578
18579 /* Make sure X resources of the face are allocated. */
18580 xassert (face != NULL);
18581 PREPARE_FACE_FOR_DISPLAY (f, face);
18582 return face;
18583 }
18584
18585
18586 /* Fill glyph string S with composition components specified by S->cmp.
18587
18588 FACES is an array of faces for all components of this composition.
18589 S->gidx is the index of the first component for S.
18590
18591 OVERLAPS non-zero means S should draw the foreground only, and use
18592 its physical height for clipping. See also draw_glyphs.
18593
18594 Value is the index of a component not in S. */
18595
18596 static int
18597 fill_composite_glyph_string (s, faces, overlaps)
18598 struct glyph_string *s;
18599 struct face **faces;
18600 int overlaps;
18601 {
18602 int i;
18603
18604 xassert (s);
18605
18606 s->for_overlaps = overlaps;
18607
18608 s->face = faces[s->gidx];
18609 s->font = s->face->font;
18610 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18611
18612 /* For all glyphs of this composition, starting at the offset
18613 S->gidx, until we reach the end of the definition or encounter a
18614 glyph that requires the different face, add it to S. */
18615 ++s->nchars;
18616 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18617 ++s->nchars;
18618
18619 /* All glyph strings for the same composition has the same width,
18620 i.e. the width set for the first component of the composition. */
18621
18622 s->width = s->first_glyph->pixel_width;
18623
18624 /* If the specified font could not be loaded, use the frame's
18625 default font, but record the fact that we couldn't load it in
18626 the glyph string so that we can draw rectangles for the
18627 characters of the glyph string. */
18628 if (s->font == NULL)
18629 {
18630 s->font_not_found_p = 1;
18631 s->font = FRAME_FONT (s->f);
18632 }
18633
18634 /* Adjust base line for subscript/superscript text. */
18635 s->ybase += s->first_glyph->voffset;
18636
18637 xassert (s->face && s->face->gc);
18638
18639 /* This glyph string must always be drawn with 16-bit functions. */
18640 s->two_byte_p = 1;
18641
18642 return s->gidx + s->nchars;
18643 }
18644
18645
18646 /* Fill glyph string S from a sequence of character glyphs.
18647
18648 FACE_ID is the face id of the string. START is the index of the
18649 first glyph to consider, END is the index of the last + 1.
18650 OVERLAPS non-zero means S should draw the foreground only, and use
18651 its physical height for clipping. See also draw_glyphs.
18652
18653 Value is the index of the first glyph not in S. */
18654
18655 static int
18656 fill_glyph_string (s, face_id, start, end, overlaps)
18657 struct glyph_string *s;
18658 int face_id;
18659 int start, end, overlaps;
18660 {
18661 struct glyph *glyph, *last;
18662 int voffset;
18663 int glyph_not_available_p;
18664
18665 xassert (s->f == XFRAME (s->w->frame));
18666 xassert (s->nchars == 0);
18667 xassert (start >= 0 && end > start);
18668
18669 s->for_overlaps = overlaps,
18670 glyph = s->row->glyphs[s->area] + start;
18671 last = s->row->glyphs[s->area] + end;
18672 voffset = glyph->voffset;
18673
18674 glyph_not_available_p = glyph->glyph_not_available_p;
18675
18676 while (glyph < last
18677 && glyph->type == CHAR_GLYPH
18678 && glyph->voffset == voffset
18679 /* Same face id implies same font, nowadays. */
18680 && glyph->face_id == face_id
18681 && glyph->glyph_not_available_p == glyph_not_available_p)
18682 {
18683 int two_byte_p;
18684
18685 s->face = get_glyph_face_and_encoding (s->f, glyph,
18686 s->char2b + s->nchars,
18687 &two_byte_p);
18688 s->two_byte_p = two_byte_p;
18689 ++s->nchars;
18690 xassert (s->nchars <= end - start);
18691 s->width += glyph->pixel_width;
18692 ++glyph;
18693 }
18694
18695 s->font = s->face->font;
18696 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18697
18698 /* If the specified font could not be loaded, use the frame's font,
18699 but record the fact that we couldn't load it in
18700 S->font_not_found_p so that we can draw rectangles for the
18701 characters of the glyph string. */
18702 if (s->font == NULL || glyph_not_available_p)
18703 {
18704 s->font_not_found_p = 1;
18705 s->font = FRAME_FONT (s->f);
18706 }
18707
18708 /* Adjust base line for subscript/superscript text. */
18709 s->ybase += voffset;
18710
18711 xassert (s->face && s->face->gc);
18712 return glyph - s->row->glyphs[s->area];
18713 }
18714
18715
18716 /* Fill glyph string S from image glyph S->first_glyph. */
18717
18718 static void
18719 fill_image_glyph_string (s)
18720 struct glyph_string *s;
18721 {
18722 xassert (s->first_glyph->type == IMAGE_GLYPH);
18723 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18724 xassert (s->img);
18725 s->slice = s->first_glyph->slice;
18726 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18727 s->font = s->face->font;
18728 s->width = s->first_glyph->pixel_width;
18729
18730 /* Adjust base line for subscript/superscript text. */
18731 s->ybase += s->first_glyph->voffset;
18732 }
18733
18734
18735 /* Fill glyph string S from a sequence of stretch glyphs.
18736
18737 ROW is the glyph row in which the glyphs are found, AREA is the
18738 area within the row. START is the index of the first glyph to
18739 consider, END is the index of the last + 1.
18740
18741 Value is the index of the first glyph not in S. */
18742
18743 static int
18744 fill_stretch_glyph_string (s, row, area, start, end)
18745 struct glyph_string *s;
18746 struct glyph_row *row;
18747 enum glyph_row_area area;
18748 int start, end;
18749 {
18750 struct glyph *glyph, *last;
18751 int voffset, face_id;
18752
18753 xassert (s->first_glyph->type == STRETCH_GLYPH);
18754
18755 glyph = s->row->glyphs[s->area] + start;
18756 last = s->row->glyphs[s->area] + end;
18757 face_id = glyph->face_id;
18758 s->face = FACE_FROM_ID (s->f, face_id);
18759 s->font = s->face->font;
18760 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18761 s->width = glyph->pixel_width;
18762 s->nchars = 1;
18763 voffset = glyph->voffset;
18764
18765 for (++glyph;
18766 (glyph < last
18767 && glyph->type == STRETCH_GLYPH
18768 && glyph->voffset == voffset
18769 && glyph->face_id == face_id);
18770 ++glyph)
18771 s->width += glyph->pixel_width;
18772
18773 /* Adjust base line for subscript/superscript text. */
18774 s->ybase += voffset;
18775
18776 /* The case that face->gc == 0 is handled when drawing the glyph
18777 string by calling PREPARE_FACE_FOR_DISPLAY. */
18778 xassert (s->face);
18779 return glyph - s->row->glyphs[s->area];
18780 }
18781
18782
18783 /* EXPORT for RIF:
18784 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18785 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18786 assumed to be zero. */
18787
18788 void
18789 x_get_glyph_overhangs (glyph, f, left, right)
18790 struct glyph *glyph;
18791 struct frame *f;
18792 int *left, *right;
18793 {
18794 *left = *right = 0;
18795
18796 if (glyph->type == CHAR_GLYPH)
18797 {
18798 XFontStruct *font;
18799 struct face *face;
18800 struct font_info *font_info;
18801 XChar2b char2b;
18802 XCharStruct *pcm;
18803
18804 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18805 font = face->font;
18806 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18807 if (font /* ++KFS: Should this be font_info ? */
18808 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18809 {
18810 if (pcm->rbearing > pcm->width)
18811 *right = pcm->rbearing - pcm->width;
18812 if (pcm->lbearing < 0)
18813 *left = -pcm->lbearing;
18814 }
18815 }
18816 }
18817
18818
18819 /* Return the index of the first glyph preceding glyph string S that
18820 is overwritten by S because of S's left overhang. Value is -1
18821 if no glyphs are overwritten. */
18822
18823 static int
18824 left_overwritten (s)
18825 struct glyph_string *s;
18826 {
18827 int k;
18828
18829 if (s->left_overhang)
18830 {
18831 int x = 0, i;
18832 struct glyph *glyphs = s->row->glyphs[s->area];
18833 int first = s->first_glyph - glyphs;
18834
18835 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18836 x -= glyphs[i].pixel_width;
18837
18838 k = i + 1;
18839 }
18840 else
18841 k = -1;
18842
18843 return k;
18844 }
18845
18846
18847 /* Return the index of the first glyph preceding glyph string S that
18848 is overwriting S because of its right overhang. Value is -1 if no
18849 glyph in front of S overwrites S. */
18850
18851 static int
18852 left_overwriting (s)
18853 struct glyph_string *s;
18854 {
18855 int i, k, x;
18856 struct glyph *glyphs = s->row->glyphs[s->area];
18857 int first = s->first_glyph - glyphs;
18858
18859 k = -1;
18860 x = 0;
18861 for (i = first - 1; i >= 0; --i)
18862 {
18863 int left, right;
18864 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18865 if (x + right > 0)
18866 k = i;
18867 x -= glyphs[i].pixel_width;
18868 }
18869
18870 return k;
18871 }
18872
18873
18874 /* Return the index of the last glyph following glyph string S that is
18875 not overwritten by S because of S's right overhang. Value is -1 if
18876 no such glyph is found. */
18877
18878 static int
18879 right_overwritten (s)
18880 struct glyph_string *s;
18881 {
18882 int k = -1;
18883
18884 if (s->right_overhang)
18885 {
18886 int x = 0, i;
18887 struct glyph *glyphs = s->row->glyphs[s->area];
18888 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18889 int end = s->row->used[s->area];
18890
18891 for (i = first; i < end && s->right_overhang > x; ++i)
18892 x += glyphs[i].pixel_width;
18893
18894 k = i;
18895 }
18896
18897 return k;
18898 }
18899
18900
18901 /* Return the index of the last glyph following glyph string S that
18902 overwrites S because of its left overhang. Value is negative
18903 if no such glyph is found. */
18904
18905 static int
18906 right_overwriting (s)
18907 struct glyph_string *s;
18908 {
18909 int i, k, x;
18910 int end = s->row->used[s->area];
18911 struct glyph *glyphs = s->row->glyphs[s->area];
18912 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18913
18914 k = -1;
18915 x = 0;
18916 for (i = first; i < end; ++i)
18917 {
18918 int left, right;
18919 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18920 if (x - left < 0)
18921 k = i;
18922 x += glyphs[i].pixel_width;
18923 }
18924
18925 return k;
18926 }
18927
18928
18929 /* Get face and two-byte form of character C in face FACE_ID on frame
18930 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18931 means we want to display multibyte text. DISPLAY_P non-zero means
18932 make sure that X resources for the face returned are allocated.
18933 Value is a pointer to a realized face that is ready for display if
18934 DISPLAY_P is non-zero. */
18935
18936 static INLINE struct face *
18937 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18938 struct frame *f;
18939 int c, face_id;
18940 XChar2b *char2b;
18941 int multibyte_p, display_p;
18942 {
18943 struct face *face = FACE_FROM_ID (f, face_id);
18944
18945 if (!multibyte_p)
18946 {
18947 /* Unibyte case. We don't have to encode, but we have to make
18948 sure to use a face suitable for unibyte. */
18949 STORE_XCHAR2B (char2b, 0, c);
18950 face_id = FACE_FOR_CHAR (f, face, c);
18951 face = FACE_FROM_ID (f, face_id);
18952 }
18953 else if (c < 128)
18954 {
18955 /* Case of ASCII in a face known to fit ASCII. */
18956 STORE_XCHAR2B (char2b, 0, c);
18957 }
18958 else
18959 {
18960 int c1, c2, charset;
18961
18962 /* Split characters into bytes. If c2 is -1 afterwards, C is
18963 really a one-byte character so that byte1 is zero. */
18964 SPLIT_CHAR (c, charset, c1, c2);
18965 if (c2 > 0)
18966 STORE_XCHAR2B (char2b, c1, c2);
18967 else
18968 STORE_XCHAR2B (char2b, 0, c1);
18969
18970 /* Maybe encode the character in *CHAR2B. */
18971 if (face->font != NULL)
18972 {
18973 struct font_info *font_info
18974 = FONT_INFO_FROM_ID (f, face->font_info_id);
18975 if (font_info)
18976 rif->encode_char (c, char2b, font_info, 0);
18977 }
18978 }
18979
18980 /* Make sure X resources of the face are allocated. */
18981 #ifdef HAVE_X_WINDOWS
18982 if (display_p)
18983 #endif
18984 {
18985 xassert (face != NULL);
18986 PREPARE_FACE_FOR_DISPLAY (f, face);
18987 }
18988
18989 return face;
18990 }
18991
18992
18993 /* Set background width of glyph string S. START is the index of the
18994 first glyph following S. LAST_X is the right-most x-position + 1
18995 in the drawing area. */
18996
18997 static INLINE void
18998 set_glyph_string_background_width (s, start, last_x)
18999 struct glyph_string *s;
19000 int start;
19001 int last_x;
19002 {
19003 /* If the face of this glyph string has to be drawn to the end of
19004 the drawing area, set S->extends_to_end_of_line_p. */
19005
19006 if (start == s->row->used[s->area]
19007 && s->area == TEXT_AREA
19008 && ((s->row->fill_line_p
19009 && (s->hl == DRAW_NORMAL_TEXT
19010 || s->hl == DRAW_IMAGE_RAISED
19011 || s->hl == DRAW_IMAGE_SUNKEN))
19012 || s->hl == DRAW_MOUSE_FACE))
19013 s->extends_to_end_of_line_p = 1;
19014
19015 /* If S extends its face to the end of the line, set its
19016 background_width to the distance to the right edge of the drawing
19017 area. */
19018 if (s->extends_to_end_of_line_p)
19019 s->background_width = last_x - s->x + 1;
19020 else
19021 s->background_width = s->width;
19022 }
19023
19024
19025 /* Compute overhangs and x-positions for glyph string S and its
19026 predecessors, or successors. X is the starting x-position for S.
19027 BACKWARD_P non-zero means process predecessors. */
19028
19029 static void
19030 compute_overhangs_and_x (s, x, backward_p)
19031 struct glyph_string *s;
19032 int x;
19033 int backward_p;
19034 {
19035 if (backward_p)
19036 {
19037 while (s)
19038 {
19039 if (rif->compute_glyph_string_overhangs)
19040 rif->compute_glyph_string_overhangs (s);
19041 x -= s->width;
19042 s->x = x;
19043 s = s->prev;
19044 }
19045 }
19046 else
19047 {
19048 while (s)
19049 {
19050 if (rif->compute_glyph_string_overhangs)
19051 rif->compute_glyph_string_overhangs (s);
19052 s->x = x;
19053 x += s->width;
19054 s = s->next;
19055 }
19056 }
19057 }
19058
19059
19060
19061 /* The following macros are only called from draw_glyphs below.
19062 They reference the following parameters of that function directly:
19063 `w', `row', `area', and `overlap_p'
19064 as well as the following local variables:
19065 `s', `f', and `hdc' (in W32) */
19066
19067 #ifdef HAVE_NTGUI
19068 /* On W32, silently add local `hdc' variable to argument list of
19069 init_glyph_string. */
19070 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19071 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19072 #else
19073 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19074 init_glyph_string (s, char2b, w, row, area, start, hl)
19075 #endif
19076
19077 /* Add a glyph string for a stretch glyph to the list of strings
19078 between HEAD and TAIL. START is the index of the stretch glyph in
19079 row area AREA of glyph row ROW. END is the index of the last glyph
19080 in that glyph row area. X is the current output position assigned
19081 to the new glyph string constructed. HL overrides that face of the
19082 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19083 is the right-most x-position of the drawing area. */
19084
19085 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19086 and below -- keep them on one line. */
19087 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19088 do \
19089 { \
19090 s = (struct glyph_string *) alloca (sizeof *s); \
19091 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19092 START = fill_stretch_glyph_string (s, row, area, START, END); \
19093 append_glyph_string (&HEAD, &TAIL, s); \
19094 s->x = (X); \
19095 } \
19096 while (0)
19097
19098
19099 /* Add a glyph string for an image glyph to the list of strings
19100 between HEAD and TAIL. START is the index of the image glyph in
19101 row area AREA of glyph row ROW. END is the index of the last glyph
19102 in that glyph row area. X is the current output position assigned
19103 to the new glyph string constructed. HL overrides that face of the
19104 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19105 is the right-most x-position of the drawing area. */
19106
19107 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19108 do \
19109 { \
19110 s = (struct glyph_string *) alloca (sizeof *s); \
19111 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19112 fill_image_glyph_string (s); \
19113 append_glyph_string (&HEAD, &TAIL, s); \
19114 ++START; \
19115 s->x = (X); \
19116 } \
19117 while (0)
19118
19119
19120 /* Add a glyph string for a sequence of character glyphs to the list
19121 of strings between HEAD and TAIL. START is the index of the first
19122 glyph in row area AREA of glyph row ROW that is part of the new
19123 glyph string. END is the index of the last glyph in that glyph row
19124 area. X is the current output position assigned to the new glyph
19125 string constructed. HL overrides that face of the glyph; e.g. it
19126 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19127 right-most x-position of the drawing area. */
19128
19129 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19130 do \
19131 { \
19132 int c, face_id; \
19133 XChar2b *char2b; \
19134 \
19135 c = (row)->glyphs[area][START].u.ch; \
19136 face_id = (row)->glyphs[area][START].face_id; \
19137 \
19138 s = (struct glyph_string *) alloca (sizeof *s); \
19139 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19140 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19141 append_glyph_string (&HEAD, &TAIL, s); \
19142 s->x = (X); \
19143 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19144 } \
19145 while (0)
19146
19147
19148 /* Add a glyph string for a composite sequence to the list of strings
19149 between HEAD and TAIL. START is the index of the first glyph in
19150 row area AREA of glyph row ROW that is part of the new glyph
19151 string. END is the index of the last glyph in that glyph row area.
19152 X is the current output position assigned to the new glyph string
19153 constructed. HL overrides that face of the glyph; e.g. it is
19154 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19155 x-position of the drawing area. */
19156
19157 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19158 do { \
19159 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19160 int face_id = (row)->glyphs[area][START].face_id; \
19161 struct face *base_face = FACE_FROM_ID (f, face_id); \
19162 struct composition *cmp = composition_table[cmp_id]; \
19163 int glyph_len = cmp->glyph_len; \
19164 XChar2b *char2b; \
19165 struct face **faces; \
19166 struct glyph_string *first_s = NULL; \
19167 int n; \
19168 \
19169 base_face = base_face->ascii_face; \
19170 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19171 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19172 /* At first, fill in `char2b' and `faces'. */ \
19173 for (n = 0; n < glyph_len; n++) \
19174 { \
19175 int c = COMPOSITION_GLYPH (cmp, n); \
19176 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19177 faces[n] = FACE_FROM_ID (f, this_face_id); \
19178 get_char_face_and_encoding (f, c, this_face_id, \
19179 char2b + n, 1, 1); \
19180 } \
19181 \
19182 /* Make glyph_strings for each glyph sequence that is drawable by \
19183 the same face, and append them to HEAD/TAIL. */ \
19184 for (n = 0; n < cmp->glyph_len;) \
19185 { \
19186 s = (struct glyph_string *) alloca (sizeof *s); \
19187 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19188 append_glyph_string (&(HEAD), &(TAIL), s); \
19189 s->cmp = cmp; \
19190 s->gidx = n; \
19191 s->x = (X); \
19192 \
19193 if (n == 0) \
19194 first_s = s; \
19195 \
19196 n = fill_composite_glyph_string (s, faces, overlaps); \
19197 } \
19198 \
19199 ++START; \
19200 s = first_s; \
19201 } while (0)
19202
19203
19204 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19205 of AREA of glyph row ROW on window W between indices START and END.
19206 HL overrides the face for drawing glyph strings, e.g. it is
19207 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19208 x-positions of the drawing area.
19209
19210 This is an ugly monster macro construct because we must use alloca
19211 to allocate glyph strings (because draw_glyphs can be called
19212 asynchronously). */
19213
19214 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19215 do \
19216 { \
19217 HEAD = TAIL = NULL; \
19218 while (START < END) \
19219 { \
19220 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19221 switch (first_glyph->type) \
19222 { \
19223 case CHAR_GLYPH: \
19224 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19225 HL, X, LAST_X); \
19226 break; \
19227 \
19228 case COMPOSITE_GLYPH: \
19229 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19230 HL, X, LAST_X); \
19231 break; \
19232 \
19233 case STRETCH_GLYPH: \
19234 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19235 HL, X, LAST_X); \
19236 break; \
19237 \
19238 case IMAGE_GLYPH: \
19239 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19240 HL, X, LAST_X); \
19241 break; \
19242 \
19243 default: \
19244 abort (); \
19245 } \
19246 \
19247 set_glyph_string_background_width (s, START, LAST_X); \
19248 (X) += s->width; \
19249 } \
19250 } \
19251 while (0)
19252
19253
19254 /* Draw glyphs between START and END in AREA of ROW on window W,
19255 starting at x-position X. X is relative to AREA in W. HL is a
19256 face-override with the following meaning:
19257
19258 DRAW_NORMAL_TEXT draw normally
19259 DRAW_CURSOR draw in cursor face
19260 DRAW_MOUSE_FACE draw in mouse face.
19261 DRAW_INVERSE_VIDEO draw in mode line face
19262 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19263 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19264
19265 If OVERLAPS is non-zero, draw only the foreground of characters and
19266 clip to the physical height of ROW. Non-zero value also defines
19267 the overlapping part to be drawn:
19268
19269 OVERLAPS_PRED overlap with preceding rows
19270 OVERLAPS_SUCC overlap with succeeding rows
19271 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19272 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19273
19274 Value is the x-position reached, relative to AREA of W. */
19275
19276 static int
19277 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19278 struct window *w;
19279 int x;
19280 struct glyph_row *row;
19281 enum glyph_row_area area;
19282 int start, end;
19283 enum draw_glyphs_face hl;
19284 int overlaps;
19285 {
19286 struct glyph_string *head, *tail;
19287 struct glyph_string *s;
19288 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19289 int last_x, area_width;
19290 int x_reached;
19291 int i, j;
19292 struct frame *f = XFRAME (WINDOW_FRAME (w));
19293 DECLARE_HDC (hdc);
19294
19295 ALLOCATE_HDC (hdc, f);
19296
19297 /* Let's rather be paranoid than getting a SEGV. */
19298 end = min (end, row->used[area]);
19299 start = max (0, start);
19300 start = min (end, start);
19301
19302 /* Translate X to frame coordinates. Set last_x to the right
19303 end of the drawing area. */
19304 if (row->full_width_p)
19305 {
19306 /* X is relative to the left edge of W, without scroll bars
19307 or fringes. */
19308 x += WINDOW_LEFT_EDGE_X (w);
19309 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19310 }
19311 else
19312 {
19313 int area_left = window_box_left (w, area);
19314 x += area_left;
19315 area_width = window_box_width (w, area);
19316 last_x = area_left + area_width;
19317 }
19318
19319 /* Build a doubly-linked list of glyph_string structures between
19320 head and tail from what we have to draw. Note that the macro
19321 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19322 the reason we use a separate variable `i'. */
19323 i = start;
19324 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19325 if (tail)
19326 x_reached = tail->x + tail->background_width;
19327 else
19328 x_reached = x;
19329
19330 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19331 the row, redraw some glyphs in front or following the glyph
19332 strings built above. */
19333 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19334 {
19335 int dummy_x = 0;
19336 struct glyph_string *h, *t;
19337
19338 /* Compute overhangs for all glyph strings. */
19339 if (rif->compute_glyph_string_overhangs)
19340 for (s = head; s; s = s->next)
19341 rif->compute_glyph_string_overhangs (s);
19342
19343 /* Prepend glyph strings for glyphs in front of the first glyph
19344 string that are overwritten because of the first glyph
19345 string's left overhang. The background of all strings
19346 prepended must be drawn because the first glyph string
19347 draws over it. */
19348 i = left_overwritten (head);
19349 if (i >= 0)
19350 {
19351 j = i;
19352 BUILD_GLYPH_STRINGS (j, start, h, t,
19353 DRAW_NORMAL_TEXT, dummy_x, last_x);
19354 start = i;
19355 compute_overhangs_and_x (t, head->x, 1);
19356 prepend_glyph_string_lists (&head, &tail, h, t);
19357 clip_head = head;
19358 }
19359
19360 /* Prepend glyph strings for glyphs in front of the first glyph
19361 string that overwrite that glyph string because of their
19362 right overhang. For these strings, only the foreground must
19363 be drawn, because it draws over the glyph string at `head'.
19364 The background must not be drawn because this would overwrite
19365 right overhangs of preceding glyphs for which no glyph
19366 strings exist. */
19367 i = left_overwriting (head);
19368 if (i >= 0)
19369 {
19370 clip_head = head;
19371 BUILD_GLYPH_STRINGS (i, start, h, t,
19372 DRAW_NORMAL_TEXT, dummy_x, last_x);
19373 for (s = h; s; s = s->next)
19374 s->background_filled_p = 1;
19375 compute_overhangs_and_x (t, head->x, 1);
19376 prepend_glyph_string_lists (&head, &tail, h, t);
19377 }
19378
19379 /* Append glyphs strings for glyphs following the last glyph
19380 string tail that are overwritten by tail. The background of
19381 these strings has to be drawn because tail's foreground draws
19382 over it. */
19383 i = right_overwritten (tail);
19384 if (i >= 0)
19385 {
19386 BUILD_GLYPH_STRINGS (end, i, h, t,
19387 DRAW_NORMAL_TEXT, x, last_x);
19388 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19389 append_glyph_string_lists (&head, &tail, h, t);
19390 clip_tail = tail;
19391 }
19392
19393 /* Append glyph strings for glyphs following the last glyph
19394 string tail that overwrite tail. The foreground of such
19395 glyphs has to be drawn because it writes into the background
19396 of tail. The background must not be drawn because it could
19397 paint over the foreground of following glyphs. */
19398 i = right_overwriting (tail);
19399 if (i >= 0)
19400 {
19401 clip_tail = tail;
19402 BUILD_GLYPH_STRINGS (end, i, h, t,
19403 DRAW_NORMAL_TEXT, x, last_x);
19404 for (s = h; s; s = s->next)
19405 s->background_filled_p = 1;
19406 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19407 append_glyph_string_lists (&head, &tail, h, t);
19408 }
19409 if (clip_head || clip_tail)
19410 for (s = head; s; s = s->next)
19411 {
19412 s->clip_head = clip_head;
19413 s->clip_tail = clip_tail;
19414 }
19415 }
19416
19417 /* Draw all strings. */
19418 for (s = head; s; s = s->next)
19419 rif->draw_glyph_string (s);
19420
19421 if (area == TEXT_AREA
19422 && !row->full_width_p
19423 /* When drawing overlapping rows, only the glyph strings'
19424 foreground is drawn, which doesn't erase a cursor
19425 completely. */
19426 && !overlaps)
19427 {
19428 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19429 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19430 : (tail ? tail->x + tail->background_width : x));
19431
19432 int text_left = window_box_left (w, TEXT_AREA);
19433 x0 -= text_left;
19434 x1 -= text_left;
19435
19436 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19437 row->y, MATRIX_ROW_BOTTOM_Y (row));
19438 }
19439
19440 /* Value is the x-position up to which drawn, relative to AREA of W.
19441 This doesn't include parts drawn because of overhangs. */
19442 if (row->full_width_p)
19443 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19444 else
19445 x_reached -= window_box_left (w, area);
19446
19447 RELEASE_HDC (hdc, f);
19448
19449 return x_reached;
19450 }
19451
19452 /* Expand row matrix if too narrow. Don't expand if area
19453 is not present. */
19454
19455 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19456 { \
19457 if (!fonts_changed_p \
19458 && (it->glyph_row->glyphs[area] \
19459 < it->glyph_row->glyphs[area + 1])) \
19460 { \
19461 it->w->ncols_scale_factor++; \
19462 fonts_changed_p = 1; \
19463 } \
19464 }
19465
19466 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19467 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19468
19469 static INLINE void
19470 append_glyph (it)
19471 struct it *it;
19472 {
19473 struct glyph *glyph;
19474 enum glyph_row_area area = it->area;
19475
19476 xassert (it->glyph_row);
19477 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19478
19479 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19480 if (glyph < it->glyph_row->glyphs[area + 1])
19481 {
19482 glyph->charpos = CHARPOS (it->position);
19483 glyph->object = it->object;
19484 glyph->pixel_width = it->pixel_width;
19485 glyph->ascent = it->ascent;
19486 glyph->descent = it->descent;
19487 glyph->voffset = it->voffset;
19488 glyph->type = CHAR_GLYPH;
19489 glyph->multibyte_p = it->multibyte_p;
19490 glyph->left_box_line_p = it->start_of_box_run_p;
19491 glyph->right_box_line_p = it->end_of_box_run_p;
19492 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19493 || it->phys_descent > it->descent);
19494 glyph->padding_p = 0;
19495 glyph->glyph_not_available_p = it->glyph_not_available_p;
19496 glyph->face_id = it->face_id;
19497 glyph->u.ch = it->char_to_display;
19498 glyph->slice = null_glyph_slice;
19499 glyph->font_type = FONT_TYPE_UNKNOWN;
19500 ++it->glyph_row->used[area];
19501 }
19502 else
19503 IT_EXPAND_MATRIX_WIDTH (it, area);
19504 }
19505
19506 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19507 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19508
19509 static INLINE void
19510 append_composite_glyph (it)
19511 struct it *it;
19512 {
19513 struct glyph *glyph;
19514 enum glyph_row_area area = it->area;
19515
19516 xassert (it->glyph_row);
19517
19518 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19519 if (glyph < it->glyph_row->glyphs[area + 1])
19520 {
19521 glyph->charpos = CHARPOS (it->position);
19522 glyph->object = it->object;
19523 glyph->pixel_width = it->pixel_width;
19524 glyph->ascent = it->ascent;
19525 glyph->descent = it->descent;
19526 glyph->voffset = it->voffset;
19527 glyph->type = COMPOSITE_GLYPH;
19528 glyph->multibyte_p = it->multibyte_p;
19529 glyph->left_box_line_p = it->start_of_box_run_p;
19530 glyph->right_box_line_p = it->end_of_box_run_p;
19531 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19532 || it->phys_descent > it->descent);
19533 glyph->padding_p = 0;
19534 glyph->glyph_not_available_p = 0;
19535 glyph->face_id = it->face_id;
19536 glyph->u.cmp_id = it->cmp_id;
19537 glyph->slice = null_glyph_slice;
19538 glyph->font_type = FONT_TYPE_UNKNOWN;
19539 ++it->glyph_row->used[area];
19540 }
19541 else
19542 IT_EXPAND_MATRIX_WIDTH (it, area);
19543 }
19544
19545
19546 /* Change IT->ascent and IT->height according to the setting of
19547 IT->voffset. */
19548
19549 static INLINE void
19550 take_vertical_position_into_account (it)
19551 struct it *it;
19552 {
19553 if (it->voffset)
19554 {
19555 if (it->voffset < 0)
19556 /* Increase the ascent so that we can display the text higher
19557 in the line. */
19558 it->ascent -= it->voffset;
19559 else
19560 /* Increase the descent so that we can display the text lower
19561 in the line. */
19562 it->descent += it->voffset;
19563 }
19564 }
19565
19566
19567 /* Produce glyphs/get display metrics for the image IT is loaded with.
19568 See the description of struct display_iterator in dispextern.h for
19569 an overview of struct display_iterator. */
19570
19571 static void
19572 produce_image_glyph (it)
19573 struct it *it;
19574 {
19575 struct image *img;
19576 struct face *face;
19577 int glyph_ascent;
19578 struct glyph_slice slice;
19579
19580 xassert (it->what == IT_IMAGE);
19581
19582 face = FACE_FROM_ID (it->f, it->face_id);
19583 xassert (face);
19584 /* Make sure X resources of the face is loaded. */
19585 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19586
19587 if (it->image_id < 0)
19588 {
19589 /* Fringe bitmap. */
19590 it->ascent = it->phys_ascent = 0;
19591 it->descent = it->phys_descent = 0;
19592 it->pixel_width = 0;
19593 it->nglyphs = 0;
19594 return;
19595 }
19596
19597 img = IMAGE_FROM_ID (it->f, it->image_id);
19598 xassert (img);
19599 /* Make sure X resources of the image is loaded. */
19600 prepare_image_for_display (it->f, img);
19601
19602 slice.x = slice.y = 0;
19603 slice.width = img->width;
19604 slice.height = img->height;
19605
19606 if (INTEGERP (it->slice.x))
19607 slice.x = XINT (it->slice.x);
19608 else if (FLOATP (it->slice.x))
19609 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19610
19611 if (INTEGERP (it->slice.y))
19612 slice.y = XINT (it->slice.y);
19613 else if (FLOATP (it->slice.y))
19614 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19615
19616 if (INTEGERP (it->slice.width))
19617 slice.width = XINT (it->slice.width);
19618 else if (FLOATP (it->slice.width))
19619 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19620
19621 if (INTEGERP (it->slice.height))
19622 slice.height = XINT (it->slice.height);
19623 else if (FLOATP (it->slice.height))
19624 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19625
19626 if (slice.x >= img->width)
19627 slice.x = img->width;
19628 if (slice.y >= img->height)
19629 slice.y = img->height;
19630 if (slice.x + slice.width >= img->width)
19631 slice.width = img->width - slice.x;
19632 if (slice.y + slice.height > img->height)
19633 slice.height = img->height - slice.y;
19634
19635 if (slice.width == 0 || slice.height == 0)
19636 return;
19637
19638 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19639
19640 it->descent = slice.height - glyph_ascent;
19641 if (slice.y == 0)
19642 it->descent += img->vmargin;
19643 if (slice.y + slice.height == img->height)
19644 it->descent += img->vmargin;
19645 it->phys_descent = it->descent;
19646
19647 it->pixel_width = slice.width;
19648 if (slice.x == 0)
19649 it->pixel_width += img->hmargin;
19650 if (slice.x + slice.width == img->width)
19651 it->pixel_width += img->hmargin;
19652
19653 /* It's quite possible for images to have an ascent greater than
19654 their height, so don't get confused in that case. */
19655 if (it->descent < 0)
19656 it->descent = 0;
19657
19658 #if 0 /* this breaks image tiling */
19659 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19660 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19661 if (face_ascent > it->ascent)
19662 it->ascent = it->phys_ascent = face_ascent;
19663 #endif
19664
19665 it->nglyphs = 1;
19666
19667 if (face->box != FACE_NO_BOX)
19668 {
19669 if (face->box_line_width > 0)
19670 {
19671 if (slice.y == 0)
19672 it->ascent += face->box_line_width;
19673 if (slice.y + slice.height == img->height)
19674 it->descent += face->box_line_width;
19675 }
19676
19677 if (it->start_of_box_run_p && slice.x == 0)
19678 it->pixel_width += abs (face->box_line_width);
19679 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19680 it->pixel_width += abs (face->box_line_width);
19681 }
19682
19683 take_vertical_position_into_account (it);
19684
19685 if (it->glyph_row)
19686 {
19687 struct glyph *glyph;
19688 enum glyph_row_area area = it->area;
19689
19690 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19691 if (glyph < it->glyph_row->glyphs[area + 1])
19692 {
19693 glyph->charpos = CHARPOS (it->position);
19694 glyph->object = it->object;
19695 glyph->pixel_width = it->pixel_width;
19696 glyph->ascent = glyph_ascent;
19697 glyph->descent = it->descent;
19698 glyph->voffset = it->voffset;
19699 glyph->type = IMAGE_GLYPH;
19700 glyph->multibyte_p = it->multibyte_p;
19701 glyph->left_box_line_p = it->start_of_box_run_p;
19702 glyph->right_box_line_p = it->end_of_box_run_p;
19703 glyph->overlaps_vertically_p = 0;
19704 glyph->padding_p = 0;
19705 glyph->glyph_not_available_p = 0;
19706 glyph->face_id = it->face_id;
19707 glyph->u.img_id = img->id;
19708 glyph->slice = slice;
19709 glyph->font_type = FONT_TYPE_UNKNOWN;
19710 ++it->glyph_row->used[area];
19711 }
19712 else
19713 IT_EXPAND_MATRIX_WIDTH (it, area);
19714 }
19715 }
19716
19717
19718 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19719 of the glyph, WIDTH and HEIGHT are the width and height of the
19720 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19721
19722 static void
19723 append_stretch_glyph (it, object, width, height, ascent)
19724 struct it *it;
19725 Lisp_Object object;
19726 int width, height;
19727 int ascent;
19728 {
19729 struct glyph *glyph;
19730 enum glyph_row_area area = it->area;
19731
19732 xassert (ascent >= 0 && ascent <= height);
19733
19734 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19735 if (glyph < it->glyph_row->glyphs[area + 1])
19736 {
19737 glyph->charpos = CHARPOS (it->position);
19738 glyph->object = object;
19739 glyph->pixel_width = width;
19740 glyph->ascent = ascent;
19741 glyph->descent = height - ascent;
19742 glyph->voffset = it->voffset;
19743 glyph->type = STRETCH_GLYPH;
19744 glyph->multibyte_p = it->multibyte_p;
19745 glyph->left_box_line_p = it->start_of_box_run_p;
19746 glyph->right_box_line_p = it->end_of_box_run_p;
19747 glyph->overlaps_vertically_p = 0;
19748 glyph->padding_p = 0;
19749 glyph->glyph_not_available_p = 0;
19750 glyph->face_id = it->face_id;
19751 glyph->u.stretch.ascent = ascent;
19752 glyph->u.stretch.height = height;
19753 glyph->slice = null_glyph_slice;
19754 glyph->font_type = FONT_TYPE_UNKNOWN;
19755 ++it->glyph_row->used[area];
19756 }
19757 else
19758 IT_EXPAND_MATRIX_WIDTH (it, area);
19759 }
19760
19761
19762 /* Produce a stretch glyph for iterator IT. IT->object is the value
19763 of the glyph property displayed. The value must be a list
19764 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19765 being recognized:
19766
19767 1. `:width WIDTH' specifies that the space should be WIDTH *
19768 canonical char width wide. WIDTH may be an integer or floating
19769 point number.
19770
19771 2. `:relative-width FACTOR' specifies that the width of the stretch
19772 should be computed from the width of the first character having the
19773 `glyph' property, and should be FACTOR times that width.
19774
19775 3. `:align-to HPOS' specifies that the space should be wide enough
19776 to reach HPOS, a value in canonical character units.
19777
19778 Exactly one of the above pairs must be present.
19779
19780 4. `:height HEIGHT' specifies that the height of the stretch produced
19781 should be HEIGHT, measured in canonical character units.
19782
19783 5. `:relative-height FACTOR' specifies that the height of the
19784 stretch should be FACTOR times the height of the characters having
19785 the glyph property.
19786
19787 Either none or exactly one of 4 or 5 must be present.
19788
19789 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19790 of the stretch should be used for the ascent of the stretch.
19791 ASCENT must be in the range 0 <= ASCENT <= 100. */
19792
19793 static void
19794 produce_stretch_glyph (it)
19795 struct it *it;
19796 {
19797 /* (space :width WIDTH :height HEIGHT ...) */
19798 Lisp_Object prop, plist;
19799 int width = 0, height = 0, align_to = -1;
19800 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19801 int ascent = 0;
19802 double tem;
19803 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19804 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19805
19806 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19807
19808 /* List should start with `space'. */
19809 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19810 plist = XCDR (it->object);
19811
19812 /* Compute the width of the stretch. */
19813 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19814 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19815 {
19816 /* Absolute width `:width WIDTH' specified and valid. */
19817 zero_width_ok_p = 1;
19818 width = (int)tem;
19819 }
19820 else if (prop = Fplist_get (plist, QCrelative_width),
19821 NUMVAL (prop) > 0)
19822 {
19823 /* Relative width `:relative-width FACTOR' specified and valid.
19824 Compute the width of the characters having the `glyph'
19825 property. */
19826 struct it it2;
19827 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19828
19829 it2 = *it;
19830 if (it->multibyte_p)
19831 {
19832 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19833 - IT_BYTEPOS (*it));
19834 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19835 }
19836 else
19837 it2.c = *p, it2.len = 1;
19838
19839 it2.glyph_row = NULL;
19840 it2.what = IT_CHARACTER;
19841 x_produce_glyphs (&it2);
19842 width = NUMVAL (prop) * it2.pixel_width;
19843 }
19844 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19845 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19846 {
19847 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19848 align_to = (align_to < 0
19849 ? 0
19850 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19851 else if (align_to < 0)
19852 align_to = window_box_left_offset (it->w, TEXT_AREA);
19853 width = max (0, (int)tem + align_to - it->current_x);
19854 zero_width_ok_p = 1;
19855 }
19856 else
19857 /* Nothing specified -> width defaults to canonical char width. */
19858 width = FRAME_COLUMN_WIDTH (it->f);
19859
19860 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19861 width = 1;
19862
19863 /* Compute height. */
19864 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19865 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19866 {
19867 height = (int)tem;
19868 zero_height_ok_p = 1;
19869 }
19870 else if (prop = Fplist_get (plist, QCrelative_height),
19871 NUMVAL (prop) > 0)
19872 height = FONT_HEIGHT (font) * NUMVAL (prop);
19873 else
19874 height = FONT_HEIGHT (font);
19875
19876 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19877 height = 1;
19878
19879 /* Compute percentage of height used for ascent. If
19880 `:ascent ASCENT' is present and valid, use that. Otherwise,
19881 derive the ascent from the font in use. */
19882 if (prop = Fplist_get (plist, QCascent),
19883 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19884 ascent = height * NUMVAL (prop) / 100.0;
19885 else if (!NILP (prop)
19886 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19887 ascent = min (max (0, (int)tem), height);
19888 else
19889 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19890
19891 if (width > 0 && !it->truncate_lines_p
19892 && it->current_x + width > it->last_visible_x)
19893 width = it->last_visible_x - it->current_x - 1;
19894
19895 if (width > 0 && height > 0 && it->glyph_row)
19896 {
19897 Lisp_Object object = it->stack[it->sp - 1].string;
19898 if (!STRINGP (object))
19899 object = it->w->buffer;
19900 append_stretch_glyph (it, object, width, height, ascent);
19901 }
19902
19903 it->pixel_width = width;
19904 it->ascent = it->phys_ascent = ascent;
19905 it->descent = it->phys_descent = height - it->ascent;
19906 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19907
19908 take_vertical_position_into_account (it);
19909 }
19910
19911 /* Get line-height and line-spacing property at point.
19912 If line-height has format (HEIGHT TOTAL), return TOTAL
19913 in TOTAL_HEIGHT. */
19914
19915 static Lisp_Object
19916 get_line_height_property (it, prop)
19917 struct it *it;
19918 Lisp_Object prop;
19919 {
19920 Lisp_Object position;
19921
19922 if (STRINGP (it->object))
19923 position = make_number (IT_STRING_CHARPOS (*it));
19924 else if (BUFFERP (it->object))
19925 position = make_number (IT_CHARPOS (*it));
19926 else
19927 return Qnil;
19928
19929 return Fget_char_property (position, prop, it->object);
19930 }
19931
19932 /* Calculate line-height and line-spacing properties.
19933 An integer value specifies explicit pixel value.
19934 A float value specifies relative value to current face height.
19935 A cons (float . face-name) specifies relative value to
19936 height of specified face font.
19937
19938 Returns height in pixels, or nil. */
19939
19940
19941 static Lisp_Object
19942 calc_line_height_property (it, val, font, boff, override)
19943 struct it *it;
19944 Lisp_Object val;
19945 XFontStruct *font;
19946 int boff, override;
19947 {
19948 Lisp_Object face_name = Qnil;
19949 int ascent, descent, height;
19950
19951 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19952 return val;
19953
19954 if (CONSP (val))
19955 {
19956 face_name = XCAR (val);
19957 val = XCDR (val);
19958 if (!NUMBERP (val))
19959 val = make_number (1);
19960 if (NILP (face_name))
19961 {
19962 height = it->ascent + it->descent;
19963 goto scale;
19964 }
19965 }
19966
19967 if (NILP (face_name))
19968 {
19969 font = FRAME_FONT (it->f);
19970 boff = FRAME_BASELINE_OFFSET (it->f);
19971 }
19972 else if (EQ (face_name, Qt))
19973 {
19974 override = 0;
19975 }
19976 else
19977 {
19978 int face_id;
19979 struct face *face;
19980 struct font_info *font_info;
19981
19982 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19983 if (face_id < 0)
19984 return make_number (-1);
19985
19986 face = FACE_FROM_ID (it->f, face_id);
19987 font = face->font;
19988 if (font == NULL)
19989 return make_number (-1);
19990
19991 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19992 boff = font_info->baseline_offset;
19993 if (font_info->vertical_centering)
19994 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19995 }
19996
19997 ascent = FONT_BASE (font) + boff;
19998 descent = FONT_DESCENT (font) - boff;
19999
20000 if (override)
20001 {
20002 it->override_ascent = ascent;
20003 it->override_descent = descent;
20004 it->override_boff = boff;
20005 }
20006
20007 height = ascent + descent;
20008
20009 scale:
20010 if (FLOATP (val))
20011 height = (int)(XFLOAT_DATA (val) * height);
20012 else if (INTEGERP (val))
20013 height *= XINT (val);
20014
20015 return make_number (height);
20016 }
20017
20018
20019 /* RIF:
20020 Produce glyphs/get display metrics for the display element IT is
20021 loaded with. See the description of struct it in dispextern.h
20022 for an overview of struct it. */
20023
20024 void
20025 x_produce_glyphs (it)
20026 struct it *it;
20027 {
20028 int extra_line_spacing = it->extra_line_spacing;
20029
20030 it->glyph_not_available_p = 0;
20031
20032 if (it->what == IT_CHARACTER)
20033 {
20034 XChar2b char2b;
20035 XFontStruct *font;
20036 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20037 XCharStruct *pcm;
20038 int font_not_found_p;
20039 struct font_info *font_info;
20040 int boff; /* baseline offset */
20041 /* We may change it->multibyte_p upon unibyte<->multibyte
20042 conversion. So, save the current value now and restore it
20043 later.
20044
20045 Note: It seems that we don't have to record multibyte_p in
20046 struct glyph because the character code itself tells if or
20047 not the character is multibyte. Thus, in the future, we must
20048 consider eliminating the field `multibyte_p' in the struct
20049 glyph. */
20050 int saved_multibyte_p = it->multibyte_p;
20051
20052 /* Maybe translate single-byte characters to multibyte, or the
20053 other way. */
20054 it->char_to_display = it->c;
20055 if (!ASCII_BYTE_P (it->c))
20056 {
20057 if (unibyte_display_via_language_environment
20058 && SINGLE_BYTE_CHAR_P (it->c)
20059 && (it->c >= 0240
20060 || !NILP (Vnonascii_translation_table)))
20061 {
20062 it->char_to_display = unibyte_char_to_multibyte (it->c);
20063 it->multibyte_p = 1;
20064 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20065 face = FACE_FROM_ID (it->f, it->face_id);
20066 }
20067 else if (!SINGLE_BYTE_CHAR_P (it->c)
20068 && !it->multibyte_p)
20069 {
20070 it->multibyte_p = 1;
20071 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20072 face = FACE_FROM_ID (it->f, it->face_id);
20073 }
20074 }
20075
20076 /* Get font to use. Encode IT->char_to_display. */
20077 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20078 &char2b, it->multibyte_p, 0);
20079 font = face->font;
20080
20081 /* When no suitable font found, use the default font. */
20082 font_not_found_p = font == NULL;
20083 if (font_not_found_p)
20084 {
20085 font = FRAME_FONT (it->f);
20086 boff = FRAME_BASELINE_OFFSET (it->f);
20087 font_info = NULL;
20088 }
20089 else
20090 {
20091 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20092 boff = font_info->baseline_offset;
20093 if (font_info->vertical_centering)
20094 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20095 }
20096
20097 if (it->char_to_display >= ' '
20098 && (!it->multibyte_p || it->char_to_display < 128))
20099 {
20100 /* Either unibyte or ASCII. */
20101 int stretched_p;
20102
20103 it->nglyphs = 1;
20104
20105 pcm = rif->per_char_metric (font, &char2b,
20106 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20107
20108 if (it->override_ascent >= 0)
20109 {
20110 it->ascent = it->override_ascent;
20111 it->descent = it->override_descent;
20112 boff = it->override_boff;
20113 }
20114 else
20115 {
20116 it->ascent = FONT_BASE (font) + boff;
20117 it->descent = FONT_DESCENT (font) - boff;
20118 }
20119
20120 if (pcm)
20121 {
20122 it->phys_ascent = pcm->ascent + boff;
20123 it->phys_descent = pcm->descent - boff;
20124 it->pixel_width = pcm->width;
20125 }
20126 else
20127 {
20128 it->glyph_not_available_p = 1;
20129 it->phys_ascent = it->ascent;
20130 it->phys_descent = it->descent;
20131 it->pixel_width = FONT_WIDTH (font);
20132 }
20133
20134 if (it->constrain_row_ascent_descent_p)
20135 {
20136 if (it->descent > it->max_descent)
20137 {
20138 it->ascent += it->descent - it->max_descent;
20139 it->descent = it->max_descent;
20140 }
20141 if (it->ascent > it->max_ascent)
20142 {
20143 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20144 it->ascent = it->max_ascent;
20145 }
20146 it->phys_ascent = min (it->phys_ascent, it->ascent);
20147 it->phys_descent = min (it->phys_descent, it->descent);
20148 extra_line_spacing = 0;
20149 }
20150
20151 /* If this is a space inside a region of text with
20152 `space-width' property, change its width. */
20153 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20154 if (stretched_p)
20155 it->pixel_width *= XFLOATINT (it->space_width);
20156
20157 /* If face has a box, add the box thickness to the character
20158 height. If character has a box line to the left and/or
20159 right, add the box line width to the character's width. */
20160 if (face->box != FACE_NO_BOX)
20161 {
20162 int thick = face->box_line_width;
20163
20164 if (thick > 0)
20165 {
20166 it->ascent += thick;
20167 it->descent += thick;
20168 }
20169 else
20170 thick = -thick;
20171
20172 if (it->start_of_box_run_p)
20173 it->pixel_width += thick;
20174 if (it->end_of_box_run_p)
20175 it->pixel_width += thick;
20176 }
20177
20178 /* If face has an overline, add the height of the overline
20179 (1 pixel) and a 1 pixel margin to the character height. */
20180 if (face->overline_p)
20181 it->ascent += 2;
20182
20183 if (it->constrain_row_ascent_descent_p)
20184 {
20185 if (it->ascent > it->max_ascent)
20186 it->ascent = it->max_ascent;
20187 if (it->descent > it->max_descent)
20188 it->descent = it->max_descent;
20189 }
20190
20191 take_vertical_position_into_account (it);
20192
20193 /* If we have to actually produce glyphs, do it. */
20194 if (it->glyph_row)
20195 {
20196 if (stretched_p)
20197 {
20198 /* Translate a space with a `space-width' property
20199 into a stretch glyph. */
20200 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20201 / FONT_HEIGHT (font));
20202 append_stretch_glyph (it, it->object, it->pixel_width,
20203 it->ascent + it->descent, ascent);
20204 }
20205 else
20206 append_glyph (it);
20207
20208 /* If characters with lbearing or rbearing are displayed
20209 in this line, record that fact in a flag of the
20210 glyph row. This is used to optimize X output code. */
20211 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20212 it->glyph_row->contains_overlapping_glyphs_p = 1;
20213 }
20214 }
20215 else if (it->char_to_display == '\n')
20216 {
20217 /* A newline has no width but we need the height of the line.
20218 But if previous part of the line set a height, don't
20219 increase that height */
20220
20221 Lisp_Object height;
20222 Lisp_Object total_height = Qnil;
20223
20224 it->override_ascent = -1;
20225 it->pixel_width = 0;
20226 it->nglyphs = 0;
20227
20228 height = get_line_height_property(it, Qline_height);
20229 /* Split (line-height total-height) list */
20230 if (CONSP (height)
20231 && CONSP (XCDR (height))
20232 && NILP (XCDR (XCDR (height))))
20233 {
20234 total_height = XCAR (XCDR (height));
20235 height = XCAR (height);
20236 }
20237 height = calc_line_height_property(it, height, font, boff, 1);
20238
20239 if (it->override_ascent >= 0)
20240 {
20241 it->ascent = it->override_ascent;
20242 it->descent = it->override_descent;
20243 boff = it->override_boff;
20244 }
20245 else
20246 {
20247 it->ascent = FONT_BASE (font) + boff;
20248 it->descent = FONT_DESCENT (font) - boff;
20249 }
20250
20251 if (EQ (height, Qt))
20252 {
20253 if (it->descent > it->max_descent)
20254 {
20255 it->ascent += it->descent - it->max_descent;
20256 it->descent = it->max_descent;
20257 }
20258 if (it->ascent > it->max_ascent)
20259 {
20260 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20261 it->ascent = it->max_ascent;
20262 }
20263 it->phys_ascent = min (it->phys_ascent, it->ascent);
20264 it->phys_descent = min (it->phys_descent, it->descent);
20265 it->constrain_row_ascent_descent_p = 1;
20266 extra_line_spacing = 0;
20267 }
20268 else
20269 {
20270 Lisp_Object spacing;
20271
20272 it->phys_ascent = it->ascent;
20273 it->phys_descent = it->descent;
20274
20275 if ((it->max_ascent > 0 || it->max_descent > 0)
20276 && face->box != FACE_NO_BOX
20277 && face->box_line_width > 0)
20278 {
20279 it->ascent += face->box_line_width;
20280 it->descent += face->box_line_width;
20281 }
20282 if (!NILP (height)
20283 && XINT (height) > it->ascent + it->descent)
20284 it->ascent = XINT (height) - it->descent;
20285
20286 if (!NILP (total_height))
20287 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20288 else
20289 {
20290 spacing = get_line_height_property(it, Qline_spacing);
20291 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20292 }
20293 if (INTEGERP (spacing))
20294 {
20295 extra_line_spacing = XINT (spacing);
20296 if (!NILP (total_height))
20297 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20298 }
20299 }
20300 }
20301 else if (it->char_to_display == '\t')
20302 {
20303 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20304 int x = it->current_x + it->continuation_lines_width;
20305 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20306
20307 /* If the distance from the current position to the next tab
20308 stop is less than a space character width, use the
20309 tab stop after that. */
20310 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20311 next_tab_x += tab_width;
20312
20313 it->pixel_width = next_tab_x - x;
20314 it->nglyphs = 1;
20315 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20316 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20317
20318 if (it->glyph_row)
20319 {
20320 append_stretch_glyph (it, it->object, it->pixel_width,
20321 it->ascent + it->descent, it->ascent);
20322 }
20323 }
20324 else
20325 {
20326 /* A multi-byte character. Assume that the display width of the
20327 character is the width of the character multiplied by the
20328 width of the font. */
20329
20330 /* If we found a font, this font should give us the right
20331 metrics. If we didn't find a font, use the frame's
20332 default font and calculate the width of the character
20333 from the charset width; this is what old redisplay code
20334 did. */
20335
20336 pcm = rif->per_char_metric (font, &char2b,
20337 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20338
20339 if (font_not_found_p || !pcm)
20340 {
20341 int charset = CHAR_CHARSET (it->char_to_display);
20342
20343 it->glyph_not_available_p = 1;
20344 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20345 * CHARSET_WIDTH (charset));
20346 it->phys_ascent = FONT_BASE (font) + boff;
20347 it->phys_descent = FONT_DESCENT (font) - boff;
20348 }
20349 else
20350 {
20351 it->pixel_width = pcm->width;
20352 it->phys_ascent = pcm->ascent + boff;
20353 it->phys_descent = pcm->descent - boff;
20354 if (it->glyph_row
20355 && (pcm->lbearing < 0
20356 || pcm->rbearing > pcm->width))
20357 it->glyph_row->contains_overlapping_glyphs_p = 1;
20358 }
20359 it->nglyphs = 1;
20360 it->ascent = FONT_BASE (font) + boff;
20361 it->descent = FONT_DESCENT (font) - boff;
20362 if (face->box != FACE_NO_BOX)
20363 {
20364 int thick = face->box_line_width;
20365
20366 if (thick > 0)
20367 {
20368 it->ascent += thick;
20369 it->descent += thick;
20370 }
20371 else
20372 thick = - thick;
20373
20374 if (it->start_of_box_run_p)
20375 it->pixel_width += thick;
20376 if (it->end_of_box_run_p)
20377 it->pixel_width += thick;
20378 }
20379
20380 /* If face has an overline, add the height of the overline
20381 (1 pixel) and a 1 pixel margin to the character height. */
20382 if (face->overline_p)
20383 it->ascent += 2;
20384
20385 take_vertical_position_into_account (it);
20386
20387 if (it->glyph_row)
20388 append_glyph (it);
20389 }
20390 it->multibyte_p = saved_multibyte_p;
20391 }
20392 else if (it->what == IT_COMPOSITION)
20393 {
20394 /* Note: A composition is represented as one glyph in the
20395 glyph matrix. There are no padding glyphs. */
20396 XChar2b char2b;
20397 XFontStruct *font;
20398 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20399 XCharStruct *pcm;
20400 int font_not_found_p;
20401 struct font_info *font_info;
20402 int boff; /* baseline offset */
20403 struct composition *cmp = composition_table[it->cmp_id];
20404
20405 /* Maybe translate single-byte characters to multibyte. */
20406 it->char_to_display = it->c;
20407 if (unibyte_display_via_language_environment
20408 && SINGLE_BYTE_CHAR_P (it->c)
20409 && (it->c >= 0240
20410 || (it->c >= 0200
20411 && !NILP (Vnonascii_translation_table))))
20412 {
20413 it->char_to_display = unibyte_char_to_multibyte (it->c);
20414 }
20415
20416 /* Get face and font to use. Encode IT->char_to_display. */
20417 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20418 face = FACE_FROM_ID (it->f, it->face_id);
20419 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20420 &char2b, it->multibyte_p, 0);
20421 font = face->font;
20422
20423 /* When no suitable font found, use the default font. */
20424 font_not_found_p = font == NULL;
20425 if (font_not_found_p)
20426 {
20427 font = FRAME_FONT (it->f);
20428 boff = FRAME_BASELINE_OFFSET (it->f);
20429 font_info = NULL;
20430 }
20431 else
20432 {
20433 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20434 boff = font_info->baseline_offset;
20435 if (font_info->vertical_centering)
20436 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20437 }
20438
20439 /* There are no padding glyphs, so there is only one glyph to
20440 produce for the composition. Important is that pixel_width,
20441 ascent and descent are the values of what is drawn by
20442 draw_glyphs (i.e. the values of the overall glyphs composed). */
20443 it->nglyphs = 1;
20444
20445 /* If we have not yet calculated pixel size data of glyphs of
20446 the composition for the current face font, calculate them
20447 now. Theoretically, we have to check all fonts for the
20448 glyphs, but that requires much time and memory space. So,
20449 here we check only the font of the first glyph. This leads
20450 to incorrect display very rarely, and C-l (recenter) can
20451 correct the display anyway. */
20452 if (cmp->font != (void *) font)
20453 {
20454 /* Ascent and descent of the font of the first character of
20455 this composition (adjusted by baseline offset). Ascent
20456 and descent of overall glyphs should not be less than
20457 them respectively. */
20458 int font_ascent = FONT_BASE (font) + boff;
20459 int font_descent = FONT_DESCENT (font) - boff;
20460 /* Bounding box of the overall glyphs. */
20461 int leftmost, rightmost, lowest, highest;
20462 int i, width, ascent, descent;
20463
20464 cmp->font = (void *) font;
20465
20466 /* Initialize the bounding box. */
20467 if (font_info
20468 && (pcm = rif->per_char_metric (font, &char2b,
20469 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20470 {
20471 width = pcm->width;
20472 ascent = pcm->ascent;
20473 descent = pcm->descent;
20474 }
20475 else
20476 {
20477 width = FONT_WIDTH (font);
20478 ascent = FONT_BASE (font);
20479 descent = FONT_DESCENT (font);
20480 }
20481
20482 rightmost = width;
20483 lowest = - descent + boff;
20484 highest = ascent + boff;
20485 leftmost = 0;
20486
20487 if (font_info
20488 && font_info->default_ascent
20489 && CHAR_TABLE_P (Vuse_default_ascent)
20490 && !NILP (Faref (Vuse_default_ascent,
20491 make_number (it->char_to_display))))
20492 highest = font_info->default_ascent + boff;
20493
20494 /* Draw the first glyph at the normal position. It may be
20495 shifted to right later if some other glyphs are drawn at
20496 the left. */
20497 cmp->offsets[0] = 0;
20498 cmp->offsets[1] = boff;
20499
20500 /* Set cmp->offsets for the remaining glyphs. */
20501 for (i = 1; i < cmp->glyph_len; i++)
20502 {
20503 int left, right, btm, top;
20504 int ch = COMPOSITION_GLYPH (cmp, i);
20505 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20506
20507 face = FACE_FROM_ID (it->f, face_id);
20508 get_char_face_and_encoding (it->f, ch, face->id,
20509 &char2b, it->multibyte_p, 0);
20510 font = face->font;
20511 if (font == NULL)
20512 {
20513 font = FRAME_FONT (it->f);
20514 boff = FRAME_BASELINE_OFFSET (it->f);
20515 font_info = NULL;
20516 }
20517 else
20518 {
20519 font_info
20520 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20521 boff = font_info->baseline_offset;
20522 if (font_info->vertical_centering)
20523 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20524 }
20525
20526 if (font_info
20527 && (pcm = rif->per_char_metric (font, &char2b,
20528 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20529 {
20530 width = pcm->width;
20531 ascent = pcm->ascent;
20532 descent = pcm->descent;
20533 }
20534 else
20535 {
20536 width = FONT_WIDTH (font);
20537 ascent = 1;
20538 descent = 0;
20539 }
20540
20541 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20542 {
20543 /* Relative composition with or without
20544 alternate chars. */
20545 left = (leftmost + rightmost - width) / 2;
20546 btm = - descent + boff;
20547 if (font_info && font_info->relative_compose
20548 && (! CHAR_TABLE_P (Vignore_relative_composition)
20549 || NILP (Faref (Vignore_relative_composition,
20550 make_number (ch)))))
20551 {
20552
20553 if (- descent >= font_info->relative_compose)
20554 /* One extra pixel between two glyphs. */
20555 btm = highest + 1;
20556 else if (ascent <= 0)
20557 /* One extra pixel between two glyphs. */
20558 btm = lowest - 1 - ascent - descent;
20559 }
20560 }
20561 else
20562 {
20563 /* A composition rule is specified by an integer
20564 value that encodes global and new reference
20565 points (GREF and NREF). GREF and NREF are
20566 specified by numbers as below:
20567
20568 0---1---2 -- ascent
20569 | |
20570 | |
20571 | |
20572 9--10--11 -- center
20573 | |
20574 ---3---4---5--- baseline
20575 | |
20576 6---7---8 -- descent
20577 */
20578 int rule = COMPOSITION_RULE (cmp, i);
20579 int gref, nref, grefx, grefy, nrefx, nrefy;
20580
20581 COMPOSITION_DECODE_RULE (rule, gref, nref);
20582 grefx = gref % 3, nrefx = nref % 3;
20583 grefy = gref / 3, nrefy = nref / 3;
20584
20585 left = (leftmost
20586 + grefx * (rightmost - leftmost) / 2
20587 - nrefx * width / 2);
20588 btm = ((grefy == 0 ? highest
20589 : grefy == 1 ? 0
20590 : grefy == 2 ? lowest
20591 : (highest + lowest) / 2)
20592 - (nrefy == 0 ? ascent + descent
20593 : nrefy == 1 ? descent - boff
20594 : nrefy == 2 ? 0
20595 : (ascent + descent) / 2));
20596 }
20597
20598 cmp->offsets[i * 2] = left;
20599 cmp->offsets[i * 2 + 1] = btm + descent;
20600
20601 /* Update the bounding box of the overall glyphs. */
20602 right = left + width;
20603 top = btm + descent + ascent;
20604 if (left < leftmost)
20605 leftmost = left;
20606 if (right > rightmost)
20607 rightmost = right;
20608 if (top > highest)
20609 highest = top;
20610 if (btm < lowest)
20611 lowest = btm;
20612 }
20613
20614 /* If there are glyphs whose x-offsets are negative,
20615 shift all glyphs to the right and make all x-offsets
20616 non-negative. */
20617 if (leftmost < 0)
20618 {
20619 for (i = 0; i < cmp->glyph_len; i++)
20620 cmp->offsets[i * 2] -= leftmost;
20621 rightmost -= leftmost;
20622 }
20623
20624 cmp->pixel_width = rightmost;
20625 cmp->ascent = highest;
20626 cmp->descent = - lowest;
20627 if (cmp->ascent < font_ascent)
20628 cmp->ascent = font_ascent;
20629 if (cmp->descent < font_descent)
20630 cmp->descent = font_descent;
20631 }
20632
20633 it->pixel_width = cmp->pixel_width;
20634 it->ascent = it->phys_ascent = cmp->ascent;
20635 it->descent = it->phys_descent = cmp->descent;
20636
20637 if (face->box != FACE_NO_BOX)
20638 {
20639 int thick = face->box_line_width;
20640
20641 if (thick > 0)
20642 {
20643 it->ascent += thick;
20644 it->descent += thick;
20645 }
20646 else
20647 thick = - thick;
20648
20649 if (it->start_of_box_run_p)
20650 it->pixel_width += thick;
20651 if (it->end_of_box_run_p)
20652 it->pixel_width += thick;
20653 }
20654
20655 /* If face has an overline, add the height of the overline
20656 (1 pixel) and a 1 pixel margin to the character height. */
20657 if (face->overline_p)
20658 it->ascent += 2;
20659
20660 take_vertical_position_into_account (it);
20661
20662 if (it->glyph_row)
20663 append_composite_glyph (it);
20664 }
20665 else if (it->what == IT_IMAGE)
20666 produce_image_glyph (it);
20667 else if (it->what == IT_STRETCH)
20668 produce_stretch_glyph (it);
20669
20670 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20671 because this isn't true for images with `:ascent 100'. */
20672 xassert (it->ascent >= 0 && it->descent >= 0);
20673 if (it->area == TEXT_AREA)
20674 it->current_x += it->pixel_width;
20675
20676 if (extra_line_spacing > 0)
20677 {
20678 it->descent += extra_line_spacing;
20679 if (extra_line_spacing > it->max_extra_line_spacing)
20680 it->max_extra_line_spacing = extra_line_spacing;
20681 }
20682
20683 it->max_ascent = max (it->max_ascent, it->ascent);
20684 it->max_descent = max (it->max_descent, it->descent);
20685 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20686 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20687 }
20688
20689 /* EXPORT for RIF:
20690 Output LEN glyphs starting at START at the nominal cursor position.
20691 Advance the nominal cursor over the text. The global variable
20692 updated_window contains the window being updated, updated_row is
20693 the glyph row being updated, and updated_area is the area of that
20694 row being updated. */
20695
20696 void
20697 x_write_glyphs (start, len)
20698 struct glyph *start;
20699 int len;
20700 {
20701 int x, hpos;
20702
20703 xassert (updated_window && updated_row);
20704 BLOCK_INPUT;
20705
20706 /* Write glyphs. */
20707
20708 hpos = start - updated_row->glyphs[updated_area];
20709 x = draw_glyphs (updated_window, output_cursor.x,
20710 updated_row, updated_area,
20711 hpos, hpos + len,
20712 DRAW_NORMAL_TEXT, 0);
20713
20714 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20715 if (updated_area == TEXT_AREA
20716 && updated_window->phys_cursor_on_p
20717 && updated_window->phys_cursor.vpos == output_cursor.vpos
20718 && updated_window->phys_cursor.hpos >= hpos
20719 && updated_window->phys_cursor.hpos < hpos + len)
20720 updated_window->phys_cursor_on_p = 0;
20721
20722 UNBLOCK_INPUT;
20723
20724 /* Advance the output cursor. */
20725 output_cursor.hpos += len;
20726 output_cursor.x = x;
20727 }
20728
20729
20730 /* EXPORT for RIF:
20731 Insert LEN glyphs from START at the nominal cursor position. */
20732
20733 void
20734 x_insert_glyphs (start, len)
20735 struct glyph *start;
20736 int len;
20737 {
20738 struct frame *f;
20739 struct window *w;
20740 int line_height, shift_by_width, shifted_region_width;
20741 struct glyph_row *row;
20742 struct glyph *glyph;
20743 int frame_x, frame_y, hpos;
20744
20745 xassert (updated_window && updated_row);
20746 BLOCK_INPUT;
20747 w = updated_window;
20748 f = XFRAME (WINDOW_FRAME (w));
20749
20750 /* Get the height of the line we are in. */
20751 row = updated_row;
20752 line_height = row->height;
20753
20754 /* Get the width of the glyphs to insert. */
20755 shift_by_width = 0;
20756 for (glyph = start; glyph < start + len; ++glyph)
20757 shift_by_width += glyph->pixel_width;
20758
20759 /* Get the width of the region to shift right. */
20760 shifted_region_width = (window_box_width (w, updated_area)
20761 - output_cursor.x
20762 - shift_by_width);
20763
20764 /* Shift right. */
20765 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20766 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20767
20768 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20769 line_height, shift_by_width);
20770
20771 /* Write the glyphs. */
20772 hpos = start - row->glyphs[updated_area];
20773 draw_glyphs (w, output_cursor.x, row, updated_area,
20774 hpos, hpos + len,
20775 DRAW_NORMAL_TEXT, 0);
20776
20777 /* Advance the output cursor. */
20778 output_cursor.hpos += len;
20779 output_cursor.x += shift_by_width;
20780 UNBLOCK_INPUT;
20781 }
20782
20783
20784 /* EXPORT for RIF:
20785 Erase the current text line from the nominal cursor position
20786 (inclusive) to pixel column TO_X (exclusive). The idea is that
20787 everything from TO_X onward is already erased.
20788
20789 TO_X is a pixel position relative to updated_area of
20790 updated_window. TO_X == -1 means clear to the end of this area. */
20791
20792 void
20793 x_clear_end_of_line (to_x)
20794 int to_x;
20795 {
20796 struct frame *f;
20797 struct window *w = updated_window;
20798 int max_x, min_y, max_y;
20799 int from_x, from_y, to_y;
20800
20801 xassert (updated_window && updated_row);
20802 f = XFRAME (w->frame);
20803
20804 if (updated_row->full_width_p)
20805 max_x = WINDOW_TOTAL_WIDTH (w);
20806 else
20807 max_x = window_box_width (w, updated_area);
20808 max_y = window_text_bottom_y (w);
20809
20810 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20811 of window. For TO_X > 0, truncate to end of drawing area. */
20812 if (to_x == 0)
20813 return;
20814 else if (to_x < 0)
20815 to_x = max_x;
20816 else
20817 to_x = min (to_x, max_x);
20818
20819 to_y = min (max_y, output_cursor.y + updated_row->height);
20820
20821 /* Notice if the cursor will be cleared by this operation. */
20822 if (!updated_row->full_width_p)
20823 notice_overwritten_cursor (w, updated_area,
20824 output_cursor.x, -1,
20825 updated_row->y,
20826 MATRIX_ROW_BOTTOM_Y (updated_row));
20827
20828 from_x = output_cursor.x;
20829
20830 /* Translate to frame coordinates. */
20831 if (updated_row->full_width_p)
20832 {
20833 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20834 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20835 }
20836 else
20837 {
20838 int area_left = window_box_left (w, updated_area);
20839 from_x += area_left;
20840 to_x += area_left;
20841 }
20842
20843 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20844 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20845 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20846
20847 /* Prevent inadvertently clearing to end of the X window. */
20848 if (to_x > from_x && to_y > from_y)
20849 {
20850 BLOCK_INPUT;
20851 rif->clear_frame_area (f, from_x, from_y,
20852 to_x - from_x, to_y - from_y);
20853 UNBLOCK_INPUT;
20854 }
20855 }
20856
20857 #endif /* HAVE_WINDOW_SYSTEM */
20858
20859
20860 \f
20861 /***********************************************************************
20862 Cursor types
20863 ***********************************************************************/
20864
20865 /* Value is the internal representation of the specified cursor type
20866 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20867 of the bar cursor. */
20868
20869 static enum text_cursor_kinds
20870 get_specified_cursor_type (arg, width)
20871 Lisp_Object arg;
20872 int *width;
20873 {
20874 enum text_cursor_kinds type;
20875
20876 if (NILP (arg))
20877 return NO_CURSOR;
20878
20879 if (EQ (arg, Qbox))
20880 return FILLED_BOX_CURSOR;
20881
20882 if (EQ (arg, Qhollow))
20883 return HOLLOW_BOX_CURSOR;
20884
20885 if (EQ (arg, Qbar))
20886 {
20887 *width = 2;
20888 return BAR_CURSOR;
20889 }
20890
20891 if (CONSP (arg)
20892 && EQ (XCAR (arg), Qbar)
20893 && INTEGERP (XCDR (arg))
20894 && XINT (XCDR (arg)) >= 0)
20895 {
20896 *width = XINT (XCDR (arg));
20897 return BAR_CURSOR;
20898 }
20899
20900 if (EQ (arg, Qhbar))
20901 {
20902 *width = 2;
20903 return HBAR_CURSOR;
20904 }
20905
20906 if (CONSP (arg)
20907 && EQ (XCAR (arg), Qhbar)
20908 && INTEGERP (XCDR (arg))
20909 && XINT (XCDR (arg)) >= 0)
20910 {
20911 *width = XINT (XCDR (arg));
20912 return HBAR_CURSOR;
20913 }
20914
20915 /* Treat anything unknown as "hollow box cursor".
20916 It was bad to signal an error; people have trouble fixing
20917 .Xdefaults with Emacs, when it has something bad in it. */
20918 type = HOLLOW_BOX_CURSOR;
20919
20920 return type;
20921 }
20922
20923 /* Set the default cursor types for specified frame. */
20924 void
20925 set_frame_cursor_types (f, arg)
20926 struct frame *f;
20927 Lisp_Object arg;
20928 {
20929 int width;
20930 Lisp_Object tem;
20931
20932 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20933 FRAME_CURSOR_WIDTH (f) = width;
20934
20935 /* By default, set up the blink-off state depending on the on-state. */
20936
20937 tem = Fassoc (arg, Vblink_cursor_alist);
20938 if (!NILP (tem))
20939 {
20940 FRAME_BLINK_OFF_CURSOR (f)
20941 = get_specified_cursor_type (XCDR (tem), &width);
20942 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20943 }
20944 else
20945 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20946 }
20947
20948
20949 /* Return the cursor we want to be displayed in window W. Return
20950 width of bar/hbar cursor through WIDTH arg. Return with
20951 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20952 (i.e. if the `system caret' should track this cursor).
20953
20954 In a mini-buffer window, we want the cursor only to appear if we
20955 are reading input from this window. For the selected window, we
20956 want the cursor type given by the frame parameter or buffer local
20957 setting of cursor-type. If explicitly marked off, draw no cursor.
20958 In all other cases, we want a hollow box cursor. */
20959
20960 static enum text_cursor_kinds
20961 get_window_cursor_type (w, glyph, width, active_cursor)
20962 struct window *w;
20963 struct glyph *glyph;
20964 int *width;
20965 int *active_cursor;
20966 {
20967 struct frame *f = XFRAME (w->frame);
20968 struct buffer *b = XBUFFER (w->buffer);
20969 int cursor_type = DEFAULT_CURSOR;
20970 Lisp_Object alt_cursor;
20971 int non_selected = 0;
20972
20973 *active_cursor = 1;
20974
20975 /* Echo area */
20976 if (cursor_in_echo_area
20977 && FRAME_HAS_MINIBUF_P (f)
20978 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20979 {
20980 if (w == XWINDOW (echo_area_window))
20981 {
20982 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
20983 {
20984 *width = FRAME_CURSOR_WIDTH (f);
20985 return FRAME_DESIRED_CURSOR (f);
20986 }
20987 else
20988 return get_specified_cursor_type (b->cursor_type, width);
20989 }
20990
20991 *active_cursor = 0;
20992 non_selected = 1;
20993 }
20994
20995 /* Nonselected window or nonselected frame. */
20996 else if (w != XWINDOW (f->selected_window)
20997 #ifdef HAVE_WINDOW_SYSTEM
20998 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20999 #endif
21000 )
21001 {
21002 *active_cursor = 0;
21003
21004 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21005 return NO_CURSOR;
21006
21007 non_selected = 1;
21008 }
21009
21010 /* Never display a cursor in a window in which cursor-type is nil. */
21011 if (NILP (b->cursor_type))
21012 return NO_CURSOR;
21013
21014 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21015 if (non_selected)
21016 {
21017 alt_cursor = b->cursor_in_non_selected_windows;
21018 return get_specified_cursor_type (alt_cursor, width);
21019 }
21020
21021 /* Get the normal cursor type for this window. */
21022 if (EQ (b->cursor_type, Qt))
21023 {
21024 cursor_type = FRAME_DESIRED_CURSOR (f);
21025 *width = FRAME_CURSOR_WIDTH (f);
21026 }
21027 else
21028 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21029
21030 /* Use normal cursor if not blinked off. */
21031 if (!w->cursor_off_p)
21032 {
21033 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
21034 if (cursor_type == FILLED_BOX_CURSOR)
21035 cursor_type = HOLLOW_BOX_CURSOR;
21036 }
21037 return cursor_type;
21038 }
21039
21040 /* Cursor is blinked off, so determine how to "toggle" it. */
21041
21042 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21043 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21044 return get_specified_cursor_type (XCDR (alt_cursor), width);
21045
21046 /* Then see if frame has specified a specific blink off cursor type. */
21047 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21048 {
21049 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21050 return FRAME_BLINK_OFF_CURSOR (f);
21051 }
21052
21053 #if 0
21054 /* Some people liked having a permanently visible blinking cursor,
21055 while others had very strong opinions against it. So it was
21056 decided to remove it. KFS 2003-09-03 */
21057
21058 /* Finally perform built-in cursor blinking:
21059 filled box <-> hollow box
21060 wide [h]bar <-> narrow [h]bar
21061 narrow [h]bar <-> no cursor
21062 other type <-> no cursor */
21063
21064 if (cursor_type == FILLED_BOX_CURSOR)
21065 return HOLLOW_BOX_CURSOR;
21066
21067 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21068 {
21069 *width = 1;
21070 return cursor_type;
21071 }
21072 #endif
21073
21074 return NO_CURSOR;
21075 }
21076
21077
21078 #ifdef HAVE_WINDOW_SYSTEM
21079
21080 /* Notice when the text cursor of window W has been completely
21081 overwritten by a drawing operation that outputs glyphs in AREA
21082 starting at X0 and ending at X1 in the line starting at Y0 and
21083 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21084 the rest of the line after X0 has been written. Y coordinates
21085 are window-relative. */
21086
21087 static void
21088 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21089 struct window *w;
21090 enum glyph_row_area area;
21091 int x0, y0, x1, y1;
21092 {
21093 int cx0, cx1, cy0, cy1;
21094 struct glyph_row *row;
21095
21096 if (!w->phys_cursor_on_p)
21097 return;
21098 if (area != TEXT_AREA)
21099 return;
21100
21101 if (w->phys_cursor.vpos < 0
21102 || w->phys_cursor.vpos >= w->current_matrix->nrows
21103 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21104 !(row->enabled_p && row->displays_text_p)))
21105 return;
21106
21107 if (row->cursor_in_fringe_p)
21108 {
21109 row->cursor_in_fringe_p = 0;
21110 draw_fringe_bitmap (w, row, 0);
21111 w->phys_cursor_on_p = 0;
21112 return;
21113 }
21114
21115 cx0 = w->phys_cursor.x;
21116 cx1 = cx0 + w->phys_cursor_width;
21117 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21118 return;
21119
21120 /* The cursor image will be completely removed from the
21121 screen if the output area intersects the cursor area in
21122 y-direction. When we draw in [y0 y1[, and some part of
21123 the cursor is at y < y0, that part must have been drawn
21124 before. When scrolling, the cursor is erased before
21125 actually scrolling, so we don't come here. When not
21126 scrolling, the rows above the old cursor row must have
21127 changed, and in this case these rows must have written
21128 over the cursor image.
21129
21130 Likewise if part of the cursor is below y1, with the
21131 exception of the cursor being in the first blank row at
21132 the buffer and window end because update_text_area
21133 doesn't draw that row. (Except when it does, but
21134 that's handled in update_text_area.) */
21135
21136 cy0 = w->phys_cursor.y;
21137 cy1 = cy0 + w->phys_cursor_height;
21138 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21139 return;
21140
21141 w->phys_cursor_on_p = 0;
21142 }
21143
21144 #endif /* HAVE_WINDOW_SYSTEM */
21145
21146 \f
21147 /************************************************************************
21148 Mouse Face
21149 ************************************************************************/
21150
21151 #ifdef HAVE_WINDOW_SYSTEM
21152
21153 /* EXPORT for RIF:
21154 Fix the display of area AREA of overlapping row ROW in window W
21155 with respect to the overlapping part OVERLAPS. */
21156
21157 void
21158 x_fix_overlapping_area (w, row, area, overlaps)
21159 struct window *w;
21160 struct glyph_row *row;
21161 enum glyph_row_area area;
21162 int overlaps;
21163 {
21164 int i, x;
21165
21166 BLOCK_INPUT;
21167
21168 x = 0;
21169 for (i = 0; i < row->used[area];)
21170 {
21171 if (row->glyphs[area][i].overlaps_vertically_p)
21172 {
21173 int start = i, start_x = x;
21174
21175 do
21176 {
21177 x += row->glyphs[area][i].pixel_width;
21178 ++i;
21179 }
21180 while (i < row->used[area]
21181 && row->glyphs[area][i].overlaps_vertically_p);
21182
21183 draw_glyphs (w, start_x, row, area,
21184 start, i,
21185 DRAW_NORMAL_TEXT, overlaps);
21186 }
21187 else
21188 {
21189 x += row->glyphs[area][i].pixel_width;
21190 ++i;
21191 }
21192 }
21193
21194 UNBLOCK_INPUT;
21195 }
21196
21197
21198 /* EXPORT:
21199 Draw the cursor glyph of window W in glyph row ROW. See the
21200 comment of draw_glyphs for the meaning of HL. */
21201
21202 void
21203 draw_phys_cursor_glyph (w, row, hl)
21204 struct window *w;
21205 struct glyph_row *row;
21206 enum draw_glyphs_face hl;
21207 {
21208 /* If cursor hpos is out of bounds, don't draw garbage. This can
21209 happen in mini-buffer windows when switching between echo area
21210 glyphs and mini-buffer. */
21211 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21212 {
21213 int on_p = w->phys_cursor_on_p;
21214 int x1;
21215 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21216 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21217 hl, 0);
21218 w->phys_cursor_on_p = on_p;
21219
21220 if (hl == DRAW_CURSOR)
21221 w->phys_cursor_width = x1 - w->phys_cursor.x;
21222 /* When we erase the cursor, and ROW is overlapped by other
21223 rows, make sure that these overlapping parts of other rows
21224 are redrawn. */
21225 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21226 {
21227 w->phys_cursor_width = x1 - w->phys_cursor.x;
21228
21229 if (row > w->current_matrix->rows
21230 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21231 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21232 OVERLAPS_ERASED_CURSOR);
21233
21234 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21235 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21236 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21237 OVERLAPS_ERASED_CURSOR);
21238 }
21239 }
21240 }
21241
21242
21243 /* EXPORT:
21244 Erase the image of a cursor of window W from the screen. */
21245
21246 void
21247 erase_phys_cursor (w)
21248 struct window *w;
21249 {
21250 struct frame *f = XFRAME (w->frame);
21251 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21252 int hpos = w->phys_cursor.hpos;
21253 int vpos = w->phys_cursor.vpos;
21254 int mouse_face_here_p = 0;
21255 struct glyph_matrix *active_glyphs = w->current_matrix;
21256 struct glyph_row *cursor_row;
21257 struct glyph *cursor_glyph;
21258 enum draw_glyphs_face hl;
21259
21260 /* No cursor displayed or row invalidated => nothing to do on the
21261 screen. */
21262 if (w->phys_cursor_type == NO_CURSOR)
21263 goto mark_cursor_off;
21264
21265 /* VPOS >= active_glyphs->nrows means that window has been resized.
21266 Don't bother to erase the cursor. */
21267 if (vpos >= active_glyphs->nrows)
21268 goto mark_cursor_off;
21269
21270 /* If row containing cursor is marked invalid, there is nothing we
21271 can do. */
21272 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21273 if (!cursor_row->enabled_p)
21274 goto mark_cursor_off;
21275
21276 /* If line spacing is > 0, old cursor may only be partially visible in
21277 window after split-window. So adjust visible height. */
21278 cursor_row->visible_height = min (cursor_row->visible_height,
21279 window_text_bottom_y (w) - cursor_row->y);
21280
21281 /* If row is completely invisible, don't attempt to delete a cursor which
21282 isn't there. This can happen if cursor is at top of a window, and
21283 we switch to a buffer with a header line in that window. */
21284 if (cursor_row->visible_height <= 0)
21285 goto mark_cursor_off;
21286
21287 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21288 if (cursor_row->cursor_in_fringe_p)
21289 {
21290 cursor_row->cursor_in_fringe_p = 0;
21291 draw_fringe_bitmap (w, cursor_row, 0);
21292 goto mark_cursor_off;
21293 }
21294
21295 /* This can happen when the new row is shorter than the old one.
21296 In this case, either draw_glyphs or clear_end_of_line
21297 should have cleared the cursor. Note that we wouldn't be
21298 able to erase the cursor in this case because we don't have a
21299 cursor glyph at hand. */
21300 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21301 goto mark_cursor_off;
21302
21303 /* If the cursor is in the mouse face area, redisplay that when
21304 we clear the cursor. */
21305 if (! NILP (dpyinfo->mouse_face_window)
21306 && w == XWINDOW (dpyinfo->mouse_face_window)
21307 && (vpos > dpyinfo->mouse_face_beg_row
21308 || (vpos == dpyinfo->mouse_face_beg_row
21309 && hpos >= dpyinfo->mouse_face_beg_col))
21310 && (vpos < dpyinfo->mouse_face_end_row
21311 || (vpos == dpyinfo->mouse_face_end_row
21312 && hpos < dpyinfo->mouse_face_end_col))
21313 /* Don't redraw the cursor's spot in mouse face if it is at the
21314 end of a line (on a newline). The cursor appears there, but
21315 mouse highlighting does not. */
21316 && cursor_row->used[TEXT_AREA] > hpos)
21317 mouse_face_here_p = 1;
21318
21319 /* Maybe clear the display under the cursor. */
21320 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21321 {
21322 int x, y;
21323 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21324 int width;
21325
21326 cursor_glyph = get_phys_cursor_glyph (w);
21327 if (cursor_glyph == NULL)
21328 goto mark_cursor_off;
21329
21330 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21331 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21332 width = min (cursor_glyph->pixel_width,
21333 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
21334
21335 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21336 }
21337
21338 /* Erase the cursor by redrawing the character underneath it. */
21339 if (mouse_face_here_p)
21340 hl = DRAW_MOUSE_FACE;
21341 else
21342 hl = DRAW_NORMAL_TEXT;
21343 draw_phys_cursor_glyph (w, cursor_row, hl);
21344
21345 mark_cursor_off:
21346 w->phys_cursor_on_p = 0;
21347 w->phys_cursor_type = NO_CURSOR;
21348 }
21349
21350
21351 /* EXPORT:
21352 Display or clear cursor of window W. If ON is zero, clear the
21353 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21354 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21355
21356 void
21357 display_and_set_cursor (w, on, hpos, vpos, x, y)
21358 struct window *w;
21359 int on, hpos, vpos, x, y;
21360 {
21361 struct frame *f = XFRAME (w->frame);
21362 int new_cursor_type;
21363 int new_cursor_width;
21364 int active_cursor;
21365 struct glyph_row *glyph_row;
21366 struct glyph *glyph;
21367
21368 /* This is pointless on invisible frames, and dangerous on garbaged
21369 windows and frames; in the latter case, the frame or window may
21370 be in the midst of changing its size, and x and y may be off the
21371 window. */
21372 if (! FRAME_VISIBLE_P (f)
21373 || FRAME_GARBAGED_P (f)
21374 || vpos >= w->current_matrix->nrows
21375 || hpos >= w->current_matrix->matrix_w)
21376 return;
21377
21378 /* If cursor is off and we want it off, return quickly. */
21379 if (!on && !w->phys_cursor_on_p)
21380 return;
21381
21382 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21383 /* If cursor row is not enabled, we don't really know where to
21384 display the cursor. */
21385 if (!glyph_row->enabled_p)
21386 {
21387 w->phys_cursor_on_p = 0;
21388 return;
21389 }
21390
21391 glyph = NULL;
21392 if (!glyph_row->exact_window_width_line_p
21393 || hpos < glyph_row->used[TEXT_AREA])
21394 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21395
21396 xassert (interrupt_input_blocked);
21397
21398 /* Set new_cursor_type to the cursor we want to be displayed. */
21399 new_cursor_type = get_window_cursor_type (w, glyph,
21400 &new_cursor_width, &active_cursor);
21401
21402 /* If cursor is currently being shown and we don't want it to be or
21403 it is in the wrong place, or the cursor type is not what we want,
21404 erase it. */
21405 if (w->phys_cursor_on_p
21406 && (!on
21407 || w->phys_cursor.x != x
21408 || w->phys_cursor.y != y
21409 || new_cursor_type != w->phys_cursor_type
21410 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21411 && new_cursor_width != w->phys_cursor_width)))
21412 erase_phys_cursor (w);
21413
21414 /* Don't check phys_cursor_on_p here because that flag is only set
21415 to zero in some cases where we know that the cursor has been
21416 completely erased, to avoid the extra work of erasing the cursor
21417 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21418 still not be visible, or it has only been partly erased. */
21419 if (on)
21420 {
21421 w->phys_cursor_ascent = glyph_row->ascent;
21422 w->phys_cursor_height = glyph_row->height;
21423
21424 /* Set phys_cursor_.* before x_draw_.* is called because some
21425 of them may need the information. */
21426 w->phys_cursor.x = x;
21427 w->phys_cursor.y = glyph_row->y;
21428 w->phys_cursor.hpos = hpos;
21429 w->phys_cursor.vpos = vpos;
21430 }
21431
21432 rif->draw_window_cursor (w, glyph_row, x, y,
21433 new_cursor_type, new_cursor_width,
21434 on, active_cursor);
21435 }
21436
21437
21438 /* Switch the display of W's cursor on or off, according to the value
21439 of ON. */
21440
21441 static void
21442 update_window_cursor (w, on)
21443 struct window *w;
21444 int on;
21445 {
21446 /* Don't update cursor in windows whose frame is in the process
21447 of being deleted. */
21448 if (w->current_matrix)
21449 {
21450 BLOCK_INPUT;
21451 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21452 w->phys_cursor.x, w->phys_cursor.y);
21453 UNBLOCK_INPUT;
21454 }
21455 }
21456
21457
21458 /* Call update_window_cursor with parameter ON_P on all leaf windows
21459 in the window tree rooted at W. */
21460
21461 static void
21462 update_cursor_in_window_tree (w, on_p)
21463 struct window *w;
21464 int on_p;
21465 {
21466 while (w)
21467 {
21468 if (!NILP (w->hchild))
21469 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21470 else if (!NILP (w->vchild))
21471 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21472 else
21473 update_window_cursor (w, on_p);
21474
21475 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21476 }
21477 }
21478
21479
21480 /* EXPORT:
21481 Display the cursor on window W, or clear it, according to ON_P.
21482 Don't change the cursor's position. */
21483
21484 void
21485 x_update_cursor (f, on_p)
21486 struct frame *f;
21487 int on_p;
21488 {
21489 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21490 }
21491
21492
21493 /* EXPORT:
21494 Clear the cursor of window W to background color, and mark the
21495 cursor as not shown. This is used when the text where the cursor
21496 is is about to be rewritten. */
21497
21498 void
21499 x_clear_cursor (w)
21500 struct window *w;
21501 {
21502 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21503 update_window_cursor (w, 0);
21504 }
21505
21506
21507 /* EXPORT:
21508 Display the active region described by mouse_face_* according to DRAW. */
21509
21510 void
21511 show_mouse_face (dpyinfo, draw)
21512 Display_Info *dpyinfo;
21513 enum draw_glyphs_face draw;
21514 {
21515 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21516 struct frame *f = XFRAME (WINDOW_FRAME (w));
21517
21518 if (/* If window is in the process of being destroyed, don't bother
21519 to do anything. */
21520 w->current_matrix != NULL
21521 /* Don't update mouse highlight if hidden */
21522 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21523 /* Recognize when we are called to operate on rows that don't exist
21524 anymore. This can happen when a window is split. */
21525 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21526 {
21527 int phys_cursor_on_p = w->phys_cursor_on_p;
21528 struct glyph_row *row, *first, *last;
21529
21530 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21531 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21532
21533 for (row = first; row <= last && row->enabled_p; ++row)
21534 {
21535 int start_hpos, end_hpos, start_x;
21536
21537 /* For all but the first row, the highlight starts at column 0. */
21538 if (row == first)
21539 {
21540 start_hpos = dpyinfo->mouse_face_beg_col;
21541 start_x = dpyinfo->mouse_face_beg_x;
21542 }
21543 else
21544 {
21545 start_hpos = 0;
21546 start_x = 0;
21547 }
21548
21549 if (row == last)
21550 end_hpos = dpyinfo->mouse_face_end_col;
21551 else
21552 {
21553 end_hpos = row->used[TEXT_AREA];
21554 if (draw == DRAW_NORMAL_TEXT)
21555 row->fill_line_p = 1; /* Clear to end of line */
21556 }
21557
21558 if (end_hpos > start_hpos)
21559 {
21560 draw_glyphs (w, start_x, row, TEXT_AREA,
21561 start_hpos, end_hpos,
21562 draw, 0);
21563
21564 row->mouse_face_p
21565 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21566 }
21567 }
21568
21569 /* When we've written over the cursor, arrange for it to
21570 be displayed again. */
21571 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21572 {
21573 BLOCK_INPUT;
21574 display_and_set_cursor (w, 1,
21575 w->phys_cursor.hpos, w->phys_cursor.vpos,
21576 w->phys_cursor.x, w->phys_cursor.y);
21577 UNBLOCK_INPUT;
21578 }
21579 }
21580
21581 /* Change the mouse cursor. */
21582 if (draw == DRAW_NORMAL_TEXT)
21583 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21584 else if (draw == DRAW_MOUSE_FACE)
21585 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21586 else
21587 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21588 }
21589
21590 /* EXPORT:
21591 Clear out the mouse-highlighted active region.
21592 Redraw it un-highlighted first. Value is non-zero if mouse
21593 face was actually drawn unhighlighted. */
21594
21595 int
21596 clear_mouse_face (dpyinfo)
21597 Display_Info *dpyinfo;
21598 {
21599 int cleared = 0;
21600
21601 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21602 {
21603 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21604 cleared = 1;
21605 }
21606
21607 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21608 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21609 dpyinfo->mouse_face_window = Qnil;
21610 dpyinfo->mouse_face_overlay = Qnil;
21611 return cleared;
21612 }
21613
21614
21615 /* EXPORT:
21616 Non-zero if physical cursor of window W is within mouse face. */
21617
21618 int
21619 cursor_in_mouse_face_p (w)
21620 struct window *w;
21621 {
21622 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21623 int in_mouse_face = 0;
21624
21625 if (WINDOWP (dpyinfo->mouse_face_window)
21626 && XWINDOW (dpyinfo->mouse_face_window) == w)
21627 {
21628 int hpos = w->phys_cursor.hpos;
21629 int vpos = w->phys_cursor.vpos;
21630
21631 if (vpos >= dpyinfo->mouse_face_beg_row
21632 && vpos <= dpyinfo->mouse_face_end_row
21633 && (vpos > dpyinfo->mouse_face_beg_row
21634 || hpos >= dpyinfo->mouse_face_beg_col)
21635 && (vpos < dpyinfo->mouse_face_end_row
21636 || hpos < dpyinfo->mouse_face_end_col
21637 || dpyinfo->mouse_face_past_end))
21638 in_mouse_face = 1;
21639 }
21640
21641 return in_mouse_face;
21642 }
21643
21644
21645
21646 \f
21647 /* Find the glyph matrix position of buffer position CHARPOS in window
21648 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21649 current glyphs must be up to date. If CHARPOS is above window
21650 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21651 of last line in W. In the row containing CHARPOS, stop before glyphs
21652 having STOP as object. */
21653
21654 #if 1 /* This is a version of fast_find_position that's more correct
21655 in the presence of hscrolling, for example. I didn't install
21656 it right away because the problem fixed is minor, it failed
21657 in 20.x as well, and I think it's too risky to install
21658 so near the release of 21.1. 2001-09-25 gerd. */
21659
21660 static int
21661 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21662 struct window *w;
21663 int charpos;
21664 int *hpos, *vpos, *x, *y;
21665 Lisp_Object stop;
21666 {
21667 struct glyph_row *row, *first;
21668 struct glyph *glyph, *end;
21669 int past_end = 0;
21670
21671 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21672 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21673 {
21674 *x = first->x;
21675 *y = first->y;
21676 *hpos = 0;
21677 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21678 return 1;
21679 }
21680
21681 row = row_containing_pos (w, charpos, first, NULL, 0);
21682 if (row == NULL)
21683 {
21684 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21685 past_end = 1;
21686 }
21687
21688 /* If whole rows or last part of a row came from a display overlay,
21689 row_containing_pos will skip over such rows because their end pos
21690 equals the start pos of the overlay or interval.
21691
21692 Move back if we have a STOP object and previous row's
21693 end glyph came from STOP. */
21694 if (!NILP (stop))
21695 {
21696 struct glyph_row *prev;
21697 while ((prev = row - 1, prev >= first)
21698 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21699 && prev->used[TEXT_AREA] > 0)
21700 {
21701 struct glyph *beg = prev->glyphs[TEXT_AREA];
21702 glyph = beg + prev->used[TEXT_AREA];
21703 while (--glyph >= beg
21704 && INTEGERP (glyph->object));
21705 if (glyph < beg
21706 || !EQ (stop, glyph->object))
21707 break;
21708 row = prev;
21709 }
21710 }
21711
21712 *x = row->x;
21713 *y = row->y;
21714 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21715
21716 glyph = row->glyphs[TEXT_AREA];
21717 end = glyph + row->used[TEXT_AREA];
21718
21719 /* Skip over glyphs not having an object at the start of the row.
21720 These are special glyphs like truncation marks on terminal
21721 frames. */
21722 if (row->displays_text_p)
21723 while (glyph < end
21724 && INTEGERP (glyph->object)
21725 && !EQ (stop, glyph->object)
21726 && glyph->charpos < 0)
21727 {
21728 *x += glyph->pixel_width;
21729 ++glyph;
21730 }
21731
21732 while (glyph < end
21733 && !INTEGERP (glyph->object)
21734 && !EQ (stop, glyph->object)
21735 && (!BUFFERP (glyph->object)
21736 || glyph->charpos < charpos))
21737 {
21738 *x += glyph->pixel_width;
21739 ++glyph;
21740 }
21741
21742 *hpos = glyph - row->glyphs[TEXT_AREA];
21743 return !past_end;
21744 }
21745
21746 #else /* not 1 */
21747
21748 static int
21749 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21750 struct window *w;
21751 int pos;
21752 int *hpos, *vpos, *x, *y;
21753 Lisp_Object stop;
21754 {
21755 int i;
21756 int lastcol;
21757 int maybe_next_line_p = 0;
21758 int line_start_position;
21759 int yb = window_text_bottom_y (w);
21760 struct glyph_row *row, *best_row;
21761 int row_vpos, best_row_vpos;
21762 int current_x;
21763
21764 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21765 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21766
21767 while (row->y < yb)
21768 {
21769 if (row->used[TEXT_AREA])
21770 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21771 else
21772 line_start_position = 0;
21773
21774 if (line_start_position > pos)
21775 break;
21776 /* If the position sought is the end of the buffer,
21777 don't include the blank lines at the bottom of the window. */
21778 else if (line_start_position == pos
21779 && pos == BUF_ZV (XBUFFER (w->buffer)))
21780 {
21781 maybe_next_line_p = 1;
21782 break;
21783 }
21784 else if (line_start_position > 0)
21785 {
21786 best_row = row;
21787 best_row_vpos = row_vpos;
21788 }
21789
21790 if (row->y + row->height >= yb)
21791 break;
21792
21793 ++row;
21794 ++row_vpos;
21795 }
21796
21797 /* Find the right column within BEST_ROW. */
21798 lastcol = 0;
21799 current_x = best_row->x;
21800 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21801 {
21802 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21803 int charpos = glyph->charpos;
21804
21805 if (BUFFERP (glyph->object))
21806 {
21807 if (charpos == pos)
21808 {
21809 *hpos = i;
21810 *vpos = best_row_vpos;
21811 *x = current_x;
21812 *y = best_row->y;
21813 return 1;
21814 }
21815 else if (charpos > pos)
21816 break;
21817 }
21818 else if (EQ (glyph->object, stop))
21819 break;
21820
21821 if (charpos > 0)
21822 lastcol = i;
21823 current_x += glyph->pixel_width;
21824 }
21825
21826 /* If we're looking for the end of the buffer,
21827 and we didn't find it in the line we scanned,
21828 use the start of the following line. */
21829 if (maybe_next_line_p)
21830 {
21831 ++best_row;
21832 ++best_row_vpos;
21833 lastcol = 0;
21834 current_x = best_row->x;
21835 }
21836
21837 *vpos = best_row_vpos;
21838 *hpos = lastcol + 1;
21839 *x = current_x;
21840 *y = best_row->y;
21841 return 0;
21842 }
21843
21844 #endif /* not 1 */
21845
21846
21847 /* Find the position of the glyph for position POS in OBJECT in
21848 window W's current matrix, and return in *X, *Y the pixel
21849 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21850
21851 RIGHT_P non-zero means return the position of the right edge of the
21852 glyph, RIGHT_P zero means return the left edge position.
21853
21854 If no glyph for POS exists in the matrix, return the position of
21855 the glyph with the next smaller position that is in the matrix, if
21856 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21857 exists in the matrix, return the position of the glyph with the
21858 next larger position in OBJECT.
21859
21860 Value is non-zero if a glyph was found. */
21861
21862 static int
21863 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21864 struct window *w;
21865 int pos;
21866 Lisp_Object object;
21867 int *hpos, *vpos, *x, *y;
21868 int right_p;
21869 {
21870 int yb = window_text_bottom_y (w);
21871 struct glyph_row *r;
21872 struct glyph *best_glyph = NULL;
21873 struct glyph_row *best_row = NULL;
21874 int best_x = 0;
21875
21876 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21877 r->enabled_p && r->y < yb;
21878 ++r)
21879 {
21880 struct glyph *g = r->glyphs[TEXT_AREA];
21881 struct glyph *e = g + r->used[TEXT_AREA];
21882 int gx;
21883
21884 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21885 if (EQ (g->object, object))
21886 {
21887 if (g->charpos == pos)
21888 {
21889 best_glyph = g;
21890 best_x = gx;
21891 best_row = r;
21892 goto found;
21893 }
21894 else if (best_glyph == NULL
21895 || ((abs (g->charpos - pos)
21896 < abs (best_glyph->charpos - pos))
21897 && (right_p
21898 ? g->charpos < pos
21899 : g->charpos > pos)))
21900 {
21901 best_glyph = g;
21902 best_x = gx;
21903 best_row = r;
21904 }
21905 }
21906 }
21907
21908 found:
21909
21910 if (best_glyph)
21911 {
21912 *x = best_x;
21913 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21914
21915 if (right_p)
21916 {
21917 *x += best_glyph->pixel_width;
21918 ++*hpos;
21919 }
21920
21921 *y = best_row->y;
21922 *vpos = best_row - w->current_matrix->rows;
21923 }
21924
21925 return best_glyph != NULL;
21926 }
21927
21928
21929 /* See if position X, Y is within a hot-spot of an image. */
21930
21931 static int
21932 on_hot_spot_p (hot_spot, x, y)
21933 Lisp_Object hot_spot;
21934 int x, y;
21935 {
21936 if (!CONSP (hot_spot))
21937 return 0;
21938
21939 if (EQ (XCAR (hot_spot), Qrect))
21940 {
21941 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21942 Lisp_Object rect = XCDR (hot_spot);
21943 Lisp_Object tem;
21944 if (!CONSP (rect))
21945 return 0;
21946 if (!CONSP (XCAR (rect)))
21947 return 0;
21948 if (!CONSP (XCDR (rect)))
21949 return 0;
21950 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21951 return 0;
21952 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21953 return 0;
21954 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21955 return 0;
21956 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21957 return 0;
21958 return 1;
21959 }
21960 else if (EQ (XCAR (hot_spot), Qcircle))
21961 {
21962 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21963 Lisp_Object circ = XCDR (hot_spot);
21964 Lisp_Object lr, lx0, ly0;
21965 if (CONSP (circ)
21966 && CONSP (XCAR (circ))
21967 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21968 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21969 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21970 {
21971 double r = XFLOATINT (lr);
21972 double dx = XINT (lx0) - x;
21973 double dy = XINT (ly0) - y;
21974 return (dx * dx + dy * dy <= r * r);
21975 }
21976 }
21977 else if (EQ (XCAR (hot_spot), Qpoly))
21978 {
21979 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21980 if (VECTORP (XCDR (hot_spot)))
21981 {
21982 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21983 Lisp_Object *poly = v->contents;
21984 int n = v->size;
21985 int i;
21986 int inside = 0;
21987 Lisp_Object lx, ly;
21988 int x0, y0;
21989
21990 /* Need an even number of coordinates, and at least 3 edges. */
21991 if (n < 6 || n & 1)
21992 return 0;
21993
21994 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21995 If count is odd, we are inside polygon. Pixels on edges
21996 may or may not be included depending on actual geometry of the
21997 polygon. */
21998 if ((lx = poly[n-2], !INTEGERP (lx))
21999 || (ly = poly[n-1], !INTEGERP (lx)))
22000 return 0;
22001 x0 = XINT (lx), y0 = XINT (ly);
22002 for (i = 0; i < n; i += 2)
22003 {
22004 int x1 = x0, y1 = y0;
22005 if ((lx = poly[i], !INTEGERP (lx))
22006 || (ly = poly[i+1], !INTEGERP (ly)))
22007 return 0;
22008 x0 = XINT (lx), y0 = XINT (ly);
22009
22010 /* Does this segment cross the X line? */
22011 if (x0 >= x)
22012 {
22013 if (x1 >= x)
22014 continue;
22015 }
22016 else if (x1 < x)
22017 continue;
22018 if (y > y0 && y > y1)
22019 continue;
22020 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22021 inside = !inside;
22022 }
22023 return inside;
22024 }
22025 }
22026 /* If we don't understand the format, pretend we're not in the hot-spot. */
22027 return 0;
22028 }
22029
22030 Lisp_Object
22031 find_hot_spot (map, x, y)
22032 Lisp_Object map;
22033 int x, y;
22034 {
22035 while (CONSP (map))
22036 {
22037 if (CONSP (XCAR (map))
22038 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22039 return XCAR (map);
22040 map = XCDR (map);
22041 }
22042
22043 return Qnil;
22044 }
22045
22046 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22047 3, 3, 0,
22048 doc: /* Lookup in image map MAP coordinates X and Y.
22049 An image map is an alist where each element has the format (AREA ID PLIST).
22050 An AREA is specified as either a rectangle, a circle, or a polygon:
22051 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22052 pixel coordinates of the upper left and bottom right corners.
22053 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22054 and the radius of the circle; r may be a float or integer.
22055 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22056 vector describes one corner in the polygon.
22057 Returns the alist element for the first matching AREA in MAP. */)
22058 (map, x, y)
22059 Lisp_Object map;
22060 Lisp_Object x, y;
22061 {
22062 if (NILP (map))
22063 return Qnil;
22064
22065 CHECK_NUMBER (x);
22066 CHECK_NUMBER (y);
22067
22068 return find_hot_spot (map, XINT (x), XINT (y));
22069 }
22070
22071
22072 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22073 static void
22074 define_frame_cursor1 (f, cursor, pointer)
22075 struct frame *f;
22076 Cursor cursor;
22077 Lisp_Object pointer;
22078 {
22079 /* Do not change cursor shape while dragging mouse. */
22080 if (!NILP (do_mouse_tracking))
22081 return;
22082
22083 if (!NILP (pointer))
22084 {
22085 if (EQ (pointer, Qarrow))
22086 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22087 else if (EQ (pointer, Qhand))
22088 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22089 else if (EQ (pointer, Qtext))
22090 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22091 else if (EQ (pointer, intern ("hdrag")))
22092 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22093 #ifdef HAVE_X_WINDOWS
22094 else if (EQ (pointer, intern ("vdrag")))
22095 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22096 #endif
22097 else if (EQ (pointer, intern ("hourglass")))
22098 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22099 else if (EQ (pointer, Qmodeline))
22100 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22101 else
22102 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22103 }
22104
22105 if (cursor != No_Cursor)
22106 rif->define_frame_cursor (f, cursor);
22107 }
22108
22109 /* Take proper action when mouse has moved to the mode or header line
22110 or marginal area AREA of window W, x-position X and y-position Y.
22111 X is relative to the start of the text display area of W, so the
22112 width of bitmap areas and scroll bars must be subtracted to get a
22113 position relative to the start of the mode line. */
22114
22115 static void
22116 note_mode_line_or_margin_highlight (window, x, y, area)
22117 Lisp_Object window;
22118 int x, y;
22119 enum window_part area;
22120 {
22121 struct window *w = XWINDOW (window);
22122 struct frame *f = XFRAME (w->frame);
22123 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22124 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22125 Lisp_Object pointer = Qnil;
22126 int charpos, dx, dy, width, height;
22127 Lisp_Object string, object = Qnil;
22128 Lisp_Object pos, help;
22129
22130 Lisp_Object mouse_face;
22131 int original_x_pixel = x;
22132 struct glyph * glyph = NULL;
22133 struct glyph_row *row;
22134
22135 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22136 {
22137 int x0;
22138 struct glyph *end;
22139
22140 string = mode_line_string (w, area, &x, &y, &charpos,
22141 &object, &dx, &dy, &width, &height);
22142
22143 row = (area == ON_MODE_LINE
22144 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22145 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22146
22147 /* Find glyph */
22148 if (row->mode_line_p && row->enabled_p)
22149 {
22150 glyph = row->glyphs[TEXT_AREA];
22151 end = glyph + row->used[TEXT_AREA];
22152
22153 for (x0 = original_x_pixel;
22154 glyph < end && x0 >= glyph->pixel_width;
22155 ++glyph)
22156 x0 -= glyph->pixel_width;
22157
22158 if (glyph >= end)
22159 glyph = NULL;
22160 }
22161 }
22162 else
22163 {
22164 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22165 string = marginal_area_string (w, area, &x, &y, &charpos,
22166 &object, &dx, &dy, &width, &height);
22167 }
22168
22169 help = Qnil;
22170
22171 if (IMAGEP (object))
22172 {
22173 Lisp_Object image_map, hotspot;
22174 if ((image_map = Fplist_get (XCDR (object), QCmap),
22175 !NILP (image_map))
22176 && (hotspot = find_hot_spot (image_map, dx, dy),
22177 CONSP (hotspot))
22178 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22179 {
22180 Lisp_Object area_id, plist;
22181
22182 area_id = XCAR (hotspot);
22183 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22184 If so, we could look for mouse-enter, mouse-leave
22185 properties in PLIST (and do something...). */
22186 hotspot = XCDR (hotspot);
22187 if (CONSP (hotspot)
22188 && (plist = XCAR (hotspot), CONSP (plist)))
22189 {
22190 pointer = Fplist_get (plist, Qpointer);
22191 if (NILP (pointer))
22192 pointer = Qhand;
22193 help = Fplist_get (plist, Qhelp_echo);
22194 if (!NILP (help))
22195 {
22196 help_echo_string = help;
22197 /* Is this correct? ++kfs */
22198 XSETWINDOW (help_echo_window, w);
22199 help_echo_object = w->buffer;
22200 help_echo_pos = charpos;
22201 }
22202 }
22203 }
22204 if (NILP (pointer))
22205 pointer = Fplist_get (XCDR (object), QCpointer);
22206 }
22207
22208 if (STRINGP (string))
22209 {
22210 pos = make_number (charpos);
22211 /* If we're on a string with `help-echo' text property, arrange
22212 for the help to be displayed. This is done by setting the
22213 global variable help_echo_string to the help string. */
22214 if (NILP (help))
22215 {
22216 help = Fget_text_property (pos, Qhelp_echo, string);
22217 if (!NILP (help))
22218 {
22219 help_echo_string = help;
22220 XSETWINDOW (help_echo_window, w);
22221 help_echo_object = string;
22222 help_echo_pos = charpos;
22223 }
22224 }
22225
22226 if (NILP (pointer))
22227 pointer = Fget_text_property (pos, Qpointer, string);
22228
22229 /* Change the mouse pointer according to what is under X/Y. */
22230 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22231 {
22232 Lisp_Object map;
22233 map = Fget_text_property (pos, Qlocal_map, string);
22234 if (!KEYMAPP (map))
22235 map = Fget_text_property (pos, Qkeymap, string);
22236 if (!KEYMAPP (map))
22237 cursor = dpyinfo->vertical_scroll_bar_cursor;
22238 }
22239
22240 /* Change the mouse face according to what is under X/Y. */
22241 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22242 if (!NILP (mouse_face)
22243 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22244 && glyph)
22245 {
22246 Lisp_Object b, e;
22247
22248 struct glyph * tmp_glyph;
22249
22250 int gpos;
22251 int gseq_length;
22252 int total_pixel_width;
22253 int ignore;
22254
22255 int vpos, hpos;
22256
22257 b = Fprevious_single_property_change (make_number (charpos + 1),
22258 Qmouse_face, string, Qnil);
22259 if (NILP (b))
22260 b = make_number (0);
22261
22262 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22263 if (NILP (e))
22264 e = make_number (SCHARS (string));
22265
22266 /* Calculate the position(glyph position: GPOS) of GLYPH in
22267 displayed string. GPOS is different from CHARPOS.
22268
22269 CHARPOS is the position of glyph in internal string
22270 object. A mode line string format has structures which
22271 is converted to a flatten by emacs lisp interpreter.
22272 The internal string is an element of the structures.
22273 The displayed string is the flatten string. */
22274 for (tmp_glyph = glyph - 1, gpos = 0;
22275 tmp_glyph->charpos >= XINT (b);
22276 tmp_glyph--, gpos++)
22277 {
22278 if (!EQ (tmp_glyph->object, glyph->object))
22279 break;
22280 }
22281
22282 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22283 displayed string holding GLYPH.
22284
22285 GSEQ_LENGTH is different from SCHARS (STRING).
22286 SCHARS (STRING) returns the length of the internal string. */
22287 for (tmp_glyph = glyph, gseq_length = gpos;
22288 tmp_glyph->charpos < XINT (e);
22289 tmp_glyph++, gseq_length++)
22290 {
22291 if (!EQ (tmp_glyph->object, glyph->object))
22292 break;
22293 }
22294
22295 total_pixel_width = 0;
22296 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22297 total_pixel_width += tmp_glyph->pixel_width;
22298
22299 /* Pre calculation of re-rendering position */
22300 vpos = (x - gpos);
22301 hpos = (area == ON_MODE_LINE
22302 ? (w->current_matrix)->nrows - 1
22303 : 0);
22304
22305 /* If the re-rendering position is included in the last
22306 re-rendering area, we should do nothing. */
22307 if ( EQ (window, dpyinfo->mouse_face_window)
22308 && dpyinfo->mouse_face_beg_col <= vpos
22309 && vpos < dpyinfo->mouse_face_end_col
22310 && dpyinfo->mouse_face_beg_row == hpos )
22311 return;
22312
22313 if (clear_mouse_face (dpyinfo))
22314 cursor = No_Cursor;
22315
22316 dpyinfo->mouse_face_beg_col = vpos;
22317 dpyinfo->mouse_face_beg_row = hpos;
22318
22319 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22320 dpyinfo->mouse_face_beg_y = 0;
22321
22322 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22323 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22324
22325 dpyinfo->mouse_face_end_x = 0;
22326 dpyinfo->mouse_face_end_y = 0;
22327
22328 dpyinfo->mouse_face_past_end = 0;
22329 dpyinfo->mouse_face_window = window;
22330
22331 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22332 charpos,
22333 0, 0, 0, &ignore,
22334 glyph->face_id, 1);
22335 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22336
22337 if (NILP (pointer))
22338 pointer = Qhand;
22339 }
22340 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22341 clear_mouse_face (dpyinfo);
22342 }
22343 define_frame_cursor1 (f, cursor, pointer);
22344 }
22345
22346
22347 /* EXPORT:
22348 Take proper action when the mouse has moved to position X, Y on
22349 frame F as regards highlighting characters that have mouse-face
22350 properties. Also de-highlighting chars where the mouse was before.
22351 X and Y can be negative or out of range. */
22352
22353 void
22354 note_mouse_highlight (f, x, y)
22355 struct frame *f;
22356 int x, y;
22357 {
22358 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22359 enum window_part part;
22360 Lisp_Object window;
22361 struct window *w;
22362 Cursor cursor = No_Cursor;
22363 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22364 struct buffer *b;
22365
22366 /* When a menu is active, don't highlight because this looks odd. */
22367 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22368 if (popup_activated ())
22369 return;
22370 #endif
22371
22372 if (NILP (Vmouse_highlight)
22373 || !f->glyphs_initialized_p)
22374 return;
22375
22376 dpyinfo->mouse_face_mouse_x = x;
22377 dpyinfo->mouse_face_mouse_y = y;
22378 dpyinfo->mouse_face_mouse_frame = f;
22379
22380 if (dpyinfo->mouse_face_defer)
22381 return;
22382
22383 if (gc_in_progress)
22384 {
22385 dpyinfo->mouse_face_deferred_gc = 1;
22386 return;
22387 }
22388
22389 /* Which window is that in? */
22390 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22391
22392 /* If we were displaying active text in another window, clear that.
22393 Also clear if we move out of text area in same window. */
22394 if (! EQ (window, dpyinfo->mouse_face_window)
22395 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22396 && !NILP (dpyinfo->mouse_face_window)))
22397 clear_mouse_face (dpyinfo);
22398
22399 /* Not on a window -> return. */
22400 if (!WINDOWP (window))
22401 return;
22402
22403 /* Reset help_echo_string. It will get recomputed below. */
22404 help_echo_string = Qnil;
22405
22406 /* Convert to window-relative pixel coordinates. */
22407 w = XWINDOW (window);
22408 frame_to_window_pixel_xy (w, &x, &y);
22409
22410 /* Handle tool-bar window differently since it doesn't display a
22411 buffer. */
22412 if (EQ (window, f->tool_bar_window))
22413 {
22414 note_tool_bar_highlight (f, x, y);
22415 return;
22416 }
22417
22418 /* Mouse is on the mode, header line or margin? */
22419 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22420 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22421 {
22422 note_mode_line_or_margin_highlight (window, x, y, part);
22423 return;
22424 }
22425
22426 if (part == ON_VERTICAL_BORDER)
22427 {
22428 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22429 help_echo_string = build_string ("drag-mouse-1: resize");
22430 }
22431 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22432 || part == ON_SCROLL_BAR)
22433 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22434 else
22435 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22436
22437 /* Are we in a window whose display is up to date?
22438 And verify the buffer's text has not changed. */
22439 b = XBUFFER (w->buffer);
22440 if (part == ON_TEXT
22441 && EQ (w->window_end_valid, w->buffer)
22442 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22443 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22444 {
22445 int hpos, vpos, pos, i, dx, dy, area;
22446 struct glyph *glyph;
22447 Lisp_Object object;
22448 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22449 Lisp_Object *overlay_vec = NULL;
22450 int noverlays;
22451 struct buffer *obuf;
22452 int obegv, ozv, same_region;
22453
22454 /* Find the glyph under X/Y. */
22455 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22456
22457 /* Look for :pointer property on image. */
22458 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22459 {
22460 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22461 if (img != NULL && IMAGEP (img->spec))
22462 {
22463 Lisp_Object image_map, hotspot;
22464 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22465 !NILP (image_map))
22466 && (hotspot = find_hot_spot (image_map,
22467 glyph->slice.x + dx,
22468 glyph->slice.y + dy),
22469 CONSP (hotspot))
22470 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22471 {
22472 Lisp_Object area_id, plist;
22473
22474 area_id = XCAR (hotspot);
22475 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22476 If so, we could look for mouse-enter, mouse-leave
22477 properties in PLIST (and do something...). */
22478 hotspot = XCDR (hotspot);
22479 if (CONSP (hotspot)
22480 && (plist = XCAR (hotspot), CONSP (plist)))
22481 {
22482 pointer = Fplist_get (plist, Qpointer);
22483 if (NILP (pointer))
22484 pointer = Qhand;
22485 help_echo_string = Fplist_get (plist, Qhelp_echo);
22486 if (!NILP (help_echo_string))
22487 {
22488 help_echo_window = window;
22489 help_echo_object = glyph->object;
22490 help_echo_pos = glyph->charpos;
22491 }
22492 }
22493 }
22494 if (NILP (pointer))
22495 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22496 }
22497 }
22498
22499 /* Clear mouse face if X/Y not over text. */
22500 if (glyph == NULL
22501 || area != TEXT_AREA
22502 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22503 {
22504 if (clear_mouse_face (dpyinfo))
22505 cursor = No_Cursor;
22506 if (NILP (pointer))
22507 {
22508 if (area != TEXT_AREA)
22509 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22510 else
22511 pointer = Vvoid_text_area_pointer;
22512 }
22513 goto set_cursor;
22514 }
22515
22516 pos = glyph->charpos;
22517 object = glyph->object;
22518 if (!STRINGP (object) && !BUFFERP (object))
22519 goto set_cursor;
22520
22521 /* If we get an out-of-range value, return now; avoid an error. */
22522 if (BUFFERP (object) && pos > BUF_Z (b))
22523 goto set_cursor;
22524
22525 /* Make the window's buffer temporarily current for
22526 overlays_at and compute_char_face. */
22527 obuf = current_buffer;
22528 current_buffer = b;
22529 obegv = BEGV;
22530 ozv = ZV;
22531 BEGV = BEG;
22532 ZV = Z;
22533
22534 /* Is this char mouse-active or does it have help-echo? */
22535 position = make_number (pos);
22536
22537 if (BUFFERP (object))
22538 {
22539 /* Put all the overlays we want in a vector in overlay_vec. */
22540 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22541 /* Sort overlays into increasing priority order. */
22542 noverlays = sort_overlays (overlay_vec, noverlays, w);
22543 }
22544 else
22545 noverlays = 0;
22546
22547 same_region = (EQ (window, dpyinfo->mouse_face_window)
22548 && vpos >= dpyinfo->mouse_face_beg_row
22549 && vpos <= dpyinfo->mouse_face_end_row
22550 && (vpos > dpyinfo->mouse_face_beg_row
22551 || hpos >= dpyinfo->mouse_face_beg_col)
22552 && (vpos < dpyinfo->mouse_face_end_row
22553 || hpos < dpyinfo->mouse_face_end_col
22554 || dpyinfo->mouse_face_past_end));
22555
22556 if (same_region)
22557 cursor = No_Cursor;
22558
22559 /* Check mouse-face highlighting. */
22560 if (! same_region
22561 /* If there exists an overlay with mouse-face overlapping
22562 the one we are currently highlighting, we have to
22563 check if we enter the overlapping overlay, and then
22564 highlight only that. */
22565 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22566 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22567 {
22568 /* Find the highest priority overlay that has a mouse-face
22569 property. */
22570 overlay = Qnil;
22571 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22572 {
22573 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22574 if (!NILP (mouse_face))
22575 overlay = overlay_vec[i];
22576 }
22577
22578 /* If we're actually highlighting the same overlay as
22579 before, there's no need to do that again. */
22580 if (!NILP (overlay)
22581 && EQ (overlay, dpyinfo->mouse_face_overlay))
22582 goto check_help_echo;
22583
22584 dpyinfo->mouse_face_overlay = overlay;
22585
22586 /* Clear the display of the old active region, if any. */
22587 if (clear_mouse_face (dpyinfo))
22588 cursor = No_Cursor;
22589
22590 /* If no overlay applies, get a text property. */
22591 if (NILP (overlay))
22592 mouse_face = Fget_text_property (position, Qmouse_face, object);
22593
22594 /* Handle the overlay case. */
22595 if (!NILP (overlay))
22596 {
22597 /* Find the range of text around this char that
22598 should be active. */
22599 Lisp_Object before, after;
22600 int ignore;
22601
22602 before = Foverlay_start (overlay);
22603 after = Foverlay_end (overlay);
22604 /* Record this as the current active region. */
22605 fast_find_position (w, XFASTINT (before),
22606 &dpyinfo->mouse_face_beg_col,
22607 &dpyinfo->mouse_face_beg_row,
22608 &dpyinfo->mouse_face_beg_x,
22609 &dpyinfo->mouse_face_beg_y, Qnil);
22610
22611 dpyinfo->mouse_face_past_end
22612 = !fast_find_position (w, XFASTINT (after),
22613 &dpyinfo->mouse_face_end_col,
22614 &dpyinfo->mouse_face_end_row,
22615 &dpyinfo->mouse_face_end_x,
22616 &dpyinfo->mouse_face_end_y, Qnil);
22617 dpyinfo->mouse_face_window = window;
22618
22619 dpyinfo->mouse_face_face_id
22620 = face_at_buffer_position (w, pos, 0, 0,
22621 &ignore, pos + 1,
22622 !dpyinfo->mouse_face_hidden);
22623
22624 /* Display it as active. */
22625 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22626 cursor = No_Cursor;
22627 }
22628 /* Handle the text property case. */
22629 else if (!NILP (mouse_face) && BUFFERP (object))
22630 {
22631 /* Find the range of text around this char that
22632 should be active. */
22633 Lisp_Object before, after, beginning, end;
22634 int ignore;
22635
22636 beginning = Fmarker_position (w->start);
22637 end = make_number (BUF_Z (XBUFFER (object))
22638 - XFASTINT (w->window_end_pos));
22639 before
22640 = Fprevious_single_property_change (make_number (pos + 1),
22641 Qmouse_face,
22642 object, beginning);
22643 after
22644 = Fnext_single_property_change (position, Qmouse_face,
22645 object, end);
22646
22647 /* Record this as the current active region. */
22648 fast_find_position (w, XFASTINT (before),
22649 &dpyinfo->mouse_face_beg_col,
22650 &dpyinfo->mouse_face_beg_row,
22651 &dpyinfo->mouse_face_beg_x,
22652 &dpyinfo->mouse_face_beg_y, Qnil);
22653 dpyinfo->mouse_face_past_end
22654 = !fast_find_position (w, XFASTINT (after),
22655 &dpyinfo->mouse_face_end_col,
22656 &dpyinfo->mouse_face_end_row,
22657 &dpyinfo->mouse_face_end_x,
22658 &dpyinfo->mouse_face_end_y, Qnil);
22659 dpyinfo->mouse_face_window = window;
22660
22661 if (BUFFERP (object))
22662 dpyinfo->mouse_face_face_id
22663 = face_at_buffer_position (w, pos, 0, 0,
22664 &ignore, pos + 1,
22665 !dpyinfo->mouse_face_hidden);
22666
22667 /* Display it as active. */
22668 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22669 cursor = No_Cursor;
22670 }
22671 else if (!NILP (mouse_face) && STRINGP (object))
22672 {
22673 Lisp_Object b, e;
22674 int ignore;
22675
22676 b = Fprevious_single_property_change (make_number (pos + 1),
22677 Qmouse_face,
22678 object, Qnil);
22679 e = Fnext_single_property_change (position, Qmouse_face,
22680 object, Qnil);
22681 if (NILP (b))
22682 b = make_number (0);
22683 if (NILP (e))
22684 e = make_number (SCHARS (object) - 1);
22685
22686 fast_find_string_pos (w, XINT (b), object,
22687 &dpyinfo->mouse_face_beg_col,
22688 &dpyinfo->mouse_face_beg_row,
22689 &dpyinfo->mouse_face_beg_x,
22690 &dpyinfo->mouse_face_beg_y, 0);
22691 fast_find_string_pos (w, XINT (e), object,
22692 &dpyinfo->mouse_face_end_col,
22693 &dpyinfo->mouse_face_end_row,
22694 &dpyinfo->mouse_face_end_x,
22695 &dpyinfo->mouse_face_end_y, 1);
22696 dpyinfo->mouse_face_past_end = 0;
22697 dpyinfo->mouse_face_window = window;
22698 dpyinfo->mouse_face_face_id
22699 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22700 glyph->face_id, 1);
22701 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22702 cursor = No_Cursor;
22703 }
22704 else if (STRINGP (object) && NILP (mouse_face))
22705 {
22706 /* A string which doesn't have mouse-face, but
22707 the text ``under'' it might have. */
22708 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22709 int start = MATRIX_ROW_START_CHARPOS (r);
22710
22711 pos = string_buffer_position (w, object, start);
22712 if (pos > 0)
22713 mouse_face = get_char_property_and_overlay (make_number (pos),
22714 Qmouse_face,
22715 w->buffer,
22716 &overlay);
22717 if (!NILP (mouse_face) && !NILP (overlay))
22718 {
22719 Lisp_Object before = Foverlay_start (overlay);
22720 Lisp_Object after = Foverlay_end (overlay);
22721 int ignore;
22722
22723 /* Note that we might not be able to find position
22724 BEFORE in the glyph matrix if the overlay is
22725 entirely covered by a `display' property. In
22726 this case, we overshoot. So let's stop in
22727 the glyph matrix before glyphs for OBJECT. */
22728 fast_find_position (w, XFASTINT (before),
22729 &dpyinfo->mouse_face_beg_col,
22730 &dpyinfo->mouse_face_beg_row,
22731 &dpyinfo->mouse_face_beg_x,
22732 &dpyinfo->mouse_face_beg_y,
22733 object);
22734
22735 dpyinfo->mouse_face_past_end
22736 = !fast_find_position (w, XFASTINT (after),
22737 &dpyinfo->mouse_face_end_col,
22738 &dpyinfo->mouse_face_end_row,
22739 &dpyinfo->mouse_face_end_x,
22740 &dpyinfo->mouse_face_end_y,
22741 Qnil);
22742 dpyinfo->mouse_face_window = window;
22743 dpyinfo->mouse_face_face_id
22744 = face_at_buffer_position (w, pos, 0, 0,
22745 &ignore, pos + 1,
22746 !dpyinfo->mouse_face_hidden);
22747
22748 /* Display it as active. */
22749 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22750 cursor = No_Cursor;
22751 }
22752 }
22753 }
22754
22755 check_help_echo:
22756
22757 /* Look for a `help-echo' property. */
22758 if (NILP (help_echo_string)) {
22759 Lisp_Object help, overlay;
22760
22761 /* Check overlays first. */
22762 help = overlay = Qnil;
22763 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22764 {
22765 overlay = overlay_vec[i];
22766 help = Foverlay_get (overlay, Qhelp_echo);
22767 }
22768
22769 if (!NILP (help))
22770 {
22771 help_echo_string = help;
22772 help_echo_window = window;
22773 help_echo_object = overlay;
22774 help_echo_pos = pos;
22775 }
22776 else
22777 {
22778 Lisp_Object object = glyph->object;
22779 int charpos = glyph->charpos;
22780
22781 /* Try text properties. */
22782 if (STRINGP (object)
22783 && charpos >= 0
22784 && charpos < SCHARS (object))
22785 {
22786 help = Fget_text_property (make_number (charpos),
22787 Qhelp_echo, object);
22788 if (NILP (help))
22789 {
22790 /* If the string itself doesn't specify a help-echo,
22791 see if the buffer text ``under'' it does. */
22792 struct glyph_row *r
22793 = MATRIX_ROW (w->current_matrix, vpos);
22794 int start = MATRIX_ROW_START_CHARPOS (r);
22795 int pos = string_buffer_position (w, object, start);
22796 if (pos > 0)
22797 {
22798 help = Fget_char_property (make_number (pos),
22799 Qhelp_echo, w->buffer);
22800 if (!NILP (help))
22801 {
22802 charpos = pos;
22803 object = w->buffer;
22804 }
22805 }
22806 }
22807 }
22808 else if (BUFFERP (object)
22809 && charpos >= BEGV
22810 && charpos < ZV)
22811 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22812 object);
22813
22814 if (!NILP (help))
22815 {
22816 help_echo_string = help;
22817 help_echo_window = window;
22818 help_echo_object = object;
22819 help_echo_pos = charpos;
22820 }
22821 }
22822 }
22823
22824 /* Look for a `pointer' property. */
22825 if (NILP (pointer))
22826 {
22827 /* Check overlays first. */
22828 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22829 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22830
22831 if (NILP (pointer))
22832 {
22833 Lisp_Object object = glyph->object;
22834 int charpos = glyph->charpos;
22835
22836 /* Try text properties. */
22837 if (STRINGP (object)
22838 && charpos >= 0
22839 && charpos < SCHARS (object))
22840 {
22841 pointer = Fget_text_property (make_number (charpos),
22842 Qpointer, object);
22843 if (NILP (pointer))
22844 {
22845 /* If the string itself doesn't specify a pointer,
22846 see if the buffer text ``under'' it does. */
22847 struct glyph_row *r
22848 = MATRIX_ROW (w->current_matrix, vpos);
22849 int start = MATRIX_ROW_START_CHARPOS (r);
22850 int pos = string_buffer_position (w, object, start);
22851 if (pos > 0)
22852 pointer = Fget_char_property (make_number (pos),
22853 Qpointer, w->buffer);
22854 }
22855 }
22856 else if (BUFFERP (object)
22857 && charpos >= BEGV
22858 && charpos < ZV)
22859 pointer = Fget_text_property (make_number (charpos),
22860 Qpointer, object);
22861 }
22862 }
22863
22864 BEGV = obegv;
22865 ZV = ozv;
22866 current_buffer = obuf;
22867 }
22868
22869 set_cursor:
22870
22871 define_frame_cursor1 (f, cursor, pointer);
22872 }
22873
22874
22875 /* EXPORT for RIF:
22876 Clear any mouse-face on window W. This function is part of the
22877 redisplay interface, and is called from try_window_id and similar
22878 functions to ensure the mouse-highlight is off. */
22879
22880 void
22881 x_clear_window_mouse_face (w)
22882 struct window *w;
22883 {
22884 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22885 Lisp_Object window;
22886
22887 BLOCK_INPUT;
22888 XSETWINDOW (window, w);
22889 if (EQ (window, dpyinfo->mouse_face_window))
22890 clear_mouse_face (dpyinfo);
22891 UNBLOCK_INPUT;
22892 }
22893
22894
22895 /* EXPORT:
22896 Just discard the mouse face information for frame F, if any.
22897 This is used when the size of F is changed. */
22898
22899 void
22900 cancel_mouse_face (f)
22901 struct frame *f;
22902 {
22903 Lisp_Object window;
22904 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22905
22906 window = dpyinfo->mouse_face_window;
22907 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22908 {
22909 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22910 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22911 dpyinfo->mouse_face_window = Qnil;
22912 }
22913 }
22914
22915
22916 #endif /* HAVE_WINDOW_SYSTEM */
22917
22918 \f
22919 /***********************************************************************
22920 Exposure Events
22921 ***********************************************************************/
22922
22923 #ifdef HAVE_WINDOW_SYSTEM
22924
22925 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22926 which intersects rectangle R. R is in window-relative coordinates. */
22927
22928 static void
22929 expose_area (w, row, r, area)
22930 struct window *w;
22931 struct glyph_row *row;
22932 XRectangle *r;
22933 enum glyph_row_area area;
22934 {
22935 struct glyph *first = row->glyphs[area];
22936 struct glyph *end = row->glyphs[area] + row->used[area];
22937 struct glyph *last;
22938 int first_x, start_x, x;
22939
22940 if (area == TEXT_AREA && row->fill_line_p)
22941 /* If row extends face to end of line write the whole line. */
22942 draw_glyphs (w, 0, row, area,
22943 0, row->used[area],
22944 DRAW_NORMAL_TEXT, 0);
22945 else
22946 {
22947 /* Set START_X to the window-relative start position for drawing glyphs of
22948 AREA. The first glyph of the text area can be partially visible.
22949 The first glyphs of other areas cannot. */
22950 start_x = window_box_left_offset (w, area);
22951 x = start_x;
22952 if (area == TEXT_AREA)
22953 x += row->x;
22954
22955 /* Find the first glyph that must be redrawn. */
22956 while (first < end
22957 && x + first->pixel_width < r->x)
22958 {
22959 x += first->pixel_width;
22960 ++first;
22961 }
22962
22963 /* Find the last one. */
22964 last = first;
22965 first_x = x;
22966 while (last < end
22967 && x < r->x + r->width)
22968 {
22969 x += last->pixel_width;
22970 ++last;
22971 }
22972
22973 /* Repaint. */
22974 if (last > first)
22975 draw_glyphs (w, first_x - start_x, row, area,
22976 first - row->glyphs[area], last - row->glyphs[area],
22977 DRAW_NORMAL_TEXT, 0);
22978 }
22979 }
22980
22981
22982 /* Redraw the parts of the glyph row ROW on window W intersecting
22983 rectangle R. R is in window-relative coordinates. Value is
22984 non-zero if mouse-face was overwritten. */
22985
22986 static int
22987 expose_line (w, row, r)
22988 struct window *w;
22989 struct glyph_row *row;
22990 XRectangle *r;
22991 {
22992 xassert (row->enabled_p);
22993
22994 if (row->mode_line_p || w->pseudo_window_p)
22995 draw_glyphs (w, 0, row, TEXT_AREA,
22996 0, row->used[TEXT_AREA],
22997 DRAW_NORMAL_TEXT, 0);
22998 else
22999 {
23000 if (row->used[LEFT_MARGIN_AREA])
23001 expose_area (w, row, r, LEFT_MARGIN_AREA);
23002 if (row->used[TEXT_AREA])
23003 expose_area (w, row, r, TEXT_AREA);
23004 if (row->used[RIGHT_MARGIN_AREA])
23005 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23006 draw_row_fringe_bitmaps (w, row);
23007 }
23008
23009 return row->mouse_face_p;
23010 }
23011
23012
23013 /* Redraw those parts of glyphs rows during expose event handling that
23014 overlap other rows. Redrawing of an exposed line writes over parts
23015 of lines overlapping that exposed line; this function fixes that.
23016
23017 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23018 row in W's current matrix that is exposed and overlaps other rows.
23019 LAST_OVERLAPPING_ROW is the last such row. */
23020
23021 static void
23022 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23023 struct window *w;
23024 struct glyph_row *first_overlapping_row;
23025 struct glyph_row *last_overlapping_row;
23026 {
23027 struct glyph_row *row;
23028
23029 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23030 if (row->overlapping_p)
23031 {
23032 xassert (row->enabled_p && !row->mode_line_p);
23033
23034 if (row->used[LEFT_MARGIN_AREA])
23035 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23036
23037 if (row->used[TEXT_AREA])
23038 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23039
23040 if (row->used[RIGHT_MARGIN_AREA])
23041 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23042 }
23043 }
23044
23045
23046 /* Return non-zero if W's cursor intersects rectangle R. */
23047
23048 static int
23049 phys_cursor_in_rect_p (w, r)
23050 struct window *w;
23051 XRectangle *r;
23052 {
23053 XRectangle cr, result;
23054 struct glyph *cursor_glyph;
23055
23056 cursor_glyph = get_phys_cursor_glyph (w);
23057 if (cursor_glyph)
23058 {
23059 /* r is relative to W's box, but w->phys_cursor.x is relative
23060 to left edge of W's TEXT area. Adjust it. */
23061 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23062 cr.y = w->phys_cursor.y;
23063 cr.width = cursor_glyph->pixel_width;
23064 cr.height = w->phys_cursor_height;
23065 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23066 I assume the effect is the same -- and this is portable. */
23067 return x_intersect_rectangles (&cr, r, &result);
23068 }
23069 else
23070 return 0;
23071 }
23072
23073
23074 /* EXPORT:
23075 Draw a vertical window border to the right of window W if W doesn't
23076 have vertical scroll bars. */
23077
23078 void
23079 x_draw_vertical_border (w)
23080 struct window *w;
23081 {
23082 /* We could do better, if we knew what type of scroll-bar the adjacent
23083 windows (on either side) have... But we don't :-(
23084 However, I think this works ok. ++KFS 2003-04-25 */
23085
23086 /* Redraw borders between horizontally adjacent windows. Don't
23087 do it for frames with vertical scroll bars because either the
23088 right scroll bar of a window, or the left scroll bar of its
23089 neighbor will suffice as a border. */
23090 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23091 return;
23092
23093 if (!WINDOW_RIGHTMOST_P (w)
23094 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23095 {
23096 int x0, x1, y0, y1;
23097
23098 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23099 y1 -= 1;
23100
23101 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23102 x1 -= 1;
23103
23104 rif->draw_vertical_window_border (w, x1, y0, y1);
23105 }
23106 else if (!WINDOW_LEFTMOST_P (w)
23107 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23108 {
23109 int x0, x1, y0, y1;
23110
23111 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23112 y1 -= 1;
23113
23114 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23115 x0 -= 1;
23116
23117 rif->draw_vertical_window_border (w, x0, y0, y1);
23118 }
23119 }
23120
23121
23122 /* Redraw the part of window W intersection rectangle FR. Pixel
23123 coordinates in FR are frame-relative. Call this function with
23124 input blocked. Value is non-zero if the exposure overwrites
23125 mouse-face. */
23126
23127 static int
23128 expose_window (w, fr)
23129 struct window *w;
23130 XRectangle *fr;
23131 {
23132 struct frame *f = XFRAME (w->frame);
23133 XRectangle wr, r;
23134 int mouse_face_overwritten_p = 0;
23135
23136 /* If window is not yet fully initialized, do nothing. This can
23137 happen when toolkit scroll bars are used and a window is split.
23138 Reconfiguring the scroll bar will generate an expose for a newly
23139 created window. */
23140 if (w->current_matrix == NULL)
23141 return 0;
23142
23143 /* When we're currently updating the window, display and current
23144 matrix usually don't agree. Arrange for a thorough display
23145 later. */
23146 if (w == updated_window)
23147 {
23148 SET_FRAME_GARBAGED (f);
23149 return 0;
23150 }
23151
23152 /* Frame-relative pixel rectangle of W. */
23153 wr.x = WINDOW_LEFT_EDGE_X (w);
23154 wr.y = WINDOW_TOP_EDGE_Y (w);
23155 wr.width = WINDOW_TOTAL_WIDTH (w);
23156 wr.height = WINDOW_TOTAL_HEIGHT (w);
23157
23158 if (x_intersect_rectangles (fr, &wr, &r))
23159 {
23160 int yb = window_text_bottom_y (w);
23161 struct glyph_row *row;
23162 int cursor_cleared_p;
23163 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23164
23165 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23166 r.x, r.y, r.width, r.height));
23167
23168 /* Convert to window coordinates. */
23169 r.x -= WINDOW_LEFT_EDGE_X (w);
23170 r.y -= WINDOW_TOP_EDGE_Y (w);
23171
23172 /* Turn off the cursor. */
23173 if (!w->pseudo_window_p
23174 && phys_cursor_in_rect_p (w, &r))
23175 {
23176 x_clear_cursor (w);
23177 cursor_cleared_p = 1;
23178 }
23179 else
23180 cursor_cleared_p = 0;
23181
23182 /* Update lines intersecting rectangle R. */
23183 first_overlapping_row = last_overlapping_row = NULL;
23184 for (row = w->current_matrix->rows;
23185 row->enabled_p;
23186 ++row)
23187 {
23188 int y0 = row->y;
23189 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23190
23191 if ((y0 >= r.y && y0 < r.y + r.height)
23192 || (y1 > r.y && y1 < r.y + r.height)
23193 || (r.y >= y0 && r.y < y1)
23194 || (r.y + r.height > y0 && r.y + r.height < y1))
23195 {
23196 /* A header line may be overlapping, but there is no need
23197 to fix overlapping areas for them. KFS 2005-02-12 */
23198 if (row->overlapping_p && !row->mode_line_p)
23199 {
23200 if (first_overlapping_row == NULL)
23201 first_overlapping_row = row;
23202 last_overlapping_row = row;
23203 }
23204
23205 if (expose_line (w, row, &r))
23206 mouse_face_overwritten_p = 1;
23207 }
23208
23209 if (y1 >= yb)
23210 break;
23211 }
23212
23213 /* Display the mode line if there is one. */
23214 if (WINDOW_WANTS_MODELINE_P (w)
23215 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23216 row->enabled_p)
23217 && row->y < r.y + r.height)
23218 {
23219 if (expose_line (w, row, &r))
23220 mouse_face_overwritten_p = 1;
23221 }
23222
23223 if (!w->pseudo_window_p)
23224 {
23225 /* Fix the display of overlapping rows. */
23226 if (first_overlapping_row)
23227 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23228
23229 /* Draw border between windows. */
23230 x_draw_vertical_border (w);
23231
23232 /* Turn the cursor on again. */
23233 if (cursor_cleared_p)
23234 update_window_cursor (w, 1);
23235 }
23236 }
23237
23238 return mouse_face_overwritten_p;
23239 }
23240
23241
23242
23243 /* Redraw (parts) of all windows in the window tree rooted at W that
23244 intersect R. R contains frame pixel coordinates. Value is
23245 non-zero if the exposure overwrites mouse-face. */
23246
23247 static int
23248 expose_window_tree (w, r)
23249 struct window *w;
23250 XRectangle *r;
23251 {
23252 struct frame *f = XFRAME (w->frame);
23253 int mouse_face_overwritten_p = 0;
23254
23255 while (w && !FRAME_GARBAGED_P (f))
23256 {
23257 if (!NILP (w->hchild))
23258 mouse_face_overwritten_p
23259 |= expose_window_tree (XWINDOW (w->hchild), r);
23260 else if (!NILP (w->vchild))
23261 mouse_face_overwritten_p
23262 |= expose_window_tree (XWINDOW (w->vchild), r);
23263 else
23264 mouse_face_overwritten_p |= expose_window (w, r);
23265
23266 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23267 }
23268
23269 return mouse_face_overwritten_p;
23270 }
23271
23272
23273 /* EXPORT:
23274 Redisplay an exposed area of frame F. X and Y are the upper-left
23275 corner of the exposed rectangle. W and H are width and height of
23276 the exposed area. All are pixel values. W or H zero means redraw
23277 the entire frame. */
23278
23279 void
23280 expose_frame (f, x, y, w, h)
23281 struct frame *f;
23282 int x, y, w, h;
23283 {
23284 XRectangle r;
23285 int mouse_face_overwritten_p = 0;
23286
23287 TRACE ((stderr, "expose_frame "));
23288
23289 /* No need to redraw if frame will be redrawn soon. */
23290 if (FRAME_GARBAGED_P (f))
23291 {
23292 TRACE ((stderr, " garbaged\n"));
23293 return;
23294 }
23295
23296 /* If basic faces haven't been realized yet, there is no point in
23297 trying to redraw anything. This can happen when we get an expose
23298 event while Emacs is starting, e.g. by moving another window. */
23299 if (FRAME_FACE_CACHE (f) == NULL
23300 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23301 {
23302 TRACE ((stderr, " no faces\n"));
23303 return;
23304 }
23305
23306 if (w == 0 || h == 0)
23307 {
23308 r.x = r.y = 0;
23309 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23310 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23311 }
23312 else
23313 {
23314 r.x = x;
23315 r.y = y;
23316 r.width = w;
23317 r.height = h;
23318 }
23319
23320 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23321 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23322
23323 if (WINDOWP (f->tool_bar_window))
23324 mouse_face_overwritten_p
23325 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23326
23327 #ifdef HAVE_X_WINDOWS
23328 #ifndef MSDOS
23329 #ifndef USE_X_TOOLKIT
23330 if (WINDOWP (f->menu_bar_window))
23331 mouse_face_overwritten_p
23332 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23333 #endif /* not USE_X_TOOLKIT */
23334 #endif
23335 #endif
23336
23337 /* Some window managers support a focus-follows-mouse style with
23338 delayed raising of frames. Imagine a partially obscured frame,
23339 and moving the mouse into partially obscured mouse-face on that
23340 frame. The visible part of the mouse-face will be highlighted,
23341 then the WM raises the obscured frame. With at least one WM, KDE
23342 2.1, Emacs is not getting any event for the raising of the frame
23343 (even tried with SubstructureRedirectMask), only Expose events.
23344 These expose events will draw text normally, i.e. not
23345 highlighted. Which means we must redo the highlight here.
23346 Subsume it under ``we love X''. --gerd 2001-08-15 */
23347 /* Included in Windows version because Windows most likely does not
23348 do the right thing if any third party tool offers
23349 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23350 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23351 {
23352 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23353 if (f == dpyinfo->mouse_face_mouse_frame)
23354 {
23355 int x = dpyinfo->mouse_face_mouse_x;
23356 int y = dpyinfo->mouse_face_mouse_y;
23357 clear_mouse_face (dpyinfo);
23358 note_mouse_highlight (f, x, y);
23359 }
23360 }
23361 }
23362
23363
23364 /* EXPORT:
23365 Determine the intersection of two rectangles R1 and R2. Return
23366 the intersection in *RESULT. Value is non-zero if RESULT is not
23367 empty. */
23368
23369 int
23370 x_intersect_rectangles (r1, r2, result)
23371 XRectangle *r1, *r2, *result;
23372 {
23373 XRectangle *left, *right;
23374 XRectangle *upper, *lower;
23375 int intersection_p = 0;
23376
23377 /* Rearrange so that R1 is the left-most rectangle. */
23378 if (r1->x < r2->x)
23379 left = r1, right = r2;
23380 else
23381 left = r2, right = r1;
23382
23383 /* X0 of the intersection is right.x0, if this is inside R1,
23384 otherwise there is no intersection. */
23385 if (right->x <= left->x + left->width)
23386 {
23387 result->x = right->x;
23388
23389 /* The right end of the intersection is the minimum of the
23390 the right ends of left and right. */
23391 result->width = (min (left->x + left->width, right->x + right->width)
23392 - result->x);
23393
23394 /* Same game for Y. */
23395 if (r1->y < r2->y)
23396 upper = r1, lower = r2;
23397 else
23398 upper = r2, lower = r1;
23399
23400 /* The upper end of the intersection is lower.y0, if this is inside
23401 of upper. Otherwise, there is no intersection. */
23402 if (lower->y <= upper->y + upper->height)
23403 {
23404 result->y = lower->y;
23405
23406 /* The lower end of the intersection is the minimum of the lower
23407 ends of upper and lower. */
23408 result->height = (min (lower->y + lower->height,
23409 upper->y + upper->height)
23410 - result->y);
23411 intersection_p = 1;
23412 }
23413 }
23414
23415 return intersection_p;
23416 }
23417
23418 #endif /* HAVE_WINDOW_SYSTEM */
23419
23420 \f
23421 /***********************************************************************
23422 Initialization
23423 ***********************************************************************/
23424
23425 void
23426 syms_of_xdisp ()
23427 {
23428 Vwith_echo_area_save_vector = Qnil;
23429 staticpro (&Vwith_echo_area_save_vector);
23430
23431 Vmessage_stack = Qnil;
23432 staticpro (&Vmessage_stack);
23433
23434 Qinhibit_redisplay = intern ("inhibit-redisplay");
23435 staticpro (&Qinhibit_redisplay);
23436
23437 message_dolog_marker1 = Fmake_marker ();
23438 staticpro (&message_dolog_marker1);
23439 message_dolog_marker2 = Fmake_marker ();
23440 staticpro (&message_dolog_marker2);
23441 message_dolog_marker3 = Fmake_marker ();
23442 staticpro (&message_dolog_marker3);
23443
23444 #if GLYPH_DEBUG
23445 defsubr (&Sdump_frame_glyph_matrix);
23446 defsubr (&Sdump_glyph_matrix);
23447 defsubr (&Sdump_glyph_row);
23448 defsubr (&Sdump_tool_bar_row);
23449 defsubr (&Strace_redisplay);
23450 defsubr (&Strace_to_stderr);
23451 #endif
23452 #ifdef HAVE_WINDOW_SYSTEM
23453 defsubr (&Stool_bar_lines_needed);
23454 defsubr (&Slookup_image_map);
23455 #endif
23456 defsubr (&Sformat_mode_line);
23457
23458 staticpro (&Qmenu_bar_update_hook);
23459 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23460
23461 staticpro (&Qoverriding_terminal_local_map);
23462 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23463
23464 staticpro (&Qoverriding_local_map);
23465 Qoverriding_local_map = intern ("overriding-local-map");
23466
23467 staticpro (&Qwindow_scroll_functions);
23468 Qwindow_scroll_functions = intern ("window-scroll-functions");
23469
23470 staticpro (&Qredisplay_end_trigger_functions);
23471 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23472
23473 staticpro (&Qinhibit_point_motion_hooks);
23474 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23475
23476 QCdata = intern (":data");
23477 staticpro (&QCdata);
23478 Qdisplay = intern ("display");
23479 staticpro (&Qdisplay);
23480 Qspace_width = intern ("space-width");
23481 staticpro (&Qspace_width);
23482 Qraise = intern ("raise");
23483 staticpro (&Qraise);
23484 Qslice = intern ("slice");
23485 staticpro (&Qslice);
23486 Qspace = intern ("space");
23487 staticpro (&Qspace);
23488 Qmargin = intern ("margin");
23489 staticpro (&Qmargin);
23490 Qpointer = intern ("pointer");
23491 staticpro (&Qpointer);
23492 Qleft_margin = intern ("left-margin");
23493 staticpro (&Qleft_margin);
23494 Qright_margin = intern ("right-margin");
23495 staticpro (&Qright_margin);
23496 Qcenter = intern ("center");
23497 staticpro (&Qcenter);
23498 Qline_height = intern ("line-height");
23499 staticpro (&Qline_height);
23500 QCalign_to = intern (":align-to");
23501 staticpro (&QCalign_to);
23502 QCrelative_width = intern (":relative-width");
23503 staticpro (&QCrelative_width);
23504 QCrelative_height = intern (":relative-height");
23505 staticpro (&QCrelative_height);
23506 QCeval = intern (":eval");
23507 staticpro (&QCeval);
23508 QCpropertize = intern (":propertize");
23509 staticpro (&QCpropertize);
23510 QCfile = intern (":file");
23511 staticpro (&QCfile);
23512 Qfontified = intern ("fontified");
23513 staticpro (&Qfontified);
23514 Qfontification_functions = intern ("fontification-functions");
23515 staticpro (&Qfontification_functions);
23516 Qtrailing_whitespace = intern ("trailing-whitespace");
23517 staticpro (&Qtrailing_whitespace);
23518 Qescape_glyph = intern ("escape-glyph");
23519 staticpro (&Qescape_glyph);
23520 Qnobreak_space = intern ("nobreak-space");
23521 staticpro (&Qnobreak_space);
23522 Qimage = intern ("image");
23523 staticpro (&Qimage);
23524 QCmap = intern (":map");
23525 staticpro (&QCmap);
23526 QCpointer = intern (":pointer");
23527 staticpro (&QCpointer);
23528 Qrect = intern ("rect");
23529 staticpro (&Qrect);
23530 Qcircle = intern ("circle");
23531 staticpro (&Qcircle);
23532 Qpoly = intern ("poly");
23533 staticpro (&Qpoly);
23534 Qmessage_truncate_lines = intern ("message-truncate-lines");
23535 staticpro (&Qmessage_truncate_lines);
23536 Qgrow_only = intern ("grow-only");
23537 staticpro (&Qgrow_only);
23538 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23539 staticpro (&Qinhibit_menubar_update);
23540 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23541 staticpro (&Qinhibit_eval_during_redisplay);
23542 Qposition = intern ("position");
23543 staticpro (&Qposition);
23544 Qbuffer_position = intern ("buffer-position");
23545 staticpro (&Qbuffer_position);
23546 Qobject = intern ("object");
23547 staticpro (&Qobject);
23548 Qbar = intern ("bar");
23549 staticpro (&Qbar);
23550 Qhbar = intern ("hbar");
23551 staticpro (&Qhbar);
23552 Qbox = intern ("box");
23553 staticpro (&Qbox);
23554 Qhollow = intern ("hollow");
23555 staticpro (&Qhollow);
23556 Qhand = intern ("hand");
23557 staticpro (&Qhand);
23558 Qarrow = intern ("arrow");
23559 staticpro (&Qarrow);
23560 Qtext = intern ("text");
23561 staticpro (&Qtext);
23562 Qrisky_local_variable = intern ("risky-local-variable");
23563 staticpro (&Qrisky_local_variable);
23564 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23565 staticpro (&Qinhibit_free_realized_faces);
23566
23567 list_of_error = Fcons (Fcons (intern ("error"),
23568 Fcons (intern ("void-variable"), Qnil)),
23569 Qnil);
23570 staticpro (&list_of_error);
23571
23572 Qlast_arrow_position = intern ("last-arrow-position");
23573 staticpro (&Qlast_arrow_position);
23574 Qlast_arrow_string = intern ("last-arrow-string");
23575 staticpro (&Qlast_arrow_string);
23576
23577 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23578 staticpro (&Qoverlay_arrow_string);
23579 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23580 staticpro (&Qoverlay_arrow_bitmap);
23581
23582 echo_buffer[0] = echo_buffer[1] = Qnil;
23583 staticpro (&echo_buffer[0]);
23584 staticpro (&echo_buffer[1]);
23585
23586 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23587 staticpro (&echo_area_buffer[0]);
23588 staticpro (&echo_area_buffer[1]);
23589
23590 Vmessages_buffer_name = build_string ("*Messages*");
23591 staticpro (&Vmessages_buffer_name);
23592
23593 mode_line_proptrans_alist = Qnil;
23594 staticpro (&mode_line_proptrans_alist);
23595 mode_line_string_list = Qnil;
23596 staticpro (&mode_line_string_list);
23597 mode_line_string_face = Qnil;
23598 staticpro (&mode_line_string_face);
23599 mode_line_string_face_prop = Qnil;
23600 staticpro (&mode_line_string_face_prop);
23601 Vmode_line_unwind_vector = Qnil;
23602 staticpro (&Vmode_line_unwind_vector);
23603
23604 help_echo_string = Qnil;
23605 staticpro (&help_echo_string);
23606 help_echo_object = Qnil;
23607 staticpro (&help_echo_object);
23608 help_echo_window = Qnil;
23609 staticpro (&help_echo_window);
23610 previous_help_echo_string = Qnil;
23611 staticpro (&previous_help_echo_string);
23612 help_echo_pos = -1;
23613
23614 #ifdef HAVE_WINDOW_SYSTEM
23615 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23616 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23617 For example, if a block cursor is over a tab, it will be drawn as
23618 wide as that tab on the display. */);
23619 x_stretch_cursor_p = 0;
23620 #endif
23621
23622 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23623 doc: /* *Non-nil means highlight trailing whitespace.
23624 The face used for trailing whitespace is `trailing-whitespace'. */);
23625 Vshow_trailing_whitespace = Qnil;
23626
23627 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23628 doc: /* *Control highlighting of nobreak space and soft hyphen.
23629 A value of t means highlight the character itself (for nobreak space,
23630 use face `nobreak-space').
23631 A value of nil means no highlighting.
23632 Other values mean display the escape glyph followed by an ordinary
23633 space or ordinary hyphen. */);
23634 Vnobreak_char_display = Qt;
23635
23636 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23637 doc: /* *The pointer shape to show in void text areas.
23638 A value of nil means to show the text pointer. Other options are `arrow',
23639 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23640 Vvoid_text_area_pointer = Qarrow;
23641
23642 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23643 doc: /* Non-nil means don't actually do any redisplay.
23644 This is used for internal purposes. */);
23645 Vinhibit_redisplay = Qnil;
23646
23647 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23648 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23649 Vglobal_mode_string = Qnil;
23650
23651 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23652 doc: /* Marker for where to display an arrow on top of the buffer text.
23653 This must be the beginning of a line in order to work.
23654 See also `overlay-arrow-string'. */);
23655 Voverlay_arrow_position = Qnil;
23656
23657 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23658 doc: /* String to display as an arrow in non-window frames.
23659 See also `overlay-arrow-position'. */);
23660 Voverlay_arrow_string = build_string ("=>");
23661
23662 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23663 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23664 The symbols on this list are examined during redisplay to determine
23665 where to display overlay arrows. */);
23666 Voverlay_arrow_variable_list
23667 = Fcons (intern ("overlay-arrow-position"), Qnil);
23668
23669 DEFVAR_INT ("scroll-step", &scroll_step,
23670 doc: /* *The number of lines to try scrolling a window by when point moves out.
23671 If that fails to bring point back on frame, point is centered instead.
23672 If this is zero, point is always centered after it moves off frame.
23673 If you want scrolling to always be a line at a time, you should set
23674 `scroll-conservatively' to a large value rather than set this to 1. */);
23675
23676 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23677 doc: /* *Scroll up to this many lines, to bring point back on screen.
23678 A value of zero means to scroll the text to center point vertically
23679 in the window. */);
23680 scroll_conservatively = 0;
23681
23682 DEFVAR_INT ("scroll-margin", &scroll_margin,
23683 doc: /* *Number of lines of margin at the top and bottom of a window.
23684 Recenter the window whenever point gets within this many lines
23685 of the top or bottom of the window. */);
23686 scroll_margin = 0;
23687
23688 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23689 doc: /* Pixels per inch value for non-window system displays.
23690 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23691 Vdisplay_pixels_per_inch = make_float (72.0);
23692
23693 #if GLYPH_DEBUG
23694 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23695 #endif
23696
23697 DEFVAR_BOOL ("truncate-partial-width-windows",
23698 &truncate_partial_width_windows,
23699 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23700 truncate_partial_width_windows = 1;
23701
23702 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23703 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23704 Any other value means to use the appropriate face, `mode-line',
23705 `header-line', or `menu' respectively. */);
23706 mode_line_inverse_video = 1;
23707
23708 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23709 doc: /* *Maximum buffer size for which line number should be displayed.
23710 If the buffer is bigger than this, the line number does not appear
23711 in the mode line. A value of nil means no limit. */);
23712 Vline_number_display_limit = Qnil;
23713
23714 DEFVAR_INT ("line-number-display-limit-width",
23715 &line_number_display_limit_width,
23716 doc: /* *Maximum line width (in characters) for line number display.
23717 If the average length of the lines near point is bigger than this, then the
23718 line number may be omitted from the mode line. */);
23719 line_number_display_limit_width = 200;
23720
23721 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23722 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23723 highlight_nonselected_windows = 0;
23724
23725 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23726 doc: /* Non-nil if more than one frame is visible on this display.
23727 Minibuffer-only frames don't count, but iconified frames do.
23728 This variable is not guaranteed to be accurate except while processing
23729 `frame-title-format' and `icon-title-format'. */);
23730
23731 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23732 doc: /* Template for displaying the title bar of visible frames.
23733 \(Assuming the window manager supports this feature.)
23734 This variable has the same structure as `mode-line-format' (which see),
23735 and is used only on frames for which no explicit name has been set
23736 \(see `modify-frame-parameters'). */);
23737
23738 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23739 doc: /* Template for displaying the title bar of an iconified frame.
23740 \(Assuming the window manager supports this feature.)
23741 This variable has the same structure as `mode-line-format' (which see),
23742 and is used only on frames for which no explicit name has been set
23743 \(see `modify-frame-parameters'). */);
23744 Vicon_title_format
23745 = Vframe_title_format
23746 = Fcons (intern ("multiple-frames"),
23747 Fcons (build_string ("%b"),
23748 Fcons (Fcons (empty_string,
23749 Fcons (intern ("invocation-name"),
23750 Fcons (build_string ("@"),
23751 Fcons (intern ("system-name"),
23752 Qnil)))),
23753 Qnil)));
23754
23755 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23756 doc: /* Maximum number of lines to keep in the message log buffer.
23757 If nil, disable message logging. If t, log messages but don't truncate
23758 the buffer when it becomes large. */);
23759 Vmessage_log_max = make_number (50);
23760
23761 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23762 doc: /* Functions called before redisplay, if window sizes have changed.
23763 The value should be a list of functions that take one argument.
23764 Just before redisplay, for each frame, if any of its windows have changed
23765 size since the last redisplay, or have been split or deleted,
23766 all the functions in the list are called, with the frame as argument. */);
23767 Vwindow_size_change_functions = Qnil;
23768
23769 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23770 doc: /* List of functions to call before redisplaying a window with scrolling.
23771 Each function is called with two arguments, the window
23772 and its new display-start position. Note that the value of `window-end'
23773 is not valid when these functions are called. */);
23774 Vwindow_scroll_functions = Qnil;
23775
23776 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23777 doc: /* Functions called when redisplay of a window reaches the end trigger.
23778 Each function is called with two arguments, the window and the end trigger value.
23779 See `set-window-redisplay-end-trigger'. */);
23780 Vredisplay_end_trigger_functions = Qnil;
23781
23782 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23783 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23784 mouse_autoselect_window = 0;
23785
23786 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23787 doc: /* *Non-nil means automatically resize tool-bars.
23788 This increases a tool-bar's height if not all tool-bar items are visible.
23789 It decreases a tool-bar's height when it would display blank lines
23790 otherwise. */);
23791 auto_resize_tool_bars_p = 1;
23792
23793 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23794 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23795 auto_raise_tool_bar_buttons_p = 1;
23796
23797 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23798 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23799 make_cursor_line_fully_visible_p = 1;
23800
23801 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
23802 doc: /* *Border below tool-bar in pixels.
23803 If an integer, use it as the height of the border.
23804 If it is one of `internal-border-width' or `border-width', use the
23805 value of the corresponding frame parameter.
23806 Otherwise, no border is added below the tool-bar. */);
23807 Vtool_bar_border = Qinternal_border_width;
23808
23809 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23810 doc: /* *Margin around tool-bar buttons in pixels.
23811 If an integer, use that for both horizontal and vertical margins.
23812 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23813 HORZ specifying the horizontal margin, and VERT specifying the
23814 vertical margin. */);
23815 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23816
23817 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23818 doc: /* *Relief thickness of tool-bar buttons. */);
23819 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23820
23821 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23822 doc: /* List of functions to call to fontify regions of text.
23823 Each function is called with one argument POS. Functions must
23824 fontify a region starting at POS in the current buffer, and give
23825 fontified regions the property `fontified'. */);
23826 Vfontification_functions = Qnil;
23827 Fmake_variable_buffer_local (Qfontification_functions);
23828
23829 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23830 &unibyte_display_via_language_environment,
23831 doc: /* *Non-nil means display unibyte text according to language environment.
23832 Specifically this means that unibyte non-ASCII characters
23833 are displayed by converting them to the equivalent multibyte characters
23834 according to the current language environment. As a result, they are
23835 displayed according to the current fontset. */);
23836 unibyte_display_via_language_environment = 0;
23837
23838 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23839 doc: /* *Maximum height for resizing mini-windows.
23840 If a float, it specifies a fraction of the mini-window frame's height.
23841 If an integer, it specifies a number of lines. */);
23842 Vmax_mini_window_height = make_float (0.25);
23843
23844 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23845 doc: /* *How to resize mini-windows.
23846 A value of nil means don't automatically resize mini-windows.
23847 A value of t means resize them to fit the text displayed in them.
23848 A value of `grow-only', the default, means let mini-windows grow
23849 only, until their display becomes empty, at which point the windows
23850 go back to their normal size. */);
23851 Vresize_mini_windows = Qgrow_only;
23852
23853 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23854 doc: /* Alist specifying how to blink the cursor off.
23855 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23856 `cursor-type' frame-parameter or variable equals ON-STATE,
23857 comparing using `equal', Emacs uses OFF-STATE to specify
23858 how to blink it off. ON-STATE and OFF-STATE are values for
23859 the `cursor-type' frame parameter.
23860
23861 If a frame's ON-STATE has no entry in this list,
23862 the frame's other specifications determine how to blink the cursor off. */);
23863 Vblink_cursor_alist = Qnil;
23864
23865 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23866 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23867 automatic_hscrolling_p = 1;
23868
23869 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23870 doc: /* *How many columns away from the window edge point is allowed to get
23871 before automatic hscrolling will horizontally scroll the window. */);
23872 hscroll_margin = 5;
23873
23874 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23875 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23876 When point is less than `hscroll-margin' columns from the window
23877 edge, automatic hscrolling will scroll the window by the amount of columns
23878 determined by this variable. If its value is a positive integer, scroll that
23879 many columns. If it's a positive floating-point number, it specifies the
23880 fraction of the window's width to scroll. If it's nil or zero, point will be
23881 centered horizontally after the scroll. Any other value, including negative
23882 numbers, are treated as if the value were zero.
23883
23884 Automatic hscrolling always moves point outside the scroll margin, so if
23885 point was more than scroll step columns inside the margin, the window will
23886 scroll more than the value given by the scroll step.
23887
23888 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23889 and `scroll-right' overrides this variable's effect. */);
23890 Vhscroll_step = make_number (0);
23891
23892 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23893 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23894 Bind this around calls to `message' to let it take effect. */);
23895 message_truncate_lines = 0;
23896
23897 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23898 doc: /* Normal hook run to update the menu bar definitions.
23899 Redisplay runs this hook before it redisplays the menu bar.
23900 This is used to update submenus such as Buffers,
23901 whose contents depend on various data. */);
23902 Vmenu_bar_update_hook = Qnil;
23903
23904 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23905 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23906 inhibit_menubar_update = 0;
23907
23908 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23909 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23910 inhibit_eval_during_redisplay = 0;
23911
23912 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23913 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23914 inhibit_free_realized_faces = 0;
23915
23916 #if GLYPH_DEBUG
23917 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23918 doc: /* Inhibit try_window_id display optimization. */);
23919 inhibit_try_window_id = 0;
23920
23921 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23922 doc: /* Inhibit try_window_reusing display optimization. */);
23923 inhibit_try_window_reusing = 0;
23924
23925 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23926 doc: /* Inhibit try_cursor_movement display optimization. */);
23927 inhibit_try_cursor_movement = 0;
23928 #endif /* GLYPH_DEBUG */
23929 }
23930
23931
23932 /* Initialize this module when Emacs starts. */
23933
23934 void
23935 init_xdisp ()
23936 {
23937 Lisp_Object root_window;
23938 struct window *mini_w;
23939
23940 current_header_line_height = current_mode_line_height = -1;
23941
23942 CHARPOS (this_line_start_pos) = 0;
23943
23944 mini_w = XWINDOW (minibuf_window);
23945 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23946
23947 if (!noninteractive)
23948 {
23949 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23950 int i;
23951
23952 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23953 set_window_height (root_window,
23954 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23955 0);
23956 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23957 set_window_height (minibuf_window, 1, 0);
23958
23959 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23960 mini_w->total_cols = make_number (FRAME_COLS (f));
23961
23962 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23963 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23964 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23965
23966 /* The default ellipsis glyphs `...'. */
23967 for (i = 0; i < 3; ++i)
23968 default_invis_vector[i] = make_number ('.');
23969 }
23970
23971 {
23972 /* Allocate the buffer for frame titles.
23973 Also used for `format-mode-line'. */
23974 int size = 100;
23975 mode_line_noprop_buf = (char *) xmalloc (size);
23976 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23977 mode_line_noprop_ptr = mode_line_noprop_buf;
23978 mode_line_target = MODE_LINE_DISPLAY;
23979 }
23980
23981 help_echo_showing_p = 0;
23982 }
23983
23984
23985 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23986 (do not change this comment) */