]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Code cleanup in xdisp.c regarding string_char_and_length.
[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, 2008, 2009, 2010
5 Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171 #include <limits.h>
172 #include <setjmp.h>
173
174 #include "lisp.h"
175 #include "keyboard.h"
176 #include "frame.h"
177 #include "window.h"
178 #include "termchar.h"
179 #include "dispextern.h"
180 #include "buffer.h"
181 #include "character.h"
182 #include "charset.h"
183 #include "indent.h"
184 #include "commands.h"
185 #include "keymap.h"
186 #include "macros.h"
187 #include "disptab.h"
188 #include "termhooks.h"
189 #include "intervals.h"
190 #include "coding.h"
191 #include "process.h"
192 #include "region-cache.h"
193 #include "font.h"
194 #include "fontset.h"
195 #include "blockinput.h"
196
197 #ifdef HAVE_X_WINDOWS
198 #include "xterm.h"
199 #endif
200 #ifdef WINDOWSNT
201 #include "w32term.h"
202 #endif
203 #ifdef HAVE_NS
204 #include "nsterm.h"
205 #endif
206 #ifdef USE_GTK
207 #include "gtkutil.h"
208 #endif
209
210 #include "font.h"
211
212 #ifndef FRAME_X_OUTPUT
213 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
214 #endif
215
216 #define INFINITY 10000000
217
218 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
219 || defined(HAVE_NS) || defined (USE_GTK)
220 extern void set_frame_menubar P_ ((struct frame *f, int, int));
221 extern int pending_menu_activation;
222 #endif
223
224 extern int interrupt_input;
225 extern int command_loop_level;
226
227 extern Lisp_Object do_mouse_tracking;
228
229 extern int minibuffer_auto_raise;
230 extern Lisp_Object Vminibuffer_list;
231
232 extern Lisp_Object Qface;
233 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
234
235 extern Lisp_Object Voverriding_local_map;
236 extern Lisp_Object Voverriding_local_map_menu_flag;
237 extern Lisp_Object Qmenu_item;
238 extern Lisp_Object Qwhen;
239 extern Lisp_Object Qhelp_echo;
240 extern Lisp_Object Qbefore_string, Qafter_string;
241
242 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
243 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
244 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
245 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
246 Lisp_Object Qinhibit_point_motion_hooks;
247 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
248 Lisp_Object Qfontified;
249 Lisp_Object Qgrow_only;
250 Lisp_Object Qinhibit_eval_during_redisplay;
251 Lisp_Object Qbuffer_position, Qposition, Qobject;
252
253 /* Cursor shapes */
254 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
255
256 /* Pointer shapes */
257 Lisp_Object Qarrow, Qhand, Qtext;
258
259 Lisp_Object Qrisky_local_variable;
260
261 /* Holds the list (error). */
262 Lisp_Object list_of_error;
263
264 /* Functions called to fontify regions of text. */
265
266 Lisp_Object Vfontification_functions;
267 Lisp_Object Qfontification_functions;
268
269 /* Non-nil means automatically select any window when the mouse
270 cursor moves into it. */
271 Lisp_Object Vmouse_autoselect_window;
272
273 Lisp_Object Vwrap_prefix, Qwrap_prefix;
274 Lisp_Object Vline_prefix, Qline_prefix;
275
276 /* Non-zero means draw tool bar buttons raised when the mouse moves
277 over them. */
278
279 int auto_raise_tool_bar_buttons_p;
280
281 /* Non-zero means to reposition window if cursor line is only partially visible. */
282
283 int make_cursor_line_fully_visible_p;
284
285 /* Margin below tool bar in pixels. 0 or nil means no margin.
286 If value is `internal-border-width' or `border-width',
287 the corresponding frame parameter is used. */
288
289 Lisp_Object Vtool_bar_border;
290
291 /* Margin around tool bar buttons in pixels. */
292
293 Lisp_Object Vtool_bar_button_margin;
294
295 /* Thickness of shadow to draw around tool bar buttons. */
296
297 EMACS_INT tool_bar_button_relief;
298
299 /* Non-nil means automatically resize tool-bars so that all tool-bar
300 items are visible, and no blank lines remain.
301
302 If value is `grow-only', only make tool-bar bigger. */
303
304 Lisp_Object Vauto_resize_tool_bars;
305
306 /* Non-zero means draw block and hollow cursor as wide as the glyph
307 under it. For example, if a block cursor is over a tab, it will be
308 drawn as wide as that tab on the display. */
309
310 int x_stretch_cursor_p;
311
312 /* Non-nil means don't actually do any redisplay. */
313
314 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
315
316 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
317
318 int inhibit_eval_during_redisplay;
319
320 /* Names of text properties relevant for redisplay. */
321
322 Lisp_Object Qdisplay;
323 extern Lisp_Object Qface, Qinvisible, Qwidth;
324
325 /* Symbols used in text property values. */
326
327 Lisp_Object Vdisplay_pixels_per_inch;
328 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
329 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
330 Lisp_Object Qslice;
331 Lisp_Object Qcenter;
332 Lisp_Object Qmargin, Qpointer;
333 Lisp_Object Qline_height;
334 extern Lisp_Object Qheight;
335 extern Lisp_Object QCwidth, QCheight, QCascent;
336 extern Lisp_Object Qscroll_bar;
337 extern Lisp_Object Qcursor;
338
339 /* Non-nil means highlight trailing whitespace. */
340
341 Lisp_Object Vshow_trailing_whitespace;
342
343 /* Non-nil means escape non-break space and hyphens. */
344
345 Lisp_Object Vnobreak_char_display;
346
347 #ifdef HAVE_WINDOW_SYSTEM
348 extern Lisp_Object Voverflow_newline_into_fringe;
349
350 /* Test if overflow newline into fringe. Called with iterator IT
351 at or past right window margin, and with IT->current_x set. */
352
353 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
354 (!NILP (Voverflow_newline_into_fringe) \
355 && FRAME_WINDOW_P (it->f) \
356 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
357 && it->current_x == it->last_visible_x \
358 && it->line_wrap != WORD_WRAP)
359
360 #else /* !HAVE_WINDOW_SYSTEM */
361 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
362 #endif /* HAVE_WINDOW_SYSTEM */
363
364 /* Test if the display element loaded in IT is a space or tab
365 character. This is used to determine word wrapping. */
366
367 #define IT_DISPLAYING_WHITESPACE(it) \
368 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
369
370 /* Non-nil means show the text cursor in void text areas
371 i.e. in blank areas after eol and eob. This used to be
372 the default in 21.3. */
373
374 Lisp_Object Vvoid_text_area_pointer;
375
376 /* Name of the face used to highlight trailing whitespace. */
377
378 Lisp_Object Qtrailing_whitespace;
379
380 /* Name and number of the face used to highlight escape glyphs. */
381
382 Lisp_Object Qescape_glyph;
383
384 /* Name and number of the face used to highlight non-breaking spaces. */
385
386 Lisp_Object Qnobreak_space;
387
388 /* The symbol `image' which is the car of the lists used to represent
389 images in Lisp. */
390
391 Lisp_Object Qimage;
392
393 /* The image map types. */
394 Lisp_Object QCmap, QCpointer;
395 Lisp_Object Qrect, Qcircle, Qpoly;
396
397 /* Non-zero means print newline to stdout before next mini-buffer
398 message. */
399
400 int noninteractive_need_newline;
401
402 /* Non-zero means print newline to message log before next message. */
403
404 static int message_log_need_newline;
405
406 /* Three markers that message_dolog uses.
407 It could allocate them itself, but that causes trouble
408 in handling memory-full errors. */
409 static Lisp_Object message_dolog_marker1;
410 static Lisp_Object message_dolog_marker2;
411 static Lisp_Object message_dolog_marker3;
412 \f
413 /* The buffer position of the first character appearing entirely or
414 partially on the line of the selected window which contains the
415 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
416 redisplay optimization in redisplay_internal. */
417
418 static struct text_pos this_line_start_pos;
419
420 /* Number of characters past the end of the line above, including the
421 terminating newline. */
422
423 static struct text_pos this_line_end_pos;
424
425 /* The vertical positions and the height of this line. */
426
427 static int this_line_vpos;
428 static int this_line_y;
429 static int this_line_pixel_height;
430
431 /* X position at which this display line starts. Usually zero;
432 negative if first character is partially visible. */
433
434 static int this_line_start_x;
435
436 /* Buffer that this_line_.* variables are referring to. */
437
438 static struct buffer *this_line_buffer;
439
440 /* Nonzero means truncate lines in all windows less wide than the
441 frame. */
442
443 Lisp_Object Vtruncate_partial_width_windows;
444
445 /* A flag to control how to display unibyte 8-bit character. */
446
447 int unibyte_display_via_language_environment;
448
449 /* Nonzero means we have more than one non-mini-buffer-only frame.
450 Not guaranteed to be accurate except while parsing
451 frame-title-format. */
452
453 int multiple_frames;
454
455 Lisp_Object Vglobal_mode_string;
456
457
458 /* List of variables (symbols) which hold markers for overlay arrows.
459 The symbols on this list are examined during redisplay to determine
460 where to display overlay arrows. */
461
462 Lisp_Object Voverlay_arrow_variable_list;
463
464 /* Marker for where to display an arrow on top of the buffer text. */
465
466 Lisp_Object Voverlay_arrow_position;
467
468 /* String to display for the arrow. Only used on terminal frames. */
469
470 Lisp_Object Voverlay_arrow_string;
471
472 /* Values of those variables at last redisplay are stored as
473 properties on `overlay-arrow-position' symbol. However, if
474 Voverlay_arrow_position is a marker, last-arrow-position is its
475 numerical position. */
476
477 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
478
479 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
480 properties on a symbol in overlay-arrow-variable-list. */
481
482 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
483
484 /* Like mode-line-format, but for the title bar on a visible frame. */
485
486 Lisp_Object Vframe_title_format;
487
488 /* Like mode-line-format, but for the title bar on an iconified frame. */
489
490 Lisp_Object Vicon_title_format;
491
492 /* List of functions to call when a window's size changes. These
493 functions get one arg, a frame on which one or more windows' sizes
494 have changed. */
495
496 static Lisp_Object Vwindow_size_change_functions;
497
498 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
499
500 /* Nonzero if an overlay arrow has been displayed in this window. */
501
502 static int overlay_arrow_seen;
503
504 /* Nonzero means highlight the region even in nonselected windows. */
505
506 int highlight_nonselected_windows;
507
508 /* If cursor motion alone moves point off frame, try scrolling this
509 many lines up or down if that will bring it back. */
510
511 static EMACS_INT scroll_step;
512
513 /* Nonzero means scroll just far enough to bring point back on the
514 screen, when appropriate. */
515
516 static EMACS_INT scroll_conservatively;
517
518 /* Recenter the window whenever point gets within this many lines of
519 the top or bottom of the window. This value is translated into a
520 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
521 that there is really a fixed pixel height scroll margin. */
522
523 EMACS_INT scroll_margin;
524
525 /* Number of windows showing the buffer of the selected window (or
526 another buffer with the same base buffer). keyboard.c refers to
527 this. */
528
529 int buffer_shared;
530
531 /* Vector containing glyphs for an ellipsis `...'. */
532
533 static Lisp_Object default_invis_vector[3];
534
535 /* Zero means display the mode-line/header-line/menu-bar in the default face
536 (this slightly odd definition is for compatibility with previous versions
537 of emacs), non-zero means display them using their respective faces.
538
539 This variable is deprecated. */
540
541 int mode_line_inverse_video;
542
543 /* Prompt to display in front of the mini-buffer contents. */
544
545 Lisp_Object minibuf_prompt;
546
547 /* Width of current mini-buffer prompt. Only set after display_line
548 of the line that contains the prompt. */
549
550 int minibuf_prompt_width;
551
552 /* This is the window where the echo area message was displayed. It
553 is always a mini-buffer window, but it may not be the same window
554 currently active as a mini-buffer. */
555
556 Lisp_Object echo_area_window;
557
558 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
559 pushes the current message and the value of
560 message_enable_multibyte on the stack, the function restore_message
561 pops the stack and displays MESSAGE again. */
562
563 Lisp_Object Vmessage_stack;
564
565 /* Nonzero means multibyte characters were enabled when the echo area
566 message was specified. */
567
568 int message_enable_multibyte;
569
570 /* Nonzero if we should redraw the mode lines on the next redisplay. */
571
572 int update_mode_lines;
573
574 /* Nonzero if window sizes or contents have changed since last
575 redisplay that finished. */
576
577 int windows_or_buffers_changed;
578
579 /* Nonzero means a frame's cursor type has been changed. */
580
581 int cursor_type_changed;
582
583 /* Nonzero after display_mode_line if %l was used and it displayed a
584 line number. */
585
586 int line_number_displayed;
587
588 /* Maximum buffer size for which to display line numbers. */
589
590 Lisp_Object Vline_number_display_limit;
591
592 /* Line width to consider when repositioning for line number display. */
593
594 static EMACS_INT line_number_display_limit_width;
595
596 /* Number of lines to keep in the message log buffer. t means
597 infinite. nil means don't log at all. */
598
599 Lisp_Object Vmessage_log_max;
600
601 /* The name of the *Messages* buffer, a string. */
602
603 static Lisp_Object Vmessages_buffer_name;
604
605 /* Current, index 0, and last displayed echo area message. Either
606 buffers from echo_buffers, or nil to indicate no message. */
607
608 Lisp_Object echo_area_buffer[2];
609
610 /* The buffers referenced from echo_area_buffer. */
611
612 static Lisp_Object echo_buffer[2];
613
614 /* A vector saved used in with_area_buffer to reduce consing. */
615
616 static Lisp_Object Vwith_echo_area_save_vector;
617
618 /* Non-zero means display_echo_area should display the last echo area
619 message again. Set by redisplay_preserve_echo_area. */
620
621 static int display_last_displayed_message_p;
622
623 /* Nonzero if echo area is being used by print; zero if being used by
624 message. */
625
626 int message_buf_print;
627
628 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
629
630 Lisp_Object Qinhibit_menubar_update;
631 int inhibit_menubar_update;
632
633 /* When evaluating expressions from menu bar items (enable conditions,
634 for instance), this is the frame they are being processed for. */
635
636 Lisp_Object Vmenu_updating_frame;
637
638 /* Maximum height for resizing mini-windows. Either a float
639 specifying a fraction of the available height, or an integer
640 specifying a number of lines. */
641
642 Lisp_Object Vmax_mini_window_height;
643
644 /* Non-zero means messages should be displayed with truncated
645 lines instead of being continued. */
646
647 int message_truncate_lines;
648 Lisp_Object Qmessage_truncate_lines;
649
650 /* Set to 1 in clear_message to make redisplay_internal aware
651 of an emptied echo area. */
652
653 static int message_cleared_p;
654
655 /* How to blink the default frame cursor off. */
656 Lisp_Object Vblink_cursor_alist;
657
658 /* A scratch glyph row with contents used for generating truncation
659 glyphs. Also used in direct_output_for_insert. */
660
661 #define MAX_SCRATCH_GLYPHS 100
662 struct glyph_row scratch_glyph_row;
663 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
664
665 /* Ascent and height of the last line processed by move_it_to. */
666
667 static int last_max_ascent, last_height;
668
669 /* Non-zero if there's a help-echo in the echo area. */
670
671 int help_echo_showing_p;
672
673 /* If >= 0, computed, exact values of mode-line and header-line height
674 to use in the macros CURRENT_MODE_LINE_HEIGHT and
675 CURRENT_HEADER_LINE_HEIGHT. */
676
677 int current_mode_line_height, current_header_line_height;
678
679 /* The maximum distance to look ahead for text properties. Values
680 that are too small let us call compute_char_face and similar
681 functions too often which is expensive. Values that are too large
682 let us call compute_char_face and alike too often because we
683 might not be interested in text properties that far away. */
684
685 #define TEXT_PROP_DISTANCE_LIMIT 100
686
687 #if GLYPH_DEBUG
688
689 /* Variables to turn off display optimizations from Lisp. */
690
691 int inhibit_try_window_id, inhibit_try_window_reusing;
692 int inhibit_try_cursor_movement;
693
694 /* Non-zero means print traces of redisplay if compiled with
695 GLYPH_DEBUG != 0. */
696
697 int trace_redisplay_p;
698
699 #endif /* GLYPH_DEBUG */
700
701 #ifdef DEBUG_TRACE_MOVE
702 /* Non-zero means trace with TRACE_MOVE to stderr. */
703 int trace_move;
704
705 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
706 #else
707 #define TRACE_MOVE(x) (void) 0
708 #endif
709
710 /* Non-zero means automatically scroll windows horizontally to make
711 point visible. */
712
713 int automatic_hscrolling_p;
714 Lisp_Object Qauto_hscroll_mode;
715
716 /* How close to the margin can point get before the window is scrolled
717 horizontally. */
718 EMACS_INT hscroll_margin;
719
720 /* How much to scroll horizontally when point is inside the above margin. */
721 Lisp_Object Vhscroll_step;
722
723 /* The variable `resize-mini-windows'. If nil, don't resize
724 mini-windows. If t, always resize them to fit the text they
725 display. If `grow-only', let mini-windows grow only until they
726 become empty. */
727
728 Lisp_Object Vresize_mini_windows;
729
730 /* Buffer being redisplayed -- for redisplay_window_error. */
731
732 struct buffer *displayed_buffer;
733
734 /* Space between overline and text. */
735
736 EMACS_INT overline_margin;
737
738 /* Require underline to be at least this many screen pixels below baseline
739 This to avoid underline "merging" with the base of letters at small
740 font sizes, particularly when x_use_underline_position_properties is on. */
741
742 EMACS_INT underline_minimum_offset;
743
744 /* Value returned from text property handlers (see below). */
745
746 enum prop_handled
747 {
748 HANDLED_NORMALLY,
749 HANDLED_RECOMPUTE_PROPS,
750 HANDLED_OVERLAY_STRING_CONSUMED,
751 HANDLED_RETURN
752 };
753
754 /* A description of text properties that redisplay is interested
755 in. */
756
757 struct props
758 {
759 /* The name of the property. */
760 Lisp_Object *name;
761
762 /* A unique index for the property. */
763 enum prop_idx idx;
764
765 /* A handler function called to set up iterator IT from the property
766 at IT's current position. Value is used to steer handle_stop. */
767 enum prop_handled (*handler) P_ ((struct it *it));
768 };
769
770 static enum prop_handled handle_face_prop P_ ((struct it *));
771 static enum prop_handled handle_invisible_prop P_ ((struct it *));
772 static enum prop_handled handle_display_prop P_ ((struct it *));
773 static enum prop_handled handle_composition_prop P_ ((struct it *));
774 static enum prop_handled handle_overlay_change P_ ((struct it *));
775 static enum prop_handled handle_fontified_prop P_ ((struct it *));
776
777 /* Properties handled by iterators. */
778
779 static struct props it_props[] =
780 {
781 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
782 /* Handle `face' before `display' because some sub-properties of
783 `display' need to know the face. */
784 {&Qface, FACE_PROP_IDX, handle_face_prop},
785 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
786 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
787 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
788 {NULL, 0, NULL}
789 };
790
791 /* Value is the position described by X. If X is a marker, value is
792 the marker_position of X. Otherwise, value is X. */
793
794 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
795
796 /* Enumeration returned by some move_it_.* functions internally. */
797
798 enum move_it_result
799 {
800 /* Not used. Undefined value. */
801 MOVE_UNDEFINED,
802
803 /* Move ended at the requested buffer position or ZV. */
804 MOVE_POS_MATCH_OR_ZV,
805
806 /* Move ended at the requested X pixel position. */
807 MOVE_X_REACHED,
808
809 /* Move within a line ended at the end of a line that must be
810 continued. */
811 MOVE_LINE_CONTINUED,
812
813 /* Move within a line ended at the end of a line that would
814 be displayed truncated. */
815 MOVE_LINE_TRUNCATED,
816
817 /* Move within a line ended at a line end. */
818 MOVE_NEWLINE_OR_CR
819 };
820
821 /* This counter is used to clear the face cache every once in a while
822 in redisplay_internal. It is incremented for each redisplay.
823 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
824 cleared. */
825
826 #define CLEAR_FACE_CACHE_COUNT 500
827 static int clear_face_cache_count;
828
829 /* Similarly for the image cache. */
830
831 #ifdef HAVE_WINDOW_SYSTEM
832 #define CLEAR_IMAGE_CACHE_COUNT 101
833 static int clear_image_cache_count;
834 #endif
835
836 /* Non-zero while redisplay_internal is in progress. */
837
838 int redisplaying_p;
839
840 /* Non-zero means don't free realized faces. Bound while freeing
841 realized faces is dangerous because glyph matrices might still
842 reference them. */
843
844 int inhibit_free_realized_faces;
845 Lisp_Object Qinhibit_free_realized_faces;
846
847 /* If a string, XTread_socket generates an event to display that string.
848 (The display is done in read_char.) */
849
850 Lisp_Object help_echo_string;
851 Lisp_Object help_echo_window;
852 Lisp_Object help_echo_object;
853 int help_echo_pos;
854
855 /* Temporary variable for XTread_socket. */
856
857 Lisp_Object previous_help_echo_string;
858
859 /* Null glyph slice */
860
861 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
862
863 /* Platform-independent portion of hourglass implementation. */
864
865 /* Non-zero means we're allowed to display a hourglass pointer. */
866 int display_hourglass_p;
867
868 /* Non-zero means an hourglass cursor is currently shown. */
869 int hourglass_shown_p;
870
871 /* If non-null, an asynchronous timer that, when it expires, displays
872 an hourglass cursor on all frames. */
873 struct atimer *hourglass_atimer;
874
875 /* Number of seconds to wait before displaying an hourglass cursor. */
876 Lisp_Object Vhourglass_delay;
877
878 /* Default number of seconds to wait before displaying an hourglass
879 cursor. */
880 #define DEFAULT_HOURGLASS_DELAY 1
881
882 \f
883 /* Function prototypes. */
884
885 static void setup_for_ellipsis P_ ((struct it *, int));
886 static void mark_window_display_accurate_1 P_ ((struct window *, int));
887 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
888 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
889 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
890 static int redisplay_mode_lines P_ ((Lisp_Object, int));
891 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
892
893 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
894
895 static void handle_line_prefix P_ ((struct it *));
896
897 static void pint2str P_ ((char *, int, int));
898 static void pint2hrstr P_ ((char *, int, int));
899 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
900 struct text_pos));
901 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
902 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
903 static void store_mode_line_noprop_char P_ ((char));
904 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
905 static void x_consider_frame_title P_ ((Lisp_Object));
906 static void handle_stop P_ ((struct it *));
907 static int tool_bar_lines_needed P_ ((struct frame *, int *));
908 static int single_display_spec_intangible_p P_ ((Lisp_Object));
909 static void ensure_echo_area_buffers P_ ((void));
910 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
911 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
912 static int with_echo_area_buffer P_ ((struct window *, int,
913 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
914 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
915 static void clear_garbaged_frames P_ ((void));
916 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
918 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
919 static int display_echo_area P_ ((struct window *));
920 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
921 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
922 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
923 static int string_char_and_length P_ ((const unsigned char *, int *));
924 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
925 struct text_pos));
926 static int compute_window_start_on_continuation_line P_ ((struct window *));
927 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
928 static void insert_left_trunc_glyphs P_ ((struct it *));
929 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
930 Lisp_Object));
931 static void extend_face_to_end_of_line P_ ((struct it *));
932 static int append_space_for_newline P_ ((struct it *, int));
933 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
934 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
935 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
936 static int trailing_whitespace_p P_ ((int));
937 static int message_log_check_duplicate P_ ((int, int, int, int));
938 static void push_it P_ ((struct it *));
939 static void pop_it P_ ((struct it *));
940 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
941 static void select_frame_for_redisplay P_ ((Lisp_Object));
942 static void redisplay_internal P_ ((int));
943 static int echo_area_display P_ ((int));
944 static void redisplay_windows P_ ((Lisp_Object));
945 static void redisplay_window P_ ((Lisp_Object, int));
946 static Lisp_Object redisplay_window_error ();
947 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
948 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
949 static int update_menu_bar P_ ((struct frame *, int, int));
950 static int try_window_reusing_current_matrix P_ ((struct window *));
951 static int try_window_id P_ ((struct window *));
952 static int display_line P_ ((struct it *));
953 static int display_mode_lines P_ ((struct window *));
954 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
955 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
956 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
957 static char *decode_mode_spec P_ ((struct window *, int, int, int,
958 Lisp_Object *));
959 static void display_menu_bar P_ ((struct window *));
960 static int display_count_lines P_ ((int, int, int, int, int *));
961 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
962 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
963 static void compute_line_metrics P_ ((struct it *));
964 static void run_redisplay_end_trigger_hook P_ ((struct it *));
965 static int get_overlay_strings P_ ((struct it *, int));
966 static int get_overlay_strings_1 P_ ((struct it *, int, int));
967 static void next_overlay_string P_ ((struct it *));
968 static void reseat P_ ((struct it *, struct text_pos, int));
969 static void reseat_1 P_ ((struct it *, struct text_pos, int));
970 static void back_to_previous_visible_line_start P_ ((struct it *));
971 void reseat_at_previous_visible_line_start P_ ((struct it *));
972 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
973 static int next_element_from_ellipsis P_ ((struct it *));
974 static int next_element_from_display_vector P_ ((struct it *));
975 static int next_element_from_string P_ ((struct it *));
976 static int next_element_from_c_string P_ ((struct it *));
977 static int next_element_from_buffer P_ ((struct it *));
978 static int next_element_from_composition P_ ((struct it *));
979 static int next_element_from_image P_ ((struct it *));
980 static int next_element_from_stretch P_ ((struct it *));
981 static void load_overlay_strings P_ ((struct it *, int));
982 static int init_from_display_pos P_ ((struct it *, struct window *,
983 struct display_pos *));
984 static void reseat_to_string P_ ((struct it *, unsigned char *,
985 Lisp_Object, int, int, int, int));
986 static enum move_it_result
987 move_it_in_display_line_to (struct it *, EMACS_INT, int,
988 enum move_operation_enum);
989 void move_it_vertically_backward P_ ((struct it *, int));
990 static void init_to_row_start P_ ((struct it *, struct window *,
991 struct glyph_row *));
992 static int init_to_row_end P_ ((struct it *, struct window *,
993 struct glyph_row *));
994 static void back_to_previous_line_start P_ ((struct it *));
995 static int forward_to_next_line_start P_ ((struct it *, int *));
996 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
997 Lisp_Object, int));
998 static struct text_pos string_pos P_ ((int, Lisp_Object));
999 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
1000 static int number_of_chars P_ ((unsigned char *, int));
1001 static void compute_stop_pos P_ ((struct it *));
1002 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1003 Lisp_Object));
1004 static int face_before_or_after_it_pos P_ ((struct it *, int));
1005 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1006 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1007 Lisp_Object, Lisp_Object,
1008 struct text_pos *, int));
1009 static int underlying_face_id P_ ((struct it *));
1010 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1011 struct window *));
1012
1013 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1014 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1015
1016 #ifdef HAVE_WINDOW_SYSTEM
1017
1018 static void update_tool_bar P_ ((struct frame *, int));
1019 static void build_desired_tool_bar_string P_ ((struct frame *f));
1020 static int redisplay_tool_bar P_ ((struct frame *));
1021 static void display_tool_bar_line P_ ((struct it *, int));
1022 static void notice_overwritten_cursor P_ ((struct window *,
1023 enum glyph_row_area,
1024 int, int, int, int));
1025
1026
1027
1028 #endif /* HAVE_WINDOW_SYSTEM */
1029
1030 \f
1031 /***********************************************************************
1032 Window display dimensions
1033 ***********************************************************************/
1034
1035 /* Return the bottom boundary y-position for text lines in window W.
1036 This is the first y position at which a line cannot start.
1037 It is relative to the top of the window.
1038
1039 This is the height of W minus the height of a mode line, if any. */
1040
1041 INLINE int
1042 window_text_bottom_y (w)
1043 struct window *w;
1044 {
1045 int height = WINDOW_TOTAL_HEIGHT (w);
1046
1047 if (WINDOW_WANTS_MODELINE_P (w))
1048 height -= CURRENT_MODE_LINE_HEIGHT (w);
1049 return height;
1050 }
1051
1052 /* Return the pixel width of display area AREA of window W. AREA < 0
1053 means return the total width of W, not including fringes to
1054 the left and right of the window. */
1055
1056 INLINE int
1057 window_box_width (w, area)
1058 struct window *w;
1059 int area;
1060 {
1061 int cols = XFASTINT (w->total_cols);
1062 int pixels = 0;
1063
1064 if (!w->pseudo_window_p)
1065 {
1066 cols -= WINDOW_SCROLL_BAR_COLS (w);
1067
1068 if (area == TEXT_AREA)
1069 {
1070 if (INTEGERP (w->left_margin_cols))
1071 cols -= XFASTINT (w->left_margin_cols);
1072 if (INTEGERP (w->right_margin_cols))
1073 cols -= XFASTINT (w->right_margin_cols);
1074 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1075 }
1076 else if (area == LEFT_MARGIN_AREA)
1077 {
1078 cols = (INTEGERP (w->left_margin_cols)
1079 ? XFASTINT (w->left_margin_cols) : 0);
1080 pixels = 0;
1081 }
1082 else if (area == RIGHT_MARGIN_AREA)
1083 {
1084 cols = (INTEGERP (w->right_margin_cols)
1085 ? XFASTINT (w->right_margin_cols) : 0);
1086 pixels = 0;
1087 }
1088 }
1089
1090 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1091 }
1092
1093
1094 /* Return the pixel height of the display area of window W, not
1095 including mode lines of W, if any. */
1096
1097 INLINE int
1098 window_box_height (w)
1099 struct window *w;
1100 {
1101 struct frame *f = XFRAME (w->frame);
1102 int height = WINDOW_TOTAL_HEIGHT (w);
1103
1104 xassert (height >= 0);
1105
1106 /* Note: the code below that determines the mode-line/header-line
1107 height is essentially the same as that contained in the macro
1108 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1109 the appropriate glyph row has its `mode_line_p' flag set,
1110 and if it doesn't, uses estimate_mode_line_height instead. */
1111
1112 if (WINDOW_WANTS_MODELINE_P (w))
1113 {
1114 struct glyph_row *ml_row
1115 = (w->current_matrix && w->current_matrix->rows
1116 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1117 : 0);
1118 if (ml_row && ml_row->mode_line_p)
1119 height -= ml_row->height;
1120 else
1121 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1122 }
1123
1124 if (WINDOW_WANTS_HEADER_LINE_P (w))
1125 {
1126 struct glyph_row *hl_row
1127 = (w->current_matrix && w->current_matrix->rows
1128 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1129 : 0);
1130 if (hl_row && hl_row->mode_line_p)
1131 height -= hl_row->height;
1132 else
1133 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1134 }
1135
1136 /* With a very small font and a mode-line that's taller than
1137 default, we might end up with a negative height. */
1138 return max (0, height);
1139 }
1140
1141 /* Return the window-relative coordinate of the left edge of display
1142 area AREA of window W. AREA < 0 means return the left edge of the
1143 whole window, to the right of the left fringe of W. */
1144
1145 INLINE int
1146 window_box_left_offset (w, area)
1147 struct window *w;
1148 int area;
1149 {
1150 int x;
1151
1152 if (w->pseudo_window_p)
1153 return 0;
1154
1155 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1156
1157 if (area == TEXT_AREA)
1158 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1159 + window_box_width (w, LEFT_MARGIN_AREA));
1160 else if (area == RIGHT_MARGIN_AREA)
1161 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1162 + window_box_width (w, LEFT_MARGIN_AREA)
1163 + window_box_width (w, TEXT_AREA)
1164 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1165 ? 0
1166 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1167 else if (area == LEFT_MARGIN_AREA
1168 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1169 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1170
1171 return x;
1172 }
1173
1174
1175 /* Return the window-relative coordinate of the right edge of display
1176 area AREA of window W. AREA < 0 means return the left edge of the
1177 whole window, to the left of the right fringe of W. */
1178
1179 INLINE int
1180 window_box_right_offset (w, area)
1181 struct window *w;
1182 int area;
1183 {
1184 return window_box_left_offset (w, area) + window_box_width (w, area);
1185 }
1186
1187 /* Return the frame-relative coordinate of the left edge of display
1188 area AREA of window W. AREA < 0 means return the left edge of the
1189 whole window, to the right of the left fringe of W. */
1190
1191 INLINE int
1192 window_box_left (w, area)
1193 struct window *w;
1194 int area;
1195 {
1196 struct frame *f = XFRAME (w->frame);
1197 int x;
1198
1199 if (w->pseudo_window_p)
1200 return FRAME_INTERNAL_BORDER_WIDTH (f);
1201
1202 x = (WINDOW_LEFT_EDGE_X (w)
1203 + window_box_left_offset (w, area));
1204
1205 return x;
1206 }
1207
1208
1209 /* Return the frame-relative coordinate of the right edge of display
1210 area AREA of window W. AREA < 0 means return the left edge of the
1211 whole window, to the left of the right fringe of W. */
1212
1213 INLINE int
1214 window_box_right (w, area)
1215 struct window *w;
1216 int area;
1217 {
1218 return window_box_left (w, area) + window_box_width (w, area);
1219 }
1220
1221 /* Get the bounding box of the display area AREA of window W, without
1222 mode lines, in frame-relative coordinates. AREA < 0 means the
1223 whole window, not including the left and right fringes of
1224 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1225 coordinates of the upper-left corner of the box. Return in
1226 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1227
1228 INLINE void
1229 window_box (w, area, box_x, box_y, box_width, box_height)
1230 struct window *w;
1231 int area;
1232 int *box_x, *box_y, *box_width, *box_height;
1233 {
1234 if (box_width)
1235 *box_width = window_box_width (w, area);
1236 if (box_height)
1237 *box_height = window_box_height (w);
1238 if (box_x)
1239 *box_x = window_box_left (w, area);
1240 if (box_y)
1241 {
1242 *box_y = WINDOW_TOP_EDGE_Y (w);
1243 if (WINDOW_WANTS_HEADER_LINE_P (w))
1244 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1245 }
1246 }
1247
1248
1249 /* Get the bounding box of the display area AREA of window W, without
1250 mode lines. AREA < 0 means the whole window, not including the
1251 left and right fringe of the window. Return in *TOP_LEFT_X
1252 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1253 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1254 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1255 box. */
1256
1257 INLINE void
1258 window_box_edges (w, area, top_left_x, top_left_y,
1259 bottom_right_x, bottom_right_y)
1260 struct window *w;
1261 int area;
1262 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1263 {
1264 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1265 bottom_right_y);
1266 *bottom_right_x += *top_left_x;
1267 *bottom_right_y += *top_left_y;
1268 }
1269
1270
1271 \f
1272 /***********************************************************************
1273 Utilities
1274 ***********************************************************************/
1275
1276 /* Return the bottom y-position of the line the iterator IT is in.
1277 This can modify IT's settings. */
1278
1279 int
1280 line_bottom_y (it)
1281 struct it *it;
1282 {
1283 int line_height = it->max_ascent + it->max_descent;
1284 int line_top_y = it->current_y;
1285
1286 if (line_height == 0)
1287 {
1288 if (last_height)
1289 line_height = last_height;
1290 else if (IT_CHARPOS (*it) < ZV)
1291 {
1292 move_it_by_lines (it, 1, 1);
1293 line_height = (it->max_ascent || it->max_descent
1294 ? it->max_ascent + it->max_descent
1295 : last_height);
1296 }
1297 else
1298 {
1299 struct glyph_row *row = it->glyph_row;
1300
1301 /* Use the default character height. */
1302 it->glyph_row = NULL;
1303 it->what = IT_CHARACTER;
1304 it->c = ' ';
1305 it->len = 1;
1306 PRODUCE_GLYPHS (it);
1307 line_height = it->ascent + it->descent;
1308 it->glyph_row = row;
1309 }
1310 }
1311
1312 return line_top_y + line_height;
1313 }
1314
1315
1316 /* Return 1 if position CHARPOS is visible in window W.
1317 CHARPOS < 0 means return info about WINDOW_END position.
1318 If visible, set *X and *Y to pixel coordinates of top left corner.
1319 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1320 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1321
1322 int
1323 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1324 struct window *w;
1325 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1326 {
1327 struct it it;
1328 struct text_pos top;
1329 int visible_p = 0;
1330 struct buffer *old_buffer = NULL;
1331
1332 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1333 return visible_p;
1334
1335 if (XBUFFER (w->buffer) != current_buffer)
1336 {
1337 old_buffer = current_buffer;
1338 set_buffer_internal_1 (XBUFFER (w->buffer));
1339 }
1340
1341 SET_TEXT_POS_FROM_MARKER (top, w->start);
1342
1343 /* Compute exact mode line heights. */
1344 if (WINDOW_WANTS_MODELINE_P (w))
1345 current_mode_line_height
1346 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1347 current_buffer->mode_line_format);
1348
1349 if (WINDOW_WANTS_HEADER_LINE_P (w))
1350 current_header_line_height
1351 = display_mode_line (w, HEADER_LINE_FACE_ID,
1352 current_buffer->header_line_format);
1353
1354 start_display (&it, w, top);
1355 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1356 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1357
1358 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1359 {
1360 /* We have reached CHARPOS, or passed it. How the call to
1361 move_it_to can overshoot: (i) If CHARPOS is on invisible
1362 text, move_it_to stops at the end of the invisible text,
1363 after CHARPOS. (ii) If CHARPOS is in a display vector,
1364 move_it_to stops on its last glyph. */
1365 int top_x = it.current_x;
1366 int top_y = it.current_y;
1367 enum it_method it_method = it.method;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 int bottom_y = (last_height = 0, line_bottom_y (&it));
1370 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1371
1372 if (top_y < window_top_y)
1373 visible_p = bottom_y > window_top_y;
1374 else if (top_y < it.last_visible_y)
1375 visible_p = 1;
1376 if (visible_p)
1377 {
1378 if (it_method == GET_FROM_DISPLAY_VECTOR)
1379 {
1380 /* We stopped on the last glyph of a display vector.
1381 Try and recompute. Hack alert! */
1382 if (charpos < 2 || top.charpos >= charpos)
1383 top_x = it.glyph_row->x;
1384 else
1385 {
1386 struct it it2;
1387 start_display (&it2, w, top);
1388 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1389 get_next_display_element (&it2);
1390 PRODUCE_GLYPHS (&it2);
1391 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1392 || it2.current_x > it2.last_visible_x)
1393 top_x = it.glyph_row->x;
1394 else
1395 {
1396 top_x = it2.current_x;
1397 top_y = it2.current_y;
1398 }
1399 }
1400 }
1401
1402 *x = top_x;
1403 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1404 *rtop = max (0, window_top_y - top_y);
1405 *rbot = max (0, bottom_y - it.last_visible_y);
1406 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1407 - max (top_y, window_top_y)));
1408 *vpos = it.vpos;
1409 }
1410 }
1411 else
1412 {
1413 struct it it2;
1414
1415 it2 = it;
1416 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1417 move_it_by_lines (&it, 1, 0);
1418 if (charpos < IT_CHARPOS (it)
1419 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1420 {
1421 visible_p = 1;
1422 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1423 *x = it2.current_x;
1424 *y = it2.current_y + it2.max_ascent - it2.ascent;
1425 *rtop = max (0, -it2.current_y);
1426 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1427 - it.last_visible_y));
1428 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1429 it.last_visible_y)
1430 - max (it2.current_y,
1431 WINDOW_HEADER_LINE_HEIGHT (w))));
1432 *vpos = it2.vpos;
1433 }
1434 }
1435
1436 if (old_buffer)
1437 set_buffer_internal_1 (old_buffer);
1438
1439 current_header_line_height = current_mode_line_height = -1;
1440
1441 if (visible_p && XFASTINT (w->hscroll) > 0)
1442 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1443
1444 #if 0
1445 /* Debugging code. */
1446 if (visible_p)
1447 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1448 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1449 else
1450 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1451 #endif
1452
1453 return visible_p;
1454 }
1455
1456
1457 /* Return the next character from STR. Return in *LEN the length of
1458 the character. This is like STRING_CHAR_AND_LENGTH but never
1459 returns an invalid character. If we find one, we return a `?', but
1460 with the length of the invalid character. */
1461
1462 static INLINE int
1463 string_char_and_length (str, len)
1464 const unsigned char *str;
1465 int *len;
1466 {
1467 int c;
1468
1469 c = STRING_CHAR_AND_LENGTH (str, *len);
1470 if (!CHAR_VALID_P (c, 1))
1471 /* We may not change the length here because other places in Emacs
1472 don't use this function, i.e. they silently accept invalid
1473 characters. */
1474 c = '?';
1475
1476 return c;
1477 }
1478
1479
1480
1481 /* Given a position POS containing a valid character and byte position
1482 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1483
1484 static struct text_pos
1485 string_pos_nchars_ahead (pos, string, nchars)
1486 struct text_pos pos;
1487 Lisp_Object string;
1488 int nchars;
1489 {
1490 xassert (STRINGP (string) && nchars >= 0);
1491
1492 if (STRING_MULTIBYTE (string))
1493 {
1494 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1495 int len;
1496
1497 while (nchars--)
1498 {
1499 string_char_and_length (p, &len);
1500 p += len;
1501 CHARPOS (pos) += 1;
1502 BYTEPOS (pos) += len;
1503 }
1504 }
1505 else
1506 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1507
1508 return pos;
1509 }
1510
1511
1512 /* Value is the text position, i.e. character and byte position,
1513 for character position CHARPOS in STRING. */
1514
1515 static INLINE struct text_pos
1516 string_pos (charpos, string)
1517 int charpos;
1518 Lisp_Object string;
1519 {
1520 struct text_pos pos;
1521 xassert (STRINGP (string));
1522 xassert (charpos >= 0);
1523 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1524 return pos;
1525 }
1526
1527
1528 /* Value is a text position, i.e. character and byte position, for
1529 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1530 means recognize multibyte characters. */
1531
1532 static struct text_pos
1533 c_string_pos (charpos, s, multibyte_p)
1534 int charpos;
1535 unsigned char *s;
1536 int multibyte_p;
1537 {
1538 struct text_pos pos;
1539
1540 xassert (s != NULL);
1541 xassert (charpos >= 0);
1542
1543 if (multibyte_p)
1544 {
1545 int len;
1546
1547 SET_TEXT_POS (pos, 0, 0);
1548 while (charpos--)
1549 {
1550 string_char_and_length (s, &len);
1551 s += len;
1552 CHARPOS (pos) += 1;
1553 BYTEPOS (pos) += len;
1554 }
1555 }
1556 else
1557 SET_TEXT_POS (pos, charpos, charpos);
1558
1559 return pos;
1560 }
1561
1562
1563 /* Value is the number of characters in C string S. MULTIBYTE_P
1564 non-zero means recognize multibyte characters. */
1565
1566 static int
1567 number_of_chars (s, multibyte_p)
1568 unsigned char *s;
1569 int multibyte_p;
1570 {
1571 int nchars;
1572
1573 if (multibyte_p)
1574 {
1575 int rest = strlen (s), len;
1576 unsigned char *p = (unsigned char *) s;
1577
1578 for (nchars = 0; rest > 0; ++nchars)
1579 {
1580 string_char_and_length (p, &len);
1581 rest -= len, p += len;
1582 }
1583 }
1584 else
1585 nchars = strlen (s);
1586
1587 return nchars;
1588 }
1589
1590
1591 /* Compute byte position NEWPOS->bytepos corresponding to
1592 NEWPOS->charpos. POS is a known position in string STRING.
1593 NEWPOS->charpos must be >= POS.charpos. */
1594
1595 static void
1596 compute_string_pos (newpos, pos, string)
1597 struct text_pos *newpos, pos;
1598 Lisp_Object string;
1599 {
1600 xassert (STRINGP (string));
1601 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1602
1603 if (STRING_MULTIBYTE (string))
1604 *newpos = string_pos_nchars_ahead (pos, string,
1605 CHARPOS (*newpos) - CHARPOS (pos));
1606 else
1607 BYTEPOS (*newpos) = CHARPOS (*newpos);
1608 }
1609
1610 /* EXPORT:
1611 Return an estimation of the pixel height of mode or header lines on
1612 frame F. FACE_ID specifies what line's height to estimate. */
1613
1614 int
1615 estimate_mode_line_height (f, face_id)
1616 struct frame *f;
1617 enum face_id face_id;
1618 {
1619 #ifdef HAVE_WINDOW_SYSTEM
1620 if (FRAME_WINDOW_P (f))
1621 {
1622 int height = FONT_HEIGHT (FRAME_FONT (f));
1623
1624 /* This function is called so early when Emacs starts that the face
1625 cache and mode line face are not yet initialized. */
1626 if (FRAME_FACE_CACHE (f))
1627 {
1628 struct face *face = FACE_FROM_ID (f, face_id);
1629 if (face)
1630 {
1631 if (face->font)
1632 height = FONT_HEIGHT (face->font);
1633 if (face->box_line_width > 0)
1634 height += 2 * face->box_line_width;
1635 }
1636 }
1637
1638 return height;
1639 }
1640 #endif
1641
1642 return 1;
1643 }
1644
1645 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1646 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1647 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1648 not force the value into range. */
1649
1650 void
1651 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1652 FRAME_PTR f;
1653 register int pix_x, pix_y;
1654 int *x, *y;
1655 NativeRectangle *bounds;
1656 int noclip;
1657 {
1658
1659 #ifdef HAVE_WINDOW_SYSTEM
1660 if (FRAME_WINDOW_P (f))
1661 {
1662 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1663 even for negative values. */
1664 if (pix_x < 0)
1665 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1666 if (pix_y < 0)
1667 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1668
1669 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1670 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1671
1672 if (bounds)
1673 STORE_NATIVE_RECT (*bounds,
1674 FRAME_COL_TO_PIXEL_X (f, pix_x),
1675 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1676 FRAME_COLUMN_WIDTH (f) - 1,
1677 FRAME_LINE_HEIGHT (f) - 1);
1678
1679 if (!noclip)
1680 {
1681 if (pix_x < 0)
1682 pix_x = 0;
1683 else if (pix_x > FRAME_TOTAL_COLS (f))
1684 pix_x = FRAME_TOTAL_COLS (f);
1685
1686 if (pix_y < 0)
1687 pix_y = 0;
1688 else if (pix_y > FRAME_LINES (f))
1689 pix_y = FRAME_LINES (f);
1690 }
1691 }
1692 #endif
1693
1694 *x = pix_x;
1695 *y = pix_y;
1696 }
1697
1698
1699 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1700 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1701 can't tell the positions because W's display is not up to date,
1702 return 0. */
1703
1704 int
1705 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1706 struct window *w;
1707 int hpos, vpos;
1708 int *frame_x, *frame_y;
1709 {
1710 #ifdef HAVE_WINDOW_SYSTEM
1711 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1712 {
1713 int success_p;
1714
1715 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1716 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1717
1718 if (display_completed)
1719 {
1720 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1721 struct glyph *glyph = row->glyphs[TEXT_AREA];
1722 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1723
1724 hpos = row->x;
1725 vpos = row->y;
1726 while (glyph < end)
1727 {
1728 hpos += glyph->pixel_width;
1729 ++glyph;
1730 }
1731
1732 /* If first glyph is partially visible, its first visible position is still 0. */
1733 if (hpos < 0)
1734 hpos = 0;
1735
1736 success_p = 1;
1737 }
1738 else
1739 {
1740 hpos = vpos = 0;
1741 success_p = 0;
1742 }
1743
1744 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1745 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1746 return success_p;
1747 }
1748 #endif
1749
1750 *frame_x = hpos;
1751 *frame_y = vpos;
1752 return 1;
1753 }
1754
1755
1756 #ifdef HAVE_WINDOW_SYSTEM
1757
1758 /* Find the glyph under window-relative coordinates X/Y in window W.
1759 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1760 strings. Return in *HPOS and *VPOS the row and column number of
1761 the glyph found. Return in *AREA the glyph area containing X.
1762 Value is a pointer to the glyph found or null if X/Y is not on
1763 text, or we can't tell because W's current matrix is not up to
1764 date. */
1765
1766 static
1767 struct glyph *
1768 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1769 struct window *w;
1770 int x, y;
1771 int *hpos, *vpos, *dx, *dy, *area;
1772 {
1773 struct glyph *glyph, *end;
1774 struct glyph_row *row = NULL;
1775 int x0, i;
1776
1777 /* Find row containing Y. Give up if some row is not enabled. */
1778 for (i = 0; i < w->current_matrix->nrows; ++i)
1779 {
1780 row = MATRIX_ROW (w->current_matrix, i);
1781 if (!row->enabled_p)
1782 return NULL;
1783 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1784 break;
1785 }
1786
1787 *vpos = i;
1788 *hpos = 0;
1789
1790 /* Give up if Y is not in the window. */
1791 if (i == w->current_matrix->nrows)
1792 return NULL;
1793
1794 /* Get the glyph area containing X. */
1795 if (w->pseudo_window_p)
1796 {
1797 *area = TEXT_AREA;
1798 x0 = 0;
1799 }
1800 else
1801 {
1802 if (x < window_box_left_offset (w, TEXT_AREA))
1803 {
1804 *area = LEFT_MARGIN_AREA;
1805 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1806 }
1807 else if (x < window_box_right_offset (w, TEXT_AREA))
1808 {
1809 *area = TEXT_AREA;
1810 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1811 }
1812 else
1813 {
1814 *area = RIGHT_MARGIN_AREA;
1815 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1816 }
1817 }
1818
1819 /* Find glyph containing X. */
1820 glyph = row->glyphs[*area];
1821 end = glyph + row->used[*area];
1822 x -= x0;
1823 while (glyph < end && x >= glyph->pixel_width)
1824 {
1825 x -= glyph->pixel_width;
1826 ++glyph;
1827 }
1828
1829 if (glyph == end)
1830 return NULL;
1831
1832 if (dx)
1833 {
1834 *dx = x;
1835 *dy = y - (row->y + row->ascent - glyph->ascent);
1836 }
1837
1838 *hpos = glyph - row->glyphs[*area];
1839 return glyph;
1840 }
1841
1842
1843 /* EXPORT:
1844 Convert frame-relative x/y to coordinates relative to window W.
1845 Takes pseudo-windows into account. */
1846
1847 void
1848 frame_to_window_pixel_xy (w, x, y)
1849 struct window *w;
1850 int *x, *y;
1851 {
1852 if (w->pseudo_window_p)
1853 {
1854 /* A pseudo-window is always full-width, and starts at the
1855 left edge of the frame, plus a frame border. */
1856 struct frame *f = XFRAME (w->frame);
1857 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1858 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1859 }
1860 else
1861 {
1862 *x -= WINDOW_LEFT_EDGE_X (w);
1863 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1864 }
1865 }
1866
1867 /* EXPORT:
1868 Return in RECTS[] at most N clipping rectangles for glyph string S.
1869 Return the number of stored rectangles. */
1870
1871 int
1872 get_glyph_string_clip_rects (s, rects, n)
1873 struct glyph_string *s;
1874 NativeRectangle *rects;
1875 int n;
1876 {
1877 XRectangle r;
1878
1879 if (n <= 0)
1880 return 0;
1881
1882 if (s->row->full_width_p)
1883 {
1884 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1885 r.x = WINDOW_LEFT_EDGE_X (s->w);
1886 r.width = WINDOW_TOTAL_WIDTH (s->w);
1887
1888 /* Unless displaying a mode or menu bar line, which are always
1889 fully visible, clip to the visible part of the row. */
1890 if (s->w->pseudo_window_p)
1891 r.height = s->row->visible_height;
1892 else
1893 r.height = s->height;
1894 }
1895 else
1896 {
1897 /* This is a text line that may be partially visible. */
1898 r.x = window_box_left (s->w, s->area);
1899 r.width = window_box_width (s->w, s->area);
1900 r.height = s->row->visible_height;
1901 }
1902
1903 if (s->clip_head)
1904 if (r.x < s->clip_head->x)
1905 {
1906 if (r.width >= s->clip_head->x - r.x)
1907 r.width -= s->clip_head->x - r.x;
1908 else
1909 r.width = 0;
1910 r.x = s->clip_head->x;
1911 }
1912 if (s->clip_tail)
1913 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1914 {
1915 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1916 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1917 else
1918 r.width = 0;
1919 }
1920
1921 /* If S draws overlapping rows, it's sufficient to use the top and
1922 bottom of the window for clipping because this glyph string
1923 intentionally draws over other lines. */
1924 if (s->for_overlaps)
1925 {
1926 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1927 r.height = window_text_bottom_y (s->w) - r.y;
1928
1929 /* Alas, the above simple strategy does not work for the
1930 environments with anti-aliased text: if the same text is
1931 drawn onto the same place multiple times, it gets thicker.
1932 If the overlap we are processing is for the erased cursor, we
1933 take the intersection with the rectagle of the cursor. */
1934 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1935 {
1936 XRectangle rc, r_save = r;
1937
1938 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1939 rc.y = s->w->phys_cursor.y;
1940 rc.width = s->w->phys_cursor_width;
1941 rc.height = s->w->phys_cursor_height;
1942
1943 x_intersect_rectangles (&r_save, &rc, &r);
1944 }
1945 }
1946 else
1947 {
1948 /* Don't use S->y for clipping because it doesn't take partially
1949 visible lines into account. For example, it can be negative for
1950 partially visible lines at the top of a window. */
1951 if (!s->row->full_width_p
1952 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1953 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1954 else
1955 r.y = max (0, s->row->y);
1956 }
1957
1958 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1959
1960 /* If drawing the cursor, don't let glyph draw outside its
1961 advertised boundaries. Cleartype does this under some circumstances. */
1962 if (s->hl == DRAW_CURSOR)
1963 {
1964 struct glyph *glyph = s->first_glyph;
1965 int height, max_y;
1966
1967 if (s->x > r.x)
1968 {
1969 r.width -= s->x - r.x;
1970 r.x = s->x;
1971 }
1972 r.width = min (r.width, glyph->pixel_width);
1973
1974 /* If r.y is below window bottom, ensure that we still see a cursor. */
1975 height = min (glyph->ascent + glyph->descent,
1976 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1977 max_y = window_text_bottom_y (s->w) - height;
1978 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1979 if (s->ybase - glyph->ascent > max_y)
1980 {
1981 r.y = max_y;
1982 r.height = height;
1983 }
1984 else
1985 {
1986 /* Don't draw cursor glyph taller than our actual glyph. */
1987 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1988 if (height < r.height)
1989 {
1990 max_y = r.y + r.height;
1991 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1992 r.height = min (max_y - r.y, height);
1993 }
1994 }
1995 }
1996
1997 if (s->row->clip)
1998 {
1999 XRectangle r_save = r;
2000
2001 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2002 r.width = 0;
2003 }
2004
2005 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2006 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2007 {
2008 #ifdef CONVERT_FROM_XRECT
2009 CONVERT_FROM_XRECT (r, *rects);
2010 #else
2011 *rects = r;
2012 #endif
2013 return 1;
2014 }
2015 else
2016 {
2017 /* If we are processing overlapping and allowed to return
2018 multiple clipping rectangles, we exclude the row of the glyph
2019 string from the clipping rectangle. This is to avoid drawing
2020 the same text on the environment with anti-aliasing. */
2021 #ifdef CONVERT_FROM_XRECT
2022 XRectangle rs[2];
2023 #else
2024 XRectangle *rs = rects;
2025 #endif
2026 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2027
2028 if (s->for_overlaps & OVERLAPS_PRED)
2029 {
2030 rs[i] = r;
2031 if (r.y + r.height > row_y)
2032 {
2033 if (r.y < row_y)
2034 rs[i].height = row_y - r.y;
2035 else
2036 rs[i].height = 0;
2037 }
2038 i++;
2039 }
2040 if (s->for_overlaps & OVERLAPS_SUCC)
2041 {
2042 rs[i] = r;
2043 if (r.y < row_y + s->row->visible_height)
2044 {
2045 if (r.y + r.height > row_y + s->row->visible_height)
2046 {
2047 rs[i].y = row_y + s->row->visible_height;
2048 rs[i].height = r.y + r.height - rs[i].y;
2049 }
2050 else
2051 rs[i].height = 0;
2052 }
2053 i++;
2054 }
2055
2056 n = i;
2057 #ifdef CONVERT_FROM_XRECT
2058 for (i = 0; i < n; i++)
2059 CONVERT_FROM_XRECT (rs[i], rects[i]);
2060 #endif
2061 return n;
2062 }
2063 }
2064
2065 /* EXPORT:
2066 Return in *NR the clipping rectangle for glyph string S. */
2067
2068 void
2069 get_glyph_string_clip_rect (s, nr)
2070 struct glyph_string *s;
2071 NativeRectangle *nr;
2072 {
2073 get_glyph_string_clip_rects (s, nr, 1);
2074 }
2075
2076
2077 /* EXPORT:
2078 Return the position and height of the phys cursor in window W.
2079 Set w->phys_cursor_width to width of phys cursor.
2080 */
2081
2082 void
2083 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2084 struct window *w;
2085 struct glyph_row *row;
2086 struct glyph *glyph;
2087 int *xp, *yp, *heightp;
2088 {
2089 struct frame *f = XFRAME (WINDOW_FRAME (w));
2090 int x, y, wd, h, h0, y0;
2091
2092 /* Compute the width of the rectangle to draw. If on a stretch
2093 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2094 rectangle as wide as the glyph, but use a canonical character
2095 width instead. */
2096 wd = glyph->pixel_width - 1;
2097 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2098 wd++; /* Why? */
2099 #endif
2100
2101 x = w->phys_cursor.x;
2102 if (x < 0)
2103 {
2104 wd += x;
2105 x = 0;
2106 }
2107
2108 if (glyph->type == STRETCH_GLYPH
2109 && !x_stretch_cursor_p)
2110 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2111 w->phys_cursor_width = wd;
2112
2113 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2114
2115 /* If y is below window bottom, ensure that we still see a cursor. */
2116 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2117
2118 h = max (h0, glyph->ascent + glyph->descent);
2119 h0 = min (h0, glyph->ascent + glyph->descent);
2120
2121 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2122 if (y < y0)
2123 {
2124 h = max (h - (y0 - y) + 1, h0);
2125 y = y0 - 1;
2126 }
2127 else
2128 {
2129 y0 = window_text_bottom_y (w) - h0;
2130 if (y > y0)
2131 {
2132 h += y - y0;
2133 y = y0;
2134 }
2135 }
2136
2137 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2138 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2139 *heightp = h;
2140 }
2141
2142 /*
2143 * Remember which glyph the mouse is over.
2144 */
2145
2146 void
2147 remember_mouse_glyph (f, gx, gy, rect)
2148 struct frame *f;
2149 int gx, gy;
2150 NativeRectangle *rect;
2151 {
2152 Lisp_Object window;
2153 struct window *w;
2154 struct glyph_row *r, *gr, *end_row;
2155 enum window_part part;
2156 enum glyph_row_area area;
2157 int x, y, width, height;
2158
2159 /* Try to determine frame pixel position and size of the glyph under
2160 frame pixel coordinates X/Y on frame F. */
2161
2162 if (!f->glyphs_initialized_p
2163 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2164 NILP (window)))
2165 {
2166 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2167 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2168 goto virtual_glyph;
2169 }
2170
2171 w = XWINDOW (window);
2172 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2173 height = WINDOW_FRAME_LINE_HEIGHT (w);
2174
2175 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2176 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2177
2178 if (w->pseudo_window_p)
2179 {
2180 area = TEXT_AREA;
2181 part = ON_MODE_LINE; /* Don't adjust margin. */
2182 goto text_glyph;
2183 }
2184
2185 switch (part)
2186 {
2187 case ON_LEFT_MARGIN:
2188 area = LEFT_MARGIN_AREA;
2189 goto text_glyph;
2190
2191 case ON_RIGHT_MARGIN:
2192 area = RIGHT_MARGIN_AREA;
2193 goto text_glyph;
2194
2195 case ON_HEADER_LINE:
2196 case ON_MODE_LINE:
2197 gr = (part == ON_HEADER_LINE
2198 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2199 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2200 gy = gr->y;
2201 area = TEXT_AREA;
2202 goto text_glyph_row_found;
2203
2204 case ON_TEXT:
2205 area = TEXT_AREA;
2206
2207 text_glyph:
2208 gr = 0; gy = 0;
2209 for (; r <= end_row && r->enabled_p; ++r)
2210 if (r->y + r->height > y)
2211 {
2212 gr = r; gy = r->y;
2213 break;
2214 }
2215
2216 text_glyph_row_found:
2217 if (gr && gy <= y)
2218 {
2219 struct glyph *g = gr->glyphs[area];
2220 struct glyph *end = g + gr->used[area];
2221
2222 height = gr->height;
2223 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2224 if (gx + g->pixel_width > x)
2225 break;
2226
2227 if (g < end)
2228 {
2229 if (g->type == IMAGE_GLYPH)
2230 {
2231 /* Don't remember when mouse is over image, as
2232 image may have hot-spots. */
2233 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2234 return;
2235 }
2236 width = g->pixel_width;
2237 }
2238 else
2239 {
2240 /* Use nominal char spacing at end of line. */
2241 x -= gx;
2242 gx += (x / width) * width;
2243 }
2244
2245 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2246 gx += window_box_left_offset (w, area);
2247 }
2248 else
2249 {
2250 /* Use nominal line height at end of window. */
2251 gx = (x / width) * width;
2252 y -= gy;
2253 gy += (y / height) * height;
2254 }
2255 break;
2256
2257 case ON_LEFT_FRINGE:
2258 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2259 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2260 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2261 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2262 goto row_glyph;
2263
2264 case ON_RIGHT_FRINGE:
2265 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2266 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2267 : window_box_right_offset (w, TEXT_AREA));
2268 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2269 goto row_glyph;
2270
2271 case ON_SCROLL_BAR:
2272 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2273 ? 0
2274 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2275 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2276 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2277 : 0)));
2278 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2279
2280 row_glyph:
2281 gr = 0, gy = 0;
2282 for (; r <= end_row && r->enabled_p; ++r)
2283 if (r->y + r->height > y)
2284 {
2285 gr = r; gy = r->y;
2286 break;
2287 }
2288
2289 if (gr && gy <= y)
2290 height = gr->height;
2291 else
2292 {
2293 /* Use nominal line height at end of window. */
2294 y -= gy;
2295 gy += (y / height) * height;
2296 }
2297 break;
2298
2299 default:
2300 ;
2301 virtual_glyph:
2302 /* If there is no glyph under the mouse, then we divide the screen
2303 into a grid of the smallest glyph in the frame, and use that
2304 as our "glyph". */
2305
2306 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2307 round down even for negative values. */
2308 if (gx < 0)
2309 gx -= width - 1;
2310 if (gy < 0)
2311 gy -= height - 1;
2312
2313 gx = (gx / width) * width;
2314 gy = (gy / height) * height;
2315
2316 goto store_rect;
2317 }
2318
2319 gx += WINDOW_LEFT_EDGE_X (w);
2320 gy += WINDOW_TOP_EDGE_Y (w);
2321
2322 store_rect:
2323 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2324
2325 /* Visible feedback for debugging. */
2326 #if 0
2327 #if HAVE_X_WINDOWS
2328 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2329 f->output_data.x->normal_gc,
2330 gx, gy, width, height);
2331 #endif
2332 #endif
2333 }
2334
2335
2336 #endif /* HAVE_WINDOW_SYSTEM */
2337
2338 \f
2339 /***********************************************************************
2340 Lisp form evaluation
2341 ***********************************************************************/
2342
2343 /* Error handler for safe_eval and safe_call. */
2344
2345 static Lisp_Object
2346 safe_eval_handler (arg)
2347 Lisp_Object arg;
2348 {
2349 add_to_log ("Error during redisplay: %s", arg, Qnil);
2350 return Qnil;
2351 }
2352
2353
2354 /* Evaluate SEXPR and return the result, or nil if something went
2355 wrong. Prevent redisplay during the evaluation. */
2356
2357 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2358 Return the result, or nil if something went wrong. Prevent
2359 redisplay during the evaluation. */
2360
2361 Lisp_Object
2362 safe_call (nargs, args)
2363 int nargs;
2364 Lisp_Object *args;
2365 {
2366 Lisp_Object val;
2367
2368 if (inhibit_eval_during_redisplay)
2369 val = Qnil;
2370 else
2371 {
2372 int count = SPECPDL_INDEX ();
2373 struct gcpro gcpro1;
2374
2375 GCPRO1 (args[0]);
2376 gcpro1.nvars = nargs;
2377 specbind (Qinhibit_redisplay, Qt);
2378 /* Use Qt to ensure debugger does not run,
2379 so there is no possibility of wanting to redisplay. */
2380 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2381 safe_eval_handler);
2382 UNGCPRO;
2383 val = unbind_to (count, val);
2384 }
2385
2386 return val;
2387 }
2388
2389
2390 /* Call function FN with one argument ARG.
2391 Return the result, or nil if something went wrong. */
2392
2393 Lisp_Object
2394 safe_call1 (fn, arg)
2395 Lisp_Object fn, arg;
2396 {
2397 Lisp_Object args[2];
2398 args[0] = fn;
2399 args[1] = arg;
2400 return safe_call (2, args);
2401 }
2402
2403 static Lisp_Object Qeval;
2404
2405 Lisp_Object
2406 safe_eval (Lisp_Object sexpr)
2407 {
2408 return safe_call1 (Qeval, sexpr);
2409 }
2410
2411 /* Call function FN with one argument ARG.
2412 Return the result, or nil if something went wrong. */
2413
2414 Lisp_Object
2415 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2416 {
2417 Lisp_Object args[3];
2418 args[0] = fn;
2419 args[1] = arg1;
2420 args[2] = arg2;
2421 return safe_call (3, args);
2422 }
2423
2424
2425 \f
2426 /***********************************************************************
2427 Debugging
2428 ***********************************************************************/
2429
2430 #if 0
2431
2432 /* Define CHECK_IT to perform sanity checks on iterators.
2433 This is for debugging. It is too slow to do unconditionally. */
2434
2435 static void
2436 check_it (it)
2437 struct it *it;
2438 {
2439 if (it->method == GET_FROM_STRING)
2440 {
2441 xassert (STRINGP (it->string));
2442 xassert (IT_STRING_CHARPOS (*it) >= 0);
2443 }
2444 else
2445 {
2446 xassert (IT_STRING_CHARPOS (*it) < 0);
2447 if (it->method == GET_FROM_BUFFER)
2448 {
2449 /* Check that character and byte positions agree. */
2450 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2451 }
2452 }
2453
2454 if (it->dpvec)
2455 xassert (it->current.dpvec_index >= 0);
2456 else
2457 xassert (it->current.dpvec_index < 0);
2458 }
2459
2460 #define CHECK_IT(IT) check_it ((IT))
2461
2462 #else /* not 0 */
2463
2464 #define CHECK_IT(IT) (void) 0
2465
2466 #endif /* not 0 */
2467
2468
2469 #if GLYPH_DEBUG
2470
2471 /* Check that the window end of window W is what we expect it
2472 to be---the last row in the current matrix displaying text. */
2473
2474 static void
2475 check_window_end (w)
2476 struct window *w;
2477 {
2478 if (!MINI_WINDOW_P (w)
2479 && !NILP (w->window_end_valid))
2480 {
2481 struct glyph_row *row;
2482 xassert ((row = MATRIX_ROW (w->current_matrix,
2483 XFASTINT (w->window_end_vpos)),
2484 !row->enabled_p
2485 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2486 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2487 }
2488 }
2489
2490 #define CHECK_WINDOW_END(W) check_window_end ((W))
2491
2492 #else /* not GLYPH_DEBUG */
2493
2494 #define CHECK_WINDOW_END(W) (void) 0
2495
2496 #endif /* not GLYPH_DEBUG */
2497
2498
2499 \f
2500 /***********************************************************************
2501 Iterator initialization
2502 ***********************************************************************/
2503
2504 /* Initialize IT for displaying current_buffer in window W, starting
2505 at character position CHARPOS. CHARPOS < 0 means that no buffer
2506 position is specified which is useful when the iterator is assigned
2507 a position later. BYTEPOS is the byte position corresponding to
2508 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2509
2510 If ROW is not null, calls to produce_glyphs with IT as parameter
2511 will produce glyphs in that row.
2512
2513 BASE_FACE_ID is the id of a base face to use. It must be one of
2514 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2515 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2516 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2517
2518 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2519 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2520 will be initialized to use the corresponding mode line glyph row of
2521 the desired matrix of W. */
2522
2523 void
2524 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2525 struct it *it;
2526 struct window *w;
2527 int charpos, bytepos;
2528 struct glyph_row *row;
2529 enum face_id base_face_id;
2530 {
2531 int highlight_region_p;
2532 enum face_id remapped_base_face_id = base_face_id;
2533
2534 /* Some precondition checks. */
2535 xassert (w != NULL && it != NULL);
2536 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2537 && charpos <= ZV));
2538
2539 /* If face attributes have been changed since the last redisplay,
2540 free realized faces now because they depend on face definitions
2541 that might have changed. Don't free faces while there might be
2542 desired matrices pending which reference these faces. */
2543 if (face_change_count && !inhibit_free_realized_faces)
2544 {
2545 face_change_count = 0;
2546 free_all_realized_faces (Qnil);
2547 }
2548
2549 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2550 if (! NILP (Vface_remapping_alist))
2551 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2552
2553 /* Use one of the mode line rows of W's desired matrix if
2554 appropriate. */
2555 if (row == NULL)
2556 {
2557 if (base_face_id == MODE_LINE_FACE_ID
2558 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2559 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2560 else if (base_face_id == HEADER_LINE_FACE_ID)
2561 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2562 }
2563
2564 /* Clear IT. */
2565 bzero (it, sizeof *it);
2566 it->current.overlay_string_index = -1;
2567 it->current.dpvec_index = -1;
2568 it->base_face_id = remapped_base_face_id;
2569 it->string = Qnil;
2570 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2571
2572 /* The window in which we iterate over current_buffer: */
2573 XSETWINDOW (it->window, w);
2574 it->w = w;
2575 it->f = XFRAME (w->frame);
2576
2577 it->cmp_it.id = -1;
2578
2579 /* Extra space between lines (on window systems only). */
2580 if (base_face_id == DEFAULT_FACE_ID
2581 && FRAME_WINDOW_P (it->f))
2582 {
2583 if (NATNUMP (current_buffer->extra_line_spacing))
2584 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2585 else if (FLOATP (current_buffer->extra_line_spacing))
2586 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2587 * FRAME_LINE_HEIGHT (it->f));
2588 else if (it->f->extra_line_spacing > 0)
2589 it->extra_line_spacing = it->f->extra_line_spacing;
2590 it->max_extra_line_spacing = 0;
2591 }
2592
2593 /* If realized faces have been removed, e.g. because of face
2594 attribute changes of named faces, recompute them. When running
2595 in batch mode, the face cache of the initial frame is null. If
2596 we happen to get called, make a dummy face cache. */
2597 if (FRAME_FACE_CACHE (it->f) == NULL)
2598 init_frame_faces (it->f);
2599 if (FRAME_FACE_CACHE (it->f)->used == 0)
2600 recompute_basic_faces (it->f);
2601
2602 /* Current value of the `slice', `space-width', and 'height' properties. */
2603 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2604 it->space_width = Qnil;
2605 it->font_height = Qnil;
2606 it->override_ascent = -1;
2607
2608 /* Are control characters displayed as `^C'? */
2609 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2610
2611 /* -1 means everything between a CR and the following line end
2612 is invisible. >0 means lines indented more than this value are
2613 invisible. */
2614 it->selective = (INTEGERP (current_buffer->selective_display)
2615 ? XFASTINT (current_buffer->selective_display)
2616 : (!NILP (current_buffer->selective_display)
2617 ? -1 : 0));
2618 it->selective_display_ellipsis_p
2619 = !NILP (current_buffer->selective_display_ellipses);
2620
2621 /* Display table to use. */
2622 it->dp = window_display_table (w);
2623
2624 /* Are multibyte characters enabled in current_buffer? */
2625 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2626
2627 /* Non-zero if we should highlight the region. */
2628 highlight_region_p
2629 = (!NILP (Vtransient_mark_mode)
2630 && !NILP (current_buffer->mark_active)
2631 && XMARKER (current_buffer->mark)->buffer != 0);
2632
2633 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2634 start and end of a visible region in window IT->w. Set both to
2635 -1 to indicate no region. */
2636 if (highlight_region_p
2637 /* Maybe highlight only in selected window. */
2638 && (/* Either show region everywhere. */
2639 highlight_nonselected_windows
2640 /* Or show region in the selected window. */
2641 || w == XWINDOW (selected_window)
2642 /* Or show the region if we are in the mini-buffer and W is
2643 the window the mini-buffer refers to. */
2644 || (MINI_WINDOW_P (XWINDOW (selected_window))
2645 && WINDOWP (minibuf_selected_window)
2646 && w == XWINDOW (minibuf_selected_window))))
2647 {
2648 int charpos = marker_position (current_buffer->mark);
2649 it->region_beg_charpos = min (PT, charpos);
2650 it->region_end_charpos = max (PT, charpos);
2651 }
2652 else
2653 it->region_beg_charpos = it->region_end_charpos = -1;
2654
2655 /* Get the position at which the redisplay_end_trigger hook should
2656 be run, if it is to be run at all. */
2657 if (MARKERP (w->redisplay_end_trigger)
2658 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2659 it->redisplay_end_trigger_charpos
2660 = marker_position (w->redisplay_end_trigger);
2661 else if (INTEGERP (w->redisplay_end_trigger))
2662 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2663
2664 /* Correct bogus values of tab_width. */
2665 it->tab_width = XINT (current_buffer->tab_width);
2666 if (it->tab_width <= 0 || it->tab_width > 1000)
2667 it->tab_width = 8;
2668
2669 /* Are lines in the display truncated? */
2670 if (base_face_id != DEFAULT_FACE_ID
2671 || XINT (it->w->hscroll)
2672 || (! WINDOW_FULL_WIDTH_P (it->w)
2673 && ((!NILP (Vtruncate_partial_width_windows)
2674 && !INTEGERP (Vtruncate_partial_width_windows))
2675 || (INTEGERP (Vtruncate_partial_width_windows)
2676 && (WINDOW_TOTAL_COLS (it->w)
2677 < XINT (Vtruncate_partial_width_windows))))))
2678 it->line_wrap = TRUNCATE;
2679 else if (NILP (current_buffer->truncate_lines))
2680 it->line_wrap = NILP (current_buffer->word_wrap)
2681 ? WINDOW_WRAP : WORD_WRAP;
2682 else
2683 it->line_wrap = TRUNCATE;
2684
2685 /* Get dimensions of truncation and continuation glyphs. These are
2686 displayed as fringe bitmaps under X, so we don't need them for such
2687 frames. */
2688 if (!FRAME_WINDOW_P (it->f))
2689 {
2690 if (it->line_wrap == TRUNCATE)
2691 {
2692 /* We will need the truncation glyph. */
2693 xassert (it->glyph_row == NULL);
2694 produce_special_glyphs (it, IT_TRUNCATION);
2695 it->truncation_pixel_width = it->pixel_width;
2696 }
2697 else
2698 {
2699 /* We will need the continuation glyph. */
2700 xassert (it->glyph_row == NULL);
2701 produce_special_glyphs (it, IT_CONTINUATION);
2702 it->continuation_pixel_width = it->pixel_width;
2703 }
2704
2705 /* Reset these values to zero because the produce_special_glyphs
2706 above has changed them. */
2707 it->pixel_width = it->ascent = it->descent = 0;
2708 it->phys_ascent = it->phys_descent = 0;
2709 }
2710
2711 /* Set this after getting the dimensions of truncation and
2712 continuation glyphs, so that we don't produce glyphs when calling
2713 produce_special_glyphs, above. */
2714 it->glyph_row = row;
2715 it->area = TEXT_AREA;
2716
2717 /* Get the dimensions of the display area. The display area
2718 consists of the visible window area plus a horizontally scrolled
2719 part to the left of the window. All x-values are relative to the
2720 start of this total display area. */
2721 if (base_face_id != DEFAULT_FACE_ID)
2722 {
2723 /* Mode lines, menu bar in terminal frames. */
2724 it->first_visible_x = 0;
2725 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2726 }
2727 else
2728 {
2729 it->first_visible_x
2730 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2731 it->last_visible_x = (it->first_visible_x
2732 + window_box_width (w, TEXT_AREA));
2733
2734 /* If we truncate lines, leave room for the truncator glyph(s) at
2735 the right margin. Otherwise, leave room for the continuation
2736 glyph(s). Truncation and continuation glyphs are not inserted
2737 for window-based redisplay. */
2738 if (!FRAME_WINDOW_P (it->f))
2739 {
2740 if (it->line_wrap == TRUNCATE)
2741 it->last_visible_x -= it->truncation_pixel_width;
2742 else
2743 it->last_visible_x -= it->continuation_pixel_width;
2744 }
2745
2746 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2747 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2748 }
2749
2750 /* Leave room for a border glyph. */
2751 if (!FRAME_WINDOW_P (it->f)
2752 && !WINDOW_RIGHTMOST_P (it->w))
2753 it->last_visible_x -= 1;
2754
2755 it->last_visible_y = window_text_bottom_y (w);
2756
2757 /* For mode lines and alike, arrange for the first glyph having a
2758 left box line if the face specifies a box. */
2759 if (base_face_id != DEFAULT_FACE_ID)
2760 {
2761 struct face *face;
2762
2763 it->face_id = remapped_base_face_id;
2764
2765 /* If we have a boxed mode line, make the first character appear
2766 with a left box line. */
2767 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2768 if (face->box != FACE_NO_BOX)
2769 it->start_of_box_run_p = 1;
2770 }
2771
2772 /* If a buffer position was specified, set the iterator there,
2773 getting overlays and face properties from that position. */
2774 if (charpos >= BUF_BEG (current_buffer))
2775 {
2776 it->end_charpos = ZV;
2777 it->face_id = -1;
2778 IT_CHARPOS (*it) = charpos;
2779
2780 /* Compute byte position if not specified. */
2781 if (bytepos < charpos)
2782 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2783 else
2784 IT_BYTEPOS (*it) = bytepos;
2785
2786 it->start = it->current;
2787
2788 /* Compute faces etc. */
2789 reseat (it, it->current.pos, 1);
2790 }
2791
2792 CHECK_IT (it);
2793 }
2794
2795
2796 /* Initialize IT for the display of window W with window start POS. */
2797
2798 void
2799 start_display (it, w, pos)
2800 struct it *it;
2801 struct window *w;
2802 struct text_pos pos;
2803 {
2804 struct glyph_row *row;
2805 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2806
2807 row = w->desired_matrix->rows + first_vpos;
2808 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2809 it->first_vpos = first_vpos;
2810
2811 /* Don't reseat to previous visible line start if current start
2812 position is in a string or image. */
2813 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2814 {
2815 int start_at_line_beg_p;
2816 int first_y = it->current_y;
2817
2818 /* If window start is not at a line start, skip forward to POS to
2819 get the correct continuation lines width. */
2820 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2821 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2822 if (!start_at_line_beg_p)
2823 {
2824 int new_x;
2825
2826 reseat_at_previous_visible_line_start (it);
2827 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2828
2829 new_x = it->current_x + it->pixel_width;
2830
2831 /* If lines are continued, this line may end in the middle
2832 of a multi-glyph character (e.g. a control character
2833 displayed as \003, or in the middle of an overlay
2834 string). In this case move_it_to above will not have
2835 taken us to the start of the continuation line but to the
2836 end of the continued line. */
2837 if (it->current_x > 0
2838 && it->line_wrap != TRUNCATE /* Lines are continued. */
2839 && (/* And glyph doesn't fit on the line. */
2840 new_x > it->last_visible_x
2841 /* Or it fits exactly and we're on a window
2842 system frame. */
2843 || (new_x == it->last_visible_x
2844 && FRAME_WINDOW_P (it->f))))
2845 {
2846 if (it->current.dpvec_index >= 0
2847 || it->current.overlay_string_index >= 0)
2848 {
2849 set_iterator_to_next (it, 1);
2850 move_it_in_display_line_to (it, -1, -1, 0);
2851 }
2852
2853 it->continuation_lines_width += it->current_x;
2854 }
2855
2856 /* We're starting a new display line, not affected by the
2857 height of the continued line, so clear the appropriate
2858 fields in the iterator structure. */
2859 it->max_ascent = it->max_descent = 0;
2860 it->max_phys_ascent = it->max_phys_descent = 0;
2861
2862 it->current_y = first_y;
2863 it->vpos = 0;
2864 it->current_x = it->hpos = 0;
2865 }
2866 }
2867 }
2868
2869
2870 /* Return 1 if POS is a position in ellipses displayed for invisible
2871 text. W is the window we display, for text property lookup. */
2872
2873 static int
2874 in_ellipses_for_invisible_text_p (pos, w)
2875 struct display_pos *pos;
2876 struct window *w;
2877 {
2878 Lisp_Object prop, window;
2879 int ellipses_p = 0;
2880 int charpos = CHARPOS (pos->pos);
2881
2882 /* If POS specifies a position in a display vector, this might
2883 be for an ellipsis displayed for invisible text. We won't
2884 get the iterator set up for delivering that ellipsis unless
2885 we make sure that it gets aware of the invisible text. */
2886 if (pos->dpvec_index >= 0
2887 && pos->overlay_string_index < 0
2888 && CHARPOS (pos->string_pos) < 0
2889 && charpos > BEGV
2890 && (XSETWINDOW (window, w),
2891 prop = Fget_char_property (make_number (charpos),
2892 Qinvisible, window),
2893 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2894 {
2895 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2896 window);
2897 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2898 }
2899
2900 return ellipses_p;
2901 }
2902
2903
2904 /* Initialize IT for stepping through current_buffer in window W,
2905 starting at position POS that includes overlay string and display
2906 vector/ control character translation position information. Value
2907 is zero if there are overlay strings with newlines at POS. */
2908
2909 static int
2910 init_from_display_pos (it, w, pos)
2911 struct it *it;
2912 struct window *w;
2913 struct display_pos *pos;
2914 {
2915 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2916 int i, overlay_strings_with_newlines = 0;
2917
2918 /* If POS specifies a position in a display vector, this might
2919 be for an ellipsis displayed for invisible text. We won't
2920 get the iterator set up for delivering that ellipsis unless
2921 we make sure that it gets aware of the invisible text. */
2922 if (in_ellipses_for_invisible_text_p (pos, w))
2923 {
2924 --charpos;
2925 bytepos = 0;
2926 }
2927
2928 /* Keep in mind: the call to reseat in init_iterator skips invisible
2929 text, so we might end up at a position different from POS. This
2930 is only a problem when POS is a row start after a newline and an
2931 overlay starts there with an after-string, and the overlay has an
2932 invisible property. Since we don't skip invisible text in
2933 display_line and elsewhere immediately after consuming the
2934 newline before the row start, such a POS will not be in a string,
2935 but the call to init_iterator below will move us to the
2936 after-string. */
2937 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2938
2939 /* This only scans the current chunk -- it should scan all chunks.
2940 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2941 to 16 in 22.1 to make this a lesser problem. */
2942 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2943 {
2944 const char *s = SDATA (it->overlay_strings[i]);
2945 const char *e = s + SBYTES (it->overlay_strings[i]);
2946
2947 while (s < e && *s != '\n')
2948 ++s;
2949
2950 if (s < e)
2951 {
2952 overlay_strings_with_newlines = 1;
2953 break;
2954 }
2955 }
2956
2957 /* If position is within an overlay string, set up IT to the right
2958 overlay string. */
2959 if (pos->overlay_string_index >= 0)
2960 {
2961 int relative_index;
2962
2963 /* If the first overlay string happens to have a `display'
2964 property for an image, the iterator will be set up for that
2965 image, and we have to undo that setup first before we can
2966 correct the overlay string index. */
2967 if (it->method == GET_FROM_IMAGE)
2968 pop_it (it);
2969
2970 /* We already have the first chunk of overlay strings in
2971 IT->overlay_strings. Load more until the one for
2972 pos->overlay_string_index is in IT->overlay_strings. */
2973 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2974 {
2975 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2976 it->current.overlay_string_index = 0;
2977 while (n--)
2978 {
2979 load_overlay_strings (it, 0);
2980 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2981 }
2982 }
2983
2984 it->current.overlay_string_index = pos->overlay_string_index;
2985 relative_index = (it->current.overlay_string_index
2986 % OVERLAY_STRING_CHUNK_SIZE);
2987 it->string = it->overlay_strings[relative_index];
2988 xassert (STRINGP (it->string));
2989 it->current.string_pos = pos->string_pos;
2990 it->method = GET_FROM_STRING;
2991 }
2992
2993 if (CHARPOS (pos->string_pos) >= 0)
2994 {
2995 /* Recorded position is not in an overlay string, but in another
2996 string. This can only be a string from a `display' property.
2997 IT should already be filled with that string. */
2998 it->current.string_pos = pos->string_pos;
2999 xassert (STRINGP (it->string));
3000 }
3001
3002 /* Restore position in display vector translations, control
3003 character translations or ellipses. */
3004 if (pos->dpvec_index >= 0)
3005 {
3006 if (it->dpvec == NULL)
3007 get_next_display_element (it);
3008 xassert (it->dpvec && it->current.dpvec_index == 0);
3009 it->current.dpvec_index = pos->dpvec_index;
3010 }
3011
3012 CHECK_IT (it);
3013 return !overlay_strings_with_newlines;
3014 }
3015
3016
3017 /* Initialize IT for stepping through current_buffer in window W
3018 starting at ROW->start. */
3019
3020 static void
3021 init_to_row_start (it, w, row)
3022 struct it *it;
3023 struct window *w;
3024 struct glyph_row *row;
3025 {
3026 init_from_display_pos (it, w, &row->start);
3027 it->start = row->start;
3028 it->continuation_lines_width = row->continuation_lines_width;
3029 CHECK_IT (it);
3030 }
3031
3032
3033 /* Initialize IT for stepping through current_buffer in window W
3034 starting in the line following ROW, i.e. starting at ROW->end.
3035 Value is zero if there are overlay strings with newlines at ROW's
3036 end position. */
3037
3038 static int
3039 init_to_row_end (it, w, row)
3040 struct it *it;
3041 struct window *w;
3042 struct glyph_row *row;
3043 {
3044 int success = 0;
3045
3046 if (init_from_display_pos (it, w, &row->end))
3047 {
3048 if (row->continued_p)
3049 it->continuation_lines_width
3050 = row->continuation_lines_width + row->pixel_width;
3051 CHECK_IT (it);
3052 success = 1;
3053 }
3054
3055 return success;
3056 }
3057
3058
3059
3060 \f
3061 /***********************************************************************
3062 Text properties
3063 ***********************************************************************/
3064
3065 /* Called when IT reaches IT->stop_charpos. Handle text property and
3066 overlay changes. Set IT->stop_charpos to the next position where
3067 to stop. */
3068
3069 static void
3070 handle_stop (it)
3071 struct it *it;
3072 {
3073 enum prop_handled handled;
3074 int handle_overlay_change_p;
3075 struct props *p;
3076
3077 it->dpvec = NULL;
3078 it->current.dpvec_index = -1;
3079 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3080 it->ignore_overlay_strings_at_pos_p = 0;
3081 it->ellipsis_p = 0;
3082
3083 /* Use face of preceding text for ellipsis (if invisible) */
3084 if (it->selective_display_ellipsis_p)
3085 it->saved_face_id = it->face_id;
3086
3087 do
3088 {
3089 handled = HANDLED_NORMALLY;
3090
3091 /* Call text property handlers. */
3092 for (p = it_props; p->handler; ++p)
3093 {
3094 handled = p->handler (it);
3095
3096 if (handled == HANDLED_RECOMPUTE_PROPS)
3097 break;
3098 else if (handled == HANDLED_RETURN)
3099 {
3100 /* We still want to show before and after strings from
3101 overlays even if the actual buffer text is replaced. */
3102 if (!handle_overlay_change_p
3103 || it->sp > 1
3104 || !get_overlay_strings_1 (it, 0, 0))
3105 {
3106 if (it->ellipsis_p)
3107 setup_for_ellipsis (it, 0);
3108 /* When handling a display spec, we might load an
3109 empty string. In that case, discard it here. We
3110 used to discard it in handle_single_display_spec,
3111 but that causes get_overlay_strings_1, above, to
3112 ignore overlay strings that we must check. */
3113 if (STRINGP (it->string) && !SCHARS (it->string))
3114 pop_it (it);
3115 return;
3116 }
3117 else if (STRINGP (it->string) && !SCHARS (it->string))
3118 pop_it (it);
3119 else
3120 {
3121 it->ignore_overlay_strings_at_pos_p = 1;
3122 it->string_from_display_prop_p = 0;
3123 handle_overlay_change_p = 0;
3124 }
3125 handled = HANDLED_RECOMPUTE_PROPS;
3126 break;
3127 }
3128 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3129 handle_overlay_change_p = 0;
3130 }
3131
3132 if (handled != HANDLED_RECOMPUTE_PROPS)
3133 {
3134 /* Don't check for overlay strings below when set to deliver
3135 characters from a display vector. */
3136 if (it->method == GET_FROM_DISPLAY_VECTOR)
3137 handle_overlay_change_p = 0;
3138
3139 /* Handle overlay changes.
3140 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3141 if it finds overlays. */
3142 if (handle_overlay_change_p)
3143 handled = handle_overlay_change (it);
3144 }
3145
3146 if (it->ellipsis_p)
3147 {
3148 setup_for_ellipsis (it, 0);
3149 break;
3150 }
3151 }
3152 while (handled == HANDLED_RECOMPUTE_PROPS);
3153
3154 /* Determine where to stop next. */
3155 if (handled == HANDLED_NORMALLY)
3156 compute_stop_pos (it);
3157 }
3158
3159
3160 /* Compute IT->stop_charpos from text property and overlay change
3161 information for IT's current position. */
3162
3163 static void
3164 compute_stop_pos (it)
3165 struct it *it;
3166 {
3167 register INTERVAL iv, next_iv;
3168 Lisp_Object object, limit, position;
3169 EMACS_INT charpos, bytepos;
3170
3171 /* If nowhere else, stop at the end. */
3172 it->stop_charpos = it->end_charpos;
3173
3174 if (STRINGP (it->string))
3175 {
3176 /* Strings are usually short, so don't limit the search for
3177 properties. */
3178 object = it->string;
3179 limit = Qnil;
3180 charpos = IT_STRING_CHARPOS (*it);
3181 bytepos = IT_STRING_BYTEPOS (*it);
3182 }
3183 else
3184 {
3185 EMACS_INT pos;
3186
3187 /* If next overlay change is in front of the current stop pos
3188 (which is IT->end_charpos), stop there. Note: value of
3189 next_overlay_change is point-max if no overlay change
3190 follows. */
3191 charpos = IT_CHARPOS (*it);
3192 bytepos = IT_BYTEPOS (*it);
3193 pos = next_overlay_change (charpos);
3194 if (pos < it->stop_charpos)
3195 it->stop_charpos = pos;
3196
3197 /* If showing the region, we have to stop at the region
3198 start or end because the face might change there. */
3199 if (it->region_beg_charpos > 0)
3200 {
3201 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3202 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3203 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3204 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3205 }
3206
3207 /* Set up variables for computing the stop position from text
3208 property changes. */
3209 XSETBUFFER (object, current_buffer);
3210 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3211 }
3212
3213 /* Get the interval containing IT's position. Value is a null
3214 interval if there isn't such an interval. */
3215 position = make_number (charpos);
3216 iv = validate_interval_range (object, &position, &position, 0);
3217 if (!NULL_INTERVAL_P (iv))
3218 {
3219 Lisp_Object values_here[LAST_PROP_IDX];
3220 struct props *p;
3221
3222 /* Get properties here. */
3223 for (p = it_props; p->handler; ++p)
3224 values_here[p->idx] = textget (iv->plist, *p->name);
3225
3226 /* Look for an interval following iv that has different
3227 properties. */
3228 for (next_iv = next_interval (iv);
3229 (!NULL_INTERVAL_P (next_iv)
3230 && (NILP (limit)
3231 || XFASTINT (limit) > next_iv->position));
3232 next_iv = next_interval (next_iv))
3233 {
3234 for (p = it_props; p->handler; ++p)
3235 {
3236 Lisp_Object new_value;
3237
3238 new_value = textget (next_iv->plist, *p->name);
3239 if (!EQ (values_here[p->idx], new_value))
3240 break;
3241 }
3242
3243 if (p->handler)
3244 break;
3245 }
3246
3247 if (!NULL_INTERVAL_P (next_iv))
3248 {
3249 if (INTEGERP (limit)
3250 && next_iv->position >= XFASTINT (limit))
3251 /* No text property change up to limit. */
3252 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3253 else
3254 /* Text properties change in next_iv. */
3255 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3256 }
3257 }
3258
3259 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3260 it->stop_charpos, it->string);
3261
3262 xassert (STRINGP (it->string)
3263 || (it->stop_charpos >= BEGV
3264 && it->stop_charpos >= IT_CHARPOS (*it)));
3265 }
3266
3267
3268 /* Return the position of the next overlay change after POS in
3269 current_buffer. Value is point-max if no overlay change
3270 follows. This is like `next-overlay-change' but doesn't use
3271 xmalloc. */
3272
3273 static EMACS_INT
3274 next_overlay_change (pos)
3275 EMACS_INT pos;
3276 {
3277 int noverlays;
3278 EMACS_INT endpos;
3279 Lisp_Object *overlays;
3280 int i;
3281
3282 /* Get all overlays at the given position. */
3283 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3284
3285 /* If any of these overlays ends before endpos,
3286 use its ending point instead. */
3287 for (i = 0; i < noverlays; ++i)
3288 {
3289 Lisp_Object oend;
3290 EMACS_INT oendpos;
3291
3292 oend = OVERLAY_END (overlays[i]);
3293 oendpos = OVERLAY_POSITION (oend);
3294 endpos = min (endpos, oendpos);
3295 }
3296
3297 return endpos;
3298 }
3299
3300
3301 \f
3302 /***********************************************************************
3303 Fontification
3304 ***********************************************************************/
3305
3306 /* Handle changes in the `fontified' property of the current buffer by
3307 calling hook functions from Qfontification_functions to fontify
3308 regions of text. */
3309
3310 static enum prop_handled
3311 handle_fontified_prop (it)
3312 struct it *it;
3313 {
3314 Lisp_Object prop, pos;
3315 enum prop_handled handled = HANDLED_NORMALLY;
3316
3317 if (!NILP (Vmemory_full))
3318 return handled;
3319
3320 /* Get the value of the `fontified' property at IT's current buffer
3321 position. (The `fontified' property doesn't have a special
3322 meaning in strings.) If the value is nil, call functions from
3323 Qfontification_functions. */
3324 if (!STRINGP (it->string)
3325 && it->s == NULL
3326 && !NILP (Vfontification_functions)
3327 && !NILP (Vrun_hooks)
3328 && (pos = make_number (IT_CHARPOS (*it)),
3329 prop = Fget_char_property (pos, Qfontified, Qnil),
3330 /* Ignore the special cased nil value always present at EOB since
3331 no amount of fontifying will be able to change it. */
3332 NILP (prop) && IT_CHARPOS (*it) < Z))
3333 {
3334 int count = SPECPDL_INDEX ();
3335 Lisp_Object val;
3336
3337 val = Vfontification_functions;
3338 specbind (Qfontification_functions, Qnil);
3339
3340 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3341 safe_call1 (val, pos);
3342 else
3343 {
3344 Lisp_Object globals, fn;
3345 struct gcpro gcpro1, gcpro2;
3346
3347 globals = Qnil;
3348 GCPRO2 (val, globals);
3349
3350 for (; CONSP (val); val = XCDR (val))
3351 {
3352 fn = XCAR (val);
3353
3354 if (EQ (fn, Qt))
3355 {
3356 /* A value of t indicates this hook has a local
3357 binding; it means to run the global binding too.
3358 In a global value, t should not occur. If it
3359 does, we must ignore it to avoid an endless
3360 loop. */
3361 for (globals = Fdefault_value (Qfontification_functions);
3362 CONSP (globals);
3363 globals = XCDR (globals))
3364 {
3365 fn = XCAR (globals);
3366 if (!EQ (fn, Qt))
3367 safe_call1 (fn, pos);
3368 }
3369 }
3370 else
3371 safe_call1 (fn, pos);
3372 }
3373
3374 UNGCPRO;
3375 }
3376
3377 unbind_to (count, Qnil);
3378
3379 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3380 something. This avoids an endless loop if they failed to
3381 fontify the text for which reason ever. */
3382 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3383 handled = HANDLED_RECOMPUTE_PROPS;
3384 }
3385
3386 return handled;
3387 }
3388
3389
3390 \f
3391 /***********************************************************************
3392 Faces
3393 ***********************************************************************/
3394
3395 /* Set up iterator IT from face properties at its current position.
3396 Called from handle_stop. */
3397
3398 static enum prop_handled
3399 handle_face_prop (it)
3400 struct it *it;
3401 {
3402 int new_face_id;
3403 EMACS_INT next_stop;
3404
3405 if (!STRINGP (it->string))
3406 {
3407 new_face_id
3408 = face_at_buffer_position (it->w,
3409 IT_CHARPOS (*it),
3410 it->region_beg_charpos,
3411 it->region_end_charpos,
3412 &next_stop,
3413 (IT_CHARPOS (*it)
3414 + TEXT_PROP_DISTANCE_LIMIT),
3415 0, it->base_face_id);
3416
3417 /* Is this a start of a run of characters with box face?
3418 Caveat: this can be called for a freshly initialized
3419 iterator; face_id is -1 in this case. We know that the new
3420 face will not change until limit, i.e. if the new face has a
3421 box, all characters up to limit will have one. But, as
3422 usual, we don't know whether limit is really the end. */
3423 if (new_face_id != it->face_id)
3424 {
3425 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3426
3427 /* If new face has a box but old face has not, this is
3428 the start of a run of characters with box, i.e. it has
3429 a shadow on the left side. The value of face_id of the
3430 iterator will be -1 if this is the initial call that gets
3431 the face. In this case, we have to look in front of IT's
3432 position and see whether there is a face != new_face_id. */
3433 it->start_of_box_run_p
3434 = (new_face->box != FACE_NO_BOX
3435 && (it->face_id >= 0
3436 || IT_CHARPOS (*it) == BEG
3437 || new_face_id != face_before_it_pos (it)));
3438 it->face_box_p = new_face->box != FACE_NO_BOX;
3439 }
3440 }
3441 else
3442 {
3443 int base_face_id, bufpos;
3444 int i;
3445 Lisp_Object from_overlay
3446 = (it->current.overlay_string_index >= 0
3447 ? it->string_overlays[it->current.overlay_string_index]
3448 : Qnil);
3449
3450 /* See if we got to this string directly or indirectly from
3451 an overlay property. That includes the before-string or
3452 after-string of an overlay, strings in display properties
3453 provided by an overlay, their text properties, etc.
3454
3455 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3456 if (! NILP (from_overlay))
3457 for (i = it->sp - 1; i >= 0; i--)
3458 {
3459 if (it->stack[i].current.overlay_string_index >= 0)
3460 from_overlay
3461 = it->string_overlays[it->stack[i].current.overlay_string_index];
3462 else if (! NILP (it->stack[i].from_overlay))
3463 from_overlay = it->stack[i].from_overlay;
3464
3465 if (!NILP (from_overlay))
3466 break;
3467 }
3468
3469 if (! NILP (from_overlay))
3470 {
3471 bufpos = IT_CHARPOS (*it);
3472 /* For a string from an overlay, the base face depends
3473 only on text properties and ignores overlays. */
3474 base_face_id
3475 = face_for_overlay_string (it->w,
3476 IT_CHARPOS (*it),
3477 it->region_beg_charpos,
3478 it->region_end_charpos,
3479 &next_stop,
3480 (IT_CHARPOS (*it)
3481 + TEXT_PROP_DISTANCE_LIMIT),
3482 0,
3483 from_overlay);
3484 }
3485 else
3486 {
3487 bufpos = 0;
3488
3489 /* For strings from a `display' property, use the face at
3490 IT's current buffer position as the base face to merge
3491 with, so that overlay strings appear in the same face as
3492 surrounding text, unless they specify their own
3493 faces. */
3494 base_face_id = underlying_face_id (it);
3495 }
3496
3497 new_face_id = face_at_string_position (it->w,
3498 it->string,
3499 IT_STRING_CHARPOS (*it),
3500 bufpos,
3501 it->region_beg_charpos,
3502 it->region_end_charpos,
3503 &next_stop,
3504 base_face_id, 0);
3505
3506 /* Is this a start of a run of characters with box? Caveat:
3507 this can be called for a freshly allocated iterator; face_id
3508 is -1 is this case. We know that the new face will not
3509 change until the next check pos, i.e. if the new face has a
3510 box, all characters up to that position will have a
3511 box. But, as usual, we don't know whether that position
3512 is really the end. */
3513 if (new_face_id != it->face_id)
3514 {
3515 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3516 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3517
3518 /* If new face has a box but old face hasn't, this is the
3519 start of a run of characters with box, i.e. it has a
3520 shadow on the left side. */
3521 it->start_of_box_run_p
3522 = new_face->box && (old_face == NULL || !old_face->box);
3523 it->face_box_p = new_face->box != FACE_NO_BOX;
3524 }
3525 }
3526
3527 it->face_id = new_face_id;
3528 return HANDLED_NORMALLY;
3529 }
3530
3531
3532 /* Return the ID of the face ``underlying'' IT's current position,
3533 which is in a string. If the iterator is associated with a
3534 buffer, return the face at IT's current buffer position.
3535 Otherwise, use the iterator's base_face_id. */
3536
3537 static int
3538 underlying_face_id (it)
3539 struct it *it;
3540 {
3541 int face_id = it->base_face_id, i;
3542
3543 xassert (STRINGP (it->string));
3544
3545 for (i = it->sp - 1; i >= 0; --i)
3546 if (NILP (it->stack[i].string))
3547 face_id = it->stack[i].face_id;
3548
3549 return face_id;
3550 }
3551
3552
3553 /* Compute the face one character before or after the current position
3554 of IT. BEFORE_P non-zero means get the face in front of IT's
3555 position. Value is the id of the face. */
3556
3557 static int
3558 face_before_or_after_it_pos (it, before_p)
3559 struct it *it;
3560 int before_p;
3561 {
3562 int face_id, limit;
3563 EMACS_INT next_check_charpos;
3564 struct text_pos pos;
3565
3566 xassert (it->s == NULL);
3567
3568 if (STRINGP (it->string))
3569 {
3570 int bufpos, base_face_id;
3571
3572 /* No face change past the end of the string (for the case
3573 we are padding with spaces). No face change before the
3574 string start. */
3575 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3576 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3577 return it->face_id;
3578
3579 /* Set pos to the position before or after IT's current position. */
3580 if (before_p)
3581 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3582 else
3583 /* For composition, we must check the character after the
3584 composition. */
3585 pos = (it->what == IT_COMPOSITION
3586 ? string_pos (IT_STRING_CHARPOS (*it)
3587 + it->cmp_it.nchars, it->string)
3588 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3589
3590 if (it->current.overlay_string_index >= 0)
3591 bufpos = IT_CHARPOS (*it);
3592 else
3593 bufpos = 0;
3594
3595 base_face_id = underlying_face_id (it);
3596
3597 /* Get the face for ASCII, or unibyte. */
3598 face_id = face_at_string_position (it->w,
3599 it->string,
3600 CHARPOS (pos),
3601 bufpos,
3602 it->region_beg_charpos,
3603 it->region_end_charpos,
3604 &next_check_charpos,
3605 base_face_id, 0);
3606
3607 /* Correct the face for charsets different from ASCII. Do it
3608 for the multibyte case only. The face returned above is
3609 suitable for unibyte text if IT->string is unibyte. */
3610 if (STRING_MULTIBYTE (it->string))
3611 {
3612 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3613 int c, len;
3614 struct face *face = FACE_FROM_ID (it->f, face_id);
3615
3616 c = string_char_and_length (p, &len);
3617 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3618 }
3619 }
3620 else
3621 {
3622 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3623 || (IT_CHARPOS (*it) <= BEGV && before_p))
3624 return it->face_id;
3625
3626 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3627 pos = it->current.pos;
3628
3629 if (before_p)
3630 DEC_TEXT_POS (pos, it->multibyte_p);
3631 else
3632 {
3633 if (it->what == IT_COMPOSITION)
3634 /* For composition, we must check the position after the
3635 composition. */
3636 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3637 else
3638 INC_TEXT_POS (pos, it->multibyte_p);
3639 }
3640
3641 /* Determine face for CHARSET_ASCII, or unibyte. */
3642 face_id = face_at_buffer_position (it->w,
3643 CHARPOS (pos),
3644 it->region_beg_charpos,
3645 it->region_end_charpos,
3646 &next_check_charpos,
3647 limit, 0, -1);
3648
3649 /* Correct the face for charsets different from ASCII. Do it
3650 for the multibyte case only. The face returned above is
3651 suitable for unibyte text if current_buffer is unibyte. */
3652 if (it->multibyte_p)
3653 {
3654 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3655 struct face *face = FACE_FROM_ID (it->f, face_id);
3656 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3657 }
3658 }
3659
3660 return face_id;
3661 }
3662
3663
3664 \f
3665 /***********************************************************************
3666 Invisible text
3667 ***********************************************************************/
3668
3669 /* Set up iterator IT from invisible properties at its current
3670 position. Called from handle_stop. */
3671
3672 static enum prop_handled
3673 handle_invisible_prop (it)
3674 struct it *it;
3675 {
3676 enum prop_handled handled = HANDLED_NORMALLY;
3677
3678 if (STRINGP (it->string))
3679 {
3680 extern Lisp_Object Qinvisible;
3681 Lisp_Object prop, end_charpos, limit, charpos;
3682
3683 /* Get the value of the invisible text property at the
3684 current position. Value will be nil if there is no such
3685 property. */
3686 charpos = make_number (IT_STRING_CHARPOS (*it));
3687 prop = Fget_text_property (charpos, Qinvisible, it->string);
3688
3689 if (!NILP (prop)
3690 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3691 {
3692 handled = HANDLED_RECOMPUTE_PROPS;
3693
3694 /* Get the position at which the next change of the
3695 invisible text property can be found in IT->string.
3696 Value will be nil if the property value is the same for
3697 all the rest of IT->string. */
3698 XSETINT (limit, SCHARS (it->string));
3699 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3700 it->string, limit);
3701
3702 /* Text at current position is invisible. The next
3703 change in the property is at position end_charpos.
3704 Move IT's current position to that position. */
3705 if (INTEGERP (end_charpos)
3706 && XFASTINT (end_charpos) < XFASTINT (limit))
3707 {
3708 struct text_pos old;
3709 old = it->current.string_pos;
3710 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3711 compute_string_pos (&it->current.string_pos, old, it->string);
3712 }
3713 else
3714 {
3715 /* The rest of the string is invisible. If this is an
3716 overlay string, proceed with the next overlay string
3717 or whatever comes and return a character from there. */
3718 if (it->current.overlay_string_index >= 0)
3719 {
3720 next_overlay_string (it);
3721 /* Don't check for overlay strings when we just
3722 finished processing them. */
3723 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3724 }
3725 else
3726 {
3727 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3728 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3729 }
3730 }
3731 }
3732 }
3733 else
3734 {
3735 int invis_p;
3736 EMACS_INT newpos, next_stop, start_charpos;
3737 Lisp_Object pos, prop, overlay;
3738
3739 /* First of all, is there invisible text at this position? */
3740 start_charpos = IT_CHARPOS (*it);
3741 pos = make_number (IT_CHARPOS (*it));
3742 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3743 &overlay);
3744 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3745
3746 /* If we are on invisible text, skip over it. */
3747 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3748 {
3749 /* Record whether we have to display an ellipsis for the
3750 invisible text. */
3751 int display_ellipsis_p = invis_p == 2;
3752
3753 handled = HANDLED_RECOMPUTE_PROPS;
3754
3755 /* Loop skipping over invisible text. The loop is left at
3756 ZV or with IT on the first char being visible again. */
3757 do
3758 {
3759 /* Try to skip some invisible text. Return value is the
3760 position reached which can be equal to IT's position
3761 if there is nothing invisible here. This skips both
3762 over invisible text properties and overlays with
3763 invisible property. */
3764 newpos = skip_invisible (IT_CHARPOS (*it),
3765 &next_stop, ZV, it->window);
3766
3767 /* If we skipped nothing at all we weren't at invisible
3768 text in the first place. If everything to the end of
3769 the buffer was skipped, end the loop. */
3770 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3771 invis_p = 0;
3772 else
3773 {
3774 /* We skipped some characters but not necessarily
3775 all there are. Check if we ended up on visible
3776 text. Fget_char_property returns the property of
3777 the char before the given position, i.e. if we
3778 get invis_p = 0, this means that the char at
3779 newpos is visible. */
3780 pos = make_number (newpos);
3781 prop = Fget_char_property (pos, Qinvisible, it->window);
3782 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3783 }
3784
3785 /* If we ended up on invisible text, proceed to
3786 skip starting with next_stop. */
3787 if (invis_p)
3788 IT_CHARPOS (*it) = next_stop;
3789
3790 /* If there are adjacent invisible texts, don't lose the
3791 second one's ellipsis. */
3792 if (invis_p == 2)
3793 display_ellipsis_p = 1;
3794 }
3795 while (invis_p);
3796
3797 /* The position newpos is now either ZV or on visible text. */
3798 IT_CHARPOS (*it) = newpos;
3799 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3800
3801 /* If there are before-strings at the start of invisible
3802 text, and the text is invisible because of a text
3803 property, arrange to show before-strings because 20.x did
3804 it that way. (If the text is invisible because of an
3805 overlay property instead of a text property, this is
3806 already handled in the overlay code.) */
3807 if (NILP (overlay)
3808 && get_overlay_strings (it, start_charpos))
3809 {
3810 handled = HANDLED_RECOMPUTE_PROPS;
3811 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3812 }
3813 else if (display_ellipsis_p)
3814 {
3815 /* Make sure that the glyphs of the ellipsis will get
3816 correct `charpos' values. If we would not update
3817 it->position here, the glyphs would belong to the
3818 last visible character _before_ the invisible
3819 text, which confuses `set_cursor_from_row'.
3820
3821 We use the last invisible position instead of the
3822 first because this way the cursor is always drawn on
3823 the first "." of the ellipsis, whenever PT is inside
3824 the invisible text. Otherwise the cursor would be
3825 placed _after_ the ellipsis when the point is after the
3826 first invisible character. */
3827 if (!STRINGP (it->object))
3828 {
3829 it->position.charpos = IT_CHARPOS (*it) - 1;
3830 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3831 }
3832 it->ellipsis_p = 1;
3833 /* Let the ellipsis display before
3834 considering any properties of the following char.
3835 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3836 handled = HANDLED_RETURN;
3837 }
3838 }
3839 }
3840
3841 return handled;
3842 }
3843
3844
3845 /* Make iterator IT return `...' next.
3846 Replaces LEN characters from buffer. */
3847
3848 static void
3849 setup_for_ellipsis (it, len)
3850 struct it *it;
3851 int len;
3852 {
3853 /* Use the display table definition for `...'. Invalid glyphs
3854 will be handled by the method returning elements from dpvec. */
3855 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3856 {
3857 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3858 it->dpvec = v->contents;
3859 it->dpend = v->contents + v->size;
3860 }
3861 else
3862 {
3863 /* Default `...'. */
3864 it->dpvec = default_invis_vector;
3865 it->dpend = default_invis_vector + 3;
3866 }
3867
3868 it->dpvec_char_len = len;
3869 it->current.dpvec_index = 0;
3870 it->dpvec_face_id = -1;
3871
3872 /* Remember the current face id in case glyphs specify faces.
3873 IT's face is restored in set_iterator_to_next.
3874 saved_face_id was set to preceding char's face in handle_stop. */
3875 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3876 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3877
3878 it->method = GET_FROM_DISPLAY_VECTOR;
3879 it->ellipsis_p = 1;
3880 }
3881
3882
3883 \f
3884 /***********************************************************************
3885 'display' property
3886 ***********************************************************************/
3887
3888 /* Set up iterator IT from `display' property at its current position.
3889 Called from handle_stop.
3890 We return HANDLED_RETURN if some part of the display property
3891 overrides the display of the buffer text itself.
3892 Otherwise we return HANDLED_NORMALLY. */
3893
3894 static enum prop_handled
3895 handle_display_prop (it)
3896 struct it *it;
3897 {
3898 Lisp_Object prop, object, overlay;
3899 struct text_pos *position;
3900 /* Nonzero if some property replaces the display of the text itself. */
3901 int display_replaced_p = 0;
3902
3903 if (STRINGP (it->string))
3904 {
3905 object = it->string;
3906 position = &it->current.string_pos;
3907 }
3908 else
3909 {
3910 XSETWINDOW (object, it->w);
3911 position = &it->current.pos;
3912 }
3913
3914 /* Reset those iterator values set from display property values. */
3915 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3916 it->space_width = Qnil;
3917 it->font_height = Qnil;
3918 it->voffset = 0;
3919
3920 /* We don't support recursive `display' properties, i.e. string
3921 values that have a string `display' property, that have a string
3922 `display' property etc. */
3923 if (!it->string_from_display_prop_p)
3924 it->area = TEXT_AREA;
3925
3926 prop = get_char_property_and_overlay (make_number (position->charpos),
3927 Qdisplay, object, &overlay);
3928 if (NILP (prop))
3929 return HANDLED_NORMALLY;
3930 /* Now OVERLAY is the overlay that gave us this property, or nil
3931 if it was a text property. */
3932
3933 if (!STRINGP (it->string))
3934 object = it->w->buffer;
3935
3936 if (CONSP (prop)
3937 /* Simple properties. */
3938 && !EQ (XCAR (prop), Qimage)
3939 && !EQ (XCAR (prop), Qspace)
3940 && !EQ (XCAR (prop), Qwhen)
3941 && !EQ (XCAR (prop), Qslice)
3942 && !EQ (XCAR (prop), Qspace_width)
3943 && !EQ (XCAR (prop), Qheight)
3944 && !EQ (XCAR (prop), Qraise)
3945 /* Marginal area specifications. */
3946 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3947 && !EQ (XCAR (prop), Qleft_fringe)
3948 && !EQ (XCAR (prop), Qright_fringe)
3949 && !NILP (XCAR (prop)))
3950 {
3951 for (; CONSP (prop); prop = XCDR (prop))
3952 {
3953 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3954 position, display_replaced_p))
3955 {
3956 display_replaced_p = 1;
3957 /* If some text in a string is replaced, `position' no
3958 longer points to the position of `object'. */
3959 if (STRINGP (object))
3960 break;
3961 }
3962 }
3963 }
3964 else if (VECTORP (prop))
3965 {
3966 int i;
3967 for (i = 0; i < ASIZE (prop); ++i)
3968 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3969 position, display_replaced_p))
3970 {
3971 display_replaced_p = 1;
3972 /* If some text in a string is replaced, `position' no
3973 longer points to the position of `object'. */
3974 if (STRINGP (object))
3975 break;
3976 }
3977 }
3978 else
3979 {
3980 if (handle_single_display_spec (it, prop, object, overlay,
3981 position, 0))
3982 display_replaced_p = 1;
3983 }
3984
3985 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3986 }
3987
3988
3989 /* Value is the position of the end of the `display' property starting
3990 at START_POS in OBJECT. */
3991
3992 static struct text_pos
3993 display_prop_end (it, object, start_pos)
3994 struct it *it;
3995 Lisp_Object object;
3996 struct text_pos start_pos;
3997 {
3998 Lisp_Object end;
3999 struct text_pos end_pos;
4000
4001 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4002 Qdisplay, object, Qnil);
4003 CHARPOS (end_pos) = XFASTINT (end);
4004 if (STRINGP (object))
4005 compute_string_pos (&end_pos, start_pos, it->string);
4006 else
4007 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4008
4009 return end_pos;
4010 }
4011
4012
4013 /* Set up IT from a single `display' specification PROP. OBJECT
4014 is the object in which the `display' property was found. *POSITION
4015 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4016 means that we previously saw a display specification which already
4017 replaced text display with something else, for example an image;
4018 we ignore such properties after the first one has been processed.
4019
4020 OVERLAY is the overlay this `display' property came from,
4021 or nil if it was a text property.
4022
4023 If PROP is a `space' or `image' specification, and in some other
4024 cases too, set *POSITION to the position where the `display'
4025 property ends.
4026
4027 Value is non-zero if something was found which replaces the display
4028 of buffer or string text. */
4029
4030 static int
4031 handle_single_display_spec (it, spec, object, overlay, position,
4032 display_replaced_before_p)
4033 struct it *it;
4034 Lisp_Object spec;
4035 Lisp_Object object;
4036 Lisp_Object overlay;
4037 struct text_pos *position;
4038 int display_replaced_before_p;
4039 {
4040 Lisp_Object form;
4041 Lisp_Object location, value;
4042 struct text_pos start_pos, save_pos;
4043 int valid_p;
4044
4045 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4046 If the result is non-nil, use VALUE instead of SPEC. */
4047 form = Qt;
4048 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4049 {
4050 spec = XCDR (spec);
4051 if (!CONSP (spec))
4052 return 0;
4053 form = XCAR (spec);
4054 spec = XCDR (spec);
4055 }
4056
4057 if (!NILP (form) && !EQ (form, Qt))
4058 {
4059 int count = SPECPDL_INDEX ();
4060 struct gcpro gcpro1;
4061
4062 /* Bind `object' to the object having the `display' property, a
4063 buffer or string. Bind `position' to the position in the
4064 object where the property was found, and `buffer-position'
4065 to the current position in the buffer. */
4066 specbind (Qobject, object);
4067 specbind (Qposition, make_number (CHARPOS (*position)));
4068 specbind (Qbuffer_position,
4069 make_number (STRINGP (object)
4070 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4071 GCPRO1 (form);
4072 form = safe_eval (form);
4073 UNGCPRO;
4074 unbind_to (count, Qnil);
4075 }
4076
4077 if (NILP (form))
4078 return 0;
4079
4080 /* Handle `(height HEIGHT)' specifications. */
4081 if (CONSP (spec)
4082 && EQ (XCAR (spec), Qheight)
4083 && CONSP (XCDR (spec)))
4084 {
4085 if (!FRAME_WINDOW_P (it->f))
4086 return 0;
4087
4088 it->font_height = XCAR (XCDR (spec));
4089 if (!NILP (it->font_height))
4090 {
4091 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4092 int new_height = -1;
4093
4094 if (CONSP (it->font_height)
4095 && (EQ (XCAR (it->font_height), Qplus)
4096 || EQ (XCAR (it->font_height), Qminus))
4097 && CONSP (XCDR (it->font_height))
4098 && INTEGERP (XCAR (XCDR (it->font_height))))
4099 {
4100 /* `(+ N)' or `(- N)' where N is an integer. */
4101 int steps = XINT (XCAR (XCDR (it->font_height)));
4102 if (EQ (XCAR (it->font_height), Qplus))
4103 steps = - steps;
4104 it->face_id = smaller_face (it->f, it->face_id, steps);
4105 }
4106 else if (FUNCTIONP (it->font_height))
4107 {
4108 /* Call function with current height as argument.
4109 Value is the new height. */
4110 Lisp_Object height;
4111 height = safe_call1 (it->font_height,
4112 face->lface[LFACE_HEIGHT_INDEX]);
4113 if (NUMBERP (height))
4114 new_height = XFLOATINT (height);
4115 }
4116 else if (NUMBERP (it->font_height))
4117 {
4118 /* Value is a multiple of the canonical char height. */
4119 struct face *face;
4120
4121 face = FACE_FROM_ID (it->f,
4122 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4123 new_height = (XFLOATINT (it->font_height)
4124 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4125 }
4126 else
4127 {
4128 /* Evaluate IT->font_height with `height' bound to the
4129 current specified height to get the new height. */
4130 int count = SPECPDL_INDEX ();
4131
4132 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4133 value = safe_eval (it->font_height);
4134 unbind_to (count, Qnil);
4135
4136 if (NUMBERP (value))
4137 new_height = XFLOATINT (value);
4138 }
4139
4140 if (new_height > 0)
4141 it->face_id = face_with_height (it->f, it->face_id, new_height);
4142 }
4143
4144 return 0;
4145 }
4146
4147 /* Handle `(space-width WIDTH)'. */
4148 if (CONSP (spec)
4149 && EQ (XCAR (spec), Qspace_width)
4150 && CONSP (XCDR (spec)))
4151 {
4152 if (!FRAME_WINDOW_P (it->f))
4153 return 0;
4154
4155 value = XCAR (XCDR (spec));
4156 if (NUMBERP (value) && XFLOATINT (value) > 0)
4157 it->space_width = value;
4158
4159 return 0;
4160 }
4161
4162 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4163 if (CONSP (spec)
4164 && EQ (XCAR (spec), Qslice))
4165 {
4166 Lisp_Object tem;
4167
4168 if (!FRAME_WINDOW_P (it->f))
4169 return 0;
4170
4171 if (tem = XCDR (spec), CONSP (tem))
4172 {
4173 it->slice.x = XCAR (tem);
4174 if (tem = XCDR (tem), CONSP (tem))
4175 {
4176 it->slice.y = XCAR (tem);
4177 if (tem = XCDR (tem), CONSP (tem))
4178 {
4179 it->slice.width = XCAR (tem);
4180 if (tem = XCDR (tem), CONSP (tem))
4181 it->slice.height = XCAR (tem);
4182 }
4183 }
4184 }
4185
4186 return 0;
4187 }
4188
4189 /* Handle `(raise FACTOR)'. */
4190 if (CONSP (spec)
4191 && EQ (XCAR (spec), Qraise)
4192 && CONSP (XCDR (spec)))
4193 {
4194 if (!FRAME_WINDOW_P (it->f))
4195 return 0;
4196
4197 #ifdef HAVE_WINDOW_SYSTEM
4198 value = XCAR (XCDR (spec));
4199 if (NUMBERP (value))
4200 {
4201 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4202 it->voffset = - (XFLOATINT (value)
4203 * (FONT_HEIGHT (face->font)));
4204 }
4205 #endif /* HAVE_WINDOW_SYSTEM */
4206
4207 return 0;
4208 }
4209
4210 /* Don't handle the other kinds of display specifications
4211 inside a string that we got from a `display' property. */
4212 if (it->string_from_display_prop_p)
4213 return 0;
4214
4215 /* Characters having this form of property are not displayed, so
4216 we have to find the end of the property. */
4217 start_pos = *position;
4218 *position = display_prop_end (it, object, start_pos);
4219 value = Qnil;
4220
4221 /* Stop the scan at that end position--we assume that all
4222 text properties change there. */
4223 it->stop_charpos = position->charpos;
4224
4225 /* Handle `(left-fringe BITMAP [FACE])'
4226 and `(right-fringe BITMAP [FACE])'. */
4227 if (CONSP (spec)
4228 && (EQ (XCAR (spec), Qleft_fringe)
4229 || EQ (XCAR (spec), Qright_fringe))
4230 && CONSP (XCDR (spec)))
4231 {
4232 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4233 int fringe_bitmap;
4234
4235 if (!FRAME_WINDOW_P (it->f))
4236 /* If we return here, POSITION has been advanced
4237 across the text with this property. */
4238 return 0;
4239
4240 #ifdef HAVE_WINDOW_SYSTEM
4241 value = XCAR (XCDR (spec));
4242 if (!SYMBOLP (value)
4243 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4244 /* If we return here, POSITION has been advanced
4245 across the text with this property. */
4246 return 0;
4247
4248 if (CONSP (XCDR (XCDR (spec))))
4249 {
4250 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4251 int face_id2 = lookup_derived_face (it->f, face_name,
4252 FRINGE_FACE_ID, 0);
4253 if (face_id2 >= 0)
4254 face_id = face_id2;
4255 }
4256
4257 /* Save current settings of IT so that we can restore them
4258 when we are finished with the glyph property value. */
4259
4260 save_pos = it->position;
4261 it->position = *position;
4262 push_it (it);
4263 it->position = save_pos;
4264
4265 it->area = TEXT_AREA;
4266 it->what = IT_IMAGE;
4267 it->image_id = -1; /* no image */
4268 it->position = start_pos;
4269 it->object = NILP (object) ? it->w->buffer : object;
4270 it->method = GET_FROM_IMAGE;
4271 it->from_overlay = Qnil;
4272 it->face_id = face_id;
4273
4274 /* Say that we haven't consumed the characters with
4275 `display' property yet. The call to pop_it in
4276 set_iterator_to_next will clean this up. */
4277 *position = start_pos;
4278
4279 if (EQ (XCAR (spec), Qleft_fringe))
4280 {
4281 it->left_user_fringe_bitmap = fringe_bitmap;
4282 it->left_user_fringe_face_id = face_id;
4283 }
4284 else
4285 {
4286 it->right_user_fringe_bitmap = fringe_bitmap;
4287 it->right_user_fringe_face_id = face_id;
4288 }
4289 #endif /* HAVE_WINDOW_SYSTEM */
4290 return 1;
4291 }
4292
4293 /* Prepare to handle `((margin left-margin) ...)',
4294 `((margin right-margin) ...)' and `((margin nil) ...)'
4295 prefixes for display specifications. */
4296 location = Qunbound;
4297 if (CONSP (spec) && CONSP (XCAR (spec)))
4298 {
4299 Lisp_Object tem;
4300
4301 value = XCDR (spec);
4302 if (CONSP (value))
4303 value = XCAR (value);
4304
4305 tem = XCAR (spec);
4306 if (EQ (XCAR (tem), Qmargin)
4307 && (tem = XCDR (tem),
4308 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4309 (NILP (tem)
4310 || EQ (tem, Qleft_margin)
4311 || EQ (tem, Qright_margin))))
4312 location = tem;
4313 }
4314
4315 if (EQ (location, Qunbound))
4316 {
4317 location = Qnil;
4318 value = spec;
4319 }
4320
4321 /* After this point, VALUE is the property after any
4322 margin prefix has been stripped. It must be a string,
4323 an image specification, or `(space ...)'.
4324
4325 LOCATION specifies where to display: `left-margin',
4326 `right-margin' or nil. */
4327
4328 valid_p = (STRINGP (value)
4329 #ifdef HAVE_WINDOW_SYSTEM
4330 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4331 #endif /* not HAVE_WINDOW_SYSTEM */
4332 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4333
4334 if (valid_p && !display_replaced_before_p)
4335 {
4336 /* Save current settings of IT so that we can restore them
4337 when we are finished with the glyph property value. */
4338 save_pos = it->position;
4339 it->position = *position;
4340 push_it (it);
4341 it->position = save_pos;
4342 it->from_overlay = overlay;
4343
4344 if (NILP (location))
4345 it->area = TEXT_AREA;
4346 else if (EQ (location, Qleft_margin))
4347 it->area = LEFT_MARGIN_AREA;
4348 else
4349 it->area = RIGHT_MARGIN_AREA;
4350
4351 if (STRINGP (value))
4352 {
4353 it->string = value;
4354 it->multibyte_p = STRING_MULTIBYTE (it->string);
4355 it->current.overlay_string_index = -1;
4356 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4357 it->end_charpos = it->string_nchars = SCHARS (it->string);
4358 it->method = GET_FROM_STRING;
4359 it->stop_charpos = 0;
4360 it->string_from_display_prop_p = 1;
4361 /* Say that we haven't consumed the characters with
4362 `display' property yet. The call to pop_it in
4363 set_iterator_to_next will clean this up. */
4364 if (BUFFERP (object))
4365 *position = start_pos;
4366 }
4367 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4368 {
4369 it->method = GET_FROM_STRETCH;
4370 it->object = value;
4371 *position = it->position = start_pos;
4372 }
4373 #ifdef HAVE_WINDOW_SYSTEM
4374 else
4375 {
4376 it->what = IT_IMAGE;
4377 it->image_id = lookup_image (it->f, value);
4378 it->position = start_pos;
4379 it->object = NILP (object) ? it->w->buffer : object;
4380 it->method = GET_FROM_IMAGE;
4381
4382 /* Say that we haven't consumed the characters with
4383 `display' property yet. The call to pop_it in
4384 set_iterator_to_next will clean this up. */
4385 *position = start_pos;
4386 }
4387 #endif /* HAVE_WINDOW_SYSTEM */
4388
4389 return 1;
4390 }
4391
4392 /* Invalid property or property not supported. Restore
4393 POSITION to what it was before. */
4394 *position = start_pos;
4395 return 0;
4396 }
4397
4398
4399 /* Check if SPEC is a display sub-property value whose text should be
4400 treated as intangible. */
4401
4402 static int
4403 single_display_spec_intangible_p (prop)
4404 Lisp_Object prop;
4405 {
4406 /* Skip over `when FORM'. */
4407 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4408 {
4409 prop = XCDR (prop);
4410 if (!CONSP (prop))
4411 return 0;
4412 prop = XCDR (prop);
4413 }
4414
4415 if (STRINGP (prop))
4416 return 1;
4417
4418 if (!CONSP (prop))
4419 return 0;
4420
4421 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4422 we don't need to treat text as intangible. */
4423 if (EQ (XCAR (prop), Qmargin))
4424 {
4425 prop = XCDR (prop);
4426 if (!CONSP (prop))
4427 return 0;
4428
4429 prop = XCDR (prop);
4430 if (!CONSP (prop)
4431 || EQ (XCAR (prop), Qleft_margin)
4432 || EQ (XCAR (prop), Qright_margin))
4433 return 0;
4434 }
4435
4436 return (CONSP (prop)
4437 && (EQ (XCAR (prop), Qimage)
4438 || EQ (XCAR (prop), Qspace)));
4439 }
4440
4441
4442 /* Check if PROP is a display property value whose text should be
4443 treated as intangible. */
4444
4445 int
4446 display_prop_intangible_p (prop)
4447 Lisp_Object prop;
4448 {
4449 if (CONSP (prop)
4450 && CONSP (XCAR (prop))
4451 && !EQ (Qmargin, XCAR (XCAR (prop))))
4452 {
4453 /* A list of sub-properties. */
4454 while (CONSP (prop))
4455 {
4456 if (single_display_spec_intangible_p (XCAR (prop)))
4457 return 1;
4458 prop = XCDR (prop);
4459 }
4460 }
4461 else if (VECTORP (prop))
4462 {
4463 /* A vector of sub-properties. */
4464 int i;
4465 for (i = 0; i < ASIZE (prop); ++i)
4466 if (single_display_spec_intangible_p (AREF (prop, i)))
4467 return 1;
4468 }
4469 else
4470 return single_display_spec_intangible_p (prop);
4471
4472 return 0;
4473 }
4474
4475
4476 /* Return 1 if PROP is a display sub-property value containing STRING. */
4477
4478 static int
4479 single_display_spec_string_p (prop, string)
4480 Lisp_Object prop, string;
4481 {
4482 if (EQ (string, prop))
4483 return 1;
4484
4485 /* Skip over `when FORM'. */
4486 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4487 {
4488 prop = XCDR (prop);
4489 if (!CONSP (prop))
4490 return 0;
4491 prop = XCDR (prop);
4492 }
4493
4494 if (CONSP (prop))
4495 /* Skip over `margin LOCATION'. */
4496 if (EQ (XCAR (prop), Qmargin))
4497 {
4498 prop = XCDR (prop);
4499 if (!CONSP (prop))
4500 return 0;
4501
4502 prop = XCDR (prop);
4503 if (!CONSP (prop))
4504 return 0;
4505 }
4506
4507 return CONSP (prop) && EQ (XCAR (prop), string);
4508 }
4509
4510
4511 /* Return 1 if STRING appears in the `display' property PROP. */
4512
4513 static int
4514 display_prop_string_p (prop, string)
4515 Lisp_Object prop, string;
4516 {
4517 if (CONSP (prop)
4518 && CONSP (XCAR (prop))
4519 && !EQ (Qmargin, XCAR (XCAR (prop))))
4520 {
4521 /* A list of sub-properties. */
4522 while (CONSP (prop))
4523 {
4524 if (single_display_spec_string_p (XCAR (prop), string))
4525 return 1;
4526 prop = XCDR (prop);
4527 }
4528 }
4529 else if (VECTORP (prop))
4530 {
4531 /* A vector of sub-properties. */
4532 int i;
4533 for (i = 0; i < ASIZE (prop); ++i)
4534 if (single_display_spec_string_p (AREF (prop, i), string))
4535 return 1;
4536 }
4537 else
4538 return single_display_spec_string_p (prop, string);
4539
4540 return 0;
4541 }
4542
4543
4544 /* Determine which buffer position in W's buffer STRING comes from.
4545 AROUND_CHARPOS is an approximate position where it could come from.
4546 Value is the buffer position or 0 if it couldn't be determined.
4547
4548 W's buffer must be current.
4549
4550 This function is necessary because we don't record buffer positions
4551 in glyphs generated from strings (to keep struct glyph small).
4552 This function may only use code that doesn't eval because it is
4553 called asynchronously from note_mouse_highlight. */
4554
4555 int
4556 string_buffer_position (w, string, around_charpos)
4557 struct window *w;
4558 Lisp_Object string;
4559 int around_charpos;
4560 {
4561 Lisp_Object limit, prop, pos;
4562 const int MAX_DISTANCE = 1000;
4563 int found = 0;
4564
4565 pos = make_number (around_charpos);
4566 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4567 while (!found && !EQ (pos, limit))
4568 {
4569 prop = Fget_char_property (pos, Qdisplay, Qnil);
4570 if (!NILP (prop) && display_prop_string_p (prop, string))
4571 found = 1;
4572 else
4573 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4574 }
4575
4576 if (!found)
4577 {
4578 pos = make_number (around_charpos);
4579 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4580 while (!found && !EQ (pos, limit))
4581 {
4582 prop = Fget_char_property (pos, Qdisplay, Qnil);
4583 if (!NILP (prop) && display_prop_string_p (prop, string))
4584 found = 1;
4585 else
4586 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4587 limit);
4588 }
4589 }
4590
4591 return found ? XINT (pos) : 0;
4592 }
4593
4594
4595 \f
4596 /***********************************************************************
4597 `composition' property
4598 ***********************************************************************/
4599
4600 /* Set up iterator IT from `composition' property at its current
4601 position. Called from handle_stop. */
4602
4603 static enum prop_handled
4604 handle_composition_prop (it)
4605 struct it *it;
4606 {
4607 Lisp_Object prop, string;
4608 EMACS_INT pos, pos_byte, start, end;
4609
4610 if (STRINGP (it->string))
4611 {
4612 unsigned char *s;
4613
4614 pos = IT_STRING_CHARPOS (*it);
4615 pos_byte = IT_STRING_BYTEPOS (*it);
4616 string = it->string;
4617 s = SDATA (string) + pos_byte;
4618 it->c = STRING_CHAR (s);
4619 }
4620 else
4621 {
4622 pos = IT_CHARPOS (*it);
4623 pos_byte = IT_BYTEPOS (*it);
4624 string = Qnil;
4625 it->c = FETCH_CHAR (pos_byte);
4626 }
4627
4628 /* If there's a valid composition and point is not inside of the
4629 composition (in the case that the composition is from the current
4630 buffer), draw a glyph composed from the composition components. */
4631 if (find_composition (pos, -1, &start, &end, &prop, string)
4632 && COMPOSITION_VALID_P (start, end, prop)
4633 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4634 {
4635 if (start != pos)
4636 {
4637 if (STRINGP (it->string))
4638 pos_byte = string_char_to_byte (it->string, start);
4639 else
4640 pos_byte = CHAR_TO_BYTE (start);
4641 }
4642 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4643 prop, string);
4644
4645 if (it->cmp_it.id >= 0)
4646 {
4647 it->cmp_it.ch = -1;
4648 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4649 it->cmp_it.nglyphs = -1;
4650 }
4651 }
4652
4653 return HANDLED_NORMALLY;
4654 }
4655
4656
4657 \f
4658 /***********************************************************************
4659 Overlay strings
4660 ***********************************************************************/
4661
4662 /* The following structure is used to record overlay strings for
4663 later sorting in load_overlay_strings. */
4664
4665 struct overlay_entry
4666 {
4667 Lisp_Object overlay;
4668 Lisp_Object string;
4669 int priority;
4670 int after_string_p;
4671 };
4672
4673
4674 /* Set up iterator IT from overlay strings at its current position.
4675 Called from handle_stop. */
4676
4677 static enum prop_handled
4678 handle_overlay_change (it)
4679 struct it *it;
4680 {
4681 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4682 return HANDLED_RECOMPUTE_PROPS;
4683 else
4684 return HANDLED_NORMALLY;
4685 }
4686
4687
4688 /* Set up the next overlay string for delivery by IT, if there is an
4689 overlay string to deliver. Called by set_iterator_to_next when the
4690 end of the current overlay string is reached. If there are more
4691 overlay strings to display, IT->string and
4692 IT->current.overlay_string_index are set appropriately here.
4693 Otherwise IT->string is set to nil. */
4694
4695 static void
4696 next_overlay_string (it)
4697 struct it *it;
4698 {
4699 ++it->current.overlay_string_index;
4700 if (it->current.overlay_string_index == it->n_overlay_strings)
4701 {
4702 /* No more overlay strings. Restore IT's settings to what
4703 they were before overlay strings were processed, and
4704 continue to deliver from current_buffer. */
4705
4706 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4707 pop_it (it);
4708 xassert (it->sp > 0
4709 || (NILP (it->string)
4710 && it->method == GET_FROM_BUFFER
4711 && it->stop_charpos >= BEGV
4712 && it->stop_charpos <= it->end_charpos));
4713 it->current.overlay_string_index = -1;
4714 it->n_overlay_strings = 0;
4715 it->overlay_strings_charpos = -1;
4716
4717 /* If we're at the end of the buffer, record that we have
4718 processed the overlay strings there already, so that
4719 next_element_from_buffer doesn't try it again. */
4720 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4721 it->overlay_strings_at_end_processed_p = 1;
4722 }
4723 else
4724 {
4725 /* There are more overlay strings to process. If
4726 IT->current.overlay_string_index has advanced to a position
4727 where we must load IT->overlay_strings with more strings, do
4728 it. We must load at the IT->overlay_strings_charpos where
4729 IT->n_overlay_strings was originally computed; when invisible
4730 text is present, this might not be IT_CHARPOS (Bug#7016). */
4731 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4732
4733 if (it->current.overlay_string_index && i == 0)
4734 load_overlay_strings (it, it->overlay_strings_charpos);
4735
4736 /* Initialize IT to deliver display elements from the overlay
4737 string. */
4738 it->string = it->overlay_strings[i];
4739 it->multibyte_p = STRING_MULTIBYTE (it->string);
4740 SET_TEXT_POS (it->current.string_pos, 0, 0);
4741 it->method = GET_FROM_STRING;
4742 it->stop_charpos = 0;
4743 if (it->cmp_it.stop_pos >= 0)
4744 it->cmp_it.stop_pos = 0;
4745 }
4746
4747 CHECK_IT (it);
4748 }
4749
4750
4751 /* Compare two overlay_entry structures E1 and E2. Used as a
4752 comparison function for qsort in load_overlay_strings. Overlay
4753 strings for the same position are sorted so that
4754
4755 1. All after-strings come in front of before-strings, except
4756 when they come from the same overlay.
4757
4758 2. Within after-strings, strings are sorted so that overlay strings
4759 from overlays with higher priorities come first.
4760
4761 2. Within before-strings, strings are sorted so that overlay
4762 strings from overlays with higher priorities come last.
4763
4764 Value is analogous to strcmp. */
4765
4766
4767 static int
4768 compare_overlay_entries (e1, e2)
4769 void *e1, *e2;
4770 {
4771 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4772 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4773 int result;
4774
4775 if (entry1->after_string_p != entry2->after_string_p)
4776 {
4777 /* Let after-strings appear in front of before-strings if
4778 they come from different overlays. */
4779 if (EQ (entry1->overlay, entry2->overlay))
4780 result = entry1->after_string_p ? 1 : -1;
4781 else
4782 result = entry1->after_string_p ? -1 : 1;
4783 }
4784 else if (entry1->after_string_p)
4785 /* After-strings sorted in order of decreasing priority. */
4786 result = entry2->priority - entry1->priority;
4787 else
4788 /* Before-strings sorted in order of increasing priority. */
4789 result = entry1->priority - entry2->priority;
4790
4791 return result;
4792 }
4793
4794
4795 /* Load the vector IT->overlay_strings with overlay strings from IT's
4796 current buffer position, or from CHARPOS if that is > 0. Set
4797 IT->n_overlays to the total number of overlay strings found.
4798
4799 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4800 a time. On entry into load_overlay_strings,
4801 IT->current.overlay_string_index gives the number of overlay
4802 strings that have already been loaded by previous calls to this
4803 function.
4804
4805 IT->add_overlay_start contains an additional overlay start
4806 position to consider for taking overlay strings from, if non-zero.
4807 This position comes into play when the overlay has an `invisible'
4808 property, and both before and after-strings. When we've skipped to
4809 the end of the overlay, because of its `invisible' property, we
4810 nevertheless want its before-string to appear.
4811 IT->add_overlay_start will contain the overlay start position
4812 in this case.
4813
4814 Overlay strings are sorted so that after-string strings come in
4815 front of before-string strings. Within before and after-strings,
4816 strings are sorted by overlay priority. See also function
4817 compare_overlay_entries. */
4818
4819 static void
4820 load_overlay_strings (it, charpos)
4821 struct it *it;
4822 int charpos;
4823 {
4824 extern Lisp_Object Qwindow, Qpriority;
4825 Lisp_Object overlay, window, str, invisible;
4826 struct Lisp_Overlay *ov;
4827 int start, end;
4828 int size = 20;
4829 int n = 0, i, j, invis_p;
4830 struct overlay_entry *entries
4831 = (struct overlay_entry *) alloca (size * sizeof *entries);
4832
4833 if (charpos <= 0)
4834 charpos = IT_CHARPOS (*it);
4835
4836 /* Append the overlay string STRING of overlay OVERLAY to vector
4837 `entries' which has size `size' and currently contains `n'
4838 elements. AFTER_P non-zero means STRING is an after-string of
4839 OVERLAY. */
4840 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4841 do \
4842 { \
4843 Lisp_Object priority; \
4844 \
4845 if (n == size) \
4846 { \
4847 int new_size = 2 * size; \
4848 struct overlay_entry *old = entries; \
4849 entries = \
4850 (struct overlay_entry *) alloca (new_size \
4851 * sizeof *entries); \
4852 bcopy (old, entries, size * sizeof *entries); \
4853 size = new_size; \
4854 } \
4855 \
4856 entries[n].string = (STRING); \
4857 entries[n].overlay = (OVERLAY); \
4858 priority = Foverlay_get ((OVERLAY), Qpriority); \
4859 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4860 entries[n].after_string_p = (AFTER_P); \
4861 ++n; \
4862 } \
4863 while (0)
4864
4865 /* Process overlay before the overlay center. */
4866 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4867 {
4868 XSETMISC (overlay, ov);
4869 xassert (OVERLAYP (overlay));
4870 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4871 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4872
4873 if (end < charpos)
4874 break;
4875
4876 /* Skip this overlay if it doesn't start or end at IT's current
4877 position. */
4878 if (end != charpos && start != charpos)
4879 continue;
4880
4881 /* Skip this overlay if it doesn't apply to IT->w. */
4882 window = Foverlay_get (overlay, Qwindow);
4883 if (WINDOWP (window) && XWINDOW (window) != it->w)
4884 continue;
4885
4886 /* If the text ``under'' the overlay is invisible, both before-
4887 and after-strings from this overlay are visible; start and
4888 end position are indistinguishable. */
4889 invisible = Foverlay_get (overlay, Qinvisible);
4890 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4891
4892 /* If overlay has a non-empty before-string, record it. */
4893 if ((start == charpos || (end == charpos && invis_p))
4894 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4895 && SCHARS (str))
4896 RECORD_OVERLAY_STRING (overlay, str, 0);
4897
4898 /* If overlay has a non-empty after-string, record it. */
4899 if ((end == charpos || (start == charpos && invis_p))
4900 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4901 && SCHARS (str))
4902 RECORD_OVERLAY_STRING (overlay, str, 1);
4903 }
4904
4905 /* Process overlays after the overlay center. */
4906 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4907 {
4908 XSETMISC (overlay, ov);
4909 xassert (OVERLAYP (overlay));
4910 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4911 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4912
4913 if (start > charpos)
4914 break;
4915
4916 /* Skip this overlay if it doesn't start or end at IT's current
4917 position. */
4918 if (end != charpos && start != charpos)
4919 continue;
4920
4921 /* Skip this overlay if it doesn't apply to IT->w. */
4922 window = Foverlay_get (overlay, Qwindow);
4923 if (WINDOWP (window) && XWINDOW (window) != it->w)
4924 continue;
4925
4926 /* If the text ``under'' the overlay is invisible, it has a zero
4927 dimension, and both before- and after-strings apply. */
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 #undef RECORD_OVERLAY_STRING
4945
4946 /* Sort entries. */
4947 if (n > 1)
4948 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4949
4950 /* Record number of overlay strings, and where we computed it. */
4951 it->n_overlay_strings = n;
4952 it->overlay_strings_charpos = charpos;
4953
4954 /* IT->current.overlay_string_index is the number of overlay strings
4955 that have already been consumed by IT. Copy some of the
4956 remaining overlay strings to IT->overlay_strings. */
4957 i = 0;
4958 j = it->current.overlay_string_index;
4959 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4960 {
4961 it->overlay_strings[i] = entries[j].string;
4962 it->string_overlays[i++] = entries[j++].overlay;
4963 }
4964
4965 CHECK_IT (it);
4966 }
4967
4968
4969 /* Get the first chunk of overlay strings at IT's current buffer
4970 position, or at CHARPOS if that is > 0. Value is non-zero if at
4971 least one overlay string was found. */
4972
4973 static int
4974 get_overlay_strings_1 (it, charpos, compute_stop_p)
4975 struct it *it;
4976 int charpos;
4977 int compute_stop_p;
4978 {
4979 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4980 process. This fills IT->overlay_strings with strings, and sets
4981 IT->n_overlay_strings to the total number of strings to process.
4982 IT->pos.overlay_string_index has to be set temporarily to zero
4983 because load_overlay_strings needs this; it must be set to -1
4984 when no overlay strings are found because a zero value would
4985 indicate a position in the first overlay string. */
4986 it->current.overlay_string_index = 0;
4987 load_overlay_strings (it, charpos);
4988
4989 /* If we found overlay strings, set up IT to deliver display
4990 elements from the first one. Otherwise set up IT to deliver
4991 from current_buffer. */
4992 if (it->n_overlay_strings)
4993 {
4994 /* Make sure we know settings in current_buffer, so that we can
4995 restore meaningful values when we're done with the overlay
4996 strings. */
4997 if (compute_stop_p)
4998 compute_stop_pos (it);
4999 xassert (it->face_id >= 0);
5000
5001 /* Save IT's settings. They are restored after all overlay
5002 strings have been processed. */
5003 xassert (!compute_stop_p || it->sp == 0);
5004
5005 /* When called from handle_stop, there might be an empty display
5006 string loaded. In that case, don't bother saving it. */
5007 if (!STRINGP (it->string) || SCHARS (it->string))
5008 push_it (it);
5009
5010 /* Set up IT to deliver display elements from the first overlay
5011 string. */
5012 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5013 it->string = it->overlay_strings[0];
5014 it->from_overlay = Qnil;
5015 it->stop_charpos = 0;
5016 xassert (STRINGP (it->string));
5017 it->end_charpos = SCHARS (it->string);
5018 it->multibyte_p = STRING_MULTIBYTE (it->string);
5019 it->method = GET_FROM_STRING;
5020 return 1;
5021 }
5022
5023 it->current.overlay_string_index = -1;
5024 return 0;
5025 }
5026
5027 static int
5028 get_overlay_strings (it, charpos)
5029 struct it *it;
5030 int charpos;
5031 {
5032 it->string = Qnil;
5033 it->method = GET_FROM_BUFFER;
5034
5035 (void) get_overlay_strings_1 (it, charpos, 1);
5036
5037 CHECK_IT (it);
5038
5039 /* Value is non-zero if we found at least one overlay string. */
5040 return STRINGP (it->string);
5041 }
5042
5043
5044 \f
5045 /***********************************************************************
5046 Saving and restoring state
5047 ***********************************************************************/
5048
5049 /* Save current settings of IT on IT->stack. Called, for example,
5050 before setting up IT for an overlay string, to be able to restore
5051 IT's settings to what they were after the overlay string has been
5052 processed. */
5053
5054 static void
5055 push_it (it)
5056 struct it *it;
5057 {
5058 struct iterator_stack_entry *p;
5059
5060 xassert (it->sp < IT_STACK_SIZE);
5061 p = it->stack + it->sp;
5062
5063 p->stop_charpos = it->stop_charpos;
5064 p->cmp_it = it->cmp_it;
5065 xassert (it->face_id >= 0);
5066 p->face_id = it->face_id;
5067 p->string = it->string;
5068 p->method = it->method;
5069 p->from_overlay = it->from_overlay;
5070 switch (p->method)
5071 {
5072 case GET_FROM_IMAGE:
5073 p->u.image.object = it->object;
5074 p->u.image.image_id = it->image_id;
5075 p->u.image.slice = it->slice;
5076 break;
5077 case GET_FROM_STRETCH:
5078 p->u.stretch.object = it->object;
5079 break;
5080 }
5081 p->position = it->position;
5082 p->current = it->current;
5083 p->end_charpos = it->end_charpos;
5084 p->string_nchars = it->string_nchars;
5085 p->area = it->area;
5086 p->multibyte_p = it->multibyte_p;
5087 p->avoid_cursor_p = it->avoid_cursor_p;
5088 p->space_width = it->space_width;
5089 p->font_height = it->font_height;
5090 p->voffset = it->voffset;
5091 p->string_from_display_prop_p = it->string_from_display_prop_p;
5092 p->display_ellipsis_p = 0;
5093 p->line_wrap = it->line_wrap;
5094 ++it->sp;
5095 }
5096
5097
5098 /* Restore IT's settings from IT->stack. Called, for example, when no
5099 more overlay strings must be processed, and we return to delivering
5100 display elements from a buffer, or when the end of a string from a
5101 `display' property is reached and we return to delivering display
5102 elements from an overlay string, or from a buffer. */
5103
5104 static void
5105 pop_it (it)
5106 struct it *it;
5107 {
5108 struct iterator_stack_entry *p;
5109
5110 xassert (it->sp > 0);
5111 --it->sp;
5112 p = it->stack + it->sp;
5113 it->stop_charpos = p->stop_charpos;
5114 it->cmp_it = p->cmp_it;
5115 it->face_id = p->face_id;
5116 it->current = p->current;
5117 it->position = p->position;
5118 it->string = p->string;
5119 it->from_overlay = p->from_overlay;
5120 if (NILP (it->string))
5121 SET_TEXT_POS (it->current.string_pos, -1, -1);
5122 it->method = p->method;
5123 switch (it->method)
5124 {
5125 case GET_FROM_IMAGE:
5126 it->image_id = p->u.image.image_id;
5127 it->object = p->u.image.object;
5128 it->slice = p->u.image.slice;
5129 break;
5130 case GET_FROM_STRETCH:
5131 it->object = p->u.comp.object;
5132 break;
5133 case GET_FROM_BUFFER:
5134 it->object = it->w->buffer;
5135 break;
5136 case GET_FROM_STRING:
5137 it->object = it->string;
5138 break;
5139 case GET_FROM_DISPLAY_VECTOR:
5140 if (it->s)
5141 it->method = GET_FROM_C_STRING;
5142 else if (STRINGP (it->string))
5143 it->method = GET_FROM_STRING;
5144 else
5145 {
5146 it->method = GET_FROM_BUFFER;
5147 it->object = it->w->buffer;
5148 }
5149 }
5150 it->end_charpos = p->end_charpos;
5151 it->string_nchars = p->string_nchars;
5152 it->area = p->area;
5153 it->multibyte_p = p->multibyte_p;
5154 it->avoid_cursor_p = p->avoid_cursor_p;
5155 it->space_width = p->space_width;
5156 it->font_height = p->font_height;
5157 it->voffset = p->voffset;
5158 it->string_from_display_prop_p = p->string_from_display_prop_p;
5159 it->line_wrap = p->line_wrap;
5160 }
5161
5162
5163 \f
5164 /***********************************************************************
5165 Moving over lines
5166 ***********************************************************************/
5167
5168 /* Set IT's current position to the previous line start. */
5169
5170 static void
5171 back_to_previous_line_start (it)
5172 struct it *it;
5173 {
5174 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5175 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5176 }
5177
5178
5179 /* Move IT to the next line start.
5180
5181 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5182 we skipped over part of the text (as opposed to moving the iterator
5183 continuously over the text). Otherwise, don't change the value
5184 of *SKIPPED_P.
5185
5186 Newlines may come from buffer text, overlay strings, or strings
5187 displayed via the `display' property. That's the reason we can't
5188 simply use find_next_newline_no_quit.
5189
5190 Note that this function may not skip over invisible text that is so
5191 because of text properties and immediately follows a newline. If
5192 it would, function reseat_at_next_visible_line_start, when called
5193 from set_iterator_to_next, would effectively make invisible
5194 characters following a newline part of the wrong glyph row, which
5195 leads to wrong cursor motion. */
5196
5197 static int
5198 forward_to_next_line_start (it, skipped_p)
5199 struct it *it;
5200 int *skipped_p;
5201 {
5202 int old_selective, newline_found_p, n;
5203 const int MAX_NEWLINE_DISTANCE = 500;
5204
5205 /* If already on a newline, just consume it to avoid unintended
5206 skipping over invisible text below. */
5207 if (it->what == IT_CHARACTER
5208 && it->c == '\n'
5209 && CHARPOS (it->position) == IT_CHARPOS (*it))
5210 {
5211 set_iterator_to_next (it, 0);
5212 it->c = 0;
5213 return 1;
5214 }
5215
5216 /* Don't handle selective display in the following. It's (a)
5217 unnecessary because it's done by the caller, and (b) leads to an
5218 infinite recursion because next_element_from_ellipsis indirectly
5219 calls this function. */
5220 old_selective = it->selective;
5221 it->selective = 0;
5222
5223 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5224 from buffer text. */
5225 for (n = newline_found_p = 0;
5226 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5227 n += STRINGP (it->string) ? 0 : 1)
5228 {
5229 if (!get_next_display_element (it))
5230 return 0;
5231 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5232 set_iterator_to_next (it, 0);
5233 }
5234
5235 /* If we didn't find a newline near enough, see if we can use a
5236 short-cut. */
5237 if (!newline_found_p)
5238 {
5239 int start = IT_CHARPOS (*it);
5240 int limit = find_next_newline_no_quit (start, 1);
5241 Lisp_Object pos;
5242
5243 xassert (!STRINGP (it->string));
5244
5245 /* If there isn't any `display' property in sight, and no
5246 overlays, we can just use the position of the newline in
5247 buffer text. */
5248 if (it->stop_charpos >= limit
5249 || ((pos = Fnext_single_property_change (make_number (start),
5250 Qdisplay,
5251 Qnil, make_number (limit)),
5252 NILP (pos))
5253 && next_overlay_change (start) == ZV))
5254 {
5255 IT_CHARPOS (*it) = limit;
5256 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5257 *skipped_p = newline_found_p = 1;
5258 }
5259 else
5260 {
5261 while (get_next_display_element (it)
5262 && !newline_found_p)
5263 {
5264 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5265 set_iterator_to_next (it, 0);
5266 }
5267 }
5268 }
5269
5270 it->selective = old_selective;
5271 return newline_found_p;
5272 }
5273
5274
5275 /* Set IT's current position to the previous visible line start. Skip
5276 invisible text that is so either due to text properties or due to
5277 selective display. Caution: this does not change IT->current_x and
5278 IT->hpos. */
5279
5280 static void
5281 back_to_previous_visible_line_start (it)
5282 struct it *it;
5283 {
5284 while (IT_CHARPOS (*it) > BEGV)
5285 {
5286 back_to_previous_line_start (it);
5287
5288 if (IT_CHARPOS (*it) <= BEGV)
5289 break;
5290
5291 /* If selective > 0, then lines indented more than that values
5292 are invisible. */
5293 if (it->selective > 0
5294 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5295 (double) it->selective)) /* iftc */
5296 continue;
5297
5298 /* Check the newline before point for invisibility. */
5299 {
5300 Lisp_Object prop;
5301 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5302 Qinvisible, it->window);
5303 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5304 continue;
5305 }
5306
5307 if (IT_CHARPOS (*it) <= BEGV)
5308 break;
5309
5310 {
5311 struct it it2;
5312 int pos;
5313 EMACS_INT beg, end;
5314 Lisp_Object val, overlay;
5315
5316 /* If newline is part of a composition, continue from start of composition */
5317 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5318 && beg < IT_CHARPOS (*it))
5319 goto replaced;
5320
5321 /* If newline is replaced by a display property, find start of overlay
5322 or interval and continue search from that point. */
5323 it2 = *it;
5324 pos = --IT_CHARPOS (it2);
5325 --IT_BYTEPOS (it2);
5326 it2.sp = 0;
5327 it2.string_from_display_prop_p = 0;
5328 if (handle_display_prop (&it2) == HANDLED_RETURN
5329 && !NILP (val = get_char_property_and_overlay
5330 (make_number (pos), Qdisplay, Qnil, &overlay))
5331 && (OVERLAYP (overlay)
5332 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5333 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5334 goto replaced;
5335
5336 /* Newline is not replaced by anything -- so we are done. */
5337 break;
5338
5339 replaced:
5340 if (beg < BEGV)
5341 beg = BEGV;
5342 IT_CHARPOS (*it) = beg;
5343 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5344 }
5345 }
5346
5347 it->continuation_lines_width = 0;
5348
5349 xassert (IT_CHARPOS (*it) >= BEGV);
5350 xassert (IT_CHARPOS (*it) == BEGV
5351 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5352 CHECK_IT (it);
5353 }
5354
5355
5356 /* Reseat iterator IT at the previous visible line start. Skip
5357 invisible text that is so either due to text properties or due to
5358 selective display. At the end, update IT's overlay information,
5359 face information etc. */
5360
5361 void
5362 reseat_at_previous_visible_line_start (it)
5363 struct it *it;
5364 {
5365 back_to_previous_visible_line_start (it);
5366 reseat (it, it->current.pos, 1);
5367 CHECK_IT (it);
5368 }
5369
5370
5371 /* Reseat iterator IT on the next visible line start in the current
5372 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5373 preceding the line start. Skip over invisible text that is so
5374 because of selective display. Compute faces, overlays etc at the
5375 new position. Note that this function does not skip over text that
5376 is invisible because of text properties. */
5377
5378 static void
5379 reseat_at_next_visible_line_start (it, on_newline_p)
5380 struct it *it;
5381 int on_newline_p;
5382 {
5383 int newline_found_p, skipped_p = 0;
5384
5385 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5386
5387 /* Skip over lines that are invisible because they are indented
5388 more than the value of IT->selective. */
5389 if (it->selective > 0)
5390 while (IT_CHARPOS (*it) < ZV
5391 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5392 (double) it->selective)) /* iftc */
5393 {
5394 xassert (IT_BYTEPOS (*it) == BEGV
5395 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5396 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5397 }
5398
5399 /* Position on the newline if that's what's requested. */
5400 if (on_newline_p && newline_found_p)
5401 {
5402 if (STRINGP (it->string))
5403 {
5404 if (IT_STRING_CHARPOS (*it) > 0)
5405 {
5406 --IT_STRING_CHARPOS (*it);
5407 --IT_STRING_BYTEPOS (*it);
5408 }
5409 }
5410 else if (IT_CHARPOS (*it) > BEGV)
5411 {
5412 --IT_CHARPOS (*it);
5413 --IT_BYTEPOS (*it);
5414 reseat (it, it->current.pos, 0);
5415 }
5416 }
5417 else if (skipped_p)
5418 reseat (it, it->current.pos, 0);
5419
5420 CHECK_IT (it);
5421 }
5422
5423
5424 \f
5425 /***********************************************************************
5426 Changing an iterator's position
5427 ***********************************************************************/
5428
5429 /* Change IT's current position to POS in current_buffer. If FORCE_P
5430 is non-zero, always check for text properties at the new position.
5431 Otherwise, text properties are only looked up if POS >=
5432 IT->check_charpos of a property. */
5433
5434 static void
5435 reseat (it, pos, force_p)
5436 struct it *it;
5437 struct text_pos pos;
5438 int force_p;
5439 {
5440 int original_pos = IT_CHARPOS (*it);
5441
5442 reseat_1 (it, pos, 0);
5443
5444 /* Determine where to check text properties. Avoid doing it
5445 where possible because text property lookup is very expensive. */
5446 if (force_p
5447 || CHARPOS (pos) > it->stop_charpos
5448 || CHARPOS (pos) < original_pos)
5449 handle_stop (it);
5450
5451 CHECK_IT (it);
5452 }
5453
5454
5455 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5456 IT->stop_pos to POS, also. */
5457
5458 static void
5459 reseat_1 (it, pos, set_stop_p)
5460 struct it *it;
5461 struct text_pos pos;
5462 int set_stop_p;
5463 {
5464 /* Don't call this function when scanning a C string. */
5465 xassert (it->s == NULL);
5466
5467 /* POS must be a reasonable value. */
5468 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5469
5470 it->current.pos = it->position = pos;
5471 it->end_charpos = ZV;
5472 it->dpvec = NULL;
5473 it->current.dpvec_index = -1;
5474 it->current.overlay_string_index = -1;
5475 IT_STRING_CHARPOS (*it) = -1;
5476 IT_STRING_BYTEPOS (*it) = -1;
5477 it->string = Qnil;
5478 it->string_from_display_prop_p = 0;
5479 it->method = GET_FROM_BUFFER;
5480 it->object = it->w->buffer;
5481 it->area = TEXT_AREA;
5482 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5483 it->sp = 0;
5484 it->string_from_display_prop_p = 0;
5485 it->face_before_selective_p = 0;
5486
5487 if (set_stop_p)
5488 it->stop_charpos = CHARPOS (pos);
5489 }
5490
5491
5492 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5493 If S is non-null, it is a C string to iterate over. Otherwise,
5494 STRING gives a Lisp string to iterate over.
5495
5496 If PRECISION > 0, don't return more then PRECISION number of
5497 characters from the string.
5498
5499 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5500 characters have been returned. FIELD_WIDTH < 0 means an infinite
5501 field width.
5502
5503 MULTIBYTE = 0 means disable processing of multibyte characters,
5504 MULTIBYTE > 0 means enable it,
5505 MULTIBYTE < 0 means use IT->multibyte_p.
5506
5507 IT must be initialized via a prior call to init_iterator before
5508 calling this function. */
5509
5510 static void
5511 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5512 struct it *it;
5513 unsigned char *s;
5514 Lisp_Object string;
5515 int charpos;
5516 int precision, field_width, multibyte;
5517 {
5518 /* No region in strings. */
5519 it->region_beg_charpos = it->region_end_charpos = -1;
5520
5521 /* No text property checks performed by default, but see below. */
5522 it->stop_charpos = -1;
5523
5524 /* Set iterator position and end position. */
5525 bzero (&it->current, sizeof it->current);
5526 it->current.overlay_string_index = -1;
5527 it->current.dpvec_index = -1;
5528 xassert (charpos >= 0);
5529
5530 /* If STRING is specified, use its multibyteness, otherwise use the
5531 setting of MULTIBYTE, if specified. */
5532 if (multibyte >= 0)
5533 it->multibyte_p = multibyte > 0;
5534
5535 if (s == NULL)
5536 {
5537 xassert (STRINGP (string));
5538 it->string = string;
5539 it->s = NULL;
5540 it->end_charpos = it->string_nchars = SCHARS (string);
5541 it->method = GET_FROM_STRING;
5542 it->current.string_pos = string_pos (charpos, string);
5543 }
5544 else
5545 {
5546 it->s = s;
5547 it->string = Qnil;
5548
5549 /* Note that we use IT->current.pos, not it->current.string_pos,
5550 for displaying C strings. */
5551 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5552 if (it->multibyte_p)
5553 {
5554 it->current.pos = c_string_pos (charpos, s, 1);
5555 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5556 }
5557 else
5558 {
5559 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5560 it->end_charpos = it->string_nchars = strlen (s);
5561 }
5562
5563 it->method = GET_FROM_C_STRING;
5564 }
5565
5566 /* PRECISION > 0 means don't return more than PRECISION characters
5567 from the string. */
5568 if (precision > 0 && it->end_charpos - charpos > precision)
5569 it->end_charpos = it->string_nchars = charpos + precision;
5570
5571 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5572 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5573 FIELD_WIDTH < 0 means infinite field width. This is useful for
5574 padding with `-' at the end of a mode line. */
5575 if (field_width < 0)
5576 field_width = INFINITY;
5577 if (field_width > it->end_charpos - charpos)
5578 it->end_charpos = charpos + field_width;
5579
5580 /* Use the standard display table for displaying strings. */
5581 if (DISP_TABLE_P (Vstandard_display_table))
5582 it->dp = XCHAR_TABLE (Vstandard_display_table);
5583
5584 it->stop_charpos = charpos;
5585 if (s == NULL && it->multibyte_p)
5586 {
5587 EMACS_INT endpos = SCHARS (it->string);
5588 if (endpos > it->end_charpos)
5589 endpos = it->end_charpos;
5590 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5591 it->string);
5592 }
5593 CHECK_IT (it);
5594 }
5595
5596
5597 \f
5598 /***********************************************************************
5599 Iteration
5600 ***********************************************************************/
5601
5602 /* Map enum it_method value to corresponding next_element_from_* function. */
5603
5604 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5605 {
5606 next_element_from_buffer,
5607 next_element_from_display_vector,
5608 next_element_from_string,
5609 next_element_from_c_string,
5610 next_element_from_image,
5611 next_element_from_stretch
5612 };
5613
5614 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5615
5616
5617 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5618 (possibly with the following characters). */
5619
5620 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5621 ((IT)->cmp_it.id >= 0 \
5622 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5623 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5624 END_CHARPOS, (IT)->w, \
5625 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5626 (IT)->string)))
5627
5628
5629 /* Load IT's display element fields with information about the next
5630 display element from the current position of IT. Value is zero if
5631 end of buffer (or C string) is reached. */
5632
5633 static struct frame *last_escape_glyph_frame = NULL;
5634 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5635 static int last_escape_glyph_merged_face_id = 0;
5636
5637 int
5638 get_next_display_element (it)
5639 struct it *it;
5640 {
5641 /* Non-zero means that we found a display element. Zero means that
5642 we hit the end of what we iterate over. Performance note: the
5643 function pointer `method' used here turns out to be faster than
5644 using a sequence of if-statements. */
5645 int success_p;
5646
5647 get_next:
5648 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5649
5650 if (it->what == IT_CHARACTER)
5651 {
5652 /* Map via display table or translate control characters.
5653 IT->c, IT->len etc. have been set to the next character by
5654 the function call above. If we have a display table, and it
5655 contains an entry for IT->c, translate it. Don't do this if
5656 IT->c itself comes from a display table, otherwise we could
5657 end up in an infinite recursion. (An alternative could be to
5658 count the recursion depth of this function and signal an
5659 error when a certain maximum depth is reached.) Is it worth
5660 it? */
5661 if (success_p && it->dpvec == NULL)
5662 {
5663 Lisp_Object dv;
5664 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5665 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5666 nbsp_or_shy = char_is_other;
5667 int c = it->c; /* This is the character to display. */
5668
5669 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5670 {
5671 xassert (SINGLE_BYTE_CHAR_P (c));
5672 if (unibyte_display_via_language_environment)
5673 {
5674 c = DECODE_CHAR (unibyte, c);
5675 if (c < 0)
5676 c = BYTE8_TO_CHAR (it->c);
5677 }
5678 else
5679 c = BYTE8_TO_CHAR (it->c);
5680 }
5681
5682 if (it->dp
5683 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5684 VECTORP (dv)))
5685 {
5686 struct Lisp_Vector *v = XVECTOR (dv);
5687
5688 /* Return the first character from the display table
5689 entry, if not empty. If empty, don't display the
5690 current character. */
5691 if (v->size)
5692 {
5693 it->dpvec_char_len = it->len;
5694 it->dpvec = v->contents;
5695 it->dpend = v->contents + v->size;
5696 it->current.dpvec_index = 0;
5697 it->dpvec_face_id = -1;
5698 it->saved_face_id = it->face_id;
5699 it->method = GET_FROM_DISPLAY_VECTOR;
5700 it->ellipsis_p = 0;
5701 }
5702 else
5703 {
5704 set_iterator_to_next (it, 0);
5705 }
5706 goto get_next;
5707 }
5708
5709 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5710 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5711 : c == 0xAD ? char_is_soft_hyphen
5712 : char_is_other);
5713
5714 /* Translate control characters into `\003' or `^C' form.
5715 Control characters coming from a display table entry are
5716 currently not translated because we use IT->dpvec to hold
5717 the translation. This could easily be changed but I
5718 don't believe that it is worth doing.
5719
5720 NBSP and SOFT-HYPEN are property translated too.
5721
5722 Non-printable characters and raw-byte characters are also
5723 translated to octal form. */
5724 if (((c < ' ' || c == 127) /* ASCII control chars */
5725 ? (it->area != TEXT_AREA
5726 /* In mode line, treat \n, \t like other crl chars. */
5727 || (c != '\t'
5728 && it->glyph_row
5729 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5730 || (c != '\n' && c != '\t'))
5731 : (nbsp_or_shy
5732 || CHAR_BYTE8_P (c)
5733 || ! CHAR_PRINTABLE_P (c))))
5734 {
5735 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5736 or a non-printable character which must be displayed
5737 either as '\003' or as `^C' where the '\\' and '^'
5738 can be defined in the display table. Fill
5739 IT->ctl_chars with glyphs for what we have to
5740 display. Then, set IT->dpvec to these glyphs. */
5741 Lisp_Object gc;
5742 int ctl_len;
5743 int face_id, lface_id = 0 ;
5744 int escape_glyph;
5745
5746 /* Handle control characters with ^. */
5747
5748 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5749 {
5750 int g;
5751
5752 g = '^'; /* default glyph for Control */
5753 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5754 if (it->dp
5755 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5756 && GLYPH_CODE_CHAR_VALID_P (gc))
5757 {
5758 g = GLYPH_CODE_CHAR (gc);
5759 lface_id = GLYPH_CODE_FACE (gc);
5760 }
5761 if (lface_id)
5762 {
5763 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5764 }
5765 else if (it->f == last_escape_glyph_frame
5766 && it->face_id == last_escape_glyph_face_id)
5767 {
5768 face_id = last_escape_glyph_merged_face_id;
5769 }
5770 else
5771 {
5772 /* Merge the escape-glyph face into the current face. */
5773 face_id = merge_faces (it->f, Qescape_glyph, 0,
5774 it->face_id);
5775 last_escape_glyph_frame = it->f;
5776 last_escape_glyph_face_id = it->face_id;
5777 last_escape_glyph_merged_face_id = face_id;
5778 }
5779
5780 XSETINT (it->ctl_chars[0], g);
5781 XSETINT (it->ctl_chars[1], c ^ 0100);
5782 ctl_len = 2;
5783 goto display_control;
5784 }
5785
5786 /* Handle non-break space in the mode where it only gets
5787 highlighting. */
5788
5789 if (EQ (Vnobreak_char_display, Qt)
5790 && nbsp_or_shy == char_is_nbsp)
5791 {
5792 /* Merge the no-break-space face into the current face. */
5793 face_id = merge_faces (it->f, Qnobreak_space, 0,
5794 it->face_id);
5795
5796 c = ' ';
5797 XSETINT (it->ctl_chars[0], ' ');
5798 ctl_len = 1;
5799 goto display_control;
5800 }
5801
5802 /* Handle sequences that start with the "escape glyph". */
5803
5804 /* the default escape glyph is \. */
5805 escape_glyph = '\\';
5806
5807 if (it->dp
5808 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5809 && GLYPH_CODE_CHAR_VALID_P (gc))
5810 {
5811 escape_glyph = GLYPH_CODE_CHAR (gc);
5812 lface_id = GLYPH_CODE_FACE (gc);
5813 }
5814 if (lface_id)
5815 {
5816 /* The display table specified a face.
5817 Merge it into face_id and also into escape_glyph. */
5818 face_id = merge_faces (it->f, Qt, lface_id,
5819 it->face_id);
5820 }
5821 else if (it->f == last_escape_glyph_frame
5822 && it->face_id == last_escape_glyph_face_id)
5823 {
5824 face_id = last_escape_glyph_merged_face_id;
5825 }
5826 else
5827 {
5828 /* Merge the escape-glyph face into the current face. */
5829 face_id = merge_faces (it->f, Qescape_glyph, 0,
5830 it->face_id);
5831 last_escape_glyph_frame = it->f;
5832 last_escape_glyph_face_id = it->face_id;
5833 last_escape_glyph_merged_face_id = face_id;
5834 }
5835
5836 /* Handle soft hyphens in the mode where they only get
5837 highlighting. */
5838
5839 if (EQ (Vnobreak_char_display, Qt)
5840 && nbsp_or_shy == char_is_soft_hyphen)
5841 {
5842 XSETINT (it->ctl_chars[0], '-');
5843 ctl_len = 1;
5844 goto display_control;
5845 }
5846
5847 /* Handle non-break space and soft hyphen
5848 with the escape glyph. */
5849
5850 if (nbsp_or_shy)
5851 {
5852 XSETINT (it->ctl_chars[0], escape_glyph);
5853 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5854 XSETINT (it->ctl_chars[1], c);
5855 ctl_len = 2;
5856 goto display_control;
5857 }
5858
5859 {
5860 char str[10];
5861 int len, i;
5862
5863 if (CHAR_BYTE8_P (c))
5864 /* Display \200 instead of \17777600. */
5865 c = CHAR_TO_BYTE8 (c);
5866 len = sprintf (str, "%03o", c);
5867
5868 XSETINT (it->ctl_chars[0], escape_glyph);
5869 for (i = 0; i < len; i++)
5870 XSETINT (it->ctl_chars[i + 1], str[i]);
5871 ctl_len = len + 1;
5872 }
5873
5874 display_control:
5875 /* Set up IT->dpvec and return first character from it. */
5876 it->dpvec_char_len = it->len;
5877 it->dpvec = it->ctl_chars;
5878 it->dpend = it->dpvec + ctl_len;
5879 it->current.dpvec_index = 0;
5880 it->dpvec_face_id = face_id;
5881 it->saved_face_id = it->face_id;
5882 it->method = GET_FROM_DISPLAY_VECTOR;
5883 it->ellipsis_p = 0;
5884 goto get_next;
5885 }
5886 it->char_to_display = c;
5887 }
5888 else if (success_p)
5889 {
5890 it->char_to_display = it->c;
5891 }
5892 }
5893
5894 #ifdef HAVE_WINDOW_SYSTEM
5895 /* Adjust face id for a multibyte character. There are no multibyte
5896 character in unibyte text. */
5897 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5898 && it->multibyte_p
5899 && success_p
5900 && FRAME_WINDOW_P (it->f))
5901 {
5902 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5903
5904 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5905 {
5906 /* Automatic composition with glyph-string. */
5907 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5908
5909 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5910 }
5911 else
5912 {
5913 int pos = (it->s ? -1
5914 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5915 : IT_CHARPOS (*it));
5916
5917 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
5918 it->string);
5919 }
5920 }
5921 #endif
5922
5923 /* Is this character the last one of a run of characters with
5924 box? If yes, set IT->end_of_box_run_p to 1. */
5925 if (it->face_box_p
5926 && it->s == NULL)
5927 {
5928 if (it->method == GET_FROM_STRING && it->sp)
5929 {
5930 int face_id = underlying_face_id (it);
5931 struct face *face = FACE_FROM_ID (it->f, face_id);
5932
5933 if (face)
5934 {
5935 if (face->box == FACE_NO_BOX)
5936 {
5937 /* If the box comes from face properties in a
5938 display string, check faces in that string. */
5939 int string_face_id = face_after_it_pos (it);
5940 it->end_of_box_run_p
5941 = (FACE_FROM_ID (it->f, string_face_id)->box
5942 == FACE_NO_BOX);
5943 }
5944 /* Otherwise, the box comes from the underlying face.
5945 If this is the last string character displayed, check
5946 the next buffer location. */
5947 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5948 && (it->current.overlay_string_index
5949 == it->n_overlay_strings - 1))
5950 {
5951 EMACS_INT ignore;
5952 int next_face_id;
5953 struct text_pos pos = it->current.pos;
5954 INC_TEXT_POS (pos, it->multibyte_p);
5955
5956 next_face_id = face_at_buffer_position
5957 (it->w, CHARPOS (pos), it->region_beg_charpos,
5958 it->region_end_charpos, &ignore,
5959 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5960 -1);
5961 it->end_of_box_run_p
5962 = (FACE_FROM_ID (it->f, next_face_id)->box
5963 == FACE_NO_BOX);
5964 }
5965 }
5966 }
5967 else
5968 {
5969 int face_id = face_after_it_pos (it);
5970 it->end_of_box_run_p
5971 = (face_id != it->face_id
5972 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
5973 }
5974 }
5975
5976 /* Value is 0 if end of buffer or string reached. */
5977 return success_p;
5978 }
5979
5980
5981 /* Move IT to the next display element.
5982
5983 RESEAT_P non-zero means if called on a newline in buffer text,
5984 skip to the next visible line start.
5985
5986 Functions get_next_display_element and set_iterator_to_next are
5987 separate because I find this arrangement easier to handle than a
5988 get_next_display_element function that also increments IT's
5989 position. The way it is we can first look at an iterator's current
5990 display element, decide whether it fits on a line, and if it does,
5991 increment the iterator position. The other way around we probably
5992 would either need a flag indicating whether the iterator has to be
5993 incremented the next time, or we would have to implement a
5994 decrement position function which would not be easy to write. */
5995
5996 void
5997 set_iterator_to_next (it, reseat_p)
5998 struct it *it;
5999 int reseat_p;
6000 {
6001 /* Reset flags indicating start and end of a sequence of characters
6002 with box. Reset them at the start of this function because
6003 moving the iterator to a new position might set them. */
6004 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6005
6006 switch (it->method)
6007 {
6008 case GET_FROM_BUFFER:
6009 /* The current display element of IT is a character from
6010 current_buffer. Advance in the buffer, and maybe skip over
6011 invisible lines that are so because of selective display. */
6012 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6013 reseat_at_next_visible_line_start (it, 0);
6014 else if (it->cmp_it.id >= 0)
6015 {
6016 IT_CHARPOS (*it) += it->cmp_it.nchars;
6017 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6018 if (it->cmp_it.to < it->cmp_it.nglyphs)
6019 it->cmp_it.from = it->cmp_it.to;
6020 else
6021 {
6022 it->cmp_it.id = -1;
6023 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6024 IT_BYTEPOS (*it), it->stop_charpos,
6025 Qnil);
6026 }
6027 }
6028 else
6029 {
6030 xassert (it->len != 0);
6031 IT_BYTEPOS (*it) += it->len;
6032 IT_CHARPOS (*it) += 1;
6033 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6034 }
6035 break;
6036
6037 case GET_FROM_C_STRING:
6038 /* Current display element of IT is from a C string. */
6039 IT_BYTEPOS (*it) += it->len;
6040 IT_CHARPOS (*it) += 1;
6041 break;
6042
6043 case GET_FROM_DISPLAY_VECTOR:
6044 /* Current display element of IT is from a display table entry.
6045 Advance in the display table definition. Reset it to null if
6046 end reached, and continue with characters from buffers/
6047 strings. */
6048 ++it->current.dpvec_index;
6049
6050 /* Restore face of the iterator to what they were before the
6051 display vector entry (these entries may contain faces). */
6052 it->face_id = it->saved_face_id;
6053
6054 if (it->dpvec + it->current.dpvec_index == it->dpend)
6055 {
6056 int recheck_faces = it->ellipsis_p;
6057
6058 if (it->s)
6059 it->method = GET_FROM_C_STRING;
6060 else if (STRINGP (it->string))
6061 it->method = GET_FROM_STRING;
6062 else
6063 {
6064 it->method = GET_FROM_BUFFER;
6065 it->object = it->w->buffer;
6066 }
6067
6068 it->dpvec = NULL;
6069 it->current.dpvec_index = -1;
6070
6071 /* Skip over characters which were displayed via IT->dpvec. */
6072 if (it->dpvec_char_len < 0)
6073 reseat_at_next_visible_line_start (it, 1);
6074 else if (it->dpvec_char_len > 0)
6075 {
6076 if (it->method == GET_FROM_STRING
6077 && it->n_overlay_strings > 0)
6078 it->ignore_overlay_strings_at_pos_p = 1;
6079 it->len = it->dpvec_char_len;
6080 set_iterator_to_next (it, reseat_p);
6081 }
6082
6083 /* Maybe recheck faces after display vector */
6084 if (recheck_faces)
6085 it->stop_charpos = IT_CHARPOS (*it);
6086 }
6087 break;
6088
6089 case GET_FROM_STRING:
6090 /* Current display element is a character from a Lisp string. */
6091 xassert (it->s == NULL && STRINGP (it->string));
6092 if (it->cmp_it.id >= 0)
6093 {
6094 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6095 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6096 if (it->cmp_it.to < it->cmp_it.nglyphs)
6097 it->cmp_it.from = it->cmp_it.to;
6098 else
6099 {
6100 it->cmp_it.id = -1;
6101 composition_compute_stop_pos (&it->cmp_it,
6102 IT_STRING_CHARPOS (*it),
6103 IT_STRING_BYTEPOS (*it),
6104 it->stop_charpos, it->string);
6105 }
6106 }
6107 else
6108 {
6109 IT_STRING_BYTEPOS (*it) += it->len;
6110 IT_STRING_CHARPOS (*it) += 1;
6111 }
6112
6113 consider_string_end:
6114
6115 if (it->current.overlay_string_index >= 0)
6116 {
6117 /* IT->string is an overlay string. Advance to the
6118 next, if there is one. */
6119 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6120 {
6121 it->ellipsis_p = 0;
6122 next_overlay_string (it);
6123 if (it->ellipsis_p)
6124 setup_for_ellipsis (it, 0);
6125 }
6126 }
6127 else
6128 {
6129 /* IT->string is not an overlay string. If we reached
6130 its end, and there is something on IT->stack, proceed
6131 with what is on the stack. This can be either another
6132 string, this time an overlay string, or a buffer. */
6133 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6134 && it->sp > 0)
6135 {
6136 pop_it (it);
6137 if (it->method == GET_FROM_STRING)
6138 goto consider_string_end;
6139 }
6140 }
6141 break;
6142
6143 case GET_FROM_IMAGE:
6144 case GET_FROM_STRETCH:
6145 /* The position etc with which we have to proceed are on
6146 the stack. The position may be at the end of a string,
6147 if the `display' property takes up the whole string. */
6148 xassert (it->sp > 0);
6149 pop_it (it);
6150 if (it->method == GET_FROM_STRING)
6151 goto consider_string_end;
6152 break;
6153
6154 default:
6155 /* There are no other methods defined, so this should be a bug. */
6156 abort ();
6157 }
6158
6159 xassert (it->method != GET_FROM_STRING
6160 || (STRINGP (it->string)
6161 && IT_STRING_CHARPOS (*it) >= 0));
6162 }
6163
6164 /* Load IT's display element fields with information about the next
6165 display element which comes from a display table entry or from the
6166 result of translating a control character to one of the forms `^C'
6167 or `\003'.
6168
6169 IT->dpvec holds the glyphs to return as characters.
6170 IT->saved_face_id holds the face id before the display vector--it
6171 is restored into IT->face_id in set_iterator_to_next. */
6172
6173 static int
6174 next_element_from_display_vector (it)
6175 struct it *it;
6176 {
6177 Lisp_Object gc;
6178
6179 /* Precondition. */
6180 xassert (it->dpvec && it->current.dpvec_index >= 0);
6181
6182 it->face_id = it->saved_face_id;
6183
6184 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6185 That seemed totally bogus - so I changed it... */
6186 gc = it->dpvec[it->current.dpvec_index];
6187
6188 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6189 {
6190 it->c = GLYPH_CODE_CHAR (gc);
6191 it->len = CHAR_BYTES (it->c);
6192
6193 /* The entry may contain a face id to use. Such a face id is
6194 the id of a Lisp face, not a realized face. A face id of
6195 zero means no face is specified. */
6196 if (it->dpvec_face_id >= 0)
6197 it->face_id = it->dpvec_face_id;
6198 else
6199 {
6200 int lface_id = GLYPH_CODE_FACE (gc);
6201 if (lface_id > 0)
6202 it->face_id = merge_faces (it->f, Qt, lface_id,
6203 it->saved_face_id);
6204 }
6205 }
6206 else
6207 /* Display table entry is invalid. Return a space. */
6208 it->c = ' ', it->len = 1;
6209
6210 /* Don't change position and object of the iterator here. They are
6211 still the values of the character that had this display table
6212 entry or was translated, and that's what we want. */
6213 it->what = IT_CHARACTER;
6214 return 1;
6215 }
6216
6217
6218 /* Load IT with the next display element from Lisp string IT->string.
6219 IT->current.string_pos is the current position within the string.
6220 If IT->current.overlay_string_index >= 0, the Lisp string is an
6221 overlay string. */
6222
6223 static int
6224 next_element_from_string (it)
6225 struct it *it;
6226 {
6227 struct text_pos position;
6228
6229 xassert (STRINGP (it->string));
6230 xassert (IT_STRING_CHARPOS (*it) >= 0);
6231 position = it->current.string_pos;
6232
6233 /* Time to check for invisible text? */
6234 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6235 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6236 {
6237 handle_stop (it);
6238
6239 /* Since a handler may have changed IT->method, we must
6240 recurse here. */
6241 return GET_NEXT_DISPLAY_ELEMENT (it);
6242 }
6243
6244 if (it->current.overlay_string_index >= 0)
6245 {
6246 /* Get the next character from an overlay string. In overlay
6247 strings, There is no field width or padding with spaces to
6248 do. */
6249 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6250 {
6251 it->what = IT_EOB;
6252 return 0;
6253 }
6254 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6255 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6256 && next_element_from_composition (it))
6257 {
6258 return 1;
6259 }
6260 else if (STRING_MULTIBYTE (it->string))
6261 {
6262 const unsigned char *s = (SDATA (it->string)
6263 + IT_STRING_BYTEPOS (*it));
6264 it->c = string_char_and_length (s, &it->len);
6265 }
6266 else
6267 {
6268 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6269 it->len = 1;
6270 }
6271 }
6272 else
6273 {
6274 /* Get the next character from a Lisp string that is not an
6275 overlay string. Such strings come from the mode line, for
6276 example. We may have to pad with spaces, or truncate the
6277 string. See also next_element_from_c_string. */
6278 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6279 {
6280 it->what = IT_EOB;
6281 return 0;
6282 }
6283 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6284 {
6285 /* Pad with spaces. */
6286 it->c = ' ', it->len = 1;
6287 CHARPOS (position) = BYTEPOS (position) = -1;
6288 }
6289 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6290 IT_STRING_BYTEPOS (*it), it->string_nchars)
6291 && next_element_from_composition (it))
6292 {
6293 return 1;
6294 }
6295 else if (STRING_MULTIBYTE (it->string))
6296 {
6297 const unsigned char *s = (SDATA (it->string)
6298 + IT_STRING_BYTEPOS (*it));
6299 it->c = string_char_and_length (s, &it->len);
6300 }
6301 else
6302 {
6303 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6304 it->len = 1;
6305 }
6306 }
6307
6308 /* Record what we have and where it came from. */
6309 it->what = IT_CHARACTER;
6310 it->object = it->string;
6311 it->position = position;
6312 return 1;
6313 }
6314
6315
6316 /* Load IT with next display element from C string IT->s.
6317 IT->string_nchars is the maximum number of characters to return
6318 from the string. IT->end_charpos may be greater than
6319 IT->string_nchars when this function is called, in which case we
6320 may have to return padding spaces. Value is zero if end of string
6321 reached, including padding spaces. */
6322
6323 static int
6324 next_element_from_c_string (it)
6325 struct it *it;
6326 {
6327 int success_p = 1;
6328
6329 xassert (it->s);
6330 it->what = IT_CHARACTER;
6331 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6332 it->object = Qnil;
6333
6334 /* IT's position can be greater IT->string_nchars in case a field
6335 width or precision has been specified when the iterator was
6336 initialized. */
6337 if (IT_CHARPOS (*it) >= it->end_charpos)
6338 {
6339 /* End of the game. */
6340 it->what = IT_EOB;
6341 success_p = 0;
6342 }
6343 else if (IT_CHARPOS (*it) >= it->string_nchars)
6344 {
6345 /* Pad with spaces. */
6346 it->c = ' ', it->len = 1;
6347 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6348 }
6349 else if (it->multibyte_p)
6350 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6351 else
6352 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6353
6354 return success_p;
6355 }
6356
6357
6358 /* Set up IT to return characters from an ellipsis, if appropriate.
6359 The definition of the ellipsis glyphs may come from a display table
6360 entry. This function fills IT with the first glyph from the
6361 ellipsis if an ellipsis is to be displayed. */
6362
6363 static int
6364 next_element_from_ellipsis (it)
6365 struct it *it;
6366 {
6367 if (it->selective_display_ellipsis_p)
6368 setup_for_ellipsis (it, it->len);
6369 else
6370 {
6371 /* The face at the current position may be different from the
6372 face we find after the invisible text. Remember what it
6373 was in IT->saved_face_id, and signal that it's there by
6374 setting face_before_selective_p. */
6375 it->saved_face_id = it->face_id;
6376 it->method = GET_FROM_BUFFER;
6377 it->object = it->w->buffer;
6378 reseat_at_next_visible_line_start (it, 1);
6379 it->face_before_selective_p = 1;
6380 }
6381
6382 return GET_NEXT_DISPLAY_ELEMENT (it);
6383 }
6384
6385
6386 /* Deliver an image display element. The iterator IT is already
6387 filled with image information (done in handle_display_prop). Value
6388 is always 1. */
6389
6390
6391 static int
6392 next_element_from_image (it)
6393 struct it *it;
6394 {
6395 it->what = IT_IMAGE;
6396 it->ignore_overlay_strings_at_pos_p = 0;
6397 return 1;
6398 }
6399
6400
6401 /* Fill iterator IT with next display element from a stretch glyph
6402 property. IT->object is the value of the text property. Value is
6403 always 1. */
6404
6405 static int
6406 next_element_from_stretch (it)
6407 struct it *it;
6408 {
6409 it->what = IT_STRETCH;
6410 return 1;
6411 }
6412
6413
6414 /* Load IT with the next display element from current_buffer. Value
6415 is zero if end of buffer reached. IT->stop_charpos is the next
6416 position at which to stop and check for text properties or buffer
6417 end. */
6418
6419 static int
6420 next_element_from_buffer (it)
6421 struct it *it;
6422 {
6423 int success_p = 1;
6424
6425 xassert (IT_CHARPOS (*it) >= BEGV);
6426
6427 if (IT_CHARPOS (*it) >= it->stop_charpos)
6428 {
6429 if (IT_CHARPOS (*it) >= it->end_charpos)
6430 {
6431 int overlay_strings_follow_p;
6432
6433 /* End of the game, except when overlay strings follow that
6434 haven't been returned yet. */
6435 if (it->overlay_strings_at_end_processed_p)
6436 overlay_strings_follow_p = 0;
6437 else
6438 {
6439 it->overlay_strings_at_end_processed_p = 1;
6440 overlay_strings_follow_p = get_overlay_strings (it, 0);
6441 }
6442
6443 if (overlay_strings_follow_p)
6444 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6445 else
6446 {
6447 it->what = IT_EOB;
6448 it->position = it->current.pos;
6449 success_p = 0;
6450 }
6451 }
6452 else
6453 {
6454 handle_stop (it);
6455 return GET_NEXT_DISPLAY_ELEMENT (it);
6456 }
6457 }
6458 else
6459 {
6460 /* No face changes, overlays etc. in sight, so just return a
6461 character from current_buffer. */
6462 unsigned char *p;
6463
6464 /* Maybe run the redisplay end trigger hook. Performance note:
6465 This doesn't seem to cost measurable time. */
6466 if (it->redisplay_end_trigger_charpos
6467 && it->glyph_row
6468 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6469 run_redisplay_end_trigger_hook (it);
6470
6471 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6472 it->end_charpos)
6473 && next_element_from_composition (it))
6474 {
6475 return 1;
6476 }
6477
6478 /* Get the next character, maybe multibyte. */
6479 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6480 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6481 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6482 else
6483 it->c = *p, it->len = 1;
6484
6485 /* Record what we have and where it came from. */
6486 it->what = IT_CHARACTER;
6487 it->object = it->w->buffer;
6488 it->position = it->current.pos;
6489
6490 /* Normally we return the character found above, except when we
6491 really want to return an ellipsis for selective display. */
6492 if (it->selective)
6493 {
6494 if (it->c == '\n')
6495 {
6496 /* A value of selective > 0 means hide lines indented more
6497 than that number of columns. */
6498 if (it->selective > 0
6499 && IT_CHARPOS (*it) + 1 < ZV
6500 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6501 IT_BYTEPOS (*it) + 1,
6502 (double) it->selective)) /* iftc */
6503 {
6504 success_p = next_element_from_ellipsis (it);
6505 it->dpvec_char_len = -1;
6506 }
6507 }
6508 else if (it->c == '\r' && it->selective == -1)
6509 {
6510 /* A value of selective == -1 means that everything from the
6511 CR to the end of the line is invisible, with maybe an
6512 ellipsis displayed for it. */
6513 success_p = next_element_from_ellipsis (it);
6514 it->dpvec_char_len = -1;
6515 }
6516 }
6517 }
6518
6519 /* Value is zero if end of buffer reached. */
6520 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6521 return success_p;
6522 }
6523
6524
6525 /* Run the redisplay end trigger hook for IT. */
6526
6527 static void
6528 run_redisplay_end_trigger_hook (it)
6529 struct it *it;
6530 {
6531 Lisp_Object args[3];
6532
6533 /* IT->glyph_row should be non-null, i.e. we should be actually
6534 displaying something, or otherwise we should not run the hook. */
6535 xassert (it->glyph_row);
6536
6537 /* Set up hook arguments. */
6538 args[0] = Qredisplay_end_trigger_functions;
6539 args[1] = it->window;
6540 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6541 it->redisplay_end_trigger_charpos = 0;
6542
6543 /* Since we are *trying* to run these functions, don't try to run
6544 them again, even if they get an error. */
6545 it->w->redisplay_end_trigger = Qnil;
6546 Frun_hook_with_args (3, args);
6547
6548 /* Notice if it changed the face of the character we are on. */
6549 handle_face_prop (it);
6550 }
6551
6552
6553 /* Deliver a composition display element. Unlike the other
6554 next_element_from_XXX, this function is not registered in the array
6555 get_next_element[]. It is called from next_element_from_buffer and
6556 next_element_from_string when necessary. */
6557
6558 static int
6559 next_element_from_composition (it)
6560 struct it *it;
6561 {
6562 it->what = IT_COMPOSITION;
6563 it->len = it->cmp_it.nbytes;
6564 if (STRINGP (it->string))
6565 {
6566 if (it->c < 0)
6567 {
6568 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6569 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6570 return 0;
6571 }
6572 it->position = it->current.string_pos;
6573 it->object = it->string;
6574 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6575 IT_STRING_BYTEPOS (*it), it->string);
6576 }
6577 else
6578 {
6579 if (it->c < 0)
6580 {
6581 IT_CHARPOS (*it) += it->cmp_it.nchars;
6582 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6583 return 0;
6584 }
6585 it->position = it->current.pos;
6586 it->object = it->w->buffer;
6587 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6588 IT_BYTEPOS (*it), Qnil);
6589 }
6590 return 1;
6591 }
6592
6593
6594 \f
6595 /***********************************************************************
6596 Moving an iterator without producing glyphs
6597 ***********************************************************************/
6598
6599 /* Check if iterator is at a position corresponding to a valid buffer
6600 position after some move_it_ call. */
6601
6602 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6603 ((it)->method == GET_FROM_STRING \
6604 ? IT_STRING_CHARPOS (*it) == 0 \
6605 : 1)
6606
6607
6608 /* Move iterator IT to a specified buffer or X position within one
6609 line on the display without producing glyphs.
6610
6611 OP should be a bit mask including some or all of these bits:
6612 MOVE_TO_X: Stop on reaching x-position TO_X.
6613 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6614 Regardless of OP's value, stop in reaching the end of the display line.
6615
6616 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6617 This means, in particular, that TO_X includes window's horizontal
6618 scroll amount.
6619
6620 The return value has several possible values that
6621 say what condition caused the scan to stop:
6622
6623 MOVE_POS_MATCH_OR_ZV
6624 - when TO_POS or ZV was reached.
6625
6626 MOVE_X_REACHED
6627 -when TO_X was reached before TO_POS or ZV were reached.
6628
6629 MOVE_LINE_CONTINUED
6630 - when we reached the end of the display area and the line must
6631 be continued.
6632
6633 MOVE_LINE_TRUNCATED
6634 - when we reached the end of the display area and the line is
6635 truncated.
6636
6637 MOVE_NEWLINE_OR_CR
6638 - when we stopped at a line end, i.e. a newline or a CR and selective
6639 display is on. */
6640
6641 static enum move_it_result
6642 move_it_in_display_line_to (struct it *it,
6643 EMACS_INT to_charpos, int to_x,
6644 enum move_operation_enum op)
6645 {
6646 enum move_it_result result = MOVE_UNDEFINED;
6647 struct glyph_row *saved_glyph_row;
6648 struct it wrap_it, atpos_it, atx_it;
6649 int may_wrap = 0;
6650
6651 /* Don't produce glyphs in produce_glyphs. */
6652 saved_glyph_row = it->glyph_row;
6653 it->glyph_row = NULL;
6654
6655 /* Use wrap_it to save a copy of IT wherever a word wrap could
6656 occur. Use atpos_it to save a copy of IT at the desired buffer
6657 position, if found, so that we can scan ahead and check if the
6658 word later overshoots the window edge. Use atx_it similarly, for
6659 pixel positions. */
6660 wrap_it.sp = -1;
6661 atpos_it.sp = -1;
6662 atx_it.sp = -1;
6663
6664 #define BUFFER_POS_REACHED_P() \
6665 ((op & MOVE_TO_POS) != 0 \
6666 && BUFFERP (it->object) \
6667 && IT_CHARPOS (*it) >= to_charpos \
6668 && (it->method == GET_FROM_BUFFER \
6669 || (it->method == GET_FROM_DISPLAY_VECTOR \
6670 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6671
6672 /* If there's a line-/wrap-prefix, handle it. */
6673 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6674 && it->current_y < it->last_visible_y)
6675 handle_line_prefix (it);
6676
6677 while (1)
6678 {
6679 int x, i, ascent = 0, descent = 0;
6680
6681 /* Utility macro to reset an iterator with x, ascent, and descent. */
6682 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6683 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6684 (IT)->max_descent = descent)
6685
6686 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6687 glyph). */
6688 if ((op & MOVE_TO_POS) != 0
6689 && BUFFERP (it->object)
6690 && it->method == GET_FROM_BUFFER
6691 && IT_CHARPOS (*it) > to_charpos)
6692 {
6693 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6694 {
6695 result = MOVE_POS_MATCH_OR_ZV;
6696 break;
6697 }
6698 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6699 /* If wrap_it is valid, the current position might be in a
6700 word that is wrapped. So, save the iterator in
6701 atpos_it and continue to see if wrapping happens. */
6702 atpos_it = *it;
6703 }
6704
6705 /* Stop when ZV reached.
6706 We used to stop here when TO_CHARPOS reached as well, but that is
6707 too soon if this glyph does not fit on this line. So we handle it
6708 explicitly below. */
6709 if (!get_next_display_element (it))
6710 {
6711 result = MOVE_POS_MATCH_OR_ZV;
6712 break;
6713 }
6714
6715 if (it->line_wrap == TRUNCATE)
6716 {
6717 if (BUFFER_POS_REACHED_P ())
6718 {
6719 result = MOVE_POS_MATCH_OR_ZV;
6720 break;
6721 }
6722 }
6723 else
6724 {
6725 if (it->line_wrap == WORD_WRAP)
6726 {
6727 if (IT_DISPLAYING_WHITESPACE (it))
6728 may_wrap = 1;
6729 else if (may_wrap)
6730 {
6731 /* We have reached a glyph that follows one or more
6732 whitespace characters. If the position is
6733 already found, we are done. */
6734 if (atpos_it.sp >= 0)
6735 {
6736 *it = atpos_it;
6737 result = MOVE_POS_MATCH_OR_ZV;
6738 goto done;
6739 }
6740 if (atx_it.sp >= 0)
6741 {
6742 *it = atx_it;
6743 result = MOVE_X_REACHED;
6744 goto done;
6745 }
6746 /* Otherwise, we can wrap here. */
6747 wrap_it = *it;
6748 may_wrap = 0;
6749 }
6750 }
6751 }
6752
6753 /* Remember the line height for the current line, in case
6754 the next element doesn't fit on the line. */
6755 ascent = it->max_ascent;
6756 descent = it->max_descent;
6757
6758 /* The call to produce_glyphs will get the metrics of the
6759 display element IT is loaded with. Record the x-position
6760 before this display element, in case it doesn't fit on the
6761 line. */
6762 x = it->current_x;
6763
6764 PRODUCE_GLYPHS (it);
6765
6766 if (it->area != TEXT_AREA)
6767 {
6768 set_iterator_to_next (it, 1);
6769 continue;
6770 }
6771
6772 /* The number of glyphs we get back in IT->nglyphs will normally
6773 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6774 character on a terminal frame, or (iii) a line end. For the
6775 second case, IT->nglyphs - 1 padding glyphs will be present.
6776 (On X frames, there is only one glyph produced for a
6777 composite character.)
6778
6779 The behavior implemented below means, for continuation lines,
6780 that as many spaces of a TAB as fit on the current line are
6781 displayed there. For terminal frames, as many glyphs of a
6782 multi-glyph character are displayed in the current line, too.
6783 This is what the old redisplay code did, and we keep it that
6784 way. Under X, the whole shape of a complex character must
6785 fit on the line or it will be completely displayed in the
6786 next line.
6787
6788 Note that both for tabs and padding glyphs, all glyphs have
6789 the same width. */
6790 if (it->nglyphs)
6791 {
6792 /* More than one glyph or glyph doesn't fit on line. All
6793 glyphs have the same width. */
6794 int single_glyph_width = it->pixel_width / it->nglyphs;
6795 int new_x;
6796 int x_before_this_char = x;
6797 int hpos_before_this_char = it->hpos;
6798
6799 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6800 {
6801 new_x = x + single_glyph_width;
6802
6803 /* We want to leave anything reaching TO_X to the caller. */
6804 if ((op & MOVE_TO_X) && new_x > to_x)
6805 {
6806 if (BUFFER_POS_REACHED_P ())
6807 {
6808 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6809 goto buffer_pos_reached;
6810 if (atpos_it.sp < 0)
6811 {
6812 atpos_it = *it;
6813 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6814 }
6815 }
6816 else
6817 {
6818 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6819 {
6820 it->current_x = x;
6821 result = MOVE_X_REACHED;
6822 break;
6823 }
6824 if (atx_it.sp < 0)
6825 {
6826 atx_it = *it;
6827 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6828 }
6829 }
6830 }
6831
6832 if (/* Lines are continued. */
6833 it->line_wrap != TRUNCATE
6834 && (/* And glyph doesn't fit on the line. */
6835 new_x > it->last_visible_x
6836 /* Or it fits exactly and we're on a window
6837 system frame. */
6838 || (new_x == it->last_visible_x
6839 && FRAME_WINDOW_P (it->f))))
6840 {
6841 if (/* IT->hpos == 0 means the very first glyph
6842 doesn't fit on the line, e.g. a wide image. */
6843 it->hpos == 0
6844 || (new_x == it->last_visible_x
6845 && FRAME_WINDOW_P (it->f)))
6846 {
6847 ++it->hpos;
6848 it->current_x = new_x;
6849
6850 /* The character's last glyph just barely fits
6851 in this row. */
6852 if (i == it->nglyphs - 1)
6853 {
6854 /* If this is the destination position,
6855 return a position *before* it in this row,
6856 now that we know it fits in this row. */
6857 if (BUFFER_POS_REACHED_P ())
6858 {
6859 if (it->line_wrap != WORD_WRAP
6860 || wrap_it.sp < 0)
6861 {
6862 it->hpos = hpos_before_this_char;
6863 it->current_x = x_before_this_char;
6864 result = MOVE_POS_MATCH_OR_ZV;
6865 break;
6866 }
6867 if (it->line_wrap == WORD_WRAP
6868 && atpos_it.sp < 0)
6869 {
6870 atpos_it = *it;
6871 atpos_it.current_x = x_before_this_char;
6872 atpos_it.hpos = hpos_before_this_char;
6873 }
6874 }
6875
6876 set_iterator_to_next (it, 1);
6877 /* On graphical terminals, newlines may
6878 "overflow" into the fringe if
6879 overflow-newline-into-fringe is non-nil.
6880 On text-only terminals, newlines may
6881 overflow into the last glyph on the
6882 display line.*/
6883 if (!FRAME_WINDOW_P (it->f)
6884 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6885 {
6886 if (!get_next_display_element (it))
6887 {
6888 result = MOVE_POS_MATCH_OR_ZV;
6889 break;
6890 }
6891 if (BUFFER_POS_REACHED_P ())
6892 {
6893 if (ITERATOR_AT_END_OF_LINE_P (it))
6894 result = MOVE_POS_MATCH_OR_ZV;
6895 else
6896 result = MOVE_LINE_CONTINUED;
6897 break;
6898 }
6899 if (ITERATOR_AT_END_OF_LINE_P (it))
6900 {
6901 result = MOVE_NEWLINE_OR_CR;
6902 break;
6903 }
6904 }
6905 }
6906 }
6907 else
6908 IT_RESET_X_ASCENT_DESCENT (it);
6909
6910 if (wrap_it.sp >= 0)
6911 {
6912 *it = wrap_it;
6913 atpos_it.sp = -1;
6914 atx_it.sp = -1;
6915 }
6916
6917 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6918 IT_CHARPOS (*it)));
6919 result = MOVE_LINE_CONTINUED;
6920 break;
6921 }
6922
6923 if (BUFFER_POS_REACHED_P ())
6924 {
6925 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6926 goto buffer_pos_reached;
6927 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6928 {
6929 atpos_it = *it;
6930 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6931 }
6932 }
6933
6934 if (new_x > it->first_visible_x)
6935 {
6936 /* Glyph is visible. Increment number of glyphs that
6937 would be displayed. */
6938 ++it->hpos;
6939 }
6940 }
6941
6942 if (result != MOVE_UNDEFINED)
6943 break;
6944 }
6945 else if (BUFFER_POS_REACHED_P ())
6946 {
6947 buffer_pos_reached:
6948 IT_RESET_X_ASCENT_DESCENT (it);
6949 result = MOVE_POS_MATCH_OR_ZV;
6950 break;
6951 }
6952 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6953 {
6954 /* Stop when TO_X specified and reached. This check is
6955 necessary here because of lines consisting of a line end,
6956 only. The line end will not produce any glyphs and we
6957 would never get MOVE_X_REACHED. */
6958 xassert (it->nglyphs == 0);
6959 result = MOVE_X_REACHED;
6960 break;
6961 }
6962
6963 /* Is this a line end? If yes, we're done. */
6964 if (ITERATOR_AT_END_OF_LINE_P (it))
6965 {
6966 result = MOVE_NEWLINE_OR_CR;
6967 break;
6968 }
6969
6970 /* The current display element has been consumed. Advance
6971 to the next. */
6972 set_iterator_to_next (it, 1);
6973
6974 /* Stop if lines are truncated and IT's current x-position is
6975 past the right edge of the window now. */
6976 if (it->line_wrap == TRUNCATE
6977 && it->current_x >= it->last_visible_x)
6978 {
6979 if (!FRAME_WINDOW_P (it->f)
6980 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6981 {
6982 if (!get_next_display_element (it)
6983 || BUFFER_POS_REACHED_P ())
6984 {
6985 result = MOVE_POS_MATCH_OR_ZV;
6986 break;
6987 }
6988 if (ITERATOR_AT_END_OF_LINE_P (it))
6989 {
6990 result = MOVE_NEWLINE_OR_CR;
6991 break;
6992 }
6993 }
6994 result = MOVE_LINE_TRUNCATED;
6995 break;
6996 }
6997 #undef IT_RESET_X_ASCENT_DESCENT
6998 }
6999
7000 #undef BUFFER_POS_REACHED_P
7001
7002 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7003 restore the saved iterator. */
7004 if (atpos_it.sp >= 0)
7005 *it = atpos_it;
7006 else if (atx_it.sp >= 0)
7007 *it = atx_it;
7008
7009 done:
7010
7011 /* Restore the iterator settings altered at the beginning of this
7012 function. */
7013 it->glyph_row = saved_glyph_row;
7014 return result;
7015 }
7016
7017 /* For external use. */
7018 void
7019 move_it_in_display_line (struct it *it,
7020 EMACS_INT to_charpos, int to_x,
7021 enum move_operation_enum op)
7022 {
7023 if (it->line_wrap == WORD_WRAP
7024 && (op & MOVE_TO_X))
7025 {
7026 struct it save_it = *it;
7027 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7028 /* When word-wrap is on, TO_X may lie past the end
7029 of a wrapped line. Then it->current is the
7030 character on the next line, so backtrack to the
7031 space before the wrap point. */
7032 if (skip == MOVE_LINE_CONTINUED)
7033 {
7034 int prev_x = max (it->current_x - 1, 0);
7035 *it = save_it;
7036 move_it_in_display_line_to
7037 (it, -1, prev_x, MOVE_TO_X);
7038 }
7039 }
7040 else
7041 move_it_in_display_line_to (it, to_charpos, to_x, op);
7042 }
7043
7044
7045 /* Move IT forward until it satisfies one or more of the criteria in
7046 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7047
7048 OP is a bit-mask that specifies where to stop, and in particular,
7049 which of those four position arguments makes a difference. See the
7050 description of enum move_operation_enum.
7051
7052 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7053 screen line, this function will set IT to the next position >
7054 TO_CHARPOS. */
7055
7056 void
7057 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7058 struct it *it;
7059 int to_charpos, to_x, to_y, to_vpos;
7060 int op;
7061 {
7062 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7063 int line_height, line_start_x = 0, reached = 0;
7064
7065 for (;;)
7066 {
7067 if (op & MOVE_TO_VPOS)
7068 {
7069 /* If no TO_CHARPOS and no TO_X specified, stop at the
7070 start of the line TO_VPOS. */
7071 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7072 {
7073 if (it->vpos == to_vpos)
7074 {
7075 reached = 1;
7076 break;
7077 }
7078 else
7079 skip = move_it_in_display_line_to (it, -1, -1, 0);
7080 }
7081 else
7082 {
7083 /* TO_VPOS >= 0 means stop at TO_X in the line at
7084 TO_VPOS, or at TO_POS, whichever comes first. */
7085 if (it->vpos == to_vpos)
7086 {
7087 reached = 2;
7088 break;
7089 }
7090
7091 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7092
7093 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7094 {
7095 reached = 3;
7096 break;
7097 }
7098 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7099 {
7100 /* We have reached TO_X but not in the line we want. */
7101 skip = move_it_in_display_line_to (it, to_charpos,
7102 -1, MOVE_TO_POS);
7103 if (skip == MOVE_POS_MATCH_OR_ZV)
7104 {
7105 reached = 4;
7106 break;
7107 }
7108 }
7109 }
7110 }
7111 else if (op & MOVE_TO_Y)
7112 {
7113 struct it it_backup;
7114
7115 if (it->line_wrap == WORD_WRAP)
7116 it_backup = *it;
7117
7118 /* TO_Y specified means stop at TO_X in the line containing
7119 TO_Y---or at TO_CHARPOS if this is reached first. The
7120 problem is that we can't really tell whether the line
7121 contains TO_Y before we have completely scanned it, and
7122 this may skip past TO_X. What we do is to first scan to
7123 TO_X.
7124
7125 If TO_X is not specified, use a TO_X of zero. The reason
7126 is to make the outcome of this function more predictable.
7127 If we didn't use TO_X == 0, we would stop at the end of
7128 the line which is probably not what a caller would expect
7129 to happen. */
7130 skip = move_it_in_display_line_to
7131 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7132 (MOVE_TO_X | (op & MOVE_TO_POS)));
7133
7134 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7135 if (skip == MOVE_POS_MATCH_OR_ZV)
7136 reached = 5;
7137 else if (skip == MOVE_X_REACHED)
7138 {
7139 /* If TO_X was reached, we want to know whether TO_Y is
7140 in the line. We know this is the case if the already
7141 scanned glyphs make the line tall enough. Otherwise,
7142 we must check by scanning the rest of the line. */
7143 line_height = it->max_ascent + it->max_descent;
7144 if (to_y >= it->current_y
7145 && to_y < it->current_y + line_height)
7146 {
7147 reached = 6;
7148 break;
7149 }
7150 it_backup = *it;
7151 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7152 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7153 op & MOVE_TO_POS);
7154 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7155 line_height = it->max_ascent + it->max_descent;
7156 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7157
7158 if (to_y >= it->current_y
7159 && to_y < it->current_y + line_height)
7160 {
7161 /* If TO_Y is in this line and TO_X was reached
7162 above, we scanned too far. We have to restore
7163 IT's settings to the ones before skipping. */
7164 *it = it_backup;
7165 reached = 6;
7166 }
7167 else
7168 {
7169 skip = skip2;
7170 if (skip == MOVE_POS_MATCH_OR_ZV)
7171 reached = 7;
7172 }
7173 }
7174 else
7175 {
7176 /* Check whether TO_Y is in this line. */
7177 line_height = it->max_ascent + it->max_descent;
7178 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7179
7180 if (to_y >= it->current_y
7181 && to_y < it->current_y + line_height)
7182 {
7183 /* When word-wrap is on, TO_X may lie past the end
7184 of a wrapped line. Then it->current is the
7185 character on the next line, so backtrack to the
7186 space before the wrap point. */
7187 if (skip == MOVE_LINE_CONTINUED
7188 && it->line_wrap == WORD_WRAP)
7189 {
7190 int prev_x = max (it->current_x - 1, 0);
7191 *it = it_backup;
7192 skip = move_it_in_display_line_to
7193 (it, -1, prev_x, MOVE_TO_X);
7194 }
7195 reached = 6;
7196 }
7197 }
7198
7199 if (reached)
7200 break;
7201 }
7202 else if (BUFFERP (it->object)
7203 && (it->method == GET_FROM_BUFFER
7204 || it->method == GET_FROM_STRETCH)
7205 && IT_CHARPOS (*it) >= to_charpos)
7206 skip = MOVE_POS_MATCH_OR_ZV;
7207 else
7208 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7209
7210 switch (skip)
7211 {
7212 case MOVE_POS_MATCH_OR_ZV:
7213 reached = 8;
7214 goto out;
7215
7216 case MOVE_NEWLINE_OR_CR:
7217 set_iterator_to_next (it, 1);
7218 it->continuation_lines_width = 0;
7219 break;
7220
7221 case MOVE_LINE_TRUNCATED:
7222 it->continuation_lines_width = 0;
7223 reseat_at_next_visible_line_start (it, 0);
7224 if ((op & MOVE_TO_POS) != 0
7225 && IT_CHARPOS (*it) > to_charpos)
7226 {
7227 reached = 9;
7228 goto out;
7229 }
7230 break;
7231
7232 case MOVE_LINE_CONTINUED:
7233 /* For continued lines ending in a tab, some of the glyphs
7234 associated with the tab are displayed on the current
7235 line. Since it->current_x does not include these glyphs,
7236 we use it->last_visible_x instead. */
7237 if (it->c == '\t')
7238 {
7239 it->continuation_lines_width += it->last_visible_x;
7240 /* When moving by vpos, ensure that the iterator really
7241 advances to the next line (bug#847, bug#969). Fixme:
7242 do we need to do this in other circumstances? */
7243 if (it->current_x != it->last_visible_x
7244 && (op & MOVE_TO_VPOS)
7245 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7246 {
7247 line_start_x = it->current_x + it->pixel_width
7248 - it->last_visible_x;
7249 set_iterator_to_next (it, 0);
7250 }
7251 }
7252 else
7253 it->continuation_lines_width += it->current_x;
7254 break;
7255
7256 default:
7257 abort ();
7258 }
7259
7260 /* Reset/increment for the next run. */
7261 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7262 it->current_x = line_start_x;
7263 line_start_x = 0;
7264 it->hpos = 0;
7265 it->current_y += it->max_ascent + it->max_descent;
7266 ++it->vpos;
7267 last_height = it->max_ascent + it->max_descent;
7268 last_max_ascent = it->max_ascent;
7269 it->max_ascent = it->max_descent = 0;
7270 }
7271
7272 out:
7273
7274 /* On text terminals, we may stop at the end of a line in the middle
7275 of a multi-character glyph. If the glyph itself is continued,
7276 i.e. it is actually displayed on the next line, don't treat this
7277 stopping point as valid; move to the next line instead (unless
7278 that brings us offscreen). */
7279 if (!FRAME_WINDOW_P (it->f)
7280 && op & MOVE_TO_POS
7281 && IT_CHARPOS (*it) == to_charpos
7282 && it->what == IT_CHARACTER
7283 && it->nglyphs > 1
7284 && it->line_wrap == WINDOW_WRAP
7285 && it->current_x == it->last_visible_x - 1
7286 && it->c != '\n'
7287 && it->c != '\t'
7288 && it->vpos < XFASTINT (it->w->window_end_vpos))
7289 {
7290 it->continuation_lines_width += it->current_x;
7291 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7292 it->current_y += it->max_ascent + it->max_descent;
7293 ++it->vpos;
7294 last_height = it->max_ascent + it->max_descent;
7295 last_max_ascent = it->max_ascent;
7296 }
7297
7298 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7299 }
7300
7301
7302 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7303
7304 If DY > 0, move IT backward at least that many pixels. DY = 0
7305 means move IT backward to the preceding line start or BEGV. This
7306 function may move over more than DY pixels if IT->current_y - DY
7307 ends up in the middle of a line; in this case IT->current_y will be
7308 set to the top of the line moved to. */
7309
7310 void
7311 move_it_vertically_backward (it, dy)
7312 struct it *it;
7313 int dy;
7314 {
7315 int nlines, h;
7316 struct it it2, it3;
7317 int start_pos;
7318
7319 move_further_back:
7320 xassert (dy >= 0);
7321
7322 start_pos = IT_CHARPOS (*it);
7323
7324 /* Estimate how many newlines we must move back. */
7325 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7326
7327 /* Set the iterator's position that many lines back. */
7328 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7329 back_to_previous_visible_line_start (it);
7330
7331 /* Reseat the iterator here. When moving backward, we don't want
7332 reseat to skip forward over invisible text, set up the iterator
7333 to deliver from overlay strings at the new position etc. So,
7334 use reseat_1 here. */
7335 reseat_1 (it, it->current.pos, 1);
7336
7337 /* We are now surely at a line start. */
7338 it->current_x = it->hpos = 0;
7339 it->continuation_lines_width = 0;
7340
7341 /* Move forward and see what y-distance we moved. First move to the
7342 start of the next line so that we get its height. We need this
7343 height to be able to tell whether we reached the specified
7344 y-distance. */
7345 it2 = *it;
7346 it2.max_ascent = it2.max_descent = 0;
7347 do
7348 {
7349 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7350 MOVE_TO_POS | MOVE_TO_VPOS);
7351 }
7352 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7353 xassert (IT_CHARPOS (*it) >= BEGV);
7354 it3 = it2;
7355
7356 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7357 xassert (IT_CHARPOS (*it) >= BEGV);
7358 /* H is the actual vertical distance from the position in *IT
7359 and the starting position. */
7360 h = it2.current_y - it->current_y;
7361 /* NLINES is the distance in number of lines. */
7362 nlines = it2.vpos - it->vpos;
7363
7364 /* Correct IT's y and vpos position
7365 so that they are relative to the starting point. */
7366 it->vpos -= nlines;
7367 it->current_y -= h;
7368
7369 if (dy == 0)
7370 {
7371 /* DY == 0 means move to the start of the screen line. The
7372 value of nlines is > 0 if continuation lines were involved. */
7373 if (nlines > 0)
7374 move_it_by_lines (it, nlines, 1);
7375 }
7376 else
7377 {
7378 /* The y-position we try to reach, relative to *IT.
7379 Note that H has been subtracted in front of the if-statement. */
7380 int target_y = it->current_y + h - dy;
7381 int y0 = it3.current_y;
7382 int y1 = line_bottom_y (&it3);
7383 int line_height = y1 - y0;
7384
7385 /* If we did not reach target_y, try to move further backward if
7386 we can. If we moved too far backward, try to move forward. */
7387 if (target_y < it->current_y
7388 /* This is heuristic. In a window that's 3 lines high, with
7389 a line height of 13 pixels each, recentering with point
7390 on the bottom line will try to move -39/2 = 19 pixels
7391 backward. Try to avoid moving into the first line. */
7392 && (it->current_y - target_y
7393 > min (window_box_height (it->w), line_height * 2 / 3))
7394 && IT_CHARPOS (*it) > BEGV)
7395 {
7396 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7397 target_y - it->current_y));
7398 dy = it->current_y - target_y;
7399 goto move_further_back;
7400 }
7401 else if (target_y >= it->current_y + line_height
7402 && IT_CHARPOS (*it) < ZV)
7403 {
7404 /* Should move forward by at least one line, maybe more.
7405
7406 Note: Calling move_it_by_lines can be expensive on
7407 terminal frames, where compute_motion is used (via
7408 vmotion) to do the job, when there are very long lines
7409 and truncate-lines is nil. That's the reason for
7410 treating terminal frames specially here. */
7411
7412 if (!FRAME_WINDOW_P (it->f))
7413 move_it_vertically (it, target_y - (it->current_y + line_height));
7414 else
7415 {
7416 do
7417 {
7418 move_it_by_lines (it, 1, 1);
7419 }
7420 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7421 }
7422 }
7423 }
7424 }
7425
7426
7427 /* Move IT by a specified amount of pixel lines DY. DY negative means
7428 move backwards. DY = 0 means move to start of screen line. At the
7429 end, IT will be on the start of a screen line. */
7430
7431 void
7432 move_it_vertically (it, dy)
7433 struct it *it;
7434 int dy;
7435 {
7436 if (dy <= 0)
7437 move_it_vertically_backward (it, -dy);
7438 else
7439 {
7440 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7441 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7442 MOVE_TO_POS | MOVE_TO_Y);
7443 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7444
7445 /* If buffer ends in ZV without a newline, move to the start of
7446 the line to satisfy the post-condition. */
7447 if (IT_CHARPOS (*it) == ZV
7448 && ZV > BEGV
7449 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7450 move_it_by_lines (it, 0, 0);
7451 }
7452 }
7453
7454
7455 /* Move iterator IT past the end of the text line it is in. */
7456
7457 void
7458 move_it_past_eol (it)
7459 struct it *it;
7460 {
7461 enum move_it_result rc;
7462
7463 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7464 if (rc == MOVE_NEWLINE_OR_CR)
7465 set_iterator_to_next (it, 0);
7466 }
7467
7468
7469 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7470 negative means move up. DVPOS == 0 means move to the start of the
7471 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7472 NEED_Y_P is zero, IT->current_y will be left unchanged.
7473
7474 Further optimization ideas: If we would know that IT->f doesn't use
7475 a face with proportional font, we could be faster for
7476 truncate-lines nil. */
7477
7478 void
7479 move_it_by_lines (it, dvpos, need_y_p)
7480 struct it *it;
7481 int dvpos, need_y_p;
7482 {
7483 struct position pos;
7484
7485 /* The commented-out optimization uses vmotion on terminals. This
7486 gives bad results, because elements like it->what, on which
7487 callers such as pos_visible_p rely, aren't updated. */
7488 /* if (!FRAME_WINDOW_P (it->f))
7489 {
7490 struct text_pos textpos;
7491
7492 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7493 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7494 reseat (it, textpos, 1);
7495 it->vpos += pos.vpos;
7496 it->current_y += pos.vpos;
7497 }
7498 else */
7499
7500 if (dvpos == 0)
7501 {
7502 /* DVPOS == 0 means move to the start of the screen line. */
7503 move_it_vertically_backward (it, 0);
7504 xassert (it->current_x == 0 && it->hpos == 0);
7505 /* Let next call to line_bottom_y calculate real line height */
7506 last_height = 0;
7507 }
7508 else if (dvpos > 0)
7509 {
7510 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7511 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7512 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7513 }
7514 else
7515 {
7516 struct it it2;
7517 int start_charpos, i;
7518
7519 /* Start at the beginning of the screen line containing IT's
7520 position. This may actually move vertically backwards,
7521 in case of overlays, so adjust dvpos accordingly. */
7522 dvpos += it->vpos;
7523 move_it_vertically_backward (it, 0);
7524 dvpos -= it->vpos;
7525
7526 /* Go back -DVPOS visible lines and reseat the iterator there. */
7527 start_charpos = IT_CHARPOS (*it);
7528 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7529 back_to_previous_visible_line_start (it);
7530 reseat (it, it->current.pos, 1);
7531
7532 /* Move further back if we end up in a string or an image. */
7533 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7534 {
7535 /* First try to move to start of display line. */
7536 dvpos += it->vpos;
7537 move_it_vertically_backward (it, 0);
7538 dvpos -= it->vpos;
7539 if (IT_POS_VALID_AFTER_MOVE_P (it))
7540 break;
7541 /* If start of line is still in string or image,
7542 move further back. */
7543 back_to_previous_visible_line_start (it);
7544 reseat (it, it->current.pos, 1);
7545 dvpos--;
7546 }
7547
7548 it->current_x = it->hpos = 0;
7549
7550 /* Above call may have moved too far if continuation lines
7551 are involved. Scan forward and see if it did. */
7552 it2 = *it;
7553 it2.vpos = it2.current_y = 0;
7554 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7555 it->vpos -= it2.vpos;
7556 it->current_y -= it2.current_y;
7557 it->current_x = it->hpos = 0;
7558
7559 /* If we moved too far back, move IT some lines forward. */
7560 if (it2.vpos > -dvpos)
7561 {
7562 int delta = it2.vpos + dvpos;
7563 it2 = *it;
7564 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7565 /* Move back again if we got too far ahead. */
7566 if (IT_CHARPOS (*it) >= start_charpos)
7567 *it = it2;
7568 }
7569 }
7570 }
7571
7572 /* Return 1 if IT points into the middle of a display vector. */
7573
7574 int
7575 in_display_vector_p (it)
7576 struct it *it;
7577 {
7578 return (it->method == GET_FROM_DISPLAY_VECTOR
7579 && it->current.dpvec_index > 0
7580 && it->dpvec + it->current.dpvec_index != it->dpend);
7581 }
7582
7583 \f
7584 /***********************************************************************
7585 Messages
7586 ***********************************************************************/
7587
7588
7589 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7590 to *Messages*. */
7591
7592 void
7593 add_to_log (format, arg1, arg2)
7594 char *format;
7595 Lisp_Object arg1, arg2;
7596 {
7597 Lisp_Object args[3];
7598 Lisp_Object msg, fmt;
7599 char *buffer;
7600 int len;
7601 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7602 USE_SAFE_ALLOCA;
7603
7604 /* Do nothing if called asynchronously. Inserting text into
7605 a buffer may call after-change-functions and alike and
7606 that would means running Lisp asynchronously. */
7607 if (handling_signal)
7608 return;
7609
7610 fmt = msg = Qnil;
7611 GCPRO4 (fmt, msg, arg1, arg2);
7612
7613 args[0] = fmt = build_string (format);
7614 args[1] = arg1;
7615 args[2] = arg2;
7616 msg = Fformat (3, args);
7617
7618 len = SBYTES (msg) + 1;
7619 SAFE_ALLOCA (buffer, char *, len);
7620 bcopy (SDATA (msg), buffer, len);
7621
7622 message_dolog (buffer, len - 1, 1, 0);
7623 SAFE_FREE ();
7624
7625 UNGCPRO;
7626 }
7627
7628
7629 /* Output a newline in the *Messages* buffer if "needs" one. */
7630
7631 void
7632 message_log_maybe_newline ()
7633 {
7634 if (message_log_need_newline)
7635 message_dolog ("", 0, 1, 0);
7636 }
7637
7638
7639 /* Add a string M of length NBYTES to the message log, optionally
7640 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7641 nonzero, means interpret the contents of M as multibyte. This
7642 function calls low-level routines in order to bypass text property
7643 hooks, etc. which might not be safe to run.
7644
7645 This may GC (insert may run before/after change hooks),
7646 so the buffer M must NOT point to a Lisp string. */
7647
7648 void
7649 message_dolog (m, nbytes, nlflag, multibyte)
7650 const char *m;
7651 int nbytes, nlflag, multibyte;
7652 {
7653 if (!NILP (Vmemory_full))
7654 return;
7655
7656 if (!NILP (Vmessage_log_max))
7657 {
7658 struct buffer *oldbuf;
7659 Lisp_Object oldpoint, oldbegv, oldzv;
7660 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7661 int point_at_end = 0;
7662 int zv_at_end = 0;
7663 Lisp_Object old_deactivate_mark, tem;
7664 struct gcpro gcpro1;
7665
7666 old_deactivate_mark = Vdeactivate_mark;
7667 oldbuf = current_buffer;
7668 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7669 current_buffer->undo_list = Qt;
7670
7671 oldpoint = message_dolog_marker1;
7672 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7673 oldbegv = message_dolog_marker2;
7674 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7675 oldzv = message_dolog_marker3;
7676 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7677 GCPRO1 (old_deactivate_mark);
7678
7679 if (PT == Z)
7680 point_at_end = 1;
7681 if (ZV == Z)
7682 zv_at_end = 1;
7683
7684 BEGV = BEG;
7685 BEGV_BYTE = BEG_BYTE;
7686 ZV = Z;
7687 ZV_BYTE = Z_BYTE;
7688 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7689
7690 /* Insert the string--maybe converting multibyte to single byte
7691 or vice versa, so that all the text fits the buffer. */
7692 if (multibyte
7693 && NILP (current_buffer->enable_multibyte_characters))
7694 {
7695 int i, c, char_bytes;
7696 unsigned char work[1];
7697
7698 /* Convert a multibyte string to single-byte
7699 for the *Message* buffer. */
7700 for (i = 0; i < nbytes; i += char_bytes)
7701 {
7702 c = string_char_and_length (m + i, &char_bytes);
7703 work[0] = (ASCII_CHAR_P (c)
7704 ? c
7705 : multibyte_char_to_unibyte (c, Qnil));
7706 insert_1_both (work, 1, 1, 1, 0, 0);
7707 }
7708 }
7709 else if (! multibyte
7710 && ! NILP (current_buffer->enable_multibyte_characters))
7711 {
7712 int i, c, char_bytes;
7713 unsigned char *msg = (unsigned char *) m;
7714 unsigned char str[MAX_MULTIBYTE_LENGTH];
7715 /* Convert a single-byte string to multibyte
7716 for the *Message* buffer. */
7717 for (i = 0; i < nbytes; i++)
7718 {
7719 c = msg[i];
7720 MAKE_CHAR_MULTIBYTE (c);
7721 char_bytes = CHAR_STRING (c, str);
7722 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7723 }
7724 }
7725 else if (nbytes)
7726 insert_1 (m, nbytes, 1, 0, 0);
7727
7728 if (nlflag)
7729 {
7730 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7731 insert_1 ("\n", 1, 1, 0, 0);
7732
7733 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7734 this_bol = PT;
7735 this_bol_byte = PT_BYTE;
7736
7737 /* See if this line duplicates the previous one.
7738 If so, combine duplicates. */
7739 if (this_bol > BEG)
7740 {
7741 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7742 prev_bol = PT;
7743 prev_bol_byte = PT_BYTE;
7744
7745 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7746 this_bol, this_bol_byte);
7747 if (dup)
7748 {
7749 del_range_both (prev_bol, prev_bol_byte,
7750 this_bol, this_bol_byte, 0);
7751 if (dup > 1)
7752 {
7753 char dupstr[40];
7754 int duplen;
7755
7756 /* If you change this format, don't forget to also
7757 change message_log_check_duplicate. */
7758 sprintf (dupstr, " [%d times]", dup);
7759 duplen = strlen (dupstr);
7760 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7761 insert_1 (dupstr, duplen, 1, 0, 1);
7762 }
7763 }
7764 }
7765
7766 /* If we have more than the desired maximum number of lines
7767 in the *Messages* buffer now, delete the oldest ones.
7768 This is safe because we don't have undo in this buffer. */
7769
7770 if (NATNUMP (Vmessage_log_max))
7771 {
7772 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7773 -XFASTINT (Vmessage_log_max) - 1, 0);
7774 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7775 }
7776 }
7777 BEGV = XMARKER (oldbegv)->charpos;
7778 BEGV_BYTE = marker_byte_position (oldbegv);
7779
7780 if (zv_at_end)
7781 {
7782 ZV = Z;
7783 ZV_BYTE = Z_BYTE;
7784 }
7785 else
7786 {
7787 ZV = XMARKER (oldzv)->charpos;
7788 ZV_BYTE = marker_byte_position (oldzv);
7789 }
7790
7791 if (point_at_end)
7792 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7793 else
7794 /* We can't do Fgoto_char (oldpoint) because it will run some
7795 Lisp code. */
7796 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7797 XMARKER (oldpoint)->bytepos);
7798
7799 UNGCPRO;
7800 unchain_marker (XMARKER (oldpoint));
7801 unchain_marker (XMARKER (oldbegv));
7802 unchain_marker (XMARKER (oldzv));
7803
7804 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7805 set_buffer_internal (oldbuf);
7806 if (NILP (tem))
7807 windows_or_buffers_changed = old_windows_or_buffers_changed;
7808 message_log_need_newline = !nlflag;
7809 Vdeactivate_mark = old_deactivate_mark;
7810 }
7811 }
7812
7813
7814 /* We are at the end of the buffer after just having inserted a newline.
7815 (Note: We depend on the fact we won't be crossing the gap.)
7816 Check to see if the most recent message looks a lot like the previous one.
7817 Return 0 if different, 1 if the new one should just replace it, or a
7818 value N > 1 if we should also append " [N times]". */
7819
7820 static int
7821 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7822 int prev_bol, this_bol;
7823 int prev_bol_byte, this_bol_byte;
7824 {
7825 int i;
7826 int len = Z_BYTE - 1 - this_bol_byte;
7827 int seen_dots = 0;
7828 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7829 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7830
7831 for (i = 0; i < len; i++)
7832 {
7833 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7834 seen_dots = 1;
7835 if (p1[i] != p2[i])
7836 return seen_dots;
7837 }
7838 p1 += len;
7839 if (*p1 == '\n')
7840 return 2;
7841 if (*p1++ == ' ' && *p1++ == '[')
7842 {
7843 int n = 0;
7844 while (*p1 >= '0' && *p1 <= '9')
7845 n = n * 10 + *p1++ - '0';
7846 if (strncmp (p1, " times]\n", 8) == 0)
7847 return n+1;
7848 }
7849 return 0;
7850 }
7851 \f
7852
7853 /* Display an echo area message M with a specified length of NBYTES
7854 bytes. The string may include null characters. If M is 0, clear
7855 out any existing message, and let the mini-buffer text show
7856 through.
7857
7858 This may GC, so the buffer M must NOT point to a Lisp string. */
7859
7860 void
7861 message2 (m, nbytes, multibyte)
7862 const char *m;
7863 int nbytes;
7864 int multibyte;
7865 {
7866 /* First flush out any partial line written with print. */
7867 message_log_maybe_newline ();
7868 if (m)
7869 message_dolog (m, nbytes, 1, multibyte);
7870 message2_nolog (m, nbytes, multibyte);
7871 }
7872
7873
7874 /* The non-logging counterpart of message2. */
7875
7876 void
7877 message2_nolog (m, nbytes, multibyte)
7878 const char *m;
7879 int nbytes, multibyte;
7880 {
7881 struct frame *sf = SELECTED_FRAME ();
7882 message_enable_multibyte = multibyte;
7883
7884 if (FRAME_INITIAL_P (sf))
7885 {
7886 if (noninteractive_need_newline)
7887 putc ('\n', stderr);
7888 noninteractive_need_newline = 0;
7889 if (m)
7890 fwrite (m, nbytes, 1, stderr);
7891 if (cursor_in_echo_area == 0)
7892 fprintf (stderr, "\n");
7893 fflush (stderr);
7894 }
7895 /* A null message buffer means that the frame hasn't really been
7896 initialized yet. Error messages get reported properly by
7897 cmd_error, so this must be just an informative message; toss it. */
7898 else if (INTERACTIVE
7899 && sf->glyphs_initialized_p
7900 && FRAME_MESSAGE_BUF (sf))
7901 {
7902 Lisp_Object mini_window;
7903 struct frame *f;
7904
7905 /* Get the frame containing the mini-buffer
7906 that the selected frame is using. */
7907 mini_window = FRAME_MINIBUF_WINDOW (sf);
7908 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7909
7910 FRAME_SAMPLE_VISIBILITY (f);
7911 if (FRAME_VISIBLE_P (sf)
7912 && ! FRAME_VISIBLE_P (f))
7913 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7914
7915 if (m)
7916 {
7917 set_message (m, Qnil, nbytes, multibyte);
7918 if (minibuffer_auto_raise)
7919 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7920 }
7921 else
7922 clear_message (1, 1);
7923
7924 do_pending_window_change (0);
7925 echo_area_display (1);
7926 do_pending_window_change (0);
7927 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7928 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7929 }
7930 }
7931
7932
7933 /* Display an echo area message M with a specified length of NBYTES
7934 bytes. The string may include null characters. If M is not a
7935 string, clear out any existing message, and let the mini-buffer
7936 text show through.
7937
7938 This function cancels echoing. */
7939
7940 void
7941 message3 (m, nbytes, multibyte)
7942 Lisp_Object m;
7943 int nbytes;
7944 int multibyte;
7945 {
7946 struct gcpro gcpro1;
7947
7948 GCPRO1 (m);
7949 clear_message (1,1);
7950 cancel_echoing ();
7951
7952 /* First flush out any partial line written with print. */
7953 message_log_maybe_newline ();
7954 if (STRINGP (m))
7955 {
7956 char *buffer;
7957 USE_SAFE_ALLOCA;
7958
7959 SAFE_ALLOCA (buffer, char *, nbytes);
7960 bcopy (SDATA (m), buffer, nbytes);
7961 message_dolog (buffer, nbytes, 1, multibyte);
7962 SAFE_FREE ();
7963 }
7964 message3_nolog (m, nbytes, multibyte);
7965
7966 UNGCPRO;
7967 }
7968
7969
7970 /* The non-logging version of message3.
7971 This does not cancel echoing, because it is used for echoing.
7972 Perhaps we need to make a separate function for echoing
7973 and make this cancel echoing. */
7974
7975 void
7976 message3_nolog (m, nbytes, multibyte)
7977 Lisp_Object m;
7978 int nbytes, multibyte;
7979 {
7980 struct frame *sf = SELECTED_FRAME ();
7981 message_enable_multibyte = multibyte;
7982
7983 if (FRAME_INITIAL_P (sf))
7984 {
7985 if (noninteractive_need_newline)
7986 putc ('\n', stderr);
7987 noninteractive_need_newline = 0;
7988 if (STRINGP (m))
7989 fwrite (SDATA (m), nbytes, 1, stderr);
7990 if (cursor_in_echo_area == 0)
7991 fprintf (stderr, "\n");
7992 fflush (stderr);
7993 }
7994 /* A null message buffer means that the frame hasn't really been
7995 initialized yet. Error messages get reported properly by
7996 cmd_error, so this must be just an informative message; toss it. */
7997 else if (INTERACTIVE
7998 && sf->glyphs_initialized_p
7999 && FRAME_MESSAGE_BUF (sf))
8000 {
8001 Lisp_Object mini_window;
8002 Lisp_Object frame;
8003 struct frame *f;
8004
8005 /* Get the frame containing the mini-buffer
8006 that the selected frame is using. */
8007 mini_window = FRAME_MINIBUF_WINDOW (sf);
8008 frame = XWINDOW (mini_window)->frame;
8009 f = XFRAME (frame);
8010
8011 FRAME_SAMPLE_VISIBILITY (f);
8012 if (FRAME_VISIBLE_P (sf)
8013 && !FRAME_VISIBLE_P (f))
8014 Fmake_frame_visible (frame);
8015
8016 if (STRINGP (m) && SCHARS (m) > 0)
8017 {
8018 set_message (NULL, m, nbytes, multibyte);
8019 if (minibuffer_auto_raise)
8020 Fraise_frame (frame);
8021 /* Assume we are not echoing.
8022 (If we are, echo_now will override this.) */
8023 echo_message_buffer = Qnil;
8024 }
8025 else
8026 clear_message (1, 1);
8027
8028 do_pending_window_change (0);
8029 echo_area_display (1);
8030 do_pending_window_change (0);
8031 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8032 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8033 }
8034 }
8035
8036
8037 /* Display a null-terminated echo area message M. If M is 0, clear
8038 out any existing message, and let the mini-buffer text show through.
8039
8040 The buffer M must continue to exist until after the echo area gets
8041 cleared or some other message gets displayed there. Do not pass
8042 text that is stored in a Lisp string. Do not pass text in a buffer
8043 that was alloca'd. */
8044
8045 void
8046 message1 (m)
8047 char *m;
8048 {
8049 message2 (m, (m ? strlen (m) : 0), 0);
8050 }
8051
8052
8053 /* The non-logging counterpart of message1. */
8054
8055 void
8056 message1_nolog (m)
8057 char *m;
8058 {
8059 message2_nolog (m, (m ? strlen (m) : 0), 0);
8060 }
8061
8062 /* Display a message M which contains a single %s
8063 which gets replaced with STRING. */
8064
8065 void
8066 message_with_string (m, string, log)
8067 char *m;
8068 Lisp_Object string;
8069 int log;
8070 {
8071 CHECK_STRING (string);
8072
8073 if (noninteractive)
8074 {
8075 if (m)
8076 {
8077 if (noninteractive_need_newline)
8078 putc ('\n', stderr);
8079 noninteractive_need_newline = 0;
8080 fprintf (stderr, m, SDATA (string));
8081 if (!cursor_in_echo_area)
8082 fprintf (stderr, "\n");
8083 fflush (stderr);
8084 }
8085 }
8086 else if (INTERACTIVE)
8087 {
8088 /* The frame whose minibuffer we're going to display the message on.
8089 It may be larger than the selected frame, so we need
8090 to use its buffer, not the selected frame's buffer. */
8091 Lisp_Object mini_window;
8092 struct frame *f, *sf = SELECTED_FRAME ();
8093
8094 /* Get the frame containing the minibuffer
8095 that the selected frame is using. */
8096 mini_window = FRAME_MINIBUF_WINDOW (sf);
8097 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8098
8099 /* A null message buffer means that the frame hasn't really been
8100 initialized yet. Error messages get reported properly by
8101 cmd_error, so this must be just an informative message; toss it. */
8102 if (FRAME_MESSAGE_BUF (f))
8103 {
8104 Lisp_Object args[2], message;
8105 struct gcpro gcpro1, gcpro2;
8106
8107 args[0] = build_string (m);
8108 args[1] = message = string;
8109 GCPRO2 (args[0], message);
8110 gcpro1.nvars = 2;
8111
8112 message = Fformat (2, args);
8113
8114 if (log)
8115 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8116 else
8117 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8118
8119 UNGCPRO;
8120
8121 /* Print should start at the beginning of the message
8122 buffer next time. */
8123 message_buf_print = 0;
8124 }
8125 }
8126 }
8127
8128
8129 /* Dump an informative message to the minibuf. If M is 0, clear out
8130 any existing message, and let the mini-buffer text show through. */
8131
8132 /* VARARGS 1 */
8133 void
8134 message (m, a1, a2, a3)
8135 char *m;
8136 EMACS_INT a1, a2, a3;
8137 {
8138 if (noninteractive)
8139 {
8140 if (m)
8141 {
8142 if (noninteractive_need_newline)
8143 putc ('\n', stderr);
8144 noninteractive_need_newline = 0;
8145 fprintf (stderr, m, a1, a2, a3);
8146 if (cursor_in_echo_area == 0)
8147 fprintf (stderr, "\n");
8148 fflush (stderr);
8149 }
8150 }
8151 else if (INTERACTIVE)
8152 {
8153 /* The frame whose mini-buffer we're going to display the message
8154 on. It may be larger than the selected frame, so we need to
8155 use its buffer, not the selected frame's buffer. */
8156 Lisp_Object mini_window;
8157 struct frame *f, *sf = SELECTED_FRAME ();
8158
8159 /* Get the frame containing the mini-buffer
8160 that the selected frame is using. */
8161 mini_window = FRAME_MINIBUF_WINDOW (sf);
8162 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8163
8164 /* A null message buffer means that the frame hasn't really been
8165 initialized yet. Error messages get reported properly by
8166 cmd_error, so this must be just an informative message; toss
8167 it. */
8168 if (FRAME_MESSAGE_BUF (f))
8169 {
8170 if (m)
8171 {
8172 int len;
8173 #ifdef NO_ARG_ARRAY
8174 char *a[3];
8175 a[0] = (char *) a1;
8176 a[1] = (char *) a2;
8177 a[2] = (char *) a3;
8178
8179 len = doprnt (FRAME_MESSAGE_BUF (f),
8180 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8181 #else
8182 len = doprnt (FRAME_MESSAGE_BUF (f),
8183 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8184 (char **) &a1);
8185 #endif /* NO_ARG_ARRAY */
8186
8187 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8188 }
8189 else
8190 message1 (0);
8191
8192 /* Print should start at the beginning of the message
8193 buffer next time. */
8194 message_buf_print = 0;
8195 }
8196 }
8197 }
8198
8199
8200 /* The non-logging version of message. */
8201
8202 void
8203 message_nolog (m, a1, a2, a3)
8204 char *m;
8205 EMACS_INT a1, a2, a3;
8206 {
8207 Lisp_Object old_log_max;
8208 old_log_max = Vmessage_log_max;
8209 Vmessage_log_max = Qnil;
8210 message (m, a1, a2, a3);
8211 Vmessage_log_max = old_log_max;
8212 }
8213
8214
8215 /* Display the current message in the current mini-buffer. This is
8216 only called from error handlers in process.c, and is not time
8217 critical. */
8218
8219 void
8220 update_echo_area ()
8221 {
8222 if (!NILP (echo_area_buffer[0]))
8223 {
8224 Lisp_Object string;
8225 string = Fcurrent_message ();
8226 message3 (string, SBYTES (string),
8227 !NILP (current_buffer->enable_multibyte_characters));
8228 }
8229 }
8230
8231
8232 /* Make sure echo area buffers in `echo_buffers' are live.
8233 If they aren't, make new ones. */
8234
8235 static void
8236 ensure_echo_area_buffers ()
8237 {
8238 int i;
8239
8240 for (i = 0; i < 2; ++i)
8241 if (!BUFFERP (echo_buffer[i])
8242 || NILP (XBUFFER (echo_buffer[i])->name))
8243 {
8244 char name[30];
8245 Lisp_Object old_buffer;
8246 int j;
8247
8248 old_buffer = echo_buffer[i];
8249 sprintf (name, " *Echo Area %d*", i);
8250 echo_buffer[i] = Fget_buffer_create (build_string (name));
8251 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8252 /* to force word wrap in echo area -
8253 it was decided to postpone this*/
8254 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8255
8256 for (j = 0; j < 2; ++j)
8257 if (EQ (old_buffer, echo_area_buffer[j]))
8258 echo_area_buffer[j] = echo_buffer[i];
8259 }
8260 }
8261
8262
8263 /* Call FN with args A1..A4 with either the current or last displayed
8264 echo_area_buffer as current buffer.
8265
8266 WHICH zero means use the current message buffer
8267 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8268 from echo_buffer[] and clear it.
8269
8270 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8271 suitable buffer from echo_buffer[] and clear it.
8272
8273 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8274 that the current message becomes the last displayed one, make
8275 choose a suitable buffer for echo_area_buffer[0], and clear it.
8276
8277 Value is what FN returns. */
8278
8279 static int
8280 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8281 struct window *w;
8282 int which;
8283 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8284 EMACS_INT a1;
8285 Lisp_Object a2;
8286 EMACS_INT a3, a4;
8287 {
8288 Lisp_Object buffer;
8289 int this_one, the_other, clear_buffer_p, rc;
8290 int count = SPECPDL_INDEX ();
8291
8292 /* If buffers aren't live, make new ones. */
8293 ensure_echo_area_buffers ();
8294
8295 clear_buffer_p = 0;
8296
8297 if (which == 0)
8298 this_one = 0, the_other = 1;
8299 else if (which > 0)
8300 this_one = 1, the_other = 0;
8301 else
8302 {
8303 this_one = 0, the_other = 1;
8304 clear_buffer_p = 1;
8305
8306 /* We need a fresh one in case the current echo buffer equals
8307 the one containing the last displayed echo area message. */
8308 if (!NILP (echo_area_buffer[this_one])
8309 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8310 echo_area_buffer[this_one] = Qnil;
8311 }
8312
8313 /* Choose a suitable buffer from echo_buffer[] is we don't
8314 have one. */
8315 if (NILP (echo_area_buffer[this_one]))
8316 {
8317 echo_area_buffer[this_one]
8318 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8319 ? echo_buffer[the_other]
8320 : echo_buffer[this_one]);
8321 clear_buffer_p = 1;
8322 }
8323
8324 buffer = echo_area_buffer[this_one];
8325
8326 /* Don't get confused by reusing the buffer used for echoing
8327 for a different purpose. */
8328 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8329 cancel_echoing ();
8330
8331 record_unwind_protect (unwind_with_echo_area_buffer,
8332 with_echo_area_buffer_unwind_data (w));
8333
8334 /* Make the echo area buffer current. Note that for display
8335 purposes, it is not necessary that the displayed window's buffer
8336 == current_buffer, except for text property lookup. So, let's
8337 only set that buffer temporarily here without doing a full
8338 Fset_window_buffer. We must also change w->pointm, though,
8339 because otherwise an assertions in unshow_buffer fails, and Emacs
8340 aborts. */
8341 set_buffer_internal_1 (XBUFFER (buffer));
8342 if (w)
8343 {
8344 w->buffer = buffer;
8345 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8346 }
8347
8348 current_buffer->undo_list = Qt;
8349 current_buffer->read_only = Qnil;
8350 specbind (Qinhibit_read_only, Qt);
8351 specbind (Qinhibit_modification_hooks, Qt);
8352
8353 if (clear_buffer_p && Z > BEG)
8354 del_range (BEG, Z);
8355
8356 xassert (BEGV >= BEG);
8357 xassert (ZV <= Z && ZV >= BEGV);
8358
8359 rc = fn (a1, a2, a3, a4);
8360
8361 xassert (BEGV >= BEG);
8362 xassert (ZV <= Z && ZV >= BEGV);
8363
8364 unbind_to (count, Qnil);
8365 return rc;
8366 }
8367
8368
8369 /* Save state that should be preserved around the call to the function
8370 FN called in with_echo_area_buffer. */
8371
8372 static Lisp_Object
8373 with_echo_area_buffer_unwind_data (w)
8374 struct window *w;
8375 {
8376 int i = 0;
8377 Lisp_Object vector, tmp;
8378
8379 /* Reduce consing by keeping one vector in
8380 Vwith_echo_area_save_vector. */
8381 vector = Vwith_echo_area_save_vector;
8382 Vwith_echo_area_save_vector = Qnil;
8383
8384 if (NILP (vector))
8385 vector = Fmake_vector (make_number (7), Qnil);
8386
8387 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8388 ASET (vector, i, Vdeactivate_mark); ++i;
8389 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8390
8391 if (w)
8392 {
8393 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8394 ASET (vector, i, w->buffer); ++i;
8395 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8396 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8397 }
8398 else
8399 {
8400 int end = i + 4;
8401 for (; i < end; ++i)
8402 ASET (vector, i, Qnil);
8403 }
8404
8405 xassert (i == ASIZE (vector));
8406 return vector;
8407 }
8408
8409
8410 /* Restore global state from VECTOR which was created by
8411 with_echo_area_buffer_unwind_data. */
8412
8413 static Lisp_Object
8414 unwind_with_echo_area_buffer (vector)
8415 Lisp_Object vector;
8416 {
8417 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8418 Vdeactivate_mark = AREF (vector, 1);
8419 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8420
8421 if (WINDOWP (AREF (vector, 3)))
8422 {
8423 struct window *w;
8424 Lisp_Object buffer, charpos, bytepos;
8425
8426 w = XWINDOW (AREF (vector, 3));
8427 buffer = AREF (vector, 4);
8428 charpos = AREF (vector, 5);
8429 bytepos = AREF (vector, 6);
8430
8431 w->buffer = buffer;
8432 set_marker_both (w->pointm, buffer,
8433 XFASTINT (charpos), XFASTINT (bytepos));
8434 }
8435
8436 Vwith_echo_area_save_vector = vector;
8437 return Qnil;
8438 }
8439
8440
8441 /* Set up the echo area for use by print functions. MULTIBYTE_P
8442 non-zero means we will print multibyte. */
8443
8444 void
8445 setup_echo_area_for_printing (multibyte_p)
8446 int multibyte_p;
8447 {
8448 /* If we can't find an echo area any more, exit. */
8449 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8450 Fkill_emacs (Qnil);
8451
8452 ensure_echo_area_buffers ();
8453
8454 if (!message_buf_print)
8455 {
8456 /* A message has been output since the last time we printed.
8457 Choose a fresh echo area buffer. */
8458 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8459 echo_area_buffer[0] = echo_buffer[1];
8460 else
8461 echo_area_buffer[0] = echo_buffer[0];
8462
8463 /* Switch to that buffer and clear it. */
8464 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8465 current_buffer->truncate_lines = Qnil;
8466
8467 if (Z > BEG)
8468 {
8469 int count = SPECPDL_INDEX ();
8470 specbind (Qinhibit_read_only, Qt);
8471 /* Note that undo recording is always disabled. */
8472 del_range (BEG, Z);
8473 unbind_to (count, Qnil);
8474 }
8475 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8476
8477 /* Set up the buffer for the multibyteness we need. */
8478 if (multibyte_p
8479 != !NILP (current_buffer->enable_multibyte_characters))
8480 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8481
8482 /* Raise the frame containing the echo area. */
8483 if (minibuffer_auto_raise)
8484 {
8485 struct frame *sf = SELECTED_FRAME ();
8486 Lisp_Object mini_window;
8487 mini_window = FRAME_MINIBUF_WINDOW (sf);
8488 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8489 }
8490
8491 message_log_maybe_newline ();
8492 message_buf_print = 1;
8493 }
8494 else
8495 {
8496 if (NILP (echo_area_buffer[0]))
8497 {
8498 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8499 echo_area_buffer[0] = echo_buffer[1];
8500 else
8501 echo_area_buffer[0] = echo_buffer[0];
8502 }
8503
8504 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8505 {
8506 /* Someone switched buffers between print requests. */
8507 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8508 current_buffer->truncate_lines = Qnil;
8509 }
8510 }
8511 }
8512
8513
8514 /* Display an echo area message in window W. Value is non-zero if W's
8515 height is changed. If display_last_displayed_message_p is
8516 non-zero, display the message that was last displayed, otherwise
8517 display the current message. */
8518
8519 static int
8520 display_echo_area (w)
8521 struct window *w;
8522 {
8523 int i, no_message_p, window_height_changed_p, count;
8524
8525 /* Temporarily disable garbage collections while displaying the echo
8526 area. This is done because a GC can print a message itself.
8527 That message would modify the echo area buffer's contents while a
8528 redisplay of the buffer is going on, and seriously confuse
8529 redisplay. */
8530 count = inhibit_garbage_collection ();
8531
8532 /* If there is no message, we must call display_echo_area_1
8533 nevertheless because it resizes the window. But we will have to
8534 reset the echo_area_buffer in question to nil at the end because
8535 with_echo_area_buffer will sets it to an empty buffer. */
8536 i = display_last_displayed_message_p ? 1 : 0;
8537 no_message_p = NILP (echo_area_buffer[i]);
8538
8539 window_height_changed_p
8540 = with_echo_area_buffer (w, display_last_displayed_message_p,
8541 display_echo_area_1,
8542 (EMACS_INT) w, Qnil, 0, 0);
8543
8544 if (no_message_p)
8545 echo_area_buffer[i] = Qnil;
8546
8547 unbind_to (count, Qnil);
8548 return window_height_changed_p;
8549 }
8550
8551
8552 /* Helper for display_echo_area. Display the current buffer which
8553 contains the current echo area message in window W, a mini-window,
8554 a pointer to which is passed in A1. A2..A4 are currently not used.
8555 Change the height of W so that all of the message is displayed.
8556 Value is non-zero if height of W was changed. */
8557
8558 static int
8559 display_echo_area_1 (a1, a2, a3, a4)
8560 EMACS_INT a1;
8561 Lisp_Object a2;
8562 EMACS_INT a3, a4;
8563 {
8564 struct window *w = (struct window *) a1;
8565 Lisp_Object window;
8566 struct text_pos start;
8567 int window_height_changed_p = 0;
8568
8569 /* Do this before displaying, so that we have a large enough glyph
8570 matrix for the display. If we can't get enough space for the
8571 whole text, display the last N lines. That works by setting w->start. */
8572 window_height_changed_p = resize_mini_window (w, 0);
8573
8574 /* Use the starting position chosen by resize_mini_window. */
8575 SET_TEXT_POS_FROM_MARKER (start, w->start);
8576
8577 /* Display. */
8578 clear_glyph_matrix (w->desired_matrix);
8579 XSETWINDOW (window, w);
8580 try_window (window, start, 0);
8581
8582 return window_height_changed_p;
8583 }
8584
8585
8586 /* Resize the echo area window to exactly the size needed for the
8587 currently displayed message, if there is one. If a mini-buffer
8588 is active, don't shrink it. */
8589
8590 void
8591 resize_echo_area_exactly ()
8592 {
8593 if (BUFFERP (echo_area_buffer[0])
8594 && WINDOWP (echo_area_window))
8595 {
8596 struct window *w = XWINDOW (echo_area_window);
8597 int resized_p;
8598 Lisp_Object resize_exactly;
8599
8600 if (minibuf_level == 0)
8601 resize_exactly = Qt;
8602 else
8603 resize_exactly = Qnil;
8604
8605 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8606 (EMACS_INT) w, resize_exactly, 0, 0);
8607 if (resized_p)
8608 {
8609 ++windows_or_buffers_changed;
8610 ++update_mode_lines;
8611 redisplay_internal (0);
8612 }
8613 }
8614 }
8615
8616
8617 /* Callback function for with_echo_area_buffer, when used from
8618 resize_echo_area_exactly. A1 contains a pointer to the window to
8619 resize, EXACTLY non-nil means resize the mini-window exactly to the
8620 size of the text displayed. A3 and A4 are not used. Value is what
8621 resize_mini_window returns. */
8622
8623 static int
8624 resize_mini_window_1 (a1, exactly, a3, a4)
8625 EMACS_INT a1;
8626 Lisp_Object exactly;
8627 EMACS_INT a3, a4;
8628 {
8629 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8630 }
8631
8632
8633 /* Resize mini-window W to fit the size of its contents. EXACT_P
8634 means size the window exactly to the size needed. Otherwise, it's
8635 only enlarged until W's buffer is empty.
8636
8637 Set W->start to the right place to begin display. If the whole
8638 contents fit, start at the beginning. Otherwise, start so as
8639 to make the end of the contents appear. This is particularly
8640 important for y-or-n-p, but seems desirable generally.
8641
8642 Value is non-zero if the window height has been changed. */
8643
8644 int
8645 resize_mini_window (w, exact_p)
8646 struct window *w;
8647 int exact_p;
8648 {
8649 struct frame *f = XFRAME (w->frame);
8650 int window_height_changed_p = 0;
8651
8652 xassert (MINI_WINDOW_P (w));
8653
8654 /* By default, start display at the beginning. */
8655 set_marker_both (w->start, w->buffer,
8656 BUF_BEGV (XBUFFER (w->buffer)),
8657 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8658
8659 /* Don't resize windows while redisplaying a window; it would
8660 confuse redisplay functions when the size of the window they are
8661 displaying changes from under them. Such a resizing can happen,
8662 for instance, when which-func prints a long message while
8663 we are running fontification-functions. We're running these
8664 functions with safe_call which binds inhibit-redisplay to t. */
8665 if (!NILP (Vinhibit_redisplay))
8666 return 0;
8667
8668 /* Nil means don't try to resize. */
8669 if (NILP (Vresize_mini_windows)
8670 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8671 return 0;
8672
8673 if (!FRAME_MINIBUF_ONLY_P (f))
8674 {
8675 struct it it;
8676 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8677 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8678 int height, max_height;
8679 int unit = FRAME_LINE_HEIGHT (f);
8680 struct text_pos start;
8681 struct buffer *old_current_buffer = NULL;
8682
8683 if (current_buffer != XBUFFER (w->buffer))
8684 {
8685 old_current_buffer = current_buffer;
8686 set_buffer_internal (XBUFFER (w->buffer));
8687 }
8688
8689 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8690
8691 /* Compute the max. number of lines specified by the user. */
8692 if (FLOATP (Vmax_mini_window_height))
8693 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8694 else if (INTEGERP (Vmax_mini_window_height))
8695 max_height = XINT (Vmax_mini_window_height);
8696 else
8697 max_height = total_height / 4;
8698
8699 /* Correct that max. height if it's bogus. */
8700 max_height = max (1, max_height);
8701 max_height = min (total_height, max_height);
8702
8703 /* Find out the height of the text in the window. */
8704 if (it.line_wrap == TRUNCATE)
8705 height = 1;
8706 else
8707 {
8708 last_height = 0;
8709 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8710 if (it.max_ascent == 0 && it.max_descent == 0)
8711 height = it.current_y + last_height;
8712 else
8713 height = it.current_y + it.max_ascent + it.max_descent;
8714 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8715 height = (height + unit - 1) / unit;
8716 }
8717
8718 /* Compute a suitable window start. */
8719 if (height > max_height)
8720 {
8721 height = max_height;
8722 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8723 move_it_vertically_backward (&it, (height - 1) * unit);
8724 start = it.current.pos;
8725 }
8726 else
8727 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8728 SET_MARKER_FROM_TEXT_POS (w->start, start);
8729
8730 if (EQ (Vresize_mini_windows, Qgrow_only))
8731 {
8732 /* Let it grow only, until we display an empty message, in which
8733 case the window shrinks again. */
8734 if (height > WINDOW_TOTAL_LINES (w))
8735 {
8736 int old_height = WINDOW_TOTAL_LINES (w);
8737 freeze_window_starts (f, 1);
8738 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8739 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8740 }
8741 else if (height < WINDOW_TOTAL_LINES (w)
8742 && (exact_p || BEGV == ZV))
8743 {
8744 int old_height = WINDOW_TOTAL_LINES (w);
8745 freeze_window_starts (f, 0);
8746 shrink_mini_window (w);
8747 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8748 }
8749 }
8750 else
8751 {
8752 /* Always resize to exact size needed. */
8753 if (height > WINDOW_TOTAL_LINES (w))
8754 {
8755 int old_height = WINDOW_TOTAL_LINES (w);
8756 freeze_window_starts (f, 1);
8757 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8758 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8759 }
8760 else if (height < WINDOW_TOTAL_LINES (w))
8761 {
8762 int old_height = WINDOW_TOTAL_LINES (w);
8763 freeze_window_starts (f, 0);
8764 shrink_mini_window (w);
8765
8766 if (height)
8767 {
8768 freeze_window_starts (f, 1);
8769 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8770 }
8771
8772 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8773 }
8774 }
8775
8776 if (old_current_buffer)
8777 set_buffer_internal (old_current_buffer);
8778 }
8779
8780 return window_height_changed_p;
8781 }
8782
8783
8784 /* Value is the current message, a string, or nil if there is no
8785 current message. */
8786
8787 Lisp_Object
8788 current_message ()
8789 {
8790 Lisp_Object msg;
8791
8792 if (!BUFFERP (echo_area_buffer[0]))
8793 msg = Qnil;
8794 else
8795 {
8796 with_echo_area_buffer (0, 0, current_message_1,
8797 (EMACS_INT) &msg, Qnil, 0, 0);
8798 if (NILP (msg))
8799 echo_area_buffer[0] = Qnil;
8800 }
8801
8802 return msg;
8803 }
8804
8805
8806 static int
8807 current_message_1 (a1, a2, a3, a4)
8808 EMACS_INT a1;
8809 Lisp_Object a2;
8810 EMACS_INT a3, a4;
8811 {
8812 Lisp_Object *msg = (Lisp_Object *) a1;
8813
8814 if (Z > BEG)
8815 *msg = make_buffer_string (BEG, Z, 1);
8816 else
8817 *msg = Qnil;
8818 return 0;
8819 }
8820
8821
8822 /* Push the current message on Vmessage_stack for later restauration
8823 by restore_message. Value is non-zero if the current message isn't
8824 empty. This is a relatively infrequent operation, so it's not
8825 worth optimizing. */
8826
8827 int
8828 push_message ()
8829 {
8830 Lisp_Object msg;
8831 msg = current_message ();
8832 Vmessage_stack = Fcons (msg, Vmessage_stack);
8833 return STRINGP (msg);
8834 }
8835
8836
8837 /* Restore message display from the top of Vmessage_stack. */
8838
8839 void
8840 restore_message ()
8841 {
8842 Lisp_Object msg;
8843
8844 xassert (CONSP (Vmessage_stack));
8845 msg = XCAR (Vmessage_stack);
8846 if (STRINGP (msg))
8847 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8848 else
8849 message3_nolog (msg, 0, 0);
8850 }
8851
8852
8853 /* Handler for record_unwind_protect calling pop_message. */
8854
8855 Lisp_Object
8856 pop_message_unwind (dummy)
8857 Lisp_Object dummy;
8858 {
8859 pop_message ();
8860 return Qnil;
8861 }
8862
8863 /* Pop the top-most entry off Vmessage_stack. */
8864
8865 void
8866 pop_message ()
8867 {
8868 xassert (CONSP (Vmessage_stack));
8869 Vmessage_stack = XCDR (Vmessage_stack);
8870 }
8871
8872
8873 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8874 exits. If the stack is not empty, we have a missing pop_message
8875 somewhere. */
8876
8877 void
8878 check_message_stack ()
8879 {
8880 if (!NILP (Vmessage_stack))
8881 abort ();
8882 }
8883
8884
8885 /* Truncate to NCHARS what will be displayed in the echo area the next
8886 time we display it---but don't redisplay it now. */
8887
8888 void
8889 truncate_echo_area (nchars)
8890 int nchars;
8891 {
8892 if (nchars == 0)
8893 echo_area_buffer[0] = Qnil;
8894 /* A null message buffer means that the frame hasn't really been
8895 initialized yet. Error messages get reported properly by
8896 cmd_error, so this must be just an informative message; toss it. */
8897 else if (!noninteractive
8898 && INTERACTIVE
8899 && !NILP (echo_area_buffer[0]))
8900 {
8901 struct frame *sf = SELECTED_FRAME ();
8902 if (FRAME_MESSAGE_BUF (sf))
8903 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8904 }
8905 }
8906
8907
8908 /* Helper function for truncate_echo_area. Truncate the current
8909 message to at most NCHARS characters. */
8910
8911 static int
8912 truncate_message_1 (nchars, a2, a3, a4)
8913 EMACS_INT nchars;
8914 Lisp_Object a2;
8915 EMACS_INT a3, a4;
8916 {
8917 if (BEG + nchars < Z)
8918 del_range (BEG + nchars, Z);
8919 if (Z == BEG)
8920 echo_area_buffer[0] = Qnil;
8921 return 0;
8922 }
8923
8924
8925 /* Set the current message to a substring of S or STRING.
8926
8927 If STRING is a Lisp string, set the message to the first NBYTES
8928 bytes from STRING. NBYTES zero means use the whole string. If
8929 STRING is multibyte, the message will be displayed multibyte.
8930
8931 If S is not null, set the message to the first LEN bytes of S. LEN
8932 zero means use the whole string. MULTIBYTE_P non-zero means S is
8933 multibyte. Display the message multibyte in that case.
8934
8935 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8936 to t before calling set_message_1 (which calls insert).
8937 */
8938
8939 void
8940 set_message (s, string, nbytes, multibyte_p)
8941 const char *s;
8942 Lisp_Object string;
8943 int nbytes, multibyte_p;
8944 {
8945 message_enable_multibyte
8946 = ((s && multibyte_p)
8947 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8948
8949 with_echo_area_buffer (0, -1, set_message_1,
8950 (EMACS_INT) s, string, nbytes, multibyte_p);
8951 message_buf_print = 0;
8952 help_echo_showing_p = 0;
8953 }
8954
8955
8956 /* Helper function for set_message. Arguments have the same meaning
8957 as there, with A1 corresponding to S and A2 corresponding to STRING
8958 This function is called with the echo area buffer being
8959 current. */
8960
8961 static int
8962 set_message_1 (a1, a2, nbytes, multibyte_p)
8963 EMACS_INT a1;
8964 Lisp_Object a2;
8965 EMACS_INT nbytes, multibyte_p;
8966 {
8967 const char *s = (const char *) a1;
8968 Lisp_Object string = a2;
8969
8970 /* Change multibyteness of the echo buffer appropriately. */
8971 if (message_enable_multibyte
8972 != !NILP (current_buffer->enable_multibyte_characters))
8973 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8974
8975 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8976
8977 /* Insert new message at BEG. */
8978 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8979
8980 if (STRINGP (string))
8981 {
8982 int nchars;
8983
8984 if (nbytes == 0)
8985 nbytes = SBYTES (string);
8986 nchars = string_byte_to_char (string, nbytes);
8987
8988 /* This function takes care of single/multibyte conversion. We
8989 just have to ensure that the echo area buffer has the right
8990 setting of enable_multibyte_characters. */
8991 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8992 }
8993 else if (s)
8994 {
8995 if (nbytes == 0)
8996 nbytes = strlen (s);
8997
8998 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8999 {
9000 /* Convert from multi-byte to single-byte. */
9001 int i, c, n;
9002 unsigned char work[1];
9003
9004 /* Convert a multibyte string to single-byte. */
9005 for (i = 0; i < nbytes; i += n)
9006 {
9007 c = string_char_and_length (s + i, &n);
9008 work[0] = (ASCII_CHAR_P (c)
9009 ? c
9010 : multibyte_char_to_unibyte (c, Qnil));
9011 insert_1_both (work, 1, 1, 1, 0, 0);
9012 }
9013 }
9014 else if (!multibyte_p
9015 && !NILP (current_buffer->enable_multibyte_characters))
9016 {
9017 /* Convert from single-byte to multi-byte. */
9018 int i, c, n;
9019 const unsigned char *msg = (const unsigned char *) s;
9020 unsigned char str[MAX_MULTIBYTE_LENGTH];
9021
9022 /* Convert a single-byte string to multibyte. */
9023 for (i = 0; i < nbytes; i++)
9024 {
9025 c = msg[i];
9026 MAKE_CHAR_MULTIBYTE (c);
9027 n = CHAR_STRING (c, str);
9028 insert_1_both (str, 1, n, 1, 0, 0);
9029 }
9030 }
9031 else
9032 insert_1 (s, nbytes, 1, 0, 0);
9033 }
9034
9035 return 0;
9036 }
9037
9038
9039 /* Clear messages. CURRENT_P non-zero means clear the current
9040 message. LAST_DISPLAYED_P non-zero means clear the message
9041 last displayed. */
9042
9043 void
9044 clear_message (current_p, last_displayed_p)
9045 int current_p, last_displayed_p;
9046 {
9047 if (current_p)
9048 {
9049 echo_area_buffer[0] = Qnil;
9050 message_cleared_p = 1;
9051 }
9052
9053 if (last_displayed_p)
9054 echo_area_buffer[1] = Qnil;
9055
9056 message_buf_print = 0;
9057 }
9058
9059 /* Clear garbaged frames.
9060
9061 This function is used where the old redisplay called
9062 redraw_garbaged_frames which in turn called redraw_frame which in
9063 turn called clear_frame. The call to clear_frame was a source of
9064 flickering. I believe a clear_frame is not necessary. It should
9065 suffice in the new redisplay to invalidate all current matrices,
9066 and ensure a complete redisplay of all windows. */
9067
9068 static void
9069 clear_garbaged_frames ()
9070 {
9071 if (frame_garbaged)
9072 {
9073 Lisp_Object tail, frame;
9074 int changed_count = 0;
9075
9076 FOR_EACH_FRAME (tail, frame)
9077 {
9078 struct frame *f = XFRAME (frame);
9079
9080 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9081 {
9082 if (f->resized_p)
9083 {
9084 Fredraw_frame (frame);
9085 f->force_flush_display_p = 1;
9086 }
9087 clear_current_matrices (f);
9088 changed_count++;
9089 f->garbaged = 0;
9090 f->resized_p = 0;
9091 }
9092 }
9093
9094 frame_garbaged = 0;
9095 if (changed_count)
9096 ++windows_or_buffers_changed;
9097 }
9098 }
9099
9100
9101 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9102 is non-zero update selected_frame. Value is non-zero if the
9103 mini-windows height has been changed. */
9104
9105 static int
9106 echo_area_display (update_frame_p)
9107 int update_frame_p;
9108 {
9109 Lisp_Object mini_window;
9110 struct window *w;
9111 struct frame *f;
9112 int window_height_changed_p = 0;
9113 struct frame *sf = SELECTED_FRAME ();
9114
9115 mini_window = FRAME_MINIBUF_WINDOW (sf);
9116 w = XWINDOW (mini_window);
9117 f = XFRAME (WINDOW_FRAME (w));
9118
9119 /* Don't display if frame is invisible or not yet initialized. */
9120 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9121 return 0;
9122
9123 #ifdef HAVE_WINDOW_SYSTEM
9124 /* When Emacs starts, selected_frame may be the initial terminal
9125 frame. If we let this through, a message would be displayed on
9126 the terminal. */
9127 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9128 return 0;
9129 #endif /* HAVE_WINDOW_SYSTEM */
9130
9131 /* Redraw garbaged frames. */
9132 if (frame_garbaged)
9133 clear_garbaged_frames ();
9134
9135 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9136 {
9137 echo_area_window = mini_window;
9138 window_height_changed_p = display_echo_area (w);
9139 w->must_be_updated_p = 1;
9140
9141 /* Update the display, unless called from redisplay_internal.
9142 Also don't update the screen during redisplay itself. The
9143 update will happen at the end of redisplay, and an update
9144 here could cause confusion. */
9145 if (update_frame_p && !redisplaying_p)
9146 {
9147 int n = 0;
9148
9149 /* If the display update has been interrupted by pending
9150 input, update mode lines in the frame. Due to the
9151 pending input, it might have been that redisplay hasn't
9152 been called, so that mode lines above the echo area are
9153 garbaged. This looks odd, so we prevent it here. */
9154 if (!display_completed)
9155 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9156
9157 if (window_height_changed_p
9158 /* Don't do this if Emacs is shutting down. Redisplay
9159 needs to run hooks. */
9160 && !NILP (Vrun_hooks))
9161 {
9162 /* Must update other windows. Likewise as in other
9163 cases, don't let this update be interrupted by
9164 pending input. */
9165 int count = SPECPDL_INDEX ();
9166 specbind (Qredisplay_dont_pause, Qt);
9167 windows_or_buffers_changed = 1;
9168 redisplay_internal (0);
9169 unbind_to (count, Qnil);
9170 }
9171 else if (FRAME_WINDOW_P (f) && n == 0)
9172 {
9173 /* Window configuration is the same as before.
9174 Can do with a display update of the echo area,
9175 unless we displayed some mode lines. */
9176 update_single_window (w, 1);
9177 FRAME_RIF (f)->flush_display (f);
9178 }
9179 else
9180 update_frame (f, 1, 1);
9181
9182 /* If cursor is in the echo area, make sure that the next
9183 redisplay displays the minibuffer, so that the cursor will
9184 be replaced with what the minibuffer wants. */
9185 if (cursor_in_echo_area)
9186 ++windows_or_buffers_changed;
9187 }
9188 }
9189 else if (!EQ (mini_window, selected_window))
9190 windows_or_buffers_changed++;
9191
9192 /* Last displayed message is now the current message. */
9193 echo_area_buffer[1] = echo_area_buffer[0];
9194 /* Inform read_char that we're not echoing. */
9195 echo_message_buffer = Qnil;
9196
9197 /* Prevent redisplay optimization in redisplay_internal by resetting
9198 this_line_start_pos. This is done because the mini-buffer now
9199 displays the message instead of its buffer text. */
9200 if (EQ (mini_window, selected_window))
9201 CHARPOS (this_line_start_pos) = 0;
9202
9203 return window_height_changed_p;
9204 }
9205
9206
9207 \f
9208 /***********************************************************************
9209 Mode Lines and Frame Titles
9210 ***********************************************************************/
9211
9212 /* A buffer for constructing non-propertized mode-line strings and
9213 frame titles in it; allocated from the heap in init_xdisp and
9214 resized as needed in store_mode_line_noprop_char. */
9215
9216 static char *mode_line_noprop_buf;
9217
9218 /* The buffer's end, and a current output position in it. */
9219
9220 static char *mode_line_noprop_buf_end;
9221 static char *mode_line_noprop_ptr;
9222
9223 #define MODE_LINE_NOPROP_LEN(start) \
9224 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9225
9226 static enum {
9227 MODE_LINE_DISPLAY = 0,
9228 MODE_LINE_TITLE,
9229 MODE_LINE_NOPROP,
9230 MODE_LINE_STRING
9231 } mode_line_target;
9232
9233 /* Alist that caches the results of :propertize.
9234 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9235 static Lisp_Object mode_line_proptrans_alist;
9236
9237 /* List of strings making up the mode-line. */
9238 static Lisp_Object mode_line_string_list;
9239
9240 /* Base face property when building propertized mode line string. */
9241 static Lisp_Object mode_line_string_face;
9242 static Lisp_Object mode_line_string_face_prop;
9243
9244
9245 /* Unwind data for mode line strings */
9246
9247 static Lisp_Object Vmode_line_unwind_vector;
9248
9249 static Lisp_Object
9250 format_mode_line_unwind_data (struct buffer *obuf,
9251 Lisp_Object owin,
9252 int save_proptrans)
9253 {
9254 Lisp_Object vector, tmp;
9255
9256 /* Reduce consing by keeping one vector in
9257 Vwith_echo_area_save_vector. */
9258 vector = Vmode_line_unwind_vector;
9259 Vmode_line_unwind_vector = Qnil;
9260
9261 if (NILP (vector))
9262 vector = Fmake_vector (make_number (8), Qnil);
9263
9264 ASET (vector, 0, make_number (mode_line_target));
9265 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9266 ASET (vector, 2, mode_line_string_list);
9267 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9268 ASET (vector, 4, mode_line_string_face);
9269 ASET (vector, 5, mode_line_string_face_prop);
9270
9271 if (obuf)
9272 XSETBUFFER (tmp, obuf);
9273 else
9274 tmp = Qnil;
9275 ASET (vector, 6, tmp);
9276 ASET (vector, 7, owin);
9277
9278 return vector;
9279 }
9280
9281 static Lisp_Object
9282 unwind_format_mode_line (vector)
9283 Lisp_Object vector;
9284 {
9285 mode_line_target = XINT (AREF (vector, 0));
9286 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9287 mode_line_string_list = AREF (vector, 2);
9288 if (! EQ (AREF (vector, 3), Qt))
9289 mode_line_proptrans_alist = AREF (vector, 3);
9290 mode_line_string_face = AREF (vector, 4);
9291 mode_line_string_face_prop = AREF (vector, 5);
9292
9293 if (!NILP (AREF (vector, 7)))
9294 /* Select window before buffer, since it may change the buffer. */
9295 Fselect_window (AREF (vector, 7), Qt);
9296
9297 if (!NILP (AREF (vector, 6)))
9298 {
9299 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9300 ASET (vector, 6, Qnil);
9301 }
9302
9303 Vmode_line_unwind_vector = vector;
9304 return Qnil;
9305 }
9306
9307
9308 /* Store a single character C for the frame title in mode_line_noprop_buf.
9309 Re-allocate mode_line_noprop_buf if necessary. */
9310
9311 static void
9312 #ifdef PROTOTYPES
9313 store_mode_line_noprop_char (char c)
9314 #else
9315 store_mode_line_noprop_char (c)
9316 char c;
9317 #endif
9318 {
9319 /* If output position has reached the end of the allocated buffer,
9320 double the buffer's size. */
9321 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9322 {
9323 int len = MODE_LINE_NOPROP_LEN (0);
9324 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9325 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9326 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9327 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9328 }
9329
9330 *mode_line_noprop_ptr++ = c;
9331 }
9332
9333
9334 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9335 mode_line_noprop_ptr. STR is the string to store. Do not copy
9336 characters that yield more columns than PRECISION; PRECISION <= 0
9337 means copy the whole string. Pad with spaces until FIELD_WIDTH
9338 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9339 pad. Called from display_mode_element when it is used to build a
9340 frame title. */
9341
9342 static int
9343 store_mode_line_noprop (str, field_width, precision)
9344 const unsigned char *str;
9345 int field_width, precision;
9346 {
9347 int n = 0;
9348 int dummy, nbytes;
9349
9350 /* Copy at most PRECISION chars from STR. */
9351 nbytes = strlen (str);
9352 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9353 while (nbytes--)
9354 store_mode_line_noprop_char (*str++);
9355
9356 /* Fill up with spaces until FIELD_WIDTH reached. */
9357 while (field_width > 0
9358 && n < field_width)
9359 {
9360 store_mode_line_noprop_char (' ');
9361 ++n;
9362 }
9363
9364 return n;
9365 }
9366
9367 /***********************************************************************
9368 Frame Titles
9369 ***********************************************************************/
9370
9371 #ifdef HAVE_WINDOW_SYSTEM
9372
9373 /* Set the title of FRAME, if it has changed. The title format is
9374 Vicon_title_format if FRAME is iconified, otherwise it is
9375 frame_title_format. */
9376
9377 static void
9378 x_consider_frame_title (frame)
9379 Lisp_Object frame;
9380 {
9381 struct frame *f = XFRAME (frame);
9382
9383 if (FRAME_WINDOW_P (f)
9384 || FRAME_MINIBUF_ONLY_P (f)
9385 || f->explicit_name)
9386 {
9387 /* Do we have more than one visible frame on this X display? */
9388 Lisp_Object tail;
9389 Lisp_Object fmt;
9390 int title_start;
9391 char *title;
9392 int len;
9393 struct it it;
9394 int count = SPECPDL_INDEX ();
9395
9396 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9397 {
9398 Lisp_Object other_frame = XCAR (tail);
9399 struct frame *tf = XFRAME (other_frame);
9400
9401 if (tf != f
9402 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9403 && !FRAME_MINIBUF_ONLY_P (tf)
9404 && !EQ (other_frame, tip_frame)
9405 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9406 break;
9407 }
9408
9409 /* Set global variable indicating that multiple frames exist. */
9410 multiple_frames = CONSP (tail);
9411
9412 /* Switch to the buffer of selected window of the frame. Set up
9413 mode_line_target so that display_mode_element will output into
9414 mode_line_noprop_buf; then display the title. */
9415 record_unwind_protect (unwind_format_mode_line,
9416 format_mode_line_unwind_data
9417 (current_buffer, selected_window, 0));
9418
9419 Fselect_window (f->selected_window, Qt);
9420 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9421 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9422
9423 mode_line_target = MODE_LINE_TITLE;
9424 title_start = MODE_LINE_NOPROP_LEN (0);
9425 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9426 NULL, DEFAULT_FACE_ID);
9427 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9428 len = MODE_LINE_NOPROP_LEN (title_start);
9429 title = mode_line_noprop_buf + title_start;
9430 unbind_to (count, Qnil);
9431
9432 /* Set the title only if it's changed. This avoids consing in
9433 the common case where it hasn't. (If it turns out that we've
9434 already wasted too much time by walking through the list with
9435 display_mode_element, then we might need to optimize at a
9436 higher level than this.) */
9437 if (! STRINGP (f->name)
9438 || SBYTES (f->name) != len
9439 || bcmp (title, SDATA (f->name), len) != 0)
9440 x_implicitly_set_name (f, make_string (title, len), Qnil);
9441 }
9442 }
9443
9444 #endif /* not HAVE_WINDOW_SYSTEM */
9445
9446
9447
9448 \f
9449 /***********************************************************************
9450 Menu Bars
9451 ***********************************************************************/
9452
9453
9454 /* Prepare for redisplay by updating menu-bar item lists when
9455 appropriate. This can call eval. */
9456
9457 void
9458 prepare_menu_bars ()
9459 {
9460 int all_windows;
9461 struct gcpro gcpro1, gcpro2;
9462 struct frame *f;
9463 Lisp_Object tooltip_frame;
9464
9465 #ifdef HAVE_WINDOW_SYSTEM
9466 tooltip_frame = tip_frame;
9467 #else
9468 tooltip_frame = Qnil;
9469 #endif
9470
9471 /* Update all frame titles based on their buffer names, etc. We do
9472 this before the menu bars so that the buffer-menu will show the
9473 up-to-date frame titles. */
9474 #ifdef HAVE_WINDOW_SYSTEM
9475 if (windows_or_buffers_changed || update_mode_lines)
9476 {
9477 Lisp_Object tail, frame;
9478
9479 FOR_EACH_FRAME (tail, frame)
9480 {
9481 f = XFRAME (frame);
9482 if (!EQ (frame, tooltip_frame)
9483 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9484 x_consider_frame_title (frame);
9485 }
9486 }
9487 #endif /* HAVE_WINDOW_SYSTEM */
9488
9489 /* Update the menu bar item lists, if appropriate. This has to be
9490 done before any actual redisplay or generation of display lines. */
9491 all_windows = (update_mode_lines
9492 || buffer_shared > 1
9493 || windows_or_buffers_changed);
9494 if (all_windows)
9495 {
9496 Lisp_Object tail, frame;
9497 int count = SPECPDL_INDEX ();
9498 /* 1 means that update_menu_bar has run its hooks
9499 so any further calls to update_menu_bar shouldn't do so again. */
9500 int menu_bar_hooks_run = 0;
9501
9502 record_unwind_save_match_data ();
9503
9504 FOR_EACH_FRAME (tail, frame)
9505 {
9506 f = XFRAME (frame);
9507
9508 /* Ignore tooltip frame. */
9509 if (EQ (frame, tooltip_frame))
9510 continue;
9511
9512 /* If a window on this frame changed size, report that to
9513 the user and clear the size-change flag. */
9514 if (FRAME_WINDOW_SIZES_CHANGED (f))
9515 {
9516 Lisp_Object functions;
9517
9518 /* Clear flag first in case we get an error below. */
9519 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9520 functions = Vwindow_size_change_functions;
9521 GCPRO2 (tail, functions);
9522
9523 while (CONSP (functions))
9524 {
9525 if (!EQ (XCAR (functions), Qt))
9526 call1 (XCAR (functions), frame);
9527 functions = XCDR (functions);
9528 }
9529 UNGCPRO;
9530 }
9531
9532 GCPRO1 (tail);
9533 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9534 #ifdef HAVE_WINDOW_SYSTEM
9535 update_tool_bar (f, 0);
9536 #endif
9537 #ifdef HAVE_NS
9538 if (windows_or_buffers_changed
9539 && FRAME_NS_P (f))
9540 ns_set_doc_edited (f, Fbuffer_modified_p
9541 (XWINDOW (f->selected_window)->buffer));
9542 #endif
9543 UNGCPRO;
9544 }
9545
9546 unbind_to (count, Qnil);
9547 }
9548 else
9549 {
9550 struct frame *sf = SELECTED_FRAME ();
9551 update_menu_bar (sf, 1, 0);
9552 #ifdef HAVE_WINDOW_SYSTEM
9553 update_tool_bar (sf, 1);
9554 #endif
9555 }
9556
9557 /* Motif needs this. See comment in xmenu.c. Turn it off when
9558 pending_menu_activation is not defined. */
9559 #ifdef USE_X_TOOLKIT
9560 pending_menu_activation = 0;
9561 #endif
9562 }
9563
9564
9565 /* Update the menu bar item list for frame F. This has to be done
9566 before we start to fill in any display lines, because it can call
9567 eval.
9568
9569 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9570
9571 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9572 already ran the menu bar hooks for this redisplay, so there
9573 is no need to run them again. The return value is the
9574 updated value of this flag, to pass to the next call. */
9575
9576 static int
9577 update_menu_bar (f, save_match_data, hooks_run)
9578 struct frame *f;
9579 int save_match_data;
9580 int hooks_run;
9581 {
9582 Lisp_Object window;
9583 register struct window *w;
9584
9585 /* If called recursively during a menu update, do nothing. This can
9586 happen when, for instance, an activate-menubar-hook causes a
9587 redisplay. */
9588 if (inhibit_menubar_update)
9589 return hooks_run;
9590
9591 window = FRAME_SELECTED_WINDOW (f);
9592 w = XWINDOW (window);
9593
9594 if (FRAME_WINDOW_P (f)
9595 ?
9596 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9597 || defined (HAVE_NS) || defined (USE_GTK)
9598 FRAME_EXTERNAL_MENU_BAR (f)
9599 #else
9600 FRAME_MENU_BAR_LINES (f) > 0
9601 #endif
9602 : FRAME_MENU_BAR_LINES (f) > 0)
9603 {
9604 /* If the user has switched buffers or windows, we need to
9605 recompute to reflect the new bindings. But we'll
9606 recompute when update_mode_lines is set too; that means
9607 that people can use force-mode-line-update to request
9608 that the menu bar be recomputed. The adverse effect on
9609 the rest of the redisplay algorithm is about the same as
9610 windows_or_buffers_changed anyway. */
9611 if (windows_or_buffers_changed
9612 /* This used to test w->update_mode_line, but we believe
9613 there is no need to recompute the menu in that case. */
9614 || update_mode_lines
9615 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9616 < BUF_MODIFF (XBUFFER (w->buffer)))
9617 != !NILP (w->last_had_star))
9618 || ((!NILP (Vtransient_mark_mode)
9619 && !NILP (XBUFFER (w->buffer)->mark_active))
9620 != !NILP (w->region_showing)))
9621 {
9622 struct buffer *prev = current_buffer;
9623 int count = SPECPDL_INDEX ();
9624
9625 specbind (Qinhibit_menubar_update, Qt);
9626
9627 set_buffer_internal_1 (XBUFFER (w->buffer));
9628 if (save_match_data)
9629 record_unwind_save_match_data ();
9630 if (NILP (Voverriding_local_map_menu_flag))
9631 {
9632 specbind (Qoverriding_terminal_local_map, Qnil);
9633 specbind (Qoverriding_local_map, Qnil);
9634 }
9635
9636 if (!hooks_run)
9637 {
9638 /* Run the Lucid hook. */
9639 safe_run_hooks (Qactivate_menubar_hook);
9640
9641 /* If it has changed current-menubar from previous value,
9642 really recompute the menu-bar from the value. */
9643 if (! NILP (Vlucid_menu_bar_dirty_flag))
9644 call0 (Qrecompute_lucid_menubar);
9645
9646 safe_run_hooks (Qmenu_bar_update_hook);
9647
9648 hooks_run = 1;
9649 }
9650
9651 XSETFRAME (Vmenu_updating_frame, f);
9652 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9653
9654 /* Redisplay the menu bar in case we changed it. */
9655 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9656 || defined (HAVE_NS) || defined (USE_GTK)
9657 if (FRAME_WINDOW_P (f))
9658 {
9659 #if defined (HAVE_NS)
9660 /* All frames on Mac OS share the same menubar. So only
9661 the selected frame should be allowed to set it. */
9662 if (f == SELECTED_FRAME ())
9663 #endif
9664 set_frame_menubar (f, 0, 0);
9665 }
9666 else
9667 /* On a terminal screen, the menu bar is an ordinary screen
9668 line, and this makes it get updated. */
9669 w->update_mode_line = Qt;
9670 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9671 /* In the non-toolkit version, the menu bar is an ordinary screen
9672 line, and this makes it get updated. */
9673 w->update_mode_line = Qt;
9674 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9675
9676 unbind_to (count, Qnil);
9677 set_buffer_internal_1 (prev);
9678 }
9679 }
9680
9681 return hooks_run;
9682 }
9683
9684
9685 \f
9686 /***********************************************************************
9687 Output Cursor
9688 ***********************************************************************/
9689
9690 #ifdef HAVE_WINDOW_SYSTEM
9691
9692 /* EXPORT:
9693 Nominal cursor position -- where to draw output.
9694 HPOS and VPOS are window relative glyph matrix coordinates.
9695 X and Y are window relative pixel coordinates. */
9696
9697 struct cursor_pos output_cursor;
9698
9699
9700 /* EXPORT:
9701 Set the global variable output_cursor to CURSOR. All cursor
9702 positions are relative to updated_window. */
9703
9704 void
9705 set_output_cursor (cursor)
9706 struct cursor_pos *cursor;
9707 {
9708 output_cursor.hpos = cursor->hpos;
9709 output_cursor.vpos = cursor->vpos;
9710 output_cursor.x = cursor->x;
9711 output_cursor.y = cursor->y;
9712 }
9713
9714
9715 /* EXPORT for RIF:
9716 Set a nominal cursor position.
9717
9718 HPOS and VPOS are column/row positions in a window glyph matrix. X
9719 and Y are window text area relative pixel positions.
9720
9721 If this is done during an update, updated_window will contain the
9722 window that is being updated and the position is the future output
9723 cursor position for that window. If updated_window is null, use
9724 selected_window and display the cursor at the given position. */
9725
9726 void
9727 x_cursor_to (vpos, hpos, y, x)
9728 int vpos, hpos, y, x;
9729 {
9730 struct window *w;
9731
9732 /* If updated_window is not set, work on selected_window. */
9733 if (updated_window)
9734 w = updated_window;
9735 else
9736 w = XWINDOW (selected_window);
9737
9738 /* Set the output cursor. */
9739 output_cursor.hpos = hpos;
9740 output_cursor.vpos = vpos;
9741 output_cursor.x = x;
9742 output_cursor.y = y;
9743
9744 /* If not called as part of an update, really display the cursor.
9745 This will also set the cursor position of W. */
9746 if (updated_window == NULL)
9747 {
9748 BLOCK_INPUT;
9749 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9750 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9751 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9752 UNBLOCK_INPUT;
9753 }
9754 }
9755
9756 #endif /* HAVE_WINDOW_SYSTEM */
9757
9758 \f
9759 /***********************************************************************
9760 Tool-bars
9761 ***********************************************************************/
9762
9763 #ifdef HAVE_WINDOW_SYSTEM
9764
9765 /* Where the mouse was last time we reported a mouse event. */
9766
9767 FRAME_PTR last_mouse_frame;
9768
9769 /* Tool-bar item index of the item on which a mouse button was pressed
9770 or -1. */
9771
9772 int last_tool_bar_item;
9773
9774
9775 static Lisp_Object
9776 update_tool_bar_unwind (frame)
9777 Lisp_Object frame;
9778 {
9779 selected_frame = frame;
9780 return Qnil;
9781 }
9782
9783 /* Update the tool-bar item list for frame F. This has to be done
9784 before we start to fill in any display lines. Called from
9785 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9786 and restore it here. */
9787
9788 static void
9789 update_tool_bar (f, save_match_data)
9790 struct frame *f;
9791 int save_match_data;
9792 {
9793 #if defined (USE_GTK) || defined (HAVE_NS)
9794 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9795 #else
9796 int do_update = WINDOWP (f->tool_bar_window)
9797 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9798 #endif
9799
9800 if (do_update)
9801 {
9802 Lisp_Object window;
9803 struct window *w;
9804
9805 window = FRAME_SELECTED_WINDOW (f);
9806 w = XWINDOW (window);
9807
9808 /* If the user has switched buffers or windows, we need to
9809 recompute to reflect the new bindings. But we'll
9810 recompute when update_mode_lines is set too; that means
9811 that people can use force-mode-line-update to request
9812 that the menu bar be recomputed. The adverse effect on
9813 the rest of the redisplay algorithm is about the same as
9814 windows_or_buffers_changed anyway. */
9815 if (windows_or_buffers_changed
9816 || !NILP (w->update_mode_line)
9817 || update_mode_lines
9818 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9819 < BUF_MODIFF (XBUFFER (w->buffer)))
9820 != !NILP (w->last_had_star))
9821 || ((!NILP (Vtransient_mark_mode)
9822 && !NILP (XBUFFER (w->buffer)->mark_active))
9823 != !NILP (w->region_showing)))
9824 {
9825 struct buffer *prev = current_buffer;
9826 int count = SPECPDL_INDEX ();
9827 Lisp_Object frame, new_tool_bar;
9828 int new_n_tool_bar;
9829 struct gcpro gcpro1;
9830
9831 /* Set current_buffer to the buffer of the selected
9832 window of the frame, so that we get the right local
9833 keymaps. */
9834 set_buffer_internal_1 (XBUFFER (w->buffer));
9835
9836 /* Save match data, if we must. */
9837 if (save_match_data)
9838 record_unwind_save_match_data ();
9839
9840 /* Make sure that we don't accidentally use bogus keymaps. */
9841 if (NILP (Voverriding_local_map_menu_flag))
9842 {
9843 specbind (Qoverriding_terminal_local_map, Qnil);
9844 specbind (Qoverriding_local_map, Qnil);
9845 }
9846
9847 GCPRO1 (new_tool_bar);
9848
9849 /* We must temporarily set the selected frame to this frame
9850 before calling tool_bar_items, because the calculation of
9851 the tool-bar keymap uses the selected frame (see
9852 `tool-bar-make-keymap' in tool-bar.el). */
9853 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9854 XSETFRAME (frame, f);
9855 selected_frame = frame;
9856
9857 /* Build desired tool-bar items from keymaps. */
9858 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9859 &new_n_tool_bar);
9860
9861 /* Redisplay the tool-bar if we changed it. */
9862 if (new_n_tool_bar != f->n_tool_bar_items
9863 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9864 {
9865 /* Redisplay that happens asynchronously due to an expose event
9866 may access f->tool_bar_items. Make sure we update both
9867 variables within BLOCK_INPUT so no such event interrupts. */
9868 BLOCK_INPUT;
9869 f->tool_bar_items = new_tool_bar;
9870 f->n_tool_bar_items = new_n_tool_bar;
9871 w->update_mode_line = Qt;
9872 UNBLOCK_INPUT;
9873 }
9874
9875 UNGCPRO;
9876
9877 unbind_to (count, Qnil);
9878 set_buffer_internal_1 (prev);
9879 }
9880 }
9881 }
9882
9883
9884 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9885 F's desired tool-bar contents. F->tool_bar_items must have
9886 been set up previously by calling prepare_menu_bars. */
9887
9888 static void
9889 build_desired_tool_bar_string (f)
9890 struct frame *f;
9891 {
9892 int i, size, size_needed;
9893 struct gcpro gcpro1, gcpro2, gcpro3;
9894 Lisp_Object image, plist, props;
9895
9896 image = plist = props = Qnil;
9897 GCPRO3 (image, plist, props);
9898
9899 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9900 Otherwise, make a new string. */
9901
9902 /* The size of the string we might be able to reuse. */
9903 size = (STRINGP (f->desired_tool_bar_string)
9904 ? SCHARS (f->desired_tool_bar_string)
9905 : 0);
9906
9907 /* We need one space in the string for each image. */
9908 size_needed = f->n_tool_bar_items;
9909
9910 /* Reuse f->desired_tool_bar_string, if possible. */
9911 if (size < size_needed || NILP (f->desired_tool_bar_string))
9912 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9913 make_number (' '));
9914 else
9915 {
9916 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9917 Fremove_text_properties (make_number (0), make_number (size),
9918 props, f->desired_tool_bar_string);
9919 }
9920
9921 /* Put a `display' property on the string for the images to display,
9922 put a `menu_item' property on tool-bar items with a value that
9923 is the index of the item in F's tool-bar item vector. */
9924 for (i = 0; i < f->n_tool_bar_items; ++i)
9925 {
9926 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9927
9928 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9929 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9930 int hmargin, vmargin, relief, idx, end;
9931 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9932
9933 /* If image is a vector, choose the image according to the
9934 button state. */
9935 image = PROP (TOOL_BAR_ITEM_IMAGES);
9936 if (VECTORP (image))
9937 {
9938 if (enabled_p)
9939 idx = (selected_p
9940 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9941 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9942 else
9943 idx = (selected_p
9944 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9945 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9946
9947 xassert (ASIZE (image) >= idx);
9948 image = AREF (image, idx);
9949 }
9950 else
9951 idx = -1;
9952
9953 /* Ignore invalid image specifications. */
9954 if (!valid_image_p (image))
9955 continue;
9956
9957 /* Display the tool-bar button pressed, or depressed. */
9958 plist = Fcopy_sequence (XCDR (image));
9959
9960 /* Compute margin and relief to draw. */
9961 relief = (tool_bar_button_relief >= 0
9962 ? tool_bar_button_relief
9963 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9964 hmargin = vmargin = relief;
9965
9966 if (INTEGERP (Vtool_bar_button_margin)
9967 && XINT (Vtool_bar_button_margin) > 0)
9968 {
9969 hmargin += XFASTINT (Vtool_bar_button_margin);
9970 vmargin += XFASTINT (Vtool_bar_button_margin);
9971 }
9972 else if (CONSP (Vtool_bar_button_margin))
9973 {
9974 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9975 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9976 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9977
9978 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9979 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9980 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9981 }
9982
9983 if (auto_raise_tool_bar_buttons_p)
9984 {
9985 /* Add a `:relief' property to the image spec if the item is
9986 selected. */
9987 if (selected_p)
9988 {
9989 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9990 hmargin -= relief;
9991 vmargin -= relief;
9992 }
9993 }
9994 else
9995 {
9996 /* If image is selected, display it pressed, i.e. with a
9997 negative relief. If it's not selected, display it with a
9998 raised relief. */
9999 plist = Fplist_put (plist, QCrelief,
10000 (selected_p
10001 ? make_number (-relief)
10002 : make_number (relief)));
10003 hmargin -= relief;
10004 vmargin -= relief;
10005 }
10006
10007 /* Put a margin around the image. */
10008 if (hmargin || vmargin)
10009 {
10010 if (hmargin == vmargin)
10011 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10012 else
10013 plist = Fplist_put (plist, QCmargin,
10014 Fcons (make_number (hmargin),
10015 make_number (vmargin)));
10016 }
10017
10018 /* If button is not enabled, and we don't have special images
10019 for the disabled state, make the image appear disabled by
10020 applying an appropriate algorithm to it. */
10021 if (!enabled_p && idx < 0)
10022 plist = Fplist_put (plist, QCconversion, Qdisabled);
10023
10024 /* Put a `display' text property on the string for the image to
10025 display. Put a `menu-item' property on the string that gives
10026 the start of this item's properties in the tool-bar items
10027 vector. */
10028 image = Fcons (Qimage, plist);
10029 props = list4 (Qdisplay, image,
10030 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10031
10032 /* Let the last image hide all remaining spaces in the tool bar
10033 string. The string can be longer than needed when we reuse a
10034 previous string. */
10035 if (i + 1 == f->n_tool_bar_items)
10036 end = SCHARS (f->desired_tool_bar_string);
10037 else
10038 end = i + 1;
10039 Fadd_text_properties (make_number (i), make_number (end),
10040 props, f->desired_tool_bar_string);
10041 #undef PROP
10042 }
10043
10044 UNGCPRO;
10045 }
10046
10047
10048 /* Display one line of the tool-bar of frame IT->f.
10049
10050 HEIGHT specifies the desired height of the tool-bar line.
10051 If the actual height of the glyph row is less than HEIGHT, the
10052 row's height is increased to HEIGHT, and the icons are centered
10053 vertically in the new height.
10054
10055 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10056 count a final empty row in case the tool-bar width exactly matches
10057 the window width.
10058 */
10059
10060 static void
10061 display_tool_bar_line (it, height)
10062 struct it *it;
10063 int height;
10064 {
10065 struct glyph_row *row = it->glyph_row;
10066 int max_x = it->last_visible_x;
10067 struct glyph *last;
10068
10069 prepare_desired_row (row);
10070 row->y = it->current_y;
10071
10072 /* Note that this isn't made use of if the face hasn't a box,
10073 so there's no need to check the face here. */
10074 it->start_of_box_run_p = 1;
10075
10076 while (it->current_x < max_x)
10077 {
10078 int x, n_glyphs_before, i, nglyphs;
10079 struct it it_before;
10080
10081 /* Get the next display element. */
10082 if (!get_next_display_element (it))
10083 {
10084 /* Don't count empty row if we are counting needed tool-bar lines. */
10085 if (height < 0 && !it->hpos)
10086 return;
10087 break;
10088 }
10089
10090 /* Produce glyphs. */
10091 n_glyphs_before = row->used[TEXT_AREA];
10092 it_before = *it;
10093
10094 PRODUCE_GLYPHS (it);
10095
10096 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10097 i = 0;
10098 x = it_before.current_x;
10099 while (i < nglyphs)
10100 {
10101 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10102
10103 if (x + glyph->pixel_width > max_x)
10104 {
10105 /* Glyph doesn't fit on line. Backtrack. */
10106 row->used[TEXT_AREA] = n_glyphs_before;
10107 *it = it_before;
10108 /* If this is the only glyph on this line, it will never fit on the
10109 toolbar, so skip it. But ensure there is at least one glyph,
10110 so we don't accidentally disable the tool-bar. */
10111 if (n_glyphs_before == 0
10112 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10113 break;
10114 goto out;
10115 }
10116
10117 ++it->hpos;
10118 x += glyph->pixel_width;
10119 ++i;
10120 }
10121
10122 /* Stop at line ends. */
10123 if (ITERATOR_AT_END_OF_LINE_P (it))
10124 break;
10125
10126 set_iterator_to_next (it, 1);
10127 }
10128
10129 out:;
10130
10131 row->displays_text_p = row->used[TEXT_AREA] != 0;
10132
10133 /* Use default face for the border below the tool bar.
10134
10135 FIXME: When auto-resize-tool-bars is grow-only, there is
10136 no additional border below the possibly empty tool-bar lines.
10137 So to make the extra empty lines look "normal", we have to
10138 use the tool-bar face for the border too. */
10139 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10140 it->face_id = DEFAULT_FACE_ID;
10141
10142 extend_face_to_end_of_line (it);
10143 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10144 last->right_box_line_p = 1;
10145 if (last == row->glyphs[TEXT_AREA])
10146 last->left_box_line_p = 1;
10147
10148 /* Make line the desired height and center it vertically. */
10149 if ((height -= it->max_ascent + it->max_descent) > 0)
10150 {
10151 /* Don't add more than one line height. */
10152 height %= FRAME_LINE_HEIGHT (it->f);
10153 it->max_ascent += height / 2;
10154 it->max_descent += (height + 1) / 2;
10155 }
10156
10157 compute_line_metrics (it);
10158
10159 /* If line is empty, make it occupy the rest of the tool-bar. */
10160 if (!row->displays_text_p)
10161 {
10162 row->height = row->phys_height = it->last_visible_y - row->y;
10163 row->visible_height = row->height;
10164 row->ascent = row->phys_ascent = 0;
10165 row->extra_line_spacing = 0;
10166 }
10167
10168 row->full_width_p = 1;
10169 row->continued_p = 0;
10170 row->truncated_on_left_p = 0;
10171 row->truncated_on_right_p = 0;
10172
10173 it->current_x = it->hpos = 0;
10174 it->current_y += row->height;
10175 ++it->vpos;
10176 ++it->glyph_row;
10177 }
10178
10179
10180 /* Max tool-bar height. */
10181
10182 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10183 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10184
10185 /* Value is the number of screen lines needed to make all tool-bar
10186 items of frame F visible. The number of actual rows needed is
10187 returned in *N_ROWS if non-NULL. */
10188
10189 static int
10190 tool_bar_lines_needed (f, n_rows)
10191 struct frame *f;
10192 int *n_rows;
10193 {
10194 struct window *w = XWINDOW (f->tool_bar_window);
10195 struct it it;
10196 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10197 the desired matrix, so use (unused) mode-line row as temporary row to
10198 avoid destroying the first tool-bar row. */
10199 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10200
10201 /* Initialize an iterator for iteration over
10202 F->desired_tool_bar_string in the tool-bar window of frame F. */
10203 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10204 it.first_visible_x = 0;
10205 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10206 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10207
10208 while (!ITERATOR_AT_END_P (&it))
10209 {
10210 clear_glyph_row (temp_row);
10211 it.glyph_row = temp_row;
10212 display_tool_bar_line (&it, -1);
10213 }
10214 clear_glyph_row (temp_row);
10215
10216 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10217 if (n_rows)
10218 *n_rows = it.vpos > 0 ? it.vpos : -1;
10219
10220 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10221 }
10222
10223
10224 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10225 0, 1, 0,
10226 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10227 (frame)
10228 Lisp_Object frame;
10229 {
10230 struct frame *f;
10231 struct window *w;
10232 int nlines = 0;
10233
10234 if (NILP (frame))
10235 frame = selected_frame;
10236 else
10237 CHECK_FRAME (frame);
10238 f = XFRAME (frame);
10239
10240 if (WINDOWP (f->tool_bar_window)
10241 || (w = XWINDOW (f->tool_bar_window),
10242 WINDOW_TOTAL_LINES (w) > 0))
10243 {
10244 update_tool_bar (f, 1);
10245 if (f->n_tool_bar_items)
10246 {
10247 build_desired_tool_bar_string (f);
10248 nlines = tool_bar_lines_needed (f, NULL);
10249 }
10250 }
10251
10252 return make_number (nlines);
10253 }
10254
10255
10256 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10257 height should be changed. */
10258
10259 static int
10260 redisplay_tool_bar (f)
10261 struct frame *f;
10262 {
10263 struct window *w;
10264 struct it it;
10265 struct glyph_row *row;
10266
10267 #if defined (USE_GTK) || defined (HAVE_NS)
10268 if (FRAME_EXTERNAL_TOOL_BAR (f))
10269 update_frame_tool_bar (f);
10270 return 0;
10271 #endif
10272
10273 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10274 do anything. This means you must start with tool-bar-lines
10275 non-zero to get the auto-sizing effect. Or in other words, you
10276 can turn off tool-bars by specifying tool-bar-lines zero. */
10277 if (!WINDOWP (f->tool_bar_window)
10278 || (w = XWINDOW (f->tool_bar_window),
10279 WINDOW_TOTAL_LINES (w) == 0))
10280 return 0;
10281
10282 /* Set up an iterator for the tool-bar window. */
10283 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10284 it.first_visible_x = 0;
10285 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10286 row = it.glyph_row;
10287
10288 /* Build a string that represents the contents of the tool-bar. */
10289 build_desired_tool_bar_string (f);
10290 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10291
10292 if (f->n_tool_bar_rows == 0)
10293 {
10294 int nlines;
10295
10296 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10297 nlines != WINDOW_TOTAL_LINES (w)))
10298 {
10299 extern Lisp_Object Qtool_bar_lines;
10300 Lisp_Object frame;
10301 int old_height = WINDOW_TOTAL_LINES (w);
10302
10303 XSETFRAME (frame, f);
10304 Fmodify_frame_parameters (frame,
10305 Fcons (Fcons (Qtool_bar_lines,
10306 make_number (nlines)),
10307 Qnil));
10308 if (WINDOW_TOTAL_LINES (w) != old_height)
10309 {
10310 clear_glyph_matrix (w->desired_matrix);
10311 fonts_changed_p = 1;
10312 return 1;
10313 }
10314 }
10315 }
10316
10317 /* Display as many lines as needed to display all tool-bar items. */
10318
10319 if (f->n_tool_bar_rows > 0)
10320 {
10321 int border, rows, height, extra;
10322
10323 if (INTEGERP (Vtool_bar_border))
10324 border = XINT (Vtool_bar_border);
10325 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10326 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10327 else if (EQ (Vtool_bar_border, Qborder_width))
10328 border = f->border_width;
10329 else
10330 border = 0;
10331 if (border < 0)
10332 border = 0;
10333
10334 rows = f->n_tool_bar_rows;
10335 height = max (1, (it.last_visible_y - border) / rows);
10336 extra = it.last_visible_y - border - height * rows;
10337
10338 while (it.current_y < it.last_visible_y)
10339 {
10340 int h = 0;
10341 if (extra > 0 && rows-- > 0)
10342 {
10343 h = (extra + rows - 1) / rows;
10344 extra -= h;
10345 }
10346 display_tool_bar_line (&it, height + h);
10347 }
10348 }
10349 else
10350 {
10351 while (it.current_y < it.last_visible_y)
10352 display_tool_bar_line (&it, 0);
10353 }
10354
10355 /* It doesn't make much sense to try scrolling in the tool-bar
10356 window, so don't do it. */
10357 w->desired_matrix->no_scrolling_p = 1;
10358 w->must_be_updated_p = 1;
10359
10360 if (!NILP (Vauto_resize_tool_bars))
10361 {
10362 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10363 int change_height_p = 0;
10364
10365 /* If we couldn't display everything, change the tool-bar's
10366 height if there is room for more. */
10367 if (IT_STRING_CHARPOS (it) < it.end_charpos
10368 && it.current_y < max_tool_bar_height)
10369 change_height_p = 1;
10370
10371 row = it.glyph_row - 1;
10372
10373 /* If there are blank lines at the end, except for a partially
10374 visible blank line at the end that is smaller than
10375 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10376 if (!row->displays_text_p
10377 && row->height >= FRAME_LINE_HEIGHT (f))
10378 change_height_p = 1;
10379
10380 /* If row displays tool-bar items, but is partially visible,
10381 change the tool-bar's height. */
10382 if (row->displays_text_p
10383 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10384 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10385 change_height_p = 1;
10386
10387 /* Resize windows as needed by changing the `tool-bar-lines'
10388 frame parameter. */
10389 if (change_height_p)
10390 {
10391 extern Lisp_Object Qtool_bar_lines;
10392 Lisp_Object frame;
10393 int old_height = WINDOW_TOTAL_LINES (w);
10394 int nrows;
10395 int nlines = tool_bar_lines_needed (f, &nrows);
10396
10397 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10398 && !f->minimize_tool_bar_window_p)
10399 ? (nlines > old_height)
10400 : (nlines != old_height));
10401 f->minimize_tool_bar_window_p = 0;
10402
10403 if (change_height_p)
10404 {
10405 XSETFRAME (frame, f);
10406 Fmodify_frame_parameters (frame,
10407 Fcons (Fcons (Qtool_bar_lines,
10408 make_number (nlines)),
10409 Qnil));
10410 if (WINDOW_TOTAL_LINES (w) != old_height)
10411 {
10412 clear_glyph_matrix (w->desired_matrix);
10413 f->n_tool_bar_rows = nrows;
10414 fonts_changed_p = 1;
10415 return 1;
10416 }
10417 }
10418 }
10419 }
10420
10421 f->minimize_tool_bar_window_p = 0;
10422 return 0;
10423 }
10424
10425
10426 /* Get information about the tool-bar item which is displayed in GLYPH
10427 on frame F. Return in *PROP_IDX the index where tool-bar item
10428 properties start in F->tool_bar_items. Value is zero if
10429 GLYPH doesn't display a tool-bar item. */
10430
10431 static int
10432 tool_bar_item_info (f, glyph, prop_idx)
10433 struct frame *f;
10434 struct glyph *glyph;
10435 int *prop_idx;
10436 {
10437 Lisp_Object prop;
10438 int success_p;
10439 int charpos;
10440
10441 /* This function can be called asynchronously, which means we must
10442 exclude any possibility that Fget_text_property signals an
10443 error. */
10444 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10445 charpos = max (0, charpos);
10446
10447 /* Get the text property `menu-item' at pos. The value of that
10448 property is the start index of this item's properties in
10449 F->tool_bar_items. */
10450 prop = Fget_text_property (make_number (charpos),
10451 Qmenu_item, f->current_tool_bar_string);
10452 if (INTEGERP (prop))
10453 {
10454 *prop_idx = XINT (prop);
10455 success_p = 1;
10456 }
10457 else
10458 success_p = 0;
10459
10460 return success_p;
10461 }
10462
10463 \f
10464 /* Get information about the tool-bar item at position X/Y on frame F.
10465 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10466 the current matrix of the tool-bar window of F, or NULL if not
10467 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10468 item in F->tool_bar_items. Value is
10469
10470 -1 if X/Y is not on a tool-bar item
10471 0 if X/Y is on the same item that was highlighted before.
10472 1 otherwise. */
10473
10474 static int
10475 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10476 struct frame *f;
10477 int x, y;
10478 struct glyph **glyph;
10479 int *hpos, *vpos, *prop_idx;
10480 {
10481 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10482 struct window *w = XWINDOW (f->tool_bar_window);
10483 int area;
10484
10485 /* Find the glyph under X/Y. */
10486 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10487 if (*glyph == NULL)
10488 return -1;
10489
10490 /* Get the start of this tool-bar item's properties in
10491 f->tool_bar_items. */
10492 if (!tool_bar_item_info (f, *glyph, prop_idx))
10493 return -1;
10494
10495 /* Is mouse on the highlighted item? */
10496 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10497 && *vpos >= dpyinfo->mouse_face_beg_row
10498 && *vpos <= dpyinfo->mouse_face_end_row
10499 && (*vpos > dpyinfo->mouse_face_beg_row
10500 || *hpos >= dpyinfo->mouse_face_beg_col)
10501 && (*vpos < dpyinfo->mouse_face_end_row
10502 || *hpos < dpyinfo->mouse_face_end_col
10503 || dpyinfo->mouse_face_past_end))
10504 return 0;
10505
10506 return 1;
10507 }
10508
10509
10510 /* EXPORT:
10511 Handle mouse button event on the tool-bar of frame F, at
10512 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10513 0 for button release. MODIFIERS is event modifiers for button
10514 release. */
10515
10516 void
10517 handle_tool_bar_click (f, x, y, down_p, modifiers)
10518 struct frame *f;
10519 int x, y, down_p;
10520 unsigned int modifiers;
10521 {
10522 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10523 struct window *w = XWINDOW (f->tool_bar_window);
10524 int hpos, vpos, prop_idx;
10525 struct glyph *glyph;
10526 Lisp_Object enabled_p;
10527
10528 /* If not on the highlighted tool-bar item, return. */
10529 frame_to_window_pixel_xy (w, &x, &y);
10530 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10531 return;
10532
10533 /* If item is disabled, do nothing. */
10534 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10535 if (NILP (enabled_p))
10536 return;
10537
10538 if (down_p)
10539 {
10540 /* Show item in pressed state. */
10541 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10542 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10543 last_tool_bar_item = prop_idx;
10544 }
10545 else
10546 {
10547 Lisp_Object key, frame;
10548 struct input_event event;
10549 EVENT_INIT (event);
10550
10551 /* Show item in released state. */
10552 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10553 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10554
10555 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10556
10557 XSETFRAME (frame, f);
10558 event.kind = TOOL_BAR_EVENT;
10559 event.frame_or_window = frame;
10560 event.arg = frame;
10561 kbd_buffer_store_event (&event);
10562
10563 event.kind = TOOL_BAR_EVENT;
10564 event.frame_or_window = frame;
10565 event.arg = key;
10566 event.modifiers = modifiers;
10567 kbd_buffer_store_event (&event);
10568 last_tool_bar_item = -1;
10569 }
10570 }
10571
10572
10573 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10574 tool-bar window-relative coordinates X/Y. Called from
10575 note_mouse_highlight. */
10576
10577 static void
10578 note_tool_bar_highlight (f, x, y)
10579 struct frame *f;
10580 int x, y;
10581 {
10582 Lisp_Object window = f->tool_bar_window;
10583 struct window *w = XWINDOW (window);
10584 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10585 int hpos, vpos;
10586 struct glyph *glyph;
10587 struct glyph_row *row;
10588 int i;
10589 Lisp_Object enabled_p;
10590 int prop_idx;
10591 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10592 int mouse_down_p, rc;
10593
10594 /* Function note_mouse_highlight is called with negative x(y
10595 values when mouse moves outside of the frame. */
10596 if (x <= 0 || y <= 0)
10597 {
10598 clear_mouse_face (dpyinfo);
10599 return;
10600 }
10601
10602 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10603 if (rc < 0)
10604 {
10605 /* Not on tool-bar item. */
10606 clear_mouse_face (dpyinfo);
10607 return;
10608 }
10609 else if (rc == 0)
10610 /* On same tool-bar item as before. */
10611 goto set_help_echo;
10612
10613 clear_mouse_face (dpyinfo);
10614
10615 /* Mouse is down, but on different tool-bar item? */
10616 mouse_down_p = (dpyinfo->grabbed
10617 && f == last_mouse_frame
10618 && FRAME_LIVE_P (f));
10619 if (mouse_down_p
10620 && last_tool_bar_item != prop_idx)
10621 return;
10622
10623 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10624 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10625
10626 /* If tool-bar item is not enabled, don't highlight it. */
10627 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10628 if (!NILP (enabled_p))
10629 {
10630 /* Compute the x-position of the glyph. In front and past the
10631 image is a space. We include this in the highlighted area. */
10632 row = MATRIX_ROW (w->current_matrix, vpos);
10633 for (i = x = 0; i < hpos; ++i)
10634 x += row->glyphs[TEXT_AREA][i].pixel_width;
10635
10636 /* Record this as the current active region. */
10637 dpyinfo->mouse_face_beg_col = hpos;
10638 dpyinfo->mouse_face_beg_row = vpos;
10639 dpyinfo->mouse_face_beg_x = x;
10640 dpyinfo->mouse_face_beg_y = row->y;
10641 dpyinfo->mouse_face_past_end = 0;
10642
10643 dpyinfo->mouse_face_end_col = hpos + 1;
10644 dpyinfo->mouse_face_end_row = vpos;
10645 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10646 dpyinfo->mouse_face_end_y = row->y;
10647 dpyinfo->mouse_face_window = window;
10648 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10649
10650 /* Display it as active. */
10651 show_mouse_face (dpyinfo, draw);
10652 dpyinfo->mouse_face_image_state = draw;
10653 }
10654
10655 set_help_echo:
10656
10657 /* Set help_echo_string to a help string to display for this tool-bar item.
10658 XTread_socket does the rest. */
10659 help_echo_object = help_echo_window = Qnil;
10660 help_echo_pos = -1;
10661 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10662 if (NILP (help_echo_string))
10663 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10664 }
10665
10666 #endif /* HAVE_WINDOW_SYSTEM */
10667
10668
10669 \f
10670 /************************************************************************
10671 Horizontal scrolling
10672 ************************************************************************/
10673
10674 static int hscroll_window_tree P_ ((Lisp_Object));
10675 static int hscroll_windows P_ ((Lisp_Object));
10676
10677 /* For all leaf windows in the window tree rooted at WINDOW, set their
10678 hscroll value so that PT is (i) visible in the window, and (ii) so
10679 that it is not within a certain margin at the window's left and
10680 right border. Value is non-zero if any window's hscroll has been
10681 changed. */
10682
10683 static int
10684 hscroll_window_tree (window)
10685 Lisp_Object window;
10686 {
10687 int hscrolled_p = 0;
10688 int hscroll_relative_p = FLOATP (Vhscroll_step);
10689 int hscroll_step_abs = 0;
10690 double hscroll_step_rel = 0;
10691
10692 if (hscroll_relative_p)
10693 {
10694 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10695 if (hscroll_step_rel < 0)
10696 {
10697 hscroll_relative_p = 0;
10698 hscroll_step_abs = 0;
10699 }
10700 }
10701 else if (INTEGERP (Vhscroll_step))
10702 {
10703 hscroll_step_abs = XINT (Vhscroll_step);
10704 if (hscroll_step_abs < 0)
10705 hscroll_step_abs = 0;
10706 }
10707 else
10708 hscroll_step_abs = 0;
10709
10710 while (WINDOWP (window))
10711 {
10712 struct window *w = XWINDOW (window);
10713
10714 if (WINDOWP (w->hchild))
10715 hscrolled_p |= hscroll_window_tree (w->hchild);
10716 else if (WINDOWP (w->vchild))
10717 hscrolled_p |= hscroll_window_tree (w->vchild);
10718 else if (w->cursor.vpos >= 0)
10719 {
10720 int h_margin;
10721 int text_area_width;
10722 struct glyph_row *current_cursor_row
10723 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10724 struct glyph_row *desired_cursor_row
10725 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10726 struct glyph_row *cursor_row
10727 = (desired_cursor_row->enabled_p
10728 ? desired_cursor_row
10729 : current_cursor_row);
10730
10731 text_area_width = window_box_width (w, TEXT_AREA);
10732
10733 /* Scroll when cursor is inside this scroll margin. */
10734 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10735
10736 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10737 && ((XFASTINT (w->hscroll)
10738 && w->cursor.x <= h_margin)
10739 || (cursor_row->enabled_p
10740 && cursor_row->truncated_on_right_p
10741 && (w->cursor.x >= text_area_width - h_margin))))
10742 {
10743 struct it it;
10744 int hscroll;
10745 struct buffer *saved_current_buffer;
10746 int pt;
10747 int wanted_x;
10748
10749 /* Find point in a display of infinite width. */
10750 saved_current_buffer = current_buffer;
10751 current_buffer = XBUFFER (w->buffer);
10752
10753 if (w == XWINDOW (selected_window))
10754 pt = BUF_PT (current_buffer);
10755 else
10756 {
10757 pt = marker_position (w->pointm);
10758 pt = max (BEGV, pt);
10759 pt = min (ZV, pt);
10760 }
10761
10762 /* Move iterator to pt starting at cursor_row->start in
10763 a line with infinite width. */
10764 init_to_row_start (&it, w, cursor_row);
10765 it.last_visible_x = INFINITY;
10766 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10767 current_buffer = saved_current_buffer;
10768
10769 /* Position cursor in window. */
10770 if (!hscroll_relative_p && hscroll_step_abs == 0)
10771 hscroll = max (0, (it.current_x
10772 - (ITERATOR_AT_END_OF_LINE_P (&it)
10773 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10774 : (text_area_width / 2))))
10775 / FRAME_COLUMN_WIDTH (it.f);
10776 else if (w->cursor.x >= text_area_width - h_margin)
10777 {
10778 if (hscroll_relative_p)
10779 wanted_x = text_area_width * (1 - hscroll_step_rel)
10780 - h_margin;
10781 else
10782 wanted_x = text_area_width
10783 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10784 - h_margin;
10785 hscroll
10786 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10787 }
10788 else
10789 {
10790 if (hscroll_relative_p)
10791 wanted_x = text_area_width * hscroll_step_rel
10792 + h_margin;
10793 else
10794 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10795 + h_margin;
10796 hscroll
10797 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10798 }
10799 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10800
10801 /* Don't call Fset_window_hscroll if value hasn't
10802 changed because it will prevent redisplay
10803 optimizations. */
10804 if (XFASTINT (w->hscroll) != hscroll)
10805 {
10806 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10807 w->hscroll = make_number (hscroll);
10808 hscrolled_p = 1;
10809 }
10810 }
10811 }
10812
10813 window = w->next;
10814 }
10815
10816 /* Value is non-zero if hscroll of any leaf window has been changed. */
10817 return hscrolled_p;
10818 }
10819
10820
10821 /* Set hscroll so that cursor is visible and not inside horizontal
10822 scroll margins for all windows in the tree rooted at WINDOW. See
10823 also hscroll_window_tree above. Value is non-zero if any window's
10824 hscroll has been changed. If it has, desired matrices on the frame
10825 of WINDOW are cleared. */
10826
10827 static int
10828 hscroll_windows (window)
10829 Lisp_Object window;
10830 {
10831 int hscrolled_p = hscroll_window_tree (window);
10832 if (hscrolled_p)
10833 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10834 return hscrolled_p;
10835 }
10836
10837
10838 \f
10839 /************************************************************************
10840 Redisplay
10841 ************************************************************************/
10842
10843 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10844 to a non-zero value. This is sometimes handy to have in a debugger
10845 session. */
10846
10847 #if GLYPH_DEBUG
10848
10849 /* First and last unchanged row for try_window_id. */
10850
10851 int debug_first_unchanged_at_end_vpos;
10852 int debug_last_unchanged_at_beg_vpos;
10853
10854 /* Delta vpos and y. */
10855
10856 int debug_dvpos, debug_dy;
10857
10858 /* Delta in characters and bytes for try_window_id. */
10859
10860 int debug_delta, debug_delta_bytes;
10861
10862 /* Values of window_end_pos and window_end_vpos at the end of
10863 try_window_id. */
10864
10865 EMACS_INT debug_end_pos, debug_end_vpos;
10866
10867 /* Append a string to W->desired_matrix->method. FMT is a printf
10868 format string. A1...A9 are a supplement for a variable-length
10869 argument list. If trace_redisplay_p is non-zero also printf the
10870 resulting string to stderr. */
10871
10872 static void
10873 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10874 struct window *w;
10875 char *fmt;
10876 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10877 {
10878 char buffer[512];
10879 char *method = w->desired_matrix->method;
10880 int len = strlen (method);
10881 int size = sizeof w->desired_matrix->method;
10882 int remaining = size - len - 1;
10883
10884 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10885 if (len && remaining)
10886 {
10887 method[len] = '|';
10888 --remaining, ++len;
10889 }
10890
10891 strncpy (method + len, buffer, remaining);
10892
10893 if (trace_redisplay_p)
10894 fprintf (stderr, "%p (%s): %s\n",
10895 w,
10896 ((BUFFERP (w->buffer)
10897 && STRINGP (XBUFFER (w->buffer)->name))
10898 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10899 : "no buffer"),
10900 buffer);
10901 }
10902
10903 #endif /* GLYPH_DEBUG */
10904
10905
10906 /* Value is non-zero if all changes in window W, which displays
10907 current_buffer, are in the text between START and END. START is a
10908 buffer position, END is given as a distance from Z. Used in
10909 redisplay_internal for display optimization. */
10910
10911 static INLINE int
10912 text_outside_line_unchanged_p (w, start, end)
10913 struct window *w;
10914 int start, end;
10915 {
10916 int unchanged_p = 1;
10917
10918 /* If text or overlays have changed, see where. */
10919 if (XFASTINT (w->last_modified) < MODIFF
10920 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10921 {
10922 /* Gap in the line? */
10923 if (GPT < start || Z - GPT < end)
10924 unchanged_p = 0;
10925
10926 /* Changes start in front of the line, or end after it? */
10927 if (unchanged_p
10928 && (BEG_UNCHANGED < start - 1
10929 || END_UNCHANGED < end))
10930 unchanged_p = 0;
10931
10932 /* If selective display, can't optimize if changes start at the
10933 beginning of the line. */
10934 if (unchanged_p
10935 && INTEGERP (current_buffer->selective_display)
10936 && XINT (current_buffer->selective_display) > 0
10937 && (BEG_UNCHANGED < start || GPT <= start))
10938 unchanged_p = 0;
10939
10940 /* If there are overlays at the start or end of the line, these
10941 may have overlay strings with newlines in them. A change at
10942 START, for instance, may actually concern the display of such
10943 overlay strings as well, and they are displayed on different
10944 lines. So, quickly rule out this case. (For the future, it
10945 might be desirable to implement something more telling than
10946 just BEG/END_UNCHANGED.) */
10947 if (unchanged_p)
10948 {
10949 if (BEG + BEG_UNCHANGED == start
10950 && overlay_touches_p (start))
10951 unchanged_p = 0;
10952 if (END_UNCHANGED == end
10953 && overlay_touches_p (Z - end))
10954 unchanged_p = 0;
10955 }
10956 }
10957
10958 return unchanged_p;
10959 }
10960
10961
10962 /* Do a frame update, taking possible shortcuts into account. This is
10963 the main external entry point for redisplay.
10964
10965 If the last redisplay displayed an echo area message and that message
10966 is no longer requested, we clear the echo area or bring back the
10967 mini-buffer if that is in use. */
10968
10969 void
10970 redisplay ()
10971 {
10972 redisplay_internal (0);
10973 }
10974
10975
10976 static Lisp_Object
10977 overlay_arrow_string_or_property (var)
10978 Lisp_Object var;
10979 {
10980 Lisp_Object val;
10981
10982 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10983 return val;
10984
10985 return Voverlay_arrow_string;
10986 }
10987
10988 /* Return 1 if there are any overlay-arrows in current_buffer. */
10989 static int
10990 overlay_arrow_in_current_buffer_p ()
10991 {
10992 Lisp_Object vlist;
10993
10994 for (vlist = Voverlay_arrow_variable_list;
10995 CONSP (vlist);
10996 vlist = XCDR (vlist))
10997 {
10998 Lisp_Object var = XCAR (vlist);
10999 Lisp_Object val;
11000
11001 if (!SYMBOLP (var))
11002 continue;
11003 val = find_symbol_value (var);
11004 if (MARKERP (val)
11005 && current_buffer == XMARKER (val)->buffer)
11006 return 1;
11007 }
11008 return 0;
11009 }
11010
11011
11012 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11013 has changed. */
11014
11015 static int
11016 overlay_arrows_changed_p ()
11017 {
11018 Lisp_Object vlist;
11019
11020 for (vlist = Voverlay_arrow_variable_list;
11021 CONSP (vlist);
11022 vlist = XCDR (vlist))
11023 {
11024 Lisp_Object var = XCAR (vlist);
11025 Lisp_Object val, pstr;
11026
11027 if (!SYMBOLP (var))
11028 continue;
11029 val = find_symbol_value (var);
11030 if (!MARKERP (val))
11031 continue;
11032 if (! EQ (COERCE_MARKER (val),
11033 Fget (var, Qlast_arrow_position))
11034 || ! (pstr = overlay_arrow_string_or_property (var),
11035 EQ (pstr, Fget (var, Qlast_arrow_string))))
11036 return 1;
11037 }
11038 return 0;
11039 }
11040
11041 /* Mark overlay arrows to be updated on next redisplay. */
11042
11043 static void
11044 update_overlay_arrows (up_to_date)
11045 int up_to_date;
11046 {
11047 Lisp_Object vlist;
11048
11049 for (vlist = Voverlay_arrow_variable_list;
11050 CONSP (vlist);
11051 vlist = XCDR (vlist))
11052 {
11053 Lisp_Object var = XCAR (vlist);
11054
11055 if (!SYMBOLP (var))
11056 continue;
11057
11058 if (up_to_date > 0)
11059 {
11060 Lisp_Object val = find_symbol_value (var);
11061 Fput (var, Qlast_arrow_position,
11062 COERCE_MARKER (val));
11063 Fput (var, Qlast_arrow_string,
11064 overlay_arrow_string_or_property (var));
11065 }
11066 else if (up_to_date < 0
11067 || !NILP (Fget (var, Qlast_arrow_position)))
11068 {
11069 Fput (var, Qlast_arrow_position, Qt);
11070 Fput (var, Qlast_arrow_string, Qt);
11071 }
11072 }
11073 }
11074
11075
11076 /* Return overlay arrow string to display at row.
11077 Return integer (bitmap number) for arrow bitmap in left fringe.
11078 Return nil if no overlay arrow. */
11079
11080 static Lisp_Object
11081 overlay_arrow_at_row (it, row)
11082 struct it *it;
11083 struct glyph_row *row;
11084 {
11085 Lisp_Object vlist;
11086
11087 for (vlist = Voverlay_arrow_variable_list;
11088 CONSP (vlist);
11089 vlist = XCDR (vlist))
11090 {
11091 Lisp_Object var = XCAR (vlist);
11092 Lisp_Object val;
11093
11094 if (!SYMBOLP (var))
11095 continue;
11096
11097 val = find_symbol_value (var);
11098
11099 if (MARKERP (val)
11100 && current_buffer == XMARKER (val)->buffer
11101 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11102 {
11103 if (FRAME_WINDOW_P (it->f)
11104 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11105 {
11106 #ifdef HAVE_WINDOW_SYSTEM
11107 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11108 {
11109 int fringe_bitmap;
11110 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11111 return make_number (fringe_bitmap);
11112 }
11113 #endif
11114 return make_number (-1); /* Use default arrow bitmap */
11115 }
11116 return overlay_arrow_string_or_property (var);
11117 }
11118 }
11119
11120 return Qnil;
11121 }
11122
11123 /* Return 1 if point moved out of or into a composition. Otherwise
11124 return 0. PREV_BUF and PREV_PT are the last point buffer and
11125 position. BUF and PT are the current point buffer and position. */
11126
11127 int
11128 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11129 struct buffer *prev_buf, *buf;
11130 int prev_pt, pt;
11131 {
11132 EMACS_INT start, end;
11133 Lisp_Object prop;
11134 Lisp_Object buffer;
11135
11136 XSETBUFFER (buffer, buf);
11137 /* Check a composition at the last point if point moved within the
11138 same buffer. */
11139 if (prev_buf == buf)
11140 {
11141 if (prev_pt == pt)
11142 /* Point didn't move. */
11143 return 0;
11144
11145 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11146 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11147 && COMPOSITION_VALID_P (start, end, prop)
11148 && start < prev_pt && end > prev_pt)
11149 /* The last point was within the composition. Return 1 iff
11150 point moved out of the composition. */
11151 return (pt <= start || pt >= end);
11152 }
11153
11154 /* Check a composition at the current point. */
11155 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11156 && find_composition (pt, -1, &start, &end, &prop, buffer)
11157 && COMPOSITION_VALID_P (start, end, prop)
11158 && start < pt && end > pt);
11159 }
11160
11161
11162 /* Reconsider the setting of B->clip_changed which is displayed
11163 in window W. */
11164
11165 static INLINE void
11166 reconsider_clip_changes (w, b)
11167 struct window *w;
11168 struct buffer *b;
11169 {
11170 if (b->clip_changed
11171 && !NILP (w->window_end_valid)
11172 && w->current_matrix->buffer == b
11173 && w->current_matrix->zv == BUF_ZV (b)
11174 && w->current_matrix->begv == BUF_BEGV (b))
11175 b->clip_changed = 0;
11176
11177 /* If display wasn't paused, and W is not a tool bar window, see if
11178 point has been moved into or out of a composition. In that case,
11179 we set b->clip_changed to 1 to force updating the screen. If
11180 b->clip_changed has already been set to 1, we can skip this
11181 check. */
11182 if (!b->clip_changed
11183 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11184 {
11185 int pt;
11186
11187 if (w == XWINDOW (selected_window))
11188 pt = BUF_PT (current_buffer);
11189 else
11190 pt = marker_position (w->pointm);
11191
11192 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11193 || pt != XINT (w->last_point))
11194 && check_point_in_composition (w->current_matrix->buffer,
11195 XINT (w->last_point),
11196 XBUFFER (w->buffer), pt))
11197 b->clip_changed = 1;
11198 }
11199 }
11200 \f
11201
11202 /* Select FRAME to forward the values of frame-local variables into C
11203 variables so that the redisplay routines can access those values
11204 directly. */
11205
11206 static void
11207 select_frame_for_redisplay (frame)
11208 Lisp_Object frame;
11209 {
11210 Lisp_Object tail, symbol, val;
11211 Lisp_Object old = selected_frame;
11212 struct Lisp_Symbol *sym;
11213
11214 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11215
11216 selected_frame = frame;
11217
11218 do
11219 {
11220 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11221 if (CONSP (XCAR (tail))
11222 && (symbol = XCAR (XCAR (tail)),
11223 SYMBOLP (symbol))
11224 && (sym = indirect_variable (XSYMBOL (symbol)),
11225 val = sym->value,
11226 (BUFFER_LOCAL_VALUEP (val)))
11227 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11228 /* Use find_symbol_value rather than Fsymbol_value
11229 to avoid an error if it is void. */
11230 find_symbol_value (symbol);
11231 } while (!EQ (frame, old) && (frame = old, 1));
11232 }
11233
11234
11235 #define STOP_POLLING \
11236 do { if (! polling_stopped_here) stop_polling (); \
11237 polling_stopped_here = 1; } while (0)
11238
11239 #define RESUME_POLLING \
11240 do { if (polling_stopped_here) start_polling (); \
11241 polling_stopped_here = 0; } while (0)
11242
11243
11244 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11245 response to any user action; therefore, we should preserve the echo
11246 area. (Actually, our caller does that job.) Perhaps in the future
11247 avoid recentering windows if it is not necessary; currently that
11248 causes some problems. */
11249
11250 static void
11251 redisplay_internal (preserve_echo_area)
11252 int preserve_echo_area;
11253 {
11254 struct window *w = XWINDOW (selected_window);
11255 struct frame *f;
11256 int pause;
11257 int must_finish = 0;
11258 struct text_pos tlbufpos, tlendpos;
11259 int number_of_visible_frames;
11260 int count, count1;
11261 struct frame *sf;
11262 int polling_stopped_here = 0;
11263 Lisp_Object old_frame = selected_frame;
11264
11265 /* Non-zero means redisplay has to consider all windows on all
11266 frames. Zero means, only selected_window is considered. */
11267 int consider_all_windows_p;
11268
11269 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11270
11271 /* No redisplay if running in batch mode or frame is not yet fully
11272 initialized, or redisplay is explicitly turned off by setting
11273 Vinhibit_redisplay. */
11274 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11275 || !NILP (Vinhibit_redisplay))
11276 return;
11277
11278 /* Don't examine these until after testing Vinhibit_redisplay.
11279 When Emacs is shutting down, perhaps because its connection to
11280 X has dropped, we should not look at them at all. */
11281 f = XFRAME (w->frame);
11282 sf = SELECTED_FRAME ();
11283
11284 if (!f->glyphs_initialized_p)
11285 return;
11286
11287 /* The flag redisplay_performed_directly_p is set by
11288 direct_output_for_insert when it already did the whole screen
11289 update necessary. */
11290 if (redisplay_performed_directly_p)
11291 {
11292 redisplay_performed_directly_p = 0;
11293 if (!hscroll_windows (selected_window))
11294 return;
11295 }
11296
11297 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11298 if (popup_activated ())
11299 return;
11300 #endif
11301
11302 /* I don't think this happens but let's be paranoid. */
11303 if (redisplaying_p)
11304 return;
11305
11306 /* Record a function that resets redisplaying_p to its old value
11307 when we leave this function. */
11308 count = SPECPDL_INDEX ();
11309 record_unwind_protect (unwind_redisplay,
11310 Fcons (make_number (redisplaying_p), selected_frame));
11311 ++redisplaying_p;
11312 specbind (Qinhibit_free_realized_faces, Qnil);
11313
11314 {
11315 Lisp_Object tail, frame;
11316
11317 FOR_EACH_FRAME (tail, frame)
11318 {
11319 struct frame *f = XFRAME (frame);
11320 f->already_hscrolled_p = 0;
11321 }
11322 }
11323
11324 retry:
11325 if (!EQ (old_frame, selected_frame)
11326 && FRAME_LIVE_P (XFRAME (old_frame)))
11327 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11328 selected_frame and selected_window to be temporarily out-of-sync so
11329 when we come back here via `goto retry', we need to resync because we
11330 may need to run Elisp code (via prepare_menu_bars). */
11331 select_frame_for_redisplay (old_frame);
11332
11333 pause = 0;
11334 reconsider_clip_changes (w, current_buffer);
11335 last_escape_glyph_frame = NULL;
11336 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11337
11338 /* If new fonts have been loaded that make a glyph matrix adjustment
11339 necessary, do it. */
11340 if (fonts_changed_p)
11341 {
11342 adjust_glyphs (NULL);
11343 ++windows_or_buffers_changed;
11344 fonts_changed_p = 0;
11345 }
11346
11347 /* If face_change_count is non-zero, init_iterator will free all
11348 realized faces, which includes the faces referenced from current
11349 matrices. So, we can't reuse current matrices in this case. */
11350 if (face_change_count)
11351 ++windows_or_buffers_changed;
11352
11353 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11354 && FRAME_TTY (sf)->previous_frame != sf)
11355 {
11356 /* Since frames on a single ASCII terminal share the same
11357 display area, displaying a different frame means redisplay
11358 the whole thing. */
11359 windows_or_buffers_changed++;
11360 SET_FRAME_GARBAGED (sf);
11361 #ifndef DOS_NT
11362 set_tty_color_mode (FRAME_TTY (sf), sf);
11363 #endif
11364 FRAME_TTY (sf)->previous_frame = sf;
11365 }
11366
11367 /* Set the visible flags for all frames. Do this before checking
11368 for resized or garbaged frames; they want to know if their frames
11369 are visible. See the comment in frame.h for
11370 FRAME_SAMPLE_VISIBILITY. */
11371 {
11372 Lisp_Object tail, frame;
11373
11374 number_of_visible_frames = 0;
11375
11376 FOR_EACH_FRAME (tail, frame)
11377 {
11378 struct frame *f = XFRAME (frame);
11379
11380 FRAME_SAMPLE_VISIBILITY (f);
11381 if (FRAME_VISIBLE_P (f))
11382 ++number_of_visible_frames;
11383 clear_desired_matrices (f);
11384 }
11385 }
11386
11387 /* Notice any pending interrupt request to change frame size. */
11388 do_pending_window_change (1);
11389
11390 /* Clear frames marked as garbaged. */
11391 if (frame_garbaged)
11392 clear_garbaged_frames ();
11393
11394 /* Build menubar and tool-bar items. */
11395 if (NILP (Vmemory_full))
11396 prepare_menu_bars ();
11397
11398 if (windows_or_buffers_changed)
11399 update_mode_lines++;
11400
11401 /* Detect case that we need to write or remove a star in the mode line. */
11402 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11403 {
11404 w->update_mode_line = Qt;
11405 if (buffer_shared > 1)
11406 update_mode_lines++;
11407 }
11408
11409 /* Avoid invocation of point motion hooks by `current_column' below. */
11410 count1 = SPECPDL_INDEX ();
11411 specbind (Qinhibit_point_motion_hooks, Qt);
11412
11413 /* If %c is in the mode line, update it if needed. */
11414 if (!NILP (w->column_number_displayed)
11415 /* This alternative quickly identifies a common case
11416 where no change is needed. */
11417 && !(PT == XFASTINT (w->last_point)
11418 && XFASTINT (w->last_modified) >= MODIFF
11419 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11420 && (XFASTINT (w->column_number_displayed)
11421 != (int) current_column ())) /* iftc */
11422 w->update_mode_line = Qt;
11423
11424 unbind_to (count1, Qnil);
11425
11426 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11427
11428 /* The variable buffer_shared is set in redisplay_window and
11429 indicates that we redisplay a buffer in different windows. See
11430 there. */
11431 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11432 || cursor_type_changed);
11433
11434 /* If specs for an arrow have changed, do thorough redisplay
11435 to ensure we remove any arrow that should no longer exist. */
11436 if (overlay_arrows_changed_p ())
11437 consider_all_windows_p = windows_or_buffers_changed = 1;
11438
11439 /* Normally the message* functions will have already displayed and
11440 updated the echo area, but the frame may have been trashed, or
11441 the update may have been preempted, so display the echo area
11442 again here. Checking message_cleared_p captures the case that
11443 the echo area should be cleared. */
11444 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11445 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11446 || (message_cleared_p
11447 && minibuf_level == 0
11448 /* If the mini-window is currently selected, this means the
11449 echo-area doesn't show through. */
11450 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11451 {
11452 int window_height_changed_p = echo_area_display (0);
11453 must_finish = 1;
11454
11455 /* If we don't display the current message, don't clear the
11456 message_cleared_p flag, because, if we did, we wouldn't clear
11457 the echo area in the next redisplay which doesn't preserve
11458 the echo area. */
11459 if (!display_last_displayed_message_p)
11460 message_cleared_p = 0;
11461
11462 if (fonts_changed_p)
11463 goto retry;
11464 else if (window_height_changed_p)
11465 {
11466 consider_all_windows_p = 1;
11467 ++update_mode_lines;
11468 ++windows_or_buffers_changed;
11469
11470 /* If window configuration was changed, frames may have been
11471 marked garbaged. Clear them or we will experience
11472 surprises wrt scrolling. */
11473 if (frame_garbaged)
11474 clear_garbaged_frames ();
11475 }
11476 }
11477 else if (EQ (selected_window, minibuf_window)
11478 && (current_buffer->clip_changed
11479 || XFASTINT (w->last_modified) < MODIFF
11480 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11481 && resize_mini_window (w, 0))
11482 {
11483 /* Resized active mini-window to fit the size of what it is
11484 showing if its contents might have changed. */
11485 must_finish = 1;
11486 /* FIXME: this causes all frames to be updated, which seems unnecessary
11487 since only the current frame needs to be considered. This function needs
11488 to be rewritten with two variables, consider_all_windows and
11489 consider_all_frames. */
11490 consider_all_windows_p = 1;
11491 ++windows_or_buffers_changed;
11492 ++update_mode_lines;
11493
11494 /* If window configuration was changed, frames may have been
11495 marked garbaged. Clear them or we will experience
11496 surprises wrt scrolling. */
11497 if (frame_garbaged)
11498 clear_garbaged_frames ();
11499 }
11500
11501
11502 /* If showing the region, and mark has changed, we must redisplay
11503 the whole window. The assignment to this_line_start_pos prevents
11504 the optimization directly below this if-statement. */
11505 if (((!NILP (Vtransient_mark_mode)
11506 && !NILP (XBUFFER (w->buffer)->mark_active))
11507 != !NILP (w->region_showing))
11508 || (!NILP (w->region_showing)
11509 && !EQ (w->region_showing,
11510 Fmarker_position (XBUFFER (w->buffer)->mark))))
11511 CHARPOS (this_line_start_pos) = 0;
11512
11513 /* Optimize the case that only the line containing the cursor in the
11514 selected window has changed. Variables starting with this_ are
11515 set in display_line and record information about the line
11516 containing the cursor. */
11517 tlbufpos = this_line_start_pos;
11518 tlendpos = this_line_end_pos;
11519 if (!consider_all_windows_p
11520 && CHARPOS (tlbufpos) > 0
11521 && NILP (w->update_mode_line)
11522 && !current_buffer->clip_changed
11523 && !current_buffer->prevent_redisplay_optimizations_p
11524 && FRAME_VISIBLE_P (XFRAME (w->frame))
11525 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11526 /* Make sure recorded data applies to current buffer, etc. */
11527 && this_line_buffer == current_buffer
11528 && current_buffer == XBUFFER (w->buffer)
11529 && NILP (w->force_start)
11530 && NILP (w->optional_new_start)
11531 /* Point must be on the line that we have info recorded about. */
11532 && PT >= CHARPOS (tlbufpos)
11533 && PT <= Z - CHARPOS (tlendpos)
11534 /* All text outside that line, including its final newline,
11535 must be unchanged. */
11536 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11537 CHARPOS (tlendpos)))
11538 {
11539 if (CHARPOS (tlbufpos) > BEGV
11540 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11541 && (CHARPOS (tlbufpos) == ZV
11542 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11543 /* Former continuation line has disappeared by becoming empty. */
11544 goto cancel;
11545 else if (XFASTINT (w->last_modified) < MODIFF
11546 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11547 || MINI_WINDOW_P (w))
11548 {
11549 /* We have to handle the case of continuation around a
11550 wide-column character (see the comment in indent.c around
11551 line 1340).
11552
11553 For instance, in the following case:
11554
11555 -------- Insert --------
11556 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11557 J_I_ ==> J_I_ `^^' are cursors.
11558 ^^ ^^
11559 -------- --------
11560
11561 As we have to redraw the line above, we cannot use this
11562 optimization. */
11563
11564 struct it it;
11565 int line_height_before = this_line_pixel_height;
11566
11567 /* Note that start_display will handle the case that the
11568 line starting at tlbufpos is a continuation line. */
11569 start_display (&it, w, tlbufpos);
11570
11571 /* Implementation note: It this still necessary? */
11572 if (it.current_x != this_line_start_x)
11573 goto cancel;
11574
11575 TRACE ((stderr, "trying display optimization 1\n"));
11576 w->cursor.vpos = -1;
11577 overlay_arrow_seen = 0;
11578 it.vpos = this_line_vpos;
11579 it.current_y = this_line_y;
11580 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11581 display_line (&it);
11582
11583 /* If line contains point, is not continued,
11584 and ends at same distance from eob as before, we win. */
11585 if (w->cursor.vpos >= 0
11586 /* Line is not continued, otherwise this_line_start_pos
11587 would have been set to 0 in display_line. */
11588 && CHARPOS (this_line_start_pos)
11589 /* Line ends as before. */
11590 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11591 /* Line has same height as before. Otherwise other lines
11592 would have to be shifted up or down. */
11593 && this_line_pixel_height == line_height_before)
11594 {
11595 /* If this is not the window's last line, we must adjust
11596 the charstarts of the lines below. */
11597 if (it.current_y < it.last_visible_y)
11598 {
11599 struct glyph_row *row
11600 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11601 int delta, delta_bytes;
11602
11603 /* We used to distinguish between two cases here,
11604 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11605 when the line ends in a newline or the end of the
11606 buffer's accessible portion. But both cases did
11607 the same, so they were collapsed. */
11608 delta = (Z
11609 - CHARPOS (tlendpos)
11610 - MATRIX_ROW_START_CHARPOS (row));
11611 delta_bytes = (Z_BYTE
11612 - BYTEPOS (tlendpos)
11613 - MATRIX_ROW_START_BYTEPOS (row));
11614
11615 increment_matrix_positions (w->current_matrix,
11616 this_line_vpos + 1,
11617 w->current_matrix->nrows,
11618 delta, delta_bytes);
11619 }
11620
11621 /* If this row displays text now but previously didn't,
11622 or vice versa, w->window_end_vpos may have to be
11623 adjusted. */
11624 if ((it.glyph_row - 1)->displays_text_p)
11625 {
11626 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11627 XSETINT (w->window_end_vpos, this_line_vpos);
11628 }
11629 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11630 && this_line_vpos > 0)
11631 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11632 w->window_end_valid = Qnil;
11633
11634 /* Update hint: No need to try to scroll in update_window. */
11635 w->desired_matrix->no_scrolling_p = 1;
11636
11637 #if GLYPH_DEBUG
11638 *w->desired_matrix->method = 0;
11639 debug_method_add (w, "optimization 1");
11640 #endif
11641 #ifdef HAVE_WINDOW_SYSTEM
11642 update_window_fringes (w, 0);
11643 #endif
11644 goto update;
11645 }
11646 else
11647 goto cancel;
11648 }
11649 else if (/* Cursor position hasn't changed. */
11650 PT == XFASTINT (w->last_point)
11651 /* Make sure the cursor was last displayed
11652 in this window. Otherwise we have to reposition it. */
11653 && 0 <= w->cursor.vpos
11654 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11655 {
11656 if (!must_finish)
11657 {
11658 do_pending_window_change (1);
11659
11660 /* We used to always goto end_of_redisplay here, but this
11661 isn't enough if we have a blinking cursor. */
11662 if (w->cursor_off_p == w->last_cursor_off_p)
11663 goto end_of_redisplay;
11664 }
11665 goto update;
11666 }
11667 /* If highlighting the region, or if the cursor is in the echo area,
11668 then we can't just move the cursor. */
11669 else if (! (!NILP (Vtransient_mark_mode)
11670 && !NILP (current_buffer->mark_active))
11671 && (EQ (selected_window, current_buffer->last_selected_window)
11672 || highlight_nonselected_windows)
11673 && NILP (w->region_showing)
11674 && NILP (Vshow_trailing_whitespace)
11675 && !cursor_in_echo_area)
11676 {
11677 struct it it;
11678 struct glyph_row *row;
11679
11680 /* Skip from tlbufpos to PT and see where it is. Note that
11681 PT may be in invisible text. If so, we will end at the
11682 next visible position. */
11683 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11684 NULL, DEFAULT_FACE_ID);
11685 it.current_x = this_line_start_x;
11686 it.current_y = this_line_y;
11687 it.vpos = this_line_vpos;
11688
11689 /* The call to move_it_to stops in front of PT, but
11690 moves over before-strings. */
11691 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11692
11693 if (it.vpos == this_line_vpos
11694 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11695 row->enabled_p))
11696 {
11697 xassert (this_line_vpos == it.vpos);
11698 xassert (this_line_y == it.current_y);
11699 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11700 #if GLYPH_DEBUG
11701 *w->desired_matrix->method = 0;
11702 debug_method_add (w, "optimization 3");
11703 #endif
11704 goto update;
11705 }
11706 else
11707 goto cancel;
11708 }
11709
11710 cancel:
11711 /* Text changed drastically or point moved off of line. */
11712 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11713 }
11714
11715 CHARPOS (this_line_start_pos) = 0;
11716 consider_all_windows_p |= buffer_shared > 1;
11717 ++clear_face_cache_count;
11718 #ifdef HAVE_WINDOW_SYSTEM
11719 ++clear_image_cache_count;
11720 #endif
11721
11722 /* Build desired matrices, and update the display. If
11723 consider_all_windows_p is non-zero, do it for all windows on all
11724 frames. Otherwise do it for selected_window, only. */
11725
11726 if (consider_all_windows_p)
11727 {
11728 Lisp_Object tail, frame;
11729
11730 FOR_EACH_FRAME (tail, frame)
11731 XFRAME (frame)->updated_p = 0;
11732
11733 /* Recompute # windows showing selected buffer. This will be
11734 incremented each time such a window is displayed. */
11735 buffer_shared = 0;
11736
11737 FOR_EACH_FRAME (tail, frame)
11738 {
11739 struct frame *f = XFRAME (frame);
11740
11741 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11742 {
11743 if (! EQ (frame, selected_frame))
11744 /* Select the frame, for the sake of frame-local
11745 variables. */
11746 select_frame_for_redisplay (frame);
11747
11748 /* Mark all the scroll bars to be removed; we'll redeem
11749 the ones we want when we redisplay their windows. */
11750 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11751 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11752
11753 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11754 redisplay_windows (FRAME_ROOT_WINDOW (f));
11755
11756 /* The X error handler may have deleted that frame. */
11757 if (!FRAME_LIVE_P (f))
11758 continue;
11759
11760 /* Any scroll bars which redisplay_windows should have
11761 nuked should now go away. */
11762 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11763 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11764
11765 /* If fonts changed, display again. */
11766 /* ??? rms: I suspect it is a mistake to jump all the way
11767 back to retry here. It should just retry this frame. */
11768 if (fonts_changed_p)
11769 goto retry;
11770
11771 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11772 {
11773 /* See if we have to hscroll. */
11774 if (!f->already_hscrolled_p)
11775 {
11776 f->already_hscrolled_p = 1;
11777 if (hscroll_windows (f->root_window))
11778 goto retry;
11779 }
11780
11781 /* Prevent various kinds of signals during display
11782 update. stdio is not robust about handling
11783 signals, which can cause an apparent I/O
11784 error. */
11785 if (interrupt_input)
11786 unrequest_sigio ();
11787 STOP_POLLING;
11788
11789 /* Update the display. */
11790 set_window_update_flags (XWINDOW (f->root_window), 1);
11791 pause |= update_frame (f, 0, 0);
11792 f->updated_p = 1;
11793 }
11794 }
11795 }
11796
11797 if (!EQ (old_frame, selected_frame)
11798 && FRAME_LIVE_P (XFRAME (old_frame)))
11799 /* We played a bit fast-and-loose above and allowed selected_frame
11800 and selected_window to be temporarily out-of-sync but let's make
11801 sure this stays contained. */
11802 select_frame_for_redisplay (old_frame);
11803 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11804
11805 if (!pause)
11806 {
11807 /* Do the mark_window_display_accurate after all windows have
11808 been redisplayed because this call resets flags in buffers
11809 which are needed for proper redisplay. */
11810 FOR_EACH_FRAME (tail, frame)
11811 {
11812 struct frame *f = XFRAME (frame);
11813 if (f->updated_p)
11814 {
11815 mark_window_display_accurate (f->root_window, 1);
11816 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11817 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11818 }
11819 }
11820 }
11821 }
11822 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11823 {
11824 Lisp_Object mini_window;
11825 struct frame *mini_frame;
11826
11827 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11828 /* Use list_of_error, not Qerror, so that
11829 we catch only errors and don't run the debugger. */
11830 internal_condition_case_1 (redisplay_window_1, selected_window,
11831 list_of_error,
11832 redisplay_window_error);
11833
11834 /* Compare desired and current matrices, perform output. */
11835
11836 update:
11837 /* If fonts changed, display again. */
11838 if (fonts_changed_p)
11839 goto retry;
11840
11841 /* Prevent various kinds of signals during display update.
11842 stdio is not robust about handling signals,
11843 which can cause an apparent I/O error. */
11844 if (interrupt_input)
11845 unrequest_sigio ();
11846 STOP_POLLING;
11847
11848 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11849 {
11850 if (hscroll_windows (selected_window))
11851 goto retry;
11852
11853 XWINDOW (selected_window)->must_be_updated_p = 1;
11854 pause = update_frame (sf, 0, 0);
11855 }
11856
11857 /* We may have called echo_area_display at the top of this
11858 function. If the echo area is on another frame, that may
11859 have put text on a frame other than the selected one, so the
11860 above call to update_frame would not have caught it. Catch
11861 it here. */
11862 mini_window = FRAME_MINIBUF_WINDOW (sf);
11863 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11864
11865 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11866 {
11867 XWINDOW (mini_window)->must_be_updated_p = 1;
11868 pause |= update_frame (mini_frame, 0, 0);
11869 if (!pause && hscroll_windows (mini_window))
11870 goto retry;
11871 }
11872 }
11873
11874 /* If display was paused because of pending input, make sure we do a
11875 thorough update the next time. */
11876 if (pause)
11877 {
11878 /* Prevent the optimization at the beginning of
11879 redisplay_internal that tries a single-line update of the
11880 line containing the cursor in the selected window. */
11881 CHARPOS (this_line_start_pos) = 0;
11882
11883 /* Let the overlay arrow be updated the next time. */
11884 update_overlay_arrows (0);
11885
11886 /* If we pause after scrolling, some rows in the current
11887 matrices of some windows are not valid. */
11888 if (!WINDOW_FULL_WIDTH_P (w)
11889 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11890 update_mode_lines = 1;
11891 }
11892 else
11893 {
11894 if (!consider_all_windows_p)
11895 {
11896 /* This has already been done above if
11897 consider_all_windows_p is set. */
11898 mark_window_display_accurate_1 (w, 1);
11899
11900 /* Say overlay arrows are up to date. */
11901 update_overlay_arrows (1);
11902
11903 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11904 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11905 }
11906
11907 update_mode_lines = 0;
11908 windows_or_buffers_changed = 0;
11909 cursor_type_changed = 0;
11910 }
11911
11912 /* Start SIGIO interrupts coming again. Having them off during the
11913 code above makes it less likely one will discard output, but not
11914 impossible, since there might be stuff in the system buffer here.
11915 But it is much hairier to try to do anything about that. */
11916 if (interrupt_input)
11917 request_sigio ();
11918 RESUME_POLLING;
11919
11920 /* If a frame has become visible which was not before, redisplay
11921 again, so that we display it. Expose events for such a frame
11922 (which it gets when becoming visible) don't call the parts of
11923 redisplay constructing glyphs, so simply exposing a frame won't
11924 display anything in this case. So, we have to display these
11925 frames here explicitly. */
11926 if (!pause)
11927 {
11928 Lisp_Object tail, frame;
11929 int new_count = 0;
11930
11931 FOR_EACH_FRAME (tail, frame)
11932 {
11933 int this_is_visible = 0;
11934
11935 if (XFRAME (frame)->visible)
11936 this_is_visible = 1;
11937 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11938 if (XFRAME (frame)->visible)
11939 this_is_visible = 1;
11940
11941 if (this_is_visible)
11942 new_count++;
11943 }
11944
11945 if (new_count != number_of_visible_frames)
11946 windows_or_buffers_changed++;
11947 }
11948
11949 /* Change frame size now if a change is pending. */
11950 do_pending_window_change (1);
11951
11952 /* If we just did a pending size change, or have additional
11953 visible frames, redisplay again. */
11954 if (windows_or_buffers_changed && !pause)
11955 goto retry;
11956
11957 /* Clear the face cache eventually. */
11958 if (consider_all_windows_p)
11959 {
11960 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11961 {
11962 clear_face_cache (0);
11963 clear_face_cache_count = 0;
11964 }
11965 #ifdef HAVE_WINDOW_SYSTEM
11966 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11967 {
11968 clear_image_caches (Qnil);
11969 clear_image_cache_count = 0;
11970 }
11971 #endif /* HAVE_WINDOW_SYSTEM */
11972 }
11973
11974 end_of_redisplay:
11975 unbind_to (count, Qnil);
11976 RESUME_POLLING;
11977 }
11978
11979
11980 /* Redisplay, but leave alone any recent echo area message unless
11981 another message has been requested in its place.
11982
11983 This is useful in situations where you need to redisplay but no
11984 user action has occurred, making it inappropriate for the message
11985 area to be cleared. See tracking_off and
11986 wait_reading_process_output for examples of these situations.
11987
11988 FROM_WHERE is an integer saying from where this function was
11989 called. This is useful for debugging. */
11990
11991 void
11992 redisplay_preserve_echo_area (from_where)
11993 int from_where;
11994 {
11995 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11996
11997 if (!NILP (echo_area_buffer[1]))
11998 {
11999 /* We have a previously displayed message, but no current
12000 message. Redisplay the previous message. */
12001 display_last_displayed_message_p = 1;
12002 redisplay_internal (1);
12003 display_last_displayed_message_p = 0;
12004 }
12005 else
12006 redisplay_internal (1);
12007
12008 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12009 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12010 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12011 }
12012
12013
12014 /* Function registered with record_unwind_protect in
12015 redisplay_internal. Reset redisplaying_p to the value it had
12016 before redisplay_internal was called, and clear
12017 prevent_freeing_realized_faces_p. It also selects the previously
12018 selected frame, unless it has been deleted (by an X connection
12019 failure during redisplay, for example). */
12020
12021 static Lisp_Object
12022 unwind_redisplay (val)
12023 Lisp_Object val;
12024 {
12025 Lisp_Object old_redisplaying_p, old_frame;
12026
12027 old_redisplaying_p = XCAR (val);
12028 redisplaying_p = XFASTINT (old_redisplaying_p);
12029 old_frame = XCDR (val);
12030 if (! EQ (old_frame, selected_frame)
12031 && FRAME_LIVE_P (XFRAME (old_frame)))
12032 select_frame_for_redisplay (old_frame);
12033 return Qnil;
12034 }
12035
12036
12037 /* Mark the display of window W as accurate or inaccurate. If
12038 ACCURATE_P is non-zero mark display of W as accurate. If
12039 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12040 redisplay_internal is called. */
12041
12042 static void
12043 mark_window_display_accurate_1 (w, accurate_p)
12044 struct window *w;
12045 int accurate_p;
12046 {
12047 if (BUFFERP (w->buffer))
12048 {
12049 struct buffer *b = XBUFFER (w->buffer);
12050
12051 w->last_modified
12052 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12053 w->last_overlay_modified
12054 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12055 w->last_had_star
12056 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12057
12058 if (accurate_p)
12059 {
12060 b->clip_changed = 0;
12061 b->prevent_redisplay_optimizations_p = 0;
12062
12063 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12064 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12065 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12066 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12067
12068 w->current_matrix->buffer = b;
12069 w->current_matrix->begv = BUF_BEGV (b);
12070 w->current_matrix->zv = BUF_ZV (b);
12071
12072 w->last_cursor = w->cursor;
12073 w->last_cursor_off_p = w->cursor_off_p;
12074
12075 if (w == XWINDOW (selected_window))
12076 w->last_point = make_number (BUF_PT (b));
12077 else
12078 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12079 }
12080 }
12081
12082 if (accurate_p)
12083 {
12084 w->window_end_valid = w->buffer;
12085 w->update_mode_line = Qnil;
12086 }
12087 }
12088
12089
12090 /* Mark the display of windows in the window tree rooted at WINDOW as
12091 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12092 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12093 be redisplayed the next time redisplay_internal is called. */
12094
12095 void
12096 mark_window_display_accurate (window, accurate_p)
12097 Lisp_Object window;
12098 int accurate_p;
12099 {
12100 struct window *w;
12101
12102 for (; !NILP (window); window = w->next)
12103 {
12104 w = XWINDOW (window);
12105 mark_window_display_accurate_1 (w, accurate_p);
12106
12107 if (!NILP (w->vchild))
12108 mark_window_display_accurate (w->vchild, accurate_p);
12109 if (!NILP (w->hchild))
12110 mark_window_display_accurate (w->hchild, accurate_p);
12111 }
12112
12113 if (accurate_p)
12114 {
12115 update_overlay_arrows (1);
12116 }
12117 else
12118 {
12119 /* Force a thorough redisplay the next time by setting
12120 last_arrow_position and last_arrow_string to t, which is
12121 unequal to any useful value of Voverlay_arrow_... */
12122 update_overlay_arrows (-1);
12123 }
12124 }
12125
12126
12127 /* Return value in display table DP (Lisp_Char_Table *) for character
12128 C. Since a display table doesn't have any parent, we don't have to
12129 follow parent. Do not call this function directly but use the
12130 macro DISP_CHAR_VECTOR. */
12131
12132 Lisp_Object
12133 disp_char_vector (dp, c)
12134 struct Lisp_Char_Table *dp;
12135 int c;
12136 {
12137 Lisp_Object val;
12138
12139 if (ASCII_CHAR_P (c))
12140 {
12141 val = dp->ascii;
12142 if (SUB_CHAR_TABLE_P (val))
12143 val = XSUB_CHAR_TABLE (val)->contents[c];
12144 }
12145 else
12146 {
12147 Lisp_Object table;
12148
12149 XSETCHAR_TABLE (table, dp);
12150 val = char_table_ref (table, c);
12151 }
12152 if (NILP (val))
12153 val = dp->defalt;
12154 return val;
12155 }
12156
12157
12158 \f
12159 /***********************************************************************
12160 Window Redisplay
12161 ***********************************************************************/
12162
12163 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12164
12165 static void
12166 redisplay_windows (window)
12167 Lisp_Object window;
12168 {
12169 while (!NILP (window))
12170 {
12171 struct window *w = XWINDOW (window);
12172
12173 if (!NILP (w->hchild))
12174 redisplay_windows (w->hchild);
12175 else if (!NILP (w->vchild))
12176 redisplay_windows (w->vchild);
12177 else if (!NILP (w->buffer))
12178 {
12179 displayed_buffer = XBUFFER (w->buffer);
12180 /* Use list_of_error, not Qerror, so that
12181 we catch only errors and don't run the debugger. */
12182 internal_condition_case_1 (redisplay_window_0, window,
12183 list_of_error,
12184 redisplay_window_error);
12185 }
12186
12187 window = w->next;
12188 }
12189 }
12190
12191 static Lisp_Object
12192 redisplay_window_error ()
12193 {
12194 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12195 return Qnil;
12196 }
12197
12198 static Lisp_Object
12199 redisplay_window_0 (window)
12200 Lisp_Object window;
12201 {
12202 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12203 redisplay_window (window, 0);
12204 return Qnil;
12205 }
12206
12207 static Lisp_Object
12208 redisplay_window_1 (window)
12209 Lisp_Object window;
12210 {
12211 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12212 redisplay_window (window, 1);
12213 return Qnil;
12214 }
12215 \f
12216
12217 /* Increment GLYPH until it reaches END or CONDITION fails while
12218 adding (GLYPH)->pixel_width to X. */
12219
12220 #define SKIP_GLYPHS(glyph, end, x, condition) \
12221 do \
12222 { \
12223 (x) += (glyph)->pixel_width; \
12224 ++(glyph); \
12225 } \
12226 while ((glyph) < (end) && (condition))
12227
12228
12229 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12230 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12231 which positions recorded in ROW differ from current buffer
12232 positions.
12233
12234 Return 0 if cursor is not on this row, 1 otherwise. */
12235
12236 int
12237 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12238 struct window *w;
12239 struct glyph_row *row;
12240 struct glyph_matrix *matrix;
12241 int delta, delta_bytes, dy, dvpos;
12242 {
12243 struct glyph *glyph = row->glyphs[TEXT_AREA];
12244 struct glyph *end = glyph + row->used[TEXT_AREA];
12245 struct glyph *cursor = NULL;
12246 /* The first glyph that starts a sequence of glyphs from a string
12247 that is a value of a display property. */
12248 struct glyph *string_start;
12249 /* The X coordinate of string_start. */
12250 int string_start_x;
12251 /* The last known character position in row. */
12252 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12253 /* The last known character position before string_start. */
12254 int string_before_pos;
12255 int x = row->x;
12256 int cursor_x = x;
12257 /* Last buffer position covered by an overlay. */
12258 int cursor_from_overlay_pos = 0;
12259 int pt_old = PT - delta;
12260
12261 /* Skip over glyphs not having an object at the start of the row.
12262 These are special glyphs like truncation marks on terminal
12263 frames. */
12264 if (row->displays_text_p)
12265 while (glyph < end
12266 && INTEGERP (glyph->object)
12267 && glyph->charpos < 0)
12268 {
12269 x += glyph->pixel_width;
12270 ++glyph;
12271 }
12272
12273 string_start = NULL;
12274 while (glyph < end
12275 && !INTEGERP (glyph->object)
12276 && (!BUFFERP (glyph->object)
12277 || (last_pos = glyph->charpos) < pt_old
12278 || glyph->avoid_cursor_p))
12279 {
12280 if (! STRINGP (glyph->object))
12281 {
12282 string_start = NULL;
12283 x += glyph->pixel_width;
12284 ++glyph;
12285 /* If we are beyond the cursor position computed from the
12286 last overlay seen, that overlay is not in effect for
12287 current cursor position. Reset the cursor information
12288 computed from that overlay. */
12289 if (cursor_from_overlay_pos
12290 && last_pos >= cursor_from_overlay_pos)
12291 {
12292 cursor_from_overlay_pos = 0;
12293 cursor = NULL;
12294 }
12295 }
12296 else
12297 {
12298 if (string_start == NULL)
12299 {
12300 string_before_pos = last_pos;
12301 string_start = glyph;
12302 string_start_x = x;
12303 }
12304 /* Skip all glyphs from a string. */
12305 do
12306 {
12307 Lisp_Object cprop;
12308 int pos;
12309 if ((cursor == NULL || glyph > cursor)
12310 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12311 Qcursor, (glyph)->object),
12312 !NILP (cprop))
12313 && (pos = string_buffer_position (w, glyph->object,
12314 string_before_pos),
12315 (pos == 0 /* from overlay */
12316 || pos == pt_old)))
12317 {
12318 /* Compute the first buffer position after the overlay.
12319 If the `cursor' property tells us how many positions
12320 are associated with the overlay, use that. Otherwise,
12321 estimate from the buffer positions of the glyphs
12322 before and after the overlay. */
12323 cursor_from_overlay_pos = (pos ? 0 : last_pos
12324 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12325 cursor = glyph;
12326 cursor_x = x;
12327 }
12328 x += glyph->pixel_width;
12329 ++glyph;
12330 }
12331 while (glyph < end && EQ (glyph->object, string_start->object));
12332 }
12333 }
12334
12335 if (cursor != NULL)
12336 {
12337 glyph = cursor;
12338 x = cursor_x;
12339 }
12340 else if (row->ends_in_ellipsis_p && glyph == end)
12341 {
12342 /* Scan back over the ellipsis glyphs, decrementing positions. */
12343 while (glyph > row->glyphs[TEXT_AREA]
12344 && (glyph - 1)->charpos == last_pos)
12345 glyph--, x -= glyph->pixel_width;
12346 /* That loop always goes one position too far, including the
12347 glyph before the ellipsis. So scan forward over that one. */
12348 x += glyph->pixel_width;
12349 glyph++;
12350 }
12351 else if (string_start
12352 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12353 {
12354 /* We may have skipped over point because the previous glyphs
12355 are from string. As there's no easy way to know the
12356 character position of the current glyph, find the correct
12357 glyph on point by scanning from string_start again. */
12358 Lisp_Object limit;
12359 Lisp_Object string;
12360 struct glyph *stop = glyph;
12361 int pos;
12362
12363 limit = make_number (pt_old + 1);
12364 glyph = string_start;
12365 x = string_start_x;
12366 string = glyph->object;
12367 pos = string_buffer_position (w, string, string_before_pos);
12368 /* If POS == 0, STRING is from overlay. We skip such glyphs
12369 because we always put the cursor after overlay strings. */
12370 while (pos == 0 && glyph < stop)
12371 {
12372 string = glyph->object;
12373 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12374 if (glyph < stop)
12375 pos = string_buffer_position (w, glyph->object, string_before_pos);
12376 }
12377
12378 while (glyph < stop)
12379 {
12380 pos = XINT (Fnext_single_char_property_change
12381 (make_number (pos), Qdisplay, Qnil, limit));
12382 if (pos > pt_old)
12383 break;
12384 /* Skip glyphs from the same string. */
12385 string = glyph->object;
12386 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12387 /* Skip glyphs from an overlay. */
12388 while (glyph < stop
12389 && ! string_buffer_position (w, glyph->object, pos))
12390 {
12391 string = glyph->object;
12392 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12393 }
12394 }
12395
12396 /* If we reached the end of the line, and END was from a string,
12397 the cursor is not on this line. */
12398 if (glyph == end && row->continued_p)
12399 return 0;
12400 }
12401
12402 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12403 w->cursor.x = x;
12404 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12405 w->cursor.y = row->y + dy;
12406
12407 if (w == XWINDOW (selected_window))
12408 {
12409 if (!row->continued_p
12410 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12411 && row->x == 0)
12412 {
12413 this_line_buffer = XBUFFER (w->buffer);
12414
12415 CHARPOS (this_line_start_pos)
12416 = MATRIX_ROW_START_CHARPOS (row) + delta;
12417 BYTEPOS (this_line_start_pos)
12418 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12419
12420 CHARPOS (this_line_end_pos)
12421 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12422 BYTEPOS (this_line_end_pos)
12423 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12424
12425 this_line_y = w->cursor.y;
12426 this_line_pixel_height = row->height;
12427 this_line_vpos = w->cursor.vpos;
12428 this_line_start_x = row->x;
12429 }
12430 else
12431 CHARPOS (this_line_start_pos) = 0;
12432 }
12433
12434 return 1;
12435 }
12436
12437
12438 /* Run window scroll functions, if any, for WINDOW with new window
12439 start STARTP. Sets the window start of WINDOW to that position.
12440
12441 We assume that the window's buffer is really current. */
12442
12443 static INLINE struct text_pos
12444 run_window_scroll_functions (window, startp)
12445 Lisp_Object window;
12446 struct text_pos startp;
12447 {
12448 struct window *w = XWINDOW (window);
12449 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12450
12451 if (current_buffer != XBUFFER (w->buffer))
12452 abort ();
12453
12454 if (!NILP (Vwindow_scroll_functions))
12455 {
12456 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12457 make_number (CHARPOS (startp)));
12458 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12459 /* In case the hook functions switch buffers. */
12460 if (current_buffer != XBUFFER (w->buffer))
12461 set_buffer_internal_1 (XBUFFER (w->buffer));
12462 }
12463
12464 return startp;
12465 }
12466
12467
12468 /* Make sure the line containing the cursor is fully visible.
12469 A value of 1 means there is nothing to be done.
12470 (Either the line is fully visible, or it cannot be made so,
12471 or we cannot tell.)
12472
12473 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12474 is higher than window.
12475
12476 A value of 0 means the caller should do scrolling
12477 as if point had gone off the screen. */
12478
12479 static int
12480 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12481 struct window *w;
12482 int force_p;
12483 int current_matrix_p;
12484 {
12485 struct glyph_matrix *matrix;
12486 struct glyph_row *row;
12487 int window_height;
12488
12489 if (!make_cursor_line_fully_visible_p)
12490 return 1;
12491
12492 /* It's not always possible to find the cursor, e.g, when a window
12493 is full of overlay strings. Don't do anything in that case. */
12494 if (w->cursor.vpos < 0)
12495 return 1;
12496
12497 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12498 row = MATRIX_ROW (matrix, w->cursor.vpos);
12499
12500 /* If the cursor row is not partially visible, there's nothing to do. */
12501 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12502 return 1;
12503
12504 /* If the row the cursor is in is taller than the window's height,
12505 it's not clear what to do, so do nothing. */
12506 window_height = window_box_height (w);
12507 if (row->height >= window_height)
12508 {
12509 if (!force_p || MINI_WINDOW_P (w)
12510 || w->vscroll || w->cursor.vpos == 0)
12511 return 1;
12512 }
12513 return 0;
12514 }
12515
12516
12517 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12518 non-zero means only WINDOW is redisplayed in redisplay_internal.
12519 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12520 in redisplay_window to bring a partially visible line into view in
12521 the case that only the cursor has moved.
12522
12523 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12524 last screen line's vertical height extends past the end of the screen.
12525
12526 Value is
12527
12528 1 if scrolling succeeded
12529
12530 0 if scrolling didn't find point.
12531
12532 -1 if new fonts have been loaded so that we must interrupt
12533 redisplay, adjust glyph matrices, and try again. */
12534
12535 enum
12536 {
12537 SCROLLING_SUCCESS,
12538 SCROLLING_FAILED,
12539 SCROLLING_NEED_LARGER_MATRICES
12540 };
12541
12542 static int
12543 try_scrolling (window, just_this_one_p, scroll_conservatively,
12544 scroll_step, temp_scroll_step, last_line_misfit)
12545 Lisp_Object window;
12546 int just_this_one_p;
12547 EMACS_INT scroll_conservatively, scroll_step;
12548 int temp_scroll_step;
12549 int last_line_misfit;
12550 {
12551 struct window *w = XWINDOW (window);
12552 struct frame *f = XFRAME (w->frame);
12553 struct text_pos pos, startp;
12554 struct it it;
12555 int this_scroll_margin, scroll_max, rc, height;
12556 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12557 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12558 Lisp_Object aggressive;
12559 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12560
12561 #if GLYPH_DEBUG
12562 debug_method_add (w, "try_scrolling");
12563 #endif
12564
12565 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12566
12567 /* Compute scroll margin height in pixels. We scroll when point is
12568 within this distance from the top or bottom of the window. */
12569 if (scroll_margin > 0)
12570 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12571 * FRAME_LINE_HEIGHT (f);
12572 else
12573 this_scroll_margin = 0;
12574
12575 /* Force scroll_conservatively to have a reasonable value, to avoid
12576 overflow while computing how much to scroll. Note that the user
12577 can supply scroll-conservatively equal to `most-positive-fixnum',
12578 which can be larger than INT_MAX. */
12579 if (scroll_conservatively > scroll_limit)
12580 {
12581 scroll_conservatively = scroll_limit;
12582 scroll_max = INT_MAX;
12583 }
12584 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12585 /* Compute how much we should try to scroll maximally to bring
12586 point into view. */
12587 scroll_max = (max (scroll_step,
12588 max (scroll_conservatively, temp_scroll_step))
12589 * FRAME_LINE_HEIGHT (f));
12590 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12591 || NUMBERP (current_buffer->scroll_up_aggressively))
12592 /* We're trying to scroll because of aggressive scrolling but no
12593 scroll_step is set. Choose an arbitrary one. */
12594 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12595 else
12596 scroll_max = 0;
12597
12598 too_near_end:
12599
12600 /* Decide whether to scroll down. */
12601 if (PT > CHARPOS (startp))
12602 {
12603 int scroll_margin_y;
12604
12605 /* Compute the pixel ypos of the scroll margin, then move it to
12606 either that ypos or PT, whichever comes first. */
12607 start_display (&it, w, startp);
12608 scroll_margin_y = it.last_visible_y - this_scroll_margin
12609 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12610 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12611 (MOVE_TO_POS | MOVE_TO_Y));
12612
12613 if (PT > CHARPOS (it.current.pos))
12614 {
12615 int y0 = line_bottom_y (&it);
12616
12617 /* Compute the distance from the scroll margin to PT
12618 (including the height of the cursor line). Moving the
12619 iterator unconditionally to PT can be slow if PT is far
12620 away, so stop 10 lines past the window bottom (is there a
12621 way to do the right thing quickly?). */
12622 move_it_to (&it, PT, -1,
12623 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12624 -1, MOVE_TO_POS | MOVE_TO_Y);
12625 dy = line_bottom_y (&it) - y0;
12626
12627 if (dy > scroll_max)
12628 return SCROLLING_FAILED;
12629
12630 scroll_down_p = 1;
12631 }
12632 }
12633
12634 if (scroll_down_p)
12635 {
12636 /* Point is in or below the bottom scroll margin, so move the
12637 window start down. If scrolling conservatively, move it just
12638 enough down to make point visible. If scroll_step is set,
12639 move it down by scroll_step. */
12640 if (scroll_conservatively)
12641 amount_to_scroll
12642 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12643 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12644 else if (scroll_step || temp_scroll_step)
12645 amount_to_scroll = scroll_max;
12646 else
12647 {
12648 aggressive = current_buffer->scroll_up_aggressively;
12649 height = WINDOW_BOX_TEXT_HEIGHT (w);
12650 if (NUMBERP (aggressive))
12651 {
12652 double float_amount = XFLOATINT (aggressive) * height;
12653 amount_to_scroll = float_amount;
12654 if (amount_to_scroll == 0 && float_amount > 0)
12655 amount_to_scroll = 1;
12656 }
12657 }
12658
12659 if (amount_to_scroll <= 0)
12660 return SCROLLING_FAILED;
12661
12662 start_display (&it, w, startp);
12663 move_it_vertically (&it, amount_to_scroll);
12664
12665 /* If STARTP is unchanged, move it down another screen line. */
12666 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12667 move_it_by_lines (&it, 1, 1);
12668 startp = it.current.pos;
12669 }
12670 else
12671 {
12672 struct text_pos scroll_margin_pos = startp;
12673
12674 /* See if point is inside the scroll margin at the top of the
12675 window. */
12676 if (this_scroll_margin)
12677 {
12678 start_display (&it, w, startp);
12679 move_it_vertically (&it, this_scroll_margin);
12680 scroll_margin_pos = it.current.pos;
12681 }
12682
12683 if (PT < CHARPOS (scroll_margin_pos))
12684 {
12685 /* Point is in the scroll margin at the top of the window or
12686 above what is displayed in the window. */
12687 int y0;
12688
12689 /* Compute the vertical distance from PT to the scroll
12690 margin position. Give up if distance is greater than
12691 scroll_max. */
12692 SET_TEXT_POS (pos, PT, PT_BYTE);
12693 start_display (&it, w, pos);
12694 y0 = it.current_y;
12695 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12696 it.last_visible_y, -1,
12697 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12698 dy = it.current_y - y0;
12699 if (dy > scroll_max)
12700 return SCROLLING_FAILED;
12701
12702 /* Compute new window start. */
12703 start_display (&it, w, startp);
12704
12705 if (scroll_conservatively)
12706 amount_to_scroll
12707 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12708 else if (scroll_step || temp_scroll_step)
12709 amount_to_scroll = scroll_max;
12710 else
12711 {
12712 aggressive = current_buffer->scroll_down_aggressively;
12713 height = WINDOW_BOX_TEXT_HEIGHT (w);
12714 if (NUMBERP (aggressive))
12715 {
12716 double float_amount = XFLOATINT (aggressive) * height;
12717 amount_to_scroll = float_amount;
12718 if (amount_to_scroll == 0 && float_amount > 0)
12719 amount_to_scroll = 1;
12720 }
12721 }
12722
12723 if (amount_to_scroll <= 0)
12724 return SCROLLING_FAILED;
12725
12726 move_it_vertically_backward (&it, amount_to_scroll);
12727 startp = it.current.pos;
12728 }
12729 }
12730
12731 /* Run window scroll functions. */
12732 startp = run_window_scroll_functions (window, startp);
12733
12734 /* Display the window. Give up if new fonts are loaded, or if point
12735 doesn't appear. */
12736 if (!try_window (window, startp, 0))
12737 rc = SCROLLING_NEED_LARGER_MATRICES;
12738 else if (w->cursor.vpos < 0)
12739 {
12740 clear_glyph_matrix (w->desired_matrix);
12741 rc = SCROLLING_FAILED;
12742 }
12743 else
12744 {
12745 /* Maybe forget recorded base line for line number display. */
12746 if (!just_this_one_p
12747 || current_buffer->clip_changed
12748 || BEG_UNCHANGED < CHARPOS (startp))
12749 w->base_line_number = Qnil;
12750
12751 /* If cursor ends up on a partially visible line,
12752 treat that as being off the bottom of the screen. */
12753 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
12754 /* It's possible that the cursor is on the first line of the
12755 buffer, which is partially obscured due to a vscroll
12756 (Bug#7537). In that case, avoid looping forever . */
12757 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
12758 {
12759 clear_glyph_matrix (w->desired_matrix);
12760 ++extra_scroll_margin_lines;
12761 goto too_near_end;
12762 }
12763 rc = SCROLLING_SUCCESS;
12764 }
12765
12766 return rc;
12767 }
12768
12769
12770 /* Compute a suitable window start for window W if display of W starts
12771 on a continuation line. Value is non-zero if a new window start
12772 was computed.
12773
12774 The new window start will be computed, based on W's width, starting
12775 from the start of the continued line. It is the start of the
12776 screen line with the minimum distance from the old start W->start. */
12777
12778 static int
12779 compute_window_start_on_continuation_line (w)
12780 struct window *w;
12781 {
12782 struct text_pos pos, start_pos;
12783 int window_start_changed_p = 0;
12784
12785 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12786
12787 /* If window start is on a continuation line... Window start may be
12788 < BEGV in case there's invisible text at the start of the
12789 buffer (M-x rmail, for example). */
12790 if (CHARPOS (start_pos) > BEGV
12791 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12792 {
12793 struct it it;
12794 struct glyph_row *row;
12795
12796 /* Handle the case that the window start is out of range. */
12797 if (CHARPOS (start_pos) < BEGV)
12798 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12799 else if (CHARPOS (start_pos) > ZV)
12800 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12801
12802 /* Find the start of the continued line. This should be fast
12803 because scan_buffer is fast (newline cache). */
12804 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12805 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12806 row, DEFAULT_FACE_ID);
12807 reseat_at_previous_visible_line_start (&it);
12808
12809 /* If the line start is "too far" away from the window start,
12810 say it takes too much time to compute a new window start. */
12811 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12812 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12813 {
12814 int min_distance, distance;
12815
12816 /* Move forward by display lines to find the new window
12817 start. If window width was enlarged, the new start can
12818 be expected to be > the old start. If window width was
12819 decreased, the new window start will be < the old start.
12820 So, we're looking for the display line start with the
12821 minimum distance from the old window start. */
12822 pos = it.current.pos;
12823 min_distance = INFINITY;
12824 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12825 distance < min_distance)
12826 {
12827 min_distance = distance;
12828 pos = it.current.pos;
12829 move_it_by_lines (&it, 1, 0);
12830 }
12831
12832 /* Set the window start there. */
12833 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12834 window_start_changed_p = 1;
12835 }
12836 }
12837
12838 return window_start_changed_p;
12839 }
12840
12841
12842 /* Try cursor movement in case text has not changed in window WINDOW,
12843 with window start STARTP. Value is
12844
12845 CURSOR_MOVEMENT_SUCCESS if successful
12846
12847 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12848
12849 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12850 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12851 we want to scroll as if scroll-step were set to 1. See the code.
12852
12853 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12854 which case we have to abort this redisplay, and adjust matrices
12855 first. */
12856
12857 enum
12858 {
12859 CURSOR_MOVEMENT_SUCCESS,
12860 CURSOR_MOVEMENT_CANNOT_BE_USED,
12861 CURSOR_MOVEMENT_MUST_SCROLL,
12862 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12863 };
12864
12865 static int
12866 try_cursor_movement (window, startp, scroll_step)
12867 Lisp_Object window;
12868 struct text_pos startp;
12869 int *scroll_step;
12870 {
12871 struct window *w = XWINDOW (window);
12872 struct frame *f = XFRAME (w->frame);
12873 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12874
12875 #if GLYPH_DEBUG
12876 if (inhibit_try_cursor_movement)
12877 return rc;
12878 #endif
12879
12880 /* Handle case where text has not changed, only point, and it has
12881 not moved off the frame. */
12882 if (/* Point may be in this window. */
12883 PT >= CHARPOS (startp)
12884 /* Selective display hasn't changed. */
12885 && !current_buffer->clip_changed
12886 /* Function force-mode-line-update is used to force a thorough
12887 redisplay. It sets either windows_or_buffers_changed or
12888 update_mode_lines. So don't take a shortcut here for these
12889 cases. */
12890 && !update_mode_lines
12891 && !windows_or_buffers_changed
12892 && !cursor_type_changed
12893 /* Can't use this case if highlighting a region. When a
12894 region exists, cursor movement has to do more than just
12895 set the cursor. */
12896 && !(!NILP (Vtransient_mark_mode)
12897 && !NILP (current_buffer->mark_active))
12898 && NILP (w->region_showing)
12899 && NILP (Vshow_trailing_whitespace)
12900 /* Right after splitting windows, last_point may be nil. */
12901 && INTEGERP (w->last_point)
12902 /* This code is not used for mini-buffer for the sake of the case
12903 of redisplaying to replace an echo area message; since in
12904 that case the mini-buffer contents per se are usually
12905 unchanged. This code is of no real use in the mini-buffer
12906 since the handling of this_line_start_pos, etc., in redisplay
12907 handles the same cases. */
12908 && !EQ (window, minibuf_window)
12909 /* When splitting windows or for new windows, it happens that
12910 redisplay is called with a nil window_end_vpos or one being
12911 larger than the window. This should really be fixed in
12912 window.c. I don't have this on my list, now, so we do
12913 approximately the same as the old redisplay code. --gerd. */
12914 && INTEGERP (w->window_end_vpos)
12915 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12916 && (FRAME_WINDOW_P (f)
12917 || !overlay_arrow_in_current_buffer_p ()))
12918 {
12919 int this_scroll_margin, top_scroll_margin;
12920 struct glyph_row *row = NULL;
12921
12922 #if GLYPH_DEBUG
12923 debug_method_add (w, "cursor movement");
12924 #endif
12925
12926 /* Scroll if point within this distance from the top or bottom
12927 of the window. This is a pixel value. */
12928 if (scroll_margin > 0)
12929 {
12930 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12931 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12932 }
12933 else
12934 this_scroll_margin = 0;
12935
12936 top_scroll_margin = this_scroll_margin;
12937 if (WINDOW_WANTS_HEADER_LINE_P (w))
12938 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12939
12940 /* Start with the row the cursor was displayed during the last
12941 not paused redisplay. Give up if that row is not valid. */
12942 if (w->last_cursor.vpos < 0
12943 || w->last_cursor.vpos >= w->current_matrix->nrows)
12944 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12945 else
12946 {
12947 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12948 if (row->mode_line_p)
12949 ++row;
12950 if (!row->enabled_p)
12951 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12952 }
12953
12954 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12955 {
12956 int scroll_p = 0;
12957 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12958
12959 if (PT > XFASTINT (w->last_point))
12960 {
12961 /* Point has moved forward. */
12962 while (MATRIX_ROW_END_CHARPOS (row) < PT
12963 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12964 {
12965 xassert (row->enabled_p);
12966 ++row;
12967 }
12968
12969 /* The end position of a row equals the start position
12970 of the next row. If PT is there, we would rather
12971 display it in the next line. */
12972 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12973 && MATRIX_ROW_END_CHARPOS (row) == PT
12974 && !cursor_row_p (w, row))
12975 ++row;
12976
12977 /* If within the scroll margin, scroll. Note that
12978 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12979 the next line would be drawn, and that
12980 this_scroll_margin can be zero. */
12981 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12982 || PT > MATRIX_ROW_END_CHARPOS (row)
12983 /* Line is completely visible last line in window
12984 and PT is to be set in the next line. */
12985 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12986 && PT == MATRIX_ROW_END_CHARPOS (row)
12987 && !row->ends_at_zv_p
12988 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12989 scroll_p = 1;
12990 }
12991 else if (PT < XFASTINT (w->last_point))
12992 {
12993 /* Cursor has to be moved backward. Note that PT >=
12994 CHARPOS (startp) because of the outer if-statement. */
12995 while (!row->mode_line_p
12996 && (MATRIX_ROW_START_CHARPOS (row) > PT
12997 || (MATRIX_ROW_START_CHARPOS (row) == PT
12998 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12999 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13000 row > w->current_matrix->rows
13001 && (row-1)->ends_in_newline_from_string_p))))
13002 && (row->y > top_scroll_margin
13003 || CHARPOS (startp) == BEGV))
13004 {
13005 xassert (row->enabled_p);
13006 --row;
13007 }
13008
13009 /* Consider the following case: Window starts at BEGV,
13010 there is invisible, intangible text at BEGV, so that
13011 display starts at some point START > BEGV. It can
13012 happen that we are called with PT somewhere between
13013 BEGV and START. Try to handle that case. */
13014 if (row < w->current_matrix->rows
13015 || row->mode_line_p)
13016 {
13017 row = w->current_matrix->rows;
13018 if (row->mode_line_p)
13019 ++row;
13020 }
13021
13022 /* Due to newlines in overlay strings, we may have to
13023 skip forward over overlay strings. */
13024 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13025 && MATRIX_ROW_END_CHARPOS (row) == PT
13026 && !cursor_row_p (w, row))
13027 ++row;
13028
13029 /* If within the scroll margin, scroll. */
13030 if (row->y < top_scroll_margin
13031 && CHARPOS (startp) != BEGV)
13032 scroll_p = 1;
13033 }
13034 else
13035 {
13036 /* Cursor did not move. So don't scroll even if cursor line
13037 is partially visible, as it was so before. */
13038 rc = CURSOR_MOVEMENT_SUCCESS;
13039 }
13040
13041 if (PT < MATRIX_ROW_START_CHARPOS (row)
13042 || PT > MATRIX_ROW_END_CHARPOS (row))
13043 {
13044 /* if PT is not in the glyph row, give up. */
13045 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13046 }
13047 else if (rc != CURSOR_MOVEMENT_SUCCESS
13048 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13049 && make_cursor_line_fully_visible_p)
13050 {
13051 if (PT == MATRIX_ROW_END_CHARPOS (row)
13052 && !row->ends_at_zv_p
13053 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13054 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13055 else if (row->height > window_box_height (w))
13056 {
13057 /* If we end up in a partially visible line, let's
13058 make it fully visible, except when it's taller
13059 than the window, in which case we can't do much
13060 about it. */
13061 *scroll_step = 1;
13062 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13063 }
13064 else
13065 {
13066 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13067 if (!cursor_row_fully_visible_p (w, 0, 1))
13068 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13069 else
13070 rc = CURSOR_MOVEMENT_SUCCESS;
13071 }
13072 }
13073 else if (scroll_p)
13074 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13075 else
13076 {
13077 do
13078 {
13079 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13080 {
13081 rc = CURSOR_MOVEMENT_SUCCESS;
13082 break;
13083 }
13084 ++row;
13085 }
13086 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13087 && MATRIX_ROW_START_CHARPOS (row) == PT
13088 && cursor_row_p (w, row));
13089 }
13090 }
13091 }
13092
13093 return rc;
13094 }
13095
13096 void
13097 set_vertical_scroll_bar (w)
13098 struct window *w;
13099 {
13100 int start, end, whole;
13101
13102 /* Calculate the start and end positions for the current window.
13103 At some point, it would be nice to choose between scrollbars
13104 which reflect the whole buffer size, with special markers
13105 indicating narrowing, and scrollbars which reflect only the
13106 visible region.
13107
13108 Note that mini-buffers sometimes aren't displaying any text. */
13109 if (!MINI_WINDOW_P (w)
13110 || (w == XWINDOW (minibuf_window)
13111 && NILP (echo_area_buffer[0])))
13112 {
13113 struct buffer *buf = XBUFFER (w->buffer);
13114 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13115 start = marker_position (w->start) - BUF_BEGV (buf);
13116 /* I don't think this is guaranteed to be right. For the
13117 moment, we'll pretend it is. */
13118 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13119
13120 if (end < start)
13121 end = start;
13122 if (whole < (end - start))
13123 whole = end - start;
13124 }
13125 else
13126 start = end = whole = 0;
13127
13128 /* Indicate what this scroll bar ought to be displaying now. */
13129 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13130 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13131 (w, end - start, whole, start);
13132 }
13133
13134
13135 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13136 selected_window is redisplayed.
13137
13138 We can return without actually redisplaying the window if
13139 fonts_changed_p is nonzero. In that case, redisplay_internal will
13140 retry. */
13141
13142 static void
13143 redisplay_window (window, just_this_one_p)
13144 Lisp_Object window;
13145 int just_this_one_p;
13146 {
13147 struct window *w = XWINDOW (window);
13148 struct frame *f = XFRAME (w->frame);
13149 struct buffer *buffer = XBUFFER (w->buffer);
13150 struct buffer *old = current_buffer;
13151 struct text_pos lpoint, opoint, startp;
13152 int update_mode_line;
13153 int tem;
13154 struct it it;
13155 /* Record it now because it's overwritten. */
13156 int current_matrix_up_to_date_p = 0;
13157 int used_current_matrix_p = 0;
13158 /* This is less strict than current_matrix_up_to_date_p.
13159 It indictes that the buffer contents and narrowing are unchanged. */
13160 int buffer_unchanged_p = 0;
13161 int temp_scroll_step = 0;
13162 int count = SPECPDL_INDEX ();
13163 int rc;
13164 int centering_position = -1;
13165 int last_line_misfit = 0;
13166 int beg_unchanged, end_unchanged;
13167
13168 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13169 opoint = lpoint;
13170
13171 /* W must be a leaf window here. */
13172 xassert (!NILP (w->buffer));
13173 #if GLYPH_DEBUG
13174 *w->desired_matrix->method = 0;
13175 #endif
13176
13177 restart:
13178 reconsider_clip_changes (w, buffer);
13179
13180 /* Has the mode line to be updated? */
13181 update_mode_line = (!NILP (w->update_mode_line)
13182 || update_mode_lines
13183 || buffer->clip_changed
13184 || buffer->prevent_redisplay_optimizations_p);
13185
13186 if (MINI_WINDOW_P (w))
13187 {
13188 if (w == XWINDOW (echo_area_window)
13189 && !NILP (echo_area_buffer[0]))
13190 {
13191 if (update_mode_line)
13192 /* We may have to update a tty frame's menu bar or a
13193 tool-bar. Example `M-x C-h C-h C-g'. */
13194 goto finish_menu_bars;
13195 else
13196 /* We've already displayed the echo area glyphs in this window. */
13197 goto finish_scroll_bars;
13198 }
13199 else if ((w != XWINDOW (minibuf_window)
13200 || minibuf_level == 0)
13201 /* When buffer is nonempty, redisplay window normally. */
13202 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13203 /* Quail displays non-mini buffers in minibuffer window.
13204 In that case, redisplay the window normally. */
13205 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13206 {
13207 /* W is a mini-buffer window, but it's not active, so clear
13208 it. */
13209 int yb = window_text_bottom_y (w);
13210 struct glyph_row *row;
13211 int y;
13212
13213 for (y = 0, row = w->desired_matrix->rows;
13214 y < yb;
13215 y += row->height, ++row)
13216 blank_row (w, row, y);
13217 goto finish_scroll_bars;
13218 }
13219
13220 clear_glyph_matrix (w->desired_matrix);
13221 }
13222
13223 /* Otherwise set up data on this window; select its buffer and point
13224 value. */
13225 /* Really select the buffer, for the sake of buffer-local
13226 variables. */
13227 set_buffer_internal_1 (XBUFFER (w->buffer));
13228
13229 current_matrix_up_to_date_p
13230 = (!NILP (w->window_end_valid)
13231 && !current_buffer->clip_changed
13232 && !current_buffer->prevent_redisplay_optimizations_p
13233 && XFASTINT (w->last_modified) >= MODIFF
13234 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13235
13236 /* Run the window-bottom-change-functions
13237 if it is possible that the text on the screen has changed
13238 (either due to modification of the text, or any other reason). */
13239 if (!current_matrix_up_to_date_p
13240 && !NILP (Vwindow_text_change_functions))
13241 {
13242 safe_run_hooks (Qwindow_text_change_functions);
13243 goto restart;
13244 }
13245
13246 beg_unchanged = BEG_UNCHANGED;
13247 end_unchanged = END_UNCHANGED;
13248
13249 SET_TEXT_POS (opoint, PT, PT_BYTE);
13250
13251 specbind (Qinhibit_point_motion_hooks, Qt);
13252
13253 buffer_unchanged_p
13254 = (!NILP (w->window_end_valid)
13255 && !current_buffer->clip_changed
13256 && XFASTINT (w->last_modified) >= MODIFF
13257 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13258
13259 /* When windows_or_buffers_changed is non-zero, we can't rely on
13260 the window end being valid, so set it to nil there. */
13261 if (windows_or_buffers_changed)
13262 {
13263 /* If window starts on a continuation line, maybe adjust the
13264 window start in case the window's width changed. */
13265 if (XMARKER (w->start)->buffer == current_buffer)
13266 compute_window_start_on_continuation_line (w);
13267
13268 w->window_end_valid = Qnil;
13269 }
13270
13271 /* Some sanity checks. */
13272 CHECK_WINDOW_END (w);
13273 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13274 abort ();
13275 if (BYTEPOS (opoint) < CHARPOS (opoint))
13276 abort ();
13277
13278 /* If %c is in mode line, update it if needed. */
13279 if (!NILP (w->column_number_displayed)
13280 /* This alternative quickly identifies a common case
13281 where no change is needed. */
13282 && !(PT == XFASTINT (w->last_point)
13283 && XFASTINT (w->last_modified) >= MODIFF
13284 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13285 && (XFASTINT (w->column_number_displayed)
13286 != (int) current_column ())) /* iftc */
13287 update_mode_line = 1;
13288
13289 /* Count number of windows showing the selected buffer. An indirect
13290 buffer counts as its base buffer. */
13291 if (!just_this_one_p)
13292 {
13293 struct buffer *current_base, *window_base;
13294 current_base = current_buffer;
13295 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13296 if (current_base->base_buffer)
13297 current_base = current_base->base_buffer;
13298 if (window_base->base_buffer)
13299 window_base = window_base->base_buffer;
13300 if (current_base == window_base)
13301 buffer_shared++;
13302 }
13303
13304 /* Point refers normally to the selected window. For any other
13305 window, set up appropriate value. */
13306 if (!EQ (window, selected_window))
13307 {
13308 int new_pt = XMARKER (w->pointm)->charpos;
13309 int new_pt_byte = marker_byte_position (w->pointm);
13310 if (new_pt < BEGV)
13311 {
13312 new_pt = BEGV;
13313 new_pt_byte = BEGV_BYTE;
13314 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13315 }
13316 else if (new_pt > (ZV - 1))
13317 {
13318 new_pt = ZV;
13319 new_pt_byte = ZV_BYTE;
13320 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13321 }
13322
13323 /* We don't use SET_PT so that the point-motion hooks don't run. */
13324 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13325 }
13326
13327 /* If any of the character widths specified in the display table
13328 have changed, invalidate the width run cache. It's true that
13329 this may be a bit late to catch such changes, but the rest of
13330 redisplay goes (non-fatally) haywire when the display table is
13331 changed, so why should we worry about doing any better? */
13332 if (current_buffer->width_run_cache)
13333 {
13334 struct Lisp_Char_Table *disptab = buffer_display_table ();
13335
13336 if (! disptab_matches_widthtab (disptab,
13337 XVECTOR (current_buffer->width_table)))
13338 {
13339 invalidate_region_cache (current_buffer,
13340 current_buffer->width_run_cache,
13341 BEG, Z);
13342 recompute_width_table (current_buffer, disptab);
13343 }
13344 }
13345
13346 /* If window-start is screwed up, choose a new one. */
13347 if (XMARKER (w->start)->buffer != current_buffer)
13348 goto recenter;
13349
13350 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13351
13352 /* If someone specified a new starting point but did not insist,
13353 check whether it can be used. */
13354 if (!NILP (w->optional_new_start)
13355 && CHARPOS (startp) >= BEGV
13356 && CHARPOS (startp) <= ZV)
13357 {
13358 w->optional_new_start = Qnil;
13359 start_display (&it, w, startp);
13360 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13361 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13362 if (IT_CHARPOS (it) == PT)
13363 w->force_start = Qt;
13364 /* IT may overshoot PT if text at PT is invisible. */
13365 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13366 w->force_start = Qt;
13367 }
13368
13369 force_start:
13370
13371 /* Handle case where place to start displaying has been specified,
13372 unless the specified location is outside the accessible range. */
13373 if (!NILP (w->force_start)
13374 || w->frozen_window_start_p)
13375 {
13376 /* We set this later on if we have to adjust point. */
13377 int new_vpos = -1;
13378
13379 w->force_start = Qnil;
13380 w->vscroll = 0;
13381 w->window_end_valid = Qnil;
13382
13383 /* Forget any recorded base line for line number display. */
13384 if (!buffer_unchanged_p)
13385 w->base_line_number = Qnil;
13386
13387 /* Redisplay the mode line. Select the buffer properly for that.
13388 Also, run the hook window-scroll-functions
13389 because we have scrolled. */
13390 /* Note, we do this after clearing force_start because
13391 if there's an error, it is better to forget about force_start
13392 than to get into an infinite loop calling the hook functions
13393 and having them get more errors. */
13394 if (!update_mode_line
13395 || ! NILP (Vwindow_scroll_functions))
13396 {
13397 update_mode_line = 1;
13398 w->update_mode_line = Qt;
13399 startp = run_window_scroll_functions (window, startp);
13400 }
13401
13402 w->last_modified = make_number (0);
13403 w->last_overlay_modified = make_number (0);
13404 if (CHARPOS (startp) < BEGV)
13405 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13406 else if (CHARPOS (startp) > ZV)
13407 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13408
13409 /* Redisplay, then check if cursor has been set during the
13410 redisplay. Give up if new fonts were loaded. */
13411 /* We used to issue a CHECK_MARGINS argument to try_window here,
13412 but this causes scrolling to fail when point begins inside
13413 the scroll margin (bug#148) -- cyd */
13414 if (!try_window (window, startp, 0))
13415 {
13416 w->force_start = Qt;
13417 clear_glyph_matrix (w->desired_matrix);
13418 goto need_larger_matrices;
13419 }
13420
13421 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13422 {
13423 /* If point does not appear, try to move point so it does
13424 appear. The desired matrix has been built above, so we
13425 can use it here. */
13426 new_vpos = window_box_height (w) / 2;
13427 }
13428
13429 if (!cursor_row_fully_visible_p (w, 0, 0))
13430 {
13431 /* Point does appear, but on a line partly visible at end of window.
13432 Move it back to a fully-visible line. */
13433 new_vpos = window_box_height (w);
13434 }
13435
13436 /* If we need to move point for either of the above reasons,
13437 now actually do it. */
13438 if (new_vpos >= 0)
13439 {
13440 struct glyph_row *row;
13441
13442 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13443 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13444 ++row;
13445
13446 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13447 MATRIX_ROW_START_BYTEPOS (row));
13448
13449 if (w != XWINDOW (selected_window))
13450 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13451 else if (current_buffer == old)
13452 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13453
13454 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13455
13456 /* If we are highlighting the region, then we just changed
13457 the region, so redisplay to show it. */
13458 if (!NILP (Vtransient_mark_mode)
13459 && !NILP (current_buffer->mark_active))
13460 {
13461 clear_glyph_matrix (w->desired_matrix);
13462 if (!try_window (window, startp, 0))
13463 goto need_larger_matrices;
13464 }
13465 }
13466
13467 #if GLYPH_DEBUG
13468 debug_method_add (w, "forced window start");
13469 #endif
13470 goto done;
13471 }
13472
13473 /* Handle case where text has not changed, only point, and it has
13474 not moved off the frame, and we are not retrying after hscroll.
13475 (current_matrix_up_to_date_p is nonzero when retrying.) */
13476 if (current_matrix_up_to_date_p
13477 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13478 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13479 {
13480 switch (rc)
13481 {
13482 case CURSOR_MOVEMENT_SUCCESS:
13483 used_current_matrix_p = 1;
13484 goto done;
13485
13486 case CURSOR_MOVEMENT_MUST_SCROLL:
13487 goto try_to_scroll;
13488
13489 default:
13490 abort ();
13491 }
13492 }
13493 /* If current starting point was originally the beginning of a line
13494 but no longer is, find a new starting point. */
13495 else if (!NILP (w->start_at_line_beg)
13496 && !(CHARPOS (startp) <= BEGV
13497 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13498 {
13499 #if GLYPH_DEBUG
13500 debug_method_add (w, "recenter 1");
13501 #endif
13502 goto recenter;
13503 }
13504
13505 /* Try scrolling with try_window_id. Value is > 0 if update has
13506 been done, it is -1 if we know that the same window start will
13507 not work. It is 0 if unsuccessful for some other reason. */
13508 else if ((tem = try_window_id (w)) != 0)
13509 {
13510 #if GLYPH_DEBUG
13511 debug_method_add (w, "try_window_id %d", tem);
13512 #endif
13513
13514 if (fonts_changed_p)
13515 goto need_larger_matrices;
13516 if (tem > 0)
13517 goto done;
13518
13519 /* Otherwise try_window_id has returned -1 which means that we
13520 don't want the alternative below this comment to execute. */
13521 }
13522 else if (CHARPOS (startp) >= BEGV
13523 && CHARPOS (startp) <= ZV
13524 && PT >= CHARPOS (startp)
13525 && (CHARPOS (startp) < ZV
13526 /* Avoid starting at end of buffer. */
13527 || CHARPOS (startp) == BEGV
13528 || (XFASTINT (w->last_modified) >= MODIFF
13529 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13530 {
13531
13532 /* If first window line is a continuation line, and window start
13533 is inside the modified region, but the first change is before
13534 current window start, we must select a new window start.
13535
13536 However, if this is the result of a down-mouse event (e.g. by
13537 extending the mouse-drag-overlay), we don't want to select a
13538 new window start, since that would change the position under
13539 the mouse, resulting in an unwanted mouse-movement rather
13540 than a simple mouse-click. */
13541 if (NILP (w->start_at_line_beg)
13542 && NILP (do_mouse_tracking)
13543 && CHARPOS (startp) > BEGV
13544 && CHARPOS (startp) > BEG + beg_unchanged
13545 && CHARPOS (startp) <= Z - end_unchanged
13546 /* Even if w->start_at_line_beg is nil, a new window may
13547 start at a line_beg, since that's how set_buffer_window
13548 sets it. So, we need to check the return value of
13549 compute_window_start_on_continuation_line. (See also
13550 bug#197). */
13551 && XMARKER (w->start)->buffer == current_buffer
13552 && compute_window_start_on_continuation_line (w))
13553 {
13554 w->force_start = Qt;
13555 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13556 goto force_start;
13557 }
13558
13559 #if GLYPH_DEBUG
13560 debug_method_add (w, "same window start");
13561 #endif
13562
13563 /* Try to redisplay starting at same place as before.
13564 If point has not moved off frame, accept the results. */
13565 if (!current_matrix_up_to_date_p
13566 /* Don't use try_window_reusing_current_matrix in this case
13567 because a window scroll function can have changed the
13568 buffer. */
13569 || !NILP (Vwindow_scroll_functions)
13570 || MINI_WINDOW_P (w)
13571 || !(used_current_matrix_p
13572 = try_window_reusing_current_matrix (w)))
13573 {
13574 IF_DEBUG (debug_method_add (w, "1"));
13575 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
13576 /* -1 means we need to scroll.
13577 0 means we need new matrices, but fonts_changed_p
13578 is set in that case, so we will detect it below. */
13579 goto try_to_scroll;
13580 }
13581
13582 if (fonts_changed_p)
13583 goto need_larger_matrices;
13584
13585 if (w->cursor.vpos >= 0)
13586 {
13587 if (!just_this_one_p
13588 || current_buffer->clip_changed
13589 || BEG_UNCHANGED < CHARPOS (startp))
13590 /* Forget any recorded base line for line number display. */
13591 w->base_line_number = Qnil;
13592
13593 if (!cursor_row_fully_visible_p (w, 1, 0))
13594 {
13595 clear_glyph_matrix (w->desired_matrix);
13596 last_line_misfit = 1;
13597 }
13598 /* Drop through and scroll. */
13599 else
13600 goto done;
13601 }
13602 else
13603 clear_glyph_matrix (w->desired_matrix);
13604 }
13605
13606 try_to_scroll:
13607
13608 w->last_modified = make_number (0);
13609 w->last_overlay_modified = make_number (0);
13610
13611 /* Redisplay the mode line. Select the buffer properly for that. */
13612 if (!update_mode_line)
13613 {
13614 update_mode_line = 1;
13615 w->update_mode_line = Qt;
13616 }
13617
13618 /* Try to scroll by specified few lines. */
13619 if ((scroll_conservatively
13620 || scroll_step
13621 || temp_scroll_step
13622 || NUMBERP (current_buffer->scroll_up_aggressively)
13623 || NUMBERP (current_buffer->scroll_down_aggressively))
13624 && !current_buffer->clip_changed
13625 && CHARPOS (startp) >= BEGV
13626 && CHARPOS (startp) <= ZV)
13627 {
13628 /* The function returns -1 if new fonts were loaded, 1 if
13629 successful, 0 if not successful. */
13630 int rc = try_scrolling (window, just_this_one_p,
13631 scroll_conservatively,
13632 scroll_step,
13633 temp_scroll_step, last_line_misfit);
13634 switch (rc)
13635 {
13636 case SCROLLING_SUCCESS:
13637 goto done;
13638
13639 case SCROLLING_NEED_LARGER_MATRICES:
13640 goto need_larger_matrices;
13641
13642 case SCROLLING_FAILED:
13643 break;
13644
13645 default:
13646 abort ();
13647 }
13648 }
13649
13650 /* Finally, just choose place to start which centers point */
13651
13652 recenter:
13653 if (centering_position < 0)
13654 centering_position = window_box_height (w) / 2;
13655
13656 #if GLYPH_DEBUG
13657 debug_method_add (w, "recenter");
13658 #endif
13659
13660 /* w->vscroll = 0; */
13661
13662 /* Forget any previously recorded base line for line number display. */
13663 if (!buffer_unchanged_p)
13664 w->base_line_number = Qnil;
13665
13666 /* Move backward half the height of the window. */
13667 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13668 it.current_y = it.last_visible_y;
13669 move_it_vertically_backward (&it, centering_position);
13670 xassert (IT_CHARPOS (it) >= BEGV);
13671
13672 /* The function move_it_vertically_backward may move over more
13673 than the specified y-distance. If it->w is small, e.g. a
13674 mini-buffer window, we may end up in front of the window's
13675 display area. Start displaying at the start of the line
13676 containing PT in this case. */
13677 if (it.current_y <= 0)
13678 {
13679 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13680 move_it_vertically_backward (&it, 0);
13681 it.current_y = 0;
13682 }
13683
13684 it.current_x = it.hpos = 0;
13685
13686 /* Set startp here explicitly in case that helps avoid an infinite loop
13687 in case the window-scroll-functions functions get errors. */
13688 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13689
13690 /* Run scroll hooks. */
13691 startp = run_window_scroll_functions (window, it.current.pos);
13692
13693 /* Redisplay the window. */
13694 if (!current_matrix_up_to_date_p
13695 || windows_or_buffers_changed
13696 || cursor_type_changed
13697 /* Don't use try_window_reusing_current_matrix in this case
13698 because it can have changed the buffer. */
13699 || !NILP (Vwindow_scroll_functions)
13700 || !just_this_one_p
13701 || MINI_WINDOW_P (w)
13702 || !(used_current_matrix_p
13703 = try_window_reusing_current_matrix (w)))
13704 try_window (window, startp, 0);
13705
13706 /* If new fonts have been loaded (due to fontsets), give up. We
13707 have to start a new redisplay since we need to re-adjust glyph
13708 matrices. */
13709 if (fonts_changed_p)
13710 goto need_larger_matrices;
13711
13712 /* If cursor did not appear assume that the middle of the window is
13713 in the first line of the window. Do it again with the next line.
13714 (Imagine a window of height 100, displaying two lines of height
13715 60. Moving back 50 from it->last_visible_y will end in the first
13716 line.) */
13717 if (w->cursor.vpos < 0)
13718 {
13719 if (!NILP (w->window_end_valid)
13720 && PT >= Z - XFASTINT (w->window_end_pos))
13721 {
13722 clear_glyph_matrix (w->desired_matrix);
13723 move_it_by_lines (&it, 1, 0);
13724 try_window (window, it.current.pos, 0);
13725 }
13726 else if (PT < IT_CHARPOS (it))
13727 {
13728 clear_glyph_matrix (w->desired_matrix);
13729 move_it_by_lines (&it, -1, 0);
13730 try_window (window, it.current.pos, 0);
13731 }
13732 else
13733 {
13734 /* Not much we can do about it. */
13735 }
13736 }
13737
13738 /* Consider the following case: Window starts at BEGV, there is
13739 invisible, intangible text at BEGV, so that display starts at
13740 some point START > BEGV. It can happen that we are called with
13741 PT somewhere between BEGV and START. Try to handle that case. */
13742 if (w->cursor.vpos < 0)
13743 {
13744 struct glyph_row *row = w->current_matrix->rows;
13745 if (row->mode_line_p)
13746 ++row;
13747 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13748 }
13749
13750 if (!cursor_row_fully_visible_p (w, 0, 0))
13751 {
13752 /* If vscroll is enabled, disable it and try again. */
13753 if (w->vscroll)
13754 {
13755 w->vscroll = 0;
13756 clear_glyph_matrix (w->desired_matrix);
13757 goto recenter;
13758 }
13759
13760 /* If centering point failed to make the whole line visible,
13761 put point at the top instead. That has to make the whole line
13762 visible, if it can be done. */
13763 if (centering_position == 0)
13764 goto done;
13765
13766 clear_glyph_matrix (w->desired_matrix);
13767 centering_position = 0;
13768 goto recenter;
13769 }
13770
13771 done:
13772
13773 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13774 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13775 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13776 ? Qt : Qnil);
13777
13778 /* Display the mode line, if we must. */
13779 if ((update_mode_line
13780 /* If window not full width, must redo its mode line
13781 if (a) the window to its side is being redone and
13782 (b) we do a frame-based redisplay. This is a consequence
13783 of how inverted lines are drawn in frame-based redisplay. */
13784 || (!just_this_one_p
13785 && !FRAME_WINDOW_P (f)
13786 && !WINDOW_FULL_WIDTH_P (w))
13787 /* Line number to display. */
13788 || INTEGERP (w->base_line_pos)
13789 /* Column number is displayed and different from the one displayed. */
13790 || (!NILP (w->column_number_displayed)
13791 && (XFASTINT (w->column_number_displayed)
13792 != (int) current_column ()))) /* iftc */
13793 /* This means that the window has a mode line. */
13794 && (WINDOW_WANTS_MODELINE_P (w)
13795 || WINDOW_WANTS_HEADER_LINE_P (w)))
13796 {
13797 display_mode_lines (w);
13798
13799 /* If mode line height has changed, arrange for a thorough
13800 immediate redisplay using the correct mode line height. */
13801 if (WINDOW_WANTS_MODELINE_P (w)
13802 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13803 {
13804 fonts_changed_p = 1;
13805 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13806 = DESIRED_MODE_LINE_HEIGHT (w);
13807 }
13808
13809 /* If header line height has changed, arrange for a thorough
13810 immediate redisplay using the correct header line height. */
13811 if (WINDOW_WANTS_HEADER_LINE_P (w)
13812 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13813 {
13814 fonts_changed_p = 1;
13815 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13816 = DESIRED_HEADER_LINE_HEIGHT (w);
13817 }
13818
13819 if (fonts_changed_p)
13820 goto need_larger_matrices;
13821 }
13822
13823 if (!line_number_displayed
13824 && !BUFFERP (w->base_line_pos))
13825 {
13826 w->base_line_pos = Qnil;
13827 w->base_line_number = Qnil;
13828 }
13829
13830 finish_menu_bars:
13831
13832 /* When we reach a frame's selected window, redo the frame's menu bar. */
13833 if (update_mode_line
13834 && EQ (FRAME_SELECTED_WINDOW (f), window))
13835 {
13836 int redisplay_menu_p = 0;
13837 int redisplay_tool_bar_p = 0;
13838
13839 if (FRAME_WINDOW_P (f))
13840 {
13841 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13842 || defined (HAVE_NS) || defined (USE_GTK)
13843 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13844 #else
13845 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13846 #endif
13847 }
13848 else
13849 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13850
13851 if (redisplay_menu_p)
13852 display_menu_bar (w);
13853
13854 #ifdef HAVE_WINDOW_SYSTEM
13855 if (FRAME_WINDOW_P (f))
13856 {
13857 #if defined (USE_GTK) || defined (HAVE_NS)
13858 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13859 #else
13860 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13861 && (FRAME_TOOL_BAR_LINES (f) > 0
13862 || !NILP (Vauto_resize_tool_bars));
13863 #endif
13864
13865 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13866 {
13867 extern int ignore_mouse_drag_p;
13868 ignore_mouse_drag_p = 1;
13869 }
13870 }
13871 #endif
13872 }
13873
13874 #ifdef HAVE_WINDOW_SYSTEM
13875 if (FRAME_WINDOW_P (f)
13876 && update_window_fringes (w, (just_this_one_p
13877 || (!used_current_matrix_p && !overlay_arrow_seen)
13878 || w->pseudo_window_p)))
13879 {
13880 update_begin (f);
13881 BLOCK_INPUT;
13882 if (draw_window_fringes (w, 1))
13883 x_draw_vertical_border (w);
13884 UNBLOCK_INPUT;
13885 update_end (f);
13886 }
13887 #endif /* HAVE_WINDOW_SYSTEM */
13888
13889 /* We go to this label, with fonts_changed_p nonzero,
13890 if it is necessary to try again using larger glyph matrices.
13891 We have to redeem the scroll bar even in this case,
13892 because the loop in redisplay_internal expects that. */
13893 need_larger_matrices:
13894 ;
13895 finish_scroll_bars:
13896
13897 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13898 {
13899 /* Set the thumb's position and size. */
13900 set_vertical_scroll_bar (w);
13901
13902 /* Note that we actually used the scroll bar attached to this
13903 window, so it shouldn't be deleted at the end of redisplay. */
13904 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13905 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13906 }
13907
13908 /* Restore current_buffer and value of point in it. The window
13909 update may have changed the buffer, so first make sure `opoint'
13910 is still valid (Bug#6177). */
13911 if (CHARPOS (opoint) < BEGV)
13912 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13913 else if (CHARPOS (opoint) > ZV)
13914 TEMP_SET_PT_BOTH (Z, Z_BYTE);
13915 else
13916 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13917
13918 set_buffer_internal_1 (old);
13919 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13920 shorter. This can be caused by log truncation in *Messages*. */
13921 if (CHARPOS (lpoint) <= ZV)
13922 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13923
13924 unbind_to (count, Qnil);
13925 }
13926
13927
13928 /* Build the complete desired matrix of WINDOW with a window start
13929 buffer position POS.
13930
13931 Value is 1 if successful. It is zero if fonts were loaded during
13932 redisplay which makes re-adjusting glyph matrices necessary, and -1
13933 if point would appear in the scroll margins.
13934 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
13935 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
13936 set in FLAGS.) */
13937
13938 int
13939 try_window (window, pos, flags)
13940 Lisp_Object window;
13941 struct text_pos pos;
13942 int flags;
13943 {
13944 struct window *w = XWINDOW (window);
13945 struct it it;
13946 struct glyph_row *last_text_row = NULL;
13947 struct frame *f = XFRAME (w->frame);
13948
13949 /* Make POS the new window start. */
13950 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13951
13952 /* Mark cursor position as unknown. No overlay arrow seen. */
13953 w->cursor.vpos = -1;
13954 overlay_arrow_seen = 0;
13955
13956 /* Initialize iterator and info to start at POS. */
13957 start_display (&it, w, pos);
13958
13959 /* Display all lines of W. */
13960 while (it.current_y < it.last_visible_y)
13961 {
13962 if (display_line (&it))
13963 last_text_row = it.glyph_row - 1;
13964 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
13965 return 0;
13966 }
13967
13968 /* Don't let the cursor end in the scroll margins. */
13969 if ((flags & TRY_WINDOW_CHECK_MARGINS)
13970 && !MINI_WINDOW_P (w))
13971 {
13972 int this_scroll_margin;
13973
13974 if (scroll_margin > 0)
13975 {
13976 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13977 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13978 }
13979 else
13980 this_scroll_margin = 0;
13981
13982 if ((w->cursor.y >= 0 /* not vscrolled */
13983 && w->cursor.y < this_scroll_margin
13984 && CHARPOS (pos) > BEGV
13985 && IT_CHARPOS (it) < ZV)
13986 /* rms: considering make_cursor_line_fully_visible_p here
13987 seems to give wrong results. We don't want to recenter
13988 when the last line is partly visible, we want to allow
13989 that case to be handled in the usual way. */
13990 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
13991 {
13992 w->cursor.vpos = -1;
13993 clear_glyph_matrix (w->desired_matrix);
13994 return -1;
13995 }
13996 }
13997
13998 /* If bottom moved off end of frame, change mode line percentage. */
13999 if (XFASTINT (w->window_end_pos) <= 0
14000 && Z != IT_CHARPOS (it))
14001 w->update_mode_line = Qt;
14002
14003 /* Set window_end_pos to the offset of the last character displayed
14004 on the window from the end of current_buffer. Set
14005 window_end_vpos to its row number. */
14006 if (last_text_row)
14007 {
14008 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14009 w->window_end_bytepos
14010 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14011 w->window_end_pos
14012 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14013 w->window_end_vpos
14014 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14015 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14016 ->displays_text_p);
14017 }
14018 else
14019 {
14020 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14021 w->window_end_pos = make_number (Z - ZV);
14022 w->window_end_vpos = make_number (0);
14023 }
14024
14025 /* But that is not valid info until redisplay finishes. */
14026 w->window_end_valid = Qnil;
14027 return 1;
14028 }
14029
14030
14031 \f
14032 /************************************************************************
14033 Window redisplay reusing current matrix when buffer has not changed
14034 ************************************************************************/
14035
14036 /* Try redisplay of window W showing an unchanged buffer with a
14037 different window start than the last time it was displayed by
14038 reusing its current matrix. Value is non-zero if successful.
14039 W->start is the new window start. */
14040
14041 static int
14042 try_window_reusing_current_matrix (w)
14043 struct window *w;
14044 {
14045 struct frame *f = XFRAME (w->frame);
14046 struct glyph_row *row, *bottom_row;
14047 struct it it;
14048 struct run run;
14049 struct text_pos start, new_start;
14050 int nrows_scrolled, i;
14051 struct glyph_row *last_text_row;
14052 struct glyph_row *last_reused_text_row;
14053 struct glyph_row *start_row;
14054 int start_vpos, min_y, max_y;
14055
14056 #if GLYPH_DEBUG
14057 if (inhibit_try_window_reusing)
14058 return 0;
14059 #endif
14060
14061 if (/* This function doesn't handle terminal frames. */
14062 !FRAME_WINDOW_P (f)
14063 /* Don't try to reuse the display if windows have been split
14064 or such. */
14065 || windows_or_buffers_changed
14066 || cursor_type_changed)
14067 return 0;
14068
14069 /* Can't do this if region may have changed. */
14070 if ((!NILP (Vtransient_mark_mode)
14071 && !NILP (current_buffer->mark_active))
14072 || !NILP (w->region_showing)
14073 || !NILP (Vshow_trailing_whitespace))
14074 return 0;
14075
14076 /* If top-line visibility has changed, give up. */
14077 if (WINDOW_WANTS_HEADER_LINE_P (w)
14078 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14079 return 0;
14080
14081 /* Give up if old or new display is scrolled vertically. We could
14082 make this function handle this, but right now it doesn't. */
14083 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14084 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14085 return 0;
14086
14087 /* The variable new_start now holds the new window start. The old
14088 start `start' can be determined from the current matrix. */
14089 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14090 start = start_row->start.pos;
14091 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14092
14093 /* Clear the desired matrix for the display below. */
14094 clear_glyph_matrix (w->desired_matrix);
14095
14096 if (CHARPOS (new_start) <= CHARPOS (start))
14097 {
14098 int first_row_y;
14099
14100 /* Don't use this method if the display starts with an ellipsis
14101 displayed for invisible text. It's not easy to handle that case
14102 below, and it's certainly not worth the effort since this is
14103 not a frequent case. */
14104 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14105 return 0;
14106
14107 IF_DEBUG (debug_method_add (w, "twu1"));
14108
14109 /* Display up to a row that can be reused. The variable
14110 last_text_row is set to the last row displayed that displays
14111 text. Note that it.vpos == 0 if or if not there is a
14112 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14113 start_display (&it, w, new_start);
14114 first_row_y = it.current_y;
14115 w->cursor.vpos = -1;
14116 last_text_row = last_reused_text_row = NULL;
14117
14118 while (it.current_y < it.last_visible_y
14119 && !fonts_changed_p)
14120 {
14121 /* If we have reached into the characters in the START row,
14122 that means the line boundaries have changed. So we
14123 can't start copying with the row START. Maybe it will
14124 work to start copying with the following row. */
14125 while (IT_CHARPOS (it) > CHARPOS (start))
14126 {
14127 /* Advance to the next row as the "start". */
14128 start_row++;
14129 start = start_row->start.pos;
14130 /* If there are no more rows to try, or just one, give up. */
14131 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14132 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14133 || CHARPOS (start) == ZV)
14134 {
14135 clear_glyph_matrix (w->desired_matrix);
14136 return 0;
14137 }
14138
14139 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14140 }
14141 /* If we have reached alignment,
14142 we can copy the rest of the rows. */
14143 if (IT_CHARPOS (it) == CHARPOS (start))
14144 break;
14145
14146 if (display_line (&it))
14147 last_text_row = it.glyph_row - 1;
14148 }
14149
14150 /* A value of current_y < last_visible_y means that we stopped
14151 at the previous window start, which in turn means that we
14152 have at least one reusable row. */
14153 if (it.current_y < it.last_visible_y)
14154 {
14155 /* IT.vpos always starts from 0; it counts text lines. */
14156 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14157
14158 /* Find PT if not already found in the lines displayed. */
14159 if (w->cursor.vpos < 0)
14160 {
14161 int dy = it.current_y - start_row->y;
14162
14163 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14164 row = row_containing_pos (w, PT, row, NULL, dy);
14165 if (row)
14166 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14167 dy, nrows_scrolled);
14168 else
14169 {
14170 clear_glyph_matrix (w->desired_matrix);
14171 return 0;
14172 }
14173 }
14174
14175 /* Scroll the display. Do it before the current matrix is
14176 changed. The problem here is that update has not yet
14177 run, i.e. part of the current matrix is not up to date.
14178 scroll_run_hook will clear the cursor, and use the
14179 current matrix to get the height of the row the cursor is
14180 in. */
14181 run.current_y = start_row->y;
14182 run.desired_y = it.current_y;
14183 run.height = it.last_visible_y - it.current_y;
14184
14185 if (run.height > 0 && run.current_y != run.desired_y)
14186 {
14187 update_begin (f);
14188 FRAME_RIF (f)->update_window_begin_hook (w);
14189 FRAME_RIF (f)->clear_window_mouse_face (w);
14190 FRAME_RIF (f)->scroll_run_hook (w, &run);
14191 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14192 update_end (f);
14193 }
14194
14195 /* Shift current matrix down by nrows_scrolled lines. */
14196 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14197 rotate_matrix (w->current_matrix,
14198 start_vpos,
14199 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14200 nrows_scrolled);
14201
14202 /* Disable lines that must be updated. */
14203 for (i = 0; i < nrows_scrolled; ++i)
14204 (start_row + i)->enabled_p = 0;
14205
14206 /* Re-compute Y positions. */
14207 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14208 max_y = it.last_visible_y;
14209 for (row = start_row + nrows_scrolled;
14210 row < bottom_row;
14211 ++row)
14212 {
14213 row->y = it.current_y;
14214 row->visible_height = row->height;
14215
14216 if (row->y < min_y)
14217 row->visible_height -= min_y - row->y;
14218 if (row->y + row->height > max_y)
14219 row->visible_height -= row->y + row->height - max_y;
14220 row->redraw_fringe_bitmaps_p = 1;
14221
14222 it.current_y += row->height;
14223
14224 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14225 last_reused_text_row = row;
14226 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14227 break;
14228 }
14229
14230 /* Disable lines in the current matrix which are now
14231 below the window. */
14232 for (++row; row < bottom_row; ++row)
14233 row->enabled_p = row->mode_line_p = 0;
14234 }
14235
14236 /* Update window_end_pos etc.; last_reused_text_row is the last
14237 reused row from the current matrix containing text, if any.
14238 The value of last_text_row is the last displayed line
14239 containing text. */
14240 if (last_reused_text_row)
14241 {
14242 w->window_end_bytepos
14243 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14244 w->window_end_pos
14245 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14246 w->window_end_vpos
14247 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14248 w->current_matrix));
14249 }
14250 else if (last_text_row)
14251 {
14252 w->window_end_bytepos
14253 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14254 w->window_end_pos
14255 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14256 w->window_end_vpos
14257 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14258 }
14259 else
14260 {
14261 /* This window must be completely empty. */
14262 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14263 w->window_end_pos = make_number (Z - ZV);
14264 w->window_end_vpos = make_number (0);
14265 }
14266 w->window_end_valid = Qnil;
14267
14268 /* Update hint: don't try scrolling again in update_window. */
14269 w->desired_matrix->no_scrolling_p = 1;
14270
14271 #if GLYPH_DEBUG
14272 debug_method_add (w, "try_window_reusing_current_matrix 1");
14273 #endif
14274 return 1;
14275 }
14276 else if (CHARPOS (new_start) > CHARPOS (start))
14277 {
14278 struct glyph_row *pt_row, *row;
14279 struct glyph_row *first_reusable_row;
14280 struct glyph_row *first_row_to_display;
14281 int dy;
14282 int yb = window_text_bottom_y (w);
14283
14284 /* Find the row starting at new_start, if there is one. Don't
14285 reuse a partially visible line at the end. */
14286 first_reusable_row = start_row;
14287 while (first_reusable_row->enabled_p
14288 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14289 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14290 < CHARPOS (new_start)))
14291 ++first_reusable_row;
14292
14293 /* Give up if there is no row to reuse. */
14294 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14295 || !first_reusable_row->enabled_p
14296 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14297 != CHARPOS (new_start)))
14298 return 0;
14299
14300 /* We can reuse fully visible rows beginning with
14301 first_reusable_row to the end of the window. Set
14302 first_row_to_display to the first row that cannot be reused.
14303 Set pt_row to the row containing point, if there is any. */
14304 pt_row = NULL;
14305 for (first_row_to_display = first_reusable_row;
14306 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14307 ++first_row_to_display)
14308 {
14309 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14310 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14311 pt_row = first_row_to_display;
14312 }
14313
14314 /* Start displaying at the start of first_row_to_display. */
14315 xassert (first_row_to_display->y < yb);
14316 init_to_row_start (&it, w, first_row_to_display);
14317
14318 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14319 - start_vpos);
14320 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14321 - nrows_scrolled);
14322 it.current_y = (first_row_to_display->y - first_reusable_row->y
14323 + WINDOW_HEADER_LINE_HEIGHT (w));
14324
14325 /* Display lines beginning with first_row_to_display in the
14326 desired matrix. Set last_text_row to the last row displayed
14327 that displays text. */
14328 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14329 if (pt_row == NULL)
14330 w->cursor.vpos = -1;
14331 last_text_row = NULL;
14332 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14333 if (display_line (&it))
14334 last_text_row = it.glyph_row - 1;
14335
14336 /* If point is in a reused row, adjust y and vpos of the cursor
14337 position. */
14338 if (pt_row)
14339 {
14340 w->cursor.vpos -= nrows_scrolled;
14341 w->cursor.y -= first_reusable_row->y - start_row->y;
14342 }
14343
14344 /* Give up if point isn't in a row displayed or reused. (This
14345 also handles the case where w->cursor.vpos < nrows_scrolled
14346 after the calls to display_line, which can happen with scroll
14347 margins. See bug#1295.) */
14348 if (w->cursor.vpos < 0)
14349 {
14350 clear_glyph_matrix (w->desired_matrix);
14351 return 0;
14352 }
14353
14354 /* Scroll the display. */
14355 run.current_y = first_reusable_row->y;
14356 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14357 run.height = it.last_visible_y - run.current_y;
14358 dy = run.current_y - run.desired_y;
14359
14360 if (run.height)
14361 {
14362 update_begin (f);
14363 FRAME_RIF (f)->update_window_begin_hook (w);
14364 FRAME_RIF (f)->clear_window_mouse_face (w);
14365 FRAME_RIF (f)->scroll_run_hook (w, &run);
14366 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14367 update_end (f);
14368 }
14369
14370 /* Adjust Y positions of reused rows. */
14371 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14372 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14373 max_y = it.last_visible_y;
14374 for (row = first_reusable_row; row < first_row_to_display; ++row)
14375 {
14376 row->y -= dy;
14377 row->visible_height = row->height;
14378 if (row->y < min_y)
14379 row->visible_height -= min_y - row->y;
14380 if (row->y + row->height > max_y)
14381 row->visible_height -= row->y + row->height - max_y;
14382 row->redraw_fringe_bitmaps_p = 1;
14383 }
14384
14385 /* Scroll the current matrix. */
14386 xassert (nrows_scrolled > 0);
14387 rotate_matrix (w->current_matrix,
14388 start_vpos,
14389 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14390 -nrows_scrolled);
14391
14392 /* Disable rows not reused. */
14393 for (row -= nrows_scrolled; row < bottom_row; ++row)
14394 row->enabled_p = 0;
14395
14396 /* Point may have moved to a different line, so we cannot assume that
14397 the previous cursor position is valid; locate the correct row. */
14398 if (pt_row)
14399 {
14400 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14401 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14402 row++)
14403 {
14404 w->cursor.vpos++;
14405 w->cursor.y = row->y;
14406 }
14407 if (row < bottom_row)
14408 {
14409 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14410 struct glyph *end = glyph + row->used[TEXT_AREA];
14411
14412 for (; glyph < end
14413 && (!BUFFERP (glyph->object)
14414 || glyph->charpos < PT);
14415 glyph++)
14416 {
14417 w->cursor.hpos++;
14418 w->cursor.x += glyph->pixel_width;
14419 }
14420 }
14421 }
14422
14423 /* Adjust window end. A null value of last_text_row means that
14424 the window end is in reused rows which in turn means that
14425 only its vpos can have changed. */
14426 if (last_text_row)
14427 {
14428 w->window_end_bytepos
14429 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14430 w->window_end_pos
14431 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14432 w->window_end_vpos
14433 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14434 }
14435 else
14436 {
14437 w->window_end_vpos
14438 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14439 }
14440
14441 w->window_end_valid = Qnil;
14442 w->desired_matrix->no_scrolling_p = 1;
14443
14444 #if GLYPH_DEBUG
14445 debug_method_add (w, "try_window_reusing_current_matrix 2");
14446 #endif
14447 return 1;
14448 }
14449
14450 return 0;
14451 }
14452
14453
14454 \f
14455 /************************************************************************
14456 Window redisplay reusing current matrix when buffer has changed
14457 ************************************************************************/
14458
14459 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14460 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14461 int *, int *));
14462 static struct glyph_row *
14463 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14464 struct glyph_row *));
14465
14466
14467 /* Return the last row in MATRIX displaying text. If row START is
14468 non-null, start searching with that row. IT gives the dimensions
14469 of the display. Value is null if matrix is empty; otherwise it is
14470 a pointer to the row found. */
14471
14472 static struct glyph_row *
14473 find_last_row_displaying_text (matrix, it, start)
14474 struct glyph_matrix *matrix;
14475 struct it *it;
14476 struct glyph_row *start;
14477 {
14478 struct glyph_row *row, *row_found;
14479
14480 /* Set row_found to the last row in IT->w's current matrix
14481 displaying text. The loop looks funny but think of partially
14482 visible lines. */
14483 row_found = NULL;
14484 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14485 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14486 {
14487 xassert (row->enabled_p);
14488 row_found = row;
14489 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14490 break;
14491 ++row;
14492 }
14493
14494 return row_found;
14495 }
14496
14497
14498 /* Return the last row in the current matrix of W that is not affected
14499 by changes at the start of current_buffer that occurred since W's
14500 current matrix was built. Value is null if no such row exists.
14501
14502 BEG_UNCHANGED us the number of characters unchanged at the start of
14503 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14504 first changed character in current_buffer. Characters at positions <
14505 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14506 when the current matrix was built. */
14507
14508 static struct glyph_row *
14509 find_last_unchanged_at_beg_row (w)
14510 struct window *w;
14511 {
14512 int first_changed_pos = BEG + BEG_UNCHANGED;
14513 struct glyph_row *row;
14514 struct glyph_row *row_found = NULL;
14515 int yb = window_text_bottom_y (w);
14516
14517 /* Find the last row displaying unchanged text. */
14518 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14519 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14520 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14521 ++row)
14522 {
14523 if (/* If row ends before first_changed_pos, it is unchanged,
14524 except in some case. */
14525 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14526 /* When row ends in ZV and we write at ZV it is not
14527 unchanged. */
14528 && !row->ends_at_zv_p
14529 /* When first_changed_pos is the end of a continued line,
14530 row is not unchanged because it may be no longer
14531 continued. */
14532 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14533 && (row->continued_p
14534 || row->exact_window_width_line_p)))
14535 row_found = row;
14536
14537 /* Stop if last visible row. */
14538 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14539 break;
14540 }
14541
14542 return row_found;
14543 }
14544
14545
14546 /* Find the first glyph row in the current matrix of W that is not
14547 affected by changes at the end of current_buffer since the
14548 time W's current matrix was built.
14549
14550 Return in *DELTA the number of chars by which buffer positions in
14551 unchanged text at the end of current_buffer must be adjusted.
14552
14553 Return in *DELTA_BYTES the corresponding number of bytes.
14554
14555 Value is null if no such row exists, i.e. all rows are affected by
14556 changes. */
14557
14558 static struct glyph_row *
14559 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14560 struct window *w;
14561 int *delta, *delta_bytes;
14562 {
14563 struct glyph_row *row;
14564 struct glyph_row *row_found = NULL;
14565
14566 *delta = *delta_bytes = 0;
14567
14568 /* Display must not have been paused, otherwise the current matrix
14569 is not up to date. */
14570 eassert (!NILP (w->window_end_valid));
14571
14572 /* A value of window_end_pos >= END_UNCHANGED means that the window
14573 end is in the range of changed text. If so, there is no
14574 unchanged row at the end of W's current matrix. */
14575 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14576 return NULL;
14577
14578 /* Set row to the last row in W's current matrix displaying text. */
14579 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14580
14581 /* If matrix is entirely empty, no unchanged row exists. */
14582 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14583 {
14584 /* The value of row is the last glyph row in the matrix having a
14585 meaningful buffer position in it. The end position of row
14586 corresponds to window_end_pos. This allows us to translate
14587 buffer positions in the current matrix to current buffer
14588 positions for characters not in changed text. */
14589 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14590 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14591 int last_unchanged_pos, last_unchanged_pos_old;
14592 struct glyph_row *first_text_row
14593 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14594
14595 *delta = Z - Z_old;
14596 *delta_bytes = Z_BYTE - Z_BYTE_old;
14597
14598 /* Set last_unchanged_pos to the buffer position of the last
14599 character in the buffer that has not been changed. Z is the
14600 index + 1 of the last character in current_buffer, i.e. by
14601 subtracting END_UNCHANGED we get the index of the last
14602 unchanged character, and we have to add BEG to get its buffer
14603 position. */
14604 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14605 last_unchanged_pos_old = last_unchanged_pos - *delta;
14606
14607 /* Search backward from ROW for a row displaying a line that
14608 starts at a minimum position >= last_unchanged_pos_old. */
14609 for (; row > first_text_row; --row)
14610 {
14611 /* This used to abort, but it can happen.
14612 It is ok to just stop the search instead here. KFS. */
14613 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14614 break;
14615
14616 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14617 row_found = row;
14618 }
14619 }
14620
14621 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14622
14623 return row_found;
14624 }
14625
14626
14627 /* Make sure that glyph rows in the current matrix of window W
14628 reference the same glyph memory as corresponding rows in the
14629 frame's frame matrix. This function is called after scrolling W's
14630 current matrix on a terminal frame in try_window_id and
14631 try_window_reusing_current_matrix. */
14632
14633 static void
14634 sync_frame_with_window_matrix_rows (w)
14635 struct window *w;
14636 {
14637 struct frame *f = XFRAME (w->frame);
14638 struct glyph_row *window_row, *window_row_end, *frame_row;
14639
14640 /* Preconditions: W must be a leaf window and full-width. Its frame
14641 must have a frame matrix. */
14642 xassert (NILP (w->hchild) && NILP (w->vchild));
14643 xassert (WINDOW_FULL_WIDTH_P (w));
14644 xassert (!FRAME_WINDOW_P (f));
14645
14646 /* If W is a full-width window, glyph pointers in W's current matrix
14647 have, by definition, to be the same as glyph pointers in the
14648 corresponding frame matrix. Note that frame matrices have no
14649 marginal areas (see build_frame_matrix). */
14650 window_row = w->current_matrix->rows;
14651 window_row_end = window_row + w->current_matrix->nrows;
14652 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14653 while (window_row < window_row_end)
14654 {
14655 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14656 struct glyph *end = window_row->glyphs[LAST_AREA];
14657
14658 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14659 frame_row->glyphs[TEXT_AREA] = start;
14660 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14661 frame_row->glyphs[LAST_AREA] = end;
14662
14663 /* Disable frame rows whose corresponding window rows have
14664 been disabled in try_window_id. */
14665 if (!window_row->enabled_p)
14666 frame_row->enabled_p = 0;
14667
14668 ++window_row, ++frame_row;
14669 }
14670 }
14671
14672
14673 /* Find the glyph row in window W containing CHARPOS. Consider all
14674 rows between START and END (not inclusive). END null means search
14675 all rows to the end of the display area of W. Value is the row
14676 containing CHARPOS or null. */
14677
14678 struct glyph_row *
14679 row_containing_pos (w, charpos, start, end, dy)
14680 struct window *w;
14681 int charpos;
14682 struct glyph_row *start, *end;
14683 int dy;
14684 {
14685 struct glyph_row *row = start;
14686 int last_y;
14687
14688 /* If we happen to start on a header-line, skip that. */
14689 if (row->mode_line_p)
14690 ++row;
14691
14692 if ((end && row >= end) || !row->enabled_p)
14693 return NULL;
14694
14695 last_y = window_text_bottom_y (w) - dy;
14696
14697 while (1)
14698 {
14699 /* Give up if we have gone too far. */
14700 if (end && row >= end)
14701 return NULL;
14702 /* This formerly returned if they were equal.
14703 I think that both quantities are of a "last plus one" type;
14704 if so, when they are equal, the row is within the screen. -- rms. */
14705 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14706 return NULL;
14707
14708 /* If it is in this row, return this row. */
14709 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14710 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14711 /* The end position of a row equals the start
14712 position of the next row. If CHARPOS is there, we
14713 would rather display it in the next line, except
14714 when this line ends in ZV. */
14715 && !row->ends_at_zv_p
14716 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14717 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14718 return row;
14719 ++row;
14720 }
14721 }
14722
14723
14724 /* Try to redisplay window W by reusing its existing display. W's
14725 current matrix must be up to date when this function is called,
14726 i.e. window_end_valid must not be nil.
14727
14728 Value is
14729
14730 1 if display has been updated
14731 0 if otherwise unsuccessful
14732 -1 if redisplay with same window start is known not to succeed
14733
14734 The following steps are performed:
14735
14736 1. Find the last row in the current matrix of W that is not
14737 affected by changes at the start of current_buffer. If no such row
14738 is found, give up.
14739
14740 2. Find the first row in W's current matrix that is not affected by
14741 changes at the end of current_buffer. Maybe there is no such row.
14742
14743 3. Display lines beginning with the row + 1 found in step 1 to the
14744 row found in step 2 or, if step 2 didn't find a row, to the end of
14745 the window.
14746
14747 4. If cursor is not known to appear on the window, give up.
14748
14749 5. If display stopped at the row found in step 2, scroll the
14750 display and current matrix as needed.
14751
14752 6. Maybe display some lines at the end of W, if we must. This can
14753 happen under various circumstances, like a partially visible line
14754 becoming fully visible, or because newly displayed lines are displayed
14755 in smaller font sizes.
14756
14757 7. Update W's window end information. */
14758
14759 static int
14760 try_window_id (w)
14761 struct window *w;
14762 {
14763 struct frame *f = XFRAME (w->frame);
14764 struct glyph_matrix *current_matrix = w->current_matrix;
14765 struct glyph_matrix *desired_matrix = w->desired_matrix;
14766 struct glyph_row *last_unchanged_at_beg_row;
14767 struct glyph_row *first_unchanged_at_end_row;
14768 struct glyph_row *row;
14769 struct glyph_row *bottom_row;
14770 int bottom_vpos;
14771 struct it it;
14772 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14773 struct text_pos start_pos;
14774 struct run run;
14775 int first_unchanged_at_end_vpos = 0;
14776 struct glyph_row *last_text_row, *last_text_row_at_end;
14777 struct text_pos start;
14778 int first_changed_charpos, last_changed_charpos;
14779
14780 #if GLYPH_DEBUG
14781 if (inhibit_try_window_id)
14782 return 0;
14783 #endif
14784
14785 /* This is handy for debugging. */
14786 #if 0
14787 #define GIVE_UP(X) \
14788 do { \
14789 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14790 return 0; \
14791 } while (0)
14792 #else
14793 #define GIVE_UP(X) return 0
14794 #endif
14795
14796 SET_TEXT_POS_FROM_MARKER (start, w->start);
14797
14798 /* Don't use this for mini-windows because these can show
14799 messages and mini-buffers, and we don't handle that here. */
14800 if (MINI_WINDOW_P (w))
14801 GIVE_UP (1);
14802
14803 /* This flag is used to prevent redisplay optimizations. */
14804 if (windows_or_buffers_changed || cursor_type_changed)
14805 GIVE_UP (2);
14806
14807 /* Verify that narrowing has not changed.
14808 Also verify that we were not told to prevent redisplay optimizations.
14809 It would be nice to further
14810 reduce the number of cases where this prevents try_window_id. */
14811 if (current_buffer->clip_changed
14812 || current_buffer->prevent_redisplay_optimizations_p)
14813 GIVE_UP (3);
14814
14815 /* Window must either use window-based redisplay or be full width. */
14816 if (!FRAME_WINDOW_P (f)
14817 && (!FRAME_LINE_INS_DEL_OK (f)
14818 || !WINDOW_FULL_WIDTH_P (w)))
14819 GIVE_UP (4);
14820
14821 /* Give up if point is known NOT to appear in W. */
14822 if (PT < CHARPOS (start))
14823 GIVE_UP (5);
14824
14825 /* Another way to prevent redisplay optimizations. */
14826 if (XFASTINT (w->last_modified) == 0)
14827 GIVE_UP (6);
14828
14829 /* Verify that window is not hscrolled. */
14830 if (XFASTINT (w->hscroll) != 0)
14831 GIVE_UP (7);
14832
14833 /* Verify that display wasn't paused. */
14834 if (NILP (w->window_end_valid))
14835 GIVE_UP (8);
14836
14837 /* Can't use this if highlighting a region because a cursor movement
14838 will do more than just set the cursor. */
14839 if (!NILP (Vtransient_mark_mode)
14840 && !NILP (current_buffer->mark_active))
14841 GIVE_UP (9);
14842
14843 /* Likewise if highlighting trailing whitespace. */
14844 if (!NILP (Vshow_trailing_whitespace))
14845 GIVE_UP (11);
14846
14847 /* Likewise if showing a region. */
14848 if (!NILP (w->region_showing))
14849 GIVE_UP (10);
14850
14851 /* Can't use this if overlay arrow position and/or string have
14852 changed. */
14853 if (overlay_arrows_changed_p ())
14854 GIVE_UP (12);
14855
14856 /* When word-wrap is on, adding a space to the first word of a
14857 wrapped line can change the wrap position, altering the line
14858 above it. It might be worthwhile to handle this more
14859 intelligently, but for now just redisplay from scratch. */
14860 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14861 GIVE_UP (21);
14862
14863 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14864 only if buffer has really changed. The reason is that the gap is
14865 initially at Z for freshly visited files. The code below would
14866 set end_unchanged to 0 in that case. */
14867 if (MODIFF > SAVE_MODIFF
14868 /* This seems to happen sometimes after saving a buffer. */
14869 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14870 {
14871 if (GPT - BEG < BEG_UNCHANGED)
14872 BEG_UNCHANGED = GPT - BEG;
14873 if (Z - GPT < END_UNCHANGED)
14874 END_UNCHANGED = Z - GPT;
14875 }
14876
14877 /* The position of the first and last character that has been changed. */
14878 first_changed_charpos = BEG + BEG_UNCHANGED;
14879 last_changed_charpos = Z - END_UNCHANGED;
14880
14881 /* If window starts after a line end, and the last change is in
14882 front of that newline, then changes don't affect the display.
14883 This case happens with stealth-fontification. Note that although
14884 the display is unchanged, glyph positions in the matrix have to
14885 be adjusted, of course. */
14886 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14887 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14888 && ((last_changed_charpos < CHARPOS (start)
14889 && CHARPOS (start) == BEGV)
14890 || (last_changed_charpos < CHARPOS (start) - 1
14891 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14892 {
14893 int Z_old, delta, Z_BYTE_old, delta_bytes;
14894 struct glyph_row *r0;
14895
14896 /* Compute how many chars/bytes have been added to or removed
14897 from the buffer. */
14898 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14899 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14900 delta = Z - Z_old;
14901 delta_bytes = Z_BYTE - Z_BYTE_old;
14902
14903 /* Give up if PT is not in the window. Note that it already has
14904 been checked at the start of try_window_id that PT is not in
14905 front of the window start. */
14906 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14907 GIVE_UP (13);
14908
14909 /* If window start is unchanged, we can reuse the whole matrix
14910 as is, after adjusting glyph positions. No need to compute
14911 the window end again, since its offset from Z hasn't changed. */
14912 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14913 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14914 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14915 /* PT must not be in a partially visible line. */
14916 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14917 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14918 {
14919 /* Adjust positions in the glyph matrix. */
14920 if (delta || delta_bytes)
14921 {
14922 struct glyph_row *r1
14923 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14924 increment_matrix_positions (w->current_matrix,
14925 MATRIX_ROW_VPOS (r0, current_matrix),
14926 MATRIX_ROW_VPOS (r1, current_matrix),
14927 delta, delta_bytes);
14928 }
14929
14930 /* Set the cursor. */
14931 row = row_containing_pos (w, PT, r0, NULL, 0);
14932 if (row)
14933 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14934 else
14935 abort ();
14936 return 1;
14937 }
14938 }
14939
14940 /* Handle the case that changes are all below what is displayed in
14941 the window, and that PT is in the window. This shortcut cannot
14942 be taken if ZV is visible in the window, and text has been added
14943 there that is visible in the window. */
14944 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14945 /* ZV is not visible in the window, or there are no
14946 changes at ZV, actually. */
14947 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14948 || first_changed_charpos == last_changed_charpos))
14949 {
14950 struct glyph_row *r0;
14951
14952 /* Give up if PT is not in the window. Note that it already has
14953 been checked at the start of try_window_id that PT is not in
14954 front of the window start. */
14955 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14956 GIVE_UP (14);
14957
14958 /* If window start is unchanged, we can reuse the whole matrix
14959 as is, without changing glyph positions since no text has
14960 been added/removed in front of the window end. */
14961 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14962 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14963 /* PT must not be in a partially visible line. */
14964 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14965 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14966 {
14967 /* We have to compute the window end anew since text
14968 can have been added/removed after it. */
14969 w->window_end_pos
14970 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14971 w->window_end_bytepos
14972 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14973
14974 /* Set the cursor. */
14975 row = row_containing_pos (w, PT, r0, NULL, 0);
14976 if (row)
14977 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14978 else
14979 abort ();
14980 return 2;
14981 }
14982 }
14983
14984 /* Give up if window start is in the changed area.
14985
14986 The condition used to read
14987
14988 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14989
14990 but why that was tested escapes me at the moment. */
14991 if (CHARPOS (start) >= first_changed_charpos
14992 && CHARPOS (start) <= last_changed_charpos)
14993 GIVE_UP (15);
14994
14995 /* Check that window start agrees with the start of the first glyph
14996 row in its current matrix. Check this after we know the window
14997 start is not in changed text, otherwise positions would not be
14998 comparable. */
14999 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15000 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15001 GIVE_UP (16);
15002
15003 /* Give up if the window ends in strings. Overlay strings
15004 at the end are difficult to handle, so don't try. */
15005 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15006 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15007 GIVE_UP (20);
15008
15009 /* Compute the position at which we have to start displaying new
15010 lines. Some of the lines at the top of the window might be
15011 reusable because they are not displaying changed text. Find the
15012 last row in W's current matrix not affected by changes at the
15013 start of current_buffer. Value is null if changes start in the
15014 first line of window. */
15015 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15016 if (last_unchanged_at_beg_row)
15017 {
15018 /* Avoid starting to display in the moddle of a character, a TAB
15019 for instance. This is easier than to set up the iterator
15020 exactly, and it's not a frequent case, so the additional
15021 effort wouldn't really pay off. */
15022 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15023 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15024 && last_unchanged_at_beg_row > w->current_matrix->rows)
15025 --last_unchanged_at_beg_row;
15026
15027 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15028 GIVE_UP (17);
15029
15030 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15031 GIVE_UP (18);
15032 start_pos = it.current.pos;
15033
15034 /* Start displaying new lines in the desired matrix at the same
15035 vpos we would use in the current matrix, i.e. below
15036 last_unchanged_at_beg_row. */
15037 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15038 current_matrix);
15039 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15040 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15041
15042 xassert (it.hpos == 0 && it.current_x == 0);
15043 }
15044 else
15045 {
15046 /* There are no reusable lines at the start of the window.
15047 Start displaying in the first text line. */
15048 start_display (&it, w, start);
15049 it.vpos = it.first_vpos;
15050 start_pos = it.current.pos;
15051 }
15052
15053 /* Find the first row that is not affected by changes at the end of
15054 the buffer. Value will be null if there is no unchanged row, in
15055 which case we must redisplay to the end of the window. delta
15056 will be set to the value by which buffer positions beginning with
15057 first_unchanged_at_end_row have to be adjusted due to text
15058 changes. */
15059 first_unchanged_at_end_row
15060 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15061 IF_DEBUG (debug_delta = delta);
15062 IF_DEBUG (debug_delta_bytes = delta_bytes);
15063
15064 /* Set stop_pos to the buffer position up to which we will have to
15065 display new lines. If first_unchanged_at_end_row != NULL, this
15066 is the buffer position of the start of the line displayed in that
15067 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15068 that we don't stop at a buffer position. */
15069 stop_pos = 0;
15070 if (first_unchanged_at_end_row)
15071 {
15072 xassert (last_unchanged_at_beg_row == NULL
15073 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15074
15075 /* If this is a continuation line, move forward to the next one
15076 that isn't. Changes in lines above affect this line.
15077 Caution: this may move first_unchanged_at_end_row to a row
15078 not displaying text. */
15079 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15080 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15081 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15082 < it.last_visible_y))
15083 ++first_unchanged_at_end_row;
15084
15085 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15086 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15087 >= it.last_visible_y))
15088 first_unchanged_at_end_row = NULL;
15089 else
15090 {
15091 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15092 + delta);
15093 first_unchanged_at_end_vpos
15094 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15095 xassert (stop_pos >= Z - END_UNCHANGED);
15096 }
15097 }
15098 else if (last_unchanged_at_beg_row == NULL)
15099 GIVE_UP (19);
15100
15101
15102 #if GLYPH_DEBUG
15103
15104 /* Either there is no unchanged row at the end, or the one we have
15105 now displays text. This is a necessary condition for the window
15106 end pos calculation at the end of this function. */
15107 xassert (first_unchanged_at_end_row == NULL
15108 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15109
15110 debug_last_unchanged_at_beg_vpos
15111 = (last_unchanged_at_beg_row
15112 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15113 : -1);
15114 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15115
15116 #endif /* GLYPH_DEBUG != 0 */
15117
15118
15119 /* Display new lines. Set last_text_row to the last new line
15120 displayed which has text on it, i.e. might end up as being the
15121 line where the window_end_vpos is. */
15122 w->cursor.vpos = -1;
15123 last_text_row = NULL;
15124 overlay_arrow_seen = 0;
15125 while (it.current_y < it.last_visible_y
15126 && !fonts_changed_p
15127 && (first_unchanged_at_end_row == NULL
15128 || IT_CHARPOS (it) < stop_pos))
15129 {
15130 if (display_line (&it))
15131 last_text_row = it.glyph_row - 1;
15132 }
15133
15134 if (fonts_changed_p)
15135 return -1;
15136
15137
15138 /* Compute differences in buffer positions, y-positions etc. for
15139 lines reused at the bottom of the window. Compute what we can
15140 scroll. */
15141 if (first_unchanged_at_end_row
15142 /* No lines reused because we displayed everything up to the
15143 bottom of the window. */
15144 && it.current_y < it.last_visible_y)
15145 {
15146 dvpos = (it.vpos
15147 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15148 current_matrix));
15149 dy = it.current_y - first_unchanged_at_end_row->y;
15150 run.current_y = first_unchanged_at_end_row->y;
15151 run.desired_y = run.current_y + dy;
15152 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15153 }
15154 else
15155 {
15156 delta = delta_bytes = dvpos = dy
15157 = run.current_y = run.desired_y = run.height = 0;
15158 first_unchanged_at_end_row = NULL;
15159 }
15160 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15161
15162
15163 /* Find the cursor if not already found. We have to decide whether
15164 PT will appear on this window (it sometimes doesn't, but this is
15165 not a very frequent case.) This decision has to be made before
15166 the current matrix is altered. A value of cursor.vpos < 0 means
15167 that PT is either in one of the lines beginning at
15168 first_unchanged_at_end_row or below the window. Don't care for
15169 lines that might be displayed later at the window end; as
15170 mentioned, this is not a frequent case. */
15171 if (w->cursor.vpos < 0)
15172 {
15173 /* Cursor in unchanged rows at the top? */
15174 if (PT < CHARPOS (start_pos)
15175 && last_unchanged_at_beg_row)
15176 {
15177 row = row_containing_pos (w, PT,
15178 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15179 last_unchanged_at_beg_row + 1, 0);
15180 if (row)
15181 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15182 }
15183
15184 /* Start from first_unchanged_at_end_row looking for PT. */
15185 else if (first_unchanged_at_end_row)
15186 {
15187 row = row_containing_pos (w, PT - delta,
15188 first_unchanged_at_end_row, NULL, 0);
15189 if (row)
15190 set_cursor_from_row (w, row, w->current_matrix, delta,
15191 delta_bytes, dy, dvpos);
15192 }
15193
15194 /* Give up if cursor was not found. */
15195 if (w->cursor.vpos < 0)
15196 {
15197 clear_glyph_matrix (w->desired_matrix);
15198 return -1;
15199 }
15200 }
15201
15202 /* Don't let the cursor end in the scroll margins. */
15203 {
15204 int this_scroll_margin, cursor_height;
15205
15206 this_scroll_margin = max (0, scroll_margin);
15207 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15208 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15209 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15210
15211 if ((w->cursor.y < this_scroll_margin
15212 && CHARPOS (start) > BEGV)
15213 /* Old redisplay didn't take scroll margin into account at the bottom,
15214 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15215 || (w->cursor.y + (make_cursor_line_fully_visible_p
15216 ? cursor_height + this_scroll_margin
15217 : 1)) > it.last_visible_y)
15218 {
15219 w->cursor.vpos = -1;
15220 clear_glyph_matrix (w->desired_matrix);
15221 return -1;
15222 }
15223 }
15224
15225 /* Scroll the display. Do it before changing the current matrix so
15226 that xterm.c doesn't get confused about where the cursor glyph is
15227 found. */
15228 if (dy && run.height)
15229 {
15230 update_begin (f);
15231
15232 if (FRAME_WINDOW_P (f))
15233 {
15234 FRAME_RIF (f)->update_window_begin_hook (w);
15235 FRAME_RIF (f)->clear_window_mouse_face (w);
15236 FRAME_RIF (f)->scroll_run_hook (w, &run);
15237 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15238 }
15239 else
15240 {
15241 /* Terminal frame. In this case, dvpos gives the number of
15242 lines to scroll by; dvpos < 0 means scroll up. */
15243 int first_unchanged_at_end_vpos
15244 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15245 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15246 int end = (WINDOW_TOP_EDGE_LINE (w)
15247 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15248 + window_internal_height (w));
15249
15250 /* Perform the operation on the screen. */
15251 if (dvpos > 0)
15252 {
15253 /* Scroll last_unchanged_at_beg_row to the end of the
15254 window down dvpos lines. */
15255 set_terminal_window (f, end);
15256
15257 /* On dumb terminals delete dvpos lines at the end
15258 before inserting dvpos empty lines. */
15259 if (!FRAME_SCROLL_REGION_OK (f))
15260 ins_del_lines (f, end - dvpos, -dvpos);
15261
15262 /* Insert dvpos empty lines in front of
15263 last_unchanged_at_beg_row. */
15264 ins_del_lines (f, from, dvpos);
15265 }
15266 else if (dvpos < 0)
15267 {
15268 /* Scroll up last_unchanged_at_beg_vpos to the end of
15269 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15270 set_terminal_window (f, end);
15271
15272 /* Delete dvpos lines in front of
15273 last_unchanged_at_beg_vpos. ins_del_lines will set
15274 the cursor to the given vpos and emit |dvpos| delete
15275 line sequences. */
15276 ins_del_lines (f, from + dvpos, dvpos);
15277
15278 /* On a dumb terminal insert dvpos empty lines at the
15279 end. */
15280 if (!FRAME_SCROLL_REGION_OK (f))
15281 ins_del_lines (f, end + dvpos, -dvpos);
15282 }
15283
15284 set_terminal_window (f, 0);
15285 }
15286
15287 update_end (f);
15288 }
15289
15290 /* Shift reused rows of the current matrix to the right position.
15291 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15292 text. */
15293 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15294 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15295 if (dvpos < 0)
15296 {
15297 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15298 bottom_vpos, dvpos);
15299 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15300 bottom_vpos, 0);
15301 }
15302 else if (dvpos > 0)
15303 {
15304 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15305 bottom_vpos, dvpos);
15306 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15307 first_unchanged_at_end_vpos + dvpos, 0);
15308 }
15309
15310 /* For frame-based redisplay, make sure that current frame and window
15311 matrix are in sync with respect to glyph memory. */
15312 if (!FRAME_WINDOW_P (f))
15313 sync_frame_with_window_matrix_rows (w);
15314
15315 /* Adjust buffer positions in reused rows. */
15316 if (delta || delta_bytes)
15317 increment_matrix_positions (current_matrix,
15318 first_unchanged_at_end_vpos + dvpos,
15319 bottom_vpos, delta, delta_bytes);
15320
15321 /* Adjust Y positions. */
15322 if (dy)
15323 shift_glyph_matrix (w, current_matrix,
15324 first_unchanged_at_end_vpos + dvpos,
15325 bottom_vpos, dy);
15326
15327 if (first_unchanged_at_end_row)
15328 {
15329 first_unchanged_at_end_row += dvpos;
15330 if (first_unchanged_at_end_row->y >= it.last_visible_y
15331 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15332 first_unchanged_at_end_row = NULL;
15333 }
15334
15335 /* If scrolling up, there may be some lines to display at the end of
15336 the window. */
15337 last_text_row_at_end = NULL;
15338 if (dy < 0)
15339 {
15340 /* Scrolling up can leave for example a partially visible line
15341 at the end of the window to be redisplayed. */
15342 /* Set last_row to the glyph row in the current matrix where the
15343 window end line is found. It has been moved up or down in
15344 the matrix by dvpos. */
15345 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15346 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15347
15348 /* If last_row is the window end line, it should display text. */
15349 xassert (last_row->displays_text_p);
15350
15351 /* If window end line was partially visible before, begin
15352 displaying at that line. Otherwise begin displaying with the
15353 line following it. */
15354 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15355 {
15356 init_to_row_start (&it, w, last_row);
15357 it.vpos = last_vpos;
15358 it.current_y = last_row->y;
15359 }
15360 else
15361 {
15362 init_to_row_end (&it, w, last_row);
15363 it.vpos = 1 + last_vpos;
15364 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15365 ++last_row;
15366 }
15367
15368 /* We may start in a continuation line. If so, we have to
15369 get the right continuation_lines_width and current_x. */
15370 it.continuation_lines_width = last_row->continuation_lines_width;
15371 it.hpos = it.current_x = 0;
15372
15373 /* Display the rest of the lines at the window end. */
15374 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15375 while (it.current_y < it.last_visible_y
15376 && !fonts_changed_p)
15377 {
15378 /* Is it always sure that the display agrees with lines in
15379 the current matrix? I don't think so, so we mark rows
15380 displayed invalid in the current matrix by setting their
15381 enabled_p flag to zero. */
15382 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15383 if (display_line (&it))
15384 last_text_row_at_end = it.glyph_row - 1;
15385 }
15386 }
15387
15388 /* Update window_end_pos and window_end_vpos. */
15389 if (first_unchanged_at_end_row
15390 && !last_text_row_at_end)
15391 {
15392 /* Window end line if one of the preserved rows from the current
15393 matrix. Set row to the last row displaying text in current
15394 matrix starting at first_unchanged_at_end_row, after
15395 scrolling. */
15396 xassert (first_unchanged_at_end_row->displays_text_p);
15397 row = find_last_row_displaying_text (w->current_matrix, &it,
15398 first_unchanged_at_end_row);
15399 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15400
15401 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15402 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15403 w->window_end_vpos
15404 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15405 xassert (w->window_end_bytepos >= 0);
15406 IF_DEBUG (debug_method_add (w, "A"));
15407 }
15408 else if (last_text_row_at_end)
15409 {
15410 w->window_end_pos
15411 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15412 w->window_end_bytepos
15413 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15414 w->window_end_vpos
15415 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15416 xassert (w->window_end_bytepos >= 0);
15417 IF_DEBUG (debug_method_add (w, "B"));
15418 }
15419 else if (last_text_row)
15420 {
15421 /* We have displayed either to the end of the window or at the
15422 end of the window, i.e. the last row with text is to be found
15423 in the desired matrix. */
15424 w->window_end_pos
15425 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15426 w->window_end_bytepos
15427 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15428 w->window_end_vpos
15429 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15430 xassert (w->window_end_bytepos >= 0);
15431 }
15432 else if (first_unchanged_at_end_row == NULL
15433 && last_text_row == NULL
15434 && last_text_row_at_end == NULL)
15435 {
15436 /* Displayed to end of window, but no line containing text was
15437 displayed. Lines were deleted at the end of the window. */
15438 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15439 int vpos = XFASTINT (w->window_end_vpos);
15440 struct glyph_row *current_row = current_matrix->rows + vpos;
15441 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15442
15443 for (row = NULL;
15444 row == NULL && vpos >= first_vpos;
15445 --vpos, --current_row, --desired_row)
15446 {
15447 if (desired_row->enabled_p)
15448 {
15449 if (desired_row->displays_text_p)
15450 row = desired_row;
15451 }
15452 else if (current_row->displays_text_p)
15453 row = current_row;
15454 }
15455
15456 xassert (row != NULL);
15457 w->window_end_vpos = make_number (vpos + 1);
15458 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15459 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15460 xassert (w->window_end_bytepos >= 0);
15461 IF_DEBUG (debug_method_add (w, "C"));
15462 }
15463 else
15464 abort ();
15465
15466 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15467 debug_end_vpos = XFASTINT (w->window_end_vpos));
15468
15469 /* Record that display has not been completed. */
15470 w->window_end_valid = Qnil;
15471 w->desired_matrix->no_scrolling_p = 1;
15472 return 3;
15473
15474 #undef GIVE_UP
15475 }
15476
15477
15478 \f
15479 /***********************************************************************
15480 More debugging support
15481 ***********************************************************************/
15482
15483 #if GLYPH_DEBUG
15484
15485 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15486 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15487 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15488
15489
15490 /* Dump the contents of glyph matrix MATRIX on stderr.
15491
15492 GLYPHS 0 means don't show glyph contents.
15493 GLYPHS 1 means show glyphs in short form
15494 GLYPHS > 1 means show glyphs in long form. */
15495
15496 void
15497 dump_glyph_matrix (matrix, glyphs)
15498 struct glyph_matrix *matrix;
15499 int glyphs;
15500 {
15501 int i;
15502 for (i = 0; i < matrix->nrows; ++i)
15503 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15504 }
15505
15506
15507 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15508 the glyph row and area where the glyph comes from. */
15509
15510 void
15511 dump_glyph (row, glyph, area)
15512 struct glyph_row *row;
15513 struct glyph *glyph;
15514 int area;
15515 {
15516 if (glyph->type == CHAR_GLYPH)
15517 {
15518 fprintf (stderr,
15519 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15520 glyph - row->glyphs[TEXT_AREA],
15521 'C',
15522 glyph->charpos,
15523 (BUFFERP (glyph->object)
15524 ? 'B'
15525 : (STRINGP (glyph->object)
15526 ? 'S'
15527 : '-')),
15528 glyph->pixel_width,
15529 glyph->u.ch,
15530 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15531 ? glyph->u.ch
15532 : '.'),
15533 glyph->face_id,
15534 glyph->left_box_line_p,
15535 glyph->right_box_line_p);
15536 }
15537 else if (glyph->type == STRETCH_GLYPH)
15538 {
15539 fprintf (stderr,
15540 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15541 glyph - row->glyphs[TEXT_AREA],
15542 'S',
15543 glyph->charpos,
15544 (BUFFERP (glyph->object)
15545 ? 'B'
15546 : (STRINGP (glyph->object)
15547 ? 'S'
15548 : '-')),
15549 glyph->pixel_width,
15550 0,
15551 '.',
15552 glyph->face_id,
15553 glyph->left_box_line_p,
15554 glyph->right_box_line_p);
15555 }
15556 else if (glyph->type == IMAGE_GLYPH)
15557 {
15558 fprintf (stderr,
15559 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15560 glyph - row->glyphs[TEXT_AREA],
15561 'I',
15562 glyph->charpos,
15563 (BUFFERP (glyph->object)
15564 ? 'B'
15565 : (STRINGP (glyph->object)
15566 ? 'S'
15567 : '-')),
15568 glyph->pixel_width,
15569 glyph->u.img_id,
15570 '.',
15571 glyph->face_id,
15572 glyph->left_box_line_p,
15573 glyph->right_box_line_p);
15574 }
15575 else if (glyph->type == COMPOSITE_GLYPH)
15576 {
15577 fprintf (stderr,
15578 " %5d %4c %6d %c %3d 0x%05x",
15579 glyph - row->glyphs[TEXT_AREA],
15580 '+',
15581 glyph->charpos,
15582 (BUFFERP (glyph->object)
15583 ? 'B'
15584 : (STRINGP (glyph->object)
15585 ? 'S'
15586 : '-')),
15587 glyph->pixel_width,
15588 glyph->u.cmp.id);
15589 if (glyph->u.cmp.automatic)
15590 fprintf (stderr,
15591 "[%d-%d]",
15592 glyph->u.cmp.from, glyph->u.cmp.to);
15593 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15594 glyph->face_id,
15595 glyph->left_box_line_p,
15596 glyph->right_box_line_p);
15597 }
15598 }
15599
15600
15601 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15602 GLYPHS 0 means don't show glyph contents.
15603 GLYPHS 1 means show glyphs in short form
15604 GLYPHS > 1 means show glyphs in long form. */
15605
15606 void
15607 dump_glyph_row (row, vpos, glyphs)
15608 struct glyph_row *row;
15609 int vpos, glyphs;
15610 {
15611 if (glyphs != 1)
15612 {
15613 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15614 fprintf (stderr, "======================================================================\n");
15615
15616 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15617 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15618 vpos,
15619 MATRIX_ROW_START_CHARPOS (row),
15620 MATRIX_ROW_END_CHARPOS (row),
15621 row->used[TEXT_AREA],
15622 row->contains_overlapping_glyphs_p,
15623 row->enabled_p,
15624 row->truncated_on_left_p,
15625 row->truncated_on_right_p,
15626 row->continued_p,
15627 MATRIX_ROW_CONTINUATION_LINE_P (row),
15628 row->displays_text_p,
15629 row->ends_at_zv_p,
15630 row->fill_line_p,
15631 row->ends_in_middle_of_char_p,
15632 row->starts_in_middle_of_char_p,
15633 row->mouse_face_p,
15634 row->x,
15635 row->y,
15636 row->pixel_width,
15637 row->height,
15638 row->visible_height,
15639 row->ascent,
15640 row->phys_ascent);
15641 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15642 row->end.overlay_string_index,
15643 row->continuation_lines_width);
15644 fprintf (stderr, "%9d %5d\n",
15645 CHARPOS (row->start.string_pos),
15646 CHARPOS (row->end.string_pos));
15647 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15648 row->end.dpvec_index);
15649 }
15650
15651 if (glyphs > 1)
15652 {
15653 int area;
15654
15655 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15656 {
15657 struct glyph *glyph = row->glyphs[area];
15658 struct glyph *glyph_end = glyph + row->used[area];
15659
15660 /* Glyph for a line end in text. */
15661 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15662 ++glyph_end;
15663
15664 if (glyph < glyph_end)
15665 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15666
15667 for (; glyph < glyph_end; ++glyph)
15668 dump_glyph (row, glyph, area);
15669 }
15670 }
15671 else if (glyphs == 1)
15672 {
15673 int area;
15674
15675 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15676 {
15677 char *s = (char *) alloca (row->used[area] + 1);
15678 int i;
15679
15680 for (i = 0; i < row->used[area]; ++i)
15681 {
15682 struct glyph *glyph = row->glyphs[area] + i;
15683 if (glyph->type == CHAR_GLYPH
15684 && glyph->u.ch < 0x80
15685 && glyph->u.ch >= ' ')
15686 s[i] = glyph->u.ch;
15687 else
15688 s[i] = '.';
15689 }
15690
15691 s[i] = '\0';
15692 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15693 }
15694 }
15695 }
15696
15697
15698 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15699 Sdump_glyph_matrix, 0, 1, "p",
15700 doc: /* Dump the current matrix of the selected window to stderr.
15701 Shows contents of glyph row structures. With non-nil
15702 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15703 glyphs in short form, otherwise show glyphs in long form. */)
15704 (glyphs)
15705 Lisp_Object glyphs;
15706 {
15707 struct window *w = XWINDOW (selected_window);
15708 struct buffer *buffer = XBUFFER (w->buffer);
15709
15710 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15711 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15712 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15713 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15714 fprintf (stderr, "=============================================\n");
15715 dump_glyph_matrix (w->current_matrix,
15716 NILP (glyphs) ? 0 : XINT (glyphs));
15717 return Qnil;
15718 }
15719
15720
15721 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15722 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15723 ()
15724 {
15725 struct frame *f = XFRAME (selected_frame);
15726 dump_glyph_matrix (f->current_matrix, 1);
15727 return Qnil;
15728 }
15729
15730
15731 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15732 doc: /* Dump glyph row ROW to stderr.
15733 GLYPH 0 means don't dump glyphs.
15734 GLYPH 1 means dump glyphs in short form.
15735 GLYPH > 1 or omitted means dump glyphs in long form. */)
15736 (row, glyphs)
15737 Lisp_Object row, glyphs;
15738 {
15739 struct glyph_matrix *matrix;
15740 int vpos;
15741
15742 CHECK_NUMBER (row);
15743 matrix = XWINDOW (selected_window)->current_matrix;
15744 vpos = XINT (row);
15745 if (vpos >= 0 && vpos < matrix->nrows)
15746 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15747 vpos,
15748 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15749 return Qnil;
15750 }
15751
15752
15753 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15754 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15755 GLYPH 0 means don't dump glyphs.
15756 GLYPH 1 means dump glyphs in short form.
15757 GLYPH > 1 or omitted means dump glyphs in long form. */)
15758 (row, glyphs)
15759 Lisp_Object row, glyphs;
15760 {
15761 struct frame *sf = SELECTED_FRAME ();
15762 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15763 int vpos;
15764
15765 CHECK_NUMBER (row);
15766 vpos = XINT (row);
15767 if (vpos >= 0 && vpos < m->nrows)
15768 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15769 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15770 return Qnil;
15771 }
15772
15773
15774 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15775 doc: /* Toggle tracing of redisplay.
15776 With ARG, turn tracing on if and only if ARG is positive. */)
15777 (arg)
15778 Lisp_Object arg;
15779 {
15780 if (NILP (arg))
15781 trace_redisplay_p = !trace_redisplay_p;
15782 else
15783 {
15784 arg = Fprefix_numeric_value (arg);
15785 trace_redisplay_p = XINT (arg) > 0;
15786 }
15787
15788 return Qnil;
15789 }
15790
15791
15792 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15793 doc: /* Like `format', but print result to stderr.
15794 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15795 (nargs, args)
15796 int nargs;
15797 Lisp_Object *args;
15798 {
15799 Lisp_Object s = Fformat (nargs, args);
15800 fprintf (stderr, "%s", SDATA (s));
15801 return Qnil;
15802 }
15803
15804 #endif /* GLYPH_DEBUG */
15805
15806
15807 \f
15808 /***********************************************************************
15809 Building Desired Matrix Rows
15810 ***********************************************************************/
15811
15812 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15813 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15814
15815 static struct glyph_row *
15816 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15817 struct window *w;
15818 Lisp_Object overlay_arrow_string;
15819 {
15820 struct frame *f = XFRAME (WINDOW_FRAME (w));
15821 struct buffer *buffer = XBUFFER (w->buffer);
15822 struct buffer *old = current_buffer;
15823 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15824 int arrow_len = SCHARS (overlay_arrow_string);
15825 const unsigned char *arrow_end = arrow_string + arrow_len;
15826 const unsigned char *p;
15827 struct it it;
15828 int multibyte_p;
15829 int n_glyphs_before;
15830
15831 set_buffer_temp (buffer);
15832 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15833 it.glyph_row->used[TEXT_AREA] = 0;
15834 SET_TEXT_POS (it.position, 0, 0);
15835
15836 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15837 p = arrow_string;
15838 while (p < arrow_end)
15839 {
15840 Lisp_Object face, ilisp;
15841
15842 /* Get the next character. */
15843 if (multibyte_p)
15844 it.c = it.char_to_display = string_char_and_length (p, &it.len);
15845 else
15846 {
15847 it.c = it.char_to_display = *p, it.len = 1;
15848 if (! ASCII_CHAR_P (it.c))
15849 it.char_to_display = BYTE8_TO_CHAR (it.c);
15850 }
15851 p += it.len;
15852
15853 /* Get its face. */
15854 ilisp = make_number (p - arrow_string);
15855 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15856 it.face_id = compute_char_face (f, it.char_to_display, face);
15857
15858 /* Compute its width, get its glyphs. */
15859 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15860 SET_TEXT_POS (it.position, -1, -1);
15861 PRODUCE_GLYPHS (&it);
15862
15863 /* If this character doesn't fit any more in the line, we have
15864 to remove some glyphs. */
15865 if (it.current_x > it.last_visible_x)
15866 {
15867 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15868 break;
15869 }
15870 }
15871
15872 set_buffer_temp (old);
15873 return it.glyph_row;
15874 }
15875
15876
15877 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15878 glyphs are only inserted for terminal frames since we can't really
15879 win with truncation glyphs when partially visible glyphs are
15880 involved. Which glyphs to insert is determined by
15881 produce_special_glyphs. */
15882
15883 static void
15884 insert_left_trunc_glyphs (it)
15885 struct it *it;
15886 {
15887 struct it truncate_it;
15888 struct glyph *from, *end, *to, *toend;
15889
15890 xassert (!FRAME_WINDOW_P (it->f));
15891
15892 /* Get the truncation glyphs. */
15893 truncate_it = *it;
15894 truncate_it.current_x = 0;
15895 truncate_it.face_id = DEFAULT_FACE_ID;
15896 truncate_it.glyph_row = &scratch_glyph_row;
15897 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15898 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15899 truncate_it.object = make_number (0);
15900 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15901
15902 /* Overwrite glyphs from IT with truncation glyphs. */
15903 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15904 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15905 to = it->glyph_row->glyphs[TEXT_AREA];
15906 toend = to + it->glyph_row->used[TEXT_AREA];
15907
15908 while (from < end)
15909 *to++ = *from++;
15910
15911 /* There may be padding glyphs left over. Overwrite them too. */
15912 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15913 {
15914 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15915 while (from < end)
15916 *to++ = *from++;
15917 }
15918
15919 if (to > toend)
15920 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15921 }
15922
15923
15924 /* Compute the pixel height and width of IT->glyph_row.
15925
15926 Most of the time, ascent and height of a display line will be equal
15927 to the max_ascent and max_height values of the display iterator
15928 structure. This is not the case if
15929
15930 1. We hit ZV without displaying anything. In this case, max_ascent
15931 and max_height will be zero.
15932
15933 2. We have some glyphs that don't contribute to the line height.
15934 (The glyph row flag contributes_to_line_height_p is for future
15935 pixmap extensions).
15936
15937 The first case is easily covered by using default values because in
15938 these cases, the line height does not really matter, except that it
15939 must not be zero. */
15940
15941 static void
15942 compute_line_metrics (it)
15943 struct it *it;
15944 {
15945 struct glyph_row *row = it->glyph_row;
15946 int area, i;
15947
15948 if (FRAME_WINDOW_P (it->f))
15949 {
15950 int i, min_y, max_y;
15951
15952 /* The line may consist of one space only, that was added to
15953 place the cursor on it. If so, the row's height hasn't been
15954 computed yet. */
15955 if (row->height == 0)
15956 {
15957 if (it->max_ascent + it->max_descent == 0)
15958 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15959 row->ascent = it->max_ascent;
15960 row->height = it->max_ascent + it->max_descent;
15961 row->phys_ascent = it->max_phys_ascent;
15962 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15963 row->extra_line_spacing = it->max_extra_line_spacing;
15964 }
15965
15966 /* Compute the width of this line. */
15967 row->pixel_width = row->x;
15968 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15969 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15970
15971 xassert (row->pixel_width >= 0);
15972 xassert (row->ascent >= 0 && row->height > 0);
15973
15974 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15975 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15976
15977 /* If first line's physical ascent is larger than its logical
15978 ascent, use the physical ascent, and make the row taller.
15979 This makes accented characters fully visible. */
15980 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15981 && row->phys_ascent > row->ascent)
15982 {
15983 row->height += row->phys_ascent - row->ascent;
15984 row->ascent = row->phys_ascent;
15985 }
15986
15987 /* Compute how much of the line is visible. */
15988 row->visible_height = row->height;
15989
15990 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15991 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15992
15993 if (row->y < min_y)
15994 row->visible_height -= min_y - row->y;
15995 if (row->y + row->height > max_y)
15996 row->visible_height -= row->y + row->height - max_y;
15997 }
15998 else
15999 {
16000 row->pixel_width = row->used[TEXT_AREA];
16001 if (row->continued_p)
16002 row->pixel_width -= it->continuation_pixel_width;
16003 else if (row->truncated_on_right_p)
16004 row->pixel_width -= it->truncation_pixel_width;
16005 row->ascent = row->phys_ascent = 0;
16006 row->height = row->phys_height = row->visible_height = 1;
16007 row->extra_line_spacing = 0;
16008 }
16009
16010 /* Compute a hash code for this row. */
16011 row->hash = 0;
16012 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16013 for (i = 0; i < row->used[area]; ++i)
16014 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16015 + row->glyphs[area][i].u.val
16016 + row->glyphs[area][i].face_id
16017 + row->glyphs[area][i].padding_p
16018 + (row->glyphs[area][i].type << 2));
16019
16020 it->max_ascent = it->max_descent = 0;
16021 it->max_phys_ascent = it->max_phys_descent = 0;
16022 }
16023
16024
16025 /* Append one space to the glyph row of iterator IT if doing a
16026 window-based redisplay. The space has the same face as
16027 IT->face_id. Value is non-zero if a space was added.
16028
16029 This function is called to make sure that there is always one glyph
16030 at the end of a glyph row that the cursor can be set on under
16031 window-systems. (If there weren't such a glyph we would not know
16032 how wide and tall a box cursor should be displayed).
16033
16034 At the same time this space let's a nicely handle clearing to the
16035 end of the line if the row ends in italic text. */
16036
16037 static int
16038 append_space_for_newline (it, default_face_p)
16039 struct it *it;
16040 int default_face_p;
16041 {
16042 if (FRAME_WINDOW_P (it->f))
16043 {
16044 int n = it->glyph_row->used[TEXT_AREA];
16045
16046 if (it->glyph_row->glyphs[TEXT_AREA] + n
16047 < it->glyph_row->glyphs[1 + TEXT_AREA])
16048 {
16049 /* Save some values that must not be changed.
16050 Must save IT->c and IT->len because otherwise
16051 ITERATOR_AT_END_P wouldn't work anymore after
16052 append_space_for_newline has been called. */
16053 enum display_element_type saved_what = it->what;
16054 int saved_c = it->c, saved_len = it->len;
16055 int saved_char_to_display = it->char_to_display;
16056 int saved_x = it->current_x;
16057 int saved_face_id = it->face_id;
16058 struct text_pos saved_pos;
16059 Lisp_Object saved_object;
16060 struct face *face;
16061
16062 saved_object = it->object;
16063 saved_pos = it->position;
16064
16065 it->what = IT_CHARACTER;
16066 bzero (&it->position, sizeof it->position);
16067 it->object = make_number (0);
16068 it->c = it->char_to_display = ' ';
16069 it->len = 1;
16070
16071 if (default_face_p)
16072 it->face_id = DEFAULT_FACE_ID;
16073 else if (it->face_before_selective_p)
16074 it->face_id = it->saved_face_id;
16075 face = FACE_FROM_ID (it->f, it->face_id);
16076 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16077
16078 PRODUCE_GLYPHS (it);
16079
16080 it->override_ascent = -1;
16081 it->constrain_row_ascent_descent_p = 0;
16082 it->current_x = saved_x;
16083 it->object = saved_object;
16084 it->position = saved_pos;
16085 it->what = saved_what;
16086 it->face_id = saved_face_id;
16087 it->len = saved_len;
16088 it->c = saved_c;
16089 it->char_to_display = saved_char_to_display;
16090 return 1;
16091 }
16092 }
16093
16094 return 0;
16095 }
16096
16097
16098 /* Extend the face of the last glyph in the text area of IT->glyph_row
16099 to the end of the display line. Called from display_line.
16100 If the glyph row is empty, add a space glyph to it so that we
16101 know the face to draw. Set the glyph row flag fill_line_p. */
16102
16103 static void
16104 extend_face_to_end_of_line (it)
16105 struct it *it;
16106 {
16107 struct face *face;
16108 struct frame *f = it->f;
16109
16110 /* If line is already filled, do nothing. */
16111 if (it->current_x >= it->last_visible_x)
16112 return;
16113
16114 /* Face extension extends the background and box of IT->face_id
16115 to the end of the line. If the background equals the background
16116 of the frame, we don't have to do anything. */
16117 if (it->face_before_selective_p)
16118 face = FACE_FROM_ID (it->f, it->saved_face_id);
16119 else
16120 face = FACE_FROM_ID (f, it->face_id);
16121
16122 if (FRAME_WINDOW_P (f)
16123 && it->glyph_row->displays_text_p
16124 && face->box == FACE_NO_BOX
16125 && face->background == FRAME_BACKGROUND_PIXEL (f)
16126 && !face->stipple)
16127 return;
16128
16129 /* Set the glyph row flag indicating that the face of the last glyph
16130 in the text area has to be drawn to the end of the text area. */
16131 it->glyph_row->fill_line_p = 1;
16132
16133 /* If current character of IT is not ASCII, make sure we have the
16134 ASCII face. This will be automatically undone the next time
16135 get_next_display_element returns a multibyte character. Note
16136 that the character will always be single byte in unibyte
16137 text. */
16138 if (!ASCII_CHAR_P (it->c))
16139 {
16140 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16141 }
16142
16143 if (FRAME_WINDOW_P (f))
16144 {
16145 /* If the row is empty, add a space with the current face of IT,
16146 so that we know which face to draw. */
16147 if (it->glyph_row->used[TEXT_AREA] == 0)
16148 {
16149 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16150 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16151 it->glyph_row->used[TEXT_AREA] = 1;
16152 }
16153 }
16154 else
16155 {
16156 /* Save some values that must not be changed. */
16157 int saved_x = it->current_x;
16158 struct text_pos saved_pos;
16159 Lisp_Object saved_object;
16160 enum display_element_type saved_what = it->what;
16161 int saved_face_id = it->face_id;
16162
16163 saved_object = it->object;
16164 saved_pos = it->position;
16165
16166 it->what = IT_CHARACTER;
16167 bzero (&it->position, sizeof it->position);
16168 it->object = make_number (0);
16169 it->c = it->char_to_display = ' ';
16170 it->len = 1;
16171 it->face_id = face->id;
16172
16173 PRODUCE_GLYPHS (it);
16174
16175 while (it->current_x <= it->last_visible_x)
16176 PRODUCE_GLYPHS (it);
16177
16178 /* Don't count these blanks really. It would let us insert a left
16179 truncation glyph below and make us set the cursor on them, maybe. */
16180 it->current_x = saved_x;
16181 it->object = saved_object;
16182 it->position = saved_pos;
16183 it->what = saved_what;
16184 it->face_id = saved_face_id;
16185 }
16186 }
16187
16188
16189 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16190 trailing whitespace. */
16191
16192 static int
16193 trailing_whitespace_p (charpos)
16194 int charpos;
16195 {
16196 int bytepos = CHAR_TO_BYTE (charpos);
16197 int c = 0;
16198
16199 while (bytepos < ZV_BYTE
16200 && (c = FETCH_CHAR (bytepos),
16201 c == ' ' || c == '\t'))
16202 ++bytepos;
16203
16204 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16205 {
16206 if (bytepos != PT_BYTE)
16207 return 1;
16208 }
16209 return 0;
16210 }
16211
16212
16213 /* Highlight trailing whitespace, if any, in ROW. */
16214
16215 void
16216 highlight_trailing_whitespace (f, row)
16217 struct frame *f;
16218 struct glyph_row *row;
16219 {
16220 int used = row->used[TEXT_AREA];
16221
16222 if (used)
16223 {
16224 struct glyph *start = row->glyphs[TEXT_AREA];
16225 struct glyph *glyph = start + used - 1;
16226
16227 /* Skip over glyphs inserted to display the cursor at the
16228 end of a line, for extending the face of the last glyph
16229 to the end of the line on terminals, and for truncation
16230 and continuation glyphs. */
16231 while (glyph >= start
16232 && glyph->type == CHAR_GLYPH
16233 && INTEGERP (glyph->object))
16234 --glyph;
16235
16236 /* If last glyph is a space or stretch, and it's trailing
16237 whitespace, set the face of all trailing whitespace glyphs in
16238 IT->glyph_row to `trailing-whitespace'. */
16239 if (glyph >= start
16240 && BUFFERP (glyph->object)
16241 && (glyph->type == STRETCH_GLYPH
16242 || (glyph->type == CHAR_GLYPH
16243 && glyph->u.ch == ' '))
16244 && trailing_whitespace_p (glyph->charpos))
16245 {
16246 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16247 if (face_id < 0)
16248 return;
16249
16250 while (glyph >= start
16251 && BUFFERP (glyph->object)
16252 && (glyph->type == STRETCH_GLYPH
16253 || (glyph->type == CHAR_GLYPH
16254 && glyph->u.ch == ' ')))
16255 (glyph--)->face_id = face_id;
16256 }
16257 }
16258 }
16259
16260
16261 /* Value is non-zero if glyph row ROW in window W should be
16262 used to hold the cursor. */
16263
16264 static int
16265 cursor_row_p (w, row)
16266 struct window *w;
16267 struct glyph_row *row;
16268 {
16269 int cursor_row_p = 1;
16270
16271 if (PT == MATRIX_ROW_END_CHARPOS (row))
16272 {
16273 /* Suppose the row ends on a string.
16274 Unless the row is continued, that means it ends on a newline
16275 in the string. If it's anything other than a display string
16276 (e.g. a before-string from an overlay), we don't want the
16277 cursor there. (This heuristic seems to give the optimal
16278 behavior for the various types of multi-line strings.) */
16279 if (CHARPOS (row->end.string_pos) >= 0)
16280 {
16281 if (row->continued_p)
16282 cursor_row_p = 1;
16283 else
16284 {
16285 /* Check for `display' property. */
16286 struct glyph *beg = row->glyphs[TEXT_AREA];
16287 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16288 struct glyph *glyph;
16289
16290 cursor_row_p = 0;
16291 for (glyph = end; glyph >= beg; --glyph)
16292 if (STRINGP (glyph->object))
16293 {
16294 Lisp_Object prop
16295 = Fget_char_property (make_number (PT),
16296 Qdisplay, Qnil);
16297 cursor_row_p =
16298 (!NILP (prop)
16299 && display_prop_string_p (prop, glyph->object));
16300 break;
16301 }
16302 }
16303 }
16304 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16305 {
16306 /* If the row ends in middle of a real character,
16307 and the line is continued, we want the cursor here.
16308 That's because MATRIX_ROW_END_CHARPOS would equal
16309 PT if PT is before the character. */
16310 if (!row->ends_in_ellipsis_p)
16311 cursor_row_p = row->continued_p;
16312 else
16313 /* If the row ends in an ellipsis, then
16314 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16315 We want that position to be displayed after the ellipsis. */
16316 cursor_row_p = 0;
16317 }
16318 /* If the row ends at ZV, display the cursor at the end of that
16319 row instead of at the start of the row below. */
16320 else if (row->ends_at_zv_p)
16321 cursor_row_p = 1;
16322 else
16323 cursor_row_p = 0;
16324 }
16325
16326 return cursor_row_p;
16327 }
16328
16329 \f
16330
16331 /* Push the display property PROP so that it will be rendered at the
16332 current position in IT. Return 1 if PROP was successfully pushed,
16333 0 otherwise. */
16334
16335 static int
16336 push_display_prop (struct it *it, Lisp_Object prop)
16337 {
16338 push_it (it);
16339
16340 if (STRINGP (prop))
16341 {
16342 if (SCHARS (prop) == 0)
16343 {
16344 pop_it (it);
16345 return 0;
16346 }
16347
16348 it->string = prop;
16349 it->multibyte_p = STRING_MULTIBYTE (it->string);
16350 it->current.overlay_string_index = -1;
16351 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16352 it->end_charpos = it->string_nchars = SCHARS (it->string);
16353 it->method = GET_FROM_STRING;
16354 it->stop_charpos = 0;
16355 }
16356 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16357 {
16358 it->method = GET_FROM_STRETCH;
16359 it->object = prop;
16360 }
16361 #ifdef HAVE_WINDOW_SYSTEM
16362 else if (IMAGEP (prop))
16363 {
16364 it->what = IT_IMAGE;
16365 it->image_id = lookup_image (it->f, prop);
16366 it->method = GET_FROM_IMAGE;
16367 }
16368 #endif /* HAVE_WINDOW_SYSTEM */
16369 else
16370 {
16371 pop_it (it); /* bogus display property, give up */
16372 return 0;
16373 }
16374
16375 return 1;
16376 }
16377
16378 /* Return the character-property PROP at the current position in IT. */
16379
16380 static Lisp_Object
16381 get_it_property (it, prop)
16382 struct it *it;
16383 Lisp_Object prop;
16384 {
16385 Lisp_Object position;
16386
16387 if (STRINGP (it->object))
16388 position = make_number (IT_STRING_CHARPOS (*it));
16389 else if (BUFFERP (it->object))
16390 position = make_number (IT_CHARPOS (*it));
16391 else
16392 return Qnil;
16393
16394 return Fget_char_property (position, prop, it->object);
16395 }
16396
16397 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16398
16399 static void
16400 handle_line_prefix (struct it *it)
16401 {
16402 Lisp_Object prefix;
16403 if (it->continuation_lines_width > 0)
16404 {
16405 prefix = get_it_property (it, Qwrap_prefix);
16406 if (NILP (prefix))
16407 prefix = Vwrap_prefix;
16408 }
16409 else
16410 {
16411 prefix = get_it_property (it, Qline_prefix);
16412 if (NILP (prefix))
16413 prefix = Vline_prefix;
16414 }
16415 if (! NILP (prefix) && push_display_prop (it, prefix))
16416 {
16417 /* If the prefix is wider than the window, and we try to wrap
16418 it, it would acquire its own wrap prefix, and so on till the
16419 iterator stack overflows. So, don't wrap the prefix. */
16420 it->line_wrap = TRUNCATE;
16421 it->avoid_cursor_p = 1;
16422 }
16423 }
16424
16425 \f
16426
16427 /* Construct the glyph row IT->glyph_row in the desired matrix of
16428 IT->w from text at the current position of IT. See dispextern.h
16429 for an overview of struct it. Value is non-zero if
16430 IT->glyph_row displays text, as opposed to a line displaying ZV
16431 only. */
16432
16433 static int
16434 display_line (it)
16435 struct it *it;
16436 {
16437 struct glyph_row *row = it->glyph_row;
16438 Lisp_Object overlay_arrow_string;
16439 struct it wrap_it;
16440 int may_wrap = 0, wrap_x;
16441 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16442 int wrap_row_phys_ascent, wrap_row_phys_height;
16443 int wrap_row_extra_line_spacing;
16444
16445 /* We always start displaying at hpos zero even if hscrolled. */
16446 xassert (it->hpos == 0 && it->current_x == 0);
16447
16448 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16449 >= it->w->desired_matrix->nrows)
16450 {
16451 it->w->nrows_scale_factor++;
16452 fonts_changed_p = 1;
16453 return 0;
16454 }
16455
16456 /* Is IT->w showing the region? */
16457 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16458
16459 /* Clear the result glyph row and enable it. */
16460 prepare_desired_row (row);
16461
16462 row->y = it->current_y;
16463 row->start = it->start;
16464 row->continuation_lines_width = it->continuation_lines_width;
16465 row->displays_text_p = 1;
16466 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16467 it->starts_in_middle_of_char_p = 0;
16468
16469 /* Arrange the overlays nicely for our purposes. Usually, we call
16470 display_line on only one line at a time, in which case this
16471 can't really hurt too much, or we call it on lines which appear
16472 one after another in the buffer, in which case all calls to
16473 recenter_overlay_lists but the first will be pretty cheap. */
16474 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16475
16476 /* Move over display elements that are not visible because we are
16477 hscrolled. This may stop at an x-position < IT->first_visible_x
16478 if the first glyph is partially visible or if we hit a line end. */
16479 if (it->current_x < it->first_visible_x)
16480 {
16481 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16482 MOVE_TO_POS | MOVE_TO_X);
16483 }
16484 else
16485 {
16486 /* We only do this when not calling `move_it_in_display_line_to'
16487 above, because move_it_in_display_line_to calls
16488 handle_line_prefix itself. */
16489 handle_line_prefix (it);
16490 }
16491
16492 /* Get the initial row height. This is either the height of the
16493 text hscrolled, if there is any, or zero. */
16494 row->ascent = it->max_ascent;
16495 row->height = it->max_ascent + it->max_descent;
16496 row->phys_ascent = it->max_phys_ascent;
16497 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16498 row->extra_line_spacing = it->max_extra_line_spacing;
16499
16500 /* Loop generating characters. The loop is left with IT on the next
16501 character to display. */
16502 while (1)
16503 {
16504 int n_glyphs_before, hpos_before, x_before;
16505 int x, i, nglyphs;
16506 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16507
16508 /* Retrieve the next thing to display. Value is zero if end of
16509 buffer reached. */
16510 if (!get_next_display_element (it))
16511 {
16512 /* Maybe add a space at the end of this line that is used to
16513 display the cursor there under X. Set the charpos of the
16514 first glyph of blank lines not corresponding to any text
16515 to -1. */
16516 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16517 row->exact_window_width_line_p = 1;
16518 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16519 || row->used[TEXT_AREA] == 0)
16520 {
16521 row->glyphs[TEXT_AREA]->charpos = -1;
16522 row->displays_text_p = 0;
16523
16524 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16525 && (!MINI_WINDOW_P (it->w)
16526 || (minibuf_level && EQ (it->window, minibuf_window))))
16527 row->indicate_empty_line_p = 1;
16528 }
16529
16530 it->continuation_lines_width = 0;
16531 row->ends_at_zv_p = 1;
16532 break;
16533 }
16534
16535 /* Now, get the metrics of what we want to display. This also
16536 generates glyphs in `row' (which is IT->glyph_row). */
16537 n_glyphs_before = row->used[TEXT_AREA];
16538 x = it->current_x;
16539
16540 /* Remember the line height so far in case the next element doesn't
16541 fit on the line. */
16542 if (it->line_wrap != TRUNCATE)
16543 {
16544 ascent = it->max_ascent;
16545 descent = it->max_descent;
16546 phys_ascent = it->max_phys_ascent;
16547 phys_descent = it->max_phys_descent;
16548
16549 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16550 {
16551 if (IT_DISPLAYING_WHITESPACE (it))
16552 may_wrap = 1;
16553 else if (may_wrap)
16554 {
16555 wrap_it = *it;
16556 wrap_x = x;
16557 wrap_row_used = row->used[TEXT_AREA];
16558 wrap_row_ascent = row->ascent;
16559 wrap_row_height = row->height;
16560 wrap_row_phys_ascent = row->phys_ascent;
16561 wrap_row_phys_height = row->phys_height;
16562 wrap_row_extra_line_spacing = row->extra_line_spacing;
16563 may_wrap = 0;
16564 }
16565 }
16566 }
16567
16568 PRODUCE_GLYPHS (it);
16569
16570 /* If this display element was in marginal areas, continue with
16571 the next one. */
16572 if (it->area != TEXT_AREA)
16573 {
16574 row->ascent = max (row->ascent, it->max_ascent);
16575 row->height = max (row->height, it->max_ascent + it->max_descent);
16576 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16577 row->phys_height = max (row->phys_height,
16578 it->max_phys_ascent + it->max_phys_descent);
16579 row->extra_line_spacing = max (row->extra_line_spacing,
16580 it->max_extra_line_spacing);
16581 set_iterator_to_next (it, 1);
16582 continue;
16583 }
16584
16585 /* Does the display element fit on the line? If we truncate
16586 lines, we should draw past the right edge of the window. If
16587 we don't truncate, we want to stop so that we can display the
16588 continuation glyph before the right margin. If lines are
16589 continued, there are two possible strategies for characters
16590 resulting in more than 1 glyph (e.g. tabs): Display as many
16591 glyphs as possible in this line and leave the rest for the
16592 continuation line, or display the whole element in the next
16593 line. Original redisplay did the former, so we do it also. */
16594 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16595 hpos_before = it->hpos;
16596 x_before = x;
16597
16598 if (/* Not a newline. */
16599 nglyphs > 0
16600 /* Glyphs produced fit entirely in the line. */
16601 && it->current_x < it->last_visible_x)
16602 {
16603 it->hpos += nglyphs;
16604 row->ascent = max (row->ascent, it->max_ascent);
16605 row->height = max (row->height, it->max_ascent + it->max_descent);
16606 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16607 row->phys_height = max (row->phys_height,
16608 it->max_phys_ascent + it->max_phys_descent);
16609 row->extra_line_spacing = max (row->extra_line_spacing,
16610 it->max_extra_line_spacing);
16611 if (it->current_x - it->pixel_width < it->first_visible_x)
16612 row->x = x - it->first_visible_x;
16613 }
16614 else
16615 {
16616 int new_x;
16617 struct glyph *glyph;
16618
16619 for (i = 0; i < nglyphs; ++i, x = new_x)
16620 {
16621 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16622 new_x = x + glyph->pixel_width;
16623
16624 if (/* Lines are continued. */
16625 it->line_wrap != TRUNCATE
16626 && (/* Glyph doesn't fit on the line. */
16627 new_x > it->last_visible_x
16628 /* Or it fits exactly on a window system frame. */
16629 || (new_x == it->last_visible_x
16630 && FRAME_WINDOW_P (it->f))))
16631 {
16632 /* End of a continued line. */
16633
16634 if (it->hpos == 0
16635 || (new_x == it->last_visible_x
16636 && FRAME_WINDOW_P (it->f)))
16637 {
16638 /* Current glyph is the only one on the line or
16639 fits exactly on the line. We must continue
16640 the line because we can't draw the cursor
16641 after the glyph. */
16642 row->continued_p = 1;
16643 it->current_x = new_x;
16644 it->continuation_lines_width += new_x;
16645 ++it->hpos;
16646 if (i == nglyphs - 1)
16647 {
16648 /* If line-wrap is on, check if a previous
16649 wrap point was found. */
16650 if (wrap_row_used > 0
16651 /* Even if there is a previous wrap
16652 point, continue the line here as
16653 usual, if (i) the previous character
16654 was a space or tab AND (ii) the
16655 current character is not. */
16656 && (!may_wrap
16657 || IT_DISPLAYING_WHITESPACE (it)))
16658 goto back_to_wrap;
16659
16660 set_iterator_to_next (it, 1);
16661 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16662 {
16663 if (!get_next_display_element (it))
16664 {
16665 row->exact_window_width_line_p = 1;
16666 it->continuation_lines_width = 0;
16667 row->continued_p = 0;
16668 row->ends_at_zv_p = 1;
16669 }
16670 else if (ITERATOR_AT_END_OF_LINE_P (it))
16671 {
16672 row->continued_p = 0;
16673 row->exact_window_width_line_p = 1;
16674 }
16675 }
16676 }
16677 }
16678 else if (CHAR_GLYPH_PADDING_P (*glyph)
16679 && !FRAME_WINDOW_P (it->f))
16680 {
16681 /* A padding glyph that doesn't fit on this line.
16682 This means the whole character doesn't fit
16683 on the line. */
16684 row->used[TEXT_AREA] = n_glyphs_before;
16685
16686 /* Fill the rest of the row with continuation
16687 glyphs like in 20.x. */
16688 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16689 < row->glyphs[1 + TEXT_AREA])
16690 produce_special_glyphs (it, IT_CONTINUATION);
16691
16692 row->continued_p = 1;
16693 it->current_x = x_before;
16694 it->continuation_lines_width += x_before;
16695
16696 /* Restore the height to what it was before the
16697 element not fitting on the line. */
16698 it->max_ascent = ascent;
16699 it->max_descent = descent;
16700 it->max_phys_ascent = phys_ascent;
16701 it->max_phys_descent = phys_descent;
16702 }
16703 else if (wrap_row_used > 0)
16704 {
16705 back_to_wrap:
16706 *it = wrap_it;
16707 it->continuation_lines_width += wrap_x;
16708 row->used[TEXT_AREA] = wrap_row_used;
16709 row->ascent = wrap_row_ascent;
16710 row->height = wrap_row_height;
16711 row->phys_ascent = wrap_row_phys_ascent;
16712 row->phys_height = wrap_row_phys_height;
16713 row->extra_line_spacing = wrap_row_extra_line_spacing;
16714 row->continued_p = 1;
16715 row->ends_at_zv_p = 0;
16716 row->exact_window_width_line_p = 0;
16717 it->continuation_lines_width += x;
16718
16719 /* Make sure that a non-default face is extended
16720 up to the right margin of the window. */
16721 extend_face_to_end_of_line (it);
16722 }
16723 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16724 {
16725 /* A TAB that extends past the right edge of the
16726 window. This produces a single glyph on
16727 window system frames. We leave the glyph in
16728 this row and let it fill the row, but don't
16729 consume the TAB. */
16730 it->continuation_lines_width += it->last_visible_x;
16731 row->ends_in_middle_of_char_p = 1;
16732 row->continued_p = 1;
16733 glyph->pixel_width = it->last_visible_x - x;
16734 it->starts_in_middle_of_char_p = 1;
16735 }
16736 else
16737 {
16738 /* Something other than a TAB that draws past
16739 the right edge of the window. Restore
16740 positions to values before the element. */
16741 row->used[TEXT_AREA] = n_glyphs_before + i;
16742
16743 /* Display continuation glyphs. */
16744 if (!FRAME_WINDOW_P (it->f))
16745 produce_special_glyphs (it, IT_CONTINUATION);
16746 row->continued_p = 1;
16747
16748 it->current_x = x_before;
16749 it->continuation_lines_width += x;
16750 extend_face_to_end_of_line (it);
16751
16752 if (nglyphs > 1 && i > 0)
16753 {
16754 row->ends_in_middle_of_char_p = 1;
16755 it->starts_in_middle_of_char_p = 1;
16756 }
16757
16758 /* Restore the height to what it was before the
16759 element not fitting on the line. */
16760 it->max_ascent = ascent;
16761 it->max_descent = descent;
16762 it->max_phys_ascent = phys_ascent;
16763 it->max_phys_descent = phys_descent;
16764 }
16765
16766 break;
16767 }
16768 else if (new_x > it->first_visible_x)
16769 {
16770 /* Increment number of glyphs actually displayed. */
16771 ++it->hpos;
16772
16773 if (x < it->first_visible_x)
16774 /* Glyph is partially visible, i.e. row starts at
16775 negative X position. */
16776 row->x = x - it->first_visible_x;
16777 }
16778 else
16779 {
16780 /* Glyph is completely off the left margin of the
16781 window. This should not happen because of the
16782 move_it_in_display_line at the start of this
16783 function, unless the text display area of the
16784 window is empty. */
16785 xassert (it->first_visible_x <= it->last_visible_x);
16786 }
16787 }
16788
16789 row->ascent = max (row->ascent, it->max_ascent);
16790 row->height = max (row->height, it->max_ascent + it->max_descent);
16791 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16792 row->phys_height = max (row->phys_height,
16793 it->max_phys_ascent + it->max_phys_descent);
16794 row->extra_line_spacing = max (row->extra_line_spacing,
16795 it->max_extra_line_spacing);
16796
16797 /* End of this display line if row is continued. */
16798 if (row->continued_p || row->ends_at_zv_p)
16799 break;
16800 }
16801
16802 at_end_of_line:
16803 /* Is this a line end? If yes, we're also done, after making
16804 sure that a non-default face is extended up to the right
16805 margin of the window. */
16806 if (ITERATOR_AT_END_OF_LINE_P (it))
16807 {
16808 int used_before = row->used[TEXT_AREA];
16809
16810 row->ends_in_newline_from_string_p = STRINGP (it->object);
16811
16812 /* Add a space at the end of the line that is used to
16813 display the cursor there. */
16814 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16815 append_space_for_newline (it, 0);
16816
16817 /* Extend the face to the end of the line. */
16818 extend_face_to_end_of_line (it);
16819
16820 /* Make sure we have the position. */
16821 if (used_before == 0)
16822 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16823
16824 /* Consume the line end. This skips over invisible lines. */
16825 set_iterator_to_next (it, 1);
16826 it->continuation_lines_width = 0;
16827 break;
16828 }
16829
16830 /* Proceed with next display element. Note that this skips
16831 over lines invisible because of selective display. */
16832 set_iterator_to_next (it, 1);
16833
16834 /* If we truncate lines, we are done when the last displayed
16835 glyphs reach past the right margin of the window. */
16836 if (it->line_wrap == TRUNCATE
16837 && (FRAME_WINDOW_P (it->f)
16838 ? (it->current_x >= it->last_visible_x)
16839 : (it->current_x > it->last_visible_x)))
16840 {
16841 /* Maybe add truncation glyphs. */
16842 if (!FRAME_WINDOW_P (it->f))
16843 {
16844 int i, n;
16845
16846 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16847 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16848 break;
16849
16850 for (n = row->used[TEXT_AREA]; i < n; ++i)
16851 {
16852 row->used[TEXT_AREA] = i;
16853 produce_special_glyphs (it, IT_TRUNCATION);
16854 }
16855 }
16856 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16857 {
16858 /* Don't truncate if we can overflow newline into fringe. */
16859 if (!get_next_display_element (it))
16860 {
16861 it->continuation_lines_width = 0;
16862 row->ends_at_zv_p = 1;
16863 row->exact_window_width_line_p = 1;
16864 break;
16865 }
16866 if (ITERATOR_AT_END_OF_LINE_P (it))
16867 {
16868 row->exact_window_width_line_p = 1;
16869 goto at_end_of_line;
16870 }
16871 }
16872
16873 row->truncated_on_right_p = 1;
16874 it->continuation_lines_width = 0;
16875 reseat_at_next_visible_line_start (it, 0);
16876 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16877 it->hpos = hpos_before;
16878 it->current_x = x_before;
16879 break;
16880 }
16881 }
16882
16883 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16884 at the left window margin. */
16885 if (it->first_visible_x
16886 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16887 {
16888 if (!FRAME_WINDOW_P (it->f))
16889 insert_left_trunc_glyphs (it);
16890 row->truncated_on_left_p = 1;
16891 }
16892
16893 /* If the start of this line is the overlay arrow-position, then
16894 mark this glyph row as the one containing the overlay arrow.
16895 This is clearly a mess with variable size fonts. It would be
16896 better to let it be displayed like cursors under X. */
16897 if ((row->displays_text_p || !overlay_arrow_seen)
16898 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16899 !NILP (overlay_arrow_string)))
16900 {
16901 /* Overlay arrow in window redisplay is a fringe bitmap. */
16902 if (STRINGP (overlay_arrow_string))
16903 {
16904 struct glyph_row *arrow_row
16905 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16906 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16907 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16908 struct glyph *p = row->glyphs[TEXT_AREA];
16909 struct glyph *p2, *end;
16910
16911 /* Copy the arrow glyphs. */
16912 while (glyph < arrow_end)
16913 *p++ = *glyph++;
16914
16915 /* Throw away padding glyphs. */
16916 p2 = p;
16917 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16918 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16919 ++p2;
16920 if (p2 > p)
16921 {
16922 while (p2 < end)
16923 *p++ = *p2++;
16924 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16925 }
16926 }
16927 else
16928 {
16929 xassert (INTEGERP (overlay_arrow_string));
16930 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16931 }
16932 overlay_arrow_seen = 1;
16933 }
16934
16935 /* Compute pixel dimensions of this line. */
16936 compute_line_metrics (it);
16937
16938 /* Remember the position at which this line ends. */
16939 row->end = it->current;
16940
16941 /* Record whether this row ends inside an ellipsis. */
16942 row->ends_in_ellipsis_p
16943 = (it->method == GET_FROM_DISPLAY_VECTOR
16944 && it->ellipsis_p);
16945
16946 /* Save fringe bitmaps in this row. */
16947 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16948 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16949 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16950 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16951
16952 it->left_user_fringe_bitmap = 0;
16953 it->left_user_fringe_face_id = 0;
16954 it->right_user_fringe_bitmap = 0;
16955 it->right_user_fringe_face_id = 0;
16956
16957 /* Maybe set the cursor. */
16958 if (it->w->cursor.vpos < 0
16959 && PT >= MATRIX_ROW_START_CHARPOS (row)
16960 && PT <= MATRIX_ROW_END_CHARPOS (row)
16961 && cursor_row_p (it->w, row))
16962 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16963
16964 /* Highlight trailing whitespace. */
16965 if (!NILP (Vshow_trailing_whitespace))
16966 highlight_trailing_whitespace (it->f, it->glyph_row);
16967
16968 /* Prepare for the next line. This line starts horizontally at (X
16969 HPOS) = (0 0). Vertical positions are incremented. As a
16970 convenience for the caller, IT->glyph_row is set to the next
16971 row to be used. */
16972 it->current_x = it->hpos = 0;
16973 it->current_y += row->height;
16974 ++it->vpos;
16975 ++it->glyph_row;
16976 it->start = it->current;
16977 return row->displays_text_p;
16978 }
16979
16980
16981 \f
16982 /***********************************************************************
16983 Menu Bar
16984 ***********************************************************************/
16985
16986 /* Redisplay the menu bar in the frame for window W.
16987
16988 The menu bar of X frames that don't have X toolkit support is
16989 displayed in a special window W->frame->menu_bar_window.
16990
16991 The menu bar of terminal frames is treated specially as far as
16992 glyph matrices are concerned. Menu bar lines are not part of
16993 windows, so the update is done directly on the frame matrix rows
16994 for the menu bar. */
16995
16996 static void
16997 display_menu_bar (w)
16998 struct window *w;
16999 {
17000 struct frame *f = XFRAME (WINDOW_FRAME (w));
17001 struct it it;
17002 Lisp_Object items;
17003 int i;
17004
17005 /* Don't do all this for graphical frames. */
17006 #ifdef HAVE_NTGUI
17007 if (FRAME_W32_P (f))
17008 return;
17009 #endif
17010 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17011 if (FRAME_X_P (f))
17012 return;
17013 #endif
17014
17015 #ifdef HAVE_NS
17016 if (FRAME_NS_P (f))
17017 return;
17018 #endif /* HAVE_NS */
17019
17020 #ifdef USE_X_TOOLKIT
17021 xassert (!FRAME_WINDOW_P (f));
17022 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17023 it.first_visible_x = 0;
17024 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17025 #else /* not USE_X_TOOLKIT */
17026 if (FRAME_WINDOW_P (f))
17027 {
17028 /* Menu bar lines are displayed in the desired matrix of the
17029 dummy window menu_bar_window. */
17030 struct window *menu_w;
17031 xassert (WINDOWP (f->menu_bar_window));
17032 menu_w = XWINDOW (f->menu_bar_window);
17033 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17034 MENU_FACE_ID);
17035 it.first_visible_x = 0;
17036 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17037 }
17038 else
17039 {
17040 /* This is a TTY frame, i.e. character hpos/vpos are used as
17041 pixel x/y. */
17042 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17043 MENU_FACE_ID);
17044 it.first_visible_x = 0;
17045 it.last_visible_x = FRAME_COLS (f);
17046 }
17047 #endif /* not USE_X_TOOLKIT */
17048
17049 if (! mode_line_inverse_video)
17050 /* Force the menu-bar to be displayed in the default face. */
17051 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17052
17053 /* Clear all rows of the menu bar. */
17054 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17055 {
17056 struct glyph_row *row = it.glyph_row + i;
17057 clear_glyph_row (row);
17058 row->enabled_p = 1;
17059 row->full_width_p = 1;
17060 }
17061
17062 /* Display all items of the menu bar. */
17063 items = FRAME_MENU_BAR_ITEMS (it.f);
17064 for (i = 0; i < XVECTOR (items)->size; i += 4)
17065 {
17066 Lisp_Object string;
17067
17068 /* Stop at nil string. */
17069 string = AREF (items, i + 1);
17070 if (NILP (string))
17071 break;
17072
17073 /* Remember where item was displayed. */
17074 ASET (items, i + 3, make_number (it.hpos));
17075
17076 /* Display the item, pad with one space. */
17077 if (it.current_x < it.last_visible_x)
17078 display_string (NULL, string, Qnil, 0, 0, &it,
17079 SCHARS (string) + 1, 0, 0, -1);
17080 }
17081
17082 /* Fill out the line with spaces. */
17083 if (it.current_x < it.last_visible_x)
17084 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17085
17086 /* Compute the total height of the lines. */
17087 compute_line_metrics (&it);
17088 }
17089
17090
17091 \f
17092 /***********************************************************************
17093 Mode Line
17094 ***********************************************************************/
17095
17096 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17097 FORCE is non-zero, redisplay mode lines unconditionally.
17098 Otherwise, redisplay only mode lines that are garbaged. Value is
17099 the number of windows whose mode lines were redisplayed. */
17100
17101 static int
17102 redisplay_mode_lines (window, force)
17103 Lisp_Object window;
17104 int force;
17105 {
17106 int nwindows = 0;
17107
17108 while (!NILP (window))
17109 {
17110 struct window *w = XWINDOW (window);
17111
17112 if (WINDOWP (w->hchild))
17113 nwindows += redisplay_mode_lines (w->hchild, force);
17114 else if (WINDOWP (w->vchild))
17115 nwindows += redisplay_mode_lines (w->vchild, force);
17116 else if (force
17117 || FRAME_GARBAGED_P (XFRAME (w->frame))
17118 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17119 {
17120 struct text_pos lpoint;
17121 struct buffer *old = current_buffer;
17122
17123 /* Set the window's buffer for the mode line display. */
17124 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17125 set_buffer_internal_1 (XBUFFER (w->buffer));
17126
17127 /* Point refers normally to the selected window. For any
17128 other window, set up appropriate value. */
17129 if (!EQ (window, selected_window))
17130 {
17131 struct text_pos pt;
17132
17133 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17134 if (CHARPOS (pt) < BEGV)
17135 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17136 else if (CHARPOS (pt) > (ZV - 1))
17137 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17138 else
17139 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17140 }
17141
17142 /* Display mode lines. */
17143 clear_glyph_matrix (w->desired_matrix);
17144 if (display_mode_lines (w))
17145 {
17146 ++nwindows;
17147 w->must_be_updated_p = 1;
17148 }
17149
17150 /* Restore old settings. */
17151 set_buffer_internal_1 (old);
17152 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17153 }
17154
17155 window = w->next;
17156 }
17157
17158 return nwindows;
17159 }
17160
17161
17162 /* Display the mode and/or header line of window W. Value is the
17163 sum number of mode lines and header lines displayed. */
17164
17165 static int
17166 display_mode_lines (w)
17167 struct window *w;
17168 {
17169 Lisp_Object old_selected_window, old_selected_frame;
17170 int n = 0;
17171
17172 old_selected_frame = selected_frame;
17173 selected_frame = w->frame;
17174 old_selected_window = selected_window;
17175 XSETWINDOW (selected_window, w);
17176
17177 /* These will be set while the mode line specs are processed. */
17178 line_number_displayed = 0;
17179 w->column_number_displayed = Qnil;
17180
17181 if (WINDOW_WANTS_MODELINE_P (w))
17182 {
17183 struct window *sel_w = XWINDOW (old_selected_window);
17184
17185 /* Select mode line face based on the real selected window. */
17186 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17187 current_buffer->mode_line_format);
17188 ++n;
17189 }
17190
17191 if (WINDOW_WANTS_HEADER_LINE_P (w))
17192 {
17193 display_mode_line (w, HEADER_LINE_FACE_ID,
17194 current_buffer->header_line_format);
17195 ++n;
17196 }
17197
17198 selected_frame = old_selected_frame;
17199 selected_window = old_selected_window;
17200 return n;
17201 }
17202
17203
17204 /* Display mode or header line of window W. FACE_ID specifies which
17205 line to display; it is either MODE_LINE_FACE_ID or
17206 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17207 display. Value is the pixel height of the mode/header line
17208 displayed. */
17209
17210 static int
17211 display_mode_line (w, face_id, format)
17212 struct window *w;
17213 enum face_id face_id;
17214 Lisp_Object format;
17215 {
17216 struct it it;
17217 struct face *face;
17218 int count = SPECPDL_INDEX ();
17219
17220 init_iterator (&it, w, -1, -1, NULL, face_id);
17221 /* Don't extend on a previously drawn mode-line.
17222 This may happen if called from pos_visible_p. */
17223 it.glyph_row->enabled_p = 0;
17224 prepare_desired_row (it.glyph_row);
17225
17226 it.glyph_row->mode_line_p = 1;
17227
17228 if (! mode_line_inverse_video)
17229 /* Force the mode-line to be displayed in the default face. */
17230 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17231
17232 record_unwind_protect (unwind_format_mode_line,
17233 format_mode_line_unwind_data (NULL, Qnil, 0));
17234
17235 mode_line_target = MODE_LINE_DISPLAY;
17236
17237 /* Temporarily make frame's keyboard the current kboard so that
17238 kboard-local variables in the mode_line_format will get the right
17239 values. */
17240 push_kboard (FRAME_KBOARD (it.f));
17241 record_unwind_save_match_data ();
17242 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17243 pop_kboard ();
17244
17245 unbind_to (count, Qnil);
17246
17247 /* Fill up with spaces. */
17248 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17249
17250 compute_line_metrics (&it);
17251 it.glyph_row->full_width_p = 1;
17252 it.glyph_row->continued_p = 0;
17253 it.glyph_row->truncated_on_left_p = 0;
17254 it.glyph_row->truncated_on_right_p = 0;
17255
17256 /* Make a 3D mode-line have a shadow at its right end. */
17257 face = FACE_FROM_ID (it.f, face_id);
17258 extend_face_to_end_of_line (&it);
17259 if (face->box != FACE_NO_BOX)
17260 {
17261 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17262 + it.glyph_row->used[TEXT_AREA] - 1);
17263 last->right_box_line_p = 1;
17264 }
17265
17266 return it.glyph_row->height;
17267 }
17268
17269 /* Move element ELT in LIST to the front of LIST.
17270 Return the updated list. */
17271
17272 static Lisp_Object
17273 move_elt_to_front (elt, list)
17274 Lisp_Object elt, list;
17275 {
17276 register Lisp_Object tail, prev;
17277 register Lisp_Object tem;
17278
17279 tail = list;
17280 prev = Qnil;
17281 while (CONSP (tail))
17282 {
17283 tem = XCAR (tail);
17284
17285 if (EQ (elt, tem))
17286 {
17287 /* Splice out the link TAIL. */
17288 if (NILP (prev))
17289 list = XCDR (tail);
17290 else
17291 Fsetcdr (prev, XCDR (tail));
17292
17293 /* Now make it the first. */
17294 Fsetcdr (tail, list);
17295 return tail;
17296 }
17297 else
17298 prev = tail;
17299 tail = XCDR (tail);
17300 QUIT;
17301 }
17302
17303 /* Not found--return unchanged LIST. */
17304 return list;
17305 }
17306
17307 /* Contribute ELT to the mode line for window IT->w. How it
17308 translates into text depends on its data type.
17309
17310 IT describes the display environment in which we display, as usual.
17311
17312 DEPTH is the depth in recursion. It is used to prevent
17313 infinite recursion here.
17314
17315 FIELD_WIDTH is the number of characters the display of ELT should
17316 occupy in the mode line, and PRECISION is the maximum number of
17317 characters to display from ELT's representation. See
17318 display_string for details.
17319
17320 Returns the hpos of the end of the text generated by ELT.
17321
17322 PROPS is a property list to add to any string we encounter.
17323
17324 If RISKY is nonzero, remove (disregard) any properties in any string
17325 we encounter, and ignore :eval and :propertize.
17326
17327 The global variable `mode_line_target' determines whether the
17328 output is passed to `store_mode_line_noprop',
17329 `store_mode_line_string', or `display_string'. */
17330
17331 static int
17332 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17333 struct it *it;
17334 int depth;
17335 int field_width, precision;
17336 Lisp_Object elt, props;
17337 int risky;
17338 {
17339 int n = 0, field, prec;
17340 int literal = 0;
17341
17342 tail_recurse:
17343 if (depth > 100)
17344 elt = build_string ("*too-deep*");
17345
17346 depth++;
17347
17348 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17349 {
17350 case Lisp_String:
17351 {
17352 /* A string: output it and check for %-constructs within it. */
17353 unsigned char c;
17354 int offset = 0;
17355
17356 if (SCHARS (elt) > 0
17357 && (!NILP (props) || risky))
17358 {
17359 Lisp_Object oprops, aelt;
17360 oprops = Ftext_properties_at (make_number (0), elt);
17361
17362 /* If the starting string's properties are not what
17363 we want, translate the string. Also, if the string
17364 is risky, do that anyway. */
17365
17366 if (NILP (Fequal (props, oprops)) || risky)
17367 {
17368 /* If the starting string has properties,
17369 merge the specified ones onto the existing ones. */
17370 if (! NILP (oprops) && !risky)
17371 {
17372 Lisp_Object tem;
17373
17374 oprops = Fcopy_sequence (oprops);
17375 tem = props;
17376 while (CONSP (tem))
17377 {
17378 oprops = Fplist_put (oprops, XCAR (tem),
17379 XCAR (XCDR (tem)));
17380 tem = XCDR (XCDR (tem));
17381 }
17382 props = oprops;
17383 }
17384
17385 aelt = Fassoc (elt, mode_line_proptrans_alist);
17386 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17387 {
17388 /* AELT is what we want. Move it to the front
17389 without consing. */
17390 elt = XCAR (aelt);
17391 mode_line_proptrans_alist
17392 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17393 }
17394 else
17395 {
17396 Lisp_Object tem;
17397
17398 /* If AELT has the wrong props, it is useless.
17399 so get rid of it. */
17400 if (! NILP (aelt))
17401 mode_line_proptrans_alist
17402 = Fdelq (aelt, mode_line_proptrans_alist);
17403
17404 elt = Fcopy_sequence (elt);
17405 Fset_text_properties (make_number (0), Flength (elt),
17406 props, elt);
17407 /* Add this item to mode_line_proptrans_alist. */
17408 mode_line_proptrans_alist
17409 = Fcons (Fcons (elt, props),
17410 mode_line_proptrans_alist);
17411 /* Truncate mode_line_proptrans_alist
17412 to at most 50 elements. */
17413 tem = Fnthcdr (make_number (50),
17414 mode_line_proptrans_alist);
17415 if (! NILP (tem))
17416 XSETCDR (tem, Qnil);
17417 }
17418 }
17419 }
17420
17421 offset = 0;
17422
17423 if (literal)
17424 {
17425 prec = precision - n;
17426 switch (mode_line_target)
17427 {
17428 case MODE_LINE_NOPROP:
17429 case MODE_LINE_TITLE:
17430 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17431 break;
17432 case MODE_LINE_STRING:
17433 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17434 break;
17435 case MODE_LINE_DISPLAY:
17436 n += display_string (NULL, elt, Qnil, 0, 0, it,
17437 0, prec, 0, STRING_MULTIBYTE (elt));
17438 break;
17439 }
17440
17441 break;
17442 }
17443
17444 /* Handle the non-literal case. */
17445
17446 while ((precision <= 0 || n < precision)
17447 && SREF (elt, offset) != 0
17448 && (mode_line_target != MODE_LINE_DISPLAY
17449 || it->current_x < it->last_visible_x))
17450 {
17451 int last_offset = offset;
17452
17453 /* Advance to end of string or next format specifier. */
17454 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17455 ;
17456
17457 if (offset - 1 != last_offset)
17458 {
17459 int nchars, nbytes;
17460
17461 /* Output to end of string or up to '%'. Field width
17462 is length of string. Don't output more than
17463 PRECISION allows us. */
17464 offset--;
17465
17466 prec = c_string_width (SDATA (elt) + last_offset,
17467 offset - last_offset, precision - n,
17468 &nchars, &nbytes);
17469
17470 switch (mode_line_target)
17471 {
17472 case MODE_LINE_NOPROP:
17473 case MODE_LINE_TITLE:
17474 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17475 break;
17476 case MODE_LINE_STRING:
17477 {
17478 int bytepos = last_offset;
17479 int charpos = string_byte_to_char (elt, bytepos);
17480 int endpos = (precision <= 0
17481 ? string_byte_to_char (elt, offset)
17482 : charpos + nchars);
17483
17484 n += store_mode_line_string (NULL,
17485 Fsubstring (elt, make_number (charpos),
17486 make_number (endpos)),
17487 0, 0, 0, Qnil);
17488 }
17489 break;
17490 case MODE_LINE_DISPLAY:
17491 {
17492 int bytepos = last_offset;
17493 int charpos = string_byte_to_char (elt, bytepos);
17494
17495 if (precision <= 0)
17496 nchars = string_byte_to_char (elt, offset) - charpos;
17497 n += display_string (NULL, elt, Qnil, 0, charpos,
17498 it, 0, nchars, 0,
17499 STRING_MULTIBYTE (elt));
17500 }
17501 break;
17502 }
17503 }
17504 else /* c == '%' */
17505 {
17506 int percent_position = offset;
17507
17508 /* Get the specified minimum width. Zero means
17509 don't pad. */
17510 field = 0;
17511 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17512 field = field * 10 + c - '0';
17513
17514 /* Don't pad beyond the total padding allowed. */
17515 if (field_width - n > 0 && field > field_width - n)
17516 field = field_width - n;
17517
17518 /* Note that either PRECISION <= 0 or N < PRECISION. */
17519 prec = precision - n;
17520
17521 if (c == 'M')
17522 n += display_mode_element (it, depth, field, prec,
17523 Vglobal_mode_string, props,
17524 risky);
17525 else if (c != 0)
17526 {
17527 int multibyte;
17528 int bytepos, charpos;
17529 unsigned char *spec;
17530 Lisp_Object string;
17531
17532 bytepos = percent_position;
17533 charpos = (STRING_MULTIBYTE (elt)
17534 ? string_byte_to_char (elt, bytepos)
17535 : bytepos);
17536 spec = decode_mode_spec (it->w, c, field, prec, &string);
17537 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
17538
17539 switch (mode_line_target)
17540 {
17541 case MODE_LINE_NOPROP:
17542 case MODE_LINE_TITLE:
17543 n += store_mode_line_noprop (spec, field, prec);
17544 break;
17545 case MODE_LINE_STRING:
17546 {
17547 int len = strlen (spec);
17548 Lisp_Object tem = make_string (spec, len);
17549 props = Ftext_properties_at (make_number (charpos), elt);
17550 /* Should only keep face property in props */
17551 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17552 }
17553 break;
17554 case MODE_LINE_DISPLAY:
17555 {
17556 int nglyphs_before, nwritten;
17557
17558 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17559 nwritten = display_string (spec, string, elt,
17560 charpos, 0, it,
17561 field, prec, 0,
17562 multibyte);
17563
17564 /* Assign to the glyphs written above the
17565 string where the `%x' came from, position
17566 of the `%'. */
17567 if (nwritten > 0)
17568 {
17569 struct glyph *glyph
17570 = (it->glyph_row->glyphs[TEXT_AREA]
17571 + nglyphs_before);
17572 int i;
17573
17574 for (i = 0; i < nwritten; ++i)
17575 {
17576 glyph[i].object = elt;
17577 glyph[i].charpos = charpos;
17578 }
17579
17580 n += nwritten;
17581 }
17582 }
17583 break;
17584 }
17585 }
17586 else /* c == 0 */
17587 break;
17588 }
17589 }
17590 }
17591 break;
17592
17593 case Lisp_Symbol:
17594 /* A symbol: process the value of the symbol recursively
17595 as if it appeared here directly. Avoid error if symbol void.
17596 Special case: if value of symbol is a string, output the string
17597 literally. */
17598 {
17599 register Lisp_Object tem;
17600
17601 /* If the variable is not marked as risky to set
17602 then its contents are risky to use. */
17603 if (NILP (Fget (elt, Qrisky_local_variable)))
17604 risky = 1;
17605
17606 tem = Fboundp (elt);
17607 if (!NILP (tem))
17608 {
17609 tem = Fsymbol_value (elt);
17610 /* If value is a string, output that string literally:
17611 don't check for % within it. */
17612 if (STRINGP (tem))
17613 literal = 1;
17614
17615 if (!EQ (tem, elt))
17616 {
17617 /* Give up right away for nil or t. */
17618 elt = tem;
17619 goto tail_recurse;
17620 }
17621 }
17622 }
17623 break;
17624
17625 case Lisp_Cons:
17626 {
17627 register Lisp_Object car, tem;
17628
17629 /* A cons cell: five distinct cases.
17630 If first element is :eval or :propertize, do something special.
17631 If first element is a string or a cons, process all the elements
17632 and effectively concatenate them.
17633 If first element is a negative number, truncate displaying cdr to
17634 at most that many characters. If positive, pad (with spaces)
17635 to at least that many characters.
17636 If first element is a symbol, process the cadr or caddr recursively
17637 according to whether the symbol's value is non-nil or nil. */
17638 car = XCAR (elt);
17639 if (EQ (car, QCeval))
17640 {
17641 /* An element of the form (:eval FORM) means evaluate FORM
17642 and use the result as mode line elements. */
17643
17644 if (risky)
17645 break;
17646
17647 if (CONSP (XCDR (elt)))
17648 {
17649 Lisp_Object spec;
17650 spec = safe_eval (XCAR (XCDR (elt)));
17651 n += display_mode_element (it, depth, field_width - n,
17652 precision - n, spec, props,
17653 risky);
17654 }
17655 }
17656 else if (EQ (car, QCpropertize))
17657 {
17658 /* An element of the form (:propertize ELT PROPS...)
17659 means display ELT but applying properties PROPS. */
17660
17661 if (risky)
17662 break;
17663
17664 if (CONSP (XCDR (elt)))
17665 n += display_mode_element (it, depth, field_width - n,
17666 precision - n, XCAR (XCDR (elt)),
17667 XCDR (XCDR (elt)), risky);
17668 }
17669 else if (SYMBOLP (car))
17670 {
17671 tem = Fboundp (car);
17672 elt = XCDR (elt);
17673 if (!CONSP (elt))
17674 goto invalid;
17675 /* elt is now the cdr, and we know it is a cons cell.
17676 Use its car if CAR has a non-nil value. */
17677 if (!NILP (tem))
17678 {
17679 tem = Fsymbol_value (car);
17680 if (!NILP (tem))
17681 {
17682 elt = XCAR (elt);
17683 goto tail_recurse;
17684 }
17685 }
17686 /* Symbol's value is nil (or symbol is unbound)
17687 Get the cddr of the original list
17688 and if possible find the caddr and use that. */
17689 elt = XCDR (elt);
17690 if (NILP (elt))
17691 break;
17692 else if (!CONSP (elt))
17693 goto invalid;
17694 elt = XCAR (elt);
17695 goto tail_recurse;
17696 }
17697 else if (INTEGERP (car))
17698 {
17699 register int lim = XINT (car);
17700 elt = XCDR (elt);
17701 if (lim < 0)
17702 {
17703 /* Negative int means reduce maximum width. */
17704 if (precision <= 0)
17705 precision = -lim;
17706 else
17707 precision = min (precision, -lim);
17708 }
17709 else if (lim > 0)
17710 {
17711 /* Padding specified. Don't let it be more than
17712 current maximum. */
17713 if (precision > 0)
17714 lim = min (precision, lim);
17715
17716 /* If that's more padding than already wanted, queue it.
17717 But don't reduce padding already specified even if
17718 that is beyond the current truncation point. */
17719 field_width = max (lim, field_width);
17720 }
17721 goto tail_recurse;
17722 }
17723 else if (STRINGP (car) || CONSP (car))
17724 {
17725 Lisp_Object halftail = elt;
17726 int len = 0;
17727
17728 while (CONSP (elt)
17729 && (precision <= 0 || n < precision))
17730 {
17731 n += display_mode_element (it, depth,
17732 /* Do padding only after the last
17733 element in the list. */
17734 (! CONSP (XCDR (elt))
17735 ? field_width - n
17736 : 0),
17737 precision - n, XCAR (elt),
17738 props, risky);
17739 elt = XCDR (elt);
17740 len++;
17741 if ((len & 1) == 0)
17742 halftail = XCDR (halftail);
17743 /* Check for cycle. */
17744 if (EQ (halftail, elt))
17745 break;
17746 }
17747 }
17748 }
17749 break;
17750
17751 default:
17752 invalid:
17753 elt = build_string ("*invalid*");
17754 goto tail_recurse;
17755 }
17756
17757 /* Pad to FIELD_WIDTH. */
17758 if (field_width > 0 && n < field_width)
17759 {
17760 switch (mode_line_target)
17761 {
17762 case MODE_LINE_NOPROP:
17763 case MODE_LINE_TITLE:
17764 n += store_mode_line_noprop ("", field_width - n, 0);
17765 break;
17766 case MODE_LINE_STRING:
17767 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17768 break;
17769 case MODE_LINE_DISPLAY:
17770 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17771 0, 0, 0);
17772 break;
17773 }
17774 }
17775
17776 return n;
17777 }
17778
17779 /* Store a mode-line string element in mode_line_string_list.
17780
17781 If STRING is non-null, display that C string. Otherwise, the Lisp
17782 string LISP_STRING is displayed.
17783
17784 FIELD_WIDTH is the minimum number of output glyphs to produce.
17785 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17786 with spaces. FIELD_WIDTH <= 0 means don't pad.
17787
17788 PRECISION is the maximum number of characters to output from
17789 STRING. PRECISION <= 0 means don't truncate the string.
17790
17791 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17792 properties to the string.
17793
17794 PROPS are the properties to add to the string.
17795 The mode_line_string_face face property is always added to the string.
17796 */
17797
17798 static int
17799 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17800 char *string;
17801 Lisp_Object lisp_string;
17802 int copy_string;
17803 int field_width;
17804 int precision;
17805 Lisp_Object props;
17806 {
17807 int len;
17808 int n = 0;
17809
17810 if (string != NULL)
17811 {
17812 len = strlen (string);
17813 if (precision > 0 && len > precision)
17814 len = precision;
17815 lisp_string = make_string (string, len);
17816 if (NILP (props))
17817 props = mode_line_string_face_prop;
17818 else if (!NILP (mode_line_string_face))
17819 {
17820 Lisp_Object face = Fplist_get (props, Qface);
17821 props = Fcopy_sequence (props);
17822 if (NILP (face))
17823 face = mode_line_string_face;
17824 else
17825 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17826 props = Fplist_put (props, Qface, face);
17827 }
17828 Fadd_text_properties (make_number (0), make_number (len),
17829 props, lisp_string);
17830 }
17831 else
17832 {
17833 len = XFASTINT (Flength (lisp_string));
17834 if (precision > 0 && len > precision)
17835 {
17836 len = precision;
17837 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17838 precision = -1;
17839 }
17840 if (!NILP (mode_line_string_face))
17841 {
17842 Lisp_Object face;
17843 if (NILP (props))
17844 props = Ftext_properties_at (make_number (0), lisp_string);
17845 face = Fplist_get (props, Qface);
17846 if (NILP (face))
17847 face = mode_line_string_face;
17848 else
17849 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17850 props = Fcons (Qface, Fcons (face, Qnil));
17851 if (copy_string)
17852 lisp_string = Fcopy_sequence (lisp_string);
17853 }
17854 if (!NILP (props))
17855 Fadd_text_properties (make_number (0), make_number (len),
17856 props, lisp_string);
17857 }
17858
17859 if (len > 0)
17860 {
17861 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17862 n += len;
17863 }
17864
17865 if (field_width > len)
17866 {
17867 field_width -= len;
17868 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17869 if (!NILP (props))
17870 Fadd_text_properties (make_number (0), make_number (field_width),
17871 props, lisp_string);
17872 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17873 n += field_width;
17874 }
17875
17876 return n;
17877 }
17878
17879
17880 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17881 1, 4, 0,
17882 doc: /* Format a string out of a mode line format specification.
17883 First arg FORMAT specifies the mode line format (see `mode-line-format'
17884 for details) to use.
17885
17886 Optional second arg FACE specifies the face property to put
17887 on all characters for which no face is specified.
17888 The value t means whatever face the window's mode line currently uses
17889 \(either `mode-line' or `mode-line-inactive', depending).
17890 A value of nil means the default is no face property.
17891 If FACE is an integer, the value string has no text properties.
17892
17893 Optional third and fourth args WINDOW and BUFFER specify the window
17894 and buffer to use as the context for the formatting (defaults
17895 are the selected window and the window's buffer). */)
17896 (format, face, window, buffer)
17897 Lisp_Object format, face, window, buffer;
17898 {
17899 struct it it;
17900 int len;
17901 struct window *w;
17902 struct buffer *old_buffer = NULL;
17903 int face_id = -1;
17904 int no_props = INTEGERP (face);
17905 int count = SPECPDL_INDEX ();
17906 Lisp_Object str;
17907 int string_start = 0;
17908
17909 if (NILP (window))
17910 window = selected_window;
17911 CHECK_WINDOW (window);
17912 w = XWINDOW (window);
17913
17914 if (NILP (buffer))
17915 buffer = w->buffer;
17916 CHECK_BUFFER (buffer);
17917
17918 /* Make formatting the modeline a non-op when noninteractive, otherwise
17919 there will be problems later caused by a partially initialized frame. */
17920 if (NILP (format) || noninteractive)
17921 return empty_unibyte_string;
17922
17923 if (no_props)
17924 face = Qnil;
17925
17926 if (!NILP (face))
17927 {
17928 if (EQ (face, Qt))
17929 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17930 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17931 }
17932
17933 if (face_id < 0)
17934 face_id = DEFAULT_FACE_ID;
17935
17936 if (XBUFFER (buffer) != current_buffer)
17937 old_buffer = current_buffer;
17938
17939 /* Save things including mode_line_proptrans_alist,
17940 and set that to nil so that we don't alter the outer value. */
17941 record_unwind_protect (unwind_format_mode_line,
17942 format_mode_line_unwind_data
17943 (old_buffer, selected_window, 1));
17944 mode_line_proptrans_alist = Qnil;
17945
17946 Fselect_window (window, Qt);
17947 if (old_buffer)
17948 set_buffer_internal_1 (XBUFFER (buffer));
17949
17950 init_iterator (&it, w, -1, -1, NULL, face_id);
17951
17952 if (no_props)
17953 {
17954 mode_line_target = MODE_LINE_NOPROP;
17955 mode_line_string_face_prop = Qnil;
17956 mode_line_string_list = Qnil;
17957 string_start = MODE_LINE_NOPROP_LEN (0);
17958 }
17959 else
17960 {
17961 mode_line_target = MODE_LINE_STRING;
17962 mode_line_string_list = Qnil;
17963 mode_line_string_face = face;
17964 mode_line_string_face_prop
17965 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17966 }
17967
17968 push_kboard (FRAME_KBOARD (it.f));
17969 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17970 pop_kboard ();
17971
17972 if (no_props)
17973 {
17974 len = MODE_LINE_NOPROP_LEN (string_start);
17975 str = make_string (mode_line_noprop_buf + string_start, len);
17976 }
17977 else
17978 {
17979 mode_line_string_list = Fnreverse (mode_line_string_list);
17980 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17981 empty_unibyte_string);
17982 }
17983
17984 unbind_to (count, Qnil);
17985 return str;
17986 }
17987
17988 /* Write a null-terminated, right justified decimal representation of
17989 the positive integer D to BUF using a minimal field width WIDTH. */
17990
17991 static void
17992 pint2str (buf, width, d)
17993 register char *buf;
17994 register int width;
17995 register int d;
17996 {
17997 register char *p = buf;
17998
17999 if (d <= 0)
18000 *p++ = '0';
18001 else
18002 {
18003 while (d > 0)
18004 {
18005 *p++ = d % 10 + '0';
18006 d /= 10;
18007 }
18008 }
18009
18010 for (width -= (int) (p - buf); width > 0; --width)
18011 *p++ = ' ';
18012 *p-- = '\0';
18013 while (p > buf)
18014 {
18015 d = *buf;
18016 *buf++ = *p;
18017 *p-- = d;
18018 }
18019 }
18020
18021 /* Write a null-terminated, right justified decimal and "human
18022 readable" representation of the nonnegative integer D to BUF using
18023 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18024
18025 static const char power_letter[] =
18026 {
18027 0, /* not used */
18028 'k', /* kilo */
18029 'M', /* mega */
18030 'G', /* giga */
18031 'T', /* tera */
18032 'P', /* peta */
18033 'E', /* exa */
18034 'Z', /* zetta */
18035 'Y' /* yotta */
18036 };
18037
18038 static void
18039 pint2hrstr (buf, width, d)
18040 char *buf;
18041 int width;
18042 int d;
18043 {
18044 /* We aim to represent the nonnegative integer D as
18045 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18046 int quotient = d;
18047 int remainder = 0;
18048 /* -1 means: do not use TENTHS. */
18049 int tenths = -1;
18050 int exponent = 0;
18051
18052 /* Length of QUOTIENT.TENTHS as a string. */
18053 int length;
18054
18055 char * psuffix;
18056 char * p;
18057
18058 if (1000 <= quotient)
18059 {
18060 /* Scale to the appropriate EXPONENT. */
18061 do
18062 {
18063 remainder = quotient % 1000;
18064 quotient /= 1000;
18065 exponent++;
18066 }
18067 while (1000 <= quotient);
18068
18069 /* Round to nearest and decide whether to use TENTHS or not. */
18070 if (quotient <= 9)
18071 {
18072 tenths = remainder / 100;
18073 if (50 <= remainder % 100)
18074 {
18075 if (tenths < 9)
18076 tenths++;
18077 else
18078 {
18079 quotient++;
18080 if (quotient == 10)
18081 tenths = -1;
18082 else
18083 tenths = 0;
18084 }
18085 }
18086 }
18087 else
18088 if (500 <= remainder)
18089 {
18090 if (quotient < 999)
18091 quotient++;
18092 else
18093 {
18094 quotient = 1;
18095 exponent++;
18096 tenths = 0;
18097 }
18098 }
18099 }
18100
18101 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18102 if (tenths == -1 && quotient <= 99)
18103 if (quotient <= 9)
18104 length = 1;
18105 else
18106 length = 2;
18107 else
18108 length = 3;
18109 p = psuffix = buf + max (width, length);
18110
18111 /* Print EXPONENT. */
18112 if (exponent)
18113 *psuffix++ = power_letter[exponent];
18114 *psuffix = '\0';
18115
18116 /* Print TENTHS. */
18117 if (tenths >= 0)
18118 {
18119 *--p = '0' + tenths;
18120 *--p = '.';
18121 }
18122
18123 /* Print QUOTIENT. */
18124 do
18125 {
18126 int digit = quotient % 10;
18127 *--p = '0' + digit;
18128 }
18129 while ((quotient /= 10) != 0);
18130
18131 /* Print leading spaces. */
18132 while (buf < p)
18133 *--p = ' ';
18134 }
18135
18136 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18137 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18138 type of CODING_SYSTEM. Return updated pointer into BUF. */
18139
18140 static unsigned char invalid_eol_type[] = "(*invalid*)";
18141
18142 static char *
18143 decode_mode_spec_coding (coding_system, buf, eol_flag)
18144 Lisp_Object coding_system;
18145 register char *buf;
18146 int eol_flag;
18147 {
18148 Lisp_Object val;
18149 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18150 const unsigned char *eol_str;
18151 int eol_str_len;
18152 /* The EOL conversion we are using. */
18153 Lisp_Object eoltype;
18154
18155 val = CODING_SYSTEM_SPEC (coding_system);
18156 eoltype = Qnil;
18157
18158 if (!VECTORP (val)) /* Not yet decided. */
18159 {
18160 if (multibyte)
18161 *buf++ = '-';
18162 if (eol_flag)
18163 eoltype = eol_mnemonic_undecided;
18164 /* Don't mention EOL conversion if it isn't decided. */
18165 }
18166 else
18167 {
18168 Lisp_Object attrs;
18169 Lisp_Object eolvalue;
18170
18171 attrs = AREF (val, 0);
18172 eolvalue = AREF (val, 2);
18173
18174 if (multibyte)
18175 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18176
18177 if (eol_flag)
18178 {
18179 /* The EOL conversion that is normal on this system. */
18180
18181 if (NILP (eolvalue)) /* Not yet decided. */
18182 eoltype = eol_mnemonic_undecided;
18183 else if (VECTORP (eolvalue)) /* Not yet decided. */
18184 eoltype = eol_mnemonic_undecided;
18185 else /* eolvalue is Qunix, Qdos, or Qmac. */
18186 eoltype = (EQ (eolvalue, Qunix)
18187 ? eol_mnemonic_unix
18188 : (EQ (eolvalue, Qdos) == 1
18189 ? eol_mnemonic_dos : eol_mnemonic_mac));
18190 }
18191 }
18192
18193 if (eol_flag)
18194 {
18195 /* Mention the EOL conversion if it is not the usual one. */
18196 if (STRINGP (eoltype))
18197 {
18198 eol_str = SDATA (eoltype);
18199 eol_str_len = SBYTES (eoltype);
18200 }
18201 else if (CHARACTERP (eoltype))
18202 {
18203 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18204 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18205 eol_str = tmp;
18206 }
18207 else
18208 {
18209 eol_str = invalid_eol_type;
18210 eol_str_len = sizeof (invalid_eol_type) - 1;
18211 }
18212 bcopy (eol_str, buf, eol_str_len);
18213 buf += eol_str_len;
18214 }
18215
18216 return buf;
18217 }
18218
18219 /* Return a string for the output of a mode line %-spec for window W,
18220 generated by character C. PRECISION >= 0 means don't return a
18221 string longer than that value. FIELD_WIDTH > 0 means pad the
18222 string returned with spaces to that value. Return a Lisp string in
18223 *STRING if the resulting string is taken from that Lisp string.
18224
18225 Note we operate on the current buffer for most purposes,
18226 the exception being w->base_line_pos. */
18227
18228 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18229
18230 static char *
18231 decode_mode_spec (w, c, field_width, precision, string)
18232 struct window *w;
18233 register int c;
18234 int field_width, precision;
18235 Lisp_Object *string;
18236 {
18237 Lisp_Object obj;
18238 struct frame *f = XFRAME (WINDOW_FRAME (w));
18239 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18240 struct buffer *b = current_buffer;
18241
18242 obj = Qnil;
18243 *string = Qnil;
18244
18245 switch (c)
18246 {
18247 case '*':
18248 if (!NILP (b->read_only))
18249 return "%";
18250 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18251 return "*";
18252 return "-";
18253
18254 case '+':
18255 /* This differs from %* only for a modified read-only buffer. */
18256 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18257 return "*";
18258 if (!NILP (b->read_only))
18259 return "%";
18260 return "-";
18261
18262 case '&':
18263 /* This differs from %* in ignoring read-only-ness. */
18264 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18265 return "*";
18266 return "-";
18267
18268 case '%':
18269 return "%";
18270
18271 case '[':
18272 {
18273 int i;
18274 char *p;
18275
18276 if (command_loop_level > 5)
18277 return "[[[... ";
18278 p = decode_mode_spec_buf;
18279 for (i = 0; i < command_loop_level; i++)
18280 *p++ = '[';
18281 *p = 0;
18282 return decode_mode_spec_buf;
18283 }
18284
18285 case ']':
18286 {
18287 int i;
18288 char *p;
18289
18290 if (command_loop_level > 5)
18291 return " ...]]]";
18292 p = decode_mode_spec_buf;
18293 for (i = 0; i < command_loop_level; i++)
18294 *p++ = ']';
18295 *p = 0;
18296 return decode_mode_spec_buf;
18297 }
18298
18299 case '-':
18300 {
18301 register int i;
18302
18303 /* Let lots_of_dashes be a string of infinite length. */
18304 if (mode_line_target == MODE_LINE_NOPROP ||
18305 mode_line_target == MODE_LINE_STRING)
18306 return "--";
18307 if (field_width <= 0
18308 || field_width > sizeof (lots_of_dashes))
18309 {
18310 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18311 decode_mode_spec_buf[i] = '-';
18312 decode_mode_spec_buf[i] = '\0';
18313 return decode_mode_spec_buf;
18314 }
18315 else
18316 return lots_of_dashes;
18317 }
18318
18319 case 'b':
18320 obj = b->name;
18321 break;
18322
18323 case 'c':
18324 /* %c and %l are ignored in `frame-title-format'.
18325 (In redisplay_internal, the frame title is drawn _before_ the
18326 windows are updated, so the stuff which depends on actual
18327 window contents (such as %l) may fail to render properly, or
18328 even crash emacs.) */
18329 if (mode_line_target == MODE_LINE_TITLE)
18330 return "";
18331 else
18332 {
18333 int col = (int) current_column (); /* iftc */
18334 w->column_number_displayed = make_number (col);
18335 pint2str (decode_mode_spec_buf, field_width, col);
18336 return decode_mode_spec_buf;
18337 }
18338
18339 case 'e':
18340 #ifndef SYSTEM_MALLOC
18341 {
18342 if (NILP (Vmemory_full))
18343 return "";
18344 else
18345 return "!MEM FULL! ";
18346 }
18347 #else
18348 return "";
18349 #endif
18350
18351 case 'F':
18352 /* %F displays the frame name. */
18353 if (!NILP (f->title))
18354 return (char *) SDATA (f->title);
18355 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18356 return (char *) SDATA (f->name);
18357 return "Emacs";
18358
18359 case 'f':
18360 obj = b->filename;
18361 break;
18362
18363 case 'i':
18364 {
18365 int size = ZV - BEGV;
18366 pint2str (decode_mode_spec_buf, field_width, size);
18367 return decode_mode_spec_buf;
18368 }
18369
18370 case 'I':
18371 {
18372 int size = ZV - BEGV;
18373 pint2hrstr (decode_mode_spec_buf, field_width, size);
18374 return decode_mode_spec_buf;
18375 }
18376
18377 case 'l':
18378 {
18379 int startpos, startpos_byte, line, linepos, linepos_byte;
18380 int topline, nlines, junk, height;
18381
18382 /* %c and %l are ignored in `frame-title-format'. */
18383 if (mode_line_target == MODE_LINE_TITLE)
18384 return "";
18385
18386 startpos = XMARKER (w->start)->charpos;
18387 startpos_byte = marker_byte_position (w->start);
18388 height = WINDOW_TOTAL_LINES (w);
18389
18390 /* If we decided that this buffer isn't suitable for line numbers,
18391 don't forget that too fast. */
18392 if (EQ (w->base_line_pos, w->buffer))
18393 goto no_value;
18394 /* But do forget it, if the window shows a different buffer now. */
18395 else if (BUFFERP (w->base_line_pos))
18396 w->base_line_pos = Qnil;
18397
18398 /* If the buffer is very big, don't waste time. */
18399 if (INTEGERP (Vline_number_display_limit)
18400 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18401 {
18402 w->base_line_pos = Qnil;
18403 w->base_line_number = Qnil;
18404 goto no_value;
18405 }
18406
18407 if (INTEGERP (w->base_line_number)
18408 && INTEGERP (w->base_line_pos)
18409 && XFASTINT (w->base_line_pos) <= startpos)
18410 {
18411 line = XFASTINT (w->base_line_number);
18412 linepos = XFASTINT (w->base_line_pos);
18413 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18414 }
18415 else
18416 {
18417 line = 1;
18418 linepos = BUF_BEGV (b);
18419 linepos_byte = BUF_BEGV_BYTE (b);
18420 }
18421
18422 /* Count lines from base line to window start position. */
18423 nlines = display_count_lines (linepos, linepos_byte,
18424 startpos_byte,
18425 startpos, &junk);
18426
18427 topline = nlines + line;
18428
18429 /* Determine a new base line, if the old one is too close
18430 or too far away, or if we did not have one.
18431 "Too close" means it's plausible a scroll-down would
18432 go back past it. */
18433 if (startpos == BUF_BEGV (b))
18434 {
18435 w->base_line_number = make_number (topline);
18436 w->base_line_pos = make_number (BUF_BEGV (b));
18437 }
18438 else if (nlines < height + 25 || nlines > height * 3 + 50
18439 || linepos == BUF_BEGV (b))
18440 {
18441 int limit = BUF_BEGV (b);
18442 int limit_byte = BUF_BEGV_BYTE (b);
18443 int position;
18444 int distance = (height * 2 + 30) * line_number_display_limit_width;
18445
18446 if (startpos - distance > limit)
18447 {
18448 limit = startpos - distance;
18449 limit_byte = CHAR_TO_BYTE (limit);
18450 }
18451
18452 nlines = display_count_lines (startpos, startpos_byte,
18453 limit_byte,
18454 - (height * 2 + 30),
18455 &position);
18456 /* If we couldn't find the lines we wanted within
18457 line_number_display_limit_width chars per line,
18458 give up on line numbers for this window. */
18459 if (position == limit_byte && limit == startpos - distance)
18460 {
18461 w->base_line_pos = w->buffer;
18462 w->base_line_number = Qnil;
18463 goto no_value;
18464 }
18465
18466 w->base_line_number = make_number (topline - nlines);
18467 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18468 }
18469
18470 /* Now count lines from the start pos to point. */
18471 nlines = display_count_lines (startpos, startpos_byte,
18472 PT_BYTE, PT, &junk);
18473
18474 /* Record that we did display the line number. */
18475 line_number_displayed = 1;
18476
18477 /* Make the string to show. */
18478 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18479 return decode_mode_spec_buf;
18480 no_value:
18481 {
18482 char* p = decode_mode_spec_buf;
18483 int pad = field_width - 2;
18484 while (pad-- > 0)
18485 *p++ = ' ';
18486 *p++ = '?';
18487 *p++ = '?';
18488 *p = '\0';
18489 return decode_mode_spec_buf;
18490 }
18491 }
18492 break;
18493
18494 case 'm':
18495 obj = b->mode_name;
18496 break;
18497
18498 case 'n':
18499 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18500 return " Narrow";
18501 break;
18502
18503 case 'p':
18504 {
18505 int pos = marker_position (w->start);
18506 int total = BUF_ZV (b) - BUF_BEGV (b);
18507
18508 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18509 {
18510 if (pos <= BUF_BEGV (b))
18511 return "All";
18512 else
18513 return "Bottom";
18514 }
18515 else if (pos <= BUF_BEGV (b))
18516 return "Top";
18517 else
18518 {
18519 if (total > 1000000)
18520 /* Do it differently for a large value, to avoid overflow. */
18521 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18522 else
18523 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18524 /* We can't normally display a 3-digit number,
18525 so get us a 2-digit number that is close. */
18526 if (total == 100)
18527 total = 99;
18528 sprintf (decode_mode_spec_buf, "%2d%%", total);
18529 return decode_mode_spec_buf;
18530 }
18531 }
18532
18533 /* Display percentage of size above the bottom of the screen. */
18534 case 'P':
18535 {
18536 int toppos = marker_position (w->start);
18537 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18538 int total = BUF_ZV (b) - BUF_BEGV (b);
18539
18540 if (botpos >= BUF_ZV (b))
18541 {
18542 if (toppos <= BUF_BEGV (b))
18543 return "All";
18544 else
18545 return "Bottom";
18546 }
18547 else
18548 {
18549 if (total > 1000000)
18550 /* Do it differently for a large value, to avoid overflow. */
18551 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18552 else
18553 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18554 /* We can't normally display a 3-digit number,
18555 so get us a 2-digit number that is close. */
18556 if (total == 100)
18557 total = 99;
18558 if (toppos <= BUF_BEGV (b))
18559 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18560 else
18561 sprintf (decode_mode_spec_buf, "%2d%%", total);
18562 return decode_mode_spec_buf;
18563 }
18564 }
18565
18566 case 's':
18567 /* status of process */
18568 obj = Fget_buffer_process (Fcurrent_buffer ());
18569 if (NILP (obj))
18570 return "no process";
18571 #ifdef subprocesses
18572 obj = Fsymbol_name (Fprocess_status (obj));
18573 #endif
18574 break;
18575
18576 case '@':
18577 {
18578 int count = inhibit_garbage_collection ();
18579 Lisp_Object val = call1 (intern ("file-remote-p"),
18580 current_buffer->directory);
18581 unbind_to (count, Qnil);
18582
18583 if (NILP (val))
18584 return "-";
18585 else
18586 return "@";
18587 }
18588
18589 case 't': /* indicate TEXT or BINARY */
18590 #ifdef MODE_LINE_BINARY_TEXT
18591 return MODE_LINE_BINARY_TEXT (b);
18592 #else
18593 return "T";
18594 #endif
18595
18596 case 'z':
18597 /* coding-system (not including end-of-line format) */
18598 case 'Z':
18599 /* coding-system (including end-of-line type) */
18600 {
18601 int eol_flag = (c == 'Z');
18602 char *p = decode_mode_spec_buf;
18603
18604 if (! FRAME_WINDOW_P (f))
18605 {
18606 /* No need to mention EOL here--the terminal never needs
18607 to do EOL conversion. */
18608 p = decode_mode_spec_coding (CODING_ID_NAME
18609 (FRAME_KEYBOARD_CODING (f)->id),
18610 p, 0);
18611 p = decode_mode_spec_coding (CODING_ID_NAME
18612 (FRAME_TERMINAL_CODING (f)->id),
18613 p, 0);
18614 }
18615 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18616 p, eol_flag);
18617
18618 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18619 #ifdef subprocesses
18620 obj = Fget_buffer_process (Fcurrent_buffer ());
18621 if (PROCESSP (obj))
18622 {
18623 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18624 p, eol_flag);
18625 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18626 p, eol_flag);
18627 }
18628 #endif /* subprocesses */
18629 #endif /* 0 */
18630 *p = 0;
18631 return decode_mode_spec_buf;
18632 }
18633 }
18634
18635 if (STRINGP (obj))
18636 {
18637 *string = obj;
18638 return (char *) SDATA (obj);
18639 }
18640 else
18641 return "";
18642 }
18643
18644
18645 /* Count up to COUNT lines starting from START / START_BYTE.
18646 But don't go beyond LIMIT_BYTE.
18647 Return the number of lines thus found (always nonnegative).
18648
18649 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18650
18651 static int
18652 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18653 int start, start_byte, limit_byte, count;
18654 int *byte_pos_ptr;
18655 {
18656 register unsigned char *cursor;
18657 unsigned char *base;
18658
18659 register int ceiling;
18660 register unsigned char *ceiling_addr;
18661 int orig_count = count;
18662
18663 /* If we are not in selective display mode,
18664 check only for newlines. */
18665 int selective_display = (!NILP (current_buffer->selective_display)
18666 && !INTEGERP (current_buffer->selective_display));
18667
18668 if (count > 0)
18669 {
18670 while (start_byte < limit_byte)
18671 {
18672 ceiling = BUFFER_CEILING_OF (start_byte);
18673 ceiling = min (limit_byte - 1, ceiling);
18674 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18675 base = (cursor = BYTE_POS_ADDR (start_byte));
18676 while (1)
18677 {
18678 if (selective_display)
18679 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18680 ;
18681 else
18682 while (*cursor != '\n' && ++cursor != ceiling_addr)
18683 ;
18684
18685 if (cursor != ceiling_addr)
18686 {
18687 if (--count == 0)
18688 {
18689 start_byte += cursor - base + 1;
18690 *byte_pos_ptr = start_byte;
18691 return orig_count;
18692 }
18693 else
18694 if (++cursor == ceiling_addr)
18695 break;
18696 }
18697 else
18698 break;
18699 }
18700 start_byte += cursor - base;
18701 }
18702 }
18703 else
18704 {
18705 while (start_byte > limit_byte)
18706 {
18707 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18708 ceiling = max (limit_byte, ceiling);
18709 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18710 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18711 while (1)
18712 {
18713 if (selective_display)
18714 while (--cursor != ceiling_addr
18715 && *cursor != '\n' && *cursor != 015)
18716 ;
18717 else
18718 while (--cursor != ceiling_addr && *cursor != '\n')
18719 ;
18720
18721 if (cursor != ceiling_addr)
18722 {
18723 if (++count == 0)
18724 {
18725 start_byte += cursor - base + 1;
18726 *byte_pos_ptr = start_byte;
18727 /* When scanning backwards, we should
18728 not count the newline posterior to which we stop. */
18729 return - orig_count - 1;
18730 }
18731 }
18732 else
18733 break;
18734 }
18735 /* Here we add 1 to compensate for the last decrement
18736 of CURSOR, which took it past the valid range. */
18737 start_byte += cursor - base + 1;
18738 }
18739 }
18740
18741 *byte_pos_ptr = limit_byte;
18742
18743 if (count < 0)
18744 return - orig_count + count;
18745 return orig_count - count;
18746
18747 }
18748
18749
18750 \f
18751 /***********************************************************************
18752 Displaying strings
18753 ***********************************************************************/
18754
18755 /* Display a NUL-terminated string, starting with index START.
18756
18757 If STRING is non-null, display that C string. Otherwise, the Lisp
18758 string LISP_STRING is displayed. There's a case that STRING is
18759 non-null and LISP_STRING is not nil. It means STRING is a string
18760 data of LISP_STRING. In that case, we display LISP_STRING while
18761 ignoring its text properties.
18762
18763 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18764 FACE_STRING. Display STRING or LISP_STRING with the face at
18765 FACE_STRING_POS in FACE_STRING:
18766
18767 Display the string in the environment given by IT, but use the
18768 standard display table, temporarily.
18769
18770 FIELD_WIDTH is the minimum number of output glyphs to produce.
18771 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18772 with spaces. If STRING has more characters, more than FIELD_WIDTH
18773 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18774
18775 PRECISION is the maximum number of characters to output from
18776 STRING. PRECISION < 0 means don't truncate the string.
18777
18778 This is roughly equivalent to printf format specifiers:
18779
18780 FIELD_WIDTH PRECISION PRINTF
18781 ----------------------------------------
18782 -1 -1 %s
18783 -1 10 %.10s
18784 10 -1 %10s
18785 20 10 %20.10s
18786
18787 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18788 display them, and < 0 means obey the current buffer's value of
18789 enable_multibyte_characters.
18790
18791 Value is the number of columns displayed. */
18792
18793 static int
18794 display_string (string, lisp_string, face_string, face_string_pos,
18795 start, it, field_width, precision, max_x, multibyte)
18796 unsigned char *string;
18797 Lisp_Object lisp_string;
18798 Lisp_Object face_string;
18799 EMACS_INT face_string_pos;
18800 EMACS_INT start;
18801 struct it *it;
18802 int field_width, precision, max_x;
18803 int multibyte;
18804 {
18805 int hpos_at_start = it->hpos;
18806 int saved_face_id = it->face_id;
18807 struct glyph_row *row = it->glyph_row;
18808
18809 /* Initialize the iterator IT for iteration over STRING beginning
18810 with index START. */
18811 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
18812 precision, field_width, multibyte);
18813 if (string && STRINGP (lisp_string))
18814 /* LISP_STRING is the one returned by decode_mode_spec. We should
18815 ignore its text properties. */
18816 it->stop_charpos = -1;
18817
18818 /* If displaying STRING, set up the face of the iterator
18819 from LISP_STRING, if that's given. */
18820 if (STRINGP (face_string))
18821 {
18822 EMACS_INT endptr;
18823 struct face *face;
18824
18825 it->face_id
18826 = face_at_string_position (it->w, face_string, face_string_pos,
18827 0, it->region_beg_charpos,
18828 it->region_end_charpos,
18829 &endptr, it->base_face_id, 0);
18830 face = FACE_FROM_ID (it->f, it->face_id);
18831 it->face_box_p = face->box != FACE_NO_BOX;
18832 }
18833
18834 /* Set max_x to the maximum allowed X position. Don't let it go
18835 beyond the right edge of the window. */
18836 if (max_x <= 0)
18837 max_x = it->last_visible_x;
18838 else
18839 max_x = min (max_x, it->last_visible_x);
18840
18841 /* Skip over display elements that are not visible. because IT->w is
18842 hscrolled. */
18843 if (it->current_x < it->first_visible_x)
18844 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18845 MOVE_TO_POS | MOVE_TO_X);
18846
18847 row->ascent = it->max_ascent;
18848 row->height = it->max_ascent + it->max_descent;
18849 row->phys_ascent = it->max_phys_ascent;
18850 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18851 row->extra_line_spacing = it->max_extra_line_spacing;
18852
18853 /* This condition is for the case that we are called with current_x
18854 past last_visible_x. */
18855 while (it->current_x < max_x)
18856 {
18857 int x_before, x, n_glyphs_before, i, nglyphs;
18858
18859 /* Get the next display element. */
18860 if (!get_next_display_element (it))
18861 break;
18862
18863 /* Produce glyphs. */
18864 x_before = it->current_x;
18865 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18866 PRODUCE_GLYPHS (it);
18867
18868 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18869 i = 0;
18870 x = x_before;
18871 while (i < nglyphs)
18872 {
18873 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18874
18875 if (it->line_wrap != TRUNCATE
18876 && x + glyph->pixel_width > max_x)
18877 {
18878 /* End of continued line or max_x reached. */
18879 if (CHAR_GLYPH_PADDING_P (*glyph))
18880 {
18881 /* A wide character is unbreakable. */
18882 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18883 it->current_x = x_before;
18884 }
18885 else
18886 {
18887 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18888 it->current_x = x;
18889 }
18890 break;
18891 }
18892 else if (x + glyph->pixel_width >= it->first_visible_x)
18893 {
18894 /* Glyph is at least partially visible. */
18895 ++it->hpos;
18896 if (x < it->first_visible_x)
18897 it->glyph_row->x = x - it->first_visible_x;
18898 }
18899 else
18900 {
18901 /* Glyph is off the left margin of the display area.
18902 Should not happen. */
18903 abort ();
18904 }
18905
18906 row->ascent = max (row->ascent, it->max_ascent);
18907 row->height = max (row->height, it->max_ascent + it->max_descent);
18908 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18909 row->phys_height = max (row->phys_height,
18910 it->max_phys_ascent + it->max_phys_descent);
18911 row->extra_line_spacing = max (row->extra_line_spacing,
18912 it->max_extra_line_spacing);
18913 x += glyph->pixel_width;
18914 ++i;
18915 }
18916
18917 /* Stop if max_x reached. */
18918 if (i < nglyphs)
18919 break;
18920
18921 /* Stop at line ends. */
18922 if (ITERATOR_AT_END_OF_LINE_P (it))
18923 {
18924 it->continuation_lines_width = 0;
18925 break;
18926 }
18927
18928 set_iterator_to_next (it, 1);
18929
18930 /* Stop if truncating at the right edge. */
18931 if (it->line_wrap == TRUNCATE
18932 && it->current_x >= it->last_visible_x)
18933 {
18934 /* Add truncation mark, but don't do it if the line is
18935 truncated at a padding space. */
18936 if (IT_CHARPOS (*it) < it->string_nchars)
18937 {
18938 if (!FRAME_WINDOW_P (it->f))
18939 {
18940 int i, n;
18941
18942 if (it->current_x > it->last_visible_x)
18943 {
18944 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18945 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18946 break;
18947 for (n = row->used[TEXT_AREA]; i < n; ++i)
18948 {
18949 row->used[TEXT_AREA] = i;
18950 produce_special_glyphs (it, IT_TRUNCATION);
18951 }
18952 }
18953 produce_special_glyphs (it, IT_TRUNCATION);
18954 }
18955 it->glyph_row->truncated_on_right_p = 1;
18956 }
18957 break;
18958 }
18959 }
18960
18961 /* Maybe insert a truncation at the left. */
18962 if (it->first_visible_x
18963 && IT_CHARPOS (*it) > 0)
18964 {
18965 if (!FRAME_WINDOW_P (it->f))
18966 insert_left_trunc_glyphs (it);
18967 it->glyph_row->truncated_on_left_p = 1;
18968 }
18969
18970 it->face_id = saved_face_id;
18971
18972 /* Value is number of columns displayed. */
18973 return it->hpos - hpos_at_start;
18974 }
18975
18976
18977 \f
18978 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18979 appears as an element of LIST or as the car of an element of LIST.
18980 If PROPVAL is a list, compare each element against LIST in that
18981 way, and return 1/2 if any element of PROPVAL is found in LIST.
18982 Otherwise return 0. This function cannot quit.
18983 The return value is 2 if the text is invisible but with an ellipsis
18984 and 1 if it's invisible and without an ellipsis. */
18985
18986 int
18987 invisible_p (propval, list)
18988 register Lisp_Object propval;
18989 Lisp_Object list;
18990 {
18991 register Lisp_Object tail, proptail;
18992
18993 for (tail = list; CONSP (tail); tail = XCDR (tail))
18994 {
18995 register Lisp_Object tem;
18996 tem = XCAR (tail);
18997 if (EQ (propval, tem))
18998 return 1;
18999 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19000 return NILP (XCDR (tem)) ? 1 : 2;
19001 }
19002
19003 if (CONSP (propval))
19004 {
19005 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19006 {
19007 Lisp_Object propelt;
19008 propelt = XCAR (proptail);
19009 for (tail = list; CONSP (tail); tail = XCDR (tail))
19010 {
19011 register Lisp_Object tem;
19012 tem = XCAR (tail);
19013 if (EQ (propelt, tem))
19014 return 1;
19015 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19016 return NILP (XCDR (tem)) ? 1 : 2;
19017 }
19018 }
19019 }
19020
19021 return 0;
19022 }
19023
19024 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19025 doc: /* Non-nil if the property makes the text invisible.
19026 POS-OR-PROP can be a marker or number, in which case it is taken to be
19027 a position in the current buffer and the value of the `invisible' property
19028 is checked; or it can be some other value, which is then presumed to be the
19029 value of the `invisible' property of the text of interest.
19030 The non-nil value returned can be t for truly invisible text or something
19031 else if the text is replaced by an ellipsis. */)
19032 (pos_or_prop)
19033 Lisp_Object pos_or_prop;
19034 {
19035 Lisp_Object prop
19036 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19037 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19038 : pos_or_prop);
19039 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19040 return (invis == 0 ? Qnil
19041 : invis == 1 ? Qt
19042 : make_number (invis));
19043 }
19044
19045 /* Calculate a width or height in pixels from a specification using
19046 the following elements:
19047
19048 SPEC ::=
19049 NUM - a (fractional) multiple of the default font width/height
19050 (NUM) - specifies exactly NUM pixels
19051 UNIT - a fixed number of pixels, see below.
19052 ELEMENT - size of a display element in pixels, see below.
19053 (NUM . SPEC) - equals NUM * SPEC
19054 (+ SPEC SPEC ...) - add pixel values
19055 (- SPEC SPEC ...) - subtract pixel values
19056 (- SPEC) - negate pixel value
19057
19058 NUM ::=
19059 INT or FLOAT - a number constant
19060 SYMBOL - use symbol's (buffer local) variable binding.
19061
19062 UNIT ::=
19063 in - pixels per inch *)
19064 mm - pixels per 1/1000 meter *)
19065 cm - pixels per 1/100 meter *)
19066 width - width of current font in pixels.
19067 height - height of current font in pixels.
19068
19069 *) using the ratio(s) defined in display-pixels-per-inch.
19070
19071 ELEMENT ::=
19072
19073 left-fringe - left fringe width in pixels
19074 right-fringe - right fringe width in pixels
19075
19076 left-margin - left margin width in pixels
19077 right-margin - right margin width in pixels
19078
19079 scroll-bar - scroll-bar area width in pixels
19080
19081 Examples:
19082
19083 Pixels corresponding to 5 inches:
19084 (5 . in)
19085
19086 Total width of non-text areas on left side of window (if scroll-bar is on left):
19087 '(space :width (+ left-fringe left-margin scroll-bar))
19088
19089 Align to first text column (in header line):
19090 '(space :align-to 0)
19091
19092 Align to middle of text area minus half the width of variable `my-image'
19093 containing a loaded image:
19094 '(space :align-to (0.5 . (- text my-image)))
19095
19096 Width of left margin minus width of 1 character in the default font:
19097 '(space :width (- left-margin 1))
19098
19099 Width of left margin minus width of 2 characters in the current font:
19100 '(space :width (- left-margin (2 . width)))
19101
19102 Center 1 character over left-margin (in header line):
19103 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19104
19105 Different ways to express width of left fringe plus left margin minus one pixel:
19106 '(space :width (- (+ left-fringe left-margin) (1)))
19107 '(space :width (+ left-fringe left-margin (- (1))))
19108 '(space :width (+ left-fringe left-margin (-1)))
19109
19110 */
19111
19112 #define NUMVAL(X) \
19113 ((INTEGERP (X) || FLOATP (X)) \
19114 ? XFLOATINT (X) \
19115 : - 1)
19116
19117 int
19118 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19119 double *res;
19120 struct it *it;
19121 Lisp_Object prop;
19122 struct font *font;
19123 int width_p, *align_to;
19124 {
19125 double pixels;
19126
19127 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19128 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19129
19130 if (NILP (prop))
19131 return OK_PIXELS (0);
19132
19133 xassert (FRAME_LIVE_P (it->f));
19134
19135 if (SYMBOLP (prop))
19136 {
19137 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19138 {
19139 char *unit = SDATA (SYMBOL_NAME (prop));
19140
19141 if (unit[0] == 'i' && unit[1] == 'n')
19142 pixels = 1.0;
19143 else if (unit[0] == 'm' && unit[1] == 'm')
19144 pixels = 25.4;
19145 else if (unit[0] == 'c' && unit[1] == 'm')
19146 pixels = 2.54;
19147 else
19148 pixels = 0;
19149 if (pixels > 0)
19150 {
19151 double ppi;
19152 #ifdef HAVE_WINDOW_SYSTEM
19153 if (FRAME_WINDOW_P (it->f)
19154 && (ppi = (width_p
19155 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19156 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19157 ppi > 0))
19158 return OK_PIXELS (ppi / pixels);
19159 #endif
19160
19161 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19162 || (CONSP (Vdisplay_pixels_per_inch)
19163 && (ppi = (width_p
19164 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19165 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19166 ppi > 0)))
19167 return OK_PIXELS (ppi / pixels);
19168
19169 return 0;
19170 }
19171 }
19172
19173 #ifdef HAVE_WINDOW_SYSTEM
19174 if (EQ (prop, Qheight))
19175 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19176 if (EQ (prop, Qwidth))
19177 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19178 #else
19179 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19180 return OK_PIXELS (1);
19181 #endif
19182
19183 if (EQ (prop, Qtext))
19184 return OK_PIXELS (width_p
19185 ? window_box_width (it->w, TEXT_AREA)
19186 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19187
19188 if (align_to && *align_to < 0)
19189 {
19190 *res = 0;
19191 if (EQ (prop, Qleft))
19192 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19193 if (EQ (prop, Qright))
19194 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19195 if (EQ (prop, Qcenter))
19196 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19197 + window_box_width (it->w, TEXT_AREA) / 2);
19198 if (EQ (prop, Qleft_fringe))
19199 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19200 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19201 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19202 if (EQ (prop, Qright_fringe))
19203 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19204 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19205 : window_box_right_offset (it->w, TEXT_AREA));
19206 if (EQ (prop, Qleft_margin))
19207 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19208 if (EQ (prop, Qright_margin))
19209 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19210 if (EQ (prop, Qscroll_bar))
19211 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19212 ? 0
19213 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19214 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19215 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19216 : 0)));
19217 }
19218 else
19219 {
19220 if (EQ (prop, Qleft_fringe))
19221 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19222 if (EQ (prop, Qright_fringe))
19223 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19224 if (EQ (prop, Qleft_margin))
19225 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19226 if (EQ (prop, Qright_margin))
19227 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19228 if (EQ (prop, Qscroll_bar))
19229 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19230 }
19231
19232 prop = Fbuffer_local_value (prop, it->w->buffer);
19233 }
19234
19235 if (INTEGERP (prop) || FLOATP (prop))
19236 {
19237 int base_unit = (width_p
19238 ? FRAME_COLUMN_WIDTH (it->f)
19239 : FRAME_LINE_HEIGHT (it->f));
19240 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19241 }
19242
19243 if (CONSP (prop))
19244 {
19245 Lisp_Object car = XCAR (prop);
19246 Lisp_Object cdr = XCDR (prop);
19247
19248 if (SYMBOLP (car))
19249 {
19250 #ifdef HAVE_WINDOW_SYSTEM
19251 if (FRAME_WINDOW_P (it->f)
19252 && valid_image_p (prop))
19253 {
19254 int id = lookup_image (it->f, prop);
19255 struct image *img = IMAGE_FROM_ID (it->f, id);
19256
19257 return OK_PIXELS (width_p ? img->width : img->height);
19258 }
19259 #endif
19260 if (EQ (car, Qplus) || EQ (car, Qminus))
19261 {
19262 int first = 1;
19263 double px;
19264
19265 pixels = 0;
19266 while (CONSP (cdr))
19267 {
19268 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19269 font, width_p, align_to))
19270 return 0;
19271 if (first)
19272 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19273 else
19274 pixels += px;
19275 cdr = XCDR (cdr);
19276 }
19277 if (EQ (car, Qminus))
19278 pixels = -pixels;
19279 return OK_PIXELS (pixels);
19280 }
19281
19282 car = Fbuffer_local_value (car, it->w->buffer);
19283 }
19284
19285 if (INTEGERP (car) || FLOATP (car))
19286 {
19287 double fact;
19288 pixels = XFLOATINT (car);
19289 if (NILP (cdr))
19290 return OK_PIXELS (pixels);
19291 if (calc_pixel_width_or_height (&fact, it, cdr,
19292 font, width_p, align_to))
19293 return OK_PIXELS (pixels * fact);
19294 return 0;
19295 }
19296
19297 return 0;
19298 }
19299
19300 return 0;
19301 }
19302
19303 \f
19304 /***********************************************************************
19305 Glyph Display
19306 ***********************************************************************/
19307
19308 #ifdef HAVE_WINDOW_SYSTEM
19309
19310 #if GLYPH_DEBUG
19311
19312 void
19313 dump_glyph_string (s)
19314 struct glyph_string *s;
19315 {
19316 fprintf (stderr, "glyph string\n");
19317 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19318 s->x, s->y, s->width, s->height);
19319 fprintf (stderr, " ybase = %d\n", s->ybase);
19320 fprintf (stderr, " hl = %d\n", s->hl);
19321 fprintf (stderr, " left overhang = %d, right = %d\n",
19322 s->left_overhang, s->right_overhang);
19323 fprintf (stderr, " nchars = %d\n", s->nchars);
19324 fprintf (stderr, " extends to end of line = %d\n",
19325 s->extends_to_end_of_line_p);
19326 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19327 fprintf (stderr, " bg width = %d\n", s->background_width);
19328 }
19329
19330 #endif /* GLYPH_DEBUG */
19331
19332 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19333 of XChar2b structures for S; it can't be allocated in
19334 init_glyph_string because it must be allocated via `alloca'. W
19335 is the window on which S is drawn. ROW and AREA are the glyph row
19336 and area within the row from which S is constructed. START is the
19337 index of the first glyph structure covered by S. HL is a
19338 face-override for drawing S. */
19339
19340 #ifdef HAVE_NTGUI
19341 #define OPTIONAL_HDC(hdc) hdc,
19342 #define DECLARE_HDC(hdc) HDC hdc;
19343 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19344 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19345 #endif
19346
19347 #ifndef OPTIONAL_HDC
19348 #define OPTIONAL_HDC(hdc)
19349 #define DECLARE_HDC(hdc)
19350 #define ALLOCATE_HDC(hdc, f)
19351 #define RELEASE_HDC(hdc, f)
19352 #endif
19353
19354 static void
19355 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19356 struct glyph_string *s;
19357 DECLARE_HDC (hdc)
19358 XChar2b *char2b;
19359 struct window *w;
19360 struct glyph_row *row;
19361 enum glyph_row_area area;
19362 int start;
19363 enum draw_glyphs_face hl;
19364 {
19365 bzero (s, sizeof *s);
19366 s->w = w;
19367 s->f = XFRAME (w->frame);
19368 #ifdef HAVE_NTGUI
19369 s->hdc = hdc;
19370 #endif
19371 s->display = FRAME_X_DISPLAY (s->f);
19372 s->window = FRAME_X_WINDOW (s->f);
19373 s->char2b = char2b;
19374 s->hl = hl;
19375 s->row = row;
19376 s->area = area;
19377 s->first_glyph = row->glyphs[area] + start;
19378 s->height = row->height;
19379 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19380 s->ybase = s->y + row->ascent;
19381 }
19382
19383
19384 /* Append the list of glyph strings with head H and tail T to the list
19385 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19386
19387 static INLINE void
19388 append_glyph_string_lists (head, tail, h, t)
19389 struct glyph_string **head, **tail;
19390 struct glyph_string *h, *t;
19391 {
19392 if (h)
19393 {
19394 if (*head)
19395 (*tail)->next = h;
19396 else
19397 *head = h;
19398 h->prev = *tail;
19399 *tail = t;
19400 }
19401 }
19402
19403
19404 /* Prepend the list of glyph strings with head H and tail T to the
19405 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19406 result. */
19407
19408 static INLINE void
19409 prepend_glyph_string_lists (head, tail, h, t)
19410 struct glyph_string **head, **tail;
19411 struct glyph_string *h, *t;
19412 {
19413 if (h)
19414 {
19415 if (*head)
19416 (*head)->prev = t;
19417 else
19418 *tail = t;
19419 t->next = *head;
19420 *head = h;
19421 }
19422 }
19423
19424
19425 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19426 Set *HEAD and *TAIL to the resulting list. */
19427
19428 static INLINE void
19429 append_glyph_string (head, tail, s)
19430 struct glyph_string **head, **tail;
19431 struct glyph_string *s;
19432 {
19433 s->next = s->prev = NULL;
19434 append_glyph_string_lists (head, tail, s, s);
19435 }
19436
19437
19438 /* Get face and two-byte form of character C in face FACE_ID on frame
19439 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19440 means we want to display multibyte text. DISPLAY_P non-zero means
19441 make sure that X resources for the face returned are allocated.
19442 Value is a pointer to a realized face that is ready for display if
19443 DISPLAY_P is non-zero. */
19444
19445 static INLINE struct face *
19446 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19447 struct frame *f;
19448 int c, face_id;
19449 XChar2b *char2b;
19450 int multibyte_p, display_p;
19451 {
19452 struct face *face = FACE_FROM_ID (f, face_id);
19453
19454 if (face->font)
19455 {
19456 unsigned code = face->font->driver->encode_char (face->font, c);
19457
19458 if (code != FONT_INVALID_CODE)
19459 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19460 else
19461 STORE_XCHAR2B (char2b, 0, 0);
19462 }
19463
19464 /* Make sure X resources of the face are allocated. */
19465 #ifdef HAVE_X_WINDOWS
19466 if (display_p)
19467 #endif
19468 {
19469 xassert (face != NULL);
19470 PREPARE_FACE_FOR_DISPLAY (f, face);
19471 }
19472
19473 return face;
19474 }
19475
19476
19477 /* Get face and two-byte form of character glyph GLYPH on frame F.
19478 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19479 a pointer to a realized face that is ready for display. */
19480
19481 static INLINE struct face *
19482 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19483 struct frame *f;
19484 struct glyph *glyph;
19485 XChar2b *char2b;
19486 int *two_byte_p;
19487 {
19488 struct face *face;
19489
19490 xassert (glyph->type == CHAR_GLYPH);
19491 face = FACE_FROM_ID (f, glyph->face_id);
19492
19493 if (two_byte_p)
19494 *two_byte_p = 0;
19495
19496 if (face->font)
19497 {
19498 unsigned code;
19499
19500 if (CHAR_BYTE8_P (glyph->u.ch))
19501 code = CHAR_TO_BYTE8 (glyph->u.ch);
19502 else
19503 code = face->font->driver->encode_char (face->font, glyph->u.ch);
19504
19505 if (code != FONT_INVALID_CODE)
19506 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19507 else
19508 STORE_XCHAR2B (char2b, 0, 0);
19509 }
19510
19511 /* Make sure X resources of the face are allocated. */
19512 xassert (face != NULL);
19513 PREPARE_FACE_FOR_DISPLAY (f, face);
19514 return face;
19515 }
19516
19517
19518 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
19519 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
19520
19521 static INLINE int
19522 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
19523 {
19524 unsigned code;
19525
19526 if (CHAR_BYTE8_P (c))
19527 code = CHAR_TO_BYTE8 (c);
19528 else
19529 code = font->driver->encode_char (font, c);
19530
19531 if (code == FONT_INVALID_CODE)
19532 return 0;
19533 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19534 return 1;
19535 }
19536
19537
19538 /* Fill glyph string S with composition components specified by S->cmp.
19539
19540 BASE_FACE is the base face of the composition.
19541 S->cmp_from is the index of the first component for S.
19542
19543 OVERLAPS non-zero means S should draw the foreground only, and use
19544 its physical height for clipping. See also draw_glyphs.
19545
19546 Value is the index of a component not in S. */
19547
19548 static int
19549 fill_composite_glyph_string (s, base_face, overlaps)
19550 struct glyph_string *s;
19551 struct face *base_face;
19552 int overlaps;
19553 {
19554 int i;
19555 /* For all glyphs of this composition, starting at the offset
19556 S->cmp_from, until we reach the end of the definition or encounter a
19557 glyph that requires the different face, add it to S. */
19558 struct face *face;
19559
19560 xassert (s);
19561
19562 s->for_overlaps = overlaps;
19563 s->face = NULL;
19564 s->font = NULL;
19565 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19566 {
19567 int c = COMPOSITION_GLYPH (s->cmp, i);
19568
19569 if (c != '\t')
19570 {
19571 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19572 -1, Qnil);
19573
19574 face = get_char_face_and_encoding (s->f, c, face_id,
19575 s->char2b + i, 1, 1);
19576 if (face)
19577 {
19578 if (! s->face)
19579 {
19580 s->face = face;
19581 s->font = s->face->font;
19582 }
19583 else if (s->face != face)
19584 break;
19585 }
19586 }
19587 ++s->nchars;
19588 }
19589 s->cmp_to = i;
19590
19591 /* All glyph strings for the same composition has the same width,
19592 i.e. the width set for the first component of the composition. */
19593 s->width = s->first_glyph->pixel_width;
19594
19595 /* If the specified font could not be loaded, use the frame's
19596 default font, but record the fact that we couldn't load it in
19597 the glyph string so that we can draw rectangles for the
19598 characters of the glyph string. */
19599 if (s->font == NULL)
19600 {
19601 s->font_not_found_p = 1;
19602 s->font = FRAME_FONT (s->f);
19603 }
19604
19605 /* Adjust base line for subscript/superscript text. */
19606 s->ybase += s->first_glyph->voffset;
19607
19608 /* This glyph string must always be drawn with 16-bit functions. */
19609 s->two_byte_p = 1;
19610
19611 return s->cmp_to;
19612 }
19613
19614 static int
19615 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19616 struct glyph_string *s;
19617 int face_id;
19618 int start, end, overlaps;
19619 {
19620 struct glyph *glyph, *last;
19621 Lisp_Object lgstring;
19622 int i;
19623
19624 s->for_overlaps = overlaps;
19625 glyph = s->row->glyphs[s->area] + start;
19626 last = s->row->glyphs[s->area] + end;
19627 s->cmp_id = glyph->u.cmp.id;
19628 s->cmp_from = glyph->u.cmp.from;
19629 s->cmp_to = glyph->u.cmp.to + 1;
19630 s->face = FACE_FROM_ID (s->f, face_id);
19631 lgstring = composition_gstring_from_id (s->cmp_id);
19632 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19633 glyph++;
19634 while (glyph < last
19635 && glyph->u.cmp.automatic
19636 && glyph->u.cmp.id == s->cmp_id
19637 && s->cmp_to == glyph->u.cmp.from)
19638 s->cmp_to = (glyph++)->u.cmp.to + 1;
19639
19640 for (i = s->cmp_from; i < s->cmp_to; i++)
19641 {
19642 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19643 unsigned code = LGLYPH_CODE (lglyph);
19644
19645 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19646 }
19647 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19648 return glyph - s->row->glyphs[s->area];
19649 }
19650
19651
19652 /* Fill glyph string S from a sequence of character glyphs.
19653
19654 FACE_ID is the face id of the string. START is the index of the
19655 first glyph to consider, END is the index of the last + 1.
19656 OVERLAPS non-zero means S should draw the foreground only, and use
19657 its physical height for clipping. See also draw_glyphs.
19658
19659 Value is the index of the first glyph not in S. */
19660
19661 static int
19662 fill_glyph_string (s, face_id, start, end, overlaps)
19663 struct glyph_string *s;
19664 int face_id;
19665 int start, end, overlaps;
19666 {
19667 struct glyph *glyph, *last;
19668 int voffset;
19669 int glyph_not_available_p;
19670
19671 xassert (s->f == XFRAME (s->w->frame));
19672 xassert (s->nchars == 0);
19673 xassert (start >= 0 && end > start);
19674
19675 s->for_overlaps = overlaps;
19676 glyph = s->row->glyphs[s->area] + start;
19677 last = s->row->glyphs[s->area] + end;
19678 voffset = glyph->voffset;
19679 s->padding_p = glyph->padding_p;
19680 glyph_not_available_p = glyph->glyph_not_available_p;
19681
19682 while (glyph < last
19683 && glyph->type == CHAR_GLYPH
19684 && glyph->voffset == voffset
19685 /* Same face id implies same font, nowadays. */
19686 && glyph->face_id == face_id
19687 && glyph->glyph_not_available_p == glyph_not_available_p)
19688 {
19689 int two_byte_p;
19690
19691 s->face = get_glyph_face_and_encoding (s->f, glyph,
19692 s->char2b + s->nchars,
19693 &two_byte_p);
19694 s->two_byte_p = two_byte_p;
19695 ++s->nchars;
19696 xassert (s->nchars <= end - start);
19697 s->width += glyph->pixel_width;
19698 if (glyph++->padding_p != s->padding_p)
19699 break;
19700 }
19701
19702 s->font = s->face->font;
19703
19704 /* If the specified font could not be loaded, use the frame's font,
19705 but record the fact that we couldn't load it in
19706 S->font_not_found_p so that we can draw rectangles for the
19707 characters of the glyph string. */
19708 if (s->font == NULL || glyph_not_available_p)
19709 {
19710 s->font_not_found_p = 1;
19711 s->font = FRAME_FONT (s->f);
19712 }
19713
19714 /* Adjust base line for subscript/superscript text. */
19715 s->ybase += voffset;
19716
19717 xassert (s->face && s->face->gc);
19718 return glyph - s->row->glyphs[s->area];
19719 }
19720
19721
19722 /* Fill glyph string S from image glyph S->first_glyph. */
19723
19724 static void
19725 fill_image_glyph_string (s)
19726 struct glyph_string *s;
19727 {
19728 xassert (s->first_glyph->type == IMAGE_GLYPH);
19729 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19730 xassert (s->img);
19731 s->slice = s->first_glyph->slice;
19732 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19733 s->font = s->face->font;
19734 s->width = s->first_glyph->pixel_width;
19735
19736 /* Adjust base line for subscript/superscript text. */
19737 s->ybase += s->first_glyph->voffset;
19738 }
19739
19740
19741 /* Fill glyph string S from a sequence of stretch glyphs.
19742
19743 ROW is the glyph row in which the glyphs are found, AREA is the
19744 area within the row. START is the index of the first glyph to
19745 consider, END is the index of the last + 1.
19746
19747 Value is the index of the first glyph not in S. */
19748
19749 static int
19750 fill_stretch_glyph_string (s, row, area, start, end)
19751 struct glyph_string *s;
19752 struct glyph_row *row;
19753 enum glyph_row_area area;
19754 int start, end;
19755 {
19756 struct glyph *glyph, *last;
19757 int voffset, face_id;
19758
19759 xassert (s->first_glyph->type == STRETCH_GLYPH);
19760
19761 glyph = s->row->glyphs[s->area] + start;
19762 last = s->row->glyphs[s->area] + end;
19763 face_id = glyph->face_id;
19764 s->face = FACE_FROM_ID (s->f, face_id);
19765 s->font = s->face->font;
19766 s->width = glyph->pixel_width;
19767 s->nchars = 1;
19768 voffset = glyph->voffset;
19769
19770 for (++glyph;
19771 (glyph < last
19772 && glyph->type == STRETCH_GLYPH
19773 && glyph->voffset == voffset
19774 && glyph->face_id == face_id);
19775 ++glyph)
19776 s->width += glyph->pixel_width;
19777
19778 /* Adjust base line for subscript/superscript text. */
19779 s->ybase += voffset;
19780
19781 /* The case that face->gc == 0 is handled when drawing the glyph
19782 string by calling PREPARE_FACE_FOR_DISPLAY. */
19783 xassert (s->face);
19784 return glyph - s->row->glyphs[s->area];
19785 }
19786
19787 static struct font_metrics *
19788 get_per_char_metric (f, font, char2b)
19789 struct frame *f;
19790 struct font *font;
19791 XChar2b *char2b;
19792 {
19793 static struct font_metrics metrics;
19794 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19795
19796 if (! font || code == FONT_INVALID_CODE)
19797 return NULL;
19798 font->driver->text_extents (font, &code, 1, &metrics);
19799 return &metrics;
19800 }
19801
19802 /* EXPORT for RIF:
19803 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19804 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19805 assumed to be zero. */
19806
19807 void
19808 x_get_glyph_overhangs (glyph, f, left, right)
19809 struct glyph *glyph;
19810 struct frame *f;
19811 int *left, *right;
19812 {
19813 *left = *right = 0;
19814
19815 if (glyph->type == CHAR_GLYPH)
19816 {
19817 struct face *face;
19818 XChar2b char2b;
19819 struct font_metrics *pcm;
19820
19821 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19822 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19823 {
19824 if (pcm->rbearing > pcm->width)
19825 *right = pcm->rbearing - pcm->width;
19826 if (pcm->lbearing < 0)
19827 *left = -pcm->lbearing;
19828 }
19829 }
19830 else if (glyph->type == COMPOSITE_GLYPH)
19831 {
19832 if (! glyph->u.cmp.automatic)
19833 {
19834 struct composition *cmp = composition_table[glyph->u.cmp.id];
19835
19836 if (cmp->rbearing > cmp->pixel_width)
19837 *right = cmp->rbearing - cmp->pixel_width;
19838 if (cmp->lbearing < 0)
19839 *left = - cmp->lbearing;
19840 }
19841 else
19842 {
19843 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19844 struct font_metrics metrics;
19845
19846 composition_gstring_width (gstring, glyph->u.cmp.from,
19847 glyph->u.cmp.to + 1, &metrics);
19848 if (metrics.rbearing > metrics.width)
19849 *right = metrics.rbearing - metrics.width;
19850 if (metrics.lbearing < 0)
19851 *left = - metrics.lbearing;
19852 }
19853 }
19854 }
19855
19856
19857 /* Return the index of the first glyph preceding glyph string S that
19858 is overwritten by S because of S's left overhang. Value is -1
19859 if no glyphs are overwritten. */
19860
19861 static int
19862 left_overwritten (s)
19863 struct glyph_string *s;
19864 {
19865 int k;
19866
19867 if (s->left_overhang)
19868 {
19869 int x = 0, i;
19870 struct glyph *glyphs = s->row->glyphs[s->area];
19871 int first = s->first_glyph - glyphs;
19872
19873 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19874 x -= glyphs[i].pixel_width;
19875
19876 k = i + 1;
19877 }
19878 else
19879 k = -1;
19880
19881 return k;
19882 }
19883
19884
19885 /* Return the index of the first glyph preceding glyph string S that
19886 is overwriting S because of its right overhang. Value is -1 if no
19887 glyph in front of S overwrites S. */
19888
19889 static int
19890 left_overwriting (s)
19891 struct glyph_string *s;
19892 {
19893 int i, k, x;
19894 struct glyph *glyphs = s->row->glyphs[s->area];
19895 int first = s->first_glyph - glyphs;
19896
19897 k = -1;
19898 x = 0;
19899 for (i = first - 1; i >= 0; --i)
19900 {
19901 int left, right;
19902 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19903 if (x + right > 0)
19904 k = i;
19905 x -= glyphs[i].pixel_width;
19906 }
19907
19908 return k;
19909 }
19910
19911
19912 /* Return the index of the last glyph following glyph string S that is
19913 overwritten by S because of S's right overhang. Value is -1 if
19914 no such glyph is found. */
19915
19916 static int
19917 right_overwritten (s)
19918 struct glyph_string *s;
19919 {
19920 int k = -1;
19921
19922 if (s->right_overhang)
19923 {
19924 int x = 0, i;
19925 struct glyph *glyphs = s->row->glyphs[s->area];
19926 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19927 int end = s->row->used[s->area];
19928
19929 for (i = first; i < end && s->right_overhang > x; ++i)
19930 x += glyphs[i].pixel_width;
19931
19932 k = i;
19933 }
19934
19935 return k;
19936 }
19937
19938
19939 /* Return the index of the last glyph following glyph string S that
19940 overwrites S because of its left overhang. Value is negative
19941 if no such glyph is found. */
19942
19943 static int
19944 right_overwriting (s)
19945 struct glyph_string *s;
19946 {
19947 int i, k, x;
19948 int end = s->row->used[s->area];
19949 struct glyph *glyphs = s->row->glyphs[s->area];
19950 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19951
19952 k = -1;
19953 x = 0;
19954 for (i = first; i < end; ++i)
19955 {
19956 int left, right;
19957 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19958 if (x - left < 0)
19959 k = i;
19960 x += glyphs[i].pixel_width;
19961 }
19962
19963 return k;
19964 }
19965
19966
19967 /* Set background width of glyph string S. START is the index of the
19968 first glyph following S. LAST_X is the right-most x-position + 1
19969 in the drawing area. */
19970
19971 static INLINE void
19972 set_glyph_string_background_width (s, start, last_x)
19973 struct glyph_string *s;
19974 int start;
19975 int last_x;
19976 {
19977 /* If the face of this glyph string has to be drawn to the end of
19978 the drawing area, set S->extends_to_end_of_line_p. */
19979
19980 if (start == s->row->used[s->area]
19981 && s->area == TEXT_AREA
19982 && ((s->row->fill_line_p
19983 && (s->hl == DRAW_NORMAL_TEXT
19984 || s->hl == DRAW_IMAGE_RAISED
19985 || s->hl == DRAW_IMAGE_SUNKEN))
19986 || s->hl == DRAW_MOUSE_FACE))
19987 s->extends_to_end_of_line_p = 1;
19988
19989 /* If S extends its face to the end of the line, set its
19990 background_width to the distance to the right edge of the drawing
19991 area. */
19992 if (s->extends_to_end_of_line_p)
19993 s->background_width = last_x - s->x + 1;
19994 else
19995 s->background_width = s->width;
19996 }
19997
19998
19999 /* Compute overhangs and x-positions for glyph string S and its
20000 predecessors, or successors. X is the starting x-position for S.
20001 BACKWARD_P non-zero means process predecessors. */
20002
20003 static void
20004 compute_overhangs_and_x (s, x, backward_p)
20005 struct glyph_string *s;
20006 int x;
20007 int backward_p;
20008 {
20009 if (backward_p)
20010 {
20011 while (s)
20012 {
20013 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20014 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20015 x -= s->width;
20016 s->x = x;
20017 s = s->prev;
20018 }
20019 }
20020 else
20021 {
20022 while (s)
20023 {
20024 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20025 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20026 s->x = x;
20027 x += s->width;
20028 s = s->next;
20029 }
20030 }
20031 }
20032
20033
20034
20035 /* The following macros are only called from draw_glyphs below.
20036 They reference the following parameters of that function directly:
20037 `w', `row', `area', and `overlap_p'
20038 as well as the following local variables:
20039 `s', `f', and `hdc' (in W32) */
20040
20041 #ifdef HAVE_NTGUI
20042 /* On W32, silently add local `hdc' variable to argument list of
20043 init_glyph_string. */
20044 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20045 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20046 #else
20047 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20048 init_glyph_string (s, char2b, w, row, area, start, hl)
20049 #endif
20050
20051 /* Add a glyph string for a stretch glyph to the list of strings
20052 between HEAD and TAIL. START is the index of the stretch glyph in
20053 row area AREA of glyph row ROW. END is the index of the last glyph
20054 in that glyph row area. X is the current output position assigned
20055 to the new glyph string constructed. HL overrides that face of the
20056 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20057 is the right-most x-position of the drawing area. */
20058
20059 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20060 and below -- keep them on one line. */
20061 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20062 do \
20063 { \
20064 s = (struct glyph_string *) alloca (sizeof *s); \
20065 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20066 START = fill_stretch_glyph_string (s, row, area, START, END); \
20067 append_glyph_string (&HEAD, &TAIL, s); \
20068 s->x = (X); \
20069 } \
20070 while (0)
20071
20072
20073 /* Add a glyph string for an image glyph to the list of strings
20074 between HEAD and TAIL. START is the index of the image glyph in
20075 row area AREA of glyph row ROW. END is the index of the last glyph
20076 in that glyph row area. X is the current output position assigned
20077 to the new glyph string constructed. HL overrides that face of the
20078 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20079 is the right-most x-position of the drawing area. */
20080
20081 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20082 do \
20083 { \
20084 s = (struct glyph_string *) alloca (sizeof *s); \
20085 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20086 fill_image_glyph_string (s); \
20087 append_glyph_string (&HEAD, &TAIL, s); \
20088 ++START; \
20089 s->x = (X); \
20090 } \
20091 while (0)
20092
20093
20094 /* Add a glyph string for a sequence of character glyphs to the list
20095 of strings between HEAD and TAIL. START is the index of the first
20096 glyph in row area AREA of glyph row ROW that is part of the new
20097 glyph string. END is the index of the last glyph in that glyph row
20098 area. X is the current output position assigned to the new glyph
20099 string constructed. HL overrides that face of the glyph; e.g. it
20100 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20101 right-most x-position of the drawing area. */
20102
20103 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20104 do \
20105 { \
20106 int face_id; \
20107 XChar2b *char2b; \
20108 \
20109 face_id = (row)->glyphs[area][START].face_id; \
20110 \
20111 s = (struct glyph_string *) alloca (sizeof *s); \
20112 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20113 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20114 append_glyph_string (&HEAD, &TAIL, s); \
20115 s->x = (X); \
20116 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20117 } \
20118 while (0)
20119
20120
20121 /* Add a glyph string for a composite sequence to the list of strings
20122 between HEAD and TAIL. START is the index of the first glyph in
20123 row area AREA of glyph row ROW that is part of the new glyph
20124 string. END is the index of the last glyph in that glyph row area.
20125 X is the current output position assigned to the new glyph string
20126 constructed. HL overrides that face of the glyph; e.g. it is
20127 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20128 x-position of the drawing area. */
20129
20130 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20131 do { \
20132 int face_id = (row)->glyphs[area][START].face_id; \
20133 struct face *base_face = FACE_FROM_ID (f, face_id); \
20134 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20135 struct composition *cmp = composition_table[cmp_id]; \
20136 XChar2b *char2b; \
20137 struct glyph_string *first_s; \
20138 int n; \
20139 \
20140 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20141 \
20142 /* Make glyph_strings for each glyph sequence that is drawable by \
20143 the same face, and append them to HEAD/TAIL. */ \
20144 for (n = 0; n < cmp->glyph_len;) \
20145 { \
20146 s = (struct glyph_string *) alloca (sizeof *s); \
20147 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20148 append_glyph_string (&(HEAD), &(TAIL), s); \
20149 s->cmp = cmp; \
20150 s->cmp_from = n; \
20151 s->x = (X); \
20152 if (n == 0) \
20153 first_s = s; \
20154 n = fill_composite_glyph_string (s, base_face, overlaps); \
20155 } \
20156 \
20157 ++START; \
20158 s = first_s; \
20159 } while (0)
20160
20161
20162 /* Add a glyph string for a glyph-string sequence to the list of strings
20163 between HEAD and TAIL. */
20164
20165 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20166 do { \
20167 int face_id; \
20168 XChar2b *char2b; \
20169 Lisp_Object gstring; \
20170 \
20171 face_id = (row)->glyphs[area][START].face_id; \
20172 gstring = (composition_gstring_from_id \
20173 ((row)->glyphs[area][START].u.cmp.id)); \
20174 s = (struct glyph_string *) alloca (sizeof *s); \
20175 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20176 * LGSTRING_GLYPH_LEN (gstring)); \
20177 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20178 append_glyph_string (&(HEAD), &(TAIL), s); \
20179 s->x = (X); \
20180 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20181 } while (0)
20182
20183
20184 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20185 of AREA of glyph row ROW on window W between indices START and END.
20186 HL overrides the face for drawing glyph strings, e.g. it is
20187 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20188 x-positions of the drawing area.
20189
20190 This is an ugly monster macro construct because we must use alloca
20191 to allocate glyph strings (because draw_glyphs can be called
20192 asynchronously). */
20193
20194 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20195 do \
20196 { \
20197 HEAD = TAIL = NULL; \
20198 while (START < END) \
20199 { \
20200 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20201 switch (first_glyph->type) \
20202 { \
20203 case CHAR_GLYPH: \
20204 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20205 HL, X, LAST_X); \
20206 break; \
20207 \
20208 case COMPOSITE_GLYPH: \
20209 if (first_glyph->u.cmp.automatic) \
20210 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20211 HL, X, LAST_X); \
20212 else \
20213 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20214 HL, X, LAST_X); \
20215 break; \
20216 \
20217 case STRETCH_GLYPH: \
20218 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20219 HL, X, LAST_X); \
20220 break; \
20221 \
20222 case IMAGE_GLYPH: \
20223 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20224 HL, X, LAST_X); \
20225 break; \
20226 \
20227 default: \
20228 abort (); \
20229 } \
20230 \
20231 if (s) \
20232 { \
20233 set_glyph_string_background_width (s, START, LAST_X); \
20234 (X) += s->width; \
20235 } \
20236 } \
20237 } while (0)
20238
20239
20240 /* Draw glyphs between START and END in AREA of ROW on window W,
20241 starting at x-position X. X is relative to AREA in W. HL is a
20242 face-override with the following meaning:
20243
20244 DRAW_NORMAL_TEXT draw normally
20245 DRAW_CURSOR draw in cursor face
20246 DRAW_MOUSE_FACE draw in mouse face.
20247 DRAW_INVERSE_VIDEO draw in mode line face
20248 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20249 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20250
20251 If OVERLAPS is non-zero, draw only the foreground of characters and
20252 clip to the physical height of ROW. Non-zero value also defines
20253 the overlapping part to be drawn:
20254
20255 OVERLAPS_PRED overlap with preceding rows
20256 OVERLAPS_SUCC overlap with succeeding rows
20257 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20258 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20259
20260 Value is the x-position reached, relative to AREA of W. */
20261
20262 static int
20263 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20264 struct window *w;
20265 int x;
20266 struct glyph_row *row;
20267 enum glyph_row_area area;
20268 EMACS_INT start, end;
20269 enum draw_glyphs_face hl;
20270 int overlaps;
20271 {
20272 struct glyph_string *head, *tail;
20273 struct glyph_string *s;
20274 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20275 int i, j, x_reached, last_x, area_left = 0;
20276 struct frame *f = XFRAME (WINDOW_FRAME (w));
20277 DECLARE_HDC (hdc);
20278
20279 ALLOCATE_HDC (hdc, f);
20280
20281 /* Let's rather be paranoid than getting a SEGV. */
20282 end = min (end, row->used[area]);
20283 start = max (0, start);
20284 start = min (end, start);
20285
20286 /* Translate X to frame coordinates. Set last_x to the right
20287 end of the drawing area. */
20288 if (row->full_width_p)
20289 {
20290 /* X is relative to the left edge of W, without scroll bars
20291 or fringes. */
20292 area_left = WINDOW_LEFT_EDGE_X (w);
20293 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20294 }
20295 else
20296 {
20297 area_left = window_box_left (w, area);
20298 last_x = area_left + window_box_width (w, area);
20299 }
20300 x += area_left;
20301
20302 /* Build a doubly-linked list of glyph_string structures between
20303 head and tail from what we have to draw. Note that the macro
20304 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20305 the reason we use a separate variable `i'. */
20306 i = start;
20307 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20308 if (tail)
20309 x_reached = tail->x + tail->background_width;
20310 else
20311 x_reached = x;
20312
20313 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20314 the row, redraw some glyphs in front or following the glyph
20315 strings built above. */
20316 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20317 {
20318 struct glyph_string *h, *t;
20319 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20320 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20321 int dummy_x = 0;
20322
20323 /* If mouse highlighting is on, we may need to draw adjacent
20324 glyphs using mouse-face highlighting. */
20325 if (area == TEXT_AREA && row->mouse_face_p)
20326 {
20327 struct glyph_row *mouse_beg_row, *mouse_end_row;
20328
20329 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20330 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20331
20332 if (row >= mouse_beg_row && row <= mouse_end_row)
20333 {
20334 check_mouse_face = 1;
20335 mouse_beg_col = (row == mouse_beg_row)
20336 ? dpyinfo->mouse_face_beg_col : 0;
20337 mouse_end_col = (row == mouse_end_row)
20338 ? dpyinfo->mouse_face_end_col
20339 : row->used[TEXT_AREA];
20340 }
20341 }
20342
20343 /* Compute overhangs for all glyph strings. */
20344 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20345 for (s = head; s; s = s->next)
20346 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20347
20348 /* Prepend glyph strings for glyphs in front of the first glyph
20349 string that are overwritten because of the first glyph
20350 string's left overhang. The background of all strings
20351 prepended must be drawn because the first glyph string
20352 draws over it. */
20353 i = left_overwritten (head);
20354 if (i >= 0)
20355 {
20356 enum draw_glyphs_face overlap_hl;
20357
20358 /* If this row contains mouse highlighting, attempt to draw
20359 the overlapped glyphs with the correct highlight. This
20360 code fails if the overlap encompasses more than one glyph
20361 and mouse-highlight spans only some of these glyphs.
20362 However, making it work perfectly involves a lot more
20363 code, and I don't know if the pathological case occurs in
20364 practice, so we'll stick to this for now. --- cyd */
20365 if (check_mouse_face
20366 && mouse_beg_col < start && mouse_end_col > i)
20367 overlap_hl = DRAW_MOUSE_FACE;
20368 else
20369 overlap_hl = DRAW_NORMAL_TEXT;
20370
20371 j = i;
20372 BUILD_GLYPH_STRINGS (j, start, h, t,
20373 overlap_hl, dummy_x, last_x);
20374 start = i;
20375 compute_overhangs_and_x (t, head->x, 1);
20376 prepend_glyph_string_lists (&head, &tail, h, t);
20377 clip_head = head;
20378 }
20379
20380 /* Prepend glyph strings for glyphs in front of the first glyph
20381 string that overwrite that glyph string because of their
20382 right overhang. For these strings, only the foreground must
20383 be drawn, because it draws over the glyph string at `head'.
20384 The background must not be drawn because this would overwrite
20385 right overhangs of preceding glyphs for which no glyph
20386 strings exist. */
20387 i = left_overwriting (head);
20388 if (i >= 0)
20389 {
20390 enum draw_glyphs_face overlap_hl;
20391
20392 if (check_mouse_face
20393 && mouse_beg_col < start && mouse_end_col > i)
20394 overlap_hl = DRAW_MOUSE_FACE;
20395 else
20396 overlap_hl = DRAW_NORMAL_TEXT;
20397
20398 clip_head = head;
20399 BUILD_GLYPH_STRINGS (i, start, h, t,
20400 overlap_hl, dummy_x, last_x);
20401 for (s = h; s; s = s->next)
20402 s->background_filled_p = 1;
20403 compute_overhangs_and_x (t, head->x, 1);
20404 prepend_glyph_string_lists (&head, &tail, h, t);
20405 }
20406
20407 /* Append glyphs strings for glyphs following the last glyph
20408 string tail that are overwritten by tail. The background of
20409 these strings has to be drawn because tail's foreground draws
20410 over it. */
20411 i = right_overwritten (tail);
20412 if (i >= 0)
20413 {
20414 enum draw_glyphs_face overlap_hl;
20415
20416 if (check_mouse_face
20417 && mouse_beg_col < i && mouse_end_col > end)
20418 overlap_hl = DRAW_MOUSE_FACE;
20419 else
20420 overlap_hl = DRAW_NORMAL_TEXT;
20421
20422 BUILD_GLYPH_STRINGS (end, i, h, t,
20423 overlap_hl, x, last_x);
20424 /* Because BUILD_GLYPH_STRINGS updates the first argument,
20425 we don't have `end = i;' here. */
20426 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20427 append_glyph_string_lists (&head, &tail, h, t);
20428 clip_tail = tail;
20429 }
20430
20431 /* Append glyph strings for glyphs following the last glyph
20432 string tail that overwrite tail. The foreground of such
20433 glyphs has to be drawn because it writes into the background
20434 of tail. The background must not be drawn because it could
20435 paint over the foreground of following glyphs. */
20436 i = right_overwriting (tail);
20437 if (i >= 0)
20438 {
20439 enum draw_glyphs_face overlap_hl;
20440 if (check_mouse_face
20441 && mouse_beg_col < i && mouse_end_col > end)
20442 overlap_hl = DRAW_MOUSE_FACE;
20443 else
20444 overlap_hl = DRAW_NORMAL_TEXT;
20445
20446 clip_tail = tail;
20447 i++; /* We must include the Ith glyph. */
20448 BUILD_GLYPH_STRINGS (end, i, h, t,
20449 overlap_hl, x, last_x);
20450 for (s = h; s; s = s->next)
20451 s->background_filled_p = 1;
20452 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20453 append_glyph_string_lists (&head, &tail, h, t);
20454 }
20455 if (clip_head || clip_tail)
20456 for (s = head; s; s = s->next)
20457 {
20458 s->clip_head = clip_head;
20459 s->clip_tail = clip_tail;
20460 }
20461 }
20462
20463 /* Draw all strings. */
20464 for (s = head; s; s = s->next)
20465 FRAME_RIF (f)->draw_glyph_string (s);
20466
20467 #ifndef HAVE_NS
20468 /* When focus a sole frame and move horizontally, this sets on_p to 0
20469 causing a failure to erase prev cursor position. */
20470 if (area == TEXT_AREA
20471 && !row->full_width_p
20472 /* When drawing overlapping rows, only the glyph strings'
20473 foreground is drawn, which doesn't erase a cursor
20474 completely. */
20475 && !overlaps)
20476 {
20477 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20478 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20479 : (tail ? tail->x + tail->background_width : x));
20480 x0 -= area_left;
20481 x1 -= area_left;
20482
20483 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20484 row->y, MATRIX_ROW_BOTTOM_Y (row));
20485 }
20486 #endif
20487
20488 /* Value is the x-position up to which drawn, relative to AREA of W.
20489 This doesn't include parts drawn because of overhangs. */
20490 if (row->full_width_p)
20491 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20492 else
20493 x_reached -= area_left;
20494
20495 RELEASE_HDC (hdc, f);
20496
20497 return x_reached;
20498 }
20499
20500 /* Expand row matrix if too narrow. Don't expand if area
20501 is not present. */
20502
20503 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20504 { \
20505 if (!fonts_changed_p \
20506 && (it->glyph_row->glyphs[area] \
20507 < it->glyph_row->glyphs[area + 1])) \
20508 { \
20509 it->w->ncols_scale_factor++; \
20510 fonts_changed_p = 1; \
20511 } \
20512 }
20513
20514 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20515 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20516
20517 static INLINE void
20518 append_glyph (it)
20519 struct it *it;
20520 {
20521 struct glyph *glyph;
20522 enum glyph_row_area area = it->area;
20523
20524 xassert (it->glyph_row);
20525 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20526
20527 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20528 if (glyph < it->glyph_row->glyphs[area + 1])
20529 {
20530 glyph->charpos = CHARPOS (it->position);
20531 glyph->object = it->object;
20532 if (it->pixel_width > 0)
20533 {
20534 glyph->pixel_width = it->pixel_width;
20535 glyph->padding_p = 0;
20536 }
20537 else
20538 {
20539 /* Assure at least 1-pixel width. Otherwise, cursor can't
20540 be displayed correctly. */
20541 glyph->pixel_width = 1;
20542 glyph->padding_p = 1;
20543 }
20544 glyph->ascent = it->ascent;
20545 glyph->descent = it->descent;
20546 glyph->voffset = it->voffset;
20547 glyph->type = CHAR_GLYPH;
20548 glyph->avoid_cursor_p = it->avoid_cursor_p;
20549 glyph->multibyte_p = it->multibyte_p;
20550 glyph->left_box_line_p = it->start_of_box_run_p;
20551 glyph->right_box_line_p = it->end_of_box_run_p;
20552 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20553 || it->phys_descent > it->descent);
20554 glyph->glyph_not_available_p = it->glyph_not_available_p;
20555 glyph->face_id = it->face_id;
20556 glyph->u.ch = it->char_to_display;
20557 glyph->slice = null_glyph_slice;
20558 glyph->font_type = FONT_TYPE_UNKNOWN;
20559 ++it->glyph_row->used[area];
20560 }
20561 else
20562 IT_EXPAND_MATRIX_WIDTH (it, area);
20563 }
20564
20565 /* Store one glyph for the composition IT->cmp_it.id in
20566 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20567 non-null. */
20568
20569 static INLINE void
20570 append_composite_glyph (it)
20571 struct it *it;
20572 {
20573 struct glyph *glyph;
20574 enum glyph_row_area area = it->area;
20575
20576 xassert (it->glyph_row);
20577
20578 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20579 if (glyph < it->glyph_row->glyphs[area + 1])
20580 {
20581 glyph->charpos = CHARPOS (it->position);
20582 glyph->object = it->object;
20583 glyph->pixel_width = it->pixel_width;
20584 glyph->ascent = it->ascent;
20585 glyph->descent = it->descent;
20586 glyph->voffset = it->voffset;
20587 glyph->type = COMPOSITE_GLYPH;
20588 if (it->cmp_it.ch < 0)
20589 {
20590 glyph->u.cmp.automatic = 0;
20591 glyph->u.cmp.id = it->cmp_it.id;
20592 }
20593 else
20594 {
20595 glyph->u.cmp.automatic = 1;
20596 glyph->u.cmp.id = it->cmp_it.id;
20597 glyph->u.cmp.from = it->cmp_it.from;
20598 glyph->u.cmp.to = it->cmp_it.to - 1;
20599 }
20600 glyph->avoid_cursor_p = it->avoid_cursor_p;
20601 glyph->multibyte_p = it->multibyte_p;
20602 glyph->left_box_line_p = it->start_of_box_run_p;
20603 glyph->right_box_line_p = it->end_of_box_run_p;
20604 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20605 || it->phys_descent > it->descent);
20606 glyph->padding_p = 0;
20607 glyph->glyph_not_available_p = 0;
20608 glyph->face_id = it->face_id;
20609 glyph->slice = null_glyph_slice;
20610 glyph->font_type = FONT_TYPE_UNKNOWN;
20611 ++it->glyph_row->used[area];
20612 }
20613 else
20614 IT_EXPAND_MATRIX_WIDTH (it, area);
20615 }
20616
20617
20618 /* Change IT->ascent and IT->height according to the setting of
20619 IT->voffset. */
20620
20621 static INLINE void
20622 take_vertical_position_into_account (it)
20623 struct it *it;
20624 {
20625 if (it->voffset)
20626 {
20627 if (it->voffset < 0)
20628 /* Increase the ascent so that we can display the text higher
20629 in the line. */
20630 it->ascent -= it->voffset;
20631 else
20632 /* Increase the descent so that we can display the text lower
20633 in the line. */
20634 it->descent += it->voffset;
20635 }
20636 }
20637
20638
20639 /* Produce glyphs/get display metrics for the image IT is loaded with.
20640 See the description of struct display_iterator in dispextern.h for
20641 an overview of struct display_iterator. */
20642
20643 static void
20644 produce_image_glyph (it)
20645 struct it *it;
20646 {
20647 struct image *img;
20648 struct face *face;
20649 int glyph_ascent, crop;
20650 struct glyph_slice slice;
20651
20652 xassert (it->what == IT_IMAGE);
20653
20654 face = FACE_FROM_ID (it->f, it->face_id);
20655 xassert (face);
20656 /* Make sure X resources of the face is loaded. */
20657 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20658
20659 if (it->image_id < 0)
20660 {
20661 /* Fringe bitmap. */
20662 it->ascent = it->phys_ascent = 0;
20663 it->descent = it->phys_descent = 0;
20664 it->pixel_width = 0;
20665 it->nglyphs = 0;
20666 return;
20667 }
20668
20669 img = IMAGE_FROM_ID (it->f, it->image_id);
20670 xassert (img);
20671 /* Make sure X resources of the image is loaded. */
20672 prepare_image_for_display (it->f, img);
20673
20674 slice.x = slice.y = 0;
20675 slice.width = img->width;
20676 slice.height = img->height;
20677
20678 if (INTEGERP (it->slice.x))
20679 slice.x = XINT (it->slice.x);
20680 else if (FLOATP (it->slice.x))
20681 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20682
20683 if (INTEGERP (it->slice.y))
20684 slice.y = XINT (it->slice.y);
20685 else if (FLOATP (it->slice.y))
20686 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20687
20688 if (INTEGERP (it->slice.width))
20689 slice.width = XINT (it->slice.width);
20690 else if (FLOATP (it->slice.width))
20691 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20692
20693 if (INTEGERP (it->slice.height))
20694 slice.height = XINT (it->slice.height);
20695 else if (FLOATP (it->slice.height))
20696 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20697
20698 if (slice.x >= img->width)
20699 slice.x = img->width;
20700 if (slice.y >= img->height)
20701 slice.y = img->height;
20702 if (slice.x + slice.width >= img->width)
20703 slice.width = img->width - slice.x;
20704 if (slice.y + slice.height > img->height)
20705 slice.height = img->height - slice.y;
20706
20707 if (slice.width == 0 || slice.height == 0)
20708 return;
20709
20710 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20711
20712 it->descent = slice.height - glyph_ascent;
20713 if (slice.y == 0)
20714 it->descent += img->vmargin;
20715 if (slice.y + slice.height == img->height)
20716 it->descent += img->vmargin;
20717 it->phys_descent = it->descent;
20718
20719 it->pixel_width = slice.width;
20720 if (slice.x == 0)
20721 it->pixel_width += img->hmargin;
20722 if (slice.x + slice.width == img->width)
20723 it->pixel_width += img->hmargin;
20724
20725 /* It's quite possible for images to have an ascent greater than
20726 their height, so don't get confused in that case. */
20727 if (it->descent < 0)
20728 it->descent = 0;
20729
20730 it->nglyphs = 1;
20731
20732 if (face->box != FACE_NO_BOX)
20733 {
20734 if (face->box_line_width > 0)
20735 {
20736 if (slice.y == 0)
20737 it->ascent += face->box_line_width;
20738 if (slice.y + slice.height == img->height)
20739 it->descent += face->box_line_width;
20740 }
20741
20742 if (it->start_of_box_run_p && slice.x == 0)
20743 it->pixel_width += eabs (face->box_line_width);
20744 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20745 it->pixel_width += eabs (face->box_line_width);
20746 }
20747
20748 take_vertical_position_into_account (it);
20749
20750 /* Automatically crop wide image glyphs at right edge so we can
20751 draw the cursor on same display row. */
20752 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20753 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20754 {
20755 it->pixel_width -= crop;
20756 slice.width -= crop;
20757 }
20758
20759 if (it->glyph_row)
20760 {
20761 struct glyph *glyph;
20762 enum glyph_row_area area = it->area;
20763
20764 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20765 if (glyph < it->glyph_row->glyphs[area + 1])
20766 {
20767 glyph->charpos = CHARPOS (it->position);
20768 glyph->object = it->object;
20769 glyph->pixel_width = it->pixel_width;
20770 glyph->ascent = glyph_ascent;
20771 glyph->descent = it->descent;
20772 glyph->voffset = it->voffset;
20773 glyph->type = IMAGE_GLYPH;
20774 glyph->avoid_cursor_p = it->avoid_cursor_p;
20775 glyph->multibyte_p = it->multibyte_p;
20776 glyph->left_box_line_p = it->start_of_box_run_p;
20777 glyph->right_box_line_p = it->end_of_box_run_p;
20778 glyph->overlaps_vertically_p = 0;
20779 glyph->padding_p = 0;
20780 glyph->glyph_not_available_p = 0;
20781 glyph->face_id = it->face_id;
20782 glyph->u.img_id = img->id;
20783 glyph->slice = slice;
20784 glyph->font_type = FONT_TYPE_UNKNOWN;
20785 ++it->glyph_row->used[area];
20786 }
20787 else
20788 IT_EXPAND_MATRIX_WIDTH (it, area);
20789 }
20790 }
20791
20792
20793 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20794 of the glyph, WIDTH and HEIGHT are the width and height of the
20795 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20796
20797 static void
20798 append_stretch_glyph (it, object, width, height, ascent)
20799 struct it *it;
20800 Lisp_Object object;
20801 int width, height;
20802 int ascent;
20803 {
20804 struct glyph *glyph;
20805 enum glyph_row_area area = it->area;
20806
20807 xassert (ascent >= 0 && ascent <= height);
20808
20809 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20810 if (glyph < it->glyph_row->glyphs[area + 1])
20811 {
20812 glyph->charpos = CHARPOS (it->position);
20813 glyph->object = object;
20814 glyph->pixel_width = width;
20815 glyph->ascent = ascent;
20816 glyph->descent = height - ascent;
20817 glyph->voffset = it->voffset;
20818 glyph->type = STRETCH_GLYPH;
20819 glyph->avoid_cursor_p = it->avoid_cursor_p;
20820 glyph->multibyte_p = it->multibyte_p;
20821 glyph->left_box_line_p = it->start_of_box_run_p;
20822 glyph->right_box_line_p = it->end_of_box_run_p;
20823 glyph->overlaps_vertically_p = 0;
20824 glyph->padding_p = 0;
20825 glyph->glyph_not_available_p = 0;
20826 glyph->face_id = it->face_id;
20827 glyph->u.stretch.ascent = ascent;
20828 glyph->u.stretch.height = height;
20829 glyph->slice = null_glyph_slice;
20830 glyph->font_type = FONT_TYPE_UNKNOWN;
20831 ++it->glyph_row->used[area];
20832 }
20833 else
20834 IT_EXPAND_MATRIX_WIDTH (it, area);
20835 }
20836
20837
20838 /* Produce a stretch glyph for iterator IT. IT->object is the value
20839 of the glyph property displayed. The value must be a list
20840 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20841 being recognized:
20842
20843 1. `:width WIDTH' specifies that the space should be WIDTH *
20844 canonical char width wide. WIDTH may be an integer or floating
20845 point number.
20846
20847 2. `:relative-width FACTOR' specifies that the width of the stretch
20848 should be computed from the width of the first character having the
20849 `glyph' property, and should be FACTOR times that width.
20850
20851 3. `:align-to HPOS' specifies that the space should be wide enough
20852 to reach HPOS, a value in canonical character units.
20853
20854 Exactly one of the above pairs must be present.
20855
20856 4. `:height HEIGHT' specifies that the height of the stretch produced
20857 should be HEIGHT, measured in canonical character units.
20858
20859 5. `:relative-height FACTOR' specifies that the height of the
20860 stretch should be FACTOR times the height of the characters having
20861 the glyph property.
20862
20863 Either none or exactly one of 4 or 5 must be present.
20864
20865 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20866 of the stretch should be used for the ascent of the stretch.
20867 ASCENT must be in the range 0 <= ASCENT <= 100. */
20868
20869 static void
20870 produce_stretch_glyph (it)
20871 struct it *it;
20872 {
20873 /* (space :width WIDTH :height HEIGHT ...) */
20874 Lisp_Object prop, plist;
20875 int width = 0, height = 0, align_to = -1;
20876 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20877 int ascent = 0;
20878 double tem;
20879 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20880 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20881
20882 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20883
20884 /* List should start with `space'. */
20885 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20886 plist = XCDR (it->object);
20887
20888 /* Compute the width of the stretch. */
20889 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20890 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20891 {
20892 /* Absolute width `:width WIDTH' specified and valid. */
20893 zero_width_ok_p = 1;
20894 width = (int)tem;
20895 }
20896 else if (prop = Fplist_get (plist, QCrelative_width),
20897 NUMVAL (prop) > 0)
20898 {
20899 /* Relative width `:relative-width FACTOR' specified and valid.
20900 Compute the width of the characters having the `glyph'
20901 property. */
20902 struct it it2;
20903 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20904
20905 it2 = *it;
20906 if (it->multibyte_p)
20907 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
20908 else
20909 {
20910 it2.c = it2.char_to_display = *p, it2.len = 1;
20911 if (! ASCII_CHAR_P (it2.c))
20912 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
20913 }
20914
20915 it2.glyph_row = NULL;
20916 it2.what = IT_CHARACTER;
20917 x_produce_glyphs (&it2);
20918 width = NUMVAL (prop) * it2.pixel_width;
20919 }
20920 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20921 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20922 {
20923 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20924 align_to = (align_to < 0
20925 ? 0
20926 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20927 else if (align_to < 0)
20928 align_to = window_box_left_offset (it->w, TEXT_AREA);
20929 width = max (0, (int)tem + align_to - it->current_x);
20930 zero_width_ok_p = 1;
20931 }
20932 else
20933 /* Nothing specified -> width defaults to canonical char width. */
20934 width = FRAME_COLUMN_WIDTH (it->f);
20935
20936 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20937 width = 1;
20938
20939 /* Compute height. */
20940 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20941 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20942 {
20943 height = (int)tem;
20944 zero_height_ok_p = 1;
20945 }
20946 else if (prop = Fplist_get (plist, QCrelative_height),
20947 NUMVAL (prop) > 0)
20948 height = FONT_HEIGHT (font) * NUMVAL (prop);
20949 else
20950 height = FONT_HEIGHT (font);
20951
20952 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20953 height = 1;
20954
20955 /* Compute percentage of height used for ascent. If
20956 `:ascent ASCENT' is present and valid, use that. Otherwise,
20957 derive the ascent from the font in use. */
20958 if (prop = Fplist_get (plist, QCascent),
20959 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20960 ascent = height * NUMVAL (prop) / 100.0;
20961 else if (!NILP (prop)
20962 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20963 ascent = min (max (0, (int)tem), height);
20964 else
20965 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20966
20967 if (width > 0 && it->line_wrap != TRUNCATE
20968 && it->current_x + width > it->last_visible_x)
20969 width = it->last_visible_x - it->current_x - 1;
20970
20971 if (width > 0 && height > 0 && it->glyph_row)
20972 {
20973 Lisp_Object object = it->stack[it->sp - 1].string;
20974 if (!STRINGP (object))
20975 object = it->w->buffer;
20976 append_stretch_glyph (it, object, width, height, ascent);
20977 }
20978
20979 it->pixel_width = width;
20980 it->ascent = it->phys_ascent = ascent;
20981 it->descent = it->phys_descent = height - it->ascent;
20982 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20983
20984 take_vertical_position_into_account (it);
20985 }
20986
20987 /* Calculate line-height and line-spacing properties.
20988 An integer value specifies explicit pixel value.
20989 A float value specifies relative value to current face height.
20990 A cons (float . face-name) specifies relative value to
20991 height of specified face font.
20992
20993 Returns height in pixels, or nil. */
20994
20995
20996 static Lisp_Object
20997 calc_line_height_property (it, val, font, boff, override)
20998 struct it *it;
20999 Lisp_Object val;
21000 struct font *font;
21001 int boff, override;
21002 {
21003 Lisp_Object face_name = Qnil;
21004 int ascent, descent, height;
21005
21006 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21007 return val;
21008
21009 if (CONSP (val))
21010 {
21011 face_name = XCAR (val);
21012 val = XCDR (val);
21013 if (!NUMBERP (val))
21014 val = make_number (1);
21015 if (NILP (face_name))
21016 {
21017 height = it->ascent + it->descent;
21018 goto scale;
21019 }
21020 }
21021
21022 if (NILP (face_name))
21023 {
21024 font = FRAME_FONT (it->f);
21025 boff = FRAME_BASELINE_OFFSET (it->f);
21026 }
21027 else if (EQ (face_name, Qt))
21028 {
21029 override = 0;
21030 }
21031 else
21032 {
21033 int face_id;
21034 struct face *face;
21035
21036 face_id = lookup_named_face (it->f, face_name, 0);
21037 if (face_id < 0)
21038 return make_number (-1);
21039
21040 face = FACE_FROM_ID (it->f, face_id);
21041 font = face->font;
21042 if (font == NULL)
21043 return make_number (-1);
21044 boff = font->baseline_offset;
21045 if (font->vertical_centering)
21046 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21047 }
21048
21049 ascent = FONT_BASE (font) + boff;
21050 descent = FONT_DESCENT (font) - boff;
21051
21052 if (override)
21053 {
21054 it->override_ascent = ascent;
21055 it->override_descent = descent;
21056 it->override_boff = boff;
21057 }
21058
21059 height = ascent + descent;
21060
21061 scale:
21062 if (FLOATP (val))
21063 height = (int)(XFLOAT_DATA (val) * height);
21064 else if (INTEGERP (val))
21065 height *= XINT (val);
21066
21067 return make_number (height);
21068 }
21069
21070
21071 /* RIF:
21072 Produce glyphs/get display metrics for the display element IT is
21073 loaded with. See the description of struct it in dispextern.h
21074 for an overview of struct it. */
21075
21076 void
21077 x_produce_glyphs (it)
21078 struct it *it;
21079 {
21080 int extra_line_spacing = it->extra_line_spacing;
21081
21082 it->glyph_not_available_p = 0;
21083
21084 if (it->what == IT_CHARACTER)
21085 {
21086 XChar2b char2b;
21087 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21088 struct font *font = face->font;
21089 int font_not_found_p = font == NULL;
21090 struct font_metrics *pcm = NULL;
21091 int boff; /* baseline offset */
21092
21093 if (font_not_found_p)
21094 {
21095 /* When no suitable font found, display an empty box based
21096 on the metrics of the font of the default face (or what
21097 remapped). */
21098 struct face *no_font_face
21099 = FACE_FROM_ID (it->f,
21100 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21101 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21102 font = no_font_face->font;
21103 boff = font->baseline_offset;
21104 }
21105 else
21106 {
21107 boff = font->baseline_offset;
21108 if (font->vertical_centering)
21109 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21110 }
21111
21112 if (it->char_to_display != '\n' && it->char_to_display != '\t')
21113 {
21114 int stretched_p;
21115
21116 it->nglyphs = 1;
21117
21118 if (it->override_ascent >= 0)
21119 {
21120 it->ascent = it->override_ascent;
21121 it->descent = it->override_descent;
21122 boff = it->override_boff;
21123 }
21124 else
21125 {
21126 it->ascent = FONT_BASE (font) + boff;
21127 it->descent = FONT_DESCENT (font) - boff;
21128 }
21129
21130 if (! font_not_found_p
21131 && get_char_glyph_code (it->char_to_display, font, &char2b))
21132 {
21133 pcm = get_per_char_metric (it->f, font, &char2b);
21134 if (pcm->width == 0
21135 && pcm->rbearing == 0 && pcm->lbearing == 0)
21136 pcm = NULL;
21137 }
21138
21139 if (pcm)
21140 {
21141 it->phys_ascent = pcm->ascent + boff;
21142 it->phys_descent = pcm->descent - boff;
21143 it->pixel_width = pcm->width;
21144 }
21145 else
21146 {
21147 it->glyph_not_available_p = 1;
21148 it->phys_ascent = it->ascent;
21149 it->phys_descent = it->descent;
21150 it->pixel_width = font->space_width;
21151 }
21152
21153 if (it->constrain_row_ascent_descent_p)
21154 {
21155 if (it->descent > it->max_descent)
21156 {
21157 it->ascent += it->descent - it->max_descent;
21158 it->descent = it->max_descent;
21159 }
21160 if (it->ascent > it->max_ascent)
21161 {
21162 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21163 it->ascent = it->max_ascent;
21164 }
21165 it->phys_ascent = min (it->phys_ascent, it->ascent);
21166 it->phys_descent = min (it->phys_descent, it->descent);
21167 extra_line_spacing = 0;
21168 }
21169
21170 /* If this is a space inside a region of text with
21171 `space-width' property, change its width. */
21172 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21173 if (stretched_p)
21174 it->pixel_width *= XFLOATINT (it->space_width);
21175
21176 /* If face has a box, add the box thickness to the character
21177 height. If character has a box line to the left and/or
21178 right, add the box line width to the character's width. */
21179 if (face->box != FACE_NO_BOX)
21180 {
21181 int thick = face->box_line_width;
21182
21183 if (thick > 0)
21184 {
21185 it->ascent += thick;
21186 it->descent += thick;
21187 }
21188 else
21189 thick = -thick;
21190
21191 if (it->start_of_box_run_p)
21192 it->pixel_width += thick;
21193 if (it->end_of_box_run_p)
21194 it->pixel_width += thick;
21195 }
21196
21197 /* If face has an overline, add the height of the overline
21198 (1 pixel) and a 1 pixel margin to the character height. */
21199 if (face->overline_p)
21200 it->ascent += overline_margin;
21201
21202 if (it->constrain_row_ascent_descent_p)
21203 {
21204 if (it->ascent > it->max_ascent)
21205 it->ascent = it->max_ascent;
21206 if (it->descent > it->max_descent)
21207 it->descent = it->max_descent;
21208 }
21209
21210 take_vertical_position_into_account (it);
21211
21212 /* If we have to actually produce glyphs, do it. */
21213 if (it->glyph_row)
21214 {
21215 if (stretched_p)
21216 {
21217 /* Translate a space with a `space-width' property
21218 into a stretch glyph. */
21219 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21220 / FONT_HEIGHT (font));
21221 append_stretch_glyph (it, it->object, it->pixel_width,
21222 it->ascent + it->descent, ascent);
21223 }
21224 else
21225 append_glyph (it);
21226
21227 /* If characters with lbearing or rbearing are displayed
21228 in this line, record that fact in a flag of the
21229 glyph row. This is used to optimize X output code. */
21230 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21231 it->glyph_row->contains_overlapping_glyphs_p = 1;
21232 }
21233 if (! stretched_p && it->pixel_width == 0)
21234 /* We assure that all visible glyphs have at least 1-pixel
21235 width. */
21236 it->pixel_width = 1;
21237 }
21238 else if (it->char_to_display == '\n')
21239 {
21240 /* A newline has no width, but we need the height of the
21241 line. But if previous part of the line sets a height,
21242 don't increase that height */
21243
21244 Lisp_Object height;
21245 Lisp_Object total_height = Qnil;
21246
21247 it->override_ascent = -1;
21248 it->pixel_width = 0;
21249 it->nglyphs = 0;
21250
21251 height = get_it_property(it, Qline_height);
21252 /* Split (line-height total-height) list */
21253 if (CONSP (height)
21254 && CONSP (XCDR (height))
21255 && NILP (XCDR (XCDR (height))))
21256 {
21257 total_height = XCAR (XCDR (height));
21258 height = XCAR (height);
21259 }
21260 height = calc_line_height_property(it, height, font, boff, 1);
21261
21262 if (it->override_ascent >= 0)
21263 {
21264 it->ascent = it->override_ascent;
21265 it->descent = it->override_descent;
21266 boff = it->override_boff;
21267 }
21268 else
21269 {
21270 it->ascent = FONT_BASE (font) + boff;
21271 it->descent = FONT_DESCENT (font) - boff;
21272 }
21273
21274 if (EQ (height, Qt))
21275 {
21276 if (it->descent > it->max_descent)
21277 {
21278 it->ascent += it->descent - it->max_descent;
21279 it->descent = it->max_descent;
21280 }
21281 if (it->ascent > it->max_ascent)
21282 {
21283 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21284 it->ascent = it->max_ascent;
21285 }
21286 it->phys_ascent = min (it->phys_ascent, it->ascent);
21287 it->phys_descent = min (it->phys_descent, it->descent);
21288 it->constrain_row_ascent_descent_p = 1;
21289 extra_line_spacing = 0;
21290 }
21291 else
21292 {
21293 Lisp_Object spacing;
21294
21295 it->phys_ascent = it->ascent;
21296 it->phys_descent = it->descent;
21297
21298 if ((it->max_ascent > 0 || it->max_descent > 0)
21299 && face->box != FACE_NO_BOX
21300 && face->box_line_width > 0)
21301 {
21302 it->ascent += face->box_line_width;
21303 it->descent += face->box_line_width;
21304 }
21305 if (!NILP (height)
21306 && XINT (height) > it->ascent + it->descent)
21307 it->ascent = XINT (height) - it->descent;
21308
21309 if (!NILP (total_height))
21310 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21311 else
21312 {
21313 spacing = get_it_property(it, Qline_spacing);
21314 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21315 }
21316 if (INTEGERP (spacing))
21317 {
21318 extra_line_spacing = XINT (spacing);
21319 if (!NILP (total_height))
21320 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21321 }
21322 }
21323 }
21324 else /* i.e. (it->char_to_display == '\t') */
21325 {
21326 if (font->space_width > 0)
21327 {
21328 int tab_width = it->tab_width * font->space_width;
21329 int x = it->current_x + it->continuation_lines_width;
21330 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21331
21332 /* If the distance from the current position to the next tab
21333 stop is less than a space character width, use the
21334 tab stop after that. */
21335 if (next_tab_x - x < font->space_width)
21336 next_tab_x += tab_width;
21337
21338 it->pixel_width = next_tab_x - x;
21339 it->nglyphs = 1;
21340 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21341 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21342
21343 if (it->glyph_row)
21344 {
21345 append_stretch_glyph (it, it->object, it->pixel_width,
21346 it->ascent + it->descent, it->ascent);
21347 }
21348 }
21349 else
21350 {
21351 it->pixel_width = 0;
21352 it->nglyphs = 1;
21353 }
21354 }
21355 }
21356 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21357 {
21358 /* A static composition.
21359
21360 Note: A composition is represented as one glyph in the
21361 glyph matrix. There are no padding glyphs.
21362
21363 Important note: pixel_width, ascent, and descent are the
21364 values of what is drawn by draw_glyphs (i.e. the values of
21365 the overall glyphs composed). */
21366 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21367 int boff; /* baseline offset */
21368 struct composition *cmp = composition_table[it->cmp_it.id];
21369 int glyph_len = cmp->glyph_len;
21370 struct font *font = face->font;
21371
21372 it->nglyphs = 1;
21373
21374 /* If we have not yet calculated pixel size data of glyphs of
21375 the composition for the current face font, calculate them
21376 now. Theoretically, we have to check all fonts for the
21377 glyphs, but that requires much time and memory space. So,
21378 here we check only the font of the first glyph. This may
21379 lead to incorrect display, but it's very rare, and C-l
21380 (recenter-top-bottom) can correct the display anyway. */
21381 if (! cmp->font || cmp->font != font)
21382 {
21383 /* Ascent and descent of the font of the first character
21384 of this composition (adjusted by baseline offset).
21385 Ascent and descent of overall glyphs should not be less
21386 than these, respectively. */
21387 int font_ascent, font_descent, font_height;
21388 /* Bounding box of the overall glyphs. */
21389 int leftmost, rightmost, lowest, highest;
21390 int lbearing, rbearing;
21391 int i, width, ascent, descent;
21392 int left_padded = 0, right_padded = 0;
21393 int c;
21394 XChar2b char2b;
21395 struct font_metrics *pcm;
21396 int font_not_found_p;
21397 int pos;
21398
21399 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21400 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21401 break;
21402 if (glyph_len < cmp->glyph_len)
21403 right_padded = 1;
21404 for (i = 0; i < glyph_len; i++)
21405 {
21406 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21407 break;
21408 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21409 }
21410 if (i > 0)
21411 left_padded = 1;
21412
21413 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21414 : IT_CHARPOS (*it));
21415 /* If no suitable font is found, use the default font. */
21416 font_not_found_p = font == NULL;
21417 if (font_not_found_p)
21418 {
21419 face = face->ascii_face;
21420 font = face->font;
21421 }
21422 boff = font->baseline_offset;
21423 if (font->vertical_centering)
21424 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21425 font_ascent = FONT_BASE (font) + boff;
21426 font_descent = FONT_DESCENT (font) - boff;
21427 font_height = FONT_HEIGHT (font);
21428
21429 cmp->font = (void *) font;
21430
21431 pcm = NULL;
21432 if (! font_not_found_p)
21433 {
21434 get_char_face_and_encoding (it->f, c, it->face_id,
21435 &char2b, it->multibyte_p, 0);
21436 pcm = get_per_char_metric (it->f, font, &char2b);
21437 }
21438
21439 /* Initialize the bounding box. */
21440 if (pcm)
21441 {
21442 width = pcm->width;
21443 ascent = pcm->ascent;
21444 descent = pcm->descent;
21445 lbearing = pcm->lbearing;
21446 rbearing = pcm->rbearing;
21447 }
21448 else
21449 {
21450 width = font->space_width;
21451 ascent = FONT_BASE (font);
21452 descent = FONT_DESCENT (font);
21453 lbearing = 0;
21454 rbearing = width;
21455 }
21456
21457 rightmost = width;
21458 leftmost = 0;
21459 lowest = - descent + boff;
21460 highest = ascent + boff;
21461
21462 if (! font_not_found_p
21463 && font->default_ascent
21464 && CHAR_TABLE_P (Vuse_default_ascent)
21465 && !NILP (Faref (Vuse_default_ascent,
21466 make_number (it->char_to_display))))
21467 highest = font->default_ascent + boff;
21468
21469 /* Draw the first glyph at the normal position. It may be
21470 shifted to right later if some other glyphs are drawn
21471 at the left. */
21472 cmp->offsets[i * 2] = 0;
21473 cmp->offsets[i * 2 + 1] = boff;
21474 cmp->lbearing = lbearing;
21475 cmp->rbearing = rbearing;
21476
21477 /* Set cmp->offsets for the remaining glyphs. */
21478 for (i++; i < glyph_len; i++)
21479 {
21480 int left, right, btm, top;
21481 int ch = COMPOSITION_GLYPH (cmp, i);
21482 int face_id;
21483 struct face *this_face;
21484 int this_boff;
21485
21486 if (ch == '\t')
21487 ch = ' ';
21488 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21489 this_face = FACE_FROM_ID (it->f, face_id);
21490 font = this_face->font;
21491
21492 if (font == NULL)
21493 pcm = NULL;
21494 else
21495 {
21496 this_boff = font->baseline_offset;
21497 if (font->vertical_centering)
21498 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21499 get_char_face_and_encoding (it->f, ch, face_id,
21500 &char2b, it->multibyte_p, 0);
21501 pcm = get_per_char_metric (it->f, font, &char2b);
21502 }
21503 if (! pcm)
21504 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21505 else
21506 {
21507 width = pcm->width;
21508 ascent = pcm->ascent;
21509 descent = pcm->descent;
21510 lbearing = pcm->lbearing;
21511 rbearing = pcm->rbearing;
21512 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21513 {
21514 /* Relative composition with or without
21515 alternate chars. */
21516 left = (leftmost + rightmost - width) / 2;
21517 btm = - descent + boff;
21518 if (font->relative_compose
21519 && (! CHAR_TABLE_P (Vignore_relative_composition)
21520 || NILP (Faref (Vignore_relative_composition,
21521 make_number (ch)))))
21522 {
21523
21524 if (- descent >= font->relative_compose)
21525 /* One extra pixel between two glyphs. */
21526 btm = highest + 1;
21527 else if (ascent <= 0)
21528 /* One extra pixel between two glyphs. */
21529 btm = lowest - 1 - ascent - descent;
21530 }
21531 }
21532 else
21533 {
21534 /* A composition rule is specified by an integer
21535 value that encodes global and new reference
21536 points (GREF and NREF). GREF and NREF are
21537 specified by numbers as below:
21538
21539 0---1---2 -- ascent
21540 | |
21541 | |
21542 | |
21543 9--10--11 -- center
21544 | |
21545 ---3---4---5--- baseline
21546 | |
21547 6---7---8 -- descent
21548 */
21549 int rule = COMPOSITION_RULE (cmp, i);
21550 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21551
21552 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21553 grefx = gref % 3, nrefx = nref % 3;
21554 grefy = gref / 3, nrefy = nref / 3;
21555 if (xoff)
21556 xoff = font_height * (xoff - 128) / 256;
21557 if (yoff)
21558 yoff = font_height * (yoff - 128) / 256;
21559
21560 left = (leftmost
21561 + grefx * (rightmost - leftmost) / 2
21562 - nrefx * width / 2
21563 + xoff);
21564
21565 btm = ((grefy == 0 ? highest
21566 : grefy == 1 ? 0
21567 : grefy == 2 ? lowest
21568 : (highest + lowest) / 2)
21569 - (nrefy == 0 ? ascent + descent
21570 : nrefy == 1 ? descent - boff
21571 : nrefy == 2 ? 0
21572 : (ascent + descent) / 2)
21573 + yoff);
21574 }
21575
21576 cmp->offsets[i * 2] = left;
21577 cmp->offsets[i * 2 + 1] = btm + descent;
21578
21579 /* Update the bounding box of the overall glyphs. */
21580 if (width > 0)
21581 {
21582 right = left + width;
21583 if (left < leftmost)
21584 leftmost = left;
21585 if (right > rightmost)
21586 rightmost = right;
21587 }
21588 top = btm + descent + ascent;
21589 if (top > highest)
21590 highest = top;
21591 if (btm < lowest)
21592 lowest = btm;
21593
21594 if (cmp->lbearing > left + lbearing)
21595 cmp->lbearing = left + lbearing;
21596 if (cmp->rbearing < left + rbearing)
21597 cmp->rbearing = left + rbearing;
21598 }
21599 }
21600
21601 /* If there are glyphs whose x-offsets are negative,
21602 shift all glyphs to the right and make all x-offsets
21603 non-negative. */
21604 if (leftmost < 0)
21605 {
21606 for (i = 0; i < cmp->glyph_len; i++)
21607 cmp->offsets[i * 2] -= leftmost;
21608 rightmost -= leftmost;
21609 cmp->lbearing -= leftmost;
21610 cmp->rbearing -= leftmost;
21611 }
21612
21613 if (left_padded && cmp->lbearing < 0)
21614 {
21615 for (i = 0; i < cmp->glyph_len; i++)
21616 cmp->offsets[i * 2] -= cmp->lbearing;
21617 rightmost -= cmp->lbearing;
21618 cmp->rbearing -= cmp->lbearing;
21619 cmp->lbearing = 0;
21620 }
21621 if (right_padded && rightmost < cmp->rbearing)
21622 {
21623 rightmost = cmp->rbearing;
21624 }
21625
21626 cmp->pixel_width = rightmost;
21627 cmp->ascent = highest;
21628 cmp->descent = - lowest;
21629 if (cmp->ascent < font_ascent)
21630 cmp->ascent = font_ascent;
21631 if (cmp->descent < font_descent)
21632 cmp->descent = font_descent;
21633 }
21634
21635 if (it->glyph_row
21636 && (cmp->lbearing < 0
21637 || cmp->rbearing > cmp->pixel_width))
21638 it->glyph_row->contains_overlapping_glyphs_p = 1;
21639
21640 it->pixel_width = cmp->pixel_width;
21641 it->ascent = it->phys_ascent = cmp->ascent;
21642 it->descent = it->phys_descent = cmp->descent;
21643 if (face->box != FACE_NO_BOX)
21644 {
21645 int thick = face->box_line_width;
21646
21647 if (thick > 0)
21648 {
21649 it->ascent += thick;
21650 it->descent += thick;
21651 }
21652 else
21653 thick = - thick;
21654
21655 if (it->start_of_box_run_p)
21656 it->pixel_width += thick;
21657 if (it->end_of_box_run_p)
21658 it->pixel_width += thick;
21659 }
21660
21661 /* If face has an overline, add the height of the overline
21662 (1 pixel) and a 1 pixel margin to the character height. */
21663 if (face->overline_p)
21664 it->ascent += overline_margin;
21665
21666 take_vertical_position_into_account (it);
21667 if (it->ascent < 0)
21668 it->ascent = 0;
21669 if (it->descent < 0)
21670 it->descent = 0;
21671
21672 if (it->glyph_row)
21673 append_composite_glyph (it);
21674 }
21675 else if (it->what == IT_COMPOSITION)
21676 {
21677 /* A dynamic (automatic) composition. */
21678 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21679 Lisp_Object gstring;
21680 struct font_metrics metrics;
21681
21682 gstring = composition_gstring_from_id (it->cmp_it.id);
21683 it->pixel_width
21684 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21685 &metrics);
21686 if (it->glyph_row
21687 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21688 it->glyph_row->contains_overlapping_glyphs_p = 1;
21689 it->ascent = it->phys_ascent = metrics.ascent;
21690 it->descent = it->phys_descent = metrics.descent;
21691 if (face->box != FACE_NO_BOX)
21692 {
21693 int thick = face->box_line_width;
21694
21695 if (thick > 0)
21696 {
21697 it->ascent += thick;
21698 it->descent += thick;
21699 }
21700 else
21701 thick = - thick;
21702
21703 if (it->start_of_box_run_p)
21704 it->pixel_width += thick;
21705 if (it->end_of_box_run_p)
21706 it->pixel_width += thick;
21707 }
21708 /* If face has an overline, add the height of the overline
21709 (1 pixel) and a 1 pixel margin to the character height. */
21710 if (face->overline_p)
21711 it->ascent += overline_margin;
21712 take_vertical_position_into_account (it);
21713 if (it->ascent < 0)
21714 it->ascent = 0;
21715 if (it->descent < 0)
21716 it->descent = 0;
21717
21718 if (it->glyph_row)
21719 append_composite_glyph (it);
21720 }
21721 else if (it->what == IT_IMAGE)
21722 produce_image_glyph (it);
21723 else if (it->what == IT_STRETCH)
21724 produce_stretch_glyph (it);
21725
21726 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21727 because this isn't true for images with `:ascent 100'. */
21728 xassert (it->ascent >= 0 && it->descent >= 0);
21729 if (it->area == TEXT_AREA)
21730 it->current_x += it->pixel_width;
21731
21732 if (extra_line_spacing > 0)
21733 {
21734 it->descent += extra_line_spacing;
21735 if (extra_line_spacing > it->max_extra_line_spacing)
21736 it->max_extra_line_spacing = extra_line_spacing;
21737 }
21738
21739 it->max_ascent = max (it->max_ascent, it->ascent);
21740 it->max_descent = max (it->max_descent, it->descent);
21741 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21742 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21743 }
21744
21745 /* EXPORT for RIF:
21746 Output LEN glyphs starting at START at the nominal cursor position.
21747 Advance the nominal cursor over the text. The global variable
21748 updated_window contains the window being updated, updated_row is
21749 the glyph row being updated, and updated_area is the area of that
21750 row being updated. */
21751
21752 void
21753 x_write_glyphs (start, len)
21754 struct glyph *start;
21755 int len;
21756 {
21757 int x, hpos;
21758
21759 xassert (updated_window && updated_row);
21760 BLOCK_INPUT;
21761
21762 /* Write glyphs. */
21763
21764 hpos = start - updated_row->glyphs[updated_area];
21765 x = draw_glyphs (updated_window, output_cursor.x,
21766 updated_row, updated_area,
21767 hpos, hpos + len,
21768 DRAW_NORMAL_TEXT, 0);
21769
21770 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21771 if (updated_area == TEXT_AREA
21772 && updated_window->phys_cursor_on_p
21773 && updated_window->phys_cursor.vpos == output_cursor.vpos
21774 && updated_window->phys_cursor.hpos >= hpos
21775 && updated_window->phys_cursor.hpos < hpos + len)
21776 updated_window->phys_cursor_on_p = 0;
21777
21778 UNBLOCK_INPUT;
21779
21780 /* Advance the output cursor. */
21781 output_cursor.hpos += len;
21782 output_cursor.x = x;
21783 }
21784
21785
21786 /* EXPORT for RIF:
21787 Insert LEN glyphs from START at the nominal cursor position. */
21788
21789 void
21790 x_insert_glyphs (start, len)
21791 struct glyph *start;
21792 int len;
21793 {
21794 struct frame *f;
21795 struct window *w;
21796 int line_height, shift_by_width, shifted_region_width;
21797 struct glyph_row *row;
21798 struct glyph *glyph;
21799 int frame_x, frame_y;
21800 EMACS_INT hpos;
21801
21802 xassert (updated_window && updated_row);
21803 BLOCK_INPUT;
21804 w = updated_window;
21805 f = XFRAME (WINDOW_FRAME (w));
21806
21807 /* Get the height of the line we are in. */
21808 row = updated_row;
21809 line_height = row->height;
21810
21811 /* Get the width of the glyphs to insert. */
21812 shift_by_width = 0;
21813 for (glyph = start; glyph < start + len; ++glyph)
21814 shift_by_width += glyph->pixel_width;
21815
21816 /* Get the width of the region to shift right. */
21817 shifted_region_width = (window_box_width (w, updated_area)
21818 - output_cursor.x
21819 - shift_by_width);
21820
21821 /* Shift right. */
21822 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21823 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21824
21825 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21826 line_height, shift_by_width);
21827
21828 /* Write the glyphs. */
21829 hpos = start - row->glyphs[updated_area];
21830 draw_glyphs (w, output_cursor.x, row, updated_area,
21831 hpos, hpos + len,
21832 DRAW_NORMAL_TEXT, 0);
21833
21834 /* Advance the output cursor. */
21835 output_cursor.hpos += len;
21836 output_cursor.x += shift_by_width;
21837 UNBLOCK_INPUT;
21838 }
21839
21840
21841 /* EXPORT for RIF:
21842 Erase the current text line from the nominal cursor position
21843 (inclusive) to pixel column TO_X (exclusive). The idea is that
21844 everything from TO_X onward is already erased.
21845
21846 TO_X is a pixel position relative to updated_area of
21847 updated_window. TO_X == -1 means clear to the end of this area. */
21848
21849 void
21850 x_clear_end_of_line (to_x)
21851 int to_x;
21852 {
21853 struct frame *f;
21854 struct window *w = updated_window;
21855 int max_x, min_y, max_y;
21856 int from_x, from_y, to_y;
21857
21858 xassert (updated_window && updated_row);
21859 f = XFRAME (w->frame);
21860
21861 if (updated_row->full_width_p)
21862 max_x = WINDOW_TOTAL_WIDTH (w);
21863 else
21864 max_x = window_box_width (w, updated_area);
21865 max_y = window_text_bottom_y (w);
21866
21867 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21868 of window. For TO_X > 0, truncate to end of drawing area. */
21869 if (to_x == 0)
21870 return;
21871 else if (to_x < 0)
21872 to_x = max_x;
21873 else
21874 to_x = min (to_x, max_x);
21875
21876 to_y = min (max_y, output_cursor.y + updated_row->height);
21877
21878 /* Notice if the cursor will be cleared by this operation. */
21879 if (!updated_row->full_width_p)
21880 notice_overwritten_cursor (w, updated_area,
21881 output_cursor.x, -1,
21882 updated_row->y,
21883 MATRIX_ROW_BOTTOM_Y (updated_row));
21884
21885 from_x = output_cursor.x;
21886
21887 /* Translate to frame coordinates. */
21888 if (updated_row->full_width_p)
21889 {
21890 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21891 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21892 }
21893 else
21894 {
21895 int area_left = window_box_left (w, updated_area);
21896 from_x += area_left;
21897 to_x += area_left;
21898 }
21899
21900 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21901 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21902 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21903
21904 /* Prevent inadvertently clearing to end of the X window. */
21905 if (to_x > from_x && to_y > from_y)
21906 {
21907 BLOCK_INPUT;
21908 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21909 to_x - from_x, to_y - from_y);
21910 UNBLOCK_INPUT;
21911 }
21912 }
21913
21914 #endif /* HAVE_WINDOW_SYSTEM */
21915
21916
21917 \f
21918 /***********************************************************************
21919 Cursor types
21920 ***********************************************************************/
21921
21922 /* Value is the internal representation of the specified cursor type
21923 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21924 of the bar cursor. */
21925
21926 static enum text_cursor_kinds
21927 get_specified_cursor_type (arg, width)
21928 Lisp_Object arg;
21929 int *width;
21930 {
21931 enum text_cursor_kinds type;
21932
21933 if (NILP (arg))
21934 return NO_CURSOR;
21935
21936 if (EQ (arg, Qbox))
21937 return FILLED_BOX_CURSOR;
21938
21939 if (EQ (arg, Qhollow))
21940 return HOLLOW_BOX_CURSOR;
21941
21942 if (EQ (arg, Qbar))
21943 {
21944 *width = 2;
21945 return BAR_CURSOR;
21946 }
21947
21948 if (CONSP (arg)
21949 && EQ (XCAR (arg), Qbar)
21950 && INTEGERP (XCDR (arg))
21951 && XINT (XCDR (arg)) >= 0)
21952 {
21953 *width = XINT (XCDR (arg));
21954 return BAR_CURSOR;
21955 }
21956
21957 if (EQ (arg, Qhbar))
21958 {
21959 *width = 2;
21960 return HBAR_CURSOR;
21961 }
21962
21963 if (CONSP (arg)
21964 && EQ (XCAR (arg), Qhbar)
21965 && INTEGERP (XCDR (arg))
21966 && XINT (XCDR (arg)) >= 0)
21967 {
21968 *width = XINT (XCDR (arg));
21969 return HBAR_CURSOR;
21970 }
21971
21972 /* Treat anything unknown as "hollow box cursor".
21973 It was bad to signal an error; people have trouble fixing
21974 .Xdefaults with Emacs, when it has something bad in it. */
21975 type = HOLLOW_BOX_CURSOR;
21976
21977 return type;
21978 }
21979
21980 /* Set the default cursor types for specified frame. */
21981 void
21982 set_frame_cursor_types (f, arg)
21983 struct frame *f;
21984 Lisp_Object arg;
21985 {
21986 int width;
21987 Lisp_Object tem;
21988
21989 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21990 FRAME_CURSOR_WIDTH (f) = width;
21991
21992 /* By default, set up the blink-off state depending on the on-state. */
21993
21994 tem = Fassoc (arg, Vblink_cursor_alist);
21995 if (!NILP (tem))
21996 {
21997 FRAME_BLINK_OFF_CURSOR (f)
21998 = get_specified_cursor_type (XCDR (tem), &width);
21999 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22000 }
22001 else
22002 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22003 }
22004
22005
22006 /* Return the cursor we want to be displayed in window W. Return
22007 width of bar/hbar cursor through WIDTH arg. Return with
22008 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22009 (i.e. if the `system caret' should track this cursor).
22010
22011 In a mini-buffer window, we want the cursor only to appear if we
22012 are reading input from this window. For the selected window, we
22013 want the cursor type given by the frame parameter or buffer local
22014 setting of cursor-type. If explicitly marked off, draw no cursor.
22015 In all other cases, we want a hollow box cursor. */
22016
22017 static enum text_cursor_kinds
22018 get_window_cursor_type (w, glyph, width, active_cursor)
22019 struct window *w;
22020 struct glyph *glyph;
22021 int *width;
22022 int *active_cursor;
22023 {
22024 struct frame *f = XFRAME (w->frame);
22025 struct buffer *b = XBUFFER (w->buffer);
22026 int cursor_type = DEFAULT_CURSOR;
22027 Lisp_Object alt_cursor;
22028 int non_selected = 0;
22029
22030 *active_cursor = 1;
22031
22032 /* Echo area */
22033 if (cursor_in_echo_area
22034 && FRAME_HAS_MINIBUF_P (f)
22035 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22036 {
22037 if (w == XWINDOW (echo_area_window))
22038 {
22039 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22040 {
22041 *width = FRAME_CURSOR_WIDTH (f);
22042 return FRAME_DESIRED_CURSOR (f);
22043 }
22044 else
22045 return get_specified_cursor_type (b->cursor_type, width);
22046 }
22047
22048 *active_cursor = 0;
22049 non_selected = 1;
22050 }
22051
22052 /* Detect a nonselected window or nonselected frame. */
22053 else if (w != XWINDOW (f->selected_window)
22054 #ifdef HAVE_WINDOW_SYSTEM
22055 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22056 #endif
22057 )
22058 {
22059 *active_cursor = 0;
22060
22061 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22062 return NO_CURSOR;
22063
22064 non_selected = 1;
22065 }
22066
22067 /* Never display a cursor in a window in which cursor-type is nil. */
22068 if (NILP (b->cursor_type))
22069 return NO_CURSOR;
22070
22071 /* Get the normal cursor type for this window. */
22072 if (EQ (b->cursor_type, Qt))
22073 {
22074 cursor_type = FRAME_DESIRED_CURSOR (f);
22075 *width = FRAME_CURSOR_WIDTH (f);
22076 }
22077 else
22078 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22079
22080 /* Use cursor-in-non-selected-windows instead
22081 for non-selected window or frame. */
22082 if (non_selected)
22083 {
22084 alt_cursor = b->cursor_in_non_selected_windows;
22085 if (!EQ (Qt, alt_cursor))
22086 return get_specified_cursor_type (alt_cursor, width);
22087 /* t means modify the normal cursor type. */
22088 if (cursor_type == FILLED_BOX_CURSOR)
22089 cursor_type = HOLLOW_BOX_CURSOR;
22090 else if (cursor_type == BAR_CURSOR && *width > 1)
22091 --*width;
22092 return cursor_type;
22093 }
22094
22095 /* Use normal cursor if not blinked off. */
22096 if (!w->cursor_off_p)
22097 {
22098 #ifdef HAVE_WINDOW_SYSTEM
22099 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22100 {
22101 if (cursor_type == FILLED_BOX_CURSOR)
22102 {
22103 /* Using a block cursor on large images can be very annoying.
22104 So use a hollow cursor for "large" images.
22105 If image is not transparent (no mask), also use hollow cursor. */
22106 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22107 if (img != NULL && IMAGEP (img->spec))
22108 {
22109 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22110 where N = size of default frame font size.
22111 This should cover most of the "tiny" icons people may use. */
22112 if (!img->mask
22113 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22114 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22115 cursor_type = HOLLOW_BOX_CURSOR;
22116 }
22117 }
22118 else if (cursor_type != NO_CURSOR)
22119 {
22120 /* Display current only supports BOX and HOLLOW cursors for images.
22121 So for now, unconditionally use a HOLLOW cursor when cursor is
22122 not a solid box cursor. */
22123 cursor_type = HOLLOW_BOX_CURSOR;
22124 }
22125 }
22126 #endif
22127 return cursor_type;
22128 }
22129
22130 /* Cursor is blinked off, so determine how to "toggle" it. */
22131
22132 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22133 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22134 return get_specified_cursor_type (XCDR (alt_cursor), width);
22135
22136 /* Then see if frame has specified a specific blink off cursor type. */
22137 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22138 {
22139 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22140 return FRAME_BLINK_OFF_CURSOR (f);
22141 }
22142
22143 #if 0
22144 /* Some people liked having a permanently visible blinking cursor,
22145 while others had very strong opinions against it. So it was
22146 decided to remove it. KFS 2003-09-03 */
22147
22148 /* Finally perform built-in cursor blinking:
22149 filled box <-> hollow box
22150 wide [h]bar <-> narrow [h]bar
22151 narrow [h]bar <-> no cursor
22152 other type <-> no cursor */
22153
22154 if (cursor_type == FILLED_BOX_CURSOR)
22155 return HOLLOW_BOX_CURSOR;
22156
22157 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22158 {
22159 *width = 1;
22160 return cursor_type;
22161 }
22162 #endif
22163
22164 return NO_CURSOR;
22165 }
22166
22167
22168 #ifdef HAVE_WINDOW_SYSTEM
22169
22170 /* Notice when the text cursor of window W has been completely
22171 overwritten by a drawing operation that outputs glyphs in AREA
22172 starting at X0 and ending at X1 in the line starting at Y0 and
22173 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22174 the rest of the line after X0 has been written. Y coordinates
22175 are window-relative. */
22176
22177 static void
22178 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22179 struct window *w;
22180 enum glyph_row_area area;
22181 int x0, y0, x1, y1;
22182 {
22183 int cx0, cx1, cy0, cy1;
22184 struct glyph_row *row;
22185
22186 if (!w->phys_cursor_on_p)
22187 return;
22188 if (area != TEXT_AREA)
22189 return;
22190
22191 if (w->phys_cursor.vpos < 0
22192 || w->phys_cursor.vpos >= w->current_matrix->nrows
22193 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22194 !(row->enabled_p && row->displays_text_p)))
22195 return;
22196
22197 if (row->cursor_in_fringe_p)
22198 {
22199 row->cursor_in_fringe_p = 0;
22200 draw_fringe_bitmap (w, row, 0);
22201 w->phys_cursor_on_p = 0;
22202 return;
22203 }
22204
22205 cx0 = w->phys_cursor.x;
22206 cx1 = cx0 + w->phys_cursor_width;
22207 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22208 return;
22209
22210 /* The cursor image will be completely removed from the
22211 screen if the output area intersects the cursor area in
22212 y-direction. When we draw in [y0 y1[, and some part of
22213 the cursor is at y < y0, that part must have been drawn
22214 before. When scrolling, the cursor is erased before
22215 actually scrolling, so we don't come here. When not
22216 scrolling, the rows above the old cursor row must have
22217 changed, and in this case these rows must have written
22218 over the cursor image.
22219
22220 Likewise if part of the cursor is below y1, with the
22221 exception of the cursor being in the first blank row at
22222 the buffer and window end because update_text_area
22223 doesn't draw that row. (Except when it does, but
22224 that's handled in update_text_area.) */
22225
22226 cy0 = w->phys_cursor.y;
22227 cy1 = cy0 + w->phys_cursor_height;
22228 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22229 return;
22230
22231 w->phys_cursor_on_p = 0;
22232 }
22233
22234 #endif /* HAVE_WINDOW_SYSTEM */
22235
22236 \f
22237 /************************************************************************
22238 Mouse Face
22239 ************************************************************************/
22240
22241 #ifdef HAVE_WINDOW_SYSTEM
22242
22243 /* EXPORT for RIF:
22244 Fix the display of area AREA of overlapping row ROW in window W
22245 with respect to the overlapping part OVERLAPS. */
22246
22247 void
22248 x_fix_overlapping_area (w, row, area, overlaps)
22249 struct window *w;
22250 struct glyph_row *row;
22251 enum glyph_row_area area;
22252 int overlaps;
22253 {
22254 int i, x;
22255
22256 BLOCK_INPUT;
22257
22258 x = 0;
22259 for (i = 0; i < row->used[area];)
22260 {
22261 if (row->glyphs[area][i].overlaps_vertically_p)
22262 {
22263 int start = i, start_x = x;
22264
22265 do
22266 {
22267 x += row->glyphs[area][i].pixel_width;
22268 ++i;
22269 }
22270 while (i < row->used[area]
22271 && row->glyphs[area][i].overlaps_vertically_p);
22272
22273 draw_glyphs (w, start_x, row, area,
22274 start, i,
22275 DRAW_NORMAL_TEXT, overlaps);
22276 }
22277 else
22278 {
22279 x += row->glyphs[area][i].pixel_width;
22280 ++i;
22281 }
22282 }
22283
22284 UNBLOCK_INPUT;
22285 }
22286
22287
22288 /* EXPORT:
22289 Draw the cursor glyph of window W in glyph row ROW. See the
22290 comment of draw_glyphs for the meaning of HL. */
22291
22292 void
22293 draw_phys_cursor_glyph (w, row, hl)
22294 struct window *w;
22295 struct glyph_row *row;
22296 enum draw_glyphs_face hl;
22297 {
22298 /* If cursor hpos is out of bounds, don't draw garbage. This can
22299 happen in mini-buffer windows when switching between echo area
22300 glyphs and mini-buffer. */
22301 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22302 {
22303 int on_p = w->phys_cursor_on_p;
22304 int x1;
22305 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22306 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22307 hl, 0);
22308 w->phys_cursor_on_p = on_p;
22309
22310 if (hl == DRAW_CURSOR)
22311 w->phys_cursor_width = x1 - w->phys_cursor.x;
22312 /* When we erase the cursor, and ROW is overlapped by other
22313 rows, make sure that these overlapping parts of other rows
22314 are redrawn. */
22315 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22316 {
22317 w->phys_cursor_width = x1 - w->phys_cursor.x;
22318
22319 if (row > w->current_matrix->rows
22320 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22321 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22322 OVERLAPS_ERASED_CURSOR);
22323
22324 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22325 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22326 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22327 OVERLAPS_ERASED_CURSOR);
22328 }
22329 }
22330 }
22331
22332
22333 /* EXPORT:
22334 Erase the image of a cursor of window W from the screen. */
22335
22336 void
22337 erase_phys_cursor (w)
22338 struct window *w;
22339 {
22340 struct frame *f = XFRAME (w->frame);
22341 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22342 int hpos = w->phys_cursor.hpos;
22343 int vpos = w->phys_cursor.vpos;
22344 int mouse_face_here_p = 0;
22345 struct glyph_matrix *active_glyphs = w->current_matrix;
22346 struct glyph_row *cursor_row;
22347 struct glyph *cursor_glyph;
22348 enum draw_glyphs_face hl;
22349
22350 /* No cursor displayed or row invalidated => nothing to do on the
22351 screen. */
22352 if (w->phys_cursor_type == NO_CURSOR)
22353 goto mark_cursor_off;
22354
22355 /* VPOS >= active_glyphs->nrows means that window has been resized.
22356 Don't bother to erase the cursor. */
22357 if (vpos >= active_glyphs->nrows)
22358 goto mark_cursor_off;
22359
22360 /* If row containing cursor is marked invalid, there is nothing we
22361 can do. */
22362 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22363 if (!cursor_row->enabled_p)
22364 goto mark_cursor_off;
22365
22366 /* If line spacing is > 0, old cursor may only be partially visible in
22367 window after split-window. So adjust visible height. */
22368 cursor_row->visible_height = min (cursor_row->visible_height,
22369 window_text_bottom_y (w) - cursor_row->y);
22370
22371 /* If row is completely invisible, don't attempt to delete a cursor which
22372 isn't there. This can happen if cursor is at top of a window, and
22373 we switch to a buffer with a header line in that window. */
22374 if (cursor_row->visible_height <= 0)
22375 goto mark_cursor_off;
22376
22377 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22378 if (cursor_row->cursor_in_fringe_p)
22379 {
22380 cursor_row->cursor_in_fringe_p = 0;
22381 draw_fringe_bitmap (w, cursor_row, 0);
22382 goto mark_cursor_off;
22383 }
22384
22385 /* This can happen when the new row is shorter than the old one.
22386 In this case, either draw_glyphs or clear_end_of_line
22387 should have cleared the cursor. Note that we wouldn't be
22388 able to erase the cursor in this case because we don't have a
22389 cursor glyph at hand. */
22390 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22391 goto mark_cursor_off;
22392
22393 /* If the cursor is in the mouse face area, redisplay that when
22394 we clear the cursor. */
22395 if (! NILP (dpyinfo->mouse_face_window)
22396 && w == XWINDOW (dpyinfo->mouse_face_window)
22397 && (vpos > dpyinfo->mouse_face_beg_row
22398 || (vpos == dpyinfo->mouse_face_beg_row
22399 && hpos >= dpyinfo->mouse_face_beg_col))
22400 && (vpos < dpyinfo->mouse_face_end_row
22401 || (vpos == dpyinfo->mouse_face_end_row
22402 && hpos < dpyinfo->mouse_face_end_col))
22403 /* Don't redraw the cursor's spot in mouse face if it is at the
22404 end of a line (on a newline). The cursor appears there, but
22405 mouse highlighting does not. */
22406 && cursor_row->used[TEXT_AREA] > hpos)
22407 mouse_face_here_p = 1;
22408
22409 /* Maybe clear the display under the cursor. */
22410 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22411 {
22412 int x, y, left_x;
22413 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22414 int width;
22415
22416 cursor_glyph = get_phys_cursor_glyph (w);
22417 if (cursor_glyph == NULL)
22418 goto mark_cursor_off;
22419
22420 width = cursor_glyph->pixel_width;
22421 left_x = window_box_left_offset (w, TEXT_AREA);
22422 x = w->phys_cursor.x;
22423 if (x < left_x)
22424 width -= left_x - x;
22425 width = min (width, window_box_width (w, TEXT_AREA) - x);
22426 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22427 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22428
22429 if (width > 0)
22430 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22431 }
22432
22433 /* Erase the cursor by redrawing the character underneath it. */
22434 if (mouse_face_here_p)
22435 hl = DRAW_MOUSE_FACE;
22436 else
22437 hl = DRAW_NORMAL_TEXT;
22438 draw_phys_cursor_glyph (w, cursor_row, hl);
22439
22440 mark_cursor_off:
22441 w->phys_cursor_on_p = 0;
22442 w->phys_cursor_type = NO_CURSOR;
22443 }
22444
22445
22446 /* EXPORT:
22447 Display or clear cursor of window W. If ON is zero, clear the
22448 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22449 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22450
22451 void
22452 display_and_set_cursor (w, on, hpos, vpos, x, y)
22453 struct window *w;
22454 int on, hpos, vpos, x, y;
22455 {
22456 struct frame *f = XFRAME (w->frame);
22457 int new_cursor_type;
22458 int new_cursor_width;
22459 int active_cursor;
22460 struct glyph_row *glyph_row;
22461 struct glyph *glyph;
22462
22463 /* This is pointless on invisible frames, and dangerous on garbaged
22464 windows and frames; in the latter case, the frame or window may
22465 be in the midst of changing its size, and x and y may be off the
22466 window. */
22467 if (! FRAME_VISIBLE_P (f)
22468 || FRAME_GARBAGED_P (f)
22469 || vpos >= w->current_matrix->nrows
22470 || hpos >= w->current_matrix->matrix_w)
22471 return;
22472
22473 /* If cursor is off and we want it off, return quickly. */
22474 if (!on && !w->phys_cursor_on_p)
22475 return;
22476
22477 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22478 /* If cursor row is not enabled, we don't really know where to
22479 display the cursor. */
22480 if (!glyph_row->enabled_p)
22481 {
22482 w->phys_cursor_on_p = 0;
22483 return;
22484 }
22485
22486 glyph = NULL;
22487 if (!glyph_row->exact_window_width_line_p
22488 || hpos < glyph_row->used[TEXT_AREA])
22489 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22490
22491 xassert (interrupt_input_blocked);
22492
22493 /* Set new_cursor_type to the cursor we want to be displayed. */
22494 new_cursor_type = get_window_cursor_type (w, glyph,
22495 &new_cursor_width, &active_cursor);
22496
22497 /* If cursor is currently being shown and we don't want it to be or
22498 it is in the wrong place, or the cursor type is not what we want,
22499 erase it. */
22500 if (w->phys_cursor_on_p
22501 && (!on
22502 || w->phys_cursor.x != x
22503 || w->phys_cursor.y != y
22504 || new_cursor_type != w->phys_cursor_type
22505 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22506 && new_cursor_width != w->phys_cursor_width)))
22507 erase_phys_cursor (w);
22508
22509 /* Don't check phys_cursor_on_p here because that flag is only set
22510 to zero in some cases where we know that the cursor has been
22511 completely erased, to avoid the extra work of erasing the cursor
22512 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22513 still not be visible, or it has only been partly erased. */
22514 if (on)
22515 {
22516 w->phys_cursor_ascent = glyph_row->ascent;
22517 w->phys_cursor_height = glyph_row->height;
22518
22519 /* Set phys_cursor_.* before x_draw_.* is called because some
22520 of them may need the information. */
22521 w->phys_cursor.x = x;
22522 w->phys_cursor.y = glyph_row->y;
22523 w->phys_cursor.hpos = hpos;
22524 w->phys_cursor.vpos = vpos;
22525 }
22526
22527 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22528 new_cursor_type, new_cursor_width,
22529 on, active_cursor);
22530 }
22531
22532
22533 /* Switch the display of W's cursor on or off, according to the value
22534 of ON. */
22535
22536 void
22537 update_window_cursor (w, on)
22538 struct window *w;
22539 int on;
22540 {
22541 /* Don't update cursor in windows whose frame is in the process
22542 of being deleted. */
22543 if (w->current_matrix)
22544 {
22545 BLOCK_INPUT;
22546 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22547 w->phys_cursor.x, w->phys_cursor.y);
22548 UNBLOCK_INPUT;
22549 }
22550 }
22551
22552
22553 /* Call update_window_cursor with parameter ON_P on all leaf windows
22554 in the window tree rooted at W. */
22555
22556 static void
22557 update_cursor_in_window_tree (w, on_p)
22558 struct window *w;
22559 int on_p;
22560 {
22561 while (w)
22562 {
22563 if (!NILP (w->hchild))
22564 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22565 else if (!NILP (w->vchild))
22566 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22567 else
22568 update_window_cursor (w, on_p);
22569
22570 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22571 }
22572 }
22573
22574
22575 /* EXPORT:
22576 Display the cursor on window W, or clear it, according to ON_P.
22577 Don't change the cursor's position. */
22578
22579 void
22580 x_update_cursor (f, on_p)
22581 struct frame *f;
22582 int on_p;
22583 {
22584 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22585 }
22586
22587
22588 /* EXPORT:
22589 Clear the cursor of window W to background color, and mark the
22590 cursor as not shown. This is used when the text where the cursor
22591 is about to be rewritten. */
22592
22593 void
22594 x_clear_cursor (w)
22595 struct window *w;
22596 {
22597 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22598 update_window_cursor (w, 0);
22599 }
22600
22601
22602 /* EXPORT:
22603 Display the active region described by mouse_face_* according to DRAW. */
22604
22605 void
22606 show_mouse_face (dpyinfo, draw)
22607 Display_Info *dpyinfo;
22608 enum draw_glyphs_face draw;
22609 {
22610 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22611 struct frame *f = XFRAME (WINDOW_FRAME (w));
22612
22613 if (/* If window is in the process of being destroyed, don't bother
22614 to do anything. */
22615 w->current_matrix != NULL
22616 /* Don't update mouse highlight if hidden */
22617 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22618 /* Recognize when we are called to operate on rows that don't exist
22619 anymore. This can happen when a window is split. */
22620 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22621 {
22622 int phys_cursor_on_p = w->phys_cursor_on_p;
22623 struct glyph_row *row, *first, *last;
22624
22625 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22626 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22627
22628 for (row = first; row <= last && row->enabled_p; ++row)
22629 {
22630 int start_hpos, end_hpos, start_x;
22631
22632 /* For all but the first row, the highlight starts at column 0. */
22633 if (row == first)
22634 {
22635 start_hpos = dpyinfo->mouse_face_beg_col;
22636 start_x = dpyinfo->mouse_face_beg_x;
22637 }
22638 else
22639 {
22640 start_hpos = 0;
22641 start_x = 0;
22642 }
22643
22644 if (row == last)
22645 end_hpos = dpyinfo->mouse_face_end_col;
22646 else
22647 {
22648 end_hpos = row->used[TEXT_AREA];
22649 if (draw == DRAW_NORMAL_TEXT)
22650 row->fill_line_p = 1; /* Clear to end of line */
22651 }
22652
22653 if (end_hpos > start_hpos)
22654 {
22655 draw_glyphs (w, start_x, row, TEXT_AREA,
22656 start_hpos, end_hpos,
22657 draw, 0);
22658
22659 row->mouse_face_p
22660 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22661 }
22662 }
22663
22664 /* When we've written over the cursor, arrange for it to
22665 be displayed again. */
22666 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22667 {
22668 BLOCK_INPUT;
22669 display_and_set_cursor (w, 1,
22670 w->phys_cursor.hpos, w->phys_cursor.vpos,
22671 w->phys_cursor.x, w->phys_cursor.y);
22672 UNBLOCK_INPUT;
22673 }
22674 }
22675
22676 /* Change the mouse cursor. */
22677 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22678 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22679 else if (draw == DRAW_MOUSE_FACE)
22680 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22681 else
22682 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22683 }
22684
22685 /* EXPORT:
22686 Clear out the mouse-highlighted active region.
22687 Redraw it un-highlighted first. Value is non-zero if mouse
22688 face was actually drawn unhighlighted. */
22689
22690 int
22691 clear_mouse_face (dpyinfo)
22692 Display_Info *dpyinfo;
22693 {
22694 int cleared = 0;
22695
22696 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22697 {
22698 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22699 cleared = 1;
22700 }
22701
22702 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22703 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22704 dpyinfo->mouse_face_window = Qnil;
22705 dpyinfo->mouse_face_overlay = Qnil;
22706 return cleared;
22707 }
22708
22709
22710 /* EXPORT:
22711 Non-zero if physical cursor of window W is within mouse face. */
22712
22713 int
22714 cursor_in_mouse_face_p (w)
22715 struct window *w;
22716 {
22717 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22718 int in_mouse_face = 0;
22719
22720 if (WINDOWP (dpyinfo->mouse_face_window)
22721 && XWINDOW (dpyinfo->mouse_face_window) == w)
22722 {
22723 int hpos = w->phys_cursor.hpos;
22724 int vpos = w->phys_cursor.vpos;
22725
22726 if (vpos >= dpyinfo->mouse_face_beg_row
22727 && vpos <= dpyinfo->mouse_face_end_row
22728 && (vpos > dpyinfo->mouse_face_beg_row
22729 || hpos >= dpyinfo->mouse_face_beg_col)
22730 && (vpos < dpyinfo->mouse_face_end_row
22731 || hpos < dpyinfo->mouse_face_end_col
22732 || dpyinfo->mouse_face_past_end))
22733 in_mouse_face = 1;
22734 }
22735
22736 return in_mouse_face;
22737 }
22738
22739
22740
22741 \f
22742 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22743 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22744 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22745 for the overlay or run of text properties specifying the mouse
22746 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22747 before-string and after-string that must also be highlighted.
22748 DISPLAY_STRING, if non-nil, is a display string that may cover some
22749 or all of the highlighted text. */
22750
22751 static void
22752 mouse_face_from_buffer_pos (Lisp_Object window,
22753 Display_Info *dpyinfo,
22754 EMACS_INT mouse_charpos,
22755 EMACS_INT start_charpos,
22756 EMACS_INT end_charpos,
22757 Lisp_Object before_string,
22758 Lisp_Object after_string,
22759 Lisp_Object display_string)
22760 {
22761 struct window *w = XWINDOW (window);
22762 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22763 struct glyph_row *row;
22764 struct glyph *glyph, *end;
22765 EMACS_INT ignore;
22766 int x;
22767
22768 xassert (NILP (display_string) || STRINGP (display_string));
22769 xassert (NILP (before_string) || STRINGP (before_string));
22770 xassert (NILP (after_string) || STRINGP (after_string));
22771
22772 /* Find the first highlighted glyph. */
22773 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22774 {
22775 dpyinfo->mouse_face_beg_col = 0;
22776 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22777 dpyinfo->mouse_face_beg_x = first->x;
22778 dpyinfo->mouse_face_beg_y = first->y;
22779 }
22780 else
22781 {
22782 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22783 if (row == NULL)
22784 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22785
22786 /* If the before-string or display-string contains newlines,
22787 row_containing_pos skips to its last row. Move back. */
22788 if (!NILP (before_string) || !NILP (display_string))
22789 {
22790 struct glyph_row *prev;
22791 while ((prev = row - 1, prev >= first)
22792 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22793 && prev->used[TEXT_AREA] > 0)
22794 {
22795 struct glyph *beg = prev->glyphs[TEXT_AREA];
22796 glyph = beg + prev->used[TEXT_AREA];
22797 while (--glyph >= beg && INTEGERP (glyph->object));
22798 if (glyph < beg
22799 || !(EQ (glyph->object, before_string)
22800 || EQ (glyph->object, display_string)))
22801 break;
22802 row = prev;
22803 }
22804 }
22805
22806 glyph = row->glyphs[TEXT_AREA];
22807 end = glyph + row->used[TEXT_AREA];
22808 x = row->x;
22809 dpyinfo->mouse_face_beg_y = row->y;
22810 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22811
22812 /* Skip truncation glyphs at the start of the glyph row. */
22813 if (row->displays_text_p)
22814 for (; glyph < end
22815 && INTEGERP (glyph->object)
22816 && glyph->charpos < 0;
22817 ++glyph)
22818 x += glyph->pixel_width;
22819
22820 /* Scan the glyph row, stopping before BEFORE_STRING or
22821 DISPLAY_STRING or START_CHARPOS. */
22822 for (; glyph < end
22823 && !INTEGERP (glyph->object)
22824 && !EQ (glyph->object, before_string)
22825 && !EQ (glyph->object, display_string)
22826 && !(BUFFERP (glyph->object)
22827 && glyph->charpos >= start_charpos);
22828 ++glyph)
22829 x += glyph->pixel_width;
22830
22831 dpyinfo->mouse_face_beg_x = x;
22832 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22833 }
22834
22835 /* Find the last highlighted glyph. */
22836 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22837 if (row == NULL)
22838 {
22839 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22840 dpyinfo->mouse_face_past_end = 1;
22841 }
22842 else if (!NILP (after_string))
22843 {
22844 /* If the after-string has newlines, advance to its last row. */
22845 struct glyph_row *next;
22846 struct glyph_row *last
22847 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22848
22849 for (next = row + 1;
22850 next <= last
22851 && next->used[TEXT_AREA] > 0
22852 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22853 ++next)
22854 row = next;
22855 }
22856
22857 glyph = row->glyphs[TEXT_AREA];
22858 end = glyph + row->used[TEXT_AREA];
22859 x = row->x;
22860 dpyinfo->mouse_face_end_y = row->y;
22861 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22862
22863 /* Skip truncation glyphs at the start of the row. */
22864 if (row->displays_text_p)
22865 for (; glyph < end
22866 && INTEGERP (glyph->object)
22867 && glyph->charpos < 0;
22868 ++glyph)
22869 x += glyph->pixel_width;
22870
22871 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22872 AFTER_STRING. */
22873 for (; glyph < end
22874 && !INTEGERP (glyph->object)
22875 && !EQ (glyph->object, after_string)
22876 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22877 ++glyph)
22878 x += glyph->pixel_width;
22879
22880 /* If we found AFTER_STRING, consume it and stop. */
22881 if (EQ (glyph->object, after_string))
22882 {
22883 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22884 x += glyph->pixel_width;
22885 }
22886 else
22887 {
22888 /* If there's no after-string, we must check if we overshot,
22889 which might be the case if we stopped after a string glyph.
22890 That glyph may belong to a before-string or display-string
22891 associated with the end position, which must not be
22892 highlighted. */
22893 Lisp_Object prev_object;
22894 int pos;
22895
22896 while (glyph > row->glyphs[TEXT_AREA])
22897 {
22898 prev_object = (glyph - 1)->object;
22899 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
22900 break;
22901
22902 pos = string_buffer_position (w, prev_object, end_charpos);
22903 if (pos && pos < end_charpos)
22904 break;
22905
22906 for (; glyph > row->glyphs[TEXT_AREA]
22907 && EQ ((glyph - 1)->object, prev_object);
22908 --glyph)
22909 x -= (glyph - 1)->pixel_width;
22910 }
22911 }
22912
22913 dpyinfo->mouse_face_end_x = x;
22914 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
22915 dpyinfo->mouse_face_window = window;
22916 dpyinfo->mouse_face_face_id
22917 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
22918 mouse_charpos + 1,
22919 !dpyinfo->mouse_face_hidden, -1);
22920 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22921 }
22922
22923
22924 /* Find the position of the glyph for position POS in OBJECT in
22925 window W's current matrix, and return in *X, *Y the pixel
22926 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22927
22928 RIGHT_P non-zero means return the position of the right edge of the
22929 glyph, RIGHT_P zero means return the left edge position.
22930
22931 If no glyph for POS exists in the matrix, return the position of
22932 the glyph with the next smaller position that is in the matrix, if
22933 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22934 exists in the matrix, return the position of the glyph with the
22935 next larger position in OBJECT.
22936
22937 Value is non-zero if a glyph was found. */
22938
22939 static int
22940 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22941 struct window *w;
22942 EMACS_INT pos;
22943 Lisp_Object object;
22944 int *hpos, *vpos, *x, *y;
22945 int right_p;
22946 {
22947 int yb = window_text_bottom_y (w);
22948 struct glyph_row *r;
22949 struct glyph *best_glyph = NULL;
22950 struct glyph_row *best_row = NULL;
22951 int best_x = 0;
22952
22953 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22954 r->enabled_p && r->y < yb;
22955 ++r)
22956 {
22957 struct glyph *g = r->glyphs[TEXT_AREA];
22958 struct glyph *e = g + r->used[TEXT_AREA];
22959 int gx;
22960
22961 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22962 if (EQ (g->object, object))
22963 {
22964 if (g->charpos == pos)
22965 {
22966 best_glyph = g;
22967 best_x = gx;
22968 best_row = r;
22969 goto found;
22970 }
22971 else if (best_glyph == NULL
22972 || ((eabs (g->charpos - pos)
22973 < eabs (best_glyph->charpos - pos))
22974 && (right_p
22975 ? g->charpos < pos
22976 : g->charpos > pos)))
22977 {
22978 best_glyph = g;
22979 best_x = gx;
22980 best_row = r;
22981 }
22982 }
22983 }
22984
22985 found:
22986
22987 if (best_glyph)
22988 {
22989 *x = best_x;
22990 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22991
22992 if (right_p)
22993 {
22994 *x += best_glyph->pixel_width;
22995 ++*hpos;
22996 }
22997
22998 *y = best_row->y;
22999 *vpos = best_row - w->current_matrix->rows;
23000 }
23001
23002 return best_glyph != NULL;
23003 }
23004
23005
23006 /* See if position X, Y is within a hot-spot of an image. */
23007
23008 static int
23009 on_hot_spot_p (hot_spot, x, y)
23010 Lisp_Object hot_spot;
23011 int x, y;
23012 {
23013 if (!CONSP (hot_spot))
23014 return 0;
23015
23016 if (EQ (XCAR (hot_spot), Qrect))
23017 {
23018 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23019 Lisp_Object rect = XCDR (hot_spot);
23020 Lisp_Object tem;
23021 if (!CONSP (rect))
23022 return 0;
23023 if (!CONSP (XCAR (rect)))
23024 return 0;
23025 if (!CONSP (XCDR (rect)))
23026 return 0;
23027 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23028 return 0;
23029 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23030 return 0;
23031 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23032 return 0;
23033 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23034 return 0;
23035 return 1;
23036 }
23037 else if (EQ (XCAR (hot_spot), Qcircle))
23038 {
23039 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23040 Lisp_Object circ = XCDR (hot_spot);
23041 Lisp_Object lr, lx0, ly0;
23042 if (CONSP (circ)
23043 && CONSP (XCAR (circ))
23044 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23045 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23046 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23047 {
23048 double r = XFLOATINT (lr);
23049 double dx = XINT (lx0) - x;
23050 double dy = XINT (ly0) - y;
23051 return (dx * dx + dy * dy <= r * r);
23052 }
23053 }
23054 else if (EQ (XCAR (hot_spot), Qpoly))
23055 {
23056 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23057 if (VECTORP (XCDR (hot_spot)))
23058 {
23059 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23060 Lisp_Object *poly = v->contents;
23061 int n = v->size;
23062 int i;
23063 int inside = 0;
23064 Lisp_Object lx, ly;
23065 int x0, y0;
23066
23067 /* Need an even number of coordinates, and at least 3 edges. */
23068 if (n < 6 || n & 1)
23069 return 0;
23070
23071 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23072 If count is odd, we are inside polygon. Pixels on edges
23073 may or may not be included depending on actual geometry of the
23074 polygon. */
23075 if ((lx = poly[n-2], !INTEGERP (lx))
23076 || (ly = poly[n-1], !INTEGERP (lx)))
23077 return 0;
23078 x0 = XINT (lx), y0 = XINT (ly);
23079 for (i = 0; i < n; i += 2)
23080 {
23081 int x1 = x0, y1 = y0;
23082 if ((lx = poly[i], !INTEGERP (lx))
23083 || (ly = poly[i+1], !INTEGERP (ly)))
23084 return 0;
23085 x0 = XINT (lx), y0 = XINT (ly);
23086
23087 /* Does this segment cross the X line? */
23088 if (x0 >= x)
23089 {
23090 if (x1 >= x)
23091 continue;
23092 }
23093 else if (x1 < x)
23094 continue;
23095 if (y > y0 && y > y1)
23096 continue;
23097 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23098 inside = !inside;
23099 }
23100 return inside;
23101 }
23102 }
23103 return 0;
23104 }
23105
23106 Lisp_Object
23107 find_hot_spot (map, x, y)
23108 Lisp_Object map;
23109 int x, y;
23110 {
23111 while (CONSP (map))
23112 {
23113 if (CONSP (XCAR (map))
23114 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23115 return XCAR (map);
23116 map = XCDR (map);
23117 }
23118
23119 return Qnil;
23120 }
23121
23122 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23123 3, 3, 0,
23124 doc: /* Lookup in image map MAP coordinates X and Y.
23125 An image map is an alist where each element has the format (AREA ID PLIST).
23126 An AREA is specified as either a rectangle, a circle, or a polygon:
23127 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23128 pixel coordinates of the upper left and bottom right corners.
23129 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23130 and the radius of the circle; r may be a float or integer.
23131 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23132 vector describes one corner in the polygon.
23133 Returns the alist element for the first matching AREA in MAP. */)
23134 (map, x, y)
23135 Lisp_Object map;
23136 Lisp_Object x, y;
23137 {
23138 if (NILP (map))
23139 return Qnil;
23140
23141 CHECK_NUMBER (x);
23142 CHECK_NUMBER (y);
23143
23144 return find_hot_spot (map, XINT (x), XINT (y));
23145 }
23146
23147
23148 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23149 static void
23150 define_frame_cursor1 (f, cursor, pointer)
23151 struct frame *f;
23152 Cursor cursor;
23153 Lisp_Object pointer;
23154 {
23155 /* Do not change cursor shape while dragging mouse. */
23156 if (!NILP (do_mouse_tracking))
23157 return;
23158
23159 if (!NILP (pointer))
23160 {
23161 if (EQ (pointer, Qarrow))
23162 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23163 else if (EQ (pointer, Qhand))
23164 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23165 else if (EQ (pointer, Qtext))
23166 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23167 else if (EQ (pointer, intern ("hdrag")))
23168 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23169 #ifdef HAVE_X_WINDOWS
23170 else if (EQ (pointer, intern ("vdrag")))
23171 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23172 #endif
23173 else if (EQ (pointer, intern ("hourglass")))
23174 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23175 else if (EQ (pointer, Qmodeline))
23176 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23177 else
23178 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23179 }
23180
23181 if (cursor != No_Cursor)
23182 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23183 }
23184
23185 /* Take proper action when mouse has moved to the mode or header line
23186 or marginal area AREA of window W, x-position X and y-position Y.
23187 X is relative to the start of the text display area of W, so the
23188 width of bitmap areas and scroll bars must be subtracted to get a
23189 position relative to the start of the mode line. */
23190
23191 static void
23192 note_mode_line_or_margin_highlight (window, x, y, area)
23193 Lisp_Object window;
23194 int x, y;
23195 enum window_part area;
23196 {
23197 struct window *w = XWINDOW (window);
23198 struct frame *f = XFRAME (w->frame);
23199 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23200 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23201 Lisp_Object pointer = Qnil;
23202 int charpos, dx, dy, width, height;
23203 Lisp_Object string, object = Qnil;
23204 Lisp_Object pos, help;
23205
23206 Lisp_Object mouse_face;
23207 int original_x_pixel = x;
23208 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23209 struct glyph_row *row;
23210
23211 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23212 {
23213 int x0;
23214 struct glyph *end;
23215
23216 string = mode_line_string (w, area, &x, &y, &charpos,
23217 &object, &dx, &dy, &width, &height);
23218
23219 row = (area == ON_MODE_LINE
23220 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23221 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23222
23223 /* Find glyph */
23224 if (row->mode_line_p && row->enabled_p)
23225 {
23226 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23227 end = glyph + row->used[TEXT_AREA];
23228
23229 for (x0 = original_x_pixel;
23230 glyph < end && x0 >= glyph->pixel_width;
23231 ++glyph)
23232 x0 -= glyph->pixel_width;
23233
23234 if (glyph >= end)
23235 glyph = NULL;
23236 }
23237 }
23238 else
23239 {
23240 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23241 string = marginal_area_string (w, area, &x, &y, &charpos,
23242 &object, &dx, &dy, &width, &height);
23243 }
23244
23245 help = Qnil;
23246
23247 if (IMAGEP (object))
23248 {
23249 Lisp_Object image_map, hotspot;
23250 if ((image_map = Fplist_get (XCDR (object), QCmap),
23251 !NILP (image_map))
23252 && (hotspot = find_hot_spot (image_map, dx, dy),
23253 CONSP (hotspot))
23254 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23255 {
23256 Lisp_Object area_id, plist;
23257
23258 area_id = XCAR (hotspot);
23259 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23260 If so, we could look for mouse-enter, mouse-leave
23261 properties in PLIST (and do something...). */
23262 hotspot = XCDR (hotspot);
23263 if (CONSP (hotspot)
23264 && (plist = XCAR (hotspot), CONSP (plist)))
23265 {
23266 pointer = Fplist_get (plist, Qpointer);
23267 if (NILP (pointer))
23268 pointer = Qhand;
23269 help = Fplist_get (plist, Qhelp_echo);
23270 if (!NILP (help))
23271 {
23272 help_echo_string = help;
23273 /* Is this correct? ++kfs */
23274 XSETWINDOW (help_echo_window, w);
23275 help_echo_object = w->buffer;
23276 help_echo_pos = charpos;
23277 }
23278 }
23279 }
23280 if (NILP (pointer))
23281 pointer = Fplist_get (XCDR (object), QCpointer);
23282 }
23283
23284 if (STRINGP (string))
23285 {
23286 pos = make_number (charpos);
23287 /* If we're on a string with `help-echo' text property, arrange
23288 for the help to be displayed. This is done by setting the
23289 global variable help_echo_string to the help string. */
23290 if (NILP (help))
23291 {
23292 help = Fget_text_property (pos, Qhelp_echo, string);
23293 if (!NILP (help))
23294 {
23295 help_echo_string = help;
23296 XSETWINDOW (help_echo_window, w);
23297 help_echo_object = string;
23298 help_echo_pos = charpos;
23299 }
23300 }
23301
23302 if (NILP (pointer))
23303 pointer = Fget_text_property (pos, Qpointer, string);
23304
23305 /* Change the mouse pointer according to what is under X/Y. */
23306 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23307 {
23308 Lisp_Object map;
23309 map = Fget_text_property (pos, Qlocal_map, string);
23310 if (!KEYMAPP (map))
23311 map = Fget_text_property (pos, Qkeymap, string);
23312 if (!KEYMAPP (map))
23313 cursor = dpyinfo->vertical_scroll_bar_cursor;
23314 }
23315
23316 /* Change the mouse face according to what is under X/Y. */
23317 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23318 if (!NILP (mouse_face)
23319 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23320 && glyph)
23321 {
23322 Lisp_Object b, e;
23323
23324 struct glyph * tmp_glyph;
23325
23326 int gpos;
23327 int gseq_length;
23328 int total_pixel_width;
23329 EMACS_INT ignore;
23330
23331 int vpos, hpos;
23332
23333 b = Fprevious_single_property_change (make_number (charpos + 1),
23334 Qmouse_face, string, Qnil);
23335 if (NILP (b))
23336 b = make_number (0);
23337
23338 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23339 if (NILP (e))
23340 e = make_number (SCHARS (string));
23341
23342 /* Calculate the position(glyph position: GPOS) of GLYPH in
23343 displayed string. GPOS is different from CHARPOS.
23344
23345 CHARPOS is the position of glyph in internal string
23346 object. A mode line string format has structures which
23347 is converted to a flatten by emacs lisp interpreter.
23348 The internal string is an element of the structures.
23349 The displayed string is the flatten string. */
23350 gpos = 0;
23351 if (glyph > row_start_glyph)
23352 {
23353 tmp_glyph = glyph - 1;
23354 while (tmp_glyph >= row_start_glyph
23355 && tmp_glyph->charpos >= XINT (b)
23356 && EQ (tmp_glyph->object, glyph->object))
23357 {
23358 tmp_glyph--;
23359 gpos++;
23360 }
23361 }
23362
23363 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23364 displayed string holding GLYPH.
23365
23366 GSEQ_LENGTH is different from SCHARS (STRING).
23367 SCHARS (STRING) returns the length of the internal string. */
23368 for (tmp_glyph = glyph, gseq_length = gpos;
23369 tmp_glyph->charpos < XINT (e);
23370 tmp_glyph++, gseq_length++)
23371 {
23372 if (!EQ (tmp_glyph->object, glyph->object))
23373 break;
23374 }
23375
23376 total_pixel_width = 0;
23377 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23378 total_pixel_width += tmp_glyph->pixel_width;
23379
23380 /* Pre calculation of re-rendering position */
23381 vpos = (x - gpos);
23382 hpos = (area == ON_MODE_LINE
23383 ? (w->current_matrix)->nrows - 1
23384 : 0);
23385
23386 /* If the re-rendering position is included in the last
23387 re-rendering area, we should do nothing. */
23388 if ( EQ (window, dpyinfo->mouse_face_window)
23389 && dpyinfo->mouse_face_beg_col <= vpos
23390 && vpos < dpyinfo->mouse_face_end_col
23391 && dpyinfo->mouse_face_beg_row == hpos )
23392 return;
23393
23394 if (clear_mouse_face (dpyinfo))
23395 cursor = No_Cursor;
23396
23397 dpyinfo->mouse_face_beg_col = vpos;
23398 dpyinfo->mouse_face_beg_row = hpos;
23399
23400 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23401 dpyinfo->mouse_face_beg_y = 0;
23402
23403 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23404 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23405
23406 dpyinfo->mouse_face_end_x = 0;
23407 dpyinfo->mouse_face_end_y = 0;
23408
23409 dpyinfo->mouse_face_past_end = 0;
23410 dpyinfo->mouse_face_window = window;
23411
23412 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23413 charpos,
23414 0, 0, 0, &ignore,
23415 glyph->face_id, 1);
23416 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23417
23418 if (NILP (pointer))
23419 pointer = Qhand;
23420 }
23421 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23422 clear_mouse_face (dpyinfo);
23423 }
23424 define_frame_cursor1 (f, cursor, pointer);
23425 }
23426
23427
23428 /* EXPORT:
23429 Take proper action when the mouse has moved to position X, Y on
23430 frame F as regards highlighting characters that have mouse-face
23431 properties. Also de-highlighting chars where the mouse was before.
23432 X and Y can be negative or out of range. */
23433
23434 void
23435 note_mouse_highlight (f, x, y)
23436 struct frame *f;
23437 int x, y;
23438 {
23439 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23440 enum window_part part;
23441 Lisp_Object window;
23442 struct window *w;
23443 Cursor cursor = No_Cursor;
23444 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23445 struct buffer *b;
23446
23447 /* When a menu is active, don't highlight because this looks odd. */
23448 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23449 if (popup_activated ())
23450 return;
23451 #endif
23452
23453 if (NILP (Vmouse_highlight)
23454 || !f->glyphs_initialized_p
23455 || f->pointer_invisible)
23456 return;
23457
23458 dpyinfo->mouse_face_mouse_x = x;
23459 dpyinfo->mouse_face_mouse_y = y;
23460 dpyinfo->mouse_face_mouse_frame = f;
23461
23462 if (dpyinfo->mouse_face_defer)
23463 return;
23464
23465 if (gc_in_progress)
23466 {
23467 dpyinfo->mouse_face_deferred_gc = 1;
23468 return;
23469 }
23470
23471 /* Which window is that in? */
23472 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23473
23474 /* If we were displaying active text in another window, clear that.
23475 Also clear if we move out of text area in same window. */
23476 if (! EQ (window, dpyinfo->mouse_face_window)
23477 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23478 && !NILP (dpyinfo->mouse_face_window)))
23479 clear_mouse_face (dpyinfo);
23480
23481 /* Not on a window -> return. */
23482 if (!WINDOWP (window))
23483 return;
23484
23485 /* Reset help_echo_string. It will get recomputed below. */
23486 help_echo_string = Qnil;
23487
23488 /* Convert to window-relative pixel coordinates. */
23489 w = XWINDOW (window);
23490 frame_to_window_pixel_xy (w, &x, &y);
23491
23492 /* Handle tool-bar window differently since it doesn't display a
23493 buffer. */
23494 if (EQ (window, f->tool_bar_window))
23495 {
23496 note_tool_bar_highlight (f, x, y);
23497 return;
23498 }
23499
23500 /* Mouse is on the mode, header line or margin? */
23501 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23502 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23503 {
23504 note_mode_line_or_margin_highlight (window, x, y, part);
23505 return;
23506 }
23507
23508 if (part == ON_VERTICAL_BORDER)
23509 {
23510 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23511 help_echo_string = build_string ("drag-mouse-1: resize");
23512 }
23513 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23514 || part == ON_SCROLL_BAR)
23515 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23516 else
23517 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23518
23519 /* Are we in a window whose display is up to date?
23520 And verify the buffer's text has not changed. */
23521 b = XBUFFER (w->buffer);
23522 if (part == ON_TEXT
23523 && EQ (w->window_end_valid, w->buffer)
23524 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23525 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23526 {
23527 int hpos, vpos, pos, i, dx, dy, area;
23528 struct glyph *glyph;
23529 Lisp_Object object;
23530 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23531 Lisp_Object *overlay_vec = NULL;
23532 int noverlays;
23533 struct buffer *obuf;
23534 int obegv, ozv, same_region;
23535
23536 /* Find the glyph under X/Y. */
23537 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23538
23539 /* Look for :pointer property on image. */
23540 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23541 {
23542 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23543 if (img != NULL && IMAGEP (img->spec))
23544 {
23545 Lisp_Object image_map, hotspot;
23546 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23547 !NILP (image_map))
23548 && (hotspot = find_hot_spot (image_map,
23549 glyph->slice.x + dx,
23550 glyph->slice.y + dy),
23551 CONSP (hotspot))
23552 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23553 {
23554 Lisp_Object area_id, plist;
23555
23556 area_id = XCAR (hotspot);
23557 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23558 If so, we could look for mouse-enter, mouse-leave
23559 properties in PLIST (and do something...). */
23560 hotspot = XCDR (hotspot);
23561 if (CONSP (hotspot)
23562 && (plist = XCAR (hotspot), CONSP (plist)))
23563 {
23564 pointer = Fplist_get (plist, Qpointer);
23565 if (NILP (pointer))
23566 pointer = Qhand;
23567 help_echo_string = Fplist_get (plist, Qhelp_echo);
23568 if (!NILP (help_echo_string))
23569 {
23570 help_echo_window = window;
23571 help_echo_object = glyph->object;
23572 help_echo_pos = glyph->charpos;
23573 }
23574 }
23575 }
23576 if (NILP (pointer))
23577 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23578 }
23579 }
23580
23581 /* Clear mouse face if X/Y not over text. */
23582 if (glyph == NULL
23583 || area != TEXT_AREA
23584 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23585 {
23586 if (clear_mouse_face (dpyinfo))
23587 cursor = No_Cursor;
23588 if (NILP (pointer))
23589 {
23590 if (area != TEXT_AREA)
23591 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23592 else
23593 pointer = Vvoid_text_area_pointer;
23594 }
23595 goto set_cursor;
23596 }
23597
23598 pos = glyph->charpos;
23599 object = glyph->object;
23600 if (!STRINGP (object) && !BUFFERP (object))
23601 goto set_cursor;
23602
23603 /* If we get an out-of-range value, return now; avoid an error. */
23604 if (BUFFERP (object) && pos > BUF_Z (b))
23605 goto set_cursor;
23606
23607 /* Make the window's buffer temporarily current for
23608 overlays_at and compute_char_face. */
23609 obuf = current_buffer;
23610 current_buffer = b;
23611 obegv = BEGV;
23612 ozv = ZV;
23613 BEGV = BEG;
23614 ZV = Z;
23615
23616 /* Is this char mouse-active or does it have help-echo? */
23617 position = make_number (pos);
23618
23619 if (BUFFERP (object))
23620 {
23621 /* Put all the overlays we want in a vector in overlay_vec. */
23622 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23623 /* Sort overlays into increasing priority order. */
23624 noverlays = sort_overlays (overlay_vec, noverlays, w);
23625 }
23626 else
23627 noverlays = 0;
23628
23629 same_region = (EQ (window, dpyinfo->mouse_face_window)
23630 && vpos >= dpyinfo->mouse_face_beg_row
23631 && vpos <= dpyinfo->mouse_face_end_row
23632 && (vpos > dpyinfo->mouse_face_beg_row
23633 || hpos >= dpyinfo->mouse_face_beg_col)
23634 && (vpos < dpyinfo->mouse_face_end_row
23635 || hpos < dpyinfo->mouse_face_end_col
23636 || dpyinfo->mouse_face_past_end));
23637
23638 if (same_region)
23639 cursor = No_Cursor;
23640
23641 /* Check mouse-face highlighting. */
23642 if (! same_region
23643 /* If there exists an overlay with mouse-face overlapping
23644 the one we are currently highlighting, we have to
23645 check if we enter the overlapping overlay, and then
23646 highlight only that. */
23647 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23648 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23649 {
23650 /* Find the highest priority overlay with a mouse-face. */
23651 overlay = Qnil;
23652 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23653 {
23654 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23655 if (!NILP (mouse_face))
23656 overlay = overlay_vec[i];
23657 }
23658
23659 /* If we're highlighting the same overlay as before, there's
23660 no need to do that again. */
23661 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23662 goto check_help_echo;
23663 dpyinfo->mouse_face_overlay = overlay;
23664
23665 /* Clear the display of the old active region, if any. */
23666 if (clear_mouse_face (dpyinfo))
23667 cursor = No_Cursor;
23668
23669 /* If no overlay applies, get a text property. */
23670 if (NILP (overlay))
23671 mouse_face = Fget_text_property (position, Qmouse_face, object);
23672
23673 /* Next, compute the bounds of the mouse highlighting and
23674 display it. */
23675 if (!NILP (mouse_face) && STRINGP (object))
23676 {
23677 /* The mouse-highlighting comes from a display string
23678 with a mouse-face. */
23679 Lisp_Object b, e;
23680 EMACS_INT ignore;
23681
23682 b = Fprevious_single_property_change
23683 (make_number (pos + 1), Qmouse_face, object, Qnil);
23684 e = Fnext_single_property_change
23685 (position, Qmouse_face, object, Qnil);
23686 if (NILP (b))
23687 b = make_number (0);
23688 if (NILP (e))
23689 e = make_number (SCHARS (object) - 1);
23690
23691 fast_find_string_pos (w, XINT (b), object,
23692 &dpyinfo->mouse_face_beg_col,
23693 &dpyinfo->mouse_face_beg_row,
23694 &dpyinfo->mouse_face_beg_x,
23695 &dpyinfo->mouse_face_beg_y, 0);
23696 fast_find_string_pos (w, XINT (e), object,
23697 &dpyinfo->mouse_face_end_col,
23698 &dpyinfo->mouse_face_end_row,
23699 &dpyinfo->mouse_face_end_x,
23700 &dpyinfo->mouse_face_end_y, 1);
23701 dpyinfo->mouse_face_past_end = 0;
23702 dpyinfo->mouse_face_window = window;
23703 dpyinfo->mouse_face_face_id
23704 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23705 glyph->face_id, 1);
23706 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23707 cursor = No_Cursor;
23708 }
23709 else
23710 {
23711 /* The mouse-highlighting, if any, comes from an overlay
23712 or text property in the buffer. */
23713 Lisp_Object buffer, display_string;
23714
23715 if (STRINGP (object))
23716 {
23717 /* If we are on a display string with no mouse-face,
23718 check if the text under it has one. */
23719 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23720 int start = MATRIX_ROW_START_CHARPOS (r);
23721 pos = string_buffer_position (w, object, start);
23722 if (pos > 0)
23723 {
23724 mouse_face = get_char_property_and_overlay
23725 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23726 buffer = w->buffer;
23727 display_string = object;
23728 }
23729 }
23730 else
23731 {
23732 buffer = object;
23733 display_string = Qnil;
23734 }
23735
23736 if (!NILP (mouse_face))
23737 {
23738 Lisp_Object before, after;
23739 Lisp_Object before_string, after_string;
23740
23741 if (NILP (overlay))
23742 {
23743 /* Handle the text property case. */
23744 before = Fprevious_single_property_change
23745 (make_number (pos + 1), Qmouse_face, buffer,
23746 Fmarker_position (w->start));
23747 after = Fnext_single_property_change
23748 (make_number (pos), Qmouse_face, buffer,
23749 make_number (BUF_Z (XBUFFER (buffer))
23750 - XFASTINT (w->window_end_pos)));
23751 before_string = after_string = Qnil;
23752 }
23753 else
23754 {
23755 /* Handle the overlay case. */
23756 before = Foverlay_start (overlay);
23757 after = Foverlay_end (overlay);
23758 before_string = Foverlay_get (overlay, Qbefore_string);
23759 after_string = Foverlay_get (overlay, Qafter_string);
23760
23761 if (!STRINGP (before_string)) before_string = Qnil;
23762 if (!STRINGP (after_string)) after_string = Qnil;
23763 }
23764
23765 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23766 XFASTINT (before),
23767 XFASTINT (after),
23768 before_string, after_string,
23769 display_string);
23770 cursor = No_Cursor;
23771 }
23772 }
23773 }
23774
23775 check_help_echo:
23776
23777 /* Look for a `help-echo' property. */
23778 if (NILP (help_echo_string)) {
23779 Lisp_Object help, overlay;
23780
23781 /* Check overlays first. */
23782 help = overlay = Qnil;
23783 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23784 {
23785 overlay = overlay_vec[i];
23786 help = Foverlay_get (overlay, Qhelp_echo);
23787 }
23788
23789 if (!NILP (help))
23790 {
23791 help_echo_string = help;
23792 help_echo_window = window;
23793 help_echo_object = overlay;
23794 help_echo_pos = pos;
23795 }
23796 else
23797 {
23798 Lisp_Object object = glyph->object;
23799 int charpos = glyph->charpos;
23800
23801 /* Try text properties. */
23802 if (STRINGP (object)
23803 && charpos >= 0
23804 && charpos < SCHARS (object))
23805 {
23806 help = Fget_text_property (make_number (charpos),
23807 Qhelp_echo, object);
23808 if (NILP (help))
23809 {
23810 /* If the string itself doesn't specify a help-echo,
23811 see if the buffer text ``under'' it does. */
23812 struct glyph_row *r
23813 = MATRIX_ROW (w->current_matrix, vpos);
23814 int start = MATRIX_ROW_START_CHARPOS (r);
23815 int pos = string_buffer_position (w, object, start);
23816 if (pos > 0)
23817 {
23818 help = Fget_char_property (make_number (pos),
23819 Qhelp_echo, w->buffer);
23820 if (!NILP (help))
23821 {
23822 charpos = pos;
23823 object = w->buffer;
23824 }
23825 }
23826 }
23827 }
23828 else if (BUFFERP (object)
23829 && charpos >= BEGV
23830 && charpos < ZV)
23831 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23832 object);
23833
23834 if (!NILP (help))
23835 {
23836 help_echo_string = help;
23837 help_echo_window = window;
23838 help_echo_object = object;
23839 help_echo_pos = charpos;
23840 }
23841 }
23842 }
23843
23844 /* Look for a `pointer' property. */
23845 if (NILP (pointer))
23846 {
23847 /* Check overlays first. */
23848 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23849 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23850
23851 if (NILP (pointer))
23852 {
23853 Lisp_Object object = glyph->object;
23854 int charpos = glyph->charpos;
23855
23856 /* Try text properties. */
23857 if (STRINGP (object)
23858 && charpos >= 0
23859 && charpos < SCHARS (object))
23860 {
23861 pointer = Fget_text_property (make_number (charpos),
23862 Qpointer, object);
23863 if (NILP (pointer))
23864 {
23865 /* If the string itself doesn't specify a pointer,
23866 see if the buffer text ``under'' it does. */
23867 struct glyph_row *r
23868 = MATRIX_ROW (w->current_matrix, vpos);
23869 int start = MATRIX_ROW_START_CHARPOS (r);
23870 int pos = string_buffer_position (w, object, start);
23871 if (pos > 0)
23872 pointer = Fget_char_property (make_number (pos),
23873 Qpointer, w->buffer);
23874 }
23875 }
23876 else if (BUFFERP (object)
23877 && charpos >= BEGV
23878 && charpos < ZV)
23879 pointer = Fget_text_property (make_number (charpos),
23880 Qpointer, object);
23881 }
23882 }
23883
23884 BEGV = obegv;
23885 ZV = ozv;
23886 current_buffer = obuf;
23887 }
23888
23889 set_cursor:
23890
23891 define_frame_cursor1 (f, cursor, pointer);
23892 }
23893
23894
23895 /* EXPORT for RIF:
23896 Clear any mouse-face on window W. This function is part of the
23897 redisplay interface, and is called from try_window_id and similar
23898 functions to ensure the mouse-highlight is off. */
23899
23900 void
23901 x_clear_window_mouse_face (w)
23902 struct window *w;
23903 {
23904 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23905 Lisp_Object window;
23906
23907 BLOCK_INPUT;
23908 XSETWINDOW (window, w);
23909 if (EQ (window, dpyinfo->mouse_face_window))
23910 clear_mouse_face (dpyinfo);
23911 UNBLOCK_INPUT;
23912 }
23913
23914
23915 /* EXPORT:
23916 Just discard the mouse face information for frame F, if any.
23917 This is used when the size of F is changed. */
23918
23919 void
23920 cancel_mouse_face (f)
23921 struct frame *f;
23922 {
23923 Lisp_Object window;
23924 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23925
23926 window = dpyinfo->mouse_face_window;
23927 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23928 {
23929 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23930 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23931 dpyinfo->mouse_face_window = Qnil;
23932 }
23933 }
23934
23935
23936 #endif /* HAVE_WINDOW_SYSTEM */
23937
23938 \f
23939 /***********************************************************************
23940 Exposure Events
23941 ***********************************************************************/
23942
23943 #ifdef HAVE_WINDOW_SYSTEM
23944
23945 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23946 which intersects rectangle R. R is in window-relative coordinates. */
23947
23948 static void
23949 expose_area (w, row, r, area)
23950 struct window *w;
23951 struct glyph_row *row;
23952 XRectangle *r;
23953 enum glyph_row_area area;
23954 {
23955 struct glyph *first = row->glyphs[area];
23956 struct glyph *end = row->glyphs[area] + row->used[area];
23957 struct glyph *last;
23958 int first_x, start_x, x;
23959
23960 if (area == TEXT_AREA && row->fill_line_p)
23961 /* If row extends face to end of line write the whole line. */
23962 draw_glyphs (w, 0, row, area,
23963 0, row->used[area],
23964 DRAW_NORMAL_TEXT, 0);
23965 else
23966 {
23967 /* Set START_X to the window-relative start position for drawing glyphs of
23968 AREA. The first glyph of the text area can be partially visible.
23969 The first glyphs of other areas cannot. */
23970 start_x = window_box_left_offset (w, area);
23971 x = start_x;
23972 if (area == TEXT_AREA)
23973 x += row->x;
23974
23975 /* Find the first glyph that must be redrawn. */
23976 while (first < end
23977 && x + first->pixel_width < r->x)
23978 {
23979 x += first->pixel_width;
23980 ++first;
23981 }
23982
23983 /* Find the last one. */
23984 last = first;
23985 first_x = x;
23986 while (last < end
23987 && x < r->x + r->width)
23988 {
23989 x += last->pixel_width;
23990 ++last;
23991 }
23992
23993 /* Repaint. */
23994 if (last > first)
23995 draw_glyphs (w, first_x - start_x, row, area,
23996 first - row->glyphs[area], last - row->glyphs[area],
23997 DRAW_NORMAL_TEXT, 0);
23998 }
23999 }
24000
24001
24002 /* Redraw the parts of the glyph row ROW on window W intersecting
24003 rectangle R. R is in window-relative coordinates. Value is
24004 non-zero if mouse-face was overwritten. */
24005
24006 static int
24007 expose_line (w, row, r)
24008 struct window *w;
24009 struct glyph_row *row;
24010 XRectangle *r;
24011 {
24012 xassert (row->enabled_p);
24013
24014 if (row->mode_line_p || w->pseudo_window_p)
24015 draw_glyphs (w, 0, row, TEXT_AREA,
24016 0, row->used[TEXT_AREA],
24017 DRAW_NORMAL_TEXT, 0);
24018 else
24019 {
24020 if (row->used[LEFT_MARGIN_AREA])
24021 expose_area (w, row, r, LEFT_MARGIN_AREA);
24022 if (row->used[TEXT_AREA])
24023 expose_area (w, row, r, TEXT_AREA);
24024 if (row->used[RIGHT_MARGIN_AREA])
24025 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24026 draw_row_fringe_bitmaps (w, row);
24027 }
24028
24029 return row->mouse_face_p;
24030 }
24031
24032
24033 /* Redraw those parts of glyphs rows during expose event handling that
24034 overlap other rows. Redrawing of an exposed line writes over parts
24035 of lines overlapping that exposed line; this function fixes that.
24036
24037 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24038 row in W's current matrix that is exposed and overlaps other rows.
24039 LAST_OVERLAPPING_ROW is the last such row. */
24040
24041 static void
24042 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24043 struct window *w;
24044 struct glyph_row *first_overlapping_row;
24045 struct glyph_row *last_overlapping_row;
24046 XRectangle *r;
24047 {
24048 struct glyph_row *row;
24049
24050 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24051 if (row->overlapping_p)
24052 {
24053 xassert (row->enabled_p && !row->mode_line_p);
24054
24055 row->clip = r;
24056 if (row->used[LEFT_MARGIN_AREA])
24057 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24058
24059 if (row->used[TEXT_AREA])
24060 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24061
24062 if (row->used[RIGHT_MARGIN_AREA])
24063 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24064 row->clip = NULL;
24065 }
24066 }
24067
24068
24069 /* Return non-zero if W's cursor intersects rectangle R. */
24070
24071 static int
24072 phys_cursor_in_rect_p (w, r)
24073 struct window *w;
24074 XRectangle *r;
24075 {
24076 XRectangle cr, result;
24077 struct glyph *cursor_glyph;
24078 struct glyph_row *row;
24079
24080 if (w->phys_cursor.vpos >= 0
24081 && w->phys_cursor.vpos < w->current_matrix->nrows
24082 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24083 row->enabled_p)
24084 && row->cursor_in_fringe_p)
24085 {
24086 /* Cursor is in the fringe. */
24087 cr.x = window_box_right_offset (w,
24088 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24089 ? RIGHT_MARGIN_AREA
24090 : TEXT_AREA));
24091 cr.y = row->y;
24092 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24093 cr.height = row->height;
24094 return x_intersect_rectangles (&cr, r, &result);
24095 }
24096
24097 cursor_glyph = get_phys_cursor_glyph (w);
24098 if (cursor_glyph)
24099 {
24100 /* r is relative to W's box, but w->phys_cursor.x is relative
24101 to left edge of W's TEXT area. Adjust it. */
24102 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24103 cr.y = w->phys_cursor.y;
24104 cr.width = cursor_glyph->pixel_width;
24105 cr.height = w->phys_cursor_height;
24106 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24107 I assume the effect is the same -- and this is portable. */
24108 return x_intersect_rectangles (&cr, r, &result);
24109 }
24110 /* If we don't understand the format, pretend we're not in the hot-spot. */
24111 return 0;
24112 }
24113
24114
24115 /* EXPORT:
24116 Draw a vertical window border to the right of window W if W doesn't
24117 have vertical scroll bars. */
24118
24119 void
24120 x_draw_vertical_border (w)
24121 struct window *w;
24122 {
24123 struct frame *f = XFRAME (WINDOW_FRAME (w));
24124
24125 /* We could do better, if we knew what type of scroll-bar the adjacent
24126 windows (on either side) have... But we don't :-(
24127 However, I think this works ok. ++KFS 2003-04-25 */
24128
24129 /* Redraw borders between horizontally adjacent windows. Don't
24130 do it for frames with vertical scroll bars because either the
24131 right scroll bar of a window, or the left scroll bar of its
24132 neighbor will suffice as a border. */
24133 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24134 return;
24135
24136 if (!WINDOW_RIGHTMOST_P (w)
24137 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24138 {
24139 int x0, x1, y0, y1;
24140
24141 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24142 y1 -= 1;
24143
24144 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24145 x1 -= 1;
24146
24147 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24148 }
24149 else if (!WINDOW_LEFTMOST_P (w)
24150 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24151 {
24152 int x0, x1, y0, y1;
24153
24154 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24155 y1 -= 1;
24156
24157 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24158 x0 -= 1;
24159
24160 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24161 }
24162 }
24163
24164
24165 /* Redraw the part of window W intersection rectangle FR. Pixel
24166 coordinates in FR are frame-relative. Call this function with
24167 input blocked. Value is non-zero if the exposure overwrites
24168 mouse-face. */
24169
24170 static int
24171 expose_window (w, fr)
24172 struct window *w;
24173 XRectangle *fr;
24174 {
24175 struct frame *f = XFRAME (w->frame);
24176 XRectangle wr, r;
24177 int mouse_face_overwritten_p = 0;
24178
24179 /* If window is not yet fully initialized, do nothing. This can
24180 happen when toolkit scroll bars are used and a window is split.
24181 Reconfiguring the scroll bar will generate an expose for a newly
24182 created window. */
24183 if (w->current_matrix == NULL)
24184 return 0;
24185
24186 /* When we're currently updating the window, display and current
24187 matrix usually don't agree. Arrange for a thorough display
24188 later. */
24189 if (w == updated_window)
24190 {
24191 SET_FRAME_GARBAGED (f);
24192 return 0;
24193 }
24194
24195 /* Frame-relative pixel rectangle of W. */
24196 wr.x = WINDOW_LEFT_EDGE_X (w);
24197 wr.y = WINDOW_TOP_EDGE_Y (w);
24198 wr.width = WINDOW_TOTAL_WIDTH (w);
24199 wr.height = WINDOW_TOTAL_HEIGHT (w);
24200
24201 if (x_intersect_rectangles (fr, &wr, &r))
24202 {
24203 int yb = window_text_bottom_y (w);
24204 struct glyph_row *row;
24205 int cursor_cleared_p;
24206 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24207
24208 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24209 r.x, r.y, r.width, r.height));
24210
24211 /* Convert to window coordinates. */
24212 r.x -= WINDOW_LEFT_EDGE_X (w);
24213 r.y -= WINDOW_TOP_EDGE_Y (w);
24214
24215 /* Turn off the cursor. */
24216 if (!w->pseudo_window_p
24217 && phys_cursor_in_rect_p (w, &r))
24218 {
24219 x_clear_cursor (w);
24220 cursor_cleared_p = 1;
24221 }
24222 else
24223 cursor_cleared_p = 0;
24224
24225 /* Update lines intersecting rectangle R. */
24226 first_overlapping_row = last_overlapping_row = NULL;
24227 for (row = w->current_matrix->rows;
24228 row->enabled_p;
24229 ++row)
24230 {
24231 int y0 = row->y;
24232 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24233
24234 if ((y0 >= r.y && y0 < r.y + r.height)
24235 || (y1 > r.y && y1 < r.y + r.height)
24236 || (r.y >= y0 && r.y < y1)
24237 || (r.y + r.height > y0 && r.y + r.height < y1))
24238 {
24239 /* A header line may be overlapping, but there is no need
24240 to fix overlapping areas for them. KFS 2005-02-12 */
24241 if (row->overlapping_p && !row->mode_line_p)
24242 {
24243 if (first_overlapping_row == NULL)
24244 first_overlapping_row = row;
24245 last_overlapping_row = row;
24246 }
24247
24248 row->clip = fr;
24249 if (expose_line (w, row, &r))
24250 mouse_face_overwritten_p = 1;
24251 row->clip = NULL;
24252 }
24253 else if (row->overlapping_p)
24254 {
24255 /* We must redraw a row overlapping the exposed area. */
24256 if (y0 < r.y
24257 ? y0 + row->phys_height > r.y
24258 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24259 {
24260 if (first_overlapping_row == NULL)
24261 first_overlapping_row = row;
24262 last_overlapping_row = row;
24263 }
24264 }
24265
24266 if (y1 >= yb)
24267 break;
24268 }
24269
24270 /* Display the mode line if there is one. */
24271 if (WINDOW_WANTS_MODELINE_P (w)
24272 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24273 row->enabled_p)
24274 && row->y < r.y + r.height)
24275 {
24276 if (expose_line (w, row, &r))
24277 mouse_face_overwritten_p = 1;
24278 }
24279
24280 if (!w->pseudo_window_p)
24281 {
24282 /* Fix the display of overlapping rows. */
24283 if (first_overlapping_row)
24284 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24285 fr);
24286
24287 /* Draw border between windows. */
24288 x_draw_vertical_border (w);
24289
24290 /* Turn the cursor on again. */
24291 if (cursor_cleared_p)
24292 update_window_cursor (w, 1);
24293 }
24294 }
24295
24296 return mouse_face_overwritten_p;
24297 }
24298
24299
24300
24301 /* Redraw (parts) of all windows in the window tree rooted at W that
24302 intersect R. R contains frame pixel coordinates. Value is
24303 non-zero if the exposure overwrites mouse-face. */
24304
24305 static int
24306 expose_window_tree (w, r)
24307 struct window *w;
24308 XRectangle *r;
24309 {
24310 struct frame *f = XFRAME (w->frame);
24311 int mouse_face_overwritten_p = 0;
24312
24313 while (w && !FRAME_GARBAGED_P (f))
24314 {
24315 if (!NILP (w->hchild))
24316 mouse_face_overwritten_p
24317 |= expose_window_tree (XWINDOW (w->hchild), r);
24318 else if (!NILP (w->vchild))
24319 mouse_face_overwritten_p
24320 |= expose_window_tree (XWINDOW (w->vchild), r);
24321 else
24322 mouse_face_overwritten_p |= expose_window (w, r);
24323
24324 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24325 }
24326
24327 return mouse_face_overwritten_p;
24328 }
24329
24330
24331 /* EXPORT:
24332 Redisplay an exposed area of frame F. X and Y are the upper-left
24333 corner of the exposed rectangle. W and H are width and height of
24334 the exposed area. All are pixel values. W or H zero means redraw
24335 the entire frame. */
24336
24337 void
24338 expose_frame (f, x, y, w, h)
24339 struct frame *f;
24340 int x, y, w, h;
24341 {
24342 XRectangle r;
24343 int mouse_face_overwritten_p = 0;
24344
24345 TRACE ((stderr, "expose_frame "));
24346
24347 /* No need to redraw if frame will be redrawn soon. */
24348 if (FRAME_GARBAGED_P (f))
24349 {
24350 TRACE ((stderr, " garbaged\n"));
24351 return;
24352 }
24353
24354 /* If basic faces haven't been realized yet, there is no point in
24355 trying to redraw anything. This can happen when we get an expose
24356 event while Emacs is starting, e.g. by moving another window. */
24357 if (FRAME_FACE_CACHE (f) == NULL
24358 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24359 {
24360 TRACE ((stderr, " no faces\n"));
24361 return;
24362 }
24363
24364 if (w == 0 || h == 0)
24365 {
24366 r.x = r.y = 0;
24367 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24368 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24369 }
24370 else
24371 {
24372 r.x = x;
24373 r.y = y;
24374 r.width = w;
24375 r.height = h;
24376 }
24377
24378 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24379 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24380
24381 if (WINDOWP (f->tool_bar_window))
24382 mouse_face_overwritten_p
24383 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24384
24385 #ifdef HAVE_X_WINDOWS
24386 #ifndef MSDOS
24387 #ifndef USE_X_TOOLKIT
24388 if (WINDOWP (f->menu_bar_window))
24389 mouse_face_overwritten_p
24390 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24391 #endif /* not USE_X_TOOLKIT */
24392 #endif
24393 #endif
24394
24395 /* Some window managers support a focus-follows-mouse style with
24396 delayed raising of frames. Imagine a partially obscured frame,
24397 and moving the mouse into partially obscured mouse-face on that
24398 frame. The visible part of the mouse-face will be highlighted,
24399 then the WM raises the obscured frame. With at least one WM, KDE
24400 2.1, Emacs is not getting any event for the raising of the frame
24401 (even tried with SubstructureRedirectMask), only Expose events.
24402 These expose events will draw text normally, i.e. not
24403 highlighted. Which means we must redo the highlight here.
24404 Subsume it under ``we love X''. --gerd 2001-08-15 */
24405 /* Included in Windows version because Windows most likely does not
24406 do the right thing if any third party tool offers
24407 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24408 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24409 {
24410 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24411 if (f == dpyinfo->mouse_face_mouse_frame)
24412 {
24413 int x = dpyinfo->mouse_face_mouse_x;
24414 int y = dpyinfo->mouse_face_mouse_y;
24415 clear_mouse_face (dpyinfo);
24416 note_mouse_highlight (f, x, y);
24417 }
24418 }
24419 }
24420
24421
24422 /* EXPORT:
24423 Determine the intersection of two rectangles R1 and R2. Return
24424 the intersection in *RESULT. Value is non-zero if RESULT is not
24425 empty. */
24426
24427 int
24428 x_intersect_rectangles (r1, r2, result)
24429 XRectangle *r1, *r2, *result;
24430 {
24431 XRectangle *left, *right;
24432 XRectangle *upper, *lower;
24433 int intersection_p = 0;
24434
24435 /* Rearrange so that R1 is the left-most rectangle. */
24436 if (r1->x < r2->x)
24437 left = r1, right = r2;
24438 else
24439 left = r2, right = r1;
24440
24441 /* X0 of the intersection is right.x0, if this is inside R1,
24442 otherwise there is no intersection. */
24443 if (right->x <= left->x + left->width)
24444 {
24445 result->x = right->x;
24446
24447 /* The right end of the intersection is the minimum of the
24448 the right ends of left and right. */
24449 result->width = (min (left->x + left->width, right->x + right->width)
24450 - result->x);
24451
24452 /* Same game for Y. */
24453 if (r1->y < r2->y)
24454 upper = r1, lower = r2;
24455 else
24456 upper = r2, lower = r1;
24457
24458 /* The upper end of the intersection is lower.y0, if this is inside
24459 of upper. Otherwise, there is no intersection. */
24460 if (lower->y <= upper->y + upper->height)
24461 {
24462 result->y = lower->y;
24463
24464 /* The lower end of the intersection is the minimum of the lower
24465 ends of upper and lower. */
24466 result->height = (min (lower->y + lower->height,
24467 upper->y + upper->height)
24468 - result->y);
24469 intersection_p = 1;
24470 }
24471 }
24472
24473 return intersection_p;
24474 }
24475
24476 #endif /* HAVE_WINDOW_SYSTEM */
24477
24478 \f
24479 /***********************************************************************
24480 Initialization
24481 ***********************************************************************/
24482
24483 void
24484 syms_of_xdisp ()
24485 {
24486 Vwith_echo_area_save_vector = Qnil;
24487 staticpro (&Vwith_echo_area_save_vector);
24488
24489 Vmessage_stack = Qnil;
24490 staticpro (&Vmessage_stack);
24491
24492 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
24493 staticpro (&Qinhibit_redisplay);
24494
24495 message_dolog_marker1 = Fmake_marker ();
24496 staticpro (&message_dolog_marker1);
24497 message_dolog_marker2 = Fmake_marker ();
24498 staticpro (&message_dolog_marker2);
24499 message_dolog_marker3 = Fmake_marker ();
24500 staticpro (&message_dolog_marker3);
24501
24502 #if GLYPH_DEBUG
24503 defsubr (&Sdump_frame_glyph_matrix);
24504 defsubr (&Sdump_glyph_matrix);
24505 defsubr (&Sdump_glyph_row);
24506 defsubr (&Sdump_tool_bar_row);
24507 defsubr (&Strace_redisplay);
24508 defsubr (&Strace_to_stderr);
24509 #endif
24510 #ifdef HAVE_WINDOW_SYSTEM
24511 defsubr (&Stool_bar_lines_needed);
24512 defsubr (&Slookup_image_map);
24513 #endif
24514 defsubr (&Sformat_mode_line);
24515 defsubr (&Sinvisible_p);
24516
24517 staticpro (&Qmenu_bar_update_hook);
24518 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
24519
24520 staticpro (&Qoverriding_terminal_local_map);
24521 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
24522
24523 staticpro (&Qoverriding_local_map);
24524 Qoverriding_local_map = intern_c_string ("overriding-local-map");
24525
24526 staticpro (&Qwindow_scroll_functions);
24527 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
24528
24529 staticpro (&Qwindow_text_change_functions);
24530 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
24531
24532 staticpro (&Qredisplay_end_trigger_functions);
24533 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
24534
24535 staticpro (&Qinhibit_point_motion_hooks);
24536 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
24537
24538 Qeval = intern_c_string ("eval");
24539 staticpro (&Qeval);
24540
24541 QCdata = intern_c_string (":data");
24542 staticpro (&QCdata);
24543 Qdisplay = intern_c_string ("display");
24544 staticpro (&Qdisplay);
24545 Qspace_width = intern_c_string ("space-width");
24546 staticpro (&Qspace_width);
24547 Qraise = intern_c_string ("raise");
24548 staticpro (&Qraise);
24549 Qslice = intern_c_string ("slice");
24550 staticpro (&Qslice);
24551 Qspace = intern_c_string ("space");
24552 staticpro (&Qspace);
24553 Qmargin = intern_c_string ("margin");
24554 staticpro (&Qmargin);
24555 Qpointer = intern_c_string ("pointer");
24556 staticpro (&Qpointer);
24557 Qleft_margin = intern_c_string ("left-margin");
24558 staticpro (&Qleft_margin);
24559 Qright_margin = intern_c_string ("right-margin");
24560 staticpro (&Qright_margin);
24561 Qcenter = intern_c_string ("center");
24562 staticpro (&Qcenter);
24563 Qline_height = intern_c_string ("line-height");
24564 staticpro (&Qline_height);
24565 QCalign_to = intern_c_string (":align-to");
24566 staticpro (&QCalign_to);
24567 QCrelative_width = intern_c_string (":relative-width");
24568 staticpro (&QCrelative_width);
24569 QCrelative_height = intern_c_string (":relative-height");
24570 staticpro (&QCrelative_height);
24571 QCeval = intern_c_string (":eval");
24572 staticpro (&QCeval);
24573 QCpropertize = intern_c_string (":propertize");
24574 staticpro (&QCpropertize);
24575 QCfile = intern_c_string (":file");
24576 staticpro (&QCfile);
24577 Qfontified = intern_c_string ("fontified");
24578 staticpro (&Qfontified);
24579 Qfontification_functions = intern_c_string ("fontification-functions");
24580 staticpro (&Qfontification_functions);
24581 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
24582 staticpro (&Qtrailing_whitespace);
24583 Qescape_glyph = intern_c_string ("escape-glyph");
24584 staticpro (&Qescape_glyph);
24585 Qnobreak_space = intern_c_string ("nobreak-space");
24586 staticpro (&Qnobreak_space);
24587 Qimage = intern_c_string ("image");
24588 staticpro (&Qimage);
24589 QCmap = intern_c_string (":map");
24590 staticpro (&QCmap);
24591 QCpointer = intern_c_string (":pointer");
24592 staticpro (&QCpointer);
24593 Qrect = intern_c_string ("rect");
24594 staticpro (&Qrect);
24595 Qcircle = intern_c_string ("circle");
24596 staticpro (&Qcircle);
24597 Qpoly = intern_c_string ("poly");
24598 staticpro (&Qpoly);
24599 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
24600 staticpro (&Qmessage_truncate_lines);
24601 Qgrow_only = intern_c_string ("grow-only");
24602 staticpro (&Qgrow_only);
24603 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
24604 staticpro (&Qinhibit_menubar_update);
24605 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
24606 staticpro (&Qinhibit_eval_during_redisplay);
24607 Qposition = intern_c_string ("position");
24608 staticpro (&Qposition);
24609 Qbuffer_position = intern_c_string ("buffer-position");
24610 staticpro (&Qbuffer_position);
24611 Qobject = intern_c_string ("object");
24612 staticpro (&Qobject);
24613 Qbar = intern_c_string ("bar");
24614 staticpro (&Qbar);
24615 Qhbar = intern_c_string ("hbar");
24616 staticpro (&Qhbar);
24617 Qbox = intern_c_string ("box");
24618 staticpro (&Qbox);
24619 Qhollow = intern_c_string ("hollow");
24620 staticpro (&Qhollow);
24621 Qhand = intern_c_string ("hand");
24622 staticpro (&Qhand);
24623 Qarrow = intern_c_string ("arrow");
24624 staticpro (&Qarrow);
24625 Qtext = intern_c_string ("text");
24626 staticpro (&Qtext);
24627 Qrisky_local_variable = intern_c_string ("risky-local-variable");
24628 staticpro (&Qrisky_local_variable);
24629 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
24630 staticpro (&Qinhibit_free_realized_faces);
24631
24632 list_of_error = Fcons (Fcons (intern_c_string ("error"),
24633 Fcons (intern_c_string ("void-variable"), Qnil)),
24634 Qnil);
24635 staticpro (&list_of_error);
24636
24637 Qlast_arrow_position = intern_c_string ("last-arrow-position");
24638 staticpro (&Qlast_arrow_position);
24639 Qlast_arrow_string = intern_c_string ("last-arrow-string");
24640 staticpro (&Qlast_arrow_string);
24641
24642 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
24643 staticpro (&Qoverlay_arrow_string);
24644 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
24645 staticpro (&Qoverlay_arrow_bitmap);
24646
24647 echo_buffer[0] = echo_buffer[1] = Qnil;
24648 staticpro (&echo_buffer[0]);
24649 staticpro (&echo_buffer[1]);
24650
24651 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24652 staticpro (&echo_area_buffer[0]);
24653 staticpro (&echo_area_buffer[1]);
24654
24655 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
24656 staticpro (&Vmessages_buffer_name);
24657
24658 mode_line_proptrans_alist = Qnil;
24659 staticpro (&mode_line_proptrans_alist);
24660 mode_line_string_list = Qnil;
24661 staticpro (&mode_line_string_list);
24662 mode_line_string_face = Qnil;
24663 staticpro (&mode_line_string_face);
24664 mode_line_string_face_prop = Qnil;
24665 staticpro (&mode_line_string_face_prop);
24666 Vmode_line_unwind_vector = Qnil;
24667 staticpro (&Vmode_line_unwind_vector);
24668
24669 help_echo_string = Qnil;
24670 staticpro (&help_echo_string);
24671 help_echo_object = Qnil;
24672 staticpro (&help_echo_object);
24673 help_echo_window = Qnil;
24674 staticpro (&help_echo_window);
24675 previous_help_echo_string = Qnil;
24676 staticpro (&previous_help_echo_string);
24677 help_echo_pos = -1;
24678
24679 #ifdef HAVE_WINDOW_SYSTEM
24680 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24681 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24682 For example, if a block cursor is over a tab, it will be drawn as
24683 wide as that tab on the display. */);
24684 x_stretch_cursor_p = 0;
24685 #endif
24686
24687 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24688 doc: /* *Non-nil means highlight trailing whitespace.
24689 The face used for trailing whitespace is `trailing-whitespace'. */);
24690 Vshow_trailing_whitespace = Qnil;
24691
24692 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24693 doc: /* *Control highlighting of nobreak space and soft hyphen.
24694 A value of t means highlight the character itself (for nobreak space,
24695 use face `nobreak-space').
24696 A value of nil means no highlighting.
24697 Other values mean display the escape glyph followed by an ordinary
24698 space or ordinary hyphen. */);
24699 Vnobreak_char_display = Qt;
24700
24701 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24702 doc: /* *The pointer shape to show in void text areas.
24703 A value of nil means to show the text pointer. Other options are `arrow',
24704 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24705 Vvoid_text_area_pointer = Qarrow;
24706
24707 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24708 doc: /* Non-nil means don't actually do any redisplay.
24709 This is used for internal purposes. */);
24710 Vinhibit_redisplay = Qnil;
24711
24712 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24713 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24714 Vglobal_mode_string = Qnil;
24715
24716 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24717 doc: /* Marker for where to display an arrow on top of the buffer text.
24718 This must be the beginning of a line in order to work.
24719 See also `overlay-arrow-string'. */);
24720 Voverlay_arrow_position = Qnil;
24721
24722 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24723 doc: /* String to display as an arrow in non-window frames.
24724 See also `overlay-arrow-position'. */);
24725 Voverlay_arrow_string = make_pure_c_string ("=>");
24726
24727 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24728 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24729 The symbols on this list are examined during redisplay to determine
24730 where to display overlay arrows. */);
24731 Voverlay_arrow_variable_list
24732 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
24733
24734 DEFVAR_INT ("scroll-step", &scroll_step,
24735 doc: /* *The number of lines to try scrolling a window by when point moves out.
24736 If that fails to bring point back on frame, point is centered instead.
24737 If this is zero, point is always centered after it moves off frame.
24738 If you want scrolling to always be a line at a time, you should set
24739 `scroll-conservatively' to a large value rather than set this to 1. */);
24740
24741 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24742 doc: /* *Scroll up to this many lines, to bring point back on screen.
24743 If point moves off-screen, redisplay will scroll by up to
24744 `scroll-conservatively' lines in order to bring point just barely
24745 onto the screen again. If that cannot be done, then redisplay
24746 recenters point as usual.
24747
24748 A value of zero means always recenter point if it moves off screen. */);
24749 scroll_conservatively = 0;
24750
24751 DEFVAR_INT ("scroll-margin", &scroll_margin,
24752 doc: /* *Number of lines of margin at the top and bottom of a window.
24753 Recenter the window whenever point gets within this many lines
24754 of the top or bottom of the window. */);
24755 scroll_margin = 0;
24756
24757 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24758 doc: /* Pixels per inch value for non-window system displays.
24759 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24760 Vdisplay_pixels_per_inch = make_float (72.0);
24761
24762 #if GLYPH_DEBUG
24763 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24764 #endif
24765
24766 DEFVAR_LISP ("truncate-partial-width-windows",
24767 &Vtruncate_partial_width_windows,
24768 doc: /* Non-nil means truncate lines in windows narrower than the frame.
24769 For an integer value, truncate lines in each window narrower than the
24770 full frame width, provided the window width is less than that integer;
24771 otherwise, respect the value of `truncate-lines'.
24772
24773 For any other non-nil value, truncate lines in all windows that do
24774 not span the full frame width.
24775
24776 A value of nil means to respect the value of `truncate-lines'.
24777
24778 If `word-wrap' is enabled, you might want to reduce this. */);
24779 Vtruncate_partial_width_windows = make_number (50);
24780
24781 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24782 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24783 Any other value means to use the appropriate face, `mode-line',
24784 `header-line', or `menu' respectively. */);
24785 mode_line_inverse_video = 1;
24786
24787 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24788 doc: /* *Maximum buffer size for which line number should be displayed.
24789 If the buffer is bigger than this, the line number does not appear
24790 in the mode line. A value of nil means no limit. */);
24791 Vline_number_display_limit = Qnil;
24792
24793 DEFVAR_INT ("line-number-display-limit-width",
24794 &line_number_display_limit_width,
24795 doc: /* *Maximum line width (in characters) for line number display.
24796 If the average length of the lines near point is bigger than this, then the
24797 line number may be omitted from the mode line. */);
24798 line_number_display_limit_width = 200;
24799
24800 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24801 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24802 highlight_nonselected_windows = 0;
24803
24804 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24805 doc: /* Non-nil if more than one frame is visible on this display.
24806 Minibuffer-only frames don't count, but iconified frames do.
24807 This variable is not guaranteed to be accurate except while processing
24808 `frame-title-format' and `icon-title-format'. */);
24809
24810 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24811 doc: /* Template for displaying the title bar of visible frames.
24812 \(Assuming the window manager supports this feature.)
24813
24814 This variable has the same structure as `mode-line-format', except that
24815 the %c and %l constructs are ignored. It is used only on frames for
24816 which no explicit name has been set \(see `modify-frame-parameters'). */);
24817
24818 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24819 doc: /* Template for displaying the title bar of an iconified frame.
24820 \(Assuming the window manager supports this feature.)
24821 This variable has the same structure as `mode-line-format' (which see),
24822 and is used only on frames for which no explicit name has been set
24823 \(see `modify-frame-parameters'). */);
24824 Vicon_title_format
24825 = Vframe_title_format
24826 = pure_cons (intern_c_string ("multiple-frames"),
24827 pure_cons (make_pure_c_string ("%b"),
24828 pure_cons (pure_cons (empty_unibyte_string,
24829 pure_cons (intern_c_string ("invocation-name"),
24830 pure_cons (make_pure_c_string ("@"),
24831 pure_cons (intern_c_string ("system-name"),
24832 Qnil)))),
24833 Qnil)));
24834
24835 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24836 doc: /* Maximum number of lines to keep in the message log buffer.
24837 If nil, disable message logging. If t, log messages but don't truncate
24838 the buffer when it becomes large. */);
24839 Vmessage_log_max = make_number (100);
24840
24841 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24842 doc: /* Functions called before redisplay, if window sizes have changed.
24843 The value should be a list of functions that take one argument.
24844 Just before redisplay, for each frame, if any of its windows have changed
24845 size since the last redisplay, or have been split or deleted,
24846 all the functions in the list are called, with the frame as argument. */);
24847 Vwindow_size_change_functions = Qnil;
24848
24849 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24850 doc: /* List of functions to call before redisplaying a window with scrolling.
24851 Each function is called with two arguments, the window and its new
24852 display-start position. Note that these functions are also called by
24853 `set-window-buffer'. Also note that the value of `window-end' is not
24854 valid when these functions are called. */);
24855 Vwindow_scroll_functions = Qnil;
24856
24857 DEFVAR_LISP ("window-text-change-functions",
24858 &Vwindow_text_change_functions,
24859 doc: /* Functions to call in redisplay when text in the window might change. */);
24860 Vwindow_text_change_functions = Qnil;
24861
24862 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24863 doc: /* Functions called when redisplay of a window reaches the end trigger.
24864 Each function is called with two arguments, the window and the end trigger value.
24865 See `set-window-redisplay-end-trigger'. */);
24866 Vredisplay_end_trigger_functions = Qnil;
24867
24868 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24869 doc: /* *Non-nil means autoselect window with mouse pointer.
24870 If nil, do not autoselect windows.
24871 A positive number means delay autoselection by that many seconds: a
24872 window is autoselected only after the mouse has remained in that
24873 window for the duration of the delay.
24874 A negative number has a similar effect, but causes windows to be
24875 autoselected only after the mouse has stopped moving. \(Because of
24876 the way Emacs compares mouse events, you will occasionally wait twice
24877 that time before the window gets selected.\)
24878 Any other value means to autoselect window instantaneously when the
24879 mouse pointer enters it.
24880
24881 Autoselection selects the minibuffer only if it is active, and never
24882 unselects the minibuffer if it is active.
24883
24884 When customizing this variable make sure that the actual value of
24885 `focus-follows-mouse' matches the behavior of your window manager. */);
24886 Vmouse_autoselect_window = Qnil;
24887
24888 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24889 doc: /* *Non-nil means automatically resize tool-bars.
24890 This dynamically changes the tool-bar's height to the minimum height
24891 that is needed to make all tool-bar items visible.
24892 If value is `grow-only', the tool-bar's height is only increased
24893 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24894 Vauto_resize_tool_bars = Qt;
24895
24896 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24897 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24898 auto_raise_tool_bar_buttons_p = 1;
24899
24900 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24901 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24902 make_cursor_line_fully_visible_p = 1;
24903
24904 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24905 doc: /* *Border below tool-bar in pixels.
24906 If an integer, use it as the height of the border.
24907 If it is one of `internal-border-width' or `border-width', use the
24908 value of the corresponding frame parameter.
24909 Otherwise, no border is added below the tool-bar. */);
24910 Vtool_bar_border = Qinternal_border_width;
24911
24912 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24913 doc: /* *Margin around tool-bar buttons in pixels.
24914 If an integer, use that for both horizontal and vertical margins.
24915 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24916 HORZ specifying the horizontal margin, and VERT specifying the
24917 vertical margin. */);
24918 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24919
24920 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24921 doc: /* *Relief thickness of tool-bar buttons. */);
24922 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24923
24924 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24925 doc: /* List of functions to call to fontify regions of text.
24926 Each function is called with one argument POS. Functions must
24927 fontify a region starting at POS in the current buffer, and give
24928 fontified regions the property `fontified'. */);
24929 Vfontification_functions = Qnil;
24930 Fmake_variable_buffer_local (Qfontification_functions);
24931
24932 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24933 &unibyte_display_via_language_environment,
24934 doc: /* *Non-nil means display unibyte text according to language environment.
24935 Specifically, this means that raw bytes in the range 160-255 decimal
24936 are displayed by converting them to the equivalent multibyte characters
24937 according to the current language environment. As a result, they are
24938 displayed according to the current fontset.
24939
24940 Note that this variable affects only how these bytes are displayed,
24941 but does not change the fact they are interpreted as raw bytes. */);
24942 unibyte_display_via_language_environment = 0;
24943
24944 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24945 doc: /* *Maximum height for resizing mini-windows.
24946 If a float, it specifies a fraction of the mini-window frame's height.
24947 If an integer, it specifies a number of lines. */);
24948 Vmax_mini_window_height = make_float (0.25);
24949
24950 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24951 doc: /* *How to resize mini-windows.
24952 A value of nil means don't automatically resize mini-windows.
24953 A value of t means resize them to fit the text displayed in them.
24954 A value of `grow-only', the default, means let mini-windows grow
24955 only, until their display becomes empty, at which point the windows
24956 go back to their normal size. */);
24957 Vresize_mini_windows = Qgrow_only;
24958
24959 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24960 doc: /* Alist specifying how to blink the cursor off.
24961 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24962 `cursor-type' frame-parameter or variable equals ON-STATE,
24963 comparing using `equal', Emacs uses OFF-STATE to specify
24964 how to blink it off. ON-STATE and OFF-STATE are values for
24965 the `cursor-type' frame parameter.
24966
24967 If a frame's ON-STATE has no entry in this list,
24968 the frame's other specifications determine how to blink the cursor off. */);
24969 Vblink_cursor_alist = Qnil;
24970
24971 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24972 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24973 automatic_hscrolling_p = 1;
24974 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
24975 staticpro (&Qauto_hscroll_mode);
24976
24977 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24978 doc: /* *How many columns away from the window edge point is allowed to get
24979 before automatic hscrolling will horizontally scroll the window. */);
24980 hscroll_margin = 5;
24981
24982 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24983 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24984 When point is less than `hscroll-margin' columns from the window
24985 edge, automatic hscrolling will scroll the window by the amount of columns
24986 determined by this variable. If its value is a positive integer, scroll that
24987 many columns. If it's a positive floating-point number, it specifies the
24988 fraction of the window's width to scroll. If it's nil or zero, point will be
24989 centered horizontally after the scroll. Any other value, including negative
24990 numbers, are treated as if the value were zero.
24991
24992 Automatic hscrolling always moves point outside the scroll margin, so if
24993 point was more than scroll step columns inside the margin, the window will
24994 scroll more than the value given by the scroll step.
24995
24996 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24997 and `scroll-right' overrides this variable's effect. */);
24998 Vhscroll_step = make_number (0);
24999
25000 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25001 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25002 Bind this around calls to `message' to let it take effect. */);
25003 message_truncate_lines = 0;
25004
25005 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25006 doc: /* Normal hook run to update the menu bar definitions.
25007 Redisplay runs this hook before it redisplays the menu bar.
25008 This is used to update submenus such as Buffers,
25009 whose contents depend on various data. */);
25010 Vmenu_bar_update_hook = Qnil;
25011
25012 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25013 doc: /* Frame for which we are updating a menu.
25014 The enable predicate for a menu binding should check this variable. */);
25015 Vmenu_updating_frame = Qnil;
25016
25017 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25018 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25019 inhibit_menubar_update = 0;
25020
25021 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25022 doc: /* Prefix prepended to all continuation lines at display time.
25023 The value may be a string, an image, or a stretch-glyph; it is
25024 interpreted in the same way as the value of a `display' text property.
25025
25026 This variable is overridden by any `wrap-prefix' text or overlay
25027 property.
25028
25029 To add a prefix to non-continuation lines, use `line-prefix'. */);
25030 Vwrap_prefix = Qnil;
25031 staticpro (&Qwrap_prefix);
25032 Qwrap_prefix = intern_c_string ("wrap-prefix");
25033 Fmake_variable_buffer_local (Qwrap_prefix);
25034
25035 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25036 doc: /* Prefix prepended to all non-continuation lines at display time.
25037 The value may be a string, an image, or a stretch-glyph; it is
25038 interpreted in the same way as the value of a `display' text property.
25039
25040 This variable is overridden by any `line-prefix' text or overlay
25041 property.
25042
25043 To add a prefix to continuation lines, use `wrap-prefix'. */);
25044 Vline_prefix = Qnil;
25045 staticpro (&Qline_prefix);
25046 Qline_prefix = intern_c_string ("line-prefix");
25047 Fmake_variable_buffer_local (Qline_prefix);
25048
25049 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25050 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25051 inhibit_eval_during_redisplay = 0;
25052
25053 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25054 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25055 inhibit_free_realized_faces = 0;
25056
25057 #if GLYPH_DEBUG
25058 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25059 doc: /* Inhibit try_window_id display optimization. */);
25060 inhibit_try_window_id = 0;
25061
25062 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25063 doc: /* Inhibit try_window_reusing display optimization. */);
25064 inhibit_try_window_reusing = 0;
25065
25066 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25067 doc: /* Inhibit try_cursor_movement display optimization. */);
25068 inhibit_try_cursor_movement = 0;
25069 #endif /* GLYPH_DEBUG */
25070
25071 DEFVAR_INT ("overline-margin", &overline_margin,
25072 doc: /* *Space between overline and text, in pixels.
25073 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25074 margin to the caracter height. */);
25075 overline_margin = 2;
25076
25077 DEFVAR_INT ("underline-minimum-offset",
25078 &underline_minimum_offset,
25079 doc: /* Minimum distance between baseline and underline.
25080 This can improve legibility of underlined text at small font sizes,
25081 particularly when using variable `x-use-underline-position-properties'
25082 with fonts that specify an UNDERLINE_POSITION relatively close to the
25083 baseline. The default value is 1. */);
25084 underline_minimum_offset = 1;
25085
25086 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25087 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25088 display_hourglass_p = 1;
25089
25090 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25091 doc: /* *Seconds to wait before displaying an hourglass pointer.
25092 Value must be an integer or float. */);
25093 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25094
25095 hourglass_atimer = NULL;
25096 hourglass_shown_p = 0;
25097 }
25098
25099
25100 /* Initialize this module when Emacs starts. */
25101
25102 void
25103 init_xdisp ()
25104 {
25105 Lisp_Object root_window;
25106 struct window *mini_w;
25107
25108 current_header_line_height = current_mode_line_height = -1;
25109
25110 CHARPOS (this_line_start_pos) = 0;
25111
25112 mini_w = XWINDOW (minibuf_window);
25113 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25114
25115 if (!noninteractive)
25116 {
25117 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25118 int i;
25119
25120 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25121 set_window_height (root_window,
25122 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25123 0);
25124 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25125 set_window_height (minibuf_window, 1, 0);
25126
25127 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25128 mini_w->total_cols = make_number (FRAME_COLS (f));
25129
25130 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25131 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25132 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25133
25134 /* The default ellipsis glyphs `...'. */
25135 for (i = 0; i < 3; ++i)
25136 default_invis_vector[i] = make_number ('.');
25137 }
25138
25139 {
25140 /* Allocate the buffer for frame titles.
25141 Also used for `format-mode-line'. */
25142 int size = 100;
25143 mode_line_noprop_buf = (char *) xmalloc (size);
25144 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25145 mode_line_noprop_ptr = mode_line_noprop_buf;
25146 mode_line_target = MODE_LINE_DISPLAY;
25147 }
25148
25149 help_echo_showing_p = 0;
25150 }
25151
25152 /* Since w32 does not support atimers, it defines its own implementation of
25153 the following three functions in w32fns.c. */
25154 #ifndef WINDOWSNT
25155
25156 /* Platform-independent portion of hourglass implementation. */
25157
25158 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25159 int
25160 hourglass_started ()
25161 {
25162 return hourglass_shown_p || hourglass_atimer != NULL;
25163 }
25164
25165 /* Cancel a currently active hourglass timer, and start a new one. */
25166 void
25167 start_hourglass ()
25168 {
25169 #if defined (HAVE_WINDOW_SYSTEM)
25170 EMACS_TIME delay;
25171 int secs, usecs = 0;
25172
25173 cancel_hourglass ();
25174
25175 if (INTEGERP (Vhourglass_delay)
25176 && XINT (Vhourglass_delay) > 0)
25177 secs = XFASTINT (Vhourglass_delay);
25178 else if (FLOATP (Vhourglass_delay)
25179 && XFLOAT_DATA (Vhourglass_delay) > 0)
25180 {
25181 Lisp_Object tem;
25182 tem = Ftruncate (Vhourglass_delay, Qnil);
25183 secs = XFASTINT (tem);
25184 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25185 }
25186 else
25187 secs = DEFAULT_HOURGLASS_DELAY;
25188
25189 EMACS_SET_SECS_USECS (delay, secs, usecs);
25190 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25191 show_hourglass, NULL);
25192 #endif
25193 }
25194
25195
25196 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25197 shown. */
25198 void
25199 cancel_hourglass ()
25200 {
25201 #if defined (HAVE_WINDOW_SYSTEM)
25202 if (hourglass_atimer)
25203 {
25204 cancel_atimer (hourglass_atimer);
25205 hourglass_atimer = NULL;
25206 }
25207
25208 if (hourglass_shown_p)
25209 hide_hourglass ();
25210 #endif
25211 }
25212 #endif /* ! WINDOWSNT */
25213
25214 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25215 (do not change this comment) */