]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from emacs--devo--0
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 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 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "character.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "keymap.h"
185 #include "macros.h"
186 #include "disptab.h"
187 #include "termhooks.h"
188 #include "intervals.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.h"
192 #include "fontset.h"
193 #include "blockinput.h"
194
195 #ifdef HAVE_X_WINDOWS
196 #include "xterm.h"
197 #endif
198 #ifdef WINDOWSNT
199 #include "w32term.h"
200 #endif
201 #ifdef MAC_OS
202 #include "macterm.h"
203 #endif
204
205 #ifdef HAVE_WINDOW_SYSTEM
206 #ifdef USE_FONT_BACKEND
207 #include "font.h"
208 #endif /* USE_FONT_BACKEND */
209 #endif /* HAVE_WINDOW_SYSTEM */
210
211 #ifndef FRAME_X_OUTPUT
212 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
213 #endif
214
215 #define INFINITY 10000000
216
217 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
218 || defined (USE_GTK)
219 extern void set_frame_menubar P_ ((struct frame *f, int, int));
220 extern int pending_menu_activation;
221 #endif
222
223 extern int interrupt_input;
224 extern int command_loop_level;
225
226 extern Lisp_Object do_mouse_tracking;
227
228 extern int minibuffer_auto_raise;
229 extern Lisp_Object Vminibuffer_list;
230
231 extern Lisp_Object Qface;
232 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
233
234 extern Lisp_Object Voverriding_local_map;
235 extern Lisp_Object Voverriding_local_map_menu_flag;
236 extern Lisp_Object Qmenu_item;
237 extern Lisp_Object Qwhen;
238 extern Lisp_Object Qhelp_echo;
239
240 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
241 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
242 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
243 Lisp_Object Qinhibit_point_motion_hooks;
244 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
245 Lisp_Object Qfontified;
246 Lisp_Object Qgrow_only;
247 Lisp_Object Qinhibit_eval_during_redisplay;
248 Lisp_Object Qbuffer_position, Qposition, Qobject;
249
250 /* Cursor shapes */
251 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
252
253 /* Pointer shapes */
254 Lisp_Object Qarrow, Qhand, Qtext;
255
256 Lisp_Object Qrisky_local_variable;
257
258 /* Holds the list (error). */
259 Lisp_Object list_of_error;
260
261 /* Functions called to fontify regions of text. */
262
263 Lisp_Object Vfontification_functions;
264 Lisp_Object Qfontification_functions;
265
266 /* Non-nil means automatically select any window when the mouse
267 cursor moves into it. */
268 Lisp_Object Vmouse_autoselect_window;
269
270 /* Non-zero means draw tool bar buttons raised when the mouse moves
271 over them. */
272
273 int auto_raise_tool_bar_buttons_p;
274
275 /* Non-zero means to reposition window if cursor line is only partially visible. */
276
277 int make_cursor_line_fully_visible_p;
278
279 /* Margin below tool bar in pixels. 0 or nil means no margin.
280 If value is `internal-border-width' or `border-width',
281 the corresponding frame parameter is used. */
282
283 Lisp_Object Vtool_bar_border;
284
285 /* Margin around tool bar buttons in pixels. */
286
287 Lisp_Object Vtool_bar_button_margin;
288
289 /* Thickness of shadow to draw around tool bar buttons. */
290
291 EMACS_INT tool_bar_button_relief;
292
293 /* Non-nil means automatically resize tool-bars so that all tool-bar
294 items are visible, and no blank lines remain.
295
296 If value is `grow-only', only make tool-bar bigger. */
297
298 Lisp_Object Vauto_resize_tool_bars;
299
300 /* Non-zero means draw block and hollow cursor as wide as the glyph
301 under it. For example, if a block cursor is over a tab, it will be
302 drawn as wide as that tab on the display. */
303
304 int x_stretch_cursor_p;
305
306 /* Non-nil means don't actually do any redisplay. */
307
308 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
309
310 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
311
312 int inhibit_eval_during_redisplay;
313
314 /* Names of text properties relevant for redisplay. */
315
316 Lisp_Object Qdisplay;
317 extern Lisp_Object Qface, Qinvisible, Qwidth;
318
319 /* Symbols used in text property values. */
320
321 Lisp_Object Vdisplay_pixels_per_inch;
322 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
323 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
324 Lisp_Object Qslice;
325 Lisp_Object Qcenter;
326 Lisp_Object Qmargin, Qpointer;
327 Lisp_Object Qline_height;
328 extern Lisp_Object Qheight;
329 extern Lisp_Object QCwidth, QCheight, QCascent;
330 extern Lisp_Object Qscroll_bar;
331 extern Lisp_Object Qcursor;
332
333 /* Non-nil means highlight trailing whitespace. */
334
335 Lisp_Object Vshow_trailing_whitespace;
336
337 /* Non-nil means escape non-break space and hyphens. */
338
339 Lisp_Object Vnobreak_char_display;
340
341 #ifdef HAVE_WINDOW_SYSTEM
342 extern Lisp_Object Voverflow_newline_into_fringe;
343
344 /* Test if overflow newline into fringe. Called with iterator IT
345 at or past right window margin, and with IT->current_x set. */
346
347 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
348 (!NILP (Voverflow_newline_into_fringe) \
349 && FRAME_WINDOW_P (it->f) \
350 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
351 && it->current_x == it->last_visible_x)
352
353 #endif /* HAVE_WINDOW_SYSTEM */
354
355 /* Non-nil means show the text cursor in void text areas
356 i.e. in blank areas after eol and eob. This used to be
357 the default in 21.3. */
358
359 Lisp_Object Vvoid_text_area_pointer;
360
361 /* Name of the face used to highlight trailing whitespace. */
362
363 Lisp_Object Qtrailing_whitespace;
364
365 /* Name and number of the face used to highlight escape glyphs. */
366
367 Lisp_Object Qescape_glyph;
368
369 /* Name and number of the face used to highlight non-breaking spaces. */
370
371 Lisp_Object Qnobreak_space;
372
373 /* The symbol `image' which is the car of the lists used to represent
374 images in Lisp. */
375
376 Lisp_Object Qimage;
377
378 /* The image map types. */
379 Lisp_Object QCmap, QCpointer;
380 Lisp_Object Qrect, Qcircle, Qpoly;
381
382 /* Non-zero means print newline to stdout before next mini-buffer
383 message. */
384
385 int noninteractive_need_newline;
386
387 /* Non-zero means print newline to message log before next message. */
388
389 static int message_log_need_newline;
390
391 /* Three markers that message_dolog uses.
392 It could allocate them itself, but that causes trouble
393 in handling memory-full errors. */
394 static Lisp_Object message_dolog_marker1;
395 static Lisp_Object message_dolog_marker2;
396 static Lisp_Object message_dolog_marker3;
397 \f
398 /* The buffer position of the first character appearing entirely or
399 partially on the line of the selected window which contains the
400 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
401 redisplay optimization in redisplay_internal. */
402
403 static struct text_pos this_line_start_pos;
404
405 /* Number of characters past the end of the line above, including the
406 terminating newline. */
407
408 static struct text_pos this_line_end_pos;
409
410 /* The vertical positions and the height of this line. */
411
412 static int this_line_vpos;
413 static int this_line_y;
414 static int this_line_pixel_height;
415
416 /* X position at which this display line starts. Usually zero;
417 negative if first character is partially visible. */
418
419 static int this_line_start_x;
420
421 /* Buffer that this_line_.* variables are referring to. */
422
423 static struct buffer *this_line_buffer;
424
425 /* Nonzero means truncate lines in all windows less wide than the
426 frame. */
427
428 int truncate_partial_width_windows;
429
430 /* A flag to control how to display unibyte 8-bit character. */
431
432 int unibyte_display_via_language_environment;
433
434 /* Nonzero means we have more than one non-mini-buffer-only frame.
435 Not guaranteed to be accurate except while parsing
436 frame-title-format. */
437
438 int multiple_frames;
439
440 Lisp_Object Vglobal_mode_string;
441
442
443 /* List of variables (symbols) which hold markers for overlay arrows.
444 The symbols on this list are examined during redisplay to determine
445 where to display overlay arrows. */
446
447 Lisp_Object Voverlay_arrow_variable_list;
448
449 /* Marker for where to display an arrow on top of the buffer text. */
450
451 Lisp_Object Voverlay_arrow_position;
452
453 /* String to display for the arrow. Only used on terminal frames. */
454
455 Lisp_Object Voverlay_arrow_string;
456
457 /* Values of those variables at last redisplay are stored as
458 properties on `overlay-arrow-position' symbol. However, if
459 Voverlay_arrow_position is a marker, last-arrow-position is its
460 numerical position. */
461
462 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
463
464 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
465 properties on a symbol in overlay-arrow-variable-list. */
466
467 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
468
469 /* Like mode-line-format, but for the title bar on a visible frame. */
470
471 Lisp_Object Vframe_title_format;
472
473 /* Like mode-line-format, but for the title bar on an iconified frame. */
474
475 Lisp_Object Vicon_title_format;
476
477 /* List of functions to call when a window's size changes. These
478 functions get one arg, a frame on which one or more windows' sizes
479 have changed. */
480
481 static Lisp_Object Vwindow_size_change_functions;
482
483 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
484
485 /* Nonzero if an overlay arrow has been displayed in this window. */
486
487 static int overlay_arrow_seen;
488
489 /* Nonzero means highlight the region even in nonselected windows. */
490
491 int highlight_nonselected_windows;
492
493 /* If cursor motion alone moves point off frame, try scrolling this
494 many lines up or down if that will bring it back. */
495
496 static EMACS_INT scroll_step;
497
498 /* Nonzero means scroll just far enough to bring point back on the
499 screen, when appropriate. */
500
501 static EMACS_INT scroll_conservatively;
502
503 /* Recenter the window whenever point gets within this many lines of
504 the top or bottom of the window. This value is translated into a
505 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
506 that there is really a fixed pixel height scroll margin. */
507
508 EMACS_INT scroll_margin;
509
510 /* Number of windows showing the buffer of the selected window (or
511 another buffer with the same base buffer). keyboard.c refers to
512 this. */
513
514 int buffer_shared;
515
516 /* Vector containing glyphs for an ellipsis `...'. */
517
518 static Lisp_Object default_invis_vector[3];
519
520 /* Zero means display the mode-line/header-line/menu-bar in the default face
521 (this slightly odd definition is for compatibility with previous versions
522 of emacs), non-zero means display them using their respective faces.
523
524 This variable is deprecated. */
525
526 int mode_line_inverse_video;
527
528 /* Prompt to display in front of the mini-buffer contents. */
529
530 Lisp_Object minibuf_prompt;
531
532 /* Width of current mini-buffer prompt. Only set after display_line
533 of the line that contains the prompt. */
534
535 int minibuf_prompt_width;
536
537 /* This is the window where the echo area message was displayed. It
538 is always a mini-buffer window, but it may not be the same window
539 currently active as a mini-buffer. */
540
541 Lisp_Object echo_area_window;
542
543 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
544 pushes the current message and the value of
545 message_enable_multibyte on the stack, the function restore_message
546 pops the stack and displays MESSAGE again. */
547
548 Lisp_Object Vmessage_stack;
549
550 /* Nonzero means multibyte characters were enabled when the echo area
551 message was specified. */
552
553 int message_enable_multibyte;
554
555 /* Nonzero if we should redraw the mode lines on the next redisplay. */
556
557 int update_mode_lines;
558
559 /* Nonzero if window sizes or contents have changed since last
560 redisplay that finished. */
561
562 int windows_or_buffers_changed;
563
564 /* Nonzero means a frame's cursor type has been changed. */
565
566 int cursor_type_changed;
567
568 /* Nonzero after display_mode_line if %l was used and it displayed a
569 line number. */
570
571 int line_number_displayed;
572
573 /* Maximum buffer size for which to display line numbers. */
574
575 Lisp_Object Vline_number_display_limit;
576
577 /* Line width to consider when repositioning for line number display. */
578
579 static EMACS_INT line_number_display_limit_width;
580
581 /* Number of lines to keep in the message log buffer. t means
582 infinite. nil means don't log at all. */
583
584 Lisp_Object Vmessage_log_max;
585
586 /* The name of the *Messages* buffer, a string. */
587
588 static Lisp_Object Vmessages_buffer_name;
589
590 /* Index 0 is the buffer that holds the current (desired) echo area message,
591 or nil if none is desired right now.
592
593 Index 1 is the buffer that holds the previously displayed echo area message,
594 or nil to indicate no message. This is normally what's on the screen now.
595
596 These two can point to the same buffer. That happens when the last
597 message output by the user (or made by echoing) has been displayed. */
598
599 Lisp_Object echo_area_buffer[2];
600
601 /* Permanent pointers to the two buffers that are used for echo area
602 purposes. Once the two buffers are made, and their pointers are
603 placed here, these two slots remain unchanged unless those buffers
604 need to be created afresh. */
605
606 static Lisp_Object echo_buffer[2];
607
608 /* A vector saved used in with_area_buffer to reduce consing. */
609
610 static Lisp_Object Vwith_echo_area_save_vector;
611
612 /* Non-zero means display_echo_area should display the last echo area
613 message again. Set by redisplay_preserve_echo_area. */
614
615 static int display_last_displayed_message_p;
616
617 /* Nonzero if echo area is being used by print; zero if being used by
618 message. */
619
620 int message_buf_print;
621
622 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
623
624 Lisp_Object Qinhibit_menubar_update;
625 int inhibit_menubar_update;
626
627 /* When evaluating expressions from menu bar items (enable conditions,
628 for instance), this is the frame they are being processed for. */
629
630 Lisp_Object Vmenu_updating_frame;
631
632 /* Maximum height for resizing mini-windows. Either a float
633 specifying a fraction of the available height, or an integer
634 specifying a number of lines. */
635
636 Lisp_Object Vmax_mini_window_height;
637
638 /* Non-zero means messages should be displayed with truncated
639 lines instead of being continued. */
640
641 int message_truncate_lines;
642 Lisp_Object Qmessage_truncate_lines;
643
644 /* Set to 1 in clear_message to make redisplay_internal aware
645 of an emptied echo area. */
646
647 static int message_cleared_p;
648
649 /* How to blink the default frame cursor off. */
650 Lisp_Object Vblink_cursor_alist;
651
652 /* A scratch glyph row with contents used for generating truncation
653 glyphs. Also used in direct_output_for_insert. */
654
655 #define MAX_SCRATCH_GLYPHS 100
656 struct glyph_row scratch_glyph_row;
657 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
658
659 /* Ascent and height of the last line processed by move_it_to. */
660
661 static int last_max_ascent, last_height;
662
663 /* Non-zero if there's a help-echo in the echo area. */
664
665 int help_echo_showing_p;
666
667 /* If >= 0, computed, exact values of mode-line and header-line height
668 to use in the macros CURRENT_MODE_LINE_HEIGHT and
669 CURRENT_HEADER_LINE_HEIGHT. */
670
671 int current_mode_line_height, current_header_line_height;
672
673 /* The maximum distance to look ahead for text properties. Values
674 that are too small let us call compute_char_face and similar
675 functions too often which is expensive. Values that are too large
676 let us call compute_char_face and alike too often because we
677 might not be interested in text properties that far away. */
678
679 #define TEXT_PROP_DISTANCE_LIMIT 100
680
681 #if GLYPH_DEBUG
682
683 /* Variables to turn off display optimizations from Lisp. */
684
685 int inhibit_try_window_id, inhibit_try_window_reusing;
686 int inhibit_try_cursor_movement;
687
688 /* Non-zero means print traces of redisplay if compiled with
689 GLYPH_DEBUG != 0. */
690
691 int trace_redisplay_p;
692
693 #endif /* GLYPH_DEBUG */
694
695 #ifdef DEBUG_TRACE_MOVE
696 /* Non-zero means trace with TRACE_MOVE to stderr. */
697 int trace_move;
698
699 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
700 #else
701 #define TRACE_MOVE(x) (void) 0
702 #endif
703
704 /* Non-zero means automatically scroll windows horizontally to make
705 point visible. */
706
707 int automatic_hscrolling_p;
708
709 /* How close to the margin can point get before the window is scrolled
710 horizontally. */
711 EMACS_INT hscroll_margin;
712
713 /* How much to scroll horizontally when point is inside the above margin. */
714 Lisp_Object Vhscroll_step;
715
716 /* The variable `resize-mini-windows'. If nil, don't resize
717 mini-windows. If t, always resize them to fit the text they
718 display. If `grow-only', let mini-windows grow only until they
719 become empty. */
720
721 Lisp_Object Vresize_mini_windows;
722
723 /* Buffer being redisplayed -- for redisplay_window_error. */
724
725 struct buffer *displayed_buffer;
726
727 /* Space between overline and text. */
728
729 EMACS_INT overline_margin;
730
731 /* Value returned from text property handlers (see below). */
732
733 enum prop_handled
734 {
735 HANDLED_NORMALLY,
736 HANDLED_RECOMPUTE_PROPS,
737 HANDLED_OVERLAY_STRING_CONSUMED,
738 HANDLED_RETURN
739 };
740
741 /* A description of text properties that redisplay is interested
742 in. */
743
744 struct props
745 {
746 /* The name of the property. */
747 Lisp_Object *name;
748
749 /* A unique index for the property. */
750 enum prop_idx idx;
751
752 /* A handler function called to set up iterator IT from the property
753 at IT's current position. Value is used to steer handle_stop. */
754 enum prop_handled (*handler) P_ ((struct it *it));
755 };
756
757 static enum prop_handled handle_face_prop P_ ((struct it *));
758 static enum prop_handled handle_invisible_prop P_ ((struct it *));
759 static enum prop_handled handle_display_prop P_ ((struct it *));
760 static enum prop_handled handle_composition_prop P_ ((struct it *));
761 static enum prop_handled handle_overlay_change P_ ((struct it *));
762 static enum prop_handled handle_fontified_prop P_ ((struct it *));
763 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
764
765 /* Properties handled by iterators. */
766
767 static struct props it_props[] =
768 {
769 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
770 /* Handle `face' before `display' because some sub-properties of
771 `display' need to know the face. */
772 {&Qface, FACE_PROP_IDX, handle_face_prop},
773 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
774 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
775 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
776 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
777 {NULL, 0, NULL}
778 };
779
780 /* Value is the position described by X. If X is a marker, value is
781 the marker_position of X. Otherwise, value is X. */
782
783 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
784
785 /* Enumeration returned by some move_it_.* functions internally. */
786
787 enum move_it_result
788 {
789 /* Not used. Undefined value. */
790 MOVE_UNDEFINED,
791
792 /* Move ended at the requested buffer position or ZV. */
793 MOVE_POS_MATCH_OR_ZV,
794
795 /* Move ended at the requested X pixel position. */
796 MOVE_X_REACHED,
797
798 /* Move within a line ended at the end of a line that must be
799 continued. */
800 MOVE_LINE_CONTINUED,
801
802 /* Move within a line ended at the end of a line that would
803 be displayed truncated. */
804 MOVE_LINE_TRUNCATED,
805
806 /* Move within a line ended at a line end. */
807 MOVE_NEWLINE_OR_CR
808 };
809
810 /* This counter is used to clear the face cache every once in a while
811 in redisplay_internal. It is incremented for each redisplay.
812 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
813 cleared. */
814
815 #define CLEAR_FACE_CACHE_COUNT 500
816 static int clear_face_cache_count;
817
818 /* Similarly for the image cache. */
819
820 #ifdef HAVE_WINDOW_SYSTEM
821 #define CLEAR_IMAGE_CACHE_COUNT 101
822 static int clear_image_cache_count;
823 #endif
824
825 /* Record the previous terminal frame we displayed. */
826
827 static struct frame *previous_terminal_frame;
828
829 /* Non-zero while redisplay_internal is in progress. */
830
831 int redisplaying_p;
832
833 /* Non-zero means don't free realized faces. Bound while freeing
834 realized faces is dangerous because glyph matrices might still
835 reference them. */
836
837 int inhibit_free_realized_faces;
838 Lisp_Object Qinhibit_free_realized_faces;
839
840 /* If a string, XTread_socket generates an event to display that string.
841 (The display is done in read_char.) */
842
843 Lisp_Object help_echo_string;
844 Lisp_Object help_echo_window;
845 Lisp_Object help_echo_object;
846 int help_echo_pos;
847
848 /* Temporary variable for XTread_socket. */
849
850 Lisp_Object previous_help_echo_string;
851
852 /* Null glyph slice */
853
854 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
855
856 \f
857 /* Function prototypes. */
858
859 static void setup_for_ellipsis P_ ((struct it *, int));
860 static void mark_window_display_accurate_1 P_ ((struct window *, int));
861 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
862 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
863 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
864 static int redisplay_mode_lines P_ ((Lisp_Object, int));
865 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
866
867 #if 0
868 static int invisible_text_between_p P_ ((struct it *, int, int));
869 #endif
870
871 static void pint2str P_ ((char *, int, int));
872 static void pint2hrstr P_ ((char *, int, int));
873 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
874 struct text_pos));
875 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
876 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
877 static void store_mode_line_noprop_char P_ ((char));
878 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
879 static void x_consider_frame_title P_ ((Lisp_Object));
880 static void handle_stop P_ ((struct it *));
881 static int tool_bar_lines_needed P_ ((struct frame *, int *));
882 static int single_display_spec_intangible_p P_ ((Lisp_Object));
883 static void ensure_echo_area_buffers P_ ((void));
884 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
885 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
886 static int with_echo_area_buffer P_ ((struct window *, int,
887 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
888 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
889 static void clear_garbaged_frames P_ ((void));
890 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
891 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
892 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
893 static int display_echo_area P_ ((struct window *));
894 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
895 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
896 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
897 static int string_char_and_length P_ ((const unsigned char *, int, int *));
898 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
899 struct text_pos));
900 static int compute_window_start_on_continuation_line P_ ((struct window *));
901 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
902 static void insert_left_trunc_glyphs P_ ((struct it *));
903 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
904 Lisp_Object));
905 static void extend_face_to_end_of_line P_ ((struct it *));
906 static int append_space_for_newline P_ ((struct it *, int));
907 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
908 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
909 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
910 static int trailing_whitespace_p P_ ((int));
911 static int message_log_check_duplicate P_ ((int, int, int, int));
912 static void push_it P_ ((struct it *));
913 static void pop_it P_ ((struct it *));
914 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
915 static void select_frame_for_redisplay P_ ((Lisp_Object));
916 static void redisplay_internal P_ ((int));
917 static int echo_area_display P_ ((int));
918 static void redisplay_windows P_ ((Lisp_Object));
919 static void redisplay_window P_ ((Lisp_Object, int));
920 static Lisp_Object redisplay_window_error ();
921 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
922 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
923 static int update_menu_bar P_ ((struct frame *, int, int));
924 static int try_window_reusing_current_matrix P_ ((struct window *));
925 static int try_window_id P_ ((struct window *));
926 static int display_line P_ ((struct it *));
927 static int display_mode_lines P_ ((struct window *));
928 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
929 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
930 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
931 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
932 static void display_menu_bar P_ ((struct window *));
933 static int display_count_lines P_ ((int, int, int, int, int *));
934 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
935 int, int, struct it *, int, int, int, int));
936 static void compute_line_metrics P_ ((struct it *));
937 static void run_redisplay_end_trigger_hook P_ ((struct it *));
938 static int get_overlay_strings P_ ((struct it *, int));
939 static int get_overlay_strings_1 P_ ((struct it *, int, int));
940 static void next_overlay_string P_ ((struct it *));
941 static void reseat P_ ((struct it *, struct text_pos, int));
942 static void reseat_1 P_ ((struct it *, struct text_pos, int));
943 static void back_to_previous_visible_line_start P_ ((struct it *));
944 void reseat_at_previous_visible_line_start P_ ((struct it *));
945 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
946 static int next_element_from_ellipsis P_ ((struct it *));
947 static int next_element_from_display_vector P_ ((struct it *));
948 static int next_element_from_string P_ ((struct it *));
949 static int next_element_from_c_string P_ ((struct it *));
950 static int next_element_from_buffer P_ ((struct it *));
951 static int next_element_from_composition P_ ((struct it *));
952 static int next_element_from_image P_ ((struct it *));
953 static int next_element_from_stretch P_ ((struct it *));
954 static void load_overlay_strings P_ ((struct it *, int));
955 static int init_from_display_pos P_ ((struct it *, struct window *,
956 struct display_pos *));
957 static void reseat_to_string P_ ((struct it *, unsigned char *,
958 Lisp_Object, int, int, int, int));
959 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
960 int, int, int));
961 void move_it_vertically_backward P_ ((struct it *, int));
962 static void init_to_row_start P_ ((struct it *, struct window *,
963 struct glyph_row *));
964 static int init_to_row_end P_ ((struct it *, struct window *,
965 struct glyph_row *));
966 static void back_to_previous_line_start P_ ((struct it *));
967 static int forward_to_next_line_start P_ ((struct it *, int *));
968 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
969 Lisp_Object, int));
970 static struct text_pos string_pos P_ ((int, Lisp_Object));
971 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
972 static int number_of_chars P_ ((unsigned char *, int));
973 static void compute_stop_pos P_ ((struct it *));
974 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
975 Lisp_Object));
976 static int face_before_or_after_it_pos P_ ((struct it *, int));
977 static int next_overlay_change P_ ((int));
978 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
979 Lisp_Object, struct text_pos *,
980 int));
981 static int underlying_face_id P_ ((struct it *));
982 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
983 struct window *));
984
985 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
986 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
987
988 #ifdef HAVE_WINDOW_SYSTEM
989
990 static void update_tool_bar P_ ((struct frame *, int));
991 static void build_desired_tool_bar_string P_ ((struct frame *f));
992 static int redisplay_tool_bar P_ ((struct frame *));
993 static void display_tool_bar_line P_ ((struct it *, int));
994 static void notice_overwritten_cursor P_ ((struct window *,
995 enum glyph_row_area,
996 int, int, int, int));
997
998
999
1000 #endif /* HAVE_WINDOW_SYSTEM */
1001
1002 \f
1003 /***********************************************************************
1004 Window display dimensions
1005 ***********************************************************************/
1006
1007 /* Return the bottom boundary y-position for text lines in window W.
1008 This is the first y position at which a line cannot start.
1009 It is relative to the top of the window.
1010
1011 This is the height of W minus the height of a mode line, if any. */
1012
1013 INLINE int
1014 window_text_bottom_y (w)
1015 struct window *w;
1016 {
1017 int height = WINDOW_TOTAL_HEIGHT (w);
1018
1019 if (WINDOW_WANTS_MODELINE_P (w))
1020 height -= CURRENT_MODE_LINE_HEIGHT (w);
1021 return height;
1022 }
1023
1024 /* Return the pixel width of display area AREA of window W. AREA < 0
1025 means return the total width of W, not including fringes to
1026 the left and right of the window. */
1027
1028 INLINE int
1029 window_box_width (w, area)
1030 struct window *w;
1031 int area;
1032 {
1033 int cols = XFASTINT (w->total_cols);
1034 int pixels = 0;
1035
1036 if (!w->pseudo_window_p)
1037 {
1038 cols -= WINDOW_SCROLL_BAR_COLS (w);
1039
1040 if (area == TEXT_AREA)
1041 {
1042 if (INTEGERP (w->left_margin_cols))
1043 cols -= XFASTINT (w->left_margin_cols);
1044 if (INTEGERP (w->right_margin_cols))
1045 cols -= XFASTINT (w->right_margin_cols);
1046 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1047 }
1048 else if (area == LEFT_MARGIN_AREA)
1049 {
1050 cols = (INTEGERP (w->left_margin_cols)
1051 ? XFASTINT (w->left_margin_cols) : 0);
1052 pixels = 0;
1053 }
1054 else if (area == RIGHT_MARGIN_AREA)
1055 {
1056 cols = (INTEGERP (w->right_margin_cols)
1057 ? XFASTINT (w->right_margin_cols) : 0);
1058 pixels = 0;
1059 }
1060 }
1061
1062 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1063 }
1064
1065
1066 /* Return the pixel height of the display area of window W, not
1067 including mode lines of W, if any. */
1068
1069 INLINE int
1070 window_box_height (w)
1071 struct window *w;
1072 {
1073 struct frame *f = XFRAME (w->frame);
1074 int height = WINDOW_TOTAL_HEIGHT (w);
1075
1076 xassert (height >= 0);
1077
1078 /* Note: the code below that determines the mode-line/header-line
1079 height is essentially the same as that contained in the macro
1080 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1081 the appropriate glyph row has its `mode_line_p' flag set,
1082 and if it doesn't, uses estimate_mode_line_height instead. */
1083
1084 if (WINDOW_WANTS_MODELINE_P (w))
1085 {
1086 struct glyph_row *ml_row
1087 = (w->current_matrix && w->current_matrix->rows
1088 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1089 : 0);
1090 if (ml_row && ml_row->mode_line_p)
1091 height -= ml_row->height;
1092 else
1093 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1094 }
1095
1096 if (WINDOW_WANTS_HEADER_LINE_P (w))
1097 {
1098 struct glyph_row *hl_row
1099 = (w->current_matrix && w->current_matrix->rows
1100 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1101 : 0);
1102 if (hl_row && hl_row->mode_line_p)
1103 height -= hl_row->height;
1104 else
1105 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1106 }
1107
1108 /* With a very small font and a mode-line that's taller than
1109 default, we might end up with a negative height. */
1110 return max (0, height);
1111 }
1112
1113 /* Return the window-relative coordinate of the left edge of display
1114 area AREA of window W. AREA < 0 means return the left edge of the
1115 whole window, to the right of the left fringe of W. */
1116
1117 INLINE int
1118 window_box_left_offset (w, area)
1119 struct window *w;
1120 int area;
1121 {
1122 int x;
1123
1124 if (w->pseudo_window_p)
1125 return 0;
1126
1127 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1128
1129 if (area == TEXT_AREA)
1130 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1131 + window_box_width (w, LEFT_MARGIN_AREA));
1132 else if (area == RIGHT_MARGIN_AREA)
1133 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1134 + window_box_width (w, LEFT_MARGIN_AREA)
1135 + window_box_width (w, TEXT_AREA)
1136 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1137 ? 0
1138 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1139 else if (area == LEFT_MARGIN_AREA
1140 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1141 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1142
1143 return x;
1144 }
1145
1146
1147 /* Return the window-relative coordinate of the right edge of display
1148 area AREA of window W. AREA < 0 means return the left edge of the
1149 whole window, to the left of the right fringe of W. */
1150
1151 INLINE int
1152 window_box_right_offset (w, area)
1153 struct window *w;
1154 int area;
1155 {
1156 return window_box_left_offset (w, area) + window_box_width (w, area);
1157 }
1158
1159 /* Return the frame-relative coordinate of the left edge of display
1160 area AREA of window W. AREA < 0 means return the left edge of the
1161 whole window, to the right of the left fringe of W. */
1162
1163 INLINE int
1164 window_box_left (w, area)
1165 struct window *w;
1166 int area;
1167 {
1168 struct frame *f = XFRAME (w->frame);
1169 int x;
1170
1171 if (w->pseudo_window_p)
1172 return FRAME_INTERNAL_BORDER_WIDTH (f);
1173
1174 x = (WINDOW_LEFT_EDGE_X (w)
1175 + window_box_left_offset (w, area));
1176
1177 return x;
1178 }
1179
1180
1181 /* Return the frame-relative coordinate of the right edge of display
1182 area AREA of window W. AREA < 0 means return the left edge of the
1183 whole window, to the left of the right fringe of W. */
1184
1185 INLINE int
1186 window_box_right (w, area)
1187 struct window *w;
1188 int area;
1189 {
1190 return window_box_left (w, area) + window_box_width (w, area);
1191 }
1192
1193 /* Get the bounding box of the display area AREA of window W, without
1194 mode lines, in frame-relative coordinates. AREA < 0 means the
1195 whole window, not including the left and right fringes of
1196 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1197 coordinates of the upper-left corner of the box. Return in
1198 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1199
1200 INLINE void
1201 window_box (w, area, box_x, box_y, box_width, box_height)
1202 struct window *w;
1203 int area;
1204 int *box_x, *box_y, *box_width, *box_height;
1205 {
1206 if (box_width)
1207 *box_width = window_box_width (w, area);
1208 if (box_height)
1209 *box_height = window_box_height (w);
1210 if (box_x)
1211 *box_x = window_box_left (w, area);
1212 if (box_y)
1213 {
1214 *box_y = WINDOW_TOP_EDGE_Y (w);
1215 if (WINDOW_WANTS_HEADER_LINE_P (w))
1216 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1217 }
1218 }
1219
1220
1221 /* Get the bounding box of the display area AREA of window W, without
1222 mode lines. AREA < 0 means the whole window, not including the
1223 left and right fringe of the window. Return in *TOP_LEFT_X
1224 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1225 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1226 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1227 box. */
1228
1229 INLINE void
1230 window_box_edges (w, area, top_left_x, top_left_y,
1231 bottom_right_x, bottom_right_y)
1232 struct window *w;
1233 int area;
1234 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1235 {
1236 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1237 bottom_right_y);
1238 *bottom_right_x += *top_left_x;
1239 *bottom_right_y += *top_left_y;
1240 }
1241
1242
1243 \f
1244 /***********************************************************************
1245 Utilities
1246 ***********************************************************************/
1247
1248 /* Return the bottom y-position of the line the iterator IT is in.
1249 This can modify IT's settings. */
1250
1251 int
1252 line_bottom_y (it)
1253 struct it *it;
1254 {
1255 int line_height = it->max_ascent + it->max_descent;
1256 int line_top_y = it->current_y;
1257
1258 if (line_height == 0)
1259 {
1260 if (last_height)
1261 line_height = last_height;
1262 else if (IT_CHARPOS (*it) < ZV)
1263 {
1264 move_it_by_lines (it, 1, 1);
1265 line_height = (it->max_ascent || it->max_descent
1266 ? it->max_ascent + it->max_descent
1267 : last_height);
1268 }
1269 else
1270 {
1271 struct glyph_row *row = it->glyph_row;
1272
1273 /* Use the default character height. */
1274 it->glyph_row = NULL;
1275 it->what = IT_CHARACTER;
1276 it->c = ' ';
1277 it->len = 1;
1278 PRODUCE_GLYPHS (it);
1279 line_height = it->ascent + it->descent;
1280 it->glyph_row = row;
1281 }
1282 }
1283
1284 return line_top_y + line_height;
1285 }
1286
1287
1288 /* Return 1 if position CHARPOS is visible in window W.
1289 CHARPOS < 0 means return info about WINDOW_END position.
1290 If visible, set *X and *Y to pixel coordinates of top left corner.
1291 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1292 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1293
1294 int
1295 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1296 struct window *w;
1297 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1298 {
1299 struct it it;
1300 struct text_pos top;
1301 int visible_p = 0;
1302 struct buffer *old_buffer = NULL;
1303
1304 if (noninteractive)
1305 return visible_p;
1306
1307 if (XBUFFER (w->buffer) != current_buffer)
1308 {
1309 old_buffer = current_buffer;
1310 set_buffer_internal_1 (XBUFFER (w->buffer));
1311 }
1312
1313 SET_TEXT_POS_FROM_MARKER (top, w->start);
1314
1315 /* Compute exact mode line heights. */
1316 if (WINDOW_WANTS_MODELINE_P (w))
1317 current_mode_line_height
1318 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1319 current_buffer->mode_line_format);
1320
1321 if (WINDOW_WANTS_HEADER_LINE_P (w))
1322 current_header_line_height
1323 = display_mode_line (w, HEADER_LINE_FACE_ID,
1324 current_buffer->header_line_format);
1325
1326 start_display (&it, w, top);
1327 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1328 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1329
1330 /* Note that we may overshoot because of invisible text. */
1331 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1332 {
1333 int top_x = it.current_x;
1334 int top_y = it.current_y;
1335 int bottom_y = (last_height = 0, line_bottom_y (&it));
1336 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1337
1338 if (top_y < window_top_y)
1339 visible_p = bottom_y > window_top_y;
1340 else if (top_y < it.last_visible_y)
1341 visible_p = 1;
1342 if (visible_p)
1343 {
1344 *x = top_x;
1345 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1346 *rtop = max (0, window_top_y - top_y);
1347 *rbot = max (0, bottom_y - it.last_visible_y);
1348 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1349 - max (top_y, window_top_y)));
1350 *vpos = it.vpos;
1351 }
1352 }
1353 else
1354 {
1355 struct it it2;
1356
1357 it2 = it;
1358 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1359 move_it_by_lines (&it, 1, 0);
1360 if (charpos < IT_CHARPOS (it)
1361 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1362 {
1363 visible_p = 1;
1364 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1365 *x = it2.current_x;
1366 *y = it2.current_y + it2.max_ascent - it2.ascent;
1367 *rtop = max (0, -it2.current_y);
1368 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1369 - it.last_visible_y));
1370 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1371 it.last_visible_y)
1372 - max (it2.current_y,
1373 WINDOW_HEADER_LINE_HEIGHT (w))));
1374 *vpos = it2.vpos;
1375 }
1376 }
1377
1378 if (old_buffer)
1379 set_buffer_internal_1 (old_buffer);
1380
1381 current_header_line_height = current_mode_line_height = -1;
1382
1383 if (visible_p && XFASTINT (w->hscroll) > 0)
1384 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1385
1386 #if 0
1387 /* Debugging code. */
1388 if (visible_p)
1389 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1390 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1391 else
1392 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1393 #endif
1394
1395 return visible_p;
1396 }
1397
1398
1399 /* Return the next character from STR which is MAXLEN bytes long.
1400 Return in *LEN the length of the character. This is like
1401 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1402 we find one, we return a `?', but with the length of the invalid
1403 character. */
1404
1405 static INLINE int
1406 string_char_and_length (str, maxlen, len)
1407 const unsigned char *str;
1408 int maxlen, *len;
1409 {
1410 int c;
1411
1412 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1413 if (!CHAR_VALID_P (c, 1))
1414 /* We may not change the length here because other places in Emacs
1415 don't use this function, i.e. they silently accept invalid
1416 characters. */
1417 c = '?';
1418
1419 return c;
1420 }
1421
1422
1423
1424 /* Given a position POS containing a valid character and byte position
1425 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1426
1427 static struct text_pos
1428 string_pos_nchars_ahead (pos, string, nchars)
1429 struct text_pos pos;
1430 Lisp_Object string;
1431 int nchars;
1432 {
1433 xassert (STRINGP (string) && nchars >= 0);
1434
1435 if (STRING_MULTIBYTE (string))
1436 {
1437 int rest = SBYTES (string) - BYTEPOS (pos);
1438 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1439 int len;
1440
1441 while (nchars--)
1442 {
1443 string_char_and_length (p, rest, &len);
1444 p += len, rest -= len;
1445 xassert (rest >= 0);
1446 CHARPOS (pos) += 1;
1447 BYTEPOS (pos) += len;
1448 }
1449 }
1450 else
1451 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1452
1453 return pos;
1454 }
1455
1456
1457 /* Value is the text position, i.e. character and byte position,
1458 for character position CHARPOS in STRING. */
1459
1460 static INLINE struct text_pos
1461 string_pos (charpos, string)
1462 int charpos;
1463 Lisp_Object string;
1464 {
1465 struct text_pos pos;
1466 xassert (STRINGP (string));
1467 xassert (charpos >= 0);
1468 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1469 return pos;
1470 }
1471
1472
1473 /* Value is a text position, i.e. character and byte position, for
1474 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1475 means recognize multibyte characters. */
1476
1477 static struct text_pos
1478 c_string_pos (charpos, s, multibyte_p)
1479 int charpos;
1480 unsigned char *s;
1481 int multibyte_p;
1482 {
1483 struct text_pos pos;
1484
1485 xassert (s != NULL);
1486 xassert (charpos >= 0);
1487
1488 if (multibyte_p)
1489 {
1490 int rest = strlen (s), len;
1491
1492 SET_TEXT_POS (pos, 0, 0);
1493 while (charpos--)
1494 {
1495 string_char_and_length (s, rest, &len);
1496 s += len, rest -= len;
1497 xassert (rest >= 0);
1498 CHARPOS (pos) += 1;
1499 BYTEPOS (pos) += len;
1500 }
1501 }
1502 else
1503 SET_TEXT_POS (pos, charpos, charpos);
1504
1505 return pos;
1506 }
1507
1508
1509 /* Value is the number of characters in C string S. MULTIBYTE_P
1510 non-zero means recognize multibyte characters. */
1511
1512 static int
1513 number_of_chars (s, multibyte_p)
1514 unsigned char *s;
1515 int multibyte_p;
1516 {
1517 int nchars;
1518
1519 if (multibyte_p)
1520 {
1521 int rest = strlen (s), len;
1522 unsigned char *p = (unsigned char *) s;
1523
1524 for (nchars = 0; rest > 0; ++nchars)
1525 {
1526 string_char_and_length (p, rest, &len);
1527 rest -= len, p += len;
1528 }
1529 }
1530 else
1531 nchars = strlen (s);
1532
1533 return nchars;
1534 }
1535
1536
1537 /* Compute byte position NEWPOS->bytepos corresponding to
1538 NEWPOS->charpos. POS is a known position in string STRING.
1539 NEWPOS->charpos must be >= POS.charpos. */
1540
1541 static void
1542 compute_string_pos (newpos, pos, string)
1543 struct text_pos *newpos, pos;
1544 Lisp_Object string;
1545 {
1546 xassert (STRINGP (string));
1547 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1548
1549 if (STRING_MULTIBYTE (string))
1550 *newpos = string_pos_nchars_ahead (pos, string,
1551 CHARPOS (*newpos) - CHARPOS (pos));
1552 else
1553 BYTEPOS (*newpos) = CHARPOS (*newpos);
1554 }
1555
1556 /* EXPORT:
1557 Return an estimation of the pixel height of mode or top lines on
1558 frame F. FACE_ID specifies what line's height to estimate. */
1559
1560 int
1561 estimate_mode_line_height (f, face_id)
1562 struct frame *f;
1563 enum face_id face_id;
1564 {
1565 #ifdef HAVE_WINDOW_SYSTEM
1566 if (FRAME_WINDOW_P (f))
1567 {
1568 int height = FONT_HEIGHT (FRAME_FONT (f));
1569
1570 /* This function is called so early when Emacs starts that the face
1571 cache and mode line face are not yet initialized. */
1572 if (FRAME_FACE_CACHE (f))
1573 {
1574 struct face *face = FACE_FROM_ID (f, face_id);
1575 if (face)
1576 {
1577 if (face->font)
1578 height = FONT_HEIGHT (face->font);
1579 if (face->box_line_width > 0)
1580 height += 2 * face->box_line_width;
1581 }
1582 }
1583
1584 return height;
1585 }
1586 #endif
1587
1588 return 1;
1589 }
1590
1591 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1592 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1593 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1594 not force the value into range. */
1595
1596 void
1597 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1598 FRAME_PTR f;
1599 register int pix_x, pix_y;
1600 int *x, *y;
1601 NativeRectangle *bounds;
1602 int noclip;
1603 {
1604
1605 #ifdef HAVE_WINDOW_SYSTEM
1606 if (FRAME_WINDOW_P (f))
1607 {
1608 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1609 even for negative values. */
1610 if (pix_x < 0)
1611 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1612 if (pix_y < 0)
1613 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1614
1615 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1616 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1617
1618 if (bounds)
1619 STORE_NATIVE_RECT (*bounds,
1620 FRAME_COL_TO_PIXEL_X (f, pix_x),
1621 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1622 FRAME_COLUMN_WIDTH (f) - 1,
1623 FRAME_LINE_HEIGHT (f) - 1);
1624
1625 if (!noclip)
1626 {
1627 if (pix_x < 0)
1628 pix_x = 0;
1629 else if (pix_x > FRAME_TOTAL_COLS (f))
1630 pix_x = FRAME_TOTAL_COLS (f);
1631
1632 if (pix_y < 0)
1633 pix_y = 0;
1634 else if (pix_y > FRAME_LINES (f))
1635 pix_y = FRAME_LINES (f);
1636 }
1637 }
1638 #endif
1639
1640 *x = pix_x;
1641 *y = pix_y;
1642 }
1643
1644
1645 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1646 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1647 can't tell the positions because W's display is not up to date,
1648 return 0. */
1649
1650 int
1651 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1652 struct window *w;
1653 int hpos, vpos;
1654 int *frame_x, *frame_y;
1655 {
1656 #ifdef HAVE_WINDOW_SYSTEM
1657 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1658 {
1659 int success_p;
1660
1661 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1662 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1663
1664 if (display_completed)
1665 {
1666 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1667 struct glyph *glyph = row->glyphs[TEXT_AREA];
1668 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1669
1670 hpos = row->x;
1671 vpos = row->y;
1672 while (glyph < end)
1673 {
1674 hpos += glyph->pixel_width;
1675 ++glyph;
1676 }
1677
1678 /* If first glyph is partially visible, its first visible position is still 0. */
1679 if (hpos < 0)
1680 hpos = 0;
1681
1682 success_p = 1;
1683 }
1684 else
1685 {
1686 hpos = vpos = 0;
1687 success_p = 0;
1688 }
1689
1690 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1691 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1692 return success_p;
1693 }
1694 #endif
1695
1696 *frame_x = hpos;
1697 *frame_y = vpos;
1698 return 1;
1699 }
1700
1701
1702 #ifdef HAVE_WINDOW_SYSTEM
1703
1704 /* Find the glyph under window-relative coordinates X/Y in window W.
1705 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1706 strings. Return in *HPOS and *VPOS the row and column number of
1707 the glyph found. Return in *AREA the glyph area containing X.
1708 Value is a pointer to the glyph found or null if X/Y is not on
1709 text, or we can't tell because W's current matrix is not up to
1710 date. */
1711
1712 static struct glyph *
1713 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1714 struct window *w;
1715 int x, y;
1716 int *hpos, *vpos, *dx, *dy, *area;
1717 {
1718 struct glyph *glyph, *end;
1719 struct glyph_row *row = NULL;
1720 int x0, i;
1721
1722 /* Find row containing Y. Give up if some row is not enabled. */
1723 for (i = 0; i < w->current_matrix->nrows; ++i)
1724 {
1725 row = MATRIX_ROW (w->current_matrix, i);
1726 if (!row->enabled_p)
1727 return NULL;
1728 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1729 break;
1730 }
1731
1732 *vpos = i;
1733 *hpos = 0;
1734
1735 /* Give up if Y is not in the window. */
1736 if (i == w->current_matrix->nrows)
1737 return NULL;
1738
1739 /* Get the glyph area containing X. */
1740 if (w->pseudo_window_p)
1741 {
1742 *area = TEXT_AREA;
1743 x0 = 0;
1744 }
1745 else
1746 {
1747 if (x < window_box_left_offset (w, TEXT_AREA))
1748 {
1749 *area = LEFT_MARGIN_AREA;
1750 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1751 }
1752 else if (x < window_box_right_offset (w, TEXT_AREA))
1753 {
1754 *area = TEXT_AREA;
1755 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1756 }
1757 else
1758 {
1759 *area = RIGHT_MARGIN_AREA;
1760 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1761 }
1762 }
1763
1764 /* Find glyph containing X. */
1765 glyph = row->glyphs[*area];
1766 end = glyph + row->used[*area];
1767 x -= x0;
1768 while (glyph < end && x >= glyph->pixel_width)
1769 {
1770 x -= glyph->pixel_width;
1771 ++glyph;
1772 }
1773
1774 if (glyph == end)
1775 return NULL;
1776
1777 if (dx)
1778 {
1779 *dx = x;
1780 *dy = y - (row->y + row->ascent - glyph->ascent);
1781 }
1782
1783 *hpos = glyph - row->glyphs[*area];
1784 return glyph;
1785 }
1786
1787
1788 /* EXPORT:
1789 Convert frame-relative x/y to coordinates relative to window W.
1790 Takes pseudo-windows into account. */
1791
1792 void
1793 frame_to_window_pixel_xy (w, x, y)
1794 struct window *w;
1795 int *x, *y;
1796 {
1797 if (w->pseudo_window_p)
1798 {
1799 /* A pseudo-window is always full-width, and starts at the
1800 left edge of the frame, plus a frame border. */
1801 struct frame *f = XFRAME (w->frame);
1802 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1803 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1804 }
1805 else
1806 {
1807 *x -= WINDOW_LEFT_EDGE_X (w);
1808 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1809 }
1810 }
1811
1812 /* EXPORT:
1813 Return in RECTS[] at most N clipping rectangles for glyph string S.
1814 Return the number of stored rectangles. */
1815
1816 int
1817 get_glyph_string_clip_rects (s, rects, n)
1818 struct glyph_string *s;
1819 NativeRectangle *rects;
1820 int n;
1821 {
1822 XRectangle r;
1823
1824 if (n <= 0)
1825 return 0;
1826
1827 if (s->row->full_width_p)
1828 {
1829 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1830 r.x = WINDOW_LEFT_EDGE_X (s->w);
1831 r.width = WINDOW_TOTAL_WIDTH (s->w);
1832
1833 /* Unless displaying a mode or menu bar line, which are always
1834 fully visible, clip to the visible part of the row. */
1835 if (s->w->pseudo_window_p)
1836 r.height = s->row->visible_height;
1837 else
1838 r.height = s->height;
1839 }
1840 else
1841 {
1842 /* This is a text line that may be partially visible. */
1843 r.x = window_box_left (s->w, s->area);
1844 r.width = window_box_width (s->w, s->area);
1845 r.height = s->row->visible_height;
1846 }
1847
1848 if (s->clip_head)
1849 if (r.x < s->clip_head->x)
1850 {
1851 if (r.width >= s->clip_head->x - r.x)
1852 r.width -= s->clip_head->x - r.x;
1853 else
1854 r.width = 0;
1855 r.x = s->clip_head->x;
1856 }
1857 if (s->clip_tail)
1858 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1859 {
1860 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1861 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1862 else
1863 r.width = 0;
1864 }
1865
1866 /* If S draws overlapping rows, it's sufficient to use the top and
1867 bottom of the window for clipping because this glyph string
1868 intentionally draws over other lines. */
1869 if (s->for_overlaps)
1870 {
1871 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1872 r.height = window_text_bottom_y (s->w) - r.y;
1873
1874 /* Alas, the above simple strategy does not work for the
1875 environments with anti-aliased text: if the same text is
1876 drawn onto the same place multiple times, it gets thicker.
1877 If the overlap we are processing is for the erased cursor, we
1878 take the intersection with the rectagle of the cursor. */
1879 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1880 {
1881 XRectangle rc, r_save = r;
1882
1883 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1884 rc.y = s->w->phys_cursor.y;
1885 rc.width = s->w->phys_cursor_width;
1886 rc.height = s->w->phys_cursor_height;
1887
1888 x_intersect_rectangles (&r_save, &rc, &r);
1889 }
1890 }
1891 else
1892 {
1893 /* Don't use S->y for clipping because it doesn't take partially
1894 visible lines into account. For example, it can be negative for
1895 partially visible lines at the top of a window. */
1896 if (!s->row->full_width_p
1897 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1898 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1899 else
1900 r.y = max (0, s->row->y);
1901
1902 /* If drawing a tool-bar window, draw it over the internal border
1903 at the top of the window. */
1904 if (WINDOWP (s->f->tool_bar_window)
1905 && s->w == XWINDOW (s->f->tool_bar_window))
1906 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1907 }
1908
1909 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1910
1911 /* If drawing the cursor, don't let glyph draw outside its
1912 advertised boundaries. Cleartype does this under some circumstances. */
1913 if (s->hl == DRAW_CURSOR)
1914 {
1915 struct glyph *glyph = s->first_glyph;
1916 int height, max_y;
1917
1918 if (s->x > r.x)
1919 {
1920 r.width -= s->x - r.x;
1921 r.x = s->x;
1922 }
1923 r.width = min (r.width, glyph->pixel_width);
1924
1925 /* If r.y is below window bottom, ensure that we still see a cursor. */
1926 height = min (glyph->ascent + glyph->descent,
1927 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1928 max_y = window_text_bottom_y (s->w) - height;
1929 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1930 if (s->ybase - glyph->ascent > max_y)
1931 {
1932 r.y = max_y;
1933 r.height = height;
1934 }
1935 else
1936 {
1937 /* Don't draw cursor glyph taller than our actual glyph. */
1938 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1939 if (height < r.height)
1940 {
1941 max_y = r.y + r.height;
1942 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1943 r.height = min (max_y - r.y, height);
1944 }
1945 }
1946 }
1947
1948 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1949 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1950 {
1951 #ifdef CONVERT_FROM_XRECT
1952 CONVERT_FROM_XRECT (r, *rects);
1953 #else
1954 *rects = r;
1955 #endif
1956 return 1;
1957 }
1958 else
1959 {
1960 /* If we are processing overlapping and allowed to return
1961 multiple clipping rectangles, we exclude the row of the glyph
1962 string from the clipping rectangle. This is to avoid drawing
1963 the same text on the environment with anti-aliasing. */
1964 #ifdef CONVERT_FROM_XRECT
1965 XRectangle rs[2];
1966 #else
1967 XRectangle *rs = rects;
1968 #endif
1969 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1970
1971 if (s->for_overlaps & OVERLAPS_PRED)
1972 {
1973 rs[i] = r;
1974 if (r.y + r.height > row_y)
1975 {
1976 if (r.y < row_y)
1977 rs[i].height = row_y - r.y;
1978 else
1979 rs[i].height = 0;
1980 }
1981 i++;
1982 }
1983 if (s->for_overlaps & OVERLAPS_SUCC)
1984 {
1985 rs[i] = r;
1986 if (r.y < row_y + s->row->visible_height)
1987 {
1988 if (r.y + r.height > row_y + s->row->visible_height)
1989 {
1990 rs[i].y = row_y + s->row->visible_height;
1991 rs[i].height = r.y + r.height - rs[i].y;
1992 }
1993 else
1994 rs[i].height = 0;
1995 }
1996 i++;
1997 }
1998
1999 n = i;
2000 #ifdef CONVERT_FROM_XRECT
2001 for (i = 0; i < n; i++)
2002 CONVERT_FROM_XRECT (rs[i], rects[i]);
2003 #endif
2004 return n;
2005 }
2006 }
2007
2008 /* EXPORT:
2009 Return in *NR the clipping rectangle for glyph string S. */
2010
2011 void
2012 get_glyph_string_clip_rect (s, nr)
2013 struct glyph_string *s;
2014 NativeRectangle *nr;
2015 {
2016 get_glyph_string_clip_rects (s, nr, 1);
2017 }
2018
2019
2020 /* EXPORT:
2021 Return the position and height of the phys cursor in window W.
2022 Set w->phys_cursor_width to width of phys cursor.
2023 */
2024
2025 void
2026 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2027 struct window *w;
2028 struct glyph_row *row;
2029 struct glyph *glyph;
2030 int *xp, *yp, *heightp;
2031 {
2032 struct frame *f = XFRAME (WINDOW_FRAME (w));
2033 int x, y, wd, h, h0, y0;
2034
2035 /* Compute the width of the rectangle to draw. If on a stretch
2036 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2037 rectangle as wide as the glyph, but use a canonical character
2038 width instead. */
2039 wd = glyph->pixel_width - 1;
2040 #ifdef HAVE_NTGUI
2041 wd++; /* Why? */
2042 #endif
2043
2044 x = w->phys_cursor.x;
2045 if (x < 0)
2046 {
2047 wd += x;
2048 x = 0;
2049 }
2050
2051 if (glyph->type == STRETCH_GLYPH
2052 && !x_stretch_cursor_p)
2053 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2054 w->phys_cursor_width = wd;
2055
2056 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2057
2058 /* If y is below window bottom, ensure that we still see a cursor. */
2059 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2060
2061 h = max (h0, glyph->ascent + glyph->descent);
2062 h0 = min (h0, glyph->ascent + glyph->descent);
2063
2064 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2065 if (y < y0)
2066 {
2067 h = max (h - (y0 - y) + 1, h0);
2068 y = y0 - 1;
2069 }
2070 else
2071 {
2072 y0 = window_text_bottom_y (w) - h0;
2073 if (y > y0)
2074 {
2075 h += y - y0;
2076 y = y0;
2077 }
2078 }
2079
2080 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2081 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2082 *heightp = h;
2083 }
2084
2085 /*
2086 * Remember which glyph the mouse is over.
2087 */
2088
2089 void
2090 remember_mouse_glyph (f, gx, gy, rect)
2091 struct frame *f;
2092 int gx, gy;
2093 NativeRectangle *rect;
2094 {
2095 Lisp_Object window;
2096 struct window *w;
2097 struct glyph_row *r, *gr, *end_row;
2098 enum window_part part;
2099 enum glyph_row_area area;
2100 int x, y, width, height;
2101
2102 /* Try to determine frame pixel position and size of the glyph under
2103 frame pixel coordinates X/Y on frame F. */
2104
2105 if (!f->glyphs_initialized_p
2106 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2107 NILP (window)))
2108 {
2109 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2110 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2111 goto virtual_glyph;
2112 }
2113
2114 w = XWINDOW (window);
2115 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2116 height = WINDOW_FRAME_LINE_HEIGHT (w);
2117
2118 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2119 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2120
2121 if (w->pseudo_window_p)
2122 {
2123 area = TEXT_AREA;
2124 part = ON_MODE_LINE; /* Don't adjust margin. */
2125 goto text_glyph;
2126 }
2127
2128 switch (part)
2129 {
2130 case ON_LEFT_MARGIN:
2131 area = LEFT_MARGIN_AREA;
2132 goto text_glyph;
2133
2134 case ON_RIGHT_MARGIN:
2135 area = RIGHT_MARGIN_AREA;
2136 goto text_glyph;
2137
2138 case ON_HEADER_LINE:
2139 case ON_MODE_LINE:
2140 gr = (part == ON_HEADER_LINE
2141 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2142 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2143 gy = gr->y;
2144 area = TEXT_AREA;
2145 goto text_glyph_row_found;
2146
2147 case ON_TEXT:
2148 area = TEXT_AREA;
2149
2150 text_glyph:
2151 gr = 0; gy = 0;
2152 for (; r <= end_row && r->enabled_p; ++r)
2153 if (r->y + r->height > y)
2154 {
2155 gr = r; gy = r->y;
2156 break;
2157 }
2158
2159 text_glyph_row_found:
2160 if (gr && gy <= y)
2161 {
2162 struct glyph *g = gr->glyphs[area];
2163 struct glyph *end = g + gr->used[area];
2164
2165 height = gr->height;
2166 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2167 if (gx + g->pixel_width > x)
2168 break;
2169
2170 if (g < end)
2171 {
2172 if (g->type == IMAGE_GLYPH)
2173 {
2174 /* Don't remember when mouse is over image, as
2175 image may have hot-spots. */
2176 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2177 return;
2178 }
2179 width = g->pixel_width;
2180 }
2181 else
2182 {
2183 /* Use nominal char spacing at end of line. */
2184 x -= gx;
2185 gx += (x / width) * width;
2186 }
2187
2188 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2189 gx += window_box_left_offset (w, area);
2190 }
2191 else
2192 {
2193 /* Use nominal line height at end of window. */
2194 gx = (x / width) * width;
2195 y -= gy;
2196 gy += (y / height) * height;
2197 }
2198 break;
2199
2200 case ON_LEFT_FRINGE:
2201 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2202 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2203 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2204 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2205 goto row_glyph;
2206
2207 case ON_RIGHT_FRINGE:
2208 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2209 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2210 : window_box_right_offset (w, TEXT_AREA));
2211 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2212 goto row_glyph;
2213
2214 case ON_SCROLL_BAR:
2215 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2216 ? 0
2217 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2218 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2219 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2220 : 0)));
2221 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2222
2223 row_glyph:
2224 gr = 0, gy = 0;
2225 for (; r <= end_row && r->enabled_p; ++r)
2226 if (r->y + r->height > y)
2227 {
2228 gr = r; gy = r->y;
2229 break;
2230 }
2231
2232 if (gr && gy <= y)
2233 height = gr->height;
2234 else
2235 {
2236 /* Use nominal line height at end of window. */
2237 y -= gy;
2238 gy += (y / height) * height;
2239 }
2240 break;
2241
2242 default:
2243 ;
2244 virtual_glyph:
2245 /* If there is no glyph under the mouse, then we divide the screen
2246 into a grid of the smallest glyph in the frame, and use that
2247 as our "glyph". */
2248
2249 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2250 round down even for negative values. */
2251 if (gx < 0)
2252 gx -= width - 1;
2253 if (gy < 0)
2254 gy -= height - 1;
2255
2256 gx = (gx / width) * width;
2257 gy = (gy / height) * height;
2258
2259 goto store_rect;
2260 }
2261
2262 gx += WINDOW_LEFT_EDGE_X (w);
2263 gy += WINDOW_TOP_EDGE_Y (w);
2264
2265 store_rect:
2266 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2267
2268 /* Visible feedback for debugging. */
2269 #if 0
2270 #if HAVE_X_WINDOWS
2271 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2272 f->output_data.x->normal_gc,
2273 gx, gy, width, height);
2274 #endif
2275 #endif
2276 }
2277
2278
2279 #endif /* HAVE_WINDOW_SYSTEM */
2280
2281 \f
2282 /***********************************************************************
2283 Lisp form evaluation
2284 ***********************************************************************/
2285
2286 /* Error handler for safe_eval and safe_call. */
2287
2288 static Lisp_Object
2289 safe_eval_handler (arg)
2290 Lisp_Object arg;
2291 {
2292 add_to_log ("Error during redisplay: %s", arg, Qnil);
2293 return Qnil;
2294 }
2295
2296
2297 /* Evaluate SEXPR and return the result, or nil if something went
2298 wrong. Prevent redisplay during the evaluation. */
2299
2300 Lisp_Object
2301 safe_eval (sexpr)
2302 Lisp_Object sexpr;
2303 {
2304 Lisp_Object val;
2305
2306 if (inhibit_eval_during_redisplay)
2307 val = Qnil;
2308 else
2309 {
2310 int count = SPECPDL_INDEX ();
2311 struct gcpro gcpro1;
2312
2313 GCPRO1 (sexpr);
2314 specbind (Qinhibit_redisplay, Qt);
2315 /* Use Qt to ensure debugger does not run,
2316 so there is no possibility of wanting to redisplay. */
2317 val = internal_condition_case_1 (Feval, sexpr, Qt,
2318 safe_eval_handler);
2319 UNGCPRO;
2320 val = unbind_to (count, val);
2321 }
2322
2323 return val;
2324 }
2325
2326
2327 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2328 Return the result, or nil if something went wrong. Prevent
2329 redisplay during the evaluation. */
2330
2331 Lisp_Object
2332 safe_call (nargs, args)
2333 int nargs;
2334 Lisp_Object *args;
2335 {
2336 Lisp_Object val;
2337
2338 if (inhibit_eval_during_redisplay)
2339 val = Qnil;
2340 else
2341 {
2342 int count = SPECPDL_INDEX ();
2343 struct gcpro gcpro1;
2344
2345 GCPRO1 (args[0]);
2346 gcpro1.nvars = nargs;
2347 specbind (Qinhibit_redisplay, Qt);
2348 /* Use Qt to ensure debugger does not run,
2349 so there is no possibility of wanting to redisplay. */
2350 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2351 safe_eval_handler);
2352 UNGCPRO;
2353 val = unbind_to (count, val);
2354 }
2355
2356 return val;
2357 }
2358
2359
2360 /* Call function FN with one argument ARG.
2361 Return the result, or nil if something went wrong. */
2362
2363 Lisp_Object
2364 safe_call1 (fn, arg)
2365 Lisp_Object fn, arg;
2366 {
2367 Lisp_Object args[2];
2368 args[0] = fn;
2369 args[1] = arg;
2370 return safe_call (2, args);
2371 }
2372
2373
2374 \f
2375 /***********************************************************************
2376 Debugging
2377 ***********************************************************************/
2378
2379 #if 0
2380
2381 /* Define CHECK_IT to perform sanity checks on iterators.
2382 This is for debugging. It is too slow to do unconditionally. */
2383
2384 static void
2385 check_it (it)
2386 struct it *it;
2387 {
2388 if (it->method == GET_FROM_STRING)
2389 {
2390 xassert (STRINGP (it->string));
2391 xassert (IT_STRING_CHARPOS (*it) >= 0);
2392 }
2393 else
2394 {
2395 xassert (IT_STRING_CHARPOS (*it) < 0);
2396 if (it->method == GET_FROM_BUFFER)
2397 {
2398 /* Check that character and byte positions agree. */
2399 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2400 }
2401 }
2402
2403 if (it->dpvec)
2404 xassert (it->current.dpvec_index >= 0);
2405 else
2406 xassert (it->current.dpvec_index < 0);
2407 }
2408
2409 #define CHECK_IT(IT) check_it ((IT))
2410
2411 #else /* not 0 */
2412
2413 #define CHECK_IT(IT) (void) 0
2414
2415 #endif /* not 0 */
2416
2417
2418 #if GLYPH_DEBUG
2419
2420 /* Check that the window end of window W is what we expect it
2421 to be---the last row in the current matrix displaying text. */
2422
2423 static void
2424 check_window_end (w)
2425 struct window *w;
2426 {
2427 if (!MINI_WINDOW_P (w)
2428 && !NILP (w->window_end_valid))
2429 {
2430 struct glyph_row *row;
2431 xassert ((row = MATRIX_ROW (w->current_matrix,
2432 XFASTINT (w->window_end_vpos)),
2433 !row->enabled_p
2434 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2435 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2436 }
2437 }
2438
2439 #define CHECK_WINDOW_END(W) check_window_end ((W))
2440
2441 #else /* not GLYPH_DEBUG */
2442
2443 #define CHECK_WINDOW_END(W) (void) 0
2444
2445 #endif /* not GLYPH_DEBUG */
2446
2447
2448 \f
2449 /***********************************************************************
2450 Iterator initialization
2451 ***********************************************************************/
2452
2453 /* Initialize IT for displaying current_buffer in window W, starting
2454 at character position CHARPOS. CHARPOS < 0 means that no buffer
2455 position is specified which is useful when the iterator is assigned
2456 a position later. BYTEPOS is the byte position corresponding to
2457 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2458
2459 If ROW is not null, calls to produce_glyphs with IT as parameter
2460 will produce glyphs in that row.
2461
2462 BASE_FACE_ID is the id of a base face to use. It must be one of
2463 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2464 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2465 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2466
2467 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2468 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2469 will be initialized to use the corresponding mode line glyph row of
2470 the desired matrix of W. */
2471
2472 void
2473 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2474 struct it *it;
2475 struct window *w;
2476 int charpos, bytepos;
2477 struct glyph_row *row;
2478 enum face_id base_face_id;
2479 {
2480 int highlight_region_p;
2481
2482 /* Some precondition checks. */
2483 xassert (w != NULL && it != NULL);
2484 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2485 && charpos <= ZV));
2486
2487 /* If face attributes have been changed since the last redisplay,
2488 free realized faces now because they depend on face definitions
2489 that might have changed. Don't free faces while there might be
2490 desired matrices pending which reference these faces. */
2491 if (face_change_count && !inhibit_free_realized_faces)
2492 {
2493 face_change_count = 0;
2494 free_all_realized_faces (Qnil);
2495 }
2496
2497 /* Use one of the mode line rows of W's desired matrix if
2498 appropriate. */
2499 if (row == NULL)
2500 {
2501 if (base_face_id == MODE_LINE_FACE_ID
2502 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2503 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2504 else if (base_face_id == HEADER_LINE_FACE_ID)
2505 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2506 }
2507
2508 /* Clear IT. */
2509 bzero (it, sizeof *it);
2510 it->current.overlay_string_index = -1;
2511 it->current.dpvec_index = -1;
2512 it->base_face_id = base_face_id;
2513 it->string = Qnil;
2514 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2515
2516 /* The window in which we iterate over current_buffer: */
2517 XSETWINDOW (it->window, w);
2518 it->w = w;
2519 it->f = XFRAME (w->frame);
2520
2521 /* Extra space between lines (on window systems only). */
2522 if (base_face_id == DEFAULT_FACE_ID
2523 && FRAME_WINDOW_P (it->f))
2524 {
2525 if (NATNUMP (current_buffer->extra_line_spacing))
2526 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2527 else if (FLOATP (current_buffer->extra_line_spacing))
2528 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2529 * FRAME_LINE_HEIGHT (it->f));
2530 else if (it->f->extra_line_spacing > 0)
2531 it->extra_line_spacing = it->f->extra_line_spacing;
2532 it->max_extra_line_spacing = 0;
2533 }
2534
2535 /* If realized faces have been removed, e.g. because of face
2536 attribute changes of named faces, recompute them. When running
2537 in batch mode, the face cache of Vterminal_frame is null. If
2538 we happen to get called, make a dummy face cache. */
2539 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2540 init_frame_faces (it->f);
2541 if (FRAME_FACE_CACHE (it->f)->used == 0)
2542 recompute_basic_faces (it->f);
2543
2544 /* Current value of the `slice', `space-width', and 'height' properties. */
2545 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2546 it->space_width = Qnil;
2547 it->font_height = Qnil;
2548 it->override_ascent = -1;
2549
2550 /* Are control characters displayed as `^C'? */
2551 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2552
2553 /* -1 means everything between a CR and the following line end
2554 is invisible. >0 means lines indented more than this value are
2555 invisible. */
2556 it->selective = (INTEGERP (current_buffer->selective_display)
2557 ? XFASTINT (current_buffer->selective_display)
2558 : (!NILP (current_buffer->selective_display)
2559 ? -1 : 0));
2560 it->selective_display_ellipsis_p
2561 = !NILP (current_buffer->selective_display_ellipses);
2562
2563 /* Display table to use. */
2564 it->dp = window_display_table (w);
2565
2566 /* Are multibyte characters enabled in current_buffer? */
2567 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2568
2569 /* Non-zero if we should highlight the region. */
2570 highlight_region_p
2571 = (!NILP (Vtransient_mark_mode)
2572 && !NILP (current_buffer->mark_active)
2573 && XMARKER (current_buffer->mark)->buffer != 0);
2574
2575 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2576 start and end of a visible region in window IT->w. Set both to
2577 -1 to indicate no region. */
2578 if (highlight_region_p
2579 /* Maybe highlight only in selected window. */
2580 && (/* Either show region everywhere. */
2581 highlight_nonselected_windows
2582 /* Or show region in the selected window. */
2583 || w == XWINDOW (selected_window)
2584 /* Or show the region if we are in the mini-buffer and W is
2585 the window the mini-buffer refers to. */
2586 || (MINI_WINDOW_P (XWINDOW (selected_window))
2587 && WINDOWP (minibuf_selected_window)
2588 && w == XWINDOW (minibuf_selected_window))))
2589 {
2590 int charpos = marker_position (current_buffer->mark);
2591 it->region_beg_charpos = min (PT, charpos);
2592 it->region_end_charpos = max (PT, charpos);
2593 }
2594 else
2595 it->region_beg_charpos = it->region_end_charpos = -1;
2596
2597 /* Get the position at which the redisplay_end_trigger hook should
2598 be run, if it is to be run at all. */
2599 if (MARKERP (w->redisplay_end_trigger)
2600 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2601 it->redisplay_end_trigger_charpos
2602 = marker_position (w->redisplay_end_trigger);
2603 else if (INTEGERP (w->redisplay_end_trigger))
2604 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2605
2606 /* Correct bogus values of tab_width. */
2607 it->tab_width = XINT (current_buffer->tab_width);
2608 if (it->tab_width <= 0 || it->tab_width > 1000)
2609 it->tab_width = 8;
2610
2611 /* Are lines in the display truncated? */
2612 it->truncate_lines_p
2613 = (base_face_id != DEFAULT_FACE_ID
2614 || XINT (it->w->hscroll)
2615 || (truncate_partial_width_windows
2616 && !WINDOW_FULL_WIDTH_P (it->w))
2617 || !NILP (current_buffer->truncate_lines));
2618
2619 /* Get dimensions of truncation and continuation glyphs. These are
2620 displayed as fringe bitmaps under X, so we don't need them for such
2621 frames. */
2622 if (!FRAME_WINDOW_P (it->f))
2623 {
2624 if (it->truncate_lines_p)
2625 {
2626 /* We will need the truncation glyph. */
2627 xassert (it->glyph_row == NULL);
2628 produce_special_glyphs (it, IT_TRUNCATION);
2629 it->truncation_pixel_width = it->pixel_width;
2630 }
2631 else
2632 {
2633 /* We will need the continuation glyph. */
2634 xassert (it->glyph_row == NULL);
2635 produce_special_glyphs (it, IT_CONTINUATION);
2636 it->continuation_pixel_width = it->pixel_width;
2637 }
2638
2639 /* Reset these values to zero because the produce_special_glyphs
2640 above has changed them. */
2641 it->pixel_width = it->ascent = it->descent = 0;
2642 it->phys_ascent = it->phys_descent = 0;
2643 }
2644
2645 /* Set this after getting the dimensions of truncation and
2646 continuation glyphs, so that we don't produce glyphs when calling
2647 produce_special_glyphs, above. */
2648 it->glyph_row = row;
2649 it->area = TEXT_AREA;
2650
2651 /* Get the dimensions of the display area. The display area
2652 consists of the visible window area plus a horizontally scrolled
2653 part to the left of the window. All x-values are relative to the
2654 start of this total display area. */
2655 if (base_face_id != DEFAULT_FACE_ID)
2656 {
2657 /* Mode lines, menu bar in terminal frames. */
2658 it->first_visible_x = 0;
2659 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2660 }
2661 else
2662 {
2663 it->first_visible_x
2664 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2665 it->last_visible_x = (it->first_visible_x
2666 + window_box_width (w, TEXT_AREA));
2667
2668 /* If we truncate lines, leave room for the truncator glyph(s) at
2669 the right margin. Otherwise, leave room for the continuation
2670 glyph(s). Truncation and continuation glyphs are not inserted
2671 for window-based redisplay. */
2672 if (!FRAME_WINDOW_P (it->f))
2673 {
2674 if (it->truncate_lines_p)
2675 it->last_visible_x -= it->truncation_pixel_width;
2676 else
2677 it->last_visible_x -= it->continuation_pixel_width;
2678 }
2679
2680 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2681 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2682 }
2683
2684 /* Leave room for a border glyph. */
2685 if (!FRAME_WINDOW_P (it->f)
2686 && !WINDOW_RIGHTMOST_P (it->w))
2687 it->last_visible_x -= 1;
2688
2689 it->last_visible_y = window_text_bottom_y (w);
2690
2691 /* For mode lines and alike, arrange for the first glyph having a
2692 left box line if the face specifies a box. */
2693 if (base_face_id != DEFAULT_FACE_ID)
2694 {
2695 struct face *face;
2696
2697 it->face_id = base_face_id;
2698
2699 /* If we have a boxed mode line, make the first character appear
2700 with a left box line. */
2701 face = FACE_FROM_ID (it->f, base_face_id);
2702 if (face->box != FACE_NO_BOX)
2703 it->start_of_box_run_p = 1;
2704 }
2705
2706 /* If a buffer position was specified, set the iterator there,
2707 getting overlays and face properties from that position. */
2708 if (charpos >= BUF_BEG (current_buffer))
2709 {
2710 it->end_charpos = ZV;
2711 it->face_id = -1;
2712 IT_CHARPOS (*it) = charpos;
2713
2714 /* Compute byte position if not specified. */
2715 if (bytepos < charpos)
2716 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2717 else
2718 IT_BYTEPOS (*it) = bytepos;
2719
2720 it->start = it->current;
2721
2722 /* Compute faces etc. */
2723 reseat (it, it->current.pos, 1);
2724 }
2725
2726 CHECK_IT (it);
2727 }
2728
2729
2730 /* Initialize IT for the display of window W with window start POS. */
2731
2732 void
2733 start_display (it, w, pos)
2734 struct it *it;
2735 struct window *w;
2736 struct text_pos pos;
2737 {
2738 struct glyph_row *row;
2739 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2740
2741 row = w->desired_matrix->rows + first_vpos;
2742 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2743 it->first_vpos = first_vpos;
2744
2745 /* Don't reseat to previous visible line start if current start
2746 position is in a string or image. */
2747 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2748 {
2749 int start_at_line_beg_p;
2750 int first_y = it->current_y;
2751
2752 /* If window start is not at a line start, skip forward to POS to
2753 get the correct continuation lines width. */
2754 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2755 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2756 if (!start_at_line_beg_p)
2757 {
2758 int new_x;
2759
2760 reseat_at_previous_visible_line_start (it);
2761 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2762
2763 new_x = it->current_x + it->pixel_width;
2764
2765 /* If lines are continued, this line may end in the middle
2766 of a multi-glyph character (e.g. a control character
2767 displayed as \003, or in the middle of an overlay
2768 string). In this case move_it_to above will not have
2769 taken us to the start of the continuation line but to the
2770 end of the continued line. */
2771 if (it->current_x > 0
2772 && !it->truncate_lines_p /* Lines are continued. */
2773 && (/* And glyph doesn't fit on the line. */
2774 new_x > it->last_visible_x
2775 /* Or it fits exactly and we're on a window
2776 system frame. */
2777 || (new_x == it->last_visible_x
2778 && FRAME_WINDOW_P (it->f))))
2779 {
2780 if (it->current.dpvec_index >= 0
2781 || it->current.overlay_string_index >= 0)
2782 {
2783 set_iterator_to_next (it, 1);
2784 move_it_in_display_line_to (it, -1, -1, 0);
2785 }
2786
2787 it->continuation_lines_width += it->current_x;
2788 }
2789
2790 /* We're starting a new display line, not affected by the
2791 height of the continued line, so clear the appropriate
2792 fields in the iterator structure. */
2793 it->max_ascent = it->max_descent = 0;
2794 it->max_phys_ascent = it->max_phys_descent = 0;
2795
2796 it->current_y = first_y;
2797 it->vpos = 0;
2798 it->current_x = it->hpos = 0;
2799 }
2800 }
2801
2802 #if 0 /* Don't assert the following because start_display is sometimes
2803 called intentionally with a window start that is not at a
2804 line start. Please leave this code in as a comment. */
2805
2806 /* Window start should be on a line start, now. */
2807 xassert (it->continuation_lines_width
2808 || IT_CHARPOS (it) == BEGV
2809 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2810 #endif /* 0 */
2811 }
2812
2813
2814 /* Return 1 if POS is a position in ellipses displayed for invisible
2815 text. W is the window we display, for text property lookup. */
2816
2817 static int
2818 in_ellipses_for_invisible_text_p (pos, w)
2819 struct display_pos *pos;
2820 struct window *w;
2821 {
2822 Lisp_Object prop, window;
2823 int ellipses_p = 0;
2824 int charpos = CHARPOS (pos->pos);
2825
2826 /* If POS specifies a position in a display vector, this might
2827 be for an ellipsis displayed for invisible text. We won't
2828 get the iterator set up for delivering that ellipsis unless
2829 we make sure that it gets aware of the invisible text. */
2830 if (pos->dpvec_index >= 0
2831 && pos->overlay_string_index < 0
2832 && CHARPOS (pos->string_pos) < 0
2833 && charpos > BEGV
2834 && (XSETWINDOW (window, w),
2835 prop = Fget_char_property (make_number (charpos),
2836 Qinvisible, window),
2837 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2838 {
2839 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2840 window);
2841 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2842 }
2843
2844 return ellipses_p;
2845 }
2846
2847
2848 /* Initialize IT for stepping through current_buffer in window W,
2849 starting at position POS that includes overlay string and display
2850 vector/ control character translation position information. Value
2851 is zero if there are overlay strings with newlines at POS. */
2852
2853 static int
2854 init_from_display_pos (it, w, pos)
2855 struct it *it;
2856 struct window *w;
2857 struct display_pos *pos;
2858 {
2859 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2860 int i, overlay_strings_with_newlines = 0;
2861
2862 /* If POS specifies a position in a display vector, this might
2863 be for an ellipsis displayed for invisible text. We won't
2864 get the iterator set up for delivering that ellipsis unless
2865 we make sure that it gets aware of the invisible text. */
2866 if (in_ellipses_for_invisible_text_p (pos, w))
2867 {
2868 --charpos;
2869 bytepos = 0;
2870 }
2871
2872 /* Keep in mind: the call to reseat in init_iterator skips invisible
2873 text, so we might end up at a position different from POS. This
2874 is only a problem when POS is a row start after a newline and an
2875 overlay starts there with an after-string, and the overlay has an
2876 invisible property. Since we don't skip invisible text in
2877 display_line and elsewhere immediately after consuming the
2878 newline before the row start, such a POS will not be in a string,
2879 but the call to init_iterator below will move us to the
2880 after-string. */
2881 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2882
2883 /* This only scans the current chunk -- it should scan all chunks.
2884 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2885 to 16 in 22.1 to make this a lesser problem. */
2886 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2887 {
2888 const char *s = SDATA (it->overlay_strings[i]);
2889 const char *e = s + SBYTES (it->overlay_strings[i]);
2890
2891 while (s < e && *s != '\n')
2892 ++s;
2893
2894 if (s < e)
2895 {
2896 overlay_strings_with_newlines = 1;
2897 break;
2898 }
2899 }
2900
2901 /* If position is within an overlay string, set up IT to the right
2902 overlay string. */
2903 if (pos->overlay_string_index >= 0)
2904 {
2905 int relative_index;
2906
2907 /* If the first overlay string happens to have a `display'
2908 property for an image, the iterator will be set up for that
2909 image, and we have to undo that setup first before we can
2910 correct the overlay string index. */
2911 if (it->method == GET_FROM_IMAGE)
2912 pop_it (it);
2913
2914 /* We already have the first chunk of overlay strings in
2915 IT->overlay_strings. Load more until the one for
2916 pos->overlay_string_index is in IT->overlay_strings. */
2917 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2918 {
2919 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2920 it->current.overlay_string_index = 0;
2921 while (n--)
2922 {
2923 load_overlay_strings (it, 0);
2924 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2925 }
2926 }
2927
2928 it->current.overlay_string_index = pos->overlay_string_index;
2929 relative_index = (it->current.overlay_string_index
2930 % OVERLAY_STRING_CHUNK_SIZE);
2931 it->string = it->overlay_strings[relative_index];
2932 xassert (STRINGP (it->string));
2933 it->current.string_pos = pos->string_pos;
2934 it->method = GET_FROM_STRING;
2935 }
2936
2937 #if 0 /* This is bogus because POS not having an overlay string
2938 position does not mean it's after the string. Example: A
2939 line starting with a before-string and initialization of IT
2940 to the previous row's end position. */
2941 else if (it->current.overlay_string_index >= 0)
2942 {
2943 /* If POS says we're already after an overlay string ending at
2944 POS, make sure to pop the iterator because it will be in
2945 front of that overlay string. When POS is ZV, we've thereby
2946 also ``processed'' overlay strings at ZV. */
2947 while (it->sp)
2948 pop_it (it);
2949 xassert (it->current.overlay_string_index == -1);
2950 xassert (it->method == GET_FROM_BUFFER);
2951 if (CHARPOS (pos->pos) == ZV)
2952 it->overlay_strings_at_end_processed_p = 1;
2953 }
2954 #endif /* 0 */
2955
2956 if (CHARPOS (pos->string_pos) >= 0)
2957 {
2958 /* Recorded position is not in an overlay string, but in another
2959 string. This can only be a string from a `display' property.
2960 IT should already be filled with that string. */
2961 it->current.string_pos = pos->string_pos;
2962 xassert (STRINGP (it->string));
2963 }
2964
2965 /* Restore position in display vector translations, control
2966 character translations or ellipses. */
2967 if (pos->dpvec_index >= 0)
2968 {
2969 if (it->dpvec == NULL)
2970 get_next_display_element (it);
2971 xassert (it->dpvec && it->current.dpvec_index == 0);
2972 it->current.dpvec_index = pos->dpvec_index;
2973 }
2974
2975 CHECK_IT (it);
2976 return !overlay_strings_with_newlines;
2977 }
2978
2979
2980 /* Initialize IT for stepping through current_buffer in window W
2981 starting at ROW->start. */
2982
2983 static void
2984 init_to_row_start (it, w, row)
2985 struct it *it;
2986 struct window *w;
2987 struct glyph_row *row;
2988 {
2989 init_from_display_pos (it, w, &row->start);
2990 it->start = row->start;
2991 it->continuation_lines_width = row->continuation_lines_width;
2992 CHECK_IT (it);
2993 }
2994
2995
2996 /* Initialize IT for stepping through current_buffer in window W
2997 starting in the line following ROW, i.e. starting at ROW->end.
2998 Value is zero if there are overlay strings with newlines at ROW's
2999 end position. */
3000
3001 static int
3002 init_to_row_end (it, w, row)
3003 struct it *it;
3004 struct window *w;
3005 struct glyph_row *row;
3006 {
3007 int success = 0;
3008
3009 if (init_from_display_pos (it, w, &row->end))
3010 {
3011 if (row->continued_p)
3012 it->continuation_lines_width
3013 = row->continuation_lines_width + row->pixel_width;
3014 CHECK_IT (it);
3015 success = 1;
3016 }
3017
3018 return success;
3019 }
3020
3021
3022
3023 \f
3024 /***********************************************************************
3025 Text properties
3026 ***********************************************************************/
3027
3028 /* Called when IT reaches IT->stop_charpos. Handle text property and
3029 overlay changes. Set IT->stop_charpos to the next position where
3030 to stop. */
3031
3032 static void
3033 handle_stop (it)
3034 struct it *it;
3035 {
3036 enum prop_handled handled;
3037 int handle_overlay_change_p;
3038 struct props *p;
3039
3040 it->dpvec = NULL;
3041 it->current.dpvec_index = -1;
3042 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3043 it->ignore_overlay_strings_at_pos_p = 0;
3044
3045 /* Use face of preceding text for ellipsis (if invisible) */
3046 if (it->selective_display_ellipsis_p)
3047 it->saved_face_id = it->face_id;
3048
3049 do
3050 {
3051 handled = HANDLED_NORMALLY;
3052
3053 /* Call text property handlers. */
3054 for (p = it_props; p->handler; ++p)
3055 {
3056 handled = p->handler (it);
3057
3058 if (handled == HANDLED_RECOMPUTE_PROPS)
3059 break;
3060 else if (handled == HANDLED_RETURN)
3061 {
3062 /* We still want to show before and after strings from
3063 overlays even if the actual buffer text is replaced. */
3064 if (!handle_overlay_change_p || it->sp > 1)
3065 return;
3066 if (!get_overlay_strings_1 (it, 0, 0))
3067 return;
3068 it->ignore_overlay_strings_at_pos_p = 1;
3069 it->string_from_display_prop_p = 0;
3070 handle_overlay_change_p = 0;
3071 handled = HANDLED_RECOMPUTE_PROPS;
3072 break;
3073 }
3074 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3075 handle_overlay_change_p = 0;
3076 }
3077
3078 if (handled != HANDLED_RECOMPUTE_PROPS)
3079 {
3080 /* Don't check for overlay strings below when set to deliver
3081 characters from a display vector. */
3082 if (it->method == GET_FROM_DISPLAY_VECTOR)
3083 handle_overlay_change_p = 0;
3084
3085 /* Handle overlay changes. */
3086 if (handle_overlay_change_p)
3087 handled = handle_overlay_change (it);
3088
3089 /* Determine where to stop next. */
3090 if (handled == HANDLED_NORMALLY)
3091 compute_stop_pos (it);
3092 }
3093 }
3094 while (handled == HANDLED_RECOMPUTE_PROPS);
3095 }
3096
3097
3098 /* Compute IT->stop_charpos from text property and overlay change
3099 information for IT's current position. */
3100
3101 static void
3102 compute_stop_pos (it)
3103 struct it *it;
3104 {
3105 register INTERVAL iv, next_iv;
3106 Lisp_Object object, limit, position;
3107
3108 /* If nowhere else, stop at the end. */
3109 it->stop_charpos = it->end_charpos;
3110
3111 if (STRINGP (it->string))
3112 {
3113 /* Strings are usually short, so don't limit the search for
3114 properties. */
3115 object = it->string;
3116 limit = Qnil;
3117 position = make_number (IT_STRING_CHARPOS (*it));
3118 }
3119 else
3120 {
3121 int charpos;
3122
3123 /* If next overlay change is in front of the current stop pos
3124 (which is IT->end_charpos), stop there. Note: value of
3125 next_overlay_change is point-max if no overlay change
3126 follows. */
3127 charpos = next_overlay_change (IT_CHARPOS (*it));
3128 if (charpos < it->stop_charpos)
3129 it->stop_charpos = charpos;
3130
3131 /* If showing the region, we have to stop at the region
3132 start or end because the face might change there. */
3133 if (it->region_beg_charpos > 0)
3134 {
3135 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3136 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3137 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3138 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3139 }
3140
3141 /* Set up variables for computing the stop position from text
3142 property changes. */
3143 XSETBUFFER (object, current_buffer);
3144 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3145 position = make_number (IT_CHARPOS (*it));
3146
3147 }
3148
3149 /* Get the interval containing IT's position. Value is a null
3150 interval if there isn't such an interval. */
3151 iv = validate_interval_range (object, &position, &position, 0);
3152 if (!NULL_INTERVAL_P (iv))
3153 {
3154 Lisp_Object values_here[LAST_PROP_IDX];
3155 struct props *p;
3156
3157 /* Get properties here. */
3158 for (p = it_props; p->handler; ++p)
3159 values_here[p->idx] = textget (iv->plist, *p->name);
3160
3161 /* Look for an interval following iv that has different
3162 properties. */
3163 for (next_iv = next_interval (iv);
3164 (!NULL_INTERVAL_P (next_iv)
3165 && (NILP (limit)
3166 || XFASTINT (limit) > next_iv->position));
3167 next_iv = next_interval (next_iv))
3168 {
3169 for (p = it_props; p->handler; ++p)
3170 {
3171 Lisp_Object new_value;
3172
3173 new_value = textget (next_iv->plist, *p->name);
3174 if (!EQ (values_here[p->idx], new_value))
3175 break;
3176 }
3177
3178 if (p->handler)
3179 break;
3180 }
3181
3182 if (!NULL_INTERVAL_P (next_iv))
3183 {
3184 if (INTEGERP (limit)
3185 && next_iv->position >= XFASTINT (limit))
3186 /* No text property change up to limit. */
3187 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3188 else
3189 /* Text properties change in next_iv. */
3190 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3191 }
3192 }
3193
3194 xassert (STRINGP (it->string)
3195 || (it->stop_charpos >= BEGV
3196 && it->stop_charpos >= IT_CHARPOS (*it)));
3197 }
3198
3199
3200 /* Return the position of the next overlay change after POS in
3201 current_buffer. Value is point-max if no overlay change
3202 follows. This is like `next-overlay-change' but doesn't use
3203 xmalloc. */
3204
3205 static int
3206 next_overlay_change (pos)
3207 int pos;
3208 {
3209 int noverlays;
3210 int endpos;
3211 Lisp_Object *overlays;
3212 int i;
3213
3214 /* Get all overlays at the given position. */
3215 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3216
3217 /* If any of these overlays ends before endpos,
3218 use its ending point instead. */
3219 for (i = 0; i < noverlays; ++i)
3220 {
3221 Lisp_Object oend;
3222 int oendpos;
3223
3224 oend = OVERLAY_END (overlays[i]);
3225 oendpos = OVERLAY_POSITION (oend);
3226 endpos = min (endpos, oendpos);
3227 }
3228
3229 return endpos;
3230 }
3231
3232
3233 \f
3234 /***********************************************************************
3235 Fontification
3236 ***********************************************************************/
3237
3238 /* Handle changes in the `fontified' property of the current buffer by
3239 calling hook functions from Qfontification_functions to fontify
3240 regions of text. */
3241
3242 static enum prop_handled
3243 handle_fontified_prop (it)
3244 struct it *it;
3245 {
3246 Lisp_Object prop, pos;
3247 enum prop_handled handled = HANDLED_NORMALLY;
3248
3249 if (!NILP (Vmemory_full))
3250 return handled;
3251
3252 /* Get the value of the `fontified' property at IT's current buffer
3253 position. (The `fontified' property doesn't have a special
3254 meaning in strings.) If the value is nil, call functions from
3255 Qfontification_functions. */
3256 if (!STRINGP (it->string)
3257 && it->s == NULL
3258 && !NILP (Vfontification_functions)
3259 && !NILP (Vrun_hooks)
3260 && (pos = make_number (IT_CHARPOS (*it)),
3261 prop = Fget_char_property (pos, Qfontified, Qnil),
3262 /* Ignore the special cased nil value always present at EOB since
3263 no amount of fontifying will be able to change it. */
3264 NILP (prop) && IT_CHARPOS (*it) < Z))
3265 {
3266 int count = SPECPDL_INDEX ();
3267 Lisp_Object val;
3268
3269 val = Vfontification_functions;
3270 specbind (Qfontification_functions, Qnil);
3271
3272 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3273 safe_call1 (val, pos);
3274 else
3275 {
3276 Lisp_Object globals, fn;
3277 struct gcpro gcpro1, gcpro2;
3278
3279 globals = Qnil;
3280 GCPRO2 (val, globals);
3281
3282 for (; CONSP (val); val = XCDR (val))
3283 {
3284 fn = XCAR (val);
3285
3286 if (EQ (fn, Qt))
3287 {
3288 /* A value of t indicates this hook has a local
3289 binding; it means to run the global binding too.
3290 In a global value, t should not occur. If it
3291 does, we must ignore it to avoid an endless
3292 loop. */
3293 for (globals = Fdefault_value (Qfontification_functions);
3294 CONSP (globals);
3295 globals = XCDR (globals))
3296 {
3297 fn = XCAR (globals);
3298 if (!EQ (fn, Qt))
3299 safe_call1 (fn, pos);
3300 }
3301 }
3302 else
3303 safe_call1 (fn, pos);
3304 }
3305
3306 UNGCPRO;
3307 }
3308
3309 unbind_to (count, Qnil);
3310
3311 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3312 something. This avoids an endless loop if they failed to
3313 fontify the text for which reason ever. */
3314 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3315 handled = HANDLED_RECOMPUTE_PROPS;
3316 }
3317
3318 return handled;
3319 }
3320
3321
3322 \f
3323 /***********************************************************************
3324 Faces
3325 ***********************************************************************/
3326
3327 /* Set up iterator IT from face properties at its current position.
3328 Called from handle_stop. */
3329
3330 static enum prop_handled
3331 handle_face_prop (it)
3332 struct it *it;
3333 {
3334 int new_face_id, next_stop;
3335
3336 if (!STRINGP (it->string))
3337 {
3338 new_face_id
3339 = face_at_buffer_position (it->w,
3340 IT_CHARPOS (*it),
3341 it->region_beg_charpos,
3342 it->region_end_charpos,
3343 &next_stop,
3344 (IT_CHARPOS (*it)
3345 + TEXT_PROP_DISTANCE_LIMIT),
3346 0);
3347
3348 /* Is this a start of a run of characters with box face?
3349 Caveat: this can be called for a freshly initialized
3350 iterator; face_id is -1 in this case. We know that the new
3351 face will not change until limit, i.e. if the new face has a
3352 box, all characters up to limit will have one. But, as
3353 usual, we don't know whether limit is really the end. */
3354 if (new_face_id != it->face_id)
3355 {
3356 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3357
3358 /* If new face has a box but old face has not, this is
3359 the start of a run of characters with box, i.e. it has
3360 a shadow on the left side. The value of face_id of the
3361 iterator will be -1 if this is the initial call that gets
3362 the face. In this case, we have to look in front of IT's
3363 position and see whether there is a face != new_face_id. */
3364 it->start_of_box_run_p
3365 = (new_face->box != FACE_NO_BOX
3366 && (it->face_id >= 0
3367 || IT_CHARPOS (*it) == BEG
3368 || new_face_id != face_before_it_pos (it)));
3369 it->face_box_p = new_face->box != FACE_NO_BOX;
3370 }
3371 }
3372 else
3373 {
3374 int base_face_id, bufpos;
3375
3376 if (it->current.overlay_string_index >= 0)
3377 bufpos = IT_CHARPOS (*it);
3378 else
3379 bufpos = 0;
3380
3381 /* For strings from a buffer, i.e. overlay strings or strings
3382 from a `display' property, use the face at IT's current
3383 buffer position as the base face to merge with, so that
3384 overlay strings appear in the same face as surrounding
3385 text, unless they specify their own faces. */
3386 base_face_id = underlying_face_id (it);
3387
3388 new_face_id = face_at_string_position (it->w,
3389 it->string,
3390 IT_STRING_CHARPOS (*it),
3391 bufpos,
3392 it->region_beg_charpos,
3393 it->region_end_charpos,
3394 &next_stop,
3395 base_face_id, 0);
3396
3397 #if 0 /* This shouldn't be neccessary. Let's check it. */
3398 /* If IT is used to display a mode line we would really like to
3399 use the mode line face instead of the frame's default face. */
3400 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3401 && new_face_id == DEFAULT_FACE_ID)
3402 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3403 #endif
3404
3405 /* Is this a start of a run of characters with box? Caveat:
3406 this can be called for a freshly allocated iterator; face_id
3407 is -1 is this case. We know that the new face will not
3408 change until the next check pos, i.e. if the new face has a
3409 box, all characters up to that position will have a
3410 box. But, as usual, we don't know whether that position
3411 is really the end. */
3412 if (new_face_id != it->face_id)
3413 {
3414 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3415 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3416
3417 /* If new face has a box but old face hasn't, this is the
3418 start of a run of characters with box, i.e. it has a
3419 shadow on the left side. */
3420 it->start_of_box_run_p
3421 = new_face->box && (old_face == NULL || !old_face->box);
3422 it->face_box_p = new_face->box != FACE_NO_BOX;
3423 }
3424 }
3425
3426 it->face_id = new_face_id;
3427 return HANDLED_NORMALLY;
3428 }
3429
3430
3431 /* Return the ID of the face ``underlying'' IT's current position,
3432 which is in a string. If the iterator is associated with a
3433 buffer, return the face at IT's current buffer position.
3434 Otherwise, use the iterator's base_face_id. */
3435
3436 static int
3437 underlying_face_id (it)
3438 struct it *it;
3439 {
3440 int face_id = it->base_face_id, i;
3441
3442 xassert (STRINGP (it->string));
3443
3444 for (i = it->sp - 1; i >= 0; --i)
3445 if (NILP (it->stack[i].string))
3446 face_id = it->stack[i].face_id;
3447
3448 return face_id;
3449 }
3450
3451
3452 /* Compute the face one character before or after the current position
3453 of IT. BEFORE_P non-zero means get the face in front of IT's
3454 position. Value is the id of the face. */
3455
3456 static int
3457 face_before_or_after_it_pos (it, before_p)
3458 struct it *it;
3459 int before_p;
3460 {
3461 int face_id, limit;
3462 int next_check_charpos;
3463 struct text_pos pos;
3464
3465 xassert (it->s == NULL);
3466
3467 if (STRINGP (it->string))
3468 {
3469 int bufpos, base_face_id;
3470
3471 /* No face change past the end of the string (for the case
3472 we are padding with spaces). No face change before the
3473 string start. */
3474 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3475 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3476 return it->face_id;
3477
3478 /* Set pos to the position before or after IT's current position. */
3479 if (before_p)
3480 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3481 else
3482 /* For composition, we must check the character after the
3483 composition. */
3484 pos = (it->what == IT_COMPOSITION
3485 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3486 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3487
3488 if (it->current.overlay_string_index >= 0)
3489 bufpos = IT_CHARPOS (*it);
3490 else
3491 bufpos = 0;
3492
3493 base_face_id = underlying_face_id (it);
3494
3495 /* Get the face for ASCII, or unibyte. */
3496 face_id = face_at_string_position (it->w,
3497 it->string,
3498 CHARPOS (pos),
3499 bufpos,
3500 it->region_beg_charpos,
3501 it->region_end_charpos,
3502 &next_check_charpos,
3503 base_face_id, 0);
3504
3505 /* Correct the face for charsets different from ASCII. Do it
3506 for the multibyte case only. The face returned above is
3507 suitable for unibyte text if IT->string is unibyte. */
3508 if (STRING_MULTIBYTE (it->string))
3509 {
3510 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3511 int rest = SBYTES (it->string) - BYTEPOS (pos);
3512 int c, len;
3513 struct face *face = FACE_FROM_ID (it->f, face_id);
3514
3515 c = string_char_and_length (p, rest, &len);
3516 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3517 }
3518 }
3519 else
3520 {
3521 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3522 || (IT_CHARPOS (*it) <= BEGV && before_p))
3523 return it->face_id;
3524
3525 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3526 pos = it->current.pos;
3527
3528 if (before_p)
3529 DEC_TEXT_POS (pos, it->multibyte_p);
3530 else
3531 {
3532 if (it->what == IT_COMPOSITION)
3533 /* For composition, we must check the position after the
3534 composition. */
3535 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3536 else
3537 INC_TEXT_POS (pos, it->multibyte_p);
3538 }
3539
3540 /* Determine face for CHARSET_ASCII, or unibyte. */
3541 face_id = face_at_buffer_position (it->w,
3542 CHARPOS (pos),
3543 it->region_beg_charpos,
3544 it->region_end_charpos,
3545 &next_check_charpos,
3546 limit, 0);
3547
3548 /* Correct the face for charsets different from ASCII. Do it
3549 for the multibyte case only. The face returned above is
3550 suitable for unibyte text if current_buffer is unibyte. */
3551 if (it->multibyte_p)
3552 {
3553 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3554 struct face *face = FACE_FROM_ID (it->f, face_id);
3555 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3556 }
3557 }
3558
3559 return face_id;
3560 }
3561
3562
3563 \f
3564 /***********************************************************************
3565 Invisible text
3566 ***********************************************************************/
3567
3568 /* Set up iterator IT from invisible properties at its current
3569 position. Called from handle_stop. */
3570
3571 static enum prop_handled
3572 handle_invisible_prop (it)
3573 struct it *it;
3574 {
3575 enum prop_handled handled = HANDLED_NORMALLY;
3576
3577 if (STRINGP (it->string))
3578 {
3579 extern Lisp_Object Qinvisible;
3580 Lisp_Object prop, end_charpos, limit, charpos;
3581
3582 /* Get the value of the invisible text property at the
3583 current position. Value will be nil if there is no such
3584 property. */
3585 charpos = make_number (IT_STRING_CHARPOS (*it));
3586 prop = Fget_text_property (charpos, Qinvisible, it->string);
3587
3588 if (!NILP (prop)
3589 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3590 {
3591 handled = HANDLED_RECOMPUTE_PROPS;
3592
3593 /* Get the position at which the next change of the
3594 invisible text property can be found in IT->string.
3595 Value will be nil if the property value is the same for
3596 all the rest of IT->string. */
3597 XSETINT (limit, SCHARS (it->string));
3598 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3599 it->string, limit);
3600
3601 /* Text at current position is invisible. The next
3602 change in the property is at position end_charpos.
3603 Move IT's current position to that position. */
3604 if (INTEGERP (end_charpos)
3605 && XFASTINT (end_charpos) < XFASTINT (limit))
3606 {
3607 struct text_pos old;
3608 old = it->current.string_pos;
3609 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3610 compute_string_pos (&it->current.string_pos, old, it->string);
3611 }
3612 else
3613 {
3614 /* The rest of the string is invisible. If this is an
3615 overlay string, proceed with the next overlay string
3616 or whatever comes and return a character from there. */
3617 if (it->current.overlay_string_index >= 0)
3618 {
3619 next_overlay_string (it);
3620 /* Don't check for overlay strings when we just
3621 finished processing them. */
3622 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3623 }
3624 else
3625 {
3626 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3627 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3628 }
3629 }
3630 }
3631 }
3632 else
3633 {
3634 int invis_p, newpos, next_stop, start_charpos;
3635 Lisp_Object pos, prop, overlay;
3636
3637 /* First of all, is there invisible text at this position? */
3638 start_charpos = IT_CHARPOS (*it);
3639 pos = make_number (IT_CHARPOS (*it));
3640 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3641 &overlay);
3642 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3643
3644 /* If we are on invisible text, skip over it. */
3645 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3646 {
3647 /* Record whether we have to display an ellipsis for the
3648 invisible text. */
3649 int display_ellipsis_p = invis_p == 2;
3650
3651 handled = HANDLED_RECOMPUTE_PROPS;
3652
3653 /* Loop skipping over invisible text. The loop is left at
3654 ZV or with IT on the first char being visible again. */
3655 do
3656 {
3657 /* Try to skip some invisible text. Return value is the
3658 position reached which can be equal to IT's position
3659 if there is nothing invisible here. This skips both
3660 over invisible text properties and overlays with
3661 invisible property. */
3662 newpos = skip_invisible (IT_CHARPOS (*it),
3663 &next_stop, ZV, it->window);
3664
3665 /* If we skipped nothing at all we weren't at invisible
3666 text in the first place. If everything to the end of
3667 the buffer was skipped, end the loop. */
3668 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3669 invis_p = 0;
3670 else
3671 {
3672 /* We skipped some characters but not necessarily
3673 all there are. Check if we ended up on visible
3674 text. Fget_char_property returns the property of
3675 the char before the given position, i.e. if we
3676 get invis_p = 0, this means that the char at
3677 newpos is visible. */
3678 pos = make_number (newpos);
3679 prop = Fget_char_property (pos, Qinvisible, it->window);
3680 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3681 }
3682
3683 /* If we ended up on invisible text, proceed to
3684 skip starting with next_stop. */
3685 if (invis_p)
3686 IT_CHARPOS (*it) = next_stop;
3687
3688 /* If there are adjacent invisible texts, don't lose the
3689 second one's ellipsis. */
3690 if (invis_p == 2)
3691 display_ellipsis_p = 1;
3692 }
3693 while (invis_p);
3694
3695 /* The position newpos is now either ZV or on visible text. */
3696 IT_CHARPOS (*it) = newpos;
3697 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3698
3699 /* If there are before-strings at the start of invisible
3700 text, and the text is invisible because of a text
3701 property, arrange to show before-strings because 20.x did
3702 it that way. (If the text is invisible because of an
3703 overlay property instead of a text property, this is
3704 already handled in the overlay code.) */
3705 if (NILP (overlay)
3706 && get_overlay_strings (it, start_charpos))
3707 {
3708 handled = HANDLED_RECOMPUTE_PROPS;
3709 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3710 }
3711 else if (display_ellipsis_p)
3712 {
3713 /* Make sure that the glyphs of the ellipsis will get
3714 correct `charpos' values. If we would not update
3715 it->position here, the glyphs would belong to the
3716 last visible character _before_ the invisible
3717 text, which confuses `set_cursor_from_row'.
3718
3719 We use the last invisible position instead of the
3720 first because this way the cursor is always drawn on
3721 the first "." of the ellipsis, whenever PT is inside
3722 the invisible text. Otherwise the cursor would be
3723 placed _after_ the ellipsis when the point is after the
3724 first invisible character. */
3725 if (!STRINGP (it->object))
3726 {
3727 it->position.charpos = IT_CHARPOS (*it) - 1;
3728 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3729 }
3730 setup_for_ellipsis (it, 0);
3731 }
3732 }
3733 }
3734
3735 return handled;
3736 }
3737
3738
3739 /* Make iterator IT return `...' next.
3740 Replaces LEN characters from buffer. */
3741
3742 static void
3743 setup_for_ellipsis (it, len)
3744 struct it *it;
3745 int len;
3746 {
3747 /* Use the display table definition for `...'. Invalid glyphs
3748 will be handled by the method returning elements from dpvec. */
3749 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3750 {
3751 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3752 it->dpvec = v->contents;
3753 it->dpend = v->contents + v->size;
3754 }
3755 else
3756 {
3757 /* Default `...'. */
3758 it->dpvec = default_invis_vector;
3759 it->dpend = default_invis_vector + 3;
3760 }
3761
3762 it->dpvec_char_len = len;
3763 it->current.dpvec_index = 0;
3764 it->dpvec_face_id = -1;
3765
3766 /* Remember the current face id in case glyphs specify faces.
3767 IT's face is restored in set_iterator_to_next.
3768 saved_face_id was set to preceding char's face in handle_stop. */
3769 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3770 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3771
3772 it->method = GET_FROM_DISPLAY_VECTOR;
3773 it->ellipsis_p = 1;
3774 }
3775
3776
3777 \f
3778 /***********************************************************************
3779 'display' property
3780 ***********************************************************************/
3781
3782 /* Set up iterator IT from `display' property at its current position.
3783 Called from handle_stop.
3784 We return HANDLED_RETURN if some part of the display property
3785 overrides the display of the buffer text itself.
3786 Otherwise we return HANDLED_NORMALLY. */
3787
3788 static enum prop_handled
3789 handle_display_prop (it)
3790 struct it *it;
3791 {
3792 Lisp_Object prop, object;
3793 struct text_pos *position;
3794 /* Nonzero if some property replaces the display of the text itself. */
3795 int display_replaced_p = 0;
3796
3797 if (STRINGP (it->string))
3798 {
3799 object = it->string;
3800 position = &it->current.string_pos;
3801 }
3802 else
3803 {
3804 XSETWINDOW (object, it->w);
3805 position = &it->current.pos;
3806 }
3807
3808 /* Reset those iterator values set from display property values. */
3809 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3810 it->space_width = Qnil;
3811 it->font_height = Qnil;
3812 it->voffset = 0;
3813
3814 /* We don't support recursive `display' properties, i.e. string
3815 values that have a string `display' property, that have a string
3816 `display' property etc. */
3817 if (!it->string_from_display_prop_p)
3818 it->area = TEXT_AREA;
3819
3820 prop = Fget_char_property (make_number (position->charpos),
3821 Qdisplay, object);
3822 if (NILP (prop))
3823 return HANDLED_NORMALLY;
3824
3825 if (!STRINGP (it->string))
3826 object = it->w->buffer;
3827
3828 if (CONSP (prop)
3829 /* Simple properties. */
3830 && !EQ (XCAR (prop), Qimage)
3831 && !EQ (XCAR (prop), Qspace)
3832 && !EQ (XCAR (prop), Qwhen)
3833 && !EQ (XCAR (prop), Qslice)
3834 && !EQ (XCAR (prop), Qspace_width)
3835 && !EQ (XCAR (prop), Qheight)
3836 && !EQ (XCAR (prop), Qraise)
3837 /* Marginal area specifications. */
3838 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3839 && !EQ (XCAR (prop), Qleft_fringe)
3840 && !EQ (XCAR (prop), Qright_fringe)
3841 && !NILP (XCAR (prop)))
3842 {
3843 for (; CONSP (prop); prop = XCDR (prop))
3844 {
3845 if (handle_single_display_spec (it, XCAR (prop), object,
3846 position, display_replaced_p))
3847 display_replaced_p = 1;
3848 }
3849 }
3850 else if (VECTORP (prop))
3851 {
3852 int i;
3853 for (i = 0; i < ASIZE (prop); ++i)
3854 if (handle_single_display_spec (it, AREF (prop, i), object,
3855 position, display_replaced_p))
3856 display_replaced_p = 1;
3857 }
3858 else
3859 {
3860 int ret = handle_single_display_spec (it, prop, object, position, 0);
3861 if (ret < 0) /* Replaced by "", i.e. nothing. */
3862 return HANDLED_RECOMPUTE_PROPS;
3863 if (ret)
3864 display_replaced_p = 1;
3865 }
3866
3867 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3868 }
3869
3870
3871 /* Value is the position of the end of the `display' property starting
3872 at START_POS in OBJECT. */
3873
3874 static struct text_pos
3875 display_prop_end (it, object, start_pos)
3876 struct it *it;
3877 Lisp_Object object;
3878 struct text_pos start_pos;
3879 {
3880 Lisp_Object end;
3881 struct text_pos end_pos;
3882
3883 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3884 Qdisplay, object, Qnil);
3885 CHARPOS (end_pos) = XFASTINT (end);
3886 if (STRINGP (object))
3887 compute_string_pos (&end_pos, start_pos, it->string);
3888 else
3889 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3890
3891 return end_pos;
3892 }
3893
3894
3895 /* Set up IT from a single `display' specification PROP. OBJECT
3896 is the object in which the `display' property was found. *POSITION
3897 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3898 means that we previously saw a display specification which already
3899 replaced text display with something else, for example an image;
3900 we ignore such properties after the first one has been processed.
3901
3902 If PROP is a `space' or `image' specification, and in some other
3903 cases too, set *POSITION to the position where the `display'
3904 property ends.
3905
3906 Value is non-zero if something was found which replaces the display
3907 of buffer or string text. Specifically, the value is -1 if that
3908 "something" is "nothing". */
3909
3910 static int
3911 handle_single_display_spec (it, spec, object, position,
3912 display_replaced_before_p)
3913 struct it *it;
3914 Lisp_Object spec;
3915 Lisp_Object object;
3916 struct text_pos *position;
3917 int display_replaced_before_p;
3918 {
3919 Lisp_Object form;
3920 Lisp_Object location, value;
3921 struct text_pos start_pos, save_pos;
3922 int valid_p;
3923
3924 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3925 If the result is non-nil, use VALUE instead of SPEC. */
3926 form = Qt;
3927 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3928 {
3929 spec = XCDR (spec);
3930 if (!CONSP (spec))
3931 return 0;
3932 form = XCAR (spec);
3933 spec = XCDR (spec);
3934 }
3935
3936 if (!NILP (form) && !EQ (form, Qt))
3937 {
3938 int count = SPECPDL_INDEX ();
3939 struct gcpro gcpro1;
3940
3941 /* Bind `object' to the object having the `display' property, a
3942 buffer or string. Bind `position' to the position in the
3943 object where the property was found, and `buffer-position'
3944 to the current position in the buffer. */
3945 specbind (Qobject, object);
3946 specbind (Qposition, make_number (CHARPOS (*position)));
3947 specbind (Qbuffer_position,
3948 make_number (STRINGP (object)
3949 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3950 GCPRO1 (form);
3951 form = safe_eval (form);
3952 UNGCPRO;
3953 unbind_to (count, Qnil);
3954 }
3955
3956 if (NILP (form))
3957 return 0;
3958
3959 /* Handle `(height HEIGHT)' specifications. */
3960 if (CONSP (spec)
3961 && EQ (XCAR (spec), Qheight)
3962 && CONSP (XCDR (spec)))
3963 {
3964 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3965 return 0;
3966
3967 it->font_height = XCAR (XCDR (spec));
3968 if (!NILP (it->font_height))
3969 {
3970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3971 int new_height = -1;
3972
3973 if (CONSP (it->font_height)
3974 && (EQ (XCAR (it->font_height), Qplus)
3975 || EQ (XCAR (it->font_height), Qminus))
3976 && CONSP (XCDR (it->font_height))
3977 && INTEGERP (XCAR (XCDR (it->font_height))))
3978 {
3979 /* `(+ N)' or `(- N)' where N is an integer. */
3980 int steps = XINT (XCAR (XCDR (it->font_height)));
3981 if (EQ (XCAR (it->font_height), Qplus))
3982 steps = - steps;
3983 it->face_id = smaller_face (it->f, it->face_id, steps);
3984 }
3985 else if (FUNCTIONP (it->font_height))
3986 {
3987 /* Call function with current height as argument.
3988 Value is the new height. */
3989 Lisp_Object height;
3990 height = safe_call1 (it->font_height,
3991 face->lface[LFACE_HEIGHT_INDEX]);
3992 if (NUMBERP (height))
3993 new_height = XFLOATINT (height);
3994 }
3995 else if (NUMBERP (it->font_height))
3996 {
3997 /* Value is a multiple of the canonical char height. */
3998 struct face *face;
3999
4000 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4001 new_height = (XFLOATINT (it->font_height)
4002 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4003 }
4004 else
4005 {
4006 /* Evaluate IT->font_height with `height' bound to the
4007 current specified height to get the new height. */
4008 int count = SPECPDL_INDEX ();
4009
4010 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4011 value = safe_eval (it->font_height);
4012 unbind_to (count, Qnil);
4013
4014 if (NUMBERP (value))
4015 new_height = XFLOATINT (value);
4016 }
4017
4018 if (new_height > 0)
4019 it->face_id = face_with_height (it->f, it->face_id, new_height);
4020 }
4021
4022 return 0;
4023 }
4024
4025 /* Handle `(space_width WIDTH)'. */
4026 if (CONSP (spec)
4027 && EQ (XCAR (spec), Qspace_width)
4028 && CONSP (XCDR (spec)))
4029 {
4030 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4031 return 0;
4032
4033 value = XCAR (XCDR (spec));
4034 if (NUMBERP (value) && XFLOATINT (value) > 0)
4035 it->space_width = value;
4036
4037 return 0;
4038 }
4039
4040 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4041 if (CONSP (spec)
4042 && EQ (XCAR (spec), Qslice))
4043 {
4044 Lisp_Object tem;
4045
4046 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4047 return 0;
4048
4049 if (tem = XCDR (spec), CONSP (tem))
4050 {
4051 it->slice.x = XCAR (tem);
4052 if (tem = XCDR (tem), CONSP (tem))
4053 {
4054 it->slice.y = XCAR (tem);
4055 if (tem = XCDR (tem), CONSP (tem))
4056 {
4057 it->slice.width = XCAR (tem);
4058 if (tem = XCDR (tem), CONSP (tem))
4059 it->slice.height = XCAR (tem);
4060 }
4061 }
4062 }
4063
4064 return 0;
4065 }
4066
4067 /* Handle `(raise FACTOR)'. */
4068 if (CONSP (spec)
4069 && EQ (XCAR (spec), Qraise)
4070 && CONSP (XCDR (spec)))
4071 {
4072 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4073 return 0;
4074
4075 #ifdef HAVE_WINDOW_SYSTEM
4076 value = XCAR (XCDR (spec));
4077 if (NUMBERP (value))
4078 {
4079 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4080 it->voffset = - (XFLOATINT (value)
4081 * (FONT_HEIGHT (face->font)));
4082 }
4083 #endif /* HAVE_WINDOW_SYSTEM */
4084
4085 return 0;
4086 }
4087
4088 /* Don't handle the other kinds of display specifications
4089 inside a string that we got from a `display' property. */
4090 if (it->string_from_display_prop_p)
4091 return 0;
4092
4093 /* Characters having this form of property are not displayed, so
4094 we have to find the end of the property. */
4095 start_pos = *position;
4096 *position = display_prop_end (it, object, start_pos);
4097 value = Qnil;
4098
4099 /* Stop the scan at that end position--we assume that all
4100 text properties change there. */
4101 it->stop_charpos = position->charpos;
4102
4103 /* Handle `(left-fringe BITMAP [FACE])'
4104 and `(right-fringe BITMAP [FACE])'. */
4105 if (CONSP (spec)
4106 && (EQ (XCAR (spec), Qleft_fringe)
4107 || EQ (XCAR (spec), Qright_fringe))
4108 && CONSP (XCDR (spec)))
4109 {
4110 int face_id = DEFAULT_FACE_ID;
4111 int fringe_bitmap;
4112
4113 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4114 /* If we return here, POSITION has been advanced
4115 across the text with this property. */
4116 return 0;
4117
4118 #ifdef HAVE_WINDOW_SYSTEM
4119 value = XCAR (XCDR (spec));
4120 if (!SYMBOLP (value)
4121 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4122 /* If we return here, POSITION has been advanced
4123 across the text with this property. */
4124 return 0;
4125
4126 if (CONSP (XCDR (XCDR (spec))))
4127 {
4128 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4129 int face_id2 = lookup_derived_face (it->f, face_name,
4130 FRINGE_FACE_ID, 0);
4131 if (face_id2 >= 0)
4132 face_id = face_id2;
4133 }
4134
4135 /* Save current settings of IT so that we can restore them
4136 when we are finished with the glyph property value. */
4137
4138 save_pos = it->position;
4139 it->position = *position;
4140 push_it (it);
4141 it->position = save_pos;
4142
4143 it->area = TEXT_AREA;
4144 it->what = IT_IMAGE;
4145 it->image_id = -1; /* no image */
4146 it->position = start_pos;
4147 it->object = NILP (object) ? it->w->buffer : object;
4148 it->method = GET_FROM_IMAGE;
4149 it->face_id = face_id;
4150
4151 /* Say that we haven't consumed the characters with
4152 `display' property yet. The call to pop_it in
4153 set_iterator_to_next will clean this up. */
4154 *position = start_pos;
4155
4156 if (EQ (XCAR (spec), Qleft_fringe))
4157 {
4158 it->left_user_fringe_bitmap = fringe_bitmap;
4159 it->left_user_fringe_face_id = face_id;
4160 }
4161 else
4162 {
4163 it->right_user_fringe_bitmap = fringe_bitmap;
4164 it->right_user_fringe_face_id = face_id;
4165 }
4166 #endif /* HAVE_WINDOW_SYSTEM */
4167 return 1;
4168 }
4169
4170 /* Prepare to handle `((margin left-margin) ...)',
4171 `((margin right-margin) ...)' and `((margin nil) ...)'
4172 prefixes for display specifications. */
4173 location = Qunbound;
4174 if (CONSP (spec) && CONSP (XCAR (spec)))
4175 {
4176 Lisp_Object tem;
4177
4178 value = XCDR (spec);
4179 if (CONSP (value))
4180 value = XCAR (value);
4181
4182 tem = XCAR (spec);
4183 if (EQ (XCAR (tem), Qmargin)
4184 && (tem = XCDR (tem),
4185 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4186 (NILP (tem)
4187 || EQ (tem, Qleft_margin)
4188 || EQ (tem, Qright_margin))))
4189 location = tem;
4190 }
4191
4192 if (EQ (location, Qunbound))
4193 {
4194 location = Qnil;
4195 value = spec;
4196 }
4197
4198 /* After this point, VALUE is the property after any
4199 margin prefix has been stripped. It must be a string,
4200 an image specification, or `(space ...)'.
4201
4202 LOCATION specifies where to display: `left-margin',
4203 `right-margin' or nil. */
4204
4205 valid_p = (STRINGP (value)
4206 #ifdef HAVE_WINDOW_SYSTEM
4207 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4208 #endif /* not HAVE_WINDOW_SYSTEM */
4209 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4210
4211 if (valid_p && !display_replaced_before_p)
4212 {
4213 /* Save current settings of IT so that we can restore them
4214 when we are finished with the glyph property value. */
4215 save_pos = it->position;
4216 it->position = *position;
4217 push_it (it);
4218 it->position = save_pos;
4219
4220 if (NILP (location))
4221 it->area = TEXT_AREA;
4222 else if (EQ (location, Qleft_margin))
4223 it->area = LEFT_MARGIN_AREA;
4224 else
4225 it->area = RIGHT_MARGIN_AREA;
4226
4227 if (STRINGP (value))
4228 {
4229 if (SCHARS (value) == 0)
4230 {
4231 pop_it (it);
4232 return -1; /* Replaced by "", i.e. nothing. */
4233 }
4234 it->string = value;
4235 it->multibyte_p = STRING_MULTIBYTE (it->string);
4236 it->current.overlay_string_index = -1;
4237 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4238 it->end_charpos = it->string_nchars = SCHARS (it->string);
4239 it->method = GET_FROM_STRING;
4240 it->stop_charpos = 0;
4241 it->string_from_display_prop_p = 1;
4242 /* Say that we haven't consumed the characters with
4243 `display' property yet. The call to pop_it in
4244 set_iterator_to_next will clean this up. */
4245 *position = start_pos;
4246 }
4247 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4248 {
4249 it->method = GET_FROM_STRETCH;
4250 it->object = value;
4251 *position = it->position = start_pos;
4252 }
4253 #ifdef HAVE_WINDOW_SYSTEM
4254 else
4255 {
4256 it->what = IT_IMAGE;
4257 it->image_id = lookup_image (it->f, value);
4258 it->position = start_pos;
4259 it->object = NILP (object) ? it->w->buffer : object;
4260 it->method = GET_FROM_IMAGE;
4261
4262 /* Say that we haven't consumed the characters with
4263 `display' property yet. The call to pop_it in
4264 set_iterator_to_next will clean this up. */
4265 *position = start_pos;
4266 }
4267 #endif /* HAVE_WINDOW_SYSTEM */
4268
4269 return 1;
4270 }
4271
4272 /* Invalid property or property not supported. Restore
4273 POSITION to what it was before. */
4274 *position = start_pos;
4275 return 0;
4276 }
4277
4278
4279 /* Check if SPEC is a display specification value whose text should be
4280 treated as intangible. */
4281
4282 static int
4283 single_display_spec_intangible_p (prop)
4284 Lisp_Object prop;
4285 {
4286 /* Skip over `when FORM'. */
4287 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4288 {
4289 prop = XCDR (prop);
4290 if (!CONSP (prop))
4291 return 0;
4292 prop = XCDR (prop);
4293 }
4294
4295 if (STRINGP (prop))
4296 return 1;
4297
4298 if (!CONSP (prop))
4299 return 0;
4300
4301 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4302 we don't need to treat text as intangible. */
4303 if (EQ (XCAR (prop), Qmargin))
4304 {
4305 prop = XCDR (prop);
4306 if (!CONSP (prop))
4307 return 0;
4308
4309 prop = XCDR (prop);
4310 if (!CONSP (prop)
4311 || EQ (XCAR (prop), Qleft_margin)
4312 || EQ (XCAR (prop), Qright_margin))
4313 return 0;
4314 }
4315
4316 return (CONSP (prop)
4317 && (EQ (XCAR (prop), Qimage)
4318 || EQ (XCAR (prop), Qspace)));
4319 }
4320
4321
4322 /* Check if PROP is a display property value whose text should be
4323 treated as intangible. */
4324
4325 int
4326 display_prop_intangible_p (prop)
4327 Lisp_Object prop;
4328 {
4329 if (CONSP (prop)
4330 && CONSP (XCAR (prop))
4331 && !EQ (Qmargin, XCAR (XCAR (prop))))
4332 {
4333 /* A list of sub-properties. */
4334 while (CONSP (prop))
4335 {
4336 if (single_display_spec_intangible_p (XCAR (prop)))
4337 return 1;
4338 prop = XCDR (prop);
4339 }
4340 }
4341 else if (VECTORP (prop))
4342 {
4343 /* A vector of sub-properties. */
4344 int i;
4345 for (i = 0; i < ASIZE (prop); ++i)
4346 if (single_display_spec_intangible_p (AREF (prop, i)))
4347 return 1;
4348 }
4349 else
4350 return single_display_spec_intangible_p (prop);
4351
4352 return 0;
4353 }
4354
4355
4356 /* Return 1 if PROP is a display sub-property value containing STRING. */
4357
4358 static int
4359 single_display_spec_string_p (prop, string)
4360 Lisp_Object prop, string;
4361 {
4362 if (EQ (string, prop))
4363 return 1;
4364
4365 /* Skip over `when FORM'. */
4366 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4367 {
4368 prop = XCDR (prop);
4369 if (!CONSP (prop))
4370 return 0;
4371 prop = XCDR (prop);
4372 }
4373
4374 if (CONSP (prop))
4375 /* Skip over `margin LOCATION'. */
4376 if (EQ (XCAR (prop), Qmargin))
4377 {
4378 prop = XCDR (prop);
4379 if (!CONSP (prop))
4380 return 0;
4381
4382 prop = XCDR (prop);
4383 if (!CONSP (prop))
4384 return 0;
4385 }
4386
4387 return CONSP (prop) && EQ (XCAR (prop), string);
4388 }
4389
4390
4391 /* Return 1 if STRING appears in the `display' property PROP. */
4392
4393 static int
4394 display_prop_string_p (prop, string)
4395 Lisp_Object prop, string;
4396 {
4397 if (CONSP (prop)
4398 && CONSP (XCAR (prop))
4399 && !EQ (Qmargin, XCAR (XCAR (prop))))
4400 {
4401 /* A list of sub-properties. */
4402 while (CONSP (prop))
4403 {
4404 if (single_display_spec_string_p (XCAR (prop), string))
4405 return 1;
4406 prop = XCDR (prop);
4407 }
4408 }
4409 else if (VECTORP (prop))
4410 {
4411 /* A vector of sub-properties. */
4412 int i;
4413 for (i = 0; i < ASIZE (prop); ++i)
4414 if (single_display_spec_string_p (AREF (prop, i), string))
4415 return 1;
4416 }
4417 else
4418 return single_display_spec_string_p (prop, string);
4419
4420 return 0;
4421 }
4422
4423
4424 /* Determine from which buffer position in W's buffer STRING comes
4425 from. AROUND_CHARPOS is an approximate position where it could
4426 be from. Value is the buffer position or 0 if it couldn't be
4427 determined.
4428
4429 W's buffer must be current.
4430
4431 This function is necessary because we don't record buffer positions
4432 in glyphs generated from strings (to keep struct glyph small).
4433 This function may only use code that doesn't eval because it is
4434 called asynchronously from note_mouse_highlight. */
4435
4436 int
4437 string_buffer_position (w, string, around_charpos)
4438 struct window *w;
4439 Lisp_Object string;
4440 int around_charpos;
4441 {
4442 Lisp_Object limit, prop, pos;
4443 const int MAX_DISTANCE = 1000;
4444 int found = 0;
4445
4446 pos = make_number (around_charpos);
4447 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4448 while (!found && !EQ (pos, limit))
4449 {
4450 prop = Fget_char_property (pos, Qdisplay, Qnil);
4451 if (!NILP (prop) && display_prop_string_p (prop, string))
4452 found = 1;
4453 else
4454 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4455 }
4456
4457 if (!found)
4458 {
4459 pos = make_number (around_charpos);
4460 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4461 while (!found && !EQ (pos, limit))
4462 {
4463 prop = Fget_char_property (pos, Qdisplay, Qnil);
4464 if (!NILP (prop) && display_prop_string_p (prop, string))
4465 found = 1;
4466 else
4467 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4468 limit);
4469 }
4470 }
4471
4472 return found ? XINT (pos) : 0;
4473 }
4474
4475
4476 \f
4477 /***********************************************************************
4478 `composition' property
4479 ***********************************************************************/
4480
4481 static enum prop_handled
4482 handle_auto_composed_prop (it)
4483 struct it *it;
4484 {
4485 enum prop_handled handled = HANDLED_NORMALLY;
4486
4487 if (FUNCTIONP (Vauto_composition_function))
4488 {
4489 Lisp_Object val;
4490 EMACS_INT pos, this_pos;
4491
4492 if (STRINGP (it->string))
4493 pos = IT_STRING_CHARPOS (*it);
4494 else
4495 pos = IT_CHARPOS (*it);
4496 this_pos = pos;
4497
4498 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
4499 if (! NILP (val))
4500 {
4501 Lisp_Object limit = Qnil, next;
4502
4503 /* As Fnext_single_char_property_change is very slow, we
4504 limit the search to the current line. */
4505 if (STRINGP (it->string))
4506 limit = make_number (SCHARS (it->string));
4507 else
4508 limit = make_number (find_next_newline_no_quit (pos, 1));
4509
4510 next = (Fnext_single_property_change
4511 (make_number (pos), Qauto_composed, it->string, limit));
4512 if (XINT (next) < XINT (limit))
4513 {
4514 /* The current point is auto-composed, but there exist
4515 characters not yet composed beyond the auto-composed
4516 region. There's a possiblity that the last
4517 characters in the region may be newly composed. */
4518 int charpos = XINT (next) - 1, bytepos, c;
4519
4520 if (STRINGP (it->string))
4521 {
4522 bytepos = string_char_to_byte (it->string, charpos);
4523 c = SDATA (it->string)[bytepos];
4524 }
4525 else
4526 {
4527 bytepos = CHAR_TO_BYTE (charpos);
4528 c = FETCH_BYTE (bytepos);
4529 }
4530 if (c != '\n')
4531 /* If the last character is not newline, it may be
4532 composed with the following characters. */
4533 val = Qnil, pos = charpos + 1;
4534 }
4535 }
4536 if (NILP (val))
4537 {
4538 int count = SPECPDL_INDEX ();
4539 Lisp_Object args[4];
4540
4541 args[0] = Vauto_composition_function;
4542 specbind (Qauto_composition_function, Qnil);
4543 args[1] = make_number (pos);
4544 args[2] = it->string;
4545 #ifdef USE_FONT_BACKEND
4546 if (enable_font_backend)
4547 {
4548 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4549 int c;
4550
4551 if (STRINGP (it->string))
4552 {
4553 EMACS_INT pos_byte = IT_STRING_BYTEPOS (*it);
4554 const unsigned char *s = SDATA (it->string) + pos_byte;
4555
4556 if (STRING_MULTIBYTE (it->string))
4557 it->c = STRING_CHAR (s, 0);
4558 else
4559 it->c = *s;
4560 }
4561 else
4562 {
4563 EMACS_INT pos_byte = IT_BYTEPOS (*it);
4564
4565 it->c = FETCH_CHAR (pos_byte);
4566 }
4567 args[3] = font_at (it->c, this_pos, face, it->w, it->string);
4568 }
4569 else
4570 #endif /* USE_FONT_BACKEND */
4571 args[3] = Qnil;
4572 safe_call (4, args);
4573 unbind_to (count, Qnil);
4574
4575 if (this_pos == pos)
4576 {
4577 val = Fget_char_property (args[1], Qauto_composed, it->string);
4578 /* Return HANDLED_RECOMPUTE_PROPS only if function composed
4579 something. This avoids an endless loop if they failed to
4580 fontify the text for which reason ever. */
4581 if (! NILP (val))
4582 handled = HANDLED_RECOMPUTE_PROPS;
4583 }
4584 else
4585 handled = HANDLED_RECOMPUTE_PROPS;
4586 }
4587 }
4588
4589 return handled;
4590 }
4591
4592 /* Set up iterator IT from `composition' property at its current
4593 position. Called from handle_stop. */
4594
4595 static enum prop_handled
4596 handle_composition_prop (it)
4597 struct it *it;
4598 {
4599 Lisp_Object prop, string;
4600 EMACS_INT pos, pos_byte, start, end;
4601 enum prop_handled handled = HANDLED_NORMALLY;
4602
4603 if (STRINGP (it->string))
4604 {
4605 pos = IT_STRING_CHARPOS (*it);
4606 pos_byte = IT_STRING_BYTEPOS (*it);
4607 string = it->string;
4608 }
4609 else
4610 {
4611 pos = IT_CHARPOS (*it);
4612 pos_byte = IT_BYTEPOS (*it);
4613 string = Qnil;
4614 }
4615
4616 /* If there's a valid composition and point is not inside of the
4617 composition (in the case that the composition is from the current
4618 buffer), draw a glyph composed from the composition components. */
4619 if (find_composition (pos, -1, &start, &end, &prop, string)
4620 && COMPOSITION_VALID_P (start, end, prop)
4621 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4622 {
4623 int id;
4624
4625 if (start != pos)
4626 {
4627 if (STRINGP (it->string))
4628 pos_byte = string_char_to_byte (it->string, start);
4629 else
4630 pos_byte = CHAR_TO_BYTE (start);
4631 }
4632 id = get_composition_id (start, pos_byte, end - start, prop, string);
4633
4634 if (id >= 0)
4635 {
4636 struct composition *cmp = composition_table[id];
4637
4638 if (cmp->glyph_len == 0)
4639 {
4640 /* No glyph. */
4641 if (STRINGP (it->string))
4642 {
4643 IT_STRING_CHARPOS (*it) = end;
4644 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4645 end);
4646 }
4647 else
4648 {
4649 IT_CHARPOS (*it) = end;
4650 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4651 }
4652 return HANDLED_RECOMPUTE_PROPS;
4653 }
4654
4655 it->stop_charpos = end;
4656 push_it (it);
4657
4658 it->method = GET_FROM_COMPOSITION;
4659 it->cmp_id = id;
4660 it->cmp_len = COMPOSITION_LENGTH (prop);
4661 /* For a terminal, draw only the first (non-TAB) character
4662 of the components. */
4663 #ifdef USE_FONT_BACKEND
4664 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4665 {
4666 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4667 ->key_and_value,
4668 cmp->hash_index * 2);
4669
4670 it->c = XINT (LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, 0)));
4671 }
4672 else
4673 #endif /* USE_FONT_BACKEND */
4674 {
4675 int i;
4676
4677 for (i = 0; i < cmp->glyph_len; i++)
4678 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4679 != '\t')
4680 break;
4681 }
4682 if (it->c == '\t')
4683 it->c = ' ';
4684 it->len = (STRINGP (it->string)
4685 ? string_char_to_byte (it->string, end)
4686 : CHAR_TO_BYTE (end)) - pos_byte;
4687 handled = HANDLED_RETURN;
4688 }
4689 }
4690
4691 return handled;
4692 }
4693
4694
4695 \f
4696 /***********************************************************************
4697 Overlay strings
4698 ***********************************************************************/
4699
4700 /* The following structure is used to record overlay strings for
4701 later sorting in load_overlay_strings. */
4702
4703 struct overlay_entry
4704 {
4705 Lisp_Object overlay;
4706 Lisp_Object string;
4707 int priority;
4708 int after_string_p;
4709 };
4710
4711
4712 /* Set up iterator IT from overlay strings at its current position.
4713 Called from handle_stop. */
4714
4715 static enum prop_handled
4716 handle_overlay_change (it)
4717 struct it *it;
4718 {
4719 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4720 return HANDLED_RECOMPUTE_PROPS;
4721 else
4722 return HANDLED_NORMALLY;
4723 }
4724
4725
4726 /* Set up the next overlay string for delivery by IT, if there is an
4727 overlay string to deliver. Called by set_iterator_to_next when the
4728 end of the current overlay string is reached. If there are more
4729 overlay strings to display, IT->string and
4730 IT->current.overlay_string_index are set appropriately here.
4731 Otherwise IT->string is set to nil. */
4732
4733 static void
4734 next_overlay_string (it)
4735 struct it *it;
4736 {
4737 ++it->current.overlay_string_index;
4738 if (it->current.overlay_string_index == it->n_overlay_strings)
4739 {
4740 /* No more overlay strings. Restore IT's settings to what
4741 they were before overlay strings were processed, and
4742 continue to deliver from current_buffer. */
4743 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4744
4745 pop_it (it);
4746 xassert (it->sp > 0
4747 || it->method == GET_FROM_COMPOSITION
4748 || (NILP (it->string)
4749 && it->method == GET_FROM_BUFFER
4750 && it->stop_charpos >= BEGV
4751 && it->stop_charpos <= it->end_charpos));
4752 it->current.overlay_string_index = -1;
4753 it->n_overlay_strings = 0;
4754
4755 /* If we're at the end of the buffer, record that we have
4756 processed the overlay strings there already, so that
4757 next_element_from_buffer doesn't try it again. */
4758 if (IT_CHARPOS (*it) >= it->end_charpos)
4759 it->overlay_strings_at_end_processed_p = 1;
4760
4761 /* If we have to display `...' for invisible text, set
4762 the iterator up for that. */
4763 if (display_ellipsis_p)
4764 setup_for_ellipsis (it, 0);
4765 }
4766 else
4767 {
4768 /* There are more overlay strings to process. If
4769 IT->current.overlay_string_index has advanced to a position
4770 where we must load IT->overlay_strings with more strings, do
4771 it. */
4772 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4773
4774 if (it->current.overlay_string_index && i == 0)
4775 load_overlay_strings (it, 0);
4776
4777 /* Initialize IT to deliver display elements from the overlay
4778 string. */
4779 it->string = it->overlay_strings[i];
4780 it->multibyte_p = STRING_MULTIBYTE (it->string);
4781 SET_TEXT_POS (it->current.string_pos, 0, 0);
4782 it->method = GET_FROM_STRING;
4783 it->stop_charpos = 0;
4784 }
4785
4786 CHECK_IT (it);
4787 }
4788
4789
4790 /* Compare two overlay_entry structures E1 and E2. Used as a
4791 comparison function for qsort in load_overlay_strings. Overlay
4792 strings for the same position are sorted so that
4793
4794 1. All after-strings come in front of before-strings, except
4795 when they come from the same overlay.
4796
4797 2. Within after-strings, strings are sorted so that overlay strings
4798 from overlays with higher priorities come first.
4799
4800 2. Within before-strings, strings are sorted so that overlay
4801 strings from overlays with higher priorities come last.
4802
4803 Value is analogous to strcmp. */
4804
4805
4806 static int
4807 compare_overlay_entries (e1, e2)
4808 void *e1, *e2;
4809 {
4810 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4811 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4812 int result;
4813
4814 if (entry1->after_string_p != entry2->after_string_p)
4815 {
4816 /* Let after-strings appear in front of before-strings if
4817 they come from different overlays. */
4818 if (EQ (entry1->overlay, entry2->overlay))
4819 result = entry1->after_string_p ? 1 : -1;
4820 else
4821 result = entry1->after_string_p ? -1 : 1;
4822 }
4823 else if (entry1->after_string_p)
4824 /* After-strings sorted in order of decreasing priority. */
4825 result = entry2->priority - entry1->priority;
4826 else
4827 /* Before-strings sorted in order of increasing priority. */
4828 result = entry1->priority - entry2->priority;
4829
4830 return result;
4831 }
4832
4833
4834 /* Load the vector IT->overlay_strings with overlay strings from IT's
4835 current buffer position, or from CHARPOS if that is > 0. Set
4836 IT->n_overlays to the total number of overlay strings found.
4837
4838 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4839 a time. On entry into load_overlay_strings,
4840 IT->current.overlay_string_index gives the number of overlay
4841 strings that have already been loaded by previous calls to this
4842 function.
4843
4844 IT->add_overlay_start contains an additional overlay start
4845 position to consider for taking overlay strings from, if non-zero.
4846 This position comes into play when the overlay has an `invisible'
4847 property, and both before and after-strings. When we've skipped to
4848 the end of the overlay, because of its `invisible' property, we
4849 nevertheless want its before-string to appear.
4850 IT->add_overlay_start will contain the overlay start position
4851 in this case.
4852
4853 Overlay strings are sorted so that after-string strings come in
4854 front of before-string strings. Within before and after-strings,
4855 strings are sorted by overlay priority. See also function
4856 compare_overlay_entries. */
4857
4858 static void
4859 load_overlay_strings (it, charpos)
4860 struct it *it;
4861 int charpos;
4862 {
4863 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4864 Lisp_Object overlay, window, str, invisible;
4865 struct Lisp_Overlay *ov;
4866 int start, end;
4867 int size = 20;
4868 int n = 0, i, j, invis_p;
4869 struct overlay_entry *entries
4870 = (struct overlay_entry *) alloca (size * sizeof *entries);
4871
4872 if (charpos <= 0)
4873 charpos = IT_CHARPOS (*it);
4874
4875 /* Append the overlay string STRING of overlay OVERLAY to vector
4876 `entries' which has size `size' and currently contains `n'
4877 elements. AFTER_P non-zero means STRING is an after-string of
4878 OVERLAY. */
4879 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4880 do \
4881 { \
4882 Lisp_Object priority; \
4883 \
4884 if (n == size) \
4885 { \
4886 int new_size = 2 * size; \
4887 struct overlay_entry *old = entries; \
4888 entries = \
4889 (struct overlay_entry *) alloca (new_size \
4890 * sizeof *entries); \
4891 bcopy (old, entries, size * sizeof *entries); \
4892 size = new_size; \
4893 } \
4894 \
4895 entries[n].string = (STRING); \
4896 entries[n].overlay = (OVERLAY); \
4897 priority = Foverlay_get ((OVERLAY), Qpriority); \
4898 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4899 entries[n].after_string_p = (AFTER_P); \
4900 ++n; \
4901 } \
4902 while (0)
4903
4904 /* Process overlay before the overlay center. */
4905 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4906 {
4907 XSETMISC (overlay, ov);
4908 xassert (OVERLAYP (overlay));
4909 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4910 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4911
4912 if (end < charpos)
4913 break;
4914
4915 /* Skip this overlay if it doesn't start or end at IT's current
4916 position. */
4917 if (end != charpos && start != charpos)
4918 continue;
4919
4920 /* Skip this overlay if it doesn't apply to IT->w. */
4921 window = Foverlay_get (overlay, Qwindow);
4922 if (WINDOWP (window) && XWINDOW (window) != it->w)
4923 continue;
4924
4925 /* If the text ``under'' the overlay is invisible, both before-
4926 and after-strings from this overlay are visible; start and
4927 end position are indistinguishable. */
4928 invisible = Foverlay_get (overlay, Qinvisible);
4929 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4930
4931 /* If overlay has a non-empty before-string, record it. */
4932 if ((start == charpos || (end == charpos && invis_p))
4933 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4934 && SCHARS (str))
4935 RECORD_OVERLAY_STRING (overlay, str, 0);
4936
4937 /* If overlay has a non-empty after-string, record it. */
4938 if ((end == charpos || (start == charpos && invis_p))
4939 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4940 && SCHARS (str))
4941 RECORD_OVERLAY_STRING (overlay, str, 1);
4942 }
4943
4944 /* Process overlays after the overlay center. */
4945 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4946 {
4947 XSETMISC (overlay, ov);
4948 xassert (OVERLAYP (overlay));
4949 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4950 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4951
4952 if (start > charpos)
4953 break;
4954
4955 /* Skip this overlay if it doesn't start or end at IT's current
4956 position. */
4957 if (end != charpos && start != charpos)
4958 continue;
4959
4960 /* Skip this overlay if it doesn't apply to IT->w. */
4961 window = Foverlay_get (overlay, Qwindow);
4962 if (WINDOWP (window) && XWINDOW (window) != it->w)
4963 continue;
4964
4965 /* If the text ``under'' the overlay is invisible, it has a zero
4966 dimension, and both before- and after-strings apply. */
4967 invisible = Foverlay_get (overlay, Qinvisible);
4968 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4969
4970 /* If overlay has a non-empty before-string, record it. */
4971 if ((start == charpos || (end == charpos && invis_p))
4972 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4973 && SCHARS (str))
4974 RECORD_OVERLAY_STRING (overlay, str, 0);
4975
4976 /* If overlay has a non-empty after-string, record it. */
4977 if ((end == charpos || (start == charpos && invis_p))
4978 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4979 && SCHARS (str))
4980 RECORD_OVERLAY_STRING (overlay, str, 1);
4981 }
4982
4983 #undef RECORD_OVERLAY_STRING
4984
4985 /* Sort entries. */
4986 if (n > 1)
4987 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4988
4989 /* Record the total number of strings to process. */
4990 it->n_overlay_strings = n;
4991
4992 /* IT->current.overlay_string_index is the number of overlay strings
4993 that have already been consumed by IT. Copy some of the
4994 remaining overlay strings to IT->overlay_strings. */
4995 i = 0;
4996 j = it->current.overlay_string_index;
4997 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4998 it->overlay_strings[i++] = entries[j++].string;
4999
5000 CHECK_IT (it);
5001 }
5002
5003
5004 /* Get the first chunk of overlay strings at IT's current buffer
5005 position, or at CHARPOS if that is > 0. Value is non-zero if at
5006 least one overlay string was found. */
5007
5008 static int
5009 get_overlay_strings_1 (it, charpos, compute_stop_p)
5010 struct it *it;
5011 int charpos;
5012 {
5013 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5014 process. This fills IT->overlay_strings with strings, and sets
5015 IT->n_overlay_strings to the total number of strings to process.
5016 IT->pos.overlay_string_index has to be set temporarily to zero
5017 because load_overlay_strings needs this; it must be set to -1
5018 when no overlay strings are found because a zero value would
5019 indicate a position in the first overlay string. */
5020 it->current.overlay_string_index = 0;
5021 load_overlay_strings (it, charpos);
5022
5023 /* If we found overlay strings, set up IT to deliver display
5024 elements from the first one. Otherwise set up IT to deliver
5025 from current_buffer. */
5026 if (it->n_overlay_strings)
5027 {
5028 /* Make sure we know settings in current_buffer, so that we can
5029 restore meaningful values when we're done with the overlay
5030 strings. */
5031 if (compute_stop_p)
5032 compute_stop_pos (it);
5033 xassert (it->face_id >= 0);
5034
5035 /* Save IT's settings. They are restored after all overlay
5036 strings have been processed. */
5037 xassert (!compute_stop_p || it->sp == 0);
5038 push_it (it);
5039
5040 /* Set up IT to deliver display elements from the first overlay
5041 string. */
5042 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5043 it->string = it->overlay_strings[0];
5044 it->stop_charpos = 0;
5045 xassert (STRINGP (it->string));
5046 it->end_charpos = SCHARS (it->string);
5047 it->multibyte_p = STRING_MULTIBYTE (it->string);
5048 it->method = GET_FROM_STRING;
5049 return 1;
5050 }
5051
5052 it->current.overlay_string_index = -1;
5053 return 0;
5054 }
5055
5056 static int
5057 get_overlay_strings (it, charpos)
5058 struct it *it;
5059 int charpos;
5060 {
5061 it->string = Qnil;
5062 it->method = GET_FROM_BUFFER;
5063
5064 (void) get_overlay_strings_1 (it, charpos, 1);
5065
5066 CHECK_IT (it);
5067
5068 /* Value is non-zero if we found at least one overlay string. */
5069 return STRINGP (it->string);
5070 }
5071
5072
5073 \f
5074 /***********************************************************************
5075 Saving and restoring state
5076 ***********************************************************************/
5077
5078 /* Save current settings of IT on IT->stack. Called, for example,
5079 before setting up IT for an overlay string, to be able to restore
5080 IT's settings to what they were after the overlay string has been
5081 processed. */
5082
5083 static void
5084 push_it (it)
5085 struct it *it;
5086 {
5087 struct iterator_stack_entry *p;
5088
5089 xassert (it->sp < IT_STACK_SIZE);
5090 p = it->stack + it->sp;
5091
5092 p->stop_charpos = it->stop_charpos;
5093 xassert (it->face_id >= 0);
5094 p->face_id = it->face_id;
5095 p->string = it->string;
5096 p->method = it->method;
5097 switch (p->method)
5098 {
5099 case GET_FROM_IMAGE:
5100 p->u.image.object = it->object;
5101 p->u.image.image_id = it->image_id;
5102 p->u.image.slice = it->slice;
5103 break;
5104 case GET_FROM_COMPOSITION:
5105 p->u.comp.object = it->object;
5106 p->u.comp.c = it->c;
5107 p->u.comp.len = it->len;
5108 p->u.comp.cmp_id = it->cmp_id;
5109 p->u.comp.cmp_len = it->cmp_len;
5110 break;
5111 case GET_FROM_STRETCH:
5112 p->u.stretch.object = it->object;
5113 break;
5114 }
5115 p->position = it->position;
5116 p->current = it->current;
5117 p->end_charpos = it->end_charpos;
5118 p->string_nchars = it->string_nchars;
5119 p->area = it->area;
5120 p->multibyte_p = it->multibyte_p;
5121 p->space_width = it->space_width;
5122 p->font_height = it->font_height;
5123 p->voffset = it->voffset;
5124 p->string_from_display_prop_p = it->string_from_display_prop_p;
5125 p->display_ellipsis_p = 0;
5126 ++it->sp;
5127 }
5128
5129
5130 /* Restore IT's settings from IT->stack. Called, for example, when no
5131 more overlay strings must be processed, and we return to delivering
5132 display elements from a buffer, or when the end of a string from a
5133 `display' property is reached and we return to delivering display
5134 elements from an overlay string, or from a buffer. */
5135
5136 static void
5137 pop_it (it)
5138 struct it *it;
5139 {
5140 struct iterator_stack_entry *p;
5141
5142 xassert (it->sp > 0);
5143 --it->sp;
5144 p = it->stack + it->sp;
5145 it->stop_charpos = p->stop_charpos;
5146 it->face_id = p->face_id;
5147 it->current = p->current;
5148 it->position = p->position;
5149 it->string = p->string;
5150 if (NILP (it->string))
5151 SET_TEXT_POS (it->current.string_pos, -1, -1);
5152 it->method = p->method;
5153 switch (it->method)
5154 {
5155 case GET_FROM_IMAGE:
5156 it->image_id = p->u.image.image_id;
5157 it->object = p->u.image.object;
5158 it->slice = p->u.image.slice;
5159 break;
5160 case GET_FROM_COMPOSITION:
5161 it->object = p->u.comp.object;
5162 it->c = p->u.comp.c;
5163 it->len = p->u.comp.len;
5164 it->cmp_id = p->u.comp.cmp_id;
5165 it->cmp_len = p->u.comp.cmp_len;
5166 break;
5167 case GET_FROM_STRETCH:
5168 it->object = p->u.comp.object;
5169 break;
5170 case GET_FROM_BUFFER:
5171 it->object = it->w->buffer;
5172 break;
5173 case GET_FROM_STRING:
5174 it->object = it->string;
5175 break;
5176 }
5177 it->end_charpos = p->end_charpos;
5178 it->string_nchars = p->string_nchars;
5179 it->area = p->area;
5180 it->multibyte_p = p->multibyte_p;
5181 it->space_width = p->space_width;
5182 it->font_height = p->font_height;
5183 it->voffset = p->voffset;
5184 it->string_from_display_prop_p = p->string_from_display_prop_p;
5185 }
5186
5187
5188 \f
5189 /***********************************************************************
5190 Moving over lines
5191 ***********************************************************************/
5192
5193 /* Set IT's current position to the previous line start. */
5194
5195 static void
5196 back_to_previous_line_start (it)
5197 struct it *it;
5198 {
5199 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5200 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5201 }
5202
5203
5204 /* Move IT to the next line start.
5205
5206 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5207 we skipped over part of the text (as opposed to moving the iterator
5208 continuously over the text). Otherwise, don't change the value
5209 of *SKIPPED_P.
5210
5211 Newlines may come from buffer text, overlay strings, or strings
5212 displayed via the `display' property. That's the reason we can't
5213 simply use find_next_newline_no_quit.
5214
5215 Note that this function may not skip over invisible text that is so
5216 because of text properties and immediately follows a newline. If
5217 it would, function reseat_at_next_visible_line_start, when called
5218 from set_iterator_to_next, would effectively make invisible
5219 characters following a newline part of the wrong glyph row, which
5220 leads to wrong cursor motion. */
5221
5222 static int
5223 forward_to_next_line_start (it, skipped_p)
5224 struct it *it;
5225 int *skipped_p;
5226 {
5227 int old_selective, newline_found_p, n;
5228 const int MAX_NEWLINE_DISTANCE = 500;
5229
5230 /* If already on a newline, just consume it to avoid unintended
5231 skipping over invisible text below. */
5232 if (it->what == IT_CHARACTER
5233 && it->c == '\n'
5234 && CHARPOS (it->position) == IT_CHARPOS (*it))
5235 {
5236 set_iterator_to_next (it, 0);
5237 it->c = 0;
5238 return 1;
5239 }
5240
5241 /* Don't handle selective display in the following. It's (a)
5242 unnecessary because it's done by the caller, and (b) leads to an
5243 infinite recursion because next_element_from_ellipsis indirectly
5244 calls this function. */
5245 old_selective = it->selective;
5246 it->selective = 0;
5247
5248 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5249 from buffer text. */
5250 for (n = newline_found_p = 0;
5251 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5252 n += STRINGP (it->string) ? 0 : 1)
5253 {
5254 if (!get_next_display_element (it))
5255 return 0;
5256 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5257 set_iterator_to_next (it, 0);
5258 }
5259
5260 /* If we didn't find a newline near enough, see if we can use a
5261 short-cut. */
5262 if (!newline_found_p)
5263 {
5264 int start = IT_CHARPOS (*it);
5265 int limit = find_next_newline_no_quit (start, 1);
5266 Lisp_Object pos;
5267
5268 xassert (!STRINGP (it->string));
5269
5270 /* If there isn't any `display' property in sight, and no
5271 overlays, we can just use the position of the newline in
5272 buffer text. */
5273 if (it->stop_charpos >= limit
5274 || ((pos = Fnext_single_property_change (make_number (start),
5275 Qdisplay,
5276 Qnil, make_number (limit)),
5277 NILP (pos))
5278 && next_overlay_change (start) == ZV))
5279 {
5280 IT_CHARPOS (*it) = limit;
5281 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5282 *skipped_p = newline_found_p = 1;
5283 }
5284 else
5285 {
5286 while (get_next_display_element (it)
5287 && !newline_found_p)
5288 {
5289 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5290 set_iterator_to_next (it, 0);
5291 }
5292 }
5293 }
5294
5295 it->selective = old_selective;
5296 return newline_found_p;
5297 }
5298
5299
5300 /* Set IT's current position to the previous visible line start. Skip
5301 invisible text that is so either due to text properties or due to
5302 selective display. Caution: this does not change IT->current_x and
5303 IT->hpos. */
5304
5305 static void
5306 back_to_previous_visible_line_start (it)
5307 struct it *it;
5308 {
5309 while (IT_CHARPOS (*it) > BEGV)
5310 {
5311 back_to_previous_line_start (it);
5312
5313 if (IT_CHARPOS (*it) <= BEGV)
5314 break;
5315
5316 /* If selective > 0, then lines indented more than that values
5317 are invisible. */
5318 if (it->selective > 0
5319 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5320 (double) it->selective)) /* iftc */
5321 continue;
5322
5323 /* Check the newline before point for invisibility. */
5324 {
5325 Lisp_Object prop;
5326 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5327 Qinvisible, it->window);
5328 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5329 continue;
5330 }
5331
5332 if (IT_CHARPOS (*it) <= BEGV)
5333 break;
5334
5335 {
5336 struct it it2;
5337 int pos;
5338 int beg, end;
5339 Lisp_Object val, overlay;
5340
5341 /* If newline is part of a composition, continue from start of composition */
5342 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5343 && beg < IT_CHARPOS (*it))
5344 goto replaced;
5345
5346 /* If newline is replaced by a display property, find start of overlay
5347 or interval and continue search from that point. */
5348 it2 = *it;
5349 pos = --IT_CHARPOS (it2);
5350 --IT_BYTEPOS (it2);
5351 it2.sp = 0;
5352 if (handle_display_prop (&it2) == HANDLED_RETURN
5353 && !NILP (val = get_char_property_and_overlay
5354 (make_number (pos), Qdisplay, Qnil, &overlay))
5355 && (OVERLAYP (overlay)
5356 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5357 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5358 goto replaced;
5359
5360 /* Newline is not replaced by anything -- so we are done. */
5361 break;
5362
5363 replaced:
5364 if (beg < BEGV)
5365 beg = BEGV;
5366 IT_CHARPOS (*it) = beg;
5367 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5368 }
5369 }
5370
5371 it->continuation_lines_width = 0;
5372
5373 xassert (IT_CHARPOS (*it) >= BEGV);
5374 xassert (IT_CHARPOS (*it) == BEGV
5375 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5376 CHECK_IT (it);
5377 }
5378
5379
5380 /* Reseat iterator IT at the previous visible line start. Skip
5381 invisible text that is so either due to text properties or due to
5382 selective display. At the end, update IT's overlay information,
5383 face information etc. */
5384
5385 void
5386 reseat_at_previous_visible_line_start (it)
5387 struct it *it;
5388 {
5389 back_to_previous_visible_line_start (it);
5390 reseat (it, it->current.pos, 1);
5391 CHECK_IT (it);
5392 }
5393
5394
5395 /* Reseat iterator IT on the next visible line start in the current
5396 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5397 preceding the line start. Skip over invisible text that is so
5398 because of selective display. Compute faces, overlays etc at the
5399 new position. Note that this function does not skip over text that
5400 is invisible because of text properties. */
5401
5402 static void
5403 reseat_at_next_visible_line_start (it, on_newline_p)
5404 struct it *it;
5405 int on_newline_p;
5406 {
5407 int newline_found_p, skipped_p = 0;
5408
5409 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5410
5411 /* Skip over lines that are invisible because they are indented
5412 more than the value of IT->selective. */
5413 if (it->selective > 0)
5414 while (IT_CHARPOS (*it) < ZV
5415 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5416 (double) it->selective)) /* iftc */
5417 {
5418 xassert (IT_BYTEPOS (*it) == BEGV
5419 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5420 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5421 }
5422
5423 /* Position on the newline if that's what's requested. */
5424 if (on_newline_p && newline_found_p)
5425 {
5426 if (STRINGP (it->string))
5427 {
5428 if (IT_STRING_CHARPOS (*it) > 0)
5429 {
5430 --IT_STRING_CHARPOS (*it);
5431 --IT_STRING_BYTEPOS (*it);
5432 }
5433 }
5434 else if (IT_CHARPOS (*it) > BEGV)
5435 {
5436 --IT_CHARPOS (*it);
5437 --IT_BYTEPOS (*it);
5438 reseat (it, it->current.pos, 0);
5439 }
5440 }
5441 else if (skipped_p)
5442 reseat (it, it->current.pos, 0);
5443
5444 CHECK_IT (it);
5445 }
5446
5447
5448 \f
5449 /***********************************************************************
5450 Changing an iterator's position
5451 ***********************************************************************/
5452
5453 /* Change IT's current position to POS in current_buffer. If FORCE_P
5454 is non-zero, always check for text properties at the new position.
5455 Otherwise, text properties are only looked up if POS >=
5456 IT->check_charpos of a property. */
5457
5458 static void
5459 reseat (it, pos, force_p)
5460 struct it *it;
5461 struct text_pos pos;
5462 int force_p;
5463 {
5464 int original_pos = IT_CHARPOS (*it);
5465
5466 reseat_1 (it, pos, 0);
5467
5468 /* Determine where to check text properties. Avoid doing it
5469 where possible because text property lookup is very expensive. */
5470 if (force_p
5471 || CHARPOS (pos) > it->stop_charpos
5472 || CHARPOS (pos) < original_pos)
5473 handle_stop (it);
5474
5475 CHECK_IT (it);
5476 }
5477
5478
5479 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5480 IT->stop_pos to POS, also. */
5481
5482 static void
5483 reseat_1 (it, pos, set_stop_p)
5484 struct it *it;
5485 struct text_pos pos;
5486 int set_stop_p;
5487 {
5488 /* Don't call this function when scanning a C string. */
5489 xassert (it->s == NULL);
5490
5491 /* POS must be a reasonable value. */
5492 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5493
5494 it->current.pos = it->position = pos;
5495 it->end_charpos = ZV;
5496 it->dpvec = NULL;
5497 it->current.dpvec_index = -1;
5498 it->current.overlay_string_index = -1;
5499 IT_STRING_CHARPOS (*it) = -1;
5500 IT_STRING_BYTEPOS (*it) = -1;
5501 it->string = Qnil;
5502 it->method = GET_FROM_BUFFER;
5503 it->object = it->w->buffer;
5504 it->area = TEXT_AREA;
5505 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5506 it->sp = 0;
5507 it->string_from_display_prop_p = 0;
5508 it->face_before_selective_p = 0;
5509
5510 if (set_stop_p)
5511 it->stop_charpos = CHARPOS (pos);
5512 }
5513
5514
5515 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5516 If S is non-null, it is a C string to iterate over. Otherwise,
5517 STRING gives a Lisp string to iterate over.
5518
5519 If PRECISION > 0, don't return more then PRECISION number of
5520 characters from the string.
5521
5522 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5523 characters have been returned. FIELD_WIDTH < 0 means an infinite
5524 field width.
5525
5526 MULTIBYTE = 0 means disable processing of multibyte characters,
5527 MULTIBYTE > 0 means enable it,
5528 MULTIBYTE < 0 means use IT->multibyte_p.
5529
5530 IT must be initialized via a prior call to init_iterator before
5531 calling this function. */
5532
5533 static void
5534 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5535 struct it *it;
5536 unsigned char *s;
5537 Lisp_Object string;
5538 int charpos;
5539 int precision, field_width, multibyte;
5540 {
5541 /* No region in strings. */
5542 it->region_beg_charpos = it->region_end_charpos = -1;
5543
5544 /* No text property checks performed by default, but see below. */
5545 it->stop_charpos = -1;
5546
5547 /* Set iterator position and end position. */
5548 bzero (&it->current, sizeof it->current);
5549 it->current.overlay_string_index = -1;
5550 it->current.dpvec_index = -1;
5551 xassert (charpos >= 0);
5552
5553 /* If STRING is specified, use its multibyteness, otherwise use the
5554 setting of MULTIBYTE, if specified. */
5555 if (multibyte >= 0)
5556 it->multibyte_p = multibyte > 0;
5557
5558 if (s == NULL)
5559 {
5560 xassert (STRINGP (string));
5561 it->string = string;
5562 it->s = NULL;
5563 it->end_charpos = it->string_nchars = SCHARS (string);
5564 it->method = GET_FROM_STRING;
5565 it->current.string_pos = string_pos (charpos, string);
5566 }
5567 else
5568 {
5569 it->s = s;
5570 it->string = Qnil;
5571
5572 /* Note that we use IT->current.pos, not it->current.string_pos,
5573 for displaying C strings. */
5574 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5575 if (it->multibyte_p)
5576 {
5577 it->current.pos = c_string_pos (charpos, s, 1);
5578 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5579 }
5580 else
5581 {
5582 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5583 it->end_charpos = it->string_nchars = strlen (s);
5584 }
5585
5586 it->method = GET_FROM_C_STRING;
5587 }
5588
5589 /* PRECISION > 0 means don't return more than PRECISION characters
5590 from the string. */
5591 if (precision > 0 && it->end_charpos - charpos > precision)
5592 it->end_charpos = it->string_nchars = charpos + precision;
5593
5594 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5595 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5596 FIELD_WIDTH < 0 means infinite field width. This is useful for
5597 padding with `-' at the end of a mode line. */
5598 if (field_width < 0)
5599 field_width = INFINITY;
5600 if (field_width > it->end_charpos - charpos)
5601 it->end_charpos = charpos + field_width;
5602
5603 /* Use the standard display table for displaying strings. */
5604 if (DISP_TABLE_P (Vstandard_display_table))
5605 it->dp = XCHAR_TABLE (Vstandard_display_table);
5606
5607 it->stop_charpos = charpos;
5608 CHECK_IT (it);
5609 }
5610
5611
5612 \f
5613 /***********************************************************************
5614 Iteration
5615 ***********************************************************************/
5616
5617 /* Map enum it_method value to corresponding next_element_from_* function. */
5618
5619 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5620 {
5621 next_element_from_buffer,
5622 next_element_from_display_vector,
5623 next_element_from_composition,
5624 next_element_from_string,
5625 next_element_from_c_string,
5626 next_element_from_image,
5627 next_element_from_stretch
5628 };
5629
5630
5631 /* Load IT's display element fields with information about the next
5632 display element from the current position of IT. Value is zero if
5633 end of buffer (or C string) is reached. */
5634
5635 static struct frame *last_escape_glyph_frame = NULL;
5636 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5637 static int last_escape_glyph_merged_face_id = 0;
5638
5639 int
5640 get_next_display_element (it)
5641 struct it *it;
5642 {
5643 /* Non-zero means that we found a display element. Zero means that
5644 we hit the end of what we iterate over. Performance note: the
5645 function pointer `method' used here turns out to be faster than
5646 using a sequence of if-statements. */
5647 int success_p;
5648
5649 get_next:
5650 success_p = (*get_next_element[it->method]) (it);
5651
5652 if (it->what == IT_CHARACTER)
5653 {
5654 /* Map via display table or translate control characters.
5655 IT->c, IT->len etc. have been set to the next character by
5656 the function call above. If we have a display table, and it
5657 contains an entry for IT->c, translate it. Don't do this if
5658 IT->c itself comes from a display table, otherwise we could
5659 end up in an infinite recursion. (An alternative could be to
5660 count the recursion depth of this function and signal an
5661 error when a certain maximum depth is reached.) Is it worth
5662 it? */
5663 if (success_p && it->dpvec == NULL)
5664 {
5665 Lisp_Object dv;
5666
5667 if (it->dp
5668 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5669 VECTORP (dv)))
5670 {
5671 struct Lisp_Vector *v = XVECTOR (dv);
5672
5673 /* Return the first character from the display table
5674 entry, if not empty. If empty, don't display the
5675 current character. */
5676 if (v->size)
5677 {
5678 it->dpvec_char_len = it->len;
5679 it->dpvec = v->contents;
5680 it->dpend = v->contents + v->size;
5681 it->current.dpvec_index = 0;
5682 it->dpvec_face_id = -1;
5683 it->saved_face_id = it->face_id;
5684 it->method = GET_FROM_DISPLAY_VECTOR;
5685 it->ellipsis_p = 0;
5686 }
5687 else
5688 {
5689 set_iterator_to_next (it, 0);
5690 }
5691 goto get_next;
5692 }
5693
5694 /* Translate control characters into `\003' or `^C' form.
5695 Control characters coming from a display table entry are
5696 currently not translated because we use IT->dpvec to hold
5697 the translation. This could easily be changed but I
5698 don't believe that it is worth doing.
5699
5700 If it->multibyte_p is nonzero, non-printable non-ASCII
5701 characters are also translated to octal form.
5702
5703 If it->multibyte_p is zero, eight-bit characters that
5704 don't have corresponding multibyte char code are also
5705 translated to octal form. */
5706 else if ((it->c < ' '
5707 ? (it->area != TEXT_AREA
5708 /* In mode line, treat \n, \t like other crl chars. */
5709 || (it->c != '\t'
5710 && it->glyph_row && it->glyph_row->mode_line_p)
5711 || (it->c != '\n' && it->c != '\t'))
5712 : (it->multibyte_p
5713 ? (!CHAR_PRINTABLE_P (it->c)
5714 || (!NILP (Vnobreak_char_display)
5715 && (it->c == 0xA0 /* NO-BREAK SPACE */
5716 || it->c == 0xAD /* SOFT HYPHEN */)))
5717 : (it->c >= 127
5718 && (! unibyte_display_via_language_environment
5719 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5720 {
5721 /* IT->c is a control character which must be displayed
5722 either as '\003' or as `^C' where the '\\' and '^'
5723 can be defined in the display table. Fill
5724 IT->ctl_chars with glyphs for what we have to
5725 display. Then, set IT->dpvec to these glyphs. */
5726 GLYPH g;
5727 int ctl_len;
5728 int face_id, lface_id = 0 ;
5729 GLYPH escape_glyph;
5730
5731 /* Handle control characters with ^. */
5732
5733 if (it->c < 128 && it->ctl_arrow_p)
5734 {
5735 g = '^'; /* default glyph for Control */
5736 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5737 if (it->dp
5738 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5739 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5740 {
5741 g = XINT (DISP_CTRL_GLYPH (it->dp));
5742 lface_id = FAST_GLYPH_FACE (g);
5743 }
5744 if (lface_id)
5745 {
5746 g = FAST_GLYPH_CHAR (g);
5747 face_id = merge_faces (it->f, Qt, lface_id,
5748 it->face_id);
5749 }
5750 else if (it->f == last_escape_glyph_frame
5751 && it->face_id == last_escape_glyph_face_id)
5752 {
5753 face_id = last_escape_glyph_merged_face_id;
5754 }
5755 else
5756 {
5757 /* Merge the escape-glyph face into the current face. */
5758 face_id = merge_faces (it->f, Qescape_glyph, 0,
5759 it->face_id);
5760 last_escape_glyph_frame = it->f;
5761 last_escape_glyph_face_id = it->face_id;
5762 last_escape_glyph_merged_face_id = face_id;
5763 }
5764
5765 XSETINT (it->ctl_chars[0], g);
5766 g = it->c ^ 0100;
5767 XSETINT (it->ctl_chars[1], g);
5768 ctl_len = 2;
5769 goto display_control;
5770 }
5771
5772 /* Handle non-break space in the mode where it only gets
5773 highlighting. */
5774
5775 if (EQ (Vnobreak_char_display, Qt)
5776 && it->c == 0xA0)
5777 {
5778 /* Merge the no-break-space face into the current face. */
5779 face_id = merge_faces (it->f, Qnobreak_space, 0,
5780 it->face_id);
5781
5782 g = it->c = ' ';
5783 XSETINT (it->ctl_chars[0], g);
5784 ctl_len = 1;
5785 goto display_control;
5786 }
5787
5788 /* Handle sequences that start with the "escape glyph". */
5789
5790 /* the default escape glyph is \. */
5791 escape_glyph = '\\';
5792
5793 if (it->dp
5794 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5795 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5796 {
5797 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5798 lface_id = FAST_GLYPH_FACE (escape_glyph);
5799 }
5800 if (lface_id)
5801 {
5802 /* The display table specified a face.
5803 Merge it into face_id and also into escape_glyph. */
5804 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5805 face_id = merge_faces (it->f, Qt, lface_id,
5806 it->face_id);
5807 }
5808 else if (it->f == last_escape_glyph_frame
5809 && it->face_id == last_escape_glyph_face_id)
5810 {
5811 face_id = last_escape_glyph_merged_face_id;
5812 }
5813 else
5814 {
5815 /* Merge the escape-glyph face into the current face. */
5816 face_id = merge_faces (it->f, Qescape_glyph, 0,
5817 it->face_id);
5818 last_escape_glyph_frame = it->f;
5819 last_escape_glyph_face_id = it->face_id;
5820 last_escape_glyph_merged_face_id = face_id;
5821 }
5822
5823 /* Handle soft hyphens in the mode where they only get
5824 highlighting. */
5825
5826 if (EQ (Vnobreak_char_display, Qt)
5827 && it->c == 0xAD)
5828 {
5829 g = it->c = '-';
5830 XSETINT (it->ctl_chars[0], g);
5831 ctl_len = 1;
5832 goto display_control;
5833 }
5834
5835 /* Handle non-break space and soft hyphen
5836 with the escape glyph. */
5837
5838 if (it->c == 0xA0 || it->c == 0xAD)
5839 {
5840 XSETINT (it->ctl_chars[0], escape_glyph);
5841 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5842 XSETINT (it->ctl_chars[1], g);
5843 ctl_len = 2;
5844 goto display_control;
5845 }
5846
5847 {
5848 unsigned char str[MAX_MULTIBYTE_LENGTH];
5849 int len;
5850 int i;
5851
5852 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5853 if (CHAR_BYTE8_P (it->c))
5854 {
5855 str[0] = CHAR_TO_BYTE8 (it->c);
5856 len = 1;
5857 }
5858 else if (it->c < 256)
5859 {
5860 str[0] = it->c;
5861 len = 1;
5862 }
5863 else
5864 {
5865 /* It's an invalid character, which shouldn't
5866 happen actually, but due to bugs it may
5867 happen. Let's print the char as is, there's
5868 not much meaningful we can do with it. */
5869 str[0] = it->c;
5870 str[1] = it->c >> 8;
5871 str[2] = it->c >> 16;
5872 str[3] = it->c >> 24;
5873 len = 4;
5874 }
5875
5876 for (i = 0; i < len; i++)
5877 {
5878 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5879 /* Insert three more glyphs into IT->ctl_chars for
5880 the octal display of the character. */
5881 g = ((str[i] >> 6) & 7) + '0';
5882 XSETINT (it->ctl_chars[i * 4 + 1], g);
5883 g = ((str[i] >> 3) & 7) + '0';
5884 XSETINT (it->ctl_chars[i * 4 + 2], g);
5885 g = (str[i] & 7) + '0';
5886 XSETINT (it->ctl_chars[i * 4 + 3], g);
5887 }
5888 ctl_len = len * 4;
5889 }
5890
5891 display_control:
5892 /* Set up IT->dpvec and return first character from it. */
5893 it->dpvec_char_len = it->len;
5894 it->dpvec = it->ctl_chars;
5895 it->dpend = it->dpvec + ctl_len;
5896 it->current.dpvec_index = 0;
5897 it->dpvec_face_id = face_id;
5898 it->saved_face_id = it->face_id;
5899 it->method = GET_FROM_DISPLAY_VECTOR;
5900 it->ellipsis_p = 0;
5901 goto get_next;
5902 }
5903 }
5904 }
5905
5906 /* Adjust face id for a multibyte character. There are no multibyte
5907 character in unibyte text. */
5908 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5909 && it->multibyte_p
5910 && success_p
5911 && FRAME_WINDOW_P (it->f))
5912 {
5913 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5914 int pos = (it->s ? -1
5915 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5916 : IT_CHARPOS (*it));
5917
5918 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5919 }
5920
5921 /* Is this character the last one of a run of characters with
5922 box? If yes, set IT->end_of_box_run_p to 1. */
5923 if (it->face_box_p
5924 && it->s == NULL)
5925 {
5926 int face_id;
5927 struct face *face;
5928
5929 it->end_of_box_run_p
5930 = ((face_id = face_after_it_pos (it),
5931 face_id != it->face_id)
5932 && (face = FACE_FROM_ID (it->f, face_id),
5933 face->box == FACE_NO_BOX));
5934 }
5935
5936 /* Value is 0 if end of buffer or string reached. */
5937 return success_p;
5938 }
5939
5940
5941 /* Move IT to the next display element.
5942
5943 RESEAT_P non-zero means if called on a newline in buffer text,
5944 skip to the next visible line start.
5945
5946 Functions get_next_display_element and set_iterator_to_next are
5947 separate because I find this arrangement easier to handle than a
5948 get_next_display_element function that also increments IT's
5949 position. The way it is we can first look at an iterator's current
5950 display element, decide whether it fits on a line, and if it does,
5951 increment the iterator position. The other way around we probably
5952 would either need a flag indicating whether the iterator has to be
5953 incremented the next time, or we would have to implement a
5954 decrement position function which would not be easy to write. */
5955
5956 void
5957 set_iterator_to_next (it, reseat_p)
5958 struct it *it;
5959 int reseat_p;
5960 {
5961 /* Reset flags indicating start and end of a sequence of characters
5962 with box. Reset them at the start of this function because
5963 moving the iterator to a new position might set them. */
5964 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5965
5966 switch (it->method)
5967 {
5968 case GET_FROM_BUFFER:
5969 /* The current display element of IT is a character from
5970 current_buffer. Advance in the buffer, and maybe skip over
5971 invisible lines that are so because of selective display. */
5972 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5973 reseat_at_next_visible_line_start (it, 0);
5974 else
5975 {
5976 xassert (it->len != 0);
5977 IT_BYTEPOS (*it) += it->len;
5978 IT_CHARPOS (*it) += 1;
5979 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5980 }
5981 break;
5982
5983 case GET_FROM_COMPOSITION:
5984 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5985 xassert (it->sp > 0);
5986 pop_it (it);
5987 if (it->method == GET_FROM_STRING)
5988 {
5989 IT_STRING_BYTEPOS (*it) += it->len;
5990 IT_STRING_CHARPOS (*it) += it->cmp_len;
5991 goto consider_string_end;
5992 }
5993 else if (it->method == GET_FROM_BUFFER)
5994 {
5995 IT_BYTEPOS (*it) += it->len;
5996 IT_CHARPOS (*it) += it->cmp_len;
5997 }
5998 break;
5999
6000 case GET_FROM_C_STRING:
6001 /* Current display element of IT is from a C string. */
6002 IT_BYTEPOS (*it) += it->len;
6003 IT_CHARPOS (*it) += 1;
6004 break;
6005
6006 case GET_FROM_DISPLAY_VECTOR:
6007 /* Current display element of IT is from a display table entry.
6008 Advance in the display table definition. Reset it to null if
6009 end reached, and continue with characters from buffers/
6010 strings. */
6011 ++it->current.dpvec_index;
6012
6013 /* Restore face of the iterator to what they were before the
6014 display vector entry (these entries may contain faces). */
6015 it->face_id = it->saved_face_id;
6016
6017 if (it->dpvec + it->current.dpvec_index == it->dpend)
6018 {
6019 int recheck_faces = it->ellipsis_p;
6020
6021 if (it->s)
6022 it->method = GET_FROM_C_STRING;
6023 else if (STRINGP (it->string))
6024 it->method = GET_FROM_STRING;
6025 else
6026 {
6027 it->method = GET_FROM_BUFFER;
6028 it->object = it->w->buffer;
6029 }
6030
6031 it->dpvec = NULL;
6032 it->current.dpvec_index = -1;
6033
6034 /* Skip over characters which were displayed via IT->dpvec. */
6035 if (it->dpvec_char_len < 0)
6036 reseat_at_next_visible_line_start (it, 1);
6037 else if (it->dpvec_char_len > 0)
6038 {
6039 if (it->method == GET_FROM_STRING
6040 && it->n_overlay_strings > 0)
6041 it->ignore_overlay_strings_at_pos_p = 1;
6042 it->len = it->dpvec_char_len;
6043 set_iterator_to_next (it, reseat_p);
6044 }
6045
6046 /* Maybe recheck faces after display vector */
6047 if (recheck_faces)
6048 it->stop_charpos = IT_CHARPOS (*it);
6049 }
6050 break;
6051
6052 case GET_FROM_STRING:
6053 /* Current display element is a character from a Lisp string. */
6054 xassert (it->s == NULL && STRINGP (it->string));
6055 IT_STRING_BYTEPOS (*it) += it->len;
6056 IT_STRING_CHARPOS (*it) += 1;
6057
6058 consider_string_end:
6059
6060 if (it->current.overlay_string_index >= 0)
6061 {
6062 /* IT->string is an overlay string. Advance to the
6063 next, if there is one. */
6064 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6065 next_overlay_string (it);
6066 }
6067 else
6068 {
6069 /* IT->string is not an overlay string. If we reached
6070 its end, and there is something on IT->stack, proceed
6071 with what is on the stack. This can be either another
6072 string, this time an overlay string, or a buffer. */
6073 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6074 && it->sp > 0)
6075 {
6076 pop_it (it);
6077 if (it->method == GET_FROM_STRING)
6078 goto consider_string_end;
6079 }
6080 }
6081 break;
6082
6083 case GET_FROM_IMAGE:
6084 case GET_FROM_STRETCH:
6085 /* The position etc with which we have to proceed are on
6086 the stack. The position may be at the end of a string,
6087 if the `display' property takes up the whole string. */
6088 xassert (it->sp > 0);
6089 pop_it (it);
6090 if (it->method == GET_FROM_STRING)
6091 goto consider_string_end;
6092 break;
6093
6094 default:
6095 /* There are no other methods defined, so this should be a bug. */
6096 abort ();
6097 }
6098
6099 xassert (it->method != GET_FROM_STRING
6100 || (STRINGP (it->string)
6101 && IT_STRING_CHARPOS (*it) >= 0));
6102 }
6103
6104 /* Load IT's display element fields with information about the next
6105 display element which comes from a display table entry or from the
6106 result of translating a control character to one of the forms `^C'
6107 or `\003'.
6108
6109 IT->dpvec holds the glyphs to return as characters.
6110 IT->saved_face_id holds the face id before the display vector--
6111 it is restored into IT->face_idin set_iterator_to_next. */
6112
6113 static int
6114 next_element_from_display_vector (it)
6115 struct it *it;
6116 {
6117 /* Precondition. */
6118 xassert (it->dpvec && it->current.dpvec_index >= 0);
6119
6120 it->face_id = it->saved_face_id;
6121
6122 if (INTEGERP (*it->dpvec)
6123 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6124 {
6125 GLYPH g;
6126
6127 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6128 it->c = FAST_GLYPH_CHAR (g);
6129 it->len = CHAR_BYTES (it->c);
6130
6131 /* The entry may contain a face id to use. Such a face id is
6132 the id of a Lisp face, not a realized face. A face id of
6133 zero means no face is specified. */
6134 if (it->dpvec_face_id >= 0)
6135 it->face_id = it->dpvec_face_id;
6136 else
6137 {
6138 int lface_id = FAST_GLYPH_FACE (g);
6139 if (lface_id > 0)
6140 it->face_id = merge_faces (it->f, Qt, lface_id,
6141 it->saved_face_id);
6142 }
6143 }
6144 else
6145 /* Display table entry is invalid. Return a space. */
6146 it->c = ' ', it->len = 1;
6147
6148 /* Don't change position and object of the iterator here. They are
6149 still the values of the character that had this display table
6150 entry or was translated, and that's what we want. */
6151 it->what = IT_CHARACTER;
6152 return 1;
6153 }
6154
6155
6156 /* Load IT with the next display element from Lisp string IT->string.
6157 IT->current.string_pos is the current position within the string.
6158 If IT->current.overlay_string_index >= 0, the Lisp string is an
6159 overlay string. */
6160
6161 static int
6162 next_element_from_string (it)
6163 struct it *it;
6164 {
6165 struct text_pos position;
6166
6167 xassert (STRINGP (it->string));
6168 xassert (IT_STRING_CHARPOS (*it) >= 0);
6169 position = it->current.string_pos;
6170
6171 /* Time to check for invisible text? */
6172 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6173 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6174 {
6175 handle_stop (it);
6176
6177 /* Since a handler may have changed IT->method, we must
6178 recurse here. */
6179 return get_next_display_element (it);
6180 }
6181
6182 if (it->current.overlay_string_index >= 0)
6183 {
6184 /* Get the next character from an overlay string. In overlay
6185 strings, There is no field width or padding with spaces to
6186 do. */
6187 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6188 {
6189 it->what = IT_EOB;
6190 return 0;
6191 }
6192 else if (STRING_MULTIBYTE (it->string))
6193 {
6194 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6195 const unsigned char *s = (SDATA (it->string)
6196 + IT_STRING_BYTEPOS (*it));
6197 it->c = string_char_and_length (s, remaining, &it->len);
6198 }
6199 else
6200 {
6201 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6202 it->len = 1;
6203 }
6204 }
6205 else
6206 {
6207 /* Get the next character from a Lisp string that is not an
6208 overlay string. Such strings come from the mode line, for
6209 example. We may have to pad with spaces, or truncate the
6210 string. See also next_element_from_c_string. */
6211 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6212 {
6213 it->what = IT_EOB;
6214 return 0;
6215 }
6216 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6217 {
6218 /* Pad with spaces. */
6219 it->c = ' ', it->len = 1;
6220 CHARPOS (position) = BYTEPOS (position) = -1;
6221 }
6222 else if (STRING_MULTIBYTE (it->string))
6223 {
6224 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6225 const unsigned char *s = (SDATA (it->string)
6226 + IT_STRING_BYTEPOS (*it));
6227 it->c = string_char_and_length (s, maxlen, &it->len);
6228 }
6229 else
6230 {
6231 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6232 it->len = 1;
6233 }
6234 }
6235
6236 /* Record what we have and where it came from. */
6237 it->what = IT_CHARACTER;
6238 it->object = it->string;
6239 it->position = position;
6240 return 1;
6241 }
6242
6243
6244 /* Load IT with next display element from C string IT->s.
6245 IT->string_nchars is the maximum number of characters to return
6246 from the string. IT->end_charpos may be greater than
6247 IT->string_nchars when this function is called, in which case we
6248 may have to return padding spaces. Value is zero if end of string
6249 reached, including padding spaces. */
6250
6251 static int
6252 next_element_from_c_string (it)
6253 struct it *it;
6254 {
6255 int success_p = 1;
6256
6257 xassert (it->s);
6258 it->what = IT_CHARACTER;
6259 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6260 it->object = Qnil;
6261
6262 /* IT's position can be greater IT->string_nchars in case a field
6263 width or precision has been specified when the iterator was
6264 initialized. */
6265 if (IT_CHARPOS (*it) >= it->end_charpos)
6266 {
6267 /* End of the game. */
6268 it->what = IT_EOB;
6269 success_p = 0;
6270 }
6271 else if (IT_CHARPOS (*it) >= it->string_nchars)
6272 {
6273 /* Pad with spaces. */
6274 it->c = ' ', it->len = 1;
6275 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6276 }
6277 else if (it->multibyte_p)
6278 {
6279 /* Implementation note: The calls to strlen apparently aren't a
6280 performance problem because there is no noticeable performance
6281 difference between Emacs running in unibyte or multibyte mode. */
6282 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6283 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6284 maxlen, &it->len);
6285 }
6286 else
6287 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6288
6289 return success_p;
6290 }
6291
6292
6293 /* Set up IT to return characters from an ellipsis, if appropriate.
6294 The definition of the ellipsis glyphs may come from a display table
6295 entry. This function Fills IT with the first glyph from the
6296 ellipsis if an ellipsis is to be displayed. */
6297
6298 static int
6299 next_element_from_ellipsis (it)
6300 struct it *it;
6301 {
6302 if (it->selective_display_ellipsis_p)
6303 setup_for_ellipsis (it, it->len);
6304 else
6305 {
6306 /* The face at the current position may be different from the
6307 face we find after the invisible text. Remember what it
6308 was in IT->saved_face_id, and signal that it's there by
6309 setting face_before_selective_p. */
6310 it->saved_face_id = it->face_id;
6311 it->method = GET_FROM_BUFFER;
6312 it->object = it->w->buffer;
6313 reseat_at_next_visible_line_start (it, 1);
6314 it->face_before_selective_p = 1;
6315 }
6316
6317 return get_next_display_element (it);
6318 }
6319
6320
6321 /* Deliver an image display element. The iterator IT is already
6322 filled with image information (done in handle_display_prop). Value
6323 is always 1. */
6324
6325
6326 static int
6327 next_element_from_image (it)
6328 struct it *it;
6329 {
6330 it->what = IT_IMAGE;
6331 return 1;
6332 }
6333
6334
6335 /* Fill iterator IT with next display element from a stretch glyph
6336 property. IT->object is the value of the text property. Value is
6337 always 1. */
6338
6339 static int
6340 next_element_from_stretch (it)
6341 struct it *it;
6342 {
6343 it->what = IT_STRETCH;
6344 return 1;
6345 }
6346
6347
6348 /* Load IT with the next display element from current_buffer. Value
6349 is zero if end of buffer reached. IT->stop_charpos is the next
6350 position at which to stop and check for text properties or buffer
6351 end. */
6352
6353 static int
6354 next_element_from_buffer (it)
6355 struct it *it;
6356 {
6357 int success_p = 1;
6358
6359 /* Check this assumption, otherwise, we would never enter the
6360 if-statement, below. */
6361 xassert (IT_CHARPOS (*it) >= BEGV
6362 && IT_CHARPOS (*it) <= it->stop_charpos);
6363
6364 if (IT_CHARPOS (*it) >= it->stop_charpos)
6365 {
6366 if (IT_CHARPOS (*it) >= it->end_charpos)
6367 {
6368 int overlay_strings_follow_p;
6369
6370 /* End of the game, except when overlay strings follow that
6371 haven't been returned yet. */
6372 if (it->overlay_strings_at_end_processed_p)
6373 overlay_strings_follow_p = 0;
6374 else
6375 {
6376 it->overlay_strings_at_end_processed_p = 1;
6377 overlay_strings_follow_p = get_overlay_strings (it, 0);
6378 }
6379
6380 if (overlay_strings_follow_p)
6381 success_p = get_next_display_element (it);
6382 else
6383 {
6384 it->what = IT_EOB;
6385 it->position = it->current.pos;
6386 success_p = 0;
6387 }
6388 }
6389 else
6390 {
6391 handle_stop (it);
6392 return get_next_display_element (it);
6393 }
6394 }
6395 else
6396 {
6397 /* No face changes, overlays etc. in sight, so just return a
6398 character from current_buffer. */
6399 unsigned char *p;
6400
6401 /* Maybe run the redisplay end trigger hook. Performance note:
6402 This doesn't seem to cost measurable time. */
6403 if (it->redisplay_end_trigger_charpos
6404 && it->glyph_row
6405 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6406 run_redisplay_end_trigger_hook (it);
6407
6408 /* Get the next character, maybe multibyte. */
6409 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6410 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6411 {
6412 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6413 - IT_BYTEPOS (*it));
6414 it->c = string_char_and_length (p, maxlen, &it->len);
6415 }
6416 else
6417 it->c = *p, it->len = 1;
6418
6419 /* Record what we have and where it came from. */
6420 it->what = IT_CHARACTER;;
6421 it->object = it->w->buffer;
6422 it->position = it->current.pos;
6423
6424 /* Normally we return the character found above, except when we
6425 really want to return an ellipsis for selective display. */
6426 if (it->selective)
6427 {
6428 if (it->c == '\n')
6429 {
6430 /* A value of selective > 0 means hide lines indented more
6431 than that number of columns. */
6432 if (it->selective > 0
6433 && IT_CHARPOS (*it) + 1 < ZV
6434 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6435 IT_BYTEPOS (*it) + 1,
6436 (double) it->selective)) /* iftc */
6437 {
6438 success_p = next_element_from_ellipsis (it);
6439 it->dpvec_char_len = -1;
6440 }
6441 }
6442 else if (it->c == '\r' && it->selective == -1)
6443 {
6444 /* A value of selective == -1 means that everything from the
6445 CR to the end of the line is invisible, with maybe an
6446 ellipsis displayed for it. */
6447 success_p = next_element_from_ellipsis (it);
6448 it->dpvec_char_len = -1;
6449 }
6450 }
6451 }
6452
6453 /* Value is zero if end of buffer reached. */
6454 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6455 return success_p;
6456 }
6457
6458
6459 /* Run the redisplay end trigger hook for IT. */
6460
6461 static void
6462 run_redisplay_end_trigger_hook (it)
6463 struct it *it;
6464 {
6465 Lisp_Object args[3];
6466
6467 /* IT->glyph_row should be non-null, i.e. we should be actually
6468 displaying something, or otherwise we should not run the hook. */
6469 xassert (it->glyph_row);
6470
6471 /* Set up hook arguments. */
6472 args[0] = Qredisplay_end_trigger_functions;
6473 args[1] = it->window;
6474 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6475 it->redisplay_end_trigger_charpos = 0;
6476
6477 /* Since we are *trying* to run these functions, don't try to run
6478 them again, even if they get an error. */
6479 it->w->redisplay_end_trigger = Qnil;
6480 Frun_hook_with_args (3, args);
6481
6482 /* Notice if it changed the face of the character we are on. */
6483 handle_face_prop (it);
6484 }
6485
6486
6487 /* Deliver a composition display element. The iterator IT is already
6488 filled with composition information (done in
6489 handle_composition_prop). Value is always 1. */
6490
6491 static int
6492 next_element_from_composition (it)
6493 struct it *it;
6494 {
6495 it->what = IT_COMPOSITION;
6496 it->position = (STRINGP (it->string)
6497 ? it->current.string_pos
6498 : it->current.pos);
6499 if (STRINGP (it->string))
6500 it->object = it->string;
6501 else
6502 it->object = it->w->buffer;
6503 return 1;
6504 }
6505
6506
6507 \f
6508 /***********************************************************************
6509 Moving an iterator without producing glyphs
6510 ***********************************************************************/
6511
6512 /* Check if iterator is at a position corresponding to a valid buffer
6513 position after some move_it_ call. */
6514
6515 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6516 ((it)->method == GET_FROM_STRING \
6517 ? IT_STRING_CHARPOS (*it) == 0 \
6518 : 1)
6519
6520
6521 /* Move iterator IT to a specified buffer or X position within one
6522 line on the display without producing glyphs.
6523
6524 OP should be a bit mask including some or all of these bits:
6525 MOVE_TO_X: Stop on reaching x-position TO_X.
6526 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6527 Regardless of OP's value, stop in reaching the end of the display line.
6528
6529 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6530 This means, in particular, that TO_X includes window's horizontal
6531 scroll amount.
6532
6533 The return value has several possible values that
6534 say what condition caused the scan to stop:
6535
6536 MOVE_POS_MATCH_OR_ZV
6537 - when TO_POS or ZV was reached.
6538
6539 MOVE_X_REACHED
6540 -when TO_X was reached before TO_POS or ZV were reached.
6541
6542 MOVE_LINE_CONTINUED
6543 - when we reached the end of the display area and the line must
6544 be continued.
6545
6546 MOVE_LINE_TRUNCATED
6547 - when we reached the end of the display area and the line is
6548 truncated.
6549
6550 MOVE_NEWLINE_OR_CR
6551 - when we stopped at a line end, i.e. a newline or a CR and selective
6552 display is on. */
6553
6554 static enum move_it_result
6555 move_it_in_display_line_to (it, to_charpos, to_x, op)
6556 struct it *it;
6557 int to_charpos, to_x, op;
6558 {
6559 enum move_it_result result = MOVE_UNDEFINED;
6560 struct glyph_row *saved_glyph_row;
6561
6562 /* Don't produce glyphs in produce_glyphs. */
6563 saved_glyph_row = it->glyph_row;
6564 it->glyph_row = NULL;
6565
6566 #define BUFFER_POS_REACHED_P() \
6567 ((op & MOVE_TO_POS) != 0 \
6568 && BUFFERP (it->object) \
6569 && IT_CHARPOS (*it) >= to_charpos \
6570 && (it->method == GET_FROM_BUFFER \
6571 || (it->method == GET_FROM_DISPLAY_VECTOR \
6572 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6573
6574
6575 while (1)
6576 {
6577 int x, i, ascent = 0, descent = 0;
6578
6579 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6580 if ((op & MOVE_TO_POS) != 0
6581 && BUFFERP (it->object)
6582 && it->method == GET_FROM_BUFFER
6583 && IT_CHARPOS (*it) > to_charpos)
6584 {
6585 result = MOVE_POS_MATCH_OR_ZV;
6586 break;
6587 }
6588
6589 /* Stop when ZV reached.
6590 We used to stop here when TO_CHARPOS reached as well, but that is
6591 too soon if this glyph does not fit on this line. So we handle it
6592 explicitly below. */
6593 if (!get_next_display_element (it)
6594 || (it->truncate_lines_p
6595 && BUFFER_POS_REACHED_P ()))
6596 {
6597 result = MOVE_POS_MATCH_OR_ZV;
6598 break;
6599 }
6600
6601 /* The call to produce_glyphs will get the metrics of the
6602 display element IT is loaded with. We record in x the
6603 x-position before this display element in case it does not
6604 fit on the line. */
6605 x = it->current_x;
6606
6607 /* Remember the line height so far in case the next element doesn't
6608 fit on the line. */
6609 if (!it->truncate_lines_p)
6610 {
6611 ascent = it->max_ascent;
6612 descent = it->max_descent;
6613 }
6614
6615 PRODUCE_GLYPHS (it);
6616
6617 if (it->area != TEXT_AREA)
6618 {
6619 set_iterator_to_next (it, 1);
6620 continue;
6621 }
6622
6623 /* The number of glyphs we get back in IT->nglyphs will normally
6624 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6625 character on a terminal frame, or (iii) a line end. For the
6626 second case, IT->nglyphs - 1 padding glyphs will be present
6627 (on X frames, there is only one glyph produced for a
6628 composite character.
6629
6630 The behavior implemented below means, for continuation lines,
6631 that as many spaces of a TAB as fit on the current line are
6632 displayed there. For terminal frames, as many glyphs of a
6633 multi-glyph character are displayed in the current line, too.
6634 This is what the old redisplay code did, and we keep it that
6635 way. Under X, the whole shape of a complex character must
6636 fit on the line or it will be completely displayed in the
6637 next line.
6638
6639 Note that both for tabs and padding glyphs, all glyphs have
6640 the same width. */
6641 if (it->nglyphs)
6642 {
6643 /* More than one glyph or glyph doesn't fit on line. All
6644 glyphs have the same width. */
6645 int single_glyph_width = it->pixel_width / it->nglyphs;
6646 int new_x;
6647 int x_before_this_char = x;
6648 int hpos_before_this_char = it->hpos;
6649
6650 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6651 {
6652 new_x = x + single_glyph_width;
6653
6654 /* We want to leave anything reaching TO_X to the caller. */
6655 if ((op & MOVE_TO_X) && new_x > to_x)
6656 {
6657 if (BUFFER_POS_REACHED_P ())
6658 goto buffer_pos_reached;
6659 it->current_x = x;
6660 result = MOVE_X_REACHED;
6661 break;
6662 }
6663 else if (/* Lines are continued. */
6664 !it->truncate_lines_p
6665 && (/* And glyph doesn't fit on the line. */
6666 new_x > it->last_visible_x
6667 /* Or it fits exactly and we're on a window
6668 system frame. */
6669 || (new_x == it->last_visible_x
6670 && FRAME_WINDOW_P (it->f))))
6671 {
6672 if (/* IT->hpos == 0 means the very first glyph
6673 doesn't fit on the line, e.g. a wide image. */
6674 it->hpos == 0
6675 || (new_x == it->last_visible_x
6676 && FRAME_WINDOW_P (it->f)))
6677 {
6678 ++it->hpos;
6679 it->current_x = new_x;
6680
6681 /* The character's last glyph just barely fits
6682 in this row. */
6683 if (i == it->nglyphs - 1)
6684 {
6685 /* If this is the destination position,
6686 return a position *before* it in this row,
6687 now that we know it fits in this row. */
6688 if (BUFFER_POS_REACHED_P ())
6689 {
6690 it->hpos = hpos_before_this_char;
6691 it->current_x = x_before_this_char;
6692 result = MOVE_POS_MATCH_OR_ZV;
6693 break;
6694 }
6695
6696 set_iterator_to_next (it, 1);
6697 #ifdef HAVE_WINDOW_SYSTEM
6698 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6699 {
6700 if (!get_next_display_element (it))
6701 {
6702 result = MOVE_POS_MATCH_OR_ZV;
6703 break;
6704 }
6705 if (BUFFER_POS_REACHED_P ())
6706 {
6707 if (ITERATOR_AT_END_OF_LINE_P (it))
6708 result = MOVE_POS_MATCH_OR_ZV;
6709 else
6710 result = MOVE_LINE_CONTINUED;
6711 break;
6712 }
6713 if (ITERATOR_AT_END_OF_LINE_P (it))
6714 {
6715 result = MOVE_NEWLINE_OR_CR;
6716 break;
6717 }
6718 }
6719 #endif /* HAVE_WINDOW_SYSTEM */
6720 }
6721 }
6722 else
6723 {
6724 it->current_x = x;
6725 it->max_ascent = ascent;
6726 it->max_descent = descent;
6727 }
6728
6729 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6730 IT_CHARPOS (*it)));
6731 result = MOVE_LINE_CONTINUED;
6732 break;
6733 }
6734 else if (BUFFER_POS_REACHED_P ())
6735 goto buffer_pos_reached;
6736 else if (new_x > it->first_visible_x)
6737 {
6738 /* Glyph is visible. Increment number of glyphs that
6739 would be displayed. */
6740 ++it->hpos;
6741 }
6742 else
6743 {
6744 /* Glyph is completely off the left margin of the display
6745 area. Nothing to do. */
6746 }
6747 }
6748
6749 if (result != MOVE_UNDEFINED)
6750 break;
6751 }
6752 else if (BUFFER_POS_REACHED_P ())
6753 {
6754 buffer_pos_reached:
6755 it->current_x = x;
6756 it->max_ascent = ascent;
6757 it->max_descent = descent;
6758 result = MOVE_POS_MATCH_OR_ZV;
6759 break;
6760 }
6761 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6762 {
6763 /* Stop when TO_X specified and reached. This check is
6764 necessary here because of lines consisting of a line end,
6765 only. The line end will not produce any glyphs and we
6766 would never get MOVE_X_REACHED. */
6767 xassert (it->nglyphs == 0);
6768 result = MOVE_X_REACHED;
6769 break;
6770 }
6771
6772 /* Is this a line end? If yes, we're done. */
6773 if (ITERATOR_AT_END_OF_LINE_P (it))
6774 {
6775 result = MOVE_NEWLINE_OR_CR;
6776 break;
6777 }
6778
6779 /* The current display element has been consumed. Advance
6780 to the next. */
6781 set_iterator_to_next (it, 1);
6782
6783 /* Stop if lines are truncated and IT's current x-position is
6784 past the right edge of the window now. */
6785 if (it->truncate_lines_p
6786 && it->current_x >= it->last_visible_x)
6787 {
6788 #ifdef HAVE_WINDOW_SYSTEM
6789 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6790 {
6791 if (!get_next_display_element (it)
6792 || BUFFER_POS_REACHED_P ())
6793 {
6794 result = MOVE_POS_MATCH_OR_ZV;
6795 break;
6796 }
6797 if (ITERATOR_AT_END_OF_LINE_P (it))
6798 {
6799 result = MOVE_NEWLINE_OR_CR;
6800 break;
6801 }
6802 }
6803 #endif /* HAVE_WINDOW_SYSTEM */
6804 result = MOVE_LINE_TRUNCATED;
6805 break;
6806 }
6807 }
6808
6809 #undef BUFFER_POS_REACHED_P
6810
6811 /* Restore the iterator settings altered at the beginning of this
6812 function. */
6813 it->glyph_row = saved_glyph_row;
6814 return result;
6815 }
6816
6817
6818 /* Move IT forward until it satisfies one or more of the criteria in
6819 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6820
6821 OP is a bit-mask that specifies where to stop, and in particular,
6822 which of those four position arguments makes a difference. See the
6823 description of enum move_operation_enum.
6824
6825 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6826 screen line, this function will set IT to the next position >
6827 TO_CHARPOS. */
6828
6829 void
6830 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6831 struct it *it;
6832 int to_charpos, to_x, to_y, to_vpos;
6833 int op;
6834 {
6835 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6836 int line_height;
6837 int reached = 0;
6838
6839 for (;;)
6840 {
6841 if (op & MOVE_TO_VPOS)
6842 {
6843 /* If no TO_CHARPOS and no TO_X specified, stop at the
6844 start of the line TO_VPOS. */
6845 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6846 {
6847 if (it->vpos == to_vpos)
6848 {
6849 reached = 1;
6850 break;
6851 }
6852 else
6853 skip = move_it_in_display_line_to (it, -1, -1, 0);
6854 }
6855 else
6856 {
6857 /* TO_VPOS >= 0 means stop at TO_X in the line at
6858 TO_VPOS, or at TO_POS, whichever comes first. */
6859 if (it->vpos == to_vpos)
6860 {
6861 reached = 2;
6862 break;
6863 }
6864
6865 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6866
6867 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6868 {
6869 reached = 3;
6870 break;
6871 }
6872 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6873 {
6874 /* We have reached TO_X but not in the line we want. */
6875 skip = move_it_in_display_line_to (it, to_charpos,
6876 -1, MOVE_TO_POS);
6877 if (skip == MOVE_POS_MATCH_OR_ZV)
6878 {
6879 reached = 4;
6880 break;
6881 }
6882 }
6883 }
6884 }
6885 else if (op & MOVE_TO_Y)
6886 {
6887 struct it it_backup;
6888
6889 /* TO_Y specified means stop at TO_X in the line containing
6890 TO_Y---or at TO_CHARPOS if this is reached first. The
6891 problem is that we can't really tell whether the line
6892 contains TO_Y before we have completely scanned it, and
6893 this may skip past TO_X. What we do is to first scan to
6894 TO_X.
6895
6896 If TO_X is not specified, use a TO_X of zero. The reason
6897 is to make the outcome of this function more predictable.
6898 If we didn't use TO_X == 0, we would stop at the end of
6899 the line which is probably not what a caller would expect
6900 to happen. */
6901 skip = move_it_in_display_line_to (it, to_charpos,
6902 ((op & MOVE_TO_X)
6903 ? to_x : 0),
6904 (MOVE_TO_X
6905 | (op & MOVE_TO_POS)));
6906
6907 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6908 if (skip == MOVE_POS_MATCH_OR_ZV)
6909 {
6910 reached = 5;
6911 break;
6912 }
6913
6914 /* If TO_X was reached, we would like to know whether TO_Y
6915 is in the line. This can only be said if we know the
6916 total line height which requires us to scan the rest of
6917 the line. */
6918 if (skip == MOVE_X_REACHED)
6919 {
6920 /* Wait! We can conclude that TO_Y is in the line if
6921 the already scanned glyphs make the line tall enough
6922 because further scanning doesn't make it shorter. */
6923 line_height = it->max_ascent + it->max_descent;
6924 if (to_y >= it->current_y
6925 && to_y < it->current_y + line_height)
6926 {
6927 reached = 6;
6928 break;
6929 }
6930 it_backup = *it;
6931 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6932 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6933 op & MOVE_TO_POS);
6934 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6935 }
6936
6937 /* Now, decide whether TO_Y is in this line. */
6938 line_height = it->max_ascent + it->max_descent;
6939 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6940
6941 if (to_y >= it->current_y
6942 && to_y < it->current_y + line_height)
6943 {
6944 if (skip == MOVE_X_REACHED)
6945 /* If TO_Y is in this line and TO_X was reached above,
6946 we scanned too far. We have to restore IT's settings
6947 to the ones before skipping. */
6948 *it = it_backup;
6949 reached = 6;
6950 }
6951 else if (skip == MOVE_X_REACHED)
6952 {
6953 skip = skip2;
6954 if (skip == MOVE_POS_MATCH_OR_ZV)
6955 reached = 7;
6956 }
6957
6958 if (reached)
6959 break;
6960 }
6961 else if (BUFFERP (it->object)
6962 && it->method == GET_FROM_BUFFER
6963 && IT_CHARPOS (*it) >= to_charpos)
6964 skip = MOVE_POS_MATCH_OR_ZV;
6965 else
6966 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6967
6968 switch (skip)
6969 {
6970 case MOVE_POS_MATCH_OR_ZV:
6971 reached = 8;
6972 goto out;
6973
6974 case MOVE_NEWLINE_OR_CR:
6975 set_iterator_to_next (it, 1);
6976 it->continuation_lines_width = 0;
6977 break;
6978
6979 case MOVE_LINE_TRUNCATED:
6980 it->continuation_lines_width = 0;
6981 reseat_at_next_visible_line_start (it, 0);
6982 if ((op & MOVE_TO_POS) != 0
6983 && IT_CHARPOS (*it) > to_charpos)
6984 {
6985 reached = 9;
6986 goto out;
6987 }
6988 break;
6989
6990 case MOVE_LINE_CONTINUED:
6991 /* For continued lines ending in a tab, some of the glyphs
6992 associated with the tab are displayed on the current
6993 line. Since it->current_x does not include these glyphs,
6994 we use it->last_visible_x instead. */
6995 it->continuation_lines_width +=
6996 (it->c == '\t') ? it->last_visible_x : it->current_x;
6997 break;
6998
6999 default:
7000 abort ();
7001 }
7002
7003 /* Reset/increment for the next run. */
7004 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7005 it->current_x = it->hpos = 0;
7006 it->current_y += it->max_ascent + it->max_descent;
7007 ++it->vpos;
7008 last_height = it->max_ascent + it->max_descent;
7009 last_max_ascent = it->max_ascent;
7010 it->max_ascent = it->max_descent = 0;
7011 }
7012
7013 out:
7014
7015 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7016 }
7017
7018
7019 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7020
7021 If DY > 0, move IT backward at least that many pixels. DY = 0
7022 means move IT backward to the preceding line start or BEGV. This
7023 function may move over more than DY pixels if IT->current_y - DY
7024 ends up in the middle of a line; in this case IT->current_y will be
7025 set to the top of the line moved to. */
7026
7027 void
7028 move_it_vertically_backward (it, dy)
7029 struct it *it;
7030 int dy;
7031 {
7032 int nlines, h;
7033 struct it it2, it3;
7034 int start_pos;
7035
7036 move_further_back:
7037 xassert (dy >= 0);
7038
7039 start_pos = IT_CHARPOS (*it);
7040
7041 /* Estimate how many newlines we must move back. */
7042 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7043
7044 /* Set the iterator's position that many lines back. */
7045 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7046 back_to_previous_visible_line_start (it);
7047
7048 /* Reseat the iterator here. When moving backward, we don't want
7049 reseat to skip forward over invisible text, set up the iterator
7050 to deliver from overlay strings at the new position etc. So,
7051 use reseat_1 here. */
7052 reseat_1 (it, it->current.pos, 1);
7053
7054 /* We are now surely at a line start. */
7055 it->current_x = it->hpos = 0;
7056 it->continuation_lines_width = 0;
7057
7058 /* Move forward and see what y-distance we moved. First move to the
7059 start of the next line so that we get its height. We need this
7060 height to be able to tell whether we reached the specified
7061 y-distance. */
7062 it2 = *it;
7063 it2.max_ascent = it2.max_descent = 0;
7064 do
7065 {
7066 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7067 MOVE_TO_POS | MOVE_TO_VPOS);
7068 }
7069 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7070 xassert (IT_CHARPOS (*it) >= BEGV);
7071 it3 = it2;
7072
7073 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7074 xassert (IT_CHARPOS (*it) >= BEGV);
7075 /* H is the actual vertical distance from the position in *IT
7076 and the starting position. */
7077 h = it2.current_y - it->current_y;
7078 /* NLINES is the distance in number of lines. */
7079 nlines = it2.vpos - it->vpos;
7080
7081 /* Correct IT's y and vpos position
7082 so that they are relative to the starting point. */
7083 it->vpos -= nlines;
7084 it->current_y -= h;
7085
7086 if (dy == 0)
7087 {
7088 /* DY == 0 means move to the start of the screen line. The
7089 value of nlines is > 0 if continuation lines were involved. */
7090 if (nlines > 0)
7091 move_it_by_lines (it, nlines, 1);
7092 #if 0
7093 /* I think this assert is bogus if buffer contains
7094 invisible text or images. KFS. */
7095 xassert (IT_CHARPOS (*it) <= start_pos);
7096 #endif
7097 }
7098 else
7099 {
7100 /* The y-position we try to reach, relative to *IT.
7101 Note that H has been subtracted in front of the if-statement. */
7102 int target_y = it->current_y + h - dy;
7103 int y0 = it3.current_y;
7104 int y1 = line_bottom_y (&it3);
7105 int line_height = y1 - y0;
7106
7107 /* If we did not reach target_y, try to move further backward if
7108 we can. If we moved too far backward, try to move forward. */
7109 if (target_y < it->current_y
7110 /* This is heuristic. In a window that's 3 lines high, with
7111 a line height of 13 pixels each, recentering with point
7112 on the bottom line will try to move -39/2 = 19 pixels
7113 backward. Try to avoid moving into the first line. */
7114 && (it->current_y - target_y
7115 > min (window_box_height (it->w), line_height * 2 / 3))
7116 && IT_CHARPOS (*it) > BEGV)
7117 {
7118 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7119 target_y - it->current_y));
7120 dy = it->current_y - target_y;
7121 goto move_further_back;
7122 }
7123 else if (target_y >= it->current_y + line_height
7124 && IT_CHARPOS (*it) < ZV)
7125 {
7126 /* Should move forward by at least one line, maybe more.
7127
7128 Note: Calling move_it_by_lines can be expensive on
7129 terminal frames, where compute_motion is used (via
7130 vmotion) to do the job, when there are very long lines
7131 and truncate-lines is nil. That's the reason for
7132 treating terminal frames specially here. */
7133
7134 if (!FRAME_WINDOW_P (it->f))
7135 move_it_vertically (it, target_y - (it->current_y + line_height));
7136 else
7137 {
7138 do
7139 {
7140 move_it_by_lines (it, 1, 1);
7141 }
7142 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7143 }
7144
7145 #if 0
7146 /* I think this assert is bogus if buffer contains
7147 invisible text or images. KFS. */
7148 xassert (IT_CHARPOS (*it) >= BEGV);
7149 #endif
7150 }
7151 }
7152 }
7153
7154
7155 /* Move IT by a specified amount of pixel lines DY. DY negative means
7156 move backwards. DY = 0 means move to start of screen line. At the
7157 end, IT will be on the start of a screen line. */
7158
7159 void
7160 move_it_vertically (it, dy)
7161 struct it *it;
7162 int dy;
7163 {
7164 if (dy <= 0)
7165 move_it_vertically_backward (it, -dy);
7166 else
7167 {
7168 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7169 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7170 MOVE_TO_POS | MOVE_TO_Y);
7171 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7172
7173 /* If buffer ends in ZV without a newline, move to the start of
7174 the line to satisfy the post-condition. */
7175 if (IT_CHARPOS (*it) == ZV
7176 && ZV > BEGV
7177 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7178 move_it_by_lines (it, 0, 0);
7179 }
7180 }
7181
7182
7183 /* Move iterator IT past the end of the text line it is in. */
7184
7185 void
7186 move_it_past_eol (it)
7187 struct it *it;
7188 {
7189 enum move_it_result rc;
7190
7191 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7192 if (rc == MOVE_NEWLINE_OR_CR)
7193 set_iterator_to_next (it, 0);
7194 }
7195
7196
7197 #if 0 /* Currently not used. */
7198
7199 /* Return non-zero if some text between buffer positions START_CHARPOS
7200 and END_CHARPOS is invisible. IT->window is the window for text
7201 property lookup. */
7202
7203 static int
7204 invisible_text_between_p (it, start_charpos, end_charpos)
7205 struct it *it;
7206 int start_charpos, end_charpos;
7207 {
7208 Lisp_Object prop, limit;
7209 int invisible_found_p;
7210
7211 xassert (it != NULL && start_charpos <= end_charpos);
7212
7213 /* Is text at START invisible? */
7214 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7215 it->window);
7216 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7217 invisible_found_p = 1;
7218 else
7219 {
7220 limit = Fnext_single_char_property_change (make_number (start_charpos),
7221 Qinvisible, Qnil,
7222 make_number (end_charpos));
7223 invisible_found_p = XFASTINT (limit) < end_charpos;
7224 }
7225
7226 return invisible_found_p;
7227 }
7228
7229 #endif /* 0 */
7230
7231
7232 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7233 negative means move up. DVPOS == 0 means move to the start of the
7234 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7235 NEED_Y_P is zero, IT->current_y will be left unchanged.
7236
7237 Further optimization ideas: If we would know that IT->f doesn't use
7238 a face with proportional font, we could be faster for
7239 truncate-lines nil. */
7240
7241 void
7242 move_it_by_lines (it, dvpos, need_y_p)
7243 struct it *it;
7244 int dvpos, need_y_p;
7245 {
7246 struct position pos;
7247
7248 if (!FRAME_WINDOW_P (it->f))
7249 {
7250 struct text_pos textpos;
7251
7252 /* We can use vmotion on frames without proportional fonts. */
7253 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7254 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7255 reseat (it, textpos, 1);
7256 it->vpos += pos.vpos;
7257 it->current_y += pos.vpos;
7258 }
7259 else if (dvpos == 0)
7260 {
7261 /* DVPOS == 0 means move to the start of the screen line. */
7262 move_it_vertically_backward (it, 0);
7263 xassert (it->current_x == 0 && it->hpos == 0);
7264 /* Let next call to line_bottom_y calculate real line height */
7265 last_height = 0;
7266 }
7267 else if (dvpos > 0)
7268 {
7269 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7270 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7271 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7272 }
7273 else
7274 {
7275 struct it it2;
7276 int start_charpos, i;
7277
7278 /* Start at the beginning of the screen line containing IT's
7279 position. This may actually move vertically backwards,
7280 in case of overlays, so adjust dvpos accordingly. */
7281 dvpos += it->vpos;
7282 move_it_vertically_backward (it, 0);
7283 dvpos -= it->vpos;
7284
7285 /* Go back -DVPOS visible lines and reseat the iterator there. */
7286 start_charpos = IT_CHARPOS (*it);
7287 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7288 back_to_previous_visible_line_start (it);
7289 reseat (it, it->current.pos, 1);
7290
7291 /* Move further back if we end up in a string or an image. */
7292 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7293 {
7294 /* First try to move to start of display line. */
7295 dvpos += it->vpos;
7296 move_it_vertically_backward (it, 0);
7297 dvpos -= it->vpos;
7298 if (IT_POS_VALID_AFTER_MOVE_P (it))
7299 break;
7300 /* If start of line is still in string or image,
7301 move further back. */
7302 back_to_previous_visible_line_start (it);
7303 reseat (it, it->current.pos, 1);
7304 dvpos--;
7305 }
7306
7307 it->current_x = it->hpos = 0;
7308
7309 /* Above call may have moved too far if continuation lines
7310 are involved. Scan forward and see if it did. */
7311 it2 = *it;
7312 it2.vpos = it2.current_y = 0;
7313 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7314 it->vpos -= it2.vpos;
7315 it->current_y -= it2.current_y;
7316 it->current_x = it->hpos = 0;
7317
7318 /* If we moved too far back, move IT some lines forward. */
7319 if (it2.vpos > -dvpos)
7320 {
7321 int delta = it2.vpos + dvpos;
7322 it2 = *it;
7323 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7324 /* Move back again if we got too far ahead. */
7325 if (IT_CHARPOS (*it) >= start_charpos)
7326 *it = it2;
7327 }
7328 }
7329 }
7330
7331 /* Return 1 if IT points into the middle of a display vector. */
7332
7333 int
7334 in_display_vector_p (it)
7335 struct it *it;
7336 {
7337 return (it->method == GET_FROM_DISPLAY_VECTOR
7338 && it->current.dpvec_index > 0
7339 && it->dpvec + it->current.dpvec_index != it->dpend);
7340 }
7341
7342 \f
7343 /***********************************************************************
7344 Messages
7345 ***********************************************************************/
7346
7347
7348 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7349 to *Messages*. */
7350
7351 void
7352 add_to_log (format, arg1, arg2)
7353 char *format;
7354 Lisp_Object arg1, arg2;
7355 {
7356 Lisp_Object args[3];
7357 Lisp_Object msg, fmt;
7358 char *buffer;
7359 int len;
7360 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7361 USE_SAFE_ALLOCA;
7362
7363 /* Do nothing if called asynchronously. Inserting text into
7364 a buffer may call after-change-functions and alike and
7365 that would means running Lisp asynchronously. */
7366 if (handling_signal)
7367 return;
7368
7369 fmt = msg = Qnil;
7370 GCPRO4 (fmt, msg, arg1, arg2);
7371
7372 args[0] = fmt = build_string (format);
7373 args[1] = arg1;
7374 args[2] = arg2;
7375 msg = Fformat (3, args);
7376
7377 len = SBYTES (msg) + 1;
7378 SAFE_ALLOCA (buffer, char *, len);
7379 bcopy (SDATA (msg), buffer, len);
7380
7381 message_dolog (buffer, len - 1, 1, 0);
7382 SAFE_FREE ();
7383
7384 UNGCPRO;
7385 }
7386
7387
7388 /* Output a newline in the *Messages* buffer if "needs" one. */
7389
7390 void
7391 message_log_maybe_newline ()
7392 {
7393 if (message_log_need_newline)
7394 message_dolog ("", 0, 1, 0);
7395 }
7396
7397
7398 /* Add a string M of length NBYTES to the message log, optionally
7399 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7400 nonzero, means interpret the contents of M as multibyte. This
7401 function calls low-level routines in order to bypass text property
7402 hooks, etc. which might not be safe to run.
7403
7404 This may GC (insert may run before/after change hooks),
7405 so the buffer M must NOT point to a Lisp string. */
7406
7407 void
7408 message_dolog (m, nbytes, nlflag, multibyte)
7409 const char *m;
7410 int nbytes, nlflag, multibyte;
7411 {
7412 if (!NILP (Vmemory_full))
7413 return;
7414
7415 if (!NILP (Vmessage_log_max))
7416 {
7417 struct buffer *oldbuf;
7418 Lisp_Object oldpoint, oldbegv, oldzv;
7419 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7420 int point_at_end = 0;
7421 int zv_at_end = 0;
7422 Lisp_Object old_deactivate_mark, tem;
7423 struct gcpro gcpro1;
7424
7425 old_deactivate_mark = Vdeactivate_mark;
7426 oldbuf = current_buffer;
7427 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7428 current_buffer->undo_list = Qt;
7429
7430 oldpoint = message_dolog_marker1;
7431 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7432 oldbegv = message_dolog_marker2;
7433 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7434 oldzv = message_dolog_marker3;
7435 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7436 GCPRO1 (old_deactivate_mark);
7437
7438 if (PT == Z)
7439 point_at_end = 1;
7440 if (ZV == Z)
7441 zv_at_end = 1;
7442
7443 BEGV = BEG;
7444 BEGV_BYTE = BEG_BYTE;
7445 ZV = Z;
7446 ZV_BYTE = Z_BYTE;
7447 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7448
7449 /* Insert the string--maybe converting multibyte to single byte
7450 or vice versa, so that all the text fits the buffer. */
7451 if (multibyte
7452 && NILP (current_buffer->enable_multibyte_characters))
7453 {
7454 int i, c, char_bytes;
7455 unsigned char work[1];
7456
7457 /* Convert a multibyte string to single-byte
7458 for the *Message* buffer. */
7459 for (i = 0; i < nbytes; i += char_bytes)
7460 {
7461 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7462 work[0] = (ASCII_CHAR_P (c)
7463 ? c
7464 : multibyte_char_to_unibyte (c, Qnil));
7465 insert_1_both (work, 1, 1, 1, 0, 0);
7466 }
7467 }
7468 else if (! multibyte
7469 && ! NILP (current_buffer->enable_multibyte_characters))
7470 {
7471 int i, c, char_bytes;
7472 unsigned char *msg = (unsigned char *) m;
7473 unsigned char str[MAX_MULTIBYTE_LENGTH];
7474 /* Convert a single-byte string to multibyte
7475 for the *Message* buffer. */
7476 for (i = 0; i < nbytes; i++)
7477 {
7478 c = msg[i];
7479 c = unibyte_char_to_multibyte (c);
7480 char_bytes = CHAR_STRING (c, str);
7481 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7482 }
7483 }
7484 else if (nbytes)
7485 insert_1 (m, nbytes, 1, 0, 0);
7486
7487 if (nlflag)
7488 {
7489 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7490 insert_1 ("\n", 1, 1, 0, 0);
7491
7492 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7493 this_bol = PT;
7494 this_bol_byte = PT_BYTE;
7495
7496 /* See if this line duplicates the previous one.
7497 If so, combine duplicates. */
7498 if (this_bol > BEG)
7499 {
7500 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7501 prev_bol = PT;
7502 prev_bol_byte = PT_BYTE;
7503
7504 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7505 this_bol, this_bol_byte);
7506 if (dup)
7507 {
7508 del_range_both (prev_bol, prev_bol_byte,
7509 this_bol, this_bol_byte, 0);
7510 if (dup > 1)
7511 {
7512 char dupstr[40];
7513 int duplen;
7514
7515 /* If you change this format, don't forget to also
7516 change message_log_check_duplicate. */
7517 sprintf (dupstr, " [%d times]", dup);
7518 duplen = strlen (dupstr);
7519 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7520 insert_1 (dupstr, duplen, 1, 0, 1);
7521 }
7522 }
7523 }
7524
7525 /* If we have more than the desired maximum number of lines
7526 in the *Messages* buffer now, delete the oldest ones.
7527 This is safe because we don't have undo in this buffer. */
7528
7529 if (NATNUMP (Vmessage_log_max))
7530 {
7531 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7532 -XFASTINT (Vmessage_log_max) - 1, 0);
7533 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7534 }
7535 }
7536 BEGV = XMARKER (oldbegv)->charpos;
7537 BEGV_BYTE = marker_byte_position (oldbegv);
7538
7539 if (zv_at_end)
7540 {
7541 ZV = Z;
7542 ZV_BYTE = Z_BYTE;
7543 }
7544 else
7545 {
7546 ZV = XMARKER (oldzv)->charpos;
7547 ZV_BYTE = marker_byte_position (oldzv);
7548 }
7549
7550 if (point_at_end)
7551 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7552 else
7553 /* We can't do Fgoto_char (oldpoint) because it will run some
7554 Lisp code. */
7555 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7556 XMARKER (oldpoint)->bytepos);
7557
7558 UNGCPRO;
7559 unchain_marker (XMARKER (oldpoint));
7560 unchain_marker (XMARKER (oldbegv));
7561 unchain_marker (XMARKER (oldzv));
7562
7563 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7564 set_buffer_internal (oldbuf);
7565 if (NILP (tem))
7566 windows_or_buffers_changed = old_windows_or_buffers_changed;
7567 message_log_need_newline = !nlflag;
7568 Vdeactivate_mark = old_deactivate_mark;
7569 }
7570 }
7571
7572
7573 /* We are at the end of the buffer after just having inserted a newline.
7574 (Note: We depend on the fact we won't be crossing the gap.)
7575 Check to see if the most recent message looks a lot like the previous one.
7576 Return 0 if different, 1 if the new one should just replace it, or a
7577 value N > 1 if we should also append " [N times]". */
7578
7579 static int
7580 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7581 int prev_bol, this_bol;
7582 int prev_bol_byte, this_bol_byte;
7583 {
7584 int i;
7585 int len = Z_BYTE - 1 - this_bol_byte;
7586 int seen_dots = 0;
7587 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7588 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7589
7590 for (i = 0; i < len; i++)
7591 {
7592 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7593 seen_dots = 1;
7594 if (p1[i] != p2[i])
7595 return seen_dots;
7596 }
7597 p1 += len;
7598 if (*p1 == '\n')
7599 return 2;
7600 if (*p1++ == ' ' && *p1++ == '[')
7601 {
7602 int n = 0;
7603 while (*p1 >= '0' && *p1 <= '9')
7604 n = n * 10 + *p1++ - '0';
7605 if (strncmp (p1, " times]\n", 8) == 0)
7606 return n+1;
7607 }
7608 return 0;
7609 }
7610 \f
7611
7612 /* Display an echo area message M with a specified length of NBYTES
7613 bytes. The string may include null characters. If M is 0, clear
7614 out any existing message, and let the mini-buffer text show
7615 through.
7616
7617 This may GC, so the buffer M must NOT point to a Lisp string. */
7618
7619 void
7620 message2 (m, nbytes, multibyte)
7621 const char *m;
7622 int nbytes;
7623 int multibyte;
7624 {
7625 /* First flush out any partial line written with print. */
7626 message_log_maybe_newline ();
7627 if (m)
7628 message_dolog (m, nbytes, 1, multibyte);
7629 message2_nolog (m, nbytes, multibyte);
7630 }
7631
7632
7633 /* The non-logging counterpart of message2. */
7634
7635 void
7636 message2_nolog (m, nbytes, multibyte)
7637 const char *m;
7638 int nbytes, multibyte;
7639 {
7640 struct frame *sf = SELECTED_FRAME ();
7641 message_enable_multibyte = multibyte;
7642
7643 if (noninteractive)
7644 {
7645 if (noninteractive_need_newline)
7646 putc ('\n', stderr);
7647 noninteractive_need_newline = 0;
7648 if (m)
7649 fwrite (m, nbytes, 1, stderr);
7650 if (cursor_in_echo_area == 0)
7651 fprintf (stderr, "\n");
7652 fflush (stderr);
7653 }
7654 /* A null message buffer means that the frame hasn't really been
7655 initialized yet. Error messages get reported properly by
7656 cmd_error, so this must be just an informative message; toss it. */
7657 else if (INTERACTIVE
7658 && sf->glyphs_initialized_p
7659 && FRAME_MESSAGE_BUF (sf))
7660 {
7661 Lisp_Object mini_window;
7662 struct frame *f;
7663
7664 /* Get the frame containing the mini-buffer
7665 that the selected frame is using. */
7666 mini_window = FRAME_MINIBUF_WINDOW (sf);
7667 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7668
7669 FRAME_SAMPLE_VISIBILITY (f);
7670 if (FRAME_VISIBLE_P (sf)
7671 && ! FRAME_VISIBLE_P (f))
7672 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7673
7674 if (m)
7675 {
7676 set_message (m, Qnil, nbytes, multibyte);
7677 if (minibuffer_auto_raise)
7678 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7679 }
7680 else
7681 clear_message (1, 1);
7682
7683 do_pending_window_change (0);
7684 echo_area_display (1);
7685 do_pending_window_change (0);
7686 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7687 (*frame_up_to_date_hook) (f);
7688 }
7689 }
7690
7691
7692 /* Display an echo area message M with a specified length of NBYTES
7693 bytes. The string may include null characters. If M is not a
7694 string, clear out any existing message, and let the mini-buffer
7695 text show through.
7696
7697 This function cancels echoing. */
7698
7699 void
7700 message3 (m, nbytes, multibyte)
7701 Lisp_Object m;
7702 int nbytes;
7703 int multibyte;
7704 {
7705 struct gcpro gcpro1;
7706
7707 GCPRO1 (m);
7708 clear_message (1,1);
7709 cancel_echoing ();
7710
7711 /* First flush out any partial line written with print. */
7712 message_log_maybe_newline ();
7713 if (STRINGP (m))
7714 {
7715 char *buffer;
7716 USE_SAFE_ALLOCA;
7717
7718 SAFE_ALLOCA (buffer, char *, nbytes);
7719 bcopy (SDATA (m), buffer, nbytes);
7720 message_dolog (buffer, nbytes, 1, multibyte);
7721 SAFE_FREE ();
7722 }
7723 message3_nolog (m, nbytes, multibyte);
7724
7725 UNGCPRO;
7726 }
7727
7728
7729 /* The non-logging version of message3.
7730 This does not cancel echoing, because it is used for echoing.
7731 Perhaps we need to make a separate function for echoing
7732 and make this cancel echoing. */
7733
7734 void
7735 message3_nolog (m, nbytes, multibyte)
7736 Lisp_Object m;
7737 int nbytes, multibyte;
7738 {
7739 struct frame *sf = SELECTED_FRAME ();
7740 message_enable_multibyte = multibyte;
7741
7742 if (noninteractive)
7743 {
7744 if (noninteractive_need_newline)
7745 putc ('\n', stderr);
7746 noninteractive_need_newline = 0;
7747 if (STRINGP (m))
7748 fwrite (SDATA (m), nbytes, 1, stderr);
7749 if (cursor_in_echo_area == 0)
7750 fprintf (stderr, "\n");
7751 fflush (stderr);
7752 }
7753 /* A null message buffer means that the frame hasn't really been
7754 initialized yet. Error messages get reported properly by
7755 cmd_error, so this must be just an informative message; toss it. */
7756 else if (INTERACTIVE
7757 && sf->glyphs_initialized_p
7758 && FRAME_MESSAGE_BUF (sf))
7759 {
7760 Lisp_Object mini_window;
7761 Lisp_Object frame;
7762 struct frame *f;
7763
7764 /* Get the frame containing the mini-buffer
7765 that the selected frame is using. */
7766 mini_window = FRAME_MINIBUF_WINDOW (sf);
7767 frame = XWINDOW (mini_window)->frame;
7768 f = XFRAME (frame);
7769
7770 FRAME_SAMPLE_VISIBILITY (f);
7771 if (FRAME_VISIBLE_P (sf)
7772 && !FRAME_VISIBLE_P (f))
7773 Fmake_frame_visible (frame);
7774
7775 if (STRINGP (m) && SCHARS (m) > 0)
7776 {
7777 set_message (NULL, m, nbytes, multibyte);
7778 if (minibuffer_auto_raise)
7779 Fraise_frame (frame);
7780 /* Assume we are not echoing.
7781 (If we are, echo_now will override this.) */
7782 echo_message_buffer = Qnil;
7783 }
7784 else
7785 clear_message (1, 1);
7786
7787 do_pending_window_change (0);
7788 echo_area_display (1);
7789 do_pending_window_change (0);
7790 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7791 (*frame_up_to_date_hook) (f);
7792 }
7793 }
7794
7795
7796 /* Display a null-terminated echo area message M. If M is 0, clear
7797 out any existing message, and let the mini-buffer text show through.
7798
7799 The buffer M must continue to exist until after the echo area gets
7800 cleared or some other message gets displayed there. Do not pass
7801 text that is stored in a Lisp string. Do not pass text in a buffer
7802 that was alloca'd. */
7803
7804 void
7805 message1 (m)
7806 char *m;
7807 {
7808 message2 (m, (m ? strlen (m) : 0), 0);
7809 }
7810
7811
7812 /* The non-logging counterpart of message1. */
7813
7814 void
7815 message1_nolog (m)
7816 char *m;
7817 {
7818 message2_nolog (m, (m ? strlen (m) : 0), 0);
7819 }
7820
7821 /* Display a message M which contains a single %s
7822 which gets replaced with STRING. */
7823
7824 void
7825 message_with_string (m, string, log)
7826 char *m;
7827 Lisp_Object string;
7828 int log;
7829 {
7830 CHECK_STRING (string);
7831
7832 if (noninteractive)
7833 {
7834 if (m)
7835 {
7836 if (noninteractive_need_newline)
7837 putc ('\n', stderr);
7838 noninteractive_need_newline = 0;
7839 fprintf (stderr, m, SDATA (string));
7840 if (cursor_in_echo_area == 0)
7841 fprintf (stderr, "\n");
7842 fflush (stderr);
7843 }
7844 }
7845 else if (INTERACTIVE)
7846 {
7847 /* The frame whose minibuffer we're going to display the message on.
7848 It may be larger than the selected frame, so we need
7849 to use its buffer, not the selected frame's buffer. */
7850 Lisp_Object mini_window;
7851 struct frame *f, *sf = SELECTED_FRAME ();
7852
7853 /* Get the frame containing the minibuffer
7854 that the selected frame is using. */
7855 mini_window = FRAME_MINIBUF_WINDOW (sf);
7856 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7857
7858 /* A null message buffer means that the frame hasn't really been
7859 initialized yet. Error messages get reported properly by
7860 cmd_error, so this must be just an informative message; toss it. */
7861 if (FRAME_MESSAGE_BUF (f))
7862 {
7863 Lisp_Object args[2], message;
7864 struct gcpro gcpro1, gcpro2;
7865
7866 args[0] = build_string (m);
7867 args[1] = message = string;
7868 GCPRO2 (args[0], message);
7869 gcpro1.nvars = 2;
7870
7871 message = Fformat (2, args);
7872
7873 if (log)
7874 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7875 else
7876 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7877
7878 UNGCPRO;
7879
7880 /* Print should start at the beginning of the message
7881 buffer next time. */
7882 message_buf_print = 0;
7883 }
7884 }
7885 }
7886
7887
7888 /* Dump an informative message to the minibuf. If M is 0, clear out
7889 any existing message, and let the mini-buffer text show through. */
7890
7891 /* VARARGS 1 */
7892 void
7893 message (m, a1, a2, a3)
7894 char *m;
7895 EMACS_INT a1, a2, a3;
7896 {
7897 if (noninteractive)
7898 {
7899 if (m)
7900 {
7901 if (noninteractive_need_newline)
7902 putc ('\n', stderr);
7903 noninteractive_need_newline = 0;
7904 fprintf (stderr, m, a1, a2, a3);
7905 if (cursor_in_echo_area == 0)
7906 fprintf (stderr, "\n");
7907 fflush (stderr);
7908 }
7909 }
7910 else if (INTERACTIVE)
7911 {
7912 /* The frame whose mini-buffer we're going to display the message
7913 on. It may be larger than the selected frame, so we need to
7914 use its buffer, not the selected frame's buffer. */
7915 Lisp_Object mini_window;
7916 struct frame *f, *sf = SELECTED_FRAME ();
7917
7918 /* Get the frame containing the mini-buffer
7919 that the selected frame is using. */
7920 mini_window = FRAME_MINIBUF_WINDOW (sf);
7921 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7922
7923 /* A null message buffer means that the frame hasn't really been
7924 initialized yet. Error messages get reported properly by
7925 cmd_error, so this must be just an informative message; toss
7926 it. */
7927 if (FRAME_MESSAGE_BUF (f))
7928 {
7929 if (m)
7930 {
7931 int len;
7932 #ifdef NO_ARG_ARRAY
7933 char *a[3];
7934 a[0] = (char *) a1;
7935 a[1] = (char *) a2;
7936 a[2] = (char *) a3;
7937
7938 len = doprnt (FRAME_MESSAGE_BUF (f),
7939 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7940 #else
7941 len = doprnt (FRAME_MESSAGE_BUF (f),
7942 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7943 (char **) &a1);
7944 #endif /* NO_ARG_ARRAY */
7945
7946 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7947 }
7948 else
7949 message1 (0);
7950
7951 /* Print should start at the beginning of the message
7952 buffer next time. */
7953 message_buf_print = 0;
7954 }
7955 }
7956 }
7957
7958
7959 /* The non-logging version of message. */
7960
7961 void
7962 message_nolog (m, a1, a2, a3)
7963 char *m;
7964 EMACS_INT a1, a2, a3;
7965 {
7966 Lisp_Object old_log_max;
7967 old_log_max = Vmessage_log_max;
7968 Vmessage_log_max = Qnil;
7969 message (m, a1, a2, a3);
7970 Vmessage_log_max = old_log_max;
7971 }
7972
7973
7974 /* Display the current message in the current mini-buffer. This is
7975 only called from error handlers in process.c, and is not time
7976 critical. */
7977
7978 void
7979 update_echo_area ()
7980 {
7981 if (!NILP (echo_area_buffer[0]))
7982 {
7983 Lisp_Object string;
7984 string = Fcurrent_message ();
7985 message3 (string, SBYTES (string),
7986 !NILP (current_buffer->enable_multibyte_characters));
7987 }
7988 }
7989
7990
7991 /* Make sure echo area buffers in `echo_buffers' are live.
7992 If they aren't, make new ones. */
7993
7994 static void
7995 ensure_echo_area_buffers ()
7996 {
7997 int i;
7998
7999 for (i = 0; i < 2; ++i)
8000 if (!BUFFERP (echo_buffer[i])
8001 || NILP (XBUFFER (echo_buffer[i])->name))
8002 {
8003 char name[30];
8004 Lisp_Object old_buffer;
8005 int j;
8006
8007 old_buffer = echo_buffer[i];
8008 sprintf (name, " *Echo Area %d*", i);
8009 echo_buffer[i] = Fget_buffer_create (build_string (name));
8010 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8011
8012 for (j = 0; j < 2; ++j)
8013 if (EQ (old_buffer, echo_area_buffer[j]))
8014 echo_area_buffer[j] = echo_buffer[i];
8015 }
8016 }
8017
8018
8019 /* Call FN with args A1..A4 with either the current or last displayed
8020 echo_area_buffer as current buffer.
8021
8022 WHICH zero means use the current message buffer
8023 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8024 from echo_buffer[] and clear it.
8025
8026 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8027 suitable buffer from echo_buffer[] and clear it.
8028
8029 Value is what FN returns. */
8030
8031 static int
8032 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8033 struct window *w;
8034 int which;
8035 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8036 EMACS_INT a1;
8037 Lisp_Object a2;
8038 EMACS_INT a3, a4;
8039 {
8040 Lisp_Object buffer;
8041 int this_one, the_other, clear_buffer_p, rc;
8042 int count = SPECPDL_INDEX ();
8043
8044 /* If buffers aren't live, make new ones. */
8045 ensure_echo_area_buffers ();
8046
8047 clear_buffer_p = 0;
8048
8049 if (which == 0)
8050 this_one = 0, the_other = 1;
8051 else if (which > 0)
8052 this_one = 1, the_other = 0;
8053
8054 /* Choose a suitable buffer from echo_buffer[] is we don't
8055 have one. */
8056 if (NILP (echo_area_buffer[this_one]))
8057 {
8058 echo_area_buffer[this_one]
8059 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8060 ? echo_buffer[the_other]
8061 : echo_buffer[this_one]);
8062 clear_buffer_p = 1;
8063 }
8064
8065 buffer = echo_area_buffer[this_one];
8066
8067 /* Don't get confused by reusing the buffer used for echoing
8068 for a different purpose. */
8069 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8070 cancel_echoing ();
8071
8072 record_unwind_protect (unwind_with_echo_area_buffer,
8073 with_echo_area_buffer_unwind_data (w));
8074
8075 /* Make the echo area buffer current. Note that for display
8076 purposes, it is not necessary that the displayed window's buffer
8077 == current_buffer, except for text property lookup. So, let's
8078 only set that buffer temporarily here without doing a full
8079 Fset_window_buffer. We must also change w->pointm, though,
8080 because otherwise an assertions in unshow_buffer fails, and Emacs
8081 aborts. */
8082 set_buffer_internal_1 (XBUFFER (buffer));
8083 if (w)
8084 {
8085 w->buffer = buffer;
8086 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8087 }
8088
8089 current_buffer->undo_list = Qt;
8090 current_buffer->read_only = Qnil;
8091 specbind (Qinhibit_read_only, Qt);
8092 specbind (Qinhibit_modification_hooks, Qt);
8093
8094 if (clear_buffer_p && Z > BEG)
8095 del_range (BEG, Z);
8096
8097 xassert (BEGV >= BEG);
8098 xassert (ZV <= Z && ZV >= BEGV);
8099
8100 rc = fn (a1, a2, a3, a4);
8101
8102 xassert (BEGV >= BEG);
8103 xassert (ZV <= Z && ZV >= BEGV);
8104
8105 unbind_to (count, Qnil);
8106 return rc;
8107 }
8108
8109
8110 /* Save state that should be preserved around the call to the function
8111 FN called in with_echo_area_buffer. */
8112
8113 static Lisp_Object
8114 with_echo_area_buffer_unwind_data (w)
8115 struct window *w;
8116 {
8117 int i = 0;
8118 Lisp_Object vector;
8119
8120 /* Reduce consing by keeping one vector in
8121 Vwith_echo_area_save_vector. */
8122 vector = Vwith_echo_area_save_vector;
8123 Vwith_echo_area_save_vector = Qnil;
8124
8125 if (NILP (vector))
8126 vector = Fmake_vector (make_number (7), Qnil);
8127
8128 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8129 AREF (vector, i) = Vdeactivate_mark, ++i;
8130 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8131
8132 if (w)
8133 {
8134 XSETWINDOW (AREF (vector, i), w); ++i;
8135 AREF (vector, i) = w->buffer; ++i;
8136 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8137 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8138 }
8139 else
8140 {
8141 int end = i + 4;
8142 for (; i < end; ++i)
8143 AREF (vector, i) = Qnil;
8144 }
8145
8146 xassert (i == ASIZE (vector));
8147 return vector;
8148 }
8149
8150
8151 /* Restore global state from VECTOR which was created by
8152 with_echo_area_buffer_unwind_data. */
8153
8154 static Lisp_Object
8155 unwind_with_echo_area_buffer (vector)
8156 Lisp_Object vector;
8157 {
8158 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8159 Vdeactivate_mark = AREF (vector, 1);
8160 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8161
8162 if (WINDOWP (AREF (vector, 3)))
8163 {
8164 struct window *w;
8165 Lisp_Object buffer, charpos, bytepos;
8166
8167 w = XWINDOW (AREF (vector, 3));
8168 buffer = AREF (vector, 4);
8169 charpos = AREF (vector, 5);
8170 bytepos = AREF (vector, 6);
8171
8172 w->buffer = buffer;
8173 set_marker_both (w->pointm, buffer,
8174 XFASTINT (charpos), XFASTINT (bytepos));
8175 }
8176
8177 Vwith_echo_area_save_vector = vector;
8178 return Qnil;
8179 }
8180
8181
8182 /* Set up the echo area for use by print functions. MULTIBYTE_P
8183 non-zero means we will print multibyte. */
8184
8185 void
8186 setup_echo_area_for_printing (multibyte_p)
8187 int multibyte_p;
8188 {
8189 /* If we can't find an echo area any more, exit. */
8190 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8191 Fkill_emacs (Qnil);
8192
8193 ensure_echo_area_buffers ();
8194
8195 if (!message_buf_print)
8196 {
8197 /* A message has been output since the last time we printed.
8198 Choose a fresh echo area buffer. */
8199 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8200 echo_area_buffer[0] = echo_buffer[1];
8201 else
8202 echo_area_buffer[0] = echo_buffer[0];
8203
8204 /* Switch to that buffer and clear it. */
8205 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8206 current_buffer->truncate_lines = Qnil;
8207
8208 if (Z > BEG)
8209 {
8210 int count = SPECPDL_INDEX ();
8211 specbind (Qinhibit_read_only, Qt);
8212 /* Note that undo recording is always disabled. */
8213 del_range (BEG, Z);
8214 unbind_to (count, Qnil);
8215 }
8216 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8217
8218 /* Set up the buffer for the multibyteness we need. */
8219 if (multibyte_p
8220 != !NILP (current_buffer->enable_multibyte_characters))
8221 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8222
8223 /* Raise the frame containing the echo area. */
8224 if (minibuffer_auto_raise)
8225 {
8226 struct frame *sf = SELECTED_FRAME ();
8227 Lisp_Object mini_window;
8228 mini_window = FRAME_MINIBUF_WINDOW (sf);
8229 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8230 }
8231
8232 message_log_maybe_newline ();
8233 message_buf_print = 1;
8234 }
8235 else
8236 {
8237 if (NILP (echo_area_buffer[0]))
8238 {
8239 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8240 echo_area_buffer[0] = echo_buffer[1];
8241 else
8242 echo_area_buffer[0] = echo_buffer[0];
8243 }
8244
8245 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8246 {
8247 /* Someone switched buffers between print requests. */
8248 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8249 current_buffer->truncate_lines = Qnil;
8250 }
8251 }
8252 }
8253
8254
8255 /* Display an echo area message in window W. Value is non-zero if W's
8256 height is changed. If display_last_displayed_message_p is
8257 non-zero, display the message that was last displayed, otherwise
8258 display the current message. */
8259
8260 static int
8261 display_echo_area (w)
8262 struct window *w;
8263 {
8264 int i, no_message_p, window_height_changed_p, count;
8265
8266 /* Temporarily disable garbage collections while displaying the echo
8267 area. This is done because a GC can print a message itself.
8268 That message would modify the echo area buffer's contents while a
8269 redisplay of the buffer is going on, and seriously confuse
8270 redisplay. */
8271 count = inhibit_garbage_collection ();
8272
8273 /* If there is no message, we must call display_echo_area_1
8274 nevertheless because it resizes the window. But we will have to
8275 reset the echo_area_buffer in question to nil at the end because
8276 with_echo_area_buffer will sets it to an empty buffer. */
8277 i = display_last_displayed_message_p ? 1 : 0;
8278 no_message_p = NILP (echo_area_buffer[i]);
8279
8280 window_height_changed_p
8281 = with_echo_area_buffer (w, display_last_displayed_message_p,
8282 display_echo_area_1,
8283 (EMACS_INT) w, Qnil, 0, 0);
8284
8285 if (no_message_p)
8286 echo_area_buffer[i] = Qnil;
8287
8288 unbind_to (count, Qnil);
8289 return window_height_changed_p;
8290 }
8291
8292
8293 /* Helper for display_echo_area. Display the current buffer which
8294 contains the current echo area message in window W, a mini-window,
8295 a pointer to which is passed in A1. A2..A4 are currently not used.
8296 Change the height of W so that all of the message is displayed.
8297 Value is non-zero if height of W was changed. */
8298
8299 static int
8300 display_echo_area_1 (a1, a2, a3, a4)
8301 EMACS_INT a1;
8302 Lisp_Object a2;
8303 EMACS_INT a3, a4;
8304 {
8305 struct window *w = (struct window *) a1;
8306 Lisp_Object window;
8307 struct text_pos start;
8308 int window_height_changed_p = 0;
8309
8310 /* Do this before displaying, so that we have a large enough glyph
8311 matrix for the display. If we can't get enough space for the
8312 whole text, display the last N lines. That works by setting w->start. */
8313 window_height_changed_p = resize_mini_window (w, 0);
8314
8315 /* Use the starting position chosen by resize_mini_window. */
8316 SET_TEXT_POS_FROM_MARKER (start, w->start);
8317
8318 /* Display. */
8319 clear_glyph_matrix (w->desired_matrix);
8320 XSETWINDOW (window, w);
8321 try_window (window, start, 0);
8322
8323 return window_height_changed_p;
8324 }
8325
8326
8327 /* Resize the echo area window to exactly the size needed for the
8328 currently displayed message, if there is one. If a mini-buffer
8329 is active, don't shrink it. */
8330
8331 void
8332 resize_echo_area_exactly ()
8333 {
8334 if (BUFFERP (echo_area_buffer[0])
8335 && WINDOWP (echo_area_window))
8336 {
8337 struct window *w = XWINDOW (echo_area_window);
8338 int resized_p;
8339 Lisp_Object resize_exactly;
8340
8341 if (minibuf_level == 0)
8342 resize_exactly = Qt;
8343 else
8344 resize_exactly = Qnil;
8345
8346 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8347 (EMACS_INT) w, resize_exactly, 0, 0);
8348 if (resized_p)
8349 {
8350 ++windows_or_buffers_changed;
8351 ++update_mode_lines;
8352 redisplay_internal (0);
8353 }
8354 }
8355 }
8356
8357
8358 /* Callback function for with_echo_area_buffer, when used from
8359 resize_echo_area_exactly. A1 contains a pointer to the window to
8360 resize, EXACTLY non-nil means resize the mini-window exactly to the
8361 size of the text displayed. A3 and A4 are not used. Value is what
8362 resize_mini_window returns. */
8363
8364 static int
8365 resize_mini_window_1 (a1, exactly, a3, a4)
8366 EMACS_INT a1;
8367 Lisp_Object exactly;
8368 EMACS_INT a3, a4;
8369 {
8370 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8371 }
8372
8373
8374 /* Resize mini-window W to fit the size of its contents. EXACT:P
8375 means size the window exactly to the size needed. Otherwise, it's
8376 only enlarged until W's buffer is empty.
8377
8378 Set W->start to the right place to begin display. If the whole
8379 contents fit, start at the beginning. Otherwise, start so as
8380 to make the end of the contents appear. This is particularly
8381 important for y-or-n-p, but seems desirable generally.
8382
8383 Value is non-zero if the window height has been changed. */
8384
8385 int
8386 resize_mini_window (w, exact_p)
8387 struct window *w;
8388 int exact_p;
8389 {
8390 struct frame *f = XFRAME (w->frame);
8391 int window_height_changed_p = 0;
8392
8393 xassert (MINI_WINDOW_P (w));
8394
8395 /* By default, start display at the beginning. */
8396 set_marker_both (w->start, w->buffer,
8397 BUF_BEGV (XBUFFER (w->buffer)),
8398 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8399
8400 /* Don't resize windows while redisplaying a window; it would
8401 confuse redisplay functions when the size of the window they are
8402 displaying changes from under them. Such a resizing can happen,
8403 for instance, when which-func prints a long message while
8404 we are running fontification-functions. We're running these
8405 functions with safe_call which binds inhibit-redisplay to t. */
8406 if (!NILP (Vinhibit_redisplay))
8407 return 0;
8408
8409 /* Nil means don't try to resize. */
8410 if (NILP (Vresize_mini_windows)
8411 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8412 return 0;
8413
8414 if (!FRAME_MINIBUF_ONLY_P (f))
8415 {
8416 struct it it;
8417 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8418 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8419 int height, max_height;
8420 int unit = FRAME_LINE_HEIGHT (f);
8421 struct text_pos start;
8422 struct buffer *old_current_buffer = NULL;
8423
8424 if (current_buffer != XBUFFER (w->buffer))
8425 {
8426 old_current_buffer = current_buffer;
8427 set_buffer_internal (XBUFFER (w->buffer));
8428 }
8429
8430 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8431
8432 /* Compute the max. number of lines specified by the user. */
8433 if (FLOATP (Vmax_mini_window_height))
8434 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8435 else if (INTEGERP (Vmax_mini_window_height))
8436 max_height = XINT (Vmax_mini_window_height);
8437 else
8438 max_height = total_height / 4;
8439
8440 /* Correct that max. height if it's bogus. */
8441 max_height = max (1, max_height);
8442 max_height = min (total_height, max_height);
8443
8444 /* Find out the height of the text in the window. */
8445 if (it.truncate_lines_p)
8446 height = 1;
8447 else
8448 {
8449 last_height = 0;
8450 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8451 if (it.max_ascent == 0 && it.max_descent == 0)
8452 height = it.current_y + last_height;
8453 else
8454 height = it.current_y + it.max_ascent + it.max_descent;
8455 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8456 height = (height + unit - 1) / unit;
8457 }
8458
8459 /* Compute a suitable window start. */
8460 if (height > max_height)
8461 {
8462 height = max_height;
8463 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8464 move_it_vertically_backward (&it, (height - 1) * unit);
8465 start = it.current.pos;
8466 }
8467 else
8468 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8469 SET_MARKER_FROM_TEXT_POS (w->start, start);
8470
8471 if (EQ (Vresize_mini_windows, Qgrow_only))
8472 {
8473 /* Let it grow only, until we display an empty message, in which
8474 case the window shrinks again. */
8475 if (height > WINDOW_TOTAL_LINES (w))
8476 {
8477 int old_height = WINDOW_TOTAL_LINES (w);
8478 freeze_window_starts (f, 1);
8479 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8480 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8481 }
8482 else if (height < WINDOW_TOTAL_LINES (w)
8483 && (exact_p || BEGV == ZV))
8484 {
8485 int old_height = WINDOW_TOTAL_LINES (w);
8486 freeze_window_starts (f, 0);
8487 shrink_mini_window (w);
8488 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8489 }
8490 }
8491 else
8492 {
8493 /* Always resize to exact size needed. */
8494 if (height > WINDOW_TOTAL_LINES (w))
8495 {
8496 int old_height = WINDOW_TOTAL_LINES (w);
8497 freeze_window_starts (f, 1);
8498 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8499 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8500 }
8501 else if (height < WINDOW_TOTAL_LINES (w))
8502 {
8503 int old_height = WINDOW_TOTAL_LINES (w);
8504 freeze_window_starts (f, 0);
8505 shrink_mini_window (w);
8506
8507 if (height)
8508 {
8509 freeze_window_starts (f, 1);
8510 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8511 }
8512
8513 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8514 }
8515 }
8516
8517 if (old_current_buffer)
8518 set_buffer_internal (old_current_buffer);
8519 }
8520
8521 return window_height_changed_p;
8522 }
8523
8524
8525 /* Value is the current message, a string, or nil if there is no
8526 current message. */
8527
8528 Lisp_Object
8529 current_message ()
8530 {
8531 Lisp_Object msg;
8532
8533 if (NILP (echo_area_buffer[0]))
8534 msg = Qnil;
8535 else
8536 {
8537 with_echo_area_buffer (0, 0, current_message_1,
8538 (EMACS_INT) &msg, Qnil, 0, 0);
8539 if (NILP (msg))
8540 echo_area_buffer[0] = Qnil;
8541 }
8542
8543 return msg;
8544 }
8545
8546
8547 static int
8548 current_message_1 (a1, a2, a3, a4)
8549 EMACS_INT a1;
8550 Lisp_Object a2;
8551 EMACS_INT a3, a4;
8552 {
8553 Lisp_Object *msg = (Lisp_Object *) a1;
8554
8555 if (Z > BEG)
8556 *msg = make_buffer_string (BEG, Z, 1);
8557 else
8558 *msg = Qnil;
8559 return 0;
8560 }
8561
8562
8563 /* Push the current message on Vmessage_stack for later restauration
8564 by restore_message. Value is non-zero if the current message isn't
8565 empty. This is a relatively infrequent operation, so it's not
8566 worth optimizing. */
8567
8568 int
8569 push_message ()
8570 {
8571 Lisp_Object msg;
8572 msg = current_message ();
8573 Vmessage_stack = Fcons (msg, Vmessage_stack);
8574 return STRINGP (msg);
8575 }
8576
8577
8578 /* Restore message display from the top of Vmessage_stack. */
8579
8580 void
8581 restore_message ()
8582 {
8583 Lisp_Object msg;
8584
8585 xassert (CONSP (Vmessage_stack));
8586 msg = XCAR (Vmessage_stack);
8587 if (STRINGP (msg))
8588 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8589 else
8590 message3_nolog (msg, 0, 0);
8591 }
8592
8593
8594 /* Handler for record_unwind_protect calling pop_message. */
8595
8596 Lisp_Object
8597 pop_message_unwind (dummy)
8598 Lisp_Object dummy;
8599 {
8600 pop_message ();
8601 return Qnil;
8602 }
8603
8604 /* Pop the top-most entry off Vmessage_stack. */
8605
8606 void
8607 pop_message ()
8608 {
8609 xassert (CONSP (Vmessage_stack));
8610 Vmessage_stack = XCDR (Vmessage_stack);
8611 }
8612
8613
8614 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8615 exits. If the stack is not empty, we have a missing pop_message
8616 somewhere. */
8617
8618 void
8619 check_message_stack ()
8620 {
8621 if (!NILP (Vmessage_stack))
8622 abort ();
8623 }
8624
8625
8626 /* Truncate to NCHARS what will be displayed in the echo area the next
8627 time we display it---but don't redisplay it now. */
8628
8629 void
8630 truncate_echo_area (nchars)
8631 int nchars;
8632 {
8633 if (nchars == 0)
8634 echo_area_buffer[0] = Qnil;
8635 /* A null message buffer means that the frame hasn't really been
8636 initialized yet. Error messages get reported properly by
8637 cmd_error, so this must be just an informative message; toss it. */
8638 else if (!noninteractive
8639 && INTERACTIVE
8640 && !NILP (echo_area_buffer[0]))
8641 {
8642 struct frame *sf = SELECTED_FRAME ();
8643 if (FRAME_MESSAGE_BUF (sf))
8644 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8645 }
8646 }
8647
8648
8649 /* Helper function for truncate_echo_area. Truncate the current
8650 message to at most NCHARS characters. */
8651
8652 static int
8653 truncate_message_1 (nchars, a2, a3, a4)
8654 EMACS_INT nchars;
8655 Lisp_Object a2;
8656 EMACS_INT a3, a4;
8657 {
8658 if (BEG + nchars < Z)
8659 del_range (BEG + nchars, Z);
8660 if (Z == BEG)
8661 echo_area_buffer[0] = Qnil;
8662 return 0;
8663 }
8664
8665
8666 /* Set the current message to a substring of S or STRING.
8667
8668 If STRING is a Lisp string, set the message to the first NBYTES
8669 bytes from STRING. NBYTES zero means use the whole string. If
8670 STRING is multibyte, the message will be displayed multibyte.
8671
8672 If S is not null, set the message to the first LEN bytes of S. LEN
8673 zero means use the whole string. MULTIBYTE_P non-zero means S is
8674 multibyte. Display the message multibyte in that case.
8675
8676 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8677 to t before calling set_message_1 (which calls insert).
8678 */
8679
8680 void
8681 set_message (s, string, nbytes, multibyte_p)
8682 const char *s;
8683 Lisp_Object string;
8684 int nbytes, multibyte_p;
8685 {
8686 message_enable_multibyte
8687 = ((s && multibyte_p)
8688 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8689
8690 with_echo_area_buffer (0, 0, set_message_1,
8691 (EMACS_INT) s, string, nbytes, multibyte_p);
8692 message_buf_print = 0;
8693 help_echo_showing_p = 0;
8694 }
8695
8696
8697 /* Helper function for set_message. Arguments have the same meaning
8698 as there, with A1 corresponding to S and A2 corresponding to STRING
8699 This function is called with the echo area buffer being
8700 current. */
8701
8702 static int
8703 set_message_1 (a1, a2, nbytes, multibyte_p)
8704 EMACS_INT a1;
8705 Lisp_Object a2;
8706 EMACS_INT nbytes, multibyte_p;
8707 {
8708 const char *s = (const char *) a1;
8709 Lisp_Object string = a2;
8710
8711 /* Change multibyteness of the echo buffer appropriately. */
8712 if (message_enable_multibyte
8713 != !NILP (current_buffer->enable_multibyte_characters))
8714 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8715
8716 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8717
8718 /* Insert new message at BEG. */
8719 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8720 Ferase_buffer ();
8721
8722 if (STRINGP (string))
8723 {
8724 int nchars;
8725
8726 if (nbytes == 0)
8727 nbytes = SBYTES (string);
8728 nchars = string_byte_to_char (string, nbytes);
8729
8730 /* This function takes care of single/multibyte conversion. We
8731 just have to ensure that the echo area buffer has the right
8732 setting of enable_multibyte_characters. */
8733 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8734 }
8735 else if (s)
8736 {
8737 if (nbytes == 0)
8738 nbytes = strlen (s);
8739
8740 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8741 {
8742 /* Convert from multi-byte to single-byte. */
8743 int i, c, n;
8744 unsigned char work[1];
8745
8746 /* Convert a multibyte string to single-byte. */
8747 for (i = 0; i < nbytes; i += n)
8748 {
8749 c = string_char_and_length (s + i, nbytes - i, &n);
8750 work[0] = (ASCII_CHAR_P (c)
8751 ? c
8752 : multibyte_char_to_unibyte (c, Qnil));
8753 insert_1_both (work, 1, 1, 1, 0, 0);
8754 }
8755 }
8756 else if (!multibyte_p
8757 && !NILP (current_buffer->enable_multibyte_characters))
8758 {
8759 /* Convert from single-byte to multi-byte. */
8760 int i, c, n;
8761 const unsigned char *msg = (const unsigned char *) s;
8762 unsigned char str[MAX_MULTIBYTE_LENGTH];
8763
8764 /* Convert a single-byte string to multibyte. */
8765 for (i = 0; i < nbytes; i++)
8766 {
8767 c = msg[i];
8768 c = unibyte_char_to_multibyte (c);
8769 n = CHAR_STRING (c, str);
8770 insert_1_both (str, 1, n, 1, 0, 0);
8771 }
8772 }
8773 else
8774 insert_1 (s, nbytes, 1, 0, 0);
8775 }
8776
8777 return 0;
8778 }
8779
8780
8781 /* Clear messages. CURRENT_P non-zero means clear the current
8782 message. LAST_DISPLAYED_P non-zero means clear the message
8783 last displayed. */
8784
8785 void
8786 clear_message (current_p, last_displayed_p)
8787 int current_p, last_displayed_p;
8788 {
8789 if (current_p)
8790 {
8791 echo_area_buffer[0] = Qnil;
8792 message_cleared_p = 1;
8793 }
8794
8795 if (last_displayed_p)
8796 echo_area_buffer[1] = Qnil;
8797
8798 message_buf_print = 0;
8799 }
8800
8801 /* Clear garbaged frames.
8802
8803 This function is used where the old redisplay called
8804 redraw_garbaged_frames which in turn called redraw_frame which in
8805 turn called clear_frame. The call to clear_frame was a source of
8806 flickering. I believe a clear_frame is not necessary. It should
8807 suffice in the new redisplay to invalidate all current matrices,
8808 and ensure a complete redisplay of all windows. */
8809
8810 static void
8811 clear_garbaged_frames ()
8812 {
8813 if (frame_garbaged)
8814 {
8815 Lisp_Object tail, frame;
8816 int changed_count = 0;
8817
8818 FOR_EACH_FRAME (tail, frame)
8819 {
8820 struct frame *f = XFRAME (frame);
8821
8822 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8823 {
8824 if (f->resized_p)
8825 {
8826 Fredraw_frame (frame);
8827 f->force_flush_display_p = 1;
8828 }
8829 clear_current_matrices (f);
8830 changed_count++;
8831 f->garbaged = 0;
8832 f->resized_p = 0;
8833 }
8834 }
8835
8836 frame_garbaged = 0;
8837 if (changed_count)
8838 ++windows_or_buffers_changed;
8839 }
8840 }
8841
8842
8843 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8844 is non-zero update selected_frame. Value is non-zero if the
8845 mini-windows height has been changed. */
8846
8847 static int
8848 echo_area_display (update_frame_p)
8849 int update_frame_p;
8850 {
8851 Lisp_Object mini_window;
8852 struct window *w;
8853 struct frame *f;
8854 int window_height_changed_p = 0;
8855 struct frame *sf = SELECTED_FRAME ();
8856
8857 mini_window = FRAME_MINIBUF_WINDOW (sf);
8858 w = XWINDOW (mini_window);
8859 f = XFRAME (WINDOW_FRAME (w));
8860
8861 /* Don't display if frame is invisible or not yet initialized. */
8862 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8863 return 0;
8864
8865 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8866 #ifndef MAC_OS8
8867 #ifdef HAVE_WINDOW_SYSTEM
8868 /* When Emacs starts, selected_frame may be a visible terminal
8869 frame, even if we run under a window system. If we let this
8870 through, a message would be displayed on the terminal. */
8871 if (EQ (selected_frame, Vterminal_frame)
8872 && !NILP (Vwindow_system))
8873 return 0;
8874 #endif /* HAVE_WINDOW_SYSTEM */
8875 #endif
8876
8877 /* Redraw garbaged frames. */
8878 if (frame_garbaged)
8879 clear_garbaged_frames ();
8880
8881 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8882 {
8883 echo_area_window = mini_window;
8884 window_height_changed_p = display_echo_area (w);
8885 w->must_be_updated_p = 1;
8886
8887 /* Update the display, unless called from redisplay_internal.
8888 Also don't update the screen during redisplay itself. The
8889 update will happen at the end of redisplay, and an update
8890 here could cause confusion. */
8891 if (update_frame_p && !redisplaying_p)
8892 {
8893 int n = 0;
8894
8895 /* If the display update has been interrupted by pending
8896 input, update mode lines in the frame. Due to the
8897 pending input, it might have been that redisplay hasn't
8898 been called, so that mode lines above the echo area are
8899 garbaged. This looks odd, so we prevent it here. */
8900 if (!display_completed)
8901 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8902
8903 if (window_height_changed_p
8904 /* Don't do this if Emacs is shutting down. Redisplay
8905 needs to run hooks. */
8906 && !NILP (Vrun_hooks))
8907 {
8908 /* Must update other windows. Likewise as in other
8909 cases, don't let this update be interrupted by
8910 pending input. */
8911 int count = SPECPDL_INDEX ();
8912 specbind (Qredisplay_dont_pause, Qt);
8913 windows_or_buffers_changed = 1;
8914 redisplay_internal (0);
8915 unbind_to (count, Qnil);
8916 }
8917 else if (FRAME_WINDOW_P (f) && n == 0)
8918 {
8919 /* Window configuration is the same as before.
8920 Can do with a display update of the echo area,
8921 unless we displayed some mode lines. */
8922 update_single_window (w, 1);
8923 rif->flush_display (f);
8924 }
8925 else
8926 update_frame (f, 1, 1);
8927
8928 /* If cursor is in the echo area, make sure that the next
8929 redisplay displays the minibuffer, so that the cursor will
8930 be replaced with what the minibuffer wants. */
8931 if (cursor_in_echo_area)
8932 ++windows_or_buffers_changed;
8933 }
8934 }
8935 else if (!EQ (mini_window, selected_window))
8936 windows_or_buffers_changed++;
8937
8938 /* The current message is now also the last one displayed. */
8939 echo_area_buffer[1] = echo_area_buffer[0];
8940
8941 /* Prevent redisplay optimization in redisplay_internal by resetting
8942 this_line_start_pos. This is done because the mini-buffer now
8943 displays the message instead of its buffer text. */
8944 if (EQ (mini_window, selected_window))
8945 CHARPOS (this_line_start_pos) = 0;
8946
8947 return window_height_changed_p;
8948 }
8949
8950
8951 \f
8952 /***********************************************************************
8953 Mode Lines and Frame Titles
8954 ***********************************************************************/
8955
8956 /* A buffer for constructing non-propertized mode-line strings and
8957 frame titles in it; allocated from the heap in init_xdisp and
8958 resized as needed in store_mode_line_noprop_char. */
8959
8960 static char *mode_line_noprop_buf;
8961
8962 /* The buffer's end, and a current output position in it. */
8963
8964 static char *mode_line_noprop_buf_end;
8965 static char *mode_line_noprop_ptr;
8966
8967 #define MODE_LINE_NOPROP_LEN(start) \
8968 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8969
8970 static enum {
8971 MODE_LINE_DISPLAY = 0,
8972 MODE_LINE_TITLE,
8973 MODE_LINE_NOPROP,
8974 MODE_LINE_STRING
8975 } mode_line_target;
8976
8977 /* Alist that caches the results of :propertize.
8978 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8979 static Lisp_Object mode_line_proptrans_alist;
8980
8981 /* List of strings making up the mode-line. */
8982 static Lisp_Object mode_line_string_list;
8983
8984 /* Base face property when building propertized mode line string. */
8985 static Lisp_Object mode_line_string_face;
8986 static Lisp_Object mode_line_string_face_prop;
8987
8988
8989 /* Unwind data for mode line strings */
8990
8991 static Lisp_Object Vmode_line_unwind_vector;
8992
8993 static Lisp_Object
8994 format_mode_line_unwind_data (obuf, save_proptrans)
8995 struct buffer *obuf;
8996 {
8997 Lisp_Object vector;
8998
8999 /* Reduce consing by keeping one vector in
9000 Vwith_echo_area_save_vector. */
9001 vector = Vmode_line_unwind_vector;
9002 Vmode_line_unwind_vector = Qnil;
9003
9004 if (NILP (vector))
9005 vector = Fmake_vector (make_number (7), Qnil);
9006
9007 AREF (vector, 0) = make_number (mode_line_target);
9008 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
9009 AREF (vector, 2) = mode_line_string_list;
9010 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
9011 AREF (vector, 4) = mode_line_string_face;
9012 AREF (vector, 5) = mode_line_string_face_prop;
9013
9014 if (obuf)
9015 XSETBUFFER (AREF (vector, 6), obuf);
9016 else
9017 AREF (vector, 6) = Qnil;
9018
9019 return vector;
9020 }
9021
9022 static Lisp_Object
9023 unwind_format_mode_line (vector)
9024 Lisp_Object vector;
9025 {
9026 mode_line_target = XINT (AREF (vector, 0));
9027 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9028 mode_line_string_list = AREF (vector, 2);
9029 if (! EQ (AREF (vector, 3), Qt))
9030 mode_line_proptrans_alist = AREF (vector, 3);
9031 mode_line_string_face = AREF (vector, 4);
9032 mode_line_string_face_prop = AREF (vector, 5);
9033
9034 if (!NILP (AREF (vector, 6)))
9035 {
9036 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9037 AREF (vector, 6) = Qnil;
9038 }
9039
9040 Vmode_line_unwind_vector = vector;
9041 return Qnil;
9042 }
9043
9044
9045 /* Store a single character C for the frame title in mode_line_noprop_buf.
9046 Re-allocate mode_line_noprop_buf if necessary. */
9047
9048 static void
9049 #ifdef PROTOTYPES
9050 store_mode_line_noprop_char (char c)
9051 #else
9052 store_mode_line_noprop_char (c)
9053 char c;
9054 #endif
9055 {
9056 /* If output position has reached the end of the allocated buffer,
9057 double the buffer's size. */
9058 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9059 {
9060 int len = MODE_LINE_NOPROP_LEN (0);
9061 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9062 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9063 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9064 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9065 }
9066
9067 *mode_line_noprop_ptr++ = c;
9068 }
9069
9070
9071 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9072 mode_line_noprop_ptr. STR is the string to store. Do not copy
9073 characters that yield more columns than PRECISION; PRECISION <= 0
9074 means copy the whole string. Pad with spaces until FIELD_WIDTH
9075 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9076 pad. Called from display_mode_element when it is used to build a
9077 frame title. */
9078
9079 static int
9080 store_mode_line_noprop (str, field_width, precision)
9081 const unsigned char *str;
9082 int field_width, precision;
9083 {
9084 int n = 0;
9085 int dummy, nbytes;
9086
9087 /* Copy at most PRECISION chars from STR. */
9088 nbytes = strlen (str);
9089 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9090 while (nbytes--)
9091 store_mode_line_noprop_char (*str++);
9092
9093 /* Fill up with spaces until FIELD_WIDTH reached. */
9094 while (field_width > 0
9095 && n < field_width)
9096 {
9097 store_mode_line_noprop_char (' ');
9098 ++n;
9099 }
9100
9101 return n;
9102 }
9103
9104 /***********************************************************************
9105 Frame Titles
9106 ***********************************************************************/
9107
9108 #ifdef HAVE_WINDOW_SYSTEM
9109
9110 /* Set the title of FRAME, if it has changed. The title format is
9111 Vicon_title_format if FRAME is iconified, otherwise it is
9112 frame_title_format. */
9113
9114 static void
9115 x_consider_frame_title (frame)
9116 Lisp_Object frame;
9117 {
9118 struct frame *f = XFRAME (frame);
9119
9120 if (FRAME_WINDOW_P (f)
9121 || FRAME_MINIBUF_ONLY_P (f)
9122 || f->explicit_name)
9123 {
9124 /* Do we have more than one visible frame on this X display? */
9125 Lisp_Object tail;
9126 Lisp_Object fmt;
9127 int title_start;
9128 char *title;
9129 int len;
9130 struct it it;
9131 int count = SPECPDL_INDEX ();
9132
9133 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9134 {
9135 Lisp_Object other_frame = XCAR (tail);
9136 struct frame *tf = XFRAME (other_frame);
9137
9138 if (tf != f
9139 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9140 && !FRAME_MINIBUF_ONLY_P (tf)
9141 && !EQ (other_frame, tip_frame)
9142 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9143 break;
9144 }
9145
9146 /* Set global variable indicating that multiple frames exist. */
9147 multiple_frames = CONSP (tail);
9148
9149 /* Switch to the buffer of selected window of the frame. Set up
9150 mode_line_target so that display_mode_element will output into
9151 mode_line_noprop_buf; then display the title. */
9152 record_unwind_protect (unwind_format_mode_line,
9153 format_mode_line_unwind_data (current_buffer, 0));
9154
9155 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9156 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9157
9158 mode_line_target = MODE_LINE_TITLE;
9159 title_start = MODE_LINE_NOPROP_LEN (0);
9160 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9161 NULL, DEFAULT_FACE_ID);
9162 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9163 len = MODE_LINE_NOPROP_LEN (title_start);
9164 title = mode_line_noprop_buf + title_start;
9165 unbind_to (count, Qnil);
9166
9167 /* Set the title only if it's changed. This avoids consing in
9168 the common case where it hasn't. (If it turns out that we've
9169 already wasted too much time by walking through the list with
9170 display_mode_element, then we might need to optimize at a
9171 higher level than this.) */
9172 if (! STRINGP (f->name)
9173 || SBYTES (f->name) != len
9174 || bcmp (title, SDATA (f->name), len) != 0)
9175 x_implicitly_set_name (f, make_string (title, len), Qnil);
9176 }
9177 }
9178
9179 #endif /* not HAVE_WINDOW_SYSTEM */
9180
9181
9182
9183 \f
9184 /***********************************************************************
9185 Menu Bars
9186 ***********************************************************************/
9187
9188
9189 /* Prepare for redisplay by updating menu-bar item lists when
9190 appropriate. This can call eval. */
9191
9192 void
9193 prepare_menu_bars ()
9194 {
9195 int all_windows;
9196 struct gcpro gcpro1, gcpro2;
9197 struct frame *f;
9198 Lisp_Object tooltip_frame;
9199
9200 #ifdef HAVE_WINDOW_SYSTEM
9201 tooltip_frame = tip_frame;
9202 #else
9203 tooltip_frame = Qnil;
9204 #endif
9205
9206 /* Update all frame titles based on their buffer names, etc. We do
9207 this before the menu bars so that the buffer-menu will show the
9208 up-to-date frame titles. */
9209 #ifdef HAVE_WINDOW_SYSTEM
9210 if (windows_or_buffers_changed || update_mode_lines)
9211 {
9212 Lisp_Object tail, frame;
9213
9214 FOR_EACH_FRAME (tail, frame)
9215 {
9216 f = XFRAME (frame);
9217 if (!EQ (frame, tooltip_frame)
9218 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9219 x_consider_frame_title (frame);
9220 }
9221 }
9222 #endif /* HAVE_WINDOW_SYSTEM */
9223
9224 /* Update the menu bar item lists, if appropriate. This has to be
9225 done before any actual redisplay or generation of display lines. */
9226 all_windows = (update_mode_lines
9227 || buffer_shared > 1
9228 || windows_or_buffers_changed);
9229 if (all_windows)
9230 {
9231 Lisp_Object tail, frame;
9232 int count = SPECPDL_INDEX ();
9233 /* 1 means that update_menu_bar has run its hooks
9234 so any further calls to update_menu_bar shouldn't do so again. */
9235 int menu_bar_hooks_run = 0;
9236
9237 record_unwind_save_match_data ();
9238
9239 FOR_EACH_FRAME (tail, frame)
9240 {
9241 f = XFRAME (frame);
9242
9243 /* Ignore tooltip frame. */
9244 if (EQ (frame, tooltip_frame))
9245 continue;
9246
9247 /* If a window on this frame changed size, report that to
9248 the user and clear the size-change flag. */
9249 if (FRAME_WINDOW_SIZES_CHANGED (f))
9250 {
9251 Lisp_Object functions;
9252
9253 /* Clear flag first in case we get an error below. */
9254 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9255 functions = Vwindow_size_change_functions;
9256 GCPRO2 (tail, functions);
9257
9258 while (CONSP (functions))
9259 {
9260 call1 (XCAR (functions), frame);
9261 functions = XCDR (functions);
9262 }
9263 UNGCPRO;
9264 }
9265
9266 GCPRO1 (tail);
9267 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9268 #ifdef HAVE_WINDOW_SYSTEM
9269 update_tool_bar (f, 0);
9270 #ifdef MAC_OS
9271 mac_update_title_bar (f, 0);
9272 #endif
9273 #endif
9274 UNGCPRO;
9275 }
9276
9277 unbind_to (count, Qnil);
9278 }
9279 else
9280 {
9281 struct frame *sf = SELECTED_FRAME ();
9282 update_menu_bar (sf, 1, 0);
9283 #ifdef HAVE_WINDOW_SYSTEM
9284 update_tool_bar (sf, 1);
9285 #ifdef MAC_OS
9286 mac_update_title_bar (sf, 1);
9287 #endif
9288 #endif
9289 }
9290
9291 /* Motif needs this. See comment in xmenu.c. Turn it off when
9292 pending_menu_activation is not defined. */
9293 #ifdef USE_X_TOOLKIT
9294 pending_menu_activation = 0;
9295 #endif
9296 }
9297
9298
9299 /* Update the menu bar item list for frame F. This has to be done
9300 before we start to fill in any display lines, because it can call
9301 eval.
9302
9303 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9304
9305 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9306 already ran the menu bar hooks for this redisplay, so there
9307 is no need to run them again. The return value is the
9308 updated value of this flag, to pass to the next call. */
9309
9310 static int
9311 update_menu_bar (f, save_match_data, hooks_run)
9312 struct frame *f;
9313 int save_match_data;
9314 int hooks_run;
9315 {
9316 Lisp_Object window;
9317 register struct window *w;
9318
9319 /* If called recursively during a menu update, do nothing. This can
9320 happen when, for instance, an activate-menubar-hook causes a
9321 redisplay. */
9322 if (inhibit_menubar_update)
9323 return hooks_run;
9324
9325 window = FRAME_SELECTED_WINDOW (f);
9326 w = XWINDOW (window);
9327
9328 #if 0 /* The if statement below this if statement used to include the
9329 condition !NILP (w->update_mode_line), rather than using
9330 update_mode_lines directly, and this if statement may have
9331 been added to make that condition work. Now the if
9332 statement below matches its comment, this isn't needed. */
9333 if (update_mode_lines)
9334 w->update_mode_line = Qt;
9335 #endif
9336
9337 if (FRAME_WINDOW_P (f)
9338 ?
9339 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9340 || defined (USE_GTK)
9341 FRAME_EXTERNAL_MENU_BAR (f)
9342 #else
9343 FRAME_MENU_BAR_LINES (f) > 0
9344 #endif
9345 : FRAME_MENU_BAR_LINES (f) > 0)
9346 {
9347 /* If the user has switched buffers or windows, we need to
9348 recompute to reflect the new bindings. But we'll
9349 recompute when update_mode_lines is set too; that means
9350 that people can use force-mode-line-update to request
9351 that the menu bar be recomputed. The adverse effect on
9352 the rest of the redisplay algorithm is about the same as
9353 windows_or_buffers_changed anyway. */
9354 if (windows_or_buffers_changed
9355 /* This used to test w->update_mode_line, but we believe
9356 there is no need to recompute the menu in that case. */
9357 || update_mode_lines
9358 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9359 < BUF_MODIFF (XBUFFER (w->buffer)))
9360 != !NILP (w->last_had_star))
9361 || ((!NILP (Vtransient_mark_mode)
9362 && !NILP (XBUFFER (w->buffer)->mark_active))
9363 != !NILP (w->region_showing)))
9364 {
9365 struct buffer *prev = current_buffer;
9366 int count = SPECPDL_INDEX ();
9367
9368 specbind (Qinhibit_menubar_update, Qt);
9369
9370 set_buffer_internal_1 (XBUFFER (w->buffer));
9371 if (save_match_data)
9372 record_unwind_save_match_data ();
9373 if (NILP (Voverriding_local_map_menu_flag))
9374 {
9375 specbind (Qoverriding_terminal_local_map, Qnil);
9376 specbind (Qoverriding_local_map, Qnil);
9377 }
9378
9379 if (!hooks_run)
9380 {
9381 /* Run the Lucid hook. */
9382 safe_run_hooks (Qactivate_menubar_hook);
9383
9384 /* If it has changed current-menubar from previous value,
9385 really recompute the menu-bar from the value. */
9386 if (! NILP (Vlucid_menu_bar_dirty_flag))
9387 call0 (Qrecompute_lucid_menubar);
9388
9389 safe_run_hooks (Qmenu_bar_update_hook);
9390
9391 hooks_run = 1;
9392 }
9393
9394 XSETFRAME (Vmenu_updating_frame, f);
9395 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9396
9397 /* Redisplay the menu bar in case we changed it. */
9398 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9399 || defined (USE_GTK)
9400 if (FRAME_WINDOW_P (f))
9401 {
9402 #ifdef MAC_OS
9403 /* All frames on Mac OS share the same menubar. So only
9404 the selected frame should be allowed to set it. */
9405 if (f == SELECTED_FRAME ())
9406 #endif
9407 set_frame_menubar (f, 0, 0);
9408 }
9409 else
9410 /* On a terminal screen, the menu bar is an ordinary screen
9411 line, and this makes it get updated. */
9412 w->update_mode_line = Qt;
9413 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9414 /* In the non-toolkit version, the menu bar is an ordinary screen
9415 line, and this makes it get updated. */
9416 w->update_mode_line = Qt;
9417 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9418
9419 unbind_to (count, Qnil);
9420 set_buffer_internal_1 (prev);
9421 }
9422 }
9423
9424 return hooks_run;
9425 }
9426
9427
9428 \f
9429 /***********************************************************************
9430 Output Cursor
9431 ***********************************************************************/
9432
9433 #ifdef HAVE_WINDOW_SYSTEM
9434
9435 /* EXPORT:
9436 Nominal cursor position -- where to draw output.
9437 HPOS and VPOS are window relative glyph matrix coordinates.
9438 X and Y are window relative pixel coordinates. */
9439
9440 struct cursor_pos output_cursor;
9441
9442
9443 /* EXPORT:
9444 Set the global variable output_cursor to CURSOR. All cursor
9445 positions are relative to updated_window. */
9446
9447 void
9448 set_output_cursor (cursor)
9449 struct cursor_pos *cursor;
9450 {
9451 output_cursor.hpos = cursor->hpos;
9452 output_cursor.vpos = cursor->vpos;
9453 output_cursor.x = cursor->x;
9454 output_cursor.y = cursor->y;
9455 }
9456
9457
9458 /* EXPORT for RIF:
9459 Set a nominal cursor position.
9460
9461 HPOS and VPOS are column/row positions in a window glyph matrix. X
9462 and Y are window text area relative pixel positions.
9463
9464 If this is done during an update, updated_window will contain the
9465 window that is being updated and the position is the future output
9466 cursor position for that window. If updated_window is null, use
9467 selected_window and display the cursor at the given position. */
9468
9469 void
9470 x_cursor_to (vpos, hpos, y, x)
9471 int vpos, hpos, y, x;
9472 {
9473 struct window *w;
9474
9475 /* If updated_window is not set, work on selected_window. */
9476 if (updated_window)
9477 w = updated_window;
9478 else
9479 w = XWINDOW (selected_window);
9480
9481 /* Set the output cursor. */
9482 output_cursor.hpos = hpos;
9483 output_cursor.vpos = vpos;
9484 output_cursor.x = x;
9485 output_cursor.y = y;
9486
9487 /* If not called as part of an update, really display the cursor.
9488 This will also set the cursor position of W. */
9489 if (updated_window == NULL)
9490 {
9491 BLOCK_INPUT;
9492 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9493 if (rif->flush_display_optional)
9494 rif->flush_display_optional (SELECTED_FRAME ());
9495 UNBLOCK_INPUT;
9496 }
9497 }
9498
9499 #endif /* HAVE_WINDOW_SYSTEM */
9500
9501 \f
9502 /***********************************************************************
9503 Tool-bars
9504 ***********************************************************************/
9505
9506 #ifdef HAVE_WINDOW_SYSTEM
9507
9508 /* Where the mouse was last time we reported a mouse event. */
9509
9510 FRAME_PTR last_mouse_frame;
9511
9512 /* Tool-bar item index of the item on which a mouse button was pressed
9513 or -1. */
9514
9515 int last_tool_bar_item;
9516
9517
9518 /* Update the tool-bar item list for frame F. This has to be done
9519 before we start to fill in any display lines. Called from
9520 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9521 and restore it here. */
9522
9523 static void
9524 update_tool_bar (f, save_match_data)
9525 struct frame *f;
9526 int save_match_data;
9527 {
9528 #ifdef USE_GTK
9529 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9530 #else
9531 int do_update = WINDOWP (f->tool_bar_window)
9532 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9533 #endif
9534
9535 if (do_update)
9536 {
9537 Lisp_Object window;
9538 struct window *w;
9539
9540 window = FRAME_SELECTED_WINDOW (f);
9541 w = XWINDOW (window);
9542
9543 /* If the user has switched buffers or windows, we need to
9544 recompute to reflect the new bindings. But we'll
9545 recompute when update_mode_lines is set too; that means
9546 that people can use force-mode-line-update to request
9547 that the menu bar be recomputed. The adverse effect on
9548 the rest of the redisplay algorithm is about the same as
9549 windows_or_buffers_changed anyway. */
9550 if (windows_or_buffers_changed
9551 || !NILP (w->update_mode_line)
9552 || update_mode_lines
9553 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9554 < BUF_MODIFF (XBUFFER (w->buffer)))
9555 != !NILP (w->last_had_star))
9556 || ((!NILP (Vtransient_mark_mode)
9557 && !NILP (XBUFFER (w->buffer)->mark_active))
9558 != !NILP (w->region_showing)))
9559 {
9560 struct buffer *prev = current_buffer;
9561 int count = SPECPDL_INDEX ();
9562 Lisp_Object new_tool_bar;
9563 int new_n_tool_bar;
9564 struct gcpro gcpro1;
9565
9566 /* Set current_buffer to the buffer of the selected
9567 window of the frame, so that we get the right local
9568 keymaps. */
9569 set_buffer_internal_1 (XBUFFER (w->buffer));
9570
9571 /* Save match data, if we must. */
9572 if (save_match_data)
9573 record_unwind_save_match_data ();
9574
9575 /* Make sure that we don't accidentally use bogus keymaps. */
9576 if (NILP (Voverriding_local_map_menu_flag))
9577 {
9578 specbind (Qoverriding_terminal_local_map, Qnil);
9579 specbind (Qoverriding_local_map, Qnil);
9580 }
9581
9582 GCPRO1 (new_tool_bar);
9583
9584 /* Build desired tool-bar items from keymaps. */
9585 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9586 &new_n_tool_bar);
9587
9588 /* Redisplay the tool-bar if we changed it. */
9589 if (new_n_tool_bar != f->n_tool_bar_items
9590 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9591 {
9592 /* Redisplay that happens asynchronously due to an expose event
9593 may access f->tool_bar_items. Make sure we update both
9594 variables within BLOCK_INPUT so no such event interrupts. */
9595 BLOCK_INPUT;
9596 f->tool_bar_items = new_tool_bar;
9597 f->n_tool_bar_items = new_n_tool_bar;
9598 w->update_mode_line = Qt;
9599 UNBLOCK_INPUT;
9600 }
9601
9602 UNGCPRO;
9603
9604 unbind_to (count, Qnil);
9605 set_buffer_internal_1 (prev);
9606 }
9607 }
9608 }
9609
9610
9611 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9612 F's desired tool-bar contents. F->tool_bar_items must have
9613 been set up previously by calling prepare_menu_bars. */
9614
9615 static void
9616 build_desired_tool_bar_string (f)
9617 struct frame *f;
9618 {
9619 int i, size, size_needed;
9620 struct gcpro gcpro1, gcpro2, gcpro3;
9621 Lisp_Object image, plist, props;
9622
9623 image = plist = props = Qnil;
9624 GCPRO3 (image, plist, props);
9625
9626 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9627 Otherwise, make a new string. */
9628
9629 /* The size of the string we might be able to reuse. */
9630 size = (STRINGP (f->desired_tool_bar_string)
9631 ? SCHARS (f->desired_tool_bar_string)
9632 : 0);
9633
9634 /* We need one space in the string for each image. */
9635 size_needed = f->n_tool_bar_items;
9636
9637 /* Reuse f->desired_tool_bar_string, if possible. */
9638 if (size < size_needed || NILP (f->desired_tool_bar_string))
9639 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9640 make_number (' '));
9641 else
9642 {
9643 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9644 Fremove_text_properties (make_number (0), make_number (size),
9645 props, f->desired_tool_bar_string);
9646 }
9647
9648 /* Put a `display' property on the string for the images to display,
9649 put a `menu_item' property on tool-bar items with a value that
9650 is the index of the item in F's tool-bar item vector. */
9651 for (i = 0; i < f->n_tool_bar_items; ++i)
9652 {
9653 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9654
9655 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9656 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9657 int hmargin, vmargin, relief, idx, end;
9658 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9659
9660 /* If image is a vector, choose the image according to the
9661 button state. */
9662 image = PROP (TOOL_BAR_ITEM_IMAGES);
9663 if (VECTORP (image))
9664 {
9665 if (enabled_p)
9666 idx = (selected_p
9667 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9668 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9669 else
9670 idx = (selected_p
9671 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9672 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9673
9674 xassert (ASIZE (image) >= idx);
9675 image = AREF (image, idx);
9676 }
9677 else
9678 idx = -1;
9679
9680 /* Ignore invalid image specifications. */
9681 if (!valid_image_p (image))
9682 continue;
9683
9684 /* Display the tool-bar button pressed, or depressed. */
9685 plist = Fcopy_sequence (XCDR (image));
9686
9687 /* Compute margin and relief to draw. */
9688 relief = (tool_bar_button_relief >= 0
9689 ? tool_bar_button_relief
9690 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9691 hmargin = vmargin = relief;
9692
9693 if (INTEGERP (Vtool_bar_button_margin)
9694 && XINT (Vtool_bar_button_margin) > 0)
9695 {
9696 hmargin += XFASTINT (Vtool_bar_button_margin);
9697 vmargin += XFASTINT (Vtool_bar_button_margin);
9698 }
9699 else if (CONSP (Vtool_bar_button_margin))
9700 {
9701 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9702 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9703 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9704
9705 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9706 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9707 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9708 }
9709
9710 if (auto_raise_tool_bar_buttons_p)
9711 {
9712 /* Add a `:relief' property to the image spec if the item is
9713 selected. */
9714 if (selected_p)
9715 {
9716 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9717 hmargin -= relief;
9718 vmargin -= relief;
9719 }
9720 }
9721 else
9722 {
9723 /* If image is selected, display it pressed, i.e. with a
9724 negative relief. If it's not selected, display it with a
9725 raised relief. */
9726 plist = Fplist_put (plist, QCrelief,
9727 (selected_p
9728 ? make_number (-relief)
9729 : make_number (relief)));
9730 hmargin -= relief;
9731 vmargin -= relief;
9732 }
9733
9734 /* Put a margin around the image. */
9735 if (hmargin || vmargin)
9736 {
9737 if (hmargin == vmargin)
9738 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9739 else
9740 plist = Fplist_put (plist, QCmargin,
9741 Fcons (make_number (hmargin),
9742 make_number (vmargin)));
9743 }
9744
9745 /* If button is not enabled, and we don't have special images
9746 for the disabled state, make the image appear disabled by
9747 applying an appropriate algorithm to it. */
9748 if (!enabled_p && idx < 0)
9749 plist = Fplist_put (plist, QCconversion, Qdisabled);
9750
9751 /* Put a `display' text property on the string for the image to
9752 display. Put a `menu-item' property on the string that gives
9753 the start of this item's properties in the tool-bar items
9754 vector. */
9755 image = Fcons (Qimage, plist);
9756 props = list4 (Qdisplay, image,
9757 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9758
9759 /* Let the last image hide all remaining spaces in the tool bar
9760 string. The string can be longer than needed when we reuse a
9761 previous string. */
9762 if (i + 1 == f->n_tool_bar_items)
9763 end = SCHARS (f->desired_tool_bar_string);
9764 else
9765 end = i + 1;
9766 Fadd_text_properties (make_number (i), make_number (end),
9767 props, f->desired_tool_bar_string);
9768 #undef PROP
9769 }
9770
9771 UNGCPRO;
9772 }
9773
9774
9775 /* Display one line of the tool-bar of frame IT->f.
9776
9777 HEIGHT specifies the desired height of the tool-bar line.
9778 If the actual height of the glyph row is less than HEIGHT, the
9779 row's height is increased to HEIGHT, and the icons are centered
9780 vertically in the new height.
9781
9782 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9783 count a final empty row in case the tool-bar width exactly matches
9784 the window width.
9785 */
9786
9787 static void
9788 display_tool_bar_line (it, height)
9789 struct it *it;
9790 int height;
9791 {
9792 struct glyph_row *row = it->glyph_row;
9793 int max_x = it->last_visible_x;
9794 struct glyph *last;
9795
9796 prepare_desired_row (row);
9797 row->y = it->current_y;
9798
9799 /* Note that this isn't made use of if the face hasn't a box,
9800 so there's no need to check the face here. */
9801 it->start_of_box_run_p = 1;
9802
9803 while (it->current_x < max_x)
9804 {
9805 int x, n_glyphs_before, i, nglyphs;
9806 struct it it_before;
9807
9808 /* Get the next display element. */
9809 if (!get_next_display_element (it))
9810 {
9811 /* Don't count empty row if we are counting needed tool-bar lines. */
9812 if (height < 0 && !it->hpos)
9813 return;
9814 break;
9815 }
9816
9817 /* Produce glyphs. */
9818 n_glyphs_before = row->used[TEXT_AREA];
9819 it_before = *it;
9820
9821 PRODUCE_GLYPHS (it);
9822
9823 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9824 i = 0;
9825 x = it_before.current_x;
9826 while (i < nglyphs)
9827 {
9828 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9829
9830 if (x + glyph->pixel_width > max_x)
9831 {
9832 /* Glyph doesn't fit on line. Backtrack. */
9833 row->used[TEXT_AREA] = n_glyphs_before;
9834 *it = it_before;
9835 /* If this is the only glyph on this line, it will never fit on the
9836 toolbar, so skip it. But ensure there is at least one glyph,
9837 so we don't accidentally disable the tool-bar. */
9838 if (n_glyphs_before == 0
9839 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9840 break;
9841 goto out;
9842 }
9843
9844 ++it->hpos;
9845 x += glyph->pixel_width;
9846 ++i;
9847 }
9848
9849 /* Stop at line ends. */
9850 if (ITERATOR_AT_END_OF_LINE_P (it))
9851 break;
9852
9853 set_iterator_to_next (it, 1);
9854 }
9855
9856 out:;
9857
9858 row->displays_text_p = row->used[TEXT_AREA] != 0;
9859
9860 /* Use default face for the border below the tool bar.
9861
9862 FIXME: When auto-resize-tool-bars is grow-only, there is
9863 no additional border below the possibly empty tool-bar lines.
9864 So to make the extra empty lines look "normal", we have to
9865 use the tool-bar face for the border too. */
9866 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9867 it->face_id = DEFAULT_FACE_ID;
9868
9869 extend_face_to_end_of_line (it);
9870 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9871 last->right_box_line_p = 1;
9872 if (last == row->glyphs[TEXT_AREA])
9873 last->left_box_line_p = 1;
9874
9875 /* Make line the desired height and center it vertically. */
9876 if ((height -= it->max_ascent + it->max_descent) > 0)
9877 {
9878 /* Don't add more than one line height. */
9879 height %= FRAME_LINE_HEIGHT (it->f);
9880 it->max_ascent += height / 2;
9881 it->max_descent += (height + 1) / 2;
9882 }
9883
9884 compute_line_metrics (it);
9885
9886 /* If line is empty, make it occupy the rest of the tool-bar. */
9887 if (!row->displays_text_p)
9888 {
9889 row->height = row->phys_height = it->last_visible_y - row->y;
9890 row->visible_height = row->height;
9891 row->ascent = row->phys_ascent = 0;
9892 row->extra_line_spacing = 0;
9893 }
9894
9895 row->full_width_p = 1;
9896 row->continued_p = 0;
9897 row->truncated_on_left_p = 0;
9898 row->truncated_on_right_p = 0;
9899
9900 it->current_x = it->hpos = 0;
9901 it->current_y += row->height;
9902 ++it->vpos;
9903 ++it->glyph_row;
9904 }
9905
9906
9907 /* Max tool-bar height. */
9908
9909 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9910 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9911
9912 /* Value is the number of screen lines needed to make all tool-bar
9913 items of frame F visible. The number of actual rows needed is
9914 returned in *N_ROWS if non-NULL. */
9915
9916 static int
9917 tool_bar_lines_needed (f, n_rows)
9918 struct frame *f;
9919 int *n_rows;
9920 {
9921 struct window *w = XWINDOW (f->tool_bar_window);
9922 struct it it;
9923 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9924 the desired matrix, so use (unused) mode-line row as temporary row to
9925 avoid destroying the first tool-bar row. */
9926 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9927
9928 /* Initialize an iterator for iteration over
9929 F->desired_tool_bar_string in the tool-bar window of frame F. */
9930 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9931 it.first_visible_x = 0;
9932 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9933 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9934
9935 while (!ITERATOR_AT_END_P (&it))
9936 {
9937 clear_glyph_row (temp_row);
9938 it.glyph_row = temp_row;
9939 display_tool_bar_line (&it, -1);
9940 }
9941 clear_glyph_row (temp_row);
9942
9943 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9944 if (n_rows)
9945 *n_rows = it.vpos > 0 ? it.vpos : -1;
9946
9947 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9948 }
9949
9950
9951 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9952 0, 1, 0,
9953 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9954 (frame)
9955 Lisp_Object frame;
9956 {
9957 struct frame *f;
9958 struct window *w;
9959 int nlines = 0;
9960
9961 if (NILP (frame))
9962 frame = selected_frame;
9963 else
9964 CHECK_FRAME (frame);
9965 f = XFRAME (frame);
9966
9967 if (WINDOWP (f->tool_bar_window)
9968 || (w = XWINDOW (f->tool_bar_window),
9969 WINDOW_TOTAL_LINES (w) > 0))
9970 {
9971 update_tool_bar (f, 1);
9972 if (f->n_tool_bar_items)
9973 {
9974 build_desired_tool_bar_string (f);
9975 nlines = tool_bar_lines_needed (f, NULL);
9976 }
9977 }
9978
9979 return make_number (nlines);
9980 }
9981
9982
9983 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9984 height should be changed. */
9985
9986 static int
9987 redisplay_tool_bar (f)
9988 struct frame *f;
9989 {
9990 struct window *w;
9991 struct it it;
9992 struct glyph_row *row;
9993
9994 #ifdef USE_GTK
9995 if (FRAME_EXTERNAL_TOOL_BAR (f))
9996 update_frame_tool_bar (f);
9997 return 0;
9998 #endif
9999
10000 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10001 do anything. This means you must start with tool-bar-lines
10002 non-zero to get the auto-sizing effect. Or in other words, you
10003 can turn off tool-bars by specifying tool-bar-lines zero. */
10004 if (!WINDOWP (f->tool_bar_window)
10005 || (w = XWINDOW (f->tool_bar_window),
10006 WINDOW_TOTAL_LINES (w) == 0))
10007 return 0;
10008
10009 /* Set up an iterator for the tool-bar window. */
10010 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10011 it.first_visible_x = 0;
10012 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10013 row = it.glyph_row;
10014
10015 /* Build a string that represents the contents of the tool-bar. */
10016 build_desired_tool_bar_string (f);
10017 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10018
10019 if (f->n_tool_bar_rows == 0)
10020 {
10021 int nlines;
10022
10023 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10024 nlines != WINDOW_TOTAL_LINES (w)))
10025 {
10026 extern Lisp_Object Qtool_bar_lines;
10027 Lisp_Object frame;
10028 int old_height = WINDOW_TOTAL_LINES (w);
10029
10030 XSETFRAME (frame, f);
10031 Fmodify_frame_parameters (frame,
10032 Fcons (Fcons (Qtool_bar_lines,
10033 make_number (nlines)),
10034 Qnil));
10035 if (WINDOW_TOTAL_LINES (w) != old_height)
10036 {
10037 clear_glyph_matrix (w->desired_matrix);
10038 fonts_changed_p = 1;
10039 return 1;
10040 }
10041 }
10042 }
10043
10044 /* Display as many lines as needed to display all tool-bar items. */
10045
10046 if (f->n_tool_bar_rows > 0)
10047 {
10048 int border, rows, height, extra;
10049
10050 if (INTEGERP (Vtool_bar_border))
10051 border = XINT (Vtool_bar_border);
10052 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10053 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10054 else if (EQ (Vtool_bar_border, Qborder_width))
10055 border = f->border_width;
10056 else
10057 border = 0;
10058 if (border < 0)
10059 border = 0;
10060
10061 rows = f->n_tool_bar_rows;
10062 height = max (1, (it.last_visible_y - border) / rows);
10063 extra = it.last_visible_y - border - height * rows;
10064
10065 while (it.current_y < it.last_visible_y)
10066 {
10067 int h = 0;
10068 if (extra > 0 && rows-- > 0)
10069 {
10070 h = (extra + rows - 1) / rows;
10071 extra -= h;
10072 }
10073 display_tool_bar_line (&it, height + h);
10074 }
10075 }
10076 else
10077 {
10078 while (it.current_y < it.last_visible_y)
10079 display_tool_bar_line (&it, 0);
10080 }
10081
10082 /* It doesn't make much sense to try scrolling in the tool-bar
10083 window, so don't do it. */
10084 w->desired_matrix->no_scrolling_p = 1;
10085 w->must_be_updated_p = 1;
10086
10087 if (!NILP (Vauto_resize_tool_bars))
10088 {
10089 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10090 int change_height_p = 0;
10091
10092 /* If we couldn't display everything, change the tool-bar's
10093 height if there is room for more. */
10094 if (IT_STRING_CHARPOS (it) < it.end_charpos
10095 && it.current_y < max_tool_bar_height)
10096 change_height_p = 1;
10097
10098 row = it.glyph_row - 1;
10099
10100 /* If there are blank lines at the end, except for a partially
10101 visible blank line at the end that is smaller than
10102 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10103 if (!row->displays_text_p
10104 && row->height >= FRAME_LINE_HEIGHT (f))
10105 change_height_p = 1;
10106
10107 /* If row displays tool-bar items, but is partially visible,
10108 change the tool-bar's height. */
10109 if (row->displays_text_p
10110 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10111 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10112 change_height_p = 1;
10113
10114 /* Resize windows as needed by changing the `tool-bar-lines'
10115 frame parameter. */
10116 if (change_height_p)
10117 {
10118 extern Lisp_Object Qtool_bar_lines;
10119 Lisp_Object frame;
10120 int old_height = WINDOW_TOTAL_LINES (w);
10121 int nrows;
10122 int nlines = tool_bar_lines_needed (f, &nrows);
10123
10124 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10125 && !f->minimize_tool_bar_window_p)
10126 ? (nlines > old_height)
10127 : (nlines != old_height));
10128 f->minimize_tool_bar_window_p = 0;
10129
10130 if (change_height_p)
10131 {
10132 XSETFRAME (frame, f);
10133 Fmodify_frame_parameters (frame,
10134 Fcons (Fcons (Qtool_bar_lines,
10135 make_number (nlines)),
10136 Qnil));
10137 if (WINDOW_TOTAL_LINES (w) != old_height)
10138 {
10139 clear_glyph_matrix (w->desired_matrix);
10140 f->n_tool_bar_rows = nrows;
10141 fonts_changed_p = 1;
10142 return 1;
10143 }
10144 }
10145 }
10146 }
10147
10148 f->minimize_tool_bar_window_p = 0;
10149 return 0;
10150 }
10151
10152
10153 /* Get information about the tool-bar item which is displayed in GLYPH
10154 on frame F. Return in *PROP_IDX the index where tool-bar item
10155 properties start in F->tool_bar_items. Value is zero if
10156 GLYPH doesn't display a tool-bar item. */
10157
10158 static int
10159 tool_bar_item_info (f, glyph, prop_idx)
10160 struct frame *f;
10161 struct glyph *glyph;
10162 int *prop_idx;
10163 {
10164 Lisp_Object prop;
10165 int success_p;
10166 int charpos;
10167
10168 /* This function can be called asynchronously, which means we must
10169 exclude any possibility that Fget_text_property signals an
10170 error. */
10171 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10172 charpos = max (0, charpos);
10173
10174 /* Get the text property `menu-item' at pos. The value of that
10175 property is the start index of this item's properties in
10176 F->tool_bar_items. */
10177 prop = Fget_text_property (make_number (charpos),
10178 Qmenu_item, f->current_tool_bar_string);
10179 if (INTEGERP (prop))
10180 {
10181 *prop_idx = XINT (prop);
10182 success_p = 1;
10183 }
10184 else
10185 success_p = 0;
10186
10187 return success_p;
10188 }
10189
10190 \f
10191 /* Get information about the tool-bar item at position X/Y on frame F.
10192 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10193 the current matrix of the tool-bar window of F, or NULL if not
10194 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10195 item in F->tool_bar_items. Value is
10196
10197 -1 if X/Y is not on a tool-bar item
10198 0 if X/Y is on the same item that was highlighted before.
10199 1 otherwise. */
10200
10201 static int
10202 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10203 struct frame *f;
10204 int x, y;
10205 struct glyph **glyph;
10206 int *hpos, *vpos, *prop_idx;
10207 {
10208 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10209 struct window *w = XWINDOW (f->tool_bar_window);
10210 int area;
10211
10212 /* Find the glyph under X/Y. */
10213 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10214 if (*glyph == NULL)
10215 return -1;
10216
10217 /* Get the start of this tool-bar item's properties in
10218 f->tool_bar_items. */
10219 if (!tool_bar_item_info (f, *glyph, prop_idx))
10220 return -1;
10221
10222 /* Is mouse on the highlighted item? */
10223 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10224 && *vpos >= dpyinfo->mouse_face_beg_row
10225 && *vpos <= dpyinfo->mouse_face_end_row
10226 && (*vpos > dpyinfo->mouse_face_beg_row
10227 || *hpos >= dpyinfo->mouse_face_beg_col)
10228 && (*vpos < dpyinfo->mouse_face_end_row
10229 || *hpos < dpyinfo->mouse_face_end_col
10230 || dpyinfo->mouse_face_past_end))
10231 return 0;
10232
10233 return 1;
10234 }
10235
10236
10237 /* EXPORT:
10238 Handle mouse button event on the tool-bar of frame F, at
10239 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10240 0 for button release. MODIFIERS is event modifiers for button
10241 release. */
10242
10243 void
10244 handle_tool_bar_click (f, x, y, down_p, modifiers)
10245 struct frame *f;
10246 int x, y, down_p;
10247 unsigned int modifiers;
10248 {
10249 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10250 struct window *w = XWINDOW (f->tool_bar_window);
10251 int hpos, vpos, prop_idx;
10252 struct glyph *glyph;
10253 Lisp_Object enabled_p;
10254
10255 /* If not on the highlighted tool-bar item, return. */
10256 frame_to_window_pixel_xy (w, &x, &y);
10257 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10258 return;
10259
10260 /* If item is disabled, do nothing. */
10261 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10262 if (NILP (enabled_p))
10263 return;
10264
10265 if (down_p)
10266 {
10267 /* Show item in pressed state. */
10268 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10269 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10270 last_tool_bar_item = prop_idx;
10271 }
10272 else
10273 {
10274 Lisp_Object key, frame;
10275 struct input_event event;
10276 EVENT_INIT (event);
10277
10278 /* Show item in released state. */
10279 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10280 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10281
10282 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10283
10284 XSETFRAME (frame, f);
10285 event.kind = TOOL_BAR_EVENT;
10286 event.frame_or_window = frame;
10287 event.arg = frame;
10288 kbd_buffer_store_event (&event);
10289
10290 event.kind = TOOL_BAR_EVENT;
10291 event.frame_or_window = frame;
10292 event.arg = key;
10293 event.modifiers = modifiers;
10294 kbd_buffer_store_event (&event);
10295 last_tool_bar_item = -1;
10296 }
10297 }
10298
10299
10300 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10301 tool-bar window-relative coordinates X/Y. Called from
10302 note_mouse_highlight. */
10303
10304 static void
10305 note_tool_bar_highlight (f, x, y)
10306 struct frame *f;
10307 int x, y;
10308 {
10309 Lisp_Object window = f->tool_bar_window;
10310 struct window *w = XWINDOW (window);
10311 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10312 int hpos, vpos;
10313 struct glyph *glyph;
10314 struct glyph_row *row;
10315 int i;
10316 Lisp_Object enabled_p;
10317 int prop_idx;
10318 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10319 int mouse_down_p, rc;
10320
10321 /* Function note_mouse_highlight is called with negative x(y
10322 values when mouse moves outside of the frame. */
10323 if (x <= 0 || y <= 0)
10324 {
10325 clear_mouse_face (dpyinfo);
10326 return;
10327 }
10328
10329 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10330 if (rc < 0)
10331 {
10332 /* Not on tool-bar item. */
10333 clear_mouse_face (dpyinfo);
10334 return;
10335 }
10336 else if (rc == 0)
10337 /* On same tool-bar item as before. */
10338 goto set_help_echo;
10339
10340 clear_mouse_face (dpyinfo);
10341
10342 /* Mouse is down, but on different tool-bar item? */
10343 mouse_down_p = (dpyinfo->grabbed
10344 && f == last_mouse_frame
10345 && FRAME_LIVE_P (f));
10346 if (mouse_down_p
10347 && last_tool_bar_item != prop_idx)
10348 return;
10349
10350 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10351 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10352
10353 /* If tool-bar item is not enabled, don't highlight it. */
10354 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10355 if (!NILP (enabled_p))
10356 {
10357 /* Compute the x-position of the glyph. In front and past the
10358 image is a space. We include this in the highlighted area. */
10359 row = MATRIX_ROW (w->current_matrix, vpos);
10360 for (i = x = 0; i < hpos; ++i)
10361 x += row->glyphs[TEXT_AREA][i].pixel_width;
10362
10363 /* Record this as the current active region. */
10364 dpyinfo->mouse_face_beg_col = hpos;
10365 dpyinfo->mouse_face_beg_row = vpos;
10366 dpyinfo->mouse_face_beg_x = x;
10367 dpyinfo->mouse_face_beg_y = row->y;
10368 dpyinfo->mouse_face_past_end = 0;
10369
10370 dpyinfo->mouse_face_end_col = hpos + 1;
10371 dpyinfo->mouse_face_end_row = vpos;
10372 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10373 dpyinfo->mouse_face_end_y = row->y;
10374 dpyinfo->mouse_face_window = window;
10375 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10376
10377 /* Display it as active. */
10378 show_mouse_face (dpyinfo, draw);
10379 dpyinfo->mouse_face_image_state = draw;
10380 }
10381
10382 set_help_echo:
10383
10384 /* Set help_echo_string to a help string to display for this tool-bar item.
10385 XTread_socket does the rest. */
10386 help_echo_object = help_echo_window = Qnil;
10387 help_echo_pos = -1;
10388 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10389 if (NILP (help_echo_string))
10390 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10391 }
10392
10393 #endif /* HAVE_WINDOW_SYSTEM */
10394
10395
10396 \f
10397 /************************************************************************
10398 Horizontal scrolling
10399 ************************************************************************/
10400
10401 static int hscroll_window_tree P_ ((Lisp_Object));
10402 static int hscroll_windows P_ ((Lisp_Object));
10403
10404 /* For all leaf windows in the window tree rooted at WINDOW, set their
10405 hscroll value so that PT is (i) visible in the window, and (ii) so
10406 that it is not within a certain margin at the window's left and
10407 right border. Value is non-zero if any window's hscroll has been
10408 changed. */
10409
10410 static int
10411 hscroll_window_tree (window)
10412 Lisp_Object window;
10413 {
10414 int hscrolled_p = 0;
10415 int hscroll_relative_p = FLOATP (Vhscroll_step);
10416 int hscroll_step_abs = 0;
10417 double hscroll_step_rel = 0;
10418
10419 if (hscroll_relative_p)
10420 {
10421 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10422 if (hscroll_step_rel < 0)
10423 {
10424 hscroll_relative_p = 0;
10425 hscroll_step_abs = 0;
10426 }
10427 }
10428 else if (INTEGERP (Vhscroll_step))
10429 {
10430 hscroll_step_abs = XINT (Vhscroll_step);
10431 if (hscroll_step_abs < 0)
10432 hscroll_step_abs = 0;
10433 }
10434 else
10435 hscroll_step_abs = 0;
10436
10437 while (WINDOWP (window))
10438 {
10439 struct window *w = XWINDOW (window);
10440
10441 if (WINDOWP (w->hchild))
10442 hscrolled_p |= hscroll_window_tree (w->hchild);
10443 else if (WINDOWP (w->vchild))
10444 hscrolled_p |= hscroll_window_tree (w->vchild);
10445 else if (w->cursor.vpos >= 0)
10446 {
10447 int h_margin;
10448 int text_area_width;
10449 struct glyph_row *current_cursor_row
10450 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10451 struct glyph_row *desired_cursor_row
10452 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10453 struct glyph_row *cursor_row
10454 = (desired_cursor_row->enabled_p
10455 ? desired_cursor_row
10456 : current_cursor_row);
10457
10458 text_area_width = window_box_width (w, TEXT_AREA);
10459
10460 /* Scroll when cursor is inside this scroll margin. */
10461 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10462
10463 if ((XFASTINT (w->hscroll)
10464 && w->cursor.x <= h_margin)
10465 || (cursor_row->enabled_p
10466 && cursor_row->truncated_on_right_p
10467 && (w->cursor.x >= text_area_width - h_margin)))
10468 {
10469 struct it it;
10470 int hscroll;
10471 struct buffer *saved_current_buffer;
10472 int pt;
10473 int wanted_x;
10474
10475 /* Find point in a display of infinite width. */
10476 saved_current_buffer = current_buffer;
10477 current_buffer = XBUFFER (w->buffer);
10478
10479 if (w == XWINDOW (selected_window))
10480 pt = BUF_PT (current_buffer);
10481 else
10482 {
10483 pt = marker_position (w->pointm);
10484 pt = max (BEGV, pt);
10485 pt = min (ZV, pt);
10486 }
10487
10488 /* Move iterator to pt starting at cursor_row->start in
10489 a line with infinite width. */
10490 init_to_row_start (&it, w, cursor_row);
10491 it.last_visible_x = INFINITY;
10492 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10493 current_buffer = saved_current_buffer;
10494
10495 /* Position cursor in window. */
10496 if (!hscroll_relative_p && hscroll_step_abs == 0)
10497 hscroll = max (0, (it.current_x
10498 - (ITERATOR_AT_END_OF_LINE_P (&it)
10499 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10500 : (text_area_width / 2))))
10501 / FRAME_COLUMN_WIDTH (it.f);
10502 else if (w->cursor.x >= text_area_width - h_margin)
10503 {
10504 if (hscroll_relative_p)
10505 wanted_x = text_area_width * (1 - hscroll_step_rel)
10506 - h_margin;
10507 else
10508 wanted_x = text_area_width
10509 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10510 - h_margin;
10511 hscroll
10512 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10513 }
10514 else
10515 {
10516 if (hscroll_relative_p)
10517 wanted_x = text_area_width * hscroll_step_rel
10518 + h_margin;
10519 else
10520 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10521 + h_margin;
10522 hscroll
10523 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10524 }
10525 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10526
10527 /* Don't call Fset_window_hscroll if value hasn't
10528 changed because it will prevent redisplay
10529 optimizations. */
10530 if (XFASTINT (w->hscroll) != hscroll)
10531 {
10532 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10533 w->hscroll = make_number (hscroll);
10534 hscrolled_p = 1;
10535 }
10536 }
10537 }
10538
10539 window = w->next;
10540 }
10541
10542 /* Value is non-zero if hscroll of any leaf window has been changed. */
10543 return hscrolled_p;
10544 }
10545
10546
10547 /* Set hscroll so that cursor is visible and not inside horizontal
10548 scroll margins for all windows in the tree rooted at WINDOW. See
10549 also hscroll_window_tree above. Value is non-zero if any window's
10550 hscroll has been changed. If it has, desired matrices on the frame
10551 of WINDOW are cleared. */
10552
10553 static int
10554 hscroll_windows (window)
10555 Lisp_Object window;
10556 {
10557 int hscrolled_p;
10558
10559 if (automatic_hscrolling_p)
10560 {
10561 hscrolled_p = hscroll_window_tree (window);
10562 if (hscrolled_p)
10563 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10564 }
10565 else
10566 hscrolled_p = 0;
10567 return hscrolled_p;
10568 }
10569
10570
10571 \f
10572 /************************************************************************
10573 Redisplay
10574 ************************************************************************/
10575
10576 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10577 to a non-zero value. This is sometimes handy to have in a debugger
10578 session. */
10579
10580 #if GLYPH_DEBUG
10581
10582 /* First and last unchanged row for try_window_id. */
10583
10584 int debug_first_unchanged_at_end_vpos;
10585 int debug_last_unchanged_at_beg_vpos;
10586
10587 /* Delta vpos and y. */
10588
10589 int debug_dvpos, debug_dy;
10590
10591 /* Delta in characters and bytes for try_window_id. */
10592
10593 int debug_delta, debug_delta_bytes;
10594
10595 /* Values of window_end_pos and window_end_vpos at the end of
10596 try_window_id. */
10597
10598 EMACS_INT debug_end_pos, debug_end_vpos;
10599
10600 /* Append a string to W->desired_matrix->method. FMT is a printf
10601 format string. A1...A9 are a supplement for a variable-length
10602 argument list. If trace_redisplay_p is non-zero also printf the
10603 resulting string to stderr. */
10604
10605 static void
10606 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10607 struct window *w;
10608 char *fmt;
10609 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10610 {
10611 char buffer[512];
10612 char *method = w->desired_matrix->method;
10613 int len = strlen (method);
10614 int size = sizeof w->desired_matrix->method;
10615 int remaining = size - len - 1;
10616
10617 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10618 if (len && remaining)
10619 {
10620 method[len] = '|';
10621 --remaining, ++len;
10622 }
10623
10624 strncpy (method + len, buffer, remaining);
10625
10626 if (trace_redisplay_p)
10627 fprintf (stderr, "%p (%s): %s\n",
10628 w,
10629 ((BUFFERP (w->buffer)
10630 && STRINGP (XBUFFER (w->buffer)->name))
10631 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10632 : "no buffer"),
10633 buffer);
10634 }
10635
10636 #endif /* GLYPH_DEBUG */
10637
10638
10639 /* Value is non-zero if all changes in window W, which displays
10640 current_buffer, are in the text between START and END. START is a
10641 buffer position, END is given as a distance from Z. Used in
10642 redisplay_internal for display optimization. */
10643
10644 static INLINE int
10645 text_outside_line_unchanged_p (w, start, end)
10646 struct window *w;
10647 int start, end;
10648 {
10649 int unchanged_p = 1;
10650
10651 /* If text or overlays have changed, see where. */
10652 if (XFASTINT (w->last_modified) < MODIFF
10653 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10654 {
10655 /* Gap in the line? */
10656 if (GPT < start || Z - GPT < end)
10657 unchanged_p = 0;
10658
10659 /* Changes start in front of the line, or end after it? */
10660 if (unchanged_p
10661 && (BEG_UNCHANGED < start - 1
10662 || END_UNCHANGED < end))
10663 unchanged_p = 0;
10664
10665 /* If selective display, can't optimize if changes start at the
10666 beginning of the line. */
10667 if (unchanged_p
10668 && INTEGERP (current_buffer->selective_display)
10669 && XINT (current_buffer->selective_display) > 0
10670 && (BEG_UNCHANGED < start || GPT <= start))
10671 unchanged_p = 0;
10672
10673 /* If there are overlays at the start or end of the line, these
10674 may have overlay strings with newlines in them. A change at
10675 START, for instance, may actually concern the display of such
10676 overlay strings as well, and they are displayed on different
10677 lines. So, quickly rule out this case. (For the future, it
10678 might be desirable to implement something more telling than
10679 just BEG/END_UNCHANGED.) */
10680 if (unchanged_p)
10681 {
10682 if (BEG + BEG_UNCHANGED == start
10683 && overlay_touches_p (start))
10684 unchanged_p = 0;
10685 if (END_UNCHANGED == end
10686 && overlay_touches_p (Z - end))
10687 unchanged_p = 0;
10688 }
10689 }
10690
10691 return unchanged_p;
10692 }
10693
10694
10695 /* Do a frame update, taking possible shortcuts into account. This is
10696 the main external entry point for redisplay.
10697
10698 If the last redisplay displayed an echo area message and that message
10699 is no longer requested, we clear the echo area or bring back the
10700 mini-buffer if that is in use. */
10701
10702 void
10703 redisplay ()
10704 {
10705 redisplay_internal (0);
10706 }
10707
10708
10709 static Lisp_Object
10710 overlay_arrow_string_or_property (var)
10711 Lisp_Object var;
10712 {
10713 Lisp_Object val;
10714
10715 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10716 return val;
10717
10718 return Voverlay_arrow_string;
10719 }
10720
10721 /* Return 1 if there are any overlay-arrows in current_buffer. */
10722 static int
10723 overlay_arrow_in_current_buffer_p ()
10724 {
10725 Lisp_Object vlist;
10726
10727 for (vlist = Voverlay_arrow_variable_list;
10728 CONSP (vlist);
10729 vlist = XCDR (vlist))
10730 {
10731 Lisp_Object var = XCAR (vlist);
10732 Lisp_Object val;
10733
10734 if (!SYMBOLP (var))
10735 continue;
10736 val = find_symbol_value (var);
10737 if (MARKERP (val)
10738 && current_buffer == XMARKER (val)->buffer)
10739 return 1;
10740 }
10741 return 0;
10742 }
10743
10744
10745 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10746 has changed. */
10747
10748 static int
10749 overlay_arrows_changed_p ()
10750 {
10751 Lisp_Object vlist;
10752
10753 for (vlist = Voverlay_arrow_variable_list;
10754 CONSP (vlist);
10755 vlist = XCDR (vlist))
10756 {
10757 Lisp_Object var = XCAR (vlist);
10758 Lisp_Object val, pstr;
10759
10760 if (!SYMBOLP (var))
10761 continue;
10762 val = find_symbol_value (var);
10763 if (!MARKERP (val))
10764 continue;
10765 if (! EQ (COERCE_MARKER (val),
10766 Fget (var, Qlast_arrow_position))
10767 || ! (pstr = overlay_arrow_string_or_property (var),
10768 EQ (pstr, Fget (var, Qlast_arrow_string))))
10769 return 1;
10770 }
10771 return 0;
10772 }
10773
10774 /* Mark overlay arrows to be updated on next redisplay. */
10775
10776 static void
10777 update_overlay_arrows (up_to_date)
10778 int up_to_date;
10779 {
10780 Lisp_Object vlist;
10781
10782 for (vlist = Voverlay_arrow_variable_list;
10783 CONSP (vlist);
10784 vlist = XCDR (vlist))
10785 {
10786 Lisp_Object var = XCAR (vlist);
10787
10788 if (!SYMBOLP (var))
10789 continue;
10790
10791 if (up_to_date > 0)
10792 {
10793 Lisp_Object val = find_symbol_value (var);
10794 Fput (var, Qlast_arrow_position,
10795 COERCE_MARKER (val));
10796 Fput (var, Qlast_arrow_string,
10797 overlay_arrow_string_or_property (var));
10798 }
10799 else if (up_to_date < 0
10800 || !NILP (Fget (var, Qlast_arrow_position)))
10801 {
10802 Fput (var, Qlast_arrow_position, Qt);
10803 Fput (var, Qlast_arrow_string, Qt);
10804 }
10805 }
10806 }
10807
10808
10809 /* Return overlay arrow string to display at row.
10810 Return integer (bitmap number) for arrow bitmap in left fringe.
10811 Return nil if no overlay arrow. */
10812
10813 static Lisp_Object
10814 overlay_arrow_at_row (it, row)
10815 struct it *it;
10816 struct glyph_row *row;
10817 {
10818 Lisp_Object vlist;
10819
10820 for (vlist = Voverlay_arrow_variable_list;
10821 CONSP (vlist);
10822 vlist = XCDR (vlist))
10823 {
10824 Lisp_Object var = XCAR (vlist);
10825 Lisp_Object val;
10826
10827 if (!SYMBOLP (var))
10828 continue;
10829
10830 val = find_symbol_value (var);
10831
10832 if (MARKERP (val)
10833 && current_buffer == XMARKER (val)->buffer
10834 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10835 {
10836 if (FRAME_WINDOW_P (it->f)
10837 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10838 {
10839 #ifdef HAVE_WINDOW_SYSTEM
10840 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10841 {
10842 int fringe_bitmap;
10843 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10844 return make_number (fringe_bitmap);
10845 }
10846 #endif
10847 return make_number (-1); /* Use default arrow bitmap */
10848 }
10849 return overlay_arrow_string_or_property (var);
10850 }
10851 }
10852
10853 return Qnil;
10854 }
10855
10856 /* Return 1 if point moved out of or into a composition. Otherwise
10857 return 0. PREV_BUF and PREV_PT are the last point buffer and
10858 position. BUF and PT are the current point buffer and position. */
10859
10860 int
10861 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10862 struct buffer *prev_buf, *buf;
10863 int prev_pt, pt;
10864 {
10865 EMACS_INT start, end;
10866 Lisp_Object prop;
10867 Lisp_Object buffer;
10868
10869 XSETBUFFER (buffer, buf);
10870 /* Check a composition at the last point if point moved within the
10871 same buffer. */
10872 if (prev_buf == buf)
10873 {
10874 if (prev_pt == pt)
10875 /* Point didn't move. */
10876 return 0;
10877
10878 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10879 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10880 && COMPOSITION_VALID_P (start, end, prop)
10881 && start < prev_pt && end > prev_pt)
10882 /* The last point was within the composition. Return 1 iff
10883 point moved out of the composition. */
10884 return (pt <= start || pt >= end);
10885 }
10886
10887 /* Check a composition at the current point. */
10888 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10889 && find_composition (pt, -1, &start, &end, &prop, buffer)
10890 && COMPOSITION_VALID_P (start, end, prop)
10891 && start < pt && end > pt);
10892 }
10893
10894
10895 /* Reconsider the setting of B->clip_changed which is displayed
10896 in window W. */
10897
10898 static INLINE void
10899 reconsider_clip_changes (w, b)
10900 struct window *w;
10901 struct buffer *b;
10902 {
10903 if (b->clip_changed
10904 && !NILP (w->window_end_valid)
10905 && w->current_matrix->buffer == b
10906 && w->current_matrix->zv == BUF_ZV (b)
10907 && w->current_matrix->begv == BUF_BEGV (b))
10908 b->clip_changed = 0;
10909
10910 /* If display wasn't paused, and W is not a tool bar window, see if
10911 point has been moved into or out of a composition. In that case,
10912 we set b->clip_changed to 1 to force updating the screen. If
10913 b->clip_changed has already been set to 1, we can skip this
10914 check. */
10915 if (!b->clip_changed
10916 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10917 {
10918 int pt;
10919
10920 if (w == XWINDOW (selected_window))
10921 pt = BUF_PT (current_buffer);
10922 else
10923 pt = marker_position (w->pointm);
10924
10925 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10926 || pt != XINT (w->last_point))
10927 && check_point_in_composition (w->current_matrix->buffer,
10928 XINT (w->last_point),
10929 XBUFFER (w->buffer), pt))
10930 b->clip_changed = 1;
10931 }
10932 }
10933 \f
10934
10935 /* Select FRAME to forward the values of frame-local variables into C
10936 variables so that the redisplay routines can access those values
10937 directly. */
10938
10939 static void
10940 select_frame_for_redisplay (frame)
10941 Lisp_Object frame;
10942 {
10943 Lisp_Object tail, sym, val;
10944 Lisp_Object old = selected_frame;
10945
10946 selected_frame = frame;
10947
10948 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10949 if (CONSP (XCAR (tail))
10950 && (sym = XCAR (XCAR (tail)),
10951 SYMBOLP (sym))
10952 && (sym = indirect_variable (sym),
10953 val = SYMBOL_VALUE (sym),
10954 (BUFFER_LOCAL_VALUEP (val)
10955 || SOME_BUFFER_LOCAL_VALUEP (val)))
10956 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10957 /* Use find_symbol_value rather than Fsymbol_value
10958 to avoid an error if it is void. */
10959 find_symbol_value (sym);
10960
10961 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10962 if (CONSP (XCAR (tail))
10963 && (sym = XCAR (XCAR (tail)),
10964 SYMBOLP (sym))
10965 && (sym = indirect_variable (sym),
10966 val = SYMBOL_VALUE (sym),
10967 (BUFFER_LOCAL_VALUEP (val)
10968 || SOME_BUFFER_LOCAL_VALUEP (val)))
10969 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10970 find_symbol_value (sym);
10971 }
10972
10973
10974 #define STOP_POLLING \
10975 do { if (! polling_stopped_here) stop_polling (); \
10976 polling_stopped_here = 1; } while (0)
10977
10978 #define RESUME_POLLING \
10979 do { if (polling_stopped_here) start_polling (); \
10980 polling_stopped_here = 0; } while (0)
10981
10982
10983 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10984 response to any user action; therefore, we should preserve the echo
10985 area. (Actually, our caller does that job.) Perhaps in the future
10986 avoid recentering windows if it is not necessary; currently that
10987 causes some problems. */
10988
10989 static void
10990 redisplay_internal (preserve_echo_area)
10991 int preserve_echo_area;
10992 {
10993 struct window *w = XWINDOW (selected_window);
10994 struct frame *f;
10995 int pause;
10996 int must_finish = 0;
10997 struct text_pos tlbufpos, tlendpos;
10998 int number_of_visible_frames;
10999 int count;
11000 struct frame *sf;
11001 int polling_stopped_here = 0;
11002
11003 /* Non-zero means redisplay has to consider all windows on all
11004 frames. Zero means, only selected_window is considered. */
11005 int consider_all_windows_p;
11006
11007 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11008
11009 /* No redisplay if running in batch mode or frame is not yet fully
11010 initialized, or redisplay is explicitly turned off by setting
11011 Vinhibit_redisplay. */
11012 if (noninteractive
11013 || !NILP (Vinhibit_redisplay))
11014 return;
11015
11016 /* Don't examine these until after testing Vinhibit_redisplay.
11017 When Emacs is shutting down, perhaps because its connection to
11018 X has dropped, we should not look at them at all. */
11019 f = XFRAME (w->frame);
11020 sf = SELECTED_FRAME ();
11021
11022 if (!f->glyphs_initialized_p)
11023 return;
11024
11025 /* The flag redisplay_performed_directly_p is set by
11026 direct_output_for_insert when it already did the whole screen
11027 update necessary. */
11028 if (redisplay_performed_directly_p)
11029 {
11030 redisplay_performed_directly_p = 0;
11031 if (!hscroll_windows (selected_window))
11032 return;
11033 }
11034
11035 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
11036 if (popup_activated ())
11037 return;
11038 #endif
11039
11040 /* I don't think this happens but let's be paranoid. */
11041 if (redisplaying_p)
11042 return;
11043
11044 /* Record a function that resets redisplaying_p to its old value
11045 when we leave this function. */
11046 count = SPECPDL_INDEX ();
11047 record_unwind_protect (unwind_redisplay,
11048 Fcons (make_number (redisplaying_p), selected_frame));
11049 ++redisplaying_p;
11050 specbind (Qinhibit_free_realized_faces, Qnil);
11051
11052 {
11053 Lisp_Object tail, frame;
11054
11055 FOR_EACH_FRAME (tail, frame)
11056 {
11057 struct frame *f = XFRAME (frame);
11058 f->already_hscrolled_p = 0;
11059 }
11060 }
11061
11062 retry:
11063 pause = 0;
11064 reconsider_clip_changes (w, current_buffer);
11065 last_escape_glyph_frame = NULL;
11066 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11067
11068 /* If new fonts have been loaded that make a glyph matrix adjustment
11069 necessary, do it. */
11070 if (fonts_changed_p)
11071 {
11072 adjust_glyphs (NULL);
11073 ++windows_or_buffers_changed;
11074 fonts_changed_p = 0;
11075 }
11076
11077 /* If face_change_count is non-zero, init_iterator will free all
11078 realized faces, which includes the faces referenced from current
11079 matrices. So, we can't reuse current matrices in this case. */
11080 if (face_change_count)
11081 ++windows_or_buffers_changed;
11082
11083 if (! FRAME_WINDOW_P (sf)
11084 && previous_terminal_frame != sf)
11085 {
11086 /* Since frames on an ASCII terminal share the same display
11087 area, displaying a different frame means redisplay the whole
11088 thing. */
11089 windows_or_buffers_changed++;
11090 SET_FRAME_GARBAGED (sf);
11091 XSETFRAME (Vterminal_frame, sf);
11092 }
11093 previous_terminal_frame = sf;
11094
11095 /* Set the visible flags for all frames. Do this before checking
11096 for resized or garbaged frames; they want to know if their frames
11097 are visible. See the comment in frame.h for
11098 FRAME_SAMPLE_VISIBILITY. */
11099 {
11100 Lisp_Object tail, frame;
11101
11102 number_of_visible_frames = 0;
11103
11104 FOR_EACH_FRAME (tail, frame)
11105 {
11106 struct frame *f = XFRAME (frame);
11107
11108 FRAME_SAMPLE_VISIBILITY (f);
11109 if (FRAME_VISIBLE_P (f))
11110 ++number_of_visible_frames;
11111 clear_desired_matrices (f);
11112 }
11113 }
11114
11115 /* Notice any pending interrupt request to change frame size. */
11116 do_pending_window_change (1);
11117
11118 /* Clear frames marked as garbaged. */
11119 if (frame_garbaged)
11120 clear_garbaged_frames ();
11121
11122 /* Build menubar and tool-bar items. */
11123 if (NILP (Vmemory_full))
11124 prepare_menu_bars ();
11125
11126 if (windows_or_buffers_changed)
11127 update_mode_lines++;
11128
11129 /* Detect case that we need to write or remove a star in the mode line. */
11130 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11131 {
11132 w->update_mode_line = Qt;
11133 if (buffer_shared > 1)
11134 update_mode_lines++;
11135 }
11136
11137 /* If %c is in the mode line, update it if needed. */
11138 if (!NILP (w->column_number_displayed)
11139 /* This alternative quickly identifies a common case
11140 where no change is needed. */
11141 && !(PT == XFASTINT (w->last_point)
11142 && XFASTINT (w->last_modified) >= MODIFF
11143 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11144 && (XFASTINT (w->column_number_displayed)
11145 != (int) current_column ())) /* iftc */
11146 w->update_mode_line = Qt;
11147
11148 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11149
11150 /* The variable buffer_shared is set in redisplay_window and
11151 indicates that we redisplay a buffer in different windows. See
11152 there. */
11153 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11154 || cursor_type_changed);
11155
11156 /* If specs for an arrow have changed, do thorough redisplay
11157 to ensure we remove any arrow that should no longer exist. */
11158 if (overlay_arrows_changed_p ())
11159 consider_all_windows_p = windows_or_buffers_changed = 1;
11160
11161 /* Normally the message* functions will have already displayed and
11162 updated the echo area, but the frame may have been trashed, or
11163 the update may have been preempted, so display the echo area
11164 again here. Checking message_cleared_p captures the case that
11165 the echo area should be cleared. */
11166 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11167 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11168 || (message_cleared_p
11169 && minibuf_level == 0
11170 /* If the mini-window is currently selected, this means the
11171 echo-area doesn't show through. */
11172 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11173 {
11174 int window_height_changed_p = echo_area_display (0);
11175 must_finish = 1;
11176
11177 /* If we don't display the current message, don't clear the
11178 message_cleared_p flag, because, if we did, we wouldn't clear
11179 the echo area in the next redisplay which doesn't preserve
11180 the echo area. */
11181 if (!display_last_displayed_message_p)
11182 message_cleared_p = 0;
11183
11184 if (fonts_changed_p)
11185 goto retry;
11186 else if (window_height_changed_p)
11187 {
11188 consider_all_windows_p = 1;
11189 ++update_mode_lines;
11190 ++windows_or_buffers_changed;
11191
11192 /* If window configuration was changed, frames may have been
11193 marked garbaged. Clear them or we will experience
11194 surprises wrt scrolling. */
11195 if (frame_garbaged)
11196 clear_garbaged_frames ();
11197 }
11198 }
11199 else if (EQ (selected_window, minibuf_window)
11200 && (current_buffer->clip_changed
11201 || XFASTINT (w->last_modified) < MODIFF
11202 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11203 && resize_mini_window (w, 0))
11204 {
11205 /* Resized active mini-window to fit the size of what it is
11206 showing if its contents might have changed. */
11207 must_finish = 1;
11208 consider_all_windows_p = 1;
11209 ++windows_or_buffers_changed;
11210 ++update_mode_lines;
11211
11212 /* If window configuration was changed, frames may have been
11213 marked garbaged. Clear them or we will experience
11214 surprises wrt scrolling. */
11215 if (frame_garbaged)
11216 clear_garbaged_frames ();
11217 }
11218
11219
11220 /* If showing the region, and mark has changed, we must redisplay
11221 the whole window. The assignment to this_line_start_pos prevents
11222 the optimization directly below this if-statement. */
11223 if (((!NILP (Vtransient_mark_mode)
11224 && !NILP (XBUFFER (w->buffer)->mark_active))
11225 != !NILP (w->region_showing))
11226 || (!NILP (w->region_showing)
11227 && !EQ (w->region_showing,
11228 Fmarker_position (XBUFFER (w->buffer)->mark))))
11229 CHARPOS (this_line_start_pos) = 0;
11230
11231 /* Optimize the case that only the line containing the cursor in the
11232 selected window has changed. Variables starting with this_ are
11233 set in display_line and record information about the line
11234 containing the cursor. */
11235 tlbufpos = this_line_start_pos;
11236 tlendpos = this_line_end_pos;
11237 if (!consider_all_windows_p
11238 && CHARPOS (tlbufpos) > 0
11239 && NILP (w->update_mode_line)
11240 && !current_buffer->clip_changed
11241 && !current_buffer->prevent_redisplay_optimizations_p
11242 && FRAME_VISIBLE_P (XFRAME (w->frame))
11243 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11244 /* Make sure recorded data applies to current buffer, etc. */
11245 && this_line_buffer == current_buffer
11246 && current_buffer == XBUFFER (w->buffer)
11247 && NILP (w->force_start)
11248 && NILP (w->optional_new_start)
11249 /* Point must be on the line that we have info recorded about. */
11250 && PT >= CHARPOS (tlbufpos)
11251 && PT <= Z - CHARPOS (tlendpos)
11252 /* All text outside that line, including its final newline,
11253 must be unchanged */
11254 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11255 CHARPOS (tlendpos)))
11256 {
11257 if (CHARPOS (tlbufpos) > BEGV
11258 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11259 && (CHARPOS (tlbufpos) == ZV
11260 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11261 /* Former continuation line has disappeared by becoming empty */
11262 goto cancel;
11263 else if (XFASTINT (w->last_modified) < MODIFF
11264 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11265 || MINI_WINDOW_P (w))
11266 {
11267 /* We have to handle the case of continuation around a
11268 wide-column character (See the comment in indent.c around
11269 line 885).
11270
11271 For instance, in the following case:
11272
11273 -------- Insert --------
11274 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11275 J_I_ ==> J_I_ `^^' are cursors.
11276 ^^ ^^
11277 -------- --------
11278
11279 As we have to redraw the line above, we should goto cancel. */
11280
11281 struct it it;
11282 int line_height_before = this_line_pixel_height;
11283
11284 /* Note that start_display will handle the case that the
11285 line starting at tlbufpos is a continuation lines. */
11286 start_display (&it, w, tlbufpos);
11287
11288 /* Implementation note: It this still necessary? */
11289 if (it.current_x != this_line_start_x)
11290 goto cancel;
11291
11292 TRACE ((stderr, "trying display optimization 1\n"));
11293 w->cursor.vpos = -1;
11294 overlay_arrow_seen = 0;
11295 it.vpos = this_line_vpos;
11296 it.current_y = this_line_y;
11297 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11298 display_line (&it);
11299
11300 /* If line contains point, is not continued,
11301 and ends at same distance from eob as before, we win */
11302 if (w->cursor.vpos >= 0
11303 /* Line is not continued, otherwise this_line_start_pos
11304 would have been set to 0 in display_line. */
11305 && CHARPOS (this_line_start_pos)
11306 /* Line ends as before. */
11307 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11308 /* Line has same height as before. Otherwise other lines
11309 would have to be shifted up or down. */
11310 && this_line_pixel_height == line_height_before)
11311 {
11312 /* If this is not the window's last line, we must adjust
11313 the charstarts of the lines below. */
11314 if (it.current_y < it.last_visible_y)
11315 {
11316 struct glyph_row *row
11317 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11318 int delta, delta_bytes;
11319
11320 if (Z - CHARPOS (tlendpos) == ZV)
11321 {
11322 /* This line ends at end of (accessible part of)
11323 buffer. There is no newline to count. */
11324 delta = (Z
11325 - CHARPOS (tlendpos)
11326 - MATRIX_ROW_START_CHARPOS (row));
11327 delta_bytes = (Z_BYTE
11328 - BYTEPOS (tlendpos)
11329 - MATRIX_ROW_START_BYTEPOS (row));
11330 }
11331 else
11332 {
11333 /* This line ends in a newline. Must take
11334 account of the newline and the rest of the
11335 text that follows. */
11336 delta = (Z
11337 - CHARPOS (tlendpos)
11338 - MATRIX_ROW_START_CHARPOS (row));
11339 delta_bytes = (Z_BYTE
11340 - BYTEPOS (tlendpos)
11341 - MATRIX_ROW_START_BYTEPOS (row));
11342 }
11343
11344 increment_matrix_positions (w->current_matrix,
11345 this_line_vpos + 1,
11346 w->current_matrix->nrows,
11347 delta, delta_bytes);
11348 }
11349
11350 /* If this row displays text now but previously didn't,
11351 or vice versa, w->window_end_vpos may have to be
11352 adjusted. */
11353 if ((it.glyph_row - 1)->displays_text_p)
11354 {
11355 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11356 XSETINT (w->window_end_vpos, this_line_vpos);
11357 }
11358 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11359 && this_line_vpos > 0)
11360 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11361 w->window_end_valid = Qnil;
11362
11363 /* Update hint: No need to try to scroll in update_window. */
11364 w->desired_matrix->no_scrolling_p = 1;
11365
11366 #if GLYPH_DEBUG
11367 *w->desired_matrix->method = 0;
11368 debug_method_add (w, "optimization 1");
11369 #endif
11370 #ifdef HAVE_WINDOW_SYSTEM
11371 update_window_fringes (w, 0);
11372 #endif
11373 goto update;
11374 }
11375 else
11376 goto cancel;
11377 }
11378 else if (/* Cursor position hasn't changed. */
11379 PT == XFASTINT (w->last_point)
11380 /* Make sure the cursor was last displayed
11381 in this window. Otherwise we have to reposition it. */
11382 && 0 <= w->cursor.vpos
11383 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11384 {
11385 if (!must_finish)
11386 {
11387 do_pending_window_change (1);
11388
11389 /* We used to always goto end_of_redisplay here, but this
11390 isn't enough if we have a blinking cursor. */
11391 if (w->cursor_off_p == w->last_cursor_off_p)
11392 goto end_of_redisplay;
11393 }
11394 goto update;
11395 }
11396 /* If highlighting the region, or if the cursor is in the echo area,
11397 then we can't just move the cursor. */
11398 else if (! (!NILP (Vtransient_mark_mode)
11399 && !NILP (current_buffer->mark_active))
11400 && (EQ (selected_window, current_buffer->last_selected_window)
11401 || highlight_nonselected_windows)
11402 && NILP (w->region_showing)
11403 && NILP (Vshow_trailing_whitespace)
11404 && !cursor_in_echo_area)
11405 {
11406 struct it it;
11407 struct glyph_row *row;
11408
11409 /* Skip from tlbufpos to PT and see where it is. Note that
11410 PT may be in invisible text. If so, we will end at the
11411 next visible position. */
11412 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11413 NULL, DEFAULT_FACE_ID);
11414 it.current_x = this_line_start_x;
11415 it.current_y = this_line_y;
11416 it.vpos = this_line_vpos;
11417
11418 /* The call to move_it_to stops in front of PT, but
11419 moves over before-strings. */
11420 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11421
11422 if (it.vpos == this_line_vpos
11423 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11424 row->enabled_p))
11425 {
11426 xassert (this_line_vpos == it.vpos);
11427 xassert (this_line_y == it.current_y);
11428 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11429 #if GLYPH_DEBUG
11430 *w->desired_matrix->method = 0;
11431 debug_method_add (w, "optimization 3");
11432 #endif
11433 goto update;
11434 }
11435 else
11436 goto cancel;
11437 }
11438
11439 cancel:
11440 /* Text changed drastically or point moved off of line. */
11441 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11442 }
11443
11444 CHARPOS (this_line_start_pos) = 0;
11445 consider_all_windows_p |= buffer_shared > 1;
11446 ++clear_face_cache_count;
11447 #ifdef HAVE_WINDOW_SYSTEM
11448 ++clear_image_cache_count;
11449 #endif
11450
11451 /* Build desired matrices, and update the display. If
11452 consider_all_windows_p is non-zero, do it for all windows on all
11453 frames. Otherwise do it for selected_window, only. */
11454
11455 if (consider_all_windows_p)
11456 {
11457 Lisp_Object tail, frame;
11458
11459 FOR_EACH_FRAME (tail, frame)
11460 XFRAME (frame)->updated_p = 0;
11461
11462 /* Recompute # windows showing selected buffer. This will be
11463 incremented each time such a window is displayed. */
11464 buffer_shared = 0;
11465
11466 FOR_EACH_FRAME (tail, frame)
11467 {
11468 struct frame *f = XFRAME (frame);
11469
11470 if (FRAME_WINDOW_P (f) || f == sf)
11471 {
11472 if (! EQ (frame, selected_frame))
11473 /* Select the frame, for the sake of frame-local
11474 variables. */
11475 select_frame_for_redisplay (frame);
11476
11477 /* Mark all the scroll bars to be removed; we'll redeem
11478 the ones we want when we redisplay their windows. */
11479 if (condemn_scroll_bars_hook)
11480 condemn_scroll_bars_hook (f);
11481
11482 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11483 redisplay_windows (FRAME_ROOT_WINDOW (f));
11484
11485 /* Any scroll bars which redisplay_windows should have
11486 nuked should now go away. */
11487 if (judge_scroll_bars_hook)
11488 judge_scroll_bars_hook (f);
11489
11490 /* If fonts changed, display again. */
11491 /* ??? rms: I suspect it is a mistake to jump all the way
11492 back to retry here. It should just retry this frame. */
11493 if (fonts_changed_p)
11494 goto retry;
11495
11496 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11497 {
11498 /* See if we have to hscroll. */
11499 if (!f->already_hscrolled_p)
11500 {
11501 f->already_hscrolled_p = 1;
11502 if (hscroll_windows (f->root_window))
11503 goto retry;
11504 }
11505
11506 /* Prevent various kinds of signals during display
11507 update. stdio is not robust about handling
11508 signals, which can cause an apparent I/O
11509 error. */
11510 if (interrupt_input)
11511 unrequest_sigio ();
11512 STOP_POLLING;
11513
11514 /* Update the display. */
11515 set_window_update_flags (XWINDOW (f->root_window), 1);
11516 pause |= update_frame (f, 0, 0);
11517 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11518 if (pause)
11519 break;
11520 #endif
11521
11522 f->updated_p = 1;
11523 }
11524 }
11525 }
11526
11527 if (!pause)
11528 {
11529 /* Do the mark_window_display_accurate after all windows have
11530 been redisplayed because this call resets flags in buffers
11531 which are needed for proper redisplay. */
11532 FOR_EACH_FRAME (tail, frame)
11533 {
11534 struct frame *f = XFRAME (frame);
11535 if (f->updated_p)
11536 {
11537 mark_window_display_accurate (f->root_window, 1);
11538 if (frame_up_to_date_hook)
11539 frame_up_to_date_hook (f);
11540 }
11541 }
11542 }
11543 }
11544 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11545 {
11546 Lisp_Object mini_window;
11547 struct frame *mini_frame;
11548
11549 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11550 /* Use list_of_error, not Qerror, so that
11551 we catch only errors and don't run the debugger. */
11552 internal_condition_case_1 (redisplay_window_1, selected_window,
11553 list_of_error,
11554 redisplay_window_error);
11555
11556 /* Compare desired and current matrices, perform output. */
11557
11558 update:
11559 /* If fonts changed, display again. */
11560 if (fonts_changed_p)
11561 goto retry;
11562
11563 /* Prevent various kinds of signals during display update.
11564 stdio is not robust about handling signals,
11565 which can cause an apparent I/O error. */
11566 if (interrupt_input)
11567 unrequest_sigio ();
11568 STOP_POLLING;
11569
11570 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11571 {
11572 if (hscroll_windows (selected_window))
11573 goto retry;
11574
11575 XWINDOW (selected_window)->must_be_updated_p = 1;
11576 pause = update_frame (sf, 0, 0);
11577 }
11578
11579 /* We may have called echo_area_display at the top of this
11580 function. If the echo area is on another frame, that may
11581 have put text on a frame other than the selected one, so the
11582 above call to update_frame would not have caught it. Catch
11583 it here. */
11584 mini_window = FRAME_MINIBUF_WINDOW (sf);
11585 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11586
11587 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11588 {
11589 XWINDOW (mini_window)->must_be_updated_p = 1;
11590 pause |= update_frame (mini_frame, 0, 0);
11591 if (!pause && hscroll_windows (mini_window))
11592 goto retry;
11593 }
11594 }
11595
11596 /* If display was paused because of pending input, make sure we do a
11597 thorough update the next time. */
11598 if (pause)
11599 {
11600 /* Prevent the optimization at the beginning of
11601 redisplay_internal that tries a single-line update of the
11602 line containing the cursor in the selected window. */
11603 CHARPOS (this_line_start_pos) = 0;
11604
11605 /* Let the overlay arrow be updated the next time. */
11606 update_overlay_arrows (0);
11607
11608 /* If we pause after scrolling, some rows in the current
11609 matrices of some windows are not valid. */
11610 if (!WINDOW_FULL_WIDTH_P (w)
11611 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11612 update_mode_lines = 1;
11613 }
11614 else
11615 {
11616 if (!consider_all_windows_p)
11617 {
11618 /* This has already been done above if
11619 consider_all_windows_p is set. */
11620 mark_window_display_accurate_1 (w, 1);
11621
11622 /* Say overlay arrows are up to date. */
11623 update_overlay_arrows (1);
11624
11625 if (frame_up_to_date_hook != 0)
11626 frame_up_to_date_hook (sf);
11627 }
11628
11629 update_mode_lines = 0;
11630 windows_or_buffers_changed = 0;
11631 cursor_type_changed = 0;
11632 }
11633
11634 /* Start SIGIO interrupts coming again. Having them off during the
11635 code above makes it less likely one will discard output, but not
11636 impossible, since there might be stuff in the system buffer here.
11637 But it is much hairier to try to do anything about that. */
11638 if (interrupt_input)
11639 request_sigio ();
11640 RESUME_POLLING;
11641
11642 /* If a frame has become visible which was not before, redisplay
11643 again, so that we display it. Expose events for such a frame
11644 (which it gets when becoming visible) don't call the parts of
11645 redisplay constructing glyphs, so simply exposing a frame won't
11646 display anything in this case. So, we have to display these
11647 frames here explicitly. */
11648 if (!pause)
11649 {
11650 Lisp_Object tail, frame;
11651 int new_count = 0;
11652
11653 FOR_EACH_FRAME (tail, frame)
11654 {
11655 int this_is_visible = 0;
11656
11657 if (XFRAME (frame)->visible)
11658 this_is_visible = 1;
11659 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11660 if (XFRAME (frame)->visible)
11661 this_is_visible = 1;
11662
11663 if (this_is_visible)
11664 new_count++;
11665 }
11666
11667 if (new_count != number_of_visible_frames)
11668 windows_or_buffers_changed++;
11669 }
11670
11671 /* Change frame size now if a change is pending. */
11672 do_pending_window_change (1);
11673
11674 /* If we just did a pending size change, or have additional
11675 visible frames, redisplay again. */
11676 if (windows_or_buffers_changed && !pause)
11677 goto retry;
11678
11679 /* Clear the face cache eventually. */
11680 if (consider_all_windows_p)
11681 {
11682 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11683 {
11684 clear_face_cache (0);
11685 clear_face_cache_count = 0;
11686 }
11687 #ifdef HAVE_WINDOW_SYSTEM
11688 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11689 {
11690 Lisp_Object tail, frame;
11691 FOR_EACH_FRAME (tail, frame)
11692 {
11693 struct frame *f = XFRAME (frame);
11694 if (FRAME_WINDOW_P (f))
11695 clear_image_cache (f, 0);
11696 }
11697 clear_image_cache_count = 0;
11698 }
11699 #endif /* HAVE_WINDOW_SYSTEM */
11700 }
11701
11702 end_of_redisplay:
11703 unbind_to (count, Qnil);
11704 RESUME_POLLING;
11705 }
11706
11707
11708 /* Redisplay, but leave alone any recent echo area message unless
11709 another message has been requested in its place.
11710
11711 This is useful in situations where you need to redisplay but no
11712 user action has occurred, making it inappropriate for the message
11713 area to be cleared. See tracking_off and
11714 wait_reading_process_output for examples of these situations.
11715
11716 FROM_WHERE is an integer saying from where this function was
11717 called. This is useful for debugging. */
11718
11719 void
11720 redisplay_preserve_echo_area (from_where)
11721 int from_where;
11722 {
11723 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11724
11725 if (!NILP (echo_area_buffer[1]))
11726 {
11727 /* We have a previously displayed message, but no current
11728 message. Redisplay the previous message. */
11729 display_last_displayed_message_p = 1;
11730 redisplay_internal (1);
11731 display_last_displayed_message_p = 0;
11732 }
11733 else
11734 redisplay_internal (1);
11735
11736 if (rif != NULL && rif->flush_display_optional)
11737 rif->flush_display_optional (NULL);
11738 }
11739
11740
11741 /* Function registered with record_unwind_protect in
11742 redisplay_internal. Reset redisplaying_p to the value it had
11743 before redisplay_internal was called, and clear
11744 prevent_freeing_realized_faces_p. It also selects the previously
11745 selected frame. */
11746
11747 static Lisp_Object
11748 unwind_redisplay (val)
11749 Lisp_Object val;
11750 {
11751 Lisp_Object old_redisplaying_p, old_frame;
11752
11753 old_redisplaying_p = XCAR (val);
11754 redisplaying_p = XFASTINT (old_redisplaying_p);
11755 old_frame = XCDR (val);
11756 if (! EQ (old_frame, selected_frame))
11757 select_frame_for_redisplay (old_frame);
11758 return Qnil;
11759 }
11760
11761
11762 /* Mark the display of window W as accurate or inaccurate. If
11763 ACCURATE_P is non-zero mark display of W as accurate. If
11764 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11765 redisplay_internal is called. */
11766
11767 static void
11768 mark_window_display_accurate_1 (w, accurate_p)
11769 struct window *w;
11770 int accurate_p;
11771 {
11772 if (BUFFERP (w->buffer))
11773 {
11774 struct buffer *b = XBUFFER (w->buffer);
11775
11776 w->last_modified
11777 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11778 w->last_overlay_modified
11779 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11780 w->last_had_star
11781 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11782
11783 if (accurate_p)
11784 {
11785 b->clip_changed = 0;
11786 b->prevent_redisplay_optimizations_p = 0;
11787
11788 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11789 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11790 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11791 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11792
11793 w->current_matrix->buffer = b;
11794 w->current_matrix->begv = BUF_BEGV (b);
11795 w->current_matrix->zv = BUF_ZV (b);
11796
11797 w->last_cursor = w->cursor;
11798 w->last_cursor_off_p = w->cursor_off_p;
11799
11800 if (w == XWINDOW (selected_window))
11801 w->last_point = make_number (BUF_PT (b));
11802 else
11803 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11804 }
11805 }
11806
11807 if (accurate_p)
11808 {
11809 w->window_end_valid = w->buffer;
11810 #if 0 /* This is incorrect with variable-height lines. */
11811 xassert (XINT (w->window_end_vpos)
11812 < (WINDOW_TOTAL_LINES (w)
11813 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11814 #endif
11815 w->update_mode_line = Qnil;
11816 }
11817 }
11818
11819
11820 /* Mark the display of windows in the window tree rooted at WINDOW as
11821 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11822 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11823 be redisplayed the next time redisplay_internal is called. */
11824
11825 void
11826 mark_window_display_accurate (window, accurate_p)
11827 Lisp_Object window;
11828 int accurate_p;
11829 {
11830 struct window *w;
11831
11832 for (; !NILP (window); window = w->next)
11833 {
11834 w = XWINDOW (window);
11835 mark_window_display_accurate_1 (w, accurate_p);
11836
11837 if (!NILP (w->vchild))
11838 mark_window_display_accurate (w->vchild, accurate_p);
11839 if (!NILP (w->hchild))
11840 mark_window_display_accurate (w->hchild, accurate_p);
11841 }
11842
11843 if (accurate_p)
11844 {
11845 update_overlay_arrows (1);
11846 }
11847 else
11848 {
11849 /* Force a thorough redisplay the next time by setting
11850 last_arrow_position and last_arrow_string to t, which is
11851 unequal to any useful value of Voverlay_arrow_... */
11852 update_overlay_arrows (-1);
11853 }
11854 }
11855
11856
11857 /* Return value in display table DP (Lisp_Char_Table *) for character
11858 C. Since a display table doesn't have any parent, we don't have to
11859 follow parent. Do not call this function directly but use the
11860 macro DISP_CHAR_VECTOR. */
11861
11862 Lisp_Object
11863 disp_char_vector (dp, c)
11864 struct Lisp_Char_Table *dp;
11865 int c;
11866 {
11867 Lisp_Object val;
11868
11869 if (ASCII_CHAR_P (c))
11870 {
11871 val = dp->ascii;
11872 if (SUB_CHAR_TABLE_P (val))
11873 val = XSUB_CHAR_TABLE (val)->contents[c];
11874 }
11875 else
11876 {
11877 Lisp_Object table;
11878
11879 XSETCHAR_TABLE (table, dp);
11880 val = char_table_ref (table, c);
11881 }
11882 if (NILP (val))
11883 val = dp->defalt;
11884 return val;
11885 }
11886
11887
11888 \f
11889 /***********************************************************************
11890 Window Redisplay
11891 ***********************************************************************/
11892
11893 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11894
11895 static void
11896 redisplay_windows (window)
11897 Lisp_Object window;
11898 {
11899 while (!NILP (window))
11900 {
11901 struct window *w = XWINDOW (window);
11902
11903 if (!NILP (w->hchild))
11904 redisplay_windows (w->hchild);
11905 else if (!NILP (w->vchild))
11906 redisplay_windows (w->vchild);
11907 else
11908 {
11909 displayed_buffer = XBUFFER (w->buffer);
11910 /* Use list_of_error, not Qerror, so that
11911 we catch only errors and don't run the debugger. */
11912 internal_condition_case_1 (redisplay_window_0, window,
11913 list_of_error,
11914 redisplay_window_error);
11915 }
11916
11917 window = w->next;
11918 }
11919 }
11920
11921 static Lisp_Object
11922 redisplay_window_error ()
11923 {
11924 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11925 return Qnil;
11926 }
11927
11928 static Lisp_Object
11929 redisplay_window_0 (window)
11930 Lisp_Object window;
11931 {
11932 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11933 redisplay_window (window, 0);
11934 return Qnil;
11935 }
11936
11937 static Lisp_Object
11938 redisplay_window_1 (window)
11939 Lisp_Object window;
11940 {
11941 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11942 redisplay_window (window, 1);
11943 return Qnil;
11944 }
11945 \f
11946
11947 /* Increment GLYPH until it reaches END or CONDITION fails while
11948 adding (GLYPH)->pixel_width to X. */
11949
11950 #define SKIP_GLYPHS(glyph, end, x, condition) \
11951 do \
11952 { \
11953 (x) += (glyph)->pixel_width; \
11954 ++(glyph); \
11955 } \
11956 while ((glyph) < (end) && (condition))
11957
11958
11959 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11960 DELTA is the number of bytes by which positions recorded in ROW
11961 differ from current buffer positions.
11962
11963 Return 0 if cursor is not on this row. 1 otherwise. */
11964
11965 int
11966 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11967 struct window *w;
11968 struct glyph_row *row;
11969 struct glyph_matrix *matrix;
11970 int delta, delta_bytes, dy, dvpos;
11971 {
11972 struct glyph *glyph = row->glyphs[TEXT_AREA];
11973 struct glyph *end = glyph + row->used[TEXT_AREA];
11974 struct glyph *cursor = NULL;
11975 /* The first glyph that starts a sequence of glyphs from string. */
11976 struct glyph *string_start;
11977 /* The X coordinate of string_start. */
11978 int string_start_x;
11979 /* The last known character position. */
11980 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11981 /* The last known character position before string_start. */
11982 int string_before_pos;
11983 int x = row->x;
11984 int cursor_x = x;
11985 int cursor_from_overlay_pos = 0;
11986 int pt_old = PT - delta;
11987
11988 /* Skip over glyphs not having an object at the start of the row.
11989 These are special glyphs like truncation marks on terminal
11990 frames. */
11991 if (row->displays_text_p)
11992 while (glyph < end
11993 && INTEGERP (glyph->object)
11994 && glyph->charpos < 0)
11995 {
11996 x += glyph->pixel_width;
11997 ++glyph;
11998 }
11999
12000 string_start = NULL;
12001 while (glyph < end
12002 && !INTEGERP (glyph->object)
12003 && (!BUFFERP (glyph->object)
12004 || (last_pos = glyph->charpos) < pt_old))
12005 {
12006 if (! STRINGP (glyph->object))
12007 {
12008 string_start = NULL;
12009 x += glyph->pixel_width;
12010 ++glyph;
12011 if (cursor_from_overlay_pos
12012 && last_pos >= cursor_from_overlay_pos)
12013 {
12014 cursor_from_overlay_pos = 0;
12015 cursor = 0;
12016 }
12017 }
12018 else
12019 {
12020 if (string_start == NULL)
12021 {
12022 string_before_pos = last_pos;
12023 string_start = glyph;
12024 string_start_x = x;
12025 }
12026 /* Skip all glyphs from string. */
12027 do
12028 {
12029 Lisp_Object cprop;
12030 int pos;
12031 if ((cursor == NULL || glyph > cursor)
12032 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12033 Qcursor, (glyph)->object),
12034 !NILP (cprop))
12035 && (pos = string_buffer_position (w, glyph->object,
12036 string_before_pos),
12037 (pos == 0 /* From overlay */
12038 || pos == pt_old)))
12039 {
12040 /* Estimate overlay buffer position from the buffer
12041 positions of the glyphs before and after the overlay.
12042 Add 1 to last_pos so that if point corresponds to the
12043 glyph right after the overlay, we still use a 'cursor'
12044 property found in that overlay. */
12045 cursor_from_overlay_pos = (pos ? 0 : last_pos
12046 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12047 cursor = glyph;
12048 cursor_x = x;
12049 }
12050 x += glyph->pixel_width;
12051 ++glyph;
12052 }
12053 while (glyph < end && EQ (glyph->object, string_start->object));
12054 }
12055 }
12056
12057 if (cursor != NULL)
12058 {
12059 glyph = cursor;
12060 x = cursor_x;
12061 }
12062 else if (row->ends_in_ellipsis_p && glyph == end)
12063 {
12064 /* Scan back over the ellipsis glyphs, decrementing positions. */
12065 while (glyph > row->glyphs[TEXT_AREA]
12066 && (glyph - 1)->charpos == last_pos)
12067 glyph--, x -= glyph->pixel_width;
12068 /* That loop always goes one position too far,
12069 including the glyph before the ellipsis.
12070 So scan forward over that one. */
12071 x += glyph->pixel_width;
12072 glyph++;
12073 }
12074 else if (string_start
12075 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12076 {
12077 /* We may have skipped over point because the previous glyphs
12078 are from string. As there's no easy way to know the
12079 character position of the current glyph, find the correct
12080 glyph on point by scanning from string_start again. */
12081 Lisp_Object limit;
12082 Lisp_Object string;
12083 struct glyph *stop = glyph;
12084 int pos;
12085
12086 limit = make_number (pt_old + 1);
12087 glyph = string_start;
12088 x = string_start_x;
12089 string = glyph->object;
12090 pos = string_buffer_position (w, string, string_before_pos);
12091 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12092 because we always put cursor after overlay strings. */
12093 while (pos == 0 && glyph < stop)
12094 {
12095 string = glyph->object;
12096 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12097 if (glyph < stop)
12098 pos = string_buffer_position (w, glyph->object, string_before_pos);
12099 }
12100
12101 while (glyph < stop)
12102 {
12103 pos = XINT (Fnext_single_char_property_change
12104 (make_number (pos), Qdisplay, Qnil, limit));
12105 if (pos > pt_old)
12106 break;
12107 /* Skip glyphs from the same string. */
12108 string = glyph->object;
12109 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12110 /* Skip glyphs from an overlay. */
12111 while (glyph < stop
12112 && ! string_buffer_position (w, glyph->object, pos))
12113 {
12114 string = glyph->object;
12115 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12116 }
12117 }
12118
12119 /* If we reached the end of the line, and end was from a string,
12120 cursor is not on this line. */
12121 if (glyph == end && row->continued_p)
12122 return 0;
12123 }
12124
12125 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12126 w->cursor.x = x;
12127 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12128 w->cursor.y = row->y + dy;
12129
12130 if (w == XWINDOW (selected_window))
12131 {
12132 if (!row->continued_p
12133 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12134 && row->x == 0)
12135 {
12136 this_line_buffer = XBUFFER (w->buffer);
12137
12138 CHARPOS (this_line_start_pos)
12139 = MATRIX_ROW_START_CHARPOS (row) + delta;
12140 BYTEPOS (this_line_start_pos)
12141 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12142
12143 CHARPOS (this_line_end_pos)
12144 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12145 BYTEPOS (this_line_end_pos)
12146 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12147
12148 this_line_y = w->cursor.y;
12149 this_line_pixel_height = row->height;
12150 this_line_vpos = w->cursor.vpos;
12151 this_line_start_x = row->x;
12152 }
12153 else
12154 CHARPOS (this_line_start_pos) = 0;
12155 }
12156
12157 return 1;
12158 }
12159
12160
12161 /* Run window scroll functions, if any, for WINDOW with new window
12162 start STARTP. Sets the window start of WINDOW to that position.
12163
12164 We assume that the window's buffer is really current. */
12165
12166 static INLINE struct text_pos
12167 run_window_scroll_functions (window, startp)
12168 Lisp_Object window;
12169 struct text_pos startp;
12170 {
12171 struct window *w = XWINDOW (window);
12172 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12173
12174 if (current_buffer != XBUFFER (w->buffer))
12175 abort ();
12176
12177 if (!NILP (Vwindow_scroll_functions))
12178 {
12179 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12180 make_number (CHARPOS (startp)));
12181 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12182 /* In case the hook functions switch buffers. */
12183 if (current_buffer != XBUFFER (w->buffer))
12184 set_buffer_internal_1 (XBUFFER (w->buffer));
12185 }
12186
12187 return startp;
12188 }
12189
12190
12191 /* Make sure the line containing the cursor is fully visible.
12192 A value of 1 means there is nothing to be done.
12193 (Either the line is fully visible, or it cannot be made so,
12194 or we cannot tell.)
12195
12196 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12197 is higher than window.
12198
12199 A value of 0 means the caller should do scrolling
12200 as if point had gone off the screen. */
12201
12202 static int
12203 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12204 struct window *w;
12205 int force_p;
12206 int current_matrix_p;
12207 {
12208 struct glyph_matrix *matrix;
12209 struct glyph_row *row;
12210 int window_height;
12211
12212 if (!make_cursor_line_fully_visible_p)
12213 return 1;
12214
12215 /* It's not always possible to find the cursor, e.g, when a window
12216 is full of overlay strings. Don't do anything in that case. */
12217 if (w->cursor.vpos < 0)
12218 return 1;
12219
12220 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12221 row = MATRIX_ROW (matrix, w->cursor.vpos);
12222
12223 /* If the cursor row is not partially visible, there's nothing to do. */
12224 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12225 return 1;
12226
12227 /* If the row the cursor is in is taller than the window's height,
12228 it's not clear what to do, so do nothing. */
12229 window_height = window_box_height (w);
12230 if (row->height >= window_height)
12231 {
12232 if (!force_p || MINI_WINDOW_P (w)
12233 || w->vscroll || w->cursor.vpos == 0)
12234 return 1;
12235 }
12236 return 0;
12237
12238 #if 0
12239 /* This code used to try to scroll the window just enough to make
12240 the line visible. It returned 0 to say that the caller should
12241 allocate larger glyph matrices. */
12242
12243 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12244 {
12245 int dy = row->height - row->visible_height;
12246 w->vscroll = 0;
12247 w->cursor.y += dy;
12248 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12249 }
12250 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12251 {
12252 int dy = - (row->height - row->visible_height);
12253 w->vscroll = dy;
12254 w->cursor.y += dy;
12255 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12256 }
12257
12258 /* When we change the cursor y-position of the selected window,
12259 change this_line_y as well so that the display optimization for
12260 the cursor line of the selected window in redisplay_internal uses
12261 the correct y-position. */
12262 if (w == XWINDOW (selected_window))
12263 this_line_y = w->cursor.y;
12264
12265 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12266 redisplay with larger matrices. */
12267 if (matrix->nrows < required_matrix_height (w))
12268 {
12269 fonts_changed_p = 1;
12270 return 0;
12271 }
12272
12273 return 1;
12274 #endif /* 0 */
12275 }
12276
12277
12278 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12279 non-zero means only WINDOW is redisplayed in redisplay_internal.
12280 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12281 in redisplay_window to bring a partially visible line into view in
12282 the case that only the cursor has moved.
12283
12284 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12285 last screen line's vertical height extends past the end of the screen.
12286
12287 Value is
12288
12289 1 if scrolling succeeded
12290
12291 0 if scrolling didn't find point.
12292
12293 -1 if new fonts have been loaded so that we must interrupt
12294 redisplay, adjust glyph matrices, and try again. */
12295
12296 enum
12297 {
12298 SCROLLING_SUCCESS,
12299 SCROLLING_FAILED,
12300 SCROLLING_NEED_LARGER_MATRICES
12301 };
12302
12303 static int
12304 try_scrolling (window, just_this_one_p, scroll_conservatively,
12305 scroll_step, temp_scroll_step, last_line_misfit)
12306 Lisp_Object window;
12307 int just_this_one_p;
12308 EMACS_INT scroll_conservatively, scroll_step;
12309 int temp_scroll_step;
12310 int last_line_misfit;
12311 {
12312 struct window *w = XWINDOW (window);
12313 struct frame *f = XFRAME (w->frame);
12314 struct text_pos scroll_margin_pos;
12315 struct text_pos pos;
12316 struct text_pos startp;
12317 struct it it;
12318 Lisp_Object window_end;
12319 int this_scroll_margin;
12320 int dy = 0;
12321 int scroll_max;
12322 int rc;
12323 int amount_to_scroll = 0;
12324 Lisp_Object aggressive;
12325 int height;
12326 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12327
12328 #if GLYPH_DEBUG
12329 debug_method_add (w, "try_scrolling");
12330 #endif
12331
12332 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12333
12334 /* Compute scroll margin height in pixels. We scroll when point is
12335 within this distance from the top or bottom of the window. */
12336 if (scroll_margin > 0)
12337 {
12338 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12339 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12340 }
12341 else
12342 this_scroll_margin = 0;
12343
12344 /* Force scroll_conservatively to have a reasonable value so it doesn't
12345 cause an overflow while computing how much to scroll. */
12346 if (scroll_conservatively)
12347 scroll_conservatively = min (scroll_conservatively,
12348 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12349
12350 /* Compute how much we should try to scroll maximally to bring point
12351 into view. */
12352 if (scroll_step || scroll_conservatively || temp_scroll_step)
12353 scroll_max = max (scroll_step,
12354 max (scroll_conservatively, temp_scroll_step));
12355 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12356 || NUMBERP (current_buffer->scroll_up_aggressively))
12357 /* We're trying to scroll because of aggressive scrolling
12358 but no scroll_step is set. Choose an arbitrary one. Maybe
12359 there should be a variable for this. */
12360 scroll_max = 10;
12361 else
12362 scroll_max = 0;
12363 scroll_max *= FRAME_LINE_HEIGHT (f);
12364
12365 /* Decide whether we have to scroll down. Start at the window end
12366 and move this_scroll_margin up to find the position of the scroll
12367 margin. */
12368 window_end = Fwindow_end (window, Qt);
12369
12370 too_near_end:
12371
12372 CHARPOS (scroll_margin_pos) = XINT (window_end);
12373 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12374
12375 if (this_scroll_margin || extra_scroll_margin_lines)
12376 {
12377 start_display (&it, w, scroll_margin_pos);
12378 if (this_scroll_margin)
12379 move_it_vertically_backward (&it, this_scroll_margin);
12380 if (extra_scroll_margin_lines)
12381 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12382 scroll_margin_pos = it.current.pos;
12383 }
12384
12385 if (PT >= CHARPOS (scroll_margin_pos))
12386 {
12387 int y0;
12388
12389 /* Point is in the scroll margin at the bottom of the window, or
12390 below. Compute a new window start that makes point visible. */
12391
12392 /* Compute the distance from the scroll margin to PT.
12393 Give up if the distance is greater than scroll_max. */
12394 start_display (&it, w, scroll_margin_pos);
12395 y0 = it.current_y;
12396 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12397 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12398
12399 /* To make point visible, we have to move the window start
12400 down so that the line the cursor is in is visible, which
12401 means we have to add in the height of the cursor line. */
12402 dy = line_bottom_y (&it) - y0;
12403
12404 if (dy > scroll_max)
12405 return SCROLLING_FAILED;
12406
12407 /* Move the window start down. If scrolling conservatively,
12408 move it just enough down to make point visible. If
12409 scroll_step is set, move it down by scroll_step. */
12410 start_display (&it, w, startp);
12411
12412 if (scroll_conservatively)
12413 /* Set AMOUNT_TO_SCROLL to at least one line,
12414 and at most scroll_conservatively lines. */
12415 amount_to_scroll
12416 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12417 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12418 else if (scroll_step || temp_scroll_step)
12419 amount_to_scroll = scroll_max;
12420 else
12421 {
12422 aggressive = current_buffer->scroll_up_aggressively;
12423 height = WINDOW_BOX_TEXT_HEIGHT (w);
12424 if (NUMBERP (aggressive))
12425 {
12426 double float_amount = XFLOATINT (aggressive) * height;
12427 amount_to_scroll = float_amount;
12428 if (amount_to_scroll == 0 && float_amount > 0)
12429 amount_to_scroll = 1;
12430 }
12431 }
12432
12433 if (amount_to_scroll <= 0)
12434 return SCROLLING_FAILED;
12435
12436 /* If moving by amount_to_scroll leaves STARTP unchanged,
12437 move it down one screen line. */
12438
12439 move_it_vertically (&it, amount_to_scroll);
12440 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12441 move_it_by_lines (&it, 1, 1);
12442 startp = it.current.pos;
12443 }
12444 else
12445 {
12446 /* See if point is inside the scroll margin at the top of the
12447 window. */
12448 scroll_margin_pos = startp;
12449 if (this_scroll_margin)
12450 {
12451 start_display (&it, w, startp);
12452 move_it_vertically (&it, this_scroll_margin);
12453 scroll_margin_pos = it.current.pos;
12454 }
12455
12456 if (PT < CHARPOS (scroll_margin_pos))
12457 {
12458 /* Point is in the scroll margin at the top of the window or
12459 above what is displayed in the window. */
12460 int y0;
12461
12462 /* Compute the vertical distance from PT to the scroll
12463 margin position. Give up if distance is greater than
12464 scroll_max. */
12465 SET_TEXT_POS (pos, PT, PT_BYTE);
12466 start_display (&it, w, pos);
12467 y0 = it.current_y;
12468 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12469 it.last_visible_y, -1,
12470 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12471 dy = it.current_y - y0;
12472 if (dy > scroll_max)
12473 return SCROLLING_FAILED;
12474
12475 /* Compute new window start. */
12476 start_display (&it, w, startp);
12477
12478 if (scroll_conservatively)
12479 amount_to_scroll
12480 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12481 else if (scroll_step || temp_scroll_step)
12482 amount_to_scroll = scroll_max;
12483 else
12484 {
12485 aggressive = current_buffer->scroll_down_aggressively;
12486 height = WINDOW_BOX_TEXT_HEIGHT (w);
12487 if (NUMBERP (aggressive))
12488 {
12489 double float_amount = XFLOATINT (aggressive) * height;
12490 amount_to_scroll = float_amount;
12491 if (amount_to_scroll == 0 && float_amount > 0)
12492 amount_to_scroll = 1;
12493 }
12494 }
12495
12496 if (amount_to_scroll <= 0)
12497 return SCROLLING_FAILED;
12498
12499 move_it_vertically_backward (&it, amount_to_scroll);
12500 startp = it.current.pos;
12501 }
12502 }
12503
12504 /* Run window scroll functions. */
12505 startp = run_window_scroll_functions (window, startp);
12506
12507 /* Display the window. Give up if new fonts are loaded, or if point
12508 doesn't appear. */
12509 if (!try_window (window, startp, 0))
12510 rc = SCROLLING_NEED_LARGER_MATRICES;
12511 else if (w->cursor.vpos < 0)
12512 {
12513 clear_glyph_matrix (w->desired_matrix);
12514 rc = SCROLLING_FAILED;
12515 }
12516 else
12517 {
12518 /* Maybe forget recorded base line for line number display. */
12519 if (!just_this_one_p
12520 || current_buffer->clip_changed
12521 || BEG_UNCHANGED < CHARPOS (startp))
12522 w->base_line_number = Qnil;
12523
12524 /* If cursor ends up on a partially visible line,
12525 treat that as being off the bottom of the screen. */
12526 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12527 {
12528 clear_glyph_matrix (w->desired_matrix);
12529 ++extra_scroll_margin_lines;
12530 goto too_near_end;
12531 }
12532 rc = SCROLLING_SUCCESS;
12533 }
12534
12535 return rc;
12536 }
12537
12538
12539 /* Compute a suitable window start for window W if display of W starts
12540 on a continuation line. Value is non-zero if a new window start
12541 was computed.
12542
12543 The new window start will be computed, based on W's width, starting
12544 from the start of the continued line. It is the start of the
12545 screen line with the minimum distance from the old start W->start. */
12546
12547 static int
12548 compute_window_start_on_continuation_line (w)
12549 struct window *w;
12550 {
12551 struct text_pos pos, start_pos;
12552 int window_start_changed_p = 0;
12553
12554 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12555
12556 /* If window start is on a continuation line... Window start may be
12557 < BEGV in case there's invisible text at the start of the
12558 buffer (M-x rmail, for example). */
12559 if (CHARPOS (start_pos) > BEGV
12560 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12561 {
12562 struct it it;
12563 struct glyph_row *row;
12564
12565 /* Handle the case that the window start is out of range. */
12566 if (CHARPOS (start_pos) < BEGV)
12567 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12568 else if (CHARPOS (start_pos) > ZV)
12569 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12570
12571 /* Find the start of the continued line. This should be fast
12572 because scan_buffer is fast (newline cache). */
12573 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12574 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12575 row, DEFAULT_FACE_ID);
12576 reseat_at_previous_visible_line_start (&it);
12577
12578 /* If the line start is "too far" away from the window start,
12579 say it takes too much time to compute a new window start. */
12580 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12581 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12582 {
12583 int min_distance, distance;
12584
12585 /* Move forward by display lines to find the new window
12586 start. If window width was enlarged, the new start can
12587 be expected to be > the old start. If window width was
12588 decreased, the new window start will be < the old start.
12589 So, we're looking for the display line start with the
12590 minimum distance from the old window start. */
12591 pos = it.current.pos;
12592 min_distance = INFINITY;
12593 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12594 distance < min_distance)
12595 {
12596 min_distance = distance;
12597 pos = it.current.pos;
12598 move_it_by_lines (&it, 1, 0);
12599 }
12600
12601 /* Set the window start there. */
12602 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12603 window_start_changed_p = 1;
12604 }
12605 }
12606
12607 return window_start_changed_p;
12608 }
12609
12610
12611 /* Try cursor movement in case text has not changed in window WINDOW,
12612 with window start STARTP. Value is
12613
12614 CURSOR_MOVEMENT_SUCCESS if successful
12615
12616 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12617
12618 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12619 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12620 we want to scroll as if scroll-step were set to 1. See the code.
12621
12622 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12623 which case we have to abort this redisplay, and adjust matrices
12624 first. */
12625
12626 enum
12627 {
12628 CURSOR_MOVEMENT_SUCCESS,
12629 CURSOR_MOVEMENT_CANNOT_BE_USED,
12630 CURSOR_MOVEMENT_MUST_SCROLL,
12631 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12632 };
12633
12634 static int
12635 try_cursor_movement (window, startp, scroll_step)
12636 Lisp_Object window;
12637 struct text_pos startp;
12638 int *scroll_step;
12639 {
12640 struct window *w = XWINDOW (window);
12641 struct frame *f = XFRAME (w->frame);
12642 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12643
12644 #if GLYPH_DEBUG
12645 if (inhibit_try_cursor_movement)
12646 return rc;
12647 #endif
12648
12649 /* Handle case where text has not changed, only point, and it has
12650 not moved off the frame. */
12651 if (/* Point may be in this window. */
12652 PT >= CHARPOS (startp)
12653 /* Selective display hasn't changed. */
12654 && !current_buffer->clip_changed
12655 /* Function force-mode-line-update is used to force a thorough
12656 redisplay. It sets either windows_or_buffers_changed or
12657 update_mode_lines. So don't take a shortcut here for these
12658 cases. */
12659 && !update_mode_lines
12660 && !windows_or_buffers_changed
12661 && !cursor_type_changed
12662 /* Can't use this case if highlighting a region. When a
12663 region exists, cursor movement has to do more than just
12664 set the cursor. */
12665 && !(!NILP (Vtransient_mark_mode)
12666 && !NILP (current_buffer->mark_active))
12667 && NILP (w->region_showing)
12668 && NILP (Vshow_trailing_whitespace)
12669 /* Right after splitting windows, last_point may be nil. */
12670 && INTEGERP (w->last_point)
12671 /* This code is not used for mini-buffer for the sake of the case
12672 of redisplaying to replace an echo area message; since in
12673 that case the mini-buffer contents per se are usually
12674 unchanged. This code is of no real use in the mini-buffer
12675 since the handling of this_line_start_pos, etc., in redisplay
12676 handles the same cases. */
12677 && !EQ (window, minibuf_window)
12678 /* When splitting windows or for new windows, it happens that
12679 redisplay is called with a nil window_end_vpos or one being
12680 larger than the window. This should really be fixed in
12681 window.c. I don't have this on my list, now, so we do
12682 approximately the same as the old redisplay code. --gerd. */
12683 && INTEGERP (w->window_end_vpos)
12684 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12685 && (FRAME_WINDOW_P (f)
12686 || !overlay_arrow_in_current_buffer_p ()))
12687 {
12688 int this_scroll_margin, top_scroll_margin;
12689 struct glyph_row *row = NULL;
12690
12691 #if GLYPH_DEBUG
12692 debug_method_add (w, "cursor movement");
12693 #endif
12694
12695 /* Scroll if point within this distance from the top or bottom
12696 of the window. This is a pixel value. */
12697 this_scroll_margin = max (0, scroll_margin);
12698 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12699 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12700
12701 top_scroll_margin = this_scroll_margin;
12702 if (WINDOW_WANTS_HEADER_LINE_P (w))
12703 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12704
12705 /* Start with the row the cursor was displayed during the last
12706 not paused redisplay. Give up if that row is not valid. */
12707 if (w->last_cursor.vpos < 0
12708 || w->last_cursor.vpos >= w->current_matrix->nrows)
12709 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12710 else
12711 {
12712 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12713 if (row->mode_line_p)
12714 ++row;
12715 if (!row->enabled_p)
12716 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12717 }
12718
12719 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12720 {
12721 int scroll_p = 0;
12722 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12723
12724 if (PT > XFASTINT (w->last_point))
12725 {
12726 /* Point has moved forward. */
12727 while (MATRIX_ROW_END_CHARPOS (row) < PT
12728 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12729 {
12730 xassert (row->enabled_p);
12731 ++row;
12732 }
12733
12734 /* The end position of a row equals the start position
12735 of the next row. If PT is there, we would rather
12736 display it in the next line. */
12737 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12738 && MATRIX_ROW_END_CHARPOS (row) == PT
12739 && !cursor_row_p (w, row))
12740 ++row;
12741
12742 /* If within the scroll margin, scroll. Note that
12743 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12744 the next line would be drawn, and that
12745 this_scroll_margin can be zero. */
12746 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12747 || PT > MATRIX_ROW_END_CHARPOS (row)
12748 /* Line is completely visible last line in window
12749 and PT is to be set in the next line. */
12750 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12751 && PT == MATRIX_ROW_END_CHARPOS (row)
12752 && !row->ends_at_zv_p
12753 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12754 scroll_p = 1;
12755 }
12756 else if (PT < XFASTINT (w->last_point))
12757 {
12758 /* Cursor has to be moved backward. Note that PT >=
12759 CHARPOS (startp) because of the outer if-statement. */
12760 while (!row->mode_line_p
12761 && (MATRIX_ROW_START_CHARPOS (row) > PT
12762 || (MATRIX_ROW_START_CHARPOS (row) == PT
12763 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12764 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12765 row > w->current_matrix->rows
12766 && (row-1)->ends_in_newline_from_string_p))))
12767 && (row->y > top_scroll_margin
12768 || CHARPOS (startp) == BEGV))
12769 {
12770 xassert (row->enabled_p);
12771 --row;
12772 }
12773
12774 /* Consider the following case: Window starts at BEGV,
12775 there is invisible, intangible text at BEGV, so that
12776 display starts at some point START > BEGV. It can
12777 happen that we are called with PT somewhere between
12778 BEGV and START. Try to handle that case. */
12779 if (row < w->current_matrix->rows
12780 || row->mode_line_p)
12781 {
12782 row = w->current_matrix->rows;
12783 if (row->mode_line_p)
12784 ++row;
12785 }
12786
12787 /* Due to newlines in overlay strings, we may have to
12788 skip forward over overlay strings. */
12789 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12790 && MATRIX_ROW_END_CHARPOS (row) == PT
12791 && !cursor_row_p (w, row))
12792 ++row;
12793
12794 /* If within the scroll margin, scroll. */
12795 if (row->y < top_scroll_margin
12796 && CHARPOS (startp) != BEGV)
12797 scroll_p = 1;
12798 }
12799 else
12800 {
12801 /* Cursor did not move. So don't scroll even if cursor line
12802 is partially visible, as it was so before. */
12803 rc = CURSOR_MOVEMENT_SUCCESS;
12804 }
12805
12806 if (PT < MATRIX_ROW_START_CHARPOS (row)
12807 || PT > MATRIX_ROW_END_CHARPOS (row))
12808 {
12809 /* if PT is not in the glyph row, give up. */
12810 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12811 }
12812 else if (rc != CURSOR_MOVEMENT_SUCCESS
12813 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12814 && make_cursor_line_fully_visible_p)
12815 {
12816 if (PT == MATRIX_ROW_END_CHARPOS (row)
12817 && !row->ends_at_zv_p
12818 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12819 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12820 else if (row->height > window_box_height (w))
12821 {
12822 /* If we end up in a partially visible line, let's
12823 make it fully visible, except when it's taller
12824 than the window, in which case we can't do much
12825 about it. */
12826 *scroll_step = 1;
12827 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12828 }
12829 else
12830 {
12831 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12832 if (!cursor_row_fully_visible_p (w, 0, 1))
12833 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12834 else
12835 rc = CURSOR_MOVEMENT_SUCCESS;
12836 }
12837 }
12838 else if (scroll_p)
12839 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12840 else
12841 {
12842 do
12843 {
12844 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12845 {
12846 rc = CURSOR_MOVEMENT_SUCCESS;
12847 break;
12848 }
12849 ++row;
12850 }
12851 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12852 && MATRIX_ROW_START_CHARPOS (row) == PT
12853 && cursor_row_p (w, row));
12854 }
12855 }
12856 }
12857
12858 return rc;
12859 }
12860
12861 void
12862 set_vertical_scroll_bar (w)
12863 struct window *w;
12864 {
12865 int start, end, whole;
12866
12867 /* Calculate the start and end positions for the current window.
12868 At some point, it would be nice to choose between scrollbars
12869 which reflect the whole buffer size, with special markers
12870 indicating narrowing, and scrollbars which reflect only the
12871 visible region.
12872
12873 Note that mini-buffers sometimes aren't displaying any text. */
12874 if (!MINI_WINDOW_P (w)
12875 || (w == XWINDOW (minibuf_window)
12876 && NILP (echo_area_buffer[0])))
12877 {
12878 struct buffer *buf = XBUFFER (w->buffer);
12879 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12880 start = marker_position (w->start) - BUF_BEGV (buf);
12881 /* I don't think this is guaranteed to be right. For the
12882 moment, we'll pretend it is. */
12883 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12884
12885 if (end < start)
12886 end = start;
12887 if (whole < (end - start))
12888 whole = end - start;
12889 }
12890 else
12891 start = end = whole = 0;
12892
12893 /* Indicate what this scroll bar ought to be displaying now. */
12894 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12895 }
12896
12897
12898 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12899 selected_window is redisplayed.
12900
12901 We can return without actually redisplaying the window if
12902 fonts_changed_p is nonzero. In that case, redisplay_internal will
12903 retry. */
12904
12905 static void
12906 redisplay_window (window, just_this_one_p)
12907 Lisp_Object window;
12908 int just_this_one_p;
12909 {
12910 struct window *w = XWINDOW (window);
12911 struct frame *f = XFRAME (w->frame);
12912 struct buffer *buffer = XBUFFER (w->buffer);
12913 struct buffer *old = current_buffer;
12914 struct text_pos lpoint, opoint, startp;
12915 int update_mode_line;
12916 int tem;
12917 struct it it;
12918 /* Record it now because it's overwritten. */
12919 int current_matrix_up_to_date_p = 0;
12920 int used_current_matrix_p = 0;
12921 /* This is less strict than current_matrix_up_to_date_p.
12922 It indictes that the buffer contents and narrowing are unchanged. */
12923 int buffer_unchanged_p = 0;
12924 int temp_scroll_step = 0;
12925 int count = SPECPDL_INDEX ();
12926 int rc;
12927 int centering_position = -1;
12928 int last_line_misfit = 0;
12929
12930 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12931 opoint = lpoint;
12932
12933 /* W must be a leaf window here. */
12934 xassert (!NILP (w->buffer));
12935 #if GLYPH_DEBUG
12936 *w->desired_matrix->method = 0;
12937 #endif
12938
12939 specbind (Qinhibit_point_motion_hooks, Qt);
12940
12941 reconsider_clip_changes (w, buffer);
12942
12943 /* Has the mode line to be updated? */
12944 update_mode_line = (!NILP (w->update_mode_line)
12945 || update_mode_lines
12946 || buffer->clip_changed
12947 || buffer->prevent_redisplay_optimizations_p);
12948
12949 if (MINI_WINDOW_P (w))
12950 {
12951 if (w == XWINDOW (echo_area_window)
12952 && !NILP (echo_area_buffer[0]))
12953 {
12954 if (update_mode_line)
12955 /* We may have to update a tty frame's menu bar or a
12956 tool-bar. Example `M-x C-h C-h C-g'. */
12957 goto finish_menu_bars;
12958 else
12959 /* We've already displayed the echo area glyphs in this window. */
12960 goto finish_scroll_bars;
12961 }
12962 else if ((w != XWINDOW (minibuf_window)
12963 || minibuf_level == 0)
12964 /* When buffer is nonempty, redisplay window normally. */
12965 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12966 /* Quail displays non-mini buffers in minibuffer window.
12967 In that case, redisplay the window normally. */
12968 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12969 {
12970 /* W is a mini-buffer window, but it's not active, so clear
12971 it. */
12972 int yb = window_text_bottom_y (w);
12973 struct glyph_row *row;
12974 int y;
12975
12976 for (y = 0, row = w->desired_matrix->rows;
12977 y < yb;
12978 y += row->height, ++row)
12979 blank_row (w, row, y);
12980 goto finish_scroll_bars;
12981 }
12982
12983 clear_glyph_matrix (w->desired_matrix);
12984 }
12985
12986 /* Otherwise set up data on this window; select its buffer and point
12987 value. */
12988 /* Really select the buffer, for the sake of buffer-local
12989 variables. */
12990 set_buffer_internal_1 (XBUFFER (w->buffer));
12991 SET_TEXT_POS (opoint, PT, PT_BYTE);
12992
12993 current_matrix_up_to_date_p
12994 = (!NILP (w->window_end_valid)
12995 && !current_buffer->clip_changed
12996 && !current_buffer->prevent_redisplay_optimizations_p
12997 && XFASTINT (w->last_modified) >= MODIFF
12998 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12999
13000 buffer_unchanged_p
13001 = (!NILP (w->window_end_valid)
13002 && !current_buffer->clip_changed
13003 && XFASTINT (w->last_modified) >= MODIFF
13004 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13005
13006 /* When windows_or_buffers_changed is non-zero, we can't rely on
13007 the window end being valid, so set it to nil there. */
13008 if (windows_or_buffers_changed)
13009 {
13010 /* If window starts on a continuation line, maybe adjust the
13011 window start in case the window's width changed. */
13012 if (XMARKER (w->start)->buffer == current_buffer)
13013 compute_window_start_on_continuation_line (w);
13014
13015 w->window_end_valid = Qnil;
13016 }
13017
13018 /* Some sanity checks. */
13019 CHECK_WINDOW_END (w);
13020 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13021 abort ();
13022 if (BYTEPOS (opoint) < CHARPOS (opoint))
13023 abort ();
13024
13025 /* If %c is in mode line, update it if needed. */
13026 if (!NILP (w->column_number_displayed)
13027 /* This alternative quickly identifies a common case
13028 where no change is needed. */
13029 && !(PT == XFASTINT (w->last_point)
13030 && XFASTINT (w->last_modified) >= MODIFF
13031 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13032 && (XFASTINT (w->column_number_displayed)
13033 != (int) current_column ())) /* iftc */
13034 update_mode_line = 1;
13035
13036 /* Count number of windows showing the selected buffer. An indirect
13037 buffer counts as its base buffer. */
13038 if (!just_this_one_p)
13039 {
13040 struct buffer *current_base, *window_base;
13041 current_base = current_buffer;
13042 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13043 if (current_base->base_buffer)
13044 current_base = current_base->base_buffer;
13045 if (window_base->base_buffer)
13046 window_base = window_base->base_buffer;
13047 if (current_base == window_base)
13048 buffer_shared++;
13049 }
13050
13051 /* Point refers normally to the selected window. For any other
13052 window, set up appropriate value. */
13053 if (!EQ (window, selected_window))
13054 {
13055 int new_pt = XMARKER (w->pointm)->charpos;
13056 int new_pt_byte = marker_byte_position (w->pointm);
13057 if (new_pt < BEGV)
13058 {
13059 new_pt = BEGV;
13060 new_pt_byte = BEGV_BYTE;
13061 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13062 }
13063 else if (new_pt > (ZV - 1))
13064 {
13065 new_pt = ZV;
13066 new_pt_byte = ZV_BYTE;
13067 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13068 }
13069
13070 /* We don't use SET_PT so that the point-motion hooks don't run. */
13071 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13072 }
13073
13074 /* If any of the character widths specified in the display table
13075 have changed, invalidate the width run cache. It's true that
13076 this may be a bit late to catch such changes, but the rest of
13077 redisplay goes (non-fatally) haywire when the display table is
13078 changed, so why should we worry about doing any better? */
13079 if (current_buffer->width_run_cache)
13080 {
13081 struct Lisp_Char_Table *disptab = buffer_display_table ();
13082
13083 if (! disptab_matches_widthtab (disptab,
13084 XVECTOR (current_buffer->width_table)))
13085 {
13086 invalidate_region_cache (current_buffer,
13087 current_buffer->width_run_cache,
13088 BEG, Z);
13089 recompute_width_table (current_buffer, disptab);
13090 }
13091 }
13092
13093 /* If window-start is screwed up, choose a new one. */
13094 if (XMARKER (w->start)->buffer != current_buffer)
13095 goto recenter;
13096
13097 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13098
13099 /* If someone specified a new starting point but did not insist,
13100 check whether it can be used. */
13101 if (!NILP (w->optional_new_start)
13102 && CHARPOS (startp) >= BEGV
13103 && CHARPOS (startp) <= ZV)
13104 {
13105 w->optional_new_start = Qnil;
13106 start_display (&it, w, startp);
13107 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13108 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13109 if (IT_CHARPOS (it) == PT)
13110 w->force_start = Qt;
13111 /* IT may overshoot PT if text at PT is invisible. */
13112 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13113 w->force_start = Qt;
13114 }
13115
13116 /* Handle case where place to start displaying has been specified,
13117 unless the specified location is outside the accessible range. */
13118 if (!NILP (w->force_start)
13119 || w->frozen_window_start_p)
13120 {
13121 /* We set this later on if we have to adjust point. */
13122 int new_vpos = -1;
13123 int val;
13124
13125 w->force_start = Qnil;
13126 w->vscroll = 0;
13127 w->window_end_valid = Qnil;
13128
13129 /* Forget any recorded base line for line number display. */
13130 if (!buffer_unchanged_p)
13131 w->base_line_number = Qnil;
13132
13133 /* Redisplay the mode line. Select the buffer properly for that.
13134 Also, run the hook window-scroll-functions
13135 because we have scrolled. */
13136 /* Note, we do this after clearing force_start because
13137 if there's an error, it is better to forget about force_start
13138 than to get into an infinite loop calling the hook functions
13139 and having them get more errors. */
13140 if (!update_mode_line
13141 || ! NILP (Vwindow_scroll_functions))
13142 {
13143 update_mode_line = 1;
13144 w->update_mode_line = Qt;
13145 startp = run_window_scroll_functions (window, startp);
13146 }
13147
13148 w->last_modified = make_number (0);
13149 w->last_overlay_modified = make_number (0);
13150 if (CHARPOS (startp) < BEGV)
13151 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13152 else if (CHARPOS (startp) > ZV)
13153 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13154
13155 /* Redisplay, then check if cursor has been set during the
13156 redisplay. Give up if new fonts were loaded. */
13157 val = try_window (window, startp, 1);
13158 if (!val)
13159 {
13160 w->force_start = Qt;
13161 clear_glyph_matrix (w->desired_matrix);
13162 goto need_larger_matrices;
13163 }
13164 /* Point was outside the scroll margins. */
13165 if (val < 0)
13166 new_vpos = window_box_height (w) / 2;
13167
13168 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13169 {
13170 /* If point does not appear, try to move point so it does
13171 appear. The desired matrix has been built above, so we
13172 can use it here. */
13173 new_vpos = window_box_height (w) / 2;
13174 }
13175
13176 if (!cursor_row_fully_visible_p (w, 0, 0))
13177 {
13178 /* Point does appear, but on a line partly visible at end of window.
13179 Move it back to a fully-visible line. */
13180 new_vpos = window_box_height (w);
13181 }
13182
13183 /* If we need to move point for either of the above reasons,
13184 now actually do it. */
13185 if (new_vpos >= 0)
13186 {
13187 struct glyph_row *row;
13188
13189 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13190 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13191 ++row;
13192
13193 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13194 MATRIX_ROW_START_BYTEPOS (row));
13195
13196 if (w != XWINDOW (selected_window))
13197 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13198 else if (current_buffer == old)
13199 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13200
13201 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13202
13203 /* If we are highlighting the region, then we just changed
13204 the region, so redisplay to show it. */
13205 if (!NILP (Vtransient_mark_mode)
13206 && !NILP (current_buffer->mark_active))
13207 {
13208 clear_glyph_matrix (w->desired_matrix);
13209 if (!try_window (window, startp, 0))
13210 goto need_larger_matrices;
13211 }
13212 }
13213
13214 #if GLYPH_DEBUG
13215 debug_method_add (w, "forced window start");
13216 #endif
13217 goto done;
13218 }
13219
13220 /* Handle case where text has not changed, only point, and it has
13221 not moved off the frame, and we are not retrying after hscroll.
13222 (current_matrix_up_to_date_p is nonzero when retrying.) */
13223 if (current_matrix_up_to_date_p
13224 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13225 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13226 {
13227 switch (rc)
13228 {
13229 case CURSOR_MOVEMENT_SUCCESS:
13230 used_current_matrix_p = 1;
13231 goto done;
13232
13233 #if 0 /* try_cursor_movement never returns this value. */
13234 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13235 goto need_larger_matrices;
13236 #endif
13237
13238 case CURSOR_MOVEMENT_MUST_SCROLL:
13239 goto try_to_scroll;
13240
13241 default:
13242 abort ();
13243 }
13244 }
13245 /* If current starting point was originally the beginning of a line
13246 but no longer is, find a new starting point. */
13247 else if (!NILP (w->start_at_line_beg)
13248 && !(CHARPOS (startp) <= BEGV
13249 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13250 {
13251 #if GLYPH_DEBUG
13252 debug_method_add (w, "recenter 1");
13253 #endif
13254 goto recenter;
13255 }
13256
13257 /* Try scrolling with try_window_id. Value is > 0 if update has
13258 been done, it is -1 if we know that the same window start will
13259 not work. It is 0 if unsuccessful for some other reason. */
13260 else if ((tem = try_window_id (w)) != 0)
13261 {
13262 #if GLYPH_DEBUG
13263 debug_method_add (w, "try_window_id %d", tem);
13264 #endif
13265
13266 if (fonts_changed_p)
13267 goto need_larger_matrices;
13268 if (tem > 0)
13269 goto done;
13270
13271 /* Otherwise try_window_id has returned -1 which means that we
13272 don't want the alternative below this comment to execute. */
13273 }
13274 else if (CHARPOS (startp) >= BEGV
13275 && CHARPOS (startp) <= ZV
13276 && PT >= CHARPOS (startp)
13277 && (CHARPOS (startp) < ZV
13278 /* Avoid starting at end of buffer. */
13279 || CHARPOS (startp) == BEGV
13280 || (XFASTINT (w->last_modified) >= MODIFF
13281 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13282 {
13283
13284 /* If first window line is a continuation line, and window start
13285 is inside the modified region, but the first change is before
13286 current window start, we must select a new window start.*/
13287 if (NILP (w->start_at_line_beg)
13288 && CHARPOS (startp) > BEGV)
13289 {
13290 /* Make sure beg_unchanged and end_unchanged are up to date.
13291 Do it only if buffer has really changed. This may or may
13292 not have been done by try_window_id (see which) already. */
13293 if (MODIFF > SAVE_MODIFF
13294 /* This seems to happen sometimes after saving a buffer. */
13295 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13296 {
13297 if (GPT - BEG < BEG_UNCHANGED)
13298 BEG_UNCHANGED = GPT - BEG;
13299 if (Z - GPT < END_UNCHANGED)
13300 END_UNCHANGED = Z - GPT;
13301 }
13302
13303 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
13304 && CHARPOS (startp) <= Z - END_UNCHANGED)
13305 {
13306 /* There doesn't seems to be a simple way to find a new
13307 window start that is near the old window start, so
13308 we just recenter. */
13309 goto recenter;
13310 }
13311 }
13312
13313 #if GLYPH_DEBUG
13314 debug_method_add (w, "same window start");
13315 #endif
13316
13317 /* Try to redisplay starting at same place as before.
13318 If point has not moved off frame, accept the results. */
13319 if (!current_matrix_up_to_date_p
13320 /* Don't use try_window_reusing_current_matrix in this case
13321 because a window scroll function can have changed the
13322 buffer. */
13323 || !NILP (Vwindow_scroll_functions)
13324 || MINI_WINDOW_P (w)
13325 || !(used_current_matrix_p
13326 = try_window_reusing_current_matrix (w)))
13327 {
13328 IF_DEBUG (debug_method_add (w, "1"));
13329 if (try_window (window, startp, 1) < 0)
13330 /* -1 means we need to scroll.
13331 0 means we need new matrices, but fonts_changed_p
13332 is set in that case, so we will detect it below. */
13333 goto try_to_scroll;
13334 }
13335
13336 if (fonts_changed_p)
13337 goto need_larger_matrices;
13338
13339 if (w->cursor.vpos >= 0)
13340 {
13341 if (!just_this_one_p
13342 || current_buffer->clip_changed
13343 || BEG_UNCHANGED < CHARPOS (startp))
13344 /* Forget any recorded base line for line number display. */
13345 w->base_line_number = Qnil;
13346
13347 if (!cursor_row_fully_visible_p (w, 1, 0))
13348 {
13349 clear_glyph_matrix (w->desired_matrix);
13350 last_line_misfit = 1;
13351 }
13352 /* Drop through and scroll. */
13353 else
13354 goto done;
13355 }
13356 else
13357 clear_glyph_matrix (w->desired_matrix);
13358 }
13359
13360 try_to_scroll:
13361
13362 w->last_modified = make_number (0);
13363 w->last_overlay_modified = make_number (0);
13364
13365 /* Redisplay the mode line. Select the buffer properly for that. */
13366 if (!update_mode_line)
13367 {
13368 update_mode_line = 1;
13369 w->update_mode_line = Qt;
13370 }
13371
13372 /* Try to scroll by specified few lines. */
13373 if ((scroll_conservatively
13374 || scroll_step
13375 || temp_scroll_step
13376 || NUMBERP (current_buffer->scroll_up_aggressively)
13377 || NUMBERP (current_buffer->scroll_down_aggressively))
13378 && !current_buffer->clip_changed
13379 && CHARPOS (startp) >= BEGV
13380 && CHARPOS (startp) <= ZV)
13381 {
13382 /* The function returns -1 if new fonts were loaded, 1 if
13383 successful, 0 if not successful. */
13384 int rc = try_scrolling (window, just_this_one_p,
13385 scroll_conservatively,
13386 scroll_step,
13387 temp_scroll_step, last_line_misfit);
13388 switch (rc)
13389 {
13390 case SCROLLING_SUCCESS:
13391 goto done;
13392
13393 case SCROLLING_NEED_LARGER_MATRICES:
13394 goto need_larger_matrices;
13395
13396 case SCROLLING_FAILED:
13397 break;
13398
13399 default:
13400 abort ();
13401 }
13402 }
13403
13404 /* Finally, just choose place to start which centers point */
13405
13406 recenter:
13407 if (centering_position < 0)
13408 centering_position = window_box_height (w) / 2;
13409
13410 #if GLYPH_DEBUG
13411 debug_method_add (w, "recenter");
13412 #endif
13413
13414 /* w->vscroll = 0; */
13415
13416 /* Forget any previously recorded base line for line number display. */
13417 if (!buffer_unchanged_p)
13418 w->base_line_number = Qnil;
13419
13420 /* Move backward half the height of the window. */
13421 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13422 it.current_y = it.last_visible_y;
13423 move_it_vertically_backward (&it, centering_position);
13424 xassert (IT_CHARPOS (it) >= BEGV);
13425
13426 /* The function move_it_vertically_backward may move over more
13427 than the specified y-distance. If it->w is small, e.g. a
13428 mini-buffer window, we may end up in front of the window's
13429 display area. Start displaying at the start of the line
13430 containing PT in this case. */
13431 if (it.current_y <= 0)
13432 {
13433 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13434 move_it_vertically_backward (&it, 0);
13435 #if 0
13436 /* I think this assert is bogus if buffer contains
13437 invisible text or images. KFS. */
13438 xassert (IT_CHARPOS (it) <= PT);
13439 #endif
13440 it.current_y = 0;
13441 }
13442
13443 it.current_x = it.hpos = 0;
13444
13445 /* Set startp here explicitly in case that helps avoid an infinite loop
13446 in case the window-scroll-functions functions get errors. */
13447 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13448
13449 /* Run scroll hooks. */
13450 startp = run_window_scroll_functions (window, it.current.pos);
13451
13452 /* Redisplay the window. */
13453 if (!current_matrix_up_to_date_p
13454 || windows_or_buffers_changed
13455 || cursor_type_changed
13456 /* Don't use try_window_reusing_current_matrix in this case
13457 because it can have changed the buffer. */
13458 || !NILP (Vwindow_scroll_functions)
13459 || !just_this_one_p
13460 || MINI_WINDOW_P (w)
13461 || !(used_current_matrix_p
13462 = try_window_reusing_current_matrix (w)))
13463 try_window (window, startp, 0);
13464
13465 /* If new fonts have been loaded (due to fontsets), give up. We
13466 have to start a new redisplay since we need to re-adjust glyph
13467 matrices. */
13468 if (fonts_changed_p)
13469 goto need_larger_matrices;
13470
13471 /* If cursor did not appear assume that the middle of the window is
13472 in the first line of the window. Do it again with the next line.
13473 (Imagine a window of height 100, displaying two lines of height
13474 60. Moving back 50 from it->last_visible_y will end in the first
13475 line.) */
13476 if (w->cursor.vpos < 0)
13477 {
13478 if (!NILP (w->window_end_valid)
13479 && PT >= Z - XFASTINT (w->window_end_pos))
13480 {
13481 clear_glyph_matrix (w->desired_matrix);
13482 move_it_by_lines (&it, 1, 0);
13483 try_window (window, it.current.pos, 0);
13484 }
13485 else if (PT < IT_CHARPOS (it))
13486 {
13487 clear_glyph_matrix (w->desired_matrix);
13488 move_it_by_lines (&it, -1, 0);
13489 try_window (window, it.current.pos, 0);
13490 }
13491 else
13492 {
13493 /* Not much we can do about it. */
13494 }
13495 }
13496
13497 /* Consider the following case: Window starts at BEGV, there is
13498 invisible, intangible text at BEGV, so that display starts at
13499 some point START > BEGV. It can happen that we are called with
13500 PT somewhere between BEGV and START. Try to handle that case. */
13501 if (w->cursor.vpos < 0)
13502 {
13503 struct glyph_row *row = w->current_matrix->rows;
13504 if (row->mode_line_p)
13505 ++row;
13506 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13507 }
13508
13509 if (!cursor_row_fully_visible_p (w, 0, 0))
13510 {
13511 /* If vscroll is enabled, disable it and try again. */
13512 if (w->vscroll)
13513 {
13514 w->vscroll = 0;
13515 clear_glyph_matrix (w->desired_matrix);
13516 goto recenter;
13517 }
13518
13519 /* If centering point failed to make the whole line visible,
13520 put point at the top instead. That has to make the whole line
13521 visible, if it can be done. */
13522 if (centering_position == 0)
13523 goto done;
13524
13525 clear_glyph_matrix (w->desired_matrix);
13526 centering_position = 0;
13527 goto recenter;
13528 }
13529
13530 done:
13531
13532 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13533 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13534 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13535 ? Qt : Qnil);
13536
13537 /* Display the mode line, if we must. */
13538 if ((update_mode_line
13539 /* If window not full width, must redo its mode line
13540 if (a) the window to its side is being redone and
13541 (b) we do a frame-based redisplay. This is a consequence
13542 of how inverted lines are drawn in frame-based redisplay. */
13543 || (!just_this_one_p
13544 && !FRAME_WINDOW_P (f)
13545 && !WINDOW_FULL_WIDTH_P (w))
13546 /* Line number to display. */
13547 || INTEGERP (w->base_line_pos)
13548 /* Column number is displayed and different from the one displayed. */
13549 || (!NILP (w->column_number_displayed)
13550 && (XFASTINT (w->column_number_displayed)
13551 != (int) current_column ()))) /* iftc */
13552 /* This means that the window has a mode line. */
13553 && (WINDOW_WANTS_MODELINE_P (w)
13554 || WINDOW_WANTS_HEADER_LINE_P (w)))
13555 {
13556 display_mode_lines (w);
13557
13558 /* If mode line height has changed, arrange for a thorough
13559 immediate redisplay using the correct mode line height. */
13560 if (WINDOW_WANTS_MODELINE_P (w)
13561 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13562 {
13563 fonts_changed_p = 1;
13564 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13565 = DESIRED_MODE_LINE_HEIGHT (w);
13566 }
13567
13568 /* If top line height has changed, arrange for a thorough
13569 immediate redisplay using the correct mode line height. */
13570 if (WINDOW_WANTS_HEADER_LINE_P (w)
13571 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13572 {
13573 fonts_changed_p = 1;
13574 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13575 = DESIRED_HEADER_LINE_HEIGHT (w);
13576 }
13577
13578 if (fonts_changed_p)
13579 goto need_larger_matrices;
13580 }
13581
13582 if (!line_number_displayed
13583 && !BUFFERP (w->base_line_pos))
13584 {
13585 w->base_line_pos = Qnil;
13586 w->base_line_number = Qnil;
13587 }
13588
13589 finish_menu_bars:
13590
13591 /* When we reach a frame's selected window, redo the frame's menu bar. */
13592 if (update_mode_line
13593 && EQ (FRAME_SELECTED_WINDOW (f), window))
13594 {
13595 int redisplay_menu_p = 0;
13596 int redisplay_tool_bar_p = 0;
13597
13598 if (FRAME_WINDOW_P (f))
13599 {
13600 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13601 || defined (USE_GTK)
13602 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13603 #else
13604 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13605 #endif
13606 }
13607 else
13608 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13609
13610 if (redisplay_menu_p)
13611 display_menu_bar (w);
13612
13613 #ifdef HAVE_WINDOW_SYSTEM
13614 #ifdef USE_GTK
13615 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13616 #else
13617 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13618 && (FRAME_TOOL_BAR_LINES (f) > 0
13619 || !NILP (Vauto_resize_tool_bars));
13620
13621 #endif
13622
13623 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13624 {
13625 extern int ignore_mouse_drag_p;
13626 ignore_mouse_drag_p = 1;
13627 }
13628 #endif
13629 }
13630
13631 #ifdef HAVE_WINDOW_SYSTEM
13632 if (FRAME_WINDOW_P (f)
13633 && update_window_fringes (w, (just_this_one_p
13634 || (!used_current_matrix_p && !overlay_arrow_seen)
13635 || w->pseudo_window_p)))
13636 {
13637 update_begin (f);
13638 BLOCK_INPUT;
13639 if (draw_window_fringes (w, 1))
13640 x_draw_vertical_border (w);
13641 UNBLOCK_INPUT;
13642 update_end (f);
13643 }
13644 #endif /* HAVE_WINDOW_SYSTEM */
13645
13646 /* We go to this label, with fonts_changed_p nonzero,
13647 if it is necessary to try again using larger glyph matrices.
13648 We have to redeem the scroll bar even in this case,
13649 because the loop in redisplay_internal expects that. */
13650 need_larger_matrices:
13651 ;
13652 finish_scroll_bars:
13653
13654 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13655 {
13656 /* Set the thumb's position and size. */
13657 set_vertical_scroll_bar (w);
13658
13659 /* Note that we actually used the scroll bar attached to this
13660 window, so it shouldn't be deleted at the end of redisplay. */
13661 redeem_scroll_bar_hook (w);
13662 }
13663
13664 /* Restore current_buffer and value of point in it. */
13665 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13666 set_buffer_internal_1 (old);
13667 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13668
13669 unbind_to (count, Qnil);
13670 }
13671
13672
13673 /* Build the complete desired matrix of WINDOW with a window start
13674 buffer position POS.
13675
13676 Value is 1 if successful. It is zero if fonts were loaded during
13677 redisplay which makes re-adjusting glyph matrices necessary, and -1
13678 if point would appear in the scroll margins.
13679 (We check that only if CHECK_MARGINS is nonzero. */
13680
13681 int
13682 try_window (window, pos, check_margins)
13683 Lisp_Object window;
13684 struct text_pos pos;
13685 int check_margins;
13686 {
13687 struct window *w = XWINDOW (window);
13688 struct it it;
13689 struct glyph_row *last_text_row = NULL;
13690 struct frame *f = XFRAME (w->frame);
13691
13692 /* Make POS the new window start. */
13693 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13694
13695 /* Mark cursor position as unknown. No overlay arrow seen. */
13696 w->cursor.vpos = -1;
13697 overlay_arrow_seen = 0;
13698
13699 /* Initialize iterator and info to start at POS. */
13700 start_display (&it, w, pos);
13701
13702 /* Display all lines of W. */
13703 while (it.current_y < it.last_visible_y)
13704 {
13705 if (display_line (&it))
13706 last_text_row = it.glyph_row - 1;
13707 if (fonts_changed_p)
13708 return 0;
13709 }
13710
13711 /* Don't let the cursor end in the scroll margins. */
13712 if (check_margins
13713 && !MINI_WINDOW_P (w))
13714 {
13715 int this_scroll_margin;
13716
13717 this_scroll_margin = max (0, scroll_margin);
13718 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13719 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13720
13721 if ((w->cursor.y >= 0 /* not vscrolled */
13722 && w->cursor.y < this_scroll_margin
13723 && CHARPOS (pos) > BEGV
13724 && IT_CHARPOS (it) < ZV)
13725 /* rms: considering make_cursor_line_fully_visible_p here
13726 seems to give wrong results. We don't want to recenter
13727 when the last line is partly visible, we want to allow
13728 that case to be handled in the usual way. */
13729 || (w->cursor.y + 1) > it.last_visible_y)
13730 {
13731 w->cursor.vpos = -1;
13732 clear_glyph_matrix (w->desired_matrix);
13733 return -1;
13734 }
13735 }
13736
13737 /* If bottom moved off end of frame, change mode line percentage. */
13738 if (XFASTINT (w->window_end_pos) <= 0
13739 && Z != IT_CHARPOS (it))
13740 w->update_mode_line = Qt;
13741
13742 /* Set window_end_pos to the offset of the last character displayed
13743 on the window from the end of current_buffer. Set
13744 window_end_vpos to its row number. */
13745 if (last_text_row)
13746 {
13747 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13748 w->window_end_bytepos
13749 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13750 w->window_end_pos
13751 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13752 w->window_end_vpos
13753 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13754 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13755 ->displays_text_p);
13756 }
13757 else
13758 {
13759 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13760 w->window_end_pos = make_number (Z - ZV);
13761 w->window_end_vpos = make_number (0);
13762 }
13763
13764 /* But that is not valid info until redisplay finishes. */
13765 w->window_end_valid = Qnil;
13766 return 1;
13767 }
13768
13769
13770 \f
13771 /************************************************************************
13772 Window redisplay reusing current matrix when buffer has not changed
13773 ************************************************************************/
13774
13775 /* Try redisplay of window W showing an unchanged buffer with a
13776 different window start than the last time it was displayed by
13777 reusing its current matrix. Value is non-zero if successful.
13778 W->start is the new window start. */
13779
13780 static int
13781 try_window_reusing_current_matrix (w)
13782 struct window *w;
13783 {
13784 struct frame *f = XFRAME (w->frame);
13785 struct glyph_row *row, *bottom_row;
13786 struct it it;
13787 struct run run;
13788 struct text_pos start, new_start;
13789 int nrows_scrolled, i;
13790 struct glyph_row *last_text_row;
13791 struct glyph_row *last_reused_text_row;
13792 struct glyph_row *start_row;
13793 int start_vpos, min_y, max_y;
13794
13795 #if GLYPH_DEBUG
13796 if (inhibit_try_window_reusing)
13797 return 0;
13798 #endif
13799
13800 if (/* This function doesn't handle terminal frames. */
13801 !FRAME_WINDOW_P (f)
13802 /* Don't try to reuse the display if windows have been split
13803 or such. */
13804 || windows_or_buffers_changed
13805 || cursor_type_changed)
13806 return 0;
13807
13808 /* Can't do this if region may have changed. */
13809 if ((!NILP (Vtransient_mark_mode)
13810 && !NILP (current_buffer->mark_active))
13811 || !NILP (w->region_showing)
13812 || !NILP (Vshow_trailing_whitespace))
13813 return 0;
13814
13815 /* If top-line visibility has changed, give up. */
13816 if (WINDOW_WANTS_HEADER_LINE_P (w)
13817 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13818 return 0;
13819
13820 /* Give up if old or new display is scrolled vertically. We could
13821 make this function handle this, but right now it doesn't. */
13822 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13823 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13824 return 0;
13825
13826 /* The variable new_start now holds the new window start. The old
13827 start `start' can be determined from the current matrix. */
13828 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13829 start = start_row->start.pos;
13830 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13831
13832 /* Clear the desired matrix for the display below. */
13833 clear_glyph_matrix (w->desired_matrix);
13834
13835 if (CHARPOS (new_start) <= CHARPOS (start))
13836 {
13837 int first_row_y;
13838
13839 /* Don't use this method if the display starts with an ellipsis
13840 displayed for invisible text. It's not easy to handle that case
13841 below, and it's certainly not worth the effort since this is
13842 not a frequent case. */
13843 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13844 return 0;
13845
13846 IF_DEBUG (debug_method_add (w, "twu1"));
13847
13848 /* Display up to a row that can be reused. The variable
13849 last_text_row is set to the last row displayed that displays
13850 text. Note that it.vpos == 0 if or if not there is a
13851 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13852 start_display (&it, w, new_start);
13853 first_row_y = it.current_y;
13854 w->cursor.vpos = -1;
13855 last_text_row = last_reused_text_row = NULL;
13856
13857 while (it.current_y < it.last_visible_y
13858 && !fonts_changed_p)
13859 {
13860 /* If we have reached into the characters in the START row,
13861 that means the line boundaries have changed. So we
13862 can't start copying with the row START. Maybe it will
13863 work to start copying with the following row. */
13864 while (IT_CHARPOS (it) > CHARPOS (start))
13865 {
13866 /* Advance to the next row as the "start". */
13867 start_row++;
13868 start = start_row->start.pos;
13869 /* If there are no more rows to try, or just one, give up. */
13870 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13871 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13872 || CHARPOS (start) == ZV)
13873 {
13874 clear_glyph_matrix (w->desired_matrix);
13875 return 0;
13876 }
13877
13878 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13879 }
13880 /* If we have reached alignment,
13881 we can copy the rest of the rows. */
13882 if (IT_CHARPOS (it) == CHARPOS (start))
13883 break;
13884
13885 if (display_line (&it))
13886 last_text_row = it.glyph_row - 1;
13887 }
13888
13889 /* A value of current_y < last_visible_y means that we stopped
13890 at the previous window start, which in turn means that we
13891 have at least one reusable row. */
13892 if (it.current_y < it.last_visible_y)
13893 {
13894 /* IT.vpos always starts from 0; it counts text lines. */
13895 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13896
13897 /* Find PT if not already found in the lines displayed. */
13898 if (w->cursor.vpos < 0)
13899 {
13900 int dy = it.current_y - start_row->y;
13901
13902 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13903 row = row_containing_pos (w, PT, row, NULL, dy);
13904 if (row)
13905 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13906 dy, nrows_scrolled);
13907 else
13908 {
13909 clear_glyph_matrix (w->desired_matrix);
13910 return 0;
13911 }
13912 }
13913
13914 /* Scroll the display. Do it before the current matrix is
13915 changed. The problem here is that update has not yet
13916 run, i.e. part of the current matrix is not up to date.
13917 scroll_run_hook will clear the cursor, and use the
13918 current matrix to get the height of the row the cursor is
13919 in. */
13920 run.current_y = start_row->y;
13921 run.desired_y = it.current_y;
13922 run.height = it.last_visible_y - it.current_y;
13923
13924 if (run.height > 0 && run.current_y != run.desired_y)
13925 {
13926 update_begin (f);
13927 rif->update_window_begin_hook (w);
13928 rif->clear_window_mouse_face (w);
13929 rif->scroll_run_hook (w, &run);
13930 rif->update_window_end_hook (w, 0, 0);
13931 update_end (f);
13932 }
13933
13934 /* Shift current matrix down by nrows_scrolled lines. */
13935 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13936 rotate_matrix (w->current_matrix,
13937 start_vpos,
13938 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13939 nrows_scrolled);
13940
13941 /* Disable lines that must be updated. */
13942 for (i = 0; i < it.vpos; ++i)
13943 (start_row + i)->enabled_p = 0;
13944
13945 /* Re-compute Y positions. */
13946 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13947 max_y = it.last_visible_y;
13948 for (row = start_row + nrows_scrolled;
13949 row < bottom_row;
13950 ++row)
13951 {
13952 row->y = it.current_y;
13953 row->visible_height = row->height;
13954
13955 if (row->y < min_y)
13956 row->visible_height -= min_y - row->y;
13957 if (row->y + row->height > max_y)
13958 row->visible_height -= row->y + row->height - max_y;
13959 row->redraw_fringe_bitmaps_p = 1;
13960
13961 it.current_y += row->height;
13962
13963 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13964 last_reused_text_row = row;
13965 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13966 break;
13967 }
13968
13969 /* Disable lines in the current matrix which are now
13970 below the window. */
13971 for (++row; row < bottom_row; ++row)
13972 row->enabled_p = row->mode_line_p = 0;
13973 }
13974
13975 /* Update window_end_pos etc.; last_reused_text_row is the last
13976 reused row from the current matrix containing text, if any.
13977 The value of last_text_row is the last displayed line
13978 containing text. */
13979 if (last_reused_text_row)
13980 {
13981 w->window_end_bytepos
13982 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13983 w->window_end_pos
13984 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13985 w->window_end_vpos
13986 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13987 w->current_matrix));
13988 }
13989 else if (last_text_row)
13990 {
13991 w->window_end_bytepos
13992 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13993 w->window_end_pos
13994 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13995 w->window_end_vpos
13996 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13997 }
13998 else
13999 {
14000 /* This window must be completely empty. */
14001 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14002 w->window_end_pos = make_number (Z - ZV);
14003 w->window_end_vpos = make_number (0);
14004 }
14005 w->window_end_valid = Qnil;
14006
14007 /* Update hint: don't try scrolling again in update_window. */
14008 w->desired_matrix->no_scrolling_p = 1;
14009
14010 #if GLYPH_DEBUG
14011 debug_method_add (w, "try_window_reusing_current_matrix 1");
14012 #endif
14013 return 1;
14014 }
14015 else if (CHARPOS (new_start) > CHARPOS (start))
14016 {
14017 struct glyph_row *pt_row, *row;
14018 struct glyph_row *first_reusable_row;
14019 struct glyph_row *first_row_to_display;
14020 int dy;
14021 int yb = window_text_bottom_y (w);
14022
14023 /* Find the row starting at new_start, if there is one. Don't
14024 reuse a partially visible line at the end. */
14025 first_reusable_row = start_row;
14026 while (first_reusable_row->enabled_p
14027 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14028 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14029 < CHARPOS (new_start)))
14030 ++first_reusable_row;
14031
14032 /* Give up if there is no row to reuse. */
14033 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14034 || !first_reusable_row->enabled_p
14035 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14036 != CHARPOS (new_start)))
14037 return 0;
14038
14039 /* We can reuse fully visible rows beginning with
14040 first_reusable_row to the end of the window. Set
14041 first_row_to_display to the first row that cannot be reused.
14042 Set pt_row to the row containing point, if there is any. */
14043 pt_row = NULL;
14044 for (first_row_to_display = first_reusable_row;
14045 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14046 ++first_row_to_display)
14047 {
14048 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14049 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14050 pt_row = first_row_to_display;
14051 }
14052
14053 /* Start displaying at the start of first_row_to_display. */
14054 xassert (first_row_to_display->y < yb);
14055 init_to_row_start (&it, w, first_row_to_display);
14056
14057 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14058 - start_vpos);
14059 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14060 - nrows_scrolled);
14061 it.current_y = (first_row_to_display->y - first_reusable_row->y
14062 + WINDOW_HEADER_LINE_HEIGHT (w));
14063
14064 /* Display lines beginning with first_row_to_display in the
14065 desired matrix. Set last_text_row to the last row displayed
14066 that displays text. */
14067 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14068 if (pt_row == NULL)
14069 w->cursor.vpos = -1;
14070 last_text_row = NULL;
14071 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14072 if (display_line (&it))
14073 last_text_row = it.glyph_row - 1;
14074
14075 /* Give up If point isn't in a row displayed or reused. */
14076 if (w->cursor.vpos < 0)
14077 {
14078 clear_glyph_matrix (w->desired_matrix);
14079 return 0;
14080 }
14081
14082 /* If point is in a reused row, adjust y and vpos of the cursor
14083 position. */
14084 if (pt_row)
14085 {
14086 w->cursor.vpos -= nrows_scrolled;
14087 w->cursor.y -= first_reusable_row->y - start_row->y;
14088 }
14089
14090 /* Scroll the display. */
14091 run.current_y = first_reusable_row->y;
14092 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14093 run.height = it.last_visible_y - run.current_y;
14094 dy = run.current_y - run.desired_y;
14095
14096 if (run.height)
14097 {
14098 update_begin (f);
14099 rif->update_window_begin_hook (w);
14100 rif->clear_window_mouse_face (w);
14101 rif->scroll_run_hook (w, &run);
14102 rif->update_window_end_hook (w, 0, 0);
14103 update_end (f);
14104 }
14105
14106 /* Adjust Y positions of reused rows. */
14107 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14108 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14109 max_y = it.last_visible_y;
14110 for (row = first_reusable_row; row < first_row_to_display; ++row)
14111 {
14112 row->y -= dy;
14113 row->visible_height = row->height;
14114 if (row->y < min_y)
14115 row->visible_height -= min_y - row->y;
14116 if (row->y + row->height > max_y)
14117 row->visible_height -= row->y + row->height - max_y;
14118 row->redraw_fringe_bitmaps_p = 1;
14119 }
14120
14121 /* Scroll the current matrix. */
14122 xassert (nrows_scrolled > 0);
14123 rotate_matrix (w->current_matrix,
14124 start_vpos,
14125 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14126 -nrows_scrolled);
14127
14128 /* Disable rows not reused. */
14129 for (row -= nrows_scrolled; row < bottom_row; ++row)
14130 row->enabled_p = 0;
14131
14132 /* Point may have moved to a different line, so we cannot assume that
14133 the previous cursor position is valid; locate the correct row. */
14134 if (pt_row)
14135 {
14136 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14137 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14138 row++)
14139 {
14140 w->cursor.vpos++;
14141 w->cursor.y = row->y;
14142 }
14143 if (row < bottom_row)
14144 {
14145 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14146 while (glyph->charpos < PT)
14147 {
14148 w->cursor.hpos++;
14149 w->cursor.x += glyph->pixel_width;
14150 glyph++;
14151 }
14152 }
14153 }
14154
14155 /* Adjust window end. A null value of last_text_row means that
14156 the window end is in reused rows which in turn means that
14157 only its vpos can have changed. */
14158 if (last_text_row)
14159 {
14160 w->window_end_bytepos
14161 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14162 w->window_end_pos
14163 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14164 w->window_end_vpos
14165 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14166 }
14167 else
14168 {
14169 w->window_end_vpos
14170 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14171 }
14172
14173 w->window_end_valid = Qnil;
14174 w->desired_matrix->no_scrolling_p = 1;
14175
14176 #if GLYPH_DEBUG
14177 debug_method_add (w, "try_window_reusing_current_matrix 2");
14178 #endif
14179 return 1;
14180 }
14181
14182 return 0;
14183 }
14184
14185
14186 \f
14187 /************************************************************************
14188 Window redisplay reusing current matrix when buffer has changed
14189 ************************************************************************/
14190
14191 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14192 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14193 int *, int *));
14194 static struct glyph_row *
14195 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14196 struct glyph_row *));
14197
14198
14199 /* Return the last row in MATRIX displaying text. If row START is
14200 non-null, start searching with that row. IT gives the dimensions
14201 of the display. Value is null if matrix is empty; otherwise it is
14202 a pointer to the row found. */
14203
14204 static struct glyph_row *
14205 find_last_row_displaying_text (matrix, it, start)
14206 struct glyph_matrix *matrix;
14207 struct it *it;
14208 struct glyph_row *start;
14209 {
14210 struct glyph_row *row, *row_found;
14211
14212 /* Set row_found to the last row in IT->w's current matrix
14213 displaying text. The loop looks funny but think of partially
14214 visible lines. */
14215 row_found = NULL;
14216 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14217 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14218 {
14219 xassert (row->enabled_p);
14220 row_found = row;
14221 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14222 break;
14223 ++row;
14224 }
14225
14226 return row_found;
14227 }
14228
14229
14230 /* Return the last row in the current matrix of W that is not affected
14231 by changes at the start of current_buffer that occurred since W's
14232 current matrix was built. Value is null if no such row exists.
14233
14234 BEG_UNCHANGED us the number of characters unchanged at the start of
14235 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14236 first changed character in current_buffer. Characters at positions <
14237 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14238 when the current matrix was built. */
14239
14240 static struct glyph_row *
14241 find_last_unchanged_at_beg_row (w)
14242 struct window *w;
14243 {
14244 int first_changed_pos = BEG + BEG_UNCHANGED;
14245 struct glyph_row *row;
14246 struct glyph_row *row_found = NULL;
14247 int yb = window_text_bottom_y (w);
14248
14249 /* Find the last row displaying unchanged text. */
14250 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14251 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14252 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14253 {
14254 if (/* If row ends before first_changed_pos, it is unchanged,
14255 except in some case. */
14256 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14257 /* When row ends in ZV and we write at ZV it is not
14258 unchanged. */
14259 && !row->ends_at_zv_p
14260 /* When first_changed_pos is the end of a continued line,
14261 row is not unchanged because it may be no longer
14262 continued. */
14263 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14264 && (row->continued_p
14265 || row->exact_window_width_line_p)))
14266 row_found = row;
14267
14268 /* Stop if last visible row. */
14269 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14270 break;
14271
14272 ++row;
14273 }
14274
14275 return row_found;
14276 }
14277
14278
14279 /* Find the first glyph row in the current matrix of W that is not
14280 affected by changes at the end of current_buffer since the
14281 time W's current matrix was built.
14282
14283 Return in *DELTA the number of chars by which buffer positions in
14284 unchanged text at the end of current_buffer must be adjusted.
14285
14286 Return in *DELTA_BYTES the corresponding number of bytes.
14287
14288 Value is null if no such row exists, i.e. all rows are affected by
14289 changes. */
14290
14291 static struct glyph_row *
14292 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14293 struct window *w;
14294 int *delta, *delta_bytes;
14295 {
14296 struct glyph_row *row;
14297 struct glyph_row *row_found = NULL;
14298
14299 *delta = *delta_bytes = 0;
14300
14301 /* Display must not have been paused, otherwise the current matrix
14302 is not up to date. */
14303 if (NILP (w->window_end_valid))
14304 abort ();
14305
14306 /* A value of window_end_pos >= END_UNCHANGED means that the window
14307 end is in the range of changed text. If so, there is no
14308 unchanged row at the end of W's current matrix. */
14309 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14310 return NULL;
14311
14312 /* Set row to the last row in W's current matrix displaying text. */
14313 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14314
14315 /* If matrix is entirely empty, no unchanged row exists. */
14316 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14317 {
14318 /* The value of row is the last glyph row in the matrix having a
14319 meaningful buffer position in it. The end position of row
14320 corresponds to window_end_pos. This allows us to translate
14321 buffer positions in the current matrix to current buffer
14322 positions for characters not in changed text. */
14323 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14324 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14325 int last_unchanged_pos, last_unchanged_pos_old;
14326 struct glyph_row *first_text_row
14327 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14328
14329 *delta = Z - Z_old;
14330 *delta_bytes = Z_BYTE - Z_BYTE_old;
14331
14332 /* Set last_unchanged_pos to the buffer position of the last
14333 character in the buffer that has not been changed. Z is the
14334 index + 1 of the last character in current_buffer, i.e. by
14335 subtracting END_UNCHANGED we get the index of the last
14336 unchanged character, and we have to add BEG to get its buffer
14337 position. */
14338 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14339 last_unchanged_pos_old = last_unchanged_pos - *delta;
14340
14341 /* Search backward from ROW for a row displaying a line that
14342 starts at a minimum position >= last_unchanged_pos_old. */
14343 for (; row > first_text_row; --row)
14344 {
14345 /* This used to abort, but it can happen.
14346 It is ok to just stop the search instead here. KFS. */
14347 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14348 break;
14349
14350 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14351 row_found = row;
14352 }
14353 }
14354
14355 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14356 abort ();
14357
14358 return row_found;
14359 }
14360
14361
14362 /* Make sure that glyph rows in the current matrix of window W
14363 reference the same glyph memory as corresponding rows in the
14364 frame's frame matrix. This function is called after scrolling W's
14365 current matrix on a terminal frame in try_window_id and
14366 try_window_reusing_current_matrix. */
14367
14368 static void
14369 sync_frame_with_window_matrix_rows (w)
14370 struct window *w;
14371 {
14372 struct frame *f = XFRAME (w->frame);
14373 struct glyph_row *window_row, *window_row_end, *frame_row;
14374
14375 /* Preconditions: W must be a leaf window and full-width. Its frame
14376 must have a frame matrix. */
14377 xassert (NILP (w->hchild) && NILP (w->vchild));
14378 xassert (WINDOW_FULL_WIDTH_P (w));
14379 xassert (!FRAME_WINDOW_P (f));
14380
14381 /* If W is a full-width window, glyph pointers in W's current matrix
14382 have, by definition, to be the same as glyph pointers in the
14383 corresponding frame matrix. Note that frame matrices have no
14384 marginal areas (see build_frame_matrix). */
14385 window_row = w->current_matrix->rows;
14386 window_row_end = window_row + w->current_matrix->nrows;
14387 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14388 while (window_row < window_row_end)
14389 {
14390 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14391 struct glyph *end = window_row->glyphs[LAST_AREA];
14392
14393 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14394 frame_row->glyphs[TEXT_AREA] = start;
14395 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14396 frame_row->glyphs[LAST_AREA] = end;
14397
14398 /* Disable frame rows whose corresponding window rows have
14399 been disabled in try_window_id. */
14400 if (!window_row->enabled_p)
14401 frame_row->enabled_p = 0;
14402
14403 ++window_row, ++frame_row;
14404 }
14405 }
14406
14407
14408 /* Find the glyph row in window W containing CHARPOS. Consider all
14409 rows between START and END (not inclusive). END null means search
14410 all rows to the end of the display area of W. Value is the row
14411 containing CHARPOS or null. */
14412
14413 struct glyph_row *
14414 row_containing_pos (w, charpos, start, end, dy)
14415 struct window *w;
14416 int charpos;
14417 struct glyph_row *start, *end;
14418 int dy;
14419 {
14420 struct glyph_row *row = start;
14421 int last_y;
14422
14423 /* If we happen to start on a header-line, skip that. */
14424 if (row->mode_line_p)
14425 ++row;
14426
14427 if ((end && row >= end) || !row->enabled_p)
14428 return NULL;
14429
14430 last_y = window_text_bottom_y (w) - dy;
14431
14432 while (1)
14433 {
14434 /* Give up if we have gone too far. */
14435 if (end && row >= end)
14436 return NULL;
14437 /* This formerly returned if they were equal.
14438 I think that both quantities are of a "last plus one" type;
14439 if so, when they are equal, the row is within the screen. -- rms. */
14440 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14441 return NULL;
14442
14443 /* If it is in this row, return this row. */
14444 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14445 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14446 /* The end position of a row equals the start
14447 position of the next row. If CHARPOS is there, we
14448 would rather display it in the next line, except
14449 when this line ends in ZV. */
14450 && !row->ends_at_zv_p
14451 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14452 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14453 return row;
14454 ++row;
14455 }
14456 }
14457
14458
14459 /* Try to redisplay window W by reusing its existing display. W's
14460 current matrix must be up to date when this function is called,
14461 i.e. window_end_valid must not be nil.
14462
14463 Value is
14464
14465 1 if display has been updated
14466 0 if otherwise unsuccessful
14467 -1 if redisplay with same window start is known not to succeed
14468
14469 The following steps are performed:
14470
14471 1. Find the last row in the current matrix of W that is not
14472 affected by changes at the start of current_buffer. If no such row
14473 is found, give up.
14474
14475 2. Find the first row in W's current matrix that is not affected by
14476 changes at the end of current_buffer. Maybe there is no such row.
14477
14478 3. Display lines beginning with the row + 1 found in step 1 to the
14479 row found in step 2 or, if step 2 didn't find a row, to the end of
14480 the window.
14481
14482 4. If cursor is not known to appear on the window, give up.
14483
14484 5. If display stopped at the row found in step 2, scroll the
14485 display and current matrix as needed.
14486
14487 6. Maybe display some lines at the end of W, if we must. This can
14488 happen under various circumstances, like a partially visible line
14489 becoming fully visible, or because newly displayed lines are displayed
14490 in smaller font sizes.
14491
14492 7. Update W's window end information. */
14493
14494 static int
14495 try_window_id (w)
14496 struct window *w;
14497 {
14498 struct frame *f = XFRAME (w->frame);
14499 struct glyph_matrix *current_matrix = w->current_matrix;
14500 struct glyph_matrix *desired_matrix = w->desired_matrix;
14501 struct glyph_row *last_unchanged_at_beg_row;
14502 struct glyph_row *first_unchanged_at_end_row;
14503 struct glyph_row *row;
14504 struct glyph_row *bottom_row;
14505 int bottom_vpos;
14506 struct it it;
14507 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14508 struct text_pos start_pos;
14509 struct run run;
14510 int first_unchanged_at_end_vpos = 0;
14511 struct glyph_row *last_text_row, *last_text_row_at_end;
14512 struct text_pos start;
14513 int first_changed_charpos, last_changed_charpos;
14514
14515 #if GLYPH_DEBUG
14516 if (inhibit_try_window_id)
14517 return 0;
14518 #endif
14519
14520 /* This is handy for debugging. */
14521 #if 0
14522 #define GIVE_UP(X) \
14523 do { \
14524 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14525 return 0; \
14526 } while (0)
14527 #else
14528 #define GIVE_UP(X) return 0
14529 #endif
14530
14531 SET_TEXT_POS_FROM_MARKER (start, w->start);
14532
14533 /* Don't use this for mini-windows because these can show
14534 messages and mini-buffers, and we don't handle that here. */
14535 if (MINI_WINDOW_P (w))
14536 GIVE_UP (1);
14537
14538 /* This flag is used to prevent redisplay optimizations. */
14539 if (windows_or_buffers_changed || cursor_type_changed)
14540 GIVE_UP (2);
14541
14542 /* Verify that narrowing has not changed.
14543 Also verify that we were not told to prevent redisplay optimizations.
14544 It would be nice to further
14545 reduce the number of cases where this prevents try_window_id. */
14546 if (current_buffer->clip_changed
14547 || current_buffer->prevent_redisplay_optimizations_p)
14548 GIVE_UP (3);
14549
14550 /* Window must either use window-based redisplay or be full width. */
14551 if (!FRAME_WINDOW_P (f)
14552 && (!line_ins_del_ok
14553 || !WINDOW_FULL_WIDTH_P (w)))
14554 GIVE_UP (4);
14555
14556 /* Give up if point is not known NOT to appear in W. */
14557 if (PT < CHARPOS (start))
14558 GIVE_UP (5);
14559
14560 /* Another way to prevent redisplay optimizations. */
14561 if (XFASTINT (w->last_modified) == 0)
14562 GIVE_UP (6);
14563
14564 /* Verify that window is not hscrolled. */
14565 if (XFASTINT (w->hscroll) != 0)
14566 GIVE_UP (7);
14567
14568 /* Verify that display wasn't paused. */
14569 if (NILP (w->window_end_valid))
14570 GIVE_UP (8);
14571
14572 /* Can't use this if highlighting a region because a cursor movement
14573 will do more than just set the cursor. */
14574 if (!NILP (Vtransient_mark_mode)
14575 && !NILP (current_buffer->mark_active))
14576 GIVE_UP (9);
14577
14578 /* Likewise if highlighting trailing whitespace. */
14579 if (!NILP (Vshow_trailing_whitespace))
14580 GIVE_UP (11);
14581
14582 /* Likewise if showing a region. */
14583 if (!NILP (w->region_showing))
14584 GIVE_UP (10);
14585
14586 /* Can use this if overlay arrow position and or string have changed. */
14587 if (overlay_arrows_changed_p ())
14588 GIVE_UP (12);
14589
14590
14591 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14592 only if buffer has really changed. The reason is that the gap is
14593 initially at Z for freshly visited files. The code below would
14594 set end_unchanged to 0 in that case. */
14595 if (MODIFF > SAVE_MODIFF
14596 /* This seems to happen sometimes after saving a buffer. */
14597 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14598 {
14599 if (GPT - BEG < BEG_UNCHANGED)
14600 BEG_UNCHANGED = GPT - BEG;
14601 if (Z - GPT < END_UNCHANGED)
14602 END_UNCHANGED = Z - GPT;
14603 }
14604
14605 /* The position of the first and last character that has been changed. */
14606 first_changed_charpos = BEG + BEG_UNCHANGED;
14607 last_changed_charpos = Z - END_UNCHANGED;
14608
14609 /* If window starts after a line end, and the last change is in
14610 front of that newline, then changes don't affect the display.
14611 This case happens with stealth-fontification. Note that although
14612 the display is unchanged, glyph positions in the matrix have to
14613 be adjusted, of course. */
14614 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14615 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14616 && ((last_changed_charpos < CHARPOS (start)
14617 && CHARPOS (start) == BEGV)
14618 || (last_changed_charpos < CHARPOS (start) - 1
14619 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14620 {
14621 int Z_old, delta, Z_BYTE_old, delta_bytes;
14622 struct glyph_row *r0;
14623
14624 /* Compute how many chars/bytes have been added to or removed
14625 from the buffer. */
14626 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14627 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14628 delta = Z - Z_old;
14629 delta_bytes = Z_BYTE - Z_BYTE_old;
14630
14631 /* Give up if PT is not in the window. Note that it already has
14632 been checked at the start of try_window_id that PT is not in
14633 front of the window start. */
14634 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14635 GIVE_UP (13);
14636
14637 /* If window start is unchanged, we can reuse the whole matrix
14638 as is, after adjusting glyph positions. No need to compute
14639 the window end again, since its offset from Z hasn't changed. */
14640 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14641 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14642 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14643 /* PT must not be in a partially visible line. */
14644 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14645 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14646 {
14647 /* Adjust positions in the glyph matrix. */
14648 if (delta || delta_bytes)
14649 {
14650 struct glyph_row *r1
14651 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14652 increment_matrix_positions (w->current_matrix,
14653 MATRIX_ROW_VPOS (r0, current_matrix),
14654 MATRIX_ROW_VPOS (r1, current_matrix),
14655 delta, delta_bytes);
14656 }
14657
14658 /* Set the cursor. */
14659 row = row_containing_pos (w, PT, r0, NULL, 0);
14660 if (row)
14661 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14662 else
14663 abort ();
14664 return 1;
14665 }
14666 }
14667
14668 /* Handle the case that changes are all below what is displayed in
14669 the window, and that PT is in the window. This shortcut cannot
14670 be taken if ZV is visible in the window, and text has been added
14671 there that is visible in the window. */
14672 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14673 /* ZV is not visible in the window, or there are no
14674 changes at ZV, actually. */
14675 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14676 || first_changed_charpos == last_changed_charpos))
14677 {
14678 struct glyph_row *r0;
14679
14680 /* Give up if PT is not in the window. Note that it already has
14681 been checked at the start of try_window_id that PT is not in
14682 front of the window start. */
14683 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14684 GIVE_UP (14);
14685
14686 /* If window start is unchanged, we can reuse the whole matrix
14687 as is, without changing glyph positions since no text has
14688 been added/removed in front of the window end. */
14689 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14690 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14691 /* PT must not be in a partially visible line. */
14692 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14693 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14694 {
14695 /* We have to compute the window end anew since text
14696 can have been added/removed after it. */
14697 w->window_end_pos
14698 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14699 w->window_end_bytepos
14700 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14701
14702 /* Set the cursor. */
14703 row = row_containing_pos (w, PT, r0, NULL, 0);
14704 if (row)
14705 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14706 else
14707 abort ();
14708 return 2;
14709 }
14710 }
14711
14712 /* Give up if window start is in the changed area.
14713
14714 The condition used to read
14715
14716 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14717
14718 but why that was tested escapes me at the moment. */
14719 if (CHARPOS (start) >= first_changed_charpos
14720 && CHARPOS (start) <= last_changed_charpos)
14721 GIVE_UP (15);
14722
14723 /* Check that window start agrees with the start of the first glyph
14724 row in its current matrix. Check this after we know the window
14725 start is not in changed text, otherwise positions would not be
14726 comparable. */
14727 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14728 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14729 GIVE_UP (16);
14730
14731 /* Give up if the window ends in strings. Overlay strings
14732 at the end are difficult to handle, so don't try. */
14733 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14734 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14735 GIVE_UP (20);
14736
14737 /* Compute the position at which we have to start displaying new
14738 lines. Some of the lines at the top of the window might be
14739 reusable because they are not displaying changed text. Find the
14740 last row in W's current matrix not affected by changes at the
14741 start of current_buffer. Value is null if changes start in the
14742 first line of window. */
14743 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14744 if (last_unchanged_at_beg_row)
14745 {
14746 /* Avoid starting to display in the moddle of a character, a TAB
14747 for instance. This is easier than to set up the iterator
14748 exactly, and it's not a frequent case, so the additional
14749 effort wouldn't really pay off. */
14750 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14751 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14752 && last_unchanged_at_beg_row > w->current_matrix->rows)
14753 --last_unchanged_at_beg_row;
14754
14755 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14756 GIVE_UP (17);
14757
14758 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14759 GIVE_UP (18);
14760 start_pos = it.current.pos;
14761
14762 /* Start displaying new lines in the desired matrix at the same
14763 vpos we would use in the current matrix, i.e. below
14764 last_unchanged_at_beg_row. */
14765 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14766 current_matrix);
14767 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14768 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14769
14770 xassert (it.hpos == 0 && it.current_x == 0);
14771 }
14772 else
14773 {
14774 /* There are no reusable lines at the start of the window.
14775 Start displaying in the first text line. */
14776 start_display (&it, w, start);
14777 it.vpos = it.first_vpos;
14778 start_pos = it.current.pos;
14779 }
14780
14781 /* Find the first row that is not affected by changes at the end of
14782 the buffer. Value will be null if there is no unchanged row, in
14783 which case we must redisplay to the end of the window. delta
14784 will be set to the value by which buffer positions beginning with
14785 first_unchanged_at_end_row have to be adjusted due to text
14786 changes. */
14787 first_unchanged_at_end_row
14788 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14789 IF_DEBUG (debug_delta = delta);
14790 IF_DEBUG (debug_delta_bytes = delta_bytes);
14791
14792 /* Set stop_pos to the buffer position up to which we will have to
14793 display new lines. If first_unchanged_at_end_row != NULL, this
14794 is the buffer position of the start of the line displayed in that
14795 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14796 that we don't stop at a buffer position. */
14797 stop_pos = 0;
14798 if (first_unchanged_at_end_row)
14799 {
14800 xassert (last_unchanged_at_beg_row == NULL
14801 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14802
14803 /* If this is a continuation line, move forward to the next one
14804 that isn't. Changes in lines above affect this line.
14805 Caution: this may move first_unchanged_at_end_row to a row
14806 not displaying text. */
14807 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14808 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14809 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14810 < it.last_visible_y))
14811 ++first_unchanged_at_end_row;
14812
14813 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14814 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14815 >= it.last_visible_y))
14816 first_unchanged_at_end_row = NULL;
14817 else
14818 {
14819 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14820 + delta);
14821 first_unchanged_at_end_vpos
14822 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14823 xassert (stop_pos >= Z - END_UNCHANGED);
14824 }
14825 }
14826 else if (last_unchanged_at_beg_row == NULL)
14827 GIVE_UP (19);
14828
14829
14830 #if GLYPH_DEBUG
14831
14832 /* Either there is no unchanged row at the end, or the one we have
14833 now displays text. This is a necessary condition for the window
14834 end pos calculation at the end of this function. */
14835 xassert (first_unchanged_at_end_row == NULL
14836 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14837
14838 debug_last_unchanged_at_beg_vpos
14839 = (last_unchanged_at_beg_row
14840 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14841 : -1);
14842 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14843
14844 #endif /* GLYPH_DEBUG != 0 */
14845
14846
14847 /* Display new lines. Set last_text_row to the last new line
14848 displayed which has text on it, i.e. might end up as being the
14849 line where the window_end_vpos is. */
14850 w->cursor.vpos = -1;
14851 last_text_row = NULL;
14852 overlay_arrow_seen = 0;
14853 while (it.current_y < it.last_visible_y
14854 && !fonts_changed_p
14855 && (first_unchanged_at_end_row == NULL
14856 || IT_CHARPOS (it) < stop_pos))
14857 {
14858 if (display_line (&it))
14859 last_text_row = it.glyph_row - 1;
14860 }
14861
14862 if (fonts_changed_p)
14863 return -1;
14864
14865
14866 /* Compute differences in buffer positions, y-positions etc. for
14867 lines reused at the bottom of the window. Compute what we can
14868 scroll. */
14869 if (first_unchanged_at_end_row
14870 /* No lines reused because we displayed everything up to the
14871 bottom of the window. */
14872 && it.current_y < it.last_visible_y)
14873 {
14874 dvpos = (it.vpos
14875 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14876 current_matrix));
14877 dy = it.current_y - first_unchanged_at_end_row->y;
14878 run.current_y = first_unchanged_at_end_row->y;
14879 run.desired_y = run.current_y + dy;
14880 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14881 }
14882 else
14883 {
14884 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14885 first_unchanged_at_end_row = NULL;
14886 }
14887 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14888
14889
14890 /* Find the cursor if not already found. We have to decide whether
14891 PT will appear on this window (it sometimes doesn't, but this is
14892 not a very frequent case.) This decision has to be made before
14893 the current matrix is altered. A value of cursor.vpos < 0 means
14894 that PT is either in one of the lines beginning at
14895 first_unchanged_at_end_row or below the window. Don't care for
14896 lines that might be displayed later at the window end; as
14897 mentioned, this is not a frequent case. */
14898 if (w->cursor.vpos < 0)
14899 {
14900 /* Cursor in unchanged rows at the top? */
14901 if (PT < CHARPOS (start_pos)
14902 && last_unchanged_at_beg_row)
14903 {
14904 row = row_containing_pos (w, PT,
14905 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14906 last_unchanged_at_beg_row + 1, 0);
14907 if (row)
14908 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14909 }
14910
14911 /* Start from first_unchanged_at_end_row looking for PT. */
14912 else if (first_unchanged_at_end_row)
14913 {
14914 row = row_containing_pos (w, PT - delta,
14915 first_unchanged_at_end_row, NULL, 0);
14916 if (row)
14917 set_cursor_from_row (w, row, w->current_matrix, delta,
14918 delta_bytes, dy, dvpos);
14919 }
14920
14921 /* Give up if cursor was not found. */
14922 if (w->cursor.vpos < 0)
14923 {
14924 clear_glyph_matrix (w->desired_matrix);
14925 return -1;
14926 }
14927 }
14928
14929 /* Don't let the cursor end in the scroll margins. */
14930 {
14931 int this_scroll_margin, cursor_height;
14932
14933 this_scroll_margin = max (0, scroll_margin);
14934 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14935 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14936 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14937
14938 if ((w->cursor.y < this_scroll_margin
14939 && CHARPOS (start) > BEGV)
14940 /* Old redisplay didn't take scroll margin into account at the bottom,
14941 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14942 || (w->cursor.y + (make_cursor_line_fully_visible_p
14943 ? cursor_height + this_scroll_margin
14944 : 1)) > it.last_visible_y)
14945 {
14946 w->cursor.vpos = -1;
14947 clear_glyph_matrix (w->desired_matrix);
14948 return -1;
14949 }
14950 }
14951
14952 /* Scroll the display. Do it before changing the current matrix so
14953 that xterm.c doesn't get confused about where the cursor glyph is
14954 found. */
14955 if (dy && run.height)
14956 {
14957 update_begin (f);
14958
14959 if (FRAME_WINDOW_P (f))
14960 {
14961 rif->update_window_begin_hook (w);
14962 rif->clear_window_mouse_face (w);
14963 rif->scroll_run_hook (w, &run);
14964 rif->update_window_end_hook (w, 0, 0);
14965 }
14966 else
14967 {
14968 /* Terminal frame. In this case, dvpos gives the number of
14969 lines to scroll by; dvpos < 0 means scroll up. */
14970 int first_unchanged_at_end_vpos
14971 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14972 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14973 int end = (WINDOW_TOP_EDGE_LINE (w)
14974 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14975 + window_internal_height (w));
14976
14977 /* Perform the operation on the screen. */
14978 if (dvpos > 0)
14979 {
14980 /* Scroll last_unchanged_at_beg_row to the end of the
14981 window down dvpos lines. */
14982 set_terminal_window (end);
14983
14984 /* On dumb terminals delete dvpos lines at the end
14985 before inserting dvpos empty lines. */
14986 if (!scroll_region_ok)
14987 ins_del_lines (end - dvpos, -dvpos);
14988
14989 /* Insert dvpos empty lines in front of
14990 last_unchanged_at_beg_row. */
14991 ins_del_lines (from, dvpos);
14992 }
14993 else if (dvpos < 0)
14994 {
14995 /* Scroll up last_unchanged_at_beg_vpos to the end of
14996 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14997 set_terminal_window (end);
14998
14999 /* Delete dvpos lines in front of
15000 last_unchanged_at_beg_vpos. ins_del_lines will set
15001 the cursor to the given vpos and emit |dvpos| delete
15002 line sequences. */
15003 ins_del_lines (from + dvpos, dvpos);
15004
15005 /* On a dumb terminal insert dvpos empty lines at the
15006 end. */
15007 if (!scroll_region_ok)
15008 ins_del_lines (end + dvpos, -dvpos);
15009 }
15010
15011 set_terminal_window (0);
15012 }
15013
15014 update_end (f);
15015 }
15016
15017 /* Shift reused rows of the current matrix to the right position.
15018 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15019 text. */
15020 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15021 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15022 if (dvpos < 0)
15023 {
15024 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15025 bottom_vpos, dvpos);
15026 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15027 bottom_vpos, 0);
15028 }
15029 else if (dvpos > 0)
15030 {
15031 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15032 bottom_vpos, dvpos);
15033 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15034 first_unchanged_at_end_vpos + dvpos, 0);
15035 }
15036
15037 /* For frame-based redisplay, make sure that current frame and window
15038 matrix are in sync with respect to glyph memory. */
15039 if (!FRAME_WINDOW_P (f))
15040 sync_frame_with_window_matrix_rows (w);
15041
15042 /* Adjust buffer positions in reused rows. */
15043 if (delta)
15044 increment_matrix_positions (current_matrix,
15045 first_unchanged_at_end_vpos + dvpos,
15046 bottom_vpos, delta, delta_bytes);
15047
15048 /* Adjust Y positions. */
15049 if (dy)
15050 shift_glyph_matrix (w, current_matrix,
15051 first_unchanged_at_end_vpos + dvpos,
15052 bottom_vpos, dy);
15053
15054 if (first_unchanged_at_end_row)
15055 {
15056 first_unchanged_at_end_row += dvpos;
15057 if (first_unchanged_at_end_row->y >= it.last_visible_y
15058 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15059 first_unchanged_at_end_row = NULL;
15060 }
15061
15062 /* If scrolling up, there may be some lines to display at the end of
15063 the window. */
15064 last_text_row_at_end = NULL;
15065 if (dy < 0)
15066 {
15067 /* Scrolling up can leave for example a partially visible line
15068 at the end of the window to be redisplayed. */
15069 /* Set last_row to the glyph row in the current matrix where the
15070 window end line is found. It has been moved up or down in
15071 the matrix by dvpos. */
15072 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15073 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15074
15075 /* If last_row is the window end line, it should display text. */
15076 xassert (last_row->displays_text_p);
15077
15078 /* If window end line was partially visible before, begin
15079 displaying at that line. Otherwise begin displaying with the
15080 line following it. */
15081 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15082 {
15083 init_to_row_start (&it, w, last_row);
15084 it.vpos = last_vpos;
15085 it.current_y = last_row->y;
15086 }
15087 else
15088 {
15089 init_to_row_end (&it, w, last_row);
15090 it.vpos = 1 + last_vpos;
15091 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15092 ++last_row;
15093 }
15094
15095 /* We may start in a continuation line. If so, we have to
15096 get the right continuation_lines_width and current_x. */
15097 it.continuation_lines_width = last_row->continuation_lines_width;
15098 it.hpos = it.current_x = 0;
15099
15100 /* Display the rest of the lines at the window end. */
15101 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15102 while (it.current_y < it.last_visible_y
15103 && !fonts_changed_p)
15104 {
15105 /* Is it always sure that the display agrees with lines in
15106 the current matrix? I don't think so, so we mark rows
15107 displayed invalid in the current matrix by setting their
15108 enabled_p flag to zero. */
15109 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15110 if (display_line (&it))
15111 last_text_row_at_end = it.glyph_row - 1;
15112 }
15113 }
15114
15115 /* Update window_end_pos and window_end_vpos. */
15116 if (first_unchanged_at_end_row
15117 && !last_text_row_at_end)
15118 {
15119 /* Window end line if one of the preserved rows from the current
15120 matrix. Set row to the last row displaying text in current
15121 matrix starting at first_unchanged_at_end_row, after
15122 scrolling. */
15123 xassert (first_unchanged_at_end_row->displays_text_p);
15124 row = find_last_row_displaying_text (w->current_matrix, &it,
15125 first_unchanged_at_end_row);
15126 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15127
15128 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15129 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15130 w->window_end_vpos
15131 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15132 xassert (w->window_end_bytepos >= 0);
15133 IF_DEBUG (debug_method_add (w, "A"));
15134 }
15135 else if (last_text_row_at_end)
15136 {
15137 w->window_end_pos
15138 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15139 w->window_end_bytepos
15140 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15141 w->window_end_vpos
15142 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15143 xassert (w->window_end_bytepos >= 0);
15144 IF_DEBUG (debug_method_add (w, "B"));
15145 }
15146 else if (last_text_row)
15147 {
15148 /* We have displayed either to the end of the window or at the
15149 end of the window, i.e. the last row with text is to be found
15150 in the desired matrix. */
15151 w->window_end_pos
15152 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15153 w->window_end_bytepos
15154 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15155 w->window_end_vpos
15156 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15157 xassert (w->window_end_bytepos >= 0);
15158 }
15159 else if (first_unchanged_at_end_row == NULL
15160 && last_text_row == NULL
15161 && last_text_row_at_end == NULL)
15162 {
15163 /* Displayed to end of window, but no line containing text was
15164 displayed. Lines were deleted at the end of the window. */
15165 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15166 int vpos = XFASTINT (w->window_end_vpos);
15167 struct glyph_row *current_row = current_matrix->rows + vpos;
15168 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15169
15170 for (row = NULL;
15171 row == NULL && vpos >= first_vpos;
15172 --vpos, --current_row, --desired_row)
15173 {
15174 if (desired_row->enabled_p)
15175 {
15176 if (desired_row->displays_text_p)
15177 row = desired_row;
15178 }
15179 else if (current_row->displays_text_p)
15180 row = current_row;
15181 }
15182
15183 xassert (row != NULL);
15184 w->window_end_vpos = make_number (vpos + 1);
15185 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15186 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15187 xassert (w->window_end_bytepos >= 0);
15188 IF_DEBUG (debug_method_add (w, "C"));
15189 }
15190 else
15191 abort ();
15192
15193 #if 0 /* This leads to problems, for instance when the cursor is
15194 at ZV, and the cursor line displays no text. */
15195 /* Disable rows below what's displayed in the window. This makes
15196 debugging easier. */
15197 enable_glyph_matrix_rows (current_matrix,
15198 XFASTINT (w->window_end_vpos) + 1,
15199 bottom_vpos, 0);
15200 #endif
15201
15202 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15203 debug_end_vpos = XFASTINT (w->window_end_vpos));
15204
15205 /* Record that display has not been completed. */
15206 w->window_end_valid = Qnil;
15207 w->desired_matrix->no_scrolling_p = 1;
15208 return 3;
15209
15210 #undef GIVE_UP
15211 }
15212
15213
15214 \f
15215 /***********************************************************************
15216 More debugging support
15217 ***********************************************************************/
15218
15219 #if GLYPH_DEBUG
15220
15221 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15222 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15223 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15224
15225
15226 /* Dump the contents of glyph matrix MATRIX on stderr.
15227
15228 GLYPHS 0 means don't show glyph contents.
15229 GLYPHS 1 means show glyphs in short form
15230 GLYPHS > 1 means show glyphs in long form. */
15231
15232 void
15233 dump_glyph_matrix (matrix, glyphs)
15234 struct glyph_matrix *matrix;
15235 int glyphs;
15236 {
15237 int i;
15238 for (i = 0; i < matrix->nrows; ++i)
15239 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15240 }
15241
15242
15243 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15244 the glyph row and area where the glyph comes from. */
15245
15246 void
15247 dump_glyph (row, glyph, area)
15248 struct glyph_row *row;
15249 struct glyph *glyph;
15250 int area;
15251 {
15252 if (glyph->type == CHAR_GLYPH)
15253 {
15254 fprintf (stderr,
15255 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15256 glyph - row->glyphs[TEXT_AREA],
15257 'C',
15258 glyph->charpos,
15259 (BUFFERP (glyph->object)
15260 ? 'B'
15261 : (STRINGP (glyph->object)
15262 ? 'S'
15263 : '-')),
15264 glyph->pixel_width,
15265 glyph->u.ch,
15266 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15267 ? glyph->u.ch
15268 : '.'),
15269 glyph->face_id,
15270 glyph->left_box_line_p,
15271 glyph->right_box_line_p);
15272 }
15273 else if (glyph->type == STRETCH_GLYPH)
15274 {
15275 fprintf (stderr,
15276 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15277 glyph - row->glyphs[TEXT_AREA],
15278 'S',
15279 glyph->charpos,
15280 (BUFFERP (glyph->object)
15281 ? 'B'
15282 : (STRINGP (glyph->object)
15283 ? 'S'
15284 : '-')),
15285 glyph->pixel_width,
15286 0,
15287 '.',
15288 glyph->face_id,
15289 glyph->left_box_line_p,
15290 glyph->right_box_line_p);
15291 }
15292 else if (glyph->type == IMAGE_GLYPH)
15293 {
15294 fprintf (stderr,
15295 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15296 glyph - row->glyphs[TEXT_AREA],
15297 'I',
15298 glyph->charpos,
15299 (BUFFERP (glyph->object)
15300 ? 'B'
15301 : (STRINGP (glyph->object)
15302 ? 'S'
15303 : '-')),
15304 glyph->pixel_width,
15305 glyph->u.img_id,
15306 '.',
15307 glyph->face_id,
15308 glyph->left_box_line_p,
15309 glyph->right_box_line_p);
15310 }
15311 else if (glyph->type == COMPOSITE_GLYPH)
15312 {
15313 fprintf (stderr,
15314 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15315 glyph - row->glyphs[TEXT_AREA],
15316 '+',
15317 glyph->charpos,
15318 (BUFFERP (glyph->object)
15319 ? 'B'
15320 : (STRINGP (glyph->object)
15321 ? 'S'
15322 : '-')),
15323 glyph->pixel_width,
15324 glyph->u.cmp_id,
15325 '.',
15326 glyph->face_id,
15327 glyph->left_box_line_p,
15328 glyph->right_box_line_p);
15329 }
15330 }
15331
15332
15333 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15334 GLYPHS 0 means don't show glyph contents.
15335 GLYPHS 1 means show glyphs in short form
15336 GLYPHS > 1 means show glyphs in long form. */
15337
15338 void
15339 dump_glyph_row (row, vpos, glyphs)
15340 struct glyph_row *row;
15341 int vpos, glyphs;
15342 {
15343 if (glyphs != 1)
15344 {
15345 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15346 fprintf (stderr, "======================================================================\n");
15347
15348 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15349 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15350 vpos,
15351 MATRIX_ROW_START_CHARPOS (row),
15352 MATRIX_ROW_END_CHARPOS (row),
15353 row->used[TEXT_AREA],
15354 row->contains_overlapping_glyphs_p,
15355 row->enabled_p,
15356 row->truncated_on_left_p,
15357 row->truncated_on_right_p,
15358 row->continued_p,
15359 MATRIX_ROW_CONTINUATION_LINE_P (row),
15360 row->displays_text_p,
15361 row->ends_at_zv_p,
15362 row->fill_line_p,
15363 row->ends_in_middle_of_char_p,
15364 row->starts_in_middle_of_char_p,
15365 row->mouse_face_p,
15366 row->x,
15367 row->y,
15368 row->pixel_width,
15369 row->height,
15370 row->visible_height,
15371 row->ascent,
15372 row->phys_ascent);
15373 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15374 row->end.overlay_string_index,
15375 row->continuation_lines_width);
15376 fprintf (stderr, "%9d %5d\n",
15377 CHARPOS (row->start.string_pos),
15378 CHARPOS (row->end.string_pos));
15379 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15380 row->end.dpvec_index);
15381 }
15382
15383 if (glyphs > 1)
15384 {
15385 int area;
15386
15387 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15388 {
15389 struct glyph *glyph = row->glyphs[area];
15390 struct glyph *glyph_end = glyph + row->used[area];
15391
15392 /* Glyph for a line end in text. */
15393 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15394 ++glyph_end;
15395
15396 if (glyph < glyph_end)
15397 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15398
15399 for (; glyph < glyph_end; ++glyph)
15400 dump_glyph (row, glyph, area);
15401 }
15402 }
15403 else if (glyphs == 1)
15404 {
15405 int area;
15406
15407 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15408 {
15409 char *s = (char *) alloca (row->used[area] + 1);
15410 int i;
15411
15412 for (i = 0; i < row->used[area]; ++i)
15413 {
15414 struct glyph *glyph = row->glyphs[area] + i;
15415 if (glyph->type == CHAR_GLYPH
15416 && glyph->u.ch < 0x80
15417 && glyph->u.ch >= ' ')
15418 s[i] = glyph->u.ch;
15419 else
15420 s[i] = '.';
15421 }
15422
15423 s[i] = '\0';
15424 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15425 }
15426 }
15427 }
15428
15429
15430 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15431 Sdump_glyph_matrix, 0, 1, "p",
15432 doc: /* Dump the current matrix of the selected window to stderr.
15433 Shows contents of glyph row structures. With non-nil
15434 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15435 glyphs in short form, otherwise show glyphs in long form. */)
15436 (glyphs)
15437 Lisp_Object glyphs;
15438 {
15439 struct window *w = XWINDOW (selected_window);
15440 struct buffer *buffer = XBUFFER (w->buffer);
15441
15442 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15443 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15444 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15445 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15446 fprintf (stderr, "=============================================\n");
15447 dump_glyph_matrix (w->current_matrix,
15448 NILP (glyphs) ? 0 : XINT (glyphs));
15449 return Qnil;
15450 }
15451
15452
15453 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15454 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15455 ()
15456 {
15457 struct frame *f = XFRAME (selected_frame);
15458 dump_glyph_matrix (f->current_matrix, 1);
15459 return Qnil;
15460 }
15461
15462
15463 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15464 doc: /* Dump glyph row ROW to stderr.
15465 GLYPH 0 means don't dump glyphs.
15466 GLYPH 1 means dump glyphs in short form.
15467 GLYPH > 1 or omitted means dump glyphs in long form. */)
15468 (row, glyphs)
15469 Lisp_Object row, glyphs;
15470 {
15471 struct glyph_matrix *matrix;
15472 int vpos;
15473
15474 CHECK_NUMBER (row);
15475 matrix = XWINDOW (selected_window)->current_matrix;
15476 vpos = XINT (row);
15477 if (vpos >= 0 && vpos < matrix->nrows)
15478 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15479 vpos,
15480 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15481 return Qnil;
15482 }
15483
15484
15485 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15486 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15487 GLYPH 0 means don't dump glyphs.
15488 GLYPH 1 means dump glyphs in short form.
15489 GLYPH > 1 or omitted means dump glyphs in long form. */)
15490 (row, glyphs)
15491 Lisp_Object row, glyphs;
15492 {
15493 struct frame *sf = SELECTED_FRAME ();
15494 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15495 int vpos;
15496
15497 CHECK_NUMBER (row);
15498 vpos = XINT (row);
15499 if (vpos >= 0 && vpos < m->nrows)
15500 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15501 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15502 return Qnil;
15503 }
15504
15505
15506 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15507 doc: /* Toggle tracing of redisplay.
15508 With ARG, turn tracing on if and only if ARG is positive. */)
15509 (arg)
15510 Lisp_Object arg;
15511 {
15512 if (NILP (arg))
15513 trace_redisplay_p = !trace_redisplay_p;
15514 else
15515 {
15516 arg = Fprefix_numeric_value (arg);
15517 trace_redisplay_p = XINT (arg) > 0;
15518 }
15519
15520 return Qnil;
15521 }
15522
15523
15524 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15525 doc: /* Like `format', but print result to stderr.
15526 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15527 (nargs, args)
15528 int nargs;
15529 Lisp_Object *args;
15530 {
15531 Lisp_Object s = Fformat (nargs, args);
15532 fprintf (stderr, "%s", SDATA (s));
15533 return Qnil;
15534 }
15535
15536 #endif /* GLYPH_DEBUG */
15537
15538
15539 \f
15540 /***********************************************************************
15541 Building Desired Matrix Rows
15542 ***********************************************************************/
15543
15544 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15545 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15546
15547 static struct glyph_row *
15548 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15549 struct window *w;
15550 Lisp_Object overlay_arrow_string;
15551 {
15552 struct frame *f = XFRAME (WINDOW_FRAME (w));
15553 struct buffer *buffer = XBUFFER (w->buffer);
15554 struct buffer *old = current_buffer;
15555 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15556 int arrow_len = SCHARS (overlay_arrow_string);
15557 const unsigned char *arrow_end = arrow_string + arrow_len;
15558 const unsigned char *p;
15559 struct it it;
15560 int multibyte_p;
15561 int n_glyphs_before;
15562
15563 set_buffer_temp (buffer);
15564 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15565 it.glyph_row->used[TEXT_AREA] = 0;
15566 SET_TEXT_POS (it.position, 0, 0);
15567
15568 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15569 p = arrow_string;
15570 while (p < arrow_end)
15571 {
15572 Lisp_Object face, ilisp;
15573
15574 /* Get the next character. */
15575 if (multibyte_p)
15576 it.c = string_char_and_length (p, arrow_len, &it.len);
15577 else
15578 it.c = *p, it.len = 1;
15579 p += it.len;
15580
15581 /* Get its face. */
15582 ilisp = make_number (p - arrow_string);
15583 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15584 it.face_id = compute_char_face (f, it.c, face);
15585
15586 /* Compute its width, get its glyphs. */
15587 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15588 SET_TEXT_POS (it.position, -1, -1);
15589 PRODUCE_GLYPHS (&it);
15590
15591 /* If this character doesn't fit any more in the line, we have
15592 to remove some glyphs. */
15593 if (it.current_x > it.last_visible_x)
15594 {
15595 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15596 break;
15597 }
15598 }
15599
15600 set_buffer_temp (old);
15601 return it.glyph_row;
15602 }
15603
15604
15605 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15606 glyphs are only inserted for terminal frames since we can't really
15607 win with truncation glyphs when partially visible glyphs are
15608 involved. Which glyphs to insert is determined by
15609 produce_special_glyphs. */
15610
15611 static void
15612 insert_left_trunc_glyphs (it)
15613 struct it *it;
15614 {
15615 struct it truncate_it;
15616 struct glyph *from, *end, *to, *toend;
15617
15618 xassert (!FRAME_WINDOW_P (it->f));
15619
15620 /* Get the truncation glyphs. */
15621 truncate_it = *it;
15622 truncate_it.current_x = 0;
15623 truncate_it.face_id = DEFAULT_FACE_ID;
15624 truncate_it.glyph_row = &scratch_glyph_row;
15625 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15626 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15627 truncate_it.object = make_number (0);
15628 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15629
15630 /* Overwrite glyphs from IT with truncation glyphs. */
15631 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15632 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15633 to = it->glyph_row->glyphs[TEXT_AREA];
15634 toend = to + it->glyph_row->used[TEXT_AREA];
15635
15636 while (from < end)
15637 *to++ = *from++;
15638
15639 /* There may be padding glyphs left over. Overwrite them too. */
15640 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15641 {
15642 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15643 while (from < end)
15644 *to++ = *from++;
15645 }
15646
15647 if (to > toend)
15648 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15649 }
15650
15651
15652 /* Compute the pixel height and width of IT->glyph_row.
15653
15654 Most of the time, ascent and height of a display line will be equal
15655 to the max_ascent and max_height values of the display iterator
15656 structure. This is not the case if
15657
15658 1. We hit ZV without displaying anything. In this case, max_ascent
15659 and max_height will be zero.
15660
15661 2. We have some glyphs that don't contribute to the line height.
15662 (The glyph row flag contributes_to_line_height_p is for future
15663 pixmap extensions).
15664
15665 The first case is easily covered by using default values because in
15666 these cases, the line height does not really matter, except that it
15667 must not be zero. */
15668
15669 static void
15670 compute_line_metrics (it)
15671 struct it *it;
15672 {
15673 struct glyph_row *row = it->glyph_row;
15674 int area, i;
15675
15676 if (FRAME_WINDOW_P (it->f))
15677 {
15678 int i, min_y, max_y;
15679
15680 /* The line may consist of one space only, that was added to
15681 place the cursor on it. If so, the row's height hasn't been
15682 computed yet. */
15683 if (row->height == 0)
15684 {
15685 if (it->max_ascent + it->max_descent == 0)
15686 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15687 row->ascent = it->max_ascent;
15688 row->height = it->max_ascent + it->max_descent;
15689 row->phys_ascent = it->max_phys_ascent;
15690 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15691 row->extra_line_spacing = it->max_extra_line_spacing;
15692 }
15693
15694 /* Compute the width of this line. */
15695 row->pixel_width = row->x;
15696 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15697 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15698
15699 xassert (row->pixel_width >= 0);
15700 xassert (row->ascent >= 0 && row->height > 0);
15701
15702 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15703 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15704
15705 /* If first line's physical ascent is larger than its logical
15706 ascent, use the physical ascent, and make the row taller.
15707 This makes accented characters fully visible. */
15708 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15709 && row->phys_ascent > row->ascent)
15710 {
15711 row->height += row->phys_ascent - row->ascent;
15712 row->ascent = row->phys_ascent;
15713 }
15714
15715 /* Compute how much of the line is visible. */
15716 row->visible_height = row->height;
15717
15718 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15719 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15720
15721 if (row->y < min_y)
15722 row->visible_height -= min_y - row->y;
15723 if (row->y + row->height > max_y)
15724 row->visible_height -= row->y + row->height - max_y;
15725 }
15726 else
15727 {
15728 row->pixel_width = row->used[TEXT_AREA];
15729 if (row->continued_p)
15730 row->pixel_width -= it->continuation_pixel_width;
15731 else if (row->truncated_on_right_p)
15732 row->pixel_width -= it->truncation_pixel_width;
15733 row->ascent = row->phys_ascent = 0;
15734 row->height = row->phys_height = row->visible_height = 1;
15735 row->extra_line_spacing = 0;
15736 }
15737
15738 /* Compute a hash code for this row. */
15739 row->hash = 0;
15740 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15741 for (i = 0; i < row->used[area]; ++i)
15742 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15743 + row->glyphs[area][i].u.val
15744 + row->glyphs[area][i].face_id
15745 + row->glyphs[area][i].padding_p
15746 + (row->glyphs[area][i].type << 2));
15747
15748 it->max_ascent = it->max_descent = 0;
15749 it->max_phys_ascent = it->max_phys_descent = 0;
15750 }
15751
15752
15753 /* Append one space to the glyph row of iterator IT if doing a
15754 window-based redisplay. The space has the same face as
15755 IT->face_id. Value is non-zero if a space was added.
15756
15757 This function is called to make sure that there is always one glyph
15758 at the end of a glyph row that the cursor can be set on under
15759 window-systems. (If there weren't such a glyph we would not know
15760 how wide and tall a box cursor should be displayed).
15761
15762 At the same time this space let's a nicely handle clearing to the
15763 end of the line if the row ends in italic text. */
15764
15765 static int
15766 append_space_for_newline (it, default_face_p)
15767 struct it *it;
15768 int default_face_p;
15769 {
15770 if (FRAME_WINDOW_P (it->f))
15771 {
15772 int n = it->glyph_row->used[TEXT_AREA];
15773
15774 if (it->glyph_row->glyphs[TEXT_AREA] + n
15775 < it->glyph_row->glyphs[1 + TEXT_AREA])
15776 {
15777 /* Save some values that must not be changed.
15778 Must save IT->c and IT->len because otherwise
15779 ITERATOR_AT_END_P wouldn't work anymore after
15780 append_space_for_newline has been called. */
15781 enum display_element_type saved_what = it->what;
15782 int saved_c = it->c, saved_len = it->len;
15783 int saved_x = it->current_x;
15784 int saved_face_id = it->face_id;
15785 struct text_pos saved_pos;
15786 Lisp_Object saved_object;
15787 struct face *face;
15788
15789 saved_object = it->object;
15790 saved_pos = it->position;
15791
15792 it->what = IT_CHARACTER;
15793 bzero (&it->position, sizeof it->position);
15794 it->object = make_number (0);
15795 it->c = ' ';
15796 it->len = 1;
15797
15798 if (default_face_p)
15799 it->face_id = DEFAULT_FACE_ID;
15800 else if (it->face_before_selective_p)
15801 it->face_id = it->saved_face_id;
15802 face = FACE_FROM_ID (it->f, it->face_id);
15803 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15804
15805 PRODUCE_GLYPHS (it);
15806
15807 it->override_ascent = -1;
15808 it->constrain_row_ascent_descent_p = 0;
15809 it->current_x = saved_x;
15810 it->object = saved_object;
15811 it->position = saved_pos;
15812 it->what = saved_what;
15813 it->face_id = saved_face_id;
15814 it->len = saved_len;
15815 it->c = saved_c;
15816 return 1;
15817 }
15818 }
15819
15820 return 0;
15821 }
15822
15823
15824 /* Extend the face of the last glyph in the text area of IT->glyph_row
15825 to the end of the display line. Called from display_line.
15826 If the glyph row is empty, add a space glyph to it so that we
15827 know the face to draw. Set the glyph row flag fill_line_p. */
15828
15829 static void
15830 extend_face_to_end_of_line (it)
15831 struct it *it;
15832 {
15833 struct face *face;
15834 struct frame *f = it->f;
15835
15836 /* If line is already filled, do nothing. */
15837 if (it->current_x >= it->last_visible_x)
15838 return;
15839
15840 /* Face extension extends the background and box of IT->face_id
15841 to the end of the line. If the background equals the background
15842 of the frame, we don't have to do anything. */
15843 if (it->face_before_selective_p)
15844 face = FACE_FROM_ID (it->f, it->saved_face_id);
15845 else
15846 face = FACE_FROM_ID (f, it->face_id);
15847
15848 if (FRAME_WINDOW_P (f)
15849 && it->glyph_row->displays_text_p
15850 && face->box == FACE_NO_BOX
15851 && face->background == FRAME_BACKGROUND_PIXEL (f)
15852 && !face->stipple)
15853 return;
15854
15855 /* Set the glyph row flag indicating that the face of the last glyph
15856 in the text area has to be drawn to the end of the text area. */
15857 it->glyph_row->fill_line_p = 1;
15858
15859 /* If current character of IT is not ASCII, make sure we have the
15860 ASCII face. This will be automatically undone the next time
15861 get_next_display_element returns a multibyte character. Note
15862 that the character will always be single byte in unibyte text. */
15863 if (!ASCII_CHAR_P (it->c))
15864 {
15865 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15866 }
15867
15868 if (FRAME_WINDOW_P (f))
15869 {
15870 /* If the row is empty, add a space with the current face of IT,
15871 so that we know which face to draw. */
15872 if (it->glyph_row->used[TEXT_AREA] == 0)
15873 {
15874 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15875 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15876 it->glyph_row->used[TEXT_AREA] = 1;
15877 }
15878 }
15879 else
15880 {
15881 /* Save some values that must not be changed. */
15882 int saved_x = it->current_x;
15883 struct text_pos saved_pos;
15884 Lisp_Object saved_object;
15885 enum display_element_type saved_what = it->what;
15886 int saved_face_id = it->face_id;
15887
15888 saved_object = it->object;
15889 saved_pos = it->position;
15890
15891 it->what = IT_CHARACTER;
15892 bzero (&it->position, sizeof it->position);
15893 it->object = make_number (0);
15894 it->c = ' ';
15895 it->len = 1;
15896 it->face_id = face->id;
15897
15898 PRODUCE_GLYPHS (it);
15899
15900 while (it->current_x <= it->last_visible_x)
15901 PRODUCE_GLYPHS (it);
15902
15903 /* Don't count these blanks really. It would let us insert a left
15904 truncation glyph below and make us set the cursor on them, maybe. */
15905 it->current_x = saved_x;
15906 it->object = saved_object;
15907 it->position = saved_pos;
15908 it->what = saved_what;
15909 it->face_id = saved_face_id;
15910 }
15911 }
15912
15913
15914 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15915 trailing whitespace. */
15916
15917 static int
15918 trailing_whitespace_p (charpos)
15919 int charpos;
15920 {
15921 int bytepos = CHAR_TO_BYTE (charpos);
15922 int c = 0;
15923
15924 while (bytepos < ZV_BYTE
15925 && (c = FETCH_CHAR (bytepos),
15926 c == ' ' || c == '\t'))
15927 ++bytepos;
15928
15929 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15930 {
15931 if (bytepos != PT_BYTE)
15932 return 1;
15933 }
15934 return 0;
15935 }
15936
15937
15938 /* Highlight trailing whitespace, if any, in ROW. */
15939
15940 void
15941 highlight_trailing_whitespace (f, row)
15942 struct frame *f;
15943 struct glyph_row *row;
15944 {
15945 int used = row->used[TEXT_AREA];
15946
15947 if (used)
15948 {
15949 struct glyph *start = row->glyphs[TEXT_AREA];
15950 struct glyph *glyph = start + used - 1;
15951
15952 /* Skip over glyphs inserted to display the cursor at the
15953 end of a line, for extending the face of the last glyph
15954 to the end of the line on terminals, and for truncation
15955 and continuation glyphs. */
15956 while (glyph >= start
15957 && glyph->type == CHAR_GLYPH
15958 && INTEGERP (glyph->object))
15959 --glyph;
15960
15961 /* If last glyph is a space or stretch, and it's trailing
15962 whitespace, set the face of all trailing whitespace glyphs in
15963 IT->glyph_row to `trailing-whitespace'. */
15964 if (glyph >= start
15965 && BUFFERP (glyph->object)
15966 && (glyph->type == STRETCH_GLYPH
15967 || (glyph->type == CHAR_GLYPH
15968 && glyph->u.ch == ' '))
15969 && trailing_whitespace_p (glyph->charpos))
15970 {
15971 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
15972 if (face_id < 0)
15973 return;
15974
15975 while (glyph >= start
15976 && BUFFERP (glyph->object)
15977 && (glyph->type == STRETCH_GLYPH
15978 || (glyph->type == CHAR_GLYPH
15979 && glyph->u.ch == ' ')))
15980 (glyph--)->face_id = face_id;
15981 }
15982 }
15983 }
15984
15985
15986 /* Value is non-zero if glyph row ROW in window W should be
15987 used to hold the cursor. */
15988
15989 static int
15990 cursor_row_p (w, row)
15991 struct window *w;
15992 struct glyph_row *row;
15993 {
15994 int cursor_row_p = 1;
15995
15996 if (PT == MATRIX_ROW_END_CHARPOS (row))
15997 {
15998 /* If the row ends with a newline from a string, we don't want
15999 the cursor there, but we still want it at the start of the
16000 string if the string starts in this row.
16001 If the row is continued it doesn't end in a newline. */
16002 if (CHARPOS (row->end.string_pos) >= 0)
16003 cursor_row_p = (row->continued_p
16004 || PT >= MATRIX_ROW_START_CHARPOS (row));
16005 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16006 {
16007 /* If the row ends in middle of a real character,
16008 and the line is continued, we want the cursor here.
16009 That's because MATRIX_ROW_END_CHARPOS would equal
16010 PT if PT is before the character. */
16011 if (!row->ends_in_ellipsis_p)
16012 cursor_row_p = row->continued_p;
16013 else
16014 /* If the row ends in an ellipsis, then
16015 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16016 We want that position to be displayed after the ellipsis. */
16017 cursor_row_p = 0;
16018 }
16019 /* If the row ends at ZV, display the cursor at the end of that
16020 row instead of at the start of the row below. */
16021 else if (row->ends_at_zv_p)
16022 cursor_row_p = 1;
16023 else
16024 cursor_row_p = 0;
16025 }
16026
16027 return cursor_row_p;
16028 }
16029
16030
16031 /* Construct the glyph row IT->glyph_row in the desired matrix of
16032 IT->w from text at the current position of IT. See dispextern.h
16033 for an overview of struct it. Value is non-zero if
16034 IT->glyph_row displays text, as opposed to a line displaying ZV
16035 only. */
16036
16037 static int
16038 display_line (it)
16039 struct it *it;
16040 {
16041 struct glyph_row *row = it->glyph_row;
16042 Lisp_Object overlay_arrow_string;
16043
16044 /* We always start displaying at hpos zero even if hscrolled. */
16045 xassert (it->hpos == 0 && it->current_x == 0);
16046
16047 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16048 >= it->w->desired_matrix->nrows)
16049 {
16050 it->w->nrows_scale_factor++;
16051 fonts_changed_p = 1;
16052 return 0;
16053 }
16054
16055 /* Is IT->w showing the region? */
16056 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16057
16058 /* Clear the result glyph row and enable it. */
16059 prepare_desired_row (row);
16060
16061 row->y = it->current_y;
16062 row->start = it->start;
16063 row->continuation_lines_width = it->continuation_lines_width;
16064 row->displays_text_p = 1;
16065 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16066 it->starts_in_middle_of_char_p = 0;
16067
16068 /* Arrange the overlays nicely for our purposes. Usually, we call
16069 display_line on only one line at a time, in which case this
16070 can't really hurt too much, or we call it on lines which appear
16071 one after another in the buffer, in which case all calls to
16072 recenter_overlay_lists but the first will be pretty cheap. */
16073 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16074
16075 /* Move over display elements that are not visible because we are
16076 hscrolled. This may stop at an x-position < IT->first_visible_x
16077 if the first glyph is partially visible or if we hit a line end. */
16078 if (it->current_x < it->first_visible_x)
16079 {
16080 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16081 MOVE_TO_POS | MOVE_TO_X);
16082 }
16083
16084 /* Get the initial row height. This is either the height of the
16085 text hscrolled, if there is any, or zero. */
16086 row->ascent = it->max_ascent;
16087 row->height = it->max_ascent + it->max_descent;
16088 row->phys_ascent = it->max_phys_ascent;
16089 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16090 row->extra_line_spacing = it->max_extra_line_spacing;
16091
16092 /* Loop generating characters. The loop is left with IT on the next
16093 character to display. */
16094 while (1)
16095 {
16096 int n_glyphs_before, hpos_before, x_before;
16097 int x, i, nglyphs;
16098 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16099
16100 /* Retrieve the next thing to display. Value is zero if end of
16101 buffer reached. */
16102 if (!get_next_display_element (it))
16103 {
16104 /* Maybe add a space at the end of this line that is used to
16105 display the cursor there under X. Set the charpos of the
16106 first glyph of blank lines not corresponding to any text
16107 to -1. */
16108 #ifdef HAVE_WINDOW_SYSTEM
16109 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16110 row->exact_window_width_line_p = 1;
16111 else
16112 #endif /* HAVE_WINDOW_SYSTEM */
16113 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16114 || row->used[TEXT_AREA] == 0)
16115 {
16116 row->glyphs[TEXT_AREA]->charpos = -1;
16117 row->displays_text_p = 0;
16118
16119 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16120 && (!MINI_WINDOW_P (it->w)
16121 || (minibuf_level && EQ (it->window, minibuf_window))))
16122 row->indicate_empty_line_p = 1;
16123 }
16124
16125 it->continuation_lines_width = 0;
16126 row->ends_at_zv_p = 1;
16127 break;
16128 }
16129
16130 /* Now, get the metrics of what we want to display. This also
16131 generates glyphs in `row' (which is IT->glyph_row). */
16132 n_glyphs_before = row->used[TEXT_AREA];
16133 x = it->current_x;
16134
16135 /* Remember the line height so far in case the next element doesn't
16136 fit on the line. */
16137 if (!it->truncate_lines_p)
16138 {
16139 ascent = it->max_ascent;
16140 descent = it->max_descent;
16141 phys_ascent = it->max_phys_ascent;
16142 phys_descent = it->max_phys_descent;
16143 }
16144
16145 PRODUCE_GLYPHS (it);
16146
16147 /* If this display element was in marginal areas, continue with
16148 the next one. */
16149 if (it->area != TEXT_AREA)
16150 {
16151 row->ascent = max (row->ascent, it->max_ascent);
16152 row->height = max (row->height, it->max_ascent + it->max_descent);
16153 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16154 row->phys_height = max (row->phys_height,
16155 it->max_phys_ascent + it->max_phys_descent);
16156 row->extra_line_spacing = max (row->extra_line_spacing,
16157 it->max_extra_line_spacing);
16158 set_iterator_to_next (it, 1);
16159 continue;
16160 }
16161
16162 /* Does the display element fit on the line? If we truncate
16163 lines, we should draw past the right edge of the window. If
16164 we don't truncate, we want to stop so that we can display the
16165 continuation glyph before the right margin. If lines are
16166 continued, there are two possible strategies for characters
16167 resulting in more than 1 glyph (e.g. tabs): Display as many
16168 glyphs as possible in this line and leave the rest for the
16169 continuation line, or display the whole element in the next
16170 line. Original redisplay did the former, so we do it also. */
16171 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16172 hpos_before = it->hpos;
16173 x_before = x;
16174
16175 if (/* Not a newline. */
16176 nglyphs > 0
16177 /* Glyphs produced fit entirely in the line. */
16178 && it->current_x < it->last_visible_x)
16179 {
16180 it->hpos += nglyphs;
16181 row->ascent = max (row->ascent, it->max_ascent);
16182 row->height = max (row->height, it->max_ascent + it->max_descent);
16183 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16184 row->phys_height = max (row->phys_height,
16185 it->max_phys_ascent + it->max_phys_descent);
16186 row->extra_line_spacing = max (row->extra_line_spacing,
16187 it->max_extra_line_spacing);
16188 if (it->current_x - it->pixel_width < it->first_visible_x)
16189 row->x = x - it->first_visible_x;
16190 }
16191 else
16192 {
16193 int new_x;
16194 struct glyph *glyph;
16195
16196 for (i = 0; i < nglyphs; ++i, x = new_x)
16197 {
16198 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16199 new_x = x + glyph->pixel_width;
16200
16201 if (/* Lines are continued. */
16202 !it->truncate_lines_p
16203 && (/* Glyph doesn't fit on the line. */
16204 new_x > it->last_visible_x
16205 /* Or it fits exactly on a window system frame. */
16206 || (new_x == it->last_visible_x
16207 && FRAME_WINDOW_P (it->f))))
16208 {
16209 /* End of a continued line. */
16210
16211 if (it->hpos == 0
16212 || (new_x == it->last_visible_x
16213 && FRAME_WINDOW_P (it->f)))
16214 {
16215 /* Current glyph is the only one on the line or
16216 fits exactly on the line. We must continue
16217 the line because we can't draw the cursor
16218 after the glyph. */
16219 row->continued_p = 1;
16220 it->current_x = new_x;
16221 it->continuation_lines_width += new_x;
16222 ++it->hpos;
16223 if (i == nglyphs - 1)
16224 {
16225 set_iterator_to_next (it, 1);
16226 #ifdef HAVE_WINDOW_SYSTEM
16227 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16228 {
16229 if (!get_next_display_element (it))
16230 {
16231 row->exact_window_width_line_p = 1;
16232 it->continuation_lines_width = 0;
16233 row->continued_p = 0;
16234 row->ends_at_zv_p = 1;
16235 }
16236 else if (ITERATOR_AT_END_OF_LINE_P (it))
16237 {
16238 row->continued_p = 0;
16239 row->exact_window_width_line_p = 1;
16240 }
16241 }
16242 #endif /* HAVE_WINDOW_SYSTEM */
16243 }
16244 }
16245 else if (CHAR_GLYPH_PADDING_P (*glyph)
16246 && !FRAME_WINDOW_P (it->f))
16247 {
16248 /* A padding glyph that doesn't fit on this line.
16249 This means the whole character doesn't fit
16250 on the line. */
16251 row->used[TEXT_AREA] = n_glyphs_before;
16252
16253 /* Fill the rest of the row with continuation
16254 glyphs like in 20.x. */
16255 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16256 < row->glyphs[1 + TEXT_AREA])
16257 produce_special_glyphs (it, IT_CONTINUATION);
16258
16259 row->continued_p = 1;
16260 it->current_x = x_before;
16261 it->continuation_lines_width += x_before;
16262
16263 /* Restore the height to what it was before the
16264 element not fitting on the line. */
16265 it->max_ascent = ascent;
16266 it->max_descent = descent;
16267 it->max_phys_ascent = phys_ascent;
16268 it->max_phys_descent = phys_descent;
16269 }
16270 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16271 {
16272 /* A TAB that extends past the right edge of the
16273 window. This produces a single glyph on
16274 window system frames. We leave the glyph in
16275 this row and let it fill the row, but don't
16276 consume the TAB. */
16277 it->continuation_lines_width += it->last_visible_x;
16278 row->ends_in_middle_of_char_p = 1;
16279 row->continued_p = 1;
16280 glyph->pixel_width = it->last_visible_x - x;
16281 it->starts_in_middle_of_char_p = 1;
16282 }
16283 else
16284 {
16285 /* Something other than a TAB that draws past
16286 the right edge of the window. Restore
16287 positions to values before the element. */
16288 row->used[TEXT_AREA] = n_glyphs_before + i;
16289
16290 /* Display continuation glyphs. */
16291 if (!FRAME_WINDOW_P (it->f))
16292 produce_special_glyphs (it, IT_CONTINUATION);
16293 row->continued_p = 1;
16294
16295 it->current_x = x_before;
16296 it->continuation_lines_width += x;
16297 extend_face_to_end_of_line (it);
16298
16299 if (nglyphs > 1 && i > 0)
16300 {
16301 row->ends_in_middle_of_char_p = 1;
16302 it->starts_in_middle_of_char_p = 1;
16303 }
16304
16305 /* Restore the height to what it was before the
16306 element not fitting on the line. */
16307 it->max_ascent = ascent;
16308 it->max_descent = descent;
16309 it->max_phys_ascent = phys_ascent;
16310 it->max_phys_descent = phys_descent;
16311 }
16312
16313 break;
16314 }
16315 else if (new_x > it->first_visible_x)
16316 {
16317 /* Increment number of glyphs actually displayed. */
16318 ++it->hpos;
16319
16320 if (x < it->first_visible_x)
16321 /* Glyph is partially visible, i.e. row starts at
16322 negative X position. */
16323 row->x = x - it->first_visible_x;
16324 }
16325 else
16326 {
16327 /* Glyph is completely off the left margin of the
16328 window. This should not happen because of the
16329 move_it_in_display_line at the start of this
16330 function, unless the text display area of the
16331 window is empty. */
16332 xassert (it->first_visible_x <= it->last_visible_x);
16333 }
16334 }
16335
16336 row->ascent = max (row->ascent, it->max_ascent);
16337 row->height = max (row->height, it->max_ascent + it->max_descent);
16338 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16339 row->phys_height = max (row->phys_height,
16340 it->max_phys_ascent + it->max_phys_descent);
16341 row->extra_line_spacing = max (row->extra_line_spacing,
16342 it->max_extra_line_spacing);
16343
16344 /* End of this display line if row is continued. */
16345 if (row->continued_p || row->ends_at_zv_p)
16346 break;
16347 }
16348
16349 at_end_of_line:
16350 /* Is this a line end? If yes, we're also done, after making
16351 sure that a non-default face is extended up to the right
16352 margin of the window. */
16353 if (ITERATOR_AT_END_OF_LINE_P (it))
16354 {
16355 int used_before = row->used[TEXT_AREA];
16356
16357 row->ends_in_newline_from_string_p = STRINGP (it->object);
16358
16359 #ifdef HAVE_WINDOW_SYSTEM
16360 /* Add a space at the end of the line that is used to
16361 display the cursor there. */
16362 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16363 append_space_for_newline (it, 0);
16364 #endif /* HAVE_WINDOW_SYSTEM */
16365
16366 /* Extend the face to the end of the line. */
16367 extend_face_to_end_of_line (it);
16368
16369 /* Make sure we have the position. */
16370 if (used_before == 0)
16371 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16372
16373 /* Consume the line end. This skips over invisible lines. */
16374 set_iterator_to_next (it, 1);
16375 it->continuation_lines_width = 0;
16376 break;
16377 }
16378
16379 /* Proceed with next display element. Note that this skips
16380 over lines invisible because of selective display. */
16381 set_iterator_to_next (it, 1);
16382
16383 /* If we truncate lines, we are done when the last displayed
16384 glyphs reach past the right margin of the window. */
16385 if (it->truncate_lines_p
16386 && (FRAME_WINDOW_P (it->f)
16387 ? (it->current_x >= it->last_visible_x)
16388 : (it->current_x > it->last_visible_x)))
16389 {
16390 /* Maybe add truncation glyphs. */
16391 if (!FRAME_WINDOW_P (it->f))
16392 {
16393 int i, n;
16394
16395 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16396 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16397 break;
16398
16399 for (n = row->used[TEXT_AREA]; i < n; ++i)
16400 {
16401 row->used[TEXT_AREA] = i;
16402 produce_special_glyphs (it, IT_TRUNCATION);
16403 }
16404 }
16405 #ifdef HAVE_WINDOW_SYSTEM
16406 else
16407 {
16408 /* Don't truncate if we can overflow newline into fringe. */
16409 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16410 {
16411 if (!get_next_display_element (it))
16412 {
16413 it->continuation_lines_width = 0;
16414 row->ends_at_zv_p = 1;
16415 row->exact_window_width_line_p = 1;
16416 break;
16417 }
16418 if (ITERATOR_AT_END_OF_LINE_P (it))
16419 {
16420 row->exact_window_width_line_p = 1;
16421 goto at_end_of_line;
16422 }
16423 }
16424 }
16425 #endif /* HAVE_WINDOW_SYSTEM */
16426
16427 row->truncated_on_right_p = 1;
16428 it->continuation_lines_width = 0;
16429 reseat_at_next_visible_line_start (it, 0);
16430 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16431 it->hpos = hpos_before;
16432 it->current_x = x_before;
16433 break;
16434 }
16435 }
16436
16437 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16438 at the left window margin. */
16439 if (it->first_visible_x
16440 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16441 {
16442 if (!FRAME_WINDOW_P (it->f))
16443 insert_left_trunc_glyphs (it);
16444 row->truncated_on_left_p = 1;
16445 }
16446
16447 /* If the start of this line is the overlay arrow-position, then
16448 mark this glyph row as the one containing the overlay arrow.
16449 This is clearly a mess with variable size fonts. It would be
16450 better to let it be displayed like cursors under X. */
16451 if ((row->displays_text_p || !overlay_arrow_seen)
16452 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16453 !NILP (overlay_arrow_string)))
16454 {
16455 /* Overlay arrow in window redisplay is a fringe bitmap. */
16456 if (STRINGP (overlay_arrow_string))
16457 {
16458 struct glyph_row *arrow_row
16459 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16460 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16461 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16462 struct glyph *p = row->glyphs[TEXT_AREA];
16463 struct glyph *p2, *end;
16464
16465 /* Copy the arrow glyphs. */
16466 while (glyph < arrow_end)
16467 *p++ = *glyph++;
16468
16469 /* Throw away padding glyphs. */
16470 p2 = p;
16471 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16472 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16473 ++p2;
16474 if (p2 > p)
16475 {
16476 while (p2 < end)
16477 *p++ = *p2++;
16478 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16479 }
16480 }
16481 else
16482 {
16483 xassert (INTEGERP (overlay_arrow_string));
16484 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16485 }
16486 overlay_arrow_seen = 1;
16487 }
16488
16489 /* Compute pixel dimensions of this line. */
16490 compute_line_metrics (it);
16491
16492 /* Remember the position at which this line ends. */
16493 row->end = it->current;
16494
16495 /* Record whether this row ends inside an ellipsis. */
16496 row->ends_in_ellipsis_p
16497 = (it->method == GET_FROM_DISPLAY_VECTOR
16498 && it->ellipsis_p);
16499
16500 /* Save fringe bitmaps in this row. */
16501 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16502 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16503 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16504 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16505
16506 it->left_user_fringe_bitmap = 0;
16507 it->left_user_fringe_face_id = 0;
16508 it->right_user_fringe_bitmap = 0;
16509 it->right_user_fringe_face_id = 0;
16510
16511 /* Maybe set the cursor. */
16512 if (it->w->cursor.vpos < 0
16513 && PT >= MATRIX_ROW_START_CHARPOS (row)
16514 && PT <= MATRIX_ROW_END_CHARPOS (row)
16515 && cursor_row_p (it->w, row))
16516 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16517
16518 /* Highlight trailing whitespace. */
16519 if (!NILP (Vshow_trailing_whitespace))
16520 highlight_trailing_whitespace (it->f, it->glyph_row);
16521
16522 /* Prepare for the next line. This line starts horizontally at (X
16523 HPOS) = (0 0). Vertical positions are incremented. As a
16524 convenience for the caller, IT->glyph_row is set to the next
16525 row to be used. */
16526 it->current_x = it->hpos = 0;
16527 it->current_y += row->height;
16528 ++it->vpos;
16529 ++it->glyph_row;
16530 it->start = it->current;
16531 return row->displays_text_p;
16532 }
16533
16534
16535 \f
16536 /***********************************************************************
16537 Menu Bar
16538 ***********************************************************************/
16539
16540 /* Redisplay the menu bar in the frame for window W.
16541
16542 The menu bar of X frames that don't have X toolkit support is
16543 displayed in a special window W->frame->menu_bar_window.
16544
16545 The menu bar of terminal frames is treated specially as far as
16546 glyph matrices are concerned. Menu bar lines are not part of
16547 windows, so the update is done directly on the frame matrix rows
16548 for the menu bar. */
16549
16550 static void
16551 display_menu_bar (w)
16552 struct window *w;
16553 {
16554 struct frame *f = XFRAME (WINDOW_FRAME (w));
16555 struct it it;
16556 Lisp_Object items;
16557 int i;
16558
16559 /* Don't do all this for graphical frames. */
16560 #ifdef HAVE_NTGUI
16561 if (!NILP (Vwindow_system))
16562 return;
16563 #endif
16564 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16565 if (FRAME_X_P (f))
16566 return;
16567 #endif
16568 #ifdef MAC_OS
16569 if (FRAME_MAC_P (f))
16570 return;
16571 #endif
16572
16573 #ifdef USE_X_TOOLKIT
16574 xassert (!FRAME_WINDOW_P (f));
16575 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16576 it.first_visible_x = 0;
16577 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16578 #else /* not USE_X_TOOLKIT */
16579 if (FRAME_WINDOW_P (f))
16580 {
16581 /* Menu bar lines are displayed in the desired matrix of the
16582 dummy window menu_bar_window. */
16583 struct window *menu_w;
16584 xassert (WINDOWP (f->menu_bar_window));
16585 menu_w = XWINDOW (f->menu_bar_window);
16586 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16587 MENU_FACE_ID);
16588 it.first_visible_x = 0;
16589 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16590 }
16591 else
16592 {
16593 /* This is a TTY frame, i.e. character hpos/vpos are used as
16594 pixel x/y. */
16595 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16596 MENU_FACE_ID);
16597 it.first_visible_x = 0;
16598 it.last_visible_x = FRAME_COLS (f);
16599 }
16600 #endif /* not USE_X_TOOLKIT */
16601
16602 if (! mode_line_inverse_video)
16603 /* Force the menu-bar to be displayed in the default face. */
16604 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16605
16606 /* Clear all rows of the menu bar. */
16607 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16608 {
16609 struct glyph_row *row = it.glyph_row + i;
16610 clear_glyph_row (row);
16611 row->enabled_p = 1;
16612 row->full_width_p = 1;
16613 }
16614
16615 /* Display all items of the menu bar. */
16616 items = FRAME_MENU_BAR_ITEMS (it.f);
16617 for (i = 0; i < XVECTOR (items)->size; i += 4)
16618 {
16619 Lisp_Object string;
16620
16621 /* Stop at nil string. */
16622 string = AREF (items, i + 1);
16623 if (NILP (string))
16624 break;
16625
16626 /* Remember where item was displayed. */
16627 AREF (items, i + 3) = make_number (it.hpos);
16628
16629 /* Display the item, pad with one space. */
16630 if (it.current_x < it.last_visible_x)
16631 display_string (NULL, string, Qnil, 0, 0, &it,
16632 SCHARS (string) + 1, 0, 0, -1);
16633 }
16634
16635 /* Fill out the line with spaces. */
16636 if (it.current_x < it.last_visible_x)
16637 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16638
16639 /* Compute the total height of the lines. */
16640 compute_line_metrics (&it);
16641 }
16642
16643
16644 \f
16645 /***********************************************************************
16646 Mode Line
16647 ***********************************************************************/
16648
16649 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16650 FORCE is non-zero, redisplay mode lines unconditionally.
16651 Otherwise, redisplay only mode lines that are garbaged. Value is
16652 the number of windows whose mode lines were redisplayed. */
16653
16654 static int
16655 redisplay_mode_lines (window, force)
16656 Lisp_Object window;
16657 int force;
16658 {
16659 int nwindows = 0;
16660
16661 while (!NILP (window))
16662 {
16663 struct window *w = XWINDOW (window);
16664
16665 if (WINDOWP (w->hchild))
16666 nwindows += redisplay_mode_lines (w->hchild, force);
16667 else if (WINDOWP (w->vchild))
16668 nwindows += redisplay_mode_lines (w->vchild, force);
16669 else if (force
16670 || FRAME_GARBAGED_P (XFRAME (w->frame))
16671 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16672 {
16673 struct text_pos lpoint;
16674 struct buffer *old = current_buffer;
16675
16676 /* Set the window's buffer for the mode line display. */
16677 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16678 set_buffer_internal_1 (XBUFFER (w->buffer));
16679
16680 /* Point refers normally to the selected window. For any
16681 other window, set up appropriate value. */
16682 if (!EQ (window, selected_window))
16683 {
16684 struct text_pos pt;
16685
16686 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16687 if (CHARPOS (pt) < BEGV)
16688 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16689 else if (CHARPOS (pt) > (ZV - 1))
16690 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16691 else
16692 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16693 }
16694
16695 /* Display mode lines. */
16696 clear_glyph_matrix (w->desired_matrix);
16697 if (display_mode_lines (w))
16698 {
16699 ++nwindows;
16700 w->must_be_updated_p = 1;
16701 }
16702
16703 /* Restore old settings. */
16704 set_buffer_internal_1 (old);
16705 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16706 }
16707
16708 window = w->next;
16709 }
16710
16711 return nwindows;
16712 }
16713
16714
16715 /* Display the mode and/or top line of window W. Value is the number
16716 of mode lines displayed. */
16717
16718 static int
16719 display_mode_lines (w)
16720 struct window *w;
16721 {
16722 Lisp_Object old_selected_window, old_selected_frame;
16723 int n = 0;
16724
16725 old_selected_frame = selected_frame;
16726 selected_frame = w->frame;
16727 old_selected_window = selected_window;
16728 XSETWINDOW (selected_window, w);
16729
16730 /* These will be set while the mode line specs are processed. */
16731 line_number_displayed = 0;
16732 w->column_number_displayed = Qnil;
16733
16734 if (WINDOW_WANTS_MODELINE_P (w))
16735 {
16736 struct window *sel_w = XWINDOW (old_selected_window);
16737
16738 /* Select mode line face based on the real selected window. */
16739 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16740 current_buffer->mode_line_format);
16741 ++n;
16742 }
16743
16744 if (WINDOW_WANTS_HEADER_LINE_P (w))
16745 {
16746 display_mode_line (w, HEADER_LINE_FACE_ID,
16747 current_buffer->header_line_format);
16748 ++n;
16749 }
16750
16751 selected_frame = old_selected_frame;
16752 selected_window = old_selected_window;
16753 return n;
16754 }
16755
16756
16757 /* Display mode or top line of window W. FACE_ID specifies which line
16758 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16759 FORMAT is the mode line format to display. Value is the pixel
16760 height of the mode line displayed. */
16761
16762 static int
16763 display_mode_line (w, face_id, format)
16764 struct window *w;
16765 enum face_id face_id;
16766 Lisp_Object format;
16767 {
16768 struct it it;
16769 struct face *face;
16770 int count = SPECPDL_INDEX ();
16771
16772 init_iterator (&it, w, -1, -1, NULL, face_id);
16773 /* Don't extend on a previously drawn mode-line.
16774 This may happen if called from pos_visible_p. */
16775 it.glyph_row->enabled_p = 0;
16776 prepare_desired_row (it.glyph_row);
16777
16778 it.glyph_row->mode_line_p = 1;
16779
16780 if (! mode_line_inverse_video)
16781 /* Force the mode-line to be displayed in the default face. */
16782 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16783
16784 record_unwind_protect (unwind_format_mode_line,
16785 format_mode_line_unwind_data (NULL, 0));
16786
16787 mode_line_target = MODE_LINE_DISPLAY;
16788
16789 /* Temporarily make frame's keyboard the current kboard so that
16790 kboard-local variables in the mode_line_format will get the right
16791 values. */
16792 push_frame_kboard (it.f);
16793 record_unwind_save_match_data ();
16794 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16795 pop_frame_kboard ();
16796
16797 unbind_to (count, Qnil);
16798
16799 /* Fill up with spaces. */
16800 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16801
16802 compute_line_metrics (&it);
16803 it.glyph_row->full_width_p = 1;
16804 it.glyph_row->continued_p = 0;
16805 it.glyph_row->truncated_on_left_p = 0;
16806 it.glyph_row->truncated_on_right_p = 0;
16807
16808 /* Make a 3D mode-line have a shadow at its right end. */
16809 face = FACE_FROM_ID (it.f, face_id);
16810 extend_face_to_end_of_line (&it);
16811 if (face->box != FACE_NO_BOX)
16812 {
16813 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16814 + it.glyph_row->used[TEXT_AREA] - 1);
16815 last->right_box_line_p = 1;
16816 }
16817
16818 return it.glyph_row->height;
16819 }
16820
16821 /* Move element ELT in LIST to the front of LIST.
16822 Return the updated list. */
16823
16824 static Lisp_Object
16825 move_elt_to_front (elt, list)
16826 Lisp_Object elt, list;
16827 {
16828 register Lisp_Object tail, prev;
16829 register Lisp_Object tem;
16830
16831 tail = list;
16832 prev = Qnil;
16833 while (CONSP (tail))
16834 {
16835 tem = XCAR (tail);
16836
16837 if (EQ (elt, tem))
16838 {
16839 /* Splice out the link TAIL. */
16840 if (NILP (prev))
16841 list = XCDR (tail);
16842 else
16843 Fsetcdr (prev, XCDR (tail));
16844
16845 /* Now make it the first. */
16846 Fsetcdr (tail, list);
16847 return tail;
16848 }
16849 else
16850 prev = tail;
16851 tail = XCDR (tail);
16852 QUIT;
16853 }
16854
16855 /* Not found--return unchanged LIST. */
16856 return list;
16857 }
16858
16859 /* Contribute ELT to the mode line for window IT->w. How it
16860 translates into text depends on its data type.
16861
16862 IT describes the display environment in which we display, as usual.
16863
16864 DEPTH is the depth in recursion. It is used to prevent
16865 infinite recursion here.
16866
16867 FIELD_WIDTH is the number of characters the display of ELT should
16868 occupy in the mode line, and PRECISION is the maximum number of
16869 characters to display from ELT's representation. See
16870 display_string for details.
16871
16872 Returns the hpos of the end of the text generated by ELT.
16873
16874 PROPS is a property list to add to any string we encounter.
16875
16876 If RISKY is nonzero, remove (disregard) any properties in any string
16877 we encounter, and ignore :eval and :propertize.
16878
16879 The global variable `mode_line_target' determines whether the
16880 output is passed to `store_mode_line_noprop',
16881 `store_mode_line_string', or `display_string'. */
16882
16883 static int
16884 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16885 struct it *it;
16886 int depth;
16887 int field_width, precision;
16888 Lisp_Object elt, props;
16889 int risky;
16890 {
16891 int n = 0, field, prec;
16892 int literal = 0;
16893
16894 tail_recurse:
16895 if (depth > 100)
16896 elt = build_string ("*too-deep*");
16897
16898 depth++;
16899
16900 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16901 {
16902 case Lisp_String:
16903 {
16904 /* A string: output it and check for %-constructs within it. */
16905 unsigned char c;
16906 int offset = 0;
16907
16908 if (SCHARS (elt) > 0
16909 && (!NILP (props) || risky))
16910 {
16911 Lisp_Object oprops, aelt;
16912 oprops = Ftext_properties_at (make_number (0), elt);
16913
16914 /* If the starting string's properties are not what
16915 we want, translate the string. Also, if the string
16916 is risky, do that anyway. */
16917
16918 if (NILP (Fequal (props, oprops)) || risky)
16919 {
16920 /* If the starting string has properties,
16921 merge the specified ones onto the existing ones. */
16922 if (! NILP (oprops) && !risky)
16923 {
16924 Lisp_Object tem;
16925
16926 oprops = Fcopy_sequence (oprops);
16927 tem = props;
16928 while (CONSP (tem))
16929 {
16930 oprops = Fplist_put (oprops, XCAR (tem),
16931 XCAR (XCDR (tem)));
16932 tem = XCDR (XCDR (tem));
16933 }
16934 props = oprops;
16935 }
16936
16937 aelt = Fassoc (elt, mode_line_proptrans_alist);
16938 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16939 {
16940 /* AELT is what we want. Move it to the front
16941 without consing. */
16942 elt = XCAR (aelt);
16943 mode_line_proptrans_alist
16944 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16945 }
16946 else
16947 {
16948 Lisp_Object tem;
16949
16950 /* If AELT has the wrong props, it is useless.
16951 so get rid of it. */
16952 if (! NILP (aelt))
16953 mode_line_proptrans_alist
16954 = Fdelq (aelt, mode_line_proptrans_alist);
16955
16956 elt = Fcopy_sequence (elt);
16957 Fset_text_properties (make_number (0), Flength (elt),
16958 props, elt);
16959 /* Add this item to mode_line_proptrans_alist. */
16960 mode_line_proptrans_alist
16961 = Fcons (Fcons (elt, props),
16962 mode_line_proptrans_alist);
16963 /* Truncate mode_line_proptrans_alist
16964 to at most 50 elements. */
16965 tem = Fnthcdr (make_number (50),
16966 mode_line_proptrans_alist);
16967 if (! NILP (tem))
16968 XSETCDR (tem, Qnil);
16969 }
16970 }
16971 }
16972
16973 offset = 0;
16974
16975 if (literal)
16976 {
16977 prec = precision - n;
16978 switch (mode_line_target)
16979 {
16980 case MODE_LINE_NOPROP:
16981 case MODE_LINE_TITLE:
16982 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16983 break;
16984 case MODE_LINE_STRING:
16985 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16986 break;
16987 case MODE_LINE_DISPLAY:
16988 n += display_string (NULL, elt, Qnil, 0, 0, it,
16989 0, prec, 0, STRING_MULTIBYTE (elt));
16990 break;
16991 }
16992
16993 break;
16994 }
16995
16996 /* Handle the non-literal case. */
16997
16998 while ((precision <= 0 || n < precision)
16999 && SREF (elt, offset) != 0
17000 && (mode_line_target != MODE_LINE_DISPLAY
17001 || it->current_x < it->last_visible_x))
17002 {
17003 int last_offset = offset;
17004
17005 /* Advance to end of string or next format specifier. */
17006 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17007 ;
17008
17009 if (offset - 1 != last_offset)
17010 {
17011 int nchars, nbytes;
17012
17013 /* Output to end of string or up to '%'. Field width
17014 is length of string. Don't output more than
17015 PRECISION allows us. */
17016 offset--;
17017
17018 prec = c_string_width (SDATA (elt) + last_offset,
17019 offset - last_offset, precision - n,
17020 &nchars, &nbytes);
17021
17022 switch (mode_line_target)
17023 {
17024 case MODE_LINE_NOPROP:
17025 case MODE_LINE_TITLE:
17026 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17027 break;
17028 case MODE_LINE_STRING:
17029 {
17030 int bytepos = last_offset;
17031 int charpos = string_byte_to_char (elt, bytepos);
17032 int endpos = (precision <= 0
17033 ? string_byte_to_char (elt, offset)
17034 : charpos + nchars);
17035
17036 n += store_mode_line_string (NULL,
17037 Fsubstring (elt, make_number (charpos),
17038 make_number (endpos)),
17039 0, 0, 0, Qnil);
17040 }
17041 break;
17042 case MODE_LINE_DISPLAY:
17043 {
17044 int bytepos = last_offset;
17045 int charpos = string_byte_to_char (elt, bytepos);
17046
17047 if (precision <= 0)
17048 nchars = string_byte_to_char (elt, offset) - charpos;
17049 n += display_string (NULL, elt, Qnil, 0, charpos,
17050 it, 0, nchars, 0,
17051 STRING_MULTIBYTE (elt));
17052 }
17053 break;
17054 }
17055 }
17056 else /* c == '%' */
17057 {
17058 int percent_position = offset;
17059
17060 /* Get the specified minimum width. Zero means
17061 don't pad. */
17062 field = 0;
17063 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17064 field = field * 10 + c - '0';
17065
17066 /* Don't pad beyond the total padding allowed. */
17067 if (field_width - n > 0 && field > field_width - n)
17068 field = field_width - n;
17069
17070 /* Note that either PRECISION <= 0 or N < PRECISION. */
17071 prec = precision - n;
17072
17073 if (c == 'M')
17074 n += display_mode_element (it, depth, field, prec,
17075 Vglobal_mode_string, props,
17076 risky);
17077 else if (c != 0)
17078 {
17079 int multibyte;
17080 int bytepos, charpos;
17081 unsigned char *spec;
17082
17083 bytepos = percent_position;
17084 charpos = (STRING_MULTIBYTE (elt)
17085 ? string_byte_to_char (elt, bytepos)
17086 : bytepos);
17087
17088 spec
17089 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17090
17091 switch (mode_line_target)
17092 {
17093 case MODE_LINE_NOPROP:
17094 case MODE_LINE_TITLE:
17095 n += store_mode_line_noprop (spec, field, prec);
17096 break;
17097 case MODE_LINE_STRING:
17098 {
17099 int len = strlen (spec);
17100 Lisp_Object tem = make_string (spec, len);
17101 props = Ftext_properties_at (make_number (charpos), elt);
17102 /* Should only keep face property in props */
17103 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17104 }
17105 break;
17106 case MODE_LINE_DISPLAY:
17107 {
17108 int nglyphs_before, nwritten;
17109
17110 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17111 nwritten = display_string (spec, Qnil, elt,
17112 charpos, 0, it,
17113 field, prec, 0,
17114 multibyte);
17115
17116 /* Assign to the glyphs written above the
17117 string where the `%x' came from, position
17118 of the `%'. */
17119 if (nwritten > 0)
17120 {
17121 struct glyph *glyph
17122 = (it->glyph_row->glyphs[TEXT_AREA]
17123 + nglyphs_before);
17124 int i;
17125
17126 for (i = 0; i < nwritten; ++i)
17127 {
17128 glyph[i].object = elt;
17129 glyph[i].charpos = charpos;
17130 }
17131
17132 n += nwritten;
17133 }
17134 }
17135 break;
17136 }
17137 }
17138 else /* c == 0 */
17139 break;
17140 }
17141 }
17142 }
17143 break;
17144
17145 case Lisp_Symbol:
17146 /* A symbol: process the value of the symbol recursively
17147 as if it appeared here directly. Avoid error if symbol void.
17148 Special case: if value of symbol is a string, output the string
17149 literally. */
17150 {
17151 register Lisp_Object tem;
17152
17153 /* If the variable is not marked as risky to set
17154 then its contents are risky to use. */
17155 if (NILP (Fget (elt, Qrisky_local_variable)))
17156 risky = 1;
17157
17158 tem = Fboundp (elt);
17159 if (!NILP (tem))
17160 {
17161 tem = Fsymbol_value (elt);
17162 /* If value is a string, output that string literally:
17163 don't check for % within it. */
17164 if (STRINGP (tem))
17165 literal = 1;
17166
17167 if (!EQ (tem, elt))
17168 {
17169 /* Give up right away for nil or t. */
17170 elt = tem;
17171 goto tail_recurse;
17172 }
17173 }
17174 }
17175 break;
17176
17177 case Lisp_Cons:
17178 {
17179 register Lisp_Object car, tem;
17180
17181 /* A cons cell: five distinct cases.
17182 If first element is :eval or :propertize, do something special.
17183 If first element is a string or a cons, process all the elements
17184 and effectively concatenate them.
17185 If first element is a negative number, truncate displaying cdr to
17186 at most that many characters. If positive, pad (with spaces)
17187 to at least that many characters.
17188 If first element is a symbol, process the cadr or caddr recursively
17189 according to whether the symbol's value is non-nil or nil. */
17190 car = XCAR (elt);
17191 if (EQ (car, QCeval))
17192 {
17193 /* An element of the form (:eval FORM) means evaluate FORM
17194 and use the result as mode line elements. */
17195
17196 if (risky)
17197 break;
17198
17199 if (CONSP (XCDR (elt)))
17200 {
17201 Lisp_Object spec;
17202 spec = safe_eval (XCAR (XCDR (elt)));
17203 n += display_mode_element (it, depth, field_width - n,
17204 precision - n, spec, props,
17205 risky);
17206 }
17207 }
17208 else if (EQ (car, QCpropertize))
17209 {
17210 /* An element of the form (:propertize ELT PROPS...)
17211 means display ELT but applying properties PROPS. */
17212
17213 if (risky)
17214 break;
17215
17216 if (CONSP (XCDR (elt)))
17217 n += display_mode_element (it, depth, field_width - n,
17218 precision - n, XCAR (XCDR (elt)),
17219 XCDR (XCDR (elt)), risky);
17220 }
17221 else if (SYMBOLP (car))
17222 {
17223 tem = Fboundp (car);
17224 elt = XCDR (elt);
17225 if (!CONSP (elt))
17226 goto invalid;
17227 /* elt is now the cdr, and we know it is a cons cell.
17228 Use its car if CAR has a non-nil value. */
17229 if (!NILP (tem))
17230 {
17231 tem = Fsymbol_value (car);
17232 if (!NILP (tem))
17233 {
17234 elt = XCAR (elt);
17235 goto tail_recurse;
17236 }
17237 }
17238 /* Symbol's value is nil (or symbol is unbound)
17239 Get the cddr of the original list
17240 and if possible find the caddr and use that. */
17241 elt = XCDR (elt);
17242 if (NILP (elt))
17243 break;
17244 else if (!CONSP (elt))
17245 goto invalid;
17246 elt = XCAR (elt);
17247 goto tail_recurse;
17248 }
17249 else if (INTEGERP (car))
17250 {
17251 register int lim = XINT (car);
17252 elt = XCDR (elt);
17253 if (lim < 0)
17254 {
17255 /* Negative int means reduce maximum width. */
17256 if (precision <= 0)
17257 precision = -lim;
17258 else
17259 precision = min (precision, -lim);
17260 }
17261 else if (lim > 0)
17262 {
17263 /* Padding specified. Don't let it be more than
17264 current maximum. */
17265 if (precision > 0)
17266 lim = min (precision, lim);
17267
17268 /* If that's more padding than already wanted, queue it.
17269 But don't reduce padding already specified even if
17270 that is beyond the current truncation point. */
17271 field_width = max (lim, field_width);
17272 }
17273 goto tail_recurse;
17274 }
17275 else if (STRINGP (car) || CONSP (car))
17276 {
17277 register int limit = 50;
17278 /* Limit is to protect against circular lists. */
17279 while (CONSP (elt)
17280 && --limit > 0
17281 && (precision <= 0 || n < precision))
17282 {
17283 n += display_mode_element (it, depth,
17284 /* Do padding only after the last
17285 element in the list. */
17286 (! CONSP (XCDR (elt))
17287 ? field_width - n
17288 : 0),
17289 precision - n, XCAR (elt),
17290 props, risky);
17291 elt = XCDR (elt);
17292 }
17293 }
17294 }
17295 break;
17296
17297 default:
17298 invalid:
17299 elt = build_string ("*invalid*");
17300 goto tail_recurse;
17301 }
17302
17303 /* Pad to FIELD_WIDTH. */
17304 if (field_width > 0 && n < field_width)
17305 {
17306 switch (mode_line_target)
17307 {
17308 case MODE_LINE_NOPROP:
17309 case MODE_LINE_TITLE:
17310 n += store_mode_line_noprop ("", field_width - n, 0);
17311 break;
17312 case MODE_LINE_STRING:
17313 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17314 break;
17315 case MODE_LINE_DISPLAY:
17316 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17317 0, 0, 0);
17318 break;
17319 }
17320 }
17321
17322 return n;
17323 }
17324
17325 /* Store a mode-line string element in mode_line_string_list.
17326
17327 If STRING is non-null, display that C string. Otherwise, the Lisp
17328 string LISP_STRING is displayed.
17329
17330 FIELD_WIDTH is the minimum number of output glyphs to produce.
17331 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17332 with spaces. FIELD_WIDTH <= 0 means don't pad.
17333
17334 PRECISION is the maximum number of characters to output from
17335 STRING. PRECISION <= 0 means don't truncate the string.
17336
17337 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17338 properties to the string.
17339
17340 PROPS are the properties to add to the string.
17341 The mode_line_string_face face property is always added to the string.
17342 */
17343
17344 static int
17345 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17346 char *string;
17347 Lisp_Object lisp_string;
17348 int copy_string;
17349 int field_width;
17350 int precision;
17351 Lisp_Object props;
17352 {
17353 int len;
17354 int n = 0;
17355
17356 if (string != NULL)
17357 {
17358 len = strlen (string);
17359 if (precision > 0 && len > precision)
17360 len = precision;
17361 lisp_string = make_string (string, len);
17362 if (NILP (props))
17363 props = mode_line_string_face_prop;
17364 else if (!NILP (mode_line_string_face))
17365 {
17366 Lisp_Object face = Fplist_get (props, Qface);
17367 props = Fcopy_sequence (props);
17368 if (NILP (face))
17369 face = mode_line_string_face;
17370 else
17371 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17372 props = Fplist_put (props, Qface, face);
17373 }
17374 Fadd_text_properties (make_number (0), make_number (len),
17375 props, lisp_string);
17376 }
17377 else
17378 {
17379 len = XFASTINT (Flength (lisp_string));
17380 if (precision > 0 && len > precision)
17381 {
17382 len = precision;
17383 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17384 precision = -1;
17385 }
17386 if (!NILP (mode_line_string_face))
17387 {
17388 Lisp_Object face;
17389 if (NILP (props))
17390 props = Ftext_properties_at (make_number (0), lisp_string);
17391 face = Fplist_get (props, Qface);
17392 if (NILP (face))
17393 face = mode_line_string_face;
17394 else
17395 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17396 props = Fcons (Qface, Fcons (face, Qnil));
17397 if (copy_string)
17398 lisp_string = Fcopy_sequence (lisp_string);
17399 }
17400 if (!NILP (props))
17401 Fadd_text_properties (make_number (0), make_number (len),
17402 props, lisp_string);
17403 }
17404
17405 if (len > 0)
17406 {
17407 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17408 n += len;
17409 }
17410
17411 if (field_width > len)
17412 {
17413 field_width -= len;
17414 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17415 if (!NILP (props))
17416 Fadd_text_properties (make_number (0), make_number (field_width),
17417 props, lisp_string);
17418 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17419 n += field_width;
17420 }
17421
17422 return n;
17423 }
17424
17425
17426 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17427 1, 4, 0,
17428 doc: /* Format a string out of a mode line format specification.
17429 First arg FORMAT specifies the mode line format (see `mode-line-format'
17430 for details) to use.
17431
17432 Optional second arg FACE specifies the face property to put
17433 on all characters for which no face is specified.
17434 The value t means whatever face the window's mode line currently uses
17435 \(either `mode-line' or `mode-line-inactive', depending).
17436 A value of nil means the default is no face property.
17437 If FACE is an integer, the value string has no text properties.
17438
17439 Optional third and fourth args WINDOW and BUFFER specify the window
17440 and buffer to use as the context for the formatting (defaults
17441 are the selected window and the window's buffer). */)
17442 (format, face, window, buffer)
17443 Lisp_Object format, face, window, buffer;
17444 {
17445 struct it it;
17446 int len;
17447 struct window *w;
17448 struct buffer *old_buffer = NULL;
17449 int face_id = -1;
17450 int no_props = INTEGERP (face);
17451 int count = SPECPDL_INDEX ();
17452 Lisp_Object str;
17453 int string_start = 0;
17454
17455 if (NILP (window))
17456 window = selected_window;
17457 CHECK_WINDOW (window);
17458 w = XWINDOW (window);
17459
17460 if (NILP (buffer))
17461 buffer = w->buffer;
17462 CHECK_BUFFER (buffer);
17463
17464 if (NILP (format))
17465 return build_string ("");
17466
17467 if (no_props)
17468 face = Qnil;
17469
17470 if (!NILP (face))
17471 {
17472 if (EQ (face, Qt))
17473 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17474 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17475 }
17476
17477 if (face_id < 0)
17478 face_id = DEFAULT_FACE_ID;
17479
17480 if (XBUFFER (buffer) != current_buffer)
17481 old_buffer = current_buffer;
17482
17483 /* Save things including mode_line_proptrans_alist,
17484 and set that to nil so that we don't alter the outer value. */
17485 record_unwind_protect (unwind_format_mode_line,
17486 format_mode_line_unwind_data (old_buffer, 1));
17487 mode_line_proptrans_alist = Qnil;
17488
17489 if (old_buffer)
17490 set_buffer_internal_1 (XBUFFER (buffer));
17491
17492 init_iterator (&it, w, -1, -1, NULL, face_id);
17493
17494 if (no_props)
17495 {
17496 mode_line_target = MODE_LINE_NOPROP;
17497 mode_line_string_face_prop = Qnil;
17498 mode_line_string_list = Qnil;
17499 string_start = MODE_LINE_NOPROP_LEN (0);
17500 }
17501 else
17502 {
17503 mode_line_target = MODE_LINE_STRING;
17504 mode_line_string_list = Qnil;
17505 mode_line_string_face = face;
17506 mode_line_string_face_prop
17507 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17508 }
17509
17510 push_frame_kboard (it.f);
17511 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17512 pop_frame_kboard ();
17513
17514 if (no_props)
17515 {
17516 len = MODE_LINE_NOPROP_LEN (string_start);
17517 str = make_string (mode_line_noprop_buf + string_start, len);
17518 }
17519 else
17520 {
17521 mode_line_string_list = Fnreverse (mode_line_string_list);
17522 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17523 make_string ("", 0));
17524 }
17525
17526 unbind_to (count, Qnil);
17527 return str;
17528 }
17529
17530 /* Write a null-terminated, right justified decimal representation of
17531 the positive integer D to BUF using a minimal field width WIDTH. */
17532
17533 static void
17534 pint2str (buf, width, d)
17535 register char *buf;
17536 register int width;
17537 register int d;
17538 {
17539 register char *p = buf;
17540
17541 if (d <= 0)
17542 *p++ = '0';
17543 else
17544 {
17545 while (d > 0)
17546 {
17547 *p++ = d % 10 + '0';
17548 d /= 10;
17549 }
17550 }
17551
17552 for (width -= (int) (p - buf); width > 0; --width)
17553 *p++ = ' ';
17554 *p-- = '\0';
17555 while (p > buf)
17556 {
17557 d = *buf;
17558 *buf++ = *p;
17559 *p-- = d;
17560 }
17561 }
17562
17563 /* Write a null-terminated, right justified decimal and "human
17564 readable" representation of the nonnegative integer D to BUF using
17565 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17566
17567 static const char power_letter[] =
17568 {
17569 0, /* not used */
17570 'k', /* kilo */
17571 'M', /* mega */
17572 'G', /* giga */
17573 'T', /* tera */
17574 'P', /* peta */
17575 'E', /* exa */
17576 'Z', /* zetta */
17577 'Y' /* yotta */
17578 };
17579
17580 static void
17581 pint2hrstr (buf, width, d)
17582 char *buf;
17583 int width;
17584 int d;
17585 {
17586 /* We aim to represent the nonnegative integer D as
17587 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17588 int quotient = d;
17589 int remainder = 0;
17590 /* -1 means: do not use TENTHS. */
17591 int tenths = -1;
17592 int exponent = 0;
17593
17594 /* Length of QUOTIENT.TENTHS as a string. */
17595 int length;
17596
17597 char * psuffix;
17598 char * p;
17599
17600 if (1000 <= quotient)
17601 {
17602 /* Scale to the appropriate EXPONENT. */
17603 do
17604 {
17605 remainder = quotient % 1000;
17606 quotient /= 1000;
17607 exponent++;
17608 }
17609 while (1000 <= quotient);
17610
17611 /* Round to nearest and decide whether to use TENTHS or not. */
17612 if (quotient <= 9)
17613 {
17614 tenths = remainder / 100;
17615 if (50 <= remainder % 100)
17616 {
17617 if (tenths < 9)
17618 tenths++;
17619 else
17620 {
17621 quotient++;
17622 if (quotient == 10)
17623 tenths = -1;
17624 else
17625 tenths = 0;
17626 }
17627 }
17628 }
17629 else
17630 if (500 <= remainder)
17631 {
17632 if (quotient < 999)
17633 quotient++;
17634 else
17635 {
17636 quotient = 1;
17637 exponent++;
17638 tenths = 0;
17639 }
17640 }
17641 }
17642
17643 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17644 if (tenths == -1 && quotient <= 99)
17645 if (quotient <= 9)
17646 length = 1;
17647 else
17648 length = 2;
17649 else
17650 length = 3;
17651 p = psuffix = buf + max (width, length);
17652
17653 /* Print EXPONENT. */
17654 if (exponent)
17655 *psuffix++ = power_letter[exponent];
17656 *psuffix = '\0';
17657
17658 /* Print TENTHS. */
17659 if (tenths >= 0)
17660 {
17661 *--p = '0' + tenths;
17662 *--p = '.';
17663 }
17664
17665 /* Print QUOTIENT. */
17666 do
17667 {
17668 int digit = quotient % 10;
17669 *--p = '0' + digit;
17670 }
17671 while ((quotient /= 10) != 0);
17672
17673 /* Print leading spaces. */
17674 while (buf < p)
17675 *--p = ' ';
17676 }
17677
17678 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17679 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17680 type of CODING_SYSTEM. Return updated pointer into BUF. */
17681
17682 static unsigned char invalid_eol_type[] = "(*invalid*)";
17683
17684 static char *
17685 decode_mode_spec_coding (coding_system, buf, eol_flag)
17686 Lisp_Object coding_system;
17687 register char *buf;
17688 int eol_flag;
17689 {
17690 Lisp_Object val;
17691 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17692 const unsigned char *eol_str;
17693 int eol_str_len;
17694 /* The EOL conversion we are using. */
17695 Lisp_Object eoltype;
17696
17697 val = CODING_SYSTEM_SPEC (coding_system);
17698 eoltype = Qnil;
17699
17700 if (!VECTORP (val)) /* Not yet decided. */
17701 {
17702 if (multibyte)
17703 *buf++ = '-';
17704 if (eol_flag)
17705 eoltype = eol_mnemonic_undecided;
17706 /* Don't mention EOL conversion if it isn't decided. */
17707 }
17708 else
17709 {
17710 Lisp_Object attrs;
17711 Lisp_Object eolvalue;
17712
17713 attrs = AREF (val, 0);
17714 eolvalue = AREF (val, 2);
17715
17716 if (multibyte)
17717 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17718
17719 if (eol_flag)
17720 {
17721 /* The EOL conversion that is normal on this system. */
17722
17723 if (NILP (eolvalue)) /* Not yet decided. */
17724 eoltype = eol_mnemonic_undecided;
17725 else if (VECTORP (eolvalue)) /* Not yet decided. */
17726 eoltype = eol_mnemonic_undecided;
17727 else /* eolvalue is Qunix, Qdos, or Qmac. */
17728 eoltype = (EQ (eolvalue, Qunix)
17729 ? eol_mnemonic_unix
17730 : (EQ (eolvalue, Qdos) == 1
17731 ? eol_mnemonic_dos : eol_mnemonic_mac));
17732 }
17733 }
17734
17735 if (eol_flag)
17736 {
17737 /* Mention the EOL conversion if it is not the usual one. */
17738 if (STRINGP (eoltype))
17739 {
17740 eol_str = SDATA (eoltype);
17741 eol_str_len = SBYTES (eoltype);
17742 }
17743 else if (CHARACTERP (eoltype))
17744 {
17745 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17746 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17747 eol_str = tmp;
17748 }
17749 else
17750 {
17751 eol_str = invalid_eol_type;
17752 eol_str_len = sizeof (invalid_eol_type) - 1;
17753 }
17754 bcopy (eol_str, buf, eol_str_len);
17755 buf += eol_str_len;
17756 }
17757
17758 return buf;
17759 }
17760
17761 /* Return a string for the output of a mode line %-spec for window W,
17762 generated by character C. PRECISION >= 0 means don't return a
17763 string longer than that value. FIELD_WIDTH > 0 means pad the
17764 string returned with spaces to that value. Return 1 in *MULTIBYTE
17765 if the result is multibyte text.
17766
17767 Note we operate on the current buffer for most purposes,
17768 the exception being w->base_line_pos. */
17769
17770 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17771
17772 static char *
17773 decode_mode_spec (w, c, field_width, precision, multibyte)
17774 struct window *w;
17775 register int c;
17776 int field_width, precision;
17777 int *multibyte;
17778 {
17779 Lisp_Object obj;
17780 struct frame *f = XFRAME (WINDOW_FRAME (w));
17781 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17782 struct buffer *b = current_buffer;
17783
17784 obj = Qnil;
17785 *multibyte = 0;
17786
17787 switch (c)
17788 {
17789 case '*':
17790 if (!NILP (b->read_only))
17791 return "%";
17792 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17793 return "*";
17794 return "-";
17795
17796 case '+':
17797 /* This differs from %* only for a modified read-only buffer. */
17798 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17799 return "*";
17800 if (!NILP (b->read_only))
17801 return "%";
17802 return "-";
17803
17804 case '&':
17805 /* This differs from %* in ignoring read-only-ness. */
17806 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17807 return "*";
17808 return "-";
17809
17810 case '%':
17811 return "%";
17812
17813 case '[':
17814 {
17815 int i;
17816 char *p;
17817
17818 if (command_loop_level > 5)
17819 return "[[[... ";
17820 p = decode_mode_spec_buf;
17821 for (i = 0; i < command_loop_level; i++)
17822 *p++ = '[';
17823 *p = 0;
17824 return decode_mode_spec_buf;
17825 }
17826
17827 case ']':
17828 {
17829 int i;
17830 char *p;
17831
17832 if (command_loop_level > 5)
17833 return " ...]]]";
17834 p = decode_mode_spec_buf;
17835 for (i = 0; i < command_loop_level; i++)
17836 *p++ = ']';
17837 *p = 0;
17838 return decode_mode_spec_buf;
17839 }
17840
17841 case '-':
17842 {
17843 register int i;
17844
17845 /* Let lots_of_dashes be a string of infinite length. */
17846 if (mode_line_target == MODE_LINE_NOPROP ||
17847 mode_line_target == MODE_LINE_STRING)
17848 return "--";
17849 if (field_width <= 0
17850 || field_width > sizeof (lots_of_dashes))
17851 {
17852 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17853 decode_mode_spec_buf[i] = '-';
17854 decode_mode_spec_buf[i] = '\0';
17855 return decode_mode_spec_buf;
17856 }
17857 else
17858 return lots_of_dashes;
17859 }
17860
17861 case 'b':
17862 obj = b->name;
17863 break;
17864
17865 case 'c':
17866 /* %c and %l are ignored in `frame-title-format'.
17867 (In redisplay_internal, the frame title is drawn _before_ the
17868 windows are updated, so the stuff which depends on actual
17869 window contents (such as %l) may fail to render properly, or
17870 even crash emacs.) */
17871 if (mode_line_target == MODE_LINE_TITLE)
17872 return "";
17873 else
17874 {
17875 int col = (int) current_column (); /* iftc */
17876 w->column_number_displayed = make_number (col);
17877 pint2str (decode_mode_spec_buf, field_width, col);
17878 return decode_mode_spec_buf;
17879 }
17880
17881 case 'e':
17882 #ifndef SYSTEM_MALLOC
17883 {
17884 if (NILP (Vmemory_full))
17885 return "";
17886 else
17887 return "!MEM FULL! ";
17888 }
17889 #else
17890 return "";
17891 #endif
17892
17893 case 'F':
17894 /* %F displays the frame name. */
17895 if (!NILP (f->title))
17896 return (char *) SDATA (f->title);
17897 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17898 return (char *) SDATA (f->name);
17899 return "Emacs";
17900
17901 case 'f':
17902 obj = b->filename;
17903 break;
17904
17905 case 'i':
17906 {
17907 int size = ZV - BEGV;
17908 pint2str (decode_mode_spec_buf, field_width, size);
17909 return decode_mode_spec_buf;
17910 }
17911
17912 case 'I':
17913 {
17914 int size = ZV - BEGV;
17915 pint2hrstr (decode_mode_spec_buf, field_width, size);
17916 return decode_mode_spec_buf;
17917 }
17918
17919 case 'l':
17920 {
17921 int startpos, startpos_byte, line, linepos, linepos_byte;
17922 int topline, nlines, junk, height;
17923
17924 /* %c and %l are ignored in `frame-title-format'. */
17925 if (mode_line_target == MODE_LINE_TITLE)
17926 return "";
17927
17928 startpos = XMARKER (w->start)->charpos;
17929 startpos_byte = marker_byte_position (w->start);
17930 height = WINDOW_TOTAL_LINES (w);
17931
17932 /* If we decided that this buffer isn't suitable for line numbers,
17933 don't forget that too fast. */
17934 if (EQ (w->base_line_pos, w->buffer))
17935 goto no_value;
17936 /* But do forget it, if the window shows a different buffer now. */
17937 else if (BUFFERP (w->base_line_pos))
17938 w->base_line_pos = Qnil;
17939
17940 /* If the buffer is very big, don't waste time. */
17941 if (INTEGERP (Vline_number_display_limit)
17942 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17943 {
17944 w->base_line_pos = Qnil;
17945 w->base_line_number = Qnil;
17946 goto no_value;
17947 }
17948
17949 if (!NILP (w->base_line_number)
17950 && !NILP (w->base_line_pos)
17951 && XFASTINT (w->base_line_pos) <= startpos)
17952 {
17953 line = XFASTINT (w->base_line_number);
17954 linepos = XFASTINT (w->base_line_pos);
17955 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17956 }
17957 else
17958 {
17959 line = 1;
17960 linepos = BUF_BEGV (b);
17961 linepos_byte = BUF_BEGV_BYTE (b);
17962 }
17963
17964 /* Count lines from base line to window start position. */
17965 nlines = display_count_lines (linepos, linepos_byte,
17966 startpos_byte,
17967 startpos, &junk);
17968
17969 topline = nlines + line;
17970
17971 /* Determine a new base line, if the old one is too close
17972 or too far away, or if we did not have one.
17973 "Too close" means it's plausible a scroll-down would
17974 go back past it. */
17975 if (startpos == BUF_BEGV (b))
17976 {
17977 w->base_line_number = make_number (topline);
17978 w->base_line_pos = make_number (BUF_BEGV (b));
17979 }
17980 else if (nlines < height + 25 || nlines > height * 3 + 50
17981 || linepos == BUF_BEGV (b))
17982 {
17983 int limit = BUF_BEGV (b);
17984 int limit_byte = BUF_BEGV_BYTE (b);
17985 int position;
17986 int distance = (height * 2 + 30) * line_number_display_limit_width;
17987
17988 if (startpos - distance > limit)
17989 {
17990 limit = startpos - distance;
17991 limit_byte = CHAR_TO_BYTE (limit);
17992 }
17993
17994 nlines = display_count_lines (startpos, startpos_byte,
17995 limit_byte,
17996 - (height * 2 + 30),
17997 &position);
17998 /* If we couldn't find the lines we wanted within
17999 line_number_display_limit_width chars per line,
18000 give up on line numbers for this window. */
18001 if (position == limit_byte && limit == startpos - distance)
18002 {
18003 w->base_line_pos = w->buffer;
18004 w->base_line_number = Qnil;
18005 goto no_value;
18006 }
18007
18008 w->base_line_number = make_number (topline - nlines);
18009 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18010 }
18011
18012 /* Now count lines from the start pos to point. */
18013 nlines = display_count_lines (startpos, startpos_byte,
18014 PT_BYTE, PT, &junk);
18015
18016 /* Record that we did display the line number. */
18017 line_number_displayed = 1;
18018
18019 /* Make the string to show. */
18020 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18021 return decode_mode_spec_buf;
18022 no_value:
18023 {
18024 char* p = decode_mode_spec_buf;
18025 int pad = field_width - 2;
18026 while (pad-- > 0)
18027 *p++ = ' ';
18028 *p++ = '?';
18029 *p++ = '?';
18030 *p = '\0';
18031 return decode_mode_spec_buf;
18032 }
18033 }
18034 break;
18035
18036 case 'm':
18037 obj = b->mode_name;
18038 break;
18039
18040 case 'n':
18041 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18042 return " Narrow";
18043 break;
18044
18045 case 'p':
18046 {
18047 int pos = marker_position (w->start);
18048 int total = BUF_ZV (b) - BUF_BEGV (b);
18049
18050 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18051 {
18052 if (pos <= BUF_BEGV (b))
18053 return "All";
18054 else
18055 return "Bottom";
18056 }
18057 else if (pos <= BUF_BEGV (b))
18058 return "Top";
18059 else
18060 {
18061 if (total > 1000000)
18062 /* Do it differently for a large value, to avoid overflow. */
18063 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18064 else
18065 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18066 /* We can't normally display a 3-digit number,
18067 so get us a 2-digit number that is close. */
18068 if (total == 100)
18069 total = 99;
18070 sprintf (decode_mode_spec_buf, "%2d%%", total);
18071 return decode_mode_spec_buf;
18072 }
18073 }
18074
18075 /* Display percentage of size above the bottom of the screen. */
18076 case 'P':
18077 {
18078 int toppos = marker_position (w->start);
18079 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18080 int total = BUF_ZV (b) - BUF_BEGV (b);
18081
18082 if (botpos >= BUF_ZV (b))
18083 {
18084 if (toppos <= BUF_BEGV (b))
18085 return "All";
18086 else
18087 return "Bottom";
18088 }
18089 else
18090 {
18091 if (total > 1000000)
18092 /* Do it differently for a large value, to avoid overflow. */
18093 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18094 else
18095 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18096 /* We can't normally display a 3-digit number,
18097 so get us a 2-digit number that is close. */
18098 if (total == 100)
18099 total = 99;
18100 if (toppos <= BUF_BEGV (b))
18101 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18102 else
18103 sprintf (decode_mode_spec_buf, "%2d%%", total);
18104 return decode_mode_spec_buf;
18105 }
18106 }
18107
18108 case 's':
18109 /* status of process */
18110 obj = Fget_buffer_process (Fcurrent_buffer ());
18111 if (NILP (obj))
18112 return "no process";
18113 #ifdef subprocesses
18114 obj = Fsymbol_name (Fprocess_status (obj));
18115 #endif
18116 break;
18117
18118 case 't': /* indicate TEXT or BINARY */
18119 #ifdef MODE_LINE_BINARY_TEXT
18120 return MODE_LINE_BINARY_TEXT (b);
18121 #else
18122 return "T";
18123 #endif
18124
18125 case 'z':
18126 /* coding-system (not including end-of-line format) */
18127 case 'Z':
18128 /* coding-system (including end-of-line type) */
18129 {
18130 int eol_flag = (c == 'Z');
18131 char *p = decode_mode_spec_buf;
18132
18133 if (! FRAME_WINDOW_P (f))
18134 {
18135 /* No need to mention EOL here--the terminal never needs
18136 to do EOL conversion. */
18137 p = decode_mode_spec_coding (CODING_ID_NAME (keyboard_coding.id),
18138 p, 0);
18139 p = decode_mode_spec_coding (CODING_ID_NAME (terminal_coding.id),
18140 p, 0);
18141 }
18142 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18143 p, eol_flag);
18144
18145 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18146 #ifdef subprocesses
18147 obj = Fget_buffer_process (Fcurrent_buffer ());
18148 if (PROCESSP (obj))
18149 {
18150 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18151 p, eol_flag);
18152 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18153 p, eol_flag);
18154 }
18155 #endif /* subprocesses */
18156 #endif /* 0 */
18157 *p = 0;
18158 return decode_mode_spec_buf;
18159 }
18160 }
18161
18162 if (STRINGP (obj))
18163 {
18164 *multibyte = STRING_MULTIBYTE (obj);
18165 return (char *) SDATA (obj);
18166 }
18167 else
18168 return "";
18169 }
18170
18171
18172 /* Count up to COUNT lines starting from START / START_BYTE.
18173 But don't go beyond LIMIT_BYTE.
18174 Return the number of lines thus found (always nonnegative).
18175
18176 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18177
18178 static int
18179 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18180 int start, start_byte, limit_byte, count;
18181 int *byte_pos_ptr;
18182 {
18183 register unsigned char *cursor;
18184 unsigned char *base;
18185
18186 register int ceiling;
18187 register unsigned char *ceiling_addr;
18188 int orig_count = count;
18189
18190 /* If we are not in selective display mode,
18191 check only for newlines. */
18192 int selective_display = (!NILP (current_buffer->selective_display)
18193 && !INTEGERP (current_buffer->selective_display));
18194
18195 if (count > 0)
18196 {
18197 while (start_byte < limit_byte)
18198 {
18199 ceiling = BUFFER_CEILING_OF (start_byte);
18200 ceiling = min (limit_byte - 1, ceiling);
18201 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18202 base = (cursor = BYTE_POS_ADDR (start_byte));
18203 while (1)
18204 {
18205 if (selective_display)
18206 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18207 ;
18208 else
18209 while (*cursor != '\n' && ++cursor != ceiling_addr)
18210 ;
18211
18212 if (cursor != ceiling_addr)
18213 {
18214 if (--count == 0)
18215 {
18216 start_byte += cursor - base + 1;
18217 *byte_pos_ptr = start_byte;
18218 return orig_count;
18219 }
18220 else
18221 if (++cursor == ceiling_addr)
18222 break;
18223 }
18224 else
18225 break;
18226 }
18227 start_byte += cursor - base;
18228 }
18229 }
18230 else
18231 {
18232 while (start_byte > limit_byte)
18233 {
18234 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18235 ceiling = max (limit_byte, ceiling);
18236 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18237 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18238 while (1)
18239 {
18240 if (selective_display)
18241 while (--cursor != ceiling_addr
18242 && *cursor != '\n' && *cursor != 015)
18243 ;
18244 else
18245 while (--cursor != ceiling_addr && *cursor != '\n')
18246 ;
18247
18248 if (cursor != ceiling_addr)
18249 {
18250 if (++count == 0)
18251 {
18252 start_byte += cursor - base + 1;
18253 *byte_pos_ptr = start_byte;
18254 /* When scanning backwards, we should
18255 not count the newline posterior to which we stop. */
18256 return - orig_count - 1;
18257 }
18258 }
18259 else
18260 break;
18261 }
18262 /* Here we add 1 to compensate for the last decrement
18263 of CURSOR, which took it past the valid range. */
18264 start_byte += cursor - base + 1;
18265 }
18266 }
18267
18268 *byte_pos_ptr = limit_byte;
18269
18270 if (count < 0)
18271 return - orig_count + count;
18272 return orig_count - count;
18273
18274 }
18275
18276
18277 \f
18278 /***********************************************************************
18279 Displaying strings
18280 ***********************************************************************/
18281
18282 /* Display a NUL-terminated string, starting with index START.
18283
18284 If STRING is non-null, display that C string. Otherwise, the Lisp
18285 string LISP_STRING is displayed.
18286
18287 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18288 FACE_STRING. Display STRING or LISP_STRING with the face at
18289 FACE_STRING_POS in FACE_STRING:
18290
18291 Display the string in the environment given by IT, but use the
18292 standard display table, temporarily.
18293
18294 FIELD_WIDTH is the minimum number of output glyphs to produce.
18295 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18296 with spaces. If STRING has more characters, more than FIELD_WIDTH
18297 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18298
18299 PRECISION is the maximum number of characters to output from
18300 STRING. PRECISION < 0 means don't truncate the string.
18301
18302 This is roughly equivalent to printf format specifiers:
18303
18304 FIELD_WIDTH PRECISION PRINTF
18305 ----------------------------------------
18306 -1 -1 %s
18307 -1 10 %.10s
18308 10 -1 %10s
18309 20 10 %20.10s
18310
18311 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18312 display them, and < 0 means obey the current buffer's value of
18313 enable_multibyte_characters.
18314
18315 Value is the number of columns displayed. */
18316
18317 static int
18318 display_string (string, lisp_string, face_string, face_string_pos,
18319 start, it, field_width, precision, max_x, multibyte)
18320 unsigned char *string;
18321 Lisp_Object lisp_string;
18322 Lisp_Object face_string;
18323 int face_string_pos;
18324 int start;
18325 struct it *it;
18326 int field_width, precision, max_x;
18327 int multibyte;
18328 {
18329 int hpos_at_start = it->hpos;
18330 int saved_face_id = it->face_id;
18331 struct glyph_row *row = it->glyph_row;
18332
18333 /* Initialize the iterator IT for iteration over STRING beginning
18334 with index START. */
18335 reseat_to_string (it, string, lisp_string, start,
18336 precision, field_width, multibyte);
18337
18338 /* If displaying STRING, set up the face of the iterator
18339 from LISP_STRING, if that's given. */
18340 if (STRINGP (face_string))
18341 {
18342 int endptr;
18343 struct face *face;
18344
18345 it->face_id
18346 = face_at_string_position (it->w, face_string, face_string_pos,
18347 0, it->region_beg_charpos,
18348 it->region_end_charpos,
18349 &endptr, it->base_face_id, 0);
18350 face = FACE_FROM_ID (it->f, it->face_id);
18351 it->face_box_p = face->box != FACE_NO_BOX;
18352 }
18353
18354 /* Set max_x to the maximum allowed X position. Don't let it go
18355 beyond the right edge of the window. */
18356 if (max_x <= 0)
18357 max_x = it->last_visible_x;
18358 else
18359 max_x = min (max_x, it->last_visible_x);
18360
18361 /* Skip over display elements that are not visible. because IT->w is
18362 hscrolled. */
18363 if (it->current_x < it->first_visible_x)
18364 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18365 MOVE_TO_POS | MOVE_TO_X);
18366
18367 row->ascent = it->max_ascent;
18368 row->height = it->max_ascent + it->max_descent;
18369 row->phys_ascent = it->max_phys_ascent;
18370 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18371 row->extra_line_spacing = it->max_extra_line_spacing;
18372
18373 /* This condition is for the case that we are called with current_x
18374 past last_visible_x. */
18375 while (it->current_x < max_x)
18376 {
18377 int x_before, x, n_glyphs_before, i, nglyphs;
18378
18379 /* Get the next display element. */
18380 if (!get_next_display_element (it))
18381 break;
18382
18383 /* Produce glyphs. */
18384 x_before = it->current_x;
18385 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18386 PRODUCE_GLYPHS (it);
18387
18388 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18389 i = 0;
18390 x = x_before;
18391 while (i < nglyphs)
18392 {
18393 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18394
18395 if (!it->truncate_lines_p
18396 && x + glyph->pixel_width > max_x)
18397 {
18398 /* End of continued line or max_x reached. */
18399 if (CHAR_GLYPH_PADDING_P (*glyph))
18400 {
18401 /* A wide character is unbreakable. */
18402 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18403 it->current_x = x_before;
18404 }
18405 else
18406 {
18407 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18408 it->current_x = x;
18409 }
18410 break;
18411 }
18412 else if (x + glyph->pixel_width >= it->first_visible_x)
18413 {
18414 /* Glyph is at least partially visible. */
18415 ++it->hpos;
18416 if (x < it->first_visible_x)
18417 it->glyph_row->x = x - it->first_visible_x;
18418 }
18419 else
18420 {
18421 /* Glyph is off the left margin of the display area.
18422 Should not happen. */
18423 abort ();
18424 }
18425
18426 row->ascent = max (row->ascent, it->max_ascent);
18427 row->height = max (row->height, it->max_ascent + it->max_descent);
18428 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18429 row->phys_height = max (row->phys_height,
18430 it->max_phys_ascent + it->max_phys_descent);
18431 row->extra_line_spacing = max (row->extra_line_spacing,
18432 it->max_extra_line_spacing);
18433 x += glyph->pixel_width;
18434 ++i;
18435 }
18436
18437 /* Stop if max_x reached. */
18438 if (i < nglyphs)
18439 break;
18440
18441 /* Stop at line ends. */
18442 if (ITERATOR_AT_END_OF_LINE_P (it))
18443 {
18444 it->continuation_lines_width = 0;
18445 break;
18446 }
18447
18448 set_iterator_to_next (it, 1);
18449
18450 /* Stop if truncating at the right edge. */
18451 if (it->truncate_lines_p
18452 && it->current_x >= it->last_visible_x)
18453 {
18454 /* Add truncation mark, but don't do it if the line is
18455 truncated at a padding space. */
18456 if (IT_CHARPOS (*it) < it->string_nchars)
18457 {
18458 if (!FRAME_WINDOW_P (it->f))
18459 {
18460 int i, n;
18461
18462 if (it->current_x > it->last_visible_x)
18463 {
18464 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18465 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18466 break;
18467 for (n = row->used[TEXT_AREA]; i < n; ++i)
18468 {
18469 row->used[TEXT_AREA] = i;
18470 produce_special_glyphs (it, IT_TRUNCATION);
18471 }
18472 }
18473 produce_special_glyphs (it, IT_TRUNCATION);
18474 }
18475 it->glyph_row->truncated_on_right_p = 1;
18476 }
18477 break;
18478 }
18479 }
18480
18481 /* Maybe insert a truncation at the left. */
18482 if (it->first_visible_x
18483 && IT_CHARPOS (*it) > 0)
18484 {
18485 if (!FRAME_WINDOW_P (it->f))
18486 insert_left_trunc_glyphs (it);
18487 it->glyph_row->truncated_on_left_p = 1;
18488 }
18489
18490 it->face_id = saved_face_id;
18491
18492 /* Value is number of columns displayed. */
18493 return it->hpos - hpos_at_start;
18494 }
18495
18496
18497 \f
18498 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18499 appears as an element of LIST or as the car of an element of LIST.
18500 If PROPVAL is a list, compare each element against LIST in that
18501 way, and return 1/2 if any element of PROPVAL is found in LIST.
18502 Otherwise return 0. This function cannot quit.
18503 The return value is 2 if the text is invisible but with an ellipsis
18504 and 1 if it's invisible and without an ellipsis. */
18505
18506 int
18507 invisible_p (propval, list)
18508 register Lisp_Object propval;
18509 Lisp_Object list;
18510 {
18511 register Lisp_Object tail, proptail;
18512
18513 for (tail = list; CONSP (tail); tail = XCDR (tail))
18514 {
18515 register Lisp_Object tem;
18516 tem = XCAR (tail);
18517 if (EQ (propval, tem))
18518 return 1;
18519 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18520 return NILP (XCDR (tem)) ? 1 : 2;
18521 }
18522
18523 if (CONSP (propval))
18524 {
18525 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18526 {
18527 Lisp_Object propelt;
18528 propelt = XCAR (proptail);
18529 for (tail = list; CONSP (tail); tail = XCDR (tail))
18530 {
18531 register Lisp_Object tem;
18532 tem = XCAR (tail);
18533 if (EQ (propelt, tem))
18534 return 1;
18535 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18536 return NILP (XCDR (tem)) ? 1 : 2;
18537 }
18538 }
18539 }
18540
18541 return 0;
18542 }
18543
18544 /* Calculate a width or height in pixels from a specification using
18545 the following elements:
18546
18547 SPEC ::=
18548 NUM - a (fractional) multiple of the default font width/height
18549 (NUM) - specifies exactly NUM pixels
18550 UNIT - a fixed number of pixels, see below.
18551 ELEMENT - size of a display element in pixels, see below.
18552 (NUM . SPEC) - equals NUM * SPEC
18553 (+ SPEC SPEC ...) - add pixel values
18554 (- SPEC SPEC ...) - subtract pixel values
18555 (- SPEC) - negate pixel value
18556
18557 NUM ::=
18558 INT or FLOAT - a number constant
18559 SYMBOL - use symbol's (buffer local) variable binding.
18560
18561 UNIT ::=
18562 in - pixels per inch *)
18563 mm - pixels per 1/1000 meter *)
18564 cm - pixels per 1/100 meter *)
18565 width - width of current font in pixels.
18566 height - height of current font in pixels.
18567
18568 *) using the ratio(s) defined in display-pixels-per-inch.
18569
18570 ELEMENT ::=
18571
18572 left-fringe - left fringe width in pixels
18573 right-fringe - right fringe width in pixels
18574
18575 left-margin - left margin width in pixels
18576 right-margin - right margin width in pixels
18577
18578 scroll-bar - scroll-bar area width in pixels
18579
18580 Examples:
18581
18582 Pixels corresponding to 5 inches:
18583 (5 . in)
18584
18585 Total width of non-text areas on left side of window (if scroll-bar is on left):
18586 '(space :width (+ left-fringe left-margin scroll-bar))
18587
18588 Align to first text column (in header line):
18589 '(space :align-to 0)
18590
18591 Align to middle of text area minus half the width of variable `my-image'
18592 containing a loaded image:
18593 '(space :align-to (0.5 . (- text my-image)))
18594
18595 Width of left margin minus width of 1 character in the default font:
18596 '(space :width (- left-margin 1))
18597
18598 Width of left margin minus width of 2 characters in the current font:
18599 '(space :width (- left-margin (2 . width)))
18600
18601 Center 1 character over left-margin (in header line):
18602 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18603
18604 Different ways to express width of left fringe plus left margin minus one pixel:
18605 '(space :width (- (+ left-fringe left-margin) (1)))
18606 '(space :width (+ left-fringe left-margin (- (1))))
18607 '(space :width (+ left-fringe left-margin (-1)))
18608
18609 */
18610
18611 #define NUMVAL(X) \
18612 ((INTEGERP (X) || FLOATP (X)) \
18613 ? XFLOATINT (X) \
18614 : - 1)
18615
18616 int
18617 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18618 double *res;
18619 struct it *it;
18620 Lisp_Object prop;
18621 void *font;
18622 int width_p, *align_to;
18623 {
18624 double pixels;
18625
18626 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18627 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18628
18629 if (NILP (prop))
18630 return OK_PIXELS (0);
18631
18632 if (SYMBOLP (prop))
18633 {
18634 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18635 {
18636 char *unit = SDATA (SYMBOL_NAME (prop));
18637
18638 if (unit[0] == 'i' && unit[1] == 'n')
18639 pixels = 1.0;
18640 else if (unit[0] == 'm' && unit[1] == 'm')
18641 pixels = 25.4;
18642 else if (unit[0] == 'c' && unit[1] == 'm')
18643 pixels = 2.54;
18644 else
18645 pixels = 0;
18646 if (pixels > 0)
18647 {
18648 double ppi;
18649 #ifdef HAVE_WINDOW_SYSTEM
18650 if (FRAME_WINDOW_P (it->f)
18651 && (ppi = (width_p
18652 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18653 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18654 ppi > 0))
18655 return OK_PIXELS (ppi / pixels);
18656 #endif
18657
18658 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18659 || (CONSP (Vdisplay_pixels_per_inch)
18660 && (ppi = (width_p
18661 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18662 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18663 ppi > 0)))
18664 return OK_PIXELS (ppi / pixels);
18665
18666 return 0;
18667 }
18668 }
18669
18670 #ifdef HAVE_WINDOW_SYSTEM
18671 if (EQ (prop, Qheight))
18672 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18673 if (EQ (prop, Qwidth))
18674 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18675 #else
18676 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18677 return OK_PIXELS (1);
18678 #endif
18679
18680 if (EQ (prop, Qtext))
18681 return OK_PIXELS (width_p
18682 ? window_box_width (it->w, TEXT_AREA)
18683 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18684
18685 if (align_to && *align_to < 0)
18686 {
18687 *res = 0;
18688 if (EQ (prop, Qleft))
18689 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18690 if (EQ (prop, Qright))
18691 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18692 if (EQ (prop, Qcenter))
18693 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18694 + window_box_width (it->w, TEXT_AREA) / 2);
18695 if (EQ (prop, Qleft_fringe))
18696 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18697 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18698 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18699 if (EQ (prop, Qright_fringe))
18700 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18701 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18702 : window_box_right_offset (it->w, TEXT_AREA));
18703 if (EQ (prop, Qleft_margin))
18704 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18705 if (EQ (prop, Qright_margin))
18706 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18707 if (EQ (prop, Qscroll_bar))
18708 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18709 ? 0
18710 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18711 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18712 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18713 : 0)));
18714 }
18715 else
18716 {
18717 if (EQ (prop, Qleft_fringe))
18718 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18719 if (EQ (prop, Qright_fringe))
18720 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18721 if (EQ (prop, Qleft_margin))
18722 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18723 if (EQ (prop, Qright_margin))
18724 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18725 if (EQ (prop, Qscroll_bar))
18726 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18727 }
18728
18729 prop = Fbuffer_local_value (prop, it->w->buffer);
18730 }
18731
18732 if (INTEGERP (prop) || FLOATP (prop))
18733 {
18734 int base_unit = (width_p
18735 ? FRAME_COLUMN_WIDTH (it->f)
18736 : FRAME_LINE_HEIGHT (it->f));
18737 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18738 }
18739
18740 if (CONSP (prop))
18741 {
18742 Lisp_Object car = XCAR (prop);
18743 Lisp_Object cdr = XCDR (prop);
18744
18745 if (SYMBOLP (car))
18746 {
18747 #ifdef HAVE_WINDOW_SYSTEM
18748 if (valid_image_p (prop))
18749 {
18750 int id = lookup_image (it->f, prop);
18751 struct image *img = IMAGE_FROM_ID (it->f, id);
18752
18753 return OK_PIXELS (width_p ? img->width : img->height);
18754 }
18755 #endif
18756 if (EQ (car, Qplus) || EQ (car, Qminus))
18757 {
18758 int first = 1;
18759 double px;
18760
18761 pixels = 0;
18762 while (CONSP (cdr))
18763 {
18764 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18765 font, width_p, align_to))
18766 return 0;
18767 if (first)
18768 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18769 else
18770 pixels += px;
18771 cdr = XCDR (cdr);
18772 }
18773 if (EQ (car, Qminus))
18774 pixels = -pixels;
18775 return OK_PIXELS (pixels);
18776 }
18777
18778 car = Fbuffer_local_value (car, it->w->buffer);
18779 }
18780
18781 if (INTEGERP (car) || FLOATP (car))
18782 {
18783 double fact;
18784 pixels = XFLOATINT (car);
18785 if (NILP (cdr))
18786 return OK_PIXELS (pixels);
18787 if (calc_pixel_width_or_height (&fact, it, cdr,
18788 font, width_p, align_to))
18789 return OK_PIXELS (pixels * fact);
18790 return 0;
18791 }
18792
18793 return 0;
18794 }
18795
18796 return 0;
18797 }
18798
18799 \f
18800 /***********************************************************************
18801 Glyph Display
18802 ***********************************************************************/
18803
18804 #ifdef HAVE_WINDOW_SYSTEM
18805
18806 #if GLYPH_DEBUG
18807
18808 void
18809 dump_glyph_string (s)
18810 struct glyph_string *s;
18811 {
18812 fprintf (stderr, "glyph string\n");
18813 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18814 s->x, s->y, s->width, s->height);
18815 fprintf (stderr, " ybase = %d\n", s->ybase);
18816 fprintf (stderr, " hl = %d\n", s->hl);
18817 fprintf (stderr, " left overhang = %d, right = %d\n",
18818 s->left_overhang, s->right_overhang);
18819 fprintf (stderr, " nchars = %d\n", s->nchars);
18820 fprintf (stderr, " extends to end of line = %d\n",
18821 s->extends_to_end_of_line_p);
18822 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18823 fprintf (stderr, " bg width = %d\n", s->background_width);
18824 }
18825
18826 #endif /* GLYPH_DEBUG */
18827
18828 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18829 of XChar2b structures for S; it can't be allocated in
18830 init_glyph_string because it must be allocated via `alloca'. W
18831 is the window on which S is drawn. ROW and AREA are the glyph row
18832 and area within the row from which S is constructed. START is the
18833 index of the first glyph structure covered by S. HL is a
18834 face-override for drawing S. */
18835
18836 #ifdef HAVE_NTGUI
18837 #define OPTIONAL_HDC(hdc) hdc,
18838 #define DECLARE_HDC(hdc) HDC hdc;
18839 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18840 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18841 #endif
18842
18843 #ifndef OPTIONAL_HDC
18844 #define OPTIONAL_HDC(hdc)
18845 #define DECLARE_HDC(hdc)
18846 #define ALLOCATE_HDC(hdc, f)
18847 #define RELEASE_HDC(hdc, f)
18848 #endif
18849
18850 static void
18851 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18852 struct glyph_string *s;
18853 DECLARE_HDC (hdc)
18854 XChar2b *char2b;
18855 struct window *w;
18856 struct glyph_row *row;
18857 enum glyph_row_area area;
18858 int start;
18859 enum draw_glyphs_face hl;
18860 {
18861 bzero (s, sizeof *s);
18862 s->w = w;
18863 s->f = XFRAME (w->frame);
18864 #ifdef HAVE_NTGUI
18865 s->hdc = hdc;
18866 #endif
18867 s->display = FRAME_X_DISPLAY (s->f);
18868 s->window = FRAME_X_WINDOW (s->f);
18869 s->char2b = char2b;
18870 s->hl = hl;
18871 s->row = row;
18872 s->area = area;
18873 s->first_glyph = row->glyphs[area] + start;
18874 s->height = row->height;
18875 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18876
18877 /* Display the internal border below the tool-bar window. */
18878 if (WINDOWP (s->f->tool_bar_window)
18879 && s->w == XWINDOW (s->f->tool_bar_window))
18880 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18881
18882 s->ybase = s->y + row->ascent;
18883 }
18884
18885
18886 /* Append the list of glyph strings with head H and tail T to the list
18887 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18888
18889 static INLINE void
18890 append_glyph_string_lists (head, tail, h, t)
18891 struct glyph_string **head, **tail;
18892 struct glyph_string *h, *t;
18893 {
18894 if (h)
18895 {
18896 if (*head)
18897 (*tail)->next = h;
18898 else
18899 *head = h;
18900 h->prev = *tail;
18901 *tail = t;
18902 }
18903 }
18904
18905
18906 /* Prepend the list of glyph strings with head H and tail T to the
18907 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18908 result. */
18909
18910 static INLINE void
18911 prepend_glyph_string_lists (head, tail, h, t)
18912 struct glyph_string **head, **tail;
18913 struct glyph_string *h, *t;
18914 {
18915 if (h)
18916 {
18917 if (*head)
18918 (*head)->prev = t;
18919 else
18920 *tail = t;
18921 t->next = *head;
18922 *head = h;
18923 }
18924 }
18925
18926
18927 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18928 Set *HEAD and *TAIL to the resulting list. */
18929
18930 static INLINE void
18931 append_glyph_string (head, tail, s)
18932 struct glyph_string **head, **tail;
18933 struct glyph_string *s;
18934 {
18935 s->next = s->prev = NULL;
18936 append_glyph_string_lists (head, tail, s, s);
18937 }
18938
18939
18940 /* Get face and two-byte form of character C in face FACE_ID on frame
18941 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18942 means we want to display multibyte text. DISPLAY_P non-zero means
18943 make sure that X resources for the face returned are allocated.
18944 Value is a pointer to a realized face that is ready for display if
18945 DISPLAY_P is non-zero. */
18946
18947 static INLINE struct face *
18948 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18949 struct frame *f;
18950 int c, face_id;
18951 XChar2b *char2b;
18952 int multibyte_p, display_p;
18953 {
18954 struct face *face = FACE_FROM_ID (f, face_id);
18955
18956 #ifdef USE_FONT_BACKEND
18957 if (enable_font_backend)
18958 {
18959 struct font *font = (struct font *) face->font_info;
18960
18961 if (font)
18962 {
18963 unsigned code = font->driver->encode_char (font, c);
18964
18965 if (code != FONT_INVALID_CODE)
18966 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
18967 else
18968 STORE_XCHAR2B (char2b, 0, 0);
18969 }
18970 }
18971 else
18972 #endif /* USE_FONT_BACKEND */
18973 if (!multibyte_p)
18974 {
18975 /* Unibyte case. We don't have to encode, but we have to make
18976 sure to use a face suitable for unibyte. */
18977 STORE_XCHAR2B (char2b, 0, c);
18978 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
18979 face = FACE_FROM_ID (f, face_id);
18980 }
18981 else if (c < 128)
18982 {
18983 /* Case of ASCII in a face known to fit ASCII. */
18984 STORE_XCHAR2B (char2b, 0, c);
18985 }
18986 else if (face->font != NULL)
18987 {
18988 struct font_info *font_info
18989 = FONT_INFO_FROM_ID (f, face->font_info_id);
18990 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
18991 unsigned code = ENCODE_CHAR (charset, c);
18992
18993 if (CHARSET_DIMENSION (charset) == 1)
18994 STORE_XCHAR2B (char2b, 0, code);
18995 else
18996 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
18997 /* Maybe encode the character in *CHAR2B. */
18998 rif->encode_char (c, char2b, font_info, charset, NULL);
18999 }
19000
19001 /* Make sure X resources of the face are allocated. */
19002 #ifdef HAVE_X_WINDOWS
19003 if (display_p)
19004 #endif
19005 {
19006 xassert (face != NULL);
19007 PREPARE_FACE_FOR_DISPLAY (f, face);
19008 }
19009
19010 return face;
19011 }
19012
19013
19014 /* Get face and two-byte form of character glyph GLYPH on frame F.
19015 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19016 a pointer to a realized face that is ready for display. */
19017
19018 static INLINE struct face *
19019 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19020 struct frame *f;
19021 struct glyph *glyph;
19022 XChar2b *char2b;
19023 int *two_byte_p;
19024 {
19025 struct face *face;
19026
19027 xassert (glyph->type == CHAR_GLYPH);
19028 face = FACE_FROM_ID (f, glyph->face_id);
19029
19030 if (two_byte_p)
19031 *two_byte_p = 0;
19032
19033 #ifdef USE_FONT_BACKEND
19034 if (enable_font_backend)
19035 {
19036 struct font *font = (struct font *) face->font_info;
19037
19038 if (font)
19039 {
19040 unsigned code = font->driver->encode_char (font, glyph->u.ch);
19041
19042 if (code != FONT_INVALID_CODE)
19043 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19044 else
19045 STORE_XCHAR2B (char2b, 0, code);
19046 }
19047 }
19048 else
19049 #endif /* USE_FONT_BACKEND */
19050 if (!glyph->multibyte_p)
19051 {
19052 /* Unibyte case. We don't have to encode, but we have to make
19053 sure to use a face suitable for unibyte. */
19054 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19055 }
19056 else if (glyph->u.ch < 128)
19057 {
19058 /* Case of ASCII in a face known to fit ASCII. */
19059 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19060 }
19061 else
19062 {
19063 struct font_info *font_info
19064 = FONT_INFO_FROM_ID (f, face->font_info_id);
19065 if (font_info)
19066 {
19067 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19068 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
19069
19070 if (CHARSET_DIMENSION (charset) == 1)
19071 STORE_XCHAR2B (char2b, 0, code);
19072 else
19073 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19074
19075 /* Maybe encode the character in *CHAR2B. */
19076 if (CHARSET_ID (charset) != charset_ascii)
19077 {
19078 glyph->font_type
19079 = rif->encode_char (glyph->u.ch, char2b, font_info, charset,
19080 two_byte_p);
19081 }
19082 }
19083 }
19084
19085 /* Make sure X resources of the face are allocated. */
19086 xassert (face != NULL);
19087 PREPARE_FACE_FOR_DISPLAY (f, face);
19088 return face;
19089 }
19090
19091
19092 /* Fill glyph string S with composition components specified by S->cmp.
19093
19094 BASE_FACE is the base face of the composition.
19095 S->gidx is the index of the first component for S.
19096
19097 OVERLAPS non-zero means S should draw the foreground only, and use
19098 its physical height for clipping. See also draw_glyphs.
19099
19100 Value is the index of a component not in S. */
19101
19102 static int
19103 fill_composite_glyph_string (s, base_face, overlaps)
19104 struct glyph_string *s;
19105 struct face *base_face;
19106 int overlaps;
19107 {
19108 int i;
19109
19110 xassert (s);
19111
19112 s->for_overlaps = overlaps;
19113
19114 #ifdef USE_FONT_BACKEND
19115 if (enable_font_backend && s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19116 {
19117 Lisp_Object gstring
19118 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19119 s->cmp->hash_index * 2);
19120
19121 s->face = base_face;
19122 s->font_info = s->cmp->font;
19123 s->font = s->font_info->font;
19124 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19125 {
19126 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19127 unsigned code;
19128
19129 if (NILP (LGLYPH_FROM (g)))
19130 break;
19131 code = XUINT (LGLYPH_CODE (g));
19132 STORE_XCHAR2B (s->char2b + i, code >> 8, code & 0xFF);
19133 }
19134 s->width = s->cmp->pixel_width;
19135 }
19136 else
19137 #endif /* USE_FONT_BACKEND */
19138 {
19139 /* For all glyphs of this composition, starting at the offset
19140 S->gidx, until we reach the end of the definition or encounter a
19141 glyph that requires the different face, add it to S. */
19142 struct face *face;
19143
19144 s->face = NULL;
19145 s->font = NULL;
19146 s->font_info = NULL;
19147 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19148 {
19149 int c = COMPOSITION_GLYPH (s->cmp, i);
19150
19151 if (c != '\t')
19152 {
19153 int face_id = FACE_FOR_CHAR (s->f, base_face, c, -1, Qnil);
19154
19155 face = get_char_face_and_encoding (s->f, c, face_id,
19156 s->char2b + i, 1, 1);
19157 if (face)
19158 {
19159 if (! s->face)
19160 {
19161 s->face = face;
19162 s->font = s->face->font;
19163 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19164 }
19165 else if (s->face != face)
19166 break;
19167 }
19168 }
19169 ++s->nchars;
19170 }
19171
19172 /* All glyph strings for the same composition has the same width,
19173 i.e. the width set for the first component of the composition. */
19174 s->width = s->first_glyph->pixel_width;
19175 }
19176
19177 /* If the specified font could not be loaded, use the frame's
19178 default font, but record the fact that we couldn't load it in
19179 the glyph string so that we can draw rectangles for the
19180 characters of the glyph string. */
19181 if (s->font == NULL)
19182 {
19183 s->font_not_found_p = 1;
19184 s->font = FRAME_FONT (s->f);
19185 }
19186
19187 /* Adjust base line for subscript/superscript text. */
19188 s->ybase += s->first_glyph->voffset;
19189
19190 /* This glyph string must always be drawn with 16-bit functions. */
19191 s->two_byte_p = 1;
19192
19193 return s->gidx + s->nchars;
19194 }
19195
19196
19197 /* Fill glyph string S from a sequence of character glyphs.
19198
19199 FACE_ID is the face id of the string. START is the index of the
19200 first glyph to consider, END is the index of the last + 1.
19201 OVERLAPS non-zero means S should draw the foreground only, and use
19202 its physical height for clipping. See also draw_glyphs.
19203
19204 Value is the index of the first glyph not in S. */
19205
19206 static int
19207 fill_glyph_string (s, face_id, start, end, overlaps)
19208 struct glyph_string *s;
19209 int face_id;
19210 int start, end, overlaps;
19211 {
19212 struct glyph *glyph, *last;
19213 int voffset;
19214 int glyph_not_available_p;
19215
19216 xassert (s->f == XFRAME (s->w->frame));
19217 xassert (s->nchars == 0);
19218 xassert (start >= 0 && end > start);
19219
19220 s->for_overlaps = overlaps,
19221 glyph = s->row->glyphs[s->area] + start;
19222 last = s->row->glyphs[s->area] + end;
19223 voffset = glyph->voffset;
19224
19225 glyph_not_available_p = glyph->glyph_not_available_p;
19226
19227 while (glyph < last
19228 && glyph->type == CHAR_GLYPH
19229 && glyph->voffset == voffset
19230 /* Same face id implies same font, nowadays. */
19231 && glyph->face_id == face_id
19232 && glyph->glyph_not_available_p == glyph_not_available_p)
19233 {
19234 int two_byte_p;
19235
19236 s->face = get_glyph_face_and_encoding (s->f, glyph,
19237 s->char2b + s->nchars,
19238 &two_byte_p);
19239 s->two_byte_p = two_byte_p;
19240 ++s->nchars;
19241 xassert (s->nchars <= end - start);
19242 s->width += glyph->pixel_width;
19243 ++glyph;
19244 }
19245
19246 s->font = s->face->font;
19247 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19248
19249 /* If the specified font could not be loaded, use the frame's font,
19250 but record the fact that we couldn't load it in
19251 S->font_not_found_p so that we can draw rectangles for the
19252 characters of the glyph string. */
19253 if (s->font == NULL || glyph_not_available_p)
19254 {
19255 s->font_not_found_p = 1;
19256 s->font = FRAME_FONT (s->f);
19257 }
19258
19259 /* Adjust base line for subscript/superscript text. */
19260 s->ybase += voffset;
19261
19262 xassert (s->face && s->face->gc);
19263 return glyph - s->row->glyphs[s->area];
19264 }
19265
19266
19267 /* Fill glyph string S from image glyph S->first_glyph. */
19268
19269 static void
19270 fill_image_glyph_string (s)
19271 struct glyph_string *s;
19272 {
19273 xassert (s->first_glyph->type == IMAGE_GLYPH);
19274 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19275 xassert (s->img);
19276 s->slice = s->first_glyph->slice;
19277 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19278 s->font = s->face->font;
19279 s->width = s->first_glyph->pixel_width;
19280
19281 /* Adjust base line for subscript/superscript text. */
19282 s->ybase += s->first_glyph->voffset;
19283 }
19284
19285
19286 /* Fill glyph string S from a sequence of stretch glyphs.
19287
19288 ROW is the glyph row in which the glyphs are found, AREA is the
19289 area within the row. START is the index of the first glyph to
19290 consider, END is the index of the last + 1.
19291
19292 Value is the index of the first glyph not in S. */
19293
19294 static int
19295 fill_stretch_glyph_string (s, row, area, start, end)
19296 struct glyph_string *s;
19297 struct glyph_row *row;
19298 enum glyph_row_area area;
19299 int start, end;
19300 {
19301 struct glyph *glyph, *last;
19302 int voffset, face_id;
19303
19304 xassert (s->first_glyph->type == STRETCH_GLYPH);
19305
19306 glyph = s->row->glyphs[s->area] + start;
19307 last = s->row->glyphs[s->area] + end;
19308 face_id = glyph->face_id;
19309 s->face = FACE_FROM_ID (s->f, face_id);
19310 s->font = s->face->font;
19311 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19312 s->width = glyph->pixel_width;
19313 s->nchars = 1;
19314 voffset = glyph->voffset;
19315
19316 for (++glyph;
19317 (glyph < last
19318 && glyph->type == STRETCH_GLYPH
19319 && glyph->voffset == voffset
19320 && glyph->face_id == face_id);
19321 ++glyph)
19322 s->width += glyph->pixel_width;
19323
19324 /* Adjust base line for subscript/superscript text. */
19325 s->ybase += voffset;
19326
19327 /* The case that face->gc == 0 is handled when drawing the glyph
19328 string by calling PREPARE_FACE_FOR_DISPLAY. */
19329 xassert (s->face);
19330 return glyph - s->row->glyphs[s->area];
19331 }
19332
19333 static XCharStruct *
19334 get_per_char_metric (font, font_info, char2b, font_type)
19335 XFontStruct *font;
19336 struct font_info *font_info;
19337 XChar2b *char2b;
19338 int font_type;
19339 {
19340 #ifdef USE_FONT_BACKEND
19341 if (enable_font_backend)
19342 {
19343 static XCharStruct pcm_value;
19344 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19345 struct font *fontp;
19346 struct font_metrics metrics;
19347
19348 if (! font_info || code == FONT_INVALID_CODE)
19349 return NULL;
19350 fontp = (struct font *) font_info;
19351 fontp->driver->text_extents (fontp, &code, 1, &metrics);
19352 pcm_value.lbearing = metrics.lbearing;
19353 pcm_value.rbearing = metrics.rbearing;
19354 pcm_value.ascent = metrics.ascent;
19355 pcm_value.descent = metrics.descent;
19356 pcm_value.width = metrics.width;
19357 return &pcm_value;
19358 }
19359 #endif /* USE_FONT_BACKEND */
19360 return rif->per_char_metric (font, char2b, font_type);
19361 }
19362
19363 /* EXPORT for RIF:
19364 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19365 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19366 assumed to be zero. */
19367
19368 void
19369 x_get_glyph_overhangs (glyph, f, left, right)
19370 struct glyph *glyph;
19371 struct frame *f;
19372 int *left, *right;
19373 {
19374 *left = *right = 0;
19375
19376 if (glyph->type == CHAR_GLYPH)
19377 {
19378 XFontStruct *font;
19379 struct face *face;
19380 struct font_info *font_info;
19381 XChar2b char2b;
19382 XCharStruct *pcm;
19383
19384 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19385 font = face->font;
19386 font_info = FONT_INFO_FROM_FACE (f, face);
19387 if (font /* ++KFS: Should this be font_info ? */
19388 && (pcm = get_per_char_metric (font, font_info, &char2b, glyph->font_type)))
19389 {
19390 if (pcm->rbearing > pcm->width)
19391 *right = pcm->rbearing - pcm->width;
19392 if (pcm->lbearing < 0)
19393 *left = -pcm->lbearing;
19394 }
19395 }
19396 else if (glyph->type == COMPOSITE_GLYPH)
19397 {
19398 struct composition *cmp = composition_table[glyph->u.cmp_id];
19399
19400 *right = cmp->rbearing - cmp->pixel_width;
19401 *left = - cmp->lbearing;
19402 }
19403 }
19404
19405
19406 /* Return the index of the first glyph preceding glyph string S that
19407 is overwritten by S because of S's left overhang. Value is -1
19408 if no glyphs are overwritten. */
19409
19410 static int
19411 left_overwritten (s)
19412 struct glyph_string *s;
19413 {
19414 int k;
19415
19416 if (s->left_overhang)
19417 {
19418 int x = 0, i;
19419 struct glyph *glyphs = s->row->glyphs[s->area];
19420 int first = s->first_glyph - glyphs;
19421
19422 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19423 x -= glyphs[i].pixel_width;
19424
19425 k = i + 1;
19426 }
19427 else
19428 k = -1;
19429
19430 return k;
19431 }
19432
19433
19434 /* Return the index of the first glyph preceding glyph string S that
19435 is overwriting S because of its right overhang. Value is -1 if no
19436 glyph in front of S overwrites S. */
19437
19438 static int
19439 left_overwriting (s)
19440 struct glyph_string *s;
19441 {
19442 int i, k, x;
19443 struct glyph *glyphs = s->row->glyphs[s->area];
19444 int first = s->first_glyph - glyphs;
19445
19446 k = -1;
19447 x = 0;
19448 for (i = first - 1; i >= 0; --i)
19449 {
19450 int left, right;
19451 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19452 if (x + right > 0)
19453 k = i;
19454 x -= glyphs[i].pixel_width;
19455 }
19456
19457 return k;
19458 }
19459
19460
19461 /* Return the index of the last glyph following glyph string S that is
19462 not overwritten by S because of S's right overhang. Value is -1 if
19463 no such glyph is found. */
19464
19465 static int
19466 right_overwritten (s)
19467 struct glyph_string *s;
19468 {
19469 int k = -1;
19470
19471 if (s->right_overhang)
19472 {
19473 int x = 0, i;
19474 struct glyph *glyphs = s->row->glyphs[s->area];
19475 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19476 int end = s->row->used[s->area];
19477
19478 for (i = first; i < end && s->right_overhang > x; ++i)
19479 x += glyphs[i].pixel_width;
19480
19481 k = i;
19482 }
19483
19484 return k;
19485 }
19486
19487
19488 /* Return the index of the last glyph following glyph string S that
19489 overwrites S because of its left overhang. Value is negative
19490 if no such glyph is found. */
19491
19492 static int
19493 right_overwriting (s)
19494 struct glyph_string *s;
19495 {
19496 int i, k, x;
19497 int end = s->row->used[s->area];
19498 struct glyph *glyphs = s->row->glyphs[s->area];
19499 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19500
19501 k = -1;
19502 x = 0;
19503 for (i = first; i < end; ++i)
19504 {
19505 int left, right;
19506 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19507 if (x - left < 0)
19508 k = i;
19509 x += glyphs[i].pixel_width;
19510 }
19511
19512 return k;
19513 }
19514
19515
19516 /* Set background width of glyph string S. START is the index of the
19517 first glyph following S. LAST_X is the right-most x-position + 1
19518 in the drawing area. */
19519
19520 static INLINE void
19521 set_glyph_string_background_width (s, start, last_x)
19522 struct glyph_string *s;
19523 int start;
19524 int last_x;
19525 {
19526 /* If the face of this glyph string has to be drawn to the end of
19527 the drawing area, set S->extends_to_end_of_line_p. */
19528
19529 if (start == s->row->used[s->area]
19530 && s->area == TEXT_AREA
19531 && ((s->row->fill_line_p
19532 && (s->hl == DRAW_NORMAL_TEXT
19533 || s->hl == DRAW_IMAGE_RAISED
19534 || s->hl == DRAW_IMAGE_SUNKEN))
19535 || s->hl == DRAW_MOUSE_FACE))
19536 s->extends_to_end_of_line_p = 1;
19537
19538 /* If S extends its face to the end of the line, set its
19539 background_width to the distance to the right edge of the drawing
19540 area. */
19541 if (s->extends_to_end_of_line_p)
19542 s->background_width = last_x - s->x + 1;
19543 else
19544 s->background_width = s->width;
19545 }
19546
19547
19548 /* Compute overhangs and x-positions for glyph string S and its
19549 predecessors, or successors. X is the starting x-position for S.
19550 BACKWARD_P non-zero means process predecessors. */
19551
19552 static void
19553 compute_overhangs_and_x (s, x, backward_p)
19554 struct glyph_string *s;
19555 int x;
19556 int backward_p;
19557 {
19558 if (backward_p)
19559 {
19560 while (s)
19561 {
19562 if (rif->compute_glyph_string_overhangs)
19563 rif->compute_glyph_string_overhangs (s);
19564 x -= s->width;
19565 s->x = x;
19566 s = s->prev;
19567 }
19568 }
19569 else
19570 {
19571 while (s)
19572 {
19573 if (rif->compute_glyph_string_overhangs)
19574 rif->compute_glyph_string_overhangs (s);
19575 s->x = x;
19576 x += s->width;
19577 s = s->next;
19578 }
19579 }
19580 }
19581
19582
19583
19584 /* The following macros are only called from draw_glyphs below.
19585 They reference the following parameters of that function directly:
19586 `w', `row', `area', and `overlap_p'
19587 as well as the following local variables:
19588 `s', `f', and `hdc' (in W32) */
19589
19590 #ifdef HAVE_NTGUI
19591 /* On W32, silently add local `hdc' variable to argument list of
19592 init_glyph_string. */
19593 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19594 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19595 #else
19596 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19597 init_glyph_string (s, char2b, w, row, area, start, hl)
19598 #endif
19599
19600 /* Add a glyph string for a stretch glyph to the list of strings
19601 between HEAD and TAIL. START is the index of the stretch glyph in
19602 row area AREA of glyph row ROW. END is the index of the last glyph
19603 in that glyph row area. X is the current output position assigned
19604 to the new glyph string constructed. HL overrides that face of the
19605 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19606 is the right-most x-position of the drawing area. */
19607
19608 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19609 and below -- keep them on one line. */
19610 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19611 do \
19612 { \
19613 s = (struct glyph_string *) alloca (sizeof *s); \
19614 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19615 START = fill_stretch_glyph_string (s, row, area, START, END); \
19616 append_glyph_string (&HEAD, &TAIL, s); \
19617 s->x = (X); \
19618 } \
19619 while (0)
19620
19621
19622 /* Add a glyph string for an image glyph to the list of strings
19623 between HEAD and TAIL. START is the index of the image glyph in
19624 row area AREA of glyph row ROW. END is the index of the last glyph
19625 in that glyph row area. X is the current output position assigned
19626 to the new glyph string constructed. HL overrides that face of the
19627 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19628 is the right-most x-position of the drawing area. */
19629
19630 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19631 do \
19632 { \
19633 s = (struct glyph_string *) alloca (sizeof *s); \
19634 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19635 fill_image_glyph_string (s); \
19636 append_glyph_string (&HEAD, &TAIL, s); \
19637 ++START; \
19638 s->x = (X); \
19639 } \
19640 while (0)
19641
19642
19643 /* Add a glyph string for a sequence of character glyphs to the list
19644 of strings between HEAD and TAIL. START is the index of the first
19645 glyph in row area AREA of glyph row ROW that is part of the new
19646 glyph string. END is the index of the last glyph in that glyph row
19647 area. X is the current output position assigned to the new glyph
19648 string constructed. HL overrides that face of the glyph; e.g. it
19649 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19650 right-most x-position of the drawing area. */
19651
19652 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19653 do \
19654 { \
19655 int face_id; \
19656 XChar2b *char2b; \
19657 \
19658 face_id = (row)->glyphs[area][START].face_id; \
19659 \
19660 s = (struct glyph_string *) alloca (sizeof *s); \
19661 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19662 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19663 append_glyph_string (&HEAD, &TAIL, s); \
19664 s->x = (X); \
19665 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19666 } \
19667 while (0)
19668
19669
19670 /* Add a glyph string for a composite sequence to the list of strings
19671 between HEAD and TAIL. START is the index of the first glyph in
19672 row area AREA of glyph row ROW that is part of the new glyph
19673 string. END is the index of the last glyph in that glyph row area.
19674 X is the current output position assigned to the new glyph string
19675 constructed. HL overrides that face of the glyph; e.g. it is
19676 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19677 x-position of the drawing area. */
19678
19679 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19680 do { \
19681 int face_id = (row)->glyphs[area][START].face_id; \
19682 struct face *base_face = FACE_FROM_ID (f, face_id); \
19683 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19684 struct composition *cmp = composition_table[cmp_id]; \
19685 XChar2b *char2b; \
19686 struct glyph_string *first_s; \
19687 int n; \
19688 \
19689 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19690 base_face = base_face->ascii_face; \
19691 \
19692 /* Make glyph_strings for each glyph sequence that is drawable by \
19693 the same face, and append them to HEAD/TAIL. */ \
19694 for (n = 0; n < cmp->glyph_len;) \
19695 { \
19696 s = (struct glyph_string *) alloca (sizeof *s); \
19697 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19698 append_glyph_string (&(HEAD), &(TAIL), s); \
19699 s->cmp = cmp; \
19700 s->gidx = n; \
19701 s->x = (X); \
19702 if (n == 0) \
19703 first_s = s; \
19704 n = fill_composite_glyph_string (s, base_face, overlaps); \
19705 } \
19706 \
19707 ++START; \
19708 s = first_s; \
19709 } while (0)
19710
19711
19712 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19713 of AREA of glyph row ROW on window W between indices START and END.
19714 HL overrides the face for drawing glyph strings, e.g. it is
19715 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19716 x-positions of the drawing area.
19717
19718 This is an ugly monster macro construct because we must use alloca
19719 to allocate glyph strings (because draw_glyphs can be called
19720 asynchronously). */
19721
19722 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19723 do \
19724 { \
19725 HEAD = TAIL = NULL; \
19726 while (START < END) \
19727 { \
19728 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19729 switch (first_glyph->type) \
19730 { \
19731 case CHAR_GLYPH: \
19732 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19733 HL, X, LAST_X); \
19734 break; \
19735 \
19736 case COMPOSITE_GLYPH: \
19737 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19738 HL, X, LAST_X); \
19739 break; \
19740 \
19741 case STRETCH_GLYPH: \
19742 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19743 HL, X, LAST_X); \
19744 break; \
19745 \
19746 case IMAGE_GLYPH: \
19747 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19748 HL, X, LAST_X); \
19749 break; \
19750 \
19751 default: \
19752 abort (); \
19753 } \
19754 \
19755 if (s) \
19756 { \
19757 set_glyph_string_background_width (s, START, LAST_X); \
19758 (X) += s->width; \
19759 } \
19760 } \
19761 } \
19762 while (0)
19763
19764
19765 /* Draw glyphs between START and END in AREA of ROW on window W,
19766 starting at x-position X. X is relative to AREA in W. HL is a
19767 face-override with the following meaning:
19768
19769 DRAW_NORMAL_TEXT draw normally
19770 DRAW_CURSOR draw in cursor face
19771 DRAW_MOUSE_FACE draw in mouse face.
19772 DRAW_INVERSE_VIDEO draw in mode line face
19773 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19774 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19775
19776 If OVERLAPS is non-zero, draw only the foreground of characters and
19777 clip to the physical height of ROW. Non-zero value also defines
19778 the overlapping part to be drawn:
19779
19780 OVERLAPS_PRED overlap with preceding rows
19781 OVERLAPS_SUCC overlap with succeeding rows
19782 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19783 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19784
19785 Value is the x-position reached, relative to AREA of W. */
19786
19787 static int
19788 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19789 struct window *w;
19790 int x;
19791 struct glyph_row *row;
19792 enum glyph_row_area area;
19793 EMACS_INT start, end;
19794 enum draw_glyphs_face hl;
19795 int overlaps;
19796 {
19797 struct glyph_string *head, *tail;
19798 struct glyph_string *s;
19799 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19800 int last_x, area_width;
19801 int x_reached;
19802 int i, j;
19803 struct frame *f = XFRAME (WINDOW_FRAME (w));
19804 DECLARE_HDC (hdc);
19805
19806 ALLOCATE_HDC (hdc, f);
19807
19808 /* Let's rather be paranoid than getting a SEGV. */
19809 end = min (end, row->used[area]);
19810 start = max (0, start);
19811 start = min (end, start);
19812
19813 /* Translate X to frame coordinates. Set last_x to the right
19814 end of the drawing area. */
19815 if (row->full_width_p)
19816 {
19817 /* X is relative to the left edge of W, without scroll bars
19818 or fringes. */
19819 x += WINDOW_LEFT_EDGE_X (w);
19820 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19821 }
19822 else
19823 {
19824 int area_left = window_box_left (w, area);
19825 x += area_left;
19826 area_width = window_box_width (w, area);
19827 last_x = area_left + area_width;
19828 }
19829
19830 /* Build a doubly-linked list of glyph_string structures between
19831 head and tail from what we have to draw. Note that the macro
19832 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19833 the reason we use a separate variable `i'. */
19834 i = start;
19835 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19836 if (tail)
19837 x_reached = tail->x + tail->background_width;
19838 else
19839 x_reached = x;
19840
19841 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19842 the row, redraw some glyphs in front or following the glyph
19843 strings built above. */
19844 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19845 {
19846 int dummy_x = 0;
19847 struct glyph_string *h, *t;
19848
19849 /* Compute overhangs for all glyph strings. */
19850 if (rif->compute_glyph_string_overhangs)
19851 for (s = head; s; s = s->next)
19852 rif->compute_glyph_string_overhangs (s);
19853
19854 /* Prepend glyph strings for glyphs in front of the first glyph
19855 string that are overwritten because of the first glyph
19856 string's left overhang. The background of all strings
19857 prepended must be drawn because the first glyph string
19858 draws over it. */
19859 i = left_overwritten (head);
19860 if (i >= 0)
19861 {
19862 j = i;
19863 BUILD_GLYPH_STRINGS (j, start, h, t,
19864 DRAW_NORMAL_TEXT, dummy_x, last_x);
19865 start = i;
19866 compute_overhangs_and_x (t, head->x, 1);
19867 prepend_glyph_string_lists (&head, &tail, h, t);
19868 clip_head = head;
19869 }
19870
19871 /* Prepend glyph strings for glyphs in front of the first glyph
19872 string that overwrite that glyph string because of their
19873 right overhang. For these strings, only the foreground must
19874 be drawn, because it draws over the glyph string at `head'.
19875 The background must not be drawn because this would overwrite
19876 right overhangs of preceding glyphs for which no glyph
19877 strings exist. */
19878 i = left_overwriting (head);
19879 if (i >= 0)
19880 {
19881 clip_head = head;
19882 BUILD_GLYPH_STRINGS (i, start, h, t,
19883 DRAW_NORMAL_TEXT, dummy_x, last_x);
19884 for (s = h; s; s = s->next)
19885 s->background_filled_p = 1;
19886 compute_overhangs_and_x (t, head->x, 1);
19887 prepend_glyph_string_lists (&head, &tail, h, t);
19888 }
19889
19890 /* Append glyphs strings for glyphs following the last glyph
19891 string tail that are overwritten by tail. The background of
19892 these strings has to be drawn because tail's foreground draws
19893 over it. */
19894 i = right_overwritten (tail);
19895 if (i >= 0)
19896 {
19897 BUILD_GLYPH_STRINGS (end, i, h, t,
19898 DRAW_NORMAL_TEXT, x, last_x);
19899 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19900 append_glyph_string_lists (&head, &tail, h, t);
19901 clip_tail = tail;
19902 }
19903
19904 /* Append glyph strings for glyphs following the last glyph
19905 string tail that overwrite tail. The foreground of such
19906 glyphs has to be drawn because it writes into the background
19907 of tail. The background must not be drawn because it could
19908 paint over the foreground of following glyphs. */
19909 i = right_overwriting (tail);
19910 if (i >= 0)
19911 {
19912 clip_tail = tail;
19913 i++; /* We must include the Ith glyph. */
19914 BUILD_GLYPH_STRINGS (end, i, h, t,
19915 DRAW_NORMAL_TEXT, x, last_x);
19916 for (s = h; s; s = s->next)
19917 s->background_filled_p = 1;
19918 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19919 append_glyph_string_lists (&head, &tail, h, t);
19920 }
19921 if (clip_head || clip_tail)
19922 for (s = head; s; s = s->next)
19923 {
19924 s->clip_head = clip_head;
19925 s->clip_tail = clip_tail;
19926 }
19927 }
19928
19929 /* Draw all strings. */
19930 for (s = head; s; s = s->next)
19931 rif->draw_glyph_string (s);
19932
19933 if (area == TEXT_AREA
19934 && !row->full_width_p
19935 /* When drawing overlapping rows, only the glyph strings'
19936 foreground is drawn, which doesn't erase a cursor
19937 completely. */
19938 && !overlaps)
19939 {
19940 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19941 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19942 : (tail ? tail->x + tail->background_width : x));
19943
19944 int text_left = window_box_left (w, TEXT_AREA);
19945 x0 -= text_left;
19946 x1 -= text_left;
19947
19948 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19949 row->y, MATRIX_ROW_BOTTOM_Y (row));
19950 }
19951
19952 /* Value is the x-position up to which drawn, relative to AREA of W.
19953 This doesn't include parts drawn because of overhangs. */
19954 if (row->full_width_p)
19955 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19956 else
19957 x_reached -= window_box_left (w, area);
19958
19959 RELEASE_HDC (hdc, f);
19960
19961 return x_reached;
19962 }
19963
19964 /* Expand row matrix if too narrow. Don't expand if area
19965 is not present. */
19966
19967 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19968 { \
19969 if (!fonts_changed_p \
19970 && (it->glyph_row->glyphs[area] \
19971 < it->glyph_row->glyphs[area + 1])) \
19972 { \
19973 it->w->ncols_scale_factor++; \
19974 fonts_changed_p = 1; \
19975 } \
19976 }
19977
19978 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19979 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19980
19981 static INLINE void
19982 append_glyph (it)
19983 struct it *it;
19984 {
19985 struct glyph *glyph;
19986 enum glyph_row_area area = it->area;
19987
19988 xassert (it->glyph_row);
19989 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19990
19991 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19992 if (glyph < it->glyph_row->glyphs[area + 1])
19993 {
19994 glyph->charpos = CHARPOS (it->position);
19995 glyph->object = it->object;
19996 glyph->pixel_width = it->pixel_width;
19997 glyph->ascent = it->ascent;
19998 glyph->descent = it->descent;
19999 glyph->voffset = it->voffset;
20000 glyph->type = CHAR_GLYPH;
20001 glyph->multibyte_p = it->multibyte_p;
20002 glyph->left_box_line_p = it->start_of_box_run_p;
20003 glyph->right_box_line_p = it->end_of_box_run_p;
20004 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20005 || it->phys_descent > it->descent);
20006 glyph->padding_p = 0;
20007 glyph->glyph_not_available_p = it->glyph_not_available_p;
20008 glyph->face_id = it->face_id;
20009 glyph->u.ch = it->char_to_display;
20010 glyph->slice = null_glyph_slice;
20011 glyph->font_type = FONT_TYPE_UNKNOWN;
20012 ++it->glyph_row->used[area];
20013 }
20014 else
20015 IT_EXPAND_MATRIX_WIDTH (it, area);
20016 }
20017
20018 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20019 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20020
20021 static INLINE void
20022 append_composite_glyph (it)
20023 struct it *it;
20024 {
20025 struct glyph *glyph;
20026 enum glyph_row_area area = it->area;
20027
20028 xassert (it->glyph_row);
20029
20030 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20031 if (glyph < it->glyph_row->glyphs[area + 1])
20032 {
20033 glyph->charpos = CHARPOS (it->position);
20034 glyph->object = it->object;
20035 glyph->pixel_width = it->pixel_width;
20036 glyph->ascent = it->ascent;
20037 glyph->descent = it->descent;
20038 glyph->voffset = it->voffset;
20039 glyph->type = COMPOSITE_GLYPH;
20040 glyph->multibyte_p = it->multibyte_p;
20041 glyph->left_box_line_p = it->start_of_box_run_p;
20042 glyph->right_box_line_p = it->end_of_box_run_p;
20043 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20044 || it->phys_descent > it->descent);
20045 glyph->padding_p = 0;
20046 glyph->glyph_not_available_p = 0;
20047 glyph->face_id = it->face_id;
20048 glyph->u.cmp_id = it->cmp_id;
20049 glyph->slice = null_glyph_slice;
20050 glyph->font_type = FONT_TYPE_UNKNOWN;
20051 ++it->glyph_row->used[area];
20052 }
20053 else
20054 IT_EXPAND_MATRIX_WIDTH (it, area);
20055 }
20056
20057
20058 /* Change IT->ascent and IT->height according to the setting of
20059 IT->voffset. */
20060
20061 static INLINE void
20062 take_vertical_position_into_account (it)
20063 struct it *it;
20064 {
20065 if (it->voffset)
20066 {
20067 if (it->voffset < 0)
20068 /* Increase the ascent so that we can display the text higher
20069 in the line. */
20070 it->ascent -= it->voffset;
20071 else
20072 /* Increase the descent so that we can display the text lower
20073 in the line. */
20074 it->descent += it->voffset;
20075 }
20076 }
20077
20078
20079 /* Produce glyphs/get display metrics for the image IT is loaded with.
20080 See the description of struct display_iterator in dispextern.h for
20081 an overview of struct display_iterator. */
20082
20083 static void
20084 produce_image_glyph (it)
20085 struct it *it;
20086 {
20087 struct image *img;
20088 struct face *face;
20089 int glyph_ascent, crop;
20090 struct glyph_slice slice;
20091
20092 xassert (it->what == IT_IMAGE);
20093
20094 face = FACE_FROM_ID (it->f, it->face_id);
20095 xassert (face);
20096 /* Make sure X resources of the face is loaded. */
20097 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20098
20099 if (it->image_id < 0)
20100 {
20101 /* Fringe bitmap. */
20102 it->ascent = it->phys_ascent = 0;
20103 it->descent = it->phys_descent = 0;
20104 it->pixel_width = 0;
20105 it->nglyphs = 0;
20106 return;
20107 }
20108
20109 img = IMAGE_FROM_ID (it->f, it->image_id);
20110 xassert (img);
20111 /* Make sure X resources of the image is loaded. */
20112 prepare_image_for_display (it->f, img);
20113
20114 slice.x = slice.y = 0;
20115 slice.width = img->width;
20116 slice.height = img->height;
20117
20118 if (INTEGERP (it->slice.x))
20119 slice.x = XINT (it->slice.x);
20120 else if (FLOATP (it->slice.x))
20121 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20122
20123 if (INTEGERP (it->slice.y))
20124 slice.y = XINT (it->slice.y);
20125 else if (FLOATP (it->slice.y))
20126 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20127
20128 if (INTEGERP (it->slice.width))
20129 slice.width = XINT (it->slice.width);
20130 else if (FLOATP (it->slice.width))
20131 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20132
20133 if (INTEGERP (it->slice.height))
20134 slice.height = XINT (it->slice.height);
20135 else if (FLOATP (it->slice.height))
20136 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20137
20138 if (slice.x >= img->width)
20139 slice.x = img->width;
20140 if (slice.y >= img->height)
20141 slice.y = img->height;
20142 if (slice.x + slice.width >= img->width)
20143 slice.width = img->width - slice.x;
20144 if (slice.y + slice.height > img->height)
20145 slice.height = img->height - slice.y;
20146
20147 if (slice.width == 0 || slice.height == 0)
20148 return;
20149
20150 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20151
20152 it->descent = slice.height - glyph_ascent;
20153 if (slice.y == 0)
20154 it->descent += img->vmargin;
20155 if (slice.y + slice.height == img->height)
20156 it->descent += img->vmargin;
20157 it->phys_descent = it->descent;
20158
20159 it->pixel_width = slice.width;
20160 if (slice.x == 0)
20161 it->pixel_width += img->hmargin;
20162 if (slice.x + slice.width == img->width)
20163 it->pixel_width += img->hmargin;
20164
20165 /* It's quite possible for images to have an ascent greater than
20166 their height, so don't get confused in that case. */
20167 if (it->descent < 0)
20168 it->descent = 0;
20169
20170 #if 0 /* this breaks image tiling */
20171 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20172 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20173 if (face_ascent > it->ascent)
20174 it->ascent = it->phys_ascent = face_ascent;
20175 #endif
20176
20177 it->nglyphs = 1;
20178
20179 if (face->box != FACE_NO_BOX)
20180 {
20181 if (face->box_line_width > 0)
20182 {
20183 if (slice.y == 0)
20184 it->ascent += face->box_line_width;
20185 if (slice.y + slice.height == img->height)
20186 it->descent += face->box_line_width;
20187 }
20188
20189 if (it->start_of_box_run_p && slice.x == 0)
20190 it->pixel_width += abs (face->box_line_width);
20191 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20192 it->pixel_width += abs (face->box_line_width);
20193 }
20194
20195 take_vertical_position_into_account (it);
20196
20197 /* Automatically crop wide image glyphs at right edge so we can
20198 draw the cursor on same display row. */
20199 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20200 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20201 {
20202 it->pixel_width -= crop;
20203 slice.width -= crop;
20204 }
20205
20206 if (it->glyph_row)
20207 {
20208 struct glyph *glyph;
20209 enum glyph_row_area area = it->area;
20210
20211 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20212 if (glyph < it->glyph_row->glyphs[area + 1])
20213 {
20214 glyph->charpos = CHARPOS (it->position);
20215 glyph->object = it->object;
20216 glyph->pixel_width = it->pixel_width;
20217 glyph->ascent = glyph_ascent;
20218 glyph->descent = it->descent;
20219 glyph->voffset = it->voffset;
20220 glyph->type = IMAGE_GLYPH;
20221 glyph->multibyte_p = it->multibyte_p;
20222 glyph->left_box_line_p = it->start_of_box_run_p;
20223 glyph->right_box_line_p = it->end_of_box_run_p;
20224 glyph->overlaps_vertically_p = 0;
20225 glyph->padding_p = 0;
20226 glyph->glyph_not_available_p = 0;
20227 glyph->face_id = it->face_id;
20228 glyph->u.img_id = img->id;
20229 glyph->slice = slice;
20230 glyph->font_type = FONT_TYPE_UNKNOWN;
20231 ++it->glyph_row->used[area];
20232 }
20233 else
20234 IT_EXPAND_MATRIX_WIDTH (it, area);
20235 }
20236 }
20237
20238
20239 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20240 of the glyph, WIDTH and HEIGHT are the width and height of the
20241 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20242
20243 static void
20244 append_stretch_glyph (it, object, width, height, ascent)
20245 struct it *it;
20246 Lisp_Object object;
20247 int width, height;
20248 int ascent;
20249 {
20250 struct glyph *glyph;
20251 enum glyph_row_area area = it->area;
20252
20253 xassert (ascent >= 0 && ascent <= height);
20254
20255 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20256 if (glyph < it->glyph_row->glyphs[area + 1])
20257 {
20258 glyph->charpos = CHARPOS (it->position);
20259 glyph->object = object;
20260 glyph->pixel_width = width;
20261 glyph->ascent = ascent;
20262 glyph->descent = height - ascent;
20263 glyph->voffset = it->voffset;
20264 glyph->type = STRETCH_GLYPH;
20265 glyph->multibyte_p = it->multibyte_p;
20266 glyph->left_box_line_p = it->start_of_box_run_p;
20267 glyph->right_box_line_p = it->end_of_box_run_p;
20268 glyph->overlaps_vertically_p = 0;
20269 glyph->padding_p = 0;
20270 glyph->glyph_not_available_p = 0;
20271 glyph->face_id = it->face_id;
20272 glyph->u.stretch.ascent = ascent;
20273 glyph->u.stretch.height = height;
20274 glyph->slice = null_glyph_slice;
20275 glyph->font_type = FONT_TYPE_UNKNOWN;
20276 ++it->glyph_row->used[area];
20277 }
20278 else
20279 IT_EXPAND_MATRIX_WIDTH (it, area);
20280 }
20281
20282
20283 /* Produce a stretch glyph for iterator IT. IT->object is the value
20284 of the glyph property displayed. The value must be a list
20285 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20286 being recognized:
20287
20288 1. `:width WIDTH' specifies that the space should be WIDTH *
20289 canonical char width wide. WIDTH may be an integer or floating
20290 point number.
20291
20292 2. `:relative-width FACTOR' specifies that the width of the stretch
20293 should be computed from the width of the first character having the
20294 `glyph' property, and should be FACTOR times that width.
20295
20296 3. `:align-to HPOS' specifies that the space should be wide enough
20297 to reach HPOS, a value in canonical character units.
20298
20299 Exactly one of the above pairs must be present.
20300
20301 4. `:height HEIGHT' specifies that the height of the stretch produced
20302 should be HEIGHT, measured in canonical character units.
20303
20304 5. `:relative-height FACTOR' specifies that the height of the
20305 stretch should be FACTOR times the height of the characters having
20306 the glyph property.
20307
20308 Either none or exactly one of 4 or 5 must be present.
20309
20310 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20311 of the stretch should be used for the ascent of the stretch.
20312 ASCENT must be in the range 0 <= ASCENT <= 100. */
20313
20314 static void
20315 produce_stretch_glyph (it)
20316 struct it *it;
20317 {
20318 /* (space :width WIDTH :height HEIGHT ...) */
20319 Lisp_Object prop, plist;
20320 int width = 0, height = 0, align_to = -1;
20321 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20322 int ascent = 0;
20323 double tem;
20324 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20325 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20326
20327 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20328
20329 /* List should start with `space'. */
20330 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20331 plist = XCDR (it->object);
20332
20333 /* Compute the width of the stretch. */
20334 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20335 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20336 {
20337 /* Absolute width `:width WIDTH' specified and valid. */
20338 zero_width_ok_p = 1;
20339 width = (int)tem;
20340 }
20341 else if (prop = Fplist_get (plist, QCrelative_width),
20342 NUMVAL (prop) > 0)
20343 {
20344 /* Relative width `:relative-width FACTOR' specified and valid.
20345 Compute the width of the characters having the `glyph'
20346 property. */
20347 struct it it2;
20348 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20349
20350 it2 = *it;
20351 if (it->multibyte_p)
20352 {
20353 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20354 - IT_BYTEPOS (*it));
20355 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20356 }
20357 else
20358 it2.c = *p, it2.len = 1;
20359
20360 it2.glyph_row = NULL;
20361 it2.what = IT_CHARACTER;
20362 x_produce_glyphs (&it2);
20363 width = NUMVAL (prop) * it2.pixel_width;
20364 }
20365 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20366 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20367 {
20368 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20369 align_to = (align_to < 0
20370 ? 0
20371 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20372 else if (align_to < 0)
20373 align_to = window_box_left_offset (it->w, TEXT_AREA);
20374 width = max (0, (int)tem + align_to - it->current_x);
20375 zero_width_ok_p = 1;
20376 }
20377 else
20378 /* Nothing specified -> width defaults to canonical char width. */
20379 width = FRAME_COLUMN_WIDTH (it->f);
20380
20381 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20382 width = 1;
20383
20384 /* Compute height. */
20385 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20386 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20387 {
20388 height = (int)tem;
20389 zero_height_ok_p = 1;
20390 }
20391 else if (prop = Fplist_get (plist, QCrelative_height),
20392 NUMVAL (prop) > 0)
20393 height = FONT_HEIGHT (font) * NUMVAL (prop);
20394 else
20395 height = FONT_HEIGHT (font);
20396
20397 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20398 height = 1;
20399
20400 /* Compute percentage of height used for ascent. If
20401 `:ascent ASCENT' is present and valid, use that. Otherwise,
20402 derive the ascent from the font in use. */
20403 if (prop = Fplist_get (plist, QCascent),
20404 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20405 ascent = height * NUMVAL (prop) / 100.0;
20406 else if (!NILP (prop)
20407 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20408 ascent = min (max (0, (int)tem), height);
20409 else
20410 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20411
20412 if (width > 0 && !it->truncate_lines_p
20413 && it->current_x + width > it->last_visible_x)
20414 width = it->last_visible_x - it->current_x - 1;
20415
20416 if (width > 0 && height > 0 && it->glyph_row)
20417 {
20418 Lisp_Object object = it->stack[it->sp - 1].string;
20419 if (!STRINGP (object))
20420 object = it->w->buffer;
20421 append_stretch_glyph (it, object, width, height, ascent);
20422 }
20423
20424 it->pixel_width = width;
20425 it->ascent = it->phys_ascent = ascent;
20426 it->descent = it->phys_descent = height - it->ascent;
20427 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20428
20429 take_vertical_position_into_account (it);
20430 }
20431
20432 /* Get line-height and line-spacing property at point.
20433 If line-height has format (HEIGHT TOTAL), return TOTAL
20434 in TOTAL_HEIGHT. */
20435
20436 static Lisp_Object
20437 get_line_height_property (it, prop)
20438 struct it *it;
20439 Lisp_Object prop;
20440 {
20441 Lisp_Object position;
20442
20443 if (STRINGP (it->object))
20444 position = make_number (IT_STRING_CHARPOS (*it));
20445 else if (BUFFERP (it->object))
20446 position = make_number (IT_CHARPOS (*it));
20447 else
20448 return Qnil;
20449
20450 return Fget_char_property (position, prop, it->object);
20451 }
20452
20453 /* Calculate line-height and line-spacing properties.
20454 An integer value specifies explicit pixel value.
20455 A float value specifies relative value to current face height.
20456 A cons (float . face-name) specifies relative value to
20457 height of specified face font.
20458
20459 Returns height in pixels, or nil. */
20460
20461
20462 static Lisp_Object
20463 calc_line_height_property (it, val, font, boff, override)
20464 struct it *it;
20465 Lisp_Object val;
20466 XFontStruct *font;
20467 int boff, override;
20468 {
20469 Lisp_Object face_name = Qnil;
20470 int ascent, descent, height;
20471
20472 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20473 return val;
20474
20475 if (CONSP (val))
20476 {
20477 face_name = XCAR (val);
20478 val = XCDR (val);
20479 if (!NUMBERP (val))
20480 val = make_number (1);
20481 if (NILP (face_name))
20482 {
20483 height = it->ascent + it->descent;
20484 goto scale;
20485 }
20486 }
20487
20488 if (NILP (face_name))
20489 {
20490 font = FRAME_FONT (it->f);
20491 boff = FRAME_BASELINE_OFFSET (it->f);
20492 }
20493 else if (EQ (face_name, Qt))
20494 {
20495 override = 0;
20496 }
20497 else
20498 {
20499 int face_id;
20500 struct face *face;
20501 struct font_info *font_info;
20502
20503 face_id = lookup_named_face (it->f, face_name, 0);
20504 if (face_id < 0)
20505 return make_number (-1);
20506
20507 face = FACE_FROM_ID (it->f, face_id);
20508 font = face->font;
20509 if (font == NULL)
20510 return make_number (-1);
20511
20512 font_info = FONT_INFO_FROM_FACE (it->f, face);
20513 boff = font_info->baseline_offset;
20514 if (font_info->vertical_centering)
20515 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20516 }
20517
20518 ascent = FONT_BASE (font) + boff;
20519 descent = FONT_DESCENT (font) - boff;
20520
20521 if (override)
20522 {
20523 it->override_ascent = ascent;
20524 it->override_descent = descent;
20525 it->override_boff = boff;
20526 }
20527
20528 height = ascent + descent;
20529
20530 scale:
20531 if (FLOATP (val))
20532 height = (int)(XFLOAT_DATA (val) * height);
20533 else if (INTEGERP (val))
20534 height *= XINT (val);
20535
20536 return make_number (height);
20537 }
20538
20539
20540 /* RIF:
20541 Produce glyphs/get display metrics for the display element IT is
20542 loaded with. See the description of struct it in dispextern.h
20543 for an overview of struct it. */
20544
20545 void
20546 x_produce_glyphs (it)
20547 struct it *it;
20548 {
20549 int extra_line_spacing = it->extra_line_spacing;
20550
20551 it->glyph_not_available_p = 0;
20552
20553 if (it->what == IT_CHARACTER)
20554 {
20555 XChar2b char2b;
20556 XFontStruct *font;
20557 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20558 XCharStruct *pcm;
20559 int font_not_found_p;
20560 struct font_info *font_info;
20561 int boff; /* baseline offset */
20562 /* We may change it->multibyte_p upon unibyte<->multibyte
20563 conversion. So, save the current value now and restore it
20564 later.
20565
20566 Note: It seems that we don't have to record multibyte_p in
20567 struct glyph because the character code itself tells if or
20568 not the character is multibyte. Thus, in the future, we must
20569 consider eliminating the field `multibyte_p' in the struct
20570 glyph. */
20571 int saved_multibyte_p = it->multibyte_p;
20572
20573 /* Maybe translate single-byte characters to multibyte, or the
20574 other way. */
20575 it->char_to_display = it->c;
20576 if (!ASCII_BYTE_P (it->c)
20577 && ! it->multibyte_p)
20578 {
20579 if (SINGLE_BYTE_CHAR_P (it->c)
20580 && unibyte_display_via_language_environment)
20581 it->char_to_display = unibyte_char_to_multibyte (it->c);
20582 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20583 {
20584 it->multibyte_p = 1;
20585 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20586 -1, Qnil);
20587 face = FACE_FROM_ID (it->f, it->face_id);
20588 }
20589 }
20590
20591 /* Get font to use. Encode IT->char_to_display. */
20592 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20593 &char2b, it->multibyte_p, 0);
20594 font = face->font;
20595
20596 /* When no suitable font found, use the default font. */
20597 font_not_found_p = font == NULL;
20598 if (font_not_found_p)
20599 {
20600 font = FRAME_FONT (it->f);
20601 boff = FRAME_BASELINE_OFFSET (it->f);
20602 font_info = NULL;
20603 }
20604 else
20605 {
20606 font_info = FONT_INFO_FROM_FACE (it->f, face);
20607 boff = font_info->baseline_offset;
20608 if (font_info->vertical_centering)
20609 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20610 }
20611
20612 if (it->char_to_display >= ' '
20613 && (!it->multibyte_p || it->char_to_display < 128))
20614 {
20615 /* Either unibyte or ASCII. */
20616 int stretched_p;
20617
20618 it->nglyphs = 1;
20619
20620 pcm = get_per_char_metric (font, font_info, &char2b,
20621 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20622
20623 if (it->override_ascent >= 0)
20624 {
20625 it->ascent = it->override_ascent;
20626 it->descent = it->override_descent;
20627 boff = it->override_boff;
20628 }
20629 else
20630 {
20631 it->ascent = FONT_BASE (font) + boff;
20632 it->descent = FONT_DESCENT (font) - boff;
20633 }
20634
20635 if (pcm)
20636 {
20637 it->phys_ascent = pcm->ascent + boff;
20638 it->phys_descent = pcm->descent - boff;
20639 it->pixel_width = pcm->width;
20640 }
20641 else
20642 {
20643 it->glyph_not_available_p = 1;
20644 it->phys_ascent = it->ascent;
20645 it->phys_descent = it->descent;
20646 it->pixel_width = FONT_WIDTH (font);
20647 }
20648
20649 if (it->constrain_row_ascent_descent_p)
20650 {
20651 if (it->descent > it->max_descent)
20652 {
20653 it->ascent += it->descent - it->max_descent;
20654 it->descent = it->max_descent;
20655 }
20656 if (it->ascent > it->max_ascent)
20657 {
20658 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20659 it->ascent = it->max_ascent;
20660 }
20661 it->phys_ascent = min (it->phys_ascent, it->ascent);
20662 it->phys_descent = min (it->phys_descent, it->descent);
20663 extra_line_spacing = 0;
20664 }
20665
20666 /* If this is a space inside a region of text with
20667 `space-width' property, change its width. */
20668 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20669 if (stretched_p)
20670 it->pixel_width *= XFLOATINT (it->space_width);
20671
20672 /* If face has a box, add the box thickness to the character
20673 height. If character has a box line to the left and/or
20674 right, add the box line width to the character's width. */
20675 if (face->box != FACE_NO_BOX)
20676 {
20677 int thick = face->box_line_width;
20678
20679 if (thick > 0)
20680 {
20681 it->ascent += thick;
20682 it->descent += thick;
20683 }
20684 else
20685 thick = -thick;
20686
20687 if (it->start_of_box_run_p)
20688 it->pixel_width += thick;
20689 if (it->end_of_box_run_p)
20690 it->pixel_width += thick;
20691 }
20692
20693 /* If face has an overline, add the height of the overline
20694 (1 pixel) and a 1 pixel margin to the character height. */
20695 if (face->overline_p)
20696 it->ascent += overline_margin;
20697
20698 if (it->constrain_row_ascent_descent_p)
20699 {
20700 if (it->ascent > it->max_ascent)
20701 it->ascent = it->max_ascent;
20702 if (it->descent > it->max_descent)
20703 it->descent = it->max_descent;
20704 }
20705
20706 take_vertical_position_into_account (it);
20707
20708 /* If we have to actually produce glyphs, do it. */
20709 if (it->glyph_row)
20710 {
20711 if (stretched_p)
20712 {
20713 /* Translate a space with a `space-width' property
20714 into a stretch glyph. */
20715 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20716 / FONT_HEIGHT (font));
20717 append_stretch_glyph (it, it->object, it->pixel_width,
20718 it->ascent + it->descent, ascent);
20719 }
20720 else
20721 append_glyph (it);
20722
20723 /* If characters with lbearing or rbearing are displayed
20724 in this line, record that fact in a flag of the
20725 glyph row. This is used to optimize X output code. */
20726 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20727 it->glyph_row->contains_overlapping_glyphs_p = 1;
20728 }
20729 }
20730 else if (it->char_to_display == '\n')
20731 {
20732 /* A newline has no width but we need the height of the line.
20733 But if previous part of the line set a height, don't
20734 increase that height */
20735
20736 Lisp_Object height;
20737 Lisp_Object total_height = Qnil;
20738
20739 it->override_ascent = -1;
20740 it->pixel_width = 0;
20741 it->nglyphs = 0;
20742
20743 height = get_line_height_property(it, Qline_height);
20744 /* Split (line-height total-height) list */
20745 if (CONSP (height)
20746 && CONSP (XCDR (height))
20747 && NILP (XCDR (XCDR (height))))
20748 {
20749 total_height = XCAR (XCDR (height));
20750 height = XCAR (height);
20751 }
20752 height = calc_line_height_property(it, height, font, boff, 1);
20753
20754 if (it->override_ascent >= 0)
20755 {
20756 it->ascent = it->override_ascent;
20757 it->descent = it->override_descent;
20758 boff = it->override_boff;
20759 }
20760 else
20761 {
20762 it->ascent = FONT_BASE (font) + boff;
20763 it->descent = FONT_DESCENT (font) - boff;
20764 }
20765
20766 if (EQ (height, Qt))
20767 {
20768 if (it->descent > it->max_descent)
20769 {
20770 it->ascent += it->descent - it->max_descent;
20771 it->descent = it->max_descent;
20772 }
20773 if (it->ascent > it->max_ascent)
20774 {
20775 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20776 it->ascent = it->max_ascent;
20777 }
20778 it->phys_ascent = min (it->phys_ascent, it->ascent);
20779 it->phys_descent = min (it->phys_descent, it->descent);
20780 it->constrain_row_ascent_descent_p = 1;
20781 extra_line_spacing = 0;
20782 }
20783 else
20784 {
20785 Lisp_Object spacing;
20786
20787 it->phys_ascent = it->ascent;
20788 it->phys_descent = it->descent;
20789
20790 if ((it->max_ascent > 0 || it->max_descent > 0)
20791 && face->box != FACE_NO_BOX
20792 && face->box_line_width > 0)
20793 {
20794 it->ascent += face->box_line_width;
20795 it->descent += face->box_line_width;
20796 }
20797 if (!NILP (height)
20798 && XINT (height) > it->ascent + it->descent)
20799 it->ascent = XINT (height) - it->descent;
20800
20801 if (!NILP (total_height))
20802 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20803 else
20804 {
20805 spacing = get_line_height_property(it, Qline_spacing);
20806 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20807 }
20808 if (INTEGERP (spacing))
20809 {
20810 extra_line_spacing = XINT (spacing);
20811 if (!NILP (total_height))
20812 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20813 }
20814 }
20815 }
20816 else if (it->char_to_display == '\t')
20817 {
20818 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20819 int x = it->current_x + it->continuation_lines_width;
20820 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20821
20822 /* If the distance from the current position to the next tab
20823 stop is less than a space character width, use the
20824 tab stop after that. */
20825 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20826 next_tab_x += tab_width;
20827
20828 it->pixel_width = next_tab_x - x;
20829 it->nglyphs = 1;
20830 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20831 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20832
20833 if (it->glyph_row)
20834 {
20835 append_stretch_glyph (it, it->object, it->pixel_width,
20836 it->ascent + it->descent, it->ascent);
20837 }
20838 }
20839 else
20840 {
20841 /* A multi-byte character. Assume that the display width of the
20842 character is the width of the character multiplied by the
20843 width of the font. */
20844
20845 /* If we found a font, this font should give us the right
20846 metrics. If we didn't find a font, use the frame's
20847 default font and calculate the width of the character by
20848 multiplying the width of font by the width of the
20849 character. */
20850
20851 pcm = get_per_char_metric (font, font_info, &char2b,
20852 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20853
20854 if (font_not_found_p || !pcm)
20855 {
20856 it->glyph_not_available_p = 1;
20857 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20858 * CHAR_WIDTH (it->char_to_display));
20859 it->phys_ascent = FONT_BASE (font) + boff;
20860 it->phys_descent = FONT_DESCENT (font) - boff;
20861 }
20862 else
20863 {
20864 it->pixel_width = pcm->width;
20865 it->phys_ascent = pcm->ascent + boff;
20866 it->phys_descent = pcm->descent - boff;
20867 if (it->glyph_row
20868 && (pcm->lbearing < 0
20869 || pcm->rbearing > pcm->width))
20870 it->glyph_row->contains_overlapping_glyphs_p = 1;
20871 }
20872 it->nglyphs = 1;
20873 it->ascent = FONT_BASE (font) + boff;
20874 it->descent = FONT_DESCENT (font) - boff;
20875 if (face->box != FACE_NO_BOX)
20876 {
20877 int thick = face->box_line_width;
20878
20879 if (thick > 0)
20880 {
20881 it->ascent += thick;
20882 it->descent += thick;
20883 }
20884 else
20885 thick = - thick;
20886
20887 if (it->start_of_box_run_p)
20888 it->pixel_width += thick;
20889 if (it->end_of_box_run_p)
20890 it->pixel_width += thick;
20891 }
20892
20893 /* If face has an overline, add the height of the overline
20894 (1 pixel) and a 1 pixel margin to the character height. */
20895 if (face->overline_p)
20896 it->ascent += overline_margin;
20897
20898 take_vertical_position_into_account (it);
20899
20900 if (it->glyph_row)
20901 append_glyph (it);
20902 }
20903 it->multibyte_p = saved_multibyte_p;
20904 }
20905 else if (it->what == IT_COMPOSITION)
20906 {
20907 /* Note: A composition is represented as one glyph in the
20908 glyph matrix. There are no padding glyphs.
20909
20910 Important is that pixel_width, ascent, and descent are the
20911 values of what is drawn by draw_glyphs (i.e. the values of
20912 the overall glyphs composed). */
20913 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20914 int boff; /* baseline offset */
20915 struct composition *cmp = composition_table[it->cmp_id];
20916 int glyph_len = cmp->glyph_len;
20917 XFontStruct *font = face->font;
20918
20919 it->nglyphs = 1;
20920
20921 #ifdef USE_FONT_BACKEND
20922 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
20923 {
20924 if (! cmp->font || cmp->font != font)
20925 font_prepare_composition (cmp);
20926 }
20927 else
20928 #endif /* USE_FONT_BACKEND */
20929 /* If we have not yet calculated pixel size data of glyphs of
20930 the composition for the current face font, calculate them
20931 now. Theoretically, we have to check all fonts for the
20932 glyphs, but that requires much time and memory space. So,
20933 here we check only the font of the first glyph. This leads
20934 to incorrect display, but it's very rare, and C-l (recenter)
20935 can correct the display anyway. */
20936 if (! cmp->font || cmp->font != font)
20937 {
20938 /* Ascent and descent of the font of the first character
20939 of this composition (adjusted by baseline offset).
20940 Ascent and descent of overall glyphs should not be less
20941 than them respectively. */
20942 int font_ascent, font_descent, font_height;
20943 /* Bounding box of the overall glyphs. */
20944 int leftmost, rightmost, lowest, highest;
20945 int lbearing, rbearing;
20946 int i, width, ascent, descent;
20947 int left_padded = 0, right_padded = 0;
20948 int face_id;
20949 int c;
20950 XChar2b char2b;
20951 XCharStruct *pcm;
20952 int font_not_found_p;
20953 struct font_info *font_info;
20954 int pos;
20955
20956 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
20957 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
20958 break;
20959 if (glyph_len < cmp->glyph_len)
20960 right_padded = 1;
20961 for (i = 0; i < glyph_len; i++)
20962 {
20963 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
20964 break;
20965 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
20966 }
20967 if (i > 0)
20968 left_padded = 1;
20969
20970 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
20971 : IT_CHARPOS (*it));
20972 /* When no suitable font found, use the default font. */
20973 font_not_found_p = font == NULL;
20974 if (font_not_found_p)
20975 {
20976 face = face->ascii_face;
20977 font = face->font;
20978 }
20979 font_info = FONT_INFO_FROM_FACE (it->f, face);
20980 boff = font_info->baseline_offset;
20981 if (font_info->vertical_centering)
20982 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20983 font_ascent = FONT_BASE (font) + boff;
20984 font_descent = FONT_DESCENT (font) - boff;
20985 font_height = FONT_HEIGHT (font);
20986
20987 cmp->font = (void *) font;
20988
20989 pcm = NULL;
20990 if (! font_not_found_p)
20991 {
20992 get_char_face_and_encoding (it->f, c, it->face_id,
20993 &char2b, it->multibyte_p, 0);
20994 pcm = get_per_char_metric (font, font_info, &char2b,
20995 FONT_TYPE_FOR_MULTIBYTE (font, c));
20996 }
20997
20998 /* Initialize the bounding box. */
20999 if (pcm)
21000 {
21001 width = pcm->width;
21002 ascent = pcm->ascent;
21003 descent = pcm->descent;
21004 lbearing = pcm->lbearing;
21005 rbearing = pcm->rbearing;
21006 }
21007 else
21008 {
21009 width = FONT_WIDTH (font);
21010 ascent = FONT_BASE (font);
21011 descent = FONT_DESCENT (font);
21012 lbearing = 0;
21013 rbearing = width;
21014 }
21015
21016 rightmost = width;
21017 leftmost = 0;
21018 lowest = - descent + boff;
21019 highest = ascent + boff;
21020
21021 if (! font_not_found_p
21022 && font_info->default_ascent
21023 && CHAR_TABLE_P (Vuse_default_ascent)
21024 && !NILP (Faref (Vuse_default_ascent,
21025 make_number (it->char_to_display))))
21026 highest = font_info->default_ascent + boff;
21027
21028 /* Draw the first glyph at the normal position. It may be
21029 shifted to right later if some other glyphs are drawn
21030 at the left. */
21031 cmp->offsets[i * 2] = 0;
21032 cmp->offsets[i * 2 + 1] = boff;
21033 cmp->lbearing = lbearing;
21034 cmp->rbearing = rbearing;
21035
21036 /* Set cmp->offsets for the remaining glyphs. */
21037 for (i++; i < glyph_len; i++)
21038 {
21039 int left, right, btm, top;
21040 int ch = COMPOSITION_GLYPH (cmp, i);
21041 int face_id;
21042 struct face *this_face;
21043 int this_boff;
21044
21045 if (ch == '\t')
21046 ch = ' ';
21047 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21048 this_face = FACE_FROM_ID (it->f, face_id);
21049 font = this_face->font;
21050
21051 if (font == NULL)
21052 pcm = NULL;
21053 else
21054 {
21055 font_info = FONT_INFO_FROM_FACE (it->f, this_face);
21056 this_boff = font_info->baseline_offset;
21057 if (font_info->vertical_centering)
21058 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21059 get_char_face_and_encoding (it->f, ch, face_id,
21060 &char2b, it->multibyte_p, 0);
21061 pcm = get_per_char_metric (font, font_info, &char2b,
21062 FONT_TYPE_FOR_MULTIBYTE (font,
21063 ch));
21064 }
21065 if (! pcm)
21066 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21067 else
21068 {
21069 width = pcm->width;
21070 ascent = pcm->ascent;
21071 descent = pcm->descent;
21072 lbearing = pcm->lbearing;
21073 rbearing = pcm->rbearing;
21074 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21075 {
21076 /* Relative composition with or without
21077 alternate chars. */
21078 left = (leftmost + rightmost - width) / 2;
21079 btm = - descent + boff;
21080 if (font_info->relative_compose
21081 && (! CHAR_TABLE_P (Vignore_relative_composition)
21082 || NILP (Faref (Vignore_relative_composition,
21083 make_number (ch)))))
21084 {
21085
21086 if (- descent >= font_info->relative_compose)
21087 /* One extra pixel between two glyphs. */
21088 btm = highest + 1;
21089 else if (ascent <= 0)
21090 /* One extra pixel between two glyphs. */
21091 btm = lowest - 1 - ascent - descent;
21092 }
21093 }
21094 else
21095 {
21096 /* A composition rule is specified by an integer
21097 value that encodes global and new reference
21098 points (GREF and NREF). GREF and NREF are
21099 specified by numbers as below:
21100
21101 0---1---2 -- ascent
21102 | |
21103 | |
21104 | |
21105 9--10--11 -- center
21106 | |
21107 ---3---4---5--- baseline
21108 | |
21109 6---7---8 -- descent
21110 */
21111 int rule = COMPOSITION_RULE (cmp, i);
21112 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21113
21114 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21115 grefx = gref % 3, nrefx = nref % 3;
21116 grefy = gref / 3, nrefy = nref / 3;
21117 if (xoff)
21118 xoff = font_height * (xoff - 128) / 256;
21119 if (yoff)
21120 yoff = font_height * (yoff - 128) / 256;
21121
21122 left = (leftmost
21123 + grefx * (rightmost - leftmost) / 2
21124 - nrefx * width / 2
21125 + xoff);
21126
21127 btm = ((grefy == 0 ? highest
21128 : grefy == 1 ? 0
21129 : grefy == 2 ? lowest
21130 : (highest + lowest) / 2)
21131 - (nrefy == 0 ? ascent + descent
21132 : nrefy == 1 ? descent - boff
21133 : nrefy == 2 ? 0
21134 : (ascent + descent) / 2)
21135 + yoff);
21136 }
21137
21138 cmp->offsets[i * 2] = left;
21139 cmp->offsets[i * 2 + 1] = btm + descent;
21140
21141 /* Update the bounding box of the overall glyphs. */
21142 if (width > 0)
21143 {
21144 right = left + width;
21145 if (left < leftmost)
21146 leftmost = left;
21147 if (right > rightmost)
21148 rightmost = right;
21149 }
21150 top = btm + descent + ascent;
21151 if (top > highest)
21152 highest = top;
21153 if (btm < lowest)
21154 lowest = btm;
21155
21156 if (cmp->lbearing > left + lbearing)
21157 cmp->lbearing = left + lbearing;
21158 if (cmp->rbearing < left + rbearing)
21159 cmp->rbearing = left + rbearing;
21160 }
21161 }
21162
21163 /* If there are glyphs whose x-offsets are negative,
21164 shift all glyphs to the right and make all x-offsets
21165 non-negative. */
21166 if (leftmost < 0)
21167 {
21168 for (i = 0; i < cmp->glyph_len; i++)
21169 cmp->offsets[i * 2] -= leftmost;
21170 rightmost -= leftmost;
21171 cmp->lbearing -= leftmost;
21172 cmp->rbearing -= leftmost;
21173 }
21174
21175 if (left_padded && cmp->lbearing < 0)
21176 {
21177 for (i = 0; i < cmp->glyph_len; i++)
21178 cmp->offsets[i * 2] -= cmp->lbearing;
21179 rightmost -= cmp->lbearing;
21180 cmp->rbearing -= cmp->lbearing;
21181 cmp->lbearing = 0;
21182 }
21183 if (right_padded && rightmost < cmp->rbearing)
21184 {
21185 rightmost = cmp->rbearing;
21186 }
21187
21188 cmp->pixel_width = rightmost;
21189 cmp->ascent = highest;
21190 cmp->descent = - lowest;
21191 if (cmp->ascent < font_ascent)
21192 cmp->ascent = font_ascent;
21193 if (cmp->descent < font_descent)
21194 cmp->descent = font_descent;
21195 }
21196
21197 if (it->glyph_row
21198 && (cmp->lbearing < 0
21199 || cmp->rbearing > cmp->pixel_width))
21200 it->glyph_row->contains_overlapping_glyphs_p = 1;
21201
21202 it->pixel_width = cmp->pixel_width;
21203 it->ascent = it->phys_ascent = cmp->ascent;
21204 it->descent = it->phys_descent = cmp->descent;
21205
21206 if (face->box != FACE_NO_BOX)
21207 {
21208 int thick = face->box_line_width;
21209
21210 if (thick > 0)
21211 {
21212 it->ascent += thick;
21213 it->descent += thick;
21214 }
21215 else
21216 thick = - thick;
21217
21218 if (it->start_of_box_run_p)
21219 it->pixel_width += thick;
21220 if (it->end_of_box_run_p)
21221 it->pixel_width += thick;
21222 }
21223
21224 /* If face has an overline, add the height of the overline
21225 (1 pixel) and a 1 pixel margin to the character height. */
21226 if (face->overline_p)
21227 it->ascent += overline_margin;
21228
21229 take_vertical_position_into_account (it);
21230
21231 if (it->glyph_row)
21232 append_composite_glyph (it);
21233 }
21234 else if (it->what == IT_IMAGE)
21235 produce_image_glyph (it);
21236 else if (it->what == IT_STRETCH)
21237 produce_stretch_glyph (it);
21238
21239 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21240 because this isn't true for images with `:ascent 100'. */
21241 xassert (it->ascent >= 0 && it->descent >= 0);
21242 if (it->area == TEXT_AREA)
21243 it->current_x += it->pixel_width;
21244
21245 if (extra_line_spacing > 0)
21246 {
21247 it->descent += extra_line_spacing;
21248 if (extra_line_spacing > it->max_extra_line_spacing)
21249 it->max_extra_line_spacing = extra_line_spacing;
21250 }
21251
21252 it->max_ascent = max (it->max_ascent, it->ascent);
21253 it->max_descent = max (it->max_descent, it->descent);
21254 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21255 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21256 }
21257
21258 /* EXPORT for RIF:
21259 Output LEN glyphs starting at START at the nominal cursor position.
21260 Advance the nominal cursor over the text. The global variable
21261 updated_window contains the window being updated, updated_row is
21262 the glyph row being updated, and updated_area is the area of that
21263 row being updated. */
21264
21265 void
21266 x_write_glyphs (start, len)
21267 struct glyph *start;
21268 int len;
21269 {
21270 int x, hpos;
21271
21272 xassert (updated_window && updated_row);
21273 BLOCK_INPUT;
21274
21275 /* Write glyphs. */
21276
21277 hpos = start - updated_row->glyphs[updated_area];
21278 x = draw_glyphs (updated_window, output_cursor.x,
21279 updated_row, updated_area,
21280 hpos, hpos + len,
21281 DRAW_NORMAL_TEXT, 0);
21282
21283 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21284 if (updated_area == TEXT_AREA
21285 && updated_window->phys_cursor_on_p
21286 && updated_window->phys_cursor.vpos == output_cursor.vpos
21287 && updated_window->phys_cursor.hpos >= hpos
21288 && updated_window->phys_cursor.hpos < hpos + len)
21289 updated_window->phys_cursor_on_p = 0;
21290
21291 UNBLOCK_INPUT;
21292
21293 /* Advance the output cursor. */
21294 output_cursor.hpos += len;
21295 output_cursor.x = x;
21296 }
21297
21298
21299 /* EXPORT for RIF:
21300 Insert LEN glyphs from START at the nominal cursor position. */
21301
21302 void
21303 x_insert_glyphs (start, len)
21304 struct glyph *start;
21305 int len;
21306 {
21307 struct frame *f;
21308 struct window *w;
21309 int line_height, shift_by_width, shifted_region_width;
21310 struct glyph_row *row;
21311 struct glyph *glyph;
21312 int frame_x, frame_y;
21313 EMACS_INT hpos;
21314
21315 xassert (updated_window && updated_row);
21316 BLOCK_INPUT;
21317 w = updated_window;
21318 f = XFRAME (WINDOW_FRAME (w));
21319
21320 /* Get the height of the line we are in. */
21321 row = updated_row;
21322 line_height = row->height;
21323
21324 /* Get the width of the glyphs to insert. */
21325 shift_by_width = 0;
21326 for (glyph = start; glyph < start + len; ++glyph)
21327 shift_by_width += glyph->pixel_width;
21328
21329 /* Get the width of the region to shift right. */
21330 shifted_region_width = (window_box_width (w, updated_area)
21331 - output_cursor.x
21332 - shift_by_width);
21333
21334 /* Shift right. */
21335 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21336 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21337
21338 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21339 line_height, shift_by_width);
21340
21341 /* Write the glyphs. */
21342 hpos = start - row->glyphs[updated_area];
21343 draw_glyphs (w, output_cursor.x, row, updated_area,
21344 hpos, hpos + len,
21345 DRAW_NORMAL_TEXT, 0);
21346
21347 /* Advance the output cursor. */
21348 output_cursor.hpos += len;
21349 output_cursor.x += shift_by_width;
21350 UNBLOCK_INPUT;
21351 }
21352
21353
21354 /* EXPORT for RIF:
21355 Erase the current text line from the nominal cursor position
21356 (inclusive) to pixel column TO_X (exclusive). The idea is that
21357 everything from TO_X onward is already erased.
21358
21359 TO_X is a pixel position relative to updated_area of
21360 updated_window. TO_X == -1 means clear to the end of this area. */
21361
21362 void
21363 x_clear_end_of_line (to_x)
21364 int to_x;
21365 {
21366 struct frame *f;
21367 struct window *w = updated_window;
21368 int max_x, min_y, max_y;
21369 int from_x, from_y, to_y;
21370
21371 xassert (updated_window && updated_row);
21372 f = XFRAME (w->frame);
21373
21374 if (updated_row->full_width_p)
21375 max_x = WINDOW_TOTAL_WIDTH (w);
21376 else
21377 max_x = window_box_width (w, updated_area);
21378 max_y = window_text_bottom_y (w);
21379
21380 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21381 of window. For TO_X > 0, truncate to end of drawing area. */
21382 if (to_x == 0)
21383 return;
21384 else if (to_x < 0)
21385 to_x = max_x;
21386 else
21387 to_x = min (to_x, max_x);
21388
21389 to_y = min (max_y, output_cursor.y + updated_row->height);
21390
21391 /* Notice if the cursor will be cleared by this operation. */
21392 if (!updated_row->full_width_p)
21393 notice_overwritten_cursor (w, updated_area,
21394 output_cursor.x, -1,
21395 updated_row->y,
21396 MATRIX_ROW_BOTTOM_Y (updated_row));
21397
21398 from_x = output_cursor.x;
21399
21400 /* Translate to frame coordinates. */
21401 if (updated_row->full_width_p)
21402 {
21403 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21404 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21405 }
21406 else
21407 {
21408 int area_left = window_box_left (w, updated_area);
21409 from_x += area_left;
21410 to_x += area_left;
21411 }
21412
21413 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21414 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21415 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21416
21417 /* Prevent inadvertently clearing to end of the X window. */
21418 if (to_x > from_x && to_y > from_y)
21419 {
21420 BLOCK_INPUT;
21421 rif->clear_frame_area (f, from_x, from_y,
21422 to_x - from_x, to_y - from_y);
21423 UNBLOCK_INPUT;
21424 }
21425 }
21426
21427 #endif /* HAVE_WINDOW_SYSTEM */
21428
21429
21430 \f
21431 /***********************************************************************
21432 Cursor types
21433 ***********************************************************************/
21434
21435 /* Value is the internal representation of the specified cursor type
21436 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21437 of the bar cursor. */
21438
21439 static enum text_cursor_kinds
21440 get_specified_cursor_type (arg, width)
21441 Lisp_Object arg;
21442 int *width;
21443 {
21444 enum text_cursor_kinds type;
21445
21446 if (NILP (arg))
21447 return NO_CURSOR;
21448
21449 if (EQ (arg, Qbox))
21450 return FILLED_BOX_CURSOR;
21451
21452 if (EQ (arg, Qhollow))
21453 return HOLLOW_BOX_CURSOR;
21454
21455 if (EQ (arg, Qbar))
21456 {
21457 *width = 2;
21458 return BAR_CURSOR;
21459 }
21460
21461 if (CONSP (arg)
21462 && EQ (XCAR (arg), Qbar)
21463 && INTEGERP (XCDR (arg))
21464 && XINT (XCDR (arg)) >= 0)
21465 {
21466 *width = XINT (XCDR (arg));
21467 return BAR_CURSOR;
21468 }
21469
21470 if (EQ (arg, Qhbar))
21471 {
21472 *width = 2;
21473 return HBAR_CURSOR;
21474 }
21475
21476 if (CONSP (arg)
21477 && EQ (XCAR (arg), Qhbar)
21478 && INTEGERP (XCDR (arg))
21479 && XINT (XCDR (arg)) >= 0)
21480 {
21481 *width = XINT (XCDR (arg));
21482 return HBAR_CURSOR;
21483 }
21484
21485 /* Treat anything unknown as "hollow box cursor".
21486 It was bad to signal an error; people have trouble fixing
21487 .Xdefaults with Emacs, when it has something bad in it. */
21488 type = HOLLOW_BOX_CURSOR;
21489
21490 return type;
21491 }
21492
21493 /* Set the default cursor types for specified frame. */
21494 void
21495 set_frame_cursor_types (f, arg)
21496 struct frame *f;
21497 Lisp_Object arg;
21498 {
21499 int width;
21500 Lisp_Object tem;
21501
21502 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21503 FRAME_CURSOR_WIDTH (f) = width;
21504
21505 /* By default, set up the blink-off state depending on the on-state. */
21506
21507 tem = Fassoc (arg, Vblink_cursor_alist);
21508 if (!NILP (tem))
21509 {
21510 FRAME_BLINK_OFF_CURSOR (f)
21511 = get_specified_cursor_type (XCDR (tem), &width);
21512 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21513 }
21514 else
21515 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21516 }
21517
21518
21519 /* Return the cursor we want to be displayed in window W. Return
21520 width of bar/hbar cursor through WIDTH arg. Return with
21521 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21522 (i.e. if the `system caret' should track this cursor).
21523
21524 In a mini-buffer window, we want the cursor only to appear if we
21525 are reading input from this window. For the selected window, we
21526 want the cursor type given by the frame parameter or buffer local
21527 setting of cursor-type. If explicitly marked off, draw no cursor.
21528 In all other cases, we want a hollow box cursor. */
21529
21530 static enum text_cursor_kinds
21531 get_window_cursor_type (w, glyph, width, active_cursor)
21532 struct window *w;
21533 struct glyph *glyph;
21534 int *width;
21535 int *active_cursor;
21536 {
21537 struct frame *f = XFRAME (w->frame);
21538 struct buffer *b = XBUFFER (w->buffer);
21539 int cursor_type = DEFAULT_CURSOR;
21540 Lisp_Object alt_cursor;
21541 int non_selected = 0;
21542
21543 *active_cursor = 1;
21544
21545 /* Echo area */
21546 if (cursor_in_echo_area
21547 && FRAME_HAS_MINIBUF_P (f)
21548 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21549 {
21550 if (w == XWINDOW (echo_area_window))
21551 {
21552 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21553 {
21554 *width = FRAME_CURSOR_WIDTH (f);
21555 return FRAME_DESIRED_CURSOR (f);
21556 }
21557 else
21558 return get_specified_cursor_type (b->cursor_type, width);
21559 }
21560
21561 *active_cursor = 0;
21562 non_selected = 1;
21563 }
21564
21565 /* Nonselected window or nonselected frame. */
21566 else if (w != XWINDOW (f->selected_window)
21567 #ifdef HAVE_WINDOW_SYSTEM
21568 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21569 #endif
21570 )
21571 {
21572 *active_cursor = 0;
21573
21574 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21575 return NO_CURSOR;
21576
21577 non_selected = 1;
21578 }
21579
21580 /* Never display a cursor in a window in which cursor-type is nil. */
21581 if (NILP (b->cursor_type))
21582 return NO_CURSOR;
21583
21584 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21585 if (non_selected)
21586 {
21587 alt_cursor = b->cursor_in_non_selected_windows;
21588 return get_specified_cursor_type (alt_cursor, width);
21589 }
21590
21591 /* Get the normal cursor type for this window. */
21592 if (EQ (b->cursor_type, Qt))
21593 {
21594 cursor_type = FRAME_DESIRED_CURSOR (f);
21595 *width = FRAME_CURSOR_WIDTH (f);
21596 }
21597 else
21598 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21599
21600 /* Use normal cursor if not blinked off. */
21601 if (!w->cursor_off_p)
21602 {
21603 #ifdef HAVE_WINDOW_SYSTEM
21604 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21605 {
21606 if (cursor_type == FILLED_BOX_CURSOR)
21607 {
21608 /* Using a block cursor on large images can be very annoying.
21609 So use a hollow cursor for "large" images.
21610 If image is not transparent (no mask), also use hollow cursor. */
21611 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21612 if (img != NULL && IMAGEP (img->spec))
21613 {
21614 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21615 where N = size of default frame font size.
21616 This should cover most of the "tiny" icons people may use. */
21617 if (!img->mask
21618 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21619 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21620 cursor_type = HOLLOW_BOX_CURSOR;
21621 }
21622 }
21623 else if (cursor_type != NO_CURSOR)
21624 {
21625 /* Display current only supports BOX and HOLLOW cursors for images.
21626 So for now, unconditionally use a HOLLOW cursor when cursor is
21627 not a solid box cursor. */
21628 cursor_type = HOLLOW_BOX_CURSOR;
21629 }
21630 }
21631 #endif
21632 return cursor_type;
21633 }
21634
21635 /* Cursor is blinked off, so determine how to "toggle" it. */
21636
21637 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21638 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21639 return get_specified_cursor_type (XCDR (alt_cursor), width);
21640
21641 /* Then see if frame has specified a specific blink off cursor type. */
21642 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21643 {
21644 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21645 return FRAME_BLINK_OFF_CURSOR (f);
21646 }
21647
21648 #if 0
21649 /* Some people liked having a permanently visible blinking cursor,
21650 while others had very strong opinions against it. So it was
21651 decided to remove it. KFS 2003-09-03 */
21652
21653 /* Finally perform built-in cursor blinking:
21654 filled box <-> hollow box
21655 wide [h]bar <-> narrow [h]bar
21656 narrow [h]bar <-> no cursor
21657 other type <-> no cursor */
21658
21659 if (cursor_type == FILLED_BOX_CURSOR)
21660 return HOLLOW_BOX_CURSOR;
21661
21662 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21663 {
21664 *width = 1;
21665 return cursor_type;
21666 }
21667 #endif
21668
21669 return NO_CURSOR;
21670 }
21671
21672
21673 #ifdef HAVE_WINDOW_SYSTEM
21674
21675 /* Notice when the text cursor of window W has been completely
21676 overwritten by a drawing operation that outputs glyphs in AREA
21677 starting at X0 and ending at X1 in the line starting at Y0 and
21678 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21679 the rest of the line after X0 has been written. Y coordinates
21680 are window-relative. */
21681
21682 static void
21683 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21684 struct window *w;
21685 enum glyph_row_area area;
21686 int x0, y0, x1, y1;
21687 {
21688 int cx0, cx1, cy0, cy1;
21689 struct glyph_row *row;
21690
21691 if (!w->phys_cursor_on_p)
21692 return;
21693 if (area != TEXT_AREA)
21694 return;
21695
21696 if (w->phys_cursor.vpos < 0
21697 || w->phys_cursor.vpos >= w->current_matrix->nrows
21698 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21699 !(row->enabled_p && row->displays_text_p)))
21700 return;
21701
21702 if (row->cursor_in_fringe_p)
21703 {
21704 row->cursor_in_fringe_p = 0;
21705 draw_fringe_bitmap (w, row, 0);
21706 w->phys_cursor_on_p = 0;
21707 return;
21708 }
21709
21710 cx0 = w->phys_cursor.x;
21711 cx1 = cx0 + w->phys_cursor_width;
21712 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21713 return;
21714
21715 /* The cursor image will be completely removed from the
21716 screen if the output area intersects the cursor area in
21717 y-direction. When we draw in [y0 y1[, and some part of
21718 the cursor is at y < y0, that part must have been drawn
21719 before. When scrolling, the cursor is erased before
21720 actually scrolling, so we don't come here. When not
21721 scrolling, the rows above the old cursor row must have
21722 changed, and in this case these rows must have written
21723 over the cursor image.
21724
21725 Likewise if part of the cursor is below y1, with the
21726 exception of the cursor being in the first blank row at
21727 the buffer and window end because update_text_area
21728 doesn't draw that row. (Except when it does, but
21729 that's handled in update_text_area.) */
21730
21731 cy0 = w->phys_cursor.y;
21732 cy1 = cy0 + w->phys_cursor_height;
21733 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21734 return;
21735
21736 w->phys_cursor_on_p = 0;
21737 }
21738
21739 #endif /* HAVE_WINDOW_SYSTEM */
21740
21741 \f
21742 /************************************************************************
21743 Mouse Face
21744 ************************************************************************/
21745
21746 #ifdef HAVE_WINDOW_SYSTEM
21747
21748 /* EXPORT for RIF:
21749 Fix the display of area AREA of overlapping row ROW in window W
21750 with respect to the overlapping part OVERLAPS. */
21751
21752 void
21753 x_fix_overlapping_area (w, row, area, overlaps)
21754 struct window *w;
21755 struct glyph_row *row;
21756 enum glyph_row_area area;
21757 int overlaps;
21758 {
21759 int i, x;
21760
21761 BLOCK_INPUT;
21762
21763 x = 0;
21764 for (i = 0; i < row->used[area];)
21765 {
21766 if (row->glyphs[area][i].overlaps_vertically_p)
21767 {
21768 int start = i, start_x = x;
21769
21770 do
21771 {
21772 x += row->glyphs[area][i].pixel_width;
21773 ++i;
21774 }
21775 while (i < row->used[area]
21776 && row->glyphs[area][i].overlaps_vertically_p);
21777
21778 draw_glyphs (w, start_x, row, area,
21779 start, i,
21780 DRAW_NORMAL_TEXT, overlaps);
21781 }
21782 else
21783 {
21784 x += row->glyphs[area][i].pixel_width;
21785 ++i;
21786 }
21787 }
21788
21789 UNBLOCK_INPUT;
21790 }
21791
21792
21793 /* EXPORT:
21794 Draw the cursor glyph of window W in glyph row ROW. See the
21795 comment of draw_glyphs for the meaning of HL. */
21796
21797 void
21798 draw_phys_cursor_glyph (w, row, hl)
21799 struct window *w;
21800 struct glyph_row *row;
21801 enum draw_glyphs_face hl;
21802 {
21803 /* If cursor hpos is out of bounds, don't draw garbage. This can
21804 happen in mini-buffer windows when switching between echo area
21805 glyphs and mini-buffer. */
21806 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21807 {
21808 int on_p = w->phys_cursor_on_p;
21809 int x1;
21810 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21811 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21812 hl, 0);
21813 w->phys_cursor_on_p = on_p;
21814
21815 if (hl == DRAW_CURSOR)
21816 w->phys_cursor_width = x1 - w->phys_cursor.x;
21817 /* When we erase the cursor, and ROW is overlapped by other
21818 rows, make sure that these overlapping parts of other rows
21819 are redrawn. */
21820 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21821 {
21822 w->phys_cursor_width = x1 - w->phys_cursor.x;
21823
21824 if (row > w->current_matrix->rows
21825 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21826 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21827 OVERLAPS_ERASED_CURSOR);
21828
21829 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21830 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21831 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21832 OVERLAPS_ERASED_CURSOR);
21833 }
21834 }
21835 }
21836
21837
21838 /* EXPORT:
21839 Erase the image of a cursor of window W from the screen. */
21840
21841 void
21842 erase_phys_cursor (w)
21843 struct window *w;
21844 {
21845 struct frame *f = XFRAME (w->frame);
21846 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21847 int hpos = w->phys_cursor.hpos;
21848 int vpos = w->phys_cursor.vpos;
21849 int mouse_face_here_p = 0;
21850 struct glyph_matrix *active_glyphs = w->current_matrix;
21851 struct glyph_row *cursor_row;
21852 struct glyph *cursor_glyph;
21853 enum draw_glyphs_face hl;
21854
21855 /* No cursor displayed or row invalidated => nothing to do on the
21856 screen. */
21857 if (w->phys_cursor_type == NO_CURSOR)
21858 goto mark_cursor_off;
21859
21860 /* VPOS >= active_glyphs->nrows means that window has been resized.
21861 Don't bother to erase the cursor. */
21862 if (vpos >= active_glyphs->nrows)
21863 goto mark_cursor_off;
21864
21865 /* If row containing cursor is marked invalid, there is nothing we
21866 can do. */
21867 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21868 if (!cursor_row->enabled_p)
21869 goto mark_cursor_off;
21870
21871 /* If line spacing is > 0, old cursor may only be partially visible in
21872 window after split-window. So adjust visible height. */
21873 cursor_row->visible_height = min (cursor_row->visible_height,
21874 window_text_bottom_y (w) - cursor_row->y);
21875
21876 /* If row is completely invisible, don't attempt to delete a cursor which
21877 isn't there. This can happen if cursor is at top of a window, and
21878 we switch to a buffer with a header line in that window. */
21879 if (cursor_row->visible_height <= 0)
21880 goto mark_cursor_off;
21881
21882 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21883 if (cursor_row->cursor_in_fringe_p)
21884 {
21885 cursor_row->cursor_in_fringe_p = 0;
21886 draw_fringe_bitmap (w, cursor_row, 0);
21887 goto mark_cursor_off;
21888 }
21889
21890 /* This can happen when the new row is shorter than the old one.
21891 In this case, either draw_glyphs or clear_end_of_line
21892 should have cleared the cursor. Note that we wouldn't be
21893 able to erase the cursor in this case because we don't have a
21894 cursor glyph at hand. */
21895 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21896 goto mark_cursor_off;
21897
21898 /* If the cursor is in the mouse face area, redisplay that when
21899 we clear the cursor. */
21900 if (! NILP (dpyinfo->mouse_face_window)
21901 && w == XWINDOW (dpyinfo->mouse_face_window)
21902 && (vpos > dpyinfo->mouse_face_beg_row
21903 || (vpos == dpyinfo->mouse_face_beg_row
21904 && hpos >= dpyinfo->mouse_face_beg_col))
21905 && (vpos < dpyinfo->mouse_face_end_row
21906 || (vpos == dpyinfo->mouse_face_end_row
21907 && hpos < dpyinfo->mouse_face_end_col))
21908 /* Don't redraw the cursor's spot in mouse face if it is at the
21909 end of a line (on a newline). The cursor appears there, but
21910 mouse highlighting does not. */
21911 && cursor_row->used[TEXT_AREA] > hpos)
21912 mouse_face_here_p = 1;
21913
21914 /* Maybe clear the display under the cursor. */
21915 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21916 {
21917 int x, y, left_x;
21918 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21919 int width;
21920
21921 cursor_glyph = get_phys_cursor_glyph (w);
21922 if (cursor_glyph == NULL)
21923 goto mark_cursor_off;
21924
21925 width = cursor_glyph->pixel_width;
21926 left_x = window_box_left_offset (w, TEXT_AREA);
21927 x = w->phys_cursor.x;
21928 if (x < left_x)
21929 width -= left_x - x;
21930 width = min (width, window_box_width (w, TEXT_AREA) - x);
21931 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21932 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21933
21934 if (width > 0)
21935 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21936 }
21937
21938 /* Erase the cursor by redrawing the character underneath it. */
21939 if (mouse_face_here_p)
21940 hl = DRAW_MOUSE_FACE;
21941 else
21942 hl = DRAW_NORMAL_TEXT;
21943 draw_phys_cursor_glyph (w, cursor_row, hl);
21944
21945 mark_cursor_off:
21946 w->phys_cursor_on_p = 0;
21947 w->phys_cursor_type = NO_CURSOR;
21948 }
21949
21950
21951 /* EXPORT:
21952 Display or clear cursor of window W. If ON is zero, clear the
21953 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21954 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21955
21956 void
21957 display_and_set_cursor (w, on, hpos, vpos, x, y)
21958 struct window *w;
21959 int on, hpos, vpos, x, y;
21960 {
21961 struct frame *f = XFRAME (w->frame);
21962 int new_cursor_type;
21963 int new_cursor_width;
21964 int active_cursor;
21965 struct glyph_row *glyph_row;
21966 struct glyph *glyph;
21967
21968 /* This is pointless on invisible frames, and dangerous on garbaged
21969 windows and frames; in the latter case, the frame or window may
21970 be in the midst of changing its size, and x and y may be off the
21971 window. */
21972 if (! FRAME_VISIBLE_P (f)
21973 || FRAME_GARBAGED_P (f)
21974 || vpos >= w->current_matrix->nrows
21975 || hpos >= w->current_matrix->matrix_w)
21976 return;
21977
21978 /* If cursor is off and we want it off, return quickly. */
21979 if (!on && !w->phys_cursor_on_p)
21980 return;
21981
21982 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21983 /* If cursor row is not enabled, we don't really know where to
21984 display the cursor. */
21985 if (!glyph_row->enabled_p)
21986 {
21987 w->phys_cursor_on_p = 0;
21988 return;
21989 }
21990
21991 glyph = NULL;
21992 if (!glyph_row->exact_window_width_line_p
21993 || hpos < glyph_row->used[TEXT_AREA])
21994 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21995
21996 xassert (interrupt_input_blocked);
21997
21998 /* Set new_cursor_type to the cursor we want to be displayed. */
21999 new_cursor_type = get_window_cursor_type (w, glyph,
22000 &new_cursor_width, &active_cursor);
22001
22002 /* If cursor is currently being shown and we don't want it to be or
22003 it is in the wrong place, or the cursor type is not what we want,
22004 erase it. */
22005 if (w->phys_cursor_on_p
22006 && (!on
22007 || w->phys_cursor.x != x
22008 || w->phys_cursor.y != y
22009 || new_cursor_type != w->phys_cursor_type
22010 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22011 && new_cursor_width != w->phys_cursor_width)))
22012 erase_phys_cursor (w);
22013
22014 /* Don't check phys_cursor_on_p here because that flag is only set
22015 to zero in some cases where we know that the cursor has been
22016 completely erased, to avoid the extra work of erasing the cursor
22017 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22018 still not be visible, or it has only been partly erased. */
22019 if (on)
22020 {
22021 w->phys_cursor_ascent = glyph_row->ascent;
22022 w->phys_cursor_height = glyph_row->height;
22023
22024 /* Set phys_cursor_.* before x_draw_.* is called because some
22025 of them may need the information. */
22026 w->phys_cursor.x = x;
22027 w->phys_cursor.y = glyph_row->y;
22028 w->phys_cursor.hpos = hpos;
22029 w->phys_cursor.vpos = vpos;
22030 }
22031
22032 rif->draw_window_cursor (w, glyph_row, x, y,
22033 new_cursor_type, new_cursor_width,
22034 on, active_cursor);
22035 }
22036
22037
22038 /* Switch the display of W's cursor on or off, according to the value
22039 of ON. */
22040
22041 static void
22042 update_window_cursor (w, on)
22043 struct window *w;
22044 int on;
22045 {
22046 /* Don't update cursor in windows whose frame is in the process
22047 of being deleted. */
22048 if (w->current_matrix)
22049 {
22050 BLOCK_INPUT;
22051 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22052 w->phys_cursor.x, w->phys_cursor.y);
22053 UNBLOCK_INPUT;
22054 }
22055 }
22056
22057
22058 /* Call update_window_cursor with parameter ON_P on all leaf windows
22059 in the window tree rooted at W. */
22060
22061 static void
22062 update_cursor_in_window_tree (w, on_p)
22063 struct window *w;
22064 int on_p;
22065 {
22066 while (w)
22067 {
22068 if (!NILP (w->hchild))
22069 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22070 else if (!NILP (w->vchild))
22071 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22072 else
22073 update_window_cursor (w, on_p);
22074
22075 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22076 }
22077 }
22078
22079
22080 /* EXPORT:
22081 Display the cursor on window W, or clear it, according to ON_P.
22082 Don't change the cursor's position. */
22083
22084 void
22085 x_update_cursor (f, on_p)
22086 struct frame *f;
22087 int on_p;
22088 {
22089 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22090 }
22091
22092
22093 /* EXPORT:
22094 Clear the cursor of window W to background color, and mark the
22095 cursor as not shown. This is used when the text where the cursor
22096 is is about to be rewritten. */
22097
22098 void
22099 x_clear_cursor (w)
22100 struct window *w;
22101 {
22102 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22103 update_window_cursor (w, 0);
22104 }
22105
22106
22107 /* EXPORT:
22108 Display the active region described by mouse_face_* according to DRAW. */
22109
22110 void
22111 show_mouse_face (dpyinfo, draw)
22112 Display_Info *dpyinfo;
22113 enum draw_glyphs_face draw;
22114 {
22115 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22116 struct frame *f = XFRAME (WINDOW_FRAME (w));
22117
22118 if (/* If window is in the process of being destroyed, don't bother
22119 to do anything. */
22120 w->current_matrix != NULL
22121 /* Don't update mouse highlight if hidden */
22122 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22123 /* Recognize when we are called to operate on rows that don't exist
22124 anymore. This can happen when a window is split. */
22125 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22126 {
22127 int phys_cursor_on_p = w->phys_cursor_on_p;
22128 struct glyph_row *row, *first, *last;
22129
22130 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22131 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22132
22133 for (row = first; row <= last && row->enabled_p; ++row)
22134 {
22135 int start_hpos, end_hpos, start_x;
22136
22137 /* For all but the first row, the highlight starts at column 0. */
22138 if (row == first)
22139 {
22140 start_hpos = dpyinfo->mouse_face_beg_col;
22141 start_x = dpyinfo->mouse_face_beg_x;
22142 }
22143 else
22144 {
22145 start_hpos = 0;
22146 start_x = 0;
22147 }
22148
22149 if (row == last)
22150 end_hpos = dpyinfo->mouse_face_end_col;
22151 else
22152 {
22153 end_hpos = row->used[TEXT_AREA];
22154 if (draw == DRAW_NORMAL_TEXT)
22155 row->fill_line_p = 1; /* Clear to end of line */
22156 }
22157
22158 if (end_hpos > start_hpos)
22159 {
22160 draw_glyphs (w, start_x, row, TEXT_AREA,
22161 start_hpos, end_hpos,
22162 draw, 0);
22163
22164 row->mouse_face_p
22165 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22166 }
22167 }
22168
22169 /* When we've written over the cursor, arrange for it to
22170 be displayed again. */
22171 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22172 {
22173 BLOCK_INPUT;
22174 display_and_set_cursor (w, 1,
22175 w->phys_cursor.hpos, w->phys_cursor.vpos,
22176 w->phys_cursor.x, w->phys_cursor.y);
22177 UNBLOCK_INPUT;
22178 }
22179 }
22180
22181 /* Change the mouse cursor. */
22182 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22183 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22184 else if (draw == DRAW_MOUSE_FACE)
22185 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22186 else
22187 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22188 }
22189
22190 /* EXPORT:
22191 Clear out the mouse-highlighted active region.
22192 Redraw it un-highlighted first. Value is non-zero if mouse
22193 face was actually drawn unhighlighted. */
22194
22195 int
22196 clear_mouse_face (dpyinfo)
22197 Display_Info *dpyinfo;
22198 {
22199 int cleared = 0;
22200
22201 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22202 {
22203 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22204 cleared = 1;
22205 }
22206
22207 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22208 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22209 dpyinfo->mouse_face_window = Qnil;
22210 dpyinfo->mouse_face_overlay = Qnil;
22211 return cleared;
22212 }
22213
22214
22215 /* EXPORT:
22216 Non-zero if physical cursor of window W is within mouse face. */
22217
22218 int
22219 cursor_in_mouse_face_p (w)
22220 struct window *w;
22221 {
22222 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22223 int in_mouse_face = 0;
22224
22225 if (WINDOWP (dpyinfo->mouse_face_window)
22226 && XWINDOW (dpyinfo->mouse_face_window) == w)
22227 {
22228 int hpos = w->phys_cursor.hpos;
22229 int vpos = w->phys_cursor.vpos;
22230
22231 if (vpos >= dpyinfo->mouse_face_beg_row
22232 && vpos <= dpyinfo->mouse_face_end_row
22233 && (vpos > dpyinfo->mouse_face_beg_row
22234 || hpos >= dpyinfo->mouse_face_beg_col)
22235 && (vpos < dpyinfo->mouse_face_end_row
22236 || hpos < dpyinfo->mouse_face_end_col
22237 || dpyinfo->mouse_face_past_end))
22238 in_mouse_face = 1;
22239 }
22240
22241 return in_mouse_face;
22242 }
22243
22244
22245
22246 \f
22247 /* Find the glyph matrix position of buffer position CHARPOS in window
22248 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22249 current glyphs must be up to date. If CHARPOS is above window
22250 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22251 of last line in W. In the row containing CHARPOS, stop before glyphs
22252 having STOP as object. */
22253
22254 #if 1 /* This is a version of fast_find_position that's more correct
22255 in the presence of hscrolling, for example. I didn't install
22256 it right away because the problem fixed is minor, it failed
22257 in 20.x as well, and I think it's too risky to install
22258 so near the release of 21.1. 2001-09-25 gerd. */
22259
22260 static int
22261 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22262 struct window *w;
22263 EMACS_INT charpos;
22264 int *hpos, *vpos, *x, *y;
22265 Lisp_Object stop;
22266 {
22267 struct glyph_row *row, *first;
22268 struct glyph *glyph, *end;
22269 int past_end = 0;
22270
22271 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22272 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22273 {
22274 *x = first->x;
22275 *y = first->y;
22276 *hpos = 0;
22277 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22278 return 1;
22279 }
22280
22281 row = row_containing_pos (w, charpos, first, NULL, 0);
22282 if (row == NULL)
22283 {
22284 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22285 past_end = 1;
22286 }
22287
22288 /* If whole rows or last part of a row came from a display overlay,
22289 row_containing_pos will skip over such rows because their end pos
22290 equals the start pos of the overlay or interval.
22291
22292 Move back if we have a STOP object and previous row's
22293 end glyph came from STOP. */
22294 if (!NILP (stop))
22295 {
22296 struct glyph_row *prev;
22297 while ((prev = row - 1, prev >= first)
22298 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22299 && prev->used[TEXT_AREA] > 0)
22300 {
22301 struct glyph *beg = prev->glyphs[TEXT_AREA];
22302 glyph = beg + prev->used[TEXT_AREA];
22303 while (--glyph >= beg
22304 && INTEGERP (glyph->object));
22305 if (glyph < beg
22306 || !EQ (stop, glyph->object))
22307 break;
22308 row = prev;
22309 }
22310 }
22311
22312 *x = row->x;
22313 *y = row->y;
22314 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22315
22316 glyph = row->glyphs[TEXT_AREA];
22317 end = glyph + row->used[TEXT_AREA];
22318
22319 /* Skip over glyphs not having an object at the start of the row.
22320 These are special glyphs like truncation marks on terminal
22321 frames. */
22322 if (row->displays_text_p)
22323 while (glyph < end
22324 && INTEGERP (glyph->object)
22325 && !EQ (stop, glyph->object)
22326 && glyph->charpos < 0)
22327 {
22328 *x += glyph->pixel_width;
22329 ++glyph;
22330 }
22331
22332 while (glyph < end
22333 && !INTEGERP (glyph->object)
22334 && !EQ (stop, glyph->object)
22335 && (!BUFFERP (glyph->object)
22336 || glyph->charpos < charpos))
22337 {
22338 *x += glyph->pixel_width;
22339 ++glyph;
22340 }
22341
22342 *hpos = glyph - row->glyphs[TEXT_AREA];
22343 return !past_end;
22344 }
22345
22346 #else /* not 1 */
22347
22348 static int
22349 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22350 struct window *w;
22351 EMACS_INT pos;
22352 int *hpos, *vpos, *x, *y;
22353 Lisp_Object stop;
22354 {
22355 int i;
22356 int lastcol;
22357 int maybe_next_line_p = 0;
22358 int line_start_position;
22359 int yb = window_text_bottom_y (w);
22360 struct glyph_row *row, *best_row;
22361 int row_vpos, best_row_vpos;
22362 int current_x;
22363
22364 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22365 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22366
22367 while (row->y < yb)
22368 {
22369 if (row->used[TEXT_AREA])
22370 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22371 else
22372 line_start_position = 0;
22373
22374 if (line_start_position > pos)
22375 break;
22376 /* If the position sought is the end of the buffer,
22377 don't include the blank lines at the bottom of the window. */
22378 else if (line_start_position == pos
22379 && pos == BUF_ZV (XBUFFER (w->buffer)))
22380 {
22381 maybe_next_line_p = 1;
22382 break;
22383 }
22384 else if (line_start_position > 0)
22385 {
22386 best_row = row;
22387 best_row_vpos = row_vpos;
22388 }
22389
22390 if (row->y + row->height >= yb)
22391 break;
22392
22393 ++row;
22394 ++row_vpos;
22395 }
22396
22397 /* Find the right column within BEST_ROW. */
22398 lastcol = 0;
22399 current_x = best_row->x;
22400 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22401 {
22402 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22403 int charpos = glyph->charpos;
22404
22405 if (BUFFERP (glyph->object))
22406 {
22407 if (charpos == pos)
22408 {
22409 *hpos = i;
22410 *vpos = best_row_vpos;
22411 *x = current_x;
22412 *y = best_row->y;
22413 return 1;
22414 }
22415 else if (charpos > pos)
22416 break;
22417 }
22418 else if (EQ (glyph->object, stop))
22419 break;
22420
22421 if (charpos > 0)
22422 lastcol = i;
22423 current_x += glyph->pixel_width;
22424 }
22425
22426 /* If we're looking for the end of the buffer,
22427 and we didn't find it in the line we scanned,
22428 use the start of the following line. */
22429 if (maybe_next_line_p)
22430 {
22431 ++best_row;
22432 ++best_row_vpos;
22433 lastcol = 0;
22434 current_x = best_row->x;
22435 }
22436
22437 *vpos = best_row_vpos;
22438 *hpos = lastcol + 1;
22439 *x = current_x;
22440 *y = best_row->y;
22441 return 0;
22442 }
22443
22444 #endif /* not 1 */
22445
22446
22447 /* Find the position of the glyph for position POS in OBJECT in
22448 window W's current matrix, and return in *X, *Y the pixel
22449 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22450
22451 RIGHT_P non-zero means return the position of the right edge of the
22452 glyph, RIGHT_P zero means return the left edge position.
22453
22454 If no glyph for POS exists in the matrix, return the position of
22455 the glyph with the next smaller position that is in the matrix, if
22456 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22457 exists in the matrix, return the position of the glyph with the
22458 next larger position in OBJECT.
22459
22460 Value is non-zero if a glyph was found. */
22461
22462 static int
22463 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22464 struct window *w;
22465 EMACS_INT pos;
22466 Lisp_Object object;
22467 int *hpos, *vpos, *x, *y;
22468 int right_p;
22469 {
22470 int yb = window_text_bottom_y (w);
22471 struct glyph_row *r;
22472 struct glyph *best_glyph = NULL;
22473 struct glyph_row *best_row = NULL;
22474 int best_x = 0;
22475
22476 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22477 r->enabled_p && r->y < yb;
22478 ++r)
22479 {
22480 struct glyph *g = r->glyphs[TEXT_AREA];
22481 struct glyph *e = g + r->used[TEXT_AREA];
22482 int gx;
22483
22484 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22485 if (EQ (g->object, object))
22486 {
22487 if (g->charpos == pos)
22488 {
22489 best_glyph = g;
22490 best_x = gx;
22491 best_row = r;
22492 goto found;
22493 }
22494 else if (best_glyph == NULL
22495 || ((abs (g->charpos - pos)
22496 < abs (best_glyph->charpos - pos))
22497 && (right_p
22498 ? g->charpos < pos
22499 : g->charpos > pos)))
22500 {
22501 best_glyph = g;
22502 best_x = gx;
22503 best_row = r;
22504 }
22505 }
22506 }
22507
22508 found:
22509
22510 if (best_glyph)
22511 {
22512 *x = best_x;
22513 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22514
22515 if (right_p)
22516 {
22517 *x += best_glyph->pixel_width;
22518 ++*hpos;
22519 }
22520
22521 *y = best_row->y;
22522 *vpos = best_row - w->current_matrix->rows;
22523 }
22524
22525 return best_glyph != NULL;
22526 }
22527
22528
22529 /* See if position X, Y is within a hot-spot of an image. */
22530
22531 static int
22532 on_hot_spot_p (hot_spot, x, y)
22533 Lisp_Object hot_spot;
22534 int x, y;
22535 {
22536 if (!CONSP (hot_spot))
22537 return 0;
22538
22539 if (EQ (XCAR (hot_spot), Qrect))
22540 {
22541 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22542 Lisp_Object rect = XCDR (hot_spot);
22543 Lisp_Object tem;
22544 if (!CONSP (rect))
22545 return 0;
22546 if (!CONSP (XCAR (rect)))
22547 return 0;
22548 if (!CONSP (XCDR (rect)))
22549 return 0;
22550 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22551 return 0;
22552 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22553 return 0;
22554 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22555 return 0;
22556 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22557 return 0;
22558 return 1;
22559 }
22560 else if (EQ (XCAR (hot_spot), Qcircle))
22561 {
22562 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22563 Lisp_Object circ = XCDR (hot_spot);
22564 Lisp_Object lr, lx0, ly0;
22565 if (CONSP (circ)
22566 && CONSP (XCAR (circ))
22567 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22568 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22569 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22570 {
22571 double r = XFLOATINT (lr);
22572 double dx = XINT (lx0) - x;
22573 double dy = XINT (ly0) - y;
22574 return (dx * dx + dy * dy <= r * r);
22575 }
22576 }
22577 else if (EQ (XCAR (hot_spot), Qpoly))
22578 {
22579 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22580 if (VECTORP (XCDR (hot_spot)))
22581 {
22582 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22583 Lisp_Object *poly = v->contents;
22584 int n = v->size;
22585 int i;
22586 int inside = 0;
22587 Lisp_Object lx, ly;
22588 int x0, y0;
22589
22590 /* Need an even number of coordinates, and at least 3 edges. */
22591 if (n < 6 || n & 1)
22592 return 0;
22593
22594 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22595 If count is odd, we are inside polygon. Pixels on edges
22596 may or may not be included depending on actual geometry of the
22597 polygon. */
22598 if ((lx = poly[n-2], !INTEGERP (lx))
22599 || (ly = poly[n-1], !INTEGERP (lx)))
22600 return 0;
22601 x0 = XINT (lx), y0 = XINT (ly);
22602 for (i = 0; i < n; i += 2)
22603 {
22604 int x1 = x0, y1 = y0;
22605 if ((lx = poly[i], !INTEGERP (lx))
22606 || (ly = poly[i+1], !INTEGERP (ly)))
22607 return 0;
22608 x0 = XINT (lx), y0 = XINT (ly);
22609
22610 /* Does this segment cross the X line? */
22611 if (x0 >= x)
22612 {
22613 if (x1 >= x)
22614 continue;
22615 }
22616 else if (x1 < x)
22617 continue;
22618 if (y > y0 && y > y1)
22619 continue;
22620 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22621 inside = !inside;
22622 }
22623 return inside;
22624 }
22625 }
22626 /* If we don't understand the format, pretend we're not in the hot-spot. */
22627 return 0;
22628 }
22629
22630 Lisp_Object
22631 find_hot_spot (map, x, y)
22632 Lisp_Object map;
22633 int x, y;
22634 {
22635 while (CONSP (map))
22636 {
22637 if (CONSP (XCAR (map))
22638 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22639 return XCAR (map);
22640 map = XCDR (map);
22641 }
22642
22643 return Qnil;
22644 }
22645
22646 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22647 3, 3, 0,
22648 doc: /* Lookup in image map MAP coordinates X and Y.
22649 An image map is an alist where each element has the format (AREA ID PLIST).
22650 An AREA is specified as either a rectangle, a circle, or a polygon:
22651 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22652 pixel coordinates of the upper left and bottom right corners.
22653 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22654 and the radius of the circle; r may be a float or integer.
22655 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22656 vector describes one corner in the polygon.
22657 Returns the alist element for the first matching AREA in MAP. */)
22658 (map, x, y)
22659 Lisp_Object map;
22660 Lisp_Object x, y;
22661 {
22662 if (NILP (map))
22663 return Qnil;
22664
22665 CHECK_NUMBER (x);
22666 CHECK_NUMBER (y);
22667
22668 return find_hot_spot (map, XINT (x), XINT (y));
22669 }
22670
22671
22672 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22673 static void
22674 define_frame_cursor1 (f, cursor, pointer)
22675 struct frame *f;
22676 Cursor cursor;
22677 Lisp_Object pointer;
22678 {
22679 /* Do not change cursor shape while dragging mouse. */
22680 if (!NILP (do_mouse_tracking))
22681 return;
22682
22683 if (!NILP (pointer))
22684 {
22685 if (EQ (pointer, Qarrow))
22686 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22687 else if (EQ (pointer, Qhand))
22688 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22689 else if (EQ (pointer, Qtext))
22690 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22691 else if (EQ (pointer, intern ("hdrag")))
22692 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22693 #ifdef HAVE_X_WINDOWS
22694 else if (EQ (pointer, intern ("vdrag")))
22695 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22696 #endif
22697 else if (EQ (pointer, intern ("hourglass")))
22698 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22699 else if (EQ (pointer, Qmodeline))
22700 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22701 else
22702 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22703 }
22704
22705 if (cursor != No_Cursor)
22706 rif->define_frame_cursor (f, cursor);
22707 }
22708
22709 /* Take proper action when mouse has moved to the mode or header line
22710 or marginal area AREA of window W, x-position X and y-position Y.
22711 X is relative to the start of the text display area of W, so the
22712 width of bitmap areas and scroll bars must be subtracted to get a
22713 position relative to the start of the mode line. */
22714
22715 static void
22716 note_mode_line_or_margin_highlight (window, x, y, area)
22717 Lisp_Object window;
22718 int x, y;
22719 enum window_part area;
22720 {
22721 struct window *w = XWINDOW (window);
22722 struct frame *f = XFRAME (w->frame);
22723 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22724 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22725 Lisp_Object pointer = Qnil;
22726 int charpos, dx, dy, width, height;
22727 Lisp_Object string, object = Qnil;
22728 Lisp_Object pos, help;
22729
22730 Lisp_Object mouse_face;
22731 int original_x_pixel = x;
22732 struct glyph * glyph = NULL;
22733 struct glyph_row *row;
22734
22735 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22736 {
22737 int x0;
22738 struct glyph *end;
22739
22740 string = mode_line_string (w, area, &x, &y, &charpos,
22741 &object, &dx, &dy, &width, &height);
22742
22743 row = (area == ON_MODE_LINE
22744 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22745 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22746
22747 /* Find glyph */
22748 if (row->mode_line_p && row->enabled_p)
22749 {
22750 glyph = row->glyphs[TEXT_AREA];
22751 end = glyph + row->used[TEXT_AREA];
22752
22753 for (x0 = original_x_pixel;
22754 glyph < end && x0 >= glyph->pixel_width;
22755 ++glyph)
22756 x0 -= glyph->pixel_width;
22757
22758 if (glyph >= end)
22759 glyph = NULL;
22760 }
22761 }
22762 else
22763 {
22764 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22765 string = marginal_area_string (w, area, &x, &y, &charpos,
22766 &object, &dx, &dy, &width, &height);
22767 }
22768
22769 help = Qnil;
22770
22771 if (IMAGEP (object))
22772 {
22773 Lisp_Object image_map, hotspot;
22774 if ((image_map = Fplist_get (XCDR (object), QCmap),
22775 !NILP (image_map))
22776 && (hotspot = find_hot_spot (image_map, dx, dy),
22777 CONSP (hotspot))
22778 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22779 {
22780 Lisp_Object area_id, plist;
22781
22782 area_id = XCAR (hotspot);
22783 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22784 If so, we could look for mouse-enter, mouse-leave
22785 properties in PLIST (and do something...). */
22786 hotspot = XCDR (hotspot);
22787 if (CONSP (hotspot)
22788 && (plist = XCAR (hotspot), CONSP (plist)))
22789 {
22790 pointer = Fplist_get (plist, Qpointer);
22791 if (NILP (pointer))
22792 pointer = Qhand;
22793 help = Fplist_get (plist, Qhelp_echo);
22794 if (!NILP (help))
22795 {
22796 help_echo_string = help;
22797 /* Is this correct? ++kfs */
22798 XSETWINDOW (help_echo_window, w);
22799 help_echo_object = w->buffer;
22800 help_echo_pos = charpos;
22801 }
22802 }
22803 }
22804 if (NILP (pointer))
22805 pointer = Fplist_get (XCDR (object), QCpointer);
22806 }
22807
22808 if (STRINGP (string))
22809 {
22810 pos = make_number (charpos);
22811 /* If we're on a string with `help-echo' text property, arrange
22812 for the help to be displayed. This is done by setting the
22813 global variable help_echo_string to the help string. */
22814 if (NILP (help))
22815 {
22816 help = Fget_text_property (pos, Qhelp_echo, string);
22817 if (!NILP (help))
22818 {
22819 help_echo_string = help;
22820 XSETWINDOW (help_echo_window, w);
22821 help_echo_object = string;
22822 help_echo_pos = charpos;
22823 }
22824 }
22825
22826 if (NILP (pointer))
22827 pointer = Fget_text_property (pos, Qpointer, string);
22828
22829 /* Change the mouse pointer according to what is under X/Y. */
22830 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22831 {
22832 Lisp_Object map;
22833 map = Fget_text_property (pos, Qlocal_map, string);
22834 if (!KEYMAPP (map))
22835 map = Fget_text_property (pos, Qkeymap, string);
22836 if (!KEYMAPP (map))
22837 cursor = dpyinfo->vertical_scroll_bar_cursor;
22838 }
22839
22840 /* Change the mouse face according to what is under X/Y. */
22841 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22842 if (!NILP (mouse_face)
22843 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22844 && glyph)
22845 {
22846 Lisp_Object b, e;
22847
22848 struct glyph * tmp_glyph;
22849
22850 int gpos;
22851 int gseq_length;
22852 int total_pixel_width;
22853 int ignore;
22854
22855 int vpos, hpos;
22856
22857 b = Fprevious_single_property_change (make_number (charpos + 1),
22858 Qmouse_face, string, Qnil);
22859 if (NILP (b))
22860 b = make_number (0);
22861
22862 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22863 if (NILP (e))
22864 e = make_number (SCHARS (string));
22865
22866 /* Calculate the position(glyph position: GPOS) of GLYPH in
22867 displayed string. GPOS is different from CHARPOS.
22868
22869 CHARPOS is the position of glyph in internal string
22870 object. A mode line string format has structures which
22871 is converted to a flatten by emacs lisp interpreter.
22872 The internal string is an element of the structures.
22873 The displayed string is the flatten string. */
22874 for (tmp_glyph = glyph - 1, gpos = 0;
22875 tmp_glyph->charpos >= XINT (b);
22876 tmp_glyph--, gpos++)
22877 {
22878 if (!EQ (tmp_glyph->object, glyph->object))
22879 break;
22880 }
22881
22882 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22883 displayed string holding GLYPH.
22884
22885 GSEQ_LENGTH is different from SCHARS (STRING).
22886 SCHARS (STRING) returns the length of the internal string. */
22887 for (tmp_glyph = glyph, gseq_length = gpos;
22888 tmp_glyph->charpos < XINT (e);
22889 tmp_glyph++, gseq_length++)
22890 {
22891 if (!EQ (tmp_glyph->object, glyph->object))
22892 break;
22893 }
22894
22895 total_pixel_width = 0;
22896 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22897 total_pixel_width += tmp_glyph->pixel_width;
22898
22899 /* Pre calculation of re-rendering position */
22900 vpos = (x - gpos);
22901 hpos = (area == ON_MODE_LINE
22902 ? (w->current_matrix)->nrows - 1
22903 : 0);
22904
22905 /* If the re-rendering position is included in the last
22906 re-rendering area, we should do nothing. */
22907 if ( EQ (window, dpyinfo->mouse_face_window)
22908 && dpyinfo->mouse_face_beg_col <= vpos
22909 && vpos < dpyinfo->mouse_face_end_col
22910 && dpyinfo->mouse_face_beg_row == hpos )
22911 return;
22912
22913 if (clear_mouse_face (dpyinfo))
22914 cursor = No_Cursor;
22915
22916 dpyinfo->mouse_face_beg_col = vpos;
22917 dpyinfo->mouse_face_beg_row = hpos;
22918
22919 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22920 dpyinfo->mouse_face_beg_y = 0;
22921
22922 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22923 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22924
22925 dpyinfo->mouse_face_end_x = 0;
22926 dpyinfo->mouse_face_end_y = 0;
22927
22928 dpyinfo->mouse_face_past_end = 0;
22929 dpyinfo->mouse_face_window = window;
22930
22931 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22932 charpos,
22933 0, 0, 0, &ignore,
22934 glyph->face_id, 1);
22935 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22936
22937 if (NILP (pointer))
22938 pointer = Qhand;
22939 }
22940 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22941 clear_mouse_face (dpyinfo);
22942 }
22943 define_frame_cursor1 (f, cursor, pointer);
22944 }
22945
22946
22947 /* EXPORT:
22948 Take proper action when the mouse has moved to position X, Y on
22949 frame F as regards highlighting characters that have mouse-face
22950 properties. Also de-highlighting chars where the mouse was before.
22951 X and Y can be negative or out of range. */
22952
22953 void
22954 note_mouse_highlight (f, x, y)
22955 struct frame *f;
22956 int x, y;
22957 {
22958 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22959 enum window_part part;
22960 Lisp_Object window;
22961 struct window *w;
22962 Cursor cursor = No_Cursor;
22963 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22964 struct buffer *b;
22965
22966 /* When a menu is active, don't highlight because this looks odd. */
22967 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22968 if (popup_activated ())
22969 return;
22970 #endif
22971
22972 if (NILP (Vmouse_highlight)
22973 || !f->glyphs_initialized_p)
22974 return;
22975
22976 dpyinfo->mouse_face_mouse_x = x;
22977 dpyinfo->mouse_face_mouse_y = y;
22978 dpyinfo->mouse_face_mouse_frame = f;
22979
22980 if (dpyinfo->mouse_face_defer)
22981 return;
22982
22983 if (gc_in_progress)
22984 {
22985 dpyinfo->mouse_face_deferred_gc = 1;
22986 return;
22987 }
22988
22989 /* Which window is that in? */
22990 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22991
22992 /* If we were displaying active text in another window, clear that.
22993 Also clear if we move out of text area in same window. */
22994 if (! EQ (window, dpyinfo->mouse_face_window)
22995 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22996 && !NILP (dpyinfo->mouse_face_window)))
22997 clear_mouse_face (dpyinfo);
22998
22999 /* Not on a window -> return. */
23000 if (!WINDOWP (window))
23001 return;
23002
23003 /* Reset help_echo_string. It will get recomputed below. */
23004 help_echo_string = Qnil;
23005
23006 /* Convert to window-relative pixel coordinates. */
23007 w = XWINDOW (window);
23008 frame_to_window_pixel_xy (w, &x, &y);
23009
23010 /* Handle tool-bar window differently since it doesn't display a
23011 buffer. */
23012 if (EQ (window, f->tool_bar_window))
23013 {
23014 note_tool_bar_highlight (f, x, y);
23015 return;
23016 }
23017
23018 /* Mouse is on the mode, header line or margin? */
23019 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23020 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23021 {
23022 note_mode_line_or_margin_highlight (window, x, y, part);
23023 return;
23024 }
23025
23026 if (part == ON_VERTICAL_BORDER)
23027 {
23028 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23029 help_echo_string = build_string ("drag-mouse-1: resize");
23030 }
23031 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23032 || part == ON_SCROLL_BAR)
23033 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23034 else
23035 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23036
23037 /* Are we in a window whose display is up to date?
23038 And verify the buffer's text has not changed. */
23039 b = XBUFFER (w->buffer);
23040 if (part == ON_TEXT
23041 && EQ (w->window_end_valid, w->buffer)
23042 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23043 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23044 {
23045 int hpos, vpos, pos, i, dx, dy, area;
23046 struct glyph *glyph;
23047 Lisp_Object object;
23048 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23049 Lisp_Object *overlay_vec = NULL;
23050 int noverlays;
23051 struct buffer *obuf;
23052 int obegv, ozv, same_region;
23053
23054 /* Find the glyph under X/Y. */
23055 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23056
23057 /* Look for :pointer property on image. */
23058 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23059 {
23060 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23061 if (img != NULL && IMAGEP (img->spec))
23062 {
23063 Lisp_Object image_map, hotspot;
23064 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23065 !NILP (image_map))
23066 && (hotspot = find_hot_spot (image_map,
23067 glyph->slice.x + dx,
23068 glyph->slice.y + dy),
23069 CONSP (hotspot))
23070 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23071 {
23072 Lisp_Object area_id, plist;
23073
23074 area_id = XCAR (hotspot);
23075 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23076 If so, we could look for mouse-enter, mouse-leave
23077 properties in PLIST (and do something...). */
23078 hotspot = XCDR (hotspot);
23079 if (CONSP (hotspot)
23080 && (plist = XCAR (hotspot), CONSP (plist)))
23081 {
23082 pointer = Fplist_get (plist, Qpointer);
23083 if (NILP (pointer))
23084 pointer = Qhand;
23085 help_echo_string = Fplist_get (plist, Qhelp_echo);
23086 if (!NILP (help_echo_string))
23087 {
23088 help_echo_window = window;
23089 help_echo_object = glyph->object;
23090 help_echo_pos = glyph->charpos;
23091 }
23092 }
23093 }
23094 if (NILP (pointer))
23095 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23096 }
23097 }
23098
23099 /* Clear mouse face if X/Y not over text. */
23100 if (glyph == NULL
23101 || area != TEXT_AREA
23102 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23103 {
23104 if (clear_mouse_face (dpyinfo))
23105 cursor = No_Cursor;
23106 if (NILP (pointer))
23107 {
23108 if (area != TEXT_AREA)
23109 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23110 else
23111 pointer = Vvoid_text_area_pointer;
23112 }
23113 goto set_cursor;
23114 }
23115
23116 pos = glyph->charpos;
23117 object = glyph->object;
23118 if (!STRINGP (object) && !BUFFERP (object))
23119 goto set_cursor;
23120
23121 /* If we get an out-of-range value, return now; avoid an error. */
23122 if (BUFFERP (object) && pos > BUF_Z (b))
23123 goto set_cursor;
23124
23125 /* Make the window's buffer temporarily current for
23126 overlays_at and compute_char_face. */
23127 obuf = current_buffer;
23128 current_buffer = b;
23129 obegv = BEGV;
23130 ozv = ZV;
23131 BEGV = BEG;
23132 ZV = Z;
23133
23134 /* Is this char mouse-active or does it have help-echo? */
23135 position = make_number (pos);
23136
23137 if (BUFFERP (object))
23138 {
23139 /* Put all the overlays we want in a vector in overlay_vec. */
23140 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23141 /* Sort overlays into increasing priority order. */
23142 noverlays = sort_overlays (overlay_vec, noverlays, w);
23143 }
23144 else
23145 noverlays = 0;
23146
23147 same_region = (EQ (window, dpyinfo->mouse_face_window)
23148 && vpos >= dpyinfo->mouse_face_beg_row
23149 && vpos <= dpyinfo->mouse_face_end_row
23150 && (vpos > dpyinfo->mouse_face_beg_row
23151 || hpos >= dpyinfo->mouse_face_beg_col)
23152 && (vpos < dpyinfo->mouse_face_end_row
23153 || hpos < dpyinfo->mouse_face_end_col
23154 || dpyinfo->mouse_face_past_end));
23155
23156 if (same_region)
23157 cursor = No_Cursor;
23158
23159 /* Check mouse-face highlighting. */
23160 if (! same_region
23161 /* If there exists an overlay with mouse-face overlapping
23162 the one we are currently highlighting, we have to
23163 check if we enter the overlapping overlay, and then
23164 highlight only that. */
23165 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23166 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23167 {
23168 /* Find the highest priority overlay that has a mouse-face
23169 property. */
23170 overlay = Qnil;
23171 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23172 {
23173 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23174 if (!NILP (mouse_face))
23175 overlay = overlay_vec[i];
23176 }
23177
23178 /* If we're actually highlighting the same overlay as
23179 before, there's no need to do that again. */
23180 if (!NILP (overlay)
23181 && EQ (overlay, dpyinfo->mouse_face_overlay))
23182 goto check_help_echo;
23183
23184 dpyinfo->mouse_face_overlay = overlay;
23185
23186 /* Clear the display of the old active region, if any. */
23187 if (clear_mouse_face (dpyinfo))
23188 cursor = No_Cursor;
23189
23190 /* If no overlay applies, get a text property. */
23191 if (NILP (overlay))
23192 mouse_face = Fget_text_property (position, Qmouse_face, object);
23193
23194 /* Handle the overlay case. */
23195 if (!NILP (overlay))
23196 {
23197 /* Find the range of text around this char that
23198 should be active. */
23199 Lisp_Object before, after;
23200 int ignore;
23201
23202 before = Foverlay_start (overlay);
23203 after = Foverlay_end (overlay);
23204 /* Record this as the current active region. */
23205 fast_find_position (w, XFASTINT (before),
23206 &dpyinfo->mouse_face_beg_col,
23207 &dpyinfo->mouse_face_beg_row,
23208 &dpyinfo->mouse_face_beg_x,
23209 &dpyinfo->mouse_face_beg_y, Qnil);
23210
23211 dpyinfo->mouse_face_past_end
23212 = !fast_find_position (w, XFASTINT (after),
23213 &dpyinfo->mouse_face_end_col,
23214 &dpyinfo->mouse_face_end_row,
23215 &dpyinfo->mouse_face_end_x,
23216 &dpyinfo->mouse_face_end_y, Qnil);
23217 dpyinfo->mouse_face_window = window;
23218
23219 dpyinfo->mouse_face_face_id
23220 = face_at_buffer_position (w, pos, 0, 0,
23221 &ignore, pos + 1,
23222 !dpyinfo->mouse_face_hidden);
23223
23224 /* Display it as active. */
23225 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23226 cursor = No_Cursor;
23227 }
23228 /* Handle the text property case. */
23229 else if (!NILP (mouse_face) && BUFFERP (object))
23230 {
23231 /* Find the range of text around this char that
23232 should be active. */
23233 Lisp_Object before, after, beginning, end;
23234 int ignore;
23235
23236 beginning = Fmarker_position (w->start);
23237 end = make_number (BUF_Z (XBUFFER (object))
23238 - XFASTINT (w->window_end_pos));
23239 before
23240 = Fprevious_single_property_change (make_number (pos + 1),
23241 Qmouse_face,
23242 object, beginning);
23243 after
23244 = Fnext_single_property_change (position, Qmouse_face,
23245 object, end);
23246
23247 /* Record this as the current active region. */
23248 fast_find_position (w, XFASTINT (before),
23249 &dpyinfo->mouse_face_beg_col,
23250 &dpyinfo->mouse_face_beg_row,
23251 &dpyinfo->mouse_face_beg_x,
23252 &dpyinfo->mouse_face_beg_y, Qnil);
23253 dpyinfo->mouse_face_past_end
23254 = !fast_find_position (w, XFASTINT (after),
23255 &dpyinfo->mouse_face_end_col,
23256 &dpyinfo->mouse_face_end_row,
23257 &dpyinfo->mouse_face_end_x,
23258 &dpyinfo->mouse_face_end_y, Qnil);
23259 dpyinfo->mouse_face_window = window;
23260
23261 if (BUFFERP (object))
23262 dpyinfo->mouse_face_face_id
23263 = face_at_buffer_position (w, pos, 0, 0,
23264 &ignore, pos + 1,
23265 !dpyinfo->mouse_face_hidden);
23266
23267 /* Display it as active. */
23268 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23269 cursor = No_Cursor;
23270 }
23271 else if (!NILP (mouse_face) && STRINGP (object))
23272 {
23273 Lisp_Object b, e;
23274 int ignore;
23275
23276 b = Fprevious_single_property_change (make_number (pos + 1),
23277 Qmouse_face,
23278 object, Qnil);
23279 e = Fnext_single_property_change (position, Qmouse_face,
23280 object, Qnil);
23281 if (NILP (b))
23282 b = make_number (0);
23283 if (NILP (e))
23284 e = make_number (SCHARS (object) - 1);
23285
23286 fast_find_string_pos (w, XINT (b), object,
23287 &dpyinfo->mouse_face_beg_col,
23288 &dpyinfo->mouse_face_beg_row,
23289 &dpyinfo->mouse_face_beg_x,
23290 &dpyinfo->mouse_face_beg_y, 0);
23291 fast_find_string_pos (w, XINT (e), object,
23292 &dpyinfo->mouse_face_end_col,
23293 &dpyinfo->mouse_face_end_row,
23294 &dpyinfo->mouse_face_end_x,
23295 &dpyinfo->mouse_face_end_y, 1);
23296 dpyinfo->mouse_face_past_end = 0;
23297 dpyinfo->mouse_face_window = window;
23298 dpyinfo->mouse_face_face_id
23299 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23300 glyph->face_id, 1);
23301 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23302 cursor = No_Cursor;
23303 }
23304 else if (STRINGP (object) && NILP (mouse_face))
23305 {
23306 /* A string which doesn't have mouse-face, but
23307 the text ``under'' it might have. */
23308 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23309 int start = MATRIX_ROW_START_CHARPOS (r);
23310
23311 pos = string_buffer_position (w, object, start);
23312 if (pos > 0)
23313 mouse_face = get_char_property_and_overlay (make_number (pos),
23314 Qmouse_face,
23315 w->buffer,
23316 &overlay);
23317 if (!NILP (mouse_face) && !NILP (overlay))
23318 {
23319 Lisp_Object before = Foverlay_start (overlay);
23320 Lisp_Object after = Foverlay_end (overlay);
23321 int ignore;
23322
23323 /* Note that we might not be able to find position
23324 BEFORE in the glyph matrix if the overlay is
23325 entirely covered by a `display' property. In
23326 this case, we overshoot. So let's stop in
23327 the glyph matrix before glyphs for OBJECT. */
23328 fast_find_position (w, XFASTINT (before),
23329 &dpyinfo->mouse_face_beg_col,
23330 &dpyinfo->mouse_face_beg_row,
23331 &dpyinfo->mouse_face_beg_x,
23332 &dpyinfo->mouse_face_beg_y,
23333 object);
23334
23335 dpyinfo->mouse_face_past_end
23336 = !fast_find_position (w, XFASTINT (after),
23337 &dpyinfo->mouse_face_end_col,
23338 &dpyinfo->mouse_face_end_row,
23339 &dpyinfo->mouse_face_end_x,
23340 &dpyinfo->mouse_face_end_y,
23341 Qnil);
23342 dpyinfo->mouse_face_window = window;
23343 dpyinfo->mouse_face_face_id
23344 = face_at_buffer_position (w, pos, 0, 0,
23345 &ignore, pos + 1,
23346 !dpyinfo->mouse_face_hidden);
23347
23348 /* Display it as active. */
23349 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23350 cursor = No_Cursor;
23351 }
23352 }
23353 }
23354
23355 check_help_echo:
23356
23357 /* Look for a `help-echo' property. */
23358 if (NILP (help_echo_string)) {
23359 Lisp_Object help, overlay;
23360
23361 /* Check overlays first. */
23362 help = overlay = Qnil;
23363 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23364 {
23365 overlay = overlay_vec[i];
23366 help = Foverlay_get (overlay, Qhelp_echo);
23367 }
23368
23369 if (!NILP (help))
23370 {
23371 help_echo_string = help;
23372 help_echo_window = window;
23373 help_echo_object = overlay;
23374 help_echo_pos = pos;
23375 }
23376 else
23377 {
23378 Lisp_Object object = glyph->object;
23379 int charpos = glyph->charpos;
23380
23381 /* Try text properties. */
23382 if (STRINGP (object)
23383 && charpos >= 0
23384 && charpos < SCHARS (object))
23385 {
23386 help = Fget_text_property (make_number (charpos),
23387 Qhelp_echo, object);
23388 if (NILP (help))
23389 {
23390 /* If the string itself doesn't specify a help-echo,
23391 see if the buffer text ``under'' it does. */
23392 struct glyph_row *r
23393 = MATRIX_ROW (w->current_matrix, vpos);
23394 int start = MATRIX_ROW_START_CHARPOS (r);
23395 int pos = string_buffer_position (w, object, start);
23396 if (pos > 0)
23397 {
23398 help = Fget_char_property (make_number (pos),
23399 Qhelp_echo, w->buffer);
23400 if (!NILP (help))
23401 {
23402 charpos = pos;
23403 object = w->buffer;
23404 }
23405 }
23406 }
23407 }
23408 else if (BUFFERP (object)
23409 && charpos >= BEGV
23410 && charpos < ZV)
23411 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23412 object);
23413
23414 if (!NILP (help))
23415 {
23416 help_echo_string = help;
23417 help_echo_window = window;
23418 help_echo_object = object;
23419 help_echo_pos = charpos;
23420 }
23421 }
23422 }
23423
23424 /* Look for a `pointer' property. */
23425 if (NILP (pointer))
23426 {
23427 /* Check overlays first. */
23428 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23429 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23430
23431 if (NILP (pointer))
23432 {
23433 Lisp_Object object = glyph->object;
23434 int charpos = glyph->charpos;
23435
23436 /* Try text properties. */
23437 if (STRINGP (object)
23438 && charpos >= 0
23439 && charpos < SCHARS (object))
23440 {
23441 pointer = Fget_text_property (make_number (charpos),
23442 Qpointer, object);
23443 if (NILP (pointer))
23444 {
23445 /* If the string itself doesn't specify a pointer,
23446 see if the buffer text ``under'' it does. */
23447 struct glyph_row *r
23448 = MATRIX_ROW (w->current_matrix, vpos);
23449 int start = MATRIX_ROW_START_CHARPOS (r);
23450 int pos = string_buffer_position (w, object, start);
23451 if (pos > 0)
23452 pointer = Fget_char_property (make_number (pos),
23453 Qpointer, w->buffer);
23454 }
23455 }
23456 else if (BUFFERP (object)
23457 && charpos >= BEGV
23458 && charpos < ZV)
23459 pointer = Fget_text_property (make_number (charpos),
23460 Qpointer, object);
23461 }
23462 }
23463
23464 BEGV = obegv;
23465 ZV = ozv;
23466 current_buffer = obuf;
23467 }
23468
23469 set_cursor:
23470
23471 define_frame_cursor1 (f, cursor, pointer);
23472 }
23473
23474
23475 /* EXPORT for RIF:
23476 Clear any mouse-face on window W. This function is part of the
23477 redisplay interface, and is called from try_window_id and similar
23478 functions to ensure the mouse-highlight is off. */
23479
23480 void
23481 x_clear_window_mouse_face (w)
23482 struct window *w;
23483 {
23484 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23485 Lisp_Object window;
23486
23487 BLOCK_INPUT;
23488 XSETWINDOW (window, w);
23489 if (EQ (window, dpyinfo->mouse_face_window))
23490 clear_mouse_face (dpyinfo);
23491 UNBLOCK_INPUT;
23492 }
23493
23494
23495 /* EXPORT:
23496 Just discard the mouse face information for frame F, if any.
23497 This is used when the size of F is changed. */
23498
23499 void
23500 cancel_mouse_face (f)
23501 struct frame *f;
23502 {
23503 Lisp_Object window;
23504 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23505
23506 window = dpyinfo->mouse_face_window;
23507 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23508 {
23509 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23510 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23511 dpyinfo->mouse_face_window = Qnil;
23512 }
23513 }
23514
23515
23516 #endif /* HAVE_WINDOW_SYSTEM */
23517
23518 \f
23519 /***********************************************************************
23520 Exposure Events
23521 ***********************************************************************/
23522
23523 #ifdef HAVE_WINDOW_SYSTEM
23524
23525 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23526 which intersects rectangle R. R is in window-relative coordinates. */
23527
23528 static void
23529 expose_area (w, row, r, area)
23530 struct window *w;
23531 struct glyph_row *row;
23532 XRectangle *r;
23533 enum glyph_row_area area;
23534 {
23535 struct glyph *first = row->glyphs[area];
23536 struct glyph *end = row->glyphs[area] + row->used[area];
23537 struct glyph *last;
23538 int first_x, start_x, x;
23539
23540 if (area == TEXT_AREA && row->fill_line_p)
23541 /* If row extends face to end of line write the whole line. */
23542 draw_glyphs (w, 0, row, area,
23543 0, row->used[area],
23544 DRAW_NORMAL_TEXT, 0);
23545 else
23546 {
23547 /* Set START_X to the window-relative start position for drawing glyphs of
23548 AREA. The first glyph of the text area can be partially visible.
23549 The first glyphs of other areas cannot. */
23550 start_x = window_box_left_offset (w, area);
23551 x = start_x;
23552 if (area == TEXT_AREA)
23553 x += row->x;
23554
23555 /* Find the first glyph that must be redrawn. */
23556 while (first < end
23557 && x + first->pixel_width < r->x)
23558 {
23559 x += first->pixel_width;
23560 ++first;
23561 }
23562
23563 /* Find the last one. */
23564 last = first;
23565 first_x = x;
23566 while (last < end
23567 && x < r->x + r->width)
23568 {
23569 x += last->pixel_width;
23570 ++last;
23571 }
23572
23573 /* Repaint. */
23574 if (last > first)
23575 draw_glyphs (w, first_x - start_x, row, area,
23576 first - row->glyphs[area], last - row->glyphs[area],
23577 DRAW_NORMAL_TEXT, 0);
23578 }
23579 }
23580
23581
23582 /* Redraw the parts of the glyph row ROW on window W intersecting
23583 rectangle R. R is in window-relative coordinates. Value is
23584 non-zero if mouse-face was overwritten. */
23585
23586 static int
23587 expose_line (w, row, r)
23588 struct window *w;
23589 struct glyph_row *row;
23590 XRectangle *r;
23591 {
23592 xassert (row->enabled_p);
23593
23594 if (row->mode_line_p || w->pseudo_window_p)
23595 draw_glyphs (w, 0, row, TEXT_AREA,
23596 0, row->used[TEXT_AREA],
23597 DRAW_NORMAL_TEXT, 0);
23598 else
23599 {
23600 if (row->used[LEFT_MARGIN_AREA])
23601 expose_area (w, row, r, LEFT_MARGIN_AREA);
23602 if (row->used[TEXT_AREA])
23603 expose_area (w, row, r, TEXT_AREA);
23604 if (row->used[RIGHT_MARGIN_AREA])
23605 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23606 draw_row_fringe_bitmaps (w, row);
23607 }
23608
23609 return row->mouse_face_p;
23610 }
23611
23612
23613 /* Redraw those parts of glyphs rows during expose event handling that
23614 overlap other rows. Redrawing of an exposed line writes over parts
23615 of lines overlapping that exposed line; this function fixes that.
23616
23617 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23618 row in W's current matrix that is exposed and overlaps other rows.
23619 LAST_OVERLAPPING_ROW is the last such row. */
23620
23621 static void
23622 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23623 struct window *w;
23624 struct glyph_row *first_overlapping_row;
23625 struct glyph_row *last_overlapping_row;
23626 {
23627 struct glyph_row *row;
23628
23629 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23630 if (row->overlapping_p)
23631 {
23632 xassert (row->enabled_p && !row->mode_line_p);
23633
23634 if (row->used[LEFT_MARGIN_AREA])
23635 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23636
23637 if (row->used[TEXT_AREA])
23638 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23639
23640 if (row->used[RIGHT_MARGIN_AREA])
23641 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23642 }
23643 }
23644
23645
23646 /* Return non-zero if W's cursor intersects rectangle R. */
23647
23648 static int
23649 phys_cursor_in_rect_p (w, r)
23650 struct window *w;
23651 XRectangle *r;
23652 {
23653 XRectangle cr, result;
23654 struct glyph *cursor_glyph;
23655
23656 cursor_glyph = get_phys_cursor_glyph (w);
23657 if (cursor_glyph)
23658 {
23659 /* r is relative to W's box, but w->phys_cursor.x is relative
23660 to left edge of W's TEXT area. Adjust it. */
23661 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23662 cr.y = w->phys_cursor.y;
23663 cr.width = cursor_glyph->pixel_width;
23664 cr.height = w->phys_cursor_height;
23665 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23666 I assume the effect is the same -- and this is portable. */
23667 return x_intersect_rectangles (&cr, r, &result);
23668 }
23669 else
23670 return 0;
23671 }
23672
23673
23674 /* EXPORT:
23675 Draw a vertical window border to the right of window W if W doesn't
23676 have vertical scroll bars. */
23677
23678 void
23679 x_draw_vertical_border (w)
23680 struct window *w;
23681 {
23682 /* We could do better, if we knew what type of scroll-bar the adjacent
23683 windows (on either side) have... But we don't :-(
23684 However, I think this works ok. ++KFS 2003-04-25 */
23685
23686 /* Redraw borders between horizontally adjacent windows. Don't
23687 do it for frames with vertical scroll bars because either the
23688 right scroll bar of a window, or the left scroll bar of its
23689 neighbor will suffice as a border. */
23690 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23691 return;
23692
23693 if (!WINDOW_RIGHTMOST_P (w)
23694 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23695 {
23696 int x0, x1, y0, y1;
23697
23698 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23699 y1 -= 1;
23700
23701 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23702 x1 -= 1;
23703
23704 rif->draw_vertical_window_border (w, x1, y0, y1);
23705 }
23706 else if (!WINDOW_LEFTMOST_P (w)
23707 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23708 {
23709 int x0, x1, y0, y1;
23710
23711 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23712 y1 -= 1;
23713
23714 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23715 x0 -= 1;
23716
23717 rif->draw_vertical_window_border (w, x0, y0, y1);
23718 }
23719 }
23720
23721
23722 /* Redraw the part of window W intersection rectangle FR. Pixel
23723 coordinates in FR are frame-relative. Call this function with
23724 input blocked. Value is non-zero if the exposure overwrites
23725 mouse-face. */
23726
23727 static int
23728 expose_window (w, fr)
23729 struct window *w;
23730 XRectangle *fr;
23731 {
23732 struct frame *f = XFRAME (w->frame);
23733 XRectangle wr, r;
23734 int mouse_face_overwritten_p = 0;
23735
23736 /* If window is not yet fully initialized, do nothing. This can
23737 happen when toolkit scroll bars are used and a window is split.
23738 Reconfiguring the scroll bar will generate an expose for a newly
23739 created window. */
23740 if (w->current_matrix == NULL)
23741 return 0;
23742
23743 /* When we're currently updating the window, display and current
23744 matrix usually don't agree. Arrange for a thorough display
23745 later. */
23746 if (w == updated_window)
23747 {
23748 SET_FRAME_GARBAGED (f);
23749 return 0;
23750 }
23751
23752 /* Frame-relative pixel rectangle of W. */
23753 wr.x = WINDOW_LEFT_EDGE_X (w);
23754 wr.y = WINDOW_TOP_EDGE_Y (w);
23755 wr.width = WINDOW_TOTAL_WIDTH (w);
23756 wr.height = WINDOW_TOTAL_HEIGHT (w);
23757
23758 if (x_intersect_rectangles (fr, &wr, &r))
23759 {
23760 int yb = window_text_bottom_y (w);
23761 struct glyph_row *row;
23762 int cursor_cleared_p;
23763 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23764
23765 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23766 r.x, r.y, r.width, r.height));
23767
23768 /* Convert to window coordinates. */
23769 r.x -= WINDOW_LEFT_EDGE_X (w);
23770 r.y -= WINDOW_TOP_EDGE_Y (w);
23771
23772 /* Turn off the cursor. */
23773 if (!w->pseudo_window_p
23774 && phys_cursor_in_rect_p (w, &r))
23775 {
23776 x_clear_cursor (w);
23777 cursor_cleared_p = 1;
23778 }
23779 else
23780 cursor_cleared_p = 0;
23781
23782 /* Update lines intersecting rectangle R. */
23783 first_overlapping_row = last_overlapping_row = NULL;
23784 for (row = w->current_matrix->rows;
23785 row->enabled_p;
23786 ++row)
23787 {
23788 int y0 = row->y;
23789 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23790
23791 if ((y0 >= r.y && y0 < r.y + r.height)
23792 || (y1 > r.y && y1 < r.y + r.height)
23793 || (r.y >= y0 && r.y < y1)
23794 || (r.y + r.height > y0 && r.y + r.height < y1))
23795 {
23796 /* A header line may be overlapping, but there is no need
23797 to fix overlapping areas for them. KFS 2005-02-12 */
23798 if (row->overlapping_p && !row->mode_line_p)
23799 {
23800 if (first_overlapping_row == NULL)
23801 first_overlapping_row = row;
23802 last_overlapping_row = row;
23803 }
23804
23805 if (expose_line (w, row, &r))
23806 mouse_face_overwritten_p = 1;
23807 }
23808
23809 if (y1 >= yb)
23810 break;
23811 }
23812
23813 /* Display the mode line if there is one. */
23814 if (WINDOW_WANTS_MODELINE_P (w)
23815 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23816 row->enabled_p)
23817 && row->y < r.y + r.height)
23818 {
23819 if (expose_line (w, row, &r))
23820 mouse_face_overwritten_p = 1;
23821 }
23822
23823 if (!w->pseudo_window_p)
23824 {
23825 /* Fix the display of overlapping rows. */
23826 if (first_overlapping_row)
23827 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23828
23829 /* Draw border between windows. */
23830 x_draw_vertical_border (w);
23831
23832 /* Turn the cursor on again. */
23833 if (cursor_cleared_p)
23834 update_window_cursor (w, 1);
23835 }
23836 }
23837
23838 return mouse_face_overwritten_p;
23839 }
23840
23841
23842
23843 /* Redraw (parts) of all windows in the window tree rooted at W that
23844 intersect R. R contains frame pixel coordinates. Value is
23845 non-zero if the exposure overwrites mouse-face. */
23846
23847 static int
23848 expose_window_tree (w, r)
23849 struct window *w;
23850 XRectangle *r;
23851 {
23852 struct frame *f = XFRAME (w->frame);
23853 int mouse_face_overwritten_p = 0;
23854
23855 while (w && !FRAME_GARBAGED_P (f))
23856 {
23857 if (!NILP (w->hchild))
23858 mouse_face_overwritten_p
23859 |= expose_window_tree (XWINDOW (w->hchild), r);
23860 else if (!NILP (w->vchild))
23861 mouse_face_overwritten_p
23862 |= expose_window_tree (XWINDOW (w->vchild), r);
23863 else
23864 mouse_face_overwritten_p |= expose_window (w, r);
23865
23866 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23867 }
23868
23869 return mouse_face_overwritten_p;
23870 }
23871
23872
23873 /* EXPORT:
23874 Redisplay an exposed area of frame F. X and Y are the upper-left
23875 corner of the exposed rectangle. W and H are width and height of
23876 the exposed area. All are pixel values. W or H zero means redraw
23877 the entire frame. */
23878
23879 void
23880 expose_frame (f, x, y, w, h)
23881 struct frame *f;
23882 int x, y, w, h;
23883 {
23884 XRectangle r;
23885 int mouse_face_overwritten_p = 0;
23886
23887 TRACE ((stderr, "expose_frame "));
23888
23889 /* No need to redraw if frame will be redrawn soon. */
23890 if (FRAME_GARBAGED_P (f))
23891 {
23892 TRACE ((stderr, " garbaged\n"));
23893 return;
23894 }
23895
23896 /* If basic faces haven't been realized yet, there is no point in
23897 trying to redraw anything. This can happen when we get an expose
23898 event while Emacs is starting, e.g. by moving another window. */
23899 if (FRAME_FACE_CACHE (f) == NULL
23900 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23901 {
23902 TRACE ((stderr, " no faces\n"));
23903 return;
23904 }
23905
23906 if (w == 0 || h == 0)
23907 {
23908 r.x = r.y = 0;
23909 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23910 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23911 }
23912 else
23913 {
23914 r.x = x;
23915 r.y = y;
23916 r.width = w;
23917 r.height = h;
23918 }
23919
23920 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23921 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23922
23923 if (WINDOWP (f->tool_bar_window))
23924 mouse_face_overwritten_p
23925 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23926
23927 #ifdef HAVE_X_WINDOWS
23928 #ifndef MSDOS
23929 #ifndef USE_X_TOOLKIT
23930 if (WINDOWP (f->menu_bar_window))
23931 mouse_face_overwritten_p
23932 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23933 #endif /* not USE_X_TOOLKIT */
23934 #endif
23935 #endif
23936
23937 /* Some window managers support a focus-follows-mouse style with
23938 delayed raising of frames. Imagine a partially obscured frame,
23939 and moving the mouse into partially obscured mouse-face on that
23940 frame. The visible part of the mouse-face will be highlighted,
23941 then the WM raises the obscured frame. With at least one WM, KDE
23942 2.1, Emacs is not getting any event for the raising of the frame
23943 (even tried with SubstructureRedirectMask), only Expose events.
23944 These expose events will draw text normally, i.e. not
23945 highlighted. Which means we must redo the highlight here.
23946 Subsume it under ``we love X''. --gerd 2001-08-15 */
23947 /* Included in Windows version because Windows most likely does not
23948 do the right thing if any third party tool offers
23949 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23950 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23951 {
23952 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23953 if (f == dpyinfo->mouse_face_mouse_frame)
23954 {
23955 int x = dpyinfo->mouse_face_mouse_x;
23956 int y = dpyinfo->mouse_face_mouse_y;
23957 clear_mouse_face (dpyinfo);
23958 note_mouse_highlight (f, x, y);
23959 }
23960 }
23961 }
23962
23963
23964 /* EXPORT:
23965 Determine the intersection of two rectangles R1 and R2. Return
23966 the intersection in *RESULT. Value is non-zero if RESULT is not
23967 empty. */
23968
23969 int
23970 x_intersect_rectangles (r1, r2, result)
23971 XRectangle *r1, *r2, *result;
23972 {
23973 XRectangle *left, *right;
23974 XRectangle *upper, *lower;
23975 int intersection_p = 0;
23976
23977 /* Rearrange so that R1 is the left-most rectangle. */
23978 if (r1->x < r2->x)
23979 left = r1, right = r2;
23980 else
23981 left = r2, right = r1;
23982
23983 /* X0 of the intersection is right.x0, if this is inside R1,
23984 otherwise there is no intersection. */
23985 if (right->x <= left->x + left->width)
23986 {
23987 result->x = right->x;
23988
23989 /* The right end of the intersection is the minimum of the
23990 the right ends of left and right. */
23991 result->width = (min (left->x + left->width, right->x + right->width)
23992 - result->x);
23993
23994 /* Same game for Y. */
23995 if (r1->y < r2->y)
23996 upper = r1, lower = r2;
23997 else
23998 upper = r2, lower = r1;
23999
24000 /* The upper end of the intersection is lower.y0, if this is inside
24001 of upper. Otherwise, there is no intersection. */
24002 if (lower->y <= upper->y + upper->height)
24003 {
24004 result->y = lower->y;
24005
24006 /* The lower end of the intersection is the minimum of the lower
24007 ends of upper and lower. */
24008 result->height = (min (lower->y + lower->height,
24009 upper->y + upper->height)
24010 - result->y);
24011 intersection_p = 1;
24012 }
24013 }
24014
24015 return intersection_p;
24016 }
24017
24018 #endif /* HAVE_WINDOW_SYSTEM */
24019
24020 \f
24021 /***********************************************************************
24022 Initialization
24023 ***********************************************************************/
24024
24025 void
24026 syms_of_xdisp ()
24027 {
24028 Vwith_echo_area_save_vector = Qnil;
24029 staticpro (&Vwith_echo_area_save_vector);
24030
24031 Vmessage_stack = Qnil;
24032 staticpro (&Vmessage_stack);
24033
24034 Qinhibit_redisplay = intern ("inhibit-redisplay");
24035 staticpro (&Qinhibit_redisplay);
24036
24037 message_dolog_marker1 = Fmake_marker ();
24038 staticpro (&message_dolog_marker1);
24039 message_dolog_marker2 = Fmake_marker ();
24040 staticpro (&message_dolog_marker2);
24041 message_dolog_marker3 = Fmake_marker ();
24042 staticpro (&message_dolog_marker3);
24043
24044 #if GLYPH_DEBUG
24045 defsubr (&Sdump_frame_glyph_matrix);
24046 defsubr (&Sdump_glyph_matrix);
24047 defsubr (&Sdump_glyph_row);
24048 defsubr (&Sdump_tool_bar_row);
24049 defsubr (&Strace_redisplay);
24050 defsubr (&Strace_to_stderr);
24051 #endif
24052 #ifdef HAVE_WINDOW_SYSTEM
24053 defsubr (&Stool_bar_lines_needed);
24054 defsubr (&Slookup_image_map);
24055 #endif
24056 defsubr (&Sformat_mode_line);
24057
24058 staticpro (&Qmenu_bar_update_hook);
24059 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24060
24061 staticpro (&Qoverriding_terminal_local_map);
24062 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24063
24064 staticpro (&Qoverriding_local_map);
24065 Qoverriding_local_map = intern ("overriding-local-map");
24066
24067 staticpro (&Qwindow_scroll_functions);
24068 Qwindow_scroll_functions = intern ("window-scroll-functions");
24069
24070 staticpro (&Qredisplay_end_trigger_functions);
24071 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24072
24073 staticpro (&Qinhibit_point_motion_hooks);
24074 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24075
24076 QCdata = intern (":data");
24077 staticpro (&QCdata);
24078 Qdisplay = intern ("display");
24079 staticpro (&Qdisplay);
24080 Qspace_width = intern ("space-width");
24081 staticpro (&Qspace_width);
24082 Qraise = intern ("raise");
24083 staticpro (&Qraise);
24084 Qslice = intern ("slice");
24085 staticpro (&Qslice);
24086 Qspace = intern ("space");
24087 staticpro (&Qspace);
24088 Qmargin = intern ("margin");
24089 staticpro (&Qmargin);
24090 Qpointer = intern ("pointer");
24091 staticpro (&Qpointer);
24092 Qleft_margin = intern ("left-margin");
24093 staticpro (&Qleft_margin);
24094 Qright_margin = intern ("right-margin");
24095 staticpro (&Qright_margin);
24096 Qcenter = intern ("center");
24097 staticpro (&Qcenter);
24098 Qline_height = intern ("line-height");
24099 staticpro (&Qline_height);
24100 QCalign_to = intern (":align-to");
24101 staticpro (&QCalign_to);
24102 QCrelative_width = intern (":relative-width");
24103 staticpro (&QCrelative_width);
24104 QCrelative_height = intern (":relative-height");
24105 staticpro (&QCrelative_height);
24106 QCeval = intern (":eval");
24107 staticpro (&QCeval);
24108 QCpropertize = intern (":propertize");
24109 staticpro (&QCpropertize);
24110 QCfile = intern (":file");
24111 staticpro (&QCfile);
24112 Qfontified = intern ("fontified");
24113 staticpro (&Qfontified);
24114 Qfontification_functions = intern ("fontification-functions");
24115 staticpro (&Qfontification_functions);
24116 Qtrailing_whitespace = intern ("trailing-whitespace");
24117 staticpro (&Qtrailing_whitespace);
24118 Qescape_glyph = intern ("escape-glyph");
24119 staticpro (&Qescape_glyph);
24120 Qnobreak_space = intern ("nobreak-space");
24121 staticpro (&Qnobreak_space);
24122 Qimage = intern ("image");
24123 staticpro (&Qimage);
24124 QCmap = intern (":map");
24125 staticpro (&QCmap);
24126 QCpointer = intern (":pointer");
24127 staticpro (&QCpointer);
24128 Qrect = intern ("rect");
24129 staticpro (&Qrect);
24130 Qcircle = intern ("circle");
24131 staticpro (&Qcircle);
24132 Qpoly = intern ("poly");
24133 staticpro (&Qpoly);
24134 Qmessage_truncate_lines = intern ("message-truncate-lines");
24135 staticpro (&Qmessage_truncate_lines);
24136 Qgrow_only = intern ("grow-only");
24137 staticpro (&Qgrow_only);
24138 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24139 staticpro (&Qinhibit_menubar_update);
24140 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24141 staticpro (&Qinhibit_eval_during_redisplay);
24142 Qposition = intern ("position");
24143 staticpro (&Qposition);
24144 Qbuffer_position = intern ("buffer-position");
24145 staticpro (&Qbuffer_position);
24146 Qobject = intern ("object");
24147 staticpro (&Qobject);
24148 Qbar = intern ("bar");
24149 staticpro (&Qbar);
24150 Qhbar = intern ("hbar");
24151 staticpro (&Qhbar);
24152 Qbox = intern ("box");
24153 staticpro (&Qbox);
24154 Qhollow = intern ("hollow");
24155 staticpro (&Qhollow);
24156 Qhand = intern ("hand");
24157 staticpro (&Qhand);
24158 Qarrow = intern ("arrow");
24159 staticpro (&Qarrow);
24160 Qtext = intern ("text");
24161 staticpro (&Qtext);
24162 Qrisky_local_variable = intern ("risky-local-variable");
24163 staticpro (&Qrisky_local_variable);
24164 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24165 staticpro (&Qinhibit_free_realized_faces);
24166
24167 list_of_error = Fcons (Fcons (intern ("error"),
24168 Fcons (intern ("void-variable"), Qnil)),
24169 Qnil);
24170 staticpro (&list_of_error);
24171
24172 Qlast_arrow_position = intern ("last-arrow-position");
24173 staticpro (&Qlast_arrow_position);
24174 Qlast_arrow_string = intern ("last-arrow-string");
24175 staticpro (&Qlast_arrow_string);
24176
24177 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24178 staticpro (&Qoverlay_arrow_string);
24179 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24180 staticpro (&Qoverlay_arrow_bitmap);
24181
24182 echo_buffer[0] = echo_buffer[1] = Qnil;
24183 staticpro (&echo_buffer[0]);
24184 staticpro (&echo_buffer[1]);
24185
24186 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24187 staticpro (&echo_area_buffer[0]);
24188 staticpro (&echo_area_buffer[1]);
24189
24190 Vmessages_buffer_name = build_string ("*Messages*");
24191 staticpro (&Vmessages_buffer_name);
24192
24193 mode_line_proptrans_alist = Qnil;
24194 staticpro (&mode_line_proptrans_alist);
24195 mode_line_string_list = Qnil;
24196 staticpro (&mode_line_string_list);
24197 mode_line_string_face = Qnil;
24198 staticpro (&mode_line_string_face);
24199 mode_line_string_face_prop = Qnil;
24200 staticpro (&mode_line_string_face_prop);
24201 Vmode_line_unwind_vector = Qnil;
24202 staticpro (&Vmode_line_unwind_vector);
24203
24204 help_echo_string = Qnil;
24205 staticpro (&help_echo_string);
24206 help_echo_object = Qnil;
24207 staticpro (&help_echo_object);
24208 help_echo_window = Qnil;
24209 staticpro (&help_echo_window);
24210 previous_help_echo_string = Qnil;
24211 staticpro (&previous_help_echo_string);
24212 help_echo_pos = -1;
24213
24214 #ifdef HAVE_WINDOW_SYSTEM
24215 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24216 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24217 For example, if a block cursor is over a tab, it will be drawn as
24218 wide as that tab on the display. */);
24219 x_stretch_cursor_p = 0;
24220 #endif
24221
24222 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24223 doc: /* *Non-nil means highlight trailing whitespace.
24224 The face used for trailing whitespace is `trailing-whitespace'. */);
24225 Vshow_trailing_whitespace = Qnil;
24226
24227 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24228 doc: /* *Control highlighting of nobreak space and soft hyphen.
24229 A value of t means highlight the character itself (for nobreak space,
24230 use face `nobreak-space').
24231 A value of nil means no highlighting.
24232 Other values mean display the escape glyph followed by an ordinary
24233 space or ordinary hyphen. */);
24234 Vnobreak_char_display = Qt;
24235
24236 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24237 doc: /* *The pointer shape to show in void text areas.
24238 A value of nil means to show the text pointer. Other options are `arrow',
24239 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24240 Vvoid_text_area_pointer = Qarrow;
24241
24242 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24243 doc: /* Non-nil means don't actually do any redisplay.
24244 This is used for internal purposes. */);
24245 Vinhibit_redisplay = Qnil;
24246
24247 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24248 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24249 Vglobal_mode_string = Qnil;
24250
24251 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24252 doc: /* Marker for where to display an arrow on top of the buffer text.
24253 This must be the beginning of a line in order to work.
24254 See also `overlay-arrow-string'. */);
24255 Voverlay_arrow_position = Qnil;
24256
24257 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24258 doc: /* String to display as an arrow in non-window frames.
24259 See also `overlay-arrow-position'. */);
24260 Voverlay_arrow_string = build_string ("=>");
24261
24262 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24263 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24264 The symbols on this list are examined during redisplay to determine
24265 where to display overlay arrows. */);
24266 Voverlay_arrow_variable_list
24267 = Fcons (intern ("overlay-arrow-position"), Qnil);
24268
24269 DEFVAR_INT ("scroll-step", &scroll_step,
24270 doc: /* *The number of lines to try scrolling a window by when point moves out.
24271 If that fails to bring point back on frame, point is centered instead.
24272 If this is zero, point is always centered after it moves off frame.
24273 If you want scrolling to always be a line at a time, you should set
24274 `scroll-conservatively' to a large value rather than set this to 1. */);
24275
24276 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24277 doc: /* *Scroll up to this many lines, to bring point back on screen.
24278 A value of zero means to scroll the text to center point vertically
24279 in the window. */);
24280 scroll_conservatively = 0;
24281
24282 DEFVAR_INT ("scroll-margin", &scroll_margin,
24283 doc: /* *Number of lines of margin at the top and bottom of a window.
24284 Recenter the window whenever point gets within this many lines
24285 of the top or bottom of the window. */);
24286 scroll_margin = 0;
24287
24288 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24289 doc: /* Pixels per inch value for non-window system displays.
24290 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24291 Vdisplay_pixels_per_inch = make_float (72.0);
24292
24293 #if GLYPH_DEBUG
24294 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24295 #endif
24296
24297 DEFVAR_BOOL ("truncate-partial-width-windows",
24298 &truncate_partial_width_windows,
24299 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24300 truncate_partial_width_windows = 1;
24301
24302 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24303 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24304 Any other value means to use the appropriate face, `mode-line',
24305 `header-line', or `menu' respectively. */);
24306 mode_line_inverse_video = 1;
24307
24308 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24309 doc: /* *Maximum buffer size for which line number should be displayed.
24310 If the buffer is bigger than this, the line number does not appear
24311 in the mode line. A value of nil means no limit. */);
24312 Vline_number_display_limit = Qnil;
24313
24314 DEFVAR_INT ("line-number-display-limit-width",
24315 &line_number_display_limit_width,
24316 doc: /* *Maximum line width (in characters) for line number display.
24317 If the average length of the lines near point is bigger than this, then the
24318 line number may be omitted from the mode line. */);
24319 line_number_display_limit_width = 200;
24320
24321 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24322 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24323 highlight_nonselected_windows = 0;
24324
24325 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24326 doc: /* Non-nil if more than one frame is visible on this display.
24327 Minibuffer-only frames don't count, but iconified frames do.
24328 This variable is not guaranteed to be accurate except while processing
24329 `frame-title-format' and `icon-title-format'. */);
24330
24331 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24332 doc: /* Template for displaying the title bar of visible frames.
24333 \(Assuming the window manager supports this feature.)
24334
24335 This variable has the same structure as `mode-line-format', except that
24336 the %c and %l constructs are ignored. It is used only on frames for
24337 which no explicit name has been set \(see `modify-frame-parameters'). */);
24338
24339 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24340 doc: /* Template for displaying the title bar of an iconified frame.
24341 \(Assuming the window manager supports this feature.)
24342 This variable has the same structure as `mode-line-format' (which see),
24343 and is used only on frames for which no explicit name has been set
24344 \(see `modify-frame-parameters'). */);
24345 Vicon_title_format
24346 = Vframe_title_format
24347 = Fcons (intern ("multiple-frames"),
24348 Fcons (build_string ("%b"),
24349 Fcons (Fcons (empty_string,
24350 Fcons (intern ("invocation-name"),
24351 Fcons (build_string ("@"),
24352 Fcons (intern ("system-name"),
24353 Qnil)))),
24354 Qnil)));
24355
24356 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24357 doc: /* Maximum number of lines to keep in the message log buffer.
24358 If nil, disable message logging. If t, log messages but don't truncate
24359 the buffer when it becomes large. */);
24360 Vmessage_log_max = make_number (50);
24361
24362 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24363 doc: /* Functions called before redisplay, if window sizes have changed.
24364 The value should be a list of functions that take one argument.
24365 Just before redisplay, for each frame, if any of its windows have changed
24366 size since the last redisplay, or have been split or deleted,
24367 all the functions in the list are called, with the frame as argument. */);
24368 Vwindow_size_change_functions = Qnil;
24369
24370 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24371 doc: /* List of functions to call before redisplaying a window with scrolling.
24372 Each function is called with two arguments, the window
24373 and its new display-start position. Note that the value of `window-end'
24374 is not valid when these functions are called. */);
24375 Vwindow_scroll_functions = Qnil;
24376
24377 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24378 doc: /* Functions called when redisplay of a window reaches the end trigger.
24379 Each function is called with two arguments, the window and the end trigger value.
24380 See `set-window-redisplay-end-trigger'. */);
24381 Vredisplay_end_trigger_functions = Qnil;
24382
24383 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24384 doc: /* *Non-nil means autoselect window with mouse pointer.
24385 If nil, do not autoselect windows.
24386 A positive number means delay autoselection by that many seconds: a
24387 window is autoselected only after the mouse has remained in that
24388 window for the duration of the delay.
24389 A negative number has a similar effect, but causes windows to be
24390 autoselected only after the mouse has stopped moving. \(Because of
24391 the way Emacs compares mouse events, you will occasionally wait twice
24392 that time before the window gets selected.\)
24393 Any other value means to autoselect window instantaneously when the
24394 mouse pointer enters it.
24395
24396 Autoselection selects the minibuffer only if it is active, and never
24397 unselects the minibuffer if it is active. */);
24398 Vmouse_autoselect_window = Qnil;
24399
24400 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24401 doc: /* *Non-nil means automatically resize tool-bars.
24402 This dynamically changes the tool-bar's height to the minimum height
24403 that is needed to make all tool-bar items visible.
24404 If value is `grow-only', the tool-bar's height is only increased
24405 automatically; to decreace the tool-bar height, use \\[recenter]. */);
24406 Vauto_resize_tool_bars = Qt;
24407
24408 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24409 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24410 auto_raise_tool_bar_buttons_p = 1;
24411
24412 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24413 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24414 make_cursor_line_fully_visible_p = 1;
24415
24416 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24417 doc: /* *Border below tool-bar in pixels.
24418 If an integer, use it as the height of the border.
24419 If it is one of `internal-border-width' or `border-width', use the
24420 value of the corresponding frame parameter.
24421 Otherwise, no border is added below the tool-bar. */);
24422 Vtool_bar_border = Qinternal_border_width;
24423
24424 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24425 doc: /* *Margin around tool-bar buttons in pixels.
24426 If an integer, use that for both horizontal and vertical margins.
24427 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24428 HORZ specifying the horizontal margin, and VERT specifying the
24429 vertical margin. */);
24430 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24431
24432 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24433 doc: /* *Relief thickness of tool-bar buttons. */);
24434 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24435
24436 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24437 doc: /* List of functions to call to fontify regions of text.
24438 Each function is called with one argument POS. Functions must
24439 fontify a region starting at POS in the current buffer, and give
24440 fontified regions the property `fontified'. */);
24441 Vfontification_functions = Qnil;
24442 Fmake_variable_buffer_local (Qfontification_functions);
24443
24444 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24445 &unibyte_display_via_language_environment,
24446 doc: /* *Non-nil means display unibyte text according to language environment.
24447 Specifically this means that unibyte non-ASCII characters
24448 are displayed by converting them to the equivalent multibyte characters
24449 according to the current language environment. As a result, they are
24450 displayed according to the current fontset. */);
24451 unibyte_display_via_language_environment = 0;
24452
24453 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24454 doc: /* *Maximum height for resizing mini-windows.
24455 If a float, it specifies a fraction of the mini-window frame's height.
24456 If an integer, it specifies a number of lines. */);
24457 Vmax_mini_window_height = make_float (0.25);
24458
24459 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24460 doc: /* *How to resize mini-windows.
24461 A value of nil means don't automatically resize mini-windows.
24462 A value of t means resize them to fit the text displayed in them.
24463 A value of `grow-only', the default, means let mini-windows grow
24464 only, until their display becomes empty, at which point the windows
24465 go back to their normal size. */);
24466 Vresize_mini_windows = Qgrow_only;
24467
24468 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24469 doc: /* Alist specifying how to blink the cursor off.
24470 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24471 `cursor-type' frame-parameter or variable equals ON-STATE,
24472 comparing using `equal', Emacs uses OFF-STATE to specify
24473 how to blink it off. ON-STATE and OFF-STATE are values for
24474 the `cursor-type' frame parameter.
24475
24476 If a frame's ON-STATE has no entry in this list,
24477 the frame's other specifications determine how to blink the cursor off. */);
24478 Vblink_cursor_alist = Qnil;
24479
24480 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24481 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24482 automatic_hscrolling_p = 1;
24483
24484 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24485 doc: /* *How many columns away from the window edge point is allowed to get
24486 before automatic hscrolling will horizontally scroll the window. */);
24487 hscroll_margin = 5;
24488
24489 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24490 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24491 When point is less than `hscroll-margin' columns from the window
24492 edge, automatic hscrolling will scroll the window by the amount of columns
24493 determined by this variable. If its value is a positive integer, scroll that
24494 many columns. If it's a positive floating-point number, it specifies the
24495 fraction of the window's width to scroll. If it's nil or zero, point will be
24496 centered horizontally after the scroll. Any other value, including negative
24497 numbers, are treated as if the value were zero.
24498
24499 Automatic hscrolling always moves point outside the scroll margin, so if
24500 point was more than scroll step columns inside the margin, the window will
24501 scroll more than the value given by the scroll step.
24502
24503 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24504 and `scroll-right' overrides this variable's effect. */);
24505 Vhscroll_step = make_number (0);
24506
24507 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24508 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24509 Bind this around calls to `message' to let it take effect. */);
24510 message_truncate_lines = 0;
24511
24512 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24513 doc: /* Normal hook run to update the menu bar definitions.
24514 Redisplay runs this hook before it redisplays the menu bar.
24515 This is used to update submenus such as Buffers,
24516 whose contents depend on various data. */);
24517 Vmenu_bar_update_hook = Qnil;
24518
24519 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24520 doc: /* Frame for which we are updating a menu.
24521 The enable predicate for a menu binding should check this variable. */);
24522 Vmenu_updating_frame = Qnil;
24523
24524 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24525 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24526 inhibit_menubar_update = 0;
24527
24528 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24529 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24530 inhibit_eval_during_redisplay = 0;
24531
24532 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24533 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24534 inhibit_free_realized_faces = 0;
24535
24536 #if GLYPH_DEBUG
24537 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24538 doc: /* Inhibit try_window_id display optimization. */);
24539 inhibit_try_window_id = 0;
24540
24541 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24542 doc: /* Inhibit try_window_reusing display optimization. */);
24543 inhibit_try_window_reusing = 0;
24544
24545 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24546 doc: /* Inhibit try_cursor_movement display optimization. */);
24547 inhibit_try_cursor_movement = 0;
24548 #endif /* GLYPH_DEBUG */
24549
24550 DEFVAR_INT ("overline-margin", &overline_margin,
24551 doc: /* *Space between overline and text, in pixels.
24552 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24553 margin to the caracter height. */);
24554 overline_margin = 2;
24555 }
24556
24557
24558 /* Initialize this module when Emacs starts. */
24559
24560 void
24561 init_xdisp ()
24562 {
24563 Lisp_Object root_window;
24564 struct window *mini_w;
24565
24566 current_header_line_height = current_mode_line_height = -1;
24567
24568 CHARPOS (this_line_start_pos) = 0;
24569
24570 mini_w = XWINDOW (minibuf_window);
24571 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24572
24573 if (!noninteractive)
24574 {
24575 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24576 int i;
24577
24578 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24579 set_window_height (root_window,
24580 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24581 0);
24582 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24583 set_window_height (minibuf_window, 1, 0);
24584
24585 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24586 mini_w->total_cols = make_number (FRAME_COLS (f));
24587
24588 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24589 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24590 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24591
24592 /* The default ellipsis glyphs `...'. */
24593 for (i = 0; i < 3; ++i)
24594 default_invis_vector[i] = make_number ('.');
24595 }
24596
24597 {
24598 /* Allocate the buffer for frame titles.
24599 Also used for `format-mode-line'. */
24600 int size = 100;
24601 mode_line_noprop_buf = (char *) xmalloc (size);
24602 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24603 mode_line_noprop_ptr = mode_line_noprop_buf;
24604 mode_line_target = MODE_LINE_DISPLAY;
24605 }
24606
24607 help_echo_showing_p = 0;
24608 }
24609
24610
24611 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24612 (do not change this comment) */