Replace `abs' with `eabs'.
[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, 2007 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 3, 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-nil means automatically select any window when the mouse
260 cursor moves into it. */
261 Lisp_Object Vmouse_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-nil means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain.
288
289 If value is `grow-only', only make tool-bar bigger. */
290
291 Lisp_Object Vauto_resize_tool_bars;
292
293 /* Non-zero means draw block and hollow cursor as wide as the glyph
294 under it. For example, if a block cursor is over a tab, it will be
295 drawn as wide as that tab on the display. */
296
297 int x_stretch_cursor_p;
298
299 /* Non-nil means don't actually do any redisplay. */
300
301 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
302
303 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
304
305 int inhibit_eval_during_redisplay;
306
307 /* Names of text properties relevant for redisplay. */
308
309 Lisp_Object Qdisplay;
310 extern Lisp_Object Qface, Qinvisible, Qwidth;
311
312 /* Symbols used in text property values. */
313
314 Lisp_Object Vdisplay_pixels_per_inch;
315 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
316 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
317 Lisp_Object Qslice;
318 Lisp_Object Qcenter;
319 Lisp_Object Qmargin, Qpointer;
320 Lisp_Object Qline_height;
321 extern Lisp_Object Qheight;
322 extern Lisp_Object QCwidth, QCheight, QCascent;
323 extern Lisp_Object Qscroll_bar;
324 extern Lisp_Object Qcursor;
325
326 /* Non-nil means highlight trailing whitespace. */
327
328 Lisp_Object Vshow_trailing_whitespace;
329
330 /* Non-nil means escape non-break space and hyphens. */
331
332 Lisp_Object Vnobreak_char_display;
333
334 #ifdef HAVE_WINDOW_SYSTEM
335 extern Lisp_Object Voverflow_newline_into_fringe;
336
337 /* Test if overflow newline into fringe. Called with iterator IT
338 at or past right window margin, and with IT->current_x set. */
339
340 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
341 (!NILP (Voverflow_newline_into_fringe) \
342 && FRAME_WINDOW_P (it->f) \
343 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
344 && it->current_x == it->last_visible_x)
345
346 #endif /* HAVE_WINDOW_SYSTEM */
347
348 /* Non-nil means show the text cursor in void text areas
349 i.e. in blank areas after eol and eob. This used to be
350 the default in 21.3. */
351
352 Lisp_Object Vvoid_text_area_pointer;
353
354 /* Name of the face used to highlight trailing whitespace. */
355
356 Lisp_Object Qtrailing_whitespace;
357
358 /* Name and number of the face used to highlight escape glyphs. */
359
360 Lisp_Object Qescape_glyph;
361
362 /* Name and number of the face used to highlight non-breaking spaces. */
363
364 Lisp_Object Qnobreak_space;
365
366 /* The symbol `image' which is the car of the lists used to represent
367 images in Lisp. */
368
369 Lisp_Object Qimage;
370
371 /* The image map types. */
372 Lisp_Object QCmap, QCpointer;
373 Lisp_Object Qrect, Qcircle, Qpoly;
374
375 /* Non-zero means print newline to stdout before next mini-buffer
376 message. */
377
378 int noninteractive_need_newline;
379
380 /* Non-zero means print newline to message log before next message. */
381
382 static int message_log_need_newline;
383
384 /* Three markers that message_dolog uses.
385 It could allocate them itself, but that causes trouble
386 in handling memory-full errors. */
387 static Lisp_Object message_dolog_marker1;
388 static Lisp_Object message_dolog_marker2;
389 static Lisp_Object message_dolog_marker3;
390 \f
391 /* The buffer position of the first character appearing entirely or
392 partially on the line of the selected window which contains the
393 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
394 redisplay optimization in redisplay_internal. */
395
396 static struct text_pos this_line_start_pos;
397
398 /* Number of characters past the end of the line above, including the
399 terminating newline. */
400
401 static struct text_pos this_line_end_pos;
402
403 /* The vertical positions and the height of this line. */
404
405 static int this_line_vpos;
406 static int this_line_y;
407 static int this_line_pixel_height;
408
409 /* X position at which this display line starts. Usually zero;
410 negative if first character is partially visible. */
411
412 static int this_line_start_x;
413
414 /* Buffer that this_line_.* variables are referring to. */
415
416 static struct buffer *this_line_buffer;
417
418 /* Nonzero means truncate lines in all windows less wide than the
419 frame. */
420
421 int truncate_partial_width_windows;
422
423 /* A flag to control how to display unibyte 8-bit character. */
424
425 int unibyte_display_via_language_environment;
426
427 /* Nonzero means we have more than one non-mini-buffer-only frame.
428 Not guaranteed to be accurate except while parsing
429 frame-title-format. */
430
431 int multiple_frames;
432
433 Lisp_Object Vglobal_mode_string;
434
435
436 /* List of variables (symbols) which hold markers for overlay arrows.
437 The symbols on this list are examined during redisplay to determine
438 where to display overlay arrows. */
439
440 Lisp_Object Voverlay_arrow_variable_list;
441
442 /* Marker for where to display an arrow on top of the buffer text. */
443
444 Lisp_Object Voverlay_arrow_position;
445
446 /* String to display for the arrow. Only used on terminal frames. */
447
448 Lisp_Object Voverlay_arrow_string;
449
450 /* Values of those variables at last redisplay are stored as
451 properties on `overlay-arrow-position' symbol. However, if
452 Voverlay_arrow_position is a marker, last-arrow-position is its
453 numerical position. */
454
455 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
456
457 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
458 properties on a symbol in overlay-arrow-variable-list. */
459
460 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
461
462 /* Like mode-line-format, but for the title bar on a visible frame. */
463
464 Lisp_Object Vframe_title_format;
465
466 /* Like mode-line-format, but for the title bar on an iconified frame. */
467
468 Lisp_Object Vicon_title_format;
469
470 /* List of functions to call when a window's size changes. These
471 functions get one arg, a frame on which one or more windows' sizes
472 have changed. */
473
474 static Lisp_Object Vwindow_size_change_functions;
475
476 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
477
478 /* Nonzero if an overlay arrow has been displayed in this window. */
479
480 static int overlay_arrow_seen;
481
482 /* Nonzero means highlight the region even in nonselected windows. */
483
484 int highlight_nonselected_windows;
485
486 /* If cursor motion alone moves point off frame, try scrolling this
487 many lines up or down if that will bring it back. */
488
489 static EMACS_INT scroll_step;
490
491 /* Nonzero means scroll just far enough to bring point back on the
492 screen, when appropriate. */
493
494 static EMACS_INT scroll_conservatively;
495
496 /* Recenter the window whenever point gets within this many lines of
497 the top or bottom of the window. This value is translated into a
498 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
499 that there is really a fixed pixel height scroll margin. */
500
501 EMACS_INT scroll_margin;
502
503 /* Number of windows showing the buffer of the selected window (or
504 another buffer with the same base buffer). keyboard.c refers to
505 this. */
506
507 int buffer_shared;
508
509 /* Vector containing glyphs for an ellipsis `...'. */
510
511 static Lisp_Object default_invis_vector[3];
512
513 /* Zero means display the mode-line/header-line/menu-bar in the default face
514 (this slightly odd definition is for compatibility with previous versions
515 of emacs), non-zero means display them using their respective faces.
516
517 This variable is deprecated. */
518
519 int mode_line_inverse_video;
520
521 /* Prompt to display in front of the mini-buffer contents. */
522
523 Lisp_Object minibuf_prompt;
524
525 /* Width of current mini-buffer prompt. Only set after display_line
526 of the line that contains the prompt. */
527
528 int minibuf_prompt_width;
529
530 /* This is the window where the echo area message was displayed. It
531 is always a mini-buffer window, but it may not be the same window
532 currently active as a mini-buffer. */
533
534 Lisp_Object echo_area_window;
535
536 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
537 pushes the current message and the value of
538 message_enable_multibyte on the stack, the function restore_message
539 pops the stack and displays MESSAGE again. */
540
541 Lisp_Object Vmessage_stack;
542
543 /* Nonzero means multibyte characters were enabled when the echo area
544 message was specified. */
545
546 int message_enable_multibyte;
547
548 /* Nonzero if we should redraw the mode lines on the next redisplay. */
549
550 int update_mode_lines;
551
552 /* Nonzero if window sizes or contents have changed since last
553 redisplay that finished. */
554
555 int windows_or_buffers_changed;
556
557 /* Nonzero means a frame's cursor type has been changed. */
558
559 int cursor_type_changed;
560
561 /* Nonzero after display_mode_line if %l was used and it displayed a
562 line number. */
563
564 int line_number_displayed;
565
566 /* Maximum buffer size for which to display line numbers. */
567
568 Lisp_Object Vline_number_display_limit;
569
570 /* Line width to consider when repositioning for line number display. */
571
572 static EMACS_INT line_number_display_limit_width;
573
574 /* Number of lines to keep in the message log buffer. t means
575 infinite. nil means don't log at all. */
576
577 Lisp_Object Vmessage_log_max;
578
579 /* The name of the *Messages* buffer, a string. */
580
581 static Lisp_Object Vmessages_buffer_name;
582
583 /* Current, index 0, and last displayed echo area message. Either
584 buffers from echo_buffers, or nil to indicate no message. */
585
586 Lisp_Object echo_area_buffer[2];
587
588 /* The buffers referenced from echo_area_buffer. */
589
590 static Lisp_Object echo_buffer[2];
591
592 /* A vector saved used in with_area_buffer to reduce consing. */
593
594 static Lisp_Object Vwith_echo_area_save_vector;
595
596 /* Non-zero means display_echo_area should display the last echo area
597 message again. Set by redisplay_preserve_echo_area. */
598
599 static int display_last_displayed_message_p;
600
601 /* Nonzero if echo area is being used by print; zero if being used by
602 message. */
603
604 int message_buf_print;
605
606 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
607
608 Lisp_Object Qinhibit_menubar_update;
609 int inhibit_menubar_update;
610
611 /* When evaluating expressions from menu bar items (enable conditions,
612 for instance), this is the frame they are being processed for. */
613
614 Lisp_Object Vmenu_updating_frame;
615
616 /* Maximum height for resizing mini-windows. Either a float
617 specifying a fraction of the available height, or an integer
618 specifying a number of lines. */
619
620 Lisp_Object Vmax_mini_window_height;
621
622 /* Non-zero means messages should be displayed with truncated
623 lines instead of being continued. */
624
625 int message_truncate_lines;
626 Lisp_Object Qmessage_truncate_lines;
627
628 /* Set to 1 in clear_message to make redisplay_internal aware
629 of an emptied echo area. */
630
631 static int message_cleared_p;
632
633 /* How to blink the default frame cursor off. */
634 Lisp_Object Vblink_cursor_alist;
635
636 /* A scratch glyph row with contents used for generating truncation
637 glyphs. Also used in direct_output_for_insert. */
638
639 #define MAX_SCRATCH_GLYPHS 100
640 struct glyph_row scratch_glyph_row;
641 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
642
643 /* Ascent and height of the last line processed by move_it_to. */
644
645 static int last_max_ascent, last_height;
646
647 /* Non-zero if there's a help-echo in the echo area. */
648
649 int help_echo_showing_p;
650
651 /* If >= 0, computed, exact values of mode-line and header-line height
652 to use in the macros CURRENT_MODE_LINE_HEIGHT and
653 CURRENT_HEADER_LINE_HEIGHT. */
654
655 int current_mode_line_height, current_header_line_height;
656
657 /* The maximum distance to look ahead for text properties. Values
658 that are too small let us call compute_char_face and similar
659 functions too often which is expensive. Values that are too large
660 let us call compute_char_face and alike too often because we
661 might not be interested in text properties that far away. */
662
663 #define TEXT_PROP_DISTANCE_LIMIT 100
664
665 #if GLYPH_DEBUG
666
667 /* Variables to turn off display optimizations from Lisp. */
668
669 int inhibit_try_window_id, inhibit_try_window_reusing;
670 int inhibit_try_cursor_movement;
671
672 /* Non-zero means print traces of redisplay if compiled with
673 GLYPH_DEBUG != 0. */
674
675 int trace_redisplay_p;
676
677 #endif /* GLYPH_DEBUG */
678
679 #ifdef DEBUG_TRACE_MOVE
680 /* Non-zero means trace with TRACE_MOVE to stderr. */
681 int trace_move;
682
683 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
684 #else
685 #define TRACE_MOVE(x) (void) 0
686 #endif
687
688 /* Non-zero means automatically scroll windows horizontally to make
689 point visible. */
690
691 int automatic_hscrolling_p;
692
693 /* How close to the margin can point get before the window is scrolled
694 horizontally. */
695 EMACS_INT hscroll_margin;
696
697 /* How much to scroll horizontally when point is inside the above margin. */
698 Lisp_Object Vhscroll_step;
699
700 /* The variable `resize-mini-windows'. If nil, don't resize
701 mini-windows. If t, always resize them to fit the text they
702 display. If `grow-only', let mini-windows grow only until they
703 become empty. */
704
705 Lisp_Object Vresize_mini_windows;
706
707 /* Buffer being redisplayed -- for redisplay_window_error. */
708
709 struct buffer *displayed_buffer;
710
711 /* Space between overline and text. */
712
713 EMACS_INT overline_margin;
714
715 /* Value returned from text property handlers (see below). */
716
717 enum prop_handled
718 {
719 HANDLED_NORMALLY,
720 HANDLED_RECOMPUTE_PROPS,
721 HANDLED_OVERLAY_STRING_CONSUMED,
722 HANDLED_RETURN
723 };
724
725 /* A description of text properties that redisplay is interested
726 in. */
727
728 struct props
729 {
730 /* The name of the property. */
731 Lisp_Object *name;
732
733 /* A unique index for the property. */
734 enum prop_idx idx;
735
736 /* A handler function called to set up iterator IT from the property
737 at IT's current position. Value is used to steer handle_stop. */
738 enum prop_handled (*handler) P_ ((struct it *it));
739 };
740
741 static enum prop_handled handle_face_prop P_ ((struct it *));
742 static enum prop_handled handle_invisible_prop P_ ((struct it *));
743 static enum prop_handled handle_display_prop P_ ((struct it *));
744 static enum prop_handled handle_composition_prop P_ ((struct it *));
745 static enum prop_handled handle_overlay_change P_ ((struct it *));
746 static enum prop_handled handle_fontified_prop P_ ((struct it *));
747
748 /* Properties handled by iterators. */
749
750 static struct props it_props[] =
751 {
752 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
753 /* Handle `face' before `display' because some sub-properties of
754 `display' need to know the face. */
755 {&Qface, FACE_PROP_IDX, handle_face_prop},
756 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
757 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
758 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
759 {NULL, 0, NULL}
760 };
761
762 /* Value is the position described by X. If X is a marker, value is
763 the marker_position of X. Otherwise, value is X. */
764
765 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
766
767 /* Enumeration returned by some move_it_.* functions internally. */
768
769 enum move_it_result
770 {
771 /* Not used. Undefined value. */
772 MOVE_UNDEFINED,
773
774 /* Move ended at the requested buffer position or ZV. */
775 MOVE_POS_MATCH_OR_ZV,
776
777 /* Move ended at the requested X pixel position. */
778 MOVE_X_REACHED,
779
780 /* Move within a line ended at the end of a line that must be
781 continued. */
782 MOVE_LINE_CONTINUED,
783
784 /* Move within a line ended at the end of a line that would
785 be displayed truncated. */
786 MOVE_LINE_TRUNCATED,
787
788 /* Move within a line ended at a line end. */
789 MOVE_NEWLINE_OR_CR
790 };
791
792 /* This counter is used to clear the face cache every once in a while
793 in redisplay_internal. It is incremented for each redisplay.
794 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
795 cleared. */
796
797 #define CLEAR_FACE_CACHE_COUNT 500
798 static int clear_face_cache_count;
799
800 /* Similarly for the image cache. */
801
802 #ifdef HAVE_WINDOW_SYSTEM
803 #define CLEAR_IMAGE_CACHE_COUNT 101
804 static int clear_image_cache_count;
805 #endif
806
807 /* Non-zero while redisplay_internal is in progress. */
808
809 int redisplaying_p;
810
811 /* Non-zero means don't free realized faces. Bound while freeing
812 realized faces is dangerous because glyph matrices might still
813 reference them. */
814
815 int inhibit_free_realized_faces;
816 Lisp_Object Qinhibit_free_realized_faces;
817
818 /* If a string, XTread_socket generates an event to display that string.
819 (The display is done in read_char.) */
820
821 Lisp_Object help_echo_string;
822 Lisp_Object help_echo_window;
823 Lisp_Object help_echo_object;
824 int help_echo_pos;
825
826 /* Temporary variable for XTread_socket. */
827
828 Lisp_Object previous_help_echo_string;
829
830 /* Null glyph slice */
831
832 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
833
834 \f
835 /* Function prototypes. */
836
837 static void setup_for_ellipsis P_ ((struct it *, int));
838 static void mark_window_display_accurate_1 P_ ((struct window *, int));
839 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
840 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
841 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
842 static int redisplay_mode_lines P_ ((Lisp_Object, int));
843 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
844
845 #if 0
846 static int invisible_text_between_p P_ ((struct it *, int, int));
847 #endif
848
849 static void pint2str P_ ((char *, int, int));
850 static void pint2hrstr P_ ((char *, int, int));
851 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
852 struct text_pos));
853 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
854 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
855 static void store_mode_line_noprop_char P_ ((char));
856 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
857 static void x_consider_frame_title P_ ((Lisp_Object));
858 static void handle_stop P_ ((struct it *));
859 static int tool_bar_lines_needed P_ ((struct frame *, int *));
860 static int single_display_spec_intangible_p P_ ((Lisp_Object));
861 static void ensure_echo_area_buffers P_ ((void));
862 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
863 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
864 static int with_echo_area_buffer P_ ((struct window *, int,
865 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
866 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static void clear_garbaged_frames P_ ((void));
868 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
870 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int display_echo_area P_ ((struct window *));
872 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
873 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
874 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
875 static int string_char_and_length P_ ((const unsigned char *, int, int *));
876 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
877 struct text_pos));
878 static int compute_window_start_on_continuation_line P_ ((struct window *));
879 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
880 static void insert_left_trunc_glyphs P_ ((struct it *));
881 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
882 Lisp_Object));
883 static void extend_face_to_end_of_line P_ ((struct it *));
884 static int append_space_for_newline P_ ((struct it *, int));
885 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
886 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
887 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
888 static int trailing_whitespace_p P_ ((int));
889 static int message_log_check_duplicate P_ ((int, int, int, int));
890 static void push_it P_ ((struct it *));
891 static void pop_it P_ ((struct it *));
892 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
893 static void select_frame_for_redisplay P_ ((Lisp_Object));
894 static void redisplay_internal P_ ((int));
895 static int echo_area_display P_ ((int));
896 static void redisplay_windows P_ ((Lisp_Object));
897 static void redisplay_window P_ ((Lisp_Object, int));
898 static Lisp_Object redisplay_window_error ();
899 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
900 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
901 static int update_menu_bar P_ ((struct frame *, int, int));
902 static int try_window_reusing_current_matrix P_ ((struct window *));
903 static int try_window_id P_ ((struct window *));
904 static int display_line P_ ((struct it *));
905 static int display_mode_lines P_ ((struct window *));
906 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
907 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
908 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
909 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
910 static void display_menu_bar P_ ((struct window *));
911 static int display_count_lines P_ ((int, int, int, int, int *));
912 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
913 int, int, struct it *, int, int, int, int));
914 static void compute_line_metrics P_ ((struct it *));
915 static void run_redisplay_end_trigger_hook P_ ((struct it *));
916 static int get_overlay_strings P_ ((struct it *, int));
917 static int get_overlay_strings_1 P_ ((struct it *, int, int));
918 static void next_overlay_string P_ ((struct it *));
919 static void reseat P_ ((struct it *, struct text_pos, int));
920 static void reseat_1 P_ ((struct it *, struct text_pos, int));
921 static void back_to_previous_visible_line_start P_ ((struct it *));
922 void reseat_at_previous_visible_line_start P_ ((struct it *));
923 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
924 static int next_element_from_ellipsis P_ ((struct it *));
925 static int next_element_from_display_vector P_ ((struct it *));
926 static int next_element_from_string P_ ((struct it *));
927 static int next_element_from_c_string P_ ((struct it *));
928 static int next_element_from_buffer P_ ((struct it *));
929 static int next_element_from_composition P_ ((struct it *));
930 static int next_element_from_image P_ ((struct it *));
931 static int next_element_from_stretch P_ ((struct it *));
932 static void load_overlay_strings P_ ((struct it *, int));
933 static int init_from_display_pos P_ ((struct it *, struct window *,
934 struct display_pos *));
935 static void reseat_to_string P_ ((struct it *, unsigned char *,
936 Lisp_Object, int, int, int, int));
937 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
938 int, int, int));
939 void move_it_vertically_backward P_ ((struct it *, int));
940 static void init_to_row_start P_ ((struct it *, struct window *,
941 struct glyph_row *));
942 static int init_to_row_end P_ ((struct it *, struct window *,
943 struct glyph_row *));
944 static void back_to_previous_line_start P_ ((struct it *));
945 static int forward_to_next_line_start P_ ((struct it *, int *));
946 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
947 Lisp_Object, int));
948 static struct text_pos string_pos P_ ((int, Lisp_Object));
949 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
950 static int number_of_chars P_ ((unsigned char *, int));
951 static void compute_stop_pos P_ ((struct it *));
952 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
953 Lisp_Object));
954 static int face_before_or_after_it_pos P_ ((struct it *, int));
955 static int next_overlay_change P_ ((int));
956 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
957 Lisp_Object, struct text_pos *,
958 int));
959 static int underlying_face_id P_ ((struct it *));
960 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
961 struct window *));
962
963 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
964 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
965
966 #ifdef HAVE_WINDOW_SYSTEM
967
968 static void update_tool_bar P_ ((struct frame *, int));
969 static void build_desired_tool_bar_string P_ ((struct frame *f));
970 static int redisplay_tool_bar P_ ((struct frame *));
971 static void display_tool_bar_line P_ ((struct it *, int));
972 static void notice_overwritten_cursor P_ ((struct window *,
973 enum glyph_row_area,
974 int, int, int, int));
975
976
977
978 #endif /* HAVE_WINDOW_SYSTEM */
979
980 \f
981 /***********************************************************************
982 Window display dimensions
983 ***********************************************************************/
984
985 /* Return the bottom boundary y-position for text lines in window W.
986 This is the first y position at which a line cannot start.
987 It is relative to the top of the window.
988
989 This is the height of W minus the height of a mode line, if any. */
990
991 INLINE int
992 window_text_bottom_y (w)
993 struct window *w;
994 {
995 int height = WINDOW_TOTAL_HEIGHT (w);
996
997 if (WINDOW_WANTS_MODELINE_P (w))
998 height -= CURRENT_MODE_LINE_HEIGHT (w);
999 return height;
1000 }
1001
1002 /* Return the pixel width of display area AREA of window W. AREA < 0
1003 means return the total width of W, not including fringes to
1004 the left and right of the window. */
1005
1006 INLINE int
1007 window_box_width (w, area)
1008 struct window *w;
1009 int area;
1010 {
1011 int cols = XFASTINT (w->total_cols);
1012 int pixels = 0;
1013
1014 if (!w->pseudo_window_p)
1015 {
1016 cols -= WINDOW_SCROLL_BAR_COLS (w);
1017
1018 if (area == TEXT_AREA)
1019 {
1020 if (INTEGERP (w->left_margin_cols))
1021 cols -= XFASTINT (w->left_margin_cols);
1022 if (INTEGERP (w->right_margin_cols))
1023 cols -= XFASTINT (w->right_margin_cols);
1024 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1025 }
1026 else if (area == LEFT_MARGIN_AREA)
1027 {
1028 cols = (INTEGERP (w->left_margin_cols)
1029 ? XFASTINT (w->left_margin_cols) : 0);
1030 pixels = 0;
1031 }
1032 else if (area == RIGHT_MARGIN_AREA)
1033 {
1034 cols = (INTEGERP (w->right_margin_cols)
1035 ? XFASTINT (w->right_margin_cols) : 0);
1036 pixels = 0;
1037 }
1038 }
1039
1040 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1041 }
1042
1043
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1046
1047 INLINE int
1048 window_box_height (w)
1049 struct window *w;
1050 {
1051 struct frame *f = XFRAME (w->frame);
1052 int height = WINDOW_TOTAL_HEIGHT (w);
1053
1054 xassert (height >= 0);
1055
1056 /* Note: the code below that determines the mode-line/header-line
1057 height is essentially the same as that contained in the macro
1058 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1059 the appropriate glyph row has its `mode_line_p' flag set,
1060 and if it doesn't, uses estimate_mode_line_height instead. */
1061
1062 if (WINDOW_WANTS_MODELINE_P (w))
1063 {
1064 struct glyph_row *ml_row
1065 = (w->current_matrix && w->current_matrix->rows
1066 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1067 : 0);
1068 if (ml_row && ml_row->mode_line_p)
1069 height -= ml_row->height;
1070 else
1071 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1072 }
1073
1074 if (WINDOW_WANTS_HEADER_LINE_P (w))
1075 {
1076 struct glyph_row *hl_row
1077 = (w->current_matrix && w->current_matrix->rows
1078 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1079 : 0);
1080 if (hl_row && hl_row->mode_line_p)
1081 height -= hl_row->height;
1082 else
1083 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1084 }
1085
1086 /* With a very small font and a mode-line that's taller than
1087 default, we might end up with a negative height. */
1088 return max (0, height);
1089 }
1090
1091 /* Return the window-relative coordinate of the left edge of display
1092 area AREA of window W. AREA < 0 means return the left edge of the
1093 whole window, to the right of the left fringe of W. */
1094
1095 INLINE int
1096 window_box_left_offset (w, area)
1097 struct window *w;
1098 int area;
1099 {
1100 int x;
1101
1102 if (w->pseudo_window_p)
1103 return 0;
1104
1105 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1106
1107 if (area == TEXT_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA));
1110 else if (area == RIGHT_MARGIN_AREA)
1111 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1112 + window_box_width (w, LEFT_MARGIN_AREA)
1113 + window_box_width (w, TEXT_AREA)
1114 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1115 ? 0
1116 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1117 else if (area == LEFT_MARGIN_AREA
1118 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1119 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1120
1121 return x;
1122 }
1123
1124
1125 /* Return the window-relative coordinate of the right edge of display
1126 area AREA of window W. AREA < 0 means return the left edge of the
1127 whole window, to the left of the right fringe of W. */
1128
1129 INLINE int
1130 window_box_right_offset (w, area)
1131 struct window *w;
1132 int area;
1133 {
1134 return window_box_left_offset (w, area) + window_box_width (w, area);
1135 }
1136
1137 /* Return the frame-relative coordinate of the left edge of display
1138 area AREA of window W. AREA < 0 means return the left edge of the
1139 whole window, to the right of the left fringe of W. */
1140
1141 INLINE int
1142 window_box_left (w, area)
1143 struct window *w;
1144 int area;
1145 {
1146 struct frame *f = XFRAME (w->frame);
1147 int x;
1148
1149 if (w->pseudo_window_p)
1150 return FRAME_INTERNAL_BORDER_WIDTH (f);
1151
1152 x = (WINDOW_LEFT_EDGE_X (w)
1153 + window_box_left_offset (w, area));
1154
1155 return x;
1156 }
1157
1158
1159 /* Return the frame-relative coordinate of the right edge of display
1160 area AREA of window W. AREA < 0 means return the left edge of the
1161 whole window, to the left of the right fringe of W. */
1162
1163 INLINE int
1164 window_box_right (w, area)
1165 struct window *w;
1166 int area;
1167 {
1168 return window_box_left (w, area) + window_box_width (w, area);
1169 }
1170
1171 /* Get the bounding box of the display area AREA of window W, without
1172 mode lines, in frame-relative coordinates. AREA < 0 means the
1173 whole window, not including the left and right fringes of
1174 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1175 coordinates of the upper-left corner of the box. Return in
1176 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1177
1178 INLINE void
1179 window_box (w, area, box_x, box_y, box_width, box_height)
1180 struct window *w;
1181 int area;
1182 int *box_x, *box_y, *box_width, *box_height;
1183 {
1184 if (box_width)
1185 *box_width = window_box_width (w, area);
1186 if (box_height)
1187 *box_height = window_box_height (w);
1188 if (box_x)
1189 *box_x = window_box_left (w, area);
1190 if (box_y)
1191 {
1192 *box_y = WINDOW_TOP_EDGE_Y (w);
1193 if (WINDOW_WANTS_HEADER_LINE_P (w))
1194 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1195 }
1196 }
1197
1198
1199 /* Get the bounding box of the display area AREA of window W, without
1200 mode lines. AREA < 0 means the whole window, not including the
1201 left and right fringe of the window. Return in *TOP_LEFT_X
1202 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1203 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1204 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1205 box. */
1206
1207 INLINE void
1208 window_box_edges (w, area, top_left_x, top_left_y,
1209 bottom_right_x, bottom_right_y)
1210 struct window *w;
1211 int area;
1212 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1213 {
1214 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1215 bottom_right_y);
1216 *bottom_right_x += *top_left_x;
1217 *bottom_right_y += *top_left_y;
1218 }
1219
1220
1221 \f
1222 /***********************************************************************
1223 Utilities
1224 ***********************************************************************/
1225
1226 /* Return the bottom y-position of the line the iterator IT is in.
1227 This can modify IT's settings. */
1228
1229 int
1230 line_bottom_y (it)
1231 struct it *it;
1232 {
1233 int line_height = it->max_ascent + it->max_descent;
1234 int line_top_y = it->current_y;
1235
1236 if (line_height == 0)
1237 {
1238 if (last_height)
1239 line_height = last_height;
1240 else if (IT_CHARPOS (*it) < ZV)
1241 {
1242 move_it_by_lines (it, 1, 1);
1243 line_height = (it->max_ascent || it->max_descent
1244 ? it->max_ascent + it->max_descent
1245 : last_height);
1246 }
1247 else
1248 {
1249 struct glyph_row *row = it->glyph_row;
1250
1251 /* Use the default character height. */
1252 it->glyph_row = NULL;
1253 it->what = IT_CHARACTER;
1254 it->c = ' ';
1255 it->len = 1;
1256 PRODUCE_GLYPHS (it);
1257 line_height = it->ascent + it->descent;
1258 it->glyph_row = row;
1259 }
1260 }
1261
1262 return line_top_y + line_height;
1263 }
1264
1265
1266 /* Return 1 if position CHARPOS is visible in window W.
1267 CHARPOS < 0 means return info about WINDOW_END position.
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 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1271
1272 int
1273 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1274 struct window *w;
1275 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1276 {
1277 struct it it;
1278 struct text_pos top;
1279 int visible_p = 0;
1280 struct buffer *old_buffer = NULL;
1281
1282 if (noninteractive)
1283 return visible_p;
1284
1285 if (XBUFFER (w->buffer) != current_buffer)
1286 {
1287 old_buffer = current_buffer;
1288 set_buffer_internal_1 (XBUFFER (w->buffer));
1289 }
1290
1291 SET_TEXT_POS_FROM_MARKER (top, w->start);
1292
1293 /* Compute exact mode line heights. */
1294 if (WINDOW_WANTS_MODELINE_P (w))
1295 current_mode_line_height
1296 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1297 current_buffer->mode_line_format);
1298
1299 if (WINDOW_WANTS_HEADER_LINE_P (w))
1300 current_header_line_height
1301 = display_mode_line (w, HEADER_LINE_FACE_ID,
1302 current_buffer->header_line_format);
1303
1304 start_display (&it, w, top);
1305 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1306 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1307
1308 /* Note that we may overshoot because of invisible text. */
1309 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1310 {
1311 int top_x = it.current_x;
1312 int top_y = it.current_y;
1313 int bottom_y = (last_height = 0, line_bottom_y (&it));
1314 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1315
1316 if (top_y < window_top_y)
1317 visible_p = bottom_y > window_top_y;
1318 else if (top_y < it.last_visible_y)
1319 visible_p = 1;
1320 if (visible_p)
1321 {
1322 *x = top_x;
1323 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1324 *rtop = max (0, window_top_y - top_y);
1325 *rbot = max (0, bottom_y - it.last_visible_y);
1326 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1327 - max (top_y, window_top_y)));
1328 *vpos = it.vpos;
1329 }
1330 }
1331 else
1332 {
1333 struct it it2;
1334
1335 it2 = it;
1336 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1337 move_it_by_lines (&it, 1, 0);
1338 if (charpos < IT_CHARPOS (it)
1339 || (it.what == IT_EOB && 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 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1349 it.last_visible_y)
1350 - max (it2.current_y,
1351 WINDOW_HEADER_LINE_HEIGHT (w))));
1352 *vpos = it2.vpos;
1353 }
1354 }
1355
1356 if (old_buffer)
1357 set_buffer_internal_1 (old_buffer);
1358
1359 current_header_line_height = current_mode_line_height = -1;
1360
1361 if (visible_p && XFASTINT (w->hscroll) > 0)
1362 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1363
1364 #if 0
1365 /* Debugging code. */
1366 if (visible_p)
1367 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1368 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1369 else
1370 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1371 #endif
1372
1373 return visible_p;
1374 }
1375
1376
1377 /* Return the next character from STR which is MAXLEN bytes long.
1378 Return in *LEN the length of the character. This is like
1379 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1380 we find one, we return a `?', but with the length of the invalid
1381 character. */
1382
1383 static INLINE int
1384 string_char_and_length (str, maxlen, len)
1385 const unsigned char *str;
1386 int maxlen, *len;
1387 {
1388 int c;
1389
1390 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1391 if (!CHAR_VALID_P (c, 1))
1392 /* We may not change the length here because other places in Emacs
1393 don't use this function, i.e. they silently accept invalid
1394 characters. */
1395 c = '?';
1396
1397 return c;
1398 }
1399
1400
1401
1402 /* Given a position POS containing a valid character and byte position
1403 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1404
1405 static struct text_pos
1406 string_pos_nchars_ahead (pos, string, nchars)
1407 struct text_pos pos;
1408 Lisp_Object string;
1409 int nchars;
1410 {
1411 xassert (STRINGP (string) && nchars >= 0);
1412
1413 if (STRING_MULTIBYTE (string))
1414 {
1415 int rest = SBYTES (string) - BYTEPOS (pos);
1416 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1417 int len;
1418
1419 while (nchars--)
1420 {
1421 string_char_and_length (p, rest, &len);
1422 p += len, rest -= len;
1423 xassert (rest >= 0);
1424 CHARPOS (pos) += 1;
1425 BYTEPOS (pos) += len;
1426 }
1427 }
1428 else
1429 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1430
1431 return pos;
1432 }
1433
1434
1435 /* Value is the text position, i.e. character and byte position,
1436 for character position CHARPOS in STRING. */
1437
1438 static INLINE struct text_pos
1439 string_pos (charpos, string)
1440 int charpos;
1441 Lisp_Object string;
1442 {
1443 struct text_pos pos;
1444 xassert (STRINGP (string));
1445 xassert (charpos >= 0);
1446 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1447 return pos;
1448 }
1449
1450
1451 /* Value is a text position, i.e. character and byte position, for
1452 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1453 means recognize multibyte characters. */
1454
1455 static struct text_pos
1456 c_string_pos (charpos, s, multibyte_p)
1457 int charpos;
1458 unsigned char *s;
1459 int multibyte_p;
1460 {
1461 struct text_pos pos;
1462
1463 xassert (s != NULL);
1464 xassert (charpos >= 0);
1465
1466 if (multibyte_p)
1467 {
1468 int rest = strlen (s), len;
1469
1470 SET_TEXT_POS (pos, 0, 0);
1471 while (charpos--)
1472 {
1473 string_char_and_length (s, rest, &len);
1474 s += len, rest -= len;
1475 xassert (rest >= 0);
1476 CHARPOS (pos) += 1;
1477 BYTEPOS (pos) += len;
1478 }
1479 }
1480 else
1481 SET_TEXT_POS (pos, charpos, charpos);
1482
1483 return pos;
1484 }
1485
1486
1487 /* Value is the number of characters in C string S. MULTIBYTE_P
1488 non-zero means recognize multibyte characters. */
1489
1490 static int
1491 number_of_chars (s, multibyte_p)
1492 unsigned char *s;
1493 int multibyte_p;
1494 {
1495 int nchars;
1496
1497 if (multibyte_p)
1498 {
1499 int rest = strlen (s), len;
1500 unsigned char *p = (unsigned char *) s;
1501
1502 for (nchars = 0; rest > 0; ++nchars)
1503 {
1504 string_char_and_length (p, rest, &len);
1505 rest -= len, p += len;
1506 }
1507 }
1508 else
1509 nchars = strlen (s);
1510
1511 return nchars;
1512 }
1513
1514
1515 /* Compute byte position NEWPOS->bytepos corresponding to
1516 NEWPOS->charpos. POS is a known position in string STRING.
1517 NEWPOS->charpos must be >= POS.charpos. */
1518
1519 static void
1520 compute_string_pos (newpos, pos, string)
1521 struct text_pos *newpos, pos;
1522 Lisp_Object string;
1523 {
1524 xassert (STRINGP (string));
1525 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1526
1527 if (STRING_MULTIBYTE (string))
1528 *newpos = string_pos_nchars_ahead (pos, string,
1529 CHARPOS (*newpos) - CHARPOS (pos));
1530 else
1531 BYTEPOS (*newpos) = CHARPOS (*newpos);
1532 }
1533
1534 /* EXPORT:
1535 Return an estimation of the pixel height of mode or top lines on
1536 frame F. FACE_ID specifies what line's height to estimate. */
1537
1538 int
1539 estimate_mode_line_height (f, face_id)
1540 struct frame *f;
1541 enum face_id face_id;
1542 {
1543 #ifdef HAVE_WINDOW_SYSTEM
1544 if (FRAME_WINDOW_P (f))
1545 {
1546 int height = FONT_HEIGHT (FRAME_FONT (f));
1547
1548 /* This function is called so early when Emacs starts that the face
1549 cache and mode line face are not yet initialized. */
1550 if (FRAME_FACE_CACHE (f))
1551 {
1552 struct face *face = FACE_FROM_ID (f, face_id);
1553 if (face)
1554 {
1555 if (face->font)
1556 height = FONT_HEIGHT (face->font);
1557 if (face->box_line_width > 0)
1558 height += 2 * face->box_line_width;
1559 }
1560 }
1561
1562 return height;
1563 }
1564 #endif
1565
1566 return 1;
1567 }
1568
1569 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1570 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1571 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1572 not force the value into range. */
1573
1574 void
1575 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1576 FRAME_PTR f;
1577 register int pix_x, pix_y;
1578 int *x, *y;
1579 NativeRectangle *bounds;
1580 int noclip;
1581 {
1582
1583 #ifdef HAVE_WINDOW_SYSTEM
1584 if (FRAME_WINDOW_P (f))
1585 {
1586 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1587 even for negative values. */
1588 if (pix_x < 0)
1589 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1590 if (pix_y < 0)
1591 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1592
1593 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1594 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1595
1596 if (bounds)
1597 STORE_NATIVE_RECT (*bounds,
1598 FRAME_COL_TO_PIXEL_X (f, pix_x),
1599 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1600 FRAME_COLUMN_WIDTH (f) - 1,
1601 FRAME_LINE_HEIGHT (f) - 1);
1602
1603 if (!noclip)
1604 {
1605 if (pix_x < 0)
1606 pix_x = 0;
1607 else if (pix_x > FRAME_TOTAL_COLS (f))
1608 pix_x = FRAME_TOTAL_COLS (f);
1609
1610 if (pix_y < 0)
1611 pix_y = 0;
1612 else if (pix_y > FRAME_LINES (f))
1613 pix_y = FRAME_LINES (f);
1614 }
1615 }
1616 #endif
1617
1618 *x = pix_x;
1619 *y = pix_y;
1620 }
1621
1622
1623 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1624 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1625 can't tell the positions because W's display is not up to date,
1626 return 0. */
1627
1628 int
1629 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1630 struct window *w;
1631 int hpos, vpos;
1632 int *frame_x, *frame_y;
1633 {
1634 #ifdef HAVE_WINDOW_SYSTEM
1635 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1636 {
1637 int success_p;
1638
1639 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1640 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1641
1642 if (display_completed)
1643 {
1644 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1645 struct glyph *glyph = row->glyphs[TEXT_AREA];
1646 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1647
1648 hpos = row->x;
1649 vpos = row->y;
1650 while (glyph < end)
1651 {
1652 hpos += glyph->pixel_width;
1653 ++glyph;
1654 }
1655
1656 /* If first glyph is partially visible, its first visible position is still 0. */
1657 if (hpos < 0)
1658 hpos = 0;
1659
1660 success_p = 1;
1661 }
1662 else
1663 {
1664 hpos = vpos = 0;
1665 success_p = 0;
1666 }
1667
1668 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1669 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1670 return success_p;
1671 }
1672 #endif
1673
1674 *frame_x = hpos;
1675 *frame_y = vpos;
1676 return 1;
1677 }
1678
1679
1680 #ifdef HAVE_WINDOW_SYSTEM
1681
1682 /* Find the glyph under window-relative coordinates X/Y in window W.
1683 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1684 strings. Return in *HPOS and *VPOS the row and column number of
1685 the glyph found. Return in *AREA the glyph area containing X.
1686 Value is a pointer to the glyph found or null if X/Y is not on
1687 text, or we can't tell because W's current matrix is not up to
1688 date. */
1689
1690 static struct glyph *
1691 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1692 struct window *w;
1693 int x, y;
1694 int *hpos, *vpos, *dx, *dy, *area;
1695 {
1696 struct glyph *glyph, *end;
1697 struct glyph_row *row = NULL;
1698 int x0, i;
1699
1700 /* Find row containing Y. Give up if some row is not enabled. */
1701 for (i = 0; i < w->current_matrix->nrows; ++i)
1702 {
1703 row = MATRIX_ROW (w->current_matrix, i);
1704 if (!row->enabled_p)
1705 return NULL;
1706 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1707 break;
1708 }
1709
1710 *vpos = i;
1711 *hpos = 0;
1712
1713 /* Give up if Y is not in the window. */
1714 if (i == w->current_matrix->nrows)
1715 return NULL;
1716
1717 /* Get the glyph area containing X. */
1718 if (w->pseudo_window_p)
1719 {
1720 *area = TEXT_AREA;
1721 x0 = 0;
1722 }
1723 else
1724 {
1725 if (x < window_box_left_offset (w, TEXT_AREA))
1726 {
1727 *area = LEFT_MARGIN_AREA;
1728 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1729 }
1730 else if (x < window_box_right_offset (w, TEXT_AREA))
1731 {
1732 *area = TEXT_AREA;
1733 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1734 }
1735 else
1736 {
1737 *area = RIGHT_MARGIN_AREA;
1738 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1739 }
1740 }
1741
1742 /* Find glyph containing X. */
1743 glyph = row->glyphs[*area];
1744 end = glyph + row->used[*area];
1745 x -= x0;
1746 while (glyph < end && x >= glyph->pixel_width)
1747 {
1748 x -= glyph->pixel_width;
1749 ++glyph;
1750 }
1751
1752 if (glyph == end)
1753 return NULL;
1754
1755 if (dx)
1756 {
1757 *dx = x;
1758 *dy = y - (row->y + row->ascent - glyph->ascent);
1759 }
1760
1761 *hpos = glyph - row->glyphs[*area];
1762 return glyph;
1763 }
1764
1765
1766 /* EXPORT:
1767 Convert frame-relative x/y to coordinates relative to window W.
1768 Takes pseudo-windows into account. */
1769
1770 void
1771 frame_to_window_pixel_xy (w, x, y)
1772 struct window *w;
1773 int *x, *y;
1774 {
1775 if (w->pseudo_window_p)
1776 {
1777 /* A pseudo-window is always full-width, and starts at the
1778 left edge of the frame, plus a frame border. */
1779 struct frame *f = XFRAME (w->frame);
1780 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1781 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1782 }
1783 else
1784 {
1785 *x -= WINDOW_LEFT_EDGE_X (w);
1786 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1787 }
1788 }
1789
1790 /* EXPORT:
1791 Return in RECTS[] at most N clipping rectangles for glyph string S.
1792 Return the number of stored rectangles. */
1793
1794 int
1795 get_glyph_string_clip_rects (s, rects, n)
1796 struct glyph_string *s;
1797 NativeRectangle *rects;
1798 int n;
1799 {
1800 XRectangle r;
1801
1802 if (n <= 0)
1803 return 0;
1804
1805 if (s->row->full_width_p)
1806 {
1807 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1808 r.x = WINDOW_LEFT_EDGE_X (s->w);
1809 r.width = WINDOW_TOTAL_WIDTH (s->w);
1810
1811 /* Unless displaying a mode or menu bar line, which are always
1812 fully visible, clip to the visible part of the row. */
1813 if (s->w->pseudo_window_p)
1814 r.height = s->row->visible_height;
1815 else
1816 r.height = s->height;
1817 }
1818 else
1819 {
1820 /* This is a text line that may be partially visible. */
1821 r.x = window_box_left (s->w, s->area);
1822 r.width = window_box_width (s->w, s->area);
1823 r.height = s->row->visible_height;
1824 }
1825
1826 if (s->clip_head)
1827 if (r.x < s->clip_head->x)
1828 {
1829 if (r.width >= s->clip_head->x - r.x)
1830 r.width -= s->clip_head->x - r.x;
1831 else
1832 r.width = 0;
1833 r.x = s->clip_head->x;
1834 }
1835 if (s->clip_tail)
1836 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1837 {
1838 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1839 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1840 else
1841 r.width = 0;
1842 }
1843
1844 /* If S draws overlapping rows, it's sufficient to use the top and
1845 bottom of the window for clipping because this glyph string
1846 intentionally draws over other lines. */
1847 if (s->for_overlaps)
1848 {
1849 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1850 r.height = window_text_bottom_y (s->w) - r.y;
1851
1852 /* Alas, the above simple strategy does not work for the
1853 environments with anti-aliased text: if the same text is
1854 drawn onto the same place multiple times, it gets thicker.
1855 If the overlap we are processing is for the erased cursor, we
1856 take the intersection with the rectagle of the cursor. */
1857 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1858 {
1859 XRectangle rc, r_save = r;
1860
1861 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1862 rc.y = s->w->phys_cursor.y;
1863 rc.width = s->w->phys_cursor_width;
1864 rc.height = s->w->phys_cursor_height;
1865
1866 x_intersect_rectangles (&r_save, &rc, &r);
1867 }
1868 }
1869 else
1870 {
1871 /* Don't use S->y for clipping because it doesn't take partially
1872 visible lines into account. For example, it can be negative for
1873 partially visible lines at the top of a window. */
1874 if (!s->row->full_width_p
1875 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1876 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1877 else
1878 r.y = max (0, s->row->y);
1879
1880 /* If drawing a tool-bar window, draw it over the internal border
1881 at the top of the window. */
1882 if (WINDOWP (s->f->tool_bar_window)
1883 && s->w == XWINDOW (s->f->tool_bar_window))
1884 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1885 }
1886
1887 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1888
1889 /* If drawing the cursor, don't let glyph draw outside its
1890 advertised boundaries. Cleartype does this under some circumstances. */
1891 if (s->hl == DRAW_CURSOR)
1892 {
1893 struct glyph *glyph = s->first_glyph;
1894 int height, max_y;
1895
1896 if (s->x > r.x)
1897 {
1898 r.width -= s->x - r.x;
1899 r.x = s->x;
1900 }
1901 r.width = min (r.width, glyph->pixel_width);
1902
1903 /* If r.y is below window bottom, ensure that we still see a cursor. */
1904 height = min (glyph->ascent + glyph->descent,
1905 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1906 max_y = window_text_bottom_y (s->w) - height;
1907 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1908 if (s->ybase - glyph->ascent > max_y)
1909 {
1910 r.y = max_y;
1911 r.height = height;
1912 }
1913 else
1914 {
1915 /* Don't draw cursor glyph taller than our actual glyph. */
1916 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1917 if (height < r.height)
1918 {
1919 max_y = r.y + r.height;
1920 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1921 r.height = min (max_y - r.y, height);
1922 }
1923 }
1924 }
1925
1926 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1927 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1928 {
1929 #ifdef CONVERT_FROM_XRECT
1930 CONVERT_FROM_XRECT (r, *rects);
1931 #else
1932 *rects = r;
1933 #endif
1934 return 1;
1935 }
1936 else
1937 {
1938 /* If we are processing overlapping and allowed to return
1939 multiple clipping rectangles, we exclude the row of the glyph
1940 string from the clipping rectangle. This is to avoid drawing
1941 the same text on the environment with anti-aliasing. */
1942 #ifdef CONVERT_FROM_XRECT
1943 XRectangle rs[2];
1944 #else
1945 XRectangle *rs = rects;
1946 #endif
1947 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1948
1949 if (s->for_overlaps & OVERLAPS_PRED)
1950 {
1951 rs[i] = r;
1952 if (r.y + r.height > row_y)
1953 {
1954 if (r.y < row_y)
1955 rs[i].height = row_y - r.y;
1956 else
1957 rs[i].height = 0;
1958 }
1959 i++;
1960 }
1961 if (s->for_overlaps & OVERLAPS_SUCC)
1962 {
1963 rs[i] = r;
1964 if (r.y < row_y + s->row->visible_height)
1965 {
1966 if (r.y + r.height > row_y + s->row->visible_height)
1967 {
1968 rs[i].y = row_y + s->row->visible_height;
1969 rs[i].height = r.y + r.height - rs[i].y;
1970 }
1971 else
1972 rs[i].height = 0;
1973 }
1974 i++;
1975 }
1976
1977 n = i;
1978 #ifdef CONVERT_FROM_XRECT
1979 for (i = 0; i < n; i++)
1980 CONVERT_FROM_XRECT (rs[i], rects[i]);
1981 #endif
1982 return n;
1983 }
1984 }
1985
1986 /* EXPORT:
1987 Return in *NR the clipping rectangle for glyph string S. */
1988
1989 void
1990 get_glyph_string_clip_rect (s, nr)
1991 struct glyph_string *s;
1992 NativeRectangle *nr;
1993 {
1994 get_glyph_string_clip_rects (s, nr, 1);
1995 }
1996
1997
1998 /* EXPORT:
1999 Return the position and height of the phys cursor in window W.
2000 Set w->phys_cursor_width to width of phys cursor.
2001 */
2002
2003 void
2004 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2005 struct window *w;
2006 struct glyph_row *row;
2007 struct glyph *glyph;
2008 int *xp, *yp, *heightp;
2009 {
2010 struct frame *f = XFRAME (WINDOW_FRAME (w));
2011 int x, y, wd, h, h0, y0;
2012
2013 /* Compute the width of the rectangle to draw. If on a stretch
2014 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2015 rectangle as wide as the glyph, but use a canonical character
2016 width instead. */
2017 wd = glyph->pixel_width - 1;
2018 #ifdef HAVE_NTGUI
2019 wd++; /* Why? */
2020 #endif
2021
2022 x = w->phys_cursor.x;
2023 if (x < 0)
2024 {
2025 wd += x;
2026 x = 0;
2027 }
2028
2029 if (glyph->type == STRETCH_GLYPH
2030 && !x_stretch_cursor_p)
2031 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2032 w->phys_cursor_width = wd;
2033
2034 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2035
2036 /* If y is below window bottom, ensure that we still see a cursor. */
2037 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2038
2039 h = max (h0, glyph->ascent + glyph->descent);
2040 h0 = min (h0, glyph->ascent + glyph->descent);
2041
2042 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2043 if (y < y0)
2044 {
2045 h = max (h - (y0 - y) + 1, h0);
2046 y = y0 - 1;
2047 }
2048 else
2049 {
2050 y0 = window_text_bottom_y (w) - h0;
2051 if (y > y0)
2052 {
2053 h += y - y0;
2054 y = y0;
2055 }
2056 }
2057
2058 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2059 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2060 *heightp = h;
2061 }
2062
2063 /*
2064 * Remember which glyph the mouse is over.
2065 */
2066
2067 void
2068 remember_mouse_glyph (f, gx, gy, rect)
2069 struct frame *f;
2070 int gx, gy;
2071 NativeRectangle *rect;
2072 {
2073 Lisp_Object window;
2074 struct window *w;
2075 struct glyph_row *r, *gr, *end_row;
2076 enum window_part part;
2077 enum glyph_row_area area;
2078 int x, y, width, height;
2079
2080 /* Try to determine frame pixel position and size of the glyph under
2081 frame pixel coordinates X/Y on frame F. */
2082
2083 if (!f->glyphs_initialized_p
2084 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2085 NILP (window)))
2086 {
2087 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2088 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2089 goto virtual_glyph;
2090 }
2091
2092 w = XWINDOW (window);
2093 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2094 height = WINDOW_FRAME_LINE_HEIGHT (w);
2095
2096 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2097 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2098
2099 if (w->pseudo_window_p)
2100 {
2101 area = TEXT_AREA;
2102 part = ON_MODE_LINE; /* Don't adjust margin. */
2103 goto text_glyph;
2104 }
2105
2106 switch (part)
2107 {
2108 case ON_LEFT_MARGIN:
2109 area = LEFT_MARGIN_AREA;
2110 goto text_glyph;
2111
2112 case ON_RIGHT_MARGIN:
2113 area = RIGHT_MARGIN_AREA;
2114 goto text_glyph;
2115
2116 case ON_HEADER_LINE:
2117 case ON_MODE_LINE:
2118 gr = (part == ON_HEADER_LINE
2119 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2120 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2121 gy = gr->y;
2122 area = TEXT_AREA;
2123 goto text_glyph_row_found;
2124
2125 case ON_TEXT:
2126 area = TEXT_AREA;
2127
2128 text_glyph:
2129 gr = 0; gy = 0;
2130 for (; r <= end_row && r->enabled_p; ++r)
2131 if (r->y + r->height > y)
2132 {
2133 gr = r; gy = r->y;
2134 break;
2135 }
2136
2137 text_glyph_row_found:
2138 if (gr && gy <= y)
2139 {
2140 struct glyph *g = gr->glyphs[area];
2141 struct glyph *end = g + gr->used[area];
2142
2143 height = gr->height;
2144 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2145 if (gx + g->pixel_width > x)
2146 break;
2147
2148 if (g < end)
2149 {
2150 if (g->type == IMAGE_GLYPH)
2151 {
2152 /* Don't remember when mouse is over image, as
2153 image may have hot-spots. */
2154 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2155 return;
2156 }
2157 width = g->pixel_width;
2158 }
2159 else
2160 {
2161 /* Use nominal char spacing at end of line. */
2162 x -= gx;
2163 gx += (x / width) * width;
2164 }
2165
2166 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2167 gx += window_box_left_offset (w, area);
2168 }
2169 else
2170 {
2171 /* Use nominal line height at end of window. */
2172 gx = (x / width) * width;
2173 y -= gy;
2174 gy += (y / height) * height;
2175 }
2176 break;
2177
2178 case ON_LEFT_FRINGE:
2179 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2180 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2181 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2182 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2183 goto row_glyph;
2184
2185 case ON_RIGHT_FRINGE:
2186 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2187 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2188 : window_box_right_offset (w, TEXT_AREA));
2189 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2190 goto row_glyph;
2191
2192 case ON_SCROLL_BAR:
2193 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2194 ? 0
2195 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2196 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2197 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2198 : 0)));
2199 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2200
2201 row_glyph:
2202 gr = 0, gy = 0;
2203 for (; r <= end_row && r->enabled_p; ++r)
2204 if (r->y + r->height > y)
2205 {
2206 gr = r; gy = r->y;
2207 break;
2208 }
2209
2210 if (gr && gy <= y)
2211 height = gr->height;
2212 else
2213 {
2214 /* Use nominal line height at end of window. */
2215 y -= gy;
2216 gy += (y / height) * height;
2217 }
2218 break;
2219
2220 default:
2221 ;
2222 virtual_glyph:
2223 /* If there is no glyph under the mouse, then we divide the screen
2224 into a grid of the smallest glyph in the frame, and use that
2225 as our "glyph". */
2226
2227 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2228 round down even for negative values. */
2229 if (gx < 0)
2230 gx -= width - 1;
2231 if (gy < 0)
2232 gy -= height - 1;
2233
2234 gx = (gx / width) * width;
2235 gy = (gy / height) * height;
2236
2237 goto store_rect;
2238 }
2239
2240 gx += WINDOW_LEFT_EDGE_X (w);
2241 gy += WINDOW_TOP_EDGE_Y (w);
2242
2243 store_rect:
2244 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2245
2246 /* Visible feedback for debugging. */
2247 #if 0
2248 #if HAVE_X_WINDOWS
2249 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2250 f->output_data.x->normal_gc,
2251 gx, gy, width, height);
2252 #endif
2253 #endif
2254 }
2255
2256
2257 #endif /* HAVE_WINDOW_SYSTEM */
2258
2259 \f
2260 /***********************************************************************
2261 Lisp form evaluation
2262 ***********************************************************************/
2263
2264 /* Error handler for safe_eval and safe_call. */
2265
2266 static Lisp_Object
2267 safe_eval_handler (arg)
2268 Lisp_Object arg;
2269 {
2270 add_to_log ("Error during redisplay: %s", arg, Qnil);
2271 return Qnil;
2272 }
2273
2274
2275 /* Evaluate SEXPR and return the result, or nil if something went
2276 wrong. Prevent redisplay during the evaluation. */
2277
2278 Lisp_Object
2279 safe_eval (sexpr)
2280 Lisp_Object sexpr;
2281 {
2282 Lisp_Object val;
2283
2284 if (inhibit_eval_during_redisplay)
2285 val = Qnil;
2286 else
2287 {
2288 int count = SPECPDL_INDEX ();
2289 struct gcpro gcpro1;
2290
2291 GCPRO1 (sexpr);
2292 specbind (Qinhibit_redisplay, Qt);
2293 /* Use Qt to ensure debugger does not run,
2294 so there is no possibility of wanting to redisplay. */
2295 val = internal_condition_case_1 (Feval, sexpr, Qt,
2296 safe_eval_handler);
2297 UNGCPRO;
2298 val = unbind_to (count, val);
2299 }
2300
2301 return val;
2302 }
2303
2304
2305 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2306 Return the result, or nil if something went wrong. Prevent
2307 redisplay during the evaluation. */
2308
2309 Lisp_Object
2310 safe_call (nargs, args)
2311 int nargs;
2312 Lisp_Object *args;
2313 {
2314 Lisp_Object val;
2315
2316 if (inhibit_eval_during_redisplay)
2317 val = Qnil;
2318 else
2319 {
2320 int count = SPECPDL_INDEX ();
2321 struct gcpro gcpro1;
2322
2323 GCPRO1 (args[0]);
2324 gcpro1.nvars = nargs;
2325 specbind (Qinhibit_redisplay, Qt);
2326 /* Use Qt to ensure debugger does not run,
2327 so there is no possibility of wanting to redisplay. */
2328 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2329 safe_eval_handler);
2330 UNGCPRO;
2331 val = unbind_to (count, val);
2332 }
2333
2334 return val;
2335 }
2336
2337
2338 /* Call function FN with one argument ARG.
2339 Return the result, or nil if something went wrong. */
2340
2341 Lisp_Object
2342 safe_call1 (fn, arg)
2343 Lisp_Object fn, arg;
2344 {
2345 Lisp_Object args[2];
2346 args[0] = fn;
2347 args[1] = arg;
2348 return safe_call (2, args);
2349 }
2350
2351
2352 \f
2353 /***********************************************************************
2354 Debugging
2355 ***********************************************************************/
2356
2357 #if 0
2358
2359 /* Define CHECK_IT to perform sanity checks on iterators.
2360 This is for debugging. It is too slow to do unconditionally. */
2361
2362 static void
2363 check_it (it)
2364 struct it *it;
2365 {
2366 if (it->method == GET_FROM_STRING)
2367 {
2368 xassert (STRINGP (it->string));
2369 xassert (IT_STRING_CHARPOS (*it) >= 0);
2370 }
2371 else
2372 {
2373 xassert (IT_STRING_CHARPOS (*it) < 0);
2374 if (it->method == GET_FROM_BUFFER)
2375 {
2376 /* Check that character and byte positions agree. */
2377 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2378 }
2379 }
2380
2381 if (it->dpvec)
2382 xassert (it->current.dpvec_index >= 0);
2383 else
2384 xassert (it->current.dpvec_index < 0);
2385 }
2386
2387 #define CHECK_IT(IT) check_it ((IT))
2388
2389 #else /* not 0 */
2390
2391 #define CHECK_IT(IT) (void) 0
2392
2393 #endif /* not 0 */
2394
2395
2396 #if GLYPH_DEBUG
2397
2398 /* Check that the window end of window W is what we expect it
2399 to be---the last row in the current matrix displaying text. */
2400
2401 static void
2402 check_window_end (w)
2403 struct window *w;
2404 {
2405 if (!MINI_WINDOW_P (w)
2406 && !NILP (w->window_end_valid))
2407 {
2408 struct glyph_row *row;
2409 xassert ((row = MATRIX_ROW (w->current_matrix,
2410 XFASTINT (w->window_end_vpos)),
2411 !row->enabled_p
2412 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2413 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2414 }
2415 }
2416
2417 #define CHECK_WINDOW_END(W) check_window_end ((W))
2418
2419 #else /* not GLYPH_DEBUG */
2420
2421 #define CHECK_WINDOW_END(W) (void) 0
2422
2423 #endif /* not GLYPH_DEBUG */
2424
2425
2426 \f
2427 /***********************************************************************
2428 Iterator initialization
2429 ***********************************************************************/
2430
2431 /* Initialize IT for displaying current_buffer in window W, starting
2432 at character position CHARPOS. CHARPOS < 0 means that no buffer
2433 position is specified which is useful when the iterator is assigned
2434 a position later. BYTEPOS is the byte position corresponding to
2435 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2436
2437 If ROW is not null, calls to produce_glyphs with IT as parameter
2438 will produce glyphs in that row.
2439
2440 BASE_FACE_ID is the id of a base face to use. It must be one of
2441 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2442 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2443 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2444
2445 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2446 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2447 will be initialized to use the corresponding mode line glyph row of
2448 the desired matrix of W. */
2449
2450 void
2451 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2452 struct it *it;
2453 struct window *w;
2454 int charpos, bytepos;
2455 struct glyph_row *row;
2456 enum face_id base_face_id;
2457 {
2458 int highlight_region_p;
2459
2460 /* Some precondition checks. */
2461 xassert (w != NULL && it != NULL);
2462 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2463 && charpos <= ZV));
2464
2465 /* If face attributes have been changed since the last redisplay,
2466 free realized faces now because they depend on face definitions
2467 that might have changed. Don't free faces while there might be
2468 desired matrices pending which reference these faces. */
2469 if (face_change_count && !inhibit_free_realized_faces)
2470 {
2471 face_change_count = 0;
2472 free_all_realized_faces (Qnil);
2473 }
2474
2475 /* Use one of the mode line rows of W's desired matrix if
2476 appropriate. */
2477 if (row == NULL)
2478 {
2479 if (base_face_id == MODE_LINE_FACE_ID
2480 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2481 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2482 else if (base_face_id == HEADER_LINE_FACE_ID)
2483 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2484 }
2485
2486 /* Clear IT. */
2487 bzero (it, sizeof *it);
2488 it->current.overlay_string_index = -1;
2489 it->current.dpvec_index = -1;
2490 it->base_face_id = base_face_id;
2491 it->string = Qnil;
2492 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2493
2494 /* The window in which we iterate over current_buffer: */
2495 XSETWINDOW (it->window, w);
2496 it->w = w;
2497 it->f = XFRAME (w->frame);
2498
2499 /* Extra space between lines (on window systems only). */
2500 if (base_face_id == DEFAULT_FACE_ID
2501 && FRAME_WINDOW_P (it->f))
2502 {
2503 if (NATNUMP (current_buffer->extra_line_spacing))
2504 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2505 else if (FLOATP (current_buffer->extra_line_spacing))
2506 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2507 * FRAME_LINE_HEIGHT (it->f));
2508 else if (it->f->extra_line_spacing > 0)
2509 it->extra_line_spacing = it->f->extra_line_spacing;
2510 it->max_extra_line_spacing = 0;
2511 }
2512
2513 /* If realized faces have been removed, e.g. because of face
2514 attribute changes of named faces, recompute them. When running
2515 in batch mode, the face cache of the initial frame is null. If
2516 we happen to get called, make a dummy face cache. */
2517 if (FRAME_FACE_CACHE (it->f) == NULL)
2518 init_frame_faces (it->f);
2519 if (FRAME_FACE_CACHE (it->f)->used == 0)
2520 recompute_basic_faces (it->f);
2521
2522 /* Current value of the `slice', `space-width', and 'height' properties. */
2523 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2524 it->space_width = Qnil;
2525 it->font_height = Qnil;
2526 it->override_ascent = -1;
2527
2528 /* Are control characters displayed as `^C'? */
2529 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2530
2531 /* -1 means everything between a CR and the following line end
2532 is invisible. >0 means lines indented more than this value are
2533 invisible. */
2534 it->selective = (INTEGERP (current_buffer->selective_display)
2535 ? XFASTINT (current_buffer->selective_display)
2536 : (!NILP (current_buffer->selective_display)
2537 ? -1 : 0));
2538 it->selective_display_ellipsis_p
2539 = !NILP (current_buffer->selective_display_ellipses);
2540
2541 /* Display table to use. */
2542 it->dp = window_display_table (w);
2543
2544 /* Are multibyte characters enabled in current_buffer? */
2545 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2546
2547 /* Non-zero if we should highlight the region. */
2548 highlight_region_p
2549 = (!NILP (Vtransient_mark_mode)
2550 && !NILP (current_buffer->mark_active)
2551 && XMARKER (current_buffer->mark)->buffer != 0);
2552
2553 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2554 start and end of a visible region in window IT->w. Set both to
2555 -1 to indicate no region. */
2556 if (highlight_region_p
2557 /* Maybe highlight only in selected window. */
2558 && (/* Either show region everywhere. */
2559 highlight_nonselected_windows
2560 /* Or show region in the selected window. */
2561 || w == XWINDOW (selected_window)
2562 /* Or show the region if we are in the mini-buffer and W is
2563 the window the mini-buffer refers to. */
2564 || (MINI_WINDOW_P (XWINDOW (selected_window))
2565 && WINDOWP (minibuf_selected_window)
2566 && w == XWINDOW (minibuf_selected_window))))
2567 {
2568 int charpos = marker_position (current_buffer->mark);
2569 it->region_beg_charpos = min (PT, charpos);
2570 it->region_end_charpos = max (PT, charpos);
2571 }
2572 else
2573 it->region_beg_charpos = it->region_end_charpos = -1;
2574
2575 /* Get the position at which the redisplay_end_trigger hook should
2576 be run, if it is to be run at all. */
2577 if (MARKERP (w->redisplay_end_trigger)
2578 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2579 it->redisplay_end_trigger_charpos
2580 = marker_position (w->redisplay_end_trigger);
2581 else if (INTEGERP (w->redisplay_end_trigger))
2582 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2583
2584 /* Correct bogus values of tab_width. */
2585 it->tab_width = XINT (current_buffer->tab_width);
2586 if (it->tab_width <= 0 || it->tab_width > 1000)
2587 it->tab_width = 8;
2588
2589 /* Are lines in the display truncated? */
2590 it->truncate_lines_p
2591 = (base_face_id != DEFAULT_FACE_ID
2592 || XINT (it->w->hscroll)
2593 || (truncate_partial_width_windows
2594 && !WINDOW_FULL_WIDTH_P (it->w))
2595 || !NILP (current_buffer->truncate_lines));
2596
2597 /* Get dimensions of truncation and continuation glyphs. These are
2598 displayed as fringe bitmaps under X, so we don't need them for such
2599 frames. */
2600 if (!FRAME_WINDOW_P (it->f))
2601 {
2602 if (it->truncate_lines_p)
2603 {
2604 /* We will need the truncation glyph. */
2605 xassert (it->glyph_row == NULL);
2606 produce_special_glyphs (it, IT_TRUNCATION);
2607 it->truncation_pixel_width = it->pixel_width;
2608 }
2609 else
2610 {
2611 /* We will need the continuation glyph. */
2612 xassert (it->glyph_row == NULL);
2613 produce_special_glyphs (it, IT_CONTINUATION);
2614 it->continuation_pixel_width = it->pixel_width;
2615 }
2616
2617 /* Reset these values to zero because the produce_special_glyphs
2618 above has changed them. */
2619 it->pixel_width = it->ascent = it->descent = 0;
2620 it->phys_ascent = it->phys_descent = 0;
2621 }
2622
2623 /* Set this after getting the dimensions of truncation and
2624 continuation glyphs, so that we don't produce glyphs when calling
2625 produce_special_glyphs, above. */
2626 it->glyph_row = row;
2627 it->area = TEXT_AREA;
2628
2629 /* Get the dimensions of the display area. The display area
2630 consists of the visible window area plus a horizontally scrolled
2631 part to the left of the window. All x-values are relative to the
2632 start of this total display area. */
2633 if (base_face_id != DEFAULT_FACE_ID)
2634 {
2635 /* Mode lines, menu bar in terminal frames. */
2636 it->first_visible_x = 0;
2637 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2638 }
2639 else
2640 {
2641 it->first_visible_x
2642 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2643 it->last_visible_x = (it->first_visible_x
2644 + window_box_width (w, TEXT_AREA));
2645
2646 /* If we truncate lines, leave room for the truncator glyph(s) at
2647 the right margin. Otherwise, leave room for the continuation
2648 glyph(s). Truncation and continuation glyphs are not inserted
2649 for window-based redisplay. */
2650 if (!FRAME_WINDOW_P (it->f))
2651 {
2652 if (it->truncate_lines_p)
2653 it->last_visible_x -= it->truncation_pixel_width;
2654 else
2655 it->last_visible_x -= it->continuation_pixel_width;
2656 }
2657
2658 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2659 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2660 }
2661
2662 /* Leave room for a border glyph. */
2663 if (!FRAME_WINDOW_P (it->f)
2664 && !WINDOW_RIGHTMOST_P (it->w))
2665 it->last_visible_x -= 1;
2666
2667 it->last_visible_y = window_text_bottom_y (w);
2668
2669 /* For mode lines and alike, arrange for the first glyph having a
2670 left box line if the face specifies a box. */
2671 if (base_face_id != DEFAULT_FACE_ID)
2672 {
2673 struct face *face;
2674
2675 it->face_id = base_face_id;
2676
2677 /* If we have a boxed mode line, make the first character appear
2678 with a left box line. */
2679 face = FACE_FROM_ID (it->f, base_face_id);
2680 if (face->box != FACE_NO_BOX)
2681 it->start_of_box_run_p = 1;
2682 }
2683
2684 /* If a buffer position was specified, set the iterator there,
2685 getting overlays and face properties from that position. */
2686 if (charpos >= BUF_BEG (current_buffer))
2687 {
2688 it->end_charpos = ZV;
2689 it->face_id = -1;
2690 IT_CHARPOS (*it) = charpos;
2691
2692 /* Compute byte position if not specified. */
2693 if (bytepos < charpos)
2694 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2695 else
2696 IT_BYTEPOS (*it) = bytepos;
2697
2698 it->start = it->current;
2699
2700 /* Compute faces etc. */
2701 reseat (it, it->current.pos, 1);
2702 }
2703
2704 CHECK_IT (it);
2705 }
2706
2707
2708 /* Initialize IT for the display of window W with window start POS. */
2709
2710 void
2711 start_display (it, w, pos)
2712 struct it *it;
2713 struct window *w;
2714 struct text_pos pos;
2715 {
2716 struct glyph_row *row;
2717 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2718
2719 row = w->desired_matrix->rows + first_vpos;
2720 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2721 it->first_vpos = first_vpos;
2722
2723 /* Don't reseat to previous visible line start if current start
2724 position is in a string or image. */
2725 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2726 {
2727 int start_at_line_beg_p;
2728 int first_y = it->current_y;
2729
2730 /* If window start is not at a line start, skip forward to POS to
2731 get the correct continuation lines width. */
2732 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2733 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2734 if (!start_at_line_beg_p)
2735 {
2736 int new_x;
2737
2738 reseat_at_previous_visible_line_start (it);
2739 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2740
2741 new_x = it->current_x + it->pixel_width;
2742
2743 /* If lines are continued, this line may end in the middle
2744 of a multi-glyph character (e.g. a control character
2745 displayed as \003, or in the middle of an overlay
2746 string). In this case move_it_to above will not have
2747 taken us to the start of the continuation line but to the
2748 end of the continued line. */
2749 if (it->current_x > 0
2750 && !it->truncate_lines_p /* Lines are continued. */
2751 && (/* And glyph doesn't fit on the line. */
2752 new_x > it->last_visible_x
2753 /* Or it fits exactly and we're on a window
2754 system frame. */
2755 || (new_x == it->last_visible_x
2756 && FRAME_WINDOW_P (it->f))))
2757 {
2758 if (it->current.dpvec_index >= 0
2759 || it->current.overlay_string_index >= 0)
2760 {
2761 set_iterator_to_next (it, 1);
2762 move_it_in_display_line_to (it, -1, -1, 0);
2763 }
2764
2765 it->continuation_lines_width += it->current_x;
2766 }
2767
2768 /* We're starting a new display line, not affected by the
2769 height of the continued line, so clear the appropriate
2770 fields in the iterator structure. */
2771 it->max_ascent = it->max_descent = 0;
2772 it->max_phys_ascent = it->max_phys_descent = 0;
2773
2774 it->current_y = first_y;
2775 it->vpos = 0;
2776 it->current_x = it->hpos = 0;
2777 }
2778 }
2779
2780 #if 0 /* Don't assert the following because start_display is sometimes
2781 called intentionally with a window start that is not at a
2782 line start. Please leave this code in as a comment. */
2783
2784 /* Window start should be on a line start, now. */
2785 xassert (it->continuation_lines_width
2786 || IT_CHARPOS (it) == BEGV
2787 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2788 #endif /* 0 */
2789 }
2790
2791
2792 /* Return 1 if POS is a position in ellipses displayed for invisible
2793 text. W is the window we display, for text property lookup. */
2794
2795 static int
2796 in_ellipses_for_invisible_text_p (pos, w)
2797 struct display_pos *pos;
2798 struct window *w;
2799 {
2800 Lisp_Object prop, window;
2801 int ellipses_p = 0;
2802 int charpos = CHARPOS (pos->pos);
2803
2804 /* If POS specifies a position in a display vector, this might
2805 be for an ellipsis displayed for invisible text. We won't
2806 get the iterator set up for delivering that ellipsis unless
2807 we make sure that it gets aware of the invisible text. */
2808 if (pos->dpvec_index >= 0
2809 && pos->overlay_string_index < 0
2810 && CHARPOS (pos->string_pos) < 0
2811 && charpos > BEGV
2812 && (XSETWINDOW (window, w),
2813 prop = Fget_char_property (make_number (charpos),
2814 Qinvisible, window),
2815 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2816 {
2817 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2818 window);
2819 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2820 }
2821
2822 return ellipses_p;
2823 }
2824
2825
2826 /* Initialize IT for stepping through current_buffer in window W,
2827 starting at position POS that includes overlay string and display
2828 vector/ control character translation position information. Value
2829 is zero if there are overlay strings with newlines at POS. */
2830
2831 static int
2832 init_from_display_pos (it, w, pos)
2833 struct it *it;
2834 struct window *w;
2835 struct display_pos *pos;
2836 {
2837 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2838 int i, overlay_strings_with_newlines = 0;
2839
2840 /* If POS specifies a position in a display vector, this might
2841 be for an ellipsis displayed for invisible text. We won't
2842 get the iterator set up for delivering that ellipsis unless
2843 we make sure that it gets aware of the invisible text. */
2844 if (in_ellipses_for_invisible_text_p (pos, w))
2845 {
2846 --charpos;
2847 bytepos = 0;
2848 }
2849
2850 /* Keep in mind: the call to reseat in init_iterator skips invisible
2851 text, so we might end up at a position different from POS. This
2852 is only a problem when POS is a row start after a newline and an
2853 overlay starts there with an after-string, and the overlay has an
2854 invisible property. Since we don't skip invisible text in
2855 display_line and elsewhere immediately after consuming the
2856 newline before the row start, such a POS will not be in a string,
2857 but the call to init_iterator below will move us to the
2858 after-string. */
2859 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2860
2861 /* This only scans the current chunk -- it should scan all chunks.
2862 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2863 to 16 in 22.1 to make this a lesser problem. */
2864 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2865 {
2866 const char *s = SDATA (it->overlay_strings[i]);
2867 const char *e = s + SBYTES (it->overlay_strings[i]);
2868
2869 while (s < e && *s != '\n')
2870 ++s;
2871
2872 if (s < e)
2873 {
2874 overlay_strings_with_newlines = 1;
2875 break;
2876 }
2877 }
2878
2879 /* If position is within an overlay string, set up IT to the right
2880 overlay string. */
2881 if (pos->overlay_string_index >= 0)
2882 {
2883 int relative_index;
2884
2885 /* If the first overlay string happens to have a `display'
2886 property for an image, the iterator will be set up for that
2887 image, and we have to undo that setup first before we can
2888 correct the overlay string index. */
2889 if (it->method == GET_FROM_IMAGE)
2890 pop_it (it);
2891
2892 /* We already have the first chunk of overlay strings in
2893 IT->overlay_strings. Load more until the one for
2894 pos->overlay_string_index is in IT->overlay_strings. */
2895 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2896 {
2897 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2898 it->current.overlay_string_index = 0;
2899 while (n--)
2900 {
2901 load_overlay_strings (it, 0);
2902 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2903 }
2904 }
2905
2906 it->current.overlay_string_index = pos->overlay_string_index;
2907 relative_index = (it->current.overlay_string_index
2908 % OVERLAY_STRING_CHUNK_SIZE);
2909 it->string = it->overlay_strings[relative_index];
2910 xassert (STRINGP (it->string));
2911 it->current.string_pos = pos->string_pos;
2912 it->method = GET_FROM_STRING;
2913 }
2914
2915 #if 0 /* This is bogus because POS not having an overlay string
2916 position does not mean it's after the string. Example: A
2917 line starting with a before-string and initialization of IT
2918 to the previous row's end position. */
2919 else if (it->current.overlay_string_index >= 0)
2920 {
2921 /* If POS says we're already after an overlay string ending at
2922 POS, make sure to pop the iterator because it will be in
2923 front of that overlay string. When POS is ZV, we've thereby
2924 also ``processed'' overlay strings at ZV. */
2925 while (it->sp)
2926 pop_it (it);
2927 xassert (it->current.overlay_string_index == -1);
2928 xassert (it->method == GET_FROM_BUFFER);
2929 if (CHARPOS (pos->pos) == ZV)
2930 it->overlay_strings_at_end_processed_p = 1;
2931 }
2932 #endif /* 0 */
2933
2934 if (CHARPOS (pos->string_pos) >= 0)
2935 {
2936 /* Recorded position is not in an overlay string, but in another
2937 string. This can only be a string from a `display' property.
2938 IT should already be filled with that string. */
2939 it->current.string_pos = pos->string_pos;
2940 xassert (STRINGP (it->string));
2941 }
2942
2943 /* Restore position in display vector translations, control
2944 character translations or ellipses. */
2945 if (pos->dpvec_index >= 0)
2946 {
2947 if (it->dpvec == NULL)
2948 get_next_display_element (it);
2949 xassert (it->dpvec && it->current.dpvec_index == 0);
2950 it->current.dpvec_index = pos->dpvec_index;
2951 }
2952
2953 CHECK_IT (it);
2954 return !overlay_strings_with_newlines;
2955 }
2956
2957
2958 /* Initialize IT for stepping through current_buffer in window W
2959 starting at ROW->start. */
2960
2961 static void
2962 init_to_row_start (it, w, row)
2963 struct it *it;
2964 struct window *w;
2965 struct glyph_row *row;
2966 {
2967 init_from_display_pos (it, w, &row->start);
2968 it->start = row->start;
2969 it->continuation_lines_width = row->continuation_lines_width;
2970 CHECK_IT (it);
2971 }
2972
2973
2974 /* Initialize IT for stepping through current_buffer in window W
2975 starting in the line following ROW, i.e. starting at ROW->end.
2976 Value is zero if there are overlay strings with newlines at ROW's
2977 end position. */
2978
2979 static int
2980 init_to_row_end (it, w, row)
2981 struct it *it;
2982 struct window *w;
2983 struct glyph_row *row;
2984 {
2985 int success = 0;
2986
2987 if (init_from_display_pos (it, w, &row->end))
2988 {
2989 if (row->continued_p)
2990 it->continuation_lines_width
2991 = row->continuation_lines_width + row->pixel_width;
2992 CHECK_IT (it);
2993 success = 1;
2994 }
2995
2996 return success;
2997 }
2998
2999
3000
3001 \f
3002 /***********************************************************************
3003 Text properties
3004 ***********************************************************************/
3005
3006 /* Called when IT reaches IT->stop_charpos. Handle text property and
3007 overlay changes. Set IT->stop_charpos to the next position where
3008 to stop. */
3009
3010 static void
3011 handle_stop (it)
3012 struct it *it;
3013 {
3014 enum prop_handled handled;
3015 int handle_overlay_change_p;
3016 struct props *p;
3017
3018 it->dpvec = NULL;
3019 it->current.dpvec_index = -1;
3020 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3021 it->ignore_overlay_strings_at_pos_p = 0;
3022
3023 /* Use face of preceding text for ellipsis (if invisible) */
3024 if (it->selective_display_ellipsis_p)
3025 it->saved_face_id = it->face_id;
3026
3027 do
3028 {
3029 handled = HANDLED_NORMALLY;
3030
3031 /* Call text property handlers. */
3032 for (p = it_props; p->handler; ++p)
3033 {
3034 handled = p->handler (it);
3035
3036 if (handled == HANDLED_RECOMPUTE_PROPS)
3037 break;
3038 else if (handled == HANDLED_RETURN)
3039 {
3040 /* We still want to show before and after strings from
3041 overlays even if the actual buffer text is replaced. */
3042 if (!handle_overlay_change_p || it->sp > 1)
3043 return;
3044 if (!get_overlay_strings_1 (it, 0, 0))
3045 return;
3046 it->ignore_overlay_strings_at_pos_p = 1;
3047 it->string_from_display_prop_p = 0;
3048 handle_overlay_change_p = 0;
3049 handled = HANDLED_RECOMPUTE_PROPS;
3050 break;
3051 }
3052 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3053 handle_overlay_change_p = 0;
3054 }
3055
3056 if (handled != HANDLED_RECOMPUTE_PROPS)
3057 {
3058 /* Don't check for overlay strings below when set to deliver
3059 characters from a display vector. */
3060 if (it->method == GET_FROM_DISPLAY_VECTOR)
3061 handle_overlay_change_p = 0;
3062
3063 /* Handle overlay changes. */
3064 if (handle_overlay_change_p)
3065 handled = handle_overlay_change (it);
3066
3067 /* Determine where to stop next. */
3068 if (handled == HANDLED_NORMALLY)
3069 compute_stop_pos (it);
3070 }
3071 }
3072 while (handled == HANDLED_RECOMPUTE_PROPS);
3073 }
3074
3075
3076 /* Compute IT->stop_charpos from text property and overlay change
3077 information for IT's current position. */
3078
3079 static void
3080 compute_stop_pos (it)
3081 struct it *it;
3082 {
3083 register INTERVAL iv, next_iv;
3084 Lisp_Object object, limit, position;
3085
3086 /* If nowhere else, stop at the end. */
3087 it->stop_charpos = it->end_charpos;
3088
3089 if (STRINGP (it->string))
3090 {
3091 /* Strings are usually short, so don't limit the search for
3092 properties. */
3093 object = it->string;
3094 limit = Qnil;
3095 position = make_number (IT_STRING_CHARPOS (*it));
3096 }
3097 else
3098 {
3099 int charpos;
3100
3101 /* If next overlay change is in front of the current stop pos
3102 (which is IT->end_charpos), stop there. Note: value of
3103 next_overlay_change is point-max if no overlay change
3104 follows. */
3105 charpos = next_overlay_change (IT_CHARPOS (*it));
3106 if (charpos < it->stop_charpos)
3107 it->stop_charpos = charpos;
3108
3109 /* If showing the region, we have to stop at the region
3110 start or end because the face might change there. */
3111 if (it->region_beg_charpos > 0)
3112 {
3113 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3114 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3115 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3116 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3117 }
3118
3119 /* Set up variables for computing the stop position from text
3120 property changes. */
3121 XSETBUFFER (object, current_buffer);
3122 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3123 position = make_number (IT_CHARPOS (*it));
3124
3125 }
3126
3127 /* Get the interval containing IT's position. Value is a null
3128 interval if there isn't such an interval. */
3129 iv = validate_interval_range (object, &position, &position, 0);
3130 if (!NULL_INTERVAL_P (iv))
3131 {
3132 Lisp_Object values_here[LAST_PROP_IDX];
3133 struct props *p;
3134
3135 /* Get properties here. */
3136 for (p = it_props; p->handler; ++p)
3137 values_here[p->idx] = textget (iv->plist, *p->name);
3138
3139 /* Look for an interval following iv that has different
3140 properties. */
3141 for (next_iv = next_interval (iv);
3142 (!NULL_INTERVAL_P (next_iv)
3143 && (NILP (limit)
3144 || XFASTINT (limit) > next_iv->position));
3145 next_iv = next_interval (next_iv))
3146 {
3147 for (p = it_props; p->handler; ++p)
3148 {
3149 Lisp_Object new_value;
3150
3151 new_value = textget (next_iv->plist, *p->name);
3152 if (!EQ (values_here[p->idx], new_value))
3153 break;
3154 }
3155
3156 if (p->handler)
3157 break;
3158 }
3159
3160 if (!NULL_INTERVAL_P (next_iv))
3161 {
3162 if (INTEGERP (limit)
3163 && next_iv->position >= XFASTINT (limit))
3164 /* No text property change up to limit. */
3165 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3166 else
3167 /* Text properties change in next_iv. */
3168 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3169 }
3170 }
3171
3172 xassert (STRINGP (it->string)
3173 || (it->stop_charpos >= BEGV
3174 && it->stop_charpos >= IT_CHARPOS (*it)));
3175 }
3176
3177
3178 /* Return the position of the next overlay change after POS in
3179 current_buffer. Value is point-max if no overlay change
3180 follows. This is like `next-overlay-change' but doesn't use
3181 xmalloc. */
3182
3183 static int
3184 next_overlay_change (pos)
3185 int pos;
3186 {
3187 int noverlays;
3188 int endpos;
3189 Lisp_Object *overlays;
3190 int i;
3191
3192 /* Get all overlays at the given position. */
3193 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3194
3195 /* If any of these overlays ends before endpos,
3196 use its ending point instead. */
3197 for (i = 0; i < noverlays; ++i)
3198 {
3199 Lisp_Object oend;
3200 int oendpos;
3201
3202 oend = OVERLAY_END (overlays[i]);
3203 oendpos = OVERLAY_POSITION (oend);
3204 endpos = min (endpos, oendpos);
3205 }
3206
3207 return endpos;
3208 }
3209
3210
3211 \f
3212 /***********************************************************************
3213 Fontification
3214 ***********************************************************************/
3215
3216 /* Handle changes in the `fontified' property of the current buffer by
3217 calling hook functions from Qfontification_functions to fontify
3218 regions of text. */
3219
3220 static enum prop_handled
3221 handle_fontified_prop (it)
3222 struct it *it;
3223 {
3224 Lisp_Object prop, pos;
3225 enum prop_handled handled = HANDLED_NORMALLY;
3226
3227 if (!NILP (Vmemory_full))
3228 return handled;
3229
3230 /* Get the value of the `fontified' property at IT's current buffer
3231 position. (The `fontified' property doesn't have a special
3232 meaning in strings.) If the value is nil, call functions from
3233 Qfontification_functions. */
3234 if (!STRINGP (it->string)
3235 && it->s == NULL
3236 && !NILP (Vfontification_functions)
3237 && !NILP (Vrun_hooks)
3238 && (pos = make_number (IT_CHARPOS (*it)),
3239 prop = Fget_char_property (pos, Qfontified, Qnil),
3240 /* Ignore the special cased nil value always present at EOB since
3241 no amount of fontifying will be able to change it. */
3242 NILP (prop) && IT_CHARPOS (*it) < Z))
3243 {
3244 int count = SPECPDL_INDEX ();
3245 Lisp_Object val;
3246
3247 val = Vfontification_functions;
3248 specbind (Qfontification_functions, Qnil);
3249
3250 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3251 safe_call1 (val, pos);
3252 else
3253 {
3254 Lisp_Object globals, fn;
3255 struct gcpro gcpro1, gcpro2;
3256
3257 globals = Qnil;
3258 GCPRO2 (val, globals);
3259
3260 for (; CONSP (val); val = XCDR (val))
3261 {
3262 fn = XCAR (val);
3263
3264 if (EQ (fn, Qt))
3265 {
3266 /* A value of t indicates this hook has a local
3267 binding; it means to run the global binding too.
3268 In a global value, t should not occur. If it
3269 does, we must ignore it to avoid an endless
3270 loop. */
3271 for (globals = Fdefault_value (Qfontification_functions);
3272 CONSP (globals);
3273 globals = XCDR (globals))
3274 {
3275 fn = XCAR (globals);
3276 if (!EQ (fn, Qt))
3277 safe_call1 (fn, pos);
3278 }
3279 }
3280 else
3281 safe_call1 (fn, pos);
3282 }
3283
3284 UNGCPRO;
3285 }
3286
3287 unbind_to (count, Qnil);
3288
3289 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3290 something. This avoids an endless loop if they failed to
3291 fontify the text for which reason ever. */
3292 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3293 handled = HANDLED_RECOMPUTE_PROPS;
3294 }
3295
3296 return handled;
3297 }
3298
3299
3300 \f
3301 /***********************************************************************
3302 Faces
3303 ***********************************************************************/
3304
3305 /* Set up iterator IT from face properties at its current position.
3306 Called from handle_stop. */
3307
3308 static enum prop_handled
3309 handle_face_prop (it)
3310 struct it *it;
3311 {
3312 int new_face_id, next_stop;
3313
3314 if (!STRINGP (it->string))
3315 {
3316 new_face_id
3317 = face_at_buffer_position (it->w,
3318 IT_CHARPOS (*it),
3319 it->region_beg_charpos,
3320 it->region_end_charpos,
3321 &next_stop,
3322 (IT_CHARPOS (*it)
3323 + TEXT_PROP_DISTANCE_LIMIT),
3324 0);
3325
3326 /* Is this a start of a run of characters with box face?
3327 Caveat: this can be called for a freshly initialized
3328 iterator; face_id is -1 in this case. We know that the new
3329 face will not change until limit, i.e. if the new face has a
3330 box, all characters up to limit will have one. But, as
3331 usual, we don't know whether limit is really the end. */
3332 if (new_face_id != it->face_id)
3333 {
3334 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3335
3336 /* If new face has a box but old face has not, this is
3337 the start of a run of characters with box, i.e. it has
3338 a shadow on the left side. The value of face_id of the
3339 iterator will be -1 if this is the initial call that gets
3340 the face. In this case, we have to look in front of IT's
3341 position and see whether there is a face != new_face_id. */
3342 it->start_of_box_run_p
3343 = (new_face->box != FACE_NO_BOX
3344 && (it->face_id >= 0
3345 || IT_CHARPOS (*it) == BEG
3346 || new_face_id != face_before_it_pos (it)));
3347 it->face_box_p = new_face->box != FACE_NO_BOX;
3348 }
3349 }
3350 else
3351 {
3352 int base_face_id, bufpos;
3353
3354 if (it->current.overlay_string_index >= 0)
3355 bufpos = IT_CHARPOS (*it);
3356 else
3357 bufpos = 0;
3358
3359 /* For strings from a buffer, i.e. overlay strings or strings
3360 from a `display' property, use the face at IT's current
3361 buffer position as the base face to merge with, so that
3362 overlay strings appear in the same face as surrounding
3363 text, unless they specify their own faces. */
3364 base_face_id = underlying_face_id (it);
3365
3366 new_face_id = face_at_string_position (it->w,
3367 it->string,
3368 IT_STRING_CHARPOS (*it),
3369 bufpos,
3370 it->region_beg_charpos,
3371 it->region_end_charpos,
3372 &next_stop,
3373 base_face_id, 0);
3374
3375 #if 0 /* This shouldn't be neccessary. Let's check it. */
3376 /* If IT is used to display a mode line we would really like to
3377 use the mode line face instead of the frame's default face. */
3378 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3379 && new_face_id == DEFAULT_FACE_ID)
3380 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3381 #endif
3382
3383 /* Is this a start of a run of characters with box? Caveat:
3384 this can be called for a freshly allocated iterator; face_id
3385 is -1 is this case. We know that the new face will not
3386 change until the next check pos, i.e. if the new face has a
3387 box, all characters up to that position will have a
3388 box. But, as usual, we don't know whether that position
3389 is really the end. */
3390 if (new_face_id != it->face_id)
3391 {
3392 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3393 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3394
3395 /* If new face has a box but old face hasn't, this is the
3396 start of a run of characters with box, i.e. it has a
3397 shadow on the left side. */
3398 it->start_of_box_run_p
3399 = new_face->box && (old_face == NULL || !old_face->box);
3400 it->face_box_p = new_face->box != FACE_NO_BOX;
3401 }
3402 }
3403
3404 it->face_id = new_face_id;
3405 return HANDLED_NORMALLY;
3406 }
3407
3408
3409 /* Return the ID of the face ``underlying'' IT's current position,
3410 which is in a string. If the iterator is associated with a
3411 buffer, return the face at IT's current buffer position.
3412 Otherwise, use the iterator's base_face_id. */
3413
3414 static int
3415 underlying_face_id (it)
3416 struct it *it;
3417 {
3418 int face_id = it->base_face_id, i;
3419
3420 xassert (STRINGP (it->string));
3421
3422 for (i = it->sp - 1; i >= 0; --i)
3423 if (NILP (it->stack[i].string))
3424 face_id = it->stack[i].face_id;
3425
3426 return face_id;
3427 }
3428
3429
3430 /* Compute the face one character before or after the current position
3431 of IT. BEFORE_P non-zero means get the face in front of IT's
3432 position. Value is the id of the face. */
3433
3434 static int
3435 face_before_or_after_it_pos (it, before_p)
3436 struct it *it;
3437 int before_p;
3438 {
3439 int face_id, limit;
3440 int next_check_charpos;
3441 struct text_pos pos;
3442
3443 xassert (it->s == NULL);
3444
3445 if (STRINGP (it->string))
3446 {
3447 int bufpos, base_face_id;
3448
3449 /* No face change past the end of the string (for the case
3450 we are padding with spaces). No face change before the
3451 string start. */
3452 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3453 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3454 return it->face_id;
3455
3456 /* Set pos to the position before or after IT's current position. */
3457 if (before_p)
3458 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3459 else
3460 /* For composition, we must check the character after the
3461 composition. */
3462 pos = (it->what == IT_COMPOSITION
3463 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3464 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3465
3466 if (it->current.overlay_string_index >= 0)
3467 bufpos = IT_CHARPOS (*it);
3468 else
3469 bufpos = 0;
3470
3471 base_face_id = underlying_face_id (it);
3472
3473 /* Get the face for ASCII, or unibyte. */
3474 face_id = face_at_string_position (it->w,
3475 it->string,
3476 CHARPOS (pos),
3477 bufpos,
3478 it->region_beg_charpos,
3479 it->region_end_charpos,
3480 &next_check_charpos,
3481 base_face_id, 0);
3482
3483 /* Correct the face for charsets different from ASCII. Do it
3484 for the multibyte case only. The face returned above is
3485 suitable for unibyte text if IT->string is unibyte. */
3486 if (STRING_MULTIBYTE (it->string))
3487 {
3488 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3489 int rest = SBYTES (it->string) - BYTEPOS (pos);
3490 int c, len;
3491 struct face *face = FACE_FROM_ID (it->f, face_id);
3492
3493 c = string_char_and_length (p, rest, &len);
3494 face_id = FACE_FOR_CHAR (it->f, face, c);
3495 }
3496 }
3497 else
3498 {
3499 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3500 || (IT_CHARPOS (*it) <= BEGV && before_p))
3501 return it->face_id;
3502
3503 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3504 pos = it->current.pos;
3505
3506 if (before_p)
3507 DEC_TEXT_POS (pos, it->multibyte_p);
3508 else
3509 {
3510 if (it->what == IT_COMPOSITION)
3511 /* For composition, we must check the position after the
3512 composition. */
3513 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3514 else
3515 INC_TEXT_POS (pos, it->multibyte_p);
3516 }
3517
3518 /* Determine face for CHARSET_ASCII, or unibyte. */
3519 face_id = face_at_buffer_position (it->w,
3520 CHARPOS (pos),
3521 it->region_beg_charpos,
3522 it->region_end_charpos,
3523 &next_check_charpos,
3524 limit, 0);
3525
3526 /* Correct the face for charsets different from ASCII. Do it
3527 for the multibyte case only. The face returned above is
3528 suitable for unibyte text if current_buffer is unibyte. */
3529 if (it->multibyte_p)
3530 {
3531 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3532 struct face *face = FACE_FROM_ID (it->f, face_id);
3533 face_id = FACE_FOR_CHAR (it->f, face, c);
3534 }
3535 }
3536
3537 return face_id;
3538 }
3539
3540
3541 \f
3542 /***********************************************************************
3543 Invisible text
3544 ***********************************************************************/
3545
3546 /* Set up iterator IT from invisible properties at its current
3547 position. Called from handle_stop. */
3548
3549 static enum prop_handled
3550 handle_invisible_prop (it)
3551 struct it *it;
3552 {
3553 enum prop_handled handled = HANDLED_NORMALLY;
3554
3555 if (STRINGP (it->string))
3556 {
3557 extern Lisp_Object Qinvisible;
3558 Lisp_Object prop, end_charpos, limit, charpos;
3559
3560 /* Get the value of the invisible text property at the
3561 current position. Value will be nil if there is no such
3562 property. */
3563 charpos = make_number (IT_STRING_CHARPOS (*it));
3564 prop = Fget_text_property (charpos, Qinvisible, it->string);
3565
3566 if (!NILP (prop)
3567 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3568 {
3569 handled = HANDLED_RECOMPUTE_PROPS;
3570
3571 /* Get the position at which the next change of the
3572 invisible text property can be found in IT->string.
3573 Value will be nil if the property value is the same for
3574 all the rest of IT->string. */
3575 XSETINT (limit, SCHARS (it->string));
3576 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3577 it->string, limit);
3578
3579 /* Text at current position is invisible. The next
3580 change in the property is at position end_charpos.
3581 Move IT's current position to that position. */
3582 if (INTEGERP (end_charpos)
3583 && XFASTINT (end_charpos) < XFASTINT (limit))
3584 {
3585 struct text_pos old;
3586 old = it->current.string_pos;
3587 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3588 compute_string_pos (&it->current.string_pos, old, it->string);
3589 }
3590 else
3591 {
3592 /* The rest of the string is invisible. If this is an
3593 overlay string, proceed with the next overlay string
3594 or whatever comes and return a character from there. */
3595 if (it->current.overlay_string_index >= 0)
3596 {
3597 next_overlay_string (it);
3598 /* Don't check for overlay strings when we just
3599 finished processing them. */
3600 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3601 }
3602 else
3603 {
3604 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3605 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3606 }
3607 }
3608 }
3609 }
3610 else
3611 {
3612 int invis_p;
3613 EMACS_INT newpos, next_stop, start_charpos;
3614 Lisp_Object pos, prop, overlay;
3615
3616 /* First of all, is there invisible text at this position? */
3617 start_charpos = IT_CHARPOS (*it);
3618 pos = make_number (IT_CHARPOS (*it));
3619 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3620 &overlay);
3621 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3622
3623 /* If we are on invisible text, skip over it. */
3624 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3625 {
3626 /* Record whether we have to display an ellipsis for the
3627 invisible text. */
3628 int display_ellipsis_p = invis_p == 2;
3629
3630 handled = HANDLED_RECOMPUTE_PROPS;
3631
3632 /* Loop skipping over invisible text. The loop is left at
3633 ZV or with IT on the first char being visible again. */
3634 do
3635 {
3636 /* Try to skip some invisible text. Return value is the
3637 position reached which can be equal to IT's position
3638 if there is nothing invisible here. This skips both
3639 over invisible text properties and overlays with
3640 invisible property. */
3641 newpos = skip_invisible (IT_CHARPOS (*it),
3642 &next_stop, ZV, it->window);
3643
3644 /* If we skipped nothing at all we weren't at invisible
3645 text in the first place. If everything to the end of
3646 the buffer was skipped, end the loop. */
3647 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3648 invis_p = 0;
3649 else
3650 {
3651 /* We skipped some characters but not necessarily
3652 all there are. Check if we ended up on visible
3653 text. Fget_char_property returns the property of
3654 the char before the given position, i.e. if we
3655 get invis_p = 0, this means that the char at
3656 newpos is visible. */
3657 pos = make_number (newpos);
3658 prop = Fget_char_property (pos, Qinvisible, it->window);
3659 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3660 }
3661
3662 /* If we ended up on invisible text, proceed to
3663 skip starting with next_stop. */
3664 if (invis_p)
3665 IT_CHARPOS (*it) = next_stop;
3666
3667 /* If there are adjacent invisible texts, don't lose the
3668 second one's ellipsis. */
3669 if (invis_p == 2)
3670 display_ellipsis_p = 1;
3671 }
3672 while (invis_p);
3673
3674 /* The position newpos is now either ZV or on visible text. */
3675 IT_CHARPOS (*it) = newpos;
3676 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3677
3678 /* If there are before-strings at the start of invisible
3679 text, and the text is invisible because of a text
3680 property, arrange to show before-strings because 20.x did
3681 it that way. (If the text is invisible because of an
3682 overlay property instead of a text property, this is
3683 already handled in the overlay code.) */
3684 if (NILP (overlay)
3685 && get_overlay_strings (it, start_charpos))
3686 {
3687 handled = HANDLED_RECOMPUTE_PROPS;
3688 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3689 }
3690 else if (display_ellipsis_p)
3691 {
3692 /* Make sure that the glyphs of the ellipsis will get
3693 correct `charpos' values. If we would not update
3694 it->position here, the glyphs would belong to the
3695 last visible character _before_ the invisible
3696 text, which confuses `set_cursor_from_row'.
3697
3698 We use the last invisible position instead of the
3699 first because this way the cursor is always drawn on
3700 the first "." of the ellipsis, whenever PT is inside
3701 the invisible text. Otherwise the cursor would be
3702 placed _after_ the ellipsis when the point is after the
3703 first invisible character. */
3704 if (!STRINGP (it->object))
3705 {
3706 it->position.charpos = IT_CHARPOS (*it) - 1;
3707 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3708 }
3709 setup_for_ellipsis (it, 0);
3710 }
3711 }
3712 }
3713
3714 return handled;
3715 }
3716
3717
3718 /* Make iterator IT return `...' next.
3719 Replaces LEN characters from buffer. */
3720
3721 static void
3722 setup_for_ellipsis (it, len)
3723 struct it *it;
3724 int len;
3725 {
3726 /* Use the display table definition for `...'. Invalid glyphs
3727 will be handled by the method returning elements from dpvec. */
3728 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3729 {
3730 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3731 it->dpvec = v->contents;
3732 it->dpend = v->contents + v->size;
3733 }
3734 else
3735 {
3736 /* Default `...'. */
3737 it->dpvec = default_invis_vector;
3738 it->dpend = default_invis_vector + 3;
3739 }
3740
3741 it->dpvec_char_len = len;
3742 it->current.dpvec_index = 0;
3743 it->dpvec_face_id = -1;
3744
3745 /* Remember the current face id in case glyphs specify faces.
3746 IT's face is restored in set_iterator_to_next.
3747 saved_face_id was set to preceding char's face in handle_stop. */
3748 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3749 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3750
3751 it->method = GET_FROM_DISPLAY_VECTOR;
3752 it->ellipsis_p = 1;
3753 }
3754
3755
3756 \f
3757 /***********************************************************************
3758 'display' property
3759 ***********************************************************************/
3760
3761 /* Set up iterator IT from `display' property at its current position.
3762 Called from handle_stop.
3763 We return HANDLED_RETURN if some part of the display property
3764 overrides the display of the buffer text itself.
3765 Otherwise we return HANDLED_NORMALLY. */
3766
3767 static enum prop_handled
3768 handle_display_prop (it)
3769 struct it *it;
3770 {
3771 Lisp_Object prop, object;
3772 struct text_pos *position;
3773 /* Nonzero if some property replaces the display of the text itself. */
3774 int display_replaced_p = 0;
3775
3776 if (STRINGP (it->string))
3777 {
3778 object = it->string;
3779 position = &it->current.string_pos;
3780 }
3781 else
3782 {
3783 XSETWINDOW (object, it->w);
3784 position = &it->current.pos;
3785 }
3786
3787 /* Reset those iterator values set from display property values. */
3788 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3789 it->space_width = Qnil;
3790 it->font_height = Qnil;
3791 it->voffset = 0;
3792
3793 /* We don't support recursive `display' properties, i.e. string
3794 values that have a string `display' property, that have a string
3795 `display' property etc. */
3796 if (!it->string_from_display_prop_p)
3797 it->area = TEXT_AREA;
3798
3799 prop = Fget_char_property (make_number (position->charpos),
3800 Qdisplay, object);
3801 if (NILP (prop))
3802 return HANDLED_NORMALLY;
3803
3804 if (!STRINGP (it->string))
3805 object = it->w->buffer;
3806
3807 if (CONSP (prop)
3808 /* Simple properties. */
3809 && !EQ (XCAR (prop), Qimage)
3810 && !EQ (XCAR (prop), Qspace)
3811 && !EQ (XCAR (prop), Qwhen)
3812 && !EQ (XCAR (prop), Qslice)
3813 && !EQ (XCAR (prop), Qspace_width)
3814 && !EQ (XCAR (prop), Qheight)
3815 && !EQ (XCAR (prop), Qraise)
3816 /* Marginal area specifications. */
3817 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3818 && !EQ (XCAR (prop), Qleft_fringe)
3819 && !EQ (XCAR (prop), Qright_fringe)
3820 && !NILP (XCAR (prop)))
3821 {
3822 for (; CONSP (prop); prop = XCDR (prop))
3823 {
3824 if (handle_single_display_spec (it, XCAR (prop), object,
3825 position, display_replaced_p))
3826 display_replaced_p = 1;
3827 }
3828 }
3829 else if (VECTORP (prop))
3830 {
3831 int i;
3832 for (i = 0; i < ASIZE (prop); ++i)
3833 if (handle_single_display_spec (it, AREF (prop, i), object,
3834 position, display_replaced_p))
3835 display_replaced_p = 1;
3836 }
3837 else
3838 {
3839 int ret = handle_single_display_spec (it, prop, object, position, 0);
3840 if (ret < 0) /* Replaced by "", i.e. nothing. */
3841 return HANDLED_RECOMPUTE_PROPS;
3842 if (ret)
3843 display_replaced_p = 1;
3844 }
3845
3846 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3847 }
3848
3849
3850 /* Value is the position of the end of the `display' property starting
3851 at START_POS in OBJECT. */
3852
3853 static struct text_pos
3854 display_prop_end (it, object, start_pos)
3855 struct it *it;
3856 Lisp_Object object;
3857 struct text_pos start_pos;
3858 {
3859 Lisp_Object end;
3860 struct text_pos end_pos;
3861
3862 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3863 Qdisplay, object, Qnil);
3864 CHARPOS (end_pos) = XFASTINT (end);
3865 if (STRINGP (object))
3866 compute_string_pos (&end_pos, start_pos, it->string);
3867 else
3868 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3869
3870 return end_pos;
3871 }
3872
3873
3874 /* Set up IT from a single `display' specification PROP. OBJECT
3875 is the object in which the `display' property was found. *POSITION
3876 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3877 means that we previously saw a display specification which already
3878 replaced text display with something else, for example an image;
3879 we ignore such properties after the first one has been processed.
3880
3881 If PROP is a `space' or `image' specification, and in some other
3882 cases too, set *POSITION to the position where the `display'
3883 property ends.
3884
3885 Value is non-zero if something was found which replaces the display
3886 of buffer or string text. Specifically, the value is -1 if that
3887 "something" is "nothing". */
3888
3889 static int
3890 handle_single_display_spec (it, spec, object, position,
3891 display_replaced_before_p)
3892 struct it *it;
3893 Lisp_Object spec;
3894 Lisp_Object object;
3895 struct text_pos *position;
3896 int display_replaced_before_p;
3897 {
3898 Lisp_Object form;
3899 Lisp_Object location, value;
3900 struct text_pos start_pos, save_pos;
3901 int valid_p;
3902
3903 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3904 If the result is non-nil, use VALUE instead of SPEC. */
3905 form = Qt;
3906 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3907 {
3908 spec = XCDR (spec);
3909 if (!CONSP (spec))
3910 return 0;
3911 form = XCAR (spec);
3912 spec = XCDR (spec);
3913 }
3914
3915 if (!NILP (form) && !EQ (form, Qt))
3916 {
3917 int count = SPECPDL_INDEX ();
3918 struct gcpro gcpro1;
3919
3920 /* Bind `object' to the object having the `display' property, a
3921 buffer or string. Bind `position' to the position in the
3922 object where the property was found, and `buffer-position'
3923 to the current position in the buffer. */
3924 specbind (Qobject, object);
3925 specbind (Qposition, make_number (CHARPOS (*position)));
3926 specbind (Qbuffer_position,
3927 make_number (STRINGP (object)
3928 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3929 GCPRO1 (form);
3930 form = safe_eval (form);
3931 UNGCPRO;
3932 unbind_to (count, Qnil);
3933 }
3934
3935 if (NILP (form))
3936 return 0;
3937
3938 /* Handle `(height HEIGHT)' specifications. */
3939 if (CONSP (spec)
3940 && EQ (XCAR (spec), Qheight)
3941 && CONSP (XCDR (spec)))
3942 {
3943 if (!FRAME_WINDOW_P (it->f))
3944 return 0;
3945
3946 it->font_height = XCAR (XCDR (spec));
3947 if (!NILP (it->font_height))
3948 {
3949 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3950 int new_height = -1;
3951
3952 if (CONSP (it->font_height)
3953 && (EQ (XCAR (it->font_height), Qplus)
3954 || EQ (XCAR (it->font_height), Qminus))
3955 && CONSP (XCDR (it->font_height))
3956 && INTEGERP (XCAR (XCDR (it->font_height))))
3957 {
3958 /* `(+ N)' or `(- N)' where N is an integer. */
3959 int steps = XINT (XCAR (XCDR (it->font_height)));
3960 if (EQ (XCAR (it->font_height), Qplus))
3961 steps = - steps;
3962 it->face_id = smaller_face (it->f, it->face_id, steps);
3963 }
3964 else if (FUNCTIONP (it->font_height))
3965 {
3966 /* Call function with current height as argument.
3967 Value is the new height. */
3968 Lisp_Object height;
3969 height = safe_call1 (it->font_height,
3970 face->lface[LFACE_HEIGHT_INDEX]);
3971 if (NUMBERP (height))
3972 new_height = XFLOATINT (height);
3973 }
3974 else if (NUMBERP (it->font_height))
3975 {
3976 /* Value is a multiple of the canonical char height. */
3977 struct face *face;
3978
3979 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3980 new_height = (XFLOATINT (it->font_height)
3981 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3982 }
3983 else
3984 {
3985 /* Evaluate IT->font_height with `height' bound to the
3986 current specified height to get the new height. */
3987 int count = SPECPDL_INDEX ();
3988
3989 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3990 value = safe_eval (it->font_height);
3991 unbind_to (count, Qnil);
3992
3993 if (NUMBERP (value))
3994 new_height = XFLOATINT (value);
3995 }
3996
3997 if (new_height > 0)
3998 it->face_id = face_with_height (it->f, it->face_id, new_height);
3999 }
4000
4001 return 0;
4002 }
4003
4004 /* Handle `(space_width WIDTH)'. */
4005 if (CONSP (spec)
4006 && EQ (XCAR (spec), Qspace_width)
4007 && CONSP (XCDR (spec)))
4008 {
4009 if (!FRAME_WINDOW_P (it->f))
4010 return 0;
4011
4012 value = XCAR (XCDR (spec));
4013 if (NUMBERP (value) && XFLOATINT (value) > 0)
4014 it->space_width = value;
4015
4016 return 0;
4017 }
4018
4019 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4020 if (CONSP (spec)
4021 && EQ (XCAR (spec), Qslice))
4022 {
4023 Lisp_Object tem;
4024
4025 if (!FRAME_WINDOW_P (it->f))
4026 return 0;
4027
4028 if (tem = XCDR (spec), CONSP (tem))
4029 {
4030 it->slice.x = XCAR (tem);
4031 if (tem = XCDR (tem), CONSP (tem))
4032 {
4033 it->slice.y = XCAR (tem);
4034 if (tem = XCDR (tem), CONSP (tem))
4035 {
4036 it->slice.width = XCAR (tem);
4037 if (tem = XCDR (tem), CONSP (tem))
4038 it->slice.height = XCAR (tem);
4039 }
4040 }
4041 }
4042
4043 return 0;
4044 }
4045
4046 /* Handle `(raise FACTOR)'. */
4047 if (CONSP (spec)
4048 && EQ (XCAR (spec), Qraise)
4049 && CONSP (XCDR (spec)))
4050 {
4051 if (!FRAME_WINDOW_P (it->f))
4052 return 0;
4053
4054 #ifdef HAVE_WINDOW_SYSTEM
4055 value = XCAR (XCDR (spec));
4056 if (NUMBERP (value))
4057 {
4058 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4059 it->voffset = - (XFLOATINT (value)
4060 * (FONT_HEIGHT (face->font)));
4061 }
4062 #endif /* HAVE_WINDOW_SYSTEM */
4063
4064 return 0;
4065 }
4066
4067 /* Don't handle the other kinds of display specifications
4068 inside a string that we got from a `display' property. */
4069 if (it->string_from_display_prop_p)
4070 return 0;
4071
4072 /* Characters having this form of property are not displayed, so
4073 we have to find the end of the property. */
4074 start_pos = *position;
4075 *position = display_prop_end (it, object, start_pos);
4076 value = Qnil;
4077
4078 /* Stop the scan at that end position--we assume that all
4079 text properties change there. */
4080 it->stop_charpos = position->charpos;
4081
4082 /* Handle `(left-fringe BITMAP [FACE])'
4083 and `(right-fringe BITMAP [FACE])'. */
4084 if (CONSP (spec)
4085 && (EQ (XCAR (spec), Qleft_fringe)
4086 || EQ (XCAR (spec), Qright_fringe))
4087 && CONSP (XCDR (spec)))
4088 {
4089 int face_id = DEFAULT_FACE_ID;
4090 int fringe_bitmap;
4091
4092 if (!FRAME_WINDOW_P (it->f))
4093 /* If we return here, POSITION has been advanced
4094 across the text with this property. */
4095 return 0;
4096
4097 #ifdef HAVE_WINDOW_SYSTEM
4098 value = XCAR (XCDR (spec));
4099 if (!SYMBOLP (value)
4100 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4101 /* If we return here, POSITION has been advanced
4102 across the text with this property. */
4103 return 0;
4104
4105 if (CONSP (XCDR (XCDR (spec))))
4106 {
4107 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4108 int face_id2 = lookup_derived_face (it->f, face_name,
4109 'A', FRINGE_FACE_ID, 0);
4110 if (face_id2 >= 0)
4111 face_id = face_id2;
4112 }
4113
4114 /* Save current settings of IT so that we can restore them
4115 when we are finished with the glyph property value. */
4116
4117 save_pos = it->position;
4118 it->position = *position;
4119 push_it (it);
4120 it->position = save_pos;
4121
4122 it->area = TEXT_AREA;
4123 it->what = IT_IMAGE;
4124 it->image_id = -1; /* no image */
4125 it->position = start_pos;
4126 it->object = NILP (object) ? it->w->buffer : object;
4127 it->method = GET_FROM_IMAGE;
4128 it->face_id = face_id;
4129
4130 /* Say that we haven't consumed the characters with
4131 `display' property yet. The call to pop_it in
4132 set_iterator_to_next will clean this up. */
4133 *position = start_pos;
4134
4135 if (EQ (XCAR (spec), Qleft_fringe))
4136 {
4137 it->left_user_fringe_bitmap = fringe_bitmap;
4138 it->left_user_fringe_face_id = face_id;
4139 }
4140 else
4141 {
4142 it->right_user_fringe_bitmap = fringe_bitmap;
4143 it->right_user_fringe_face_id = face_id;
4144 }
4145 #endif /* HAVE_WINDOW_SYSTEM */
4146 return 1;
4147 }
4148
4149 /* Prepare to handle `((margin left-margin) ...)',
4150 `((margin right-margin) ...)' and `((margin nil) ...)'
4151 prefixes for display specifications. */
4152 location = Qunbound;
4153 if (CONSP (spec) && CONSP (XCAR (spec)))
4154 {
4155 Lisp_Object tem;
4156
4157 value = XCDR (spec);
4158 if (CONSP (value))
4159 value = XCAR (value);
4160
4161 tem = XCAR (spec);
4162 if (EQ (XCAR (tem), Qmargin)
4163 && (tem = XCDR (tem),
4164 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4165 (NILP (tem)
4166 || EQ (tem, Qleft_margin)
4167 || EQ (tem, Qright_margin))))
4168 location = tem;
4169 }
4170
4171 if (EQ (location, Qunbound))
4172 {
4173 location = Qnil;
4174 value = spec;
4175 }
4176
4177 /* After this point, VALUE is the property after any
4178 margin prefix has been stripped. It must be a string,
4179 an image specification, or `(space ...)'.
4180
4181 LOCATION specifies where to display: `left-margin',
4182 `right-margin' or nil. */
4183
4184 valid_p = (STRINGP (value)
4185 #ifdef HAVE_WINDOW_SYSTEM
4186 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4187 #endif /* not HAVE_WINDOW_SYSTEM */
4188 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4189
4190 if (valid_p && !display_replaced_before_p)
4191 {
4192 /* Save current settings of IT so that we can restore them
4193 when we are finished with the glyph property value. */
4194 save_pos = it->position;
4195 it->position = *position;
4196 push_it (it);
4197 it->position = save_pos;
4198
4199 if (NILP (location))
4200 it->area = TEXT_AREA;
4201 else if (EQ (location, Qleft_margin))
4202 it->area = LEFT_MARGIN_AREA;
4203 else
4204 it->area = RIGHT_MARGIN_AREA;
4205
4206 if (STRINGP (value))
4207 {
4208 if (SCHARS (value) == 0)
4209 {
4210 pop_it (it);
4211 return -1; /* Replaced by "", i.e. nothing. */
4212 }
4213 it->string = value;
4214 it->multibyte_p = STRING_MULTIBYTE (it->string);
4215 it->current.overlay_string_index = -1;
4216 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4217 it->end_charpos = it->string_nchars = SCHARS (it->string);
4218 it->method = GET_FROM_STRING;
4219 it->stop_charpos = 0;
4220 it->string_from_display_prop_p = 1;
4221 /* Say that we haven't consumed the characters with
4222 `display' property yet. The call to pop_it in
4223 set_iterator_to_next will clean this up. */
4224 *position = start_pos;
4225 }
4226 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4227 {
4228 it->method = GET_FROM_STRETCH;
4229 it->object = value;
4230 *position = it->position = start_pos;
4231 }
4232 #ifdef HAVE_WINDOW_SYSTEM
4233 else
4234 {
4235 it->what = IT_IMAGE;
4236 it->image_id = lookup_image (it->f, value);
4237 it->position = start_pos;
4238 it->object = NILP (object) ? it->w->buffer : object;
4239 it->method = GET_FROM_IMAGE;
4240
4241 /* Say that we haven't consumed the characters with
4242 `display' property yet. The call to pop_it in
4243 set_iterator_to_next will clean this up. */
4244 *position = start_pos;
4245 }
4246 #endif /* HAVE_WINDOW_SYSTEM */
4247
4248 return 1;
4249 }
4250
4251 /* Invalid property or property not supported. Restore
4252 POSITION to what it was before. */
4253 *position = start_pos;
4254 return 0;
4255 }
4256
4257
4258 /* Check if SPEC is a display sub-property value whose text should be
4259 treated as intangible. */
4260
4261 static int
4262 single_display_spec_intangible_p (prop)
4263 Lisp_Object prop;
4264 {
4265 /* Skip over `when FORM'. */
4266 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4267 {
4268 prop = XCDR (prop);
4269 if (!CONSP (prop))
4270 return 0;
4271 prop = XCDR (prop);
4272 }
4273
4274 if (STRINGP (prop))
4275 return 1;
4276
4277 if (!CONSP (prop))
4278 return 0;
4279
4280 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4281 we don't need to treat text as intangible. */
4282 if (EQ (XCAR (prop), Qmargin))
4283 {
4284 prop = XCDR (prop);
4285 if (!CONSP (prop))
4286 return 0;
4287
4288 prop = XCDR (prop);
4289 if (!CONSP (prop)
4290 || EQ (XCAR (prop), Qleft_margin)
4291 || EQ (XCAR (prop), Qright_margin))
4292 return 0;
4293 }
4294
4295 return (CONSP (prop)
4296 && (EQ (XCAR (prop), Qimage)
4297 || EQ (XCAR (prop), Qspace)));
4298 }
4299
4300
4301 /* Check if PROP is a display property value whose text should be
4302 treated as intangible. */
4303
4304 int
4305 display_prop_intangible_p (prop)
4306 Lisp_Object prop;
4307 {
4308 if (CONSP (prop)
4309 && CONSP (XCAR (prop))
4310 && !EQ (Qmargin, XCAR (XCAR (prop))))
4311 {
4312 /* A list of sub-properties. */
4313 while (CONSP (prop))
4314 {
4315 if (single_display_spec_intangible_p (XCAR (prop)))
4316 return 1;
4317 prop = XCDR (prop);
4318 }
4319 }
4320 else if (VECTORP (prop))
4321 {
4322 /* A vector of sub-properties. */
4323 int i;
4324 for (i = 0; i < ASIZE (prop); ++i)
4325 if (single_display_spec_intangible_p (AREF (prop, i)))
4326 return 1;
4327 }
4328 else
4329 return single_display_spec_intangible_p (prop);
4330
4331 return 0;
4332 }
4333
4334
4335 /* Return 1 if PROP is a display sub-property value containing STRING. */
4336
4337 static int
4338 single_display_spec_string_p (prop, string)
4339 Lisp_Object prop, string;
4340 {
4341 if (EQ (string, prop))
4342 return 1;
4343
4344 /* Skip over `when FORM'. */
4345 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4346 {
4347 prop = XCDR (prop);
4348 if (!CONSP (prop))
4349 return 0;
4350 prop = XCDR (prop);
4351 }
4352
4353 if (CONSP (prop))
4354 /* Skip over `margin LOCATION'. */
4355 if (EQ (XCAR (prop), Qmargin))
4356 {
4357 prop = XCDR (prop);
4358 if (!CONSP (prop))
4359 return 0;
4360
4361 prop = XCDR (prop);
4362 if (!CONSP (prop))
4363 return 0;
4364 }
4365
4366 return CONSP (prop) && EQ (XCAR (prop), string);
4367 }
4368
4369
4370 /* Return 1 if STRING appears in the `display' property PROP. */
4371
4372 static int
4373 display_prop_string_p (prop, string)
4374 Lisp_Object prop, string;
4375 {
4376 if (CONSP (prop)
4377 && CONSP (XCAR (prop))
4378 && !EQ (Qmargin, XCAR (XCAR (prop))))
4379 {
4380 /* A list of sub-properties. */
4381 while (CONSP (prop))
4382 {
4383 if (single_display_spec_string_p (XCAR (prop), string))
4384 return 1;
4385 prop = XCDR (prop);
4386 }
4387 }
4388 else if (VECTORP (prop))
4389 {
4390 /* A vector of sub-properties. */
4391 int i;
4392 for (i = 0; i < ASIZE (prop); ++i)
4393 if (single_display_spec_string_p (AREF (prop, i), string))
4394 return 1;
4395 }
4396 else
4397 return single_display_spec_string_p (prop, string);
4398
4399 return 0;
4400 }
4401
4402
4403 /* Determine from which buffer position in W's buffer STRING comes
4404 from. AROUND_CHARPOS is an approximate position where it could
4405 be from. Value is the buffer position or 0 if it couldn't be
4406 determined.
4407
4408 W's buffer must be current.
4409
4410 This function is necessary because we don't record buffer positions
4411 in glyphs generated from strings (to keep struct glyph small).
4412 This function may only use code that doesn't eval because it is
4413 called asynchronously from note_mouse_highlight. */
4414
4415 int
4416 string_buffer_position (w, string, around_charpos)
4417 struct window *w;
4418 Lisp_Object string;
4419 int around_charpos;
4420 {
4421 Lisp_Object limit, prop, pos;
4422 const int MAX_DISTANCE = 1000;
4423 int found = 0;
4424
4425 pos = make_number (around_charpos);
4426 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4427 while (!found && !EQ (pos, limit))
4428 {
4429 prop = Fget_char_property (pos, Qdisplay, Qnil);
4430 if (!NILP (prop) && display_prop_string_p (prop, string))
4431 found = 1;
4432 else
4433 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4434 }
4435
4436 if (!found)
4437 {
4438 pos = make_number (around_charpos);
4439 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4440 while (!found && !EQ (pos, limit))
4441 {
4442 prop = Fget_char_property (pos, Qdisplay, Qnil);
4443 if (!NILP (prop) && display_prop_string_p (prop, string))
4444 found = 1;
4445 else
4446 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4447 limit);
4448 }
4449 }
4450
4451 return found ? XINT (pos) : 0;
4452 }
4453
4454
4455 \f
4456 /***********************************************************************
4457 `composition' property
4458 ***********************************************************************/
4459
4460 /* Set up iterator IT from `composition' property at its current
4461 position. Called from handle_stop. */
4462
4463 static enum prop_handled
4464 handle_composition_prop (it)
4465 struct it *it;
4466 {
4467 Lisp_Object prop, string;
4468 int pos, pos_byte, end;
4469 enum prop_handled handled = HANDLED_NORMALLY;
4470
4471 if (STRINGP (it->string))
4472 {
4473 pos = IT_STRING_CHARPOS (*it);
4474 pos_byte = IT_STRING_BYTEPOS (*it);
4475 string = it->string;
4476 }
4477 else
4478 {
4479 pos = IT_CHARPOS (*it);
4480 pos_byte = IT_BYTEPOS (*it);
4481 string = Qnil;
4482 }
4483
4484 /* If there's a valid composition and point is not inside of the
4485 composition (in the case that the composition is from the current
4486 buffer), draw a glyph composed from the composition components. */
4487 if (find_composition (pos, -1, &pos, &end, &prop, string)
4488 && COMPOSITION_VALID_P (pos, end, prop)
4489 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4490 {
4491 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4492
4493 if (id >= 0)
4494 {
4495 struct composition *cmp = composition_table[id];
4496
4497 if (cmp->glyph_len == 0)
4498 {
4499 /* No glyph. */
4500 if (STRINGP (it->string))
4501 {
4502 IT_STRING_CHARPOS (*it) = end;
4503 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4504 end);
4505 }
4506 else
4507 {
4508 IT_CHARPOS (*it) = end;
4509 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4510 }
4511 return HANDLED_RECOMPUTE_PROPS;
4512 }
4513
4514 it->stop_charpos = end;
4515 push_it (it);
4516
4517 it->method = GET_FROM_COMPOSITION;
4518 it->cmp_id = id;
4519 it->cmp_len = COMPOSITION_LENGTH (prop);
4520 /* For a terminal, draw only the first character of the
4521 components. */
4522 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4523 it->len = (STRINGP (it->string)
4524 ? string_char_to_byte (it->string, end)
4525 : CHAR_TO_BYTE (end)) - pos_byte;
4526 handled = HANDLED_RETURN;
4527 }
4528 }
4529
4530 return handled;
4531 }
4532
4533
4534 \f
4535 /***********************************************************************
4536 Overlay strings
4537 ***********************************************************************/
4538
4539 /* The following structure is used to record overlay strings for
4540 later sorting in load_overlay_strings. */
4541
4542 struct overlay_entry
4543 {
4544 Lisp_Object overlay;
4545 Lisp_Object string;
4546 int priority;
4547 int after_string_p;
4548 };
4549
4550
4551 /* Set up iterator IT from overlay strings at its current position.
4552 Called from handle_stop. */
4553
4554 static enum prop_handled
4555 handle_overlay_change (it)
4556 struct it *it;
4557 {
4558 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4559 return HANDLED_RECOMPUTE_PROPS;
4560 else
4561 return HANDLED_NORMALLY;
4562 }
4563
4564
4565 /* Set up the next overlay string for delivery by IT, if there is an
4566 overlay string to deliver. Called by set_iterator_to_next when the
4567 end of the current overlay string is reached. If there are more
4568 overlay strings to display, IT->string and
4569 IT->current.overlay_string_index are set appropriately here.
4570 Otherwise IT->string is set to nil. */
4571
4572 static void
4573 next_overlay_string (it)
4574 struct it *it;
4575 {
4576 ++it->current.overlay_string_index;
4577 if (it->current.overlay_string_index == it->n_overlay_strings)
4578 {
4579 /* No more overlay strings. Restore IT's settings to what
4580 they were before overlay strings were processed, and
4581 continue to deliver from current_buffer. */
4582 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4583
4584 pop_it (it);
4585 xassert (it->sp > 0
4586 || it->method == GET_FROM_COMPOSITION
4587 || (NILP (it->string)
4588 && it->method == GET_FROM_BUFFER
4589 && it->stop_charpos >= BEGV
4590 && it->stop_charpos <= it->end_charpos));
4591 it->current.overlay_string_index = -1;
4592 it->n_overlay_strings = 0;
4593
4594 /* If we're at the end of the buffer, record that we have
4595 processed the overlay strings there already, so that
4596 next_element_from_buffer doesn't try it again. */
4597 if (IT_CHARPOS (*it) >= it->end_charpos)
4598 it->overlay_strings_at_end_processed_p = 1;
4599
4600 /* If we have to display `...' for invisible text, set
4601 the iterator up for that. */
4602 if (display_ellipsis_p)
4603 setup_for_ellipsis (it, 0);
4604 }
4605 else
4606 {
4607 /* There are more overlay strings to process. If
4608 IT->current.overlay_string_index has advanced to a position
4609 where we must load IT->overlay_strings with more strings, do
4610 it. */
4611 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4612
4613 if (it->current.overlay_string_index && i == 0)
4614 load_overlay_strings (it, 0);
4615
4616 /* Initialize IT to deliver display elements from the overlay
4617 string. */
4618 it->string = it->overlay_strings[i];
4619 it->multibyte_p = STRING_MULTIBYTE (it->string);
4620 SET_TEXT_POS (it->current.string_pos, 0, 0);
4621 it->method = GET_FROM_STRING;
4622 it->stop_charpos = 0;
4623 }
4624
4625 CHECK_IT (it);
4626 }
4627
4628
4629 /* Compare two overlay_entry structures E1 and E2. Used as a
4630 comparison function for qsort in load_overlay_strings. Overlay
4631 strings for the same position are sorted so that
4632
4633 1. All after-strings come in front of before-strings, except
4634 when they come from the same overlay.
4635
4636 2. Within after-strings, strings are sorted so that overlay strings
4637 from overlays with higher priorities come first.
4638
4639 2. Within before-strings, strings are sorted so that overlay
4640 strings from overlays with higher priorities come last.
4641
4642 Value is analogous to strcmp. */
4643
4644
4645 static int
4646 compare_overlay_entries (e1, e2)
4647 void *e1, *e2;
4648 {
4649 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4650 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4651 int result;
4652
4653 if (entry1->after_string_p != entry2->after_string_p)
4654 {
4655 /* Let after-strings appear in front of before-strings if
4656 they come from different overlays. */
4657 if (EQ (entry1->overlay, entry2->overlay))
4658 result = entry1->after_string_p ? 1 : -1;
4659 else
4660 result = entry1->after_string_p ? -1 : 1;
4661 }
4662 else if (entry1->after_string_p)
4663 /* After-strings sorted in order of decreasing priority. */
4664 result = entry2->priority - entry1->priority;
4665 else
4666 /* Before-strings sorted in order of increasing priority. */
4667 result = entry1->priority - entry2->priority;
4668
4669 return result;
4670 }
4671
4672
4673 /* Load the vector IT->overlay_strings with overlay strings from IT's
4674 current buffer position, or from CHARPOS if that is > 0. Set
4675 IT->n_overlays to the total number of overlay strings found.
4676
4677 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4678 a time. On entry into load_overlay_strings,
4679 IT->current.overlay_string_index gives the number of overlay
4680 strings that have already been loaded by previous calls to this
4681 function.
4682
4683 IT->add_overlay_start contains an additional overlay start
4684 position to consider for taking overlay strings from, if non-zero.
4685 This position comes into play when the overlay has an `invisible'
4686 property, and both before and after-strings. When we've skipped to
4687 the end of the overlay, because of its `invisible' property, we
4688 nevertheless want its before-string to appear.
4689 IT->add_overlay_start will contain the overlay start position
4690 in this case.
4691
4692 Overlay strings are sorted so that after-string strings come in
4693 front of before-string strings. Within before and after-strings,
4694 strings are sorted by overlay priority. See also function
4695 compare_overlay_entries. */
4696
4697 static void
4698 load_overlay_strings (it, charpos)
4699 struct it *it;
4700 int charpos;
4701 {
4702 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4703 Lisp_Object overlay, window, str, invisible;
4704 struct Lisp_Overlay *ov;
4705 int start, end;
4706 int size = 20;
4707 int n = 0, i, j, invis_p;
4708 struct overlay_entry *entries
4709 = (struct overlay_entry *) alloca (size * sizeof *entries);
4710
4711 if (charpos <= 0)
4712 charpos = IT_CHARPOS (*it);
4713
4714 /* Append the overlay string STRING of overlay OVERLAY to vector
4715 `entries' which has size `size' and currently contains `n'
4716 elements. AFTER_P non-zero means STRING is an after-string of
4717 OVERLAY. */
4718 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4719 do \
4720 { \
4721 Lisp_Object priority; \
4722 \
4723 if (n == size) \
4724 { \
4725 int new_size = 2 * size; \
4726 struct overlay_entry *old = entries; \
4727 entries = \
4728 (struct overlay_entry *) alloca (new_size \
4729 * sizeof *entries); \
4730 bcopy (old, entries, size * sizeof *entries); \
4731 size = new_size; \
4732 } \
4733 \
4734 entries[n].string = (STRING); \
4735 entries[n].overlay = (OVERLAY); \
4736 priority = Foverlay_get ((OVERLAY), Qpriority); \
4737 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4738 entries[n].after_string_p = (AFTER_P); \
4739 ++n; \
4740 } \
4741 while (0)
4742
4743 /* Process overlay before the overlay center. */
4744 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4745 {
4746 XSETMISC (overlay, ov);
4747 xassert (OVERLAYP (overlay));
4748 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4749 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4750
4751 if (end < charpos)
4752 break;
4753
4754 /* Skip this overlay if it doesn't start or end at IT's current
4755 position. */
4756 if (end != charpos && start != charpos)
4757 continue;
4758
4759 /* Skip this overlay if it doesn't apply to IT->w. */
4760 window = Foverlay_get (overlay, Qwindow);
4761 if (WINDOWP (window) && XWINDOW (window) != it->w)
4762 continue;
4763
4764 /* If the text ``under'' the overlay is invisible, both before-
4765 and after-strings from this overlay are visible; start and
4766 end position are indistinguishable. */
4767 invisible = Foverlay_get (overlay, Qinvisible);
4768 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4769
4770 /* If overlay has a non-empty before-string, record it. */
4771 if ((start == charpos || (end == charpos && invis_p))
4772 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4773 && SCHARS (str))
4774 RECORD_OVERLAY_STRING (overlay, str, 0);
4775
4776 /* If overlay has a non-empty after-string, record it. */
4777 if ((end == charpos || (start == charpos && invis_p))
4778 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4779 && SCHARS (str))
4780 RECORD_OVERLAY_STRING (overlay, str, 1);
4781 }
4782
4783 /* Process overlays after the overlay center. */
4784 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4785 {
4786 XSETMISC (overlay, ov);
4787 xassert (OVERLAYP (overlay));
4788 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4789 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4790
4791 if (start > charpos)
4792 break;
4793
4794 /* Skip this overlay if it doesn't start or end at IT's current
4795 position. */
4796 if (end != charpos && start != charpos)
4797 continue;
4798
4799 /* Skip this overlay if it doesn't apply to IT->w. */
4800 window = Foverlay_get (overlay, Qwindow);
4801 if (WINDOWP (window) && XWINDOW (window) != it->w)
4802 continue;
4803
4804 /* If the text ``under'' the overlay is invisible, it has a zero
4805 dimension, and both before- and after-strings apply. */
4806 invisible = Foverlay_get (overlay, Qinvisible);
4807 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4808
4809 /* If overlay has a non-empty before-string, record it. */
4810 if ((start == charpos || (end == charpos && invis_p))
4811 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4812 && SCHARS (str))
4813 RECORD_OVERLAY_STRING (overlay, str, 0);
4814
4815 /* If overlay has a non-empty after-string, record it. */
4816 if ((end == charpos || (start == charpos && invis_p))
4817 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4818 && SCHARS (str))
4819 RECORD_OVERLAY_STRING (overlay, str, 1);
4820 }
4821
4822 #undef RECORD_OVERLAY_STRING
4823
4824 /* Sort entries. */
4825 if (n > 1)
4826 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4827
4828 /* Record the total number of strings to process. */
4829 it->n_overlay_strings = n;
4830
4831 /* IT->current.overlay_string_index is the number of overlay strings
4832 that have already been consumed by IT. Copy some of the
4833 remaining overlay strings to IT->overlay_strings. */
4834 i = 0;
4835 j = it->current.overlay_string_index;
4836 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4837 it->overlay_strings[i++] = entries[j++].string;
4838
4839 CHECK_IT (it);
4840 }
4841
4842
4843 /* Get the first chunk of overlay strings at IT's current buffer
4844 position, or at CHARPOS if that is > 0. Value is non-zero if at
4845 least one overlay string was found. */
4846
4847 static int
4848 get_overlay_strings_1 (it, charpos, compute_stop_p)
4849 struct it *it;
4850 int charpos;
4851 {
4852 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4853 process. This fills IT->overlay_strings with strings, and sets
4854 IT->n_overlay_strings to the total number of strings to process.
4855 IT->pos.overlay_string_index has to be set temporarily to zero
4856 because load_overlay_strings needs this; it must be set to -1
4857 when no overlay strings are found because a zero value would
4858 indicate a position in the first overlay string. */
4859 it->current.overlay_string_index = 0;
4860 load_overlay_strings (it, charpos);
4861
4862 /* If we found overlay strings, set up IT to deliver display
4863 elements from the first one. Otherwise set up IT to deliver
4864 from current_buffer. */
4865 if (it->n_overlay_strings)
4866 {
4867 /* Make sure we know settings in current_buffer, so that we can
4868 restore meaningful values when we're done with the overlay
4869 strings. */
4870 if (compute_stop_p)
4871 compute_stop_pos (it);
4872 xassert (it->face_id >= 0);
4873
4874 /* Save IT's settings. They are restored after all overlay
4875 strings have been processed. */
4876 xassert (!compute_stop_p || it->sp == 0);
4877 push_it (it);
4878
4879 /* Set up IT to deliver display elements from the first overlay
4880 string. */
4881 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4882 it->string = it->overlay_strings[0];
4883 it->stop_charpos = 0;
4884 xassert (STRINGP (it->string));
4885 it->end_charpos = SCHARS (it->string);
4886 it->multibyte_p = STRING_MULTIBYTE (it->string);
4887 it->method = GET_FROM_STRING;
4888 return 1;
4889 }
4890
4891 it->current.overlay_string_index = -1;
4892 return 0;
4893 }
4894
4895 static int
4896 get_overlay_strings (it, charpos)
4897 struct it *it;
4898 int charpos;
4899 {
4900 it->string = Qnil;
4901 it->method = GET_FROM_BUFFER;
4902
4903 (void) get_overlay_strings_1 (it, charpos, 1);
4904
4905 CHECK_IT (it);
4906
4907 /* Value is non-zero if we found at least one overlay string. */
4908 return STRINGP (it->string);
4909 }
4910
4911
4912 \f
4913 /***********************************************************************
4914 Saving and restoring state
4915 ***********************************************************************/
4916
4917 /* Save current settings of IT on IT->stack. Called, for example,
4918 before setting up IT for an overlay string, to be able to restore
4919 IT's settings to what they were after the overlay string has been
4920 processed. */
4921
4922 static void
4923 push_it (it)
4924 struct it *it;
4925 {
4926 struct iterator_stack_entry *p;
4927
4928 xassert (it->sp < IT_STACK_SIZE);
4929 p = it->stack + it->sp;
4930
4931 p->stop_charpos = it->stop_charpos;
4932 xassert (it->face_id >= 0);
4933 p->face_id = it->face_id;
4934 p->string = it->string;
4935 p->method = it->method;
4936 switch (p->method)
4937 {
4938 case GET_FROM_IMAGE:
4939 p->u.image.object = it->object;
4940 p->u.image.image_id = it->image_id;
4941 p->u.image.slice = it->slice;
4942 break;
4943 case GET_FROM_COMPOSITION:
4944 p->u.comp.object = it->object;
4945 p->u.comp.c = it->c;
4946 p->u.comp.len = it->len;
4947 p->u.comp.cmp_id = it->cmp_id;
4948 p->u.comp.cmp_len = it->cmp_len;
4949 break;
4950 case GET_FROM_STRETCH:
4951 p->u.stretch.object = it->object;
4952 break;
4953 }
4954 p->position = it->position;
4955 p->current = it->current;
4956 p->end_charpos = it->end_charpos;
4957 p->string_nchars = it->string_nchars;
4958 p->area = it->area;
4959 p->multibyte_p = it->multibyte_p;
4960 p->space_width = it->space_width;
4961 p->font_height = it->font_height;
4962 p->voffset = it->voffset;
4963 p->string_from_display_prop_p = it->string_from_display_prop_p;
4964 p->display_ellipsis_p = 0;
4965 ++it->sp;
4966 }
4967
4968
4969 /* Restore IT's settings from IT->stack. Called, for example, when no
4970 more overlay strings must be processed, and we return to delivering
4971 display elements from a buffer, or when the end of a string from a
4972 `display' property is reached and we return to delivering display
4973 elements from an overlay string, or from a buffer. */
4974
4975 static void
4976 pop_it (it)
4977 struct it *it;
4978 {
4979 struct iterator_stack_entry *p;
4980
4981 xassert (it->sp > 0);
4982 --it->sp;
4983 p = it->stack + it->sp;
4984 it->stop_charpos = p->stop_charpos;
4985 it->face_id = p->face_id;
4986 it->current = p->current;
4987 it->position = p->position;
4988 it->string = p->string;
4989 if (NILP (it->string))
4990 SET_TEXT_POS (it->current.string_pos, -1, -1);
4991 it->method = p->method;
4992 switch (it->method)
4993 {
4994 case GET_FROM_IMAGE:
4995 it->image_id = p->u.image.image_id;
4996 it->object = p->u.image.object;
4997 it->slice = p->u.image.slice;
4998 break;
4999 case GET_FROM_COMPOSITION:
5000 it->object = p->u.comp.object;
5001 it->c = p->u.comp.c;
5002 it->len = p->u.comp.len;
5003 it->cmp_id = p->u.comp.cmp_id;
5004 it->cmp_len = p->u.comp.cmp_len;
5005 break;
5006 case GET_FROM_STRETCH:
5007 it->object = p->u.comp.object;
5008 break;
5009 case GET_FROM_BUFFER:
5010 it->object = it->w->buffer;
5011 break;
5012 case GET_FROM_STRING:
5013 it->object = it->string;
5014 break;
5015 }
5016 it->end_charpos = p->end_charpos;
5017 it->string_nchars = p->string_nchars;
5018 it->area = p->area;
5019 it->multibyte_p = p->multibyte_p;
5020 it->space_width = p->space_width;
5021 it->font_height = p->font_height;
5022 it->voffset = p->voffset;
5023 it->string_from_display_prop_p = p->string_from_display_prop_p;
5024 }
5025
5026
5027 \f
5028 /***********************************************************************
5029 Moving over lines
5030 ***********************************************************************/
5031
5032 /* Set IT's current position to the previous line start. */
5033
5034 static void
5035 back_to_previous_line_start (it)
5036 struct it *it;
5037 {
5038 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5039 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5040 }
5041
5042
5043 /* Move IT to the next line start.
5044
5045 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5046 we skipped over part of the text (as opposed to moving the iterator
5047 continuously over the text). Otherwise, don't change the value
5048 of *SKIPPED_P.
5049
5050 Newlines may come from buffer text, overlay strings, or strings
5051 displayed via the `display' property. That's the reason we can't
5052 simply use find_next_newline_no_quit.
5053
5054 Note that this function may not skip over invisible text that is so
5055 because of text properties and immediately follows a newline. If
5056 it would, function reseat_at_next_visible_line_start, when called
5057 from set_iterator_to_next, would effectively make invisible
5058 characters following a newline part of the wrong glyph row, which
5059 leads to wrong cursor motion. */
5060
5061 static int
5062 forward_to_next_line_start (it, skipped_p)
5063 struct it *it;
5064 int *skipped_p;
5065 {
5066 int old_selective, newline_found_p, n;
5067 const int MAX_NEWLINE_DISTANCE = 500;
5068
5069 /* If already on a newline, just consume it to avoid unintended
5070 skipping over invisible text below. */
5071 if (it->what == IT_CHARACTER
5072 && it->c == '\n'
5073 && CHARPOS (it->position) == IT_CHARPOS (*it))
5074 {
5075 set_iterator_to_next (it, 0);
5076 it->c = 0;
5077 return 1;
5078 }
5079
5080 /* Don't handle selective display in the following. It's (a)
5081 unnecessary because it's done by the caller, and (b) leads to an
5082 infinite recursion because next_element_from_ellipsis indirectly
5083 calls this function. */
5084 old_selective = it->selective;
5085 it->selective = 0;
5086
5087 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5088 from buffer text. */
5089 for (n = newline_found_p = 0;
5090 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5091 n += STRINGP (it->string) ? 0 : 1)
5092 {
5093 if (!get_next_display_element (it))
5094 return 0;
5095 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5096 set_iterator_to_next (it, 0);
5097 }
5098
5099 /* If we didn't find a newline near enough, see if we can use a
5100 short-cut. */
5101 if (!newline_found_p)
5102 {
5103 int start = IT_CHARPOS (*it);
5104 int limit = find_next_newline_no_quit (start, 1);
5105 Lisp_Object pos;
5106
5107 xassert (!STRINGP (it->string));
5108
5109 /* If there isn't any `display' property in sight, and no
5110 overlays, we can just use the position of the newline in
5111 buffer text. */
5112 if (it->stop_charpos >= limit
5113 || ((pos = Fnext_single_property_change (make_number (start),
5114 Qdisplay,
5115 Qnil, make_number (limit)),
5116 NILP (pos))
5117 && next_overlay_change (start) == ZV))
5118 {
5119 IT_CHARPOS (*it) = limit;
5120 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5121 *skipped_p = newline_found_p = 1;
5122 }
5123 else
5124 {
5125 while (get_next_display_element (it)
5126 && !newline_found_p)
5127 {
5128 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5129 set_iterator_to_next (it, 0);
5130 }
5131 }
5132 }
5133
5134 it->selective = old_selective;
5135 return newline_found_p;
5136 }
5137
5138
5139 /* Set IT's current position to the previous visible line start. Skip
5140 invisible text that is so either due to text properties or due to
5141 selective display. Caution: this does not change IT->current_x and
5142 IT->hpos. */
5143
5144 static void
5145 back_to_previous_visible_line_start (it)
5146 struct it *it;
5147 {
5148 while (IT_CHARPOS (*it) > BEGV)
5149 {
5150 back_to_previous_line_start (it);
5151
5152 if (IT_CHARPOS (*it) <= BEGV)
5153 break;
5154
5155 /* If selective > 0, then lines indented more than that values
5156 are invisible. */
5157 if (it->selective > 0
5158 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5159 (double) it->selective)) /* iftc */
5160 continue;
5161
5162 /* Check the newline before point for invisibility. */
5163 {
5164 Lisp_Object prop;
5165 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5166 Qinvisible, it->window);
5167 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5168 continue;
5169 }
5170
5171 if (IT_CHARPOS (*it) <= BEGV)
5172 break;
5173
5174 {
5175 struct it it2;
5176 int pos;
5177 int beg, end;
5178 Lisp_Object val, overlay;
5179
5180 /* If newline is part of a composition, continue from start of composition */
5181 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5182 && beg < IT_CHARPOS (*it))
5183 goto replaced;
5184
5185 /* If newline is replaced by a display property, find start of overlay
5186 or interval and continue search from that point. */
5187 it2 = *it;
5188 pos = --IT_CHARPOS (it2);
5189 --IT_BYTEPOS (it2);
5190 it2.sp = 0;
5191 if (handle_display_prop (&it2) == HANDLED_RETURN
5192 && !NILP (val = get_char_property_and_overlay
5193 (make_number (pos), Qdisplay, Qnil, &overlay))
5194 && (OVERLAYP (overlay)
5195 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5196 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5197 goto replaced;
5198
5199 /* Newline is not replaced by anything -- so we are done. */
5200 break;
5201
5202 replaced:
5203 if (beg < BEGV)
5204 beg = BEGV;
5205 IT_CHARPOS (*it) = beg;
5206 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5207 }
5208 }
5209
5210 it->continuation_lines_width = 0;
5211
5212 xassert (IT_CHARPOS (*it) >= BEGV);
5213 xassert (IT_CHARPOS (*it) == BEGV
5214 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5215 CHECK_IT (it);
5216 }
5217
5218
5219 /* Reseat iterator IT at the previous visible line start. Skip
5220 invisible text that is so either due to text properties or due to
5221 selective display. At the end, update IT's overlay information,
5222 face information etc. */
5223
5224 void
5225 reseat_at_previous_visible_line_start (it)
5226 struct it *it;
5227 {
5228 back_to_previous_visible_line_start (it);
5229 reseat (it, it->current.pos, 1);
5230 CHECK_IT (it);
5231 }
5232
5233
5234 /* Reseat iterator IT on the next visible line start in the current
5235 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5236 preceding the line start. Skip over invisible text that is so
5237 because of selective display. Compute faces, overlays etc at the
5238 new position. Note that this function does not skip over text that
5239 is invisible because of text properties. */
5240
5241 static void
5242 reseat_at_next_visible_line_start (it, on_newline_p)
5243 struct it *it;
5244 int on_newline_p;
5245 {
5246 int newline_found_p, skipped_p = 0;
5247
5248 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5249
5250 /* Skip over lines that are invisible because they are indented
5251 more than the value of IT->selective. */
5252 if (it->selective > 0)
5253 while (IT_CHARPOS (*it) < ZV
5254 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5255 (double) it->selective)) /* iftc */
5256 {
5257 xassert (IT_BYTEPOS (*it) == BEGV
5258 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5259 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5260 }
5261
5262 /* Position on the newline if that's what's requested. */
5263 if (on_newline_p && newline_found_p)
5264 {
5265 if (STRINGP (it->string))
5266 {
5267 if (IT_STRING_CHARPOS (*it) > 0)
5268 {
5269 --IT_STRING_CHARPOS (*it);
5270 --IT_STRING_BYTEPOS (*it);
5271 }
5272 }
5273 else if (IT_CHARPOS (*it) > BEGV)
5274 {
5275 --IT_CHARPOS (*it);
5276 --IT_BYTEPOS (*it);
5277 reseat (it, it->current.pos, 0);
5278 }
5279 }
5280 else if (skipped_p)
5281 reseat (it, it->current.pos, 0);
5282
5283 CHECK_IT (it);
5284 }
5285
5286
5287 \f
5288 /***********************************************************************
5289 Changing an iterator's position
5290 ***********************************************************************/
5291
5292 /* Change IT's current position to POS in current_buffer. If FORCE_P
5293 is non-zero, always check for text properties at the new position.
5294 Otherwise, text properties are only looked up if POS >=
5295 IT->check_charpos of a property. */
5296
5297 static void
5298 reseat (it, pos, force_p)
5299 struct it *it;
5300 struct text_pos pos;
5301 int force_p;
5302 {
5303 int original_pos = IT_CHARPOS (*it);
5304
5305 reseat_1 (it, pos, 0);
5306
5307 /* Determine where to check text properties. Avoid doing it
5308 where possible because text property lookup is very expensive. */
5309 if (force_p
5310 || CHARPOS (pos) > it->stop_charpos
5311 || CHARPOS (pos) < original_pos)
5312 handle_stop (it);
5313
5314 CHECK_IT (it);
5315 }
5316
5317
5318 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5319 IT->stop_pos to POS, also. */
5320
5321 static void
5322 reseat_1 (it, pos, set_stop_p)
5323 struct it *it;
5324 struct text_pos pos;
5325 int set_stop_p;
5326 {
5327 /* Don't call this function when scanning a C string. */
5328 xassert (it->s == NULL);
5329
5330 /* POS must be a reasonable value. */
5331 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5332
5333 it->current.pos = it->position = pos;
5334 it->end_charpos = ZV;
5335 it->dpvec = NULL;
5336 it->current.dpvec_index = -1;
5337 it->current.overlay_string_index = -1;
5338 IT_STRING_CHARPOS (*it) = -1;
5339 IT_STRING_BYTEPOS (*it) = -1;
5340 it->string = Qnil;
5341 it->method = GET_FROM_BUFFER;
5342 it->object = it->w->buffer;
5343 it->area = TEXT_AREA;
5344 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5345 it->sp = 0;
5346 it->string_from_display_prop_p = 0;
5347 it->face_before_selective_p = 0;
5348
5349 if (set_stop_p)
5350 it->stop_charpos = CHARPOS (pos);
5351 }
5352
5353
5354 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5355 If S is non-null, it is a C string to iterate over. Otherwise,
5356 STRING gives a Lisp string to iterate over.
5357
5358 If PRECISION > 0, don't return more then PRECISION number of
5359 characters from the string.
5360
5361 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5362 characters have been returned. FIELD_WIDTH < 0 means an infinite
5363 field width.
5364
5365 MULTIBYTE = 0 means disable processing of multibyte characters,
5366 MULTIBYTE > 0 means enable it,
5367 MULTIBYTE < 0 means use IT->multibyte_p.
5368
5369 IT must be initialized via a prior call to init_iterator before
5370 calling this function. */
5371
5372 static void
5373 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5374 struct it *it;
5375 unsigned char *s;
5376 Lisp_Object string;
5377 int charpos;
5378 int precision, field_width, multibyte;
5379 {
5380 /* No region in strings. */
5381 it->region_beg_charpos = it->region_end_charpos = -1;
5382
5383 /* No text property checks performed by default, but see below. */
5384 it->stop_charpos = -1;
5385
5386 /* Set iterator position and end position. */
5387 bzero (&it->current, sizeof it->current);
5388 it->current.overlay_string_index = -1;
5389 it->current.dpvec_index = -1;
5390 xassert (charpos >= 0);
5391
5392 /* If STRING is specified, use its multibyteness, otherwise use the
5393 setting of MULTIBYTE, if specified. */
5394 if (multibyte >= 0)
5395 it->multibyte_p = multibyte > 0;
5396
5397 if (s == NULL)
5398 {
5399 xassert (STRINGP (string));
5400 it->string = string;
5401 it->s = NULL;
5402 it->end_charpos = it->string_nchars = SCHARS (string);
5403 it->method = GET_FROM_STRING;
5404 it->current.string_pos = string_pos (charpos, string);
5405 }
5406 else
5407 {
5408 it->s = s;
5409 it->string = Qnil;
5410
5411 /* Note that we use IT->current.pos, not it->current.string_pos,
5412 for displaying C strings. */
5413 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5414 if (it->multibyte_p)
5415 {
5416 it->current.pos = c_string_pos (charpos, s, 1);
5417 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5418 }
5419 else
5420 {
5421 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5422 it->end_charpos = it->string_nchars = strlen (s);
5423 }
5424
5425 it->method = GET_FROM_C_STRING;
5426 }
5427
5428 /* PRECISION > 0 means don't return more than PRECISION characters
5429 from the string. */
5430 if (precision > 0 && it->end_charpos - charpos > precision)
5431 it->end_charpos = it->string_nchars = charpos + precision;
5432
5433 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5434 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5435 FIELD_WIDTH < 0 means infinite field width. This is useful for
5436 padding with `-' at the end of a mode line. */
5437 if (field_width < 0)
5438 field_width = INFINITY;
5439 if (field_width > it->end_charpos - charpos)
5440 it->end_charpos = charpos + field_width;
5441
5442 /* Use the standard display table for displaying strings. */
5443 if (DISP_TABLE_P (Vstandard_display_table))
5444 it->dp = XCHAR_TABLE (Vstandard_display_table);
5445
5446 it->stop_charpos = charpos;
5447 CHECK_IT (it);
5448 }
5449
5450
5451 \f
5452 /***********************************************************************
5453 Iteration
5454 ***********************************************************************/
5455
5456 /* Map enum it_method value to corresponding next_element_from_* function. */
5457
5458 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5459 {
5460 next_element_from_buffer,
5461 next_element_from_display_vector,
5462 next_element_from_composition,
5463 next_element_from_string,
5464 next_element_from_c_string,
5465 next_element_from_image,
5466 next_element_from_stretch
5467 };
5468
5469
5470 /* Load IT's display element fields with information about the next
5471 display element from the current position of IT. Value is zero if
5472 end of buffer (or C string) is reached. */
5473
5474 static struct frame *last_escape_glyph_frame = NULL;
5475 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5476 static int last_escape_glyph_merged_face_id = 0;
5477
5478 int
5479 get_next_display_element (it)
5480 struct it *it;
5481 {
5482 /* Non-zero means that we found a display element. Zero means that
5483 we hit the end of what we iterate over. Performance note: the
5484 function pointer `method' used here turns out to be faster than
5485 using a sequence of if-statements. */
5486 int success_p;
5487
5488 get_next:
5489 success_p = (*get_next_element[it->method]) (it);
5490
5491 if (it->what == IT_CHARACTER)
5492 {
5493 /* Map via display table or translate control characters.
5494 IT->c, IT->len etc. have been set to the next character by
5495 the function call above. If we have a display table, and it
5496 contains an entry for IT->c, translate it. Don't do this if
5497 IT->c itself comes from a display table, otherwise we could
5498 end up in an infinite recursion. (An alternative could be to
5499 count the recursion depth of this function and signal an
5500 error when a certain maximum depth is reached.) Is it worth
5501 it? */
5502 if (success_p && it->dpvec == NULL)
5503 {
5504 Lisp_Object dv;
5505
5506 if (it->dp
5507 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5508 VECTORP (dv)))
5509 {
5510 struct Lisp_Vector *v = XVECTOR (dv);
5511
5512 /* Return the first character from the display table
5513 entry, if not empty. If empty, don't display the
5514 current character. */
5515 if (v->size)
5516 {
5517 it->dpvec_char_len = it->len;
5518 it->dpvec = v->contents;
5519 it->dpend = v->contents + v->size;
5520 it->current.dpvec_index = 0;
5521 it->dpvec_face_id = -1;
5522 it->saved_face_id = it->face_id;
5523 it->method = GET_FROM_DISPLAY_VECTOR;
5524 it->ellipsis_p = 0;
5525 }
5526 else
5527 {
5528 set_iterator_to_next (it, 0);
5529 }
5530 goto get_next;
5531 }
5532
5533 /* Translate control characters into `\003' or `^C' form.
5534 Control characters coming from a display table entry are
5535 currently not translated because we use IT->dpvec to hold
5536 the translation. This could easily be changed but I
5537 don't believe that it is worth doing.
5538
5539 If it->multibyte_p is nonzero, eight-bit characters and
5540 non-printable multibyte characters are also translated to
5541 octal form.
5542
5543 If it->multibyte_p is zero, eight-bit characters that
5544 don't have corresponding multibyte char code are also
5545 translated to octal form. */
5546 else if ((it->c < ' '
5547 && (it->area != TEXT_AREA
5548 /* In mode line, treat \n like other crl chars. */
5549 || (it->c != '\t'
5550 && it->glyph_row && it->glyph_row->mode_line_p)
5551 || (it->c != '\n' && it->c != '\t')))
5552 || (it->multibyte_p
5553 ? ((it->c >= 127
5554 && it->len == 1)
5555 || !CHAR_PRINTABLE_P (it->c)
5556 || (!NILP (Vnobreak_char_display)
5557 && (it->c == 0x8a0 || it->c == 0x8ad
5558 || it->c == 0x920 || it->c == 0x92d
5559 || it->c == 0xe20 || it->c == 0xe2d
5560 || it->c == 0xf20 || it->c == 0xf2d)))
5561 : (it->c >= 127
5562 && (!unibyte_display_via_language_environment
5563 || it->c == unibyte_char_to_multibyte (it->c)))))
5564 {
5565 /* IT->c is a control character which must be displayed
5566 either as '\003' or as `^C' where the '\\' and '^'
5567 can be defined in the display table. Fill
5568 IT->ctl_chars with glyphs for what we have to
5569 display. Then, set IT->dpvec to these glyphs. */
5570 GLYPH g;
5571 int ctl_len;
5572 int face_id, lface_id = 0 ;
5573 GLYPH escape_glyph;
5574
5575 /* Handle control characters with ^. */
5576
5577 if (it->c < 128 && it->ctl_arrow_p)
5578 {
5579 g = '^'; /* default glyph for Control */
5580 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5581 if (it->dp
5582 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5583 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5584 {
5585 g = XINT (DISP_CTRL_GLYPH (it->dp));
5586 lface_id = FAST_GLYPH_FACE (g);
5587 }
5588 if (lface_id)
5589 {
5590 g = FAST_GLYPH_CHAR (g);
5591 face_id = merge_faces (it->f, Qt, lface_id,
5592 it->face_id);
5593 }
5594 else if (it->f == last_escape_glyph_frame
5595 && it->face_id == last_escape_glyph_face_id)
5596 {
5597 face_id = last_escape_glyph_merged_face_id;
5598 }
5599 else
5600 {
5601 /* Merge the escape-glyph face into the current face. */
5602 face_id = merge_faces (it->f, Qescape_glyph, 0,
5603 it->face_id);
5604 last_escape_glyph_frame = it->f;
5605 last_escape_glyph_face_id = it->face_id;
5606 last_escape_glyph_merged_face_id = face_id;
5607 }
5608
5609 XSETINT (it->ctl_chars[0], g);
5610 g = it->c ^ 0100;
5611 XSETINT (it->ctl_chars[1], g);
5612 ctl_len = 2;
5613 goto display_control;
5614 }
5615
5616 /* Handle non-break space in the mode where it only gets
5617 highlighting. */
5618
5619 if (EQ (Vnobreak_char_display, Qt)
5620 && (it->c == 0x8a0 || it->c == 0x920
5621 || it->c == 0xe20 || it->c == 0xf20))
5622 {
5623 /* Merge the no-break-space face into the current face. */
5624 face_id = merge_faces (it->f, Qnobreak_space, 0,
5625 it->face_id);
5626
5627 g = it->c = ' ';
5628 XSETINT (it->ctl_chars[0], g);
5629 ctl_len = 1;
5630 goto display_control;
5631 }
5632
5633 /* Handle sequences that start with the "escape glyph". */
5634
5635 /* the default escape glyph is \. */
5636 escape_glyph = '\\';
5637
5638 if (it->dp
5639 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5640 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5641 {
5642 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5643 lface_id = FAST_GLYPH_FACE (escape_glyph);
5644 }
5645 if (lface_id)
5646 {
5647 /* The display table specified a face.
5648 Merge it into face_id and also into escape_glyph. */
5649 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5650 face_id = merge_faces (it->f, Qt, lface_id,
5651 it->face_id);
5652 }
5653 else if (it->f == last_escape_glyph_frame
5654 && it->face_id == last_escape_glyph_face_id)
5655 {
5656 face_id = last_escape_glyph_merged_face_id;
5657 }
5658 else
5659 {
5660 /* Merge the escape-glyph face into the current face. */
5661 face_id = merge_faces (it->f, Qescape_glyph, 0,
5662 it->face_id);
5663 last_escape_glyph_frame = it->f;
5664 last_escape_glyph_face_id = it->face_id;
5665 last_escape_glyph_merged_face_id = face_id;
5666 }
5667
5668 /* Handle soft hyphens in the mode where they only get
5669 highlighting. */
5670
5671 if (EQ (Vnobreak_char_display, Qt)
5672 && (it->c == 0x8ad || it->c == 0x92d
5673 || it->c == 0xe2d || it->c == 0xf2d))
5674 {
5675 g = it->c = '-';
5676 XSETINT (it->ctl_chars[0], g);
5677 ctl_len = 1;
5678 goto display_control;
5679 }
5680
5681 /* Handle non-break space and soft hyphen
5682 with the escape glyph. */
5683
5684 if (it->c == 0x8a0 || it->c == 0x8ad
5685 || it->c == 0x920 || it->c == 0x92d
5686 || it->c == 0xe20 || it->c == 0xe2d
5687 || it->c == 0xf20 || it->c == 0xf2d)
5688 {
5689 XSETINT (it->ctl_chars[0], escape_glyph);
5690 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5691 XSETINT (it->ctl_chars[1], g);
5692 ctl_len = 2;
5693 goto display_control;
5694 }
5695
5696 {
5697 unsigned char str[MAX_MULTIBYTE_LENGTH];
5698 int len;
5699 int i;
5700
5701 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5702 if (SINGLE_BYTE_CHAR_P (it->c))
5703 str[0] = it->c, len = 1;
5704 else
5705 {
5706 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5707 if (len < 0)
5708 {
5709 /* It's an invalid character, which shouldn't
5710 happen actually, but due to bugs it may
5711 happen. Let's print the char as is, there's
5712 not much meaningful we can do with it. */
5713 str[0] = it->c;
5714 str[1] = it->c >> 8;
5715 str[2] = it->c >> 16;
5716 str[3] = it->c >> 24;
5717 len = 4;
5718 }
5719 }
5720
5721 for (i = 0; i < len; i++)
5722 {
5723 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5724 /* Insert three more glyphs into IT->ctl_chars for
5725 the octal display of the character. */
5726 g = ((str[i] >> 6) & 7) + '0';
5727 XSETINT (it->ctl_chars[i * 4 + 1], g);
5728 g = ((str[i] >> 3) & 7) + '0';
5729 XSETINT (it->ctl_chars[i * 4 + 2], g);
5730 g = (str[i] & 7) + '0';
5731 XSETINT (it->ctl_chars[i * 4 + 3], g);
5732 }
5733 ctl_len = len * 4;
5734 }
5735
5736 display_control:
5737 /* Set up IT->dpvec and return first character from it. */
5738 it->dpvec_char_len = it->len;
5739 it->dpvec = it->ctl_chars;
5740 it->dpend = it->dpvec + ctl_len;
5741 it->current.dpvec_index = 0;
5742 it->dpvec_face_id = face_id;
5743 it->saved_face_id = it->face_id;
5744 it->method = GET_FROM_DISPLAY_VECTOR;
5745 it->ellipsis_p = 0;
5746 goto get_next;
5747 }
5748 }
5749
5750 /* Adjust face id for a multibyte character. There are no
5751 multibyte character in unibyte text. */
5752 if (it->multibyte_p
5753 && success_p
5754 && FRAME_WINDOW_P (it->f))
5755 {
5756 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5757 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5758 }
5759 }
5760
5761 /* Is this character the last one of a run of characters with
5762 box? If yes, set IT->end_of_box_run_p to 1. */
5763 if (it->face_box_p
5764 && it->s == NULL)
5765 {
5766 int face_id;
5767 struct face *face;
5768
5769 it->end_of_box_run_p
5770 = ((face_id = face_after_it_pos (it),
5771 face_id != it->face_id)
5772 && (face = FACE_FROM_ID (it->f, face_id),
5773 face->box == FACE_NO_BOX));
5774 }
5775
5776 /* Value is 0 if end of buffer or string reached. */
5777 return success_p;
5778 }
5779
5780
5781 /* Move IT to the next display element.
5782
5783 RESEAT_P non-zero means if called on a newline in buffer text,
5784 skip to the next visible line start.
5785
5786 Functions get_next_display_element and set_iterator_to_next are
5787 separate because I find this arrangement easier to handle than a
5788 get_next_display_element function that also increments IT's
5789 position. The way it is we can first look at an iterator's current
5790 display element, decide whether it fits on a line, and if it does,
5791 increment the iterator position. The other way around we probably
5792 would either need a flag indicating whether the iterator has to be
5793 incremented the next time, or we would have to implement a
5794 decrement position function which would not be easy to write. */
5795
5796 void
5797 set_iterator_to_next (it, reseat_p)
5798 struct it *it;
5799 int reseat_p;
5800 {
5801 /* Reset flags indicating start and end of a sequence of characters
5802 with box. Reset them at the start of this function because
5803 moving the iterator to a new position might set them. */
5804 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5805
5806 switch (it->method)
5807 {
5808 case GET_FROM_BUFFER:
5809 /* The current display element of IT is a character from
5810 current_buffer. Advance in the buffer, and maybe skip over
5811 invisible lines that are so because of selective display. */
5812 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5813 reseat_at_next_visible_line_start (it, 0);
5814 else
5815 {
5816 xassert (it->len != 0);
5817 IT_BYTEPOS (*it) += it->len;
5818 IT_CHARPOS (*it) += 1;
5819 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5820 }
5821 break;
5822
5823 case GET_FROM_COMPOSITION:
5824 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5825 xassert (it->sp > 0);
5826 pop_it (it);
5827 if (it->method == GET_FROM_STRING)
5828 {
5829 IT_STRING_BYTEPOS (*it) += it->len;
5830 IT_STRING_CHARPOS (*it) += it->cmp_len;
5831 goto consider_string_end;
5832 }
5833 else if (it->method == GET_FROM_BUFFER)
5834 {
5835 IT_BYTEPOS (*it) += it->len;
5836 IT_CHARPOS (*it) += it->cmp_len;
5837 }
5838 break;
5839
5840 case GET_FROM_C_STRING:
5841 /* Current display element of IT is from a C string. */
5842 IT_BYTEPOS (*it) += it->len;
5843 IT_CHARPOS (*it) += 1;
5844 break;
5845
5846 case GET_FROM_DISPLAY_VECTOR:
5847 /* Current display element of IT is from a display table entry.
5848 Advance in the display table definition. Reset it to null if
5849 end reached, and continue with characters from buffers/
5850 strings. */
5851 ++it->current.dpvec_index;
5852
5853 /* Restore face of the iterator to what they were before the
5854 display vector entry (these entries may contain faces). */
5855 it->face_id = it->saved_face_id;
5856
5857 if (it->dpvec + it->current.dpvec_index == it->dpend)
5858 {
5859 int recheck_faces = it->ellipsis_p;
5860
5861 if (it->s)
5862 it->method = GET_FROM_C_STRING;
5863 else if (STRINGP (it->string))
5864 it->method = GET_FROM_STRING;
5865 else
5866 {
5867 it->method = GET_FROM_BUFFER;
5868 it->object = it->w->buffer;
5869 }
5870
5871 it->dpvec = NULL;
5872 it->current.dpvec_index = -1;
5873
5874 /* Skip over characters which were displayed via IT->dpvec. */
5875 if (it->dpvec_char_len < 0)
5876 reseat_at_next_visible_line_start (it, 1);
5877 else if (it->dpvec_char_len > 0)
5878 {
5879 if (it->method == GET_FROM_STRING
5880 && it->n_overlay_strings > 0)
5881 it->ignore_overlay_strings_at_pos_p = 1;
5882 it->len = it->dpvec_char_len;
5883 set_iterator_to_next (it, reseat_p);
5884 }
5885
5886 /* Maybe recheck faces after display vector */
5887 if (recheck_faces)
5888 it->stop_charpos = IT_CHARPOS (*it);
5889 }
5890 break;
5891
5892 case GET_FROM_STRING:
5893 /* Current display element is a character from a Lisp string. */
5894 xassert (it->s == NULL && STRINGP (it->string));
5895 IT_STRING_BYTEPOS (*it) += it->len;
5896 IT_STRING_CHARPOS (*it) += 1;
5897
5898 consider_string_end:
5899
5900 if (it->current.overlay_string_index >= 0)
5901 {
5902 /* IT->string is an overlay string. Advance to the
5903 next, if there is one. */
5904 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5905 next_overlay_string (it);
5906 }
5907 else
5908 {
5909 /* IT->string is not an overlay string. If we reached
5910 its end, and there is something on IT->stack, proceed
5911 with what is on the stack. This can be either another
5912 string, this time an overlay string, or a buffer. */
5913 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5914 && it->sp > 0)
5915 {
5916 pop_it (it);
5917 if (it->method == GET_FROM_STRING)
5918 goto consider_string_end;
5919 }
5920 }
5921 break;
5922
5923 case GET_FROM_IMAGE:
5924 case GET_FROM_STRETCH:
5925 /* The position etc with which we have to proceed are on
5926 the stack. The position may be at the end of a string,
5927 if the `display' property takes up the whole string. */
5928 xassert (it->sp > 0);
5929 pop_it (it);
5930 if (it->method == GET_FROM_STRING)
5931 goto consider_string_end;
5932 break;
5933
5934 default:
5935 /* There are no other methods defined, so this should be a bug. */
5936 abort ();
5937 }
5938
5939 xassert (it->method != GET_FROM_STRING
5940 || (STRINGP (it->string)
5941 && IT_STRING_CHARPOS (*it) >= 0));
5942 }
5943
5944 /* Load IT's display element fields with information about the next
5945 display element which comes from a display table entry or from the
5946 result of translating a control character to one of the forms `^C'
5947 or `\003'.
5948
5949 IT->dpvec holds the glyphs to return as characters.
5950 IT->saved_face_id holds the face id before the display vector--
5951 it is restored into IT->face_idin set_iterator_to_next. */
5952
5953 static int
5954 next_element_from_display_vector (it)
5955 struct it *it;
5956 {
5957 /* Precondition. */
5958 xassert (it->dpvec && it->current.dpvec_index >= 0);
5959
5960 it->face_id = it->saved_face_id;
5961
5962 if (INTEGERP (*it->dpvec)
5963 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5964 {
5965 GLYPH g;
5966
5967 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5968 it->c = FAST_GLYPH_CHAR (g);
5969 it->len = CHAR_BYTES (it->c);
5970
5971 /* The entry may contain a face id to use. Such a face id is
5972 the id of a Lisp face, not a realized face. A face id of
5973 zero means no face is specified. */
5974 if (it->dpvec_face_id >= 0)
5975 it->face_id = it->dpvec_face_id;
5976 else
5977 {
5978 int lface_id = FAST_GLYPH_FACE (g);
5979 if (lface_id > 0)
5980 it->face_id = merge_faces (it->f, Qt, lface_id,
5981 it->saved_face_id);
5982 }
5983 }
5984 else
5985 /* Display table entry is invalid. Return a space. */
5986 it->c = ' ', it->len = 1;
5987
5988 /* Don't change position and object of the iterator here. They are
5989 still the values of the character that had this display table
5990 entry or was translated, and that's what we want. */
5991 it->what = IT_CHARACTER;
5992 return 1;
5993 }
5994
5995
5996 /* Load IT with the next display element from Lisp string IT->string.
5997 IT->current.string_pos is the current position within the string.
5998 If IT->current.overlay_string_index >= 0, the Lisp string is an
5999 overlay string. */
6000
6001 static int
6002 next_element_from_string (it)
6003 struct it *it;
6004 {
6005 struct text_pos position;
6006
6007 xassert (STRINGP (it->string));
6008 xassert (IT_STRING_CHARPOS (*it) >= 0);
6009 position = it->current.string_pos;
6010
6011 /* Time to check for invisible text? */
6012 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6013 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6014 {
6015 handle_stop (it);
6016
6017 /* Since a handler may have changed IT->method, we must
6018 recurse here. */
6019 return get_next_display_element (it);
6020 }
6021
6022 if (it->current.overlay_string_index >= 0)
6023 {
6024 /* Get the next character from an overlay string. In overlay
6025 strings, There is no field width or padding with spaces to
6026 do. */
6027 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6028 {
6029 it->what = IT_EOB;
6030 return 0;
6031 }
6032 else if (STRING_MULTIBYTE (it->string))
6033 {
6034 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6035 const unsigned char *s = (SDATA (it->string)
6036 + IT_STRING_BYTEPOS (*it));
6037 it->c = string_char_and_length (s, remaining, &it->len);
6038 }
6039 else
6040 {
6041 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6042 it->len = 1;
6043 }
6044 }
6045 else
6046 {
6047 /* Get the next character from a Lisp string that is not an
6048 overlay string. Such strings come from the mode line, for
6049 example. We may have to pad with spaces, or truncate the
6050 string. See also next_element_from_c_string. */
6051 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6052 {
6053 it->what = IT_EOB;
6054 return 0;
6055 }
6056 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6057 {
6058 /* Pad with spaces. */
6059 it->c = ' ', it->len = 1;
6060 CHARPOS (position) = BYTEPOS (position) = -1;
6061 }
6062 else if (STRING_MULTIBYTE (it->string))
6063 {
6064 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6065 const unsigned char *s = (SDATA (it->string)
6066 + IT_STRING_BYTEPOS (*it));
6067 it->c = string_char_and_length (s, maxlen, &it->len);
6068 }
6069 else
6070 {
6071 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6072 it->len = 1;
6073 }
6074 }
6075
6076 /* Record what we have and where it came from. */
6077 it->what = IT_CHARACTER;
6078 it->object = it->string;
6079 it->position = position;
6080 return 1;
6081 }
6082
6083
6084 /* Load IT with next display element from C string IT->s.
6085 IT->string_nchars is the maximum number of characters to return
6086 from the string. IT->end_charpos may be greater than
6087 IT->string_nchars when this function is called, in which case we
6088 may have to return padding spaces. Value is zero if end of string
6089 reached, including padding spaces. */
6090
6091 static int
6092 next_element_from_c_string (it)
6093 struct it *it;
6094 {
6095 int success_p = 1;
6096
6097 xassert (it->s);
6098 it->what = IT_CHARACTER;
6099 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6100 it->object = Qnil;
6101
6102 /* IT's position can be greater IT->string_nchars in case a field
6103 width or precision has been specified when the iterator was
6104 initialized. */
6105 if (IT_CHARPOS (*it) >= it->end_charpos)
6106 {
6107 /* End of the game. */
6108 it->what = IT_EOB;
6109 success_p = 0;
6110 }
6111 else if (IT_CHARPOS (*it) >= it->string_nchars)
6112 {
6113 /* Pad with spaces. */
6114 it->c = ' ', it->len = 1;
6115 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6116 }
6117 else if (it->multibyte_p)
6118 {
6119 /* Implementation note: The calls to strlen apparently aren't a
6120 performance problem because there is no noticeable performance
6121 difference between Emacs running in unibyte or multibyte mode. */
6122 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6123 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6124 maxlen, &it->len);
6125 }
6126 else
6127 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6128
6129 return success_p;
6130 }
6131
6132
6133 /* Set up IT to return characters from an ellipsis, if appropriate.
6134 The definition of the ellipsis glyphs may come from a display table
6135 entry. This function Fills IT with the first glyph from the
6136 ellipsis if an ellipsis is to be displayed. */
6137
6138 static int
6139 next_element_from_ellipsis (it)
6140 struct it *it;
6141 {
6142 if (it->selective_display_ellipsis_p)
6143 setup_for_ellipsis (it, it->len);
6144 else
6145 {
6146 /* The face at the current position may be different from the
6147 face we find after the invisible text. Remember what it
6148 was in IT->saved_face_id, and signal that it's there by
6149 setting face_before_selective_p. */
6150 it->saved_face_id = it->face_id;
6151 it->method = GET_FROM_BUFFER;
6152 it->object = it->w->buffer;
6153 reseat_at_next_visible_line_start (it, 1);
6154 it->face_before_selective_p = 1;
6155 }
6156
6157 return get_next_display_element (it);
6158 }
6159
6160
6161 /* Deliver an image display element. The iterator IT is already
6162 filled with image information (done in handle_display_prop). Value
6163 is always 1. */
6164
6165
6166 static int
6167 next_element_from_image (it)
6168 struct it *it;
6169 {
6170 it->what = IT_IMAGE;
6171 return 1;
6172 }
6173
6174
6175 /* Fill iterator IT with next display element from a stretch glyph
6176 property. IT->object is the value of the text property. Value is
6177 always 1. */
6178
6179 static int
6180 next_element_from_stretch (it)
6181 struct it *it;
6182 {
6183 it->what = IT_STRETCH;
6184 return 1;
6185 }
6186
6187
6188 /* Load IT with the next display element from current_buffer. Value
6189 is zero if end of buffer reached. IT->stop_charpos is the next
6190 position at which to stop and check for text properties or buffer
6191 end. */
6192
6193 static int
6194 next_element_from_buffer (it)
6195 struct it *it;
6196 {
6197 int success_p = 1;
6198
6199 /* Check this assumption, otherwise, we would never enter the
6200 if-statement, below. */
6201 xassert (IT_CHARPOS (*it) >= BEGV
6202 && IT_CHARPOS (*it) <= it->stop_charpos);
6203
6204 if (IT_CHARPOS (*it) >= it->stop_charpos)
6205 {
6206 if (IT_CHARPOS (*it) >= it->end_charpos)
6207 {
6208 int overlay_strings_follow_p;
6209
6210 /* End of the game, except when overlay strings follow that
6211 haven't been returned yet. */
6212 if (it->overlay_strings_at_end_processed_p)
6213 overlay_strings_follow_p = 0;
6214 else
6215 {
6216 it->overlay_strings_at_end_processed_p = 1;
6217 overlay_strings_follow_p = get_overlay_strings (it, 0);
6218 }
6219
6220 if (overlay_strings_follow_p)
6221 success_p = get_next_display_element (it);
6222 else
6223 {
6224 it->what = IT_EOB;
6225 it->position = it->current.pos;
6226 success_p = 0;
6227 }
6228 }
6229 else
6230 {
6231 handle_stop (it);
6232 return get_next_display_element (it);
6233 }
6234 }
6235 else
6236 {
6237 /* No face changes, overlays etc. in sight, so just return a
6238 character from current_buffer. */
6239 unsigned char *p;
6240
6241 /* Maybe run the redisplay end trigger hook. Performance note:
6242 This doesn't seem to cost measurable time. */
6243 if (it->redisplay_end_trigger_charpos
6244 && it->glyph_row
6245 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6246 run_redisplay_end_trigger_hook (it);
6247
6248 /* Get the next character, maybe multibyte. */
6249 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6250 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6251 {
6252 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6253 - IT_BYTEPOS (*it));
6254 it->c = string_char_and_length (p, maxlen, &it->len);
6255 }
6256 else
6257 it->c = *p, it->len = 1;
6258
6259 /* Record what we have and where it came from. */
6260 it->what = IT_CHARACTER;
6261 it->object = it->w->buffer;
6262 it->position = it->current.pos;
6263
6264 /* Normally we return the character found above, except when we
6265 really want to return an ellipsis for selective display. */
6266 if (it->selective)
6267 {
6268 if (it->c == '\n')
6269 {
6270 /* A value of selective > 0 means hide lines indented more
6271 than that number of columns. */
6272 if (it->selective > 0
6273 && IT_CHARPOS (*it) + 1 < ZV
6274 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6275 IT_BYTEPOS (*it) + 1,
6276 (double) it->selective)) /* iftc */
6277 {
6278 success_p = next_element_from_ellipsis (it);
6279 it->dpvec_char_len = -1;
6280 }
6281 }
6282 else if (it->c == '\r' && it->selective == -1)
6283 {
6284 /* A value of selective == -1 means that everything from the
6285 CR to the end of the line is invisible, with maybe an
6286 ellipsis displayed for it. */
6287 success_p = next_element_from_ellipsis (it);
6288 it->dpvec_char_len = -1;
6289 }
6290 }
6291 }
6292
6293 /* Value is zero if end of buffer reached. */
6294 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6295 return success_p;
6296 }
6297
6298
6299 /* Run the redisplay end trigger hook for IT. */
6300
6301 static void
6302 run_redisplay_end_trigger_hook (it)
6303 struct it *it;
6304 {
6305 Lisp_Object args[3];
6306
6307 /* IT->glyph_row should be non-null, i.e. we should be actually
6308 displaying something, or otherwise we should not run the hook. */
6309 xassert (it->glyph_row);
6310
6311 /* Set up hook arguments. */
6312 args[0] = Qredisplay_end_trigger_functions;
6313 args[1] = it->window;
6314 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6315 it->redisplay_end_trigger_charpos = 0;
6316
6317 /* Since we are *trying* to run these functions, don't try to run
6318 them again, even if they get an error. */
6319 it->w->redisplay_end_trigger = Qnil;
6320 Frun_hook_with_args (3, args);
6321
6322 /* Notice if it changed the face of the character we are on. */
6323 handle_face_prop (it);
6324 }
6325
6326
6327 /* Deliver a composition display element. The iterator IT is already
6328 filled with composition information (done in
6329 handle_composition_prop). Value is always 1. */
6330
6331 static int
6332 next_element_from_composition (it)
6333 struct it *it;
6334 {
6335 it->what = IT_COMPOSITION;
6336 it->position = (STRINGP (it->string)
6337 ? it->current.string_pos
6338 : it->current.pos);
6339 if (STRINGP (it->string))
6340 it->object = it->string;
6341 else
6342 it->object = it->w->buffer;
6343 return 1;
6344 }
6345
6346
6347 \f
6348 /***********************************************************************
6349 Moving an iterator without producing glyphs
6350 ***********************************************************************/
6351
6352 /* Check if iterator is at a position corresponding to a valid buffer
6353 position after some move_it_ call. */
6354
6355 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6356 ((it)->method == GET_FROM_STRING \
6357 ? IT_STRING_CHARPOS (*it) == 0 \
6358 : 1)
6359
6360
6361 /* Move iterator IT to a specified buffer or X position within one
6362 line on the display without producing glyphs.
6363
6364 OP should be a bit mask including some or all of these bits:
6365 MOVE_TO_X: Stop on reaching x-position TO_X.
6366 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6367 Regardless of OP's value, stop in reaching the end of the display line.
6368
6369 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6370 This means, in particular, that TO_X includes window's horizontal
6371 scroll amount.
6372
6373 The return value has several possible values that
6374 say what condition caused the scan to stop:
6375
6376 MOVE_POS_MATCH_OR_ZV
6377 - when TO_POS or ZV was reached.
6378
6379 MOVE_X_REACHED
6380 -when TO_X was reached before TO_POS or ZV were reached.
6381
6382 MOVE_LINE_CONTINUED
6383 - when we reached the end of the display area and the line must
6384 be continued.
6385
6386 MOVE_LINE_TRUNCATED
6387 - when we reached the end of the display area and the line is
6388 truncated.
6389
6390 MOVE_NEWLINE_OR_CR
6391 - when we stopped at a line end, i.e. a newline or a CR and selective
6392 display is on. */
6393
6394 static enum move_it_result
6395 move_it_in_display_line_to (it, to_charpos, to_x, op)
6396 struct it *it;
6397 int to_charpos, to_x, op;
6398 {
6399 enum move_it_result result = MOVE_UNDEFINED;
6400 struct glyph_row *saved_glyph_row;
6401
6402 /* Don't produce glyphs in produce_glyphs. */
6403 saved_glyph_row = it->glyph_row;
6404 it->glyph_row = NULL;
6405
6406 #define BUFFER_POS_REACHED_P() \
6407 ((op & MOVE_TO_POS) != 0 \
6408 && BUFFERP (it->object) \
6409 && IT_CHARPOS (*it) >= to_charpos \
6410 && (it->method == GET_FROM_BUFFER \
6411 || (it->method == GET_FROM_DISPLAY_VECTOR \
6412 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6413
6414
6415 while (1)
6416 {
6417 int x, i, ascent = 0, descent = 0;
6418
6419 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6420 if ((op & MOVE_TO_POS) != 0
6421 && BUFFERP (it->object)
6422 && it->method == GET_FROM_BUFFER
6423 && IT_CHARPOS (*it) > to_charpos)
6424 {
6425 result = MOVE_POS_MATCH_OR_ZV;
6426 break;
6427 }
6428
6429 /* Stop when ZV reached.
6430 We used to stop here when TO_CHARPOS reached as well, but that is
6431 too soon if this glyph does not fit on this line. So we handle it
6432 explicitly below. */
6433 if (!get_next_display_element (it)
6434 || (it->truncate_lines_p
6435 && BUFFER_POS_REACHED_P ()))
6436 {
6437 result = MOVE_POS_MATCH_OR_ZV;
6438 break;
6439 }
6440
6441 /* The call to produce_glyphs will get the metrics of the
6442 display element IT is loaded with. We record in x the
6443 x-position before this display element in case it does not
6444 fit on the line. */
6445 x = it->current_x;
6446
6447 /* Remember the line height so far in case the next element doesn't
6448 fit on the line. */
6449 if (!it->truncate_lines_p)
6450 {
6451 ascent = it->max_ascent;
6452 descent = it->max_descent;
6453 }
6454
6455 PRODUCE_GLYPHS (it);
6456
6457 if (it->area != TEXT_AREA)
6458 {
6459 set_iterator_to_next (it, 1);
6460 continue;
6461 }
6462
6463 /* The number of glyphs we get back in IT->nglyphs will normally
6464 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6465 character on a terminal frame, or (iii) a line end. For the
6466 second case, IT->nglyphs - 1 padding glyphs will be present
6467 (on X frames, there is only one glyph produced for a
6468 composite character.
6469
6470 The behavior implemented below means, for continuation lines,
6471 that as many spaces of a TAB as fit on the current line are
6472 displayed there. For terminal frames, as many glyphs of a
6473 multi-glyph character are displayed in the current line, too.
6474 This is what the old redisplay code did, and we keep it that
6475 way. Under X, the whole shape of a complex character must
6476 fit on the line or it will be completely displayed in the
6477 next line.
6478
6479 Note that both for tabs and padding glyphs, all glyphs have
6480 the same width. */
6481 if (it->nglyphs)
6482 {
6483 /* More than one glyph or glyph doesn't fit on line. All
6484 glyphs have the same width. */
6485 int single_glyph_width = it->pixel_width / it->nglyphs;
6486 int new_x;
6487 int x_before_this_char = x;
6488 int hpos_before_this_char = it->hpos;
6489
6490 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6491 {
6492 new_x = x + single_glyph_width;
6493
6494 /* We want to leave anything reaching TO_X to the caller. */
6495 if ((op & MOVE_TO_X) && new_x > to_x)
6496 {
6497 if (BUFFER_POS_REACHED_P ())
6498 goto buffer_pos_reached;
6499 it->current_x = x;
6500 result = MOVE_X_REACHED;
6501 break;
6502 }
6503 else if (/* Lines are continued. */
6504 !it->truncate_lines_p
6505 && (/* And glyph doesn't fit on the line. */
6506 new_x > it->last_visible_x
6507 /* Or it fits exactly and we're on a window
6508 system frame. */
6509 || (new_x == it->last_visible_x
6510 && FRAME_WINDOW_P (it->f))))
6511 {
6512 if (/* IT->hpos == 0 means the very first glyph
6513 doesn't fit on the line, e.g. a wide image. */
6514 it->hpos == 0
6515 || (new_x == it->last_visible_x
6516 && FRAME_WINDOW_P (it->f)))
6517 {
6518 ++it->hpos;
6519 it->current_x = new_x;
6520
6521 /* The character's last glyph just barely fits
6522 in this row. */
6523 if (i == it->nglyphs - 1)
6524 {
6525 /* If this is the destination position,
6526 return a position *before* it in this row,
6527 now that we know it fits in this row. */
6528 if (BUFFER_POS_REACHED_P ())
6529 {
6530 it->hpos = hpos_before_this_char;
6531 it->current_x = x_before_this_char;
6532 result = MOVE_POS_MATCH_OR_ZV;
6533 break;
6534 }
6535
6536 set_iterator_to_next (it, 1);
6537 #ifdef HAVE_WINDOW_SYSTEM
6538 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6539 {
6540 if (!get_next_display_element (it))
6541 {
6542 result = MOVE_POS_MATCH_OR_ZV;
6543 break;
6544 }
6545 if (BUFFER_POS_REACHED_P ())
6546 {
6547 if (ITERATOR_AT_END_OF_LINE_P (it))
6548 result = MOVE_POS_MATCH_OR_ZV;
6549 else
6550 result = MOVE_LINE_CONTINUED;
6551 break;
6552 }
6553 if (ITERATOR_AT_END_OF_LINE_P (it))
6554 {
6555 result = MOVE_NEWLINE_OR_CR;
6556 break;
6557 }
6558 }
6559 #endif /* HAVE_WINDOW_SYSTEM */
6560 }
6561 }
6562 else
6563 {
6564 it->current_x = x;
6565 it->max_ascent = ascent;
6566 it->max_descent = descent;
6567 }
6568
6569 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6570 IT_CHARPOS (*it)));
6571 result = MOVE_LINE_CONTINUED;
6572 break;
6573 }
6574 else if (BUFFER_POS_REACHED_P ())
6575 goto buffer_pos_reached;
6576 else if (new_x > it->first_visible_x)
6577 {
6578 /* Glyph is visible. Increment number of glyphs that
6579 would be displayed. */
6580 ++it->hpos;
6581 }
6582 else
6583 {
6584 /* Glyph is completely off the left margin of the display
6585 area. Nothing to do. */
6586 }
6587 }
6588
6589 if (result != MOVE_UNDEFINED)
6590 break;
6591 }
6592 else if (BUFFER_POS_REACHED_P ())
6593 {
6594 buffer_pos_reached:
6595 it->current_x = x;
6596 it->max_ascent = ascent;
6597 it->max_descent = descent;
6598 result = MOVE_POS_MATCH_OR_ZV;
6599 break;
6600 }
6601 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6602 {
6603 /* Stop when TO_X specified and reached. This check is
6604 necessary here because of lines consisting of a line end,
6605 only. The line end will not produce any glyphs and we
6606 would never get MOVE_X_REACHED. */
6607 xassert (it->nglyphs == 0);
6608 result = MOVE_X_REACHED;
6609 break;
6610 }
6611
6612 /* Is this a line end? If yes, we're done. */
6613 if (ITERATOR_AT_END_OF_LINE_P (it))
6614 {
6615 result = MOVE_NEWLINE_OR_CR;
6616 break;
6617 }
6618
6619 /* The current display element has been consumed. Advance
6620 to the next. */
6621 set_iterator_to_next (it, 1);
6622
6623 /* Stop if lines are truncated and IT's current x-position is
6624 past the right edge of the window now. */
6625 if (it->truncate_lines_p
6626 && it->current_x >= it->last_visible_x)
6627 {
6628 #ifdef HAVE_WINDOW_SYSTEM
6629 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6630 {
6631 if (!get_next_display_element (it)
6632 || BUFFER_POS_REACHED_P ())
6633 {
6634 result = MOVE_POS_MATCH_OR_ZV;
6635 break;
6636 }
6637 if (ITERATOR_AT_END_OF_LINE_P (it))
6638 {
6639 result = MOVE_NEWLINE_OR_CR;
6640 break;
6641 }
6642 }
6643 #endif /* HAVE_WINDOW_SYSTEM */
6644 result = MOVE_LINE_TRUNCATED;
6645 break;
6646 }
6647 }
6648
6649 #undef BUFFER_POS_REACHED_P
6650
6651 /* Restore the iterator settings altered at the beginning of this
6652 function. */
6653 it->glyph_row = saved_glyph_row;
6654 return result;
6655 }
6656
6657
6658 /* Move IT forward until it satisfies one or more of the criteria in
6659 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6660
6661 OP is a bit-mask that specifies where to stop, and in particular,
6662 which of those four position arguments makes a difference. See the
6663 description of enum move_operation_enum.
6664
6665 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6666 screen line, this function will set IT to the next position >
6667 TO_CHARPOS. */
6668
6669 void
6670 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6671 struct it *it;
6672 int to_charpos, to_x, to_y, to_vpos;
6673 int op;
6674 {
6675 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6676 int line_height;
6677 int reached = 0;
6678
6679 for (;;)
6680 {
6681 if (op & MOVE_TO_VPOS)
6682 {
6683 /* If no TO_CHARPOS and no TO_X specified, stop at the
6684 start of the line TO_VPOS. */
6685 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6686 {
6687 if (it->vpos == to_vpos)
6688 {
6689 reached = 1;
6690 break;
6691 }
6692 else
6693 skip = move_it_in_display_line_to (it, -1, -1, 0);
6694 }
6695 else
6696 {
6697 /* TO_VPOS >= 0 means stop at TO_X in the line at
6698 TO_VPOS, or at TO_POS, whichever comes first. */
6699 if (it->vpos == to_vpos)
6700 {
6701 reached = 2;
6702 break;
6703 }
6704
6705 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6706
6707 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6708 {
6709 reached = 3;
6710 break;
6711 }
6712 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6713 {
6714 /* We have reached TO_X but not in the line we want. */
6715 skip = move_it_in_display_line_to (it, to_charpos,
6716 -1, MOVE_TO_POS);
6717 if (skip == MOVE_POS_MATCH_OR_ZV)
6718 {
6719 reached = 4;
6720 break;
6721 }
6722 }
6723 }
6724 }
6725 else if (op & MOVE_TO_Y)
6726 {
6727 struct it it_backup;
6728
6729 /* TO_Y specified means stop at TO_X in the line containing
6730 TO_Y---or at TO_CHARPOS if this is reached first. The
6731 problem is that we can't really tell whether the line
6732 contains TO_Y before we have completely scanned it, and
6733 this may skip past TO_X. What we do is to first scan to
6734 TO_X.
6735
6736 If TO_X is not specified, use a TO_X of zero. The reason
6737 is to make the outcome of this function more predictable.
6738 If we didn't use TO_X == 0, we would stop at the end of
6739 the line which is probably not what a caller would expect
6740 to happen. */
6741 skip = move_it_in_display_line_to (it, to_charpos,
6742 ((op & MOVE_TO_X)
6743 ? to_x : 0),
6744 (MOVE_TO_X
6745 | (op & MOVE_TO_POS)));
6746
6747 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6748 if (skip == MOVE_POS_MATCH_OR_ZV)
6749 {
6750 reached = 5;
6751 break;
6752 }
6753
6754 /* If TO_X was reached, we would like to know whether TO_Y
6755 is in the line. This can only be said if we know the
6756 total line height which requires us to scan the rest of
6757 the line. */
6758 if (skip == MOVE_X_REACHED)
6759 {
6760 it_backup = *it;
6761 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6762 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6763 op & MOVE_TO_POS);
6764 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6765 }
6766
6767 /* Now, decide whether TO_Y is in this line. */
6768 line_height = it->max_ascent + it->max_descent;
6769 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6770
6771 if (to_y >= it->current_y
6772 && to_y < it->current_y + line_height)
6773 {
6774 if (skip == MOVE_X_REACHED)
6775 /* If TO_Y is in this line and TO_X was reached above,
6776 we scanned too far. We have to restore IT's settings
6777 to the ones before skipping. */
6778 *it = it_backup;
6779 reached = 6;
6780 }
6781 else if (skip == MOVE_X_REACHED)
6782 {
6783 skip = skip2;
6784 if (skip == MOVE_POS_MATCH_OR_ZV)
6785 reached = 7;
6786 }
6787
6788 if (reached)
6789 break;
6790 }
6791 else if (BUFFERP (it->object)
6792 && it->method == GET_FROM_BUFFER
6793 && IT_CHARPOS (*it) >= to_charpos)
6794 skip = MOVE_POS_MATCH_OR_ZV;
6795 else
6796 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6797
6798 switch (skip)
6799 {
6800 case MOVE_POS_MATCH_OR_ZV:
6801 reached = 8;
6802 goto out;
6803
6804 case MOVE_NEWLINE_OR_CR:
6805 set_iterator_to_next (it, 1);
6806 it->continuation_lines_width = 0;
6807 break;
6808
6809 case MOVE_LINE_TRUNCATED:
6810 it->continuation_lines_width = 0;
6811 reseat_at_next_visible_line_start (it, 0);
6812 if ((op & MOVE_TO_POS) != 0
6813 && IT_CHARPOS (*it) > to_charpos)
6814 {
6815 reached = 9;
6816 goto out;
6817 }
6818 break;
6819
6820 case MOVE_LINE_CONTINUED:
6821 /* For continued lines ending in a tab, some of the glyphs
6822 associated with the tab are displayed on the current
6823 line. Since it->current_x does not include these glyphs,
6824 we use it->last_visible_x instead. */
6825 it->continuation_lines_width +=
6826 (it->c == '\t') ? it->last_visible_x : it->current_x;
6827 break;
6828
6829 default:
6830 abort ();
6831 }
6832
6833 /* Reset/increment for the next run. */
6834 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6835 it->current_x = it->hpos = 0;
6836 it->current_y += it->max_ascent + it->max_descent;
6837 ++it->vpos;
6838 last_height = it->max_ascent + it->max_descent;
6839 last_max_ascent = it->max_ascent;
6840 it->max_ascent = it->max_descent = 0;
6841 }
6842
6843 out:
6844
6845 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6846 }
6847
6848
6849 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6850
6851 If DY > 0, move IT backward at least that many pixels. DY = 0
6852 means move IT backward to the preceding line start or BEGV. This
6853 function may move over more than DY pixels if IT->current_y - DY
6854 ends up in the middle of a line; in this case IT->current_y will be
6855 set to the top of the line moved to. */
6856
6857 void
6858 move_it_vertically_backward (it, dy)
6859 struct it *it;
6860 int dy;
6861 {
6862 int nlines, h;
6863 struct it it2, it3;
6864 int start_pos;
6865
6866 move_further_back:
6867 xassert (dy >= 0);
6868
6869 start_pos = IT_CHARPOS (*it);
6870
6871 /* Estimate how many newlines we must move back. */
6872 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6873
6874 /* Set the iterator's position that many lines back. */
6875 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6876 back_to_previous_visible_line_start (it);
6877
6878 /* Reseat the iterator here. When moving backward, we don't want
6879 reseat to skip forward over invisible text, set up the iterator
6880 to deliver from overlay strings at the new position etc. So,
6881 use reseat_1 here. */
6882 reseat_1 (it, it->current.pos, 1);
6883
6884 /* We are now surely at a line start. */
6885 it->current_x = it->hpos = 0;
6886 it->continuation_lines_width = 0;
6887
6888 /* Move forward and see what y-distance we moved. First move to the
6889 start of the next line so that we get its height. We need this
6890 height to be able to tell whether we reached the specified
6891 y-distance. */
6892 it2 = *it;
6893 it2.max_ascent = it2.max_descent = 0;
6894 do
6895 {
6896 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6897 MOVE_TO_POS | MOVE_TO_VPOS);
6898 }
6899 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6900 xassert (IT_CHARPOS (*it) >= BEGV);
6901 it3 = it2;
6902
6903 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6904 xassert (IT_CHARPOS (*it) >= BEGV);
6905 /* H is the actual vertical distance from the position in *IT
6906 and the starting position. */
6907 h = it2.current_y - it->current_y;
6908 /* NLINES is the distance in number of lines. */
6909 nlines = it2.vpos - it->vpos;
6910
6911 /* Correct IT's y and vpos position
6912 so that they are relative to the starting point. */
6913 it->vpos -= nlines;
6914 it->current_y -= h;
6915
6916 if (dy == 0)
6917 {
6918 /* DY == 0 means move to the start of the screen line. The
6919 value of nlines is > 0 if continuation lines were involved. */
6920 if (nlines > 0)
6921 move_it_by_lines (it, nlines, 1);
6922 #if 0
6923 /* I think this assert is bogus if buffer contains
6924 invisible text or images. KFS. */
6925 xassert (IT_CHARPOS (*it) <= start_pos);
6926 #endif
6927 }
6928 else
6929 {
6930 /* The y-position we try to reach, relative to *IT.
6931 Note that H has been subtracted in front of the if-statement. */
6932 int target_y = it->current_y + h - dy;
6933 int y0 = it3.current_y;
6934 int y1 = line_bottom_y (&it3);
6935 int line_height = y1 - y0;
6936
6937 /* If we did not reach target_y, try to move further backward if
6938 we can. If we moved too far backward, try to move forward. */
6939 if (target_y < it->current_y
6940 /* This is heuristic. In a window that's 3 lines high, with
6941 a line height of 13 pixels each, recentering with point
6942 on the bottom line will try to move -39/2 = 19 pixels
6943 backward. Try to avoid moving into the first line. */
6944 && (it->current_y - target_y
6945 > min (window_box_height (it->w), line_height * 2 / 3))
6946 && IT_CHARPOS (*it) > BEGV)
6947 {
6948 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6949 target_y - it->current_y));
6950 dy = it->current_y - target_y;
6951 goto move_further_back;
6952 }
6953 else if (target_y >= it->current_y + line_height
6954 && IT_CHARPOS (*it) < ZV)
6955 {
6956 /* Should move forward by at least one line, maybe more.
6957
6958 Note: Calling move_it_by_lines can be expensive on
6959 terminal frames, where compute_motion is used (via
6960 vmotion) to do the job, when there are very long lines
6961 and truncate-lines is nil. That's the reason for
6962 treating terminal frames specially here. */
6963
6964 if (!FRAME_WINDOW_P (it->f))
6965 move_it_vertically (it, target_y - (it->current_y + line_height));
6966 else
6967 {
6968 do
6969 {
6970 move_it_by_lines (it, 1, 1);
6971 }
6972 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6973 }
6974
6975 #if 0
6976 /* I think this assert is bogus if buffer contains
6977 invisible text or images. KFS. */
6978 xassert (IT_CHARPOS (*it) >= BEGV);
6979 #endif
6980 }
6981 }
6982 }
6983
6984
6985 /* Move IT by a specified amount of pixel lines DY. DY negative means
6986 move backwards. DY = 0 means move to start of screen line. At the
6987 end, IT will be on the start of a screen line. */
6988
6989 void
6990 move_it_vertically (it, dy)
6991 struct it *it;
6992 int dy;
6993 {
6994 if (dy <= 0)
6995 move_it_vertically_backward (it, -dy);
6996 else
6997 {
6998 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6999 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7000 MOVE_TO_POS | MOVE_TO_Y);
7001 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7002
7003 /* If buffer ends in ZV without a newline, move to the start of
7004 the line to satisfy the post-condition. */
7005 if (IT_CHARPOS (*it) == ZV
7006 && ZV > BEGV
7007 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7008 move_it_by_lines (it, 0, 0);
7009 }
7010 }
7011
7012
7013 /* Move iterator IT past the end of the text line it is in. */
7014
7015 void
7016 move_it_past_eol (it)
7017 struct it *it;
7018 {
7019 enum move_it_result rc;
7020
7021 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7022 if (rc == MOVE_NEWLINE_OR_CR)
7023 set_iterator_to_next (it, 0);
7024 }
7025
7026
7027 #if 0 /* Currently not used. */
7028
7029 /* Return non-zero if some text between buffer positions START_CHARPOS
7030 and END_CHARPOS is invisible. IT->window is the window for text
7031 property lookup. */
7032
7033 static int
7034 invisible_text_between_p (it, start_charpos, end_charpos)
7035 struct it *it;
7036 int start_charpos, end_charpos;
7037 {
7038 Lisp_Object prop, limit;
7039 int invisible_found_p;
7040
7041 xassert (it != NULL && start_charpos <= end_charpos);
7042
7043 /* Is text at START invisible? */
7044 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7045 it->window);
7046 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7047 invisible_found_p = 1;
7048 else
7049 {
7050 limit = Fnext_single_char_property_change (make_number (start_charpos),
7051 Qinvisible, Qnil,
7052 make_number (end_charpos));
7053 invisible_found_p = XFASTINT (limit) < end_charpos;
7054 }
7055
7056 return invisible_found_p;
7057 }
7058
7059 #endif /* 0 */
7060
7061
7062 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7063 negative means move up. DVPOS == 0 means move to the start of the
7064 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7065 NEED_Y_P is zero, IT->current_y will be left unchanged.
7066
7067 Further optimization ideas: If we would know that IT->f doesn't use
7068 a face with proportional font, we could be faster for
7069 truncate-lines nil. */
7070
7071 void
7072 move_it_by_lines (it, dvpos, need_y_p)
7073 struct it *it;
7074 int dvpos, need_y_p;
7075 {
7076 struct position pos;
7077
7078 /* The commented-out optimization uses vmotion on terminals. This
7079 gives bad results, because elements like it->what, on which
7080 callers such as pos_visible_p rely, aren't updated. */
7081 /* if (!FRAME_WINDOW_P (it->f))
7082 {
7083 struct text_pos textpos;
7084
7085 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7086 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7087 reseat (it, textpos, 1);
7088 it->vpos += pos.vpos;
7089 it->current_y += pos.vpos;
7090 }
7091 else */
7092
7093 if (dvpos == 0)
7094 {
7095 /* DVPOS == 0 means move to the start of the screen line. */
7096 move_it_vertically_backward (it, 0);
7097 xassert (it->current_x == 0 && it->hpos == 0);
7098 /* Let next call to line_bottom_y calculate real line height */
7099 last_height = 0;
7100 }
7101 else if (dvpos > 0)
7102 {
7103 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7104 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7105 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7106 }
7107 else
7108 {
7109 struct it it2;
7110 int start_charpos, i;
7111
7112 /* Start at the beginning of the screen line containing IT's
7113 position. This may actually move vertically backwards,
7114 in case of overlays, so adjust dvpos accordingly. */
7115 dvpos += it->vpos;
7116 move_it_vertically_backward (it, 0);
7117 dvpos -= it->vpos;
7118
7119 /* Go back -DVPOS visible lines and reseat the iterator there. */
7120 start_charpos = IT_CHARPOS (*it);
7121 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7122 back_to_previous_visible_line_start (it);
7123 reseat (it, it->current.pos, 1);
7124
7125 /* Move further back if we end up in a string or an image. */
7126 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7127 {
7128 /* First try to move to start of display line. */
7129 dvpos += it->vpos;
7130 move_it_vertically_backward (it, 0);
7131 dvpos -= it->vpos;
7132 if (IT_POS_VALID_AFTER_MOVE_P (it))
7133 break;
7134 /* If start of line is still in string or image,
7135 move further back. */
7136 back_to_previous_visible_line_start (it);
7137 reseat (it, it->current.pos, 1);
7138 dvpos--;
7139 }
7140
7141 it->current_x = it->hpos = 0;
7142
7143 /* Above call may have moved too far if continuation lines
7144 are involved. Scan forward and see if it did. */
7145 it2 = *it;
7146 it2.vpos = it2.current_y = 0;
7147 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7148 it->vpos -= it2.vpos;
7149 it->current_y -= it2.current_y;
7150 it->current_x = it->hpos = 0;
7151
7152 /* If we moved too far back, move IT some lines forward. */
7153 if (it2.vpos > -dvpos)
7154 {
7155 int delta = it2.vpos + dvpos;
7156 it2 = *it;
7157 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7158 /* Move back again if we got too far ahead. */
7159 if (IT_CHARPOS (*it) >= start_charpos)
7160 *it = it2;
7161 }
7162 }
7163 }
7164
7165 /* Return 1 if IT points into the middle of a display vector. */
7166
7167 int
7168 in_display_vector_p (it)
7169 struct it *it;
7170 {
7171 return (it->method == GET_FROM_DISPLAY_VECTOR
7172 && it->current.dpvec_index > 0
7173 && it->dpvec + it->current.dpvec_index != it->dpend);
7174 }
7175
7176 \f
7177 /***********************************************************************
7178 Messages
7179 ***********************************************************************/
7180
7181
7182 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7183 to *Messages*. */
7184
7185 void
7186 add_to_log (format, arg1, arg2)
7187 char *format;
7188 Lisp_Object arg1, arg2;
7189 {
7190 Lisp_Object args[3];
7191 Lisp_Object msg, fmt;
7192 char *buffer;
7193 int len;
7194 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7195 USE_SAFE_ALLOCA;
7196
7197 /* Do nothing if called asynchronously. Inserting text into
7198 a buffer may call after-change-functions and alike and
7199 that would means running Lisp asynchronously. */
7200 if (handling_signal)
7201 return;
7202
7203 fmt = msg = Qnil;
7204 GCPRO4 (fmt, msg, arg1, arg2);
7205
7206 args[0] = fmt = build_string (format);
7207 args[1] = arg1;
7208 args[2] = arg2;
7209 msg = Fformat (3, args);
7210
7211 len = SBYTES (msg) + 1;
7212 SAFE_ALLOCA (buffer, char *, len);
7213 bcopy (SDATA (msg), buffer, len);
7214
7215 message_dolog (buffer, len - 1, 1, 0);
7216 SAFE_FREE ();
7217
7218 UNGCPRO;
7219 }
7220
7221
7222 /* Output a newline in the *Messages* buffer if "needs" one. */
7223
7224 void
7225 message_log_maybe_newline ()
7226 {
7227 if (message_log_need_newline)
7228 message_dolog ("", 0, 1, 0);
7229 }
7230
7231
7232 /* Add a string M of length NBYTES to the message log, optionally
7233 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7234 nonzero, means interpret the contents of M as multibyte. This
7235 function calls low-level routines in order to bypass text property
7236 hooks, etc. which might not be safe to run.
7237
7238 This may GC (insert may run before/after change hooks),
7239 so the buffer M must NOT point to a Lisp string. */
7240
7241 void
7242 message_dolog (m, nbytes, nlflag, multibyte)
7243 const char *m;
7244 int nbytes, nlflag, multibyte;
7245 {
7246 if (!NILP (Vmemory_full))
7247 return;
7248
7249 if (!NILP (Vmessage_log_max))
7250 {
7251 struct buffer *oldbuf;
7252 Lisp_Object oldpoint, oldbegv, oldzv;
7253 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7254 int point_at_end = 0;
7255 int zv_at_end = 0;
7256 Lisp_Object old_deactivate_mark, tem;
7257 struct gcpro gcpro1;
7258
7259 old_deactivate_mark = Vdeactivate_mark;
7260 oldbuf = current_buffer;
7261 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7262 current_buffer->undo_list = Qt;
7263
7264 oldpoint = message_dolog_marker1;
7265 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7266 oldbegv = message_dolog_marker2;
7267 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7268 oldzv = message_dolog_marker3;
7269 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7270 GCPRO1 (old_deactivate_mark);
7271
7272 if (PT == Z)
7273 point_at_end = 1;
7274 if (ZV == Z)
7275 zv_at_end = 1;
7276
7277 BEGV = BEG;
7278 BEGV_BYTE = BEG_BYTE;
7279 ZV = Z;
7280 ZV_BYTE = Z_BYTE;
7281 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7282
7283 /* Insert the string--maybe converting multibyte to single byte
7284 or vice versa, so that all the text fits the buffer. */
7285 if (multibyte
7286 && NILP (current_buffer->enable_multibyte_characters))
7287 {
7288 int i, c, char_bytes;
7289 unsigned char work[1];
7290
7291 /* Convert a multibyte string to single-byte
7292 for the *Message* buffer. */
7293 for (i = 0; i < nbytes; i += char_bytes)
7294 {
7295 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7296 work[0] = (SINGLE_BYTE_CHAR_P (c)
7297 ? c
7298 : multibyte_char_to_unibyte (c, Qnil));
7299 insert_1_both (work, 1, 1, 1, 0, 0);
7300 }
7301 }
7302 else if (! multibyte
7303 && ! NILP (current_buffer->enable_multibyte_characters))
7304 {
7305 int i, c, char_bytes;
7306 unsigned char *msg = (unsigned char *) m;
7307 unsigned char str[MAX_MULTIBYTE_LENGTH];
7308 /* Convert a single-byte string to multibyte
7309 for the *Message* buffer. */
7310 for (i = 0; i < nbytes; i++)
7311 {
7312 c = unibyte_char_to_multibyte (msg[i]);
7313 char_bytes = CHAR_STRING (c, str);
7314 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7315 }
7316 }
7317 else if (nbytes)
7318 insert_1 (m, nbytes, 1, 0, 0);
7319
7320 if (nlflag)
7321 {
7322 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7323 insert_1 ("\n", 1, 1, 0, 0);
7324
7325 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7326 this_bol = PT;
7327 this_bol_byte = PT_BYTE;
7328
7329 /* See if this line duplicates the previous one.
7330 If so, combine duplicates. */
7331 if (this_bol > BEG)
7332 {
7333 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7334 prev_bol = PT;
7335 prev_bol_byte = PT_BYTE;
7336
7337 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7338 this_bol, this_bol_byte);
7339 if (dup)
7340 {
7341 del_range_both (prev_bol, prev_bol_byte,
7342 this_bol, this_bol_byte, 0);
7343 if (dup > 1)
7344 {
7345 char dupstr[40];
7346 int duplen;
7347
7348 /* If you change this format, don't forget to also
7349 change message_log_check_duplicate. */
7350 sprintf (dupstr, " [%d times]", dup);
7351 duplen = strlen (dupstr);
7352 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7353 insert_1 (dupstr, duplen, 1, 0, 1);
7354 }
7355 }
7356 }
7357
7358 /* If we have more than the desired maximum number of lines
7359 in the *Messages* buffer now, delete the oldest ones.
7360 This is safe because we don't have undo in this buffer. */
7361
7362 if (NATNUMP (Vmessage_log_max))
7363 {
7364 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7365 -XFASTINT (Vmessage_log_max) - 1, 0);
7366 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7367 }
7368 }
7369 BEGV = XMARKER (oldbegv)->charpos;
7370 BEGV_BYTE = marker_byte_position (oldbegv);
7371
7372 if (zv_at_end)
7373 {
7374 ZV = Z;
7375 ZV_BYTE = Z_BYTE;
7376 }
7377 else
7378 {
7379 ZV = XMARKER (oldzv)->charpos;
7380 ZV_BYTE = marker_byte_position (oldzv);
7381 }
7382
7383 if (point_at_end)
7384 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7385 else
7386 /* We can't do Fgoto_char (oldpoint) because it will run some
7387 Lisp code. */
7388 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7389 XMARKER (oldpoint)->bytepos);
7390
7391 UNGCPRO;
7392 unchain_marker (XMARKER (oldpoint));
7393 unchain_marker (XMARKER (oldbegv));
7394 unchain_marker (XMARKER (oldzv));
7395
7396 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7397 set_buffer_internal (oldbuf);
7398 if (NILP (tem))
7399 windows_or_buffers_changed = old_windows_or_buffers_changed;
7400 message_log_need_newline = !nlflag;
7401 Vdeactivate_mark = old_deactivate_mark;
7402 }
7403 }
7404
7405
7406 /* We are at the end of the buffer after just having inserted a newline.
7407 (Note: We depend on the fact we won't be crossing the gap.)
7408 Check to see if the most recent message looks a lot like the previous one.
7409 Return 0 if different, 1 if the new one should just replace it, or a
7410 value N > 1 if we should also append " [N times]". */
7411
7412 static int
7413 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7414 int prev_bol, this_bol;
7415 int prev_bol_byte, this_bol_byte;
7416 {
7417 int i;
7418 int len = Z_BYTE - 1 - this_bol_byte;
7419 int seen_dots = 0;
7420 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7421 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7422
7423 for (i = 0; i < len; i++)
7424 {
7425 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7426 seen_dots = 1;
7427 if (p1[i] != p2[i])
7428 return seen_dots;
7429 }
7430 p1 += len;
7431 if (*p1 == '\n')
7432 return 2;
7433 if (*p1++ == ' ' && *p1++ == '[')
7434 {
7435 int n = 0;
7436 while (*p1 >= '0' && *p1 <= '9')
7437 n = n * 10 + *p1++ - '0';
7438 if (strncmp (p1, " times]\n", 8) == 0)
7439 return n+1;
7440 }
7441 return 0;
7442 }
7443 \f
7444
7445 /* Display an echo area message M with a specified length of NBYTES
7446 bytes. The string may include null characters. If M is 0, clear
7447 out any existing message, and let the mini-buffer text show
7448 through.
7449
7450 This may GC, so the buffer M must NOT point to a Lisp string. */
7451
7452 void
7453 message2 (m, nbytes, multibyte)
7454 const char *m;
7455 int nbytes;
7456 int multibyte;
7457 {
7458 /* First flush out any partial line written with print. */
7459 message_log_maybe_newline ();
7460 if (m)
7461 message_dolog (m, nbytes, 1, multibyte);
7462 message2_nolog (m, nbytes, multibyte);
7463 }
7464
7465
7466 /* The non-logging counterpart of message2. */
7467
7468 void
7469 message2_nolog (m, nbytes, multibyte)
7470 const char *m;
7471 int nbytes, multibyte;
7472 {
7473 struct frame *sf = SELECTED_FRAME ();
7474 message_enable_multibyte = multibyte;
7475
7476 if (noninteractive)
7477 {
7478 if (noninteractive_need_newline)
7479 putc ('\n', stderr);
7480 noninteractive_need_newline = 0;
7481 if (m)
7482 fwrite (m, nbytes, 1, stderr);
7483 if (cursor_in_echo_area == 0)
7484 fprintf (stderr, "\n");
7485 fflush (stderr);
7486 }
7487 /* A null message buffer means that the frame hasn't really been
7488 initialized yet. Error messages get reported properly by
7489 cmd_error, so this must be just an informative message; toss it. */
7490 else if (INTERACTIVE
7491 && sf->glyphs_initialized_p
7492 && FRAME_MESSAGE_BUF (sf))
7493 {
7494 Lisp_Object mini_window;
7495 struct frame *f;
7496
7497 /* Get the frame containing the mini-buffer
7498 that the selected frame is using. */
7499 mini_window = FRAME_MINIBUF_WINDOW (sf);
7500 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7501
7502 FRAME_SAMPLE_VISIBILITY (f);
7503 if (FRAME_VISIBLE_P (sf)
7504 && ! FRAME_VISIBLE_P (f))
7505 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7506
7507 if (m)
7508 {
7509 set_message (m, Qnil, nbytes, multibyte);
7510 if (minibuffer_auto_raise)
7511 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7512 }
7513 else
7514 clear_message (1, 1);
7515
7516 do_pending_window_change (0);
7517 echo_area_display (1);
7518 do_pending_window_change (0);
7519 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7520 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7521 }
7522 }
7523
7524
7525 /* Display an echo area message M with a specified length of NBYTES
7526 bytes. The string may include null characters. If M is not a
7527 string, clear out any existing message, and let the mini-buffer
7528 text show through.
7529
7530 This function cancels echoing. */
7531
7532 void
7533 message3 (m, nbytes, multibyte)
7534 Lisp_Object m;
7535 int nbytes;
7536 int multibyte;
7537 {
7538 struct gcpro gcpro1;
7539
7540 GCPRO1 (m);
7541 clear_message (1,1);
7542 cancel_echoing ();
7543
7544 /* First flush out any partial line written with print. */
7545 message_log_maybe_newline ();
7546 if (STRINGP (m))
7547 {
7548 char *buffer;
7549 USE_SAFE_ALLOCA;
7550
7551 SAFE_ALLOCA (buffer, char *, nbytes);
7552 bcopy (SDATA (m), buffer, nbytes);
7553 message_dolog (buffer, nbytes, 1, multibyte);
7554 SAFE_FREE ();
7555 }
7556 message3_nolog (m, nbytes, multibyte);
7557
7558 UNGCPRO;
7559 }
7560
7561
7562 /* The non-logging version of message3.
7563 This does not cancel echoing, because it is used for echoing.
7564 Perhaps we need to make a separate function for echoing
7565 and make this cancel echoing. */
7566
7567 void
7568 message3_nolog (m, nbytes, multibyte)
7569 Lisp_Object m;
7570 int nbytes, multibyte;
7571 {
7572 struct frame *sf = SELECTED_FRAME ();
7573 message_enable_multibyte = multibyte;
7574
7575 if (noninteractive)
7576 {
7577 if (noninteractive_need_newline)
7578 putc ('\n', stderr);
7579 noninteractive_need_newline = 0;
7580 if (STRINGP (m))
7581 fwrite (SDATA (m), nbytes, 1, stderr);
7582 if (cursor_in_echo_area == 0)
7583 fprintf (stderr, "\n");
7584 fflush (stderr);
7585 }
7586 /* A null message buffer means that the frame hasn't really been
7587 initialized yet. Error messages get reported properly by
7588 cmd_error, so this must be just an informative message; toss it. */
7589 else if (INTERACTIVE
7590 && sf->glyphs_initialized_p
7591 && FRAME_MESSAGE_BUF (sf))
7592 {
7593 Lisp_Object mini_window;
7594 Lisp_Object frame;
7595 struct frame *f;
7596
7597 /* Get the frame containing the mini-buffer
7598 that the selected frame is using. */
7599 mini_window = FRAME_MINIBUF_WINDOW (sf);
7600 frame = XWINDOW (mini_window)->frame;
7601 f = XFRAME (frame);
7602
7603 FRAME_SAMPLE_VISIBILITY (f);
7604 if (FRAME_VISIBLE_P (sf)
7605 && !FRAME_VISIBLE_P (f))
7606 Fmake_frame_visible (frame);
7607
7608 if (STRINGP (m) && SCHARS (m) > 0)
7609 {
7610 set_message (NULL, m, nbytes, multibyte);
7611 if (minibuffer_auto_raise)
7612 Fraise_frame (frame);
7613 /* Assume we are not echoing.
7614 (If we are, echo_now will override this.) */
7615 echo_message_buffer = Qnil;
7616 }
7617 else
7618 clear_message (1, 1);
7619
7620 do_pending_window_change (0);
7621 echo_area_display (1);
7622 do_pending_window_change (0);
7623 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7624 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7625 }
7626 }
7627
7628
7629 /* Display a null-terminated echo area message M. If M is 0, clear
7630 out any existing message, and let the mini-buffer text show through.
7631
7632 The buffer M must continue to exist until after the echo area gets
7633 cleared or some other message gets displayed there. Do not pass
7634 text that is stored in a Lisp string. Do not pass text in a buffer
7635 that was alloca'd. */
7636
7637 void
7638 message1 (m)
7639 char *m;
7640 {
7641 message2 (m, (m ? strlen (m) : 0), 0);
7642 }
7643
7644
7645 /* The non-logging counterpart of message1. */
7646
7647 void
7648 message1_nolog (m)
7649 char *m;
7650 {
7651 message2_nolog (m, (m ? strlen (m) : 0), 0);
7652 }
7653
7654 /* Display a message M which contains a single %s
7655 which gets replaced with STRING. */
7656
7657 void
7658 message_with_string (m, string, log)
7659 char *m;
7660 Lisp_Object string;
7661 int log;
7662 {
7663 CHECK_STRING (string);
7664
7665 if (noninteractive)
7666 {
7667 if (m)
7668 {
7669 if (noninteractive_need_newline)
7670 putc ('\n', stderr);
7671 noninteractive_need_newline = 0;
7672 fprintf (stderr, m, SDATA (string));
7673 if (cursor_in_echo_area == 0)
7674 fprintf (stderr, "\n");
7675 fflush (stderr);
7676 }
7677 }
7678 else if (INTERACTIVE)
7679 {
7680 /* The frame whose minibuffer we're going to display the message on.
7681 It may be larger than the selected frame, so we need
7682 to use its buffer, not the selected frame's buffer. */
7683 Lisp_Object mini_window;
7684 struct frame *f, *sf = SELECTED_FRAME ();
7685
7686 /* Get the frame containing the minibuffer
7687 that the selected frame is using. */
7688 mini_window = FRAME_MINIBUF_WINDOW (sf);
7689 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7690
7691 /* A null message buffer means that the frame hasn't really been
7692 initialized yet. Error messages get reported properly by
7693 cmd_error, so this must be just an informative message; toss it. */
7694 if (FRAME_MESSAGE_BUF (f))
7695 {
7696 Lisp_Object args[2], message;
7697 struct gcpro gcpro1, gcpro2;
7698
7699 args[0] = build_string (m);
7700 args[1] = message = string;
7701 GCPRO2 (args[0], message);
7702 gcpro1.nvars = 2;
7703
7704 message = Fformat (2, args);
7705
7706 if (log)
7707 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7708 else
7709 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7710
7711 UNGCPRO;
7712
7713 /* Print should start at the beginning of the message
7714 buffer next time. */
7715 message_buf_print = 0;
7716 }
7717 }
7718 }
7719
7720
7721 /* Dump an informative message to the minibuf. If M is 0, clear out
7722 any existing message, and let the mini-buffer text show through. */
7723
7724 /* VARARGS 1 */
7725 void
7726 message (m, a1, a2, a3)
7727 char *m;
7728 EMACS_INT a1, a2, a3;
7729 {
7730 if (noninteractive)
7731 {
7732 if (m)
7733 {
7734 if (noninteractive_need_newline)
7735 putc ('\n', stderr);
7736 noninteractive_need_newline = 0;
7737 fprintf (stderr, m, a1, a2, a3);
7738 if (cursor_in_echo_area == 0)
7739 fprintf (stderr, "\n");
7740 fflush (stderr);
7741 }
7742 }
7743 else if (INTERACTIVE)
7744 {
7745 /* The frame whose mini-buffer we're going to display the message
7746 on. It may be larger than the selected frame, so we need to
7747 use its buffer, not the selected frame's buffer. */
7748 Lisp_Object mini_window;
7749 struct frame *f, *sf = SELECTED_FRAME ();
7750
7751 /* Get the frame containing the mini-buffer
7752 that the selected frame is using. */
7753 mini_window = FRAME_MINIBUF_WINDOW (sf);
7754 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7755
7756 /* A null message buffer means that the frame hasn't really been
7757 initialized yet. Error messages get reported properly by
7758 cmd_error, so this must be just an informative message; toss
7759 it. */
7760 if (FRAME_MESSAGE_BUF (f))
7761 {
7762 if (m)
7763 {
7764 int len;
7765 #ifdef NO_ARG_ARRAY
7766 char *a[3];
7767 a[0] = (char *) a1;
7768 a[1] = (char *) a2;
7769 a[2] = (char *) a3;
7770
7771 len = doprnt (FRAME_MESSAGE_BUF (f),
7772 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7773 #else
7774 len = doprnt (FRAME_MESSAGE_BUF (f),
7775 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7776 (char **) &a1);
7777 #endif /* NO_ARG_ARRAY */
7778
7779 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7780 }
7781 else
7782 message1 (0);
7783
7784 /* Print should start at the beginning of the message
7785 buffer next time. */
7786 message_buf_print = 0;
7787 }
7788 }
7789 }
7790
7791
7792 /* The non-logging version of message. */
7793
7794 void
7795 message_nolog (m, a1, a2, a3)
7796 char *m;
7797 EMACS_INT a1, a2, a3;
7798 {
7799 Lisp_Object old_log_max;
7800 old_log_max = Vmessage_log_max;
7801 Vmessage_log_max = Qnil;
7802 message (m, a1, a2, a3);
7803 Vmessage_log_max = old_log_max;
7804 }
7805
7806
7807 /* Display the current message in the current mini-buffer. This is
7808 only called from error handlers in process.c, and is not time
7809 critical. */
7810
7811 void
7812 update_echo_area ()
7813 {
7814 if (!NILP (echo_area_buffer[0]))
7815 {
7816 Lisp_Object string;
7817 string = Fcurrent_message ();
7818 message3 (string, SBYTES (string),
7819 !NILP (current_buffer->enable_multibyte_characters));
7820 }
7821 }
7822
7823
7824 /* Make sure echo area buffers in `echo_buffers' are live.
7825 If they aren't, make new ones. */
7826
7827 static void
7828 ensure_echo_area_buffers ()
7829 {
7830 int i;
7831
7832 for (i = 0; i < 2; ++i)
7833 if (!BUFFERP (echo_buffer[i])
7834 || NILP (XBUFFER (echo_buffer[i])->name))
7835 {
7836 char name[30];
7837 Lisp_Object old_buffer;
7838 int j;
7839
7840 old_buffer = echo_buffer[i];
7841 sprintf (name, " *Echo Area %d*", i);
7842 echo_buffer[i] = Fget_buffer_create (build_string (name));
7843 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7844
7845 for (j = 0; j < 2; ++j)
7846 if (EQ (old_buffer, echo_area_buffer[j]))
7847 echo_area_buffer[j] = echo_buffer[i];
7848 }
7849 }
7850
7851
7852 /* Call FN with args A1..A4 with either the current or last displayed
7853 echo_area_buffer as current buffer.
7854
7855 WHICH zero means use the current message buffer
7856 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7857 from echo_buffer[] and clear it.
7858
7859 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7860 suitable buffer from echo_buffer[] and clear it.
7861
7862 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7863 that the current message becomes the last displayed one, make
7864 choose a suitable buffer for echo_area_buffer[0], and clear it.
7865
7866 Value is what FN returns. */
7867
7868 static int
7869 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7870 struct window *w;
7871 int which;
7872 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7873 EMACS_INT a1;
7874 Lisp_Object a2;
7875 EMACS_INT a3, a4;
7876 {
7877 Lisp_Object buffer;
7878 int this_one, the_other, clear_buffer_p, rc;
7879 int count = SPECPDL_INDEX ();
7880
7881 /* If buffers aren't live, make new ones. */
7882 ensure_echo_area_buffers ();
7883
7884 clear_buffer_p = 0;
7885
7886 if (which == 0)
7887 this_one = 0, the_other = 1;
7888 else if (which > 0)
7889 this_one = 1, the_other = 0;
7890 else
7891 {
7892 this_one = 0, the_other = 1;
7893 clear_buffer_p = 1;
7894
7895 /* We need a fresh one in case the current echo buffer equals
7896 the one containing the last displayed echo area message. */
7897 if (!NILP (echo_area_buffer[this_one])
7898 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7899 echo_area_buffer[this_one] = Qnil;
7900 }
7901
7902 /* Choose a suitable buffer from echo_buffer[] is we don't
7903 have one. */
7904 if (NILP (echo_area_buffer[this_one]))
7905 {
7906 echo_area_buffer[this_one]
7907 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7908 ? echo_buffer[the_other]
7909 : echo_buffer[this_one]);
7910 clear_buffer_p = 1;
7911 }
7912
7913 buffer = echo_area_buffer[this_one];
7914
7915 /* Don't get confused by reusing the buffer used for echoing
7916 for a different purpose. */
7917 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7918 cancel_echoing ();
7919
7920 record_unwind_protect (unwind_with_echo_area_buffer,
7921 with_echo_area_buffer_unwind_data (w));
7922
7923 /* Make the echo area buffer current. Note that for display
7924 purposes, it is not necessary that the displayed window's buffer
7925 == current_buffer, except for text property lookup. So, let's
7926 only set that buffer temporarily here without doing a full
7927 Fset_window_buffer. We must also change w->pointm, though,
7928 because otherwise an assertions in unshow_buffer fails, and Emacs
7929 aborts. */
7930 set_buffer_internal_1 (XBUFFER (buffer));
7931 if (w)
7932 {
7933 w->buffer = buffer;
7934 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7935 }
7936
7937 current_buffer->undo_list = Qt;
7938 current_buffer->read_only = Qnil;
7939 specbind (Qinhibit_read_only, Qt);
7940 specbind (Qinhibit_modification_hooks, Qt);
7941
7942 if (clear_buffer_p && Z > BEG)
7943 del_range (BEG, Z);
7944
7945 xassert (BEGV >= BEG);
7946 xassert (ZV <= Z && ZV >= BEGV);
7947
7948 rc = fn (a1, a2, a3, a4);
7949
7950 xassert (BEGV >= BEG);
7951 xassert (ZV <= Z && ZV >= BEGV);
7952
7953 unbind_to (count, Qnil);
7954 return rc;
7955 }
7956
7957
7958 /* Save state that should be preserved around the call to the function
7959 FN called in with_echo_area_buffer. */
7960
7961 static Lisp_Object
7962 with_echo_area_buffer_unwind_data (w)
7963 struct window *w;
7964 {
7965 int i = 0;
7966 Lisp_Object vector;
7967
7968 /* Reduce consing by keeping one vector in
7969 Vwith_echo_area_save_vector. */
7970 vector = Vwith_echo_area_save_vector;
7971 Vwith_echo_area_save_vector = Qnil;
7972
7973 if (NILP (vector))
7974 vector = Fmake_vector (make_number (7), Qnil);
7975
7976 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7977 AREF (vector, i) = Vdeactivate_mark, ++i;
7978 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7979
7980 if (w)
7981 {
7982 XSETWINDOW (AREF (vector, i), w); ++i;
7983 AREF (vector, i) = w->buffer; ++i;
7984 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7985 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7986 }
7987 else
7988 {
7989 int end = i + 4;
7990 for (; i < end; ++i)
7991 AREF (vector, i) = Qnil;
7992 }
7993
7994 xassert (i == ASIZE (vector));
7995 return vector;
7996 }
7997
7998
7999 /* Restore global state from VECTOR which was created by
8000 with_echo_area_buffer_unwind_data. */
8001
8002 static Lisp_Object
8003 unwind_with_echo_area_buffer (vector)
8004 Lisp_Object vector;
8005 {
8006 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8007 Vdeactivate_mark = AREF (vector, 1);
8008 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8009
8010 if (WINDOWP (AREF (vector, 3)))
8011 {
8012 struct window *w;
8013 Lisp_Object buffer, charpos, bytepos;
8014
8015 w = XWINDOW (AREF (vector, 3));
8016 buffer = AREF (vector, 4);
8017 charpos = AREF (vector, 5);
8018 bytepos = AREF (vector, 6);
8019
8020 w->buffer = buffer;
8021 set_marker_both (w->pointm, buffer,
8022 XFASTINT (charpos), XFASTINT (bytepos));
8023 }
8024
8025 Vwith_echo_area_save_vector = vector;
8026 return Qnil;
8027 }
8028
8029
8030 /* Set up the echo area for use by print functions. MULTIBYTE_P
8031 non-zero means we will print multibyte. */
8032
8033 void
8034 setup_echo_area_for_printing (multibyte_p)
8035 int multibyte_p;
8036 {
8037 /* If we can't find an echo area any more, exit. */
8038 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8039 Fkill_emacs (Qnil);
8040
8041 ensure_echo_area_buffers ();
8042
8043 if (!message_buf_print)
8044 {
8045 /* A message has been output since the last time we printed.
8046 Choose a fresh echo area buffer. */
8047 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8048 echo_area_buffer[0] = echo_buffer[1];
8049 else
8050 echo_area_buffer[0] = echo_buffer[0];
8051
8052 /* Switch to that buffer and clear it. */
8053 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8054 current_buffer->truncate_lines = Qnil;
8055
8056 if (Z > BEG)
8057 {
8058 int count = SPECPDL_INDEX ();
8059 specbind (Qinhibit_read_only, Qt);
8060 /* Note that undo recording is always disabled. */
8061 del_range (BEG, Z);
8062 unbind_to (count, Qnil);
8063 }
8064 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8065
8066 /* Set up the buffer for the multibyteness we need. */
8067 if (multibyte_p
8068 != !NILP (current_buffer->enable_multibyte_characters))
8069 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8070
8071 /* Raise the frame containing the echo area. */
8072 if (minibuffer_auto_raise)
8073 {
8074 struct frame *sf = SELECTED_FRAME ();
8075 Lisp_Object mini_window;
8076 mini_window = FRAME_MINIBUF_WINDOW (sf);
8077 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8078 }
8079
8080 message_log_maybe_newline ();
8081 message_buf_print = 1;
8082 }
8083 else
8084 {
8085 if (NILP (echo_area_buffer[0]))
8086 {
8087 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8088 echo_area_buffer[0] = echo_buffer[1];
8089 else
8090 echo_area_buffer[0] = echo_buffer[0];
8091 }
8092
8093 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8094 {
8095 /* Someone switched buffers between print requests. */
8096 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8097 current_buffer->truncate_lines = Qnil;
8098 }
8099 }
8100 }
8101
8102
8103 /* Display an echo area message in window W. Value is non-zero if W's
8104 height is changed. If display_last_displayed_message_p is
8105 non-zero, display the message that was last displayed, otherwise
8106 display the current message. */
8107
8108 static int
8109 display_echo_area (w)
8110 struct window *w;
8111 {
8112 int i, no_message_p, window_height_changed_p, count;
8113
8114 /* Temporarily disable garbage collections while displaying the echo
8115 area. This is done because a GC can print a message itself.
8116 That message would modify the echo area buffer's contents while a
8117 redisplay of the buffer is going on, and seriously confuse
8118 redisplay. */
8119 count = inhibit_garbage_collection ();
8120
8121 /* If there is no message, we must call display_echo_area_1
8122 nevertheless because it resizes the window. But we will have to
8123 reset the echo_area_buffer in question to nil at the end because
8124 with_echo_area_buffer will sets it to an empty buffer. */
8125 i = display_last_displayed_message_p ? 1 : 0;
8126 no_message_p = NILP (echo_area_buffer[i]);
8127
8128 window_height_changed_p
8129 = with_echo_area_buffer (w, display_last_displayed_message_p,
8130 display_echo_area_1,
8131 (EMACS_INT) w, Qnil, 0, 0);
8132
8133 if (no_message_p)
8134 echo_area_buffer[i] = Qnil;
8135
8136 unbind_to (count, Qnil);
8137 return window_height_changed_p;
8138 }
8139
8140
8141 /* Helper for display_echo_area. Display the current buffer which
8142 contains the current echo area message in window W, a mini-window,
8143 a pointer to which is passed in A1. A2..A4 are currently not used.
8144 Change the height of W so that all of the message is displayed.
8145 Value is non-zero if height of W was changed. */
8146
8147 static int
8148 display_echo_area_1 (a1, a2, a3, a4)
8149 EMACS_INT a1;
8150 Lisp_Object a2;
8151 EMACS_INT a3, a4;
8152 {
8153 struct window *w = (struct window *) a1;
8154 Lisp_Object window;
8155 struct text_pos start;
8156 int window_height_changed_p = 0;
8157
8158 /* Do this before displaying, so that we have a large enough glyph
8159 matrix for the display. If we can't get enough space for the
8160 whole text, display the last N lines. That works by setting w->start. */
8161 window_height_changed_p = resize_mini_window (w, 0);
8162
8163 /* Use the starting position chosen by resize_mini_window. */
8164 SET_TEXT_POS_FROM_MARKER (start, w->start);
8165
8166 /* Display. */
8167 clear_glyph_matrix (w->desired_matrix);
8168 XSETWINDOW (window, w);
8169 try_window (window, start, 0);
8170
8171 return window_height_changed_p;
8172 }
8173
8174
8175 /* Resize the echo area window to exactly the size needed for the
8176 currently displayed message, if there is one. If a mini-buffer
8177 is active, don't shrink it. */
8178
8179 void
8180 resize_echo_area_exactly ()
8181 {
8182 if (BUFFERP (echo_area_buffer[0])
8183 && WINDOWP (echo_area_window))
8184 {
8185 struct window *w = XWINDOW (echo_area_window);
8186 int resized_p;
8187 Lisp_Object resize_exactly;
8188
8189 if (minibuf_level == 0)
8190 resize_exactly = Qt;
8191 else
8192 resize_exactly = Qnil;
8193
8194 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8195 (EMACS_INT) w, resize_exactly, 0, 0);
8196 if (resized_p)
8197 {
8198 ++windows_or_buffers_changed;
8199 ++update_mode_lines;
8200 redisplay_internal (0);
8201 }
8202 }
8203 }
8204
8205
8206 /* Callback function for with_echo_area_buffer, when used from
8207 resize_echo_area_exactly. A1 contains a pointer to the window to
8208 resize, EXACTLY non-nil means resize the mini-window exactly to the
8209 size of the text displayed. A3 and A4 are not used. Value is what
8210 resize_mini_window returns. */
8211
8212 static int
8213 resize_mini_window_1 (a1, exactly, a3, a4)
8214 EMACS_INT a1;
8215 Lisp_Object exactly;
8216 EMACS_INT a3, a4;
8217 {
8218 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8219 }
8220
8221
8222 /* Resize mini-window W to fit the size of its contents. EXACT:P
8223 means size the window exactly to the size needed. Otherwise, it's
8224 only enlarged until W's buffer is empty.
8225
8226 Set W->start to the right place to begin display. If the whole
8227 contents fit, start at the beginning. Otherwise, start so as
8228 to make the end of the contents appear. This is particularly
8229 important for y-or-n-p, but seems desirable generally.
8230
8231 Value is non-zero if the window height has been changed. */
8232
8233 int
8234 resize_mini_window (w, exact_p)
8235 struct window *w;
8236 int exact_p;
8237 {
8238 struct frame *f = XFRAME (w->frame);
8239 int window_height_changed_p = 0;
8240
8241 xassert (MINI_WINDOW_P (w));
8242
8243 /* By default, start display at the beginning. */
8244 set_marker_both (w->start, w->buffer,
8245 BUF_BEGV (XBUFFER (w->buffer)),
8246 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8247
8248 /* Don't resize windows while redisplaying a window; it would
8249 confuse redisplay functions when the size of the window they are
8250 displaying changes from under them. Such a resizing can happen,
8251 for instance, when which-func prints a long message while
8252 we are running fontification-functions. We're running these
8253 functions with safe_call which binds inhibit-redisplay to t. */
8254 if (!NILP (Vinhibit_redisplay))
8255 return 0;
8256
8257 /* Nil means don't try to resize. */
8258 if (NILP (Vresize_mini_windows)
8259 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8260 return 0;
8261
8262 if (!FRAME_MINIBUF_ONLY_P (f))
8263 {
8264 struct it it;
8265 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8266 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8267 int height, max_height;
8268 int unit = FRAME_LINE_HEIGHT (f);
8269 struct text_pos start;
8270 struct buffer *old_current_buffer = NULL;
8271
8272 if (current_buffer != XBUFFER (w->buffer))
8273 {
8274 old_current_buffer = current_buffer;
8275 set_buffer_internal (XBUFFER (w->buffer));
8276 }
8277
8278 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8279
8280 /* Compute the max. number of lines specified by the user. */
8281 if (FLOATP (Vmax_mini_window_height))
8282 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8283 else if (INTEGERP (Vmax_mini_window_height))
8284 max_height = XINT (Vmax_mini_window_height);
8285 else
8286 max_height = total_height / 4;
8287
8288 /* Correct that max. height if it's bogus. */
8289 max_height = max (1, max_height);
8290 max_height = min (total_height, max_height);
8291
8292 /* Find out the height of the text in the window. */
8293 if (it.truncate_lines_p)
8294 height = 1;
8295 else
8296 {
8297 last_height = 0;
8298 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8299 if (it.max_ascent == 0 && it.max_descent == 0)
8300 height = it.current_y + last_height;
8301 else
8302 height = it.current_y + it.max_ascent + it.max_descent;
8303 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8304 height = (height + unit - 1) / unit;
8305 }
8306
8307 /* Compute a suitable window start. */
8308 if (height > max_height)
8309 {
8310 height = max_height;
8311 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8312 move_it_vertically_backward (&it, (height - 1) * unit);
8313 start = it.current.pos;
8314 }
8315 else
8316 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8317 SET_MARKER_FROM_TEXT_POS (w->start, start);
8318
8319 if (EQ (Vresize_mini_windows, Qgrow_only))
8320 {
8321 /* Let it grow only, until we display an empty message, in which
8322 case the window shrinks again. */
8323 if (height > WINDOW_TOTAL_LINES (w))
8324 {
8325 int old_height = WINDOW_TOTAL_LINES (w);
8326 freeze_window_starts (f, 1);
8327 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8328 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8329 }
8330 else if (height < WINDOW_TOTAL_LINES (w)
8331 && (exact_p || BEGV == ZV))
8332 {
8333 int old_height = WINDOW_TOTAL_LINES (w);
8334 freeze_window_starts (f, 0);
8335 shrink_mini_window (w);
8336 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8337 }
8338 }
8339 else
8340 {
8341 /* Always resize to exact size needed. */
8342 if (height > WINDOW_TOTAL_LINES (w))
8343 {
8344 int old_height = WINDOW_TOTAL_LINES (w);
8345 freeze_window_starts (f, 1);
8346 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8347 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8348 }
8349 else if (height < WINDOW_TOTAL_LINES (w))
8350 {
8351 int old_height = WINDOW_TOTAL_LINES (w);
8352 freeze_window_starts (f, 0);
8353 shrink_mini_window (w);
8354
8355 if (height)
8356 {
8357 freeze_window_starts (f, 1);
8358 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8359 }
8360
8361 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8362 }
8363 }
8364
8365 if (old_current_buffer)
8366 set_buffer_internal (old_current_buffer);
8367 }
8368
8369 return window_height_changed_p;
8370 }
8371
8372
8373 /* Value is the current message, a string, or nil if there is no
8374 current message. */
8375
8376 Lisp_Object
8377 current_message ()
8378 {
8379 Lisp_Object msg;
8380
8381 if (NILP (echo_area_buffer[0]))
8382 msg = Qnil;
8383 else
8384 {
8385 with_echo_area_buffer (0, 0, current_message_1,
8386 (EMACS_INT) &msg, Qnil, 0, 0);
8387 if (NILP (msg))
8388 echo_area_buffer[0] = Qnil;
8389 }
8390
8391 return msg;
8392 }
8393
8394
8395 static int
8396 current_message_1 (a1, a2, a3, a4)
8397 EMACS_INT a1;
8398 Lisp_Object a2;
8399 EMACS_INT a3, a4;
8400 {
8401 Lisp_Object *msg = (Lisp_Object *) a1;
8402
8403 if (Z > BEG)
8404 *msg = make_buffer_string (BEG, Z, 1);
8405 else
8406 *msg = Qnil;
8407 return 0;
8408 }
8409
8410
8411 /* Push the current message on Vmessage_stack for later restauration
8412 by restore_message. Value is non-zero if the current message isn't
8413 empty. This is a relatively infrequent operation, so it's not
8414 worth optimizing. */
8415
8416 int
8417 push_message ()
8418 {
8419 Lisp_Object msg;
8420 msg = current_message ();
8421 Vmessage_stack = Fcons (msg, Vmessage_stack);
8422 return STRINGP (msg);
8423 }
8424
8425
8426 /* Restore message display from the top of Vmessage_stack. */
8427
8428 void
8429 restore_message ()
8430 {
8431 Lisp_Object msg;
8432
8433 xassert (CONSP (Vmessage_stack));
8434 msg = XCAR (Vmessage_stack);
8435 if (STRINGP (msg))
8436 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8437 else
8438 message3_nolog (msg, 0, 0);
8439 }
8440
8441
8442 /* Handler for record_unwind_protect calling pop_message. */
8443
8444 Lisp_Object
8445 pop_message_unwind (dummy)
8446 Lisp_Object dummy;
8447 {
8448 pop_message ();
8449 return Qnil;
8450 }
8451
8452 /* Pop the top-most entry off Vmessage_stack. */
8453
8454 void
8455 pop_message ()
8456 {
8457 xassert (CONSP (Vmessage_stack));
8458 Vmessage_stack = XCDR (Vmessage_stack);
8459 }
8460
8461
8462 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8463 exits. If the stack is not empty, we have a missing pop_message
8464 somewhere. */
8465
8466 void
8467 check_message_stack ()
8468 {
8469 if (!NILP (Vmessage_stack))
8470 abort ();
8471 }
8472
8473
8474 /* Truncate to NCHARS what will be displayed in the echo area the next
8475 time we display it---but don't redisplay it now. */
8476
8477 void
8478 truncate_echo_area (nchars)
8479 int nchars;
8480 {
8481 if (nchars == 0)
8482 echo_area_buffer[0] = Qnil;
8483 /* A null message buffer means that the frame hasn't really been
8484 initialized yet. Error messages get reported properly by
8485 cmd_error, so this must be just an informative message; toss it. */
8486 else if (!noninteractive
8487 && INTERACTIVE
8488 && !NILP (echo_area_buffer[0]))
8489 {
8490 struct frame *sf = SELECTED_FRAME ();
8491 if (FRAME_MESSAGE_BUF (sf))
8492 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8493 }
8494 }
8495
8496
8497 /* Helper function for truncate_echo_area. Truncate the current
8498 message to at most NCHARS characters. */
8499
8500 static int
8501 truncate_message_1 (nchars, a2, a3, a4)
8502 EMACS_INT nchars;
8503 Lisp_Object a2;
8504 EMACS_INT a3, a4;
8505 {
8506 if (BEG + nchars < Z)
8507 del_range (BEG + nchars, Z);
8508 if (Z == BEG)
8509 echo_area_buffer[0] = Qnil;
8510 return 0;
8511 }
8512
8513
8514 /* Set the current message to a substring of S or STRING.
8515
8516 If STRING is a Lisp string, set the message to the first NBYTES
8517 bytes from STRING. NBYTES zero means use the whole string. If
8518 STRING is multibyte, the message will be displayed multibyte.
8519
8520 If S is not null, set the message to the first LEN bytes of S. LEN
8521 zero means use the whole string. MULTIBYTE_P non-zero means S is
8522 multibyte. Display the message multibyte in that case.
8523
8524 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8525 to t before calling set_message_1 (which calls insert).
8526 */
8527
8528 void
8529 set_message (s, string, nbytes, multibyte_p)
8530 const char *s;
8531 Lisp_Object string;
8532 int nbytes, multibyte_p;
8533 {
8534 message_enable_multibyte
8535 = ((s && multibyte_p)
8536 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8537
8538 with_echo_area_buffer (0, -1, set_message_1,
8539 (EMACS_INT) s, string, nbytes, multibyte_p);
8540 message_buf_print = 0;
8541 help_echo_showing_p = 0;
8542 }
8543
8544
8545 /* Helper function for set_message. Arguments have the same meaning
8546 as there, with A1 corresponding to S and A2 corresponding to STRING
8547 This function is called with the echo area buffer being
8548 current. */
8549
8550 static int
8551 set_message_1 (a1, a2, nbytes, multibyte_p)
8552 EMACS_INT a1;
8553 Lisp_Object a2;
8554 EMACS_INT nbytes, multibyte_p;
8555 {
8556 const char *s = (const char *) a1;
8557 Lisp_Object string = a2;
8558
8559 /* Change multibyteness of the echo buffer appropriately. */
8560 if (message_enable_multibyte
8561 != !NILP (current_buffer->enable_multibyte_characters))
8562 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8563
8564 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8565
8566 /* Insert new message at BEG. */
8567 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8568
8569 if (STRINGP (string))
8570 {
8571 int nchars;
8572
8573 if (nbytes == 0)
8574 nbytes = SBYTES (string);
8575 nchars = string_byte_to_char (string, nbytes);
8576
8577 /* This function takes care of single/multibyte conversion. We
8578 just have to ensure that the echo area buffer has the right
8579 setting of enable_multibyte_characters. */
8580 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8581 }
8582 else if (s)
8583 {
8584 if (nbytes == 0)
8585 nbytes = strlen (s);
8586
8587 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8588 {
8589 /* Convert from multi-byte to single-byte. */
8590 int i, c, n;
8591 unsigned char work[1];
8592
8593 /* Convert a multibyte string to single-byte. */
8594 for (i = 0; i < nbytes; i += n)
8595 {
8596 c = string_char_and_length (s + i, nbytes - i, &n);
8597 work[0] = (SINGLE_BYTE_CHAR_P (c)
8598 ? c
8599 : multibyte_char_to_unibyte (c, Qnil));
8600 insert_1_both (work, 1, 1, 1, 0, 0);
8601 }
8602 }
8603 else if (!multibyte_p
8604 && !NILP (current_buffer->enable_multibyte_characters))
8605 {
8606 /* Convert from single-byte to multi-byte. */
8607 int i, c, n;
8608 const unsigned char *msg = (const unsigned char *) s;
8609 unsigned char str[MAX_MULTIBYTE_LENGTH];
8610
8611 /* Convert a single-byte string to multibyte. */
8612 for (i = 0; i < nbytes; i++)
8613 {
8614 c = unibyte_char_to_multibyte (msg[i]);
8615 n = CHAR_STRING (c, str);
8616 insert_1_both (str, 1, n, 1, 0, 0);
8617 }
8618 }
8619 else
8620 insert_1 (s, nbytes, 1, 0, 0);
8621 }
8622
8623 return 0;
8624 }
8625
8626
8627 /* Clear messages. CURRENT_P non-zero means clear the current
8628 message. LAST_DISPLAYED_P non-zero means clear the message
8629 last displayed. */
8630
8631 void
8632 clear_message (current_p, last_displayed_p)
8633 int current_p, last_displayed_p;
8634 {
8635 if (current_p)
8636 {
8637 echo_area_buffer[0] = Qnil;
8638 message_cleared_p = 1;
8639 }
8640
8641 if (last_displayed_p)
8642 echo_area_buffer[1] = Qnil;
8643
8644 message_buf_print = 0;
8645 }
8646
8647 /* Clear garbaged frames.
8648
8649 This function is used where the old redisplay called
8650 redraw_garbaged_frames which in turn called redraw_frame which in
8651 turn called clear_frame. The call to clear_frame was a source of
8652 flickering. I believe a clear_frame is not necessary. It should
8653 suffice in the new redisplay to invalidate all current matrices,
8654 and ensure a complete redisplay of all windows. */
8655
8656 static void
8657 clear_garbaged_frames ()
8658 {
8659 if (frame_garbaged)
8660 {
8661 Lisp_Object tail, frame;
8662 int changed_count = 0;
8663
8664 FOR_EACH_FRAME (tail, frame)
8665 {
8666 struct frame *f = XFRAME (frame);
8667
8668 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8669 {
8670 if (f->resized_p)
8671 {
8672 Fredraw_frame (frame);
8673 f->force_flush_display_p = 1;
8674 }
8675 clear_current_matrices (f);
8676 changed_count++;
8677 f->garbaged = 0;
8678 f->resized_p = 0;
8679 }
8680 }
8681
8682 frame_garbaged = 0;
8683 if (changed_count)
8684 ++windows_or_buffers_changed;
8685 }
8686 }
8687
8688
8689 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8690 is non-zero update selected_frame. Value is non-zero if the
8691 mini-windows height has been changed. */
8692
8693 static int
8694 echo_area_display (update_frame_p)
8695 int update_frame_p;
8696 {
8697 Lisp_Object mini_window;
8698 struct window *w;
8699 struct frame *f;
8700 int window_height_changed_p = 0;
8701 struct frame *sf = SELECTED_FRAME ();
8702
8703 mini_window = FRAME_MINIBUF_WINDOW (sf);
8704 w = XWINDOW (mini_window);
8705 f = XFRAME (WINDOW_FRAME (w));
8706
8707 /* Don't display if frame is invisible or not yet initialized. */
8708 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8709 return 0;
8710
8711 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8712 #ifndef MAC_OS8
8713 #ifdef HAVE_WINDOW_SYSTEM
8714 /* When Emacs starts, selected_frame may be the initial terminal
8715 frame. If we let this through, a message would be displayed on
8716 the terminal. */
8717 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8718 return 0;
8719 #endif /* HAVE_WINDOW_SYSTEM */
8720 #endif
8721
8722 /* Redraw garbaged frames. */
8723 if (frame_garbaged)
8724 clear_garbaged_frames ();
8725
8726 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8727 {
8728 echo_area_window = mini_window;
8729 window_height_changed_p = display_echo_area (w);
8730 w->must_be_updated_p = 1;
8731
8732 /* Update the display, unless called from redisplay_internal.
8733 Also don't update the screen during redisplay itself. The
8734 update will happen at the end of redisplay, and an update
8735 here could cause confusion. */
8736 if (update_frame_p && !redisplaying_p)
8737 {
8738 int n = 0;
8739
8740 /* If the display update has been interrupted by pending
8741 input, update mode lines in the frame. Due to the
8742 pending input, it might have been that redisplay hasn't
8743 been called, so that mode lines above the echo area are
8744 garbaged. This looks odd, so we prevent it here. */
8745 if (!display_completed)
8746 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8747
8748 if (window_height_changed_p
8749 /* Don't do this if Emacs is shutting down. Redisplay
8750 needs to run hooks. */
8751 && !NILP (Vrun_hooks))
8752 {
8753 /* Must update other windows. Likewise as in other
8754 cases, don't let this update be interrupted by
8755 pending input. */
8756 int count = SPECPDL_INDEX ();
8757 specbind (Qredisplay_dont_pause, Qt);
8758 windows_or_buffers_changed = 1;
8759 redisplay_internal (0);
8760 unbind_to (count, Qnil);
8761 }
8762 else if (FRAME_WINDOW_P (f) && n == 0)
8763 {
8764 /* Window configuration is the same as before.
8765 Can do with a display update of the echo area,
8766 unless we displayed some mode lines. */
8767 update_single_window (w, 1);
8768 FRAME_RIF (f)->flush_display (f);
8769 }
8770 else
8771 update_frame (f, 1, 1);
8772
8773 /* If cursor is in the echo area, make sure that the next
8774 redisplay displays the minibuffer, so that the cursor will
8775 be replaced with what the minibuffer wants. */
8776 if (cursor_in_echo_area)
8777 ++windows_or_buffers_changed;
8778 }
8779 }
8780 else if (!EQ (mini_window, selected_window))
8781 windows_or_buffers_changed++;
8782
8783 /* Last displayed message is now the current message. */
8784 echo_area_buffer[1] = echo_area_buffer[0];
8785 /* Inform read_char that we're not echoing. */
8786 echo_message_buffer = Qnil;
8787
8788 /* Prevent redisplay optimization in redisplay_internal by resetting
8789 this_line_start_pos. This is done because the mini-buffer now
8790 displays the message instead of its buffer text. */
8791 if (EQ (mini_window, selected_window))
8792 CHARPOS (this_line_start_pos) = 0;
8793
8794 return window_height_changed_p;
8795 }
8796
8797
8798 \f
8799 /***********************************************************************
8800 Mode Lines and Frame Titles
8801 ***********************************************************************/
8802
8803 /* A buffer for constructing non-propertized mode-line strings and
8804 frame titles in it; allocated from the heap in init_xdisp and
8805 resized as needed in store_mode_line_noprop_char. */
8806
8807 static char *mode_line_noprop_buf;
8808
8809 /* The buffer's end, and a current output position in it. */
8810
8811 static char *mode_line_noprop_buf_end;
8812 static char *mode_line_noprop_ptr;
8813
8814 #define MODE_LINE_NOPROP_LEN(start) \
8815 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8816
8817 static enum {
8818 MODE_LINE_DISPLAY = 0,
8819 MODE_LINE_TITLE,
8820 MODE_LINE_NOPROP,
8821 MODE_LINE_STRING
8822 } mode_line_target;
8823
8824 /* Alist that caches the results of :propertize.
8825 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8826 static Lisp_Object mode_line_proptrans_alist;
8827
8828 /* List of strings making up the mode-line. */
8829 static Lisp_Object mode_line_string_list;
8830
8831 /* Base face property when building propertized mode line string. */
8832 static Lisp_Object mode_line_string_face;
8833 static Lisp_Object mode_line_string_face_prop;
8834
8835
8836 /* Unwind data for mode line strings */
8837
8838 static Lisp_Object Vmode_line_unwind_vector;
8839
8840 static Lisp_Object
8841 format_mode_line_unwind_data (obuf, save_proptrans)
8842 struct buffer *obuf;
8843 {
8844 Lisp_Object vector;
8845
8846 /* Reduce consing by keeping one vector in
8847 Vwith_echo_area_save_vector. */
8848 vector = Vmode_line_unwind_vector;
8849 Vmode_line_unwind_vector = Qnil;
8850
8851 if (NILP (vector))
8852 vector = Fmake_vector (make_number (7), Qnil);
8853
8854 AREF (vector, 0) = make_number (mode_line_target);
8855 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8856 AREF (vector, 2) = mode_line_string_list;
8857 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8858 AREF (vector, 4) = mode_line_string_face;
8859 AREF (vector, 5) = mode_line_string_face_prop;
8860
8861 if (obuf)
8862 XSETBUFFER (AREF (vector, 6), obuf);
8863 else
8864 AREF (vector, 6) = Qnil;
8865
8866 return vector;
8867 }
8868
8869 static Lisp_Object
8870 unwind_format_mode_line (vector)
8871 Lisp_Object vector;
8872 {
8873 mode_line_target = XINT (AREF (vector, 0));
8874 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8875 mode_line_string_list = AREF (vector, 2);
8876 if (! EQ (AREF (vector, 3), Qt))
8877 mode_line_proptrans_alist = AREF (vector, 3);
8878 mode_line_string_face = AREF (vector, 4);
8879 mode_line_string_face_prop = AREF (vector, 5);
8880
8881 if (!NILP (AREF (vector, 6)))
8882 {
8883 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8884 AREF (vector, 6) = Qnil;
8885 }
8886
8887 Vmode_line_unwind_vector = vector;
8888 return Qnil;
8889 }
8890
8891
8892 /* Store a single character C for the frame title in mode_line_noprop_buf.
8893 Re-allocate mode_line_noprop_buf if necessary. */
8894
8895 static void
8896 #ifdef PROTOTYPES
8897 store_mode_line_noprop_char (char c)
8898 #else
8899 store_mode_line_noprop_char (c)
8900 char c;
8901 #endif
8902 {
8903 /* If output position has reached the end of the allocated buffer,
8904 double the buffer's size. */
8905 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8906 {
8907 int len = MODE_LINE_NOPROP_LEN (0);
8908 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8909 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8910 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8911 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8912 }
8913
8914 *mode_line_noprop_ptr++ = c;
8915 }
8916
8917
8918 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8919 mode_line_noprop_ptr. STR is the string to store. Do not copy
8920 characters that yield more columns than PRECISION; PRECISION <= 0
8921 means copy the whole string. Pad with spaces until FIELD_WIDTH
8922 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8923 pad. Called from display_mode_element when it is used to build a
8924 frame title. */
8925
8926 static int
8927 store_mode_line_noprop (str, field_width, precision)
8928 const unsigned char *str;
8929 int field_width, precision;
8930 {
8931 int n = 0;
8932 int dummy, nbytes;
8933
8934 /* Copy at most PRECISION chars from STR. */
8935 nbytes = strlen (str);
8936 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8937 while (nbytes--)
8938 store_mode_line_noprop_char (*str++);
8939
8940 /* Fill up with spaces until FIELD_WIDTH reached. */
8941 while (field_width > 0
8942 && n < field_width)
8943 {
8944 store_mode_line_noprop_char (' ');
8945 ++n;
8946 }
8947
8948 return n;
8949 }
8950
8951 /***********************************************************************
8952 Frame Titles
8953 ***********************************************************************/
8954
8955 #ifdef HAVE_WINDOW_SYSTEM
8956
8957 /* Set the title of FRAME, if it has changed. The title format is
8958 Vicon_title_format if FRAME is iconified, otherwise it is
8959 frame_title_format. */
8960
8961 static void
8962 x_consider_frame_title (frame)
8963 Lisp_Object frame;
8964 {
8965 struct frame *f = XFRAME (frame);
8966
8967 if (FRAME_WINDOW_P (f)
8968 || FRAME_MINIBUF_ONLY_P (f)
8969 || f->explicit_name)
8970 {
8971 /* Do we have more than one visible frame on this X display? */
8972 Lisp_Object tail;
8973 Lisp_Object fmt;
8974 int title_start;
8975 char *title;
8976 int len;
8977 struct it it;
8978 int count = SPECPDL_INDEX ();
8979
8980 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8981 {
8982 Lisp_Object other_frame = XCAR (tail);
8983 struct frame *tf = XFRAME (other_frame);
8984
8985 if (tf != f
8986 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8987 && !FRAME_MINIBUF_ONLY_P (tf)
8988 && !EQ (other_frame, tip_frame)
8989 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8990 break;
8991 }
8992
8993 /* Set global variable indicating that multiple frames exist. */
8994 multiple_frames = CONSP (tail);
8995
8996 /* Switch to the buffer of selected window of the frame. Set up
8997 mode_line_target so that display_mode_element will output into
8998 mode_line_noprop_buf; then display the title. */
8999 record_unwind_protect (unwind_format_mode_line,
9000 format_mode_line_unwind_data (current_buffer, 0));
9001
9002 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9003 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9004
9005 mode_line_target = MODE_LINE_TITLE;
9006 title_start = MODE_LINE_NOPROP_LEN (0);
9007 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9008 NULL, DEFAULT_FACE_ID);
9009 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9010 len = MODE_LINE_NOPROP_LEN (title_start);
9011 title = mode_line_noprop_buf + title_start;
9012 unbind_to (count, Qnil);
9013
9014 /* Set the title only if it's changed. This avoids consing in
9015 the common case where it hasn't. (If it turns out that we've
9016 already wasted too much time by walking through the list with
9017 display_mode_element, then we might need to optimize at a
9018 higher level than this.) */
9019 if (! STRINGP (f->name)
9020 || SBYTES (f->name) != len
9021 || bcmp (title, SDATA (f->name), len) != 0)
9022 x_implicitly_set_name (f, make_string (title, len), Qnil);
9023 }
9024 }
9025
9026 #endif /* not HAVE_WINDOW_SYSTEM */
9027
9028
9029
9030 \f
9031 /***********************************************************************
9032 Menu Bars
9033 ***********************************************************************/
9034
9035
9036 /* Prepare for redisplay by updating menu-bar item lists when
9037 appropriate. This can call eval. */
9038
9039 void
9040 prepare_menu_bars ()
9041 {
9042 int all_windows;
9043 struct gcpro gcpro1, gcpro2;
9044 struct frame *f;
9045 Lisp_Object tooltip_frame;
9046
9047 #ifdef HAVE_WINDOW_SYSTEM
9048 tooltip_frame = tip_frame;
9049 #else
9050 tooltip_frame = Qnil;
9051 #endif
9052
9053 /* Update all frame titles based on their buffer names, etc. We do
9054 this before the menu bars so that the buffer-menu will show the
9055 up-to-date frame titles. */
9056 #ifdef HAVE_WINDOW_SYSTEM
9057 if (windows_or_buffers_changed || update_mode_lines)
9058 {
9059 Lisp_Object tail, frame;
9060
9061 FOR_EACH_FRAME (tail, frame)
9062 {
9063 f = XFRAME (frame);
9064 if (!EQ (frame, tooltip_frame)
9065 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9066 x_consider_frame_title (frame);
9067 }
9068 }
9069 #endif /* HAVE_WINDOW_SYSTEM */
9070
9071 /* Update the menu bar item lists, if appropriate. This has to be
9072 done before any actual redisplay or generation of display lines. */
9073 all_windows = (update_mode_lines
9074 || buffer_shared > 1
9075 || windows_or_buffers_changed);
9076 if (all_windows)
9077 {
9078 Lisp_Object tail, frame;
9079 int count = SPECPDL_INDEX ();
9080 /* 1 means that update_menu_bar has run its hooks
9081 so any further calls to update_menu_bar shouldn't do so again. */
9082 int menu_bar_hooks_run = 0;
9083
9084 record_unwind_save_match_data ();
9085
9086 FOR_EACH_FRAME (tail, frame)
9087 {
9088 f = XFRAME (frame);
9089
9090 /* Ignore tooltip frame. */
9091 if (EQ (frame, tooltip_frame))
9092 continue;
9093
9094 /* If a window on this frame changed size, report that to
9095 the user and clear the size-change flag. */
9096 if (FRAME_WINDOW_SIZES_CHANGED (f))
9097 {
9098 Lisp_Object functions;
9099
9100 /* Clear flag first in case we get an error below. */
9101 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9102 functions = Vwindow_size_change_functions;
9103 GCPRO2 (tail, functions);
9104
9105 while (CONSP (functions))
9106 {
9107 call1 (XCAR (functions), frame);
9108 functions = XCDR (functions);
9109 }
9110 UNGCPRO;
9111 }
9112
9113 GCPRO1 (tail);
9114 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9115 #ifdef HAVE_WINDOW_SYSTEM
9116 update_tool_bar (f, 0);
9117 #ifdef MAC_OS
9118 mac_update_title_bar (f, 0);
9119 #endif
9120 #endif
9121 UNGCPRO;
9122 }
9123
9124 unbind_to (count, Qnil);
9125 }
9126 else
9127 {
9128 struct frame *sf = SELECTED_FRAME ();
9129 update_menu_bar (sf, 1, 0);
9130 #ifdef HAVE_WINDOW_SYSTEM
9131 update_tool_bar (sf, 1);
9132 #ifdef MAC_OS
9133 mac_update_title_bar (sf, 1);
9134 #endif
9135 #endif
9136 }
9137
9138 /* Motif needs this. See comment in xmenu.c. Turn it off when
9139 pending_menu_activation is not defined. */
9140 #ifdef USE_X_TOOLKIT
9141 pending_menu_activation = 0;
9142 #endif
9143 }
9144
9145
9146 /* Update the menu bar item list for frame F. This has to be done
9147 before we start to fill in any display lines, because it can call
9148 eval.
9149
9150 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9151
9152 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9153 already ran the menu bar hooks for this redisplay, so there
9154 is no need to run them again. The return value is the
9155 updated value of this flag, to pass to the next call. */
9156
9157 static int
9158 update_menu_bar (f, save_match_data, hooks_run)
9159 struct frame *f;
9160 int save_match_data;
9161 int hooks_run;
9162 {
9163 Lisp_Object window;
9164 register struct window *w;
9165
9166 /* If called recursively during a menu update, do nothing. This can
9167 happen when, for instance, an activate-menubar-hook causes a
9168 redisplay. */
9169 if (inhibit_menubar_update)
9170 return hooks_run;
9171
9172 window = FRAME_SELECTED_WINDOW (f);
9173 w = XWINDOW (window);
9174
9175 #if 0 /* The if statement below this if statement used to include the
9176 condition !NILP (w->update_mode_line), rather than using
9177 update_mode_lines directly, and this if statement may have
9178 been added to make that condition work. Now the if
9179 statement below matches its comment, this isn't needed. */
9180 if (update_mode_lines)
9181 w->update_mode_line = Qt;
9182 #endif
9183
9184 if (FRAME_WINDOW_P (f)
9185 ?
9186 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9187 || defined (USE_GTK)
9188 FRAME_EXTERNAL_MENU_BAR (f)
9189 #else
9190 FRAME_MENU_BAR_LINES (f) > 0
9191 #endif
9192 : FRAME_MENU_BAR_LINES (f) > 0)
9193 {
9194 /* If the user has switched buffers or windows, we need to
9195 recompute to reflect the new bindings. But we'll
9196 recompute when update_mode_lines is set too; that means
9197 that people can use force-mode-line-update to request
9198 that the menu bar be recomputed. The adverse effect on
9199 the rest of the redisplay algorithm is about the same as
9200 windows_or_buffers_changed anyway. */
9201 if (windows_or_buffers_changed
9202 /* This used to test w->update_mode_line, but we believe
9203 there is no need to recompute the menu in that case. */
9204 || update_mode_lines
9205 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9206 < BUF_MODIFF (XBUFFER (w->buffer)))
9207 != !NILP (w->last_had_star))
9208 || ((!NILP (Vtransient_mark_mode)
9209 && !NILP (XBUFFER (w->buffer)->mark_active))
9210 != !NILP (w->region_showing)))
9211 {
9212 struct buffer *prev = current_buffer;
9213 int count = SPECPDL_INDEX ();
9214
9215 specbind (Qinhibit_menubar_update, Qt);
9216
9217 set_buffer_internal_1 (XBUFFER (w->buffer));
9218 if (save_match_data)
9219 record_unwind_save_match_data ();
9220 if (NILP (Voverriding_local_map_menu_flag))
9221 {
9222 specbind (Qoverriding_terminal_local_map, Qnil);
9223 specbind (Qoverriding_local_map, Qnil);
9224 }
9225
9226 if (!hooks_run)
9227 {
9228 /* Run the Lucid hook. */
9229 safe_run_hooks (Qactivate_menubar_hook);
9230
9231 /* If it has changed current-menubar from previous value,
9232 really recompute the menu-bar from the value. */
9233 if (! NILP (Vlucid_menu_bar_dirty_flag))
9234 call0 (Qrecompute_lucid_menubar);
9235
9236 safe_run_hooks (Qmenu_bar_update_hook);
9237
9238 hooks_run = 1;
9239 }
9240
9241 XSETFRAME (Vmenu_updating_frame, f);
9242 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9243
9244 /* Redisplay the menu bar in case we changed it. */
9245 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9246 || defined (USE_GTK)
9247 if (FRAME_WINDOW_P (f))
9248 {
9249 #ifdef MAC_OS
9250 /* All frames on Mac OS share the same menubar. So only
9251 the selected frame should be allowed to set it. */
9252 if (f == SELECTED_FRAME ())
9253 #endif
9254 set_frame_menubar (f, 0, 0);
9255 }
9256 else
9257 /* On a terminal screen, the menu bar is an ordinary screen
9258 line, and this makes it get updated. */
9259 w->update_mode_line = Qt;
9260 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9261 /* In the non-toolkit version, the menu bar is an ordinary screen
9262 line, and this makes it get updated. */
9263 w->update_mode_line = Qt;
9264 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9265
9266 unbind_to (count, Qnil);
9267 set_buffer_internal_1 (prev);
9268 }
9269 }
9270
9271 return hooks_run;
9272 }
9273
9274
9275 \f
9276 /***********************************************************************
9277 Output Cursor
9278 ***********************************************************************/
9279
9280 #ifdef HAVE_WINDOW_SYSTEM
9281
9282 /* EXPORT:
9283 Nominal cursor position -- where to draw output.
9284 HPOS and VPOS are window relative glyph matrix coordinates.
9285 X and Y are window relative pixel coordinates. */
9286
9287 struct cursor_pos output_cursor;
9288
9289
9290 /* EXPORT:
9291 Set the global variable output_cursor to CURSOR. All cursor
9292 positions are relative to updated_window. */
9293
9294 void
9295 set_output_cursor (cursor)
9296 struct cursor_pos *cursor;
9297 {
9298 output_cursor.hpos = cursor->hpos;
9299 output_cursor.vpos = cursor->vpos;
9300 output_cursor.x = cursor->x;
9301 output_cursor.y = cursor->y;
9302 }
9303
9304
9305 /* EXPORT for RIF:
9306 Set a nominal cursor position.
9307
9308 HPOS and VPOS are column/row positions in a window glyph matrix. X
9309 and Y are window text area relative pixel positions.
9310
9311 If this is done during an update, updated_window will contain the
9312 window that is being updated and the position is the future output
9313 cursor position for that window. If updated_window is null, use
9314 selected_window and display the cursor at the given position. */
9315
9316 void
9317 x_cursor_to (vpos, hpos, y, x)
9318 int vpos, hpos, y, x;
9319 {
9320 struct window *w;
9321
9322 /* If updated_window is not set, work on selected_window. */
9323 if (updated_window)
9324 w = updated_window;
9325 else
9326 w = XWINDOW (selected_window);
9327
9328 /* Set the output cursor. */
9329 output_cursor.hpos = hpos;
9330 output_cursor.vpos = vpos;
9331 output_cursor.x = x;
9332 output_cursor.y = y;
9333
9334 /* If not called as part of an update, really display the cursor.
9335 This will also set the cursor position of W. */
9336 if (updated_window == NULL)
9337 {
9338 BLOCK_INPUT;
9339 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9340 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9341 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9342 UNBLOCK_INPUT;
9343 }
9344 }
9345
9346 #endif /* HAVE_WINDOW_SYSTEM */
9347
9348 \f
9349 /***********************************************************************
9350 Tool-bars
9351 ***********************************************************************/
9352
9353 #ifdef HAVE_WINDOW_SYSTEM
9354
9355 /* Where the mouse was last time we reported a mouse event. */
9356
9357 FRAME_PTR last_mouse_frame;
9358
9359 /* Tool-bar item index of the item on which a mouse button was pressed
9360 or -1. */
9361
9362 int last_tool_bar_item;
9363
9364
9365 /* Update the tool-bar item list for frame F. This has to be done
9366 before we start to fill in any display lines. Called from
9367 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9368 and restore it here. */
9369
9370 static void
9371 update_tool_bar (f, save_match_data)
9372 struct frame *f;
9373 int save_match_data;
9374 {
9375 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9376 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9377 #else
9378 int do_update = WINDOWP (f->tool_bar_window)
9379 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9380 #endif
9381
9382 if (do_update)
9383 {
9384 Lisp_Object window;
9385 struct window *w;
9386
9387 window = FRAME_SELECTED_WINDOW (f);
9388 w = XWINDOW (window);
9389
9390 /* If the user has switched buffers or windows, we need to
9391 recompute to reflect the new bindings. But we'll
9392 recompute when update_mode_lines is set too; that means
9393 that people can use force-mode-line-update to request
9394 that the menu bar be recomputed. The adverse effect on
9395 the rest of the redisplay algorithm is about the same as
9396 windows_or_buffers_changed anyway. */
9397 if (windows_or_buffers_changed
9398 || !NILP (w->update_mode_line)
9399 || update_mode_lines
9400 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9401 < BUF_MODIFF (XBUFFER (w->buffer)))
9402 != !NILP (w->last_had_star))
9403 || ((!NILP (Vtransient_mark_mode)
9404 && !NILP (XBUFFER (w->buffer)->mark_active))
9405 != !NILP (w->region_showing)))
9406 {
9407 struct buffer *prev = current_buffer;
9408 int count = SPECPDL_INDEX ();
9409 Lisp_Object new_tool_bar;
9410 int new_n_tool_bar;
9411 struct gcpro gcpro1;
9412
9413 /* Set current_buffer to the buffer of the selected
9414 window of the frame, so that we get the right local
9415 keymaps. */
9416 set_buffer_internal_1 (XBUFFER (w->buffer));
9417
9418 /* Save match data, if we must. */
9419 if (save_match_data)
9420 record_unwind_save_match_data ();
9421
9422 /* Make sure that we don't accidentally use bogus keymaps. */
9423 if (NILP (Voverriding_local_map_menu_flag))
9424 {
9425 specbind (Qoverriding_terminal_local_map, Qnil);
9426 specbind (Qoverriding_local_map, Qnil);
9427 }
9428
9429 GCPRO1 (new_tool_bar);
9430
9431 /* Build desired tool-bar items from keymaps. */
9432 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9433 &new_n_tool_bar);
9434
9435 /* Redisplay the tool-bar if we changed it. */
9436 if (new_n_tool_bar != f->n_tool_bar_items
9437 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9438 {
9439 /* Redisplay that happens asynchronously due to an expose event
9440 may access f->tool_bar_items. Make sure we update both
9441 variables within BLOCK_INPUT so no such event interrupts. */
9442 BLOCK_INPUT;
9443 f->tool_bar_items = new_tool_bar;
9444 f->n_tool_bar_items = new_n_tool_bar;
9445 w->update_mode_line = Qt;
9446 UNBLOCK_INPUT;
9447 }
9448
9449 UNGCPRO;
9450
9451 unbind_to (count, Qnil);
9452 set_buffer_internal_1 (prev);
9453 }
9454 }
9455 }
9456
9457
9458 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9459 F's desired tool-bar contents. F->tool_bar_items must have
9460 been set up previously by calling prepare_menu_bars. */
9461
9462 static void
9463 build_desired_tool_bar_string (f)
9464 struct frame *f;
9465 {
9466 int i, size, size_needed;
9467 struct gcpro gcpro1, gcpro2, gcpro3;
9468 Lisp_Object image, plist, props;
9469
9470 image = plist = props = Qnil;
9471 GCPRO3 (image, plist, props);
9472
9473 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9474 Otherwise, make a new string. */
9475
9476 /* The size of the string we might be able to reuse. */
9477 size = (STRINGP (f->desired_tool_bar_string)
9478 ? SCHARS (f->desired_tool_bar_string)
9479 : 0);
9480
9481 /* We need one space in the string for each image. */
9482 size_needed = f->n_tool_bar_items;
9483
9484 /* Reuse f->desired_tool_bar_string, if possible. */
9485 if (size < size_needed || NILP (f->desired_tool_bar_string))
9486 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9487 make_number (' '));
9488 else
9489 {
9490 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9491 Fremove_text_properties (make_number (0), make_number (size),
9492 props, f->desired_tool_bar_string);
9493 }
9494
9495 /* Put a `display' property on the string for the images to display,
9496 put a `menu_item' property on tool-bar items with a value that
9497 is the index of the item in F's tool-bar item vector. */
9498 for (i = 0; i < f->n_tool_bar_items; ++i)
9499 {
9500 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9501
9502 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9503 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9504 int hmargin, vmargin, relief, idx, end;
9505 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9506
9507 /* If image is a vector, choose the image according to the
9508 button state. */
9509 image = PROP (TOOL_BAR_ITEM_IMAGES);
9510 if (VECTORP (image))
9511 {
9512 if (enabled_p)
9513 idx = (selected_p
9514 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9515 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9516 else
9517 idx = (selected_p
9518 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9519 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9520
9521 xassert (ASIZE (image) >= idx);
9522 image = AREF (image, idx);
9523 }
9524 else
9525 idx = -1;
9526
9527 /* Ignore invalid image specifications. */
9528 if (!valid_image_p (image))
9529 continue;
9530
9531 /* Display the tool-bar button pressed, or depressed. */
9532 plist = Fcopy_sequence (XCDR (image));
9533
9534 /* Compute margin and relief to draw. */
9535 relief = (tool_bar_button_relief >= 0
9536 ? tool_bar_button_relief
9537 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9538 hmargin = vmargin = relief;
9539
9540 if (INTEGERP (Vtool_bar_button_margin)
9541 && XINT (Vtool_bar_button_margin) > 0)
9542 {
9543 hmargin += XFASTINT (Vtool_bar_button_margin);
9544 vmargin += XFASTINT (Vtool_bar_button_margin);
9545 }
9546 else if (CONSP (Vtool_bar_button_margin))
9547 {
9548 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9549 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9550 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9551
9552 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9553 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9554 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9555 }
9556
9557 if (auto_raise_tool_bar_buttons_p)
9558 {
9559 /* Add a `:relief' property to the image spec if the item is
9560 selected. */
9561 if (selected_p)
9562 {
9563 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9564 hmargin -= relief;
9565 vmargin -= relief;
9566 }
9567 }
9568 else
9569 {
9570 /* If image is selected, display it pressed, i.e. with a
9571 negative relief. If it's not selected, display it with a
9572 raised relief. */
9573 plist = Fplist_put (plist, QCrelief,
9574 (selected_p
9575 ? make_number (-relief)
9576 : make_number (relief)));
9577 hmargin -= relief;
9578 vmargin -= relief;
9579 }
9580
9581 /* Put a margin around the image. */
9582 if (hmargin || vmargin)
9583 {
9584 if (hmargin == vmargin)
9585 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9586 else
9587 plist = Fplist_put (plist, QCmargin,
9588 Fcons (make_number (hmargin),
9589 make_number (vmargin)));
9590 }
9591
9592 /* If button is not enabled, and we don't have special images
9593 for the disabled state, make the image appear disabled by
9594 applying an appropriate algorithm to it. */
9595 if (!enabled_p && idx < 0)
9596 plist = Fplist_put (plist, QCconversion, Qdisabled);
9597
9598 /* Put a `display' text property on the string for the image to
9599 display. Put a `menu-item' property on the string that gives
9600 the start of this item's properties in the tool-bar items
9601 vector. */
9602 image = Fcons (Qimage, plist);
9603 props = list4 (Qdisplay, image,
9604 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9605
9606 /* Let the last image hide all remaining spaces in the tool bar
9607 string. The string can be longer than needed when we reuse a
9608 previous string. */
9609 if (i + 1 == f->n_tool_bar_items)
9610 end = SCHARS (f->desired_tool_bar_string);
9611 else
9612 end = i + 1;
9613 Fadd_text_properties (make_number (i), make_number (end),
9614 props, f->desired_tool_bar_string);
9615 #undef PROP
9616 }
9617
9618 UNGCPRO;
9619 }
9620
9621
9622 /* Display one line of the tool-bar of frame IT->f.
9623
9624 HEIGHT specifies the desired height of the tool-bar line.
9625 If the actual height of the glyph row is less than HEIGHT, the
9626 row's height is increased to HEIGHT, and the icons are centered
9627 vertically in the new height.
9628
9629 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9630 count a final empty row in case the tool-bar width exactly matches
9631 the window width.
9632 */
9633
9634 static void
9635 display_tool_bar_line (it, height)
9636 struct it *it;
9637 int height;
9638 {
9639 struct glyph_row *row = it->glyph_row;
9640 int max_x = it->last_visible_x;
9641 struct glyph *last;
9642
9643 prepare_desired_row (row);
9644 row->y = it->current_y;
9645
9646 /* Note that this isn't made use of if the face hasn't a box,
9647 so there's no need to check the face here. */
9648 it->start_of_box_run_p = 1;
9649
9650 while (it->current_x < max_x)
9651 {
9652 int x, n_glyphs_before, i, nglyphs;
9653 struct it it_before;
9654
9655 /* Get the next display element. */
9656 if (!get_next_display_element (it))
9657 {
9658 /* Don't count empty row if we are counting needed tool-bar lines. */
9659 if (height < 0 && !it->hpos)
9660 return;
9661 break;
9662 }
9663
9664 /* Produce glyphs. */
9665 n_glyphs_before = row->used[TEXT_AREA];
9666 it_before = *it;
9667
9668 PRODUCE_GLYPHS (it);
9669
9670 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9671 i = 0;
9672 x = it_before.current_x;
9673 while (i < nglyphs)
9674 {
9675 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9676
9677 if (x + glyph->pixel_width > max_x)
9678 {
9679 /* Glyph doesn't fit on line. Backtrack. */
9680 row->used[TEXT_AREA] = n_glyphs_before;
9681 *it = it_before;
9682 /* If this is the only glyph on this line, it will never fit on the
9683 toolbar, so skip it. But ensure there is at least one glyph,
9684 so we don't accidentally disable the tool-bar. */
9685 if (n_glyphs_before == 0
9686 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9687 break;
9688 goto out;
9689 }
9690
9691 ++it->hpos;
9692 x += glyph->pixel_width;
9693 ++i;
9694 }
9695
9696 /* Stop at line ends. */
9697 if (ITERATOR_AT_END_OF_LINE_P (it))
9698 break;
9699
9700 set_iterator_to_next (it, 1);
9701 }
9702
9703 out:;
9704
9705 row->displays_text_p = row->used[TEXT_AREA] != 0;
9706
9707 /* Use default face for the border below the tool bar.
9708
9709 FIXME: When auto-resize-tool-bars is grow-only, there is
9710 no additional border below the possibly empty tool-bar lines.
9711 So to make the extra empty lines look "normal", we have to
9712 use the tool-bar face for the border too. */
9713 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9714 it->face_id = DEFAULT_FACE_ID;
9715
9716 extend_face_to_end_of_line (it);
9717 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9718 last->right_box_line_p = 1;
9719 if (last == row->glyphs[TEXT_AREA])
9720 last->left_box_line_p = 1;
9721
9722 /* Make line the desired height and center it vertically. */
9723 if ((height -= it->max_ascent + it->max_descent) > 0)
9724 {
9725 /* Don't add more than one line height. */
9726 height %= FRAME_LINE_HEIGHT (it->f);
9727 it->max_ascent += height / 2;
9728 it->max_descent += (height + 1) / 2;
9729 }
9730
9731 compute_line_metrics (it);
9732
9733 /* If line is empty, make it occupy the rest of the tool-bar. */
9734 if (!row->displays_text_p)
9735 {
9736 row->height = row->phys_height = it->last_visible_y - row->y;
9737 row->visible_height = row->height;
9738 row->ascent = row->phys_ascent = 0;
9739 row->extra_line_spacing = 0;
9740 }
9741
9742 row->full_width_p = 1;
9743 row->continued_p = 0;
9744 row->truncated_on_left_p = 0;
9745 row->truncated_on_right_p = 0;
9746
9747 it->current_x = it->hpos = 0;
9748 it->current_y += row->height;
9749 ++it->vpos;
9750 ++it->glyph_row;
9751 }
9752
9753
9754 /* Max tool-bar height. */
9755
9756 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9757 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9758
9759 /* Value is the number of screen lines needed to make all tool-bar
9760 items of frame F visible. The number of actual rows needed is
9761 returned in *N_ROWS if non-NULL. */
9762
9763 static int
9764 tool_bar_lines_needed (f, n_rows)
9765 struct frame *f;
9766 int *n_rows;
9767 {
9768 struct window *w = XWINDOW (f->tool_bar_window);
9769 struct it it;
9770 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9771 the desired matrix, so use (unused) mode-line row as temporary row to
9772 avoid destroying the first tool-bar row. */
9773 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9774
9775 /* Initialize an iterator for iteration over
9776 F->desired_tool_bar_string in the tool-bar window of frame F. */
9777 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9778 it.first_visible_x = 0;
9779 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9780 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9781
9782 while (!ITERATOR_AT_END_P (&it))
9783 {
9784 clear_glyph_row (temp_row);
9785 it.glyph_row = temp_row;
9786 display_tool_bar_line (&it, -1);
9787 }
9788 clear_glyph_row (temp_row);
9789
9790 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9791 if (n_rows)
9792 *n_rows = it.vpos > 0 ? it.vpos : -1;
9793
9794 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9795 }
9796
9797
9798 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9799 0, 1, 0,
9800 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9801 (frame)
9802 Lisp_Object frame;
9803 {
9804 struct frame *f;
9805 struct window *w;
9806 int nlines = 0;
9807
9808 if (NILP (frame))
9809 frame = selected_frame;
9810 else
9811 CHECK_FRAME (frame);
9812 f = XFRAME (frame);
9813
9814 if (WINDOWP (f->tool_bar_window)
9815 || (w = XWINDOW (f->tool_bar_window),
9816 WINDOW_TOTAL_LINES (w) > 0))
9817 {
9818 update_tool_bar (f, 1);
9819 if (f->n_tool_bar_items)
9820 {
9821 build_desired_tool_bar_string (f);
9822 nlines = tool_bar_lines_needed (f, NULL);
9823 }
9824 }
9825
9826 return make_number (nlines);
9827 }
9828
9829
9830 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9831 height should be changed. */
9832
9833 static int
9834 redisplay_tool_bar (f)
9835 struct frame *f;
9836 {
9837 struct window *w;
9838 struct it it;
9839 struct glyph_row *row;
9840
9841 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9842 if (FRAME_EXTERNAL_TOOL_BAR (f))
9843 update_frame_tool_bar (f);
9844 return 0;
9845 #endif
9846
9847 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9848 do anything. This means you must start with tool-bar-lines
9849 non-zero to get the auto-sizing effect. Or in other words, you
9850 can turn off tool-bars by specifying tool-bar-lines zero. */
9851 if (!WINDOWP (f->tool_bar_window)
9852 || (w = XWINDOW (f->tool_bar_window),
9853 WINDOW_TOTAL_LINES (w) == 0))
9854 return 0;
9855
9856 /* Set up an iterator for the tool-bar window. */
9857 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9858 it.first_visible_x = 0;
9859 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9860 row = it.glyph_row;
9861
9862 /* Build a string that represents the contents of the tool-bar. */
9863 build_desired_tool_bar_string (f);
9864 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9865
9866 if (f->n_tool_bar_rows == 0)
9867 {
9868 int nlines;
9869
9870 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9871 nlines != WINDOW_TOTAL_LINES (w)))
9872 {
9873 extern Lisp_Object Qtool_bar_lines;
9874 Lisp_Object frame;
9875 int old_height = WINDOW_TOTAL_LINES (w);
9876
9877 XSETFRAME (frame, f);
9878 Fmodify_frame_parameters (frame,
9879 Fcons (Fcons (Qtool_bar_lines,
9880 make_number (nlines)),
9881 Qnil));
9882 if (WINDOW_TOTAL_LINES (w) != old_height)
9883 {
9884 clear_glyph_matrix (w->desired_matrix);
9885 fonts_changed_p = 1;
9886 return 1;
9887 }
9888 }
9889 }
9890
9891 /* Display as many lines as needed to display all tool-bar items. */
9892
9893 if (f->n_tool_bar_rows > 0)
9894 {
9895 int border, rows, height, extra;
9896
9897 if (INTEGERP (Vtool_bar_border))
9898 border = XINT (Vtool_bar_border);
9899 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9900 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9901 else if (EQ (Vtool_bar_border, Qborder_width))
9902 border = f->border_width;
9903 else
9904 border = 0;
9905 if (border < 0)
9906 border = 0;
9907
9908 rows = f->n_tool_bar_rows;
9909 height = max (1, (it.last_visible_y - border) / rows);
9910 extra = it.last_visible_y - border - height * rows;
9911
9912 while (it.current_y < it.last_visible_y)
9913 {
9914 int h = 0;
9915 if (extra > 0 && rows-- > 0)
9916 {
9917 h = (extra + rows - 1) / rows;
9918 extra -= h;
9919 }
9920 display_tool_bar_line (&it, height + h);
9921 }
9922 }
9923 else
9924 {
9925 while (it.current_y < it.last_visible_y)
9926 display_tool_bar_line (&it, 0);
9927 }
9928
9929 /* It doesn't make much sense to try scrolling in the tool-bar
9930 window, so don't do it. */
9931 w->desired_matrix->no_scrolling_p = 1;
9932 w->must_be_updated_p = 1;
9933
9934 if (!NILP (Vauto_resize_tool_bars))
9935 {
9936 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9937 int change_height_p = 0;
9938
9939 /* If we couldn't display everything, change the tool-bar's
9940 height if there is room for more. */
9941 if (IT_STRING_CHARPOS (it) < it.end_charpos
9942 && it.current_y < max_tool_bar_height)
9943 change_height_p = 1;
9944
9945 row = it.glyph_row - 1;
9946
9947 /* If there are blank lines at the end, except for a partially
9948 visible blank line at the end that is smaller than
9949 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9950 if (!row->displays_text_p
9951 && row->height >= FRAME_LINE_HEIGHT (f))
9952 change_height_p = 1;
9953
9954 /* If row displays tool-bar items, but is partially visible,
9955 change the tool-bar's height. */
9956 if (row->displays_text_p
9957 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9958 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9959 change_height_p = 1;
9960
9961 /* Resize windows as needed by changing the `tool-bar-lines'
9962 frame parameter. */
9963 if (change_height_p)
9964 {
9965 extern Lisp_Object Qtool_bar_lines;
9966 Lisp_Object frame;
9967 int old_height = WINDOW_TOTAL_LINES (w);
9968 int nrows;
9969 int nlines = tool_bar_lines_needed (f, &nrows);
9970
9971 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
9972 && !f->minimize_tool_bar_window_p)
9973 ? (nlines > old_height)
9974 : (nlines != old_height));
9975 f->minimize_tool_bar_window_p = 0;
9976
9977 if (change_height_p)
9978 {
9979 XSETFRAME (frame, f);
9980 Fmodify_frame_parameters (frame,
9981 Fcons (Fcons (Qtool_bar_lines,
9982 make_number (nlines)),
9983 Qnil));
9984 if (WINDOW_TOTAL_LINES (w) != old_height)
9985 {
9986 clear_glyph_matrix (w->desired_matrix);
9987 f->n_tool_bar_rows = nrows;
9988 fonts_changed_p = 1;
9989 return 1;
9990 }
9991 }
9992 }
9993 }
9994
9995 f->minimize_tool_bar_window_p = 0;
9996 return 0;
9997 }
9998
9999
10000 /* Get information about the tool-bar item which is displayed in GLYPH
10001 on frame F. Return in *PROP_IDX the index where tool-bar item
10002 properties start in F->tool_bar_items. Value is zero if
10003 GLYPH doesn't display a tool-bar item. */
10004
10005 static int
10006 tool_bar_item_info (f, glyph, prop_idx)
10007 struct frame *f;
10008 struct glyph *glyph;
10009 int *prop_idx;
10010 {
10011 Lisp_Object prop;
10012 int success_p;
10013 int charpos;
10014
10015 /* This function can be called asynchronously, which means we must
10016 exclude any possibility that Fget_text_property signals an
10017 error. */
10018 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10019 charpos = max (0, charpos);
10020
10021 /* Get the text property `menu-item' at pos. The value of that
10022 property is the start index of this item's properties in
10023 F->tool_bar_items. */
10024 prop = Fget_text_property (make_number (charpos),
10025 Qmenu_item, f->current_tool_bar_string);
10026 if (INTEGERP (prop))
10027 {
10028 *prop_idx = XINT (prop);
10029 success_p = 1;
10030 }
10031 else
10032 success_p = 0;
10033
10034 return success_p;
10035 }
10036
10037 \f
10038 /* Get information about the tool-bar item at position X/Y on frame F.
10039 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10040 the current matrix of the tool-bar window of F, or NULL if not
10041 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10042 item in F->tool_bar_items. Value is
10043
10044 -1 if X/Y is not on a tool-bar item
10045 0 if X/Y is on the same item that was highlighted before.
10046 1 otherwise. */
10047
10048 static int
10049 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10050 struct frame *f;
10051 int x, y;
10052 struct glyph **glyph;
10053 int *hpos, *vpos, *prop_idx;
10054 {
10055 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10056 struct window *w = XWINDOW (f->tool_bar_window);
10057 int area;
10058
10059 /* Find the glyph under X/Y. */
10060 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10061 if (*glyph == NULL)
10062 return -1;
10063
10064 /* Get the start of this tool-bar item's properties in
10065 f->tool_bar_items. */
10066 if (!tool_bar_item_info (f, *glyph, prop_idx))
10067 return -1;
10068
10069 /* Is mouse on the highlighted item? */
10070 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10071 && *vpos >= dpyinfo->mouse_face_beg_row
10072 && *vpos <= dpyinfo->mouse_face_end_row
10073 && (*vpos > dpyinfo->mouse_face_beg_row
10074 || *hpos >= dpyinfo->mouse_face_beg_col)
10075 && (*vpos < dpyinfo->mouse_face_end_row
10076 || *hpos < dpyinfo->mouse_face_end_col
10077 || dpyinfo->mouse_face_past_end))
10078 return 0;
10079
10080 return 1;
10081 }
10082
10083
10084 /* EXPORT:
10085 Handle mouse button event on the tool-bar of frame F, at
10086 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10087 0 for button release. MODIFIERS is event modifiers for button
10088 release. */
10089
10090 void
10091 handle_tool_bar_click (f, x, y, down_p, modifiers)
10092 struct frame *f;
10093 int x, y, down_p;
10094 unsigned int modifiers;
10095 {
10096 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10097 struct window *w = XWINDOW (f->tool_bar_window);
10098 int hpos, vpos, prop_idx;
10099 struct glyph *glyph;
10100 Lisp_Object enabled_p;
10101
10102 /* If not on the highlighted tool-bar item, return. */
10103 frame_to_window_pixel_xy (w, &x, &y);
10104 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10105 return;
10106
10107 /* If item is disabled, do nothing. */
10108 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10109 if (NILP (enabled_p))
10110 return;
10111
10112 if (down_p)
10113 {
10114 /* Show item in pressed state. */
10115 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10116 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10117 last_tool_bar_item = prop_idx;
10118 }
10119 else
10120 {
10121 Lisp_Object key, frame;
10122 struct input_event event;
10123 EVENT_INIT (event);
10124
10125 /* Show item in released state. */
10126 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10127 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10128
10129 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10130
10131 XSETFRAME (frame, f);
10132 event.kind = TOOL_BAR_EVENT;
10133 event.frame_or_window = frame;
10134 event.arg = frame;
10135 kbd_buffer_store_event (&event);
10136
10137 event.kind = TOOL_BAR_EVENT;
10138 event.frame_or_window = frame;
10139 event.arg = key;
10140 event.modifiers = modifiers;
10141 kbd_buffer_store_event (&event);
10142 last_tool_bar_item = -1;
10143 }
10144 }
10145
10146
10147 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10148 tool-bar window-relative coordinates X/Y. Called from
10149 note_mouse_highlight. */
10150
10151 static void
10152 note_tool_bar_highlight (f, x, y)
10153 struct frame *f;
10154 int x, y;
10155 {
10156 Lisp_Object window = f->tool_bar_window;
10157 struct window *w = XWINDOW (window);
10158 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10159 int hpos, vpos;
10160 struct glyph *glyph;
10161 struct glyph_row *row;
10162 int i;
10163 Lisp_Object enabled_p;
10164 int prop_idx;
10165 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10166 int mouse_down_p, rc;
10167
10168 /* Function note_mouse_highlight is called with negative x(y
10169 values when mouse moves outside of the frame. */
10170 if (x <= 0 || y <= 0)
10171 {
10172 clear_mouse_face (dpyinfo);
10173 return;
10174 }
10175
10176 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10177 if (rc < 0)
10178 {
10179 /* Not on tool-bar item. */
10180 clear_mouse_face (dpyinfo);
10181 return;
10182 }
10183 else if (rc == 0)
10184 /* On same tool-bar item as before. */
10185 goto set_help_echo;
10186
10187 clear_mouse_face (dpyinfo);
10188
10189 /* Mouse is down, but on different tool-bar item? */
10190 mouse_down_p = (dpyinfo->grabbed
10191 && f == last_mouse_frame
10192 && FRAME_LIVE_P (f));
10193 if (mouse_down_p
10194 && last_tool_bar_item != prop_idx)
10195 return;
10196
10197 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10198 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10199
10200 /* If tool-bar item is not enabled, don't highlight it. */
10201 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10202 if (!NILP (enabled_p))
10203 {
10204 /* Compute the x-position of the glyph. In front and past the
10205 image is a space. We include this in the highlighted area. */
10206 row = MATRIX_ROW (w->current_matrix, vpos);
10207 for (i = x = 0; i < hpos; ++i)
10208 x += row->glyphs[TEXT_AREA][i].pixel_width;
10209
10210 /* Record this as the current active region. */
10211 dpyinfo->mouse_face_beg_col = hpos;
10212 dpyinfo->mouse_face_beg_row = vpos;
10213 dpyinfo->mouse_face_beg_x = x;
10214 dpyinfo->mouse_face_beg_y = row->y;
10215 dpyinfo->mouse_face_past_end = 0;
10216
10217 dpyinfo->mouse_face_end_col = hpos + 1;
10218 dpyinfo->mouse_face_end_row = vpos;
10219 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10220 dpyinfo->mouse_face_end_y = row->y;
10221 dpyinfo->mouse_face_window = window;
10222 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10223
10224 /* Display it as active. */
10225 show_mouse_face (dpyinfo, draw);
10226 dpyinfo->mouse_face_image_state = draw;
10227 }
10228
10229 set_help_echo:
10230
10231 /* Set help_echo_string to a help string to display for this tool-bar item.
10232 XTread_socket does the rest. */
10233 help_echo_object = help_echo_window = Qnil;
10234 help_echo_pos = -1;
10235 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10236 if (NILP (help_echo_string))
10237 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10238 }
10239
10240 #endif /* HAVE_WINDOW_SYSTEM */
10241
10242
10243 \f
10244 /************************************************************************
10245 Horizontal scrolling
10246 ************************************************************************/
10247
10248 static int hscroll_window_tree P_ ((Lisp_Object));
10249 static int hscroll_windows P_ ((Lisp_Object));
10250
10251 /* For all leaf windows in the window tree rooted at WINDOW, set their
10252 hscroll value so that PT is (i) visible in the window, and (ii) so
10253 that it is not within a certain margin at the window's left and
10254 right border. Value is non-zero if any window's hscroll has been
10255 changed. */
10256
10257 static int
10258 hscroll_window_tree (window)
10259 Lisp_Object window;
10260 {
10261 int hscrolled_p = 0;
10262 int hscroll_relative_p = FLOATP (Vhscroll_step);
10263 int hscroll_step_abs = 0;
10264 double hscroll_step_rel = 0;
10265
10266 if (hscroll_relative_p)
10267 {
10268 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10269 if (hscroll_step_rel < 0)
10270 {
10271 hscroll_relative_p = 0;
10272 hscroll_step_abs = 0;
10273 }
10274 }
10275 else if (INTEGERP (Vhscroll_step))
10276 {
10277 hscroll_step_abs = XINT (Vhscroll_step);
10278 if (hscroll_step_abs < 0)
10279 hscroll_step_abs = 0;
10280 }
10281 else
10282 hscroll_step_abs = 0;
10283
10284 while (WINDOWP (window))
10285 {
10286 struct window *w = XWINDOW (window);
10287
10288 if (WINDOWP (w->hchild))
10289 hscrolled_p |= hscroll_window_tree (w->hchild);
10290 else if (WINDOWP (w->vchild))
10291 hscrolled_p |= hscroll_window_tree (w->vchild);
10292 else if (w->cursor.vpos >= 0)
10293 {
10294 int h_margin;
10295 int text_area_width;
10296 struct glyph_row *current_cursor_row
10297 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10298 struct glyph_row *desired_cursor_row
10299 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10300 struct glyph_row *cursor_row
10301 = (desired_cursor_row->enabled_p
10302 ? desired_cursor_row
10303 : current_cursor_row);
10304
10305 text_area_width = window_box_width (w, TEXT_AREA);
10306
10307 /* Scroll when cursor is inside this scroll margin. */
10308 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10309
10310 if ((XFASTINT (w->hscroll)
10311 && w->cursor.x <= h_margin)
10312 || (cursor_row->enabled_p
10313 && cursor_row->truncated_on_right_p
10314 && (w->cursor.x >= text_area_width - h_margin)))
10315 {
10316 struct it it;
10317 int hscroll;
10318 struct buffer *saved_current_buffer;
10319 int pt;
10320 int wanted_x;
10321
10322 /* Find point in a display of infinite width. */
10323 saved_current_buffer = current_buffer;
10324 current_buffer = XBUFFER (w->buffer);
10325
10326 if (w == XWINDOW (selected_window))
10327 pt = BUF_PT (current_buffer);
10328 else
10329 {
10330 pt = marker_position (w->pointm);
10331 pt = max (BEGV, pt);
10332 pt = min (ZV, pt);
10333 }
10334
10335 /* Move iterator to pt starting at cursor_row->start in
10336 a line with infinite width. */
10337 init_to_row_start (&it, w, cursor_row);
10338 it.last_visible_x = INFINITY;
10339 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10340 current_buffer = saved_current_buffer;
10341
10342 /* Position cursor in window. */
10343 if (!hscroll_relative_p && hscroll_step_abs == 0)
10344 hscroll = max (0, (it.current_x
10345 - (ITERATOR_AT_END_OF_LINE_P (&it)
10346 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10347 : (text_area_width / 2))))
10348 / FRAME_COLUMN_WIDTH (it.f);
10349 else if (w->cursor.x >= text_area_width - h_margin)
10350 {
10351 if (hscroll_relative_p)
10352 wanted_x = text_area_width * (1 - hscroll_step_rel)
10353 - h_margin;
10354 else
10355 wanted_x = text_area_width
10356 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10357 - h_margin;
10358 hscroll
10359 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10360 }
10361 else
10362 {
10363 if (hscroll_relative_p)
10364 wanted_x = text_area_width * hscroll_step_rel
10365 + h_margin;
10366 else
10367 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10368 + h_margin;
10369 hscroll
10370 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10371 }
10372 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10373
10374 /* Don't call Fset_window_hscroll if value hasn't
10375 changed because it will prevent redisplay
10376 optimizations. */
10377 if (XFASTINT (w->hscroll) != hscroll)
10378 {
10379 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10380 w->hscroll = make_number (hscroll);
10381 hscrolled_p = 1;
10382 }
10383 }
10384 }
10385
10386 window = w->next;
10387 }
10388
10389 /* Value is non-zero if hscroll of any leaf window has been changed. */
10390 return hscrolled_p;
10391 }
10392
10393
10394 /* Set hscroll so that cursor is visible and not inside horizontal
10395 scroll margins for all windows in the tree rooted at WINDOW. See
10396 also hscroll_window_tree above. Value is non-zero if any window's
10397 hscroll has been changed. If it has, desired matrices on the frame
10398 of WINDOW are cleared. */
10399
10400 static int
10401 hscroll_windows (window)
10402 Lisp_Object window;
10403 {
10404 int hscrolled_p;
10405
10406 if (automatic_hscrolling_p)
10407 {
10408 hscrolled_p = hscroll_window_tree (window);
10409 if (hscrolled_p)
10410 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10411 }
10412 else
10413 hscrolled_p = 0;
10414 return hscrolled_p;
10415 }
10416
10417
10418 \f
10419 /************************************************************************
10420 Redisplay
10421 ************************************************************************/
10422
10423 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10424 to a non-zero value. This is sometimes handy to have in a debugger
10425 session. */
10426
10427 #if GLYPH_DEBUG
10428
10429 /* First and last unchanged row for try_window_id. */
10430
10431 int debug_first_unchanged_at_end_vpos;
10432 int debug_last_unchanged_at_beg_vpos;
10433
10434 /* Delta vpos and y. */
10435
10436 int debug_dvpos, debug_dy;
10437
10438 /* Delta in characters and bytes for try_window_id. */
10439
10440 int debug_delta, debug_delta_bytes;
10441
10442 /* Values of window_end_pos and window_end_vpos at the end of
10443 try_window_id. */
10444
10445 EMACS_INT debug_end_pos, debug_end_vpos;
10446
10447 /* Append a string to W->desired_matrix->method. FMT is a printf
10448 format string. A1...A9 are a supplement for a variable-length
10449 argument list. If trace_redisplay_p is non-zero also printf the
10450 resulting string to stderr. */
10451
10452 static void
10453 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10454 struct window *w;
10455 char *fmt;
10456 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10457 {
10458 char buffer[512];
10459 char *method = w->desired_matrix->method;
10460 int len = strlen (method);
10461 int size = sizeof w->desired_matrix->method;
10462 int remaining = size - len - 1;
10463
10464 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10465 if (len && remaining)
10466 {
10467 method[len] = '|';
10468 --remaining, ++len;
10469 }
10470
10471 strncpy (method + len, buffer, remaining);
10472
10473 if (trace_redisplay_p)
10474 fprintf (stderr, "%p (%s): %s\n",
10475 w,
10476 ((BUFFERP (w->buffer)
10477 && STRINGP (XBUFFER (w->buffer)->name))
10478 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10479 : "no buffer"),
10480 buffer);
10481 }
10482
10483 #endif /* GLYPH_DEBUG */
10484
10485
10486 /* Value is non-zero if all changes in window W, which displays
10487 current_buffer, are in the text between START and END. START is a
10488 buffer position, END is given as a distance from Z. Used in
10489 redisplay_internal for display optimization. */
10490
10491 static INLINE int
10492 text_outside_line_unchanged_p (w, start, end)
10493 struct window *w;
10494 int start, end;
10495 {
10496 int unchanged_p = 1;
10497
10498 /* If text or overlays have changed, see where. */
10499 if (XFASTINT (w->last_modified) < MODIFF
10500 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10501 {
10502 /* Gap in the line? */
10503 if (GPT < start || Z - GPT < end)
10504 unchanged_p = 0;
10505
10506 /* Changes start in front of the line, or end after it? */
10507 if (unchanged_p
10508 && (BEG_UNCHANGED < start - 1
10509 || END_UNCHANGED < end))
10510 unchanged_p = 0;
10511
10512 /* If selective display, can't optimize if changes start at the
10513 beginning of the line. */
10514 if (unchanged_p
10515 && INTEGERP (current_buffer->selective_display)
10516 && XINT (current_buffer->selective_display) > 0
10517 && (BEG_UNCHANGED < start || GPT <= start))
10518 unchanged_p = 0;
10519
10520 /* If there are overlays at the start or end of the line, these
10521 may have overlay strings with newlines in them. A change at
10522 START, for instance, may actually concern the display of such
10523 overlay strings as well, and they are displayed on different
10524 lines. So, quickly rule out this case. (For the future, it
10525 might be desirable to implement something more telling than
10526 just BEG/END_UNCHANGED.) */
10527 if (unchanged_p)
10528 {
10529 if (BEG + BEG_UNCHANGED == start
10530 && overlay_touches_p (start))
10531 unchanged_p = 0;
10532 if (END_UNCHANGED == end
10533 && overlay_touches_p (Z - end))
10534 unchanged_p = 0;
10535 }
10536 }
10537
10538 return unchanged_p;
10539 }
10540
10541
10542 /* Do a frame update, taking possible shortcuts into account. This is
10543 the main external entry point for redisplay.
10544
10545 If the last redisplay displayed an echo area message and that message
10546 is no longer requested, we clear the echo area or bring back the
10547 mini-buffer if that is in use. */
10548
10549 void
10550 redisplay ()
10551 {
10552 redisplay_internal (0);
10553 }
10554
10555
10556 static Lisp_Object
10557 overlay_arrow_string_or_property (var)
10558 Lisp_Object var;
10559 {
10560 Lisp_Object val;
10561
10562 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10563 return val;
10564
10565 return Voverlay_arrow_string;
10566 }
10567
10568 /* Return 1 if there are any overlay-arrows in current_buffer. */
10569 static int
10570 overlay_arrow_in_current_buffer_p ()
10571 {
10572 Lisp_Object vlist;
10573
10574 for (vlist = Voverlay_arrow_variable_list;
10575 CONSP (vlist);
10576 vlist = XCDR (vlist))
10577 {
10578 Lisp_Object var = XCAR (vlist);
10579 Lisp_Object val;
10580
10581 if (!SYMBOLP (var))
10582 continue;
10583 val = find_symbol_value (var);
10584 if (MARKERP (val)
10585 && current_buffer == XMARKER (val)->buffer)
10586 return 1;
10587 }
10588 return 0;
10589 }
10590
10591
10592 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10593 has changed. */
10594
10595 static int
10596 overlay_arrows_changed_p ()
10597 {
10598 Lisp_Object vlist;
10599
10600 for (vlist = Voverlay_arrow_variable_list;
10601 CONSP (vlist);
10602 vlist = XCDR (vlist))
10603 {
10604 Lisp_Object var = XCAR (vlist);
10605 Lisp_Object val, pstr;
10606
10607 if (!SYMBOLP (var))
10608 continue;
10609 val = find_symbol_value (var);
10610 if (!MARKERP (val))
10611 continue;
10612 if (! EQ (COERCE_MARKER (val),
10613 Fget (var, Qlast_arrow_position))
10614 || ! (pstr = overlay_arrow_string_or_property (var),
10615 EQ (pstr, Fget (var, Qlast_arrow_string))))
10616 return 1;
10617 }
10618 return 0;
10619 }
10620
10621 /* Mark overlay arrows to be updated on next redisplay. */
10622
10623 static void
10624 update_overlay_arrows (up_to_date)
10625 int up_to_date;
10626 {
10627 Lisp_Object vlist;
10628
10629 for (vlist = Voverlay_arrow_variable_list;
10630 CONSP (vlist);
10631 vlist = XCDR (vlist))
10632 {
10633 Lisp_Object var = XCAR (vlist);
10634
10635 if (!SYMBOLP (var))
10636 continue;
10637
10638 if (up_to_date > 0)
10639 {
10640 Lisp_Object val = find_symbol_value (var);
10641 Fput (var, Qlast_arrow_position,
10642 COERCE_MARKER (val));
10643 Fput (var, Qlast_arrow_string,
10644 overlay_arrow_string_or_property (var));
10645 }
10646 else if (up_to_date < 0
10647 || !NILP (Fget (var, Qlast_arrow_position)))
10648 {
10649 Fput (var, Qlast_arrow_position, Qt);
10650 Fput (var, Qlast_arrow_string, Qt);
10651 }
10652 }
10653 }
10654
10655
10656 /* Return overlay arrow string to display at row.
10657 Return integer (bitmap number) for arrow bitmap in left fringe.
10658 Return nil if no overlay arrow. */
10659
10660 static Lisp_Object
10661 overlay_arrow_at_row (it, row)
10662 struct it *it;
10663 struct glyph_row *row;
10664 {
10665 Lisp_Object vlist;
10666
10667 for (vlist = Voverlay_arrow_variable_list;
10668 CONSP (vlist);
10669 vlist = XCDR (vlist))
10670 {
10671 Lisp_Object var = XCAR (vlist);
10672 Lisp_Object val;
10673
10674 if (!SYMBOLP (var))
10675 continue;
10676
10677 val = find_symbol_value (var);
10678
10679 if (MARKERP (val)
10680 && current_buffer == XMARKER (val)->buffer
10681 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10682 {
10683 if (FRAME_WINDOW_P (it->f)
10684 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10685 {
10686 #ifdef HAVE_WINDOW_SYSTEM
10687 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10688 {
10689 int fringe_bitmap;
10690 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10691 return make_number (fringe_bitmap);
10692 }
10693 #endif
10694 return make_number (-1); /* Use default arrow bitmap */
10695 }
10696 return overlay_arrow_string_or_property (var);
10697 }
10698 }
10699
10700 return Qnil;
10701 }
10702
10703 /* Return 1 if point moved out of or into a composition. Otherwise
10704 return 0. PREV_BUF and PREV_PT are the last point buffer and
10705 position. BUF and PT are the current point buffer and position. */
10706
10707 int
10708 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10709 struct buffer *prev_buf, *buf;
10710 int prev_pt, pt;
10711 {
10712 int start, end;
10713 Lisp_Object prop;
10714 Lisp_Object buffer;
10715
10716 XSETBUFFER (buffer, buf);
10717 /* Check a composition at the last point if point moved within the
10718 same buffer. */
10719 if (prev_buf == buf)
10720 {
10721 if (prev_pt == pt)
10722 /* Point didn't move. */
10723 return 0;
10724
10725 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10726 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10727 && COMPOSITION_VALID_P (start, end, prop)
10728 && start < prev_pt && end > prev_pt)
10729 /* The last point was within the composition. Return 1 iff
10730 point moved out of the composition. */
10731 return (pt <= start || pt >= end);
10732 }
10733
10734 /* Check a composition at the current point. */
10735 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10736 && find_composition (pt, -1, &start, &end, &prop, buffer)
10737 && COMPOSITION_VALID_P (start, end, prop)
10738 && start < pt && end > pt);
10739 }
10740
10741
10742 /* Reconsider the setting of B->clip_changed which is displayed
10743 in window W. */
10744
10745 static INLINE void
10746 reconsider_clip_changes (w, b)
10747 struct window *w;
10748 struct buffer *b;
10749 {
10750 if (b->clip_changed
10751 && !NILP (w->window_end_valid)
10752 && w->current_matrix->buffer == b
10753 && w->current_matrix->zv == BUF_ZV (b)
10754 && w->current_matrix->begv == BUF_BEGV (b))
10755 b->clip_changed = 0;
10756
10757 /* If display wasn't paused, and W is not a tool bar window, see if
10758 point has been moved into or out of a composition. In that case,
10759 we set b->clip_changed to 1 to force updating the screen. If
10760 b->clip_changed has already been set to 1, we can skip this
10761 check. */
10762 if (!b->clip_changed
10763 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10764 {
10765 int pt;
10766
10767 if (w == XWINDOW (selected_window))
10768 pt = BUF_PT (current_buffer);
10769 else
10770 pt = marker_position (w->pointm);
10771
10772 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10773 || pt != XINT (w->last_point))
10774 && check_point_in_composition (w->current_matrix->buffer,
10775 XINT (w->last_point),
10776 XBUFFER (w->buffer), pt))
10777 b->clip_changed = 1;
10778 }
10779 }
10780 \f
10781
10782 /* Select FRAME to forward the values of frame-local variables into C
10783 variables so that the redisplay routines can access those values
10784 directly. */
10785
10786 static void
10787 select_frame_for_redisplay (frame)
10788 Lisp_Object frame;
10789 {
10790 Lisp_Object tail, sym, val;
10791 Lisp_Object old = selected_frame;
10792
10793 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
10794
10795 selected_frame = frame;
10796
10797 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10798 if (CONSP (XCAR (tail))
10799 && (sym = XCAR (XCAR (tail)),
10800 SYMBOLP (sym))
10801 && (sym = indirect_variable (sym),
10802 val = SYMBOL_VALUE (sym),
10803 (BUFFER_LOCAL_VALUEP (val)
10804 || SOME_BUFFER_LOCAL_VALUEP (val)))
10805 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10806 /* Use find_symbol_value rather than Fsymbol_value
10807 to avoid an error if it is void. */
10808 find_symbol_value (sym);
10809
10810 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10811 if (CONSP (XCAR (tail))
10812 && (sym = XCAR (XCAR (tail)),
10813 SYMBOLP (sym))
10814 && (sym = indirect_variable (sym),
10815 val = SYMBOL_VALUE (sym),
10816 (BUFFER_LOCAL_VALUEP (val)
10817 || SOME_BUFFER_LOCAL_VALUEP (val)))
10818 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10819 find_symbol_value (sym);
10820 }
10821
10822
10823 #define STOP_POLLING \
10824 do { if (! polling_stopped_here) stop_polling (); \
10825 polling_stopped_here = 1; } while (0)
10826
10827 #define RESUME_POLLING \
10828 do { if (polling_stopped_here) start_polling (); \
10829 polling_stopped_here = 0; } while (0)
10830
10831
10832 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10833 response to any user action; therefore, we should preserve the echo
10834 area. (Actually, our caller does that job.) Perhaps in the future
10835 avoid recentering windows if it is not necessary; currently that
10836 causes some problems. */
10837
10838 static void
10839 redisplay_internal (preserve_echo_area)
10840 int preserve_echo_area;
10841 {
10842 struct window *w = XWINDOW (selected_window);
10843 struct frame *f;
10844 int pause;
10845 int must_finish = 0;
10846 struct text_pos tlbufpos, tlendpos;
10847 int number_of_visible_frames;
10848 int count, count1;
10849 struct frame *sf;
10850 int polling_stopped_here = 0;
10851 Lisp_Object old_frame = selected_frame;
10852
10853 /* Non-zero means redisplay has to consider all windows on all
10854 frames. Zero means, only selected_window is considered. */
10855 int consider_all_windows_p;
10856
10857 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10858
10859 /* No redisplay if running in batch mode or frame is not yet fully
10860 initialized, or redisplay is explicitly turned off by setting
10861 Vinhibit_redisplay. */
10862 if (noninteractive
10863 || !NILP (Vinhibit_redisplay))
10864 return;
10865
10866 /* Don't examine these until after testing Vinhibit_redisplay.
10867 When Emacs is shutting down, perhaps because its connection to
10868 X has dropped, we should not look at them at all. */
10869 f = XFRAME (w->frame);
10870 sf = SELECTED_FRAME ();
10871
10872 if (!f->glyphs_initialized_p)
10873 return;
10874
10875 /* The flag redisplay_performed_directly_p is set by
10876 direct_output_for_insert when it already did the whole screen
10877 update necessary. */
10878 if (redisplay_performed_directly_p)
10879 {
10880 redisplay_performed_directly_p = 0;
10881 if (!hscroll_windows (selected_window))
10882 return;
10883 }
10884
10885 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10886 if (popup_activated ())
10887 return;
10888 #endif
10889
10890 /* I don't think this happens but let's be paranoid. */
10891 if (redisplaying_p)
10892 return;
10893
10894 /* Record a function that resets redisplaying_p to its old value
10895 when we leave this function. */
10896 count = SPECPDL_INDEX ();
10897 record_unwind_protect (unwind_redisplay,
10898 Fcons (make_number (redisplaying_p), selected_frame));
10899 ++redisplaying_p;
10900 specbind (Qinhibit_free_realized_faces, Qnil);
10901
10902 {
10903 Lisp_Object tail, frame;
10904
10905 FOR_EACH_FRAME (tail, frame)
10906 {
10907 struct frame *f = XFRAME (frame);
10908 f->already_hscrolled_p = 0;
10909 }
10910 }
10911
10912 retry:
10913 if (!EQ (old_frame, selected_frame)
10914 && FRAME_LIVE_P (XFRAME (old_frame)))
10915 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
10916 selected_frame and selected_window to be temporarily out-of-sync so
10917 when we come back here via `goto retry', we need to resync because we
10918 may need to run Elisp code (via prepare_menu_bars). */
10919 select_frame_for_redisplay (old_frame);
10920
10921 pause = 0;
10922 reconsider_clip_changes (w, current_buffer);
10923 last_escape_glyph_frame = NULL;
10924 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10925
10926 /* If new fonts have been loaded that make a glyph matrix adjustment
10927 necessary, do it. */
10928 if (fonts_changed_p)
10929 {
10930 adjust_glyphs (NULL);
10931 ++windows_or_buffers_changed;
10932 fonts_changed_p = 0;
10933 }
10934
10935 /* If face_change_count is non-zero, init_iterator will free all
10936 realized faces, which includes the faces referenced from current
10937 matrices. So, we can't reuse current matrices in this case. */
10938 if (face_change_count)
10939 ++windows_or_buffers_changed;
10940
10941 if (FRAME_TERMCAP_P (sf)
10942 && FRAME_TTY (sf)->previous_frame != sf)
10943 {
10944 /* Since frames on a single ASCII terminal share the same
10945 display area, displaying a different frame means redisplay
10946 the whole thing. */
10947 windows_or_buffers_changed++;
10948 SET_FRAME_GARBAGED (sf);
10949 FRAME_TTY (sf)->previous_frame = sf;
10950 }
10951
10952 /* Set the visible flags for all frames. Do this before checking
10953 for resized or garbaged frames; they want to know if their frames
10954 are visible. See the comment in frame.h for
10955 FRAME_SAMPLE_VISIBILITY. */
10956 {
10957 Lisp_Object tail, frame;
10958
10959 number_of_visible_frames = 0;
10960
10961 FOR_EACH_FRAME (tail, frame)
10962 {
10963 struct frame *f = XFRAME (frame);
10964
10965 FRAME_SAMPLE_VISIBILITY (f);
10966 if (FRAME_VISIBLE_P (f))
10967 ++number_of_visible_frames;
10968 clear_desired_matrices (f);
10969 }
10970 }
10971
10972
10973 /* Notice any pending interrupt request to change frame size. */
10974 do_pending_window_change (1);
10975
10976 /* Clear frames marked as garbaged. */
10977 if (frame_garbaged)
10978 clear_garbaged_frames ();
10979
10980 /* Build menubar and tool-bar items. */
10981 if (NILP (Vmemory_full))
10982 prepare_menu_bars ();
10983
10984 if (windows_or_buffers_changed)
10985 update_mode_lines++;
10986
10987 /* Detect case that we need to write or remove a star in the mode line. */
10988 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10989 {
10990 w->update_mode_line = Qt;
10991 if (buffer_shared > 1)
10992 update_mode_lines++;
10993 }
10994
10995 /* Avoid invocation of point motion hooks by `current_column' below. */
10996 count1 = SPECPDL_INDEX ();
10997 specbind (Qinhibit_point_motion_hooks, Qt);
10998
10999 /* If %c is in the mode line, update it if needed. */
11000 if (!NILP (w->column_number_displayed)
11001 /* This alternative quickly identifies a common case
11002 where no change is needed. */
11003 && !(PT == XFASTINT (w->last_point)
11004 && XFASTINT (w->last_modified) >= MODIFF
11005 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11006 && (XFASTINT (w->column_number_displayed)
11007 != (int) current_column ())) /* iftc */
11008 w->update_mode_line = Qt;
11009
11010 unbind_to (count1, Qnil);
11011
11012 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11013
11014 /* The variable buffer_shared is set in redisplay_window and
11015 indicates that we redisplay a buffer in different windows. See
11016 there. */
11017 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11018 || cursor_type_changed);
11019
11020 /* If specs for an arrow have changed, do thorough redisplay
11021 to ensure we remove any arrow that should no longer exist. */
11022 if (overlay_arrows_changed_p ())
11023 consider_all_windows_p = windows_or_buffers_changed = 1;
11024
11025 /* Normally the message* functions will have already displayed and
11026 updated the echo area, but the frame may have been trashed, or
11027 the update may have been preempted, so display the echo area
11028 again here. Checking message_cleared_p captures the case that
11029 the echo area should be cleared. */
11030 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11031 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11032 || (message_cleared_p
11033 && minibuf_level == 0
11034 /* If the mini-window is currently selected, this means the
11035 echo-area doesn't show through. */
11036 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11037 {
11038 int window_height_changed_p = echo_area_display (0);
11039 must_finish = 1;
11040
11041 /* If we don't display the current message, don't clear the
11042 message_cleared_p flag, because, if we did, we wouldn't clear
11043 the echo area in the next redisplay which doesn't preserve
11044 the echo area. */
11045 if (!display_last_displayed_message_p)
11046 message_cleared_p = 0;
11047
11048 if (fonts_changed_p)
11049 goto retry;
11050 else if (window_height_changed_p)
11051 {
11052 consider_all_windows_p = 1;
11053 ++update_mode_lines;
11054 ++windows_or_buffers_changed;
11055
11056 /* If window configuration was changed, frames may have been
11057 marked garbaged. Clear them or we will experience
11058 surprises wrt scrolling. */
11059 if (frame_garbaged)
11060 clear_garbaged_frames ();
11061 }
11062 }
11063 else if (EQ (selected_window, minibuf_window)
11064 && (current_buffer->clip_changed
11065 || XFASTINT (w->last_modified) < MODIFF
11066 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11067 && resize_mini_window (w, 0))
11068 {
11069 /* Resized active mini-window to fit the size of what it is
11070 showing if its contents might have changed. */
11071 must_finish = 1;
11072 consider_all_windows_p = 1;
11073 ++windows_or_buffers_changed;
11074 ++update_mode_lines;
11075
11076 /* If window configuration was changed, frames may have been
11077 marked garbaged. Clear them or we will experience
11078 surprises wrt scrolling. */
11079 if (frame_garbaged)
11080 clear_garbaged_frames ();
11081 }
11082
11083
11084 /* If showing the region, and mark has changed, we must redisplay
11085 the whole window. The assignment to this_line_start_pos prevents
11086 the optimization directly below this if-statement. */
11087 if (((!NILP (Vtransient_mark_mode)
11088 && !NILP (XBUFFER (w->buffer)->mark_active))
11089 != !NILP (w->region_showing))
11090 || (!NILP (w->region_showing)
11091 && !EQ (w->region_showing,
11092 Fmarker_position (XBUFFER (w->buffer)->mark))))
11093 CHARPOS (this_line_start_pos) = 0;
11094
11095 /* Optimize the case that only the line containing the cursor in the
11096 selected window has changed. Variables starting with this_ are
11097 set in display_line and record information about the line
11098 containing the cursor. */
11099 tlbufpos = this_line_start_pos;
11100 tlendpos = this_line_end_pos;
11101 if (!consider_all_windows_p
11102 && CHARPOS (tlbufpos) > 0
11103 && NILP (w->update_mode_line)
11104 && !current_buffer->clip_changed
11105 && !current_buffer->prevent_redisplay_optimizations_p
11106 && FRAME_VISIBLE_P (XFRAME (w->frame))
11107 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11108 /* Make sure recorded data applies to current buffer, etc. */
11109 && this_line_buffer == current_buffer
11110 && current_buffer == XBUFFER (w->buffer)
11111 && NILP (w->force_start)
11112 && NILP (w->optional_new_start)
11113 /* Point must be on the line that we have info recorded about. */
11114 && PT >= CHARPOS (tlbufpos)
11115 && PT <= Z - CHARPOS (tlendpos)
11116 /* All text outside that line, including its final newline,
11117 must be unchanged */
11118 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11119 CHARPOS (tlendpos)))
11120 {
11121 if (CHARPOS (tlbufpos) > BEGV
11122 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11123 && (CHARPOS (tlbufpos) == ZV
11124 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11125 /* Former continuation line has disappeared by becoming empty */
11126 goto cancel;
11127 else if (XFASTINT (w->last_modified) < MODIFF
11128 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11129 || MINI_WINDOW_P (w))
11130 {
11131 /* We have to handle the case of continuation around a
11132 wide-column character (See the comment in indent.c around
11133 line 885).
11134
11135 For instance, in the following case:
11136
11137 -------- Insert --------
11138 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11139 J_I_ ==> J_I_ `^^' are cursors.
11140 ^^ ^^
11141 -------- --------
11142
11143 As we have to redraw the line above, we should goto cancel. */
11144
11145 struct it it;
11146 int line_height_before = this_line_pixel_height;
11147
11148 /* Note that start_display will handle the case that the
11149 line starting at tlbufpos is a continuation lines. */
11150 start_display (&it, w, tlbufpos);
11151
11152 /* Implementation note: It this still necessary? */
11153 if (it.current_x != this_line_start_x)
11154 goto cancel;
11155
11156 TRACE ((stderr, "trying display optimization 1\n"));
11157 w->cursor.vpos = -1;
11158 overlay_arrow_seen = 0;
11159 it.vpos = this_line_vpos;
11160 it.current_y = this_line_y;
11161 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11162 display_line (&it);
11163
11164 /* If line contains point, is not continued,
11165 and ends at same distance from eob as before, we win */
11166 if (w->cursor.vpos >= 0
11167 /* Line is not continued, otherwise this_line_start_pos
11168 would have been set to 0 in display_line. */
11169 && CHARPOS (this_line_start_pos)
11170 /* Line ends as before. */
11171 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11172 /* Line has same height as before. Otherwise other lines
11173 would have to be shifted up or down. */
11174 && this_line_pixel_height == line_height_before)
11175 {
11176 /* If this is not the window's last line, we must adjust
11177 the charstarts of the lines below. */
11178 if (it.current_y < it.last_visible_y)
11179 {
11180 struct glyph_row *row
11181 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11182 int delta, delta_bytes;
11183
11184 if (Z - CHARPOS (tlendpos) == ZV)
11185 {
11186 /* This line ends at end of (accessible part of)
11187 buffer. There is no newline to count. */
11188 delta = (Z
11189 - CHARPOS (tlendpos)
11190 - MATRIX_ROW_START_CHARPOS (row));
11191 delta_bytes = (Z_BYTE
11192 - BYTEPOS (tlendpos)
11193 - MATRIX_ROW_START_BYTEPOS (row));
11194 }
11195 else
11196 {
11197 /* This line ends in a newline. Must take
11198 account of the newline and the rest of the
11199 text that follows. */
11200 delta = (Z
11201 - CHARPOS (tlendpos)
11202 - MATRIX_ROW_START_CHARPOS (row));
11203 delta_bytes = (Z_BYTE
11204 - BYTEPOS (tlendpos)
11205 - MATRIX_ROW_START_BYTEPOS (row));
11206 }
11207
11208 increment_matrix_positions (w->current_matrix,
11209 this_line_vpos + 1,
11210 w->current_matrix->nrows,
11211 delta, delta_bytes);
11212 }
11213
11214 /* If this row displays text now but previously didn't,
11215 or vice versa, w->window_end_vpos may have to be
11216 adjusted. */
11217 if ((it.glyph_row - 1)->displays_text_p)
11218 {
11219 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11220 XSETINT (w->window_end_vpos, this_line_vpos);
11221 }
11222 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11223 && this_line_vpos > 0)
11224 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11225 w->window_end_valid = Qnil;
11226
11227 /* Update hint: No need to try to scroll in update_window. */
11228 w->desired_matrix->no_scrolling_p = 1;
11229
11230 #if GLYPH_DEBUG
11231 *w->desired_matrix->method = 0;
11232 debug_method_add (w, "optimization 1");
11233 #endif
11234 #ifdef HAVE_WINDOW_SYSTEM
11235 update_window_fringes (w, 0);
11236 #endif
11237 goto update;
11238 }
11239 else
11240 goto cancel;
11241 }
11242 else if (/* Cursor position hasn't changed. */
11243 PT == XFASTINT (w->last_point)
11244 /* Make sure the cursor was last displayed
11245 in this window. Otherwise we have to reposition it. */
11246 && 0 <= w->cursor.vpos
11247 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11248 {
11249 if (!must_finish)
11250 {
11251 do_pending_window_change (1);
11252
11253 /* We used to always goto end_of_redisplay here, but this
11254 isn't enough if we have a blinking cursor. */
11255 if (w->cursor_off_p == w->last_cursor_off_p)
11256 goto end_of_redisplay;
11257 }
11258 goto update;
11259 }
11260 /* If highlighting the region, or if the cursor is in the echo area,
11261 then we can't just move the cursor. */
11262 else if (! (!NILP (Vtransient_mark_mode)
11263 && !NILP (current_buffer->mark_active))
11264 && (EQ (selected_window, current_buffer->last_selected_window)
11265 || highlight_nonselected_windows)
11266 && NILP (w->region_showing)
11267 && NILP (Vshow_trailing_whitespace)
11268 && !cursor_in_echo_area)
11269 {
11270 struct it it;
11271 struct glyph_row *row;
11272
11273 /* Skip from tlbufpos to PT and see where it is. Note that
11274 PT may be in invisible text. If so, we will end at the
11275 next visible position. */
11276 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11277 NULL, DEFAULT_FACE_ID);
11278 it.current_x = this_line_start_x;
11279 it.current_y = this_line_y;
11280 it.vpos = this_line_vpos;
11281
11282 /* The call to move_it_to stops in front of PT, but
11283 moves over before-strings. */
11284 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11285
11286 if (it.vpos == this_line_vpos
11287 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11288 row->enabled_p))
11289 {
11290 xassert (this_line_vpos == it.vpos);
11291 xassert (this_line_y == it.current_y);
11292 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11293 #if GLYPH_DEBUG
11294 *w->desired_matrix->method = 0;
11295 debug_method_add (w, "optimization 3");
11296 #endif
11297 goto update;
11298 }
11299 else
11300 goto cancel;
11301 }
11302
11303 cancel:
11304 /* Text changed drastically or point moved off of line. */
11305 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11306 }
11307
11308 CHARPOS (this_line_start_pos) = 0;
11309 consider_all_windows_p |= buffer_shared > 1;
11310 ++clear_face_cache_count;
11311 #ifdef HAVE_WINDOW_SYSTEM
11312 ++clear_image_cache_count;
11313 #endif
11314
11315 /* Build desired matrices, and update the display. If
11316 consider_all_windows_p is non-zero, do it for all windows on all
11317 frames. Otherwise do it for selected_window, only. */
11318
11319 if (consider_all_windows_p)
11320 {
11321 Lisp_Object tail, frame;
11322
11323 FOR_EACH_FRAME (tail, frame)
11324 XFRAME (frame)->updated_p = 0;
11325
11326 /* Recompute # windows showing selected buffer. This will be
11327 incremented each time such a window is displayed. */
11328 buffer_shared = 0;
11329
11330 FOR_EACH_FRAME (tail, frame)
11331 {
11332 struct frame *f = XFRAME (frame);
11333
11334 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11335 {
11336 if (! EQ (frame, selected_frame))
11337 /* Select the frame, for the sake of frame-local
11338 variables. */
11339 select_frame_for_redisplay (frame);
11340
11341 /* Mark all the scroll bars to be removed; we'll redeem
11342 the ones we want when we redisplay their windows. */
11343 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11344 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11345
11346 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11347 redisplay_windows (FRAME_ROOT_WINDOW (f));
11348
11349 /* Any scroll bars which redisplay_windows should have
11350 nuked should now go away. */
11351 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11352 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11353
11354 /* If fonts changed, display again. */
11355 /* ??? rms: I suspect it is a mistake to jump all the way
11356 back to retry here. It should just retry this frame. */
11357 if (fonts_changed_p)
11358 goto retry;
11359
11360 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11361 {
11362 /* See if we have to hscroll. */
11363 if (!f->already_hscrolled_p)
11364 {
11365 f->already_hscrolled_p = 1;
11366 if (hscroll_windows (f->root_window))
11367 goto retry;
11368 }
11369
11370 /* Prevent various kinds of signals during display
11371 update. stdio is not robust about handling
11372 signals, which can cause an apparent I/O
11373 error. */
11374 if (interrupt_input)
11375 unrequest_sigio ();
11376 STOP_POLLING;
11377
11378 /* Update the display. */
11379 set_window_update_flags (XWINDOW (f->root_window), 1);
11380 pause |= update_frame (f, 0, 0);
11381 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11382 if (pause)
11383 break;
11384 #endif
11385
11386 f->updated_p = 1;
11387 }
11388 }
11389 }
11390
11391 if (!pause)
11392 {
11393 /* Do the mark_window_display_accurate after all windows have
11394 been redisplayed because this call resets flags in buffers
11395 which are needed for proper redisplay. */
11396 FOR_EACH_FRAME (tail, frame)
11397 {
11398 struct frame *f = XFRAME (frame);
11399 if (f->updated_p)
11400 {
11401 mark_window_display_accurate (f->root_window, 1);
11402 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11403 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11404 }
11405 }
11406 }
11407 }
11408 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11409 {
11410 Lisp_Object mini_window;
11411 struct frame *mini_frame;
11412
11413 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11414 /* Use list_of_error, not Qerror, so that
11415 we catch only errors and don't run the debugger. */
11416 internal_condition_case_1 (redisplay_window_1, selected_window,
11417 list_of_error,
11418 redisplay_window_error);
11419
11420 /* Compare desired and current matrices, perform output. */
11421
11422 update:
11423 /* If fonts changed, display again. */
11424 if (fonts_changed_p)
11425 goto retry;
11426
11427 /* Prevent various kinds of signals during display update.
11428 stdio is not robust about handling signals,
11429 which can cause an apparent I/O error. */
11430 if (interrupt_input)
11431 unrequest_sigio ();
11432 STOP_POLLING;
11433
11434 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11435 {
11436 if (hscroll_windows (selected_window))
11437 goto retry;
11438
11439 XWINDOW (selected_window)->must_be_updated_p = 1;
11440 pause = update_frame (sf, 0, 0);
11441 }
11442
11443 /* We may have called echo_area_display at the top of this
11444 function. If the echo area is on another frame, that may
11445 have put text on a frame other than the selected one, so the
11446 above call to update_frame would not have caught it. Catch
11447 it here. */
11448 mini_window = FRAME_MINIBUF_WINDOW (sf);
11449 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11450
11451 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11452 {
11453 XWINDOW (mini_window)->must_be_updated_p = 1;
11454 pause |= update_frame (mini_frame, 0, 0);
11455 if (!pause && hscroll_windows (mini_window))
11456 goto retry;
11457 }
11458 }
11459
11460 /* If display was paused because of pending input, make sure we do a
11461 thorough update the next time. */
11462 if (pause)
11463 {
11464 /* Prevent the optimization at the beginning of
11465 redisplay_internal that tries a single-line update of the
11466 line containing the cursor in the selected window. */
11467 CHARPOS (this_line_start_pos) = 0;
11468
11469 /* Let the overlay arrow be updated the next time. */
11470 update_overlay_arrows (0);
11471
11472 /* If we pause after scrolling, some rows in the current
11473 matrices of some windows are not valid. */
11474 if (!WINDOW_FULL_WIDTH_P (w)
11475 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11476 update_mode_lines = 1;
11477 }
11478 else
11479 {
11480 if (!consider_all_windows_p)
11481 {
11482 /* This has already been done above if
11483 consider_all_windows_p is set. */
11484 mark_window_display_accurate_1 (w, 1);
11485
11486 /* Say overlay arrows are up to date. */
11487 update_overlay_arrows (1);
11488
11489 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11490 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11491 }
11492
11493 update_mode_lines = 0;
11494 windows_or_buffers_changed = 0;
11495 cursor_type_changed = 0;
11496 }
11497
11498 /* Start SIGIO interrupts coming again. Having them off during the
11499 code above makes it less likely one will discard output, but not
11500 impossible, since there might be stuff in the system buffer here.
11501 But it is much hairier to try to do anything about that. */
11502 if (interrupt_input)
11503 request_sigio ();
11504 RESUME_POLLING;
11505
11506 /* If a frame has become visible which was not before, redisplay
11507 again, so that we display it. Expose events for such a frame
11508 (which it gets when becoming visible) don't call the parts of
11509 redisplay constructing glyphs, so simply exposing a frame won't
11510 display anything in this case. So, we have to display these
11511 frames here explicitly. */
11512 if (!pause)
11513 {
11514 Lisp_Object tail, frame;
11515 int new_count = 0;
11516
11517 FOR_EACH_FRAME (tail, frame)
11518 {
11519 int this_is_visible = 0;
11520
11521 if (XFRAME (frame)->visible)
11522 this_is_visible = 1;
11523 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11524 if (XFRAME (frame)->visible)
11525 this_is_visible = 1;
11526
11527 if (this_is_visible)
11528 new_count++;
11529 }
11530
11531 if (new_count != number_of_visible_frames)
11532 windows_or_buffers_changed++;
11533 }
11534
11535 /* Change frame size now if a change is pending. */
11536 do_pending_window_change (1);
11537
11538 /* If we just did a pending size change, or have additional
11539 visible frames, redisplay again. */
11540 if (windows_or_buffers_changed && !pause)
11541 goto retry;
11542
11543 /* Clear the face cache eventually. */
11544 if (consider_all_windows_p)
11545 {
11546 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11547 {
11548 clear_face_cache (0);
11549 clear_face_cache_count = 0;
11550 }
11551 #ifdef HAVE_WINDOW_SYSTEM
11552 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11553 {
11554 Lisp_Object tail, frame;
11555 FOR_EACH_FRAME (tail, frame)
11556 {
11557 struct frame *f = XFRAME (frame);
11558 if (FRAME_WINDOW_P (f))
11559 clear_image_cache (f, 0);
11560 }
11561 clear_image_cache_count = 0;
11562 }
11563 #endif /* HAVE_WINDOW_SYSTEM */
11564 }
11565
11566 end_of_redisplay:
11567 unbind_to (count, Qnil);
11568 RESUME_POLLING;
11569 }
11570
11571
11572 /* Redisplay, but leave alone any recent echo area message unless
11573 another message has been requested in its place.
11574
11575 This is useful in situations where you need to redisplay but no
11576 user action has occurred, making it inappropriate for the message
11577 area to be cleared. See tracking_off and
11578 wait_reading_process_output for examples of these situations.
11579
11580 FROM_WHERE is an integer saying from where this function was
11581 called. This is useful for debugging. */
11582
11583 void
11584 redisplay_preserve_echo_area (from_where)
11585 int from_where;
11586 {
11587 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11588
11589 if (!NILP (echo_area_buffer[1]))
11590 {
11591 /* We have a previously displayed message, but no current
11592 message. Redisplay the previous message. */
11593 display_last_displayed_message_p = 1;
11594 redisplay_internal (1);
11595 display_last_displayed_message_p = 0;
11596 }
11597 else
11598 redisplay_internal (1);
11599
11600 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11601 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11602 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11603 }
11604
11605
11606 /* Function registered with record_unwind_protect in
11607 redisplay_internal. Reset redisplaying_p to the value it had
11608 before redisplay_internal was called, and clear
11609 prevent_freeing_realized_faces_p. It also selects the previously
11610 selected frame, unless it has been deleted (by an X connection
11611 failure during redisplay, for example). */
11612
11613 static Lisp_Object
11614 unwind_redisplay (val)
11615 Lisp_Object val;
11616 {
11617 Lisp_Object old_redisplaying_p, old_frame;
11618
11619 old_redisplaying_p = XCAR (val);
11620 redisplaying_p = XFASTINT (old_redisplaying_p);
11621 old_frame = XCDR (val);
11622 if (! EQ (old_frame, selected_frame)
11623 && FRAME_LIVE_P (XFRAME (old_frame)))
11624 select_frame_for_redisplay (old_frame);
11625 return Qnil;
11626 }
11627
11628
11629 /* Mark the display of window W as accurate or inaccurate. If
11630 ACCURATE_P is non-zero mark display of W as accurate. If
11631 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11632 redisplay_internal is called. */
11633
11634 static void
11635 mark_window_display_accurate_1 (w, accurate_p)
11636 struct window *w;
11637 int accurate_p;
11638 {
11639 if (BUFFERP (w->buffer))
11640 {
11641 struct buffer *b = XBUFFER (w->buffer);
11642
11643 w->last_modified
11644 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11645 w->last_overlay_modified
11646 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11647 w->last_had_star
11648 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11649
11650 if (accurate_p)
11651 {
11652 b->clip_changed = 0;
11653 b->prevent_redisplay_optimizations_p = 0;
11654
11655 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11656 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11657 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11658 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11659
11660 w->current_matrix->buffer = b;
11661 w->current_matrix->begv = BUF_BEGV (b);
11662 w->current_matrix->zv = BUF_ZV (b);
11663
11664 w->last_cursor = w->cursor;
11665 w->last_cursor_off_p = w->cursor_off_p;
11666
11667 if (w == XWINDOW (selected_window))
11668 w->last_point = make_number (BUF_PT (b));
11669 else
11670 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11671 }
11672 }
11673
11674 if (accurate_p)
11675 {
11676 w->window_end_valid = w->buffer;
11677 #if 0 /* This is incorrect with variable-height lines. */
11678 xassert (XINT (w->window_end_vpos)
11679 < (WINDOW_TOTAL_LINES (w)
11680 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11681 #endif
11682 w->update_mode_line = Qnil;
11683 }
11684 }
11685
11686
11687 /* Mark the display of windows in the window tree rooted at WINDOW as
11688 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11689 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11690 be redisplayed the next time redisplay_internal is called. */
11691
11692 void
11693 mark_window_display_accurate (window, accurate_p)
11694 Lisp_Object window;
11695 int accurate_p;
11696 {
11697 struct window *w;
11698
11699 for (; !NILP (window); window = w->next)
11700 {
11701 w = XWINDOW (window);
11702 mark_window_display_accurate_1 (w, accurate_p);
11703
11704 if (!NILP (w->vchild))
11705 mark_window_display_accurate (w->vchild, accurate_p);
11706 if (!NILP (w->hchild))
11707 mark_window_display_accurate (w->hchild, accurate_p);
11708 }
11709
11710 if (accurate_p)
11711 {
11712 update_overlay_arrows (1);
11713 }
11714 else
11715 {
11716 /* Force a thorough redisplay the next time by setting
11717 last_arrow_position and last_arrow_string to t, which is
11718 unequal to any useful value of Voverlay_arrow_... */
11719 update_overlay_arrows (-1);
11720 }
11721 }
11722
11723
11724 /* Return value in display table DP (Lisp_Char_Table *) for character
11725 C. Since a display table doesn't have any parent, we don't have to
11726 follow parent. Do not call this function directly but use the
11727 macro DISP_CHAR_VECTOR. */
11728
11729 Lisp_Object
11730 disp_char_vector (dp, c)
11731 struct Lisp_Char_Table *dp;
11732 int c;
11733 {
11734 int code[4], i;
11735 Lisp_Object val;
11736
11737 if (SINGLE_BYTE_CHAR_P (c))
11738 return (dp->contents[c]);
11739
11740 SPLIT_CHAR (c, code[0], code[1], code[2]);
11741 if (code[1] < 32)
11742 code[1] = -1;
11743 else if (code[2] < 32)
11744 code[2] = -1;
11745
11746 /* Here, the possible range of code[0] (== charset ID) is
11747 128..max_charset. Since the top level char table contains data
11748 for multibyte characters after 256th element, we must increment
11749 code[0] by 128 to get a correct index. */
11750 code[0] += 128;
11751 code[3] = -1; /* anchor */
11752
11753 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11754 {
11755 val = dp->contents[code[i]];
11756 if (!SUB_CHAR_TABLE_P (val))
11757 return (NILP (val) ? dp->defalt : val);
11758 }
11759
11760 /* Here, val is a sub char table. We return the default value of
11761 it. */
11762 return (dp->defalt);
11763 }
11764
11765
11766 \f
11767 /***********************************************************************
11768 Window Redisplay
11769 ***********************************************************************/
11770
11771 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11772
11773 static void
11774 redisplay_windows (window)
11775 Lisp_Object window;
11776 {
11777 while (!NILP (window))
11778 {
11779 struct window *w = XWINDOW (window);
11780
11781 if (!NILP (w->hchild))
11782 redisplay_windows (w->hchild);
11783 else if (!NILP (w->vchild))
11784 redisplay_windows (w->vchild);
11785 else
11786 {
11787 displayed_buffer = XBUFFER (w->buffer);
11788 /* Use list_of_error, not Qerror, so that
11789 we catch only errors and don't run the debugger. */
11790 internal_condition_case_1 (redisplay_window_0, window,
11791 list_of_error,
11792 redisplay_window_error);
11793 }
11794
11795 window = w->next;
11796 }
11797 }
11798
11799 static Lisp_Object
11800 redisplay_window_error ()
11801 {
11802 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11803 return Qnil;
11804 }
11805
11806 static Lisp_Object
11807 redisplay_window_0 (window)
11808 Lisp_Object window;
11809 {
11810 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11811 redisplay_window (window, 0);
11812 return Qnil;
11813 }
11814
11815 static Lisp_Object
11816 redisplay_window_1 (window)
11817 Lisp_Object window;
11818 {
11819 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11820 redisplay_window (window, 1);
11821 return Qnil;
11822 }
11823 \f
11824
11825 /* Increment GLYPH until it reaches END or CONDITION fails while
11826 adding (GLYPH)->pixel_width to X. */
11827
11828 #define SKIP_GLYPHS(glyph, end, x, condition) \
11829 do \
11830 { \
11831 (x) += (glyph)->pixel_width; \
11832 ++(glyph); \
11833 } \
11834 while ((glyph) < (end) && (condition))
11835
11836
11837 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11838 DELTA is the number of bytes by which positions recorded in ROW
11839 differ from current buffer positions.
11840
11841 Return 0 if cursor is not on this row. 1 otherwise. */
11842
11843 int
11844 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11845 struct window *w;
11846 struct glyph_row *row;
11847 struct glyph_matrix *matrix;
11848 int delta, delta_bytes, dy, dvpos;
11849 {
11850 struct glyph *glyph = row->glyphs[TEXT_AREA];
11851 struct glyph *end = glyph + row->used[TEXT_AREA];
11852 struct glyph *cursor = NULL;
11853 /* The first glyph that starts a sequence of glyphs from string. */
11854 struct glyph *string_start;
11855 /* The X coordinate of string_start. */
11856 int string_start_x;
11857 /* The last known character position. */
11858 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11859 /* The last known character position before string_start. */
11860 int string_before_pos;
11861 int x = row->x;
11862 int cursor_x = x;
11863 int cursor_from_overlay_pos = 0;
11864 int pt_old = PT - delta;
11865
11866 /* Skip over glyphs not having an object at the start of the row.
11867 These are special glyphs like truncation marks on terminal
11868 frames. */
11869 if (row->displays_text_p)
11870 while (glyph < end
11871 && INTEGERP (glyph->object)
11872 && glyph->charpos < 0)
11873 {
11874 x += glyph->pixel_width;
11875 ++glyph;
11876 }
11877
11878 string_start = NULL;
11879 while (glyph < end
11880 && !INTEGERP (glyph->object)
11881 && (!BUFFERP (glyph->object)
11882 || (last_pos = glyph->charpos) < pt_old))
11883 {
11884 if (! STRINGP (glyph->object))
11885 {
11886 string_start = NULL;
11887 x += glyph->pixel_width;
11888 ++glyph;
11889 if (cursor_from_overlay_pos
11890 && last_pos >= cursor_from_overlay_pos)
11891 {
11892 cursor_from_overlay_pos = 0;
11893 cursor = 0;
11894 }
11895 }
11896 else
11897 {
11898 if (string_start == NULL)
11899 {
11900 string_before_pos = last_pos;
11901 string_start = glyph;
11902 string_start_x = x;
11903 }
11904 /* Skip all glyphs from string. */
11905 do
11906 {
11907 Lisp_Object cprop;
11908 int pos;
11909 if ((cursor == NULL || glyph > cursor)
11910 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11911 Qcursor, (glyph)->object),
11912 !NILP (cprop))
11913 && (pos = string_buffer_position (w, glyph->object,
11914 string_before_pos),
11915 (pos == 0 /* From overlay */
11916 || pos == pt_old)))
11917 {
11918 /* Estimate overlay buffer position from the buffer
11919 positions of the glyphs before and after the overlay.
11920 Add 1 to last_pos so that if point corresponds to the
11921 glyph right after the overlay, we still use a 'cursor'
11922 property found in that overlay. */
11923 cursor_from_overlay_pos = (pos ? 0 : last_pos
11924 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11925 cursor = glyph;
11926 cursor_x = x;
11927 }
11928 x += glyph->pixel_width;
11929 ++glyph;
11930 }
11931 while (glyph < end && EQ (glyph->object, string_start->object));
11932 }
11933 }
11934
11935 if (cursor != NULL)
11936 {
11937 glyph = cursor;
11938 x = cursor_x;
11939 }
11940 else if (row->ends_in_ellipsis_p && glyph == end)
11941 {
11942 /* Scan back over the ellipsis glyphs, decrementing positions. */
11943 while (glyph > row->glyphs[TEXT_AREA]
11944 && (glyph - 1)->charpos == last_pos)
11945 glyph--, x -= glyph->pixel_width;
11946 /* That loop always goes one position too far,
11947 including the glyph before the ellipsis.
11948 So scan forward over that one. */
11949 x += glyph->pixel_width;
11950 glyph++;
11951 }
11952 else if (string_start
11953 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11954 {
11955 /* We may have skipped over point because the previous glyphs
11956 are from string. As there's no easy way to know the
11957 character position of the current glyph, find the correct
11958 glyph on point by scanning from string_start again. */
11959 Lisp_Object limit;
11960 Lisp_Object string;
11961 struct glyph *stop = glyph;
11962 int pos;
11963
11964 limit = make_number (pt_old + 1);
11965 glyph = string_start;
11966 x = string_start_x;
11967 string = glyph->object;
11968 pos = string_buffer_position (w, string, string_before_pos);
11969 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11970 because we always put cursor after overlay strings. */
11971 while (pos == 0 && glyph < stop)
11972 {
11973 string = glyph->object;
11974 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11975 if (glyph < stop)
11976 pos = string_buffer_position (w, glyph->object, string_before_pos);
11977 }
11978
11979 while (glyph < stop)
11980 {
11981 pos = XINT (Fnext_single_char_property_change
11982 (make_number (pos), Qdisplay, Qnil, limit));
11983 if (pos > pt_old)
11984 break;
11985 /* Skip glyphs from the same string. */
11986 string = glyph->object;
11987 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11988 /* Skip glyphs from an overlay. */
11989 while (glyph < stop
11990 && ! string_buffer_position (w, glyph->object, pos))
11991 {
11992 string = glyph->object;
11993 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11994 }
11995 }
11996
11997 /* If we reached the end of the line, and end was from a string,
11998 cursor is not on this line. */
11999 if (glyph == end && row->continued_p)
12000 return 0;
12001 }
12002
12003 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12004 w->cursor.x = x;
12005 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12006 w->cursor.y = row->y + dy;
12007
12008 if (w == XWINDOW (selected_window))
12009 {
12010 if (!row->continued_p
12011 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12012 && row->x == 0)
12013 {
12014 this_line_buffer = XBUFFER (w->buffer);
12015
12016 CHARPOS (this_line_start_pos)
12017 = MATRIX_ROW_START_CHARPOS (row) + delta;
12018 BYTEPOS (this_line_start_pos)
12019 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12020
12021 CHARPOS (this_line_end_pos)
12022 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12023 BYTEPOS (this_line_end_pos)
12024 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12025
12026 this_line_y = w->cursor.y;
12027 this_line_pixel_height = row->height;
12028 this_line_vpos = w->cursor.vpos;
12029 this_line_start_x = row->x;
12030 }
12031 else
12032 CHARPOS (this_line_start_pos) = 0;
12033 }
12034
12035 return 1;
12036 }
12037
12038
12039 /* Run window scroll functions, if any, for WINDOW with new window
12040 start STARTP. Sets the window start of WINDOW to that position.
12041
12042 We assume that the window's buffer is really current. */
12043
12044 static INLINE struct text_pos
12045 run_window_scroll_functions (window, startp)
12046 Lisp_Object window;
12047 struct text_pos startp;
12048 {
12049 struct window *w = XWINDOW (window);
12050 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12051
12052 if (current_buffer != XBUFFER (w->buffer))
12053 abort ();
12054
12055 if (!NILP (Vwindow_scroll_functions))
12056 {
12057 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12058 make_number (CHARPOS (startp)));
12059 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12060 /* In case the hook functions switch buffers. */
12061 if (current_buffer != XBUFFER (w->buffer))
12062 set_buffer_internal_1 (XBUFFER (w->buffer));
12063 }
12064
12065 return startp;
12066 }
12067
12068
12069 /* Make sure the line containing the cursor is fully visible.
12070 A value of 1 means there is nothing to be done.
12071 (Either the line is fully visible, or it cannot be made so,
12072 or we cannot tell.)
12073
12074 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12075 is higher than window.
12076
12077 A value of 0 means the caller should do scrolling
12078 as if point had gone off the screen. */
12079
12080 static int
12081 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12082 struct window *w;
12083 int force_p;
12084 int current_matrix_p;
12085 {
12086 struct glyph_matrix *matrix;
12087 struct glyph_row *row;
12088 int window_height;
12089
12090 if (!make_cursor_line_fully_visible_p)
12091 return 1;
12092
12093 /* It's not always possible to find the cursor, e.g, when a window
12094 is full of overlay strings. Don't do anything in that case. */
12095 if (w->cursor.vpos < 0)
12096 return 1;
12097
12098 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12099 row = MATRIX_ROW (matrix, w->cursor.vpos);
12100
12101 /* If the cursor row is not partially visible, there's nothing to do. */
12102 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12103 return 1;
12104
12105 /* If the row the cursor is in is taller than the window's height,
12106 it's not clear what to do, so do nothing. */
12107 window_height = window_box_height (w);
12108 if (row->height >= window_height)
12109 {
12110 if (!force_p || MINI_WINDOW_P (w)
12111 || w->vscroll || w->cursor.vpos == 0)
12112 return 1;
12113 }
12114 return 0;
12115
12116 #if 0
12117 /* This code used to try to scroll the window just enough to make
12118 the line visible. It returned 0 to say that the caller should
12119 allocate larger glyph matrices. */
12120
12121 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12122 {
12123 int dy = row->height - row->visible_height;
12124 w->vscroll = 0;
12125 w->cursor.y += dy;
12126 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12127 }
12128 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12129 {
12130 int dy = - (row->height - row->visible_height);
12131 w->vscroll = dy;
12132 w->cursor.y += dy;
12133 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12134 }
12135
12136 /* When we change the cursor y-position of the selected window,
12137 change this_line_y as well so that the display optimization for
12138 the cursor line of the selected window in redisplay_internal uses
12139 the correct y-position. */
12140 if (w == XWINDOW (selected_window))
12141 this_line_y = w->cursor.y;
12142
12143 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12144 redisplay with larger matrices. */
12145 if (matrix->nrows < required_matrix_height (w))
12146 {
12147 fonts_changed_p = 1;
12148 return 0;
12149 }
12150
12151 return 1;
12152 #endif /* 0 */
12153 }
12154
12155
12156 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12157 non-zero means only WINDOW is redisplayed in redisplay_internal.
12158 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12159 in redisplay_window to bring a partially visible line into view in
12160 the case that only the cursor has moved.
12161
12162 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12163 last screen line's vertical height extends past the end of the screen.
12164
12165 Value is
12166
12167 1 if scrolling succeeded
12168
12169 0 if scrolling didn't find point.
12170
12171 -1 if new fonts have been loaded so that we must interrupt
12172 redisplay, adjust glyph matrices, and try again. */
12173
12174 enum
12175 {
12176 SCROLLING_SUCCESS,
12177 SCROLLING_FAILED,
12178 SCROLLING_NEED_LARGER_MATRICES
12179 };
12180
12181 static int
12182 try_scrolling (window, just_this_one_p, scroll_conservatively,
12183 scroll_step, temp_scroll_step, last_line_misfit)
12184 Lisp_Object window;
12185 int just_this_one_p;
12186 EMACS_INT scroll_conservatively, scroll_step;
12187 int temp_scroll_step;
12188 int last_line_misfit;
12189 {
12190 struct window *w = XWINDOW (window);
12191 struct frame *f = XFRAME (w->frame);
12192 struct text_pos scroll_margin_pos;
12193 struct text_pos pos;
12194 struct text_pos startp;
12195 struct it it;
12196 Lisp_Object window_end;
12197 int this_scroll_margin;
12198 int dy = 0;
12199 int scroll_max;
12200 int rc;
12201 int amount_to_scroll = 0;
12202 Lisp_Object aggressive;
12203 int height;
12204 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12205
12206 #if GLYPH_DEBUG
12207 debug_method_add (w, "try_scrolling");
12208 #endif
12209
12210 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12211
12212 /* Compute scroll margin height in pixels. We scroll when point is
12213 within this distance from the top or bottom of the window. */
12214 if (scroll_margin > 0)
12215 {
12216 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12217 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12218 }
12219 else
12220 this_scroll_margin = 0;
12221
12222 /* Force scroll_conservatively to have a reasonable value so it doesn't
12223 cause an overflow while computing how much to scroll. */
12224 if (scroll_conservatively)
12225 scroll_conservatively = min (scroll_conservatively,
12226 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12227
12228 /* Compute how much we should try to scroll maximally to bring point
12229 into view. */
12230 if (scroll_step || scroll_conservatively || temp_scroll_step)
12231 scroll_max = max (scroll_step,
12232 max (scroll_conservatively, temp_scroll_step));
12233 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12234 || NUMBERP (current_buffer->scroll_up_aggressively))
12235 /* We're trying to scroll because of aggressive scrolling
12236 but no scroll_step is set. Choose an arbitrary one. Maybe
12237 there should be a variable for this. */
12238 scroll_max = 10;
12239 else
12240 scroll_max = 0;
12241 scroll_max *= FRAME_LINE_HEIGHT (f);
12242
12243 /* Decide whether we have to scroll down. Start at the window end
12244 and move this_scroll_margin up to find the position of the scroll
12245 margin. */
12246 window_end = Fwindow_end (window, Qt);
12247
12248 too_near_end:
12249
12250 CHARPOS (scroll_margin_pos) = XINT (window_end);
12251 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12252
12253 if (this_scroll_margin || extra_scroll_margin_lines)
12254 {
12255 start_display (&it, w, scroll_margin_pos);
12256 if (this_scroll_margin)
12257 move_it_vertically_backward (&it, this_scroll_margin);
12258 if (extra_scroll_margin_lines)
12259 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12260 scroll_margin_pos = it.current.pos;
12261 }
12262
12263 if (PT >= CHARPOS (scroll_margin_pos))
12264 {
12265 int y0;
12266
12267 /* Point is in the scroll margin at the bottom of the window, or
12268 below. Compute a new window start that makes point visible. */
12269
12270 /* Compute the distance from the scroll margin to PT.
12271 Give up if the distance is greater than scroll_max. */
12272 start_display (&it, w, scroll_margin_pos);
12273 y0 = it.current_y;
12274 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12275 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12276
12277 /* To make point visible, we have to move the window start
12278 down so that the line the cursor is in is visible, which
12279 means we have to add in the height of the cursor line. */
12280 dy = line_bottom_y (&it) - y0;
12281
12282 if (dy > scroll_max)
12283 return SCROLLING_FAILED;
12284
12285 /* Move the window start down. If scrolling conservatively,
12286 move it just enough down to make point visible. If
12287 scroll_step is set, move it down by scroll_step. */
12288 start_display (&it, w, startp);
12289
12290 if (scroll_conservatively)
12291 /* Set AMOUNT_TO_SCROLL to at least one line,
12292 and at most scroll_conservatively lines. */
12293 amount_to_scroll
12294 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12295 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12296 else if (scroll_step || temp_scroll_step)
12297 amount_to_scroll = scroll_max;
12298 else
12299 {
12300 aggressive = current_buffer->scroll_up_aggressively;
12301 height = WINDOW_BOX_TEXT_HEIGHT (w);
12302 if (NUMBERP (aggressive))
12303 {
12304 double float_amount = XFLOATINT (aggressive) * height;
12305 amount_to_scroll = float_amount;
12306 if (amount_to_scroll == 0 && float_amount > 0)
12307 amount_to_scroll = 1;
12308 }
12309 }
12310
12311 if (amount_to_scroll <= 0)
12312 return SCROLLING_FAILED;
12313
12314 /* If moving by amount_to_scroll leaves STARTP unchanged,
12315 move it down one screen line. */
12316
12317 move_it_vertically (&it, amount_to_scroll);
12318 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12319 move_it_by_lines (&it, 1, 1);
12320 startp = it.current.pos;
12321 }
12322 else
12323 {
12324 /* See if point is inside the scroll margin at the top of the
12325 window. */
12326 scroll_margin_pos = startp;
12327 if (this_scroll_margin)
12328 {
12329 start_display (&it, w, startp);
12330 move_it_vertically (&it, this_scroll_margin);
12331 scroll_margin_pos = it.current.pos;
12332 }
12333
12334 if (PT < CHARPOS (scroll_margin_pos))
12335 {
12336 /* Point is in the scroll margin at the top of the window or
12337 above what is displayed in the window. */
12338 int y0;
12339
12340 /* Compute the vertical distance from PT to the scroll
12341 margin position. Give up if distance is greater than
12342 scroll_max. */
12343 SET_TEXT_POS (pos, PT, PT_BYTE);
12344 start_display (&it, w, pos);
12345 y0 = it.current_y;
12346 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12347 it.last_visible_y, -1,
12348 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12349 dy = it.current_y - y0;
12350 if (dy > scroll_max)
12351 return SCROLLING_FAILED;
12352
12353 /* Compute new window start. */
12354 start_display (&it, w, startp);
12355
12356 if (scroll_conservatively)
12357 amount_to_scroll
12358 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12359 else if (scroll_step || temp_scroll_step)
12360 amount_to_scroll = scroll_max;
12361 else
12362 {
12363 aggressive = current_buffer->scroll_down_aggressively;
12364 height = WINDOW_BOX_TEXT_HEIGHT (w);
12365 if (NUMBERP (aggressive))
12366 {
12367 double float_amount = XFLOATINT (aggressive) * height;
12368 amount_to_scroll = float_amount;
12369 if (amount_to_scroll == 0 && float_amount > 0)
12370 amount_to_scroll = 1;
12371 }
12372 }
12373
12374 if (amount_to_scroll <= 0)
12375 return SCROLLING_FAILED;
12376
12377 move_it_vertically_backward (&it, amount_to_scroll);
12378 startp = it.current.pos;
12379 }
12380 }
12381
12382 /* Run window scroll functions. */
12383 startp = run_window_scroll_functions (window, startp);
12384
12385 /* Display the window. Give up if new fonts are loaded, or if point
12386 doesn't appear. */
12387 if (!try_window (window, startp, 0))
12388 rc = SCROLLING_NEED_LARGER_MATRICES;
12389 else if (w->cursor.vpos < 0)
12390 {
12391 clear_glyph_matrix (w->desired_matrix);
12392 rc = SCROLLING_FAILED;
12393 }
12394 else
12395 {
12396 /* Maybe forget recorded base line for line number display. */
12397 if (!just_this_one_p
12398 || current_buffer->clip_changed
12399 || BEG_UNCHANGED < CHARPOS (startp))
12400 w->base_line_number = Qnil;
12401
12402 /* If cursor ends up on a partially visible line,
12403 treat that as being off the bottom of the screen. */
12404 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12405 {
12406 clear_glyph_matrix (w->desired_matrix);
12407 ++extra_scroll_margin_lines;
12408 goto too_near_end;
12409 }
12410 rc = SCROLLING_SUCCESS;
12411 }
12412
12413 return rc;
12414 }
12415
12416
12417 /* Compute a suitable window start for window W if display of W starts
12418 on a continuation line. Value is non-zero if a new window start
12419 was computed.
12420
12421 The new window start will be computed, based on W's width, starting
12422 from the start of the continued line. It is the start of the
12423 screen line with the minimum distance from the old start W->start. */
12424
12425 static int
12426 compute_window_start_on_continuation_line (w)
12427 struct window *w;
12428 {
12429 struct text_pos pos, start_pos;
12430 int window_start_changed_p = 0;
12431
12432 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12433
12434 /* If window start is on a continuation line... Window start may be
12435 < BEGV in case there's invisible text at the start of the
12436 buffer (M-x rmail, for example). */
12437 if (CHARPOS (start_pos) > BEGV
12438 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12439 {
12440 struct it it;
12441 struct glyph_row *row;
12442
12443 /* Handle the case that the window start is out of range. */
12444 if (CHARPOS (start_pos) < BEGV)
12445 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12446 else if (CHARPOS (start_pos) > ZV)
12447 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12448
12449 /* Find the start of the continued line. This should be fast
12450 because scan_buffer is fast (newline cache). */
12451 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12452 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12453 row, DEFAULT_FACE_ID);
12454 reseat_at_previous_visible_line_start (&it);
12455
12456 /* If the line start is "too far" away from the window start,
12457 say it takes too much time to compute a new window start. */
12458 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12459 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12460 {
12461 int min_distance, distance;
12462
12463 /* Move forward by display lines to find the new window
12464 start. If window width was enlarged, the new start can
12465 be expected to be > the old start. If window width was
12466 decreased, the new window start will be < the old start.
12467 So, we're looking for the display line start with the
12468 minimum distance from the old window start. */
12469 pos = it.current.pos;
12470 min_distance = INFINITY;
12471 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12472 distance < min_distance)
12473 {
12474 min_distance = distance;
12475 pos = it.current.pos;
12476 move_it_by_lines (&it, 1, 0);
12477 }
12478
12479 /* Set the window start there. */
12480 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12481 window_start_changed_p = 1;
12482 }
12483 }
12484
12485 return window_start_changed_p;
12486 }
12487
12488
12489 /* Try cursor movement in case text has not changed in window WINDOW,
12490 with window start STARTP. Value is
12491
12492 CURSOR_MOVEMENT_SUCCESS if successful
12493
12494 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12495
12496 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12497 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12498 we want to scroll as if scroll-step were set to 1. See the code.
12499
12500 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12501 which case we have to abort this redisplay, and adjust matrices
12502 first. */
12503
12504 enum
12505 {
12506 CURSOR_MOVEMENT_SUCCESS,
12507 CURSOR_MOVEMENT_CANNOT_BE_USED,
12508 CURSOR_MOVEMENT_MUST_SCROLL,
12509 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12510 };
12511
12512 static int
12513 try_cursor_movement (window, startp, scroll_step)
12514 Lisp_Object window;
12515 struct text_pos startp;
12516 int *scroll_step;
12517 {
12518 struct window *w = XWINDOW (window);
12519 struct frame *f = XFRAME (w->frame);
12520 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12521
12522 #if GLYPH_DEBUG
12523 if (inhibit_try_cursor_movement)
12524 return rc;
12525 #endif
12526
12527 /* Handle case where text has not changed, only point, and it has
12528 not moved off the frame. */
12529 if (/* Point may be in this window. */
12530 PT >= CHARPOS (startp)
12531 /* Selective display hasn't changed. */
12532 && !current_buffer->clip_changed
12533 /* Function force-mode-line-update is used to force a thorough
12534 redisplay. It sets either windows_or_buffers_changed or
12535 update_mode_lines. So don't take a shortcut here for these
12536 cases. */
12537 && !update_mode_lines
12538 && !windows_or_buffers_changed
12539 && !cursor_type_changed
12540 /* Can't use this case if highlighting a region. When a
12541 region exists, cursor movement has to do more than just
12542 set the cursor. */
12543 && !(!NILP (Vtransient_mark_mode)
12544 && !NILP (current_buffer->mark_active))
12545 && NILP (w->region_showing)
12546 && NILP (Vshow_trailing_whitespace)
12547 /* Right after splitting windows, last_point may be nil. */
12548 && INTEGERP (w->last_point)
12549 /* This code is not used for mini-buffer for the sake of the case
12550 of redisplaying to replace an echo area message; since in
12551 that case the mini-buffer contents per se are usually
12552 unchanged. This code is of no real use in the mini-buffer
12553 since the handling of this_line_start_pos, etc., in redisplay
12554 handles the same cases. */
12555 && !EQ (window, minibuf_window)
12556 /* When splitting windows or for new windows, it happens that
12557 redisplay is called with a nil window_end_vpos or one being
12558 larger than the window. This should really be fixed in
12559 window.c. I don't have this on my list, now, so we do
12560 approximately the same as the old redisplay code. --gerd. */
12561 && INTEGERP (w->window_end_vpos)
12562 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12563 && (FRAME_WINDOW_P (f)
12564 || !overlay_arrow_in_current_buffer_p ()))
12565 {
12566 int this_scroll_margin, top_scroll_margin;
12567 struct glyph_row *row = NULL;
12568
12569 #if GLYPH_DEBUG
12570 debug_method_add (w, "cursor movement");
12571 #endif
12572
12573 /* Scroll if point within this distance from the top or bottom
12574 of the window. This is a pixel value. */
12575 this_scroll_margin = max (0, scroll_margin);
12576 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12577 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12578
12579 top_scroll_margin = this_scroll_margin;
12580 if (WINDOW_WANTS_HEADER_LINE_P (w))
12581 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12582
12583 /* Start with the row the cursor was displayed during the last
12584 not paused redisplay. Give up if that row is not valid. */
12585 if (w->last_cursor.vpos < 0
12586 || w->last_cursor.vpos >= w->current_matrix->nrows)
12587 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12588 else
12589 {
12590 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12591 if (row->mode_line_p)
12592 ++row;
12593 if (!row->enabled_p)
12594 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12595 }
12596
12597 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12598 {
12599 int scroll_p = 0;
12600 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12601
12602 if (PT > XFASTINT (w->last_point))
12603 {
12604 /* Point has moved forward. */
12605 while (MATRIX_ROW_END_CHARPOS (row) < PT
12606 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12607 {
12608 xassert (row->enabled_p);
12609 ++row;
12610 }
12611
12612 /* The end position of a row equals the start position
12613 of the next row. If PT is there, we would rather
12614 display it in the next line. */
12615 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12616 && MATRIX_ROW_END_CHARPOS (row) == PT
12617 && !cursor_row_p (w, row))
12618 ++row;
12619
12620 /* If within the scroll margin, scroll. Note that
12621 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12622 the next line would be drawn, and that
12623 this_scroll_margin can be zero. */
12624 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12625 || PT > MATRIX_ROW_END_CHARPOS (row)
12626 /* Line is completely visible last line in window
12627 and PT is to be set in the next line. */
12628 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12629 && PT == MATRIX_ROW_END_CHARPOS (row)
12630 && !row->ends_at_zv_p
12631 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12632 scroll_p = 1;
12633 }
12634 else if (PT < XFASTINT (w->last_point))
12635 {
12636 /* Cursor has to be moved backward. Note that PT >=
12637 CHARPOS (startp) because of the outer if-statement. */
12638 while (!row->mode_line_p
12639 && (MATRIX_ROW_START_CHARPOS (row) > PT
12640 || (MATRIX_ROW_START_CHARPOS (row) == PT
12641 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12642 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12643 row > w->current_matrix->rows
12644 && (row-1)->ends_in_newline_from_string_p))))
12645 && (row->y > top_scroll_margin
12646 || CHARPOS (startp) == BEGV))
12647 {
12648 xassert (row->enabled_p);
12649 --row;
12650 }
12651
12652 /* Consider the following case: Window starts at BEGV,
12653 there is invisible, intangible text at BEGV, so that
12654 display starts at some point START > BEGV. It can
12655 happen that we are called with PT somewhere between
12656 BEGV and START. Try to handle that case. */
12657 if (row < w->current_matrix->rows
12658 || row->mode_line_p)
12659 {
12660 row = w->current_matrix->rows;
12661 if (row->mode_line_p)
12662 ++row;
12663 }
12664
12665 /* Due to newlines in overlay strings, we may have to
12666 skip forward over overlay strings. */
12667 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12668 && MATRIX_ROW_END_CHARPOS (row) == PT
12669 && !cursor_row_p (w, row))
12670 ++row;
12671
12672 /* If within the scroll margin, scroll. */
12673 if (row->y < top_scroll_margin
12674 && CHARPOS (startp) != BEGV)
12675 scroll_p = 1;
12676 }
12677 else
12678 {
12679 /* Cursor did not move. So don't scroll even if cursor line
12680 is partially visible, as it was so before. */
12681 rc = CURSOR_MOVEMENT_SUCCESS;
12682 }
12683
12684 if (PT < MATRIX_ROW_START_CHARPOS (row)
12685 || PT > MATRIX_ROW_END_CHARPOS (row))
12686 {
12687 /* if PT is not in the glyph row, give up. */
12688 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12689 }
12690 else if (rc != CURSOR_MOVEMENT_SUCCESS
12691 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12692 && make_cursor_line_fully_visible_p)
12693 {
12694 if (PT == MATRIX_ROW_END_CHARPOS (row)
12695 && !row->ends_at_zv_p
12696 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12697 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12698 else if (row->height > window_box_height (w))
12699 {
12700 /* If we end up in a partially visible line, let's
12701 make it fully visible, except when it's taller
12702 than the window, in which case we can't do much
12703 about it. */
12704 *scroll_step = 1;
12705 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12706 }
12707 else
12708 {
12709 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12710 if (!cursor_row_fully_visible_p (w, 0, 1))
12711 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12712 else
12713 rc = CURSOR_MOVEMENT_SUCCESS;
12714 }
12715 }
12716 else if (scroll_p)
12717 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12718 else
12719 {
12720 do
12721 {
12722 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12723 {
12724 rc = CURSOR_MOVEMENT_SUCCESS;
12725 break;
12726 }
12727 ++row;
12728 }
12729 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12730 && MATRIX_ROW_START_CHARPOS (row) == PT
12731 && cursor_row_p (w, row));
12732 }
12733 }
12734 }
12735
12736 return rc;
12737 }
12738
12739 void
12740 set_vertical_scroll_bar (w)
12741 struct window *w;
12742 {
12743 int start, end, whole;
12744
12745 /* Calculate the start and end positions for the current window.
12746 At some point, it would be nice to choose between scrollbars
12747 which reflect the whole buffer size, with special markers
12748 indicating narrowing, and scrollbars which reflect only the
12749 visible region.
12750
12751 Note that mini-buffers sometimes aren't displaying any text. */
12752 if (!MINI_WINDOW_P (w)
12753 || (w == XWINDOW (minibuf_window)
12754 && NILP (echo_area_buffer[0])))
12755 {
12756 struct buffer *buf = XBUFFER (w->buffer);
12757 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12758 start = marker_position (w->start) - BUF_BEGV (buf);
12759 /* I don't think this is guaranteed to be right. For the
12760 moment, we'll pretend it is. */
12761 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12762
12763 if (end < start)
12764 end = start;
12765 if (whole < (end - start))
12766 whole = end - start;
12767 }
12768 else
12769 start = end = whole = 0;
12770
12771 /* Indicate what this scroll bar ought to be displaying now. */
12772 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12773 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12774 (w, end - start, whole, start);
12775 }
12776
12777
12778 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12779 selected_window is redisplayed.
12780
12781 We can return without actually redisplaying the window if
12782 fonts_changed_p is nonzero. In that case, redisplay_internal will
12783 retry. */
12784
12785 static void
12786 redisplay_window (window, just_this_one_p)
12787 Lisp_Object window;
12788 int just_this_one_p;
12789 {
12790 struct window *w = XWINDOW (window);
12791 struct frame *f = XFRAME (w->frame);
12792 struct buffer *buffer = XBUFFER (w->buffer);
12793 struct buffer *old = current_buffer;
12794 struct text_pos lpoint, opoint, startp;
12795 int update_mode_line;
12796 int tem;
12797 struct it it;
12798 /* Record it now because it's overwritten. */
12799 int current_matrix_up_to_date_p = 0;
12800 int used_current_matrix_p = 0;
12801 /* This is less strict than current_matrix_up_to_date_p.
12802 It indictes that the buffer contents and narrowing are unchanged. */
12803 int buffer_unchanged_p = 0;
12804 int temp_scroll_step = 0;
12805 int count = SPECPDL_INDEX ();
12806 int rc;
12807 int centering_position = -1;
12808 int last_line_misfit = 0;
12809 int beg_unchanged, end_unchanged;
12810
12811 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12812 opoint = lpoint;
12813
12814 /* W must be a leaf window here. */
12815 xassert (!NILP (w->buffer));
12816 #if GLYPH_DEBUG
12817 *w->desired_matrix->method = 0;
12818 #endif
12819
12820 specbind (Qinhibit_point_motion_hooks, Qt);
12821
12822 reconsider_clip_changes (w, buffer);
12823
12824 /* Has the mode line to be updated? */
12825 update_mode_line = (!NILP (w->update_mode_line)
12826 || update_mode_lines
12827 || buffer->clip_changed
12828 || buffer->prevent_redisplay_optimizations_p);
12829
12830 if (MINI_WINDOW_P (w))
12831 {
12832 if (w == XWINDOW (echo_area_window)
12833 && !NILP (echo_area_buffer[0]))
12834 {
12835 if (update_mode_line)
12836 /* We may have to update a tty frame's menu bar or a
12837 tool-bar. Example `M-x C-h C-h C-g'. */
12838 goto finish_menu_bars;
12839 else
12840 /* We've already displayed the echo area glyphs in this window. */
12841 goto finish_scroll_bars;
12842 }
12843 else if ((w != XWINDOW (minibuf_window)
12844 || minibuf_level == 0)
12845 /* When buffer is nonempty, redisplay window normally. */
12846 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12847 /* Quail displays non-mini buffers in minibuffer window.
12848 In that case, redisplay the window normally. */
12849 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12850 {
12851 /* W is a mini-buffer window, but it's not active, so clear
12852 it. */
12853 int yb = window_text_bottom_y (w);
12854 struct glyph_row *row;
12855 int y;
12856
12857 for (y = 0, row = w->desired_matrix->rows;
12858 y < yb;
12859 y += row->height, ++row)
12860 blank_row (w, row, y);
12861 goto finish_scroll_bars;
12862 }
12863
12864 clear_glyph_matrix (w->desired_matrix);
12865 }
12866
12867 /* Otherwise set up data on this window; select its buffer and point
12868 value. */
12869 /* Really select the buffer, for the sake of buffer-local
12870 variables. */
12871 set_buffer_internal_1 (XBUFFER (w->buffer));
12872 SET_TEXT_POS (opoint, PT, PT_BYTE);
12873
12874 beg_unchanged = BEG_UNCHANGED;
12875 end_unchanged = END_UNCHANGED;
12876
12877 current_matrix_up_to_date_p
12878 = (!NILP (w->window_end_valid)
12879 && !current_buffer->clip_changed
12880 && !current_buffer->prevent_redisplay_optimizations_p
12881 && XFASTINT (w->last_modified) >= MODIFF
12882 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12883
12884 buffer_unchanged_p
12885 = (!NILP (w->window_end_valid)
12886 && !current_buffer->clip_changed
12887 && XFASTINT (w->last_modified) >= MODIFF
12888 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12889
12890 /* When windows_or_buffers_changed is non-zero, we can't rely on
12891 the window end being valid, so set it to nil there. */
12892 if (windows_or_buffers_changed)
12893 {
12894 /* If window starts on a continuation line, maybe adjust the
12895 window start in case the window's width changed. */
12896 if (XMARKER (w->start)->buffer == current_buffer)
12897 compute_window_start_on_continuation_line (w);
12898
12899 w->window_end_valid = Qnil;
12900 }
12901
12902 /* Some sanity checks. */
12903 CHECK_WINDOW_END (w);
12904 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12905 abort ();
12906 if (BYTEPOS (opoint) < CHARPOS (opoint))
12907 abort ();
12908
12909 /* If %c is in mode line, update it if needed. */
12910 if (!NILP (w->column_number_displayed)
12911 /* This alternative quickly identifies a common case
12912 where no change is needed. */
12913 && !(PT == XFASTINT (w->last_point)
12914 && XFASTINT (w->last_modified) >= MODIFF
12915 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12916 && (XFASTINT (w->column_number_displayed)
12917 != (int) current_column ())) /* iftc */
12918 update_mode_line = 1;
12919
12920 /* Count number of windows showing the selected buffer. An indirect
12921 buffer counts as its base buffer. */
12922 if (!just_this_one_p)
12923 {
12924 struct buffer *current_base, *window_base;
12925 current_base = current_buffer;
12926 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12927 if (current_base->base_buffer)
12928 current_base = current_base->base_buffer;
12929 if (window_base->base_buffer)
12930 window_base = window_base->base_buffer;
12931 if (current_base == window_base)
12932 buffer_shared++;
12933 }
12934
12935 /* Point refers normally to the selected window. For any other
12936 window, set up appropriate value. */
12937 if (!EQ (window, selected_window))
12938 {
12939 int new_pt = XMARKER (w->pointm)->charpos;
12940 int new_pt_byte = marker_byte_position (w->pointm);
12941 if (new_pt < BEGV)
12942 {
12943 new_pt = BEGV;
12944 new_pt_byte = BEGV_BYTE;
12945 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12946 }
12947 else if (new_pt > (ZV - 1))
12948 {
12949 new_pt = ZV;
12950 new_pt_byte = ZV_BYTE;
12951 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12952 }
12953
12954 /* We don't use SET_PT so that the point-motion hooks don't run. */
12955 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12956 }
12957
12958 /* If any of the character widths specified in the display table
12959 have changed, invalidate the width run cache. It's true that
12960 this may be a bit late to catch such changes, but the rest of
12961 redisplay goes (non-fatally) haywire when the display table is
12962 changed, so why should we worry about doing any better? */
12963 if (current_buffer->width_run_cache)
12964 {
12965 struct Lisp_Char_Table *disptab = buffer_display_table ();
12966
12967 if (! disptab_matches_widthtab (disptab,
12968 XVECTOR (current_buffer->width_table)))
12969 {
12970 invalidate_region_cache (current_buffer,
12971 current_buffer->width_run_cache,
12972 BEG, Z);
12973 recompute_width_table (current_buffer, disptab);
12974 }
12975 }
12976
12977 /* If window-start is screwed up, choose a new one. */
12978 if (XMARKER (w->start)->buffer != current_buffer)
12979 goto recenter;
12980
12981 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12982
12983 /* If someone specified a new starting point but did not insist,
12984 check whether it can be used. */
12985 if (!NILP (w->optional_new_start)
12986 && CHARPOS (startp) >= BEGV
12987 && CHARPOS (startp) <= ZV)
12988 {
12989 w->optional_new_start = Qnil;
12990 start_display (&it, w, startp);
12991 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12992 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12993 if (IT_CHARPOS (it) == PT)
12994 w->force_start = Qt;
12995 /* IT may overshoot PT if text at PT is invisible. */
12996 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12997 w->force_start = Qt;
12998 }
12999
13000 force_start:
13001
13002 /* Handle case where place to start displaying has been specified,
13003 unless the specified location is outside the accessible range. */
13004 if (!NILP (w->force_start)
13005 || w->frozen_window_start_p)
13006 {
13007 /* We set this later on if we have to adjust point. */
13008 int new_vpos = -1;
13009 int val;
13010
13011 w->force_start = Qnil;
13012 w->vscroll = 0;
13013 w->window_end_valid = Qnil;
13014
13015 /* Forget any recorded base line for line number display. */
13016 if (!buffer_unchanged_p)
13017 w->base_line_number = Qnil;
13018
13019 /* Redisplay the mode line. Select the buffer properly for that.
13020 Also, run the hook window-scroll-functions
13021 because we have scrolled. */
13022 /* Note, we do this after clearing force_start because
13023 if there's an error, it is better to forget about force_start
13024 than to get into an infinite loop calling the hook functions
13025 and having them get more errors. */
13026 if (!update_mode_line
13027 || ! NILP (Vwindow_scroll_functions))
13028 {
13029 update_mode_line = 1;
13030 w->update_mode_line = Qt;
13031 startp = run_window_scroll_functions (window, startp);
13032 }
13033
13034 w->last_modified = make_number (0);
13035 w->last_overlay_modified = make_number (0);
13036 if (CHARPOS (startp) < BEGV)
13037 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13038 else if (CHARPOS (startp) > ZV)
13039 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13040
13041 /* Redisplay, then check if cursor has been set during the
13042 redisplay. Give up if new fonts were loaded. */
13043 val = try_window (window, startp, 1);
13044 if (!val)
13045 {
13046 w->force_start = Qt;
13047 clear_glyph_matrix (w->desired_matrix);
13048 goto need_larger_matrices;
13049 }
13050 /* Point was outside the scroll margins. */
13051 if (val < 0)
13052 new_vpos = window_box_height (w) / 2;
13053
13054 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13055 {
13056 /* If point does not appear, try to move point so it does
13057 appear. The desired matrix has been built above, so we
13058 can use it here. */
13059 new_vpos = window_box_height (w) / 2;
13060 }
13061
13062 if (!cursor_row_fully_visible_p (w, 0, 0))
13063 {
13064 /* Point does appear, but on a line partly visible at end of window.
13065 Move it back to a fully-visible line. */
13066 new_vpos = window_box_height (w);
13067 }
13068
13069 /* If we need to move point for either of the above reasons,
13070 now actually do it. */
13071 if (new_vpos >= 0)
13072 {
13073 struct glyph_row *row;
13074
13075 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13076 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13077 ++row;
13078
13079 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13080 MATRIX_ROW_START_BYTEPOS (row));
13081
13082 if (w != XWINDOW (selected_window))
13083 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13084 else if (current_buffer == old)
13085 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13086
13087 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13088
13089 /* If we are highlighting the region, then we just changed
13090 the region, so redisplay to show it. */
13091 if (!NILP (Vtransient_mark_mode)
13092 && !NILP (current_buffer->mark_active))
13093 {
13094 clear_glyph_matrix (w->desired_matrix);
13095 if (!try_window (window, startp, 0))
13096 goto need_larger_matrices;
13097 }
13098 }
13099
13100 #if GLYPH_DEBUG
13101 debug_method_add (w, "forced window start");
13102 #endif
13103 goto done;
13104 }
13105
13106 /* Handle case where text has not changed, only point, and it has
13107 not moved off the frame, and we are not retrying after hscroll.
13108 (current_matrix_up_to_date_p is nonzero when retrying.) */
13109 if (current_matrix_up_to_date_p
13110 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13111 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13112 {
13113 switch (rc)
13114 {
13115 case CURSOR_MOVEMENT_SUCCESS:
13116 used_current_matrix_p = 1;
13117 goto done;
13118
13119 #if 0 /* try_cursor_movement never returns this value. */
13120 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13121 goto need_larger_matrices;
13122 #endif
13123
13124 case CURSOR_MOVEMENT_MUST_SCROLL:
13125 goto try_to_scroll;
13126
13127 default:
13128 abort ();
13129 }
13130 }
13131 /* If current starting point was originally the beginning of a line
13132 but no longer is, find a new starting point. */
13133 else if (!NILP (w->start_at_line_beg)
13134 && !(CHARPOS (startp) <= BEGV
13135 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13136 {
13137 #if GLYPH_DEBUG
13138 debug_method_add (w, "recenter 1");
13139 #endif
13140 goto recenter;
13141 }
13142
13143 /* Try scrolling with try_window_id. Value is > 0 if update has
13144 been done, it is -1 if we know that the same window start will
13145 not work. It is 0 if unsuccessful for some other reason. */
13146 else if ((tem = try_window_id (w)) != 0)
13147 {
13148 #if GLYPH_DEBUG
13149 debug_method_add (w, "try_window_id %d", tem);
13150 #endif
13151
13152 if (fonts_changed_p)
13153 goto need_larger_matrices;
13154 if (tem > 0)
13155 goto done;
13156
13157 /* Otherwise try_window_id has returned -1 which means that we
13158 don't want the alternative below this comment to execute. */
13159 }
13160 else if (CHARPOS (startp) >= BEGV
13161 && CHARPOS (startp) <= ZV
13162 && PT >= CHARPOS (startp)
13163 && (CHARPOS (startp) < ZV
13164 /* Avoid starting at end of buffer. */
13165 || CHARPOS (startp) == BEGV
13166 || (XFASTINT (w->last_modified) >= MODIFF
13167 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13168 {
13169
13170 /* If first window line is a continuation line, and window start
13171 is inside the modified region, but the first change is before
13172 current window start, we must select a new window start.
13173
13174 However, if this is the result of a down-mouse event (e.g. by
13175 extending the mouse-drag-overlay), we don't want to select a
13176 new window start, since that would change the position under
13177 the mouse, resulting in an unwanted mouse-movement rather
13178 than a simple mouse-click. */
13179 if (NILP (w->start_at_line_beg)
13180 && NILP (do_mouse_tracking)
13181 && CHARPOS (startp) > BEGV
13182 && CHARPOS (startp) > BEG + beg_unchanged
13183 && CHARPOS (startp) <= Z - end_unchanged)
13184 {
13185 w->force_start = Qt;
13186 if (XMARKER (w->start)->buffer == current_buffer)
13187 compute_window_start_on_continuation_line (w);
13188 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13189 goto force_start;
13190 }
13191
13192 #if GLYPH_DEBUG
13193 debug_method_add (w, "same window start");
13194 #endif
13195
13196 /* Try to redisplay starting at same place as before.
13197 If point has not moved off frame, accept the results. */
13198 if (!current_matrix_up_to_date_p
13199 /* Don't use try_window_reusing_current_matrix in this case
13200 because a window scroll function can have changed the
13201 buffer. */
13202 || !NILP (Vwindow_scroll_functions)
13203 || MINI_WINDOW_P (w)
13204 || !(used_current_matrix_p
13205 = try_window_reusing_current_matrix (w)))
13206 {
13207 IF_DEBUG (debug_method_add (w, "1"));
13208 if (try_window (window, startp, 1) < 0)
13209 /* -1 means we need to scroll.
13210 0 means we need new matrices, but fonts_changed_p
13211 is set in that case, so we will detect it below. */
13212 goto try_to_scroll;
13213 }
13214
13215 if (fonts_changed_p)
13216 goto need_larger_matrices;
13217
13218 if (w->cursor.vpos >= 0)
13219 {
13220 if (!just_this_one_p
13221 || current_buffer->clip_changed
13222 || BEG_UNCHANGED < CHARPOS (startp))
13223 /* Forget any recorded base line for line number display. */
13224 w->base_line_number = Qnil;
13225
13226 if (!cursor_row_fully_visible_p (w, 1, 0))
13227 {
13228 clear_glyph_matrix (w->desired_matrix);
13229 last_line_misfit = 1;
13230 }
13231 /* Drop through and scroll. */
13232 else
13233 goto done;
13234 }
13235 else
13236 clear_glyph_matrix (w->desired_matrix);
13237 }
13238
13239 try_to_scroll:
13240
13241 w->last_modified = make_number (0);
13242 w->last_overlay_modified = make_number (0);
13243
13244 /* Redisplay the mode line. Select the buffer properly for that. */
13245 if (!update_mode_line)
13246 {
13247 update_mode_line = 1;
13248 w->update_mode_line = Qt;
13249 }
13250
13251 /* Try to scroll by specified few lines. */
13252 if ((scroll_conservatively
13253 || scroll_step
13254 || temp_scroll_step
13255 || NUMBERP (current_buffer->scroll_up_aggressively)
13256 || NUMBERP (current_buffer->scroll_down_aggressively))
13257 && !current_buffer->clip_changed
13258 && CHARPOS (startp) >= BEGV
13259 && CHARPOS (startp) <= ZV)
13260 {
13261 /* The function returns -1 if new fonts were loaded, 1 if
13262 successful, 0 if not successful. */
13263 int rc = try_scrolling (window, just_this_one_p,
13264 scroll_conservatively,
13265 scroll_step,
13266 temp_scroll_step, last_line_misfit);
13267 switch (rc)
13268 {
13269 case SCROLLING_SUCCESS:
13270 goto done;
13271
13272 case SCROLLING_NEED_LARGER_MATRICES:
13273 goto need_larger_matrices;
13274
13275 case SCROLLING_FAILED:
13276 break;
13277
13278 default:
13279 abort ();
13280 }
13281 }
13282
13283 /* Finally, just choose place to start which centers point */
13284
13285 recenter:
13286 if (centering_position < 0)
13287 centering_position = window_box_height (w) / 2;
13288
13289 #if GLYPH_DEBUG
13290 debug_method_add (w, "recenter");
13291 #endif
13292
13293 /* w->vscroll = 0; */
13294
13295 /* Forget any previously recorded base line for line number display. */
13296 if (!buffer_unchanged_p)
13297 w->base_line_number = Qnil;
13298
13299 /* Move backward half the height of the window. */
13300 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13301 it.current_y = it.last_visible_y;
13302 move_it_vertically_backward (&it, centering_position);
13303 xassert (IT_CHARPOS (it) >= BEGV);
13304
13305 /* The function move_it_vertically_backward may move over more
13306 than the specified y-distance. If it->w is small, e.g. a
13307 mini-buffer window, we may end up in front of the window's
13308 display area. Start displaying at the start of the line
13309 containing PT in this case. */
13310 if (it.current_y <= 0)
13311 {
13312 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13313 move_it_vertically_backward (&it, 0);
13314 #if 0
13315 /* I think this assert is bogus if buffer contains
13316 invisible text or images. KFS. */
13317 xassert (IT_CHARPOS (it) <= PT);
13318 #endif
13319 it.current_y = 0;
13320 }
13321
13322 it.current_x = it.hpos = 0;
13323
13324 /* Set startp here explicitly in case that helps avoid an infinite loop
13325 in case the window-scroll-functions functions get errors. */
13326 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13327
13328 /* Run scroll hooks. */
13329 startp = run_window_scroll_functions (window, it.current.pos);
13330
13331 /* Redisplay the window. */
13332 if (!current_matrix_up_to_date_p
13333 || windows_or_buffers_changed
13334 || cursor_type_changed
13335 /* Don't use try_window_reusing_current_matrix in this case
13336 because it can have changed the buffer. */
13337 || !NILP (Vwindow_scroll_functions)
13338 || !just_this_one_p
13339 || MINI_WINDOW_P (w)
13340 || !(used_current_matrix_p
13341 = try_window_reusing_current_matrix (w)))
13342 try_window (window, startp, 0);
13343
13344 /* If new fonts have been loaded (due to fontsets), give up. We
13345 have to start a new redisplay since we need to re-adjust glyph
13346 matrices. */
13347 if (fonts_changed_p)
13348 goto need_larger_matrices;
13349
13350 /* If cursor did not appear assume that the middle of the window is
13351 in the first line of the window. Do it again with the next line.
13352 (Imagine a window of height 100, displaying two lines of height
13353 60. Moving back 50 from it->last_visible_y will end in the first
13354 line.) */
13355 if (w->cursor.vpos < 0)
13356 {
13357 if (!NILP (w->window_end_valid)
13358 && PT >= Z - XFASTINT (w->window_end_pos))
13359 {
13360 clear_glyph_matrix (w->desired_matrix);
13361 move_it_by_lines (&it, 1, 0);
13362 try_window (window, it.current.pos, 0);
13363 }
13364 else if (PT < IT_CHARPOS (it))
13365 {
13366 clear_glyph_matrix (w->desired_matrix);
13367 move_it_by_lines (&it, -1, 0);
13368 try_window (window, it.current.pos, 0);
13369 }
13370 else
13371 {
13372 /* Not much we can do about it. */
13373 }
13374 }
13375
13376 /* Consider the following case: Window starts at BEGV, there is
13377 invisible, intangible text at BEGV, so that display starts at
13378 some point START > BEGV. It can happen that we are called with
13379 PT somewhere between BEGV and START. Try to handle that case. */
13380 if (w->cursor.vpos < 0)
13381 {
13382 struct glyph_row *row = w->current_matrix->rows;
13383 if (row->mode_line_p)
13384 ++row;
13385 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13386 }
13387
13388 if (!cursor_row_fully_visible_p (w, 0, 0))
13389 {
13390 /* If vscroll is enabled, disable it and try again. */
13391 if (w->vscroll)
13392 {
13393 w->vscroll = 0;
13394 clear_glyph_matrix (w->desired_matrix);
13395 goto recenter;
13396 }
13397
13398 /* If centering point failed to make the whole line visible,
13399 put point at the top instead. That has to make the whole line
13400 visible, if it can be done. */
13401 if (centering_position == 0)
13402 goto done;
13403
13404 clear_glyph_matrix (w->desired_matrix);
13405 centering_position = 0;
13406 goto recenter;
13407 }
13408
13409 done:
13410
13411 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13412 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13413 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13414 ? Qt : Qnil);
13415
13416 /* Display the mode line, if we must. */
13417 if ((update_mode_line
13418 /* If window not full width, must redo its mode line
13419 if (a) the window to its side is being redone and
13420 (b) we do a frame-based redisplay. This is a consequence
13421 of how inverted lines are drawn in frame-based redisplay. */
13422 || (!just_this_one_p
13423 && !FRAME_WINDOW_P (f)
13424 && !WINDOW_FULL_WIDTH_P (w))
13425 /* Line number to display. */
13426 || INTEGERP (w->base_line_pos)
13427 /* Column number is displayed and different from the one displayed. */
13428 || (!NILP (w->column_number_displayed)
13429 && (XFASTINT (w->column_number_displayed)
13430 != (int) current_column ()))) /* iftc */
13431 /* This means that the window has a mode line. */
13432 && (WINDOW_WANTS_MODELINE_P (w)
13433 || WINDOW_WANTS_HEADER_LINE_P (w)))
13434 {
13435 display_mode_lines (w);
13436
13437 /* If mode line height has changed, arrange for a thorough
13438 immediate redisplay using the correct mode line height. */
13439 if (WINDOW_WANTS_MODELINE_P (w)
13440 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13441 {
13442 fonts_changed_p = 1;
13443 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13444 = DESIRED_MODE_LINE_HEIGHT (w);
13445 }
13446
13447 /* If top line height has changed, arrange for a thorough
13448 immediate redisplay using the correct mode line height. */
13449 if (WINDOW_WANTS_HEADER_LINE_P (w)
13450 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13451 {
13452 fonts_changed_p = 1;
13453 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13454 = DESIRED_HEADER_LINE_HEIGHT (w);
13455 }
13456
13457 if (fonts_changed_p)
13458 goto need_larger_matrices;
13459 }
13460
13461 if (!line_number_displayed
13462 && !BUFFERP (w->base_line_pos))
13463 {
13464 w->base_line_pos = Qnil;
13465 w->base_line_number = Qnil;
13466 }
13467
13468 finish_menu_bars:
13469
13470 /* When we reach a frame's selected window, redo the frame's menu bar. */
13471 if (update_mode_line
13472 && EQ (FRAME_SELECTED_WINDOW (f), window))
13473 {
13474 int redisplay_menu_p = 0;
13475 int redisplay_tool_bar_p = 0;
13476
13477 if (FRAME_WINDOW_P (f))
13478 {
13479 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13480 || defined (USE_GTK)
13481 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13482 #else
13483 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13484 #endif
13485 }
13486 else
13487 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13488
13489 if (redisplay_menu_p)
13490 display_menu_bar (w);
13491
13492 #ifdef HAVE_WINDOW_SYSTEM
13493 if (FRAME_WINDOW_P (f))
13494 {
13495 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13496 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13497 #else
13498 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13499 && (FRAME_TOOL_BAR_LINES (f) > 0
13500 || !NILP (Vauto_resize_tool_bars));
13501 #endif
13502
13503 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13504 {
13505 extern int ignore_mouse_drag_p;
13506 ignore_mouse_drag_p = 1;
13507 }
13508 }
13509 #endif
13510 }
13511
13512 #ifdef HAVE_WINDOW_SYSTEM
13513 if (FRAME_WINDOW_P (f)
13514 && update_window_fringes (w, (just_this_one_p
13515 || (!used_current_matrix_p && !overlay_arrow_seen)
13516 || w->pseudo_window_p)))
13517 {
13518 update_begin (f);
13519 BLOCK_INPUT;
13520 if (draw_window_fringes (w, 1))
13521 x_draw_vertical_border (w);
13522 UNBLOCK_INPUT;
13523 update_end (f);
13524 }
13525 #endif /* HAVE_WINDOW_SYSTEM */
13526
13527 /* We go to this label, with fonts_changed_p nonzero,
13528 if it is necessary to try again using larger glyph matrices.
13529 We have to redeem the scroll bar even in this case,
13530 because the loop in redisplay_internal expects that. */
13531 need_larger_matrices:
13532 ;
13533 finish_scroll_bars:
13534
13535 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13536 {
13537 /* Set the thumb's position and size. */
13538 set_vertical_scroll_bar (w);
13539
13540 /* Note that we actually used the scroll bar attached to this
13541 window, so it shouldn't be deleted at the end of redisplay. */
13542 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13543 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13544 }
13545
13546 /* Restore current_buffer and value of point in it. */
13547 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13548 set_buffer_internal_1 (old);
13549 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13550 shorter. This can be caused by log truncation in *Messages*. */
13551 if (CHARPOS (lpoint) <= ZV)
13552 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13553
13554 unbind_to (count, Qnil);
13555 }
13556
13557
13558 /* Build the complete desired matrix of WINDOW with a window start
13559 buffer position POS.
13560
13561 Value is 1 if successful. It is zero if fonts were loaded during
13562 redisplay which makes re-adjusting glyph matrices necessary, and -1
13563 if point would appear in the scroll margins.
13564 (We check that only if CHECK_MARGINS is nonzero. */
13565
13566 int
13567 try_window (window, pos, check_margins)
13568 Lisp_Object window;
13569 struct text_pos pos;
13570 int check_margins;
13571 {
13572 struct window *w = XWINDOW (window);
13573 struct it it;
13574 struct glyph_row *last_text_row = NULL;
13575 struct frame *f = XFRAME (w->frame);
13576
13577 /* Make POS the new window start. */
13578 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13579
13580 /* Mark cursor position as unknown. No overlay arrow seen. */
13581 w->cursor.vpos = -1;
13582 overlay_arrow_seen = 0;
13583
13584 /* Initialize iterator and info to start at POS. */
13585 start_display (&it, w, pos);
13586
13587 /* Display all lines of W. */
13588 while (it.current_y < it.last_visible_y)
13589 {
13590 if (display_line (&it))
13591 last_text_row = it.glyph_row - 1;
13592 if (fonts_changed_p)
13593 return 0;
13594 }
13595
13596 /* Don't let the cursor end in the scroll margins. */
13597 if (check_margins
13598 && !MINI_WINDOW_P (w))
13599 {
13600 int this_scroll_margin;
13601
13602 this_scroll_margin = max (0, scroll_margin);
13603 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13604 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13605
13606 if ((w->cursor.y >= 0 /* not vscrolled */
13607 && w->cursor.y < this_scroll_margin
13608 && CHARPOS (pos) > BEGV
13609 && IT_CHARPOS (it) < ZV)
13610 /* rms: considering make_cursor_line_fully_visible_p here
13611 seems to give wrong results. We don't want to recenter
13612 when the last line is partly visible, we want to allow
13613 that case to be handled in the usual way. */
13614 || (w->cursor.y + 1) > it.last_visible_y)
13615 {
13616 w->cursor.vpos = -1;
13617 clear_glyph_matrix (w->desired_matrix);
13618 return -1;
13619 }
13620 }
13621
13622 /* If bottom moved off end of frame, change mode line percentage. */
13623 if (XFASTINT (w->window_end_pos) <= 0
13624 && Z != IT_CHARPOS (it))
13625 w->update_mode_line = Qt;
13626
13627 /* Set window_end_pos to the offset of the last character displayed
13628 on the window from the end of current_buffer. Set
13629 window_end_vpos to its row number. */
13630 if (last_text_row)
13631 {
13632 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13633 w->window_end_bytepos
13634 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13635 w->window_end_pos
13636 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13637 w->window_end_vpos
13638 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13639 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13640 ->displays_text_p);
13641 }
13642 else
13643 {
13644 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13645 w->window_end_pos = make_number (Z - ZV);
13646 w->window_end_vpos = make_number (0);
13647 }
13648
13649 /* But that is not valid info until redisplay finishes. */
13650 w->window_end_valid = Qnil;
13651 return 1;
13652 }
13653
13654
13655 \f
13656 /************************************************************************
13657 Window redisplay reusing current matrix when buffer has not changed
13658 ************************************************************************/
13659
13660 /* Try redisplay of window W showing an unchanged buffer with a
13661 different window start than the last time it was displayed by
13662 reusing its current matrix. Value is non-zero if successful.
13663 W->start is the new window start. */
13664
13665 static int
13666 try_window_reusing_current_matrix (w)
13667 struct window *w;
13668 {
13669 struct frame *f = XFRAME (w->frame);
13670 struct glyph_row *row, *bottom_row;
13671 struct it it;
13672 struct run run;
13673 struct text_pos start, new_start;
13674 int nrows_scrolled, i;
13675 struct glyph_row *last_text_row;
13676 struct glyph_row *last_reused_text_row;
13677 struct glyph_row *start_row;
13678 int start_vpos, min_y, max_y;
13679
13680 #if GLYPH_DEBUG
13681 if (inhibit_try_window_reusing)
13682 return 0;
13683 #endif
13684
13685 if (/* This function doesn't handle terminal frames. */
13686 !FRAME_WINDOW_P (f)
13687 /* Don't try to reuse the display if windows have been split
13688 or such. */
13689 || windows_or_buffers_changed
13690 || cursor_type_changed)
13691 return 0;
13692
13693 /* Can't do this if region may have changed. */
13694 if ((!NILP (Vtransient_mark_mode)
13695 && !NILP (current_buffer->mark_active))
13696 || !NILP (w->region_showing)
13697 || !NILP (Vshow_trailing_whitespace))
13698 return 0;
13699
13700 /* If top-line visibility has changed, give up. */
13701 if (WINDOW_WANTS_HEADER_LINE_P (w)
13702 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13703 return 0;
13704
13705 /* Give up if old or new display is scrolled vertically. We could
13706 make this function handle this, but right now it doesn't. */
13707 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13708 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13709 return 0;
13710
13711 /* The variable new_start now holds the new window start. The old
13712 start `start' can be determined from the current matrix. */
13713 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13714 start = start_row->start.pos;
13715 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13716
13717 /* Clear the desired matrix for the display below. */
13718 clear_glyph_matrix (w->desired_matrix);
13719
13720 if (CHARPOS (new_start) <= CHARPOS (start))
13721 {
13722 int first_row_y;
13723
13724 /* Don't use this method if the display starts with an ellipsis
13725 displayed for invisible text. It's not easy to handle that case
13726 below, and it's certainly not worth the effort since this is
13727 not a frequent case. */
13728 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13729 return 0;
13730
13731 IF_DEBUG (debug_method_add (w, "twu1"));
13732
13733 /* Display up to a row that can be reused. The variable
13734 last_text_row is set to the last row displayed that displays
13735 text. Note that it.vpos == 0 if or if not there is a
13736 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13737 start_display (&it, w, new_start);
13738 first_row_y = it.current_y;
13739 w->cursor.vpos = -1;
13740 last_text_row = last_reused_text_row = NULL;
13741
13742 while (it.current_y < it.last_visible_y
13743 && !fonts_changed_p)
13744 {
13745 /* If we have reached into the characters in the START row,
13746 that means the line boundaries have changed. So we
13747 can't start copying with the row START. Maybe it will
13748 work to start copying with the following row. */
13749 while (IT_CHARPOS (it) > CHARPOS (start))
13750 {
13751 /* Advance to the next row as the "start". */
13752 start_row++;
13753 start = start_row->start.pos;
13754 /* If there are no more rows to try, or just one, give up. */
13755 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13756 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13757 || CHARPOS (start) == ZV)
13758 {
13759 clear_glyph_matrix (w->desired_matrix);
13760 return 0;
13761 }
13762
13763 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13764 }
13765 /* If we have reached alignment,
13766 we can copy the rest of the rows. */
13767 if (IT_CHARPOS (it) == CHARPOS (start))
13768 break;
13769
13770 if (display_line (&it))
13771 last_text_row = it.glyph_row - 1;
13772 }
13773
13774 /* A value of current_y < last_visible_y means that we stopped
13775 at the previous window start, which in turn means that we
13776 have at least one reusable row. */
13777 if (it.current_y < it.last_visible_y)
13778 {
13779 /* IT.vpos always starts from 0; it counts text lines. */
13780 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13781
13782 /* Find PT if not already found in the lines displayed. */
13783 if (w->cursor.vpos < 0)
13784 {
13785 int dy = it.current_y - start_row->y;
13786
13787 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13788 row = row_containing_pos (w, PT, row, NULL, dy);
13789 if (row)
13790 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13791 dy, nrows_scrolled);
13792 else
13793 {
13794 clear_glyph_matrix (w->desired_matrix);
13795 return 0;
13796 }
13797 }
13798
13799 /* Scroll the display. Do it before the current matrix is
13800 changed. The problem here is that update has not yet
13801 run, i.e. part of the current matrix is not up to date.
13802 scroll_run_hook will clear the cursor, and use the
13803 current matrix to get the height of the row the cursor is
13804 in. */
13805 run.current_y = start_row->y;
13806 run.desired_y = it.current_y;
13807 run.height = it.last_visible_y - it.current_y;
13808
13809 if (run.height > 0 && run.current_y != run.desired_y)
13810 {
13811 update_begin (f);
13812 FRAME_RIF (f)->update_window_begin_hook (w);
13813 FRAME_RIF (f)->clear_window_mouse_face (w);
13814 FRAME_RIF (f)->scroll_run_hook (w, &run);
13815 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13816 update_end (f);
13817 }
13818
13819 /* Shift current matrix down by nrows_scrolled lines. */
13820 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13821 rotate_matrix (w->current_matrix,
13822 start_vpos,
13823 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13824 nrows_scrolled);
13825
13826 /* Disable lines that must be updated. */
13827 for (i = 0; i < nrows_scrolled; ++i)
13828 (start_row + i)->enabled_p = 0;
13829
13830 /* Re-compute Y positions. */
13831 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13832 max_y = it.last_visible_y;
13833 for (row = start_row + nrows_scrolled;
13834 row < bottom_row;
13835 ++row)
13836 {
13837 row->y = it.current_y;
13838 row->visible_height = row->height;
13839
13840 if (row->y < min_y)
13841 row->visible_height -= min_y - row->y;
13842 if (row->y + row->height > max_y)
13843 row->visible_height -= row->y + row->height - max_y;
13844 row->redraw_fringe_bitmaps_p = 1;
13845
13846 it.current_y += row->height;
13847
13848 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13849 last_reused_text_row = row;
13850 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13851 break;
13852 }
13853
13854 /* Disable lines in the current matrix which are now
13855 below the window. */
13856 for (++row; row < bottom_row; ++row)
13857 row->enabled_p = row->mode_line_p = 0;
13858 }
13859
13860 /* Update window_end_pos etc.; last_reused_text_row is the last
13861 reused row from the current matrix containing text, if any.
13862 The value of last_text_row is the last displayed line
13863 containing text. */
13864 if (last_reused_text_row)
13865 {
13866 w->window_end_bytepos
13867 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13868 w->window_end_pos
13869 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13870 w->window_end_vpos
13871 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13872 w->current_matrix));
13873 }
13874 else if (last_text_row)
13875 {
13876 w->window_end_bytepos
13877 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13878 w->window_end_pos
13879 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13880 w->window_end_vpos
13881 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13882 }
13883 else
13884 {
13885 /* This window must be completely empty. */
13886 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13887 w->window_end_pos = make_number (Z - ZV);
13888 w->window_end_vpos = make_number (0);
13889 }
13890 w->window_end_valid = Qnil;
13891
13892 /* Update hint: don't try scrolling again in update_window. */
13893 w->desired_matrix->no_scrolling_p = 1;
13894
13895 #if GLYPH_DEBUG
13896 debug_method_add (w, "try_window_reusing_current_matrix 1");
13897 #endif
13898 return 1;
13899 }
13900 else if (CHARPOS (new_start) > CHARPOS (start))
13901 {
13902 struct glyph_row *pt_row, *row;
13903 struct glyph_row *first_reusable_row;
13904 struct glyph_row *first_row_to_display;
13905 int dy;
13906 int yb = window_text_bottom_y (w);
13907
13908 /* Find the row starting at new_start, if there is one. Don't
13909 reuse a partially visible line at the end. */
13910 first_reusable_row = start_row;
13911 while (first_reusable_row->enabled_p
13912 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13913 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13914 < CHARPOS (new_start)))
13915 ++first_reusable_row;
13916
13917 /* Give up if there is no row to reuse. */
13918 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13919 || !first_reusable_row->enabled_p
13920 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13921 != CHARPOS (new_start)))
13922 return 0;
13923
13924 /* We can reuse fully visible rows beginning with
13925 first_reusable_row to the end of the window. Set
13926 first_row_to_display to the first row that cannot be reused.
13927 Set pt_row to the row containing point, if there is any. */
13928 pt_row = NULL;
13929 for (first_row_to_display = first_reusable_row;
13930 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13931 ++first_row_to_display)
13932 {
13933 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13934 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13935 pt_row = first_row_to_display;
13936 }
13937
13938 /* Start displaying at the start of first_row_to_display. */
13939 xassert (first_row_to_display->y < yb);
13940 init_to_row_start (&it, w, first_row_to_display);
13941
13942 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13943 - start_vpos);
13944 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13945 - nrows_scrolled);
13946 it.current_y = (first_row_to_display->y - first_reusable_row->y
13947 + WINDOW_HEADER_LINE_HEIGHT (w));
13948
13949 /* Display lines beginning with first_row_to_display in the
13950 desired matrix. Set last_text_row to the last row displayed
13951 that displays text. */
13952 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13953 if (pt_row == NULL)
13954 w->cursor.vpos = -1;
13955 last_text_row = NULL;
13956 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13957 if (display_line (&it))
13958 last_text_row = it.glyph_row - 1;
13959
13960 /* Give up If point isn't in a row displayed or reused. */
13961 if (w->cursor.vpos < 0)
13962 {
13963 clear_glyph_matrix (w->desired_matrix);
13964 return 0;
13965 }
13966
13967 /* If point is in a reused row, adjust y and vpos of the cursor
13968 position. */
13969 if (pt_row)
13970 {
13971 w->cursor.vpos -= nrows_scrolled;
13972 w->cursor.y -= first_reusable_row->y - start_row->y;
13973 }
13974
13975 /* Scroll the display. */
13976 run.current_y = first_reusable_row->y;
13977 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13978 run.height = it.last_visible_y - run.current_y;
13979 dy = run.current_y - run.desired_y;
13980
13981 if (run.height)
13982 {
13983 update_begin (f);
13984 FRAME_RIF (f)->update_window_begin_hook (w);
13985 FRAME_RIF (f)->clear_window_mouse_face (w);
13986 FRAME_RIF (f)->scroll_run_hook (w, &run);
13987 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13988 update_end (f);
13989 }
13990
13991 /* Adjust Y positions of reused rows. */
13992 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13993 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13994 max_y = it.last_visible_y;
13995 for (row = first_reusable_row; row < first_row_to_display; ++row)
13996 {
13997 row->y -= dy;
13998 row->visible_height = row->height;
13999 if (row->y < min_y)
14000 row->visible_height -= min_y - row->y;
14001 if (row->y + row->height > max_y)
14002 row->visible_height -= row->y + row->height - max_y;
14003 row->redraw_fringe_bitmaps_p = 1;
14004 }
14005
14006 /* Scroll the current matrix. */
14007 xassert (nrows_scrolled > 0);
14008 rotate_matrix (w->current_matrix,
14009 start_vpos,
14010 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14011 -nrows_scrolled);
14012
14013 /* Disable rows not reused. */
14014 for (row -= nrows_scrolled; row < bottom_row; ++row)
14015 row->enabled_p = 0;
14016
14017 /* Point may have moved to a different line, so we cannot assume that
14018 the previous cursor position is valid; locate the correct row. */
14019 if (pt_row)
14020 {
14021 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14022 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14023 row++)
14024 {
14025 w->cursor.vpos++;
14026 w->cursor.y = row->y;
14027 }
14028 if (row < bottom_row)
14029 {
14030 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14031 while (glyph->charpos < PT)
14032 {
14033 w->cursor.hpos++;
14034 w->cursor.x += glyph->pixel_width;
14035 glyph++;
14036 }
14037 }
14038 }
14039
14040 /* Adjust window end. A null value of last_text_row means that
14041 the window end is in reused rows which in turn means that
14042 only its vpos can have changed. */
14043 if (last_text_row)
14044 {
14045 w->window_end_bytepos
14046 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14047 w->window_end_pos
14048 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14049 w->window_end_vpos
14050 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14051 }
14052 else
14053 {
14054 w->window_end_vpos
14055 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14056 }
14057
14058 w->window_end_valid = Qnil;
14059 w->desired_matrix->no_scrolling_p = 1;
14060
14061 #if GLYPH_DEBUG
14062 debug_method_add (w, "try_window_reusing_current_matrix 2");
14063 #endif
14064 return 1;
14065 }
14066
14067 return 0;
14068 }
14069
14070
14071 \f
14072 /************************************************************************
14073 Window redisplay reusing current matrix when buffer has changed
14074 ************************************************************************/
14075
14076 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14077 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14078 int *, int *));
14079 static struct glyph_row *
14080 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14081 struct glyph_row *));
14082
14083
14084 /* Return the last row in MATRIX displaying text. If row START is
14085 non-null, start searching with that row. IT gives the dimensions
14086 of the display. Value is null if matrix is empty; otherwise it is
14087 a pointer to the row found. */
14088
14089 static struct glyph_row *
14090 find_last_row_displaying_text (matrix, it, start)
14091 struct glyph_matrix *matrix;
14092 struct it *it;
14093 struct glyph_row *start;
14094 {
14095 struct glyph_row *row, *row_found;
14096
14097 /* Set row_found to the last row in IT->w's current matrix
14098 displaying text. The loop looks funny but think of partially
14099 visible lines. */
14100 row_found = NULL;
14101 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14102 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14103 {
14104 xassert (row->enabled_p);
14105 row_found = row;
14106 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14107 break;
14108 ++row;
14109 }
14110
14111 return row_found;
14112 }
14113
14114
14115 /* Return the last row in the current matrix of W that is not affected
14116 by changes at the start of current_buffer that occurred since W's
14117 current matrix was built. Value is null if no such row exists.
14118
14119 BEG_UNCHANGED us the number of characters unchanged at the start of
14120 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14121 first changed character in current_buffer. Characters at positions <
14122 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14123 when the current matrix was built. */
14124
14125 static struct glyph_row *
14126 find_last_unchanged_at_beg_row (w)
14127 struct window *w;
14128 {
14129 int first_changed_pos = BEG + BEG_UNCHANGED;
14130 struct glyph_row *row;
14131 struct glyph_row *row_found = NULL;
14132 int yb = window_text_bottom_y (w);
14133
14134 /* Find the last row displaying unchanged text. */
14135 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14136 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14137 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14138 {
14139 if (/* If row ends before first_changed_pos, it is unchanged,
14140 except in some case. */
14141 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14142 /* When row ends in ZV and we write at ZV it is not
14143 unchanged. */
14144 && !row->ends_at_zv_p
14145 /* When first_changed_pos is the end of a continued line,
14146 row is not unchanged because it may be no longer
14147 continued. */
14148 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14149 && (row->continued_p
14150 || row->exact_window_width_line_p)))
14151 row_found = row;
14152
14153 /* Stop if last visible row. */
14154 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14155 break;
14156
14157 ++row;
14158 }
14159
14160 return row_found;
14161 }
14162
14163
14164 /* Find the first glyph row in the current matrix of W that is not
14165 affected by changes at the end of current_buffer since the
14166 time W's current matrix was built.
14167
14168 Return in *DELTA the number of chars by which buffer positions in
14169 unchanged text at the end of current_buffer must be adjusted.
14170
14171 Return in *DELTA_BYTES the corresponding number of bytes.
14172
14173 Value is null if no such row exists, i.e. all rows are affected by
14174 changes. */
14175
14176 static struct glyph_row *
14177 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14178 struct window *w;
14179 int *delta, *delta_bytes;
14180 {
14181 struct glyph_row *row;
14182 struct glyph_row *row_found = NULL;
14183
14184 *delta = *delta_bytes = 0;
14185
14186 /* Display must not have been paused, otherwise the current matrix
14187 is not up to date. */
14188 if (NILP (w->window_end_valid))
14189 abort ();
14190
14191 /* A value of window_end_pos >= END_UNCHANGED means that the window
14192 end is in the range of changed text. If so, there is no
14193 unchanged row at the end of W's current matrix. */
14194 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14195 return NULL;
14196
14197 /* Set row to the last row in W's current matrix displaying text. */
14198 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14199
14200 /* If matrix is entirely empty, no unchanged row exists. */
14201 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14202 {
14203 /* The value of row is the last glyph row in the matrix having a
14204 meaningful buffer position in it. The end position of row
14205 corresponds to window_end_pos. This allows us to translate
14206 buffer positions in the current matrix to current buffer
14207 positions for characters not in changed text. */
14208 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14209 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14210 int last_unchanged_pos, last_unchanged_pos_old;
14211 struct glyph_row *first_text_row
14212 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14213
14214 *delta = Z - Z_old;
14215 *delta_bytes = Z_BYTE - Z_BYTE_old;
14216
14217 /* Set last_unchanged_pos to the buffer position of the last
14218 character in the buffer that has not been changed. Z is the
14219 index + 1 of the last character in current_buffer, i.e. by
14220 subtracting END_UNCHANGED we get the index of the last
14221 unchanged character, and we have to add BEG to get its buffer
14222 position. */
14223 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14224 last_unchanged_pos_old = last_unchanged_pos - *delta;
14225
14226 /* Search backward from ROW for a row displaying a line that
14227 starts at a minimum position >= last_unchanged_pos_old. */
14228 for (; row > first_text_row; --row)
14229 {
14230 /* This used to abort, but it can happen.
14231 It is ok to just stop the search instead here. KFS. */
14232 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14233 break;
14234
14235 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14236 row_found = row;
14237 }
14238 }
14239
14240 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14241 abort ();
14242
14243 return row_found;
14244 }
14245
14246
14247 /* Make sure that glyph rows in the current matrix of window W
14248 reference the same glyph memory as corresponding rows in the
14249 frame's frame matrix. This function is called after scrolling W's
14250 current matrix on a terminal frame in try_window_id and
14251 try_window_reusing_current_matrix. */
14252
14253 static void
14254 sync_frame_with_window_matrix_rows (w)
14255 struct window *w;
14256 {
14257 struct frame *f = XFRAME (w->frame);
14258 struct glyph_row *window_row, *window_row_end, *frame_row;
14259
14260 /* Preconditions: W must be a leaf window and full-width. Its frame
14261 must have a frame matrix. */
14262 xassert (NILP (w->hchild) && NILP (w->vchild));
14263 xassert (WINDOW_FULL_WIDTH_P (w));
14264 xassert (!FRAME_WINDOW_P (f));
14265
14266 /* If W is a full-width window, glyph pointers in W's current matrix
14267 have, by definition, to be the same as glyph pointers in the
14268 corresponding frame matrix. Note that frame matrices have no
14269 marginal areas (see build_frame_matrix). */
14270 window_row = w->current_matrix->rows;
14271 window_row_end = window_row + w->current_matrix->nrows;
14272 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14273 while (window_row < window_row_end)
14274 {
14275 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14276 struct glyph *end = window_row->glyphs[LAST_AREA];
14277
14278 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14279 frame_row->glyphs[TEXT_AREA] = start;
14280 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14281 frame_row->glyphs[LAST_AREA] = end;
14282
14283 /* Disable frame rows whose corresponding window rows have
14284 been disabled in try_window_id. */
14285 if (!window_row->enabled_p)
14286 frame_row->enabled_p = 0;
14287
14288 ++window_row, ++frame_row;
14289 }
14290 }
14291
14292
14293 /* Find the glyph row in window W containing CHARPOS. Consider all
14294 rows between START and END (not inclusive). END null means search
14295 all rows to the end of the display area of W. Value is the row
14296 containing CHARPOS or null. */
14297
14298 struct glyph_row *
14299 row_containing_pos (w, charpos, start, end, dy)
14300 struct window *w;
14301 int charpos;
14302 struct glyph_row *start, *end;
14303 int dy;
14304 {
14305 struct glyph_row *row = start;
14306 int last_y;
14307
14308 /* If we happen to start on a header-line, skip that. */
14309 if (row->mode_line_p)
14310 ++row;
14311
14312 if ((end && row >= end) || !row->enabled_p)
14313 return NULL;
14314
14315 last_y = window_text_bottom_y (w) - dy;
14316
14317 while (1)
14318 {
14319 /* Give up if we have gone too far. */
14320 if (end && row >= end)
14321 return NULL;
14322 /* This formerly returned if they were equal.
14323 I think that both quantities are of a "last plus one" type;
14324 if so, when they are equal, the row is within the screen. -- rms. */
14325 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14326 return NULL;
14327
14328 /* If it is in this row, return this row. */
14329 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14330 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14331 /* The end position of a row equals the start
14332 position of the next row. If CHARPOS is there, we
14333 would rather display it in the next line, except
14334 when this line ends in ZV. */
14335 && !row->ends_at_zv_p
14336 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14337 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14338 return row;
14339 ++row;
14340 }
14341 }
14342
14343
14344 /* Try to redisplay window W by reusing its existing display. W's
14345 current matrix must be up to date when this function is called,
14346 i.e. window_end_valid must not be nil.
14347
14348 Value is
14349
14350 1 if display has been updated
14351 0 if otherwise unsuccessful
14352 -1 if redisplay with same window start is known not to succeed
14353
14354 The following steps are performed:
14355
14356 1. Find the last row in the current matrix of W that is not
14357 affected by changes at the start of current_buffer. If no such row
14358 is found, give up.
14359
14360 2. Find the first row in W's current matrix that is not affected by
14361 changes at the end of current_buffer. Maybe there is no such row.
14362
14363 3. Display lines beginning with the row + 1 found in step 1 to the
14364 row found in step 2 or, if step 2 didn't find a row, to the end of
14365 the window.
14366
14367 4. If cursor is not known to appear on the window, give up.
14368
14369 5. If display stopped at the row found in step 2, scroll the
14370 display and current matrix as needed.
14371
14372 6. Maybe display some lines at the end of W, if we must. This can
14373 happen under various circumstances, like a partially visible line
14374 becoming fully visible, or because newly displayed lines are displayed
14375 in smaller font sizes.
14376
14377 7. Update W's window end information. */
14378
14379 static int
14380 try_window_id (w)
14381 struct window *w;
14382 {
14383 struct frame *f = XFRAME (w->frame);
14384 struct glyph_matrix *current_matrix = w->current_matrix;
14385 struct glyph_matrix *desired_matrix = w->desired_matrix;
14386 struct glyph_row *last_unchanged_at_beg_row;
14387 struct glyph_row *first_unchanged_at_end_row;
14388 struct glyph_row *row;
14389 struct glyph_row *bottom_row;
14390 int bottom_vpos;
14391 struct it it;
14392 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14393 struct text_pos start_pos;
14394 struct run run;
14395 int first_unchanged_at_end_vpos = 0;
14396 struct glyph_row *last_text_row, *last_text_row_at_end;
14397 struct text_pos start;
14398 int first_changed_charpos, last_changed_charpos;
14399
14400 #if GLYPH_DEBUG
14401 if (inhibit_try_window_id)
14402 return 0;
14403 #endif
14404
14405 /* This is handy for debugging. */
14406 #if 0
14407 #define GIVE_UP(X) \
14408 do { \
14409 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14410 return 0; \
14411 } while (0)
14412 #else
14413 #define GIVE_UP(X) return 0
14414 #endif
14415
14416 SET_TEXT_POS_FROM_MARKER (start, w->start);
14417
14418 /* Don't use this for mini-windows because these can show
14419 messages and mini-buffers, and we don't handle that here. */
14420 if (MINI_WINDOW_P (w))
14421 GIVE_UP (1);
14422
14423 /* This flag is used to prevent redisplay optimizations. */
14424 if (windows_or_buffers_changed || cursor_type_changed)
14425 GIVE_UP (2);
14426
14427 /* Verify that narrowing has not changed.
14428 Also verify that we were not told to prevent redisplay optimizations.
14429 It would be nice to further
14430 reduce the number of cases where this prevents try_window_id. */
14431 if (current_buffer->clip_changed
14432 || current_buffer->prevent_redisplay_optimizations_p)
14433 GIVE_UP (3);
14434
14435 /* Window must either use window-based redisplay or be full width. */
14436 if (!FRAME_WINDOW_P (f)
14437 && (!FRAME_LINE_INS_DEL_OK (f)
14438 || !WINDOW_FULL_WIDTH_P (w)))
14439 GIVE_UP (4);
14440
14441 /* Give up if point is not known NOT to appear in W. */
14442 if (PT < CHARPOS (start))
14443 GIVE_UP (5);
14444
14445 /* Another way to prevent redisplay optimizations. */
14446 if (XFASTINT (w->last_modified) == 0)
14447 GIVE_UP (6);
14448
14449 /* Verify that window is not hscrolled. */
14450 if (XFASTINT (w->hscroll) != 0)
14451 GIVE_UP (7);
14452
14453 /* Verify that display wasn't paused. */
14454 if (NILP (w->window_end_valid))
14455 GIVE_UP (8);
14456
14457 /* Can't use this if highlighting a region because a cursor movement
14458 will do more than just set the cursor. */
14459 if (!NILP (Vtransient_mark_mode)
14460 && !NILP (current_buffer->mark_active))
14461 GIVE_UP (9);
14462
14463 /* Likewise if highlighting trailing whitespace. */
14464 if (!NILP (Vshow_trailing_whitespace))
14465 GIVE_UP (11);
14466
14467 /* Likewise if showing a region. */
14468 if (!NILP (w->region_showing))
14469 GIVE_UP (10);
14470
14471 /* Can use this if overlay arrow position and or string have changed. */
14472 if (overlay_arrows_changed_p ())
14473 GIVE_UP (12);
14474
14475
14476 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14477 only if buffer has really changed. The reason is that the gap is
14478 initially at Z for freshly visited files. The code below would
14479 set end_unchanged to 0 in that case. */
14480 if (MODIFF > SAVE_MODIFF
14481 /* This seems to happen sometimes after saving a buffer. */
14482 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14483 {
14484 if (GPT - BEG < BEG_UNCHANGED)
14485 BEG_UNCHANGED = GPT - BEG;
14486 if (Z - GPT < END_UNCHANGED)
14487 END_UNCHANGED = Z - GPT;
14488 }
14489
14490 /* The position of the first and last character that has been changed. */
14491 first_changed_charpos = BEG + BEG_UNCHANGED;
14492 last_changed_charpos = Z - END_UNCHANGED;
14493
14494 /* If window starts after a line end, and the last change is in
14495 front of that newline, then changes don't affect the display.
14496 This case happens with stealth-fontification. Note that although
14497 the display is unchanged, glyph positions in the matrix have to
14498 be adjusted, of course. */
14499 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14500 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14501 && ((last_changed_charpos < CHARPOS (start)
14502 && CHARPOS (start) == BEGV)
14503 || (last_changed_charpos < CHARPOS (start) - 1
14504 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14505 {
14506 int Z_old, delta, Z_BYTE_old, delta_bytes;
14507 struct glyph_row *r0;
14508
14509 /* Compute how many chars/bytes have been added to or removed
14510 from the buffer. */
14511 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14512 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14513 delta = Z - Z_old;
14514 delta_bytes = Z_BYTE - Z_BYTE_old;
14515
14516 /* Give up if PT is not in the window. Note that it already has
14517 been checked at the start of try_window_id that PT is not in
14518 front of the window start. */
14519 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14520 GIVE_UP (13);
14521
14522 /* If window start is unchanged, we can reuse the whole matrix
14523 as is, after adjusting glyph positions. No need to compute
14524 the window end again, since its offset from Z hasn't changed. */
14525 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14526 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14527 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14528 /* PT must not be in a partially visible line. */
14529 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14530 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14531 {
14532 /* Adjust positions in the glyph matrix. */
14533 if (delta || delta_bytes)
14534 {
14535 struct glyph_row *r1
14536 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14537 increment_matrix_positions (w->current_matrix,
14538 MATRIX_ROW_VPOS (r0, current_matrix),
14539 MATRIX_ROW_VPOS (r1, current_matrix),
14540 delta, delta_bytes);
14541 }
14542
14543 /* Set the cursor. */
14544 row = row_containing_pos (w, PT, r0, NULL, 0);
14545 if (row)
14546 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14547 else
14548 abort ();
14549 return 1;
14550 }
14551 }
14552
14553 /* Handle the case that changes are all below what is displayed in
14554 the window, and that PT is in the window. This shortcut cannot
14555 be taken if ZV is visible in the window, and text has been added
14556 there that is visible in the window. */
14557 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14558 /* ZV is not visible in the window, or there are no
14559 changes at ZV, actually. */
14560 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14561 || first_changed_charpos == last_changed_charpos))
14562 {
14563 struct glyph_row *r0;
14564
14565 /* Give up if PT is not in the window. Note that it already has
14566 been checked at the start of try_window_id that PT is not in
14567 front of the window start. */
14568 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14569 GIVE_UP (14);
14570
14571 /* If window start is unchanged, we can reuse the whole matrix
14572 as is, without changing glyph positions since no text has
14573 been added/removed in front of the window end. */
14574 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14575 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14576 /* PT must not be in a partially visible line. */
14577 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14578 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14579 {
14580 /* We have to compute the window end anew since text
14581 can have been added/removed after it. */
14582 w->window_end_pos
14583 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14584 w->window_end_bytepos
14585 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14586
14587 /* Set the cursor. */
14588 row = row_containing_pos (w, PT, r0, NULL, 0);
14589 if (row)
14590 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14591 else
14592 abort ();
14593 return 2;
14594 }
14595 }
14596
14597 /* Give up if window start is in the changed area.
14598
14599 The condition used to read
14600
14601 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14602
14603 but why that was tested escapes me at the moment. */
14604 if (CHARPOS (start) >= first_changed_charpos
14605 && CHARPOS (start) <= last_changed_charpos)
14606 GIVE_UP (15);
14607
14608 /* Check that window start agrees with the start of the first glyph
14609 row in its current matrix. Check this after we know the window
14610 start is not in changed text, otherwise positions would not be
14611 comparable. */
14612 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14613 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14614 GIVE_UP (16);
14615
14616 /* Give up if the window ends in strings. Overlay strings
14617 at the end are difficult to handle, so don't try. */
14618 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14619 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14620 GIVE_UP (20);
14621
14622 /* Compute the position at which we have to start displaying new
14623 lines. Some of the lines at the top of the window might be
14624 reusable because they are not displaying changed text. Find the
14625 last row in W's current matrix not affected by changes at the
14626 start of current_buffer. Value is null if changes start in the
14627 first line of window. */
14628 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14629 if (last_unchanged_at_beg_row)
14630 {
14631 /* Avoid starting to display in the moddle of a character, a TAB
14632 for instance. This is easier than to set up the iterator
14633 exactly, and it's not a frequent case, so the additional
14634 effort wouldn't really pay off. */
14635 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14636 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14637 && last_unchanged_at_beg_row > w->current_matrix->rows)
14638 --last_unchanged_at_beg_row;
14639
14640 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14641 GIVE_UP (17);
14642
14643 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14644 GIVE_UP (18);
14645 start_pos = it.current.pos;
14646
14647 /* Start displaying new lines in the desired matrix at the same
14648 vpos we would use in the current matrix, i.e. below
14649 last_unchanged_at_beg_row. */
14650 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14651 current_matrix);
14652 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14653 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14654
14655 xassert (it.hpos == 0 && it.current_x == 0);
14656 }
14657 else
14658 {
14659 /* There are no reusable lines at the start of the window.
14660 Start displaying in the first text line. */
14661 start_display (&it, w, start);
14662 it.vpos = it.first_vpos;
14663 start_pos = it.current.pos;
14664 }
14665
14666 /* Find the first row that is not affected by changes at the end of
14667 the buffer. Value will be null if there is no unchanged row, in
14668 which case we must redisplay to the end of the window. delta
14669 will be set to the value by which buffer positions beginning with
14670 first_unchanged_at_end_row have to be adjusted due to text
14671 changes. */
14672 first_unchanged_at_end_row
14673 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14674 IF_DEBUG (debug_delta = delta);
14675 IF_DEBUG (debug_delta_bytes = delta_bytes);
14676
14677 /* Set stop_pos to the buffer position up to which we will have to
14678 display new lines. If first_unchanged_at_end_row != NULL, this
14679 is the buffer position of the start of the line displayed in that
14680 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14681 that we don't stop at a buffer position. */
14682 stop_pos = 0;
14683 if (first_unchanged_at_end_row)
14684 {
14685 xassert (last_unchanged_at_beg_row == NULL
14686 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14687
14688 /* If this is a continuation line, move forward to the next one
14689 that isn't. Changes in lines above affect this line.
14690 Caution: this may move first_unchanged_at_end_row to a row
14691 not displaying text. */
14692 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14693 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14694 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14695 < it.last_visible_y))
14696 ++first_unchanged_at_end_row;
14697
14698 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14699 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14700 >= it.last_visible_y))
14701 first_unchanged_at_end_row = NULL;
14702 else
14703 {
14704 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14705 + delta);
14706 first_unchanged_at_end_vpos
14707 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14708 xassert (stop_pos >= Z - END_UNCHANGED);
14709 }
14710 }
14711 else if (last_unchanged_at_beg_row == NULL)
14712 GIVE_UP (19);
14713
14714
14715 #if GLYPH_DEBUG
14716
14717 /* Either there is no unchanged row at the end, or the one we have
14718 now displays text. This is a necessary condition for the window
14719 end pos calculation at the end of this function. */
14720 xassert (first_unchanged_at_end_row == NULL
14721 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14722
14723 debug_last_unchanged_at_beg_vpos
14724 = (last_unchanged_at_beg_row
14725 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14726 : -1);
14727 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14728
14729 #endif /* GLYPH_DEBUG != 0 */
14730
14731
14732 /* Display new lines. Set last_text_row to the last new line
14733 displayed which has text on it, i.e. might end up as being the
14734 line where the window_end_vpos is. */
14735 w->cursor.vpos = -1;
14736 last_text_row = NULL;
14737 overlay_arrow_seen = 0;
14738 while (it.current_y < it.last_visible_y
14739 && !fonts_changed_p
14740 && (first_unchanged_at_end_row == NULL
14741 || IT_CHARPOS (it) < stop_pos))
14742 {
14743 if (display_line (&it))
14744 last_text_row = it.glyph_row - 1;
14745 }
14746
14747 if (fonts_changed_p)
14748 return -1;
14749
14750
14751 /* Compute differences in buffer positions, y-positions etc. for
14752 lines reused at the bottom of the window. Compute what we can
14753 scroll. */
14754 if (first_unchanged_at_end_row
14755 /* No lines reused because we displayed everything up to the
14756 bottom of the window. */
14757 && it.current_y < it.last_visible_y)
14758 {
14759 dvpos = (it.vpos
14760 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14761 current_matrix));
14762 dy = it.current_y - first_unchanged_at_end_row->y;
14763 run.current_y = first_unchanged_at_end_row->y;
14764 run.desired_y = run.current_y + dy;
14765 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14766 }
14767 else
14768 {
14769 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14770 first_unchanged_at_end_row = NULL;
14771 }
14772 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14773
14774
14775 /* Find the cursor if not already found. We have to decide whether
14776 PT will appear on this window (it sometimes doesn't, but this is
14777 not a very frequent case.) This decision has to be made before
14778 the current matrix is altered. A value of cursor.vpos < 0 means
14779 that PT is either in one of the lines beginning at
14780 first_unchanged_at_end_row or below the window. Don't care for
14781 lines that might be displayed later at the window end; as
14782 mentioned, this is not a frequent case. */
14783 if (w->cursor.vpos < 0)
14784 {
14785 /* Cursor in unchanged rows at the top? */
14786 if (PT < CHARPOS (start_pos)
14787 && last_unchanged_at_beg_row)
14788 {
14789 row = row_containing_pos (w, PT,
14790 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14791 last_unchanged_at_beg_row + 1, 0);
14792 if (row)
14793 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14794 }
14795
14796 /* Start from first_unchanged_at_end_row looking for PT. */
14797 else if (first_unchanged_at_end_row)
14798 {
14799 row = row_containing_pos (w, PT - delta,
14800 first_unchanged_at_end_row, NULL, 0);
14801 if (row)
14802 set_cursor_from_row (w, row, w->current_matrix, delta,
14803 delta_bytes, dy, dvpos);
14804 }
14805
14806 /* Give up if cursor was not found. */
14807 if (w->cursor.vpos < 0)
14808 {
14809 clear_glyph_matrix (w->desired_matrix);
14810 return -1;
14811 }
14812 }
14813
14814 /* Don't let the cursor end in the scroll margins. */
14815 {
14816 int this_scroll_margin, cursor_height;
14817
14818 this_scroll_margin = max (0, scroll_margin);
14819 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14820 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14821 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14822
14823 if ((w->cursor.y < this_scroll_margin
14824 && CHARPOS (start) > BEGV)
14825 /* Old redisplay didn't take scroll margin into account at the bottom,
14826 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14827 || (w->cursor.y + (make_cursor_line_fully_visible_p
14828 ? cursor_height + this_scroll_margin
14829 : 1)) > it.last_visible_y)
14830 {
14831 w->cursor.vpos = -1;
14832 clear_glyph_matrix (w->desired_matrix);
14833 return -1;
14834 }
14835 }
14836
14837 /* Scroll the display. Do it before changing the current matrix so
14838 that xterm.c doesn't get confused about where the cursor glyph is
14839 found. */
14840 if (dy && run.height)
14841 {
14842 update_begin (f);
14843
14844 if (FRAME_WINDOW_P (f))
14845 {
14846 FRAME_RIF (f)->update_window_begin_hook (w);
14847 FRAME_RIF (f)->clear_window_mouse_face (w);
14848 FRAME_RIF (f)->scroll_run_hook (w, &run);
14849 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14850 }
14851 else
14852 {
14853 /* Terminal frame. In this case, dvpos gives the number of
14854 lines to scroll by; dvpos < 0 means scroll up. */
14855 int first_unchanged_at_end_vpos
14856 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14857 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14858 int end = (WINDOW_TOP_EDGE_LINE (w)
14859 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14860 + window_internal_height (w));
14861
14862 /* Perform the operation on the screen. */
14863 if (dvpos > 0)
14864 {
14865 /* Scroll last_unchanged_at_beg_row to the end of the
14866 window down dvpos lines. */
14867 set_terminal_window (f, end);
14868
14869 /* On dumb terminals delete dvpos lines at the end
14870 before inserting dvpos empty lines. */
14871 if (!FRAME_SCROLL_REGION_OK (f))
14872 ins_del_lines (f, end - dvpos, -dvpos);
14873
14874 /* Insert dvpos empty lines in front of
14875 last_unchanged_at_beg_row. */
14876 ins_del_lines (f, from, dvpos);
14877 }
14878 else if (dvpos < 0)
14879 {
14880 /* Scroll up last_unchanged_at_beg_vpos to the end of
14881 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14882 set_terminal_window (f, end);
14883
14884 /* Delete dvpos lines in front of
14885 last_unchanged_at_beg_vpos. ins_del_lines will set
14886 the cursor to the given vpos and emit |dvpos| delete
14887 line sequences. */
14888 ins_del_lines (f, from + dvpos, dvpos);
14889
14890 /* On a dumb terminal insert dvpos empty lines at the
14891 end. */
14892 if (!FRAME_SCROLL_REGION_OK (f))
14893 ins_del_lines (f, end + dvpos, -dvpos);
14894 }
14895
14896 set_terminal_window (f, 0);
14897 }
14898
14899 update_end (f);
14900 }
14901
14902 /* Shift reused rows of the current matrix to the right position.
14903 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14904 text. */
14905 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14906 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14907 if (dvpos < 0)
14908 {
14909 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14910 bottom_vpos, dvpos);
14911 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14912 bottom_vpos, 0);
14913 }
14914 else if (dvpos > 0)
14915 {
14916 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14917 bottom_vpos, dvpos);
14918 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14919 first_unchanged_at_end_vpos + dvpos, 0);
14920 }
14921
14922 /* For frame-based redisplay, make sure that current frame and window
14923 matrix are in sync with respect to glyph memory. */
14924 if (!FRAME_WINDOW_P (f))
14925 sync_frame_with_window_matrix_rows (w);
14926
14927 /* Adjust buffer positions in reused rows. */
14928 if (delta || delta_bytes)
14929 increment_matrix_positions (current_matrix,
14930 first_unchanged_at_end_vpos + dvpos,
14931 bottom_vpos, delta, delta_bytes);
14932
14933 /* Adjust Y positions. */
14934 if (dy)
14935 shift_glyph_matrix (w, current_matrix,
14936 first_unchanged_at_end_vpos + dvpos,
14937 bottom_vpos, dy);
14938
14939 if (first_unchanged_at_end_row)
14940 {
14941 first_unchanged_at_end_row += dvpos;
14942 if (first_unchanged_at_end_row->y >= it.last_visible_y
14943 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14944 first_unchanged_at_end_row = NULL;
14945 }
14946
14947 /* If scrolling up, there may be some lines to display at the end of
14948 the window. */
14949 last_text_row_at_end = NULL;
14950 if (dy < 0)
14951 {
14952 /* Scrolling up can leave for example a partially visible line
14953 at the end of the window to be redisplayed. */
14954 /* Set last_row to the glyph row in the current matrix where the
14955 window end line is found. It has been moved up or down in
14956 the matrix by dvpos. */
14957 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14958 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14959
14960 /* If last_row is the window end line, it should display text. */
14961 xassert (last_row->displays_text_p);
14962
14963 /* If window end line was partially visible before, begin
14964 displaying at that line. Otherwise begin displaying with the
14965 line following it. */
14966 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14967 {
14968 init_to_row_start (&it, w, last_row);
14969 it.vpos = last_vpos;
14970 it.current_y = last_row->y;
14971 }
14972 else
14973 {
14974 init_to_row_end (&it, w, last_row);
14975 it.vpos = 1 + last_vpos;
14976 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14977 ++last_row;
14978 }
14979
14980 /* We may start in a continuation line. If so, we have to
14981 get the right continuation_lines_width and current_x. */
14982 it.continuation_lines_width = last_row->continuation_lines_width;
14983 it.hpos = it.current_x = 0;
14984
14985 /* Display the rest of the lines at the window end. */
14986 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14987 while (it.current_y < it.last_visible_y
14988 && !fonts_changed_p)
14989 {
14990 /* Is it always sure that the display agrees with lines in
14991 the current matrix? I don't think so, so we mark rows
14992 displayed invalid in the current matrix by setting their
14993 enabled_p flag to zero. */
14994 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14995 if (display_line (&it))
14996 last_text_row_at_end = it.glyph_row - 1;
14997 }
14998 }
14999
15000 /* Update window_end_pos and window_end_vpos. */
15001 if (first_unchanged_at_end_row
15002 && !last_text_row_at_end)
15003 {
15004 /* Window end line if one of the preserved rows from the current
15005 matrix. Set row to the last row displaying text in current
15006 matrix starting at first_unchanged_at_end_row, after
15007 scrolling. */
15008 xassert (first_unchanged_at_end_row->displays_text_p);
15009 row = find_last_row_displaying_text (w->current_matrix, &it,
15010 first_unchanged_at_end_row);
15011 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15012
15013 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15014 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15015 w->window_end_vpos
15016 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15017 xassert (w->window_end_bytepos >= 0);
15018 IF_DEBUG (debug_method_add (w, "A"));
15019 }
15020 else if (last_text_row_at_end)
15021 {
15022 w->window_end_pos
15023 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15024 w->window_end_bytepos
15025 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15026 w->window_end_vpos
15027 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15028 xassert (w->window_end_bytepos >= 0);
15029 IF_DEBUG (debug_method_add (w, "B"));
15030 }
15031 else if (last_text_row)
15032 {
15033 /* We have displayed either to the end of the window or at the
15034 end of the window, i.e. the last row with text is to be found
15035 in the desired matrix. */
15036 w->window_end_pos
15037 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15038 w->window_end_bytepos
15039 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15040 w->window_end_vpos
15041 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15042 xassert (w->window_end_bytepos >= 0);
15043 }
15044 else if (first_unchanged_at_end_row == NULL
15045 && last_text_row == NULL
15046 && last_text_row_at_end == NULL)
15047 {
15048 /* Displayed to end of window, but no line containing text was
15049 displayed. Lines were deleted at the end of the window. */
15050 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15051 int vpos = XFASTINT (w->window_end_vpos);
15052 struct glyph_row *current_row = current_matrix->rows + vpos;
15053 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15054
15055 for (row = NULL;
15056 row == NULL && vpos >= first_vpos;
15057 --vpos, --current_row, --desired_row)
15058 {
15059 if (desired_row->enabled_p)
15060 {
15061 if (desired_row->displays_text_p)
15062 row = desired_row;
15063 }
15064 else if (current_row->displays_text_p)
15065 row = current_row;
15066 }
15067
15068 xassert (row != NULL);
15069 w->window_end_vpos = make_number (vpos + 1);
15070 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15071 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15072 xassert (w->window_end_bytepos >= 0);
15073 IF_DEBUG (debug_method_add (w, "C"));
15074 }
15075 else
15076 abort ();
15077
15078 #if 0 /* This leads to problems, for instance when the cursor is
15079 at ZV, and the cursor line displays no text. */
15080 /* Disable rows below what's displayed in the window. This makes
15081 debugging easier. */
15082 enable_glyph_matrix_rows (current_matrix,
15083 XFASTINT (w->window_end_vpos) + 1,
15084 bottom_vpos, 0);
15085 #endif
15086
15087 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15088 debug_end_vpos = XFASTINT (w->window_end_vpos));
15089
15090 /* Record that display has not been completed. */
15091 w->window_end_valid = Qnil;
15092 w->desired_matrix->no_scrolling_p = 1;
15093 return 3;
15094
15095 #undef GIVE_UP
15096 }
15097
15098
15099 \f
15100 /***********************************************************************
15101 More debugging support
15102 ***********************************************************************/
15103
15104 #if GLYPH_DEBUG
15105
15106 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15107 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15108 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15109
15110
15111 /* Dump the contents of glyph matrix MATRIX on stderr.
15112
15113 GLYPHS 0 means don't show glyph contents.
15114 GLYPHS 1 means show glyphs in short form
15115 GLYPHS > 1 means show glyphs in long form. */
15116
15117 void
15118 dump_glyph_matrix (matrix, glyphs)
15119 struct glyph_matrix *matrix;
15120 int glyphs;
15121 {
15122 int i;
15123 for (i = 0; i < matrix->nrows; ++i)
15124 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15125 }
15126
15127
15128 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15129 the glyph row and area where the glyph comes from. */
15130
15131 void
15132 dump_glyph (row, glyph, area)
15133 struct glyph_row *row;
15134 struct glyph *glyph;
15135 int area;
15136 {
15137 if (glyph->type == CHAR_GLYPH)
15138 {
15139 fprintf (stderr,
15140 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15141 glyph - row->glyphs[TEXT_AREA],
15142 'C',
15143 glyph->charpos,
15144 (BUFFERP (glyph->object)
15145 ? 'B'
15146 : (STRINGP (glyph->object)
15147 ? 'S'
15148 : '-')),
15149 glyph->pixel_width,
15150 glyph->u.ch,
15151 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15152 ? glyph->u.ch
15153 : '.'),
15154 glyph->face_id,
15155 glyph->left_box_line_p,
15156 glyph->right_box_line_p);
15157 }
15158 else if (glyph->type == STRETCH_GLYPH)
15159 {
15160 fprintf (stderr,
15161 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15162 glyph - row->glyphs[TEXT_AREA],
15163 'S',
15164 glyph->charpos,
15165 (BUFFERP (glyph->object)
15166 ? 'B'
15167 : (STRINGP (glyph->object)
15168 ? 'S'
15169 : '-')),
15170 glyph->pixel_width,
15171 0,
15172 '.',
15173 glyph->face_id,
15174 glyph->left_box_line_p,
15175 glyph->right_box_line_p);
15176 }
15177 else if (glyph->type == IMAGE_GLYPH)
15178 {
15179 fprintf (stderr,
15180 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15181 glyph - row->glyphs[TEXT_AREA],
15182 'I',
15183 glyph->charpos,
15184 (BUFFERP (glyph->object)
15185 ? 'B'
15186 : (STRINGP (glyph->object)
15187 ? 'S'
15188 : '-')),
15189 glyph->pixel_width,
15190 glyph->u.img_id,
15191 '.',
15192 glyph->face_id,
15193 glyph->left_box_line_p,
15194 glyph->right_box_line_p);
15195 }
15196 else if (glyph->type == COMPOSITE_GLYPH)
15197 {
15198 fprintf (stderr,
15199 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15200 glyph - row->glyphs[TEXT_AREA],
15201 '+',
15202 glyph->charpos,
15203 (BUFFERP (glyph->object)
15204 ? 'B'
15205 : (STRINGP (glyph->object)
15206 ? 'S'
15207 : '-')),
15208 glyph->pixel_width,
15209 glyph->u.cmp_id,
15210 '.',
15211 glyph->face_id,
15212 glyph->left_box_line_p,
15213 glyph->right_box_line_p);
15214 }
15215 }
15216
15217
15218 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15219 GLYPHS 0 means don't show glyph contents.
15220 GLYPHS 1 means show glyphs in short form
15221 GLYPHS > 1 means show glyphs in long form. */
15222
15223 void
15224 dump_glyph_row (row, vpos, glyphs)
15225 struct glyph_row *row;
15226 int vpos, glyphs;
15227 {
15228 if (glyphs != 1)
15229 {
15230 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15231 fprintf (stderr, "======================================================================\n");
15232
15233 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15234 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15235 vpos,
15236 MATRIX_ROW_START_CHARPOS (row),
15237 MATRIX_ROW_END_CHARPOS (row),
15238 row->used[TEXT_AREA],
15239 row->contains_overlapping_glyphs_p,
15240 row->enabled_p,
15241 row->truncated_on_left_p,
15242 row->truncated_on_right_p,
15243 row->continued_p,
15244 MATRIX_ROW_CONTINUATION_LINE_P (row),
15245 row->displays_text_p,
15246 row->ends_at_zv_p,
15247 row->fill_line_p,
15248 row->ends_in_middle_of_char_p,
15249 row->starts_in_middle_of_char_p,
15250 row->mouse_face_p,
15251 row->x,
15252 row->y,
15253 row->pixel_width,
15254 row->height,
15255 row->visible_height,
15256 row->ascent,
15257 row->phys_ascent);
15258 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15259 row->end.overlay_string_index,
15260 row->continuation_lines_width);
15261 fprintf (stderr, "%9d %5d\n",
15262 CHARPOS (row->start.string_pos),
15263 CHARPOS (row->end.string_pos));
15264 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15265 row->end.dpvec_index);
15266 }
15267
15268 if (glyphs > 1)
15269 {
15270 int area;
15271
15272 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15273 {
15274 struct glyph *glyph = row->glyphs[area];
15275 struct glyph *glyph_end = glyph + row->used[area];
15276
15277 /* Glyph for a line end in text. */
15278 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15279 ++glyph_end;
15280
15281 if (glyph < glyph_end)
15282 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15283
15284 for (; glyph < glyph_end; ++glyph)
15285 dump_glyph (row, glyph, area);
15286 }
15287 }
15288 else if (glyphs == 1)
15289 {
15290 int area;
15291
15292 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15293 {
15294 char *s = (char *) alloca (row->used[area] + 1);
15295 int i;
15296
15297 for (i = 0; i < row->used[area]; ++i)
15298 {
15299 struct glyph *glyph = row->glyphs[area] + i;
15300 if (glyph->type == CHAR_GLYPH
15301 && glyph->u.ch < 0x80
15302 && glyph->u.ch >= ' ')
15303 s[i] = glyph->u.ch;
15304 else
15305 s[i] = '.';
15306 }
15307
15308 s[i] = '\0';
15309 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15310 }
15311 }
15312 }
15313
15314
15315 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15316 Sdump_glyph_matrix, 0, 1, "p",
15317 doc: /* Dump the current matrix of the selected window to stderr.
15318 Shows contents of glyph row structures. With non-nil
15319 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15320 glyphs in short form, otherwise show glyphs in long form. */)
15321 (glyphs)
15322 Lisp_Object glyphs;
15323 {
15324 struct window *w = XWINDOW (selected_window);
15325 struct buffer *buffer = XBUFFER (w->buffer);
15326
15327 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15328 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15329 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15330 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15331 fprintf (stderr, "=============================================\n");
15332 dump_glyph_matrix (w->current_matrix,
15333 NILP (glyphs) ? 0 : XINT (glyphs));
15334 return Qnil;
15335 }
15336
15337
15338 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15339 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15340 ()
15341 {
15342 struct frame *f = XFRAME (selected_frame);
15343 dump_glyph_matrix (f->current_matrix, 1);
15344 return Qnil;
15345 }
15346
15347
15348 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15349 doc: /* Dump glyph row ROW to stderr.
15350 GLYPH 0 means don't dump glyphs.
15351 GLYPH 1 means dump glyphs in short form.
15352 GLYPH > 1 or omitted means dump glyphs in long form. */)
15353 (row, glyphs)
15354 Lisp_Object row, glyphs;
15355 {
15356 struct glyph_matrix *matrix;
15357 int vpos;
15358
15359 CHECK_NUMBER (row);
15360 matrix = XWINDOW (selected_window)->current_matrix;
15361 vpos = XINT (row);
15362 if (vpos >= 0 && vpos < matrix->nrows)
15363 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15364 vpos,
15365 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15366 return Qnil;
15367 }
15368
15369
15370 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15371 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15372 GLYPH 0 means don't dump glyphs.
15373 GLYPH 1 means dump glyphs in short form.
15374 GLYPH > 1 or omitted means dump glyphs in long form. */)
15375 (row, glyphs)
15376 Lisp_Object row, glyphs;
15377 {
15378 struct frame *sf = SELECTED_FRAME ();
15379 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15380 int vpos;
15381
15382 CHECK_NUMBER (row);
15383 vpos = XINT (row);
15384 if (vpos >= 0 && vpos < m->nrows)
15385 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15386 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15387 return Qnil;
15388 }
15389
15390
15391 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15392 doc: /* Toggle tracing of redisplay.
15393 With ARG, turn tracing on if and only if ARG is positive. */)
15394 (arg)
15395 Lisp_Object arg;
15396 {
15397 if (NILP (arg))
15398 trace_redisplay_p = !trace_redisplay_p;
15399 else
15400 {
15401 arg = Fprefix_numeric_value (arg);
15402 trace_redisplay_p = XINT (arg) > 0;
15403 }
15404
15405 return Qnil;
15406 }
15407
15408
15409 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15410 doc: /* Like `format', but print result to stderr.
15411 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15412 (nargs, args)
15413 int nargs;
15414 Lisp_Object *args;
15415 {
15416 Lisp_Object s = Fformat (nargs, args);
15417 fprintf (stderr, "%s", SDATA (s));
15418 return Qnil;
15419 }
15420
15421 #endif /* GLYPH_DEBUG */
15422
15423
15424 \f
15425 /***********************************************************************
15426 Building Desired Matrix Rows
15427 ***********************************************************************/
15428
15429 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15430 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15431
15432 static struct glyph_row *
15433 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15434 struct window *w;
15435 Lisp_Object overlay_arrow_string;
15436 {
15437 struct frame *f = XFRAME (WINDOW_FRAME (w));
15438 struct buffer *buffer = XBUFFER (w->buffer);
15439 struct buffer *old = current_buffer;
15440 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15441 int arrow_len = SCHARS (overlay_arrow_string);
15442 const unsigned char *arrow_end = arrow_string + arrow_len;
15443 const unsigned char *p;
15444 struct it it;
15445 int multibyte_p;
15446 int n_glyphs_before;
15447
15448 set_buffer_temp (buffer);
15449 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15450 it.glyph_row->used[TEXT_AREA] = 0;
15451 SET_TEXT_POS (it.position, 0, 0);
15452
15453 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15454 p = arrow_string;
15455 while (p < arrow_end)
15456 {
15457 Lisp_Object face, ilisp;
15458
15459 /* Get the next character. */
15460 if (multibyte_p)
15461 it.c = string_char_and_length (p, arrow_len, &it.len);
15462 else
15463 it.c = *p, it.len = 1;
15464 p += it.len;
15465
15466 /* Get its face. */
15467 ilisp = make_number (p - arrow_string);
15468 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15469 it.face_id = compute_char_face (f, it.c, face);
15470
15471 /* Compute its width, get its glyphs. */
15472 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15473 SET_TEXT_POS (it.position, -1, -1);
15474 PRODUCE_GLYPHS (&it);
15475
15476 /* If this character doesn't fit any more in the line, we have
15477 to remove some glyphs. */
15478 if (it.current_x > it.last_visible_x)
15479 {
15480 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15481 break;
15482 }
15483 }
15484
15485 set_buffer_temp (old);
15486 return it.glyph_row;
15487 }
15488
15489
15490 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15491 glyphs are only inserted for terminal frames since we can't really
15492 win with truncation glyphs when partially visible glyphs are
15493 involved. Which glyphs to insert is determined by
15494 produce_special_glyphs. */
15495
15496 static void
15497 insert_left_trunc_glyphs (it)
15498 struct it *it;
15499 {
15500 struct it truncate_it;
15501 struct glyph *from, *end, *to, *toend;
15502
15503 xassert (!FRAME_WINDOW_P (it->f));
15504
15505 /* Get the truncation glyphs. */
15506 truncate_it = *it;
15507 truncate_it.current_x = 0;
15508 truncate_it.face_id = DEFAULT_FACE_ID;
15509 truncate_it.glyph_row = &scratch_glyph_row;
15510 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15511 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15512 truncate_it.object = make_number (0);
15513 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15514
15515 /* Overwrite glyphs from IT with truncation glyphs. */
15516 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15517 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15518 to = it->glyph_row->glyphs[TEXT_AREA];
15519 toend = to + it->glyph_row->used[TEXT_AREA];
15520
15521 while (from < end)
15522 *to++ = *from++;
15523
15524 /* There may be padding glyphs left over. Overwrite them too. */
15525 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15526 {
15527 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15528 while (from < end)
15529 *to++ = *from++;
15530 }
15531
15532 if (to > toend)
15533 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15534 }
15535
15536
15537 /* Compute the pixel height and width of IT->glyph_row.
15538
15539 Most of the time, ascent and height of a display line will be equal
15540 to the max_ascent and max_height values of the display iterator
15541 structure. This is not the case if
15542
15543 1. We hit ZV without displaying anything. In this case, max_ascent
15544 and max_height will be zero.
15545
15546 2. We have some glyphs that don't contribute to the line height.
15547 (The glyph row flag contributes_to_line_height_p is for future
15548 pixmap extensions).
15549
15550 The first case is easily covered by using default values because in
15551 these cases, the line height does not really matter, except that it
15552 must not be zero. */
15553
15554 static void
15555 compute_line_metrics (it)
15556 struct it *it;
15557 {
15558 struct glyph_row *row = it->glyph_row;
15559 int area, i;
15560
15561 if (FRAME_WINDOW_P (it->f))
15562 {
15563 int i, min_y, max_y;
15564
15565 /* The line may consist of one space only, that was added to
15566 place the cursor on it. If so, the row's height hasn't been
15567 computed yet. */
15568 if (row->height == 0)
15569 {
15570 if (it->max_ascent + it->max_descent == 0)
15571 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15572 row->ascent = it->max_ascent;
15573 row->height = it->max_ascent + it->max_descent;
15574 row->phys_ascent = it->max_phys_ascent;
15575 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15576 row->extra_line_spacing = it->max_extra_line_spacing;
15577 }
15578
15579 /* Compute the width of this line. */
15580 row->pixel_width = row->x;
15581 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15582 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15583
15584 xassert (row->pixel_width >= 0);
15585 xassert (row->ascent >= 0 && row->height > 0);
15586
15587 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15588 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15589
15590 /* If first line's physical ascent is larger than its logical
15591 ascent, use the physical ascent, and make the row taller.
15592 This makes accented characters fully visible. */
15593 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15594 && row->phys_ascent > row->ascent)
15595 {
15596 row->height += row->phys_ascent - row->ascent;
15597 row->ascent = row->phys_ascent;
15598 }
15599
15600 /* Compute how much of the line is visible. */
15601 row->visible_height = row->height;
15602
15603 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15604 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15605
15606 if (row->y < min_y)
15607 row->visible_height -= min_y - row->y;
15608 if (row->y + row->height > max_y)
15609 row->visible_height -= row->y + row->height - max_y;
15610 }
15611 else
15612 {
15613 row->pixel_width = row->used[TEXT_AREA];
15614 if (row->continued_p)
15615 row->pixel_width -= it->continuation_pixel_width;
15616 else if (row->truncated_on_right_p)
15617 row->pixel_width -= it->truncation_pixel_width;
15618 row->ascent = row->phys_ascent = 0;
15619 row->height = row->phys_height = row->visible_height = 1;
15620 row->extra_line_spacing = 0;
15621 }
15622
15623 /* Compute a hash code for this row. */
15624 row->hash = 0;
15625 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15626 for (i = 0; i < row->used[area]; ++i)
15627 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15628 + row->glyphs[area][i].u.val
15629 + row->glyphs[area][i].face_id
15630 + row->glyphs[area][i].padding_p
15631 + (row->glyphs[area][i].type << 2));
15632
15633 it->max_ascent = it->max_descent = 0;
15634 it->max_phys_ascent = it->max_phys_descent = 0;
15635 }
15636
15637
15638 /* Append one space to the glyph row of iterator IT if doing a
15639 window-based redisplay. The space has the same face as
15640 IT->face_id. Value is non-zero if a space was added.
15641
15642 This function is called to make sure that there is always one glyph
15643 at the end of a glyph row that the cursor can be set on under
15644 window-systems. (If there weren't such a glyph we would not know
15645 how wide and tall a box cursor should be displayed).
15646
15647 At the same time this space let's a nicely handle clearing to the
15648 end of the line if the row ends in italic text. */
15649
15650 static int
15651 append_space_for_newline (it, default_face_p)
15652 struct it *it;
15653 int default_face_p;
15654 {
15655 if (FRAME_WINDOW_P (it->f))
15656 {
15657 int n = it->glyph_row->used[TEXT_AREA];
15658
15659 if (it->glyph_row->glyphs[TEXT_AREA] + n
15660 < it->glyph_row->glyphs[1 + TEXT_AREA])
15661 {
15662 /* Save some values that must not be changed.
15663 Must save IT->c and IT->len because otherwise
15664 ITERATOR_AT_END_P wouldn't work anymore after
15665 append_space_for_newline has been called. */
15666 enum display_element_type saved_what = it->what;
15667 int saved_c = it->c, saved_len = it->len;
15668 int saved_x = it->current_x;
15669 int saved_face_id = it->face_id;
15670 struct text_pos saved_pos;
15671 Lisp_Object saved_object;
15672 struct face *face;
15673
15674 saved_object = it->object;
15675 saved_pos = it->position;
15676
15677 it->what = IT_CHARACTER;
15678 bzero (&it->position, sizeof it->position);
15679 it->object = make_number (0);
15680 it->c = ' ';
15681 it->len = 1;
15682
15683 if (default_face_p)
15684 it->face_id = DEFAULT_FACE_ID;
15685 else if (it->face_before_selective_p)
15686 it->face_id = it->saved_face_id;
15687 face = FACE_FROM_ID (it->f, it->face_id);
15688 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15689
15690 PRODUCE_GLYPHS (it);
15691
15692 it->override_ascent = -1;
15693 it->constrain_row_ascent_descent_p = 0;
15694 it->current_x = saved_x;
15695 it->object = saved_object;
15696 it->position = saved_pos;
15697 it->what = saved_what;
15698 it->face_id = saved_face_id;
15699 it->len = saved_len;
15700 it->c = saved_c;
15701 return 1;
15702 }
15703 }
15704
15705 return 0;
15706 }
15707
15708
15709 /* Extend the face of the last glyph in the text area of IT->glyph_row
15710 to the end of the display line. Called from display_line.
15711 If the glyph row is empty, add a space glyph to it so that we
15712 know the face to draw. Set the glyph row flag fill_line_p. */
15713
15714 static void
15715 extend_face_to_end_of_line (it)
15716 struct it *it;
15717 {
15718 struct face *face;
15719 struct frame *f = it->f;
15720
15721 /* If line is already filled, do nothing. */
15722 if (it->current_x >= it->last_visible_x)
15723 return;
15724
15725 /* Face extension extends the background and box of IT->face_id
15726 to the end of the line. If the background equals the background
15727 of the frame, we don't have to do anything. */
15728 if (it->face_before_selective_p)
15729 face = FACE_FROM_ID (it->f, it->saved_face_id);
15730 else
15731 face = FACE_FROM_ID (f, it->face_id);
15732
15733 if (FRAME_WINDOW_P (f)
15734 && it->glyph_row->displays_text_p
15735 && face->box == FACE_NO_BOX
15736 && face->background == FRAME_BACKGROUND_PIXEL (f)
15737 && !face->stipple)
15738 return;
15739
15740 /* Set the glyph row flag indicating that the face of the last glyph
15741 in the text area has to be drawn to the end of the text area. */
15742 it->glyph_row->fill_line_p = 1;
15743
15744 /* If current character of IT is not ASCII, make sure we have the
15745 ASCII face. This will be automatically undone the next time
15746 get_next_display_element returns a multibyte character. Note
15747 that the character will always be single byte in unibyte text. */
15748 if (!SINGLE_BYTE_CHAR_P (it->c))
15749 {
15750 it->face_id = FACE_FOR_CHAR (f, face, 0);
15751 }
15752
15753 if (FRAME_WINDOW_P (f))
15754 {
15755 /* If the row is empty, add a space with the current face of IT,
15756 so that we know which face to draw. */
15757 if (it->glyph_row->used[TEXT_AREA] == 0)
15758 {
15759 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15760 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15761 it->glyph_row->used[TEXT_AREA] = 1;
15762 }
15763 }
15764 else
15765 {
15766 /* Save some values that must not be changed. */
15767 int saved_x = it->current_x;
15768 struct text_pos saved_pos;
15769 Lisp_Object saved_object;
15770 enum display_element_type saved_what = it->what;
15771 int saved_face_id = it->face_id;
15772
15773 saved_object = it->object;
15774 saved_pos = it->position;
15775
15776 it->what = IT_CHARACTER;
15777 bzero (&it->position, sizeof it->position);
15778 it->object = make_number (0);
15779 it->c = ' ';
15780 it->len = 1;
15781 it->face_id = face->id;
15782
15783 PRODUCE_GLYPHS (it);
15784
15785 while (it->current_x <= it->last_visible_x)
15786 PRODUCE_GLYPHS (it);
15787
15788 /* Don't count these blanks really. It would let us insert a left
15789 truncation glyph below and make us set the cursor on them, maybe. */
15790 it->current_x = saved_x;
15791 it->object = saved_object;
15792 it->position = saved_pos;
15793 it->what = saved_what;
15794 it->face_id = saved_face_id;
15795 }
15796 }
15797
15798
15799 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15800 trailing whitespace. */
15801
15802 static int
15803 trailing_whitespace_p (charpos)
15804 int charpos;
15805 {
15806 int bytepos = CHAR_TO_BYTE (charpos);
15807 int c = 0;
15808
15809 while (bytepos < ZV_BYTE
15810 && (c = FETCH_CHAR (bytepos),
15811 c == ' ' || c == '\t'))
15812 ++bytepos;
15813
15814 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15815 {
15816 if (bytepos != PT_BYTE)
15817 return 1;
15818 }
15819 return 0;
15820 }
15821
15822
15823 /* Highlight trailing whitespace, if any, in ROW. */
15824
15825 void
15826 highlight_trailing_whitespace (f, row)
15827 struct frame *f;
15828 struct glyph_row *row;
15829 {
15830 int used = row->used[TEXT_AREA];
15831
15832 if (used)
15833 {
15834 struct glyph *start = row->glyphs[TEXT_AREA];
15835 struct glyph *glyph = start + used - 1;
15836
15837 /* Skip over glyphs inserted to display the cursor at the
15838 end of a line, for extending the face of the last glyph
15839 to the end of the line on terminals, and for truncation
15840 and continuation glyphs. */
15841 while (glyph >= start
15842 && glyph->type == CHAR_GLYPH
15843 && INTEGERP (glyph->object))
15844 --glyph;
15845
15846 /* If last glyph is a space or stretch, and it's trailing
15847 whitespace, set the face of all trailing whitespace glyphs in
15848 IT->glyph_row to `trailing-whitespace'. */
15849 if (glyph >= start
15850 && BUFFERP (glyph->object)
15851 && (glyph->type == STRETCH_GLYPH
15852 || (glyph->type == CHAR_GLYPH
15853 && glyph->u.ch == ' '))
15854 && trailing_whitespace_p (glyph->charpos))
15855 {
15856 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15857 if (face_id < 0)
15858 return;
15859
15860 while (glyph >= start
15861 && BUFFERP (glyph->object)
15862 && (glyph->type == STRETCH_GLYPH
15863 || (glyph->type == CHAR_GLYPH
15864 && glyph->u.ch == ' ')))
15865 (glyph--)->face_id = face_id;
15866 }
15867 }
15868 }
15869
15870
15871 /* Value is non-zero if glyph row ROW in window W should be
15872 used to hold the cursor. */
15873
15874 static int
15875 cursor_row_p (w, row)
15876 struct window *w;
15877 struct glyph_row *row;
15878 {
15879 int cursor_row_p = 1;
15880
15881 if (PT == MATRIX_ROW_END_CHARPOS (row))
15882 {
15883 /* Suppose the row ends on a string.
15884 Unless the row is continued, that means it ends on a newline
15885 in the string. If it's anything other than a display string
15886 (e.g. a before-string from an overlay), we don't want the
15887 cursor there. (This heuristic seems to give the optimal
15888 behavior for the various types of multi-line strings.) */
15889 if (CHARPOS (row->end.string_pos) >= 0)
15890 {
15891 if (row->continued_p)
15892 cursor_row_p = 1;
15893 else
15894 {
15895 /* Check for `display' property. */
15896 struct glyph *beg = row->glyphs[TEXT_AREA];
15897 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15898 struct glyph *glyph;
15899
15900 cursor_row_p = 0;
15901 for (glyph = end; glyph >= beg; --glyph)
15902 if (STRINGP (glyph->object))
15903 {
15904 Lisp_Object prop
15905 = Fget_char_property (make_number (PT),
15906 Qdisplay, Qnil);
15907 cursor_row_p =
15908 (!NILP (prop)
15909 && display_prop_string_p (prop, glyph->object));
15910 break;
15911 }
15912 }
15913 }
15914 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15915 {
15916 /* If the row ends in middle of a real character,
15917 and the line is continued, we want the cursor here.
15918 That's because MATRIX_ROW_END_CHARPOS would equal
15919 PT if PT is before the character. */
15920 if (!row->ends_in_ellipsis_p)
15921 cursor_row_p = row->continued_p;
15922 else
15923 /* If the row ends in an ellipsis, then
15924 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15925 We want that position to be displayed after the ellipsis. */
15926 cursor_row_p = 0;
15927 }
15928 /* If the row ends at ZV, display the cursor at the end of that
15929 row instead of at the start of the row below. */
15930 else if (row->ends_at_zv_p)
15931 cursor_row_p = 1;
15932 else
15933 cursor_row_p = 0;
15934 }
15935
15936 return cursor_row_p;
15937 }
15938
15939
15940 /* Construct the glyph row IT->glyph_row in the desired matrix of
15941 IT->w from text at the current position of IT. See dispextern.h
15942 for an overview of struct it. Value is non-zero if
15943 IT->glyph_row displays text, as opposed to a line displaying ZV
15944 only. */
15945
15946 static int
15947 display_line (it)
15948 struct it *it;
15949 {
15950 struct glyph_row *row = it->glyph_row;
15951 Lisp_Object overlay_arrow_string;
15952
15953 /* We always start displaying at hpos zero even if hscrolled. */
15954 xassert (it->hpos == 0 && it->current_x == 0);
15955
15956 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15957 >= it->w->desired_matrix->nrows)
15958 {
15959 it->w->nrows_scale_factor++;
15960 fonts_changed_p = 1;
15961 return 0;
15962 }
15963
15964 /* Is IT->w showing the region? */
15965 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15966
15967 /* Clear the result glyph row and enable it. */
15968 prepare_desired_row (row);
15969
15970 row->y = it->current_y;
15971 row->start = it->start;
15972 row->continuation_lines_width = it->continuation_lines_width;
15973 row->displays_text_p = 1;
15974 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15975 it->starts_in_middle_of_char_p = 0;
15976
15977 /* Arrange the overlays nicely for our purposes. Usually, we call
15978 display_line on only one line at a time, in which case this
15979 can't really hurt too much, or we call it on lines which appear
15980 one after another in the buffer, in which case all calls to
15981 recenter_overlay_lists but the first will be pretty cheap. */
15982 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15983
15984 /* Move over display elements that are not visible because we are
15985 hscrolled. This may stop at an x-position < IT->first_visible_x
15986 if the first glyph is partially visible or if we hit a line end. */
15987 if (it->current_x < it->first_visible_x)
15988 {
15989 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15990 MOVE_TO_POS | MOVE_TO_X);
15991 }
15992
15993 /* Get the initial row height. This is either the height of the
15994 text hscrolled, if there is any, or zero. */
15995 row->ascent = it->max_ascent;
15996 row->height = it->max_ascent + it->max_descent;
15997 row->phys_ascent = it->max_phys_ascent;
15998 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15999 row->extra_line_spacing = it->max_extra_line_spacing;
16000
16001 /* Loop generating characters. The loop is left with IT on the next
16002 character to display. */
16003 while (1)
16004 {
16005 int n_glyphs_before, hpos_before, x_before;
16006 int x, i, nglyphs;
16007 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16008
16009 /* Retrieve the next thing to display. Value is zero if end of
16010 buffer reached. */
16011 if (!get_next_display_element (it))
16012 {
16013 /* Maybe add a space at the end of this line that is used to
16014 display the cursor there under X. Set the charpos of the
16015 first glyph of blank lines not corresponding to any text
16016 to -1. */
16017 #ifdef HAVE_WINDOW_SYSTEM
16018 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16019 row->exact_window_width_line_p = 1;
16020 else
16021 #endif /* HAVE_WINDOW_SYSTEM */
16022 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16023 || row->used[TEXT_AREA] == 0)
16024 {
16025 row->glyphs[TEXT_AREA]->charpos = -1;
16026 row->displays_text_p = 0;
16027
16028 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16029 && (!MINI_WINDOW_P (it->w)
16030 || (minibuf_level && EQ (it->window, minibuf_window))))
16031 row->indicate_empty_line_p = 1;
16032 }
16033
16034 it->continuation_lines_width = 0;
16035 row->ends_at_zv_p = 1;
16036 break;
16037 }
16038
16039 /* Now, get the metrics of what we want to display. This also
16040 generates glyphs in `row' (which is IT->glyph_row). */
16041 n_glyphs_before = row->used[TEXT_AREA];
16042 x = it->current_x;
16043
16044 /* Remember the line height so far in case the next element doesn't
16045 fit on the line. */
16046 if (!it->truncate_lines_p)
16047 {
16048 ascent = it->max_ascent;
16049 descent = it->max_descent;
16050 phys_ascent = it->max_phys_ascent;
16051 phys_descent = it->max_phys_descent;
16052 }
16053
16054 PRODUCE_GLYPHS (it);
16055
16056 /* If this display element was in marginal areas, continue with
16057 the next one. */
16058 if (it->area != TEXT_AREA)
16059 {
16060 row->ascent = max (row->ascent, it->max_ascent);
16061 row->height = max (row->height, it->max_ascent + it->max_descent);
16062 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16063 row->phys_height = max (row->phys_height,
16064 it->max_phys_ascent + it->max_phys_descent);
16065 row->extra_line_spacing = max (row->extra_line_spacing,
16066 it->max_extra_line_spacing);
16067 set_iterator_to_next (it, 1);
16068 continue;
16069 }
16070
16071 /* Does the display element fit on the line? If we truncate
16072 lines, we should draw past the right edge of the window. If
16073 we don't truncate, we want to stop so that we can display the
16074 continuation glyph before the right margin. If lines are
16075 continued, there are two possible strategies for characters
16076 resulting in more than 1 glyph (e.g. tabs): Display as many
16077 glyphs as possible in this line and leave the rest for the
16078 continuation line, or display the whole element in the next
16079 line. Original redisplay did the former, so we do it also. */
16080 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16081 hpos_before = it->hpos;
16082 x_before = x;
16083
16084 if (/* Not a newline. */
16085 nglyphs > 0
16086 /* Glyphs produced fit entirely in the line. */
16087 && it->current_x < it->last_visible_x)
16088 {
16089 it->hpos += nglyphs;
16090 row->ascent = max (row->ascent, it->max_ascent);
16091 row->height = max (row->height, it->max_ascent + it->max_descent);
16092 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16093 row->phys_height = max (row->phys_height,
16094 it->max_phys_ascent + it->max_phys_descent);
16095 row->extra_line_spacing = max (row->extra_line_spacing,
16096 it->max_extra_line_spacing);
16097 if (it->current_x - it->pixel_width < it->first_visible_x)
16098 row->x = x - it->first_visible_x;
16099 }
16100 else
16101 {
16102 int new_x;
16103 struct glyph *glyph;
16104
16105 for (i = 0; i < nglyphs; ++i, x = new_x)
16106 {
16107 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16108 new_x = x + glyph->pixel_width;
16109
16110 if (/* Lines are continued. */
16111 !it->truncate_lines_p
16112 && (/* Glyph doesn't fit on the line. */
16113 new_x > it->last_visible_x
16114 /* Or it fits exactly on a window system frame. */
16115 || (new_x == it->last_visible_x
16116 && FRAME_WINDOW_P (it->f))))
16117 {
16118 /* End of a continued line. */
16119
16120 if (it->hpos == 0
16121 || (new_x == it->last_visible_x
16122 && FRAME_WINDOW_P (it->f)))
16123 {
16124 /* Current glyph is the only one on the line or
16125 fits exactly on the line. We must continue
16126 the line because we can't draw the cursor
16127 after the glyph. */
16128 row->continued_p = 1;
16129 it->current_x = new_x;
16130 it->continuation_lines_width += new_x;
16131 ++it->hpos;
16132 if (i == nglyphs - 1)
16133 {
16134 set_iterator_to_next (it, 1);
16135 #ifdef HAVE_WINDOW_SYSTEM
16136 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16137 {
16138 if (!get_next_display_element (it))
16139 {
16140 row->exact_window_width_line_p = 1;
16141 it->continuation_lines_width = 0;
16142 row->continued_p = 0;
16143 row->ends_at_zv_p = 1;
16144 }
16145 else if (ITERATOR_AT_END_OF_LINE_P (it))
16146 {
16147 row->continued_p = 0;
16148 row->exact_window_width_line_p = 1;
16149 }
16150 }
16151 #endif /* HAVE_WINDOW_SYSTEM */
16152 }
16153 }
16154 else if (CHAR_GLYPH_PADDING_P (*glyph)
16155 && !FRAME_WINDOW_P (it->f))
16156 {
16157 /* A padding glyph that doesn't fit on this line.
16158 This means the whole character doesn't fit
16159 on the line. */
16160 row->used[TEXT_AREA] = n_glyphs_before;
16161
16162 /* Fill the rest of the row with continuation
16163 glyphs like in 20.x. */
16164 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16165 < row->glyphs[1 + TEXT_AREA])
16166 produce_special_glyphs (it, IT_CONTINUATION);
16167
16168 row->continued_p = 1;
16169 it->current_x = x_before;
16170 it->continuation_lines_width += x_before;
16171
16172 /* Restore the height to what it was before the
16173 element not fitting on the line. */
16174 it->max_ascent = ascent;
16175 it->max_descent = descent;
16176 it->max_phys_ascent = phys_ascent;
16177 it->max_phys_descent = phys_descent;
16178 }
16179 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16180 {
16181 /* A TAB that extends past the right edge of the
16182 window. This produces a single glyph on
16183 window system frames. We leave the glyph in
16184 this row and let it fill the row, but don't
16185 consume the TAB. */
16186 it->continuation_lines_width += it->last_visible_x;
16187 row->ends_in_middle_of_char_p = 1;
16188 row->continued_p = 1;
16189 glyph->pixel_width = it->last_visible_x - x;
16190 it->starts_in_middle_of_char_p = 1;
16191 }
16192 else
16193 {
16194 /* Something other than a TAB that draws past
16195 the right edge of the window. Restore
16196 positions to values before the element. */
16197 row->used[TEXT_AREA] = n_glyphs_before + i;
16198
16199 /* Display continuation glyphs. */
16200 if (!FRAME_WINDOW_P (it->f))
16201 produce_special_glyphs (it, IT_CONTINUATION);
16202 row->continued_p = 1;
16203
16204 it->current_x = x_before;
16205 it->continuation_lines_width += x;
16206 extend_face_to_end_of_line (it);
16207
16208 if (nglyphs > 1 && i > 0)
16209 {
16210 row->ends_in_middle_of_char_p = 1;
16211 it->starts_in_middle_of_char_p = 1;
16212 }
16213
16214 /* Restore the height to what it was before the
16215 element not fitting on the line. */
16216 it->max_ascent = ascent;
16217 it->max_descent = descent;
16218 it->max_phys_ascent = phys_ascent;
16219 it->max_phys_descent = phys_descent;
16220 }
16221
16222 break;
16223 }
16224 else if (new_x > it->first_visible_x)
16225 {
16226 /* Increment number of glyphs actually displayed. */
16227 ++it->hpos;
16228
16229 if (x < it->first_visible_x)
16230 /* Glyph is partially visible, i.e. row starts at
16231 negative X position. */
16232 row->x = x - it->first_visible_x;
16233 }
16234 else
16235 {
16236 /* Glyph is completely off the left margin of the
16237 window. This should not happen because of the
16238 move_it_in_display_line at the start of this
16239 function, unless the text display area of the
16240 window is empty. */
16241 xassert (it->first_visible_x <= it->last_visible_x);
16242 }
16243 }
16244
16245 row->ascent = max (row->ascent, it->max_ascent);
16246 row->height = max (row->height, it->max_ascent + it->max_descent);
16247 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16248 row->phys_height = max (row->phys_height,
16249 it->max_phys_ascent + it->max_phys_descent);
16250 row->extra_line_spacing = max (row->extra_line_spacing,
16251 it->max_extra_line_spacing);
16252
16253 /* End of this display line if row is continued. */
16254 if (row->continued_p || row->ends_at_zv_p)
16255 break;
16256 }
16257
16258 at_end_of_line:
16259 /* Is this a line end? If yes, we're also done, after making
16260 sure that a non-default face is extended up to the right
16261 margin of the window. */
16262 if (ITERATOR_AT_END_OF_LINE_P (it))
16263 {
16264 int used_before = row->used[TEXT_AREA];
16265
16266 row->ends_in_newline_from_string_p = STRINGP (it->object);
16267
16268 #ifdef HAVE_WINDOW_SYSTEM
16269 /* Add a space at the end of the line that is used to
16270 display the cursor there. */
16271 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16272 append_space_for_newline (it, 0);
16273 #endif /* HAVE_WINDOW_SYSTEM */
16274
16275 /* Extend the face to the end of the line. */
16276 extend_face_to_end_of_line (it);
16277
16278 /* Make sure we have the position. */
16279 if (used_before == 0)
16280 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16281
16282 /* Consume the line end. This skips over invisible lines. */
16283 set_iterator_to_next (it, 1);
16284 it->continuation_lines_width = 0;
16285 break;
16286 }
16287
16288 /* Proceed with next display element. Note that this skips
16289 over lines invisible because of selective display. */
16290 set_iterator_to_next (it, 1);
16291
16292 /* If we truncate lines, we are done when the last displayed
16293 glyphs reach past the right margin of the window. */
16294 if (it->truncate_lines_p
16295 && (FRAME_WINDOW_P (it->f)
16296 ? (it->current_x >= it->last_visible_x)
16297 : (it->current_x > it->last_visible_x)))
16298 {
16299 /* Maybe add truncation glyphs. */
16300 if (!FRAME_WINDOW_P (it->f))
16301 {
16302 int i, n;
16303
16304 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16305 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16306 break;
16307
16308 for (n = row->used[TEXT_AREA]; i < n; ++i)
16309 {
16310 row->used[TEXT_AREA] = i;
16311 produce_special_glyphs (it, IT_TRUNCATION);
16312 }
16313 }
16314 #ifdef HAVE_WINDOW_SYSTEM
16315 else
16316 {
16317 /* Don't truncate if we can overflow newline into fringe. */
16318 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16319 {
16320 if (!get_next_display_element (it))
16321 {
16322 it->continuation_lines_width = 0;
16323 row->ends_at_zv_p = 1;
16324 row->exact_window_width_line_p = 1;
16325 break;
16326 }
16327 if (ITERATOR_AT_END_OF_LINE_P (it))
16328 {
16329 row->exact_window_width_line_p = 1;
16330 goto at_end_of_line;
16331 }
16332 }
16333 }
16334 #endif /* HAVE_WINDOW_SYSTEM */
16335
16336 row->truncated_on_right_p = 1;
16337 it->continuation_lines_width = 0;
16338 reseat_at_next_visible_line_start (it, 0);
16339 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16340 it->hpos = hpos_before;
16341 it->current_x = x_before;
16342 break;
16343 }
16344 }
16345
16346 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16347 at the left window margin. */
16348 if (it->first_visible_x
16349 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16350 {
16351 if (!FRAME_WINDOW_P (it->f))
16352 insert_left_trunc_glyphs (it);
16353 row->truncated_on_left_p = 1;
16354 }
16355
16356 /* If the start of this line is the overlay arrow-position, then
16357 mark this glyph row as the one containing the overlay arrow.
16358 This is clearly a mess with variable size fonts. It would be
16359 better to let it be displayed like cursors under X. */
16360 if ((row->displays_text_p || !overlay_arrow_seen)
16361 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16362 !NILP (overlay_arrow_string)))
16363 {
16364 /* Overlay arrow in window redisplay is a fringe bitmap. */
16365 if (STRINGP (overlay_arrow_string))
16366 {
16367 struct glyph_row *arrow_row
16368 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16369 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16370 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16371 struct glyph *p = row->glyphs[TEXT_AREA];
16372 struct glyph *p2, *end;
16373
16374 /* Copy the arrow glyphs. */
16375 while (glyph < arrow_end)
16376 *p++ = *glyph++;
16377
16378 /* Throw away padding glyphs. */
16379 p2 = p;
16380 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16381 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16382 ++p2;
16383 if (p2 > p)
16384 {
16385 while (p2 < end)
16386 *p++ = *p2++;
16387 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16388 }
16389 }
16390 else
16391 {
16392 xassert (INTEGERP (overlay_arrow_string));
16393 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16394 }
16395 overlay_arrow_seen = 1;
16396 }
16397
16398 /* Compute pixel dimensions of this line. */
16399 compute_line_metrics (it);
16400
16401 /* Remember the position at which this line ends. */
16402 row->end = it->current;
16403
16404 /* Record whether this row ends inside an ellipsis. */
16405 row->ends_in_ellipsis_p
16406 = (it->method == GET_FROM_DISPLAY_VECTOR
16407 && it->ellipsis_p);
16408
16409 /* Save fringe bitmaps in this row. */
16410 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16411 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16412 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16413 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16414
16415 it->left_user_fringe_bitmap = 0;
16416 it->left_user_fringe_face_id = 0;
16417 it->right_user_fringe_bitmap = 0;
16418 it->right_user_fringe_face_id = 0;
16419
16420 /* Maybe set the cursor. */
16421 if (it->w->cursor.vpos < 0
16422 && PT >= MATRIX_ROW_START_CHARPOS (row)
16423 && PT <= MATRIX_ROW_END_CHARPOS (row)
16424 && cursor_row_p (it->w, row))
16425 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16426
16427 /* Highlight trailing whitespace. */
16428 if (!NILP (Vshow_trailing_whitespace))
16429 highlight_trailing_whitespace (it->f, it->glyph_row);
16430
16431 /* Prepare for the next line. This line starts horizontally at (X
16432 HPOS) = (0 0). Vertical positions are incremented. As a
16433 convenience for the caller, IT->glyph_row is set to the next
16434 row to be used. */
16435 it->current_x = it->hpos = 0;
16436 it->current_y += row->height;
16437 ++it->vpos;
16438 ++it->glyph_row;
16439 it->start = it->current;
16440 return row->displays_text_p;
16441 }
16442
16443
16444 \f
16445 /***********************************************************************
16446 Menu Bar
16447 ***********************************************************************/
16448
16449 /* Redisplay the menu bar in the frame for window W.
16450
16451 The menu bar of X frames that don't have X toolkit support is
16452 displayed in a special window W->frame->menu_bar_window.
16453
16454 The menu bar of terminal frames is treated specially as far as
16455 glyph matrices are concerned. Menu bar lines are not part of
16456 windows, so the update is done directly on the frame matrix rows
16457 for the menu bar. */
16458
16459 static void
16460 display_menu_bar (w)
16461 struct window *w;
16462 {
16463 struct frame *f = XFRAME (WINDOW_FRAME (w));
16464 struct it it;
16465 Lisp_Object items;
16466 int i;
16467
16468 /* Don't do all this for graphical frames. */
16469 #ifdef HAVE_NTGUI
16470 if (FRAME_W32_P (f))
16471 return;
16472 #endif
16473 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16474 if (FRAME_X_P (f))
16475 return;
16476 #endif
16477 #ifdef MAC_OS
16478 if (FRAME_MAC_P (f))
16479 return;
16480 #endif
16481
16482 #ifdef USE_X_TOOLKIT
16483 xassert (!FRAME_WINDOW_P (f));
16484 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16485 it.first_visible_x = 0;
16486 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16487 #else /* not USE_X_TOOLKIT */
16488 if (FRAME_WINDOW_P (f))
16489 {
16490 /* Menu bar lines are displayed in the desired matrix of the
16491 dummy window menu_bar_window. */
16492 struct window *menu_w;
16493 xassert (WINDOWP (f->menu_bar_window));
16494 menu_w = XWINDOW (f->menu_bar_window);
16495 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16496 MENU_FACE_ID);
16497 it.first_visible_x = 0;
16498 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16499 }
16500 else
16501 {
16502 /* This is a TTY frame, i.e. character hpos/vpos are used as
16503 pixel x/y. */
16504 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16505 MENU_FACE_ID);
16506 it.first_visible_x = 0;
16507 it.last_visible_x = FRAME_COLS (f);
16508 }
16509 #endif /* not USE_X_TOOLKIT */
16510
16511 if (! mode_line_inverse_video)
16512 /* Force the menu-bar to be displayed in the default face. */
16513 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16514
16515 /* Clear all rows of the menu bar. */
16516 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16517 {
16518 struct glyph_row *row = it.glyph_row + i;
16519 clear_glyph_row (row);
16520 row->enabled_p = 1;
16521 row->full_width_p = 1;
16522 }
16523
16524 /* Display all items of the menu bar. */
16525 items = FRAME_MENU_BAR_ITEMS (it.f);
16526 for (i = 0; i < XVECTOR (items)->size; i += 4)
16527 {
16528 Lisp_Object string;
16529
16530 /* Stop at nil string. */
16531 string = AREF (items, i + 1);
16532 if (NILP (string))
16533 break;
16534
16535 /* Remember where item was displayed. */
16536 AREF (items, i + 3) = make_number (it.hpos);
16537
16538 /* Display the item, pad with one space. */
16539 if (it.current_x < it.last_visible_x)
16540 display_string (NULL, string, Qnil, 0, 0, &it,
16541 SCHARS (string) + 1, 0, 0, -1);
16542 }
16543
16544 /* Fill out the line with spaces. */
16545 if (it.current_x < it.last_visible_x)
16546 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16547
16548 /* Compute the total height of the lines. */
16549 compute_line_metrics (&it);
16550 }
16551
16552
16553 \f
16554 /***********************************************************************
16555 Mode Line
16556 ***********************************************************************/
16557
16558 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16559 FORCE is non-zero, redisplay mode lines unconditionally.
16560 Otherwise, redisplay only mode lines that are garbaged. Value is
16561 the number of windows whose mode lines were redisplayed. */
16562
16563 static int
16564 redisplay_mode_lines (window, force)
16565 Lisp_Object window;
16566 int force;
16567 {
16568 int nwindows = 0;
16569
16570 while (!NILP (window))
16571 {
16572 struct window *w = XWINDOW (window);
16573
16574 if (WINDOWP (w->hchild))
16575 nwindows += redisplay_mode_lines (w->hchild, force);
16576 else if (WINDOWP (w->vchild))
16577 nwindows += redisplay_mode_lines (w->vchild, force);
16578 else if (force
16579 || FRAME_GARBAGED_P (XFRAME (w->frame))
16580 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16581 {
16582 struct text_pos lpoint;
16583 struct buffer *old = current_buffer;
16584
16585 /* Set the window's buffer for the mode line display. */
16586 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16587 set_buffer_internal_1 (XBUFFER (w->buffer));
16588
16589 /* Point refers normally to the selected window. For any
16590 other window, set up appropriate value. */
16591 if (!EQ (window, selected_window))
16592 {
16593 struct text_pos pt;
16594
16595 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16596 if (CHARPOS (pt) < BEGV)
16597 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16598 else if (CHARPOS (pt) > (ZV - 1))
16599 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16600 else
16601 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16602 }
16603
16604 /* Display mode lines. */
16605 clear_glyph_matrix (w->desired_matrix);
16606 if (display_mode_lines (w))
16607 {
16608 ++nwindows;
16609 w->must_be_updated_p = 1;
16610 }
16611
16612 /* Restore old settings. */
16613 set_buffer_internal_1 (old);
16614 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16615 }
16616
16617 window = w->next;
16618 }
16619
16620 return nwindows;
16621 }
16622
16623
16624 /* Display the mode and/or top line of window W. Value is the number
16625 of mode lines displayed. */
16626
16627 static int
16628 display_mode_lines (w)
16629 struct window *w;
16630 {
16631 Lisp_Object old_selected_window, old_selected_frame;
16632 int n = 0;
16633
16634 old_selected_frame = selected_frame;
16635 selected_frame = w->frame;
16636 old_selected_window = selected_window;
16637 XSETWINDOW (selected_window, w);
16638
16639 /* These will be set while the mode line specs are processed. */
16640 line_number_displayed = 0;
16641 w->column_number_displayed = Qnil;
16642
16643 if (WINDOW_WANTS_MODELINE_P (w))
16644 {
16645 struct window *sel_w = XWINDOW (old_selected_window);
16646
16647 /* Select mode line face based on the real selected window. */
16648 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16649 current_buffer->mode_line_format);
16650 ++n;
16651 }
16652
16653 if (WINDOW_WANTS_HEADER_LINE_P (w))
16654 {
16655 display_mode_line (w, HEADER_LINE_FACE_ID,
16656 current_buffer->header_line_format);
16657 ++n;
16658 }
16659
16660 selected_frame = old_selected_frame;
16661 selected_window = old_selected_window;
16662 return n;
16663 }
16664
16665
16666 /* Display mode or top line of window W. FACE_ID specifies which line
16667 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16668 FORMAT is the mode line format to display. Value is the pixel
16669 height of the mode line displayed. */
16670
16671 static int
16672 display_mode_line (w, face_id, format)
16673 struct window *w;
16674 enum face_id face_id;
16675 Lisp_Object format;
16676 {
16677 struct it it;
16678 struct face *face;
16679 int count = SPECPDL_INDEX ();
16680
16681 init_iterator (&it, w, -1, -1, NULL, face_id);
16682 /* Don't extend on a previously drawn mode-line.
16683 This may happen if called from pos_visible_p. */
16684 it.glyph_row->enabled_p = 0;
16685 prepare_desired_row (it.glyph_row);
16686
16687 it.glyph_row->mode_line_p = 1;
16688
16689 if (! mode_line_inverse_video)
16690 /* Force the mode-line to be displayed in the default face. */
16691 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16692
16693 record_unwind_protect (unwind_format_mode_line,
16694 format_mode_line_unwind_data (NULL, 0));
16695
16696 mode_line_target = MODE_LINE_DISPLAY;
16697
16698 /* Temporarily make frame's keyboard the current kboard so that
16699 kboard-local variables in the mode_line_format will get the right
16700 values. */
16701 push_kboard (FRAME_KBOARD (it.f));
16702 record_unwind_save_match_data ();
16703 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16704 pop_kboard ();
16705
16706 unbind_to (count, Qnil);
16707
16708 /* Fill up with spaces. */
16709 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16710
16711 compute_line_metrics (&it);
16712 it.glyph_row->full_width_p = 1;
16713 it.glyph_row->continued_p = 0;
16714 it.glyph_row->truncated_on_left_p = 0;
16715 it.glyph_row->truncated_on_right_p = 0;
16716
16717 /* Make a 3D mode-line have a shadow at its right end. */
16718 face = FACE_FROM_ID (it.f, face_id);
16719 extend_face_to_end_of_line (&it);
16720 if (face->box != FACE_NO_BOX)
16721 {
16722 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16723 + it.glyph_row->used[TEXT_AREA] - 1);
16724 last->right_box_line_p = 1;
16725 }
16726
16727 return it.glyph_row->height;
16728 }
16729
16730 /* Move element ELT in LIST to the front of LIST.
16731 Return the updated list. */
16732
16733 static Lisp_Object
16734 move_elt_to_front (elt, list)
16735 Lisp_Object elt, list;
16736 {
16737 register Lisp_Object tail, prev;
16738 register Lisp_Object tem;
16739
16740 tail = list;
16741 prev = Qnil;
16742 while (CONSP (tail))
16743 {
16744 tem = XCAR (tail);
16745
16746 if (EQ (elt, tem))
16747 {
16748 /* Splice out the link TAIL. */
16749 if (NILP (prev))
16750 list = XCDR (tail);
16751 else
16752 Fsetcdr (prev, XCDR (tail));
16753
16754 /* Now make it the first. */
16755 Fsetcdr (tail, list);
16756 return tail;
16757 }
16758 else
16759 prev = tail;
16760 tail = XCDR (tail);
16761 QUIT;
16762 }
16763
16764 /* Not found--return unchanged LIST. */
16765 return list;
16766 }
16767
16768 /* Contribute ELT to the mode line for window IT->w. How it
16769 translates into text depends on its data type.
16770
16771 IT describes the display environment in which we display, as usual.
16772
16773 DEPTH is the depth in recursion. It is used to prevent
16774 infinite recursion here.
16775
16776 FIELD_WIDTH is the number of characters the display of ELT should
16777 occupy in the mode line, and PRECISION is the maximum number of
16778 characters to display from ELT's representation. See
16779 display_string for details.
16780
16781 Returns the hpos of the end of the text generated by ELT.
16782
16783 PROPS is a property list to add to any string we encounter.
16784
16785 If RISKY is nonzero, remove (disregard) any properties in any string
16786 we encounter, and ignore :eval and :propertize.
16787
16788 The global variable `mode_line_target' determines whether the
16789 output is passed to `store_mode_line_noprop',
16790 `store_mode_line_string', or `display_string'. */
16791
16792 static int
16793 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16794 struct it *it;
16795 int depth;
16796 int field_width, precision;
16797 Lisp_Object elt, props;
16798 int risky;
16799 {
16800 int n = 0, field, prec;
16801 int literal = 0;
16802
16803 tail_recurse:
16804 if (depth > 100)
16805 elt = build_string ("*too-deep*");
16806
16807 depth++;
16808
16809 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16810 {
16811 case Lisp_String:
16812 {
16813 /* A string: output it and check for %-constructs within it. */
16814 unsigned char c;
16815 int offset = 0;
16816
16817 if (SCHARS (elt) > 0
16818 && (!NILP (props) || risky))
16819 {
16820 Lisp_Object oprops, aelt;
16821 oprops = Ftext_properties_at (make_number (0), elt);
16822
16823 /* If the starting string's properties are not what
16824 we want, translate the string. Also, if the string
16825 is risky, do that anyway. */
16826
16827 if (NILP (Fequal (props, oprops)) || risky)
16828 {
16829 /* If the starting string has properties,
16830 merge the specified ones onto the existing ones. */
16831 if (! NILP (oprops) && !risky)
16832 {
16833 Lisp_Object tem;
16834
16835 oprops = Fcopy_sequence (oprops);
16836 tem = props;
16837 while (CONSP (tem))
16838 {
16839 oprops = Fplist_put (oprops, XCAR (tem),
16840 XCAR (XCDR (tem)));
16841 tem = XCDR (XCDR (tem));
16842 }
16843 props = oprops;
16844 }
16845
16846 aelt = Fassoc (elt, mode_line_proptrans_alist);
16847 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16848 {
16849 /* AELT is what we want. Move it to the front
16850 without consing. */
16851 elt = XCAR (aelt);
16852 mode_line_proptrans_alist
16853 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16854 }
16855 else
16856 {
16857 Lisp_Object tem;
16858
16859 /* If AELT has the wrong props, it is useless.
16860 so get rid of it. */
16861 if (! NILP (aelt))
16862 mode_line_proptrans_alist
16863 = Fdelq (aelt, mode_line_proptrans_alist);
16864
16865 elt = Fcopy_sequence (elt);
16866 Fset_text_properties (make_number (0), Flength (elt),
16867 props, elt);
16868 /* Add this item to mode_line_proptrans_alist. */
16869 mode_line_proptrans_alist
16870 = Fcons (Fcons (elt, props),
16871 mode_line_proptrans_alist);
16872 /* Truncate mode_line_proptrans_alist
16873 to at most 50 elements. */
16874 tem = Fnthcdr (make_number (50),
16875 mode_line_proptrans_alist);
16876 if (! NILP (tem))
16877 XSETCDR (tem, Qnil);
16878 }
16879 }
16880 }
16881
16882 offset = 0;
16883
16884 if (literal)
16885 {
16886 prec = precision - n;
16887 switch (mode_line_target)
16888 {
16889 case MODE_LINE_NOPROP:
16890 case MODE_LINE_TITLE:
16891 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16892 break;
16893 case MODE_LINE_STRING:
16894 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16895 break;
16896 case MODE_LINE_DISPLAY:
16897 n += display_string (NULL, elt, Qnil, 0, 0, it,
16898 0, prec, 0, STRING_MULTIBYTE (elt));
16899 break;
16900 }
16901
16902 break;
16903 }
16904
16905 /* Handle the non-literal case. */
16906
16907 while ((precision <= 0 || n < precision)
16908 && SREF (elt, offset) != 0
16909 && (mode_line_target != MODE_LINE_DISPLAY
16910 || it->current_x < it->last_visible_x))
16911 {
16912 int last_offset = offset;
16913
16914 /* Advance to end of string or next format specifier. */
16915 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16916 ;
16917
16918 if (offset - 1 != last_offset)
16919 {
16920 int nchars, nbytes;
16921
16922 /* Output to end of string or up to '%'. Field width
16923 is length of string. Don't output more than
16924 PRECISION allows us. */
16925 offset--;
16926
16927 prec = c_string_width (SDATA (elt) + last_offset,
16928 offset - last_offset, precision - n,
16929 &nchars, &nbytes);
16930
16931 switch (mode_line_target)
16932 {
16933 case MODE_LINE_NOPROP:
16934 case MODE_LINE_TITLE:
16935 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16936 break;
16937 case MODE_LINE_STRING:
16938 {
16939 int bytepos = last_offset;
16940 int charpos = string_byte_to_char (elt, bytepos);
16941 int endpos = (precision <= 0
16942 ? string_byte_to_char (elt, offset)
16943 : charpos + nchars);
16944
16945 n += store_mode_line_string (NULL,
16946 Fsubstring (elt, make_number (charpos),
16947 make_number (endpos)),
16948 0, 0, 0, Qnil);
16949 }
16950 break;
16951 case MODE_LINE_DISPLAY:
16952 {
16953 int bytepos = last_offset;
16954 int charpos = string_byte_to_char (elt, bytepos);
16955
16956 if (precision <= 0)
16957 nchars = string_byte_to_char (elt, offset) - charpos;
16958 n += display_string (NULL, elt, Qnil, 0, charpos,
16959 it, 0, nchars, 0,
16960 STRING_MULTIBYTE (elt));
16961 }
16962 break;
16963 }
16964 }
16965 else /* c == '%' */
16966 {
16967 int percent_position = offset;
16968
16969 /* Get the specified minimum width. Zero means
16970 don't pad. */
16971 field = 0;
16972 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16973 field = field * 10 + c - '0';
16974
16975 /* Don't pad beyond the total padding allowed. */
16976 if (field_width - n > 0 && field > field_width - n)
16977 field = field_width - n;
16978
16979 /* Note that either PRECISION <= 0 or N < PRECISION. */
16980 prec = precision - n;
16981
16982 if (c == 'M')
16983 n += display_mode_element (it, depth, field, prec,
16984 Vglobal_mode_string, props,
16985 risky);
16986 else if (c != 0)
16987 {
16988 int multibyte;
16989 int bytepos, charpos;
16990 unsigned char *spec;
16991
16992 bytepos = percent_position;
16993 charpos = (STRING_MULTIBYTE (elt)
16994 ? string_byte_to_char (elt, bytepos)
16995 : bytepos);
16996
16997 spec
16998 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16999
17000 switch (mode_line_target)
17001 {
17002 case MODE_LINE_NOPROP:
17003 case MODE_LINE_TITLE:
17004 n += store_mode_line_noprop (spec, field, prec);
17005 break;
17006 case MODE_LINE_STRING:
17007 {
17008 int len = strlen (spec);
17009 Lisp_Object tem = make_string (spec, len);
17010 props = Ftext_properties_at (make_number (charpos), elt);
17011 /* Should only keep face property in props */
17012 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17013 }
17014 break;
17015 case MODE_LINE_DISPLAY:
17016 {
17017 int nglyphs_before, nwritten;
17018
17019 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17020 nwritten = display_string (spec, Qnil, elt,
17021 charpos, 0, it,
17022 field, prec, 0,
17023 multibyte);
17024
17025 /* Assign to the glyphs written above the
17026 string where the `%x' came from, position
17027 of the `%'. */
17028 if (nwritten > 0)
17029 {
17030 struct glyph *glyph
17031 = (it->glyph_row->glyphs[TEXT_AREA]
17032 + nglyphs_before);
17033 int i;
17034
17035 for (i = 0; i < nwritten; ++i)
17036 {
17037 glyph[i].object = elt;
17038 glyph[i].charpos = charpos;
17039 }
17040
17041 n += nwritten;
17042 }
17043 }
17044 break;
17045 }
17046 }
17047 else /* c == 0 */
17048 break;
17049 }
17050 }
17051 }
17052 break;
17053
17054 case Lisp_Symbol:
17055 /* A symbol: process the value of the symbol recursively
17056 as if it appeared here directly. Avoid error if symbol void.
17057 Special case: if value of symbol is a string, output the string
17058 literally. */
17059 {
17060 register Lisp_Object tem;
17061
17062 /* If the variable is not marked as risky to set
17063 then its contents are risky to use. */
17064 if (NILP (Fget (elt, Qrisky_local_variable)))
17065 risky = 1;
17066
17067 tem = Fboundp (elt);
17068 if (!NILP (tem))
17069 {
17070 tem = Fsymbol_value (elt);
17071 /* If value is a string, output that string literally:
17072 don't check for % within it. */
17073 if (STRINGP (tem))
17074 literal = 1;
17075
17076 if (!EQ (tem, elt))
17077 {
17078 /* Give up right away for nil or t. */
17079 elt = tem;
17080 goto tail_recurse;
17081 }
17082 }
17083 }
17084 break;
17085
17086 case Lisp_Cons:
17087 {
17088 register Lisp_Object car, tem;
17089
17090 /* A cons cell: five distinct cases.
17091 If first element is :eval or :propertize, do something special.
17092 If first element is a string or a cons, process all the elements
17093 and effectively concatenate them.
17094 If first element is a negative number, truncate displaying cdr to
17095 at most that many characters. If positive, pad (with spaces)
17096 to at least that many characters.
17097 If first element is a symbol, process the cadr or caddr recursively
17098 according to whether the symbol's value is non-nil or nil. */
17099 car = XCAR (elt);
17100 if (EQ (car, QCeval))
17101 {
17102 /* An element of the form (:eval FORM) means evaluate FORM
17103 and use the result as mode line elements. */
17104
17105 if (risky)
17106 break;
17107
17108 if (CONSP (XCDR (elt)))
17109 {
17110 Lisp_Object spec;
17111 spec = safe_eval (XCAR (XCDR (elt)));
17112 n += display_mode_element (it, depth, field_width - n,
17113 precision - n, spec, props,
17114 risky);
17115 }
17116 }
17117 else if (EQ (car, QCpropertize))
17118 {
17119 /* An element of the form (:propertize ELT PROPS...)
17120 means display ELT but applying properties PROPS. */
17121
17122 if (risky)
17123 break;
17124
17125 if (CONSP (XCDR (elt)))
17126 n += display_mode_element (it, depth, field_width - n,
17127 precision - n, XCAR (XCDR (elt)),
17128 XCDR (XCDR (elt)), risky);
17129 }
17130 else if (SYMBOLP (car))
17131 {
17132 tem = Fboundp (car);
17133 elt = XCDR (elt);
17134 if (!CONSP (elt))
17135 goto invalid;
17136 /* elt is now the cdr, and we know it is a cons cell.
17137 Use its car if CAR has a non-nil value. */
17138 if (!NILP (tem))
17139 {
17140 tem = Fsymbol_value (car);
17141 if (!NILP (tem))
17142 {
17143 elt = XCAR (elt);
17144 goto tail_recurse;
17145 }
17146 }
17147 /* Symbol's value is nil (or symbol is unbound)
17148 Get the cddr of the original list
17149 and if possible find the caddr and use that. */
17150 elt = XCDR (elt);
17151 if (NILP (elt))
17152 break;
17153 else if (!CONSP (elt))
17154 goto invalid;
17155 elt = XCAR (elt);
17156 goto tail_recurse;
17157 }
17158 else if (INTEGERP (car))
17159 {
17160 register int lim = XINT (car);
17161 elt = XCDR (elt);
17162 if (lim < 0)
17163 {
17164 /* Negative int means reduce maximum width. */
17165 if (precision <= 0)
17166 precision = -lim;
17167 else
17168 precision = min (precision, -lim);
17169 }
17170 else if (lim > 0)
17171 {
17172 /* Padding specified. Don't let it be more than
17173 current maximum. */
17174 if (precision > 0)
17175 lim = min (precision, lim);
17176
17177 /* If that's more padding than already wanted, queue it.
17178 But don't reduce padding already specified even if
17179 that is beyond the current truncation point. */
17180 field_width = max (lim, field_width);
17181 }
17182 goto tail_recurse;
17183 }
17184 else if (STRINGP (car) || CONSP (car))
17185 {
17186 register int limit = 50;
17187 /* Limit is to protect against circular lists. */
17188 while (CONSP (elt)
17189 && --limit > 0
17190 && (precision <= 0 || n < precision))
17191 {
17192 n += display_mode_element (it, depth,
17193 /* Do padding only after the last
17194 element in the list. */
17195 (! CONSP (XCDR (elt))
17196 ? field_width - n
17197 : 0),
17198 precision - n, XCAR (elt),
17199 props, risky);
17200 elt = XCDR (elt);
17201 }
17202 }
17203 }
17204 break;
17205
17206 default:
17207 invalid:
17208 elt = build_string ("*invalid*");
17209 goto tail_recurse;
17210 }
17211
17212 /* Pad to FIELD_WIDTH. */
17213 if (field_width > 0 && n < field_width)
17214 {
17215 switch (mode_line_target)
17216 {
17217 case MODE_LINE_NOPROP:
17218 case MODE_LINE_TITLE:
17219 n += store_mode_line_noprop ("", field_width - n, 0);
17220 break;
17221 case MODE_LINE_STRING:
17222 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17223 break;
17224 case MODE_LINE_DISPLAY:
17225 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17226 0, 0, 0);
17227 break;
17228 }
17229 }
17230
17231 return n;
17232 }
17233
17234 /* Store a mode-line string element in mode_line_string_list.
17235
17236 If STRING is non-null, display that C string. Otherwise, the Lisp
17237 string LISP_STRING is displayed.
17238
17239 FIELD_WIDTH is the minimum number of output glyphs to produce.
17240 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17241 with spaces. FIELD_WIDTH <= 0 means don't pad.
17242
17243 PRECISION is the maximum number of characters to output from
17244 STRING. PRECISION <= 0 means don't truncate the string.
17245
17246 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17247 properties to the string.
17248
17249 PROPS are the properties to add to the string.
17250 The mode_line_string_face face property is always added to the string.
17251 */
17252
17253 static int
17254 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17255 char *string;
17256 Lisp_Object lisp_string;
17257 int copy_string;
17258 int field_width;
17259 int precision;
17260 Lisp_Object props;
17261 {
17262 int len;
17263 int n = 0;
17264
17265 if (string != NULL)
17266 {
17267 len = strlen (string);
17268 if (precision > 0 && len > precision)
17269 len = precision;
17270 lisp_string = make_string (string, len);
17271 if (NILP (props))
17272 props = mode_line_string_face_prop;
17273 else if (!NILP (mode_line_string_face))
17274 {
17275 Lisp_Object face = Fplist_get (props, Qface);
17276 props = Fcopy_sequence (props);
17277 if (NILP (face))
17278 face = mode_line_string_face;
17279 else
17280 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17281 props = Fplist_put (props, Qface, face);
17282 }
17283 Fadd_text_properties (make_number (0), make_number (len),
17284 props, lisp_string);
17285 }
17286 else
17287 {
17288 len = XFASTINT (Flength (lisp_string));
17289 if (precision > 0 && len > precision)
17290 {
17291 len = precision;
17292 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17293 precision = -1;
17294 }
17295 if (!NILP (mode_line_string_face))
17296 {
17297 Lisp_Object face;
17298 if (NILP (props))
17299 props = Ftext_properties_at (make_number (0), lisp_string);
17300 face = Fplist_get (props, Qface);
17301 if (NILP (face))
17302 face = mode_line_string_face;
17303 else
17304 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17305 props = Fcons (Qface, Fcons (face, Qnil));
17306 if (copy_string)
17307 lisp_string = Fcopy_sequence (lisp_string);
17308 }
17309 if (!NILP (props))
17310 Fadd_text_properties (make_number (0), make_number (len),
17311 props, lisp_string);
17312 }
17313
17314 if (len > 0)
17315 {
17316 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17317 n += len;
17318 }
17319
17320 if (field_width > len)
17321 {
17322 field_width -= len;
17323 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17324 if (!NILP (props))
17325 Fadd_text_properties (make_number (0), make_number (field_width),
17326 props, lisp_string);
17327 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17328 n += field_width;
17329 }
17330
17331 return n;
17332 }
17333
17334
17335 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17336 1, 4, 0,
17337 doc: /* Format a string out of a mode line format specification.
17338 First arg FORMAT specifies the mode line format (see `mode-line-format'
17339 for details) to use.
17340
17341 Optional second arg FACE specifies the face property to put
17342 on all characters for which no face is specified.
17343 The value t means whatever face the window's mode line currently uses
17344 \(either `mode-line' or `mode-line-inactive', depending).
17345 A value of nil means the default is no face property.
17346 If FACE is an integer, the value string has no text properties.
17347
17348 Optional third and fourth args WINDOW and BUFFER specify the window
17349 and buffer to use as the context for the formatting (defaults
17350 are the selected window and the window's buffer). */)
17351 (format, face, window, buffer)
17352 Lisp_Object format, face, window, buffer;
17353 {
17354 struct it it;
17355 int len;
17356 struct window *w;
17357 struct buffer *old_buffer = NULL;
17358 int face_id = -1;
17359 int no_props = INTEGERP (face);
17360 int count = SPECPDL_INDEX ();
17361 Lisp_Object str;
17362 int string_start = 0;
17363
17364 if (NILP (window))
17365 window = selected_window;
17366 CHECK_WINDOW (window);
17367 w = XWINDOW (window);
17368
17369 if (NILP (buffer))
17370 buffer = w->buffer;
17371 CHECK_BUFFER (buffer);
17372
17373 if (NILP (format))
17374 return empty_unibyte_string;
17375
17376 if (no_props)
17377 face = Qnil;
17378
17379 if (!NILP (face))
17380 {
17381 if (EQ (face, Qt))
17382 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17383 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17384 }
17385
17386 if (face_id < 0)
17387 face_id = DEFAULT_FACE_ID;
17388
17389 if (XBUFFER (buffer) != current_buffer)
17390 old_buffer = current_buffer;
17391
17392 /* Save things including mode_line_proptrans_alist,
17393 and set that to nil so that we don't alter the outer value. */
17394 record_unwind_protect (unwind_format_mode_line,
17395 format_mode_line_unwind_data (old_buffer, 1));
17396 mode_line_proptrans_alist = Qnil;
17397
17398 if (old_buffer)
17399 set_buffer_internal_1 (XBUFFER (buffer));
17400
17401 init_iterator (&it, w, -1, -1, NULL, face_id);
17402
17403 if (no_props)
17404 {
17405 mode_line_target = MODE_LINE_NOPROP;
17406 mode_line_string_face_prop = Qnil;
17407 mode_line_string_list = Qnil;
17408 string_start = MODE_LINE_NOPROP_LEN (0);
17409 }
17410 else
17411 {
17412 mode_line_target = MODE_LINE_STRING;
17413 mode_line_string_list = Qnil;
17414 mode_line_string_face = face;
17415 mode_line_string_face_prop
17416 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17417 }
17418
17419 push_kboard (FRAME_KBOARD (it.f));
17420 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17421 pop_kboard ();
17422
17423 if (no_props)
17424 {
17425 len = MODE_LINE_NOPROP_LEN (string_start);
17426 str = make_string (mode_line_noprop_buf + string_start, len);
17427 }
17428 else
17429 {
17430 mode_line_string_list = Fnreverse (mode_line_string_list);
17431 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17432 empty_unibyte_string);
17433 }
17434
17435 unbind_to (count, Qnil);
17436 return str;
17437 }
17438
17439 /* Write a null-terminated, right justified decimal representation of
17440 the positive integer D to BUF using a minimal field width WIDTH. */
17441
17442 static void
17443 pint2str (buf, width, d)
17444 register char *buf;
17445 register int width;
17446 register int d;
17447 {
17448 register char *p = buf;
17449
17450 if (d <= 0)
17451 *p++ = '0';
17452 else
17453 {
17454 while (d > 0)
17455 {
17456 *p++ = d % 10 + '0';
17457 d /= 10;
17458 }
17459 }
17460
17461 for (width -= (int) (p - buf); width > 0; --width)
17462 *p++ = ' ';
17463 *p-- = '\0';
17464 while (p > buf)
17465 {
17466 d = *buf;
17467 *buf++ = *p;
17468 *p-- = d;
17469 }
17470 }
17471
17472 /* Write a null-terminated, right justified decimal and "human
17473 readable" representation of the nonnegative integer D to BUF using
17474 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17475
17476 static const char power_letter[] =
17477 {
17478 0, /* not used */
17479 'k', /* kilo */
17480 'M', /* mega */
17481 'G', /* giga */
17482 'T', /* tera */
17483 'P', /* peta */
17484 'E', /* exa */
17485 'Z', /* zetta */
17486 'Y' /* yotta */
17487 };
17488
17489 static void
17490 pint2hrstr (buf, width, d)
17491 char *buf;
17492 int width;
17493 int d;
17494 {
17495 /* We aim to represent the nonnegative integer D as
17496 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17497 int quotient = d;
17498 int remainder = 0;
17499 /* -1 means: do not use TENTHS. */
17500 int tenths = -1;
17501 int exponent = 0;
17502
17503 /* Length of QUOTIENT.TENTHS as a string. */
17504 int length;
17505
17506 char * psuffix;
17507 char * p;
17508
17509 if (1000 <= quotient)
17510 {
17511 /* Scale to the appropriate EXPONENT. */
17512 do
17513 {
17514 remainder = quotient % 1000;
17515 quotient /= 1000;
17516 exponent++;
17517 }
17518 while (1000 <= quotient);
17519
17520 /* Round to nearest and decide whether to use TENTHS or not. */
17521 if (quotient <= 9)
17522 {
17523 tenths = remainder / 100;
17524 if (50 <= remainder % 100)
17525 {
17526 if (tenths < 9)
17527 tenths++;
17528 else
17529 {
17530 quotient++;
17531 if (quotient == 10)
17532 tenths = -1;
17533 else
17534 tenths = 0;
17535 }
17536 }
17537 }
17538 else
17539 if (500 <= remainder)
17540 {
17541 if (quotient < 999)
17542 quotient++;
17543 else
17544 {
17545 quotient = 1;
17546 exponent++;
17547 tenths = 0;
17548 }
17549 }
17550 }
17551
17552 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17553 if (tenths == -1 && quotient <= 99)
17554 if (quotient <= 9)
17555 length = 1;
17556 else
17557 length = 2;
17558 else
17559 length = 3;
17560 p = psuffix = buf + max (width, length);
17561
17562 /* Print EXPONENT. */
17563 if (exponent)
17564 *psuffix++ = power_letter[exponent];
17565 *psuffix = '\0';
17566
17567 /* Print TENTHS. */
17568 if (tenths >= 0)
17569 {
17570 *--p = '0' + tenths;
17571 *--p = '.';
17572 }
17573
17574 /* Print QUOTIENT. */
17575 do
17576 {
17577 int digit = quotient % 10;
17578 *--p = '0' + digit;
17579 }
17580 while ((quotient /= 10) != 0);
17581
17582 /* Print leading spaces. */
17583 while (buf < p)
17584 *--p = ' ';
17585 }
17586
17587 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17588 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17589 type of CODING_SYSTEM. Return updated pointer into BUF. */
17590
17591 static unsigned char invalid_eol_type[] = "(*invalid*)";
17592
17593 static char *
17594 decode_mode_spec_coding (coding_system, buf, eol_flag)
17595 Lisp_Object coding_system;
17596 register char *buf;
17597 int eol_flag;
17598 {
17599 Lisp_Object val;
17600 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17601 const unsigned char *eol_str;
17602 int eol_str_len;
17603 /* The EOL conversion we are using. */
17604 Lisp_Object eoltype;
17605
17606 val = Fget (coding_system, Qcoding_system);
17607 eoltype = Qnil;
17608
17609 if (!VECTORP (val)) /* Not yet decided. */
17610 {
17611 if (multibyte)
17612 *buf++ = '-';
17613 if (eol_flag)
17614 eoltype = eol_mnemonic_undecided;
17615 /* Don't mention EOL conversion if it isn't decided. */
17616 }
17617 else
17618 {
17619 Lisp_Object eolvalue;
17620
17621 eolvalue = Fget (coding_system, Qeol_type);
17622
17623 if (multibyte)
17624 *buf++ = XFASTINT (AREF (val, 1));
17625
17626 if (eol_flag)
17627 {
17628 /* The EOL conversion that is normal on this system. */
17629
17630 if (NILP (eolvalue)) /* Not yet decided. */
17631 eoltype = eol_mnemonic_undecided;
17632 else if (VECTORP (eolvalue)) /* Not yet decided. */
17633 eoltype = eol_mnemonic_undecided;
17634 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17635 eoltype = (XFASTINT (eolvalue) == 0
17636 ? eol_mnemonic_unix
17637 : (XFASTINT (eolvalue) == 1
17638 ? eol_mnemonic_dos : eol_mnemonic_mac));
17639 }
17640 }
17641
17642 if (eol_flag)
17643 {
17644 /* Mention the EOL conversion if it is not the usual one. */
17645 if (STRINGP (eoltype))
17646 {
17647 eol_str = SDATA (eoltype);
17648 eol_str_len = SBYTES (eoltype);
17649 }
17650 else if (INTEGERP (eoltype)
17651 && CHAR_VALID_P (XINT (eoltype), 0))
17652 {
17653 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17654 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17655 eol_str = tmp;
17656 }
17657 else
17658 {
17659 eol_str = invalid_eol_type;
17660 eol_str_len = sizeof (invalid_eol_type) - 1;
17661 }
17662 bcopy (eol_str, buf, eol_str_len);
17663 buf += eol_str_len;
17664 }
17665
17666 return buf;
17667 }
17668
17669 /* Return a string for the output of a mode line %-spec for window W,
17670 generated by character C. PRECISION >= 0 means don't return a
17671 string longer than that value. FIELD_WIDTH > 0 means pad the
17672 string returned with spaces to that value. Return 1 in *MULTIBYTE
17673 if the result is multibyte text.
17674
17675 Note we operate on the current buffer for most purposes,
17676 the exception being w->base_line_pos. */
17677
17678 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17679
17680 static char *
17681 decode_mode_spec (w, c, field_width, precision, multibyte)
17682 struct window *w;
17683 register int c;
17684 int field_width, precision;
17685 int *multibyte;
17686 {
17687 Lisp_Object obj;
17688 struct frame *f = XFRAME (WINDOW_FRAME (w));
17689 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17690 struct buffer *b = current_buffer;
17691
17692 obj = Qnil;
17693 *multibyte = 0;
17694
17695 switch (c)
17696 {
17697 case '*':
17698 if (!NILP (b->read_only))
17699 return "%";
17700 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17701 return "*";
17702 return "-";
17703
17704 case '+':
17705 /* This differs from %* only for a modified read-only buffer. */
17706 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17707 return "*";
17708 if (!NILP (b->read_only))
17709 return "%";
17710 return "-";
17711
17712 case '&':
17713 /* This differs from %* in ignoring read-only-ness. */
17714 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17715 return "*";
17716 return "-";
17717
17718 case '%':
17719 return "%";
17720
17721 case '[':
17722 {
17723 int i;
17724 char *p;
17725
17726 if (command_loop_level > 5)
17727 return "[[[... ";
17728 p = decode_mode_spec_buf;
17729 for (i = 0; i < command_loop_level; i++)
17730 *p++ = '[';
17731 *p = 0;
17732 return decode_mode_spec_buf;
17733 }
17734
17735 case ']':
17736 {
17737 int i;
17738 char *p;
17739
17740 if (command_loop_level > 5)
17741 return " ...]]]";
17742 p = decode_mode_spec_buf;
17743 for (i = 0; i < command_loop_level; i++)
17744 *p++ = ']';
17745 *p = 0;
17746 return decode_mode_spec_buf;
17747 }
17748
17749 case '-':
17750 {
17751 register int i;
17752
17753 /* Let lots_of_dashes be a string of infinite length. */
17754 if (mode_line_target == MODE_LINE_NOPROP ||
17755 mode_line_target == MODE_LINE_STRING)
17756 return "--";
17757 if (field_width <= 0
17758 || field_width > sizeof (lots_of_dashes))
17759 {
17760 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17761 decode_mode_spec_buf[i] = '-';
17762 decode_mode_spec_buf[i] = '\0';
17763 return decode_mode_spec_buf;
17764 }
17765 else
17766 return lots_of_dashes;
17767 }
17768
17769 case 'b':
17770 obj = b->name;
17771 break;
17772
17773 case 'c':
17774 /* %c and %l are ignored in `frame-title-format'.
17775 (In redisplay_internal, the frame title is drawn _before_ the
17776 windows are updated, so the stuff which depends on actual
17777 window contents (such as %l) may fail to render properly, or
17778 even crash emacs.) */
17779 if (mode_line_target == MODE_LINE_TITLE)
17780 return "";
17781 else
17782 {
17783 int col = (int) current_column (); /* iftc */
17784 w->column_number_displayed = make_number (col);
17785 pint2str (decode_mode_spec_buf, field_width, col);
17786 return decode_mode_spec_buf;
17787 }
17788
17789 case 'e':
17790 #ifndef SYSTEM_MALLOC
17791 {
17792 if (NILP (Vmemory_full))
17793 return "";
17794 else
17795 return "!MEM FULL! ";
17796 }
17797 #else
17798 return "";
17799 #endif
17800
17801 case 'F':
17802 /* %F displays the frame name. */
17803 if (!NILP (f->title))
17804 return (char *) SDATA (f->title);
17805 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17806 return (char *) SDATA (f->name);
17807 return "Emacs";
17808
17809 case 'f':
17810 obj = b->filename;
17811 break;
17812
17813 case 'i':
17814 {
17815 int size = ZV - BEGV;
17816 pint2str (decode_mode_spec_buf, field_width, size);
17817 return decode_mode_spec_buf;
17818 }
17819
17820 case 'I':
17821 {
17822 int size = ZV - BEGV;
17823 pint2hrstr (decode_mode_spec_buf, field_width, size);
17824 return decode_mode_spec_buf;
17825 }
17826
17827 case 'l':
17828 {
17829 int startpos, startpos_byte, line, linepos, linepos_byte;
17830 int topline, nlines, junk, height;
17831
17832 /* %c and %l are ignored in `frame-title-format'. */
17833 if (mode_line_target == MODE_LINE_TITLE)
17834 return "";
17835
17836 startpos = XMARKER (w->start)->charpos;
17837 startpos_byte = marker_byte_position (w->start);
17838 height = WINDOW_TOTAL_LINES (w);
17839
17840 /* If we decided that this buffer isn't suitable for line numbers,
17841 don't forget that too fast. */
17842 if (EQ (w->base_line_pos, w->buffer))
17843 goto no_value;
17844 /* But do forget it, if the window shows a different buffer now. */
17845 else if (BUFFERP (w->base_line_pos))
17846 w->base_line_pos = Qnil;
17847
17848 /* If the buffer is very big, don't waste time. */
17849 if (INTEGERP (Vline_number_display_limit)
17850 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17851 {
17852 w->base_line_pos = Qnil;
17853 w->base_line_number = Qnil;
17854 goto no_value;
17855 }
17856
17857 if (!NILP (w->base_line_number)
17858 && !NILP (w->base_line_pos)
17859 && XFASTINT (w->base_line_pos) <= startpos)
17860 {
17861 line = XFASTINT (w->base_line_number);
17862 linepos = XFASTINT (w->base_line_pos);
17863 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17864 }
17865 else
17866 {
17867 line = 1;
17868 linepos = BUF_BEGV (b);
17869 linepos_byte = BUF_BEGV_BYTE (b);
17870 }
17871
17872 /* Count lines from base line to window start position. */
17873 nlines = display_count_lines (linepos, linepos_byte,
17874 startpos_byte,
17875 startpos, &junk);
17876
17877 topline = nlines + line;
17878
17879 /* Determine a new base line, if the old one is too close
17880 or too far away, or if we did not have one.
17881 "Too close" means it's plausible a scroll-down would
17882 go back past it. */
17883 if (startpos == BUF_BEGV (b))
17884 {
17885 w->base_line_number = make_number (topline);
17886 w->base_line_pos = make_number (BUF_BEGV (b));
17887 }
17888 else if (nlines < height + 25 || nlines > height * 3 + 50
17889 || linepos == BUF_BEGV (b))
17890 {
17891 int limit = BUF_BEGV (b);
17892 int limit_byte = BUF_BEGV_BYTE (b);
17893 int position;
17894 int distance = (height * 2 + 30) * line_number_display_limit_width;
17895
17896 if (startpos - distance > limit)
17897 {
17898 limit = startpos - distance;
17899 limit_byte = CHAR_TO_BYTE (limit);
17900 }
17901
17902 nlines = display_count_lines (startpos, startpos_byte,
17903 limit_byte,
17904 - (height * 2 + 30),
17905 &position);
17906 /* If we couldn't find the lines we wanted within
17907 line_number_display_limit_width chars per line,
17908 give up on line numbers for this window. */
17909 if (position == limit_byte && limit == startpos - distance)
17910 {
17911 w->base_line_pos = w->buffer;
17912 w->base_line_number = Qnil;
17913 goto no_value;
17914 }
17915
17916 w->base_line_number = make_number (topline - nlines);
17917 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17918 }
17919
17920 /* Now count lines from the start pos to point. */
17921 nlines = display_count_lines (startpos, startpos_byte,
17922 PT_BYTE, PT, &junk);
17923
17924 /* Record that we did display the line number. */
17925 line_number_displayed = 1;
17926
17927 /* Make the string to show. */
17928 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17929 return decode_mode_spec_buf;
17930 no_value:
17931 {
17932 char* p = decode_mode_spec_buf;
17933 int pad = field_width - 2;
17934 while (pad-- > 0)
17935 *p++ = ' ';
17936 *p++ = '?';
17937 *p++ = '?';
17938 *p = '\0';
17939 return decode_mode_spec_buf;
17940 }
17941 }
17942 break;
17943
17944 case 'm':
17945 obj = b->mode_name;
17946 break;
17947
17948 case 'n':
17949 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17950 return " Narrow";
17951 break;
17952
17953 case 'p':
17954 {
17955 int pos = marker_position (w->start);
17956 int total = BUF_ZV (b) - BUF_BEGV (b);
17957
17958 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17959 {
17960 if (pos <= BUF_BEGV (b))
17961 return "All";
17962 else
17963 return "Bottom";
17964 }
17965 else if (pos <= BUF_BEGV (b))
17966 return "Top";
17967 else
17968 {
17969 if (total > 1000000)
17970 /* Do it differently for a large value, to avoid overflow. */
17971 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17972 else
17973 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17974 /* We can't normally display a 3-digit number,
17975 so get us a 2-digit number that is close. */
17976 if (total == 100)
17977 total = 99;
17978 sprintf (decode_mode_spec_buf, "%2d%%", total);
17979 return decode_mode_spec_buf;
17980 }
17981 }
17982
17983 /* Display percentage of size above the bottom of the screen. */
17984 case 'P':
17985 {
17986 int toppos = marker_position (w->start);
17987 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17988 int total = BUF_ZV (b) - BUF_BEGV (b);
17989
17990 if (botpos >= BUF_ZV (b))
17991 {
17992 if (toppos <= BUF_BEGV (b))
17993 return "All";
17994 else
17995 return "Bottom";
17996 }
17997 else
17998 {
17999 if (total > 1000000)
18000 /* Do it differently for a large value, to avoid overflow. */
18001 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18002 else
18003 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18004 /* We can't normally display a 3-digit number,
18005 so get us a 2-digit number that is close. */
18006 if (total == 100)
18007 total = 99;
18008 if (toppos <= BUF_BEGV (b))
18009 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18010 else
18011 sprintf (decode_mode_spec_buf, "%2d%%", total);
18012 return decode_mode_spec_buf;
18013 }
18014 }
18015
18016 case 's':
18017 /* status of process */
18018 obj = Fget_buffer_process (Fcurrent_buffer ());
18019 if (NILP (obj))
18020 return "no process";
18021 #ifdef subprocesses
18022 obj = Fsymbol_name (Fprocess_status (obj));
18023 #endif
18024 break;
18025
18026 case '@':
18027 {
18028 Lisp_Object val;
18029 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18030 if (NILP (val))
18031 return "-";
18032 else
18033 return "@";
18034 }
18035
18036 case 't': /* indicate TEXT or BINARY */
18037 #ifdef MODE_LINE_BINARY_TEXT
18038 return MODE_LINE_BINARY_TEXT (b);
18039 #else
18040 return "T";
18041 #endif
18042
18043 case 'z':
18044 /* coding-system (not including end-of-line format) */
18045 case 'Z':
18046 /* coding-system (including end-of-line type) */
18047 {
18048 int eol_flag = (c == 'Z');
18049 char *p = decode_mode_spec_buf;
18050
18051 if (! FRAME_WINDOW_P (f))
18052 {
18053 /* No need to mention EOL here--the terminal never needs
18054 to do EOL conversion. */
18055 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
18056 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
18057 }
18058 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18059 p, eol_flag);
18060
18061 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18062 #ifdef subprocesses
18063 obj = Fget_buffer_process (Fcurrent_buffer ());
18064 if (PROCESSP (obj))
18065 {
18066 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18067 p, eol_flag);
18068 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18069 p, eol_flag);
18070 }
18071 #endif /* subprocesses */
18072 #endif /* 0 */
18073 *p = 0;
18074 return decode_mode_spec_buf;
18075 }
18076 }
18077
18078 if (STRINGP (obj))
18079 {
18080 *multibyte = STRING_MULTIBYTE (obj);
18081 return (char *) SDATA (obj);
18082 }
18083 else
18084 return "";
18085 }
18086
18087
18088 /* Count up to COUNT lines starting from START / START_BYTE.
18089 But don't go beyond LIMIT_BYTE.
18090 Return the number of lines thus found (always nonnegative).
18091
18092 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18093
18094 static int
18095 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18096 int start, start_byte, limit_byte, count;
18097 int *byte_pos_ptr;
18098 {
18099 register unsigned char *cursor;
18100 unsigned char *base;
18101
18102 register int ceiling;
18103 register unsigned char *ceiling_addr;
18104 int orig_count = count;
18105
18106 /* If we are not in selective display mode,
18107 check only for newlines. */
18108 int selective_display = (!NILP (current_buffer->selective_display)
18109 && !INTEGERP (current_buffer->selective_display));
18110
18111 if (count > 0)
18112 {
18113 while (start_byte < limit_byte)
18114 {
18115 ceiling = BUFFER_CEILING_OF (start_byte);
18116 ceiling = min (limit_byte - 1, ceiling);
18117 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18118 base = (cursor = BYTE_POS_ADDR (start_byte));
18119 while (1)
18120 {
18121 if (selective_display)
18122 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18123 ;
18124 else
18125 while (*cursor != '\n' && ++cursor != ceiling_addr)
18126 ;
18127
18128 if (cursor != ceiling_addr)
18129 {
18130 if (--count == 0)
18131 {
18132 start_byte += cursor - base + 1;
18133 *byte_pos_ptr = start_byte;
18134 return orig_count;
18135 }
18136 else
18137 if (++cursor == ceiling_addr)
18138 break;
18139 }
18140 else
18141 break;
18142 }
18143 start_byte += cursor - base;
18144 }
18145 }
18146 else
18147 {
18148 while (start_byte > limit_byte)
18149 {
18150 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18151 ceiling = max (limit_byte, ceiling);
18152 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18153 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18154 while (1)
18155 {
18156 if (selective_display)
18157 while (--cursor != ceiling_addr
18158 && *cursor != '\n' && *cursor != 015)
18159 ;
18160 else
18161 while (--cursor != ceiling_addr && *cursor != '\n')
18162 ;
18163
18164 if (cursor != ceiling_addr)
18165 {
18166 if (++count == 0)
18167 {
18168 start_byte += cursor - base + 1;
18169 *byte_pos_ptr = start_byte;
18170 /* When scanning backwards, we should
18171 not count the newline posterior to which we stop. */
18172 return - orig_count - 1;
18173 }
18174 }
18175 else
18176 break;
18177 }
18178 /* Here we add 1 to compensate for the last decrement
18179 of CURSOR, which took it past the valid range. */
18180 start_byte += cursor - base + 1;
18181 }
18182 }
18183
18184 *byte_pos_ptr = limit_byte;
18185
18186 if (count < 0)
18187 return - orig_count + count;
18188 return orig_count - count;
18189
18190 }
18191
18192
18193 \f
18194 /***********************************************************************
18195 Displaying strings
18196 ***********************************************************************/
18197
18198 /* Display a NUL-terminated string, starting with index START.
18199
18200 If STRING is non-null, display that C string. Otherwise, the Lisp
18201 string LISP_STRING is displayed.
18202
18203 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18204 FACE_STRING. Display STRING or LISP_STRING with the face at
18205 FACE_STRING_POS in FACE_STRING:
18206
18207 Display the string in the environment given by IT, but use the
18208 standard display table, temporarily.
18209
18210 FIELD_WIDTH is the minimum number of output glyphs to produce.
18211 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18212 with spaces. If STRING has more characters, more than FIELD_WIDTH
18213 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18214
18215 PRECISION is the maximum number of characters to output from
18216 STRING. PRECISION < 0 means don't truncate the string.
18217
18218 This is roughly equivalent to printf format specifiers:
18219
18220 FIELD_WIDTH PRECISION PRINTF
18221 ----------------------------------------
18222 -1 -1 %s
18223 -1 10 %.10s
18224 10 -1 %10s
18225 20 10 %20.10s
18226
18227 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18228 display them, and < 0 means obey the current buffer's value of
18229 enable_multibyte_characters.
18230
18231 Value is the number of columns displayed. */
18232
18233 static int
18234 display_string (string, lisp_string, face_string, face_string_pos,
18235 start, it, field_width, precision, max_x, multibyte)
18236 unsigned char *string;
18237 Lisp_Object lisp_string;
18238 Lisp_Object face_string;
18239 int face_string_pos;
18240 int start;
18241 struct it *it;
18242 int field_width, precision, max_x;
18243 int multibyte;
18244 {
18245 int hpos_at_start = it->hpos;
18246 int saved_face_id = it->face_id;
18247 struct glyph_row *row = it->glyph_row;
18248
18249 /* Initialize the iterator IT for iteration over STRING beginning
18250 with index START. */
18251 reseat_to_string (it, string, lisp_string, start,
18252 precision, field_width, multibyte);
18253
18254 /* If displaying STRING, set up the face of the iterator
18255 from LISP_STRING, if that's given. */
18256 if (STRINGP (face_string))
18257 {
18258 int endptr;
18259 struct face *face;
18260
18261 it->face_id
18262 = face_at_string_position (it->w, face_string, face_string_pos,
18263 0, it->region_beg_charpos,
18264 it->region_end_charpos,
18265 &endptr, it->base_face_id, 0);
18266 face = FACE_FROM_ID (it->f, it->face_id);
18267 it->face_box_p = face->box != FACE_NO_BOX;
18268 }
18269
18270 /* Set max_x to the maximum allowed X position. Don't let it go
18271 beyond the right edge of the window. */
18272 if (max_x <= 0)
18273 max_x = it->last_visible_x;
18274 else
18275 max_x = min (max_x, it->last_visible_x);
18276
18277 /* Skip over display elements that are not visible. because IT->w is
18278 hscrolled. */
18279 if (it->current_x < it->first_visible_x)
18280 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18281 MOVE_TO_POS | MOVE_TO_X);
18282
18283 row->ascent = it->max_ascent;
18284 row->height = it->max_ascent + it->max_descent;
18285 row->phys_ascent = it->max_phys_ascent;
18286 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18287 row->extra_line_spacing = it->max_extra_line_spacing;
18288
18289 /* This condition is for the case that we are called with current_x
18290 past last_visible_x. */
18291 while (it->current_x < max_x)
18292 {
18293 int x_before, x, n_glyphs_before, i, nglyphs;
18294
18295 /* Get the next display element. */
18296 if (!get_next_display_element (it))
18297 break;
18298
18299 /* Produce glyphs. */
18300 x_before = it->current_x;
18301 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18302 PRODUCE_GLYPHS (it);
18303
18304 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18305 i = 0;
18306 x = x_before;
18307 while (i < nglyphs)
18308 {
18309 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18310
18311 if (!it->truncate_lines_p
18312 && x + glyph->pixel_width > max_x)
18313 {
18314 /* End of continued line or max_x reached. */
18315 if (CHAR_GLYPH_PADDING_P (*glyph))
18316 {
18317 /* A wide character is unbreakable. */
18318 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18319 it->current_x = x_before;
18320 }
18321 else
18322 {
18323 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18324 it->current_x = x;
18325 }
18326 break;
18327 }
18328 else if (x + glyph->pixel_width > it->first_visible_x)
18329 {
18330 /* Glyph is at least partially visible. */
18331 ++it->hpos;
18332 if (x < it->first_visible_x)
18333 it->glyph_row->x = x - it->first_visible_x;
18334 }
18335 else
18336 {
18337 /* Glyph is off the left margin of the display area.
18338 Should not happen. */
18339 abort ();
18340 }
18341
18342 row->ascent = max (row->ascent, it->max_ascent);
18343 row->height = max (row->height, it->max_ascent + it->max_descent);
18344 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18345 row->phys_height = max (row->phys_height,
18346 it->max_phys_ascent + it->max_phys_descent);
18347 row->extra_line_spacing = max (row->extra_line_spacing,
18348 it->max_extra_line_spacing);
18349 x += glyph->pixel_width;
18350 ++i;
18351 }
18352
18353 /* Stop if max_x reached. */
18354 if (i < nglyphs)
18355 break;
18356
18357 /* Stop at line ends. */
18358 if (ITERATOR_AT_END_OF_LINE_P (it))
18359 {
18360 it->continuation_lines_width = 0;
18361 break;
18362 }
18363
18364 set_iterator_to_next (it, 1);
18365
18366 /* Stop if truncating at the right edge. */
18367 if (it->truncate_lines_p
18368 && it->current_x >= it->last_visible_x)
18369 {
18370 /* Add truncation mark, but don't do it if the line is
18371 truncated at a padding space. */
18372 if (IT_CHARPOS (*it) < it->string_nchars)
18373 {
18374 if (!FRAME_WINDOW_P (it->f))
18375 {
18376 int i, n;
18377
18378 if (it->current_x > it->last_visible_x)
18379 {
18380 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18381 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18382 break;
18383 for (n = row->used[TEXT_AREA]; i < n; ++i)
18384 {
18385 row->used[TEXT_AREA] = i;
18386 produce_special_glyphs (it, IT_TRUNCATION);
18387 }
18388 }
18389 produce_special_glyphs (it, IT_TRUNCATION);
18390 }
18391 it->glyph_row->truncated_on_right_p = 1;
18392 }
18393 break;
18394 }
18395 }
18396
18397 /* Maybe insert a truncation at the left. */
18398 if (it->first_visible_x
18399 && IT_CHARPOS (*it) > 0)
18400 {
18401 if (!FRAME_WINDOW_P (it->f))
18402 insert_left_trunc_glyphs (it);
18403 it->glyph_row->truncated_on_left_p = 1;
18404 }
18405
18406 it->face_id = saved_face_id;
18407
18408 /* Value is number of columns displayed. */
18409 return it->hpos - hpos_at_start;
18410 }
18411
18412
18413 \f
18414 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18415 appears as an element of LIST or as the car of an element of LIST.
18416 If PROPVAL is a list, compare each element against LIST in that
18417 way, and return 1/2 if any element of PROPVAL is found in LIST.
18418 Otherwise return 0. This function cannot quit.
18419 The return value is 2 if the text is invisible but with an ellipsis
18420 and 1 if it's invisible and without an ellipsis. */
18421
18422 int
18423 invisible_p (propval, list)
18424 register Lisp_Object propval;
18425 Lisp_Object list;
18426 {
18427 register Lisp_Object tail, proptail;
18428
18429 for (tail = list; CONSP (tail); tail = XCDR (tail))
18430 {
18431 register Lisp_Object tem;
18432 tem = XCAR (tail);
18433 if (EQ (propval, tem))
18434 return 1;
18435 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18436 return NILP (XCDR (tem)) ? 1 : 2;
18437 }
18438
18439 if (CONSP (propval))
18440 {
18441 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18442 {
18443 Lisp_Object propelt;
18444 propelt = XCAR (proptail);
18445 for (tail = list; CONSP (tail); tail = XCDR (tail))
18446 {
18447 register Lisp_Object tem;
18448 tem = XCAR (tail);
18449 if (EQ (propelt, tem))
18450 return 1;
18451 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18452 return NILP (XCDR (tem)) ? 1 : 2;
18453 }
18454 }
18455 }
18456
18457 return 0;
18458 }
18459
18460 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18461 doc: /* Non-nil if the property makes the text invisible.
18462 POS-OR-PROP can be a marker or number, in which case it is taken to be
18463 a position in the current buffer and the value of the `invisible' property
18464 is checked; or it can be some other value, which is then presumed to be the
18465 value of the `invisible' property of the text of interest.
18466 The non-nil value returned can be t for truly invisible text or something
18467 else if the text is replaced by an ellipsis. */)
18468 (pos_or_prop)
18469 Lisp_Object pos_or_prop;
18470 {
18471 Lisp_Object prop
18472 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18473 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18474 : pos_or_prop);
18475 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18476 return (invis == 0 ? Qnil
18477 : invis == 1 ? Qt
18478 : make_number (invis));
18479 }
18480
18481 /* Calculate a width or height in pixels from a specification using
18482 the following elements:
18483
18484 SPEC ::=
18485 NUM - a (fractional) multiple of the default font width/height
18486 (NUM) - specifies exactly NUM pixels
18487 UNIT - a fixed number of pixels, see below.
18488 ELEMENT - size of a display element in pixels, see below.
18489 (NUM . SPEC) - equals NUM * SPEC
18490 (+ SPEC SPEC ...) - add pixel values
18491 (- SPEC SPEC ...) - subtract pixel values
18492 (- SPEC) - negate pixel value
18493
18494 NUM ::=
18495 INT or FLOAT - a number constant
18496 SYMBOL - use symbol's (buffer local) variable binding.
18497
18498 UNIT ::=
18499 in - pixels per inch *)
18500 mm - pixels per 1/1000 meter *)
18501 cm - pixels per 1/100 meter *)
18502 width - width of current font in pixels.
18503 height - height of current font in pixels.
18504
18505 *) using the ratio(s) defined in display-pixels-per-inch.
18506
18507 ELEMENT ::=
18508
18509 left-fringe - left fringe width in pixels
18510 right-fringe - right fringe width in pixels
18511
18512 left-margin - left margin width in pixels
18513 right-margin - right margin width in pixels
18514
18515 scroll-bar - scroll-bar area width in pixels
18516
18517 Examples:
18518
18519 Pixels corresponding to 5 inches:
18520 (5 . in)
18521
18522 Total width of non-text areas on left side of window (if scroll-bar is on left):
18523 '(space :width (+ left-fringe left-margin scroll-bar))
18524
18525 Align to first text column (in header line):
18526 '(space :align-to 0)
18527
18528 Align to middle of text area minus half the width of variable `my-image'
18529 containing a loaded image:
18530 '(space :align-to (0.5 . (- text my-image)))
18531
18532 Width of left margin minus width of 1 character in the default font:
18533 '(space :width (- left-margin 1))
18534
18535 Width of left margin minus width of 2 characters in the current font:
18536 '(space :width (- left-margin (2 . width)))
18537
18538 Center 1 character over left-margin (in header line):
18539 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18540
18541 Different ways to express width of left fringe plus left margin minus one pixel:
18542 '(space :width (- (+ left-fringe left-margin) (1)))
18543 '(space :width (+ left-fringe left-margin (- (1))))
18544 '(space :width (+ left-fringe left-margin (-1)))
18545
18546 */
18547
18548 #define NUMVAL(X) \
18549 ((INTEGERP (X) || FLOATP (X)) \
18550 ? XFLOATINT (X) \
18551 : - 1)
18552
18553 int
18554 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18555 double *res;
18556 struct it *it;
18557 Lisp_Object prop;
18558 void *font;
18559 int width_p, *align_to;
18560 {
18561 double pixels;
18562
18563 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18564 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18565
18566 if (NILP (prop))
18567 return OK_PIXELS (0);
18568
18569 xassert (FRAME_LIVE_P (it->f));
18570
18571 if (SYMBOLP (prop))
18572 {
18573 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18574 {
18575 char *unit = SDATA (SYMBOL_NAME (prop));
18576
18577 if (unit[0] == 'i' && unit[1] == 'n')
18578 pixels = 1.0;
18579 else if (unit[0] == 'm' && unit[1] == 'm')
18580 pixels = 25.4;
18581 else if (unit[0] == 'c' && unit[1] == 'm')
18582 pixels = 2.54;
18583 else
18584 pixels = 0;
18585 if (pixels > 0)
18586 {
18587 double ppi;
18588 #ifdef HAVE_WINDOW_SYSTEM
18589 if (FRAME_WINDOW_P (it->f)
18590 && (ppi = (width_p
18591 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18592 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18593 ppi > 0))
18594 return OK_PIXELS (ppi / pixels);
18595 #endif
18596
18597 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18598 || (CONSP (Vdisplay_pixels_per_inch)
18599 && (ppi = (width_p
18600 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18601 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18602 ppi > 0)))
18603 return OK_PIXELS (ppi / pixels);
18604
18605 return 0;
18606 }
18607 }
18608
18609 #ifdef HAVE_WINDOW_SYSTEM
18610 if (EQ (prop, Qheight))
18611 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18612 if (EQ (prop, Qwidth))
18613 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18614 #else
18615 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18616 return OK_PIXELS (1);
18617 #endif
18618
18619 if (EQ (prop, Qtext))
18620 return OK_PIXELS (width_p
18621 ? window_box_width (it->w, TEXT_AREA)
18622 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18623
18624 if (align_to && *align_to < 0)
18625 {
18626 *res = 0;
18627 if (EQ (prop, Qleft))
18628 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18629 if (EQ (prop, Qright))
18630 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18631 if (EQ (prop, Qcenter))
18632 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18633 + window_box_width (it->w, TEXT_AREA) / 2);
18634 if (EQ (prop, Qleft_fringe))
18635 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18636 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18637 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18638 if (EQ (prop, Qright_fringe))
18639 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18640 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18641 : window_box_right_offset (it->w, TEXT_AREA));
18642 if (EQ (prop, Qleft_margin))
18643 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18644 if (EQ (prop, Qright_margin))
18645 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18646 if (EQ (prop, Qscroll_bar))
18647 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18648 ? 0
18649 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18650 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18651 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18652 : 0)));
18653 }
18654 else
18655 {
18656 if (EQ (prop, Qleft_fringe))
18657 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18658 if (EQ (prop, Qright_fringe))
18659 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18660 if (EQ (prop, Qleft_margin))
18661 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18662 if (EQ (prop, Qright_margin))
18663 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18664 if (EQ (prop, Qscroll_bar))
18665 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18666 }
18667
18668 prop = Fbuffer_local_value (prop, it->w->buffer);
18669 }
18670
18671 if (INTEGERP (prop) || FLOATP (prop))
18672 {
18673 int base_unit = (width_p
18674 ? FRAME_COLUMN_WIDTH (it->f)
18675 : FRAME_LINE_HEIGHT (it->f));
18676 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18677 }
18678
18679 if (CONSP (prop))
18680 {
18681 Lisp_Object car = XCAR (prop);
18682 Lisp_Object cdr = XCDR (prop);
18683
18684 if (SYMBOLP (car))
18685 {
18686 #ifdef HAVE_WINDOW_SYSTEM
18687 if (FRAME_WINDOW_P (it->f)
18688 && valid_image_p (prop))
18689 {
18690 int id = lookup_image (it->f, prop);
18691 struct image *img = IMAGE_FROM_ID (it->f, id);
18692
18693 return OK_PIXELS (width_p ? img->width : img->height);
18694 }
18695 #endif
18696 if (EQ (car, Qplus) || EQ (car, Qminus))
18697 {
18698 int first = 1;
18699 double px;
18700
18701 pixels = 0;
18702 while (CONSP (cdr))
18703 {
18704 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18705 font, width_p, align_to))
18706 return 0;
18707 if (first)
18708 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18709 else
18710 pixels += px;
18711 cdr = XCDR (cdr);
18712 }
18713 if (EQ (car, Qminus))
18714 pixels = -pixels;
18715 return OK_PIXELS (pixels);
18716 }
18717
18718 car = Fbuffer_local_value (car, it->w->buffer);
18719 }
18720
18721 if (INTEGERP (car) || FLOATP (car))
18722 {
18723 double fact;
18724 pixels = XFLOATINT (car);
18725 if (NILP (cdr))
18726 return OK_PIXELS (pixels);
18727 if (calc_pixel_width_or_height (&fact, it, cdr,
18728 font, width_p, align_to))
18729 return OK_PIXELS (pixels * fact);
18730 return 0;
18731 }
18732
18733 return 0;
18734 }
18735
18736 return 0;
18737 }
18738
18739 \f
18740 /***********************************************************************
18741 Glyph Display
18742 ***********************************************************************/
18743
18744 #ifdef HAVE_WINDOW_SYSTEM
18745
18746 #if GLYPH_DEBUG
18747
18748 void
18749 dump_glyph_string (s)
18750 struct glyph_string *s;
18751 {
18752 fprintf (stderr, "glyph string\n");
18753 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18754 s->x, s->y, s->width, s->height);
18755 fprintf (stderr, " ybase = %d\n", s->ybase);
18756 fprintf (stderr, " hl = %d\n", s->hl);
18757 fprintf (stderr, " left overhang = %d, right = %d\n",
18758 s->left_overhang, s->right_overhang);
18759 fprintf (stderr, " nchars = %d\n", s->nchars);
18760 fprintf (stderr, " extends to end of line = %d\n",
18761 s->extends_to_end_of_line_p);
18762 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18763 fprintf (stderr, " bg width = %d\n", s->background_width);
18764 }
18765
18766 #endif /* GLYPH_DEBUG */
18767
18768 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18769 of XChar2b structures for S; it can't be allocated in
18770 init_glyph_string because it must be allocated via `alloca'. W
18771 is the window on which S is drawn. ROW and AREA are the glyph row
18772 and area within the row from which S is constructed. START is the
18773 index of the first glyph structure covered by S. HL is a
18774 face-override for drawing S. */
18775
18776 #ifdef HAVE_NTGUI
18777 #define OPTIONAL_HDC(hdc) hdc,
18778 #define DECLARE_HDC(hdc) HDC hdc;
18779 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18780 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18781 #endif
18782
18783 #ifndef OPTIONAL_HDC
18784 #define OPTIONAL_HDC(hdc)
18785 #define DECLARE_HDC(hdc)
18786 #define ALLOCATE_HDC(hdc, f)
18787 #define RELEASE_HDC(hdc, f)
18788 #endif
18789
18790 static void
18791 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18792 struct glyph_string *s;
18793 DECLARE_HDC (hdc)
18794 XChar2b *char2b;
18795 struct window *w;
18796 struct glyph_row *row;
18797 enum glyph_row_area area;
18798 int start;
18799 enum draw_glyphs_face hl;
18800 {
18801 bzero (s, sizeof *s);
18802 s->w = w;
18803 s->f = XFRAME (w->frame);
18804 #ifdef HAVE_NTGUI
18805 s->hdc = hdc;
18806 #endif
18807 s->display = FRAME_X_DISPLAY (s->f);
18808 s->window = FRAME_X_WINDOW (s->f);
18809 s->char2b = char2b;
18810 s->hl = hl;
18811 s->row = row;
18812 s->area = area;
18813 s->first_glyph = row->glyphs[area] + start;
18814 s->height = row->height;
18815 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18816
18817 /* Display the internal border below the tool-bar window. */
18818 if (WINDOWP (s->f->tool_bar_window)
18819 && s->w == XWINDOW (s->f->tool_bar_window))
18820 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18821
18822 s->ybase = s->y + row->ascent;
18823 }
18824
18825
18826 /* Append the list of glyph strings with head H and tail T to the list
18827 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18828
18829 static INLINE void
18830 append_glyph_string_lists (head, tail, h, t)
18831 struct glyph_string **head, **tail;
18832 struct glyph_string *h, *t;
18833 {
18834 if (h)
18835 {
18836 if (*head)
18837 (*tail)->next = h;
18838 else
18839 *head = h;
18840 h->prev = *tail;
18841 *tail = t;
18842 }
18843 }
18844
18845
18846 /* Prepend the list of glyph strings with head H and tail T to the
18847 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18848 result. */
18849
18850 static INLINE void
18851 prepend_glyph_string_lists (head, tail, h, t)
18852 struct glyph_string **head, **tail;
18853 struct glyph_string *h, *t;
18854 {
18855 if (h)
18856 {
18857 if (*head)
18858 (*head)->prev = t;
18859 else
18860 *tail = t;
18861 t->next = *head;
18862 *head = h;
18863 }
18864 }
18865
18866
18867 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18868 Set *HEAD and *TAIL to the resulting list. */
18869
18870 static INLINE void
18871 append_glyph_string (head, tail, s)
18872 struct glyph_string **head, **tail;
18873 struct glyph_string *s;
18874 {
18875 s->next = s->prev = NULL;
18876 append_glyph_string_lists (head, tail, s, s);
18877 }
18878
18879
18880 /* Get face and two-byte form of character glyph GLYPH on frame F.
18881 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18882 a pointer to a realized face that is ready for display. */
18883
18884 static INLINE struct face *
18885 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18886 struct frame *f;
18887 struct glyph *glyph;
18888 XChar2b *char2b;
18889 int *two_byte_p;
18890 {
18891 struct face *face;
18892
18893 xassert (glyph->type == CHAR_GLYPH);
18894 face = FACE_FROM_ID (f, glyph->face_id);
18895
18896 if (two_byte_p)
18897 *two_byte_p = 0;
18898
18899 if (!glyph->multibyte_p)
18900 {
18901 /* Unibyte case. We don't have to encode, but we have to make
18902 sure to use a face suitable for unibyte. */
18903 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18904 }
18905 else if (glyph->u.ch < 128)
18906 {
18907 /* Case of ASCII in a face known to fit ASCII. */
18908 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18909 }
18910 else
18911 {
18912 int c1, c2, charset;
18913
18914 /* Split characters into bytes. If c2 is -1 afterwards, C is
18915 really a one-byte character so that byte1 is zero. */
18916 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18917 if (c2 > 0)
18918 STORE_XCHAR2B (char2b, c1, c2);
18919 else
18920 STORE_XCHAR2B (char2b, 0, c1);
18921
18922 /* Maybe encode the character in *CHAR2B. */
18923 if (charset != CHARSET_ASCII)
18924 {
18925 struct font_info *font_info
18926 = FONT_INFO_FROM_ID (f, face->font_info_id);
18927 if (font_info)
18928 glyph->font_type
18929 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18930 }
18931 }
18932
18933 /* Make sure X resources of the face are allocated. */
18934 xassert (face != NULL);
18935 PREPARE_FACE_FOR_DISPLAY (f, face);
18936 return face;
18937 }
18938
18939
18940 /* Fill glyph string S with composition components specified by S->cmp.
18941
18942 FACES is an array of faces for all components of this composition.
18943 S->gidx is the index of the first component for S.
18944
18945 OVERLAPS non-zero means S should draw the foreground only, and use
18946 its physical height for clipping. See also draw_glyphs.
18947
18948 Value is the index of a component not in S. */
18949
18950 static int
18951 fill_composite_glyph_string (s, faces, overlaps)
18952 struct glyph_string *s;
18953 struct face **faces;
18954 int overlaps;
18955 {
18956 int i;
18957
18958 xassert (s);
18959
18960 s->for_overlaps = overlaps;
18961
18962 s->face = faces[s->gidx];
18963 s->font = s->face->font;
18964 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18965
18966 /* For all glyphs of this composition, starting at the offset
18967 S->gidx, until we reach the end of the definition or encounter a
18968 glyph that requires the different face, add it to S. */
18969 ++s->nchars;
18970 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18971 ++s->nchars;
18972
18973 /* All glyph strings for the same composition has the same width,
18974 i.e. the width set for the first component of the composition. */
18975
18976 s->width = s->first_glyph->pixel_width;
18977
18978 /* If the specified font could not be loaded, use the frame's
18979 default font, but record the fact that we couldn't load it in
18980 the glyph string so that we can draw rectangles for the
18981 characters of the glyph string. */
18982 if (s->font == NULL)
18983 {
18984 s->font_not_found_p = 1;
18985 s->font = FRAME_FONT (s->f);
18986 }
18987
18988 /* Adjust base line for subscript/superscript text. */
18989 s->ybase += s->first_glyph->voffset;
18990
18991 xassert (s->face && s->face->gc);
18992
18993 /* This glyph string must always be drawn with 16-bit functions. */
18994 s->two_byte_p = 1;
18995
18996 return s->gidx + s->nchars;
18997 }
18998
18999
19000 /* Fill glyph string S from a sequence of character glyphs.
19001
19002 FACE_ID is the face id of the string. START is the index of the
19003 first glyph to consider, END is the index of the last + 1.
19004 OVERLAPS non-zero means S should draw the foreground only, and use
19005 its physical height for clipping. See also draw_glyphs.
19006
19007 Value is the index of the first glyph not in S. */
19008
19009 static int
19010 fill_glyph_string (s, face_id, start, end, overlaps)
19011 struct glyph_string *s;
19012 int face_id;
19013 int start, end, overlaps;
19014 {
19015 struct glyph *glyph, *last;
19016 int voffset;
19017 int glyph_not_available_p;
19018
19019 xassert (s->f == XFRAME (s->w->frame));
19020 xassert (s->nchars == 0);
19021 xassert (start >= 0 && end > start);
19022
19023 s->for_overlaps = overlaps,
19024 glyph = s->row->glyphs[s->area] + start;
19025 last = s->row->glyphs[s->area] + end;
19026 voffset = glyph->voffset;
19027
19028 glyph_not_available_p = glyph->glyph_not_available_p;
19029
19030 while (glyph < last
19031 && glyph->type == CHAR_GLYPH
19032 && glyph->voffset == voffset
19033 /* Same face id implies same font, nowadays. */
19034 && glyph->face_id == face_id
19035 && glyph->glyph_not_available_p == glyph_not_available_p)
19036 {
19037 int two_byte_p;
19038
19039 s->face = get_glyph_face_and_encoding (s->f, glyph,
19040 s->char2b + s->nchars,
19041 &two_byte_p);
19042 s->two_byte_p = two_byte_p;
19043 ++s->nchars;
19044 xassert (s->nchars <= end - start);
19045 s->width += glyph->pixel_width;
19046 ++glyph;
19047 }
19048
19049 s->font = s->face->font;
19050 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19051
19052 /* If the specified font could not be loaded, use the frame's font,
19053 but record the fact that we couldn't load it in
19054 S->font_not_found_p so that we can draw rectangles for the
19055 characters of the glyph string. */
19056 if (s->font == NULL || glyph_not_available_p)
19057 {
19058 s->font_not_found_p = 1;
19059 s->font = FRAME_FONT (s->f);
19060 }
19061
19062 /* Adjust base line for subscript/superscript text. */
19063 s->ybase += voffset;
19064
19065 xassert (s->face && s->face->gc);
19066 return glyph - s->row->glyphs[s->area];
19067 }
19068
19069
19070 /* Fill glyph string S from image glyph S->first_glyph. */
19071
19072 static void
19073 fill_image_glyph_string (s)
19074 struct glyph_string *s;
19075 {
19076 xassert (s->first_glyph->type == IMAGE_GLYPH);
19077 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19078 xassert (s->img);
19079 s->slice = s->first_glyph->slice;
19080 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19081 s->font = s->face->font;
19082 s->width = s->first_glyph->pixel_width;
19083
19084 /* Adjust base line for subscript/superscript text. */
19085 s->ybase += s->first_glyph->voffset;
19086 }
19087
19088
19089 /* Fill glyph string S from a sequence of stretch glyphs.
19090
19091 ROW is the glyph row in which the glyphs are found, AREA is the
19092 area within the row. START is the index of the first glyph to
19093 consider, END is the index of the last + 1.
19094
19095 Value is the index of the first glyph not in S. */
19096
19097 static int
19098 fill_stretch_glyph_string (s, row, area, start, end)
19099 struct glyph_string *s;
19100 struct glyph_row *row;
19101 enum glyph_row_area area;
19102 int start, end;
19103 {
19104 struct glyph *glyph, *last;
19105 int voffset, face_id;
19106
19107 xassert (s->first_glyph->type == STRETCH_GLYPH);
19108
19109 glyph = s->row->glyphs[s->area] + start;
19110 last = s->row->glyphs[s->area] + end;
19111 face_id = glyph->face_id;
19112 s->face = FACE_FROM_ID (s->f, face_id);
19113 s->font = s->face->font;
19114 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19115 s->width = glyph->pixel_width;
19116 s->nchars = 1;
19117 voffset = glyph->voffset;
19118
19119 for (++glyph;
19120 (glyph < last
19121 && glyph->type == STRETCH_GLYPH
19122 && glyph->voffset == voffset
19123 && glyph->face_id == face_id);
19124 ++glyph)
19125 s->width += glyph->pixel_width;
19126
19127 /* Adjust base line for subscript/superscript text. */
19128 s->ybase += voffset;
19129
19130 /* The case that face->gc == 0 is handled when drawing the glyph
19131 string by calling PREPARE_FACE_FOR_DISPLAY. */
19132 xassert (s->face);
19133 return glyph - s->row->glyphs[s->area];
19134 }
19135
19136
19137 /* EXPORT for RIF:
19138 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19139 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19140 assumed to be zero. */
19141
19142 void
19143 x_get_glyph_overhangs (glyph, f, left, right)
19144 struct glyph *glyph;
19145 struct frame *f;
19146 int *left, *right;
19147 {
19148 *left = *right = 0;
19149
19150 if (glyph->type == CHAR_GLYPH)
19151 {
19152 XFontStruct *font;
19153 struct face *face;
19154 struct font_info *font_info;
19155 XChar2b char2b;
19156 XCharStruct *pcm;
19157
19158 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19159 font = face->font;
19160 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19161 if (font /* ++KFS: Should this be font_info ? */
19162 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
19163 {
19164 if (pcm->rbearing > pcm->width)
19165 *right = pcm->rbearing - pcm->width;
19166 if (pcm->lbearing < 0)
19167 *left = -pcm->lbearing;
19168 }
19169 }
19170 }
19171
19172
19173 /* Return the index of the first glyph preceding glyph string S that
19174 is overwritten by S because of S's left overhang. Value is -1
19175 if no glyphs are overwritten. */
19176
19177 static int
19178 left_overwritten (s)
19179 struct glyph_string *s;
19180 {
19181 int k;
19182
19183 if (s->left_overhang)
19184 {
19185 int x = 0, i;
19186 struct glyph *glyphs = s->row->glyphs[s->area];
19187 int first = s->first_glyph - glyphs;
19188
19189 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19190 x -= glyphs[i].pixel_width;
19191
19192 k = i + 1;
19193 }
19194 else
19195 k = -1;
19196
19197 return k;
19198 }
19199
19200
19201 /* Return the index of the first glyph preceding glyph string S that
19202 is overwriting S because of its right overhang. Value is -1 if no
19203 glyph in front of S overwrites S. */
19204
19205 static int
19206 left_overwriting (s)
19207 struct glyph_string *s;
19208 {
19209 int i, k, x;
19210 struct glyph *glyphs = s->row->glyphs[s->area];
19211 int first = s->first_glyph - glyphs;
19212
19213 k = -1;
19214 x = 0;
19215 for (i = first - 1; i >= 0; --i)
19216 {
19217 int left, right;
19218 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19219 if (x + right > 0)
19220 k = i;
19221 x -= glyphs[i].pixel_width;
19222 }
19223
19224 return k;
19225 }
19226
19227
19228 /* Return the index of the last glyph following glyph string S that is
19229 not overwritten by S because of S's right overhang. Value is -1 if
19230 no such glyph is found. */
19231
19232 static int
19233 right_overwritten (s)
19234 struct glyph_string *s;
19235 {
19236 int k = -1;
19237
19238 if (s->right_overhang)
19239 {
19240 int x = 0, i;
19241 struct glyph *glyphs = s->row->glyphs[s->area];
19242 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19243 int end = s->row->used[s->area];
19244
19245 for (i = first; i < end && s->right_overhang > x; ++i)
19246 x += glyphs[i].pixel_width;
19247
19248 k = i;
19249 }
19250
19251 return k;
19252 }
19253
19254
19255 /* Return the index of the last glyph following glyph string S that
19256 overwrites S because of its left overhang. Value is negative
19257 if no such glyph is found. */
19258
19259 static int
19260 right_overwriting (s)
19261 struct glyph_string *s;
19262 {
19263 int i, k, x;
19264 int end = s->row->used[s->area];
19265 struct glyph *glyphs = s->row->glyphs[s->area];
19266 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19267
19268 k = -1;
19269 x = 0;
19270 for (i = first; i < end; ++i)
19271 {
19272 int left, right;
19273 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19274 if (x - left < 0)
19275 k = i;
19276 x += glyphs[i].pixel_width;
19277 }
19278
19279 return k;
19280 }
19281
19282
19283 /* Get face and two-byte form of character C in face FACE_ID on frame
19284 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19285 means we want to display multibyte text. DISPLAY_P non-zero means
19286 make sure that X resources for the face returned are allocated.
19287 Value is a pointer to a realized face that is ready for display if
19288 DISPLAY_P is non-zero. */
19289
19290 static INLINE struct face *
19291 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19292 struct frame *f;
19293 int c, face_id;
19294 XChar2b *char2b;
19295 int multibyte_p, display_p;
19296 {
19297 struct face *face = FACE_FROM_ID (f, face_id);
19298
19299 if (!multibyte_p)
19300 {
19301 /* Unibyte case. We don't have to encode, but we have to make
19302 sure to use a face suitable for unibyte. */
19303 STORE_XCHAR2B (char2b, 0, c);
19304 face_id = FACE_FOR_CHAR (f, face, c);
19305 face = FACE_FROM_ID (f, face_id);
19306 }
19307 else if (c < 128)
19308 {
19309 /* Case of ASCII in a face known to fit ASCII. */
19310 STORE_XCHAR2B (char2b, 0, c);
19311 }
19312 else
19313 {
19314 int c1, c2, charset;
19315
19316 /* Split characters into bytes. If c2 is -1 afterwards, C is
19317 really a one-byte character so that byte1 is zero. */
19318 SPLIT_CHAR (c, charset, c1, c2);
19319 if (c2 > 0)
19320 STORE_XCHAR2B (char2b, c1, c2);
19321 else
19322 STORE_XCHAR2B (char2b, 0, c1);
19323
19324 /* Maybe encode the character in *CHAR2B. */
19325 if (face->font != NULL)
19326 {
19327 struct font_info *font_info
19328 = FONT_INFO_FROM_ID (f, face->font_info_id);
19329 if (font_info)
19330 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
19331 }
19332 }
19333
19334 /* Make sure X resources of the face are allocated. */
19335 #ifdef HAVE_X_WINDOWS
19336 if (display_p)
19337 #endif
19338 {
19339 xassert (face != NULL);
19340 PREPARE_FACE_FOR_DISPLAY (f, face);
19341 }
19342
19343 return face;
19344 }
19345
19346
19347 /* Set background width of glyph string S. START is the index of the
19348 first glyph following S. LAST_X is the right-most x-position + 1
19349 in the drawing area. */
19350
19351 static INLINE void
19352 set_glyph_string_background_width (s, start, last_x)
19353 struct glyph_string *s;
19354 int start;
19355 int last_x;
19356 {
19357 /* If the face of this glyph string has to be drawn to the end of
19358 the drawing area, set S->extends_to_end_of_line_p. */
19359
19360 if (start == s->row->used[s->area]
19361 && s->area == TEXT_AREA
19362 && ((s->row->fill_line_p
19363 && (s->hl == DRAW_NORMAL_TEXT
19364 || s->hl == DRAW_IMAGE_RAISED
19365 || s->hl == DRAW_IMAGE_SUNKEN))
19366 || s->hl == DRAW_MOUSE_FACE))
19367 s->extends_to_end_of_line_p = 1;
19368
19369 /* If S extends its face to the end of the line, set its
19370 background_width to the distance to the right edge of the drawing
19371 area. */
19372 if (s->extends_to_end_of_line_p)
19373 s->background_width = last_x - s->x + 1;
19374 else
19375 s->background_width = s->width;
19376 }
19377
19378
19379 /* Compute overhangs and x-positions for glyph string S and its
19380 predecessors, or successors. X is the starting x-position for S.
19381 BACKWARD_P non-zero means process predecessors. */
19382
19383 static void
19384 compute_overhangs_and_x (s, x, backward_p)
19385 struct glyph_string *s;
19386 int x;
19387 int backward_p;
19388 {
19389 if (backward_p)
19390 {
19391 while (s)
19392 {
19393 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19394 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19395 x -= s->width;
19396 s->x = x;
19397 s = s->prev;
19398 }
19399 }
19400 else
19401 {
19402 while (s)
19403 {
19404 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19405 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19406 s->x = x;
19407 x += s->width;
19408 s = s->next;
19409 }
19410 }
19411 }
19412
19413
19414
19415 /* The following macros are only called from draw_glyphs below.
19416 They reference the following parameters of that function directly:
19417 `w', `row', `area', and `overlap_p'
19418 as well as the following local variables:
19419 `s', `f', and `hdc' (in W32) */
19420
19421 #ifdef HAVE_NTGUI
19422 /* On W32, silently add local `hdc' variable to argument list of
19423 init_glyph_string. */
19424 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19425 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19426 #else
19427 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19428 init_glyph_string (s, char2b, w, row, area, start, hl)
19429 #endif
19430
19431 /* Add a glyph string for a stretch glyph to the list of strings
19432 between HEAD and TAIL. START is the index of the stretch glyph in
19433 row area AREA of glyph row ROW. END is the index of the last glyph
19434 in that glyph row area. X is the current output position assigned
19435 to the new glyph string constructed. HL overrides that face of the
19436 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19437 is the right-most x-position of the drawing area. */
19438
19439 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19440 and below -- keep them on one line. */
19441 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19442 do \
19443 { \
19444 s = (struct glyph_string *) alloca (sizeof *s); \
19445 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19446 START = fill_stretch_glyph_string (s, row, area, START, END); \
19447 append_glyph_string (&HEAD, &TAIL, s); \
19448 s->x = (X); \
19449 } \
19450 while (0)
19451
19452
19453 /* Add a glyph string for an image glyph to the list of strings
19454 between HEAD and TAIL. START is the index of the image glyph in
19455 row area AREA of glyph row ROW. END is the index of the last glyph
19456 in that glyph row area. X is the current output position assigned
19457 to the new glyph string constructed. HL overrides that face of the
19458 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19459 is the right-most x-position of the drawing area. */
19460
19461 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19462 do \
19463 { \
19464 s = (struct glyph_string *) alloca (sizeof *s); \
19465 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19466 fill_image_glyph_string (s); \
19467 append_glyph_string (&HEAD, &TAIL, s); \
19468 ++START; \
19469 s->x = (X); \
19470 } \
19471 while (0)
19472
19473
19474 /* Add a glyph string for a sequence of character glyphs to the list
19475 of strings between HEAD and TAIL. START is the index of the first
19476 glyph in row area AREA of glyph row ROW that is part of the new
19477 glyph string. END is the index of the last glyph in that glyph row
19478 area. X is the current output position assigned to the new glyph
19479 string constructed. HL overrides that face of the glyph; e.g. it
19480 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19481 right-most x-position of the drawing area. */
19482
19483 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19484 do \
19485 { \
19486 int c, face_id; \
19487 XChar2b *char2b; \
19488 \
19489 c = (row)->glyphs[area][START].u.ch; \
19490 face_id = (row)->glyphs[area][START].face_id; \
19491 \
19492 s = (struct glyph_string *) alloca (sizeof *s); \
19493 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19494 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19495 append_glyph_string (&HEAD, &TAIL, s); \
19496 s->x = (X); \
19497 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19498 } \
19499 while (0)
19500
19501
19502 /* Add a glyph string for a composite sequence to the list of strings
19503 between HEAD and TAIL. START is the index of the first glyph in
19504 row area AREA of glyph row ROW that is part of the new glyph
19505 string. END is the index of the last glyph in that glyph row area.
19506 X is the current output position assigned to the new glyph string
19507 constructed. HL overrides that face of the glyph; e.g. it is
19508 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19509 x-position of the drawing area. */
19510
19511 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19512 do { \
19513 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19514 int face_id = (row)->glyphs[area][START].face_id; \
19515 struct face *base_face = FACE_FROM_ID (f, face_id); \
19516 struct composition *cmp = composition_table[cmp_id]; \
19517 int glyph_len = cmp->glyph_len; \
19518 XChar2b *char2b; \
19519 struct face **faces; \
19520 struct glyph_string *first_s = NULL; \
19521 int n; \
19522 \
19523 base_face = base_face->ascii_face; \
19524 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19525 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19526 /* At first, fill in `char2b' and `faces'. */ \
19527 for (n = 0; n < glyph_len; n++) \
19528 { \
19529 int c = COMPOSITION_GLYPH (cmp, n); \
19530 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19531 faces[n] = FACE_FROM_ID (f, this_face_id); \
19532 get_char_face_and_encoding (f, c, this_face_id, \
19533 char2b + n, 1, 1); \
19534 } \
19535 \
19536 /* Make glyph_strings for each glyph sequence that is drawable by \
19537 the same face, and append them to HEAD/TAIL. */ \
19538 for (n = 0; n < cmp->glyph_len;) \
19539 { \
19540 s = (struct glyph_string *) alloca (sizeof *s); \
19541 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19542 append_glyph_string (&(HEAD), &(TAIL), s); \
19543 s->cmp = cmp; \
19544 s->gidx = n; \
19545 s->x = (X); \
19546 \
19547 if (n == 0) \
19548 first_s = s; \
19549 \
19550 n = fill_composite_glyph_string (s, faces, overlaps); \
19551 } \
19552 \
19553 ++START; \
19554 s = first_s; \
19555 } while (0)
19556
19557
19558 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19559 of AREA of glyph row ROW on window W between indices START and END.
19560 HL overrides the face for drawing glyph strings, e.g. it is
19561 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19562 x-positions of the drawing area.
19563
19564 This is an ugly monster macro construct because we must use alloca
19565 to allocate glyph strings (because draw_glyphs can be called
19566 asynchronously). */
19567
19568 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19569 do \
19570 { \
19571 HEAD = TAIL = NULL; \
19572 while (START < END) \
19573 { \
19574 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19575 switch (first_glyph->type) \
19576 { \
19577 case CHAR_GLYPH: \
19578 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19579 HL, X, LAST_X); \
19580 break; \
19581 \
19582 case COMPOSITE_GLYPH: \
19583 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19584 HL, X, LAST_X); \
19585 break; \
19586 \
19587 case STRETCH_GLYPH: \
19588 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19589 HL, X, LAST_X); \
19590 break; \
19591 \
19592 case IMAGE_GLYPH: \
19593 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19594 HL, X, LAST_X); \
19595 break; \
19596 \
19597 default: \
19598 abort (); \
19599 } \
19600 \
19601 set_glyph_string_background_width (s, START, LAST_X); \
19602 (X) += s->width; \
19603 } \
19604 } \
19605 while (0)
19606
19607
19608 /* Draw glyphs between START and END in AREA of ROW on window W,
19609 starting at x-position X. X is relative to AREA in W. HL is a
19610 face-override with the following meaning:
19611
19612 DRAW_NORMAL_TEXT draw normally
19613 DRAW_CURSOR draw in cursor face
19614 DRAW_MOUSE_FACE draw in mouse face.
19615 DRAW_INVERSE_VIDEO draw in mode line face
19616 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19617 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19618
19619 If OVERLAPS is non-zero, draw only the foreground of characters and
19620 clip to the physical height of ROW. Non-zero value also defines
19621 the overlapping part to be drawn:
19622
19623 OVERLAPS_PRED overlap with preceding rows
19624 OVERLAPS_SUCC overlap with succeeding rows
19625 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19626 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19627
19628 Value is the x-position reached, relative to AREA of W. */
19629
19630 static int
19631 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19632 struct window *w;
19633 int x;
19634 struct glyph_row *row;
19635 enum glyph_row_area area;
19636 int start, end;
19637 enum draw_glyphs_face hl;
19638 int overlaps;
19639 {
19640 struct glyph_string *head, *tail;
19641 struct glyph_string *s;
19642 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19643 int last_x, area_width;
19644 int x_reached;
19645 int i, j;
19646 struct frame *f = XFRAME (WINDOW_FRAME (w));
19647 DECLARE_HDC (hdc);
19648
19649 ALLOCATE_HDC (hdc, f);
19650
19651 /* Let's rather be paranoid than getting a SEGV. */
19652 end = min (end, row->used[area]);
19653 start = max (0, start);
19654 start = min (end, start);
19655
19656 /* Translate X to frame coordinates. Set last_x to the right
19657 end of the drawing area. */
19658 if (row->full_width_p)
19659 {
19660 /* X is relative to the left edge of W, without scroll bars
19661 or fringes. */
19662 x += WINDOW_LEFT_EDGE_X (w);
19663 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19664 }
19665 else
19666 {
19667 int area_left = window_box_left (w, area);
19668 x += area_left;
19669 area_width = window_box_width (w, area);
19670 last_x = area_left + area_width;
19671 }
19672
19673 /* Build a doubly-linked list of glyph_string structures between
19674 head and tail from what we have to draw. Note that the macro
19675 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19676 the reason we use a separate variable `i'. */
19677 i = start;
19678 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19679 if (tail)
19680 x_reached = tail->x + tail->background_width;
19681 else
19682 x_reached = x;
19683
19684 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19685 the row, redraw some glyphs in front or following the glyph
19686 strings built above. */
19687 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19688 {
19689 int dummy_x = 0;
19690 struct glyph_string *h, *t;
19691
19692 /* Compute overhangs for all glyph strings. */
19693 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19694 for (s = head; s; s = s->next)
19695 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19696
19697 /* Prepend glyph strings for glyphs in front of the first glyph
19698 string that are overwritten because of the first glyph
19699 string's left overhang. The background of all strings
19700 prepended must be drawn because the first glyph string
19701 draws over it. */
19702 i = left_overwritten (head);
19703 if (i >= 0)
19704 {
19705 j = i;
19706 BUILD_GLYPH_STRINGS (j, start, h, t,
19707 DRAW_NORMAL_TEXT, dummy_x, last_x);
19708 start = i;
19709 compute_overhangs_and_x (t, head->x, 1);
19710 prepend_glyph_string_lists (&head, &tail, h, t);
19711 clip_head = head;
19712 }
19713
19714 /* Prepend glyph strings for glyphs in front of the first glyph
19715 string that overwrite that glyph string because of their
19716 right overhang. For these strings, only the foreground must
19717 be drawn, because it draws over the glyph string at `head'.
19718 The background must not be drawn because this would overwrite
19719 right overhangs of preceding glyphs for which no glyph
19720 strings exist. */
19721 i = left_overwriting (head);
19722 if (i >= 0)
19723 {
19724 clip_head = head;
19725 BUILD_GLYPH_STRINGS (i, start, h, t,
19726 DRAW_NORMAL_TEXT, dummy_x, last_x);
19727 for (s = h; s; s = s->next)
19728 s->background_filled_p = 1;
19729 compute_overhangs_and_x (t, head->x, 1);
19730 prepend_glyph_string_lists (&head, &tail, h, t);
19731 }
19732
19733 /* Append glyphs strings for glyphs following the last glyph
19734 string tail that are overwritten by tail. The background of
19735 these strings has to be drawn because tail's foreground draws
19736 over it. */
19737 i = right_overwritten (tail);
19738 if (i >= 0)
19739 {
19740 BUILD_GLYPH_STRINGS (end, i, h, t,
19741 DRAW_NORMAL_TEXT, x, last_x);
19742 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19743 append_glyph_string_lists (&head, &tail, h, t);
19744 clip_tail = tail;
19745 }
19746
19747 /* Append glyph strings for glyphs following the last glyph
19748 string tail that overwrite tail. The foreground of such
19749 glyphs has to be drawn because it writes into the background
19750 of tail. The background must not be drawn because it could
19751 paint over the foreground of following glyphs. */
19752 i = right_overwriting (tail);
19753 if (i >= 0)
19754 {
19755 clip_tail = tail;
19756 BUILD_GLYPH_STRINGS (end, i, h, t,
19757 DRAW_NORMAL_TEXT, x, last_x);
19758 for (s = h; s; s = s->next)
19759 s->background_filled_p = 1;
19760 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19761 append_glyph_string_lists (&head, &tail, h, t);
19762 }
19763 if (clip_head || clip_tail)
19764 for (s = head; s; s = s->next)
19765 {
19766 s->clip_head = clip_head;
19767 s->clip_tail = clip_tail;
19768 }
19769 }
19770
19771 /* Draw all strings. */
19772 for (s = head; s; s = s->next)
19773 FRAME_RIF (f)->draw_glyph_string (s);
19774
19775 if (area == TEXT_AREA
19776 && !row->full_width_p
19777 /* When drawing overlapping rows, only the glyph strings'
19778 foreground is drawn, which doesn't erase a cursor
19779 completely. */
19780 && !overlaps)
19781 {
19782 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19783 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19784 : (tail ? tail->x + tail->background_width : x));
19785
19786 int text_left = window_box_left (w, TEXT_AREA);
19787 x0 -= text_left;
19788 x1 -= text_left;
19789
19790 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19791 row->y, MATRIX_ROW_BOTTOM_Y (row));
19792 }
19793
19794 /* Value is the x-position up to which drawn, relative to AREA of W.
19795 This doesn't include parts drawn because of overhangs. */
19796 if (row->full_width_p)
19797 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19798 else
19799 x_reached -= window_box_left (w, area);
19800
19801 RELEASE_HDC (hdc, f);
19802
19803 return x_reached;
19804 }
19805
19806 /* Expand row matrix if too narrow. Don't expand if area
19807 is not present. */
19808
19809 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19810 { \
19811 if (!fonts_changed_p \
19812 && (it->glyph_row->glyphs[area] \
19813 < it->glyph_row->glyphs[area + 1])) \
19814 { \
19815 it->w->ncols_scale_factor++; \
19816 fonts_changed_p = 1; \
19817 } \
19818 }
19819
19820 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19821 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19822
19823 static INLINE void
19824 append_glyph (it)
19825 struct it *it;
19826 {
19827 struct glyph *glyph;
19828 enum glyph_row_area area = it->area;
19829
19830 xassert (it->glyph_row);
19831 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19832
19833 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19834 if (glyph < it->glyph_row->glyphs[area + 1])
19835 {
19836 glyph->charpos = CHARPOS (it->position);
19837 glyph->object = it->object;
19838 glyph->pixel_width = it->pixel_width;
19839 glyph->ascent = it->ascent;
19840 glyph->descent = it->descent;
19841 glyph->voffset = it->voffset;
19842 glyph->type = CHAR_GLYPH;
19843 glyph->multibyte_p = it->multibyte_p;
19844 glyph->left_box_line_p = it->start_of_box_run_p;
19845 glyph->right_box_line_p = it->end_of_box_run_p;
19846 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19847 || it->phys_descent > it->descent);
19848 glyph->padding_p = 0;
19849 glyph->glyph_not_available_p = it->glyph_not_available_p;
19850 glyph->face_id = it->face_id;
19851 glyph->u.ch = it->char_to_display;
19852 glyph->slice = null_glyph_slice;
19853 glyph->font_type = FONT_TYPE_UNKNOWN;
19854 ++it->glyph_row->used[area];
19855 }
19856 else
19857 IT_EXPAND_MATRIX_WIDTH (it, area);
19858 }
19859
19860 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19861 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19862
19863 static INLINE void
19864 append_composite_glyph (it)
19865 struct it *it;
19866 {
19867 struct glyph *glyph;
19868 enum glyph_row_area area = it->area;
19869
19870 xassert (it->glyph_row);
19871
19872 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19873 if (glyph < it->glyph_row->glyphs[area + 1])
19874 {
19875 glyph->charpos = CHARPOS (it->position);
19876 glyph->object = it->object;
19877 glyph->pixel_width = it->pixel_width;
19878 glyph->ascent = it->ascent;
19879 glyph->descent = it->descent;
19880 glyph->voffset = it->voffset;
19881 glyph->type = COMPOSITE_GLYPH;
19882 glyph->multibyte_p = it->multibyte_p;
19883 glyph->left_box_line_p = it->start_of_box_run_p;
19884 glyph->right_box_line_p = it->end_of_box_run_p;
19885 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19886 || it->phys_descent > it->descent);
19887 glyph->padding_p = 0;
19888 glyph->glyph_not_available_p = 0;
19889 glyph->face_id = it->face_id;
19890 glyph->u.cmp_id = it->cmp_id;
19891 glyph->slice = null_glyph_slice;
19892 glyph->font_type = FONT_TYPE_UNKNOWN;
19893 ++it->glyph_row->used[area];
19894 }
19895 else
19896 IT_EXPAND_MATRIX_WIDTH (it, area);
19897 }
19898
19899
19900 /* Change IT->ascent and IT->height according to the setting of
19901 IT->voffset. */
19902
19903 static INLINE void
19904 take_vertical_position_into_account (it)
19905 struct it *it;
19906 {
19907 if (it->voffset)
19908 {
19909 if (it->voffset < 0)
19910 /* Increase the ascent so that we can display the text higher
19911 in the line. */
19912 it->ascent -= it->voffset;
19913 else
19914 /* Increase the descent so that we can display the text lower
19915 in the line. */
19916 it->descent += it->voffset;
19917 }
19918 }
19919
19920
19921 /* Produce glyphs/get display metrics for the image IT is loaded with.
19922 See the description of struct display_iterator in dispextern.h for
19923 an overview of struct display_iterator. */
19924
19925 static void
19926 produce_image_glyph (it)
19927 struct it *it;
19928 {
19929 struct image *img;
19930 struct face *face;
19931 int glyph_ascent, crop;
19932 struct glyph_slice slice;
19933
19934 xassert (it->what == IT_IMAGE);
19935
19936 face = FACE_FROM_ID (it->f, it->face_id);
19937 xassert (face);
19938 /* Make sure X resources of the face is loaded. */
19939 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19940
19941 if (it->image_id < 0)
19942 {
19943 /* Fringe bitmap. */
19944 it->ascent = it->phys_ascent = 0;
19945 it->descent = it->phys_descent = 0;
19946 it->pixel_width = 0;
19947 it->nglyphs = 0;
19948 return;
19949 }
19950
19951 img = IMAGE_FROM_ID (it->f, it->image_id);
19952 xassert (img);
19953 /* Make sure X resources of the image is loaded. */
19954 prepare_image_for_display (it->f, img);
19955
19956 slice.x = slice.y = 0;
19957 slice.width = img->width;
19958 slice.height = img->height;
19959
19960 if (INTEGERP (it->slice.x))
19961 slice.x = XINT (it->slice.x);
19962 else if (FLOATP (it->slice.x))
19963 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19964
19965 if (INTEGERP (it->slice.y))
19966 slice.y = XINT (it->slice.y);
19967 else if (FLOATP (it->slice.y))
19968 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19969
19970 if (INTEGERP (it->slice.width))
19971 slice.width = XINT (it->slice.width);
19972 else if (FLOATP (it->slice.width))
19973 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19974
19975 if (INTEGERP (it->slice.height))
19976 slice.height = XINT (it->slice.height);
19977 else if (FLOATP (it->slice.height))
19978 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19979
19980 if (slice.x >= img->width)
19981 slice.x = img->width;
19982 if (slice.y >= img->height)
19983 slice.y = img->height;
19984 if (slice.x + slice.width >= img->width)
19985 slice.width = img->width - slice.x;
19986 if (slice.y + slice.height > img->height)
19987 slice.height = img->height - slice.y;
19988
19989 if (slice.width == 0 || slice.height == 0)
19990 return;
19991
19992 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19993
19994 it->descent = slice.height - glyph_ascent;
19995 if (slice.y == 0)
19996 it->descent += img->vmargin;
19997 if (slice.y + slice.height == img->height)
19998 it->descent += img->vmargin;
19999 it->phys_descent = it->descent;
20000
20001 it->pixel_width = slice.width;
20002 if (slice.x == 0)
20003 it->pixel_width += img->hmargin;
20004 if (slice.x + slice.width == img->width)
20005 it->pixel_width += img->hmargin;
20006
20007 /* It's quite possible for images to have an ascent greater than
20008 their height, so don't get confused in that case. */
20009 if (it->descent < 0)
20010 it->descent = 0;
20011
20012 #if 0 /* this breaks image tiling */
20013 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20014 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20015 if (face_ascent > it->ascent)
20016 it->ascent = it->phys_ascent = face_ascent;
20017 #endif
20018
20019 it->nglyphs = 1;
20020
20021 if (face->box != FACE_NO_BOX)
20022 {
20023 if (face->box_line_width > 0)
20024 {
20025 if (slice.y == 0)
20026 it->ascent += face->box_line_width;
20027 if (slice.y + slice.height == img->height)
20028 it->descent += face->box_line_width;
20029 }
20030
20031 if (it->start_of_box_run_p && slice.x == 0)
20032 it->pixel_width += eabs (face->box_line_width);
20033 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20034 it->pixel_width += eabs (face->box_line_width);
20035 }
20036
20037 take_vertical_position_into_account (it);
20038
20039 /* Automatically crop wide image glyphs at right edge so we can
20040 draw the cursor on same display row. */
20041 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20042 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20043 {
20044 it->pixel_width -= crop;
20045 slice.width -= crop;
20046 }
20047
20048 if (it->glyph_row)
20049 {
20050 struct glyph *glyph;
20051 enum glyph_row_area area = it->area;
20052
20053 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20054 if (glyph < it->glyph_row->glyphs[area + 1])
20055 {
20056 glyph->charpos = CHARPOS (it->position);
20057 glyph->object = it->object;
20058 glyph->pixel_width = it->pixel_width;
20059 glyph->ascent = glyph_ascent;
20060 glyph->descent = it->descent;
20061 glyph->voffset = it->voffset;
20062 glyph->type = IMAGE_GLYPH;
20063 glyph->multibyte_p = it->multibyte_p;
20064 glyph->left_box_line_p = it->start_of_box_run_p;
20065 glyph->right_box_line_p = it->end_of_box_run_p;
20066 glyph->overlaps_vertically_p = 0;
20067 glyph->padding_p = 0;
20068 glyph->glyph_not_available_p = 0;
20069 glyph->face_id = it->face_id;
20070 glyph->u.img_id = img->id;
20071 glyph->slice = slice;
20072 glyph->font_type = FONT_TYPE_UNKNOWN;
20073 ++it->glyph_row->used[area];
20074 }
20075 else
20076 IT_EXPAND_MATRIX_WIDTH (it, area);
20077 }
20078 }
20079
20080
20081 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20082 of the glyph, WIDTH and HEIGHT are the width and height of the
20083 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20084
20085 static void
20086 append_stretch_glyph (it, object, width, height, ascent)
20087 struct it *it;
20088 Lisp_Object object;
20089 int width, height;
20090 int ascent;
20091 {
20092 struct glyph *glyph;
20093 enum glyph_row_area area = it->area;
20094
20095 xassert (ascent >= 0 && ascent <= height);
20096
20097 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20098 if (glyph < it->glyph_row->glyphs[area + 1])
20099 {
20100 glyph->charpos = CHARPOS (it->position);
20101 glyph->object = object;
20102 glyph->pixel_width = width;
20103 glyph->ascent = ascent;
20104 glyph->descent = height - ascent;
20105 glyph->voffset = it->voffset;
20106 glyph->type = STRETCH_GLYPH;
20107 glyph->multibyte_p = it->multibyte_p;
20108 glyph->left_box_line_p = it->start_of_box_run_p;
20109 glyph->right_box_line_p = it->end_of_box_run_p;
20110 glyph->overlaps_vertically_p = 0;
20111 glyph->padding_p = 0;
20112 glyph->glyph_not_available_p = 0;
20113 glyph->face_id = it->face_id;
20114 glyph->u.stretch.ascent = ascent;
20115 glyph->u.stretch.height = height;
20116 glyph->slice = null_glyph_slice;
20117 glyph->font_type = FONT_TYPE_UNKNOWN;
20118 ++it->glyph_row->used[area];
20119 }
20120 else
20121 IT_EXPAND_MATRIX_WIDTH (it, area);
20122 }
20123
20124
20125 /* Produce a stretch glyph for iterator IT. IT->object is the value
20126 of the glyph property displayed. The value must be a list
20127 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20128 being recognized:
20129
20130 1. `:width WIDTH' specifies that the space should be WIDTH *
20131 canonical char width wide. WIDTH may be an integer or floating
20132 point number.
20133
20134 2. `:relative-width FACTOR' specifies that the width of the stretch
20135 should be computed from the width of the first character having the
20136 `glyph' property, and should be FACTOR times that width.
20137
20138 3. `:align-to HPOS' specifies that the space should be wide enough
20139 to reach HPOS, a value in canonical character units.
20140
20141 Exactly one of the above pairs must be present.
20142
20143 4. `:height HEIGHT' specifies that the height of the stretch produced
20144 should be HEIGHT, measured in canonical character units.
20145
20146 5. `:relative-height FACTOR' specifies that the height of the
20147 stretch should be FACTOR times the height of the characters having
20148 the glyph property.
20149
20150 Either none or exactly one of 4 or 5 must be present.
20151
20152 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20153 of the stretch should be used for the ascent of the stretch.
20154 ASCENT must be in the range 0 <= ASCENT <= 100. */
20155
20156 static void
20157 produce_stretch_glyph (it)
20158 struct it *it;
20159 {
20160 /* (space :width WIDTH :height HEIGHT ...) */
20161 Lisp_Object prop, plist;
20162 int width = 0, height = 0, align_to = -1;
20163 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20164 int ascent = 0;
20165 double tem;
20166 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20167 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20168
20169 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20170
20171 /* List should start with `space'. */
20172 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20173 plist = XCDR (it->object);
20174
20175 /* Compute the width of the stretch. */
20176 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20177 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20178 {
20179 /* Absolute width `:width WIDTH' specified and valid. */
20180 zero_width_ok_p = 1;
20181 width = (int)tem;
20182 }
20183 else if (prop = Fplist_get (plist, QCrelative_width),
20184 NUMVAL (prop) > 0)
20185 {
20186 /* Relative width `:relative-width FACTOR' specified and valid.
20187 Compute the width of the characters having the `glyph'
20188 property. */
20189 struct it it2;
20190 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20191
20192 it2 = *it;
20193 if (it->multibyte_p)
20194 {
20195 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20196 - IT_BYTEPOS (*it));
20197 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20198 }
20199 else
20200 it2.c = *p, it2.len = 1;
20201
20202 it2.glyph_row = NULL;
20203 it2.what = IT_CHARACTER;
20204 x_produce_glyphs (&it2);
20205 width = NUMVAL (prop) * it2.pixel_width;
20206 }
20207 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20208 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20209 {
20210 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20211 align_to = (align_to < 0
20212 ? 0
20213 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20214 else if (align_to < 0)
20215 align_to = window_box_left_offset (it->w, TEXT_AREA);
20216 width = max (0, (int)tem + align_to - it->current_x);
20217 zero_width_ok_p = 1;
20218 }
20219 else
20220 /* Nothing specified -> width defaults to canonical char width. */
20221 width = FRAME_COLUMN_WIDTH (it->f);
20222
20223 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20224 width = 1;
20225
20226 /* Compute height. */
20227 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20228 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20229 {
20230 height = (int)tem;
20231 zero_height_ok_p = 1;
20232 }
20233 else if (prop = Fplist_get (plist, QCrelative_height),
20234 NUMVAL (prop) > 0)
20235 height = FONT_HEIGHT (font) * NUMVAL (prop);
20236 else
20237 height = FONT_HEIGHT (font);
20238
20239 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20240 height = 1;
20241
20242 /* Compute percentage of height used for ascent. If
20243 `:ascent ASCENT' is present and valid, use that. Otherwise,
20244 derive the ascent from the font in use. */
20245 if (prop = Fplist_get (plist, QCascent),
20246 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20247 ascent = height * NUMVAL (prop) / 100.0;
20248 else if (!NILP (prop)
20249 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20250 ascent = min (max (0, (int)tem), height);
20251 else
20252 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20253
20254 if (width > 0 && !it->truncate_lines_p
20255 && it->current_x + width > it->last_visible_x)
20256 width = it->last_visible_x - it->current_x - 1;
20257
20258 if (width > 0 && height > 0 && it->glyph_row)
20259 {
20260 Lisp_Object object = it->stack[it->sp - 1].string;
20261 if (!STRINGP (object))
20262 object = it->w->buffer;
20263 append_stretch_glyph (it, object, width, height, ascent);
20264 }
20265
20266 it->pixel_width = width;
20267 it->ascent = it->phys_ascent = ascent;
20268 it->descent = it->phys_descent = height - it->ascent;
20269 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20270
20271 take_vertical_position_into_account (it);
20272 }
20273
20274 /* Get line-height and line-spacing property at point.
20275 If line-height has format (HEIGHT TOTAL), return TOTAL
20276 in TOTAL_HEIGHT. */
20277
20278 static Lisp_Object
20279 get_line_height_property (it, prop)
20280 struct it *it;
20281 Lisp_Object prop;
20282 {
20283 Lisp_Object position;
20284
20285 if (STRINGP (it->object))
20286 position = make_number (IT_STRING_CHARPOS (*it));
20287 else if (BUFFERP (it->object))
20288 position = make_number (IT_CHARPOS (*it));
20289 else
20290 return Qnil;
20291
20292 return Fget_char_property (position, prop, it->object);
20293 }
20294
20295 /* Calculate line-height and line-spacing properties.
20296 An integer value specifies explicit pixel value.
20297 A float value specifies relative value to current face height.
20298 A cons (float . face-name) specifies relative value to
20299 height of specified face font.
20300
20301 Returns height in pixels, or nil. */
20302
20303
20304 static Lisp_Object
20305 calc_line_height_property (it, val, font, boff, override)
20306 struct it *it;
20307 Lisp_Object val;
20308 XFontStruct *font;
20309 int boff, override;
20310 {
20311 Lisp_Object face_name = Qnil;
20312 int ascent, descent, height;
20313
20314 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20315 return val;
20316
20317 if (CONSP (val))
20318 {
20319 face_name = XCAR (val);
20320 val = XCDR (val);
20321 if (!NUMBERP (val))
20322 val = make_number (1);
20323 if (NILP (face_name))
20324 {
20325 height = it->ascent + it->descent;
20326 goto scale;
20327 }
20328 }
20329
20330 if (NILP (face_name))
20331 {
20332 font = FRAME_FONT (it->f);
20333 boff = FRAME_BASELINE_OFFSET (it->f);
20334 }
20335 else if (EQ (face_name, Qt))
20336 {
20337 override = 0;
20338 }
20339 else
20340 {
20341 int face_id;
20342 struct face *face;
20343 struct font_info *font_info;
20344
20345 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20346 if (face_id < 0)
20347 return make_number (-1);
20348
20349 face = FACE_FROM_ID (it->f, face_id);
20350 font = face->font;
20351 if (font == NULL)
20352 return make_number (-1);
20353
20354 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20355 boff = font_info->baseline_offset;
20356 if (font_info->vertical_centering)
20357 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20358 }
20359
20360 ascent = FONT_BASE (font) + boff;
20361 descent = FONT_DESCENT (font) - boff;
20362
20363 if (override)
20364 {
20365 it->override_ascent = ascent;
20366 it->override_descent = descent;
20367 it->override_boff = boff;
20368 }
20369
20370 height = ascent + descent;
20371
20372 scale:
20373 if (FLOATP (val))
20374 height = (int)(XFLOAT_DATA (val) * height);
20375 else if (INTEGERP (val))
20376 height *= XINT (val);
20377
20378 return make_number (height);
20379 }
20380
20381
20382 /* RIF:
20383 Produce glyphs/get display metrics for the display element IT is
20384 loaded with. See the description of struct it in dispextern.h
20385 for an overview of struct it. */
20386
20387 void
20388 x_produce_glyphs (it)
20389 struct it *it;
20390 {
20391 int extra_line_spacing = it->extra_line_spacing;
20392
20393 it->glyph_not_available_p = 0;
20394
20395 if (it->what == IT_CHARACTER)
20396 {
20397 XChar2b char2b;
20398 XFontStruct *font;
20399 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20400 XCharStruct *pcm;
20401 int font_not_found_p;
20402 struct font_info *font_info;
20403 int boff; /* baseline offset */
20404 /* We may change it->multibyte_p upon unibyte<->multibyte
20405 conversion. So, save the current value now and restore it
20406 later.
20407
20408 Note: It seems that we don't have to record multibyte_p in
20409 struct glyph because the character code itself tells if or
20410 not the character is multibyte. Thus, in the future, we must
20411 consider eliminating the field `multibyte_p' in the struct
20412 glyph. */
20413 int saved_multibyte_p = it->multibyte_p;
20414
20415 /* Maybe translate single-byte characters to multibyte, or the
20416 other way. */
20417 it->char_to_display = it->c;
20418 if (!ASCII_BYTE_P (it->c))
20419 {
20420 if (unibyte_display_via_language_environment
20421 && SINGLE_BYTE_CHAR_P (it->c)
20422 && (it->c >= 0240
20423 || !NILP (Vnonascii_translation_table)))
20424 {
20425 it->char_to_display = unibyte_char_to_multibyte (it->c);
20426 it->multibyte_p = 1;
20427 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20428 face = FACE_FROM_ID (it->f, it->face_id);
20429 }
20430 else if (!SINGLE_BYTE_CHAR_P (it->c)
20431 && !it->multibyte_p)
20432 {
20433 it->multibyte_p = 1;
20434 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20435 face = FACE_FROM_ID (it->f, it->face_id);
20436 }
20437 }
20438
20439 /* Get font to use. Encode IT->char_to_display. */
20440 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20441 &char2b, it->multibyte_p, 0);
20442 font = face->font;
20443
20444 /* When no suitable font found, use the default font. */
20445 font_not_found_p = font == NULL;
20446 if (font_not_found_p)
20447 {
20448 font = FRAME_FONT (it->f);
20449 boff = FRAME_BASELINE_OFFSET (it->f);
20450 font_info = NULL;
20451 }
20452 else
20453 {
20454 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20455 boff = font_info->baseline_offset;
20456 if (font_info->vertical_centering)
20457 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20458 }
20459
20460 if (it->char_to_display >= ' '
20461 && (!it->multibyte_p || it->char_to_display < 128))
20462 {
20463 /* Either unibyte or ASCII. */
20464 int stretched_p;
20465
20466 it->nglyphs = 1;
20467
20468 pcm = FRAME_RIF (it->f)->per_char_metric
20469 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20470
20471 if (it->override_ascent >= 0)
20472 {
20473 it->ascent = it->override_ascent;
20474 it->descent = it->override_descent;
20475 boff = it->override_boff;
20476 }
20477 else
20478 {
20479 it->ascent = FONT_BASE (font) + boff;
20480 it->descent = FONT_DESCENT (font) - boff;
20481 }
20482
20483 if (pcm)
20484 {
20485 it->phys_ascent = pcm->ascent + boff;
20486 it->phys_descent = pcm->descent - boff;
20487 it->pixel_width = pcm->width;
20488 }
20489 else
20490 {
20491 it->glyph_not_available_p = 1;
20492 it->phys_ascent = it->ascent;
20493 it->phys_descent = it->descent;
20494 it->pixel_width = FONT_WIDTH (font);
20495 }
20496
20497 if (it->constrain_row_ascent_descent_p)
20498 {
20499 if (it->descent > it->max_descent)
20500 {
20501 it->ascent += it->descent - it->max_descent;
20502 it->descent = it->max_descent;
20503 }
20504 if (it->ascent > it->max_ascent)
20505 {
20506 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20507 it->ascent = it->max_ascent;
20508 }
20509 it->phys_ascent = min (it->phys_ascent, it->ascent);
20510 it->phys_descent = min (it->phys_descent, it->descent);
20511 extra_line_spacing = 0;
20512 }
20513
20514 /* If this is a space inside a region of text with
20515 `space-width' property, change its width. */
20516 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20517 if (stretched_p)
20518 it->pixel_width *= XFLOATINT (it->space_width);
20519
20520 /* If face has a box, add the box thickness to the character
20521 height. If character has a box line to the left and/or
20522 right, add the box line width to the character's width. */
20523 if (face->box != FACE_NO_BOX)
20524 {
20525 int thick = face->box_line_width;
20526
20527 if (thick > 0)
20528 {
20529 it->ascent += thick;
20530 it->descent += thick;
20531 }
20532 else
20533 thick = -thick;
20534
20535 if (it->start_of_box_run_p)
20536 it->pixel_width += thick;
20537 if (it->end_of_box_run_p)
20538 it->pixel_width += thick;
20539 }
20540
20541 /* If face has an overline, add the height of the overline
20542 (1 pixel) and a 1 pixel margin to the character height. */
20543 if (face->overline_p)
20544 it->ascent += overline_margin;
20545
20546 if (it->constrain_row_ascent_descent_p)
20547 {
20548 if (it->ascent > it->max_ascent)
20549 it->ascent = it->max_ascent;
20550 if (it->descent > it->max_descent)
20551 it->descent = it->max_descent;
20552 }
20553
20554 take_vertical_position_into_account (it);
20555
20556 /* If we have to actually produce glyphs, do it. */
20557 if (it->glyph_row)
20558 {
20559 if (stretched_p)
20560 {
20561 /* Translate a space with a `space-width' property
20562 into a stretch glyph. */
20563 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20564 / FONT_HEIGHT (font));
20565 append_stretch_glyph (it, it->object, it->pixel_width,
20566 it->ascent + it->descent, ascent);
20567 }
20568 else
20569 append_glyph (it);
20570
20571 /* If characters with lbearing or rbearing are displayed
20572 in this line, record that fact in a flag of the
20573 glyph row. This is used to optimize X output code. */
20574 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20575 it->glyph_row->contains_overlapping_glyphs_p = 1;
20576 }
20577 }
20578 else if (it->char_to_display == '\n')
20579 {
20580 /* A newline has no width but we need the height of the line.
20581 But if previous part of the line set a height, don't
20582 increase that height */
20583
20584 Lisp_Object height;
20585 Lisp_Object total_height = Qnil;
20586
20587 it->override_ascent = -1;
20588 it->pixel_width = 0;
20589 it->nglyphs = 0;
20590
20591 height = get_line_height_property(it, Qline_height);
20592 /* Split (line-height total-height) list */
20593 if (CONSP (height)
20594 && CONSP (XCDR (height))
20595 && NILP (XCDR (XCDR (height))))
20596 {
20597 total_height = XCAR (XCDR (height));
20598 height = XCAR (height);
20599 }
20600 height = calc_line_height_property(it, height, font, boff, 1);
20601
20602 if (it->override_ascent >= 0)
20603 {
20604 it->ascent = it->override_ascent;
20605 it->descent = it->override_descent;
20606 boff = it->override_boff;
20607 }
20608 else
20609 {
20610 it->ascent = FONT_BASE (font) + boff;
20611 it->descent = FONT_DESCENT (font) - boff;
20612 }
20613
20614 if (EQ (height, Qt))
20615 {
20616 if (it->descent > it->max_descent)
20617 {
20618 it->ascent += it->descent - it->max_descent;
20619 it->descent = it->max_descent;
20620 }
20621 if (it->ascent > it->max_ascent)
20622 {
20623 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20624 it->ascent = it->max_ascent;
20625 }
20626 it->phys_ascent = min (it->phys_ascent, it->ascent);
20627 it->phys_descent = min (it->phys_descent, it->descent);
20628 it->constrain_row_ascent_descent_p = 1;
20629 extra_line_spacing = 0;
20630 }
20631 else
20632 {
20633 Lisp_Object spacing;
20634
20635 it->phys_ascent = it->ascent;
20636 it->phys_descent = it->descent;
20637
20638 if ((it->max_ascent > 0 || it->max_descent > 0)
20639 && face->box != FACE_NO_BOX
20640 && face->box_line_width > 0)
20641 {
20642 it->ascent += face->box_line_width;
20643 it->descent += face->box_line_width;
20644 }
20645 if (!NILP (height)
20646 && XINT (height) > it->ascent + it->descent)
20647 it->ascent = XINT (height) - it->descent;
20648
20649 if (!NILP (total_height))
20650 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20651 else
20652 {
20653 spacing = get_line_height_property(it, Qline_spacing);
20654 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20655 }
20656 if (INTEGERP (spacing))
20657 {
20658 extra_line_spacing = XINT (spacing);
20659 if (!NILP (total_height))
20660 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20661 }
20662 }
20663 }
20664 else if (it->char_to_display == '\t')
20665 {
20666 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20667 int x = it->current_x + it->continuation_lines_width;
20668 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20669
20670 /* If the distance from the current position to the next tab
20671 stop is less than a space character width, use the
20672 tab stop after that. */
20673 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20674 next_tab_x += tab_width;
20675
20676 it->pixel_width = next_tab_x - x;
20677 it->nglyphs = 1;
20678 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20679 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20680
20681 if (it->glyph_row)
20682 {
20683 append_stretch_glyph (it, it->object, it->pixel_width,
20684 it->ascent + it->descent, it->ascent);
20685 }
20686 }
20687 else
20688 {
20689 /* A multi-byte character. Assume that the display width of the
20690 character is the width of the character multiplied by the
20691 width of the font. */
20692
20693 /* If we found a font, this font should give us the right
20694 metrics. If we didn't find a font, use the frame's
20695 default font and calculate the width of the character
20696 from the charset width; this is what old redisplay code
20697 did. */
20698
20699 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20700 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20701
20702 if (font_not_found_p || !pcm)
20703 {
20704 int charset = CHAR_CHARSET (it->char_to_display);
20705
20706 it->glyph_not_available_p = 1;
20707 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20708 * CHARSET_WIDTH (charset));
20709 it->phys_ascent = FONT_BASE (font) + boff;
20710 it->phys_descent = FONT_DESCENT (font) - boff;
20711 }
20712 else
20713 {
20714 it->pixel_width = pcm->width;
20715 it->phys_ascent = pcm->ascent + boff;
20716 it->phys_descent = pcm->descent - boff;
20717 if (it->glyph_row
20718 && (pcm->lbearing < 0
20719 || pcm->rbearing > pcm->width))
20720 it->glyph_row->contains_overlapping_glyphs_p = 1;
20721 }
20722 it->nglyphs = 1;
20723 it->ascent = FONT_BASE (font) + boff;
20724 it->descent = FONT_DESCENT (font) - boff;
20725 if (face->box != FACE_NO_BOX)
20726 {
20727 int thick = face->box_line_width;
20728
20729 if (thick > 0)
20730 {
20731 it->ascent += thick;
20732 it->descent += thick;
20733 }
20734 else
20735 thick = - thick;
20736
20737 if (it->start_of_box_run_p)
20738 it->pixel_width += thick;
20739 if (it->end_of_box_run_p)
20740 it->pixel_width += thick;
20741 }
20742
20743 /* If face has an overline, add the height of the overline
20744 (1 pixel) and a 1 pixel margin to the character height. */
20745 if (face->overline_p)
20746 it->ascent += overline_margin;
20747
20748 take_vertical_position_into_account (it);
20749
20750 if (it->glyph_row)
20751 append_glyph (it);
20752 }
20753 it->multibyte_p = saved_multibyte_p;
20754 }
20755 else if (it->what == IT_COMPOSITION)
20756 {
20757 /* Note: A composition is represented as one glyph in the
20758 glyph matrix. There are no padding glyphs. */
20759 XChar2b char2b;
20760 XFontStruct *font;
20761 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20762 XCharStruct *pcm;
20763 int font_not_found_p;
20764 struct font_info *font_info;
20765 int boff; /* baseline offset */
20766 struct composition *cmp = composition_table[it->cmp_id];
20767
20768 /* Maybe translate single-byte characters to multibyte. */
20769 it->char_to_display = it->c;
20770 if (unibyte_display_via_language_environment
20771 && SINGLE_BYTE_CHAR_P (it->c)
20772 && (it->c >= 0240
20773 || (it->c >= 0200
20774 && !NILP (Vnonascii_translation_table))))
20775 {
20776 it->char_to_display = unibyte_char_to_multibyte (it->c);
20777 }
20778
20779 /* Get face and font to use. Encode IT->char_to_display. */
20780 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20781 face = FACE_FROM_ID (it->f, it->face_id);
20782 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20783 &char2b, it->multibyte_p, 0);
20784 font = face->font;
20785
20786 /* When no suitable font found, use the default font. */
20787 font_not_found_p = font == NULL;
20788 if (font_not_found_p)
20789 {
20790 font = FRAME_FONT (it->f);
20791 boff = FRAME_BASELINE_OFFSET (it->f);
20792 font_info = NULL;
20793 }
20794 else
20795 {
20796 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20797 boff = font_info->baseline_offset;
20798 if (font_info->vertical_centering)
20799 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20800 }
20801
20802 /* There are no padding glyphs, so there is only one glyph to
20803 produce for the composition. Important is that pixel_width,
20804 ascent and descent are the values of what is drawn by
20805 draw_glyphs (i.e. the values of the overall glyphs composed). */
20806 it->nglyphs = 1;
20807
20808 /* If we have not yet calculated pixel size data of glyphs of
20809 the composition for the current face font, calculate them
20810 now. Theoretically, we have to check all fonts for the
20811 glyphs, but that requires much time and memory space. So,
20812 here we check only the font of the first glyph. This leads
20813 to incorrect display very rarely, and C-l (recenter) can
20814 correct the display anyway. */
20815 if (cmp->font != (void *) font)
20816 {
20817 /* Ascent and descent of the font of the first character of
20818 this composition (adjusted by baseline offset). Ascent
20819 and descent of overall glyphs should not be less than
20820 them respectively. */
20821 int font_ascent = FONT_BASE (font) + boff;
20822 int font_descent = FONT_DESCENT (font) - boff;
20823 /* Bounding box of the overall glyphs. */
20824 int leftmost, rightmost, lowest, highest;
20825 int i, width, ascent, descent;
20826
20827 cmp->font = (void *) font;
20828
20829 /* Initialize the bounding box. */
20830 if (font_info
20831 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20832 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20833 {
20834 width = pcm->width;
20835 ascent = pcm->ascent;
20836 descent = pcm->descent;
20837 }
20838 else
20839 {
20840 width = FONT_WIDTH (font);
20841 ascent = FONT_BASE (font);
20842 descent = FONT_DESCENT (font);
20843 }
20844
20845 rightmost = width;
20846 lowest = - descent + boff;
20847 highest = ascent + boff;
20848 leftmost = 0;
20849
20850 if (font_info
20851 && font_info->default_ascent
20852 && CHAR_TABLE_P (Vuse_default_ascent)
20853 && !NILP (Faref (Vuse_default_ascent,
20854 make_number (it->char_to_display))))
20855 highest = font_info->default_ascent + boff;
20856
20857 /* Draw the first glyph at the normal position. It may be
20858 shifted to right later if some other glyphs are drawn at
20859 the left. */
20860 cmp->offsets[0] = 0;
20861 cmp->offsets[1] = boff;
20862
20863 /* Set cmp->offsets for the remaining glyphs. */
20864 for (i = 1; i < cmp->glyph_len; i++)
20865 {
20866 int left, right, btm, top;
20867 int ch = COMPOSITION_GLYPH (cmp, i);
20868 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20869
20870 face = FACE_FROM_ID (it->f, face_id);
20871 get_char_face_and_encoding (it->f, ch, face->id,
20872 &char2b, it->multibyte_p, 0);
20873 font = face->font;
20874 if (font == NULL)
20875 {
20876 font = FRAME_FONT (it->f);
20877 boff = FRAME_BASELINE_OFFSET (it->f);
20878 font_info = NULL;
20879 }
20880 else
20881 {
20882 font_info
20883 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20884 boff = font_info->baseline_offset;
20885 if (font_info->vertical_centering)
20886 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20887 }
20888
20889 if (font_info
20890 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20891 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20892 {
20893 width = pcm->width;
20894 ascent = pcm->ascent;
20895 descent = pcm->descent;
20896 }
20897 else
20898 {
20899 width = FONT_WIDTH (font);
20900 ascent = 1;
20901 descent = 0;
20902 }
20903
20904 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20905 {
20906 /* Relative composition with or without
20907 alternate chars. */
20908 left = (leftmost + rightmost - width) / 2;
20909 btm = - descent + boff;
20910 if (font_info && font_info->relative_compose
20911 && (! CHAR_TABLE_P (Vignore_relative_composition)
20912 || NILP (Faref (Vignore_relative_composition,
20913 make_number (ch)))))
20914 {
20915
20916 if (- descent >= font_info->relative_compose)
20917 /* One extra pixel between two glyphs. */
20918 btm = highest + 1;
20919 else if (ascent <= 0)
20920 /* One extra pixel between two glyphs. */
20921 btm = lowest - 1 - ascent - descent;
20922 }
20923 }
20924 else
20925 {
20926 /* A composition rule is specified by an integer
20927 value that encodes global and new reference
20928 points (GREF and NREF). GREF and NREF are
20929 specified by numbers as below:
20930
20931 0---1---2 -- ascent
20932 | |
20933 | |
20934 | |
20935 9--10--11 -- center
20936 | |
20937 ---3---4---5--- baseline
20938 | |
20939 6---7---8 -- descent
20940 */
20941 int rule = COMPOSITION_RULE (cmp, i);
20942 int gref, nref, grefx, grefy, nrefx, nrefy;
20943
20944 COMPOSITION_DECODE_RULE (rule, gref, nref);
20945 grefx = gref % 3, nrefx = nref % 3;
20946 grefy = gref / 3, nrefy = nref / 3;
20947
20948 left = (leftmost
20949 + grefx * (rightmost - leftmost) / 2
20950 - nrefx * width / 2);
20951 btm = ((grefy == 0 ? highest
20952 : grefy == 1 ? 0
20953 : grefy == 2 ? lowest
20954 : (highest + lowest) / 2)
20955 - (nrefy == 0 ? ascent + descent
20956 : nrefy == 1 ? descent - boff
20957 : nrefy == 2 ? 0
20958 : (ascent + descent) / 2));
20959 }
20960
20961 cmp->offsets[i * 2] = left;
20962 cmp->offsets[i * 2 + 1] = btm + descent;
20963
20964 /* Update the bounding box of the overall glyphs. */
20965 right = left + width;
20966 top = btm + descent + ascent;
20967 if (left < leftmost)
20968 leftmost = left;
20969 if (right > rightmost)
20970 rightmost = right;
20971 if (top > highest)
20972 highest = top;
20973 if (btm < lowest)
20974 lowest = btm;
20975 }
20976
20977 /* If there are glyphs whose x-offsets are negative,
20978 shift all glyphs to the right and make all x-offsets
20979 non-negative. */
20980 if (leftmost < 0)
20981 {
20982 for (i = 0; i < cmp->glyph_len; i++)
20983 cmp->offsets[i * 2] -= leftmost;
20984 rightmost -= leftmost;
20985 }
20986
20987 cmp->pixel_width = rightmost;
20988 cmp->ascent = highest;
20989 cmp->descent = - lowest;
20990 if (cmp->ascent < font_ascent)
20991 cmp->ascent = font_ascent;
20992 if (cmp->descent < font_descent)
20993 cmp->descent = font_descent;
20994 }
20995
20996 it->pixel_width = cmp->pixel_width;
20997 it->ascent = it->phys_ascent = cmp->ascent;
20998 it->descent = it->phys_descent = cmp->descent;
20999
21000 if (face->box != FACE_NO_BOX)
21001 {
21002 int thick = face->box_line_width;
21003
21004 if (thick > 0)
21005 {
21006 it->ascent += thick;
21007 it->descent += thick;
21008 }
21009 else
21010 thick = - thick;
21011
21012 if (it->start_of_box_run_p)
21013 it->pixel_width += thick;
21014 if (it->end_of_box_run_p)
21015 it->pixel_width += thick;
21016 }
21017
21018 /* If face has an overline, add the height of the overline
21019 (1 pixel) and a 1 pixel margin to the character height. */
21020 if (face->overline_p)
21021 it->ascent += overline_margin;
21022
21023 take_vertical_position_into_account (it);
21024
21025 if (it->glyph_row)
21026 append_composite_glyph (it);
21027 }
21028 else if (it->what == IT_IMAGE)
21029 produce_image_glyph (it);
21030 else if (it->what == IT_STRETCH)
21031 produce_stretch_glyph (it);
21032
21033 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21034 because this isn't true for images with `:ascent 100'. */
21035 xassert (it->ascent >= 0 && it->descent >= 0);
21036 if (it->area == TEXT_AREA)
21037 it->current_x += it->pixel_width;
21038
21039 if (extra_line_spacing > 0)
21040 {
21041 it->descent += extra_line_spacing;
21042 if (extra_line_spacing > it->max_extra_line_spacing)
21043 it->max_extra_line_spacing = extra_line_spacing;
21044 }
21045
21046 it->max_ascent = max (it->max_ascent, it->ascent);
21047 it->max_descent = max (it->max_descent, it->descent);
21048 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21049 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21050 }
21051
21052 /* EXPORT for RIF:
21053 Output LEN glyphs starting at START at the nominal cursor position.
21054 Advance the nominal cursor over the text. The global variable
21055 updated_window contains the window being updated, updated_row is
21056 the glyph row being updated, and updated_area is the area of that
21057 row being updated. */
21058
21059 void
21060 x_write_glyphs (start, len)
21061 struct glyph *start;
21062 int len;
21063 {
21064 int x, hpos;
21065
21066 xassert (updated_window && updated_row);
21067 BLOCK_INPUT;
21068
21069 /* Write glyphs. */
21070
21071 hpos = start - updated_row->glyphs[updated_area];
21072 x = draw_glyphs (updated_window, output_cursor.x,
21073 updated_row, updated_area,
21074 hpos, hpos + len,
21075 DRAW_NORMAL_TEXT, 0);
21076
21077 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21078 if (updated_area == TEXT_AREA
21079 && updated_window->phys_cursor_on_p
21080 && updated_window->phys_cursor.vpos == output_cursor.vpos
21081 && updated_window->phys_cursor.hpos >= hpos
21082 && updated_window->phys_cursor.hpos < hpos + len)
21083 updated_window->phys_cursor_on_p = 0;
21084
21085 UNBLOCK_INPUT;
21086
21087 /* Advance the output cursor. */
21088 output_cursor.hpos += len;
21089 output_cursor.x = x;
21090 }
21091
21092
21093 /* EXPORT for RIF:
21094 Insert LEN glyphs from START at the nominal cursor position. */
21095
21096 void
21097 x_insert_glyphs (start, len)
21098 struct glyph *start;
21099 int len;
21100 {
21101 struct frame *f;
21102 struct window *w;
21103 int line_height, shift_by_width, shifted_region_width;
21104 struct glyph_row *row;
21105 struct glyph *glyph;
21106 int frame_x, frame_y, hpos;
21107
21108 xassert (updated_window && updated_row);
21109 BLOCK_INPUT;
21110 w = updated_window;
21111 f = XFRAME (WINDOW_FRAME (w));
21112
21113 /* Get the height of the line we are in. */
21114 row = updated_row;
21115 line_height = row->height;
21116
21117 /* Get the width of the glyphs to insert. */
21118 shift_by_width = 0;
21119 for (glyph = start; glyph < start + len; ++glyph)
21120 shift_by_width += glyph->pixel_width;
21121
21122 /* Get the width of the region to shift right. */
21123 shifted_region_width = (window_box_width (w, updated_area)
21124 - output_cursor.x
21125 - shift_by_width);
21126
21127 /* Shift right. */
21128 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21129 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21130
21131 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21132 line_height, shift_by_width);
21133
21134 /* Write the glyphs. */
21135 hpos = start - row->glyphs[updated_area];
21136 draw_glyphs (w, output_cursor.x, row, updated_area,
21137 hpos, hpos + len,
21138 DRAW_NORMAL_TEXT, 0);
21139
21140 /* Advance the output cursor. */
21141 output_cursor.hpos += len;
21142 output_cursor.x += shift_by_width;
21143 UNBLOCK_INPUT;
21144 }
21145
21146
21147 /* EXPORT for RIF:
21148 Erase the current text line from the nominal cursor position
21149 (inclusive) to pixel column TO_X (exclusive). The idea is that
21150 everything from TO_X onward is already erased.
21151
21152 TO_X is a pixel position relative to updated_area of
21153 updated_window. TO_X == -1 means clear to the end of this area. */
21154
21155 void
21156 x_clear_end_of_line (to_x)
21157 int to_x;
21158 {
21159 struct frame *f;
21160 struct window *w = updated_window;
21161 int max_x, min_y, max_y;
21162 int from_x, from_y, to_y;
21163
21164 xassert (updated_window && updated_row);
21165 f = XFRAME (w->frame);
21166
21167 if (updated_row->full_width_p)
21168 max_x = WINDOW_TOTAL_WIDTH (w);
21169 else
21170 max_x = window_box_width (w, updated_area);
21171 max_y = window_text_bottom_y (w);
21172
21173 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21174 of window. For TO_X > 0, truncate to end of drawing area. */
21175 if (to_x == 0)
21176 return;
21177 else if (to_x < 0)
21178 to_x = max_x;
21179 else
21180 to_x = min (to_x, max_x);
21181
21182 to_y = min (max_y, output_cursor.y + updated_row->height);
21183
21184 /* Notice if the cursor will be cleared by this operation. */
21185 if (!updated_row->full_width_p)
21186 notice_overwritten_cursor (w, updated_area,
21187 output_cursor.x, -1,
21188 updated_row->y,
21189 MATRIX_ROW_BOTTOM_Y (updated_row));
21190
21191 from_x = output_cursor.x;
21192
21193 /* Translate to frame coordinates. */
21194 if (updated_row->full_width_p)
21195 {
21196 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21197 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21198 }
21199 else
21200 {
21201 int area_left = window_box_left (w, updated_area);
21202 from_x += area_left;
21203 to_x += area_left;
21204 }
21205
21206 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21207 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21208 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21209
21210 /* Prevent inadvertently clearing to end of the X window. */
21211 if (to_x > from_x && to_y > from_y)
21212 {
21213 BLOCK_INPUT;
21214 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21215 to_x - from_x, to_y - from_y);
21216 UNBLOCK_INPUT;
21217 }
21218 }
21219
21220 #endif /* HAVE_WINDOW_SYSTEM */
21221
21222
21223 \f
21224 /***********************************************************************
21225 Cursor types
21226 ***********************************************************************/
21227
21228 /* Value is the internal representation of the specified cursor type
21229 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21230 of the bar cursor. */
21231
21232 static enum text_cursor_kinds
21233 get_specified_cursor_type (arg, width)
21234 Lisp_Object arg;
21235 int *width;
21236 {
21237 enum text_cursor_kinds type;
21238
21239 if (NILP (arg))
21240 return NO_CURSOR;
21241
21242 if (EQ (arg, Qbox))
21243 return FILLED_BOX_CURSOR;
21244
21245 if (EQ (arg, Qhollow))
21246 return HOLLOW_BOX_CURSOR;
21247
21248 if (EQ (arg, Qbar))
21249 {
21250 *width = 2;
21251 return BAR_CURSOR;
21252 }
21253
21254 if (CONSP (arg)
21255 && EQ (XCAR (arg), Qbar)
21256 && INTEGERP (XCDR (arg))
21257 && XINT (XCDR (arg)) >= 0)
21258 {
21259 *width = XINT (XCDR (arg));
21260 return BAR_CURSOR;
21261 }
21262
21263 if (EQ (arg, Qhbar))
21264 {
21265 *width = 2;
21266 return HBAR_CURSOR;
21267 }
21268
21269 if (CONSP (arg)
21270 && EQ (XCAR (arg), Qhbar)
21271 && INTEGERP (XCDR (arg))
21272 && XINT (XCDR (arg)) >= 0)
21273 {
21274 *width = XINT (XCDR (arg));
21275 return HBAR_CURSOR;
21276 }
21277
21278 /* Treat anything unknown as "hollow box cursor".
21279 It was bad to signal an error; people have trouble fixing
21280 .Xdefaults with Emacs, when it has something bad in it. */
21281 type = HOLLOW_BOX_CURSOR;
21282
21283 return type;
21284 }
21285
21286 /* Set the default cursor types for specified frame. */
21287 void
21288 set_frame_cursor_types (f, arg)
21289 struct frame *f;
21290 Lisp_Object arg;
21291 {
21292 int width;
21293 Lisp_Object tem;
21294
21295 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21296 FRAME_CURSOR_WIDTH (f) = width;
21297
21298 /* By default, set up the blink-off state depending on the on-state. */
21299
21300 tem = Fassoc (arg, Vblink_cursor_alist);
21301 if (!NILP (tem))
21302 {
21303 FRAME_BLINK_OFF_CURSOR (f)
21304 = get_specified_cursor_type (XCDR (tem), &width);
21305 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21306 }
21307 else
21308 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21309 }
21310
21311
21312 /* Return the cursor we want to be displayed in window W. Return
21313 width of bar/hbar cursor through WIDTH arg. Return with
21314 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21315 (i.e. if the `system caret' should track this cursor).
21316
21317 In a mini-buffer window, we want the cursor only to appear if we
21318 are reading input from this window. For the selected window, we
21319 want the cursor type given by the frame parameter or buffer local
21320 setting of cursor-type. If explicitly marked off, draw no cursor.
21321 In all other cases, we want a hollow box cursor. */
21322
21323 static enum text_cursor_kinds
21324 get_window_cursor_type (w, glyph, width, active_cursor)
21325 struct window *w;
21326 struct glyph *glyph;
21327 int *width;
21328 int *active_cursor;
21329 {
21330 struct frame *f = XFRAME (w->frame);
21331 struct buffer *b = XBUFFER (w->buffer);
21332 int cursor_type = DEFAULT_CURSOR;
21333 Lisp_Object alt_cursor;
21334 int non_selected = 0;
21335
21336 *active_cursor = 1;
21337
21338 /* Echo area */
21339 if (cursor_in_echo_area
21340 && FRAME_HAS_MINIBUF_P (f)
21341 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21342 {
21343 if (w == XWINDOW (echo_area_window))
21344 {
21345 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21346 {
21347 *width = FRAME_CURSOR_WIDTH (f);
21348 return FRAME_DESIRED_CURSOR (f);
21349 }
21350 else
21351 return get_specified_cursor_type (b->cursor_type, width);
21352 }
21353
21354 *active_cursor = 0;
21355 non_selected = 1;
21356 }
21357
21358 /* Detect a nonselected window or nonselected frame. */
21359 else if (w != XWINDOW (f->selected_window)
21360 #ifdef HAVE_WINDOW_SYSTEM
21361 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21362 #endif
21363 )
21364 {
21365 *active_cursor = 0;
21366
21367 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21368 return NO_CURSOR;
21369
21370 non_selected = 1;
21371 }
21372
21373 /* Never display a cursor in a window in which cursor-type is nil. */
21374 if (NILP (b->cursor_type))
21375 return NO_CURSOR;
21376
21377 /* Get the normal cursor type for this window. */
21378 if (EQ (b->cursor_type, Qt))
21379 {
21380 cursor_type = FRAME_DESIRED_CURSOR (f);
21381 *width = FRAME_CURSOR_WIDTH (f);
21382 }
21383 else
21384 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21385
21386 /* Use cursor-in-non-selected-windows instead
21387 for non-selected window or frame. */
21388 if (non_selected)
21389 {
21390 alt_cursor = b->cursor_in_non_selected_windows;
21391 if (!EQ (Qt, alt_cursor))
21392 return get_specified_cursor_type (alt_cursor, width);
21393 /* t means modify the normal cursor type. */
21394 if (cursor_type == FILLED_BOX_CURSOR)
21395 cursor_type = HOLLOW_BOX_CURSOR;
21396 else if (cursor_type == BAR_CURSOR && *width > 1)
21397 --*width;
21398 return cursor_type;
21399 }
21400
21401 /* Use normal cursor if not blinked off. */
21402 if (!w->cursor_off_p)
21403 {
21404 #ifdef HAVE_WINDOW_SYSTEM
21405 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21406 {
21407 if (cursor_type == FILLED_BOX_CURSOR)
21408 {
21409 /* Using a block cursor on large images can be very annoying.
21410 So use a hollow cursor for "large" images.
21411 If image is not transparent (no mask), also use hollow cursor. */
21412 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21413 if (img != NULL && IMAGEP (img->spec))
21414 {
21415 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21416 where N = size of default frame font size.
21417 This should cover most of the "tiny" icons people may use. */
21418 if (!img->mask
21419 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21420 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21421 cursor_type = HOLLOW_BOX_CURSOR;
21422 }
21423 }
21424 else if (cursor_type != NO_CURSOR)
21425 {
21426 /* Display current only supports BOX and HOLLOW cursors for images.
21427 So for now, unconditionally use a HOLLOW cursor when cursor is
21428 not a solid box cursor. */
21429 cursor_type = HOLLOW_BOX_CURSOR;
21430 }
21431 }
21432 #endif
21433 return cursor_type;
21434 }
21435
21436 /* Cursor is blinked off, so determine how to "toggle" it. */
21437
21438 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21439 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21440 return get_specified_cursor_type (XCDR (alt_cursor), width);
21441
21442 /* Then see if frame has specified a specific blink off cursor type. */
21443 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21444 {
21445 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21446 return FRAME_BLINK_OFF_CURSOR (f);
21447 }
21448
21449 #if 0
21450 /* Some people liked having a permanently visible blinking cursor,
21451 while others had very strong opinions against it. So it was
21452 decided to remove it. KFS 2003-09-03 */
21453
21454 /* Finally perform built-in cursor blinking:
21455 filled box <-> hollow box
21456 wide [h]bar <-> narrow [h]bar
21457 narrow [h]bar <-> no cursor
21458 other type <-> no cursor */
21459
21460 if (cursor_type == FILLED_BOX_CURSOR)
21461 return HOLLOW_BOX_CURSOR;
21462
21463 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21464 {
21465 *width = 1;
21466 return cursor_type;
21467 }
21468 #endif
21469
21470 return NO_CURSOR;
21471 }
21472
21473
21474 #ifdef HAVE_WINDOW_SYSTEM
21475
21476 /* Notice when the text cursor of window W has been completely
21477 overwritten by a drawing operation that outputs glyphs in AREA
21478 starting at X0 and ending at X1 in the line starting at Y0 and
21479 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21480 the rest of the line after X0 has been written. Y coordinates
21481 are window-relative. */
21482
21483 static void
21484 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21485 struct window *w;
21486 enum glyph_row_area area;
21487 int x0, y0, x1, y1;
21488 {
21489 int cx0, cx1, cy0, cy1;
21490 struct glyph_row *row;
21491
21492 if (!w->phys_cursor_on_p)
21493 return;
21494 if (area != TEXT_AREA)
21495 return;
21496
21497 if (w->phys_cursor.vpos < 0
21498 || w->phys_cursor.vpos >= w->current_matrix->nrows
21499 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21500 !(row->enabled_p && row->displays_text_p)))
21501 return;
21502
21503 if (row->cursor_in_fringe_p)
21504 {
21505 row->cursor_in_fringe_p = 0;
21506 draw_fringe_bitmap (w, row, 0);
21507 w->phys_cursor_on_p = 0;
21508 return;
21509 }
21510
21511 cx0 = w->phys_cursor.x;
21512 cx1 = cx0 + w->phys_cursor_width;
21513 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21514 return;
21515
21516 /* The cursor image will be completely removed from the
21517 screen if the output area intersects the cursor area in
21518 y-direction. When we draw in [y0 y1[, and some part of
21519 the cursor is at y < y0, that part must have been drawn
21520 before. When scrolling, the cursor is erased before
21521 actually scrolling, so we don't come here. When not
21522 scrolling, the rows above the old cursor row must have
21523 changed, and in this case these rows must have written
21524 over the cursor image.
21525
21526 Likewise if part of the cursor is below y1, with the
21527 exception of the cursor being in the first blank row at
21528 the buffer and window end because update_text_area
21529 doesn't draw that row. (Except when it does, but
21530 that's handled in update_text_area.) */
21531
21532 cy0 = w->phys_cursor.y;
21533 cy1 = cy0 + w->phys_cursor_height;
21534 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21535 return;
21536
21537 w->phys_cursor_on_p = 0;
21538 }
21539
21540 #endif /* HAVE_WINDOW_SYSTEM */
21541
21542 \f
21543 /************************************************************************
21544 Mouse Face
21545 ************************************************************************/
21546
21547 #ifdef HAVE_WINDOW_SYSTEM
21548
21549 /* EXPORT for RIF:
21550 Fix the display of area AREA of overlapping row ROW in window W
21551 with respect to the overlapping part OVERLAPS. */
21552
21553 void
21554 x_fix_overlapping_area (w, row, area, overlaps)
21555 struct window *w;
21556 struct glyph_row *row;
21557 enum glyph_row_area area;
21558 int overlaps;
21559 {
21560 int i, x;
21561
21562 BLOCK_INPUT;
21563
21564 x = 0;
21565 for (i = 0; i < row->used[area];)
21566 {
21567 if (row->glyphs[area][i].overlaps_vertically_p)
21568 {
21569 int start = i, start_x = x;
21570
21571 do
21572 {
21573 x += row->glyphs[area][i].pixel_width;
21574 ++i;
21575 }
21576 while (i < row->used[area]
21577 && row->glyphs[area][i].overlaps_vertically_p);
21578
21579 draw_glyphs (w, start_x, row, area,
21580 start, i,
21581 DRAW_NORMAL_TEXT, overlaps);
21582 }
21583 else
21584 {
21585 x += row->glyphs[area][i].pixel_width;
21586 ++i;
21587 }
21588 }
21589
21590 UNBLOCK_INPUT;
21591 }
21592
21593
21594 /* EXPORT:
21595 Draw the cursor glyph of window W in glyph row ROW. See the
21596 comment of draw_glyphs for the meaning of HL. */
21597
21598 void
21599 draw_phys_cursor_glyph (w, row, hl)
21600 struct window *w;
21601 struct glyph_row *row;
21602 enum draw_glyphs_face hl;
21603 {
21604 /* If cursor hpos is out of bounds, don't draw garbage. This can
21605 happen in mini-buffer windows when switching between echo area
21606 glyphs and mini-buffer. */
21607 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21608 {
21609 int on_p = w->phys_cursor_on_p;
21610 int x1;
21611 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21612 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21613 hl, 0);
21614 w->phys_cursor_on_p = on_p;
21615
21616 if (hl == DRAW_CURSOR)
21617 w->phys_cursor_width = x1 - w->phys_cursor.x;
21618 /* When we erase the cursor, and ROW is overlapped by other
21619 rows, make sure that these overlapping parts of other rows
21620 are redrawn. */
21621 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21622 {
21623 w->phys_cursor_width = x1 - w->phys_cursor.x;
21624
21625 if (row > w->current_matrix->rows
21626 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21627 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21628 OVERLAPS_ERASED_CURSOR);
21629
21630 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21631 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21632 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21633 OVERLAPS_ERASED_CURSOR);
21634 }
21635 }
21636 }
21637
21638
21639 /* EXPORT:
21640 Erase the image of a cursor of window W from the screen. */
21641
21642 void
21643 erase_phys_cursor (w)
21644 struct window *w;
21645 {
21646 struct frame *f = XFRAME (w->frame);
21647 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21648 int hpos = w->phys_cursor.hpos;
21649 int vpos = w->phys_cursor.vpos;
21650 int mouse_face_here_p = 0;
21651 struct glyph_matrix *active_glyphs = w->current_matrix;
21652 struct glyph_row *cursor_row;
21653 struct glyph *cursor_glyph;
21654 enum draw_glyphs_face hl;
21655
21656 /* No cursor displayed or row invalidated => nothing to do on the
21657 screen. */
21658 if (w->phys_cursor_type == NO_CURSOR)
21659 goto mark_cursor_off;
21660
21661 /* VPOS >= active_glyphs->nrows means that window has been resized.
21662 Don't bother to erase the cursor. */
21663 if (vpos >= active_glyphs->nrows)
21664 goto mark_cursor_off;
21665
21666 /* If row containing cursor is marked invalid, there is nothing we
21667 can do. */
21668 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21669 if (!cursor_row->enabled_p)
21670 goto mark_cursor_off;
21671
21672 /* If line spacing is > 0, old cursor may only be partially visible in
21673 window after split-window. So adjust visible height. */
21674 cursor_row->visible_height = min (cursor_row->visible_height,
21675 window_text_bottom_y (w) - cursor_row->y);
21676
21677 /* If row is completely invisible, don't attempt to delete a cursor which
21678 isn't there. This can happen if cursor is at top of a window, and
21679 we switch to a buffer with a header line in that window. */
21680 if (cursor_row->visible_height <= 0)
21681 goto mark_cursor_off;
21682
21683 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21684 if (cursor_row->cursor_in_fringe_p)
21685 {
21686 cursor_row->cursor_in_fringe_p = 0;
21687 draw_fringe_bitmap (w, cursor_row, 0);
21688 goto mark_cursor_off;
21689 }
21690
21691 /* This can happen when the new row is shorter than the old one.
21692 In this case, either draw_glyphs or clear_end_of_line
21693 should have cleared the cursor. Note that we wouldn't be
21694 able to erase the cursor in this case because we don't have a
21695 cursor glyph at hand. */
21696 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21697 goto mark_cursor_off;
21698
21699 /* If the cursor is in the mouse face area, redisplay that when
21700 we clear the cursor. */
21701 if (! NILP (dpyinfo->mouse_face_window)
21702 && w == XWINDOW (dpyinfo->mouse_face_window)
21703 && (vpos > dpyinfo->mouse_face_beg_row
21704 || (vpos == dpyinfo->mouse_face_beg_row
21705 && hpos >= dpyinfo->mouse_face_beg_col))
21706 && (vpos < dpyinfo->mouse_face_end_row
21707 || (vpos == dpyinfo->mouse_face_end_row
21708 && hpos < dpyinfo->mouse_face_end_col))
21709 /* Don't redraw the cursor's spot in mouse face if it is at the
21710 end of a line (on a newline). The cursor appears there, but
21711 mouse highlighting does not. */
21712 && cursor_row->used[TEXT_AREA] > hpos)
21713 mouse_face_here_p = 1;
21714
21715 /* Maybe clear the display under the cursor. */
21716 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21717 {
21718 int x, y, left_x;
21719 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21720 int width;
21721
21722 cursor_glyph = get_phys_cursor_glyph (w);
21723 if (cursor_glyph == NULL)
21724 goto mark_cursor_off;
21725
21726 width = cursor_glyph->pixel_width;
21727 left_x = window_box_left_offset (w, TEXT_AREA);
21728 x = w->phys_cursor.x;
21729 if (x < left_x)
21730 width -= left_x - x;
21731 width = min (width, window_box_width (w, TEXT_AREA) - x);
21732 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21733 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21734
21735 if (width > 0)
21736 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21737 }
21738
21739 /* Erase the cursor by redrawing the character underneath it. */
21740 if (mouse_face_here_p)
21741 hl = DRAW_MOUSE_FACE;
21742 else
21743 hl = DRAW_NORMAL_TEXT;
21744 draw_phys_cursor_glyph (w, cursor_row, hl);
21745
21746 mark_cursor_off:
21747 w->phys_cursor_on_p = 0;
21748 w->phys_cursor_type = NO_CURSOR;
21749 }
21750
21751
21752 /* EXPORT:
21753 Display or clear cursor of window W. If ON is zero, clear the
21754 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21755 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21756
21757 void
21758 display_and_set_cursor (w, on, hpos, vpos, x, y)
21759 struct window *w;
21760 int on, hpos, vpos, x, y;
21761 {
21762 struct frame *f = XFRAME (w->frame);
21763 int new_cursor_type;
21764 int new_cursor_width;
21765 int active_cursor;
21766 struct glyph_row *glyph_row;
21767 struct glyph *glyph;
21768
21769 /* This is pointless on invisible frames, and dangerous on garbaged
21770 windows and frames; in the latter case, the frame or window may
21771 be in the midst of changing its size, and x and y may be off the
21772 window. */
21773 if (! FRAME_VISIBLE_P (f)
21774 || FRAME_GARBAGED_P (f)
21775 || vpos >= w->current_matrix->nrows
21776 || hpos >= w->current_matrix->matrix_w)
21777 return;
21778
21779 /* If cursor is off and we want it off, return quickly. */
21780 if (!on && !w->phys_cursor_on_p)
21781 return;
21782
21783 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21784 /* If cursor row is not enabled, we don't really know where to
21785 display the cursor. */
21786 if (!glyph_row->enabled_p)
21787 {
21788 w->phys_cursor_on_p = 0;
21789 return;
21790 }
21791
21792 glyph = NULL;
21793 if (!glyph_row->exact_window_width_line_p
21794 || hpos < glyph_row->used[TEXT_AREA])
21795 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21796
21797 xassert (interrupt_input_blocked);
21798
21799 /* Set new_cursor_type to the cursor we want to be displayed. */
21800 new_cursor_type = get_window_cursor_type (w, glyph,
21801 &new_cursor_width, &active_cursor);
21802
21803 /* If cursor is currently being shown and we don't want it to be or
21804 it is in the wrong place, or the cursor type is not what we want,
21805 erase it. */
21806 if (w->phys_cursor_on_p
21807 && (!on
21808 || w->phys_cursor.x != x
21809 || w->phys_cursor.y != y
21810 || new_cursor_type != w->phys_cursor_type
21811 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21812 && new_cursor_width != w->phys_cursor_width)))
21813 erase_phys_cursor (w);
21814
21815 /* Don't check phys_cursor_on_p here because that flag is only set
21816 to zero in some cases where we know that the cursor has been
21817 completely erased, to avoid the extra work of erasing the cursor
21818 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21819 still not be visible, or it has only been partly erased. */
21820 if (on)
21821 {
21822 w->phys_cursor_ascent = glyph_row->ascent;
21823 w->phys_cursor_height = glyph_row->height;
21824
21825 /* Set phys_cursor_.* before x_draw_.* is called because some
21826 of them may need the information. */
21827 w->phys_cursor.x = x;
21828 w->phys_cursor.y = glyph_row->y;
21829 w->phys_cursor.hpos = hpos;
21830 w->phys_cursor.vpos = vpos;
21831 }
21832
21833 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
21834 new_cursor_type, new_cursor_width,
21835 on, active_cursor);
21836 }
21837
21838
21839 /* Switch the display of W's cursor on or off, according to the value
21840 of ON. */
21841
21842 static void
21843 update_window_cursor (w, on)
21844 struct window *w;
21845 int on;
21846 {
21847 /* Don't update cursor in windows whose frame is in the process
21848 of being deleted. */
21849 if (w->current_matrix)
21850 {
21851 BLOCK_INPUT;
21852 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21853 w->phys_cursor.x, w->phys_cursor.y);
21854 UNBLOCK_INPUT;
21855 }
21856 }
21857
21858
21859 /* Call update_window_cursor with parameter ON_P on all leaf windows
21860 in the window tree rooted at W. */
21861
21862 static void
21863 update_cursor_in_window_tree (w, on_p)
21864 struct window *w;
21865 int on_p;
21866 {
21867 while (w)
21868 {
21869 if (!NILP (w->hchild))
21870 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21871 else if (!NILP (w->vchild))
21872 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21873 else
21874 update_window_cursor (w, on_p);
21875
21876 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21877 }
21878 }
21879
21880
21881 /* EXPORT:
21882 Display the cursor on window W, or clear it, according to ON_P.
21883 Don't change the cursor's position. */
21884
21885 void
21886 x_update_cursor (f, on_p)
21887 struct frame *f;
21888 int on_p;
21889 {
21890 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21891 }
21892
21893
21894 /* EXPORT:
21895 Clear the cursor of window W to background color, and mark the
21896 cursor as not shown. This is used when the text where the cursor
21897 is is about to be rewritten. */
21898
21899 void
21900 x_clear_cursor (w)
21901 struct window *w;
21902 {
21903 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21904 update_window_cursor (w, 0);
21905 }
21906
21907
21908 /* EXPORT:
21909 Display the active region described by mouse_face_* according to DRAW. */
21910
21911 void
21912 show_mouse_face (dpyinfo, draw)
21913 Display_Info *dpyinfo;
21914 enum draw_glyphs_face draw;
21915 {
21916 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21917 struct frame *f = XFRAME (WINDOW_FRAME (w));
21918
21919 if (/* If window is in the process of being destroyed, don't bother
21920 to do anything. */
21921 w->current_matrix != NULL
21922 /* Don't update mouse highlight if hidden */
21923 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21924 /* Recognize when we are called to operate on rows that don't exist
21925 anymore. This can happen when a window is split. */
21926 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21927 {
21928 int phys_cursor_on_p = w->phys_cursor_on_p;
21929 struct glyph_row *row, *first, *last;
21930
21931 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21932 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21933
21934 for (row = first; row <= last && row->enabled_p; ++row)
21935 {
21936 int start_hpos, end_hpos, start_x;
21937
21938 /* For all but the first row, the highlight starts at column 0. */
21939 if (row == first)
21940 {
21941 start_hpos = dpyinfo->mouse_face_beg_col;
21942 start_x = dpyinfo->mouse_face_beg_x;
21943 }
21944 else
21945 {
21946 start_hpos = 0;
21947 start_x = 0;
21948 }
21949
21950 if (row == last)
21951 end_hpos = dpyinfo->mouse_face_end_col;
21952 else
21953 {
21954 end_hpos = row->used[TEXT_AREA];
21955 if (draw == DRAW_NORMAL_TEXT)
21956 row->fill_line_p = 1; /* Clear to end of line */
21957 }
21958
21959 if (end_hpos > start_hpos)
21960 {
21961 draw_glyphs (w, start_x, row, TEXT_AREA,
21962 start_hpos, end_hpos,
21963 draw, 0);
21964
21965 row->mouse_face_p
21966 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21967 }
21968 }
21969
21970 /* When we've written over the cursor, arrange for it to
21971 be displayed again. */
21972 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21973 {
21974 BLOCK_INPUT;
21975 display_and_set_cursor (w, 1,
21976 w->phys_cursor.hpos, w->phys_cursor.vpos,
21977 w->phys_cursor.x, w->phys_cursor.y);
21978 UNBLOCK_INPUT;
21979 }
21980 }
21981
21982 /* Change the mouse cursor. */
21983 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
21984 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21985 else if (draw == DRAW_MOUSE_FACE)
21986 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21987 else
21988 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21989 }
21990
21991 /* EXPORT:
21992 Clear out the mouse-highlighted active region.
21993 Redraw it un-highlighted first. Value is non-zero if mouse
21994 face was actually drawn unhighlighted. */
21995
21996 int
21997 clear_mouse_face (dpyinfo)
21998 Display_Info *dpyinfo;
21999 {
22000 int cleared = 0;
22001
22002 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22003 {
22004 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22005 cleared = 1;
22006 }
22007
22008 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22009 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22010 dpyinfo->mouse_face_window = Qnil;
22011 dpyinfo->mouse_face_overlay = Qnil;
22012 return cleared;
22013 }
22014
22015
22016 /* EXPORT:
22017 Non-zero if physical cursor of window W is within mouse face. */
22018
22019 int
22020 cursor_in_mouse_face_p (w)
22021 struct window *w;
22022 {
22023 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22024 int in_mouse_face = 0;
22025
22026 if (WINDOWP (dpyinfo->mouse_face_window)
22027 && XWINDOW (dpyinfo->mouse_face_window) == w)
22028 {
22029 int hpos = w->phys_cursor.hpos;
22030 int vpos = w->phys_cursor.vpos;
22031
22032 if (vpos >= dpyinfo->mouse_face_beg_row
22033 && vpos <= dpyinfo->mouse_face_end_row
22034 && (vpos > dpyinfo->mouse_face_beg_row
22035 || hpos >= dpyinfo->mouse_face_beg_col)
22036 && (vpos < dpyinfo->mouse_face_end_row
22037 || hpos < dpyinfo->mouse_face_end_col
22038 || dpyinfo->mouse_face_past_end))
22039 in_mouse_face = 1;
22040 }
22041
22042 return in_mouse_face;
22043 }
22044
22045
22046
22047 \f
22048 /* Find the glyph matrix position of buffer position CHARPOS in window
22049 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22050 current glyphs must be up to date. If CHARPOS is above window
22051 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22052 of last line in W. In the row containing CHARPOS, stop before glyphs
22053 having STOP as object. */
22054
22055 #if 1 /* This is a version of fast_find_position that's more correct
22056 in the presence of hscrolling, for example. I didn't install
22057 it right away because the problem fixed is minor, it failed
22058 in 20.x as well, and I think it's too risky to install
22059 so near the release of 21.1. 2001-09-25 gerd. */
22060
22061 static int
22062 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22063 struct window *w;
22064 int charpos;
22065 int *hpos, *vpos, *x, *y;
22066 Lisp_Object stop;
22067 {
22068 struct glyph_row *row, *first;
22069 struct glyph *glyph, *end;
22070 int past_end = 0;
22071
22072 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22073 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22074 {
22075 *x = first->x;
22076 *y = first->y;
22077 *hpos = 0;
22078 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22079 return 1;
22080 }
22081
22082 row = row_containing_pos (w, charpos, first, NULL, 0);
22083 if (row == NULL)
22084 {
22085 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22086 past_end = 1;
22087 }
22088
22089 /* If whole rows or last part of a row came from a display overlay,
22090 row_containing_pos will skip over such rows because their end pos
22091 equals the start pos of the overlay or interval.
22092
22093 Move back if we have a STOP object and previous row's
22094 end glyph came from STOP. */
22095 if (!NILP (stop))
22096 {
22097 struct glyph_row *prev;
22098 while ((prev = row - 1, prev >= first)
22099 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22100 && prev->used[TEXT_AREA] > 0)
22101 {
22102 struct glyph *beg = prev->glyphs[TEXT_AREA];
22103 glyph = beg + prev->used[TEXT_AREA];
22104 while (--glyph >= beg
22105 && INTEGERP (glyph->object));
22106 if (glyph < beg
22107 || !EQ (stop, glyph->object))
22108 break;
22109 row = prev;
22110 }
22111 }
22112
22113 *x = row->x;
22114 *y = row->y;
22115 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22116
22117 glyph = row->glyphs[TEXT_AREA];
22118 end = glyph + row->used[TEXT_AREA];
22119
22120 /* Skip over glyphs not having an object at the start of the row.
22121 These are special glyphs like truncation marks on terminal
22122 frames. */
22123 if (row->displays_text_p)
22124 while (glyph < end
22125 && INTEGERP (glyph->object)
22126 && !EQ (stop, glyph->object)
22127 && glyph->charpos < 0)
22128 {
22129 *x += glyph->pixel_width;
22130 ++glyph;
22131 }
22132
22133 while (glyph < end
22134 && !INTEGERP (glyph->object)
22135 && !EQ (stop, glyph->object)
22136 && (!BUFFERP (glyph->object)
22137 || glyph->charpos < charpos))
22138 {
22139 *x += glyph->pixel_width;
22140 ++glyph;
22141 }
22142
22143 *hpos = glyph - row->glyphs[TEXT_AREA];
22144 return !past_end;
22145 }
22146
22147 #else /* not 1 */
22148
22149 static int
22150 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22151 struct window *w;
22152 int pos;
22153 int *hpos, *vpos, *x, *y;
22154 Lisp_Object stop;
22155 {
22156 int i;
22157 int lastcol;
22158 int maybe_next_line_p = 0;
22159 int line_start_position;
22160 int yb = window_text_bottom_y (w);
22161 struct glyph_row *row, *best_row;
22162 int row_vpos, best_row_vpos;
22163 int current_x;
22164
22165 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22166 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22167
22168 while (row->y < yb)
22169 {
22170 if (row->used[TEXT_AREA])
22171 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22172 else
22173 line_start_position = 0;
22174
22175 if (line_start_position > pos)
22176 break;
22177 /* If the position sought is the end of the buffer,
22178 don't include the blank lines at the bottom of the window. */
22179 else if (line_start_position == pos
22180 && pos == BUF_ZV (XBUFFER (w->buffer)))
22181 {
22182 maybe_next_line_p = 1;
22183 break;
22184 }
22185 else if (line_start_position > 0)
22186 {
22187 best_row = row;
22188 best_row_vpos = row_vpos;
22189 }
22190
22191 if (row->y + row->height >= yb)
22192 break;
22193
22194 ++row;
22195 ++row_vpos;
22196 }
22197
22198 /* Find the right column within BEST_ROW. */
22199 lastcol = 0;
22200 current_x = best_row->x;
22201 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22202 {
22203 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22204 int charpos = glyph->charpos;
22205
22206 if (BUFFERP (glyph->object))
22207 {
22208 if (charpos == pos)
22209 {
22210 *hpos = i;
22211 *vpos = best_row_vpos;
22212 *x = current_x;
22213 *y = best_row->y;
22214 return 1;
22215 }
22216 else if (charpos > pos)
22217 break;
22218 }
22219 else if (EQ (glyph->object, stop))
22220 break;
22221
22222 if (charpos > 0)
22223 lastcol = i;
22224 current_x += glyph->pixel_width;
22225 }
22226
22227 /* If we're looking for the end of the buffer,
22228 and we didn't find it in the line we scanned,
22229 use the start of the following line. */
22230 if (maybe_next_line_p)
22231 {
22232 ++best_row;
22233 ++best_row_vpos;
22234 lastcol = 0;
22235 current_x = best_row->x;
22236 }
22237
22238 *vpos = best_row_vpos;
22239 *hpos = lastcol + 1;
22240 *x = current_x;
22241 *y = best_row->y;
22242 return 0;
22243 }
22244
22245 #endif /* not 1 */
22246
22247
22248 /* Find the position of the glyph for position POS in OBJECT in
22249 window W's current matrix, and return in *X, *Y the pixel
22250 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22251
22252 RIGHT_P non-zero means return the position of the right edge of the
22253 glyph, RIGHT_P zero means return the left edge position.
22254
22255 If no glyph for POS exists in the matrix, return the position of
22256 the glyph with the next smaller position that is in the matrix, if
22257 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22258 exists in the matrix, return the position of the glyph with the
22259 next larger position in OBJECT.
22260
22261 Value is non-zero if a glyph was found. */
22262
22263 static int
22264 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22265 struct window *w;
22266 int pos;
22267 Lisp_Object object;
22268 int *hpos, *vpos, *x, *y;
22269 int right_p;
22270 {
22271 int yb = window_text_bottom_y (w);
22272 struct glyph_row *r;
22273 struct glyph *best_glyph = NULL;
22274 struct glyph_row *best_row = NULL;
22275 int best_x = 0;
22276
22277 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22278 r->enabled_p && r->y < yb;
22279 ++r)
22280 {
22281 struct glyph *g = r->glyphs[TEXT_AREA];
22282 struct glyph *e = g + r->used[TEXT_AREA];
22283 int gx;
22284
22285 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22286 if (EQ (g->object, object))
22287 {
22288 if (g->charpos == pos)
22289 {
22290 best_glyph = g;
22291 best_x = gx;
22292 best_row = r;
22293 goto found;
22294 }
22295 else if (best_glyph == NULL
22296 || ((eabs (g->charpos - pos)
22297 < eabs (best_glyph->charpos - pos))
22298 && (right_p
22299 ? g->charpos < pos
22300 : g->charpos > pos)))
22301 {
22302 best_glyph = g;
22303 best_x = gx;
22304 best_row = r;
22305 }
22306 }
22307 }
22308
22309 found:
22310
22311 if (best_glyph)
22312 {
22313 *x = best_x;
22314 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22315
22316 if (right_p)
22317 {
22318 *x += best_glyph->pixel_width;
22319 ++*hpos;
22320 }
22321
22322 *y = best_row->y;
22323 *vpos = best_row - w->current_matrix->rows;
22324 }
22325
22326 return best_glyph != NULL;
22327 }
22328
22329
22330 /* See if position X, Y is within a hot-spot of an image. */
22331
22332 static int
22333 on_hot_spot_p (hot_spot, x, y)
22334 Lisp_Object hot_spot;
22335 int x, y;
22336 {
22337 if (!CONSP (hot_spot))
22338 return 0;
22339
22340 if (EQ (XCAR (hot_spot), Qrect))
22341 {
22342 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22343 Lisp_Object rect = XCDR (hot_spot);
22344 Lisp_Object tem;
22345 if (!CONSP (rect))
22346 return 0;
22347 if (!CONSP (XCAR (rect)))
22348 return 0;
22349 if (!CONSP (XCDR (rect)))
22350 return 0;
22351 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22352 return 0;
22353 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22354 return 0;
22355 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22356 return 0;
22357 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22358 return 0;
22359 return 1;
22360 }
22361 else if (EQ (XCAR (hot_spot), Qcircle))
22362 {
22363 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22364 Lisp_Object circ = XCDR (hot_spot);
22365 Lisp_Object lr, lx0, ly0;
22366 if (CONSP (circ)
22367 && CONSP (XCAR (circ))
22368 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22369 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22370 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22371 {
22372 double r = XFLOATINT (lr);
22373 double dx = XINT (lx0) - x;
22374 double dy = XINT (ly0) - y;
22375 return (dx * dx + dy * dy <= r * r);
22376 }
22377 }
22378 else if (EQ (XCAR (hot_spot), Qpoly))
22379 {
22380 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22381 if (VECTORP (XCDR (hot_spot)))
22382 {
22383 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22384 Lisp_Object *poly = v->contents;
22385 int n = v->size;
22386 int i;
22387 int inside = 0;
22388 Lisp_Object lx, ly;
22389 int x0, y0;
22390
22391 /* Need an even number of coordinates, and at least 3 edges. */
22392 if (n < 6 || n & 1)
22393 return 0;
22394
22395 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22396 If count is odd, we are inside polygon. Pixels on edges
22397 may or may not be included depending on actual geometry of the
22398 polygon. */
22399 if ((lx = poly[n-2], !INTEGERP (lx))
22400 || (ly = poly[n-1], !INTEGERP (lx)))
22401 return 0;
22402 x0 = XINT (lx), y0 = XINT (ly);
22403 for (i = 0; i < n; i += 2)
22404 {
22405 int x1 = x0, y1 = y0;
22406 if ((lx = poly[i], !INTEGERP (lx))
22407 || (ly = poly[i+1], !INTEGERP (ly)))
22408 return 0;
22409 x0 = XINT (lx), y0 = XINT (ly);
22410
22411 /* Does this segment cross the X line? */
22412 if (x0 >= x)
22413 {
22414 if (x1 >= x)
22415 continue;
22416 }
22417 else if (x1 < x)
22418 continue;
22419 if (y > y0 && y > y1)
22420 continue;
22421 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22422 inside = !inside;
22423 }
22424 return inside;
22425 }
22426 }
22427 return 0;
22428 }
22429
22430 Lisp_Object
22431 find_hot_spot (map, x, y)
22432 Lisp_Object map;
22433 int x, y;
22434 {
22435 while (CONSP (map))
22436 {
22437 if (CONSP (XCAR (map))
22438 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22439 return XCAR (map);
22440 map = XCDR (map);
22441 }
22442
22443 return Qnil;
22444 }
22445
22446 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22447 3, 3, 0,
22448 doc: /* Lookup in image map MAP coordinates X and Y.
22449 An image map is an alist where each element has the format (AREA ID PLIST).
22450 An AREA is specified as either a rectangle, a circle, or a polygon:
22451 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22452 pixel coordinates of the upper left and bottom right corners.
22453 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22454 and the radius of the circle; r may be a float or integer.
22455 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22456 vector describes one corner in the polygon.
22457 Returns the alist element for the first matching AREA in MAP. */)
22458 (map, x, y)
22459 Lisp_Object map;
22460 Lisp_Object x, y;
22461 {
22462 if (NILP (map))
22463 return Qnil;
22464
22465 CHECK_NUMBER (x);
22466 CHECK_NUMBER (y);
22467
22468 return find_hot_spot (map, XINT (x), XINT (y));
22469 }
22470
22471
22472 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22473 static void
22474 define_frame_cursor1 (f, cursor, pointer)
22475 struct frame *f;
22476 Cursor cursor;
22477 Lisp_Object pointer;
22478 {
22479 /* Do not change cursor shape while dragging mouse. */
22480 if (!NILP (do_mouse_tracking))
22481 return;
22482
22483 if (!NILP (pointer))
22484 {
22485 if (EQ (pointer, Qarrow))
22486 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22487 else if (EQ (pointer, Qhand))
22488 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22489 else if (EQ (pointer, Qtext))
22490 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22491 else if (EQ (pointer, intern ("hdrag")))
22492 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22493 #ifdef HAVE_X_WINDOWS
22494 else if (EQ (pointer, intern ("vdrag")))
22495 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22496 #endif
22497 else if (EQ (pointer, intern ("hourglass")))
22498 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22499 else if (EQ (pointer, Qmodeline))
22500 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22501 else
22502 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22503 }
22504
22505 if (cursor != No_Cursor)
22506 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22507 }
22508
22509 /* Take proper action when mouse has moved to the mode or header line
22510 or marginal area AREA of window W, x-position X and y-position Y.
22511 X is relative to the start of the text display area of W, so the
22512 width of bitmap areas and scroll bars must be subtracted to get a
22513 position relative to the start of the mode line. */
22514
22515 static void
22516 note_mode_line_or_margin_highlight (window, x, y, area)
22517 Lisp_Object window;
22518 int x, y;
22519 enum window_part area;
22520 {
22521 struct window *w = XWINDOW (window);
22522 struct frame *f = XFRAME (w->frame);
22523 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22524 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22525 Lisp_Object pointer = Qnil;
22526 int charpos, dx, dy, width, height;
22527 Lisp_Object string, object = Qnil;
22528 Lisp_Object pos, help;
22529
22530 Lisp_Object mouse_face;
22531 int original_x_pixel = x;
22532 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22533 struct glyph_row *row;
22534
22535 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22536 {
22537 int x0;
22538 struct glyph *end;
22539
22540 string = mode_line_string (w, area, &x, &y, &charpos,
22541 &object, &dx, &dy, &width, &height);
22542
22543 row = (area == ON_MODE_LINE
22544 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22545 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22546
22547 /* Find glyph */
22548 if (row->mode_line_p && row->enabled_p)
22549 {
22550 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22551 end = glyph + row->used[TEXT_AREA];
22552
22553 for (x0 = original_x_pixel;
22554 glyph < end && x0 >= glyph->pixel_width;
22555 ++glyph)
22556 x0 -= glyph->pixel_width;
22557
22558 if (glyph >= end)
22559 glyph = NULL;
22560 }
22561 }
22562 else
22563 {
22564 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22565 string = marginal_area_string (w, area, &x, &y, &charpos,
22566 &object, &dx, &dy, &width, &height);
22567 }
22568
22569 help = Qnil;
22570
22571 if (IMAGEP (object))
22572 {
22573 Lisp_Object image_map, hotspot;
22574 if ((image_map = Fplist_get (XCDR (object), QCmap),
22575 !NILP (image_map))
22576 && (hotspot = find_hot_spot (image_map, dx, dy),
22577 CONSP (hotspot))
22578 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22579 {
22580 Lisp_Object area_id, plist;
22581
22582 area_id = XCAR (hotspot);
22583 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22584 If so, we could look for mouse-enter, mouse-leave
22585 properties in PLIST (and do something...). */
22586 hotspot = XCDR (hotspot);
22587 if (CONSP (hotspot)
22588 && (plist = XCAR (hotspot), CONSP (plist)))
22589 {
22590 pointer = Fplist_get (plist, Qpointer);
22591 if (NILP (pointer))
22592 pointer = Qhand;
22593 help = Fplist_get (plist, Qhelp_echo);
22594 if (!NILP (help))
22595 {
22596 help_echo_string = help;
22597 /* Is this correct? ++kfs */
22598 XSETWINDOW (help_echo_window, w);
22599 help_echo_object = w->buffer;
22600 help_echo_pos = charpos;
22601 }
22602 }
22603 }
22604 if (NILP (pointer))
22605 pointer = Fplist_get (XCDR (object), QCpointer);
22606 }
22607
22608 if (STRINGP (string))
22609 {
22610 pos = make_number (charpos);
22611 /* If we're on a string with `help-echo' text property, arrange
22612 for the help to be displayed. This is done by setting the
22613 global variable help_echo_string to the help string. */
22614 if (NILP (help))
22615 {
22616 help = Fget_text_property (pos, Qhelp_echo, string);
22617 if (!NILP (help))
22618 {
22619 help_echo_string = help;
22620 XSETWINDOW (help_echo_window, w);
22621 help_echo_object = string;
22622 help_echo_pos = charpos;
22623 }
22624 }
22625
22626 if (NILP (pointer))
22627 pointer = Fget_text_property (pos, Qpointer, string);
22628
22629 /* Change the mouse pointer according to what is under X/Y. */
22630 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22631 {
22632 Lisp_Object map;
22633 map = Fget_text_property (pos, Qlocal_map, string);
22634 if (!KEYMAPP (map))
22635 map = Fget_text_property (pos, Qkeymap, string);
22636 if (!KEYMAPP (map))
22637 cursor = dpyinfo->vertical_scroll_bar_cursor;
22638 }
22639
22640 /* Change the mouse face according to what is under X/Y. */
22641 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22642 if (!NILP (mouse_face)
22643 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22644 && glyph)
22645 {
22646 Lisp_Object b, e;
22647
22648 struct glyph * tmp_glyph;
22649
22650 int gpos;
22651 int gseq_length;
22652 int total_pixel_width;
22653 int ignore;
22654
22655 int vpos, hpos;
22656
22657 b = Fprevious_single_property_change (make_number (charpos + 1),
22658 Qmouse_face, string, Qnil);
22659 if (NILP (b))
22660 b = make_number (0);
22661
22662 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22663 if (NILP (e))
22664 e = make_number (SCHARS (string));
22665
22666 /* Calculate the position(glyph position: GPOS) of GLYPH in
22667 displayed string. GPOS is different from CHARPOS.
22668
22669 CHARPOS is the position of glyph in internal string
22670 object. A mode line string format has structures which
22671 is converted to a flatten by emacs lisp interpreter.
22672 The internal string is an element of the structures.
22673 The displayed string is the flatten string. */
22674 gpos = 0;
22675 if (glyph > row_start_glyph)
22676 {
22677 tmp_glyph = glyph - 1;
22678 while (tmp_glyph >= row_start_glyph
22679 && tmp_glyph->charpos >= XINT (b)
22680 && EQ (tmp_glyph->object, glyph->object))
22681 {
22682 tmp_glyph--;
22683 gpos++;
22684 }
22685 }
22686
22687 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22688 displayed string holding GLYPH.
22689
22690 GSEQ_LENGTH is different from SCHARS (STRING).
22691 SCHARS (STRING) returns the length of the internal string. */
22692 for (tmp_glyph = glyph, gseq_length = gpos;
22693 tmp_glyph->charpos < XINT (e);
22694 tmp_glyph++, gseq_length++)
22695 {
22696 if (!EQ (tmp_glyph->object, glyph->object))
22697 break;
22698 }
22699
22700 total_pixel_width = 0;
22701 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22702 total_pixel_width += tmp_glyph->pixel_width;
22703
22704 /* Pre calculation of re-rendering position */
22705 vpos = (x - gpos);
22706 hpos = (area == ON_MODE_LINE
22707 ? (w->current_matrix)->nrows - 1
22708 : 0);
22709
22710 /* If the re-rendering position is included in the last
22711 re-rendering area, we should do nothing. */
22712 if ( EQ (window, dpyinfo->mouse_face_window)
22713 && dpyinfo->mouse_face_beg_col <= vpos
22714 && vpos < dpyinfo->mouse_face_end_col
22715 && dpyinfo->mouse_face_beg_row == hpos )
22716 return;
22717
22718 if (clear_mouse_face (dpyinfo))
22719 cursor = No_Cursor;
22720
22721 dpyinfo->mouse_face_beg_col = vpos;
22722 dpyinfo->mouse_face_beg_row = hpos;
22723
22724 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22725 dpyinfo->mouse_face_beg_y = 0;
22726
22727 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22728 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22729
22730 dpyinfo->mouse_face_end_x = 0;
22731 dpyinfo->mouse_face_end_y = 0;
22732
22733 dpyinfo->mouse_face_past_end = 0;
22734 dpyinfo->mouse_face_window = window;
22735
22736 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22737 charpos,
22738 0, 0, 0, &ignore,
22739 glyph->face_id, 1);
22740 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22741
22742 if (NILP (pointer))
22743 pointer = Qhand;
22744 }
22745 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22746 clear_mouse_face (dpyinfo);
22747 }
22748 define_frame_cursor1 (f, cursor, pointer);
22749 }
22750
22751
22752 /* EXPORT:
22753 Take proper action when the mouse has moved to position X, Y on
22754 frame F as regards highlighting characters that have mouse-face
22755 properties. Also de-highlighting chars where the mouse was before.
22756 X and Y can be negative or out of range. */
22757
22758 void
22759 note_mouse_highlight (f, x, y)
22760 struct frame *f;
22761 int x, y;
22762 {
22763 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22764 enum window_part part;
22765 Lisp_Object window;
22766 struct window *w;
22767 Cursor cursor = No_Cursor;
22768 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22769 struct buffer *b;
22770
22771 /* When a menu is active, don't highlight because this looks odd. */
22772 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22773 if (popup_activated ())
22774 return;
22775 #endif
22776
22777 if (NILP (Vmouse_highlight)
22778 || !f->glyphs_initialized_p)
22779 return;
22780
22781 dpyinfo->mouse_face_mouse_x = x;
22782 dpyinfo->mouse_face_mouse_y = y;
22783 dpyinfo->mouse_face_mouse_frame = f;
22784
22785 if (dpyinfo->mouse_face_defer)
22786 return;
22787
22788 if (gc_in_progress)
22789 {
22790 dpyinfo->mouse_face_deferred_gc = 1;
22791 return;
22792 }
22793
22794 /* Which window is that in? */
22795 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22796
22797 /* If we were displaying active text in another window, clear that.
22798 Also clear if we move out of text area in same window. */
22799 if (! EQ (window, dpyinfo->mouse_face_window)
22800 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22801 && !NILP (dpyinfo->mouse_face_window)))
22802 clear_mouse_face (dpyinfo);
22803
22804 /* Not on a window -> return. */
22805 if (!WINDOWP (window))
22806 return;
22807
22808 /* Reset help_echo_string. It will get recomputed below. */
22809 help_echo_string = Qnil;
22810
22811 /* Convert to window-relative pixel coordinates. */
22812 w = XWINDOW (window);
22813 frame_to_window_pixel_xy (w, &x, &y);
22814
22815 /* Handle tool-bar window differently since it doesn't display a
22816 buffer. */
22817 if (EQ (window, f->tool_bar_window))
22818 {
22819 note_tool_bar_highlight (f, x, y);
22820 return;
22821 }
22822
22823 /* Mouse is on the mode, header line or margin? */
22824 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22825 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22826 {
22827 note_mode_line_or_margin_highlight (window, x, y, part);
22828 return;
22829 }
22830
22831 if (part == ON_VERTICAL_BORDER)
22832 {
22833 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22834 help_echo_string = build_string ("drag-mouse-1: resize");
22835 }
22836 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22837 || part == ON_SCROLL_BAR)
22838 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22839 else
22840 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22841
22842 /* Are we in a window whose display is up to date?
22843 And verify the buffer's text has not changed. */
22844 b = XBUFFER (w->buffer);
22845 if (part == ON_TEXT
22846 && EQ (w->window_end_valid, w->buffer)
22847 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22848 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22849 {
22850 int hpos, vpos, pos, i, dx, dy, area;
22851 struct glyph *glyph;
22852 Lisp_Object object;
22853 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22854 Lisp_Object *overlay_vec = NULL;
22855 int noverlays;
22856 struct buffer *obuf;
22857 int obegv, ozv, same_region;
22858
22859 /* Find the glyph under X/Y. */
22860 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22861
22862 /* Look for :pointer property on image. */
22863 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22864 {
22865 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22866 if (img != NULL && IMAGEP (img->spec))
22867 {
22868 Lisp_Object image_map, hotspot;
22869 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22870 !NILP (image_map))
22871 && (hotspot = find_hot_spot (image_map,
22872 glyph->slice.x + dx,
22873 glyph->slice.y + dy),
22874 CONSP (hotspot))
22875 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22876 {
22877 Lisp_Object area_id, plist;
22878
22879 area_id = XCAR (hotspot);
22880 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22881 If so, we could look for mouse-enter, mouse-leave
22882 properties in PLIST (and do something...). */
22883 hotspot = XCDR (hotspot);
22884 if (CONSP (hotspot)
22885 && (plist = XCAR (hotspot), CONSP (plist)))
22886 {
22887 pointer = Fplist_get (plist, Qpointer);
22888 if (NILP (pointer))
22889 pointer = Qhand;
22890 help_echo_string = Fplist_get (plist, Qhelp_echo);
22891 if (!NILP (help_echo_string))
22892 {
22893 help_echo_window = window;
22894 help_echo_object = glyph->object;
22895 help_echo_pos = glyph->charpos;
22896 }
22897 }
22898 }
22899 if (NILP (pointer))
22900 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22901 }
22902 }
22903
22904 /* Clear mouse face if X/Y not over text. */
22905 if (glyph == NULL
22906 || area != TEXT_AREA
22907 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22908 {
22909 if (clear_mouse_face (dpyinfo))
22910 cursor = No_Cursor;
22911 if (NILP (pointer))
22912 {
22913 if (area != TEXT_AREA)
22914 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22915 else
22916 pointer = Vvoid_text_area_pointer;
22917 }
22918 goto set_cursor;
22919 }
22920
22921 pos = glyph->charpos;
22922 object = glyph->object;
22923 if (!STRINGP (object) && !BUFFERP (object))
22924 goto set_cursor;
22925
22926 /* If we get an out-of-range value, return now; avoid an error. */
22927 if (BUFFERP (object) && pos > BUF_Z (b))
22928 goto set_cursor;
22929
22930 /* Make the window's buffer temporarily current for
22931 overlays_at and compute_char_face. */
22932 obuf = current_buffer;
22933 current_buffer = b;
22934 obegv = BEGV;
22935 ozv = ZV;
22936 BEGV = BEG;
22937 ZV = Z;
22938
22939 /* Is this char mouse-active or does it have help-echo? */
22940 position = make_number (pos);
22941
22942 if (BUFFERP (object))
22943 {
22944 /* Put all the overlays we want in a vector in overlay_vec. */
22945 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22946 /* Sort overlays into increasing priority order. */
22947 noverlays = sort_overlays (overlay_vec, noverlays, w);
22948 }
22949 else
22950 noverlays = 0;
22951
22952 same_region = (EQ (window, dpyinfo->mouse_face_window)
22953 && vpos >= dpyinfo->mouse_face_beg_row
22954 && vpos <= dpyinfo->mouse_face_end_row
22955 && (vpos > dpyinfo->mouse_face_beg_row
22956 || hpos >= dpyinfo->mouse_face_beg_col)
22957 && (vpos < dpyinfo->mouse_face_end_row
22958 || hpos < dpyinfo->mouse_face_end_col
22959 || dpyinfo->mouse_face_past_end));
22960
22961 if (same_region)
22962 cursor = No_Cursor;
22963
22964 /* Check mouse-face highlighting. */
22965 if (! same_region
22966 /* If there exists an overlay with mouse-face overlapping
22967 the one we are currently highlighting, we have to
22968 check if we enter the overlapping overlay, and then
22969 highlight only that. */
22970 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22971 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22972 {
22973 /* Find the highest priority overlay that has a mouse-face
22974 property. */
22975 overlay = Qnil;
22976 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22977 {
22978 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22979 if (!NILP (mouse_face))
22980 overlay = overlay_vec[i];
22981 }
22982
22983 /* If we're actually highlighting the same overlay as
22984 before, there's no need to do that again. */
22985 if (!NILP (overlay)
22986 && EQ (overlay, dpyinfo->mouse_face_overlay))
22987 goto check_help_echo;
22988
22989 dpyinfo->mouse_face_overlay = overlay;
22990
22991 /* Clear the display of the old active region, if any. */
22992 if (clear_mouse_face (dpyinfo))
22993 cursor = No_Cursor;
22994
22995 /* If no overlay applies, get a text property. */
22996 if (NILP (overlay))
22997 mouse_face = Fget_text_property (position, Qmouse_face, object);
22998
22999 /* Handle the overlay case. */
23000 if (!NILP (overlay))
23001 {
23002 /* Find the range of text around this char that
23003 should be active. */
23004 Lisp_Object before, after;
23005 int ignore;
23006
23007 before = Foverlay_start (overlay);
23008 after = Foverlay_end (overlay);
23009 /* Record this as the current active region. */
23010 fast_find_position (w, XFASTINT (before),
23011 &dpyinfo->mouse_face_beg_col,
23012 &dpyinfo->mouse_face_beg_row,
23013 &dpyinfo->mouse_face_beg_x,
23014 &dpyinfo->mouse_face_beg_y, Qnil);
23015
23016 dpyinfo->mouse_face_past_end
23017 = !fast_find_position (w, XFASTINT (after),
23018 &dpyinfo->mouse_face_end_col,
23019 &dpyinfo->mouse_face_end_row,
23020 &dpyinfo->mouse_face_end_x,
23021 &dpyinfo->mouse_face_end_y, Qnil);
23022 dpyinfo->mouse_face_window = window;
23023
23024 dpyinfo->mouse_face_face_id
23025 = face_at_buffer_position (w, pos, 0, 0,
23026 &ignore, pos + 1,
23027 !dpyinfo->mouse_face_hidden);
23028
23029 /* Display it as active. */
23030 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23031 cursor = No_Cursor;
23032 }
23033 /* Handle the text property case. */
23034 else if (!NILP (mouse_face) && BUFFERP (object))
23035 {
23036 /* Find the range of text around this char that
23037 should be active. */
23038 Lisp_Object before, after, beginning, end;
23039 int ignore;
23040
23041 beginning = Fmarker_position (w->start);
23042 end = make_number (BUF_Z (XBUFFER (object))
23043 - XFASTINT (w->window_end_pos));
23044 before
23045 = Fprevious_single_property_change (make_number (pos + 1),
23046 Qmouse_face,
23047 object, beginning);
23048 after
23049 = Fnext_single_property_change (position, Qmouse_face,
23050 object, end);
23051
23052 /* Record this as the current active region. */
23053 fast_find_position (w, XFASTINT (before),
23054 &dpyinfo->mouse_face_beg_col,
23055 &dpyinfo->mouse_face_beg_row,
23056 &dpyinfo->mouse_face_beg_x,
23057 &dpyinfo->mouse_face_beg_y, Qnil);
23058 dpyinfo->mouse_face_past_end
23059 = !fast_find_position (w, XFASTINT (after),
23060 &dpyinfo->mouse_face_end_col,
23061 &dpyinfo->mouse_face_end_row,
23062 &dpyinfo->mouse_face_end_x,
23063 &dpyinfo->mouse_face_end_y, Qnil);
23064 dpyinfo->mouse_face_window = window;
23065
23066 if (BUFFERP (object))
23067 dpyinfo->mouse_face_face_id
23068 = face_at_buffer_position (w, pos, 0, 0,
23069 &ignore, pos + 1,
23070 !dpyinfo->mouse_face_hidden);
23071
23072 /* Display it as active. */
23073 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23074 cursor = No_Cursor;
23075 }
23076 else if (!NILP (mouse_face) && STRINGP (object))
23077 {
23078 Lisp_Object b, e;
23079 int ignore;
23080
23081 b = Fprevious_single_property_change (make_number (pos + 1),
23082 Qmouse_face,
23083 object, Qnil);
23084 e = Fnext_single_property_change (position, Qmouse_face,
23085 object, Qnil);
23086 if (NILP (b))
23087 b = make_number (0);
23088 if (NILP (e))
23089 e = make_number (SCHARS (object) - 1);
23090
23091 fast_find_string_pos (w, XINT (b), object,
23092 &dpyinfo->mouse_face_beg_col,
23093 &dpyinfo->mouse_face_beg_row,
23094 &dpyinfo->mouse_face_beg_x,
23095 &dpyinfo->mouse_face_beg_y, 0);
23096 fast_find_string_pos (w, XINT (e), object,
23097 &dpyinfo->mouse_face_end_col,
23098 &dpyinfo->mouse_face_end_row,
23099 &dpyinfo->mouse_face_end_x,
23100 &dpyinfo->mouse_face_end_y, 1);
23101 dpyinfo->mouse_face_past_end = 0;
23102 dpyinfo->mouse_face_window = window;
23103 dpyinfo->mouse_face_face_id
23104 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23105 glyph->face_id, 1);
23106 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23107 cursor = No_Cursor;
23108 }
23109 else if (STRINGP (object) && NILP (mouse_face))
23110 {
23111 /* A string which doesn't have mouse-face, but
23112 the text ``under'' it might have. */
23113 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23114 int start = MATRIX_ROW_START_CHARPOS (r);
23115
23116 pos = string_buffer_position (w, object, start);
23117 if (pos > 0)
23118 mouse_face = get_char_property_and_overlay (make_number (pos),
23119 Qmouse_face,
23120 w->buffer,
23121 &overlay);
23122 if (!NILP (mouse_face) && !NILP (overlay))
23123 {
23124 Lisp_Object before = Foverlay_start (overlay);
23125 Lisp_Object after = Foverlay_end (overlay);
23126 int ignore;
23127
23128 /* Note that we might not be able to find position
23129 BEFORE in the glyph matrix if the overlay is
23130 entirely covered by a `display' property. In
23131 this case, we overshoot. So let's stop in
23132 the glyph matrix before glyphs for OBJECT. */
23133 fast_find_position (w, XFASTINT (before),
23134 &dpyinfo->mouse_face_beg_col,
23135 &dpyinfo->mouse_face_beg_row,
23136 &dpyinfo->mouse_face_beg_x,
23137 &dpyinfo->mouse_face_beg_y,
23138 object);
23139
23140 dpyinfo->mouse_face_past_end
23141 = !fast_find_position (w, XFASTINT (after),
23142 &dpyinfo->mouse_face_end_col,
23143 &dpyinfo->mouse_face_end_row,
23144 &dpyinfo->mouse_face_end_x,
23145 &dpyinfo->mouse_face_end_y,
23146 Qnil);
23147 dpyinfo->mouse_face_window = window;
23148 dpyinfo->mouse_face_face_id
23149 = face_at_buffer_position (w, pos, 0, 0,
23150 &ignore, pos + 1,
23151 !dpyinfo->mouse_face_hidden);
23152
23153 /* Display it as active. */
23154 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23155 cursor = No_Cursor;
23156 }
23157 }
23158 }
23159
23160 check_help_echo:
23161
23162 /* Look for a `help-echo' property. */
23163 if (NILP (help_echo_string)) {
23164 Lisp_Object help, overlay;
23165
23166 /* Check overlays first. */
23167 help = overlay = Qnil;
23168 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23169 {
23170 overlay = overlay_vec[i];
23171 help = Foverlay_get (overlay, Qhelp_echo);
23172 }
23173
23174 if (!NILP (help))
23175 {
23176 help_echo_string = help;
23177 help_echo_window = window;
23178 help_echo_object = overlay;
23179 help_echo_pos = pos;
23180 }
23181 else
23182 {
23183 Lisp_Object object = glyph->object;
23184 int charpos = glyph->charpos;
23185
23186 /* Try text properties. */
23187 if (STRINGP (object)
23188 && charpos >= 0
23189 && charpos < SCHARS (object))
23190 {
23191 help = Fget_text_property (make_number (charpos),
23192 Qhelp_echo, object);
23193 if (NILP (help))
23194 {
23195 /* If the string itself doesn't specify a help-echo,
23196 see if the buffer text ``under'' it does. */
23197 struct glyph_row *r
23198 = MATRIX_ROW (w->current_matrix, vpos);
23199 int start = MATRIX_ROW_START_CHARPOS (r);
23200 int pos = string_buffer_position (w, object, start);
23201 if (pos > 0)
23202 {
23203 help = Fget_char_property (make_number (pos),
23204 Qhelp_echo, w->buffer);
23205 if (!NILP (help))
23206 {
23207 charpos = pos;
23208 object = w->buffer;
23209 }
23210 }
23211 }
23212 }
23213 else if (BUFFERP (object)
23214 && charpos >= BEGV
23215 && charpos < ZV)
23216 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23217 object);
23218
23219 if (!NILP (help))
23220 {
23221 help_echo_string = help;
23222 help_echo_window = window;
23223 help_echo_object = object;
23224 help_echo_pos = charpos;
23225 }
23226 }
23227 }
23228
23229 /* Look for a `pointer' property. */
23230 if (NILP (pointer))
23231 {
23232 /* Check overlays first. */
23233 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23234 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23235
23236 if (NILP (pointer))
23237 {
23238 Lisp_Object object = glyph->object;
23239 int charpos = glyph->charpos;
23240
23241 /* Try text properties. */
23242 if (STRINGP (object)
23243 && charpos >= 0
23244 && charpos < SCHARS (object))
23245 {
23246 pointer = Fget_text_property (make_number (charpos),
23247 Qpointer, object);
23248 if (NILP (pointer))
23249 {
23250 /* If the string itself doesn't specify a pointer,
23251 see if the buffer text ``under'' it does. */
23252 struct glyph_row *r
23253 = MATRIX_ROW (w->current_matrix, vpos);
23254 int start = MATRIX_ROW_START_CHARPOS (r);
23255 int pos = string_buffer_position (w, object, start);
23256 if (pos > 0)
23257 pointer = Fget_char_property (make_number (pos),
23258 Qpointer, w->buffer);
23259 }
23260 }
23261 else if (BUFFERP (object)
23262 && charpos >= BEGV
23263 && charpos < ZV)
23264 pointer = Fget_text_property (make_number (charpos),
23265 Qpointer, object);
23266 }
23267 }
23268
23269 BEGV = obegv;
23270 ZV = ozv;
23271 current_buffer = obuf;
23272 }
23273
23274 set_cursor:
23275
23276 define_frame_cursor1 (f, cursor, pointer);
23277 }
23278
23279
23280 /* EXPORT for RIF:
23281 Clear any mouse-face on window W. This function is part of the
23282 redisplay interface, and is called from try_window_id and similar
23283 functions to ensure the mouse-highlight is off. */
23284
23285 void
23286 x_clear_window_mouse_face (w)
23287 struct window *w;
23288 {
23289 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23290 Lisp_Object window;
23291
23292 BLOCK_INPUT;
23293 XSETWINDOW (window, w);
23294 if (EQ (window, dpyinfo->mouse_face_window))
23295 clear_mouse_face (dpyinfo);
23296 UNBLOCK_INPUT;
23297 }
23298
23299
23300 /* EXPORT:
23301 Just discard the mouse face information for frame F, if any.
23302 This is used when the size of F is changed. */
23303
23304 void
23305 cancel_mouse_face (f)
23306 struct frame *f;
23307 {
23308 Lisp_Object window;
23309 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23310
23311 window = dpyinfo->mouse_face_window;
23312 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23313 {
23314 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23315 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23316 dpyinfo->mouse_face_window = Qnil;
23317 }
23318 }
23319
23320
23321 #endif /* HAVE_WINDOW_SYSTEM */
23322
23323 \f
23324 /***********************************************************************
23325 Exposure Events
23326 ***********************************************************************/
23327
23328 #ifdef HAVE_WINDOW_SYSTEM
23329
23330 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23331 which intersects rectangle R. R is in window-relative coordinates. */
23332
23333 static void
23334 expose_area (w, row, r, area)
23335 struct window *w;
23336 struct glyph_row *row;
23337 XRectangle *r;
23338 enum glyph_row_area area;
23339 {
23340 struct glyph *first = row->glyphs[area];
23341 struct glyph *end = row->glyphs[area] + row->used[area];
23342 struct glyph *last;
23343 int first_x, start_x, x;
23344
23345 if (area == TEXT_AREA && row->fill_line_p)
23346 /* If row extends face to end of line write the whole line. */
23347 draw_glyphs (w, 0, row, area,
23348 0, row->used[area],
23349 DRAW_NORMAL_TEXT, 0);
23350 else
23351 {
23352 /* Set START_X to the window-relative start position for drawing glyphs of
23353 AREA. The first glyph of the text area can be partially visible.
23354 The first glyphs of other areas cannot. */
23355 start_x = window_box_left_offset (w, area);
23356 x = start_x;
23357 if (area == TEXT_AREA)
23358 x += row->x;
23359
23360 /* Find the first glyph that must be redrawn. */
23361 while (first < end
23362 && x + first->pixel_width < r->x)
23363 {
23364 x += first->pixel_width;
23365 ++first;
23366 }
23367
23368 /* Find the last one. */
23369 last = first;
23370 first_x = x;
23371 while (last < end
23372 && x < r->x + r->width)
23373 {
23374 x += last->pixel_width;
23375 ++last;
23376 }
23377
23378 /* Repaint. */
23379 if (last > first)
23380 draw_glyphs (w, first_x - start_x, row, area,
23381 first - row->glyphs[area], last - row->glyphs[area],
23382 DRAW_NORMAL_TEXT, 0);
23383 }
23384 }
23385
23386
23387 /* Redraw the parts of the glyph row ROW on window W intersecting
23388 rectangle R. R is in window-relative coordinates. Value is
23389 non-zero if mouse-face was overwritten. */
23390
23391 static int
23392 expose_line (w, row, r)
23393 struct window *w;
23394 struct glyph_row *row;
23395 XRectangle *r;
23396 {
23397 xassert (row->enabled_p);
23398
23399 if (row->mode_line_p || w->pseudo_window_p)
23400 draw_glyphs (w, 0, row, TEXT_AREA,
23401 0, row->used[TEXT_AREA],
23402 DRAW_NORMAL_TEXT, 0);
23403 else
23404 {
23405 if (row->used[LEFT_MARGIN_AREA])
23406 expose_area (w, row, r, LEFT_MARGIN_AREA);
23407 if (row->used[TEXT_AREA])
23408 expose_area (w, row, r, TEXT_AREA);
23409 if (row->used[RIGHT_MARGIN_AREA])
23410 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23411 draw_row_fringe_bitmaps (w, row);
23412 }
23413
23414 return row->mouse_face_p;
23415 }
23416
23417
23418 /* Redraw those parts of glyphs rows during expose event handling that
23419 overlap other rows. Redrawing of an exposed line writes over parts
23420 of lines overlapping that exposed line; this function fixes that.
23421
23422 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23423 row in W's current matrix that is exposed and overlaps other rows.
23424 LAST_OVERLAPPING_ROW is the last such row. */
23425
23426 static void
23427 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23428 struct window *w;
23429 struct glyph_row *first_overlapping_row;
23430 struct glyph_row *last_overlapping_row;
23431 {
23432 struct glyph_row *row;
23433
23434 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23435 if (row->overlapping_p)
23436 {
23437 xassert (row->enabled_p && !row->mode_line_p);
23438
23439 if (row->used[LEFT_MARGIN_AREA])
23440 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23441
23442 if (row->used[TEXT_AREA])
23443 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23444
23445 if (row->used[RIGHT_MARGIN_AREA])
23446 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23447 }
23448 }
23449
23450
23451 /* Return non-zero if W's cursor intersects rectangle R. */
23452
23453 static int
23454 phys_cursor_in_rect_p (w, r)
23455 struct window *w;
23456 XRectangle *r;
23457 {
23458 XRectangle cr, result;
23459 struct glyph *cursor_glyph;
23460
23461 cursor_glyph = get_phys_cursor_glyph (w);
23462 if (cursor_glyph)
23463 {
23464 /* r is relative to W's box, but w->phys_cursor.x is relative
23465 to left edge of W's TEXT area. Adjust it. */
23466 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23467 cr.y = w->phys_cursor.y;
23468 cr.width = cursor_glyph->pixel_width;
23469 cr.height = w->phys_cursor_height;
23470 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23471 I assume the effect is the same -- and this is portable. */
23472 return x_intersect_rectangles (&cr, r, &result);
23473 }
23474 /* If we don't understand the format, pretend we're not in the hot-spot. */
23475 return 0;
23476 }
23477
23478
23479 /* EXPORT:
23480 Draw a vertical window border to the right of window W if W doesn't
23481 have vertical scroll bars. */
23482
23483 void
23484 x_draw_vertical_border (w)
23485 struct window *w;
23486 {
23487 struct frame *f = XFRAME (WINDOW_FRAME (w));
23488
23489 /* We could do better, if we knew what type of scroll-bar the adjacent
23490 windows (on either side) have... But we don't :-(
23491 However, I think this works ok. ++KFS 2003-04-25 */
23492
23493 /* Redraw borders between horizontally adjacent windows. Don't
23494 do it for frames with vertical scroll bars because either the
23495 right scroll bar of a window, or the left scroll bar of its
23496 neighbor will suffice as a border. */
23497 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23498 return;
23499
23500 if (!WINDOW_RIGHTMOST_P (w)
23501 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23502 {
23503 int x0, x1, y0, y1;
23504
23505 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23506 y1 -= 1;
23507
23508 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23509 x1 -= 1;
23510
23511 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23512 }
23513 else if (!WINDOW_LEFTMOST_P (w)
23514 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23515 {
23516 int x0, x1, y0, y1;
23517
23518 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23519 y1 -= 1;
23520
23521 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23522 x0 -= 1;
23523
23524 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23525 }
23526 }
23527
23528
23529 /* Redraw the part of window W intersection rectangle FR. Pixel
23530 coordinates in FR are frame-relative. Call this function with
23531 input blocked. Value is non-zero if the exposure overwrites
23532 mouse-face. */
23533
23534 static int
23535 expose_window (w, fr)
23536 struct window *w;
23537 XRectangle *fr;
23538 {
23539 struct frame *f = XFRAME (w->frame);
23540 XRectangle wr, r;
23541 int mouse_face_overwritten_p = 0;
23542
23543 /* If window is not yet fully initialized, do nothing. This can
23544 happen when toolkit scroll bars are used and a window is split.
23545 Reconfiguring the scroll bar will generate an expose for a newly
23546 created window. */
23547 if (w->current_matrix == NULL)
23548 return 0;
23549
23550 /* When we're currently updating the window, display and current
23551 matrix usually don't agree. Arrange for a thorough display
23552 later. */
23553 if (w == updated_window)
23554 {
23555 SET_FRAME_GARBAGED (f);
23556 return 0;
23557 }
23558
23559 /* Frame-relative pixel rectangle of W. */
23560 wr.x = WINDOW_LEFT_EDGE_X (w);
23561 wr.y = WINDOW_TOP_EDGE_Y (w);
23562 wr.width = WINDOW_TOTAL_WIDTH (w);
23563 wr.height = WINDOW_TOTAL_HEIGHT (w);
23564
23565 if (x_intersect_rectangles (fr, &wr, &r))
23566 {
23567 int yb = window_text_bottom_y (w);
23568 struct glyph_row *row;
23569 int cursor_cleared_p;
23570 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23571
23572 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23573 r.x, r.y, r.width, r.height));
23574
23575 /* Convert to window coordinates. */
23576 r.x -= WINDOW_LEFT_EDGE_X (w);
23577 r.y -= WINDOW_TOP_EDGE_Y (w);
23578
23579 /* Turn off the cursor. */
23580 if (!w->pseudo_window_p
23581 && phys_cursor_in_rect_p (w, &r))
23582 {
23583 x_clear_cursor (w);
23584 cursor_cleared_p = 1;
23585 }
23586 else
23587 cursor_cleared_p = 0;
23588
23589 /* Update lines intersecting rectangle R. */
23590 first_overlapping_row = last_overlapping_row = NULL;
23591 for (row = w->current_matrix->rows;
23592 row->enabled_p;
23593 ++row)
23594 {
23595 int y0 = row->y;
23596 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23597
23598 if ((y0 >= r.y && y0 < r.y + r.height)
23599 || (y1 > r.y && y1 < r.y + r.height)
23600 || (r.y >= y0 && r.y < y1)
23601 || (r.y + r.height > y0 && r.y + r.height < y1))
23602 {
23603 /* A header line may be overlapping, but there is no need
23604 to fix overlapping areas for them. KFS 2005-02-12 */
23605 if (row->overlapping_p && !row->mode_line_p)
23606 {
23607 if (first_overlapping_row == NULL)
23608 first_overlapping_row = row;
23609 last_overlapping_row = row;
23610 }
23611
23612 if (expose_line (w, row, &r))
23613 mouse_face_overwritten_p = 1;
23614 }
23615
23616 if (y1 >= yb)
23617 break;
23618 }
23619
23620 /* Display the mode line if there is one. */
23621 if (WINDOW_WANTS_MODELINE_P (w)
23622 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23623 row->enabled_p)
23624 && row->y < r.y + r.height)
23625 {
23626 if (expose_line (w, row, &r))
23627 mouse_face_overwritten_p = 1;
23628 }
23629
23630 if (!w->pseudo_window_p)
23631 {
23632 /* Fix the display of overlapping rows. */
23633 if (first_overlapping_row)
23634 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23635
23636 /* Draw border between windows. */
23637 x_draw_vertical_border (w);
23638
23639 /* Turn the cursor on again. */
23640 if (cursor_cleared_p)
23641 update_window_cursor (w, 1);
23642 }
23643 }
23644
23645 return mouse_face_overwritten_p;
23646 }
23647
23648
23649
23650 /* Redraw (parts) of all windows in the window tree rooted at W that
23651 intersect R. R contains frame pixel coordinates. Value is
23652 non-zero if the exposure overwrites mouse-face. */
23653
23654 static int
23655 expose_window_tree (w, r)
23656 struct window *w;
23657 XRectangle *r;
23658 {
23659 struct frame *f = XFRAME (w->frame);
23660 int mouse_face_overwritten_p = 0;
23661
23662 while (w && !FRAME_GARBAGED_P (f))
23663 {
23664 if (!NILP (w->hchild))
23665 mouse_face_overwritten_p
23666 |= expose_window_tree (XWINDOW (w->hchild), r);
23667 else if (!NILP (w->vchild))
23668 mouse_face_overwritten_p
23669 |= expose_window_tree (XWINDOW (w->vchild), r);
23670 else
23671 mouse_face_overwritten_p |= expose_window (w, r);
23672
23673 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23674 }
23675
23676 return mouse_face_overwritten_p;
23677 }
23678
23679
23680 /* EXPORT:
23681 Redisplay an exposed area of frame F. X and Y are the upper-left
23682 corner of the exposed rectangle. W and H are width and height of
23683 the exposed area. All are pixel values. W or H zero means redraw
23684 the entire frame. */
23685
23686 void
23687 expose_frame (f, x, y, w, h)
23688 struct frame *f;
23689 int x, y, w, h;
23690 {
23691 XRectangle r;
23692 int mouse_face_overwritten_p = 0;
23693
23694 TRACE ((stderr, "expose_frame "));
23695
23696 /* No need to redraw if frame will be redrawn soon. */
23697 if (FRAME_GARBAGED_P (f))
23698 {
23699 TRACE ((stderr, " garbaged\n"));
23700 return;
23701 }
23702
23703 /* If basic faces haven't been realized yet, there is no point in
23704 trying to redraw anything. This can happen when we get an expose
23705 event while Emacs is starting, e.g. by moving another window. */
23706 if (FRAME_FACE_CACHE (f) == NULL
23707 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23708 {
23709 TRACE ((stderr, " no faces\n"));
23710 return;
23711 }
23712
23713 if (w == 0 || h == 0)
23714 {
23715 r.x = r.y = 0;
23716 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23717 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23718 }
23719 else
23720 {
23721 r.x = x;
23722 r.y = y;
23723 r.width = w;
23724 r.height = h;
23725 }
23726
23727 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23728 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23729
23730 if (WINDOWP (f->tool_bar_window))
23731 mouse_face_overwritten_p
23732 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23733
23734 #ifdef HAVE_X_WINDOWS
23735 #ifndef MSDOS
23736 #ifndef USE_X_TOOLKIT
23737 if (WINDOWP (f->menu_bar_window))
23738 mouse_face_overwritten_p
23739 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23740 #endif /* not USE_X_TOOLKIT */
23741 #endif
23742 #endif
23743
23744 /* Some window managers support a focus-follows-mouse style with
23745 delayed raising of frames. Imagine a partially obscured frame,
23746 and moving the mouse into partially obscured mouse-face on that
23747 frame. The visible part of the mouse-face will be highlighted,
23748 then the WM raises the obscured frame. With at least one WM, KDE
23749 2.1, Emacs is not getting any event for the raising of the frame
23750 (even tried with SubstructureRedirectMask), only Expose events.
23751 These expose events will draw text normally, i.e. not
23752 highlighted. Which means we must redo the highlight here.
23753 Subsume it under ``we love X''. --gerd 2001-08-15 */
23754 /* Included in Windows version because Windows most likely does not
23755 do the right thing if any third party tool offers
23756 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23757 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23758 {
23759 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23760 if (f == dpyinfo->mouse_face_mouse_frame)
23761 {
23762 int x = dpyinfo->mouse_face_mouse_x;
23763 int y = dpyinfo->mouse_face_mouse_y;
23764 clear_mouse_face (dpyinfo);
23765 note_mouse_highlight (f, x, y);
23766 }
23767 }
23768 }
23769
23770
23771 /* EXPORT:
23772 Determine the intersection of two rectangles R1 and R2. Return
23773 the intersection in *RESULT. Value is non-zero if RESULT is not
23774 empty. */
23775
23776 int
23777 x_intersect_rectangles (r1, r2, result)
23778 XRectangle *r1, *r2, *result;
23779 {
23780 XRectangle *left, *right;
23781 XRectangle *upper, *lower;
23782 int intersection_p = 0;
23783
23784 /* Rearrange so that R1 is the left-most rectangle. */
23785 if (r1->x < r2->x)
23786 left = r1, right = r2;
23787 else
23788 left = r2, right = r1;
23789
23790 /* X0 of the intersection is right.x0, if this is inside R1,
23791 otherwise there is no intersection. */
23792 if (right->x <= left->x + left->width)
23793 {
23794 result->x = right->x;
23795
23796 /* The right end of the intersection is the minimum of the
23797 the right ends of left and right. */
23798 result->width = (min (left->x + left->width, right->x + right->width)
23799 - result->x);
23800
23801 /* Same game for Y. */
23802 if (r1->y < r2->y)
23803 upper = r1, lower = r2;
23804 else
23805 upper = r2, lower = r1;
23806
23807 /* The upper end of the intersection is lower.y0, if this is inside
23808 of upper. Otherwise, there is no intersection. */
23809 if (lower->y <= upper->y + upper->height)
23810 {
23811 result->y = lower->y;
23812
23813 /* The lower end of the intersection is the minimum of the lower
23814 ends of upper and lower. */
23815 result->height = (min (lower->y + lower->height,
23816 upper->y + upper->height)
23817 - result->y);
23818 intersection_p = 1;
23819 }
23820 }
23821
23822 return intersection_p;
23823 }
23824
23825 #endif /* HAVE_WINDOW_SYSTEM */
23826
23827 \f
23828 /***********************************************************************
23829 Initialization
23830 ***********************************************************************/
23831
23832 void
23833 syms_of_xdisp ()
23834 {
23835 Vwith_echo_area_save_vector = Qnil;
23836 staticpro (&Vwith_echo_area_save_vector);
23837
23838 Vmessage_stack = Qnil;
23839 staticpro (&Vmessage_stack);
23840
23841 Qinhibit_redisplay = intern ("inhibit-redisplay");
23842 staticpro (&Qinhibit_redisplay);
23843
23844 message_dolog_marker1 = Fmake_marker ();
23845 staticpro (&message_dolog_marker1);
23846 message_dolog_marker2 = Fmake_marker ();
23847 staticpro (&message_dolog_marker2);
23848 message_dolog_marker3 = Fmake_marker ();
23849 staticpro (&message_dolog_marker3);
23850
23851 #if GLYPH_DEBUG
23852 defsubr (&Sdump_frame_glyph_matrix);
23853 defsubr (&Sdump_glyph_matrix);
23854 defsubr (&Sdump_glyph_row);
23855 defsubr (&Sdump_tool_bar_row);
23856 defsubr (&Strace_redisplay);
23857 defsubr (&Strace_to_stderr);
23858 #endif
23859 #ifdef HAVE_WINDOW_SYSTEM
23860 defsubr (&Stool_bar_lines_needed);
23861 defsubr (&Slookup_image_map);
23862 #endif
23863 defsubr (&Sformat_mode_line);
23864 defsubr (&Sinvisible_p);
23865
23866 staticpro (&Qmenu_bar_update_hook);
23867 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23868
23869 staticpro (&Qoverriding_terminal_local_map);
23870 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23871
23872 staticpro (&Qoverriding_local_map);
23873 Qoverriding_local_map = intern ("overriding-local-map");
23874
23875 staticpro (&Qwindow_scroll_functions);
23876 Qwindow_scroll_functions = intern ("window-scroll-functions");
23877
23878 staticpro (&Qredisplay_end_trigger_functions);
23879 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23880
23881 staticpro (&Qinhibit_point_motion_hooks);
23882 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23883
23884 QCdata = intern (":data");
23885 staticpro (&QCdata);
23886 Qdisplay = intern ("display");
23887 staticpro (&Qdisplay);
23888 Qspace_width = intern ("space-width");
23889 staticpro (&Qspace_width);
23890 Qraise = intern ("raise");
23891 staticpro (&Qraise);
23892 Qslice = intern ("slice");
23893 staticpro (&Qslice);
23894 Qspace = intern ("space");
23895 staticpro (&Qspace);
23896 Qmargin = intern ("margin");
23897 staticpro (&Qmargin);
23898 Qpointer = intern ("pointer");
23899 staticpro (&Qpointer);
23900 Qleft_margin = intern ("left-margin");
23901 staticpro (&Qleft_margin);
23902 Qright_margin = intern ("right-margin");
23903 staticpro (&Qright_margin);
23904 Qcenter = intern ("center");
23905 staticpro (&Qcenter);
23906 Qline_height = intern ("line-height");
23907 staticpro (&Qline_height);
23908 QCalign_to = intern (":align-to");
23909 staticpro (&QCalign_to);
23910 QCrelative_width = intern (":relative-width");
23911 staticpro (&QCrelative_width);
23912 QCrelative_height = intern (":relative-height");
23913 staticpro (&QCrelative_height);
23914 QCeval = intern (":eval");
23915 staticpro (&QCeval);
23916 QCpropertize = intern (":propertize");
23917 staticpro (&QCpropertize);
23918 QCfile = intern (":file");
23919 staticpro (&QCfile);
23920 Qfontified = intern ("fontified");
23921 staticpro (&Qfontified);
23922 Qfontification_functions = intern ("fontification-functions");
23923 staticpro (&Qfontification_functions);
23924 Qtrailing_whitespace = intern ("trailing-whitespace");
23925 staticpro (&Qtrailing_whitespace);
23926 Qescape_glyph = intern ("escape-glyph");
23927 staticpro (&Qescape_glyph);
23928 Qnobreak_space = intern ("nobreak-space");
23929 staticpro (&Qnobreak_space);
23930 Qimage = intern ("image");
23931 staticpro (&Qimage);
23932 QCmap = intern (":map");
23933 staticpro (&QCmap);
23934 QCpointer = intern (":pointer");
23935 staticpro (&QCpointer);
23936 Qrect = intern ("rect");
23937 staticpro (&Qrect);
23938 Qcircle = intern ("circle");
23939 staticpro (&Qcircle);
23940 Qpoly = intern ("poly");
23941 staticpro (&Qpoly);
23942 Qmessage_truncate_lines = intern ("message-truncate-lines");
23943 staticpro (&Qmessage_truncate_lines);
23944 Qgrow_only = intern ("grow-only");
23945 staticpro (&Qgrow_only);
23946 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23947 staticpro (&Qinhibit_menubar_update);
23948 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23949 staticpro (&Qinhibit_eval_during_redisplay);
23950 Qposition = intern ("position");
23951 staticpro (&Qposition);
23952 Qbuffer_position = intern ("buffer-position");
23953 staticpro (&Qbuffer_position);
23954 Qobject = intern ("object");
23955 staticpro (&Qobject);
23956 Qbar = intern ("bar");
23957 staticpro (&Qbar);
23958 Qhbar = intern ("hbar");
23959 staticpro (&Qhbar);
23960 Qbox = intern ("box");
23961 staticpro (&Qbox);
23962 Qhollow = intern ("hollow");
23963 staticpro (&Qhollow);
23964 Qhand = intern ("hand");
23965 staticpro (&Qhand);
23966 Qarrow = intern ("arrow");
23967 staticpro (&Qarrow);
23968 Qtext = intern ("text");
23969 staticpro (&Qtext);
23970 Qrisky_local_variable = intern ("risky-local-variable");
23971 staticpro (&Qrisky_local_variable);
23972 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23973 staticpro (&Qinhibit_free_realized_faces);
23974
23975 list_of_error = Fcons (Fcons (intern ("error"),
23976 Fcons (intern ("void-variable"), Qnil)),
23977 Qnil);
23978 staticpro (&list_of_error);
23979
23980 Qlast_arrow_position = intern ("last-arrow-position");
23981 staticpro (&Qlast_arrow_position);
23982 Qlast_arrow_string = intern ("last-arrow-string");
23983 staticpro (&Qlast_arrow_string);
23984
23985 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23986 staticpro (&Qoverlay_arrow_string);
23987 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23988 staticpro (&Qoverlay_arrow_bitmap);
23989
23990 echo_buffer[0] = echo_buffer[1] = Qnil;
23991 staticpro (&echo_buffer[0]);
23992 staticpro (&echo_buffer[1]);
23993
23994 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23995 staticpro (&echo_area_buffer[0]);
23996 staticpro (&echo_area_buffer[1]);
23997
23998 Vmessages_buffer_name = build_string ("*Messages*");
23999 staticpro (&Vmessages_buffer_name);
24000
24001 mode_line_proptrans_alist = Qnil;
24002 staticpro (&mode_line_proptrans_alist);
24003 mode_line_string_list = Qnil;
24004 staticpro (&mode_line_string_list);
24005 mode_line_string_face = Qnil;
24006 staticpro (&mode_line_string_face);
24007 mode_line_string_face_prop = Qnil;
24008 staticpro (&mode_line_string_face_prop);
24009 Vmode_line_unwind_vector = Qnil;
24010 staticpro (&Vmode_line_unwind_vector);
24011
24012 help_echo_string = Qnil;
24013 staticpro (&help_echo_string);
24014 help_echo_object = Qnil;
24015 staticpro (&help_echo_object);
24016 help_echo_window = Qnil;
24017 staticpro (&help_echo_window);
24018 previous_help_echo_string = Qnil;
24019 staticpro (&previous_help_echo_string);
24020 help_echo_pos = -1;
24021
24022 #ifdef HAVE_WINDOW_SYSTEM
24023 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24024 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24025 For example, if a block cursor is over a tab, it will be drawn as
24026 wide as that tab on the display. */);
24027 x_stretch_cursor_p = 0;
24028 #endif
24029
24030 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24031 doc: /* *Non-nil means highlight trailing whitespace.
24032 The face used for trailing whitespace is `trailing-whitespace'. */);
24033 Vshow_trailing_whitespace = Qnil;
24034
24035 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24036 doc: /* *Control highlighting of nobreak space and soft hyphen.
24037 A value of t means highlight the character itself (for nobreak space,
24038 use face `nobreak-space').
24039 A value of nil means no highlighting.
24040 Other values mean display the escape glyph followed by an ordinary
24041 space or ordinary hyphen. */);
24042 Vnobreak_char_display = Qt;
24043
24044 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24045 doc: /* *The pointer shape to show in void text areas.
24046 A value of nil means to show the text pointer. Other options are `arrow',
24047 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24048 Vvoid_text_area_pointer = Qarrow;
24049
24050 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24051 doc: /* Non-nil means don't actually do any redisplay.
24052 This is used for internal purposes. */);
24053 Vinhibit_redisplay = Qnil;
24054
24055 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24056 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24057 Vglobal_mode_string = Qnil;
24058
24059 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24060 doc: /* Marker for where to display an arrow on top of the buffer text.
24061 This must be the beginning of a line in order to work.
24062 See also `overlay-arrow-string'. */);
24063 Voverlay_arrow_position = Qnil;
24064
24065 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24066 doc: /* String to display as an arrow in non-window frames.
24067 See also `overlay-arrow-position'. */);
24068 Voverlay_arrow_string = build_string ("=>");
24069
24070 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24071 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24072 The symbols on this list are examined during redisplay to determine
24073 where to display overlay arrows. */);
24074 Voverlay_arrow_variable_list
24075 = Fcons (intern ("overlay-arrow-position"), Qnil);
24076
24077 DEFVAR_INT ("scroll-step", &scroll_step,
24078 doc: /* *The number of lines to try scrolling a window by when point moves out.
24079 If that fails to bring point back on frame, point is centered instead.
24080 If this is zero, point is always centered after it moves off frame.
24081 If you want scrolling to always be a line at a time, you should set
24082 `scroll-conservatively' to a large value rather than set this to 1. */);
24083
24084 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24085 doc: /* *Scroll up to this many lines, to bring point back on screen.
24086 A value of zero means to scroll the text to center point vertically
24087 in the window. */);
24088 scroll_conservatively = 0;
24089
24090 DEFVAR_INT ("scroll-margin", &scroll_margin,
24091 doc: /* *Number of lines of margin at the top and bottom of a window.
24092 Recenter the window whenever point gets within this many lines
24093 of the top or bottom of the window. */);
24094 scroll_margin = 0;
24095
24096 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24097 doc: /* Pixels per inch value for non-window system displays.
24098 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24099 Vdisplay_pixels_per_inch = make_float (72.0);
24100
24101 #if GLYPH_DEBUG
24102 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24103 #endif
24104
24105 DEFVAR_BOOL ("truncate-partial-width-windows",
24106 &truncate_partial_width_windows,
24107 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24108 truncate_partial_width_windows = 1;
24109
24110 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24111 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24112 Any other value means to use the appropriate face, `mode-line',
24113 `header-line', or `menu' respectively. */);
24114 mode_line_inverse_video = 1;
24115
24116 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24117 doc: /* *Maximum buffer size for which line number should be displayed.
24118 If the buffer is bigger than this, the line number does not appear
24119 in the mode line. A value of nil means no limit. */);
24120 Vline_number_display_limit = Qnil;
24121
24122 DEFVAR_INT ("line-number-display-limit-width",
24123 &line_number_display_limit_width,
24124 doc: /* *Maximum line width (in characters) for line number display.
24125 If the average length of the lines near point is bigger than this, then the
24126 line number may be omitted from the mode line. */);
24127 line_number_display_limit_width = 200;
24128
24129 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24130 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24131 highlight_nonselected_windows = 0;
24132
24133 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24134 doc: /* Non-nil if more than one frame is visible on this display.
24135 Minibuffer-only frames don't count, but iconified frames do.
24136 This variable is not guaranteed to be accurate except while processing
24137 `frame-title-format' and `icon-title-format'. */);
24138
24139 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24140 doc: /* Template for displaying the title bar of visible frames.
24141 \(Assuming the window manager supports this feature.)
24142
24143 This variable has the same structure as `mode-line-format', except that
24144 the %c and %l constructs are ignored. It is used only on frames for
24145 which no explicit name has been set \(see `modify-frame-parameters'). */);
24146
24147 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24148 doc: /* Template for displaying the title bar of an iconified frame.
24149 \(Assuming the window manager supports this feature.)
24150 This variable has the same structure as `mode-line-format' (which see),
24151 and is used only on frames for which no explicit name has been set
24152 \(see `modify-frame-parameters'). */);
24153 Vicon_title_format
24154 = Vframe_title_format
24155 = Fcons (intern ("multiple-frames"),
24156 Fcons (build_string ("%b"),
24157 Fcons (Fcons (empty_unibyte_string,
24158 Fcons (intern ("invocation-name"),
24159 Fcons (build_string ("@"),
24160 Fcons (intern ("system-name"),
24161 Qnil)))),
24162 Qnil)));
24163
24164 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24165 doc: /* Maximum number of lines to keep in the message log buffer.
24166 If nil, disable message logging. If t, log messages but don't truncate
24167 the buffer when it becomes large. */);
24168 Vmessage_log_max = make_number (100);
24169
24170 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24171 doc: /* Functions called before redisplay, if window sizes have changed.
24172 The value should be a list of functions that take one argument.
24173 Just before redisplay, for each frame, if any of its windows have changed
24174 size since the last redisplay, or have been split or deleted,
24175 all the functions in the list are called, with the frame as argument. */);
24176 Vwindow_size_change_functions = Qnil;
24177
24178 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24179 doc: /* List of functions to call before redisplaying a window with scrolling.
24180 Each function is called with two arguments, the window
24181 and its new display-start position. Note that the value of `window-end'
24182 is not valid when these functions are called. */);
24183 Vwindow_scroll_functions = Qnil;
24184
24185 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24186 doc: /* Functions called when redisplay of a window reaches the end trigger.
24187 Each function is called with two arguments, the window and the end trigger value.
24188 See `set-window-redisplay-end-trigger'. */);
24189 Vredisplay_end_trigger_functions = Qnil;
24190
24191 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24192 doc: /* *Non-nil means autoselect window with mouse pointer.
24193 If nil, do not autoselect windows.
24194 A positive number means delay autoselection by that many seconds: a
24195 window is autoselected only after the mouse has remained in that
24196 window for the duration of the delay.
24197 A negative number has a similar effect, but causes windows to be
24198 autoselected only after the mouse has stopped moving. \(Because of
24199 the way Emacs compares mouse events, you will occasionally wait twice
24200 that time before the window gets selected.\)
24201 Any other value means to autoselect window instantaneously when the
24202 mouse pointer enters it.
24203
24204 Autoselection selects the minibuffer only if it is active, and never
24205 unselects the minibuffer if it is active.
24206
24207 When customizing this variable make sure that the actual value of
24208 `focus-follows-mouse' matches the behavior of your window manager. */);
24209 Vmouse_autoselect_window = Qnil;
24210
24211 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24212 doc: /* *Non-nil means automatically resize tool-bars.
24213 This dynamically changes the tool-bar's height to the minimum height
24214 that is needed to make all tool-bar items visible.
24215 If value is `grow-only', the tool-bar's height is only increased
24216 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24217 Vauto_resize_tool_bars = Qt;
24218
24219 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24220 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24221 auto_raise_tool_bar_buttons_p = 1;
24222
24223 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24224 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24225 make_cursor_line_fully_visible_p = 1;
24226
24227 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24228 doc: /* *Border below tool-bar in pixels.
24229 If an integer, use it as the height of the border.
24230 If it is one of `internal-border-width' or `border-width', use the
24231 value of the corresponding frame parameter.
24232 Otherwise, no border is added below the tool-bar. */);
24233 Vtool_bar_border = Qinternal_border_width;
24234
24235 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24236 doc: /* *Margin around tool-bar buttons in pixels.
24237 If an integer, use that for both horizontal and vertical margins.
24238 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24239 HORZ specifying the horizontal margin, and VERT specifying the
24240 vertical margin. */);
24241 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24242
24243 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24244 doc: /* *Relief thickness of tool-bar buttons. */);
24245 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24246
24247 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24248 doc: /* List of functions to call to fontify regions of text.
24249 Each function is called with one argument POS. Functions must
24250 fontify a region starting at POS in the current buffer, and give
24251 fontified regions the property `fontified'. */);
24252 Vfontification_functions = Qnil;
24253 Fmake_variable_buffer_local (Qfontification_functions);
24254
24255 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24256 &unibyte_display_via_language_environment,
24257 doc: /* *Non-nil means display unibyte text according to language environment.
24258 Specifically this means that unibyte non-ASCII characters
24259 are displayed by converting them to the equivalent multibyte characters
24260 according to the current language environment. As a result, they are
24261 displayed according to the current fontset. */);
24262 unibyte_display_via_language_environment = 0;
24263
24264 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24265 doc: /* *Maximum height for resizing mini-windows.
24266 If a float, it specifies a fraction of the mini-window frame's height.
24267 If an integer, it specifies a number of lines. */);
24268 Vmax_mini_window_height = make_float (0.25);
24269
24270 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24271 doc: /* *How to resize mini-windows.
24272 A value of nil means don't automatically resize mini-windows.
24273 A value of t means resize them to fit the text displayed in them.
24274 A value of `grow-only', the default, means let mini-windows grow
24275 only, until their display becomes empty, at which point the windows
24276 go back to their normal size. */);
24277 Vresize_mini_windows = Qgrow_only;
24278
24279 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24280 doc: /* Alist specifying how to blink the cursor off.
24281 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24282 `cursor-type' frame-parameter or variable equals ON-STATE,
24283 comparing using `equal', Emacs uses OFF-STATE to specify
24284 how to blink it off. ON-STATE and OFF-STATE are values for
24285 the `cursor-type' frame parameter.
24286
24287 If a frame's ON-STATE has no entry in this list,
24288 the frame's other specifications determine how to blink the cursor off. */);
24289 Vblink_cursor_alist = Qnil;
24290
24291 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24292 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24293 automatic_hscrolling_p = 1;
24294
24295 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24296 doc: /* *How many columns away from the window edge point is allowed to get
24297 before automatic hscrolling will horizontally scroll the window. */);
24298 hscroll_margin = 5;
24299
24300 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24301 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24302 When point is less than `hscroll-margin' columns from the window
24303 edge, automatic hscrolling will scroll the window by the amount of columns
24304 determined by this variable. If its value is a positive integer, scroll that
24305 many columns. If it's a positive floating-point number, it specifies the
24306 fraction of the window's width to scroll. If it's nil or zero, point will be
24307 centered horizontally after the scroll. Any other value, including negative
24308 numbers, are treated as if the value were zero.
24309
24310 Automatic hscrolling always moves point outside the scroll margin, so if
24311 point was more than scroll step columns inside the margin, the window will
24312 scroll more than the value given by the scroll step.
24313
24314 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24315 and `scroll-right' overrides this variable's effect. */);
24316 Vhscroll_step = make_number (0);
24317
24318 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24319 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24320 Bind this around calls to `message' to let it take effect. */);
24321 message_truncate_lines = 0;
24322
24323 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24324 doc: /* Normal hook run to update the menu bar definitions.
24325 Redisplay runs this hook before it redisplays the menu bar.
24326 This is used to update submenus such as Buffers,
24327 whose contents depend on various data. */);
24328 Vmenu_bar_update_hook = Qnil;
24329
24330 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24331 doc: /* Frame for which we are updating a menu.
24332 The enable predicate for a menu binding should check this variable. */);
24333 Vmenu_updating_frame = Qnil;
24334
24335 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24336 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24337 inhibit_menubar_update = 0;
24338
24339 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24340 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24341 inhibit_eval_during_redisplay = 0;
24342
24343 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24344 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24345 inhibit_free_realized_faces = 0;
24346
24347 #if GLYPH_DEBUG
24348 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24349 doc: /* Inhibit try_window_id display optimization. */);
24350 inhibit_try_window_id = 0;
24351
24352 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24353 doc: /* Inhibit try_window_reusing display optimization. */);
24354 inhibit_try_window_reusing = 0;
24355
24356 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24357 doc: /* Inhibit try_cursor_movement display optimization. */);
24358 inhibit_try_cursor_movement = 0;
24359 #endif /* GLYPH_DEBUG */
24360
24361 DEFVAR_INT ("overline-margin", &overline_margin,
24362 doc: /* *Space between overline and text, in pixels.
24363 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24364 margin to the caracter height. */);
24365 overline_margin = 2;
24366 }
24367
24368
24369 /* Initialize this module when Emacs starts. */
24370
24371 void
24372 init_xdisp ()
24373 {
24374 Lisp_Object root_window;
24375 struct window *mini_w;
24376
24377 current_header_line_height = current_mode_line_height = -1;
24378
24379 CHARPOS (this_line_start_pos) = 0;
24380
24381 mini_w = XWINDOW (minibuf_window);
24382 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24383
24384 if (!noninteractive)
24385 {
24386 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24387 int i;
24388
24389 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24390 set_window_height (root_window,
24391 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24392 0);
24393 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24394 set_window_height (minibuf_window, 1, 0);
24395
24396 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24397 mini_w->total_cols = make_number (FRAME_COLS (f));
24398
24399 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24400 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24401 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24402
24403 /* The default ellipsis glyphs `...'. */
24404 for (i = 0; i < 3; ++i)
24405 default_invis_vector[i] = make_number ('.');
24406 }
24407
24408 {
24409 /* Allocate the buffer for frame titles.
24410 Also used for `format-mode-line'. */
24411 int size = 100;
24412 mode_line_noprop_buf = (char *) xmalloc (size);
24413 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24414 mode_line_noprop_ptr = mode_line_noprop_buf;
24415 mode_line_target = MODE_LINE_DISPLAY;
24416 }
24417
24418 help_echo_showing_p = 0;
24419 }
24420
24421
24422 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24423 (do not change this comment) */