]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Retrospective commit from 2009-12-26.
[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 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
37
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
47
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
61 |
62 expose_window (asynchronous) |
63 |
64 X expose events -----+
65
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
70
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
80
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
86
87
88 Direct operations.
89
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
94
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
101
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
107
108
109 Desired matrices.
110
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
117
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
124
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
130
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
137
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
145
146
147 Frame matrices.
148
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
155
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
167
168 #include <config.h>
169 #include <stdio.h>
170 #include <limits.h>
171 #include <setjmp.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "character.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "keymap.h"
185 #include "macros.h"
186 #include "disptab.h"
187 #include "termhooks.h"
188 #include "intervals.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.h"
192 #include "font.h"
193 #include "fontset.h"
194 #include "blockinput.h"
195
196 #ifdef HAVE_X_WINDOWS
197 #include "xterm.h"
198 #endif
199 #ifdef WINDOWSNT
200 #include "w32term.h"
201 #endif
202 #ifdef HAVE_NS
203 #include "nsterm.h"
204 #endif
205 #ifdef USE_GTK
206 #include "gtkutil.h"
207 #endif
208
209 #include "font.h"
210
211 #ifndef FRAME_X_OUTPUT
212 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
213 #endif
214
215 #define INFINITY 10000000
216
217 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
218 || defined(HAVE_NS) || defined (USE_GTK)
219 extern void set_frame_menubar P_ ((struct frame *f, int, int));
220 extern int pending_menu_activation;
221 #endif
222
223 extern int interrupt_input;
224 extern int command_loop_level;
225
226 extern Lisp_Object do_mouse_tracking;
227
228 extern int minibuffer_auto_raise;
229 extern Lisp_Object Vminibuffer_list;
230
231 extern Lisp_Object Qface;
232 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
233
234 extern Lisp_Object Voverriding_local_map;
235 extern Lisp_Object Voverriding_local_map_menu_flag;
236 extern Lisp_Object Qmenu_item;
237 extern Lisp_Object Qwhen;
238 extern Lisp_Object Qhelp_echo;
239 extern Lisp_Object Qbefore_string, Qafter_string;
240
241 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
242 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
243 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
244 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
245 Lisp_Object Qinhibit_point_motion_hooks;
246 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
247 Lisp_Object Qfontified;
248 Lisp_Object Qgrow_only;
249 Lisp_Object Qinhibit_eval_during_redisplay;
250 Lisp_Object Qbuffer_position, Qposition, Qobject;
251 Lisp_Object Qright_to_left, Qleft_to_right;
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, int *));
958 static void display_menu_bar P_ ((struct window *));
959 static int display_count_lines P_ ((int, int, int, int, int *));
960 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
961 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
962 static void compute_line_metrics P_ ((struct it *));
963 static void run_redisplay_end_trigger_hook P_ ((struct it *));
964 static int get_overlay_strings P_ ((struct it *, int));
965 static int get_overlay_strings_1 P_ ((struct it *, int, int));
966 static void next_overlay_string P_ ((struct it *));
967 static void reseat P_ ((struct it *, struct text_pos, int));
968 static void reseat_1 P_ ((struct it *, struct text_pos, int));
969 static void back_to_previous_visible_line_start P_ ((struct it *));
970 void reseat_at_previous_visible_line_start P_ ((struct it *));
971 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
972 static int next_element_from_ellipsis P_ ((struct it *));
973 static int next_element_from_display_vector P_ ((struct it *));
974 static int next_element_from_string P_ ((struct it *));
975 static int next_element_from_c_string P_ ((struct it *));
976 static int next_element_from_buffer P_ ((struct it *));
977 static int next_element_from_composition P_ ((struct it *));
978 static int next_element_from_image P_ ((struct it *));
979 static int next_element_from_stretch P_ ((struct it *));
980 static void load_overlay_strings P_ ((struct it *, int));
981 static int init_from_display_pos P_ ((struct it *, struct window *,
982 struct display_pos *));
983 static void reseat_to_string P_ ((struct it *, unsigned char *,
984 Lisp_Object, int, int, int, int));
985 static enum move_it_result
986 move_it_in_display_line_to (struct it *, EMACS_INT, int,
987 enum move_operation_enum);
988 void move_it_vertically_backward P_ ((struct it *, int));
989 static void init_to_row_start P_ ((struct it *, struct window *,
990 struct glyph_row *));
991 static int init_to_row_end P_ ((struct it *, struct window *,
992 struct glyph_row *));
993 static void back_to_previous_line_start P_ ((struct it *));
994 static int forward_to_next_line_start P_ ((struct it *, int *));
995 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
996 Lisp_Object, int));
997 static struct text_pos string_pos P_ ((int, Lisp_Object));
998 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
999 static int number_of_chars P_ ((unsigned char *, int));
1000 static void compute_stop_pos P_ ((struct it *));
1001 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1002 Lisp_Object));
1003 static int face_before_or_after_it_pos P_ ((struct it *, int));
1004 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1005 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1006 Lisp_Object, Lisp_Object,
1007 struct text_pos *, int));
1008 static int underlying_face_id P_ ((struct it *));
1009 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1010 struct window *));
1011
1012 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1013 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1014
1015 #ifdef HAVE_WINDOW_SYSTEM
1016
1017 static void update_tool_bar P_ ((struct frame *, int));
1018 static void build_desired_tool_bar_string P_ ((struct frame *f));
1019 static int redisplay_tool_bar P_ ((struct frame *));
1020 static void display_tool_bar_line P_ ((struct it *, int));
1021 static void notice_overwritten_cursor P_ ((struct window *,
1022 enum glyph_row_area,
1023 int, int, int, int));
1024
1025
1026
1027 #endif /* HAVE_WINDOW_SYSTEM */
1028
1029 \f
1030 /***********************************************************************
1031 Window display dimensions
1032 ***********************************************************************/
1033
1034 /* Return the bottom boundary y-position for text lines in window W.
1035 This is the first y position at which a line cannot start.
1036 It is relative to the top of the window.
1037
1038 This is the height of W minus the height of a mode line, if any. */
1039
1040 INLINE int
1041 window_text_bottom_y (w)
1042 struct window *w;
1043 {
1044 int height = WINDOW_TOTAL_HEIGHT (w);
1045
1046 if (WINDOW_WANTS_MODELINE_P (w))
1047 height -= CURRENT_MODE_LINE_HEIGHT (w);
1048 return height;
1049 }
1050
1051 /* Return the pixel width of display area AREA of window W. AREA < 0
1052 means return the total width of W, not including fringes to
1053 the left and right of the window. */
1054
1055 INLINE int
1056 window_box_width (w, area)
1057 struct window *w;
1058 int area;
1059 {
1060 int cols = XFASTINT (w->total_cols);
1061 int pixels = 0;
1062
1063 if (!w->pseudo_window_p)
1064 {
1065 cols -= WINDOW_SCROLL_BAR_COLS (w);
1066
1067 if (area == TEXT_AREA)
1068 {
1069 if (INTEGERP (w->left_margin_cols))
1070 cols -= XFASTINT (w->left_margin_cols);
1071 if (INTEGERP (w->right_margin_cols))
1072 cols -= XFASTINT (w->right_margin_cols);
1073 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1074 }
1075 else if (area == LEFT_MARGIN_AREA)
1076 {
1077 cols = (INTEGERP (w->left_margin_cols)
1078 ? XFASTINT (w->left_margin_cols) : 0);
1079 pixels = 0;
1080 }
1081 else if (area == RIGHT_MARGIN_AREA)
1082 {
1083 cols = (INTEGERP (w->right_margin_cols)
1084 ? XFASTINT (w->right_margin_cols) : 0);
1085 pixels = 0;
1086 }
1087 }
1088
1089 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1090 }
1091
1092
1093 /* Return the pixel height of the display area of window W, not
1094 including mode lines of W, if any. */
1095
1096 INLINE int
1097 window_box_height (w)
1098 struct window *w;
1099 {
1100 struct frame *f = XFRAME (w->frame);
1101 int height = WINDOW_TOTAL_HEIGHT (w);
1102
1103 xassert (height >= 0);
1104
1105 /* Note: the code below that determines the mode-line/header-line
1106 height is essentially the same as that contained in the macro
1107 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1108 the appropriate glyph row has its `mode_line_p' flag set,
1109 and if it doesn't, uses estimate_mode_line_height instead. */
1110
1111 if (WINDOW_WANTS_MODELINE_P (w))
1112 {
1113 struct glyph_row *ml_row
1114 = (w->current_matrix && w->current_matrix->rows
1115 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1116 : 0);
1117 if (ml_row && ml_row->mode_line_p)
1118 height -= ml_row->height;
1119 else
1120 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1121 }
1122
1123 if (WINDOW_WANTS_HEADER_LINE_P (w))
1124 {
1125 struct glyph_row *hl_row
1126 = (w->current_matrix && w->current_matrix->rows
1127 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1128 : 0);
1129 if (hl_row && hl_row->mode_line_p)
1130 height -= hl_row->height;
1131 else
1132 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1133 }
1134
1135 /* With a very small font and a mode-line that's taller than
1136 default, we might end up with a negative height. */
1137 return max (0, height);
1138 }
1139
1140 /* Return the window-relative coordinate of the left edge of display
1141 area AREA of window W. AREA < 0 means return the left edge of the
1142 whole window, to the right of the left fringe of W. */
1143
1144 INLINE int
1145 window_box_left_offset (w, area)
1146 struct window *w;
1147 int area;
1148 {
1149 int x;
1150
1151 if (w->pseudo_window_p)
1152 return 0;
1153
1154 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1155
1156 if (area == TEXT_AREA)
1157 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1158 + window_box_width (w, LEFT_MARGIN_AREA));
1159 else if (area == RIGHT_MARGIN_AREA)
1160 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1161 + window_box_width (w, LEFT_MARGIN_AREA)
1162 + window_box_width (w, TEXT_AREA)
1163 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1164 ? 0
1165 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1166 else if (area == LEFT_MARGIN_AREA
1167 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1168 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1169
1170 return x;
1171 }
1172
1173
1174 /* Return the window-relative coordinate of the right edge of display
1175 area AREA of window W. AREA < 0 means return the left edge of the
1176 whole window, to the left of the right fringe of W. */
1177
1178 INLINE int
1179 window_box_right_offset (w, area)
1180 struct window *w;
1181 int area;
1182 {
1183 return window_box_left_offset (w, area) + window_box_width (w, area);
1184 }
1185
1186 /* Return the frame-relative coordinate of the left edge of display
1187 area AREA of window W. AREA < 0 means return the left edge of the
1188 whole window, to the right of the left fringe of W. */
1189
1190 INLINE int
1191 window_box_left (w, area)
1192 struct window *w;
1193 int area;
1194 {
1195 struct frame *f = XFRAME (w->frame);
1196 int x;
1197
1198 if (w->pseudo_window_p)
1199 return FRAME_INTERNAL_BORDER_WIDTH (f);
1200
1201 x = (WINDOW_LEFT_EDGE_X (w)
1202 + window_box_left_offset (w, area));
1203
1204 return x;
1205 }
1206
1207
1208 /* Return the frame-relative coordinate of the right edge of display
1209 area AREA of window W. AREA < 0 means return the left edge of the
1210 whole window, to the left of the right fringe of W. */
1211
1212 INLINE int
1213 window_box_right (w, area)
1214 struct window *w;
1215 int area;
1216 {
1217 return window_box_left (w, area) + window_box_width (w, area);
1218 }
1219
1220 /* Get the bounding box of the display area AREA of window W, without
1221 mode lines, in frame-relative coordinates. AREA < 0 means the
1222 whole window, not including the left and right fringes of
1223 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1224 coordinates of the upper-left corner of the box. Return in
1225 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1226
1227 INLINE void
1228 window_box (w, area, box_x, box_y, box_width, box_height)
1229 struct window *w;
1230 int area;
1231 int *box_x, *box_y, *box_width, *box_height;
1232 {
1233 if (box_width)
1234 *box_width = window_box_width (w, area);
1235 if (box_height)
1236 *box_height = window_box_height (w);
1237 if (box_x)
1238 *box_x = window_box_left (w, area);
1239 if (box_y)
1240 {
1241 *box_y = WINDOW_TOP_EDGE_Y (w);
1242 if (WINDOW_WANTS_HEADER_LINE_P (w))
1243 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1244 }
1245 }
1246
1247
1248 /* Get the bounding box of the display area AREA of window W, without
1249 mode lines. AREA < 0 means the whole window, not including the
1250 left and right fringe of the window. Return in *TOP_LEFT_X
1251 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1252 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1253 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1254 box. */
1255
1256 INLINE void
1257 window_box_edges (w, area, top_left_x, top_left_y,
1258 bottom_right_x, bottom_right_y)
1259 struct window *w;
1260 int area;
1261 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1262 {
1263 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1264 bottom_right_y);
1265 *bottom_right_x += *top_left_x;
1266 *bottom_right_y += *top_left_y;
1267 }
1268
1269
1270 \f
1271 /***********************************************************************
1272 Utilities
1273 ***********************************************************************/
1274
1275 /* Return the bottom y-position of the line the iterator IT is in.
1276 This can modify IT's settings. */
1277
1278 int
1279 line_bottom_y (it)
1280 struct it *it;
1281 {
1282 int line_height = it->max_ascent + it->max_descent;
1283 int line_top_y = it->current_y;
1284
1285 if (line_height == 0)
1286 {
1287 if (last_height)
1288 line_height = last_height;
1289 else if (IT_CHARPOS (*it) < ZV)
1290 {
1291 move_it_by_lines (it, 1, 1);
1292 line_height = (it->max_ascent || it->max_descent
1293 ? it->max_ascent + it->max_descent
1294 : last_height);
1295 }
1296 else
1297 {
1298 struct glyph_row *row = it->glyph_row;
1299
1300 /* Use the default character height. */
1301 it->glyph_row = NULL;
1302 it->what = IT_CHARACTER;
1303 it->c = ' ';
1304 it->len = 1;
1305 PRODUCE_GLYPHS (it);
1306 line_height = it->ascent + it->descent;
1307 it->glyph_row = row;
1308 }
1309 }
1310
1311 return line_top_y + line_height;
1312 }
1313
1314
1315 /* Return 1 if position CHARPOS is visible in window W.
1316 CHARPOS < 0 means return info about WINDOW_END position.
1317 If visible, set *X and *Y to pixel coordinates of top left corner.
1318 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1319 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1320
1321 int
1322 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1323 struct window *w;
1324 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1325 {
1326 struct it it;
1327 struct text_pos top;
1328 int visible_p = 0;
1329 struct buffer *old_buffer = NULL;
1330
1331 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1332 return visible_p;
1333
1334 if (XBUFFER (w->buffer) != current_buffer)
1335 {
1336 old_buffer = current_buffer;
1337 set_buffer_internal_1 (XBUFFER (w->buffer));
1338 }
1339
1340 SET_TEXT_POS_FROM_MARKER (top, w->start);
1341
1342 /* Compute exact mode line heights. */
1343 if (WINDOW_WANTS_MODELINE_P (w))
1344 current_mode_line_height
1345 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1346 current_buffer->mode_line_format);
1347
1348 if (WINDOW_WANTS_HEADER_LINE_P (w))
1349 current_header_line_height
1350 = display_mode_line (w, HEADER_LINE_FACE_ID,
1351 current_buffer->header_line_format);
1352
1353 start_display (&it, w, top);
1354 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1355 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1356
1357 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1358 {
1359 /* We have reached CHARPOS, or passed it. How the call to
1360 move_it_to can overshoot: (i) If CHARPOS is on invisible
1361 text, move_it_to stops at the end of the invisible text,
1362 after CHARPOS. (ii) If CHARPOS is in a display vector,
1363 move_it_to stops on its last glyph. */
1364 int top_x = it.current_x;
1365 int top_y = it.current_y;
1366 enum it_method it_method = it.method;
1367 /* Calling line_bottom_y may change it.method. */
1368 int bottom_y = (last_height = 0, line_bottom_y (&it));
1369 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1370
1371 if (top_y < window_top_y)
1372 visible_p = bottom_y > window_top_y;
1373 else if (top_y < it.last_visible_y)
1374 visible_p = 1;
1375 if (visible_p)
1376 {
1377 if (it_method == GET_FROM_BUFFER)
1378 {
1379 Lisp_Object window, prop;
1380
1381 XSETWINDOW (window, w);
1382 prop = Fget_char_property (make_number (it.position.charpos),
1383 Qinvisible, window);
1384
1385 /* If charpos coincides with invisible text covered with an
1386 ellipsis, use the first glyph of the ellipsis to compute
1387 the pixel positions. */
1388 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1389 {
1390 struct glyph_row *row = it.glyph_row;
1391 struct glyph *glyph = row->glyphs[TEXT_AREA];
1392 struct glyph *end = glyph + row->used[TEXT_AREA];
1393 int x = row->x;
1394
1395 for (; glyph < end
1396 && (!BUFFERP (glyph->object)
1397 || glyph->charpos < charpos);
1398 glyph++)
1399 x += glyph->pixel_width;
1400 top_x = x;
1401 }
1402 }
1403 else if (it_method == GET_FROM_DISPLAY_VECTOR)
1404 {
1405 /* We stopped on the last glyph of a display vector.
1406 Try and recompute. Hack alert! */
1407 if (charpos < 2 || top.charpos >= charpos)
1408 top_x = it.glyph_row->x;
1409 else
1410 {
1411 struct it it2;
1412 start_display (&it2, w, top);
1413 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1414 get_next_display_element (&it2);
1415 PRODUCE_GLYPHS (&it2);
1416 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1417 || it2.current_x > it2.last_visible_x)
1418 top_x = it.glyph_row->x;
1419 else
1420 {
1421 top_x = it2.current_x;
1422 top_y = it2.current_y;
1423 }
1424 }
1425 }
1426
1427 *x = top_x;
1428 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1429 *rtop = max (0, window_top_y - top_y);
1430 *rbot = max (0, bottom_y - it.last_visible_y);
1431 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1432 - max (top_y, window_top_y)));
1433 *vpos = it.vpos;
1434 }
1435 }
1436 else
1437 {
1438 struct it it2;
1439
1440 it2 = it;
1441 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1442 move_it_by_lines (&it, 1, 0);
1443 if (charpos < IT_CHARPOS (it)
1444 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1445 {
1446 visible_p = 1;
1447 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1448 *x = it2.current_x;
1449 *y = it2.current_y + it2.max_ascent - it2.ascent;
1450 *rtop = max (0, -it2.current_y);
1451 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1452 - it.last_visible_y));
1453 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1454 it.last_visible_y)
1455 - max (it2.current_y,
1456 WINDOW_HEADER_LINE_HEIGHT (w))));
1457 *vpos = it2.vpos;
1458 }
1459 }
1460
1461 if (old_buffer)
1462 set_buffer_internal_1 (old_buffer);
1463
1464 current_header_line_height = current_mode_line_height = -1;
1465
1466 if (visible_p && XFASTINT (w->hscroll) > 0)
1467 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1468
1469 #if 0
1470 /* Debugging code. */
1471 if (visible_p)
1472 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1473 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1474 else
1475 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1476 #endif
1477
1478 return visible_p;
1479 }
1480
1481
1482 /* Return the next character from STR which is MAXLEN bytes long.
1483 Return in *LEN the length of the character. This is like
1484 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1485 we find one, we return a `?', but with the length of the invalid
1486 character. */
1487
1488 static INLINE int
1489 string_char_and_length (str, len)
1490 const unsigned char *str;
1491 int *len;
1492 {
1493 int c;
1494
1495 c = STRING_CHAR_AND_LENGTH (str, *len);
1496 if (!CHAR_VALID_P (c, 1))
1497 /* We may not change the length here because other places in Emacs
1498 don't use this function, i.e. they silently accept invalid
1499 characters. */
1500 c = '?';
1501
1502 return c;
1503 }
1504
1505
1506
1507 /* Given a position POS containing a valid character and byte position
1508 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1509
1510 static struct text_pos
1511 string_pos_nchars_ahead (pos, string, nchars)
1512 struct text_pos pos;
1513 Lisp_Object string;
1514 int nchars;
1515 {
1516 xassert (STRINGP (string) && nchars >= 0);
1517
1518 if (STRING_MULTIBYTE (string))
1519 {
1520 int rest = SBYTES (string) - BYTEPOS (pos);
1521 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1522 int len;
1523
1524 while (nchars--)
1525 {
1526 string_char_and_length (p, &len);
1527 p += len, rest -= len;
1528 xassert (rest >= 0);
1529 CHARPOS (pos) += 1;
1530 BYTEPOS (pos) += len;
1531 }
1532 }
1533 else
1534 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1535
1536 return pos;
1537 }
1538
1539
1540 /* Value is the text position, i.e. character and byte position,
1541 for character position CHARPOS in STRING. */
1542
1543 static INLINE struct text_pos
1544 string_pos (charpos, string)
1545 int charpos;
1546 Lisp_Object string;
1547 {
1548 struct text_pos pos;
1549 xassert (STRINGP (string));
1550 xassert (charpos >= 0);
1551 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1552 return pos;
1553 }
1554
1555
1556 /* Value is a text position, i.e. character and byte position, for
1557 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1558 means recognize multibyte characters. */
1559
1560 static struct text_pos
1561 c_string_pos (charpos, s, multibyte_p)
1562 int charpos;
1563 unsigned char *s;
1564 int multibyte_p;
1565 {
1566 struct text_pos pos;
1567
1568 xassert (s != NULL);
1569 xassert (charpos >= 0);
1570
1571 if (multibyte_p)
1572 {
1573 int rest = strlen (s), len;
1574
1575 SET_TEXT_POS (pos, 0, 0);
1576 while (charpos--)
1577 {
1578 string_char_and_length (s, &len);
1579 s += len, rest -= len;
1580 xassert (rest >= 0);
1581 CHARPOS (pos) += 1;
1582 BYTEPOS (pos) += len;
1583 }
1584 }
1585 else
1586 SET_TEXT_POS (pos, charpos, charpos);
1587
1588 return pos;
1589 }
1590
1591
1592 /* Value is the number of characters in C string S. MULTIBYTE_P
1593 non-zero means recognize multibyte characters. */
1594
1595 static int
1596 number_of_chars (s, multibyte_p)
1597 unsigned char *s;
1598 int multibyte_p;
1599 {
1600 int nchars;
1601
1602 if (multibyte_p)
1603 {
1604 int rest = strlen (s), len;
1605 unsigned char *p = (unsigned char *) s;
1606
1607 for (nchars = 0; rest > 0; ++nchars)
1608 {
1609 string_char_and_length (p, &len);
1610 rest -= len, p += len;
1611 }
1612 }
1613 else
1614 nchars = strlen (s);
1615
1616 return nchars;
1617 }
1618
1619
1620 /* Compute byte position NEWPOS->bytepos corresponding to
1621 NEWPOS->charpos. POS is a known position in string STRING.
1622 NEWPOS->charpos must be >= POS.charpos. */
1623
1624 static void
1625 compute_string_pos (newpos, pos, string)
1626 struct text_pos *newpos, pos;
1627 Lisp_Object string;
1628 {
1629 xassert (STRINGP (string));
1630 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1631
1632 if (STRING_MULTIBYTE (string))
1633 *newpos = string_pos_nchars_ahead (pos, string,
1634 CHARPOS (*newpos) - CHARPOS (pos));
1635 else
1636 BYTEPOS (*newpos) = CHARPOS (*newpos);
1637 }
1638
1639 /* EXPORT:
1640 Return an estimation of the pixel height of mode or header lines on
1641 frame F. FACE_ID specifies what line's height to estimate. */
1642
1643 int
1644 estimate_mode_line_height (f, face_id)
1645 struct frame *f;
1646 enum face_id face_id;
1647 {
1648 #ifdef HAVE_WINDOW_SYSTEM
1649 if (FRAME_WINDOW_P (f))
1650 {
1651 int height = FONT_HEIGHT (FRAME_FONT (f));
1652
1653 /* This function is called so early when Emacs starts that the face
1654 cache and mode line face are not yet initialized. */
1655 if (FRAME_FACE_CACHE (f))
1656 {
1657 struct face *face = FACE_FROM_ID (f, face_id);
1658 if (face)
1659 {
1660 if (face->font)
1661 height = FONT_HEIGHT (face->font);
1662 if (face->box_line_width > 0)
1663 height += 2 * face->box_line_width;
1664 }
1665 }
1666
1667 return height;
1668 }
1669 #endif
1670
1671 return 1;
1672 }
1673
1674 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1675 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1676 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1677 not force the value into range. */
1678
1679 void
1680 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1681 FRAME_PTR f;
1682 register int pix_x, pix_y;
1683 int *x, *y;
1684 NativeRectangle *bounds;
1685 int noclip;
1686 {
1687
1688 #ifdef HAVE_WINDOW_SYSTEM
1689 if (FRAME_WINDOW_P (f))
1690 {
1691 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1692 even for negative values. */
1693 if (pix_x < 0)
1694 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1695 if (pix_y < 0)
1696 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1697
1698 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1699 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1700
1701 if (bounds)
1702 STORE_NATIVE_RECT (*bounds,
1703 FRAME_COL_TO_PIXEL_X (f, pix_x),
1704 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1705 FRAME_COLUMN_WIDTH (f) - 1,
1706 FRAME_LINE_HEIGHT (f) - 1);
1707
1708 if (!noclip)
1709 {
1710 if (pix_x < 0)
1711 pix_x = 0;
1712 else if (pix_x > FRAME_TOTAL_COLS (f))
1713 pix_x = FRAME_TOTAL_COLS (f);
1714
1715 if (pix_y < 0)
1716 pix_y = 0;
1717 else if (pix_y > FRAME_LINES (f))
1718 pix_y = FRAME_LINES (f);
1719 }
1720 }
1721 #endif
1722
1723 *x = pix_x;
1724 *y = pix_y;
1725 }
1726
1727
1728 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1729 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1730 can't tell the positions because W's display is not up to date,
1731 return 0. */
1732
1733 int
1734 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1735 struct window *w;
1736 int hpos, vpos;
1737 int *frame_x, *frame_y;
1738 {
1739 #ifdef HAVE_WINDOW_SYSTEM
1740 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1741 {
1742 int success_p;
1743
1744 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1745 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1746
1747 if (display_completed)
1748 {
1749 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1750 struct glyph *glyph = row->glyphs[TEXT_AREA];
1751 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1752
1753 hpos = row->x;
1754 vpos = row->y;
1755 while (glyph < end)
1756 {
1757 hpos += glyph->pixel_width;
1758 ++glyph;
1759 }
1760
1761 /* If first glyph is partially visible, its first visible position is still 0. */
1762 if (hpos < 0)
1763 hpos = 0;
1764
1765 success_p = 1;
1766 }
1767 else
1768 {
1769 hpos = vpos = 0;
1770 success_p = 0;
1771 }
1772
1773 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1774 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1775 return success_p;
1776 }
1777 #endif
1778
1779 *frame_x = hpos;
1780 *frame_y = vpos;
1781 return 1;
1782 }
1783
1784
1785 #ifdef HAVE_WINDOW_SYSTEM
1786
1787 /* Find the glyph under window-relative coordinates X/Y in window W.
1788 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1789 strings. Return in *HPOS and *VPOS the row and column number of
1790 the glyph found. Return in *AREA the glyph area containing X.
1791 Value is a pointer to the glyph found or null if X/Y is not on
1792 text, or we can't tell because W's current matrix is not up to
1793 date. */
1794
1795 static
1796 struct glyph *
1797 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1798 struct window *w;
1799 int x, y;
1800 int *hpos, *vpos, *dx, *dy, *area;
1801 {
1802 struct glyph *glyph, *end;
1803 struct glyph_row *row = NULL;
1804 int x0, i;
1805
1806 /* Find row containing Y. Give up if some row is not enabled. */
1807 for (i = 0; i < w->current_matrix->nrows; ++i)
1808 {
1809 row = MATRIX_ROW (w->current_matrix, i);
1810 if (!row->enabled_p)
1811 return NULL;
1812 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1813 break;
1814 }
1815
1816 *vpos = i;
1817 *hpos = 0;
1818
1819 /* Give up if Y is not in the window. */
1820 if (i == w->current_matrix->nrows)
1821 return NULL;
1822
1823 /* Get the glyph area containing X. */
1824 if (w->pseudo_window_p)
1825 {
1826 *area = TEXT_AREA;
1827 x0 = 0;
1828 }
1829 else
1830 {
1831 if (x < window_box_left_offset (w, TEXT_AREA))
1832 {
1833 *area = LEFT_MARGIN_AREA;
1834 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1835 }
1836 else if (x < window_box_right_offset (w, TEXT_AREA))
1837 {
1838 *area = TEXT_AREA;
1839 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1840 }
1841 else
1842 {
1843 *area = RIGHT_MARGIN_AREA;
1844 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1845 }
1846 }
1847
1848 /* Find glyph containing X. */
1849 glyph = row->glyphs[*area];
1850 end = glyph + row->used[*area];
1851 x -= x0;
1852 while (glyph < end && x >= glyph->pixel_width)
1853 {
1854 x -= glyph->pixel_width;
1855 ++glyph;
1856 }
1857
1858 if (glyph == end)
1859 return NULL;
1860
1861 if (dx)
1862 {
1863 *dx = x;
1864 *dy = y - (row->y + row->ascent - glyph->ascent);
1865 }
1866
1867 *hpos = glyph - row->glyphs[*area];
1868 return glyph;
1869 }
1870
1871
1872 /* EXPORT:
1873 Convert frame-relative x/y to coordinates relative to window W.
1874 Takes pseudo-windows into account. */
1875
1876 void
1877 frame_to_window_pixel_xy (w, x, y)
1878 struct window *w;
1879 int *x, *y;
1880 {
1881 if (w->pseudo_window_p)
1882 {
1883 /* A pseudo-window is always full-width, and starts at the
1884 left edge of the frame, plus a frame border. */
1885 struct frame *f = XFRAME (w->frame);
1886 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1887 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1888 }
1889 else
1890 {
1891 *x -= WINDOW_LEFT_EDGE_X (w);
1892 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1893 }
1894 }
1895
1896 /* EXPORT:
1897 Return in RECTS[] at most N clipping rectangles for glyph string S.
1898 Return the number of stored rectangles. */
1899
1900 int
1901 get_glyph_string_clip_rects (s, rects, n)
1902 struct glyph_string *s;
1903 NativeRectangle *rects;
1904 int n;
1905 {
1906 XRectangle r;
1907
1908 if (n <= 0)
1909 return 0;
1910
1911 if (s->row->full_width_p)
1912 {
1913 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1914 r.x = WINDOW_LEFT_EDGE_X (s->w);
1915 r.width = WINDOW_TOTAL_WIDTH (s->w);
1916
1917 /* Unless displaying a mode or menu bar line, which are always
1918 fully visible, clip to the visible part of the row. */
1919 if (s->w->pseudo_window_p)
1920 r.height = s->row->visible_height;
1921 else
1922 r.height = s->height;
1923 }
1924 else
1925 {
1926 /* This is a text line that may be partially visible. */
1927 r.x = window_box_left (s->w, s->area);
1928 r.width = window_box_width (s->w, s->area);
1929 r.height = s->row->visible_height;
1930 }
1931
1932 if (s->clip_head)
1933 if (r.x < s->clip_head->x)
1934 {
1935 if (r.width >= s->clip_head->x - r.x)
1936 r.width -= s->clip_head->x - r.x;
1937 else
1938 r.width = 0;
1939 r.x = s->clip_head->x;
1940 }
1941 if (s->clip_tail)
1942 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1943 {
1944 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1945 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1946 else
1947 r.width = 0;
1948 }
1949
1950 /* If S draws overlapping rows, it's sufficient to use the top and
1951 bottom of the window for clipping because this glyph string
1952 intentionally draws over other lines. */
1953 if (s->for_overlaps)
1954 {
1955 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1956 r.height = window_text_bottom_y (s->w) - r.y;
1957
1958 /* Alas, the above simple strategy does not work for the
1959 environments with anti-aliased text: if the same text is
1960 drawn onto the same place multiple times, it gets thicker.
1961 If the overlap we are processing is for the erased cursor, we
1962 take the intersection with the rectagle of the cursor. */
1963 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1964 {
1965 XRectangle rc, r_save = r;
1966
1967 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1968 rc.y = s->w->phys_cursor.y;
1969 rc.width = s->w->phys_cursor_width;
1970 rc.height = s->w->phys_cursor_height;
1971
1972 x_intersect_rectangles (&r_save, &rc, &r);
1973 }
1974 }
1975 else
1976 {
1977 /* Don't use S->y for clipping because it doesn't take partially
1978 visible lines into account. For example, it can be negative for
1979 partially visible lines at the top of a window. */
1980 if (!s->row->full_width_p
1981 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1982 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1983 else
1984 r.y = max (0, s->row->y);
1985
1986 /* If drawing a tool-bar window, draw it over the internal border
1987 at the top of the window. */
1988 if (WINDOWP (s->f->tool_bar_window)
1989 && s->w == XWINDOW (s->f->tool_bar_window))
1990 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1991 }
1992
1993 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1994
1995 /* If drawing the cursor, don't let glyph draw outside its
1996 advertised boundaries. Cleartype does this under some circumstances. */
1997 if (s->hl == DRAW_CURSOR)
1998 {
1999 struct glyph *glyph = s->first_glyph;
2000 int height, max_y;
2001
2002 if (s->x > r.x)
2003 {
2004 r.width -= s->x - r.x;
2005 r.x = s->x;
2006 }
2007 r.width = min (r.width, glyph->pixel_width);
2008
2009 /* If r.y is below window bottom, ensure that we still see a cursor. */
2010 height = min (glyph->ascent + glyph->descent,
2011 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2012 max_y = window_text_bottom_y (s->w) - height;
2013 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2014 if (s->ybase - glyph->ascent > max_y)
2015 {
2016 r.y = max_y;
2017 r.height = height;
2018 }
2019 else
2020 {
2021 /* Don't draw cursor glyph taller than our actual glyph. */
2022 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2023 if (height < r.height)
2024 {
2025 max_y = r.y + r.height;
2026 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2027 r.height = min (max_y - r.y, height);
2028 }
2029 }
2030 }
2031
2032 if (s->row->clip)
2033 {
2034 XRectangle r_save = r;
2035
2036 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2037 r.width = 0;
2038 }
2039
2040 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2041 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2042 {
2043 #ifdef CONVERT_FROM_XRECT
2044 CONVERT_FROM_XRECT (r, *rects);
2045 #else
2046 *rects = r;
2047 #endif
2048 return 1;
2049 }
2050 else
2051 {
2052 /* If we are processing overlapping and allowed to return
2053 multiple clipping rectangles, we exclude the row of the glyph
2054 string from the clipping rectangle. This is to avoid drawing
2055 the same text on the environment with anti-aliasing. */
2056 #ifdef CONVERT_FROM_XRECT
2057 XRectangle rs[2];
2058 #else
2059 XRectangle *rs = rects;
2060 #endif
2061 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2062
2063 if (s->for_overlaps & OVERLAPS_PRED)
2064 {
2065 rs[i] = r;
2066 if (r.y + r.height > row_y)
2067 {
2068 if (r.y < row_y)
2069 rs[i].height = row_y - r.y;
2070 else
2071 rs[i].height = 0;
2072 }
2073 i++;
2074 }
2075 if (s->for_overlaps & OVERLAPS_SUCC)
2076 {
2077 rs[i] = r;
2078 if (r.y < row_y + s->row->visible_height)
2079 {
2080 if (r.y + r.height > row_y + s->row->visible_height)
2081 {
2082 rs[i].y = row_y + s->row->visible_height;
2083 rs[i].height = r.y + r.height - rs[i].y;
2084 }
2085 else
2086 rs[i].height = 0;
2087 }
2088 i++;
2089 }
2090
2091 n = i;
2092 #ifdef CONVERT_FROM_XRECT
2093 for (i = 0; i < n; i++)
2094 CONVERT_FROM_XRECT (rs[i], rects[i]);
2095 #endif
2096 return n;
2097 }
2098 }
2099
2100 /* EXPORT:
2101 Return in *NR the clipping rectangle for glyph string S. */
2102
2103 void
2104 get_glyph_string_clip_rect (s, nr)
2105 struct glyph_string *s;
2106 NativeRectangle *nr;
2107 {
2108 get_glyph_string_clip_rects (s, nr, 1);
2109 }
2110
2111
2112 /* EXPORT:
2113 Return the position and height of the phys cursor in window W.
2114 Set w->phys_cursor_width to width of phys cursor.
2115 */
2116
2117 void
2118 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2119 struct window *w;
2120 struct glyph_row *row;
2121 struct glyph *glyph;
2122 int *xp, *yp, *heightp;
2123 {
2124 struct frame *f = XFRAME (WINDOW_FRAME (w));
2125 int x, y, wd, h, h0, y0;
2126
2127 /* Compute the width of the rectangle to draw. If on a stretch
2128 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2129 rectangle as wide as the glyph, but use a canonical character
2130 width instead. */
2131 wd = glyph->pixel_width - 1;
2132 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2133 wd++; /* Why? */
2134 #endif
2135
2136 x = w->phys_cursor.x;
2137 if (x < 0)
2138 {
2139 wd += x;
2140 x = 0;
2141 }
2142
2143 if (glyph->type == STRETCH_GLYPH
2144 && !x_stretch_cursor_p)
2145 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2146 w->phys_cursor_width = wd;
2147
2148 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2149
2150 /* If y is below window bottom, ensure that we still see a cursor. */
2151 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2152
2153 h = max (h0, glyph->ascent + glyph->descent);
2154 h0 = min (h0, glyph->ascent + glyph->descent);
2155
2156 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2157 if (y < y0)
2158 {
2159 h = max (h - (y0 - y) + 1, h0);
2160 y = y0 - 1;
2161 }
2162 else
2163 {
2164 y0 = window_text_bottom_y (w) - h0;
2165 if (y > y0)
2166 {
2167 h += y - y0;
2168 y = y0;
2169 }
2170 }
2171
2172 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2173 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2174 *heightp = h;
2175 }
2176
2177 /*
2178 * Remember which glyph the mouse is over.
2179 */
2180
2181 void
2182 remember_mouse_glyph (f, gx, gy, rect)
2183 struct frame *f;
2184 int gx, gy;
2185 NativeRectangle *rect;
2186 {
2187 Lisp_Object window;
2188 struct window *w;
2189 struct glyph_row *r, *gr, *end_row;
2190 enum window_part part;
2191 enum glyph_row_area area;
2192 int x, y, width, height;
2193
2194 /* Try to determine frame pixel position and size of the glyph under
2195 frame pixel coordinates X/Y on frame F. */
2196
2197 if (!f->glyphs_initialized_p
2198 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2199 NILP (window)))
2200 {
2201 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2202 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2203 goto virtual_glyph;
2204 }
2205
2206 w = XWINDOW (window);
2207 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2208 height = WINDOW_FRAME_LINE_HEIGHT (w);
2209
2210 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2211 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2212
2213 if (w->pseudo_window_p)
2214 {
2215 area = TEXT_AREA;
2216 part = ON_MODE_LINE; /* Don't adjust margin. */
2217 goto text_glyph;
2218 }
2219
2220 switch (part)
2221 {
2222 case ON_LEFT_MARGIN:
2223 area = LEFT_MARGIN_AREA;
2224 goto text_glyph;
2225
2226 case ON_RIGHT_MARGIN:
2227 area = RIGHT_MARGIN_AREA;
2228 goto text_glyph;
2229
2230 case ON_HEADER_LINE:
2231 case ON_MODE_LINE:
2232 gr = (part == ON_HEADER_LINE
2233 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2234 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2235 gy = gr->y;
2236 area = TEXT_AREA;
2237 goto text_glyph_row_found;
2238
2239 case ON_TEXT:
2240 area = TEXT_AREA;
2241
2242 text_glyph:
2243 gr = 0; gy = 0;
2244 for (; r <= end_row && r->enabled_p; ++r)
2245 if (r->y + r->height > y)
2246 {
2247 gr = r; gy = r->y;
2248 break;
2249 }
2250
2251 text_glyph_row_found:
2252 if (gr && gy <= y)
2253 {
2254 struct glyph *g = gr->glyphs[area];
2255 struct glyph *end = g + gr->used[area];
2256
2257 height = gr->height;
2258 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2259 if (gx + g->pixel_width > x)
2260 break;
2261
2262 if (g < end)
2263 {
2264 if (g->type == IMAGE_GLYPH)
2265 {
2266 /* Don't remember when mouse is over image, as
2267 image may have hot-spots. */
2268 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2269 return;
2270 }
2271 width = g->pixel_width;
2272 }
2273 else
2274 {
2275 /* Use nominal char spacing at end of line. */
2276 x -= gx;
2277 gx += (x / width) * width;
2278 }
2279
2280 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2281 gx += window_box_left_offset (w, area);
2282 }
2283 else
2284 {
2285 /* Use nominal line height at end of window. */
2286 gx = (x / width) * width;
2287 y -= gy;
2288 gy += (y / height) * height;
2289 }
2290 break;
2291
2292 case ON_LEFT_FRINGE:
2293 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2294 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2295 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2296 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2297 goto row_glyph;
2298
2299 case ON_RIGHT_FRINGE:
2300 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2301 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2302 : window_box_right_offset (w, TEXT_AREA));
2303 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2304 goto row_glyph;
2305
2306 case ON_SCROLL_BAR:
2307 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2308 ? 0
2309 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2310 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2311 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2312 : 0)));
2313 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2314
2315 row_glyph:
2316 gr = 0, gy = 0;
2317 for (; r <= end_row && r->enabled_p; ++r)
2318 if (r->y + r->height > y)
2319 {
2320 gr = r; gy = r->y;
2321 break;
2322 }
2323
2324 if (gr && gy <= y)
2325 height = gr->height;
2326 else
2327 {
2328 /* Use nominal line height at end of window. */
2329 y -= gy;
2330 gy += (y / height) * height;
2331 }
2332 break;
2333
2334 default:
2335 ;
2336 virtual_glyph:
2337 /* If there is no glyph under the mouse, then we divide the screen
2338 into a grid of the smallest glyph in the frame, and use that
2339 as our "glyph". */
2340
2341 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2342 round down even for negative values. */
2343 if (gx < 0)
2344 gx -= width - 1;
2345 if (gy < 0)
2346 gy -= height - 1;
2347
2348 gx = (gx / width) * width;
2349 gy = (gy / height) * height;
2350
2351 goto store_rect;
2352 }
2353
2354 gx += WINDOW_LEFT_EDGE_X (w);
2355 gy += WINDOW_TOP_EDGE_Y (w);
2356
2357 store_rect:
2358 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2359
2360 /* Visible feedback for debugging. */
2361 #if 0
2362 #if HAVE_X_WINDOWS
2363 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2364 f->output_data.x->normal_gc,
2365 gx, gy, width, height);
2366 #endif
2367 #endif
2368 }
2369
2370
2371 #endif /* HAVE_WINDOW_SYSTEM */
2372
2373 \f
2374 /***********************************************************************
2375 Lisp form evaluation
2376 ***********************************************************************/
2377
2378 /* Error handler for safe_eval and safe_call. */
2379
2380 static Lisp_Object
2381 safe_eval_handler (arg)
2382 Lisp_Object arg;
2383 {
2384 add_to_log ("Error during redisplay: %s", arg, Qnil);
2385 return Qnil;
2386 }
2387
2388
2389 /* Evaluate SEXPR and return the result, or nil if something went
2390 wrong. Prevent redisplay during the evaluation. */
2391
2392 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2393 Return the result, or nil if something went wrong. Prevent
2394 redisplay during the evaluation. */
2395
2396 Lisp_Object
2397 safe_call (nargs, args)
2398 int nargs;
2399 Lisp_Object *args;
2400 {
2401 Lisp_Object val;
2402
2403 if (inhibit_eval_during_redisplay)
2404 val = Qnil;
2405 else
2406 {
2407 int count = SPECPDL_INDEX ();
2408 struct gcpro gcpro1;
2409
2410 GCPRO1 (args[0]);
2411 gcpro1.nvars = nargs;
2412 specbind (Qinhibit_redisplay, Qt);
2413 /* Use Qt to ensure debugger does not run,
2414 so there is no possibility of wanting to redisplay. */
2415 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2416 safe_eval_handler);
2417 UNGCPRO;
2418 val = unbind_to (count, val);
2419 }
2420
2421 return val;
2422 }
2423
2424
2425 /* Call function FN with one argument ARG.
2426 Return the result, or nil if something went wrong. */
2427
2428 Lisp_Object
2429 safe_call1 (fn, arg)
2430 Lisp_Object fn, arg;
2431 {
2432 Lisp_Object args[2];
2433 args[0] = fn;
2434 args[1] = arg;
2435 return safe_call (2, args);
2436 }
2437
2438 static Lisp_Object Qeval;
2439
2440 Lisp_Object
2441 safe_eval (Lisp_Object sexpr)
2442 {
2443 return safe_call1 (Qeval, sexpr);
2444 }
2445
2446 /* Call function FN with one argument ARG.
2447 Return the result, or nil if something went wrong. */
2448
2449 Lisp_Object
2450 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2451 {
2452 Lisp_Object args[3];
2453 args[0] = fn;
2454 args[1] = arg1;
2455 args[2] = arg2;
2456 return safe_call (3, args);
2457 }
2458
2459
2460 \f
2461 /***********************************************************************
2462 Debugging
2463 ***********************************************************************/
2464
2465 #if 0
2466
2467 /* Define CHECK_IT to perform sanity checks on iterators.
2468 This is for debugging. It is too slow to do unconditionally. */
2469
2470 static void
2471 check_it (it)
2472 struct it *it;
2473 {
2474 if (it->method == GET_FROM_STRING)
2475 {
2476 xassert (STRINGP (it->string));
2477 xassert (IT_STRING_CHARPOS (*it) >= 0);
2478 }
2479 else
2480 {
2481 xassert (IT_STRING_CHARPOS (*it) < 0);
2482 if (it->method == GET_FROM_BUFFER)
2483 {
2484 /* Check that character and byte positions agree. */
2485 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2486 }
2487 }
2488
2489 if (it->dpvec)
2490 xassert (it->current.dpvec_index >= 0);
2491 else
2492 xassert (it->current.dpvec_index < 0);
2493 }
2494
2495 #define CHECK_IT(IT) check_it ((IT))
2496
2497 #else /* not 0 */
2498
2499 #define CHECK_IT(IT) (void) 0
2500
2501 #endif /* not 0 */
2502
2503
2504 #if GLYPH_DEBUG
2505
2506 /* Check that the window end of window W is what we expect it
2507 to be---the last row in the current matrix displaying text. */
2508
2509 static void
2510 check_window_end (w)
2511 struct window *w;
2512 {
2513 if (!MINI_WINDOW_P (w)
2514 && !NILP (w->window_end_valid))
2515 {
2516 struct glyph_row *row;
2517 xassert ((row = MATRIX_ROW (w->current_matrix,
2518 XFASTINT (w->window_end_vpos)),
2519 !row->enabled_p
2520 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2521 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2522 }
2523 }
2524
2525 #define CHECK_WINDOW_END(W) check_window_end ((W))
2526
2527 #else /* not GLYPH_DEBUG */
2528
2529 #define CHECK_WINDOW_END(W) (void) 0
2530
2531 #endif /* not GLYPH_DEBUG */
2532
2533
2534 \f
2535 /***********************************************************************
2536 Iterator initialization
2537 ***********************************************************************/
2538
2539 /* Initialize IT for displaying current_buffer in window W, starting
2540 at character position CHARPOS. CHARPOS < 0 means that no buffer
2541 position is specified which is useful when the iterator is assigned
2542 a position later. BYTEPOS is the byte position corresponding to
2543 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2544
2545 If ROW is not null, calls to produce_glyphs with IT as parameter
2546 will produce glyphs in that row.
2547
2548 BASE_FACE_ID is the id of a base face to use. It must be one of
2549 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2550 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2551 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2552
2553 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2554 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2555 will be initialized to use the corresponding mode line glyph row of
2556 the desired matrix of W. */
2557
2558 void
2559 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2560 struct it *it;
2561 struct window *w;
2562 int charpos, bytepos;
2563 struct glyph_row *row;
2564 enum face_id base_face_id;
2565 {
2566 int highlight_region_p;
2567 enum face_id remapped_base_face_id = base_face_id;
2568
2569 /* Some precondition checks. */
2570 xassert (w != NULL && it != NULL);
2571 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2572 && charpos <= ZV));
2573
2574 /* If face attributes have been changed since the last redisplay,
2575 free realized faces now because they depend on face definitions
2576 that might have changed. Don't free faces while there might be
2577 desired matrices pending which reference these faces. */
2578 if (face_change_count && !inhibit_free_realized_faces)
2579 {
2580 face_change_count = 0;
2581 free_all_realized_faces (Qnil);
2582 }
2583
2584 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2585 if (! NILP (Vface_remapping_alist))
2586 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2587
2588 /* Use one of the mode line rows of W's desired matrix if
2589 appropriate. */
2590 if (row == NULL)
2591 {
2592 if (base_face_id == MODE_LINE_FACE_ID
2593 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2594 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2595 else if (base_face_id == HEADER_LINE_FACE_ID)
2596 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2597 }
2598
2599 /* Clear IT. */
2600 bzero (it, sizeof *it);
2601 it->current.overlay_string_index = -1;
2602 it->current.dpvec_index = -1;
2603 it->base_face_id = remapped_base_face_id;
2604 it->string = Qnil;
2605 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2606
2607 /* The window in which we iterate over current_buffer: */
2608 XSETWINDOW (it->window, w);
2609 it->w = w;
2610 it->f = XFRAME (w->frame);
2611
2612 it->cmp_it.id = -1;
2613
2614 /* Extra space between lines (on window systems only). */
2615 if (base_face_id == DEFAULT_FACE_ID
2616 && FRAME_WINDOW_P (it->f))
2617 {
2618 if (NATNUMP (current_buffer->extra_line_spacing))
2619 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2620 else if (FLOATP (current_buffer->extra_line_spacing))
2621 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2622 * FRAME_LINE_HEIGHT (it->f));
2623 else if (it->f->extra_line_spacing > 0)
2624 it->extra_line_spacing = it->f->extra_line_spacing;
2625 it->max_extra_line_spacing = 0;
2626 }
2627
2628 /* If realized faces have been removed, e.g. because of face
2629 attribute changes of named faces, recompute them. When running
2630 in batch mode, the face cache of the initial frame is null. If
2631 we happen to get called, make a dummy face cache. */
2632 if (FRAME_FACE_CACHE (it->f) == NULL)
2633 init_frame_faces (it->f);
2634 if (FRAME_FACE_CACHE (it->f)->used == 0)
2635 recompute_basic_faces (it->f);
2636
2637 /* Current value of the `slice', `space-width', and 'height' properties. */
2638 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2639 it->space_width = Qnil;
2640 it->font_height = Qnil;
2641 it->override_ascent = -1;
2642
2643 /* Are control characters displayed as `^C'? */
2644 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2645
2646 /* -1 means everything between a CR and the following line end
2647 is invisible. >0 means lines indented more than this value are
2648 invisible. */
2649 it->selective = (INTEGERP (current_buffer->selective_display)
2650 ? XFASTINT (current_buffer->selective_display)
2651 : (!NILP (current_buffer->selective_display)
2652 ? -1 : 0));
2653 it->selective_display_ellipsis_p
2654 = !NILP (current_buffer->selective_display_ellipses);
2655
2656 /* Display table to use. */
2657 it->dp = window_display_table (w);
2658
2659 /* Are multibyte characters enabled in current_buffer? */
2660 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2661
2662 /* Do we need to reorder bidirectional text? */
2663 it->bidi_p = !NILP (current_buffer->bidi_display_reordering);
2664
2665 /* Non-zero if we should highlight the region. */
2666 highlight_region_p
2667 = (!NILP (Vtransient_mark_mode)
2668 && !NILP (current_buffer->mark_active)
2669 && XMARKER (current_buffer->mark)->buffer != 0);
2670
2671 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2672 start and end of a visible region in window IT->w. Set both to
2673 -1 to indicate no region. */
2674 if (highlight_region_p
2675 /* Maybe highlight only in selected window. */
2676 && (/* Either show region everywhere. */
2677 highlight_nonselected_windows
2678 /* Or show region in the selected window. */
2679 || w == XWINDOW (selected_window)
2680 /* Or show the region if we are in the mini-buffer and W is
2681 the window the mini-buffer refers to. */
2682 || (MINI_WINDOW_P (XWINDOW (selected_window))
2683 && WINDOWP (minibuf_selected_window)
2684 && w == XWINDOW (minibuf_selected_window))))
2685 {
2686 int charpos = marker_position (current_buffer->mark);
2687 it->region_beg_charpos = min (PT, charpos);
2688 it->region_end_charpos = max (PT, charpos);
2689 }
2690 else
2691 it->region_beg_charpos = it->region_end_charpos = -1;
2692
2693 /* Get the position at which the redisplay_end_trigger hook should
2694 be run, if it is to be run at all. */
2695 if (MARKERP (w->redisplay_end_trigger)
2696 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2697 it->redisplay_end_trigger_charpos
2698 = marker_position (w->redisplay_end_trigger);
2699 else if (INTEGERP (w->redisplay_end_trigger))
2700 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2701
2702 /* Correct bogus values of tab_width. */
2703 it->tab_width = XINT (current_buffer->tab_width);
2704 if (it->tab_width <= 0 || it->tab_width > 1000)
2705 it->tab_width = 8;
2706
2707 /* Are lines in the display truncated? */
2708 if (base_face_id != DEFAULT_FACE_ID
2709 || XINT (it->w->hscroll)
2710 || (! WINDOW_FULL_WIDTH_P (it->w)
2711 && ((!NILP (Vtruncate_partial_width_windows)
2712 && !INTEGERP (Vtruncate_partial_width_windows))
2713 || (INTEGERP (Vtruncate_partial_width_windows)
2714 && (WINDOW_TOTAL_COLS (it->w)
2715 < XINT (Vtruncate_partial_width_windows))))))
2716 it->line_wrap = TRUNCATE;
2717 else if (NILP (current_buffer->truncate_lines))
2718 it->line_wrap = NILP (current_buffer->word_wrap)
2719 ? WINDOW_WRAP : WORD_WRAP;
2720 else
2721 it->line_wrap = TRUNCATE;
2722
2723 /* Get dimensions of truncation and continuation glyphs. These are
2724 displayed as fringe bitmaps under X, so we don't need them for such
2725 frames. */
2726 if (!FRAME_WINDOW_P (it->f))
2727 {
2728 if (it->line_wrap == TRUNCATE)
2729 {
2730 /* We will need the truncation glyph. */
2731 xassert (it->glyph_row == NULL);
2732 produce_special_glyphs (it, IT_TRUNCATION);
2733 it->truncation_pixel_width = it->pixel_width;
2734 }
2735 else
2736 {
2737 /* We will need the continuation glyph. */
2738 xassert (it->glyph_row == NULL);
2739 produce_special_glyphs (it, IT_CONTINUATION);
2740 it->continuation_pixel_width = it->pixel_width;
2741 }
2742
2743 /* Reset these values to zero because the produce_special_glyphs
2744 above has changed them. */
2745 it->pixel_width = it->ascent = it->descent = 0;
2746 it->phys_ascent = it->phys_descent = 0;
2747 }
2748
2749 /* Set this after getting the dimensions of truncation and
2750 continuation glyphs, so that we don't produce glyphs when calling
2751 produce_special_glyphs, above. */
2752 it->glyph_row = row;
2753 it->area = TEXT_AREA;
2754
2755 /* Get the dimensions of the display area. The display area
2756 consists of the visible window area plus a horizontally scrolled
2757 part to the left of the window. All x-values are relative to the
2758 start of this total display area. */
2759 if (base_face_id != DEFAULT_FACE_ID)
2760 {
2761 /* Mode lines, menu bar in terminal frames. */
2762 it->first_visible_x = 0;
2763 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2764 }
2765 else
2766 {
2767 it->first_visible_x
2768 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2769 it->last_visible_x = (it->first_visible_x
2770 + window_box_width (w, TEXT_AREA));
2771
2772 /* If we truncate lines, leave room for the truncator glyph(s) at
2773 the right margin. Otherwise, leave room for the continuation
2774 glyph(s). Truncation and continuation glyphs are not inserted
2775 for window-based redisplay. */
2776 if (!FRAME_WINDOW_P (it->f))
2777 {
2778 if (it->line_wrap == TRUNCATE)
2779 it->last_visible_x -= it->truncation_pixel_width;
2780 else
2781 it->last_visible_x -= it->continuation_pixel_width;
2782 }
2783
2784 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2785 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2786 }
2787
2788 /* Leave room for a border glyph. */
2789 if (!FRAME_WINDOW_P (it->f)
2790 && !WINDOW_RIGHTMOST_P (it->w))
2791 it->last_visible_x -= 1;
2792
2793 it->last_visible_y = window_text_bottom_y (w);
2794
2795 /* For mode lines and alike, arrange for the first glyph having a
2796 left box line if the face specifies a box. */
2797 if (base_face_id != DEFAULT_FACE_ID)
2798 {
2799 struct face *face;
2800
2801 it->face_id = remapped_base_face_id;
2802
2803 /* If we have a boxed mode line, make the first character appear
2804 with a left box line. */
2805 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2806 if (face->box != FACE_NO_BOX)
2807 it->start_of_box_run_p = 1;
2808 }
2809
2810 /* If we are to reorder bidirectional text, init the bidi
2811 iterator. */
2812 if (it->bidi_p)
2813 {
2814 /* Note the paragraph direction that this buffer wants to
2815 use. */
2816 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2817 it->paragraph_embedding = L2R;
2818 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2819 it->paragraph_embedding = R2L;
2820 else
2821 it->paragraph_embedding = NEUTRAL_DIR;
2822 bidi_init_it (charpos, bytepos, &it->bidi_it);
2823 }
2824
2825 /* If a buffer position was specified, set the iterator there,
2826 getting overlays and face properties from that position. */
2827 if (charpos >= BUF_BEG (current_buffer))
2828 {
2829 it->end_charpos = ZV;
2830 it->face_id = -1;
2831 IT_CHARPOS (*it) = charpos;
2832
2833 /* Compute byte position if not specified. */
2834 if (bytepos < charpos)
2835 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2836 else
2837 IT_BYTEPOS (*it) = bytepos;
2838
2839 it->start = it->current;
2840
2841 /* Compute faces etc. */
2842 reseat (it, it->current.pos, 1);
2843 }
2844
2845 CHECK_IT (it);
2846 }
2847
2848
2849 /* Initialize IT for the display of window W with window start POS. */
2850
2851 void
2852 start_display (it, w, pos)
2853 struct it *it;
2854 struct window *w;
2855 struct text_pos pos;
2856 {
2857 struct glyph_row *row;
2858 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2859
2860 row = w->desired_matrix->rows + first_vpos;
2861 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2862 it->first_vpos = first_vpos;
2863
2864 /* Don't reseat to previous visible line start if current start
2865 position is in a string or image. */
2866 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2867 {
2868 int start_at_line_beg_p;
2869 int first_y = it->current_y;
2870
2871 /* If window start is not at a line start, skip forward to POS to
2872 get the correct continuation lines width. */
2873 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2874 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2875 if (!start_at_line_beg_p)
2876 {
2877 int new_x;
2878
2879 reseat_at_previous_visible_line_start (it);
2880 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2881
2882 new_x = it->current_x + it->pixel_width;
2883
2884 /* If lines are continued, this line may end in the middle
2885 of a multi-glyph character (e.g. a control character
2886 displayed as \003, or in the middle of an overlay
2887 string). In this case move_it_to above will not have
2888 taken us to the start of the continuation line but to the
2889 end of the continued line. */
2890 if (it->current_x > 0
2891 && it->line_wrap != TRUNCATE /* Lines are continued. */
2892 && (/* And glyph doesn't fit on the line. */
2893 new_x > it->last_visible_x
2894 /* Or it fits exactly and we're on a window
2895 system frame. */
2896 || (new_x == it->last_visible_x
2897 && FRAME_WINDOW_P (it->f))))
2898 {
2899 if (it->current.dpvec_index >= 0
2900 || it->current.overlay_string_index >= 0)
2901 {
2902 set_iterator_to_next (it, 1);
2903 move_it_in_display_line_to (it, -1, -1, 0);
2904 }
2905
2906 it->continuation_lines_width += it->current_x;
2907 }
2908
2909 /* We're starting a new display line, not affected by the
2910 height of the continued line, so clear the appropriate
2911 fields in the iterator structure. */
2912 it->max_ascent = it->max_descent = 0;
2913 it->max_phys_ascent = it->max_phys_descent = 0;
2914
2915 it->current_y = first_y;
2916 it->vpos = 0;
2917 it->current_x = it->hpos = 0;
2918 }
2919 }
2920 }
2921
2922
2923 /* Return 1 if POS is a position in ellipses displayed for invisible
2924 text. W is the window we display, for text property lookup. */
2925
2926 static int
2927 in_ellipses_for_invisible_text_p (pos, w)
2928 struct display_pos *pos;
2929 struct window *w;
2930 {
2931 Lisp_Object prop, window;
2932 int ellipses_p = 0;
2933 int charpos = CHARPOS (pos->pos);
2934
2935 /* If POS specifies a position in a display vector, this might
2936 be for an ellipsis displayed for invisible text. We won't
2937 get the iterator set up for delivering that ellipsis unless
2938 we make sure that it gets aware of the invisible text. */
2939 if (pos->dpvec_index >= 0
2940 && pos->overlay_string_index < 0
2941 && CHARPOS (pos->string_pos) < 0
2942 && charpos > BEGV
2943 && (XSETWINDOW (window, w),
2944 prop = Fget_char_property (make_number (charpos),
2945 Qinvisible, window),
2946 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2947 {
2948 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2949 window);
2950 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2951 }
2952
2953 return ellipses_p;
2954 }
2955
2956
2957 /* Initialize IT for stepping through current_buffer in window W,
2958 starting at position POS that includes overlay string and display
2959 vector/ control character translation position information. Value
2960 is zero if there are overlay strings with newlines at POS. */
2961
2962 static int
2963 init_from_display_pos (it, w, pos)
2964 struct it *it;
2965 struct window *w;
2966 struct display_pos *pos;
2967 {
2968 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2969 int i, overlay_strings_with_newlines = 0;
2970
2971 /* If POS specifies a position in a display vector, this might
2972 be for an ellipsis displayed for invisible text. We won't
2973 get the iterator set up for delivering that ellipsis unless
2974 we make sure that it gets aware of the invisible text. */
2975 if (in_ellipses_for_invisible_text_p (pos, w))
2976 {
2977 --charpos;
2978 bytepos = 0;
2979 }
2980
2981 /* Keep in mind: the call to reseat in init_iterator skips invisible
2982 text, so we might end up at a position different from POS. This
2983 is only a problem when POS is a row start after a newline and an
2984 overlay starts there with an after-string, and the overlay has an
2985 invisible property. Since we don't skip invisible text in
2986 display_line and elsewhere immediately after consuming the
2987 newline before the row start, such a POS will not be in a string,
2988 but the call to init_iterator below will move us to the
2989 after-string. */
2990 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2991
2992 /* This only scans the current chunk -- it should scan all chunks.
2993 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2994 to 16 in 22.1 to make this a lesser problem. */
2995 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2996 {
2997 const char *s = SDATA (it->overlay_strings[i]);
2998 const char *e = s + SBYTES (it->overlay_strings[i]);
2999
3000 while (s < e && *s != '\n')
3001 ++s;
3002
3003 if (s < e)
3004 {
3005 overlay_strings_with_newlines = 1;
3006 break;
3007 }
3008 }
3009
3010 /* If position is within an overlay string, set up IT to the right
3011 overlay string. */
3012 if (pos->overlay_string_index >= 0)
3013 {
3014 int relative_index;
3015
3016 /* If the first overlay string happens to have a `display'
3017 property for an image, the iterator will be set up for that
3018 image, and we have to undo that setup first before we can
3019 correct the overlay string index. */
3020 if (it->method == GET_FROM_IMAGE)
3021 pop_it (it);
3022
3023 /* We already have the first chunk of overlay strings in
3024 IT->overlay_strings. Load more until the one for
3025 pos->overlay_string_index is in IT->overlay_strings. */
3026 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3027 {
3028 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3029 it->current.overlay_string_index = 0;
3030 while (n--)
3031 {
3032 load_overlay_strings (it, 0);
3033 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3034 }
3035 }
3036
3037 it->current.overlay_string_index = pos->overlay_string_index;
3038 relative_index = (it->current.overlay_string_index
3039 % OVERLAY_STRING_CHUNK_SIZE);
3040 it->string = it->overlay_strings[relative_index];
3041 xassert (STRINGP (it->string));
3042 it->current.string_pos = pos->string_pos;
3043 it->method = GET_FROM_STRING;
3044 }
3045
3046 if (CHARPOS (pos->string_pos) >= 0)
3047 {
3048 /* Recorded position is not in an overlay string, but in another
3049 string. This can only be a string from a `display' property.
3050 IT should already be filled with that string. */
3051 it->current.string_pos = pos->string_pos;
3052 xassert (STRINGP (it->string));
3053 }
3054
3055 /* Restore position in display vector translations, control
3056 character translations or ellipses. */
3057 if (pos->dpvec_index >= 0)
3058 {
3059 if (it->dpvec == NULL)
3060 get_next_display_element (it);
3061 xassert (it->dpvec && it->current.dpvec_index == 0);
3062 it->current.dpvec_index = pos->dpvec_index;
3063 }
3064
3065 CHECK_IT (it);
3066 return !overlay_strings_with_newlines;
3067 }
3068
3069
3070 /* Initialize IT for stepping through current_buffer in window W
3071 starting at ROW->start. */
3072
3073 static void
3074 init_to_row_start (it, w, row)
3075 struct it *it;
3076 struct window *w;
3077 struct glyph_row *row;
3078 {
3079 init_from_display_pos (it, w, &row->start);
3080 it->start = row->start;
3081 it->continuation_lines_width = row->continuation_lines_width;
3082 CHECK_IT (it);
3083 }
3084
3085
3086 /* Initialize IT for stepping through current_buffer in window W
3087 starting in the line following ROW, i.e. starting at ROW->end.
3088 Value is zero if there are overlay strings with newlines at ROW's
3089 end position. */
3090
3091 static int
3092 init_to_row_end (it, w, row)
3093 struct it *it;
3094 struct window *w;
3095 struct glyph_row *row;
3096 {
3097 int success = 0;
3098
3099 if (init_from_display_pos (it, w, &row->end))
3100 {
3101 if (row->continued_p)
3102 it->continuation_lines_width
3103 = row->continuation_lines_width + row->pixel_width;
3104 CHECK_IT (it);
3105 success = 1;
3106 }
3107
3108 return success;
3109 }
3110
3111
3112
3113 \f
3114 /***********************************************************************
3115 Text properties
3116 ***********************************************************************/
3117
3118 /* Called when IT reaches IT->stop_charpos. Handle text property and
3119 overlay changes. Set IT->stop_charpos to the next position where
3120 to stop. */
3121
3122 static void
3123 handle_stop (it)
3124 struct it *it;
3125 {
3126 enum prop_handled handled;
3127 int handle_overlay_change_p;
3128 struct props *p;
3129
3130 it->dpvec = NULL;
3131 it->current.dpvec_index = -1;
3132 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3133 it->ignore_overlay_strings_at_pos_p = 0;
3134 it->ellipsis_p = 0;
3135
3136 /* Use face of preceding text for ellipsis (if invisible) */
3137 if (it->selective_display_ellipsis_p)
3138 it->saved_face_id = it->face_id;
3139
3140 do
3141 {
3142 handled = HANDLED_NORMALLY;
3143
3144 /* Call text property handlers. */
3145 for (p = it_props; p->handler; ++p)
3146 {
3147 handled = p->handler (it);
3148
3149 if (handled == HANDLED_RECOMPUTE_PROPS)
3150 break;
3151 else if (handled == HANDLED_RETURN)
3152 {
3153 /* We still want to show before and after strings from
3154 overlays even if the actual buffer text is replaced. */
3155 if (!handle_overlay_change_p
3156 || it->sp > 1
3157 || !get_overlay_strings_1 (it, 0, 0))
3158 {
3159 if (it->ellipsis_p)
3160 setup_for_ellipsis (it, 0);
3161 /* When handling a display spec, we might load an
3162 empty string. In that case, discard it here. We
3163 used to discard it in handle_single_display_spec,
3164 but that causes get_overlay_strings_1, above, to
3165 ignore overlay strings that we must check. */
3166 if (STRINGP (it->string) && !SCHARS (it->string))
3167 pop_it (it);
3168 return;
3169 }
3170 else if (STRINGP (it->string) && !SCHARS (it->string))
3171 pop_it (it);
3172 else
3173 {
3174 it->ignore_overlay_strings_at_pos_p = 1;
3175 it->string_from_display_prop_p = 0;
3176 handle_overlay_change_p = 0;
3177 }
3178 handled = HANDLED_RECOMPUTE_PROPS;
3179 break;
3180 }
3181 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3182 handle_overlay_change_p = 0;
3183 }
3184
3185 if (handled != HANDLED_RECOMPUTE_PROPS)
3186 {
3187 /* Don't check for overlay strings below when set to deliver
3188 characters from a display vector. */
3189 if (it->method == GET_FROM_DISPLAY_VECTOR)
3190 handle_overlay_change_p = 0;
3191
3192 /* Handle overlay changes.
3193 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3194 if it finds overlays. */
3195 if (handle_overlay_change_p)
3196 handled = handle_overlay_change (it);
3197 }
3198
3199 if (it->ellipsis_p)
3200 {
3201 setup_for_ellipsis (it, 0);
3202 break;
3203 }
3204 }
3205 while (handled == HANDLED_RECOMPUTE_PROPS);
3206
3207 /* Determine where to stop next. */
3208 if (handled == HANDLED_NORMALLY)
3209 compute_stop_pos (it);
3210 }
3211
3212
3213 /* Compute IT->stop_charpos from text property and overlay change
3214 information for IT's current position. */
3215
3216 static void
3217 compute_stop_pos (it)
3218 struct it *it;
3219 {
3220 register INTERVAL iv, next_iv;
3221 Lisp_Object object, limit, position;
3222 EMACS_INT charpos, bytepos;
3223
3224 /* If nowhere else, stop at the end. */
3225 it->stop_charpos = it->end_charpos;
3226
3227 if (STRINGP (it->string))
3228 {
3229 /* Strings are usually short, so don't limit the search for
3230 properties. */
3231 object = it->string;
3232 limit = Qnil;
3233 charpos = IT_STRING_CHARPOS (*it);
3234 bytepos = IT_STRING_BYTEPOS (*it);
3235 }
3236 else
3237 {
3238 EMACS_INT pos;
3239
3240 /* If next overlay change is in front of the current stop pos
3241 (which is IT->end_charpos), stop there. Note: value of
3242 next_overlay_change is point-max if no overlay change
3243 follows. */
3244 charpos = IT_CHARPOS (*it);
3245 bytepos = IT_BYTEPOS (*it);
3246 pos = next_overlay_change (charpos);
3247 if (pos < it->stop_charpos)
3248 it->stop_charpos = pos;
3249
3250 /* If showing the region, we have to stop at the region
3251 start or end because the face might change there. */
3252 if (it->region_beg_charpos > 0)
3253 {
3254 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3255 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3256 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3257 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3258 }
3259
3260 /* Set up variables for computing the stop position from text
3261 property changes. */
3262 XSETBUFFER (object, current_buffer);
3263 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3264 }
3265
3266 /* Get the interval containing IT's position. Value is a null
3267 interval if there isn't such an interval. */
3268 position = make_number (charpos);
3269 iv = validate_interval_range (object, &position, &position, 0);
3270 if (!NULL_INTERVAL_P (iv))
3271 {
3272 Lisp_Object values_here[LAST_PROP_IDX];
3273 struct props *p;
3274
3275 /* Get properties here. */
3276 for (p = it_props; p->handler; ++p)
3277 values_here[p->idx] = textget (iv->plist, *p->name);
3278
3279 /* Look for an interval following iv that has different
3280 properties. */
3281 for (next_iv = next_interval (iv);
3282 (!NULL_INTERVAL_P (next_iv)
3283 && (NILP (limit)
3284 || XFASTINT (limit) > next_iv->position));
3285 next_iv = next_interval (next_iv))
3286 {
3287 for (p = it_props; p->handler; ++p)
3288 {
3289 Lisp_Object new_value;
3290
3291 new_value = textget (next_iv->plist, *p->name);
3292 if (!EQ (values_here[p->idx], new_value))
3293 break;
3294 }
3295
3296 if (p->handler)
3297 break;
3298 }
3299
3300 if (!NULL_INTERVAL_P (next_iv))
3301 {
3302 if (INTEGERP (limit)
3303 && next_iv->position >= XFASTINT (limit))
3304 /* No text property change up to limit. */
3305 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3306 else
3307 /* Text properties change in next_iv. */
3308 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3309 }
3310 }
3311
3312 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3313 it->stop_charpos, it->string);
3314
3315 xassert (STRINGP (it->string)
3316 || (it->stop_charpos >= BEGV
3317 && it->stop_charpos >= IT_CHARPOS (*it)));
3318 }
3319
3320
3321 /* Return the position of the next overlay change after POS in
3322 current_buffer. Value is point-max if no overlay change
3323 follows. This is like `next-overlay-change' but doesn't use
3324 xmalloc. */
3325
3326 static EMACS_INT
3327 next_overlay_change (pos)
3328 EMACS_INT pos;
3329 {
3330 int noverlays;
3331 EMACS_INT endpos;
3332 Lisp_Object *overlays;
3333 int i;
3334
3335 /* Get all overlays at the given position. */
3336 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3337
3338 /* If any of these overlays ends before endpos,
3339 use its ending point instead. */
3340 for (i = 0; i < noverlays; ++i)
3341 {
3342 Lisp_Object oend;
3343 EMACS_INT oendpos;
3344
3345 oend = OVERLAY_END (overlays[i]);
3346 oendpos = OVERLAY_POSITION (oend);
3347 endpos = min (endpos, oendpos);
3348 }
3349
3350 return endpos;
3351 }
3352
3353
3354 \f
3355 /***********************************************************************
3356 Fontification
3357 ***********************************************************************/
3358
3359 /* Handle changes in the `fontified' property of the current buffer by
3360 calling hook functions from Qfontification_functions to fontify
3361 regions of text. */
3362
3363 static enum prop_handled
3364 handle_fontified_prop (it)
3365 struct it *it;
3366 {
3367 Lisp_Object prop, pos;
3368 enum prop_handled handled = HANDLED_NORMALLY;
3369
3370 if (!NILP (Vmemory_full))
3371 return handled;
3372
3373 /* Get the value of the `fontified' property at IT's current buffer
3374 position. (The `fontified' property doesn't have a special
3375 meaning in strings.) If the value is nil, call functions from
3376 Qfontification_functions. */
3377 if (!STRINGP (it->string)
3378 && it->s == NULL
3379 && !NILP (Vfontification_functions)
3380 && !NILP (Vrun_hooks)
3381 && (pos = make_number (IT_CHARPOS (*it)),
3382 prop = Fget_char_property (pos, Qfontified, Qnil),
3383 /* Ignore the special cased nil value always present at EOB since
3384 no amount of fontifying will be able to change it. */
3385 NILP (prop) && IT_CHARPOS (*it) < Z))
3386 {
3387 int count = SPECPDL_INDEX ();
3388 Lisp_Object val;
3389
3390 val = Vfontification_functions;
3391 specbind (Qfontification_functions, Qnil);
3392
3393 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3394 safe_call1 (val, pos);
3395 else
3396 {
3397 Lisp_Object globals, fn;
3398 struct gcpro gcpro1, gcpro2;
3399
3400 globals = Qnil;
3401 GCPRO2 (val, globals);
3402
3403 for (; CONSP (val); val = XCDR (val))
3404 {
3405 fn = XCAR (val);
3406
3407 if (EQ (fn, Qt))
3408 {
3409 /* A value of t indicates this hook has a local
3410 binding; it means to run the global binding too.
3411 In a global value, t should not occur. If it
3412 does, we must ignore it to avoid an endless
3413 loop. */
3414 for (globals = Fdefault_value (Qfontification_functions);
3415 CONSP (globals);
3416 globals = XCDR (globals))
3417 {
3418 fn = XCAR (globals);
3419 if (!EQ (fn, Qt))
3420 safe_call1 (fn, pos);
3421 }
3422 }
3423 else
3424 safe_call1 (fn, pos);
3425 }
3426
3427 UNGCPRO;
3428 }
3429
3430 unbind_to (count, Qnil);
3431
3432 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3433 something. This avoids an endless loop if they failed to
3434 fontify the text for which reason ever. */
3435 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3436 handled = HANDLED_RECOMPUTE_PROPS;
3437 }
3438
3439 return handled;
3440 }
3441
3442
3443 \f
3444 /***********************************************************************
3445 Faces
3446 ***********************************************************************/
3447
3448 /* Set up iterator IT from face properties at its current position.
3449 Called from handle_stop. */
3450
3451 static enum prop_handled
3452 handle_face_prop (it)
3453 struct it *it;
3454 {
3455 int new_face_id;
3456 EMACS_INT next_stop;
3457
3458 if (!STRINGP (it->string))
3459 {
3460 new_face_id
3461 = face_at_buffer_position (it->w,
3462 IT_CHARPOS (*it),
3463 it->region_beg_charpos,
3464 it->region_end_charpos,
3465 &next_stop,
3466 (IT_CHARPOS (*it)
3467 + TEXT_PROP_DISTANCE_LIMIT),
3468 0, it->base_face_id);
3469
3470 /* Is this a start of a run of characters with box face?
3471 Caveat: this can be called for a freshly initialized
3472 iterator; face_id is -1 in this case. We know that the new
3473 face will not change until limit, i.e. if the new face has a
3474 box, all characters up to limit will have one. But, as
3475 usual, we don't know whether limit is really the end. */
3476 if (new_face_id != it->face_id)
3477 {
3478 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3479
3480 /* If new face has a box but old face has not, this is
3481 the start of a run of characters with box, i.e. it has
3482 a shadow on the left side. The value of face_id of the
3483 iterator will be -1 if this is the initial call that gets
3484 the face. In this case, we have to look in front of IT's
3485 position and see whether there is a face != new_face_id. */
3486 it->start_of_box_run_p
3487 = (new_face->box != FACE_NO_BOX
3488 && (it->face_id >= 0
3489 || IT_CHARPOS (*it) == BEG
3490 || new_face_id != face_before_it_pos (it)));
3491 it->face_box_p = new_face->box != FACE_NO_BOX;
3492 }
3493 }
3494 else
3495 {
3496 int base_face_id, bufpos;
3497 int i;
3498 Lisp_Object from_overlay
3499 = (it->current.overlay_string_index >= 0
3500 ? it->string_overlays[it->current.overlay_string_index]
3501 : Qnil);
3502
3503 /* See if we got to this string directly or indirectly from
3504 an overlay property. That includes the before-string or
3505 after-string of an overlay, strings in display properties
3506 provided by an overlay, their text properties, etc.
3507
3508 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3509 if (! NILP (from_overlay))
3510 for (i = it->sp - 1; i >= 0; i--)
3511 {
3512 if (it->stack[i].current.overlay_string_index >= 0)
3513 from_overlay
3514 = it->string_overlays[it->stack[i].current.overlay_string_index];
3515 else if (! NILP (it->stack[i].from_overlay))
3516 from_overlay = it->stack[i].from_overlay;
3517
3518 if (!NILP (from_overlay))
3519 break;
3520 }
3521
3522 if (! NILP (from_overlay))
3523 {
3524 bufpos = IT_CHARPOS (*it);
3525 /* For a string from an overlay, the base face depends
3526 only on text properties and ignores overlays. */
3527 base_face_id
3528 = face_for_overlay_string (it->w,
3529 IT_CHARPOS (*it),
3530 it->region_beg_charpos,
3531 it->region_end_charpos,
3532 &next_stop,
3533 (IT_CHARPOS (*it)
3534 + TEXT_PROP_DISTANCE_LIMIT),
3535 0,
3536 from_overlay);
3537 }
3538 else
3539 {
3540 bufpos = 0;
3541
3542 /* For strings from a `display' property, use the face at
3543 IT's current buffer position as the base face to merge
3544 with, so that overlay strings appear in the same face as
3545 surrounding text, unless they specify their own
3546 faces. */
3547 base_face_id = underlying_face_id (it);
3548 }
3549
3550 new_face_id = face_at_string_position (it->w,
3551 it->string,
3552 IT_STRING_CHARPOS (*it),
3553 bufpos,
3554 it->region_beg_charpos,
3555 it->region_end_charpos,
3556 &next_stop,
3557 base_face_id, 0);
3558
3559 /* Is this a start of a run of characters with box? Caveat:
3560 this can be called for a freshly allocated iterator; face_id
3561 is -1 is this case. We know that the new face will not
3562 change until the next check pos, i.e. if the new face has a
3563 box, all characters up to that position will have a
3564 box. But, as usual, we don't know whether that position
3565 is really the end. */
3566 if (new_face_id != it->face_id)
3567 {
3568 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3569 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3570
3571 /* If new face has a box but old face hasn't, this is the
3572 start of a run of characters with box, i.e. it has a
3573 shadow on the left side. */
3574 it->start_of_box_run_p
3575 = new_face->box && (old_face == NULL || !old_face->box);
3576 it->face_box_p = new_face->box != FACE_NO_BOX;
3577 }
3578 }
3579
3580 it->face_id = new_face_id;
3581 return HANDLED_NORMALLY;
3582 }
3583
3584
3585 /* Return the ID of the face ``underlying'' IT's current position,
3586 which is in a string. If the iterator is associated with a
3587 buffer, return the face at IT's current buffer position.
3588 Otherwise, use the iterator's base_face_id. */
3589
3590 static int
3591 underlying_face_id (it)
3592 struct it *it;
3593 {
3594 int face_id = it->base_face_id, i;
3595
3596 xassert (STRINGP (it->string));
3597
3598 for (i = it->sp - 1; i >= 0; --i)
3599 if (NILP (it->stack[i].string))
3600 face_id = it->stack[i].face_id;
3601
3602 return face_id;
3603 }
3604
3605
3606 /* Compute the face one character before or after the current position
3607 of IT. BEFORE_P non-zero means get the face in front of IT's
3608 position. Value is the id of the face. */
3609
3610 static int
3611 face_before_or_after_it_pos (it, before_p)
3612 struct it *it;
3613 int before_p;
3614 {
3615 int face_id, limit;
3616 EMACS_INT next_check_charpos;
3617 struct text_pos pos;
3618
3619 xassert (it->s == NULL);
3620
3621 if (STRINGP (it->string))
3622 {
3623 int bufpos, base_face_id;
3624
3625 /* No face change past the end of the string (for the case
3626 we are padding with spaces). No face change before the
3627 string start. */
3628 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3629 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3630 return it->face_id;
3631
3632 /* Set pos to the position before or after IT's current position. */
3633 if (before_p)
3634 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3635 else
3636 /* For composition, we must check the character after the
3637 composition. */
3638 pos = (it->what == IT_COMPOSITION
3639 ? string_pos (IT_STRING_CHARPOS (*it)
3640 + it->cmp_it.nchars, it->string)
3641 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3642
3643 if (it->current.overlay_string_index >= 0)
3644 bufpos = IT_CHARPOS (*it);
3645 else
3646 bufpos = 0;
3647
3648 base_face_id = underlying_face_id (it);
3649
3650 /* Get the face for ASCII, or unibyte. */
3651 face_id = face_at_string_position (it->w,
3652 it->string,
3653 CHARPOS (pos),
3654 bufpos,
3655 it->region_beg_charpos,
3656 it->region_end_charpos,
3657 &next_check_charpos,
3658 base_face_id, 0);
3659
3660 /* Correct the face for charsets different from ASCII. Do it
3661 for the multibyte case only. The face returned above is
3662 suitable for unibyte text if IT->string is unibyte. */
3663 if (STRING_MULTIBYTE (it->string))
3664 {
3665 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3666 int rest = SBYTES (it->string) - BYTEPOS (pos);
3667 int c, len;
3668 struct face *face = FACE_FROM_ID (it->f, face_id);
3669
3670 c = string_char_and_length (p, &len);
3671 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3672 }
3673 }
3674 else
3675 {
3676 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3677 || (IT_CHARPOS (*it) <= BEGV && before_p))
3678 return it->face_id;
3679
3680 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3681 pos = it->current.pos;
3682
3683 if (before_p)
3684 DEC_TEXT_POS (pos, it->multibyte_p);
3685 else
3686 {
3687 if (it->what == IT_COMPOSITION)
3688 /* For composition, we must check the position after the
3689 composition. */
3690 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3691 else
3692 INC_TEXT_POS (pos, it->multibyte_p);
3693 }
3694
3695 /* Determine face for CHARSET_ASCII, or unibyte. */
3696 face_id = face_at_buffer_position (it->w,
3697 CHARPOS (pos),
3698 it->region_beg_charpos,
3699 it->region_end_charpos,
3700 &next_check_charpos,
3701 limit, 0, -1);
3702
3703 /* Correct the face for charsets different from ASCII. Do it
3704 for the multibyte case only. The face returned above is
3705 suitable for unibyte text if current_buffer is unibyte. */
3706 if (it->multibyte_p)
3707 {
3708 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3709 struct face *face = FACE_FROM_ID (it->f, face_id);
3710 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3711 }
3712 }
3713
3714 return face_id;
3715 }
3716
3717
3718 \f
3719 /***********************************************************************
3720 Invisible text
3721 ***********************************************************************/
3722
3723 /* Set up iterator IT from invisible properties at its current
3724 position. Called from handle_stop. */
3725
3726 static enum prop_handled
3727 handle_invisible_prop (it)
3728 struct it *it;
3729 {
3730 enum prop_handled handled = HANDLED_NORMALLY;
3731
3732 if (STRINGP (it->string))
3733 {
3734 extern Lisp_Object Qinvisible;
3735 Lisp_Object prop, end_charpos, limit, charpos;
3736
3737 /* Get the value of the invisible text property at the
3738 current position. Value will be nil if there is no such
3739 property. */
3740 charpos = make_number (IT_STRING_CHARPOS (*it));
3741 prop = Fget_text_property (charpos, Qinvisible, it->string);
3742
3743 if (!NILP (prop)
3744 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3745 {
3746 handled = HANDLED_RECOMPUTE_PROPS;
3747
3748 /* Get the position at which the next change of the
3749 invisible text property can be found in IT->string.
3750 Value will be nil if the property value is the same for
3751 all the rest of IT->string. */
3752 XSETINT (limit, SCHARS (it->string));
3753 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3754 it->string, limit);
3755
3756 /* Text at current position is invisible. The next
3757 change in the property is at position end_charpos.
3758 Move IT's current position to that position. */
3759 if (INTEGERP (end_charpos)
3760 && XFASTINT (end_charpos) < XFASTINT (limit))
3761 {
3762 struct text_pos old;
3763 old = it->current.string_pos;
3764 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3765 compute_string_pos (&it->current.string_pos, old, it->string);
3766 }
3767 else
3768 {
3769 /* The rest of the string is invisible. If this is an
3770 overlay string, proceed with the next overlay string
3771 or whatever comes and return a character from there. */
3772 if (it->current.overlay_string_index >= 0)
3773 {
3774 next_overlay_string (it);
3775 /* Don't check for overlay strings when we just
3776 finished processing them. */
3777 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3778 }
3779 else
3780 {
3781 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3782 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3783 }
3784 }
3785 }
3786 }
3787 else
3788 {
3789 int invis_p;
3790 EMACS_INT newpos, next_stop, start_charpos;
3791 Lisp_Object pos, prop, overlay;
3792
3793 /* First of all, is there invisible text at this position? */
3794 start_charpos = IT_CHARPOS (*it);
3795 pos = make_number (IT_CHARPOS (*it));
3796 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3797 &overlay);
3798 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3799
3800 /* If we are on invisible text, skip over it. */
3801 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3802 {
3803 /* Record whether we have to display an ellipsis for the
3804 invisible text. */
3805 int display_ellipsis_p = invis_p == 2;
3806
3807 handled = HANDLED_RECOMPUTE_PROPS;
3808
3809 /* Loop skipping over invisible text. The loop is left at
3810 ZV or with IT on the first char being visible again. */
3811 do
3812 {
3813 /* Try to skip some invisible text. Return value is the
3814 position reached which can be equal to IT's position
3815 if there is nothing invisible here. This skips both
3816 over invisible text properties and overlays with
3817 invisible property. */
3818 newpos = skip_invisible (IT_CHARPOS (*it),
3819 &next_stop, ZV, it->window);
3820
3821 /* If we skipped nothing at all we weren't at invisible
3822 text in the first place. If everything to the end of
3823 the buffer was skipped, end the loop. */
3824 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3825 invis_p = 0;
3826 else
3827 {
3828 /* We skipped some characters but not necessarily
3829 all there are. Check if we ended up on visible
3830 text. Fget_char_property returns the property of
3831 the char before the given position, i.e. if we
3832 get invis_p = 0, this means that the char at
3833 newpos is visible. */
3834 pos = make_number (newpos);
3835 prop = Fget_char_property (pos, Qinvisible, it->window);
3836 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3837 }
3838
3839 /* If we ended up on invisible text, proceed to
3840 skip starting with next_stop. */
3841 if (invis_p)
3842 IT_CHARPOS (*it) = next_stop;
3843
3844 /* If there are adjacent invisible texts, don't lose the
3845 second one's ellipsis. */
3846 if (invis_p == 2)
3847 display_ellipsis_p = 1;
3848 }
3849 while (invis_p);
3850
3851 /* The position newpos is now either ZV or on visible text. */
3852 IT_CHARPOS (*it) = newpos;
3853 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3854
3855 /* If there are before-strings at the start of invisible
3856 text, and the text is invisible because of a text
3857 property, arrange to show before-strings because 20.x did
3858 it that way. (If the text is invisible because of an
3859 overlay property instead of a text property, this is
3860 already handled in the overlay code.) */
3861 if (NILP (overlay)
3862 && get_overlay_strings (it, start_charpos))
3863 {
3864 handled = HANDLED_RECOMPUTE_PROPS;
3865 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3866 }
3867 else if (display_ellipsis_p)
3868 {
3869 /* Make sure that the glyphs of the ellipsis will get
3870 correct `charpos' values. If we would not update
3871 it->position here, the glyphs would belong to the
3872 last visible character _before_ the invisible
3873 text, which confuses `set_cursor_from_row'.
3874
3875 We use the last invisible position instead of the
3876 first because this way the cursor is always drawn on
3877 the first "." of the ellipsis, whenever PT is inside
3878 the invisible text. Otherwise the cursor would be
3879 placed _after_ the ellipsis when the point is after the
3880 first invisible character. */
3881 if (!STRINGP (it->object))
3882 {
3883 it->position.charpos = IT_CHARPOS (*it) - 1;
3884 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3885 }
3886 it->ellipsis_p = 1;
3887 /* Let the ellipsis display before
3888 considering any properties of the following char.
3889 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3890 handled = HANDLED_RETURN;
3891 }
3892 }
3893 }
3894
3895 return handled;
3896 }
3897
3898
3899 /* Make iterator IT return `...' next.
3900 Replaces LEN characters from buffer. */
3901
3902 static void
3903 setup_for_ellipsis (it, len)
3904 struct it *it;
3905 int len;
3906 {
3907 /* Use the display table definition for `...'. Invalid glyphs
3908 will be handled by the method returning elements from dpvec. */
3909 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3910 {
3911 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3912 it->dpvec = v->contents;
3913 it->dpend = v->contents + v->size;
3914 }
3915 else
3916 {
3917 /* Default `...'. */
3918 it->dpvec = default_invis_vector;
3919 it->dpend = default_invis_vector + 3;
3920 }
3921
3922 it->dpvec_char_len = len;
3923 it->current.dpvec_index = 0;
3924 it->dpvec_face_id = -1;
3925
3926 /* Remember the current face id in case glyphs specify faces.
3927 IT's face is restored in set_iterator_to_next.
3928 saved_face_id was set to preceding char's face in handle_stop. */
3929 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3930 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3931
3932 it->method = GET_FROM_DISPLAY_VECTOR;
3933 it->ellipsis_p = 1;
3934 }
3935
3936
3937 \f
3938 /***********************************************************************
3939 'display' property
3940 ***********************************************************************/
3941
3942 /* Set up iterator IT from `display' property at its current position.
3943 Called from handle_stop.
3944 We return HANDLED_RETURN if some part of the display property
3945 overrides the display of the buffer text itself.
3946 Otherwise we return HANDLED_NORMALLY. */
3947
3948 static enum prop_handled
3949 handle_display_prop (it)
3950 struct it *it;
3951 {
3952 Lisp_Object prop, object, overlay;
3953 struct text_pos *position;
3954 /* Nonzero if some property replaces the display of the text itself. */
3955 int display_replaced_p = 0;
3956
3957 if (STRINGP (it->string))
3958 {
3959 object = it->string;
3960 position = &it->current.string_pos;
3961 }
3962 else
3963 {
3964 XSETWINDOW (object, it->w);
3965 position = &it->current.pos;
3966 }
3967
3968 /* Reset those iterator values set from display property values. */
3969 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3970 it->space_width = Qnil;
3971 it->font_height = Qnil;
3972 it->voffset = 0;
3973
3974 /* We don't support recursive `display' properties, i.e. string
3975 values that have a string `display' property, that have a string
3976 `display' property etc. */
3977 if (!it->string_from_display_prop_p)
3978 it->area = TEXT_AREA;
3979
3980 prop = get_char_property_and_overlay (make_number (position->charpos),
3981 Qdisplay, object, &overlay);
3982 if (NILP (prop))
3983 return HANDLED_NORMALLY;
3984 /* Now OVERLAY is the overlay that gave us this property, or nil
3985 if it was a text property. */
3986
3987 if (!STRINGP (it->string))
3988 object = it->w->buffer;
3989
3990 if (CONSP (prop)
3991 /* Simple properties. */
3992 && !EQ (XCAR (prop), Qimage)
3993 && !EQ (XCAR (prop), Qspace)
3994 && !EQ (XCAR (prop), Qwhen)
3995 && !EQ (XCAR (prop), Qslice)
3996 && !EQ (XCAR (prop), Qspace_width)
3997 && !EQ (XCAR (prop), Qheight)
3998 && !EQ (XCAR (prop), Qraise)
3999 /* Marginal area specifications. */
4000 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4001 && !EQ (XCAR (prop), Qleft_fringe)
4002 && !EQ (XCAR (prop), Qright_fringe)
4003 && !NILP (XCAR (prop)))
4004 {
4005 for (; CONSP (prop); prop = XCDR (prop))
4006 {
4007 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4008 position, display_replaced_p))
4009 {
4010 display_replaced_p = 1;
4011 /* If some text in a string is replaced, `position' no
4012 longer points to the position of `object'. */
4013 if (STRINGP (object))
4014 break;
4015 }
4016 }
4017 }
4018 else if (VECTORP (prop))
4019 {
4020 int i;
4021 for (i = 0; i < ASIZE (prop); ++i)
4022 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4023 position, display_replaced_p))
4024 {
4025 display_replaced_p = 1;
4026 /* If some text in a string is replaced, `position' no
4027 longer points to the position of `object'. */
4028 if (STRINGP (object))
4029 break;
4030 }
4031 }
4032 else
4033 {
4034 if (handle_single_display_spec (it, prop, object, overlay,
4035 position, 0))
4036 display_replaced_p = 1;
4037 }
4038
4039 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4040 }
4041
4042
4043 /* Value is the position of the end of the `display' property starting
4044 at START_POS in OBJECT. */
4045
4046 static struct text_pos
4047 display_prop_end (it, object, start_pos)
4048 struct it *it;
4049 Lisp_Object object;
4050 struct text_pos start_pos;
4051 {
4052 Lisp_Object end;
4053 struct text_pos end_pos;
4054
4055 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4056 Qdisplay, object, Qnil);
4057 CHARPOS (end_pos) = XFASTINT (end);
4058 if (STRINGP (object))
4059 compute_string_pos (&end_pos, start_pos, it->string);
4060 else
4061 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4062
4063 return end_pos;
4064 }
4065
4066
4067 /* Set up IT from a single `display' specification PROP. OBJECT
4068 is the object in which the `display' property was found. *POSITION
4069 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4070 means that we previously saw a display specification which already
4071 replaced text display with something else, for example an image;
4072 we ignore such properties after the first one has been processed.
4073
4074 OVERLAY is the overlay this `display' property came from,
4075 or nil if it was a text property.
4076
4077 If PROP is a `space' or `image' specification, and in some other
4078 cases too, set *POSITION to the position where the `display'
4079 property ends.
4080
4081 Value is non-zero if something was found which replaces the display
4082 of buffer or string text. */
4083
4084 static int
4085 handle_single_display_spec (it, spec, object, overlay, position,
4086 display_replaced_before_p)
4087 struct it *it;
4088 Lisp_Object spec;
4089 Lisp_Object object;
4090 Lisp_Object overlay;
4091 struct text_pos *position;
4092 int display_replaced_before_p;
4093 {
4094 Lisp_Object form;
4095 Lisp_Object location, value;
4096 struct text_pos start_pos, save_pos;
4097 int valid_p;
4098
4099 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4100 If the result is non-nil, use VALUE instead of SPEC. */
4101 form = Qt;
4102 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4103 {
4104 spec = XCDR (spec);
4105 if (!CONSP (spec))
4106 return 0;
4107 form = XCAR (spec);
4108 spec = XCDR (spec);
4109 }
4110
4111 if (!NILP (form) && !EQ (form, Qt))
4112 {
4113 int count = SPECPDL_INDEX ();
4114 struct gcpro gcpro1;
4115
4116 /* Bind `object' to the object having the `display' property, a
4117 buffer or string. Bind `position' to the position in the
4118 object where the property was found, and `buffer-position'
4119 to the current position in the buffer. */
4120 specbind (Qobject, object);
4121 specbind (Qposition, make_number (CHARPOS (*position)));
4122 specbind (Qbuffer_position,
4123 make_number (STRINGP (object)
4124 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4125 GCPRO1 (form);
4126 form = safe_eval (form);
4127 UNGCPRO;
4128 unbind_to (count, Qnil);
4129 }
4130
4131 if (NILP (form))
4132 return 0;
4133
4134 /* Handle `(height HEIGHT)' specifications. */
4135 if (CONSP (spec)
4136 && EQ (XCAR (spec), Qheight)
4137 && CONSP (XCDR (spec)))
4138 {
4139 if (!FRAME_WINDOW_P (it->f))
4140 return 0;
4141
4142 it->font_height = XCAR (XCDR (spec));
4143 if (!NILP (it->font_height))
4144 {
4145 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4146 int new_height = -1;
4147
4148 if (CONSP (it->font_height)
4149 && (EQ (XCAR (it->font_height), Qplus)
4150 || EQ (XCAR (it->font_height), Qminus))
4151 && CONSP (XCDR (it->font_height))
4152 && INTEGERP (XCAR (XCDR (it->font_height))))
4153 {
4154 /* `(+ N)' or `(- N)' where N is an integer. */
4155 int steps = XINT (XCAR (XCDR (it->font_height)));
4156 if (EQ (XCAR (it->font_height), Qplus))
4157 steps = - steps;
4158 it->face_id = smaller_face (it->f, it->face_id, steps);
4159 }
4160 else if (FUNCTIONP (it->font_height))
4161 {
4162 /* Call function with current height as argument.
4163 Value is the new height. */
4164 Lisp_Object height;
4165 height = safe_call1 (it->font_height,
4166 face->lface[LFACE_HEIGHT_INDEX]);
4167 if (NUMBERP (height))
4168 new_height = XFLOATINT (height);
4169 }
4170 else if (NUMBERP (it->font_height))
4171 {
4172 /* Value is a multiple of the canonical char height. */
4173 struct face *face;
4174
4175 face = FACE_FROM_ID (it->f,
4176 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4177 new_height = (XFLOATINT (it->font_height)
4178 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4179 }
4180 else
4181 {
4182 /* Evaluate IT->font_height with `height' bound to the
4183 current specified height to get the new height. */
4184 int count = SPECPDL_INDEX ();
4185
4186 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4187 value = safe_eval (it->font_height);
4188 unbind_to (count, Qnil);
4189
4190 if (NUMBERP (value))
4191 new_height = XFLOATINT (value);
4192 }
4193
4194 if (new_height > 0)
4195 it->face_id = face_with_height (it->f, it->face_id, new_height);
4196 }
4197
4198 return 0;
4199 }
4200
4201 /* Handle `(space-width WIDTH)'. */
4202 if (CONSP (spec)
4203 && EQ (XCAR (spec), Qspace_width)
4204 && CONSP (XCDR (spec)))
4205 {
4206 if (!FRAME_WINDOW_P (it->f))
4207 return 0;
4208
4209 value = XCAR (XCDR (spec));
4210 if (NUMBERP (value) && XFLOATINT (value) > 0)
4211 it->space_width = value;
4212
4213 return 0;
4214 }
4215
4216 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4217 if (CONSP (spec)
4218 && EQ (XCAR (spec), Qslice))
4219 {
4220 Lisp_Object tem;
4221
4222 if (!FRAME_WINDOW_P (it->f))
4223 return 0;
4224
4225 if (tem = XCDR (spec), CONSP (tem))
4226 {
4227 it->slice.x = XCAR (tem);
4228 if (tem = XCDR (tem), CONSP (tem))
4229 {
4230 it->slice.y = XCAR (tem);
4231 if (tem = XCDR (tem), CONSP (tem))
4232 {
4233 it->slice.width = XCAR (tem);
4234 if (tem = XCDR (tem), CONSP (tem))
4235 it->slice.height = XCAR (tem);
4236 }
4237 }
4238 }
4239
4240 return 0;
4241 }
4242
4243 /* Handle `(raise FACTOR)'. */
4244 if (CONSP (spec)
4245 && EQ (XCAR (spec), Qraise)
4246 && CONSP (XCDR (spec)))
4247 {
4248 if (!FRAME_WINDOW_P (it->f))
4249 return 0;
4250
4251 #ifdef HAVE_WINDOW_SYSTEM
4252 value = XCAR (XCDR (spec));
4253 if (NUMBERP (value))
4254 {
4255 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4256 it->voffset = - (XFLOATINT (value)
4257 * (FONT_HEIGHT (face->font)));
4258 }
4259 #endif /* HAVE_WINDOW_SYSTEM */
4260
4261 return 0;
4262 }
4263
4264 /* Don't handle the other kinds of display specifications
4265 inside a string that we got from a `display' property. */
4266 if (it->string_from_display_prop_p)
4267 return 0;
4268
4269 /* Characters having this form of property are not displayed, so
4270 we have to find the end of the property. */
4271 start_pos = *position;
4272 *position = display_prop_end (it, object, start_pos);
4273 value = Qnil;
4274
4275 /* Stop the scan at that end position--we assume that all
4276 text properties change there. */
4277 it->stop_charpos = position->charpos;
4278
4279 /* Handle `(left-fringe BITMAP [FACE])'
4280 and `(right-fringe BITMAP [FACE])'. */
4281 if (CONSP (spec)
4282 && (EQ (XCAR (spec), Qleft_fringe)
4283 || EQ (XCAR (spec), Qright_fringe))
4284 && CONSP (XCDR (spec)))
4285 {
4286 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4287 int fringe_bitmap;
4288
4289 if (!FRAME_WINDOW_P (it->f))
4290 /* If we return here, POSITION has been advanced
4291 across the text with this property. */
4292 return 0;
4293
4294 #ifdef HAVE_WINDOW_SYSTEM
4295 value = XCAR (XCDR (spec));
4296 if (!SYMBOLP (value)
4297 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4298 /* If we return here, POSITION has been advanced
4299 across the text with this property. */
4300 return 0;
4301
4302 if (CONSP (XCDR (XCDR (spec))))
4303 {
4304 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4305 int face_id2 = lookup_derived_face (it->f, face_name,
4306 FRINGE_FACE_ID, 0);
4307 if (face_id2 >= 0)
4308 face_id = face_id2;
4309 }
4310
4311 /* Save current settings of IT so that we can restore them
4312 when we are finished with the glyph property value. */
4313
4314 save_pos = it->position;
4315 it->position = *position;
4316 push_it (it);
4317 it->position = save_pos;
4318
4319 it->area = TEXT_AREA;
4320 it->what = IT_IMAGE;
4321 it->image_id = -1; /* no image */
4322 it->position = start_pos;
4323 it->object = NILP (object) ? it->w->buffer : object;
4324 it->method = GET_FROM_IMAGE;
4325 it->from_overlay = Qnil;
4326 it->face_id = face_id;
4327
4328 /* Say that we haven't consumed the characters with
4329 `display' property yet. The call to pop_it in
4330 set_iterator_to_next will clean this up. */
4331 *position = start_pos;
4332
4333 if (EQ (XCAR (spec), Qleft_fringe))
4334 {
4335 it->left_user_fringe_bitmap = fringe_bitmap;
4336 it->left_user_fringe_face_id = face_id;
4337 }
4338 else
4339 {
4340 it->right_user_fringe_bitmap = fringe_bitmap;
4341 it->right_user_fringe_face_id = face_id;
4342 }
4343 #endif /* HAVE_WINDOW_SYSTEM */
4344 return 1;
4345 }
4346
4347 /* Prepare to handle `((margin left-margin) ...)',
4348 `((margin right-margin) ...)' and `((margin nil) ...)'
4349 prefixes for display specifications. */
4350 location = Qunbound;
4351 if (CONSP (spec) && CONSP (XCAR (spec)))
4352 {
4353 Lisp_Object tem;
4354
4355 value = XCDR (spec);
4356 if (CONSP (value))
4357 value = XCAR (value);
4358
4359 tem = XCAR (spec);
4360 if (EQ (XCAR (tem), Qmargin)
4361 && (tem = XCDR (tem),
4362 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4363 (NILP (tem)
4364 || EQ (tem, Qleft_margin)
4365 || EQ (tem, Qright_margin))))
4366 location = tem;
4367 }
4368
4369 if (EQ (location, Qunbound))
4370 {
4371 location = Qnil;
4372 value = spec;
4373 }
4374
4375 /* After this point, VALUE is the property after any
4376 margin prefix has been stripped. It must be a string,
4377 an image specification, or `(space ...)'.
4378
4379 LOCATION specifies where to display: `left-margin',
4380 `right-margin' or nil. */
4381
4382 valid_p = (STRINGP (value)
4383 #ifdef HAVE_WINDOW_SYSTEM
4384 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4385 #endif /* not HAVE_WINDOW_SYSTEM */
4386 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4387
4388 if (valid_p && !display_replaced_before_p)
4389 {
4390 /* Save current settings of IT so that we can restore them
4391 when we are finished with the glyph property value. */
4392 save_pos = it->position;
4393 it->position = *position;
4394 push_it (it);
4395 it->position = save_pos;
4396 it->from_overlay = overlay;
4397
4398 if (NILP (location))
4399 it->area = TEXT_AREA;
4400 else if (EQ (location, Qleft_margin))
4401 it->area = LEFT_MARGIN_AREA;
4402 else
4403 it->area = RIGHT_MARGIN_AREA;
4404
4405 if (STRINGP (value))
4406 {
4407 it->string = value;
4408 it->multibyte_p = STRING_MULTIBYTE (it->string);
4409 it->current.overlay_string_index = -1;
4410 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4411 it->end_charpos = it->string_nchars = SCHARS (it->string);
4412 it->method = GET_FROM_STRING;
4413 it->stop_charpos = 0;
4414 it->string_from_display_prop_p = 1;
4415 /* Say that we haven't consumed the characters with
4416 `display' property yet. The call to pop_it in
4417 set_iterator_to_next will clean this up. */
4418 if (BUFFERP (object))
4419 *position = start_pos;
4420 }
4421 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4422 {
4423 it->method = GET_FROM_STRETCH;
4424 it->object = value;
4425 *position = it->position = start_pos;
4426 }
4427 #ifdef HAVE_WINDOW_SYSTEM
4428 else
4429 {
4430 it->what = IT_IMAGE;
4431 it->image_id = lookup_image (it->f, value);
4432 it->position = start_pos;
4433 it->object = NILP (object) ? it->w->buffer : object;
4434 it->method = GET_FROM_IMAGE;
4435
4436 /* Say that we haven't consumed the characters with
4437 `display' property yet. The call to pop_it in
4438 set_iterator_to_next will clean this up. */
4439 *position = start_pos;
4440 }
4441 #endif /* HAVE_WINDOW_SYSTEM */
4442
4443 return 1;
4444 }
4445
4446 /* Invalid property or property not supported. Restore
4447 POSITION to what it was before. */
4448 *position = start_pos;
4449 return 0;
4450 }
4451
4452
4453 /* Check if SPEC is a display sub-property value whose text should be
4454 treated as intangible. */
4455
4456 static int
4457 single_display_spec_intangible_p (prop)
4458 Lisp_Object prop;
4459 {
4460 /* Skip over `when FORM'. */
4461 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4462 {
4463 prop = XCDR (prop);
4464 if (!CONSP (prop))
4465 return 0;
4466 prop = XCDR (prop);
4467 }
4468
4469 if (STRINGP (prop))
4470 return 1;
4471
4472 if (!CONSP (prop))
4473 return 0;
4474
4475 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4476 we don't need to treat text as intangible. */
4477 if (EQ (XCAR (prop), Qmargin))
4478 {
4479 prop = XCDR (prop);
4480 if (!CONSP (prop))
4481 return 0;
4482
4483 prop = XCDR (prop);
4484 if (!CONSP (prop)
4485 || EQ (XCAR (prop), Qleft_margin)
4486 || EQ (XCAR (prop), Qright_margin))
4487 return 0;
4488 }
4489
4490 return (CONSP (prop)
4491 && (EQ (XCAR (prop), Qimage)
4492 || EQ (XCAR (prop), Qspace)));
4493 }
4494
4495
4496 /* Check if PROP is a display property value whose text should be
4497 treated as intangible. */
4498
4499 int
4500 display_prop_intangible_p (prop)
4501 Lisp_Object prop;
4502 {
4503 if (CONSP (prop)
4504 && CONSP (XCAR (prop))
4505 && !EQ (Qmargin, XCAR (XCAR (prop))))
4506 {
4507 /* A list of sub-properties. */
4508 while (CONSP (prop))
4509 {
4510 if (single_display_spec_intangible_p (XCAR (prop)))
4511 return 1;
4512 prop = XCDR (prop);
4513 }
4514 }
4515 else if (VECTORP (prop))
4516 {
4517 /* A vector of sub-properties. */
4518 int i;
4519 for (i = 0; i < ASIZE (prop); ++i)
4520 if (single_display_spec_intangible_p (AREF (prop, i)))
4521 return 1;
4522 }
4523 else
4524 return single_display_spec_intangible_p (prop);
4525
4526 return 0;
4527 }
4528
4529
4530 /* Return 1 if PROP is a display sub-property value containing STRING. */
4531
4532 static int
4533 single_display_spec_string_p (prop, string)
4534 Lisp_Object prop, string;
4535 {
4536 if (EQ (string, prop))
4537 return 1;
4538
4539 /* Skip over `when FORM'. */
4540 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4541 {
4542 prop = XCDR (prop);
4543 if (!CONSP (prop))
4544 return 0;
4545 prop = XCDR (prop);
4546 }
4547
4548 if (CONSP (prop))
4549 /* Skip over `margin LOCATION'. */
4550 if (EQ (XCAR (prop), Qmargin))
4551 {
4552 prop = XCDR (prop);
4553 if (!CONSP (prop))
4554 return 0;
4555
4556 prop = XCDR (prop);
4557 if (!CONSP (prop))
4558 return 0;
4559 }
4560
4561 return CONSP (prop) && EQ (XCAR (prop), string);
4562 }
4563
4564
4565 /* Return 1 if STRING appears in the `display' property PROP. */
4566
4567 static int
4568 display_prop_string_p (prop, string)
4569 Lisp_Object prop, string;
4570 {
4571 if (CONSP (prop)
4572 && CONSP (XCAR (prop))
4573 && !EQ (Qmargin, XCAR (XCAR (prop))))
4574 {
4575 /* A list of sub-properties. */
4576 while (CONSP (prop))
4577 {
4578 if (single_display_spec_string_p (XCAR (prop), string))
4579 return 1;
4580 prop = XCDR (prop);
4581 }
4582 }
4583 else if (VECTORP (prop))
4584 {
4585 /* A vector of sub-properties. */
4586 int i;
4587 for (i = 0; i < ASIZE (prop); ++i)
4588 if (single_display_spec_string_p (AREF (prop, i), string))
4589 return 1;
4590 }
4591 else
4592 return single_display_spec_string_p (prop, string);
4593
4594 return 0;
4595 }
4596
4597 /* Look for STRING in overlays and text properties in W's buffer,
4598 between character positions FROM and TO (excluding TO).
4599 BACK_P non-zero means look back (in this case, TO is supposed to be
4600 less than FROM).
4601 Value is the first character position where STRING was found, or
4602 zero if it wasn't found before hitting TO.
4603
4604 W's buffer must be current.
4605
4606 This function may only use code that doesn't eval because it is
4607 called asynchronously from note_mouse_highlight. */
4608
4609 static EMACS_INT
4610 string_buffer_position_lim (w, string, from, to, back_p)
4611 struct window *w;
4612 Lisp_Object string;
4613 EMACS_INT from, to;
4614 int back_p;
4615 {
4616 Lisp_Object limit, prop, pos;
4617 int found = 0;
4618
4619 pos = make_number (from);
4620
4621 if (!back_p) /* looking forward */
4622 {
4623 limit = make_number (min (to, ZV));
4624 while (!found && !EQ (pos, limit))
4625 {
4626 prop = Fget_char_property (pos, Qdisplay, Qnil);
4627 if (!NILP (prop) && display_prop_string_p (prop, string))
4628 found = 1;
4629 else
4630 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4631 limit);
4632 }
4633 }
4634 else /* looking back */
4635 {
4636 limit = make_number (max (to, BEGV));
4637 while (!found && !EQ (pos, limit))
4638 {
4639 prop = Fget_char_property (pos, Qdisplay, Qnil);
4640 if (!NILP (prop) && display_prop_string_p (prop, string))
4641 found = 1;
4642 else
4643 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4644 limit);
4645 }
4646 }
4647
4648 return found ? XINT (pos) : 0;
4649 }
4650
4651 /* Determine which buffer position in W's buffer STRING comes from.
4652 AROUND_CHARPOS is an approximate position where it could come from.
4653 Value is the buffer position or 0 if it couldn't be determined.
4654
4655 W's buffer must be current.
4656
4657 This function is necessary because we don't record buffer positions
4658 in glyphs generated from strings (to keep struct glyph small).
4659 This function may only use code that doesn't eval because it is
4660 called asynchronously from note_mouse_highlight. */
4661
4662 EMACS_INT
4663 string_buffer_position (w, string, around_charpos)
4664 struct window *w;
4665 Lisp_Object string;
4666 EMACS_INT around_charpos;
4667 {
4668 Lisp_Object limit, prop, pos;
4669 const int MAX_DISTANCE = 1000;
4670 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4671 around_charpos + MAX_DISTANCE,
4672 0);
4673
4674 if (!found)
4675 found = string_buffer_position_lim (w, string, around_charpos,
4676 around_charpos - MAX_DISTANCE, 1);
4677 return found;
4678 }
4679
4680
4681 \f
4682 /***********************************************************************
4683 `composition' property
4684 ***********************************************************************/
4685
4686 /* Set up iterator IT from `composition' property at its current
4687 position. Called from handle_stop. */
4688
4689 static enum prop_handled
4690 handle_composition_prop (it)
4691 struct it *it;
4692 {
4693 Lisp_Object prop, string;
4694 EMACS_INT pos, pos_byte, start, end;
4695
4696 if (STRINGP (it->string))
4697 {
4698 unsigned char *s;
4699
4700 pos = IT_STRING_CHARPOS (*it);
4701 pos_byte = IT_STRING_BYTEPOS (*it);
4702 string = it->string;
4703 s = SDATA (string) + pos_byte;
4704 it->c = STRING_CHAR (s);
4705 }
4706 else
4707 {
4708 pos = IT_CHARPOS (*it);
4709 pos_byte = IT_BYTEPOS (*it);
4710 string = Qnil;
4711 it->c = FETCH_CHAR (pos_byte);
4712 }
4713
4714 /* If there's a valid composition and point is not inside of the
4715 composition (in the case that the composition is from the current
4716 buffer), draw a glyph composed from the composition components. */
4717 if (find_composition (pos, -1, &start, &end, &prop, string)
4718 && COMPOSITION_VALID_P (start, end, prop)
4719 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4720 {
4721 if (start != pos)
4722 {
4723 if (STRINGP (it->string))
4724 pos_byte = string_char_to_byte (it->string, start);
4725 else
4726 pos_byte = CHAR_TO_BYTE (start);
4727 }
4728 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4729 prop, string);
4730
4731 if (it->cmp_it.id >= 0)
4732 {
4733 it->cmp_it.ch = -1;
4734 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4735 it->cmp_it.nglyphs = -1;
4736 }
4737 }
4738
4739 return HANDLED_NORMALLY;
4740 }
4741
4742
4743 \f
4744 /***********************************************************************
4745 Overlay strings
4746 ***********************************************************************/
4747
4748 /* The following structure is used to record overlay strings for
4749 later sorting in load_overlay_strings. */
4750
4751 struct overlay_entry
4752 {
4753 Lisp_Object overlay;
4754 Lisp_Object string;
4755 int priority;
4756 int after_string_p;
4757 };
4758
4759
4760 /* Set up iterator IT from overlay strings at its current position.
4761 Called from handle_stop. */
4762
4763 static enum prop_handled
4764 handle_overlay_change (it)
4765 struct it *it;
4766 {
4767 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4768 return HANDLED_RECOMPUTE_PROPS;
4769 else
4770 return HANDLED_NORMALLY;
4771 }
4772
4773
4774 /* Set up the next overlay string for delivery by IT, if there is an
4775 overlay string to deliver. Called by set_iterator_to_next when the
4776 end of the current overlay string is reached. If there are more
4777 overlay strings to display, IT->string and
4778 IT->current.overlay_string_index are set appropriately here.
4779 Otherwise IT->string is set to nil. */
4780
4781 static void
4782 next_overlay_string (it)
4783 struct it *it;
4784 {
4785 ++it->current.overlay_string_index;
4786 if (it->current.overlay_string_index == it->n_overlay_strings)
4787 {
4788 /* No more overlay strings. Restore IT's settings to what
4789 they were before overlay strings were processed, and
4790 continue to deliver from current_buffer. */
4791
4792 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4793 pop_it (it);
4794 xassert (it->sp > 0
4795 || (NILP (it->string)
4796 && it->method == GET_FROM_BUFFER
4797 && it->stop_charpos >= BEGV
4798 && it->stop_charpos <= it->end_charpos));
4799 it->current.overlay_string_index = -1;
4800 it->n_overlay_strings = 0;
4801
4802 /* If we're at the end of the buffer, record that we have
4803 processed the overlay strings there already, so that
4804 next_element_from_buffer doesn't try it again. */
4805 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4806 it->overlay_strings_at_end_processed_p = 1;
4807 }
4808 else
4809 {
4810 /* There are more overlay strings to process. If
4811 IT->current.overlay_string_index has advanced to a position
4812 where we must load IT->overlay_strings with more strings, do
4813 it. */
4814 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4815
4816 if (it->current.overlay_string_index && i == 0)
4817 load_overlay_strings (it, 0);
4818
4819 /* Initialize IT to deliver display elements from the overlay
4820 string. */
4821 it->string = it->overlay_strings[i];
4822 it->multibyte_p = STRING_MULTIBYTE (it->string);
4823 SET_TEXT_POS (it->current.string_pos, 0, 0);
4824 it->method = GET_FROM_STRING;
4825 it->stop_charpos = 0;
4826 if (it->cmp_it.stop_pos >= 0)
4827 it->cmp_it.stop_pos = 0;
4828 }
4829
4830 CHECK_IT (it);
4831 }
4832
4833
4834 /* Compare two overlay_entry structures E1 and E2. Used as a
4835 comparison function for qsort in load_overlay_strings. Overlay
4836 strings for the same position are sorted so that
4837
4838 1. All after-strings come in front of before-strings, except
4839 when they come from the same overlay.
4840
4841 2. Within after-strings, strings are sorted so that overlay strings
4842 from overlays with higher priorities come first.
4843
4844 2. Within before-strings, strings are sorted so that overlay
4845 strings from overlays with higher priorities come last.
4846
4847 Value is analogous to strcmp. */
4848
4849
4850 static int
4851 compare_overlay_entries (e1, e2)
4852 void *e1, *e2;
4853 {
4854 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4855 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4856 int result;
4857
4858 if (entry1->after_string_p != entry2->after_string_p)
4859 {
4860 /* Let after-strings appear in front of before-strings if
4861 they come from different overlays. */
4862 if (EQ (entry1->overlay, entry2->overlay))
4863 result = entry1->after_string_p ? 1 : -1;
4864 else
4865 result = entry1->after_string_p ? -1 : 1;
4866 }
4867 else if (entry1->after_string_p)
4868 /* After-strings sorted in order of decreasing priority. */
4869 result = entry2->priority - entry1->priority;
4870 else
4871 /* Before-strings sorted in order of increasing priority. */
4872 result = entry1->priority - entry2->priority;
4873
4874 return result;
4875 }
4876
4877
4878 /* Load the vector IT->overlay_strings with overlay strings from IT's
4879 current buffer position, or from CHARPOS if that is > 0. Set
4880 IT->n_overlays to the total number of overlay strings found.
4881
4882 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4883 a time. On entry into load_overlay_strings,
4884 IT->current.overlay_string_index gives the number of overlay
4885 strings that have already been loaded by previous calls to this
4886 function.
4887
4888 IT->add_overlay_start contains an additional overlay start
4889 position to consider for taking overlay strings from, if non-zero.
4890 This position comes into play when the overlay has an `invisible'
4891 property, and both before and after-strings. When we've skipped to
4892 the end of the overlay, because of its `invisible' property, we
4893 nevertheless want its before-string to appear.
4894 IT->add_overlay_start will contain the overlay start position
4895 in this case.
4896
4897 Overlay strings are sorted so that after-string strings come in
4898 front of before-string strings. Within before and after-strings,
4899 strings are sorted by overlay priority. See also function
4900 compare_overlay_entries. */
4901
4902 static void
4903 load_overlay_strings (it, charpos)
4904 struct it *it;
4905 int charpos;
4906 {
4907 extern Lisp_Object Qwindow, Qpriority;
4908 Lisp_Object overlay, window, str, invisible;
4909 struct Lisp_Overlay *ov;
4910 int start, end;
4911 int size = 20;
4912 int n = 0, i, j, invis_p;
4913 struct overlay_entry *entries
4914 = (struct overlay_entry *) alloca (size * sizeof *entries);
4915
4916 if (charpos <= 0)
4917 charpos = IT_CHARPOS (*it);
4918
4919 /* Append the overlay string STRING of overlay OVERLAY to vector
4920 `entries' which has size `size' and currently contains `n'
4921 elements. AFTER_P non-zero means STRING is an after-string of
4922 OVERLAY. */
4923 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4924 do \
4925 { \
4926 Lisp_Object priority; \
4927 \
4928 if (n == size) \
4929 { \
4930 int new_size = 2 * size; \
4931 struct overlay_entry *old = entries; \
4932 entries = \
4933 (struct overlay_entry *) alloca (new_size \
4934 * sizeof *entries); \
4935 bcopy (old, entries, size * sizeof *entries); \
4936 size = new_size; \
4937 } \
4938 \
4939 entries[n].string = (STRING); \
4940 entries[n].overlay = (OVERLAY); \
4941 priority = Foverlay_get ((OVERLAY), Qpriority); \
4942 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4943 entries[n].after_string_p = (AFTER_P); \
4944 ++n; \
4945 } \
4946 while (0)
4947
4948 /* Process overlay before the overlay center. */
4949 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4950 {
4951 XSETMISC (overlay, ov);
4952 xassert (OVERLAYP (overlay));
4953 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4954 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4955
4956 if (end < charpos)
4957 break;
4958
4959 /* Skip this overlay if it doesn't start or end at IT's current
4960 position. */
4961 if (end != charpos && start != charpos)
4962 continue;
4963
4964 /* Skip this overlay if it doesn't apply to IT->w. */
4965 window = Foverlay_get (overlay, Qwindow);
4966 if (WINDOWP (window) && XWINDOW (window) != it->w)
4967 continue;
4968
4969 /* If the text ``under'' the overlay is invisible, both before-
4970 and after-strings from this overlay are visible; start and
4971 end position are indistinguishable. */
4972 invisible = Foverlay_get (overlay, Qinvisible);
4973 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4974
4975 /* If overlay has a non-empty before-string, record it. */
4976 if ((start == charpos || (end == charpos && invis_p))
4977 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4978 && SCHARS (str))
4979 RECORD_OVERLAY_STRING (overlay, str, 0);
4980
4981 /* If overlay has a non-empty after-string, record it. */
4982 if ((end == charpos || (start == charpos && invis_p))
4983 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4984 && SCHARS (str))
4985 RECORD_OVERLAY_STRING (overlay, str, 1);
4986 }
4987
4988 /* Process overlays after the overlay center. */
4989 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4990 {
4991 XSETMISC (overlay, ov);
4992 xassert (OVERLAYP (overlay));
4993 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4994 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4995
4996 if (start > charpos)
4997 break;
4998
4999 /* Skip this overlay if it doesn't start or end at IT's current
5000 position. */
5001 if (end != charpos && start != charpos)
5002 continue;
5003
5004 /* Skip this overlay if it doesn't apply to IT->w. */
5005 window = Foverlay_get (overlay, Qwindow);
5006 if (WINDOWP (window) && XWINDOW (window) != it->w)
5007 continue;
5008
5009 /* If the text ``under'' the overlay is invisible, it has a zero
5010 dimension, and both before- and after-strings apply. */
5011 invisible = Foverlay_get (overlay, Qinvisible);
5012 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5013
5014 /* If overlay has a non-empty before-string, record it. */
5015 if ((start == charpos || (end == charpos && invis_p))
5016 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5017 && SCHARS (str))
5018 RECORD_OVERLAY_STRING (overlay, str, 0);
5019
5020 /* If overlay has a non-empty after-string, record it. */
5021 if ((end == charpos || (start == charpos && invis_p))
5022 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5023 && SCHARS (str))
5024 RECORD_OVERLAY_STRING (overlay, str, 1);
5025 }
5026
5027 #undef RECORD_OVERLAY_STRING
5028
5029 /* Sort entries. */
5030 if (n > 1)
5031 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5032
5033 /* Record the total number of strings to process. */
5034 it->n_overlay_strings = n;
5035
5036 /* IT->current.overlay_string_index is the number of overlay strings
5037 that have already been consumed by IT. Copy some of the
5038 remaining overlay strings to IT->overlay_strings. */
5039 i = 0;
5040 j = it->current.overlay_string_index;
5041 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5042 {
5043 it->overlay_strings[i] = entries[j].string;
5044 it->string_overlays[i++] = entries[j++].overlay;
5045 }
5046
5047 CHECK_IT (it);
5048 }
5049
5050
5051 /* Get the first chunk of overlay strings at IT's current buffer
5052 position, or at CHARPOS if that is > 0. Value is non-zero if at
5053 least one overlay string was found. */
5054
5055 static int
5056 get_overlay_strings_1 (it, charpos, compute_stop_p)
5057 struct it *it;
5058 int charpos;
5059 int compute_stop_p;
5060 {
5061 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5062 process. This fills IT->overlay_strings with strings, and sets
5063 IT->n_overlay_strings to the total number of strings to process.
5064 IT->pos.overlay_string_index has to be set temporarily to zero
5065 because load_overlay_strings needs this; it must be set to -1
5066 when no overlay strings are found because a zero value would
5067 indicate a position in the first overlay string. */
5068 it->current.overlay_string_index = 0;
5069 load_overlay_strings (it, charpos);
5070
5071 /* If we found overlay strings, set up IT to deliver display
5072 elements from the first one. Otherwise set up IT to deliver
5073 from current_buffer. */
5074 if (it->n_overlay_strings)
5075 {
5076 /* Make sure we know settings in current_buffer, so that we can
5077 restore meaningful values when we're done with the overlay
5078 strings. */
5079 if (compute_stop_p)
5080 compute_stop_pos (it);
5081 xassert (it->face_id >= 0);
5082
5083 /* Save IT's settings. They are restored after all overlay
5084 strings have been processed. */
5085 xassert (!compute_stop_p || it->sp == 0);
5086
5087 /* When called from handle_stop, there might be an empty display
5088 string loaded. In that case, don't bother saving it. */
5089 if (!STRINGP (it->string) || SCHARS (it->string))
5090 push_it (it);
5091
5092 /* Set up IT to deliver display elements from the first overlay
5093 string. */
5094 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5095 it->string = it->overlay_strings[0];
5096 it->from_overlay = Qnil;
5097 it->stop_charpos = 0;
5098 xassert (STRINGP (it->string));
5099 it->end_charpos = SCHARS (it->string);
5100 it->multibyte_p = STRING_MULTIBYTE (it->string);
5101 it->method = GET_FROM_STRING;
5102 return 1;
5103 }
5104
5105 it->current.overlay_string_index = -1;
5106 return 0;
5107 }
5108
5109 static int
5110 get_overlay_strings (it, charpos)
5111 struct it *it;
5112 int charpos;
5113 {
5114 it->string = Qnil;
5115 it->method = GET_FROM_BUFFER;
5116
5117 (void) get_overlay_strings_1 (it, charpos, 1);
5118
5119 CHECK_IT (it);
5120
5121 /* Value is non-zero if we found at least one overlay string. */
5122 return STRINGP (it->string);
5123 }
5124
5125
5126 \f
5127 /***********************************************************************
5128 Saving and restoring state
5129 ***********************************************************************/
5130
5131 /* Save current settings of IT on IT->stack. Called, for example,
5132 before setting up IT for an overlay string, to be able to restore
5133 IT's settings to what they were after the overlay string has been
5134 processed. */
5135
5136 static void
5137 push_it (it)
5138 struct it *it;
5139 {
5140 struct iterator_stack_entry *p;
5141
5142 xassert (it->sp < IT_STACK_SIZE);
5143 p = it->stack + it->sp;
5144
5145 p->stop_charpos = it->stop_charpos;
5146 p->prev_stop = it->prev_stop;
5147 p->base_level_stop = it->base_level_stop;
5148 p->cmp_it = it->cmp_it;
5149 xassert (it->face_id >= 0);
5150 p->face_id = it->face_id;
5151 p->string = it->string;
5152 p->method = it->method;
5153 p->from_overlay = it->from_overlay;
5154 switch (p->method)
5155 {
5156 case GET_FROM_IMAGE:
5157 p->u.image.object = it->object;
5158 p->u.image.image_id = it->image_id;
5159 p->u.image.slice = it->slice;
5160 break;
5161 case GET_FROM_STRETCH:
5162 p->u.stretch.object = it->object;
5163 break;
5164 }
5165 p->position = it->position;
5166 p->current = it->current;
5167 p->end_charpos = it->end_charpos;
5168 p->string_nchars = it->string_nchars;
5169 p->area = it->area;
5170 p->multibyte_p = it->multibyte_p;
5171 p->avoid_cursor_p = it->avoid_cursor_p;
5172 p->space_width = it->space_width;
5173 p->font_height = it->font_height;
5174 p->voffset = it->voffset;
5175 p->string_from_display_prop_p = it->string_from_display_prop_p;
5176 p->display_ellipsis_p = 0;
5177 p->line_wrap = it->line_wrap;
5178 ++it->sp;
5179 }
5180
5181
5182 /* Restore IT's settings from IT->stack. Called, for example, when no
5183 more overlay strings must be processed, and we return to delivering
5184 display elements from a buffer, or when the end of a string from a
5185 `display' property is reached and we return to delivering display
5186 elements from an overlay string, or from a buffer. */
5187
5188 static void
5189 pop_it (it)
5190 struct it *it;
5191 {
5192 struct iterator_stack_entry *p;
5193
5194 xassert (it->sp > 0);
5195 --it->sp;
5196 p = it->stack + it->sp;
5197 it->stop_charpos = p->stop_charpos;
5198 it->prev_stop = p->prev_stop;
5199 it->base_level_stop = p->base_level_stop;
5200 it->cmp_it = p->cmp_it;
5201 it->face_id = p->face_id;
5202 it->current = p->current;
5203 it->position = p->position;
5204 it->string = p->string;
5205 it->from_overlay = p->from_overlay;
5206 if (NILP (it->string))
5207 SET_TEXT_POS (it->current.string_pos, -1, -1);
5208 it->method = p->method;
5209 switch (it->method)
5210 {
5211 case GET_FROM_IMAGE:
5212 it->image_id = p->u.image.image_id;
5213 it->object = p->u.image.object;
5214 it->slice = p->u.image.slice;
5215 break;
5216 case GET_FROM_STRETCH:
5217 it->object = p->u.comp.object;
5218 break;
5219 case GET_FROM_BUFFER:
5220 it->object = it->w->buffer;
5221 break;
5222 case GET_FROM_STRING:
5223 it->object = it->string;
5224 break;
5225 case GET_FROM_DISPLAY_VECTOR:
5226 if (it->s)
5227 it->method = GET_FROM_C_STRING;
5228 else if (STRINGP (it->string))
5229 it->method = GET_FROM_STRING;
5230 else
5231 {
5232 it->method = GET_FROM_BUFFER;
5233 it->object = it->w->buffer;
5234 }
5235 }
5236 it->end_charpos = p->end_charpos;
5237 it->string_nchars = p->string_nchars;
5238 it->area = p->area;
5239 it->multibyte_p = p->multibyte_p;
5240 it->avoid_cursor_p = p->avoid_cursor_p;
5241 it->space_width = p->space_width;
5242 it->font_height = p->font_height;
5243 it->voffset = p->voffset;
5244 it->string_from_display_prop_p = p->string_from_display_prop_p;
5245 it->line_wrap = p->line_wrap;
5246 }
5247
5248
5249 \f
5250 /***********************************************************************
5251 Moving over lines
5252 ***********************************************************************/
5253
5254 /* Set IT's current position to the previous line start. */
5255
5256 static void
5257 back_to_previous_line_start (it)
5258 struct it *it;
5259 {
5260 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5261 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5262 }
5263
5264
5265 /* Move IT to the next line start.
5266
5267 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5268 we skipped over part of the text (as opposed to moving the iterator
5269 continuously over the text). Otherwise, don't change the value
5270 of *SKIPPED_P.
5271
5272 Newlines may come from buffer text, overlay strings, or strings
5273 displayed via the `display' property. That's the reason we can't
5274 simply use find_next_newline_no_quit.
5275
5276 Note that this function may not skip over invisible text that is so
5277 because of text properties and immediately follows a newline. If
5278 it would, function reseat_at_next_visible_line_start, when called
5279 from set_iterator_to_next, would effectively make invisible
5280 characters following a newline part of the wrong glyph row, which
5281 leads to wrong cursor motion. */
5282
5283 static int
5284 forward_to_next_line_start (it, skipped_p)
5285 struct it *it;
5286 int *skipped_p;
5287 {
5288 int old_selective, newline_found_p, n;
5289 const int MAX_NEWLINE_DISTANCE = 500;
5290
5291 /* If already on a newline, just consume it to avoid unintended
5292 skipping over invisible text below. */
5293 if (it->what == IT_CHARACTER
5294 && it->c == '\n'
5295 && CHARPOS (it->position) == IT_CHARPOS (*it))
5296 {
5297 set_iterator_to_next (it, 0);
5298 it->c = 0;
5299 return 1;
5300 }
5301
5302 /* Don't handle selective display in the following. It's (a)
5303 unnecessary because it's done by the caller, and (b) leads to an
5304 infinite recursion because next_element_from_ellipsis indirectly
5305 calls this function. */
5306 old_selective = it->selective;
5307 it->selective = 0;
5308
5309 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5310 from buffer text. */
5311 for (n = newline_found_p = 0;
5312 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5313 n += STRINGP (it->string) ? 0 : 1)
5314 {
5315 if (!get_next_display_element (it))
5316 return 0;
5317 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5318 set_iterator_to_next (it, 0);
5319 }
5320
5321 /* If we didn't find a newline near enough, see if we can use a
5322 short-cut. */
5323 if (!newline_found_p)
5324 {
5325 int start = IT_CHARPOS (*it);
5326 int limit = find_next_newline_no_quit (start, 1);
5327 Lisp_Object pos;
5328
5329 xassert (!STRINGP (it->string));
5330
5331 /* If there isn't any `display' property in sight, and no
5332 overlays, we can just use the position of the newline in
5333 buffer text. */
5334 if (it->stop_charpos >= limit
5335 || ((pos = Fnext_single_property_change (make_number (start),
5336 Qdisplay,
5337 Qnil, make_number (limit)),
5338 NILP (pos))
5339 && next_overlay_change (start) == ZV))
5340 {
5341 IT_CHARPOS (*it) = limit;
5342 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5343 *skipped_p = newline_found_p = 1;
5344 }
5345 else
5346 {
5347 while (get_next_display_element (it)
5348 && !newline_found_p)
5349 {
5350 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5351 set_iterator_to_next (it, 0);
5352 }
5353 }
5354 }
5355
5356 it->selective = old_selective;
5357 return newline_found_p;
5358 }
5359
5360
5361 /* Set IT's current position to the previous visible line start. Skip
5362 invisible text that is so either due to text properties or due to
5363 selective display. Caution: this does not change IT->current_x and
5364 IT->hpos. */
5365
5366 static void
5367 back_to_previous_visible_line_start (it)
5368 struct it *it;
5369 {
5370 while (IT_CHARPOS (*it) > BEGV)
5371 {
5372 back_to_previous_line_start (it);
5373
5374 if (IT_CHARPOS (*it) <= BEGV)
5375 break;
5376
5377 /* If selective > 0, then lines indented more than its value are
5378 invisible. */
5379 if (it->selective > 0
5380 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5381 (double) it->selective)) /* iftc */
5382 continue;
5383
5384 /* Check the newline before point for invisibility. */
5385 {
5386 Lisp_Object prop;
5387 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5388 Qinvisible, it->window);
5389 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5390 continue;
5391 }
5392
5393 if (IT_CHARPOS (*it) <= BEGV)
5394 break;
5395
5396 {
5397 struct it it2;
5398 int pos;
5399 EMACS_INT beg, end;
5400 Lisp_Object val, overlay;
5401
5402 /* If newline is part of a composition, continue from start of composition */
5403 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5404 && beg < IT_CHARPOS (*it))
5405 goto replaced;
5406
5407 /* If newline is replaced by a display property, find start of overlay
5408 or interval and continue search from that point. */
5409 it2 = *it;
5410 pos = --IT_CHARPOS (it2);
5411 --IT_BYTEPOS (it2);
5412 it2.sp = 0;
5413 it2.string_from_display_prop_p = 0;
5414 if (handle_display_prop (&it2) == HANDLED_RETURN
5415 && !NILP (val = get_char_property_and_overlay
5416 (make_number (pos), Qdisplay, Qnil, &overlay))
5417 && (OVERLAYP (overlay)
5418 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5419 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5420 goto replaced;
5421
5422 /* Newline is not replaced by anything -- so we are done. */
5423 break;
5424
5425 replaced:
5426 if (beg < BEGV)
5427 beg = BEGV;
5428 IT_CHARPOS (*it) = beg;
5429 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5430 }
5431 }
5432
5433 it->continuation_lines_width = 0;
5434
5435 xassert (IT_CHARPOS (*it) >= BEGV);
5436 xassert (IT_CHARPOS (*it) == BEGV
5437 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5438 CHECK_IT (it);
5439 }
5440
5441
5442 /* Reseat iterator IT at the previous visible line start. Skip
5443 invisible text that is so either due to text properties or due to
5444 selective display. At the end, update IT's overlay information,
5445 face information etc. */
5446
5447 void
5448 reseat_at_previous_visible_line_start (it)
5449 struct it *it;
5450 {
5451 back_to_previous_visible_line_start (it);
5452 reseat (it, it->current.pos, 1);
5453 CHECK_IT (it);
5454 }
5455
5456
5457 /* Reseat iterator IT on the next visible line start in the current
5458 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5459 preceding the line start. Skip over invisible text that is so
5460 because of selective display. Compute faces, overlays etc at the
5461 new position. Note that this function does not skip over text that
5462 is invisible because of text properties. */
5463
5464 static void
5465 reseat_at_next_visible_line_start (it, on_newline_p)
5466 struct it *it;
5467 int on_newline_p;
5468 {
5469 int newline_found_p, skipped_p = 0;
5470
5471 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5472
5473 /* Skip over lines that are invisible because they are indented
5474 more than the value of IT->selective. */
5475 if (it->selective > 0)
5476 while (IT_CHARPOS (*it) < ZV
5477 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5478 (double) it->selective)) /* iftc */
5479 {
5480 xassert (IT_BYTEPOS (*it) == BEGV
5481 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5482 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5483 }
5484
5485 /* Position on the newline if that's what's requested. */
5486 if (on_newline_p && newline_found_p)
5487 {
5488 if (STRINGP (it->string))
5489 {
5490 if (IT_STRING_CHARPOS (*it) > 0)
5491 {
5492 --IT_STRING_CHARPOS (*it);
5493 --IT_STRING_BYTEPOS (*it);
5494 }
5495 }
5496 else if (IT_CHARPOS (*it) > BEGV)
5497 {
5498 --IT_CHARPOS (*it);
5499 --IT_BYTEPOS (*it);
5500 reseat (it, it->current.pos, 0);
5501 }
5502 }
5503 else if (skipped_p)
5504 reseat (it, it->current.pos, 0);
5505
5506 CHECK_IT (it);
5507 }
5508
5509
5510 \f
5511 /***********************************************************************
5512 Changing an iterator's position
5513 ***********************************************************************/
5514
5515 /* Change IT's current position to POS in current_buffer. If FORCE_P
5516 is non-zero, always check for text properties at the new position.
5517 Otherwise, text properties are only looked up if POS >=
5518 IT->check_charpos of a property. */
5519
5520 static void
5521 reseat (it, pos, force_p)
5522 struct it *it;
5523 struct text_pos pos;
5524 int force_p;
5525 {
5526 int original_pos = IT_CHARPOS (*it);
5527
5528 reseat_1 (it, pos, 0);
5529
5530 /* Determine where to check text properties. Avoid doing it
5531 where possible because text property lookup is very expensive. */
5532 if (force_p
5533 || CHARPOS (pos) > it->stop_charpos
5534 || CHARPOS (pos) < original_pos)
5535 handle_stop (it);
5536
5537 CHECK_IT (it);
5538 }
5539
5540
5541 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5542 IT->stop_pos to POS, also. */
5543
5544 static void
5545 reseat_1 (it, pos, set_stop_p)
5546 struct it *it;
5547 struct text_pos pos;
5548 int set_stop_p;
5549 {
5550 /* Don't call this function when scanning a C string. */
5551 xassert (it->s == NULL);
5552
5553 /* POS must be a reasonable value. */
5554 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5555
5556 it->current.pos = it->position = pos;
5557 it->end_charpos = ZV;
5558 it->dpvec = NULL;
5559 it->current.dpvec_index = -1;
5560 it->current.overlay_string_index = -1;
5561 IT_STRING_CHARPOS (*it) = -1;
5562 IT_STRING_BYTEPOS (*it) = -1;
5563 it->string = Qnil;
5564 it->string_from_display_prop_p = 0;
5565 it->method = GET_FROM_BUFFER;
5566 it->object = it->w->buffer;
5567 it->area = TEXT_AREA;
5568 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5569 it->sp = 0;
5570 it->string_from_display_prop_p = 0;
5571 it->face_before_selective_p = 0;
5572 if (it->bidi_p)
5573 it->bidi_it.first_elt = 1;
5574
5575 if (set_stop_p)
5576 {
5577 it->stop_charpos = CHARPOS (pos);
5578 it->base_level_stop = CHARPOS (pos);
5579 }
5580 }
5581
5582
5583 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5584 If S is non-null, it is a C string to iterate over. Otherwise,
5585 STRING gives a Lisp string to iterate over.
5586
5587 If PRECISION > 0, don't return more then PRECISION number of
5588 characters from the string.
5589
5590 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5591 characters have been returned. FIELD_WIDTH < 0 means an infinite
5592 field width.
5593
5594 MULTIBYTE = 0 means disable processing of multibyte characters,
5595 MULTIBYTE > 0 means enable it,
5596 MULTIBYTE < 0 means use IT->multibyte_p.
5597
5598 IT must be initialized via a prior call to init_iterator before
5599 calling this function. */
5600
5601 static void
5602 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5603 struct it *it;
5604 unsigned char *s;
5605 Lisp_Object string;
5606 int charpos;
5607 int precision, field_width, multibyte;
5608 {
5609 /* No region in strings. */
5610 it->region_beg_charpos = it->region_end_charpos = -1;
5611
5612 /* No text property checks performed by default, but see below. */
5613 it->stop_charpos = -1;
5614
5615 /* Set iterator position and end position. */
5616 bzero (&it->current, sizeof it->current);
5617 it->current.overlay_string_index = -1;
5618 it->current.dpvec_index = -1;
5619 xassert (charpos >= 0);
5620
5621 /* If STRING is specified, use its multibyteness, otherwise use the
5622 setting of MULTIBYTE, if specified. */
5623 if (multibyte >= 0)
5624 it->multibyte_p = multibyte > 0;
5625
5626 if (s == NULL)
5627 {
5628 xassert (STRINGP (string));
5629 it->string = string;
5630 it->s = NULL;
5631 it->end_charpos = it->string_nchars = SCHARS (string);
5632 it->method = GET_FROM_STRING;
5633 it->current.string_pos = string_pos (charpos, string);
5634 }
5635 else
5636 {
5637 it->s = s;
5638 it->string = Qnil;
5639
5640 /* Note that we use IT->current.pos, not it->current.string_pos,
5641 for displaying C strings. */
5642 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5643 if (it->multibyte_p)
5644 {
5645 it->current.pos = c_string_pos (charpos, s, 1);
5646 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5647 }
5648 else
5649 {
5650 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5651 it->end_charpos = it->string_nchars = strlen (s);
5652 }
5653
5654 it->method = GET_FROM_C_STRING;
5655 }
5656
5657 /* PRECISION > 0 means don't return more than PRECISION characters
5658 from the string. */
5659 if (precision > 0 && it->end_charpos - charpos > precision)
5660 it->end_charpos = it->string_nchars = charpos + precision;
5661
5662 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5663 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5664 FIELD_WIDTH < 0 means infinite field width. This is useful for
5665 padding with `-' at the end of a mode line. */
5666 if (field_width < 0)
5667 field_width = INFINITY;
5668 if (field_width > it->end_charpos - charpos)
5669 it->end_charpos = charpos + field_width;
5670
5671 /* Use the standard display table for displaying strings. */
5672 if (DISP_TABLE_P (Vstandard_display_table))
5673 it->dp = XCHAR_TABLE (Vstandard_display_table);
5674
5675 it->stop_charpos = charpos;
5676 CHECK_IT (it);
5677 }
5678
5679
5680 \f
5681 /***********************************************************************
5682 Iteration
5683 ***********************************************************************/
5684
5685 /* Map enum it_method value to corresponding next_element_from_* function. */
5686
5687 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5688 {
5689 next_element_from_buffer,
5690 next_element_from_display_vector,
5691 next_element_from_string,
5692 next_element_from_c_string,
5693 next_element_from_image,
5694 next_element_from_stretch
5695 };
5696
5697 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5698
5699
5700 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5701 (possibly with the following characters). */
5702
5703 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS) \
5704 ((IT)->cmp_it.id >= 0 \
5705 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5706 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5707 (IT)->end_charpos, (IT)->w, \
5708 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5709 (IT)->string)))
5710
5711
5712 /* Load IT's display element fields with information about the next
5713 display element from the current position of IT. Value is zero if
5714 end of buffer (or C string) is reached. */
5715
5716 static struct frame *last_escape_glyph_frame = NULL;
5717 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5718 static int last_escape_glyph_merged_face_id = 0;
5719
5720 int
5721 get_next_display_element (it)
5722 struct it *it;
5723 {
5724 /* Non-zero means that we found a display element. Zero means that
5725 we hit the end of what we iterate over. Performance note: the
5726 function pointer `method' used here turns out to be faster than
5727 using a sequence of if-statements. */
5728 int success_p;
5729
5730 get_next:
5731 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5732
5733 if (it->what == IT_CHARACTER)
5734 {
5735 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5736 and only if (a) the resolved directionality of that character
5737 is R..." */
5738 /* FIXME: Do we need an exception for characters from display
5739 tables? */
5740 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5741 it->c = bidi_mirror_char (it->c);
5742 /* Map via display table or translate control characters.
5743 IT->c, IT->len etc. have been set to the next character by
5744 the function call above. If we have a display table, and it
5745 contains an entry for IT->c, translate it. Don't do this if
5746 IT->c itself comes from a display table, otherwise we could
5747 end up in an infinite recursion. (An alternative could be to
5748 count the recursion depth of this function and signal an
5749 error when a certain maximum depth is reached.) Is it worth
5750 it? */
5751 if (success_p && it->dpvec == NULL)
5752 {
5753 Lisp_Object dv;
5754 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5755 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5756 nbsp_or_shy = char_is_other;
5757 int decoded = it->c;
5758
5759 if (it->dp
5760 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5761 VECTORP (dv)))
5762 {
5763 struct Lisp_Vector *v = XVECTOR (dv);
5764
5765 /* Return the first character from the display table
5766 entry, if not empty. If empty, don't display the
5767 current character. */
5768 if (v->size)
5769 {
5770 it->dpvec_char_len = it->len;
5771 it->dpvec = v->contents;
5772 it->dpend = v->contents + v->size;
5773 it->current.dpvec_index = 0;
5774 it->dpvec_face_id = -1;
5775 it->saved_face_id = it->face_id;
5776 it->method = GET_FROM_DISPLAY_VECTOR;
5777 it->ellipsis_p = 0;
5778 }
5779 else
5780 {
5781 set_iterator_to_next (it, 0);
5782 }
5783 goto get_next;
5784 }
5785
5786 if (unibyte_display_via_language_environment
5787 && !ASCII_CHAR_P (it->c))
5788 decoded = DECODE_CHAR (unibyte, it->c);
5789
5790 if (it->c >= 0x80 && ! NILP (Vnobreak_char_display))
5791 {
5792 if (it->multibyte_p)
5793 nbsp_or_shy = (it->c == 0xA0 ? char_is_nbsp
5794 : it->c == 0xAD ? char_is_soft_hyphen
5795 : char_is_other);
5796 else if (unibyte_display_via_language_environment)
5797 nbsp_or_shy = (decoded == 0xA0 ? char_is_nbsp
5798 : decoded == 0xAD ? char_is_soft_hyphen
5799 : char_is_other);
5800 }
5801
5802 /* Translate control characters into `\003' or `^C' form.
5803 Control characters coming from a display table entry are
5804 currently not translated because we use IT->dpvec to hold
5805 the translation. This could easily be changed but I
5806 don't believe that it is worth doing.
5807
5808 If it->multibyte_p is nonzero, non-printable non-ASCII
5809 characters are also translated to octal form.
5810
5811 If it->multibyte_p is zero, eight-bit characters that
5812 don't have corresponding multibyte char code are also
5813 translated to octal form. */
5814 if ((it->c < ' '
5815 ? (it->area != TEXT_AREA
5816 /* In mode line, treat \n, \t like other crl chars. */
5817 || (it->c != '\t'
5818 && it->glyph_row
5819 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5820 || (it->c != '\n' && it->c != '\t'))
5821 : (nbsp_or_shy
5822 || (it->multibyte_p
5823 ? ! CHAR_PRINTABLE_P (it->c)
5824 : (! unibyte_display_via_language_environment
5825 ? it->c >= 0x80
5826 : (decoded >= 0x80 && decoded < 0xA0))))))
5827 {
5828 /* IT->c is a control character which must be displayed
5829 either as '\003' or as `^C' where the '\\' and '^'
5830 can be defined in the display table. Fill
5831 IT->ctl_chars with glyphs for what we have to
5832 display. Then, set IT->dpvec to these glyphs. */
5833 Lisp_Object gc;
5834 int ctl_len;
5835 int face_id, lface_id = 0 ;
5836 int escape_glyph;
5837
5838 /* Handle control characters with ^. */
5839
5840 if (it->c < 128 && it->ctl_arrow_p)
5841 {
5842 int g;
5843
5844 g = '^'; /* default glyph for Control */
5845 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5846 if (it->dp
5847 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5848 && GLYPH_CODE_CHAR_VALID_P (gc))
5849 {
5850 g = GLYPH_CODE_CHAR (gc);
5851 lface_id = GLYPH_CODE_FACE (gc);
5852 }
5853 if (lface_id)
5854 {
5855 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5856 }
5857 else if (it->f == last_escape_glyph_frame
5858 && it->face_id == last_escape_glyph_face_id)
5859 {
5860 face_id = last_escape_glyph_merged_face_id;
5861 }
5862 else
5863 {
5864 /* Merge the escape-glyph face into the current face. */
5865 face_id = merge_faces (it->f, Qescape_glyph, 0,
5866 it->face_id);
5867 last_escape_glyph_frame = it->f;
5868 last_escape_glyph_face_id = it->face_id;
5869 last_escape_glyph_merged_face_id = face_id;
5870 }
5871
5872 XSETINT (it->ctl_chars[0], g);
5873 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5874 ctl_len = 2;
5875 goto display_control;
5876 }
5877
5878 /* Handle non-break space in the mode where it only gets
5879 highlighting. */
5880
5881 if (EQ (Vnobreak_char_display, Qt)
5882 && nbsp_or_shy == char_is_nbsp)
5883 {
5884 /* Merge the no-break-space face into the current face. */
5885 face_id = merge_faces (it->f, Qnobreak_space, 0,
5886 it->face_id);
5887
5888 it->c = ' ';
5889 XSETINT (it->ctl_chars[0], ' ');
5890 ctl_len = 1;
5891 goto display_control;
5892 }
5893
5894 /* Handle sequences that start with the "escape glyph". */
5895
5896 /* the default escape glyph is \. */
5897 escape_glyph = '\\';
5898
5899 if (it->dp
5900 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5901 && GLYPH_CODE_CHAR_VALID_P (gc))
5902 {
5903 escape_glyph = GLYPH_CODE_CHAR (gc);
5904 lface_id = GLYPH_CODE_FACE (gc);
5905 }
5906 if (lface_id)
5907 {
5908 /* The display table specified a face.
5909 Merge it into face_id and also into escape_glyph. */
5910 face_id = merge_faces (it->f, Qt, lface_id,
5911 it->face_id);
5912 }
5913 else if (it->f == last_escape_glyph_frame
5914 && it->face_id == last_escape_glyph_face_id)
5915 {
5916 face_id = last_escape_glyph_merged_face_id;
5917 }
5918 else
5919 {
5920 /* Merge the escape-glyph face into the current face. */
5921 face_id = merge_faces (it->f, Qescape_glyph, 0,
5922 it->face_id);
5923 last_escape_glyph_frame = it->f;
5924 last_escape_glyph_face_id = it->face_id;
5925 last_escape_glyph_merged_face_id = face_id;
5926 }
5927
5928 /* Handle soft hyphens in the mode where they only get
5929 highlighting. */
5930
5931 if (EQ (Vnobreak_char_display, Qt)
5932 && nbsp_or_shy == char_is_soft_hyphen)
5933 {
5934 it->c = '-';
5935 XSETINT (it->ctl_chars[0], '-');
5936 ctl_len = 1;
5937 goto display_control;
5938 }
5939
5940 /* Handle non-break space and soft hyphen
5941 with the escape glyph. */
5942
5943 if (nbsp_or_shy)
5944 {
5945 XSETINT (it->ctl_chars[0], escape_glyph);
5946 it->c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5947 XSETINT (it->ctl_chars[1], it->c);
5948 ctl_len = 2;
5949 goto display_control;
5950 }
5951
5952 {
5953 unsigned char str[MAX_MULTIBYTE_LENGTH];
5954 int len;
5955 int i;
5956
5957 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5958 if (CHAR_BYTE8_P (it->c))
5959 {
5960 str[0] = CHAR_TO_BYTE8 (it->c);
5961 len = 1;
5962 }
5963 else if (it->c < 256)
5964 {
5965 str[0] = it->c;
5966 len = 1;
5967 }
5968 else
5969 {
5970 /* It's an invalid character, which shouldn't
5971 happen actually, but due to bugs it may
5972 happen. Let's print the char as is, there's
5973 not much meaningful we can do with it. */
5974 str[0] = it->c;
5975 str[1] = it->c >> 8;
5976 str[2] = it->c >> 16;
5977 str[3] = it->c >> 24;
5978 len = 4;
5979 }
5980
5981 for (i = 0; i < len; i++)
5982 {
5983 int g;
5984 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5985 /* Insert three more glyphs into IT->ctl_chars for
5986 the octal display of the character. */
5987 g = ((str[i] >> 6) & 7) + '0';
5988 XSETINT (it->ctl_chars[i * 4 + 1], g);
5989 g = ((str[i] >> 3) & 7) + '0';
5990 XSETINT (it->ctl_chars[i * 4 + 2], g);
5991 g = (str[i] & 7) + '0';
5992 XSETINT (it->ctl_chars[i * 4 + 3], g);
5993 }
5994 ctl_len = len * 4;
5995 }
5996
5997 display_control:
5998 /* Set up IT->dpvec and return first character from it. */
5999 it->dpvec_char_len = it->len;
6000 it->dpvec = it->ctl_chars;
6001 it->dpend = it->dpvec + ctl_len;
6002 it->current.dpvec_index = 0;
6003 it->dpvec_face_id = face_id;
6004 it->saved_face_id = it->face_id;
6005 it->method = GET_FROM_DISPLAY_VECTOR;
6006 it->ellipsis_p = 0;
6007 goto get_next;
6008 }
6009 }
6010 }
6011
6012 #ifdef HAVE_WINDOW_SYSTEM
6013 /* Adjust face id for a multibyte character. There are no multibyte
6014 character in unibyte text. */
6015 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6016 && it->multibyte_p
6017 && success_p
6018 && FRAME_WINDOW_P (it->f))
6019 {
6020 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6021
6022 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6023 {
6024 /* Automatic composition with glyph-string. */
6025 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6026
6027 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6028 }
6029 else
6030 {
6031 int pos = (it->s ? -1
6032 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6033 : IT_CHARPOS (*it));
6034
6035 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6036 }
6037 }
6038 #endif
6039
6040 /* Is this character the last one of a run of characters with
6041 box? If yes, set IT->end_of_box_run_p to 1. */
6042 if (it->face_box_p
6043 && it->s == NULL)
6044 {
6045 if (it->method == GET_FROM_STRING && it->sp)
6046 {
6047 int face_id = underlying_face_id (it);
6048 struct face *face = FACE_FROM_ID (it->f, face_id);
6049
6050 if (face)
6051 {
6052 if (face->box == FACE_NO_BOX)
6053 {
6054 /* If the box comes from face properties in a
6055 display string, check faces in that string. */
6056 int string_face_id = face_after_it_pos (it);
6057 it->end_of_box_run_p
6058 = (FACE_FROM_ID (it->f, string_face_id)->box
6059 == FACE_NO_BOX);
6060 }
6061 /* Otherwise, the box comes from the underlying face.
6062 If this is the last string character displayed, check
6063 the next buffer location. */
6064 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6065 && (it->current.overlay_string_index
6066 == it->n_overlay_strings - 1))
6067 {
6068 EMACS_INT ignore;
6069 int next_face_id;
6070 struct text_pos pos = it->current.pos;
6071 INC_TEXT_POS (pos, it->multibyte_p);
6072
6073 next_face_id = face_at_buffer_position
6074 (it->w, CHARPOS (pos), it->region_beg_charpos,
6075 it->region_end_charpos, &ignore,
6076 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6077 -1);
6078 it->end_of_box_run_p
6079 = (FACE_FROM_ID (it->f, next_face_id)->box
6080 == FACE_NO_BOX);
6081 }
6082 }
6083 }
6084 else
6085 {
6086 int face_id = face_after_it_pos (it);
6087 it->end_of_box_run_p
6088 = (face_id != it->face_id
6089 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6090 }
6091 }
6092
6093 /* Value is 0 if end of buffer or string reached. */
6094 return success_p;
6095 }
6096
6097
6098 /* Move IT to the next display element.
6099
6100 RESEAT_P non-zero means if called on a newline in buffer text,
6101 skip to the next visible line start.
6102
6103 Functions get_next_display_element and set_iterator_to_next are
6104 separate because I find this arrangement easier to handle than a
6105 get_next_display_element function that also increments IT's
6106 position. The way it is we can first look at an iterator's current
6107 display element, decide whether it fits on a line, and if it does,
6108 increment the iterator position. The other way around we probably
6109 would either need a flag indicating whether the iterator has to be
6110 incremented the next time, or we would have to implement a
6111 decrement position function which would not be easy to write. */
6112
6113 void
6114 set_iterator_to_next (it, reseat_p)
6115 struct it *it;
6116 int reseat_p;
6117 {
6118 /* Reset flags indicating start and end of a sequence of characters
6119 with box. Reset them at the start of this function because
6120 moving the iterator to a new position might set them. */
6121 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6122
6123 switch (it->method)
6124 {
6125 case GET_FROM_BUFFER:
6126 /* The current display element of IT is a character from
6127 current_buffer. Advance in the buffer, and maybe skip over
6128 invisible lines that are so because of selective display. */
6129 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6130 reseat_at_next_visible_line_start (it, 0);
6131 else if (it->cmp_it.id >= 0)
6132 {
6133 IT_CHARPOS (*it) += it->cmp_it.nchars;
6134 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6135 if (it->cmp_it.to < it->cmp_it.nglyphs)
6136 it->cmp_it.from = it->cmp_it.to;
6137 else
6138 {
6139 it->cmp_it.id = -1;
6140 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6141 IT_BYTEPOS (*it), it->stop_charpos,
6142 Qnil);
6143 }
6144 }
6145 else
6146 {
6147 xassert (it->len != 0);
6148
6149 if (!it->bidi_p)
6150 {
6151 IT_BYTEPOS (*it) += it->len;
6152 IT_CHARPOS (*it) += 1;
6153 }
6154 else
6155 {
6156 /* If this is a new paragraph, determine its base
6157 direction (a.k.a. its base embedding level). */
6158 if (it->bidi_it.new_paragraph)
6159 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6160 bidi_get_next_char_visually (&it->bidi_it);
6161 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6162 IT_CHARPOS (*it) = it->bidi_it.charpos;
6163 }
6164 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6165 }
6166 break;
6167
6168 case GET_FROM_C_STRING:
6169 /* Current display element of IT is from a C string. */
6170 IT_BYTEPOS (*it) += it->len;
6171 IT_CHARPOS (*it) += 1;
6172 break;
6173
6174 case GET_FROM_DISPLAY_VECTOR:
6175 /* Current display element of IT is from a display table entry.
6176 Advance in the display table definition. Reset it to null if
6177 end reached, and continue with characters from buffers/
6178 strings. */
6179 ++it->current.dpvec_index;
6180
6181 /* Restore face of the iterator to what they were before the
6182 display vector entry (these entries may contain faces). */
6183 it->face_id = it->saved_face_id;
6184
6185 if (it->dpvec + it->current.dpvec_index == it->dpend)
6186 {
6187 int recheck_faces = it->ellipsis_p;
6188
6189 if (it->s)
6190 it->method = GET_FROM_C_STRING;
6191 else if (STRINGP (it->string))
6192 it->method = GET_FROM_STRING;
6193 else
6194 {
6195 it->method = GET_FROM_BUFFER;
6196 it->object = it->w->buffer;
6197 }
6198
6199 it->dpvec = NULL;
6200 it->current.dpvec_index = -1;
6201
6202 /* Skip over characters which were displayed via IT->dpvec. */
6203 if (it->dpvec_char_len < 0)
6204 reseat_at_next_visible_line_start (it, 1);
6205 else if (it->dpvec_char_len > 0)
6206 {
6207 if (it->method == GET_FROM_STRING
6208 && it->n_overlay_strings > 0)
6209 it->ignore_overlay_strings_at_pos_p = 1;
6210 it->len = it->dpvec_char_len;
6211 set_iterator_to_next (it, reseat_p);
6212 }
6213
6214 /* Maybe recheck faces after display vector */
6215 if (recheck_faces)
6216 it->stop_charpos = IT_CHARPOS (*it);
6217 }
6218 break;
6219
6220 case GET_FROM_STRING:
6221 /* Current display element is a character from a Lisp string. */
6222 xassert (it->s == NULL && STRINGP (it->string));
6223 if (it->cmp_it.id >= 0)
6224 {
6225 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6226 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6227 if (it->cmp_it.to < it->cmp_it.nglyphs)
6228 it->cmp_it.from = it->cmp_it.to;
6229 else
6230 {
6231 it->cmp_it.id = -1;
6232 composition_compute_stop_pos (&it->cmp_it,
6233 IT_STRING_CHARPOS (*it),
6234 IT_STRING_BYTEPOS (*it),
6235 it->stop_charpos, it->string);
6236 }
6237 }
6238 else
6239 {
6240 IT_STRING_BYTEPOS (*it) += it->len;
6241 IT_STRING_CHARPOS (*it) += 1;
6242 }
6243
6244 consider_string_end:
6245
6246 if (it->current.overlay_string_index >= 0)
6247 {
6248 /* IT->string is an overlay string. Advance to the
6249 next, if there is one. */
6250 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6251 {
6252 it->ellipsis_p = 0;
6253 next_overlay_string (it);
6254 if (it->ellipsis_p)
6255 setup_for_ellipsis (it, 0);
6256 }
6257 }
6258 else
6259 {
6260 /* IT->string is not an overlay string. If we reached
6261 its end, and there is something on IT->stack, proceed
6262 with what is on the stack. This can be either another
6263 string, this time an overlay string, or a buffer. */
6264 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6265 && it->sp > 0)
6266 {
6267 pop_it (it);
6268 if (it->method == GET_FROM_STRING)
6269 goto consider_string_end;
6270 }
6271 }
6272 break;
6273
6274 case GET_FROM_IMAGE:
6275 case GET_FROM_STRETCH:
6276 /* The position etc with which we have to proceed are on
6277 the stack. The position may be at the end of a string,
6278 if the `display' property takes up the whole string. */
6279 xassert (it->sp > 0);
6280 pop_it (it);
6281 if (it->method == GET_FROM_STRING)
6282 goto consider_string_end;
6283 break;
6284
6285 default:
6286 /* There are no other methods defined, so this should be a bug. */
6287 abort ();
6288 }
6289
6290 xassert (it->method != GET_FROM_STRING
6291 || (STRINGP (it->string)
6292 && IT_STRING_CHARPOS (*it) >= 0));
6293 }
6294
6295 /* Load IT's display element fields with information about the next
6296 display element which comes from a display table entry or from the
6297 result of translating a control character to one of the forms `^C'
6298 or `\003'.
6299
6300 IT->dpvec holds the glyphs to return as characters.
6301 IT->saved_face_id holds the face id before the display vector--it
6302 is restored into IT->face_id in set_iterator_to_next. */
6303
6304 static int
6305 next_element_from_display_vector (it)
6306 struct it *it;
6307 {
6308 Lisp_Object gc;
6309
6310 /* Precondition. */
6311 xassert (it->dpvec && it->current.dpvec_index >= 0);
6312
6313 it->face_id = it->saved_face_id;
6314
6315 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6316 That seemed totally bogus - so I changed it... */
6317 gc = it->dpvec[it->current.dpvec_index];
6318
6319 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6320 {
6321 it->c = GLYPH_CODE_CHAR (gc);
6322 it->len = CHAR_BYTES (it->c);
6323
6324 /* The entry may contain a face id to use. Such a face id is
6325 the id of a Lisp face, not a realized face. A face id of
6326 zero means no face is specified. */
6327 if (it->dpvec_face_id >= 0)
6328 it->face_id = it->dpvec_face_id;
6329 else
6330 {
6331 int lface_id = GLYPH_CODE_FACE (gc);
6332 if (lface_id > 0)
6333 it->face_id = merge_faces (it->f, Qt, lface_id,
6334 it->saved_face_id);
6335 }
6336 }
6337 else
6338 /* Display table entry is invalid. Return a space. */
6339 it->c = ' ', it->len = 1;
6340
6341 /* Don't change position and object of the iterator here. They are
6342 still the values of the character that had this display table
6343 entry or was translated, and that's what we want. */
6344 it->what = IT_CHARACTER;
6345 return 1;
6346 }
6347
6348
6349 /* Load IT with the next display element from Lisp string IT->string.
6350 IT->current.string_pos is the current position within the string.
6351 If IT->current.overlay_string_index >= 0, the Lisp string is an
6352 overlay string. */
6353
6354 static int
6355 next_element_from_string (it)
6356 struct it *it;
6357 {
6358 struct text_pos position;
6359
6360 xassert (STRINGP (it->string));
6361 xassert (IT_STRING_CHARPOS (*it) >= 0);
6362 position = it->current.string_pos;
6363
6364 /* Time to check for invisible text? */
6365 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6366 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6367 {
6368 handle_stop (it);
6369
6370 /* Since a handler may have changed IT->method, we must
6371 recurse here. */
6372 return GET_NEXT_DISPLAY_ELEMENT (it);
6373 }
6374
6375 if (it->current.overlay_string_index >= 0)
6376 {
6377 /* Get the next character from an overlay string. In overlay
6378 strings, There is no field width or padding with spaces to
6379 do. */
6380 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6381 {
6382 it->what = IT_EOB;
6383 return 0;
6384 }
6385 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6386 IT_STRING_BYTEPOS (*it))
6387 && next_element_from_composition (it))
6388 {
6389 return 1;
6390 }
6391 else if (STRING_MULTIBYTE (it->string))
6392 {
6393 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6394 const unsigned char *s = (SDATA (it->string)
6395 + IT_STRING_BYTEPOS (*it));
6396 it->c = string_char_and_length (s, &it->len);
6397 }
6398 else
6399 {
6400 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6401 it->len = 1;
6402 }
6403 }
6404 else
6405 {
6406 /* Get the next character from a Lisp string that is not an
6407 overlay string. Such strings come from the mode line, for
6408 example. We may have to pad with spaces, or truncate the
6409 string. See also next_element_from_c_string. */
6410 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6411 {
6412 it->what = IT_EOB;
6413 return 0;
6414 }
6415 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6416 {
6417 /* Pad with spaces. */
6418 it->c = ' ', it->len = 1;
6419 CHARPOS (position) = BYTEPOS (position) = -1;
6420 }
6421 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6422 IT_STRING_BYTEPOS (*it))
6423 && next_element_from_composition (it))
6424 {
6425 return 1;
6426 }
6427 else if (STRING_MULTIBYTE (it->string))
6428 {
6429 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6430 const unsigned char *s = (SDATA (it->string)
6431 + IT_STRING_BYTEPOS (*it));
6432 it->c = string_char_and_length (s, &it->len);
6433 }
6434 else
6435 {
6436 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6437 it->len = 1;
6438 }
6439 }
6440
6441 /* Record what we have and where it came from. */
6442 it->what = IT_CHARACTER;
6443 it->object = it->string;
6444 it->position = position;
6445 return 1;
6446 }
6447
6448
6449 /* Load IT with next display element from C string IT->s.
6450 IT->string_nchars is the maximum number of characters to return
6451 from the string. IT->end_charpos may be greater than
6452 IT->string_nchars when this function is called, in which case we
6453 may have to return padding spaces. Value is zero if end of string
6454 reached, including padding spaces. */
6455
6456 static int
6457 next_element_from_c_string (it)
6458 struct it *it;
6459 {
6460 int success_p = 1;
6461
6462 xassert (it->s);
6463 it->what = IT_CHARACTER;
6464 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6465 it->object = Qnil;
6466
6467 /* IT's position can be greater IT->string_nchars in case a field
6468 width or precision has been specified when the iterator was
6469 initialized. */
6470 if (IT_CHARPOS (*it) >= it->end_charpos)
6471 {
6472 /* End of the game. */
6473 it->what = IT_EOB;
6474 success_p = 0;
6475 }
6476 else if (IT_CHARPOS (*it) >= it->string_nchars)
6477 {
6478 /* Pad with spaces. */
6479 it->c = ' ', it->len = 1;
6480 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6481 }
6482 else if (it->multibyte_p)
6483 {
6484 /* Implementation note: The calls to strlen apparently aren't a
6485 performance problem because there is no noticeable performance
6486 difference between Emacs running in unibyte or multibyte mode. */
6487 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6488 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6489 }
6490 else
6491 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6492
6493 return success_p;
6494 }
6495
6496
6497 /* Set up IT to return characters from an ellipsis, if appropriate.
6498 The definition of the ellipsis glyphs may come from a display table
6499 entry. This function fills IT with the first glyph from the
6500 ellipsis if an ellipsis is to be displayed. */
6501
6502 static int
6503 next_element_from_ellipsis (it)
6504 struct it *it;
6505 {
6506 if (it->selective_display_ellipsis_p)
6507 setup_for_ellipsis (it, it->len);
6508 else
6509 {
6510 /* The face at the current position may be different from the
6511 face we find after the invisible text. Remember what it
6512 was in IT->saved_face_id, and signal that it's there by
6513 setting face_before_selective_p. */
6514 it->saved_face_id = it->face_id;
6515 it->method = GET_FROM_BUFFER;
6516 it->object = it->w->buffer;
6517 reseat_at_next_visible_line_start (it, 1);
6518 it->face_before_selective_p = 1;
6519 }
6520
6521 return GET_NEXT_DISPLAY_ELEMENT (it);
6522 }
6523
6524
6525 /* Deliver an image display element. The iterator IT is already
6526 filled with image information (done in handle_display_prop). Value
6527 is always 1. */
6528
6529
6530 static int
6531 next_element_from_image (it)
6532 struct it *it;
6533 {
6534 it->what = IT_IMAGE;
6535 return 1;
6536 }
6537
6538
6539 /* Fill iterator IT with next display element from a stretch glyph
6540 property. IT->object is the value of the text property. Value is
6541 always 1. */
6542
6543 static int
6544 next_element_from_stretch (it)
6545 struct it *it;
6546 {
6547 it->what = IT_STRETCH;
6548 return 1;
6549 }
6550
6551 /* Scan forward from CHARPOS in the current buffer, until we find a
6552 stop position > current IT's position. Then handle the stop
6553 position before that.
6554
6555 This is called when we are reordering bidirectional text. The
6556 caller should save and restore IT and in particular the bidi_p
6557 flag, because this function modifies them. */
6558
6559 static void
6560 handle_stop_backwards (it, charpos)
6561 struct it *it;
6562 EMACS_INT charpos;
6563 {
6564 struct text_pos pos1;
6565 EMACS_INT where_we_are = IT_CHARPOS (*it);
6566 EMACS_INT next_stop;
6567
6568 /* Scan in strict logical order. */
6569 it->bidi_p = 0;
6570 do
6571 {
6572 it->prev_stop = charpos;
6573 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6574 reseat_1 (it, pos1, 0);
6575 compute_stop_pos (it);
6576 /* We must advance forward, right? */
6577 if (it->stop_charpos <= it->prev_stop)
6578 abort ();
6579 charpos = it->stop_charpos;
6580 }
6581 while (charpos <= where_we_are);
6582
6583 next_stop = it->stop_charpos;
6584 it->stop_charpos = it->prev_stop;
6585 handle_stop (it);
6586 it->stop_charpos = next_stop;
6587 }
6588
6589 /* Load IT with the next display element from current_buffer. Value
6590 is zero if end of buffer reached. IT->stop_charpos is the next
6591 position at which to stop and check for text properties or buffer
6592 end. */
6593
6594 static int
6595 next_element_from_buffer (it)
6596 struct it *it;
6597 {
6598 int success_p = 1;
6599
6600 xassert (IT_CHARPOS (*it) >= BEGV);
6601
6602 /* With bidi reordering, the character to display might not be the
6603 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6604 we were reseat()ed to a new buffer position, which is potentially
6605 a different paragraph. */
6606 if (it->bidi_p && it->bidi_it.first_elt)
6607 {
6608 it->bidi_it.charpos = IT_CHARPOS (*it);
6609 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6610 /* If we are at the beginning of a line, we can produce the next
6611 element right away. */
6612 if (it->bidi_it.bytepos == BEGV_BYTE
6613 /* FIXME: Should support all Unicode line separators. */
6614 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6615 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6616 {
6617 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6618 /* If the paragraph base direction is R2L, its glyphs should
6619 be reversed. */
6620 if (it->glyph_row && (it->bidi_it.level_stack[0].level & 1) != 0)
6621 it->glyph_row->reversed_p = 1;
6622 bidi_get_next_char_visually (&it->bidi_it);
6623 }
6624 else
6625 {
6626 int orig_bytepos = IT_BYTEPOS (*it);
6627
6628 /* We need to prime the bidi iterator starting at the line's
6629 beginning, before we will be able to produce the next
6630 element. */
6631 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6632 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6633 it->bidi_it.charpos = IT_CHARPOS (*it);
6634 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6635 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6636 if (it->glyph_row && (it->bidi_it.level_stack[0].level & 1) != 0)
6637 it->glyph_row->reversed_p = 1;
6638 do {
6639 /* Now return to buffer position where we were asked to
6640 get the next display element, and produce that. */
6641 bidi_get_next_char_visually (&it->bidi_it);
6642 } while (it->bidi_it.bytepos != orig_bytepos
6643 && it->bidi_it.bytepos < ZV_BYTE);
6644 }
6645
6646 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6647 /* Adjust IT's position information to where we ended up. */
6648 IT_CHARPOS (*it) = it->bidi_it.charpos;
6649 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6650 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6651 }
6652
6653 if (IT_CHARPOS (*it) >= it->stop_charpos)
6654 {
6655 if (IT_CHARPOS (*it) >= it->end_charpos)
6656 {
6657 int overlay_strings_follow_p;
6658
6659 /* End of the game, except when overlay strings follow that
6660 haven't been returned yet. */
6661 if (it->overlay_strings_at_end_processed_p)
6662 overlay_strings_follow_p = 0;
6663 else
6664 {
6665 it->overlay_strings_at_end_processed_p = 1;
6666 overlay_strings_follow_p = get_overlay_strings (it, 0);
6667 }
6668
6669 if (overlay_strings_follow_p)
6670 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6671 else
6672 {
6673 it->what = IT_EOB;
6674 it->position = it->current.pos;
6675 success_p = 0;
6676 }
6677 }
6678 else if (!(!it->bidi_p
6679 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6680 || IT_CHARPOS (*it) == it->stop_charpos))
6681 {
6682 /* With bidi non-linear iteration, we could find ourselves
6683 far beyond the last computed stop_charpos, with several
6684 other stop positions in between that we missed. Scan
6685 them all now, in buffer's logical order, until we find
6686 and handle the last stop_charpos that precedes our
6687 current position. */
6688 struct it save_it = *it;
6689
6690 handle_stop_backwards (it, it->stop_charpos);
6691 it->bidi_p = 1;
6692 it->current = save_it.current;
6693 it->position = save_it.position;
6694 return GET_NEXT_DISPLAY_ELEMENT (it);
6695 }
6696 else
6697 {
6698 /* If we are at base paragraph embedding level, take note of
6699 the last stop position seen at this level. */
6700 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6701 it->base_level_stop = it->stop_charpos;
6702 handle_stop (it);
6703 return GET_NEXT_DISPLAY_ELEMENT (it);
6704 }
6705 }
6706 else if (it->bidi_p && IT_CHARPOS (*it) < it->prev_stop)
6707 {
6708 struct it save_it = *it;
6709
6710 if (it->base_level_stop <= 0)
6711 it->base_level_stop = 1;
6712 if (IT_CHARPOS (*it) < it->base_level_stop)
6713 abort ();
6714 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6715 abort ();
6716 handle_stop_backwards (it, it->base_level_stop);
6717 it->bidi_p = 1;
6718 it->current = save_it.current;
6719 it->position = save_it.position;
6720 return GET_NEXT_DISPLAY_ELEMENT (it);
6721 }
6722 else
6723 {
6724 /* No face changes, overlays etc. in sight, so just return a
6725 character from current_buffer. */
6726 unsigned char *p;
6727
6728 /* Maybe run the redisplay end trigger hook. Performance note:
6729 This doesn't seem to cost measurable time. */
6730 if (it->redisplay_end_trigger_charpos
6731 && it->glyph_row
6732 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6733 run_redisplay_end_trigger_hook (it);
6734
6735 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it))
6736 && next_element_from_composition (it))
6737 {
6738 return 1;
6739 }
6740
6741 /* Get the next character, maybe multibyte. */
6742 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6743 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6744 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6745 else
6746 it->c = *p, it->len = 1;
6747
6748 /* Record what we have and where it came from. */
6749 it->what = IT_CHARACTER;
6750 it->object = it->w->buffer;
6751 it->position = it->current.pos;
6752
6753 /* Normally we return the character found above, except when we
6754 really want to return an ellipsis for selective display. */
6755 if (it->selective)
6756 {
6757 if (it->c == '\n')
6758 {
6759 /* A value of selective > 0 means hide lines indented more
6760 than that number of columns. */
6761 if (it->selective > 0
6762 && IT_CHARPOS (*it) + 1 < ZV
6763 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6764 IT_BYTEPOS (*it) + 1,
6765 (double) it->selective)) /* iftc */
6766 {
6767 success_p = next_element_from_ellipsis (it);
6768 it->dpvec_char_len = -1;
6769 }
6770 }
6771 else if (it->c == '\r' && it->selective == -1)
6772 {
6773 /* A value of selective == -1 means that everything from the
6774 CR to the end of the line is invisible, with maybe an
6775 ellipsis displayed for it. */
6776 success_p = next_element_from_ellipsis (it);
6777 it->dpvec_char_len = -1;
6778 }
6779 }
6780 }
6781
6782 /* Value is zero if end of buffer reached. */
6783 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6784 return success_p;
6785 }
6786
6787
6788 /* Run the redisplay end trigger hook for IT. */
6789
6790 static void
6791 run_redisplay_end_trigger_hook (it)
6792 struct it *it;
6793 {
6794 Lisp_Object args[3];
6795
6796 /* IT->glyph_row should be non-null, i.e. we should be actually
6797 displaying something, or otherwise we should not run the hook. */
6798 xassert (it->glyph_row);
6799
6800 /* Set up hook arguments. */
6801 args[0] = Qredisplay_end_trigger_functions;
6802 args[1] = it->window;
6803 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6804 it->redisplay_end_trigger_charpos = 0;
6805
6806 /* Since we are *trying* to run these functions, don't try to run
6807 them again, even if they get an error. */
6808 it->w->redisplay_end_trigger = Qnil;
6809 Frun_hook_with_args (3, args);
6810
6811 /* Notice if it changed the face of the character we are on. */
6812 handle_face_prop (it);
6813 }
6814
6815
6816 /* Deliver a composition display element. Unlike the other
6817 next_element_from_XXX, this function is not registered in the array
6818 get_next_element[]. It is called from next_element_from_buffer and
6819 next_element_from_string when necessary. */
6820
6821 static int
6822 next_element_from_composition (it)
6823 struct it *it;
6824 {
6825 it->what = IT_COMPOSITION;
6826 it->len = it->cmp_it.nbytes;
6827 if (STRINGP (it->string))
6828 {
6829 if (it->c < 0)
6830 {
6831 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6832 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6833 return 0;
6834 }
6835 it->position = it->current.string_pos;
6836 it->object = it->string;
6837 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6838 IT_STRING_BYTEPOS (*it), it->string);
6839 }
6840 else
6841 {
6842 if (it->c < 0)
6843 {
6844 IT_CHARPOS (*it) += it->cmp_it.nchars;
6845 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6846 return 0;
6847 }
6848 it->position = it->current.pos;
6849 it->object = it->w->buffer;
6850 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6851 IT_BYTEPOS (*it), Qnil);
6852 }
6853 return 1;
6854 }
6855
6856
6857 \f
6858 /***********************************************************************
6859 Moving an iterator without producing glyphs
6860 ***********************************************************************/
6861
6862 /* Check if iterator is at a position corresponding to a valid buffer
6863 position after some move_it_ call. */
6864
6865 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6866 ((it)->method == GET_FROM_STRING \
6867 ? IT_STRING_CHARPOS (*it) == 0 \
6868 : 1)
6869
6870
6871 /* Move iterator IT to a specified buffer or X position within one
6872 line on the display without producing glyphs.
6873
6874 OP should be a bit mask including some or all of these bits:
6875 MOVE_TO_X: Stop upon reaching x-position TO_X.
6876 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6877 Regardless of OP's value, stop upon reaching the end of the display line.
6878
6879 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6880 This means, in particular, that TO_X includes window's horizontal
6881 scroll amount.
6882
6883 The return value has several possible values that
6884 say what condition caused the scan to stop:
6885
6886 MOVE_POS_MATCH_OR_ZV
6887 - when TO_POS or ZV was reached.
6888
6889 MOVE_X_REACHED
6890 -when TO_X was reached before TO_POS or ZV were reached.
6891
6892 MOVE_LINE_CONTINUED
6893 - when we reached the end of the display area and the line must
6894 be continued.
6895
6896 MOVE_LINE_TRUNCATED
6897 - when we reached the end of the display area and the line is
6898 truncated.
6899
6900 MOVE_NEWLINE_OR_CR
6901 - when we stopped at a line end, i.e. a newline or a CR and selective
6902 display is on. */
6903
6904 static enum move_it_result
6905 move_it_in_display_line_to (struct it *it,
6906 EMACS_INT to_charpos, int to_x,
6907 enum move_operation_enum op)
6908 {
6909 enum move_it_result result = MOVE_UNDEFINED;
6910 struct glyph_row *saved_glyph_row;
6911 struct it wrap_it, atpos_it, atx_it;
6912 int may_wrap = 0;
6913
6914 /* Don't produce glyphs in produce_glyphs. */
6915 saved_glyph_row = it->glyph_row;
6916 it->glyph_row = NULL;
6917
6918 /* Use wrap_it to save a copy of IT wherever a word wrap could
6919 occur. Use atpos_it to save a copy of IT at the desired buffer
6920 position, if found, so that we can scan ahead and check if the
6921 word later overshoots the window edge. Use atx_it similarly, for
6922 pixel positions. */
6923 wrap_it.sp = -1;
6924 atpos_it.sp = -1;
6925 atx_it.sp = -1;
6926
6927 #define BUFFER_POS_REACHED_P() \
6928 ((op & MOVE_TO_POS) != 0 \
6929 && BUFFERP (it->object) \
6930 && IT_CHARPOS (*it) >= to_charpos \
6931 && (it->method == GET_FROM_BUFFER \
6932 || (it->method == GET_FROM_DISPLAY_VECTOR \
6933 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6934
6935 /* If there's a line-/wrap-prefix, handle it. */
6936 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6937 && it->current_y < it->last_visible_y)
6938 handle_line_prefix (it);
6939
6940 while (1)
6941 {
6942 int x, i, ascent = 0, descent = 0;
6943
6944 /* Utility macro to reset an iterator with x, ascent, and descent. */
6945 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6946 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6947 (IT)->max_descent = descent)
6948
6949 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6950 glyph). */
6951 if ((op & MOVE_TO_POS) != 0
6952 && BUFFERP (it->object)
6953 && it->method == GET_FROM_BUFFER
6954 && IT_CHARPOS (*it) > to_charpos)
6955 {
6956 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6957 {
6958 result = MOVE_POS_MATCH_OR_ZV;
6959 break;
6960 }
6961 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6962 /* If wrap_it is valid, the current position might be in a
6963 word that is wrapped. So, save the iterator in
6964 atpos_it and continue to see if wrapping happens. */
6965 atpos_it = *it;
6966 }
6967
6968 /* Stop when ZV reached.
6969 We used to stop here when TO_CHARPOS reached as well, but that is
6970 too soon if this glyph does not fit on this line. So we handle it
6971 explicitly below. */
6972 if (!get_next_display_element (it))
6973 {
6974 result = MOVE_POS_MATCH_OR_ZV;
6975 break;
6976 }
6977
6978 if (it->line_wrap == TRUNCATE)
6979 {
6980 if (BUFFER_POS_REACHED_P ())
6981 {
6982 result = MOVE_POS_MATCH_OR_ZV;
6983 break;
6984 }
6985 }
6986 else
6987 {
6988 if (it->line_wrap == WORD_WRAP)
6989 {
6990 if (IT_DISPLAYING_WHITESPACE (it))
6991 may_wrap = 1;
6992 else if (may_wrap)
6993 {
6994 /* We have reached a glyph that follows one or more
6995 whitespace characters. If the position is
6996 already found, we are done. */
6997 if (atpos_it.sp >= 0)
6998 {
6999 *it = atpos_it;
7000 result = MOVE_POS_MATCH_OR_ZV;
7001 goto done;
7002 }
7003 if (atx_it.sp >= 0)
7004 {
7005 *it = atx_it;
7006 result = MOVE_X_REACHED;
7007 goto done;
7008 }
7009 /* Otherwise, we can wrap here. */
7010 wrap_it = *it;
7011 may_wrap = 0;
7012 }
7013 }
7014 }
7015
7016 /* Remember the line height for the current line, in case
7017 the next element doesn't fit on the line. */
7018 ascent = it->max_ascent;
7019 descent = it->max_descent;
7020
7021 /* The call to produce_glyphs will get the metrics of the
7022 display element IT is loaded with. Record the x-position
7023 before this display element, in case it doesn't fit on the
7024 line. */
7025 x = it->current_x;
7026
7027 PRODUCE_GLYPHS (it);
7028
7029 if (it->area != TEXT_AREA)
7030 {
7031 set_iterator_to_next (it, 1);
7032 continue;
7033 }
7034
7035 /* The number of glyphs we get back in IT->nglyphs will normally
7036 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7037 character on a terminal frame, or (iii) a line end. For the
7038 second case, IT->nglyphs - 1 padding glyphs will be present.
7039 (On X frames, there is only one glyph produced for a
7040 composite character.)
7041
7042 The behavior implemented below means, for continuation lines,
7043 that as many spaces of a TAB as fit on the current line are
7044 displayed there. For terminal frames, as many glyphs of a
7045 multi-glyph character are displayed in the current line, too.
7046 This is what the old redisplay code did, and we keep it that
7047 way. Under X, the whole shape of a complex character must
7048 fit on the line or it will be completely displayed in the
7049 next line.
7050
7051 Note that both for tabs and padding glyphs, all glyphs have
7052 the same width. */
7053 if (it->nglyphs)
7054 {
7055 /* More than one glyph or glyph doesn't fit on line. All
7056 glyphs have the same width. */
7057 int single_glyph_width = it->pixel_width / it->nglyphs;
7058 int new_x;
7059 int x_before_this_char = x;
7060 int hpos_before_this_char = it->hpos;
7061
7062 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7063 {
7064 new_x = x + single_glyph_width;
7065
7066 /* We want to leave anything reaching TO_X to the caller. */
7067 if ((op & MOVE_TO_X) && new_x > to_x)
7068 {
7069 if (BUFFER_POS_REACHED_P ())
7070 {
7071 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7072 goto buffer_pos_reached;
7073 if (atpos_it.sp < 0)
7074 {
7075 atpos_it = *it;
7076 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7077 }
7078 }
7079 else
7080 {
7081 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7082 {
7083 it->current_x = x;
7084 result = MOVE_X_REACHED;
7085 break;
7086 }
7087 if (atx_it.sp < 0)
7088 {
7089 atx_it = *it;
7090 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7091 }
7092 }
7093 }
7094
7095 if (/* Lines are continued. */
7096 it->line_wrap != TRUNCATE
7097 && (/* And glyph doesn't fit on the line. */
7098 new_x > it->last_visible_x
7099 /* Or it fits exactly and we're on a window
7100 system frame. */
7101 || (new_x == it->last_visible_x
7102 && FRAME_WINDOW_P (it->f))))
7103 {
7104 if (/* IT->hpos == 0 means the very first glyph
7105 doesn't fit on the line, e.g. a wide image. */
7106 it->hpos == 0
7107 || (new_x == it->last_visible_x
7108 && FRAME_WINDOW_P (it->f)))
7109 {
7110 ++it->hpos;
7111 it->current_x = new_x;
7112
7113 /* The character's last glyph just barely fits
7114 in this row. */
7115 if (i == it->nglyphs - 1)
7116 {
7117 /* If this is the destination position,
7118 return a position *before* it in this row,
7119 now that we know it fits in this row. */
7120 if (BUFFER_POS_REACHED_P ())
7121 {
7122 if (it->line_wrap != WORD_WRAP
7123 || wrap_it.sp < 0)
7124 {
7125 it->hpos = hpos_before_this_char;
7126 it->current_x = x_before_this_char;
7127 result = MOVE_POS_MATCH_OR_ZV;
7128 break;
7129 }
7130 if (it->line_wrap == WORD_WRAP
7131 && atpos_it.sp < 0)
7132 {
7133 atpos_it = *it;
7134 atpos_it.current_x = x_before_this_char;
7135 atpos_it.hpos = hpos_before_this_char;
7136 }
7137 }
7138
7139 set_iterator_to_next (it, 1);
7140 /* On graphical terminals, newlines may
7141 "overflow" into the fringe if
7142 overflow-newline-into-fringe is non-nil.
7143 On text-only terminals, newlines may
7144 overflow into the last glyph on the
7145 display line.*/
7146 if (!FRAME_WINDOW_P (it->f)
7147 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7148 {
7149 if (!get_next_display_element (it))
7150 {
7151 result = MOVE_POS_MATCH_OR_ZV;
7152 break;
7153 }
7154 if (BUFFER_POS_REACHED_P ())
7155 {
7156 if (ITERATOR_AT_END_OF_LINE_P (it))
7157 result = MOVE_POS_MATCH_OR_ZV;
7158 else
7159 result = MOVE_LINE_CONTINUED;
7160 break;
7161 }
7162 if (ITERATOR_AT_END_OF_LINE_P (it))
7163 {
7164 result = MOVE_NEWLINE_OR_CR;
7165 break;
7166 }
7167 }
7168 }
7169 }
7170 else
7171 IT_RESET_X_ASCENT_DESCENT (it);
7172
7173 if (wrap_it.sp >= 0)
7174 {
7175 *it = wrap_it;
7176 atpos_it.sp = -1;
7177 atx_it.sp = -1;
7178 }
7179
7180 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7181 IT_CHARPOS (*it)));
7182 result = MOVE_LINE_CONTINUED;
7183 break;
7184 }
7185
7186 if (BUFFER_POS_REACHED_P ())
7187 {
7188 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7189 goto buffer_pos_reached;
7190 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7191 {
7192 atpos_it = *it;
7193 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7194 }
7195 }
7196
7197 if (new_x > it->first_visible_x)
7198 {
7199 /* Glyph is visible. Increment number of glyphs that
7200 would be displayed. */
7201 ++it->hpos;
7202 }
7203 }
7204
7205 if (result != MOVE_UNDEFINED)
7206 break;
7207 }
7208 else if (BUFFER_POS_REACHED_P ())
7209 {
7210 buffer_pos_reached:
7211 IT_RESET_X_ASCENT_DESCENT (it);
7212 result = MOVE_POS_MATCH_OR_ZV;
7213 break;
7214 }
7215 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7216 {
7217 /* Stop when TO_X specified and reached. This check is
7218 necessary here because of lines consisting of a line end,
7219 only. The line end will not produce any glyphs and we
7220 would never get MOVE_X_REACHED. */
7221 xassert (it->nglyphs == 0);
7222 result = MOVE_X_REACHED;
7223 break;
7224 }
7225
7226 /* Is this a line end? If yes, we're done. */
7227 if (ITERATOR_AT_END_OF_LINE_P (it))
7228 {
7229 result = MOVE_NEWLINE_OR_CR;
7230 break;
7231 }
7232
7233 /* The current display element has been consumed. Advance
7234 to the next. */
7235 set_iterator_to_next (it, 1);
7236
7237 /* Stop if lines are truncated and IT's current x-position is
7238 past the right edge of the window now. */
7239 if (it->line_wrap == TRUNCATE
7240 && it->current_x >= it->last_visible_x)
7241 {
7242 if (!FRAME_WINDOW_P (it->f)
7243 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7244 {
7245 if (!get_next_display_element (it)
7246 || BUFFER_POS_REACHED_P ())
7247 {
7248 result = MOVE_POS_MATCH_OR_ZV;
7249 break;
7250 }
7251 if (ITERATOR_AT_END_OF_LINE_P (it))
7252 {
7253 result = MOVE_NEWLINE_OR_CR;
7254 break;
7255 }
7256 }
7257 result = MOVE_LINE_TRUNCATED;
7258 break;
7259 }
7260 #undef IT_RESET_X_ASCENT_DESCENT
7261 }
7262
7263 #undef BUFFER_POS_REACHED_P
7264
7265 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7266 restore the saved iterator. */
7267 if (atpos_it.sp >= 0)
7268 *it = atpos_it;
7269 else if (atx_it.sp >= 0)
7270 *it = atx_it;
7271
7272 done:
7273
7274 /* Restore the iterator settings altered at the beginning of this
7275 function. */
7276 it->glyph_row = saved_glyph_row;
7277 return result;
7278 }
7279
7280 /* For external use. */
7281 void
7282 move_it_in_display_line (struct it *it,
7283 EMACS_INT to_charpos, int to_x,
7284 enum move_operation_enum op)
7285 {
7286 if (it->line_wrap == WORD_WRAP
7287 && (op & MOVE_TO_X))
7288 {
7289 struct it save_it = *it;
7290 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7291 /* When word-wrap is on, TO_X may lie past the end
7292 of a wrapped line. Then it->current is the
7293 character on the next line, so backtrack to the
7294 space before the wrap point. */
7295 if (skip == MOVE_LINE_CONTINUED)
7296 {
7297 int prev_x = max (it->current_x - 1, 0);
7298 *it = save_it;
7299 move_it_in_display_line_to
7300 (it, -1, prev_x, MOVE_TO_X);
7301 }
7302 }
7303 else
7304 move_it_in_display_line_to (it, to_charpos, to_x, op);
7305 }
7306
7307
7308 /* Move IT forward until it satisfies one or more of the criteria in
7309 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7310
7311 OP is a bit-mask that specifies where to stop, and in particular,
7312 which of those four position arguments makes a difference. See the
7313 description of enum move_operation_enum.
7314
7315 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7316 screen line, this function will set IT to the next position >
7317 TO_CHARPOS. */
7318
7319 void
7320 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7321 struct it *it;
7322 int to_charpos, to_x, to_y, to_vpos;
7323 int op;
7324 {
7325 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7326 int line_height, line_start_x = 0, reached = 0;
7327
7328 for (;;)
7329 {
7330 if (op & MOVE_TO_VPOS)
7331 {
7332 /* If no TO_CHARPOS and no TO_X specified, stop at the
7333 start of the line TO_VPOS. */
7334 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7335 {
7336 if (it->vpos == to_vpos)
7337 {
7338 reached = 1;
7339 break;
7340 }
7341 else
7342 skip = move_it_in_display_line_to (it, -1, -1, 0);
7343 }
7344 else
7345 {
7346 /* TO_VPOS >= 0 means stop at TO_X in the line at
7347 TO_VPOS, or at TO_POS, whichever comes first. */
7348 if (it->vpos == to_vpos)
7349 {
7350 reached = 2;
7351 break;
7352 }
7353
7354 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7355
7356 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7357 {
7358 reached = 3;
7359 break;
7360 }
7361 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7362 {
7363 /* We have reached TO_X but not in the line we want. */
7364 skip = move_it_in_display_line_to (it, to_charpos,
7365 -1, MOVE_TO_POS);
7366 if (skip == MOVE_POS_MATCH_OR_ZV)
7367 {
7368 reached = 4;
7369 break;
7370 }
7371 }
7372 }
7373 }
7374 else if (op & MOVE_TO_Y)
7375 {
7376 struct it it_backup;
7377
7378 if (it->line_wrap == WORD_WRAP)
7379 it_backup = *it;
7380
7381 /* TO_Y specified means stop at TO_X in the line containing
7382 TO_Y---or at TO_CHARPOS if this is reached first. The
7383 problem is that we can't really tell whether the line
7384 contains TO_Y before we have completely scanned it, and
7385 this may skip past TO_X. What we do is to first scan to
7386 TO_X.
7387
7388 If TO_X is not specified, use a TO_X of zero. The reason
7389 is to make the outcome of this function more predictable.
7390 If we didn't use TO_X == 0, we would stop at the end of
7391 the line which is probably not what a caller would expect
7392 to happen. */
7393 skip = move_it_in_display_line_to
7394 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7395 (MOVE_TO_X | (op & MOVE_TO_POS)));
7396
7397 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7398 if (skip == MOVE_POS_MATCH_OR_ZV)
7399 reached = 5;
7400 else if (skip == MOVE_X_REACHED)
7401 {
7402 /* If TO_X was reached, we want to know whether TO_Y is
7403 in the line. We know this is the case if the already
7404 scanned glyphs make the line tall enough. Otherwise,
7405 we must check by scanning the rest of the line. */
7406 line_height = it->max_ascent + it->max_descent;
7407 if (to_y >= it->current_y
7408 && to_y < it->current_y + line_height)
7409 {
7410 reached = 6;
7411 break;
7412 }
7413 it_backup = *it;
7414 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7415 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7416 op & MOVE_TO_POS);
7417 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7418 line_height = it->max_ascent + it->max_descent;
7419 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7420
7421 if (to_y >= it->current_y
7422 && to_y < it->current_y + line_height)
7423 {
7424 /* If TO_Y is in this line and TO_X was reached
7425 above, we scanned too far. We have to restore
7426 IT's settings to the ones before skipping. */
7427 *it = it_backup;
7428 reached = 6;
7429 }
7430 else
7431 {
7432 skip = skip2;
7433 if (skip == MOVE_POS_MATCH_OR_ZV)
7434 reached = 7;
7435 }
7436 }
7437 else
7438 {
7439 /* Check whether TO_Y is in this line. */
7440 line_height = it->max_ascent + it->max_descent;
7441 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7442
7443 if (to_y >= it->current_y
7444 && to_y < it->current_y + line_height)
7445 {
7446 /* When word-wrap is on, TO_X may lie past the end
7447 of a wrapped line. Then it->current is the
7448 character on the next line, so backtrack to the
7449 space before the wrap point. */
7450 if (skip == MOVE_LINE_CONTINUED
7451 && it->line_wrap == WORD_WRAP)
7452 {
7453 int prev_x = max (it->current_x - 1, 0);
7454 *it = it_backup;
7455 skip = move_it_in_display_line_to
7456 (it, -1, prev_x, MOVE_TO_X);
7457 }
7458 reached = 6;
7459 }
7460 }
7461
7462 if (reached)
7463 break;
7464 }
7465 else if (BUFFERP (it->object)
7466 && (it->method == GET_FROM_BUFFER
7467 || it->method == GET_FROM_STRETCH)
7468 && IT_CHARPOS (*it) >= to_charpos)
7469 skip = MOVE_POS_MATCH_OR_ZV;
7470 else
7471 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7472
7473 switch (skip)
7474 {
7475 case MOVE_POS_MATCH_OR_ZV:
7476 reached = 8;
7477 goto out;
7478
7479 case MOVE_NEWLINE_OR_CR:
7480 set_iterator_to_next (it, 1);
7481 it->continuation_lines_width = 0;
7482 break;
7483
7484 case MOVE_LINE_TRUNCATED:
7485 it->continuation_lines_width = 0;
7486 reseat_at_next_visible_line_start (it, 0);
7487 if ((op & MOVE_TO_POS) != 0
7488 && IT_CHARPOS (*it) > to_charpos)
7489 {
7490 reached = 9;
7491 goto out;
7492 }
7493 break;
7494
7495 case MOVE_LINE_CONTINUED:
7496 /* For continued lines ending in a tab, some of the glyphs
7497 associated with the tab are displayed on the current
7498 line. Since it->current_x does not include these glyphs,
7499 we use it->last_visible_x instead. */
7500 if (it->c == '\t')
7501 {
7502 it->continuation_lines_width += it->last_visible_x;
7503 /* When moving by vpos, ensure that the iterator really
7504 advances to the next line (bug#847, bug#969). Fixme:
7505 do we need to do this in other circumstances? */
7506 if (it->current_x != it->last_visible_x
7507 && (op & MOVE_TO_VPOS)
7508 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7509 {
7510 line_start_x = it->current_x + it->pixel_width
7511 - it->last_visible_x;
7512 set_iterator_to_next (it, 0);
7513 }
7514 }
7515 else
7516 it->continuation_lines_width += it->current_x;
7517 break;
7518
7519 default:
7520 abort ();
7521 }
7522
7523 /* Reset/increment for the next run. */
7524 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7525 it->current_x = line_start_x;
7526 line_start_x = 0;
7527 it->hpos = 0;
7528 it->current_y += it->max_ascent + it->max_descent;
7529 ++it->vpos;
7530 last_height = it->max_ascent + it->max_descent;
7531 last_max_ascent = it->max_ascent;
7532 it->max_ascent = it->max_descent = 0;
7533 }
7534
7535 out:
7536
7537 /* On text terminals, we may stop at the end of a line in the middle
7538 of a multi-character glyph. If the glyph itself is continued,
7539 i.e. it is actually displayed on the next line, don't treat this
7540 stopping point as valid; move to the next line instead (unless
7541 that brings us offscreen). */
7542 if (!FRAME_WINDOW_P (it->f)
7543 && op & MOVE_TO_POS
7544 && IT_CHARPOS (*it) == to_charpos
7545 && it->what == IT_CHARACTER
7546 && it->nglyphs > 1
7547 && it->line_wrap == WINDOW_WRAP
7548 && it->current_x == it->last_visible_x - 1
7549 && it->c != '\n'
7550 && it->c != '\t'
7551 && it->vpos < XFASTINT (it->w->window_end_vpos))
7552 {
7553 it->continuation_lines_width += it->current_x;
7554 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7555 it->current_y += it->max_ascent + it->max_descent;
7556 ++it->vpos;
7557 last_height = it->max_ascent + it->max_descent;
7558 last_max_ascent = it->max_ascent;
7559 }
7560
7561 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7562 }
7563
7564
7565 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7566
7567 If DY > 0, move IT backward at least that many pixels. DY = 0
7568 means move IT backward to the preceding line start or BEGV. This
7569 function may move over more than DY pixels if IT->current_y - DY
7570 ends up in the middle of a line; in this case IT->current_y will be
7571 set to the top of the line moved to. */
7572
7573 void
7574 move_it_vertically_backward (it, dy)
7575 struct it *it;
7576 int dy;
7577 {
7578 int nlines, h;
7579 struct it it2, it3;
7580 int start_pos;
7581
7582 move_further_back:
7583 xassert (dy >= 0);
7584
7585 start_pos = IT_CHARPOS (*it);
7586
7587 /* Estimate how many newlines we must move back. */
7588 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7589
7590 /* Set the iterator's position that many lines back. */
7591 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7592 back_to_previous_visible_line_start (it);
7593
7594 /* Reseat the iterator here. When moving backward, we don't want
7595 reseat to skip forward over invisible text, set up the iterator
7596 to deliver from overlay strings at the new position etc. So,
7597 use reseat_1 here. */
7598 reseat_1 (it, it->current.pos, 1);
7599
7600 /* We are now surely at a line start. */
7601 it->current_x = it->hpos = 0;
7602 it->continuation_lines_width = 0;
7603
7604 /* Move forward and see what y-distance we moved. First move to the
7605 start of the next line so that we get its height. We need this
7606 height to be able to tell whether we reached the specified
7607 y-distance. */
7608 it2 = *it;
7609 it2.max_ascent = it2.max_descent = 0;
7610 do
7611 {
7612 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7613 MOVE_TO_POS | MOVE_TO_VPOS);
7614 }
7615 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7616 xassert (IT_CHARPOS (*it) >= BEGV);
7617 it3 = it2;
7618
7619 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7620 xassert (IT_CHARPOS (*it) >= BEGV);
7621 /* H is the actual vertical distance from the position in *IT
7622 and the starting position. */
7623 h = it2.current_y - it->current_y;
7624 /* NLINES is the distance in number of lines. */
7625 nlines = it2.vpos - it->vpos;
7626
7627 /* Correct IT's y and vpos position
7628 so that they are relative to the starting point. */
7629 it->vpos -= nlines;
7630 it->current_y -= h;
7631
7632 if (dy == 0)
7633 {
7634 /* DY == 0 means move to the start of the screen line. The
7635 value of nlines is > 0 if continuation lines were involved. */
7636 if (nlines > 0)
7637 move_it_by_lines (it, nlines, 1);
7638 }
7639 else
7640 {
7641 /* The y-position we try to reach, relative to *IT.
7642 Note that H has been subtracted in front of the if-statement. */
7643 int target_y = it->current_y + h - dy;
7644 int y0 = it3.current_y;
7645 int y1 = line_bottom_y (&it3);
7646 int line_height = y1 - y0;
7647
7648 /* If we did not reach target_y, try to move further backward if
7649 we can. If we moved too far backward, try to move forward. */
7650 if (target_y < it->current_y
7651 /* This is heuristic. In a window that's 3 lines high, with
7652 a line height of 13 pixels each, recentering with point
7653 on the bottom line will try to move -39/2 = 19 pixels
7654 backward. Try to avoid moving into the first line. */
7655 && (it->current_y - target_y
7656 > min (window_box_height (it->w), line_height * 2 / 3))
7657 && IT_CHARPOS (*it) > BEGV)
7658 {
7659 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7660 target_y - it->current_y));
7661 dy = it->current_y - target_y;
7662 goto move_further_back;
7663 }
7664 else if (target_y >= it->current_y + line_height
7665 && IT_CHARPOS (*it) < ZV)
7666 {
7667 /* Should move forward by at least one line, maybe more.
7668
7669 Note: Calling move_it_by_lines can be expensive on
7670 terminal frames, where compute_motion is used (via
7671 vmotion) to do the job, when there are very long lines
7672 and truncate-lines is nil. That's the reason for
7673 treating terminal frames specially here. */
7674
7675 if (!FRAME_WINDOW_P (it->f))
7676 move_it_vertically (it, target_y - (it->current_y + line_height));
7677 else
7678 {
7679 do
7680 {
7681 move_it_by_lines (it, 1, 1);
7682 }
7683 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7684 }
7685 }
7686 }
7687 }
7688
7689
7690 /* Move IT by a specified amount of pixel lines DY. DY negative means
7691 move backwards. DY = 0 means move to start of screen line. At the
7692 end, IT will be on the start of a screen line. */
7693
7694 void
7695 move_it_vertically (it, dy)
7696 struct it *it;
7697 int dy;
7698 {
7699 if (dy <= 0)
7700 move_it_vertically_backward (it, -dy);
7701 else
7702 {
7703 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7704 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7705 MOVE_TO_POS | MOVE_TO_Y);
7706 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7707
7708 /* If buffer ends in ZV without a newline, move to the start of
7709 the line to satisfy the post-condition. */
7710 if (IT_CHARPOS (*it) == ZV
7711 && ZV > BEGV
7712 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7713 move_it_by_lines (it, 0, 0);
7714 }
7715 }
7716
7717
7718 /* Move iterator IT past the end of the text line it is in. */
7719
7720 void
7721 move_it_past_eol (it)
7722 struct it *it;
7723 {
7724 enum move_it_result rc;
7725
7726 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7727 if (rc == MOVE_NEWLINE_OR_CR)
7728 set_iterator_to_next (it, 0);
7729 }
7730
7731
7732 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7733 negative means move up. DVPOS == 0 means move to the start of the
7734 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7735 NEED_Y_P is zero, IT->current_y will be left unchanged.
7736
7737 Further optimization ideas: If we would know that IT->f doesn't use
7738 a face with proportional font, we could be faster for
7739 truncate-lines nil. */
7740
7741 void
7742 move_it_by_lines (it, dvpos, need_y_p)
7743 struct it *it;
7744 int dvpos, need_y_p;
7745 {
7746 struct position pos;
7747
7748 /* The commented-out optimization uses vmotion on terminals. This
7749 gives bad results, because elements like it->what, on which
7750 callers such as pos_visible_p rely, aren't updated. */
7751 /* if (!FRAME_WINDOW_P (it->f))
7752 {
7753 struct text_pos textpos;
7754
7755 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7756 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7757 reseat (it, textpos, 1);
7758 it->vpos += pos.vpos;
7759 it->current_y += pos.vpos;
7760 }
7761 else */
7762
7763 if (dvpos == 0)
7764 {
7765 /* DVPOS == 0 means move to the start of the screen line. */
7766 move_it_vertically_backward (it, 0);
7767 xassert (it->current_x == 0 && it->hpos == 0);
7768 /* Let next call to line_bottom_y calculate real line height */
7769 last_height = 0;
7770 }
7771 else if (dvpos > 0)
7772 {
7773 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7774 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7775 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7776 }
7777 else
7778 {
7779 struct it it2;
7780 int start_charpos, i;
7781
7782 /* Start at the beginning of the screen line containing IT's
7783 position. This may actually move vertically backwards,
7784 in case of overlays, so adjust dvpos accordingly. */
7785 dvpos += it->vpos;
7786 move_it_vertically_backward (it, 0);
7787 dvpos -= it->vpos;
7788
7789 /* Go back -DVPOS visible lines and reseat the iterator there. */
7790 start_charpos = IT_CHARPOS (*it);
7791 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7792 back_to_previous_visible_line_start (it);
7793 reseat (it, it->current.pos, 1);
7794
7795 /* Move further back if we end up in a string or an image. */
7796 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7797 {
7798 /* First try to move to start of display line. */
7799 dvpos += it->vpos;
7800 move_it_vertically_backward (it, 0);
7801 dvpos -= it->vpos;
7802 if (IT_POS_VALID_AFTER_MOVE_P (it))
7803 break;
7804 /* If start of line is still in string or image,
7805 move further back. */
7806 back_to_previous_visible_line_start (it);
7807 reseat (it, it->current.pos, 1);
7808 dvpos--;
7809 }
7810
7811 it->current_x = it->hpos = 0;
7812
7813 /* Above call may have moved too far if continuation lines
7814 are involved. Scan forward and see if it did. */
7815 it2 = *it;
7816 it2.vpos = it2.current_y = 0;
7817 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7818 it->vpos -= it2.vpos;
7819 it->current_y -= it2.current_y;
7820 it->current_x = it->hpos = 0;
7821
7822 /* If we moved too far back, move IT some lines forward. */
7823 if (it2.vpos > -dvpos)
7824 {
7825 int delta = it2.vpos + dvpos;
7826 it2 = *it;
7827 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7828 /* Move back again if we got too far ahead. */
7829 if (IT_CHARPOS (*it) >= start_charpos)
7830 *it = it2;
7831 }
7832 }
7833 }
7834
7835 /* Return 1 if IT points into the middle of a display vector. */
7836
7837 int
7838 in_display_vector_p (it)
7839 struct it *it;
7840 {
7841 return (it->method == GET_FROM_DISPLAY_VECTOR
7842 && it->current.dpvec_index > 0
7843 && it->dpvec + it->current.dpvec_index != it->dpend);
7844 }
7845
7846 \f
7847 /***********************************************************************
7848 Messages
7849 ***********************************************************************/
7850
7851
7852 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7853 to *Messages*. */
7854
7855 void
7856 add_to_log (format, arg1, arg2)
7857 char *format;
7858 Lisp_Object arg1, arg2;
7859 {
7860 Lisp_Object args[3];
7861 Lisp_Object msg, fmt;
7862 char *buffer;
7863 int len;
7864 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7865 USE_SAFE_ALLOCA;
7866
7867 /* Do nothing if called asynchronously. Inserting text into
7868 a buffer may call after-change-functions and alike and
7869 that would means running Lisp asynchronously. */
7870 if (handling_signal)
7871 return;
7872
7873 fmt = msg = Qnil;
7874 GCPRO4 (fmt, msg, arg1, arg2);
7875
7876 args[0] = fmt = build_string (format);
7877 args[1] = arg1;
7878 args[2] = arg2;
7879 msg = Fformat (3, args);
7880
7881 len = SBYTES (msg) + 1;
7882 SAFE_ALLOCA (buffer, char *, len);
7883 bcopy (SDATA (msg), buffer, len);
7884
7885 message_dolog (buffer, len - 1, 1, 0);
7886 SAFE_FREE ();
7887
7888 UNGCPRO;
7889 }
7890
7891
7892 /* Output a newline in the *Messages* buffer if "needs" one. */
7893
7894 void
7895 message_log_maybe_newline ()
7896 {
7897 if (message_log_need_newline)
7898 message_dolog ("", 0, 1, 0);
7899 }
7900
7901
7902 /* Add a string M of length NBYTES to the message log, optionally
7903 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7904 nonzero, means interpret the contents of M as multibyte. This
7905 function calls low-level routines in order to bypass text property
7906 hooks, etc. which might not be safe to run.
7907
7908 This may GC (insert may run before/after change hooks),
7909 so the buffer M must NOT point to a Lisp string. */
7910
7911 void
7912 message_dolog (m, nbytes, nlflag, multibyte)
7913 const char *m;
7914 int nbytes, nlflag, multibyte;
7915 {
7916 if (!NILP (Vmemory_full))
7917 return;
7918
7919 if (!NILP (Vmessage_log_max))
7920 {
7921 struct buffer *oldbuf;
7922 Lisp_Object oldpoint, oldbegv, oldzv;
7923 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7924 int point_at_end = 0;
7925 int zv_at_end = 0;
7926 Lisp_Object old_deactivate_mark, tem;
7927 struct gcpro gcpro1;
7928
7929 old_deactivate_mark = Vdeactivate_mark;
7930 oldbuf = current_buffer;
7931 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7932 current_buffer->undo_list = Qt;
7933
7934 oldpoint = message_dolog_marker1;
7935 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7936 oldbegv = message_dolog_marker2;
7937 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7938 oldzv = message_dolog_marker3;
7939 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7940 GCPRO1 (old_deactivate_mark);
7941
7942 if (PT == Z)
7943 point_at_end = 1;
7944 if (ZV == Z)
7945 zv_at_end = 1;
7946
7947 BEGV = BEG;
7948 BEGV_BYTE = BEG_BYTE;
7949 ZV = Z;
7950 ZV_BYTE = Z_BYTE;
7951 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7952
7953 /* Insert the string--maybe converting multibyte to single byte
7954 or vice versa, so that all the text fits the buffer. */
7955 if (multibyte
7956 && NILP (current_buffer->enable_multibyte_characters))
7957 {
7958 int i, c, char_bytes;
7959 unsigned char work[1];
7960
7961 /* Convert a multibyte string to single-byte
7962 for the *Message* buffer. */
7963 for (i = 0; i < nbytes; i += char_bytes)
7964 {
7965 c = string_char_and_length (m + i, &char_bytes);
7966 work[0] = (ASCII_CHAR_P (c)
7967 ? c
7968 : multibyte_char_to_unibyte (c, Qnil));
7969 insert_1_both (work, 1, 1, 1, 0, 0);
7970 }
7971 }
7972 else if (! multibyte
7973 && ! NILP (current_buffer->enable_multibyte_characters))
7974 {
7975 int i, c, char_bytes;
7976 unsigned char *msg = (unsigned char *) m;
7977 unsigned char str[MAX_MULTIBYTE_LENGTH];
7978 /* Convert a single-byte string to multibyte
7979 for the *Message* buffer. */
7980 for (i = 0; i < nbytes; i++)
7981 {
7982 c = msg[i];
7983 MAKE_CHAR_MULTIBYTE (c);
7984 char_bytes = CHAR_STRING (c, str);
7985 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7986 }
7987 }
7988 else if (nbytes)
7989 insert_1 (m, nbytes, 1, 0, 0);
7990
7991 if (nlflag)
7992 {
7993 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7994 insert_1 ("\n", 1, 1, 0, 0);
7995
7996 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7997 this_bol = PT;
7998 this_bol_byte = PT_BYTE;
7999
8000 /* See if this line duplicates the previous one.
8001 If so, combine duplicates. */
8002 if (this_bol > BEG)
8003 {
8004 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8005 prev_bol = PT;
8006 prev_bol_byte = PT_BYTE;
8007
8008 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8009 this_bol, this_bol_byte);
8010 if (dup)
8011 {
8012 del_range_both (prev_bol, prev_bol_byte,
8013 this_bol, this_bol_byte, 0);
8014 if (dup > 1)
8015 {
8016 char dupstr[40];
8017 int duplen;
8018
8019 /* If you change this format, don't forget to also
8020 change message_log_check_duplicate. */
8021 sprintf (dupstr, " [%d times]", dup);
8022 duplen = strlen (dupstr);
8023 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8024 insert_1 (dupstr, duplen, 1, 0, 1);
8025 }
8026 }
8027 }
8028
8029 /* If we have more than the desired maximum number of lines
8030 in the *Messages* buffer now, delete the oldest ones.
8031 This is safe because we don't have undo in this buffer. */
8032
8033 if (NATNUMP (Vmessage_log_max))
8034 {
8035 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8036 -XFASTINT (Vmessage_log_max) - 1, 0);
8037 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8038 }
8039 }
8040 BEGV = XMARKER (oldbegv)->charpos;
8041 BEGV_BYTE = marker_byte_position (oldbegv);
8042
8043 if (zv_at_end)
8044 {
8045 ZV = Z;
8046 ZV_BYTE = Z_BYTE;
8047 }
8048 else
8049 {
8050 ZV = XMARKER (oldzv)->charpos;
8051 ZV_BYTE = marker_byte_position (oldzv);
8052 }
8053
8054 if (point_at_end)
8055 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8056 else
8057 /* We can't do Fgoto_char (oldpoint) because it will run some
8058 Lisp code. */
8059 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8060 XMARKER (oldpoint)->bytepos);
8061
8062 UNGCPRO;
8063 unchain_marker (XMARKER (oldpoint));
8064 unchain_marker (XMARKER (oldbegv));
8065 unchain_marker (XMARKER (oldzv));
8066
8067 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8068 set_buffer_internal (oldbuf);
8069 if (NILP (tem))
8070 windows_or_buffers_changed = old_windows_or_buffers_changed;
8071 message_log_need_newline = !nlflag;
8072 Vdeactivate_mark = old_deactivate_mark;
8073 }
8074 }
8075
8076
8077 /* We are at the end of the buffer after just having inserted a newline.
8078 (Note: We depend on the fact we won't be crossing the gap.)
8079 Check to see if the most recent message looks a lot like the previous one.
8080 Return 0 if different, 1 if the new one should just replace it, or a
8081 value N > 1 if we should also append " [N times]". */
8082
8083 static int
8084 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
8085 int prev_bol, this_bol;
8086 int prev_bol_byte, this_bol_byte;
8087 {
8088 int i;
8089 int len = Z_BYTE - 1 - this_bol_byte;
8090 int seen_dots = 0;
8091 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8092 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8093
8094 for (i = 0; i < len; i++)
8095 {
8096 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8097 seen_dots = 1;
8098 if (p1[i] != p2[i])
8099 return seen_dots;
8100 }
8101 p1 += len;
8102 if (*p1 == '\n')
8103 return 2;
8104 if (*p1++ == ' ' && *p1++ == '[')
8105 {
8106 int n = 0;
8107 while (*p1 >= '0' && *p1 <= '9')
8108 n = n * 10 + *p1++ - '0';
8109 if (strncmp (p1, " times]\n", 8) == 0)
8110 return n+1;
8111 }
8112 return 0;
8113 }
8114 \f
8115
8116 /* Display an echo area message M with a specified length of NBYTES
8117 bytes. The string may include null characters. If M is 0, clear
8118 out any existing message, and let the mini-buffer text show
8119 through.
8120
8121 This may GC, so the buffer M must NOT point to a Lisp string. */
8122
8123 void
8124 message2 (m, nbytes, multibyte)
8125 const char *m;
8126 int nbytes;
8127 int multibyte;
8128 {
8129 /* First flush out any partial line written with print. */
8130 message_log_maybe_newline ();
8131 if (m)
8132 message_dolog (m, nbytes, 1, multibyte);
8133 message2_nolog (m, nbytes, multibyte);
8134 }
8135
8136
8137 /* The non-logging counterpart of message2. */
8138
8139 void
8140 message2_nolog (m, nbytes, multibyte)
8141 const char *m;
8142 int nbytes, multibyte;
8143 {
8144 struct frame *sf = SELECTED_FRAME ();
8145 message_enable_multibyte = multibyte;
8146
8147 if (FRAME_INITIAL_P (sf))
8148 {
8149 if (noninteractive_need_newline)
8150 putc ('\n', stderr);
8151 noninteractive_need_newline = 0;
8152 if (m)
8153 fwrite (m, nbytes, 1, stderr);
8154 if (cursor_in_echo_area == 0)
8155 fprintf (stderr, "\n");
8156 fflush (stderr);
8157 }
8158 /* A null message buffer means that the frame hasn't really been
8159 initialized yet. Error messages get reported properly by
8160 cmd_error, so this must be just an informative message; toss it. */
8161 else if (INTERACTIVE
8162 && sf->glyphs_initialized_p
8163 && FRAME_MESSAGE_BUF (sf))
8164 {
8165 Lisp_Object mini_window;
8166 struct frame *f;
8167
8168 /* Get the frame containing the mini-buffer
8169 that the selected frame is using. */
8170 mini_window = FRAME_MINIBUF_WINDOW (sf);
8171 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8172
8173 FRAME_SAMPLE_VISIBILITY (f);
8174 if (FRAME_VISIBLE_P (sf)
8175 && ! FRAME_VISIBLE_P (f))
8176 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8177
8178 if (m)
8179 {
8180 set_message (m, Qnil, nbytes, multibyte);
8181 if (minibuffer_auto_raise)
8182 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8183 }
8184 else
8185 clear_message (1, 1);
8186
8187 do_pending_window_change (0);
8188 echo_area_display (1);
8189 do_pending_window_change (0);
8190 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8191 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8192 }
8193 }
8194
8195
8196 /* Display an echo area message M with a specified length of NBYTES
8197 bytes. The string may include null characters. If M is not a
8198 string, clear out any existing message, and let the mini-buffer
8199 text show through.
8200
8201 This function cancels echoing. */
8202
8203 void
8204 message3 (m, nbytes, multibyte)
8205 Lisp_Object m;
8206 int nbytes;
8207 int multibyte;
8208 {
8209 struct gcpro gcpro1;
8210
8211 GCPRO1 (m);
8212 clear_message (1,1);
8213 cancel_echoing ();
8214
8215 /* First flush out any partial line written with print. */
8216 message_log_maybe_newline ();
8217 if (STRINGP (m))
8218 {
8219 char *buffer;
8220 USE_SAFE_ALLOCA;
8221
8222 SAFE_ALLOCA (buffer, char *, nbytes);
8223 bcopy (SDATA (m), buffer, nbytes);
8224 message_dolog (buffer, nbytes, 1, multibyte);
8225 SAFE_FREE ();
8226 }
8227 message3_nolog (m, nbytes, multibyte);
8228
8229 UNGCPRO;
8230 }
8231
8232
8233 /* The non-logging version of message3.
8234 This does not cancel echoing, because it is used for echoing.
8235 Perhaps we need to make a separate function for echoing
8236 and make this cancel echoing. */
8237
8238 void
8239 message3_nolog (m, nbytes, multibyte)
8240 Lisp_Object m;
8241 int nbytes, multibyte;
8242 {
8243 struct frame *sf = SELECTED_FRAME ();
8244 message_enable_multibyte = multibyte;
8245
8246 if (FRAME_INITIAL_P (sf))
8247 {
8248 if (noninteractive_need_newline)
8249 putc ('\n', stderr);
8250 noninteractive_need_newline = 0;
8251 if (STRINGP (m))
8252 fwrite (SDATA (m), nbytes, 1, stderr);
8253 if (cursor_in_echo_area == 0)
8254 fprintf (stderr, "\n");
8255 fflush (stderr);
8256 }
8257 /* A null message buffer means that the frame hasn't really been
8258 initialized yet. Error messages get reported properly by
8259 cmd_error, so this must be just an informative message; toss it. */
8260 else if (INTERACTIVE
8261 && sf->glyphs_initialized_p
8262 && FRAME_MESSAGE_BUF (sf))
8263 {
8264 Lisp_Object mini_window;
8265 Lisp_Object frame;
8266 struct frame *f;
8267
8268 /* Get the frame containing the mini-buffer
8269 that the selected frame is using. */
8270 mini_window = FRAME_MINIBUF_WINDOW (sf);
8271 frame = XWINDOW (mini_window)->frame;
8272 f = XFRAME (frame);
8273
8274 FRAME_SAMPLE_VISIBILITY (f);
8275 if (FRAME_VISIBLE_P (sf)
8276 && !FRAME_VISIBLE_P (f))
8277 Fmake_frame_visible (frame);
8278
8279 if (STRINGP (m) && SCHARS (m) > 0)
8280 {
8281 set_message (NULL, m, nbytes, multibyte);
8282 if (minibuffer_auto_raise)
8283 Fraise_frame (frame);
8284 /* Assume we are not echoing.
8285 (If we are, echo_now will override this.) */
8286 echo_message_buffer = Qnil;
8287 }
8288 else
8289 clear_message (1, 1);
8290
8291 do_pending_window_change (0);
8292 echo_area_display (1);
8293 do_pending_window_change (0);
8294 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8295 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8296 }
8297 }
8298
8299
8300 /* Display a null-terminated echo area message M. If M is 0, clear
8301 out any existing message, and let the mini-buffer text show through.
8302
8303 The buffer M must continue to exist until after the echo area gets
8304 cleared or some other message gets displayed there. Do not pass
8305 text that is stored in a Lisp string. Do not pass text in a buffer
8306 that was alloca'd. */
8307
8308 void
8309 message1 (m)
8310 char *m;
8311 {
8312 message2 (m, (m ? strlen (m) : 0), 0);
8313 }
8314
8315
8316 /* The non-logging counterpart of message1. */
8317
8318 void
8319 message1_nolog (m)
8320 char *m;
8321 {
8322 message2_nolog (m, (m ? strlen (m) : 0), 0);
8323 }
8324
8325 /* Display a message M which contains a single %s
8326 which gets replaced with STRING. */
8327
8328 void
8329 message_with_string (m, string, log)
8330 char *m;
8331 Lisp_Object string;
8332 int log;
8333 {
8334 CHECK_STRING (string);
8335
8336 if (noninteractive)
8337 {
8338 if (m)
8339 {
8340 if (noninteractive_need_newline)
8341 putc ('\n', stderr);
8342 noninteractive_need_newline = 0;
8343 fprintf (stderr, m, SDATA (string));
8344 if (!cursor_in_echo_area)
8345 fprintf (stderr, "\n");
8346 fflush (stderr);
8347 }
8348 }
8349 else if (INTERACTIVE)
8350 {
8351 /* The frame whose minibuffer we're going to display the message on.
8352 It may be larger than the selected frame, so we need
8353 to use its buffer, not the selected frame's buffer. */
8354 Lisp_Object mini_window;
8355 struct frame *f, *sf = SELECTED_FRAME ();
8356
8357 /* Get the frame containing the minibuffer
8358 that the selected frame is using. */
8359 mini_window = FRAME_MINIBUF_WINDOW (sf);
8360 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8361
8362 /* A null message buffer means that the frame hasn't really been
8363 initialized yet. Error messages get reported properly by
8364 cmd_error, so this must be just an informative message; toss it. */
8365 if (FRAME_MESSAGE_BUF (f))
8366 {
8367 Lisp_Object args[2], message;
8368 struct gcpro gcpro1, gcpro2;
8369
8370 args[0] = build_string (m);
8371 args[1] = message = string;
8372 GCPRO2 (args[0], message);
8373 gcpro1.nvars = 2;
8374
8375 message = Fformat (2, args);
8376
8377 if (log)
8378 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8379 else
8380 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8381
8382 UNGCPRO;
8383
8384 /* Print should start at the beginning of the message
8385 buffer next time. */
8386 message_buf_print = 0;
8387 }
8388 }
8389 }
8390
8391
8392 /* Dump an informative message to the minibuf. If M is 0, clear out
8393 any existing message, and let the mini-buffer text show through. */
8394
8395 /* VARARGS 1 */
8396 void
8397 message (m, a1, a2, a3)
8398 char *m;
8399 EMACS_INT a1, a2, a3;
8400 {
8401 if (noninteractive)
8402 {
8403 if (m)
8404 {
8405 if (noninteractive_need_newline)
8406 putc ('\n', stderr);
8407 noninteractive_need_newline = 0;
8408 fprintf (stderr, m, a1, a2, a3);
8409 if (cursor_in_echo_area == 0)
8410 fprintf (stderr, "\n");
8411 fflush (stderr);
8412 }
8413 }
8414 else if (INTERACTIVE)
8415 {
8416 /* The frame whose mini-buffer we're going to display the message
8417 on. It may be larger than the selected frame, so we need to
8418 use its buffer, not the selected frame's buffer. */
8419 Lisp_Object mini_window;
8420 struct frame *f, *sf = SELECTED_FRAME ();
8421
8422 /* Get the frame containing the mini-buffer
8423 that the selected frame is using. */
8424 mini_window = FRAME_MINIBUF_WINDOW (sf);
8425 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8426
8427 /* A null message buffer means that the frame hasn't really been
8428 initialized yet. Error messages get reported properly by
8429 cmd_error, so this must be just an informative message; toss
8430 it. */
8431 if (FRAME_MESSAGE_BUF (f))
8432 {
8433 if (m)
8434 {
8435 int len;
8436 #ifdef NO_ARG_ARRAY
8437 char *a[3];
8438 a[0] = (char *) a1;
8439 a[1] = (char *) a2;
8440 a[2] = (char *) a3;
8441
8442 len = doprnt (FRAME_MESSAGE_BUF (f),
8443 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8444 #else
8445 len = doprnt (FRAME_MESSAGE_BUF (f),
8446 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8447 (char **) &a1);
8448 #endif /* NO_ARG_ARRAY */
8449
8450 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8451 }
8452 else
8453 message1 (0);
8454
8455 /* Print should start at the beginning of the message
8456 buffer next time. */
8457 message_buf_print = 0;
8458 }
8459 }
8460 }
8461
8462
8463 /* The non-logging version of message. */
8464
8465 void
8466 message_nolog (m, a1, a2, a3)
8467 char *m;
8468 EMACS_INT a1, a2, a3;
8469 {
8470 Lisp_Object old_log_max;
8471 old_log_max = Vmessage_log_max;
8472 Vmessage_log_max = Qnil;
8473 message (m, a1, a2, a3);
8474 Vmessage_log_max = old_log_max;
8475 }
8476
8477
8478 /* Display the current message in the current mini-buffer. This is
8479 only called from error handlers in process.c, and is not time
8480 critical. */
8481
8482 void
8483 update_echo_area ()
8484 {
8485 if (!NILP (echo_area_buffer[0]))
8486 {
8487 Lisp_Object string;
8488 string = Fcurrent_message ();
8489 message3 (string, SBYTES (string),
8490 !NILP (current_buffer->enable_multibyte_characters));
8491 }
8492 }
8493
8494
8495 /* Make sure echo area buffers in `echo_buffers' are live.
8496 If they aren't, make new ones. */
8497
8498 static void
8499 ensure_echo_area_buffers ()
8500 {
8501 int i;
8502
8503 for (i = 0; i < 2; ++i)
8504 if (!BUFFERP (echo_buffer[i])
8505 || NILP (XBUFFER (echo_buffer[i])->name))
8506 {
8507 char name[30];
8508 Lisp_Object old_buffer;
8509 int j;
8510
8511 old_buffer = echo_buffer[i];
8512 sprintf (name, " *Echo Area %d*", i);
8513 echo_buffer[i] = Fget_buffer_create (build_string (name));
8514 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8515 /* to force word wrap in echo area -
8516 it was decided to postpone this*/
8517 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8518
8519 for (j = 0; j < 2; ++j)
8520 if (EQ (old_buffer, echo_area_buffer[j]))
8521 echo_area_buffer[j] = echo_buffer[i];
8522 }
8523 }
8524
8525
8526 /* Call FN with args A1..A4 with either the current or last displayed
8527 echo_area_buffer as current buffer.
8528
8529 WHICH zero means use the current message buffer
8530 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8531 from echo_buffer[] and clear it.
8532
8533 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8534 suitable buffer from echo_buffer[] and clear it.
8535
8536 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8537 that the current message becomes the last displayed one, make
8538 choose a suitable buffer for echo_area_buffer[0], and clear it.
8539
8540 Value is what FN returns. */
8541
8542 static int
8543 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8544 struct window *w;
8545 int which;
8546 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8547 EMACS_INT a1;
8548 Lisp_Object a2;
8549 EMACS_INT a3, a4;
8550 {
8551 Lisp_Object buffer;
8552 int this_one, the_other, clear_buffer_p, rc;
8553 int count = SPECPDL_INDEX ();
8554
8555 /* If buffers aren't live, make new ones. */
8556 ensure_echo_area_buffers ();
8557
8558 clear_buffer_p = 0;
8559
8560 if (which == 0)
8561 this_one = 0, the_other = 1;
8562 else if (which > 0)
8563 this_one = 1, the_other = 0;
8564 else
8565 {
8566 this_one = 0, the_other = 1;
8567 clear_buffer_p = 1;
8568
8569 /* We need a fresh one in case the current echo buffer equals
8570 the one containing the last displayed echo area message. */
8571 if (!NILP (echo_area_buffer[this_one])
8572 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8573 echo_area_buffer[this_one] = Qnil;
8574 }
8575
8576 /* Choose a suitable buffer from echo_buffer[] is we don't
8577 have one. */
8578 if (NILP (echo_area_buffer[this_one]))
8579 {
8580 echo_area_buffer[this_one]
8581 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8582 ? echo_buffer[the_other]
8583 : echo_buffer[this_one]);
8584 clear_buffer_p = 1;
8585 }
8586
8587 buffer = echo_area_buffer[this_one];
8588
8589 /* Don't get confused by reusing the buffer used for echoing
8590 for a different purpose. */
8591 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8592 cancel_echoing ();
8593
8594 record_unwind_protect (unwind_with_echo_area_buffer,
8595 with_echo_area_buffer_unwind_data (w));
8596
8597 /* Make the echo area buffer current. Note that for display
8598 purposes, it is not necessary that the displayed window's buffer
8599 == current_buffer, except for text property lookup. So, let's
8600 only set that buffer temporarily here without doing a full
8601 Fset_window_buffer. We must also change w->pointm, though,
8602 because otherwise an assertions in unshow_buffer fails, and Emacs
8603 aborts. */
8604 set_buffer_internal_1 (XBUFFER (buffer));
8605 if (w)
8606 {
8607 w->buffer = buffer;
8608 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8609 }
8610
8611 current_buffer->undo_list = Qt;
8612 current_buffer->read_only = Qnil;
8613 specbind (Qinhibit_read_only, Qt);
8614 specbind (Qinhibit_modification_hooks, Qt);
8615
8616 if (clear_buffer_p && Z > BEG)
8617 del_range (BEG, Z);
8618
8619 xassert (BEGV >= BEG);
8620 xassert (ZV <= Z && ZV >= BEGV);
8621
8622 rc = fn (a1, a2, a3, a4);
8623
8624 xassert (BEGV >= BEG);
8625 xassert (ZV <= Z && ZV >= BEGV);
8626
8627 unbind_to (count, Qnil);
8628 return rc;
8629 }
8630
8631
8632 /* Save state that should be preserved around the call to the function
8633 FN called in with_echo_area_buffer. */
8634
8635 static Lisp_Object
8636 with_echo_area_buffer_unwind_data (w)
8637 struct window *w;
8638 {
8639 int i = 0;
8640 Lisp_Object vector, tmp;
8641
8642 /* Reduce consing by keeping one vector in
8643 Vwith_echo_area_save_vector. */
8644 vector = Vwith_echo_area_save_vector;
8645 Vwith_echo_area_save_vector = Qnil;
8646
8647 if (NILP (vector))
8648 vector = Fmake_vector (make_number (7), Qnil);
8649
8650 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8651 ASET (vector, i, Vdeactivate_mark); ++i;
8652 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8653
8654 if (w)
8655 {
8656 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8657 ASET (vector, i, w->buffer); ++i;
8658 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8659 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8660 }
8661 else
8662 {
8663 int end = i + 4;
8664 for (; i < end; ++i)
8665 ASET (vector, i, Qnil);
8666 }
8667
8668 xassert (i == ASIZE (vector));
8669 return vector;
8670 }
8671
8672
8673 /* Restore global state from VECTOR which was created by
8674 with_echo_area_buffer_unwind_data. */
8675
8676 static Lisp_Object
8677 unwind_with_echo_area_buffer (vector)
8678 Lisp_Object vector;
8679 {
8680 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8681 Vdeactivate_mark = AREF (vector, 1);
8682 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8683
8684 if (WINDOWP (AREF (vector, 3)))
8685 {
8686 struct window *w;
8687 Lisp_Object buffer, charpos, bytepos;
8688
8689 w = XWINDOW (AREF (vector, 3));
8690 buffer = AREF (vector, 4);
8691 charpos = AREF (vector, 5);
8692 bytepos = AREF (vector, 6);
8693
8694 w->buffer = buffer;
8695 set_marker_both (w->pointm, buffer,
8696 XFASTINT (charpos), XFASTINT (bytepos));
8697 }
8698
8699 Vwith_echo_area_save_vector = vector;
8700 return Qnil;
8701 }
8702
8703
8704 /* Set up the echo area for use by print functions. MULTIBYTE_P
8705 non-zero means we will print multibyte. */
8706
8707 void
8708 setup_echo_area_for_printing (multibyte_p)
8709 int multibyte_p;
8710 {
8711 /* If we can't find an echo area any more, exit. */
8712 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8713 Fkill_emacs (Qnil);
8714
8715 ensure_echo_area_buffers ();
8716
8717 if (!message_buf_print)
8718 {
8719 /* A message has been output since the last time we printed.
8720 Choose a fresh echo area buffer. */
8721 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8722 echo_area_buffer[0] = echo_buffer[1];
8723 else
8724 echo_area_buffer[0] = echo_buffer[0];
8725
8726 /* Switch to that buffer and clear it. */
8727 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8728 current_buffer->truncate_lines = Qnil;
8729
8730 if (Z > BEG)
8731 {
8732 int count = SPECPDL_INDEX ();
8733 specbind (Qinhibit_read_only, Qt);
8734 /* Note that undo recording is always disabled. */
8735 del_range (BEG, Z);
8736 unbind_to (count, Qnil);
8737 }
8738 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8739
8740 /* Set up the buffer for the multibyteness we need. */
8741 if (multibyte_p
8742 != !NILP (current_buffer->enable_multibyte_characters))
8743 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8744
8745 /* Raise the frame containing the echo area. */
8746 if (minibuffer_auto_raise)
8747 {
8748 struct frame *sf = SELECTED_FRAME ();
8749 Lisp_Object mini_window;
8750 mini_window = FRAME_MINIBUF_WINDOW (sf);
8751 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8752 }
8753
8754 message_log_maybe_newline ();
8755 message_buf_print = 1;
8756 }
8757 else
8758 {
8759 if (NILP (echo_area_buffer[0]))
8760 {
8761 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8762 echo_area_buffer[0] = echo_buffer[1];
8763 else
8764 echo_area_buffer[0] = echo_buffer[0];
8765 }
8766
8767 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8768 {
8769 /* Someone switched buffers between print requests. */
8770 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8771 current_buffer->truncate_lines = Qnil;
8772 }
8773 }
8774 }
8775
8776
8777 /* Display an echo area message in window W. Value is non-zero if W's
8778 height is changed. If display_last_displayed_message_p is
8779 non-zero, display the message that was last displayed, otherwise
8780 display the current message. */
8781
8782 static int
8783 display_echo_area (w)
8784 struct window *w;
8785 {
8786 int i, no_message_p, window_height_changed_p, count;
8787
8788 /* Temporarily disable garbage collections while displaying the echo
8789 area. This is done because a GC can print a message itself.
8790 That message would modify the echo area buffer's contents while a
8791 redisplay of the buffer is going on, and seriously confuse
8792 redisplay. */
8793 count = inhibit_garbage_collection ();
8794
8795 /* If there is no message, we must call display_echo_area_1
8796 nevertheless because it resizes the window. But we will have to
8797 reset the echo_area_buffer in question to nil at the end because
8798 with_echo_area_buffer will sets it to an empty buffer. */
8799 i = display_last_displayed_message_p ? 1 : 0;
8800 no_message_p = NILP (echo_area_buffer[i]);
8801
8802 window_height_changed_p
8803 = with_echo_area_buffer (w, display_last_displayed_message_p,
8804 display_echo_area_1,
8805 (EMACS_INT) w, Qnil, 0, 0);
8806
8807 if (no_message_p)
8808 echo_area_buffer[i] = Qnil;
8809
8810 unbind_to (count, Qnil);
8811 return window_height_changed_p;
8812 }
8813
8814
8815 /* Helper for display_echo_area. Display the current buffer which
8816 contains the current echo area message in window W, a mini-window,
8817 a pointer to which is passed in A1. A2..A4 are currently not used.
8818 Change the height of W so that all of the message is displayed.
8819 Value is non-zero if height of W was changed. */
8820
8821 static int
8822 display_echo_area_1 (a1, a2, a3, a4)
8823 EMACS_INT a1;
8824 Lisp_Object a2;
8825 EMACS_INT a3, a4;
8826 {
8827 struct window *w = (struct window *) a1;
8828 Lisp_Object window;
8829 struct text_pos start;
8830 int window_height_changed_p = 0;
8831
8832 /* Do this before displaying, so that we have a large enough glyph
8833 matrix for the display. If we can't get enough space for the
8834 whole text, display the last N lines. That works by setting w->start. */
8835 window_height_changed_p = resize_mini_window (w, 0);
8836
8837 /* Use the starting position chosen by resize_mini_window. */
8838 SET_TEXT_POS_FROM_MARKER (start, w->start);
8839
8840 /* Display. */
8841 clear_glyph_matrix (w->desired_matrix);
8842 XSETWINDOW (window, w);
8843 try_window (window, start, 0);
8844
8845 return window_height_changed_p;
8846 }
8847
8848
8849 /* Resize the echo area window to exactly the size needed for the
8850 currently displayed message, if there is one. If a mini-buffer
8851 is active, don't shrink it. */
8852
8853 void
8854 resize_echo_area_exactly ()
8855 {
8856 if (BUFFERP (echo_area_buffer[0])
8857 && WINDOWP (echo_area_window))
8858 {
8859 struct window *w = XWINDOW (echo_area_window);
8860 int resized_p;
8861 Lisp_Object resize_exactly;
8862
8863 if (minibuf_level == 0)
8864 resize_exactly = Qt;
8865 else
8866 resize_exactly = Qnil;
8867
8868 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8869 (EMACS_INT) w, resize_exactly, 0, 0);
8870 if (resized_p)
8871 {
8872 ++windows_or_buffers_changed;
8873 ++update_mode_lines;
8874 redisplay_internal (0);
8875 }
8876 }
8877 }
8878
8879
8880 /* Callback function for with_echo_area_buffer, when used from
8881 resize_echo_area_exactly. A1 contains a pointer to the window to
8882 resize, EXACTLY non-nil means resize the mini-window exactly to the
8883 size of the text displayed. A3 and A4 are not used. Value is what
8884 resize_mini_window returns. */
8885
8886 static int
8887 resize_mini_window_1 (a1, exactly, a3, a4)
8888 EMACS_INT a1;
8889 Lisp_Object exactly;
8890 EMACS_INT a3, a4;
8891 {
8892 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8893 }
8894
8895
8896 /* Resize mini-window W to fit the size of its contents. EXACT_P
8897 means size the window exactly to the size needed. Otherwise, it's
8898 only enlarged until W's buffer is empty.
8899
8900 Set W->start to the right place to begin display. If the whole
8901 contents fit, start at the beginning. Otherwise, start so as
8902 to make the end of the contents appear. This is particularly
8903 important for y-or-n-p, but seems desirable generally.
8904
8905 Value is non-zero if the window height has been changed. */
8906
8907 int
8908 resize_mini_window (w, exact_p)
8909 struct window *w;
8910 int exact_p;
8911 {
8912 struct frame *f = XFRAME (w->frame);
8913 int window_height_changed_p = 0;
8914
8915 xassert (MINI_WINDOW_P (w));
8916
8917 /* By default, start display at the beginning. */
8918 set_marker_both (w->start, w->buffer,
8919 BUF_BEGV (XBUFFER (w->buffer)),
8920 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8921
8922 /* Don't resize windows while redisplaying a window; it would
8923 confuse redisplay functions when the size of the window they are
8924 displaying changes from under them. Such a resizing can happen,
8925 for instance, when which-func prints a long message while
8926 we are running fontification-functions. We're running these
8927 functions with safe_call which binds inhibit-redisplay to t. */
8928 if (!NILP (Vinhibit_redisplay))
8929 return 0;
8930
8931 /* Nil means don't try to resize. */
8932 if (NILP (Vresize_mini_windows)
8933 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8934 return 0;
8935
8936 if (!FRAME_MINIBUF_ONLY_P (f))
8937 {
8938 struct it it;
8939 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8940 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8941 int height, max_height;
8942 int unit = FRAME_LINE_HEIGHT (f);
8943 struct text_pos start;
8944 struct buffer *old_current_buffer = NULL;
8945
8946 if (current_buffer != XBUFFER (w->buffer))
8947 {
8948 old_current_buffer = current_buffer;
8949 set_buffer_internal (XBUFFER (w->buffer));
8950 }
8951
8952 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8953
8954 /* Compute the max. number of lines specified by the user. */
8955 if (FLOATP (Vmax_mini_window_height))
8956 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8957 else if (INTEGERP (Vmax_mini_window_height))
8958 max_height = XINT (Vmax_mini_window_height);
8959 else
8960 max_height = total_height / 4;
8961
8962 /* Correct that max. height if it's bogus. */
8963 max_height = max (1, max_height);
8964 max_height = min (total_height, max_height);
8965
8966 /* Find out the height of the text in the window. */
8967 if (it.line_wrap == TRUNCATE)
8968 height = 1;
8969 else
8970 {
8971 last_height = 0;
8972 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8973 if (it.max_ascent == 0 && it.max_descent == 0)
8974 height = it.current_y + last_height;
8975 else
8976 height = it.current_y + it.max_ascent + it.max_descent;
8977 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8978 height = (height + unit - 1) / unit;
8979 }
8980
8981 /* Compute a suitable window start. */
8982 if (height > max_height)
8983 {
8984 height = max_height;
8985 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8986 move_it_vertically_backward (&it, (height - 1) * unit);
8987 start = it.current.pos;
8988 }
8989 else
8990 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8991 SET_MARKER_FROM_TEXT_POS (w->start, start);
8992
8993 if (EQ (Vresize_mini_windows, Qgrow_only))
8994 {
8995 /* Let it grow only, until we display an empty message, in which
8996 case the window shrinks again. */
8997 if (height > WINDOW_TOTAL_LINES (w))
8998 {
8999 int old_height = WINDOW_TOTAL_LINES (w);
9000 freeze_window_starts (f, 1);
9001 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9002 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9003 }
9004 else if (height < WINDOW_TOTAL_LINES (w)
9005 && (exact_p || BEGV == ZV))
9006 {
9007 int old_height = WINDOW_TOTAL_LINES (w);
9008 freeze_window_starts (f, 0);
9009 shrink_mini_window (w);
9010 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9011 }
9012 }
9013 else
9014 {
9015 /* Always resize to exact size needed. */
9016 if (height > WINDOW_TOTAL_LINES (w))
9017 {
9018 int old_height = WINDOW_TOTAL_LINES (w);
9019 freeze_window_starts (f, 1);
9020 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9021 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9022 }
9023 else if (height < WINDOW_TOTAL_LINES (w))
9024 {
9025 int old_height = WINDOW_TOTAL_LINES (w);
9026 freeze_window_starts (f, 0);
9027 shrink_mini_window (w);
9028
9029 if (height)
9030 {
9031 freeze_window_starts (f, 1);
9032 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9033 }
9034
9035 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9036 }
9037 }
9038
9039 if (old_current_buffer)
9040 set_buffer_internal (old_current_buffer);
9041 }
9042
9043 return window_height_changed_p;
9044 }
9045
9046
9047 /* Value is the current message, a string, or nil if there is no
9048 current message. */
9049
9050 Lisp_Object
9051 current_message ()
9052 {
9053 Lisp_Object msg;
9054
9055 if (!BUFFERP (echo_area_buffer[0]))
9056 msg = Qnil;
9057 else
9058 {
9059 with_echo_area_buffer (0, 0, current_message_1,
9060 (EMACS_INT) &msg, Qnil, 0, 0);
9061 if (NILP (msg))
9062 echo_area_buffer[0] = Qnil;
9063 }
9064
9065 return msg;
9066 }
9067
9068
9069 static int
9070 current_message_1 (a1, a2, a3, a4)
9071 EMACS_INT a1;
9072 Lisp_Object a2;
9073 EMACS_INT a3, a4;
9074 {
9075 Lisp_Object *msg = (Lisp_Object *) a1;
9076
9077 if (Z > BEG)
9078 *msg = make_buffer_string (BEG, Z, 1);
9079 else
9080 *msg = Qnil;
9081 return 0;
9082 }
9083
9084
9085 /* Push the current message on Vmessage_stack for later restauration
9086 by restore_message. Value is non-zero if the current message isn't
9087 empty. This is a relatively infrequent operation, so it's not
9088 worth optimizing. */
9089
9090 int
9091 push_message ()
9092 {
9093 Lisp_Object msg;
9094 msg = current_message ();
9095 Vmessage_stack = Fcons (msg, Vmessage_stack);
9096 return STRINGP (msg);
9097 }
9098
9099
9100 /* Restore message display from the top of Vmessage_stack. */
9101
9102 void
9103 restore_message ()
9104 {
9105 Lisp_Object msg;
9106
9107 xassert (CONSP (Vmessage_stack));
9108 msg = XCAR (Vmessage_stack);
9109 if (STRINGP (msg))
9110 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9111 else
9112 message3_nolog (msg, 0, 0);
9113 }
9114
9115
9116 /* Handler for record_unwind_protect calling pop_message. */
9117
9118 Lisp_Object
9119 pop_message_unwind (dummy)
9120 Lisp_Object dummy;
9121 {
9122 pop_message ();
9123 return Qnil;
9124 }
9125
9126 /* Pop the top-most entry off Vmessage_stack. */
9127
9128 void
9129 pop_message ()
9130 {
9131 xassert (CONSP (Vmessage_stack));
9132 Vmessage_stack = XCDR (Vmessage_stack);
9133 }
9134
9135
9136 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9137 exits. If the stack is not empty, we have a missing pop_message
9138 somewhere. */
9139
9140 void
9141 check_message_stack ()
9142 {
9143 if (!NILP (Vmessage_stack))
9144 abort ();
9145 }
9146
9147
9148 /* Truncate to NCHARS what will be displayed in the echo area the next
9149 time we display it---but don't redisplay it now. */
9150
9151 void
9152 truncate_echo_area (nchars)
9153 int nchars;
9154 {
9155 if (nchars == 0)
9156 echo_area_buffer[0] = Qnil;
9157 /* A null message buffer means that the frame hasn't really been
9158 initialized yet. Error messages get reported properly by
9159 cmd_error, so this must be just an informative message; toss it. */
9160 else if (!noninteractive
9161 && INTERACTIVE
9162 && !NILP (echo_area_buffer[0]))
9163 {
9164 struct frame *sf = SELECTED_FRAME ();
9165 if (FRAME_MESSAGE_BUF (sf))
9166 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9167 }
9168 }
9169
9170
9171 /* Helper function for truncate_echo_area. Truncate the current
9172 message to at most NCHARS characters. */
9173
9174 static int
9175 truncate_message_1 (nchars, a2, a3, a4)
9176 EMACS_INT nchars;
9177 Lisp_Object a2;
9178 EMACS_INT a3, a4;
9179 {
9180 if (BEG + nchars < Z)
9181 del_range (BEG + nchars, Z);
9182 if (Z == BEG)
9183 echo_area_buffer[0] = Qnil;
9184 return 0;
9185 }
9186
9187
9188 /* Set the current message to a substring of S or STRING.
9189
9190 If STRING is a Lisp string, set the message to the first NBYTES
9191 bytes from STRING. NBYTES zero means use the whole string. If
9192 STRING is multibyte, the message will be displayed multibyte.
9193
9194 If S is not null, set the message to the first LEN bytes of S. LEN
9195 zero means use the whole string. MULTIBYTE_P non-zero means S is
9196 multibyte. Display the message multibyte in that case.
9197
9198 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9199 to t before calling set_message_1 (which calls insert).
9200 */
9201
9202 void
9203 set_message (s, string, nbytes, multibyte_p)
9204 const char *s;
9205 Lisp_Object string;
9206 int nbytes, multibyte_p;
9207 {
9208 message_enable_multibyte
9209 = ((s && multibyte_p)
9210 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9211
9212 with_echo_area_buffer (0, -1, set_message_1,
9213 (EMACS_INT) s, string, nbytes, multibyte_p);
9214 message_buf_print = 0;
9215 help_echo_showing_p = 0;
9216 }
9217
9218
9219 /* Helper function for set_message. Arguments have the same meaning
9220 as there, with A1 corresponding to S and A2 corresponding to STRING
9221 This function is called with the echo area buffer being
9222 current. */
9223
9224 static int
9225 set_message_1 (a1, a2, nbytes, multibyte_p)
9226 EMACS_INT a1;
9227 Lisp_Object a2;
9228 EMACS_INT nbytes, multibyte_p;
9229 {
9230 const char *s = (const char *) a1;
9231 Lisp_Object string = a2;
9232
9233 /* Change multibyteness of the echo buffer appropriately. */
9234 if (message_enable_multibyte
9235 != !NILP (current_buffer->enable_multibyte_characters))
9236 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9237
9238 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9239
9240 /* Insert new message at BEG. */
9241 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9242
9243 if (STRINGP (string))
9244 {
9245 int nchars;
9246
9247 if (nbytes == 0)
9248 nbytes = SBYTES (string);
9249 nchars = string_byte_to_char (string, nbytes);
9250
9251 /* This function takes care of single/multibyte conversion. We
9252 just have to ensure that the echo area buffer has the right
9253 setting of enable_multibyte_characters. */
9254 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9255 }
9256 else if (s)
9257 {
9258 if (nbytes == 0)
9259 nbytes = strlen (s);
9260
9261 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9262 {
9263 /* Convert from multi-byte to single-byte. */
9264 int i, c, n;
9265 unsigned char work[1];
9266
9267 /* Convert a multibyte string to single-byte. */
9268 for (i = 0; i < nbytes; i += n)
9269 {
9270 c = string_char_and_length (s + i, &n);
9271 work[0] = (ASCII_CHAR_P (c)
9272 ? c
9273 : multibyte_char_to_unibyte (c, Qnil));
9274 insert_1_both (work, 1, 1, 1, 0, 0);
9275 }
9276 }
9277 else if (!multibyte_p
9278 && !NILP (current_buffer->enable_multibyte_characters))
9279 {
9280 /* Convert from single-byte to multi-byte. */
9281 int i, c, n;
9282 const unsigned char *msg = (const unsigned char *) s;
9283 unsigned char str[MAX_MULTIBYTE_LENGTH];
9284
9285 /* Convert a single-byte string to multibyte. */
9286 for (i = 0; i < nbytes; i++)
9287 {
9288 c = msg[i];
9289 MAKE_CHAR_MULTIBYTE (c);
9290 n = CHAR_STRING (c, str);
9291 insert_1_both (str, 1, n, 1, 0, 0);
9292 }
9293 }
9294 else
9295 insert_1 (s, nbytes, 1, 0, 0);
9296 }
9297
9298 return 0;
9299 }
9300
9301
9302 /* Clear messages. CURRENT_P non-zero means clear the current
9303 message. LAST_DISPLAYED_P non-zero means clear the message
9304 last displayed. */
9305
9306 void
9307 clear_message (current_p, last_displayed_p)
9308 int current_p, last_displayed_p;
9309 {
9310 if (current_p)
9311 {
9312 echo_area_buffer[0] = Qnil;
9313 message_cleared_p = 1;
9314 }
9315
9316 if (last_displayed_p)
9317 echo_area_buffer[1] = Qnil;
9318
9319 message_buf_print = 0;
9320 }
9321
9322 /* Clear garbaged frames.
9323
9324 This function is used where the old redisplay called
9325 redraw_garbaged_frames which in turn called redraw_frame which in
9326 turn called clear_frame. The call to clear_frame was a source of
9327 flickering. I believe a clear_frame is not necessary. It should
9328 suffice in the new redisplay to invalidate all current matrices,
9329 and ensure a complete redisplay of all windows. */
9330
9331 static void
9332 clear_garbaged_frames ()
9333 {
9334 if (frame_garbaged)
9335 {
9336 Lisp_Object tail, frame;
9337 int changed_count = 0;
9338
9339 FOR_EACH_FRAME (tail, frame)
9340 {
9341 struct frame *f = XFRAME (frame);
9342
9343 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9344 {
9345 if (f->resized_p)
9346 {
9347 Fredraw_frame (frame);
9348 f->force_flush_display_p = 1;
9349 }
9350 clear_current_matrices (f);
9351 changed_count++;
9352 f->garbaged = 0;
9353 f->resized_p = 0;
9354 }
9355 }
9356
9357 frame_garbaged = 0;
9358 if (changed_count)
9359 ++windows_or_buffers_changed;
9360 }
9361 }
9362
9363
9364 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9365 is non-zero update selected_frame. Value is non-zero if the
9366 mini-windows height has been changed. */
9367
9368 static int
9369 echo_area_display (update_frame_p)
9370 int update_frame_p;
9371 {
9372 Lisp_Object mini_window;
9373 struct window *w;
9374 struct frame *f;
9375 int window_height_changed_p = 0;
9376 struct frame *sf = SELECTED_FRAME ();
9377
9378 mini_window = FRAME_MINIBUF_WINDOW (sf);
9379 w = XWINDOW (mini_window);
9380 f = XFRAME (WINDOW_FRAME (w));
9381
9382 /* Don't display if frame is invisible or not yet initialized. */
9383 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9384 return 0;
9385
9386 #ifdef HAVE_WINDOW_SYSTEM
9387 /* When Emacs starts, selected_frame may be the initial terminal
9388 frame. If we let this through, a message would be displayed on
9389 the terminal. */
9390 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9391 return 0;
9392 #endif /* HAVE_WINDOW_SYSTEM */
9393
9394 /* Redraw garbaged frames. */
9395 if (frame_garbaged)
9396 clear_garbaged_frames ();
9397
9398 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9399 {
9400 echo_area_window = mini_window;
9401 window_height_changed_p = display_echo_area (w);
9402 w->must_be_updated_p = 1;
9403
9404 /* Update the display, unless called from redisplay_internal.
9405 Also don't update the screen during redisplay itself. The
9406 update will happen at the end of redisplay, and an update
9407 here could cause confusion. */
9408 if (update_frame_p && !redisplaying_p)
9409 {
9410 int n = 0;
9411
9412 /* If the display update has been interrupted by pending
9413 input, update mode lines in the frame. Due to the
9414 pending input, it might have been that redisplay hasn't
9415 been called, so that mode lines above the echo area are
9416 garbaged. This looks odd, so we prevent it here. */
9417 if (!display_completed)
9418 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9419
9420 if (window_height_changed_p
9421 /* Don't do this if Emacs is shutting down. Redisplay
9422 needs to run hooks. */
9423 && !NILP (Vrun_hooks))
9424 {
9425 /* Must update other windows. Likewise as in other
9426 cases, don't let this update be interrupted by
9427 pending input. */
9428 int count = SPECPDL_INDEX ();
9429 specbind (Qredisplay_dont_pause, Qt);
9430 windows_or_buffers_changed = 1;
9431 redisplay_internal (0);
9432 unbind_to (count, Qnil);
9433 }
9434 else if (FRAME_WINDOW_P (f) && n == 0)
9435 {
9436 /* Window configuration is the same as before.
9437 Can do with a display update of the echo area,
9438 unless we displayed some mode lines. */
9439 update_single_window (w, 1);
9440 FRAME_RIF (f)->flush_display (f);
9441 }
9442 else
9443 update_frame (f, 1, 1);
9444
9445 /* If cursor is in the echo area, make sure that the next
9446 redisplay displays the minibuffer, so that the cursor will
9447 be replaced with what the minibuffer wants. */
9448 if (cursor_in_echo_area)
9449 ++windows_or_buffers_changed;
9450 }
9451 }
9452 else if (!EQ (mini_window, selected_window))
9453 windows_or_buffers_changed++;
9454
9455 /* Last displayed message is now the current message. */
9456 echo_area_buffer[1] = echo_area_buffer[0];
9457 /* Inform read_char that we're not echoing. */
9458 echo_message_buffer = Qnil;
9459
9460 /* Prevent redisplay optimization in redisplay_internal by resetting
9461 this_line_start_pos. This is done because the mini-buffer now
9462 displays the message instead of its buffer text. */
9463 if (EQ (mini_window, selected_window))
9464 CHARPOS (this_line_start_pos) = 0;
9465
9466 return window_height_changed_p;
9467 }
9468
9469
9470 \f
9471 /***********************************************************************
9472 Mode Lines and Frame Titles
9473 ***********************************************************************/
9474
9475 /* A buffer for constructing non-propertized mode-line strings and
9476 frame titles in it; allocated from the heap in init_xdisp and
9477 resized as needed in store_mode_line_noprop_char. */
9478
9479 static char *mode_line_noprop_buf;
9480
9481 /* The buffer's end, and a current output position in it. */
9482
9483 static char *mode_line_noprop_buf_end;
9484 static char *mode_line_noprop_ptr;
9485
9486 #define MODE_LINE_NOPROP_LEN(start) \
9487 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9488
9489 static enum {
9490 MODE_LINE_DISPLAY = 0,
9491 MODE_LINE_TITLE,
9492 MODE_LINE_NOPROP,
9493 MODE_LINE_STRING
9494 } mode_line_target;
9495
9496 /* Alist that caches the results of :propertize.
9497 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9498 static Lisp_Object mode_line_proptrans_alist;
9499
9500 /* List of strings making up the mode-line. */
9501 static Lisp_Object mode_line_string_list;
9502
9503 /* Base face property when building propertized mode line string. */
9504 static Lisp_Object mode_line_string_face;
9505 static Lisp_Object mode_line_string_face_prop;
9506
9507
9508 /* Unwind data for mode line strings */
9509
9510 static Lisp_Object Vmode_line_unwind_vector;
9511
9512 static Lisp_Object
9513 format_mode_line_unwind_data (struct buffer *obuf,
9514 Lisp_Object owin,
9515 int save_proptrans)
9516 {
9517 Lisp_Object vector, tmp;
9518
9519 /* Reduce consing by keeping one vector in
9520 Vwith_echo_area_save_vector. */
9521 vector = Vmode_line_unwind_vector;
9522 Vmode_line_unwind_vector = Qnil;
9523
9524 if (NILP (vector))
9525 vector = Fmake_vector (make_number (8), Qnil);
9526
9527 ASET (vector, 0, make_number (mode_line_target));
9528 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9529 ASET (vector, 2, mode_line_string_list);
9530 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9531 ASET (vector, 4, mode_line_string_face);
9532 ASET (vector, 5, mode_line_string_face_prop);
9533
9534 if (obuf)
9535 XSETBUFFER (tmp, obuf);
9536 else
9537 tmp = Qnil;
9538 ASET (vector, 6, tmp);
9539 ASET (vector, 7, owin);
9540
9541 return vector;
9542 }
9543
9544 static Lisp_Object
9545 unwind_format_mode_line (vector)
9546 Lisp_Object vector;
9547 {
9548 mode_line_target = XINT (AREF (vector, 0));
9549 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9550 mode_line_string_list = AREF (vector, 2);
9551 if (! EQ (AREF (vector, 3), Qt))
9552 mode_line_proptrans_alist = AREF (vector, 3);
9553 mode_line_string_face = AREF (vector, 4);
9554 mode_line_string_face_prop = AREF (vector, 5);
9555
9556 if (!NILP (AREF (vector, 7)))
9557 /* Select window before buffer, since it may change the buffer. */
9558 Fselect_window (AREF (vector, 7), Qt);
9559
9560 if (!NILP (AREF (vector, 6)))
9561 {
9562 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9563 ASET (vector, 6, Qnil);
9564 }
9565
9566 Vmode_line_unwind_vector = vector;
9567 return Qnil;
9568 }
9569
9570
9571 /* Store a single character C for the frame title in mode_line_noprop_buf.
9572 Re-allocate mode_line_noprop_buf if necessary. */
9573
9574 static void
9575 #ifdef PROTOTYPES
9576 store_mode_line_noprop_char (char c)
9577 #else
9578 store_mode_line_noprop_char (c)
9579 char c;
9580 #endif
9581 {
9582 /* If output position has reached the end of the allocated buffer,
9583 double the buffer's size. */
9584 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9585 {
9586 int len = MODE_LINE_NOPROP_LEN (0);
9587 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9588 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9589 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9590 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9591 }
9592
9593 *mode_line_noprop_ptr++ = c;
9594 }
9595
9596
9597 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9598 mode_line_noprop_ptr. STR is the string to store. Do not copy
9599 characters that yield more columns than PRECISION; PRECISION <= 0
9600 means copy the whole string. Pad with spaces until FIELD_WIDTH
9601 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9602 pad. Called from display_mode_element when it is used to build a
9603 frame title. */
9604
9605 static int
9606 store_mode_line_noprop (str, field_width, precision)
9607 const unsigned char *str;
9608 int field_width, precision;
9609 {
9610 int n = 0;
9611 int dummy, nbytes;
9612
9613 /* Copy at most PRECISION chars from STR. */
9614 nbytes = strlen (str);
9615 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9616 while (nbytes--)
9617 store_mode_line_noprop_char (*str++);
9618
9619 /* Fill up with spaces until FIELD_WIDTH reached. */
9620 while (field_width > 0
9621 && n < field_width)
9622 {
9623 store_mode_line_noprop_char (' ');
9624 ++n;
9625 }
9626
9627 return n;
9628 }
9629
9630 /***********************************************************************
9631 Frame Titles
9632 ***********************************************************************/
9633
9634 #ifdef HAVE_WINDOW_SYSTEM
9635
9636 /* Set the title of FRAME, if it has changed. The title format is
9637 Vicon_title_format if FRAME is iconified, otherwise it is
9638 frame_title_format. */
9639
9640 static void
9641 x_consider_frame_title (frame)
9642 Lisp_Object frame;
9643 {
9644 struct frame *f = XFRAME (frame);
9645
9646 if (FRAME_WINDOW_P (f)
9647 || FRAME_MINIBUF_ONLY_P (f)
9648 || f->explicit_name)
9649 {
9650 /* Do we have more than one visible frame on this X display? */
9651 Lisp_Object tail;
9652 Lisp_Object fmt;
9653 int title_start;
9654 char *title;
9655 int len;
9656 struct it it;
9657 int count = SPECPDL_INDEX ();
9658
9659 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9660 {
9661 Lisp_Object other_frame = XCAR (tail);
9662 struct frame *tf = XFRAME (other_frame);
9663
9664 if (tf != f
9665 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9666 && !FRAME_MINIBUF_ONLY_P (tf)
9667 && !EQ (other_frame, tip_frame)
9668 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9669 break;
9670 }
9671
9672 /* Set global variable indicating that multiple frames exist. */
9673 multiple_frames = CONSP (tail);
9674
9675 /* Switch to the buffer of selected window of the frame. Set up
9676 mode_line_target so that display_mode_element will output into
9677 mode_line_noprop_buf; then display the title. */
9678 record_unwind_protect (unwind_format_mode_line,
9679 format_mode_line_unwind_data
9680 (current_buffer, selected_window, 0));
9681
9682 Fselect_window (f->selected_window, Qt);
9683 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9684 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9685
9686 mode_line_target = MODE_LINE_TITLE;
9687 title_start = MODE_LINE_NOPROP_LEN (0);
9688 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9689 NULL, DEFAULT_FACE_ID);
9690 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9691 len = MODE_LINE_NOPROP_LEN (title_start);
9692 title = mode_line_noprop_buf + title_start;
9693 unbind_to (count, Qnil);
9694
9695 /* Set the title only if it's changed. This avoids consing in
9696 the common case where it hasn't. (If it turns out that we've
9697 already wasted too much time by walking through the list with
9698 display_mode_element, then we might need to optimize at a
9699 higher level than this.) */
9700 if (! STRINGP (f->name)
9701 || SBYTES (f->name) != len
9702 || bcmp (title, SDATA (f->name), len) != 0)
9703 {
9704 #ifdef HAVE_NS
9705 if (FRAME_NS_P (f))
9706 {
9707 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9708 {
9709 if (EQ (fmt, Qt))
9710 ns_set_name_as_filename (f);
9711 else
9712 x_implicitly_set_name (f, make_string(title, len),
9713 Qnil);
9714 }
9715 }
9716 else
9717 #endif
9718 x_implicitly_set_name (f, make_string (title, len), Qnil);
9719 }
9720 #ifdef HAVE_NS
9721 if (FRAME_NS_P (f))
9722 {
9723 /* do this also for frames with explicit names */
9724 ns_implicitly_set_icon_type(f);
9725 ns_set_doc_edited(f, Fbuffer_modified_p
9726 (XWINDOW (f->selected_window)->buffer), Qnil);
9727 }
9728 #endif
9729 }
9730 }
9731
9732 #endif /* not HAVE_WINDOW_SYSTEM */
9733
9734
9735
9736 \f
9737 /***********************************************************************
9738 Menu Bars
9739 ***********************************************************************/
9740
9741
9742 /* Prepare for redisplay by updating menu-bar item lists when
9743 appropriate. This can call eval. */
9744
9745 void
9746 prepare_menu_bars ()
9747 {
9748 int all_windows;
9749 struct gcpro gcpro1, gcpro2;
9750 struct frame *f;
9751 Lisp_Object tooltip_frame;
9752
9753 #ifdef HAVE_WINDOW_SYSTEM
9754 tooltip_frame = tip_frame;
9755 #else
9756 tooltip_frame = Qnil;
9757 #endif
9758
9759 /* Update all frame titles based on their buffer names, etc. We do
9760 this before the menu bars so that the buffer-menu will show the
9761 up-to-date frame titles. */
9762 #ifdef HAVE_WINDOW_SYSTEM
9763 if (windows_or_buffers_changed || update_mode_lines)
9764 {
9765 Lisp_Object tail, frame;
9766
9767 FOR_EACH_FRAME (tail, frame)
9768 {
9769 f = XFRAME (frame);
9770 if (!EQ (frame, tooltip_frame)
9771 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9772 x_consider_frame_title (frame);
9773 }
9774 }
9775 #endif /* HAVE_WINDOW_SYSTEM */
9776
9777 /* Update the menu bar item lists, if appropriate. This has to be
9778 done before any actual redisplay or generation of display lines. */
9779 all_windows = (update_mode_lines
9780 || buffer_shared > 1
9781 || windows_or_buffers_changed);
9782 if (all_windows)
9783 {
9784 Lisp_Object tail, frame;
9785 int count = SPECPDL_INDEX ();
9786 /* 1 means that update_menu_bar has run its hooks
9787 so any further calls to update_menu_bar shouldn't do so again. */
9788 int menu_bar_hooks_run = 0;
9789
9790 record_unwind_save_match_data ();
9791
9792 FOR_EACH_FRAME (tail, frame)
9793 {
9794 f = XFRAME (frame);
9795
9796 /* Ignore tooltip frame. */
9797 if (EQ (frame, tooltip_frame))
9798 continue;
9799
9800 /* If a window on this frame changed size, report that to
9801 the user and clear the size-change flag. */
9802 if (FRAME_WINDOW_SIZES_CHANGED (f))
9803 {
9804 Lisp_Object functions;
9805
9806 /* Clear flag first in case we get an error below. */
9807 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9808 functions = Vwindow_size_change_functions;
9809 GCPRO2 (tail, functions);
9810
9811 while (CONSP (functions))
9812 {
9813 if (!EQ (XCAR (functions), Qt))
9814 call1 (XCAR (functions), frame);
9815 functions = XCDR (functions);
9816 }
9817 UNGCPRO;
9818 }
9819
9820 GCPRO1 (tail);
9821 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9822 #ifdef HAVE_WINDOW_SYSTEM
9823 update_tool_bar (f, 0);
9824 #endif
9825 UNGCPRO;
9826 }
9827
9828 unbind_to (count, Qnil);
9829 }
9830 else
9831 {
9832 struct frame *sf = SELECTED_FRAME ();
9833 update_menu_bar (sf, 1, 0);
9834 #ifdef HAVE_WINDOW_SYSTEM
9835 update_tool_bar (sf, 1);
9836 #endif
9837 }
9838
9839 /* Motif needs this. See comment in xmenu.c. Turn it off when
9840 pending_menu_activation is not defined. */
9841 #ifdef USE_X_TOOLKIT
9842 pending_menu_activation = 0;
9843 #endif
9844 }
9845
9846
9847 /* Update the menu bar item list for frame F. This has to be done
9848 before we start to fill in any display lines, because it can call
9849 eval.
9850
9851 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9852
9853 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9854 already ran the menu bar hooks for this redisplay, so there
9855 is no need to run them again. The return value is the
9856 updated value of this flag, to pass to the next call. */
9857
9858 static int
9859 update_menu_bar (f, save_match_data, hooks_run)
9860 struct frame *f;
9861 int save_match_data;
9862 int hooks_run;
9863 {
9864 Lisp_Object window;
9865 register struct window *w;
9866
9867 /* If called recursively during a menu update, do nothing. This can
9868 happen when, for instance, an activate-menubar-hook causes a
9869 redisplay. */
9870 if (inhibit_menubar_update)
9871 return hooks_run;
9872
9873 window = FRAME_SELECTED_WINDOW (f);
9874 w = XWINDOW (window);
9875
9876 if (FRAME_WINDOW_P (f)
9877 ?
9878 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9879 || defined (HAVE_NS) || defined (USE_GTK)
9880 FRAME_EXTERNAL_MENU_BAR (f)
9881 #else
9882 FRAME_MENU_BAR_LINES (f) > 0
9883 #endif
9884 : FRAME_MENU_BAR_LINES (f) > 0)
9885 {
9886 /* If the user has switched buffers or windows, we need to
9887 recompute to reflect the new bindings. But we'll
9888 recompute when update_mode_lines is set too; that means
9889 that people can use force-mode-line-update to request
9890 that the menu bar be recomputed. The adverse effect on
9891 the rest of the redisplay algorithm is about the same as
9892 windows_or_buffers_changed anyway. */
9893 if (windows_or_buffers_changed
9894 /* This used to test w->update_mode_line, but we believe
9895 there is no need to recompute the menu in that case. */
9896 || update_mode_lines
9897 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9898 < BUF_MODIFF (XBUFFER (w->buffer)))
9899 != !NILP (w->last_had_star))
9900 || ((!NILP (Vtransient_mark_mode)
9901 && !NILP (XBUFFER (w->buffer)->mark_active))
9902 != !NILP (w->region_showing)))
9903 {
9904 struct buffer *prev = current_buffer;
9905 int count = SPECPDL_INDEX ();
9906
9907 specbind (Qinhibit_menubar_update, Qt);
9908
9909 set_buffer_internal_1 (XBUFFER (w->buffer));
9910 if (save_match_data)
9911 record_unwind_save_match_data ();
9912 if (NILP (Voverriding_local_map_menu_flag))
9913 {
9914 specbind (Qoverriding_terminal_local_map, Qnil);
9915 specbind (Qoverriding_local_map, Qnil);
9916 }
9917
9918 if (!hooks_run)
9919 {
9920 /* Run the Lucid hook. */
9921 safe_run_hooks (Qactivate_menubar_hook);
9922
9923 /* If it has changed current-menubar from previous value,
9924 really recompute the menu-bar from the value. */
9925 if (! NILP (Vlucid_menu_bar_dirty_flag))
9926 call0 (Qrecompute_lucid_menubar);
9927
9928 safe_run_hooks (Qmenu_bar_update_hook);
9929
9930 hooks_run = 1;
9931 }
9932
9933 XSETFRAME (Vmenu_updating_frame, f);
9934 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9935
9936 /* Redisplay the menu bar in case we changed it. */
9937 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9938 || defined (HAVE_NS) || defined (USE_GTK)
9939 if (FRAME_WINDOW_P (f))
9940 {
9941 #if defined (HAVE_NS)
9942 /* All frames on Mac OS share the same menubar. So only
9943 the selected frame should be allowed to set it. */
9944 if (f == SELECTED_FRAME ())
9945 #endif
9946 set_frame_menubar (f, 0, 0);
9947 }
9948 else
9949 /* On a terminal screen, the menu bar is an ordinary screen
9950 line, and this makes it get updated. */
9951 w->update_mode_line = Qt;
9952 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9953 /* In the non-toolkit version, the menu bar is an ordinary screen
9954 line, and this makes it get updated. */
9955 w->update_mode_line = Qt;
9956 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9957
9958 unbind_to (count, Qnil);
9959 set_buffer_internal_1 (prev);
9960 }
9961 }
9962
9963 return hooks_run;
9964 }
9965
9966
9967 \f
9968 /***********************************************************************
9969 Output Cursor
9970 ***********************************************************************/
9971
9972 #ifdef HAVE_WINDOW_SYSTEM
9973
9974 /* EXPORT:
9975 Nominal cursor position -- where to draw output.
9976 HPOS and VPOS are window relative glyph matrix coordinates.
9977 X and Y are window relative pixel coordinates. */
9978
9979 struct cursor_pos output_cursor;
9980
9981
9982 /* EXPORT:
9983 Set the global variable output_cursor to CURSOR. All cursor
9984 positions are relative to updated_window. */
9985
9986 void
9987 set_output_cursor (cursor)
9988 struct cursor_pos *cursor;
9989 {
9990 output_cursor.hpos = cursor->hpos;
9991 output_cursor.vpos = cursor->vpos;
9992 output_cursor.x = cursor->x;
9993 output_cursor.y = cursor->y;
9994 }
9995
9996
9997 /* EXPORT for RIF:
9998 Set a nominal cursor position.
9999
10000 HPOS and VPOS are column/row positions in a window glyph matrix. X
10001 and Y are window text area relative pixel positions.
10002
10003 If this is done during an update, updated_window will contain the
10004 window that is being updated and the position is the future output
10005 cursor position for that window. If updated_window is null, use
10006 selected_window and display the cursor at the given position. */
10007
10008 void
10009 x_cursor_to (vpos, hpos, y, x)
10010 int vpos, hpos, y, x;
10011 {
10012 struct window *w;
10013
10014 /* If updated_window is not set, work on selected_window. */
10015 if (updated_window)
10016 w = updated_window;
10017 else
10018 w = XWINDOW (selected_window);
10019
10020 /* Set the output cursor. */
10021 output_cursor.hpos = hpos;
10022 output_cursor.vpos = vpos;
10023 output_cursor.x = x;
10024 output_cursor.y = y;
10025
10026 /* If not called as part of an update, really display the cursor.
10027 This will also set the cursor position of W. */
10028 if (updated_window == NULL)
10029 {
10030 BLOCK_INPUT;
10031 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10032 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10033 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10034 UNBLOCK_INPUT;
10035 }
10036 }
10037
10038 #endif /* HAVE_WINDOW_SYSTEM */
10039
10040 \f
10041 /***********************************************************************
10042 Tool-bars
10043 ***********************************************************************/
10044
10045 #ifdef HAVE_WINDOW_SYSTEM
10046
10047 /* Where the mouse was last time we reported a mouse event. */
10048
10049 FRAME_PTR last_mouse_frame;
10050
10051 /* Tool-bar item index of the item on which a mouse button was pressed
10052 or -1. */
10053
10054 int last_tool_bar_item;
10055
10056
10057 static Lisp_Object
10058 update_tool_bar_unwind (frame)
10059 Lisp_Object frame;
10060 {
10061 selected_frame = frame;
10062 return Qnil;
10063 }
10064
10065 /* Update the tool-bar item list for frame F. This has to be done
10066 before we start to fill in any display lines. Called from
10067 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10068 and restore it here. */
10069
10070 static void
10071 update_tool_bar (f, save_match_data)
10072 struct frame *f;
10073 int save_match_data;
10074 {
10075 #if defined (USE_GTK) || defined (HAVE_NS)
10076 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10077 #else
10078 int do_update = WINDOWP (f->tool_bar_window)
10079 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10080 #endif
10081
10082 if (do_update)
10083 {
10084 Lisp_Object window;
10085 struct window *w;
10086
10087 window = FRAME_SELECTED_WINDOW (f);
10088 w = XWINDOW (window);
10089
10090 /* If the user has switched buffers or windows, we need to
10091 recompute to reflect the new bindings. But we'll
10092 recompute when update_mode_lines is set too; that means
10093 that people can use force-mode-line-update to request
10094 that the menu bar be recomputed. The adverse effect on
10095 the rest of the redisplay algorithm is about the same as
10096 windows_or_buffers_changed anyway. */
10097 if (windows_or_buffers_changed
10098 || !NILP (w->update_mode_line)
10099 || update_mode_lines
10100 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10101 < BUF_MODIFF (XBUFFER (w->buffer)))
10102 != !NILP (w->last_had_star))
10103 || ((!NILP (Vtransient_mark_mode)
10104 && !NILP (XBUFFER (w->buffer)->mark_active))
10105 != !NILP (w->region_showing)))
10106 {
10107 struct buffer *prev = current_buffer;
10108 int count = SPECPDL_INDEX ();
10109 Lisp_Object frame, new_tool_bar;
10110 int new_n_tool_bar;
10111 struct gcpro gcpro1;
10112
10113 /* Set current_buffer to the buffer of the selected
10114 window of the frame, so that we get the right local
10115 keymaps. */
10116 set_buffer_internal_1 (XBUFFER (w->buffer));
10117
10118 /* Save match data, if we must. */
10119 if (save_match_data)
10120 record_unwind_save_match_data ();
10121
10122 /* Make sure that we don't accidentally use bogus keymaps. */
10123 if (NILP (Voverriding_local_map_menu_flag))
10124 {
10125 specbind (Qoverriding_terminal_local_map, Qnil);
10126 specbind (Qoverriding_local_map, Qnil);
10127 }
10128
10129 GCPRO1 (new_tool_bar);
10130
10131 /* We must temporarily set the selected frame to this frame
10132 before calling tool_bar_items, because the calculation of
10133 the tool-bar keymap uses the selected frame (see
10134 `tool-bar-make-keymap' in tool-bar.el). */
10135 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10136 XSETFRAME (frame, f);
10137 selected_frame = frame;
10138
10139 /* Build desired tool-bar items from keymaps. */
10140 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10141 &new_n_tool_bar);
10142
10143 /* Redisplay the tool-bar if we changed it. */
10144 if (new_n_tool_bar != f->n_tool_bar_items
10145 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10146 {
10147 /* Redisplay that happens asynchronously due to an expose event
10148 may access f->tool_bar_items. Make sure we update both
10149 variables within BLOCK_INPUT so no such event interrupts. */
10150 BLOCK_INPUT;
10151 f->tool_bar_items = new_tool_bar;
10152 f->n_tool_bar_items = new_n_tool_bar;
10153 w->update_mode_line = Qt;
10154 UNBLOCK_INPUT;
10155 }
10156
10157 UNGCPRO;
10158
10159 unbind_to (count, Qnil);
10160 set_buffer_internal_1 (prev);
10161 }
10162 }
10163 }
10164
10165
10166 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10167 F's desired tool-bar contents. F->tool_bar_items must have
10168 been set up previously by calling prepare_menu_bars. */
10169
10170 static void
10171 build_desired_tool_bar_string (f)
10172 struct frame *f;
10173 {
10174 int i, size, size_needed;
10175 struct gcpro gcpro1, gcpro2, gcpro3;
10176 Lisp_Object image, plist, props;
10177
10178 image = plist = props = Qnil;
10179 GCPRO3 (image, plist, props);
10180
10181 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10182 Otherwise, make a new string. */
10183
10184 /* The size of the string we might be able to reuse. */
10185 size = (STRINGP (f->desired_tool_bar_string)
10186 ? SCHARS (f->desired_tool_bar_string)
10187 : 0);
10188
10189 /* We need one space in the string for each image. */
10190 size_needed = f->n_tool_bar_items;
10191
10192 /* Reuse f->desired_tool_bar_string, if possible. */
10193 if (size < size_needed || NILP (f->desired_tool_bar_string))
10194 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10195 make_number (' '));
10196 else
10197 {
10198 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10199 Fremove_text_properties (make_number (0), make_number (size),
10200 props, f->desired_tool_bar_string);
10201 }
10202
10203 /* Put a `display' property on the string for the images to display,
10204 put a `menu_item' property on tool-bar items with a value that
10205 is the index of the item in F's tool-bar item vector. */
10206 for (i = 0; i < f->n_tool_bar_items; ++i)
10207 {
10208 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10209
10210 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10211 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10212 int hmargin, vmargin, relief, idx, end;
10213 extern Lisp_Object QCrelief, QCmargin, QCconversion;
10214
10215 /* If image is a vector, choose the image according to the
10216 button state. */
10217 image = PROP (TOOL_BAR_ITEM_IMAGES);
10218 if (VECTORP (image))
10219 {
10220 if (enabled_p)
10221 idx = (selected_p
10222 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10223 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10224 else
10225 idx = (selected_p
10226 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10227 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10228
10229 xassert (ASIZE (image) >= idx);
10230 image = AREF (image, idx);
10231 }
10232 else
10233 idx = -1;
10234
10235 /* Ignore invalid image specifications. */
10236 if (!valid_image_p (image))
10237 continue;
10238
10239 /* Display the tool-bar button pressed, or depressed. */
10240 plist = Fcopy_sequence (XCDR (image));
10241
10242 /* Compute margin and relief to draw. */
10243 relief = (tool_bar_button_relief >= 0
10244 ? tool_bar_button_relief
10245 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10246 hmargin = vmargin = relief;
10247
10248 if (INTEGERP (Vtool_bar_button_margin)
10249 && XINT (Vtool_bar_button_margin) > 0)
10250 {
10251 hmargin += XFASTINT (Vtool_bar_button_margin);
10252 vmargin += XFASTINT (Vtool_bar_button_margin);
10253 }
10254 else if (CONSP (Vtool_bar_button_margin))
10255 {
10256 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10257 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10258 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10259
10260 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10261 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10262 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10263 }
10264
10265 if (auto_raise_tool_bar_buttons_p)
10266 {
10267 /* Add a `:relief' property to the image spec if the item is
10268 selected. */
10269 if (selected_p)
10270 {
10271 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10272 hmargin -= relief;
10273 vmargin -= relief;
10274 }
10275 }
10276 else
10277 {
10278 /* If image is selected, display it pressed, i.e. with a
10279 negative relief. If it's not selected, display it with a
10280 raised relief. */
10281 plist = Fplist_put (plist, QCrelief,
10282 (selected_p
10283 ? make_number (-relief)
10284 : make_number (relief)));
10285 hmargin -= relief;
10286 vmargin -= relief;
10287 }
10288
10289 /* Put a margin around the image. */
10290 if (hmargin || vmargin)
10291 {
10292 if (hmargin == vmargin)
10293 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10294 else
10295 plist = Fplist_put (plist, QCmargin,
10296 Fcons (make_number (hmargin),
10297 make_number (vmargin)));
10298 }
10299
10300 /* If button is not enabled, and we don't have special images
10301 for the disabled state, make the image appear disabled by
10302 applying an appropriate algorithm to it. */
10303 if (!enabled_p && idx < 0)
10304 plist = Fplist_put (plist, QCconversion, Qdisabled);
10305
10306 /* Put a `display' text property on the string for the image to
10307 display. Put a `menu-item' property on the string that gives
10308 the start of this item's properties in the tool-bar items
10309 vector. */
10310 image = Fcons (Qimage, plist);
10311 props = list4 (Qdisplay, image,
10312 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10313
10314 /* Let the last image hide all remaining spaces in the tool bar
10315 string. The string can be longer than needed when we reuse a
10316 previous string. */
10317 if (i + 1 == f->n_tool_bar_items)
10318 end = SCHARS (f->desired_tool_bar_string);
10319 else
10320 end = i + 1;
10321 Fadd_text_properties (make_number (i), make_number (end),
10322 props, f->desired_tool_bar_string);
10323 #undef PROP
10324 }
10325
10326 UNGCPRO;
10327 }
10328
10329
10330 /* Display one line of the tool-bar of frame IT->f.
10331
10332 HEIGHT specifies the desired height of the tool-bar line.
10333 If the actual height of the glyph row is less than HEIGHT, the
10334 row's height is increased to HEIGHT, and the icons are centered
10335 vertically in the new height.
10336
10337 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10338 count a final empty row in case the tool-bar width exactly matches
10339 the window width.
10340 */
10341
10342 static void
10343 display_tool_bar_line (it, height)
10344 struct it *it;
10345 int height;
10346 {
10347 struct glyph_row *row = it->glyph_row;
10348 int max_x = it->last_visible_x;
10349 struct glyph *last;
10350
10351 prepare_desired_row (row);
10352 row->y = it->current_y;
10353
10354 /* Note that this isn't made use of if the face hasn't a box,
10355 so there's no need to check the face here. */
10356 it->start_of_box_run_p = 1;
10357
10358 while (it->current_x < max_x)
10359 {
10360 int x, n_glyphs_before, i, nglyphs;
10361 struct it it_before;
10362
10363 /* Get the next display element. */
10364 if (!get_next_display_element (it))
10365 {
10366 /* Don't count empty row if we are counting needed tool-bar lines. */
10367 if (height < 0 && !it->hpos)
10368 return;
10369 break;
10370 }
10371
10372 /* Produce glyphs. */
10373 n_glyphs_before = row->used[TEXT_AREA];
10374 it_before = *it;
10375
10376 PRODUCE_GLYPHS (it);
10377
10378 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10379 i = 0;
10380 x = it_before.current_x;
10381 while (i < nglyphs)
10382 {
10383 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10384
10385 if (x + glyph->pixel_width > max_x)
10386 {
10387 /* Glyph doesn't fit on line. Backtrack. */
10388 row->used[TEXT_AREA] = n_glyphs_before;
10389 *it = it_before;
10390 /* If this is the only glyph on this line, it will never fit on the
10391 toolbar, so skip it. But ensure there is at least one glyph,
10392 so we don't accidentally disable the tool-bar. */
10393 if (n_glyphs_before == 0
10394 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10395 break;
10396 goto out;
10397 }
10398
10399 ++it->hpos;
10400 x += glyph->pixel_width;
10401 ++i;
10402 }
10403
10404 /* Stop at line ends. */
10405 if (ITERATOR_AT_END_OF_LINE_P (it))
10406 break;
10407
10408 set_iterator_to_next (it, 1);
10409 }
10410
10411 out:;
10412
10413 row->displays_text_p = row->used[TEXT_AREA] != 0;
10414
10415 /* Use default face for the border below the tool bar.
10416
10417 FIXME: When auto-resize-tool-bars is grow-only, there is
10418 no additional border below the possibly empty tool-bar lines.
10419 So to make the extra empty lines look "normal", we have to
10420 use the tool-bar face for the border too. */
10421 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10422 it->face_id = DEFAULT_FACE_ID;
10423
10424 extend_face_to_end_of_line (it);
10425 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10426 last->right_box_line_p = 1;
10427 if (last == row->glyphs[TEXT_AREA])
10428 last->left_box_line_p = 1;
10429
10430 /* Make line the desired height and center it vertically. */
10431 if ((height -= it->max_ascent + it->max_descent) > 0)
10432 {
10433 /* Don't add more than one line height. */
10434 height %= FRAME_LINE_HEIGHT (it->f);
10435 it->max_ascent += height / 2;
10436 it->max_descent += (height + 1) / 2;
10437 }
10438
10439 compute_line_metrics (it);
10440
10441 /* If line is empty, make it occupy the rest of the tool-bar. */
10442 if (!row->displays_text_p)
10443 {
10444 row->height = row->phys_height = it->last_visible_y - row->y;
10445 row->visible_height = row->height;
10446 row->ascent = row->phys_ascent = 0;
10447 row->extra_line_spacing = 0;
10448 }
10449
10450 row->full_width_p = 1;
10451 row->continued_p = 0;
10452 row->truncated_on_left_p = 0;
10453 row->truncated_on_right_p = 0;
10454
10455 it->current_x = it->hpos = 0;
10456 it->current_y += row->height;
10457 ++it->vpos;
10458 ++it->glyph_row;
10459 }
10460
10461
10462 /* Max tool-bar height. */
10463
10464 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10465 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10466
10467 /* Value is the number of screen lines needed to make all tool-bar
10468 items of frame F visible. The number of actual rows needed is
10469 returned in *N_ROWS if non-NULL. */
10470
10471 static int
10472 tool_bar_lines_needed (f, n_rows)
10473 struct frame *f;
10474 int *n_rows;
10475 {
10476 struct window *w = XWINDOW (f->tool_bar_window);
10477 struct it it;
10478 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10479 the desired matrix, so use (unused) mode-line row as temporary row to
10480 avoid destroying the first tool-bar row. */
10481 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10482
10483 /* Initialize an iterator for iteration over
10484 F->desired_tool_bar_string in the tool-bar window of frame F. */
10485 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10486 it.first_visible_x = 0;
10487 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10488 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10489
10490 while (!ITERATOR_AT_END_P (&it))
10491 {
10492 clear_glyph_row (temp_row);
10493 it.glyph_row = temp_row;
10494 display_tool_bar_line (&it, -1);
10495 }
10496 clear_glyph_row (temp_row);
10497
10498 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10499 if (n_rows)
10500 *n_rows = it.vpos > 0 ? it.vpos : -1;
10501
10502 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10503 }
10504
10505
10506 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10507 0, 1, 0,
10508 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10509 (frame)
10510 Lisp_Object frame;
10511 {
10512 struct frame *f;
10513 struct window *w;
10514 int nlines = 0;
10515
10516 if (NILP (frame))
10517 frame = selected_frame;
10518 else
10519 CHECK_FRAME (frame);
10520 f = XFRAME (frame);
10521
10522 if (WINDOWP (f->tool_bar_window)
10523 || (w = XWINDOW (f->tool_bar_window),
10524 WINDOW_TOTAL_LINES (w) > 0))
10525 {
10526 update_tool_bar (f, 1);
10527 if (f->n_tool_bar_items)
10528 {
10529 build_desired_tool_bar_string (f);
10530 nlines = tool_bar_lines_needed (f, NULL);
10531 }
10532 }
10533
10534 return make_number (nlines);
10535 }
10536
10537
10538 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10539 height should be changed. */
10540
10541 static int
10542 redisplay_tool_bar (f)
10543 struct frame *f;
10544 {
10545 struct window *w;
10546 struct it it;
10547 struct glyph_row *row;
10548
10549 #if defined (USE_GTK) || defined (HAVE_NS)
10550 if (FRAME_EXTERNAL_TOOL_BAR (f))
10551 update_frame_tool_bar (f);
10552 return 0;
10553 #endif
10554
10555 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10556 do anything. This means you must start with tool-bar-lines
10557 non-zero to get the auto-sizing effect. Or in other words, you
10558 can turn off tool-bars by specifying tool-bar-lines zero. */
10559 if (!WINDOWP (f->tool_bar_window)
10560 || (w = XWINDOW (f->tool_bar_window),
10561 WINDOW_TOTAL_LINES (w) == 0))
10562 return 0;
10563
10564 /* Set up an iterator for the tool-bar window. */
10565 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10566 it.first_visible_x = 0;
10567 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10568 row = it.glyph_row;
10569
10570 /* Build a string that represents the contents of the tool-bar. */
10571 build_desired_tool_bar_string (f);
10572 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10573
10574 if (f->n_tool_bar_rows == 0)
10575 {
10576 int nlines;
10577
10578 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10579 nlines != WINDOW_TOTAL_LINES (w)))
10580 {
10581 extern Lisp_Object Qtool_bar_lines;
10582 Lisp_Object frame;
10583 int old_height = WINDOW_TOTAL_LINES (w);
10584
10585 XSETFRAME (frame, f);
10586 Fmodify_frame_parameters (frame,
10587 Fcons (Fcons (Qtool_bar_lines,
10588 make_number (nlines)),
10589 Qnil));
10590 if (WINDOW_TOTAL_LINES (w) != old_height)
10591 {
10592 clear_glyph_matrix (w->desired_matrix);
10593 fonts_changed_p = 1;
10594 return 1;
10595 }
10596 }
10597 }
10598
10599 /* Display as many lines as needed to display all tool-bar items. */
10600
10601 if (f->n_tool_bar_rows > 0)
10602 {
10603 int border, rows, height, extra;
10604
10605 if (INTEGERP (Vtool_bar_border))
10606 border = XINT (Vtool_bar_border);
10607 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10608 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10609 else if (EQ (Vtool_bar_border, Qborder_width))
10610 border = f->border_width;
10611 else
10612 border = 0;
10613 if (border < 0)
10614 border = 0;
10615
10616 rows = f->n_tool_bar_rows;
10617 height = max (1, (it.last_visible_y - border) / rows);
10618 extra = it.last_visible_y - border - height * rows;
10619
10620 while (it.current_y < it.last_visible_y)
10621 {
10622 int h = 0;
10623 if (extra > 0 && rows-- > 0)
10624 {
10625 h = (extra + rows - 1) / rows;
10626 extra -= h;
10627 }
10628 display_tool_bar_line (&it, height + h);
10629 }
10630 }
10631 else
10632 {
10633 while (it.current_y < it.last_visible_y)
10634 display_tool_bar_line (&it, 0);
10635 }
10636
10637 /* It doesn't make much sense to try scrolling in the tool-bar
10638 window, so don't do it. */
10639 w->desired_matrix->no_scrolling_p = 1;
10640 w->must_be_updated_p = 1;
10641
10642 if (!NILP (Vauto_resize_tool_bars))
10643 {
10644 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10645 int change_height_p = 0;
10646
10647 /* If we couldn't display everything, change the tool-bar's
10648 height if there is room for more. */
10649 if (IT_STRING_CHARPOS (it) < it.end_charpos
10650 && it.current_y < max_tool_bar_height)
10651 change_height_p = 1;
10652
10653 row = it.glyph_row - 1;
10654
10655 /* If there are blank lines at the end, except for a partially
10656 visible blank line at the end that is smaller than
10657 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10658 if (!row->displays_text_p
10659 && row->height >= FRAME_LINE_HEIGHT (f))
10660 change_height_p = 1;
10661
10662 /* If row displays tool-bar items, but is partially visible,
10663 change the tool-bar's height. */
10664 if (row->displays_text_p
10665 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10666 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10667 change_height_p = 1;
10668
10669 /* Resize windows as needed by changing the `tool-bar-lines'
10670 frame parameter. */
10671 if (change_height_p)
10672 {
10673 extern Lisp_Object Qtool_bar_lines;
10674 Lisp_Object frame;
10675 int old_height = WINDOW_TOTAL_LINES (w);
10676 int nrows;
10677 int nlines = tool_bar_lines_needed (f, &nrows);
10678
10679 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10680 && !f->minimize_tool_bar_window_p)
10681 ? (nlines > old_height)
10682 : (nlines != old_height));
10683 f->minimize_tool_bar_window_p = 0;
10684
10685 if (change_height_p)
10686 {
10687 XSETFRAME (frame, f);
10688 Fmodify_frame_parameters (frame,
10689 Fcons (Fcons (Qtool_bar_lines,
10690 make_number (nlines)),
10691 Qnil));
10692 if (WINDOW_TOTAL_LINES (w) != old_height)
10693 {
10694 clear_glyph_matrix (w->desired_matrix);
10695 f->n_tool_bar_rows = nrows;
10696 fonts_changed_p = 1;
10697 return 1;
10698 }
10699 }
10700 }
10701 }
10702
10703 f->minimize_tool_bar_window_p = 0;
10704 return 0;
10705 }
10706
10707
10708 /* Get information about the tool-bar item which is displayed in GLYPH
10709 on frame F. Return in *PROP_IDX the index where tool-bar item
10710 properties start in F->tool_bar_items. Value is zero if
10711 GLYPH doesn't display a tool-bar item. */
10712
10713 static int
10714 tool_bar_item_info (f, glyph, prop_idx)
10715 struct frame *f;
10716 struct glyph *glyph;
10717 int *prop_idx;
10718 {
10719 Lisp_Object prop;
10720 int success_p;
10721 int charpos;
10722
10723 /* This function can be called asynchronously, which means we must
10724 exclude any possibility that Fget_text_property signals an
10725 error. */
10726 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10727 charpos = max (0, charpos);
10728
10729 /* Get the text property `menu-item' at pos. The value of that
10730 property is the start index of this item's properties in
10731 F->tool_bar_items. */
10732 prop = Fget_text_property (make_number (charpos),
10733 Qmenu_item, f->current_tool_bar_string);
10734 if (INTEGERP (prop))
10735 {
10736 *prop_idx = XINT (prop);
10737 success_p = 1;
10738 }
10739 else
10740 success_p = 0;
10741
10742 return success_p;
10743 }
10744
10745 \f
10746 /* Get information about the tool-bar item at position X/Y on frame F.
10747 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10748 the current matrix of the tool-bar window of F, or NULL if not
10749 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10750 item in F->tool_bar_items. Value is
10751
10752 -1 if X/Y is not on a tool-bar item
10753 0 if X/Y is on the same item that was highlighted before.
10754 1 otherwise. */
10755
10756 static int
10757 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10758 struct frame *f;
10759 int x, y;
10760 struct glyph **glyph;
10761 int *hpos, *vpos, *prop_idx;
10762 {
10763 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10764 struct window *w = XWINDOW (f->tool_bar_window);
10765 int area;
10766
10767 /* Find the glyph under X/Y. */
10768 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10769 if (*glyph == NULL)
10770 return -1;
10771
10772 /* Get the start of this tool-bar item's properties in
10773 f->tool_bar_items. */
10774 if (!tool_bar_item_info (f, *glyph, prop_idx))
10775 return -1;
10776
10777 /* Is mouse on the highlighted item? */
10778 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10779 && *vpos >= dpyinfo->mouse_face_beg_row
10780 && *vpos <= dpyinfo->mouse_face_end_row
10781 && (*vpos > dpyinfo->mouse_face_beg_row
10782 || *hpos >= dpyinfo->mouse_face_beg_col)
10783 && (*vpos < dpyinfo->mouse_face_end_row
10784 || *hpos < dpyinfo->mouse_face_end_col
10785 || dpyinfo->mouse_face_past_end))
10786 return 0;
10787
10788 return 1;
10789 }
10790
10791
10792 /* EXPORT:
10793 Handle mouse button event on the tool-bar of frame F, at
10794 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10795 0 for button release. MODIFIERS is event modifiers for button
10796 release. */
10797
10798 void
10799 handle_tool_bar_click (f, x, y, down_p, modifiers)
10800 struct frame *f;
10801 int x, y, down_p;
10802 unsigned int modifiers;
10803 {
10804 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10805 struct window *w = XWINDOW (f->tool_bar_window);
10806 int hpos, vpos, prop_idx;
10807 struct glyph *glyph;
10808 Lisp_Object enabled_p;
10809
10810 /* If not on the highlighted tool-bar item, return. */
10811 frame_to_window_pixel_xy (w, &x, &y);
10812 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10813 return;
10814
10815 /* If item is disabled, do nothing. */
10816 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10817 if (NILP (enabled_p))
10818 return;
10819
10820 if (down_p)
10821 {
10822 /* Show item in pressed state. */
10823 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10824 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10825 last_tool_bar_item = prop_idx;
10826 }
10827 else
10828 {
10829 Lisp_Object key, frame;
10830 struct input_event event;
10831 EVENT_INIT (event);
10832
10833 /* Show item in released state. */
10834 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10835 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10836
10837 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10838
10839 XSETFRAME (frame, f);
10840 event.kind = TOOL_BAR_EVENT;
10841 event.frame_or_window = frame;
10842 event.arg = frame;
10843 kbd_buffer_store_event (&event);
10844
10845 event.kind = TOOL_BAR_EVENT;
10846 event.frame_or_window = frame;
10847 event.arg = key;
10848 event.modifiers = modifiers;
10849 kbd_buffer_store_event (&event);
10850 last_tool_bar_item = -1;
10851 }
10852 }
10853
10854
10855 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10856 tool-bar window-relative coordinates X/Y. Called from
10857 note_mouse_highlight. */
10858
10859 static void
10860 note_tool_bar_highlight (f, x, y)
10861 struct frame *f;
10862 int x, y;
10863 {
10864 Lisp_Object window = f->tool_bar_window;
10865 struct window *w = XWINDOW (window);
10866 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10867 int hpos, vpos;
10868 struct glyph *glyph;
10869 struct glyph_row *row;
10870 int i;
10871 Lisp_Object enabled_p;
10872 int prop_idx;
10873 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10874 int mouse_down_p, rc;
10875
10876 /* Function note_mouse_highlight is called with negative x(y
10877 values when mouse moves outside of the frame. */
10878 if (x <= 0 || y <= 0)
10879 {
10880 clear_mouse_face (dpyinfo);
10881 return;
10882 }
10883
10884 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10885 if (rc < 0)
10886 {
10887 /* Not on tool-bar item. */
10888 clear_mouse_face (dpyinfo);
10889 return;
10890 }
10891 else if (rc == 0)
10892 /* On same tool-bar item as before. */
10893 goto set_help_echo;
10894
10895 clear_mouse_face (dpyinfo);
10896
10897 /* Mouse is down, but on different tool-bar item? */
10898 mouse_down_p = (dpyinfo->grabbed
10899 && f == last_mouse_frame
10900 && FRAME_LIVE_P (f));
10901 if (mouse_down_p
10902 && last_tool_bar_item != prop_idx)
10903 return;
10904
10905 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10906 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10907
10908 /* If tool-bar item is not enabled, don't highlight it. */
10909 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10910 if (!NILP (enabled_p))
10911 {
10912 /* Compute the x-position of the glyph. In front and past the
10913 image is a space. We include this in the highlighted area. */
10914 row = MATRIX_ROW (w->current_matrix, vpos);
10915 for (i = x = 0; i < hpos; ++i)
10916 x += row->glyphs[TEXT_AREA][i].pixel_width;
10917
10918 /* Record this as the current active region. */
10919 dpyinfo->mouse_face_beg_col = hpos;
10920 dpyinfo->mouse_face_beg_row = vpos;
10921 dpyinfo->mouse_face_beg_x = x;
10922 dpyinfo->mouse_face_beg_y = row->y;
10923 dpyinfo->mouse_face_past_end = 0;
10924
10925 dpyinfo->mouse_face_end_col = hpos + 1;
10926 dpyinfo->mouse_face_end_row = vpos;
10927 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10928 dpyinfo->mouse_face_end_y = row->y;
10929 dpyinfo->mouse_face_window = window;
10930 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10931
10932 /* Display it as active. */
10933 show_mouse_face (dpyinfo, draw);
10934 dpyinfo->mouse_face_image_state = draw;
10935 }
10936
10937 set_help_echo:
10938
10939 /* Set help_echo_string to a help string to display for this tool-bar item.
10940 XTread_socket does the rest. */
10941 help_echo_object = help_echo_window = Qnil;
10942 help_echo_pos = -1;
10943 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10944 if (NILP (help_echo_string))
10945 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10946 }
10947
10948 #endif /* HAVE_WINDOW_SYSTEM */
10949
10950
10951 \f
10952 /************************************************************************
10953 Horizontal scrolling
10954 ************************************************************************/
10955
10956 static int hscroll_window_tree P_ ((Lisp_Object));
10957 static int hscroll_windows P_ ((Lisp_Object));
10958
10959 /* For all leaf windows in the window tree rooted at WINDOW, set their
10960 hscroll value so that PT is (i) visible in the window, and (ii) so
10961 that it is not within a certain margin at the window's left and
10962 right border. Value is non-zero if any window's hscroll has been
10963 changed. */
10964
10965 static int
10966 hscroll_window_tree (window)
10967 Lisp_Object window;
10968 {
10969 int hscrolled_p = 0;
10970 int hscroll_relative_p = FLOATP (Vhscroll_step);
10971 int hscroll_step_abs = 0;
10972 double hscroll_step_rel = 0;
10973
10974 if (hscroll_relative_p)
10975 {
10976 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10977 if (hscroll_step_rel < 0)
10978 {
10979 hscroll_relative_p = 0;
10980 hscroll_step_abs = 0;
10981 }
10982 }
10983 else if (INTEGERP (Vhscroll_step))
10984 {
10985 hscroll_step_abs = XINT (Vhscroll_step);
10986 if (hscroll_step_abs < 0)
10987 hscroll_step_abs = 0;
10988 }
10989 else
10990 hscroll_step_abs = 0;
10991
10992 while (WINDOWP (window))
10993 {
10994 struct window *w = XWINDOW (window);
10995
10996 if (WINDOWP (w->hchild))
10997 hscrolled_p |= hscroll_window_tree (w->hchild);
10998 else if (WINDOWP (w->vchild))
10999 hscrolled_p |= hscroll_window_tree (w->vchild);
11000 else if (w->cursor.vpos >= 0)
11001 {
11002 int h_margin;
11003 int text_area_width;
11004 struct glyph_row *current_cursor_row
11005 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11006 struct glyph_row *desired_cursor_row
11007 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11008 struct glyph_row *cursor_row
11009 = (desired_cursor_row->enabled_p
11010 ? desired_cursor_row
11011 : current_cursor_row);
11012
11013 text_area_width = window_box_width (w, TEXT_AREA);
11014
11015 /* Scroll when cursor is inside this scroll margin. */
11016 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11017
11018 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11019 && ((XFASTINT (w->hscroll)
11020 && w->cursor.x <= h_margin)
11021 || (cursor_row->enabled_p
11022 && cursor_row->truncated_on_right_p
11023 && (w->cursor.x >= text_area_width - h_margin))))
11024 {
11025 struct it it;
11026 int hscroll;
11027 struct buffer *saved_current_buffer;
11028 int pt;
11029 int wanted_x;
11030
11031 /* Find point in a display of infinite width. */
11032 saved_current_buffer = current_buffer;
11033 current_buffer = XBUFFER (w->buffer);
11034
11035 if (w == XWINDOW (selected_window))
11036 pt = BUF_PT (current_buffer);
11037 else
11038 {
11039 pt = marker_position (w->pointm);
11040 pt = max (BEGV, pt);
11041 pt = min (ZV, pt);
11042 }
11043
11044 /* Move iterator to pt starting at cursor_row->start in
11045 a line with infinite width. */
11046 init_to_row_start (&it, w, cursor_row);
11047 it.last_visible_x = INFINITY;
11048 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11049 current_buffer = saved_current_buffer;
11050
11051 /* Position cursor in window. */
11052 if (!hscroll_relative_p && hscroll_step_abs == 0)
11053 hscroll = max (0, (it.current_x
11054 - (ITERATOR_AT_END_OF_LINE_P (&it)
11055 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11056 : (text_area_width / 2))))
11057 / FRAME_COLUMN_WIDTH (it.f);
11058 else if (w->cursor.x >= text_area_width - h_margin)
11059 {
11060 if (hscroll_relative_p)
11061 wanted_x = text_area_width * (1 - hscroll_step_rel)
11062 - h_margin;
11063 else
11064 wanted_x = text_area_width
11065 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11066 - h_margin;
11067 hscroll
11068 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11069 }
11070 else
11071 {
11072 if (hscroll_relative_p)
11073 wanted_x = text_area_width * hscroll_step_rel
11074 + h_margin;
11075 else
11076 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11077 + h_margin;
11078 hscroll
11079 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11080 }
11081 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11082
11083 /* Don't call Fset_window_hscroll if value hasn't
11084 changed because it will prevent redisplay
11085 optimizations. */
11086 if (XFASTINT (w->hscroll) != hscroll)
11087 {
11088 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11089 w->hscroll = make_number (hscroll);
11090 hscrolled_p = 1;
11091 }
11092 }
11093 }
11094
11095 window = w->next;
11096 }
11097
11098 /* Value is non-zero if hscroll of any leaf window has been changed. */
11099 return hscrolled_p;
11100 }
11101
11102
11103 /* Set hscroll so that cursor is visible and not inside horizontal
11104 scroll margins for all windows in the tree rooted at WINDOW. See
11105 also hscroll_window_tree above. Value is non-zero if any window's
11106 hscroll has been changed. If it has, desired matrices on the frame
11107 of WINDOW are cleared. */
11108
11109 static int
11110 hscroll_windows (window)
11111 Lisp_Object window;
11112 {
11113 int hscrolled_p = hscroll_window_tree (window);
11114 if (hscrolled_p)
11115 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11116 return hscrolled_p;
11117 }
11118
11119
11120 \f
11121 /************************************************************************
11122 Redisplay
11123 ************************************************************************/
11124
11125 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11126 to a non-zero value. This is sometimes handy to have in a debugger
11127 session. */
11128
11129 #if GLYPH_DEBUG
11130
11131 /* First and last unchanged row for try_window_id. */
11132
11133 int debug_first_unchanged_at_end_vpos;
11134 int debug_last_unchanged_at_beg_vpos;
11135
11136 /* Delta vpos and y. */
11137
11138 int debug_dvpos, debug_dy;
11139
11140 /* Delta in characters and bytes for try_window_id. */
11141
11142 int debug_delta, debug_delta_bytes;
11143
11144 /* Values of window_end_pos and window_end_vpos at the end of
11145 try_window_id. */
11146
11147 EMACS_INT debug_end_pos, debug_end_vpos;
11148
11149 /* Append a string to W->desired_matrix->method. FMT is a printf
11150 format string. A1...A9 are a supplement for a variable-length
11151 argument list. If trace_redisplay_p is non-zero also printf the
11152 resulting string to stderr. */
11153
11154 static void
11155 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11156 struct window *w;
11157 char *fmt;
11158 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11159 {
11160 char buffer[512];
11161 char *method = w->desired_matrix->method;
11162 int len = strlen (method);
11163 int size = sizeof w->desired_matrix->method;
11164 int remaining = size - len - 1;
11165
11166 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11167 if (len && remaining)
11168 {
11169 method[len] = '|';
11170 --remaining, ++len;
11171 }
11172
11173 strncpy (method + len, buffer, remaining);
11174
11175 if (trace_redisplay_p)
11176 fprintf (stderr, "%p (%s): %s\n",
11177 w,
11178 ((BUFFERP (w->buffer)
11179 && STRINGP (XBUFFER (w->buffer)->name))
11180 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11181 : "no buffer"),
11182 buffer);
11183 }
11184
11185 #endif /* GLYPH_DEBUG */
11186
11187
11188 /* Value is non-zero if all changes in window W, which displays
11189 current_buffer, are in the text between START and END. START is a
11190 buffer position, END is given as a distance from Z. Used in
11191 redisplay_internal for display optimization. */
11192
11193 static INLINE int
11194 text_outside_line_unchanged_p (w, start, end)
11195 struct window *w;
11196 int start, end;
11197 {
11198 int unchanged_p = 1;
11199
11200 /* If text or overlays have changed, see where. */
11201 if (XFASTINT (w->last_modified) < MODIFF
11202 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11203 {
11204 /* Gap in the line? */
11205 if (GPT < start || Z - GPT < end)
11206 unchanged_p = 0;
11207
11208 /* Changes start in front of the line, or end after it? */
11209 if (unchanged_p
11210 && (BEG_UNCHANGED < start - 1
11211 || END_UNCHANGED < end))
11212 unchanged_p = 0;
11213
11214 /* If selective display, can't optimize if changes start at the
11215 beginning of the line. */
11216 if (unchanged_p
11217 && INTEGERP (current_buffer->selective_display)
11218 && XINT (current_buffer->selective_display) > 0
11219 && (BEG_UNCHANGED < start || GPT <= start))
11220 unchanged_p = 0;
11221
11222 /* If there are overlays at the start or end of the line, these
11223 may have overlay strings with newlines in them. A change at
11224 START, for instance, may actually concern the display of such
11225 overlay strings as well, and they are displayed on different
11226 lines. So, quickly rule out this case. (For the future, it
11227 might be desirable to implement something more telling than
11228 just BEG/END_UNCHANGED.) */
11229 if (unchanged_p)
11230 {
11231 if (BEG + BEG_UNCHANGED == start
11232 && overlay_touches_p (start))
11233 unchanged_p = 0;
11234 if (END_UNCHANGED == end
11235 && overlay_touches_p (Z - end))
11236 unchanged_p = 0;
11237 }
11238
11239 /* Under bidi reordering, adding or deleting a character in the
11240 beginning of a paragraph, before the first strong directional
11241 character, can change the base direction of the paragraph (unless
11242 the buffer specifies a fixed paragraph direction), which will
11243 require to redisplay the whole paragraph. It might be worthwhile
11244 to find the paragraph limits and widen the range of redisplayed
11245 lines to that, but for now just give up this optimization. */
11246 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11247 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11248 unchanged_p = 0;
11249 }
11250
11251 return unchanged_p;
11252 }
11253
11254
11255 /* Do a frame update, taking possible shortcuts into account. This is
11256 the main external entry point for redisplay.
11257
11258 If the last redisplay displayed an echo area message and that message
11259 is no longer requested, we clear the echo area or bring back the
11260 mini-buffer if that is in use. */
11261
11262 void
11263 redisplay ()
11264 {
11265 redisplay_internal (0);
11266 }
11267
11268
11269 static Lisp_Object
11270 overlay_arrow_string_or_property (var)
11271 Lisp_Object var;
11272 {
11273 Lisp_Object val;
11274
11275 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11276 return val;
11277
11278 return Voverlay_arrow_string;
11279 }
11280
11281 /* Return 1 if there are any overlay-arrows in current_buffer. */
11282 static int
11283 overlay_arrow_in_current_buffer_p ()
11284 {
11285 Lisp_Object vlist;
11286
11287 for (vlist = Voverlay_arrow_variable_list;
11288 CONSP (vlist);
11289 vlist = XCDR (vlist))
11290 {
11291 Lisp_Object var = XCAR (vlist);
11292 Lisp_Object val;
11293
11294 if (!SYMBOLP (var))
11295 continue;
11296 val = find_symbol_value (var);
11297 if (MARKERP (val)
11298 && current_buffer == XMARKER (val)->buffer)
11299 return 1;
11300 }
11301 return 0;
11302 }
11303
11304
11305 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11306 has changed. */
11307
11308 static int
11309 overlay_arrows_changed_p ()
11310 {
11311 Lisp_Object vlist;
11312
11313 for (vlist = Voverlay_arrow_variable_list;
11314 CONSP (vlist);
11315 vlist = XCDR (vlist))
11316 {
11317 Lisp_Object var = XCAR (vlist);
11318 Lisp_Object val, pstr;
11319
11320 if (!SYMBOLP (var))
11321 continue;
11322 val = find_symbol_value (var);
11323 if (!MARKERP (val))
11324 continue;
11325 if (! EQ (COERCE_MARKER (val),
11326 Fget (var, Qlast_arrow_position))
11327 || ! (pstr = overlay_arrow_string_or_property (var),
11328 EQ (pstr, Fget (var, Qlast_arrow_string))))
11329 return 1;
11330 }
11331 return 0;
11332 }
11333
11334 /* Mark overlay arrows to be updated on next redisplay. */
11335
11336 static void
11337 update_overlay_arrows (up_to_date)
11338 int up_to_date;
11339 {
11340 Lisp_Object vlist;
11341
11342 for (vlist = Voverlay_arrow_variable_list;
11343 CONSP (vlist);
11344 vlist = XCDR (vlist))
11345 {
11346 Lisp_Object var = XCAR (vlist);
11347
11348 if (!SYMBOLP (var))
11349 continue;
11350
11351 if (up_to_date > 0)
11352 {
11353 Lisp_Object val = find_symbol_value (var);
11354 Fput (var, Qlast_arrow_position,
11355 COERCE_MARKER (val));
11356 Fput (var, Qlast_arrow_string,
11357 overlay_arrow_string_or_property (var));
11358 }
11359 else if (up_to_date < 0
11360 || !NILP (Fget (var, Qlast_arrow_position)))
11361 {
11362 Fput (var, Qlast_arrow_position, Qt);
11363 Fput (var, Qlast_arrow_string, Qt);
11364 }
11365 }
11366 }
11367
11368
11369 /* Return overlay arrow string to display at row.
11370 Return integer (bitmap number) for arrow bitmap in left fringe.
11371 Return nil if no overlay arrow. */
11372
11373 static Lisp_Object
11374 overlay_arrow_at_row (it, row)
11375 struct it *it;
11376 struct glyph_row *row;
11377 {
11378 Lisp_Object vlist;
11379
11380 for (vlist = Voverlay_arrow_variable_list;
11381 CONSP (vlist);
11382 vlist = XCDR (vlist))
11383 {
11384 Lisp_Object var = XCAR (vlist);
11385 Lisp_Object val;
11386
11387 if (!SYMBOLP (var))
11388 continue;
11389
11390 val = find_symbol_value (var);
11391
11392 if (MARKERP (val)
11393 && current_buffer == XMARKER (val)->buffer
11394 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11395 {
11396 if (FRAME_WINDOW_P (it->f)
11397 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11398 {
11399 #ifdef HAVE_WINDOW_SYSTEM
11400 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11401 {
11402 int fringe_bitmap;
11403 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11404 return make_number (fringe_bitmap);
11405 }
11406 #endif
11407 return make_number (-1); /* Use default arrow bitmap */
11408 }
11409 return overlay_arrow_string_or_property (var);
11410 }
11411 }
11412
11413 return Qnil;
11414 }
11415
11416 /* Return 1 if point moved out of or into a composition. Otherwise
11417 return 0. PREV_BUF and PREV_PT are the last point buffer and
11418 position. BUF and PT are the current point buffer and position. */
11419
11420 int
11421 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11422 struct buffer *prev_buf, *buf;
11423 int prev_pt, pt;
11424 {
11425 EMACS_INT start, end;
11426 Lisp_Object prop;
11427 Lisp_Object buffer;
11428
11429 XSETBUFFER (buffer, buf);
11430 /* Check a composition at the last point if point moved within the
11431 same buffer. */
11432 if (prev_buf == buf)
11433 {
11434 if (prev_pt == pt)
11435 /* Point didn't move. */
11436 return 0;
11437
11438 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11439 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11440 && COMPOSITION_VALID_P (start, end, prop)
11441 && start < prev_pt && end > prev_pt)
11442 /* The last point was within the composition. Return 1 iff
11443 point moved out of the composition. */
11444 return (pt <= start || pt >= end);
11445 }
11446
11447 /* Check a composition at the current point. */
11448 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11449 && find_composition (pt, -1, &start, &end, &prop, buffer)
11450 && COMPOSITION_VALID_P (start, end, prop)
11451 && start < pt && end > pt);
11452 }
11453
11454
11455 /* Reconsider the setting of B->clip_changed which is displayed
11456 in window W. */
11457
11458 static INLINE void
11459 reconsider_clip_changes (w, b)
11460 struct window *w;
11461 struct buffer *b;
11462 {
11463 if (b->clip_changed
11464 && !NILP (w->window_end_valid)
11465 && w->current_matrix->buffer == b
11466 && w->current_matrix->zv == BUF_ZV (b)
11467 && w->current_matrix->begv == BUF_BEGV (b))
11468 b->clip_changed = 0;
11469
11470 /* If display wasn't paused, and W is not a tool bar window, see if
11471 point has been moved into or out of a composition. In that case,
11472 we set b->clip_changed to 1 to force updating the screen. If
11473 b->clip_changed has already been set to 1, we can skip this
11474 check. */
11475 if (!b->clip_changed
11476 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11477 {
11478 int pt;
11479
11480 if (w == XWINDOW (selected_window))
11481 pt = BUF_PT (current_buffer);
11482 else
11483 pt = marker_position (w->pointm);
11484
11485 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11486 || pt != XINT (w->last_point))
11487 && check_point_in_composition (w->current_matrix->buffer,
11488 XINT (w->last_point),
11489 XBUFFER (w->buffer), pt))
11490 b->clip_changed = 1;
11491 }
11492 }
11493 \f
11494
11495 /* Select FRAME to forward the values of frame-local variables into C
11496 variables so that the redisplay routines can access those values
11497 directly. */
11498
11499 static void
11500 select_frame_for_redisplay (frame)
11501 Lisp_Object frame;
11502 {
11503 Lisp_Object tail, symbol, val;
11504 Lisp_Object old = selected_frame;
11505 struct Lisp_Symbol *sym;
11506
11507 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11508
11509 selected_frame = frame;
11510
11511 do
11512 {
11513 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11514 if (CONSP (XCAR (tail))
11515 && (symbol = XCAR (XCAR (tail)),
11516 SYMBOLP (symbol))
11517 && (sym = indirect_variable (XSYMBOL (symbol)),
11518 val = sym->value,
11519 (BUFFER_LOCAL_VALUEP (val)))
11520 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11521 /* Use find_symbol_value rather than Fsymbol_value
11522 to avoid an error if it is void. */
11523 find_symbol_value (symbol);
11524 } while (!EQ (frame, old) && (frame = old, 1));
11525 }
11526
11527
11528 #define STOP_POLLING \
11529 do { if (! polling_stopped_here) stop_polling (); \
11530 polling_stopped_here = 1; } while (0)
11531
11532 #define RESUME_POLLING \
11533 do { if (polling_stopped_here) start_polling (); \
11534 polling_stopped_here = 0; } while (0)
11535
11536
11537 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11538 response to any user action; therefore, we should preserve the echo
11539 area. (Actually, our caller does that job.) Perhaps in the future
11540 avoid recentering windows if it is not necessary; currently that
11541 causes some problems. */
11542
11543 static void
11544 redisplay_internal (preserve_echo_area)
11545 int preserve_echo_area;
11546 {
11547 struct window *w = XWINDOW (selected_window);
11548 struct frame *f;
11549 int pause;
11550 int must_finish = 0;
11551 struct text_pos tlbufpos, tlendpos;
11552 int number_of_visible_frames;
11553 int count, count1;
11554 struct frame *sf;
11555 int polling_stopped_here = 0;
11556 Lisp_Object old_frame = selected_frame;
11557
11558 /* Non-zero means redisplay has to consider all windows on all
11559 frames. Zero means, only selected_window is considered. */
11560 int consider_all_windows_p;
11561
11562 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11563
11564 /* No redisplay if running in batch mode or frame is not yet fully
11565 initialized, or redisplay is explicitly turned off by setting
11566 Vinhibit_redisplay. */
11567 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11568 || !NILP (Vinhibit_redisplay))
11569 return;
11570
11571 /* Don't examine these until after testing Vinhibit_redisplay.
11572 When Emacs is shutting down, perhaps because its connection to
11573 X has dropped, we should not look at them at all. */
11574 f = XFRAME (w->frame);
11575 sf = SELECTED_FRAME ();
11576
11577 if (!f->glyphs_initialized_p)
11578 return;
11579
11580 /* The flag redisplay_performed_directly_p is set by
11581 direct_output_for_insert when it already did the whole screen
11582 update necessary. */
11583 if (redisplay_performed_directly_p)
11584 {
11585 redisplay_performed_directly_p = 0;
11586 if (!hscroll_windows (selected_window))
11587 return;
11588 }
11589
11590 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11591 if (popup_activated ())
11592 return;
11593 #endif
11594
11595 /* I don't think this happens but let's be paranoid. */
11596 if (redisplaying_p)
11597 return;
11598
11599 /* Record a function that resets redisplaying_p to its old value
11600 when we leave this function. */
11601 count = SPECPDL_INDEX ();
11602 record_unwind_protect (unwind_redisplay,
11603 Fcons (make_number (redisplaying_p), selected_frame));
11604 ++redisplaying_p;
11605 specbind (Qinhibit_free_realized_faces, Qnil);
11606
11607 {
11608 Lisp_Object tail, frame;
11609
11610 FOR_EACH_FRAME (tail, frame)
11611 {
11612 struct frame *f = XFRAME (frame);
11613 f->already_hscrolled_p = 0;
11614 }
11615 }
11616
11617 retry:
11618 if (!EQ (old_frame, selected_frame)
11619 && FRAME_LIVE_P (XFRAME (old_frame)))
11620 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11621 selected_frame and selected_window to be temporarily out-of-sync so
11622 when we come back here via `goto retry', we need to resync because we
11623 may need to run Elisp code (via prepare_menu_bars). */
11624 select_frame_for_redisplay (old_frame);
11625
11626 pause = 0;
11627 reconsider_clip_changes (w, current_buffer);
11628 last_escape_glyph_frame = NULL;
11629 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11630
11631 /* If new fonts have been loaded that make a glyph matrix adjustment
11632 necessary, do it. */
11633 if (fonts_changed_p)
11634 {
11635 adjust_glyphs (NULL);
11636 ++windows_or_buffers_changed;
11637 fonts_changed_p = 0;
11638 }
11639
11640 /* If face_change_count is non-zero, init_iterator will free all
11641 realized faces, which includes the faces referenced from current
11642 matrices. So, we can't reuse current matrices in this case. */
11643 if (face_change_count)
11644 ++windows_or_buffers_changed;
11645
11646 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11647 && FRAME_TTY (sf)->previous_frame != sf)
11648 {
11649 /* Since frames on a single ASCII terminal share the same
11650 display area, displaying a different frame means redisplay
11651 the whole thing. */
11652 windows_or_buffers_changed++;
11653 SET_FRAME_GARBAGED (sf);
11654 #ifndef DOS_NT
11655 set_tty_color_mode (FRAME_TTY (sf), sf);
11656 #endif
11657 FRAME_TTY (sf)->previous_frame = sf;
11658 }
11659
11660 /* Set the visible flags for all frames. Do this before checking
11661 for resized or garbaged frames; they want to know if their frames
11662 are visible. See the comment in frame.h for
11663 FRAME_SAMPLE_VISIBILITY. */
11664 {
11665 Lisp_Object tail, frame;
11666
11667 number_of_visible_frames = 0;
11668
11669 FOR_EACH_FRAME (tail, frame)
11670 {
11671 struct frame *f = XFRAME (frame);
11672
11673 FRAME_SAMPLE_VISIBILITY (f);
11674 if (FRAME_VISIBLE_P (f))
11675 ++number_of_visible_frames;
11676 clear_desired_matrices (f);
11677 }
11678 }
11679
11680 /* Notice any pending interrupt request to change frame size. */
11681 do_pending_window_change (1);
11682
11683 /* Clear frames marked as garbaged. */
11684 if (frame_garbaged)
11685 clear_garbaged_frames ();
11686
11687 /* Build menubar and tool-bar items. */
11688 if (NILP (Vmemory_full))
11689 prepare_menu_bars ();
11690
11691 if (windows_or_buffers_changed)
11692 update_mode_lines++;
11693
11694 /* Detect case that we need to write or remove a star in the mode line. */
11695 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11696 {
11697 w->update_mode_line = Qt;
11698 if (buffer_shared > 1)
11699 update_mode_lines++;
11700 }
11701
11702 /* Avoid invocation of point motion hooks by `current_column' below. */
11703 count1 = SPECPDL_INDEX ();
11704 specbind (Qinhibit_point_motion_hooks, Qt);
11705
11706 /* If %c is in the mode line, update it if needed. */
11707 if (!NILP (w->column_number_displayed)
11708 /* This alternative quickly identifies a common case
11709 where no change is needed. */
11710 && !(PT == XFASTINT (w->last_point)
11711 && XFASTINT (w->last_modified) >= MODIFF
11712 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11713 && (XFASTINT (w->column_number_displayed)
11714 != (int) current_column ())) /* iftc */
11715 w->update_mode_line = Qt;
11716
11717 unbind_to (count1, Qnil);
11718
11719 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11720
11721 /* The variable buffer_shared is set in redisplay_window and
11722 indicates that we redisplay a buffer in different windows. See
11723 there. */
11724 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11725 || cursor_type_changed);
11726
11727 /* If specs for an arrow have changed, do thorough redisplay
11728 to ensure we remove any arrow that should no longer exist. */
11729 if (overlay_arrows_changed_p ())
11730 consider_all_windows_p = windows_or_buffers_changed = 1;
11731
11732 /* Normally the message* functions will have already displayed and
11733 updated the echo area, but the frame may have been trashed, or
11734 the update may have been preempted, so display the echo area
11735 again here. Checking message_cleared_p captures the case that
11736 the echo area should be cleared. */
11737 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11738 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11739 || (message_cleared_p
11740 && minibuf_level == 0
11741 /* If the mini-window is currently selected, this means the
11742 echo-area doesn't show through. */
11743 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11744 {
11745 int window_height_changed_p = echo_area_display (0);
11746 must_finish = 1;
11747
11748 /* If we don't display the current message, don't clear the
11749 message_cleared_p flag, because, if we did, we wouldn't clear
11750 the echo area in the next redisplay which doesn't preserve
11751 the echo area. */
11752 if (!display_last_displayed_message_p)
11753 message_cleared_p = 0;
11754
11755 if (fonts_changed_p)
11756 goto retry;
11757 else if (window_height_changed_p)
11758 {
11759 consider_all_windows_p = 1;
11760 ++update_mode_lines;
11761 ++windows_or_buffers_changed;
11762
11763 /* If window configuration was changed, frames may have been
11764 marked garbaged. Clear them or we will experience
11765 surprises wrt scrolling. */
11766 if (frame_garbaged)
11767 clear_garbaged_frames ();
11768 }
11769 }
11770 else if (EQ (selected_window, minibuf_window)
11771 && (current_buffer->clip_changed
11772 || XFASTINT (w->last_modified) < MODIFF
11773 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11774 && resize_mini_window (w, 0))
11775 {
11776 /* Resized active mini-window to fit the size of what it is
11777 showing if its contents might have changed. */
11778 must_finish = 1;
11779 /* FIXME: this causes all frames to be updated, which seems unnecessary
11780 since only the current frame needs to be considered. This function needs
11781 to be rewritten with two variables, consider_all_windows and
11782 consider_all_frames. */
11783 consider_all_windows_p = 1;
11784 ++windows_or_buffers_changed;
11785 ++update_mode_lines;
11786
11787 /* If window configuration was changed, frames may have been
11788 marked garbaged. Clear them or we will experience
11789 surprises wrt scrolling. */
11790 if (frame_garbaged)
11791 clear_garbaged_frames ();
11792 }
11793
11794
11795 /* If showing the region, and mark has changed, we must redisplay
11796 the whole window. The assignment to this_line_start_pos prevents
11797 the optimization directly below this if-statement. */
11798 if (((!NILP (Vtransient_mark_mode)
11799 && !NILP (XBUFFER (w->buffer)->mark_active))
11800 != !NILP (w->region_showing))
11801 || (!NILP (w->region_showing)
11802 && !EQ (w->region_showing,
11803 Fmarker_position (XBUFFER (w->buffer)->mark))))
11804 CHARPOS (this_line_start_pos) = 0;
11805
11806 /* Optimize the case that only the line containing the cursor in the
11807 selected window has changed. Variables starting with this_ are
11808 set in display_line and record information about the line
11809 containing the cursor. */
11810 tlbufpos = this_line_start_pos;
11811 tlendpos = this_line_end_pos;
11812 if (!consider_all_windows_p
11813 && CHARPOS (tlbufpos) > 0
11814 && NILP (w->update_mode_line)
11815 && !current_buffer->clip_changed
11816 && !current_buffer->prevent_redisplay_optimizations_p
11817 && FRAME_VISIBLE_P (XFRAME (w->frame))
11818 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11819 /* Make sure recorded data applies to current buffer, etc. */
11820 && this_line_buffer == current_buffer
11821 && current_buffer == XBUFFER (w->buffer)
11822 && NILP (w->force_start)
11823 && NILP (w->optional_new_start)
11824 /* Point must be on the line that we have info recorded about. */
11825 && PT >= CHARPOS (tlbufpos)
11826 && PT <= Z - CHARPOS (tlendpos)
11827 /* All text outside that line, including its final newline,
11828 must be unchanged. */
11829 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11830 CHARPOS (tlendpos)))
11831 {
11832 if (CHARPOS (tlbufpos) > BEGV
11833 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11834 && (CHARPOS (tlbufpos) == ZV
11835 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11836 /* Former continuation line has disappeared by becoming empty. */
11837 goto cancel;
11838 else if (XFASTINT (w->last_modified) < MODIFF
11839 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11840 || MINI_WINDOW_P (w))
11841 {
11842 /* We have to handle the case of continuation around a
11843 wide-column character (see the comment in indent.c around
11844 line 1340).
11845
11846 For instance, in the following case:
11847
11848 -------- Insert --------
11849 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11850 J_I_ ==> J_I_ `^^' are cursors.
11851 ^^ ^^
11852 -------- --------
11853
11854 As we have to redraw the line above, we cannot use this
11855 optimization. */
11856
11857 struct it it;
11858 int line_height_before = this_line_pixel_height;
11859
11860 /* Note that start_display will handle the case that the
11861 line starting at tlbufpos is a continuation line. */
11862 start_display (&it, w, tlbufpos);
11863
11864 /* Implementation note: It this still necessary? */
11865 if (it.current_x != this_line_start_x)
11866 goto cancel;
11867
11868 TRACE ((stderr, "trying display optimization 1\n"));
11869 w->cursor.vpos = -1;
11870 overlay_arrow_seen = 0;
11871 it.vpos = this_line_vpos;
11872 it.current_y = this_line_y;
11873 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11874 display_line (&it);
11875
11876 /* If line contains point, is not continued,
11877 and ends at same distance from eob as before, we win. */
11878 if (w->cursor.vpos >= 0
11879 /* Line is not continued, otherwise this_line_start_pos
11880 would have been set to 0 in display_line. */
11881 && CHARPOS (this_line_start_pos)
11882 /* Line ends as before. */
11883 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11884 /* Line has same height as before. Otherwise other lines
11885 would have to be shifted up or down. */
11886 && this_line_pixel_height == line_height_before)
11887 {
11888 /* If this is not the window's last line, we must adjust
11889 the charstarts of the lines below. */
11890 if (it.current_y < it.last_visible_y)
11891 {
11892 struct glyph_row *row
11893 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11894 int delta, delta_bytes;
11895
11896 /* We used to distinguish between two cases here,
11897 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11898 when the line ends in a newline or the end of the
11899 buffer's accessible portion. But both cases did
11900 the same, so they were collapsed. */
11901 delta = (Z
11902 - CHARPOS (tlendpos)
11903 - MATRIX_ROW_START_CHARPOS (row));
11904 delta_bytes = (Z_BYTE
11905 - BYTEPOS (tlendpos)
11906 - MATRIX_ROW_START_BYTEPOS (row));
11907
11908 increment_matrix_positions (w->current_matrix,
11909 this_line_vpos + 1,
11910 w->current_matrix->nrows,
11911 delta, delta_bytes);
11912 }
11913
11914 /* If this row displays text now but previously didn't,
11915 or vice versa, w->window_end_vpos may have to be
11916 adjusted. */
11917 if ((it.glyph_row - 1)->displays_text_p)
11918 {
11919 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11920 XSETINT (w->window_end_vpos, this_line_vpos);
11921 }
11922 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11923 && this_line_vpos > 0)
11924 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11925 w->window_end_valid = Qnil;
11926
11927 /* Update hint: No need to try to scroll in update_window. */
11928 w->desired_matrix->no_scrolling_p = 1;
11929
11930 #if GLYPH_DEBUG
11931 *w->desired_matrix->method = 0;
11932 debug_method_add (w, "optimization 1");
11933 #endif
11934 #ifdef HAVE_WINDOW_SYSTEM
11935 update_window_fringes (w, 0);
11936 #endif
11937 goto update;
11938 }
11939 else
11940 goto cancel;
11941 }
11942 else if (/* Cursor position hasn't changed. */
11943 PT == XFASTINT (w->last_point)
11944 /* Make sure the cursor was last displayed
11945 in this window. Otherwise we have to reposition it. */
11946 && 0 <= w->cursor.vpos
11947 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11948 {
11949 if (!must_finish)
11950 {
11951 do_pending_window_change (1);
11952
11953 /* We used to always goto end_of_redisplay here, but this
11954 isn't enough if we have a blinking cursor. */
11955 if (w->cursor_off_p == w->last_cursor_off_p)
11956 goto end_of_redisplay;
11957 }
11958 goto update;
11959 }
11960 /* If highlighting the region, or if the cursor is in the echo area,
11961 then we can't just move the cursor. */
11962 else if (! (!NILP (Vtransient_mark_mode)
11963 && !NILP (current_buffer->mark_active))
11964 && (EQ (selected_window, current_buffer->last_selected_window)
11965 || highlight_nonselected_windows)
11966 && NILP (w->region_showing)
11967 && NILP (Vshow_trailing_whitespace)
11968 && !cursor_in_echo_area)
11969 {
11970 struct it it;
11971 struct glyph_row *row;
11972
11973 /* Skip from tlbufpos to PT and see where it is. Note that
11974 PT may be in invisible text. If so, we will end at the
11975 next visible position. */
11976 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11977 NULL, DEFAULT_FACE_ID);
11978 it.current_x = this_line_start_x;
11979 it.current_y = this_line_y;
11980 it.vpos = this_line_vpos;
11981
11982 /* The call to move_it_to stops in front of PT, but
11983 moves over before-strings. */
11984 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11985
11986 if (it.vpos == this_line_vpos
11987 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11988 row->enabled_p))
11989 {
11990 xassert (this_line_vpos == it.vpos);
11991 xassert (this_line_y == it.current_y);
11992 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11993 #if GLYPH_DEBUG
11994 *w->desired_matrix->method = 0;
11995 debug_method_add (w, "optimization 3");
11996 #endif
11997 goto update;
11998 }
11999 else
12000 goto cancel;
12001 }
12002
12003 cancel:
12004 /* Text changed drastically or point moved off of line. */
12005 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12006 }
12007
12008 CHARPOS (this_line_start_pos) = 0;
12009 consider_all_windows_p |= buffer_shared > 1;
12010 ++clear_face_cache_count;
12011 #ifdef HAVE_WINDOW_SYSTEM
12012 ++clear_image_cache_count;
12013 #endif
12014
12015 /* Build desired matrices, and update the display. If
12016 consider_all_windows_p is non-zero, do it for all windows on all
12017 frames. Otherwise do it for selected_window, only. */
12018
12019 if (consider_all_windows_p)
12020 {
12021 Lisp_Object tail, frame;
12022
12023 FOR_EACH_FRAME (tail, frame)
12024 XFRAME (frame)->updated_p = 0;
12025
12026 /* Recompute # windows showing selected buffer. This will be
12027 incremented each time such a window is displayed. */
12028 buffer_shared = 0;
12029
12030 FOR_EACH_FRAME (tail, frame)
12031 {
12032 struct frame *f = XFRAME (frame);
12033
12034 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12035 {
12036 if (! EQ (frame, selected_frame))
12037 /* Select the frame, for the sake of frame-local
12038 variables. */
12039 select_frame_for_redisplay (frame);
12040
12041 /* Mark all the scroll bars to be removed; we'll redeem
12042 the ones we want when we redisplay their windows. */
12043 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12044 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12045
12046 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12047 redisplay_windows (FRAME_ROOT_WINDOW (f));
12048
12049 /* The X error handler may have deleted that frame. */
12050 if (!FRAME_LIVE_P (f))
12051 continue;
12052
12053 /* Any scroll bars which redisplay_windows should have
12054 nuked should now go away. */
12055 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12056 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12057
12058 /* If fonts changed, display again. */
12059 /* ??? rms: I suspect it is a mistake to jump all the way
12060 back to retry here. It should just retry this frame. */
12061 if (fonts_changed_p)
12062 goto retry;
12063
12064 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12065 {
12066 /* See if we have to hscroll. */
12067 if (!f->already_hscrolled_p)
12068 {
12069 f->already_hscrolled_p = 1;
12070 if (hscroll_windows (f->root_window))
12071 goto retry;
12072 }
12073
12074 /* Prevent various kinds of signals during display
12075 update. stdio is not robust about handling
12076 signals, which can cause an apparent I/O
12077 error. */
12078 if (interrupt_input)
12079 unrequest_sigio ();
12080 STOP_POLLING;
12081
12082 /* Update the display. */
12083 set_window_update_flags (XWINDOW (f->root_window), 1);
12084 pause |= update_frame (f, 0, 0);
12085 f->updated_p = 1;
12086 }
12087 }
12088 }
12089
12090 if (!EQ (old_frame, selected_frame)
12091 && FRAME_LIVE_P (XFRAME (old_frame)))
12092 /* We played a bit fast-and-loose above and allowed selected_frame
12093 and selected_window to be temporarily out-of-sync but let's make
12094 sure this stays contained. */
12095 select_frame_for_redisplay (old_frame);
12096 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12097
12098 if (!pause)
12099 {
12100 /* Do the mark_window_display_accurate after all windows have
12101 been redisplayed because this call resets flags in buffers
12102 which are needed for proper redisplay. */
12103 FOR_EACH_FRAME (tail, frame)
12104 {
12105 struct frame *f = XFRAME (frame);
12106 if (f->updated_p)
12107 {
12108 mark_window_display_accurate (f->root_window, 1);
12109 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12110 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12111 }
12112 }
12113 }
12114 }
12115 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12116 {
12117 Lisp_Object mini_window;
12118 struct frame *mini_frame;
12119
12120 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12121 /* Use list_of_error, not Qerror, so that
12122 we catch only errors and don't run the debugger. */
12123 internal_condition_case_1 (redisplay_window_1, selected_window,
12124 list_of_error,
12125 redisplay_window_error);
12126
12127 /* Compare desired and current matrices, perform output. */
12128
12129 update:
12130 /* If fonts changed, display again. */
12131 if (fonts_changed_p)
12132 goto retry;
12133
12134 /* Prevent various kinds of signals during display update.
12135 stdio is not robust about handling signals,
12136 which can cause an apparent I/O error. */
12137 if (interrupt_input)
12138 unrequest_sigio ();
12139 STOP_POLLING;
12140
12141 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12142 {
12143 if (hscroll_windows (selected_window))
12144 goto retry;
12145
12146 XWINDOW (selected_window)->must_be_updated_p = 1;
12147 pause = update_frame (sf, 0, 0);
12148 }
12149
12150 /* We may have called echo_area_display at the top of this
12151 function. If the echo area is on another frame, that may
12152 have put text on a frame other than the selected one, so the
12153 above call to update_frame would not have caught it. Catch
12154 it here. */
12155 mini_window = FRAME_MINIBUF_WINDOW (sf);
12156 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12157
12158 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12159 {
12160 XWINDOW (mini_window)->must_be_updated_p = 1;
12161 pause |= update_frame (mini_frame, 0, 0);
12162 if (!pause && hscroll_windows (mini_window))
12163 goto retry;
12164 }
12165 }
12166
12167 /* If display was paused because of pending input, make sure we do a
12168 thorough update the next time. */
12169 if (pause)
12170 {
12171 /* Prevent the optimization at the beginning of
12172 redisplay_internal that tries a single-line update of the
12173 line containing the cursor in the selected window. */
12174 CHARPOS (this_line_start_pos) = 0;
12175
12176 /* Let the overlay arrow be updated the next time. */
12177 update_overlay_arrows (0);
12178
12179 /* If we pause after scrolling, some rows in the current
12180 matrices of some windows are not valid. */
12181 if (!WINDOW_FULL_WIDTH_P (w)
12182 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12183 update_mode_lines = 1;
12184 }
12185 else
12186 {
12187 if (!consider_all_windows_p)
12188 {
12189 /* This has already been done above if
12190 consider_all_windows_p is set. */
12191 mark_window_display_accurate_1 (w, 1);
12192
12193 /* Say overlay arrows are up to date. */
12194 update_overlay_arrows (1);
12195
12196 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12197 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12198 }
12199
12200 update_mode_lines = 0;
12201 windows_or_buffers_changed = 0;
12202 cursor_type_changed = 0;
12203 }
12204
12205 /* Start SIGIO interrupts coming again. Having them off during the
12206 code above makes it less likely one will discard output, but not
12207 impossible, since there might be stuff in the system buffer here.
12208 But it is much hairier to try to do anything about that. */
12209 if (interrupt_input)
12210 request_sigio ();
12211 RESUME_POLLING;
12212
12213 /* If a frame has become visible which was not before, redisplay
12214 again, so that we display it. Expose events for such a frame
12215 (which it gets when becoming visible) don't call the parts of
12216 redisplay constructing glyphs, so simply exposing a frame won't
12217 display anything in this case. So, we have to display these
12218 frames here explicitly. */
12219 if (!pause)
12220 {
12221 Lisp_Object tail, frame;
12222 int new_count = 0;
12223
12224 FOR_EACH_FRAME (tail, frame)
12225 {
12226 int this_is_visible = 0;
12227
12228 if (XFRAME (frame)->visible)
12229 this_is_visible = 1;
12230 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12231 if (XFRAME (frame)->visible)
12232 this_is_visible = 1;
12233
12234 if (this_is_visible)
12235 new_count++;
12236 }
12237
12238 if (new_count != number_of_visible_frames)
12239 windows_or_buffers_changed++;
12240 }
12241
12242 /* Change frame size now if a change is pending. */
12243 do_pending_window_change (1);
12244
12245 /* If we just did a pending size change, or have additional
12246 visible frames, redisplay again. */
12247 if (windows_or_buffers_changed && !pause)
12248 goto retry;
12249
12250 /* Clear the face cache eventually. */
12251 if (consider_all_windows_p)
12252 {
12253 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12254 {
12255 clear_face_cache (0);
12256 clear_face_cache_count = 0;
12257 }
12258 #ifdef HAVE_WINDOW_SYSTEM
12259 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12260 {
12261 clear_image_caches (Qnil);
12262 clear_image_cache_count = 0;
12263 }
12264 #endif /* HAVE_WINDOW_SYSTEM */
12265 }
12266
12267 end_of_redisplay:
12268 unbind_to (count, Qnil);
12269 RESUME_POLLING;
12270 }
12271
12272
12273 /* Redisplay, but leave alone any recent echo area message unless
12274 another message has been requested in its place.
12275
12276 This is useful in situations where you need to redisplay but no
12277 user action has occurred, making it inappropriate for the message
12278 area to be cleared. See tracking_off and
12279 wait_reading_process_output for examples of these situations.
12280
12281 FROM_WHERE is an integer saying from where this function was
12282 called. This is useful for debugging. */
12283
12284 void
12285 redisplay_preserve_echo_area (from_where)
12286 int from_where;
12287 {
12288 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12289
12290 if (!NILP (echo_area_buffer[1]))
12291 {
12292 /* We have a previously displayed message, but no current
12293 message. Redisplay the previous message. */
12294 display_last_displayed_message_p = 1;
12295 redisplay_internal (1);
12296 display_last_displayed_message_p = 0;
12297 }
12298 else
12299 redisplay_internal (1);
12300
12301 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12302 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12303 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12304 }
12305
12306
12307 /* Function registered with record_unwind_protect in
12308 redisplay_internal. Reset redisplaying_p to the value it had
12309 before redisplay_internal was called, and clear
12310 prevent_freeing_realized_faces_p. It also selects the previously
12311 selected frame, unless it has been deleted (by an X connection
12312 failure during redisplay, for example). */
12313
12314 static Lisp_Object
12315 unwind_redisplay (val)
12316 Lisp_Object val;
12317 {
12318 Lisp_Object old_redisplaying_p, old_frame;
12319
12320 old_redisplaying_p = XCAR (val);
12321 redisplaying_p = XFASTINT (old_redisplaying_p);
12322 old_frame = XCDR (val);
12323 if (! EQ (old_frame, selected_frame)
12324 && FRAME_LIVE_P (XFRAME (old_frame)))
12325 select_frame_for_redisplay (old_frame);
12326 return Qnil;
12327 }
12328
12329
12330 /* Mark the display of window W as accurate or inaccurate. If
12331 ACCURATE_P is non-zero mark display of W as accurate. If
12332 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12333 redisplay_internal is called. */
12334
12335 static void
12336 mark_window_display_accurate_1 (w, accurate_p)
12337 struct window *w;
12338 int accurate_p;
12339 {
12340 if (BUFFERP (w->buffer))
12341 {
12342 struct buffer *b = XBUFFER (w->buffer);
12343
12344 w->last_modified
12345 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12346 w->last_overlay_modified
12347 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12348 w->last_had_star
12349 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12350
12351 if (accurate_p)
12352 {
12353 b->clip_changed = 0;
12354 b->prevent_redisplay_optimizations_p = 0;
12355
12356 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12357 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12358 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12359 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12360
12361 w->current_matrix->buffer = b;
12362 w->current_matrix->begv = BUF_BEGV (b);
12363 w->current_matrix->zv = BUF_ZV (b);
12364
12365 w->last_cursor = w->cursor;
12366 w->last_cursor_off_p = w->cursor_off_p;
12367
12368 if (w == XWINDOW (selected_window))
12369 w->last_point = make_number (BUF_PT (b));
12370 else
12371 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12372 }
12373 }
12374
12375 if (accurate_p)
12376 {
12377 w->window_end_valid = w->buffer;
12378 w->update_mode_line = Qnil;
12379 }
12380 }
12381
12382
12383 /* Mark the display of windows in the window tree rooted at WINDOW as
12384 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12385 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12386 be redisplayed the next time redisplay_internal is called. */
12387
12388 void
12389 mark_window_display_accurate (window, accurate_p)
12390 Lisp_Object window;
12391 int accurate_p;
12392 {
12393 struct window *w;
12394
12395 for (; !NILP (window); window = w->next)
12396 {
12397 w = XWINDOW (window);
12398 mark_window_display_accurate_1 (w, accurate_p);
12399
12400 if (!NILP (w->vchild))
12401 mark_window_display_accurate (w->vchild, accurate_p);
12402 if (!NILP (w->hchild))
12403 mark_window_display_accurate (w->hchild, accurate_p);
12404 }
12405
12406 if (accurate_p)
12407 {
12408 update_overlay_arrows (1);
12409 }
12410 else
12411 {
12412 /* Force a thorough redisplay the next time by setting
12413 last_arrow_position and last_arrow_string to t, which is
12414 unequal to any useful value of Voverlay_arrow_... */
12415 update_overlay_arrows (-1);
12416 }
12417 }
12418
12419
12420 /* Return value in display table DP (Lisp_Char_Table *) for character
12421 C. Since a display table doesn't have any parent, we don't have to
12422 follow parent. Do not call this function directly but use the
12423 macro DISP_CHAR_VECTOR. */
12424
12425 Lisp_Object
12426 disp_char_vector (dp, c)
12427 struct Lisp_Char_Table *dp;
12428 int c;
12429 {
12430 Lisp_Object val;
12431
12432 if (ASCII_CHAR_P (c))
12433 {
12434 val = dp->ascii;
12435 if (SUB_CHAR_TABLE_P (val))
12436 val = XSUB_CHAR_TABLE (val)->contents[c];
12437 }
12438 else
12439 {
12440 Lisp_Object table;
12441
12442 XSETCHAR_TABLE (table, dp);
12443 val = char_table_ref (table, c);
12444 }
12445 if (NILP (val))
12446 val = dp->defalt;
12447 return val;
12448 }
12449
12450
12451 \f
12452 /***********************************************************************
12453 Window Redisplay
12454 ***********************************************************************/
12455
12456 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12457
12458 static void
12459 redisplay_windows (window)
12460 Lisp_Object window;
12461 {
12462 while (!NILP (window))
12463 {
12464 struct window *w = XWINDOW (window);
12465
12466 if (!NILP (w->hchild))
12467 redisplay_windows (w->hchild);
12468 else if (!NILP (w->vchild))
12469 redisplay_windows (w->vchild);
12470 else if (!NILP (w->buffer))
12471 {
12472 displayed_buffer = XBUFFER (w->buffer);
12473 /* Use list_of_error, not Qerror, so that
12474 we catch only errors and don't run the debugger. */
12475 internal_condition_case_1 (redisplay_window_0, window,
12476 list_of_error,
12477 redisplay_window_error);
12478 }
12479
12480 window = w->next;
12481 }
12482 }
12483
12484 static Lisp_Object
12485 redisplay_window_error ()
12486 {
12487 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12488 return Qnil;
12489 }
12490
12491 static Lisp_Object
12492 redisplay_window_0 (window)
12493 Lisp_Object window;
12494 {
12495 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12496 redisplay_window (window, 0);
12497 return Qnil;
12498 }
12499
12500 static Lisp_Object
12501 redisplay_window_1 (window)
12502 Lisp_Object window;
12503 {
12504 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12505 redisplay_window (window, 1);
12506 return Qnil;
12507 }
12508 \f
12509
12510 /* Increment GLYPH until it reaches END or CONDITION fails while
12511 adding (GLYPH)->pixel_width to X. */
12512
12513 #define SKIP_GLYPHS(glyph, end, x, condition) \
12514 do \
12515 { \
12516 (x) += (glyph)->pixel_width; \
12517 ++(glyph); \
12518 } \
12519 while ((glyph) < (end) && (condition))
12520
12521
12522 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12523 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12524 which positions recorded in ROW differ from current buffer
12525 positions.
12526
12527 Return 0 if cursor is not on this row, 1 otherwise. */
12528
12529 int
12530 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12531 struct window *w;
12532 struct glyph_row *row;
12533 struct glyph_matrix *matrix;
12534 int delta, delta_bytes, dy, dvpos;
12535 {
12536 struct glyph *glyph = row->glyphs[TEXT_AREA];
12537 struct glyph *end = glyph + row->used[TEXT_AREA];
12538 struct glyph *cursor = NULL;
12539 /* The last known character position in row. */
12540 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12541 int x = row->x;
12542 int cursor_x = x;
12543 EMACS_INT pt_old = PT - delta;
12544 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12545 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12546 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12547 /* Non-zero means we've found a match for cursor position, but that
12548 glyph has the avoid_cursor_p flag set. */
12549 int match_with_avoid_cursor = 0;
12550 /* Non-zero means we've seen at least one glyph that came from a
12551 display string. */
12552 int string_seen = 0;
12553 /* Largest buffer position seen so far during scan of glyph row. */
12554 EMACS_INT bpos_max = last_pos;
12555 /* Last buffer position covered by an overlay string with an integer
12556 `cursor' property. */
12557 EMACS_INT bpos_covered = 0;
12558
12559 /* Skip over glyphs not having an object at the start and the end of
12560 the row. These are special glyphs like truncation marks on
12561 terminal frames. */
12562 if (row->displays_text_p)
12563 {
12564 if (!row->reversed_p)
12565 {
12566 while (glyph < end
12567 && INTEGERP (glyph->object)
12568 && glyph->charpos < 0)
12569 {
12570 x += glyph->pixel_width;
12571 ++glyph;
12572 }
12573 while (end > glyph
12574 && INTEGERP ((end - 1)->object)
12575 /* CHARPOS is zero for blanks inserted by
12576 extend_face_to_end_of_line. */
12577 && (end - 1)->charpos <= 0)
12578 --end;
12579 glyph_before = glyph - 1;
12580 glyph_after = end;
12581 }
12582 else
12583 {
12584 struct glyph *g;
12585
12586 /* If the glyph row is reversed, we need to process it from back
12587 to front, so swap the edge pointers. */
12588 end = glyph - 1;
12589 glyph += row->used[TEXT_AREA] - 1;
12590 /* Reverse the known positions in the row. */
12591 last_pos = pos_after = MATRIX_ROW_START_CHARPOS (row) + delta;
12592 pos_before = MATRIX_ROW_END_CHARPOS (row) + delta;
12593
12594 while (glyph > end + 1
12595 && INTEGERP (glyph->object)
12596 && glyph->charpos < 0)
12597 {
12598 --glyph;
12599 x -= glyph->pixel_width;
12600 }
12601 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12602 --glyph;
12603 /* By default, put the cursor on the rightmost glyph. */
12604 for (g = end + 1; g < glyph; g++)
12605 x += g->pixel_width;
12606 cursor_x = x;
12607 while (end < glyph
12608 && INTEGERP ((end + 1)->object)
12609 && (end + 1)->charpos <= 0)
12610 ++end;
12611 glyph_before = glyph + 1;
12612 glyph_after = end;
12613 }
12614 }
12615 else if (row->reversed_p)
12616 {
12617 /* In R2L rows that don't display text, put the cursor on the
12618 rightmost glyph. Case in point: an empty last line that is
12619 part of an R2L paragraph. */
12620 cursor = end - 1;
12621 x = -1; /* will be computed below, at lable compute_x */
12622 }
12623
12624 /* Step 1: Try to find the glyph whose character position
12625 corresponds to point. If that's not possible, find 2 glyphs
12626 whose character positions are the closest to point, one before
12627 point, the other after it. */
12628 if (!row->reversed_p)
12629 while (/* not marched to end of glyph row */
12630 glyph < end
12631 /* glyph was not inserted by redisplay for internal purposes */
12632 && !INTEGERP (glyph->object))
12633 {
12634 if (BUFFERP (glyph->object))
12635 {
12636 EMACS_INT dpos = glyph->charpos - pt_old;
12637
12638 if (glyph->charpos > bpos_max)
12639 bpos_max = glyph->charpos;
12640 if (!glyph->avoid_cursor_p)
12641 {
12642 /* If we hit point, we've found the glyph on which to
12643 display the cursor. */
12644 if (dpos == 0)
12645 {
12646 match_with_avoid_cursor = 0;
12647 break;
12648 }
12649 /* See if we've found a better approximation to
12650 POS_BEFORE or to POS_AFTER. Note that we want the
12651 first (leftmost) glyph of all those that are the
12652 closest from below, and the last (rightmost) of all
12653 those from above. */
12654 if (0 > dpos && dpos > pos_before - pt_old)
12655 {
12656 pos_before = glyph->charpos;
12657 glyph_before = glyph;
12658 }
12659 else if (0 < dpos && dpos <= pos_after - pt_old)
12660 {
12661 pos_after = glyph->charpos;
12662 glyph_after = glyph;
12663 }
12664 }
12665 else if (dpos == 0)
12666 match_with_avoid_cursor = 1;
12667 }
12668 else if (STRINGP (glyph->object))
12669 {
12670 Lisp_Object chprop;
12671 int glyph_pos = glyph->charpos;
12672
12673 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12674 glyph->object);
12675 if (INTEGERP (chprop))
12676 {
12677 bpos_covered = bpos_max + XINT (chprop);
12678 /* If the `cursor' property covers buffer positions up
12679 to and including point, we should display cursor on
12680 this glyph. */
12681 /* Implementation note: bpos_max == pt_old when, e.g.,
12682 we are in an empty line, where bpos_max is set to
12683 MATRIX_ROW_START_CHARPOS, see above. */
12684 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12685 {
12686 cursor = glyph;
12687 break;
12688 }
12689 }
12690
12691 string_seen = 1;
12692 }
12693 x += glyph->pixel_width;
12694 ++glyph;
12695 }
12696 else if (glyph > end) /* row is reversed */
12697 while (!INTEGERP (glyph->object))
12698 {
12699 if (BUFFERP (glyph->object))
12700 {
12701 EMACS_INT dpos = glyph->charpos - pt_old;
12702
12703 if (glyph->charpos > bpos_max)
12704 bpos_max = glyph->charpos;
12705 if (!glyph->avoid_cursor_p)
12706 {
12707 if (dpos == 0)
12708 {
12709 match_with_avoid_cursor = 0;
12710 break;
12711 }
12712 if (0 > dpos && dpos > pos_before - pt_old)
12713 {
12714 pos_before = glyph->charpos;
12715 glyph_before = glyph;
12716 }
12717 else if (0 < dpos && dpos <= pos_after - pt_old)
12718 {
12719 pos_after = glyph->charpos;
12720 glyph_after = glyph;
12721 }
12722 }
12723 else if (dpos == 0)
12724 match_with_avoid_cursor = 1;
12725 }
12726 else if (STRINGP (glyph->object))
12727 {
12728 Lisp_Object chprop;
12729 int glyph_pos = glyph->charpos;
12730
12731 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12732 glyph->object);
12733 if (INTEGERP (chprop))
12734 {
12735 bpos_covered = bpos_max + XINT (chprop);
12736 /* If the `cursor' property covers buffer positions up
12737 to and including point, we should display cursor on
12738 this glyph. */
12739 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12740 {
12741 cursor = glyph;
12742 break;
12743 }
12744 }
12745 string_seen = 1;
12746 }
12747 --glyph;
12748 if (glyph == end)
12749 break;
12750 x -= glyph->pixel_width;
12751 }
12752
12753 /* Step 2: If we didn't find an exact match for point, we need to
12754 look for a proper place to put the cursor among glyphs between
12755 GLYPH_BEFORE and GLYPH_AFTER. */
12756 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12757 && bpos_covered < pt_old)
12758 {
12759 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12760 {
12761 EMACS_INT ellipsis_pos;
12762
12763 /* Scan back over the ellipsis glyphs. */
12764 if (!row->reversed_p)
12765 {
12766 ellipsis_pos = (glyph - 1)->charpos;
12767 while (glyph > row->glyphs[TEXT_AREA]
12768 && (glyph - 1)->charpos == ellipsis_pos)
12769 glyph--, x -= glyph->pixel_width;
12770 /* That loop always goes one position too far, including
12771 the glyph before the ellipsis. So scan forward over
12772 that one. */
12773 x += glyph->pixel_width;
12774 glyph++;
12775 }
12776 else /* row is reversed */
12777 {
12778 ellipsis_pos = (glyph + 1)->charpos;
12779 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12780 && (glyph + 1)->charpos == ellipsis_pos)
12781 glyph++, x += glyph->pixel_width;
12782 x -= glyph->pixel_width;
12783 glyph--;
12784 }
12785 }
12786 else if (match_with_avoid_cursor
12787 /* zero-width characters produce no glyphs */
12788 || eabs (glyph_after - glyph_before) == 1)
12789 {
12790 cursor = glyph_after;
12791 x = -1;
12792 }
12793 else if (string_seen)
12794 {
12795 int incr = row->reversed_p ? -1 : +1;
12796
12797 /* Need to find the glyph that came out of a string which is
12798 present at point. That glyph is somewhere between
12799 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12800 positioned between POS_BEFORE and POS_AFTER in the
12801 buffer. */
12802 struct glyph *stop = glyph_after;
12803 EMACS_INT pos = pos_before;
12804
12805 x = -1;
12806 for (glyph = glyph_before + incr;
12807 row->reversed_p ? glyph > stop : glyph < stop; )
12808 {
12809
12810 /* Any glyphs that come from the buffer are here because
12811 of bidi reordering. Skip them, and only pay
12812 attention to glyphs that came from some string. */
12813 if (STRINGP (glyph->object))
12814 {
12815 Lisp_Object str;
12816 EMACS_INT tem;
12817
12818 str = glyph->object;
12819 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12820 if (pos <= tem)
12821 {
12822 /* If the string from which this glyph came is
12823 found in the buffer at point, then we've
12824 found the glyph we've been looking for. */
12825 if (tem == pt_old)
12826 {
12827 /* The glyphs from this string could have
12828 been reordered. Find the one with the
12829 smallest string position. Or there could
12830 be a character in the string with the
12831 `cursor' property, which means display
12832 cursor on that character's glyph. */
12833 int strpos = glyph->charpos;
12834
12835 cursor = glyph;
12836 for (glyph += incr;
12837 EQ (glyph->object, str);
12838 glyph += incr)
12839 {
12840 Lisp_Object cprop;
12841 int gpos = glyph->charpos;
12842
12843 cprop = Fget_char_property (make_number (gpos),
12844 Qcursor,
12845 glyph->object);
12846 if (!NILP (cprop))
12847 {
12848 cursor = glyph;
12849 break;
12850 }
12851 if (glyph->charpos < strpos)
12852 {
12853 strpos = glyph->charpos;
12854 cursor = glyph;
12855 }
12856 }
12857
12858 goto compute_x;
12859 }
12860 pos = tem + 1; /* don't find previous instances */
12861 }
12862 /* This string is not what we want; skip all of the
12863 glyphs that came from it. */
12864 do
12865 glyph += incr;
12866 while ((row->reversed_p ? glyph > stop : glyph < stop)
12867 && EQ (glyph->object, str));
12868 }
12869 else
12870 glyph += incr;
12871 }
12872
12873 /* If we reached the end of the line, and END was from a string,
12874 the cursor is not on this line. */
12875 if (glyph == end
12876 && STRINGP ((glyph - incr)->object)
12877 && row->continued_p)
12878 return 0;
12879 }
12880 }
12881
12882 compute_x:
12883 if (cursor != NULL)
12884 glyph = cursor;
12885 if (x < 0)
12886 {
12887 struct glyph *g;
12888
12889 /* Need to compute x that corresponds to GLYPH. */
12890 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12891 {
12892 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12893 abort ();
12894 x += g->pixel_width;
12895 }
12896 }
12897
12898 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12899 w->cursor.x = x;
12900 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12901 w->cursor.y = row->y + dy;
12902
12903 if (w == XWINDOW (selected_window))
12904 {
12905 if (!row->continued_p
12906 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12907 && row->x == 0)
12908 {
12909 this_line_buffer = XBUFFER (w->buffer);
12910
12911 CHARPOS (this_line_start_pos)
12912 = MATRIX_ROW_START_CHARPOS (row) + delta;
12913 BYTEPOS (this_line_start_pos)
12914 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12915
12916 CHARPOS (this_line_end_pos)
12917 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12918 BYTEPOS (this_line_end_pos)
12919 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12920
12921 this_line_y = w->cursor.y;
12922 this_line_pixel_height = row->height;
12923 this_line_vpos = w->cursor.vpos;
12924 this_line_start_x = row->x;
12925 }
12926 else
12927 CHARPOS (this_line_start_pos) = 0;
12928 }
12929
12930 return 1;
12931 }
12932
12933
12934 /* Run window scroll functions, if any, for WINDOW with new window
12935 start STARTP. Sets the window start of WINDOW to that position.
12936
12937 We assume that the window's buffer is really current. */
12938
12939 static INLINE struct text_pos
12940 run_window_scroll_functions (window, startp)
12941 Lisp_Object window;
12942 struct text_pos startp;
12943 {
12944 struct window *w = XWINDOW (window);
12945 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12946
12947 if (current_buffer != XBUFFER (w->buffer))
12948 abort ();
12949
12950 if (!NILP (Vwindow_scroll_functions))
12951 {
12952 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12953 make_number (CHARPOS (startp)));
12954 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12955 /* In case the hook functions switch buffers. */
12956 if (current_buffer != XBUFFER (w->buffer))
12957 set_buffer_internal_1 (XBUFFER (w->buffer));
12958 }
12959
12960 return startp;
12961 }
12962
12963
12964 /* Make sure the line containing the cursor is fully visible.
12965 A value of 1 means there is nothing to be done.
12966 (Either the line is fully visible, or it cannot be made so,
12967 or we cannot tell.)
12968
12969 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12970 is higher than window.
12971
12972 A value of 0 means the caller should do scrolling
12973 as if point had gone off the screen. */
12974
12975 static int
12976 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12977 struct window *w;
12978 int force_p;
12979 int current_matrix_p;
12980 {
12981 struct glyph_matrix *matrix;
12982 struct glyph_row *row;
12983 int window_height;
12984
12985 if (!make_cursor_line_fully_visible_p)
12986 return 1;
12987
12988 /* It's not always possible to find the cursor, e.g, when a window
12989 is full of overlay strings. Don't do anything in that case. */
12990 if (w->cursor.vpos < 0)
12991 return 1;
12992
12993 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12994 row = MATRIX_ROW (matrix, w->cursor.vpos);
12995
12996 /* If the cursor row is not partially visible, there's nothing to do. */
12997 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12998 return 1;
12999
13000 /* If the row the cursor is in is taller than the window's height,
13001 it's not clear what to do, so do nothing. */
13002 window_height = window_box_height (w);
13003 if (row->height >= window_height)
13004 {
13005 if (!force_p || MINI_WINDOW_P (w)
13006 || w->vscroll || w->cursor.vpos == 0)
13007 return 1;
13008 }
13009 return 0;
13010 }
13011
13012
13013 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13014 non-zero means only WINDOW is redisplayed in redisplay_internal.
13015 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13016 in redisplay_window to bring a partially visible line into view in
13017 the case that only the cursor has moved.
13018
13019 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13020 last screen line's vertical height extends past the end of the screen.
13021
13022 Value is
13023
13024 1 if scrolling succeeded
13025
13026 0 if scrolling didn't find point.
13027
13028 -1 if new fonts have been loaded so that we must interrupt
13029 redisplay, adjust glyph matrices, and try again. */
13030
13031 enum
13032 {
13033 SCROLLING_SUCCESS,
13034 SCROLLING_FAILED,
13035 SCROLLING_NEED_LARGER_MATRICES
13036 };
13037
13038 static int
13039 try_scrolling (window, just_this_one_p, scroll_conservatively,
13040 scroll_step, temp_scroll_step, last_line_misfit)
13041 Lisp_Object window;
13042 int just_this_one_p;
13043 EMACS_INT scroll_conservatively, scroll_step;
13044 int temp_scroll_step;
13045 int last_line_misfit;
13046 {
13047 struct window *w = XWINDOW (window);
13048 struct frame *f = XFRAME (w->frame);
13049 struct text_pos pos, startp;
13050 struct it it;
13051 int this_scroll_margin, scroll_max, rc, height;
13052 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13053 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13054 Lisp_Object aggressive;
13055 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13056
13057 #if GLYPH_DEBUG
13058 debug_method_add (w, "try_scrolling");
13059 #endif
13060
13061 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13062
13063 /* Compute scroll margin height in pixels. We scroll when point is
13064 within this distance from the top or bottom of the window. */
13065 if (scroll_margin > 0)
13066 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13067 * FRAME_LINE_HEIGHT (f);
13068 else
13069 this_scroll_margin = 0;
13070
13071 /* Force scroll_conservatively to have a reasonable value, to avoid
13072 overflow while computing how much to scroll. Note that the user
13073 can supply scroll-conservatively equal to `most-positive-fixnum',
13074 which can be larger than INT_MAX. */
13075 if (scroll_conservatively > scroll_limit)
13076 {
13077 scroll_conservatively = scroll_limit;
13078 scroll_max = INT_MAX;
13079 }
13080 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13081 /* Compute how much we should try to scroll maximally to bring
13082 point into view. */
13083 scroll_max = (max (scroll_step,
13084 max (scroll_conservatively, temp_scroll_step))
13085 * FRAME_LINE_HEIGHT (f));
13086 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13087 || NUMBERP (current_buffer->scroll_up_aggressively))
13088 /* We're trying to scroll because of aggressive scrolling but no
13089 scroll_step is set. Choose an arbitrary one. */
13090 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13091 else
13092 scroll_max = 0;
13093
13094 too_near_end:
13095
13096 /* Decide whether to scroll down. */
13097 if (PT > CHARPOS (startp))
13098 {
13099 int scroll_margin_y;
13100
13101 /* Compute the pixel ypos of the scroll margin, then move it to
13102 either that ypos or PT, whichever comes first. */
13103 start_display (&it, w, startp);
13104 scroll_margin_y = it.last_visible_y - this_scroll_margin
13105 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13106 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13107 (MOVE_TO_POS | MOVE_TO_Y));
13108
13109 if (PT > CHARPOS (it.current.pos))
13110 {
13111 int y0 = line_bottom_y (&it);
13112
13113 /* Compute the distance from the scroll margin to PT
13114 (including the height of the cursor line). Moving the
13115 iterator unconditionally to PT can be slow if PT is far
13116 away, so stop 10 lines past the window bottom (is there a
13117 way to do the right thing quickly?). */
13118 move_it_to (&it, PT, -1,
13119 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
13120 -1, MOVE_TO_POS | MOVE_TO_Y);
13121 dy = line_bottom_y (&it) - y0;
13122
13123 if (dy > scroll_max)
13124 return SCROLLING_FAILED;
13125
13126 scroll_down_p = 1;
13127 }
13128 }
13129
13130 if (scroll_down_p)
13131 {
13132 /* Point is in or below the bottom scroll margin, so move the
13133 window start down. If scrolling conservatively, move it just
13134 enough down to make point visible. If scroll_step is set,
13135 move it down by scroll_step. */
13136 if (scroll_conservatively)
13137 amount_to_scroll
13138 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13139 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13140 else if (scroll_step || temp_scroll_step)
13141 amount_to_scroll = scroll_max;
13142 else
13143 {
13144 aggressive = current_buffer->scroll_up_aggressively;
13145 height = WINDOW_BOX_TEXT_HEIGHT (w);
13146 if (NUMBERP (aggressive))
13147 {
13148 double float_amount = XFLOATINT (aggressive) * height;
13149 amount_to_scroll = float_amount;
13150 if (amount_to_scroll == 0 && float_amount > 0)
13151 amount_to_scroll = 1;
13152 }
13153 }
13154
13155 if (amount_to_scroll <= 0)
13156 return SCROLLING_FAILED;
13157
13158 start_display (&it, w, startp);
13159 move_it_vertically (&it, amount_to_scroll);
13160
13161 /* If STARTP is unchanged, move it down another screen line. */
13162 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13163 move_it_by_lines (&it, 1, 1);
13164 startp = it.current.pos;
13165 }
13166 else
13167 {
13168 struct text_pos scroll_margin_pos = startp;
13169
13170 /* See if point is inside the scroll margin at the top of the
13171 window. */
13172 if (this_scroll_margin)
13173 {
13174 start_display (&it, w, startp);
13175 move_it_vertically (&it, this_scroll_margin);
13176 scroll_margin_pos = it.current.pos;
13177 }
13178
13179 if (PT < CHARPOS (scroll_margin_pos))
13180 {
13181 /* Point is in the scroll margin at the top of the window or
13182 above what is displayed in the window. */
13183 int y0;
13184
13185 /* Compute the vertical distance from PT to the scroll
13186 margin position. Give up if distance is greater than
13187 scroll_max. */
13188 SET_TEXT_POS (pos, PT, PT_BYTE);
13189 start_display (&it, w, pos);
13190 y0 = it.current_y;
13191 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13192 it.last_visible_y, -1,
13193 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13194 dy = it.current_y - y0;
13195 if (dy > scroll_max)
13196 return SCROLLING_FAILED;
13197
13198 /* Compute new window start. */
13199 start_display (&it, w, startp);
13200
13201 if (scroll_conservatively)
13202 amount_to_scroll
13203 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13204 else if (scroll_step || temp_scroll_step)
13205 amount_to_scroll = scroll_max;
13206 else
13207 {
13208 aggressive = current_buffer->scroll_down_aggressively;
13209 height = WINDOW_BOX_TEXT_HEIGHT (w);
13210 if (NUMBERP (aggressive))
13211 {
13212 double float_amount = XFLOATINT (aggressive) * height;
13213 amount_to_scroll = float_amount;
13214 if (amount_to_scroll == 0 && float_amount > 0)
13215 amount_to_scroll = 1;
13216 }
13217 }
13218
13219 if (amount_to_scroll <= 0)
13220 return SCROLLING_FAILED;
13221
13222 move_it_vertically_backward (&it, amount_to_scroll);
13223 startp = it.current.pos;
13224 }
13225 }
13226
13227 /* Run window scroll functions. */
13228 startp = run_window_scroll_functions (window, startp);
13229
13230 /* Display the window. Give up if new fonts are loaded, or if point
13231 doesn't appear. */
13232 if (!try_window (window, startp, 0))
13233 rc = SCROLLING_NEED_LARGER_MATRICES;
13234 else if (w->cursor.vpos < 0)
13235 {
13236 clear_glyph_matrix (w->desired_matrix);
13237 rc = SCROLLING_FAILED;
13238 }
13239 else
13240 {
13241 /* Maybe forget recorded base line for line number display. */
13242 if (!just_this_one_p
13243 || current_buffer->clip_changed
13244 || BEG_UNCHANGED < CHARPOS (startp))
13245 w->base_line_number = Qnil;
13246
13247 /* If cursor ends up on a partially visible line,
13248 treat that as being off the bottom of the screen. */
13249 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13250 {
13251 clear_glyph_matrix (w->desired_matrix);
13252 ++extra_scroll_margin_lines;
13253 goto too_near_end;
13254 }
13255 rc = SCROLLING_SUCCESS;
13256 }
13257
13258 return rc;
13259 }
13260
13261
13262 /* Compute a suitable window start for window W if display of W starts
13263 on a continuation line. Value is non-zero if a new window start
13264 was computed.
13265
13266 The new window start will be computed, based on W's width, starting
13267 from the start of the continued line. It is the start of the
13268 screen line with the minimum distance from the old start W->start. */
13269
13270 static int
13271 compute_window_start_on_continuation_line (w)
13272 struct window *w;
13273 {
13274 struct text_pos pos, start_pos;
13275 int window_start_changed_p = 0;
13276
13277 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13278
13279 /* If window start is on a continuation line... Window start may be
13280 < BEGV in case there's invisible text at the start of the
13281 buffer (M-x rmail, for example). */
13282 if (CHARPOS (start_pos) > BEGV
13283 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13284 {
13285 struct it it;
13286 struct glyph_row *row;
13287
13288 /* Handle the case that the window start is out of range. */
13289 if (CHARPOS (start_pos) < BEGV)
13290 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13291 else if (CHARPOS (start_pos) > ZV)
13292 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13293
13294 /* Find the start of the continued line. This should be fast
13295 because scan_buffer is fast (newline cache). */
13296 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13297 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13298 row, DEFAULT_FACE_ID);
13299 reseat_at_previous_visible_line_start (&it);
13300
13301 /* If the line start is "too far" away from the window start,
13302 say it takes too much time to compute a new window start. */
13303 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13304 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13305 {
13306 int min_distance, distance;
13307
13308 /* Move forward by display lines to find the new window
13309 start. If window width was enlarged, the new start can
13310 be expected to be > the old start. If window width was
13311 decreased, the new window start will be < the old start.
13312 So, we're looking for the display line start with the
13313 minimum distance from the old window start. */
13314 pos = it.current.pos;
13315 min_distance = INFINITY;
13316 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13317 distance < min_distance)
13318 {
13319 min_distance = distance;
13320 pos = it.current.pos;
13321 move_it_by_lines (&it, 1, 0);
13322 }
13323
13324 /* Set the window start there. */
13325 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13326 window_start_changed_p = 1;
13327 }
13328 }
13329
13330 return window_start_changed_p;
13331 }
13332
13333
13334 /* Try cursor movement in case text has not changed in window WINDOW,
13335 with window start STARTP. Value is
13336
13337 CURSOR_MOVEMENT_SUCCESS if successful
13338
13339 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13340
13341 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13342 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13343 we want to scroll as if scroll-step were set to 1. See the code.
13344
13345 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13346 which case we have to abort this redisplay, and adjust matrices
13347 first. */
13348
13349 enum
13350 {
13351 CURSOR_MOVEMENT_SUCCESS,
13352 CURSOR_MOVEMENT_CANNOT_BE_USED,
13353 CURSOR_MOVEMENT_MUST_SCROLL,
13354 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13355 };
13356
13357 static int
13358 try_cursor_movement (window, startp, scroll_step)
13359 Lisp_Object window;
13360 struct text_pos startp;
13361 int *scroll_step;
13362 {
13363 struct window *w = XWINDOW (window);
13364 struct frame *f = XFRAME (w->frame);
13365 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13366
13367 #if GLYPH_DEBUG
13368 if (inhibit_try_cursor_movement)
13369 return rc;
13370 #endif
13371
13372 /* Handle case where text has not changed, only point, and it has
13373 not moved off the frame. */
13374 if (/* Point may be in this window. */
13375 PT >= CHARPOS (startp)
13376 /* Selective display hasn't changed. */
13377 && !current_buffer->clip_changed
13378 /* Function force-mode-line-update is used to force a thorough
13379 redisplay. It sets either windows_or_buffers_changed or
13380 update_mode_lines. So don't take a shortcut here for these
13381 cases. */
13382 && !update_mode_lines
13383 && !windows_or_buffers_changed
13384 && !cursor_type_changed
13385 /* Can't use this case if highlighting a region. When a
13386 region exists, cursor movement has to do more than just
13387 set the cursor. */
13388 && !(!NILP (Vtransient_mark_mode)
13389 && !NILP (current_buffer->mark_active))
13390 && NILP (w->region_showing)
13391 && NILP (Vshow_trailing_whitespace)
13392 /* Right after splitting windows, last_point may be nil. */
13393 && INTEGERP (w->last_point)
13394 /* This code is not used for mini-buffer for the sake of the case
13395 of redisplaying to replace an echo area message; since in
13396 that case the mini-buffer contents per se are usually
13397 unchanged. This code is of no real use in the mini-buffer
13398 since the handling of this_line_start_pos, etc., in redisplay
13399 handles the same cases. */
13400 && !EQ (window, minibuf_window)
13401 /* When splitting windows or for new windows, it happens that
13402 redisplay is called with a nil window_end_vpos or one being
13403 larger than the window. This should really be fixed in
13404 window.c. I don't have this on my list, now, so we do
13405 approximately the same as the old redisplay code. --gerd. */
13406 && INTEGERP (w->window_end_vpos)
13407 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13408 && (FRAME_WINDOW_P (f)
13409 || !overlay_arrow_in_current_buffer_p ()))
13410 {
13411 int this_scroll_margin, top_scroll_margin;
13412 struct glyph_row *row = NULL;
13413
13414 #if GLYPH_DEBUG
13415 debug_method_add (w, "cursor movement");
13416 #endif
13417
13418 /* Scroll if point within this distance from the top or bottom
13419 of the window. This is a pixel value. */
13420 if (scroll_margin > 0)
13421 {
13422 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13423 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13424 }
13425 else
13426 this_scroll_margin = 0;
13427
13428 top_scroll_margin = this_scroll_margin;
13429 if (WINDOW_WANTS_HEADER_LINE_P (w))
13430 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13431
13432 /* Start with the row the cursor was displayed during the last
13433 not paused redisplay. Give up if that row is not valid. */
13434 if (w->last_cursor.vpos < 0
13435 || w->last_cursor.vpos >= w->current_matrix->nrows)
13436 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13437 else
13438 {
13439 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13440 if (row->mode_line_p)
13441 ++row;
13442 if (!row->enabled_p)
13443 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13444 }
13445
13446 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13447 {
13448 int scroll_p = 0;
13449 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13450
13451 if (PT > XFASTINT (w->last_point))
13452 {
13453 /* Point has moved forward. */
13454 while (MATRIX_ROW_END_CHARPOS (row) < PT
13455 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13456 {
13457 xassert (row->enabled_p);
13458 ++row;
13459 }
13460
13461 /* The end position of a row equals the start position
13462 of the next row. If PT is there, we would rather
13463 display it in the next line. */
13464 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13465 && MATRIX_ROW_END_CHARPOS (row) == PT
13466 && !cursor_row_p (w, row))
13467 ++row;
13468
13469 /* If within the scroll margin, scroll. Note that
13470 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13471 the next line would be drawn, and that
13472 this_scroll_margin can be zero. */
13473 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13474 || PT > MATRIX_ROW_END_CHARPOS (row)
13475 /* Line is completely visible last line in window
13476 and PT is to be set in the next line. */
13477 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13478 && PT == MATRIX_ROW_END_CHARPOS (row)
13479 && !row->ends_at_zv_p
13480 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13481 scroll_p = 1;
13482 }
13483 else if (PT < XFASTINT (w->last_point))
13484 {
13485 /* Cursor has to be moved backward. Note that PT >=
13486 CHARPOS (startp) because of the outer if-statement. */
13487 while (!row->mode_line_p
13488 && (MATRIX_ROW_START_CHARPOS (row) > PT
13489 || (MATRIX_ROW_START_CHARPOS (row) == PT
13490 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13491 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13492 row > w->current_matrix->rows
13493 && (row-1)->ends_in_newline_from_string_p))))
13494 && (row->y > top_scroll_margin
13495 || CHARPOS (startp) == BEGV))
13496 {
13497 xassert (row->enabled_p);
13498 --row;
13499 }
13500
13501 /* Consider the following case: Window starts at BEGV,
13502 there is invisible, intangible text at BEGV, so that
13503 display starts at some point START > BEGV. It can
13504 happen that we are called with PT somewhere between
13505 BEGV and START. Try to handle that case. */
13506 if (row < w->current_matrix->rows
13507 || row->mode_line_p)
13508 {
13509 row = w->current_matrix->rows;
13510 if (row->mode_line_p)
13511 ++row;
13512 }
13513
13514 /* Due to newlines in overlay strings, we may have to
13515 skip forward over overlay strings. */
13516 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13517 && MATRIX_ROW_END_CHARPOS (row) == PT
13518 && !cursor_row_p (w, row))
13519 ++row;
13520
13521 /* If within the scroll margin, scroll. */
13522 if (row->y < top_scroll_margin
13523 && CHARPOS (startp) != BEGV)
13524 scroll_p = 1;
13525 }
13526 else
13527 {
13528 /* Cursor did not move. So don't scroll even if cursor line
13529 is partially visible, as it was so before. */
13530 rc = CURSOR_MOVEMENT_SUCCESS;
13531 }
13532
13533 if (PT < MATRIX_ROW_START_CHARPOS (row)
13534 || PT > MATRIX_ROW_END_CHARPOS (row))
13535 {
13536 /* if PT is not in the glyph row, give up. */
13537 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13538 }
13539 else if (rc != CURSOR_MOVEMENT_SUCCESS
13540 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13541 && make_cursor_line_fully_visible_p)
13542 {
13543 if (PT == MATRIX_ROW_END_CHARPOS (row)
13544 && !row->ends_at_zv_p
13545 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13546 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13547 else if (row->height > window_box_height (w))
13548 {
13549 /* If we end up in a partially visible line, let's
13550 make it fully visible, except when it's taller
13551 than the window, in which case we can't do much
13552 about it. */
13553 *scroll_step = 1;
13554 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13555 }
13556 else
13557 {
13558 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13559 if (!cursor_row_fully_visible_p (w, 0, 1))
13560 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13561 else
13562 rc = CURSOR_MOVEMENT_SUCCESS;
13563 }
13564 }
13565 else if (scroll_p)
13566 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13567 else
13568 {
13569 do
13570 {
13571 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13572 {
13573 rc = CURSOR_MOVEMENT_SUCCESS;
13574 break;
13575 }
13576 ++row;
13577 }
13578 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13579 && MATRIX_ROW_START_CHARPOS (row) == PT
13580 && cursor_row_p (w, row));
13581 }
13582 }
13583 }
13584
13585 return rc;
13586 }
13587
13588 void
13589 set_vertical_scroll_bar (w)
13590 struct window *w;
13591 {
13592 int start, end, whole;
13593
13594 /* Calculate the start and end positions for the current window.
13595 At some point, it would be nice to choose between scrollbars
13596 which reflect the whole buffer size, with special markers
13597 indicating narrowing, and scrollbars which reflect only the
13598 visible region.
13599
13600 Note that mini-buffers sometimes aren't displaying any text. */
13601 if (!MINI_WINDOW_P (w)
13602 || (w == XWINDOW (minibuf_window)
13603 && NILP (echo_area_buffer[0])))
13604 {
13605 struct buffer *buf = XBUFFER (w->buffer);
13606 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13607 start = marker_position (w->start) - BUF_BEGV (buf);
13608 /* I don't think this is guaranteed to be right. For the
13609 moment, we'll pretend it is. */
13610 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13611
13612 if (end < start)
13613 end = start;
13614 if (whole < (end - start))
13615 whole = end - start;
13616 }
13617 else
13618 start = end = whole = 0;
13619
13620 /* Indicate what this scroll bar ought to be displaying now. */
13621 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13622 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13623 (w, end - start, whole, start);
13624 }
13625
13626
13627 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13628 selected_window is redisplayed.
13629
13630 We can return without actually redisplaying the window if
13631 fonts_changed_p is nonzero. In that case, redisplay_internal will
13632 retry. */
13633
13634 static void
13635 redisplay_window (window, just_this_one_p)
13636 Lisp_Object window;
13637 int just_this_one_p;
13638 {
13639 struct window *w = XWINDOW (window);
13640 struct frame *f = XFRAME (w->frame);
13641 struct buffer *buffer = XBUFFER (w->buffer);
13642 struct buffer *old = current_buffer;
13643 struct text_pos lpoint, opoint, startp;
13644 int update_mode_line;
13645 int tem;
13646 struct it it;
13647 /* Record it now because it's overwritten. */
13648 int current_matrix_up_to_date_p = 0;
13649 int used_current_matrix_p = 0;
13650 /* This is less strict than current_matrix_up_to_date_p.
13651 It indictes that the buffer contents and narrowing are unchanged. */
13652 int buffer_unchanged_p = 0;
13653 int temp_scroll_step = 0;
13654 int count = SPECPDL_INDEX ();
13655 int rc;
13656 int centering_position = -1;
13657 int last_line_misfit = 0;
13658 int beg_unchanged, end_unchanged;
13659
13660 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13661 opoint = lpoint;
13662
13663 /* W must be a leaf window here. */
13664 xassert (!NILP (w->buffer));
13665 #if GLYPH_DEBUG
13666 *w->desired_matrix->method = 0;
13667 #endif
13668
13669 restart:
13670 reconsider_clip_changes (w, buffer);
13671
13672 /* Has the mode line to be updated? */
13673 update_mode_line = (!NILP (w->update_mode_line)
13674 || update_mode_lines
13675 || buffer->clip_changed
13676 || buffer->prevent_redisplay_optimizations_p);
13677
13678 if (MINI_WINDOW_P (w))
13679 {
13680 if (w == XWINDOW (echo_area_window)
13681 && !NILP (echo_area_buffer[0]))
13682 {
13683 if (update_mode_line)
13684 /* We may have to update a tty frame's menu bar or a
13685 tool-bar. Example `M-x C-h C-h C-g'. */
13686 goto finish_menu_bars;
13687 else
13688 /* We've already displayed the echo area glyphs in this window. */
13689 goto finish_scroll_bars;
13690 }
13691 else if ((w != XWINDOW (minibuf_window)
13692 || minibuf_level == 0)
13693 /* When buffer is nonempty, redisplay window normally. */
13694 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13695 /* Quail displays non-mini buffers in minibuffer window.
13696 In that case, redisplay the window normally. */
13697 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13698 {
13699 /* W is a mini-buffer window, but it's not active, so clear
13700 it. */
13701 int yb = window_text_bottom_y (w);
13702 struct glyph_row *row;
13703 int y;
13704
13705 for (y = 0, row = w->desired_matrix->rows;
13706 y < yb;
13707 y += row->height, ++row)
13708 blank_row (w, row, y);
13709 goto finish_scroll_bars;
13710 }
13711
13712 clear_glyph_matrix (w->desired_matrix);
13713 }
13714
13715 /* Otherwise set up data on this window; select its buffer and point
13716 value. */
13717 /* Really select the buffer, for the sake of buffer-local
13718 variables. */
13719 set_buffer_internal_1 (XBUFFER (w->buffer));
13720
13721 current_matrix_up_to_date_p
13722 = (!NILP (w->window_end_valid)
13723 && !current_buffer->clip_changed
13724 && !current_buffer->prevent_redisplay_optimizations_p
13725 && XFASTINT (w->last_modified) >= MODIFF
13726 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13727
13728 /* Run the window-bottom-change-functions
13729 if it is possible that the text on the screen has changed
13730 (either due to modification of the text, or any other reason). */
13731 if (!current_matrix_up_to_date_p
13732 && !NILP (Vwindow_text_change_functions))
13733 {
13734 safe_run_hooks (Qwindow_text_change_functions);
13735 goto restart;
13736 }
13737
13738 beg_unchanged = BEG_UNCHANGED;
13739 end_unchanged = END_UNCHANGED;
13740
13741 SET_TEXT_POS (opoint, PT, PT_BYTE);
13742
13743 specbind (Qinhibit_point_motion_hooks, Qt);
13744
13745 buffer_unchanged_p
13746 = (!NILP (w->window_end_valid)
13747 && !current_buffer->clip_changed
13748 && XFASTINT (w->last_modified) >= MODIFF
13749 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13750
13751 /* When windows_or_buffers_changed is non-zero, we can't rely on
13752 the window end being valid, so set it to nil there. */
13753 if (windows_or_buffers_changed)
13754 {
13755 /* If window starts on a continuation line, maybe adjust the
13756 window start in case the window's width changed. */
13757 if (XMARKER (w->start)->buffer == current_buffer)
13758 compute_window_start_on_continuation_line (w);
13759
13760 w->window_end_valid = Qnil;
13761 }
13762
13763 /* Some sanity checks. */
13764 CHECK_WINDOW_END (w);
13765 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13766 abort ();
13767 if (BYTEPOS (opoint) < CHARPOS (opoint))
13768 abort ();
13769
13770 /* If %c is in mode line, update it if needed. */
13771 if (!NILP (w->column_number_displayed)
13772 /* This alternative quickly identifies a common case
13773 where no change is needed. */
13774 && !(PT == XFASTINT (w->last_point)
13775 && XFASTINT (w->last_modified) >= MODIFF
13776 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13777 && (XFASTINT (w->column_number_displayed)
13778 != (int) current_column ())) /* iftc */
13779 update_mode_line = 1;
13780
13781 /* Count number of windows showing the selected buffer. An indirect
13782 buffer counts as its base buffer. */
13783 if (!just_this_one_p)
13784 {
13785 struct buffer *current_base, *window_base;
13786 current_base = current_buffer;
13787 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13788 if (current_base->base_buffer)
13789 current_base = current_base->base_buffer;
13790 if (window_base->base_buffer)
13791 window_base = window_base->base_buffer;
13792 if (current_base == window_base)
13793 buffer_shared++;
13794 }
13795
13796 /* Point refers normally to the selected window. For any other
13797 window, set up appropriate value. */
13798 if (!EQ (window, selected_window))
13799 {
13800 int new_pt = XMARKER (w->pointm)->charpos;
13801 int new_pt_byte = marker_byte_position (w->pointm);
13802 if (new_pt < BEGV)
13803 {
13804 new_pt = BEGV;
13805 new_pt_byte = BEGV_BYTE;
13806 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13807 }
13808 else if (new_pt > (ZV - 1))
13809 {
13810 new_pt = ZV;
13811 new_pt_byte = ZV_BYTE;
13812 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13813 }
13814
13815 /* We don't use SET_PT so that the point-motion hooks don't run. */
13816 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13817 }
13818
13819 /* If any of the character widths specified in the display table
13820 have changed, invalidate the width run cache. It's true that
13821 this may be a bit late to catch such changes, but the rest of
13822 redisplay goes (non-fatally) haywire when the display table is
13823 changed, so why should we worry about doing any better? */
13824 if (current_buffer->width_run_cache)
13825 {
13826 struct Lisp_Char_Table *disptab = buffer_display_table ();
13827
13828 if (! disptab_matches_widthtab (disptab,
13829 XVECTOR (current_buffer->width_table)))
13830 {
13831 invalidate_region_cache (current_buffer,
13832 current_buffer->width_run_cache,
13833 BEG, Z);
13834 recompute_width_table (current_buffer, disptab);
13835 }
13836 }
13837
13838 /* If window-start is screwed up, choose a new one. */
13839 if (XMARKER (w->start)->buffer != current_buffer)
13840 goto recenter;
13841
13842 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13843
13844 /* If someone specified a new starting point but did not insist,
13845 check whether it can be used. */
13846 if (!NILP (w->optional_new_start)
13847 && CHARPOS (startp) >= BEGV
13848 && CHARPOS (startp) <= ZV)
13849 {
13850 w->optional_new_start = Qnil;
13851 start_display (&it, w, startp);
13852 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13853 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13854 if (IT_CHARPOS (it) == PT)
13855 w->force_start = Qt;
13856 /* IT may overshoot PT if text at PT is invisible. */
13857 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13858 w->force_start = Qt;
13859 }
13860
13861 force_start:
13862
13863 /* Handle case where place to start displaying has been specified,
13864 unless the specified location is outside the accessible range. */
13865 if (!NILP (w->force_start)
13866 || w->frozen_window_start_p)
13867 {
13868 /* We set this later on if we have to adjust point. */
13869 int new_vpos = -1;
13870
13871 w->force_start = Qnil;
13872 w->vscroll = 0;
13873 w->window_end_valid = Qnil;
13874
13875 /* Forget any recorded base line for line number display. */
13876 if (!buffer_unchanged_p)
13877 w->base_line_number = Qnil;
13878
13879 /* Redisplay the mode line. Select the buffer properly for that.
13880 Also, run the hook window-scroll-functions
13881 because we have scrolled. */
13882 /* Note, we do this after clearing force_start because
13883 if there's an error, it is better to forget about force_start
13884 than to get into an infinite loop calling the hook functions
13885 and having them get more errors. */
13886 if (!update_mode_line
13887 || ! NILP (Vwindow_scroll_functions))
13888 {
13889 update_mode_line = 1;
13890 w->update_mode_line = Qt;
13891 startp = run_window_scroll_functions (window, startp);
13892 }
13893
13894 w->last_modified = make_number (0);
13895 w->last_overlay_modified = make_number (0);
13896 if (CHARPOS (startp) < BEGV)
13897 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13898 else if (CHARPOS (startp) > ZV)
13899 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13900
13901 /* Redisplay, then check if cursor has been set during the
13902 redisplay. Give up if new fonts were loaded. */
13903 /* We used to issue a CHECK_MARGINS argument to try_window here,
13904 but this causes scrolling to fail when point begins inside
13905 the scroll margin (bug#148) -- cyd */
13906 if (!try_window (window, startp, 0))
13907 {
13908 w->force_start = Qt;
13909 clear_glyph_matrix (w->desired_matrix);
13910 goto need_larger_matrices;
13911 }
13912
13913 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13914 {
13915 /* If point does not appear, try to move point so it does
13916 appear. The desired matrix has been built above, so we
13917 can use it here. */
13918 new_vpos = window_box_height (w) / 2;
13919 }
13920
13921 if (!cursor_row_fully_visible_p (w, 0, 0))
13922 {
13923 /* Point does appear, but on a line partly visible at end of window.
13924 Move it back to a fully-visible line. */
13925 new_vpos = window_box_height (w);
13926 }
13927
13928 /* If we need to move point for either of the above reasons,
13929 now actually do it. */
13930 if (new_vpos >= 0)
13931 {
13932 struct glyph_row *row;
13933
13934 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13935 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13936 ++row;
13937
13938 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13939 MATRIX_ROW_START_BYTEPOS (row));
13940
13941 if (w != XWINDOW (selected_window))
13942 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13943 else if (current_buffer == old)
13944 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13945
13946 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13947
13948 /* If we are highlighting the region, then we just changed
13949 the region, so redisplay to show it. */
13950 if (!NILP (Vtransient_mark_mode)
13951 && !NILP (current_buffer->mark_active))
13952 {
13953 clear_glyph_matrix (w->desired_matrix);
13954 if (!try_window (window, startp, 0))
13955 goto need_larger_matrices;
13956 }
13957 }
13958
13959 #if GLYPH_DEBUG
13960 debug_method_add (w, "forced window start");
13961 #endif
13962 goto done;
13963 }
13964
13965 /* Handle case where text has not changed, only point, and it has
13966 not moved off the frame, and we are not retrying after hscroll.
13967 (current_matrix_up_to_date_p is nonzero when retrying.) */
13968 if (current_matrix_up_to_date_p
13969 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13970 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13971 {
13972 switch (rc)
13973 {
13974 case CURSOR_MOVEMENT_SUCCESS:
13975 used_current_matrix_p = 1;
13976 goto done;
13977
13978 case CURSOR_MOVEMENT_MUST_SCROLL:
13979 goto try_to_scroll;
13980
13981 default:
13982 abort ();
13983 }
13984 }
13985 /* If current starting point was originally the beginning of a line
13986 but no longer is, find a new starting point. */
13987 else if (!NILP (w->start_at_line_beg)
13988 && !(CHARPOS (startp) <= BEGV
13989 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13990 {
13991 #if GLYPH_DEBUG
13992 debug_method_add (w, "recenter 1");
13993 #endif
13994 goto recenter;
13995 }
13996
13997 /* Try scrolling with try_window_id. Value is > 0 if update has
13998 been done, it is -1 if we know that the same window start will
13999 not work. It is 0 if unsuccessful for some other reason. */
14000 else if ((tem = try_window_id (w)) != 0)
14001 {
14002 #if GLYPH_DEBUG
14003 debug_method_add (w, "try_window_id %d", tem);
14004 #endif
14005
14006 if (fonts_changed_p)
14007 goto need_larger_matrices;
14008 if (tem > 0)
14009 goto done;
14010
14011 /* Otherwise try_window_id has returned -1 which means that we
14012 don't want the alternative below this comment to execute. */
14013 }
14014 else if (CHARPOS (startp) >= BEGV
14015 && CHARPOS (startp) <= ZV
14016 && PT >= CHARPOS (startp)
14017 && (CHARPOS (startp) < ZV
14018 /* Avoid starting at end of buffer. */
14019 || CHARPOS (startp) == BEGV
14020 || (XFASTINT (w->last_modified) >= MODIFF
14021 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14022 {
14023
14024 /* If first window line is a continuation line, and window start
14025 is inside the modified region, but the first change is before
14026 current window start, we must select a new window start.
14027
14028 However, if this is the result of a down-mouse event (e.g. by
14029 extending the mouse-drag-overlay), we don't want to select a
14030 new window start, since that would change the position under
14031 the mouse, resulting in an unwanted mouse-movement rather
14032 than a simple mouse-click. */
14033 if (NILP (w->start_at_line_beg)
14034 && NILP (do_mouse_tracking)
14035 && CHARPOS (startp) > BEGV
14036 && CHARPOS (startp) > BEG + beg_unchanged
14037 && CHARPOS (startp) <= Z - end_unchanged
14038 /* Even if w->start_at_line_beg is nil, a new window may
14039 start at a line_beg, since that's how set_buffer_window
14040 sets it. So, we need to check the return value of
14041 compute_window_start_on_continuation_line. (See also
14042 bug#197). */
14043 && XMARKER (w->start)->buffer == current_buffer
14044 && compute_window_start_on_continuation_line (w))
14045 {
14046 w->force_start = Qt;
14047 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14048 goto force_start;
14049 }
14050
14051 #if GLYPH_DEBUG
14052 debug_method_add (w, "same window start");
14053 #endif
14054
14055 /* Try to redisplay starting at same place as before.
14056 If point has not moved off frame, accept the results. */
14057 if (!current_matrix_up_to_date_p
14058 /* Don't use try_window_reusing_current_matrix in this case
14059 because a window scroll function can have changed the
14060 buffer. */
14061 || !NILP (Vwindow_scroll_functions)
14062 || MINI_WINDOW_P (w)
14063 || !(used_current_matrix_p
14064 = try_window_reusing_current_matrix (w)))
14065 {
14066 IF_DEBUG (debug_method_add (w, "1"));
14067 if (try_window (window, startp, 1) < 0)
14068 /* -1 means we need to scroll.
14069 0 means we need new matrices, but fonts_changed_p
14070 is set in that case, so we will detect it below. */
14071 goto try_to_scroll;
14072 }
14073
14074 if (fonts_changed_p)
14075 goto need_larger_matrices;
14076
14077 if (w->cursor.vpos >= 0)
14078 {
14079 if (!just_this_one_p
14080 || current_buffer->clip_changed
14081 || BEG_UNCHANGED < CHARPOS (startp))
14082 /* Forget any recorded base line for line number display. */
14083 w->base_line_number = Qnil;
14084
14085 if (!cursor_row_fully_visible_p (w, 1, 0))
14086 {
14087 clear_glyph_matrix (w->desired_matrix);
14088 last_line_misfit = 1;
14089 }
14090 /* Drop through and scroll. */
14091 else
14092 goto done;
14093 }
14094 else
14095 clear_glyph_matrix (w->desired_matrix);
14096 }
14097
14098 try_to_scroll:
14099
14100 w->last_modified = make_number (0);
14101 w->last_overlay_modified = make_number (0);
14102
14103 /* Redisplay the mode line. Select the buffer properly for that. */
14104 if (!update_mode_line)
14105 {
14106 update_mode_line = 1;
14107 w->update_mode_line = Qt;
14108 }
14109
14110 /* Try to scroll by specified few lines. */
14111 if ((scroll_conservatively
14112 || scroll_step
14113 || temp_scroll_step
14114 || NUMBERP (current_buffer->scroll_up_aggressively)
14115 || NUMBERP (current_buffer->scroll_down_aggressively))
14116 && !current_buffer->clip_changed
14117 && CHARPOS (startp) >= BEGV
14118 && CHARPOS (startp) <= ZV)
14119 {
14120 /* The function returns -1 if new fonts were loaded, 1 if
14121 successful, 0 if not successful. */
14122 int rc = try_scrolling (window, just_this_one_p,
14123 scroll_conservatively,
14124 scroll_step,
14125 temp_scroll_step, last_line_misfit);
14126 switch (rc)
14127 {
14128 case SCROLLING_SUCCESS:
14129 goto done;
14130
14131 case SCROLLING_NEED_LARGER_MATRICES:
14132 goto need_larger_matrices;
14133
14134 case SCROLLING_FAILED:
14135 break;
14136
14137 default:
14138 abort ();
14139 }
14140 }
14141
14142 /* Finally, just choose place to start which centers point */
14143
14144 recenter:
14145 if (centering_position < 0)
14146 centering_position = window_box_height (w) / 2;
14147
14148 #if GLYPH_DEBUG
14149 debug_method_add (w, "recenter");
14150 #endif
14151
14152 /* w->vscroll = 0; */
14153
14154 /* Forget any previously recorded base line for line number display. */
14155 if (!buffer_unchanged_p)
14156 w->base_line_number = Qnil;
14157
14158 /* Move backward half the height of the window. */
14159 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14160 it.current_y = it.last_visible_y;
14161 move_it_vertically_backward (&it, centering_position);
14162 xassert (IT_CHARPOS (it) >= BEGV);
14163
14164 /* The function move_it_vertically_backward may move over more
14165 than the specified y-distance. If it->w is small, e.g. a
14166 mini-buffer window, we may end up in front of the window's
14167 display area. Start displaying at the start of the line
14168 containing PT in this case. */
14169 if (it.current_y <= 0)
14170 {
14171 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14172 move_it_vertically_backward (&it, 0);
14173 it.current_y = 0;
14174 }
14175
14176 it.current_x = it.hpos = 0;
14177
14178 /* Set startp here explicitly in case that helps avoid an infinite loop
14179 in case the window-scroll-functions functions get errors. */
14180 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14181
14182 /* Run scroll hooks. */
14183 startp = run_window_scroll_functions (window, it.current.pos);
14184
14185 /* Redisplay the window. */
14186 if (!current_matrix_up_to_date_p
14187 || windows_or_buffers_changed
14188 || cursor_type_changed
14189 /* Don't use try_window_reusing_current_matrix in this case
14190 because it can have changed the buffer. */
14191 || !NILP (Vwindow_scroll_functions)
14192 || !just_this_one_p
14193 || MINI_WINDOW_P (w)
14194 || !(used_current_matrix_p
14195 = try_window_reusing_current_matrix (w)))
14196 try_window (window, startp, 0);
14197
14198 /* If new fonts have been loaded (due to fontsets), give up. We
14199 have to start a new redisplay since we need to re-adjust glyph
14200 matrices. */
14201 if (fonts_changed_p)
14202 goto need_larger_matrices;
14203
14204 /* If cursor did not appear assume that the middle of the window is
14205 in the first line of the window. Do it again with the next line.
14206 (Imagine a window of height 100, displaying two lines of height
14207 60. Moving back 50 from it->last_visible_y will end in the first
14208 line.) */
14209 if (w->cursor.vpos < 0)
14210 {
14211 if (!NILP (w->window_end_valid)
14212 && PT >= Z - XFASTINT (w->window_end_pos))
14213 {
14214 clear_glyph_matrix (w->desired_matrix);
14215 move_it_by_lines (&it, 1, 0);
14216 try_window (window, it.current.pos, 0);
14217 }
14218 else if (PT < IT_CHARPOS (it))
14219 {
14220 clear_glyph_matrix (w->desired_matrix);
14221 move_it_by_lines (&it, -1, 0);
14222 try_window (window, it.current.pos, 0);
14223 }
14224 else
14225 {
14226 /* Not much we can do about it. */
14227 }
14228 }
14229
14230 /* Consider the following case: Window starts at BEGV, there is
14231 invisible, intangible text at BEGV, so that display starts at
14232 some point START > BEGV. It can happen that we are called with
14233 PT somewhere between BEGV and START. Try to handle that case. */
14234 if (w->cursor.vpos < 0)
14235 {
14236 struct glyph_row *row = w->current_matrix->rows;
14237 if (row->mode_line_p)
14238 ++row;
14239 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14240 }
14241
14242 if (!cursor_row_fully_visible_p (w, 0, 0))
14243 {
14244 /* If vscroll is enabled, disable it and try again. */
14245 if (w->vscroll)
14246 {
14247 w->vscroll = 0;
14248 clear_glyph_matrix (w->desired_matrix);
14249 goto recenter;
14250 }
14251
14252 /* If centering point failed to make the whole line visible,
14253 put point at the top instead. That has to make the whole line
14254 visible, if it can be done. */
14255 if (centering_position == 0)
14256 goto done;
14257
14258 clear_glyph_matrix (w->desired_matrix);
14259 centering_position = 0;
14260 goto recenter;
14261 }
14262
14263 done:
14264
14265 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14266 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14267 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14268 ? Qt : Qnil);
14269
14270 /* Display the mode line, if we must. */
14271 if ((update_mode_line
14272 /* If window not full width, must redo its mode line
14273 if (a) the window to its side is being redone and
14274 (b) we do a frame-based redisplay. This is a consequence
14275 of how inverted lines are drawn in frame-based redisplay. */
14276 || (!just_this_one_p
14277 && !FRAME_WINDOW_P (f)
14278 && !WINDOW_FULL_WIDTH_P (w))
14279 /* Line number to display. */
14280 || INTEGERP (w->base_line_pos)
14281 /* Column number is displayed and different from the one displayed. */
14282 || (!NILP (w->column_number_displayed)
14283 && (XFASTINT (w->column_number_displayed)
14284 != (int) current_column ()))) /* iftc */
14285 /* This means that the window has a mode line. */
14286 && (WINDOW_WANTS_MODELINE_P (w)
14287 || WINDOW_WANTS_HEADER_LINE_P (w)))
14288 {
14289 display_mode_lines (w);
14290
14291 /* If mode line height has changed, arrange for a thorough
14292 immediate redisplay using the correct mode line height. */
14293 if (WINDOW_WANTS_MODELINE_P (w)
14294 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14295 {
14296 fonts_changed_p = 1;
14297 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14298 = DESIRED_MODE_LINE_HEIGHT (w);
14299 }
14300
14301 /* If header line height has changed, arrange for a thorough
14302 immediate redisplay using the correct header line height. */
14303 if (WINDOW_WANTS_HEADER_LINE_P (w)
14304 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14305 {
14306 fonts_changed_p = 1;
14307 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14308 = DESIRED_HEADER_LINE_HEIGHT (w);
14309 }
14310
14311 if (fonts_changed_p)
14312 goto need_larger_matrices;
14313 }
14314
14315 if (!line_number_displayed
14316 && !BUFFERP (w->base_line_pos))
14317 {
14318 w->base_line_pos = Qnil;
14319 w->base_line_number = Qnil;
14320 }
14321
14322 finish_menu_bars:
14323
14324 /* When we reach a frame's selected window, redo the frame's menu bar. */
14325 if (update_mode_line
14326 && EQ (FRAME_SELECTED_WINDOW (f), window))
14327 {
14328 int redisplay_menu_p = 0;
14329 int redisplay_tool_bar_p = 0;
14330
14331 if (FRAME_WINDOW_P (f))
14332 {
14333 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14334 || defined (HAVE_NS) || defined (USE_GTK)
14335 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14336 #else
14337 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14338 #endif
14339 }
14340 else
14341 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14342
14343 if (redisplay_menu_p)
14344 display_menu_bar (w);
14345
14346 #ifdef HAVE_WINDOW_SYSTEM
14347 if (FRAME_WINDOW_P (f))
14348 {
14349 #if defined (USE_GTK) || defined (HAVE_NS)
14350 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14351 #else
14352 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14353 && (FRAME_TOOL_BAR_LINES (f) > 0
14354 || !NILP (Vauto_resize_tool_bars));
14355 #endif
14356
14357 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14358 {
14359 extern int ignore_mouse_drag_p;
14360 ignore_mouse_drag_p = 1;
14361 }
14362 }
14363 #endif
14364 }
14365
14366 #ifdef HAVE_WINDOW_SYSTEM
14367 if (FRAME_WINDOW_P (f)
14368 && update_window_fringes (w, (just_this_one_p
14369 || (!used_current_matrix_p && !overlay_arrow_seen)
14370 || w->pseudo_window_p)))
14371 {
14372 update_begin (f);
14373 BLOCK_INPUT;
14374 if (draw_window_fringes (w, 1))
14375 x_draw_vertical_border (w);
14376 UNBLOCK_INPUT;
14377 update_end (f);
14378 }
14379 #endif /* HAVE_WINDOW_SYSTEM */
14380
14381 /* We go to this label, with fonts_changed_p nonzero,
14382 if it is necessary to try again using larger glyph matrices.
14383 We have to redeem the scroll bar even in this case,
14384 because the loop in redisplay_internal expects that. */
14385 need_larger_matrices:
14386 ;
14387 finish_scroll_bars:
14388
14389 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14390 {
14391 /* Set the thumb's position and size. */
14392 set_vertical_scroll_bar (w);
14393
14394 /* Note that we actually used the scroll bar attached to this
14395 window, so it shouldn't be deleted at the end of redisplay. */
14396 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14397 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14398 }
14399
14400 /* Restore current_buffer and value of point in it. */
14401 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14402 set_buffer_internal_1 (old);
14403 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14404 shorter. This can be caused by log truncation in *Messages*. */
14405 if (CHARPOS (lpoint) <= ZV)
14406 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14407
14408 unbind_to (count, Qnil);
14409 }
14410
14411
14412 /* Build the complete desired matrix of WINDOW with a window start
14413 buffer position POS.
14414
14415 Value is 1 if successful. It is zero if fonts were loaded during
14416 redisplay which makes re-adjusting glyph matrices necessary, and -1
14417 if point would appear in the scroll margins.
14418 (We check that only if CHECK_MARGINS is nonzero. */
14419
14420 int
14421 try_window (window, pos, check_margins)
14422 Lisp_Object window;
14423 struct text_pos pos;
14424 int check_margins;
14425 {
14426 struct window *w = XWINDOW (window);
14427 struct it it;
14428 struct glyph_row *last_text_row = NULL;
14429 struct frame *f = XFRAME (w->frame);
14430
14431 /* Make POS the new window start. */
14432 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14433
14434 /* Mark cursor position as unknown. No overlay arrow seen. */
14435 w->cursor.vpos = -1;
14436 overlay_arrow_seen = 0;
14437
14438 /* Initialize iterator and info to start at POS. */
14439 start_display (&it, w, pos);
14440
14441 /* Display all lines of W. */
14442 while (it.current_y < it.last_visible_y)
14443 {
14444 if (display_line (&it))
14445 last_text_row = it.glyph_row - 1;
14446 if (fonts_changed_p)
14447 return 0;
14448 }
14449
14450 /* Don't let the cursor end in the scroll margins. */
14451 if (check_margins
14452 && !MINI_WINDOW_P (w))
14453 {
14454 int this_scroll_margin;
14455
14456 if (scroll_margin > 0)
14457 {
14458 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14459 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14460 }
14461 else
14462 this_scroll_margin = 0;
14463
14464 if ((w->cursor.y >= 0 /* not vscrolled */
14465 && w->cursor.y < this_scroll_margin
14466 && CHARPOS (pos) > BEGV
14467 && IT_CHARPOS (it) < ZV)
14468 /* rms: considering make_cursor_line_fully_visible_p here
14469 seems to give wrong results. We don't want to recenter
14470 when the last line is partly visible, we want to allow
14471 that case to be handled in the usual way. */
14472 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14473 {
14474 w->cursor.vpos = -1;
14475 clear_glyph_matrix (w->desired_matrix);
14476 return -1;
14477 }
14478 }
14479
14480 /* If bottom moved off end of frame, change mode line percentage. */
14481 if (XFASTINT (w->window_end_pos) <= 0
14482 && Z != IT_CHARPOS (it))
14483 w->update_mode_line = Qt;
14484
14485 /* Set window_end_pos to the offset of the last character displayed
14486 on the window from the end of current_buffer. Set
14487 window_end_vpos to its row number. */
14488 if (last_text_row)
14489 {
14490 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14491 w->window_end_bytepos
14492 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14493 w->window_end_pos
14494 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14495 w->window_end_vpos
14496 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14497 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14498 ->displays_text_p);
14499 }
14500 else
14501 {
14502 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14503 w->window_end_pos = make_number (Z - ZV);
14504 w->window_end_vpos = make_number (0);
14505 }
14506
14507 /* But that is not valid info until redisplay finishes. */
14508 w->window_end_valid = Qnil;
14509 return 1;
14510 }
14511
14512
14513 \f
14514 /************************************************************************
14515 Window redisplay reusing current matrix when buffer has not changed
14516 ************************************************************************/
14517
14518 /* Try redisplay of window W showing an unchanged buffer with a
14519 different window start than the last time it was displayed by
14520 reusing its current matrix. Value is non-zero if successful.
14521 W->start is the new window start. */
14522
14523 static int
14524 try_window_reusing_current_matrix (w)
14525 struct window *w;
14526 {
14527 struct frame *f = XFRAME (w->frame);
14528 struct glyph_row *row, *bottom_row;
14529 struct it it;
14530 struct run run;
14531 struct text_pos start, new_start;
14532 int nrows_scrolled, i;
14533 struct glyph_row *last_text_row;
14534 struct glyph_row *last_reused_text_row;
14535 struct glyph_row *start_row;
14536 int start_vpos, min_y, max_y;
14537
14538 #if GLYPH_DEBUG
14539 if (inhibit_try_window_reusing)
14540 return 0;
14541 #endif
14542
14543 if (/* This function doesn't handle terminal frames. */
14544 !FRAME_WINDOW_P (f)
14545 /* Don't try to reuse the display if windows have been split
14546 or such. */
14547 || windows_or_buffers_changed
14548 || cursor_type_changed)
14549 return 0;
14550
14551 /* Can't do this if region may have changed. */
14552 if ((!NILP (Vtransient_mark_mode)
14553 && !NILP (current_buffer->mark_active))
14554 || !NILP (w->region_showing)
14555 || !NILP (Vshow_trailing_whitespace))
14556 return 0;
14557
14558 /* If top-line visibility has changed, give up. */
14559 if (WINDOW_WANTS_HEADER_LINE_P (w)
14560 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14561 return 0;
14562
14563 /* Give up if old or new display is scrolled vertically. We could
14564 make this function handle this, but right now it doesn't. */
14565 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14566 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14567 return 0;
14568
14569 /* The variable new_start now holds the new window start. The old
14570 start `start' can be determined from the current matrix. */
14571 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14572 start = start_row->start.pos;
14573 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14574
14575 /* Clear the desired matrix for the display below. */
14576 clear_glyph_matrix (w->desired_matrix);
14577
14578 if (CHARPOS (new_start) <= CHARPOS (start))
14579 {
14580 int first_row_y;
14581
14582 /* Don't use this method if the display starts with an ellipsis
14583 displayed for invisible text. It's not easy to handle that case
14584 below, and it's certainly not worth the effort since this is
14585 not a frequent case. */
14586 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14587 return 0;
14588
14589 IF_DEBUG (debug_method_add (w, "twu1"));
14590
14591 /* Display up to a row that can be reused. The variable
14592 last_text_row is set to the last row displayed that displays
14593 text. Note that it.vpos == 0 if or if not there is a
14594 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14595 start_display (&it, w, new_start);
14596 first_row_y = it.current_y;
14597 w->cursor.vpos = -1;
14598 last_text_row = last_reused_text_row = NULL;
14599
14600 while (it.current_y < it.last_visible_y
14601 && !fonts_changed_p)
14602 {
14603 /* If we have reached into the characters in the START row,
14604 that means the line boundaries have changed. So we
14605 can't start copying with the row START. Maybe it will
14606 work to start copying with the following row. */
14607 while (IT_CHARPOS (it) > CHARPOS (start))
14608 {
14609 /* Advance to the next row as the "start". */
14610 start_row++;
14611 start = start_row->start.pos;
14612 /* If there are no more rows to try, or just one, give up. */
14613 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14614 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14615 || CHARPOS (start) == ZV)
14616 {
14617 clear_glyph_matrix (w->desired_matrix);
14618 return 0;
14619 }
14620
14621 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14622 }
14623 /* If we have reached alignment,
14624 we can copy the rest of the rows. */
14625 if (IT_CHARPOS (it) == CHARPOS (start))
14626 break;
14627
14628 if (display_line (&it))
14629 last_text_row = it.glyph_row - 1;
14630 }
14631
14632 /* A value of current_y < last_visible_y means that we stopped
14633 at the previous window start, which in turn means that we
14634 have at least one reusable row. */
14635 if (it.current_y < it.last_visible_y)
14636 {
14637 /* IT.vpos always starts from 0; it counts text lines. */
14638 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14639
14640 /* Find PT if not already found in the lines displayed. */
14641 if (w->cursor.vpos < 0)
14642 {
14643 int dy = it.current_y - start_row->y;
14644
14645 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14646 row = row_containing_pos (w, PT, row, NULL, dy);
14647 if (row)
14648 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14649 dy, nrows_scrolled);
14650 else
14651 {
14652 clear_glyph_matrix (w->desired_matrix);
14653 return 0;
14654 }
14655 }
14656
14657 /* Scroll the display. Do it before the current matrix is
14658 changed. The problem here is that update has not yet
14659 run, i.e. part of the current matrix is not up to date.
14660 scroll_run_hook will clear the cursor, and use the
14661 current matrix to get the height of the row the cursor is
14662 in. */
14663 run.current_y = start_row->y;
14664 run.desired_y = it.current_y;
14665 run.height = it.last_visible_y - it.current_y;
14666
14667 if (run.height > 0 && run.current_y != run.desired_y)
14668 {
14669 update_begin (f);
14670 FRAME_RIF (f)->update_window_begin_hook (w);
14671 FRAME_RIF (f)->clear_window_mouse_face (w);
14672 FRAME_RIF (f)->scroll_run_hook (w, &run);
14673 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14674 update_end (f);
14675 }
14676
14677 /* Shift current matrix down by nrows_scrolled lines. */
14678 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14679 rotate_matrix (w->current_matrix,
14680 start_vpos,
14681 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14682 nrows_scrolled);
14683
14684 /* Disable lines that must be updated. */
14685 for (i = 0; i < nrows_scrolled; ++i)
14686 (start_row + i)->enabled_p = 0;
14687
14688 /* Re-compute Y positions. */
14689 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14690 max_y = it.last_visible_y;
14691 for (row = start_row + nrows_scrolled;
14692 row < bottom_row;
14693 ++row)
14694 {
14695 row->y = it.current_y;
14696 row->visible_height = row->height;
14697
14698 if (row->y < min_y)
14699 row->visible_height -= min_y - row->y;
14700 if (row->y + row->height > max_y)
14701 row->visible_height -= row->y + row->height - max_y;
14702 row->redraw_fringe_bitmaps_p = 1;
14703
14704 it.current_y += row->height;
14705
14706 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14707 last_reused_text_row = row;
14708 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14709 break;
14710 }
14711
14712 /* Disable lines in the current matrix which are now
14713 below the window. */
14714 for (++row; row < bottom_row; ++row)
14715 row->enabled_p = row->mode_line_p = 0;
14716 }
14717
14718 /* Update window_end_pos etc.; last_reused_text_row is the last
14719 reused row from the current matrix containing text, if any.
14720 The value of last_text_row is the last displayed line
14721 containing text. */
14722 if (last_reused_text_row)
14723 {
14724 w->window_end_bytepos
14725 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14726 w->window_end_pos
14727 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14728 w->window_end_vpos
14729 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14730 w->current_matrix));
14731 }
14732 else if (last_text_row)
14733 {
14734 w->window_end_bytepos
14735 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14736 w->window_end_pos
14737 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14738 w->window_end_vpos
14739 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14740 }
14741 else
14742 {
14743 /* This window must be completely empty. */
14744 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14745 w->window_end_pos = make_number (Z - ZV);
14746 w->window_end_vpos = make_number (0);
14747 }
14748 w->window_end_valid = Qnil;
14749
14750 /* Update hint: don't try scrolling again in update_window. */
14751 w->desired_matrix->no_scrolling_p = 1;
14752
14753 #if GLYPH_DEBUG
14754 debug_method_add (w, "try_window_reusing_current_matrix 1");
14755 #endif
14756 return 1;
14757 }
14758 else if (CHARPOS (new_start) > CHARPOS (start))
14759 {
14760 struct glyph_row *pt_row, *row;
14761 struct glyph_row *first_reusable_row;
14762 struct glyph_row *first_row_to_display;
14763 int dy;
14764 int yb = window_text_bottom_y (w);
14765
14766 /* Find the row starting at new_start, if there is one. Don't
14767 reuse a partially visible line at the end. */
14768 first_reusable_row = start_row;
14769 while (first_reusable_row->enabled_p
14770 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14771 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14772 < CHARPOS (new_start)))
14773 ++first_reusable_row;
14774
14775 /* Give up if there is no row to reuse. */
14776 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14777 || !first_reusable_row->enabled_p
14778 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14779 != CHARPOS (new_start)))
14780 return 0;
14781
14782 /* We can reuse fully visible rows beginning with
14783 first_reusable_row to the end of the window. Set
14784 first_row_to_display to the first row that cannot be reused.
14785 Set pt_row to the row containing point, if there is any. */
14786 pt_row = NULL;
14787 for (first_row_to_display = first_reusable_row;
14788 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14789 ++first_row_to_display)
14790 {
14791 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14792 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14793 pt_row = first_row_to_display;
14794 }
14795
14796 /* Start displaying at the start of first_row_to_display. */
14797 xassert (first_row_to_display->y < yb);
14798 init_to_row_start (&it, w, first_row_to_display);
14799
14800 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14801 - start_vpos);
14802 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14803 - nrows_scrolled);
14804 it.current_y = (first_row_to_display->y - first_reusable_row->y
14805 + WINDOW_HEADER_LINE_HEIGHT (w));
14806
14807 /* Display lines beginning with first_row_to_display in the
14808 desired matrix. Set last_text_row to the last row displayed
14809 that displays text. */
14810 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14811 if (pt_row == NULL)
14812 w->cursor.vpos = -1;
14813 last_text_row = NULL;
14814 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14815 if (display_line (&it))
14816 last_text_row = it.glyph_row - 1;
14817
14818 /* If point is in a reused row, adjust y and vpos of the cursor
14819 position. */
14820 if (pt_row)
14821 {
14822 w->cursor.vpos -= nrows_scrolled;
14823 w->cursor.y -= first_reusable_row->y - start_row->y;
14824 }
14825
14826 /* Give up if point isn't in a row displayed or reused. (This
14827 also handles the case where w->cursor.vpos < nrows_scrolled
14828 after the calls to display_line, which can happen with scroll
14829 margins. See bug#1295.) */
14830 if (w->cursor.vpos < 0)
14831 {
14832 clear_glyph_matrix (w->desired_matrix);
14833 return 0;
14834 }
14835
14836 /* Scroll the display. */
14837 run.current_y = first_reusable_row->y;
14838 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14839 run.height = it.last_visible_y - run.current_y;
14840 dy = run.current_y - run.desired_y;
14841
14842 if (run.height)
14843 {
14844 update_begin (f);
14845 FRAME_RIF (f)->update_window_begin_hook (w);
14846 FRAME_RIF (f)->clear_window_mouse_face (w);
14847 FRAME_RIF (f)->scroll_run_hook (w, &run);
14848 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14849 update_end (f);
14850 }
14851
14852 /* Adjust Y positions of reused rows. */
14853 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14854 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14855 max_y = it.last_visible_y;
14856 for (row = first_reusable_row; row < first_row_to_display; ++row)
14857 {
14858 row->y -= dy;
14859 row->visible_height = row->height;
14860 if (row->y < min_y)
14861 row->visible_height -= min_y - row->y;
14862 if (row->y + row->height > max_y)
14863 row->visible_height -= row->y + row->height - max_y;
14864 row->redraw_fringe_bitmaps_p = 1;
14865 }
14866
14867 /* Scroll the current matrix. */
14868 xassert (nrows_scrolled > 0);
14869 rotate_matrix (w->current_matrix,
14870 start_vpos,
14871 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14872 -nrows_scrolled);
14873
14874 /* Disable rows not reused. */
14875 for (row -= nrows_scrolled; row < bottom_row; ++row)
14876 row->enabled_p = 0;
14877
14878 /* Point may have moved to a different line, so we cannot assume that
14879 the previous cursor position is valid; locate the correct row. */
14880 if (pt_row)
14881 {
14882 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14883 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14884 row++)
14885 {
14886 w->cursor.vpos++;
14887 w->cursor.y = row->y;
14888 }
14889 if (row < bottom_row)
14890 {
14891 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14892 struct glyph *end = glyph + row->used[TEXT_AREA];
14893 struct glyph *orig_glyph = glyph;
14894 struct cursor_pos orig_cursor = w->cursor;
14895
14896 for (; glyph < end
14897 && (!BUFFERP (glyph->object)
14898 || glyph->charpos != PT);
14899 glyph++)
14900 {
14901 w->cursor.hpos++;
14902 w->cursor.x += glyph->pixel_width;
14903 }
14904 /* With bidi reordering, charpos changes non-linearly
14905 with hpos, so the right glyph could be to the
14906 left. */
14907 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
14908 && (!BUFFERP (glyph->object) || glyph->charpos != PT))
14909 {
14910 struct glyph *start_glyph = row->glyphs[TEXT_AREA];
14911
14912 glyph = orig_glyph - 1;
14913 orig_cursor.hpos--;
14914 orig_cursor.x -= glyph->pixel_width;
14915 for (; glyph >= start_glyph
14916 && (!BUFFERP (glyph->object)
14917 || glyph->charpos != PT);
14918 glyph--)
14919 {
14920 w->cursor.hpos--;
14921 w->cursor.x -= glyph->pixel_width;
14922 }
14923 if (BUFFERP (glyph->object) && glyph->charpos == PT)
14924 w->cursor = orig_cursor;
14925 }
14926 }
14927 }
14928
14929 /* Adjust window end. A null value of last_text_row means that
14930 the window end is in reused rows which in turn means that
14931 only its vpos can have changed. */
14932 if (last_text_row)
14933 {
14934 w->window_end_bytepos
14935 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14936 w->window_end_pos
14937 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14938 w->window_end_vpos
14939 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14940 }
14941 else
14942 {
14943 w->window_end_vpos
14944 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14945 }
14946
14947 w->window_end_valid = Qnil;
14948 w->desired_matrix->no_scrolling_p = 1;
14949
14950 #if GLYPH_DEBUG
14951 debug_method_add (w, "try_window_reusing_current_matrix 2");
14952 #endif
14953 return 1;
14954 }
14955
14956 return 0;
14957 }
14958
14959
14960 \f
14961 /************************************************************************
14962 Window redisplay reusing current matrix when buffer has changed
14963 ************************************************************************/
14964
14965 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14966 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14967 int *, int *));
14968 static struct glyph_row *
14969 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14970 struct glyph_row *));
14971
14972
14973 /* Return the last row in MATRIX displaying text. If row START is
14974 non-null, start searching with that row. IT gives the dimensions
14975 of the display. Value is null if matrix is empty; otherwise it is
14976 a pointer to the row found. */
14977
14978 static struct glyph_row *
14979 find_last_row_displaying_text (matrix, it, start)
14980 struct glyph_matrix *matrix;
14981 struct it *it;
14982 struct glyph_row *start;
14983 {
14984 struct glyph_row *row, *row_found;
14985
14986 /* Set row_found to the last row in IT->w's current matrix
14987 displaying text. The loop looks funny but think of partially
14988 visible lines. */
14989 row_found = NULL;
14990 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14991 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14992 {
14993 xassert (row->enabled_p);
14994 row_found = row;
14995 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14996 break;
14997 ++row;
14998 }
14999
15000 return row_found;
15001 }
15002
15003
15004 /* Return the last row in the current matrix of W that is not affected
15005 by changes at the start of current_buffer that occurred since W's
15006 current matrix was built. Value is null if no such row exists.
15007
15008 BEG_UNCHANGED us the number of characters unchanged at the start of
15009 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15010 first changed character in current_buffer. Characters at positions <
15011 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15012 when the current matrix was built. */
15013
15014 static struct glyph_row *
15015 find_last_unchanged_at_beg_row (w)
15016 struct window *w;
15017 {
15018 int first_changed_pos = BEG + BEG_UNCHANGED;
15019 struct glyph_row *row;
15020 struct glyph_row *row_found = NULL;
15021 int yb = window_text_bottom_y (w);
15022
15023 /* Find the last row displaying unchanged text. */
15024 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15025 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15026 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15027 ++row)
15028 {
15029 if (/* If row ends before first_changed_pos, it is unchanged,
15030 except in some case. */
15031 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15032 /* When row ends in ZV and we write at ZV it is not
15033 unchanged. */
15034 && !row->ends_at_zv_p
15035 /* When first_changed_pos is the end of a continued line,
15036 row is not unchanged because it may be no longer
15037 continued. */
15038 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15039 && (row->continued_p
15040 || row->exact_window_width_line_p)))
15041 row_found = row;
15042
15043 /* Stop if last visible row. */
15044 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15045 break;
15046 }
15047
15048 return row_found;
15049 }
15050
15051
15052 /* Find the first glyph row in the current matrix of W that is not
15053 affected by changes at the end of current_buffer since the
15054 time W's current matrix was built.
15055
15056 Return in *DELTA the number of chars by which buffer positions in
15057 unchanged text at the end of current_buffer must be adjusted.
15058
15059 Return in *DELTA_BYTES the corresponding number of bytes.
15060
15061 Value is null if no such row exists, i.e. all rows are affected by
15062 changes. */
15063
15064 static struct glyph_row *
15065 find_first_unchanged_at_end_row (w, delta, delta_bytes)
15066 struct window *w;
15067 int *delta, *delta_bytes;
15068 {
15069 struct glyph_row *row;
15070 struct glyph_row *row_found = NULL;
15071
15072 *delta = *delta_bytes = 0;
15073
15074 /* Display must not have been paused, otherwise the current matrix
15075 is not up to date. */
15076 eassert (!NILP (w->window_end_valid));
15077
15078 /* A value of window_end_pos >= END_UNCHANGED means that the window
15079 end is in the range of changed text. If so, there is no
15080 unchanged row at the end of W's current matrix. */
15081 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15082 return NULL;
15083
15084 /* Set row to the last row in W's current matrix displaying text. */
15085 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15086
15087 /* If matrix is entirely empty, no unchanged row exists. */
15088 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15089 {
15090 /* The value of row is the last glyph row in the matrix having a
15091 meaningful buffer position in it. The end position of row
15092 corresponds to window_end_pos. This allows us to translate
15093 buffer positions in the current matrix to current buffer
15094 positions for characters not in changed text. */
15095 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15096 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15097 int last_unchanged_pos, last_unchanged_pos_old;
15098 struct glyph_row *first_text_row
15099 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15100
15101 *delta = Z - Z_old;
15102 *delta_bytes = Z_BYTE - Z_BYTE_old;
15103
15104 /* Set last_unchanged_pos to the buffer position of the last
15105 character in the buffer that has not been changed. Z is the
15106 index + 1 of the last character in current_buffer, i.e. by
15107 subtracting END_UNCHANGED we get the index of the last
15108 unchanged character, and we have to add BEG to get its buffer
15109 position. */
15110 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15111 last_unchanged_pos_old = last_unchanged_pos - *delta;
15112
15113 /* Search backward from ROW for a row displaying a line that
15114 starts at a minimum position >= last_unchanged_pos_old. */
15115 for (; row > first_text_row; --row)
15116 {
15117 /* This used to abort, but it can happen.
15118 It is ok to just stop the search instead here. KFS. */
15119 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15120 break;
15121
15122 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15123 row_found = row;
15124 }
15125 }
15126
15127 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15128
15129 return row_found;
15130 }
15131
15132
15133 /* Make sure that glyph rows in the current matrix of window W
15134 reference the same glyph memory as corresponding rows in the
15135 frame's frame matrix. This function is called after scrolling W's
15136 current matrix on a terminal frame in try_window_id and
15137 try_window_reusing_current_matrix. */
15138
15139 static void
15140 sync_frame_with_window_matrix_rows (w)
15141 struct window *w;
15142 {
15143 struct frame *f = XFRAME (w->frame);
15144 struct glyph_row *window_row, *window_row_end, *frame_row;
15145
15146 /* Preconditions: W must be a leaf window and full-width. Its frame
15147 must have a frame matrix. */
15148 xassert (NILP (w->hchild) && NILP (w->vchild));
15149 xassert (WINDOW_FULL_WIDTH_P (w));
15150 xassert (!FRAME_WINDOW_P (f));
15151
15152 /* If W is a full-width window, glyph pointers in W's current matrix
15153 have, by definition, to be the same as glyph pointers in the
15154 corresponding frame matrix. Note that frame matrices have no
15155 marginal areas (see build_frame_matrix). */
15156 window_row = w->current_matrix->rows;
15157 window_row_end = window_row + w->current_matrix->nrows;
15158 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15159 while (window_row < window_row_end)
15160 {
15161 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15162 struct glyph *end = window_row->glyphs[LAST_AREA];
15163
15164 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15165 frame_row->glyphs[TEXT_AREA] = start;
15166 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15167 frame_row->glyphs[LAST_AREA] = end;
15168
15169 /* Disable frame rows whose corresponding window rows have
15170 been disabled in try_window_id. */
15171 if (!window_row->enabled_p)
15172 frame_row->enabled_p = 0;
15173
15174 ++window_row, ++frame_row;
15175 }
15176 }
15177
15178
15179 /* Find the glyph row in window W containing CHARPOS. Consider all
15180 rows between START and END (not inclusive). END null means search
15181 all rows to the end of the display area of W. Value is the row
15182 containing CHARPOS or null. */
15183
15184 struct glyph_row *
15185 row_containing_pos (w, charpos, start, end, dy)
15186 struct window *w;
15187 int charpos;
15188 struct glyph_row *start, *end;
15189 int dy;
15190 {
15191 struct glyph_row *row = start;
15192 int last_y;
15193
15194 /* If we happen to start on a header-line, skip that. */
15195 if (row->mode_line_p)
15196 ++row;
15197
15198 if ((end && row >= end) || !row->enabled_p)
15199 return NULL;
15200
15201 last_y = window_text_bottom_y (w) - dy;
15202
15203 while (1)
15204 {
15205 /* Give up if we have gone too far. */
15206 if (end && row >= end)
15207 return NULL;
15208 /* This formerly returned if they were equal.
15209 I think that both quantities are of a "last plus one" type;
15210 if so, when they are equal, the row is within the screen. -- rms. */
15211 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15212 return NULL;
15213
15214 /* If it is in this row, return this row. */
15215 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15216 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15217 /* The end position of a row equals the start
15218 position of the next row. If CHARPOS is there, we
15219 would rather display it in the next line, except
15220 when this line ends in ZV. */
15221 && !row->ends_at_zv_p
15222 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15223 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15224 return row;
15225 ++row;
15226 }
15227 }
15228
15229
15230 /* Try to redisplay window W by reusing its existing display. W's
15231 current matrix must be up to date when this function is called,
15232 i.e. window_end_valid must not be nil.
15233
15234 Value is
15235
15236 1 if display has been updated
15237 0 if otherwise unsuccessful
15238 -1 if redisplay with same window start is known not to succeed
15239
15240 The following steps are performed:
15241
15242 1. Find the last row in the current matrix of W that is not
15243 affected by changes at the start of current_buffer. If no such row
15244 is found, give up.
15245
15246 2. Find the first row in W's current matrix that is not affected by
15247 changes at the end of current_buffer. Maybe there is no such row.
15248
15249 3. Display lines beginning with the row + 1 found in step 1 to the
15250 row found in step 2 or, if step 2 didn't find a row, to the end of
15251 the window.
15252
15253 4. If cursor is not known to appear on the window, give up.
15254
15255 5. If display stopped at the row found in step 2, scroll the
15256 display and current matrix as needed.
15257
15258 6. Maybe display some lines at the end of W, if we must. This can
15259 happen under various circumstances, like a partially visible line
15260 becoming fully visible, or because newly displayed lines are displayed
15261 in smaller font sizes.
15262
15263 7. Update W's window end information. */
15264
15265 static int
15266 try_window_id (w)
15267 struct window *w;
15268 {
15269 struct frame *f = XFRAME (w->frame);
15270 struct glyph_matrix *current_matrix = w->current_matrix;
15271 struct glyph_matrix *desired_matrix = w->desired_matrix;
15272 struct glyph_row *last_unchanged_at_beg_row;
15273 struct glyph_row *first_unchanged_at_end_row;
15274 struct glyph_row *row;
15275 struct glyph_row *bottom_row;
15276 int bottom_vpos;
15277 struct it it;
15278 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
15279 struct text_pos start_pos;
15280 struct run run;
15281 int first_unchanged_at_end_vpos = 0;
15282 struct glyph_row *last_text_row, *last_text_row_at_end;
15283 struct text_pos start;
15284 int first_changed_charpos, last_changed_charpos;
15285
15286 #if GLYPH_DEBUG
15287 if (inhibit_try_window_id)
15288 return 0;
15289 #endif
15290
15291 /* This is handy for debugging. */
15292 #if 0
15293 #define GIVE_UP(X) \
15294 do { \
15295 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15296 return 0; \
15297 } while (0)
15298 #else
15299 #define GIVE_UP(X) return 0
15300 #endif
15301
15302 SET_TEXT_POS_FROM_MARKER (start, w->start);
15303
15304 /* Don't use this for mini-windows because these can show
15305 messages and mini-buffers, and we don't handle that here. */
15306 if (MINI_WINDOW_P (w))
15307 GIVE_UP (1);
15308
15309 /* This flag is used to prevent redisplay optimizations. */
15310 if (windows_or_buffers_changed || cursor_type_changed)
15311 GIVE_UP (2);
15312
15313 /* Verify that narrowing has not changed.
15314 Also verify that we were not told to prevent redisplay optimizations.
15315 It would be nice to further
15316 reduce the number of cases where this prevents try_window_id. */
15317 if (current_buffer->clip_changed
15318 || current_buffer->prevent_redisplay_optimizations_p)
15319 GIVE_UP (3);
15320
15321 /* Window must either use window-based redisplay or be full width. */
15322 if (!FRAME_WINDOW_P (f)
15323 && (!FRAME_LINE_INS_DEL_OK (f)
15324 || !WINDOW_FULL_WIDTH_P (w)))
15325 GIVE_UP (4);
15326
15327 /* Give up if point is known NOT to appear in W. */
15328 if (PT < CHARPOS (start))
15329 GIVE_UP (5);
15330
15331 /* Another way to prevent redisplay optimizations. */
15332 if (XFASTINT (w->last_modified) == 0)
15333 GIVE_UP (6);
15334
15335 /* Verify that window is not hscrolled. */
15336 if (XFASTINT (w->hscroll) != 0)
15337 GIVE_UP (7);
15338
15339 /* Verify that display wasn't paused. */
15340 if (NILP (w->window_end_valid))
15341 GIVE_UP (8);
15342
15343 /* Can't use this if highlighting a region because a cursor movement
15344 will do more than just set the cursor. */
15345 if (!NILP (Vtransient_mark_mode)
15346 && !NILP (current_buffer->mark_active))
15347 GIVE_UP (9);
15348
15349 /* Likewise if highlighting trailing whitespace. */
15350 if (!NILP (Vshow_trailing_whitespace))
15351 GIVE_UP (11);
15352
15353 /* Likewise if showing a region. */
15354 if (!NILP (w->region_showing))
15355 GIVE_UP (10);
15356
15357 /* Can't use this if overlay arrow position and/or string have
15358 changed. */
15359 if (overlay_arrows_changed_p ())
15360 GIVE_UP (12);
15361
15362 /* When word-wrap is on, adding a space to the first word of a
15363 wrapped line can change the wrap position, altering the line
15364 above it. It might be worthwhile to handle this more
15365 intelligently, but for now just redisplay from scratch. */
15366 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15367 GIVE_UP (21);
15368
15369 /* Under bidi reordering, adding or deleting a character in the
15370 beginning of a paragraph, before the first strong directional
15371 character, can change the base direction of the paragraph (unless
15372 the buffer specifies a fixed paragraph direction), which will
15373 require to redisplay the whole paragraph. It might be worthwhile
15374 to find the paragraph limits and widen the range of redisplayed
15375 lines to that, but for now just give up this optimization and
15376 redisplay from scratch. */
15377 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15378 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15379 GIVE_UP (22);
15380
15381 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15382 only if buffer has really changed. The reason is that the gap is
15383 initially at Z for freshly visited files. The code below would
15384 set end_unchanged to 0 in that case. */
15385 if (MODIFF > SAVE_MODIFF
15386 /* This seems to happen sometimes after saving a buffer. */
15387 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15388 {
15389 if (GPT - BEG < BEG_UNCHANGED)
15390 BEG_UNCHANGED = GPT - BEG;
15391 if (Z - GPT < END_UNCHANGED)
15392 END_UNCHANGED = Z - GPT;
15393 }
15394
15395 /* The position of the first and last character that has been changed. */
15396 first_changed_charpos = BEG + BEG_UNCHANGED;
15397 last_changed_charpos = Z - END_UNCHANGED;
15398
15399 /* If window starts after a line end, and the last change is in
15400 front of that newline, then changes don't affect the display.
15401 This case happens with stealth-fontification. Note that although
15402 the display is unchanged, glyph positions in the matrix have to
15403 be adjusted, of course. */
15404 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15405 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15406 && ((last_changed_charpos < CHARPOS (start)
15407 && CHARPOS (start) == BEGV)
15408 || (last_changed_charpos < CHARPOS (start) - 1
15409 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15410 {
15411 int Z_old, delta, Z_BYTE_old, delta_bytes;
15412 struct glyph_row *r0;
15413
15414 /* Compute how many chars/bytes have been added to or removed
15415 from the buffer. */
15416 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15417 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15418 delta = Z - Z_old;
15419 delta_bytes = Z_BYTE - Z_BYTE_old;
15420
15421 /* Give up if PT is not in the window. Note that it already has
15422 been checked at the start of try_window_id that PT is not in
15423 front of the window start. */
15424 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15425 GIVE_UP (13);
15426
15427 /* If window start is unchanged, we can reuse the whole matrix
15428 as is, after adjusting glyph positions. No need to compute
15429 the window end again, since its offset from Z hasn't changed. */
15430 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15431 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15432 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15433 /* PT must not be in a partially visible line. */
15434 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15435 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15436 {
15437 /* Adjust positions in the glyph matrix. */
15438 if (delta || delta_bytes)
15439 {
15440 struct glyph_row *r1
15441 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15442 increment_matrix_positions (w->current_matrix,
15443 MATRIX_ROW_VPOS (r0, current_matrix),
15444 MATRIX_ROW_VPOS (r1, current_matrix),
15445 delta, delta_bytes);
15446 }
15447
15448 /* Set the cursor. */
15449 row = row_containing_pos (w, PT, r0, NULL, 0);
15450 if (row)
15451 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15452 else
15453 abort ();
15454 return 1;
15455 }
15456 }
15457
15458 /* Handle the case that changes are all below what is displayed in
15459 the window, and that PT is in the window. This shortcut cannot
15460 be taken if ZV is visible in the window, and text has been added
15461 there that is visible in the window. */
15462 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15463 /* ZV is not visible in the window, or there are no
15464 changes at ZV, actually. */
15465 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15466 || first_changed_charpos == last_changed_charpos))
15467 {
15468 struct glyph_row *r0;
15469
15470 /* Give up if PT is not in the window. Note that it already has
15471 been checked at the start of try_window_id that PT is not in
15472 front of the window start. */
15473 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15474 GIVE_UP (14);
15475
15476 /* If window start is unchanged, we can reuse the whole matrix
15477 as is, without changing glyph positions since no text has
15478 been added/removed in front of the window end. */
15479 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15480 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15481 /* PT must not be in a partially visible line. */
15482 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15483 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15484 {
15485 /* We have to compute the window end anew since text
15486 can have been added/removed after it. */
15487 w->window_end_pos
15488 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15489 w->window_end_bytepos
15490 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15491
15492 /* Set the cursor. */
15493 row = row_containing_pos (w, PT, r0, NULL, 0);
15494 if (row)
15495 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15496 else
15497 abort ();
15498 return 2;
15499 }
15500 }
15501
15502 /* Give up if window start is in the changed area.
15503
15504 The condition used to read
15505
15506 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15507
15508 but why that was tested escapes me at the moment. */
15509 if (CHARPOS (start) >= first_changed_charpos
15510 && CHARPOS (start) <= last_changed_charpos)
15511 GIVE_UP (15);
15512
15513 /* Check that window start agrees with the start of the first glyph
15514 row in its current matrix. Check this after we know the window
15515 start is not in changed text, otherwise positions would not be
15516 comparable. */
15517 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15518 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15519 GIVE_UP (16);
15520
15521 /* Give up if the window ends in strings. Overlay strings
15522 at the end are difficult to handle, so don't try. */
15523 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15524 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15525 GIVE_UP (20);
15526
15527 /* Compute the position at which we have to start displaying new
15528 lines. Some of the lines at the top of the window might be
15529 reusable because they are not displaying changed text. Find the
15530 last row in W's current matrix not affected by changes at the
15531 start of current_buffer. Value is null if changes start in the
15532 first line of window. */
15533 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15534 if (last_unchanged_at_beg_row)
15535 {
15536 /* Avoid starting to display in the moddle of a character, a TAB
15537 for instance. This is easier than to set up the iterator
15538 exactly, and it's not a frequent case, so the additional
15539 effort wouldn't really pay off. */
15540 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15541 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15542 && last_unchanged_at_beg_row > w->current_matrix->rows)
15543 --last_unchanged_at_beg_row;
15544
15545 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15546 GIVE_UP (17);
15547
15548 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15549 GIVE_UP (18);
15550 start_pos = it.current.pos;
15551
15552 /* Start displaying new lines in the desired matrix at the same
15553 vpos we would use in the current matrix, i.e. below
15554 last_unchanged_at_beg_row. */
15555 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15556 current_matrix);
15557 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15558 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15559
15560 xassert (it.hpos == 0 && it.current_x == 0);
15561 }
15562 else
15563 {
15564 /* There are no reusable lines at the start of the window.
15565 Start displaying in the first text line. */
15566 start_display (&it, w, start);
15567 it.vpos = it.first_vpos;
15568 start_pos = it.current.pos;
15569 }
15570
15571 /* Find the first row that is not affected by changes at the end of
15572 the buffer. Value will be null if there is no unchanged row, in
15573 which case we must redisplay to the end of the window. delta
15574 will be set to the value by which buffer positions beginning with
15575 first_unchanged_at_end_row have to be adjusted due to text
15576 changes. */
15577 first_unchanged_at_end_row
15578 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15579 IF_DEBUG (debug_delta = delta);
15580 IF_DEBUG (debug_delta_bytes = delta_bytes);
15581
15582 /* Set stop_pos to the buffer position up to which we will have to
15583 display new lines. If first_unchanged_at_end_row != NULL, this
15584 is the buffer position of the start of the line displayed in that
15585 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15586 that we don't stop at a buffer position. */
15587 stop_pos = 0;
15588 if (first_unchanged_at_end_row)
15589 {
15590 xassert (last_unchanged_at_beg_row == NULL
15591 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15592
15593 /* If this is a continuation line, move forward to the next one
15594 that isn't. Changes in lines above affect this line.
15595 Caution: this may move first_unchanged_at_end_row to a row
15596 not displaying text. */
15597 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15598 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15599 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15600 < it.last_visible_y))
15601 ++first_unchanged_at_end_row;
15602
15603 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15604 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15605 >= it.last_visible_y))
15606 first_unchanged_at_end_row = NULL;
15607 else
15608 {
15609 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15610 + delta);
15611 first_unchanged_at_end_vpos
15612 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15613 xassert (stop_pos >= Z - END_UNCHANGED);
15614 }
15615 }
15616 else if (last_unchanged_at_beg_row == NULL)
15617 GIVE_UP (19);
15618
15619
15620 #if GLYPH_DEBUG
15621
15622 /* Either there is no unchanged row at the end, or the one we have
15623 now displays text. This is a necessary condition for the window
15624 end pos calculation at the end of this function. */
15625 xassert (first_unchanged_at_end_row == NULL
15626 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15627
15628 debug_last_unchanged_at_beg_vpos
15629 = (last_unchanged_at_beg_row
15630 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15631 : -1);
15632 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15633
15634 #endif /* GLYPH_DEBUG != 0 */
15635
15636
15637 /* Display new lines. Set last_text_row to the last new line
15638 displayed which has text on it, i.e. might end up as being the
15639 line where the window_end_vpos is. */
15640 w->cursor.vpos = -1;
15641 last_text_row = NULL;
15642 overlay_arrow_seen = 0;
15643 while (it.current_y < it.last_visible_y
15644 && !fonts_changed_p
15645 && (first_unchanged_at_end_row == NULL
15646 || IT_CHARPOS (it) < stop_pos))
15647 {
15648 if (display_line (&it))
15649 last_text_row = it.glyph_row - 1;
15650 }
15651
15652 if (fonts_changed_p)
15653 return -1;
15654
15655
15656 /* Compute differences in buffer positions, y-positions etc. for
15657 lines reused at the bottom of the window. Compute what we can
15658 scroll. */
15659 if (first_unchanged_at_end_row
15660 /* No lines reused because we displayed everything up to the
15661 bottom of the window. */
15662 && it.current_y < it.last_visible_y)
15663 {
15664 dvpos = (it.vpos
15665 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15666 current_matrix));
15667 dy = it.current_y - first_unchanged_at_end_row->y;
15668 run.current_y = first_unchanged_at_end_row->y;
15669 run.desired_y = run.current_y + dy;
15670 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15671 }
15672 else
15673 {
15674 delta = delta_bytes = dvpos = dy
15675 = run.current_y = run.desired_y = run.height = 0;
15676 first_unchanged_at_end_row = NULL;
15677 }
15678 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15679
15680
15681 /* Find the cursor if not already found. We have to decide whether
15682 PT will appear on this window (it sometimes doesn't, but this is
15683 not a very frequent case.) This decision has to be made before
15684 the current matrix is altered. A value of cursor.vpos < 0 means
15685 that PT is either in one of the lines beginning at
15686 first_unchanged_at_end_row or below the window. Don't care for
15687 lines that might be displayed later at the window end; as
15688 mentioned, this is not a frequent case. */
15689 if (w->cursor.vpos < 0)
15690 {
15691 /* Cursor in unchanged rows at the top? */
15692 if (PT < CHARPOS (start_pos)
15693 && last_unchanged_at_beg_row)
15694 {
15695 row = row_containing_pos (w, PT,
15696 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15697 last_unchanged_at_beg_row + 1, 0);
15698 if (row)
15699 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15700 }
15701
15702 /* Start from first_unchanged_at_end_row looking for PT. */
15703 else if (first_unchanged_at_end_row)
15704 {
15705 row = row_containing_pos (w, PT - delta,
15706 first_unchanged_at_end_row, NULL, 0);
15707 if (row)
15708 set_cursor_from_row (w, row, w->current_matrix, delta,
15709 delta_bytes, dy, dvpos);
15710 }
15711
15712 /* Give up if cursor was not found. */
15713 if (w->cursor.vpos < 0)
15714 {
15715 clear_glyph_matrix (w->desired_matrix);
15716 return -1;
15717 }
15718 }
15719
15720 /* Don't let the cursor end in the scroll margins. */
15721 {
15722 int this_scroll_margin, cursor_height;
15723
15724 this_scroll_margin = max (0, scroll_margin);
15725 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15726 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15727 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15728
15729 if ((w->cursor.y < this_scroll_margin
15730 && CHARPOS (start) > BEGV)
15731 /* Old redisplay didn't take scroll margin into account at the bottom,
15732 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15733 || (w->cursor.y + (make_cursor_line_fully_visible_p
15734 ? cursor_height + this_scroll_margin
15735 : 1)) > it.last_visible_y)
15736 {
15737 w->cursor.vpos = -1;
15738 clear_glyph_matrix (w->desired_matrix);
15739 return -1;
15740 }
15741 }
15742
15743 /* Scroll the display. Do it before changing the current matrix so
15744 that xterm.c doesn't get confused about where the cursor glyph is
15745 found. */
15746 if (dy && run.height)
15747 {
15748 update_begin (f);
15749
15750 if (FRAME_WINDOW_P (f))
15751 {
15752 FRAME_RIF (f)->update_window_begin_hook (w);
15753 FRAME_RIF (f)->clear_window_mouse_face (w);
15754 FRAME_RIF (f)->scroll_run_hook (w, &run);
15755 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15756 }
15757 else
15758 {
15759 /* Terminal frame. In this case, dvpos gives the number of
15760 lines to scroll by; dvpos < 0 means scroll up. */
15761 int first_unchanged_at_end_vpos
15762 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15763 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15764 int end = (WINDOW_TOP_EDGE_LINE (w)
15765 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15766 + window_internal_height (w));
15767
15768 /* Perform the operation on the screen. */
15769 if (dvpos > 0)
15770 {
15771 /* Scroll last_unchanged_at_beg_row to the end of the
15772 window down dvpos lines. */
15773 set_terminal_window (f, end);
15774
15775 /* On dumb terminals delete dvpos lines at the end
15776 before inserting dvpos empty lines. */
15777 if (!FRAME_SCROLL_REGION_OK (f))
15778 ins_del_lines (f, end - dvpos, -dvpos);
15779
15780 /* Insert dvpos empty lines in front of
15781 last_unchanged_at_beg_row. */
15782 ins_del_lines (f, from, dvpos);
15783 }
15784 else if (dvpos < 0)
15785 {
15786 /* Scroll up last_unchanged_at_beg_vpos to the end of
15787 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15788 set_terminal_window (f, end);
15789
15790 /* Delete dvpos lines in front of
15791 last_unchanged_at_beg_vpos. ins_del_lines will set
15792 the cursor to the given vpos and emit |dvpos| delete
15793 line sequences. */
15794 ins_del_lines (f, from + dvpos, dvpos);
15795
15796 /* On a dumb terminal insert dvpos empty lines at the
15797 end. */
15798 if (!FRAME_SCROLL_REGION_OK (f))
15799 ins_del_lines (f, end + dvpos, -dvpos);
15800 }
15801
15802 set_terminal_window (f, 0);
15803 }
15804
15805 update_end (f);
15806 }
15807
15808 /* Shift reused rows of the current matrix to the right position.
15809 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15810 text. */
15811 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15812 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15813 if (dvpos < 0)
15814 {
15815 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15816 bottom_vpos, dvpos);
15817 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15818 bottom_vpos, 0);
15819 }
15820 else if (dvpos > 0)
15821 {
15822 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15823 bottom_vpos, dvpos);
15824 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15825 first_unchanged_at_end_vpos + dvpos, 0);
15826 }
15827
15828 /* For frame-based redisplay, make sure that current frame and window
15829 matrix are in sync with respect to glyph memory. */
15830 if (!FRAME_WINDOW_P (f))
15831 sync_frame_with_window_matrix_rows (w);
15832
15833 /* Adjust buffer positions in reused rows. */
15834 if (delta || delta_bytes)
15835 increment_matrix_positions (current_matrix,
15836 first_unchanged_at_end_vpos + dvpos,
15837 bottom_vpos, delta, delta_bytes);
15838
15839 /* Adjust Y positions. */
15840 if (dy)
15841 shift_glyph_matrix (w, current_matrix,
15842 first_unchanged_at_end_vpos + dvpos,
15843 bottom_vpos, dy);
15844
15845 if (first_unchanged_at_end_row)
15846 {
15847 first_unchanged_at_end_row += dvpos;
15848 if (first_unchanged_at_end_row->y >= it.last_visible_y
15849 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15850 first_unchanged_at_end_row = NULL;
15851 }
15852
15853 /* If scrolling up, there may be some lines to display at the end of
15854 the window. */
15855 last_text_row_at_end = NULL;
15856 if (dy < 0)
15857 {
15858 /* Scrolling up can leave for example a partially visible line
15859 at the end of the window to be redisplayed. */
15860 /* Set last_row to the glyph row in the current matrix where the
15861 window end line is found. It has been moved up or down in
15862 the matrix by dvpos. */
15863 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15864 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15865
15866 /* If last_row is the window end line, it should display text. */
15867 xassert (last_row->displays_text_p);
15868
15869 /* If window end line was partially visible before, begin
15870 displaying at that line. Otherwise begin displaying with the
15871 line following it. */
15872 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15873 {
15874 init_to_row_start (&it, w, last_row);
15875 it.vpos = last_vpos;
15876 it.current_y = last_row->y;
15877 }
15878 else
15879 {
15880 init_to_row_end (&it, w, last_row);
15881 it.vpos = 1 + last_vpos;
15882 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15883 ++last_row;
15884 }
15885
15886 /* We may start in a continuation line. If so, we have to
15887 get the right continuation_lines_width and current_x. */
15888 it.continuation_lines_width = last_row->continuation_lines_width;
15889 it.hpos = it.current_x = 0;
15890
15891 /* Display the rest of the lines at the window end. */
15892 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15893 while (it.current_y < it.last_visible_y
15894 && !fonts_changed_p)
15895 {
15896 /* Is it always sure that the display agrees with lines in
15897 the current matrix? I don't think so, so we mark rows
15898 displayed invalid in the current matrix by setting their
15899 enabled_p flag to zero. */
15900 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15901 if (display_line (&it))
15902 last_text_row_at_end = it.glyph_row - 1;
15903 }
15904 }
15905
15906 /* Update window_end_pos and window_end_vpos. */
15907 if (first_unchanged_at_end_row
15908 && !last_text_row_at_end)
15909 {
15910 /* Window end line if one of the preserved rows from the current
15911 matrix. Set row to the last row displaying text in current
15912 matrix starting at first_unchanged_at_end_row, after
15913 scrolling. */
15914 xassert (first_unchanged_at_end_row->displays_text_p);
15915 row = find_last_row_displaying_text (w->current_matrix, &it,
15916 first_unchanged_at_end_row);
15917 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15918
15919 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15920 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15921 w->window_end_vpos
15922 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15923 xassert (w->window_end_bytepos >= 0);
15924 IF_DEBUG (debug_method_add (w, "A"));
15925 }
15926 else if (last_text_row_at_end)
15927 {
15928 w->window_end_pos
15929 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15930 w->window_end_bytepos
15931 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15932 w->window_end_vpos
15933 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15934 xassert (w->window_end_bytepos >= 0);
15935 IF_DEBUG (debug_method_add (w, "B"));
15936 }
15937 else if (last_text_row)
15938 {
15939 /* We have displayed either to the end of the window or at the
15940 end of the window, i.e. the last row with text is to be found
15941 in the desired matrix. */
15942 w->window_end_pos
15943 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15944 w->window_end_bytepos
15945 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15946 w->window_end_vpos
15947 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15948 xassert (w->window_end_bytepos >= 0);
15949 }
15950 else if (first_unchanged_at_end_row == NULL
15951 && last_text_row == NULL
15952 && last_text_row_at_end == NULL)
15953 {
15954 /* Displayed to end of window, but no line containing text was
15955 displayed. Lines were deleted at the end of the window. */
15956 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15957 int vpos = XFASTINT (w->window_end_vpos);
15958 struct glyph_row *current_row = current_matrix->rows + vpos;
15959 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15960
15961 for (row = NULL;
15962 row == NULL && vpos >= first_vpos;
15963 --vpos, --current_row, --desired_row)
15964 {
15965 if (desired_row->enabled_p)
15966 {
15967 if (desired_row->displays_text_p)
15968 row = desired_row;
15969 }
15970 else if (current_row->displays_text_p)
15971 row = current_row;
15972 }
15973
15974 xassert (row != NULL);
15975 w->window_end_vpos = make_number (vpos + 1);
15976 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15977 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15978 xassert (w->window_end_bytepos >= 0);
15979 IF_DEBUG (debug_method_add (w, "C"));
15980 }
15981 else
15982 abort ();
15983
15984 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15985 debug_end_vpos = XFASTINT (w->window_end_vpos));
15986
15987 /* Record that display has not been completed. */
15988 w->window_end_valid = Qnil;
15989 w->desired_matrix->no_scrolling_p = 1;
15990 return 3;
15991
15992 #undef GIVE_UP
15993 }
15994
15995
15996 \f
15997 /***********************************************************************
15998 More debugging support
15999 ***********************************************************************/
16000
16001 #if GLYPH_DEBUG
16002
16003 void dump_glyph_row P_ ((struct glyph_row *, int, int));
16004 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
16005 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
16006
16007
16008 /* Dump the contents of glyph matrix MATRIX on stderr.
16009
16010 GLYPHS 0 means don't show glyph contents.
16011 GLYPHS 1 means show glyphs in short form
16012 GLYPHS > 1 means show glyphs in long form. */
16013
16014 void
16015 dump_glyph_matrix (matrix, glyphs)
16016 struct glyph_matrix *matrix;
16017 int glyphs;
16018 {
16019 int i;
16020 for (i = 0; i < matrix->nrows; ++i)
16021 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16022 }
16023
16024
16025 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16026 the glyph row and area where the glyph comes from. */
16027
16028 void
16029 dump_glyph (row, glyph, area)
16030 struct glyph_row *row;
16031 struct glyph *glyph;
16032 int area;
16033 {
16034 if (glyph->type == CHAR_GLYPH)
16035 {
16036 fprintf (stderr,
16037 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16038 glyph - row->glyphs[TEXT_AREA],
16039 'C',
16040 glyph->charpos,
16041 (BUFFERP (glyph->object)
16042 ? 'B'
16043 : (STRINGP (glyph->object)
16044 ? 'S'
16045 : '-')),
16046 glyph->pixel_width,
16047 glyph->u.ch,
16048 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16049 ? glyph->u.ch
16050 : '.'),
16051 glyph->face_id,
16052 glyph->left_box_line_p,
16053 glyph->right_box_line_p);
16054 }
16055 else if (glyph->type == STRETCH_GLYPH)
16056 {
16057 fprintf (stderr,
16058 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16059 glyph - row->glyphs[TEXT_AREA],
16060 'S',
16061 glyph->charpos,
16062 (BUFFERP (glyph->object)
16063 ? 'B'
16064 : (STRINGP (glyph->object)
16065 ? 'S'
16066 : '-')),
16067 glyph->pixel_width,
16068 0,
16069 '.',
16070 glyph->face_id,
16071 glyph->left_box_line_p,
16072 glyph->right_box_line_p);
16073 }
16074 else if (glyph->type == IMAGE_GLYPH)
16075 {
16076 fprintf (stderr,
16077 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16078 glyph - row->glyphs[TEXT_AREA],
16079 'I',
16080 glyph->charpos,
16081 (BUFFERP (glyph->object)
16082 ? 'B'
16083 : (STRINGP (glyph->object)
16084 ? 'S'
16085 : '-')),
16086 glyph->pixel_width,
16087 glyph->u.img_id,
16088 '.',
16089 glyph->face_id,
16090 glyph->left_box_line_p,
16091 glyph->right_box_line_p);
16092 }
16093 else if (glyph->type == COMPOSITE_GLYPH)
16094 {
16095 fprintf (stderr,
16096 " %5d %4c %6d %c %3d 0x%05x",
16097 glyph - row->glyphs[TEXT_AREA],
16098 '+',
16099 glyph->charpos,
16100 (BUFFERP (glyph->object)
16101 ? 'B'
16102 : (STRINGP (glyph->object)
16103 ? 'S'
16104 : '-')),
16105 glyph->pixel_width,
16106 glyph->u.cmp.id);
16107 if (glyph->u.cmp.automatic)
16108 fprintf (stderr,
16109 "[%d-%d]",
16110 glyph->u.cmp.from, glyph->u.cmp.to);
16111 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16112 glyph->face_id,
16113 glyph->left_box_line_p,
16114 glyph->right_box_line_p);
16115 }
16116 }
16117
16118
16119 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16120 GLYPHS 0 means don't show glyph contents.
16121 GLYPHS 1 means show glyphs in short form
16122 GLYPHS > 1 means show glyphs in long form. */
16123
16124 void
16125 dump_glyph_row (row, vpos, glyphs)
16126 struct glyph_row *row;
16127 int vpos, glyphs;
16128 {
16129 if (glyphs != 1)
16130 {
16131 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16132 fprintf (stderr, "======================================================================\n");
16133
16134 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16135 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16136 vpos,
16137 MATRIX_ROW_START_CHARPOS (row),
16138 MATRIX_ROW_END_CHARPOS (row),
16139 row->used[TEXT_AREA],
16140 row->contains_overlapping_glyphs_p,
16141 row->enabled_p,
16142 row->truncated_on_left_p,
16143 row->truncated_on_right_p,
16144 row->continued_p,
16145 MATRIX_ROW_CONTINUATION_LINE_P (row),
16146 row->displays_text_p,
16147 row->ends_at_zv_p,
16148 row->fill_line_p,
16149 row->ends_in_middle_of_char_p,
16150 row->starts_in_middle_of_char_p,
16151 row->mouse_face_p,
16152 row->x,
16153 row->y,
16154 row->pixel_width,
16155 row->height,
16156 row->visible_height,
16157 row->ascent,
16158 row->phys_ascent);
16159 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16160 row->end.overlay_string_index,
16161 row->continuation_lines_width);
16162 fprintf (stderr, "%9d %5d\n",
16163 CHARPOS (row->start.string_pos),
16164 CHARPOS (row->end.string_pos));
16165 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16166 row->end.dpvec_index);
16167 }
16168
16169 if (glyphs > 1)
16170 {
16171 int area;
16172
16173 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16174 {
16175 struct glyph *glyph = row->glyphs[area];
16176 struct glyph *glyph_end = glyph + row->used[area];
16177
16178 /* Glyph for a line end in text. */
16179 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16180 ++glyph_end;
16181
16182 if (glyph < glyph_end)
16183 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16184
16185 for (; glyph < glyph_end; ++glyph)
16186 dump_glyph (row, glyph, area);
16187 }
16188 }
16189 else if (glyphs == 1)
16190 {
16191 int area;
16192
16193 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16194 {
16195 char *s = (char *) alloca (row->used[area] + 1);
16196 int i;
16197
16198 for (i = 0; i < row->used[area]; ++i)
16199 {
16200 struct glyph *glyph = row->glyphs[area] + i;
16201 if (glyph->type == CHAR_GLYPH
16202 && glyph->u.ch < 0x80
16203 && glyph->u.ch >= ' ')
16204 s[i] = glyph->u.ch;
16205 else
16206 s[i] = '.';
16207 }
16208
16209 s[i] = '\0';
16210 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16211 }
16212 }
16213 }
16214
16215
16216 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16217 Sdump_glyph_matrix, 0, 1, "p",
16218 doc: /* Dump the current matrix of the selected window to stderr.
16219 Shows contents of glyph row structures. With non-nil
16220 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16221 glyphs in short form, otherwise show glyphs in long form. */)
16222 (glyphs)
16223 Lisp_Object glyphs;
16224 {
16225 struct window *w = XWINDOW (selected_window);
16226 struct buffer *buffer = XBUFFER (w->buffer);
16227
16228 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16229 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16230 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16231 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16232 fprintf (stderr, "=============================================\n");
16233 dump_glyph_matrix (w->current_matrix,
16234 NILP (glyphs) ? 0 : XINT (glyphs));
16235 return Qnil;
16236 }
16237
16238
16239 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16240 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16241 ()
16242 {
16243 struct frame *f = XFRAME (selected_frame);
16244 dump_glyph_matrix (f->current_matrix, 1);
16245 return Qnil;
16246 }
16247
16248
16249 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16250 doc: /* Dump glyph row ROW to stderr.
16251 GLYPH 0 means don't dump glyphs.
16252 GLYPH 1 means dump glyphs in short form.
16253 GLYPH > 1 or omitted means dump glyphs in long form. */)
16254 (row, glyphs)
16255 Lisp_Object row, glyphs;
16256 {
16257 struct glyph_matrix *matrix;
16258 int vpos;
16259
16260 CHECK_NUMBER (row);
16261 matrix = XWINDOW (selected_window)->current_matrix;
16262 vpos = XINT (row);
16263 if (vpos >= 0 && vpos < matrix->nrows)
16264 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16265 vpos,
16266 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16267 return Qnil;
16268 }
16269
16270
16271 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16272 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16273 GLYPH 0 means don't dump glyphs.
16274 GLYPH 1 means dump glyphs in short form.
16275 GLYPH > 1 or omitted means dump glyphs in long form. */)
16276 (row, glyphs)
16277 Lisp_Object row, glyphs;
16278 {
16279 struct frame *sf = SELECTED_FRAME ();
16280 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16281 int vpos;
16282
16283 CHECK_NUMBER (row);
16284 vpos = XINT (row);
16285 if (vpos >= 0 && vpos < m->nrows)
16286 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16287 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16288 return Qnil;
16289 }
16290
16291
16292 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16293 doc: /* Toggle tracing of redisplay.
16294 With ARG, turn tracing on if and only if ARG is positive. */)
16295 (arg)
16296 Lisp_Object arg;
16297 {
16298 if (NILP (arg))
16299 trace_redisplay_p = !trace_redisplay_p;
16300 else
16301 {
16302 arg = Fprefix_numeric_value (arg);
16303 trace_redisplay_p = XINT (arg) > 0;
16304 }
16305
16306 return Qnil;
16307 }
16308
16309
16310 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16311 doc: /* Like `format', but print result to stderr.
16312 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16313 (nargs, args)
16314 int nargs;
16315 Lisp_Object *args;
16316 {
16317 Lisp_Object s = Fformat (nargs, args);
16318 fprintf (stderr, "%s", SDATA (s));
16319 return Qnil;
16320 }
16321
16322 #endif /* GLYPH_DEBUG */
16323
16324
16325 \f
16326 /***********************************************************************
16327 Building Desired Matrix Rows
16328 ***********************************************************************/
16329
16330 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16331 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16332
16333 static struct glyph_row *
16334 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
16335 struct window *w;
16336 Lisp_Object overlay_arrow_string;
16337 {
16338 struct frame *f = XFRAME (WINDOW_FRAME (w));
16339 struct buffer *buffer = XBUFFER (w->buffer);
16340 struct buffer *old = current_buffer;
16341 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16342 int arrow_len = SCHARS (overlay_arrow_string);
16343 const unsigned char *arrow_end = arrow_string + arrow_len;
16344 const unsigned char *p;
16345 struct it it;
16346 int multibyte_p;
16347 int n_glyphs_before;
16348
16349 set_buffer_temp (buffer);
16350 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16351 it.glyph_row->used[TEXT_AREA] = 0;
16352 SET_TEXT_POS (it.position, 0, 0);
16353
16354 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16355 p = arrow_string;
16356 while (p < arrow_end)
16357 {
16358 Lisp_Object face, ilisp;
16359
16360 /* Get the next character. */
16361 if (multibyte_p)
16362 it.c = string_char_and_length (p, &it.len);
16363 else
16364 it.c = *p, it.len = 1;
16365 p += it.len;
16366
16367 /* Get its face. */
16368 ilisp = make_number (p - arrow_string);
16369 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16370 it.face_id = compute_char_face (f, it.c, face);
16371
16372 /* Compute its width, get its glyphs. */
16373 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16374 SET_TEXT_POS (it.position, -1, -1);
16375 PRODUCE_GLYPHS (&it);
16376
16377 /* If this character doesn't fit any more in the line, we have
16378 to remove some glyphs. */
16379 if (it.current_x > it.last_visible_x)
16380 {
16381 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16382 break;
16383 }
16384 }
16385
16386 set_buffer_temp (old);
16387 return it.glyph_row;
16388 }
16389
16390
16391 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16392 glyphs are only inserted for terminal frames since we can't really
16393 win with truncation glyphs when partially visible glyphs are
16394 involved. Which glyphs to insert is determined by
16395 produce_special_glyphs. */
16396
16397 static void
16398 insert_left_trunc_glyphs (it)
16399 struct it *it;
16400 {
16401 struct it truncate_it;
16402 struct glyph *from, *end, *to, *toend;
16403
16404 xassert (!FRAME_WINDOW_P (it->f));
16405
16406 /* Get the truncation glyphs. */
16407 truncate_it = *it;
16408 truncate_it.current_x = 0;
16409 truncate_it.face_id = DEFAULT_FACE_ID;
16410 truncate_it.glyph_row = &scratch_glyph_row;
16411 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16412 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16413 truncate_it.object = make_number (0);
16414 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16415
16416 /* Overwrite glyphs from IT with truncation glyphs. */
16417 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16418 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16419 to = it->glyph_row->glyphs[TEXT_AREA];
16420 toend = to + it->glyph_row->used[TEXT_AREA];
16421
16422 while (from < end)
16423 *to++ = *from++;
16424
16425 /* There may be padding glyphs left over. Overwrite them too. */
16426 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16427 {
16428 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16429 while (from < end)
16430 *to++ = *from++;
16431 }
16432
16433 if (to > toend)
16434 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16435 }
16436
16437
16438 /* Compute the pixel height and width of IT->glyph_row.
16439
16440 Most of the time, ascent and height of a display line will be equal
16441 to the max_ascent and max_height values of the display iterator
16442 structure. This is not the case if
16443
16444 1. We hit ZV without displaying anything. In this case, max_ascent
16445 and max_height will be zero.
16446
16447 2. We have some glyphs that don't contribute to the line height.
16448 (The glyph row flag contributes_to_line_height_p is for future
16449 pixmap extensions).
16450
16451 The first case is easily covered by using default values because in
16452 these cases, the line height does not really matter, except that it
16453 must not be zero. */
16454
16455 static void
16456 compute_line_metrics (it)
16457 struct it *it;
16458 {
16459 struct glyph_row *row = it->glyph_row;
16460 int area, i;
16461
16462 if (FRAME_WINDOW_P (it->f))
16463 {
16464 int i, min_y, max_y;
16465
16466 /* The line may consist of one space only, that was added to
16467 place the cursor on it. If so, the row's height hasn't been
16468 computed yet. */
16469 if (row->height == 0)
16470 {
16471 if (it->max_ascent + it->max_descent == 0)
16472 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16473 row->ascent = it->max_ascent;
16474 row->height = it->max_ascent + it->max_descent;
16475 row->phys_ascent = it->max_phys_ascent;
16476 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16477 row->extra_line_spacing = it->max_extra_line_spacing;
16478 }
16479
16480 /* Compute the width of this line. */
16481 row->pixel_width = row->x;
16482 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16483 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16484
16485 xassert (row->pixel_width >= 0);
16486 xassert (row->ascent >= 0 && row->height > 0);
16487
16488 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16489 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16490
16491 /* If first line's physical ascent is larger than its logical
16492 ascent, use the physical ascent, and make the row taller.
16493 This makes accented characters fully visible. */
16494 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16495 && row->phys_ascent > row->ascent)
16496 {
16497 row->height += row->phys_ascent - row->ascent;
16498 row->ascent = row->phys_ascent;
16499 }
16500
16501 /* Compute how much of the line is visible. */
16502 row->visible_height = row->height;
16503
16504 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16505 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16506
16507 if (row->y < min_y)
16508 row->visible_height -= min_y - row->y;
16509 if (row->y + row->height > max_y)
16510 row->visible_height -= row->y + row->height - max_y;
16511 }
16512 else
16513 {
16514 row->pixel_width = row->used[TEXT_AREA];
16515 if (row->continued_p)
16516 row->pixel_width -= it->continuation_pixel_width;
16517 else if (row->truncated_on_right_p)
16518 row->pixel_width -= it->truncation_pixel_width;
16519 row->ascent = row->phys_ascent = 0;
16520 row->height = row->phys_height = row->visible_height = 1;
16521 row->extra_line_spacing = 0;
16522 }
16523
16524 /* Compute a hash code for this row. */
16525 row->hash = 0;
16526 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16527 for (i = 0; i < row->used[area]; ++i)
16528 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16529 + row->glyphs[area][i].u.val
16530 + row->glyphs[area][i].face_id
16531 + row->glyphs[area][i].padding_p
16532 + (row->glyphs[area][i].type << 2));
16533
16534 it->max_ascent = it->max_descent = 0;
16535 it->max_phys_ascent = it->max_phys_descent = 0;
16536 }
16537
16538
16539 /* Append one space to the glyph row of iterator IT if doing a
16540 window-based redisplay. The space has the same face as
16541 IT->face_id. Value is non-zero if a space was added.
16542
16543 This function is called to make sure that there is always one glyph
16544 at the end of a glyph row that the cursor can be set on under
16545 window-systems. (If there weren't such a glyph we would not know
16546 how wide and tall a box cursor should be displayed).
16547
16548 At the same time this space let's a nicely handle clearing to the
16549 end of the line if the row ends in italic text. */
16550
16551 static int
16552 append_space_for_newline (it, default_face_p)
16553 struct it *it;
16554 int default_face_p;
16555 {
16556 if (FRAME_WINDOW_P (it->f))
16557 {
16558 int n = it->glyph_row->used[TEXT_AREA];
16559
16560 if (it->glyph_row->glyphs[TEXT_AREA] + n
16561 < it->glyph_row->glyphs[1 + TEXT_AREA])
16562 {
16563 /* Save some values that must not be changed.
16564 Must save IT->c and IT->len because otherwise
16565 ITERATOR_AT_END_P wouldn't work anymore after
16566 append_space_for_newline has been called. */
16567 enum display_element_type saved_what = it->what;
16568 int saved_c = it->c, saved_len = it->len;
16569 int saved_x = it->current_x;
16570 int saved_face_id = it->face_id;
16571 struct text_pos saved_pos;
16572 Lisp_Object saved_object;
16573 struct face *face;
16574
16575 saved_object = it->object;
16576 saved_pos = it->position;
16577
16578 it->what = IT_CHARACTER;
16579 bzero (&it->position, sizeof it->position);
16580 it->object = make_number (0);
16581 it->c = ' ';
16582 it->len = 1;
16583
16584 if (default_face_p)
16585 it->face_id = DEFAULT_FACE_ID;
16586 else if (it->face_before_selective_p)
16587 it->face_id = it->saved_face_id;
16588 face = FACE_FROM_ID (it->f, it->face_id);
16589 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16590
16591 PRODUCE_GLYPHS (it);
16592
16593 it->override_ascent = -1;
16594 it->constrain_row_ascent_descent_p = 0;
16595 it->current_x = saved_x;
16596 it->object = saved_object;
16597 it->position = saved_pos;
16598 it->what = saved_what;
16599 it->face_id = saved_face_id;
16600 it->len = saved_len;
16601 it->c = saved_c;
16602 return 1;
16603 }
16604 }
16605
16606 return 0;
16607 }
16608
16609
16610 /* Extend the face of the last glyph in the text area of IT->glyph_row
16611 to the end of the display line. Called from display_line.
16612 If the glyph row is empty, add a space glyph to it so that we
16613 know the face to draw. Set the glyph row flag fill_line_p. */
16614
16615 static void
16616 extend_face_to_end_of_line (it)
16617 struct it *it;
16618 {
16619 struct face *face;
16620 struct frame *f = it->f;
16621
16622 /* If line is already filled, do nothing. */
16623 if (it->current_x >= it->last_visible_x)
16624 return;
16625
16626 /* Face extension extends the background and box of IT->face_id
16627 to the end of the line. If the background equals the background
16628 of the frame, we don't have to do anything. */
16629 if (it->face_before_selective_p)
16630 face = FACE_FROM_ID (it->f, it->saved_face_id);
16631 else
16632 face = FACE_FROM_ID (f, it->face_id);
16633
16634 if (FRAME_WINDOW_P (f)
16635 && it->glyph_row->displays_text_p
16636 && face->box == FACE_NO_BOX
16637 && face->background == FRAME_BACKGROUND_PIXEL (f)
16638 && !face->stipple)
16639 return;
16640
16641 /* Set the glyph row flag indicating that the face of the last glyph
16642 in the text area has to be drawn to the end of the text area. */
16643 it->glyph_row->fill_line_p = 1;
16644
16645 /* If current character of IT is not ASCII, make sure we have the
16646 ASCII face. This will be automatically undone the next time
16647 get_next_display_element returns a multibyte character. Note
16648 that the character will always be single byte in unibyte
16649 text. */
16650 if (!ASCII_CHAR_P (it->c))
16651 {
16652 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16653 }
16654
16655 if (FRAME_WINDOW_P (f))
16656 {
16657 /* If the row is empty, add a space with the current face of IT,
16658 so that we know which face to draw. */
16659 if (it->glyph_row->used[TEXT_AREA] == 0)
16660 {
16661 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16662 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16663 it->glyph_row->used[TEXT_AREA] = 1;
16664 }
16665 }
16666 else
16667 {
16668 /* Save some values that must not be changed. */
16669 int saved_x = it->current_x;
16670 struct text_pos saved_pos;
16671 Lisp_Object saved_object;
16672 enum display_element_type saved_what = it->what;
16673 int saved_face_id = it->face_id;
16674
16675 saved_object = it->object;
16676 saved_pos = it->position;
16677
16678 it->what = IT_CHARACTER;
16679 bzero (&it->position, sizeof it->position);
16680 it->object = make_number (0);
16681 it->c = ' ';
16682 it->len = 1;
16683 it->face_id = face->id;
16684
16685 PRODUCE_GLYPHS (it);
16686
16687 while (it->current_x <= it->last_visible_x)
16688 PRODUCE_GLYPHS (it);
16689
16690 /* Don't count these blanks really. It would let us insert a left
16691 truncation glyph below and make us set the cursor on them, maybe. */
16692 it->current_x = saved_x;
16693 it->object = saved_object;
16694 it->position = saved_pos;
16695 it->what = saved_what;
16696 it->face_id = saved_face_id;
16697 }
16698 }
16699
16700
16701 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16702 trailing whitespace. */
16703
16704 static int
16705 trailing_whitespace_p (charpos)
16706 int charpos;
16707 {
16708 int bytepos = CHAR_TO_BYTE (charpos);
16709 int c = 0;
16710
16711 while (bytepos < ZV_BYTE
16712 && (c = FETCH_CHAR (bytepos),
16713 c == ' ' || c == '\t'))
16714 ++bytepos;
16715
16716 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16717 {
16718 if (bytepos != PT_BYTE)
16719 return 1;
16720 }
16721 return 0;
16722 }
16723
16724
16725 /* Highlight trailing whitespace, if any, in ROW. */
16726
16727 void
16728 highlight_trailing_whitespace (f, row)
16729 struct frame *f;
16730 struct glyph_row *row;
16731 {
16732 int used = row->used[TEXT_AREA];
16733
16734 if (used)
16735 {
16736 struct glyph *start = row->glyphs[TEXT_AREA];
16737 struct glyph *glyph = start + used - 1;
16738
16739 /* Skip over glyphs inserted to display the cursor at the
16740 end of a line, for extending the face of the last glyph
16741 to the end of the line on terminals, and for truncation
16742 and continuation glyphs. */
16743 while (glyph >= start
16744 && glyph->type == CHAR_GLYPH
16745 && INTEGERP (glyph->object))
16746 --glyph;
16747
16748 /* If last glyph is a space or stretch, and it's trailing
16749 whitespace, set the face of all trailing whitespace glyphs in
16750 IT->glyph_row to `trailing-whitespace'. */
16751 if (glyph >= start
16752 && BUFFERP (glyph->object)
16753 && (glyph->type == STRETCH_GLYPH
16754 || (glyph->type == CHAR_GLYPH
16755 && glyph->u.ch == ' '))
16756 && trailing_whitespace_p (glyph->charpos))
16757 {
16758 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16759 if (face_id < 0)
16760 return;
16761
16762 while (glyph >= start
16763 && BUFFERP (glyph->object)
16764 && (glyph->type == STRETCH_GLYPH
16765 || (glyph->type == CHAR_GLYPH
16766 && glyph->u.ch == ' ')))
16767 (glyph--)->face_id = face_id;
16768 }
16769 }
16770 }
16771
16772
16773 /* Value is non-zero if glyph row ROW in window W should be
16774 used to hold the cursor. */
16775
16776 static int
16777 cursor_row_p (w, row)
16778 struct window *w;
16779 struct glyph_row *row;
16780 {
16781 int cursor_row_p = 1;
16782
16783 if (PT == MATRIX_ROW_END_CHARPOS (row))
16784 {
16785 /* Suppose the row ends on a string.
16786 Unless the row is continued, that means it ends on a newline
16787 in the string. If it's anything other than a display string
16788 (e.g. a before-string from an overlay), we don't want the
16789 cursor there. (This heuristic seems to give the optimal
16790 behavior for the various types of multi-line strings.) */
16791 if (CHARPOS (row->end.string_pos) >= 0)
16792 {
16793 if (row->continued_p)
16794 cursor_row_p = 1;
16795 else
16796 {
16797 /* Check for `display' property. */
16798 struct glyph *beg = row->glyphs[TEXT_AREA];
16799 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16800 struct glyph *glyph;
16801
16802 cursor_row_p = 0;
16803 for (glyph = end; glyph >= beg; --glyph)
16804 if (STRINGP (glyph->object))
16805 {
16806 Lisp_Object prop
16807 = Fget_char_property (make_number (PT),
16808 Qdisplay, Qnil);
16809 cursor_row_p =
16810 (!NILP (prop)
16811 && display_prop_string_p (prop, glyph->object));
16812 break;
16813 }
16814 }
16815 }
16816 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16817 {
16818 /* If the row ends in middle of a real character,
16819 and the line is continued, we want the cursor here.
16820 That's because MATRIX_ROW_END_CHARPOS would equal
16821 PT if PT is before the character. */
16822 if (!row->ends_in_ellipsis_p)
16823 cursor_row_p = row->continued_p;
16824 else
16825 /* If the row ends in an ellipsis, then
16826 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16827 We want that position to be displayed after the ellipsis. */
16828 cursor_row_p = 0;
16829 }
16830 /* If the row ends at ZV, display the cursor at the end of that
16831 row instead of at the start of the row below. */
16832 else if (row->ends_at_zv_p)
16833 cursor_row_p = 1;
16834 else
16835 cursor_row_p = 0;
16836 }
16837
16838 return cursor_row_p;
16839 }
16840
16841 \f
16842
16843 /* Push the display property PROP so that it will be rendered at the
16844 current position in IT. Return 1 if PROP was successfully pushed,
16845 0 otherwise. */
16846
16847 static int
16848 push_display_prop (struct it *it, Lisp_Object prop)
16849 {
16850 push_it (it);
16851
16852 if (STRINGP (prop))
16853 {
16854 if (SCHARS (prop) == 0)
16855 {
16856 pop_it (it);
16857 return 0;
16858 }
16859
16860 it->string = prop;
16861 it->multibyte_p = STRING_MULTIBYTE (it->string);
16862 it->current.overlay_string_index = -1;
16863 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16864 it->end_charpos = it->string_nchars = SCHARS (it->string);
16865 it->method = GET_FROM_STRING;
16866 it->stop_charpos = 0;
16867 }
16868 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16869 {
16870 it->method = GET_FROM_STRETCH;
16871 it->object = prop;
16872 }
16873 #ifdef HAVE_WINDOW_SYSTEM
16874 else if (IMAGEP (prop))
16875 {
16876 it->what = IT_IMAGE;
16877 it->image_id = lookup_image (it->f, prop);
16878 it->method = GET_FROM_IMAGE;
16879 }
16880 #endif /* HAVE_WINDOW_SYSTEM */
16881 else
16882 {
16883 pop_it (it); /* bogus display property, give up */
16884 return 0;
16885 }
16886
16887 return 1;
16888 }
16889
16890 /* Return the character-property PROP at the current position in IT. */
16891
16892 static Lisp_Object
16893 get_it_property (it, prop)
16894 struct it *it;
16895 Lisp_Object prop;
16896 {
16897 Lisp_Object position;
16898
16899 if (STRINGP (it->object))
16900 position = make_number (IT_STRING_CHARPOS (*it));
16901 else if (BUFFERP (it->object))
16902 position = make_number (IT_CHARPOS (*it));
16903 else
16904 return Qnil;
16905
16906 return Fget_char_property (position, prop, it->object);
16907 }
16908
16909 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16910
16911 static void
16912 handle_line_prefix (struct it *it)
16913 {
16914 Lisp_Object prefix;
16915 if (it->continuation_lines_width > 0)
16916 {
16917 prefix = get_it_property (it, Qwrap_prefix);
16918 if (NILP (prefix))
16919 prefix = Vwrap_prefix;
16920 }
16921 else
16922 {
16923 prefix = get_it_property (it, Qline_prefix);
16924 if (NILP (prefix))
16925 prefix = Vline_prefix;
16926 }
16927 if (! NILP (prefix) && push_display_prop (it, prefix))
16928 {
16929 /* If the prefix is wider than the window, and we try to wrap
16930 it, it would acquire its own wrap prefix, and so on till the
16931 iterator stack overflows. So, don't wrap the prefix. */
16932 it->line_wrap = TRUNCATE;
16933 it->avoid_cursor_p = 1;
16934 }
16935 }
16936
16937 \f
16938
16939 /* Construct the glyph row IT->glyph_row in the desired matrix of
16940 IT->w from text at the current position of IT. See dispextern.h
16941 for an overview of struct it. Value is non-zero if
16942 IT->glyph_row displays text, as opposed to a line displaying ZV
16943 only. */
16944
16945 static int
16946 display_line (it)
16947 struct it *it;
16948 {
16949 struct glyph_row *row = it->glyph_row;
16950 Lisp_Object overlay_arrow_string;
16951 struct it wrap_it;
16952 int may_wrap = 0, wrap_x;
16953 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16954 int wrap_row_phys_ascent, wrap_row_phys_height;
16955 int wrap_row_extra_line_spacing;
16956 struct display_pos row_end;
16957
16958 /* We always start displaying at hpos zero even if hscrolled. */
16959 xassert (it->hpos == 0 && it->current_x == 0);
16960
16961 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16962 >= it->w->desired_matrix->nrows)
16963 {
16964 it->w->nrows_scale_factor++;
16965 fonts_changed_p = 1;
16966 return 0;
16967 }
16968
16969 /* Is IT->w showing the region? */
16970 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16971
16972 /* Clear the result glyph row and enable it. */
16973 prepare_desired_row (row);
16974
16975 row->y = it->current_y;
16976 row->start = it->start;
16977 row->continuation_lines_width = it->continuation_lines_width;
16978 row->displays_text_p = 1;
16979 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16980 it->starts_in_middle_of_char_p = 0;
16981 /* If the paragraph base direction is R2L, its glyphs should be
16982 reversed. */
16983 if (it->bidi_p && (it->bidi_it.level_stack[0].level & 1) != 0)
16984 row->reversed_p = 1;
16985
16986 /* Arrange the overlays nicely for our purposes. Usually, we call
16987 display_line on only one line at a time, in which case this
16988 can't really hurt too much, or we call it on lines which appear
16989 one after another in the buffer, in which case all calls to
16990 recenter_overlay_lists but the first will be pretty cheap. */
16991 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16992
16993 /* Move over display elements that are not visible because we are
16994 hscrolled. This may stop at an x-position < IT->first_visible_x
16995 if the first glyph is partially visible or if we hit a line end. */
16996 if (it->current_x < it->first_visible_x)
16997 {
16998 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16999 MOVE_TO_POS | MOVE_TO_X);
17000 }
17001 else
17002 {
17003 /* We only do this when not calling `move_it_in_display_line_to'
17004 above, because move_it_in_display_line_to calls
17005 handle_line_prefix itself. */
17006 handle_line_prefix (it);
17007 }
17008
17009 /* Get the initial row height. This is either the height of the
17010 text hscrolled, if there is any, or zero. */
17011 row->ascent = it->max_ascent;
17012 row->height = it->max_ascent + it->max_descent;
17013 row->phys_ascent = it->max_phys_ascent;
17014 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17015 row->extra_line_spacing = it->max_extra_line_spacing;
17016
17017 /* Loop generating characters. The loop is left with IT on the next
17018 character to display. */
17019 while (1)
17020 {
17021 int n_glyphs_before, hpos_before, x_before;
17022 int x, i, nglyphs;
17023 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17024
17025 /* Retrieve the next thing to display. Value is zero if end of
17026 buffer reached. */
17027 if (!get_next_display_element (it))
17028 {
17029 /* Maybe add a space at the end of this line that is used to
17030 display the cursor there under X. Set the charpos of the
17031 first glyph of blank lines not corresponding to any text
17032 to -1. */
17033 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17034 row->exact_window_width_line_p = 1;
17035 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17036 || row->used[TEXT_AREA] == 0)
17037 {
17038 row->glyphs[TEXT_AREA]->charpos = -1;
17039 row->displays_text_p = 0;
17040
17041 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17042 && (!MINI_WINDOW_P (it->w)
17043 || (minibuf_level && EQ (it->window, minibuf_window))))
17044 row->indicate_empty_line_p = 1;
17045 }
17046
17047 it->continuation_lines_width = 0;
17048 row->ends_at_zv_p = 1;
17049 /* A row that displays right-to-left text must always have
17050 its last face extended all the way to the end of line,
17051 even if this row ends in ZV. */
17052 if (row->reversed_p)
17053 extend_face_to_end_of_line (it);
17054 row_end = it->current;
17055 break;
17056 }
17057
17058 /* Now, get the metrics of what we want to display. This also
17059 generates glyphs in `row' (which is IT->glyph_row). */
17060 n_glyphs_before = row->used[TEXT_AREA];
17061 x = it->current_x;
17062
17063 /* Remember the line height so far in case the next element doesn't
17064 fit on the line. */
17065 if (it->line_wrap != TRUNCATE)
17066 {
17067 ascent = it->max_ascent;
17068 descent = it->max_descent;
17069 phys_ascent = it->max_phys_ascent;
17070 phys_descent = it->max_phys_descent;
17071
17072 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17073 {
17074 if (IT_DISPLAYING_WHITESPACE (it))
17075 may_wrap = 1;
17076 else if (may_wrap)
17077 {
17078 wrap_it = *it;
17079 wrap_x = x;
17080 wrap_row_used = row->used[TEXT_AREA];
17081 wrap_row_ascent = row->ascent;
17082 wrap_row_height = row->height;
17083 wrap_row_phys_ascent = row->phys_ascent;
17084 wrap_row_phys_height = row->phys_height;
17085 wrap_row_extra_line_spacing = row->extra_line_spacing;
17086 may_wrap = 0;
17087 }
17088 }
17089 }
17090
17091 PRODUCE_GLYPHS (it);
17092
17093 /* If this display element was in marginal areas, continue with
17094 the next one. */
17095 if (it->area != TEXT_AREA)
17096 {
17097 row->ascent = max (row->ascent, it->max_ascent);
17098 row->height = max (row->height, it->max_ascent + it->max_descent);
17099 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17100 row->phys_height = max (row->phys_height,
17101 it->max_phys_ascent + it->max_phys_descent);
17102 row->extra_line_spacing = max (row->extra_line_spacing,
17103 it->max_extra_line_spacing);
17104 set_iterator_to_next (it, 1);
17105 continue;
17106 }
17107
17108 /* Does the display element fit on the line? If we truncate
17109 lines, we should draw past the right edge of the window. If
17110 we don't truncate, we want to stop so that we can display the
17111 continuation glyph before the right margin. If lines are
17112 continued, there are two possible strategies for characters
17113 resulting in more than 1 glyph (e.g. tabs): Display as many
17114 glyphs as possible in this line and leave the rest for the
17115 continuation line, or display the whole element in the next
17116 line. Original redisplay did the former, so we do it also. */
17117 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17118 hpos_before = it->hpos;
17119 x_before = x;
17120
17121 if (/* Not a newline. */
17122 nglyphs > 0
17123 /* Glyphs produced fit entirely in the line. */
17124 && it->current_x < it->last_visible_x)
17125 {
17126 it->hpos += nglyphs;
17127 row->ascent = max (row->ascent, it->max_ascent);
17128 row->height = max (row->height, it->max_ascent + it->max_descent);
17129 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17130 row->phys_height = max (row->phys_height,
17131 it->max_phys_ascent + it->max_phys_descent);
17132 row->extra_line_spacing = max (row->extra_line_spacing,
17133 it->max_extra_line_spacing);
17134 if (it->current_x - it->pixel_width < it->first_visible_x)
17135 row->x = x - it->first_visible_x;
17136 }
17137 else
17138 {
17139 int new_x;
17140 struct glyph *glyph;
17141
17142 for (i = 0; i < nglyphs; ++i, x = new_x)
17143 {
17144 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17145 new_x = x + glyph->pixel_width;
17146
17147 if (/* Lines are continued. */
17148 it->line_wrap != TRUNCATE
17149 && (/* Glyph doesn't fit on the line. */
17150 new_x > it->last_visible_x
17151 /* Or it fits exactly on a window system frame. */
17152 || (new_x == it->last_visible_x
17153 && FRAME_WINDOW_P (it->f))))
17154 {
17155 /* End of a continued line. */
17156
17157 if (it->hpos == 0
17158 || (new_x == it->last_visible_x
17159 && FRAME_WINDOW_P (it->f)))
17160 {
17161 /* Current glyph is the only one on the line or
17162 fits exactly on the line. We must continue
17163 the line because we can't draw the cursor
17164 after the glyph. */
17165 row->continued_p = 1;
17166 it->current_x = new_x;
17167 it->continuation_lines_width += new_x;
17168 ++it->hpos;
17169 if (i == nglyphs - 1)
17170 {
17171 /* If line-wrap is on, check if a previous
17172 wrap point was found. */
17173 if (wrap_row_used > 0
17174 /* Even if there is a previous wrap
17175 point, continue the line here as
17176 usual, if (i) the previous character
17177 was a space or tab AND (ii) the
17178 current character is not. */
17179 && (!may_wrap
17180 || IT_DISPLAYING_WHITESPACE (it)))
17181 goto back_to_wrap;
17182
17183 set_iterator_to_next (it, 1);
17184 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17185 {
17186 if (!get_next_display_element (it))
17187 {
17188 row->exact_window_width_line_p = 1;
17189 it->continuation_lines_width = 0;
17190 row->continued_p = 0;
17191 row->ends_at_zv_p = 1;
17192 }
17193 else if (ITERATOR_AT_END_OF_LINE_P (it))
17194 {
17195 row->continued_p = 0;
17196 row->exact_window_width_line_p = 1;
17197 }
17198 }
17199 }
17200 }
17201 else if (CHAR_GLYPH_PADDING_P (*glyph)
17202 && !FRAME_WINDOW_P (it->f))
17203 {
17204 /* A padding glyph that doesn't fit on this line.
17205 This means the whole character doesn't fit
17206 on the line. */
17207 row->used[TEXT_AREA] = n_glyphs_before;
17208
17209 /* Fill the rest of the row with continuation
17210 glyphs like in 20.x. */
17211 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17212 < row->glyphs[1 + TEXT_AREA])
17213 produce_special_glyphs (it, IT_CONTINUATION);
17214
17215 row->continued_p = 1;
17216 it->current_x = x_before;
17217 it->continuation_lines_width += x_before;
17218
17219 /* Restore the height to what it was before the
17220 element not fitting on the line. */
17221 it->max_ascent = ascent;
17222 it->max_descent = descent;
17223 it->max_phys_ascent = phys_ascent;
17224 it->max_phys_descent = phys_descent;
17225 }
17226 else if (wrap_row_used > 0)
17227 {
17228 back_to_wrap:
17229 *it = wrap_it;
17230 it->continuation_lines_width += wrap_x;
17231 row->used[TEXT_AREA] = wrap_row_used;
17232 row->ascent = wrap_row_ascent;
17233 row->height = wrap_row_height;
17234 row->phys_ascent = wrap_row_phys_ascent;
17235 row->phys_height = wrap_row_phys_height;
17236 row->extra_line_spacing = wrap_row_extra_line_spacing;
17237 row->continued_p = 1;
17238 row->ends_at_zv_p = 0;
17239 row->exact_window_width_line_p = 0;
17240 it->continuation_lines_width += x;
17241
17242 /* Make sure that a non-default face is extended
17243 up to the right margin of the window. */
17244 extend_face_to_end_of_line (it);
17245 }
17246 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17247 {
17248 /* A TAB that extends past the right edge of the
17249 window. This produces a single glyph on
17250 window system frames. We leave the glyph in
17251 this row and let it fill the row, but don't
17252 consume the TAB. */
17253 it->continuation_lines_width += it->last_visible_x;
17254 row->ends_in_middle_of_char_p = 1;
17255 row->continued_p = 1;
17256 glyph->pixel_width = it->last_visible_x - x;
17257 it->starts_in_middle_of_char_p = 1;
17258 }
17259 else
17260 {
17261 /* Something other than a TAB that draws past
17262 the right edge of the window. Restore
17263 positions to values before the element. */
17264 row->used[TEXT_AREA] = n_glyphs_before + i;
17265
17266 /* Display continuation glyphs. */
17267 if (!FRAME_WINDOW_P (it->f))
17268 produce_special_glyphs (it, IT_CONTINUATION);
17269 row->continued_p = 1;
17270
17271 it->current_x = x_before;
17272 it->continuation_lines_width += x;
17273 extend_face_to_end_of_line (it);
17274
17275 if (nglyphs > 1 && i > 0)
17276 {
17277 row->ends_in_middle_of_char_p = 1;
17278 it->starts_in_middle_of_char_p = 1;
17279 }
17280
17281 /* Restore the height to what it was before the
17282 element not fitting on the line. */
17283 it->max_ascent = ascent;
17284 it->max_descent = descent;
17285 it->max_phys_ascent = phys_ascent;
17286 it->max_phys_descent = phys_descent;
17287 }
17288
17289 row_end = it->current;
17290 break;
17291 }
17292 else if (new_x > it->first_visible_x)
17293 {
17294 /* Increment number of glyphs actually displayed. */
17295 ++it->hpos;
17296
17297 if (x < it->first_visible_x)
17298 /* Glyph is partially visible, i.e. row starts at
17299 negative X position. */
17300 row->x = x - it->first_visible_x;
17301 }
17302 else
17303 {
17304 /* Glyph is completely off the left margin of the
17305 window. This should not happen because of the
17306 move_it_in_display_line at the start of this
17307 function, unless the text display area of the
17308 window is empty. */
17309 xassert (it->first_visible_x <= it->last_visible_x);
17310 }
17311 }
17312
17313 row->ascent = max (row->ascent, it->max_ascent);
17314 row->height = max (row->height, it->max_ascent + it->max_descent);
17315 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17316 row->phys_height = max (row->phys_height,
17317 it->max_phys_ascent + it->max_phys_descent);
17318 row->extra_line_spacing = max (row->extra_line_spacing,
17319 it->max_extra_line_spacing);
17320
17321 /* End of this display line if row is continued. */
17322 if (row->continued_p || row->ends_at_zv_p)
17323 {
17324 row_end = it->current;
17325 break;
17326 }
17327 }
17328
17329 at_end_of_line:
17330 /* Is this a line end? If yes, we're also done, after making
17331 sure that a non-default face is extended up to the right
17332 margin of the window. */
17333 if (ITERATOR_AT_END_OF_LINE_P (it))
17334 {
17335 int used_before = row->used[TEXT_AREA];
17336
17337 row->ends_in_newline_from_string_p = STRINGP (it->object);
17338
17339 /* Add a space at the end of the line that is used to
17340 display the cursor there. */
17341 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17342 append_space_for_newline (it, 0);
17343
17344 /* Extend the face to the end of the line. */
17345 extend_face_to_end_of_line (it);
17346
17347 /* Make sure we have the position. */
17348 if (used_before == 0)
17349 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17350
17351 /* Consume the line end. This skips over invisible lines. */
17352 if (it->bidi_p)
17353 {
17354 /* When we are reordering bidi text, we still need the
17355 next character in logical order, to set row->end
17356 correctly below. */
17357 push_it (it);
17358 it->bidi_p = 0;
17359 set_iterator_to_next (it, 1);
17360 row_end = it->current;
17361 pop_it (it);
17362 it->bidi_p = 1;
17363 }
17364 set_iterator_to_next (it, 1);
17365 it->continuation_lines_width = 0;
17366 if (!it->bidi_p)
17367 row_end = it->current;
17368 break;
17369 }
17370
17371 /* Proceed with next display element. Note that this skips
17372 over lines invisible because of selective display. */
17373 set_iterator_to_next (it, 1);
17374
17375 /* If we truncate lines, we are done when the last displayed
17376 glyphs reach past the right margin of the window. */
17377 if (it->line_wrap == TRUNCATE
17378 && (FRAME_WINDOW_P (it->f)
17379 ? (it->current_x >= it->last_visible_x)
17380 : (it->current_x > it->last_visible_x)))
17381 {
17382 /* Maybe add truncation glyphs. */
17383 if (!FRAME_WINDOW_P (it->f))
17384 {
17385 int i, n;
17386
17387 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17388 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17389 break;
17390
17391 for (n = row->used[TEXT_AREA]; i < n; ++i)
17392 {
17393 row->used[TEXT_AREA] = i;
17394 produce_special_glyphs (it, IT_TRUNCATION);
17395 }
17396 }
17397 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17398 {
17399 /* Don't truncate if we can overflow newline into fringe. */
17400 if (!get_next_display_element (it))
17401 {
17402 it->continuation_lines_width = 0;
17403 row->ends_at_zv_p = 1;
17404 row->exact_window_width_line_p = 1;
17405 row_end = it->current;
17406 break;
17407 }
17408 if (ITERATOR_AT_END_OF_LINE_P (it))
17409 {
17410 row->exact_window_width_line_p = 1;
17411 goto at_end_of_line;
17412 }
17413 }
17414
17415 row->truncated_on_right_p = 1;
17416 it->continuation_lines_width = 0;
17417 reseat_at_next_visible_line_start (it, 0);
17418 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17419 it->hpos = hpos_before;
17420 it->current_x = x_before;
17421 row_end = it->current;
17422 break;
17423 }
17424 }
17425
17426 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17427 at the left window margin. */
17428 if (it->first_visible_x
17429 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
17430 {
17431 if (!FRAME_WINDOW_P (it->f))
17432 insert_left_trunc_glyphs (it);
17433 row->truncated_on_left_p = 1;
17434 }
17435
17436 /* If the start of this line is the overlay arrow-position, then
17437 mark this glyph row as the one containing the overlay arrow.
17438 This is clearly a mess with variable size fonts. It would be
17439 better to let it be displayed like cursors under X. */
17440 if ((row->displays_text_p || !overlay_arrow_seen)
17441 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17442 !NILP (overlay_arrow_string)))
17443 {
17444 /* Overlay arrow in window redisplay is a fringe bitmap. */
17445 if (STRINGP (overlay_arrow_string))
17446 {
17447 struct glyph_row *arrow_row
17448 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17449 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17450 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17451 struct glyph *p = row->glyphs[TEXT_AREA];
17452 struct glyph *p2, *end;
17453
17454 /* Copy the arrow glyphs. */
17455 while (glyph < arrow_end)
17456 *p++ = *glyph++;
17457
17458 /* Throw away padding glyphs. */
17459 p2 = p;
17460 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17461 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17462 ++p2;
17463 if (p2 > p)
17464 {
17465 while (p2 < end)
17466 *p++ = *p2++;
17467 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17468 }
17469 }
17470 else
17471 {
17472 xassert (INTEGERP (overlay_arrow_string));
17473 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17474 }
17475 overlay_arrow_seen = 1;
17476 }
17477
17478 /* Compute pixel dimensions of this line. */
17479 compute_line_metrics (it);
17480
17481 /* Remember the position at which this line ends. */
17482 row->end = row_end;
17483
17484 /* Record whether this row ends inside an ellipsis. */
17485 row->ends_in_ellipsis_p
17486 = (it->method == GET_FROM_DISPLAY_VECTOR
17487 && it->ellipsis_p);
17488
17489 /* Save fringe bitmaps in this row. */
17490 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17491 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17492 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17493 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17494
17495 it->left_user_fringe_bitmap = 0;
17496 it->left_user_fringe_face_id = 0;
17497 it->right_user_fringe_bitmap = 0;
17498 it->right_user_fringe_face_id = 0;
17499
17500 /* Maybe set the cursor. */
17501 if (it->w->cursor.vpos < 0
17502 && PT >= MATRIX_ROW_START_CHARPOS (row)
17503 && PT <= MATRIX_ROW_END_CHARPOS (row)
17504 && cursor_row_p (it->w, row))
17505 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17506
17507 /* Highlight trailing whitespace. */
17508 if (!NILP (Vshow_trailing_whitespace))
17509 highlight_trailing_whitespace (it->f, it->glyph_row);
17510
17511 /* Prepare for the next line. This line starts horizontally at (X
17512 HPOS) = (0 0). Vertical positions are incremented. As a
17513 convenience for the caller, IT->glyph_row is set to the next
17514 row to be used. */
17515 it->current_x = it->hpos = 0;
17516 it->current_y += row->height;
17517 ++it->vpos;
17518 ++it->glyph_row;
17519 it->start = row_end;
17520 return row->displays_text_p;
17521 }
17522
17523
17524 \f
17525 /***********************************************************************
17526 Menu Bar
17527 ***********************************************************************/
17528
17529 /* Redisplay the menu bar in the frame for window W.
17530
17531 The menu bar of X frames that don't have X toolkit support is
17532 displayed in a special window W->frame->menu_bar_window.
17533
17534 The menu bar of terminal frames is treated specially as far as
17535 glyph matrices are concerned. Menu bar lines are not part of
17536 windows, so the update is done directly on the frame matrix rows
17537 for the menu bar. */
17538
17539 static void
17540 display_menu_bar (w)
17541 struct window *w;
17542 {
17543 struct frame *f = XFRAME (WINDOW_FRAME (w));
17544 struct it it;
17545 Lisp_Object items;
17546 int i;
17547
17548 /* Don't do all this for graphical frames. */
17549 #ifdef HAVE_NTGUI
17550 if (FRAME_W32_P (f))
17551 return;
17552 #endif
17553 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17554 if (FRAME_X_P (f))
17555 return;
17556 #endif
17557
17558 #ifdef HAVE_NS
17559 if (FRAME_NS_P (f))
17560 return;
17561 #endif /* HAVE_NS */
17562
17563 #ifdef USE_X_TOOLKIT
17564 xassert (!FRAME_WINDOW_P (f));
17565 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17566 it.first_visible_x = 0;
17567 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17568 #else /* not USE_X_TOOLKIT */
17569 if (FRAME_WINDOW_P (f))
17570 {
17571 /* Menu bar lines are displayed in the desired matrix of the
17572 dummy window menu_bar_window. */
17573 struct window *menu_w;
17574 xassert (WINDOWP (f->menu_bar_window));
17575 menu_w = XWINDOW (f->menu_bar_window);
17576 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17577 MENU_FACE_ID);
17578 it.first_visible_x = 0;
17579 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17580 }
17581 else
17582 {
17583 /* This is a TTY frame, i.e. character hpos/vpos are used as
17584 pixel x/y. */
17585 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17586 MENU_FACE_ID);
17587 it.first_visible_x = 0;
17588 it.last_visible_x = FRAME_COLS (f);
17589 }
17590 #endif /* not USE_X_TOOLKIT */
17591
17592 if (! mode_line_inverse_video)
17593 /* Force the menu-bar to be displayed in the default face. */
17594 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17595
17596 /* Clear all rows of the menu bar. */
17597 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17598 {
17599 struct glyph_row *row = it.glyph_row + i;
17600 clear_glyph_row (row);
17601 row->enabled_p = 1;
17602 row->full_width_p = 1;
17603 }
17604
17605 /* Display all items of the menu bar. */
17606 items = FRAME_MENU_BAR_ITEMS (it.f);
17607 for (i = 0; i < XVECTOR (items)->size; i += 4)
17608 {
17609 Lisp_Object string;
17610
17611 /* Stop at nil string. */
17612 string = AREF (items, i + 1);
17613 if (NILP (string))
17614 break;
17615
17616 /* Remember where item was displayed. */
17617 ASET (items, i + 3, make_number (it.hpos));
17618
17619 /* Display the item, pad with one space. */
17620 if (it.current_x < it.last_visible_x)
17621 display_string (NULL, string, Qnil, 0, 0, &it,
17622 SCHARS (string) + 1, 0, 0, -1);
17623 }
17624
17625 /* Fill out the line with spaces. */
17626 if (it.current_x < it.last_visible_x)
17627 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17628
17629 /* Compute the total height of the lines. */
17630 compute_line_metrics (&it);
17631 }
17632
17633
17634 \f
17635 /***********************************************************************
17636 Mode Line
17637 ***********************************************************************/
17638
17639 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17640 FORCE is non-zero, redisplay mode lines unconditionally.
17641 Otherwise, redisplay only mode lines that are garbaged. Value is
17642 the number of windows whose mode lines were redisplayed. */
17643
17644 static int
17645 redisplay_mode_lines (window, force)
17646 Lisp_Object window;
17647 int force;
17648 {
17649 int nwindows = 0;
17650
17651 while (!NILP (window))
17652 {
17653 struct window *w = XWINDOW (window);
17654
17655 if (WINDOWP (w->hchild))
17656 nwindows += redisplay_mode_lines (w->hchild, force);
17657 else if (WINDOWP (w->vchild))
17658 nwindows += redisplay_mode_lines (w->vchild, force);
17659 else if (force
17660 || FRAME_GARBAGED_P (XFRAME (w->frame))
17661 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17662 {
17663 struct text_pos lpoint;
17664 struct buffer *old = current_buffer;
17665
17666 /* Set the window's buffer for the mode line display. */
17667 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17668 set_buffer_internal_1 (XBUFFER (w->buffer));
17669
17670 /* Point refers normally to the selected window. For any
17671 other window, set up appropriate value. */
17672 if (!EQ (window, selected_window))
17673 {
17674 struct text_pos pt;
17675
17676 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17677 if (CHARPOS (pt) < BEGV)
17678 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17679 else if (CHARPOS (pt) > (ZV - 1))
17680 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17681 else
17682 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17683 }
17684
17685 /* Display mode lines. */
17686 clear_glyph_matrix (w->desired_matrix);
17687 if (display_mode_lines (w))
17688 {
17689 ++nwindows;
17690 w->must_be_updated_p = 1;
17691 }
17692
17693 /* Restore old settings. */
17694 set_buffer_internal_1 (old);
17695 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17696 }
17697
17698 window = w->next;
17699 }
17700
17701 return nwindows;
17702 }
17703
17704
17705 /* Display the mode and/or header line of window W. Value is the
17706 sum number of mode lines and header lines displayed. */
17707
17708 static int
17709 display_mode_lines (w)
17710 struct window *w;
17711 {
17712 Lisp_Object old_selected_window, old_selected_frame;
17713 int n = 0;
17714
17715 old_selected_frame = selected_frame;
17716 selected_frame = w->frame;
17717 old_selected_window = selected_window;
17718 XSETWINDOW (selected_window, w);
17719
17720 /* These will be set while the mode line specs are processed. */
17721 line_number_displayed = 0;
17722 w->column_number_displayed = Qnil;
17723
17724 if (WINDOW_WANTS_MODELINE_P (w))
17725 {
17726 struct window *sel_w = XWINDOW (old_selected_window);
17727
17728 /* Select mode line face based on the real selected window. */
17729 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17730 current_buffer->mode_line_format);
17731 ++n;
17732 }
17733
17734 if (WINDOW_WANTS_HEADER_LINE_P (w))
17735 {
17736 display_mode_line (w, HEADER_LINE_FACE_ID,
17737 current_buffer->header_line_format);
17738 ++n;
17739 }
17740
17741 selected_frame = old_selected_frame;
17742 selected_window = old_selected_window;
17743 return n;
17744 }
17745
17746
17747 /* Display mode or header line of window W. FACE_ID specifies which
17748 line to display; it is either MODE_LINE_FACE_ID or
17749 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17750 display. Value is the pixel height of the mode/header line
17751 displayed. */
17752
17753 static int
17754 display_mode_line (w, face_id, format)
17755 struct window *w;
17756 enum face_id face_id;
17757 Lisp_Object format;
17758 {
17759 struct it it;
17760 struct face *face;
17761 int count = SPECPDL_INDEX ();
17762
17763 init_iterator (&it, w, -1, -1, NULL, face_id);
17764 /* Don't extend on a previously drawn mode-line.
17765 This may happen if called from pos_visible_p. */
17766 it.glyph_row->enabled_p = 0;
17767 prepare_desired_row (it.glyph_row);
17768
17769 it.glyph_row->mode_line_p = 1;
17770
17771 if (! mode_line_inverse_video)
17772 /* Force the mode-line to be displayed in the default face. */
17773 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17774
17775 record_unwind_protect (unwind_format_mode_line,
17776 format_mode_line_unwind_data (NULL, Qnil, 0));
17777
17778 mode_line_target = MODE_LINE_DISPLAY;
17779
17780 /* Temporarily make frame's keyboard the current kboard so that
17781 kboard-local variables in the mode_line_format will get the right
17782 values. */
17783 push_kboard (FRAME_KBOARD (it.f));
17784 record_unwind_save_match_data ();
17785 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17786 pop_kboard ();
17787
17788 unbind_to (count, Qnil);
17789
17790 /* Fill up with spaces. */
17791 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17792
17793 compute_line_metrics (&it);
17794 it.glyph_row->full_width_p = 1;
17795 it.glyph_row->continued_p = 0;
17796 it.glyph_row->truncated_on_left_p = 0;
17797 it.glyph_row->truncated_on_right_p = 0;
17798
17799 /* Make a 3D mode-line have a shadow at its right end. */
17800 face = FACE_FROM_ID (it.f, face_id);
17801 extend_face_to_end_of_line (&it);
17802 if (face->box != FACE_NO_BOX)
17803 {
17804 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17805 + it.glyph_row->used[TEXT_AREA] - 1);
17806 last->right_box_line_p = 1;
17807 }
17808
17809 return it.glyph_row->height;
17810 }
17811
17812 /* Move element ELT in LIST to the front of LIST.
17813 Return the updated list. */
17814
17815 static Lisp_Object
17816 move_elt_to_front (elt, list)
17817 Lisp_Object elt, list;
17818 {
17819 register Lisp_Object tail, prev;
17820 register Lisp_Object tem;
17821
17822 tail = list;
17823 prev = Qnil;
17824 while (CONSP (tail))
17825 {
17826 tem = XCAR (tail);
17827
17828 if (EQ (elt, tem))
17829 {
17830 /* Splice out the link TAIL. */
17831 if (NILP (prev))
17832 list = XCDR (tail);
17833 else
17834 Fsetcdr (prev, XCDR (tail));
17835
17836 /* Now make it the first. */
17837 Fsetcdr (tail, list);
17838 return tail;
17839 }
17840 else
17841 prev = tail;
17842 tail = XCDR (tail);
17843 QUIT;
17844 }
17845
17846 /* Not found--return unchanged LIST. */
17847 return list;
17848 }
17849
17850 /* Contribute ELT to the mode line for window IT->w. How it
17851 translates into text depends on its data type.
17852
17853 IT describes the display environment in which we display, as usual.
17854
17855 DEPTH is the depth in recursion. It is used to prevent
17856 infinite recursion here.
17857
17858 FIELD_WIDTH is the number of characters the display of ELT should
17859 occupy in the mode line, and PRECISION is the maximum number of
17860 characters to display from ELT's representation. See
17861 display_string for details.
17862
17863 Returns the hpos of the end of the text generated by ELT.
17864
17865 PROPS is a property list to add to any string we encounter.
17866
17867 If RISKY is nonzero, remove (disregard) any properties in any string
17868 we encounter, and ignore :eval and :propertize.
17869
17870 The global variable `mode_line_target' determines whether the
17871 output is passed to `store_mode_line_noprop',
17872 `store_mode_line_string', or `display_string'. */
17873
17874 static int
17875 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17876 struct it *it;
17877 int depth;
17878 int field_width, precision;
17879 Lisp_Object elt, props;
17880 int risky;
17881 {
17882 int n = 0, field, prec;
17883 int literal = 0;
17884
17885 tail_recurse:
17886 if (depth > 100)
17887 elt = build_string ("*too-deep*");
17888
17889 depth++;
17890
17891 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17892 {
17893 case Lisp_String:
17894 {
17895 /* A string: output it and check for %-constructs within it. */
17896 unsigned char c;
17897 int offset = 0;
17898
17899 if (SCHARS (elt) > 0
17900 && (!NILP (props) || risky))
17901 {
17902 Lisp_Object oprops, aelt;
17903 oprops = Ftext_properties_at (make_number (0), elt);
17904
17905 /* If the starting string's properties are not what
17906 we want, translate the string. Also, if the string
17907 is risky, do that anyway. */
17908
17909 if (NILP (Fequal (props, oprops)) || risky)
17910 {
17911 /* If the starting string has properties,
17912 merge the specified ones onto the existing ones. */
17913 if (! NILP (oprops) && !risky)
17914 {
17915 Lisp_Object tem;
17916
17917 oprops = Fcopy_sequence (oprops);
17918 tem = props;
17919 while (CONSP (tem))
17920 {
17921 oprops = Fplist_put (oprops, XCAR (tem),
17922 XCAR (XCDR (tem)));
17923 tem = XCDR (XCDR (tem));
17924 }
17925 props = oprops;
17926 }
17927
17928 aelt = Fassoc (elt, mode_line_proptrans_alist);
17929 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17930 {
17931 /* AELT is what we want. Move it to the front
17932 without consing. */
17933 elt = XCAR (aelt);
17934 mode_line_proptrans_alist
17935 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17936 }
17937 else
17938 {
17939 Lisp_Object tem;
17940
17941 /* If AELT has the wrong props, it is useless.
17942 so get rid of it. */
17943 if (! NILP (aelt))
17944 mode_line_proptrans_alist
17945 = Fdelq (aelt, mode_line_proptrans_alist);
17946
17947 elt = Fcopy_sequence (elt);
17948 Fset_text_properties (make_number (0), Flength (elt),
17949 props, elt);
17950 /* Add this item to mode_line_proptrans_alist. */
17951 mode_line_proptrans_alist
17952 = Fcons (Fcons (elt, props),
17953 mode_line_proptrans_alist);
17954 /* Truncate mode_line_proptrans_alist
17955 to at most 50 elements. */
17956 tem = Fnthcdr (make_number (50),
17957 mode_line_proptrans_alist);
17958 if (! NILP (tem))
17959 XSETCDR (tem, Qnil);
17960 }
17961 }
17962 }
17963
17964 offset = 0;
17965
17966 if (literal)
17967 {
17968 prec = precision - n;
17969 switch (mode_line_target)
17970 {
17971 case MODE_LINE_NOPROP:
17972 case MODE_LINE_TITLE:
17973 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17974 break;
17975 case MODE_LINE_STRING:
17976 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17977 break;
17978 case MODE_LINE_DISPLAY:
17979 n += display_string (NULL, elt, Qnil, 0, 0, it,
17980 0, prec, 0, STRING_MULTIBYTE (elt));
17981 break;
17982 }
17983
17984 break;
17985 }
17986
17987 /* Handle the non-literal case. */
17988
17989 while ((precision <= 0 || n < precision)
17990 && SREF (elt, offset) != 0
17991 && (mode_line_target != MODE_LINE_DISPLAY
17992 || it->current_x < it->last_visible_x))
17993 {
17994 int last_offset = offset;
17995
17996 /* Advance to end of string or next format specifier. */
17997 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17998 ;
17999
18000 if (offset - 1 != last_offset)
18001 {
18002 int nchars, nbytes;
18003
18004 /* Output to end of string or up to '%'. Field width
18005 is length of string. Don't output more than
18006 PRECISION allows us. */
18007 offset--;
18008
18009 prec = c_string_width (SDATA (elt) + last_offset,
18010 offset - last_offset, precision - n,
18011 &nchars, &nbytes);
18012
18013 switch (mode_line_target)
18014 {
18015 case MODE_LINE_NOPROP:
18016 case MODE_LINE_TITLE:
18017 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18018 break;
18019 case MODE_LINE_STRING:
18020 {
18021 int bytepos = last_offset;
18022 int charpos = string_byte_to_char (elt, bytepos);
18023 int endpos = (precision <= 0
18024 ? string_byte_to_char (elt, offset)
18025 : charpos + nchars);
18026
18027 n += store_mode_line_string (NULL,
18028 Fsubstring (elt, make_number (charpos),
18029 make_number (endpos)),
18030 0, 0, 0, Qnil);
18031 }
18032 break;
18033 case MODE_LINE_DISPLAY:
18034 {
18035 int bytepos = last_offset;
18036 int charpos = string_byte_to_char (elt, bytepos);
18037
18038 if (precision <= 0)
18039 nchars = string_byte_to_char (elt, offset) - charpos;
18040 n += display_string (NULL, elt, Qnil, 0, charpos,
18041 it, 0, nchars, 0,
18042 STRING_MULTIBYTE (elt));
18043 }
18044 break;
18045 }
18046 }
18047 else /* c == '%' */
18048 {
18049 int percent_position = offset;
18050
18051 /* Get the specified minimum width. Zero means
18052 don't pad. */
18053 field = 0;
18054 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18055 field = field * 10 + c - '0';
18056
18057 /* Don't pad beyond the total padding allowed. */
18058 if (field_width - n > 0 && field > field_width - n)
18059 field = field_width - n;
18060
18061 /* Note that either PRECISION <= 0 or N < PRECISION. */
18062 prec = precision - n;
18063
18064 if (c == 'M')
18065 n += display_mode_element (it, depth, field, prec,
18066 Vglobal_mode_string, props,
18067 risky);
18068 else if (c != 0)
18069 {
18070 int multibyte;
18071 int bytepos, charpos;
18072 unsigned char *spec;
18073
18074 bytepos = percent_position;
18075 charpos = (STRING_MULTIBYTE (elt)
18076 ? string_byte_to_char (elt, bytepos)
18077 : bytepos);
18078 spec
18079 = decode_mode_spec (it->w, c, field, prec, &multibyte);
18080
18081 switch (mode_line_target)
18082 {
18083 case MODE_LINE_NOPROP:
18084 case MODE_LINE_TITLE:
18085 n += store_mode_line_noprop (spec, field, prec);
18086 break;
18087 case MODE_LINE_STRING:
18088 {
18089 int len = strlen (spec);
18090 Lisp_Object tem = make_string (spec, len);
18091 props = Ftext_properties_at (make_number (charpos), elt);
18092 /* Should only keep face property in props */
18093 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18094 }
18095 break;
18096 case MODE_LINE_DISPLAY:
18097 {
18098 int nglyphs_before, nwritten;
18099
18100 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18101 nwritten = display_string (spec, Qnil, elt,
18102 charpos, 0, it,
18103 field, prec, 0,
18104 multibyte);
18105
18106 /* Assign to the glyphs written above the
18107 string where the `%x' came from, position
18108 of the `%'. */
18109 if (nwritten > 0)
18110 {
18111 struct glyph *glyph
18112 = (it->glyph_row->glyphs[TEXT_AREA]
18113 + nglyphs_before);
18114 int i;
18115
18116 for (i = 0; i < nwritten; ++i)
18117 {
18118 glyph[i].object = elt;
18119 glyph[i].charpos = charpos;
18120 }
18121
18122 n += nwritten;
18123 }
18124 }
18125 break;
18126 }
18127 }
18128 else /* c == 0 */
18129 break;
18130 }
18131 }
18132 }
18133 break;
18134
18135 case Lisp_Symbol:
18136 /* A symbol: process the value of the symbol recursively
18137 as if it appeared here directly. Avoid error if symbol void.
18138 Special case: if value of symbol is a string, output the string
18139 literally. */
18140 {
18141 register Lisp_Object tem;
18142
18143 /* If the variable is not marked as risky to set
18144 then its contents are risky to use. */
18145 if (NILP (Fget (elt, Qrisky_local_variable)))
18146 risky = 1;
18147
18148 tem = Fboundp (elt);
18149 if (!NILP (tem))
18150 {
18151 tem = Fsymbol_value (elt);
18152 /* If value is a string, output that string literally:
18153 don't check for % within it. */
18154 if (STRINGP (tem))
18155 literal = 1;
18156
18157 if (!EQ (tem, elt))
18158 {
18159 /* Give up right away for nil or t. */
18160 elt = tem;
18161 goto tail_recurse;
18162 }
18163 }
18164 }
18165 break;
18166
18167 case Lisp_Cons:
18168 {
18169 register Lisp_Object car, tem;
18170
18171 /* A cons cell: five distinct cases.
18172 If first element is :eval or :propertize, do something special.
18173 If first element is a string or a cons, process all the elements
18174 and effectively concatenate them.
18175 If first element is a negative number, truncate displaying cdr to
18176 at most that many characters. If positive, pad (with spaces)
18177 to at least that many characters.
18178 If first element is a symbol, process the cadr or caddr recursively
18179 according to whether the symbol's value is non-nil or nil. */
18180 car = XCAR (elt);
18181 if (EQ (car, QCeval))
18182 {
18183 /* An element of the form (:eval FORM) means evaluate FORM
18184 and use the result as mode line elements. */
18185
18186 if (risky)
18187 break;
18188
18189 if (CONSP (XCDR (elt)))
18190 {
18191 Lisp_Object spec;
18192 spec = safe_eval (XCAR (XCDR (elt)));
18193 n += display_mode_element (it, depth, field_width - n,
18194 precision - n, spec, props,
18195 risky);
18196 }
18197 }
18198 else if (EQ (car, QCpropertize))
18199 {
18200 /* An element of the form (:propertize ELT PROPS...)
18201 means display ELT but applying properties PROPS. */
18202
18203 if (risky)
18204 break;
18205
18206 if (CONSP (XCDR (elt)))
18207 n += display_mode_element (it, depth, field_width - n,
18208 precision - n, XCAR (XCDR (elt)),
18209 XCDR (XCDR (elt)), risky);
18210 }
18211 else if (SYMBOLP (car))
18212 {
18213 tem = Fboundp (car);
18214 elt = XCDR (elt);
18215 if (!CONSP (elt))
18216 goto invalid;
18217 /* elt is now the cdr, and we know it is a cons cell.
18218 Use its car if CAR has a non-nil value. */
18219 if (!NILP (tem))
18220 {
18221 tem = Fsymbol_value (car);
18222 if (!NILP (tem))
18223 {
18224 elt = XCAR (elt);
18225 goto tail_recurse;
18226 }
18227 }
18228 /* Symbol's value is nil (or symbol is unbound)
18229 Get the cddr of the original list
18230 and if possible find the caddr and use that. */
18231 elt = XCDR (elt);
18232 if (NILP (elt))
18233 break;
18234 else if (!CONSP (elt))
18235 goto invalid;
18236 elt = XCAR (elt);
18237 goto tail_recurse;
18238 }
18239 else if (INTEGERP (car))
18240 {
18241 register int lim = XINT (car);
18242 elt = XCDR (elt);
18243 if (lim < 0)
18244 {
18245 /* Negative int means reduce maximum width. */
18246 if (precision <= 0)
18247 precision = -lim;
18248 else
18249 precision = min (precision, -lim);
18250 }
18251 else if (lim > 0)
18252 {
18253 /* Padding specified. Don't let it be more than
18254 current maximum. */
18255 if (precision > 0)
18256 lim = min (precision, lim);
18257
18258 /* If that's more padding than already wanted, queue it.
18259 But don't reduce padding already specified even if
18260 that is beyond the current truncation point. */
18261 field_width = max (lim, field_width);
18262 }
18263 goto tail_recurse;
18264 }
18265 else if (STRINGP (car) || CONSP (car))
18266 {
18267 Lisp_Object halftail = elt;
18268 int len = 0;
18269
18270 while (CONSP (elt)
18271 && (precision <= 0 || n < precision))
18272 {
18273 n += display_mode_element (it, depth,
18274 /* Do padding only after the last
18275 element in the list. */
18276 (! CONSP (XCDR (elt))
18277 ? field_width - n
18278 : 0),
18279 precision - n, XCAR (elt),
18280 props, risky);
18281 elt = XCDR (elt);
18282 len++;
18283 if ((len & 1) == 0)
18284 halftail = XCDR (halftail);
18285 /* Check for cycle. */
18286 if (EQ (halftail, elt))
18287 break;
18288 }
18289 }
18290 }
18291 break;
18292
18293 default:
18294 invalid:
18295 elt = build_string ("*invalid*");
18296 goto tail_recurse;
18297 }
18298
18299 /* Pad to FIELD_WIDTH. */
18300 if (field_width > 0 && n < field_width)
18301 {
18302 switch (mode_line_target)
18303 {
18304 case MODE_LINE_NOPROP:
18305 case MODE_LINE_TITLE:
18306 n += store_mode_line_noprop ("", field_width - n, 0);
18307 break;
18308 case MODE_LINE_STRING:
18309 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18310 break;
18311 case MODE_LINE_DISPLAY:
18312 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18313 0, 0, 0);
18314 break;
18315 }
18316 }
18317
18318 return n;
18319 }
18320
18321 /* Store a mode-line string element in mode_line_string_list.
18322
18323 If STRING is non-null, display that C string. Otherwise, the Lisp
18324 string LISP_STRING is displayed.
18325
18326 FIELD_WIDTH is the minimum number of output glyphs to produce.
18327 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18328 with spaces. FIELD_WIDTH <= 0 means don't pad.
18329
18330 PRECISION is the maximum number of characters to output from
18331 STRING. PRECISION <= 0 means don't truncate the string.
18332
18333 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18334 properties to the string.
18335
18336 PROPS are the properties to add to the string.
18337 The mode_line_string_face face property is always added to the string.
18338 */
18339
18340 static int
18341 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
18342 char *string;
18343 Lisp_Object lisp_string;
18344 int copy_string;
18345 int field_width;
18346 int precision;
18347 Lisp_Object props;
18348 {
18349 int len;
18350 int n = 0;
18351
18352 if (string != NULL)
18353 {
18354 len = strlen (string);
18355 if (precision > 0 && len > precision)
18356 len = precision;
18357 lisp_string = make_string (string, len);
18358 if (NILP (props))
18359 props = mode_line_string_face_prop;
18360 else if (!NILP (mode_line_string_face))
18361 {
18362 Lisp_Object face = Fplist_get (props, Qface);
18363 props = Fcopy_sequence (props);
18364 if (NILP (face))
18365 face = mode_line_string_face;
18366 else
18367 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18368 props = Fplist_put (props, Qface, face);
18369 }
18370 Fadd_text_properties (make_number (0), make_number (len),
18371 props, lisp_string);
18372 }
18373 else
18374 {
18375 len = XFASTINT (Flength (lisp_string));
18376 if (precision > 0 && len > precision)
18377 {
18378 len = precision;
18379 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18380 precision = -1;
18381 }
18382 if (!NILP (mode_line_string_face))
18383 {
18384 Lisp_Object face;
18385 if (NILP (props))
18386 props = Ftext_properties_at (make_number (0), lisp_string);
18387 face = Fplist_get (props, Qface);
18388 if (NILP (face))
18389 face = mode_line_string_face;
18390 else
18391 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18392 props = Fcons (Qface, Fcons (face, Qnil));
18393 if (copy_string)
18394 lisp_string = Fcopy_sequence (lisp_string);
18395 }
18396 if (!NILP (props))
18397 Fadd_text_properties (make_number (0), make_number (len),
18398 props, lisp_string);
18399 }
18400
18401 if (len > 0)
18402 {
18403 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18404 n += len;
18405 }
18406
18407 if (field_width > len)
18408 {
18409 field_width -= len;
18410 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18411 if (!NILP (props))
18412 Fadd_text_properties (make_number (0), make_number (field_width),
18413 props, lisp_string);
18414 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18415 n += field_width;
18416 }
18417
18418 return n;
18419 }
18420
18421
18422 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18423 1, 4, 0,
18424 doc: /* Format a string out of a mode line format specification.
18425 First arg FORMAT specifies the mode line format (see `mode-line-format'
18426 for details) to use.
18427
18428 Optional second arg FACE specifies the face property to put
18429 on all characters for which no face is specified.
18430 The value t means whatever face the window's mode line currently uses
18431 \(either `mode-line' or `mode-line-inactive', depending).
18432 A value of nil means the default is no face property.
18433 If FACE is an integer, the value string has no text properties.
18434
18435 Optional third and fourth args WINDOW and BUFFER specify the window
18436 and buffer to use as the context for the formatting (defaults
18437 are the selected window and the window's buffer). */)
18438 (format, face, window, buffer)
18439 Lisp_Object format, face, window, buffer;
18440 {
18441 struct it it;
18442 int len;
18443 struct window *w;
18444 struct buffer *old_buffer = NULL;
18445 int face_id = -1;
18446 int no_props = INTEGERP (face);
18447 int count = SPECPDL_INDEX ();
18448 Lisp_Object str;
18449 int string_start = 0;
18450
18451 if (NILP (window))
18452 window = selected_window;
18453 CHECK_WINDOW (window);
18454 w = XWINDOW (window);
18455
18456 if (NILP (buffer))
18457 buffer = w->buffer;
18458 CHECK_BUFFER (buffer);
18459
18460 /* Make formatting the modeline a non-op when noninteractive, otherwise
18461 there will be problems later caused by a partially initialized frame. */
18462 if (NILP (format) || noninteractive)
18463 return empty_unibyte_string;
18464
18465 if (no_props)
18466 face = Qnil;
18467
18468 if (!NILP (face))
18469 {
18470 if (EQ (face, Qt))
18471 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18472 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18473 }
18474
18475 if (face_id < 0)
18476 face_id = DEFAULT_FACE_ID;
18477
18478 if (XBUFFER (buffer) != current_buffer)
18479 old_buffer = current_buffer;
18480
18481 /* Save things including mode_line_proptrans_alist,
18482 and set that to nil so that we don't alter the outer value. */
18483 record_unwind_protect (unwind_format_mode_line,
18484 format_mode_line_unwind_data
18485 (old_buffer, selected_window, 1));
18486 mode_line_proptrans_alist = Qnil;
18487
18488 Fselect_window (window, Qt);
18489 if (old_buffer)
18490 set_buffer_internal_1 (XBUFFER (buffer));
18491
18492 init_iterator (&it, w, -1, -1, NULL, face_id);
18493
18494 if (no_props)
18495 {
18496 mode_line_target = MODE_LINE_NOPROP;
18497 mode_line_string_face_prop = Qnil;
18498 mode_line_string_list = Qnil;
18499 string_start = MODE_LINE_NOPROP_LEN (0);
18500 }
18501 else
18502 {
18503 mode_line_target = MODE_LINE_STRING;
18504 mode_line_string_list = Qnil;
18505 mode_line_string_face = face;
18506 mode_line_string_face_prop
18507 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18508 }
18509
18510 push_kboard (FRAME_KBOARD (it.f));
18511 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18512 pop_kboard ();
18513
18514 if (no_props)
18515 {
18516 len = MODE_LINE_NOPROP_LEN (string_start);
18517 str = make_string (mode_line_noprop_buf + string_start, len);
18518 }
18519 else
18520 {
18521 mode_line_string_list = Fnreverse (mode_line_string_list);
18522 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18523 empty_unibyte_string);
18524 }
18525
18526 unbind_to (count, Qnil);
18527 return str;
18528 }
18529
18530 /* Write a null-terminated, right justified decimal representation of
18531 the positive integer D to BUF using a minimal field width WIDTH. */
18532
18533 static void
18534 pint2str (buf, width, d)
18535 register char *buf;
18536 register int width;
18537 register int d;
18538 {
18539 register char *p = buf;
18540
18541 if (d <= 0)
18542 *p++ = '0';
18543 else
18544 {
18545 while (d > 0)
18546 {
18547 *p++ = d % 10 + '0';
18548 d /= 10;
18549 }
18550 }
18551
18552 for (width -= (int) (p - buf); width > 0; --width)
18553 *p++ = ' ';
18554 *p-- = '\0';
18555 while (p > buf)
18556 {
18557 d = *buf;
18558 *buf++ = *p;
18559 *p-- = d;
18560 }
18561 }
18562
18563 /* Write a null-terminated, right justified decimal and "human
18564 readable" representation of the nonnegative integer D to BUF using
18565 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18566
18567 static const char power_letter[] =
18568 {
18569 0, /* not used */
18570 'k', /* kilo */
18571 'M', /* mega */
18572 'G', /* giga */
18573 'T', /* tera */
18574 'P', /* peta */
18575 'E', /* exa */
18576 'Z', /* zetta */
18577 'Y' /* yotta */
18578 };
18579
18580 static void
18581 pint2hrstr (buf, width, d)
18582 char *buf;
18583 int width;
18584 int d;
18585 {
18586 /* We aim to represent the nonnegative integer D as
18587 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18588 int quotient = d;
18589 int remainder = 0;
18590 /* -1 means: do not use TENTHS. */
18591 int tenths = -1;
18592 int exponent = 0;
18593
18594 /* Length of QUOTIENT.TENTHS as a string. */
18595 int length;
18596
18597 char * psuffix;
18598 char * p;
18599
18600 if (1000 <= quotient)
18601 {
18602 /* Scale to the appropriate EXPONENT. */
18603 do
18604 {
18605 remainder = quotient % 1000;
18606 quotient /= 1000;
18607 exponent++;
18608 }
18609 while (1000 <= quotient);
18610
18611 /* Round to nearest and decide whether to use TENTHS or not. */
18612 if (quotient <= 9)
18613 {
18614 tenths = remainder / 100;
18615 if (50 <= remainder % 100)
18616 {
18617 if (tenths < 9)
18618 tenths++;
18619 else
18620 {
18621 quotient++;
18622 if (quotient == 10)
18623 tenths = -1;
18624 else
18625 tenths = 0;
18626 }
18627 }
18628 }
18629 else
18630 if (500 <= remainder)
18631 {
18632 if (quotient < 999)
18633 quotient++;
18634 else
18635 {
18636 quotient = 1;
18637 exponent++;
18638 tenths = 0;
18639 }
18640 }
18641 }
18642
18643 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18644 if (tenths == -1 && quotient <= 99)
18645 if (quotient <= 9)
18646 length = 1;
18647 else
18648 length = 2;
18649 else
18650 length = 3;
18651 p = psuffix = buf + max (width, length);
18652
18653 /* Print EXPONENT. */
18654 if (exponent)
18655 *psuffix++ = power_letter[exponent];
18656 *psuffix = '\0';
18657
18658 /* Print TENTHS. */
18659 if (tenths >= 0)
18660 {
18661 *--p = '0' + tenths;
18662 *--p = '.';
18663 }
18664
18665 /* Print QUOTIENT. */
18666 do
18667 {
18668 int digit = quotient % 10;
18669 *--p = '0' + digit;
18670 }
18671 while ((quotient /= 10) != 0);
18672
18673 /* Print leading spaces. */
18674 while (buf < p)
18675 *--p = ' ';
18676 }
18677
18678 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18679 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18680 type of CODING_SYSTEM. Return updated pointer into BUF. */
18681
18682 static unsigned char invalid_eol_type[] = "(*invalid*)";
18683
18684 static char *
18685 decode_mode_spec_coding (coding_system, buf, eol_flag)
18686 Lisp_Object coding_system;
18687 register char *buf;
18688 int eol_flag;
18689 {
18690 Lisp_Object val;
18691 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18692 const unsigned char *eol_str;
18693 int eol_str_len;
18694 /* The EOL conversion we are using. */
18695 Lisp_Object eoltype;
18696
18697 val = CODING_SYSTEM_SPEC (coding_system);
18698 eoltype = Qnil;
18699
18700 if (!VECTORP (val)) /* Not yet decided. */
18701 {
18702 if (multibyte)
18703 *buf++ = '-';
18704 if (eol_flag)
18705 eoltype = eol_mnemonic_undecided;
18706 /* Don't mention EOL conversion if it isn't decided. */
18707 }
18708 else
18709 {
18710 Lisp_Object attrs;
18711 Lisp_Object eolvalue;
18712
18713 attrs = AREF (val, 0);
18714 eolvalue = AREF (val, 2);
18715
18716 if (multibyte)
18717 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18718
18719 if (eol_flag)
18720 {
18721 /* The EOL conversion that is normal on this system. */
18722
18723 if (NILP (eolvalue)) /* Not yet decided. */
18724 eoltype = eol_mnemonic_undecided;
18725 else if (VECTORP (eolvalue)) /* Not yet decided. */
18726 eoltype = eol_mnemonic_undecided;
18727 else /* eolvalue is Qunix, Qdos, or Qmac. */
18728 eoltype = (EQ (eolvalue, Qunix)
18729 ? eol_mnemonic_unix
18730 : (EQ (eolvalue, Qdos) == 1
18731 ? eol_mnemonic_dos : eol_mnemonic_mac));
18732 }
18733 }
18734
18735 if (eol_flag)
18736 {
18737 /* Mention the EOL conversion if it is not the usual one. */
18738 if (STRINGP (eoltype))
18739 {
18740 eol_str = SDATA (eoltype);
18741 eol_str_len = SBYTES (eoltype);
18742 }
18743 else if (CHARACTERP (eoltype))
18744 {
18745 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18746 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18747 eol_str = tmp;
18748 }
18749 else
18750 {
18751 eol_str = invalid_eol_type;
18752 eol_str_len = sizeof (invalid_eol_type) - 1;
18753 }
18754 bcopy (eol_str, buf, eol_str_len);
18755 buf += eol_str_len;
18756 }
18757
18758 return buf;
18759 }
18760
18761 /* Return a string for the output of a mode line %-spec for window W,
18762 generated by character C. PRECISION >= 0 means don't return a
18763 string longer than that value. FIELD_WIDTH > 0 means pad the
18764 string returned with spaces to that value. Return 1 in *MULTIBYTE
18765 if the result is multibyte text.
18766
18767 Note we operate on the current buffer for most purposes,
18768 the exception being w->base_line_pos. */
18769
18770 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18771
18772 static char *
18773 decode_mode_spec (w, c, field_width, precision, multibyte)
18774 struct window *w;
18775 register int c;
18776 int field_width, precision;
18777 int *multibyte;
18778 {
18779 Lisp_Object obj;
18780 struct frame *f = XFRAME (WINDOW_FRAME (w));
18781 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18782 struct buffer *b = current_buffer;
18783
18784 obj = Qnil;
18785 *multibyte = 0;
18786
18787 switch (c)
18788 {
18789 case '*':
18790 if (!NILP (b->read_only))
18791 return "%";
18792 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18793 return "*";
18794 return "-";
18795
18796 case '+':
18797 /* This differs from %* only for a modified read-only buffer. */
18798 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18799 return "*";
18800 if (!NILP (b->read_only))
18801 return "%";
18802 return "-";
18803
18804 case '&':
18805 /* This differs from %* in ignoring read-only-ness. */
18806 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18807 return "*";
18808 return "-";
18809
18810 case '%':
18811 return "%";
18812
18813 case '[':
18814 {
18815 int i;
18816 char *p;
18817
18818 if (command_loop_level > 5)
18819 return "[[[... ";
18820 p = decode_mode_spec_buf;
18821 for (i = 0; i < command_loop_level; i++)
18822 *p++ = '[';
18823 *p = 0;
18824 return decode_mode_spec_buf;
18825 }
18826
18827 case ']':
18828 {
18829 int i;
18830 char *p;
18831
18832 if (command_loop_level > 5)
18833 return " ...]]]";
18834 p = decode_mode_spec_buf;
18835 for (i = 0; i < command_loop_level; i++)
18836 *p++ = ']';
18837 *p = 0;
18838 return decode_mode_spec_buf;
18839 }
18840
18841 case '-':
18842 {
18843 register int i;
18844
18845 /* Let lots_of_dashes be a string of infinite length. */
18846 if (mode_line_target == MODE_LINE_NOPROP ||
18847 mode_line_target == MODE_LINE_STRING)
18848 return "--";
18849 if (field_width <= 0
18850 || field_width > sizeof (lots_of_dashes))
18851 {
18852 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18853 decode_mode_spec_buf[i] = '-';
18854 decode_mode_spec_buf[i] = '\0';
18855 return decode_mode_spec_buf;
18856 }
18857 else
18858 return lots_of_dashes;
18859 }
18860
18861 case 'b':
18862 obj = b->name;
18863 break;
18864
18865 case 'c':
18866 /* %c and %l are ignored in `frame-title-format'.
18867 (In redisplay_internal, the frame title is drawn _before_ the
18868 windows are updated, so the stuff which depends on actual
18869 window contents (such as %l) may fail to render properly, or
18870 even crash emacs.) */
18871 if (mode_line_target == MODE_LINE_TITLE)
18872 return "";
18873 else
18874 {
18875 int col = (int) current_column (); /* iftc */
18876 w->column_number_displayed = make_number (col);
18877 pint2str (decode_mode_spec_buf, field_width, col);
18878 return decode_mode_spec_buf;
18879 }
18880
18881 case 'e':
18882 #ifndef SYSTEM_MALLOC
18883 {
18884 if (NILP (Vmemory_full))
18885 return "";
18886 else
18887 return "!MEM FULL! ";
18888 }
18889 #else
18890 return "";
18891 #endif
18892
18893 case 'F':
18894 /* %F displays the frame name. */
18895 if (!NILP (f->title))
18896 return (char *) SDATA (f->title);
18897 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18898 return (char *) SDATA (f->name);
18899 return "Emacs";
18900
18901 case 'f':
18902 obj = b->filename;
18903 break;
18904
18905 case 'i':
18906 {
18907 int size = ZV - BEGV;
18908 pint2str (decode_mode_spec_buf, field_width, size);
18909 return decode_mode_spec_buf;
18910 }
18911
18912 case 'I':
18913 {
18914 int size = ZV - BEGV;
18915 pint2hrstr (decode_mode_spec_buf, field_width, size);
18916 return decode_mode_spec_buf;
18917 }
18918
18919 case 'l':
18920 {
18921 int startpos, startpos_byte, line, linepos, linepos_byte;
18922 int topline, nlines, junk, height;
18923
18924 /* %c and %l are ignored in `frame-title-format'. */
18925 if (mode_line_target == MODE_LINE_TITLE)
18926 return "";
18927
18928 startpos = XMARKER (w->start)->charpos;
18929 startpos_byte = marker_byte_position (w->start);
18930 height = WINDOW_TOTAL_LINES (w);
18931
18932 /* If we decided that this buffer isn't suitable for line numbers,
18933 don't forget that too fast. */
18934 if (EQ (w->base_line_pos, w->buffer))
18935 goto no_value;
18936 /* But do forget it, if the window shows a different buffer now. */
18937 else if (BUFFERP (w->base_line_pos))
18938 w->base_line_pos = Qnil;
18939
18940 /* If the buffer is very big, don't waste time. */
18941 if (INTEGERP (Vline_number_display_limit)
18942 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18943 {
18944 w->base_line_pos = Qnil;
18945 w->base_line_number = Qnil;
18946 goto no_value;
18947 }
18948
18949 if (INTEGERP (w->base_line_number)
18950 && INTEGERP (w->base_line_pos)
18951 && XFASTINT (w->base_line_pos) <= startpos)
18952 {
18953 line = XFASTINT (w->base_line_number);
18954 linepos = XFASTINT (w->base_line_pos);
18955 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18956 }
18957 else
18958 {
18959 line = 1;
18960 linepos = BUF_BEGV (b);
18961 linepos_byte = BUF_BEGV_BYTE (b);
18962 }
18963
18964 /* Count lines from base line to window start position. */
18965 nlines = display_count_lines (linepos, linepos_byte,
18966 startpos_byte,
18967 startpos, &junk);
18968
18969 topline = nlines + line;
18970
18971 /* Determine a new base line, if the old one is too close
18972 or too far away, or if we did not have one.
18973 "Too close" means it's plausible a scroll-down would
18974 go back past it. */
18975 if (startpos == BUF_BEGV (b))
18976 {
18977 w->base_line_number = make_number (topline);
18978 w->base_line_pos = make_number (BUF_BEGV (b));
18979 }
18980 else if (nlines < height + 25 || nlines > height * 3 + 50
18981 || linepos == BUF_BEGV (b))
18982 {
18983 int limit = BUF_BEGV (b);
18984 int limit_byte = BUF_BEGV_BYTE (b);
18985 int position;
18986 int distance = (height * 2 + 30) * line_number_display_limit_width;
18987
18988 if (startpos - distance > limit)
18989 {
18990 limit = startpos - distance;
18991 limit_byte = CHAR_TO_BYTE (limit);
18992 }
18993
18994 nlines = display_count_lines (startpos, startpos_byte,
18995 limit_byte,
18996 - (height * 2 + 30),
18997 &position);
18998 /* If we couldn't find the lines we wanted within
18999 line_number_display_limit_width chars per line,
19000 give up on line numbers for this window. */
19001 if (position == limit_byte && limit == startpos - distance)
19002 {
19003 w->base_line_pos = w->buffer;
19004 w->base_line_number = Qnil;
19005 goto no_value;
19006 }
19007
19008 w->base_line_number = make_number (topline - nlines);
19009 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19010 }
19011
19012 /* Now count lines from the start pos to point. */
19013 nlines = display_count_lines (startpos, startpos_byte,
19014 PT_BYTE, PT, &junk);
19015
19016 /* Record that we did display the line number. */
19017 line_number_displayed = 1;
19018
19019 /* Make the string to show. */
19020 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19021 return decode_mode_spec_buf;
19022 no_value:
19023 {
19024 char* p = decode_mode_spec_buf;
19025 int pad = field_width - 2;
19026 while (pad-- > 0)
19027 *p++ = ' ';
19028 *p++ = '?';
19029 *p++ = '?';
19030 *p = '\0';
19031 return decode_mode_spec_buf;
19032 }
19033 }
19034 break;
19035
19036 case 'm':
19037 obj = b->mode_name;
19038 break;
19039
19040 case 'n':
19041 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19042 return " Narrow";
19043 break;
19044
19045 case 'p':
19046 {
19047 int pos = marker_position (w->start);
19048 int total = BUF_ZV (b) - BUF_BEGV (b);
19049
19050 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19051 {
19052 if (pos <= BUF_BEGV (b))
19053 return "All";
19054 else
19055 return "Bottom";
19056 }
19057 else if (pos <= BUF_BEGV (b))
19058 return "Top";
19059 else
19060 {
19061 if (total > 1000000)
19062 /* Do it differently for a large value, to avoid overflow. */
19063 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19064 else
19065 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19066 /* We can't normally display a 3-digit number,
19067 so get us a 2-digit number that is close. */
19068 if (total == 100)
19069 total = 99;
19070 sprintf (decode_mode_spec_buf, "%2d%%", total);
19071 return decode_mode_spec_buf;
19072 }
19073 }
19074
19075 /* Display percentage of size above the bottom of the screen. */
19076 case 'P':
19077 {
19078 int toppos = marker_position (w->start);
19079 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19080 int total = BUF_ZV (b) - BUF_BEGV (b);
19081
19082 if (botpos >= BUF_ZV (b))
19083 {
19084 if (toppos <= BUF_BEGV (b))
19085 return "All";
19086 else
19087 return "Bottom";
19088 }
19089 else
19090 {
19091 if (total > 1000000)
19092 /* Do it differently for a large value, to avoid overflow. */
19093 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19094 else
19095 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19096 /* We can't normally display a 3-digit number,
19097 so get us a 2-digit number that is close. */
19098 if (total == 100)
19099 total = 99;
19100 if (toppos <= BUF_BEGV (b))
19101 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
19102 else
19103 sprintf (decode_mode_spec_buf, "%2d%%", total);
19104 return decode_mode_spec_buf;
19105 }
19106 }
19107
19108 case 's':
19109 /* status of process */
19110 obj = Fget_buffer_process (Fcurrent_buffer ());
19111 if (NILP (obj))
19112 return "no process";
19113 #ifdef subprocesses
19114 obj = Fsymbol_name (Fprocess_status (obj));
19115 #endif
19116 break;
19117
19118 case '@':
19119 {
19120 int count = inhibit_garbage_collection ();
19121 Lisp_Object val = call1 (intern ("file-remote-p"),
19122 current_buffer->directory);
19123 unbind_to (count, Qnil);
19124
19125 if (NILP (val))
19126 return "-";
19127 else
19128 return "@";
19129 }
19130
19131 case 't': /* indicate TEXT or BINARY */
19132 #ifdef MODE_LINE_BINARY_TEXT
19133 return MODE_LINE_BINARY_TEXT (b);
19134 #else
19135 return "T";
19136 #endif
19137
19138 case 'z':
19139 /* coding-system (not including end-of-line format) */
19140 case 'Z':
19141 /* coding-system (including end-of-line type) */
19142 {
19143 int eol_flag = (c == 'Z');
19144 char *p = decode_mode_spec_buf;
19145
19146 if (! FRAME_WINDOW_P (f))
19147 {
19148 /* No need to mention EOL here--the terminal never needs
19149 to do EOL conversion. */
19150 p = decode_mode_spec_coding (CODING_ID_NAME
19151 (FRAME_KEYBOARD_CODING (f)->id),
19152 p, 0);
19153 p = decode_mode_spec_coding (CODING_ID_NAME
19154 (FRAME_TERMINAL_CODING (f)->id),
19155 p, 0);
19156 }
19157 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19158 p, eol_flag);
19159
19160 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19161 #ifdef subprocesses
19162 obj = Fget_buffer_process (Fcurrent_buffer ());
19163 if (PROCESSP (obj))
19164 {
19165 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19166 p, eol_flag);
19167 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19168 p, eol_flag);
19169 }
19170 #endif /* subprocesses */
19171 #endif /* 0 */
19172 *p = 0;
19173 return decode_mode_spec_buf;
19174 }
19175 }
19176
19177 if (STRINGP (obj))
19178 {
19179 *multibyte = STRING_MULTIBYTE (obj);
19180 return (char *) SDATA (obj);
19181 }
19182 else
19183 return "";
19184 }
19185
19186
19187 /* Count up to COUNT lines starting from START / START_BYTE.
19188 But don't go beyond LIMIT_BYTE.
19189 Return the number of lines thus found (always nonnegative).
19190
19191 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19192
19193 static int
19194 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
19195 int start, start_byte, limit_byte, count;
19196 int *byte_pos_ptr;
19197 {
19198 register unsigned char *cursor;
19199 unsigned char *base;
19200
19201 register int ceiling;
19202 register unsigned char *ceiling_addr;
19203 int orig_count = count;
19204
19205 /* If we are not in selective display mode,
19206 check only for newlines. */
19207 int selective_display = (!NILP (current_buffer->selective_display)
19208 && !INTEGERP (current_buffer->selective_display));
19209
19210 if (count > 0)
19211 {
19212 while (start_byte < limit_byte)
19213 {
19214 ceiling = BUFFER_CEILING_OF (start_byte);
19215 ceiling = min (limit_byte - 1, ceiling);
19216 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19217 base = (cursor = BYTE_POS_ADDR (start_byte));
19218 while (1)
19219 {
19220 if (selective_display)
19221 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19222 ;
19223 else
19224 while (*cursor != '\n' && ++cursor != ceiling_addr)
19225 ;
19226
19227 if (cursor != ceiling_addr)
19228 {
19229 if (--count == 0)
19230 {
19231 start_byte += cursor - base + 1;
19232 *byte_pos_ptr = start_byte;
19233 return orig_count;
19234 }
19235 else
19236 if (++cursor == ceiling_addr)
19237 break;
19238 }
19239 else
19240 break;
19241 }
19242 start_byte += cursor - base;
19243 }
19244 }
19245 else
19246 {
19247 while (start_byte > limit_byte)
19248 {
19249 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19250 ceiling = max (limit_byte, ceiling);
19251 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19252 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19253 while (1)
19254 {
19255 if (selective_display)
19256 while (--cursor != ceiling_addr
19257 && *cursor != '\n' && *cursor != 015)
19258 ;
19259 else
19260 while (--cursor != ceiling_addr && *cursor != '\n')
19261 ;
19262
19263 if (cursor != ceiling_addr)
19264 {
19265 if (++count == 0)
19266 {
19267 start_byte += cursor - base + 1;
19268 *byte_pos_ptr = start_byte;
19269 /* When scanning backwards, we should
19270 not count the newline posterior to which we stop. */
19271 return - orig_count - 1;
19272 }
19273 }
19274 else
19275 break;
19276 }
19277 /* Here we add 1 to compensate for the last decrement
19278 of CURSOR, which took it past the valid range. */
19279 start_byte += cursor - base + 1;
19280 }
19281 }
19282
19283 *byte_pos_ptr = limit_byte;
19284
19285 if (count < 0)
19286 return - orig_count + count;
19287 return orig_count - count;
19288
19289 }
19290
19291
19292 \f
19293 /***********************************************************************
19294 Displaying strings
19295 ***********************************************************************/
19296
19297 /* Display a NUL-terminated string, starting with index START.
19298
19299 If STRING is non-null, display that C string. Otherwise, the Lisp
19300 string LISP_STRING is displayed.
19301
19302 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19303 FACE_STRING. Display STRING or LISP_STRING with the face at
19304 FACE_STRING_POS in FACE_STRING:
19305
19306 Display the string in the environment given by IT, but use the
19307 standard display table, temporarily.
19308
19309 FIELD_WIDTH is the minimum number of output glyphs to produce.
19310 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19311 with spaces. If STRING has more characters, more than FIELD_WIDTH
19312 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19313
19314 PRECISION is the maximum number of characters to output from
19315 STRING. PRECISION < 0 means don't truncate the string.
19316
19317 This is roughly equivalent to printf format specifiers:
19318
19319 FIELD_WIDTH PRECISION PRINTF
19320 ----------------------------------------
19321 -1 -1 %s
19322 -1 10 %.10s
19323 10 -1 %10s
19324 20 10 %20.10s
19325
19326 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19327 display them, and < 0 means obey the current buffer's value of
19328 enable_multibyte_characters.
19329
19330 Value is the number of columns displayed. */
19331
19332 static int
19333 display_string (string, lisp_string, face_string, face_string_pos,
19334 start, it, field_width, precision, max_x, multibyte)
19335 unsigned char *string;
19336 Lisp_Object lisp_string;
19337 Lisp_Object face_string;
19338 EMACS_INT face_string_pos;
19339 EMACS_INT start;
19340 struct it *it;
19341 int field_width, precision, max_x;
19342 int multibyte;
19343 {
19344 int hpos_at_start = it->hpos;
19345 int saved_face_id = it->face_id;
19346 struct glyph_row *row = it->glyph_row;
19347
19348 /* Initialize the iterator IT for iteration over STRING beginning
19349 with index START. */
19350 reseat_to_string (it, string, lisp_string, start,
19351 precision, field_width, multibyte);
19352
19353 /* If displaying STRING, set up the face of the iterator
19354 from LISP_STRING, if that's given. */
19355 if (STRINGP (face_string))
19356 {
19357 EMACS_INT endptr;
19358 struct face *face;
19359
19360 it->face_id
19361 = face_at_string_position (it->w, face_string, face_string_pos,
19362 0, it->region_beg_charpos,
19363 it->region_end_charpos,
19364 &endptr, it->base_face_id, 0);
19365 face = FACE_FROM_ID (it->f, it->face_id);
19366 it->face_box_p = face->box != FACE_NO_BOX;
19367 }
19368
19369 /* Set max_x to the maximum allowed X position. Don't let it go
19370 beyond the right edge of the window. */
19371 if (max_x <= 0)
19372 max_x = it->last_visible_x;
19373 else
19374 max_x = min (max_x, it->last_visible_x);
19375
19376 /* Skip over display elements that are not visible. because IT->w is
19377 hscrolled. */
19378 if (it->current_x < it->first_visible_x)
19379 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19380 MOVE_TO_POS | MOVE_TO_X);
19381
19382 row->ascent = it->max_ascent;
19383 row->height = it->max_ascent + it->max_descent;
19384 row->phys_ascent = it->max_phys_ascent;
19385 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19386 row->extra_line_spacing = it->max_extra_line_spacing;
19387
19388 /* This condition is for the case that we are called with current_x
19389 past last_visible_x. */
19390 while (it->current_x < max_x)
19391 {
19392 int x_before, x, n_glyphs_before, i, nglyphs;
19393
19394 /* Get the next display element. */
19395 if (!get_next_display_element (it))
19396 break;
19397
19398 /* Produce glyphs. */
19399 x_before = it->current_x;
19400 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19401 PRODUCE_GLYPHS (it);
19402
19403 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19404 i = 0;
19405 x = x_before;
19406 while (i < nglyphs)
19407 {
19408 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19409
19410 if (it->line_wrap != TRUNCATE
19411 && x + glyph->pixel_width > max_x)
19412 {
19413 /* End of continued line or max_x reached. */
19414 if (CHAR_GLYPH_PADDING_P (*glyph))
19415 {
19416 /* A wide character is unbreakable. */
19417 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19418 it->current_x = x_before;
19419 }
19420 else
19421 {
19422 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19423 it->current_x = x;
19424 }
19425 break;
19426 }
19427 else if (x + glyph->pixel_width >= it->first_visible_x)
19428 {
19429 /* Glyph is at least partially visible. */
19430 ++it->hpos;
19431 if (x < it->first_visible_x)
19432 it->glyph_row->x = x - it->first_visible_x;
19433 }
19434 else
19435 {
19436 /* Glyph is off the left margin of the display area.
19437 Should not happen. */
19438 abort ();
19439 }
19440
19441 row->ascent = max (row->ascent, it->max_ascent);
19442 row->height = max (row->height, it->max_ascent + it->max_descent);
19443 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19444 row->phys_height = max (row->phys_height,
19445 it->max_phys_ascent + it->max_phys_descent);
19446 row->extra_line_spacing = max (row->extra_line_spacing,
19447 it->max_extra_line_spacing);
19448 x += glyph->pixel_width;
19449 ++i;
19450 }
19451
19452 /* Stop if max_x reached. */
19453 if (i < nglyphs)
19454 break;
19455
19456 /* Stop at line ends. */
19457 if (ITERATOR_AT_END_OF_LINE_P (it))
19458 {
19459 it->continuation_lines_width = 0;
19460 break;
19461 }
19462
19463 set_iterator_to_next (it, 1);
19464
19465 /* Stop if truncating at the right edge. */
19466 if (it->line_wrap == TRUNCATE
19467 && it->current_x >= it->last_visible_x)
19468 {
19469 /* Add truncation mark, but don't do it if the line is
19470 truncated at a padding space. */
19471 if (IT_CHARPOS (*it) < it->string_nchars)
19472 {
19473 if (!FRAME_WINDOW_P (it->f))
19474 {
19475 int i, n;
19476
19477 if (it->current_x > it->last_visible_x)
19478 {
19479 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19480 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19481 break;
19482 for (n = row->used[TEXT_AREA]; i < n; ++i)
19483 {
19484 row->used[TEXT_AREA] = i;
19485 produce_special_glyphs (it, IT_TRUNCATION);
19486 }
19487 }
19488 produce_special_glyphs (it, IT_TRUNCATION);
19489 }
19490 it->glyph_row->truncated_on_right_p = 1;
19491 }
19492 break;
19493 }
19494 }
19495
19496 /* Maybe insert a truncation at the left. */
19497 if (it->first_visible_x
19498 && IT_CHARPOS (*it) > 0)
19499 {
19500 if (!FRAME_WINDOW_P (it->f))
19501 insert_left_trunc_glyphs (it);
19502 it->glyph_row->truncated_on_left_p = 1;
19503 }
19504
19505 it->face_id = saved_face_id;
19506
19507 /* Value is number of columns displayed. */
19508 return it->hpos - hpos_at_start;
19509 }
19510
19511
19512 \f
19513 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19514 appears as an element of LIST or as the car of an element of LIST.
19515 If PROPVAL is a list, compare each element against LIST in that
19516 way, and return 1/2 if any element of PROPVAL is found in LIST.
19517 Otherwise return 0. This function cannot quit.
19518 The return value is 2 if the text is invisible but with an ellipsis
19519 and 1 if it's invisible and without an ellipsis. */
19520
19521 int
19522 invisible_p (propval, list)
19523 register Lisp_Object propval;
19524 Lisp_Object list;
19525 {
19526 register Lisp_Object tail, proptail;
19527
19528 for (tail = list; CONSP (tail); tail = XCDR (tail))
19529 {
19530 register Lisp_Object tem;
19531 tem = XCAR (tail);
19532 if (EQ (propval, tem))
19533 return 1;
19534 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19535 return NILP (XCDR (tem)) ? 1 : 2;
19536 }
19537
19538 if (CONSP (propval))
19539 {
19540 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19541 {
19542 Lisp_Object propelt;
19543 propelt = XCAR (proptail);
19544 for (tail = list; CONSP (tail); tail = XCDR (tail))
19545 {
19546 register Lisp_Object tem;
19547 tem = XCAR (tail);
19548 if (EQ (propelt, tem))
19549 return 1;
19550 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19551 return NILP (XCDR (tem)) ? 1 : 2;
19552 }
19553 }
19554 }
19555
19556 return 0;
19557 }
19558
19559 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19560 doc: /* Non-nil if the property makes the text invisible.
19561 POS-OR-PROP can be a marker or number, in which case it is taken to be
19562 a position in the current buffer and the value of the `invisible' property
19563 is checked; or it can be some other value, which is then presumed to be the
19564 value of the `invisible' property of the text of interest.
19565 The non-nil value returned can be t for truly invisible text or something
19566 else if the text is replaced by an ellipsis. */)
19567 (pos_or_prop)
19568 Lisp_Object pos_or_prop;
19569 {
19570 Lisp_Object prop
19571 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19572 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19573 : pos_or_prop);
19574 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19575 return (invis == 0 ? Qnil
19576 : invis == 1 ? Qt
19577 : make_number (invis));
19578 }
19579
19580 /* Calculate a width or height in pixels from a specification using
19581 the following elements:
19582
19583 SPEC ::=
19584 NUM - a (fractional) multiple of the default font width/height
19585 (NUM) - specifies exactly NUM pixels
19586 UNIT - a fixed number of pixels, see below.
19587 ELEMENT - size of a display element in pixels, see below.
19588 (NUM . SPEC) - equals NUM * SPEC
19589 (+ SPEC SPEC ...) - add pixel values
19590 (- SPEC SPEC ...) - subtract pixel values
19591 (- SPEC) - negate pixel value
19592
19593 NUM ::=
19594 INT or FLOAT - a number constant
19595 SYMBOL - use symbol's (buffer local) variable binding.
19596
19597 UNIT ::=
19598 in - pixels per inch *)
19599 mm - pixels per 1/1000 meter *)
19600 cm - pixels per 1/100 meter *)
19601 width - width of current font in pixels.
19602 height - height of current font in pixels.
19603
19604 *) using the ratio(s) defined in display-pixels-per-inch.
19605
19606 ELEMENT ::=
19607
19608 left-fringe - left fringe width in pixels
19609 right-fringe - right fringe width in pixels
19610
19611 left-margin - left margin width in pixels
19612 right-margin - right margin width in pixels
19613
19614 scroll-bar - scroll-bar area width in pixels
19615
19616 Examples:
19617
19618 Pixels corresponding to 5 inches:
19619 (5 . in)
19620
19621 Total width of non-text areas on left side of window (if scroll-bar is on left):
19622 '(space :width (+ left-fringe left-margin scroll-bar))
19623
19624 Align to first text column (in header line):
19625 '(space :align-to 0)
19626
19627 Align to middle of text area minus half the width of variable `my-image'
19628 containing a loaded image:
19629 '(space :align-to (0.5 . (- text my-image)))
19630
19631 Width of left margin minus width of 1 character in the default font:
19632 '(space :width (- left-margin 1))
19633
19634 Width of left margin minus width of 2 characters in the current font:
19635 '(space :width (- left-margin (2 . width)))
19636
19637 Center 1 character over left-margin (in header line):
19638 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19639
19640 Different ways to express width of left fringe plus left margin minus one pixel:
19641 '(space :width (- (+ left-fringe left-margin) (1)))
19642 '(space :width (+ left-fringe left-margin (- (1))))
19643 '(space :width (+ left-fringe left-margin (-1)))
19644
19645 */
19646
19647 #define NUMVAL(X) \
19648 ((INTEGERP (X) || FLOATP (X)) \
19649 ? XFLOATINT (X) \
19650 : - 1)
19651
19652 int
19653 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19654 double *res;
19655 struct it *it;
19656 Lisp_Object prop;
19657 struct font *font;
19658 int width_p, *align_to;
19659 {
19660 double pixels;
19661
19662 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19663 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19664
19665 if (NILP (prop))
19666 return OK_PIXELS (0);
19667
19668 xassert (FRAME_LIVE_P (it->f));
19669
19670 if (SYMBOLP (prop))
19671 {
19672 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19673 {
19674 char *unit = SDATA (SYMBOL_NAME (prop));
19675
19676 if (unit[0] == 'i' && unit[1] == 'n')
19677 pixels = 1.0;
19678 else if (unit[0] == 'm' && unit[1] == 'm')
19679 pixels = 25.4;
19680 else if (unit[0] == 'c' && unit[1] == 'm')
19681 pixels = 2.54;
19682 else
19683 pixels = 0;
19684 if (pixels > 0)
19685 {
19686 double ppi;
19687 #ifdef HAVE_WINDOW_SYSTEM
19688 if (FRAME_WINDOW_P (it->f)
19689 && (ppi = (width_p
19690 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19691 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19692 ppi > 0))
19693 return OK_PIXELS (ppi / pixels);
19694 #endif
19695
19696 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19697 || (CONSP (Vdisplay_pixels_per_inch)
19698 && (ppi = (width_p
19699 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19700 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19701 ppi > 0)))
19702 return OK_PIXELS (ppi / pixels);
19703
19704 return 0;
19705 }
19706 }
19707
19708 #ifdef HAVE_WINDOW_SYSTEM
19709 if (EQ (prop, Qheight))
19710 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19711 if (EQ (prop, Qwidth))
19712 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19713 #else
19714 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19715 return OK_PIXELS (1);
19716 #endif
19717
19718 if (EQ (prop, Qtext))
19719 return OK_PIXELS (width_p
19720 ? window_box_width (it->w, TEXT_AREA)
19721 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19722
19723 if (align_to && *align_to < 0)
19724 {
19725 *res = 0;
19726 if (EQ (prop, Qleft))
19727 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19728 if (EQ (prop, Qright))
19729 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19730 if (EQ (prop, Qcenter))
19731 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19732 + window_box_width (it->w, TEXT_AREA) / 2);
19733 if (EQ (prop, Qleft_fringe))
19734 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19735 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19736 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19737 if (EQ (prop, Qright_fringe))
19738 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19739 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19740 : window_box_right_offset (it->w, TEXT_AREA));
19741 if (EQ (prop, Qleft_margin))
19742 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19743 if (EQ (prop, Qright_margin))
19744 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19745 if (EQ (prop, Qscroll_bar))
19746 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19747 ? 0
19748 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19749 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19750 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19751 : 0)));
19752 }
19753 else
19754 {
19755 if (EQ (prop, Qleft_fringe))
19756 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19757 if (EQ (prop, Qright_fringe))
19758 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19759 if (EQ (prop, Qleft_margin))
19760 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19761 if (EQ (prop, Qright_margin))
19762 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19763 if (EQ (prop, Qscroll_bar))
19764 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19765 }
19766
19767 prop = Fbuffer_local_value (prop, it->w->buffer);
19768 }
19769
19770 if (INTEGERP (prop) || FLOATP (prop))
19771 {
19772 int base_unit = (width_p
19773 ? FRAME_COLUMN_WIDTH (it->f)
19774 : FRAME_LINE_HEIGHT (it->f));
19775 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19776 }
19777
19778 if (CONSP (prop))
19779 {
19780 Lisp_Object car = XCAR (prop);
19781 Lisp_Object cdr = XCDR (prop);
19782
19783 if (SYMBOLP (car))
19784 {
19785 #ifdef HAVE_WINDOW_SYSTEM
19786 if (FRAME_WINDOW_P (it->f)
19787 && valid_image_p (prop))
19788 {
19789 int id = lookup_image (it->f, prop);
19790 struct image *img = IMAGE_FROM_ID (it->f, id);
19791
19792 return OK_PIXELS (width_p ? img->width : img->height);
19793 }
19794 #endif
19795 if (EQ (car, Qplus) || EQ (car, Qminus))
19796 {
19797 int first = 1;
19798 double px;
19799
19800 pixels = 0;
19801 while (CONSP (cdr))
19802 {
19803 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19804 font, width_p, align_to))
19805 return 0;
19806 if (first)
19807 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19808 else
19809 pixels += px;
19810 cdr = XCDR (cdr);
19811 }
19812 if (EQ (car, Qminus))
19813 pixels = -pixels;
19814 return OK_PIXELS (pixels);
19815 }
19816
19817 car = Fbuffer_local_value (car, it->w->buffer);
19818 }
19819
19820 if (INTEGERP (car) || FLOATP (car))
19821 {
19822 double fact;
19823 pixels = XFLOATINT (car);
19824 if (NILP (cdr))
19825 return OK_PIXELS (pixels);
19826 if (calc_pixel_width_or_height (&fact, it, cdr,
19827 font, width_p, align_to))
19828 return OK_PIXELS (pixels * fact);
19829 return 0;
19830 }
19831
19832 return 0;
19833 }
19834
19835 return 0;
19836 }
19837
19838 \f
19839 /***********************************************************************
19840 Glyph Display
19841 ***********************************************************************/
19842
19843 #ifdef HAVE_WINDOW_SYSTEM
19844
19845 #if GLYPH_DEBUG
19846
19847 void
19848 dump_glyph_string (s)
19849 struct glyph_string *s;
19850 {
19851 fprintf (stderr, "glyph string\n");
19852 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19853 s->x, s->y, s->width, s->height);
19854 fprintf (stderr, " ybase = %d\n", s->ybase);
19855 fprintf (stderr, " hl = %d\n", s->hl);
19856 fprintf (stderr, " left overhang = %d, right = %d\n",
19857 s->left_overhang, s->right_overhang);
19858 fprintf (stderr, " nchars = %d\n", s->nchars);
19859 fprintf (stderr, " extends to end of line = %d\n",
19860 s->extends_to_end_of_line_p);
19861 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19862 fprintf (stderr, " bg width = %d\n", s->background_width);
19863 }
19864
19865 #endif /* GLYPH_DEBUG */
19866
19867 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19868 of XChar2b structures for S; it can't be allocated in
19869 init_glyph_string because it must be allocated via `alloca'. W
19870 is the window on which S is drawn. ROW and AREA are the glyph row
19871 and area within the row from which S is constructed. START is the
19872 index of the first glyph structure covered by S. HL is a
19873 face-override for drawing S. */
19874
19875 #ifdef HAVE_NTGUI
19876 #define OPTIONAL_HDC(hdc) hdc,
19877 #define DECLARE_HDC(hdc) HDC hdc;
19878 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19879 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19880 #endif
19881
19882 #ifndef OPTIONAL_HDC
19883 #define OPTIONAL_HDC(hdc)
19884 #define DECLARE_HDC(hdc)
19885 #define ALLOCATE_HDC(hdc, f)
19886 #define RELEASE_HDC(hdc, f)
19887 #endif
19888
19889 static void
19890 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19891 struct glyph_string *s;
19892 DECLARE_HDC (hdc)
19893 XChar2b *char2b;
19894 struct window *w;
19895 struct glyph_row *row;
19896 enum glyph_row_area area;
19897 int start;
19898 enum draw_glyphs_face hl;
19899 {
19900 bzero (s, sizeof *s);
19901 s->w = w;
19902 s->f = XFRAME (w->frame);
19903 #ifdef HAVE_NTGUI
19904 s->hdc = hdc;
19905 #endif
19906 s->display = FRAME_X_DISPLAY (s->f);
19907 s->window = FRAME_X_WINDOW (s->f);
19908 s->char2b = char2b;
19909 s->hl = hl;
19910 s->row = row;
19911 s->area = area;
19912 s->first_glyph = row->glyphs[area] + start;
19913 s->height = row->height;
19914 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19915
19916 /* Display the internal border below the tool-bar window. */
19917 if (WINDOWP (s->f->tool_bar_window)
19918 && s->w == XWINDOW (s->f->tool_bar_window))
19919 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19920
19921 s->ybase = s->y + row->ascent;
19922 }
19923
19924
19925 /* Append the list of glyph strings with head H and tail T to the list
19926 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19927
19928 static INLINE void
19929 append_glyph_string_lists (head, tail, h, t)
19930 struct glyph_string **head, **tail;
19931 struct glyph_string *h, *t;
19932 {
19933 if (h)
19934 {
19935 if (*head)
19936 (*tail)->next = h;
19937 else
19938 *head = h;
19939 h->prev = *tail;
19940 *tail = t;
19941 }
19942 }
19943
19944
19945 /* Prepend the list of glyph strings with head H and tail T to the
19946 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19947 result. */
19948
19949 static INLINE void
19950 prepend_glyph_string_lists (head, tail, h, t)
19951 struct glyph_string **head, **tail;
19952 struct glyph_string *h, *t;
19953 {
19954 if (h)
19955 {
19956 if (*head)
19957 (*head)->prev = t;
19958 else
19959 *tail = t;
19960 t->next = *head;
19961 *head = h;
19962 }
19963 }
19964
19965
19966 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19967 Set *HEAD and *TAIL to the resulting list. */
19968
19969 static INLINE void
19970 append_glyph_string (head, tail, s)
19971 struct glyph_string **head, **tail;
19972 struct glyph_string *s;
19973 {
19974 s->next = s->prev = NULL;
19975 append_glyph_string_lists (head, tail, s, s);
19976 }
19977
19978
19979 /* Get face and two-byte form of character C in face FACE_ID on frame
19980 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19981 means we want to display multibyte text. DISPLAY_P non-zero means
19982 make sure that X resources for the face returned are allocated.
19983 Value is a pointer to a realized face that is ready for display if
19984 DISPLAY_P is non-zero. */
19985
19986 static INLINE struct face *
19987 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19988 struct frame *f;
19989 int c, face_id;
19990 XChar2b *char2b;
19991 int multibyte_p, display_p;
19992 {
19993 struct face *face = FACE_FROM_ID (f, face_id);
19994
19995 if (face->font)
19996 {
19997 unsigned code = face->font->driver->encode_char (face->font, c);
19998
19999 if (code != FONT_INVALID_CODE)
20000 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20001 else
20002 STORE_XCHAR2B (char2b, 0, 0);
20003 }
20004
20005 /* Make sure X resources of the face are allocated. */
20006 #ifdef HAVE_X_WINDOWS
20007 if (display_p)
20008 #endif
20009 {
20010 xassert (face != NULL);
20011 PREPARE_FACE_FOR_DISPLAY (f, face);
20012 }
20013
20014 return face;
20015 }
20016
20017
20018 /* Get face and two-byte form of character glyph GLYPH on frame F.
20019 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20020 a pointer to a realized face that is ready for display. */
20021
20022 static INLINE struct face *
20023 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
20024 struct frame *f;
20025 struct glyph *glyph;
20026 XChar2b *char2b;
20027 int *two_byte_p;
20028 {
20029 struct face *face;
20030
20031 xassert (glyph->type == CHAR_GLYPH);
20032 face = FACE_FROM_ID (f, glyph->face_id);
20033
20034 if (two_byte_p)
20035 *two_byte_p = 0;
20036
20037 if (face->font)
20038 {
20039 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
20040
20041 if (code != FONT_INVALID_CODE)
20042 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20043 else
20044 STORE_XCHAR2B (char2b, 0, 0);
20045 }
20046
20047 /* Make sure X resources of the face are allocated. */
20048 xassert (face != NULL);
20049 PREPARE_FACE_FOR_DISPLAY (f, face);
20050 return face;
20051 }
20052
20053
20054 /* Fill glyph string S with composition components specified by S->cmp.
20055
20056 BASE_FACE is the base face of the composition.
20057 S->cmp_from is the index of the first component for S.
20058
20059 OVERLAPS non-zero means S should draw the foreground only, and use
20060 its physical height for clipping. See also draw_glyphs.
20061
20062 Value is the index of a component not in S. */
20063
20064 static int
20065 fill_composite_glyph_string (s, base_face, overlaps)
20066 struct glyph_string *s;
20067 struct face *base_face;
20068 int overlaps;
20069 {
20070 int i;
20071 /* For all glyphs of this composition, starting at the offset
20072 S->cmp_from, until we reach the end of the definition or encounter a
20073 glyph that requires the different face, add it to S. */
20074 struct face *face;
20075
20076 xassert (s);
20077
20078 s->for_overlaps = overlaps;
20079 s->face = NULL;
20080 s->font = NULL;
20081 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20082 {
20083 int c = COMPOSITION_GLYPH (s->cmp, i);
20084
20085 if (c != '\t')
20086 {
20087 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20088 -1, Qnil);
20089
20090 face = get_char_face_and_encoding (s->f, c, face_id,
20091 s->char2b + i, 1, 1);
20092 if (face)
20093 {
20094 if (! s->face)
20095 {
20096 s->face = face;
20097 s->font = s->face->font;
20098 }
20099 else if (s->face != face)
20100 break;
20101 }
20102 }
20103 ++s->nchars;
20104 }
20105 s->cmp_to = i;
20106
20107 /* All glyph strings for the same composition has the same width,
20108 i.e. the width set for the first component of the composition. */
20109 s->width = s->first_glyph->pixel_width;
20110
20111 /* If the specified font could not be loaded, use the frame's
20112 default font, but record the fact that we couldn't load it in
20113 the glyph string so that we can draw rectangles for the
20114 characters of the glyph string. */
20115 if (s->font == NULL)
20116 {
20117 s->font_not_found_p = 1;
20118 s->font = FRAME_FONT (s->f);
20119 }
20120
20121 /* Adjust base line for subscript/superscript text. */
20122 s->ybase += s->first_glyph->voffset;
20123
20124 /* This glyph string must always be drawn with 16-bit functions. */
20125 s->two_byte_p = 1;
20126
20127 return s->cmp_to;
20128 }
20129
20130 static int
20131 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
20132 struct glyph_string *s;
20133 int face_id;
20134 int start, end, overlaps;
20135 {
20136 struct glyph *glyph, *last;
20137 Lisp_Object lgstring;
20138 int i;
20139
20140 s->for_overlaps = overlaps;
20141 glyph = s->row->glyphs[s->area] + start;
20142 last = s->row->glyphs[s->area] + end;
20143 s->cmp_id = glyph->u.cmp.id;
20144 s->cmp_from = glyph->u.cmp.from;
20145 s->cmp_to = glyph->u.cmp.to + 1;
20146 s->face = FACE_FROM_ID (s->f, face_id);
20147 lgstring = composition_gstring_from_id (s->cmp_id);
20148 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20149 glyph++;
20150 while (glyph < last
20151 && glyph->u.cmp.automatic
20152 && glyph->u.cmp.id == s->cmp_id
20153 && s->cmp_to == glyph->u.cmp.from)
20154 s->cmp_to = (glyph++)->u.cmp.to + 1;
20155
20156 for (i = s->cmp_from; i < s->cmp_to; i++)
20157 {
20158 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20159 unsigned code = LGLYPH_CODE (lglyph);
20160
20161 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20162 }
20163 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20164 return glyph - s->row->glyphs[s->area];
20165 }
20166
20167
20168 /* Fill glyph string S from a sequence of character glyphs.
20169
20170 FACE_ID is the face id of the string. START is the index of the
20171 first glyph to consider, END is the index of the last + 1.
20172 OVERLAPS non-zero means S should draw the foreground only, and use
20173 its physical height for clipping. See also draw_glyphs.
20174
20175 Value is the index of the first glyph not in S. */
20176
20177 static int
20178 fill_glyph_string (s, face_id, start, end, overlaps)
20179 struct glyph_string *s;
20180 int face_id;
20181 int start, end, overlaps;
20182 {
20183 struct glyph *glyph, *last;
20184 int voffset;
20185 int glyph_not_available_p;
20186
20187 xassert (s->f == XFRAME (s->w->frame));
20188 xassert (s->nchars == 0);
20189 xassert (start >= 0 && end > start);
20190
20191 s->for_overlaps = overlaps;
20192 glyph = s->row->glyphs[s->area] + start;
20193 last = s->row->glyphs[s->area] + end;
20194 voffset = glyph->voffset;
20195 s->padding_p = glyph->padding_p;
20196 glyph_not_available_p = glyph->glyph_not_available_p;
20197
20198 while (glyph < last
20199 && glyph->type == CHAR_GLYPH
20200 && glyph->voffset == voffset
20201 /* Same face id implies same font, nowadays. */
20202 && glyph->face_id == face_id
20203 && glyph->glyph_not_available_p == glyph_not_available_p)
20204 {
20205 int two_byte_p;
20206
20207 s->face = get_glyph_face_and_encoding (s->f, glyph,
20208 s->char2b + s->nchars,
20209 &two_byte_p);
20210 s->two_byte_p = two_byte_p;
20211 ++s->nchars;
20212 xassert (s->nchars <= end - start);
20213 s->width += glyph->pixel_width;
20214 if (glyph++->padding_p != s->padding_p)
20215 break;
20216 }
20217
20218 s->font = s->face->font;
20219
20220 /* If the specified font could not be loaded, use the frame's font,
20221 but record the fact that we couldn't load it in
20222 S->font_not_found_p so that we can draw rectangles for the
20223 characters of the glyph string. */
20224 if (s->font == NULL || glyph_not_available_p)
20225 {
20226 s->font_not_found_p = 1;
20227 s->font = FRAME_FONT (s->f);
20228 }
20229
20230 /* Adjust base line for subscript/superscript text. */
20231 s->ybase += voffset;
20232
20233 xassert (s->face && s->face->gc);
20234 return glyph - s->row->glyphs[s->area];
20235 }
20236
20237
20238 /* Fill glyph string S from image glyph S->first_glyph. */
20239
20240 static void
20241 fill_image_glyph_string (s)
20242 struct glyph_string *s;
20243 {
20244 xassert (s->first_glyph->type == IMAGE_GLYPH);
20245 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20246 xassert (s->img);
20247 s->slice = s->first_glyph->slice;
20248 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20249 s->font = s->face->font;
20250 s->width = s->first_glyph->pixel_width;
20251
20252 /* Adjust base line for subscript/superscript text. */
20253 s->ybase += s->first_glyph->voffset;
20254 }
20255
20256
20257 /* Fill glyph string S from a sequence of stretch glyphs.
20258
20259 ROW is the glyph row in which the glyphs are found, AREA is the
20260 area within the row. START is the index of the first glyph to
20261 consider, END is the index of the last + 1.
20262
20263 Value is the index of the first glyph not in S. */
20264
20265 static int
20266 fill_stretch_glyph_string (s, row, area, start, end)
20267 struct glyph_string *s;
20268 struct glyph_row *row;
20269 enum glyph_row_area area;
20270 int start, end;
20271 {
20272 struct glyph *glyph, *last;
20273 int voffset, face_id;
20274
20275 xassert (s->first_glyph->type == STRETCH_GLYPH);
20276
20277 glyph = s->row->glyphs[s->area] + start;
20278 last = s->row->glyphs[s->area] + end;
20279 face_id = glyph->face_id;
20280 s->face = FACE_FROM_ID (s->f, face_id);
20281 s->font = s->face->font;
20282 s->width = glyph->pixel_width;
20283 s->nchars = 1;
20284 voffset = glyph->voffset;
20285
20286 for (++glyph;
20287 (glyph < last
20288 && glyph->type == STRETCH_GLYPH
20289 && glyph->voffset == voffset
20290 && glyph->face_id == face_id);
20291 ++glyph)
20292 s->width += glyph->pixel_width;
20293
20294 /* Adjust base line for subscript/superscript text. */
20295 s->ybase += voffset;
20296
20297 /* The case that face->gc == 0 is handled when drawing the glyph
20298 string by calling PREPARE_FACE_FOR_DISPLAY. */
20299 xassert (s->face);
20300 return glyph - s->row->glyphs[s->area];
20301 }
20302
20303 static struct font_metrics *
20304 get_per_char_metric (f, font, char2b)
20305 struct frame *f;
20306 struct font *font;
20307 XChar2b *char2b;
20308 {
20309 static struct font_metrics metrics;
20310 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20311
20312 if (! font || code == FONT_INVALID_CODE)
20313 return NULL;
20314 font->driver->text_extents (font, &code, 1, &metrics);
20315 return &metrics;
20316 }
20317
20318 /* EXPORT for RIF:
20319 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20320 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20321 assumed to be zero. */
20322
20323 void
20324 x_get_glyph_overhangs (glyph, f, left, right)
20325 struct glyph *glyph;
20326 struct frame *f;
20327 int *left, *right;
20328 {
20329 *left = *right = 0;
20330
20331 if (glyph->type == CHAR_GLYPH)
20332 {
20333 struct face *face;
20334 XChar2b char2b;
20335 struct font_metrics *pcm;
20336
20337 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20338 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20339 {
20340 if (pcm->rbearing > pcm->width)
20341 *right = pcm->rbearing - pcm->width;
20342 if (pcm->lbearing < 0)
20343 *left = -pcm->lbearing;
20344 }
20345 }
20346 else if (glyph->type == COMPOSITE_GLYPH)
20347 {
20348 if (! glyph->u.cmp.automatic)
20349 {
20350 struct composition *cmp = composition_table[glyph->u.cmp.id];
20351
20352 if (cmp->rbearing > cmp->pixel_width)
20353 *right = cmp->rbearing - cmp->pixel_width;
20354 if (cmp->lbearing < 0)
20355 *left = - cmp->lbearing;
20356 }
20357 else
20358 {
20359 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20360 struct font_metrics metrics;
20361
20362 composition_gstring_width (gstring, glyph->u.cmp.from,
20363 glyph->u.cmp.to + 1, &metrics);
20364 if (metrics.rbearing > metrics.width)
20365 *right = metrics.rbearing - metrics.width;
20366 if (metrics.lbearing < 0)
20367 *left = - metrics.lbearing;
20368 }
20369 }
20370 }
20371
20372
20373 /* Return the index of the first glyph preceding glyph string S that
20374 is overwritten by S because of S's left overhang. Value is -1
20375 if no glyphs are overwritten. */
20376
20377 static int
20378 left_overwritten (s)
20379 struct glyph_string *s;
20380 {
20381 int k;
20382
20383 if (s->left_overhang)
20384 {
20385 int x = 0, i;
20386 struct glyph *glyphs = s->row->glyphs[s->area];
20387 int first = s->first_glyph - glyphs;
20388
20389 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20390 x -= glyphs[i].pixel_width;
20391
20392 k = i + 1;
20393 }
20394 else
20395 k = -1;
20396
20397 return k;
20398 }
20399
20400
20401 /* Return the index of the first glyph preceding glyph string S that
20402 is overwriting S because of its right overhang. Value is -1 if no
20403 glyph in front of S overwrites S. */
20404
20405 static int
20406 left_overwriting (s)
20407 struct glyph_string *s;
20408 {
20409 int i, k, x;
20410 struct glyph *glyphs = s->row->glyphs[s->area];
20411 int first = s->first_glyph - glyphs;
20412
20413 k = -1;
20414 x = 0;
20415 for (i = first - 1; i >= 0; --i)
20416 {
20417 int left, right;
20418 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20419 if (x + right > 0)
20420 k = i;
20421 x -= glyphs[i].pixel_width;
20422 }
20423
20424 return k;
20425 }
20426
20427
20428 /* Return the index of the last glyph following glyph string S that is
20429 overwritten by S because of S's right overhang. Value is -1 if
20430 no such glyph is found. */
20431
20432 static int
20433 right_overwritten (s)
20434 struct glyph_string *s;
20435 {
20436 int k = -1;
20437
20438 if (s->right_overhang)
20439 {
20440 int x = 0, i;
20441 struct glyph *glyphs = s->row->glyphs[s->area];
20442 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20443 int end = s->row->used[s->area];
20444
20445 for (i = first; i < end && s->right_overhang > x; ++i)
20446 x += glyphs[i].pixel_width;
20447
20448 k = i;
20449 }
20450
20451 return k;
20452 }
20453
20454
20455 /* Return the index of the last glyph following glyph string S that
20456 overwrites S because of its left overhang. Value is negative
20457 if no such glyph is found. */
20458
20459 static int
20460 right_overwriting (s)
20461 struct glyph_string *s;
20462 {
20463 int i, k, x;
20464 int end = s->row->used[s->area];
20465 struct glyph *glyphs = s->row->glyphs[s->area];
20466 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20467
20468 k = -1;
20469 x = 0;
20470 for (i = first; i < end; ++i)
20471 {
20472 int left, right;
20473 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20474 if (x - left < 0)
20475 k = i;
20476 x += glyphs[i].pixel_width;
20477 }
20478
20479 return k;
20480 }
20481
20482
20483 /* Set background width of glyph string S. START is the index of the
20484 first glyph following S. LAST_X is the right-most x-position + 1
20485 in the drawing area. */
20486
20487 static INLINE void
20488 set_glyph_string_background_width (s, start, last_x)
20489 struct glyph_string *s;
20490 int start;
20491 int last_x;
20492 {
20493 /* If the face of this glyph string has to be drawn to the end of
20494 the drawing area, set S->extends_to_end_of_line_p. */
20495
20496 if (start == s->row->used[s->area]
20497 && s->area == TEXT_AREA
20498 && ((s->row->fill_line_p
20499 && (s->hl == DRAW_NORMAL_TEXT
20500 || s->hl == DRAW_IMAGE_RAISED
20501 || s->hl == DRAW_IMAGE_SUNKEN))
20502 || s->hl == DRAW_MOUSE_FACE))
20503 s->extends_to_end_of_line_p = 1;
20504
20505 /* If S extends its face to the end of the line, set its
20506 background_width to the distance to the right edge of the drawing
20507 area. */
20508 if (s->extends_to_end_of_line_p)
20509 s->background_width = last_x - s->x + 1;
20510 else
20511 s->background_width = s->width;
20512 }
20513
20514
20515 /* Compute overhangs and x-positions for glyph string S and its
20516 predecessors, or successors. X is the starting x-position for S.
20517 BACKWARD_P non-zero means process predecessors. */
20518
20519 static void
20520 compute_overhangs_and_x (s, x, backward_p)
20521 struct glyph_string *s;
20522 int x;
20523 int backward_p;
20524 {
20525 if (backward_p)
20526 {
20527 while (s)
20528 {
20529 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20530 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20531 x -= s->width;
20532 s->x = x;
20533 s = s->prev;
20534 }
20535 }
20536 else
20537 {
20538 while (s)
20539 {
20540 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20541 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20542 s->x = x;
20543 x += s->width;
20544 s = s->next;
20545 }
20546 }
20547 }
20548
20549
20550
20551 /* The following macros are only called from draw_glyphs below.
20552 They reference the following parameters of that function directly:
20553 `w', `row', `area', and `overlap_p'
20554 as well as the following local variables:
20555 `s', `f', and `hdc' (in W32) */
20556
20557 #ifdef HAVE_NTGUI
20558 /* On W32, silently add local `hdc' variable to argument list of
20559 init_glyph_string. */
20560 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20561 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20562 #else
20563 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20564 init_glyph_string (s, char2b, w, row, area, start, hl)
20565 #endif
20566
20567 /* Add a glyph string for a stretch glyph to the list of strings
20568 between HEAD and TAIL. START is the index of the stretch glyph in
20569 row area AREA of glyph row ROW. END is the index of the last glyph
20570 in that glyph row area. X is the current output position assigned
20571 to the new glyph string constructed. HL overrides that face of the
20572 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20573 is the right-most x-position of the drawing area. */
20574
20575 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20576 and below -- keep them on one line. */
20577 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20578 do \
20579 { \
20580 s = (struct glyph_string *) alloca (sizeof *s); \
20581 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20582 START = fill_stretch_glyph_string (s, row, area, START, END); \
20583 append_glyph_string (&HEAD, &TAIL, s); \
20584 s->x = (X); \
20585 } \
20586 while (0)
20587
20588
20589 /* Add a glyph string for an image glyph to the list of strings
20590 between HEAD and TAIL. START is the index of the image glyph in
20591 row area AREA of glyph row ROW. END is the index of the last glyph
20592 in that glyph row area. X is the current output position assigned
20593 to the new glyph string constructed. HL overrides that face of the
20594 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20595 is the right-most x-position of the drawing area. */
20596
20597 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20598 do \
20599 { \
20600 s = (struct glyph_string *) alloca (sizeof *s); \
20601 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20602 fill_image_glyph_string (s); \
20603 append_glyph_string (&HEAD, &TAIL, s); \
20604 ++START; \
20605 s->x = (X); \
20606 } \
20607 while (0)
20608
20609
20610 /* Add a glyph string for a sequence of character glyphs to the list
20611 of strings between HEAD and TAIL. START is the index of the first
20612 glyph in row area AREA of glyph row ROW that is part of the new
20613 glyph string. END is the index of the last glyph in that glyph row
20614 area. X is the current output position assigned to the new glyph
20615 string constructed. HL overrides that face of the glyph; e.g. it
20616 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20617 right-most x-position of the drawing area. */
20618
20619 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20620 do \
20621 { \
20622 int face_id; \
20623 XChar2b *char2b; \
20624 \
20625 face_id = (row)->glyphs[area][START].face_id; \
20626 \
20627 s = (struct glyph_string *) alloca (sizeof *s); \
20628 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20629 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20630 append_glyph_string (&HEAD, &TAIL, s); \
20631 s->x = (X); \
20632 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20633 } \
20634 while (0)
20635
20636
20637 /* Add a glyph string for a composite sequence to the list of strings
20638 between HEAD and TAIL. START is the index of the first glyph in
20639 row area AREA of glyph row ROW that is part of the new glyph
20640 string. END is the index of the last glyph in that glyph row area.
20641 X is the current output position assigned to the new glyph string
20642 constructed. HL overrides that face of the glyph; e.g. it is
20643 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20644 x-position of the drawing area. */
20645
20646 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20647 do { \
20648 int face_id = (row)->glyphs[area][START].face_id; \
20649 struct face *base_face = FACE_FROM_ID (f, face_id); \
20650 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20651 struct composition *cmp = composition_table[cmp_id]; \
20652 XChar2b *char2b; \
20653 struct glyph_string *first_s; \
20654 int n; \
20655 \
20656 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20657 \
20658 /* Make glyph_strings for each glyph sequence that is drawable by \
20659 the same face, and append them to HEAD/TAIL. */ \
20660 for (n = 0; n < cmp->glyph_len;) \
20661 { \
20662 s = (struct glyph_string *) alloca (sizeof *s); \
20663 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20664 append_glyph_string (&(HEAD), &(TAIL), s); \
20665 s->cmp = cmp; \
20666 s->cmp_from = n; \
20667 s->x = (X); \
20668 if (n == 0) \
20669 first_s = s; \
20670 n = fill_composite_glyph_string (s, base_face, overlaps); \
20671 } \
20672 \
20673 ++START; \
20674 s = first_s; \
20675 } while (0)
20676
20677
20678 /* Add a glyph string for a glyph-string sequence to the list of strings
20679 between HEAD and TAIL. */
20680
20681 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20682 do { \
20683 int face_id; \
20684 XChar2b *char2b; \
20685 Lisp_Object gstring; \
20686 \
20687 face_id = (row)->glyphs[area][START].face_id; \
20688 gstring = (composition_gstring_from_id \
20689 ((row)->glyphs[area][START].u.cmp.id)); \
20690 s = (struct glyph_string *) alloca (sizeof *s); \
20691 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20692 * LGSTRING_GLYPH_LEN (gstring)); \
20693 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20694 append_glyph_string (&(HEAD), &(TAIL), s); \
20695 s->x = (X); \
20696 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20697 } while (0)
20698
20699
20700 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20701 of AREA of glyph row ROW on window W between indices START and END.
20702 HL overrides the face for drawing glyph strings, e.g. it is
20703 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20704 x-positions of the drawing area.
20705
20706 This is an ugly monster macro construct because we must use alloca
20707 to allocate glyph strings (because draw_glyphs can be called
20708 asynchronously). */
20709
20710 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20711 do \
20712 { \
20713 HEAD = TAIL = NULL; \
20714 while (START < END) \
20715 { \
20716 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20717 switch (first_glyph->type) \
20718 { \
20719 case CHAR_GLYPH: \
20720 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20721 HL, X, LAST_X); \
20722 break; \
20723 \
20724 case COMPOSITE_GLYPH: \
20725 if (first_glyph->u.cmp.automatic) \
20726 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20727 HL, X, LAST_X); \
20728 else \
20729 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20730 HL, X, LAST_X); \
20731 break; \
20732 \
20733 case STRETCH_GLYPH: \
20734 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20735 HL, X, LAST_X); \
20736 break; \
20737 \
20738 case IMAGE_GLYPH: \
20739 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20740 HL, X, LAST_X); \
20741 break; \
20742 \
20743 default: \
20744 abort (); \
20745 } \
20746 \
20747 if (s) \
20748 { \
20749 set_glyph_string_background_width (s, START, LAST_X); \
20750 (X) += s->width; \
20751 } \
20752 } \
20753 } while (0)
20754
20755
20756 /* Draw glyphs between START and END in AREA of ROW on window W,
20757 starting at x-position X. X is relative to AREA in W. HL is a
20758 face-override with the following meaning:
20759
20760 DRAW_NORMAL_TEXT draw normally
20761 DRAW_CURSOR draw in cursor face
20762 DRAW_MOUSE_FACE draw in mouse face.
20763 DRAW_INVERSE_VIDEO draw in mode line face
20764 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20765 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20766
20767 If OVERLAPS is non-zero, draw only the foreground of characters and
20768 clip to the physical height of ROW. Non-zero value also defines
20769 the overlapping part to be drawn:
20770
20771 OVERLAPS_PRED overlap with preceding rows
20772 OVERLAPS_SUCC overlap with succeeding rows
20773 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20774 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20775
20776 Value is the x-position reached, relative to AREA of W. */
20777
20778 static int
20779 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20780 struct window *w;
20781 int x;
20782 struct glyph_row *row;
20783 enum glyph_row_area area;
20784 EMACS_INT start, end;
20785 enum draw_glyphs_face hl;
20786 int overlaps;
20787 {
20788 struct glyph_string *head, *tail;
20789 struct glyph_string *s;
20790 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20791 int i, j, x_reached, last_x, area_left = 0;
20792 struct frame *f = XFRAME (WINDOW_FRAME (w));
20793 DECLARE_HDC (hdc);
20794
20795 ALLOCATE_HDC (hdc, f);
20796
20797 /* Let's rather be paranoid than getting a SEGV. */
20798 end = min (end, row->used[area]);
20799 start = max (0, start);
20800 start = min (end, start);
20801
20802 /* Translate X to frame coordinates. Set last_x to the right
20803 end of the drawing area. */
20804 if (row->full_width_p)
20805 {
20806 /* X is relative to the left edge of W, without scroll bars
20807 or fringes. */
20808 area_left = WINDOW_LEFT_EDGE_X (w);
20809 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20810 }
20811 else
20812 {
20813 area_left = window_box_left (w, area);
20814 last_x = area_left + window_box_width (w, area);
20815 }
20816 x += area_left;
20817
20818 /* Build a doubly-linked list of glyph_string structures between
20819 head and tail from what we have to draw. Note that the macro
20820 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20821 the reason we use a separate variable `i'. */
20822 i = start;
20823 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20824 if (tail)
20825 x_reached = tail->x + tail->background_width;
20826 else
20827 x_reached = x;
20828
20829 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20830 the row, redraw some glyphs in front or following the glyph
20831 strings built above. */
20832 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20833 {
20834 struct glyph_string *h, *t;
20835 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20836 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20837 int dummy_x = 0;
20838
20839 /* If mouse highlighting is on, we may need to draw adjacent
20840 glyphs using mouse-face highlighting. */
20841 if (area == TEXT_AREA && row->mouse_face_p)
20842 {
20843 struct glyph_row *mouse_beg_row, *mouse_end_row;
20844
20845 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20846 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20847
20848 if (row >= mouse_beg_row && row <= mouse_end_row)
20849 {
20850 check_mouse_face = 1;
20851 mouse_beg_col = (row == mouse_beg_row)
20852 ? dpyinfo->mouse_face_beg_col : 0;
20853 mouse_end_col = (row == mouse_end_row)
20854 ? dpyinfo->mouse_face_end_col
20855 : row->used[TEXT_AREA];
20856 }
20857 }
20858
20859 /* Compute overhangs for all glyph strings. */
20860 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20861 for (s = head; s; s = s->next)
20862 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20863
20864 /* Prepend glyph strings for glyphs in front of the first glyph
20865 string that are overwritten because of the first glyph
20866 string's left overhang. The background of all strings
20867 prepended must be drawn because the first glyph string
20868 draws over it. */
20869 i = left_overwritten (head);
20870 if (i >= 0)
20871 {
20872 enum draw_glyphs_face overlap_hl;
20873
20874 /* If this row contains mouse highlighting, attempt to draw
20875 the overlapped glyphs with the correct highlight. This
20876 code fails if the overlap encompasses more than one glyph
20877 and mouse-highlight spans only some of these glyphs.
20878 However, making it work perfectly involves a lot more
20879 code, and I don't know if the pathological case occurs in
20880 practice, so we'll stick to this for now. --- cyd */
20881 if (check_mouse_face
20882 && mouse_beg_col < start && mouse_end_col > i)
20883 overlap_hl = DRAW_MOUSE_FACE;
20884 else
20885 overlap_hl = DRAW_NORMAL_TEXT;
20886
20887 j = i;
20888 BUILD_GLYPH_STRINGS (j, start, h, t,
20889 overlap_hl, dummy_x, last_x);
20890 compute_overhangs_and_x (t, head->x, 1);
20891 prepend_glyph_string_lists (&head, &tail, h, t);
20892 clip_head = head;
20893 }
20894
20895 /* Prepend glyph strings for glyphs in front of the first glyph
20896 string that overwrite that glyph string because of their
20897 right overhang. For these strings, only the foreground must
20898 be drawn, because it draws over the glyph string at `head'.
20899 The background must not be drawn because this would overwrite
20900 right overhangs of preceding glyphs for which no glyph
20901 strings exist. */
20902 i = left_overwriting (head);
20903 if (i >= 0)
20904 {
20905 enum draw_glyphs_face overlap_hl;
20906
20907 if (check_mouse_face
20908 && mouse_beg_col < start && mouse_end_col > i)
20909 overlap_hl = DRAW_MOUSE_FACE;
20910 else
20911 overlap_hl = DRAW_NORMAL_TEXT;
20912
20913 clip_head = head;
20914 BUILD_GLYPH_STRINGS (i, start, h, t,
20915 overlap_hl, dummy_x, last_x);
20916 for (s = h; s; s = s->next)
20917 s->background_filled_p = 1;
20918 compute_overhangs_and_x (t, head->x, 1);
20919 prepend_glyph_string_lists (&head, &tail, h, t);
20920 }
20921
20922 /* Append glyphs strings for glyphs following the last glyph
20923 string tail that are overwritten by tail. The background of
20924 these strings has to be drawn because tail's foreground draws
20925 over it. */
20926 i = right_overwritten (tail);
20927 if (i >= 0)
20928 {
20929 enum draw_glyphs_face overlap_hl;
20930
20931 if (check_mouse_face
20932 && mouse_beg_col < i && mouse_end_col > end)
20933 overlap_hl = DRAW_MOUSE_FACE;
20934 else
20935 overlap_hl = DRAW_NORMAL_TEXT;
20936
20937 BUILD_GLYPH_STRINGS (end, i, h, t,
20938 overlap_hl, x, last_x);
20939 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20940 append_glyph_string_lists (&head, &tail, h, t);
20941 clip_tail = tail;
20942 }
20943
20944 /* Append glyph strings for glyphs following the last glyph
20945 string tail that overwrite tail. The foreground of such
20946 glyphs has to be drawn because it writes into the background
20947 of tail. The background must not be drawn because it could
20948 paint over the foreground of following glyphs. */
20949 i = right_overwriting (tail);
20950 if (i >= 0)
20951 {
20952 enum draw_glyphs_face overlap_hl;
20953 if (check_mouse_face
20954 && mouse_beg_col < i && mouse_end_col > end)
20955 overlap_hl = DRAW_MOUSE_FACE;
20956 else
20957 overlap_hl = DRAW_NORMAL_TEXT;
20958
20959 clip_tail = tail;
20960 i++; /* We must include the Ith glyph. */
20961 BUILD_GLYPH_STRINGS (end, i, h, t,
20962 overlap_hl, x, last_x);
20963 for (s = h; s; s = s->next)
20964 s->background_filled_p = 1;
20965 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20966 append_glyph_string_lists (&head, &tail, h, t);
20967 }
20968 if (clip_head || clip_tail)
20969 for (s = head; s; s = s->next)
20970 {
20971 s->clip_head = clip_head;
20972 s->clip_tail = clip_tail;
20973 }
20974 }
20975
20976 /* Draw all strings. */
20977 for (s = head; s; s = s->next)
20978 FRAME_RIF (f)->draw_glyph_string (s);
20979
20980 #ifndef HAVE_NS
20981 /* When focus a sole frame and move horizontally, this sets on_p to 0
20982 causing a failure to erase prev cursor position. */
20983 if (area == TEXT_AREA
20984 && !row->full_width_p
20985 /* When drawing overlapping rows, only the glyph strings'
20986 foreground is drawn, which doesn't erase a cursor
20987 completely. */
20988 && !overlaps)
20989 {
20990 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20991 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20992 : (tail ? tail->x + tail->background_width : x));
20993 x0 -= area_left;
20994 x1 -= area_left;
20995
20996 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20997 row->y, MATRIX_ROW_BOTTOM_Y (row));
20998 }
20999 #endif
21000
21001 /* Value is the x-position up to which drawn, relative to AREA of W.
21002 This doesn't include parts drawn because of overhangs. */
21003 if (row->full_width_p)
21004 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21005 else
21006 x_reached -= area_left;
21007
21008 RELEASE_HDC (hdc, f);
21009
21010 return x_reached;
21011 }
21012
21013 /* Expand row matrix if too narrow. Don't expand if area
21014 is not present. */
21015
21016 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21017 { \
21018 if (!fonts_changed_p \
21019 && (it->glyph_row->glyphs[area] \
21020 < it->glyph_row->glyphs[area + 1])) \
21021 { \
21022 it->w->ncols_scale_factor++; \
21023 fonts_changed_p = 1; \
21024 } \
21025 }
21026
21027 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21028 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21029
21030 static INLINE void
21031 append_glyph (it)
21032 struct it *it;
21033 {
21034 struct glyph *glyph;
21035 enum glyph_row_area area = it->area;
21036
21037 xassert (it->glyph_row);
21038 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21039
21040 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21041 if (glyph < it->glyph_row->glyphs[area + 1])
21042 {
21043 glyph->charpos = CHARPOS (it->position);
21044 glyph->object = it->object;
21045 if (it->pixel_width > 0)
21046 {
21047 glyph->pixel_width = it->pixel_width;
21048 glyph->padding_p = 0;
21049 }
21050 else
21051 {
21052 /* Assure at least 1-pixel width. Otherwise, cursor can't
21053 be displayed correctly. */
21054 glyph->pixel_width = 1;
21055 glyph->padding_p = 1;
21056 }
21057 glyph->ascent = it->ascent;
21058 glyph->descent = it->descent;
21059 glyph->voffset = it->voffset;
21060 glyph->type = CHAR_GLYPH;
21061 glyph->avoid_cursor_p = it->avoid_cursor_p;
21062 glyph->multibyte_p = it->multibyte_p;
21063 glyph->left_box_line_p = it->start_of_box_run_p;
21064 glyph->right_box_line_p = it->end_of_box_run_p;
21065 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21066 || it->phys_descent > it->descent);
21067 glyph->glyph_not_available_p = it->glyph_not_available_p;
21068 glyph->face_id = it->face_id;
21069 glyph->u.ch = it->char_to_display;
21070 glyph->slice = null_glyph_slice;
21071 glyph->font_type = FONT_TYPE_UNKNOWN;
21072 if (it->bidi_p)
21073 {
21074 glyph->resolved_level = it->bidi_it.resolved_level;
21075 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21076 abort ();
21077 glyph->bidi_type = it->bidi_it.type;
21078 }
21079 ++it->glyph_row->used[area];
21080 }
21081 else
21082 IT_EXPAND_MATRIX_WIDTH (it, area);
21083 }
21084
21085 /* Store one glyph for the composition IT->cmp_it.id in
21086 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21087 non-null. */
21088
21089 static INLINE void
21090 append_composite_glyph (it)
21091 struct it *it;
21092 {
21093 struct glyph *glyph;
21094 enum glyph_row_area area = it->area;
21095
21096 xassert (it->glyph_row);
21097
21098 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21099 if (glyph < it->glyph_row->glyphs[area + 1])
21100 {
21101 glyph->charpos = CHARPOS (it->position);
21102 glyph->object = it->object;
21103 glyph->pixel_width = it->pixel_width;
21104 glyph->ascent = it->ascent;
21105 glyph->descent = it->descent;
21106 glyph->voffset = it->voffset;
21107 glyph->type = COMPOSITE_GLYPH;
21108 if (it->cmp_it.ch < 0)
21109 {
21110 glyph->u.cmp.automatic = 0;
21111 glyph->u.cmp.id = it->cmp_it.id;
21112 }
21113 else
21114 {
21115 glyph->u.cmp.automatic = 1;
21116 glyph->u.cmp.id = it->cmp_it.id;
21117 glyph->u.cmp.from = it->cmp_it.from;
21118 glyph->u.cmp.to = it->cmp_it.to - 1;
21119 }
21120 glyph->avoid_cursor_p = it->avoid_cursor_p;
21121 glyph->multibyte_p = it->multibyte_p;
21122 glyph->left_box_line_p = it->start_of_box_run_p;
21123 glyph->right_box_line_p = it->end_of_box_run_p;
21124 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21125 || it->phys_descent > it->descent);
21126 glyph->padding_p = 0;
21127 glyph->glyph_not_available_p = 0;
21128 glyph->face_id = it->face_id;
21129 glyph->slice = null_glyph_slice;
21130 glyph->font_type = FONT_TYPE_UNKNOWN;
21131 if (it->bidi_p)
21132 {
21133 glyph->resolved_level = it->bidi_it.resolved_level;
21134 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21135 abort ();
21136 glyph->bidi_type = it->bidi_it.type;
21137 }
21138 ++it->glyph_row->used[area];
21139 }
21140 else
21141 IT_EXPAND_MATRIX_WIDTH (it, area);
21142 }
21143
21144
21145 /* Change IT->ascent and IT->height according to the setting of
21146 IT->voffset. */
21147
21148 static INLINE void
21149 take_vertical_position_into_account (it)
21150 struct it *it;
21151 {
21152 if (it->voffset)
21153 {
21154 if (it->voffset < 0)
21155 /* Increase the ascent so that we can display the text higher
21156 in the line. */
21157 it->ascent -= it->voffset;
21158 else
21159 /* Increase the descent so that we can display the text lower
21160 in the line. */
21161 it->descent += it->voffset;
21162 }
21163 }
21164
21165
21166 /* Produce glyphs/get display metrics for the image IT is loaded with.
21167 See the description of struct display_iterator in dispextern.h for
21168 an overview of struct display_iterator. */
21169
21170 static void
21171 produce_image_glyph (it)
21172 struct it *it;
21173 {
21174 struct image *img;
21175 struct face *face;
21176 int glyph_ascent, crop;
21177 struct glyph_slice slice;
21178
21179 xassert (it->what == IT_IMAGE);
21180
21181 face = FACE_FROM_ID (it->f, it->face_id);
21182 xassert (face);
21183 /* Make sure X resources of the face is loaded. */
21184 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21185
21186 if (it->image_id < 0)
21187 {
21188 /* Fringe bitmap. */
21189 it->ascent = it->phys_ascent = 0;
21190 it->descent = it->phys_descent = 0;
21191 it->pixel_width = 0;
21192 it->nglyphs = 0;
21193 return;
21194 }
21195
21196 img = IMAGE_FROM_ID (it->f, it->image_id);
21197 xassert (img);
21198 /* Make sure X resources of the image is loaded. */
21199 prepare_image_for_display (it->f, img);
21200
21201 slice.x = slice.y = 0;
21202 slice.width = img->width;
21203 slice.height = img->height;
21204
21205 if (INTEGERP (it->slice.x))
21206 slice.x = XINT (it->slice.x);
21207 else if (FLOATP (it->slice.x))
21208 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21209
21210 if (INTEGERP (it->slice.y))
21211 slice.y = XINT (it->slice.y);
21212 else if (FLOATP (it->slice.y))
21213 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21214
21215 if (INTEGERP (it->slice.width))
21216 slice.width = XINT (it->slice.width);
21217 else if (FLOATP (it->slice.width))
21218 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21219
21220 if (INTEGERP (it->slice.height))
21221 slice.height = XINT (it->slice.height);
21222 else if (FLOATP (it->slice.height))
21223 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21224
21225 if (slice.x >= img->width)
21226 slice.x = img->width;
21227 if (slice.y >= img->height)
21228 slice.y = img->height;
21229 if (slice.x + slice.width >= img->width)
21230 slice.width = img->width - slice.x;
21231 if (slice.y + slice.height > img->height)
21232 slice.height = img->height - slice.y;
21233
21234 if (slice.width == 0 || slice.height == 0)
21235 return;
21236
21237 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21238
21239 it->descent = slice.height - glyph_ascent;
21240 if (slice.y == 0)
21241 it->descent += img->vmargin;
21242 if (slice.y + slice.height == img->height)
21243 it->descent += img->vmargin;
21244 it->phys_descent = it->descent;
21245
21246 it->pixel_width = slice.width;
21247 if (slice.x == 0)
21248 it->pixel_width += img->hmargin;
21249 if (slice.x + slice.width == img->width)
21250 it->pixel_width += img->hmargin;
21251
21252 /* It's quite possible for images to have an ascent greater than
21253 their height, so don't get confused in that case. */
21254 if (it->descent < 0)
21255 it->descent = 0;
21256
21257 it->nglyphs = 1;
21258
21259 if (face->box != FACE_NO_BOX)
21260 {
21261 if (face->box_line_width > 0)
21262 {
21263 if (slice.y == 0)
21264 it->ascent += face->box_line_width;
21265 if (slice.y + slice.height == img->height)
21266 it->descent += face->box_line_width;
21267 }
21268
21269 if (it->start_of_box_run_p && slice.x == 0)
21270 it->pixel_width += eabs (face->box_line_width);
21271 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21272 it->pixel_width += eabs (face->box_line_width);
21273 }
21274
21275 take_vertical_position_into_account (it);
21276
21277 /* Automatically crop wide image glyphs at right edge so we can
21278 draw the cursor on same display row. */
21279 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21280 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21281 {
21282 it->pixel_width -= crop;
21283 slice.width -= crop;
21284 }
21285
21286 if (it->glyph_row)
21287 {
21288 struct glyph *glyph;
21289 enum glyph_row_area area = it->area;
21290
21291 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21292 if (glyph < it->glyph_row->glyphs[area + 1])
21293 {
21294 glyph->charpos = CHARPOS (it->position);
21295 glyph->object = it->object;
21296 glyph->pixel_width = it->pixel_width;
21297 glyph->ascent = glyph_ascent;
21298 glyph->descent = it->descent;
21299 glyph->voffset = it->voffset;
21300 glyph->type = IMAGE_GLYPH;
21301 glyph->avoid_cursor_p = it->avoid_cursor_p;
21302 glyph->multibyte_p = it->multibyte_p;
21303 glyph->left_box_line_p = it->start_of_box_run_p;
21304 glyph->right_box_line_p = it->end_of_box_run_p;
21305 glyph->overlaps_vertically_p = 0;
21306 glyph->padding_p = 0;
21307 glyph->glyph_not_available_p = 0;
21308 glyph->face_id = it->face_id;
21309 glyph->u.img_id = img->id;
21310 glyph->slice = slice;
21311 glyph->font_type = FONT_TYPE_UNKNOWN;
21312 if (it->bidi_p)
21313 {
21314 glyph->resolved_level = it->bidi_it.resolved_level;
21315 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21316 abort ();
21317 glyph->bidi_type = it->bidi_it.type;
21318 }
21319 ++it->glyph_row->used[area];
21320 }
21321 else
21322 IT_EXPAND_MATRIX_WIDTH (it, area);
21323 }
21324 }
21325
21326
21327 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21328 of the glyph, WIDTH and HEIGHT are the width and height of the
21329 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21330
21331 static void
21332 append_stretch_glyph (it, object, width, height, ascent)
21333 struct it *it;
21334 Lisp_Object object;
21335 int width, height;
21336 int ascent;
21337 {
21338 struct glyph *glyph;
21339 enum glyph_row_area area = it->area;
21340
21341 xassert (ascent >= 0 && ascent <= height);
21342
21343 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21344 if (glyph < it->glyph_row->glyphs[area + 1])
21345 {
21346 glyph->charpos = CHARPOS (it->position);
21347 glyph->object = object;
21348 glyph->pixel_width = width;
21349 glyph->ascent = ascent;
21350 glyph->descent = height - ascent;
21351 glyph->voffset = it->voffset;
21352 glyph->type = STRETCH_GLYPH;
21353 glyph->avoid_cursor_p = it->avoid_cursor_p;
21354 glyph->multibyte_p = it->multibyte_p;
21355 glyph->left_box_line_p = it->start_of_box_run_p;
21356 glyph->right_box_line_p = it->end_of_box_run_p;
21357 glyph->overlaps_vertically_p = 0;
21358 glyph->padding_p = 0;
21359 glyph->glyph_not_available_p = 0;
21360 glyph->face_id = it->face_id;
21361 glyph->u.stretch.ascent = ascent;
21362 glyph->u.stretch.height = height;
21363 glyph->slice = null_glyph_slice;
21364 glyph->font_type = FONT_TYPE_UNKNOWN;
21365 if (it->bidi_p)
21366 {
21367 glyph->resolved_level = it->bidi_it.resolved_level;
21368 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21369 abort ();
21370 glyph->bidi_type = it->bidi_it.type;
21371 }
21372 ++it->glyph_row->used[area];
21373 }
21374 else
21375 IT_EXPAND_MATRIX_WIDTH (it, area);
21376 }
21377
21378
21379 /* Produce a stretch glyph for iterator IT. IT->object is the value
21380 of the glyph property displayed. The value must be a list
21381 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21382 being recognized:
21383
21384 1. `:width WIDTH' specifies that the space should be WIDTH *
21385 canonical char width wide. WIDTH may be an integer or floating
21386 point number.
21387
21388 2. `:relative-width FACTOR' specifies that the width of the stretch
21389 should be computed from the width of the first character having the
21390 `glyph' property, and should be FACTOR times that width.
21391
21392 3. `:align-to HPOS' specifies that the space should be wide enough
21393 to reach HPOS, a value in canonical character units.
21394
21395 Exactly one of the above pairs must be present.
21396
21397 4. `:height HEIGHT' specifies that the height of the stretch produced
21398 should be HEIGHT, measured in canonical character units.
21399
21400 5. `:relative-height FACTOR' specifies that the height of the
21401 stretch should be FACTOR times the height of the characters having
21402 the glyph property.
21403
21404 Either none or exactly one of 4 or 5 must be present.
21405
21406 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21407 of the stretch should be used for the ascent of the stretch.
21408 ASCENT must be in the range 0 <= ASCENT <= 100. */
21409
21410 static void
21411 produce_stretch_glyph (it)
21412 struct it *it;
21413 {
21414 /* (space :width WIDTH :height HEIGHT ...) */
21415 Lisp_Object prop, plist;
21416 int width = 0, height = 0, align_to = -1;
21417 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21418 int ascent = 0;
21419 double tem;
21420 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21421 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21422
21423 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21424
21425 /* List should start with `space'. */
21426 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21427 plist = XCDR (it->object);
21428
21429 /* Compute the width of the stretch. */
21430 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21431 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21432 {
21433 /* Absolute width `:width WIDTH' specified and valid. */
21434 zero_width_ok_p = 1;
21435 width = (int)tem;
21436 }
21437 else if (prop = Fplist_get (plist, QCrelative_width),
21438 NUMVAL (prop) > 0)
21439 {
21440 /* Relative width `:relative-width FACTOR' specified and valid.
21441 Compute the width of the characters having the `glyph'
21442 property. */
21443 struct it it2;
21444 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21445
21446 it2 = *it;
21447 if (it->multibyte_p)
21448 {
21449 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
21450 - IT_BYTEPOS (*it));
21451 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
21452 }
21453 else
21454 it2.c = *p, it2.len = 1;
21455
21456 it2.glyph_row = NULL;
21457 it2.what = IT_CHARACTER;
21458 x_produce_glyphs (&it2);
21459 width = NUMVAL (prop) * it2.pixel_width;
21460 }
21461 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21462 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21463 {
21464 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21465 align_to = (align_to < 0
21466 ? 0
21467 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21468 else if (align_to < 0)
21469 align_to = window_box_left_offset (it->w, TEXT_AREA);
21470 width = max (0, (int)tem + align_to - it->current_x);
21471 zero_width_ok_p = 1;
21472 }
21473 else
21474 /* Nothing specified -> width defaults to canonical char width. */
21475 width = FRAME_COLUMN_WIDTH (it->f);
21476
21477 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21478 width = 1;
21479
21480 /* Compute height. */
21481 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21482 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21483 {
21484 height = (int)tem;
21485 zero_height_ok_p = 1;
21486 }
21487 else if (prop = Fplist_get (plist, QCrelative_height),
21488 NUMVAL (prop) > 0)
21489 height = FONT_HEIGHT (font) * NUMVAL (prop);
21490 else
21491 height = FONT_HEIGHT (font);
21492
21493 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21494 height = 1;
21495
21496 /* Compute percentage of height used for ascent. If
21497 `:ascent ASCENT' is present and valid, use that. Otherwise,
21498 derive the ascent from the font in use. */
21499 if (prop = Fplist_get (plist, QCascent),
21500 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21501 ascent = height * NUMVAL (prop) / 100.0;
21502 else if (!NILP (prop)
21503 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21504 ascent = min (max (0, (int)tem), height);
21505 else
21506 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21507
21508 if (width > 0 && it->line_wrap != TRUNCATE
21509 && it->current_x + width > it->last_visible_x)
21510 width = it->last_visible_x - it->current_x - 1;
21511
21512 if (width > 0 && height > 0 && it->glyph_row)
21513 {
21514 Lisp_Object object = it->stack[it->sp - 1].string;
21515 if (!STRINGP (object))
21516 object = it->w->buffer;
21517 append_stretch_glyph (it, object, width, height, ascent);
21518 }
21519
21520 it->pixel_width = width;
21521 it->ascent = it->phys_ascent = ascent;
21522 it->descent = it->phys_descent = height - it->ascent;
21523 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21524
21525 take_vertical_position_into_account (it);
21526 }
21527
21528 /* Calculate line-height and line-spacing properties.
21529 An integer value specifies explicit pixel value.
21530 A float value specifies relative value to current face height.
21531 A cons (float . face-name) specifies relative value to
21532 height of specified face font.
21533
21534 Returns height in pixels, or nil. */
21535
21536
21537 static Lisp_Object
21538 calc_line_height_property (it, val, font, boff, override)
21539 struct it *it;
21540 Lisp_Object val;
21541 struct font *font;
21542 int boff, override;
21543 {
21544 Lisp_Object face_name = Qnil;
21545 int ascent, descent, height;
21546
21547 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21548 return val;
21549
21550 if (CONSP (val))
21551 {
21552 face_name = XCAR (val);
21553 val = XCDR (val);
21554 if (!NUMBERP (val))
21555 val = make_number (1);
21556 if (NILP (face_name))
21557 {
21558 height = it->ascent + it->descent;
21559 goto scale;
21560 }
21561 }
21562
21563 if (NILP (face_name))
21564 {
21565 font = FRAME_FONT (it->f);
21566 boff = FRAME_BASELINE_OFFSET (it->f);
21567 }
21568 else if (EQ (face_name, Qt))
21569 {
21570 override = 0;
21571 }
21572 else
21573 {
21574 int face_id;
21575 struct face *face;
21576
21577 face_id = lookup_named_face (it->f, face_name, 0);
21578 if (face_id < 0)
21579 return make_number (-1);
21580
21581 face = FACE_FROM_ID (it->f, face_id);
21582 font = face->font;
21583 if (font == NULL)
21584 return make_number (-1);
21585 boff = font->baseline_offset;
21586 if (font->vertical_centering)
21587 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21588 }
21589
21590 ascent = FONT_BASE (font) + boff;
21591 descent = FONT_DESCENT (font) - boff;
21592
21593 if (override)
21594 {
21595 it->override_ascent = ascent;
21596 it->override_descent = descent;
21597 it->override_boff = boff;
21598 }
21599
21600 height = ascent + descent;
21601
21602 scale:
21603 if (FLOATP (val))
21604 height = (int)(XFLOAT_DATA (val) * height);
21605 else if (INTEGERP (val))
21606 height *= XINT (val);
21607
21608 return make_number (height);
21609 }
21610
21611
21612 /* RIF:
21613 Produce glyphs/get display metrics for the display element IT is
21614 loaded with. See the description of struct it in dispextern.h
21615 for an overview of struct it. */
21616
21617 void
21618 x_produce_glyphs (it)
21619 struct it *it;
21620 {
21621 int extra_line_spacing = it->extra_line_spacing;
21622
21623 it->glyph_not_available_p = 0;
21624
21625 if (it->what == IT_CHARACTER)
21626 {
21627 XChar2b char2b;
21628 struct font *font;
21629 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21630 struct font_metrics *pcm;
21631 int font_not_found_p;
21632 int boff; /* baseline offset */
21633 /* We may change it->multibyte_p upon unibyte<->multibyte
21634 conversion. So, save the current value now and restore it
21635 later.
21636
21637 Note: It seems that we don't have to record multibyte_p in
21638 struct glyph because the character code itself tells whether
21639 or not the character is multibyte. Thus, in the future, we
21640 must consider eliminating the field `multibyte_p' in the
21641 struct glyph. */
21642 int saved_multibyte_p = it->multibyte_p;
21643
21644 /* Maybe translate single-byte characters to multibyte, or the
21645 other way. */
21646 it->char_to_display = it->c;
21647 if (!ASCII_BYTE_P (it->c)
21648 && ! it->multibyte_p)
21649 {
21650 if (SINGLE_BYTE_CHAR_P (it->c)
21651 && unibyte_display_via_language_environment)
21652 {
21653 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21654
21655 /* get_next_display_element assures that this decoding
21656 never fails. */
21657 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21658 it->multibyte_p = 1;
21659 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21660 -1, Qnil);
21661 face = FACE_FROM_ID (it->f, it->face_id);
21662 }
21663 }
21664
21665 /* Get font to use. Encode IT->char_to_display. */
21666 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21667 &char2b, it->multibyte_p, 0);
21668 font = face->font;
21669
21670 font_not_found_p = font == NULL;
21671 if (font_not_found_p)
21672 {
21673 /* When no suitable font found, display an empty box based
21674 on the metrics of the font of the default face (or what
21675 remapped). */
21676 struct face *no_font_face
21677 = FACE_FROM_ID (it->f,
21678 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21679 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21680 font = no_font_face->font;
21681 boff = font->baseline_offset;
21682 }
21683 else
21684 {
21685 boff = font->baseline_offset;
21686 if (font->vertical_centering)
21687 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21688 }
21689
21690 if (it->char_to_display >= ' '
21691 && (!it->multibyte_p || it->char_to_display < 128))
21692 {
21693 /* Either unibyte or ASCII. */
21694 int stretched_p;
21695
21696 it->nglyphs = 1;
21697
21698 pcm = get_per_char_metric (it->f, font, &char2b);
21699
21700 if (it->override_ascent >= 0)
21701 {
21702 it->ascent = it->override_ascent;
21703 it->descent = it->override_descent;
21704 boff = it->override_boff;
21705 }
21706 else
21707 {
21708 it->ascent = FONT_BASE (font) + boff;
21709 it->descent = FONT_DESCENT (font) - boff;
21710 }
21711
21712 if (pcm)
21713 {
21714 it->phys_ascent = pcm->ascent + boff;
21715 it->phys_descent = pcm->descent - boff;
21716 it->pixel_width = pcm->width;
21717 }
21718 else
21719 {
21720 it->glyph_not_available_p = 1;
21721 it->phys_ascent = it->ascent;
21722 it->phys_descent = it->descent;
21723 it->pixel_width = FONT_WIDTH (font);
21724 }
21725
21726 if (it->constrain_row_ascent_descent_p)
21727 {
21728 if (it->descent > it->max_descent)
21729 {
21730 it->ascent += it->descent - it->max_descent;
21731 it->descent = it->max_descent;
21732 }
21733 if (it->ascent > it->max_ascent)
21734 {
21735 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21736 it->ascent = it->max_ascent;
21737 }
21738 it->phys_ascent = min (it->phys_ascent, it->ascent);
21739 it->phys_descent = min (it->phys_descent, it->descent);
21740 extra_line_spacing = 0;
21741 }
21742
21743 /* If this is a space inside a region of text with
21744 `space-width' property, change its width. */
21745 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21746 if (stretched_p)
21747 it->pixel_width *= XFLOATINT (it->space_width);
21748
21749 /* If face has a box, add the box thickness to the character
21750 height. If character has a box line to the left and/or
21751 right, add the box line width to the character's width. */
21752 if (face->box != FACE_NO_BOX)
21753 {
21754 int thick = face->box_line_width;
21755
21756 if (thick > 0)
21757 {
21758 it->ascent += thick;
21759 it->descent += thick;
21760 }
21761 else
21762 thick = -thick;
21763
21764 if (it->start_of_box_run_p)
21765 it->pixel_width += thick;
21766 if (it->end_of_box_run_p)
21767 it->pixel_width += thick;
21768 }
21769
21770 /* If face has an overline, add the height of the overline
21771 (1 pixel) and a 1 pixel margin to the character height. */
21772 if (face->overline_p)
21773 it->ascent += overline_margin;
21774
21775 if (it->constrain_row_ascent_descent_p)
21776 {
21777 if (it->ascent > it->max_ascent)
21778 it->ascent = it->max_ascent;
21779 if (it->descent > it->max_descent)
21780 it->descent = it->max_descent;
21781 }
21782
21783 take_vertical_position_into_account (it);
21784
21785 /* If we have to actually produce glyphs, do it. */
21786 if (it->glyph_row)
21787 {
21788 if (stretched_p)
21789 {
21790 /* Translate a space with a `space-width' property
21791 into a stretch glyph. */
21792 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21793 / FONT_HEIGHT (font));
21794 append_stretch_glyph (it, it->object, it->pixel_width,
21795 it->ascent + it->descent, ascent);
21796 }
21797 else
21798 append_glyph (it);
21799
21800 /* If characters with lbearing or rbearing are displayed
21801 in this line, record that fact in a flag of the
21802 glyph row. This is used to optimize X output code. */
21803 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21804 it->glyph_row->contains_overlapping_glyphs_p = 1;
21805 }
21806 if (! stretched_p && it->pixel_width == 0)
21807 /* We assure that all visible glyphs have at least 1-pixel
21808 width. */
21809 it->pixel_width = 1;
21810 }
21811 else if (it->char_to_display == '\n')
21812 {
21813 /* A newline has no width, but we need the height of the
21814 line. But if previous part of the line sets a height,
21815 don't increase that height */
21816
21817 Lisp_Object height;
21818 Lisp_Object total_height = Qnil;
21819
21820 it->override_ascent = -1;
21821 it->pixel_width = 0;
21822 it->nglyphs = 0;
21823
21824 height = get_it_property(it, Qline_height);
21825 /* Split (line-height total-height) list */
21826 if (CONSP (height)
21827 && CONSP (XCDR (height))
21828 && NILP (XCDR (XCDR (height))))
21829 {
21830 total_height = XCAR (XCDR (height));
21831 height = XCAR (height);
21832 }
21833 height = calc_line_height_property(it, height, font, boff, 1);
21834
21835 if (it->override_ascent >= 0)
21836 {
21837 it->ascent = it->override_ascent;
21838 it->descent = it->override_descent;
21839 boff = it->override_boff;
21840 }
21841 else
21842 {
21843 it->ascent = FONT_BASE (font) + boff;
21844 it->descent = FONT_DESCENT (font) - boff;
21845 }
21846
21847 if (EQ (height, Qt))
21848 {
21849 if (it->descent > it->max_descent)
21850 {
21851 it->ascent += it->descent - it->max_descent;
21852 it->descent = it->max_descent;
21853 }
21854 if (it->ascent > it->max_ascent)
21855 {
21856 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21857 it->ascent = it->max_ascent;
21858 }
21859 it->phys_ascent = min (it->phys_ascent, it->ascent);
21860 it->phys_descent = min (it->phys_descent, it->descent);
21861 it->constrain_row_ascent_descent_p = 1;
21862 extra_line_spacing = 0;
21863 }
21864 else
21865 {
21866 Lisp_Object spacing;
21867
21868 it->phys_ascent = it->ascent;
21869 it->phys_descent = it->descent;
21870
21871 if ((it->max_ascent > 0 || it->max_descent > 0)
21872 && face->box != FACE_NO_BOX
21873 && face->box_line_width > 0)
21874 {
21875 it->ascent += face->box_line_width;
21876 it->descent += face->box_line_width;
21877 }
21878 if (!NILP (height)
21879 && XINT (height) > it->ascent + it->descent)
21880 it->ascent = XINT (height) - it->descent;
21881
21882 if (!NILP (total_height))
21883 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21884 else
21885 {
21886 spacing = get_it_property(it, Qline_spacing);
21887 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21888 }
21889 if (INTEGERP (spacing))
21890 {
21891 extra_line_spacing = XINT (spacing);
21892 if (!NILP (total_height))
21893 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21894 }
21895 }
21896 }
21897 else if (it->char_to_display == '\t')
21898 {
21899 if (font->space_width > 0)
21900 {
21901 int tab_width = it->tab_width * font->space_width;
21902 int x = it->current_x + it->continuation_lines_width;
21903 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21904
21905 /* If the distance from the current position to the next tab
21906 stop is less than a space character width, use the
21907 tab stop after that. */
21908 if (next_tab_x - x < font->space_width)
21909 next_tab_x += tab_width;
21910
21911 it->pixel_width = next_tab_x - x;
21912 it->nglyphs = 1;
21913 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21914 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21915
21916 if (it->glyph_row)
21917 {
21918 append_stretch_glyph (it, it->object, it->pixel_width,
21919 it->ascent + it->descent, it->ascent);
21920 }
21921 }
21922 else
21923 {
21924 it->pixel_width = 0;
21925 it->nglyphs = 1;
21926 }
21927 }
21928 else
21929 {
21930 /* A multi-byte character. Assume that the display width of the
21931 character is the width of the character multiplied by the
21932 width of the font. */
21933
21934 /* If we found a font, this font should give us the right
21935 metrics. If we didn't find a font, use the frame's
21936 default font and calculate the width of the character by
21937 multiplying the width of font by the width of the
21938 character. */
21939
21940 pcm = get_per_char_metric (it->f, font, &char2b);
21941
21942 if (font_not_found_p || !pcm)
21943 {
21944 int char_width = CHAR_WIDTH (it->char_to_display);
21945
21946 if (char_width == 0)
21947 /* This is a non spacing character. But, as we are
21948 going to display an empty box, the box must occupy
21949 at least one column. */
21950 char_width = 1;
21951 it->glyph_not_available_p = 1;
21952 it->pixel_width = font->space_width * char_width;
21953 it->phys_ascent = FONT_BASE (font) + boff;
21954 it->phys_descent = FONT_DESCENT (font) - boff;
21955 }
21956 else
21957 {
21958 it->pixel_width = pcm->width;
21959 it->phys_ascent = pcm->ascent + boff;
21960 it->phys_descent = pcm->descent - boff;
21961 if (it->glyph_row
21962 && (pcm->lbearing < 0
21963 || pcm->rbearing > pcm->width))
21964 it->glyph_row->contains_overlapping_glyphs_p = 1;
21965 }
21966 it->nglyphs = 1;
21967 it->ascent = FONT_BASE (font) + boff;
21968 it->descent = FONT_DESCENT (font) - boff;
21969 if (face->box != FACE_NO_BOX)
21970 {
21971 int thick = face->box_line_width;
21972
21973 if (thick > 0)
21974 {
21975 it->ascent += thick;
21976 it->descent += thick;
21977 }
21978 else
21979 thick = - thick;
21980
21981 if (it->start_of_box_run_p)
21982 it->pixel_width += thick;
21983 if (it->end_of_box_run_p)
21984 it->pixel_width += thick;
21985 }
21986
21987 /* If face has an overline, add the height of the overline
21988 (1 pixel) and a 1 pixel margin to the character height. */
21989 if (face->overline_p)
21990 it->ascent += overline_margin;
21991
21992 take_vertical_position_into_account (it);
21993
21994 if (it->ascent < 0)
21995 it->ascent = 0;
21996 if (it->descent < 0)
21997 it->descent = 0;
21998
21999 if (it->glyph_row)
22000 append_glyph (it);
22001 if (it->pixel_width == 0)
22002 /* We assure that all visible glyphs have at least 1-pixel
22003 width. */
22004 it->pixel_width = 1;
22005 }
22006 it->multibyte_p = saved_multibyte_p;
22007 }
22008 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22009 {
22010 /* A static composition.
22011
22012 Note: A composition is represented as one glyph in the
22013 glyph matrix. There are no padding glyphs.
22014
22015 Important note: pixel_width, ascent, and descent are the
22016 values of what is drawn by draw_glyphs (i.e. the values of
22017 the overall glyphs composed). */
22018 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22019 int boff; /* baseline offset */
22020 struct composition *cmp = composition_table[it->cmp_it.id];
22021 int glyph_len = cmp->glyph_len;
22022 struct font *font = face->font;
22023
22024 it->nglyphs = 1;
22025
22026 /* If we have not yet calculated pixel size data of glyphs of
22027 the composition for the current face font, calculate them
22028 now. Theoretically, we have to check all fonts for the
22029 glyphs, but that requires much time and memory space. So,
22030 here we check only the font of the first glyph. This may
22031 lead to incorrect display, but it's very rare, and C-l
22032 (recenter-top-bottom) can correct the display anyway. */
22033 if (! cmp->font || cmp->font != font)
22034 {
22035 /* Ascent and descent of the font of the first character
22036 of this composition (adjusted by baseline offset).
22037 Ascent and descent of overall glyphs should not be less
22038 than these, respectively. */
22039 int font_ascent, font_descent, font_height;
22040 /* Bounding box of the overall glyphs. */
22041 int leftmost, rightmost, lowest, highest;
22042 int lbearing, rbearing;
22043 int i, width, ascent, descent;
22044 int left_padded = 0, right_padded = 0;
22045 int c;
22046 XChar2b char2b;
22047 struct font_metrics *pcm;
22048 int font_not_found_p;
22049 int pos;
22050
22051 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22052 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22053 break;
22054 if (glyph_len < cmp->glyph_len)
22055 right_padded = 1;
22056 for (i = 0; i < glyph_len; i++)
22057 {
22058 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22059 break;
22060 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22061 }
22062 if (i > 0)
22063 left_padded = 1;
22064
22065 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22066 : IT_CHARPOS (*it));
22067 /* If no suitable font is found, use the default font. */
22068 font_not_found_p = font == NULL;
22069 if (font_not_found_p)
22070 {
22071 face = face->ascii_face;
22072 font = face->font;
22073 }
22074 boff = font->baseline_offset;
22075 if (font->vertical_centering)
22076 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22077 font_ascent = FONT_BASE (font) + boff;
22078 font_descent = FONT_DESCENT (font) - boff;
22079 font_height = FONT_HEIGHT (font);
22080
22081 cmp->font = (void *) font;
22082
22083 pcm = NULL;
22084 if (! font_not_found_p)
22085 {
22086 get_char_face_and_encoding (it->f, c, it->face_id,
22087 &char2b, it->multibyte_p, 0);
22088 pcm = get_per_char_metric (it->f, font, &char2b);
22089 }
22090
22091 /* Initialize the bounding box. */
22092 if (pcm)
22093 {
22094 width = pcm->width;
22095 ascent = pcm->ascent;
22096 descent = pcm->descent;
22097 lbearing = pcm->lbearing;
22098 rbearing = pcm->rbearing;
22099 }
22100 else
22101 {
22102 width = FONT_WIDTH (font);
22103 ascent = FONT_BASE (font);
22104 descent = FONT_DESCENT (font);
22105 lbearing = 0;
22106 rbearing = width;
22107 }
22108
22109 rightmost = width;
22110 leftmost = 0;
22111 lowest = - descent + boff;
22112 highest = ascent + boff;
22113
22114 if (! font_not_found_p
22115 && font->default_ascent
22116 && CHAR_TABLE_P (Vuse_default_ascent)
22117 && !NILP (Faref (Vuse_default_ascent,
22118 make_number (it->char_to_display))))
22119 highest = font->default_ascent + boff;
22120
22121 /* Draw the first glyph at the normal position. It may be
22122 shifted to right later if some other glyphs are drawn
22123 at the left. */
22124 cmp->offsets[i * 2] = 0;
22125 cmp->offsets[i * 2 + 1] = boff;
22126 cmp->lbearing = lbearing;
22127 cmp->rbearing = rbearing;
22128
22129 /* Set cmp->offsets for the remaining glyphs. */
22130 for (i++; i < glyph_len; i++)
22131 {
22132 int left, right, btm, top;
22133 int ch = COMPOSITION_GLYPH (cmp, i);
22134 int face_id;
22135 struct face *this_face;
22136 int this_boff;
22137
22138 if (ch == '\t')
22139 ch = ' ';
22140 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22141 this_face = FACE_FROM_ID (it->f, face_id);
22142 font = this_face->font;
22143
22144 if (font == NULL)
22145 pcm = NULL;
22146 else
22147 {
22148 this_boff = font->baseline_offset;
22149 if (font->vertical_centering)
22150 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22151 get_char_face_and_encoding (it->f, ch, face_id,
22152 &char2b, it->multibyte_p, 0);
22153 pcm = get_per_char_metric (it->f, font, &char2b);
22154 }
22155 if (! pcm)
22156 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22157 else
22158 {
22159 width = pcm->width;
22160 ascent = pcm->ascent;
22161 descent = pcm->descent;
22162 lbearing = pcm->lbearing;
22163 rbearing = pcm->rbearing;
22164 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22165 {
22166 /* Relative composition with or without
22167 alternate chars. */
22168 left = (leftmost + rightmost - width) / 2;
22169 btm = - descent + boff;
22170 if (font->relative_compose
22171 && (! CHAR_TABLE_P (Vignore_relative_composition)
22172 || NILP (Faref (Vignore_relative_composition,
22173 make_number (ch)))))
22174 {
22175
22176 if (- descent >= font->relative_compose)
22177 /* One extra pixel between two glyphs. */
22178 btm = highest + 1;
22179 else if (ascent <= 0)
22180 /* One extra pixel between two glyphs. */
22181 btm = lowest - 1 - ascent - descent;
22182 }
22183 }
22184 else
22185 {
22186 /* A composition rule is specified by an integer
22187 value that encodes global and new reference
22188 points (GREF and NREF). GREF and NREF are
22189 specified by numbers as below:
22190
22191 0---1---2 -- ascent
22192 | |
22193 | |
22194 | |
22195 9--10--11 -- center
22196 | |
22197 ---3---4---5--- baseline
22198 | |
22199 6---7---8 -- descent
22200 */
22201 int rule = COMPOSITION_RULE (cmp, i);
22202 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22203
22204 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22205 grefx = gref % 3, nrefx = nref % 3;
22206 grefy = gref / 3, nrefy = nref / 3;
22207 if (xoff)
22208 xoff = font_height * (xoff - 128) / 256;
22209 if (yoff)
22210 yoff = font_height * (yoff - 128) / 256;
22211
22212 left = (leftmost
22213 + grefx * (rightmost - leftmost) / 2
22214 - nrefx * width / 2
22215 + xoff);
22216
22217 btm = ((grefy == 0 ? highest
22218 : grefy == 1 ? 0
22219 : grefy == 2 ? lowest
22220 : (highest + lowest) / 2)
22221 - (nrefy == 0 ? ascent + descent
22222 : nrefy == 1 ? descent - boff
22223 : nrefy == 2 ? 0
22224 : (ascent + descent) / 2)
22225 + yoff);
22226 }
22227
22228 cmp->offsets[i * 2] = left;
22229 cmp->offsets[i * 2 + 1] = btm + descent;
22230
22231 /* Update the bounding box of the overall glyphs. */
22232 if (width > 0)
22233 {
22234 right = left + width;
22235 if (left < leftmost)
22236 leftmost = left;
22237 if (right > rightmost)
22238 rightmost = right;
22239 }
22240 top = btm + descent + ascent;
22241 if (top > highest)
22242 highest = top;
22243 if (btm < lowest)
22244 lowest = btm;
22245
22246 if (cmp->lbearing > left + lbearing)
22247 cmp->lbearing = left + lbearing;
22248 if (cmp->rbearing < left + rbearing)
22249 cmp->rbearing = left + rbearing;
22250 }
22251 }
22252
22253 /* If there are glyphs whose x-offsets are negative,
22254 shift all glyphs to the right and make all x-offsets
22255 non-negative. */
22256 if (leftmost < 0)
22257 {
22258 for (i = 0; i < cmp->glyph_len; i++)
22259 cmp->offsets[i * 2] -= leftmost;
22260 rightmost -= leftmost;
22261 cmp->lbearing -= leftmost;
22262 cmp->rbearing -= leftmost;
22263 }
22264
22265 if (left_padded && cmp->lbearing < 0)
22266 {
22267 for (i = 0; i < cmp->glyph_len; i++)
22268 cmp->offsets[i * 2] -= cmp->lbearing;
22269 rightmost -= cmp->lbearing;
22270 cmp->rbearing -= cmp->lbearing;
22271 cmp->lbearing = 0;
22272 }
22273 if (right_padded && rightmost < cmp->rbearing)
22274 {
22275 rightmost = cmp->rbearing;
22276 }
22277
22278 cmp->pixel_width = rightmost;
22279 cmp->ascent = highest;
22280 cmp->descent = - lowest;
22281 if (cmp->ascent < font_ascent)
22282 cmp->ascent = font_ascent;
22283 if (cmp->descent < font_descent)
22284 cmp->descent = font_descent;
22285 }
22286
22287 if (it->glyph_row
22288 && (cmp->lbearing < 0
22289 || cmp->rbearing > cmp->pixel_width))
22290 it->glyph_row->contains_overlapping_glyphs_p = 1;
22291
22292 it->pixel_width = cmp->pixel_width;
22293 it->ascent = it->phys_ascent = cmp->ascent;
22294 it->descent = it->phys_descent = cmp->descent;
22295 if (face->box != FACE_NO_BOX)
22296 {
22297 int thick = face->box_line_width;
22298
22299 if (thick > 0)
22300 {
22301 it->ascent += thick;
22302 it->descent += thick;
22303 }
22304 else
22305 thick = - thick;
22306
22307 if (it->start_of_box_run_p)
22308 it->pixel_width += thick;
22309 if (it->end_of_box_run_p)
22310 it->pixel_width += thick;
22311 }
22312
22313 /* If face has an overline, add the height of the overline
22314 (1 pixel) and a 1 pixel margin to the character height. */
22315 if (face->overline_p)
22316 it->ascent += overline_margin;
22317
22318 take_vertical_position_into_account (it);
22319 if (it->ascent < 0)
22320 it->ascent = 0;
22321 if (it->descent < 0)
22322 it->descent = 0;
22323
22324 if (it->glyph_row)
22325 append_composite_glyph (it);
22326 }
22327 else if (it->what == IT_COMPOSITION)
22328 {
22329 /* A dynamic (automatic) composition. */
22330 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22331 Lisp_Object gstring;
22332 struct font_metrics metrics;
22333
22334 gstring = composition_gstring_from_id (it->cmp_it.id);
22335 it->pixel_width
22336 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22337 &metrics);
22338 if (it->glyph_row
22339 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22340 it->glyph_row->contains_overlapping_glyphs_p = 1;
22341 it->ascent = it->phys_ascent = metrics.ascent;
22342 it->descent = it->phys_descent = metrics.descent;
22343 if (face->box != FACE_NO_BOX)
22344 {
22345 int thick = face->box_line_width;
22346
22347 if (thick > 0)
22348 {
22349 it->ascent += thick;
22350 it->descent += thick;
22351 }
22352 else
22353 thick = - thick;
22354
22355 if (it->start_of_box_run_p)
22356 it->pixel_width += thick;
22357 if (it->end_of_box_run_p)
22358 it->pixel_width += thick;
22359 }
22360 /* If face has an overline, add the height of the overline
22361 (1 pixel) and a 1 pixel margin to the character height. */
22362 if (face->overline_p)
22363 it->ascent += overline_margin;
22364 take_vertical_position_into_account (it);
22365 if (it->ascent < 0)
22366 it->ascent = 0;
22367 if (it->descent < 0)
22368 it->descent = 0;
22369
22370 if (it->glyph_row)
22371 append_composite_glyph (it);
22372 }
22373 else if (it->what == IT_IMAGE)
22374 produce_image_glyph (it);
22375 else if (it->what == IT_STRETCH)
22376 produce_stretch_glyph (it);
22377
22378 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22379 because this isn't true for images with `:ascent 100'. */
22380 xassert (it->ascent >= 0 && it->descent >= 0);
22381 if (it->area == TEXT_AREA)
22382 it->current_x += it->pixel_width;
22383
22384 if (extra_line_spacing > 0)
22385 {
22386 it->descent += extra_line_spacing;
22387 if (extra_line_spacing > it->max_extra_line_spacing)
22388 it->max_extra_line_spacing = extra_line_spacing;
22389 }
22390
22391 it->max_ascent = max (it->max_ascent, it->ascent);
22392 it->max_descent = max (it->max_descent, it->descent);
22393 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22394 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22395 }
22396
22397 /* EXPORT for RIF:
22398 Output LEN glyphs starting at START at the nominal cursor position.
22399 Advance the nominal cursor over the text. The global variable
22400 updated_window contains the window being updated, updated_row is
22401 the glyph row being updated, and updated_area is the area of that
22402 row being updated. */
22403
22404 void
22405 x_write_glyphs (start, len)
22406 struct glyph *start;
22407 int len;
22408 {
22409 int x, hpos;
22410
22411 xassert (updated_window && updated_row);
22412 BLOCK_INPUT;
22413
22414 /* Write glyphs. */
22415
22416 hpos = start - updated_row->glyphs[updated_area];
22417 x = draw_glyphs (updated_window, output_cursor.x,
22418 updated_row, updated_area,
22419 hpos, hpos + len,
22420 DRAW_NORMAL_TEXT, 0);
22421
22422 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22423 if (updated_area == TEXT_AREA
22424 && updated_window->phys_cursor_on_p
22425 && updated_window->phys_cursor.vpos == output_cursor.vpos
22426 && updated_window->phys_cursor.hpos >= hpos
22427 && updated_window->phys_cursor.hpos < hpos + len)
22428 updated_window->phys_cursor_on_p = 0;
22429
22430 UNBLOCK_INPUT;
22431
22432 /* Advance the output cursor. */
22433 output_cursor.hpos += len;
22434 output_cursor.x = x;
22435 }
22436
22437
22438 /* EXPORT for RIF:
22439 Insert LEN glyphs from START at the nominal cursor position. */
22440
22441 void
22442 x_insert_glyphs (start, len)
22443 struct glyph *start;
22444 int len;
22445 {
22446 struct frame *f;
22447 struct window *w;
22448 int line_height, shift_by_width, shifted_region_width;
22449 struct glyph_row *row;
22450 struct glyph *glyph;
22451 int frame_x, frame_y;
22452 EMACS_INT hpos;
22453
22454 xassert (updated_window && updated_row);
22455 BLOCK_INPUT;
22456 w = updated_window;
22457 f = XFRAME (WINDOW_FRAME (w));
22458
22459 /* Get the height of the line we are in. */
22460 row = updated_row;
22461 line_height = row->height;
22462
22463 /* Get the width of the glyphs to insert. */
22464 shift_by_width = 0;
22465 for (glyph = start; glyph < start + len; ++glyph)
22466 shift_by_width += glyph->pixel_width;
22467
22468 /* Get the width of the region to shift right. */
22469 shifted_region_width = (window_box_width (w, updated_area)
22470 - output_cursor.x
22471 - shift_by_width);
22472
22473 /* Shift right. */
22474 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22475 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22476
22477 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22478 line_height, shift_by_width);
22479
22480 /* Write the glyphs. */
22481 hpos = start - row->glyphs[updated_area];
22482 draw_glyphs (w, output_cursor.x, row, updated_area,
22483 hpos, hpos + len,
22484 DRAW_NORMAL_TEXT, 0);
22485
22486 /* Advance the output cursor. */
22487 output_cursor.hpos += len;
22488 output_cursor.x += shift_by_width;
22489 UNBLOCK_INPUT;
22490 }
22491
22492
22493 /* EXPORT for RIF:
22494 Erase the current text line from the nominal cursor position
22495 (inclusive) to pixel column TO_X (exclusive). The idea is that
22496 everything from TO_X onward is already erased.
22497
22498 TO_X is a pixel position relative to updated_area of
22499 updated_window. TO_X == -1 means clear to the end of this area. */
22500
22501 void
22502 x_clear_end_of_line (to_x)
22503 int to_x;
22504 {
22505 struct frame *f;
22506 struct window *w = updated_window;
22507 int max_x, min_y, max_y;
22508 int from_x, from_y, to_y;
22509
22510 xassert (updated_window && updated_row);
22511 f = XFRAME (w->frame);
22512
22513 if (updated_row->full_width_p)
22514 max_x = WINDOW_TOTAL_WIDTH (w);
22515 else
22516 max_x = window_box_width (w, updated_area);
22517 max_y = window_text_bottom_y (w);
22518
22519 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22520 of window. For TO_X > 0, truncate to end of drawing area. */
22521 if (to_x == 0)
22522 return;
22523 else if (to_x < 0)
22524 to_x = max_x;
22525 else
22526 to_x = min (to_x, max_x);
22527
22528 to_y = min (max_y, output_cursor.y + updated_row->height);
22529
22530 /* Notice if the cursor will be cleared by this operation. */
22531 if (!updated_row->full_width_p)
22532 notice_overwritten_cursor (w, updated_area,
22533 output_cursor.x, -1,
22534 updated_row->y,
22535 MATRIX_ROW_BOTTOM_Y (updated_row));
22536
22537 from_x = output_cursor.x;
22538
22539 /* Translate to frame coordinates. */
22540 if (updated_row->full_width_p)
22541 {
22542 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22543 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22544 }
22545 else
22546 {
22547 int area_left = window_box_left (w, updated_area);
22548 from_x += area_left;
22549 to_x += area_left;
22550 }
22551
22552 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22553 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22554 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22555
22556 /* Prevent inadvertently clearing to end of the X window. */
22557 if (to_x > from_x && to_y > from_y)
22558 {
22559 BLOCK_INPUT;
22560 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22561 to_x - from_x, to_y - from_y);
22562 UNBLOCK_INPUT;
22563 }
22564 }
22565
22566 #endif /* HAVE_WINDOW_SYSTEM */
22567
22568
22569 \f
22570 /***********************************************************************
22571 Cursor types
22572 ***********************************************************************/
22573
22574 /* Value is the internal representation of the specified cursor type
22575 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22576 of the bar cursor. */
22577
22578 static enum text_cursor_kinds
22579 get_specified_cursor_type (arg, width)
22580 Lisp_Object arg;
22581 int *width;
22582 {
22583 enum text_cursor_kinds type;
22584
22585 if (NILP (arg))
22586 return NO_CURSOR;
22587
22588 if (EQ (arg, Qbox))
22589 return FILLED_BOX_CURSOR;
22590
22591 if (EQ (arg, Qhollow))
22592 return HOLLOW_BOX_CURSOR;
22593
22594 if (EQ (arg, Qbar))
22595 {
22596 *width = 2;
22597 return BAR_CURSOR;
22598 }
22599
22600 if (CONSP (arg)
22601 && EQ (XCAR (arg), Qbar)
22602 && INTEGERP (XCDR (arg))
22603 && XINT (XCDR (arg)) >= 0)
22604 {
22605 *width = XINT (XCDR (arg));
22606 return BAR_CURSOR;
22607 }
22608
22609 if (EQ (arg, Qhbar))
22610 {
22611 *width = 2;
22612 return HBAR_CURSOR;
22613 }
22614
22615 if (CONSP (arg)
22616 && EQ (XCAR (arg), Qhbar)
22617 && INTEGERP (XCDR (arg))
22618 && XINT (XCDR (arg)) >= 0)
22619 {
22620 *width = XINT (XCDR (arg));
22621 return HBAR_CURSOR;
22622 }
22623
22624 /* Treat anything unknown as "hollow box cursor".
22625 It was bad to signal an error; people have trouble fixing
22626 .Xdefaults with Emacs, when it has something bad in it. */
22627 type = HOLLOW_BOX_CURSOR;
22628
22629 return type;
22630 }
22631
22632 /* Set the default cursor types for specified frame. */
22633 void
22634 set_frame_cursor_types (f, arg)
22635 struct frame *f;
22636 Lisp_Object arg;
22637 {
22638 int width;
22639 Lisp_Object tem;
22640
22641 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22642 FRAME_CURSOR_WIDTH (f) = width;
22643
22644 /* By default, set up the blink-off state depending on the on-state. */
22645
22646 tem = Fassoc (arg, Vblink_cursor_alist);
22647 if (!NILP (tem))
22648 {
22649 FRAME_BLINK_OFF_CURSOR (f)
22650 = get_specified_cursor_type (XCDR (tem), &width);
22651 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22652 }
22653 else
22654 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22655 }
22656
22657
22658 /* Return the cursor we want to be displayed in window W. Return
22659 width of bar/hbar cursor through WIDTH arg. Return with
22660 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22661 (i.e. if the `system caret' should track this cursor).
22662
22663 In a mini-buffer window, we want the cursor only to appear if we
22664 are reading input from this window. For the selected window, we
22665 want the cursor type given by the frame parameter or buffer local
22666 setting of cursor-type. If explicitly marked off, draw no cursor.
22667 In all other cases, we want a hollow box cursor. */
22668
22669 static enum text_cursor_kinds
22670 get_window_cursor_type (w, glyph, width, active_cursor)
22671 struct window *w;
22672 struct glyph *glyph;
22673 int *width;
22674 int *active_cursor;
22675 {
22676 struct frame *f = XFRAME (w->frame);
22677 struct buffer *b = XBUFFER (w->buffer);
22678 int cursor_type = DEFAULT_CURSOR;
22679 Lisp_Object alt_cursor;
22680 int non_selected = 0;
22681
22682 *active_cursor = 1;
22683
22684 /* Echo area */
22685 if (cursor_in_echo_area
22686 && FRAME_HAS_MINIBUF_P (f)
22687 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22688 {
22689 if (w == XWINDOW (echo_area_window))
22690 {
22691 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22692 {
22693 *width = FRAME_CURSOR_WIDTH (f);
22694 return FRAME_DESIRED_CURSOR (f);
22695 }
22696 else
22697 return get_specified_cursor_type (b->cursor_type, width);
22698 }
22699
22700 *active_cursor = 0;
22701 non_selected = 1;
22702 }
22703
22704 /* Detect a nonselected window or nonselected frame. */
22705 else if (w != XWINDOW (f->selected_window)
22706 #ifdef HAVE_WINDOW_SYSTEM
22707 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22708 #endif
22709 )
22710 {
22711 *active_cursor = 0;
22712
22713 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22714 return NO_CURSOR;
22715
22716 non_selected = 1;
22717 }
22718
22719 /* Never display a cursor in a window in which cursor-type is nil. */
22720 if (NILP (b->cursor_type))
22721 return NO_CURSOR;
22722
22723 /* Get the normal cursor type for this window. */
22724 if (EQ (b->cursor_type, Qt))
22725 {
22726 cursor_type = FRAME_DESIRED_CURSOR (f);
22727 *width = FRAME_CURSOR_WIDTH (f);
22728 }
22729 else
22730 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22731
22732 /* Use cursor-in-non-selected-windows instead
22733 for non-selected window or frame. */
22734 if (non_selected)
22735 {
22736 alt_cursor = b->cursor_in_non_selected_windows;
22737 if (!EQ (Qt, alt_cursor))
22738 return get_specified_cursor_type (alt_cursor, width);
22739 /* t means modify the normal cursor type. */
22740 if (cursor_type == FILLED_BOX_CURSOR)
22741 cursor_type = HOLLOW_BOX_CURSOR;
22742 else if (cursor_type == BAR_CURSOR && *width > 1)
22743 --*width;
22744 return cursor_type;
22745 }
22746
22747 /* Use normal cursor if not blinked off. */
22748 if (!w->cursor_off_p)
22749 {
22750 #ifdef HAVE_WINDOW_SYSTEM
22751 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22752 {
22753 if (cursor_type == FILLED_BOX_CURSOR)
22754 {
22755 /* Using a block cursor on large images can be very annoying.
22756 So use a hollow cursor for "large" images.
22757 If image is not transparent (no mask), also use hollow cursor. */
22758 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22759 if (img != NULL && IMAGEP (img->spec))
22760 {
22761 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22762 where N = size of default frame font size.
22763 This should cover most of the "tiny" icons people may use. */
22764 if (!img->mask
22765 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22766 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22767 cursor_type = HOLLOW_BOX_CURSOR;
22768 }
22769 }
22770 else if (cursor_type != NO_CURSOR)
22771 {
22772 /* Display current only supports BOX and HOLLOW cursors for images.
22773 So for now, unconditionally use a HOLLOW cursor when cursor is
22774 not a solid box cursor. */
22775 cursor_type = HOLLOW_BOX_CURSOR;
22776 }
22777 }
22778 #endif
22779 return cursor_type;
22780 }
22781
22782 /* Cursor is blinked off, so determine how to "toggle" it. */
22783
22784 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22785 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22786 return get_specified_cursor_type (XCDR (alt_cursor), width);
22787
22788 /* Then see if frame has specified a specific blink off cursor type. */
22789 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22790 {
22791 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22792 return FRAME_BLINK_OFF_CURSOR (f);
22793 }
22794
22795 #if 0
22796 /* Some people liked having a permanently visible blinking cursor,
22797 while others had very strong opinions against it. So it was
22798 decided to remove it. KFS 2003-09-03 */
22799
22800 /* Finally perform built-in cursor blinking:
22801 filled box <-> hollow box
22802 wide [h]bar <-> narrow [h]bar
22803 narrow [h]bar <-> no cursor
22804 other type <-> no cursor */
22805
22806 if (cursor_type == FILLED_BOX_CURSOR)
22807 return HOLLOW_BOX_CURSOR;
22808
22809 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22810 {
22811 *width = 1;
22812 return cursor_type;
22813 }
22814 #endif
22815
22816 return NO_CURSOR;
22817 }
22818
22819
22820 #ifdef HAVE_WINDOW_SYSTEM
22821
22822 /* Notice when the text cursor of window W has been completely
22823 overwritten by a drawing operation that outputs glyphs in AREA
22824 starting at X0 and ending at X1 in the line starting at Y0 and
22825 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22826 the rest of the line after X0 has been written. Y coordinates
22827 are window-relative. */
22828
22829 static void
22830 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22831 struct window *w;
22832 enum glyph_row_area area;
22833 int x0, y0, x1, y1;
22834 {
22835 int cx0, cx1, cy0, cy1;
22836 struct glyph_row *row;
22837
22838 if (!w->phys_cursor_on_p)
22839 return;
22840 if (area != TEXT_AREA)
22841 return;
22842
22843 if (w->phys_cursor.vpos < 0
22844 || w->phys_cursor.vpos >= w->current_matrix->nrows
22845 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22846 !(row->enabled_p && row->displays_text_p)))
22847 return;
22848
22849 if (row->cursor_in_fringe_p)
22850 {
22851 row->cursor_in_fringe_p = 0;
22852 draw_fringe_bitmap (w, row, 0);
22853 w->phys_cursor_on_p = 0;
22854 return;
22855 }
22856
22857 cx0 = w->phys_cursor.x;
22858 cx1 = cx0 + w->phys_cursor_width;
22859 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22860 return;
22861
22862 /* The cursor image will be completely removed from the
22863 screen if the output area intersects the cursor area in
22864 y-direction. When we draw in [y0 y1[, and some part of
22865 the cursor is at y < y0, that part must have been drawn
22866 before. When scrolling, the cursor is erased before
22867 actually scrolling, so we don't come here. When not
22868 scrolling, the rows above the old cursor row must have
22869 changed, and in this case these rows must have written
22870 over the cursor image.
22871
22872 Likewise if part of the cursor is below y1, with the
22873 exception of the cursor being in the first blank row at
22874 the buffer and window end because update_text_area
22875 doesn't draw that row. (Except when it does, but
22876 that's handled in update_text_area.) */
22877
22878 cy0 = w->phys_cursor.y;
22879 cy1 = cy0 + w->phys_cursor_height;
22880 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22881 return;
22882
22883 w->phys_cursor_on_p = 0;
22884 }
22885
22886 #endif /* HAVE_WINDOW_SYSTEM */
22887
22888 \f
22889 /************************************************************************
22890 Mouse Face
22891 ************************************************************************/
22892
22893 #ifdef HAVE_WINDOW_SYSTEM
22894
22895 /* EXPORT for RIF:
22896 Fix the display of area AREA of overlapping row ROW in window W
22897 with respect to the overlapping part OVERLAPS. */
22898
22899 void
22900 x_fix_overlapping_area (w, row, area, overlaps)
22901 struct window *w;
22902 struct glyph_row *row;
22903 enum glyph_row_area area;
22904 int overlaps;
22905 {
22906 int i, x;
22907
22908 BLOCK_INPUT;
22909
22910 x = 0;
22911 for (i = 0; i < row->used[area];)
22912 {
22913 if (row->glyphs[area][i].overlaps_vertically_p)
22914 {
22915 int start = i, start_x = x;
22916
22917 do
22918 {
22919 x += row->glyphs[area][i].pixel_width;
22920 ++i;
22921 }
22922 while (i < row->used[area]
22923 && row->glyphs[area][i].overlaps_vertically_p);
22924
22925 draw_glyphs (w, start_x, row, area,
22926 start, i,
22927 DRAW_NORMAL_TEXT, overlaps);
22928 }
22929 else
22930 {
22931 x += row->glyphs[area][i].pixel_width;
22932 ++i;
22933 }
22934 }
22935
22936 UNBLOCK_INPUT;
22937 }
22938
22939
22940 /* EXPORT:
22941 Draw the cursor glyph of window W in glyph row ROW. See the
22942 comment of draw_glyphs for the meaning of HL. */
22943
22944 void
22945 draw_phys_cursor_glyph (w, row, hl)
22946 struct window *w;
22947 struct glyph_row *row;
22948 enum draw_glyphs_face hl;
22949 {
22950 /* If cursor hpos is out of bounds, don't draw garbage. This can
22951 happen in mini-buffer windows when switching between echo area
22952 glyphs and mini-buffer. */
22953 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22954 {
22955 int on_p = w->phys_cursor_on_p;
22956 int x1;
22957 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22958 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22959 hl, 0);
22960 w->phys_cursor_on_p = on_p;
22961
22962 if (hl == DRAW_CURSOR)
22963 w->phys_cursor_width = x1 - w->phys_cursor.x;
22964 /* When we erase the cursor, and ROW is overlapped by other
22965 rows, make sure that these overlapping parts of other rows
22966 are redrawn. */
22967 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22968 {
22969 w->phys_cursor_width = x1 - w->phys_cursor.x;
22970
22971 if (row > w->current_matrix->rows
22972 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22973 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22974 OVERLAPS_ERASED_CURSOR);
22975
22976 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22977 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22978 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22979 OVERLAPS_ERASED_CURSOR);
22980 }
22981 }
22982 }
22983
22984
22985 /* EXPORT:
22986 Erase the image of a cursor of window W from the screen. */
22987
22988 void
22989 erase_phys_cursor (w)
22990 struct window *w;
22991 {
22992 struct frame *f = XFRAME (w->frame);
22993 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22994 int hpos = w->phys_cursor.hpos;
22995 int vpos = w->phys_cursor.vpos;
22996 int mouse_face_here_p = 0;
22997 struct glyph_matrix *active_glyphs = w->current_matrix;
22998 struct glyph_row *cursor_row;
22999 struct glyph *cursor_glyph;
23000 enum draw_glyphs_face hl;
23001
23002 /* No cursor displayed or row invalidated => nothing to do on the
23003 screen. */
23004 if (w->phys_cursor_type == NO_CURSOR)
23005 goto mark_cursor_off;
23006
23007 /* VPOS >= active_glyphs->nrows means that window has been resized.
23008 Don't bother to erase the cursor. */
23009 if (vpos >= active_glyphs->nrows)
23010 goto mark_cursor_off;
23011
23012 /* If row containing cursor is marked invalid, there is nothing we
23013 can do. */
23014 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23015 if (!cursor_row->enabled_p)
23016 goto mark_cursor_off;
23017
23018 /* If line spacing is > 0, old cursor may only be partially visible in
23019 window after split-window. So adjust visible height. */
23020 cursor_row->visible_height = min (cursor_row->visible_height,
23021 window_text_bottom_y (w) - cursor_row->y);
23022
23023 /* If row is completely invisible, don't attempt to delete a cursor which
23024 isn't there. This can happen if cursor is at top of a window, and
23025 we switch to a buffer with a header line in that window. */
23026 if (cursor_row->visible_height <= 0)
23027 goto mark_cursor_off;
23028
23029 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23030 if (cursor_row->cursor_in_fringe_p)
23031 {
23032 cursor_row->cursor_in_fringe_p = 0;
23033 draw_fringe_bitmap (w, cursor_row, 0);
23034 goto mark_cursor_off;
23035 }
23036
23037 /* This can happen when the new row is shorter than the old one.
23038 In this case, either draw_glyphs or clear_end_of_line
23039 should have cleared the cursor. Note that we wouldn't be
23040 able to erase the cursor in this case because we don't have a
23041 cursor glyph at hand. */
23042 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
23043 goto mark_cursor_off;
23044
23045 /* If the cursor is in the mouse face area, redisplay that when
23046 we clear the cursor. */
23047 if (! NILP (dpyinfo->mouse_face_window)
23048 && w == XWINDOW (dpyinfo->mouse_face_window)
23049 && (vpos > dpyinfo->mouse_face_beg_row
23050 || (vpos == dpyinfo->mouse_face_beg_row
23051 && hpos >= dpyinfo->mouse_face_beg_col))
23052 && (vpos < dpyinfo->mouse_face_end_row
23053 || (vpos == dpyinfo->mouse_face_end_row
23054 && hpos < dpyinfo->mouse_face_end_col))
23055 /* Don't redraw the cursor's spot in mouse face if it is at the
23056 end of a line (on a newline). The cursor appears there, but
23057 mouse highlighting does not. */
23058 && cursor_row->used[TEXT_AREA] > hpos)
23059 mouse_face_here_p = 1;
23060
23061 /* Maybe clear the display under the cursor. */
23062 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23063 {
23064 int x, y, left_x;
23065 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23066 int width;
23067
23068 cursor_glyph = get_phys_cursor_glyph (w);
23069 if (cursor_glyph == NULL)
23070 goto mark_cursor_off;
23071
23072 width = cursor_glyph->pixel_width;
23073 left_x = window_box_left_offset (w, TEXT_AREA);
23074 x = w->phys_cursor.x;
23075 if (x < left_x)
23076 width -= left_x - x;
23077 width = min (width, window_box_width (w, TEXT_AREA) - x);
23078 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23079 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23080
23081 if (width > 0)
23082 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23083 }
23084
23085 /* Erase the cursor by redrawing the character underneath it. */
23086 if (mouse_face_here_p)
23087 hl = DRAW_MOUSE_FACE;
23088 else
23089 hl = DRAW_NORMAL_TEXT;
23090 draw_phys_cursor_glyph (w, cursor_row, hl);
23091
23092 mark_cursor_off:
23093 w->phys_cursor_on_p = 0;
23094 w->phys_cursor_type = NO_CURSOR;
23095 }
23096
23097
23098 /* EXPORT:
23099 Display or clear cursor of window W. If ON is zero, clear the
23100 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23101 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23102
23103 void
23104 display_and_set_cursor (w, on, hpos, vpos, x, y)
23105 struct window *w;
23106 int on, hpos, vpos, x, y;
23107 {
23108 struct frame *f = XFRAME (w->frame);
23109 int new_cursor_type;
23110 int new_cursor_width;
23111 int active_cursor;
23112 struct glyph_row *glyph_row;
23113 struct glyph *glyph;
23114
23115 /* This is pointless on invisible frames, and dangerous on garbaged
23116 windows and frames; in the latter case, the frame or window may
23117 be in the midst of changing its size, and x and y may be off the
23118 window. */
23119 if (! FRAME_VISIBLE_P (f)
23120 || FRAME_GARBAGED_P (f)
23121 || vpos >= w->current_matrix->nrows
23122 || hpos >= w->current_matrix->matrix_w)
23123 return;
23124
23125 /* If cursor is off and we want it off, return quickly. */
23126 if (!on && !w->phys_cursor_on_p)
23127 return;
23128
23129 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23130 /* If cursor row is not enabled, we don't really know where to
23131 display the cursor. */
23132 if (!glyph_row->enabled_p)
23133 {
23134 w->phys_cursor_on_p = 0;
23135 return;
23136 }
23137
23138 glyph = NULL;
23139 if (!glyph_row->exact_window_width_line_p
23140 || hpos < glyph_row->used[TEXT_AREA])
23141 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23142
23143 xassert (interrupt_input_blocked);
23144
23145 /* Set new_cursor_type to the cursor we want to be displayed. */
23146 new_cursor_type = get_window_cursor_type (w, glyph,
23147 &new_cursor_width, &active_cursor);
23148
23149 /* If cursor is currently being shown and we don't want it to be or
23150 it is in the wrong place, or the cursor type is not what we want,
23151 erase it. */
23152 if (w->phys_cursor_on_p
23153 && (!on
23154 || w->phys_cursor.x != x
23155 || w->phys_cursor.y != y
23156 || new_cursor_type != w->phys_cursor_type
23157 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23158 && new_cursor_width != w->phys_cursor_width)))
23159 erase_phys_cursor (w);
23160
23161 /* Don't check phys_cursor_on_p here because that flag is only set
23162 to zero in some cases where we know that the cursor has been
23163 completely erased, to avoid the extra work of erasing the cursor
23164 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23165 still not be visible, or it has only been partly erased. */
23166 if (on)
23167 {
23168 w->phys_cursor_ascent = glyph_row->ascent;
23169 w->phys_cursor_height = glyph_row->height;
23170
23171 /* Set phys_cursor_.* before x_draw_.* is called because some
23172 of them may need the information. */
23173 w->phys_cursor.x = x;
23174 w->phys_cursor.y = glyph_row->y;
23175 w->phys_cursor.hpos = hpos;
23176 w->phys_cursor.vpos = vpos;
23177 }
23178
23179 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23180 new_cursor_type, new_cursor_width,
23181 on, active_cursor);
23182 }
23183
23184
23185 /* Switch the display of W's cursor on or off, according to the value
23186 of ON. */
23187
23188 #ifndef HAVE_NS
23189 static
23190 #endif
23191 void
23192 update_window_cursor (w, on)
23193 struct window *w;
23194 int on;
23195 {
23196 /* Don't update cursor in windows whose frame is in the process
23197 of being deleted. */
23198 if (w->current_matrix)
23199 {
23200 BLOCK_INPUT;
23201 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23202 w->phys_cursor.x, w->phys_cursor.y);
23203 UNBLOCK_INPUT;
23204 }
23205 }
23206
23207
23208 /* Call update_window_cursor with parameter ON_P on all leaf windows
23209 in the window tree rooted at W. */
23210
23211 static void
23212 update_cursor_in_window_tree (w, on_p)
23213 struct window *w;
23214 int on_p;
23215 {
23216 while (w)
23217 {
23218 if (!NILP (w->hchild))
23219 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23220 else if (!NILP (w->vchild))
23221 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23222 else
23223 update_window_cursor (w, on_p);
23224
23225 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23226 }
23227 }
23228
23229
23230 /* EXPORT:
23231 Display the cursor on window W, or clear it, according to ON_P.
23232 Don't change the cursor's position. */
23233
23234 void
23235 x_update_cursor (f, on_p)
23236 struct frame *f;
23237 int on_p;
23238 {
23239 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23240 }
23241
23242
23243 /* EXPORT:
23244 Clear the cursor of window W to background color, and mark the
23245 cursor as not shown. This is used when the text where the cursor
23246 is about to be rewritten. */
23247
23248 void
23249 x_clear_cursor (w)
23250 struct window *w;
23251 {
23252 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23253 update_window_cursor (w, 0);
23254 }
23255
23256
23257 /* EXPORT:
23258 Display the active region described by mouse_face_* according to DRAW. */
23259
23260 void
23261 show_mouse_face (dpyinfo, draw)
23262 Display_Info *dpyinfo;
23263 enum draw_glyphs_face draw;
23264 {
23265 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
23266 struct frame *f = XFRAME (WINDOW_FRAME (w));
23267
23268 if (/* If window is in the process of being destroyed, don't bother
23269 to do anything. */
23270 w->current_matrix != NULL
23271 /* Don't update mouse highlight if hidden */
23272 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
23273 /* Recognize when we are called to operate on rows that don't exist
23274 anymore. This can happen when a window is split. */
23275 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
23276 {
23277 int phys_cursor_on_p = w->phys_cursor_on_p;
23278 struct glyph_row *row, *first, *last;
23279
23280 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
23281 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
23282
23283 for (row = first; row <= last && row->enabled_p; ++row)
23284 {
23285 int start_hpos, end_hpos, start_x;
23286
23287 /* For all but the first row, the highlight starts at column 0. */
23288 if (row == first)
23289 {
23290 start_hpos = dpyinfo->mouse_face_beg_col;
23291 start_x = dpyinfo->mouse_face_beg_x;
23292 }
23293 else
23294 {
23295 start_hpos = 0;
23296 start_x = 0;
23297 }
23298
23299 if (row == last)
23300 end_hpos = dpyinfo->mouse_face_end_col;
23301 else
23302 {
23303 end_hpos = row->used[TEXT_AREA];
23304 if (draw == DRAW_NORMAL_TEXT)
23305 row->fill_line_p = 1; /* Clear to end of line */
23306 }
23307
23308 if (end_hpos > start_hpos)
23309 {
23310 draw_glyphs (w, start_x, row, TEXT_AREA,
23311 start_hpos, end_hpos,
23312 draw, 0);
23313
23314 row->mouse_face_p
23315 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
23316 }
23317 }
23318
23319 /* When we've written over the cursor, arrange for it to
23320 be displayed again. */
23321 if (phys_cursor_on_p && !w->phys_cursor_on_p)
23322 {
23323 BLOCK_INPUT;
23324 display_and_set_cursor (w, 1,
23325 w->phys_cursor.hpos, w->phys_cursor.vpos,
23326 w->phys_cursor.x, w->phys_cursor.y);
23327 UNBLOCK_INPUT;
23328 }
23329 }
23330
23331 /* Change the mouse cursor. */
23332 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
23333 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23334 else if (draw == DRAW_MOUSE_FACE)
23335 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23336 else
23337 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23338 }
23339
23340 /* EXPORT:
23341 Clear out the mouse-highlighted active region.
23342 Redraw it un-highlighted first. Value is non-zero if mouse
23343 face was actually drawn unhighlighted. */
23344
23345 int
23346 clear_mouse_face (dpyinfo)
23347 Display_Info *dpyinfo;
23348 {
23349 int cleared = 0;
23350
23351 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
23352 {
23353 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
23354 cleared = 1;
23355 }
23356
23357 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23358 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23359 dpyinfo->mouse_face_window = Qnil;
23360 dpyinfo->mouse_face_overlay = Qnil;
23361 return cleared;
23362 }
23363
23364
23365 /* EXPORT:
23366 Non-zero if physical cursor of window W is within mouse face. */
23367
23368 int
23369 cursor_in_mouse_face_p (w)
23370 struct window *w;
23371 {
23372 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23373 int in_mouse_face = 0;
23374
23375 if (WINDOWP (dpyinfo->mouse_face_window)
23376 && XWINDOW (dpyinfo->mouse_face_window) == w)
23377 {
23378 int hpos = w->phys_cursor.hpos;
23379 int vpos = w->phys_cursor.vpos;
23380
23381 if (vpos >= dpyinfo->mouse_face_beg_row
23382 && vpos <= dpyinfo->mouse_face_end_row
23383 && (vpos > dpyinfo->mouse_face_beg_row
23384 || hpos >= dpyinfo->mouse_face_beg_col)
23385 && (vpos < dpyinfo->mouse_face_end_row
23386 || hpos < dpyinfo->mouse_face_end_col
23387 || dpyinfo->mouse_face_past_end))
23388 in_mouse_face = 1;
23389 }
23390
23391 return in_mouse_face;
23392 }
23393
23394
23395
23396 \f
23397 /* This function sets the mouse_face_* elements of DPYINFO, assuming
23398 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23399 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23400 for the overlay or run of text properties specifying the mouse
23401 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23402 before-string and after-string that must also be highlighted.
23403 DISPLAY_STRING, if non-nil, is a display string that may cover some
23404 or all of the highlighted text. */
23405
23406 static void
23407 mouse_face_from_buffer_pos (Lisp_Object window,
23408 Display_Info *dpyinfo,
23409 EMACS_INT mouse_charpos,
23410 EMACS_INT start_charpos,
23411 EMACS_INT end_charpos,
23412 Lisp_Object before_string,
23413 Lisp_Object after_string,
23414 Lisp_Object display_string)
23415 {
23416 struct window *w = XWINDOW (window);
23417 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23418 struct glyph_row *row;
23419 struct glyph *glyph, *end;
23420 EMACS_INT ignore;
23421 int x;
23422
23423 xassert (NILP (display_string) || STRINGP (display_string));
23424 xassert (NILP (before_string) || STRINGP (before_string));
23425 xassert (NILP (after_string) || STRINGP (after_string));
23426
23427 /* Find the first highlighted glyph. */
23428 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
23429 {
23430 dpyinfo->mouse_face_beg_col = 0;
23431 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
23432 dpyinfo->mouse_face_beg_x = first->x;
23433 dpyinfo->mouse_face_beg_y = first->y;
23434 }
23435 else
23436 {
23437 row = row_containing_pos (w, start_charpos, first, NULL, 0);
23438 if (row == NULL)
23439 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23440
23441 /* If the before-string or display-string contains newlines,
23442 row_containing_pos skips to its last row. Move back. */
23443 if (!NILP (before_string) || !NILP (display_string))
23444 {
23445 struct glyph_row *prev;
23446 while ((prev = row - 1, prev >= first)
23447 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
23448 && prev->used[TEXT_AREA] > 0)
23449 {
23450 struct glyph *beg = prev->glyphs[TEXT_AREA];
23451 glyph = beg + prev->used[TEXT_AREA];
23452 while (--glyph >= beg && INTEGERP (glyph->object));
23453 if (glyph < beg
23454 || !(EQ (glyph->object, before_string)
23455 || EQ (glyph->object, display_string)))
23456 break;
23457 row = prev;
23458 }
23459 }
23460
23461 glyph = row->glyphs[TEXT_AREA];
23462 end = glyph + row->used[TEXT_AREA];
23463 x = row->x;
23464 dpyinfo->mouse_face_beg_y = row->y;
23465 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23466
23467 /* Skip truncation glyphs at the start of the glyph row. */
23468 if (row->displays_text_p)
23469 for (; glyph < end
23470 && INTEGERP (glyph->object)
23471 && glyph->charpos < 0;
23472 ++glyph)
23473 x += glyph->pixel_width;
23474
23475 /* Scan the glyph row, stopping before BEFORE_STRING or
23476 DISPLAY_STRING or START_CHARPOS. */
23477 for (; glyph < end
23478 && !INTEGERP (glyph->object)
23479 && !EQ (glyph->object, before_string)
23480 && !EQ (glyph->object, display_string)
23481 && !(BUFFERP (glyph->object)
23482 && glyph->charpos >= start_charpos);
23483 ++glyph)
23484 x += glyph->pixel_width;
23485
23486 dpyinfo->mouse_face_beg_x = x;
23487 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
23488 }
23489
23490 /* Find the last highlighted glyph. */
23491 row = row_containing_pos (w, end_charpos, first, NULL, 0);
23492 if (row == NULL)
23493 {
23494 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23495 dpyinfo->mouse_face_past_end = 1;
23496 }
23497 else if (!NILP (after_string))
23498 {
23499 /* If the after-string has newlines, advance to its last row. */
23500 struct glyph_row *next;
23501 struct glyph_row *last
23502 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23503
23504 for (next = row + 1;
23505 next <= last
23506 && next->used[TEXT_AREA] > 0
23507 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
23508 ++next)
23509 row = next;
23510 }
23511
23512 glyph = row->glyphs[TEXT_AREA];
23513 end = glyph + row->used[TEXT_AREA];
23514 x = row->x;
23515 dpyinfo->mouse_face_end_y = row->y;
23516 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23517
23518 /* Skip truncation glyphs at the start of the row. */
23519 if (row->displays_text_p)
23520 for (; glyph < end
23521 && INTEGERP (glyph->object)
23522 && glyph->charpos < 0;
23523 ++glyph)
23524 x += glyph->pixel_width;
23525
23526 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
23527 AFTER_STRING. */
23528 for (; glyph < end
23529 && !INTEGERP (glyph->object)
23530 && !EQ (glyph->object, after_string)
23531 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
23532 ++glyph)
23533 x += glyph->pixel_width;
23534
23535 /* If we found AFTER_STRING, consume it and stop. */
23536 if (EQ (glyph->object, after_string))
23537 {
23538 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
23539 x += glyph->pixel_width;
23540 }
23541 else
23542 {
23543 /* If there's no after-string, we must check if we overshot,
23544 which might be the case if we stopped after a string glyph.
23545 That glyph may belong to a before-string or display-string
23546 associated with the end position, which must not be
23547 highlighted. */
23548 Lisp_Object prev_object;
23549 EMACS_INT pos;
23550
23551 while (glyph > row->glyphs[TEXT_AREA])
23552 {
23553 prev_object = (glyph - 1)->object;
23554 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23555 break;
23556
23557 pos = string_buffer_position (w, prev_object, end_charpos);
23558 if (pos && pos < end_charpos)
23559 break;
23560
23561 for (; glyph > row->glyphs[TEXT_AREA]
23562 && EQ ((glyph - 1)->object, prev_object);
23563 --glyph)
23564 x -= (glyph - 1)->pixel_width;
23565 }
23566 }
23567
23568 dpyinfo->mouse_face_end_x = x;
23569 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23570 dpyinfo->mouse_face_window = window;
23571 dpyinfo->mouse_face_face_id
23572 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23573 mouse_charpos + 1,
23574 !dpyinfo->mouse_face_hidden, -1);
23575 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23576 }
23577
23578
23579 /* Find the position of the glyph for position POS in OBJECT in
23580 window W's current matrix, and return in *X, *Y the pixel
23581 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23582
23583 RIGHT_P non-zero means return the position of the right edge of the
23584 glyph, RIGHT_P zero means return the left edge position.
23585
23586 If no glyph for POS exists in the matrix, return the position of
23587 the glyph with the next smaller position that is in the matrix, if
23588 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23589 exists in the matrix, return the position of the glyph with the
23590 next larger position in OBJECT.
23591
23592 Value is non-zero if a glyph was found. */
23593
23594 static int
23595 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23596 struct window *w;
23597 EMACS_INT pos;
23598 Lisp_Object object;
23599 int *hpos, *vpos, *x, *y;
23600 int right_p;
23601 {
23602 int yb = window_text_bottom_y (w);
23603 struct glyph_row *r;
23604 struct glyph *best_glyph = NULL;
23605 struct glyph_row *best_row = NULL;
23606 int best_x = 0;
23607
23608 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23609 r->enabled_p && r->y < yb;
23610 ++r)
23611 {
23612 struct glyph *g = r->glyphs[TEXT_AREA];
23613 struct glyph *e = g + r->used[TEXT_AREA];
23614 int gx;
23615
23616 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23617 if (EQ (g->object, object))
23618 {
23619 if (g->charpos == pos)
23620 {
23621 best_glyph = g;
23622 best_x = gx;
23623 best_row = r;
23624 goto found;
23625 }
23626 else if (best_glyph == NULL
23627 || ((eabs (g->charpos - pos)
23628 < eabs (best_glyph->charpos - pos))
23629 && (right_p
23630 ? g->charpos < pos
23631 : g->charpos > pos)))
23632 {
23633 best_glyph = g;
23634 best_x = gx;
23635 best_row = r;
23636 }
23637 }
23638 }
23639
23640 found:
23641
23642 if (best_glyph)
23643 {
23644 *x = best_x;
23645 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23646
23647 if (right_p)
23648 {
23649 *x += best_glyph->pixel_width;
23650 ++*hpos;
23651 }
23652
23653 *y = best_row->y;
23654 *vpos = best_row - w->current_matrix->rows;
23655 }
23656
23657 return best_glyph != NULL;
23658 }
23659
23660
23661 /* See if position X, Y is within a hot-spot of an image. */
23662
23663 static int
23664 on_hot_spot_p (hot_spot, x, y)
23665 Lisp_Object hot_spot;
23666 int x, y;
23667 {
23668 if (!CONSP (hot_spot))
23669 return 0;
23670
23671 if (EQ (XCAR (hot_spot), Qrect))
23672 {
23673 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23674 Lisp_Object rect = XCDR (hot_spot);
23675 Lisp_Object tem;
23676 if (!CONSP (rect))
23677 return 0;
23678 if (!CONSP (XCAR (rect)))
23679 return 0;
23680 if (!CONSP (XCDR (rect)))
23681 return 0;
23682 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23683 return 0;
23684 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23685 return 0;
23686 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23687 return 0;
23688 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23689 return 0;
23690 return 1;
23691 }
23692 else if (EQ (XCAR (hot_spot), Qcircle))
23693 {
23694 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23695 Lisp_Object circ = XCDR (hot_spot);
23696 Lisp_Object lr, lx0, ly0;
23697 if (CONSP (circ)
23698 && CONSP (XCAR (circ))
23699 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23700 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23701 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23702 {
23703 double r = XFLOATINT (lr);
23704 double dx = XINT (lx0) - x;
23705 double dy = XINT (ly0) - y;
23706 return (dx * dx + dy * dy <= r * r);
23707 }
23708 }
23709 else if (EQ (XCAR (hot_spot), Qpoly))
23710 {
23711 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23712 if (VECTORP (XCDR (hot_spot)))
23713 {
23714 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23715 Lisp_Object *poly = v->contents;
23716 int n = v->size;
23717 int i;
23718 int inside = 0;
23719 Lisp_Object lx, ly;
23720 int x0, y0;
23721
23722 /* Need an even number of coordinates, and at least 3 edges. */
23723 if (n < 6 || n & 1)
23724 return 0;
23725
23726 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23727 If count is odd, we are inside polygon. Pixels on edges
23728 may or may not be included depending on actual geometry of the
23729 polygon. */
23730 if ((lx = poly[n-2], !INTEGERP (lx))
23731 || (ly = poly[n-1], !INTEGERP (lx)))
23732 return 0;
23733 x0 = XINT (lx), y0 = XINT (ly);
23734 for (i = 0; i < n; i += 2)
23735 {
23736 int x1 = x0, y1 = y0;
23737 if ((lx = poly[i], !INTEGERP (lx))
23738 || (ly = poly[i+1], !INTEGERP (ly)))
23739 return 0;
23740 x0 = XINT (lx), y0 = XINT (ly);
23741
23742 /* Does this segment cross the X line? */
23743 if (x0 >= x)
23744 {
23745 if (x1 >= x)
23746 continue;
23747 }
23748 else if (x1 < x)
23749 continue;
23750 if (y > y0 && y > y1)
23751 continue;
23752 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23753 inside = !inside;
23754 }
23755 return inside;
23756 }
23757 }
23758 return 0;
23759 }
23760
23761 Lisp_Object
23762 find_hot_spot (map, x, y)
23763 Lisp_Object map;
23764 int x, y;
23765 {
23766 while (CONSP (map))
23767 {
23768 if (CONSP (XCAR (map))
23769 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23770 return XCAR (map);
23771 map = XCDR (map);
23772 }
23773
23774 return Qnil;
23775 }
23776
23777 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23778 3, 3, 0,
23779 doc: /* Lookup in image map MAP coordinates X and Y.
23780 An image map is an alist where each element has the format (AREA ID PLIST).
23781 An AREA is specified as either a rectangle, a circle, or a polygon:
23782 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23783 pixel coordinates of the upper left and bottom right corners.
23784 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23785 and the radius of the circle; r may be a float or integer.
23786 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23787 vector describes one corner in the polygon.
23788 Returns the alist element for the first matching AREA in MAP. */)
23789 (map, x, y)
23790 Lisp_Object map;
23791 Lisp_Object x, y;
23792 {
23793 if (NILP (map))
23794 return Qnil;
23795
23796 CHECK_NUMBER (x);
23797 CHECK_NUMBER (y);
23798
23799 return find_hot_spot (map, XINT (x), XINT (y));
23800 }
23801
23802
23803 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23804 static void
23805 define_frame_cursor1 (f, cursor, pointer)
23806 struct frame *f;
23807 Cursor cursor;
23808 Lisp_Object pointer;
23809 {
23810 /* Do not change cursor shape while dragging mouse. */
23811 if (!NILP (do_mouse_tracking))
23812 return;
23813
23814 if (!NILP (pointer))
23815 {
23816 if (EQ (pointer, Qarrow))
23817 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23818 else if (EQ (pointer, Qhand))
23819 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23820 else if (EQ (pointer, Qtext))
23821 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23822 else if (EQ (pointer, intern ("hdrag")))
23823 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23824 #ifdef HAVE_X_WINDOWS
23825 else if (EQ (pointer, intern ("vdrag")))
23826 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23827 #endif
23828 else if (EQ (pointer, intern ("hourglass")))
23829 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23830 else if (EQ (pointer, Qmodeline))
23831 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23832 else
23833 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23834 }
23835
23836 if (cursor != No_Cursor)
23837 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23838 }
23839
23840 /* Take proper action when mouse has moved to the mode or header line
23841 or marginal area AREA of window W, x-position X and y-position Y.
23842 X is relative to the start of the text display area of W, so the
23843 width of bitmap areas and scroll bars must be subtracted to get a
23844 position relative to the start of the mode line. */
23845
23846 static void
23847 note_mode_line_or_margin_highlight (window, x, y, area)
23848 Lisp_Object window;
23849 int x, y;
23850 enum window_part area;
23851 {
23852 struct window *w = XWINDOW (window);
23853 struct frame *f = XFRAME (w->frame);
23854 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23855 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23856 Lisp_Object pointer = Qnil;
23857 int charpos, dx, dy, width, height;
23858 Lisp_Object string, object = Qnil;
23859 Lisp_Object pos, help;
23860
23861 Lisp_Object mouse_face;
23862 int original_x_pixel = x;
23863 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23864 struct glyph_row *row;
23865
23866 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23867 {
23868 int x0;
23869 struct glyph *end;
23870
23871 string = mode_line_string (w, area, &x, &y, &charpos,
23872 &object, &dx, &dy, &width, &height);
23873
23874 row = (area == ON_MODE_LINE
23875 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23876 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23877
23878 /* Find glyph */
23879 if (row->mode_line_p && row->enabled_p)
23880 {
23881 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23882 end = glyph + row->used[TEXT_AREA];
23883
23884 for (x0 = original_x_pixel;
23885 glyph < end && x0 >= glyph->pixel_width;
23886 ++glyph)
23887 x0 -= glyph->pixel_width;
23888
23889 if (glyph >= end)
23890 glyph = NULL;
23891 }
23892 }
23893 else
23894 {
23895 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23896 string = marginal_area_string (w, area, &x, &y, &charpos,
23897 &object, &dx, &dy, &width, &height);
23898 }
23899
23900 help = Qnil;
23901
23902 if (IMAGEP (object))
23903 {
23904 Lisp_Object image_map, hotspot;
23905 if ((image_map = Fplist_get (XCDR (object), QCmap),
23906 !NILP (image_map))
23907 && (hotspot = find_hot_spot (image_map, dx, dy),
23908 CONSP (hotspot))
23909 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23910 {
23911 Lisp_Object area_id, plist;
23912
23913 area_id = XCAR (hotspot);
23914 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23915 If so, we could look for mouse-enter, mouse-leave
23916 properties in PLIST (and do something...). */
23917 hotspot = XCDR (hotspot);
23918 if (CONSP (hotspot)
23919 && (plist = XCAR (hotspot), CONSP (plist)))
23920 {
23921 pointer = Fplist_get (plist, Qpointer);
23922 if (NILP (pointer))
23923 pointer = Qhand;
23924 help = Fplist_get (plist, Qhelp_echo);
23925 if (!NILP (help))
23926 {
23927 help_echo_string = help;
23928 /* Is this correct? ++kfs */
23929 XSETWINDOW (help_echo_window, w);
23930 help_echo_object = w->buffer;
23931 help_echo_pos = charpos;
23932 }
23933 }
23934 }
23935 if (NILP (pointer))
23936 pointer = Fplist_get (XCDR (object), QCpointer);
23937 }
23938
23939 if (STRINGP (string))
23940 {
23941 pos = make_number (charpos);
23942 /* If we're on a string with `help-echo' text property, arrange
23943 for the help to be displayed. This is done by setting the
23944 global variable help_echo_string to the help string. */
23945 if (NILP (help))
23946 {
23947 help = Fget_text_property (pos, Qhelp_echo, string);
23948 if (!NILP (help))
23949 {
23950 help_echo_string = help;
23951 XSETWINDOW (help_echo_window, w);
23952 help_echo_object = string;
23953 help_echo_pos = charpos;
23954 }
23955 }
23956
23957 if (NILP (pointer))
23958 pointer = Fget_text_property (pos, Qpointer, string);
23959
23960 /* Change the mouse pointer according to what is under X/Y. */
23961 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23962 {
23963 Lisp_Object map;
23964 map = Fget_text_property (pos, Qlocal_map, string);
23965 if (!KEYMAPP (map))
23966 map = Fget_text_property (pos, Qkeymap, string);
23967 if (!KEYMAPP (map))
23968 cursor = dpyinfo->vertical_scroll_bar_cursor;
23969 }
23970
23971 /* Change the mouse face according to what is under X/Y. */
23972 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23973 if (!NILP (mouse_face)
23974 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23975 && glyph)
23976 {
23977 Lisp_Object b, e;
23978
23979 struct glyph * tmp_glyph;
23980
23981 int gpos;
23982 int gseq_length;
23983 int total_pixel_width;
23984 EMACS_INT ignore;
23985
23986 int vpos, hpos;
23987
23988 b = Fprevious_single_property_change (make_number (charpos + 1),
23989 Qmouse_face, string, Qnil);
23990 if (NILP (b))
23991 b = make_number (0);
23992
23993 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23994 if (NILP (e))
23995 e = make_number (SCHARS (string));
23996
23997 /* Calculate the position(glyph position: GPOS) of GLYPH in
23998 displayed string. GPOS is different from CHARPOS.
23999
24000 CHARPOS is the position of glyph in internal string
24001 object. A mode line string format has structures which
24002 is converted to a flatten by emacs lisp interpreter.
24003 The internal string is an element of the structures.
24004 The displayed string is the flatten string. */
24005 gpos = 0;
24006 if (glyph > row_start_glyph)
24007 {
24008 tmp_glyph = glyph - 1;
24009 while (tmp_glyph >= row_start_glyph
24010 && tmp_glyph->charpos >= XINT (b)
24011 && EQ (tmp_glyph->object, glyph->object))
24012 {
24013 tmp_glyph--;
24014 gpos++;
24015 }
24016 }
24017
24018 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
24019 displayed string holding GLYPH.
24020
24021 GSEQ_LENGTH is different from SCHARS (STRING).
24022 SCHARS (STRING) returns the length of the internal string. */
24023 for (tmp_glyph = glyph, gseq_length = gpos;
24024 tmp_glyph->charpos < XINT (e);
24025 tmp_glyph++, gseq_length++)
24026 {
24027 if (!EQ (tmp_glyph->object, glyph->object))
24028 break;
24029 }
24030
24031 total_pixel_width = 0;
24032 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
24033 total_pixel_width += tmp_glyph->pixel_width;
24034
24035 /* Pre calculation of re-rendering position */
24036 vpos = (x - gpos);
24037 hpos = (area == ON_MODE_LINE
24038 ? (w->current_matrix)->nrows - 1
24039 : 0);
24040
24041 /* If the re-rendering position is included in the last
24042 re-rendering area, we should do nothing. */
24043 if ( EQ (window, dpyinfo->mouse_face_window)
24044 && dpyinfo->mouse_face_beg_col <= vpos
24045 && vpos < dpyinfo->mouse_face_end_col
24046 && dpyinfo->mouse_face_beg_row == hpos )
24047 return;
24048
24049 if (clear_mouse_face (dpyinfo))
24050 cursor = No_Cursor;
24051
24052 dpyinfo->mouse_face_beg_col = vpos;
24053 dpyinfo->mouse_face_beg_row = hpos;
24054
24055 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
24056 dpyinfo->mouse_face_beg_y = 0;
24057
24058 dpyinfo->mouse_face_end_col = vpos + gseq_length;
24059 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
24060
24061 dpyinfo->mouse_face_end_x = 0;
24062 dpyinfo->mouse_face_end_y = 0;
24063
24064 dpyinfo->mouse_face_past_end = 0;
24065 dpyinfo->mouse_face_window = window;
24066
24067 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
24068 charpos,
24069 0, 0, 0, &ignore,
24070 glyph->face_id, 1);
24071 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24072
24073 if (NILP (pointer))
24074 pointer = Qhand;
24075 }
24076 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24077 clear_mouse_face (dpyinfo);
24078 }
24079 define_frame_cursor1 (f, cursor, pointer);
24080 }
24081
24082
24083 /* EXPORT:
24084 Take proper action when the mouse has moved to position X, Y on
24085 frame F as regards highlighting characters that have mouse-face
24086 properties. Also de-highlighting chars where the mouse was before.
24087 X and Y can be negative or out of range. */
24088
24089 void
24090 note_mouse_highlight (f, x, y)
24091 struct frame *f;
24092 int x, y;
24093 {
24094 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24095 enum window_part part;
24096 Lisp_Object window;
24097 struct window *w;
24098 Cursor cursor = No_Cursor;
24099 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
24100 struct buffer *b;
24101
24102 /* When a menu is active, don't highlight because this looks odd. */
24103 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
24104 if (popup_activated ())
24105 return;
24106 #endif
24107
24108 if (NILP (Vmouse_highlight)
24109 || !f->glyphs_initialized_p)
24110 return;
24111
24112 dpyinfo->mouse_face_mouse_x = x;
24113 dpyinfo->mouse_face_mouse_y = y;
24114 dpyinfo->mouse_face_mouse_frame = f;
24115
24116 if (dpyinfo->mouse_face_defer)
24117 return;
24118
24119 if (gc_in_progress)
24120 {
24121 dpyinfo->mouse_face_deferred_gc = 1;
24122 return;
24123 }
24124
24125 /* Which window is that in? */
24126 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
24127
24128 /* If we were displaying active text in another window, clear that.
24129 Also clear if we move out of text area in same window. */
24130 if (! EQ (window, dpyinfo->mouse_face_window)
24131 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
24132 && !NILP (dpyinfo->mouse_face_window)))
24133 clear_mouse_face (dpyinfo);
24134
24135 /* Not on a window -> return. */
24136 if (!WINDOWP (window))
24137 return;
24138
24139 /* Reset help_echo_string. It will get recomputed below. */
24140 help_echo_string = Qnil;
24141
24142 /* Convert to window-relative pixel coordinates. */
24143 w = XWINDOW (window);
24144 frame_to_window_pixel_xy (w, &x, &y);
24145
24146 /* Handle tool-bar window differently since it doesn't display a
24147 buffer. */
24148 if (EQ (window, f->tool_bar_window))
24149 {
24150 note_tool_bar_highlight (f, x, y);
24151 return;
24152 }
24153
24154 /* Mouse is on the mode, header line or margin? */
24155 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
24156 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
24157 {
24158 note_mode_line_or_margin_highlight (window, x, y, part);
24159 return;
24160 }
24161
24162 if (part == ON_VERTICAL_BORDER)
24163 {
24164 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24165 help_echo_string = build_string ("drag-mouse-1: resize");
24166 }
24167 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
24168 || part == ON_SCROLL_BAR)
24169 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24170 else
24171 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24172
24173 /* Are we in a window whose display is up to date?
24174 And verify the buffer's text has not changed. */
24175 b = XBUFFER (w->buffer);
24176 if (part == ON_TEXT
24177 && EQ (w->window_end_valid, w->buffer)
24178 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
24179 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
24180 {
24181 int hpos, vpos, i, dx, dy, area;
24182 EMACS_INT pos;
24183 struct glyph *glyph;
24184 Lisp_Object object;
24185 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
24186 Lisp_Object *overlay_vec = NULL;
24187 int noverlays;
24188 struct buffer *obuf;
24189 int obegv, ozv, same_region;
24190
24191 /* Find the glyph under X/Y. */
24192 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
24193
24194 /* Look for :pointer property on image. */
24195 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24196 {
24197 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24198 if (img != NULL && IMAGEP (img->spec))
24199 {
24200 Lisp_Object image_map, hotspot;
24201 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
24202 !NILP (image_map))
24203 && (hotspot = find_hot_spot (image_map,
24204 glyph->slice.x + dx,
24205 glyph->slice.y + dy),
24206 CONSP (hotspot))
24207 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24208 {
24209 Lisp_Object area_id, plist;
24210
24211 area_id = XCAR (hotspot);
24212 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24213 If so, we could look for mouse-enter, mouse-leave
24214 properties in PLIST (and do something...). */
24215 hotspot = XCDR (hotspot);
24216 if (CONSP (hotspot)
24217 && (plist = XCAR (hotspot), CONSP (plist)))
24218 {
24219 pointer = Fplist_get (plist, Qpointer);
24220 if (NILP (pointer))
24221 pointer = Qhand;
24222 help_echo_string = Fplist_get (plist, Qhelp_echo);
24223 if (!NILP (help_echo_string))
24224 {
24225 help_echo_window = window;
24226 help_echo_object = glyph->object;
24227 help_echo_pos = glyph->charpos;
24228 }
24229 }
24230 }
24231 if (NILP (pointer))
24232 pointer = Fplist_get (XCDR (img->spec), QCpointer);
24233 }
24234 }
24235
24236 /* Clear mouse face if X/Y not over text. */
24237 if (glyph == NULL
24238 || area != TEXT_AREA
24239 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
24240 {
24241 if (clear_mouse_face (dpyinfo))
24242 cursor = No_Cursor;
24243 if (NILP (pointer))
24244 {
24245 if (area != TEXT_AREA)
24246 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24247 else
24248 pointer = Vvoid_text_area_pointer;
24249 }
24250 goto set_cursor;
24251 }
24252
24253 pos = glyph->charpos;
24254 object = glyph->object;
24255 if (!STRINGP (object) && !BUFFERP (object))
24256 goto set_cursor;
24257
24258 /* If we get an out-of-range value, return now; avoid an error. */
24259 if (BUFFERP (object) && pos > BUF_Z (b))
24260 goto set_cursor;
24261
24262 /* Make the window's buffer temporarily current for
24263 overlays_at and compute_char_face. */
24264 obuf = current_buffer;
24265 current_buffer = b;
24266 obegv = BEGV;
24267 ozv = ZV;
24268 BEGV = BEG;
24269 ZV = Z;
24270
24271 /* Is this char mouse-active or does it have help-echo? */
24272 position = make_number (pos);
24273
24274 if (BUFFERP (object))
24275 {
24276 /* Put all the overlays we want in a vector in overlay_vec. */
24277 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
24278 /* Sort overlays into increasing priority order. */
24279 noverlays = sort_overlays (overlay_vec, noverlays, w);
24280 }
24281 else
24282 noverlays = 0;
24283
24284 same_region = (EQ (window, dpyinfo->mouse_face_window)
24285 && vpos >= dpyinfo->mouse_face_beg_row
24286 && vpos <= dpyinfo->mouse_face_end_row
24287 && (vpos > dpyinfo->mouse_face_beg_row
24288 || hpos >= dpyinfo->mouse_face_beg_col)
24289 && (vpos < dpyinfo->mouse_face_end_row
24290 || hpos < dpyinfo->mouse_face_end_col
24291 || dpyinfo->mouse_face_past_end));
24292
24293 if (same_region)
24294 cursor = No_Cursor;
24295
24296 /* Check mouse-face highlighting. */
24297 if (! same_region
24298 /* If there exists an overlay with mouse-face overlapping
24299 the one we are currently highlighting, we have to
24300 check if we enter the overlapping overlay, and then
24301 highlight only that. */
24302 || (OVERLAYP (dpyinfo->mouse_face_overlay)
24303 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
24304 {
24305 /* Find the highest priority overlay with a mouse-face. */
24306 overlay = Qnil;
24307 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
24308 {
24309 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
24310 if (!NILP (mouse_face))
24311 overlay = overlay_vec[i];
24312 }
24313
24314 /* If we're highlighting the same overlay as before, there's
24315 no need to do that again. */
24316 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
24317 goto check_help_echo;
24318 dpyinfo->mouse_face_overlay = overlay;
24319
24320 /* Clear the display of the old active region, if any. */
24321 if (clear_mouse_face (dpyinfo))
24322 cursor = No_Cursor;
24323
24324 /* If no overlay applies, get a text property. */
24325 if (NILP (overlay))
24326 mouse_face = Fget_text_property (position, Qmouse_face, object);
24327
24328 /* Next, compute the bounds of the mouse highlighting and
24329 display it. */
24330 if (!NILP (mouse_face) && STRINGP (object))
24331 {
24332 /* The mouse-highlighting comes from a display string
24333 with a mouse-face. */
24334 Lisp_Object b, e;
24335 EMACS_INT ignore;
24336
24337 b = Fprevious_single_property_change
24338 (make_number (pos + 1), Qmouse_face, object, Qnil);
24339 e = Fnext_single_property_change
24340 (position, Qmouse_face, object, Qnil);
24341 if (NILP (b))
24342 b = make_number (0);
24343 if (NILP (e))
24344 e = make_number (SCHARS (object) - 1);
24345
24346 fast_find_string_pos (w, XINT (b), object,
24347 &dpyinfo->mouse_face_beg_col,
24348 &dpyinfo->mouse_face_beg_row,
24349 &dpyinfo->mouse_face_beg_x,
24350 &dpyinfo->mouse_face_beg_y, 0);
24351 fast_find_string_pos (w, XINT (e), object,
24352 &dpyinfo->mouse_face_end_col,
24353 &dpyinfo->mouse_face_end_row,
24354 &dpyinfo->mouse_face_end_x,
24355 &dpyinfo->mouse_face_end_y, 1);
24356 dpyinfo->mouse_face_past_end = 0;
24357 dpyinfo->mouse_face_window = window;
24358 dpyinfo->mouse_face_face_id
24359 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
24360 glyph->face_id, 1);
24361 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24362 cursor = No_Cursor;
24363 }
24364 else
24365 {
24366 /* The mouse-highlighting, if any, comes from an overlay
24367 or text property in the buffer. */
24368 Lisp_Object buffer, display_string;
24369
24370 if (STRINGP (object))
24371 {
24372 /* If we are on a display string with no mouse-face,
24373 check if the text under it has one. */
24374 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
24375 int start = MATRIX_ROW_START_CHARPOS (r);
24376 pos = string_buffer_position (w, object, start);
24377 if (pos > 0)
24378 {
24379 mouse_face = get_char_property_and_overlay
24380 (make_number (pos), Qmouse_face, w->buffer, &overlay);
24381 buffer = w->buffer;
24382 display_string = object;
24383 }
24384 }
24385 else
24386 {
24387 buffer = object;
24388 display_string = Qnil;
24389 }
24390
24391 if (!NILP (mouse_face))
24392 {
24393 Lisp_Object before, after;
24394 Lisp_Object before_string, after_string;
24395
24396 if (NILP (overlay))
24397 {
24398 /* Handle the text property case. */
24399 before = Fprevious_single_property_change
24400 (make_number (pos + 1), Qmouse_face, buffer,
24401 Fmarker_position (w->start));
24402 after = Fnext_single_property_change
24403 (make_number (pos), Qmouse_face, buffer,
24404 make_number (BUF_Z (XBUFFER (buffer))
24405 - XFASTINT (w->window_end_pos)));
24406 before_string = after_string = Qnil;
24407 }
24408 else
24409 {
24410 /* Handle the overlay case. */
24411 before = Foverlay_start (overlay);
24412 after = Foverlay_end (overlay);
24413 before_string = Foverlay_get (overlay, Qbefore_string);
24414 after_string = Foverlay_get (overlay, Qafter_string);
24415
24416 if (!STRINGP (before_string)) before_string = Qnil;
24417 if (!STRINGP (after_string)) after_string = Qnil;
24418 }
24419
24420 mouse_face_from_buffer_pos (window, dpyinfo, pos,
24421 XFASTINT (before),
24422 XFASTINT (after),
24423 before_string, after_string,
24424 display_string);
24425 cursor = No_Cursor;
24426 }
24427 }
24428 }
24429
24430 check_help_echo:
24431
24432 /* Look for a `help-echo' property. */
24433 if (NILP (help_echo_string)) {
24434 Lisp_Object help, overlay;
24435
24436 /* Check overlays first. */
24437 help = overlay = Qnil;
24438 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24439 {
24440 overlay = overlay_vec[i];
24441 help = Foverlay_get (overlay, Qhelp_echo);
24442 }
24443
24444 if (!NILP (help))
24445 {
24446 help_echo_string = help;
24447 help_echo_window = window;
24448 help_echo_object = overlay;
24449 help_echo_pos = pos;
24450 }
24451 else
24452 {
24453 Lisp_Object object = glyph->object;
24454 int charpos = glyph->charpos;
24455
24456 /* Try text properties. */
24457 if (STRINGP (object)
24458 && charpos >= 0
24459 && charpos < SCHARS (object))
24460 {
24461 help = Fget_text_property (make_number (charpos),
24462 Qhelp_echo, object);
24463 if (NILP (help))
24464 {
24465 /* If the string itself doesn't specify a help-echo,
24466 see if the buffer text ``under'' it does. */
24467 struct glyph_row *r
24468 = MATRIX_ROW (w->current_matrix, vpos);
24469 int start = MATRIX_ROW_START_CHARPOS (r);
24470 EMACS_INT pos = string_buffer_position (w, object, start);
24471 if (pos > 0)
24472 {
24473 help = Fget_char_property (make_number (pos),
24474 Qhelp_echo, w->buffer);
24475 if (!NILP (help))
24476 {
24477 charpos = pos;
24478 object = w->buffer;
24479 }
24480 }
24481 }
24482 }
24483 else if (BUFFERP (object)
24484 && charpos >= BEGV
24485 && charpos < ZV)
24486 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24487 object);
24488
24489 if (!NILP (help))
24490 {
24491 help_echo_string = help;
24492 help_echo_window = window;
24493 help_echo_object = object;
24494 help_echo_pos = charpos;
24495 }
24496 }
24497 }
24498
24499 /* Look for a `pointer' property. */
24500 if (NILP (pointer))
24501 {
24502 /* Check overlays first. */
24503 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24504 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24505
24506 if (NILP (pointer))
24507 {
24508 Lisp_Object object = glyph->object;
24509 int charpos = glyph->charpos;
24510
24511 /* Try text properties. */
24512 if (STRINGP (object)
24513 && charpos >= 0
24514 && charpos < SCHARS (object))
24515 {
24516 pointer = Fget_text_property (make_number (charpos),
24517 Qpointer, object);
24518 if (NILP (pointer))
24519 {
24520 /* If the string itself doesn't specify a pointer,
24521 see if the buffer text ``under'' it does. */
24522 struct glyph_row *r
24523 = MATRIX_ROW (w->current_matrix, vpos);
24524 int start = MATRIX_ROW_START_CHARPOS (r);
24525 EMACS_INT pos = string_buffer_position (w, object,
24526 start);
24527 if (pos > 0)
24528 pointer = Fget_char_property (make_number (pos),
24529 Qpointer, w->buffer);
24530 }
24531 }
24532 else if (BUFFERP (object)
24533 && charpos >= BEGV
24534 && charpos < ZV)
24535 pointer = Fget_text_property (make_number (charpos),
24536 Qpointer, object);
24537 }
24538 }
24539
24540 BEGV = obegv;
24541 ZV = ozv;
24542 current_buffer = obuf;
24543 }
24544
24545 set_cursor:
24546
24547 define_frame_cursor1 (f, cursor, pointer);
24548 }
24549
24550
24551 /* EXPORT for RIF:
24552 Clear any mouse-face on window W. This function is part of the
24553 redisplay interface, and is called from try_window_id and similar
24554 functions to ensure the mouse-highlight is off. */
24555
24556 void
24557 x_clear_window_mouse_face (w)
24558 struct window *w;
24559 {
24560 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24561 Lisp_Object window;
24562
24563 BLOCK_INPUT;
24564 XSETWINDOW (window, w);
24565 if (EQ (window, dpyinfo->mouse_face_window))
24566 clear_mouse_face (dpyinfo);
24567 UNBLOCK_INPUT;
24568 }
24569
24570
24571 /* EXPORT:
24572 Just discard the mouse face information for frame F, if any.
24573 This is used when the size of F is changed. */
24574
24575 void
24576 cancel_mouse_face (f)
24577 struct frame *f;
24578 {
24579 Lisp_Object window;
24580 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24581
24582 window = dpyinfo->mouse_face_window;
24583 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24584 {
24585 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24586 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24587 dpyinfo->mouse_face_window = Qnil;
24588 }
24589 }
24590
24591
24592 #endif /* HAVE_WINDOW_SYSTEM */
24593
24594 \f
24595 /***********************************************************************
24596 Exposure Events
24597 ***********************************************************************/
24598
24599 #ifdef HAVE_WINDOW_SYSTEM
24600
24601 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24602 which intersects rectangle R. R is in window-relative coordinates. */
24603
24604 static void
24605 expose_area (w, row, r, area)
24606 struct window *w;
24607 struct glyph_row *row;
24608 XRectangle *r;
24609 enum glyph_row_area area;
24610 {
24611 struct glyph *first = row->glyphs[area];
24612 struct glyph *end = row->glyphs[area] + row->used[area];
24613 struct glyph *last;
24614 int first_x, start_x, x;
24615
24616 if (area == TEXT_AREA && row->fill_line_p)
24617 /* If row extends face to end of line write the whole line. */
24618 draw_glyphs (w, 0, row, area,
24619 0, row->used[area],
24620 DRAW_NORMAL_TEXT, 0);
24621 else
24622 {
24623 /* Set START_X to the window-relative start position for drawing glyphs of
24624 AREA. The first glyph of the text area can be partially visible.
24625 The first glyphs of other areas cannot. */
24626 start_x = window_box_left_offset (w, area);
24627 x = start_x;
24628 if (area == TEXT_AREA)
24629 x += row->x;
24630
24631 /* Find the first glyph that must be redrawn. */
24632 while (first < end
24633 && x + first->pixel_width < r->x)
24634 {
24635 x += first->pixel_width;
24636 ++first;
24637 }
24638
24639 /* Find the last one. */
24640 last = first;
24641 first_x = x;
24642 while (last < end
24643 && x < r->x + r->width)
24644 {
24645 x += last->pixel_width;
24646 ++last;
24647 }
24648
24649 /* Repaint. */
24650 if (last > first)
24651 draw_glyphs (w, first_x - start_x, row, area,
24652 first - row->glyphs[area], last - row->glyphs[area],
24653 DRAW_NORMAL_TEXT, 0);
24654 }
24655 }
24656
24657
24658 /* Redraw the parts of the glyph row ROW on window W intersecting
24659 rectangle R. R is in window-relative coordinates. Value is
24660 non-zero if mouse-face was overwritten. */
24661
24662 static int
24663 expose_line (w, row, r)
24664 struct window *w;
24665 struct glyph_row *row;
24666 XRectangle *r;
24667 {
24668 xassert (row->enabled_p);
24669
24670 if (row->mode_line_p || w->pseudo_window_p)
24671 draw_glyphs (w, 0, row, TEXT_AREA,
24672 0, row->used[TEXT_AREA],
24673 DRAW_NORMAL_TEXT, 0);
24674 else
24675 {
24676 if (row->used[LEFT_MARGIN_AREA])
24677 expose_area (w, row, r, LEFT_MARGIN_AREA);
24678 if (row->used[TEXT_AREA])
24679 expose_area (w, row, r, TEXT_AREA);
24680 if (row->used[RIGHT_MARGIN_AREA])
24681 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24682 draw_row_fringe_bitmaps (w, row);
24683 }
24684
24685 return row->mouse_face_p;
24686 }
24687
24688
24689 /* Redraw those parts of glyphs rows during expose event handling that
24690 overlap other rows. Redrawing of an exposed line writes over parts
24691 of lines overlapping that exposed line; this function fixes that.
24692
24693 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24694 row in W's current matrix that is exposed and overlaps other rows.
24695 LAST_OVERLAPPING_ROW is the last such row. */
24696
24697 static void
24698 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24699 struct window *w;
24700 struct glyph_row *first_overlapping_row;
24701 struct glyph_row *last_overlapping_row;
24702 XRectangle *r;
24703 {
24704 struct glyph_row *row;
24705
24706 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24707 if (row->overlapping_p)
24708 {
24709 xassert (row->enabled_p && !row->mode_line_p);
24710
24711 row->clip = r;
24712 if (row->used[LEFT_MARGIN_AREA])
24713 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24714
24715 if (row->used[TEXT_AREA])
24716 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24717
24718 if (row->used[RIGHT_MARGIN_AREA])
24719 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24720 row->clip = NULL;
24721 }
24722 }
24723
24724
24725 /* Return non-zero if W's cursor intersects rectangle R. */
24726
24727 static int
24728 phys_cursor_in_rect_p (w, r)
24729 struct window *w;
24730 XRectangle *r;
24731 {
24732 XRectangle cr, result;
24733 struct glyph *cursor_glyph;
24734 struct glyph_row *row;
24735
24736 if (w->phys_cursor.vpos >= 0
24737 && w->phys_cursor.vpos < w->current_matrix->nrows
24738 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24739 row->enabled_p)
24740 && row->cursor_in_fringe_p)
24741 {
24742 /* Cursor is in the fringe. */
24743 cr.x = window_box_right_offset (w,
24744 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24745 ? RIGHT_MARGIN_AREA
24746 : TEXT_AREA));
24747 cr.y = row->y;
24748 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24749 cr.height = row->height;
24750 return x_intersect_rectangles (&cr, r, &result);
24751 }
24752
24753 cursor_glyph = get_phys_cursor_glyph (w);
24754 if (cursor_glyph)
24755 {
24756 /* r is relative to W's box, but w->phys_cursor.x is relative
24757 to left edge of W's TEXT area. Adjust it. */
24758 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24759 cr.y = w->phys_cursor.y;
24760 cr.width = cursor_glyph->pixel_width;
24761 cr.height = w->phys_cursor_height;
24762 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24763 I assume the effect is the same -- and this is portable. */
24764 return x_intersect_rectangles (&cr, r, &result);
24765 }
24766 /* If we don't understand the format, pretend we're not in the hot-spot. */
24767 return 0;
24768 }
24769
24770
24771 /* EXPORT:
24772 Draw a vertical window border to the right of window W if W doesn't
24773 have vertical scroll bars. */
24774
24775 void
24776 x_draw_vertical_border (w)
24777 struct window *w;
24778 {
24779 struct frame *f = XFRAME (WINDOW_FRAME (w));
24780
24781 /* We could do better, if we knew what type of scroll-bar the adjacent
24782 windows (on either side) have... But we don't :-(
24783 However, I think this works ok. ++KFS 2003-04-25 */
24784
24785 /* Redraw borders between horizontally adjacent windows. Don't
24786 do it for frames with vertical scroll bars because either the
24787 right scroll bar of a window, or the left scroll bar of its
24788 neighbor will suffice as a border. */
24789 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24790 return;
24791
24792 if (!WINDOW_RIGHTMOST_P (w)
24793 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24794 {
24795 int x0, x1, y0, y1;
24796
24797 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24798 y1 -= 1;
24799
24800 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24801 x1 -= 1;
24802
24803 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24804 }
24805 else if (!WINDOW_LEFTMOST_P (w)
24806 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24807 {
24808 int x0, x1, y0, y1;
24809
24810 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24811 y1 -= 1;
24812
24813 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24814 x0 -= 1;
24815
24816 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24817 }
24818 }
24819
24820
24821 /* Redraw the part of window W intersection rectangle FR. Pixel
24822 coordinates in FR are frame-relative. Call this function with
24823 input blocked. Value is non-zero if the exposure overwrites
24824 mouse-face. */
24825
24826 static int
24827 expose_window (w, fr)
24828 struct window *w;
24829 XRectangle *fr;
24830 {
24831 struct frame *f = XFRAME (w->frame);
24832 XRectangle wr, r;
24833 int mouse_face_overwritten_p = 0;
24834
24835 /* If window is not yet fully initialized, do nothing. This can
24836 happen when toolkit scroll bars are used and a window is split.
24837 Reconfiguring the scroll bar will generate an expose for a newly
24838 created window. */
24839 if (w->current_matrix == NULL)
24840 return 0;
24841
24842 /* When we're currently updating the window, display and current
24843 matrix usually don't agree. Arrange for a thorough display
24844 later. */
24845 if (w == updated_window)
24846 {
24847 SET_FRAME_GARBAGED (f);
24848 return 0;
24849 }
24850
24851 /* Frame-relative pixel rectangle of W. */
24852 wr.x = WINDOW_LEFT_EDGE_X (w);
24853 wr.y = WINDOW_TOP_EDGE_Y (w);
24854 wr.width = WINDOW_TOTAL_WIDTH (w);
24855 wr.height = WINDOW_TOTAL_HEIGHT (w);
24856
24857 if (x_intersect_rectangles (fr, &wr, &r))
24858 {
24859 int yb = window_text_bottom_y (w);
24860 struct glyph_row *row;
24861 int cursor_cleared_p;
24862 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24863
24864 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24865 r.x, r.y, r.width, r.height));
24866
24867 /* Convert to window coordinates. */
24868 r.x -= WINDOW_LEFT_EDGE_X (w);
24869 r.y -= WINDOW_TOP_EDGE_Y (w);
24870
24871 /* Turn off the cursor. */
24872 if (!w->pseudo_window_p
24873 && phys_cursor_in_rect_p (w, &r))
24874 {
24875 x_clear_cursor (w);
24876 cursor_cleared_p = 1;
24877 }
24878 else
24879 cursor_cleared_p = 0;
24880
24881 /* Update lines intersecting rectangle R. */
24882 first_overlapping_row = last_overlapping_row = NULL;
24883 for (row = w->current_matrix->rows;
24884 row->enabled_p;
24885 ++row)
24886 {
24887 int y0 = row->y;
24888 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24889
24890 if ((y0 >= r.y && y0 < r.y + r.height)
24891 || (y1 > r.y && y1 < r.y + r.height)
24892 || (r.y >= y0 && r.y < y1)
24893 || (r.y + r.height > y0 && r.y + r.height < y1))
24894 {
24895 /* A header line may be overlapping, but there is no need
24896 to fix overlapping areas for them. KFS 2005-02-12 */
24897 if (row->overlapping_p && !row->mode_line_p)
24898 {
24899 if (first_overlapping_row == NULL)
24900 first_overlapping_row = row;
24901 last_overlapping_row = row;
24902 }
24903
24904 row->clip = fr;
24905 if (expose_line (w, row, &r))
24906 mouse_face_overwritten_p = 1;
24907 row->clip = NULL;
24908 }
24909 else if (row->overlapping_p)
24910 {
24911 /* We must redraw a row overlapping the exposed area. */
24912 if (y0 < r.y
24913 ? y0 + row->phys_height > r.y
24914 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24915 {
24916 if (first_overlapping_row == NULL)
24917 first_overlapping_row = row;
24918 last_overlapping_row = row;
24919 }
24920 }
24921
24922 if (y1 >= yb)
24923 break;
24924 }
24925
24926 /* Display the mode line if there is one. */
24927 if (WINDOW_WANTS_MODELINE_P (w)
24928 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24929 row->enabled_p)
24930 && row->y < r.y + r.height)
24931 {
24932 if (expose_line (w, row, &r))
24933 mouse_face_overwritten_p = 1;
24934 }
24935
24936 if (!w->pseudo_window_p)
24937 {
24938 /* Fix the display of overlapping rows. */
24939 if (first_overlapping_row)
24940 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24941 fr);
24942
24943 /* Draw border between windows. */
24944 x_draw_vertical_border (w);
24945
24946 /* Turn the cursor on again. */
24947 if (cursor_cleared_p)
24948 update_window_cursor (w, 1);
24949 }
24950 }
24951
24952 return mouse_face_overwritten_p;
24953 }
24954
24955
24956
24957 /* Redraw (parts) of all windows in the window tree rooted at W that
24958 intersect R. R contains frame pixel coordinates. Value is
24959 non-zero if the exposure overwrites mouse-face. */
24960
24961 static int
24962 expose_window_tree (w, r)
24963 struct window *w;
24964 XRectangle *r;
24965 {
24966 struct frame *f = XFRAME (w->frame);
24967 int mouse_face_overwritten_p = 0;
24968
24969 while (w && !FRAME_GARBAGED_P (f))
24970 {
24971 if (!NILP (w->hchild))
24972 mouse_face_overwritten_p
24973 |= expose_window_tree (XWINDOW (w->hchild), r);
24974 else if (!NILP (w->vchild))
24975 mouse_face_overwritten_p
24976 |= expose_window_tree (XWINDOW (w->vchild), r);
24977 else
24978 mouse_face_overwritten_p |= expose_window (w, r);
24979
24980 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24981 }
24982
24983 return mouse_face_overwritten_p;
24984 }
24985
24986
24987 /* EXPORT:
24988 Redisplay an exposed area of frame F. X and Y are the upper-left
24989 corner of the exposed rectangle. W and H are width and height of
24990 the exposed area. All are pixel values. W or H zero means redraw
24991 the entire frame. */
24992
24993 void
24994 expose_frame (f, x, y, w, h)
24995 struct frame *f;
24996 int x, y, w, h;
24997 {
24998 XRectangle r;
24999 int mouse_face_overwritten_p = 0;
25000
25001 TRACE ((stderr, "expose_frame "));
25002
25003 /* No need to redraw if frame will be redrawn soon. */
25004 if (FRAME_GARBAGED_P (f))
25005 {
25006 TRACE ((stderr, " garbaged\n"));
25007 return;
25008 }
25009
25010 /* If basic faces haven't been realized yet, there is no point in
25011 trying to redraw anything. This can happen when we get an expose
25012 event while Emacs is starting, e.g. by moving another window. */
25013 if (FRAME_FACE_CACHE (f) == NULL
25014 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
25015 {
25016 TRACE ((stderr, " no faces\n"));
25017 return;
25018 }
25019
25020 if (w == 0 || h == 0)
25021 {
25022 r.x = r.y = 0;
25023 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
25024 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
25025 }
25026 else
25027 {
25028 r.x = x;
25029 r.y = y;
25030 r.width = w;
25031 r.height = h;
25032 }
25033
25034 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
25035 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
25036
25037 if (WINDOWP (f->tool_bar_window))
25038 mouse_face_overwritten_p
25039 |= expose_window (XWINDOW (f->tool_bar_window), &r);
25040
25041 #ifdef HAVE_X_WINDOWS
25042 #ifndef MSDOS
25043 #ifndef USE_X_TOOLKIT
25044 if (WINDOWP (f->menu_bar_window))
25045 mouse_face_overwritten_p
25046 |= expose_window (XWINDOW (f->menu_bar_window), &r);
25047 #endif /* not USE_X_TOOLKIT */
25048 #endif
25049 #endif
25050
25051 /* Some window managers support a focus-follows-mouse style with
25052 delayed raising of frames. Imagine a partially obscured frame,
25053 and moving the mouse into partially obscured mouse-face on that
25054 frame. The visible part of the mouse-face will be highlighted,
25055 then the WM raises the obscured frame. With at least one WM, KDE
25056 2.1, Emacs is not getting any event for the raising of the frame
25057 (even tried with SubstructureRedirectMask), only Expose events.
25058 These expose events will draw text normally, i.e. not
25059 highlighted. Which means we must redo the highlight here.
25060 Subsume it under ``we love X''. --gerd 2001-08-15 */
25061 /* Included in Windows version because Windows most likely does not
25062 do the right thing if any third party tool offers
25063 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
25064 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
25065 {
25066 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25067 if (f == dpyinfo->mouse_face_mouse_frame)
25068 {
25069 int x = dpyinfo->mouse_face_mouse_x;
25070 int y = dpyinfo->mouse_face_mouse_y;
25071 clear_mouse_face (dpyinfo);
25072 note_mouse_highlight (f, x, y);
25073 }
25074 }
25075 }
25076
25077
25078 /* EXPORT:
25079 Determine the intersection of two rectangles R1 and R2. Return
25080 the intersection in *RESULT. Value is non-zero if RESULT is not
25081 empty. */
25082
25083 int
25084 x_intersect_rectangles (r1, r2, result)
25085 XRectangle *r1, *r2, *result;
25086 {
25087 XRectangle *left, *right;
25088 XRectangle *upper, *lower;
25089 int intersection_p = 0;
25090
25091 /* Rearrange so that R1 is the left-most rectangle. */
25092 if (r1->x < r2->x)
25093 left = r1, right = r2;
25094 else
25095 left = r2, right = r1;
25096
25097 /* X0 of the intersection is right.x0, if this is inside R1,
25098 otherwise there is no intersection. */
25099 if (right->x <= left->x + left->width)
25100 {
25101 result->x = right->x;
25102
25103 /* The right end of the intersection is the minimum of the
25104 the right ends of left and right. */
25105 result->width = (min (left->x + left->width, right->x + right->width)
25106 - result->x);
25107
25108 /* Same game for Y. */
25109 if (r1->y < r2->y)
25110 upper = r1, lower = r2;
25111 else
25112 upper = r2, lower = r1;
25113
25114 /* The upper end of the intersection is lower.y0, if this is inside
25115 of upper. Otherwise, there is no intersection. */
25116 if (lower->y <= upper->y + upper->height)
25117 {
25118 result->y = lower->y;
25119
25120 /* The lower end of the intersection is the minimum of the lower
25121 ends of upper and lower. */
25122 result->height = (min (lower->y + lower->height,
25123 upper->y + upper->height)
25124 - result->y);
25125 intersection_p = 1;
25126 }
25127 }
25128
25129 return intersection_p;
25130 }
25131
25132 #endif /* HAVE_WINDOW_SYSTEM */
25133
25134 \f
25135 /***********************************************************************
25136 Initialization
25137 ***********************************************************************/
25138
25139 void
25140 syms_of_xdisp ()
25141 {
25142 Vwith_echo_area_save_vector = Qnil;
25143 staticpro (&Vwith_echo_area_save_vector);
25144
25145 Vmessage_stack = Qnil;
25146 staticpro (&Vmessage_stack);
25147
25148 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
25149 staticpro (&Qinhibit_redisplay);
25150
25151 message_dolog_marker1 = Fmake_marker ();
25152 staticpro (&message_dolog_marker1);
25153 message_dolog_marker2 = Fmake_marker ();
25154 staticpro (&message_dolog_marker2);
25155 message_dolog_marker3 = Fmake_marker ();
25156 staticpro (&message_dolog_marker3);
25157
25158 #if GLYPH_DEBUG
25159 defsubr (&Sdump_frame_glyph_matrix);
25160 defsubr (&Sdump_glyph_matrix);
25161 defsubr (&Sdump_glyph_row);
25162 defsubr (&Sdump_tool_bar_row);
25163 defsubr (&Strace_redisplay);
25164 defsubr (&Strace_to_stderr);
25165 #endif
25166 #ifdef HAVE_WINDOW_SYSTEM
25167 defsubr (&Stool_bar_lines_needed);
25168 defsubr (&Slookup_image_map);
25169 #endif
25170 defsubr (&Sformat_mode_line);
25171 defsubr (&Sinvisible_p);
25172
25173 staticpro (&Qmenu_bar_update_hook);
25174 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
25175
25176 staticpro (&Qoverriding_terminal_local_map);
25177 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
25178
25179 staticpro (&Qoverriding_local_map);
25180 Qoverriding_local_map = intern_c_string ("overriding-local-map");
25181
25182 staticpro (&Qwindow_scroll_functions);
25183 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
25184
25185 staticpro (&Qwindow_text_change_functions);
25186 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
25187
25188 staticpro (&Qredisplay_end_trigger_functions);
25189 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
25190
25191 staticpro (&Qinhibit_point_motion_hooks);
25192 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
25193
25194 Qeval = intern_c_string ("eval");
25195 staticpro (&Qeval);
25196
25197 QCdata = intern_c_string (":data");
25198 staticpro (&QCdata);
25199 Qdisplay = intern_c_string ("display");
25200 staticpro (&Qdisplay);
25201 Qspace_width = intern_c_string ("space-width");
25202 staticpro (&Qspace_width);
25203 Qraise = intern_c_string ("raise");
25204 staticpro (&Qraise);
25205 Qslice = intern_c_string ("slice");
25206 staticpro (&Qslice);
25207 Qspace = intern_c_string ("space");
25208 staticpro (&Qspace);
25209 Qmargin = intern_c_string ("margin");
25210 staticpro (&Qmargin);
25211 Qpointer = intern_c_string ("pointer");
25212 staticpro (&Qpointer);
25213 Qleft_margin = intern_c_string ("left-margin");
25214 staticpro (&Qleft_margin);
25215 Qright_margin = intern_c_string ("right-margin");
25216 staticpro (&Qright_margin);
25217 Qcenter = intern_c_string ("center");
25218 staticpro (&Qcenter);
25219 Qline_height = intern_c_string ("line-height");
25220 staticpro (&Qline_height);
25221 QCalign_to = intern_c_string (":align-to");
25222 staticpro (&QCalign_to);
25223 QCrelative_width = intern_c_string (":relative-width");
25224 staticpro (&QCrelative_width);
25225 QCrelative_height = intern_c_string (":relative-height");
25226 staticpro (&QCrelative_height);
25227 QCeval = intern_c_string (":eval");
25228 staticpro (&QCeval);
25229 QCpropertize = intern_c_string (":propertize");
25230 staticpro (&QCpropertize);
25231 QCfile = intern_c_string (":file");
25232 staticpro (&QCfile);
25233 Qfontified = intern_c_string ("fontified");
25234 staticpro (&Qfontified);
25235 Qfontification_functions = intern_c_string ("fontification-functions");
25236 staticpro (&Qfontification_functions);
25237 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
25238 staticpro (&Qtrailing_whitespace);
25239 Qescape_glyph = intern_c_string ("escape-glyph");
25240 staticpro (&Qescape_glyph);
25241 Qnobreak_space = intern_c_string ("nobreak-space");
25242 staticpro (&Qnobreak_space);
25243 Qimage = intern_c_string ("image");
25244 staticpro (&Qimage);
25245 QCmap = intern_c_string (":map");
25246 staticpro (&QCmap);
25247 QCpointer = intern_c_string (":pointer");
25248 staticpro (&QCpointer);
25249 Qrect = intern_c_string ("rect");
25250 staticpro (&Qrect);
25251 Qcircle = intern_c_string ("circle");
25252 staticpro (&Qcircle);
25253 Qpoly = intern_c_string ("poly");
25254 staticpro (&Qpoly);
25255 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
25256 staticpro (&Qmessage_truncate_lines);
25257 Qgrow_only = intern_c_string ("grow-only");
25258 staticpro (&Qgrow_only);
25259 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
25260 staticpro (&Qinhibit_menubar_update);
25261 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
25262 staticpro (&Qinhibit_eval_during_redisplay);
25263 Qposition = intern_c_string ("position");
25264 staticpro (&Qposition);
25265 Qbuffer_position = intern_c_string ("buffer-position");
25266 staticpro (&Qbuffer_position);
25267 Qobject = intern_c_string ("object");
25268 staticpro (&Qobject);
25269 Qbar = intern_c_string ("bar");
25270 staticpro (&Qbar);
25271 Qhbar = intern_c_string ("hbar");
25272 staticpro (&Qhbar);
25273 Qbox = intern_c_string ("box");
25274 staticpro (&Qbox);
25275 Qhollow = intern_c_string ("hollow");
25276 staticpro (&Qhollow);
25277 Qhand = intern_c_string ("hand");
25278 staticpro (&Qhand);
25279 Qarrow = intern_c_string ("arrow");
25280 staticpro (&Qarrow);
25281 Qtext = intern_c_string ("text");
25282 staticpro (&Qtext);
25283 Qrisky_local_variable = intern_c_string ("risky-local-variable");
25284 staticpro (&Qrisky_local_variable);
25285 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
25286 staticpro (&Qinhibit_free_realized_faces);
25287
25288 list_of_error = Fcons (Fcons (intern_c_string ("error"),
25289 Fcons (intern_c_string ("void-variable"), Qnil)),
25290 Qnil);
25291 staticpro (&list_of_error);
25292
25293 Qlast_arrow_position = intern_c_string ("last-arrow-position");
25294 staticpro (&Qlast_arrow_position);
25295 Qlast_arrow_string = intern_c_string ("last-arrow-string");
25296 staticpro (&Qlast_arrow_string);
25297
25298 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
25299 staticpro (&Qoverlay_arrow_string);
25300 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
25301 staticpro (&Qoverlay_arrow_bitmap);
25302
25303 echo_buffer[0] = echo_buffer[1] = Qnil;
25304 staticpro (&echo_buffer[0]);
25305 staticpro (&echo_buffer[1]);
25306
25307 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
25308 staticpro (&echo_area_buffer[0]);
25309 staticpro (&echo_area_buffer[1]);
25310
25311 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
25312 staticpro (&Vmessages_buffer_name);
25313
25314 mode_line_proptrans_alist = Qnil;
25315 staticpro (&mode_line_proptrans_alist);
25316 mode_line_string_list = Qnil;
25317 staticpro (&mode_line_string_list);
25318 mode_line_string_face = Qnil;
25319 staticpro (&mode_line_string_face);
25320 mode_line_string_face_prop = Qnil;
25321 staticpro (&mode_line_string_face_prop);
25322 Vmode_line_unwind_vector = Qnil;
25323 staticpro (&Vmode_line_unwind_vector);
25324
25325 help_echo_string = Qnil;
25326 staticpro (&help_echo_string);
25327 help_echo_object = Qnil;
25328 staticpro (&help_echo_object);
25329 help_echo_window = Qnil;
25330 staticpro (&help_echo_window);
25331 previous_help_echo_string = Qnil;
25332 staticpro (&previous_help_echo_string);
25333 help_echo_pos = -1;
25334
25335 Qright_to_left = intern ("right-to-left");
25336 staticpro (&Qright_to_left);
25337 Qleft_to_right = intern ("left-to-right");
25338 staticpro (&Qleft_to_right);
25339
25340 #ifdef HAVE_WINDOW_SYSTEM
25341 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
25342 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
25343 For example, if a block cursor is over a tab, it will be drawn as
25344 wide as that tab on the display. */);
25345 x_stretch_cursor_p = 0;
25346 #endif
25347
25348 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
25349 doc: /* *Non-nil means highlight trailing whitespace.
25350 The face used for trailing whitespace is `trailing-whitespace'. */);
25351 Vshow_trailing_whitespace = Qnil;
25352
25353 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
25354 doc: /* *Control highlighting of nobreak space and soft hyphen.
25355 A value of t means highlight the character itself (for nobreak space,
25356 use face `nobreak-space').
25357 A value of nil means no highlighting.
25358 Other values mean display the escape glyph followed by an ordinary
25359 space or ordinary hyphen. */);
25360 Vnobreak_char_display = Qt;
25361
25362 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
25363 doc: /* *The pointer shape to show in void text areas.
25364 A value of nil means to show the text pointer. Other options are `arrow',
25365 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
25366 Vvoid_text_area_pointer = Qarrow;
25367
25368 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
25369 doc: /* Non-nil means don't actually do any redisplay.
25370 This is used for internal purposes. */);
25371 Vinhibit_redisplay = Qnil;
25372
25373 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
25374 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
25375 Vglobal_mode_string = Qnil;
25376
25377 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
25378 doc: /* Marker for where to display an arrow on top of the buffer text.
25379 This must be the beginning of a line in order to work.
25380 See also `overlay-arrow-string'. */);
25381 Voverlay_arrow_position = Qnil;
25382
25383 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
25384 doc: /* String to display as an arrow in non-window frames.
25385 See also `overlay-arrow-position'. */);
25386 Voverlay_arrow_string = make_pure_c_string ("=>");
25387
25388 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
25389 doc: /* List of variables (symbols) which hold markers for overlay arrows.
25390 The symbols on this list are examined during redisplay to determine
25391 where to display overlay arrows. */);
25392 Voverlay_arrow_variable_list
25393 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
25394
25395 DEFVAR_INT ("scroll-step", &scroll_step,
25396 doc: /* *The number of lines to try scrolling a window by when point moves out.
25397 If that fails to bring point back on frame, point is centered instead.
25398 If this is zero, point is always centered after it moves off frame.
25399 If you want scrolling to always be a line at a time, you should set
25400 `scroll-conservatively' to a large value rather than set this to 1. */);
25401
25402 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
25403 doc: /* *Scroll up to this many lines, to bring point back on screen.
25404 If point moves off-screen, redisplay will scroll by up to
25405 `scroll-conservatively' lines in order to bring point just barely
25406 onto the screen again. If that cannot be done, then redisplay
25407 recenters point as usual.
25408
25409 A value of zero means always recenter point if it moves off screen. */);
25410 scroll_conservatively = 0;
25411
25412 DEFVAR_INT ("scroll-margin", &scroll_margin,
25413 doc: /* *Number of lines of margin at the top and bottom of a window.
25414 Recenter the window whenever point gets within this many lines
25415 of the top or bottom of the window. */);
25416 scroll_margin = 0;
25417
25418 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25419 doc: /* Pixels per inch value for non-window system displays.
25420 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25421 Vdisplay_pixels_per_inch = make_float (72.0);
25422
25423 #if GLYPH_DEBUG
25424 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25425 #endif
25426
25427 DEFVAR_LISP ("truncate-partial-width-windows",
25428 &Vtruncate_partial_width_windows,
25429 doc: /* Non-nil means truncate lines in windows narrower than the frame.
25430 For an integer value, truncate lines in each window narrower than the
25431 full frame width, provided the window width is less than that integer;
25432 otherwise, respect the value of `truncate-lines'.
25433
25434 For any other non-nil value, truncate lines in all windows that do
25435 not span the full frame width.
25436
25437 A value of nil means to respect the value of `truncate-lines'.
25438
25439 If `word-wrap' is enabled, you might want to reduce this. */);
25440 Vtruncate_partial_width_windows = make_number (50);
25441
25442 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25443 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25444 Any other value means to use the appropriate face, `mode-line',
25445 `header-line', or `menu' respectively. */);
25446 mode_line_inverse_video = 1;
25447
25448 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25449 doc: /* *Maximum buffer size for which line number should be displayed.
25450 If the buffer is bigger than this, the line number does not appear
25451 in the mode line. A value of nil means no limit. */);
25452 Vline_number_display_limit = Qnil;
25453
25454 DEFVAR_INT ("line-number-display-limit-width",
25455 &line_number_display_limit_width,
25456 doc: /* *Maximum line width (in characters) for line number display.
25457 If the average length of the lines near point is bigger than this, then the
25458 line number may be omitted from the mode line. */);
25459 line_number_display_limit_width = 200;
25460
25461 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25462 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25463 highlight_nonselected_windows = 0;
25464
25465 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25466 doc: /* Non-nil if more than one frame is visible on this display.
25467 Minibuffer-only frames don't count, but iconified frames do.
25468 This variable is not guaranteed to be accurate except while processing
25469 `frame-title-format' and `icon-title-format'. */);
25470
25471 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25472 doc: /* Template for displaying the title bar of visible frames.
25473 \(Assuming the window manager supports this feature.)
25474
25475 This variable has the same structure as `mode-line-format', except that
25476 the %c and %l constructs are ignored. It is used only on frames for
25477 which no explicit name has been set \(see `modify-frame-parameters'). */);
25478
25479 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25480 doc: /* Template for displaying the title bar of an iconified frame.
25481 \(Assuming the window manager supports this feature.)
25482 This variable has the same structure as `mode-line-format' (which see),
25483 and is used only on frames for which no explicit name has been set
25484 \(see `modify-frame-parameters'). */);
25485 Vicon_title_format
25486 = Vframe_title_format
25487 = pure_cons (intern_c_string ("multiple-frames"),
25488 pure_cons (make_pure_c_string ("%b"),
25489 pure_cons (pure_cons (empty_unibyte_string,
25490 pure_cons (intern_c_string ("invocation-name"),
25491 pure_cons (make_pure_c_string ("@"),
25492 pure_cons (intern_c_string ("system-name"),
25493 Qnil)))),
25494 Qnil)));
25495
25496 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25497 doc: /* Maximum number of lines to keep in the message log buffer.
25498 If nil, disable message logging. If t, log messages but don't truncate
25499 the buffer when it becomes large. */);
25500 Vmessage_log_max = make_number (100);
25501
25502 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25503 doc: /* Functions called before redisplay, if window sizes have changed.
25504 The value should be a list of functions that take one argument.
25505 Just before redisplay, for each frame, if any of its windows have changed
25506 size since the last redisplay, or have been split or deleted,
25507 all the functions in the list are called, with the frame as argument. */);
25508 Vwindow_size_change_functions = Qnil;
25509
25510 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25511 doc: /* List of functions to call before redisplaying a window with scrolling.
25512 Each function is called with two arguments, the window and its new
25513 display-start position. Note that these functions are also called by
25514 `set-window-buffer'. Also note that the value of `window-end' is not
25515 valid when these functions are called. */);
25516 Vwindow_scroll_functions = Qnil;
25517
25518 DEFVAR_LISP ("window-text-change-functions",
25519 &Vwindow_text_change_functions,
25520 doc: /* Functions to call in redisplay when text in the window might change. */);
25521 Vwindow_text_change_functions = Qnil;
25522
25523 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25524 doc: /* Functions called when redisplay of a window reaches the end trigger.
25525 Each function is called with two arguments, the window and the end trigger value.
25526 See `set-window-redisplay-end-trigger'. */);
25527 Vredisplay_end_trigger_functions = Qnil;
25528
25529 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25530 doc: /* *Non-nil means autoselect window with mouse pointer.
25531 If nil, do not autoselect windows.
25532 A positive number means delay autoselection by that many seconds: a
25533 window is autoselected only after the mouse has remained in that
25534 window for the duration of the delay.
25535 A negative number has a similar effect, but causes windows to be
25536 autoselected only after the mouse has stopped moving. \(Because of
25537 the way Emacs compares mouse events, you will occasionally wait twice
25538 that time before the window gets selected.\)
25539 Any other value means to autoselect window instantaneously when the
25540 mouse pointer enters it.
25541
25542 Autoselection selects the minibuffer only if it is active, and never
25543 unselects the minibuffer if it is active.
25544
25545 When customizing this variable make sure that the actual value of
25546 `focus-follows-mouse' matches the behavior of your window manager. */);
25547 Vmouse_autoselect_window = Qnil;
25548
25549 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25550 doc: /* *Non-nil means automatically resize tool-bars.
25551 This dynamically changes the tool-bar's height to the minimum height
25552 that is needed to make all tool-bar items visible.
25553 If value is `grow-only', the tool-bar's height is only increased
25554 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25555 Vauto_resize_tool_bars = Qt;
25556
25557 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25558 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25559 auto_raise_tool_bar_buttons_p = 1;
25560
25561 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25562 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25563 make_cursor_line_fully_visible_p = 1;
25564
25565 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25566 doc: /* *Border below tool-bar in pixels.
25567 If an integer, use it as the height of the border.
25568 If it is one of `internal-border-width' or `border-width', use the
25569 value of the corresponding frame parameter.
25570 Otherwise, no border is added below the tool-bar. */);
25571 Vtool_bar_border = Qinternal_border_width;
25572
25573 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25574 doc: /* *Margin around tool-bar buttons in pixels.
25575 If an integer, use that for both horizontal and vertical margins.
25576 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25577 HORZ specifying the horizontal margin, and VERT specifying the
25578 vertical margin. */);
25579 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25580
25581 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25582 doc: /* *Relief thickness of tool-bar buttons. */);
25583 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25584
25585 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25586 doc: /* List of functions to call to fontify regions of text.
25587 Each function is called with one argument POS. Functions must
25588 fontify a region starting at POS in the current buffer, and give
25589 fontified regions the property `fontified'. */);
25590 Vfontification_functions = Qnil;
25591 Fmake_variable_buffer_local (Qfontification_functions);
25592
25593 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25594 &unibyte_display_via_language_environment,
25595 doc: /* *Non-nil means display unibyte text according to language environment.
25596 Specifically, this means that raw bytes in the range 160-255 decimal
25597 are displayed by converting them to the equivalent multibyte characters
25598 according to the current language environment. As a result, they are
25599 displayed according to the current fontset.
25600
25601 Note that this variable affects only how these bytes are displayed,
25602 but does not change the fact they are interpreted as raw bytes. */);
25603 unibyte_display_via_language_environment = 0;
25604
25605 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25606 doc: /* *Maximum height for resizing mini-windows.
25607 If a float, it specifies a fraction of the mini-window frame's height.
25608 If an integer, it specifies a number of lines. */);
25609 Vmax_mini_window_height = make_float (0.25);
25610
25611 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25612 doc: /* *How to resize mini-windows.
25613 A value of nil means don't automatically resize mini-windows.
25614 A value of t means resize them to fit the text displayed in them.
25615 A value of `grow-only', the default, means let mini-windows grow
25616 only, until their display becomes empty, at which point the windows
25617 go back to their normal size. */);
25618 Vresize_mini_windows = Qgrow_only;
25619
25620 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25621 doc: /* Alist specifying how to blink the cursor off.
25622 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25623 `cursor-type' frame-parameter or variable equals ON-STATE,
25624 comparing using `equal', Emacs uses OFF-STATE to specify
25625 how to blink it off. ON-STATE and OFF-STATE are values for
25626 the `cursor-type' frame parameter.
25627
25628 If a frame's ON-STATE has no entry in this list,
25629 the frame's other specifications determine how to blink the cursor off. */);
25630 Vblink_cursor_alist = Qnil;
25631
25632 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25633 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25634 automatic_hscrolling_p = 1;
25635 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25636 staticpro (&Qauto_hscroll_mode);
25637
25638 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25639 doc: /* *How many columns away from the window edge point is allowed to get
25640 before automatic hscrolling will horizontally scroll the window. */);
25641 hscroll_margin = 5;
25642
25643 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25644 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25645 When point is less than `hscroll-margin' columns from the window
25646 edge, automatic hscrolling will scroll the window by the amount of columns
25647 determined by this variable. If its value is a positive integer, scroll that
25648 many columns. If it's a positive floating-point number, it specifies the
25649 fraction of the window's width to scroll. If it's nil or zero, point will be
25650 centered horizontally after the scroll. Any other value, including negative
25651 numbers, are treated as if the value were zero.
25652
25653 Automatic hscrolling always moves point outside the scroll margin, so if
25654 point was more than scroll step columns inside the margin, the window will
25655 scroll more than the value given by the scroll step.
25656
25657 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25658 and `scroll-right' overrides this variable's effect. */);
25659 Vhscroll_step = make_number (0);
25660
25661 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25662 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25663 Bind this around calls to `message' to let it take effect. */);
25664 message_truncate_lines = 0;
25665
25666 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25667 doc: /* Normal hook run to update the menu bar definitions.
25668 Redisplay runs this hook before it redisplays the menu bar.
25669 This is used to update submenus such as Buffers,
25670 whose contents depend on various data. */);
25671 Vmenu_bar_update_hook = Qnil;
25672
25673 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25674 doc: /* Frame for which we are updating a menu.
25675 The enable predicate for a menu binding should check this variable. */);
25676 Vmenu_updating_frame = Qnil;
25677
25678 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25679 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25680 inhibit_menubar_update = 0;
25681
25682 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25683 doc: /* Prefix prepended to all continuation lines at display time.
25684 The value may be a string, an image, or a stretch-glyph; it is
25685 interpreted in the same way as the value of a `display' text property.
25686
25687 This variable is overridden by any `wrap-prefix' text or overlay
25688 property.
25689
25690 To add a prefix to non-continuation lines, use `line-prefix'. */);
25691 Vwrap_prefix = Qnil;
25692 staticpro (&Qwrap_prefix);
25693 Qwrap_prefix = intern_c_string ("wrap-prefix");
25694 Fmake_variable_buffer_local (Qwrap_prefix);
25695
25696 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25697 doc: /* Prefix prepended to all non-continuation lines at display time.
25698 The value may be a string, an image, or a stretch-glyph; it is
25699 interpreted in the same way as the value of a `display' text property.
25700
25701 This variable is overridden by any `line-prefix' text or overlay
25702 property.
25703
25704 To add a prefix to continuation lines, use `wrap-prefix'. */);
25705 Vline_prefix = Qnil;
25706 staticpro (&Qline_prefix);
25707 Qline_prefix = intern_c_string ("line-prefix");
25708 Fmake_variable_buffer_local (Qline_prefix);
25709
25710 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25711 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25712 inhibit_eval_during_redisplay = 0;
25713
25714 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25715 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25716 inhibit_free_realized_faces = 0;
25717
25718 #if GLYPH_DEBUG
25719 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25720 doc: /* Inhibit try_window_id display optimization. */);
25721 inhibit_try_window_id = 0;
25722
25723 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25724 doc: /* Inhibit try_window_reusing display optimization. */);
25725 inhibit_try_window_reusing = 0;
25726
25727 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25728 doc: /* Inhibit try_cursor_movement display optimization. */);
25729 inhibit_try_cursor_movement = 0;
25730 #endif /* GLYPH_DEBUG */
25731
25732 DEFVAR_INT ("overline-margin", &overline_margin,
25733 doc: /* *Space between overline and text, in pixels.
25734 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25735 margin to the caracter height. */);
25736 overline_margin = 2;
25737
25738 DEFVAR_INT ("underline-minimum-offset",
25739 &underline_minimum_offset,
25740 doc: /* Minimum distance between baseline and underline.
25741 This can improve legibility of underlined text at small font sizes,
25742 particularly when using variable `x-use-underline-position-properties'
25743 with fonts that specify an UNDERLINE_POSITION relatively close to the
25744 baseline. The default value is 1. */);
25745 underline_minimum_offset = 1;
25746
25747 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25748 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25749 display_hourglass_p = 1;
25750
25751 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25752 doc: /* *Seconds to wait before displaying an hourglass pointer.
25753 Value must be an integer or float. */);
25754 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25755
25756 hourglass_atimer = NULL;
25757 hourglass_shown_p = 0;
25758 }
25759
25760
25761 /* Initialize this module when Emacs starts. */
25762
25763 void
25764 init_xdisp ()
25765 {
25766 Lisp_Object root_window;
25767 struct window *mini_w;
25768
25769 current_header_line_height = current_mode_line_height = -1;
25770
25771 CHARPOS (this_line_start_pos) = 0;
25772
25773 mini_w = XWINDOW (minibuf_window);
25774 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25775
25776 if (!noninteractive)
25777 {
25778 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25779 int i;
25780
25781 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25782 set_window_height (root_window,
25783 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25784 0);
25785 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25786 set_window_height (minibuf_window, 1, 0);
25787
25788 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25789 mini_w->total_cols = make_number (FRAME_COLS (f));
25790
25791 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25792 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25793 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25794
25795 /* The default ellipsis glyphs `...'. */
25796 for (i = 0; i < 3; ++i)
25797 default_invis_vector[i] = make_number ('.');
25798 }
25799
25800 {
25801 /* Allocate the buffer for frame titles.
25802 Also used for `format-mode-line'. */
25803 int size = 100;
25804 mode_line_noprop_buf = (char *) xmalloc (size);
25805 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25806 mode_line_noprop_ptr = mode_line_noprop_buf;
25807 mode_line_target = MODE_LINE_DISPLAY;
25808 }
25809
25810 help_echo_showing_p = 0;
25811 }
25812
25813 /* Since w32 does not support atimers, it defines its own implementation of
25814 the following three functions in w32fns.c. */
25815 #ifndef WINDOWSNT
25816
25817 /* Platform-independent portion of hourglass implementation. */
25818
25819 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25820 int
25821 hourglass_started ()
25822 {
25823 return hourglass_shown_p || hourglass_atimer != NULL;
25824 }
25825
25826 /* Cancel a currently active hourglass timer, and start a new one. */
25827 void
25828 start_hourglass ()
25829 {
25830 #if defined (HAVE_WINDOW_SYSTEM)
25831 EMACS_TIME delay;
25832 int secs, usecs = 0;
25833
25834 cancel_hourglass ();
25835
25836 if (INTEGERP (Vhourglass_delay)
25837 && XINT (Vhourglass_delay) > 0)
25838 secs = XFASTINT (Vhourglass_delay);
25839 else if (FLOATP (Vhourglass_delay)
25840 && XFLOAT_DATA (Vhourglass_delay) > 0)
25841 {
25842 Lisp_Object tem;
25843 tem = Ftruncate (Vhourglass_delay, Qnil);
25844 secs = XFASTINT (tem);
25845 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25846 }
25847 else
25848 secs = DEFAULT_HOURGLASS_DELAY;
25849
25850 EMACS_SET_SECS_USECS (delay, secs, usecs);
25851 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25852 show_hourglass, NULL);
25853 #endif
25854 }
25855
25856
25857 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25858 shown. */
25859 void
25860 cancel_hourglass ()
25861 {
25862 #if defined (HAVE_WINDOW_SYSTEM)
25863 if (hourglass_atimer)
25864 {
25865 cancel_atimer (hourglass_atimer);
25866 hourglass_atimer = NULL;
25867 }
25868
25869 if (hourglass_shown_p)
25870 hide_hourglass ();
25871 #endif
25872 }
25873 #endif /* ! WINDOWSNT */
25874
25875 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25876 (do not change this comment) */