Ignore a static composition that starts before the current checking position in redis...
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171 #include <limits.h>
172 #include <setjmp.h>
173
174 #include "lisp.h"
175 #include "keyboard.h"
176 #include "frame.h"
177 #include "window.h"
178 #include "termchar.h"
179 #include "dispextern.h"
180 #include "buffer.h"
181 #include "character.h"
182 #include "charset.h"
183 #include "indent.h"
184 #include "commands.h"
185 #include "keymap.h"
186 #include "macros.h"
187 #include "disptab.h"
188 #include "termhooks.h"
189 #include "intervals.h"
190 #include "coding.h"
191 #include "process.h"
192 #include "region-cache.h"
193 #include "font.h"
194 #include "fontset.h"
195 #include "blockinput.h"
196
197 #ifdef HAVE_X_WINDOWS
198 #include "xterm.h"
199 #endif
200 #ifdef WINDOWSNT
201 #include "w32term.h"
202 #endif
203 #ifdef HAVE_NS
204 #include "nsterm.h"
205 #endif
206 #ifdef USE_GTK
207 #include "gtkutil.h"
208 #endif
209
210 #include "font.h"
211
212 #ifndef FRAME_X_OUTPUT
213 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
214 #endif
215
216 #define INFINITY 10000000
217
218 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
219 || defined(HAVE_NS) || defined (USE_GTK)
220 extern void set_frame_menubar P_ ((struct frame *f, int, int));
221 extern int pending_menu_activation;
222 #endif
223
224 extern int interrupt_input;
225 extern int command_loop_level;
226
227 extern Lisp_Object do_mouse_tracking;
228
229 extern int minibuffer_auto_raise;
230 extern Lisp_Object Vminibuffer_list;
231
232 extern Lisp_Object Qface, Qdefault;
233 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
234
235 extern Lisp_Object Voverriding_local_map;
236 extern Lisp_Object Voverriding_local_map_menu_flag;
237 extern Lisp_Object Qmenu_item;
238 extern Lisp_Object Qwhen;
239 extern Lisp_Object Qhelp_echo;
240 extern Lisp_Object Qbefore_string, Qafter_string;
241
242 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
243 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
244 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
245 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
246 Lisp_Object Qinhibit_point_motion_hooks;
247 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
248 Lisp_Object Qfontified;
249 Lisp_Object Qgrow_only;
250 Lisp_Object Qinhibit_eval_during_redisplay;
251 Lisp_Object Qbuffer_position, Qposition, Qobject;
252
253 /* Cursor shapes */
254 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
255
256 /* Pointer shapes */
257 Lisp_Object Qarrow, Qhand, Qtext;
258
259 Lisp_Object Qrisky_local_variable;
260
261 /* Holds the list (error). */
262 Lisp_Object list_of_error;
263
264 /* Functions called to fontify regions of text. */
265
266 Lisp_Object Vfontification_functions;
267 Lisp_Object Qfontification_functions;
268
269 /* Non-nil means automatically select any window when the mouse
270 cursor moves into it. */
271 Lisp_Object Vmouse_autoselect_window;
272
273 Lisp_Object Vwrap_prefix, Qwrap_prefix;
274 Lisp_Object Vline_prefix, Qline_prefix;
275
276 /* Non-zero means draw tool bar buttons raised when the mouse moves
277 over them. */
278
279 int auto_raise_tool_bar_buttons_p;
280
281 /* Non-zero means to reposition window if cursor line is only partially visible. */
282
283 int make_cursor_line_fully_visible_p;
284
285 /* Margin below tool bar in pixels. 0 or nil means no margin.
286 If value is `internal-border-width' or `border-width',
287 the corresponding frame parameter is used. */
288
289 Lisp_Object Vtool_bar_border;
290
291 /* Margin around tool bar buttons in pixels. */
292
293 Lisp_Object Vtool_bar_button_margin;
294
295 /* Thickness of shadow to draw around tool bar buttons. */
296
297 EMACS_INT tool_bar_button_relief;
298
299 /* Non-nil means automatically resize tool-bars so that all tool-bar
300 items are visible, and no blank lines remain.
301
302 If value is `grow-only', only make tool-bar bigger. */
303
304 Lisp_Object Vauto_resize_tool_bars;
305
306 /* Non-zero means draw block and hollow cursor as wide as the glyph
307 under it. For example, if a block cursor is over a tab, it will be
308 drawn as wide as that tab on the display. */
309
310 int x_stretch_cursor_p;
311
312 /* Non-nil means don't actually do any redisplay. */
313
314 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
315
316 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
317
318 int inhibit_eval_during_redisplay;
319
320 /* Names of text properties relevant for redisplay. */
321
322 Lisp_Object Qdisplay;
323 extern Lisp_Object Qface, Qinvisible, Qwidth;
324
325 /* Symbols used in text property values. */
326
327 Lisp_Object Vdisplay_pixels_per_inch;
328 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
329 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
330 Lisp_Object Qslice;
331 Lisp_Object Qcenter;
332 Lisp_Object Qmargin, Qpointer;
333 Lisp_Object Qline_height;
334 extern Lisp_Object Qheight;
335 extern Lisp_Object QCwidth, QCheight, QCascent;
336 extern Lisp_Object Qscroll_bar;
337 extern Lisp_Object Qcursor;
338
339 /* Non-nil means highlight trailing whitespace. */
340
341 Lisp_Object Vshow_trailing_whitespace;
342
343 /* Non-nil means escape non-break space and hyphens. */
344
345 Lisp_Object Vnobreak_char_display;
346
347 #ifdef HAVE_WINDOW_SYSTEM
348 extern Lisp_Object Voverflow_newline_into_fringe;
349
350 /* Test if overflow newline into fringe. Called with iterator IT
351 at or past right window margin, and with IT->current_x set. */
352
353 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
354 (!NILP (Voverflow_newline_into_fringe) \
355 && FRAME_WINDOW_P (it->f) \
356 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
357 && it->current_x == it->last_visible_x \
358 && it->line_wrap != WORD_WRAP)
359
360 #else /* !HAVE_WINDOW_SYSTEM */
361 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
362 #endif /* HAVE_WINDOW_SYSTEM */
363
364 /* Test if the display element loaded in IT is a space or tab
365 character. This is used to determine word wrapping. */
366
367 #define IT_DISPLAYING_WHITESPACE(it) \
368 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
369
370 /* Non-nil means show the text cursor in void text areas
371 i.e. in blank areas after eol and eob. This used to be
372 the default in 21.3. */
373
374 Lisp_Object Vvoid_text_area_pointer;
375
376 /* Name of the face used to highlight trailing whitespace. */
377
378 Lisp_Object Qtrailing_whitespace;
379
380 /* Name and number of the face used to highlight escape glyphs. */
381
382 Lisp_Object Qescape_glyph;
383
384 /* Name and number of the face used to highlight non-breaking spaces. */
385
386 Lisp_Object Qnobreak_space;
387
388 /* The symbol `image' which is the car of the lists used to represent
389 images in Lisp. */
390
391 Lisp_Object Qimage;
392
393 /* The image map types. */
394 Lisp_Object QCmap, QCpointer;
395 Lisp_Object Qrect, Qcircle, Qpoly;
396
397 /* Non-zero means print newline to stdout before next mini-buffer
398 message. */
399
400 int noninteractive_need_newline;
401
402 /* Non-zero means print newline to message log before next message. */
403
404 static int message_log_need_newline;
405
406 /* Three markers that message_dolog uses.
407 It could allocate them itself, but that causes trouble
408 in handling memory-full errors. */
409 static Lisp_Object message_dolog_marker1;
410 static Lisp_Object message_dolog_marker2;
411 static Lisp_Object message_dolog_marker3;
412 \f
413 /* The buffer position of the first character appearing entirely or
414 partially on the line of the selected window which contains the
415 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
416 redisplay optimization in redisplay_internal. */
417
418 static struct text_pos this_line_start_pos;
419
420 /* Number of characters past the end of the line above, including the
421 terminating newline. */
422
423 static struct text_pos this_line_end_pos;
424
425 /* The vertical positions and the height of this line. */
426
427 static int this_line_vpos;
428 static int this_line_y;
429 static int this_line_pixel_height;
430
431 /* X position at which this display line starts. Usually zero;
432 negative if first character is partially visible. */
433
434 static int this_line_start_x;
435
436 /* Buffer that this_line_.* variables are referring to. */
437
438 static struct buffer *this_line_buffer;
439
440 /* Nonzero means truncate lines in all windows less wide than the
441 frame. */
442
443 Lisp_Object Vtruncate_partial_width_windows;
444
445 /* A flag to control how to display unibyte 8-bit character. */
446
447 int unibyte_display_via_language_environment;
448
449 /* Nonzero means we have more than one non-mini-buffer-only frame.
450 Not guaranteed to be accurate except while parsing
451 frame-title-format. */
452
453 int multiple_frames;
454
455 Lisp_Object Vglobal_mode_string;
456
457
458 /* List of variables (symbols) which hold markers for overlay arrows.
459 The symbols on this list are examined during redisplay to determine
460 where to display overlay arrows. */
461
462 Lisp_Object Voverlay_arrow_variable_list;
463
464 /* Marker for where to display an arrow on top of the buffer text. */
465
466 Lisp_Object Voverlay_arrow_position;
467
468 /* String to display for the arrow. Only used on terminal frames. */
469
470 Lisp_Object Voverlay_arrow_string;
471
472 /* Values of those variables at last redisplay are stored as
473 properties on `overlay-arrow-position' symbol. However, if
474 Voverlay_arrow_position is a marker, last-arrow-position is its
475 numerical position. */
476
477 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
478
479 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
480 properties on a symbol in overlay-arrow-variable-list. */
481
482 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
483
484 /* Like mode-line-format, but for the title bar on a visible frame. */
485
486 Lisp_Object Vframe_title_format;
487
488 /* Like mode-line-format, but for the title bar on an iconified frame. */
489
490 Lisp_Object Vicon_title_format;
491
492 /* List of functions to call when a window's size changes. These
493 functions get one arg, a frame on which one or more windows' sizes
494 have changed. */
495
496 static Lisp_Object Vwindow_size_change_functions;
497
498 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
499
500 /* Nonzero if an overlay arrow has been displayed in this window. */
501
502 static int overlay_arrow_seen;
503
504 /* Nonzero means highlight the region even in nonselected windows. */
505
506 int highlight_nonselected_windows;
507
508 /* If cursor motion alone moves point off frame, try scrolling this
509 many lines up or down if that will bring it back. */
510
511 static EMACS_INT scroll_step;
512
513 /* Nonzero means scroll just far enough to bring point back on the
514 screen, when appropriate. */
515
516 static EMACS_INT scroll_conservatively;
517
518 /* Recenter the window whenever point gets within this many lines of
519 the top or bottom of the window. This value is translated into a
520 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
521 that there is really a fixed pixel height scroll margin. */
522
523 EMACS_INT scroll_margin;
524
525 /* Number of windows showing the buffer of the selected window (or
526 another buffer with the same base buffer). keyboard.c refers to
527 this. */
528
529 int buffer_shared;
530
531 /* Vector containing glyphs for an ellipsis `...'. */
532
533 static Lisp_Object default_invis_vector[3];
534
535 /* Zero means display the mode-line/header-line/menu-bar in the default face
536 (this slightly odd definition is for compatibility with previous versions
537 of emacs), non-zero means display them using their respective faces.
538
539 This variable is deprecated. */
540
541 int mode_line_inverse_video;
542
543 /* Prompt to display in front of the mini-buffer contents. */
544
545 Lisp_Object minibuf_prompt;
546
547 /* Width of current mini-buffer prompt. Only set after display_line
548 of the line that contains the prompt. */
549
550 int minibuf_prompt_width;
551
552 /* This is the window where the echo area message was displayed. It
553 is always a mini-buffer window, but it may not be the same window
554 currently active as a mini-buffer. */
555
556 Lisp_Object echo_area_window;
557
558 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
559 pushes the current message and the value of
560 message_enable_multibyte on the stack, the function restore_message
561 pops the stack and displays MESSAGE again. */
562
563 Lisp_Object Vmessage_stack;
564
565 /* Nonzero means multibyte characters were enabled when the echo area
566 message was specified. */
567
568 int message_enable_multibyte;
569
570 /* Nonzero if we should redraw the mode lines on the next redisplay. */
571
572 int update_mode_lines;
573
574 /* Nonzero if window sizes or contents have changed since last
575 redisplay that finished. */
576
577 int windows_or_buffers_changed;
578
579 /* Nonzero means a frame's cursor type has been changed. */
580
581 int cursor_type_changed;
582
583 /* Nonzero after display_mode_line if %l was used and it displayed a
584 line number. */
585
586 int line_number_displayed;
587
588 /* Maximum buffer size for which to display line numbers. */
589
590 Lisp_Object Vline_number_display_limit;
591
592 /* Line width to consider when repositioning for line number display. */
593
594 static EMACS_INT line_number_display_limit_width;
595
596 /* Number of lines to keep in the message log buffer. t means
597 infinite. nil means don't log at all. */
598
599 Lisp_Object Vmessage_log_max;
600
601 /* The name of the *Messages* buffer, a string. */
602
603 static Lisp_Object Vmessages_buffer_name;
604
605 /* Current, index 0, and last displayed echo area message. Either
606 buffers from echo_buffers, or nil to indicate no message. */
607
608 Lisp_Object echo_area_buffer[2];
609
610 /* The buffers referenced from echo_area_buffer. */
611
612 static Lisp_Object echo_buffer[2];
613
614 /* A vector saved used in with_area_buffer to reduce consing. */
615
616 static Lisp_Object Vwith_echo_area_save_vector;
617
618 /* Non-zero means display_echo_area should display the last echo area
619 message again. Set by redisplay_preserve_echo_area. */
620
621 static int display_last_displayed_message_p;
622
623 /* Nonzero if echo area is being used by print; zero if being used by
624 message. */
625
626 int message_buf_print;
627
628 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
629
630 Lisp_Object Qinhibit_menubar_update;
631 int inhibit_menubar_update;
632
633 /* When evaluating expressions from menu bar items (enable conditions,
634 for instance), this is the frame they are being processed for. */
635
636 Lisp_Object Vmenu_updating_frame;
637
638 /* Maximum height for resizing mini-windows. Either a float
639 specifying a fraction of the available height, or an integer
640 specifying a number of lines. */
641
642 Lisp_Object Vmax_mini_window_height;
643
644 /* Non-zero means messages should be displayed with truncated
645 lines instead of being continued. */
646
647 int message_truncate_lines;
648 Lisp_Object Qmessage_truncate_lines;
649
650 /* Set to 1 in clear_message to make redisplay_internal aware
651 of an emptied echo area. */
652
653 static int message_cleared_p;
654
655 /* How to blink the default frame cursor off. */
656 Lisp_Object Vblink_cursor_alist;
657
658 /* A scratch glyph row with contents used for generating truncation
659 glyphs. Also used in direct_output_for_insert. */
660
661 #define MAX_SCRATCH_GLYPHS 100
662 struct glyph_row scratch_glyph_row;
663 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
664
665 /* Ascent and height of the last line processed by move_it_to. */
666
667 static int last_max_ascent, last_height;
668
669 /* Non-zero if there's a help-echo in the echo area. */
670
671 int help_echo_showing_p;
672
673 /* If >= 0, computed, exact values of mode-line and header-line height
674 to use in the macros CURRENT_MODE_LINE_HEIGHT and
675 CURRENT_HEADER_LINE_HEIGHT. */
676
677 int current_mode_line_height, current_header_line_height;
678
679 /* The maximum distance to look ahead for text properties. Values
680 that are too small let us call compute_char_face and similar
681 functions too often which is expensive. Values that are too large
682 let us call compute_char_face and alike too often because we
683 might not be interested in text properties that far away. */
684
685 #define TEXT_PROP_DISTANCE_LIMIT 100
686
687 #if GLYPH_DEBUG
688
689 /* Variables to turn off display optimizations from Lisp. */
690
691 int inhibit_try_window_id, inhibit_try_window_reusing;
692 int inhibit_try_cursor_movement;
693
694 /* Non-zero means print traces of redisplay if compiled with
695 GLYPH_DEBUG != 0. */
696
697 int trace_redisplay_p;
698
699 #endif /* GLYPH_DEBUG */
700
701 #ifdef DEBUG_TRACE_MOVE
702 /* Non-zero means trace with TRACE_MOVE to stderr. */
703 int trace_move;
704
705 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
706 #else
707 #define TRACE_MOVE(x) (void) 0
708 #endif
709
710 /* Non-zero means automatically scroll windows horizontally to make
711 point visible. */
712
713 int automatic_hscrolling_p;
714 Lisp_Object Qauto_hscroll_mode;
715
716 /* How close to the margin can point get before the window is scrolled
717 horizontally. */
718 EMACS_INT hscroll_margin;
719
720 /* How much to scroll horizontally when point is inside the above margin. */
721 Lisp_Object Vhscroll_step;
722
723 /* The variable `resize-mini-windows'. If nil, don't resize
724 mini-windows. If t, always resize them to fit the text they
725 display. If `grow-only', let mini-windows grow only until they
726 become empty. */
727
728 Lisp_Object Vresize_mini_windows;
729
730 /* Buffer being redisplayed -- for redisplay_window_error. */
731
732 struct buffer *displayed_buffer;
733
734 /* Space between overline and text. */
735
736 EMACS_INT overline_margin;
737
738 /* Require underline to be at least this many screen pixels below baseline
739 This to avoid underline "merging" with the base of letters at small
740 font sizes, particularly when x_use_underline_position_properties is on. */
741
742 EMACS_INT underline_minimum_offset;
743
744 /* Value returned from text property handlers (see below). */
745
746 enum prop_handled
747 {
748 HANDLED_NORMALLY,
749 HANDLED_RECOMPUTE_PROPS,
750 HANDLED_OVERLAY_STRING_CONSUMED,
751 HANDLED_RETURN
752 };
753
754 /* A description of text properties that redisplay is interested
755 in. */
756
757 struct props
758 {
759 /* The name of the property. */
760 Lisp_Object *name;
761
762 /* A unique index for the property. */
763 enum prop_idx idx;
764
765 /* A handler function called to set up iterator IT from the property
766 at IT's current position. Value is used to steer handle_stop. */
767 enum prop_handled (*handler) P_ ((struct it *it));
768 };
769
770 static enum prop_handled handle_face_prop P_ ((struct it *));
771 static enum prop_handled handle_invisible_prop P_ ((struct it *));
772 static enum prop_handled handle_display_prop P_ ((struct it *));
773 static enum prop_handled handle_composition_prop P_ ((struct it *));
774 static enum prop_handled handle_overlay_change P_ ((struct it *));
775 static enum prop_handled handle_fontified_prop P_ ((struct it *));
776
777 /* Properties handled by iterators. */
778
779 static struct props it_props[] =
780 {
781 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
782 /* Handle `face' before `display' because some sub-properties of
783 `display' need to know the face. */
784 {&Qface, FACE_PROP_IDX, handle_face_prop},
785 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
786 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
787 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
788 {NULL, 0, NULL}
789 };
790
791 /* Value is the position described by X. If X is a marker, value is
792 the marker_position of X. Otherwise, value is X. */
793
794 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
795
796 /* Enumeration returned by some move_it_.* functions internally. */
797
798 enum move_it_result
799 {
800 /* Not used. Undefined value. */
801 MOVE_UNDEFINED,
802
803 /* Move ended at the requested buffer position or ZV. */
804 MOVE_POS_MATCH_OR_ZV,
805
806 /* Move ended at the requested X pixel position. */
807 MOVE_X_REACHED,
808
809 /* Move within a line ended at the end of a line that must be
810 continued. */
811 MOVE_LINE_CONTINUED,
812
813 /* Move within a line ended at the end of a line that would
814 be displayed truncated. */
815 MOVE_LINE_TRUNCATED,
816
817 /* Move within a line ended at a line end. */
818 MOVE_NEWLINE_OR_CR
819 };
820
821 /* This counter is used to clear the face cache every once in a while
822 in redisplay_internal. It is incremented for each redisplay.
823 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
824 cleared. */
825
826 #define CLEAR_FACE_CACHE_COUNT 500
827 static int clear_face_cache_count;
828
829 /* Similarly for the image cache. */
830
831 #ifdef HAVE_WINDOW_SYSTEM
832 #define CLEAR_IMAGE_CACHE_COUNT 101
833 static int clear_image_cache_count;
834 #endif
835
836 /* Non-zero while redisplay_internal is in progress. */
837
838 int redisplaying_p;
839
840 /* Non-zero means don't free realized faces. Bound while freeing
841 realized faces is dangerous because glyph matrices might still
842 reference them. */
843
844 int inhibit_free_realized_faces;
845 Lisp_Object Qinhibit_free_realized_faces;
846
847 /* If a string, XTread_socket generates an event to display that string.
848 (The display is done in read_char.) */
849
850 Lisp_Object help_echo_string;
851 Lisp_Object help_echo_window;
852 Lisp_Object help_echo_object;
853 int help_echo_pos;
854
855 /* Temporary variable for XTread_socket. */
856
857 Lisp_Object previous_help_echo_string;
858
859 /* Null glyph slice */
860
861 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
862
863 /* Platform-independent portion of hourglass implementation. */
864
865 /* Non-zero means we're allowed to display a hourglass pointer. */
866 int display_hourglass_p;
867
868 /* Non-zero means an hourglass cursor is currently shown. */
869 int hourglass_shown_p;
870
871 /* If non-null, an asynchronous timer that, when it expires, displays
872 an hourglass cursor on all frames. */
873 struct atimer *hourglass_atimer;
874
875 /* Number of seconds to wait before displaying an hourglass cursor. */
876 Lisp_Object Vhourglass_delay;
877
878 /* Default number of seconds to wait before displaying an hourglass
879 cursor. */
880 #define DEFAULT_HOURGLASS_DELAY 1
881
882 \f
883 /* Function prototypes. */
884
885 static void setup_for_ellipsis P_ ((struct it *, int));
886 static void mark_window_display_accurate_1 P_ ((struct window *, int));
887 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
888 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
889 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
890 static int redisplay_mode_lines P_ ((Lisp_Object, int));
891 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
892
893 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
894
895 static void handle_line_prefix P_ ((struct it *));
896
897 static void pint2str P_ ((char *, int, int));
898 static void pint2hrstr P_ ((char *, int, int));
899 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
900 struct text_pos));
901 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
902 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
903 static void store_mode_line_noprop_char P_ ((char));
904 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
905 static void x_consider_frame_title P_ ((Lisp_Object));
906 static void handle_stop P_ ((struct it *));
907 static int tool_bar_lines_needed P_ ((struct frame *, int *));
908 static int single_display_spec_intangible_p P_ ((Lisp_Object));
909 static void ensure_echo_area_buffers P_ ((void));
910 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
911 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
912 static int with_echo_area_buffer P_ ((struct window *, int,
913 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
914 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
915 static void clear_garbaged_frames P_ ((void));
916 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
918 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
919 static int display_echo_area P_ ((struct window *));
920 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
921 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
922 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
923 static int string_char_and_length P_ ((const unsigned char *, int *));
924 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
925 struct text_pos));
926 static int compute_window_start_on_continuation_line P_ ((struct window *));
927 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
928 static void insert_left_trunc_glyphs P_ ((struct it *));
929 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
930 Lisp_Object));
931 static void extend_face_to_end_of_line P_ ((struct it *));
932 static int append_space_for_newline P_ ((struct it *, int));
933 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
934 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
935 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
936 static int trailing_whitespace_p P_ ((int));
937 static int message_log_check_duplicate P_ ((int, int, int, int));
938 static void push_it P_ ((struct it *));
939 static void pop_it P_ ((struct it *));
940 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
941 static void select_frame_for_redisplay P_ ((Lisp_Object));
942 static void redisplay_internal P_ ((int));
943 static int echo_area_display P_ ((int));
944 static void redisplay_windows P_ ((Lisp_Object));
945 static void redisplay_window P_ ((Lisp_Object, int));
946 static Lisp_Object redisplay_window_error ();
947 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
948 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
949 static int update_menu_bar P_ ((struct frame *, int, int));
950 static int try_window_reusing_current_matrix P_ ((struct window *));
951 static int try_window_id P_ ((struct window *));
952 static int display_line P_ ((struct it *));
953 static int display_mode_lines P_ ((struct window *));
954 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
955 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
956 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
957 static char *decode_mode_spec P_ ((struct window *, int, int, int,
958 Lisp_Object *));
959 static void display_menu_bar P_ ((struct window *));
960 static int display_count_lines P_ ((int, int, int, int, int *));
961 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
962 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
963 static void compute_line_metrics P_ ((struct it *));
964 static void run_redisplay_end_trigger_hook P_ ((struct it *));
965 static int get_overlay_strings P_ ((struct it *, int));
966 static int get_overlay_strings_1 P_ ((struct it *, int, int));
967 static void next_overlay_string P_ ((struct it *));
968 static void reseat P_ ((struct it *, struct text_pos, int));
969 static void reseat_1 P_ ((struct it *, struct text_pos, int));
970 static void back_to_previous_visible_line_start P_ ((struct it *));
971 void reseat_at_previous_visible_line_start P_ ((struct it *));
972 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
973 static int next_element_from_ellipsis P_ ((struct it *));
974 static int next_element_from_display_vector P_ ((struct it *));
975 static int next_element_from_string P_ ((struct it *));
976 static int next_element_from_c_string P_ ((struct it *));
977 static int next_element_from_buffer P_ ((struct it *));
978 static int next_element_from_composition P_ ((struct it *));
979 static int next_element_from_image P_ ((struct it *));
980 static int next_element_from_stretch P_ ((struct it *));
981 static void load_overlay_strings P_ ((struct it *, int));
982 static int init_from_display_pos P_ ((struct it *, struct window *,
983 struct display_pos *));
984 static void reseat_to_string P_ ((struct it *, unsigned char *,
985 Lisp_Object, int, int, int, int));
986 static enum move_it_result
987 move_it_in_display_line_to (struct it *, EMACS_INT, int,
988 enum move_operation_enum);
989 void move_it_vertically_backward P_ ((struct it *, int));
990 static void init_to_row_start P_ ((struct it *, struct window *,
991 struct glyph_row *));
992 static int init_to_row_end P_ ((struct it *, struct window *,
993 struct glyph_row *));
994 static void back_to_previous_line_start P_ ((struct it *));
995 static int forward_to_next_line_start P_ ((struct it *, int *));
996 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
997 Lisp_Object, int));
998 static struct text_pos string_pos P_ ((int, Lisp_Object));
999 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
1000 static int number_of_chars P_ ((unsigned char *, int));
1001 static void compute_stop_pos P_ ((struct it *));
1002 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1003 Lisp_Object));
1004 static int face_before_or_after_it_pos P_ ((struct it *, int));
1005 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1006 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1007 Lisp_Object, Lisp_Object,
1008 struct text_pos *, int));
1009 static int underlying_face_id P_ ((struct it *));
1010 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1011 struct window *));
1012
1013 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1014 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1015
1016 #ifdef HAVE_WINDOW_SYSTEM
1017
1018 static void update_tool_bar P_ ((struct frame *, int));
1019 static void build_desired_tool_bar_string P_ ((struct frame *f));
1020 static int redisplay_tool_bar P_ ((struct frame *));
1021 static void display_tool_bar_line P_ ((struct it *, int));
1022 static void notice_overwritten_cursor P_ ((struct window *,
1023 enum glyph_row_area,
1024 int, int, int, int));
1025
1026
1027
1028 #endif /* HAVE_WINDOW_SYSTEM */
1029
1030 \f
1031 /***********************************************************************
1032 Window display dimensions
1033 ***********************************************************************/
1034
1035 /* Return the bottom boundary y-position for text lines in window W.
1036 This is the first y position at which a line cannot start.
1037 It is relative to the top of the window.
1038
1039 This is the height of W minus the height of a mode line, if any. */
1040
1041 INLINE int
1042 window_text_bottom_y (w)
1043 struct window *w;
1044 {
1045 int height = WINDOW_TOTAL_HEIGHT (w);
1046
1047 if (WINDOW_WANTS_MODELINE_P (w))
1048 height -= CURRENT_MODE_LINE_HEIGHT (w);
1049 return height;
1050 }
1051
1052 /* Return the pixel width of display area AREA of window W. AREA < 0
1053 means return the total width of W, not including fringes to
1054 the left and right of the window. */
1055
1056 INLINE int
1057 window_box_width (w, area)
1058 struct window *w;
1059 int area;
1060 {
1061 int cols = XFASTINT (w->total_cols);
1062 int pixels = 0;
1063
1064 if (!w->pseudo_window_p)
1065 {
1066 cols -= WINDOW_SCROLL_BAR_COLS (w);
1067
1068 if (area == TEXT_AREA)
1069 {
1070 if (INTEGERP (w->left_margin_cols))
1071 cols -= XFASTINT (w->left_margin_cols);
1072 if (INTEGERP (w->right_margin_cols))
1073 cols -= XFASTINT (w->right_margin_cols);
1074 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1075 }
1076 else if (area == LEFT_MARGIN_AREA)
1077 {
1078 cols = (INTEGERP (w->left_margin_cols)
1079 ? XFASTINT (w->left_margin_cols) : 0);
1080 pixels = 0;
1081 }
1082 else if (area == RIGHT_MARGIN_AREA)
1083 {
1084 cols = (INTEGERP (w->right_margin_cols)
1085 ? XFASTINT (w->right_margin_cols) : 0);
1086 pixels = 0;
1087 }
1088 }
1089
1090 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1091 }
1092
1093
1094 /* Return the pixel height of the display area of window W, not
1095 including mode lines of W, if any. */
1096
1097 INLINE int
1098 window_box_height (w)
1099 struct window *w;
1100 {
1101 struct frame *f = XFRAME (w->frame);
1102 int height = WINDOW_TOTAL_HEIGHT (w);
1103
1104 xassert (height >= 0);
1105
1106 /* Note: the code below that determines the mode-line/header-line
1107 height is essentially the same as that contained in the macro
1108 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1109 the appropriate glyph row has its `mode_line_p' flag set,
1110 and if it doesn't, uses estimate_mode_line_height instead. */
1111
1112 if (WINDOW_WANTS_MODELINE_P (w))
1113 {
1114 struct glyph_row *ml_row
1115 = (w->current_matrix && w->current_matrix->rows
1116 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1117 : 0);
1118 if (ml_row && ml_row->mode_line_p)
1119 height -= ml_row->height;
1120 else
1121 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1122 }
1123
1124 if (WINDOW_WANTS_HEADER_LINE_P (w))
1125 {
1126 struct glyph_row *hl_row
1127 = (w->current_matrix && w->current_matrix->rows
1128 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1129 : 0);
1130 if (hl_row && hl_row->mode_line_p)
1131 height -= hl_row->height;
1132 else
1133 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1134 }
1135
1136 /* With a very small font and a mode-line that's taller than
1137 default, we might end up with a negative height. */
1138 return max (0, height);
1139 }
1140
1141 /* Return the window-relative coordinate of the left edge of display
1142 area AREA of window W. AREA < 0 means return the left edge of the
1143 whole window, to the right of the left fringe of W. */
1144
1145 INLINE int
1146 window_box_left_offset (w, area)
1147 struct window *w;
1148 int area;
1149 {
1150 int x;
1151
1152 if (w->pseudo_window_p)
1153 return 0;
1154
1155 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1156
1157 if (area == TEXT_AREA)
1158 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1159 + window_box_width (w, LEFT_MARGIN_AREA));
1160 else if (area == RIGHT_MARGIN_AREA)
1161 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1162 + window_box_width (w, LEFT_MARGIN_AREA)
1163 + window_box_width (w, TEXT_AREA)
1164 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1165 ? 0
1166 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1167 else if (area == LEFT_MARGIN_AREA
1168 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1169 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1170
1171 return x;
1172 }
1173
1174
1175 /* Return the window-relative coordinate of the right edge of display
1176 area AREA of window W. AREA < 0 means return the left edge of the
1177 whole window, to the left of the right fringe of W. */
1178
1179 INLINE int
1180 window_box_right_offset (w, area)
1181 struct window *w;
1182 int area;
1183 {
1184 return window_box_left_offset (w, area) + window_box_width (w, area);
1185 }
1186
1187 /* Return the frame-relative coordinate of the left edge of display
1188 area AREA of window W. AREA < 0 means return the left edge of the
1189 whole window, to the right of the left fringe of W. */
1190
1191 INLINE int
1192 window_box_left (w, area)
1193 struct window *w;
1194 int area;
1195 {
1196 struct frame *f = XFRAME (w->frame);
1197 int x;
1198
1199 if (w->pseudo_window_p)
1200 return FRAME_INTERNAL_BORDER_WIDTH (f);
1201
1202 x = (WINDOW_LEFT_EDGE_X (w)
1203 + window_box_left_offset (w, area));
1204
1205 return x;
1206 }
1207
1208
1209 /* Return the frame-relative coordinate of the right edge of display
1210 area AREA of window W. AREA < 0 means return the left edge of the
1211 whole window, to the left of the right fringe of W. */
1212
1213 INLINE int
1214 window_box_right (w, area)
1215 struct window *w;
1216 int area;
1217 {
1218 return window_box_left (w, area) + window_box_width (w, area);
1219 }
1220
1221 /* Get the bounding box of the display area AREA of window W, without
1222 mode lines, in frame-relative coordinates. AREA < 0 means the
1223 whole window, not including the left and right fringes of
1224 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1225 coordinates of the upper-left corner of the box. Return in
1226 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1227
1228 INLINE void
1229 window_box (w, area, box_x, box_y, box_width, box_height)
1230 struct window *w;
1231 int area;
1232 int *box_x, *box_y, *box_width, *box_height;
1233 {
1234 if (box_width)
1235 *box_width = window_box_width (w, area);
1236 if (box_height)
1237 *box_height = window_box_height (w);
1238 if (box_x)
1239 *box_x = window_box_left (w, area);
1240 if (box_y)
1241 {
1242 *box_y = WINDOW_TOP_EDGE_Y (w);
1243 if (WINDOW_WANTS_HEADER_LINE_P (w))
1244 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1245 }
1246 }
1247
1248
1249 /* Get the bounding box of the display area AREA of window W, without
1250 mode lines. AREA < 0 means the whole window, not including the
1251 left and right fringe of the window. Return in *TOP_LEFT_X
1252 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1253 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1254 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1255 box. */
1256
1257 INLINE void
1258 window_box_edges (w, area, top_left_x, top_left_y,
1259 bottom_right_x, bottom_right_y)
1260 struct window *w;
1261 int area;
1262 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1263 {
1264 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1265 bottom_right_y);
1266 *bottom_right_x += *top_left_x;
1267 *bottom_right_y += *top_left_y;
1268 }
1269
1270
1271 \f
1272 /***********************************************************************
1273 Utilities
1274 ***********************************************************************/
1275
1276 /* Return the bottom y-position of the line the iterator IT is in.
1277 This can modify IT's settings. */
1278
1279 int
1280 line_bottom_y (it)
1281 struct it *it;
1282 {
1283 int line_height = it->max_ascent + it->max_descent;
1284 int line_top_y = it->current_y;
1285
1286 if (line_height == 0)
1287 {
1288 if (last_height)
1289 line_height = last_height;
1290 else if (IT_CHARPOS (*it) < ZV)
1291 {
1292 move_it_by_lines (it, 1, 1);
1293 line_height = (it->max_ascent || it->max_descent
1294 ? it->max_ascent + it->max_descent
1295 : last_height);
1296 }
1297 else
1298 {
1299 struct glyph_row *row = it->glyph_row;
1300
1301 /* Use the default character height. */
1302 it->glyph_row = NULL;
1303 it->what = IT_CHARACTER;
1304 it->c = ' ';
1305 it->len = 1;
1306 PRODUCE_GLYPHS (it);
1307 line_height = it->ascent + it->descent;
1308 it->glyph_row = row;
1309 }
1310 }
1311
1312 return line_top_y + line_height;
1313 }
1314
1315
1316 /* Return 1 if position CHARPOS is visible in window W.
1317 CHARPOS < 0 means return info about WINDOW_END position.
1318 If visible, set *X and *Y to pixel coordinates of top left corner.
1319 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1320 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1321
1322 int
1323 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1324 struct window *w;
1325 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1326 {
1327 struct it it;
1328 struct text_pos top;
1329 int visible_p = 0;
1330 struct buffer *old_buffer = NULL;
1331
1332 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1333 return visible_p;
1334
1335 if (XBUFFER (w->buffer) != current_buffer)
1336 {
1337 old_buffer = current_buffer;
1338 set_buffer_internal_1 (XBUFFER (w->buffer));
1339 }
1340
1341 SET_TEXT_POS_FROM_MARKER (top, w->start);
1342
1343 /* Compute exact mode line heights. */
1344 if (WINDOW_WANTS_MODELINE_P (w))
1345 current_mode_line_height
1346 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1347 current_buffer->mode_line_format);
1348
1349 if (WINDOW_WANTS_HEADER_LINE_P (w))
1350 current_header_line_height
1351 = display_mode_line (w, HEADER_LINE_FACE_ID,
1352 current_buffer->header_line_format);
1353
1354 start_display (&it, w, top);
1355 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1356 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1357
1358 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1359 {
1360 /* We have reached CHARPOS, or passed it. How the call to
1361 move_it_to can overshoot: (i) If CHARPOS is on invisible
1362 text, move_it_to stops at the end of the invisible text,
1363 after CHARPOS. (ii) If CHARPOS is in a display vector,
1364 move_it_to stops on its last glyph. */
1365 int top_x = it.current_x;
1366 int top_y = it.current_y;
1367 enum it_method it_method = it.method;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 int bottom_y = (last_height = 0, line_bottom_y (&it));
1370 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1371
1372 if (top_y < window_top_y)
1373 visible_p = bottom_y > window_top_y;
1374 else if (top_y < it.last_visible_y)
1375 visible_p = 1;
1376 if (visible_p)
1377 {
1378 if (it_method == GET_FROM_DISPLAY_VECTOR)
1379 {
1380 /* We stopped on the last glyph of a display vector.
1381 Try and recompute. Hack alert! */
1382 if (charpos < 2 || top.charpos >= charpos)
1383 top_x = it.glyph_row->x;
1384 else
1385 {
1386 struct it it2;
1387 start_display (&it2, w, top);
1388 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1389 get_next_display_element (&it2);
1390 PRODUCE_GLYPHS (&it2);
1391 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1392 || it2.current_x > it2.last_visible_x)
1393 top_x = it.glyph_row->x;
1394 else
1395 {
1396 top_x = it2.current_x;
1397 top_y = it2.current_y;
1398 }
1399 }
1400 }
1401
1402 *x = top_x;
1403 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1404 *rtop = max (0, window_top_y - top_y);
1405 *rbot = max (0, bottom_y - it.last_visible_y);
1406 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1407 - max (top_y, window_top_y)));
1408 *vpos = it.vpos;
1409 }
1410 }
1411 else
1412 {
1413 struct it it2;
1414
1415 it2 = it;
1416 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1417 move_it_by_lines (&it, 1, 0);
1418 if (charpos < IT_CHARPOS (it)
1419 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1420 {
1421 visible_p = 1;
1422 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1423 *x = it2.current_x;
1424 *y = it2.current_y + it2.max_ascent - it2.ascent;
1425 *rtop = max (0, -it2.current_y);
1426 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1427 - it.last_visible_y));
1428 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1429 it.last_visible_y)
1430 - max (it2.current_y,
1431 WINDOW_HEADER_LINE_HEIGHT (w))));
1432 *vpos = it2.vpos;
1433 }
1434 }
1435
1436 if (old_buffer)
1437 set_buffer_internal_1 (old_buffer);
1438
1439 current_header_line_height = current_mode_line_height = -1;
1440
1441 if (visible_p && XFASTINT (w->hscroll) > 0)
1442 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1443
1444 #if 0
1445 /* Debugging code. */
1446 if (visible_p)
1447 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1448 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1449 else
1450 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1451 #endif
1452
1453 return visible_p;
1454 }
1455
1456
1457 /* Return the next character from STR. Return in *LEN the length of
1458 the character. This is like STRING_CHAR_AND_LENGTH but never
1459 returns an invalid character. If we find one, we return a `?', but
1460 with the length of the invalid character. */
1461
1462 static INLINE int
1463 string_char_and_length (str, len)
1464 const unsigned char *str;
1465 int *len;
1466 {
1467 int c;
1468
1469 c = STRING_CHAR_AND_LENGTH (str, *len);
1470 if (!CHAR_VALID_P (c, 1))
1471 /* We may not change the length here because other places in Emacs
1472 don't use this function, i.e. they silently accept invalid
1473 characters. */
1474 c = '?';
1475
1476 return c;
1477 }
1478
1479
1480
1481 /* Given a position POS containing a valid character and byte position
1482 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1483
1484 static struct text_pos
1485 string_pos_nchars_ahead (pos, string, nchars)
1486 struct text_pos pos;
1487 Lisp_Object string;
1488 int nchars;
1489 {
1490 xassert (STRINGP (string) && nchars >= 0);
1491
1492 if (STRING_MULTIBYTE (string))
1493 {
1494 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1495 int len;
1496
1497 while (nchars--)
1498 {
1499 string_char_and_length (p, &len);
1500 p += len;
1501 CHARPOS (pos) += 1;
1502 BYTEPOS (pos) += len;
1503 }
1504 }
1505 else
1506 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1507
1508 return pos;
1509 }
1510
1511
1512 /* Value is the text position, i.e. character and byte position,
1513 for character position CHARPOS in STRING. */
1514
1515 static INLINE struct text_pos
1516 string_pos (charpos, string)
1517 int charpos;
1518 Lisp_Object string;
1519 {
1520 struct text_pos pos;
1521 xassert (STRINGP (string));
1522 xassert (charpos >= 0);
1523 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1524 return pos;
1525 }
1526
1527
1528 /* Value is a text position, i.e. character and byte position, for
1529 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1530 means recognize multibyte characters. */
1531
1532 static struct text_pos
1533 c_string_pos (charpos, s, multibyte_p)
1534 int charpos;
1535 unsigned char *s;
1536 int multibyte_p;
1537 {
1538 struct text_pos pos;
1539
1540 xassert (s != NULL);
1541 xassert (charpos >= 0);
1542
1543 if (multibyte_p)
1544 {
1545 int len;
1546
1547 SET_TEXT_POS (pos, 0, 0);
1548 while (charpos--)
1549 {
1550 string_char_and_length (s, &len);
1551 s += len;
1552 CHARPOS (pos) += 1;
1553 BYTEPOS (pos) += len;
1554 }
1555 }
1556 else
1557 SET_TEXT_POS (pos, charpos, charpos);
1558
1559 return pos;
1560 }
1561
1562
1563 /* Value is the number of characters in C string S. MULTIBYTE_P
1564 non-zero means recognize multibyte characters. */
1565
1566 static int
1567 number_of_chars (s, multibyte_p)
1568 unsigned char *s;
1569 int multibyte_p;
1570 {
1571 int nchars;
1572
1573 if (multibyte_p)
1574 {
1575 int rest = strlen (s), len;
1576 unsigned char *p = (unsigned char *) s;
1577
1578 for (nchars = 0; rest > 0; ++nchars)
1579 {
1580 string_char_and_length (p, &len);
1581 rest -= len, p += len;
1582 }
1583 }
1584 else
1585 nchars = strlen (s);
1586
1587 return nchars;
1588 }
1589
1590
1591 /* Compute byte position NEWPOS->bytepos corresponding to
1592 NEWPOS->charpos. POS is a known position in string STRING.
1593 NEWPOS->charpos must be >= POS.charpos. */
1594
1595 static void
1596 compute_string_pos (newpos, pos, string)
1597 struct text_pos *newpos, pos;
1598 Lisp_Object string;
1599 {
1600 xassert (STRINGP (string));
1601 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1602
1603 if (STRING_MULTIBYTE (string))
1604 *newpos = string_pos_nchars_ahead (pos, string,
1605 CHARPOS (*newpos) - CHARPOS (pos));
1606 else
1607 BYTEPOS (*newpos) = CHARPOS (*newpos);
1608 }
1609
1610 /* EXPORT:
1611 Return an estimation of the pixel height of mode or header lines on
1612 frame F. FACE_ID specifies what line's height to estimate. */
1613
1614 int
1615 estimate_mode_line_height (f, face_id)
1616 struct frame *f;
1617 enum face_id face_id;
1618 {
1619 #ifdef HAVE_WINDOW_SYSTEM
1620 if (FRAME_WINDOW_P (f))
1621 {
1622 int height = FONT_HEIGHT (FRAME_FONT (f));
1623
1624 /* This function is called so early when Emacs starts that the face
1625 cache and mode line face are not yet initialized. */
1626 if (FRAME_FACE_CACHE (f))
1627 {
1628 struct face *face = FACE_FROM_ID (f, face_id);
1629 if (face)
1630 {
1631 if (face->font)
1632 height = FONT_HEIGHT (face->font);
1633 if (face->box_line_width > 0)
1634 height += 2 * face->box_line_width;
1635 }
1636 }
1637
1638 return height;
1639 }
1640 #endif
1641
1642 return 1;
1643 }
1644
1645 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1646 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1647 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1648 not force the value into range. */
1649
1650 void
1651 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1652 FRAME_PTR f;
1653 register int pix_x, pix_y;
1654 int *x, *y;
1655 NativeRectangle *bounds;
1656 int noclip;
1657 {
1658
1659 #ifdef HAVE_WINDOW_SYSTEM
1660 if (FRAME_WINDOW_P (f))
1661 {
1662 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1663 even for negative values. */
1664 if (pix_x < 0)
1665 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1666 if (pix_y < 0)
1667 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1668
1669 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1670 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1671
1672 if (bounds)
1673 STORE_NATIVE_RECT (*bounds,
1674 FRAME_COL_TO_PIXEL_X (f, pix_x),
1675 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1676 FRAME_COLUMN_WIDTH (f) - 1,
1677 FRAME_LINE_HEIGHT (f) - 1);
1678
1679 if (!noclip)
1680 {
1681 if (pix_x < 0)
1682 pix_x = 0;
1683 else if (pix_x > FRAME_TOTAL_COLS (f))
1684 pix_x = FRAME_TOTAL_COLS (f);
1685
1686 if (pix_y < 0)
1687 pix_y = 0;
1688 else if (pix_y > FRAME_LINES (f))
1689 pix_y = FRAME_LINES (f);
1690 }
1691 }
1692 #endif
1693
1694 *x = pix_x;
1695 *y = pix_y;
1696 }
1697
1698
1699 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1700 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1701 can't tell the positions because W's display is not up to date,
1702 return 0. */
1703
1704 int
1705 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1706 struct window *w;
1707 int hpos, vpos;
1708 int *frame_x, *frame_y;
1709 {
1710 #ifdef HAVE_WINDOW_SYSTEM
1711 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1712 {
1713 int success_p;
1714
1715 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1716 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1717
1718 if (display_completed)
1719 {
1720 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1721 struct glyph *glyph = row->glyphs[TEXT_AREA];
1722 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1723
1724 hpos = row->x;
1725 vpos = row->y;
1726 while (glyph < end)
1727 {
1728 hpos += glyph->pixel_width;
1729 ++glyph;
1730 }
1731
1732 /* If first glyph is partially visible, its first visible position is still 0. */
1733 if (hpos < 0)
1734 hpos = 0;
1735
1736 success_p = 1;
1737 }
1738 else
1739 {
1740 hpos = vpos = 0;
1741 success_p = 0;
1742 }
1743
1744 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1745 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1746 return success_p;
1747 }
1748 #endif
1749
1750 *frame_x = hpos;
1751 *frame_y = vpos;
1752 return 1;
1753 }
1754
1755
1756 #ifdef HAVE_WINDOW_SYSTEM
1757
1758 /* Find the glyph under window-relative coordinates X/Y in window W.
1759 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1760 strings. Return in *HPOS and *VPOS the row and column number of
1761 the glyph found. Return in *AREA the glyph area containing X.
1762 Value is a pointer to the glyph found or null if X/Y is not on
1763 text, or we can't tell because W's current matrix is not up to
1764 date. */
1765
1766 static
1767 struct glyph *
1768 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1769 struct window *w;
1770 int x, y;
1771 int *hpos, *vpos, *dx, *dy, *area;
1772 {
1773 struct glyph *glyph, *end;
1774 struct glyph_row *row = NULL;
1775 int x0, i;
1776
1777 /* Find row containing Y. Give up if some row is not enabled. */
1778 for (i = 0; i < w->current_matrix->nrows; ++i)
1779 {
1780 row = MATRIX_ROW (w->current_matrix, i);
1781 if (!row->enabled_p)
1782 return NULL;
1783 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1784 break;
1785 }
1786
1787 *vpos = i;
1788 *hpos = 0;
1789
1790 /* Give up if Y is not in the window. */
1791 if (i == w->current_matrix->nrows)
1792 return NULL;
1793
1794 /* Get the glyph area containing X. */
1795 if (w->pseudo_window_p)
1796 {
1797 *area = TEXT_AREA;
1798 x0 = 0;
1799 }
1800 else
1801 {
1802 if (x < window_box_left_offset (w, TEXT_AREA))
1803 {
1804 *area = LEFT_MARGIN_AREA;
1805 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1806 }
1807 else if (x < window_box_right_offset (w, TEXT_AREA))
1808 {
1809 *area = TEXT_AREA;
1810 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1811 }
1812 else
1813 {
1814 *area = RIGHT_MARGIN_AREA;
1815 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1816 }
1817 }
1818
1819 /* Find glyph containing X. */
1820 glyph = row->glyphs[*area];
1821 end = glyph + row->used[*area];
1822 x -= x0;
1823 while (glyph < end && x >= glyph->pixel_width)
1824 {
1825 x -= glyph->pixel_width;
1826 ++glyph;
1827 }
1828
1829 if (glyph == end)
1830 return NULL;
1831
1832 if (dx)
1833 {
1834 *dx = x;
1835 *dy = y - (row->y + row->ascent - glyph->ascent);
1836 }
1837
1838 *hpos = glyph - row->glyphs[*area];
1839 return glyph;
1840 }
1841
1842
1843 /* EXPORT:
1844 Convert frame-relative x/y to coordinates relative to window W.
1845 Takes pseudo-windows into account. */
1846
1847 void
1848 frame_to_window_pixel_xy (w, x, y)
1849 struct window *w;
1850 int *x, *y;
1851 {
1852 if (w->pseudo_window_p)
1853 {
1854 /* A pseudo-window is always full-width, and starts at the
1855 left edge of the frame, plus a frame border. */
1856 struct frame *f = XFRAME (w->frame);
1857 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1858 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1859 }
1860 else
1861 {
1862 *x -= WINDOW_LEFT_EDGE_X (w);
1863 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1864 }
1865 }
1866
1867 /* EXPORT:
1868 Return in RECTS[] at most N clipping rectangles for glyph string S.
1869 Return the number of stored rectangles. */
1870
1871 int
1872 get_glyph_string_clip_rects (s, rects, n)
1873 struct glyph_string *s;
1874 NativeRectangle *rects;
1875 int n;
1876 {
1877 XRectangle r;
1878
1879 if (n <= 0)
1880 return 0;
1881
1882 if (s->row->full_width_p)
1883 {
1884 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1885 r.x = WINDOW_LEFT_EDGE_X (s->w);
1886 r.width = WINDOW_TOTAL_WIDTH (s->w);
1887
1888 /* Unless displaying a mode or menu bar line, which are always
1889 fully visible, clip to the visible part of the row. */
1890 if (s->w->pseudo_window_p)
1891 r.height = s->row->visible_height;
1892 else
1893 r.height = s->height;
1894 }
1895 else
1896 {
1897 /* This is a text line that may be partially visible. */
1898 r.x = window_box_left (s->w, s->area);
1899 r.width = window_box_width (s->w, s->area);
1900 r.height = s->row->visible_height;
1901 }
1902
1903 if (s->clip_head)
1904 if (r.x < s->clip_head->x)
1905 {
1906 if (r.width >= s->clip_head->x - r.x)
1907 r.width -= s->clip_head->x - r.x;
1908 else
1909 r.width = 0;
1910 r.x = s->clip_head->x;
1911 }
1912 if (s->clip_tail)
1913 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1914 {
1915 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1916 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1917 else
1918 r.width = 0;
1919 }
1920
1921 /* If S draws overlapping rows, it's sufficient to use the top and
1922 bottom of the window for clipping because this glyph string
1923 intentionally draws over other lines. */
1924 if (s->for_overlaps)
1925 {
1926 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1927 r.height = window_text_bottom_y (s->w) - r.y;
1928
1929 /* Alas, the above simple strategy does not work for the
1930 environments with anti-aliased text: if the same text is
1931 drawn onto the same place multiple times, it gets thicker.
1932 If the overlap we are processing is for the erased cursor, we
1933 take the intersection with the rectagle of the cursor. */
1934 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1935 {
1936 XRectangle rc, r_save = r;
1937
1938 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1939 rc.y = s->w->phys_cursor.y;
1940 rc.width = s->w->phys_cursor_width;
1941 rc.height = s->w->phys_cursor_height;
1942
1943 x_intersect_rectangles (&r_save, &rc, &r);
1944 }
1945 }
1946 else
1947 {
1948 /* Don't use S->y for clipping because it doesn't take partially
1949 visible lines into account. For example, it can be negative for
1950 partially visible lines at the top of a window. */
1951 if (!s->row->full_width_p
1952 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1953 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1954 else
1955 r.y = max (0, s->row->y);
1956 }
1957
1958 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1959
1960 /* If drawing the cursor, don't let glyph draw outside its
1961 advertised boundaries. Cleartype does this under some circumstances. */
1962 if (s->hl == DRAW_CURSOR)
1963 {
1964 struct glyph *glyph = s->first_glyph;
1965 int height, max_y;
1966
1967 if (s->x > r.x)
1968 {
1969 r.width -= s->x - r.x;
1970 r.x = s->x;
1971 }
1972 r.width = min (r.width, glyph->pixel_width);
1973
1974 /* If r.y is below window bottom, ensure that we still see a cursor. */
1975 height = min (glyph->ascent + glyph->descent,
1976 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1977 max_y = window_text_bottom_y (s->w) - height;
1978 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1979 if (s->ybase - glyph->ascent > max_y)
1980 {
1981 r.y = max_y;
1982 r.height = height;
1983 }
1984 else
1985 {
1986 /* Don't draw cursor glyph taller than our actual glyph. */
1987 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1988 if (height < r.height)
1989 {
1990 max_y = r.y + r.height;
1991 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1992 r.height = min (max_y - r.y, height);
1993 }
1994 }
1995 }
1996
1997 if (s->row->clip)
1998 {
1999 XRectangle r_save = r;
2000
2001 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2002 r.width = 0;
2003 }
2004
2005 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2006 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2007 {
2008 #ifdef CONVERT_FROM_XRECT
2009 CONVERT_FROM_XRECT (r, *rects);
2010 #else
2011 *rects = r;
2012 #endif
2013 return 1;
2014 }
2015 else
2016 {
2017 /* If we are processing overlapping and allowed to return
2018 multiple clipping rectangles, we exclude the row of the glyph
2019 string from the clipping rectangle. This is to avoid drawing
2020 the same text on the environment with anti-aliasing. */
2021 #ifdef CONVERT_FROM_XRECT
2022 XRectangle rs[2];
2023 #else
2024 XRectangle *rs = rects;
2025 #endif
2026 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2027
2028 if (s->for_overlaps & OVERLAPS_PRED)
2029 {
2030 rs[i] = r;
2031 if (r.y + r.height > row_y)
2032 {
2033 if (r.y < row_y)
2034 rs[i].height = row_y - r.y;
2035 else
2036 rs[i].height = 0;
2037 }
2038 i++;
2039 }
2040 if (s->for_overlaps & OVERLAPS_SUCC)
2041 {
2042 rs[i] = r;
2043 if (r.y < row_y + s->row->visible_height)
2044 {
2045 if (r.y + r.height > row_y + s->row->visible_height)
2046 {
2047 rs[i].y = row_y + s->row->visible_height;
2048 rs[i].height = r.y + r.height - rs[i].y;
2049 }
2050 else
2051 rs[i].height = 0;
2052 }
2053 i++;
2054 }
2055
2056 n = i;
2057 #ifdef CONVERT_FROM_XRECT
2058 for (i = 0; i < n; i++)
2059 CONVERT_FROM_XRECT (rs[i], rects[i]);
2060 #endif
2061 return n;
2062 }
2063 }
2064
2065 /* EXPORT:
2066 Return in *NR the clipping rectangle for glyph string S. */
2067
2068 void
2069 get_glyph_string_clip_rect (s, nr)
2070 struct glyph_string *s;
2071 NativeRectangle *nr;
2072 {
2073 get_glyph_string_clip_rects (s, nr, 1);
2074 }
2075
2076
2077 /* EXPORT:
2078 Return the position and height of the phys cursor in window W.
2079 Set w->phys_cursor_width to width of phys cursor.
2080 */
2081
2082 void
2083 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2084 struct window *w;
2085 struct glyph_row *row;
2086 struct glyph *glyph;
2087 int *xp, *yp, *heightp;
2088 {
2089 struct frame *f = XFRAME (WINDOW_FRAME (w));
2090 int x, y, wd, h, h0, y0;
2091
2092 /* Compute the width of the rectangle to draw. If on a stretch
2093 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2094 rectangle as wide as the glyph, but use a canonical character
2095 width instead. */
2096 wd = glyph->pixel_width - 1;
2097 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2098 wd++; /* Why? */
2099 #endif
2100
2101 x = w->phys_cursor.x;
2102 if (x < 0)
2103 {
2104 wd += x;
2105 x = 0;
2106 }
2107
2108 if (glyph->type == STRETCH_GLYPH
2109 && !x_stretch_cursor_p)
2110 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2111 w->phys_cursor_width = wd;
2112
2113 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2114
2115 /* If y is below window bottom, ensure that we still see a cursor. */
2116 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2117
2118 h = max (h0, glyph->ascent + glyph->descent);
2119 h0 = min (h0, glyph->ascent + glyph->descent);
2120
2121 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2122 if (y < y0)
2123 {
2124 h = max (h - (y0 - y) + 1, h0);
2125 y = y0 - 1;
2126 }
2127 else
2128 {
2129 y0 = window_text_bottom_y (w) - h0;
2130 if (y > y0)
2131 {
2132 h += y - y0;
2133 y = y0;
2134 }
2135 }
2136
2137 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2138 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2139 *heightp = h;
2140 }
2141
2142 /*
2143 * Remember which glyph the mouse is over.
2144 */
2145
2146 void
2147 remember_mouse_glyph (f, gx, gy, rect)
2148 struct frame *f;
2149 int gx, gy;
2150 NativeRectangle *rect;
2151 {
2152 Lisp_Object window;
2153 struct window *w;
2154 struct glyph_row *r, *gr, *end_row;
2155 enum window_part part;
2156 enum glyph_row_area area;
2157 int x, y, width, height;
2158
2159 /* Try to determine frame pixel position and size of the glyph under
2160 frame pixel coordinates X/Y on frame F. */
2161
2162 if (!f->glyphs_initialized_p
2163 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2164 NILP (window)))
2165 {
2166 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2167 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2168 goto virtual_glyph;
2169 }
2170
2171 w = XWINDOW (window);
2172 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2173 height = WINDOW_FRAME_LINE_HEIGHT (w);
2174
2175 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2176 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2177
2178 if (w->pseudo_window_p)
2179 {
2180 area = TEXT_AREA;
2181 part = ON_MODE_LINE; /* Don't adjust margin. */
2182 goto text_glyph;
2183 }
2184
2185 switch (part)
2186 {
2187 case ON_LEFT_MARGIN:
2188 area = LEFT_MARGIN_AREA;
2189 goto text_glyph;
2190
2191 case ON_RIGHT_MARGIN:
2192 area = RIGHT_MARGIN_AREA;
2193 goto text_glyph;
2194
2195 case ON_HEADER_LINE:
2196 case ON_MODE_LINE:
2197 gr = (part == ON_HEADER_LINE
2198 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2199 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2200 gy = gr->y;
2201 area = TEXT_AREA;
2202 goto text_glyph_row_found;
2203
2204 case ON_TEXT:
2205 area = TEXT_AREA;
2206
2207 text_glyph:
2208 gr = 0; gy = 0;
2209 for (; r <= end_row && r->enabled_p; ++r)
2210 if (r->y + r->height > y)
2211 {
2212 gr = r; gy = r->y;
2213 break;
2214 }
2215
2216 text_glyph_row_found:
2217 if (gr && gy <= y)
2218 {
2219 struct glyph *g = gr->glyphs[area];
2220 struct glyph *end = g + gr->used[area];
2221
2222 height = gr->height;
2223 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2224 if (gx + g->pixel_width > x)
2225 break;
2226
2227 if (g < end)
2228 {
2229 if (g->type == IMAGE_GLYPH)
2230 {
2231 /* Don't remember when mouse is over image, as
2232 image may have hot-spots. */
2233 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2234 return;
2235 }
2236 width = g->pixel_width;
2237 }
2238 else
2239 {
2240 /* Use nominal char spacing at end of line. */
2241 x -= gx;
2242 gx += (x / width) * width;
2243 }
2244
2245 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2246 gx += window_box_left_offset (w, area);
2247 }
2248 else
2249 {
2250 /* Use nominal line height at end of window. */
2251 gx = (x / width) * width;
2252 y -= gy;
2253 gy += (y / height) * height;
2254 }
2255 break;
2256
2257 case ON_LEFT_FRINGE:
2258 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2259 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2260 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2261 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2262 goto row_glyph;
2263
2264 case ON_RIGHT_FRINGE:
2265 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2266 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2267 : window_box_right_offset (w, TEXT_AREA));
2268 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2269 goto row_glyph;
2270
2271 case ON_SCROLL_BAR:
2272 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2273 ? 0
2274 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2275 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2276 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2277 : 0)));
2278 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2279
2280 row_glyph:
2281 gr = 0, gy = 0;
2282 for (; r <= end_row && r->enabled_p; ++r)
2283 if (r->y + r->height > y)
2284 {
2285 gr = r; gy = r->y;
2286 break;
2287 }
2288
2289 if (gr && gy <= y)
2290 height = gr->height;
2291 else
2292 {
2293 /* Use nominal line height at end of window. */
2294 y -= gy;
2295 gy += (y / height) * height;
2296 }
2297 break;
2298
2299 default:
2300 ;
2301 virtual_glyph:
2302 /* If there is no glyph under the mouse, then we divide the screen
2303 into a grid of the smallest glyph in the frame, and use that
2304 as our "glyph". */
2305
2306 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2307 round down even for negative values. */
2308 if (gx < 0)
2309 gx -= width - 1;
2310 if (gy < 0)
2311 gy -= height - 1;
2312
2313 gx = (gx / width) * width;
2314 gy = (gy / height) * height;
2315
2316 goto store_rect;
2317 }
2318
2319 gx += WINDOW_LEFT_EDGE_X (w);
2320 gy += WINDOW_TOP_EDGE_Y (w);
2321
2322 store_rect:
2323 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2324
2325 /* Visible feedback for debugging. */
2326 #if 0
2327 #if HAVE_X_WINDOWS
2328 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2329 f->output_data.x->normal_gc,
2330 gx, gy, width, height);
2331 #endif
2332 #endif
2333 }
2334
2335
2336 #endif /* HAVE_WINDOW_SYSTEM */
2337
2338 \f
2339 /***********************************************************************
2340 Lisp form evaluation
2341 ***********************************************************************/
2342
2343 /* Error handler for safe_eval and safe_call. */
2344
2345 static Lisp_Object
2346 safe_eval_handler (arg)
2347 Lisp_Object arg;
2348 {
2349 add_to_log ("Error during redisplay: %s", arg, Qnil);
2350 return Qnil;
2351 }
2352
2353
2354 /* Evaluate SEXPR and return the result, or nil if something went
2355 wrong. Prevent redisplay during the evaluation. */
2356
2357 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2358 Return the result, or nil if something went wrong. Prevent
2359 redisplay during the evaluation. */
2360
2361 Lisp_Object
2362 safe_call (nargs, args)
2363 int nargs;
2364 Lisp_Object *args;
2365 {
2366 Lisp_Object val;
2367
2368 if (inhibit_eval_during_redisplay)
2369 val = Qnil;
2370 else
2371 {
2372 int count = SPECPDL_INDEX ();
2373 struct gcpro gcpro1;
2374
2375 GCPRO1 (args[0]);
2376 gcpro1.nvars = nargs;
2377 specbind (Qinhibit_redisplay, Qt);
2378 /* Use Qt to ensure debugger does not run,
2379 so there is no possibility of wanting to redisplay. */
2380 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2381 safe_eval_handler);
2382 UNGCPRO;
2383 val = unbind_to (count, val);
2384 }
2385
2386 return val;
2387 }
2388
2389
2390 /* Call function FN with one argument ARG.
2391 Return the result, or nil if something went wrong. */
2392
2393 Lisp_Object
2394 safe_call1 (fn, arg)
2395 Lisp_Object fn, arg;
2396 {
2397 Lisp_Object args[2];
2398 args[0] = fn;
2399 args[1] = arg;
2400 return safe_call (2, args);
2401 }
2402
2403 static Lisp_Object Qeval;
2404
2405 Lisp_Object
2406 safe_eval (Lisp_Object sexpr)
2407 {
2408 return safe_call1 (Qeval, sexpr);
2409 }
2410
2411 /* Call function FN with one argument ARG.
2412 Return the result, or nil if something went wrong. */
2413
2414 Lisp_Object
2415 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2416 {
2417 Lisp_Object args[3];
2418 args[0] = fn;
2419 args[1] = arg1;
2420 args[2] = arg2;
2421 return safe_call (3, args);
2422 }
2423
2424
2425 \f
2426 /***********************************************************************
2427 Debugging
2428 ***********************************************************************/
2429
2430 #if 0
2431
2432 /* Define CHECK_IT to perform sanity checks on iterators.
2433 This is for debugging. It is too slow to do unconditionally. */
2434
2435 static void
2436 check_it (it)
2437 struct it *it;
2438 {
2439 if (it->method == GET_FROM_STRING)
2440 {
2441 xassert (STRINGP (it->string));
2442 xassert (IT_STRING_CHARPOS (*it) >= 0);
2443 }
2444 else
2445 {
2446 xassert (IT_STRING_CHARPOS (*it) < 0);
2447 if (it->method == GET_FROM_BUFFER)
2448 {
2449 /* Check that character and byte positions agree. */
2450 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2451 }
2452 }
2453
2454 if (it->dpvec)
2455 xassert (it->current.dpvec_index >= 0);
2456 else
2457 xassert (it->current.dpvec_index < 0);
2458 }
2459
2460 #define CHECK_IT(IT) check_it ((IT))
2461
2462 #else /* not 0 */
2463
2464 #define CHECK_IT(IT) (void) 0
2465
2466 #endif /* not 0 */
2467
2468
2469 #if GLYPH_DEBUG
2470
2471 /* Check that the window end of window W is what we expect it
2472 to be---the last row in the current matrix displaying text. */
2473
2474 static void
2475 check_window_end (w)
2476 struct window *w;
2477 {
2478 if (!MINI_WINDOW_P (w)
2479 && !NILP (w->window_end_valid))
2480 {
2481 struct glyph_row *row;
2482 xassert ((row = MATRIX_ROW (w->current_matrix,
2483 XFASTINT (w->window_end_vpos)),
2484 !row->enabled_p
2485 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2486 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2487 }
2488 }
2489
2490 #define CHECK_WINDOW_END(W) check_window_end ((W))
2491
2492 #else /* not GLYPH_DEBUG */
2493
2494 #define CHECK_WINDOW_END(W) (void) 0
2495
2496 #endif /* not GLYPH_DEBUG */
2497
2498
2499 \f
2500 /***********************************************************************
2501 Iterator initialization
2502 ***********************************************************************/
2503
2504 /* Initialize IT for displaying current_buffer in window W, starting
2505 at character position CHARPOS. CHARPOS < 0 means that no buffer
2506 position is specified which is useful when the iterator is assigned
2507 a position later. BYTEPOS is the byte position corresponding to
2508 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2509
2510 If ROW is not null, calls to produce_glyphs with IT as parameter
2511 will produce glyphs in that row.
2512
2513 BASE_FACE_ID is the id of a base face to use. It must be one of
2514 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2515 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2516 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2517
2518 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2519 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2520 will be initialized to use the corresponding mode line glyph row of
2521 the desired matrix of W. */
2522
2523 void
2524 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2525 struct it *it;
2526 struct window *w;
2527 int charpos, bytepos;
2528 struct glyph_row *row;
2529 enum face_id base_face_id;
2530 {
2531 int highlight_region_p;
2532 enum face_id remapped_base_face_id = base_face_id;
2533
2534 /* Some precondition checks. */
2535 xassert (w != NULL && it != NULL);
2536 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2537 && charpos <= ZV));
2538
2539 /* If face attributes have been changed since the last redisplay,
2540 free realized faces now because they depend on face definitions
2541 that might have changed. Don't free faces while there might be
2542 desired matrices pending which reference these faces. */
2543 if (face_change_count && !inhibit_free_realized_faces)
2544 {
2545 face_change_count = 0;
2546 free_all_realized_faces (Qnil);
2547 }
2548
2549 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2550 if (! NILP (Vface_remapping_alist))
2551 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2552
2553 /* Use one of the mode line rows of W's desired matrix if
2554 appropriate. */
2555 if (row == NULL)
2556 {
2557 if (base_face_id == MODE_LINE_FACE_ID
2558 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2559 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2560 else if (base_face_id == HEADER_LINE_FACE_ID)
2561 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2562 }
2563
2564 /* Clear IT. */
2565 bzero (it, sizeof *it);
2566 it->current.overlay_string_index = -1;
2567 it->current.dpvec_index = -1;
2568 it->base_face_id = remapped_base_face_id;
2569 it->string = Qnil;
2570 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2571
2572 /* The window in which we iterate over current_buffer: */
2573 XSETWINDOW (it->window, w);
2574 it->w = w;
2575 it->f = XFRAME (w->frame);
2576
2577 it->cmp_it.id = -1;
2578
2579 /* Extra space between lines (on window systems only). */
2580 if (base_face_id == DEFAULT_FACE_ID
2581 && FRAME_WINDOW_P (it->f))
2582 {
2583 if (NATNUMP (current_buffer->extra_line_spacing))
2584 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2585 else if (FLOATP (current_buffer->extra_line_spacing))
2586 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2587 * FRAME_LINE_HEIGHT (it->f));
2588 else if (it->f->extra_line_spacing > 0)
2589 it->extra_line_spacing = it->f->extra_line_spacing;
2590 it->max_extra_line_spacing = 0;
2591 }
2592
2593 /* If realized faces have been removed, e.g. because of face
2594 attribute changes of named faces, recompute them. When running
2595 in batch mode, the face cache of the initial frame is null. If
2596 we happen to get called, make a dummy face cache. */
2597 if (FRAME_FACE_CACHE (it->f) == NULL)
2598 init_frame_faces (it->f);
2599 if (FRAME_FACE_CACHE (it->f)->used == 0)
2600 recompute_basic_faces (it->f);
2601
2602 /* Current value of the `slice', `space-width', and 'height' properties. */
2603 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2604 it->space_width = Qnil;
2605 it->font_height = Qnil;
2606 it->override_ascent = -1;
2607
2608 /* Are control characters displayed as `^C'? */
2609 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2610
2611 /* -1 means everything between a CR and the following line end
2612 is invisible. >0 means lines indented more than this value are
2613 invisible. */
2614 it->selective = (INTEGERP (current_buffer->selective_display)
2615 ? XFASTINT (current_buffer->selective_display)
2616 : (!NILP (current_buffer->selective_display)
2617 ? -1 : 0));
2618 it->selective_display_ellipsis_p
2619 = !NILP (current_buffer->selective_display_ellipses);
2620
2621 /* Display table to use. */
2622 it->dp = window_display_table (w);
2623
2624 /* Are multibyte characters enabled in current_buffer? */
2625 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2626
2627 /* Non-zero if we should highlight the region. */
2628 highlight_region_p
2629 = (!NILP (Vtransient_mark_mode)
2630 && !NILP (current_buffer->mark_active)
2631 && XMARKER (current_buffer->mark)->buffer != 0);
2632
2633 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2634 start and end of a visible region in window IT->w. Set both to
2635 -1 to indicate no region. */
2636 if (highlight_region_p
2637 /* Maybe highlight only in selected window. */
2638 && (/* Either show region everywhere. */
2639 highlight_nonselected_windows
2640 /* Or show region in the selected window. */
2641 || w == XWINDOW (selected_window)
2642 /* Or show the region if we are in the mini-buffer and W is
2643 the window the mini-buffer refers to. */
2644 || (MINI_WINDOW_P (XWINDOW (selected_window))
2645 && WINDOWP (minibuf_selected_window)
2646 && w == XWINDOW (minibuf_selected_window))))
2647 {
2648 int charpos = marker_position (current_buffer->mark);
2649 it->region_beg_charpos = min (PT, charpos);
2650 it->region_end_charpos = max (PT, charpos);
2651 }
2652 else
2653 it->region_beg_charpos = it->region_end_charpos = -1;
2654
2655 /* Get the position at which the redisplay_end_trigger hook should
2656 be run, if it is to be run at all. */
2657 if (MARKERP (w->redisplay_end_trigger)
2658 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2659 it->redisplay_end_trigger_charpos
2660 = marker_position (w->redisplay_end_trigger);
2661 else if (INTEGERP (w->redisplay_end_trigger))
2662 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2663
2664 /* Correct bogus values of tab_width. */
2665 it->tab_width = XINT (current_buffer->tab_width);
2666 if (it->tab_width <= 0 || it->tab_width > 1000)
2667 it->tab_width = 8;
2668
2669 /* Are lines in the display truncated? */
2670 if (base_face_id != DEFAULT_FACE_ID
2671 || XINT (it->w->hscroll)
2672 || (! WINDOW_FULL_WIDTH_P (it->w)
2673 && ((!NILP (Vtruncate_partial_width_windows)
2674 && !INTEGERP (Vtruncate_partial_width_windows))
2675 || (INTEGERP (Vtruncate_partial_width_windows)
2676 && (WINDOW_TOTAL_COLS (it->w)
2677 < XINT (Vtruncate_partial_width_windows))))))
2678 it->line_wrap = TRUNCATE;
2679 else if (NILP (current_buffer->truncate_lines))
2680 it->line_wrap = NILP (current_buffer->word_wrap)
2681 ? WINDOW_WRAP : WORD_WRAP;
2682 else
2683 it->line_wrap = TRUNCATE;
2684
2685 /* Get dimensions of truncation and continuation glyphs. These are
2686 displayed as fringe bitmaps under X, so we don't need them for such
2687 frames. */
2688 if (!FRAME_WINDOW_P (it->f))
2689 {
2690 if (it->line_wrap == TRUNCATE)
2691 {
2692 /* We will need the truncation glyph. */
2693 xassert (it->glyph_row == NULL);
2694 produce_special_glyphs (it, IT_TRUNCATION);
2695 it->truncation_pixel_width = it->pixel_width;
2696 }
2697 else
2698 {
2699 /* We will need the continuation glyph. */
2700 xassert (it->glyph_row == NULL);
2701 produce_special_glyphs (it, IT_CONTINUATION);
2702 it->continuation_pixel_width = it->pixel_width;
2703 }
2704
2705 /* Reset these values to zero because the produce_special_glyphs
2706 above has changed them. */
2707 it->pixel_width = it->ascent = it->descent = 0;
2708 it->phys_ascent = it->phys_descent = 0;
2709 }
2710
2711 /* Set this after getting the dimensions of truncation and
2712 continuation glyphs, so that we don't produce glyphs when calling
2713 produce_special_glyphs, above. */
2714 it->glyph_row = row;
2715 it->area = TEXT_AREA;
2716
2717 /* Get the dimensions of the display area. The display area
2718 consists of the visible window area plus a horizontally scrolled
2719 part to the left of the window. All x-values are relative to the
2720 start of this total display area. */
2721 if (base_face_id != DEFAULT_FACE_ID)
2722 {
2723 /* Mode lines, menu bar in terminal frames. */
2724 it->first_visible_x = 0;
2725 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2726 }
2727 else
2728 {
2729 it->first_visible_x
2730 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2731 it->last_visible_x = (it->first_visible_x
2732 + window_box_width (w, TEXT_AREA));
2733
2734 /* If we truncate lines, leave room for the truncator glyph(s) at
2735 the right margin. Otherwise, leave room for the continuation
2736 glyph(s). Truncation and continuation glyphs are not inserted
2737 for window-based redisplay. */
2738 if (!FRAME_WINDOW_P (it->f))
2739 {
2740 if (it->line_wrap == TRUNCATE)
2741 it->last_visible_x -= it->truncation_pixel_width;
2742 else
2743 it->last_visible_x -= it->continuation_pixel_width;
2744 }
2745
2746 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2747 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2748 }
2749
2750 /* Leave room for a border glyph. */
2751 if (!FRAME_WINDOW_P (it->f)
2752 && !WINDOW_RIGHTMOST_P (it->w))
2753 it->last_visible_x -= 1;
2754
2755 it->last_visible_y = window_text_bottom_y (w);
2756
2757 /* For mode lines and alike, arrange for the first glyph having a
2758 left box line if the face specifies a box. */
2759 if (base_face_id != DEFAULT_FACE_ID)
2760 {
2761 struct face *face;
2762
2763 it->face_id = remapped_base_face_id;
2764
2765 /* If we have a boxed mode line, make the first character appear
2766 with a left box line. */
2767 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2768 if (face->box != FACE_NO_BOX)
2769 it->start_of_box_run_p = 1;
2770 }
2771
2772 /* If a buffer position was specified, set the iterator there,
2773 getting overlays and face properties from that position. */
2774 if (charpos >= BUF_BEG (current_buffer))
2775 {
2776 it->end_charpos = ZV;
2777 it->face_id = -1;
2778 IT_CHARPOS (*it) = charpos;
2779
2780 /* Compute byte position if not specified. */
2781 if (bytepos < charpos)
2782 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2783 else
2784 IT_BYTEPOS (*it) = bytepos;
2785
2786 it->start = it->current;
2787
2788 /* Compute faces etc. */
2789 reseat (it, it->current.pos, 1);
2790 }
2791
2792 CHECK_IT (it);
2793 }
2794
2795
2796 /* Initialize IT for the display of window W with window start POS. */
2797
2798 void
2799 start_display (it, w, pos)
2800 struct it *it;
2801 struct window *w;
2802 struct text_pos pos;
2803 {
2804 struct glyph_row *row;
2805 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2806
2807 row = w->desired_matrix->rows + first_vpos;
2808 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2809 it->first_vpos = first_vpos;
2810
2811 /* Don't reseat to previous visible line start if current start
2812 position is in a string or image. */
2813 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2814 {
2815 int start_at_line_beg_p;
2816 int first_y = it->current_y;
2817
2818 /* If window start is not at a line start, skip forward to POS to
2819 get the correct continuation lines width. */
2820 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2821 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2822 if (!start_at_line_beg_p)
2823 {
2824 int new_x;
2825
2826 reseat_at_previous_visible_line_start (it);
2827 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2828
2829 new_x = it->current_x + it->pixel_width;
2830
2831 /* If lines are continued, this line may end in the middle
2832 of a multi-glyph character (e.g. a control character
2833 displayed as \003, or in the middle of an overlay
2834 string). In this case move_it_to above will not have
2835 taken us to the start of the continuation line but to the
2836 end of the continued line. */
2837 if (it->current_x > 0
2838 && it->line_wrap != TRUNCATE /* Lines are continued. */
2839 && (/* And glyph doesn't fit on the line. */
2840 new_x > it->last_visible_x
2841 /* Or it fits exactly and we're on a window
2842 system frame. */
2843 || (new_x == it->last_visible_x
2844 && FRAME_WINDOW_P (it->f))))
2845 {
2846 if (it->current.dpvec_index >= 0
2847 || it->current.overlay_string_index >= 0)
2848 {
2849 set_iterator_to_next (it, 1);
2850 move_it_in_display_line_to (it, -1, -1, 0);
2851 }
2852
2853 it->continuation_lines_width += it->current_x;
2854 }
2855
2856 /* We're starting a new display line, not affected by the
2857 height of the continued line, so clear the appropriate
2858 fields in the iterator structure. */
2859 it->max_ascent = it->max_descent = 0;
2860 it->max_phys_ascent = it->max_phys_descent = 0;
2861
2862 it->current_y = first_y;
2863 it->vpos = 0;
2864 it->current_x = it->hpos = 0;
2865 }
2866 }
2867 }
2868
2869
2870 /* Return 1 if POS is a position in ellipses displayed for invisible
2871 text. W is the window we display, for text property lookup. */
2872
2873 static int
2874 in_ellipses_for_invisible_text_p (pos, w)
2875 struct display_pos *pos;
2876 struct window *w;
2877 {
2878 Lisp_Object prop, window;
2879 int ellipses_p = 0;
2880 int charpos = CHARPOS (pos->pos);
2881
2882 /* If POS specifies a position in a display vector, this might
2883 be for an ellipsis displayed for invisible text. We won't
2884 get the iterator set up for delivering that ellipsis unless
2885 we make sure that it gets aware of the invisible text. */
2886 if (pos->dpvec_index >= 0
2887 && pos->overlay_string_index < 0
2888 && CHARPOS (pos->string_pos) < 0
2889 && charpos > BEGV
2890 && (XSETWINDOW (window, w),
2891 prop = Fget_char_property (make_number (charpos),
2892 Qinvisible, window),
2893 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2894 {
2895 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2896 window);
2897 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2898 }
2899
2900 return ellipses_p;
2901 }
2902
2903
2904 /* Initialize IT for stepping through current_buffer in window W,
2905 starting at position POS that includes overlay string and display
2906 vector/ control character translation position information. Value
2907 is zero if there are overlay strings with newlines at POS. */
2908
2909 static int
2910 init_from_display_pos (it, w, pos)
2911 struct it *it;
2912 struct window *w;
2913 struct display_pos *pos;
2914 {
2915 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2916 int i, overlay_strings_with_newlines = 0;
2917
2918 /* If POS specifies a position in a display vector, this might
2919 be for an ellipsis displayed for invisible text. We won't
2920 get the iterator set up for delivering that ellipsis unless
2921 we make sure that it gets aware of the invisible text. */
2922 if (in_ellipses_for_invisible_text_p (pos, w))
2923 {
2924 --charpos;
2925 bytepos = 0;
2926 }
2927
2928 /* Keep in mind: the call to reseat in init_iterator skips invisible
2929 text, so we might end up at a position different from POS. This
2930 is only a problem when POS is a row start after a newline and an
2931 overlay starts there with an after-string, and the overlay has an
2932 invisible property. Since we don't skip invisible text in
2933 display_line and elsewhere immediately after consuming the
2934 newline before the row start, such a POS will not be in a string,
2935 but the call to init_iterator below will move us to the
2936 after-string. */
2937 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2938
2939 /* This only scans the current chunk -- it should scan all chunks.
2940 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2941 to 16 in 22.1 to make this a lesser problem. */
2942 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2943 {
2944 const char *s = SDATA (it->overlay_strings[i]);
2945 const char *e = s + SBYTES (it->overlay_strings[i]);
2946
2947 while (s < e && *s != '\n')
2948 ++s;
2949
2950 if (s < e)
2951 {
2952 overlay_strings_with_newlines = 1;
2953 break;
2954 }
2955 }
2956
2957 /* If position is within an overlay string, set up IT to the right
2958 overlay string. */
2959 if (pos->overlay_string_index >= 0)
2960 {
2961 int relative_index;
2962
2963 /* If the first overlay string happens to have a `display'
2964 property for an image, the iterator will be set up for that
2965 image, and we have to undo that setup first before we can
2966 correct the overlay string index. */
2967 if (it->method == GET_FROM_IMAGE)
2968 pop_it (it);
2969
2970 /* We already have the first chunk of overlay strings in
2971 IT->overlay_strings. Load more until the one for
2972 pos->overlay_string_index is in IT->overlay_strings. */
2973 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2974 {
2975 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2976 it->current.overlay_string_index = 0;
2977 while (n--)
2978 {
2979 load_overlay_strings (it, 0);
2980 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2981 }
2982 }
2983
2984 it->current.overlay_string_index = pos->overlay_string_index;
2985 relative_index = (it->current.overlay_string_index
2986 % OVERLAY_STRING_CHUNK_SIZE);
2987 it->string = it->overlay_strings[relative_index];
2988 xassert (STRINGP (it->string));
2989 it->current.string_pos = pos->string_pos;
2990 it->method = GET_FROM_STRING;
2991 }
2992
2993 if (CHARPOS (pos->string_pos) >= 0)
2994 {
2995 /* Recorded position is not in an overlay string, but in another
2996 string. This can only be a string from a `display' property.
2997 IT should already be filled with that string. */
2998 it->current.string_pos = pos->string_pos;
2999 xassert (STRINGP (it->string));
3000 }
3001
3002 /* Restore position in display vector translations, control
3003 character translations or ellipses. */
3004 if (pos->dpvec_index >= 0)
3005 {
3006 if (it->dpvec == NULL)
3007 get_next_display_element (it);
3008 xassert (it->dpvec && it->current.dpvec_index == 0);
3009 it->current.dpvec_index = pos->dpvec_index;
3010 }
3011
3012 CHECK_IT (it);
3013 return !overlay_strings_with_newlines;
3014 }
3015
3016
3017 /* Initialize IT for stepping through current_buffer in window W
3018 starting at ROW->start. */
3019
3020 static void
3021 init_to_row_start (it, w, row)
3022 struct it *it;
3023 struct window *w;
3024 struct glyph_row *row;
3025 {
3026 init_from_display_pos (it, w, &row->start);
3027 it->start = row->start;
3028 it->continuation_lines_width = row->continuation_lines_width;
3029 CHECK_IT (it);
3030 }
3031
3032
3033 /* Initialize IT for stepping through current_buffer in window W
3034 starting in the line following ROW, i.e. starting at ROW->end.
3035 Value is zero if there are overlay strings with newlines at ROW's
3036 end position. */
3037
3038 static int
3039 init_to_row_end (it, w, row)
3040 struct it *it;
3041 struct window *w;
3042 struct glyph_row *row;
3043 {
3044 int success = 0;
3045
3046 if (init_from_display_pos (it, w, &row->end))
3047 {
3048 if (row->continued_p)
3049 it->continuation_lines_width
3050 = row->continuation_lines_width + row->pixel_width;
3051 CHECK_IT (it);
3052 success = 1;
3053 }
3054
3055 return success;
3056 }
3057
3058
3059
3060 \f
3061 /***********************************************************************
3062 Text properties
3063 ***********************************************************************/
3064
3065 /* Called when IT reaches IT->stop_charpos. Handle text property and
3066 overlay changes. Set IT->stop_charpos to the next position where
3067 to stop. */
3068
3069 static void
3070 handle_stop (it)
3071 struct it *it;
3072 {
3073 enum prop_handled handled;
3074 int handle_overlay_change_p;
3075 struct props *p;
3076
3077 it->dpvec = NULL;
3078 it->current.dpvec_index = -1;
3079 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3080 it->ignore_overlay_strings_at_pos_p = 0;
3081 it->ellipsis_p = 0;
3082
3083 /* Use face of preceding text for ellipsis (if invisible) */
3084 if (it->selective_display_ellipsis_p)
3085 it->saved_face_id = it->face_id;
3086
3087 do
3088 {
3089 handled = HANDLED_NORMALLY;
3090
3091 /* Call text property handlers. */
3092 for (p = it_props; p->handler; ++p)
3093 {
3094 handled = p->handler (it);
3095
3096 if (handled == HANDLED_RECOMPUTE_PROPS)
3097 break;
3098 else if (handled == HANDLED_RETURN)
3099 {
3100 /* We still want to show before and after strings from
3101 overlays even if the actual buffer text is replaced. */
3102 if (!handle_overlay_change_p
3103 || it->sp > 1
3104 || !get_overlay_strings_1 (it, 0, 0))
3105 {
3106 if (it->ellipsis_p)
3107 setup_for_ellipsis (it, 0);
3108 /* When handling a display spec, we might load an
3109 empty string. In that case, discard it here. We
3110 used to discard it in handle_single_display_spec,
3111 but that causes get_overlay_strings_1, above, to
3112 ignore overlay strings that we must check. */
3113 if (STRINGP (it->string) && !SCHARS (it->string))
3114 pop_it (it);
3115 return;
3116 }
3117 else if (STRINGP (it->string) && !SCHARS (it->string))
3118 pop_it (it);
3119 else
3120 {
3121 it->ignore_overlay_strings_at_pos_p = 1;
3122 it->string_from_display_prop_p = 0;
3123 handle_overlay_change_p = 0;
3124 }
3125 handled = HANDLED_RECOMPUTE_PROPS;
3126 break;
3127 }
3128 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3129 handle_overlay_change_p = 0;
3130 }
3131
3132 if (handled != HANDLED_RECOMPUTE_PROPS)
3133 {
3134 /* Don't check for overlay strings below when set to deliver
3135 characters from a display vector. */
3136 if (it->method == GET_FROM_DISPLAY_VECTOR)
3137 handle_overlay_change_p = 0;
3138
3139 /* Handle overlay changes.
3140 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3141 if it finds overlays. */
3142 if (handle_overlay_change_p)
3143 handled = handle_overlay_change (it);
3144 }
3145
3146 if (it->ellipsis_p)
3147 {
3148 setup_for_ellipsis (it, 0);
3149 break;
3150 }
3151 }
3152 while (handled == HANDLED_RECOMPUTE_PROPS);
3153
3154 /* Determine where to stop next. */
3155 if (handled == HANDLED_NORMALLY)
3156 compute_stop_pos (it);
3157 }
3158
3159
3160 /* Compute IT->stop_charpos from text property and overlay change
3161 information for IT's current position. */
3162
3163 static void
3164 compute_stop_pos (it)
3165 struct it *it;
3166 {
3167 register INTERVAL iv, next_iv;
3168 Lisp_Object object, limit, position;
3169 EMACS_INT charpos, bytepos;
3170
3171 /* If nowhere else, stop at the end. */
3172 it->stop_charpos = it->end_charpos;
3173
3174 if (STRINGP (it->string))
3175 {
3176 /* Strings are usually short, so don't limit the search for
3177 properties. */
3178 object = it->string;
3179 limit = Qnil;
3180 charpos = IT_STRING_CHARPOS (*it);
3181 bytepos = IT_STRING_BYTEPOS (*it);
3182 }
3183 else
3184 {
3185 EMACS_INT pos;
3186
3187 /* If next overlay change is in front of the current stop pos
3188 (which is IT->end_charpos), stop there. Note: value of
3189 next_overlay_change is point-max if no overlay change
3190 follows. */
3191 charpos = IT_CHARPOS (*it);
3192 bytepos = IT_BYTEPOS (*it);
3193 pos = next_overlay_change (charpos);
3194 if (pos < it->stop_charpos)
3195 it->stop_charpos = pos;
3196
3197 /* If showing the region, we have to stop at the region
3198 start or end because the face might change there. */
3199 if (it->region_beg_charpos > 0)
3200 {
3201 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3202 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3203 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3204 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3205 }
3206
3207 /* Set up variables for computing the stop position from text
3208 property changes. */
3209 XSETBUFFER (object, current_buffer);
3210 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3211 }
3212
3213 /* Get the interval containing IT's position. Value is a null
3214 interval if there isn't such an interval. */
3215 position = make_number (charpos);
3216 iv = validate_interval_range (object, &position, &position, 0);
3217 if (!NULL_INTERVAL_P (iv))
3218 {
3219 Lisp_Object values_here[LAST_PROP_IDX];
3220 struct props *p;
3221
3222 /* Get properties here. */
3223 for (p = it_props; p->handler; ++p)
3224 values_here[p->idx] = textget (iv->plist, *p->name);
3225
3226 /* Look for an interval following iv that has different
3227 properties. */
3228 for (next_iv = next_interval (iv);
3229 (!NULL_INTERVAL_P (next_iv)
3230 && (NILP (limit)
3231 || XFASTINT (limit) > next_iv->position));
3232 next_iv = next_interval (next_iv))
3233 {
3234 for (p = it_props; p->handler; ++p)
3235 {
3236 Lisp_Object new_value;
3237
3238 new_value = textget (next_iv->plist, *p->name);
3239 if (!EQ (values_here[p->idx], new_value))
3240 break;
3241 }
3242
3243 if (p->handler)
3244 break;
3245 }
3246
3247 if (!NULL_INTERVAL_P (next_iv))
3248 {
3249 if (INTEGERP (limit)
3250 && next_iv->position >= XFASTINT (limit))
3251 /* No text property change up to limit. */
3252 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3253 else
3254 /* Text properties change in next_iv. */
3255 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3256 }
3257 }
3258
3259 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3260 it->stop_charpos, it->string);
3261
3262 xassert (STRINGP (it->string)
3263 || (it->stop_charpos >= BEGV
3264 && it->stop_charpos >= IT_CHARPOS (*it)));
3265 }
3266
3267
3268 /* Return the position of the next overlay change after POS in
3269 current_buffer. Value is point-max if no overlay change
3270 follows. This is like `next-overlay-change' but doesn't use
3271 xmalloc. */
3272
3273 static EMACS_INT
3274 next_overlay_change (pos)
3275 EMACS_INT pos;
3276 {
3277 int noverlays;
3278 EMACS_INT endpos;
3279 Lisp_Object *overlays;
3280 int i;
3281
3282 /* Get all overlays at the given position. */
3283 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3284
3285 /* If any of these overlays ends before endpos,
3286 use its ending point instead. */
3287 for (i = 0; i < noverlays; ++i)
3288 {
3289 Lisp_Object oend;
3290 EMACS_INT oendpos;
3291
3292 oend = OVERLAY_END (overlays[i]);
3293 oendpos = OVERLAY_POSITION (oend);
3294 endpos = min (endpos, oendpos);
3295 }
3296
3297 return endpos;
3298 }
3299
3300
3301 \f
3302 /***********************************************************************
3303 Fontification
3304 ***********************************************************************/
3305
3306 /* Handle changes in the `fontified' property of the current buffer by
3307 calling hook functions from Qfontification_functions to fontify
3308 regions of text. */
3309
3310 static enum prop_handled
3311 handle_fontified_prop (it)
3312 struct it *it;
3313 {
3314 Lisp_Object prop, pos;
3315 enum prop_handled handled = HANDLED_NORMALLY;
3316
3317 if (!NILP (Vmemory_full))
3318 return handled;
3319
3320 /* Get the value of the `fontified' property at IT's current buffer
3321 position. (The `fontified' property doesn't have a special
3322 meaning in strings.) If the value is nil, call functions from
3323 Qfontification_functions. */
3324 if (!STRINGP (it->string)
3325 && it->s == NULL
3326 && !NILP (Vfontification_functions)
3327 && !NILP (Vrun_hooks)
3328 && (pos = make_number (IT_CHARPOS (*it)),
3329 prop = Fget_char_property (pos, Qfontified, Qnil),
3330 /* Ignore the special cased nil value always present at EOB since
3331 no amount of fontifying will be able to change it. */
3332 NILP (prop) && IT_CHARPOS (*it) < Z))
3333 {
3334 int count = SPECPDL_INDEX ();
3335 Lisp_Object val;
3336
3337 val = Vfontification_functions;
3338 specbind (Qfontification_functions, Qnil);
3339
3340 xassert (it->end_charpos == ZV);
3341
3342 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3343 safe_call1 (val, pos);
3344 else
3345 {
3346 Lisp_Object globals, fn;
3347 struct gcpro gcpro1, gcpro2;
3348
3349 globals = Qnil;
3350 GCPRO2 (val, globals);
3351
3352 for (; CONSP (val); val = XCDR (val))
3353 {
3354 fn = XCAR (val);
3355
3356 if (EQ (fn, Qt))
3357 {
3358 /* A value of t indicates this hook has a local
3359 binding; it means to run the global binding too.
3360 In a global value, t should not occur. If it
3361 does, we must ignore it to avoid an endless
3362 loop. */
3363 for (globals = Fdefault_value (Qfontification_functions);
3364 CONSP (globals);
3365 globals = XCDR (globals))
3366 {
3367 fn = XCAR (globals);
3368 if (!EQ (fn, Qt))
3369 safe_call1 (fn, pos);
3370 }
3371 }
3372 else
3373 safe_call1 (fn, pos);
3374 }
3375
3376 UNGCPRO;
3377 }
3378
3379 unbind_to (count, Qnil);
3380
3381 /* The fontification code may have added/removed text.
3382 It could do even a lot worse, but let's at least protect against
3383 the most obvious case where only the text past `pos' gets changed',
3384 as is/was done in grep.el where some escapes sequences are turned
3385 into face properties (bug#7876). */
3386 it->end_charpos = ZV;
3387
3388 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3389 something. This avoids an endless loop if they failed to
3390 fontify the text for which reason ever. */
3391 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3392 handled = HANDLED_RECOMPUTE_PROPS;
3393 }
3394
3395 return handled;
3396 }
3397
3398
3399 \f
3400 /***********************************************************************
3401 Faces
3402 ***********************************************************************/
3403
3404 /* Set up iterator IT from face properties at its current position.
3405 Called from handle_stop. */
3406
3407 static enum prop_handled
3408 handle_face_prop (it)
3409 struct it *it;
3410 {
3411 int new_face_id;
3412 EMACS_INT next_stop;
3413
3414 if (!STRINGP (it->string))
3415 {
3416 new_face_id
3417 = face_at_buffer_position (it->w,
3418 IT_CHARPOS (*it),
3419 it->region_beg_charpos,
3420 it->region_end_charpos,
3421 &next_stop,
3422 (IT_CHARPOS (*it)
3423 + TEXT_PROP_DISTANCE_LIMIT),
3424 0, it->base_face_id);
3425
3426 /* Is this a start of a run of characters with box face?
3427 Caveat: this can be called for a freshly initialized
3428 iterator; face_id is -1 in this case. We know that the new
3429 face will not change until limit, i.e. if the new face has a
3430 box, all characters up to limit will have one. But, as
3431 usual, we don't know whether limit is really the end. */
3432 if (new_face_id != it->face_id)
3433 {
3434 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3435
3436 /* If new face has a box but old face has not, this is
3437 the start of a run of characters with box, i.e. it has
3438 a shadow on the left side. The value of face_id of the
3439 iterator will be -1 if this is the initial call that gets
3440 the face. In this case, we have to look in front of IT's
3441 position and see whether there is a face != new_face_id. */
3442 it->start_of_box_run_p
3443 = (new_face->box != FACE_NO_BOX
3444 && (it->face_id >= 0
3445 || IT_CHARPOS (*it) == BEG
3446 || new_face_id != face_before_it_pos (it)));
3447 it->face_box_p = new_face->box != FACE_NO_BOX;
3448 }
3449 }
3450 else
3451 {
3452 int base_face_id, bufpos;
3453 int i;
3454 Lisp_Object from_overlay
3455 = (it->current.overlay_string_index >= 0
3456 ? it->string_overlays[it->current.overlay_string_index]
3457 : Qnil);
3458
3459 /* See if we got to this string directly or indirectly from
3460 an overlay property. That includes the before-string or
3461 after-string of an overlay, strings in display properties
3462 provided by an overlay, their text properties, etc.
3463
3464 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3465 if (! NILP (from_overlay))
3466 for (i = it->sp - 1; i >= 0; i--)
3467 {
3468 if (it->stack[i].current.overlay_string_index >= 0)
3469 from_overlay
3470 = it->string_overlays[it->stack[i].current.overlay_string_index];
3471 else if (! NILP (it->stack[i].from_overlay))
3472 from_overlay = it->stack[i].from_overlay;
3473
3474 if (!NILP (from_overlay))
3475 break;
3476 }
3477
3478 if (! NILP (from_overlay))
3479 {
3480 bufpos = IT_CHARPOS (*it);
3481 /* For a string from an overlay, the base face depends
3482 only on text properties and ignores overlays. */
3483 base_face_id
3484 = face_for_overlay_string (it->w,
3485 IT_CHARPOS (*it),
3486 it->region_beg_charpos,
3487 it->region_end_charpos,
3488 &next_stop,
3489 (IT_CHARPOS (*it)
3490 + TEXT_PROP_DISTANCE_LIMIT),
3491 0,
3492 from_overlay);
3493 }
3494 else
3495 {
3496 bufpos = 0;
3497
3498 /* For strings from a `display' property, use the face at
3499 IT's current buffer position as the base face to merge
3500 with, so that overlay strings appear in the same face as
3501 surrounding text, unless they specify their own
3502 faces. */
3503 base_face_id = underlying_face_id (it);
3504 }
3505
3506 new_face_id = face_at_string_position (it->w,
3507 it->string,
3508 IT_STRING_CHARPOS (*it),
3509 bufpos,
3510 it->region_beg_charpos,
3511 it->region_end_charpos,
3512 &next_stop,
3513 base_face_id, 0);
3514
3515 /* Is this a start of a run of characters with box? Caveat:
3516 this can be called for a freshly allocated iterator; face_id
3517 is -1 is this case. We know that the new face will not
3518 change until the next check pos, i.e. if the new face has a
3519 box, all characters up to that position will have a
3520 box. But, as usual, we don't know whether that position
3521 is really the end. */
3522 if (new_face_id != it->face_id)
3523 {
3524 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3525 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3526
3527 /* If new face has a box but old face hasn't, this is the
3528 start of a run of characters with box, i.e. it has a
3529 shadow on the left side. */
3530 it->start_of_box_run_p
3531 = new_face->box && (old_face == NULL || !old_face->box);
3532 it->face_box_p = new_face->box != FACE_NO_BOX;
3533 }
3534 }
3535
3536 it->face_id = new_face_id;
3537 return HANDLED_NORMALLY;
3538 }
3539
3540
3541 /* Return the ID of the face ``underlying'' IT's current position,
3542 which is in a string. If the iterator is associated with a
3543 buffer, return the face at IT's current buffer position.
3544 Otherwise, use the iterator's base_face_id. */
3545
3546 static int
3547 underlying_face_id (it)
3548 struct it *it;
3549 {
3550 int face_id = it->base_face_id, i;
3551
3552 xassert (STRINGP (it->string));
3553
3554 for (i = it->sp - 1; i >= 0; --i)
3555 if (NILP (it->stack[i].string))
3556 face_id = it->stack[i].face_id;
3557
3558 return face_id;
3559 }
3560
3561
3562 /* Compute the face one character before or after the current position
3563 of IT. BEFORE_P non-zero means get the face in front of IT's
3564 position. Value is the id of the face. */
3565
3566 static int
3567 face_before_or_after_it_pos (it, before_p)
3568 struct it *it;
3569 int before_p;
3570 {
3571 int face_id, limit;
3572 EMACS_INT next_check_charpos;
3573 struct text_pos pos;
3574
3575 xassert (it->s == NULL);
3576
3577 if (STRINGP (it->string))
3578 {
3579 int bufpos, base_face_id;
3580
3581 /* No face change past the end of the string (for the case
3582 we are padding with spaces). No face change before the
3583 string start. */
3584 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3585 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3586 return it->face_id;
3587
3588 /* Set pos to the position before or after IT's current position. */
3589 if (before_p)
3590 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3591 else
3592 /* For composition, we must check the character after the
3593 composition. */
3594 pos = (it->what == IT_COMPOSITION
3595 ? string_pos (IT_STRING_CHARPOS (*it)
3596 + it->cmp_it.nchars, it->string)
3597 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3598
3599 if (it->current.overlay_string_index >= 0)
3600 bufpos = IT_CHARPOS (*it);
3601 else
3602 bufpos = 0;
3603
3604 base_face_id = underlying_face_id (it);
3605
3606 /* Get the face for ASCII, or unibyte. */
3607 face_id = face_at_string_position (it->w,
3608 it->string,
3609 CHARPOS (pos),
3610 bufpos,
3611 it->region_beg_charpos,
3612 it->region_end_charpos,
3613 &next_check_charpos,
3614 base_face_id, 0);
3615
3616 /* Correct the face for charsets different from ASCII. Do it
3617 for the multibyte case only. The face returned above is
3618 suitable for unibyte text if IT->string is unibyte. */
3619 if (STRING_MULTIBYTE (it->string))
3620 {
3621 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3622 int c, len;
3623 struct face *face = FACE_FROM_ID (it->f, face_id);
3624
3625 c = string_char_and_length (p, &len);
3626 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3627 }
3628 }
3629 else
3630 {
3631 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3632 || (IT_CHARPOS (*it) <= BEGV && before_p))
3633 return it->face_id;
3634
3635 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3636 pos = it->current.pos;
3637
3638 if (before_p)
3639 DEC_TEXT_POS (pos, it->multibyte_p);
3640 else
3641 {
3642 if (it->what == IT_COMPOSITION)
3643 /* For composition, we must check the position after the
3644 composition. */
3645 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3646 else
3647 INC_TEXT_POS (pos, it->multibyte_p);
3648 }
3649
3650 /* Determine face for CHARSET_ASCII, or unibyte. */
3651 face_id = face_at_buffer_position (it->w,
3652 CHARPOS (pos),
3653 it->region_beg_charpos,
3654 it->region_end_charpos,
3655 &next_check_charpos,
3656 limit, 0, -1);
3657
3658 /* Correct the face for charsets different from ASCII. Do it
3659 for the multibyte case only. The face returned above is
3660 suitable for unibyte text if current_buffer is unibyte. */
3661 if (it->multibyte_p)
3662 {
3663 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3664 struct face *face = FACE_FROM_ID (it->f, face_id);
3665 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3666 }
3667 }
3668
3669 return face_id;
3670 }
3671
3672
3673 \f
3674 /***********************************************************************
3675 Invisible text
3676 ***********************************************************************/
3677
3678 /* Set up iterator IT from invisible properties at its current
3679 position. Called from handle_stop. */
3680
3681 static enum prop_handled
3682 handle_invisible_prop (it)
3683 struct it *it;
3684 {
3685 enum prop_handled handled = HANDLED_NORMALLY;
3686
3687 if (STRINGP (it->string))
3688 {
3689 extern Lisp_Object Qinvisible;
3690 Lisp_Object prop, end_charpos, limit, charpos;
3691
3692 /* Get the value of the invisible text property at the
3693 current position. Value will be nil if there is no such
3694 property. */
3695 charpos = make_number (IT_STRING_CHARPOS (*it));
3696 prop = Fget_text_property (charpos, Qinvisible, it->string);
3697
3698 if (!NILP (prop)
3699 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3700 {
3701 handled = HANDLED_RECOMPUTE_PROPS;
3702
3703 /* Get the position at which the next change of the
3704 invisible text property can be found in IT->string.
3705 Value will be nil if the property value is the same for
3706 all the rest of IT->string. */
3707 XSETINT (limit, SCHARS (it->string));
3708 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3709 it->string, limit);
3710
3711 /* Text at current position is invisible. The next
3712 change in the property is at position end_charpos.
3713 Move IT's current position to that position. */
3714 if (INTEGERP (end_charpos)
3715 && XFASTINT (end_charpos) < XFASTINT (limit))
3716 {
3717 struct text_pos old;
3718 old = it->current.string_pos;
3719 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3720 compute_string_pos (&it->current.string_pos, old, it->string);
3721 }
3722 else
3723 {
3724 /* The rest of the string is invisible. If this is an
3725 overlay string, proceed with the next overlay string
3726 or whatever comes and return a character from there. */
3727 if (it->current.overlay_string_index >= 0)
3728 {
3729 next_overlay_string (it);
3730 /* Don't check for overlay strings when we just
3731 finished processing them. */
3732 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3733 }
3734 else
3735 {
3736 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3737 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3738 }
3739 }
3740 }
3741 }
3742 else
3743 {
3744 int invis_p;
3745 EMACS_INT newpos, next_stop, start_charpos;
3746 Lisp_Object pos, prop, overlay;
3747
3748 /* First of all, is there invisible text at this position? */
3749 start_charpos = IT_CHARPOS (*it);
3750 pos = make_number (IT_CHARPOS (*it));
3751 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3752 &overlay);
3753 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3754
3755 /* If we are on invisible text, skip over it. */
3756 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3757 {
3758 /* Record whether we have to display an ellipsis for the
3759 invisible text. */
3760 int display_ellipsis_p = invis_p == 2;
3761
3762 handled = HANDLED_RECOMPUTE_PROPS;
3763
3764 /* Loop skipping over invisible text. The loop is left at
3765 ZV or with IT on the first char being visible again. */
3766 do
3767 {
3768 /* Try to skip some invisible text. Return value is the
3769 position reached which can be equal to IT's position
3770 if there is nothing invisible here. This skips both
3771 over invisible text properties and overlays with
3772 invisible property. */
3773 newpos = skip_invisible (IT_CHARPOS (*it),
3774 &next_stop, ZV, it->window);
3775
3776 /* If we skipped nothing at all we weren't at invisible
3777 text in the first place. If everything to the end of
3778 the buffer was skipped, end the loop. */
3779 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3780 invis_p = 0;
3781 else
3782 {
3783 /* We skipped some characters but not necessarily
3784 all there are. Check if we ended up on visible
3785 text. Fget_char_property returns the property of
3786 the char before the given position, i.e. if we
3787 get invis_p = 0, this means that the char at
3788 newpos is visible. */
3789 pos = make_number (newpos);
3790 prop = Fget_char_property (pos, Qinvisible, it->window);
3791 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3792 }
3793
3794 /* If we ended up on invisible text, proceed to
3795 skip starting with next_stop. */
3796 if (invis_p)
3797 IT_CHARPOS (*it) = next_stop;
3798
3799 /* If there are adjacent invisible texts, don't lose the
3800 second one's ellipsis. */
3801 if (invis_p == 2)
3802 display_ellipsis_p = 1;
3803 }
3804 while (invis_p);
3805
3806 /* The position newpos is now either ZV or on visible text. */
3807 IT_CHARPOS (*it) = newpos;
3808 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3809
3810 /* If there are before-strings at the start of invisible
3811 text, and the text is invisible because of a text
3812 property, arrange to show before-strings because 20.x did
3813 it that way. (If the text is invisible because of an
3814 overlay property instead of a text property, this is
3815 already handled in the overlay code.) */
3816 if (NILP (overlay)
3817 && get_overlay_strings (it, start_charpos))
3818 {
3819 handled = HANDLED_RECOMPUTE_PROPS;
3820 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3821 }
3822 else if (display_ellipsis_p)
3823 {
3824 /* Make sure that the glyphs of the ellipsis will get
3825 correct `charpos' values. If we would not update
3826 it->position here, the glyphs would belong to the
3827 last visible character _before_ the invisible
3828 text, which confuses `set_cursor_from_row'.
3829
3830 We use the last invisible position instead of the
3831 first because this way the cursor is always drawn on
3832 the first "." of the ellipsis, whenever PT is inside
3833 the invisible text. Otherwise the cursor would be
3834 placed _after_ the ellipsis when the point is after the
3835 first invisible character. */
3836 if (!STRINGP (it->object))
3837 {
3838 it->position.charpos = IT_CHARPOS (*it) - 1;
3839 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3840 }
3841 it->ellipsis_p = 1;
3842 /* Let the ellipsis display before
3843 considering any properties of the following char.
3844 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3845 handled = HANDLED_RETURN;
3846 }
3847 }
3848 }
3849
3850 return handled;
3851 }
3852
3853
3854 /* Make iterator IT return `...' next.
3855 Replaces LEN characters from buffer. */
3856
3857 static void
3858 setup_for_ellipsis (it, len)
3859 struct it *it;
3860 int len;
3861 {
3862 /* Use the display table definition for `...'. Invalid glyphs
3863 will be handled by the method returning elements from dpvec. */
3864 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3865 {
3866 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3867 it->dpvec = v->contents;
3868 it->dpend = v->contents + v->header.size;
3869 }
3870 else
3871 {
3872 /* Default `...'. */
3873 it->dpvec = default_invis_vector;
3874 it->dpend = default_invis_vector + 3;
3875 }
3876
3877 it->dpvec_char_len = len;
3878 it->current.dpvec_index = 0;
3879 it->dpvec_face_id = -1;
3880
3881 /* Remember the current face id in case glyphs specify faces.
3882 IT's face is restored in set_iterator_to_next.
3883 saved_face_id was set to preceding char's face in handle_stop. */
3884 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3885 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3886
3887 it->method = GET_FROM_DISPLAY_VECTOR;
3888 it->ellipsis_p = 1;
3889 }
3890
3891
3892 \f
3893 /***********************************************************************
3894 'display' property
3895 ***********************************************************************/
3896
3897 /* Set up iterator IT from `display' property at its current position.
3898 Called from handle_stop.
3899 We return HANDLED_RETURN if some part of the display property
3900 overrides the display of the buffer text itself.
3901 Otherwise we return HANDLED_NORMALLY. */
3902
3903 static enum prop_handled
3904 handle_display_prop (it)
3905 struct it *it;
3906 {
3907 Lisp_Object prop, object, overlay;
3908 struct text_pos *position;
3909 /* Nonzero if some property replaces the display of the text itself. */
3910 int display_replaced_p = 0;
3911
3912 if (STRINGP (it->string))
3913 {
3914 object = it->string;
3915 position = &it->current.string_pos;
3916 }
3917 else
3918 {
3919 XSETWINDOW (object, it->w);
3920 position = &it->current.pos;
3921 }
3922
3923 /* Reset those iterator values set from display property values. */
3924 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3925 it->space_width = Qnil;
3926 it->font_height = Qnil;
3927 it->voffset = 0;
3928
3929 /* We don't support recursive `display' properties, i.e. string
3930 values that have a string `display' property, that have a string
3931 `display' property etc. */
3932 if (!it->string_from_display_prop_p)
3933 it->area = TEXT_AREA;
3934
3935 prop = get_char_property_and_overlay (make_number (position->charpos),
3936 Qdisplay, object, &overlay);
3937 if (NILP (prop))
3938 return HANDLED_NORMALLY;
3939 /* Now OVERLAY is the overlay that gave us this property, or nil
3940 if it was a text property. */
3941
3942 if (!STRINGP (it->string))
3943 object = it->w->buffer;
3944
3945 if (CONSP (prop)
3946 /* Simple properties. */
3947 && !EQ (XCAR (prop), Qimage)
3948 && !EQ (XCAR (prop), Qspace)
3949 && !EQ (XCAR (prop), Qwhen)
3950 && !EQ (XCAR (prop), Qslice)
3951 && !EQ (XCAR (prop), Qspace_width)
3952 && !EQ (XCAR (prop), Qheight)
3953 && !EQ (XCAR (prop), Qraise)
3954 /* Marginal area specifications. */
3955 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3956 && !EQ (XCAR (prop), Qleft_fringe)
3957 && !EQ (XCAR (prop), Qright_fringe)
3958 && !NILP (XCAR (prop)))
3959 {
3960 for (; CONSP (prop); prop = XCDR (prop))
3961 {
3962 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3963 position, display_replaced_p))
3964 {
3965 display_replaced_p = 1;
3966 /* If some text in a string is replaced, `position' no
3967 longer points to the position of `object'. */
3968 if (STRINGP (object))
3969 break;
3970 }
3971 }
3972 }
3973 else if (VECTORP (prop))
3974 {
3975 int i;
3976 for (i = 0; i < ASIZE (prop); ++i)
3977 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3978 position, display_replaced_p))
3979 {
3980 display_replaced_p = 1;
3981 /* If some text in a string is replaced, `position' no
3982 longer points to the position of `object'. */
3983 if (STRINGP (object))
3984 break;
3985 }
3986 }
3987 else
3988 {
3989 if (handle_single_display_spec (it, prop, object, overlay,
3990 position, 0))
3991 display_replaced_p = 1;
3992 }
3993
3994 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3995 }
3996
3997
3998 /* Value is the position of the end of the `display' property starting
3999 at START_POS in OBJECT. */
4000
4001 static struct text_pos
4002 display_prop_end (it, object, start_pos)
4003 struct it *it;
4004 Lisp_Object object;
4005 struct text_pos start_pos;
4006 {
4007 Lisp_Object end;
4008 struct text_pos end_pos;
4009
4010 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4011 Qdisplay, object, Qnil);
4012 CHARPOS (end_pos) = XFASTINT (end);
4013 if (STRINGP (object))
4014 compute_string_pos (&end_pos, start_pos, it->string);
4015 else
4016 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4017
4018 return end_pos;
4019 }
4020
4021
4022 /* Set up IT from a single `display' specification PROP. OBJECT
4023 is the object in which the `display' property was found. *POSITION
4024 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4025 means that we previously saw a display specification which already
4026 replaced text display with something else, for example an image;
4027 we ignore such properties after the first one has been processed.
4028
4029 OVERLAY is the overlay this `display' property came from,
4030 or nil if it was a text property.
4031
4032 If PROP is a `space' or `image' specification, and in some other
4033 cases too, set *POSITION to the position where the `display'
4034 property ends.
4035
4036 Value is non-zero if something was found which replaces the display
4037 of buffer or string text. */
4038
4039 static int
4040 handle_single_display_spec (it, spec, object, overlay, position,
4041 display_replaced_before_p)
4042 struct it *it;
4043 Lisp_Object spec;
4044 Lisp_Object object;
4045 Lisp_Object overlay;
4046 struct text_pos *position;
4047 int display_replaced_before_p;
4048 {
4049 Lisp_Object form;
4050 Lisp_Object location, value;
4051 struct text_pos start_pos, save_pos;
4052 int valid_p;
4053
4054 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4055 If the result is non-nil, use VALUE instead of SPEC. */
4056 form = Qt;
4057 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4058 {
4059 spec = XCDR (spec);
4060 if (!CONSP (spec))
4061 return 0;
4062 form = XCAR (spec);
4063 spec = XCDR (spec);
4064 }
4065
4066 if (!NILP (form) && !EQ (form, Qt))
4067 {
4068 int count = SPECPDL_INDEX ();
4069 struct gcpro gcpro1;
4070
4071 /* Bind `object' to the object having the `display' property, a
4072 buffer or string. Bind `position' to the position in the
4073 object where the property was found, and `buffer-position'
4074 to the current position in the buffer. */
4075 specbind (Qobject, object);
4076 specbind (Qposition, make_number (CHARPOS (*position)));
4077 specbind (Qbuffer_position,
4078 make_number (STRINGP (object)
4079 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4080 GCPRO1 (form);
4081 form = safe_eval (form);
4082 UNGCPRO;
4083 unbind_to (count, Qnil);
4084 }
4085
4086 if (NILP (form))
4087 return 0;
4088
4089 /* Handle `(height HEIGHT)' specifications. */
4090 if (CONSP (spec)
4091 && EQ (XCAR (spec), Qheight)
4092 && CONSP (XCDR (spec)))
4093 {
4094 if (!FRAME_WINDOW_P (it->f))
4095 return 0;
4096
4097 it->font_height = XCAR (XCDR (spec));
4098 if (!NILP (it->font_height))
4099 {
4100 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4101 int new_height = -1;
4102
4103 if (CONSP (it->font_height)
4104 && (EQ (XCAR (it->font_height), Qplus)
4105 || EQ (XCAR (it->font_height), Qminus))
4106 && CONSP (XCDR (it->font_height))
4107 && INTEGERP (XCAR (XCDR (it->font_height))))
4108 {
4109 /* `(+ N)' or `(- N)' where N is an integer. */
4110 int steps = XINT (XCAR (XCDR (it->font_height)));
4111 if (EQ (XCAR (it->font_height), Qplus))
4112 steps = - steps;
4113 it->face_id = smaller_face (it->f, it->face_id, steps);
4114 }
4115 else if (FUNCTIONP (it->font_height))
4116 {
4117 /* Call function with current height as argument.
4118 Value is the new height. */
4119 Lisp_Object height;
4120 height = safe_call1 (it->font_height,
4121 face->lface[LFACE_HEIGHT_INDEX]);
4122 if (NUMBERP (height))
4123 new_height = XFLOATINT (height);
4124 }
4125 else if (NUMBERP (it->font_height))
4126 {
4127 /* Value is a multiple of the canonical char height. */
4128 struct face *face;
4129
4130 face = FACE_FROM_ID (it->f,
4131 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4132 new_height = (XFLOATINT (it->font_height)
4133 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4134 }
4135 else
4136 {
4137 /* Evaluate IT->font_height with `height' bound to the
4138 current specified height to get the new height. */
4139 int count = SPECPDL_INDEX ();
4140
4141 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4142 value = safe_eval (it->font_height);
4143 unbind_to (count, Qnil);
4144
4145 if (NUMBERP (value))
4146 new_height = XFLOATINT (value);
4147 }
4148
4149 if (new_height > 0)
4150 it->face_id = face_with_height (it->f, it->face_id, new_height);
4151 }
4152
4153 return 0;
4154 }
4155
4156 /* Handle `(space-width WIDTH)'. */
4157 if (CONSP (spec)
4158 && EQ (XCAR (spec), Qspace_width)
4159 && CONSP (XCDR (spec)))
4160 {
4161 if (!FRAME_WINDOW_P (it->f))
4162 return 0;
4163
4164 value = XCAR (XCDR (spec));
4165 if (NUMBERP (value) && XFLOATINT (value) > 0)
4166 it->space_width = value;
4167
4168 return 0;
4169 }
4170
4171 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4172 if (CONSP (spec)
4173 && EQ (XCAR (spec), Qslice))
4174 {
4175 Lisp_Object tem;
4176
4177 if (!FRAME_WINDOW_P (it->f))
4178 return 0;
4179
4180 if (tem = XCDR (spec), CONSP (tem))
4181 {
4182 it->slice.x = XCAR (tem);
4183 if (tem = XCDR (tem), CONSP (tem))
4184 {
4185 it->slice.y = XCAR (tem);
4186 if (tem = XCDR (tem), CONSP (tem))
4187 {
4188 it->slice.width = XCAR (tem);
4189 if (tem = XCDR (tem), CONSP (tem))
4190 it->slice.height = XCAR (tem);
4191 }
4192 }
4193 }
4194
4195 return 0;
4196 }
4197
4198 /* Handle `(raise FACTOR)'. */
4199 if (CONSP (spec)
4200 && EQ (XCAR (spec), Qraise)
4201 && CONSP (XCDR (spec)))
4202 {
4203 if (!FRAME_WINDOW_P (it->f))
4204 return 0;
4205
4206 #ifdef HAVE_WINDOW_SYSTEM
4207 value = XCAR (XCDR (spec));
4208 if (NUMBERP (value))
4209 {
4210 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4211 it->voffset = - (XFLOATINT (value)
4212 * (FONT_HEIGHT (face->font)));
4213 }
4214 #endif /* HAVE_WINDOW_SYSTEM */
4215
4216 return 0;
4217 }
4218
4219 /* Don't handle the other kinds of display specifications
4220 inside a string that we got from a `display' property. */
4221 if (it->string_from_display_prop_p)
4222 return 0;
4223
4224 /* Characters having this form of property are not displayed, so
4225 we have to find the end of the property. */
4226 start_pos = *position;
4227 *position = display_prop_end (it, object, start_pos);
4228 value = Qnil;
4229
4230 /* Stop the scan at that end position--we assume that all
4231 text properties change there. */
4232 it->stop_charpos = position->charpos;
4233
4234 /* Handle `(left-fringe BITMAP [FACE])'
4235 and `(right-fringe BITMAP [FACE])'. */
4236 if (CONSP (spec)
4237 && (EQ (XCAR (spec), Qleft_fringe)
4238 || EQ (XCAR (spec), Qright_fringe))
4239 && CONSP (XCDR (spec)))
4240 {
4241 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4242 int fringe_bitmap;
4243
4244 if (!FRAME_WINDOW_P (it->f))
4245 /* If we return here, POSITION has been advanced
4246 across the text with this property. */
4247 return 0;
4248
4249 #ifdef HAVE_WINDOW_SYSTEM
4250 value = XCAR (XCDR (spec));
4251 if (!SYMBOLP (value)
4252 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4253 /* If we return here, POSITION has been advanced
4254 across the text with this property. */
4255 return 0;
4256
4257 if (CONSP (XCDR (XCDR (spec))))
4258 {
4259 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4260 int face_id2 = lookup_derived_face (it->f, face_name,
4261 FRINGE_FACE_ID, 0);
4262 if (face_id2 >= 0)
4263 face_id = face_id2;
4264 }
4265
4266 /* Save current settings of IT so that we can restore them
4267 when we are finished with the glyph property value. */
4268
4269 save_pos = it->position;
4270 it->position = *position;
4271 push_it (it);
4272 it->position = save_pos;
4273
4274 it->area = TEXT_AREA;
4275 it->what = IT_IMAGE;
4276 it->image_id = -1; /* no image */
4277 it->position = start_pos;
4278 it->object = NILP (object) ? it->w->buffer : object;
4279 it->method = GET_FROM_IMAGE;
4280 it->from_overlay = Qnil;
4281 it->face_id = face_id;
4282
4283 /* Say that we haven't consumed the characters with
4284 `display' property yet. The call to pop_it in
4285 set_iterator_to_next will clean this up. */
4286 *position = start_pos;
4287
4288 if (EQ (XCAR (spec), Qleft_fringe))
4289 {
4290 it->left_user_fringe_bitmap = fringe_bitmap;
4291 it->left_user_fringe_face_id = face_id;
4292 }
4293 else
4294 {
4295 it->right_user_fringe_bitmap = fringe_bitmap;
4296 it->right_user_fringe_face_id = face_id;
4297 }
4298 #endif /* HAVE_WINDOW_SYSTEM */
4299 return 1;
4300 }
4301
4302 /* Prepare to handle `((margin left-margin) ...)',
4303 `((margin right-margin) ...)' and `((margin nil) ...)'
4304 prefixes for display specifications. */
4305 location = Qunbound;
4306 if (CONSP (spec) && CONSP (XCAR (spec)))
4307 {
4308 Lisp_Object tem;
4309
4310 value = XCDR (spec);
4311 if (CONSP (value))
4312 value = XCAR (value);
4313
4314 tem = XCAR (spec);
4315 if (EQ (XCAR (tem), Qmargin)
4316 && (tem = XCDR (tem),
4317 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4318 (NILP (tem)
4319 || EQ (tem, Qleft_margin)
4320 || EQ (tem, Qright_margin))))
4321 location = tem;
4322 }
4323
4324 if (EQ (location, Qunbound))
4325 {
4326 location = Qnil;
4327 value = spec;
4328 }
4329
4330 /* After this point, VALUE is the property after any
4331 margin prefix has been stripped. It must be a string,
4332 an image specification, or `(space ...)'.
4333
4334 LOCATION specifies where to display: `left-margin',
4335 `right-margin' or nil. */
4336
4337 valid_p = (STRINGP (value)
4338 #ifdef HAVE_WINDOW_SYSTEM
4339 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4340 #endif /* not HAVE_WINDOW_SYSTEM */
4341 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4342
4343 if (valid_p && !display_replaced_before_p)
4344 {
4345 /* Save current settings of IT so that we can restore them
4346 when we are finished with the glyph property value. */
4347 save_pos = it->position;
4348 it->position = *position;
4349 push_it (it);
4350 it->position = save_pos;
4351 it->from_overlay = overlay;
4352
4353 if (NILP (location))
4354 it->area = TEXT_AREA;
4355 else if (EQ (location, Qleft_margin))
4356 it->area = LEFT_MARGIN_AREA;
4357 else
4358 it->area = RIGHT_MARGIN_AREA;
4359
4360 if (STRINGP (value))
4361 {
4362 it->string = value;
4363 it->multibyte_p = STRING_MULTIBYTE (it->string);
4364 it->current.overlay_string_index = -1;
4365 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4366 it->end_charpos = it->string_nchars = SCHARS (it->string);
4367 it->method = GET_FROM_STRING;
4368 it->stop_charpos = 0;
4369 it->string_from_display_prop_p = 1;
4370 /* Say that we haven't consumed the characters with
4371 `display' property yet. The call to pop_it in
4372 set_iterator_to_next will clean this up. */
4373 if (BUFFERP (object))
4374 *position = start_pos;
4375 }
4376 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4377 {
4378 it->method = GET_FROM_STRETCH;
4379 it->object = value;
4380 *position = it->position = start_pos;
4381 }
4382 #ifdef HAVE_WINDOW_SYSTEM
4383 else
4384 {
4385 it->what = IT_IMAGE;
4386 it->image_id = lookup_image (it->f, value);
4387 it->position = start_pos;
4388 it->object = NILP (object) ? it->w->buffer : object;
4389 it->method = GET_FROM_IMAGE;
4390
4391 /* Say that we haven't consumed the characters with
4392 `display' property yet. The call to pop_it in
4393 set_iterator_to_next will clean this up. */
4394 *position = start_pos;
4395 }
4396 #endif /* HAVE_WINDOW_SYSTEM */
4397
4398 return 1;
4399 }
4400
4401 /* Invalid property or property not supported. Restore
4402 POSITION to what it was before. */
4403 *position = start_pos;
4404 return 0;
4405 }
4406
4407
4408 /* Check if SPEC is a display sub-property value whose text should be
4409 treated as intangible. */
4410
4411 static int
4412 single_display_spec_intangible_p (prop)
4413 Lisp_Object prop;
4414 {
4415 /* Skip over `when FORM'. */
4416 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4417 {
4418 prop = XCDR (prop);
4419 if (!CONSP (prop))
4420 return 0;
4421 prop = XCDR (prop);
4422 }
4423
4424 if (STRINGP (prop))
4425 return 1;
4426
4427 if (!CONSP (prop))
4428 return 0;
4429
4430 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4431 we don't need to treat text as intangible. */
4432 if (EQ (XCAR (prop), Qmargin))
4433 {
4434 prop = XCDR (prop);
4435 if (!CONSP (prop))
4436 return 0;
4437
4438 prop = XCDR (prop);
4439 if (!CONSP (prop)
4440 || EQ (XCAR (prop), Qleft_margin)
4441 || EQ (XCAR (prop), Qright_margin))
4442 return 0;
4443 }
4444
4445 return (CONSP (prop)
4446 && (EQ (XCAR (prop), Qimage)
4447 || EQ (XCAR (prop), Qspace)));
4448 }
4449
4450
4451 /* Check if PROP is a display property value whose text should be
4452 treated as intangible. */
4453
4454 int
4455 display_prop_intangible_p (prop)
4456 Lisp_Object prop;
4457 {
4458 if (CONSP (prop)
4459 && CONSP (XCAR (prop))
4460 && !EQ (Qmargin, XCAR (XCAR (prop))))
4461 {
4462 /* A list of sub-properties. */
4463 while (CONSP (prop))
4464 {
4465 if (single_display_spec_intangible_p (XCAR (prop)))
4466 return 1;
4467 prop = XCDR (prop);
4468 }
4469 }
4470 else if (VECTORP (prop))
4471 {
4472 /* A vector of sub-properties. */
4473 int i;
4474 for (i = 0; i < ASIZE (prop); ++i)
4475 if (single_display_spec_intangible_p (AREF (prop, i)))
4476 return 1;
4477 }
4478 else
4479 return single_display_spec_intangible_p (prop);
4480
4481 return 0;
4482 }
4483
4484
4485 /* Return 1 if PROP is a display sub-property value containing STRING. */
4486
4487 static int
4488 single_display_spec_string_p (prop, string)
4489 Lisp_Object prop, string;
4490 {
4491 if (EQ (string, prop))
4492 return 1;
4493
4494 /* Skip over `when FORM'. */
4495 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4496 {
4497 prop = XCDR (prop);
4498 if (!CONSP (prop))
4499 return 0;
4500 prop = XCDR (prop);
4501 }
4502
4503 if (CONSP (prop))
4504 /* Skip over `margin LOCATION'. */
4505 if (EQ (XCAR (prop), Qmargin))
4506 {
4507 prop = XCDR (prop);
4508 if (!CONSP (prop))
4509 return 0;
4510
4511 prop = XCDR (prop);
4512 if (!CONSP (prop))
4513 return 0;
4514 }
4515
4516 return CONSP (prop) && EQ (XCAR (prop), string);
4517 }
4518
4519
4520 /* Return 1 if STRING appears in the `display' property PROP. */
4521
4522 static int
4523 display_prop_string_p (prop, string)
4524 Lisp_Object prop, string;
4525 {
4526 if (CONSP (prop)
4527 && CONSP (XCAR (prop))
4528 && !EQ (Qmargin, XCAR (XCAR (prop))))
4529 {
4530 /* A list of sub-properties. */
4531 while (CONSP (prop))
4532 {
4533 if (single_display_spec_string_p (XCAR (prop), string))
4534 return 1;
4535 prop = XCDR (prop);
4536 }
4537 }
4538 else if (VECTORP (prop))
4539 {
4540 /* A vector of sub-properties. */
4541 int i;
4542 for (i = 0; i < ASIZE (prop); ++i)
4543 if (single_display_spec_string_p (AREF (prop, i), string))
4544 return 1;
4545 }
4546 else
4547 return single_display_spec_string_p (prop, string);
4548
4549 return 0;
4550 }
4551
4552
4553 /* Determine which buffer position in W's buffer STRING comes from.
4554 AROUND_CHARPOS is an approximate position where it could come from.
4555 Value is the buffer position or 0 if it couldn't be determined.
4556
4557 W's buffer must be current.
4558
4559 This function is necessary because we don't record buffer positions
4560 in glyphs generated from strings (to keep struct glyph small).
4561 This function may only use code that doesn't eval because it is
4562 called asynchronously from note_mouse_highlight. */
4563
4564 int
4565 string_buffer_position (w, string, around_charpos)
4566 struct window *w;
4567 Lisp_Object string;
4568 int around_charpos;
4569 {
4570 Lisp_Object limit, prop, pos;
4571 const int MAX_DISTANCE = 1000;
4572 int found = 0;
4573
4574 pos = make_number (around_charpos);
4575 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4576 while (!found && !EQ (pos, limit))
4577 {
4578 prop = Fget_char_property (pos, Qdisplay, Qnil);
4579 if (!NILP (prop) && display_prop_string_p (prop, string))
4580 found = 1;
4581 else
4582 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4583 }
4584
4585 if (!found)
4586 {
4587 pos = make_number (around_charpos);
4588 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4589 while (!found && !EQ (pos, limit))
4590 {
4591 prop = Fget_char_property (pos, Qdisplay, Qnil);
4592 if (!NILP (prop) && display_prop_string_p (prop, string))
4593 found = 1;
4594 else
4595 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4596 limit);
4597 }
4598 }
4599
4600 return found ? XINT (pos) : 0;
4601 }
4602
4603
4604 \f
4605 /***********************************************************************
4606 `composition' property
4607 ***********************************************************************/
4608
4609 /* Set up iterator IT from `composition' property at its current
4610 position. Called from handle_stop. */
4611
4612 static enum prop_handled
4613 handle_composition_prop (it)
4614 struct it *it;
4615 {
4616 Lisp_Object prop, string;
4617 EMACS_INT pos, pos_byte, start, end;
4618
4619 if (STRINGP (it->string))
4620 {
4621 unsigned char *s;
4622
4623 pos = IT_STRING_CHARPOS (*it);
4624 pos_byte = IT_STRING_BYTEPOS (*it);
4625 string = it->string;
4626 s = SDATA (string) + pos_byte;
4627 it->c = STRING_CHAR (s);
4628 }
4629 else
4630 {
4631 pos = IT_CHARPOS (*it);
4632 pos_byte = IT_BYTEPOS (*it);
4633 string = Qnil;
4634 it->c = FETCH_CHAR (pos_byte);
4635 }
4636
4637 /* If there's a valid composition and point is not inside of the
4638 composition (in the case that the composition is from the current
4639 buffer), draw a glyph composed from the composition components. */
4640 if (find_composition (pos, -1, &start, &end, &prop, string)
4641 && COMPOSITION_VALID_P (start, end, prop)
4642 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4643 {
4644 if (start < pos)
4645 /* As we can't handle this situation (perhaps, font-lock added
4646 a new composition), we just return here hoping that next
4647 redisplay will detect this composition much earlier. */
4648 return HANDLED_NORMALLY;
4649 if (start != pos)
4650 {
4651 if (STRINGP (it->string))
4652 pos_byte = string_char_to_byte (it->string, start);
4653 else
4654 pos_byte = CHAR_TO_BYTE (start);
4655 }
4656 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4657 prop, string);
4658
4659 if (it->cmp_it.id >= 0)
4660 {
4661 it->cmp_it.ch = -1;
4662 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4663 it->cmp_it.nglyphs = -1;
4664 }
4665 }
4666
4667 return HANDLED_NORMALLY;
4668 }
4669
4670
4671 \f
4672 /***********************************************************************
4673 Overlay strings
4674 ***********************************************************************/
4675
4676 /* The following structure is used to record overlay strings for
4677 later sorting in load_overlay_strings. */
4678
4679 struct overlay_entry
4680 {
4681 Lisp_Object overlay;
4682 Lisp_Object string;
4683 int priority;
4684 int after_string_p;
4685 };
4686
4687
4688 /* Set up iterator IT from overlay strings at its current position.
4689 Called from handle_stop. */
4690
4691 static enum prop_handled
4692 handle_overlay_change (it)
4693 struct it *it;
4694 {
4695 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4696 return HANDLED_RECOMPUTE_PROPS;
4697 else
4698 return HANDLED_NORMALLY;
4699 }
4700
4701
4702 /* Set up the next overlay string for delivery by IT, if there is an
4703 overlay string to deliver. Called by set_iterator_to_next when the
4704 end of the current overlay string is reached. If there are more
4705 overlay strings to display, IT->string and
4706 IT->current.overlay_string_index are set appropriately here.
4707 Otherwise IT->string is set to nil. */
4708
4709 static void
4710 next_overlay_string (it)
4711 struct it *it;
4712 {
4713 ++it->current.overlay_string_index;
4714 if (it->current.overlay_string_index == it->n_overlay_strings)
4715 {
4716 /* No more overlay strings. Restore IT's settings to what
4717 they were before overlay strings were processed, and
4718 continue to deliver from current_buffer. */
4719
4720 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4721 pop_it (it);
4722 xassert (it->sp > 0
4723 || (NILP (it->string)
4724 && it->method == GET_FROM_BUFFER
4725 && it->stop_charpos >= BEGV
4726 && it->stop_charpos <= it->end_charpos));
4727 it->current.overlay_string_index = -1;
4728 it->n_overlay_strings = 0;
4729 it->overlay_strings_charpos = -1;
4730
4731 /* If we're at the end of the buffer, record that we have
4732 processed the overlay strings there already, so that
4733 next_element_from_buffer doesn't try it again. */
4734 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4735 it->overlay_strings_at_end_processed_p = 1;
4736 }
4737 else
4738 {
4739 /* There are more overlay strings to process. If
4740 IT->current.overlay_string_index has advanced to a position
4741 where we must load IT->overlay_strings with more strings, do
4742 it. We must load at the IT->overlay_strings_charpos where
4743 IT->n_overlay_strings was originally computed; when invisible
4744 text is present, this might not be IT_CHARPOS (Bug#7016). */
4745 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4746
4747 if (it->current.overlay_string_index && i == 0)
4748 load_overlay_strings (it, it->overlay_strings_charpos);
4749
4750 /* Initialize IT to deliver display elements from the overlay
4751 string. */
4752 it->string = it->overlay_strings[i];
4753 it->multibyte_p = STRING_MULTIBYTE (it->string);
4754 SET_TEXT_POS (it->current.string_pos, 0, 0);
4755 it->method = GET_FROM_STRING;
4756 it->stop_charpos = 0;
4757 if (it->cmp_it.stop_pos >= 0)
4758 it->cmp_it.stop_pos = 0;
4759 }
4760
4761 CHECK_IT (it);
4762 }
4763
4764
4765 /* Compare two overlay_entry structures E1 and E2. Used as a
4766 comparison function for qsort in load_overlay_strings. Overlay
4767 strings for the same position are sorted so that
4768
4769 1. All after-strings come in front of before-strings, except
4770 when they come from the same overlay.
4771
4772 2. Within after-strings, strings are sorted so that overlay strings
4773 from overlays with higher priorities come first.
4774
4775 2. Within before-strings, strings are sorted so that overlay
4776 strings from overlays with higher priorities come last.
4777
4778 Value is analogous to strcmp. */
4779
4780
4781 static int
4782 compare_overlay_entries (e1, e2)
4783 void *e1, *e2;
4784 {
4785 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4786 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4787 int result;
4788
4789 if (entry1->after_string_p != entry2->after_string_p)
4790 {
4791 /* Let after-strings appear in front of before-strings if
4792 they come from different overlays. */
4793 if (EQ (entry1->overlay, entry2->overlay))
4794 result = entry1->after_string_p ? 1 : -1;
4795 else
4796 result = entry1->after_string_p ? -1 : 1;
4797 }
4798 else if (entry1->after_string_p)
4799 /* After-strings sorted in order of decreasing priority. */
4800 result = entry2->priority - entry1->priority;
4801 else
4802 /* Before-strings sorted in order of increasing priority. */
4803 result = entry1->priority - entry2->priority;
4804
4805 return result;
4806 }
4807
4808
4809 /* Load the vector IT->overlay_strings with overlay strings from IT's
4810 current buffer position, or from CHARPOS if that is > 0. Set
4811 IT->n_overlays to the total number of overlay strings found.
4812
4813 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4814 a time. On entry into load_overlay_strings,
4815 IT->current.overlay_string_index gives the number of overlay
4816 strings that have already been loaded by previous calls to this
4817 function.
4818
4819 IT->add_overlay_start contains an additional overlay start
4820 position to consider for taking overlay strings from, if non-zero.
4821 This position comes into play when the overlay has an `invisible'
4822 property, and both before and after-strings. When we've skipped to
4823 the end of the overlay, because of its `invisible' property, we
4824 nevertheless want its before-string to appear.
4825 IT->add_overlay_start will contain the overlay start position
4826 in this case.
4827
4828 Overlay strings are sorted so that after-string strings come in
4829 front of before-string strings. Within before and after-strings,
4830 strings are sorted by overlay priority. See also function
4831 compare_overlay_entries. */
4832
4833 static void
4834 load_overlay_strings (it, charpos)
4835 struct it *it;
4836 int charpos;
4837 {
4838 extern Lisp_Object Qwindow, Qpriority;
4839 Lisp_Object overlay, window, str, invisible;
4840 struct Lisp_Overlay *ov;
4841 int start, end;
4842 int size = 20;
4843 int n = 0, i, j, invis_p;
4844 struct overlay_entry *entries
4845 = (struct overlay_entry *) alloca (size * sizeof *entries);
4846
4847 if (charpos <= 0)
4848 charpos = IT_CHARPOS (*it);
4849
4850 /* Append the overlay string STRING of overlay OVERLAY to vector
4851 `entries' which has size `size' and currently contains `n'
4852 elements. AFTER_P non-zero means STRING is an after-string of
4853 OVERLAY. */
4854 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4855 do \
4856 { \
4857 Lisp_Object priority; \
4858 \
4859 if (n == size) \
4860 { \
4861 int new_size = 2 * size; \
4862 struct overlay_entry *old = entries; \
4863 entries = \
4864 (struct overlay_entry *) alloca (new_size \
4865 * sizeof *entries); \
4866 bcopy (old, entries, size * sizeof *entries); \
4867 size = new_size; \
4868 } \
4869 \
4870 entries[n].string = (STRING); \
4871 entries[n].overlay = (OVERLAY); \
4872 priority = Foverlay_get ((OVERLAY), Qpriority); \
4873 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4874 entries[n].after_string_p = (AFTER_P); \
4875 ++n; \
4876 } \
4877 while (0)
4878
4879 /* Process overlay before the overlay center. */
4880 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4881 {
4882 XSETMISC (overlay, ov);
4883 xassert (OVERLAYP (overlay));
4884 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4885 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4886
4887 if (end < charpos)
4888 break;
4889
4890 /* Skip this overlay if it doesn't start or end at IT's current
4891 position. */
4892 if (end != charpos && start != charpos)
4893 continue;
4894
4895 /* Skip this overlay if it doesn't apply to IT->w. */
4896 window = Foverlay_get (overlay, Qwindow);
4897 if (WINDOWP (window) && XWINDOW (window) != it->w)
4898 continue;
4899
4900 /* If the text ``under'' the overlay is invisible, both before-
4901 and after-strings from this overlay are visible; start and
4902 end position are indistinguishable. */
4903 invisible = Foverlay_get (overlay, Qinvisible);
4904 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4905
4906 /* If overlay has a non-empty before-string, record it. */
4907 if ((start == charpos || (end == charpos && invis_p))
4908 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4909 && SCHARS (str))
4910 RECORD_OVERLAY_STRING (overlay, str, 0);
4911
4912 /* If overlay has a non-empty after-string, record it. */
4913 if ((end == charpos || (start == charpos && invis_p))
4914 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4915 && SCHARS (str))
4916 RECORD_OVERLAY_STRING (overlay, str, 1);
4917 }
4918
4919 /* Process overlays after the overlay center. */
4920 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4921 {
4922 XSETMISC (overlay, ov);
4923 xassert (OVERLAYP (overlay));
4924 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4925 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4926
4927 if (start > charpos)
4928 break;
4929
4930 /* Skip this overlay if it doesn't start or end at IT's current
4931 position. */
4932 if (end != charpos && start != charpos)
4933 continue;
4934
4935 /* Skip this overlay if it doesn't apply to IT->w. */
4936 window = Foverlay_get (overlay, Qwindow);
4937 if (WINDOWP (window) && XWINDOW (window) != it->w)
4938 continue;
4939
4940 /* If the text ``under'' the overlay is invisible, it has a zero
4941 dimension, and both before- and after-strings apply. */
4942 invisible = Foverlay_get (overlay, Qinvisible);
4943 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4944
4945 /* If overlay has a non-empty before-string, record it. */
4946 if ((start == charpos || (end == charpos && invis_p))
4947 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4948 && SCHARS (str))
4949 RECORD_OVERLAY_STRING (overlay, str, 0);
4950
4951 /* If overlay has a non-empty after-string, record it. */
4952 if ((end == charpos || (start == charpos && invis_p))
4953 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4954 && SCHARS (str))
4955 RECORD_OVERLAY_STRING (overlay, str, 1);
4956 }
4957
4958 #undef RECORD_OVERLAY_STRING
4959
4960 /* Sort entries. */
4961 if (n > 1)
4962 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4963
4964 /* Record number of overlay strings, and where we computed it. */
4965 it->n_overlay_strings = n;
4966 it->overlay_strings_charpos = charpos;
4967
4968 /* IT->current.overlay_string_index is the number of overlay strings
4969 that have already been consumed by IT. Copy some of the
4970 remaining overlay strings to IT->overlay_strings. */
4971 i = 0;
4972 j = it->current.overlay_string_index;
4973 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4974 {
4975 it->overlay_strings[i] = entries[j].string;
4976 it->string_overlays[i++] = entries[j++].overlay;
4977 }
4978
4979 CHECK_IT (it);
4980 }
4981
4982
4983 /* Get the first chunk of overlay strings at IT's current buffer
4984 position, or at CHARPOS if that is > 0. Value is non-zero if at
4985 least one overlay string was found. */
4986
4987 static int
4988 get_overlay_strings_1 (it, charpos, compute_stop_p)
4989 struct it *it;
4990 int charpos;
4991 int compute_stop_p;
4992 {
4993 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4994 process. This fills IT->overlay_strings with strings, and sets
4995 IT->n_overlay_strings to the total number of strings to process.
4996 IT->pos.overlay_string_index has to be set temporarily to zero
4997 because load_overlay_strings needs this; it must be set to -1
4998 when no overlay strings are found because a zero value would
4999 indicate a position in the first overlay string. */
5000 it->current.overlay_string_index = 0;
5001 load_overlay_strings (it, charpos);
5002
5003 /* If we found overlay strings, set up IT to deliver display
5004 elements from the first one. Otherwise set up IT to deliver
5005 from current_buffer. */
5006 if (it->n_overlay_strings)
5007 {
5008 /* Make sure we know settings in current_buffer, so that we can
5009 restore meaningful values when we're done with the overlay
5010 strings. */
5011 if (compute_stop_p)
5012 compute_stop_pos (it);
5013 xassert (it->face_id >= 0);
5014
5015 /* Save IT's settings. They are restored after all overlay
5016 strings have been processed. */
5017 xassert (!compute_stop_p || it->sp == 0);
5018
5019 /* When called from handle_stop, there might be an empty display
5020 string loaded. In that case, don't bother saving it. */
5021 if (!STRINGP (it->string) || SCHARS (it->string))
5022 push_it (it);
5023
5024 /* Set up IT to deliver display elements from the first overlay
5025 string. */
5026 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5027 it->string = it->overlay_strings[0];
5028 it->from_overlay = Qnil;
5029 it->stop_charpos = 0;
5030 xassert (STRINGP (it->string));
5031 it->end_charpos = SCHARS (it->string);
5032 it->multibyte_p = STRING_MULTIBYTE (it->string);
5033 it->method = GET_FROM_STRING;
5034 return 1;
5035 }
5036
5037 it->current.overlay_string_index = -1;
5038 return 0;
5039 }
5040
5041 static int
5042 get_overlay_strings (it, charpos)
5043 struct it *it;
5044 int charpos;
5045 {
5046 it->string = Qnil;
5047 it->method = GET_FROM_BUFFER;
5048
5049 (void) get_overlay_strings_1 (it, charpos, 1);
5050
5051 CHECK_IT (it);
5052
5053 /* Value is non-zero if we found at least one overlay string. */
5054 return STRINGP (it->string);
5055 }
5056
5057
5058 \f
5059 /***********************************************************************
5060 Saving and restoring state
5061 ***********************************************************************/
5062
5063 /* Save current settings of IT on IT->stack. Called, for example,
5064 before setting up IT for an overlay string, to be able to restore
5065 IT's settings to what they were after the overlay string has been
5066 processed. */
5067
5068 static void
5069 push_it (it)
5070 struct it *it;
5071 {
5072 struct iterator_stack_entry *p;
5073
5074 xassert (it->sp < IT_STACK_SIZE);
5075 p = it->stack + it->sp;
5076
5077 p->stop_charpos = it->stop_charpos;
5078 p->cmp_it = it->cmp_it;
5079 xassert (it->face_id >= 0);
5080 p->face_id = it->face_id;
5081 p->string = it->string;
5082 p->method = it->method;
5083 p->from_overlay = it->from_overlay;
5084 switch (p->method)
5085 {
5086 case GET_FROM_IMAGE:
5087 p->u.image.object = it->object;
5088 p->u.image.image_id = it->image_id;
5089 p->u.image.slice = it->slice;
5090 break;
5091 case GET_FROM_STRETCH:
5092 p->u.stretch.object = it->object;
5093 break;
5094 }
5095 p->position = it->position;
5096 p->current = it->current;
5097 p->end_charpos = it->end_charpos;
5098 p->string_nchars = it->string_nchars;
5099 p->area = it->area;
5100 p->multibyte_p = it->multibyte_p;
5101 p->avoid_cursor_p = it->avoid_cursor_p;
5102 p->space_width = it->space_width;
5103 p->font_height = it->font_height;
5104 p->voffset = it->voffset;
5105 p->string_from_display_prop_p = it->string_from_display_prop_p;
5106 p->display_ellipsis_p = 0;
5107 p->line_wrap = it->line_wrap;
5108 ++it->sp;
5109 }
5110
5111
5112 /* Restore IT's settings from IT->stack. Called, for example, when no
5113 more overlay strings must be processed, and we return to delivering
5114 display elements from a buffer, or when the end of a string from a
5115 `display' property is reached and we return to delivering display
5116 elements from an overlay string, or from a buffer. */
5117
5118 static void
5119 pop_it (it)
5120 struct it *it;
5121 {
5122 struct iterator_stack_entry *p;
5123
5124 xassert (it->sp > 0);
5125 --it->sp;
5126 p = it->stack + it->sp;
5127 it->stop_charpos = p->stop_charpos;
5128 it->cmp_it = p->cmp_it;
5129 it->face_id = p->face_id;
5130 it->current = p->current;
5131 it->position = p->position;
5132 it->string = p->string;
5133 it->from_overlay = p->from_overlay;
5134 if (NILP (it->string))
5135 SET_TEXT_POS (it->current.string_pos, -1, -1);
5136 it->method = p->method;
5137 switch (it->method)
5138 {
5139 case GET_FROM_IMAGE:
5140 it->image_id = p->u.image.image_id;
5141 it->object = p->u.image.object;
5142 it->slice = p->u.image.slice;
5143 break;
5144 case GET_FROM_STRETCH:
5145 it->object = p->u.comp.object;
5146 break;
5147 case GET_FROM_BUFFER:
5148 it->object = it->w->buffer;
5149 break;
5150 case GET_FROM_STRING:
5151 it->object = it->string;
5152 break;
5153 case GET_FROM_DISPLAY_VECTOR:
5154 if (it->s)
5155 it->method = GET_FROM_C_STRING;
5156 else if (STRINGP (it->string))
5157 it->method = GET_FROM_STRING;
5158 else
5159 {
5160 it->method = GET_FROM_BUFFER;
5161 it->object = it->w->buffer;
5162 }
5163 }
5164 it->end_charpos = p->end_charpos;
5165 it->string_nchars = p->string_nchars;
5166 it->area = p->area;
5167 it->multibyte_p = p->multibyte_p;
5168 it->avoid_cursor_p = p->avoid_cursor_p;
5169 it->space_width = p->space_width;
5170 it->font_height = p->font_height;
5171 it->voffset = p->voffset;
5172 it->string_from_display_prop_p = p->string_from_display_prop_p;
5173 it->line_wrap = p->line_wrap;
5174 }
5175
5176
5177 \f
5178 /***********************************************************************
5179 Moving over lines
5180 ***********************************************************************/
5181
5182 /* Set IT's current position to the previous line start. */
5183
5184 static void
5185 back_to_previous_line_start (it)
5186 struct it *it;
5187 {
5188 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5189 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5190 }
5191
5192
5193 /* Move IT to the next line start.
5194
5195 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5196 we skipped over part of the text (as opposed to moving the iterator
5197 continuously over the text). Otherwise, don't change the value
5198 of *SKIPPED_P.
5199
5200 Newlines may come from buffer text, overlay strings, or strings
5201 displayed via the `display' property. That's the reason we can't
5202 simply use find_next_newline_no_quit.
5203
5204 Note that this function may not skip over invisible text that is so
5205 because of text properties and immediately follows a newline. If
5206 it would, function reseat_at_next_visible_line_start, when called
5207 from set_iterator_to_next, would effectively make invisible
5208 characters following a newline part of the wrong glyph row, which
5209 leads to wrong cursor motion. */
5210
5211 static int
5212 forward_to_next_line_start (it, skipped_p)
5213 struct it *it;
5214 int *skipped_p;
5215 {
5216 int old_selective, newline_found_p, n;
5217 const int MAX_NEWLINE_DISTANCE = 500;
5218
5219 /* If already on a newline, just consume it to avoid unintended
5220 skipping over invisible text below. */
5221 if (it->what == IT_CHARACTER
5222 && it->c == '\n'
5223 && CHARPOS (it->position) == IT_CHARPOS (*it))
5224 {
5225 set_iterator_to_next (it, 0);
5226 it->c = 0;
5227 return 1;
5228 }
5229
5230 /* Don't handle selective display in the following. It's (a)
5231 unnecessary because it's done by the caller, and (b) leads to an
5232 infinite recursion because next_element_from_ellipsis indirectly
5233 calls this function. */
5234 old_selective = it->selective;
5235 it->selective = 0;
5236
5237 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5238 from buffer text. */
5239 for (n = newline_found_p = 0;
5240 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5241 n += STRINGP (it->string) ? 0 : 1)
5242 {
5243 if (!get_next_display_element (it))
5244 return 0;
5245 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5246 set_iterator_to_next (it, 0);
5247 }
5248
5249 /* If we didn't find a newline near enough, see if we can use a
5250 short-cut. */
5251 if (!newline_found_p)
5252 {
5253 int start = IT_CHARPOS (*it);
5254 int limit = find_next_newline_no_quit (start, 1);
5255 Lisp_Object pos;
5256
5257 xassert (!STRINGP (it->string));
5258
5259 /* If there isn't any `display' property in sight, and no
5260 overlays, we can just use the position of the newline in
5261 buffer text. */
5262 if (it->stop_charpos >= limit
5263 || ((pos = Fnext_single_property_change (make_number (start),
5264 Qdisplay,
5265 Qnil, make_number (limit)),
5266 NILP (pos))
5267 && next_overlay_change (start) == ZV))
5268 {
5269 IT_CHARPOS (*it) = limit;
5270 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5271 *skipped_p = newline_found_p = 1;
5272 }
5273 else
5274 {
5275 while (get_next_display_element (it)
5276 && !newline_found_p)
5277 {
5278 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5279 set_iterator_to_next (it, 0);
5280 }
5281 }
5282 }
5283
5284 it->selective = old_selective;
5285 return newline_found_p;
5286 }
5287
5288
5289 /* Set IT's current position to the previous visible line start. Skip
5290 invisible text that is so either due to text properties or due to
5291 selective display. Caution: this does not change IT->current_x and
5292 IT->hpos. */
5293
5294 static void
5295 back_to_previous_visible_line_start (it)
5296 struct it *it;
5297 {
5298 while (IT_CHARPOS (*it) > BEGV)
5299 {
5300 back_to_previous_line_start (it);
5301
5302 if (IT_CHARPOS (*it) <= BEGV)
5303 break;
5304
5305 /* If selective > 0, then lines indented more than that values
5306 are invisible. */
5307 if (it->selective > 0
5308 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5309 (double) it->selective)) /* iftc */
5310 continue;
5311
5312 /* Check the newline before point for invisibility. */
5313 {
5314 Lisp_Object prop;
5315 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5316 Qinvisible, it->window);
5317 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5318 continue;
5319 }
5320
5321 if (IT_CHARPOS (*it) <= BEGV)
5322 break;
5323
5324 {
5325 struct it it2;
5326 int pos;
5327 EMACS_INT beg, end;
5328 Lisp_Object val, overlay;
5329
5330 /* If newline is part of a composition, continue from start of composition */
5331 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5332 && beg < IT_CHARPOS (*it))
5333 goto replaced;
5334
5335 /* If newline is replaced by a display property, find start of overlay
5336 or interval and continue search from that point. */
5337 it2 = *it;
5338 pos = --IT_CHARPOS (it2);
5339 --IT_BYTEPOS (it2);
5340 it2.sp = 0;
5341 it2.string_from_display_prop_p = 0;
5342 if (handle_display_prop (&it2) == HANDLED_RETURN
5343 && !NILP (val = get_char_property_and_overlay
5344 (make_number (pos), Qdisplay, Qnil, &overlay))
5345 && (OVERLAYP (overlay)
5346 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5347 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5348 goto replaced;
5349
5350 /* Newline is not replaced by anything -- so we are done. */
5351 break;
5352
5353 replaced:
5354 if (beg < BEGV)
5355 beg = BEGV;
5356 IT_CHARPOS (*it) = beg;
5357 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5358 }
5359 }
5360
5361 it->continuation_lines_width = 0;
5362
5363 xassert (IT_CHARPOS (*it) >= BEGV);
5364 xassert (IT_CHARPOS (*it) == BEGV
5365 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5366 CHECK_IT (it);
5367 }
5368
5369
5370 /* Reseat iterator IT at the previous visible line start. Skip
5371 invisible text that is so either due to text properties or due to
5372 selective display. At the end, update IT's overlay information,
5373 face information etc. */
5374
5375 void
5376 reseat_at_previous_visible_line_start (it)
5377 struct it *it;
5378 {
5379 back_to_previous_visible_line_start (it);
5380 reseat (it, it->current.pos, 1);
5381 CHECK_IT (it);
5382 }
5383
5384
5385 /* Reseat iterator IT on the next visible line start in the current
5386 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5387 preceding the line start. Skip over invisible text that is so
5388 because of selective display. Compute faces, overlays etc at the
5389 new position. Note that this function does not skip over text that
5390 is invisible because of text properties. */
5391
5392 static void
5393 reseat_at_next_visible_line_start (it, on_newline_p)
5394 struct it *it;
5395 int on_newline_p;
5396 {
5397 int newline_found_p, skipped_p = 0;
5398
5399 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5400
5401 /* Skip over lines that are invisible because they are indented
5402 more than the value of IT->selective. */
5403 if (it->selective > 0)
5404 while (IT_CHARPOS (*it) < ZV
5405 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5406 (double) it->selective)) /* iftc */
5407 {
5408 xassert (IT_BYTEPOS (*it) == BEGV
5409 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5410 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5411 }
5412
5413 /* Position on the newline if that's what's requested. */
5414 if (on_newline_p && newline_found_p)
5415 {
5416 if (STRINGP (it->string))
5417 {
5418 if (IT_STRING_CHARPOS (*it) > 0)
5419 {
5420 --IT_STRING_CHARPOS (*it);
5421 --IT_STRING_BYTEPOS (*it);
5422 }
5423 }
5424 else if (IT_CHARPOS (*it) > BEGV)
5425 {
5426 --IT_CHARPOS (*it);
5427 --IT_BYTEPOS (*it);
5428 reseat (it, it->current.pos, 0);
5429 }
5430 }
5431 else if (skipped_p)
5432 reseat (it, it->current.pos, 0);
5433
5434 CHECK_IT (it);
5435 }
5436
5437
5438 \f
5439 /***********************************************************************
5440 Changing an iterator's position
5441 ***********************************************************************/
5442
5443 /* Change IT's current position to POS in current_buffer. If FORCE_P
5444 is non-zero, always check for text properties at the new position.
5445 Otherwise, text properties are only looked up if POS >=
5446 IT->check_charpos of a property. */
5447
5448 static void
5449 reseat (it, pos, force_p)
5450 struct it *it;
5451 struct text_pos pos;
5452 int force_p;
5453 {
5454 int original_pos = IT_CHARPOS (*it);
5455
5456 reseat_1 (it, pos, 0);
5457
5458 /* Determine where to check text properties. Avoid doing it
5459 where possible because text property lookup is very expensive. */
5460 if (force_p
5461 || CHARPOS (pos) > it->stop_charpos
5462 || CHARPOS (pos) < original_pos)
5463 handle_stop (it);
5464
5465 CHECK_IT (it);
5466 }
5467
5468
5469 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5470 IT->stop_pos to POS, also. */
5471
5472 static void
5473 reseat_1 (it, pos, set_stop_p)
5474 struct it *it;
5475 struct text_pos pos;
5476 int set_stop_p;
5477 {
5478 /* Don't call this function when scanning a C string. */
5479 xassert (it->s == NULL);
5480
5481 /* POS must be a reasonable value. */
5482 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5483
5484 it->current.pos = it->position = pos;
5485 it->end_charpos = ZV;
5486 it->dpvec = NULL;
5487 it->current.dpvec_index = -1;
5488 it->current.overlay_string_index = -1;
5489 IT_STRING_CHARPOS (*it) = -1;
5490 IT_STRING_BYTEPOS (*it) = -1;
5491 it->string = Qnil;
5492 it->string_from_display_prop_p = 0;
5493 it->method = GET_FROM_BUFFER;
5494 it->object = it->w->buffer;
5495 it->area = TEXT_AREA;
5496 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5497 it->sp = 0;
5498 it->string_from_display_prop_p = 0;
5499 it->face_before_selective_p = 0;
5500
5501 if (set_stop_p)
5502 it->stop_charpos = CHARPOS (pos);
5503 }
5504
5505
5506 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5507 If S is non-null, it is a C string to iterate over. Otherwise,
5508 STRING gives a Lisp string to iterate over.
5509
5510 If PRECISION > 0, don't return more then PRECISION number of
5511 characters from the string.
5512
5513 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5514 characters have been returned. FIELD_WIDTH < 0 means an infinite
5515 field width.
5516
5517 MULTIBYTE = 0 means disable processing of multibyte characters,
5518 MULTIBYTE > 0 means enable it,
5519 MULTIBYTE < 0 means use IT->multibyte_p.
5520
5521 IT must be initialized via a prior call to init_iterator before
5522 calling this function. */
5523
5524 static void
5525 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5526 struct it *it;
5527 unsigned char *s;
5528 Lisp_Object string;
5529 int charpos;
5530 int precision, field_width, multibyte;
5531 {
5532 /* No region in strings. */
5533 it->region_beg_charpos = it->region_end_charpos = -1;
5534
5535 /* No text property checks performed by default, but see below. */
5536 it->stop_charpos = -1;
5537
5538 /* Set iterator position and end position. */
5539 bzero (&it->current, sizeof it->current);
5540 it->current.overlay_string_index = -1;
5541 it->current.dpvec_index = -1;
5542 xassert (charpos >= 0);
5543
5544 /* If STRING is specified, use its multibyteness, otherwise use the
5545 setting of MULTIBYTE, if specified. */
5546 if (multibyte >= 0)
5547 it->multibyte_p = multibyte > 0;
5548
5549 if (s == NULL)
5550 {
5551 xassert (STRINGP (string));
5552 it->string = string;
5553 it->s = NULL;
5554 it->end_charpos = it->string_nchars = SCHARS (string);
5555 it->method = GET_FROM_STRING;
5556 it->current.string_pos = string_pos (charpos, string);
5557 }
5558 else
5559 {
5560 it->s = s;
5561 it->string = Qnil;
5562
5563 /* Note that we use IT->current.pos, not it->current.string_pos,
5564 for displaying C strings. */
5565 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5566 if (it->multibyte_p)
5567 {
5568 it->current.pos = c_string_pos (charpos, s, 1);
5569 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5570 }
5571 else
5572 {
5573 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5574 it->end_charpos = it->string_nchars = strlen (s);
5575 }
5576
5577 it->method = GET_FROM_C_STRING;
5578 }
5579
5580 /* PRECISION > 0 means don't return more than PRECISION characters
5581 from the string. */
5582 if (precision > 0 && it->end_charpos - charpos > precision)
5583 it->end_charpos = it->string_nchars = charpos + precision;
5584
5585 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5586 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5587 FIELD_WIDTH < 0 means infinite field width. This is useful for
5588 padding with `-' at the end of a mode line. */
5589 if (field_width < 0)
5590 field_width = INFINITY;
5591 if (field_width > it->end_charpos - charpos)
5592 it->end_charpos = charpos + field_width;
5593
5594 /* Use the standard display table for displaying strings. */
5595 if (DISP_TABLE_P (Vstandard_display_table))
5596 it->dp = XCHAR_TABLE (Vstandard_display_table);
5597
5598 it->stop_charpos = charpos;
5599 if (s == NULL && it->multibyte_p)
5600 {
5601 EMACS_INT endpos = SCHARS (it->string);
5602 if (endpos > it->end_charpos)
5603 endpos = it->end_charpos;
5604 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5605 it->string);
5606 }
5607 CHECK_IT (it);
5608 }
5609
5610
5611 \f
5612 /***********************************************************************
5613 Iteration
5614 ***********************************************************************/
5615
5616 /* Map enum it_method value to corresponding next_element_from_* function. */
5617
5618 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5619 {
5620 next_element_from_buffer,
5621 next_element_from_display_vector,
5622 next_element_from_string,
5623 next_element_from_c_string,
5624 next_element_from_image,
5625 next_element_from_stretch
5626 };
5627
5628 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5629
5630
5631 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5632 (possibly with the following characters). */
5633
5634 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5635 ((IT)->cmp_it.id >= 0 \
5636 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5637 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5638 END_CHARPOS, (IT)->w, \
5639 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5640 (IT)->string)))
5641
5642
5643 /* Load IT's display element fields with information about the next
5644 display element from the current position of IT. Value is zero if
5645 end of buffer (or C string) is reached. */
5646
5647 static struct frame *last_escape_glyph_frame = NULL;
5648 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5649 static int last_escape_glyph_merged_face_id = 0;
5650
5651 int
5652 get_next_display_element (it)
5653 struct it *it;
5654 {
5655 /* Non-zero means that we found a display element. Zero means that
5656 we hit the end of what we iterate over. Performance note: the
5657 function pointer `method' used here turns out to be faster than
5658 using a sequence of if-statements. */
5659 int success_p;
5660
5661 get_next:
5662 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5663
5664 if (it->what == IT_CHARACTER)
5665 {
5666 /* Map via display table or translate control characters.
5667 IT->c, IT->len etc. have been set to the next character by
5668 the function call above. If we have a display table, and it
5669 contains an entry for IT->c, translate it. Don't do this if
5670 IT->c itself comes from a display table, otherwise we could
5671 end up in an infinite recursion. (An alternative could be to
5672 count the recursion depth of this function and signal an
5673 error when a certain maximum depth is reached.) Is it worth
5674 it? */
5675 if (success_p && it->dpvec == NULL)
5676 {
5677 Lisp_Object dv;
5678 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5679 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5680 nbsp_or_shy = char_is_other;
5681 int c = it->c; /* This is the character to display. */
5682
5683 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5684 {
5685 xassert (SINGLE_BYTE_CHAR_P (c));
5686 if (unibyte_display_via_language_environment)
5687 {
5688 c = DECODE_CHAR (unibyte, c);
5689 if (c < 0)
5690 c = BYTE8_TO_CHAR (it->c);
5691 }
5692 else
5693 c = BYTE8_TO_CHAR (it->c);
5694 }
5695
5696 if (it->dp
5697 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5698 VECTORP (dv)))
5699 {
5700 struct Lisp_Vector *v = XVECTOR (dv);
5701
5702 /* Return the first character from the display table
5703 entry, if not empty. If empty, don't display the
5704 current character. */
5705 if (v->header.size)
5706 {
5707 it->dpvec_char_len = it->len;
5708 it->dpvec = v->contents;
5709 it->dpend = v->contents + v->header.size;
5710 it->current.dpvec_index = 0;
5711 it->dpvec_face_id = -1;
5712 it->saved_face_id = it->face_id;
5713 it->method = GET_FROM_DISPLAY_VECTOR;
5714 it->ellipsis_p = 0;
5715 }
5716 else
5717 {
5718 set_iterator_to_next (it, 0);
5719 }
5720 goto get_next;
5721 }
5722
5723 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5724 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5725 : c == 0xAD ? char_is_soft_hyphen
5726 : char_is_other);
5727
5728 /* Translate control characters into `\003' or `^C' form.
5729 Control characters coming from a display table entry are
5730 currently not translated because we use IT->dpvec to hold
5731 the translation. This could easily be changed but I
5732 don't believe that it is worth doing.
5733
5734 NBSP and SOFT-HYPEN are property translated too.
5735
5736 Non-printable characters and raw-byte characters are also
5737 translated to octal form. */
5738 if (((c < ' ' || c == 127) /* ASCII control chars */
5739 ? (it->area != TEXT_AREA
5740 /* In mode line, treat \n, \t like other crl chars. */
5741 || (c != '\t'
5742 && it->glyph_row
5743 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5744 || (c != '\n' && c != '\t'))
5745 : (nbsp_or_shy
5746 || CHAR_BYTE8_P (c)
5747 || ! CHAR_PRINTABLE_P (c))))
5748 {
5749 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5750 or a non-printable character which must be displayed
5751 either as '\003' or as `^C' where the '\\' and '^'
5752 can be defined in the display table. Fill
5753 IT->ctl_chars with glyphs for what we have to
5754 display. Then, set IT->dpvec to these glyphs. */
5755 Lisp_Object gc;
5756 int ctl_len;
5757 int face_id, lface_id = 0 ;
5758 int escape_glyph;
5759
5760 /* Handle control characters with ^. */
5761
5762 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5763 {
5764 int g;
5765
5766 g = '^'; /* default glyph for Control */
5767 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5768 if (it->dp
5769 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5770 && GLYPH_CODE_CHAR_VALID_P (gc))
5771 {
5772 g = GLYPH_CODE_CHAR (gc);
5773 lface_id = GLYPH_CODE_FACE (gc);
5774 }
5775 if (lface_id)
5776 {
5777 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5778 }
5779 else if (it->f == last_escape_glyph_frame
5780 && it->face_id == last_escape_glyph_face_id)
5781 {
5782 face_id = last_escape_glyph_merged_face_id;
5783 }
5784 else
5785 {
5786 /* Merge the escape-glyph face into the current face. */
5787 face_id = merge_faces (it->f, Qescape_glyph, 0,
5788 it->face_id);
5789 last_escape_glyph_frame = it->f;
5790 last_escape_glyph_face_id = it->face_id;
5791 last_escape_glyph_merged_face_id = face_id;
5792 }
5793
5794 XSETINT (it->ctl_chars[0], g);
5795 XSETINT (it->ctl_chars[1], c ^ 0100);
5796 ctl_len = 2;
5797 goto display_control;
5798 }
5799
5800 /* Handle non-break space in the mode where it only gets
5801 highlighting. */
5802
5803 if (EQ (Vnobreak_char_display, Qt)
5804 && nbsp_or_shy == char_is_nbsp)
5805 {
5806 /* Merge the no-break-space face into the current face. */
5807 face_id = merge_faces (it->f, Qnobreak_space, 0,
5808 it->face_id);
5809
5810 c = ' ';
5811 XSETINT (it->ctl_chars[0], ' ');
5812 ctl_len = 1;
5813 goto display_control;
5814 }
5815
5816 /* Handle sequences that start with the "escape glyph". */
5817
5818 /* the default escape glyph is \. */
5819 escape_glyph = '\\';
5820
5821 if (it->dp
5822 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5823 && GLYPH_CODE_CHAR_VALID_P (gc))
5824 {
5825 escape_glyph = GLYPH_CODE_CHAR (gc);
5826 lface_id = GLYPH_CODE_FACE (gc);
5827 }
5828 if (lface_id)
5829 {
5830 /* The display table specified a face.
5831 Merge it into face_id and also into escape_glyph. */
5832 face_id = merge_faces (it->f, Qt, lface_id,
5833 it->face_id);
5834 }
5835 else if (it->f == last_escape_glyph_frame
5836 && it->face_id == last_escape_glyph_face_id)
5837 {
5838 face_id = last_escape_glyph_merged_face_id;
5839 }
5840 else
5841 {
5842 /* Merge the escape-glyph face into the current face. */
5843 face_id = merge_faces (it->f, Qescape_glyph, 0,
5844 it->face_id);
5845 last_escape_glyph_frame = it->f;
5846 last_escape_glyph_face_id = it->face_id;
5847 last_escape_glyph_merged_face_id = face_id;
5848 }
5849
5850 /* Handle soft hyphens in the mode where they only get
5851 highlighting. */
5852
5853 if (EQ (Vnobreak_char_display, Qt)
5854 && nbsp_or_shy == char_is_soft_hyphen)
5855 {
5856 XSETINT (it->ctl_chars[0], '-');
5857 ctl_len = 1;
5858 goto display_control;
5859 }
5860
5861 /* Handle non-break space and soft hyphen
5862 with the escape glyph. */
5863
5864 if (nbsp_or_shy)
5865 {
5866 XSETINT (it->ctl_chars[0], escape_glyph);
5867 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5868 XSETINT (it->ctl_chars[1], c);
5869 ctl_len = 2;
5870 goto display_control;
5871 }
5872
5873 {
5874 char str[10];
5875 int len, i;
5876
5877 if (CHAR_BYTE8_P (c))
5878 /* Display \200 instead of \17777600. */
5879 c = CHAR_TO_BYTE8 (c);
5880 len = sprintf (str, "%03o", c);
5881
5882 XSETINT (it->ctl_chars[0], escape_glyph);
5883 for (i = 0; i < len; i++)
5884 XSETINT (it->ctl_chars[i + 1], str[i]);
5885 ctl_len = len + 1;
5886 }
5887
5888 display_control:
5889 /* Set up IT->dpvec and return first character from it. */
5890 it->dpvec_char_len = it->len;
5891 it->dpvec = it->ctl_chars;
5892 it->dpend = it->dpvec + ctl_len;
5893 it->current.dpvec_index = 0;
5894 it->dpvec_face_id = face_id;
5895 it->saved_face_id = it->face_id;
5896 it->method = GET_FROM_DISPLAY_VECTOR;
5897 it->ellipsis_p = 0;
5898 goto get_next;
5899 }
5900 it->char_to_display = c;
5901 }
5902 else if (success_p)
5903 {
5904 it->char_to_display = it->c;
5905 }
5906 }
5907
5908 #ifdef HAVE_WINDOW_SYSTEM
5909 /* Adjust face id for a multibyte character. There are no multibyte
5910 character in unibyte text. */
5911 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5912 && it->multibyte_p
5913 && success_p
5914 && FRAME_WINDOW_P (it->f))
5915 {
5916 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5917
5918 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5919 {
5920 /* Automatic composition with glyph-string. */
5921 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5922
5923 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5924 }
5925 else
5926 {
5927 int pos = (it->s ? -1
5928 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5929 : IT_CHARPOS (*it));
5930 int c;
5931
5932 if (it->what == IT_CHARACTER)
5933 c = it->char_to_display;
5934 else
5935 {
5936 struct composition *cmp = composition_table[it->cmp_it.id];
5937 int i;
5938
5939 c = ' ';
5940 for (i = 0; i < cmp->glyph_len; i++)
5941 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
5942 break;
5943 }
5944 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
5945 }
5946 }
5947 #endif
5948
5949 /* Is this character the last one of a run of characters with
5950 box? If yes, set IT->end_of_box_run_p to 1. */
5951 if (it->face_box_p
5952 && it->s == NULL)
5953 {
5954 if (it->method == GET_FROM_STRING && it->sp)
5955 {
5956 int face_id = underlying_face_id (it);
5957 struct face *face = FACE_FROM_ID (it->f, face_id);
5958
5959 if (face)
5960 {
5961 if (face->box == FACE_NO_BOX)
5962 {
5963 /* If the box comes from face properties in a
5964 display string, check faces in that string. */
5965 int string_face_id = face_after_it_pos (it);
5966 it->end_of_box_run_p
5967 = (FACE_FROM_ID (it->f, string_face_id)->box
5968 == FACE_NO_BOX);
5969 }
5970 /* Otherwise, the box comes from the underlying face.
5971 If this is the last string character displayed, check
5972 the next buffer location. */
5973 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5974 && (it->current.overlay_string_index
5975 == it->n_overlay_strings - 1))
5976 {
5977 EMACS_INT ignore;
5978 int next_face_id;
5979 struct text_pos pos = it->current.pos;
5980 INC_TEXT_POS (pos, it->multibyte_p);
5981
5982 next_face_id = face_at_buffer_position
5983 (it->w, CHARPOS (pos), it->region_beg_charpos,
5984 it->region_end_charpos, &ignore,
5985 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5986 -1);
5987 it->end_of_box_run_p
5988 = (FACE_FROM_ID (it->f, next_face_id)->box
5989 == FACE_NO_BOX);
5990 }
5991 }
5992 }
5993 else
5994 {
5995 int face_id = face_after_it_pos (it);
5996 it->end_of_box_run_p
5997 = (face_id != it->face_id
5998 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
5999 }
6000 }
6001
6002 /* Value is 0 if end of buffer or string reached. */
6003 return success_p;
6004 }
6005
6006
6007 /* Move IT to the next display element.
6008
6009 RESEAT_P non-zero means if called on a newline in buffer text,
6010 skip to the next visible line start.
6011
6012 Functions get_next_display_element and set_iterator_to_next are
6013 separate because I find this arrangement easier to handle than a
6014 get_next_display_element function that also increments IT's
6015 position. The way it is we can first look at an iterator's current
6016 display element, decide whether it fits on a line, and if it does,
6017 increment the iterator position. The other way around we probably
6018 would either need a flag indicating whether the iterator has to be
6019 incremented the next time, or we would have to implement a
6020 decrement position function which would not be easy to write. */
6021
6022 void
6023 set_iterator_to_next (it, reseat_p)
6024 struct it *it;
6025 int reseat_p;
6026 {
6027 /* Reset flags indicating start and end of a sequence of characters
6028 with box. Reset them at the start of this function because
6029 moving the iterator to a new position might set them. */
6030 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6031
6032 switch (it->method)
6033 {
6034 case GET_FROM_BUFFER:
6035 /* The current display element of IT is a character from
6036 current_buffer. Advance in the buffer, and maybe skip over
6037 invisible lines that are so because of selective display. */
6038 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6039 reseat_at_next_visible_line_start (it, 0);
6040 else if (it->cmp_it.id >= 0)
6041 {
6042 IT_CHARPOS (*it) += it->cmp_it.nchars;
6043 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6044 if (it->cmp_it.to < it->cmp_it.nglyphs)
6045 it->cmp_it.from = it->cmp_it.to;
6046 else
6047 {
6048 it->cmp_it.id = -1;
6049 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6050 IT_BYTEPOS (*it), it->stop_charpos,
6051 Qnil);
6052 }
6053 }
6054 else
6055 {
6056 xassert (it->len != 0);
6057 IT_BYTEPOS (*it) += it->len;
6058 IT_CHARPOS (*it) += 1;
6059 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6060 }
6061 break;
6062
6063 case GET_FROM_C_STRING:
6064 /* Current display element of IT is from a C string. */
6065 IT_BYTEPOS (*it) += it->len;
6066 IT_CHARPOS (*it) += 1;
6067 break;
6068
6069 case GET_FROM_DISPLAY_VECTOR:
6070 /* Current display element of IT is from a display table entry.
6071 Advance in the display table definition. Reset it to null if
6072 end reached, and continue with characters from buffers/
6073 strings. */
6074 ++it->current.dpvec_index;
6075
6076 /* Restore face of the iterator to what they were before the
6077 display vector entry (these entries may contain faces). */
6078 it->face_id = it->saved_face_id;
6079
6080 if (it->dpvec + it->current.dpvec_index == it->dpend)
6081 {
6082 int recheck_faces = it->ellipsis_p;
6083
6084 if (it->s)
6085 it->method = GET_FROM_C_STRING;
6086 else if (STRINGP (it->string))
6087 it->method = GET_FROM_STRING;
6088 else
6089 {
6090 it->method = GET_FROM_BUFFER;
6091 it->object = it->w->buffer;
6092 }
6093
6094 it->dpvec = NULL;
6095 it->current.dpvec_index = -1;
6096
6097 /* Skip over characters which were displayed via IT->dpvec. */
6098 if (it->dpvec_char_len < 0)
6099 reseat_at_next_visible_line_start (it, 1);
6100 else if (it->dpvec_char_len > 0)
6101 {
6102 if (it->method == GET_FROM_STRING
6103 && it->n_overlay_strings > 0)
6104 it->ignore_overlay_strings_at_pos_p = 1;
6105 it->len = it->dpvec_char_len;
6106 set_iterator_to_next (it, reseat_p);
6107 }
6108
6109 /* Maybe recheck faces after display vector */
6110 if (recheck_faces)
6111 it->stop_charpos = IT_CHARPOS (*it);
6112 }
6113 break;
6114
6115 case GET_FROM_STRING:
6116 /* Current display element is a character from a Lisp string. */
6117 xassert (it->s == NULL && STRINGP (it->string));
6118 if (it->cmp_it.id >= 0)
6119 {
6120 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6121 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6122 if (it->cmp_it.to < it->cmp_it.nglyphs)
6123 it->cmp_it.from = it->cmp_it.to;
6124 else
6125 {
6126 it->cmp_it.id = -1;
6127 composition_compute_stop_pos (&it->cmp_it,
6128 IT_STRING_CHARPOS (*it),
6129 IT_STRING_BYTEPOS (*it),
6130 it->stop_charpos, it->string);
6131 }
6132 }
6133 else
6134 {
6135 IT_STRING_BYTEPOS (*it) += it->len;
6136 IT_STRING_CHARPOS (*it) += 1;
6137 }
6138
6139 consider_string_end:
6140
6141 if (it->current.overlay_string_index >= 0)
6142 {
6143 /* IT->string is an overlay string. Advance to the
6144 next, if there is one. */
6145 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6146 {
6147 it->ellipsis_p = 0;
6148 next_overlay_string (it);
6149 if (it->ellipsis_p)
6150 setup_for_ellipsis (it, 0);
6151 }
6152 }
6153 else
6154 {
6155 /* IT->string is not an overlay string. If we reached
6156 its end, and there is something on IT->stack, proceed
6157 with what is on the stack. This can be either another
6158 string, this time an overlay string, or a buffer. */
6159 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6160 && it->sp > 0)
6161 {
6162 pop_it (it);
6163 if (it->method == GET_FROM_STRING)
6164 goto consider_string_end;
6165 }
6166 }
6167 break;
6168
6169 case GET_FROM_IMAGE:
6170 case GET_FROM_STRETCH:
6171 /* The position etc with which we have to proceed are on
6172 the stack. The position may be at the end of a string,
6173 if the `display' property takes up the whole string. */
6174 xassert (it->sp > 0);
6175 pop_it (it);
6176 if (it->method == GET_FROM_STRING)
6177 goto consider_string_end;
6178 break;
6179
6180 default:
6181 /* There are no other methods defined, so this should be a bug. */
6182 abort ();
6183 }
6184
6185 xassert (it->method != GET_FROM_STRING
6186 || (STRINGP (it->string)
6187 && IT_STRING_CHARPOS (*it) >= 0));
6188 }
6189
6190 /* Load IT's display element fields with information about the next
6191 display element which comes from a display table entry or from the
6192 result of translating a control character to one of the forms `^C'
6193 or `\003'.
6194
6195 IT->dpvec holds the glyphs to return as characters.
6196 IT->saved_face_id holds the face id before the display vector--it
6197 is restored into IT->face_id in set_iterator_to_next. */
6198
6199 static int
6200 next_element_from_display_vector (it)
6201 struct it *it;
6202 {
6203 Lisp_Object gc;
6204
6205 /* Precondition. */
6206 xassert (it->dpvec && it->current.dpvec_index >= 0);
6207
6208 it->face_id = it->saved_face_id;
6209
6210 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6211 That seemed totally bogus - so I changed it... */
6212 gc = it->dpvec[it->current.dpvec_index];
6213
6214 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6215 {
6216 it->c = GLYPH_CODE_CHAR (gc);
6217 it->len = CHAR_BYTES (it->c);
6218
6219 /* The entry may contain a face id to use. Such a face id is
6220 the id of a Lisp face, not a realized face. A face id of
6221 zero means no face is specified. */
6222 if (it->dpvec_face_id >= 0)
6223 it->face_id = it->dpvec_face_id;
6224 else
6225 {
6226 int lface_id = GLYPH_CODE_FACE (gc);
6227 if (lface_id > 0)
6228 it->face_id = merge_faces (it->f, Qt, lface_id,
6229 it->saved_face_id);
6230 }
6231 }
6232 else
6233 /* Display table entry is invalid. Return a space. */
6234 it->c = ' ', it->len = 1;
6235
6236 /* Don't change position and object of the iterator here. They are
6237 still the values of the character that had this display table
6238 entry or was translated, and that's what we want. */
6239 it->what = IT_CHARACTER;
6240 return 1;
6241 }
6242
6243
6244 /* Load IT with the next display element from Lisp string IT->string.
6245 IT->current.string_pos is the current position within the string.
6246 If IT->current.overlay_string_index >= 0, the Lisp string is an
6247 overlay string. */
6248
6249 static int
6250 next_element_from_string (it)
6251 struct it *it;
6252 {
6253 struct text_pos position;
6254
6255 xassert (STRINGP (it->string));
6256 xassert (IT_STRING_CHARPOS (*it) >= 0);
6257 position = it->current.string_pos;
6258
6259 /* Time to check for invisible text? */
6260 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6261 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6262 {
6263 handle_stop (it);
6264
6265 /* Since a handler may have changed IT->method, we must
6266 recurse here. */
6267 return GET_NEXT_DISPLAY_ELEMENT (it);
6268 }
6269
6270 if (it->current.overlay_string_index >= 0)
6271 {
6272 /* Get the next character from an overlay string. In overlay
6273 strings, There is no field width or padding with spaces to
6274 do. */
6275 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6276 {
6277 it->what = IT_EOB;
6278 return 0;
6279 }
6280 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6281 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6282 && next_element_from_composition (it))
6283 {
6284 return 1;
6285 }
6286 else if (STRING_MULTIBYTE (it->string))
6287 {
6288 const unsigned char *s = (SDATA (it->string)
6289 + IT_STRING_BYTEPOS (*it));
6290 it->c = string_char_and_length (s, &it->len);
6291 }
6292 else
6293 {
6294 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6295 it->len = 1;
6296 }
6297 }
6298 else
6299 {
6300 /* Get the next character from a Lisp string that is not an
6301 overlay string. Such strings come from the mode line, for
6302 example. We may have to pad with spaces, or truncate the
6303 string. See also next_element_from_c_string. */
6304 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6305 {
6306 it->what = IT_EOB;
6307 return 0;
6308 }
6309 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6310 {
6311 /* Pad with spaces. */
6312 it->c = ' ', it->len = 1;
6313 CHARPOS (position) = BYTEPOS (position) = -1;
6314 }
6315 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6316 IT_STRING_BYTEPOS (*it), it->string_nchars)
6317 && next_element_from_composition (it))
6318 {
6319 return 1;
6320 }
6321 else if (STRING_MULTIBYTE (it->string))
6322 {
6323 const unsigned char *s = (SDATA (it->string)
6324 + IT_STRING_BYTEPOS (*it));
6325 it->c = string_char_and_length (s, &it->len);
6326 }
6327 else
6328 {
6329 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6330 it->len = 1;
6331 }
6332 }
6333
6334 /* Record what we have and where it came from. */
6335 it->what = IT_CHARACTER;
6336 it->object = it->string;
6337 it->position = position;
6338 return 1;
6339 }
6340
6341
6342 /* Load IT with next display element from C string IT->s.
6343 IT->string_nchars is the maximum number of characters to return
6344 from the string. IT->end_charpos may be greater than
6345 IT->string_nchars when this function is called, in which case we
6346 may have to return padding spaces. Value is zero if end of string
6347 reached, including padding spaces. */
6348
6349 static int
6350 next_element_from_c_string (it)
6351 struct it *it;
6352 {
6353 int success_p = 1;
6354
6355 xassert (it->s);
6356 it->what = IT_CHARACTER;
6357 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6358 it->object = Qnil;
6359
6360 /* IT's position can be greater IT->string_nchars in case a field
6361 width or precision has been specified when the iterator was
6362 initialized. */
6363 if (IT_CHARPOS (*it) >= it->end_charpos)
6364 {
6365 /* End of the game. */
6366 it->what = IT_EOB;
6367 success_p = 0;
6368 }
6369 else if (IT_CHARPOS (*it) >= it->string_nchars)
6370 {
6371 /* Pad with spaces. */
6372 it->c = ' ', it->len = 1;
6373 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6374 }
6375 else if (it->multibyte_p)
6376 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6377 else
6378 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6379
6380 return success_p;
6381 }
6382
6383
6384 /* Set up IT to return characters from an ellipsis, if appropriate.
6385 The definition of the ellipsis glyphs may come from a display table
6386 entry. This function fills IT with the first glyph from the
6387 ellipsis if an ellipsis is to be displayed. */
6388
6389 static int
6390 next_element_from_ellipsis (it)
6391 struct it *it;
6392 {
6393 if (it->selective_display_ellipsis_p)
6394 setup_for_ellipsis (it, it->len);
6395 else
6396 {
6397 /* The face at the current position may be different from the
6398 face we find after the invisible text. Remember what it
6399 was in IT->saved_face_id, and signal that it's there by
6400 setting face_before_selective_p. */
6401 it->saved_face_id = it->face_id;
6402 it->method = GET_FROM_BUFFER;
6403 it->object = it->w->buffer;
6404 reseat_at_next_visible_line_start (it, 1);
6405 it->face_before_selective_p = 1;
6406 }
6407
6408 return GET_NEXT_DISPLAY_ELEMENT (it);
6409 }
6410
6411
6412 /* Deliver an image display element. The iterator IT is already
6413 filled with image information (done in handle_display_prop). Value
6414 is always 1. */
6415
6416
6417 static int
6418 next_element_from_image (it)
6419 struct it *it;
6420 {
6421 it->what = IT_IMAGE;
6422 it->ignore_overlay_strings_at_pos_p = 0;
6423 return 1;
6424 }
6425
6426
6427 /* Fill iterator IT with next display element from a stretch glyph
6428 property. IT->object is the value of the text property. Value is
6429 always 1. */
6430
6431 static int
6432 next_element_from_stretch (it)
6433 struct it *it;
6434 {
6435 it->what = IT_STRETCH;
6436 return 1;
6437 }
6438
6439
6440 /* Load IT with the next display element from current_buffer. Value
6441 is zero if end of buffer reached. IT->stop_charpos is the next
6442 position at which to stop and check for text properties or buffer
6443 end. */
6444
6445 static int
6446 next_element_from_buffer (it)
6447 struct it *it;
6448 {
6449 int success_p = 1;
6450
6451 xassert (IT_CHARPOS (*it) >= BEGV);
6452
6453 if (IT_CHARPOS (*it) >= it->stop_charpos)
6454 {
6455 if (IT_CHARPOS (*it) >= it->end_charpos)
6456 {
6457 int overlay_strings_follow_p;
6458
6459 /* End of the game, except when overlay strings follow that
6460 haven't been returned yet. */
6461 if (it->overlay_strings_at_end_processed_p)
6462 overlay_strings_follow_p = 0;
6463 else
6464 {
6465 it->overlay_strings_at_end_processed_p = 1;
6466 overlay_strings_follow_p = get_overlay_strings (it, 0);
6467 }
6468
6469 if (overlay_strings_follow_p)
6470 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6471 else
6472 {
6473 it->what = IT_EOB;
6474 it->position = it->current.pos;
6475 success_p = 0;
6476 }
6477 }
6478 else
6479 {
6480 handle_stop (it);
6481 return GET_NEXT_DISPLAY_ELEMENT (it);
6482 }
6483 }
6484 else
6485 {
6486 /* No face changes, overlays etc. in sight, so just return a
6487 character from current_buffer. */
6488 unsigned char *p;
6489
6490 /* Maybe run the redisplay end trigger hook. Performance note:
6491 This doesn't seem to cost measurable time. */
6492 if (it->redisplay_end_trigger_charpos
6493 && it->glyph_row
6494 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6495 run_redisplay_end_trigger_hook (it);
6496
6497 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6498 it->end_charpos)
6499 && next_element_from_composition (it))
6500 {
6501 return 1;
6502 }
6503
6504 /* Get the next character, maybe multibyte. */
6505 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6506 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6507 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6508 else
6509 it->c = *p, it->len = 1;
6510
6511 /* Record what we have and where it came from. */
6512 it->what = IT_CHARACTER;
6513 it->object = it->w->buffer;
6514 it->position = it->current.pos;
6515
6516 /* Normally we return the character found above, except when we
6517 really want to return an ellipsis for selective display. */
6518 if (it->selective)
6519 {
6520 if (it->c == '\n')
6521 {
6522 /* A value of selective > 0 means hide lines indented more
6523 than that number of columns. */
6524 if (it->selective > 0
6525 && IT_CHARPOS (*it) + 1 < ZV
6526 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6527 IT_BYTEPOS (*it) + 1,
6528 (double) it->selective)) /* iftc */
6529 {
6530 success_p = next_element_from_ellipsis (it);
6531 it->dpvec_char_len = -1;
6532 }
6533 }
6534 else if (it->c == '\r' && it->selective == -1)
6535 {
6536 /* A value of selective == -1 means that everything from the
6537 CR to the end of the line is invisible, with maybe an
6538 ellipsis displayed for it. */
6539 success_p = next_element_from_ellipsis (it);
6540 it->dpvec_char_len = -1;
6541 }
6542 }
6543 }
6544
6545 /* Value is zero if end of buffer reached. */
6546 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6547 return success_p;
6548 }
6549
6550
6551 /* Run the redisplay end trigger hook for IT. */
6552
6553 static void
6554 run_redisplay_end_trigger_hook (it)
6555 struct it *it;
6556 {
6557 Lisp_Object args[3];
6558
6559 /* IT->glyph_row should be non-null, i.e. we should be actually
6560 displaying something, or otherwise we should not run the hook. */
6561 xassert (it->glyph_row);
6562
6563 /* Set up hook arguments. */
6564 args[0] = Qredisplay_end_trigger_functions;
6565 args[1] = it->window;
6566 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6567 it->redisplay_end_trigger_charpos = 0;
6568
6569 /* Since we are *trying* to run these functions, don't try to run
6570 them again, even if they get an error. */
6571 it->w->redisplay_end_trigger = Qnil;
6572 Frun_hook_with_args (3, args);
6573
6574 /* Notice if it changed the face of the character we are on. */
6575 handle_face_prop (it);
6576 }
6577
6578
6579 /* Deliver a composition display element. Unlike the other
6580 next_element_from_XXX, this function is not registered in the array
6581 get_next_element[]. It is called from next_element_from_buffer and
6582 next_element_from_string when necessary. */
6583
6584 static int
6585 next_element_from_composition (it)
6586 struct it *it;
6587 {
6588 it->what = IT_COMPOSITION;
6589 it->len = it->cmp_it.nbytes;
6590 if (STRINGP (it->string))
6591 {
6592 if (it->c < 0)
6593 {
6594 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6595 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6596 return 0;
6597 }
6598 it->position = it->current.string_pos;
6599 it->object = it->string;
6600 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6601 IT_STRING_BYTEPOS (*it), it->string);
6602 }
6603 else
6604 {
6605 if (it->c < 0)
6606 {
6607 IT_CHARPOS (*it) += it->cmp_it.nchars;
6608 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6609 return 0;
6610 }
6611 it->position = it->current.pos;
6612 it->object = it->w->buffer;
6613 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6614 IT_BYTEPOS (*it), Qnil);
6615 }
6616 return 1;
6617 }
6618
6619
6620 \f
6621 /***********************************************************************
6622 Moving an iterator without producing glyphs
6623 ***********************************************************************/
6624
6625 /* Check if iterator is at a position corresponding to a valid buffer
6626 position after some move_it_ call. */
6627
6628 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6629 ((it)->method == GET_FROM_STRING \
6630 ? IT_STRING_CHARPOS (*it) == 0 \
6631 : 1)
6632
6633
6634 /* Move iterator IT to a specified buffer or X position within one
6635 line on the display without producing glyphs.
6636
6637 OP should be a bit mask including some or all of these bits:
6638 MOVE_TO_X: Stop on reaching x-position TO_X.
6639 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6640 Regardless of OP's value, stop in reaching the end of the display line.
6641
6642 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6643 This means, in particular, that TO_X includes window's horizontal
6644 scroll amount.
6645
6646 The return value has several possible values that
6647 say what condition caused the scan to stop:
6648
6649 MOVE_POS_MATCH_OR_ZV
6650 - when TO_POS or ZV was reached.
6651
6652 MOVE_X_REACHED
6653 -when TO_X was reached before TO_POS or ZV were reached.
6654
6655 MOVE_LINE_CONTINUED
6656 - when we reached the end of the display area and the line must
6657 be continued.
6658
6659 MOVE_LINE_TRUNCATED
6660 - when we reached the end of the display area and the line is
6661 truncated.
6662
6663 MOVE_NEWLINE_OR_CR
6664 - when we stopped at a line end, i.e. a newline or a CR and selective
6665 display is on. */
6666
6667 static enum move_it_result
6668 move_it_in_display_line_to (struct it *it,
6669 EMACS_INT to_charpos, int to_x,
6670 enum move_operation_enum op)
6671 {
6672 enum move_it_result result = MOVE_UNDEFINED;
6673 struct glyph_row *saved_glyph_row;
6674 struct it wrap_it, atpos_it, atx_it;
6675 int may_wrap = 0;
6676
6677 /* Don't produce glyphs in produce_glyphs. */
6678 saved_glyph_row = it->glyph_row;
6679 it->glyph_row = NULL;
6680
6681 /* Use wrap_it to save a copy of IT wherever a word wrap could
6682 occur. Use atpos_it to save a copy of IT at the desired buffer
6683 position, if found, so that we can scan ahead and check if the
6684 word later overshoots the window edge. Use atx_it similarly, for
6685 pixel positions. */
6686 wrap_it.sp = -1;
6687 atpos_it.sp = -1;
6688 atx_it.sp = -1;
6689
6690 #define BUFFER_POS_REACHED_P() \
6691 ((op & MOVE_TO_POS) != 0 \
6692 && BUFFERP (it->object) \
6693 && IT_CHARPOS (*it) >= to_charpos \
6694 && (it->method == GET_FROM_BUFFER \
6695 || (it->method == GET_FROM_DISPLAY_VECTOR \
6696 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6697
6698 /* If there's a line-/wrap-prefix, handle it. */
6699 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6700 && it->current_y < it->last_visible_y)
6701 handle_line_prefix (it);
6702
6703 while (1)
6704 {
6705 int x, i, ascent = 0, descent = 0;
6706
6707 /* Utility macro to reset an iterator with x, ascent, and descent. */
6708 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6709 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6710 (IT)->max_descent = descent)
6711
6712 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6713 glyph). */
6714 if ((op & MOVE_TO_POS) != 0
6715 && BUFFERP (it->object)
6716 && it->method == GET_FROM_BUFFER
6717 && IT_CHARPOS (*it) > to_charpos)
6718 {
6719 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6720 {
6721 result = MOVE_POS_MATCH_OR_ZV;
6722 break;
6723 }
6724 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6725 /* If wrap_it is valid, the current position might be in a
6726 word that is wrapped. So, save the iterator in
6727 atpos_it and continue to see if wrapping happens. */
6728 atpos_it = *it;
6729 }
6730
6731 /* Stop when ZV reached.
6732 We used to stop here when TO_CHARPOS reached as well, but that is
6733 too soon if this glyph does not fit on this line. So we handle it
6734 explicitly below. */
6735 if (!get_next_display_element (it))
6736 {
6737 result = MOVE_POS_MATCH_OR_ZV;
6738 break;
6739 }
6740
6741 if (it->line_wrap == TRUNCATE)
6742 {
6743 if (BUFFER_POS_REACHED_P ())
6744 {
6745 result = MOVE_POS_MATCH_OR_ZV;
6746 break;
6747 }
6748 }
6749 else
6750 {
6751 if (it->line_wrap == WORD_WRAP)
6752 {
6753 if (IT_DISPLAYING_WHITESPACE (it))
6754 may_wrap = 1;
6755 else if (may_wrap)
6756 {
6757 /* We have reached a glyph that follows one or more
6758 whitespace characters. If the position is
6759 already found, we are done. */
6760 if (atpos_it.sp >= 0)
6761 {
6762 *it = atpos_it;
6763 result = MOVE_POS_MATCH_OR_ZV;
6764 goto done;
6765 }
6766 if (atx_it.sp >= 0)
6767 {
6768 *it = atx_it;
6769 result = MOVE_X_REACHED;
6770 goto done;
6771 }
6772 /* Otherwise, we can wrap here. */
6773 wrap_it = *it;
6774 may_wrap = 0;
6775 }
6776 }
6777 }
6778
6779 /* Remember the line height for the current line, in case
6780 the next element doesn't fit on the line. */
6781 ascent = it->max_ascent;
6782 descent = it->max_descent;
6783
6784 /* The call to produce_glyphs will get the metrics of the
6785 display element IT is loaded with. Record the x-position
6786 before this display element, in case it doesn't fit on the
6787 line. */
6788 x = it->current_x;
6789
6790 PRODUCE_GLYPHS (it);
6791
6792 if (it->area != TEXT_AREA)
6793 {
6794 set_iterator_to_next (it, 1);
6795 continue;
6796 }
6797
6798 /* The number of glyphs we get back in IT->nglyphs will normally
6799 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6800 character on a terminal frame, or (iii) a line end. For the
6801 second case, IT->nglyphs - 1 padding glyphs will be present.
6802 (On X frames, there is only one glyph produced for a
6803 composite character.)
6804
6805 The behavior implemented below means, for continuation lines,
6806 that as many spaces of a TAB as fit on the current line are
6807 displayed there. For terminal frames, as many glyphs of a
6808 multi-glyph character are displayed in the current line, too.
6809 This is what the old redisplay code did, and we keep it that
6810 way. Under X, the whole shape of a complex character must
6811 fit on the line or it will be completely displayed in the
6812 next line.
6813
6814 Note that both for tabs and padding glyphs, all glyphs have
6815 the same width. */
6816 if (it->nglyphs)
6817 {
6818 /* More than one glyph or glyph doesn't fit on line. All
6819 glyphs have the same width. */
6820 int single_glyph_width = it->pixel_width / it->nglyphs;
6821 int new_x;
6822 int x_before_this_char = x;
6823 int hpos_before_this_char = it->hpos;
6824
6825 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6826 {
6827 new_x = x + single_glyph_width;
6828
6829 /* We want to leave anything reaching TO_X to the caller. */
6830 if ((op & MOVE_TO_X) && new_x > to_x)
6831 {
6832 if (BUFFER_POS_REACHED_P ())
6833 {
6834 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6835 goto buffer_pos_reached;
6836 if (atpos_it.sp < 0)
6837 {
6838 atpos_it = *it;
6839 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6840 }
6841 }
6842 else
6843 {
6844 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6845 {
6846 it->current_x = x;
6847 result = MOVE_X_REACHED;
6848 break;
6849 }
6850 if (atx_it.sp < 0)
6851 {
6852 atx_it = *it;
6853 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6854 }
6855 }
6856 }
6857
6858 if (/* Lines are continued. */
6859 it->line_wrap != TRUNCATE
6860 && (/* And glyph doesn't fit on the line. */
6861 new_x > it->last_visible_x
6862 /* Or it fits exactly and we're on a window
6863 system frame. */
6864 || (new_x == it->last_visible_x
6865 && FRAME_WINDOW_P (it->f))))
6866 {
6867 if (/* IT->hpos == 0 means the very first glyph
6868 doesn't fit on the line, e.g. a wide image. */
6869 it->hpos == 0
6870 || (new_x == it->last_visible_x
6871 && FRAME_WINDOW_P (it->f)))
6872 {
6873 ++it->hpos;
6874 it->current_x = new_x;
6875
6876 /* The character's last glyph just barely fits
6877 in this row. */
6878 if (i == it->nglyphs - 1)
6879 {
6880 /* If this is the destination position,
6881 return a position *before* it in this row,
6882 now that we know it fits in this row. */
6883 if (BUFFER_POS_REACHED_P ())
6884 {
6885 if (it->line_wrap != WORD_WRAP
6886 || wrap_it.sp < 0)
6887 {
6888 it->hpos = hpos_before_this_char;
6889 it->current_x = x_before_this_char;
6890 result = MOVE_POS_MATCH_OR_ZV;
6891 break;
6892 }
6893 if (it->line_wrap == WORD_WRAP
6894 && atpos_it.sp < 0)
6895 {
6896 atpos_it = *it;
6897 atpos_it.current_x = x_before_this_char;
6898 atpos_it.hpos = hpos_before_this_char;
6899 }
6900 }
6901
6902 set_iterator_to_next (it, 1);
6903 /* On graphical terminals, newlines may
6904 "overflow" into the fringe if
6905 overflow-newline-into-fringe is non-nil.
6906 On text-only terminals, newlines may
6907 overflow into the last glyph on the
6908 display line.*/
6909 if (!FRAME_WINDOW_P (it->f)
6910 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6911 {
6912 if (!get_next_display_element (it))
6913 {
6914 result = MOVE_POS_MATCH_OR_ZV;
6915 break;
6916 }
6917 if (BUFFER_POS_REACHED_P ())
6918 {
6919 if (ITERATOR_AT_END_OF_LINE_P (it))
6920 result = MOVE_POS_MATCH_OR_ZV;
6921 else
6922 result = MOVE_LINE_CONTINUED;
6923 break;
6924 }
6925 if (ITERATOR_AT_END_OF_LINE_P (it))
6926 {
6927 result = MOVE_NEWLINE_OR_CR;
6928 break;
6929 }
6930 }
6931 }
6932 }
6933 else
6934 IT_RESET_X_ASCENT_DESCENT (it);
6935
6936 if (wrap_it.sp >= 0)
6937 {
6938 *it = wrap_it;
6939 atpos_it.sp = -1;
6940 atx_it.sp = -1;
6941 }
6942
6943 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6944 IT_CHARPOS (*it)));
6945 result = MOVE_LINE_CONTINUED;
6946 break;
6947 }
6948
6949 if (BUFFER_POS_REACHED_P ())
6950 {
6951 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6952 goto buffer_pos_reached;
6953 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6954 {
6955 atpos_it = *it;
6956 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6957 }
6958 }
6959
6960 if (new_x > it->first_visible_x)
6961 {
6962 /* Glyph is visible. Increment number of glyphs that
6963 would be displayed. */
6964 ++it->hpos;
6965 }
6966 }
6967
6968 if (result != MOVE_UNDEFINED)
6969 break;
6970 }
6971 else if (BUFFER_POS_REACHED_P ())
6972 {
6973 buffer_pos_reached:
6974 IT_RESET_X_ASCENT_DESCENT (it);
6975 result = MOVE_POS_MATCH_OR_ZV;
6976 break;
6977 }
6978 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6979 {
6980 /* Stop when TO_X specified and reached. This check is
6981 necessary here because of lines consisting of a line end,
6982 only. The line end will not produce any glyphs and we
6983 would never get MOVE_X_REACHED. */
6984 xassert (it->nglyphs == 0);
6985 result = MOVE_X_REACHED;
6986 break;
6987 }
6988
6989 /* Is this a line end? If yes, we're done. */
6990 if (ITERATOR_AT_END_OF_LINE_P (it))
6991 {
6992 result = MOVE_NEWLINE_OR_CR;
6993 break;
6994 }
6995
6996 /* The current display element has been consumed. Advance
6997 to the next. */
6998 set_iterator_to_next (it, 1);
6999
7000 /* Stop if lines are truncated and IT's current x-position is
7001 past the right edge of the window now. */
7002 if (it->line_wrap == TRUNCATE
7003 && it->current_x >= it->last_visible_x)
7004 {
7005 if (!FRAME_WINDOW_P (it->f)
7006 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7007 {
7008 if (!get_next_display_element (it)
7009 || BUFFER_POS_REACHED_P ())
7010 {
7011 result = MOVE_POS_MATCH_OR_ZV;
7012 break;
7013 }
7014 if (ITERATOR_AT_END_OF_LINE_P (it))
7015 {
7016 result = MOVE_NEWLINE_OR_CR;
7017 break;
7018 }
7019 }
7020 result = MOVE_LINE_TRUNCATED;
7021 break;
7022 }
7023 #undef IT_RESET_X_ASCENT_DESCENT
7024 }
7025
7026 #undef BUFFER_POS_REACHED_P
7027
7028 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7029 restore the saved iterator. */
7030 if (atpos_it.sp >= 0)
7031 *it = atpos_it;
7032 else if (atx_it.sp >= 0)
7033 *it = atx_it;
7034
7035 done:
7036
7037 /* Restore the iterator settings altered at the beginning of this
7038 function. */
7039 it->glyph_row = saved_glyph_row;
7040 return result;
7041 }
7042
7043 /* For external use. */
7044 void
7045 move_it_in_display_line (struct it *it,
7046 EMACS_INT to_charpos, int to_x,
7047 enum move_operation_enum op)
7048 {
7049 if (it->line_wrap == WORD_WRAP
7050 && (op & MOVE_TO_X))
7051 {
7052 struct it save_it = *it;
7053 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7054 /* When word-wrap is on, TO_X may lie past the end
7055 of a wrapped line. Then it->current is the
7056 character on the next line, so backtrack to the
7057 space before the wrap point. */
7058 if (skip == MOVE_LINE_CONTINUED)
7059 {
7060 int prev_x = max (it->current_x - 1, 0);
7061 *it = save_it;
7062 move_it_in_display_line_to
7063 (it, -1, prev_x, MOVE_TO_X);
7064 }
7065 }
7066 else
7067 move_it_in_display_line_to (it, to_charpos, to_x, op);
7068 }
7069
7070
7071 /* Move IT forward until it satisfies one or more of the criteria in
7072 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7073
7074 OP is a bit-mask that specifies where to stop, and in particular,
7075 which of those four position arguments makes a difference. See the
7076 description of enum move_operation_enum.
7077
7078 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7079 screen line, this function will set IT to the next position >
7080 TO_CHARPOS. */
7081
7082 void
7083 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7084 struct it *it;
7085 int to_charpos, to_x, to_y, to_vpos;
7086 int op;
7087 {
7088 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7089 int line_height, line_start_x = 0, reached = 0;
7090
7091 for (;;)
7092 {
7093 if (op & MOVE_TO_VPOS)
7094 {
7095 /* If no TO_CHARPOS and no TO_X specified, stop at the
7096 start of the line TO_VPOS. */
7097 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7098 {
7099 if (it->vpos == to_vpos)
7100 {
7101 reached = 1;
7102 break;
7103 }
7104 else
7105 skip = move_it_in_display_line_to (it, -1, -1, 0);
7106 }
7107 else
7108 {
7109 /* TO_VPOS >= 0 means stop at TO_X in the line at
7110 TO_VPOS, or at TO_POS, whichever comes first. */
7111 if (it->vpos == to_vpos)
7112 {
7113 reached = 2;
7114 break;
7115 }
7116
7117 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7118
7119 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7120 {
7121 reached = 3;
7122 break;
7123 }
7124 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7125 {
7126 /* We have reached TO_X but not in the line we want. */
7127 skip = move_it_in_display_line_to (it, to_charpos,
7128 -1, MOVE_TO_POS);
7129 if (skip == MOVE_POS_MATCH_OR_ZV)
7130 {
7131 reached = 4;
7132 break;
7133 }
7134 }
7135 }
7136 }
7137 else if (op & MOVE_TO_Y)
7138 {
7139 struct it it_backup;
7140
7141 if (it->line_wrap == WORD_WRAP)
7142 it_backup = *it;
7143
7144 /* TO_Y specified means stop at TO_X in the line containing
7145 TO_Y---or at TO_CHARPOS if this is reached first. The
7146 problem is that we can't really tell whether the line
7147 contains TO_Y before we have completely scanned it, and
7148 this may skip past TO_X. What we do is to first scan to
7149 TO_X.
7150
7151 If TO_X is not specified, use a TO_X of zero. The reason
7152 is to make the outcome of this function more predictable.
7153 If we didn't use TO_X == 0, we would stop at the end of
7154 the line which is probably not what a caller would expect
7155 to happen. */
7156 skip = move_it_in_display_line_to
7157 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7158 (MOVE_TO_X | (op & MOVE_TO_POS)));
7159
7160 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7161 if (skip == MOVE_POS_MATCH_OR_ZV)
7162 reached = 5;
7163 else if (skip == MOVE_X_REACHED)
7164 {
7165 /* If TO_X was reached, we want to know whether TO_Y is
7166 in the line. We know this is the case if the already
7167 scanned glyphs make the line tall enough. Otherwise,
7168 we must check by scanning the rest of the line. */
7169 line_height = it->max_ascent + it->max_descent;
7170 if (to_y >= it->current_y
7171 && to_y < it->current_y + line_height)
7172 {
7173 reached = 6;
7174 break;
7175 }
7176 it_backup = *it;
7177 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7178 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7179 op & MOVE_TO_POS);
7180 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7181 line_height = it->max_ascent + it->max_descent;
7182 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7183
7184 if (to_y >= it->current_y
7185 && to_y < it->current_y + line_height)
7186 {
7187 /* If TO_Y is in this line and TO_X was reached
7188 above, we scanned too far. We have to restore
7189 IT's settings to the ones before skipping. */
7190 *it = it_backup;
7191 reached = 6;
7192 }
7193 else
7194 {
7195 skip = skip2;
7196 if (skip == MOVE_POS_MATCH_OR_ZV)
7197 reached = 7;
7198 }
7199 }
7200 else
7201 {
7202 /* Check whether TO_Y is in this line. */
7203 line_height = it->max_ascent + it->max_descent;
7204 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7205
7206 if (to_y >= it->current_y
7207 && to_y < it->current_y + line_height)
7208 {
7209 /* When word-wrap is on, TO_X may lie past the end
7210 of a wrapped line. Then it->current is the
7211 character on the next line, so backtrack to the
7212 space before the wrap point. */
7213 if (skip == MOVE_LINE_CONTINUED
7214 && it->line_wrap == WORD_WRAP)
7215 {
7216 int prev_x = max (it->current_x - 1, 0);
7217 *it = it_backup;
7218 skip = move_it_in_display_line_to
7219 (it, -1, prev_x, MOVE_TO_X);
7220 }
7221 reached = 6;
7222 }
7223 }
7224
7225 if (reached)
7226 break;
7227 }
7228 else if (BUFFERP (it->object)
7229 && (it->method == GET_FROM_BUFFER
7230 || it->method == GET_FROM_STRETCH)
7231 && IT_CHARPOS (*it) >= to_charpos)
7232 skip = MOVE_POS_MATCH_OR_ZV;
7233 else
7234 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7235
7236 switch (skip)
7237 {
7238 case MOVE_POS_MATCH_OR_ZV:
7239 reached = 8;
7240 goto out;
7241
7242 case MOVE_NEWLINE_OR_CR:
7243 set_iterator_to_next (it, 1);
7244 it->continuation_lines_width = 0;
7245 break;
7246
7247 case MOVE_LINE_TRUNCATED:
7248 it->continuation_lines_width = 0;
7249 reseat_at_next_visible_line_start (it, 0);
7250 if ((op & MOVE_TO_POS) != 0
7251 && IT_CHARPOS (*it) > to_charpos)
7252 {
7253 reached = 9;
7254 goto out;
7255 }
7256 break;
7257
7258 case MOVE_LINE_CONTINUED:
7259 /* For continued lines ending in a tab, some of the glyphs
7260 associated with the tab are displayed on the current
7261 line. Since it->current_x does not include these glyphs,
7262 we use it->last_visible_x instead. */
7263 if (it->c == '\t')
7264 {
7265 it->continuation_lines_width += it->last_visible_x;
7266 /* When moving by vpos, ensure that the iterator really
7267 advances to the next line (bug#847, bug#969). Fixme:
7268 do we need to do this in other circumstances? */
7269 if (it->current_x != it->last_visible_x
7270 && (op & MOVE_TO_VPOS)
7271 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7272 {
7273 line_start_x = it->current_x + it->pixel_width
7274 - it->last_visible_x;
7275 set_iterator_to_next (it, 0);
7276 }
7277 }
7278 else
7279 it->continuation_lines_width += it->current_x;
7280 break;
7281
7282 default:
7283 abort ();
7284 }
7285
7286 /* Reset/increment for the next run. */
7287 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7288 it->current_x = line_start_x;
7289 line_start_x = 0;
7290 it->hpos = 0;
7291 it->current_y += it->max_ascent + it->max_descent;
7292 ++it->vpos;
7293 last_height = it->max_ascent + it->max_descent;
7294 last_max_ascent = it->max_ascent;
7295 it->max_ascent = it->max_descent = 0;
7296 }
7297
7298 out:
7299
7300 /* On text terminals, we may stop at the end of a line in the middle
7301 of a multi-character glyph. If the glyph itself is continued,
7302 i.e. it is actually displayed on the next line, don't treat this
7303 stopping point as valid; move to the next line instead (unless
7304 that brings us offscreen). */
7305 if (!FRAME_WINDOW_P (it->f)
7306 && op & MOVE_TO_POS
7307 && IT_CHARPOS (*it) == to_charpos
7308 && it->what == IT_CHARACTER
7309 && it->nglyphs > 1
7310 && it->line_wrap == WINDOW_WRAP
7311 && it->current_x == it->last_visible_x - 1
7312 && it->c != '\n'
7313 && it->c != '\t'
7314 && it->vpos < XFASTINT (it->w->window_end_vpos))
7315 {
7316 it->continuation_lines_width += it->current_x;
7317 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7318 it->current_y += it->max_ascent + it->max_descent;
7319 ++it->vpos;
7320 last_height = it->max_ascent + it->max_descent;
7321 last_max_ascent = it->max_ascent;
7322 }
7323
7324 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7325 }
7326
7327
7328 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7329
7330 If DY > 0, move IT backward at least that many pixels. DY = 0
7331 means move IT backward to the preceding line start or BEGV. This
7332 function may move over more than DY pixels if IT->current_y - DY
7333 ends up in the middle of a line; in this case IT->current_y will be
7334 set to the top of the line moved to. */
7335
7336 void
7337 move_it_vertically_backward (it, dy)
7338 struct it *it;
7339 int dy;
7340 {
7341 int nlines, h;
7342 struct it it2, it3;
7343 int start_pos;
7344
7345 move_further_back:
7346 xassert (dy >= 0);
7347
7348 start_pos = IT_CHARPOS (*it);
7349
7350 /* Estimate how many newlines we must move back. */
7351 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7352
7353 /* Set the iterator's position that many lines back. */
7354 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7355 back_to_previous_visible_line_start (it);
7356
7357 /* Reseat the iterator here. When moving backward, we don't want
7358 reseat to skip forward over invisible text, set up the iterator
7359 to deliver from overlay strings at the new position etc. So,
7360 use reseat_1 here. */
7361 reseat_1 (it, it->current.pos, 1);
7362
7363 /* We are now surely at a line start. */
7364 it->current_x = it->hpos = 0;
7365 it->continuation_lines_width = 0;
7366
7367 /* Move forward and see what y-distance we moved. First move to the
7368 start of the next line so that we get its height. We need this
7369 height to be able to tell whether we reached the specified
7370 y-distance. */
7371 it2 = *it;
7372 it2.max_ascent = it2.max_descent = 0;
7373 do
7374 {
7375 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7376 MOVE_TO_POS | MOVE_TO_VPOS);
7377 }
7378 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7379 xassert (IT_CHARPOS (*it) >= BEGV);
7380 it3 = it2;
7381
7382 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7383 xassert (IT_CHARPOS (*it) >= BEGV);
7384 /* H is the actual vertical distance from the position in *IT
7385 and the starting position. */
7386 h = it2.current_y - it->current_y;
7387 /* NLINES is the distance in number of lines. */
7388 nlines = it2.vpos - it->vpos;
7389
7390 /* Correct IT's y and vpos position
7391 so that they are relative to the starting point. */
7392 it->vpos -= nlines;
7393 it->current_y -= h;
7394
7395 if (dy == 0)
7396 {
7397 /* DY == 0 means move to the start of the screen line. The
7398 value of nlines is > 0 if continuation lines were involved. */
7399 if (nlines > 0)
7400 move_it_by_lines (it, nlines, 1);
7401 }
7402 else
7403 {
7404 /* The y-position we try to reach, relative to *IT.
7405 Note that H has been subtracted in front of the if-statement. */
7406 int target_y = it->current_y + h - dy;
7407 int y0 = it3.current_y;
7408 int y1 = line_bottom_y (&it3);
7409 int line_height = y1 - y0;
7410
7411 /* If we did not reach target_y, try to move further backward if
7412 we can. If we moved too far backward, try to move forward. */
7413 if (target_y < it->current_y
7414 /* This is heuristic. In a window that's 3 lines high, with
7415 a line height of 13 pixels each, recentering with point
7416 on the bottom line will try to move -39/2 = 19 pixels
7417 backward. Try to avoid moving into the first line. */
7418 && (it->current_y - target_y
7419 > min (window_box_height (it->w), line_height * 2 / 3))
7420 && IT_CHARPOS (*it) > BEGV)
7421 {
7422 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7423 target_y - it->current_y));
7424 dy = it->current_y - target_y;
7425 goto move_further_back;
7426 }
7427 else if (target_y >= it->current_y + line_height
7428 && IT_CHARPOS (*it) < ZV)
7429 {
7430 /* Should move forward by at least one line, maybe more.
7431
7432 Note: Calling move_it_by_lines can be expensive on
7433 terminal frames, where compute_motion is used (via
7434 vmotion) to do the job, when there are very long lines
7435 and truncate-lines is nil. That's the reason for
7436 treating terminal frames specially here. */
7437
7438 if (!FRAME_WINDOW_P (it->f))
7439 move_it_vertically (it, target_y - (it->current_y + line_height));
7440 else
7441 {
7442 do
7443 {
7444 move_it_by_lines (it, 1, 1);
7445 }
7446 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7447 }
7448 }
7449 }
7450 }
7451
7452
7453 /* Move IT by a specified amount of pixel lines DY. DY negative means
7454 move backwards. DY = 0 means move to start of screen line. At the
7455 end, IT will be on the start of a screen line. */
7456
7457 void
7458 move_it_vertically (it, dy)
7459 struct it *it;
7460 int dy;
7461 {
7462 if (dy <= 0)
7463 move_it_vertically_backward (it, -dy);
7464 else
7465 {
7466 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7467 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7468 MOVE_TO_POS | MOVE_TO_Y);
7469 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7470
7471 /* If buffer ends in ZV without a newline, move to the start of
7472 the line to satisfy the post-condition. */
7473 if (IT_CHARPOS (*it) == ZV
7474 && ZV > BEGV
7475 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7476 move_it_by_lines (it, 0, 0);
7477 }
7478 }
7479
7480
7481 /* Move iterator IT past the end of the text line it is in. */
7482
7483 void
7484 move_it_past_eol (it)
7485 struct it *it;
7486 {
7487 enum move_it_result rc;
7488
7489 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7490 if (rc == MOVE_NEWLINE_OR_CR)
7491 set_iterator_to_next (it, 0);
7492 }
7493
7494
7495 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7496 negative means move up. DVPOS == 0 means move to the start of the
7497 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7498 NEED_Y_P is zero, IT->current_y will be left unchanged.
7499
7500 Further optimization ideas: If we would know that IT->f doesn't use
7501 a face with proportional font, we could be faster for
7502 truncate-lines nil. */
7503
7504 void
7505 move_it_by_lines (it, dvpos, need_y_p)
7506 struct it *it;
7507 int dvpos, need_y_p;
7508 {
7509 struct position pos;
7510
7511 /* The commented-out optimization uses vmotion on terminals. This
7512 gives bad results, because elements like it->what, on which
7513 callers such as pos_visible_p rely, aren't updated. */
7514 /* if (!FRAME_WINDOW_P (it->f))
7515 {
7516 struct text_pos textpos;
7517
7518 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7519 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7520 reseat (it, textpos, 1);
7521 it->vpos += pos.vpos;
7522 it->current_y += pos.vpos;
7523 }
7524 else */
7525
7526 if (dvpos == 0)
7527 {
7528 /* DVPOS == 0 means move to the start of the screen line. */
7529 move_it_vertically_backward (it, 0);
7530 xassert (it->current_x == 0 && it->hpos == 0);
7531 /* Let next call to line_bottom_y calculate real line height */
7532 last_height = 0;
7533 }
7534 else if (dvpos > 0)
7535 {
7536 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7537 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7538 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7539 }
7540 else
7541 {
7542 struct it it2;
7543 int start_charpos, i;
7544
7545 /* Start at the beginning of the screen line containing IT's
7546 position. This may actually move vertically backwards,
7547 in case of overlays, so adjust dvpos accordingly. */
7548 dvpos += it->vpos;
7549 move_it_vertically_backward (it, 0);
7550 dvpos -= it->vpos;
7551
7552 /* Go back -DVPOS visible lines and reseat the iterator there. */
7553 start_charpos = IT_CHARPOS (*it);
7554 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7555 back_to_previous_visible_line_start (it);
7556 reseat (it, it->current.pos, 1);
7557
7558 /* Move further back if we end up in a string or an image. */
7559 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7560 {
7561 /* First try to move to start of display line. */
7562 dvpos += it->vpos;
7563 move_it_vertically_backward (it, 0);
7564 dvpos -= it->vpos;
7565 if (IT_POS_VALID_AFTER_MOVE_P (it))
7566 break;
7567 /* If start of line is still in string or image,
7568 move further back. */
7569 back_to_previous_visible_line_start (it);
7570 reseat (it, it->current.pos, 1);
7571 dvpos--;
7572 }
7573
7574 it->current_x = it->hpos = 0;
7575
7576 /* Above call may have moved too far if continuation lines
7577 are involved. Scan forward and see if it did. */
7578 it2 = *it;
7579 it2.vpos = it2.current_y = 0;
7580 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7581 it->vpos -= it2.vpos;
7582 it->current_y -= it2.current_y;
7583 it->current_x = it->hpos = 0;
7584
7585 /* If we moved too far back, move IT some lines forward. */
7586 if (it2.vpos > -dvpos)
7587 {
7588 int delta = it2.vpos + dvpos;
7589 it2 = *it;
7590 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7591 /* Move back again if we got too far ahead. */
7592 if (IT_CHARPOS (*it) >= start_charpos)
7593 *it = it2;
7594 }
7595 }
7596 }
7597
7598 /* Return 1 if IT points into the middle of a display vector. */
7599
7600 int
7601 in_display_vector_p (it)
7602 struct it *it;
7603 {
7604 return (it->method == GET_FROM_DISPLAY_VECTOR
7605 && it->current.dpvec_index > 0
7606 && it->dpvec + it->current.dpvec_index != it->dpend);
7607 }
7608
7609 \f
7610 /***********************************************************************
7611 Messages
7612 ***********************************************************************/
7613
7614
7615 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7616 to *Messages*. */
7617
7618 void
7619 add_to_log (format, arg1, arg2)
7620 char *format;
7621 Lisp_Object arg1, arg2;
7622 {
7623 Lisp_Object args[3];
7624 Lisp_Object msg, fmt;
7625 char *buffer;
7626 int len;
7627 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7628 USE_SAFE_ALLOCA;
7629
7630 /* Do nothing if called asynchronously. Inserting text into
7631 a buffer may call after-change-functions and alike and
7632 that would means running Lisp asynchronously. */
7633 if (handling_signal)
7634 return;
7635
7636 fmt = msg = Qnil;
7637 GCPRO4 (fmt, msg, arg1, arg2);
7638
7639 args[0] = fmt = build_string (format);
7640 args[1] = arg1;
7641 args[2] = arg2;
7642 msg = Fformat (3, args);
7643
7644 len = SBYTES (msg) + 1;
7645 SAFE_ALLOCA (buffer, char *, len);
7646 bcopy (SDATA (msg), buffer, len);
7647
7648 message_dolog (buffer, len - 1, 1, 0);
7649 SAFE_FREE ();
7650
7651 UNGCPRO;
7652 }
7653
7654
7655 /* Output a newline in the *Messages* buffer if "needs" one. */
7656
7657 void
7658 message_log_maybe_newline ()
7659 {
7660 if (message_log_need_newline)
7661 message_dolog ("", 0, 1, 0);
7662 }
7663
7664
7665 /* Add a string M of length NBYTES to the message log, optionally
7666 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7667 nonzero, means interpret the contents of M as multibyte. This
7668 function calls low-level routines in order to bypass text property
7669 hooks, etc. which might not be safe to run.
7670
7671 This may GC (insert may run before/after change hooks),
7672 so the buffer M must NOT point to a Lisp string. */
7673
7674 void
7675 message_dolog (m, nbytes, nlflag, multibyte)
7676 const char *m;
7677 int nbytes, nlflag, multibyte;
7678 {
7679 if (!NILP (Vmemory_full))
7680 return;
7681
7682 if (!NILP (Vmessage_log_max))
7683 {
7684 struct buffer *oldbuf;
7685 Lisp_Object oldpoint, oldbegv, oldzv;
7686 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7687 int point_at_end = 0;
7688 int zv_at_end = 0;
7689 Lisp_Object old_deactivate_mark, tem;
7690 struct gcpro gcpro1;
7691
7692 old_deactivate_mark = Vdeactivate_mark;
7693 oldbuf = current_buffer;
7694 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7695 current_buffer->undo_list = Qt;
7696
7697 oldpoint = message_dolog_marker1;
7698 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7699 oldbegv = message_dolog_marker2;
7700 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7701 oldzv = message_dolog_marker3;
7702 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7703 GCPRO1 (old_deactivate_mark);
7704
7705 if (PT == Z)
7706 point_at_end = 1;
7707 if (ZV == Z)
7708 zv_at_end = 1;
7709
7710 BEGV = BEG;
7711 BEGV_BYTE = BEG_BYTE;
7712 ZV = Z;
7713 ZV_BYTE = Z_BYTE;
7714 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7715
7716 /* Insert the string--maybe converting multibyte to single byte
7717 or vice versa, so that all the text fits the buffer. */
7718 if (multibyte
7719 && NILP (current_buffer->enable_multibyte_characters))
7720 {
7721 int i, c, char_bytes;
7722 unsigned char work[1];
7723
7724 /* Convert a multibyte string to single-byte
7725 for the *Message* buffer. */
7726 for (i = 0; i < nbytes; i += char_bytes)
7727 {
7728 c = string_char_and_length (m + i, &char_bytes);
7729 work[0] = (ASCII_CHAR_P (c)
7730 ? c
7731 : multibyte_char_to_unibyte (c, Qnil));
7732 insert_1_both (work, 1, 1, 1, 0, 0);
7733 }
7734 }
7735 else if (! multibyte
7736 && ! NILP (current_buffer->enable_multibyte_characters))
7737 {
7738 int i, c, char_bytes;
7739 unsigned char *msg = (unsigned char *) m;
7740 unsigned char str[MAX_MULTIBYTE_LENGTH];
7741 /* Convert a single-byte string to multibyte
7742 for the *Message* buffer. */
7743 for (i = 0; i < nbytes; i++)
7744 {
7745 c = msg[i];
7746 MAKE_CHAR_MULTIBYTE (c);
7747 char_bytes = CHAR_STRING (c, str);
7748 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7749 }
7750 }
7751 else if (nbytes)
7752 insert_1 (m, nbytes, 1, 0, 0);
7753
7754 if (nlflag)
7755 {
7756 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7757 insert_1 ("\n", 1, 1, 0, 0);
7758
7759 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7760 this_bol = PT;
7761 this_bol_byte = PT_BYTE;
7762
7763 /* See if this line duplicates the previous one.
7764 If so, combine duplicates. */
7765 if (this_bol > BEG)
7766 {
7767 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7768 prev_bol = PT;
7769 prev_bol_byte = PT_BYTE;
7770
7771 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7772 this_bol, this_bol_byte);
7773 if (dup)
7774 {
7775 del_range_both (prev_bol, prev_bol_byte,
7776 this_bol, this_bol_byte, 0);
7777 if (dup > 1)
7778 {
7779 char dupstr[40];
7780 int duplen;
7781
7782 /* If you change this format, don't forget to also
7783 change message_log_check_duplicate. */
7784 sprintf (dupstr, " [%d times]", dup);
7785 duplen = strlen (dupstr);
7786 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7787 insert_1 (dupstr, duplen, 1, 0, 1);
7788 }
7789 }
7790 }
7791
7792 /* If we have more than the desired maximum number of lines
7793 in the *Messages* buffer now, delete the oldest ones.
7794 This is safe because we don't have undo in this buffer. */
7795
7796 if (NATNUMP (Vmessage_log_max))
7797 {
7798 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7799 -XFASTINT (Vmessage_log_max) - 1, 0);
7800 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7801 }
7802 }
7803 BEGV = XMARKER (oldbegv)->charpos;
7804 BEGV_BYTE = marker_byte_position (oldbegv);
7805
7806 if (zv_at_end)
7807 {
7808 ZV = Z;
7809 ZV_BYTE = Z_BYTE;
7810 }
7811 else
7812 {
7813 ZV = XMARKER (oldzv)->charpos;
7814 ZV_BYTE = marker_byte_position (oldzv);
7815 }
7816
7817 if (point_at_end)
7818 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7819 else
7820 /* We can't do Fgoto_char (oldpoint) because it will run some
7821 Lisp code. */
7822 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7823 XMARKER (oldpoint)->bytepos);
7824
7825 UNGCPRO;
7826 unchain_marker (XMARKER (oldpoint));
7827 unchain_marker (XMARKER (oldbegv));
7828 unchain_marker (XMARKER (oldzv));
7829
7830 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7831 set_buffer_internal (oldbuf);
7832 if (NILP (tem))
7833 windows_or_buffers_changed = old_windows_or_buffers_changed;
7834 message_log_need_newline = !nlflag;
7835 Vdeactivate_mark = old_deactivate_mark;
7836 }
7837 }
7838
7839
7840 /* We are at the end of the buffer after just having inserted a newline.
7841 (Note: We depend on the fact we won't be crossing the gap.)
7842 Check to see if the most recent message looks a lot like the previous one.
7843 Return 0 if different, 1 if the new one should just replace it, or a
7844 value N > 1 if we should also append " [N times]". */
7845
7846 static int
7847 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7848 int prev_bol, this_bol;
7849 int prev_bol_byte, this_bol_byte;
7850 {
7851 int i;
7852 int len = Z_BYTE - 1 - this_bol_byte;
7853 int seen_dots = 0;
7854 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7855 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7856
7857 for (i = 0; i < len; i++)
7858 {
7859 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7860 seen_dots = 1;
7861 if (p1[i] != p2[i])
7862 return seen_dots;
7863 }
7864 p1 += len;
7865 if (*p1 == '\n')
7866 return 2;
7867 if (*p1++ == ' ' && *p1++ == '[')
7868 {
7869 int n = 0;
7870 while (*p1 >= '0' && *p1 <= '9')
7871 n = n * 10 + *p1++ - '0';
7872 if (strncmp (p1, " times]\n", 8) == 0)
7873 return n+1;
7874 }
7875 return 0;
7876 }
7877 \f
7878
7879 /* Display an echo area message M with a specified length of NBYTES
7880 bytes. The string may include null characters. If M is 0, clear
7881 out any existing message, and let the mini-buffer text show
7882 through.
7883
7884 This may GC, so the buffer M must NOT point to a Lisp string. */
7885
7886 void
7887 message2 (m, nbytes, multibyte)
7888 const char *m;
7889 int nbytes;
7890 int multibyte;
7891 {
7892 /* First flush out any partial line written with print. */
7893 message_log_maybe_newline ();
7894 if (m)
7895 message_dolog (m, nbytes, 1, multibyte);
7896 message2_nolog (m, nbytes, multibyte);
7897 }
7898
7899
7900 /* The non-logging counterpart of message2. */
7901
7902 void
7903 message2_nolog (m, nbytes, multibyte)
7904 const char *m;
7905 int nbytes, multibyte;
7906 {
7907 struct frame *sf = SELECTED_FRAME ();
7908 message_enable_multibyte = multibyte;
7909
7910 if (FRAME_INITIAL_P (sf))
7911 {
7912 if (noninteractive_need_newline)
7913 putc ('\n', stderr);
7914 noninteractive_need_newline = 0;
7915 if (m)
7916 fwrite (m, nbytes, 1, stderr);
7917 if (cursor_in_echo_area == 0)
7918 fprintf (stderr, "\n");
7919 fflush (stderr);
7920 }
7921 /* A null message buffer means that the frame hasn't really been
7922 initialized yet. Error messages get reported properly by
7923 cmd_error, so this must be just an informative message; toss it. */
7924 else if (INTERACTIVE
7925 && sf->glyphs_initialized_p
7926 && FRAME_MESSAGE_BUF (sf))
7927 {
7928 Lisp_Object mini_window;
7929 struct frame *f;
7930
7931 /* Get the frame containing the mini-buffer
7932 that the selected frame is using. */
7933 mini_window = FRAME_MINIBUF_WINDOW (sf);
7934 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7935
7936 FRAME_SAMPLE_VISIBILITY (f);
7937 if (FRAME_VISIBLE_P (sf)
7938 && ! FRAME_VISIBLE_P (f))
7939 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7940
7941 if (m)
7942 {
7943 set_message (m, Qnil, nbytes, multibyte);
7944 if (minibuffer_auto_raise)
7945 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7946 }
7947 else
7948 clear_message (1, 1);
7949
7950 do_pending_window_change (0);
7951 echo_area_display (1);
7952 do_pending_window_change (0);
7953 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7954 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7955 }
7956 }
7957
7958
7959 /* Display an echo area message M with a specified length of NBYTES
7960 bytes. The string may include null characters. If M is not a
7961 string, clear out any existing message, and let the mini-buffer
7962 text show through.
7963
7964 This function cancels echoing. */
7965
7966 void
7967 message3 (m, nbytes, multibyte)
7968 Lisp_Object m;
7969 int nbytes;
7970 int multibyte;
7971 {
7972 struct gcpro gcpro1;
7973
7974 GCPRO1 (m);
7975 clear_message (1,1);
7976 cancel_echoing ();
7977
7978 /* First flush out any partial line written with print. */
7979 message_log_maybe_newline ();
7980 if (STRINGP (m))
7981 {
7982 char *buffer;
7983 USE_SAFE_ALLOCA;
7984
7985 SAFE_ALLOCA (buffer, char *, nbytes);
7986 bcopy (SDATA (m), buffer, nbytes);
7987 message_dolog (buffer, nbytes, 1, multibyte);
7988 SAFE_FREE ();
7989 }
7990 message3_nolog (m, nbytes, multibyte);
7991
7992 UNGCPRO;
7993 }
7994
7995
7996 /* The non-logging version of message3.
7997 This does not cancel echoing, because it is used for echoing.
7998 Perhaps we need to make a separate function for echoing
7999 and make this cancel echoing. */
8000
8001 void
8002 message3_nolog (m, nbytes, multibyte)
8003 Lisp_Object m;
8004 int nbytes, multibyte;
8005 {
8006 struct frame *sf = SELECTED_FRAME ();
8007 message_enable_multibyte = multibyte;
8008
8009 if (FRAME_INITIAL_P (sf))
8010 {
8011 if (noninteractive_need_newline)
8012 putc ('\n', stderr);
8013 noninteractive_need_newline = 0;
8014 if (STRINGP (m))
8015 fwrite (SDATA (m), nbytes, 1, stderr);
8016 if (cursor_in_echo_area == 0)
8017 fprintf (stderr, "\n");
8018 fflush (stderr);
8019 }
8020 /* A null message buffer means that the frame hasn't really been
8021 initialized yet. Error messages get reported properly by
8022 cmd_error, so this must be just an informative message; toss it. */
8023 else if (INTERACTIVE
8024 && sf->glyphs_initialized_p
8025 && FRAME_MESSAGE_BUF (sf))
8026 {
8027 Lisp_Object mini_window;
8028 Lisp_Object frame;
8029 struct frame *f;
8030
8031 /* Get the frame containing the mini-buffer
8032 that the selected frame is using. */
8033 mini_window = FRAME_MINIBUF_WINDOW (sf);
8034 frame = XWINDOW (mini_window)->frame;
8035 f = XFRAME (frame);
8036
8037 FRAME_SAMPLE_VISIBILITY (f);
8038 if (FRAME_VISIBLE_P (sf)
8039 && !FRAME_VISIBLE_P (f))
8040 Fmake_frame_visible (frame);
8041
8042 if (STRINGP (m) && SCHARS (m) > 0)
8043 {
8044 set_message (NULL, m, nbytes, multibyte);
8045 if (minibuffer_auto_raise)
8046 Fraise_frame (frame);
8047 /* Assume we are not echoing.
8048 (If we are, echo_now will override this.) */
8049 echo_message_buffer = Qnil;
8050 }
8051 else
8052 clear_message (1, 1);
8053
8054 do_pending_window_change (0);
8055 echo_area_display (1);
8056 do_pending_window_change (0);
8057 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8058 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8059 }
8060 }
8061
8062
8063 /* Display a null-terminated echo area message M. If M is 0, clear
8064 out any existing message, and let the mini-buffer text show through.
8065
8066 The buffer M must continue to exist until after the echo area gets
8067 cleared or some other message gets displayed there. Do not pass
8068 text that is stored in a Lisp string. Do not pass text in a buffer
8069 that was alloca'd. */
8070
8071 void
8072 message1 (m)
8073 char *m;
8074 {
8075 message2 (m, (m ? strlen (m) : 0), 0);
8076 }
8077
8078
8079 /* The non-logging counterpart of message1. */
8080
8081 void
8082 message1_nolog (m)
8083 char *m;
8084 {
8085 message2_nolog (m, (m ? strlen (m) : 0), 0);
8086 }
8087
8088 /* Display a message M which contains a single %s
8089 which gets replaced with STRING. */
8090
8091 void
8092 message_with_string (m, string, log)
8093 char *m;
8094 Lisp_Object string;
8095 int log;
8096 {
8097 CHECK_STRING (string);
8098
8099 if (noninteractive)
8100 {
8101 if (m)
8102 {
8103 if (noninteractive_need_newline)
8104 putc ('\n', stderr);
8105 noninteractive_need_newline = 0;
8106 fprintf (stderr, m, SDATA (string));
8107 if (!cursor_in_echo_area)
8108 fprintf (stderr, "\n");
8109 fflush (stderr);
8110 }
8111 }
8112 else if (INTERACTIVE)
8113 {
8114 /* The frame whose minibuffer we're going to display the message on.
8115 It may be larger than the selected frame, so we need
8116 to use its buffer, not the selected frame's buffer. */
8117 Lisp_Object mini_window;
8118 struct frame *f, *sf = SELECTED_FRAME ();
8119
8120 /* Get the frame containing the minibuffer
8121 that the selected frame is using. */
8122 mini_window = FRAME_MINIBUF_WINDOW (sf);
8123 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8124
8125 /* A null message buffer means that the frame hasn't really been
8126 initialized yet. Error messages get reported properly by
8127 cmd_error, so this must be just an informative message; toss it. */
8128 if (FRAME_MESSAGE_BUF (f))
8129 {
8130 Lisp_Object args[2], message;
8131 struct gcpro gcpro1, gcpro2;
8132
8133 args[0] = build_string (m);
8134 args[1] = message = string;
8135 GCPRO2 (args[0], message);
8136 gcpro1.nvars = 2;
8137
8138 message = Fformat (2, args);
8139
8140 if (log)
8141 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8142 else
8143 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8144
8145 UNGCPRO;
8146
8147 /* Print should start at the beginning of the message
8148 buffer next time. */
8149 message_buf_print = 0;
8150 }
8151 }
8152 }
8153
8154
8155 /* Dump an informative message to the minibuf. If M is 0, clear out
8156 any existing message, and let the mini-buffer text show through. */
8157
8158 /* VARARGS 1 */
8159 void
8160 message (m, a1, a2, a3)
8161 char *m;
8162 EMACS_INT a1, a2, a3;
8163 {
8164 if (noninteractive)
8165 {
8166 if (m)
8167 {
8168 if (noninteractive_need_newline)
8169 putc ('\n', stderr);
8170 noninteractive_need_newline = 0;
8171 fprintf (stderr, m, a1, a2, a3);
8172 if (cursor_in_echo_area == 0)
8173 fprintf (stderr, "\n");
8174 fflush (stderr);
8175 }
8176 }
8177 else if (INTERACTIVE)
8178 {
8179 /* The frame whose mini-buffer we're going to display the message
8180 on. It may be larger than the selected frame, so we need to
8181 use its buffer, not the selected frame's buffer. */
8182 Lisp_Object mini_window;
8183 struct frame *f, *sf = SELECTED_FRAME ();
8184
8185 /* Get the frame containing the mini-buffer
8186 that the selected frame is using. */
8187 mini_window = FRAME_MINIBUF_WINDOW (sf);
8188 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8189
8190 /* A null message buffer means that the frame hasn't really been
8191 initialized yet. Error messages get reported properly by
8192 cmd_error, so this must be just an informative message; toss
8193 it. */
8194 if (FRAME_MESSAGE_BUF (f))
8195 {
8196 if (m)
8197 {
8198 int len;
8199 #ifdef NO_ARG_ARRAY
8200 char *a[3];
8201 a[0] = (char *) a1;
8202 a[1] = (char *) a2;
8203 a[2] = (char *) a3;
8204
8205 len = doprnt (FRAME_MESSAGE_BUF (f),
8206 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8207 #else
8208 len = doprnt (FRAME_MESSAGE_BUF (f),
8209 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8210 (char **) &a1);
8211 #endif /* NO_ARG_ARRAY */
8212
8213 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8214 }
8215 else
8216 message1 (0);
8217
8218 /* Print should start at the beginning of the message
8219 buffer next time. */
8220 message_buf_print = 0;
8221 }
8222 }
8223 }
8224
8225
8226 /* The non-logging version of message. */
8227
8228 void
8229 message_nolog (m, a1, a2, a3)
8230 char *m;
8231 EMACS_INT a1, a2, a3;
8232 {
8233 Lisp_Object old_log_max;
8234 old_log_max = Vmessage_log_max;
8235 Vmessage_log_max = Qnil;
8236 message (m, a1, a2, a3);
8237 Vmessage_log_max = old_log_max;
8238 }
8239
8240
8241 /* Display the current message in the current mini-buffer. This is
8242 only called from error handlers in process.c, and is not time
8243 critical. */
8244
8245 void
8246 update_echo_area ()
8247 {
8248 if (!NILP (echo_area_buffer[0]))
8249 {
8250 Lisp_Object string;
8251 string = Fcurrent_message ();
8252 message3 (string, SBYTES (string),
8253 !NILP (current_buffer->enable_multibyte_characters));
8254 }
8255 }
8256
8257
8258 /* Make sure echo area buffers in `echo_buffers' are live.
8259 If they aren't, make new ones. */
8260
8261 static void
8262 ensure_echo_area_buffers ()
8263 {
8264 int i;
8265
8266 for (i = 0; i < 2; ++i)
8267 if (!BUFFERP (echo_buffer[i])
8268 || NILP (XBUFFER (echo_buffer[i])->name))
8269 {
8270 char name[30];
8271 Lisp_Object old_buffer;
8272 int j;
8273
8274 old_buffer = echo_buffer[i];
8275 sprintf (name, " *Echo Area %d*", i);
8276 echo_buffer[i] = Fget_buffer_create (build_string (name));
8277 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8278 /* to force word wrap in echo area -
8279 it was decided to postpone this*/
8280 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8281
8282 for (j = 0; j < 2; ++j)
8283 if (EQ (old_buffer, echo_area_buffer[j]))
8284 echo_area_buffer[j] = echo_buffer[i];
8285 }
8286 }
8287
8288
8289 /* Call FN with args A1..A4 with either the current or last displayed
8290 echo_area_buffer as current buffer.
8291
8292 WHICH zero means use the current message buffer
8293 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8294 from echo_buffer[] and clear it.
8295
8296 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8297 suitable buffer from echo_buffer[] and clear it.
8298
8299 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8300 that the current message becomes the last displayed one, make
8301 choose a suitable buffer for echo_area_buffer[0], and clear it.
8302
8303 Value is what FN returns. */
8304
8305 static int
8306 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8307 struct window *w;
8308 int which;
8309 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8310 EMACS_INT a1;
8311 Lisp_Object a2;
8312 EMACS_INT a3, a4;
8313 {
8314 Lisp_Object buffer;
8315 int this_one, the_other, clear_buffer_p, rc;
8316 int count = SPECPDL_INDEX ();
8317
8318 /* If buffers aren't live, make new ones. */
8319 ensure_echo_area_buffers ();
8320
8321 clear_buffer_p = 0;
8322
8323 if (which == 0)
8324 this_one = 0, the_other = 1;
8325 else if (which > 0)
8326 this_one = 1, the_other = 0;
8327 else
8328 {
8329 this_one = 0, the_other = 1;
8330 clear_buffer_p = 1;
8331
8332 /* We need a fresh one in case the current echo buffer equals
8333 the one containing the last displayed echo area message. */
8334 if (!NILP (echo_area_buffer[this_one])
8335 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8336 echo_area_buffer[this_one] = Qnil;
8337 }
8338
8339 /* Choose a suitable buffer from echo_buffer[] is we don't
8340 have one. */
8341 if (NILP (echo_area_buffer[this_one]))
8342 {
8343 echo_area_buffer[this_one]
8344 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8345 ? echo_buffer[the_other]
8346 : echo_buffer[this_one]);
8347 clear_buffer_p = 1;
8348 }
8349
8350 buffer = echo_area_buffer[this_one];
8351
8352 /* Don't get confused by reusing the buffer used for echoing
8353 for a different purpose. */
8354 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8355 cancel_echoing ();
8356
8357 record_unwind_protect (unwind_with_echo_area_buffer,
8358 with_echo_area_buffer_unwind_data (w));
8359
8360 /* Make the echo area buffer current. Note that for display
8361 purposes, it is not necessary that the displayed window's buffer
8362 == current_buffer, except for text property lookup. So, let's
8363 only set that buffer temporarily here without doing a full
8364 Fset_window_buffer. We must also change w->pointm, though,
8365 because otherwise an assertions in unshow_buffer fails, and Emacs
8366 aborts. */
8367 set_buffer_internal_1 (XBUFFER (buffer));
8368 if (w)
8369 {
8370 w->buffer = buffer;
8371 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8372 }
8373
8374 current_buffer->undo_list = Qt;
8375 current_buffer->read_only = Qnil;
8376 specbind (Qinhibit_read_only, Qt);
8377 specbind (Qinhibit_modification_hooks, Qt);
8378
8379 if (clear_buffer_p && Z > BEG)
8380 del_range (BEG, Z);
8381
8382 xassert (BEGV >= BEG);
8383 xassert (ZV <= Z && ZV >= BEGV);
8384
8385 rc = fn (a1, a2, a3, a4);
8386
8387 xassert (BEGV >= BEG);
8388 xassert (ZV <= Z && ZV >= BEGV);
8389
8390 unbind_to (count, Qnil);
8391 return rc;
8392 }
8393
8394
8395 /* Save state that should be preserved around the call to the function
8396 FN called in with_echo_area_buffer. */
8397
8398 static Lisp_Object
8399 with_echo_area_buffer_unwind_data (w)
8400 struct window *w;
8401 {
8402 int i = 0;
8403 Lisp_Object vector, tmp;
8404
8405 /* Reduce consing by keeping one vector in
8406 Vwith_echo_area_save_vector. */
8407 vector = Vwith_echo_area_save_vector;
8408 Vwith_echo_area_save_vector = Qnil;
8409
8410 if (NILP (vector))
8411 vector = Fmake_vector (make_number (7), Qnil);
8412
8413 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8414 ASET (vector, i, Vdeactivate_mark); ++i;
8415 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8416
8417 if (w)
8418 {
8419 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8420 ASET (vector, i, w->buffer); ++i;
8421 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8422 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8423 }
8424 else
8425 {
8426 int end = i + 4;
8427 for (; i < end; ++i)
8428 ASET (vector, i, Qnil);
8429 }
8430
8431 xassert (i == ASIZE (vector));
8432 return vector;
8433 }
8434
8435
8436 /* Restore global state from VECTOR which was created by
8437 with_echo_area_buffer_unwind_data. */
8438
8439 static Lisp_Object
8440 unwind_with_echo_area_buffer (vector)
8441 Lisp_Object vector;
8442 {
8443 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8444 Vdeactivate_mark = AREF (vector, 1);
8445 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8446
8447 if (WINDOWP (AREF (vector, 3)))
8448 {
8449 struct window *w;
8450 Lisp_Object buffer, charpos, bytepos;
8451
8452 w = XWINDOW (AREF (vector, 3));
8453 buffer = AREF (vector, 4);
8454 charpos = AREF (vector, 5);
8455 bytepos = AREF (vector, 6);
8456
8457 w->buffer = buffer;
8458 set_marker_both (w->pointm, buffer,
8459 XFASTINT (charpos), XFASTINT (bytepos));
8460 }
8461
8462 Vwith_echo_area_save_vector = vector;
8463 return Qnil;
8464 }
8465
8466
8467 /* Set up the echo area for use by print functions. MULTIBYTE_P
8468 non-zero means we will print multibyte. */
8469
8470 void
8471 setup_echo_area_for_printing (multibyte_p)
8472 int multibyte_p;
8473 {
8474 /* If we can't find an echo area any more, exit. */
8475 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8476 Fkill_emacs (Qnil);
8477
8478 ensure_echo_area_buffers ();
8479
8480 if (!message_buf_print)
8481 {
8482 /* A message has been output since the last time we printed.
8483 Choose a fresh echo area buffer. */
8484 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8485 echo_area_buffer[0] = echo_buffer[1];
8486 else
8487 echo_area_buffer[0] = echo_buffer[0];
8488
8489 /* Switch to that buffer and clear it. */
8490 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8491 current_buffer->truncate_lines = Qnil;
8492
8493 if (Z > BEG)
8494 {
8495 int count = SPECPDL_INDEX ();
8496 specbind (Qinhibit_read_only, Qt);
8497 /* Note that undo recording is always disabled. */
8498 del_range (BEG, Z);
8499 unbind_to (count, Qnil);
8500 }
8501 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8502
8503 /* Set up the buffer for the multibyteness we need. */
8504 if (multibyte_p
8505 != !NILP (current_buffer->enable_multibyte_characters))
8506 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8507
8508 /* Raise the frame containing the echo area. */
8509 if (minibuffer_auto_raise)
8510 {
8511 struct frame *sf = SELECTED_FRAME ();
8512 Lisp_Object mini_window;
8513 mini_window = FRAME_MINIBUF_WINDOW (sf);
8514 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8515 }
8516
8517 message_log_maybe_newline ();
8518 message_buf_print = 1;
8519 }
8520 else
8521 {
8522 if (NILP (echo_area_buffer[0]))
8523 {
8524 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8525 echo_area_buffer[0] = echo_buffer[1];
8526 else
8527 echo_area_buffer[0] = echo_buffer[0];
8528 }
8529
8530 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8531 {
8532 /* Someone switched buffers between print requests. */
8533 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8534 current_buffer->truncate_lines = Qnil;
8535 }
8536 }
8537 }
8538
8539
8540 /* Display an echo area message in window W. Value is non-zero if W's
8541 height is changed. If display_last_displayed_message_p is
8542 non-zero, display the message that was last displayed, otherwise
8543 display the current message. */
8544
8545 static int
8546 display_echo_area (w)
8547 struct window *w;
8548 {
8549 int i, no_message_p, window_height_changed_p, count;
8550
8551 /* Temporarily disable garbage collections while displaying the echo
8552 area. This is done because a GC can print a message itself.
8553 That message would modify the echo area buffer's contents while a
8554 redisplay of the buffer is going on, and seriously confuse
8555 redisplay. */
8556 count = inhibit_garbage_collection ();
8557
8558 /* If there is no message, we must call display_echo_area_1
8559 nevertheless because it resizes the window. But we will have to
8560 reset the echo_area_buffer in question to nil at the end because
8561 with_echo_area_buffer will sets it to an empty buffer. */
8562 i = display_last_displayed_message_p ? 1 : 0;
8563 no_message_p = NILP (echo_area_buffer[i]);
8564
8565 window_height_changed_p
8566 = with_echo_area_buffer (w, display_last_displayed_message_p,
8567 display_echo_area_1,
8568 (EMACS_INT) w, Qnil, 0, 0);
8569
8570 if (no_message_p)
8571 echo_area_buffer[i] = Qnil;
8572
8573 unbind_to (count, Qnil);
8574 return window_height_changed_p;
8575 }
8576
8577
8578 /* Helper for display_echo_area. Display the current buffer which
8579 contains the current echo area message in window W, a mini-window,
8580 a pointer to which is passed in A1. A2..A4 are currently not used.
8581 Change the height of W so that all of the message is displayed.
8582 Value is non-zero if height of W was changed. */
8583
8584 static int
8585 display_echo_area_1 (a1, a2, a3, a4)
8586 EMACS_INT a1;
8587 Lisp_Object a2;
8588 EMACS_INT a3, a4;
8589 {
8590 struct window *w = (struct window *) a1;
8591 Lisp_Object window;
8592 struct text_pos start;
8593 int window_height_changed_p = 0;
8594
8595 /* Do this before displaying, so that we have a large enough glyph
8596 matrix for the display. If we can't get enough space for the
8597 whole text, display the last N lines. That works by setting w->start. */
8598 window_height_changed_p = resize_mini_window (w, 0);
8599
8600 /* Use the starting position chosen by resize_mini_window. */
8601 SET_TEXT_POS_FROM_MARKER (start, w->start);
8602
8603 /* Display. */
8604 clear_glyph_matrix (w->desired_matrix);
8605 XSETWINDOW (window, w);
8606 try_window (window, start, 0);
8607
8608 return window_height_changed_p;
8609 }
8610
8611
8612 /* Resize the echo area window to exactly the size needed for the
8613 currently displayed message, if there is one. If a mini-buffer
8614 is active, don't shrink it. */
8615
8616 void
8617 resize_echo_area_exactly ()
8618 {
8619 if (BUFFERP (echo_area_buffer[0])
8620 && WINDOWP (echo_area_window))
8621 {
8622 struct window *w = XWINDOW (echo_area_window);
8623 int resized_p;
8624 Lisp_Object resize_exactly;
8625
8626 if (minibuf_level == 0)
8627 resize_exactly = Qt;
8628 else
8629 resize_exactly = Qnil;
8630
8631 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8632 (EMACS_INT) w, resize_exactly, 0, 0);
8633 if (resized_p)
8634 {
8635 ++windows_or_buffers_changed;
8636 ++update_mode_lines;
8637 redisplay_internal (0);
8638 }
8639 }
8640 }
8641
8642
8643 /* Callback function for with_echo_area_buffer, when used from
8644 resize_echo_area_exactly. A1 contains a pointer to the window to
8645 resize, EXACTLY non-nil means resize the mini-window exactly to the
8646 size of the text displayed. A3 and A4 are not used. Value is what
8647 resize_mini_window returns. */
8648
8649 static int
8650 resize_mini_window_1 (a1, exactly, a3, a4)
8651 EMACS_INT a1;
8652 Lisp_Object exactly;
8653 EMACS_INT a3, a4;
8654 {
8655 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8656 }
8657
8658
8659 /* Resize mini-window W to fit the size of its contents. EXACT_P
8660 means size the window exactly to the size needed. Otherwise, it's
8661 only enlarged until W's buffer is empty.
8662
8663 Set W->start to the right place to begin display. If the whole
8664 contents fit, start at the beginning. Otherwise, start so as
8665 to make the end of the contents appear. This is particularly
8666 important for y-or-n-p, but seems desirable generally.
8667
8668 Value is non-zero if the window height has been changed. */
8669
8670 int
8671 resize_mini_window (w, exact_p)
8672 struct window *w;
8673 int exact_p;
8674 {
8675 struct frame *f = XFRAME (w->frame);
8676 int window_height_changed_p = 0;
8677
8678 xassert (MINI_WINDOW_P (w));
8679
8680 /* By default, start display at the beginning. */
8681 set_marker_both (w->start, w->buffer,
8682 BUF_BEGV (XBUFFER (w->buffer)),
8683 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8684
8685 /* Don't resize windows while redisplaying a window; it would
8686 confuse redisplay functions when the size of the window they are
8687 displaying changes from under them. Such a resizing can happen,
8688 for instance, when which-func prints a long message while
8689 we are running fontification-functions. We're running these
8690 functions with safe_call which binds inhibit-redisplay to t. */
8691 if (!NILP (Vinhibit_redisplay))
8692 return 0;
8693
8694 /* Nil means don't try to resize. */
8695 if (NILP (Vresize_mini_windows)
8696 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8697 return 0;
8698
8699 if (!FRAME_MINIBUF_ONLY_P (f))
8700 {
8701 struct it it;
8702 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8703 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8704 int height, max_height;
8705 int unit = FRAME_LINE_HEIGHT (f);
8706 struct text_pos start;
8707 struct buffer *old_current_buffer = NULL;
8708
8709 if (current_buffer != XBUFFER (w->buffer))
8710 {
8711 old_current_buffer = current_buffer;
8712 set_buffer_internal (XBUFFER (w->buffer));
8713 }
8714
8715 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8716
8717 /* Compute the max. number of lines specified by the user. */
8718 if (FLOATP (Vmax_mini_window_height))
8719 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8720 else if (INTEGERP (Vmax_mini_window_height))
8721 max_height = XINT (Vmax_mini_window_height);
8722 else
8723 max_height = total_height / 4;
8724
8725 /* Correct that max. height if it's bogus. */
8726 max_height = max (1, max_height);
8727 max_height = min (total_height, max_height);
8728
8729 /* Find out the height of the text in the window. */
8730 if (it.line_wrap == TRUNCATE)
8731 height = 1;
8732 else
8733 {
8734 last_height = 0;
8735 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8736 if (it.max_ascent == 0 && it.max_descent == 0)
8737 height = it.current_y + last_height;
8738 else
8739 height = it.current_y + it.max_ascent + it.max_descent;
8740 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8741 height = (height + unit - 1) / unit;
8742 }
8743
8744 /* Compute a suitable window start. */
8745 if (height > max_height)
8746 {
8747 height = max_height;
8748 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8749 move_it_vertically_backward (&it, (height - 1) * unit);
8750 start = it.current.pos;
8751 }
8752 else
8753 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8754 SET_MARKER_FROM_TEXT_POS (w->start, start);
8755
8756 if (EQ (Vresize_mini_windows, Qgrow_only))
8757 {
8758 /* Let it grow only, until we display an empty message, in which
8759 case the window shrinks again. */
8760 if (height > WINDOW_TOTAL_LINES (w))
8761 {
8762 int old_height = WINDOW_TOTAL_LINES (w);
8763 freeze_window_starts (f, 1);
8764 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8765 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8766 }
8767 else if (height < WINDOW_TOTAL_LINES (w)
8768 && (exact_p || BEGV == ZV))
8769 {
8770 int old_height = WINDOW_TOTAL_LINES (w);
8771 freeze_window_starts (f, 0);
8772 shrink_mini_window (w);
8773 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8774 }
8775 }
8776 else
8777 {
8778 /* Always resize to exact size needed. */
8779 if (height > WINDOW_TOTAL_LINES (w))
8780 {
8781 int old_height = WINDOW_TOTAL_LINES (w);
8782 freeze_window_starts (f, 1);
8783 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8784 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8785 }
8786 else if (height < WINDOW_TOTAL_LINES (w))
8787 {
8788 int old_height = WINDOW_TOTAL_LINES (w);
8789 freeze_window_starts (f, 0);
8790 shrink_mini_window (w);
8791
8792 if (height)
8793 {
8794 freeze_window_starts (f, 1);
8795 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8796 }
8797
8798 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8799 }
8800 }
8801
8802 if (old_current_buffer)
8803 set_buffer_internal (old_current_buffer);
8804 }
8805
8806 return window_height_changed_p;
8807 }
8808
8809
8810 /* Value is the current message, a string, or nil if there is no
8811 current message. */
8812
8813 Lisp_Object
8814 current_message ()
8815 {
8816 Lisp_Object msg;
8817
8818 if (!BUFFERP (echo_area_buffer[0]))
8819 msg = Qnil;
8820 else
8821 {
8822 with_echo_area_buffer (0, 0, current_message_1,
8823 (EMACS_INT) &msg, Qnil, 0, 0);
8824 if (NILP (msg))
8825 echo_area_buffer[0] = Qnil;
8826 }
8827
8828 return msg;
8829 }
8830
8831
8832 static int
8833 current_message_1 (a1, a2, a3, a4)
8834 EMACS_INT a1;
8835 Lisp_Object a2;
8836 EMACS_INT a3, a4;
8837 {
8838 Lisp_Object *msg = (Lisp_Object *) a1;
8839
8840 if (Z > BEG)
8841 *msg = make_buffer_string (BEG, Z, 1);
8842 else
8843 *msg = Qnil;
8844 return 0;
8845 }
8846
8847
8848 /* Push the current message on Vmessage_stack for later restauration
8849 by restore_message. Value is non-zero if the current message isn't
8850 empty. This is a relatively infrequent operation, so it's not
8851 worth optimizing. */
8852
8853 int
8854 push_message ()
8855 {
8856 Lisp_Object msg;
8857 msg = current_message ();
8858 Vmessage_stack = Fcons (msg, Vmessage_stack);
8859 return STRINGP (msg);
8860 }
8861
8862
8863 /* Restore message display from the top of Vmessage_stack. */
8864
8865 void
8866 restore_message ()
8867 {
8868 Lisp_Object msg;
8869
8870 xassert (CONSP (Vmessage_stack));
8871 msg = XCAR (Vmessage_stack);
8872 if (STRINGP (msg))
8873 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8874 else
8875 message3_nolog (msg, 0, 0);
8876 }
8877
8878
8879 /* Handler for record_unwind_protect calling pop_message. */
8880
8881 Lisp_Object
8882 pop_message_unwind (dummy)
8883 Lisp_Object dummy;
8884 {
8885 pop_message ();
8886 return Qnil;
8887 }
8888
8889 /* Pop the top-most entry off Vmessage_stack. */
8890
8891 void
8892 pop_message ()
8893 {
8894 xassert (CONSP (Vmessage_stack));
8895 Vmessage_stack = XCDR (Vmessage_stack);
8896 }
8897
8898
8899 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8900 exits. If the stack is not empty, we have a missing pop_message
8901 somewhere. */
8902
8903 void
8904 check_message_stack ()
8905 {
8906 if (!NILP (Vmessage_stack))
8907 abort ();
8908 }
8909
8910
8911 /* Truncate to NCHARS what will be displayed in the echo area the next
8912 time we display it---but don't redisplay it now. */
8913
8914 void
8915 truncate_echo_area (nchars)
8916 int nchars;
8917 {
8918 if (nchars == 0)
8919 echo_area_buffer[0] = Qnil;
8920 /* A null message buffer means that the frame hasn't really been
8921 initialized yet. Error messages get reported properly by
8922 cmd_error, so this must be just an informative message; toss it. */
8923 else if (!noninteractive
8924 && INTERACTIVE
8925 && !NILP (echo_area_buffer[0]))
8926 {
8927 struct frame *sf = SELECTED_FRAME ();
8928 if (FRAME_MESSAGE_BUF (sf))
8929 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8930 }
8931 }
8932
8933
8934 /* Helper function for truncate_echo_area. Truncate the current
8935 message to at most NCHARS characters. */
8936
8937 static int
8938 truncate_message_1 (nchars, a2, a3, a4)
8939 EMACS_INT nchars;
8940 Lisp_Object a2;
8941 EMACS_INT a3, a4;
8942 {
8943 if (BEG + nchars < Z)
8944 del_range (BEG + nchars, Z);
8945 if (Z == BEG)
8946 echo_area_buffer[0] = Qnil;
8947 return 0;
8948 }
8949
8950
8951 /* Set the current message to a substring of S or STRING.
8952
8953 If STRING is a Lisp string, set the message to the first NBYTES
8954 bytes from STRING. NBYTES zero means use the whole string. If
8955 STRING is multibyte, the message will be displayed multibyte.
8956
8957 If S is not null, set the message to the first LEN bytes of S. LEN
8958 zero means use the whole string. MULTIBYTE_P non-zero means S is
8959 multibyte. Display the message multibyte in that case.
8960
8961 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8962 to t before calling set_message_1 (which calls insert).
8963 */
8964
8965 void
8966 set_message (s, string, nbytes, multibyte_p)
8967 const char *s;
8968 Lisp_Object string;
8969 int nbytes, multibyte_p;
8970 {
8971 message_enable_multibyte
8972 = ((s && multibyte_p)
8973 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8974
8975 with_echo_area_buffer (0, -1, set_message_1,
8976 (EMACS_INT) s, string, nbytes, multibyte_p);
8977 message_buf_print = 0;
8978 help_echo_showing_p = 0;
8979 }
8980
8981
8982 /* Helper function for set_message. Arguments have the same meaning
8983 as there, with A1 corresponding to S and A2 corresponding to STRING
8984 This function is called with the echo area buffer being
8985 current. */
8986
8987 static int
8988 set_message_1 (a1, a2, nbytes, multibyte_p)
8989 EMACS_INT a1;
8990 Lisp_Object a2;
8991 EMACS_INT nbytes, multibyte_p;
8992 {
8993 const char *s = (const char *) a1;
8994 Lisp_Object string = a2;
8995
8996 /* Change multibyteness of the echo buffer appropriately. */
8997 if (message_enable_multibyte
8998 != !NILP (current_buffer->enable_multibyte_characters))
8999 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9000
9001 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9002
9003 /* Insert new message at BEG. */
9004 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9005
9006 if (STRINGP (string))
9007 {
9008 int nchars;
9009
9010 if (nbytes == 0)
9011 nbytes = SBYTES (string);
9012 nchars = string_byte_to_char (string, nbytes);
9013
9014 /* This function takes care of single/multibyte conversion. We
9015 just have to ensure that the echo area buffer has the right
9016 setting of enable_multibyte_characters. */
9017 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9018 }
9019 else if (s)
9020 {
9021 if (nbytes == 0)
9022 nbytes = strlen (s);
9023
9024 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9025 {
9026 /* Convert from multi-byte to single-byte. */
9027 int i, c, n;
9028 unsigned char work[1];
9029
9030 /* Convert a multibyte string to single-byte. */
9031 for (i = 0; i < nbytes; i += n)
9032 {
9033 c = string_char_and_length (s + i, &n);
9034 work[0] = (ASCII_CHAR_P (c)
9035 ? c
9036 : multibyte_char_to_unibyte (c, Qnil));
9037 insert_1_both (work, 1, 1, 1, 0, 0);
9038 }
9039 }
9040 else if (!multibyte_p
9041 && !NILP (current_buffer->enable_multibyte_characters))
9042 {
9043 /* Convert from single-byte to multi-byte. */
9044 int i, c, n;
9045 const unsigned char *msg = (const unsigned char *) s;
9046 unsigned char str[MAX_MULTIBYTE_LENGTH];
9047
9048 /* Convert a single-byte string to multibyte. */
9049 for (i = 0; i < nbytes; i++)
9050 {
9051 c = msg[i];
9052 MAKE_CHAR_MULTIBYTE (c);
9053 n = CHAR_STRING (c, str);
9054 insert_1_both (str, 1, n, 1, 0, 0);
9055 }
9056 }
9057 else
9058 insert_1 (s, nbytes, 1, 0, 0);
9059 }
9060
9061 return 0;
9062 }
9063
9064
9065 /* Clear messages. CURRENT_P non-zero means clear the current
9066 message. LAST_DISPLAYED_P non-zero means clear the message
9067 last displayed. */
9068
9069 void
9070 clear_message (current_p, last_displayed_p)
9071 int current_p, last_displayed_p;
9072 {
9073 if (current_p)
9074 {
9075 echo_area_buffer[0] = Qnil;
9076 message_cleared_p = 1;
9077 }
9078
9079 if (last_displayed_p)
9080 echo_area_buffer[1] = Qnil;
9081
9082 message_buf_print = 0;
9083 }
9084
9085 /* Clear garbaged frames.
9086
9087 This function is used where the old redisplay called
9088 redraw_garbaged_frames which in turn called redraw_frame which in
9089 turn called clear_frame. The call to clear_frame was a source of
9090 flickering. I believe a clear_frame is not necessary. It should
9091 suffice in the new redisplay to invalidate all current matrices,
9092 and ensure a complete redisplay of all windows. */
9093
9094 static void
9095 clear_garbaged_frames ()
9096 {
9097 if (frame_garbaged)
9098 {
9099 Lisp_Object tail, frame;
9100 int changed_count = 0;
9101
9102 FOR_EACH_FRAME (tail, frame)
9103 {
9104 struct frame *f = XFRAME (frame);
9105
9106 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9107 {
9108 if (f->resized_p)
9109 {
9110 Fredraw_frame (frame);
9111 f->force_flush_display_p = 1;
9112 }
9113 clear_current_matrices (f);
9114 changed_count++;
9115 f->garbaged = 0;
9116 f->resized_p = 0;
9117 }
9118 }
9119
9120 frame_garbaged = 0;
9121 if (changed_count)
9122 ++windows_or_buffers_changed;
9123 }
9124 }
9125
9126
9127 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9128 is non-zero update selected_frame. Value is non-zero if the
9129 mini-windows height has been changed. */
9130
9131 static int
9132 echo_area_display (update_frame_p)
9133 int update_frame_p;
9134 {
9135 Lisp_Object mini_window;
9136 struct window *w;
9137 struct frame *f;
9138 int window_height_changed_p = 0;
9139 struct frame *sf = SELECTED_FRAME ();
9140
9141 mini_window = FRAME_MINIBUF_WINDOW (sf);
9142 w = XWINDOW (mini_window);
9143 f = XFRAME (WINDOW_FRAME (w));
9144
9145 /* Don't display if frame is invisible or not yet initialized. */
9146 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9147 return 0;
9148
9149 #ifdef HAVE_WINDOW_SYSTEM
9150 /* When Emacs starts, selected_frame may be the initial terminal
9151 frame. If we let this through, a message would be displayed on
9152 the terminal. */
9153 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9154 return 0;
9155 #endif /* HAVE_WINDOW_SYSTEM */
9156
9157 /* Redraw garbaged frames. */
9158 if (frame_garbaged)
9159 clear_garbaged_frames ();
9160
9161 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9162 {
9163 echo_area_window = mini_window;
9164 window_height_changed_p = display_echo_area (w);
9165 w->must_be_updated_p = 1;
9166
9167 /* Update the display, unless called from redisplay_internal.
9168 Also don't update the screen during redisplay itself. The
9169 update will happen at the end of redisplay, and an update
9170 here could cause confusion. */
9171 if (update_frame_p && !redisplaying_p)
9172 {
9173 int n = 0;
9174
9175 /* If the display update has been interrupted by pending
9176 input, update mode lines in the frame. Due to the
9177 pending input, it might have been that redisplay hasn't
9178 been called, so that mode lines above the echo area are
9179 garbaged. This looks odd, so we prevent it here. */
9180 if (!display_completed)
9181 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9182
9183 if (window_height_changed_p
9184 /* Don't do this if Emacs is shutting down. Redisplay
9185 needs to run hooks. */
9186 && !NILP (Vrun_hooks))
9187 {
9188 /* Must update other windows. Likewise as in other
9189 cases, don't let this update be interrupted by
9190 pending input. */
9191 int count = SPECPDL_INDEX ();
9192 specbind (Qredisplay_dont_pause, Qt);
9193 windows_or_buffers_changed = 1;
9194 redisplay_internal (0);
9195 unbind_to (count, Qnil);
9196 }
9197 else if (FRAME_WINDOW_P (f) && n == 0)
9198 {
9199 /* Window configuration is the same as before.
9200 Can do with a display update of the echo area,
9201 unless we displayed some mode lines. */
9202 update_single_window (w, 1);
9203 FRAME_RIF (f)->flush_display (f);
9204 }
9205 else
9206 update_frame (f, 1, 1);
9207
9208 /* If cursor is in the echo area, make sure that the next
9209 redisplay displays the minibuffer, so that the cursor will
9210 be replaced with what the minibuffer wants. */
9211 if (cursor_in_echo_area)
9212 ++windows_or_buffers_changed;
9213 }
9214 }
9215 else if (!EQ (mini_window, selected_window))
9216 windows_or_buffers_changed++;
9217
9218 /* Last displayed message is now the current message. */
9219 echo_area_buffer[1] = echo_area_buffer[0];
9220 /* Inform read_char that we're not echoing. */
9221 echo_message_buffer = Qnil;
9222
9223 /* Prevent redisplay optimization in redisplay_internal by resetting
9224 this_line_start_pos. This is done because the mini-buffer now
9225 displays the message instead of its buffer text. */
9226 if (EQ (mini_window, selected_window))
9227 CHARPOS (this_line_start_pos) = 0;
9228
9229 return window_height_changed_p;
9230 }
9231
9232
9233 \f
9234 /***********************************************************************
9235 Mode Lines and Frame Titles
9236 ***********************************************************************/
9237
9238 /* A buffer for constructing non-propertized mode-line strings and
9239 frame titles in it; allocated from the heap in init_xdisp and
9240 resized as needed in store_mode_line_noprop_char. */
9241
9242 static char *mode_line_noprop_buf;
9243
9244 /* The buffer's end, and a current output position in it. */
9245
9246 static char *mode_line_noprop_buf_end;
9247 static char *mode_line_noprop_ptr;
9248
9249 #define MODE_LINE_NOPROP_LEN(start) \
9250 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9251
9252 static enum {
9253 MODE_LINE_DISPLAY = 0,
9254 MODE_LINE_TITLE,
9255 MODE_LINE_NOPROP,
9256 MODE_LINE_STRING
9257 } mode_line_target;
9258
9259 /* Alist that caches the results of :propertize.
9260 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9261 static Lisp_Object mode_line_proptrans_alist;
9262
9263 /* List of strings making up the mode-line. */
9264 static Lisp_Object mode_line_string_list;
9265
9266 /* Base face property when building propertized mode line string. */
9267 static Lisp_Object mode_line_string_face;
9268 static Lisp_Object mode_line_string_face_prop;
9269
9270
9271 /* Unwind data for mode line strings */
9272
9273 static Lisp_Object Vmode_line_unwind_vector;
9274
9275 static Lisp_Object
9276 format_mode_line_unwind_data (struct buffer *obuf,
9277 Lisp_Object owin,
9278 int save_proptrans)
9279 {
9280 Lisp_Object vector, tmp;
9281
9282 /* Reduce consing by keeping one vector in
9283 Vwith_echo_area_save_vector. */
9284 vector = Vmode_line_unwind_vector;
9285 Vmode_line_unwind_vector = Qnil;
9286
9287 if (NILP (vector))
9288 vector = Fmake_vector (make_number (8), Qnil);
9289
9290 ASET (vector, 0, make_number (mode_line_target));
9291 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9292 ASET (vector, 2, mode_line_string_list);
9293 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9294 ASET (vector, 4, mode_line_string_face);
9295 ASET (vector, 5, mode_line_string_face_prop);
9296
9297 if (obuf)
9298 XSETBUFFER (tmp, obuf);
9299 else
9300 tmp = Qnil;
9301 ASET (vector, 6, tmp);
9302 ASET (vector, 7, owin);
9303
9304 return vector;
9305 }
9306
9307 static Lisp_Object
9308 unwind_format_mode_line (vector)
9309 Lisp_Object vector;
9310 {
9311 mode_line_target = XINT (AREF (vector, 0));
9312 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9313 mode_line_string_list = AREF (vector, 2);
9314 if (! EQ (AREF (vector, 3), Qt))
9315 mode_line_proptrans_alist = AREF (vector, 3);
9316 mode_line_string_face = AREF (vector, 4);
9317 mode_line_string_face_prop = AREF (vector, 5);
9318
9319 if (!NILP (AREF (vector, 7)))
9320 /* Select window before buffer, since it may change the buffer. */
9321 Fselect_window (AREF (vector, 7), Qt);
9322
9323 if (!NILP (AREF (vector, 6)))
9324 {
9325 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9326 ASET (vector, 6, Qnil);
9327 }
9328
9329 Vmode_line_unwind_vector = vector;
9330 return Qnil;
9331 }
9332
9333
9334 /* Store a single character C for the frame title in mode_line_noprop_buf.
9335 Re-allocate mode_line_noprop_buf if necessary. */
9336
9337 static void
9338 #ifdef PROTOTYPES
9339 store_mode_line_noprop_char (char c)
9340 #else
9341 store_mode_line_noprop_char (c)
9342 char c;
9343 #endif
9344 {
9345 /* If output position has reached the end of the allocated buffer,
9346 double the buffer's size. */
9347 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9348 {
9349 int len = MODE_LINE_NOPROP_LEN (0);
9350 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9351 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9352 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9353 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9354 }
9355
9356 *mode_line_noprop_ptr++ = c;
9357 }
9358
9359
9360 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9361 mode_line_noprop_ptr. STR is the string to store. Do not copy
9362 characters that yield more columns than PRECISION; PRECISION <= 0
9363 means copy the whole string. Pad with spaces until FIELD_WIDTH
9364 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9365 pad. Called from display_mode_element when it is used to build a
9366 frame title. */
9367
9368 static int
9369 store_mode_line_noprop (str, field_width, precision)
9370 const unsigned char *str;
9371 int field_width, precision;
9372 {
9373 int n = 0;
9374 int dummy, nbytes;
9375
9376 /* Copy at most PRECISION chars from STR. */
9377 nbytes = strlen (str);
9378 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9379 while (nbytes--)
9380 store_mode_line_noprop_char (*str++);
9381
9382 /* Fill up with spaces until FIELD_WIDTH reached. */
9383 while (field_width > 0
9384 && n < field_width)
9385 {
9386 store_mode_line_noprop_char (' ');
9387 ++n;
9388 }
9389
9390 return n;
9391 }
9392
9393 /***********************************************************************
9394 Frame Titles
9395 ***********************************************************************/
9396
9397 #ifdef HAVE_WINDOW_SYSTEM
9398
9399 /* Set the title of FRAME, if it has changed. The title format is
9400 Vicon_title_format if FRAME is iconified, otherwise it is
9401 frame_title_format. */
9402
9403 static void
9404 x_consider_frame_title (frame)
9405 Lisp_Object frame;
9406 {
9407 struct frame *f = XFRAME (frame);
9408
9409 if (FRAME_WINDOW_P (f)
9410 || FRAME_MINIBUF_ONLY_P (f)
9411 || f->explicit_name)
9412 {
9413 /* Do we have more than one visible frame on this X display? */
9414 Lisp_Object tail;
9415 Lisp_Object fmt;
9416 int title_start;
9417 char *title;
9418 int len;
9419 struct it it;
9420 int count = SPECPDL_INDEX ();
9421
9422 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9423 {
9424 Lisp_Object other_frame = XCAR (tail);
9425 struct frame *tf = XFRAME (other_frame);
9426
9427 if (tf != f
9428 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9429 && !FRAME_MINIBUF_ONLY_P (tf)
9430 && !EQ (other_frame, tip_frame)
9431 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9432 break;
9433 }
9434
9435 /* Set global variable indicating that multiple frames exist. */
9436 multiple_frames = CONSP (tail);
9437
9438 /* Switch to the buffer of selected window of the frame. Set up
9439 mode_line_target so that display_mode_element will output into
9440 mode_line_noprop_buf; then display the title. */
9441 record_unwind_protect (unwind_format_mode_line,
9442 format_mode_line_unwind_data
9443 (current_buffer, selected_window, 0));
9444
9445 Fselect_window (f->selected_window, Qt);
9446 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9447 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9448
9449 mode_line_target = MODE_LINE_TITLE;
9450 title_start = MODE_LINE_NOPROP_LEN (0);
9451 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9452 NULL, DEFAULT_FACE_ID);
9453 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9454 len = MODE_LINE_NOPROP_LEN (title_start);
9455 title = mode_line_noprop_buf + title_start;
9456 unbind_to (count, Qnil);
9457
9458 /* Set the title only if it's changed. This avoids consing in
9459 the common case where it hasn't. (If it turns out that we've
9460 already wasted too much time by walking through the list with
9461 display_mode_element, then we might need to optimize at a
9462 higher level than this.) */
9463 if (! STRINGP (f->name)
9464 || SBYTES (f->name) != len
9465 || bcmp (title, SDATA (f->name), len) != 0)
9466 x_implicitly_set_name (f, make_string (title, len), Qnil);
9467 }
9468 }
9469
9470 #endif /* not HAVE_WINDOW_SYSTEM */
9471
9472
9473
9474 \f
9475 /***********************************************************************
9476 Menu Bars
9477 ***********************************************************************/
9478
9479
9480 /* Prepare for redisplay by updating menu-bar item lists when
9481 appropriate. This can call eval. */
9482
9483 void
9484 prepare_menu_bars ()
9485 {
9486 int all_windows;
9487 struct gcpro gcpro1, gcpro2;
9488 struct frame *f;
9489 Lisp_Object tooltip_frame;
9490
9491 #ifdef HAVE_WINDOW_SYSTEM
9492 tooltip_frame = tip_frame;
9493 #else
9494 tooltip_frame = Qnil;
9495 #endif
9496
9497 /* Update all frame titles based on their buffer names, etc. We do
9498 this before the menu bars so that the buffer-menu will show the
9499 up-to-date frame titles. */
9500 #ifdef HAVE_WINDOW_SYSTEM
9501 if (windows_or_buffers_changed || update_mode_lines)
9502 {
9503 Lisp_Object tail, frame;
9504
9505 FOR_EACH_FRAME (tail, frame)
9506 {
9507 f = XFRAME (frame);
9508 if (!EQ (frame, tooltip_frame)
9509 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9510 x_consider_frame_title (frame);
9511 }
9512 }
9513 #endif /* HAVE_WINDOW_SYSTEM */
9514
9515 /* Update the menu bar item lists, if appropriate. This has to be
9516 done before any actual redisplay or generation of display lines. */
9517 all_windows = (update_mode_lines
9518 || buffer_shared > 1
9519 || windows_or_buffers_changed);
9520 if (all_windows)
9521 {
9522 Lisp_Object tail, frame;
9523 int count = SPECPDL_INDEX ();
9524 /* 1 means that update_menu_bar has run its hooks
9525 so any further calls to update_menu_bar shouldn't do so again. */
9526 int menu_bar_hooks_run = 0;
9527
9528 record_unwind_save_match_data ();
9529
9530 FOR_EACH_FRAME (tail, frame)
9531 {
9532 f = XFRAME (frame);
9533
9534 /* Ignore tooltip frame. */
9535 if (EQ (frame, tooltip_frame))
9536 continue;
9537
9538 /* If a window on this frame changed size, report that to
9539 the user and clear the size-change flag. */
9540 if (FRAME_WINDOW_SIZES_CHANGED (f))
9541 {
9542 Lisp_Object functions;
9543
9544 /* Clear flag first in case we get an error below. */
9545 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9546 functions = Vwindow_size_change_functions;
9547 GCPRO2 (tail, functions);
9548
9549 while (CONSP (functions))
9550 {
9551 if (!EQ (XCAR (functions), Qt))
9552 call1 (XCAR (functions), frame);
9553 functions = XCDR (functions);
9554 }
9555 UNGCPRO;
9556 }
9557
9558 GCPRO1 (tail);
9559 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9560 #ifdef HAVE_WINDOW_SYSTEM
9561 update_tool_bar (f, 0);
9562 #endif
9563 #ifdef HAVE_NS
9564 if (windows_or_buffers_changed
9565 && FRAME_NS_P (f))
9566 ns_set_doc_edited (f, Fbuffer_modified_p
9567 (XWINDOW (f->selected_window)->buffer));
9568 #endif
9569 UNGCPRO;
9570 }
9571
9572 unbind_to (count, Qnil);
9573 }
9574 else
9575 {
9576 struct frame *sf = SELECTED_FRAME ();
9577 update_menu_bar (sf, 1, 0);
9578 #ifdef HAVE_WINDOW_SYSTEM
9579 update_tool_bar (sf, 1);
9580 #endif
9581 }
9582
9583 /* Motif needs this. See comment in xmenu.c. Turn it off when
9584 pending_menu_activation is not defined. */
9585 #ifdef USE_X_TOOLKIT
9586 pending_menu_activation = 0;
9587 #endif
9588 }
9589
9590
9591 /* Update the menu bar item list for frame F. This has to be done
9592 before we start to fill in any display lines, because it can call
9593 eval.
9594
9595 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9596
9597 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9598 already ran the menu bar hooks for this redisplay, so there
9599 is no need to run them again. The return value is the
9600 updated value of this flag, to pass to the next call. */
9601
9602 static int
9603 update_menu_bar (f, save_match_data, hooks_run)
9604 struct frame *f;
9605 int save_match_data;
9606 int hooks_run;
9607 {
9608 Lisp_Object window;
9609 register struct window *w;
9610
9611 /* If called recursively during a menu update, do nothing. This can
9612 happen when, for instance, an activate-menubar-hook causes a
9613 redisplay. */
9614 if (inhibit_menubar_update)
9615 return hooks_run;
9616
9617 window = FRAME_SELECTED_WINDOW (f);
9618 w = XWINDOW (window);
9619
9620 if (FRAME_WINDOW_P (f)
9621 ?
9622 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9623 || defined (HAVE_NS) || defined (USE_GTK)
9624 FRAME_EXTERNAL_MENU_BAR (f)
9625 #else
9626 FRAME_MENU_BAR_LINES (f) > 0
9627 #endif
9628 : FRAME_MENU_BAR_LINES (f) > 0)
9629 {
9630 /* If the user has switched buffers or windows, we need to
9631 recompute to reflect the new bindings. But we'll
9632 recompute when update_mode_lines is set too; that means
9633 that people can use force-mode-line-update to request
9634 that the menu bar be recomputed. The adverse effect on
9635 the rest of the redisplay algorithm is about the same as
9636 windows_or_buffers_changed anyway. */
9637 if (windows_or_buffers_changed
9638 /* This used to test w->update_mode_line, but we believe
9639 there is no need to recompute the menu in that case. */
9640 || update_mode_lines
9641 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9642 < BUF_MODIFF (XBUFFER (w->buffer)))
9643 != !NILP (w->last_had_star))
9644 || ((!NILP (Vtransient_mark_mode)
9645 && !NILP (XBUFFER (w->buffer)->mark_active))
9646 != !NILP (w->region_showing)))
9647 {
9648 struct buffer *prev = current_buffer;
9649 int count = SPECPDL_INDEX ();
9650
9651 specbind (Qinhibit_menubar_update, Qt);
9652
9653 set_buffer_internal_1 (XBUFFER (w->buffer));
9654 if (save_match_data)
9655 record_unwind_save_match_data ();
9656 if (NILP (Voverriding_local_map_menu_flag))
9657 {
9658 specbind (Qoverriding_terminal_local_map, Qnil);
9659 specbind (Qoverriding_local_map, Qnil);
9660 }
9661
9662 if (!hooks_run)
9663 {
9664 /* Run the Lucid hook. */
9665 safe_run_hooks (Qactivate_menubar_hook);
9666
9667 /* If it has changed current-menubar from previous value,
9668 really recompute the menu-bar from the value. */
9669 if (! NILP (Vlucid_menu_bar_dirty_flag))
9670 call0 (Qrecompute_lucid_menubar);
9671
9672 safe_run_hooks (Qmenu_bar_update_hook);
9673
9674 hooks_run = 1;
9675 }
9676
9677 XSETFRAME (Vmenu_updating_frame, f);
9678 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9679
9680 /* Redisplay the menu bar in case we changed it. */
9681 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9682 || defined (HAVE_NS) || defined (USE_GTK)
9683 if (FRAME_WINDOW_P (f))
9684 {
9685 #if defined (HAVE_NS)
9686 /* All frames on Mac OS share the same menubar. So only
9687 the selected frame should be allowed to set it. */
9688 if (f == SELECTED_FRAME ())
9689 #endif
9690 set_frame_menubar (f, 0, 0);
9691 }
9692 else
9693 /* On a terminal screen, the menu bar is an ordinary screen
9694 line, and this makes it get updated. */
9695 w->update_mode_line = Qt;
9696 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9697 /* In the non-toolkit version, the menu bar is an ordinary screen
9698 line, and this makes it get updated. */
9699 w->update_mode_line = Qt;
9700 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9701
9702 unbind_to (count, Qnil);
9703 set_buffer_internal_1 (prev);
9704 }
9705 }
9706
9707 return hooks_run;
9708 }
9709
9710
9711 \f
9712 /***********************************************************************
9713 Output Cursor
9714 ***********************************************************************/
9715
9716 #ifdef HAVE_WINDOW_SYSTEM
9717
9718 /* EXPORT:
9719 Nominal cursor position -- where to draw output.
9720 HPOS and VPOS are window relative glyph matrix coordinates.
9721 X and Y are window relative pixel coordinates. */
9722
9723 struct cursor_pos output_cursor;
9724
9725
9726 /* EXPORT:
9727 Set the global variable output_cursor to CURSOR. All cursor
9728 positions are relative to updated_window. */
9729
9730 void
9731 set_output_cursor (cursor)
9732 struct cursor_pos *cursor;
9733 {
9734 output_cursor.hpos = cursor->hpos;
9735 output_cursor.vpos = cursor->vpos;
9736 output_cursor.x = cursor->x;
9737 output_cursor.y = cursor->y;
9738 }
9739
9740
9741 /* EXPORT for RIF:
9742 Set a nominal cursor position.
9743
9744 HPOS and VPOS are column/row positions in a window glyph matrix. X
9745 and Y are window text area relative pixel positions.
9746
9747 If this is done during an update, updated_window will contain the
9748 window that is being updated and the position is the future output
9749 cursor position for that window. If updated_window is null, use
9750 selected_window and display the cursor at the given position. */
9751
9752 void
9753 x_cursor_to (vpos, hpos, y, x)
9754 int vpos, hpos, y, x;
9755 {
9756 struct window *w;
9757
9758 /* If updated_window is not set, work on selected_window. */
9759 if (updated_window)
9760 w = updated_window;
9761 else
9762 w = XWINDOW (selected_window);
9763
9764 /* Set the output cursor. */
9765 output_cursor.hpos = hpos;
9766 output_cursor.vpos = vpos;
9767 output_cursor.x = x;
9768 output_cursor.y = y;
9769
9770 /* If not called as part of an update, really display the cursor.
9771 This will also set the cursor position of W. */
9772 if (updated_window == NULL)
9773 {
9774 BLOCK_INPUT;
9775 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9776 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9777 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9778 UNBLOCK_INPUT;
9779 }
9780 }
9781
9782 #endif /* HAVE_WINDOW_SYSTEM */
9783
9784 \f
9785 /***********************************************************************
9786 Tool-bars
9787 ***********************************************************************/
9788
9789 #ifdef HAVE_WINDOW_SYSTEM
9790
9791 /* Where the mouse was last time we reported a mouse event. */
9792
9793 FRAME_PTR last_mouse_frame;
9794
9795 /* Tool-bar item index of the item on which a mouse button was pressed
9796 or -1. */
9797
9798 int last_tool_bar_item;
9799
9800
9801 static Lisp_Object
9802 update_tool_bar_unwind (frame)
9803 Lisp_Object frame;
9804 {
9805 selected_frame = frame;
9806 return Qnil;
9807 }
9808
9809 /* Update the tool-bar item list for frame F. This has to be done
9810 before we start to fill in any display lines. Called from
9811 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9812 and restore it here. */
9813
9814 static void
9815 update_tool_bar (f, save_match_data)
9816 struct frame *f;
9817 int save_match_data;
9818 {
9819 #if defined (USE_GTK) || defined (HAVE_NS)
9820 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9821 #else
9822 int do_update = WINDOWP (f->tool_bar_window)
9823 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9824 #endif
9825
9826 if (do_update)
9827 {
9828 Lisp_Object window;
9829 struct window *w;
9830
9831 window = FRAME_SELECTED_WINDOW (f);
9832 w = XWINDOW (window);
9833
9834 /* If the user has switched buffers or windows, we need to
9835 recompute to reflect the new bindings. But we'll
9836 recompute when update_mode_lines is set too; that means
9837 that people can use force-mode-line-update to request
9838 that the menu bar be recomputed. The adverse effect on
9839 the rest of the redisplay algorithm is about the same as
9840 windows_or_buffers_changed anyway. */
9841 if (windows_or_buffers_changed
9842 || !NILP (w->update_mode_line)
9843 || update_mode_lines
9844 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9845 < BUF_MODIFF (XBUFFER (w->buffer)))
9846 != !NILP (w->last_had_star))
9847 || ((!NILP (Vtransient_mark_mode)
9848 && !NILP (XBUFFER (w->buffer)->mark_active))
9849 != !NILP (w->region_showing)))
9850 {
9851 struct buffer *prev = current_buffer;
9852 int count = SPECPDL_INDEX ();
9853 Lisp_Object frame, new_tool_bar;
9854 int new_n_tool_bar;
9855 struct gcpro gcpro1;
9856
9857 /* Set current_buffer to the buffer of the selected
9858 window of the frame, so that we get the right local
9859 keymaps. */
9860 set_buffer_internal_1 (XBUFFER (w->buffer));
9861
9862 /* Save match data, if we must. */
9863 if (save_match_data)
9864 record_unwind_save_match_data ();
9865
9866 /* Make sure that we don't accidentally use bogus keymaps. */
9867 if (NILP (Voverriding_local_map_menu_flag))
9868 {
9869 specbind (Qoverriding_terminal_local_map, Qnil);
9870 specbind (Qoverriding_local_map, Qnil);
9871 }
9872
9873 GCPRO1 (new_tool_bar);
9874
9875 /* We must temporarily set the selected frame to this frame
9876 before calling tool_bar_items, because the calculation of
9877 the tool-bar keymap uses the selected frame (see
9878 `tool-bar-make-keymap' in tool-bar.el). */
9879 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9880 XSETFRAME (frame, f);
9881 selected_frame = frame;
9882
9883 /* Build desired tool-bar items from keymaps. */
9884 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9885 &new_n_tool_bar);
9886
9887 /* Redisplay the tool-bar if we changed it. */
9888 if (new_n_tool_bar != f->n_tool_bar_items
9889 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9890 {
9891 /* Redisplay that happens asynchronously due to an expose event
9892 may access f->tool_bar_items. Make sure we update both
9893 variables within BLOCK_INPUT so no such event interrupts. */
9894 BLOCK_INPUT;
9895 f->tool_bar_items = new_tool_bar;
9896 f->n_tool_bar_items = new_n_tool_bar;
9897 w->update_mode_line = Qt;
9898 UNBLOCK_INPUT;
9899 }
9900
9901 UNGCPRO;
9902
9903 unbind_to (count, Qnil);
9904 set_buffer_internal_1 (prev);
9905 }
9906 }
9907 }
9908
9909
9910 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9911 F's desired tool-bar contents. F->tool_bar_items must have
9912 been set up previously by calling prepare_menu_bars. */
9913
9914 static void
9915 build_desired_tool_bar_string (f)
9916 struct frame *f;
9917 {
9918 int i, size, size_needed;
9919 struct gcpro gcpro1, gcpro2, gcpro3;
9920 Lisp_Object image, plist, props;
9921
9922 image = plist = props = Qnil;
9923 GCPRO3 (image, plist, props);
9924
9925 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9926 Otherwise, make a new string. */
9927
9928 /* The size of the string we might be able to reuse. */
9929 size = (STRINGP (f->desired_tool_bar_string)
9930 ? SCHARS (f->desired_tool_bar_string)
9931 : 0);
9932
9933 /* We need one space in the string for each image. */
9934 size_needed = f->n_tool_bar_items;
9935
9936 /* Reuse f->desired_tool_bar_string, if possible. */
9937 if (size < size_needed || NILP (f->desired_tool_bar_string))
9938 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9939 make_number (' '));
9940 else
9941 {
9942 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9943 Fremove_text_properties (make_number (0), make_number (size),
9944 props, f->desired_tool_bar_string);
9945 }
9946
9947 /* Put a `display' property on the string for the images to display,
9948 put a `menu_item' property on tool-bar items with a value that
9949 is the index of the item in F's tool-bar item vector. */
9950 for (i = 0; i < f->n_tool_bar_items; ++i)
9951 {
9952 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9953
9954 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9955 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9956 int hmargin, vmargin, relief, idx, end;
9957 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9958
9959 /* If image is a vector, choose the image according to the
9960 button state. */
9961 image = PROP (TOOL_BAR_ITEM_IMAGES);
9962 if (VECTORP (image))
9963 {
9964 if (enabled_p)
9965 idx = (selected_p
9966 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9967 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9968 else
9969 idx = (selected_p
9970 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9971 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9972
9973 xassert (ASIZE (image) >= idx);
9974 image = AREF (image, idx);
9975 }
9976 else
9977 idx = -1;
9978
9979 /* Ignore invalid image specifications. */
9980 if (!valid_image_p (image))
9981 continue;
9982
9983 /* Display the tool-bar button pressed, or depressed. */
9984 plist = Fcopy_sequence (XCDR (image));
9985
9986 /* Compute margin and relief to draw. */
9987 relief = (tool_bar_button_relief >= 0
9988 ? tool_bar_button_relief
9989 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9990 hmargin = vmargin = relief;
9991
9992 if (INTEGERP (Vtool_bar_button_margin)
9993 && XINT (Vtool_bar_button_margin) > 0)
9994 {
9995 hmargin += XFASTINT (Vtool_bar_button_margin);
9996 vmargin += XFASTINT (Vtool_bar_button_margin);
9997 }
9998 else if (CONSP (Vtool_bar_button_margin))
9999 {
10000 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10001 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10002 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10003
10004 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10005 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10006 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10007 }
10008
10009 if (auto_raise_tool_bar_buttons_p)
10010 {
10011 /* Add a `:relief' property to the image spec if the item is
10012 selected. */
10013 if (selected_p)
10014 {
10015 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10016 hmargin -= relief;
10017 vmargin -= relief;
10018 }
10019 }
10020 else
10021 {
10022 /* If image is selected, display it pressed, i.e. with a
10023 negative relief. If it's not selected, display it with a
10024 raised relief. */
10025 plist = Fplist_put (plist, QCrelief,
10026 (selected_p
10027 ? make_number (-relief)
10028 : make_number (relief)));
10029 hmargin -= relief;
10030 vmargin -= relief;
10031 }
10032
10033 /* Put a margin around the image. */
10034 if (hmargin || vmargin)
10035 {
10036 if (hmargin == vmargin)
10037 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10038 else
10039 plist = Fplist_put (plist, QCmargin,
10040 Fcons (make_number (hmargin),
10041 make_number (vmargin)));
10042 }
10043
10044 /* If button is not enabled, and we don't have special images
10045 for the disabled state, make the image appear disabled by
10046 applying an appropriate algorithm to it. */
10047 if (!enabled_p && idx < 0)
10048 plist = Fplist_put (plist, QCconversion, Qdisabled);
10049
10050 /* Put a `display' text property on the string for the image to
10051 display. Put a `menu-item' property on the string that gives
10052 the start of this item's properties in the tool-bar items
10053 vector. */
10054 image = Fcons (Qimage, plist);
10055 props = list4 (Qdisplay, image,
10056 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10057
10058 /* Let the last image hide all remaining spaces in the tool bar
10059 string. The string can be longer than needed when we reuse a
10060 previous string. */
10061 if (i + 1 == f->n_tool_bar_items)
10062 end = SCHARS (f->desired_tool_bar_string);
10063 else
10064 end = i + 1;
10065 Fadd_text_properties (make_number (i), make_number (end),
10066 props, f->desired_tool_bar_string);
10067 #undef PROP
10068 }
10069
10070 UNGCPRO;
10071 }
10072
10073
10074 /* Display one line of the tool-bar of frame IT->f.
10075
10076 HEIGHT specifies the desired height of the tool-bar line.
10077 If the actual height of the glyph row is less than HEIGHT, the
10078 row's height is increased to HEIGHT, and the icons are centered
10079 vertically in the new height.
10080
10081 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10082 count a final empty row in case the tool-bar width exactly matches
10083 the window width.
10084 */
10085
10086 static void
10087 display_tool_bar_line (it, height)
10088 struct it *it;
10089 int height;
10090 {
10091 struct glyph_row *row = it->glyph_row;
10092 int max_x = it->last_visible_x;
10093 struct glyph *last;
10094
10095 prepare_desired_row (row);
10096 row->y = it->current_y;
10097
10098 /* Note that this isn't made use of if the face hasn't a box,
10099 so there's no need to check the face here. */
10100 it->start_of_box_run_p = 1;
10101
10102 while (it->current_x < max_x)
10103 {
10104 int x, n_glyphs_before, i, nglyphs;
10105 struct it it_before;
10106
10107 /* Get the next display element. */
10108 if (!get_next_display_element (it))
10109 {
10110 /* Don't count empty row if we are counting needed tool-bar lines. */
10111 if (height < 0 && !it->hpos)
10112 return;
10113 break;
10114 }
10115
10116 /* Produce glyphs. */
10117 n_glyphs_before = row->used[TEXT_AREA];
10118 it_before = *it;
10119
10120 PRODUCE_GLYPHS (it);
10121
10122 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10123 i = 0;
10124 x = it_before.current_x;
10125 while (i < nglyphs)
10126 {
10127 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10128
10129 if (x + glyph->pixel_width > max_x)
10130 {
10131 /* Glyph doesn't fit on line. Backtrack. */
10132 row->used[TEXT_AREA] = n_glyphs_before;
10133 *it = it_before;
10134 /* If this is the only glyph on this line, it will never fit on the
10135 toolbar, so skip it. But ensure there is at least one glyph,
10136 so we don't accidentally disable the tool-bar. */
10137 if (n_glyphs_before == 0
10138 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10139 break;
10140 goto out;
10141 }
10142
10143 ++it->hpos;
10144 x += glyph->pixel_width;
10145 ++i;
10146 }
10147
10148 /* Stop at line ends. */
10149 if (ITERATOR_AT_END_OF_LINE_P (it))
10150 break;
10151
10152 set_iterator_to_next (it, 1);
10153 }
10154
10155 out:;
10156
10157 row->displays_text_p = row->used[TEXT_AREA] != 0;
10158
10159 /* Use default face for the border below the tool bar.
10160
10161 FIXME: When auto-resize-tool-bars is grow-only, there is
10162 no additional border below the possibly empty tool-bar lines.
10163 So to make the extra empty lines look "normal", we have to
10164 use the tool-bar face for the border too. */
10165 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10166 it->face_id = DEFAULT_FACE_ID;
10167
10168 extend_face_to_end_of_line (it);
10169 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10170 last->right_box_line_p = 1;
10171 if (last == row->glyphs[TEXT_AREA])
10172 last->left_box_line_p = 1;
10173
10174 /* Make line the desired height and center it vertically. */
10175 if ((height -= it->max_ascent + it->max_descent) > 0)
10176 {
10177 /* Don't add more than one line height. */
10178 height %= FRAME_LINE_HEIGHT (it->f);
10179 it->max_ascent += height / 2;
10180 it->max_descent += (height + 1) / 2;
10181 }
10182
10183 compute_line_metrics (it);
10184
10185 /* If line is empty, make it occupy the rest of the tool-bar. */
10186 if (!row->displays_text_p)
10187 {
10188 row->height = row->phys_height = it->last_visible_y - row->y;
10189 row->visible_height = row->height;
10190 row->ascent = row->phys_ascent = 0;
10191 row->extra_line_spacing = 0;
10192 }
10193
10194 row->full_width_p = 1;
10195 row->continued_p = 0;
10196 row->truncated_on_left_p = 0;
10197 row->truncated_on_right_p = 0;
10198
10199 it->current_x = it->hpos = 0;
10200 it->current_y += row->height;
10201 ++it->vpos;
10202 ++it->glyph_row;
10203 }
10204
10205
10206 /* Max tool-bar height. */
10207
10208 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10209 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10210
10211 /* Value is the number of screen lines needed to make all tool-bar
10212 items of frame F visible. The number of actual rows needed is
10213 returned in *N_ROWS if non-NULL. */
10214
10215 static int
10216 tool_bar_lines_needed (f, n_rows)
10217 struct frame *f;
10218 int *n_rows;
10219 {
10220 struct window *w = XWINDOW (f->tool_bar_window);
10221 struct it it;
10222 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10223 the desired matrix, so use (unused) mode-line row as temporary row to
10224 avoid destroying the first tool-bar row. */
10225 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10226
10227 /* Initialize an iterator for iteration over
10228 F->desired_tool_bar_string in the tool-bar window of frame F. */
10229 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10230 it.first_visible_x = 0;
10231 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10232 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10233
10234 while (!ITERATOR_AT_END_P (&it))
10235 {
10236 clear_glyph_row (temp_row);
10237 it.glyph_row = temp_row;
10238 display_tool_bar_line (&it, -1);
10239 }
10240 clear_glyph_row (temp_row);
10241
10242 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10243 if (n_rows)
10244 *n_rows = it.vpos > 0 ? it.vpos : -1;
10245
10246 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10247 }
10248
10249
10250 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10251 0, 1, 0,
10252 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10253 (frame)
10254 Lisp_Object frame;
10255 {
10256 struct frame *f;
10257 struct window *w;
10258 int nlines = 0;
10259
10260 if (NILP (frame))
10261 frame = selected_frame;
10262 else
10263 CHECK_FRAME (frame);
10264 f = XFRAME (frame);
10265
10266 if (WINDOWP (f->tool_bar_window)
10267 || (w = XWINDOW (f->tool_bar_window),
10268 WINDOW_TOTAL_LINES (w) > 0))
10269 {
10270 update_tool_bar (f, 1);
10271 if (f->n_tool_bar_items)
10272 {
10273 build_desired_tool_bar_string (f);
10274 nlines = tool_bar_lines_needed (f, NULL);
10275 }
10276 }
10277
10278 return make_number (nlines);
10279 }
10280
10281
10282 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10283 height should be changed. */
10284
10285 static int
10286 redisplay_tool_bar (f)
10287 struct frame *f;
10288 {
10289 struct window *w;
10290 struct it it;
10291 struct glyph_row *row;
10292
10293 #if defined (USE_GTK) || defined (HAVE_NS)
10294 if (FRAME_EXTERNAL_TOOL_BAR (f))
10295 update_frame_tool_bar (f);
10296 return 0;
10297 #endif
10298
10299 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10300 do anything. This means you must start with tool-bar-lines
10301 non-zero to get the auto-sizing effect. Or in other words, you
10302 can turn off tool-bars by specifying tool-bar-lines zero. */
10303 if (!WINDOWP (f->tool_bar_window)
10304 || (w = XWINDOW (f->tool_bar_window),
10305 WINDOW_TOTAL_LINES (w) == 0))
10306 return 0;
10307
10308 /* Set up an iterator for the tool-bar window. */
10309 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10310 it.first_visible_x = 0;
10311 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10312 row = it.glyph_row;
10313
10314 /* Build a string that represents the contents of the tool-bar. */
10315 build_desired_tool_bar_string (f);
10316 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10317
10318 if (f->n_tool_bar_rows == 0)
10319 {
10320 int nlines;
10321
10322 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10323 nlines != WINDOW_TOTAL_LINES (w)))
10324 {
10325 extern Lisp_Object Qtool_bar_lines;
10326 Lisp_Object frame;
10327 int old_height = WINDOW_TOTAL_LINES (w);
10328
10329 XSETFRAME (frame, f);
10330 Fmodify_frame_parameters (frame,
10331 Fcons (Fcons (Qtool_bar_lines,
10332 make_number (nlines)),
10333 Qnil));
10334 if (WINDOW_TOTAL_LINES (w) != old_height)
10335 {
10336 clear_glyph_matrix (w->desired_matrix);
10337 fonts_changed_p = 1;
10338 return 1;
10339 }
10340 }
10341 }
10342
10343 /* Display as many lines as needed to display all tool-bar items. */
10344
10345 if (f->n_tool_bar_rows > 0)
10346 {
10347 int border, rows, height, extra;
10348
10349 if (INTEGERP (Vtool_bar_border))
10350 border = XINT (Vtool_bar_border);
10351 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10352 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10353 else if (EQ (Vtool_bar_border, Qborder_width))
10354 border = f->border_width;
10355 else
10356 border = 0;
10357 if (border < 0)
10358 border = 0;
10359
10360 rows = f->n_tool_bar_rows;
10361 height = max (1, (it.last_visible_y - border) / rows);
10362 extra = it.last_visible_y - border - height * rows;
10363
10364 while (it.current_y < it.last_visible_y)
10365 {
10366 int h = 0;
10367 if (extra > 0 && rows-- > 0)
10368 {
10369 h = (extra + rows - 1) / rows;
10370 extra -= h;
10371 }
10372 display_tool_bar_line (&it, height + h);
10373 }
10374 }
10375 else
10376 {
10377 while (it.current_y < it.last_visible_y)
10378 display_tool_bar_line (&it, 0);
10379 }
10380
10381 /* It doesn't make much sense to try scrolling in the tool-bar
10382 window, so don't do it. */
10383 w->desired_matrix->no_scrolling_p = 1;
10384 w->must_be_updated_p = 1;
10385
10386 if (!NILP (Vauto_resize_tool_bars))
10387 {
10388 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10389 int change_height_p = 0;
10390
10391 /* If we couldn't display everything, change the tool-bar's
10392 height if there is room for more. */
10393 if (IT_STRING_CHARPOS (it) < it.end_charpos
10394 && it.current_y < max_tool_bar_height)
10395 change_height_p = 1;
10396
10397 row = it.glyph_row - 1;
10398
10399 /* If there are blank lines at the end, except for a partially
10400 visible blank line at the end that is smaller than
10401 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10402 if (!row->displays_text_p
10403 && row->height >= FRAME_LINE_HEIGHT (f))
10404 change_height_p = 1;
10405
10406 /* If row displays tool-bar items, but is partially visible,
10407 change the tool-bar's height. */
10408 if (row->displays_text_p
10409 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10410 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10411 change_height_p = 1;
10412
10413 /* Resize windows as needed by changing the `tool-bar-lines'
10414 frame parameter. */
10415 if (change_height_p)
10416 {
10417 extern Lisp_Object Qtool_bar_lines;
10418 Lisp_Object frame;
10419 int old_height = WINDOW_TOTAL_LINES (w);
10420 int nrows;
10421 int nlines = tool_bar_lines_needed (f, &nrows);
10422
10423 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10424 && !f->minimize_tool_bar_window_p)
10425 ? (nlines > old_height)
10426 : (nlines != old_height));
10427 f->minimize_tool_bar_window_p = 0;
10428
10429 if (change_height_p)
10430 {
10431 XSETFRAME (frame, f);
10432 Fmodify_frame_parameters (frame,
10433 Fcons (Fcons (Qtool_bar_lines,
10434 make_number (nlines)),
10435 Qnil));
10436 if (WINDOW_TOTAL_LINES (w) != old_height)
10437 {
10438 clear_glyph_matrix (w->desired_matrix);
10439 f->n_tool_bar_rows = nrows;
10440 fonts_changed_p = 1;
10441 return 1;
10442 }
10443 }
10444 }
10445 }
10446
10447 f->minimize_tool_bar_window_p = 0;
10448 return 0;
10449 }
10450
10451
10452 /* Get information about the tool-bar item which is displayed in GLYPH
10453 on frame F. Return in *PROP_IDX the index where tool-bar item
10454 properties start in F->tool_bar_items. Value is zero if
10455 GLYPH doesn't display a tool-bar item. */
10456
10457 static int
10458 tool_bar_item_info (f, glyph, prop_idx)
10459 struct frame *f;
10460 struct glyph *glyph;
10461 int *prop_idx;
10462 {
10463 Lisp_Object prop;
10464 int success_p;
10465 int charpos;
10466
10467 /* This function can be called asynchronously, which means we must
10468 exclude any possibility that Fget_text_property signals an
10469 error. */
10470 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10471 charpos = max (0, charpos);
10472
10473 /* Get the text property `menu-item' at pos. The value of that
10474 property is the start index of this item's properties in
10475 F->tool_bar_items. */
10476 prop = Fget_text_property (make_number (charpos),
10477 Qmenu_item, f->current_tool_bar_string);
10478 if (INTEGERP (prop))
10479 {
10480 *prop_idx = XINT (prop);
10481 success_p = 1;
10482 }
10483 else
10484 success_p = 0;
10485
10486 return success_p;
10487 }
10488
10489 \f
10490 /* Get information about the tool-bar item at position X/Y on frame F.
10491 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10492 the current matrix of the tool-bar window of F, or NULL if not
10493 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10494 item in F->tool_bar_items. Value is
10495
10496 -1 if X/Y is not on a tool-bar item
10497 0 if X/Y is on the same item that was highlighted before.
10498 1 otherwise. */
10499
10500 static int
10501 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10502 struct frame *f;
10503 int x, y;
10504 struct glyph **glyph;
10505 int *hpos, *vpos, *prop_idx;
10506 {
10507 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10508 struct window *w = XWINDOW (f->tool_bar_window);
10509 int area;
10510
10511 /* Find the glyph under X/Y. */
10512 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10513 if (*glyph == NULL)
10514 return -1;
10515
10516 /* Get the start of this tool-bar item's properties in
10517 f->tool_bar_items. */
10518 if (!tool_bar_item_info (f, *glyph, prop_idx))
10519 return -1;
10520
10521 /* Is mouse on the highlighted item? */
10522 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10523 && *vpos >= dpyinfo->mouse_face_beg_row
10524 && *vpos <= dpyinfo->mouse_face_end_row
10525 && (*vpos > dpyinfo->mouse_face_beg_row
10526 || *hpos >= dpyinfo->mouse_face_beg_col)
10527 && (*vpos < dpyinfo->mouse_face_end_row
10528 || *hpos < dpyinfo->mouse_face_end_col
10529 || dpyinfo->mouse_face_past_end))
10530 return 0;
10531
10532 return 1;
10533 }
10534
10535
10536 /* EXPORT:
10537 Handle mouse button event on the tool-bar of frame F, at
10538 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10539 0 for button release. MODIFIERS is event modifiers for button
10540 release. */
10541
10542 void
10543 handle_tool_bar_click (f, x, y, down_p, modifiers)
10544 struct frame *f;
10545 int x, y, down_p;
10546 unsigned int modifiers;
10547 {
10548 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10549 struct window *w = XWINDOW (f->tool_bar_window);
10550 int hpos, vpos, prop_idx;
10551 struct glyph *glyph;
10552 Lisp_Object enabled_p;
10553
10554 /* If not on the highlighted tool-bar item, return. */
10555 frame_to_window_pixel_xy (w, &x, &y);
10556 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10557 return;
10558
10559 /* If item is disabled, do nothing. */
10560 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10561 if (NILP (enabled_p))
10562 return;
10563
10564 if (down_p)
10565 {
10566 /* Show item in pressed state. */
10567 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10568 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10569 last_tool_bar_item = prop_idx;
10570 }
10571 else
10572 {
10573 Lisp_Object key, frame;
10574 struct input_event event;
10575 EVENT_INIT (event);
10576
10577 /* Show item in released state. */
10578 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10579 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10580
10581 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10582
10583 XSETFRAME (frame, f);
10584 event.kind = TOOL_BAR_EVENT;
10585 event.frame_or_window = frame;
10586 event.arg = frame;
10587 kbd_buffer_store_event (&event);
10588
10589 event.kind = TOOL_BAR_EVENT;
10590 event.frame_or_window = frame;
10591 event.arg = key;
10592 event.modifiers = modifiers;
10593 kbd_buffer_store_event (&event);
10594 last_tool_bar_item = -1;
10595 }
10596 }
10597
10598
10599 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10600 tool-bar window-relative coordinates X/Y. Called from
10601 note_mouse_highlight. */
10602
10603 static void
10604 note_tool_bar_highlight (f, x, y)
10605 struct frame *f;
10606 int x, y;
10607 {
10608 Lisp_Object window = f->tool_bar_window;
10609 struct window *w = XWINDOW (window);
10610 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10611 int hpos, vpos;
10612 struct glyph *glyph;
10613 struct glyph_row *row;
10614 int i;
10615 Lisp_Object enabled_p;
10616 int prop_idx;
10617 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10618 int mouse_down_p, rc;
10619
10620 /* Function note_mouse_highlight is called with negative x(y
10621 values when mouse moves outside of the frame. */
10622 if (x <= 0 || y <= 0)
10623 {
10624 clear_mouse_face (dpyinfo);
10625 return;
10626 }
10627
10628 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10629 if (rc < 0)
10630 {
10631 /* Not on tool-bar item. */
10632 clear_mouse_face (dpyinfo);
10633 return;
10634 }
10635 else if (rc == 0)
10636 /* On same tool-bar item as before. */
10637 goto set_help_echo;
10638
10639 clear_mouse_face (dpyinfo);
10640
10641 /* Mouse is down, but on different tool-bar item? */
10642 mouse_down_p = (dpyinfo->grabbed
10643 && f == last_mouse_frame
10644 && FRAME_LIVE_P (f));
10645 if (mouse_down_p
10646 && last_tool_bar_item != prop_idx)
10647 return;
10648
10649 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10650 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10651
10652 /* If tool-bar item is not enabled, don't highlight it. */
10653 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10654 if (!NILP (enabled_p))
10655 {
10656 /* Compute the x-position of the glyph. In front and past the
10657 image is a space. We include this in the highlighted area. */
10658 row = MATRIX_ROW (w->current_matrix, vpos);
10659 for (i = x = 0; i < hpos; ++i)
10660 x += row->glyphs[TEXT_AREA][i].pixel_width;
10661
10662 /* Record this as the current active region. */
10663 dpyinfo->mouse_face_beg_col = hpos;
10664 dpyinfo->mouse_face_beg_row = vpos;
10665 dpyinfo->mouse_face_beg_x = x;
10666 dpyinfo->mouse_face_beg_y = row->y;
10667 dpyinfo->mouse_face_past_end = 0;
10668
10669 dpyinfo->mouse_face_end_col = hpos + 1;
10670 dpyinfo->mouse_face_end_row = vpos;
10671 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10672 dpyinfo->mouse_face_end_y = row->y;
10673 dpyinfo->mouse_face_window = window;
10674 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10675
10676 /* Display it as active. */
10677 show_mouse_face (dpyinfo, draw);
10678 dpyinfo->mouse_face_image_state = draw;
10679 }
10680
10681 set_help_echo:
10682
10683 /* Set help_echo_string to a help string to display for this tool-bar item.
10684 XTread_socket does the rest. */
10685 help_echo_object = help_echo_window = Qnil;
10686 help_echo_pos = -1;
10687 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10688 if (NILP (help_echo_string))
10689 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10690 }
10691
10692 #endif /* HAVE_WINDOW_SYSTEM */
10693
10694
10695 \f
10696 /************************************************************************
10697 Horizontal scrolling
10698 ************************************************************************/
10699
10700 static int hscroll_window_tree P_ ((Lisp_Object));
10701 static int hscroll_windows P_ ((Lisp_Object));
10702
10703 /* For all leaf windows in the window tree rooted at WINDOW, set their
10704 hscroll value so that PT is (i) visible in the window, and (ii) so
10705 that it is not within a certain margin at the window's left and
10706 right border. Value is non-zero if any window's hscroll has been
10707 changed. */
10708
10709 static int
10710 hscroll_window_tree (window)
10711 Lisp_Object window;
10712 {
10713 int hscrolled_p = 0;
10714 int hscroll_relative_p = FLOATP (Vhscroll_step);
10715 int hscroll_step_abs = 0;
10716 double hscroll_step_rel = 0;
10717
10718 if (hscroll_relative_p)
10719 {
10720 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10721 if (hscroll_step_rel < 0)
10722 {
10723 hscroll_relative_p = 0;
10724 hscroll_step_abs = 0;
10725 }
10726 }
10727 else if (INTEGERP (Vhscroll_step))
10728 {
10729 hscroll_step_abs = XINT (Vhscroll_step);
10730 if (hscroll_step_abs < 0)
10731 hscroll_step_abs = 0;
10732 }
10733 else
10734 hscroll_step_abs = 0;
10735
10736 while (WINDOWP (window))
10737 {
10738 struct window *w = XWINDOW (window);
10739
10740 if (WINDOWP (w->hchild))
10741 hscrolled_p |= hscroll_window_tree (w->hchild);
10742 else if (WINDOWP (w->vchild))
10743 hscrolled_p |= hscroll_window_tree (w->vchild);
10744 else if (w->cursor.vpos >= 0)
10745 {
10746 int h_margin;
10747 int text_area_width;
10748 struct glyph_row *current_cursor_row
10749 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10750 struct glyph_row *desired_cursor_row
10751 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10752 struct glyph_row *cursor_row
10753 = (desired_cursor_row->enabled_p
10754 ? desired_cursor_row
10755 : current_cursor_row);
10756
10757 text_area_width = window_box_width (w, TEXT_AREA);
10758
10759 /* Scroll when cursor is inside this scroll margin. */
10760 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10761
10762 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10763 && ((XFASTINT (w->hscroll)
10764 && w->cursor.x <= h_margin)
10765 || (cursor_row->enabled_p
10766 && cursor_row->truncated_on_right_p
10767 && (w->cursor.x >= text_area_width - h_margin))))
10768 {
10769 struct it it;
10770 int hscroll;
10771 struct buffer *saved_current_buffer;
10772 int pt;
10773 int wanted_x;
10774
10775 /* Find point in a display of infinite width. */
10776 saved_current_buffer = current_buffer;
10777 current_buffer = XBUFFER (w->buffer);
10778
10779 if (w == XWINDOW (selected_window))
10780 pt = PT;
10781 else
10782 {
10783 pt = marker_position (w->pointm);
10784 pt = max (BEGV, pt);
10785 pt = min (ZV, pt);
10786 }
10787
10788 /* Move iterator to pt starting at cursor_row->start in
10789 a line with infinite width. */
10790 init_to_row_start (&it, w, cursor_row);
10791 it.last_visible_x = INFINITY;
10792 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10793 current_buffer = saved_current_buffer;
10794
10795 /* Position cursor in window. */
10796 if (!hscroll_relative_p && hscroll_step_abs == 0)
10797 hscroll = max (0, (it.current_x
10798 - (ITERATOR_AT_END_OF_LINE_P (&it)
10799 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10800 : (text_area_width / 2))))
10801 / FRAME_COLUMN_WIDTH (it.f);
10802 else if (w->cursor.x >= text_area_width - h_margin)
10803 {
10804 if (hscroll_relative_p)
10805 wanted_x = text_area_width * (1 - hscroll_step_rel)
10806 - h_margin;
10807 else
10808 wanted_x = text_area_width
10809 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10810 - h_margin;
10811 hscroll
10812 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10813 }
10814 else
10815 {
10816 if (hscroll_relative_p)
10817 wanted_x = text_area_width * hscroll_step_rel
10818 + h_margin;
10819 else
10820 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10821 + h_margin;
10822 hscroll
10823 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10824 }
10825 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10826
10827 /* Don't call Fset_window_hscroll if value hasn't
10828 changed because it will prevent redisplay
10829 optimizations. */
10830 if (XFASTINT (w->hscroll) != hscroll)
10831 {
10832 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10833 w->hscroll = make_number (hscroll);
10834 hscrolled_p = 1;
10835 }
10836 }
10837 }
10838
10839 window = w->next;
10840 }
10841
10842 /* Value is non-zero if hscroll of any leaf window has been changed. */
10843 return hscrolled_p;
10844 }
10845
10846
10847 /* Set hscroll so that cursor is visible and not inside horizontal
10848 scroll margins for all windows in the tree rooted at WINDOW. See
10849 also hscroll_window_tree above. Value is non-zero if any window's
10850 hscroll has been changed. If it has, desired matrices on the frame
10851 of WINDOW are cleared. */
10852
10853 static int
10854 hscroll_windows (window)
10855 Lisp_Object window;
10856 {
10857 int hscrolled_p = hscroll_window_tree (window);
10858 if (hscrolled_p)
10859 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10860 return hscrolled_p;
10861 }
10862
10863
10864 \f
10865 /************************************************************************
10866 Redisplay
10867 ************************************************************************/
10868
10869 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10870 to a non-zero value. This is sometimes handy to have in a debugger
10871 session. */
10872
10873 #if GLYPH_DEBUG
10874
10875 /* First and last unchanged row for try_window_id. */
10876
10877 int debug_first_unchanged_at_end_vpos;
10878 int debug_last_unchanged_at_beg_vpos;
10879
10880 /* Delta vpos and y. */
10881
10882 int debug_dvpos, debug_dy;
10883
10884 /* Delta in characters and bytes for try_window_id. */
10885
10886 int debug_delta, debug_delta_bytes;
10887
10888 /* Values of window_end_pos and window_end_vpos at the end of
10889 try_window_id. */
10890
10891 EMACS_INT debug_end_pos, debug_end_vpos;
10892
10893 /* Append a string to W->desired_matrix->method. FMT is a printf
10894 format string. A1...A9 are a supplement for a variable-length
10895 argument list. If trace_redisplay_p is non-zero also printf the
10896 resulting string to stderr. */
10897
10898 static void
10899 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10900 struct window *w;
10901 char *fmt;
10902 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10903 {
10904 char buffer[512];
10905 char *method = w->desired_matrix->method;
10906 int len = strlen (method);
10907 int size = sizeof w->desired_matrix->method;
10908 int remaining = size - len - 1;
10909
10910 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10911 if (len && remaining)
10912 {
10913 method[len] = '|';
10914 --remaining, ++len;
10915 }
10916
10917 strncpy (method + len, buffer, remaining);
10918
10919 if (trace_redisplay_p)
10920 fprintf (stderr, "%p (%s): %s\n",
10921 w,
10922 ((BUFFERP (w->buffer)
10923 && STRINGP (XBUFFER (w->buffer)->name))
10924 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10925 : "no buffer"),
10926 buffer);
10927 }
10928
10929 #endif /* GLYPH_DEBUG */
10930
10931
10932 /* Value is non-zero if all changes in window W, which displays
10933 current_buffer, are in the text between START and END. START is a
10934 buffer position, END is given as a distance from Z. Used in
10935 redisplay_internal for display optimization. */
10936
10937 static INLINE int
10938 text_outside_line_unchanged_p (w, start, end)
10939 struct window *w;
10940 int start, end;
10941 {
10942 int unchanged_p = 1;
10943
10944 /* If text or overlays have changed, see where. */
10945 if (XFASTINT (w->last_modified) < MODIFF
10946 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10947 {
10948 /* Gap in the line? */
10949 if (GPT < start || Z - GPT < end)
10950 unchanged_p = 0;
10951
10952 /* Changes start in front of the line, or end after it? */
10953 if (unchanged_p
10954 && (BEG_UNCHANGED < start - 1
10955 || END_UNCHANGED < end))
10956 unchanged_p = 0;
10957
10958 /* If selective display, can't optimize if changes start at the
10959 beginning of the line. */
10960 if (unchanged_p
10961 && INTEGERP (current_buffer->selective_display)
10962 && XINT (current_buffer->selective_display) > 0
10963 && (BEG_UNCHANGED < start || GPT <= start))
10964 unchanged_p = 0;
10965
10966 /* If there are overlays at the start or end of the line, these
10967 may have overlay strings with newlines in them. A change at
10968 START, for instance, may actually concern the display of such
10969 overlay strings as well, and they are displayed on different
10970 lines. So, quickly rule out this case. (For the future, it
10971 might be desirable to implement something more telling than
10972 just BEG/END_UNCHANGED.) */
10973 if (unchanged_p)
10974 {
10975 if (BEG + BEG_UNCHANGED == start
10976 && overlay_touches_p (start))
10977 unchanged_p = 0;
10978 if (END_UNCHANGED == end
10979 && overlay_touches_p (Z - end))
10980 unchanged_p = 0;
10981 }
10982 }
10983
10984 return unchanged_p;
10985 }
10986
10987
10988 /* Do a frame update, taking possible shortcuts into account. This is
10989 the main external entry point for redisplay.
10990
10991 If the last redisplay displayed an echo area message and that message
10992 is no longer requested, we clear the echo area or bring back the
10993 mini-buffer if that is in use. */
10994
10995 void
10996 redisplay ()
10997 {
10998 redisplay_internal (0);
10999 }
11000
11001
11002 static Lisp_Object
11003 overlay_arrow_string_or_property (var)
11004 Lisp_Object var;
11005 {
11006 Lisp_Object val;
11007
11008 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11009 return val;
11010
11011 return Voverlay_arrow_string;
11012 }
11013
11014 /* Return 1 if there are any overlay-arrows in current_buffer. */
11015 static int
11016 overlay_arrow_in_current_buffer_p ()
11017 {
11018 Lisp_Object vlist;
11019
11020 for (vlist = Voverlay_arrow_variable_list;
11021 CONSP (vlist);
11022 vlist = XCDR (vlist))
11023 {
11024 Lisp_Object var = XCAR (vlist);
11025 Lisp_Object val;
11026
11027 if (!SYMBOLP (var))
11028 continue;
11029 val = find_symbol_value (var);
11030 if (MARKERP (val)
11031 && current_buffer == XMARKER (val)->buffer)
11032 return 1;
11033 }
11034 return 0;
11035 }
11036
11037
11038 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11039 has changed. */
11040
11041 static int
11042 overlay_arrows_changed_p ()
11043 {
11044 Lisp_Object vlist;
11045
11046 for (vlist = Voverlay_arrow_variable_list;
11047 CONSP (vlist);
11048 vlist = XCDR (vlist))
11049 {
11050 Lisp_Object var = XCAR (vlist);
11051 Lisp_Object val, pstr;
11052
11053 if (!SYMBOLP (var))
11054 continue;
11055 val = find_symbol_value (var);
11056 if (!MARKERP (val))
11057 continue;
11058 if (! EQ (COERCE_MARKER (val),
11059 Fget (var, Qlast_arrow_position))
11060 || ! (pstr = overlay_arrow_string_or_property (var),
11061 EQ (pstr, Fget (var, Qlast_arrow_string))))
11062 return 1;
11063 }
11064 return 0;
11065 }
11066
11067 /* Mark overlay arrows to be updated on next redisplay. */
11068
11069 static void
11070 update_overlay_arrows (up_to_date)
11071 int up_to_date;
11072 {
11073 Lisp_Object vlist;
11074
11075 for (vlist = Voverlay_arrow_variable_list;
11076 CONSP (vlist);
11077 vlist = XCDR (vlist))
11078 {
11079 Lisp_Object var = XCAR (vlist);
11080
11081 if (!SYMBOLP (var))
11082 continue;
11083
11084 if (up_to_date > 0)
11085 {
11086 Lisp_Object val = find_symbol_value (var);
11087 Fput (var, Qlast_arrow_position,
11088 COERCE_MARKER (val));
11089 Fput (var, Qlast_arrow_string,
11090 overlay_arrow_string_or_property (var));
11091 }
11092 else if (up_to_date < 0
11093 || !NILP (Fget (var, Qlast_arrow_position)))
11094 {
11095 Fput (var, Qlast_arrow_position, Qt);
11096 Fput (var, Qlast_arrow_string, Qt);
11097 }
11098 }
11099 }
11100
11101
11102 /* Return overlay arrow string to display at row.
11103 Return integer (bitmap number) for arrow bitmap in left fringe.
11104 Return nil if no overlay arrow. */
11105
11106 static Lisp_Object
11107 overlay_arrow_at_row (it, row)
11108 struct it *it;
11109 struct glyph_row *row;
11110 {
11111 Lisp_Object vlist;
11112
11113 for (vlist = Voverlay_arrow_variable_list;
11114 CONSP (vlist);
11115 vlist = XCDR (vlist))
11116 {
11117 Lisp_Object var = XCAR (vlist);
11118 Lisp_Object val;
11119
11120 if (!SYMBOLP (var))
11121 continue;
11122
11123 val = find_symbol_value (var);
11124
11125 if (MARKERP (val)
11126 && current_buffer == XMARKER (val)->buffer
11127 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11128 {
11129 if (FRAME_WINDOW_P (it->f)
11130 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11131 {
11132 #ifdef HAVE_WINDOW_SYSTEM
11133 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11134 {
11135 int fringe_bitmap;
11136 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11137 return make_number (fringe_bitmap);
11138 }
11139 #endif
11140 return make_number (-1); /* Use default arrow bitmap */
11141 }
11142 return overlay_arrow_string_or_property (var);
11143 }
11144 }
11145
11146 return Qnil;
11147 }
11148
11149 /* Return 1 if point moved out of or into a composition. Otherwise
11150 return 0. PREV_BUF and PREV_PT are the last point buffer and
11151 position. BUF and PT are the current point buffer and position. */
11152
11153 int
11154 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11155 struct buffer *prev_buf, *buf;
11156 int prev_pt, pt;
11157 {
11158 EMACS_INT start, end;
11159 Lisp_Object prop;
11160 Lisp_Object buffer;
11161
11162 XSETBUFFER (buffer, buf);
11163 /* Check a composition at the last point if point moved within the
11164 same buffer. */
11165 if (prev_buf == buf)
11166 {
11167 if (prev_pt == pt)
11168 /* Point didn't move. */
11169 return 0;
11170
11171 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11172 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11173 && COMPOSITION_VALID_P (start, end, prop)
11174 && start < prev_pt && end > prev_pt)
11175 /* The last point was within the composition. Return 1 iff
11176 point moved out of the composition. */
11177 return (pt <= start || pt >= end);
11178 }
11179
11180 /* Check a composition at the current point. */
11181 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11182 && find_composition (pt, -1, &start, &end, &prop, buffer)
11183 && COMPOSITION_VALID_P (start, end, prop)
11184 && start < pt && end > pt);
11185 }
11186
11187
11188 /* Reconsider the setting of B->clip_changed which is displayed
11189 in window W. */
11190
11191 static INLINE void
11192 reconsider_clip_changes (w, b)
11193 struct window *w;
11194 struct buffer *b;
11195 {
11196 if (b->clip_changed
11197 && !NILP (w->window_end_valid)
11198 && w->current_matrix->buffer == b
11199 && w->current_matrix->zv == BUF_ZV (b)
11200 && w->current_matrix->begv == BUF_BEGV (b))
11201 b->clip_changed = 0;
11202
11203 /* If display wasn't paused, and W is not a tool bar window, see if
11204 point has been moved into or out of a composition. In that case,
11205 we set b->clip_changed to 1 to force updating the screen. If
11206 b->clip_changed has already been set to 1, we can skip this
11207 check. */
11208 if (!b->clip_changed
11209 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11210 {
11211 int pt;
11212
11213 if (w == XWINDOW (selected_window))
11214 pt = PT;
11215 else
11216 pt = marker_position (w->pointm);
11217
11218 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11219 || pt != XINT (w->last_point))
11220 && check_point_in_composition (w->current_matrix->buffer,
11221 XINT (w->last_point),
11222 XBUFFER (w->buffer), pt))
11223 b->clip_changed = 1;
11224 }
11225 }
11226 \f
11227
11228 /* Select FRAME to forward the values of frame-local variables into C
11229 variables so that the redisplay routines can access those values
11230 directly. */
11231
11232 static void
11233 select_frame_for_redisplay (frame)
11234 Lisp_Object frame;
11235 {
11236 Lisp_Object tail, symbol, val;
11237 Lisp_Object old = selected_frame;
11238 struct Lisp_Symbol *sym;
11239
11240 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11241
11242 selected_frame = frame;
11243
11244 do
11245 {
11246 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11247 if (CONSP (XCAR (tail))
11248 && (symbol = XCAR (XCAR (tail)),
11249 SYMBOLP (symbol))
11250 && (sym = indirect_variable (XSYMBOL (symbol)),
11251 val = sym->value,
11252 (BUFFER_LOCAL_VALUEP (val)))
11253 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11254 /* Use find_symbol_value rather than Fsymbol_value
11255 to avoid an error if it is void. */
11256 find_symbol_value (symbol);
11257 } while (!EQ (frame, old) && (frame = old, 1));
11258 }
11259
11260
11261 #define STOP_POLLING \
11262 do { if (! polling_stopped_here) stop_polling (); \
11263 polling_stopped_here = 1; } while (0)
11264
11265 #define RESUME_POLLING \
11266 do { if (polling_stopped_here) start_polling (); \
11267 polling_stopped_here = 0; } while (0)
11268
11269
11270 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11271 response to any user action; therefore, we should preserve the echo
11272 area. (Actually, our caller does that job.) Perhaps in the future
11273 avoid recentering windows if it is not necessary; currently that
11274 causes some problems. */
11275
11276 static void
11277 redisplay_internal (preserve_echo_area)
11278 int preserve_echo_area;
11279 {
11280 struct window *w = XWINDOW (selected_window);
11281 struct window *sw;
11282 struct frame *f;
11283 int pause;
11284 int must_finish = 0;
11285 struct text_pos tlbufpos, tlendpos;
11286 int number_of_visible_frames;
11287 int count, count1;
11288 struct frame *sf;
11289 int polling_stopped_here = 0;
11290 Lisp_Object old_frame = selected_frame;
11291
11292 /* Non-zero means redisplay has to consider all windows on all
11293 frames. Zero means, only selected_window is considered. */
11294 int consider_all_windows_p;
11295
11296 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11297
11298 /* No redisplay if running in batch mode or frame is not yet fully
11299 initialized, or redisplay is explicitly turned off by setting
11300 Vinhibit_redisplay. */
11301 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11302 || !NILP (Vinhibit_redisplay))
11303 return;
11304
11305 /* Don't examine these until after testing Vinhibit_redisplay.
11306 When Emacs is shutting down, perhaps because its connection to
11307 X has dropped, we should not look at them at all. */
11308 f = XFRAME (w->frame);
11309 sf = SELECTED_FRAME ();
11310
11311 if (!f->glyphs_initialized_p)
11312 return;
11313
11314 /* The flag redisplay_performed_directly_p is set by
11315 direct_output_for_insert when it already did the whole screen
11316 update necessary. */
11317 if (redisplay_performed_directly_p)
11318 {
11319 redisplay_performed_directly_p = 0;
11320 if (!hscroll_windows (selected_window))
11321 return;
11322 }
11323
11324 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11325 if (popup_activated ())
11326 return;
11327 #endif
11328
11329 /* I don't think this happens but let's be paranoid. */
11330 if (redisplaying_p)
11331 return;
11332
11333 /* Record a function that resets redisplaying_p to its old value
11334 when we leave this function. */
11335 count = SPECPDL_INDEX ();
11336 record_unwind_protect (unwind_redisplay,
11337 Fcons (make_number (redisplaying_p), selected_frame));
11338 ++redisplaying_p;
11339 specbind (Qinhibit_free_realized_faces, Qnil);
11340
11341 {
11342 Lisp_Object tail, frame;
11343
11344 FOR_EACH_FRAME (tail, frame)
11345 {
11346 struct frame *f = XFRAME (frame);
11347 f->already_hscrolled_p = 0;
11348 }
11349 }
11350
11351 retry:
11352 /* Remember the currently selected window. */
11353 sw = w;
11354
11355 if (!EQ (old_frame, selected_frame)
11356 && FRAME_LIVE_P (XFRAME (old_frame)))
11357 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11358 selected_frame and selected_window to be temporarily out-of-sync so
11359 when we come back here via `goto retry', we need to resync because we
11360 may need to run Elisp code (via prepare_menu_bars). */
11361 select_frame_for_redisplay (old_frame);
11362
11363 pause = 0;
11364 reconsider_clip_changes (w, current_buffer);
11365 last_escape_glyph_frame = NULL;
11366 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11367
11368 /* If new fonts have been loaded that make a glyph matrix adjustment
11369 necessary, do it. */
11370 if (fonts_changed_p)
11371 {
11372 adjust_glyphs (NULL);
11373 ++windows_or_buffers_changed;
11374 fonts_changed_p = 0;
11375 }
11376
11377 /* If face_change_count is non-zero, init_iterator will free all
11378 realized faces, which includes the faces referenced from current
11379 matrices. So, we can't reuse current matrices in this case. */
11380 if (face_change_count)
11381 ++windows_or_buffers_changed;
11382
11383 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11384 && FRAME_TTY (sf)->previous_frame != sf)
11385 {
11386 /* Since frames on a single ASCII terminal share the same
11387 display area, displaying a different frame means redisplay
11388 the whole thing. */
11389 windows_or_buffers_changed++;
11390 SET_FRAME_GARBAGED (sf);
11391 #ifndef DOS_NT
11392 set_tty_color_mode (FRAME_TTY (sf), sf);
11393 #endif
11394 FRAME_TTY (sf)->previous_frame = sf;
11395 }
11396
11397 /* Set the visible flags for all frames. Do this before checking
11398 for resized or garbaged frames; they want to know if their frames
11399 are visible. See the comment in frame.h for
11400 FRAME_SAMPLE_VISIBILITY. */
11401 {
11402 Lisp_Object tail, frame;
11403
11404 number_of_visible_frames = 0;
11405
11406 FOR_EACH_FRAME (tail, frame)
11407 {
11408 struct frame *f = XFRAME (frame);
11409
11410 FRAME_SAMPLE_VISIBILITY (f);
11411 if (FRAME_VISIBLE_P (f))
11412 ++number_of_visible_frames;
11413 clear_desired_matrices (f);
11414 }
11415 }
11416
11417 /* Notice any pending interrupt request to change frame size. */
11418 do_pending_window_change (1);
11419
11420 /* do_pending_window_change could change the selected_window due to
11421 frame resizing which makes the selected window too small. */
11422 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
11423 {
11424 sw = w;
11425 reconsider_clip_changes (w, current_buffer);
11426 }
11427
11428 /* Clear frames marked as garbaged. */
11429 if (frame_garbaged)
11430 clear_garbaged_frames ();
11431
11432 /* Build menubar and tool-bar items. */
11433 if (NILP (Vmemory_full))
11434 prepare_menu_bars ();
11435
11436 if (windows_or_buffers_changed)
11437 update_mode_lines++;
11438
11439 /* Detect case that we need to write or remove a star in the mode line. */
11440 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11441 {
11442 w->update_mode_line = Qt;
11443 if (buffer_shared > 1)
11444 update_mode_lines++;
11445 }
11446
11447 /* Avoid invocation of point motion hooks by `current_column' below. */
11448 count1 = SPECPDL_INDEX ();
11449 specbind (Qinhibit_point_motion_hooks, Qt);
11450
11451 /* If %c is in the mode line, update it if needed. */
11452 if (!NILP (w->column_number_displayed)
11453 /* This alternative quickly identifies a common case
11454 where no change is needed. */
11455 && !(PT == XFASTINT (w->last_point)
11456 && XFASTINT (w->last_modified) >= MODIFF
11457 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11458 && (XFASTINT (w->column_number_displayed)
11459 != (int) current_column ())) /* iftc */
11460 w->update_mode_line = Qt;
11461
11462 unbind_to (count1, Qnil);
11463
11464 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11465
11466 /* The variable buffer_shared is set in redisplay_window and
11467 indicates that we redisplay a buffer in different windows. See
11468 there. */
11469 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11470 || cursor_type_changed);
11471
11472 /* If specs for an arrow have changed, do thorough redisplay
11473 to ensure we remove any arrow that should no longer exist. */
11474 if (overlay_arrows_changed_p ())
11475 consider_all_windows_p = windows_or_buffers_changed = 1;
11476
11477 /* Normally the message* functions will have already displayed and
11478 updated the echo area, but the frame may have been trashed, or
11479 the update may have been preempted, so display the echo area
11480 again here. Checking message_cleared_p captures the case that
11481 the echo area should be cleared. */
11482 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11483 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11484 || (message_cleared_p
11485 && minibuf_level == 0
11486 /* If the mini-window is currently selected, this means the
11487 echo-area doesn't show through. */
11488 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11489 {
11490 int window_height_changed_p = echo_area_display (0);
11491 must_finish = 1;
11492
11493 /* If we don't display the current message, don't clear the
11494 message_cleared_p flag, because, if we did, we wouldn't clear
11495 the echo area in the next redisplay which doesn't preserve
11496 the echo area. */
11497 if (!display_last_displayed_message_p)
11498 message_cleared_p = 0;
11499
11500 if (fonts_changed_p)
11501 goto retry;
11502 else if (window_height_changed_p)
11503 {
11504 consider_all_windows_p = 1;
11505 ++update_mode_lines;
11506 ++windows_or_buffers_changed;
11507
11508 /* If window configuration was changed, frames may have been
11509 marked garbaged. Clear them or we will experience
11510 surprises wrt scrolling. */
11511 if (frame_garbaged)
11512 clear_garbaged_frames ();
11513 }
11514 }
11515 else if (EQ (selected_window, minibuf_window)
11516 && (current_buffer->clip_changed
11517 || XFASTINT (w->last_modified) < MODIFF
11518 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11519 && resize_mini_window (w, 0))
11520 {
11521 /* Resized active mini-window to fit the size of what it is
11522 showing if its contents might have changed. */
11523 must_finish = 1;
11524 /* FIXME: this causes all frames to be updated, which seems unnecessary
11525 since only the current frame needs to be considered. This function needs
11526 to be rewritten with two variables, consider_all_windows and
11527 consider_all_frames. */
11528 consider_all_windows_p = 1;
11529 ++windows_or_buffers_changed;
11530 ++update_mode_lines;
11531
11532 /* If window configuration was changed, frames may have been
11533 marked garbaged. Clear them or we will experience
11534 surprises wrt scrolling. */
11535 if (frame_garbaged)
11536 clear_garbaged_frames ();
11537 }
11538
11539
11540 /* If showing the region, and mark has changed, we must redisplay
11541 the whole window. The assignment to this_line_start_pos prevents
11542 the optimization directly below this if-statement. */
11543 if (((!NILP (Vtransient_mark_mode)
11544 && !NILP (XBUFFER (w->buffer)->mark_active))
11545 != !NILP (w->region_showing))
11546 || (!NILP (w->region_showing)
11547 && !EQ (w->region_showing,
11548 Fmarker_position (XBUFFER (w->buffer)->mark))))
11549 CHARPOS (this_line_start_pos) = 0;
11550
11551 /* Optimize the case that only the line containing the cursor in the
11552 selected window has changed. Variables starting with this_ are
11553 set in display_line and record information about the line
11554 containing the cursor. */
11555 tlbufpos = this_line_start_pos;
11556 tlendpos = this_line_end_pos;
11557 if (!consider_all_windows_p
11558 && CHARPOS (tlbufpos) > 0
11559 && NILP (w->update_mode_line)
11560 && !current_buffer->clip_changed
11561 && !current_buffer->prevent_redisplay_optimizations_p
11562 && FRAME_VISIBLE_P (XFRAME (w->frame))
11563 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11564 /* Make sure recorded data applies to current buffer, etc. */
11565 && this_line_buffer == current_buffer
11566 && current_buffer == XBUFFER (w->buffer)
11567 && NILP (w->force_start)
11568 && NILP (w->optional_new_start)
11569 /* Point must be on the line that we have info recorded about. */
11570 && PT >= CHARPOS (tlbufpos)
11571 && PT <= Z - CHARPOS (tlendpos)
11572 /* All text outside that line, including its final newline,
11573 must be unchanged. */
11574 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11575 CHARPOS (tlendpos)))
11576 {
11577 if (CHARPOS (tlbufpos) > BEGV
11578 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11579 && (CHARPOS (tlbufpos) == ZV
11580 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11581 /* Former continuation line has disappeared by becoming empty. */
11582 goto cancel;
11583 else if (XFASTINT (w->last_modified) < MODIFF
11584 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11585 || MINI_WINDOW_P (w))
11586 {
11587 /* We have to handle the case of continuation around a
11588 wide-column character (see the comment in indent.c around
11589 line 1340).
11590
11591 For instance, in the following case:
11592
11593 -------- Insert --------
11594 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11595 J_I_ ==> J_I_ `^^' are cursors.
11596 ^^ ^^
11597 -------- --------
11598
11599 As we have to redraw the line above, we cannot use this
11600 optimization. */
11601
11602 struct it it;
11603 int line_height_before = this_line_pixel_height;
11604
11605 /* Note that start_display will handle the case that the
11606 line starting at tlbufpos is a continuation line. */
11607 start_display (&it, w, tlbufpos);
11608
11609 /* Implementation note: It this still necessary? */
11610 if (it.current_x != this_line_start_x)
11611 goto cancel;
11612
11613 TRACE ((stderr, "trying display optimization 1\n"));
11614 w->cursor.vpos = -1;
11615 overlay_arrow_seen = 0;
11616 it.vpos = this_line_vpos;
11617 it.current_y = this_line_y;
11618 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11619 display_line (&it);
11620
11621 /* If line contains point, is not continued,
11622 and ends at same distance from eob as before, we win. */
11623 if (w->cursor.vpos >= 0
11624 /* Line is not continued, otherwise this_line_start_pos
11625 would have been set to 0 in display_line. */
11626 && CHARPOS (this_line_start_pos)
11627 /* Line ends as before. */
11628 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11629 /* Line has same height as before. Otherwise other lines
11630 would have to be shifted up or down. */
11631 && this_line_pixel_height == line_height_before)
11632 {
11633 /* If this is not the window's last line, we must adjust
11634 the charstarts of the lines below. */
11635 if (it.current_y < it.last_visible_y)
11636 {
11637 struct glyph_row *row
11638 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11639 int delta, delta_bytes;
11640
11641 /* We used to distinguish between two cases here,
11642 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11643 when the line ends in a newline or the end of the
11644 buffer's accessible portion. But both cases did
11645 the same, so they were collapsed. */
11646 delta = (Z
11647 - CHARPOS (tlendpos)
11648 - MATRIX_ROW_START_CHARPOS (row));
11649 delta_bytes = (Z_BYTE
11650 - BYTEPOS (tlendpos)
11651 - MATRIX_ROW_START_BYTEPOS (row));
11652
11653 increment_matrix_positions (w->current_matrix,
11654 this_line_vpos + 1,
11655 w->current_matrix->nrows,
11656 delta, delta_bytes);
11657 }
11658
11659 /* If this row displays text now but previously didn't,
11660 or vice versa, w->window_end_vpos may have to be
11661 adjusted. */
11662 if ((it.glyph_row - 1)->displays_text_p)
11663 {
11664 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11665 XSETINT (w->window_end_vpos, this_line_vpos);
11666 }
11667 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11668 && this_line_vpos > 0)
11669 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11670 w->window_end_valid = Qnil;
11671
11672 /* Update hint: No need to try to scroll in update_window. */
11673 w->desired_matrix->no_scrolling_p = 1;
11674
11675 #if GLYPH_DEBUG
11676 *w->desired_matrix->method = 0;
11677 debug_method_add (w, "optimization 1");
11678 #endif
11679 #ifdef HAVE_WINDOW_SYSTEM
11680 update_window_fringes (w, 0);
11681 #endif
11682 goto update;
11683 }
11684 else
11685 goto cancel;
11686 }
11687 else if (/* Cursor position hasn't changed. */
11688 PT == XFASTINT (w->last_point)
11689 /* Make sure the cursor was last displayed
11690 in this window. Otherwise we have to reposition it. */
11691 && 0 <= w->cursor.vpos
11692 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11693 {
11694 if (!must_finish)
11695 {
11696 do_pending_window_change (1);
11697 /* If selected_window changed, redisplay again. */
11698 if (WINDOWP (selected_window)
11699 && (w = XWINDOW (selected_window)) != sw)
11700 goto retry;
11701
11702 /* We used to always goto end_of_redisplay here, but this
11703 isn't enough if we have a blinking cursor. */
11704 if (w->cursor_off_p == w->last_cursor_off_p)
11705 goto end_of_redisplay;
11706 }
11707 goto update;
11708 }
11709 /* If highlighting the region, or if the cursor is in the echo area,
11710 then we can't just move the cursor. */
11711 else if (! (!NILP (Vtransient_mark_mode)
11712 && !NILP (current_buffer->mark_active))
11713 && (EQ (selected_window, current_buffer->last_selected_window)
11714 || highlight_nonselected_windows)
11715 && NILP (w->region_showing)
11716 && NILP (Vshow_trailing_whitespace)
11717 && !cursor_in_echo_area)
11718 {
11719 struct it it;
11720 struct glyph_row *row;
11721
11722 /* Skip from tlbufpos to PT and see where it is. Note that
11723 PT may be in invisible text. If so, we will end at the
11724 next visible position. */
11725 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11726 NULL, DEFAULT_FACE_ID);
11727 it.current_x = this_line_start_x;
11728 it.current_y = this_line_y;
11729 it.vpos = this_line_vpos;
11730
11731 /* The call to move_it_to stops in front of PT, but
11732 moves over before-strings. */
11733 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11734
11735 if (it.vpos == this_line_vpos
11736 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11737 row->enabled_p))
11738 {
11739 xassert (this_line_vpos == it.vpos);
11740 xassert (this_line_y == it.current_y);
11741 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11742 #if GLYPH_DEBUG
11743 *w->desired_matrix->method = 0;
11744 debug_method_add (w, "optimization 3");
11745 #endif
11746 goto update;
11747 }
11748 else
11749 goto cancel;
11750 }
11751
11752 cancel:
11753 /* Text changed drastically or point moved off of line. */
11754 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11755 }
11756
11757 CHARPOS (this_line_start_pos) = 0;
11758 consider_all_windows_p |= buffer_shared > 1;
11759 ++clear_face_cache_count;
11760 #ifdef HAVE_WINDOW_SYSTEM
11761 ++clear_image_cache_count;
11762 #endif
11763
11764 /* Build desired matrices, and update the display. If
11765 consider_all_windows_p is non-zero, do it for all windows on all
11766 frames. Otherwise do it for selected_window, only. */
11767
11768 if (consider_all_windows_p)
11769 {
11770 Lisp_Object tail, frame;
11771
11772 FOR_EACH_FRAME (tail, frame)
11773 XFRAME (frame)->updated_p = 0;
11774
11775 /* Recompute # windows showing selected buffer. This will be
11776 incremented each time such a window is displayed. */
11777 buffer_shared = 0;
11778
11779 FOR_EACH_FRAME (tail, frame)
11780 {
11781 struct frame *f = XFRAME (frame);
11782
11783 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11784 {
11785 if (! EQ (frame, selected_frame))
11786 /* Select the frame, for the sake of frame-local
11787 variables. */
11788 select_frame_for_redisplay (frame);
11789
11790 /* Mark all the scroll bars to be removed; we'll redeem
11791 the ones we want when we redisplay their windows. */
11792 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11793 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11794
11795 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11796 redisplay_windows (FRAME_ROOT_WINDOW (f));
11797
11798 /* The X error handler may have deleted that frame. */
11799 if (!FRAME_LIVE_P (f))
11800 continue;
11801
11802 /* Any scroll bars which redisplay_windows should have
11803 nuked should now go away. */
11804 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11805 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11806
11807 /* If fonts changed, display again. */
11808 /* ??? rms: I suspect it is a mistake to jump all the way
11809 back to retry here. It should just retry this frame. */
11810 if (fonts_changed_p)
11811 goto retry;
11812
11813 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11814 {
11815 /* See if we have to hscroll. */
11816 if (!f->already_hscrolled_p)
11817 {
11818 f->already_hscrolled_p = 1;
11819 if (hscroll_windows (f->root_window))
11820 goto retry;
11821 }
11822
11823 /* Prevent various kinds of signals during display
11824 update. stdio is not robust about handling
11825 signals, which can cause an apparent I/O
11826 error. */
11827 if (interrupt_input)
11828 unrequest_sigio ();
11829 STOP_POLLING;
11830
11831 /* Update the display. */
11832 set_window_update_flags (XWINDOW (f->root_window), 1);
11833 pause |= update_frame (f, 0, 0);
11834 f->updated_p = 1;
11835 }
11836 }
11837 }
11838
11839 if (!EQ (old_frame, selected_frame)
11840 && FRAME_LIVE_P (XFRAME (old_frame)))
11841 /* We played a bit fast-and-loose above and allowed selected_frame
11842 and selected_window to be temporarily out-of-sync but let's make
11843 sure this stays contained. */
11844 select_frame_for_redisplay (old_frame);
11845 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11846
11847 if (!pause)
11848 {
11849 /* Do the mark_window_display_accurate after all windows have
11850 been redisplayed because this call resets flags in buffers
11851 which are needed for proper redisplay. */
11852 FOR_EACH_FRAME (tail, frame)
11853 {
11854 struct frame *f = XFRAME (frame);
11855 if (f->updated_p)
11856 {
11857 mark_window_display_accurate (f->root_window, 1);
11858 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11859 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11860 }
11861 }
11862 }
11863 }
11864 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11865 {
11866 Lisp_Object mini_window;
11867 struct frame *mini_frame;
11868
11869 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11870 /* Use list_of_error, not Qerror, so that
11871 we catch only errors and don't run the debugger. */
11872 internal_condition_case_1 (redisplay_window_1, selected_window,
11873 list_of_error,
11874 redisplay_window_error);
11875
11876 /* Compare desired and current matrices, perform output. */
11877
11878 update:
11879 /* If fonts changed, display again. */
11880 if (fonts_changed_p)
11881 goto retry;
11882
11883 /* Prevent various kinds of signals during display update.
11884 stdio is not robust about handling signals,
11885 which can cause an apparent I/O error. */
11886 if (interrupt_input)
11887 unrequest_sigio ();
11888 STOP_POLLING;
11889
11890 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11891 {
11892 if (hscroll_windows (selected_window))
11893 goto retry;
11894
11895 XWINDOW (selected_window)->must_be_updated_p = 1;
11896 pause = update_frame (sf, 0, 0);
11897 }
11898
11899 /* We may have called echo_area_display at the top of this
11900 function. If the echo area is on another frame, that may
11901 have put text on a frame other than the selected one, so the
11902 above call to update_frame would not have caught it. Catch
11903 it here. */
11904 mini_window = FRAME_MINIBUF_WINDOW (sf);
11905 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11906
11907 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11908 {
11909 XWINDOW (mini_window)->must_be_updated_p = 1;
11910 pause |= update_frame (mini_frame, 0, 0);
11911 if (!pause && hscroll_windows (mini_window))
11912 goto retry;
11913 }
11914 }
11915
11916 /* If display was paused because of pending input, make sure we do a
11917 thorough update the next time. */
11918 if (pause)
11919 {
11920 /* Prevent the optimization at the beginning of
11921 redisplay_internal that tries a single-line update of the
11922 line containing the cursor in the selected window. */
11923 CHARPOS (this_line_start_pos) = 0;
11924
11925 /* Let the overlay arrow be updated the next time. */
11926 update_overlay_arrows (0);
11927
11928 /* If we pause after scrolling, some rows in the current
11929 matrices of some windows are not valid. */
11930 if (!WINDOW_FULL_WIDTH_P (w)
11931 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11932 update_mode_lines = 1;
11933 }
11934 else
11935 {
11936 if (!consider_all_windows_p)
11937 {
11938 /* This has already been done above if
11939 consider_all_windows_p is set. */
11940 mark_window_display_accurate_1 (w, 1);
11941
11942 /* Say overlay arrows are up to date. */
11943 update_overlay_arrows (1);
11944
11945 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11946 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11947 }
11948
11949 update_mode_lines = 0;
11950 windows_or_buffers_changed = 0;
11951 cursor_type_changed = 0;
11952 }
11953
11954 /* Start SIGIO interrupts coming again. Having them off during the
11955 code above makes it less likely one will discard output, but not
11956 impossible, since there might be stuff in the system buffer here.
11957 But it is much hairier to try to do anything about that. */
11958 if (interrupt_input)
11959 request_sigio ();
11960 RESUME_POLLING;
11961
11962 /* If a frame has become visible which was not before, redisplay
11963 again, so that we display it. Expose events for such a frame
11964 (which it gets when becoming visible) don't call the parts of
11965 redisplay constructing glyphs, so simply exposing a frame won't
11966 display anything in this case. So, we have to display these
11967 frames here explicitly. */
11968 if (!pause)
11969 {
11970 Lisp_Object tail, frame;
11971 int new_count = 0;
11972
11973 FOR_EACH_FRAME (tail, frame)
11974 {
11975 int this_is_visible = 0;
11976
11977 if (XFRAME (frame)->visible)
11978 this_is_visible = 1;
11979 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11980 if (XFRAME (frame)->visible)
11981 this_is_visible = 1;
11982
11983 if (this_is_visible)
11984 new_count++;
11985 }
11986
11987 if (new_count != number_of_visible_frames)
11988 windows_or_buffers_changed++;
11989 }
11990
11991 /* Change frame size now if a change is pending. */
11992 do_pending_window_change (1);
11993
11994 /* If we just did a pending size change, or have additional
11995 visible frames, or selected_window changed, redisplay again. */
11996 if ((windows_or_buffers_changed && !pause)
11997 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
11998 goto retry;
11999
12000 /* Clear the face cache eventually. */
12001 if (consider_all_windows_p)
12002 {
12003 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12004 {
12005 clear_face_cache (0);
12006 clear_face_cache_count = 0;
12007 }
12008 #ifdef HAVE_WINDOW_SYSTEM
12009 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12010 {
12011 clear_image_caches (Qnil);
12012 clear_image_cache_count = 0;
12013 }
12014 #endif /* HAVE_WINDOW_SYSTEM */
12015 }
12016
12017 end_of_redisplay:
12018 unbind_to (count, Qnil);
12019 RESUME_POLLING;
12020 }
12021
12022
12023 /* Redisplay, but leave alone any recent echo area message unless
12024 another message has been requested in its place.
12025
12026 This is useful in situations where you need to redisplay but no
12027 user action has occurred, making it inappropriate for the message
12028 area to be cleared. See tracking_off and
12029 wait_reading_process_output for examples of these situations.
12030
12031 FROM_WHERE is an integer saying from where this function was
12032 called. This is useful for debugging. */
12033
12034 void
12035 redisplay_preserve_echo_area (from_where)
12036 int from_where;
12037 {
12038 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12039
12040 if (!NILP (echo_area_buffer[1]))
12041 {
12042 /* We have a previously displayed message, but no current
12043 message. Redisplay the previous message. */
12044 display_last_displayed_message_p = 1;
12045 redisplay_internal (1);
12046 display_last_displayed_message_p = 0;
12047 }
12048 else
12049 redisplay_internal (1);
12050
12051 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12052 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12053 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12054 }
12055
12056
12057 /* Function registered with record_unwind_protect in
12058 redisplay_internal. Reset redisplaying_p to the value it had
12059 before redisplay_internal was called, and clear
12060 prevent_freeing_realized_faces_p. It also selects the previously
12061 selected frame, unless it has been deleted (by an X connection
12062 failure during redisplay, for example). */
12063
12064 static Lisp_Object
12065 unwind_redisplay (val)
12066 Lisp_Object val;
12067 {
12068 Lisp_Object old_redisplaying_p, old_frame;
12069
12070 old_redisplaying_p = XCAR (val);
12071 redisplaying_p = XFASTINT (old_redisplaying_p);
12072 old_frame = XCDR (val);
12073 if (! EQ (old_frame, selected_frame)
12074 && FRAME_LIVE_P (XFRAME (old_frame)))
12075 select_frame_for_redisplay (old_frame);
12076 return Qnil;
12077 }
12078
12079
12080 /* Mark the display of window W as accurate or inaccurate. If
12081 ACCURATE_P is non-zero mark display of W as accurate. If
12082 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12083 redisplay_internal is called. */
12084
12085 static void
12086 mark_window_display_accurate_1 (w, accurate_p)
12087 struct window *w;
12088 int accurate_p;
12089 {
12090 if (BUFFERP (w->buffer))
12091 {
12092 struct buffer *b = XBUFFER (w->buffer);
12093
12094 w->last_modified
12095 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12096 w->last_overlay_modified
12097 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12098 w->last_had_star
12099 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12100
12101 if (accurate_p)
12102 {
12103 b->clip_changed = 0;
12104 b->prevent_redisplay_optimizations_p = 0;
12105
12106 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12107 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12108 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12109 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12110
12111 w->current_matrix->buffer = b;
12112 w->current_matrix->begv = BUF_BEGV (b);
12113 w->current_matrix->zv = BUF_ZV (b);
12114
12115 w->last_cursor = w->cursor;
12116 w->last_cursor_off_p = w->cursor_off_p;
12117
12118 if (w == XWINDOW (selected_window))
12119 w->last_point = make_number (BUF_PT (b));
12120 else
12121 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12122 }
12123 }
12124
12125 if (accurate_p)
12126 {
12127 w->window_end_valid = w->buffer;
12128 w->update_mode_line = Qnil;
12129 }
12130 }
12131
12132
12133 /* Mark the display of windows in the window tree rooted at WINDOW as
12134 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12135 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12136 be redisplayed the next time redisplay_internal is called. */
12137
12138 void
12139 mark_window_display_accurate (window, accurate_p)
12140 Lisp_Object window;
12141 int accurate_p;
12142 {
12143 struct window *w;
12144
12145 for (; !NILP (window); window = w->next)
12146 {
12147 w = XWINDOW (window);
12148 mark_window_display_accurate_1 (w, accurate_p);
12149
12150 if (!NILP (w->vchild))
12151 mark_window_display_accurate (w->vchild, accurate_p);
12152 if (!NILP (w->hchild))
12153 mark_window_display_accurate (w->hchild, accurate_p);
12154 }
12155
12156 if (accurate_p)
12157 {
12158 update_overlay_arrows (1);
12159 }
12160 else
12161 {
12162 /* Force a thorough redisplay the next time by setting
12163 last_arrow_position and last_arrow_string to t, which is
12164 unequal to any useful value of Voverlay_arrow_... */
12165 update_overlay_arrows (-1);
12166 }
12167 }
12168
12169
12170 /* Return value in display table DP (Lisp_Char_Table *) for character
12171 C. Since a display table doesn't have any parent, we don't have to
12172 follow parent. Do not call this function directly but use the
12173 macro DISP_CHAR_VECTOR. */
12174
12175 Lisp_Object
12176 disp_char_vector (dp, c)
12177 struct Lisp_Char_Table *dp;
12178 int c;
12179 {
12180 Lisp_Object val;
12181
12182 if (ASCII_CHAR_P (c))
12183 {
12184 val = dp->ascii;
12185 if (SUB_CHAR_TABLE_P (val))
12186 val = XSUB_CHAR_TABLE (val)->contents[c];
12187 }
12188 else
12189 {
12190 Lisp_Object table;
12191
12192 XSETCHAR_TABLE (table, dp);
12193 val = char_table_ref (table, c);
12194 }
12195 if (NILP (val))
12196 val = dp->defalt;
12197 return val;
12198 }
12199
12200
12201 \f
12202 /***********************************************************************
12203 Window Redisplay
12204 ***********************************************************************/
12205
12206 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12207
12208 static void
12209 redisplay_windows (window)
12210 Lisp_Object window;
12211 {
12212 while (!NILP (window))
12213 {
12214 struct window *w = XWINDOW (window);
12215
12216 if (!NILP (w->hchild))
12217 redisplay_windows (w->hchild);
12218 else if (!NILP (w->vchild))
12219 redisplay_windows (w->vchild);
12220 else if (!NILP (w->buffer))
12221 {
12222 displayed_buffer = XBUFFER (w->buffer);
12223 /* Use list_of_error, not Qerror, so that
12224 we catch only errors and don't run the debugger. */
12225 internal_condition_case_1 (redisplay_window_0, window,
12226 list_of_error,
12227 redisplay_window_error);
12228 }
12229
12230 window = w->next;
12231 }
12232 }
12233
12234 static Lisp_Object
12235 redisplay_window_error ()
12236 {
12237 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12238 return Qnil;
12239 }
12240
12241 static Lisp_Object
12242 redisplay_window_0 (window)
12243 Lisp_Object window;
12244 {
12245 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12246 redisplay_window (window, 0);
12247 return Qnil;
12248 }
12249
12250 static Lisp_Object
12251 redisplay_window_1 (window)
12252 Lisp_Object window;
12253 {
12254 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12255 redisplay_window (window, 1);
12256 return Qnil;
12257 }
12258 \f
12259
12260 /* Increment GLYPH until it reaches END or CONDITION fails while
12261 adding (GLYPH)->pixel_width to X. */
12262
12263 #define SKIP_GLYPHS(glyph, end, x, condition) \
12264 do \
12265 { \
12266 (x) += (glyph)->pixel_width; \
12267 ++(glyph); \
12268 } \
12269 while ((glyph) < (end) && (condition))
12270
12271
12272 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12273 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12274 which positions recorded in ROW differ from current buffer
12275 positions.
12276
12277 Return 0 if cursor is not on this row, 1 otherwise. */
12278
12279 int
12280 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12281 struct window *w;
12282 struct glyph_row *row;
12283 struct glyph_matrix *matrix;
12284 int delta, delta_bytes, dy, dvpos;
12285 {
12286 struct glyph *glyph = row->glyphs[TEXT_AREA];
12287 struct glyph *end = glyph + row->used[TEXT_AREA];
12288 struct glyph *cursor = NULL;
12289 /* The first glyph that starts a sequence of glyphs from a string
12290 that is a value of a display property. */
12291 struct glyph *string_start;
12292 /* The X coordinate of string_start. */
12293 int string_start_x;
12294 /* The last known character position in row. */
12295 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12296 /* The last known character position before string_start. */
12297 int string_before_pos;
12298 int x = row->x;
12299 int cursor_x = x;
12300 /* Last buffer position covered by an overlay. */
12301 int cursor_from_overlay_pos = 0;
12302 int pt_old = PT - delta;
12303
12304 /* Skip over glyphs not having an object at the start of the row.
12305 These are special glyphs like truncation marks on terminal
12306 frames. */
12307 if (row->displays_text_p)
12308 while (glyph < end
12309 && INTEGERP (glyph->object)
12310 && glyph->charpos < 0)
12311 {
12312 x += glyph->pixel_width;
12313 ++glyph;
12314 }
12315
12316 string_start = NULL;
12317 while (glyph < end
12318 && !INTEGERP (glyph->object)
12319 && (!BUFFERP (glyph->object)
12320 || (last_pos = glyph->charpos) < pt_old
12321 || glyph->avoid_cursor_p))
12322 {
12323 if (! STRINGP (glyph->object))
12324 {
12325 string_start = NULL;
12326 x += glyph->pixel_width;
12327 ++glyph;
12328 /* If we are beyond the cursor position computed from the
12329 last overlay seen, that overlay is not in effect for
12330 current cursor position. Reset the cursor information
12331 computed from that overlay. */
12332 if (cursor_from_overlay_pos
12333 && last_pos >= cursor_from_overlay_pos)
12334 {
12335 cursor_from_overlay_pos = 0;
12336 cursor = NULL;
12337 }
12338 }
12339 else
12340 {
12341 if (string_start == NULL)
12342 {
12343 string_before_pos = last_pos;
12344 string_start = glyph;
12345 string_start_x = x;
12346 }
12347 /* Skip all glyphs from a string. */
12348 do
12349 {
12350 Lisp_Object cprop;
12351 int pos;
12352 if ((cursor == NULL || glyph > cursor)
12353 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12354 Qcursor, (glyph)->object),
12355 !NILP (cprop))
12356 && (pos = string_buffer_position (w, glyph->object,
12357 string_before_pos),
12358 (pos == 0 /* from overlay */
12359 || pos == pt_old)))
12360 {
12361 /* Compute the first buffer position after the overlay.
12362 If the `cursor' property tells us how many positions
12363 are associated with the overlay, use that. Otherwise,
12364 estimate from the buffer positions of the glyphs
12365 before and after the overlay. */
12366 cursor_from_overlay_pos = (pos ? 0 : last_pos
12367 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12368 cursor = glyph;
12369 cursor_x = x;
12370 }
12371 x += glyph->pixel_width;
12372 ++glyph;
12373 }
12374 while (glyph < end && EQ (glyph->object, string_start->object));
12375 }
12376 }
12377
12378 if (cursor != NULL)
12379 {
12380 glyph = cursor;
12381 x = cursor_x;
12382 }
12383 else if (row->ends_in_ellipsis_p && glyph == end)
12384 {
12385 /* Scan back over the ellipsis glyphs, decrementing positions. */
12386 while (glyph > row->glyphs[TEXT_AREA]
12387 && (glyph - 1)->charpos == last_pos)
12388 glyph--, x -= glyph->pixel_width;
12389 /* That loop always goes one position too far, including the
12390 glyph before the ellipsis. So scan forward over that one. */
12391 x += glyph->pixel_width;
12392 glyph++;
12393 }
12394 else if (string_start
12395 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12396 {
12397 /* We may have skipped over point because the previous glyphs
12398 are from string. As there's no easy way to know the
12399 character position of the current glyph, find the correct
12400 glyph on point by scanning from string_start again. */
12401 Lisp_Object limit;
12402 Lisp_Object string;
12403 struct glyph *stop = glyph;
12404 int pos;
12405
12406 limit = make_number (pt_old + 1);
12407 glyph = string_start;
12408 x = string_start_x;
12409 string = glyph->object;
12410 pos = string_buffer_position (w, string, string_before_pos);
12411 /* If POS == 0, STRING is from overlay. We skip such glyphs
12412 because we always put the cursor after overlay strings. */
12413 while (pos == 0 && glyph < stop)
12414 {
12415 string = glyph->object;
12416 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12417 if (glyph < stop)
12418 pos = string_buffer_position (w, glyph->object, string_before_pos);
12419 }
12420
12421 while (glyph < stop)
12422 {
12423 pos = XINT (Fnext_single_char_property_change
12424 (make_number (pos), Qdisplay, Qnil, limit));
12425 if (pos > pt_old)
12426 break;
12427 /* Skip glyphs from the same string. */
12428 string = glyph->object;
12429 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12430 /* Skip glyphs from an overlay. */
12431 while (glyph < stop
12432 && ! string_buffer_position (w, glyph->object, pos))
12433 {
12434 string = glyph->object;
12435 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12436 }
12437 }
12438
12439 /* If we reached the end of the line, and END was from a string,
12440 the cursor is not on this line. */
12441 if (glyph == end && row->continued_p)
12442 return 0;
12443 }
12444
12445 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12446 w->cursor.x = x;
12447 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12448 w->cursor.y = row->y + dy;
12449
12450 if (w == XWINDOW (selected_window))
12451 {
12452 if (!row->continued_p
12453 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12454 && row->x == 0)
12455 {
12456 this_line_buffer = XBUFFER (w->buffer);
12457
12458 CHARPOS (this_line_start_pos)
12459 = MATRIX_ROW_START_CHARPOS (row) + delta;
12460 BYTEPOS (this_line_start_pos)
12461 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12462
12463 CHARPOS (this_line_end_pos)
12464 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12465 BYTEPOS (this_line_end_pos)
12466 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12467
12468 this_line_y = w->cursor.y;
12469 this_line_pixel_height = row->height;
12470 this_line_vpos = w->cursor.vpos;
12471 this_line_start_x = row->x;
12472 }
12473 else
12474 CHARPOS (this_line_start_pos) = 0;
12475 }
12476
12477 return 1;
12478 }
12479
12480
12481 /* Run window scroll functions, if any, for WINDOW with new window
12482 start STARTP. Sets the window start of WINDOW to that position.
12483
12484 We assume that the window's buffer is really current. */
12485
12486 static INLINE struct text_pos
12487 run_window_scroll_functions (window, startp)
12488 Lisp_Object window;
12489 struct text_pos startp;
12490 {
12491 struct window *w = XWINDOW (window);
12492 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12493
12494 if (current_buffer != XBUFFER (w->buffer))
12495 abort ();
12496
12497 if (!NILP (Vwindow_scroll_functions))
12498 {
12499 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12500 make_number (CHARPOS (startp)));
12501 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12502 /* In case the hook functions switch buffers. */
12503 if (current_buffer != XBUFFER (w->buffer))
12504 set_buffer_internal_1 (XBUFFER (w->buffer));
12505 }
12506
12507 return startp;
12508 }
12509
12510
12511 /* Make sure the line containing the cursor is fully visible.
12512 A value of 1 means there is nothing to be done.
12513 (Either the line is fully visible, or it cannot be made so,
12514 or we cannot tell.)
12515
12516 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12517 is higher than window.
12518
12519 A value of 0 means the caller should do scrolling
12520 as if point had gone off the screen. */
12521
12522 static int
12523 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12524 struct window *w;
12525 int force_p;
12526 int current_matrix_p;
12527 {
12528 struct glyph_matrix *matrix;
12529 struct glyph_row *row;
12530 int window_height;
12531
12532 if (!make_cursor_line_fully_visible_p)
12533 return 1;
12534
12535 /* It's not always possible to find the cursor, e.g, when a window
12536 is full of overlay strings. Don't do anything in that case. */
12537 if (w->cursor.vpos < 0)
12538 return 1;
12539
12540 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12541 row = MATRIX_ROW (matrix, w->cursor.vpos);
12542
12543 /* If the cursor row is not partially visible, there's nothing to do. */
12544 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12545 return 1;
12546
12547 /* If the row the cursor is in is taller than the window's height,
12548 it's not clear what to do, so do nothing. */
12549 window_height = window_box_height (w);
12550 if (row->height >= window_height)
12551 {
12552 if (!force_p || MINI_WINDOW_P (w)
12553 || w->vscroll || w->cursor.vpos == 0)
12554 return 1;
12555 }
12556 return 0;
12557 }
12558
12559
12560 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12561 non-zero means only WINDOW is redisplayed in redisplay_internal.
12562 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12563 in redisplay_window to bring a partially visible line into view in
12564 the case that only the cursor has moved.
12565
12566 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12567 last screen line's vertical height extends past the end of the screen.
12568
12569 Value is
12570
12571 1 if scrolling succeeded
12572
12573 0 if scrolling didn't find point.
12574
12575 -1 if new fonts have been loaded so that we must interrupt
12576 redisplay, adjust glyph matrices, and try again. */
12577
12578 enum
12579 {
12580 SCROLLING_SUCCESS,
12581 SCROLLING_FAILED,
12582 SCROLLING_NEED_LARGER_MATRICES
12583 };
12584
12585 static int
12586 try_scrolling (window, just_this_one_p, scroll_conservatively,
12587 scroll_step, temp_scroll_step, last_line_misfit)
12588 Lisp_Object window;
12589 int just_this_one_p;
12590 EMACS_INT scroll_conservatively, scroll_step;
12591 int temp_scroll_step;
12592 int last_line_misfit;
12593 {
12594 struct window *w = XWINDOW (window);
12595 struct frame *f = XFRAME (w->frame);
12596 struct text_pos pos, startp;
12597 struct it it;
12598 int this_scroll_margin, scroll_max, rc, height;
12599 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12600 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12601 Lisp_Object aggressive;
12602 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12603
12604 #if GLYPH_DEBUG
12605 debug_method_add (w, "try_scrolling");
12606 #endif
12607
12608 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12609
12610 /* Compute scroll margin height in pixels. We scroll when point is
12611 within this distance from the top or bottom of the window. */
12612 if (scroll_margin > 0)
12613 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12614 * FRAME_LINE_HEIGHT (f);
12615 else
12616 this_scroll_margin = 0;
12617
12618 /* Force scroll_conservatively to have a reasonable value, to avoid
12619 overflow while computing how much to scroll. Note that the user
12620 can supply scroll-conservatively equal to `most-positive-fixnum',
12621 which can be larger than INT_MAX. */
12622 if (scroll_conservatively > scroll_limit)
12623 {
12624 scroll_conservatively = scroll_limit;
12625 scroll_max = INT_MAX;
12626 }
12627 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12628 /* Compute how much we should try to scroll maximally to bring
12629 point into view. */
12630 scroll_max = (max (scroll_step,
12631 max (scroll_conservatively, temp_scroll_step))
12632 * FRAME_LINE_HEIGHT (f));
12633 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12634 || NUMBERP (current_buffer->scroll_up_aggressively))
12635 /* We're trying to scroll because of aggressive scrolling but no
12636 scroll_step is set. Choose an arbitrary one. */
12637 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12638 else
12639 scroll_max = 0;
12640
12641 too_near_end:
12642
12643 /* Decide whether to scroll down. */
12644 if (PT > CHARPOS (startp))
12645 {
12646 int scroll_margin_y;
12647
12648 /* Compute the pixel ypos of the scroll margin, then move it to
12649 either that ypos or PT, whichever comes first. */
12650 start_display (&it, w, startp);
12651 scroll_margin_y = it.last_visible_y - this_scroll_margin
12652 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12653 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12654 (MOVE_TO_POS | MOVE_TO_Y));
12655
12656 if (PT > CHARPOS (it.current.pos))
12657 {
12658 int y0 = line_bottom_y (&it);
12659
12660 /* Compute the distance from the scroll margin to PT
12661 (including the height of the cursor line). Moving the
12662 iterator unconditionally to PT can be slow if PT is far
12663 away, so stop 10 lines past the window bottom (is there a
12664 way to do the right thing quickly?). */
12665 move_it_to (&it, PT, -1,
12666 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12667 -1, MOVE_TO_POS | MOVE_TO_Y);
12668 dy = line_bottom_y (&it) - y0;
12669
12670 if (dy > scroll_max)
12671 return SCROLLING_FAILED;
12672
12673 scroll_down_p = 1;
12674 }
12675 }
12676
12677 if (scroll_down_p)
12678 {
12679 /* Point is in or below the bottom scroll margin, so move the
12680 window start down. If scrolling conservatively, move it just
12681 enough down to make point visible. If scroll_step is set,
12682 move it down by scroll_step. */
12683 if (scroll_conservatively)
12684 amount_to_scroll
12685 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12686 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12687 else if (scroll_step || temp_scroll_step)
12688 amount_to_scroll = scroll_max;
12689 else
12690 {
12691 aggressive = current_buffer->scroll_up_aggressively;
12692 height = WINDOW_BOX_TEXT_HEIGHT (w);
12693 if (NUMBERP (aggressive))
12694 {
12695 double float_amount = XFLOATINT (aggressive) * height;
12696 amount_to_scroll = float_amount;
12697 if (amount_to_scroll == 0 && float_amount > 0)
12698 amount_to_scroll = 1;
12699 }
12700 }
12701
12702 if (amount_to_scroll <= 0)
12703 return SCROLLING_FAILED;
12704
12705 start_display (&it, w, startp);
12706 move_it_vertically (&it, amount_to_scroll);
12707
12708 /* If STARTP is unchanged, move it down another screen line. */
12709 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12710 move_it_by_lines (&it, 1, 1);
12711 startp = it.current.pos;
12712 }
12713 else
12714 {
12715 struct text_pos scroll_margin_pos = startp;
12716
12717 /* See if point is inside the scroll margin at the top of the
12718 window. */
12719 if (this_scroll_margin)
12720 {
12721 start_display (&it, w, startp);
12722 move_it_vertically (&it, this_scroll_margin);
12723 scroll_margin_pos = it.current.pos;
12724 }
12725
12726 if (PT < CHARPOS (scroll_margin_pos))
12727 {
12728 /* Point is in the scroll margin at the top of the window or
12729 above what is displayed in the window. */
12730 int y0;
12731
12732 /* Compute the vertical distance from PT to the scroll
12733 margin position. Give up if distance is greater than
12734 scroll_max. */
12735 SET_TEXT_POS (pos, PT, PT_BYTE);
12736 start_display (&it, w, pos);
12737 y0 = it.current_y;
12738 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12739 it.last_visible_y, -1,
12740 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12741 dy = it.current_y - y0;
12742 if (dy > scroll_max)
12743 return SCROLLING_FAILED;
12744
12745 /* Compute new window start. */
12746 start_display (&it, w, startp);
12747
12748 if (scroll_conservatively)
12749 amount_to_scroll
12750 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12751 else if (scroll_step || temp_scroll_step)
12752 amount_to_scroll = scroll_max;
12753 else
12754 {
12755 aggressive = current_buffer->scroll_down_aggressively;
12756 height = WINDOW_BOX_TEXT_HEIGHT (w);
12757 if (NUMBERP (aggressive))
12758 {
12759 double float_amount = XFLOATINT (aggressive) * height;
12760 amount_to_scroll = float_amount;
12761 if (amount_to_scroll == 0 && float_amount > 0)
12762 amount_to_scroll = 1;
12763 }
12764 }
12765
12766 if (amount_to_scroll <= 0)
12767 return SCROLLING_FAILED;
12768
12769 move_it_vertically_backward (&it, amount_to_scroll);
12770 startp = it.current.pos;
12771 }
12772 }
12773
12774 /* Run window scroll functions. */
12775 startp = run_window_scroll_functions (window, startp);
12776
12777 /* Display the window. Give up if new fonts are loaded, or if point
12778 doesn't appear. */
12779 if (!try_window (window, startp, 0))
12780 rc = SCROLLING_NEED_LARGER_MATRICES;
12781 else if (w->cursor.vpos < 0)
12782 {
12783 clear_glyph_matrix (w->desired_matrix);
12784 rc = SCROLLING_FAILED;
12785 }
12786 else
12787 {
12788 /* Maybe forget recorded base line for line number display. */
12789 if (!just_this_one_p
12790 || current_buffer->clip_changed
12791 || BEG_UNCHANGED < CHARPOS (startp))
12792 w->base_line_number = Qnil;
12793
12794 /* If cursor ends up on a partially visible line,
12795 treat that as being off the bottom of the screen. */
12796 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
12797 /* It's possible that the cursor is on the first line of the
12798 buffer, which is partially obscured due to a vscroll
12799 (Bug#7537). In that case, avoid looping forever . */
12800 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
12801 {
12802 clear_glyph_matrix (w->desired_matrix);
12803 ++extra_scroll_margin_lines;
12804 goto too_near_end;
12805 }
12806 rc = SCROLLING_SUCCESS;
12807 }
12808
12809 return rc;
12810 }
12811
12812
12813 /* Compute a suitable window start for window W if display of W starts
12814 on a continuation line. Value is non-zero if a new window start
12815 was computed.
12816
12817 The new window start will be computed, based on W's width, starting
12818 from the start of the continued line. It is the start of the
12819 screen line with the minimum distance from the old start W->start. */
12820
12821 static int
12822 compute_window_start_on_continuation_line (w)
12823 struct window *w;
12824 {
12825 struct text_pos pos, start_pos;
12826 int window_start_changed_p = 0;
12827
12828 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12829
12830 /* If window start is on a continuation line... Window start may be
12831 < BEGV in case there's invisible text at the start of the
12832 buffer (M-x rmail, for example). */
12833 if (CHARPOS (start_pos) > BEGV
12834 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12835 {
12836 struct it it;
12837 struct glyph_row *row;
12838
12839 /* Handle the case that the window start is out of range. */
12840 if (CHARPOS (start_pos) < BEGV)
12841 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12842 else if (CHARPOS (start_pos) > ZV)
12843 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12844
12845 /* Find the start of the continued line. This should be fast
12846 because scan_buffer is fast (newline cache). */
12847 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12848 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12849 row, DEFAULT_FACE_ID);
12850 reseat_at_previous_visible_line_start (&it);
12851
12852 /* If the line start is "too far" away from the window start,
12853 say it takes too much time to compute a new window start. */
12854 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12855 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12856 {
12857 int min_distance, distance;
12858
12859 /* Move forward by display lines to find the new window
12860 start. If window width was enlarged, the new start can
12861 be expected to be > the old start. If window width was
12862 decreased, the new window start will be < the old start.
12863 So, we're looking for the display line start with the
12864 minimum distance from the old window start. */
12865 pos = it.current.pos;
12866 min_distance = INFINITY;
12867 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12868 distance < min_distance)
12869 {
12870 min_distance = distance;
12871 pos = it.current.pos;
12872 move_it_by_lines (&it, 1, 0);
12873 }
12874
12875 /* Set the window start there. */
12876 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12877 window_start_changed_p = 1;
12878 }
12879 }
12880
12881 return window_start_changed_p;
12882 }
12883
12884
12885 /* Try cursor movement in case text has not changed in window WINDOW,
12886 with window start STARTP. Value is
12887
12888 CURSOR_MOVEMENT_SUCCESS if successful
12889
12890 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12891
12892 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12893 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12894 we want to scroll as if scroll-step were set to 1. See the code.
12895
12896 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12897 which case we have to abort this redisplay, and adjust matrices
12898 first. */
12899
12900 enum
12901 {
12902 CURSOR_MOVEMENT_SUCCESS,
12903 CURSOR_MOVEMENT_CANNOT_BE_USED,
12904 CURSOR_MOVEMENT_MUST_SCROLL,
12905 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12906 };
12907
12908 static int
12909 try_cursor_movement (window, startp, scroll_step)
12910 Lisp_Object window;
12911 struct text_pos startp;
12912 int *scroll_step;
12913 {
12914 struct window *w = XWINDOW (window);
12915 struct frame *f = XFRAME (w->frame);
12916 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12917
12918 #if GLYPH_DEBUG
12919 if (inhibit_try_cursor_movement)
12920 return rc;
12921 #endif
12922
12923 /* Handle case where text has not changed, only point, and it has
12924 not moved off the frame. */
12925 if (/* Point may be in this window. */
12926 PT >= CHARPOS (startp)
12927 /* Selective display hasn't changed. */
12928 && !current_buffer->clip_changed
12929 /* Function force-mode-line-update is used to force a thorough
12930 redisplay. It sets either windows_or_buffers_changed or
12931 update_mode_lines. So don't take a shortcut here for these
12932 cases. */
12933 && !update_mode_lines
12934 && !windows_or_buffers_changed
12935 && !cursor_type_changed
12936 /* Can't use this case if highlighting a region. When a
12937 region exists, cursor movement has to do more than just
12938 set the cursor. */
12939 && !(!NILP (Vtransient_mark_mode)
12940 && !NILP (current_buffer->mark_active))
12941 && NILP (w->region_showing)
12942 && NILP (Vshow_trailing_whitespace)
12943 /* Right after splitting windows, last_point may be nil. */
12944 && INTEGERP (w->last_point)
12945 /* This code is not used for mini-buffer for the sake of the case
12946 of redisplaying to replace an echo area message; since in
12947 that case the mini-buffer contents per se are usually
12948 unchanged. This code is of no real use in the mini-buffer
12949 since the handling of this_line_start_pos, etc., in redisplay
12950 handles the same cases. */
12951 && !EQ (window, minibuf_window)
12952 /* When splitting windows or for new windows, it happens that
12953 redisplay is called with a nil window_end_vpos or one being
12954 larger than the window. This should really be fixed in
12955 window.c. I don't have this on my list, now, so we do
12956 approximately the same as the old redisplay code. --gerd. */
12957 && INTEGERP (w->window_end_vpos)
12958 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12959 && (FRAME_WINDOW_P (f)
12960 || !overlay_arrow_in_current_buffer_p ()))
12961 {
12962 int this_scroll_margin, top_scroll_margin;
12963 struct glyph_row *row = NULL;
12964
12965 #if GLYPH_DEBUG
12966 debug_method_add (w, "cursor movement");
12967 #endif
12968
12969 /* Scroll if point within this distance from the top or bottom
12970 of the window. This is a pixel value. */
12971 if (scroll_margin > 0)
12972 {
12973 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12974 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12975 }
12976 else
12977 this_scroll_margin = 0;
12978
12979 top_scroll_margin = this_scroll_margin;
12980 if (WINDOW_WANTS_HEADER_LINE_P (w))
12981 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12982
12983 /* Start with the row the cursor was displayed during the last
12984 not paused redisplay. Give up if that row is not valid. */
12985 if (w->last_cursor.vpos < 0
12986 || w->last_cursor.vpos >= w->current_matrix->nrows)
12987 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12988 else
12989 {
12990 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12991 if (row->mode_line_p)
12992 ++row;
12993 if (!row->enabled_p)
12994 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12995 }
12996
12997 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12998 {
12999 int scroll_p = 0;
13000 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13001
13002 if (PT > XFASTINT (w->last_point))
13003 {
13004 /* Point has moved forward. */
13005 while (MATRIX_ROW_END_CHARPOS (row) < PT
13006 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13007 {
13008 xassert (row->enabled_p);
13009 ++row;
13010 }
13011
13012 /* The end position of a row equals the start position
13013 of the next row. If PT is there, we would rather
13014 display it in the next line. */
13015 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13016 && MATRIX_ROW_END_CHARPOS (row) == PT
13017 && !cursor_row_p (w, row))
13018 ++row;
13019
13020 /* If within the scroll margin, scroll. Note that
13021 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13022 the next line would be drawn, and that
13023 this_scroll_margin can be zero. */
13024 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13025 || PT > MATRIX_ROW_END_CHARPOS (row)
13026 /* Line is completely visible last line in window
13027 and PT is to be set in the next line. */
13028 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13029 && PT == MATRIX_ROW_END_CHARPOS (row)
13030 && !row->ends_at_zv_p
13031 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13032 scroll_p = 1;
13033 }
13034 else if (PT < XFASTINT (w->last_point))
13035 {
13036 /* Cursor has to be moved backward. Note that PT >=
13037 CHARPOS (startp) because of the outer if-statement. */
13038 while (!row->mode_line_p
13039 && (MATRIX_ROW_START_CHARPOS (row) > PT
13040 || (MATRIX_ROW_START_CHARPOS (row) == PT
13041 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13042 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13043 row > w->current_matrix->rows
13044 && (row-1)->ends_in_newline_from_string_p))))
13045 && (row->y > top_scroll_margin
13046 || CHARPOS (startp) == BEGV))
13047 {
13048 xassert (row->enabled_p);
13049 --row;
13050 }
13051
13052 /* Consider the following case: Window starts at BEGV,
13053 there is invisible, intangible text at BEGV, so that
13054 display starts at some point START > BEGV. It can
13055 happen that we are called with PT somewhere between
13056 BEGV and START. Try to handle that case. */
13057 if (row < w->current_matrix->rows
13058 || row->mode_line_p)
13059 {
13060 row = w->current_matrix->rows;
13061 if (row->mode_line_p)
13062 ++row;
13063 }
13064
13065 /* Due to newlines in overlay strings, we may have to
13066 skip forward over overlay strings. */
13067 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13068 && MATRIX_ROW_END_CHARPOS (row) == PT
13069 && !cursor_row_p (w, row))
13070 ++row;
13071
13072 /* If within the scroll margin, scroll. */
13073 if (row->y < top_scroll_margin
13074 && CHARPOS (startp) != BEGV)
13075 scroll_p = 1;
13076 }
13077 else
13078 {
13079 /* Cursor did not move. So don't scroll even if cursor line
13080 is partially visible, as it was so before. */
13081 rc = CURSOR_MOVEMENT_SUCCESS;
13082 }
13083
13084 if (PT < MATRIX_ROW_START_CHARPOS (row)
13085 || PT > MATRIX_ROW_END_CHARPOS (row))
13086 {
13087 /* if PT is not in the glyph row, give up. */
13088 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13089 }
13090 else if (rc != CURSOR_MOVEMENT_SUCCESS
13091 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13092 && make_cursor_line_fully_visible_p)
13093 {
13094 if (PT == MATRIX_ROW_END_CHARPOS (row)
13095 && !row->ends_at_zv_p
13096 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13097 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13098 else if (row->height > window_box_height (w))
13099 {
13100 /* If we end up in a partially visible line, let's
13101 make it fully visible, except when it's taller
13102 than the window, in which case we can't do much
13103 about it. */
13104 *scroll_step = 1;
13105 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13106 }
13107 else
13108 {
13109 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13110 if (!cursor_row_fully_visible_p (w, 0, 1))
13111 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13112 else
13113 rc = CURSOR_MOVEMENT_SUCCESS;
13114 }
13115 }
13116 else if (scroll_p)
13117 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13118 else
13119 {
13120 do
13121 {
13122 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13123 {
13124 rc = CURSOR_MOVEMENT_SUCCESS;
13125 break;
13126 }
13127 ++row;
13128 }
13129 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13130 && MATRIX_ROW_START_CHARPOS (row) == PT
13131 && cursor_row_p (w, row));
13132 }
13133 }
13134 }
13135
13136 return rc;
13137 }
13138
13139 void
13140 set_vertical_scroll_bar (w)
13141 struct window *w;
13142 {
13143 int start, end, whole;
13144
13145 /* Calculate the start and end positions for the current window.
13146 At some point, it would be nice to choose between scrollbars
13147 which reflect the whole buffer size, with special markers
13148 indicating narrowing, and scrollbars which reflect only the
13149 visible region.
13150
13151 Note that mini-buffers sometimes aren't displaying any text. */
13152 if (!MINI_WINDOW_P (w)
13153 || (w == XWINDOW (minibuf_window)
13154 && NILP (echo_area_buffer[0])))
13155 {
13156 struct buffer *buf = XBUFFER (w->buffer);
13157 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13158 start = marker_position (w->start) - BUF_BEGV (buf);
13159 /* I don't think this is guaranteed to be right. For the
13160 moment, we'll pretend it is. */
13161 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13162
13163 if (end < start)
13164 end = start;
13165 if (whole < (end - start))
13166 whole = end - start;
13167 }
13168 else
13169 start = end = whole = 0;
13170
13171 /* Indicate what this scroll bar ought to be displaying now. */
13172 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13173 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13174 (w, end - start, whole, start);
13175 }
13176
13177
13178 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13179 selected_window is redisplayed.
13180
13181 We can return without actually redisplaying the window if
13182 fonts_changed_p is nonzero. In that case, redisplay_internal will
13183 retry. */
13184
13185 static void
13186 redisplay_window (window, just_this_one_p)
13187 Lisp_Object window;
13188 int just_this_one_p;
13189 {
13190 struct window *w = XWINDOW (window);
13191 struct frame *f = XFRAME (w->frame);
13192 struct buffer *buffer = XBUFFER (w->buffer);
13193 struct buffer *old = current_buffer;
13194 struct text_pos lpoint, opoint, startp;
13195 int update_mode_line;
13196 int tem;
13197 struct it it;
13198 /* Record it now because it's overwritten. */
13199 int current_matrix_up_to_date_p = 0;
13200 int used_current_matrix_p = 0;
13201 /* This is less strict than current_matrix_up_to_date_p.
13202 It indictes that the buffer contents and narrowing are unchanged. */
13203 int buffer_unchanged_p = 0;
13204 int temp_scroll_step = 0;
13205 int count = SPECPDL_INDEX ();
13206 int rc;
13207 int centering_position = -1;
13208 int last_line_misfit = 0;
13209 int beg_unchanged, end_unchanged;
13210
13211 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13212 opoint = lpoint;
13213
13214 /* W must be a leaf window here. */
13215 xassert (!NILP (w->buffer));
13216 #if GLYPH_DEBUG
13217 *w->desired_matrix->method = 0;
13218 #endif
13219
13220 restart:
13221 reconsider_clip_changes (w, buffer);
13222
13223 /* Has the mode line to be updated? */
13224 update_mode_line = (!NILP (w->update_mode_line)
13225 || update_mode_lines
13226 || buffer->clip_changed
13227 || buffer->prevent_redisplay_optimizations_p);
13228
13229 if (MINI_WINDOW_P (w))
13230 {
13231 if (w == XWINDOW (echo_area_window)
13232 && !NILP (echo_area_buffer[0]))
13233 {
13234 if (update_mode_line)
13235 /* We may have to update a tty frame's menu bar or a
13236 tool-bar. Example `M-x C-h C-h C-g'. */
13237 goto finish_menu_bars;
13238 else
13239 /* We've already displayed the echo area glyphs in this window. */
13240 goto finish_scroll_bars;
13241 }
13242 else if ((w != XWINDOW (minibuf_window)
13243 || minibuf_level == 0)
13244 /* When buffer is nonempty, redisplay window normally. */
13245 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13246 /* Quail displays non-mini buffers in minibuffer window.
13247 In that case, redisplay the window normally. */
13248 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13249 {
13250 /* W is a mini-buffer window, but it's not active, so clear
13251 it. */
13252 int yb = window_text_bottom_y (w);
13253 struct glyph_row *row;
13254 int y;
13255
13256 for (y = 0, row = w->desired_matrix->rows;
13257 y < yb;
13258 y += row->height, ++row)
13259 blank_row (w, row, y);
13260 goto finish_scroll_bars;
13261 }
13262
13263 clear_glyph_matrix (w->desired_matrix);
13264 }
13265
13266 /* Otherwise set up data on this window; select its buffer and point
13267 value. */
13268 /* Really select the buffer, for the sake of buffer-local
13269 variables. */
13270 set_buffer_internal_1 (XBUFFER (w->buffer));
13271
13272 current_matrix_up_to_date_p
13273 = (!NILP (w->window_end_valid)
13274 && !current_buffer->clip_changed
13275 && !current_buffer->prevent_redisplay_optimizations_p
13276 && XFASTINT (w->last_modified) >= MODIFF
13277 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13278
13279 /* Run the window-bottom-change-functions
13280 if it is possible that the text on the screen has changed
13281 (either due to modification of the text, or any other reason). */
13282 if (!current_matrix_up_to_date_p
13283 && !NILP (Vwindow_text_change_functions))
13284 {
13285 safe_run_hooks (Qwindow_text_change_functions);
13286 goto restart;
13287 }
13288
13289 beg_unchanged = BEG_UNCHANGED;
13290 end_unchanged = END_UNCHANGED;
13291
13292 SET_TEXT_POS (opoint, PT, PT_BYTE);
13293
13294 specbind (Qinhibit_point_motion_hooks, Qt);
13295
13296 buffer_unchanged_p
13297 = (!NILP (w->window_end_valid)
13298 && !current_buffer->clip_changed
13299 && XFASTINT (w->last_modified) >= MODIFF
13300 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13301
13302 /* When windows_or_buffers_changed is non-zero, we can't rely on
13303 the window end being valid, so set it to nil there. */
13304 if (windows_or_buffers_changed)
13305 {
13306 /* If window starts on a continuation line, maybe adjust the
13307 window start in case the window's width changed. */
13308 if (XMARKER (w->start)->buffer == current_buffer)
13309 compute_window_start_on_continuation_line (w);
13310
13311 w->window_end_valid = Qnil;
13312 }
13313
13314 /* Some sanity checks. */
13315 CHECK_WINDOW_END (w);
13316 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13317 abort ();
13318 if (BYTEPOS (opoint) < CHARPOS (opoint))
13319 abort ();
13320
13321 /* If %c is in mode line, update it if needed. */
13322 if (!NILP (w->column_number_displayed)
13323 /* This alternative quickly identifies a common case
13324 where no change is needed. */
13325 && !(PT == XFASTINT (w->last_point)
13326 && XFASTINT (w->last_modified) >= MODIFF
13327 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13328 && (XFASTINT (w->column_number_displayed)
13329 != (int) current_column ())) /* iftc */
13330 update_mode_line = 1;
13331
13332 /* Count number of windows showing the selected buffer. An indirect
13333 buffer counts as its base buffer. */
13334 if (!just_this_one_p)
13335 {
13336 struct buffer *current_base, *window_base;
13337 current_base = current_buffer;
13338 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13339 if (current_base->base_buffer)
13340 current_base = current_base->base_buffer;
13341 if (window_base->base_buffer)
13342 window_base = window_base->base_buffer;
13343 if (current_base == window_base)
13344 buffer_shared++;
13345 }
13346
13347 /* Point refers normally to the selected window. For any other
13348 window, set up appropriate value. */
13349 if (!EQ (window, selected_window))
13350 {
13351 int new_pt = XMARKER (w->pointm)->charpos;
13352 int new_pt_byte = marker_byte_position (w->pointm);
13353 if (new_pt < BEGV)
13354 {
13355 new_pt = BEGV;
13356 new_pt_byte = BEGV_BYTE;
13357 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13358 }
13359 else if (new_pt > (ZV - 1))
13360 {
13361 new_pt = ZV;
13362 new_pt_byte = ZV_BYTE;
13363 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13364 }
13365
13366 /* We don't use SET_PT so that the point-motion hooks don't run. */
13367 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13368 }
13369
13370 /* If any of the character widths specified in the display table
13371 have changed, invalidate the width run cache. It's true that
13372 this may be a bit late to catch such changes, but the rest of
13373 redisplay goes (non-fatally) haywire when the display table is
13374 changed, so why should we worry about doing any better? */
13375 if (current_buffer->width_run_cache)
13376 {
13377 struct Lisp_Char_Table *disptab = buffer_display_table ();
13378
13379 if (! disptab_matches_widthtab (disptab,
13380 XVECTOR (current_buffer->width_table)))
13381 {
13382 invalidate_region_cache (current_buffer,
13383 current_buffer->width_run_cache,
13384 BEG, Z);
13385 recompute_width_table (current_buffer, disptab);
13386 }
13387 }
13388
13389 /* If window-start is screwed up, choose a new one. */
13390 if (XMARKER (w->start)->buffer != current_buffer)
13391 goto recenter;
13392
13393 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13394
13395 /* If someone specified a new starting point but did not insist,
13396 check whether it can be used. */
13397 if (!NILP (w->optional_new_start)
13398 && CHARPOS (startp) >= BEGV
13399 && CHARPOS (startp) <= ZV)
13400 {
13401 w->optional_new_start = Qnil;
13402 start_display (&it, w, startp);
13403 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13404 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13405 if (IT_CHARPOS (it) == PT)
13406 w->force_start = Qt;
13407 /* IT may overshoot PT if text at PT is invisible. */
13408 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13409 w->force_start = Qt;
13410 }
13411
13412 force_start:
13413
13414 /* Handle case where place to start displaying has been specified,
13415 unless the specified location is outside the accessible range. */
13416 if (!NILP (w->force_start)
13417 || w->frozen_window_start_p)
13418 {
13419 /* We set this later on if we have to adjust point. */
13420 int new_vpos = -1;
13421
13422 w->force_start = Qnil;
13423 w->vscroll = 0;
13424 w->window_end_valid = Qnil;
13425
13426 /* Forget any recorded base line for line number display. */
13427 if (!buffer_unchanged_p)
13428 w->base_line_number = Qnil;
13429
13430 /* Redisplay the mode line. Select the buffer properly for that.
13431 Also, run the hook window-scroll-functions
13432 because we have scrolled. */
13433 /* Note, we do this after clearing force_start because
13434 if there's an error, it is better to forget about force_start
13435 than to get into an infinite loop calling the hook functions
13436 and having them get more errors. */
13437 if (!update_mode_line
13438 || ! NILP (Vwindow_scroll_functions))
13439 {
13440 update_mode_line = 1;
13441 w->update_mode_line = Qt;
13442 startp = run_window_scroll_functions (window, startp);
13443 }
13444
13445 w->last_modified = make_number (0);
13446 w->last_overlay_modified = make_number (0);
13447 if (CHARPOS (startp) < BEGV)
13448 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13449 else if (CHARPOS (startp) > ZV)
13450 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13451
13452 /* Redisplay, then check if cursor has been set during the
13453 redisplay. Give up if new fonts were loaded. */
13454 /* We used to issue a CHECK_MARGINS argument to try_window here,
13455 but this causes scrolling to fail when point begins inside
13456 the scroll margin (bug#148) -- cyd */
13457 if (!try_window (window, startp, 0))
13458 {
13459 w->force_start = Qt;
13460 clear_glyph_matrix (w->desired_matrix);
13461 goto need_larger_matrices;
13462 }
13463
13464 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13465 {
13466 /* If point does not appear, try to move point so it does
13467 appear. The desired matrix has been built above, so we
13468 can use it here. */
13469 new_vpos = window_box_height (w) / 2;
13470 }
13471
13472 if (!cursor_row_fully_visible_p (w, 0, 0))
13473 {
13474 /* Point does appear, but on a line partly visible at end of window.
13475 Move it back to a fully-visible line. */
13476 new_vpos = window_box_height (w);
13477 }
13478
13479 /* If we need to move point for either of the above reasons,
13480 now actually do it. */
13481 if (new_vpos >= 0)
13482 {
13483 struct glyph_row *row;
13484
13485 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13486 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13487 ++row;
13488
13489 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13490 MATRIX_ROW_START_BYTEPOS (row));
13491
13492 if (w != XWINDOW (selected_window))
13493 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13494 else if (current_buffer == old)
13495 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13496
13497 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13498
13499 /* If we are highlighting the region, then we just changed
13500 the region, so redisplay to show it. */
13501 if (!NILP (Vtransient_mark_mode)
13502 && !NILP (current_buffer->mark_active))
13503 {
13504 clear_glyph_matrix (w->desired_matrix);
13505 if (!try_window (window, startp, 0))
13506 goto need_larger_matrices;
13507 }
13508 }
13509
13510 #if GLYPH_DEBUG
13511 debug_method_add (w, "forced window start");
13512 #endif
13513 goto done;
13514 }
13515
13516 /* Handle case where text has not changed, only point, and it has
13517 not moved off the frame, and we are not retrying after hscroll.
13518 (current_matrix_up_to_date_p is nonzero when retrying.) */
13519 if (current_matrix_up_to_date_p
13520 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13521 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13522 {
13523 switch (rc)
13524 {
13525 case CURSOR_MOVEMENT_SUCCESS:
13526 used_current_matrix_p = 1;
13527 goto done;
13528
13529 case CURSOR_MOVEMENT_MUST_SCROLL:
13530 goto try_to_scroll;
13531
13532 default:
13533 abort ();
13534 }
13535 }
13536 /* If current starting point was originally the beginning of a line
13537 but no longer is, find a new starting point. */
13538 else if (!NILP (w->start_at_line_beg)
13539 && !(CHARPOS (startp) <= BEGV
13540 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13541 {
13542 #if GLYPH_DEBUG
13543 debug_method_add (w, "recenter 1");
13544 #endif
13545 goto recenter;
13546 }
13547
13548 /* Try scrolling with try_window_id. Value is > 0 if update has
13549 been done, it is -1 if we know that the same window start will
13550 not work. It is 0 if unsuccessful for some other reason. */
13551 else if ((tem = try_window_id (w)) != 0)
13552 {
13553 #if GLYPH_DEBUG
13554 debug_method_add (w, "try_window_id %d", tem);
13555 #endif
13556
13557 if (fonts_changed_p)
13558 goto need_larger_matrices;
13559 if (tem > 0)
13560 goto done;
13561
13562 /* Otherwise try_window_id has returned -1 which means that we
13563 don't want the alternative below this comment to execute. */
13564 }
13565 else if (CHARPOS (startp) >= BEGV
13566 && CHARPOS (startp) <= ZV
13567 && PT >= CHARPOS (startp)
13568 && (CHARPOS (startp) < ZV
13569 /* Avoid starting at end of buffer. */
13570 || CHARPOS (startp) == BEGV
13571 || (XFASTINT (w->last_modified) >= MODIFF
13572 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13573 {
13574
13575 /* If first window line is a continuation line, and window start
13576 is inside the modified region, but the first change is before
13577 current window start, we must select a new window start.
13578
13579 However, if this is the result of a down-mouse event (e.g. by
13580 extending the mouse-drag-overlay), we don't want to select a
13581 new window start, since that would change the position under
13582 the mouse, resulting in an unwanted mouse-movement rather
13583 than a simple mouse-click. */
13584 if (NILP (w->start_at_line_beg)
13585 && NILP (do_mouse_tracking)
13586 && CHARPOS (startp) > BEGV
13587 && CHARPOS (startp) > BEG + beg_unchanged
13588 && CHARPOS (startp) <= Z - end_unchanged
13589 /* Even if w->start_at_line_beg is nil, a new window may
13590 start at a line_beg, since that's how set_buffer_window
13591 sets it. So, we need to check the return value of
13592 compute_window_start_on_continuation_line. (See also
13593 bug#197). */
13594 && XMARKER (w->start)->buffer == current_buffer
13595 && compute_window_start_on_continuation_line (w))
13596 {
13597 w->force_start = Qt;
13598 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13599 goto force_start;
13600 }
13601
13602 #if GLYPH_DEBUG
13603 debug_method_add (w, "same window start");
13604 #endif
13605
13606 /* Try to redisplay starting at same place as before.
13607 If point has not moved off frame, accept the results. */
13608 if (!current_matrix_up_to_date_p
13609 /* Don't use try_window_reusing_current_matrix in this case
13610 because a window scroll function can have changed the
13611 buffer. */
13612 || !NILP (Vwindow_scroll_functions)
13613 || MINI_WINDOW_P (w)
13614 || !(used_current_matrix_p
13615 = try_window_reusing_current_matrix (w)))
13616 {
13617 IF_DEBUG (debug_method_add (w, "1"));
13618 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
13619 /* -1 means we need to scroll.
13620 0 means we need new matrices, but fonts_changed_p
13621 is set in that case, so we will detect it below. */
13622 goto try_to_scroll;
13623 }
13624
13625 if (fonts_changed_p)
13626 goto need_larger_matrices;
13627
13628 if (w->cursor.vpos >= 0)
13629 {
13630 if (!just_this_one_p
13631 || current_buffer->clip_changed
13632 || BEG_UNCHANGED < CHARPOS (startp))
13633 /* Forget any recorded base line for line number display. */
13634 w->base_line_number = Qnil;
13635
13636 if (!cursor_row_fully_visible_p (w, 1, 0))
13637 {
13638 clear_glyph_matrix (w->desired_matrix);
13639 last_line_misfit = 1;
13640 }
13641 /* Drop through and scroll. */
13642 else
13643 goto done;
13644 }
13645 else
13646 clear_glyph_matrix (w->desired_matrix);
13647 }
13648
13649 try_to_scroll:
13650
13651 w->last_modified = make_number (0);
13652 w->last_overlay_modified = make_number (0);
13653
13654 /* Redisplay the mode line. Select the buffer properly for that. */
13655 if (!update_mode_line)
13656 {
13657 update_mode_line = 1;
13658 w->update_mode_line = Qt;
13659 }
13660
13661 /* Try to scroll by specified few lines. */
13662 if ((scroll_conservatively
13663 || scroll_step
13664 || temp_scroll_step
13665 || NUMBERP (current_buffer->scroll_up_aggressively)
13666 || NUMBERP (current_buffer->scroll_down_aggressively))
13667 && !current_buffer->clip_changed
13668 && CHARPOS (startp) >= BEGV
13669 && CHARPOS (startp) <= ZV)
13670 {
13671 /* The function returns -1 if new fonts were loaded, 1 if
13672 successful, 0 if not successful. */
13673 int rc = try_scrolling (window, just_this_one_p,
13674 scroll_conservatively,
13675 scroll_step,
13676 temp_scroll_step, last_line_misfit);
13677 switch (rc)
13678 {
13679 case SCROLLING_SUCCESS:
13680 goto done;
13681
13682 case SCROLLING_NEED_LARGER_MATRICES:
13683 goto need_larger_matrices;
13684
13685 case SCROLLING_FAILED:
13686 break;
13687
13688 default:
13689 abort ();
13690 }
13691 }
13692
13693 /* Finally, just choose place to start which centers point */
13694
13695 recenter:
13696 if (centering_position < 0)
13697 centering_position = window_box_height (w) / 2;
13698
13699 #if GLYPH_DEBUG
13700 debug_method_add (w, "recenter");
13701 #endif
13702
13703 /* w->vscroll = 0; */
13704
13705 /* Forget any previously recorded base line for line number display. */
13706 if (!buffer_unchanged_p)
13707 w->base_line_number = Qnil;
13708
13709 /* Move backward half the height of the window. */
13710 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13711 it.current_y = it.last_visible_y;
13712 move_it_vertically_backward (&it, centering_position);
13713 xassert (IT_CHARPOS (it) >= BEGV);
13714
13715 /* The function move_it_vertically_backward may move over more
13716 than the specified y-distance. If it->w is small, e.g. a
13717 mini-buffer window, we may end up in front of the window's
13718 display area. Start displaying at the start of the line
13719 containing PT in this case. */
13720 if (it.current_y <= 0)
13721 {
13722 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13723 move_it_vertically_backward (&it, 0);
13724 it.current_y = 0;
13725 }
13726
13727 it.current_x = it.hpos = 0;
13728
13729 /* Set startp here explicitly in case that helps avoid an infinite loop
13730 in case the window-scroll-functions functions get errors. */
13731 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13732
13733 /* Run scroll hooks. */
13734 startp = run_window_scroll_functions (window, it.current.pos);
13735
13736 /* Redisplay the window. */
13737 if (!current_matrix_up_to_date_p
13738 || windows_or_buffers_changed
13739 || cursor_type_changed
13740 /* Don't use try_window_reusing_current_matrix in this case
13741 because it can have changed the buffer. */
13742 || !NILP (Vwindow_scroll_functions)
13743 || !just_this_one_p
13744 || MINI_WINDOW_P (w)
13745 || !(used_current_matrix_p
13746 = try_window_reusing_current_matrix (w)))
13747 try_window (window, startp, 0);
13748
13749 /* If new fonts have been loaded (due to fontsets), give up. We
13750 have to start a new redisplay since we need to re-adjust glyph
13751 matrices. */
13752 if (fonts_changed_p)
13753 goto need_larger_matrices;
13754
13755 /* If cursor did not appear assume that the middle of the window is
13756 in the first line of the window. Do it again with the next line.
13757 (Imagine a window of height 100, displaying two lines of height
13758 60. Moving back 50 from it->last_visible_y will end in the first
13759 line.) */
13760 if (w->cursor.vpos < 0)
13761 {
13762 if (!NILP (w->window_end_valid)
13763 && PT >= Z - XFASTINT (w->window_end_pos))
13764 {
13765 clear_glyph_matrix (w->desired_matrix);
13766 move_it_by_lines (&it, 1, 0);
13767 try_window (window, it.current.pos, 0);
13768 }
13769 else if (PT < IT_CHARPOS (it))
13770 {
13771 clear_glyph_matrix (w->desired_matrix);
13772 move_it_by_lines (&it, -1, 0);
13773 try_window (window, it.current.pos, 0);
13774 }
13775 else
13776 {
13777 /* Not much we can do about it. */
13778 }
13779 }
13780
13781 /* Consider the following case: Window starts at BEGV, there is
13782 invisible, intangible text at BEGV, so that display starts at
13783 some point START > BEGV. It can happen that we are called with
13784 PT somewhere between BEGV and START. Try to handle that case. */
13785 if (w->cursor.vpos < 0)
13786 {
13787 struct glyph_row *row = w->current_matrix->rows;
13788 if (row->mode_line_p)
13789 ++row;
13790 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13791 }
13792
13793 if (!cursor_row_fully_visible_p (w, 0, 0))
13794 {
13795 /* If vscroll is enabled, disable it and try again. */
13796 if (w->vscroll)
13797 {
13798 w->vscroll = 0;
13799 clear_glyph_matrix (w->desired_matrix);
13800 goto recenter;
13801 }
13802
13803 /* If centering point failed to make the whole line visible,
13804 put point at the top instead. That has to make the whole line
13805 visible, if it can be done. */
13806 if (centering_position == 0)
13807 goto done;
13808
13809 clear_glyph_matrix (w->desired_matrix);
13810 centering_position = 0;
13811 goto recenter;
13812 }
13813
13814 done:
13815
13816 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13817 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13818 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13819 ? Qt : Qnil);
13820
13821 /* Display the mode line, if we must. */
13822 if ((update_mode_line
13823 /* If window not full width, must redo its mode line
13824 if (a) the window to its side is being redone and
13825 (b) we do a frame-based redisplay. This is a consequence
13826 of how inverted lines are drawn in frame-based redisplay. */
13827 || (!just_this_one_p
13828 && !FRAME_WINDOW_P (f)
13829 && !WINDOW_FULL_WIDTH_P (w))
13830 /* Line number to display. */
13831 || INTEGERP (w->base_line_pos)
13832 /* Column number is displayed and different from the one displayed. */
13833 || (!NILP (w->column_number_displayed)
13834 && (XFASTINT (w->column_number_displayed)
13835 != (int) current_column ()))) /* iftc */
13836 /* This means that the window has a mode line. */
13837 && (WINDOW_WANTS_MODELINE_P (w)
13838 || WINDOW_WANTS_HEADER_LINE_P (w)))
13839 {
13840 display_mode_lines (w);
13841
13842 /* If mode line height has changed, arrange for a thorough
13843 immediate redisplay using the correct mode line height. */
13844 if (WINDOW_WANTS_MODELINE_P (w)
13845 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13846 {
13847 fonts_changed_p = 1;
13848 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13849 = DESIRED_MODE_LINE_HEIGHT (w);
13850 }
13851
13852 /* If header line height has changed, arrange for a thorough
13853 immediate redisplay using the correct header line height. */
13854 if (WINDOW_WANTS_HEADER_LINE_P (w)
13855 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13856 {
13857 fonts_changed_p = 1;
13858 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13859 = DESIRED_HEADER_LINE_HEIGHT (w);
13860 }
13861
13862 if (fonts_changed_p)
13863 goto need_larger_matrices;
13864 }
13865
13866 if (!line_number_displayed
13867 && !BUFFERP (w->base_line_pos))
13868 {
13869 w->base_line_pos = Qnil;
13870 w->base_line_number = Qnil;
13871 }
13872
13873 finish_menu_bars:
13874
13875 /* When we reach a frame's selected window, redo the frame's menu bar. */
13876 if (update_mode_line
13877 && EQ (FRAME_SELECTED_WINDOW (f), window))
13878 {
13879 int redisplay_menu_p = 0;
13880 int redisplay_tool_bar_p = 0;
13881
13882 if (FRAME_WINDOW_P (f))
13883 {
13884 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13885 || defined (HAVE_NS) || defined (USE_GTK)
13886 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13887 #else
13888 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13889 #endif
13890 }
13891 else
13892 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13893
13894 if (redisplay_menu_p)
13895 display_menu_bar (w);
13896
13897 #ifdef HAVE_WINDOW_SYSTEM
13898 if (FRAME_WINDOW_P (f))
13899 {
13900 #if defined (USE_GTK) || defined (HAVE_NS)
13901 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13902 #else
13903 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13904 && (FRAME_TOOL_BAR_LINES (f) > 0
13905 || !NILP (Vauto_resize_tool_bars));
13906 #endif
13907
13908 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13909 {
13910 extern int ignore_mouse_drag_p;
13911 ignore_mouse_drag_p = 1;
13912 }
13913 }
13914 #endif
13915 }
13916
13917 #ifdef HAVE_WINDOW_SYSTEM
13918 if (FRAME_WINDOW_P (f)
13919 && update_window_fringes (w, (just_this_one_p
13920 || (!used_current_matrix_p && !overlay_arrow_seen)
13921 || w->pseudo_window_p)))
13922 {
13923 update_begin (f);
13924 BLOCK_INPUT;
13925 if (draw_window_fringes (w, 1))
13926 x_draw_vertical_border (w);
13927 UNBLOCK_INPUT;
13928 update_end (f);
13929 }
13930 #endif /* HAVE_WINDOW_SYSTEM */
13931
13932 /* We go to this label, with fonts_changed_p nonzero,
13933 if it is necessary to try again using larger glyph matrices.
13934 We have to redeem the scroll bar even in this case,
13935 because the loop in redisplay_internal expects that. */
13936 need_larger_matrices:
13937 ;
13938 finish_scroll_bars:
13939
13940 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13941 {
13942 /* Set the thumb's position and size. */
13943 set_vertical_scroll_bar (w);
13944
13945 /* Note that we actually used the scroll bar attached to this
13946 window, so it shouldn't be deleted at the end of redisplay. */
13947 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13948 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13949 }
13950
13951 /* Restore current_buffer and value of point in it. The window
13952 update may have changed the buffer, so first make sure `opoint'
13953 is still valid (Bug#6177). */
13954 if (CHARPOS (opoint) < BEGV)
13955 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13956 else if (CHARPOS (opoint) > ZV)
13957 TEMP_SET_PT_BOTH (Z, Z_BYTE);
13958 else
13959 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13960
13961 set_buffer_internal_1 (old);
13962 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13963 shorter. This can be caused by log truncation in *Messages*. */
13964 if (CHARPOS (lpoint) <= ZV)
13965 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13966
13967 unbind_to (count, Qnil);
13968 }
13969
13970
13971 /* Build the complete desired matrix of WINDOW with a window start
13972 buffer position POS.
13973
13974 Value is 1 if successful. It is zero if fonts were loaded during
13975 redisplay which makes re-adjusting glyph matrices necessary, and -1
13976 if point would appear in the scroll margins.
13977 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
13978 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
13979 set in FLAGS.) */
13980
13981 int
13982 try_window (window, pos, flags)
13983 Lisp_Object window;
13984 struct text_pos pos;
13985 int flags;
13986 {
13987 struct window *w = XWINDOW (window);
13988 struct it it;
13989 struct glyph_row *last_text_row = NULL;
13990 struct frame *f = XFRAME (w->frame);
13991
13992 /* Make POS the new window start. */
13993 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13994
13995 /* Mark cursor position as unknown. No overlay arrow seen. */
13996 w->cursor.vpos = -1;
13997 overlay_arrow_seen = 0;
13998
13999 /* Initialize iterator and info to start at POS. */
14000 start_display (&it, w, pos);
14001
14002 /* Display all lines of W. */
14003 while (it.current_y < it.last_visible_y)
14004 {
14005 if (display_line (&it))
14006 last_text_row = it.glyph_row - 1;
14007 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14008 return 0;
14009 }
14010
14011 /* Don't let the cursor end in the scroll margins. */
14012 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14013 && !MINI_WINDOW_P (w))
14014 {
14015 int this_scroll_margin;
14016
14017 if (scroll_margin > 0)
14018 {
14019 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14020 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14021 }
14022 else
14023 this_scroll_margin = 0;
14024
14025 if ((w->cursor.y >= 0 /* not vscrolled */
14026 && w->cursor.y < this_scroll_margin
14027 && CHARPOS (pos) > BEGV
14028 && IT_CHARPOS (it) < ZV)
14029 /* rms: considering make_cursor_line_fully_visible_p here
14030 seems to give wrong results. We don't want to recenter
14031 when the last line is partly visible, we want to allow
14032 that case to be handled in the usual way. */
14033 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14034 {
14035 w->cursor.vpos = -1;
14036 clear_glyph_matrix (w->desired_matrix);
14037 return -1;
14038 }
14039 }
14040
14041 /* If bottom moved off end of frame, change mode line percentage. */
14042 if (XFASTINT (w->window_end_pos) <= 0
14043 && Z != IT_CHARPOS (it))
14044 w->update_mode_line = Qt;
14045
14046 /* Set window_end_pos to the offset of the last character displayed
14047 on the window from the end of current_buffer. Set
14048 window_end_vpos to its row number. */
14049 if (last_text_row)
14050 {
14051 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14052 w->window_end_bytepos
14053 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14054 w->window_end_pos
14055 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14056 w->window_end_vpos
14057 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14058 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14059 ->displays_text_p);
14060 }
14061 else
14062 {
14063 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14064 w->window_end_pos = make_number (Z - ZV);
14065 w->window_end_vpos = make_number (0);
14066 }
14067
14068 /* But that is not valid info until redisplay finishes. */
14069 w->window_end_valid = Qnil;
14070 return 1;
14071 }
14072
14073
14074 \f
14075 /************************************************************************
14076 Window redisplay reusing current matrix when buffer has not changed
14077 ************************************************************************/
14078
14079 /* Try redisplay of window W showing an unchanged buffer with a
14080 different window start than the last time it was displayed by
14081 reusing its current matrix. Value is non-zero if successful.
14082 W->start is the new window start. */
14083
14084 static int
14085 try_window_reusing_current_matrix (w)
14086 struct window *w;
14087 {
14088 struct frame *f = XFRAME (w->frame);
14089 struct glyph_row *row, *bottom_row;
14090 struct it it;
14091 struct run run;
14092 struct text_pos start, new_start;
14093 int nrows_scrolled, i;
14094 struct glyph_row *last_text_row;
14095 struct glyph_row *last_reused_text_row;
14096 struct glyph_row *start_row;
14097 int start_vpos, min_y, max_y;
14098
14099 #if GLYPH_DEBUG
14100 if (inhibit_try_window_reusing)
14101 return 0;
14102 #endif
14103
14104 if (/* This function doesn't handle terminal frames. */
14105 !FRAME_WINDOW_P (f)
14106 /* Don't try to reuse the display if windows have been split
14107 or such. */
14108 || windows_or_buffers_changed
14109 || cursor_type_changed)
14110 return 0;
14111
14112 /* Can't do this if region may have changed. */
14113 if ((!NILP (Vtransient_mark_mode)
14114 && !NILP (current_buffer->mark_active))
14115 || !NILP (w->region_showing)
14116 || !NILP (Vshow_trailing_whitespace))
14117 return 0;
14118
14119 /* If top-line visibility has changed, give up. */
14120 if (WINDOW_WANTS_HEADER_LINE_P (w)
14121 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14122 return 0;
14123
14124 /* Give up if old or new display is scrolled vertically. We could
14125 make this function handle this, but right now it doesn't. */
14126 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14127 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14128 return 0;
14129
14130 /* The variable new_start now holds the new window start. The old
14131 start `start' can be determined from the current matrix. */
14132 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14133 start = start_row->start.pos;
14134 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14135
14136 /* Clear the desired matrix for the display below. */
14137 clear_glyph_matrix (w->desired_matrix);
14138
14139 if (CHARPOS (new_start) <= CHARPOS (start))
14140 {
14141 int first_row_y;
14142
14143 /* Don't use this method if the display starts with an ellipsis
14144 displayed for invisible text. It's not easy to handle that case
14145 below, and it's certainly not worth the effort since this is
14146 not a frequent case. */
14147 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14148 return 0;
14149
14150 IF_DEBUG (debug_method_add (w, "twu1"));
14151
14152 /* Display up to a row that can be reused. The variable
14153 last_text_row is set to the last row displayed that displays
14154 text. Note that it.vpos == 0 if or if not there is a
14155 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14156 start_display (&it, w, new_start);
14157 first_row_y = it.current_y;
14158 w->cursor.vpos = -1;
14159 last_text_row = last_reused_text_row = NULL;
14160
14161 while (it.current_y < it.last_visible_y
14162 && !fonts_changed_p)
14163 {
14164 /* If we have reached into the characters in the START row,
14165 that means the line boundaries have changed. So we
14166 can't start copying with the row START. Maybe it will
14167 work to start copying with the following row. */
14168 while (IT_CHARPOS (it) > CHARPOS (start))
14169 {
14170 /* Advance to the next row as the "start". */
14171 start_row++;
14172 start = start_row->start.pos;
14173 /* If there are no more rows to try, or just one, give up. */
14174 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14175 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14176 || CHARPOS (start) == ZV)
14177 {
14178 clear_glyph_matrix (w->desired_matrix);
14179 return 0;
14180 }
14181
14182 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14183 }
14184 /* If we have reached alignment,
14185 we can copy the rest of the rows. */
14186 if (IT_CHARPOS (it) == CHARPOS (start))
14187 break;
14188
14189 if (display_line (&it))
14190 last_text_row = it.glyph_row - 1;
14191 }
14192
14193 /* A value of current_y < last_visible_y means that we stopped
14194 at the previous window start, which in turn means that we
14195 have at least one reusable row. */
14196 if (it.current_y < it.last_visible_y)
14197 {
14198 /* IT.vpos always starts from 0; it counts text lines. */
14199 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14200
14201 /* Find PT if not already found in the lines displayed. */
14202 if (w->cursor.vpos < 0)
14203 {
14204 int dy = it.current_y - start_row->y;
14205
14206 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14207 row = row_containing_pos (w, PT, row, NULL, dy);
14208 if (row)
14209 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14210 dy, nrows_scrolled);
14211 else
14212 {
14213 clear_glyph_matrix (w->desired_matrix);
14214 return 0;
14215 }
14216 }
14217
14218 /* Scroll the display. Do it before the current matrix is
14219 changed. The problem here is that update has not yet
14220 run, i.e. part of the current matrix is not up to date.
14221 scroll_run_hook will clear the cursor, and use the
14222 current matrix to get the height of the row the cursor is
14223 in. */
14224 run.current_y = start_row->y;
14225 run.desired_y = it.current_y;
14226 run.height = it.last_visible_y - it.current_y;
14227
14228 if (run.height > 0 && run.current_y != run.desired_y)
14229 {
14230 update_begin (f);
14231 FRAME_RIF (f)->update_window_begin_hook (w);
14232 FRAME_RIF (f)->clear_window_mouse_face (w);
14233 FRAME_RIF (f)->scroll_run_hook (w, &run);
14234 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14235 update_end (f);
14236 }
14237
14238 /* Shift current matrix down by nrows_scrolled lines. */
14239 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14240 rotate_matrix (w->current_matrix,
14241 start_vpos,
14242 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14243 nrows_scrolled);
14244
14245 /* Disable lines that must be updated. */
14246 for (i = 0; i < nrows_scrolled; ++i)
14247 (start_row + i)->enabled_p = 0;
14248
14249 /* Re-compute Y positions. */
14250 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14251 max_y = it.last_visible_y;
14252 for (row = start_row + nrows_scrolled;
14253 row < bottom_row;
14254 ++row)
14255 {
14256 row->y = it.current_y;
14257 row->visible_height = row->height;
14258
14259 if (row->y < min_y)
14260 row->visible_height -= min_y - row->y;
14261 if (row->y + row->height > max_y)
14262 row->visible_height -= row->y + row->height - max_y;
14263 if (row->fringe_bitmap_periodic_p)
14264 row->redraw_fringe_bitmaps_p = 1;
14265
14266 it.current_y += row->height;
14267
14268 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14269 last_reused_text_row = row;
14270 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14271 break;
14272 }
14273
14274 /* Disable lines in the current matrix which are now
14275 below the window. */
14276 for (++row; row < bottom_row; ++row)
14277 row->enabled_p = row->mode_line_p = 0;
14278 }
14279
14280 /* Update window_end_pos etc.; last_reused_text_row is the last
14281 reused row from the current matrix containing text, if any.
14282 The value of last_text_row is the last displayed line
14283 containing text. */
14284 if (last_reused_text_row)
14285 {
14286 w->window_end_bytepos
14287 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14288 w->window_end_pos
14289 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14290 w->window_end_vpos
14291 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14292 w->current_matrix));
14293 }
14294 else if (last_text_row)
14295 {
14296 w->window_end_bytepos
14297 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14298 w->window_end_pos
14299 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14300 w->window_end_vpos
14301 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14302 }
14303 else
14304 {
14305 /* This window must be completely empty. */
14306 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14307 w->window_end_pos = make_number (Z - ZV);
14308 w->window_end_vpos = make_number (0);
14309 }
14310 w->window_end_valid = Qnil;
14311
14312 /* Update hint: don't try scrolling again in update_window. */
14313 w->desired_matrix->no_scrolling_p = 1;
14314
14315 #if GLYPH_DEBUG
14316 debug_method_add (w, "try_window_reusing_current_matrix 1");
14317 #endif
14318 return 1;
14319 }
14320 else if (CHARPOS (new_start) > CHARPOS (start))
14321 {
14322 struct glyph_row *pt_row, *row;
14323 struct glyph_row *first_reusable_row;
14324 struct glyph_row *first_row_to_display;
14325 int dy;
14326 int yb = window_text_bottom_y (w);
14327
14328 /* Find the row starting at new_start, if there is one. Don't
14329 reuse a partially visible line at the end. */
14330 first_reusable_row = start_row;
14331 while (first_reusable_row->enabled_p
14332 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14333 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14334 < CHARPOS (new_start)))
14335 ++first_reusable_row;
14336
14337 /* Give up if there is no row to reuse. */
14338 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14339 || !first_reusable_row->enabled_p
14340 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14341 != CHARPOS (new_start)))
14342 return 0;
14343
14344 /* We can reuse fully visible rows beginning with
14345 first_reusable_row to the end of the window. Set
14346 first_row_to_display to the first row that cannot be reused.
14347 Set pt_row to the row containing point, if there is any. */
14348 pt_row = NULL;
14349 for (first_row_to_display = first_reusable_row;
14350 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14351 ++first_row_to_display)
14352 {
14353 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14354 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14355 pt_row = first_row_to_display;
14356 }
14357
14358 /* Start displaying at the start of first_row_to_display. */
14359 xassert (first_row_to_display->y < yb);
14360 init_to_row_start (&it, w, first_row_to_display);
14361
14362 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14363 - start_vpos);
14364 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14365 - nrows_scrolled);
14366 it.current_y = (first_row_to_display->y - first_reusable_row->y
14367 + WINDOW_HEADER_LINE_HEIGHT (w));
14368
14369 /* Display lines beginning with first_row_to_display in the
14370 desired matrix. Set last_text_row to the last row displayed
14371 that displays text. */
14372 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14373 if (pt_row == NULL)
14374 w->cursor.vpos = -1;
14375 last_text_row = NULL;
14376 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14377 if (display_line (&it))
14378 last_text_row = it.glyph_row - 1;
14379
14380 /* If point is in a reused row, adjust y and vpos of the cursor
14381 position. */
14382 if (pt_row)
14383 {
14384 w->cursor.vpos -= nrows_scrolled;
14385 w->cursor.y -= first_reusable_row->y - start_row->y;
14386 }
14387
14388 /* Give up if point isn't in a row displayed or reused. (This
14389 also handles the case where w->cursor.vpos < nrows_scrolled
14390 after the calls to display_line, which can happen with scroll
14391 margins. See bug#1295.) */
14392 if (w->cursor.vpos < 0)
14393 {
14394 clear_glyph_matrix (w->desired_matrix);
14395 return 0;
14396 }
14397
14398 /* Scroll the display. */
14399 run.current_y = first_reusable_row->y;
14400 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14401 run.height = it.last_visible_y - run.current_y;
14402 dy = run.current_y - run.desired_y;
14403
14404 if (run.height)
14405 {
14406 update_begin (f);
14407 FRAME_RIF (f)->update_window_begin_hook (w);
14408 FRAME_RIF (f)->clear_window_mouse_face (w);
14409 FRAME_RIF (f)->scroll_run_hook (w, &run);
14410 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14411 update_end (f);
14412 }
14413
14414 /* Adjust Y positions of reused rows. */
14415 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14416 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14417 max_y = it.last_visible_y;
14418 for (row = first_reusable_row; row < first_row_to_display; ++row)
14419 {
14420 row->y -= dy;
14421 row->visible_height = row->height;
14422 if (row->y < min_y)
14423 row->visible_height -= min_y - row->y;
14424 if (row->y + row->height > max_y)
14425 row->visible_height -= row->y + row->height - max_y;
14426 if (row->fringe_bitmap_periodic_p)
14427 row->redraw_fringe_bitmaps_p = 1;
14428 }
14429
14430 /* Scroll the current matrix. */
14431 xassert (nrows_scrolled > 0);
14432 rotate_matrix (w->current_matrix,
14433 start_vpos,
14434 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14435 -nrows_scrolled);
14436
14437 /* Disable rows not reused. */
14438 for (row -= nrows_scrolled; row < bottom_row; ++row)
14439 row->enabled_p = 0;
14440
14441 /* Point may have moved to a different line, so we cannot assume that
14442 the previous cursor position is valid; locate the correct row. */
14443 if (pt_row)
14444 {
14445 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14446 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14447 row++)
14448 {
14449 w->cursor.vpos++;
14450 w->cursor.y = row->y;
14451 }
14452 if (row < bottom_row)
14453 {
14454 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14455 struct glyph *end = glyph + row->used[TEXT_AREA];
14456
14457 for (; glyph < end
14458 && (!BUFFERP (glyph->object)
14459 || glyph->charpos < PT);
14460 glyph++)
14461 {
14462 w->cursor.hpos++;
14463 w->cursor.x += glyph->pixel_width;
14464 }
14465 }
14466 }
14467
14468 /* Adjust window end. A null value of last_text_row means that
14469 the window end is in reused rows which in turn means that
14470 only its vpos can have changed. */
14471 if (last_text_row)
14472 {
14473 w->window_end_bytepos
14474 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14475 w->window_end_pos
14476 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14477 w->window_end_vpos
14478 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14479 }
14480 else
14481 {
14482 w->window_end_vpos
14483 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14484 }
14485
14486 w->window_end_valid = Qnil;
14487 w->desired_matrix->no_scrolling_p = 1;
14488
14489 #if GLYPH_DEBUG
14490 debug_method_add (w, "try_window_reusing_current_matrix 2");
14491 #endif
14492 return 1;
14493 }
14494
14495 return 0;
14496 }
14497
14498
14499 \f
14500 /************************************************************************
14501 Window redisplay reusing current matrix when buffer has changed
14502 ************************************************************************/
14503
14504 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14505 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14506 int *, int *));
14507 static struct glyph_row *
14508 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14509 struct glyph_row *));
14510
14511
14512 /* Return the last row in MATRIX displaying text. If row START is
14513 non-null, start searching with that row. IT gives the dimensions
14514 of the display. Value is null if matrix is empty; otherwise it is
14515 a pointer to the row found. */
14516
14517 static struct glyph_row *
14518 find_last_row_displaying_text (matrix, it, start)
14519 struct glyph_matrix *matrix;
14520 struct it *it;
14521 struct glyph_row *start;
14522 {
14523 struct glyph_row *row, *row_found;
14524
14525 /* Set row_found to the last row in IT->w's current matrix
14526 displaying text. The loop looks funny but think of partially
14527 visible lines. */
14528 row_found = NULL;
14529 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14530 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14531 {
14532 xassert (row->enabled_p);
14533 row_found = row;
14534 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14535 break;
14536 ++row;
14537 }
14538
14539 return row_found;
14540 }
14541
14542
14543 /* Return the last row in the current matrix of W that is not affected
14544 by changes at the start of current_buffer that occurred since W's
14545 current matrix was built. Value is null if no such row exists.
14546
14547 BEG_UNCHANGED us the number of characters unchanged at the start of
14548 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14549 first changed character in current_buffer. Characters at positions <
14550 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14551 when the current matrix was built. */
14552
14553 static struct glyph_row *
14554 find_last_unchanged_at_beg_row (w)
14555 struct window *w;
14556 {
14557 int first_changed_pos = BEG + BEG_UNCHANGED;
14558 struct glyph_row *row;
14559 struct glyph_row *row_found = NULL;
14560 int yb = window_text_bottom_y (w);
14561
14562 /* Find the last row displaying unchanged text. */
14563 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14564 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14565 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14566 ++row)
14567 {
14568 if (/* If row ends before first_changed_pos, it is unchanged,
14569 except in some case. */
14570 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14571 /* When row ends in ZV and we write at ZV it is not
14572 unchanged. */
14573 && !row->ends_at_zv_p
14574 /* When first_changed_pos is the end of a continued line,
14575 row is not unchanged because it may be no longer
14576 continued. */
14577 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14578 && (row->continued_p
14579 || row->exact_window_width_line_p)))
14580 row_found = row;
14581
14582 /* Stop if last visible row. */
14583 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14584 break;
14585 }
14586
14587 return row_found;
14588 }
14589
14590
14591 /* Find the first glyph row in the current matrix of W that is not
14592 affected by changes at the end of current_buffer since the
14593 time W's current matrix was built.
14594
14595 Return in *DELTA the number of chars by which buffer positions in
14596 unchanged text at the end of current_buffer must be adjusted.
14597
14598 Return in *DELTA_BYTES the corresponding number of bytes.
14599
14600 Value is null if no such row exists, i.e. all rows are affected by
14601 changes. */
14602
14603 static struct glyph_row *
14604 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14605 struct window *w;
14606 int *delta, *delta_bytes;
14607 {
14608 struct glyph_row *row;
14609 struct glyph_row *row_found = NULL;
14610
14611 *delta = *delta_bytes = 0;
14612
14613 /* Display must not have been paused, otherwise the current matrix
14614 is not up to date. */
14615 eassert (!NILP (w->window_end_valid));
14616
14617 /* A value of window_end_pos >= END_UNCHANGED means that the window
14618 end is in the range of changed text. If so, there is no
14619 unchanged row at the end of W's current matrix. */
14620 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14621 return NULL;
14622
14623 /* Set row to the last row in W's current matrix displaying text. */
14624 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14625
14626 /* If matrix is entirely empty, no unchanged row exists. */
14627 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14628 {
14629 /* The value of row is the last glyph row in the matrix having a
14630 meaningful buffer position in it. The end position of row
14631 corresponds to window_end_pos. This allows us to translate
14632 buffer positions in the current matrix to current buffer
14633 positions for characters not in changed text. */
14634 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14635 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14636 int last_unchanged_pos, last_unchanged_pos_old;
14637 struct glyph_row *first_text_row
14638 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14639
14640 *delta = Z - Z_old;
14641 *delta_bytes = Z_BYTE - Z_BYTE_old;
14642
14643 /* Set last_unchanged_pos to the buffer position of the last
14644 character in the buffer that has not been changed. Z is the
14645 index + 1 of the last character in current_buffer, i.e. by
14646 subtracting END_UNCHANGED we get the index of the last
14647 unchanged character, and we have to add BEG to get its buffer
14648 position. */
14649 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14650 last_unchanged_pos_old = last_unchanged_pos - *delta;
14651
14652 /* Search backward from ROW for a row displaying a line that
14653 starts at a minimum position >= last_unchanged_pos_old. */
14654 for (; row > first_text_row; --row)
14655 {
14656 /* This used to abort, but it can happen.
14657 It is ok to just stop the search instead here. KFS. */
14658 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14659 break;
14660
14661 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14662 row_found = row;
14663 }
14664 }
14665
14666 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14667
14668 return row_found;
14669 }
14670
14671
14672 /* Make sure that glyph rows in the current matrix of window W
14673 reference the same glyph memory as corresponding rows in the
14674 frame's frame matrix. This function is called after scrolling W's
14675 current matrix on a terminal frame in try_window_id and
14676 try_window_reusing_current_matrix. */
14677
14678 static void
14679 sync_frame_with_window_matrix_rows (w)
14680 struct window *w;
14681 {
14682 struct frame *f = XFRAME (w->frame);
14683 struct glyph_row *window_row, *window_row_end, *frame_row;
14684
14685 /* Preconditions: W must be a leaf window and full-width. Its frame
14686 must have a frame matrix. */
14687 xassert (NILP (w->hchild) && NILP (w->vchild));
14688 xassert (WINDOW_FULL_WIDTH_P (w));
14689 xassert (!FRAME_WINDOW_P (f));
14690
14691 /* If W is a full-width window, glyph pointers in W's current matrix
14692 have, by definition, to be the same as glyph pointers in the
14693 corresponding frame matrix. Note that frame matrices have no
14694 marginal areas (see build_frame_matrix). */
14695 window_row = w->current_matrix->rows;
14696 window_row_end = window_row + w->current_matrix->nrows;
14697 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14698 while (window_row < window_row_end)
14699 {
14700 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14701 struct glyph *end = window_row->glyphs[LAST_AREA];
14702
14703 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14704 frame_row->glyphs[TEXT_AREA] = start;
14705 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14706 frame_row->glyphs[LAST_AREA] = end;
14707
14708 /* Disable frame rows whose corresponding window rows have
14709 been disabled in try_window_id. */
14710 if (!window_row->enabled_p)
14711 frame_row->enabled_p = 0;
14712
14713 ++window_row, ++frame_row;
14714 }
14715 }
14716
14717
14718 /* Find the glyph row in window W containing CHARPOS. Consider all
14719 rows between START and END (not inclusive). END null means search
14720 all rows to the end of the display area of W. Value is the row
14721 containing CHARPOS or null. */
14722
14723 struct glyph_row *
14724 row_containing_pos (w, charpos, start, end, dy)
14725 struct window *w;
14726 int charpos;
14727 struct glyph_row *start, *end;
14728 int dy;
14729 {
14730 struct glyph_row *row = start;
14731 int last_y;
14732
14733 /* If we happen to start on a header-line, skip that. */
14734 if (row->mode_line_p)
14735 ++row;
14736
14737 if ((end && row >= end) || !row->enabled_p)
14738 return NULL;
14739
14740 last_y = window_text_bottom_y (w) - dy;
14741
14742 while (1)
14743 {
14744 /* Give up if we have gone too far. */
14745 if (end && row >= end)
14746 return NULL;
14747 /* This formerly returned if they were equal.
14748 I think that both quantities are of a "last plus one" type;
14749 if so, when they are equal, the row is within the screen. -- rms. */
14750 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14751 return NULL;
14752
14753 /* If it is in this row, return this row. */
14754 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14755 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14756 /* The end position of a row equals the start
14757 position of the next row. If CHARPOS is there, we
14758 would rather display it in the next line, except
14759 when this line ends in ZV. */
14760 && !row->ends_at_zv_p
14761 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14762 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14763 return row;
14764 ++row;
14765 }
14766 }
14767
14768
14769 /* Try to redisplay window W by reusing its existing display. W's
14770 current matrix must be up to date when this function is called,
14771 i.e. window_end_valid must not be nil.
14772
14773 Value is
14774
14775 1 if display has been updated
14776 0 if otherwise unsuccessful
14777 -1 if redisplay with same window start is known not to succeed
14778
14779 The following steps are performed:
14780
14781 1. Find the last row in the current matrix of W that is not
14782 affected by changes at the start of current_buffer. If no such row
14783 is found, give up.
14784
14785 2. Find the first row in W's current matrix that is not affected by
14786 changes at the end of current_buffer. Maybe there is no such row.
14787
14788 3. Display lines beginning with the row + 1 found in step 1 to the
14789 row found in step 2 or, if step 2 didn't find a row, to the end of
14790 the window.
14791
14792 4. If cursor is not known to appear on the window, give up.
14793
14794 5. If display stopped at the row found in step 2, scroll the
14795 display and current matrix as needed.
14796
14797 6. Maybe display some lines at the end of W, if we must. This can
14798 happen under various circumstances, like a partially visible line
14799 becoming fully visible, or because newly displayed lines are displayed
14800 in smaller font sizes.
14801
14802 7. Update W's window end information. */
14803
14804 static int
14805 try_window_id (w)
14806 struct window *w;
14807 {
14808 struct frame *f = XFRAME (w->frame);
14809 struct glyph_matrix *current_matrix = w->current_matrix;
14810 struct glyph_matrix *desired_matrix = w->desired_matrix;
14811 struct glyph_row *last_unchanged_at_beg_row;
14812 struct glyph_row *first_unchanged_at_end_row;
14813 struct glyph_row *row;
14814 struct glyph_row *bottom_row;
14815 int bottom_vpos;
14816 struct it it;
14817 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14818 struct text_pos start_pos;
14819 struct run run;
14820 int first_unchanged_at_end_vpos = 0;
14821 struct glyph_row *last_text_row, *last_text_row_at_end;
14822 struct text_pos start;
14823 int first_changed_charpos, last_changed_charpos;
14824
14825 #if GLYPH_DEBUG
14826 if (inhibit_try_window_id)
14827 return 0;
14828 #endif
14829
14830 /* This is handy for debugging. */
14831 #if 0
14832 #define GIVE_UP(X) \
14833 do { \
14834 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14835 return 0; \
14836 } while (0)
14837 #else
14838 #define GIVE_UP(X) return 0
14839 #endif
14840
14841 SET_TEXT_POS_FROM_MARKER (start, w->start);
14842
14843 /* Don't use this for mini-windows because these can show
14844 messages and mini-buffers, and we don't handle that here. */
14845 if (MINI_WINDOW_P (w))
14846 GIVE_UP (1);
14847
14848 /* This flag is used to prevent redisplay optimizations. */
14849 if (windows_or_buffers_changed || cursor_type_changed)
14850 GIVE_UP (2);
14851
14852 /* Verify that narrowing has not changed.
14853 Also verify that we were not told to prevent redisplay optimizations.
14854 It would be nice to further
14855 reduce the number of cases where this prevents try_window_id. */
14856 if (current_buffer->clip_changed
14857 || current_buffer->prevent_redisplay_optimizations_p)
14858 GIVE_UP (3);
14859
14860 /* Window must either use window-based redisplay or be full width. */
14861 if (!FRAME_WINDOW_P (f)
14862 && (!FRAME_LINE_INS_DEL_OK (f)
14863 || !WINDOW_FULL_WIDTH_P (w)))
14864 GIVE_UP (4);
14865
14866 /* Give up if point is known NOT to appear in W. */
14867 if (PT < CHARPOS (start))
14868 GIVE_UP (5);
14869
14870 /* Another way to prevent redisplay optimizations. */
14871 if (XFASTINT (w->last_modified) == 0)
14872 GIVE_UP (6);
14873
14874 /* Verify that window is not hscrolled. */
14875 if (XFASTINT (w->hscroll) != 0)
14876 GIVE_UP (7);
14877
14878 /* Verify that display wasn't paused. */
14879 if (NILP (w->window_end_valid))
14880 GIVE_UP (8);
14881
14882 /* Can't use this if highlighting a region because a cursor movement
14883 will do more than just set the cursor. */
14884 if (!NILP (Vtransient_mark_mode)
14885 && !NILP (current_buffer->mark_active))
14886 GIVE_UP (9);
14887
14888 /* Likewise if highlighting trailing whitespace. */
14889 if (!NILP (Vshow_trailing_whitespace))
14890 GIVE_UP (11);
14891
14892 /* Likewise if showing a region. */
14893 if (!NILP (w->region_showing))
14894 GIVE_UP (10);
14895
14896 /* Can't use this if overlay arrow position and/or string have
14897 changed. */
14898 if (overlay_arrows_changed_p ())
14899 GIVE_UP (12);
14900
14901 /* When word-wrap is on, adding a space to the first word of a
14902 wrapped line can change the wrap position, altering the line
14903 above it. It might be worthwhile to handle this more
14904 intelligently, but for now just redisplay from scratch. */
14905 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14906 GIVE_UP (21);
14907
14908 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14909 only if buffer has really changed. The reason is that the gap is
14910 initially at Z for freshly visited files. The code below would
14911 set end_unchanged to 0 in that case. */
14912 if (MODIFF > SAVE_MODIFF
14913 /* This seems to happen sometimes after saving a buffer. */
14914 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14915 {
14916 if (GPT - BEG < BEG_UNCHANGED)
14917 BEG_UNCHANGED = GPT - BEG;
14918 if (Z - GPT < END_UNCHANGED)
14919 END_UNCHANGED = Z - GPT;
14920 }
14921
14922 /* The position of the first and last character that has been changed. */
14923 first_changed_charpos = BEG + BEG_UNCHANGED;
14924 last_changed_charpos = Z - END_UNCHANGED;
14925
14926 /* If window starts after a line end, and the last change is in
14927 front of that newline, then changes don't affect the display.
14928 This case happens with stealth-fontification. Note that although
14929 the display is unchanged, glyph positions in the matrix have to
14930 be adjusted, of course. */
14931 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14932 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14933 && ((last_changed_charpos < CHARPOS (start)
14934 && CHARPOS (start) == BEGV)
14935 || (last_changed_charpos < CHARPOS (start) - 1
14936 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14937 {
14938 int Z_old, delta, Z_BYTE_old, delta_bytes;
14939 struct glyph_row *r0;
14940
14941 /* Compute how many chars/bytes have been added to or removed
14942 from the buffer. */
14943 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14944 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14945 delta = Z - Z_old;
14946 delta_bytes = Z_BYTE - Z_BYTE_old;
14947
14948 /* Give up if PT is not in the window. Note that it already has
14949 been checked at the start of try_window_id that PT is not in
14950 front of the window start. */
14951 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14952 GIVE_UP (13);
14953
14954 /* If window start is unchanged, we can reuse the whole matrix
14955 as is, after adjusting glyph positions. No need to compute
14956 the window end again, since its offset from Z hasn't changed. */
14957 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14958 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14959 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14960 /* PT must not be in a partially visible line. */
14961 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14962 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14963 {
14964 /* Adjust positions in the glyph matrix. */
14965 if (delta || delta_bytes)
14966 {
14967 struct glyph_row *r1
14968 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14969 increment_matrix_positions (w->current_matrix,
14970 MATRIX_ROW_VPOS (r0, current_matrix),
14971 MATRIX_ROW_VPOS (r1, current_matrix),
14972 delta, delta_bytes);
14973 }
14974
14975 /* Set the cursor. */
14976 row = row_containing_pos (w, PT, r0, NULL, 0);
14977 if (row)
14978 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14979 else
14980 abort ();
14981 return 1;
14982 }
14983 }
14984
14985 /* Handle the case that changes are all below what is displayed in
14986 the window, and that PT is in the window. This shortcut cannot
14987 be taken if ZV is visible in the window, and text has been added
14988 there that is visible in the window. */
14989 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14990 /* ZV is not visible in the window, or there are no
14991 changes at ZV, actually. */
14992 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14993 || first_changed_charpos == last_changed_charpos))
14994 {
14995 struct glyph_row *r0;
14996
14997 /* Give up if PT is not in the window. Note that it already has
14998 been checked at the start of try_window_id that PT is not in
14999 front of the window start. */
15000 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15001 GIVE_UP (14);
15002
15003 /* If window start is unchanged, we can reuse the whole matrix
15004 as is, without changing glyph positions since no text has
15005 been added/removed in front of the window end. */
15006 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15007 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15008 /* PT must not be in a partially visible line. */
15009 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15010 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15011 {
15012 /* We have to compute the window end anew since text
15013 can have been added/removed after it. */
15014 w->window_end_pos
15015 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15016 w->window_end_bytepos
15017 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15018
15019 /* Set the cursor. */
15020 row = row_containing_pos (w, PT, r0, NULL, 0);
15021 if (row)
15022 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15023 else
15024 abort ();
15025 return 2;
15026 }
15027 }
15028
15029 /* Give up if window start is in the changed area.
15030
15031 The condition used to read
15032
15033 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15034
15035 but why that was tested escapes me at the moment. */
15036 if (CHARPOS (start) >= first_changed_charpos
15037 && CHARPOS (start) <= last_changed_charpos)
15038 GIVE_UP (15);
15039
15040 /* Check that window start agrees with the start of the first glyph
15041 row in its current matrix. Check this after we know the window
15042 start is not in changed text, otherwise positions would not be
15043 comparable. */
15044 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15045 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15046 GIVE_UP (16);
15047
15048 /* Give up if the window ends in strings. Overlay strings
15049 at the end are difficult to handle, so don't try. */
15050 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15051 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15052 GIVE_UP (20);
15053
15054 /* Compute the position at which we have to start displaying new
15055 lines. Some of the lines at the top of the window might be
15056 reusable because they are not displaying changed text. Find the
15057 last row in W's current matrix not affected by changes at the
15058 start of current_buffer. Value is null if changes start in the
15059 first line of window. */
15060 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15061 if (last_unchanged_at_beg_row)
15062 {
15063 /* Avoid starting to display in the moddle of a character, a TAB
15064 for instance. This is easier than to set up the iterator
15065 exactly, and it's not a frequent case, so the additional
15066 effort wouldn't really pay off. */
15067 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15068 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15069 && last_unchanged_at_beg_row > w->current_matrix->rows)
15070 --last_unchanged_at_beg_row;
15071
15072 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15073 GIVE_UP (17);
15074
15075 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15076 GIVE_UP (18);
15077 start_pos = it.current.pos;
15078
15079 /* Start displaying new lines in the desired matrix at the same
15080 vpos we would use in the current matrix, i.e. below
15081 last_unchanged_at_beg_row. */
15082 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15083 current_matrix);
15084 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15085 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15086
15087 xassert (it.hpos == 0 && it.current_x == 0);
15088 }
15089 else
15090 {
15091 /* There are no reusable lines at the start of the window.
15092 Start displaying in the first text line. */
15093 start_display (&it, w, start);
15094 it.vpos = it.first_vpos;
15095 start_pos = it.current.pos;
15096 }
15097
15098 /* Find the first row that is not affected by changes at the end of
15099 the buffer. Value will be null if there is no unchanged row, in
15100 which case we must redisplay to the end of the window. delta
15101 will be set to the value by which buffer positions beginning with
15102 first_unchanged_at_end_row have to be adjusted due to text
15103 changes. */
15104 first_unchanged_at_end_row
15105 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15106 IF_DEBUG (debug_delta = delta);
15107 IF_DEBUG (debug_delta_bytes = delta_bytes);
15108
15109 /* Set stop_pos to the buffer position up to which we will have to
15110 display new lines. If first_unchanged_at_end_row != NULL, this
15111 is the buffer position of the start of the line displayed in that
15112 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15113 that we don't stop at a buffer position. */
15114 stop_pos = 0;
15115 if (first_unchanged_at_end_row)
15116 {
15117 xassert (last_unchanged_at_beg_row == NULL
15118 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15119
15120 /* If this is a continuation line, move forward to the next one
15121 that isn't. Changes in lines above affect this line.
15122 Caution: this may move first_unchanged_at_end_row to a row
15123 not displaying text. */
15124 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15125 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15126 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15127 < it.last_visible_y))
15128 ++first_unchanged_at_end_row;
15129
15130 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15131 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15132 >= it.last_visible_y))
15133 first_unchanged_at_end_row = NULL;
15134 else
15135 {
15136 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15137 + delta);
15138 first_unchanged_at_end_vpos
15139 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15140 xassert (stop_pos >= Z - END_UNCHANGED);
15141 }
15142 }
15143 else if (last_unchanged_at_beg_row == NULL)
15144 GIVE_UP (19);
15145
15146
15147 #if GLYPH_DEBUG
15148
15149 /* Either there is no unchanged row at the end, or the one we have
15150 now displays text. This is a necessary condition for the window
15151 end pos calculation at the end of this function. */
15152 xassert (first_unchanged_at_end_row == NULL
15153 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15154
15155 debug_last_unchanged_at_beg_vpos
15156 = (last_unchanged_at_beg_row
15157 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15158 : -1);
15159 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15160
15161 #endif /* GLYPH_DEBUG != 0 */
15162
15163
15164 /* Display new lines. Set last_text_row to the last new line
15165 displayed which has text on it, i.e. might end up as being the
15166 line where the window_end_vpos is. */
15167 w->cursor.vpos = -1;
15168 last_text_row = NULL;
15169 overlay_arrow_seen = 0;
15170 while (it.current_y < it.last_visible_y
15171 && !fonts_changed_p
15172 && (first_unchanged_at_end_row == NULL
15173 || IT_CHARPOS (it) < stop_pos))
15174 {
15175 if (display_line (&it))
15176 last_text_row = it.glyph_row - 1;
15177 }
15178
15179 if (fonts_changed_p)
15180 return -1;
15181
15182
15183 /* Compute differences in buffer positions, y-positions etc. for
15184 lines reused at the bottom of the window. Compute what we can
15185 scroll. */
15186 if (first_unchanged_at_end_row
15187 /* No lines reused because we displayed everything up to the
15188 bottom of the window. */
15189 && it.current_y < it.last_visible_y)
15190 {
15191 dvpos = (it.vpos
15192 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15193 current_matrix));
15194 dy = it.current_y - first_unchanged_at_end_row->y;
15195 run.current_y = first_unchanged_at_end_row->y;
15196 run.desired_y = run.current_y + dy;
15197 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15198 }
15199 else
15200 {
15201 delta = delta_bytes = dvpos = dy
15202 = run.current_y = run.desired_y = run.height = 0;
15203 first_unchanged_at_end_row = NULL;
15204 }
15205 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15206
15207
15208 /* Find the cursor if not already found. We have to decide whether
15209 PT will appear on this window (it sometimes doesn't, but this is
15210 not a very frequent case.) This decision has to be made before
15211 the current matrix is altered. A value of cursor.vpos < 0 means
15212 that PT is either in one of the lines beginning at
15213 first_unchanged_at_end_row or below the window. Don't care for
15214 lines that might be displayed later at the window end; as
15215 mentioned, this is not a frequent case. */
15216 if (w->cursor.vpos < 0)
15217 {
15218 /* Cursor in unchanged rows at the top? */
15219 if (PT < CHARPOS (start_pos)
15220 && last_unchanged_at_beg_row)
15221 {
15222 row = row_containing_pos (w, PT,
15223 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15224 last_unchanged_at_beg_row + 1, 0);
15225 if (row)
15226 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15227 }
15228
15229 /* Start from first_unchanged_at_end_row looking for PT. */
15230 else if (first_unchanged_at_end_row)
15231 {
15232 row = row_containing_pos (w, PT - delta,
15233 first_unchanged_at_end_row, NULL, 0);
15234 if (row)
15235 set_cursor_from_row (w, row, w->current_matrix, delta,
15236 delta_bytes, dy, dvpos);
15237 }
15238
15239 /* Give up if cursor was not found. */
15240 if (w->cursor.vpos < 0)
15241 {
15242 clear_glyph_matrix (w->desired_matrix);
15243 return -1;
15244 }
15245 }
15246
15247 /* Don't let the cursor end in the scroll margins. */
15248 {
15249 int this_scroll_margin, cursor_height;
15250
15251 this_scroll_margin = max (0, scroll_margin);
15252 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15253 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15254 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15255
15256 if ((w->cursor.y < this_scroll_margin
15257 && CHARPOS (start) > BEGV)
15258 /* Old redisplay didn't take scroll margin into account at the bottom,
15259 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15260 || (w->cursor.y + (make_cursor_line_fully_visible_p
15261 ? cursor_height + this_scroll_margin
15262 : 1)) > it.last_visible_y)
15263 {
15264 w->cursor.vpos = -1;
15265 clear_glyph_matrix (w->desired_matrix);
15266 return -1;
15267 }
15268 }
15269
15270 /* Scroll the display. Do it before changing the current matrix so
15271 that xterm.c doesn't get confused about where the cursor glyph is
15272 found. */
15273 if (dy && run.height)
15274 {
15275 update_begin (f);
15276
15277 if (FRAME_WINDOW_P (f))
15278 {
15279 FRAME_RIF (f)->update_window_begin_hook (w);
15280 FRAME_RIF (f)->clear_window_mouse_face (w);
15281 FRAME_RIF (f)->scroll_run_hook (w, &run);
15282 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15283 }
15284 else
15285 {
15286 /* Terminal frame. In this case, dvpos gives the number of
15287 lines to scroll by; dvpos < 0 means scroll up. */
15288 int first_unchanged_at_end_vpos
15289 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15290 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15291 int end = (WINDOW_TOP_EDGE_LINE (w)
15292 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15293 + window_internal_height (w));
15294
15295 /* Perform the operation on the screen. */
15296 if (dvpos > 0)
15297 {
15298 /* Scroll last_unchanged_at_beg_row to the end of the
15299 window down dvpos lines. */
15300 set_terminal_window (f, end);
15301
15302 /* On dumb terminals delete dvpos lines at the end
15303 before inserting dvpos empty lines. */
15304 if (!FRAME_SCROLL_REGION_OK (f))
15305 ins_del_lines (f, end - dvpos, -dvpos);
15306
15307 /* Insert dvpos empty lines in front of
15308 last_unchanged_at_beg_row. */
15309 ins_del_lines (f, from, dvpos);
15310 }
15311 else if (dvpos < 0)
15312 {
15313 /* Scroll up last_unchanged_at_beg_vpos to the end of
15314 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15315 set_terminal_window (f, end);
15316
15317 /* Delete dvpos lines in front of
15318 last_unchanged_at_beg_vpos. ins_del_lines will set
15319 the cursor to the given vpos and emit |dvpos| delete
15320 line sequences. */
15321 ins_del_lines (f, from + dvpos, dvpos);
15322
15323 /* On a dumb terminal insert dvpos empty lines at the
15324 end. */
15325 if (!FRAME_SCROLL_REGION_OK (f))
15326 ins_del_lines (f, end + dvpos, -dvpos);
15327 }
15328
15329 set_terminal_window (f, 0);
15330 }
15331
15332 update_end (f);
15333 }
15334
15335 /* Shift reused rows of the current matrix to the right position.
15336 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15337 text. */
15338 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15339 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15340 if (dvpos < 0)
15341 {
15342 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15343 bottom_vpos, dvpos);
15344 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15345 bottom_vpos, 0);
15346 }
15347 else if (dvpos > 0)
15348 {
15349 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15350 bottom_vpos, dvpos);
15351 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15352 first_unchanged_at_end_vpos + dvpos, 0);
15353 }
15354
15355 /* For frame-based redisplay, make sure that current frame and window
15356 matrix are in sync with respect to glyph memory. */
15357 if (!FRAME_WINDOW_P (f))
15358 sync_frame_with_window_matrix_rows (w);
15359
15360 /* Adjust buffer positions in reused rows. */
15361 if (delta || delta_bytes)
15362 increment_matrix_positions (current_matrix,
15363 first_unchanged_at_end_vpos + dvpos,
15364 bottom_vpos, delta, delta_bytes);
15365
15366 /* Adjust Y positions. */
15367 if (dy)
15368 shift_glyph_matrix (w, current_matrix,
15369 first_unchanged_at_end_vpos + dvpos,
15370 bottom_vpos, dy);
15371
15372 if (first_unchanged_at_end_row)
15373 {
15374 first_unchanged_at_end_row += dvpos;
15375 if (first_unchanged_at_end_row->y >= it.last_visible_y
15376 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15377 first_unchanged_at_end_row = NULL;
15378 }
15379
15380 /* If scrolling up, there may be some lines to display at the end of
15381 the window. */
15382 last_text_row_at_end = NULL;
15383 if (dy < 0)
15384 {
15385 /* Scrolling up can leave for example a partially visible line
15386 at the end of the window to be redisplayed. */
15387 /* Set last_row to the glyph row in the current matrix where the
15388 window end line is found. It has been moved up or down in
15389 the matrix by dvpos. */
15390 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15391 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15392
15393 /* If last_row is the window end line, it should display text. */
15394 xassert (last_row->displays_text_p);
15395
15396 /* If window end line was partially visible before, begin
15397 displaying at that line. Otherwise begin displaying with the
15398 line following it. */
15399 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15400 {
15401 init_to_row_start (&it, w, last_row);
15402 it.vpos = last_vpos;
15403 it.current_y = last_row->y;
15404 }
15405 else
15406 {
15407 init_to_row_end (&it, w, last_row);
15408 it.vpos = 1 + last_vpos;
15409 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15410 ++last_row;
15411 }
15412
15413 /* We may start in a continuation line. If so, we have to
15414 get the right continuation_lines_width and current_x. */
15415 it.continuation_lines_width = last_row->continuation_lines_width;
15416 it.hpos = it.current_x = 0;
15417
15418 /* Display the rest of the lines at the window end. */
15419 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15420 while (it.current_y < it.last_visible_y
15421 && !fonts_changed_p)
15422 {
15423 /* Is it always sure that the display agrees with lines in
15424 the current matrix? I don't think so, so we mark rows
15425 displayed invalid in the current matrix by setting their
15426 enabled_p flag to zero. */
15427 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15428 if (display_line (&it))
15429 last_text_row_at_end = it.glyph_row - 1;
15430 }
15431 }
15432
15433 /* Update window_end_pos and window_end_vpos. */
15434 if (first_unchanged_at_end_row
15435 && !last_text_row_at_end)
15436 {
15437 /* Window end line if one of the preserved rows from the current
15438 matrix. Set row to the last row displaying text in current
15439 matrix starting at first_unchanged_at_end_row, after
15440 scrolling. */
15441 xassert (first_unchanged_at_end_row->displays_text_p);
15442 row = find_last_row_displaying_text (w->current_matrix, &it,
15443 first_unchanged_at_end_row);
15444 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15445
15446 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15447 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15448 w->window_end_vpos
15449 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15450 xassert (w->window_end_bytepos >= 0);
15451 IF_DEBUG (debug_method_add (w, "A"));
15452 }
15453 else if (last_text_row_at_end)
15454 {
15455 w->window_end_pos
15456 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15457 w->window_end_bytepos
15458 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15459 w->window_end_vpos
15460 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15461 xassert (w->window_end_bytepos >= 0);
15462 IF_DEBUG (debug_method_add (w, "B"));
15463 }
15464 else if (last_text_row)
15465 {
15466 /* We have displayed either to the end of the window or at the
15467 end of the window, i.e. the last row with text is to be found
15468 in the desired matrix. */
15469 w->window_end_pos
15470 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15471 w->window_end_bytepos
15472 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15473 w->window_end_vpos
15474 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15475 xassert (w->window_end_bytepos >= 0);
15476 }
15477 else if (first_unchanged_at_end_row == NULL
15478 && last_text_row == NULL
15479 && last_text_row_at_end == NULL)
15480 {
15481 /* Displayed to end of window, but no line containing text was
15482 displayed. Lines were deleted at the end of the window. */
15483 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15484 int vpos = XFASTINT (w->window_end_vpos);
15485 struct glyph_row *current_row = current_matrix->rows + vpos;
15486 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15487
15488 for (row = NULL;
15489 row == NULL && vpos >= first_vpos;
15490 --vpos, --current_row, --desired_row)
15491 {
15492 if (desired_row->enabled_p)
15493 {
15494 if (desired_row->displays_text_p)
15495 row = desired_row;
15496 }
15497 else if (current_row->displays_text_p)
15498 row = current_row;
15499 }
15500
15501 xassert (row != NULL);
15502 w->window_end_vpos = make_number (vpos + 1);
15503 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15504 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15505 xassert (w->window_end_bytepos >= 0);
15506 IF_DEBUG (debug_method_add (w, "C"));
15507 }
15508 else
15509 abort ();
15510
15511 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15512 debug_end_vpos = XFASTINT (w->window_end_vpos));
15513
15514 /* Record that display has not been completed. */
15515 w->window_end_valid = Qnil;
15516 w->desired_matrix->no_scrolling_p = 1;
15517 return 3;
15518
15519 #undef GIVE_UP
15520 }
15521
15522
15523 \f
15524 /***********************************************************************
15525 More debugging support
15526 ***********************************************************************/
15527
15528 #if GLYPH_DEBUG
15529
15530 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15531 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15532 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15533
15534
15535 /* Dump the contents of glyph matrix MATRIX on stderr.
15536
15537 GLYPHS 0 means don't show glyph contents.
15538 GLYPHS 1 means show glyphs in short form
15539 GLYPHS > 1 means show glyphs in long form. */
15540
15541 void
15542 dump_glyph_matrix (matrix, glyphs)
15543 struct glyph_matrix *matrix;
15544 int glyphs;
15545 {
15546 int i;
15547 for (i = 0; i < matrix->nrows; ++i)
15548 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15549 }
15550
15551
15552 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15553 the glyph row and area where the glyph comes from. */
15554
15555 void
15556 dump_glyph (row, glyph, area)
15557 struct glyph_row *row;
15558 struct glyph *glyph;
15559 int area;
15560 {
15561 if (glyph->type == CHAR_GLYPH)
15562 {
15563 fprintf (stderr,
15564 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15565 glyph - row->glyphs[TEXT_AREA],
15566 'C',
15567 glyph->charpos,
15568 (BUFFERP (glyph->object)
15569 ? 'B'
15570 : (STRINGP (glyph->object)
15571 ? 'S'
15572 : '-')),
15573 glyph->pixel_width,
15574 glyph->u.ch,
15575 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15576 ? glyph->u.ch
15577 : '.'),
15578 glyph->face_id,
15579 glyph->left_box_line_p,
15580 glyph->right_box_line_p);
15581 }
15582 else if (glyph->type == STRETCH_GLYPH)
15583 {
15584 fprintf (stderr,
15585 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15586 glyph - row->glyphs[TEXT_AREA],
15587 'S',
15588 glyph->charpos,
15589 (BUFFERP (glyph->object)
15590 ? 'B'
15591 : (STRINGP (glyph->object)
15592 ? 'S'
15593 : '-')),
15594 glyph->pixel_width,
15595 0,
15596 '.',
15597 glyph->face_id,
15598 glyph->left_box_line_p,
15599 glyph->right_box_line_p);
15600 }
15601 else if (glyph->type == IMAGE_GLYPH)
15602 {
15603 fprintf (stderr,
15604 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15605 glyph - row->glyphs[TEXT_AREA],
15606 'I',
15607 glyph->charpos,
15608 (BUFFERP (glyph->object)
15609 ? 'B'
15610 : (STRINGP (glyph->object)
15611 ? 'S'
15612 : '-')),
15613 glyph->pixel_width,
15614 glyph->u.img_id,
15615 '.',
15616 glyph->face_id,
15617 glyph->left_box_line_p,
15618 glyph->right_box_line_p);
15619 }
15620 else if (glyph->type == COMPOSITE_GLYPH)
15621 {
15622 fprintf (stderr,
15623 " %5d %4c %6d %c %3d 0x%05x",
15624 glyph - row->glyphs[TEXT_AREA],
15625 '+',
15626 glyph->charpos,
15627 (BUFFERP (glyph->object)
15628 ? 'B'
15629 : (STRINGP (glyph->object)
15630 ? 'S'
15631 : '-')),
15632 glyph->pixel_width,
15633 glyph->u.cmp.id);
15634 if (glyph->u.cmp.automatic)
15635 fprintf (stderr,
15636 "[%d-%d]",
15637 glyph->u.cmp.from, glyph->u.cmp.to);
15638 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15639 glyph->face_id,
15640 glyph->left_box_line_p,
15641 glyph->right_box_line_p);
15642 }
15643 }
15644
15645
15646 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15647 GLYPHS 0 means don't show glyph contents.
15648 GLYPHS 1 means show glyphs in short form
15649 GLYPHS > 1 means show glyphs in long form. */
15650
15651 void
15652 dump_glyph_row (row, vpos, glyphs)
15653 struct glyph_row *row;
15654 int vpos, glyphs;
15655 {
15656 if (glyphs != 1)
15657 {
15658 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15659 fprintf (stderr, "======================================================================\n");
15660
15661 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15662 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15663 vpos,
15664 MATRIX_ROW_START_CHARPOS (row),
15665 MATRIX_ROW_END_CHARPOS (row),
15666 row->used[TEXT_AREA],
15667 row->contains_overlapping_glyphs_p,
15668 row->enabled_p,
15669 row->truncated_on_left_p,
15670 row->truncated_on_right_p,
15671 row->continued_p,
15672 MATRIX_ROW_CONTINUATION_LINE_P (row),
15673 row->displays_text_p,
15674 row->ends_at_zv_p,
15675 row->fill_line_p,
15676 row->ends_in_middle_of_char_p,
15677 row->starts_in_middle_of_char_p,
15678 row->mouse_face_p,
15679 row->x,
15680 row->y,
15681 row->pixel_width,
15682 row->height,
15683 row->visible_height,
15684 row->ascent,
15685 row->phys_ascent);
15686 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15687 row->end.overlay_string_index,
15688 row->continuation_lines_width);
15689 fprintf (stderr, "%9d %5d\n",
15690 CHARPOS (row->start.string_pos),
15691 CHARPOS (row->end.string_pos));
15692 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15693 row->end.dpvec_index);
15694 }
15695
15696 if (glyphs > 1)
15697 {
15698 int area;
15699
15700 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15701 {
15702 struct glyph *glyph = row->glyphs[area];
15703 struct glyph *glyph_end = glyph + row->used[area];
15704
15705 /* Glyph for a line end in text. */
15706 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15707 ++glyph_end;
15708
15709 if (glyph < glyph_end)
15710 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15711
15712 for (; glyph < glyph_end; ++glyph)
15713 dump_glyph (row, glyph, area);
15714 }
15715 }
15716 else if (glyphs == 1)
15717 {
15718 int area;
15719
15720 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15721 {
15722 char *s = (char *) alloca (row->used[area] + 1);
15723 int i;
15724
15725 for (i = 0; i < row->used[area]; ++i)
15726 {
15727 struct glyph *glyph = row->glyphs[area] + i;
15728 if (glyph->type == CHAR_GLYPH
15729 && glyph->u.ch < 0x80
15730 && glyph->u.ch >= ' ')
15731 s[i] = glyph->u.ch;
15732 else
15733 s[i] = '.';
15734 }
15735
15736 s[i] = '\0';
15737 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15738 }
15739 }
15740 }
15741
15742
15743 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15744 Sdump_glyph_matrix, 0, 1, "p",
15745 doc: /* Dump the current matrix of the selected window to stderr.
15746 Shows contents of glyph row structures. With non-nil
15747 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15748 glyphs in short form, otherwise show glyphs in long form. */)
15749 (glyphs)
15750 Lisp_Object glyphs;
15751 {
15752 struct window *w = XWINDOW (selected_window);
15753 struct buffer *buffer = XBUFFER (w->buffer);
15754
15755 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15756 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15757 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15758 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15759 fprintf (stderr, "=============================================\n");
15760 dump_glyph_matrix (w->current_matrix,
15761 NILP (glyphs) ? 0 : XINT (glyphs));
15762 return Qnil;
15763 }
15764
15765
15766 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15767 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15768 ()
15769 {
15770 struct frame *f = XFRAME (selected_frame);
15771 dump_glyph_matrix (f->current_matrix, 1);
15772 return Qnil;
15773 }
15774
15775
15776 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15777 doc: /* Dump glyph row ROW to stderr.
15778 GLYPH 0 means don't dump glyphs.
15779 GLYPH 1 means dump glyphs in short form.
15780 GLYPH > 1 or omitted means dump glyphs in long form. */)
15781 (row, glyphs)
15782 Lisp_Object row, glyphs;
15783 {
15784 struct glyph_matrix *matrix;
15785 int vpos;
15786
15787 CHECK_NUMBER (row);
15788 matrix = XWINDOW (selected_window)->current_matrix;
15789 vpos = XINT (row);
15790 if (vpos >= 0 && vpos < matrix->nrows)
15791 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15792 vpos,
15793 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15794 return Qnil;
15795 }
15796
15797
15798 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15799 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15800 GLYPH 0 means don't dump glyphs.
15801 GLYPH 1 means dump glyphs in short form.
15802 GLYPH > 1 or omitted means dump glyphs in long form. */)
15803 (row, glyphs)
15804 Lisp_Object row, glyphs;
15805 {
15806 struct frame *sf = SELECTED_FRAME ();
15807 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15808 int vpos;
15809
15810 CHECK_NUMBER (row);
15811 vpos = XINT (row);
15812 if (vpos >= 0 && vpos < m->nrows)
15813 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15814 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15815 return Qnil;
15816 }
15817
15818
15819 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15820 doc: /* Toggle tracing of redisplay.
15821 With ARG, turn tracing on if and only if ARG is positive. */)
15822 (arg)
15823 Lisp_Object arg;
15824 {
15825 if (NILP (arg))
15826 trace_redisplay_p = !trace_redisplay_p;
15827 else
15828 {
15829 arg = Fprefix_numeric_value (arg);
15830 trace_redisplay_p = XINT (arg) > 0;
15831 }
15832
15833 return Qnil;
15834 }
15835
15836
15837 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15838 doc: /* Like `format', but print result to stderr.
15839 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15840 (nargs, args)
15841 int nargs;
15842 Lisp_Object *args;
15843 {
15844 Lisp_Object s = Fformat (nargs, args);
15845 fprintf (stderr, "%s", SDATA (s));
15846 return Qnil;
15847 }
15848
15849 #endif /* GLYPH_DEBUG */
15850
15851
15852 \f
15853 /***********************************************************************
15854 Building Desired Matrix Rows
15855 ***********************************************************************/
15856
15857 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15858 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15859
15860 static struct glyph_row *
15861 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15862 struct window *w;
15863 Lisp_Object overlay_arrow_string;
15864 {
15865 struct frame *f = XFRAME (WINDOW_FRAME (w));
15866 struct buffer *buffer = XBUFFER (w->buffer);
15867 struct buffer *old = current_buffer;
15868 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15869 int arrow_len = SCHARS (overlay_arrow_string);
15870 const unsigned char *arrow_end = arrow_string + arrow_len;
15871 const unsigned char *p;
15872 struct it it;
15873 int multibyte_p;
15874 int n_glyphs_before;
15875
15876 set_buffer_temp (buffer);
15877 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15878 it.glyph_row->used[TEXT_AREA] = 0;
15879 SET_TEXT_POS (it.position, 0, 0);
15880
15881 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15882 p = arrow_string;
15883 while (p < arrow_end)
15884 {
15885 Lisp_Object face, ilisp;
15886
15887 /* Get the next character. */
15888 if (multibyte_p)
15889 it.c = it.char_to_display = string_char_and_length (p, &it.len);
15890 else
15891 {
15892 it.c = it.char_to_display = *p, it.len = 1;
15893 if (! ASCII_CHAR_P (it.c))
15894 it.char_to_display = BYTE8_TO_CHAR (it.c);
15895 }
15896 p += it.len;
15897
15898 /* Get its face. */
15899 ilisp = make_number (p - arrow_string);
15900 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15901 it.face_id = compute_char_face (f, it.char_to_display, face);
15902
15903 /* Compute its width, get its glyphs. */
15904 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15905 SET_TEXT_POS (it.position, -1, -1);
15906 PRODUCE_GLYPHS (&it);
15907
15908 /* If this character doesn't fit any more in the line, we have
15909 to remove some glyphs. */
15910 if (it.current_x > it.last_visible_x)
15911 {
15912 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15913 break;
15914 }
15915 }
15916
15917 set_buffer_temp (old);
15918 return it.glyph_row;
15919 }
15920
15921
15922 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15923 glyphs are only inserted for terminal frames since we can't really
15924 win with truncation glyphs when partially visible glyphs are
15925 involved. Which glyphs to insert is determined by
15926 produce_special_glyphs. */
15927
15928 static void
15929 insert_left_trunc_glyphs (it)
15930 struct it *it;
15931 {
15932 struct it truncate_it;
15933 struct glyph *from, *end, *to, *toend;
15934
15935 xassert (!FRAME_WINDOW_P (it->f));
15936
15937 /* Get the truncation glyphs. */
15938 truncate_it = *it;
15939 truncate_it.current_x = 0;
15940 truncate_it.face_id = DEFAULT_FACE_ID;
15941 truncate_it.glyph_row = &scratch_glyph_row;
15942 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15943 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15944 truncate_it.object = make_number (0);
15945 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15946
15947 /* Overwrite glyphs from IT with truncation glyphs. */
15948 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15949 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15950 to = it->glyph_row->glyphs[TEXT_AREA];
15951 toend = to + it->glyph_row->used[TEXT_AREA];
15952
15953 while (from < end)
15954 *to++ = *from++;
15955
15956 /* There may be padding glyphs left over. Overwrite them too. */
15957 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15958 {
15959 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15960 while (from < end)
15961 *to++ = *from++;
15962 }
15963
15964 if (to > toend)
15965 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15966 }
15967
15968
15969 /* Compute the pixel height and width of IT->glyph_row.
15970
15971 Most of the time, ascent and height of a display line will be equal
15972 to the max_ascent and max_height values of the display iterator
15973 structure. This is not the case if
15974
15975 1. We hit ZV without displaying anything. In this case, max_ascent
15976 and max_height will be zero.
15977
15978 2. We have some glyphs that don't contribute to the line height.
15979 (The glyph row flag contributes_to_line_height_p is for future
15980 pixmap extensions).
15981
15982 The first case is easily covered by using default values because in
15983 these cases, the line height does not really matter, except that it
15984 must not be zero. */
15985
15986 static void
15987 compute_line_metrics (it)
15988 struct it *it;
15989 {
15990 struct glyph_row *row = it->glyph_row;
15991 int area, i;
15992
15993 if (FRAME_WINDOW_P (it->f))
15994 {
15995 int i, min_y, max_y;
15996
15997 /* The line may consist of one space only, that was added to
15998 place the cursor on it. If so, the row's height hasn't been
15999 computed yet. */
16000 if (row->height == 0)
16001 {
16002 if (it->max_ascent + it->max_descent == 0)
16003 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16004 row->ascent = it->max_ascent;
16005 row->height = it->max_ascent + it->max_descent;
16006 row->phys_ascent = it->max_phys_ascent;
16007 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16008 row->extra_line_spacing = it->max_extra_line_spacing;
16009 }
16010
16011 /* Compute the width of this line. */
16012 row->pixel_width = row->x;
16013 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16014 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16015
16016 xassert (row->pixel_width >= 0);
16017 xassert (row->ascent >= 0 && row->height > 0);
16018
16019 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16020 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16021
16022 /* If first line's physical ascent is larger than its logical
16023 ascent, use the physical ascent, and make the row taller.
16024 This makes accented characters fully visible. */
16025 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16026 && row->phys_ascent > row->ascent)
16027 {
16028 row->height += row->phys_ascent - row->ascent;
16029 row->ascent = row->phys_ascent;
16030 }
16031
16032 /* Compute how much of the line is visible. */
16033 row->visible_height = row->height;
16034
16035 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16036 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16037
16038 if (row->y < min_y)
16039 row->visible_height -= min_y - row->y;
16040 if (row->y + row->height > max_y)
16041 row->visible_height -= row->y + row->height - max_y;
16042 }
16043 else
16044 {
16045 row->pixel_width = row->used[TEXT_AREA];
16046 if (row->continued_p)
16047 row->pixel_width -= it->continuation_pixel_width;
16048 else if (row->truncated_on_right_p)
16049 row->pixel_width -= it->truncation_pixel_width;
16050 row->ascent = row->phys_ascent = 0;
16051 row->height = row->phys_height = row->visible_height = 1;
16052 row->extra_line_spacing = 0;
16053 }
16054
16055 /* Compute a hash code for this row. */
16056 row->hash = 0;
16057 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16058 for (i = 0; i < row->used[area]; ++i)
16059 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16060 + row->glyphs[area][i].u.val
16061 + row->glyphs[area][i].face_id
16062 + row->glyphs[area][i].padding_p
16063 + (row->glyphs[area][i].type << 2));
16064
16065 it->max_ascent = it->max_descent = 0;
16066 it->max_phys_ascent = it->max_phys_descent = 0;
16067 }
16068
16069
16070 /* Append one space to the glyph row of iterator IT if doing a
16071 window-based redisplay. The space has the same face as
16072 IT->face_id. Value is non-zero if a space was added.
16073
16074 This function is called to make sure that there is always one glyph
16075 at the end of a glyph row that the cursor can be set on under
16076 window-systems. (If there weren't such a glyph we would not know
16077 how wide and tall a box cursor should be displayed).
16078
16079 At the same time this space let's a nicely handle clearing to the
16080 end of the line if the row ends in italic text. */
16081
16082 static int
16083 append_space_for_newline (it, default_face_p)
16084 struct it *it;
16085 int default_face_p;
16086 {
16087 if (FRAME_WINDOW_P (it->f))
16088 {
16089 int n = it->glyph_row->used[TEXT_AREA];
16090
16091 if (it->glyph_row->glyphs[TEXT_AREA] + n
16092 < it->glyph_row->glyphs[1 + TEXT_AREA])
16093 {
16094 /* Save some values that must not be changed.
16095 Must save IT->c and IT->len because otherwise
16096 ITERATOR_AT_END_P wouldn't work anymore after
16097 append_space_for_newline has been called. */
16098 enum display_element_type saved_what = it->what;
16099 int saved_c = it->c, saved_len = it->len;
16100 int saved_char_to_display = it->char_to_display;
16101 int saved_x = it->current_x;
16102 int saved_face_id = it->face_id;
16103 struct text_pos saved_pos;
16104 Lisp_Object saved_object;
16105 struct face *face;
16106
16107 saved_object = it->object;
16108 saved_pos = it->position;
16109
16110 it->what = IT_CHARACTER;
16111 bzero (&it->position, sizeof it->position);
16112 it->object = make_number (0);
16113 it->c = it->char_to_display = ' ';
16114 it->len = 1;
16115
16116 if (default_face_p)
16117 it->face_id = DEFAULT_FACE_ID;
16118 else if (it->face_before_selective_p)
16119 it->face_id = it->saved_face_id;
16120 face = FACE_FROM_ID (it->f, it->face_id);
16121 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16122
16123 PRODUCE_GLYPHS (it);
16124
16125 it->override_ascent = -1;
16126 it->constrain_row_ascent_descent_p = 0;
16127 it->current_x = saved_x;
16128 it->object = saved_object;
16129 it->position = saved_pos;
16130 it->what = saved_what;
16131 it->face_id = saved_face_id;
16132 it->len = saved_len;
16133 it->c = saved_c;
16134 it->char_to_display = saved_char_to_display;
16135 return 1;
16136 }
16137 }
16138
16139 return 0;
16140 }
16141
16142
16143 /* Extend the face of the last glyph in the text area of IT->glyph_row
16144 to the end of the display line. Called from display_line.
16145 If the glyph row is empty, add a space glyph to it so that we
16146 know the face to draw. Set the glyph row flag fill_line_p. */
16147
16148 static void
16149 extend_face_to_end_of_line (it)
16150 struct it *it;
16151 {
16152 struct face *face;
16153 struct frame *f = it->f;
16154
16155 /* If line is already filled, do nothing. */
16156 if (it->current_x >= it->last_visible_x)
16157 return;
16158
16159 /* Face extension extends the background and box of IT->face_id
16160 to the end of the line. If the background equals the background
16161 of the frame, we don't have to do anything. */
16162 if (it->face_before_selective_p)
16163 face = FACE_FROM_ID (it->f, it->saved_face_id);
16164 else
16165 face = FACE_FROM_ID (f, it->face_id);
16166
16167 if (FRAME_WINDOW_P (f)
16168 && it->glyph_row->displays_text_p
16169 && face->box == FACE_NO_BOX
16170 && face->background == FRAME_BACKGROUND_PIXEL (f)
16171 && !face->stipple)
16172 return;
16173
16174 /* Set the glyph row flag indicating that the face of the last glyph
16175 in the text area has to be drawn to the end of the text area. */
16176 it->glyph_row->fill_line_p = 1;
16177
16178 /* If current character of IT is not ASCII, make sure we have the
16179 ASCII face. This will be automatically undone the next time
16180 get_next_display_element returns a multibyte character. Note
16181 that the character will always be single byte in unibyte
16182 text. */
16183 if (!ASCII_CHAR_P (it->c))
16184 {
16185 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16186 }
16187
16188 if (FRAME_WINDOW_P (f))
16189 {
16190 /* If the row is empty, add a space with the current face of IT,
16191 so that we know which face to draw. */
16192 if (it->glyph_row->used[TEXT_AREA] == 0)
16193 {
16194 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16195 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16196 it->glyph_row->used[TEXT_AREA] = 1;
16197 }
16198 }
16199 else
16200 {
16201 /* Save some values that must not be changed. */
16202 int saved_x = it->current_x;
16203 struct text_pos saved_pos;
16204 Lisp_Object saved_object;
16205 enum display_element_type saved_what = it->what;
16206 int saved_face_id = it->face_id;
16207
16208 saved_object = it->object;
16209 saved_pos = it->position;
16210
16211 it->what = IT_CHARACTER;
16212 bzero (&it->position, sizeof it->position);
16213 it->object = make_number (0);
16214 it->c = it->char_to_display = ' ';
16215 it->len = 1;
16216 it->face_id = face->id;
16217
16218 PRODUCE_GLYPHS (it);
16219
16220 while (it->current_x <= it->last_visible_x)
16221 PRODUCE_GLYPHS (it);
16222
16223 /* Don't count these blanks really. It would let us insert a left
16224 truncation glyph below and make us set the cursor on them, maybe. */
16225 it->current_x = saved_x;
16226 it->object = saved_object;
16227 it->position = saved_pos;
16228 it->what = saved_what;
16229 it->face_id = saved_face_id;
16230 }
16231 }
16232
16233
16234 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16235 trailing whitespace. */
16236
16237 static int
16238 trailing_whitespace_p (charpos)
16239 int charpos;
16240 {
16241 int bytepos = CHAR_TO_BYTE (charpos);
16242 int c = 0;
16243
16244 while (bytepos < ZV_BYTE
16245 && (c = FETCH_CHAR (bytepos),
16246 c == ' ' || c == '\t'))
16247 ++bytepos;
16248
16249 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16250 {
16251 if (bytepos != PT_BYTE)
16252 return 1;
16253 }
16254 return 0;
16255 }
16256
16257
16258 /* Highlight trailing whitespace, if any, in ROW. */
16259
16260 void
16261 highlight_trailing_whitespace (f, row)
16262 struct frame *f;
16263 struct glyph_row *row;
16264 {
16265 int used = row->used[TEXT_AREA];
16266
16267 if (used)
16268 {
16269 struct glyph *start = row->glyphs[TEXT_AREA];
16270 struct glyph *glyph = start + used - 1;
16271
16272 /* Skip over glyphs inserted to display the cursor at the
16273 end of a line, for extending the face of the last glyph
16274 to the end of the line on terminals, and for truncation
16275 and continuation glyphs. */
16276 while (glyph >= start
16277 && glyph->type == CHAR_GLYPH
16278 && INTEGERP (glyph->object))
16279 --glyph;
16280
16281 /* If last glyph is a space or stretch, and it's trailing
16282 whitespace, set the face of all trailing whitespace glyphs in
16283 IT->glyph_row to `trailing-whitespace'. */
16284 if (glyph >= start
16285 && BUFFERP (glyph->object)
16286 && (glyph->type == STRETCH_GLYPH
16287 || (glyph->type == CHAR_GLYPH
16288 && glyph->u.ch == ' '))
16289 && trailing_whitespace_p (glyph->charpos))
16290 {
16291 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16292 if (face_id < 0)
16293 return;
16294
16295 while (glyph >= start
16296 && BUFFERP (glyph->object)
16297 && (glyph->type == STRETCH_GLYPH
16298 || (glyph->type == CHAR_GLYPH
16299 && glyph->u.ch == ' ')))
16300 (glyph--)->face_id = face_id;
16301 }
16302 }
16303 }
16304
16305
16306 /* Value is non-zero if glyph row ROW in window W should be
16307 used to hold the cursor. */
16308
16309 static int
16310 cursor_row_p (w, row)
16311 struct window *w;
16312 struct glyph_row *row;
16313 {
16314 int cursor_row_p = 1;
16315
16316 if (PT == MATRIX_ROW_END_CHARPOS (row))
16317 {
16318 /* Suppose the row ends on a string.
16319 Unless the row is continued, that means it ends on a newline
16320 in the string. If it's anything other than a display string
16321 (e.g. a before-string from an overlay), we don't want the
16322 cursor there. (This heuristic seems to give the optimal
16323 behavior for the various types of multi-line strings.) */
16324 if (CHARPOS (row->end.string_pos) >= 0)
16325 {
16326 if (row->continued_p)
16327 cursor_row_p = 1;
16328 else
16329 {
16330 /* Check for `display' property. */
16331 struct glyph *beg = row->glyphs[TEXT_AREA];
16332 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16333 struct glyph *glyph;
16334
16335 cursor_row_p = 0;
16336 for (glyph = end; glyph >= beg; --glyph)
16337 if (STRINGP (glyph->object))
16338 {
16339 Lisp_Object prop
16340 = Fget_char_property (make_number (PT),
16341 Qdisplay, Qnil);
16342 cursor_row_p =
16343 (!NILP (prop)
16344 && display_prop_string_p (prop, glyph->object));
16345 break;
16346 }
16347 }
16348 }
16349 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16350 {
16351 /* If the row ends in middle of a real character,
16352 and the line is continued, we want the cursor here.
16353 That's because MATRIX_ROW_END_CHARPOS would equal
16354 PT if PT is before the character. */
16355 if (!row->ends_in_ellipsis_p)
16356 cursor_row_p = row->continued_p;
16357 else
16358 /* If the row ends in an ellipsis, then
16359 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16360 We want that position to be displayed after the ellipsis. */
16361 cursor_row_p = 0;
16362 }
16363 /* If the row ends at ZV, display the cursor at the end of that
16364 row instead of at the start of the row below. */
16365 else if (row->ends_at_zv_p)
16366 cursor_row_p = 1;
16367 else
16368 cursor_row_p = 0;
16369 }
16370
16371 return cursor_row_p;
16372 }
16373
16374 \f
16375
16376 /* Push the display property PROP so that it will be rendered at the
16377 current position in IT. Return 1 if PROP was successfully pushed,
16378 0 otherwise. */
16379
16380 static int
16381 push_display_prop (struct it *it, Lisp_Object prop)
16382 {
16383 push_it (it);
16384
16385 if (STRINGP (prop))
16386 {
16387 if (SCHARS (prop) == 0)
16388 {
16389 pop_it (it);
16390 return 0;
16391 }
16392
16393 it->string = prop;
16394 it->multibyte_p = STRING_MULTIBYTE (it->string);
16395 it->current.overlay_string_index = -1;
16396 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16397 it->end_charpos = it->string_nchars = SCHARS (it->string);
16398 it->method = GET_FROM_STRING;
16399 it->stop_charpos = 0;
16400 }
16401 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16402 {
16403 it->method = GET_FROM_STRETCH;
16404 it->object = prop;
16405 }
16406 #ifdef HAVE_WINDOW_SYSTEM
16407 else if (IMAGEP (prop))
16408 {
16409 it->what = IT_IMAGE;
16410 it->image_id = lookup_image (it->f, prop);
16411 it->method = GET_FROM_IMAGE;
16412 }
16413 #endif /* HAVE_WINDOW_SYSTEM */
16414 else
16415 {
16416 pop_it (it); /* bogus display property, give up */
16417 return 0;
16418 }
16419
16420 return 1;
16421 }
16422
16423 /* Return the character-property PROP at the current position in IT. */
16424
16425 static Lisp_Object
16426 get_it_property (it, prop)
16427 struct it *it;
16428 Lisp_Object prop;
16429 {
16430 Lisp_Object position;
16431
16432 if (STRINGP (it->object))
16433 position = make_number (IT_STRING_CHARPOS (*it));
16434 else if (BUFFERP (it->object))
16435 position = make_number (IT_CHARPOS (*it));
16436 else
16437 return Qnil;
16438
16439 return Fget_char_property (position, prop, it->object);
16440 }
16441
16442 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16443
16444 static void
16445 handle_line_prefix (struct it *it)
16446 {
16447 Lisp_Object prefix;
16448 if (it->continuation_lines_width > 0)
16449 {
16450 prefix = get_it_property (it, Qwrap_prefix);
16451 if (NILP (prefix))
16452 prefix = Vwrap_prefix;
16453 }
16454 else
16455 {
16456 prefix = get_it_property (it, Qline_prefix);
16457 if (NILP (prefix))
16458 prefix = Vline_prefix;
16459 }
16460 if (! NILP (prefix) && push_display_prop (it, prefix))
16461 {
16462 /* If the prefix is wider than the window, and we try to wrap
16463 it, it would acquire its own wrap prefix, and so on till the
16464 iterator stack overflows. So, don't wrap the prefix. */
16465 it->line_wrap = TRUNCATE;
16466 it->avoid_cursor_p = 1;
16467 }
16468 }
16469
16470 \f
16471
16472 /* Construct the glyph row IT->glyph_row in the desired matrix of
16473 IT->w from text at the current position of IT. See dispextern.h
16474 for an overview of struct it. Value is non-zero if
16475 IT->glyph_row displays text, as opposed to a line displaying ZV
16476 only. */
16477
16478 static int
16479 display_line (it)
16480 struct it *it;
16481 {
16482 struct glyph_row *row = it->glyph_row;
16483 Lisp_Object overlay_arrow_string;
16484 struct it wrap_it;
16485 int may_wrap = 0, wrap_x;
16486 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16487 int wrap_row_phys_ascent, wrap_row_phys_height;
16488 int wrap_row_extra_line_spacing;
16489
16490 /* We always start displaying at hpos zero even if hscrolled. */
16491 xassert (it->hpos == 0 && it->current_x == 0);
16492
16493 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16494 >= it->w->desired_matrix->nrows)
16495 {
16496 it->w->nrows_scale_factor++;
16497 fonts_changed_p = 1;
16498 return 0;
16499 }
16500
16501 /* Is IT->w showing the region? */
16502 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16503
16504 /* Clear the result glyph row and enable it. */
16505 prepare_desired_row (row);
16506
16507 row->y = it->current_y;
16508 row->start = it->start;
16509 row->continuation_lines_width = it->continuation_lines_width;
16510 row->displays_text_p = 1;
16511 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16512 it->starts_in_middle_of_char_p = 0;
16513
16514 /* Arrange the overlays nicely for our purposes. Usually, we call
16515 display_line on only one line at a time, in which case this
16516 can't really hurt too much, or we call it on lines which appear
16517 one after another in the buffer, in which case all calls to
16518 recenter_overlay_lists but the first will be pretty cheap. */
16519 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16520
16521 /* Move over display elements that are not visible because we are
16522 hscrolled. This may stop at an x-position < IT->first_visible_x
16523 if the first glyph is partially visible or if we hit a line end. */
16524 if (it->current_x < it->first_visible_x)
16525 {
16526 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16527 MOVE_TO_POS | MOVE_TO_X);
16528 }
16529 else
16530 {
16531 /* We only do this when not calling `move_it_in_display_line_to'
16532 above, because move_it_in_display_line_to calls
16533 handle_line_prefix itself. */
16534 handle_line_prefix (it);
16535 }
16536
16537 /* Get the initial row height. This is either the height of the
16538 text hscrolled, if there is any, or zero. */
16539 row->ascent = it->max_ascent;
16540 row->height = it->max_ascent + it->max_descent;
16541 row->phys_ascent = it->max_phys_ascent;
16542 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16543 row->extra_line_spacing = it->max_extra_line_spacing;
16544
16545 /* Loop generating characters. The loop is left with IT on the next
16546 character to display. */
16547 while (1)
16548 {
16549 int n_glyphs_before, hpos_before, x_before;
16550 int x, i, nglyphs;
16551 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16552
16553 /* Retrieve the next thing to display. Value is zero if end of
16554 buffer reached. */
16555 if (!get_next_display_element (it))
16556 {
16557 /* Maybe add a space at the end of this line that is used to
16558 display the cursor there under X. Set the charpos of the
16559 first glyph of blank lines not corresponding to any text
16560 to -1. */
16561 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16562 row->exact_window_width_line_p = 1;
16563 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16564 || row->used[TEXT_AREA] == 0)
16565 {
16566 row->glyphs[TEXT_AREA]->charpos = -1;
16567 row->displays_text_p = 0;
16568
16569 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16570 && (!MINI_WINDOW_P (it->w)
16571 || (minibuf_level && EQ (it->window, minibuf_window))))
16572 row->indicate_empty_line_p = 1;
16573 }
16574
16575 it->continuation_lines_width = 0;
16576 row->ends_at_zv_p = 1;
16577 break;
16578 }
16579
16580 /* Now, get the metrics of what we want to display. This also
16581 generates glyphs in `row' (which is IT->glyph_row). */
16582 n_glyphs_before = row->used[TEXT_AREA];
16583 x = it->current_x;
16584
16585 /* Remember the line height so far in case the next element doesn't
16586 fit on the line. */
16587 if (it->line_wrap != TRUNCATE)
16588 {
16589 ascent = it->max_ascent;
16590 descent = it->max_descent;
16591 phys_ascent = it->max_phys_ascent;
16592 phys_descent = it->max_phys_descent;
16593
16594 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16595 {
16596 if (IT_DISPLAYING_WHITESPACE (it))
16597 may_wrap = 1;
16598 else if (may_wrap)
16599 {
16600 wrap_it = *it;
16601 wrap_x = x;
16602 wrap_row_used = row->used[TEXT_AREA];
16603 wrap_row_ascent = row->ascent;
16604 wrap_row_height = row->height;
16605 wrap_row_phys_ascent = row->phys_ascent;
16606 wrap_row_phys_height = row->phys_height;
16607 wrap_row_extra_line_spacing = row->extra_line_spacing;
16608 may_wrap = 0;
16609 }
16610 }
16611 }
16612
16613 PRODUCE_GLYPHS (it);
16614
16615 /* If this display element was in marginal areas, continue with
16616 the next one. */
16617 if (it->area != TEXT_AREA)
16618 {
16619 row->ascent = max (row->ascent, it->max_ascent);
16620 row->height = max (row->height, it->max_ascent + it->max_descent);
16621 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16622 row->phys_height = max (row->phys_height,
16623 it->max_phys_ascent + it->max_phys_descent);
16624 row->extra_line_spacing = max (row->extra_line_spacing,
16625 it->max_extra_line_spacing);
16626 set_iterator_to_next (it, 1);
16627 continue;
16628 }
16629
16630 /* Does the display element fit on the line? If we truncate
16631 lines, we should draw past the right edge of the window. If
16632 we don't truncate, we want to stop so that we can display the
16633 continuation glyph before the right margin. If lines are
16634 continued, there are two possible strategies for characters
16635 resulting in more than 1 glyph (e.g. tabs): Display as many
16636 glyphs as possible in this line and leave the rest for the
16637 continuation line, or display the whole element in the next
16638 line. Original redisplay did the former, so we do it also. */
16639 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16640 hpos_before = it->hpos;
16641 x_before = x;
16642
16643 if (/* Not a newline. */
16644 nglyphs > 0
16645 /* Glyphs produced fit entirely in the line. */
16646 && it->current_x < it->last_visible_x)
16647 {
16648 it->hpos += nglyphs;
16649 row->ascent = max (row->ascent, it->max_ascent);
16650 row->height = max (row->height, it->max_ascent + it->max_descent);
16651 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16652 row->phys_height = max (row->phys_height,
16653 it->max_phys_ascent + it->max_phys_descent);
16654 row->extra_line_spacing = max (row->extra_line_spacing,
16655 it->max_extra_line_spacing);
16656 if (it->current_x - it->pixel_width < it->first_visible_x)
16657 row->x = x - it->first_visible_x;
16658 }
16659 else
16660 {
16661 int new_x;
16662 struct glyph *glyph;
16663
16664 for (i = 0; i < nglyphs; ++i, x = new_x)
16665 {
16666 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16667 new_x = x + glyph->pixel_width;
16668
16669 if (/* Lines are continued. */
16670 it->line_wrap != TRUNCATE
16671 && (/* Glyph doesn't fit on the line. */
16672 new_x > it->last_visible_x
16673 /* Or it fits exactly on a window system frame. */
16674 || (new_x == it->last_visible_x
16675 && FRAME_WINDOW_P (it->f))))
16676 {
16677 /* End of a continued line. */
16678
16679 if (it->hpos == 0
16680 || (new_x == it->last_visible_x
16681 && FRAME_WINDOW_P (it->f)))
16682 {
16683 /* Current glyph is the only one on the line or
16684 fits exactly on the line. We must continue
16685 the line because we can't draw the cursor
16686 after the glyph. */
16687 row->continued_p = 1;
16688 it->current_x = new_x;
16689 it->continuation_lines_width += new_x;
16690 ++it->hpos;
16691 if (i == nglyphs - 1)
16692 {
16693 /* If line-wrap is on, check if a previous
16694 wrap point was found. */
16695 if (wrap_row_used > 0
16696 /* Even if there is a previous wrap
16697 point, continue the line here as
16698 usual, if (i) the previous character
16699 was a space or tab AND (ii) the
16700 current character is not. */
16701 && (!may_wrap
16702 || IT_DISPLAYING_WHITESPACE (it)))
16703 goto back_to_wrap;
16704
16705 set_iterator_to_next (it, 1);
16706 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16707 {
16708 if (!get_next_display_element (it))
16709 {
16710 row->exact_window_width_line_p = 1;
16711 it->continuation_lines_width = 0;
16712 row->continued_p = 0;
16713 row->ends_at_zv_p = 1;
16714 }
16715 else if (ITERATOR_AT_END_OF_LINE_P (it))
16716 {
16717 row->continued_p = 0;
16718 row->exact_window_width_line_p = 1;
16719 }
16720 }
16721 }
16722 }
16723 else if (CHAR_GLYPH_PADDING_P (*glyph)
16724 && !FRAME_WINDOW_P (it->f))
16725 {
16726 /* A padding glyph that doesn't fit on this line.
16727 This means the whole character doesn't fit
16728 on the line. */
16729 row->used[TEXT_AREA] = n_glyphs_before;
16730
16731 /* Fill the rest of the row with continuation
16732 glyphs like in 20.x. */
16733 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16734 < row->glyphs[1 + TEXT_AREA])
16735 produce_special_glyphs (it, IT_CONTINUATION);
16736
16737 row->continued_p = 1;
16738 it->current_x = x_before;
16739 it->continuation_lines_width += x_before;
16740
16741 /* Restore the height to what it was before the
16742 element not fitting on the line. */
16743 it->max_ascent = ascent;
16744 it->max_descent = descent;
16745 it->max_phys_ascent = phys_ascent;
16746 it->max_phys_descent = phys_descent;
16747 }
16748 else if (wrap_row_used > 0)
16749 {
16750 back_to_wrap:
16751 *it = wrap_it;
16752 it->continuation_lines_width += wrap_x;
16753 row->used[TEXT_AREA] = wrap_row_used;
16754 row->ascent = wrap_row_ascent;
16755 row->height = wrap_row_height;
16756 row->phys_ascent = wrap_row_phys_ascent;
16757 row->phys_height = wrap_row_phys_height;
16758 row->extra_line_spacing = wrap_row_extra_line_spacing;
16759 row->continued_p = 1;
16760 row->ends_at_zv_p = 0;
16761 row->exact_window_width_line_p = 0;
16762 it->continuation_lines_width += x;
16763
16764 /* Make sure that a non-default face is extended
16765 up to the right margin of the window. */
16766 extend_face_to_end_of_line (it);
16767 }
16768 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16769 {
16770 /* A TAB that extends past the right edge of the
16771 window. This produces a single glyph on
16772 window system frames. We leave the glyph in
16773 this row and let it fill the row, but don't
16774 consume the TAB. */
16775 it->continuation_lines_width += it->last_visible_x;
16776 row->ends_in_middle_of_char_p = 1;
16777 row->continued_p = 1;
16778 glyph->pixel_width = it->last_visible_x - x;
16779 it->starts_in_middle_of_char_p = 1;
16780 }
16781 else
16782 {
16783 /* Something other than a TAB that draws past
16784 the right edge of the window. Restore
16785 positions to values before the element. */
16786 row->used[TEXT_AREA] = n_glyphs_before + i;
16787
16788 /* Display continuation glyphs. */
16789 if (!FRAME_WINDOW_P (it->f))
16790 produce_special_glyphs (it, IT_CONTINUATION);
16791 row->continued_p = 1;
16792
16793 it->current_x = x_before;
16794 it->continuation_lines_width += x;
16795 extend_face_to_end_of_line (it);
16796
16797 if (nglyphs > 1 && i > 0)
16798 {
16799 row->ends_in_middle_of_char_p = 1;
16800 it->starts_in_middle_of_char_p = 1;
16801 }
16802
16803 /* Restore the height to what it was before the
16804 element not fitting on the line. */
16805 it->max_ascent = ascent;
16806 it->max_descent = descent;
16807 it->max_phys_ascent = phys_ascent;
16808 it->max_phys_descent = phys_descent;
16809 }
16810
16811 break;
16812 }
16813 else if (new_x > it->first_visible_x)
16814 {
16815 /* Increment number of glyphs actually displayed. */
16816 ++it->hpos;
16817
16818 if (x < it->first_visible_x)
16819 /* Glyph is partially visible, i.e. row starts at
16820 negative X position. */
16821 row->x = x - it->first_visible_x;
16822 }
16823 else
16824 {
16825 /* Glyph is completely off the left margin of the
16826 window. This should not happen because of the
16827 move_it_in_display_line at the start of this
16828 function, unless the text display area of the
16829 window is empty. */
16830 xassert (it->first_visible_x <= it->last_visible_x);
16831 }
16832 }
16833
16834 row->ascent = max (row->ascent, it->max_ascent);
16835 row->height = max (row->height, it->max_ascent + it->max_descent);
16836 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16837 row->phys_height = max (row->phys_height,
16838 it->max_phys_ascent + it->max_phys_descent);
16839 row->extra_line_spacing = max (row->extra_line_spacing,
16840 it->max_extra_line_spacing);
16841
16842 /* End of this display line if row is continued. */
16843 if (row->continued_p || row->ends_at_zv_p)
16844 break;
16845 }
16846
16847 at_end_of_line:
16848 /* Is this a line end? If yes, we're also done, after making
16849 sure that a non-default face is extended up to the right
16850 margin of the window. */
16851 if (ITERATOR_AT_END_OF_LINE_P (it))
16852 {
16853 int used_before = row->used[TEXT_AREA];
16854
16855 row->ends_in_newline_from_string_p = STRINGP (it->object);
16856
16857 /* Add a space at the end of the line that is used to
16858 display the cursor there. */
16859 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16860 append_space_for_newline (it, 0);
16861
16862 /* Extend the face to the end of the line. */
16863 extend_face_to_end_of_line (it);
16864
16865 /* Make sure we have the position. */
16866 if (used_before == 0)
16867 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16868
16869 /* Consume the line end. This skips over invisible lines. */
16870 set_iterator_to_next (it, 1);
16871 it->continuation_lines_width = 0;
16872 break;
16873 }
16874
16875 /* Proceed with next display element. Note that this skips
16876 over lines invisible because of selective display. */
16877 set_iterator_to_next (it, 1);
16878
16879 /* If we truncate lines, we are done when the last displayed
16880 glyphs reach past the right margin of the window. */
16881 if (it->line_wrap == TRUNCATE
16882 && (FRAME_WINDOW_P (it->f)
16883 ? (it->current_x >= it->last_visible_x)
16884 : (it->current_x > it->last_visible_x)))
16885 {
16886 /* Maybe add truncation glyphs. */
16887 if (!FRAME_WINDOW_P (it->f))
16888 {
16889 int i, n;
16890
16891 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16892 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16893 break;
16894
16895 for (n = row->used[TEXT_AREA]; i < n; ++i)
16896 {
16897 row->used[TEXT_AREA] = i;
16898 produce_special_glyphs (it, IT_TRUNCATION);
16899 }
16900 }
16901 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16902 {
16903 /* Don't truncate if we can overflow newline into fringe. */
16904 if (!get_next_display_element (it))
16905 {
16906 it->continuation_lines_width = 0;
16907 row->ends_at_zv_p = 1;
16908 row->exact_window_width_line_p = 1;
16909 break;
16910 }
16911 if (ITERATOR_AT_END_OF_LINE_P (it))
16912 {
16913 row->exact_window_width_line_p = 1;
16914 goto at_end_of_line;
16915 }
16916 }
16917
16918 row->truncated_on_right_p = 1;
16919 it->continuation_lines_width = 0;
16920 reseat_at_next_visible_line_start (it, 0);
16921 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16922 it->hpos = hpos_before;
16923 it->current_x = x_before;
16924 break;
16925 }
16926 }
16927
16928 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16929 at the left window margin. */
16930 if (it->first_visible_x
16931 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16932 {
16933 if (!FRAME_WINDOW_P (it->f))
16934 insert_left_trunc_glyphs (it);
16935 row->truncated_on_left_p = 1;
16936 }
16937
16938 /* If the start of this line is the overlay arrow-position, then
16939 mark this glyph row as the one containing the overlay arrow.
16940 This is clearly a mess with variable size fonts. It would be
16941 better to let it be displayed like cursors under X. */
16942 if ((row->displays_text_p || !overlay_arrow_seen)
16943 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16944 !NILP (overlay_arrow_string)))
16945 {
16946 /* Overlay arrow in window redisplay is a fringe bitmap. */
16947 if (STRINGP (overlay_arrow_string))
16948 {
16949 struct glyph_row *arrow_row
16950 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16951 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16952 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16953 struct glyph *p = row->glyphs[TEXT_AREA];
16954 struct glyph *p2, *end;
16955
16956 /* Copy the arrow glyphs. */
16957 while (glyph < arrow_end)
16958 *p++ = *glyph++;
16959
16960 /* Throw away padding glyphs. */
16961 p2 = p;
16962 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16963 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16964 ++p2;
16965 if (p2 > p)
16966 {
16967 while (p2 < end)
16968 *p++ = *p2++;
16969 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16970 }
16971 }
16972 else
16973 {
16974 xassert (INTEGERP (overlay_arrow_string));
16975 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16976 }
16977 overlay_arrow_seen = 1;
16978 }
16979
16980 /* Compute pixel dimensions of this line. */
16981 compute_line_metrics (it);
16982
16983 /* Remember the position at which this line ends. */
16984 row->end = it->current;
16985
16986 /* Record whether this row ends inside an ellipsis. */
16987 row->ends_in_ellipsis_p
16988 = (it->method == GET_FROM_DISPLAY_VECTOR
16989 && it->ellipsis_p);
16990
16991 /* Save fringe bitmaps in this row. */
16992 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16993 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16994 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16995 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16996
16997 it->left_user_fringe_bitmap = 0;
16998 it->left_user_fringe_face_id = 0;
16999 it->right_user_fringe_bitmap = 0;
17000 it->right_user_fringe_face_id = 0;
17001
17002 /* Maybe set the cursor. */
17003 if (it->w->cursor.vpos < 0
17004 && PT >= MATRIX_ROW_START_CHARPOS (row)
17005 && PT <= MATRIX_ROW_END_CHARPOS (row)
17006 && cursor_row_p (it->w, row))
17007 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17008
17009 /* Highlight trailing whitespace. */
17010 if (!NILP (Vshow_trailing_whitespace))
17011 highlight_trailing_whitespace (it->f, it->glyph_row);
17012
17013 /* Prepare for the next line. This line starts horizontally at (X
17014 HPOS) = (0 0). Vertical positions are incremented. As a
17015 convenience for the caller, IT->glyph_row is set to the next
17016 row to be used. */
17017 it->current_x = it->hpos = 0;
17018 it->current_y += row->height;
17019 ++it->vpos;
17020 ++it->glyph_row;
17021 it->start = it->current;
17022 return row->displays_text_p;
17023 }
17024
17025
17026 \f
17027 /***********************************************************************
17028 Menu Bar
17029 ***********************************************************************/
17030
17031 /* Redisplay the menu bar in the frame for window W.
17032
17033 The menu bar of X frames that don't have X toolkit support is
17034 displayed in a special window W->frame->menu_bar_window.
17035
17036 The menu bar of terminal frames is treated specially as far as
17037 glyph matrices are concerned. Menu bar lines are not part of
17038 windows, so the update is done directly on the frame matrix rows
17039 for the menu bar. */
17040
17041 static void
17042 display_menu_bar (w)
17043 struct window *w;
17044 {
17045 struct frame *f = XFRAME (WINDOW_FRAME (w));
17046 struct it it;
17047 Lisp_Object items;
17048 int i;
17049
17050 /* Don't do all this for graphical frames. */
17051 #ifdef HAVE_NTGUI
17052 if (FRAME_W32_P (f))
17053 return;
17054 #endif
17055 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17056 if (FRAME_X_P (f))
17057 return;
17058 #endif
17059
17060 #ifdef HAVE_NS
17061 if (FRAME_NS_P (f))
17062 return;
17063 #endif /* HAVE_NS */
17064
17065 #ifdef USE_X_TOOLKIT
17066 xassert (!FRAME_WINDOW_P (f));
17067 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17068 it.first_visible_x = 0;
17069 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17070 #else /* not USE_X_TOOLKIT */
17071 if (FRAME_WINDOW_P (f))
17072 {
17073 /* Menu bar lines are displayed in the desired matrix of the
17074 dummy window menu_bar_window. */
17075 struct window *menu_w;
17076 xassert (WINDOWP (f->menu_bar_window));
17077 menu_w = XWINDOW (f->menu_bar_window);
17078 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17079 MENU_FACE_ID);
17080 it.first_visible_x = 0;
17081 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17082 }
17083 else
17084 {
17085 /* This is a TTY frame, i.e. character hpos/vpos are used as
17086 pixel x/y. */
17087 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17088 MENU_FACE_ID);
17089 it.first_visible_x = 0;
17090 it.last_visible_x = FRAME_COLS (f);
17091 }
17092 #endif /* not USE_X_TOOLKIT */
17093
17094 if (! mode_line_inverse_video)
17095 /* Force the menu-bar to be displayed in the default face. */
17096 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17097
17098 /* Clear all rows of the menu bar. */
17099 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17100 {
17101 struct glyph_row *row = it.glyph_row + i;
17102 clear_glyph_row (row);
17103 row->enabled_p = 1;
17104 row->full_width_p = 1;
17105 }
17106
17107 /* Display all items of the menu bar. */
17108 items = FRAME_MENU_BAR_ITEMS (it.f);
17109 for (i = 0; i < XVECTOR_SIZE (items); i += 4)
17110 {
17111 Lisp_Object string;
17112
17113 /* Stop at nil string. */
17114 string = AREF (items, i + 1);
17115 if (NILP (string))
17116 break;
17117
17118 /* Remember where item was displayed. */
17119 ASET (items, i + 3, make_number (it.hpos));
17120
17121 /* Display the item, pad with one space. */
17122 if (it.current_x < it.last_visible_x)
17123 display_string (NULL, string, Qnil, 0, 0, &it,
17124 SCHARS (string) + 1, 0, 0, -1);
17125 }
17126
17127 /* Fill out the line with spaces. */
17128 if (it.current_x < it.last_visible_x)
17129 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17130
17131 /* Compute the total height of the lines. */
17132 compute_line_metrics (&it);
17133 }
17134
17135
17136 \f
17137 /***********************************************************************
17138 Mode Line
17139 ***********************************************************************/
17140
17141 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17142 FORCE is non-zero, redisplay mode lines unconditionally.
17143 Otherwise, redisplay only mode lines that are garbaged. Value is
17144 the number of windows whose mode lines were redisplayed. */
17145
17146 static int
17147 redisplay_mode_lines (window, force)
17148 Lisp_Object window;
17149 int force;
17150 {
17151 int nwindows = 0;
17152
17153 while (!NILP (window))
17154 {
17155 struct window *w = XWINDOW (window);
17156
17157 if (WINDOWP (w->hchild))
17158 nwindows += redisplay_mode_lines (w->hchild, force);
17159 else if (WINDOWP (w->vchild))
17160 nwindows += redisplay_mode_lines (w->vchild, force);
17161 else if (force
17162 || FRAME_GARBAGED_P (XFRAME (w->frame))
17163 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17164 {
17165 struct text_pos lpoint;
17166 struct buffer *old = current_buffer;
17167
17168 /* Set the window's buffer for the mode line display. */
17169 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17170 set_buffer_internal_1 (XBUFFER (w->buffer));
17171
17172 /* Point refers normally to the selected window. For any
17173 other window, set up appropriate value. */
17174 if (!EQ (window, selected_window))
17175 {
17176 struct text_pos pt;
17177
17178 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17179 if (CHARPOS (pt) < BEGV)
17180 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17181 else if (CHARPOS (pt) > (ZV - 1))
17182 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17183 else
17184 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17185 }
17186
17187 /* Display mode lines. */
17188 clear_glyph_matrix (w->desired_matrix);
17189 if (display_mode_lines (w))
17190 {
17191 ++nwindows;
17192 w->must_be_updated_p = 1;
17193 }
17194
17195 /* Restore old settings. */
17196 set_buffer_internal_1 (old);
17197 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17198 }
17199
17200 window = w->next;
17201 }
17202
17203 return nwindows;
17204 }
17205
17206
17207 /* Display the mode and/or header line of window W. Value is the
17208 sum number of mode lines and header lines displayed. */
17209
17210 static int
17211 display_mode_lines (w)
17212 struct window *w;
17213 {
17214 Lisp_Object old_selected_window, old_selected_frame;
17215 int n = 0;
17216
17217 old_selected_frame = selected_frame;
17218 selected_frame = w->frame;
17219 old_selected_window = selected_window;
17220 XSETWINDOW (selected_window, w);
17221
17222 /* These will be set while the mode line specs are processed. */
17223 line_number_displayed = 0;
17224 w->column_number_displayed = Qnil;
17225
17226 if (WINDOW_WANTS_MODELINE_P (w))
17227 {
17228 struct window *sel_w = XWINDOW (old_selected_window);
17229
17230 /* Select mode line face based on the real selected window. */
17231 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17232 current_buffer->mode_line_format);
17233 ++n;
17234 }
17235
17236 if (WINDOW_WANTS_HEADER_LINE_P (w))
17237 {
17238 display_mode_line (w, HEADER_LINE_FACE_ID,
17239 current_buffer->header_line_format);
17240 ++n;
17241 }
17242
17243 selected_frame = old_selected_frame;
17244 selected_window = old_selected_window;
17245 return n;
17246 }
17247
17248
17249 /* Display mode or header line of window W. FACE_ID specifies which
17250 line to display; it is either MODE_LINE_FACE_ID or
17251 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17252 display. Value is the pixel height of the mode/header line
17253 displayed. */
17254
17255 static int
17256 display_mode_line (w, face_id, format)
17257 struct window *w;
17258 enum face_id face_id;
17259 Lisp_Object format;
17260 {
17261 struct it it;
17262 struct face *face;
17263 int count = SPECPDL_INDEX ();
17264
17265 init_iterator (&it, w, -1, -1, NULL, face_id);
17266 /* Don't extend on a previously drawn mode-line.
17267 This may happen if called from pos_visible_p. */
17268 it.glyph_row->enabled_p = 0;
17269 prepare_desired_row (it.glyph_row);
17270
17271 it.glyph_row->mode_line_p = 1;
17272
17273 if (! mode_line_inverse_video)
17274 /* Force the mode-line to be displayed in the default face. */
17275 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17276
17277 record_unwind_protect (unwind_format_mode_line,
17278 format_mode_line_unwind_data (NULL, Qnil, 0));
17279
17280 mode_line_target = MODE_LINE_DISPLAY;
17281
17282 /* Temporarily make frame's keyboard the current kboard so that
17283 kboard-local variables in the mode_line_format will get the right
17284 values. */
17285 push_kboard (FRAME_KBOARD (it.f));
17286 record_unwind_save_match_data ();
17287 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17288 pop_kboard ();
17289
17290 unbind_to (count, Qnil);
17291
17292 /* Fill up with spaces. */
17293 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17294
17295 compute_line_metrics (&it);
17296 it.glyph_row->full_width_p = 1;
17297 it.glyph_row->continued_p = 0;
17298 it.glyph_row->truncated_on_left_p = 0;
17299 it.glyph_row->truncated_on_right_p = 0;
17300
17301 /* Make a 3D mode-line have a shadow at its right end. */
17302 face = FACE_FROM_ID (it.f, face_id);
17303 extend_face_to_end_of_line (&it);
17304 if (face->box != FACE_NO_BOX)
17305 {
17306 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17307 + it.glyph_row->used[TEXT_AREA] - 1);
17308 last->right_box_line_p = 1;
17309 }
17310
17311 return it.glyph_row->height;
17312 }
17313
17314 /* Move element ELT in LIST to the front of LIST.
17315 Return the updated list. */
17316
17317 static Lisp_Object
17318 move_elt_to_front (elt, list)
17319 Lisp_Object elt, list;
17320 {
17321 register Lisp_Object tail, prev;
17322 register Lisp_Object tem;
17323
17324 tail = list;
17325 prev = Qnil;
17326 while (CONSP (tail))
17327 {
17328 tem = XCAR (tail);
17329
17330 if (EQ (elt, tem))
17331 {
17332 /* Splice out the link TAIL. */
17333 if (NILP (prev))
17334 list = XCDR (tail);
17335 else
17336 Fsetcdr (prev, XCDR (tail));
17337
17338 /* Now make it the first. */
17339 Fsetcdr (tail, list);
17340 return tail;
17341 }
17342 else
17343 prev = tail;
17344 tail = XCDR (tail);
17345 QUIT;
17346 }
17347
17348 /* Not found--return unchanged LIST. */
17349 return list;
17350 }
17351
17352 /* Contribute ELT to the mode line for window IT->w. How it
17353 translates into text depends on its data type.
17354
17355 IT describes the display environment in which we display, as usual.
17356
17357 DEPTH is the depth in recursion. It is used to prevent
17358 infinite recursion here.
17359
17360 FIELD_WIDTH is the number of characters the display of ELT should
17361 occupy in the mode line, and PRECISION is the maximum number of
17362 characters to display from ELT's representation. See
17363 display_string for details.
17364
17365 Returns the hpos of the end of the text generated by ELT.
17366
17367 PROPS is a property list to add to any string we encounter.
17368
17369 If RISKY is nonzero, remove (disregard) any properties in any string
17370 we encounter, and ignore :eval and :propertize.
17371
17372 The global variable `mode_line_target' determines whether the
17373 output is passed to `store_mode_line_noprop',
17374 `store_mode_line_string', or `display_string'. */
17375
17376 static int
17377 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17378 struct it *it;
17379 int depth;
17380 int field_width, precision;
17381 Lisp_Object elt, props;
17382 int risky;
17383 {
17384 int n = 0, field, prec;
17385 int literal = 0;
17386
17387 tail_recurse:
17388 if (depth > 100)
17389 elt = build_string ("*too-deep*");
17390
17391 depth++;
17392
17393 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17394 {
17395 case Lisp_String:
17396 {
17397 /* A string: output it and check for %-constructs within it. */
17398 unsigned char c;
17399 int offset = 0;
17400
17401 if (SCHARS (elt) > 0
17402 && (!NILP (props) || risky))
17403 {
17404 Lisp_Object oprops, aelt;
17405 oprops = Ftext_properties_at (make_number (0), elt);
17406
17407 /* If the starting string's properties are not what
17408 we want, translate the string. Also, if the string
17409 is risky, do that anyway. */
17410
17411 if (NILP (Fequal (props, oprops)) || risky)
17412 {
17413 /* If the starting string has properties,
17414 merge the specified ones onto the existing ones. */
17415 if (! NILP (oprops) && !risky)
17416 {
17417 Lisp_Object tem;
17418
17419 oprops = Fcopy_sequence (oprops);
17420 tem = props;
17421 while (CONSP (tem))
17422 {
17423 oprops = Fplist_put (oprops, XCAR (tem),
17424 XCAR (XCDR (tem)));
17425 tem = XCDR (XCDR (tem));
17426 }
17427 props = oprops;
17428 }
17429
17430 aelt = Fassoc (elt, mode_line_proptrans_alist);
17431 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17432 {
17433 /* AELT is what we want. Move it to the front
17434 without consing. */
17435 elt = XCAR (aelt);
17436 mode_line_proptrans_alist
17437 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17438 }
17439 else
17440 {
17441 Lisp_Object tem;
17442
17443 /* If AELT has the wrong props, it is useless.
17444 so get rid of it. */
17445 if (! NILP (aelt))
17446 mode_line_proptrans_alist
17447 = Fdelq (aelt, mode_line_proptrans_alist);
17448
17449 elt = Fcopy_sequence (elt);
17450 Fset_text_properties (make_number (0), Flength (elt),
17451 props, elt);
17452 /* Add this item to mode_line_proptrans_alist. */
17453 mode_line_proptrans_alist
17454 = Fcons (Fcons (elt, props),
17455 mode_line_proptrans_alist);
17456 /* Truncate mode_line_proptrans_alist
17457 to at most 50 elements. */
17458 tem = Fnthcdr (make_number (50),
17459 mode_line_proptrans_alist);
17460 if (! NILP (tem))
17461 XSETCDR (tem, Qnil);
17462 }
17463 }
17464 }
17465
17466 offset = 0;
17467
17468 if (literal)
17469 {
17470 prec = precision - n;
17471 switch (mode_line_target)
17472 {
17473 case MODE_LINE_NOPROP:
17474 case MODE_LINE_TITLE:
17475 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17476 break;
17477 case MODE_LINE_STRING:
17478 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17479 break;
17480 case MODE_LINE_DISPLAY:
17481 n += display_string (NULL, elt, Qnil, 0, 0, it,
17482 0, prec, 0, STRING_MULTIBYTE (elt));
17483 break;
17484 }
17485
17486 break;
17487 }
17488
17489 /* Handle the non-literal case. */
17490
17491 while ((precision <= 0 || n < precision)
17492 && SREF (elt, offset) != 0
17493 && (mode_line_target != MODE_LINE_DISPLAY
17494 || it->current_x < it->last_visible_x))
17495 {
17496 int last_offset = offset;
17497
17498 /* Advance to end of string or next format specifier. */
17499 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17500 ;
17501
17502 if (offset - 1 != last_offset)
17503 {
17504 int nchars, nbytes;
17505
17506 /* Output to end of string or up to '%'. Field width
17507 is length of string. Don't output more than
17508 PRECISION allows us. */
17509 offset--;
17510
17511 prec = c_string_width (SDATA (elt) + last_offset,
17512 offset - last_offset, precision - n,
17513 &nchars, &nbytes);
17514
17515 switch (mode_line_target)
17516 {
17517 case MODE_LINE_NOPROP:
17518 case MODE_LINE_TITLE:
17519 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17520 break;
17521 case MODE_LINE_STRING:
17522 {
17523 int bytepos = last_offset;
17524 int charpos = string_byte_to_char (elt, bytepos);
17525 int endpos = (precision <= 0
17526 ? string_byte_to_char (elt, offset)
17527 : charpos + nchars);
17528
17529 n += store_mode_line_string (NULL,
17530 Fsubstring (elt, make_number (charpos),
17531 make_number (endpos)),
17532 0, 0, 0, Qnil);
17533 }
17534 break;
17535 case MODE_LINE_DISPLAY:
17536 {
17537 int bytepos = last_offset;
17538 int charpos = string_byte_to_char (elt, bytepos);
17539
17540 if (precision <= 0)
17541 nchars = string_byte_to_char (elt, offset) - charpos;
17542 n += display_string (NULL, elt, Qnil, 0, charpos,
17543 it, 0, nchars, 0,
17544 STRING_MULTIBYTE (elt));
17545 }
17546 break;
17547 }
17548 }
17549 else /* c == '%' */
17550 {
17551 int percent_position = offset;
17552
17553 /* Get the specified minimum width. Zero means
17554 don't pad. */
17555 field = 0;
17556 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17557 field = field * 10 + c - '0';
17558
17559 /* Don't pad beyond the total padding allowed. */
17560 if (field_width - n > 0 && field > field_width - n)
17561 field = field_width - n;
17562
17563 /* Note that either PRECISION <= 0 or N < PRECISION. */
17564 prec = precision - n;
17565
17566 if (c == 'M')
17567 n += display_mode_element (it, depth, field, prec,
17568 Vglobal_mode_string, props,
17569 risky);
17570 else if (c != 0)
17571 {
17572 int multibyte;
17573 int bytepos, charpos;
17574 unsigned char *spec;
17575 Lisp_Object string;
17576
17577 bytepos = percent_position;
17578 charpos = (STRING_MULTIBYTE (elt)
17579 ? string_byte_to_char (elt, bytepos)
17580 : bytepos);
17581 spec = decode_mode_spec (it->w, c, field, prec, &string);
17582 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
17583
17584 switch (mode_line_target)
17585 {
17586 case MODE_LINE_NOPROP:
17587 case MODE_LINE_TITLE:
17588 n += store_mode_line_noprop (spec, field, prec);
17589 break;
17590 case MODE_LINE_STRING:
17591 {
17592 int len = strlen (spec);
17593 Lisp_Object tem = make_string (spec, len);
17594 props = Ftext_properties_at (make_number (charpos), elt);
17595 /* Should only keep face property in props */
17596 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17597 }
17598 break;
17599 case MODE_LINE_DISPLAY:
17600 {
17601 int nglyphs_before, nwritten;
17602
17603 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17604 nwritten = display_string (spec, string, elt,
17605 charpos, 0, it,
17606 field, prec, 0,
17607 multibyte);
17608
17609 /* Assign to the glyphs written above the
17610 string where the `%x' came from, position
17611 of the `%'. */
17612 if (nwritten > 0)
17613 {
17614 struct glyph *glyph
17615 = (it->glyph_row->glyphs[TEXT_AREA]
17616 + nglyphs_before);
17617 int i;
17618
17619 for (i = 0; i < nwritten; ++i)
17620 {
17621 glyph[i].object = elt;
17622 glyph[i].charpos = charpos;
17623 }
17624
17625 n += nwritten;
17626 }
17627 }
17628 break;
17629 }
17630 }
17631 else /* c == 0 */
17632 break;
17633 }
17634 }
17635 }
17636 break;
17637
17638 case Lisp_Symbol:
17639 /* A symbol: process the value of the symbol recursively
17640 as if it appeared here directly. Avoid error if symbol void.
17641 Special case: if value of symbol is a string, output the string
17642 literally. */
17643 {
17644 register Lisp_Object tem;
17645
17646 /* If the variable is not marked as risky to set
17647 then its contents are risky to use. */
17648 if (NILP (Fget (elt, Qrisky_local_variable)))
17649 risky = 1;
17650
17651 tem = Fboundp (elt);
17652 if (!NILP (tem))
17653 {
17654 tem = Fsymbol_value (elt);
17655 /* If value is a string, output that string literally:
17656 don't check for % within it. */
17657 if (STRINGP (tem))
17658 literal = 1;
17659
17660 if (!EQ (tem, elt))
17661 {
17662 /* Give up right away for nil or t. */
17663 elt = tem;
17664 goto tail_recurse;
17665 }
17666 }
17667 }
17668 break;
17669
17670 case Lisp_Cons:
17671 {
17672 register Lisp_Object car, tem;
17673
17674 /* A cons cell: five distinct cases.
17675 If first element is :eval or :propertize, do something special.
17676 If first element is a string or a cons, process all the elements
17677 and effectively concatenate them.
17678 If first element is a negative number, truncate displaying cdr to
17679 at most that many characters. If positive, pad (with spaces)
17680 to at least that many characters.
17681 If first element is a symbol, process the cadr or caddr recursively
17682 according to whether the symbol's value is non-nil or nil. */
17683 car = XCAR (elt);
17684 if (EQ (car, QCeval))
17685 {
17686 /* An element of the form (:eval FORM) means evaluate FORM
17687 and use the result as mode line elements. */
17688
17689 if (risky)
17690 break;
17691
17692 if (CONSP (XCDR (elt)))
17693 {
17694 Lisp_Object spec;
17695 spec = safe_eval (XCAR (XCDR (elt)));
17696 n += display_mode_element (it, depth, field_width - n,
17697 precision - n, spec, props,
17698 risky);
17699 }
17700 }
17701 else if (EQ (car, QCpropertize))
17702 {
17703 /* An element of the form (:propertize ELT PROPS...)
17704 means display ELT but applying properties PROPS. */
17705
17706 if (risky)
17707 break;
17708
17709 if (CONSP (XCDR (elt)))
17710 n += display_mode_element (it, depth, field_width - n,
17711 precision - n, XCAR (XCDR (elt)),
17712 XCDR (XCDR (elt)), risky);
17713 }
17714 else if (SYMBOLP (car))
17715 {
17716 tem = Fboundp (car);
17717 elt = XCDR (elt);
17718 if (!CONSP (elt))
17719 goto invalid;
17720 /* elt is now the cdr, and we know it is a cons cell.
17721 Use its car if CAR has a non-nil value. */
17722 if (!NILP (tem))
17723 {
17724 tem = Fsymbol_value (car);
17725 if (!NILP (tem))
17726 {
17727 elt = XCAR (elt);
17728 goto tail_recurse;
17729 }
17730 }
17731 /* Symbol's value is nil (or symbol is unbound)
17732 Get the cddr of the original list
17733 and if possible find the caddr and use that. */
17734 elt = XCDR (elt);
17735 if (NILP (elt))
17736 break;
17737 else if (!CONSP (elt))
17738 goto invalid;
17739 elt = XCAR (elt);
17740 goto tail_recurse;
17741 }
17742 else if (INTEGERP (car))
17743 {
17744 register int lim = XINT (car);
17745 elt = XCDR (elt);
17746 if (lim < 0)
17747 {
17748 /* Negative int means reduce maximum width. */
17749 if (precision <= 0)
17750 precision = -lim;
17751 else
17752 precision = min (precision, -lim);
17753 }
17754 else if (lim > 0)
17755 {
17756 /* Padding specified. Don't let it be more than
17757 current maximum. */
17758 if (precision > 0)
17759 lim = min (precision, lim);
17760
17761 /* If that's more padding than already wanted, queue it.
17762 But don't reduce padding already specified even if
17763 that is beyond the current truncation point. */
17764 field_width = max (lim, field_width);
17765 }
17766 goto tail_recurse;
17767 }
17768 else if (STRINGP (car) || CONSP (car))
17769 {
17770 Lisp_Object halftail = elt;
17771 int len = 0;
17772
17773 while (CONSP (elt)
17774 && (precision <= 0 || n < precision))
17775 {
17776 n += display_mode_element (it, depth,
17777 /* Do padding only after the last
17778 element in the list. */
17779 (! CONSP (XCDR (elt))
17780 ? field_width - n
17781 : 0),
17782 precision - n, XCAR (elt),
17783 props, risky);
17784 elt = XCDR (elt);
17785 len++;
17786 if ((len & 1) == 0)
17787 halftail = XCDR (halftail);
17788 /* Check for cycle. */
17789 if (EQ (halftail, elt))
17790 break;
17791 }
17792 }
17793 }
17794 break;
17795
17796 default:
17797 invalid:
17798 elt = build_string ("*invalid*");
17799 goto tail_recurse;
17800 }
17801
17802 /* Pad to FIELD_WIDTH. */
17803 if (field_width > 0 && n < field_width)
17804 {
17805 switch (mode_line_target)
17806 {
17807 case MODE_LINE_NOPROP:
17808 case MODE_LINE_TITLE:
17809 n += store_mode_line_noprop ("", field_width - n, 0);
17810 break;
17811 case MODE_LINE_STRING:
17812 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17813 break;
17814 case MODE_LINE_DISPLAY:
17815 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17816 0, 0, 0);
17817 break;
17818 }
17819 }
17820
17821 return n;
17822 }
17823
17824 /* Store a mode-line string element in mode_line_string_list.
17825
17826 If STRING is non-null, display that C string. Otherwise, the Lisp
17827 string LISP_STRING is displayed.
17828
17829 FIELD_WIDTH is the minimum number of output glyphs to produce.
17830 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17831 with spaces. FIELD_WIDTH <= 0 means don't pad.
17832
17833 PRECISION is the maximum number of characters to output from
17834 STRING. PRECISION <= 0 means don't truncate the string.
17835
17836 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17837 properties to the string.
17838
17839 PROPS are the properties to add to the string.
17840 The mode_line_string_face face property is always added to the string.
17841 */
17842
17843 static int
17844 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17845 char *string;
17846 Lisp_Object lisp_string;
17847 int copy_string;
17848 int field_width;
17849 int precision;
17850 Lisp_Object props;
17851 {
17852 int len;
17853 int n = 0;
17854
17855 if (string != NULL)
17856 {
17857 len = strlen (string);
17858 if (precision > 0 && len > precision)
17859 len = precision;
17860 lisp_string = make_string (string, len);
17861 if (NILP (props))
17862 props = mode_line_string_face_prop;
17863 else if (!NILP (mode_line_string_face))
17864 {
17865 Lisp_Object face = Fplist_get (props, Qface);
17866 props = Fcopy_sequence (props);
17867 if (NILP (face))
17868 face = mode_line_string_face;
17869 else
17870 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17871 props = Fplist_put (props, Qface, face);
17872 }
17873 Fadd_text_properties (make_number (0), make_number (len),
17874 props, lisp_string);
17875 }
17876 else
17877 {
17878 len = XFASTINT (Flength (lisp_string));
17879 if (precision > 0 && len > precision)
17880 {
17881 len = precision;
17882 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17883 precision = -1;
17884 }
17885 if (!NILP (mode_line_string_face))
17886 {
17887 Lisp_Object face;
17888 if (NILP (props))
17889 props = Ftext_properties_at (make_number (0), lisp_string);
17890 face = Fplist_get (props, Qface);
17891 if (NILP (face))
17892 face = mode_line_string_face;
17893 else
17894 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17895 props = Fcons (Qface, Fcons (face, Qnil));
17896 if (copy_string)
17897 lisp_string = Fcopy_sequence (lisp_string);
17898 }
17899 if (!NILP (props))
17900 Fadd_text_properties (make_number (0), make_number (len),
17901 props, lisp_string);
17902 }
17903
17904 if (len > 0)
17905 {
17906 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17907 n += len;
17908 }
17909
17910 if (field_width > len)
17911 {
17912 field_width -= len;
17913 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17914 if (!NILP (props))
17915 Fadd_text_properties (make_number (0), make_number (field_width),
17916 props, lisp_string);
17917 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17918 n += field_width;
17919 }
17920
17921 return n;
17922 }
17923
17924
17925 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17926 1, 4, 0,
17927 doc: /* Format a string out of a mode line format specification.
17928 First arg FORMAT specifies the mode line format (see `mode-line-format'
17929 for details) to use.
17930
17931 By default, the format is evaluated for the currently selected window.
17932
17933 Optional second arg FACE specifies the face property to put on all
17934 characters for which no face is specified. The value nil means the
17935 default face. The value t means whatever face the window's mode line
17936 currently uses (either `mode-line' or `mode-line-inactive',
17937 depending on whether the window is the selected window or not).
17938 An integer value means the value string has no text
17939 properties.
17940
17941 Optional third and fourth args WINDOW and BUFFER specify the window
17942 and buffer to use as the context for the formatting (defaults
17943 are the selected window and the WINDOW's buffer). */)
17944 (format, face, window, buffer)
17945 Lisp_Object format, face, window, buffer;
17946 {
17947 struct it it;
17948 int len;
17949 struct window *w;
17950 struct buffer *old_buffer = NULL;
17951 int face_id;
17952 int no_props = INTEGERP (face);
17953 int count = SPECPDL_INDEX ();
17954 Lisp_Object str;
17955 int string_start = 0;
17956
17957 if (NILP (window))
17958 window = selected_window;
17959 CHECK_WINDOW (window);
17960 w = XWINDOW (window);
17961
17962 if (NILP (buffer))
17963 buffer = w->buffer;
17964 CHECK_BUFFER (buffer);
17965
17966 /* Make formatting the modeline a non-op when noninteractive, otherwise
17967 there will be problems later caused by a partially initialized frame. */
17968 if (NILP (format) || noninteractive)
17969 return empty_unibyte_string;
17970
17971 if (no_props)
17972 face = Qnil;
17973
17974 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
17975 : EQ (face, Qt) ? (EQ (window, selected_window)
17976 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
17977 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
17978 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
17979 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
17980 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
17981 : DEFAULT_FACE_ID;
17982
17983 if (XBUFFER (buffer) != current_buffer)
17984 old_buffer = current_buffer;
17985
17986 /* Save things including mode_line_proptrans_alist,
17987 and set that to nil so that we don't alter the outer value. */
17988 record_unwind_protect (unwind_format_mode_line,
17989 format_mode_line_unwind_data
17990 (old_buffer, selected_window, 1));
17991 mode_line_proptrans_alist = Qnil;
17992
17993 Fselect_window (window, Qt);
17994 if (old_buffer)
17995 set_buffer_internal_1 (XBUFFER (buffer));
17996
17997 init_iterator (&it, w, -1, -1, NULL, face_id);
17998
17999 if (no_props)
18000 {
18001 mode_line_target = MODE_LINE_NOPROP;
18002 mode_line_string_face_prop = Qnil;
18003 mode_line_string_list = Qnil;
18004 string_start = MODE_LINE_NOPROP_LEN (0);
18005 }
18006 else
18007 {
18008 mode_line_target = MODE_LINE_STRING;
18009 mode_line_string_list = Qnil;
18010 mode_line_string_face = face;
18011 mode_line_string_face_prop
18012 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18013 }
18014
18015 push_kboard (FRAME_KBOARD (it.f));
18016 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18017 pop_kboard ();
18018
18019 if (no_props)
18020 {
18021 len = MODE_LINE_NOPROP_LEN (string_start);
18022 str = make_string (mode_line_noprop_buf + string_start, len);
18023 }
18024 else
18025 {
18026 mode_line_string_list = Fnreverse (mode_line_string_list);
18027 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18028 empty_unibyte_string);
18029 }
18030
18031 unbind_to (count, Qnil);
18032 return str;
18033 }
18034
18035 /* Write a null-terminated, right justified decimal representation of
18036 the positive integer D to BUF using a minimal field width WIDTH. */
18037
18038 static void
18039 pint2str (buf, width, d)
18040 register char *buf;
18041 register int width;
18042 register int d;
18043 {
18044 register char *p = buf;
18045
18046 if (d <= 0)
18047 *p++ = '0';
18048 else
18049 {
18050 while (d > 0)
18051 {
18052 *p++ = d % 10 + '0';
18053 d /= 10;
18054 }
18055 }
18056
18057 for (width -= (int) (p - buf); width > 0; --width)
18058 *p++ = ' ';
18059 *p-- = '\0';
18060 while (p > buf)
18061 {
18062 d = *buf;
18063 *buf++ = *p;
18064 *p-- = d;
18065 }
18066 }
18067
18068 /* Write a null-terminated, right justified decimal and "human
18069 readable" representation of the nonnegative integer D to BUF using
18070 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18071
18072 static const char power_letter[] =
18073 {
18074 0, /* not used */
18075 'k', /* kilo */
18076 'M', /* mega */
18077 'G', /* giga */
18078 'T', /* tera */
18079 'P', /* peta */
18080 'E', /* exa */
18081 'Z', /* zetta */
18082 'Y' /* yotta */
18083 };
18084
18085 static void
18086 pint2hrstr (buf, width, d)
18087 char *buf;
18088 int width;
18089 int d;
18090 {
18091 /* We aim to represent the nonnegative integer D as
18092 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18093 int quotient = d;
18094 int remainder = 0;
18095 /* -1 means: do not use TENTHS. */
18096 int tenths = -1;
18097 int exponent = 0;
18098
18099 /* Length of QUOTIENT.TENTHS as a string. */
18100 int length;
18101
18102 char * psuffix;
18103 char * p;
18104
18105 if (1000 <= quotient)
18106 {
18107 /* Scale to the appropriate EXPONENT. */
18108 do
18109 {
18110 remainder = quotient % 1000;
18111 quotient /= 1000;
18112 exponent++;
18113 }
18114 while (1000 <= quotient);
18115
18116 /* Round to nearest and decide whether to use TENTHS or not. */
18117 if (quotient <= 9)
18118 {
18119 tenths = remainder / 100;
18120 if (50 <= remainder % 100)
18121 {
18122 if (tenths < 9)
18123 tenths++;
18124 else
18125 {
18126 quotient++;
18127 if (quotient == 10)
18128 tenths = -1;
18129 else
18130 tenths = 0;
18131 }
18132 }
18133 }
18134 else
18135 if (500 <= remainder)
18136 {
18137 if (quotient < 999)
18138 quotient++;
18139 else
18140 {
18141 quotient = 1;
18142 exponent++;
18143 tenths = 0;
18144 }
18145 }
18146 }
18147
18148 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18149 if (tenths == -1 && quotient <= 99)
18150 if (quotient <= 9)
18151 length = 1;
18152 else
18153 length = 2;
18154 else
18155 length = 3;
18156 p = psuffix = buf + max (width, length);
18157
18158 /* Print EXPONENT. */
18159 if (exponent)
18160 *psuffix++ = power_letter[exponent];
18161 *psuffix = '\0';
18162
18163 /* Print TENTHS. */
18164 if (tenths >= 0)
18165 {
18166 *--p = '0' + tenths;
18167 *--p = '.';
18168 }
18169
18170 /* Print QUOTIENT. */
18171 do
18172 {
18173 int digit = quotient % 10;
18174 *--p = '0' + digit;
18175 }
18176 while ((quotient /= 10) != 0);
18177
18178 /* Print leading spaces. */
18179 while (buf < p)
18180 *--p = ' ';
18181 }
18182
18183 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18184 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18185 type of CODING_SYSTEM. Return updated pointer into BUF. */
18186
18187 static unsigned char invalid_eol_type[] = "(*invalid*)";
18188
18189 static char *
18190 decode_mode_spec_coding (coding_system, buf, eol_flag)
18191 Lisp_Object coding_system;
18192 register char *buf;
18193 int eol_flag;
18194 {
18195 Lisp_Object val;
18196 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18197 const unsigned char *eol_str;
18198 int eol_str_len;
18199 /* The EOL conversion we are using. */
18200 Lisp_Object eoltype;
18201
18202 val = CODING_SYSTEM_SPEC (coding_system);
18203 eoltype = Qnil;
18204
18205 if (!VECTORP (val)) /* Not yet decided. */
18206 {
18207 if (multibyte)
18208 *buf++ = '-';
18209 if (eol_flag)
18210 eoltype = eol_mnemonic_undecided;
18211 /* Don't mention EOL conversion if it isn't decided. */
18212 }
18213 else
18214 {
18215 Lisp_Object attrs;
18216 Lisp_Object eolvalue;
18217
18218 attrs = AREF (val, 0);
18219 eolvalue = AREF (val, 2);
18220
18221 if (multibyte)
18222 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18223
18224 if (eol_flag)
18225 {
18226 /* The EOL conversion that is normal on this system. */
18227
18228 if (NILP (eolvalue)) /* Not yet decided. */
18229 eoltype = eol_mnemonic_undecided;
18230 else if (VECTORP (eolvalue)) /* Not yet decided. */
18231 eoltype = eol_mnemonic_undecided;
18232 else /* eolvalue is Qunix, Qdos, or Qmac. */
18233 eoltype = (EQ (eolvalue, Qunix)
18234 ? eol_mnemonic_unix
18235 : (EQ (eolvalue, Qdos) == 1
18236 ? eol_mnemonic_dos : eol_mnemonic_mac));
18237 }
18238 }
18239
18240 if (eol_flag)
18241 {
18242 /* Mention the EOL conversion if it is not the usual one. */
18243 if (STRINGP (eoltype))
18244 {
18245 eol_str = SDATA (eoltype);
18246 eol_str_len = SBYTES (eoltype);
18247 }
18248 else if (CHARACTERP (eoltype))
18249 {
18250 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18251 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18252 eol_str = tmp;
18253 }
18254 else
18255 {
18256 eol_str = invalid_eol_type;
18257 eol_str_len = sizeof (invalid_eol_type) - 1;
18258 }
18259 bcopy (eol_str, buf, eol_str_len);
18260 buf += eol_str_len;
18261 }
18262
18263 return buf;
18264 }
18265
18266 /* Return a string for the output of a mode line %-spec for window W,
18267 generated by character C. PRECISION >= 0 means don't return a
18268 string longer than that value. FIELD_WIDTH > 0 means pad the
18269 string returned with spaces to that value. Return a Lisp string in
18270 *STRING if the resulting string is taken from that Lisp string.
18271
18272 Note we operate on the current buffer for most purposes,
18273 the exception being w->base_line_pos. */
18274
18275 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18276
18277 static char *
18278 decode_mode_spec (w, c, field_width, precision, string)
18279 struct window *w;
18280 register int c;
18281 int field_width, precision;
18282 Lisp_Object *string;
18283 {
18284 Lisp_Object obj;
18285 struct frame *f = XFRAME (WINDOW_FRAME (w));
18286 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18287 struct buffer *b = current_buffer;
18288
18289 obj = Qnil;
18290 *string = Qnil;
18291
18292 switch (c)
18293 {
18294 case '*':
18295 if (!NILP (b->read_only))
18296 return "%";
18297 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18298 return "*";
18299 return "-";
18300
18301 case '+':
18302 /* This differs from %* only for a modified read-only buffer. */
18303 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18304 return "*";
18305 if (!NILP (b->read_only))
18306 return "%";
18307 return "-";
18308
18309 case '&':
18310 /* This differs from %* in ignoring read-only-ness. */
18311 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18312 return "*";
18313 return "-";
18314
18315 case '%':
18316 return "%";
18317
18318 case '[':
18319 {
18320 int i;
18321 char *p;
18322
18323 if (command_loop_level > 5)
18324 return "[[[... ";
18325 p = decode_mode_spec_buf;
18326 for (i = 0; i < command_loop_level; i++)
18327 *p++ = '[';
18328 *p = 0;
18329 return decode_mode_spec_buf;
18330 }
18331
18332 case ']':
18333 {
18334 int i;
18335 char *p;
18336
18337 if (command_loop_level > 5)
18338 return " ...]]]";
18339 p = decode_mode_spec_buf;
18340 for (i = 0; i < command_loop_level; i++)
18341 *p++ = ']';
18342 *p = 0;
18343 return decode_mode_spec_buf;
18344 }
18345
18346 case '-':
18347 {
18348 register int i;
18349
18350 /* Let lots_of_dashes be a string of infinite length. */
18351 if (mode_line_target == MODE_LINE_NOPROP ||
18352 mode_line_target == MODE_LINE_STRING)
18353 return "--";
18354 if (field_width <= 0
18355 || field_width > sizeof (lots_of_dashes))
18356 {
18357 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18358 decode_mode_spec_buf[i] = '-';
18359 decode_mode_spec_buf[i] = '\0';
18360 return decode_mode_spec_buf;
18361 }
18362 else
18363 return lots_of_dashes;
18364 }
18365
18366 case 'b':
18367 obj = b->name;
18368 break;
18369
18370 case 'c':
18371 /* %c and %l are ignored in `frame-title-format'.
18372 (In redisplay_internal, the frame title is drawn _before_ the
18373 windows are updated, so the stuff which depends on actual
18374 window contents (such as %l) may fail to render properly, or
18375 even crash emacs.) */
18376 if (mode_line_target == MODE_LINE_TITLE)
18377 return "";
18378 else
18379 {
18380 int col = (int) current_column (); /* iftc */
18381 w->column_number_displayed = make_number (col);
18382 pint2str (decode_mode_spec_buf, field_width, col);
18383 return decode_mode_spec_buf;
18384 }
18385
18386 case 'e':
18387 #ifndef SYSTEM_MALLOC
18388 {
18389 if (NILP (Vmemory_full))
18390 return "";
18391 else
18392 return "!MEM FULL! ";
18393 }
18394 #else
18395 return "";
18396 #endif
18397
18398 case 'F':
18399 /* %F displays the frame name. */
18400 if (!NILP (f->title))
18401 return (char *) SDATA (f->title);
18402 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18403 return (char *) SDATA (f->name);
18404 return "Emacs";
18405
18406 case 'f':
18407 obj = b->filename;
18408 break;
18409
18410 case 'i':
18411 {
18412 int size = ZV - BEGV;
18413 pint2str (decode_mode_spec_buf, field_width, size);
18414 return decode_mode_spec_buf;
18415 }
18416
18417 case 'I':
18418 {
18419 int size = ZV - BEGV;
18420 pint2hrstr (decode_mode_spec_buf, field_width, size);
18421 return decode_mode_spec_buf;
18422 }
18423
18424 case 'l':
18425 {
18426 int startpos, startpos_byte, line, linepos, linepos_byte;
18427 int topline, nlines, junk, height;
18428
18429 /* %c and %l are ignored in `frame-title-format'. */
18430 if (mode_line_target == MODE_LINE_TITLE)
18431 return "";
18432
18433 startpos = XMARKER (w->start)->charpos;
18434 startpos_byte = marker_byte_position (w->start);
18435 height = WINDOW_TOTAL_LINES (w);
18436
18437 /* If we decided that this buffer isn't suitable for line numbers,
18438 don't forget that too fast. */
18439 if (EQ (w->base_line_pos, w->buffer))
18440 goto no_value;
18441 /* But do forget it, if the window shows a different buffer now. */
18442 else if (BUFFERP (w->base_line_pos))
18443 w->base_line_pos = Qnil;
18444
18445 /* If the buffer is very big, don't waste time. */
18446 if (INTEGERP (Vline_number_display_limit)
18447 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18448 {
18449 w->base_line_pos = Qnil;
18450 w->base_line_number = Qnil;
18451 goto no_value;
18452 }
18453
18454 if (INTEGERP (w->base_line_number)
18455 && INTEGERP (w->base_line_pos)
18456 && XFASTINT (w->base_line_pos) <= startpos)
18457 {
18458 line = XFASTINT (w->base_line_number);
18459 linepos = XFASTINT (w->base_line_pos);
18460 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18461 }
18462 else
18463 {
18464 line = 1;
18465 linepos = BUF_BEGV (b);
18466 linepos_byte = BUF_BEGV_BYTE (b);
18467 }
18468
18469 /* Count lines from base line to window start position. */
18470 nlines = display_count_lines (linepos, linepos_byte,
18471 startpos_byte,
18472 startpos, &junk);
18473
18474 topline = nlines + line;
18475
18476 /* Determine a new base line, if the old one is too close
18477 or too far away, or if we did not have one.
18478 "Too close" means it's plausible a scroll-down would
18479 go back past it. */
18480 if (startpos == BUF_BEGV (b))
18481 {
18482 w->base_line_number = make_number (topline);
18483 w->base_line_pos = make_number (BUF_BEGV (b));
18484 }
18485 else if (nlines < height + 25 || nlines > height * 3 + 50
18486 || linepos == BUF_BEGV (b))
18487 {
18488 int limit = BUF_BEGV (b);
18489 int limit_byte = BUF_BEGV_BYTE (b);
18490 int position;
18491 int distance = (height * 2 + 30) * line_number_display_limit_width;
18492
18493 if (startpos - distance > limit)
18494 {
18495 limit = startpos - distance;
18496 limit_byte = CHAR_TO_BYTE (limit);
18497 }
18498
18499 nlines = display_count_lines (startpos, startpos_byte,
18500 limit_byte,
18501 - (height * 2 + 30),
18502 &position);
18503 /* If we couldn't find the lines we wanted within
18504 line_number_display_limit_width chars per line,
18505 give up on line numbers for this window. */
18506 if (position == limit_byte && limit == startpos - distance)
18507 {
18508 w->base_line_pos = w->buffer;
18509 w->base_line_number = Qnil;
18510 goto no_value;
18511 }
18512
18513 w->base_line_number = make_number (topline - nlines);
18514 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18515 }
18516
18517 /* Now count lines from the start pos to point. */
18518 nlines = display_count_lines (startpos, startpos_byte,
18519 PT_BYTE, PT, &junk);
18520
18521 /* Record that we did display the line number. */
18522 line_number_displayed = 1;
18523
18524 /* Make the string to show. */
18525 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18526 return decode_mode_spec_buf;
18527 no_value:
18528 {
18529 char* p = decode_mode_spec_buf;
18530 int pad = field_width - 2;
18531 while (pad-- > 0)
18532 *p++ = ' ';
18533 *p++ = '?';
18534 *p++ = '?';
18535 *p = '\0';
18536 return decode_mode_spec_buf;
18537 }
18538 }
18539 break;
18540
18541 case 'm':
18542 obj = b->mode_name;
18543 break;
18544
18545 case 'n':
18546 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18547 return " Narrow";
18548 break;
18549
18550 case 'p':
18551 {
18552 int pos = marker_position (w->start);
18553 int total = BUF_ZV (b) - BUF_BEGV (b);
18554
18555 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18556 {
18557 if (pos <= BUF_BEGV (b))
18558 return "All";
18559 else
18560 return "Bottom";
18561 }
18562 else if (pos <= BUF_BEGV (b))
18563 return "Top";
18564 else
18565 {
18566 if (total > 1000000)
18567 /* Do it differently for a large value, to avoid overflow. */
18568 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18569 else
18570 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18571 /* We can't normally display a 3-digit number,
18572 so get us a 2-digit number that is close. */
18573 if (total == 100)
18574 total = 99;
18575 sprintf (decode_mode_spec_buf, "%2d%%", total);
18576 return decode_mode_spec_buf;
18577 }
18578 }
18579
18580 /* Display percentage of size above the bottom of the screen. */
18581 case 'P':
18582 {
18583 int toppos = marker_position (w->start);
18584 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18585 int total = BUF_ZV (b) - BUF_BEGV (b);
18586
18587 if (botpos >= BUF_ZV (b))
18588 {
18589 if (toppos <= BUF_BEGV (b))
18590 return "All";
18591 else
18592 return "Bottom";
18593 }
18594 else
18595 {
18596 if (total > 1000000)
18597 /* Do it differently for a large value, to avoid overflow. */
18598 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18599 else
18600 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18601 /* We can't normally display a 3-digit number,
18602 so get us a 2-digit number that is close. */
18603 if (total == 100)
18604 total = 99;
18605 if (toppos <= BUF_BEGV (b))
18606 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18607 else
18608 sprintf (decode_mode_spec_buf, "%2d%%", total);
18609 return decode_mode_spec_buf;
18610 }
18611 }
18612
18613 case 's':
18614 /* status of process */
18615 obj = Fget_buffer_process (Fcurrent_buffer ());
18616 if (NILP (obj))
18617 return "no process";
18618 #ifdef subprocesses
18619 obj = Fsymbol_name (Fprocess_status (obj));
18620 #endif
18621 break;
18622
18623 case '@':
18624 {
18625 int count = inhibit_garbage_collection ();
18626 Lisp_Object val = call1 (intern ("file-remote-p"),
18627 current_buffer->directory);
18628 unbind_to (count, Qnil);
18629
18630 if (NILP (val))
18631 return "-";
18632 else
18633 return "@";
18634 }
18635
18636 case 't': /* indicate TEXT or BINARY */
18637 #ifdef MODE_LINE_BINARY_TEXT
18638 return MODE_LINE_BINARY_TEXT (b);
18639 #else
18640 return "T";
18641 #endif
18642
18643 case 'z':
18644 /* coding-system (not including end-of-line format) */
18645 case 'Z':
18646 /* coding-system (including end-of-line type) */
18647 {
18648 int eol_flag = (c == 'Z');
18649 char *p = decode_mode_spec_buf;
18650
18651 if (! FRAME_WINDOW_P (f))
18652 {
18653 /* No need to mention EOL here--the terminal never needs
18654 to do EOL conversion. */
18655 p = decode_mode_spec_coding (CODING_ID_NAME
18656 (FRAME_KEYBOARD_CODING (f)->id),
18657 p, 0);
18658 p = decode_mode_spec_coding (CODING_ID_NAME
18659 (FRAME_TERMINAL_CODING (f)->id),
18660 p, 0);
18661 }
18662 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18663 p, eol_flag);
18664
18665 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18666 #ifdef subprocesses
18667 obj = Fget_buffer_process (Fcurrent_buffer ());
18668 if (PROCESSP (obj))
18669 {
18670 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18671 p, eol_flag);
18672 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18673 p, eol_flag);
18674 }
18675 #endif /* subprocesses */
18676 #endif /* 0 */
18677 *p = 0;
18678 return decode_mode_spec_buf;
18679 }
18680 }
18681
18682 if (STRINGP (obj))
18683 {
18684 *string = obj;
18685 return (char *) SDATA (obj);
18686 }
18687 else
18688 return "";
18689 }
18690
18691
18692 /* Count up to COUNT lines starting from START / START_BYTE.
18693 But don't go beyond LIMIT_BYTE.
18694 Return the number of lines thus found (always nonnegative).
18695
18696 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18697
18698 static int
18699 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18700 int start, start_byte, limit_byte, count;
18701 int *byte_pos_ptr;
18702 {
18703 register unsigned char *cursor;
18704 unsigned char *base;
18705
18706 register int ceiling;
18707 register unsigned char *ceiling_addr;
18708 int orig_count = count;
18709
18710 /* If we are not in selective display mode,
18711 check only for newlines. */
18712 int selective_display = (!NILP (current_buffer->selective_display)
18713 && !INTEGERP (current_buffer->selective_display));
18714
18715 if (count > 0)
18716 {
18717 while (start_byte < limit_byte)
18718 {
18719 ceiling = BUFFER_CEILING_OF (start_byte);
18720 ceiling = min (limit_byte - 1, ceiling);
18721 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18722 base = (cursor = BYTE_POS_ADDR (start_byte));
18723 while (1)
18724 {
18725 if (selective_display)
18726 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18727 ;
18728 else
18729 while (*cursor != '\n' && ++cursor != ceiling_addr)
18730 ;
18731
18732 if (cursor != ceiling_addr)
18733 {
18734 if (--count == 0)
18735 {
18736 start_byte += cursor - base + 1;
18737 *byte_pos_ptr = start_byte;
18738 return orig_count;
18739 }
18740 else
18741 if (++cursor == ceiling_addr)
18742 break;
18743 }
18744 else
18745 break;
18746 }
18747 start_byte += cursor - base;
18748 }
18749 }
18750 else
18751 {
18752 while (start_byte > limit_byte)
18753 {
18754 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18755 ceiling = max (limit_byte, ceiling);
18756 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18757 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18758 while (1)
18759 {
18760 if (selective_display)
18761 while (--cursor != ceiling_addr
18762 && *cursor != '\n' && *cursor != 015)
18763 ;
18764 else
18765 while (--cursor != ceiling_addr && *cursor != '\n')
18766 ;
18767
18768 if (cursor != ceiling_addr)
18769 {
18770 if (++count == 0)
18771 {
18772 start_byte += cursor - base + 1;
18773 *byte_pos_ptr = start_byte;
18774 /* When scanning backwards, we should
18775 not count the newline posterior to which we stop. */
18776 return - orig_count - 1;
18777 }
18778 }
18779 else
18780 break;
18781 }
18782 /* Here we add 1 to compensate for the last decrement
18783 of CURSOR, which took it past the valid range. */
18784 start_byte += cursor - base + 1;
18785 }
18786 }
18787
18788 *byte_pos_ptr = limit_byte;
18789
18790 if (count < 0)
18791 return - orig_count + count;
18792 return orig_count - count;
18793
18794 }
18795
18796
18797 \f
18798 /***********************************************************************
18799 Displaying strings
18800 ***********************************************************************/
18801
18802 /* Display a NUL-terminated string, starting with index START.
18803
18804 If STRING is non-null, display that C string. Otherwise, the Lisp
18805 string LISP_STRING is displayed. There's a case that STRING is
18806 non-null and LISP_STRING is not nil. It means STRING is a string
18807 data of LISP_STRING. In that case, we display LISP_STRING while
18808 ignoring its text properties.
18809
18810 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18811 FACE_STRING. Display STRING or LISP_STRING with the face at
18812 FACE_STRING_POS in FACE_STRING:
18813
18814 Display the string in the environment given by IT, but use the
18815 standard display table, temporarily.
18816
18817 FIELD_WIDTH is the minimum number of output glyphs to produce.
18818 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18819 with spaces. If STRING has more characters, more than FIELD_WIDTH
18820 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18821
18822 PRECISION is the maximum number of characters to output from
18823 STRING. PRECISION < 0 means don't truncate the string.
18824
18825 This is roughly equivalent to printf format specifiers:
18826
18827 FIELD_WIDTH PRECISION PRINTF
18828 ----------------------------------------
18829 -1 -1 %s
18830 -1 10 %.10s
18831 10 -1 %10s
18832 20 10 %20.10s
18833
18834 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18835 display them, and < 0 means obey the current buffer's value of
18836 enable_multibyte_characters.
18837
18838 Value is the number of columns displayed. */
18839
18840 static int
18841 display_string (string, lisp_string, face_string, face_string_pos,
18842 start, it, field_width, precision, max_x, multibyte)
18843 unsigned char *string;
18844 Lisp_Object lisp_string;
18845 Lisp_Object face_string;
18846 EMACS_INT face_string_pos;
18847 EMACS_INT start;
18848 struct it *it;
18849 int field_width, precision, max_x;
18850 int multibyte;
18851 {
18852 int hpos_at_start = it->hpos;
18853 int saved_face_id = it->face_id;
18854 struct glyph_row *row = it->glyph_row;
18855
18856 /* Initialize the iterator IT for iteration over STRING beginning
18857 with index START. */
18858 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
18859 precision, field_width, multibyte);
18860 if (string && STRINGP (lisp_string))
18861 /* LISP_STRING is the one returned by decode_mode_spec. We should
18862 ignore its text properties. */
18863 it->stop_charpos = -1;
18864
18865 /* If displaying STRING, set up the face of the iterator
18866 from LISP_STRING, if that's given. */
18867 if (STRINGP (face_string))
18868 {
18869 EMACS_INT endptr;
18870 struct face *face;
18871
18872 it->face_id
18873 = face_at_string_position (it->w, face_string, face_string_pos,
18874 0, it->region_beg_charpos,
18875 it->region_end_charpos,
18876 &endptr, it->base_face_id, 0);
18877 face = FACE_FROM_ID (it->f, it->face_id);
18878 it->face_box_p = face->box != FACE_NO_BOX;
18879 }
18880
18881 /* Set max_x to the maximum allowed X position. Don't let it go
18882 beyond the right edge of the window. */
18883 if (max_x <= 0)
18884 max_x = it->last_visible_x;
18885 else
18886 max_x = min (max_x, it->last_visible_x);
18887
18888 /* Skip over display elements that are not visible. because IT->w is
18889 hscrolled. */
18890 if (it->current_x < it->first_visible_x)
18891 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18892 MOVE_TO_POS | MOVE_TO_X);
18893
18894 row->ascent = it->max_ascent;
18895 row->height = it->max_ascent + it->max_descent;
18896 row->phys_ascent = it->max_phys_ascent;
18897 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18898 row->extra_line_spacing = it->max_extra_line_spacing;
18899
18900 /* This condition is for the case that we are called with current_x
18901 past last_visible_x. */
18902 while (it->current_x < max_x)
18903 {
18904 int x_before, x, n_glyphs_before, i, nglyphs;
18905
18906 /* Get the next display element. */
18907 if (!get_next_display_element (it))
18908 break;
18909
18910 /* Produce glyphs. */
18911 x_before = it->current_x;
18912 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18913 PRODUCE_GLYPHS (it);
18914
18915 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18916 i = 0;
18917 x = x_before;
18918 while (i < nglyphs)
18919 {
18920 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18921
18922 if (it->line_wrap != TRUNCATE
18923 && x + glyph->pixel_width > max_x)
18924 {
18925 /* End of continued line or max_x reached. */
18926 if (CHAR_GLYPH_PADDING_P (*glyph))
18927 {
18928 /* A wide character is unbreakable. */
18929 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18930 it->current_x = x_before;
18931 }
18932 else
18933 {
18934 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18935 it->current_x = x;
18936 }
18937 break;
18938 }
18939 else if (x + glyph->pixel_width >= it->first_visible_x)
18940 {
18941 /* Glyph is at least partially visible. */
18942 ++it->hpos;
18943 if (x < it->first_visible_x)
18944 it->glyph_row->x = x - it->first_visible_x;
18945 }
18946 else
18947 {
18948 /* Glyph is off the left margin of the display area.
18949 Should not happen. */
18950 abort ();
18951 }
18952
18953 row->ascent = max (row->ascent, it->max_ascent);
18954 row->height = max (row->height, it->max_ascent + it->max_descent);
18955 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18956 row->phys_height = max (row->phys_height,
18957 it->max_phys_ascent + it->max_phys_descent);
18958 row->extra_line_spacing = max (row->extra_line_spacing,
18959 it->max_extra_line_spacing);
18960 x += glyph->pixel_width;
18961 ++i;
18962 }
18963
18964 /* Stop if max_x reached. */
18965 if (i < nglyphs)
18966 break;
18967
18968 /* Stop at line ends. */
18969 if (ITERATOR_AT_END_OF_LINE_P (it))
18970 {
18971 it->continuation_lines_width = 0;
18972 break;
18973 }
18974
18975 set_iterator_to_next (it, 1);
18976
18977 /* Stop if truncating at the right edge. */
18978 if (it->line_wrap == TRUNCATE
18979 && it->current_x >= it->last_visible_x)
18980 {
18981 /* Add truncation mark, but don't do it if the line is
18982 truncated at a padding space. */
18983 if (IT_CHARPOS (*it) < it->string_nchars)
18984 {
18985 if (!FRAME_WINDOW_P (it->f))
18986 {
18987 int i, n;
18988
18989 if (it->current_x > it->last_visible_x)
18990 {
18991 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18992 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18993 break;
18994 for (n = row->used[TEXT_AREA]; i < n; ++i)
18995 {
18996 row->used[TEXT_AREA] = i;
18997 produce_special_glyphs (it, IT_TRUNCATION);
18998 }
18999 }
19000 produce_special_glyphs (it, IT_TRUNCATION);
19001 }
19002 it->glyph_row->truncated_on_right_p = 1;
19003 }
19004 break;
19005 }
19006 }
19007
19008 /* Maybe insert a truncation at the left. */
19009 if (it->first_visible_x
19010 && IT_CHARPOS (*it) > 0)
19011 {
19012 if (!FRAME_WINDOW_P (it->f))
19013 insert_left_trunc_glyphs (it);
19014 it->glyph_row->truncated_on_left_p = 1;
19015 }
19016
19017 it->face_id = saved_face_id;
19018
19019 /* Value is number of columns displayed. */
19020 return it->hpos - hpos_at_start;
19021 }
19022
19023
19024 \f
19025 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19026 appears as an element of LIST or as the car of an element of LIST.
19027 If PROPVAL is a list, compare each element against LIST in that
19028 way, and return 1/2 if any element of PROPVAL is found in LIST.
19029 Otherwise return 0. This function cannot quit.
19030 The return value is 2 if the text is invisible but with an ellipsis
19031 and 1 if it's invisible and without an ellipsis. */
19032
19033 int
19034 invisible_p (propval, list)
19035 register Lisp_Object propval;
19036 Lisp_Object list;
19037 {
19038 register Lisp_Object tail, proptail;
19039
19040 for (tail = list; CONSP (tail); tail = XCDR (tail))
19041 {
19042 register Lisp_Object tem;
19043 tem = XCAR (tail);
19044 if (EQ (propval, tem))
19045 return 1;
19046 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19047 return NILP (XCDR (tem)) ? 1 : 2;
19048 }
19049
19050 if (CONSP (propval))
19051 {
19052 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19053 {
19054 Lisp_Object propelt;
19055 propelt = XCAR (proptail);
19056 for (tail = list; CONSP (tail); tail = XCDR (tail))
19057 {
19058 register Lisp_Object tem;
19059 tem = XCAR (tail);
19060 if (EQ (propelt, tem))
19061 return 1;
19062 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19063 return NILP (XCDR (tem)) ? 1 : 2;
19064 }
19065 }
19066 }
19067
19068 return 0;
19069 }
19070
19071 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19072 doc: /* Non-nil if the property makes the text invisible.
19073 POS-OR-PROP can be a marker or number, in which case it is taken to be
19074 a position in the current buffer and the value of the `invisible' property
19075 is checked; or it can be some other value, which is then presumed to be the
19076 value of the `invisible' property of the text of interest.
19077 The non-nil value returned can be t for truly invisible text or something
19078 else if the text is replaced by an ellipsis. */)
19079 (pos_or_prop)
19080 Lisp_Object pos_or_prop;
19081 {
19082 Lisp_Object prop
19083 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19084 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19085 : pos_or_prop);
19086 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19087 return (invis == 0 ? Qnil
19088 : invis == 1 ? Qt
19089 : make_number (invis));
19090 }
19091
19092 /* Calculate a width or height in pixels from a specification using
19093 the following elements:
19094
19095 SPEC ::=
19096 NUM - a (fractional) multiple of the default font width/height
19097 (NUM) - specifies exactly NUM pixels
19098 UNIT - a fixed number of pixels, see below.
19099 ELEMENT - size of a display element in pixels, see below.
19100 (NUM . SPEC) - equals NUM * SPEC
19101 (+ SPEC SPEC ...) - add pixel values
19102 (- SPEC SPEC ...) - subtract pixel values
19103 (- SPEC) - negate pixel value
19104
19105 NUM ::=
19106 INT or FLOAT - a number constant
19107 SYMBOL - use symbol's (buffer local) variable binding.
19108
19109 UNIT ::=
19110 in - pixels per inch *)
19111 mm - pixels per 1/1000 meter *)
19112 cm - pixels per 1/100 meter *)
19113 width - width of current font in pixels.
19114 height - height of current font in pixels.
19115
19116 *) using the ratio(s) defined in display-pixels-per-inch.
19117
19118 ELEMENT ::=
19119
19120 left-fringe - left fringe width in pixels
19121 right-fringe - right fringe width in pixels
19122
19123 left-margin - left margin width in pixels
19124 right-margin - right margin width in pixels
19125
19126 scroll-bar - scroll-bar area width in pixels
19127
19128 Examples:
19129
19130 Pixels corresponding to 5 inches:
19131 (5 . in)
19132
19133 Total width of non-text areas on left side of window (if scroll-bar is on left):
19134 '(space :width (+ left-fringe left-margin scroll-bar))
19135
19136 Align to first text column (in header line):
19137 '(space :align-to 0)
19138
19139 Align to middle of text area minus half the width of variable `my-image'
19140 containing a loaded image:
19141 '(space :align-to (0.5 . (- text my-image)))
19142
19143 Width of left margin minus width of 1 character in the default font:
19144 '(space :width (- left-margin 1))
19145
19146 Width of left margin minus width of 2 characters in the current font:
19147 '(space :width (- left-margin (2 . width)))
19148
19149 Center 1 character over left-margin (in header line):
19150 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19151
19152 Different ways to express width of left fringe plus left margin minus one pixel:
19153 '(space :width (- (+ left-fringe left-margin) (1)))
19154 '(space :width (+ left-fringe left-margin (- (1))))
19155 '(space :width (+ left-fringe left-margin (-1)))
19156
19157 */
19158
19159 #define NUMVAL(X) \
19160 ((INTEGERP (X) || FLOATP (X)) \
19161 ? XFLOATINT (X) \
19162 : - 1)
19163
19164 int
19165 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19166 double *res;
19167 struct it *it;
19168 Lisp_Object prop;
19169 struct font *font;
19170 int width_p, *align_to;
19171 {
19172 double pixels;
19173
19174 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19175 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19176
19177 if (NILP (prop))
19178 return OK_PIXELS (0);
19179
19180 xassert (FRAME_LIVE_P (it->f));
19181
19182 if (SYMBOLP (prop))
19183 {
19184 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19185 {
19186 char *unit = SDATA (SYMBOL_NAME (prop));
19187
19188 if (unit[0] == 'i' && unit[1] == 'n')
19189 pixels = 1.0;
19190 else if (unit[0] == 'm' && unit[1] == 'm')
19191 pixels = 25.4;
19192 else if (unit[0] == 'c' && unit[1] == 'm')
19193 pixels = 2.54;
19194 else
19195 pixels = 0;
19196 if (pixels > 0)
19197 {
19198 double ppi;
19199 #ifdef HAVE_WINDOW_SYSTEM
19200 if (FRAME_WINDOW_P (it->f)
19201 && (ppi = (width_p
19202 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19203 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19204 ppi > 0))
19205 return OK_PIXELS (ppi / pixels);
19206 #endif
19207
19208 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19209 || (CONSP (Vdisplay_pixels_per_inch)
19210 && (ppi = (width_p
19211 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19212 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19213 ppi > 0)))
19214 return OK_PIXELS (ppi / pixels);
19215
19216 return 0;
19217 }
19218 }
19219
19220 #ifdef HAVE_WINDOW_SYSTEM
19221 if (EQ (prop, Qheight))
19222 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19223 if (EQ (prop, Qwidth))
19224 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19225 #else
19226 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19227 return OK_PIXELS (1);
19228 #endif
19229
19230 if (EQ (prop, Qtext))
19231 return OK_PIXELS (width_p
19232 ? window_box_width (it->w, TEXT_AREA)
19233 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19234
19235 if (align_to && *align_to < 0)
19236 {
19237 *res = 0;
19238 if (EQ (prop, Qleft))
19239 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19240 if (EQ (prop, Qright))
19241 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19242 if (EQ (prop, Qcenter))
19243 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19244 + window_box_width (it->w, TEXT_AREA) / 2);
19245 if (EQ (prop, Qleft_fringe))
19246 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19247 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19248 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19249 if (EQ (prop, Qright_fringe))
19250 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19251 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19252 : window_box_right_offset (it->w, TEXT_AREA));
19253 if (EQ (prop, Qleft_margin))
19254 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19255 if (EQ (prop, Qright_margin))
19256 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19257 if (EQ (prop, Qscroll_bar))
19258 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19259 ? 0
19260 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19261 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19262 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19263 : 0)));
19264 }
19265 else
19266 {
19267 if (EQ (prop, Qleft_fringe))
19268 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19269 if (EQ (prop, Qright_fringe))
19270 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19271 if (EQ (prop, Qleft_margin))
19272 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19273 if (EQ (prop, Qright_margin))
19274 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19275 if (EQ (prop, Qscroll_bar))
19276 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19277 }
19278
19279 prop = Fbuffer_local_value (prop, it->w->buffer);
19280 }
19281
19282 if (INTEGERP (prop) || FLOATP (prop))
19283 {
19284 int base_unit = (width_p
19285 ? FRAME_COLUMN_WIDTH (it->f)
19286 : FRAME_LINE_HEIGHT (it->f));
19287 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19288 }
19289
19290 if (CONSP (prop))
19291 {
19292 Lisp_Object car = XCAR (prop);
19293 Lisp_Object cdr = XCDR (prop);
19294
19295 if (SYMBOLP (car))
19296 {
19297 #ifdef HAVE_WINDOW_SYSTEM
19298 if (FRAME_WINDOW_P (it->f)
19299 && valid_image_p (prop))
19300 {
19301 int id = lookup_image (it->f, prop);
19302 struct image *img = IMAGE_FROM_ID (it->f, id);
19303
19304 return OK_PIXELS (width_p ? img->width : img->height);
19305 }
19306 #endif
19307 if (EQ (car, Qplus) || EQ (car, Qminus))
19308 {
19309 int first = 1;
19310 double px;
19311
19312 pixels = 0;
19313 while (CONSP (cdr))
19314 {
19315 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19316 font, width_p, align_to))
19317 return 0;
19318 if (first)
19319 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19320 else
19321 pixels += px;
19322 cdr = XCDR (cdr);
19323 }
19324 if (EQ (car, Qminus))
19325 pixels = -pixels;
19326 return OK_PIXELS (pixels);
19327 }
19328
19329 car = Fbuffer_local_value (car, it->w->buffer);
19330 }
19331
19332 if (INTEGERP (car) || FLOATP (car))
19333 {
19334 double fact;
19335 pixels = XFLOATINT (car);
19336 if (NILP (cdr))
19337 return OK_PIXELS (pixels);
19338 if (calc_pixel_width_or_height (&fact, it, cdr,
19339 font, width_p, align_to))
19340 return OK_PIXELS (pixels * fact);
19341 return 0;
19342 }
19343
19344 return 0;
19345 }
19346
19347 return 0;
19348 }
19349
19350 \f
19351 /***********************************************************************
19352 Glyph Display
19353 ***********************************************************************/
19354
19355 #ifdef HAVE_WINDOW_SYSTEM
19356
19357 #if GLYPH_DEBUG
19358
19359 void
19360 dump_glyph_string (s)
19361 struct glyph_string *s;
19362 {
19363 fprintf (stderr, "glyph string\n");
19364 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19365 s->x, s->y, s->width, s->height);
19366 fprintf (stderr, " ybase = %d\n", s->ybase);
19367 fprintf (stderr, " hl = %d\n", s->hl);
19368 fprintf (stderr, " left overhang = %d, right = %d\n",
19369 s->left_overhang, s->right_overhang);
19370 fprintf (stderr, " nchars = %d\n", s->nchars);
19371 fprintf (stderr, " extends to end of line = %d\n",
19372 s->extends_to_end_of_line_p);
19373 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19374 fprintf (stderr, " bg width = %d\n", s->background_width);
19375 }
19376
19377 #endif /* GLYPH_DEBUG */
19378
19379 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19380 of XChar2b structures for S; it can't be allocated in
19381 init_glyph_string because it must be allocated via `alloca'. W
19382 is the window on which S is drawn. ROW and AREA are the glyph row
19383 and area within the row from which S is constructed. START is the
19384 index of the first glyph structure covered by S. HL is a
19385 face-override for drawing S. */
19386
19387 #ifdef HAVE_NTGUI
19388 #define OPTIONAL_HDC(hdc) hdc,
19389 #define DECLARE_HDC(hdc) HDC hdc;
19390 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19391 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19392 #endif
19393
19394 #ifndef OPTIONAL_HDC
19395 #define OPTIONAL_HDC(hdc)
19396 #define DECLARE_HDC(hdc)
19397 #define ALLOCATE_HDC(hdc, f)
19398 #define RELEASE_HDC(hdc, f)
19399 #endif
19400
19401 static void
19402 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19403 struct glyph_string *s;
19404 DECLARE_HDC (hdc)
19405 XChar2b *char2b;
19406 struct window *w;
19407 struct glyph_row *row;
19408 enum glyph_row_area area;
19409 int start;
19410 enum draw_glyphs_face hl;
19411 {
19412 bzero (s, sizeof *s);
19413 s->w = w;
19414 s->f = XFRAME (w->frame);
19415 #ifdef HAVE_NTGUI
19416 s->hdc = hdc;
19417 #endif
19418 s->display = FRAME_X_DISPLAY (s->f);
19419 s->window = FRAME_X_WINDOW (s->f);
19420 s->char2b = char2b;
19421 s->hl = hl;
19422 s->row = row;
19423 s->area = area;
19424 s->first_glyph = row->glyphs[area] + start;
19425 s->height = row->height;
19426 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19427 s->ybase = s->y + row->ascent;
19428 }
19429
19430
19431 /* Append the list of glyph strings with head H and tail T to the list
19432 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19433
19434 static INLINE void
19435 append_glyph_string_lists (head, tail, h, t)
19436 struct glyph_string **head, **tail;
19437 struct glyph_string *h, *t;
19438 {
19439 if (h)
19440 {
19441 if (*head)
19442 (*tail)->next = h;
19443 else
19444 *head = h;
19445 h->prev = *tail;
19446 *tail = t;
19447 }
19448 }
19449
19450
19451 /* Prepend the list of glyph strings with head H and tail T to the
19452 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19453 result. */
19454
19455 static INLINE void
19456 prepend_glyph_string_lists (head, tail, h, t)
19457 struct glyph_string **head, **tail;
19458 struct glyph_string *h, *t;
19459 {
19460 if (h)
19461 {
19462 if (*head)
19463 (*head)->prev = t;
19464 else
19465 *tail = t;
19466 t->next = *head;
19467 *head = h;
19468 }
19469 }
19470
19471
19472 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19473 Set *HEAD and *TAIL to the resulting list. */
19474
19475 static INLINE void
19476 append_glyph_string (head, tail, s)
19477 struct glyph_string **head, **tail;
19478 struct glyph_string *s;
19479 {
19480 s->next = s->prev = NULL;
19481 append_glyph_string_lists (head, tail, s, s);
19482 }
19483
19484
19485 /* Get face and two-byte form of character C in face FACE_ID on frame
19486 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19487 means we want to display multibyte text. DISPLAY_P non-zero means
19488 make sure that X resources for the face returned are allocated.
19489 Value is a pointer to a realized face that is ready for display if
19490 DISPLAY_P is non-zero. */
19491
19492 static INLINE struct face *
19493 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19494 struct frame *f;
19495 int c, face_id;
19496 XChar2b *char2b;
19497 int multibyte_p, display_p;
19498 {
19499 struct face *face = FACE_FROM_ID (f, face_id);
19500
19501 if (face->font)
19502 {
19503 unsigned code = face->font->driver->encode_char (face->font, c);
19504
19505 if (code != FONT_INVALID_CODE)
19506 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19507 else
19508 STORE_XCHAR2B (char2b, 0, 0);
19509 }
19510
19511 /* Make sure X resources of the face are allocated. */
19512 #ifdef HAVE_X_WINDOWS
19513 if (display_p)
19514 #endif
19515 {
19516 xassert (face != NULL);
19517 PREPARE_FACE_FOR_DISPLAY (f, face);
19518 }
19519
19520 return face;
19521 }
19522
19523
19524 /* Get face and two-byte form of character glyph GLYPH on frame F.
19525 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19526 a pointer to a realized face that is ready for display. */
19527
19528 static INLINE struct face *
19529 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19530 struct frame *f;
19531 struct glyph *glyph;
19532 XChar2b *char2b;
19533 int *two_byte_p;
19534 {
19535 struct face *face;
19536
19537 xassert (glyph->type == CHAR_GLYPH);
19538 face = FACE_FROM_ID (f, glyph->face_id);
19539
19540 if (two_byte_p)
19541 *two_byte_p = 0;
19542
19543 if (face->font)
19544 {
19545 unsigned code;
19546
19547 if (CHAR_BYTE8_P (glyph->u.ch))
19548 code = CHAR_TO_BYTE8 (glyph->u.ch);
19549 else
19550 code = face->font->driver->encode_char (face->font, glyph->u.ch);
19551
19552 if (code != FONT_INVALID_CODE)
19553 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19554 else
19555 STORE_XCHAR2B (char2b, 0, 0);
19556 }
19557
19558 /* Make sure X resources of the face are allocated. */
19559 xassert (face != NULL);
19560 PREPARE_FACE_FOR_DISPLAY (f, face);
19561 return face;
19562 }
19563
19564
19565 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
19566 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
19567
19568 static INLINE int
19569 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
19570 {
19571 unsigned code;
19572
19573 if (CHAR_BYTE8_P (c))
19574 code = CHAR_TO_BYTE8 (c);
19575 else
19576 code = font->driver->encode_char (font, c);
19577
19578 if (code == FONT_INVALID_CODE)
19579 return 0;
19580 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19581 return 1;
19582 }
19583
19584
19585 /* Fill glyph string S with composition components specified by S->cmp.
19586
19587 BASE_FACE is the base face of the composition.
19588 S->cmp_from is the index of the first component for S.
19589
19590 OVERLAPS non-zero means S should draw the foreground only, and use
19591 its physical height for clipping. See also draw_glyphs.
19592
19593 Value is the index of a component not in S. */
19594
19595 static int
19596 fill_composite_glyph_string (s, base_face, overlaps)
19597 struct glyph_string *s;
19598 struct face *base_face;
19599 int overlaps;
19600 {
19601 int i;
19602 /* For all glyphs of this composition, starting at the offset
19603 S->cmp_from, until we reach the end of the definition or encounter a
19604 glyph that requires the different face, add it to S. */
19605 struct face *face;
19606
19607 xassert (s);
19608
19609 s->for_overlaps = overlaps;
19610 s->face = NULL;
19611 s->font = NULL;
19612 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19613 {
19614 int c = COMPOSITION_GLYPH (s->cmp, i);
19615
19616 if (c != '\t')
19617 {
19618 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19619 -1, Qnil);
19620
19621 face = get_char_face_and_encoding (s->f, c, face_id,
19622 s->char2b + i, 1, 1);
19623 if (face)
19624 {
19625 if (! s->face)
19626 {
19627 s->face = face;
19628 s->font = s->face->font;
19629 }
19630 else if (s->face != face)
19631 break;
19632 }
19633 }
19634 ++s->nchars;
19635 }
19636 s->cmp_to = i;
19637
19638 /* All glyph strings for the same composition has the same width,
19639 i.e. the width set for the first component of the composition. */
19640 s->width = s->first_glyph->pixel_width;
19641
19642 /* If the specified font could not be loaded, use the frame's
19643 default font, but record the fact that we couldn't load it in
19644 the glyph string so that we can draw rectangles for the
19645 characters of the glyph string. */
19646 if (s->font == NULL)
19647 {
19648 s->font_not_found_p = 1;
19649 s->font = FRAME_FONT (s->f);
19650 }
19651
19652 /* Adjust base line for subscript/superscript text. */
19653 s->ybase += s->first_glyph->voffset;
19654
19655 /* This glyph string must always be drawn with 16-bit functions. */
19656 s->two_byte_p = 1;
19657
19658 return s->cmp_to;
19659 }
19660
19661 static int
19662 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19663 struct glyph_string *s;
19664 int face_id;
19665 int start, end, overlaps;
19666 {
19667 struct glyph *glyph, *last;
19668 Lisp_Object lgstring;
19669 int i;
19670
19671 s->for_overlaps = overlaps;
19672 glyph = s->row->glyphs[s->area] + start;
19673 last = s->row->glyphs[s->area] + end;
19674 s->cmp_id = glyph->u.cmp.id;
19675 s->cmp_from = glyph->u.cmp.from;
19676 s->cmp_to = glyph->u.cmp.to + 1;
19677 s->face = FACE_FROM_ID (s->f, face_id);
19678 lgstring = composition_gstring_from_id (s->cmp_id);
19679 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19680 glyph++;
19681 while (glyph < last
19682 && glyph->u.cmp.automatic
19683 && glyph->u.cmp.id == s->cmp_id
19684 && s->cmp_to == glyph->u.cmp.from)
19685 s->cmp_to = (glyph++)->u.cmp.to + 1;
19686
19687 for (i = s->cmp_from; i < s->cmp_to; i++)
19688 {
19689 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19690 unsigned code = LGLYPH_CODE (lglyph);
19691
19692 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19693 }
19694 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19695 return glyph - s->row->glyphs[s->area];
19696 }
19697
19698
19699 /* Fill glyph string S from a sequence of character glyphs.
19700
19701 FACE_ID is the face id of the string. START is the index of the
19702 first glyph to consider, END is the index of the last + 1.
19703 OVERLAPS non-zero means S should draw the foreground only, and use
19704 its physical height for clipping. See also draw_glyphs.
19705
19706 Value is the index of the first glyph not in S. */
19707
19708 static int
19709 fill_glyph_string (s, face_id, start, end, overlaps)
19710 struct glyph_string *s;
19711 int face_id;
19712 int start, end, overlaps;
19713 {
19714 struct glyph *glyph, *last;
19715 int voffset;
19716 int glyph_not_available_p;
19717
19718 xassert (s->f == XFRAME (s->w->frame));
19719 xassert (s->nchars == 0);
19720 xassert (start >= 0 && end > start);
19721
19722 s->for_overlaps = overlaps;
19723 glyph = s->row->glyphs[s->area] + start;
19724 last = s->row->glyphs[s->area] + end;
19725 voffset = glyph->voffset;
19726 s->padding_p = glyph->padding_p;
19727 glyph_not_available_p = glyph->glyph_not_available_p;
19728
19729 while (glyph < last
19730 && glyph->type == CHAR_GLYPH
19731 && glyph->voffset == voffset
19732 /* Same face id implies same font, nowadays. */
19733 && glyph->face_id == face_id
19734 && glyph->glyph_not_available_p == glyph_not_available_p)
19735 {
19736 int two_byte_p;
19737
19738 s->face = get_glyph_face_and_encoding (s->f, glyph,
19739 s->char2b + s->nchars,
19740 &two_byte_p);
19741 s->two_byte_p = two_byte_p;
19742 ++s->nchars;
19743 xassert (s->nchars <= end - start);
19744 s->width += glyph->pixel_width;
19745 if (glyph++->padding_p != s->padding_p)
19746 break;
19747 }
19748
19749 s->font = s->face->font;
19750
19751 /* If the specified font could not be loaded, use the frame's font,
19752 but record the fact that we couldn't load it in
19753 S->font_not_found_p so that we can draw rectangles for the
19754 characters of the glyph string. */
19755 if (s->font == NULL || glyph_not_available_p)
19756 {
19757 s->font_not_found_p = 1;
19758 s->font = FRAME_FONT (s->f);
19759 }
19760
19761 /* Adjust base line for subscript/superscript text. */
19762 s->ybase += voffset;
19763
19764 xassert (s->face && s->face->gc);
19765 return glyph - s->row->glyphs[s->area];
19766 }
19767
19768
19769 /* Fill glyph string S from image glyph S->first_glyph. */
19770
19771 static void
19772 fill_image_glyph_string (s)
19773 struct glyph_string *s;
19774 {
19775 xassert (s->first_glyph->type == IMAGE_GLYPH);
19776 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19777 xassert (s->img);
19778 s->slice = s->first_glyph->slice;
19779 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19780 s->font = s->face->font;
19781 s->width = s->first_glyph->pixel_width;
19782
19783 /* Adjust base line for subscript/superscript text. */
19784 s->ybase += s->first_glyph->voffset;
19785 }
19786
19787
19788 /* Fill glyph string S from a sequence of stretch glyphs.
19789
19790 ROW is the glyph row in which the glyphs are found, AREA is the
19791 area within the row. START is the index of the first glyph to
19792 consider, END is the index of the last + 1.
19793
19794 Value is the index of the first glyph not in S. */
19795
19796 static int
19797 fill_stretch_glyph_string (s, row, area, start, end)
19798 struct glyph_string *s;
19799 struct glyph_row *row;
19800 enum glyph_row_area area;
19801 int start, end;
19802 {
19803 struct glyph *glyph, *last;
19804 int voffset, face_id;
19805
19806 xassert (s->first_glyph->type == STRETCH_GLYPH);
19807
19808 glyph = s->row->glyphs[s->area] + start;
19809 last = s->row->glyphs[s->area] + end;
19810 face_id = glyph->face_id;
19811 s->face = FACE_FROM_ID (s->f, face_id);
19812 s->font = s->face->font;
19813 s->width = glyph->pixel_width;
19814 s->nchars = 1;
19815 voffset = glyph->voffset;
19816
19817 for (++glyph;
19818 (glyph < last
19819 && glyph->type == STRETCH_GLYPH
19820 && glyph->voffset == voffset
19821 && glyph->face_id == face_id);
19822 ++glyph)
19823 s->width += glyph->pixel_width;
19824
19825 /* Adjust base line for subscript/superscript text. */
19826 s->ybase += voffset;
19827
19828 /* The case that face->gc == 0 is handled when drawing the glyph
19829 string by calling PREPARE_FACE_FOR_DISPLAY. */
19830 xassert (s->face);
19831 return glyph - s->row->glyphs[s->area];
19832 }
19833
19834 static struct font_metrics *
19835 get_per_char_metric (f, font, char2b)
19836 struct frame *f;
19837 struct font *font;
19838 XChar2b *char2b;
19839 {
19840 static struct font_metrics metrics;
19841 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19842
19843 if (! font || code == FONT_INVALID_CODE)
19844 return NULL;
19845 font->driver->text_extents (font, &code, 1, &metrics);
19846 return &metrics;
19847 }
19848
19849 /* EXPORT for RIF:
19850 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19851 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19852 assumed to be zero. */
19853
19854 void
19855 x_get_glyph_overhangs (glyph, f, left, right)
19856 struct glyph *glyph;
19857 struct frame *f;
19858 int *left, *right;
19859 {
19860 *left = *right = 0;
19861
19862 if (glyph->type == CHAR_GLYPH)
19863 {
19864 struct face *face;
19865 XChar2b char2b;
19866 struct font_metrics *pcm;
19867
19868 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19869 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19870 {
19871 if (pcm->rbearing > pcm->width)
19872 *right = pcm->rbearing - pcm->width;
19873 if (pcm->lbearing < 0)
19874 *left = -pcm->lbearing;
19875 }
19876 }
19877 else if (glyph->type == COMPOSITE_GLYPH)
19878 {
19879 if (! glyph->u.cmp.automatic)
19880 {
19881 struct composition *cmp = composition_table[glyph->u.cmp.id];
19882
19883 if (cmp->rbearing > cmp->pixel_width)
19884 *right = cmp->rbearing - cmp->pixel_width;
19885 if (cmp->lbearing < 0)
19886 *left = - cmp->lbearing;
19887 }
19888 else
19889 {
19890 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19891 struct font_metrics metrics;
19892
19893 composition_gstring_width (gstring, glyph->u.cmp.from,
19894 glyph->u.cmp.to + 1, &metrics);
19895 if (metrics.rbearing > metrics.width)
19896 *right = metrics.rbearing - metrics.width;
19897 if (metrics.lbearing < 0)
19898 *left = - metrics.lbearing;
19899 }
19900 }
19901 }
19902
19903
19904 /* Return the index of the first glyph preceding glyph string S that
19905 is overwritten by S because of S's left overhang. Value is -1
19906 if no glyphs are overwritten. */
19907
19908 static int
19909 left_overwritten (s)
19910 struct glyph_string *s;
19911 {
19912 int k;
19913
19914 if (s->left_overhang)
19915 {
19916 int x = 0, i;
19917 struct glyph *glyphs = s->row->glyphs[s->area];
19918 int first = s->first_glyph - glyphs;
19919
19920 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19921 x -= glyphs[i].pixel_width;
19922
19923 k = i + 1;
19924 }
19925 else
19926 k = -1;
19927
19928 return k;
19929 }
19930
19931
19932 /* Return the index of the first glyph preceding glyph string S that
19933 is overwriting S because of its right overhang. Value is -1 if no
19934 glyph in front of S overwrites S. */
19935
19936 static int
19937 left_overwriting (s)
19938 struct glyph_string *s;
19939 {
19940 int i, k, x;
19941 struct glyph *glyphs = s->row->glyphs[s->area];
19942 int first = s->first_glyph - glyphs;
19943
19944 k = -1;
19945 x = 0;
19946 for (i = first - 1; i >= 0; --i)
19947 {
19948 int left, right;
19949 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19950 if (x + right > 0)
19951 k = i;
19952 x -= glyphs[i].pixel_width;
19953 }
19954
19955 return k;
19956 }
19957
19958
19959 /* Return the index of the last glyph following glyph string S that is
19960 overwritten by S because of S's right overhang. Value is -1 if
19961 no such glyph is found. */
19962
19963 static int
19964 right_overwritten (s)
19965 struct glyph_string *s;
19966 {
19967 int k = -1;
19968
19969 if (s->right_overhang)
19970 {
19971 int x = 0, i;
19972 struct glyph *glyphs = s->row->glyphs[s->area];
19973 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19974 int end = s->row->used[s->area];
19975
19976 for (i = first; i < end && s->right_overhang > x; ++i)
19977 x += glyphs[i].pixel_width;
19978
19979 k = i;
19980 }
19981
19982 return k;
19983 }
19984
19985
19986 /* Return the index of the last glyph following glyph string S that
19987 overwrites S because of its left overhang. Value is negative
19988 if no such glyph is found. */
19989
19990 static int
19991 right_overwriting (s)
19992 struct glyph_string *s;
19993 {
19994 int i, k, x;
19995 int end = s->row->used[s->area];
19996 struct glyph *glyphs = s->row->glyphs[s->area];
19997 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19998
19999 k = -1;
20000 x = 0;
20001 for (i = first; i < end; ++i)
20002 {
20003 int left, right;
20004 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20005 if (x - left < 0)
20006 k = i;
20007 x += glyphs[i].pixel_width;
20008 }
20009
20010 return k;
20011 }
20012
20013
20014 /* Set background width of glyph string S. START is the index of the
20015 first glyph following S. LAST_X is the right-most x-position + 1
20016 in the drawing area. */
20017
20018 static INLINE void
20019 set_glyph_string_background_width (s, start, last_x)
20020 struct glyph_string *s;
20021 int start;
20022 int last_x;
20023 {
20024 /* If the face of this glyph string has to be drawn to the end of
20025 the drawing area, set S->extends_to_end_of_line_p. */
20026
20027 if (start == s->row->used[s->area]
20028 && s->area == TEXT_AREA
20029 && ((s->row->fill_line_p
20030 && (s->hl == DRAW_NORMAL_TEXT
20031 || s->hl == DRAW_IMAGE_RAISED
20032 || s->hl == DRAW_IMAGE_SUNKEN))
20033 || s->hl == DRAW_MOUSE_FACE))
20034 s->extends_to_end_of_line_p = 1;
20035
20036 /* If S extends its face to the end of the line, set its
20037 background_width to the distance to the right edge of the drawing
20038 area. */
20039 if (s->extends_to_end_of_line_p)
20040 s->background_width = last_x - s->x + 1;
20041 else
20042 s->background_width = s->width;
20043 }
20044
20045
20046 /* Compute overhangs and x-positions for glyph string S and its
20047 predecessors, or successors. X is the starting x-position for S.
20048 BACKWARD_P non-zero means process predecessors. */
20049
20050 static void
20051 compute_overhangs_and_x (s, x, backward_p)
20052 struct glyph_string *s;
20053 int x;
20054 int backward_p;
20055 {
20056 if (backward_p)
20057 {
20058 while (s)
20059 {
20060 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20061 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20062 x -= s->width;
20063 s->x = x;
20064 s = s->prev;
20065 }
20066 }
20067 else
20068 {
20069 while (s)
20070 {
20071 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20072 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20073 s->x = x;
20074 x += s->width;
20075 s = s->next;
20076 }
20077 }
20078 }
20079
20080
20081
20082 /* The following macros are only called from draw_glyphs below.
20083 They reference the following parameters of that function directly:
20084 `w', `row', `area', and `overlap_p'
20085 as well as the following local variables:
20086 `s', `f', and `hdc' (in W32) */
20087
20088 #ifdef HAVE_NTGUI
20089 /* On W32, silently add local `hdc' variable to argument list of
20090 init_glyph_string. */
20091 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20092 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20093 #else
20094 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20095 init_glyph_string (s, char2b, w, row, area, start, hl)
20096 #endif
20097
20098 /* Add a glyph string for a stretch glyph to the list of strings
20099 between HEAD and TAIL. START is the index of the stretch glyph in
20100 row area AREA of glyph row ROW. END is the index of the last glyph
20101 in that glyph row area. X is the current output position assigned
20102 to the new glyph string constructed. HL overrides that face of the
20103 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20104 is the right-most x-position of the drawing area. */
20105
20106 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20107 and below -- keep them on one line. */
20108 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20109 do \
20110 { \
20111 s = (struct glyph_string *) alloca (sizeof *s); \
20112 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20113 START = fill_stretch_glyph_string (s, row, area, START, END); \
20114 append_glyph_string (&HEAD, &TAIL, s); \
20115 s->x = (X); \
20116 } \
20117 while (0)
20118
20119
20120 /* Add a glyph string for an image glyph to the list of strings
20121 between HEAD and TAIL. START is the index of the image glyph in
20122 row area AREA of glyph row ROW. END is the index of the last glyph
20123 in that glyph row area. X is the current output position assigned
20124 to the new glyph string constructed. HL overrides that face of the
20125 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20126 is the right-most x-position of the drawing area. */
20127
20128 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20129 do \
20130 { \
20131 s = (struct glyph_string *) alloca (sizeof *s); \
20132 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20133 fill_image_glyph_string (s); \
20134 append_glyph_string (&HEAD, &TAIL, s); \
20135 ++START; \
20136 s->x = (X); \
20137 } \
20138 while (0)
20139
20140
20141 /* Add a glyph string for a sequence of character glyphs to the list
20142 of strings between HEAD and TAIL. START is the index of the first
20143 glyph in row area AREA of glyph row ROW that is part of the new
20144 glyph string. END is the index of the last glyph in that glyph row
20145 area. X is the current output position assigned to the new glyph
20146 string constructed. HL overrides that face of the glyph; e.g. it
20147 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20148 right-most x-position of the drawing area. */
20149
20150 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20151 do \
20152 { \
20153 int face_id; \
20154 XChar2b *char2b; \
20155 \
20156 face_id = (row)->glyphs[area][START].face_id; \
20157 \
20158 s = (struct glyph_string *) alloca (sizeof *s); \
20159 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20160 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20161 append_glyph_string (&HEAD, &TAIL, s); \
20162 s->x = (X); \
20163 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20164 } \
20165 while (0)
20166
20167
20168 /* Add a glyph string for a composite sequence to the list of strings
20169 between HEAD and TAIL. START is the index of the first glyph in
20170 row area AREA of glyph row ROW that is part of the new glyph
20171 string. END is the index of the last glyph in that glyph row area.
20172 X is the current output position assigned to the new glyph string
20173 constructed. HL overrides that face of the glyph; e.g. it is
20174 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20175 x-position of the drawing area. */
20176
20177 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20178 do { \
20179 int face_id = (row)->glyphs[area][START].face_id; \
20180 struct face *base_face = FACE_FROM_ID (f, face_id); \
20181 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20182 struct composition *cmp = composition_table[cmp_id]; \
20183 XChar2b *char2b; \
20184 struct glyph_string *first_s; \
20185 int n; \
20186 \
20187 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20188 \
20189 /* Make glyph_strings for each glyph sequence that is drawable by \
20190 the same face, and append them to HEAD/TAIL. */ \
20191 for (n = 0; n < cmp->glyph_len;) \
20192 { \
20193 s = (struct glyph_string *) alloca (sizeof *s); \
20194 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20195 append_glyph_string (&(HEAD), &(TAIL), s); \
20196 s->cmp = cmp; \
20197 s->cmp_from = n; \
20198 s->x = (X); \
20199 if (n == 0) \
20200 first_s = s; \
20201 n = fill_composite_glyph_string (s, base_face, overlaps); \
20202 } \
20203 \
20204 ++START; \
20205 s = first_s; \
20206 } while (0)
20207
20208
20209 /* Add a glyph string for a glyph-string sequence to the list of strings
20210 between HEAD and TAIL. */
20211
20212 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20213 do { \
20214 int face_id; \
20215 XChar2b *char2b; \
20216 Lisp_Object gstring; \
20217 \
20218 face_id = (row)->glyphs[area][START].face_id; \
20219 gstring = (composition_gstring_from_id \
20220 ((row)->glyphs[area][START].u.cmp.id)); \
20221 s = (struct glyph_string *) alloca (sizeof *s); \
20222 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20223 * LGSTRING_GLYPH_LEN (gstring)); \
20224 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20225 append_glyph_string (&(HEAD), &(TAIL), s); \
20226 s->x = (X); \
20227 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20228 } while (0)
20229
20230
20231 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20232 of AREA of glyph row ROW on window W between indices START and END.
20233 HL overrides the face for drawing glyph strings, e.g. it is
20234 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20235 x-positions of the drawing area.
20236
20237 This is an ugly monster macro construct because we must use alloca
20238 to allocate glyph strings (because draw_glyphs can be called
20239 asynchronously). */
20240
20241 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20242 do \
20243 { \
20244 HEAD = TAIL = NULL; \
20245 while (START < END) \
20246 { \
20247 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20248 switch (first_glyph->type) \
20249 { \
20250 case CHAR_GLYPH: \
20251 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20252 HL, X, LAST_X); \
20253 break; \
20254 \
20255 case COMPOSITE_GLYPH: \
20256 if (first_glyph->u.cmp.automatic) \
20257 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20258 HL, X, LAST_X); \
20259 else \
20260 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20261 HL, X, LAST_X); \
20262 break; \
20263 \
20264 case STRETCH_GLYPH: \
20265 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20266 HL, X, LAST_X); \
20267 break; \
20268 \
20269 case IMAGE_GLYPH: \
20270 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20271 HL, X, LAST_X); \
20272 break; \
20273 \
20274 default: \
20275 abort (); \
20276 } \
20277 \
20278 if (s) \
20279 { \
20280 set_glyph_string_background_width (s, START, LAST_X); \
20281 (X) += s->width; \
20282 } \
20283 } \
20284 } while (0)
20285
20286
20287 /* Draw glyphs between START and END in AREA of ROW on window W,
20288 starting at x-position X. X is relative to AREA in W. HL is a
20289 face-override with the following meaning:
20290
20291 DRAW_NORMAL_TEXT draw normally
20292 DRAW_CURSOR draw in cursor face
20293 DRAW_MOUSE_FACE draw in mouse face.
20294 DRAW_INVERSE_VIDEO draw in mode line face
20295 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20296 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20297
20298 If OVERLAPS is non-zero, draw only the foreground of characters and
20299 clip to the physical height of ROW. Non-zero value also defines
20300 the overlapping part to be drawn:
20301
20302 OVERLAPS_PRED overlap with preceding rows
20303 OVERLAPS_SUCC overlap with succeeding rows
20304 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20305 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20306
20307 Value is the x-position reached, relative to AREA of W. */
20308
20309 static int
20310 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20311 struct window *w;
20312 int x;
20313 struct glyph_row *row;
20314 enum glyph_row_area area;
20315 EMACS_INT start, end;
20316 enum draw_glyphs_face hl;
20317 int overlaps;
20318 {
20319 struct glyph_string *head, *tail;
20320 struct glyph_string *s;
20321 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20322 int i, j, x_reached, last_x, area_left = 0;
20323 struct frame *f = XFRAME (WINDOW_FRAME (w));
20324 DECLARE_HDC (hdc);
20325
20326 ALLOCATE_HDC (hdc, f);
20327
20328 /* Let's rather be paranoid than getting a SEGV. */
20329 end = min (end, row->used[area]);
20330 start = max (0, start);
20331 start = min (end, start);
20332
20333 /* Translate X to frame coordinates. Set last_x to the right
20334 end of the drawing area. */
20335 if (row->full_width_p)
20336 {
20337 /* X is relative to the left edge of W, without scroll bars
20338 or fringes. */
20339 area_left = WINDOW_LEFT_EDGE_X (w);
20340 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20341 }
20342 else
20343 {
20344 area_left = window_box_left (w, area);
20345 last_x = area_left + window_box_width (w, area);
20346 }
20347 x += area_left;
20348
20349 /* Build a doubly-linked list of glyph_string structures between
20350 head and tail from what we have to draw. Note that the macro
20351 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20352 the reason we use a separate variable `i'. */
20353 i = start;
20354 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20355 if (tail)
20356 x_reached = tail->x + tail->background_width;
20357 else
20358 x_reached = x;
20359
20360 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20361 the row, redraw some glyphs in front or following the glyph
20362 strings built above. */
20363 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20364 {
20365 struct glyph_string *h, *t;
20366 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20367 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20368 int dummy_x = 0;
20369
20370 /* If mouse highlighting is on, we may need to draw adjacent
20371 glyphs using mouse-face highlighting. */
20372 if (area == TEXT_AREA && row->mouse_face_p)
20373 {
20374 struct glyph_row *mouse_beg_row, *mouse_end_row;
20375
20376 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20377 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20378
20379 if (row >= mouse_beg_row && row <= mouse_end_row)
20380 {
20381 check_mouse_face = 1;
20382 mouse_beg_col = (row == mouse_beg_row)
20383 ? dpyinfo->mouse_face_beg_col : 0;
20384 mouse_end_col = (row == mouse_end_row)
20385 ? dpyinfo->mouse_face_end_col
20386 : row->used[TEXT_AREA];
20387 }
20388 }
20389
20390 /* Compute overhangs for all glyph strings. */
20391 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20392 for (s = head; s; s = s->next)
20393 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20394
20395 /* Prepend glyph strings for glyphs in front of the first glyph
20396 string that are overwritten because of the first glyph
20397 string's left overhang. The background of all strings
20398 prepended must be drawn because the first glyph string
20399 draws over it. */
20400 i = left_overwritten (head);
20401 if (i >= 0)
20402 {
20403 enum draw_glyphs_face overlap_hl;
20404
20405 /* If this row contains mouse highlighting, attempt to draw
20406 the overlapped glyphs with the correct highlight. This
20407 code fails if the overlap encompasses more than one glyph
20408 and mouse-highlight spans only some of these glyphs.
20409 However, making it work perfectly involves a lot more
20410 code, and I don't know if the pathological case occurs in
20411 practice, so we'll stick to this for now. --- cyd */
20412 if (check_mouse_face
20413 && mouse_beg_col < start && mouse_end_col > i)
20414 overlap_hl = DRAW_MOUSE_FACE;
20415 else
20416 overlap_hl = DRAW_NORMAL_TEXT;
20417
20418 j = i;
20419 BUILD_GLYPH_STRINGS (j, start, h, t,
20420 overlap_hl, dummy_x, last_x);
20421 start = i;
20422 compute_overhangs_and_x (t, head->x, 1);
20423 prepend_glyph_string_lists (&head, &tail, h, t);
20424 clip_head = head;
20425 }
20426
20427 /* Prepend glyph strings for glyphs in front of the first glyph
20428 string that overwrite that glyph string because of their
20429 right overhang. For these strings, only the foreground must
20430 be drawn, because it draws over the glyph string at `head'.
20431 The background must not be drawn because this would overwrite
20432 right overhangs of preceding glyphs for which no glyph
20433 strings exist. */
20434 i = left_overwriting (head);
20435 if (i >= 0)
20436 {
20437 enum draw_glyphs_face overlap_hl;
20438
20439 if (check_mouse_face
20440 && mouse_beg_col < start && mouse_end_col > i)
20441 overlap_hl = DRAW_MOUSE_FACE;
20442 else
20443 overlap_hl = DRAW_NORMAL_TEXT;
20444
20445 clip_head = head;
20446 BUILD_GLYPH_STRINGS (i, start, h, t,
20447 overlap_hl, dummy_x, last_x);
20448 for (s = h; s; s = s->next)
20449 s->background_filled_p = 1;
20450 compute_overhangs_and_x (t, head->x, 1);
20451 prepend_glyph_string_lists (&head, &tail, h, t);
20452 }
20453
20454 /* Append glyphs strings for glyphs following the last glyph
20455 string tail that are overwritten by tail. The background of
20456 these strings has to be drawn because tail's foreground draws
20457 over it. */
20458 i = right_overwritten (tail);
20459 if (i >= 0)
20460 {
20461 enum draw_glyphs_face overlap_hl;
20462
20463 if (check_mouse_face
20464 && mouse_beg_col < i && mouse_end_col > end)
20465 overlap_hl = DRAW_MOUSE_FACE;
20466 else
20467 overlap_hl = DRAW_NORMAL_TEXT;
20468
20469 BUILD_GLYPH_STRINGS (end, i, h, t,
20470 overlap_hl, x, last_x);
20471 /* Because BUILD_GLYPH_STRINGS updates the first argument,
20472 we don't have `end = i;' here. */
20473 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20474 append_glyph_string_lists (&head, &tail, h, t);
20475 clip_tail = tail;
20476 }
20477
20478 /* Append glyph strings for glyphs following the last glyph
20479 string tail that overwrite tail. The foreground of such
20480 glyphs has to be drawn because it writes into the background
20481 of tail. The background must not be drawn because it could
20482 paint over the foreground of following glyphs. */
20483 i = right_overwriting (tail);
20484 if (i >= 0)
20485 {
20486 enum draw_glyphs_face overlap_hl;
20487 if (check_mouse_face
20488 && mouse_beg_col < i && mouse_end_col > end)
20489 overlap_hl = DRAW_MOUSE_FACE;
20490 else
20491 overlap_hl = DRAW_NORMAL_TEXT;
20492
20493 clip_tail = tail;
20494 i++; /* We must include the Ith glyph. */
20495 BUILD_GLYPH_STRINGS (end, i, h, t,
20496 overlap_hl, x, last_x);
20497 for (s = h; s; s = s->next)
20498 s->background_filled_p = 1;
20499 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20500 append_glyph_string_lists (&head, &tail, h, t);
20501 }
20502 if (clip_head || clip_tail)
20503 for (s = head; s; s = s->next)
20504 {
20505 s->clip_head = clip_head;
20506 s->clip_tail = clip_tail;
20507 }
20508 }
20509
20510 /* Draw all strings. */
20511 for (s = head; s; s = s->next)
20512 FRAME_RIF (f)->draw_glyph_string (s);
20513
20514 #ifndef HAVE_NS
20515 /* When focus a sole frame and move horizontally, this sets on_p to 0
20516 causing a failure to erase prev cursor position. */
20517 if (area == TEXT_AREA
20518 && !row->full_width_p
20519 /* When drawing overlapping rows, only the glyph strings'
20520 foreground is drawn, which doesn't erase a cursor
20521 completely. */
20522 && !overlaps)
20523 {
20524 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20525 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20526 : (tail ? tail->x + tail->background_width : x));
20527 x0 -= area_left;
20528 x1 -= area_left;
20529
20530 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20531 row->y, MATRIX_ROW_BOTTOM_Y (row));
20532 }
20533 #endif
20534
20535 /* Value is the x-position up to which drawn, relative to AREA of W.
20536 This doesn't include parts drawn because of overhangs. */
20537 if (row->full_width_p)
20538 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20539 else
20540 x_reached -= area_left;
20541
20542 RELEASE_HDC (hdc, f);
20543
20544 return x_reached;
20545 }
20546
20547 /* Expand row matrix if too narrow. Don't expand if area
20548 is not present. */
20549
20550 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20551 { \
20552 if (!fonts_changed_p \
20553 && (it->glyph_row->glyphs[area] \
20554 < it->glyph_row->glyphs[area + 1])) \
20555 { \
20556 it->w->ncols_scale_factor++; \
20557 fonts_changed_p = 1; \
20558 } \
20559 }
20560
20561 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20562 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20563
20564 static INLINE void
20565 append_glyph (it)
20566 struct it *it;
20567 {
20568 struct glyph *glyph;
20569 enum glyph_row_area area = it->area;
20570
20571 xassert (it->glyph_row);
20572 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20573
20574 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20575 if (glyph < it->glyph_row->glyphs[area + 1])
20576 {
20577 glyph->charpos = CHARPOS (it->position);
20578 glyph->object = it->object;
20579 if (it->pixel_width > 0)
20580 {
20581 glyph->pixel_width = it->pixel_width;
20582 glyph->padding_p = 0;
20583 }
20584 else
20585 {
20586 /* Assure at least 1-pixel width. Otherwise, cursor can't
20587 be displayed correctly. */
20588 glyph->pixel_width = 1;
20589 glyph->padding_p = 1;
20590 }
20591 glyph->ascent = it->ascent;
20592 glyph->descent = it->descent;
20593 glyph->voffset = it->voffset;
20594 glyph->type = CHAR_GLYPH;
20595 glyph->avoid_cursor_p = it->avoid_cursor_p;
20596 glyph->multibyte_p = it->multibyte_p;
20597 glyph->left_box_line_p = it->start_of_box_run_p;
20598 glyph->right_box_line_p = it->end_of_box_run_p;
20599 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20600 || it->phys_descent > it->descent);
20601 glyph->glyph_not_available_p = it->glyph_not_available_p;
20602 glyph->face_id = it->face_id;
20603 glyph->u.ch = it->char_to_display;
20604 glyph->slice = null_glyph_slice;
20605 glyph->font_type = FONT_TYPE_UNKNOWN;
20606 ++it->glyph_row->used[area];
20607 }
20608 else
20609 IT_EXPAND_MATRIX_WIDTH (it, area);
20610 }
20611
20612 /* Store one glyph for the composition IT->cmp_it.id in
20613 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20614 non-null. */
20615
20616 static INLINE void
20617 append_composite_glyph (it)
20618 struct it *it;
20619 {
20620 struct glyph *glyph;
20621 enum glyph_row_area area = it->area;
20622
20623 xassert (it->glyph_row);
20624
20625 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20626 if (glyph < it->glyph_row->glyphs[area + 1])
20627 {
20628 glyph->charpos = CHARPOS (it->position);
20629 glyph->object = it->object;
20630 glyph->pixel_width = it->pixel_width;
20631 glyph->ascent = it->ascent;
20632 glyph->descent = it->descent;
20633 glyph->voffset = it->voffset;
20634 glyph->type = COMPOSITE_GLYPH;
20635 if (it->cmp_it.ch < 0)
20636 {
20637 glyph->u.cmp.automatic = 0;
20638 glyph->u.cmp.id = it->cmp_it.id;
20639 }
20640 else
20641 {
20642 glyph->u.cmp.automatic = 1;
20643 glyph->u.cmp.id = it->cmp_it.id;
20644 glyph->u.cmp.from = it->cmp_it.from;
20645 glyph->u.cmp.to = it->cmp_it.to - 1;
20646 }
20647 glyph->avoid_cursor_p = it->avoid_cursor_p;
20648 glyph->multibyte_p = it->multibyte_p;
20649 glyph->left_box_line_p = it->start_of_box_run_p;
20650 glyph->right_box_line_p = it->end_of_box_run_p;
20651 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20652 || it->phys_descent > it->descent);
20653 glyph->padding_p = 0;
20654 glyph->glyph_not_available_p = 0;
20655 glyph->face_id = it->face_id;
20656 glyph->slice = null_glyph_slice;
20657 glyph->font_type = FONT_TYPE_UNKNOWN;
20658 ++it->glyph_row->used[area];
20659 }
20660 else
20661 IT_EXPAND_MATRIX_WIDTH (it, area);
20662 }
20663
20664
20665 /* Change IT->ascent and IT->height according to the setting of
20666 IT->voffset. */
20667
20668 static INLINE void
20669 take_vertical_position_into_account (it)
20670 struct it *it;
20671 {
20672 if (it->voffset)
20673 {
20674 if (it->voffset < 0)
20675 /* Increase the ascent so that we can display the text higher
20676 in the line. */
20677 it->ascent -= it->voffset;
20678 else
20679 /* Increase the descent so that we can display the text lower
20680 in the line. */
20681 it->descent += it->voffset;
20682 }
20683 }
20684
20685
20686 /* Produce glyphs/get display metrics for the image IT is loaded with.
20687 See the description of struct display_iterator in dispextern.h for
20688 an overview of struct display_iterator. */
20689
20690 static void
20691 produce_image_glyph (it)
20692 struct it *it;
20693 {
20694 struct image *img;
20695 struct face *face;
20696 int glyph_ascent, crop;
20697 struct glyph_slice slice;
20698
20699 xassert (it->what == IT_IMAGE);
20700
20701 face = FACE_FROM_ID (it->f, it->face_id);
20702 xassert (face);
20703 /* Make sure X resources of the face is loaded. */
20704 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20705
20706 if (it->image_id < 0)
20707 {
20708 /* Fringe bitmap. */
20709 it->ascent = it->phys_ascent = 0;
20710 it->descent = it->phys_descent = 0;
20711 it->pixel_width = 0;
20712 it->nglyphs = 0;
20713 return;
20714 }
20715
20716 img = IMAGE_FROM_ID (it->f, it->image_id);
20717 xassert (img);
20718 /* Make sure X resources of the image is loaded. */
20719 prepare_image_for_display (it->f, img);
20720
20721 slice.x = slice.y = 0;
20722 slice.width = img->width;
20723 slice.height = img->height;
20724
20725 if (INTEGERP (it->slice.x))
20726 slice.x = XINT (it->slice.x);
20727 else if (FLOATP (it->slice.x))
20728 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20729
20730 if (INTEGERP (it->slice.y))
20731 slice.y = XINT (it->slice.y);
20732 else if (FLOATP (it->slice.y))
20733 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20734
20735 if (INTEGERP (it->slice.width))
20736 slice.width = XINT (it->slice.width);
20737 else if (FLOATP (it->slice.width))
20738 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20739
20740 if (INTEGERP (it->slice.height))
20741 slice.height = XINT (it->slice.height);
20742 else if (FLOATP (it->slice.height))
20743 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20744
20745 if (slice.x >= img->width)
20746 slice.x = img->width;
20747 if (slice.y >= img->height)
20748 slice.y = img->height;
20749 if (slice.x + slice.width >= img->width)
20750 slice.width = img->width - slice.x;
20751 if (slice.y + slice.height > img->height)
20752 slice.height = img->height - slice.y;
20753
20754 if (slice.width == 0 || slice.height == 0)
20755 return;
20756
20757 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20758
20759 it->descent = slice.height - glyph_ascent;
20760 if (slice.y == 0)
20761 it->descent += img->vmargin;
20762 if (slice.y + slice.height == img->height)
20763 it->descent += img->vmargin;
20764 it->phys_descent = it->descent;
20765
20766 it->pixel_width = slice.width;
20767 if (slice.x == 0)
20768 it->pixel_width += img->hmargin;
20769 if (slice.x + slice.width == img->width)
20770 it->pixel_width += img->hmargin;
20771
20772 /* It's quite possible for images to have an ascent greater than
20773 their height, so don't get confused in that case. */
20774 if (it->descent < 0)
20775 it->descent = 0;
20776
20777 it->nglyphs = 1;
20778
20779 if (face->box != FACE_NO_BOX)
20780 {
20781 if (face->box_line_width > 0)
20782 {
20783 if (slice.y == 0)
20784 it->ascent += face->box_line_width;
20785 if (slice.y + slice.height == img->height)
20786 it->descent += face->box_line_width;
20787 }
20788
20789 if (it->start_of_box_run_p && slice.x == 0)
20790 it->pixel_width += eabs (face->box_line_width);
20791 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20792 it->pixel_width += eabs (face->box_line_width);
20793 }
20794
20795 take_vertical_position_into_account (it);
20796
20797 /* Automatically crop wide image glyphs at right edge so we can
20798 draw the cursor on same display row. */
20799 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20800 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20801 {
20802 it->pixel_width -= crop;
20803 slice.width -= crop;
20804 }
20805
20806 if (it->glyph_row)
20807 {
20808 struct glyph *glyph;
20809 enum glyph_row_area area = it->area;
20810
20811 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20812 if (glyph < it->glyph_row->glyphs[area + 1])
20813 {
20814 glyph->charpos = CHARPOS (it->position);
20815 glyph->object = it->object;
20816 glyph->pixel_width = it->pixel_width;
20817 glyph->ascent = glyph_ascent;
20818 glyph->descent = it->descent;
20819 glyph->voffset = it->voffset;
20820 glyph->type = IMAGE_GLYPH;
20821 glyph->avoid_cursor_p = it->avoid_cursor_p;
20822 glyph->multibyte_p = it->multibyte_p;
20823 glyph->left_box_line_p = it->start_of_box_run_p;
20824 glyph->right_box_line_p = it->end_of_box_run_p;
20825 glyph->overlaps_vertically_p = 0;
20826 glyph->padding_p = 0;
20827 glyph->glyph_not_available_p = 0;
20828 glyph->face_id = it->face_id;
20829 glyph->u.img_id = img->id;
20830 glyph->slice = slice;
20831 glyph->font_type = FONT_TYPE_UNKNOWN;
20832 ++it->glyph_row->used[area];
20833 }
20834 else
20835 IT_EXPAND_MATRIX_WIDTH (it, area);
20836 }
20837 }
20838
20839
20840 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20841 of the glyph, WIDTH and HEIGHT are the width and height of the
20842 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20843
20844 static void
20845 append_stretch_glyph (it, object, width, height, ascent)
20846 struct it *it;
20847 Lisp_Object object;
20848 int width, height;
20849 int ascent;
20850 {
20851 struct glyph *glyph;
20852 enum glyph_row_area area = it->area;
20853
20854 xassert (ascent >= 0 && ascent <= height);
20855
20856 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20857 if (glyph < it->glyph_row->glyphs[area + 1])
20858 {
20859 glyph->charpos = CHARPOS (it->position);
20860 glyph->object = object;
20861 glyph->pixel_width = width;
20862 glyph->ascent = ascent;
20863 glyph->descent = height - ascent;
20864 glyph->voffset = it->voffset;
20865 glyph->type = STRETCH_GLYPH;
20866 glyph->avoid_cursor_p = it->avoid_cursor_p;
20867 glyph->multibyte_p = it->multibyte_p;
20868 glyph->left_box_line_p = it->start_of_box_run_p;
20869 glyph->right_box_line_p = it->end_of_box_run_p;
20870 glyph->overlaps_vertically_p = 0;
20871 glyph->padding_p = 0;
20872 glyph->glyph_not_available_p = 0;
20873 glyph->face_id = it->face_id;
20874 glyph->u.stretch.ascent = ascent;
20875 glyph->u.stretch.height = height;
20876 glyph->slice = null_glyph_slice;
20877 glyph->font_type = FONT_TYPE_UNKNOWN;
20878 ++it->glyph_row->used[area];
20879 }
20880 else
20881 IT_EXPAND_MATRIX_WIDTH (it, area);
20882 }
20883
20884
20885 /* Produce a stretch glyph for iterator IT. IT->object is the value
20886 of the glyph property displayed. The value must be a list
20887 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20888 being recognized:
20889
20890 1. `:width WIDTH' specifies that the space should be WIDTH *
20891 canonical char width wide. WIDTH may be an integer or floating
20892 point number.
20893
20894 2. `:relative-width FACTOR' specifies that the width of the stretch
20895 should be computed from the width of the first character having the
20896 `glyph' property, and should be FACTOR times that width.
20897
20898 3. `:align-to HPOS' specifies that the space should be wide enough
20899 to reach HPOS, a value in canonical character units.
20900
20901 Exactly one of the above pairs must be present.
20902
20903 4. `:height HEIGHT' specifies that the height of the stretch produced
20904 should be HEIGHT, measured in canonical character units.
20905
20906 5. `:relative-height FACTOR' specifies that the height of the
20907 stretch should be FACTOR times the height of the characters having
20908 the glyph property.
20909
20910 Either none or exactly one of 4 or 5 must be present.
20911
20912 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20913 of the stretch should be used for the ascent of the stretch.
20914 ASCENT must be in the range 0 <= ASCENT <= 100. */
20915
20916 static void
20917 produce_stretch_glyph (it)
20918 struct it *it;
20919 {
20920 /* (space :width WIDTH :height HEIGHT ...) */
20921 Lisp_Object prop, plist;
20922 int width = 0, height = 0, align_to = -1;
20923 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20924 int ascent = 0;
20925 double tem;
20926 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20927 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20928
20929 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20930
20931 /* List should start with `space'. */
20932 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20933 plist = XCDR (it->object);
20934
20935 /* Compute the width of the stretch. */
20936 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20937 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20938 {
20939 /* Absolute width `:width WIDTH' specified and valid. */
20940 zero_width_ok_p = 1;
20941 width = (int)tem;
20942 }
20943 else if (prop = Fplist_get (plist, QCrelative_width),
20944 NUMVAL (prop) > 0)
20945 {
20946 /* Relative width `:relative-width FACTOR' specified and valid.
20947 Compute the width of the characters having the `glyph'
20948 property. */
20949 struct it it2;
20950 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20951
20952 it2 = *it;
20953 if (it->multibyte_p)
20954 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
20955 else
20956 {
20957 it2.c = it2.char_to_display = *p, it2.len = 1;
20958 if (! ASCII_CHAR_P (it2.c))
20959 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
20960 }
20961
20962 it2.glyph_row = NULL;
20963 it2.what = IT_CHARACTER;
20964 x_produce_glyphs (&it2);
20965 width = NUMVAL (prop) * it2.pixel_width;
20966 }
20967 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20968 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20969 {
20970 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20971 align_to = (align_to < 0
20972 ? 0
20973 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20974 else if (align_to < 0)
20975 align_to = window_box_left_offset (it->w, TEXT_AREA);
20976 width = max (0, (int)tem + align_to - it->current_x);
20977 zero_width_ok_p = 1;
20978 }
20979 else
20980 /* Nothing specified -> width defaults to canonical char width. */
20981 width = FRAME_COLUMN_WIDTH (it->f);
20982
20983 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20984 width = 1;
20985
20986 /* Compute height. */
20987 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20988 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20989 {
20990 height = (int)tem;
20991 zero_height_ok_p = 1;
20992 }
20993 else if (prop = Fplist_get (plist, QCrelative_height),
20994 NUMVAL (prop) > 0)
20995 height = FONT_HEIGHT (font) * NUMVAL (prop);
20996 else
20997 height = FONT_HEIGHT (font);
20998
20999 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21000 height = 1;
21001
21002 /* Compute percentage of height used for ascent. If
21003 `:ascent ASCENT' is present and valid, use that. Otherwise,
21004 derive the ascent from the font in use. */
21005 if (prop = Fplist_get (plist, QCascent),
21006 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21007 ascent = height * NUMVAL (prop) / 100.0;
21008 else if (!NILP (prop)
21009 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21010 ascent = min (max (0, (int)tem), height);
21011 else
21012 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21013
21014 if (width > 0 && it->line_wrap != TRUNCATE
21015 && it->current_x + width > it->last_visible_x)
21016 width = it->last_visible_x - it->current_x - 1;
21017
21018 if (width > 0 && height > 0 && it->glyph_row)
21019 {
21020 Lisp_Object object = it->stack[it->sp - 1].string;
21021 if (!STRINGP (object))
21022 object = it->w->buffer;
21023 append_stretch_glyph (it, object, width, height, ascent);
21024 }
21025
21026 it->pixel_width = width;
21027 it->ascent = it->phys_ascent = ascent;
21028 it->descent = it->phys_descent = height - it->ascent;
21029 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21030
21031 take_vertical_position_into_account (it);
21032 }
21033
21034 /* Calculate line-height and line-spacing properties.
21035 An integer value specifies explicit pixel value.
21036 A float value specifies relative value to current face height.
21037 A cons (float . face-name) specifies relative value to
21038 height of specified face font.
21039
21040 Returns height in pixels, or nil. */
21041
21042
21043 static Lisp_Object
21044 calc_line_height_property (it, val, font, boff, override)
21045 struct it *it;
21046 Lisp_Object val;
21047 struct font *font;
21048 int boff, override;
21049 {
21050 Lisp_Object face_name = Qnil;
21051 int ascent, descent, height;
21052
21053 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21054 return val;
21055
21056 if (CONSP (val))
21057 {
21058 face_name = XCAR (val);
21059 val = XCDR (val);
21060 if (!NUMBERP (val))
21061 val = make_number (1);
21062 if (NILP (face_name))
21063 {
21064 height = it->ascent + it->descent;
21065 goto scale;
21066 }
21067 }
21068
21069 if (NILP (face_name))
21070 {
21071 font = FRAME_FONT (it->f);
21072 boff = FRAME_BASELINE_OFFSET (it->f);
21073 }
21074 else if (EQ (face_name, Qt))
21075 {
21076 override = 0;
21077 }
21078 else
21079 {
21080 int face_id;
21081 struct face *face;
21082
21083 face_id = lookup_named_face (it->f, face_name, 0);
21084 if (face_id < 0)
21085 return make_number (-1);
21086
21087 face = FACE_FROM_ID (it->f, face_id);
21088 font = face->font;
21089 if (font == NULL)
21090 return make_number (-1);
21091 boff = font->baseline_offset;
21092 if (font->vertical_centering)
21093 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21094 }
21095
21096 ascent = FONT_BASE (font) + boff;
21097 descent = FONT_DESCENT (font) - boff;
21098
21099 if (override)
21100 {
21101 it->override_ascent = ascent;
21102 it->override_descent = descent;
21103 it->override_boff = boff;
21104 }
21105
21106 height = ascent + descent;
21107
21108 scale:
21109 if (FLOATP (val))
21110 height = (int)(XFLOAT_DATA (val) * height);
21111 else if (INTEGERP (val))
21112 height *= XINT (val);
21113
21114 return make_number (height);
21115 }
21116
21117
21118 /* RIF:
21119 Produce glyphs/get display metrics for the display element IT is
21120 loaded with. See the description of struct it in dispextern.h
21121 for an overview of struct it. */
21122
21123 void
21124 x_produce_glyphs (it)
21125 struct it *it;
21126 {
21127 int extra_line_spacing = it->extra_line_spacing;
21128
21129 it->glyph_not_available_p = 0;
21130
21131 if (it->what == IT_CHARACTER)
21132 {
21133 XChar2b char2b;
21134 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21135 struct font *font = face->font;
21136 int font_not_found_p = font == NULL;
21137 struct font_metrics *pcm = NULL;
21138 int boff; /* baseline offset */
21139
21140 if (font_not_found_p)
21141 {
21142 /* When no suitable font found, display an empty box based
21143 on the metrics of the font of the default face (or what
21144 remapped). */
21145 struct face *no_font_face
21146 = FACE_FROM_ID (it->f,
21147 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21148 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21149 font = no_font_face->font;
21150 boff = font->baseline_offset;
21151 }
21152 else
21153 {
21154 boff = font->baseline_offset;
21155 if (font->vertical_centering)
21156 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21157 }
21158
21159 if (it->char_to_display != '\n' && it->char_to_display != '\t')
21160 {
21161 int stretched_p;
21162
21163 it->nglyphs = 1;
21164
21165 if (it->override_ascent >= 0)
21166 {
21167 it->ascent = it->override_ascent;
21168 it->descent = it->override_descent;
21169 boff = it->override_boff;
21170 }
21171 else
21172 {
21173 it->ascent = FONT_BASE (font) + boff;
21174 it->descent = FONT_DESCENT (font) - boff;
21175 }
21176
21177 if (! font_not_found_p
21178 && get_char_glyph_code (it->char_to_display, font, &char2b))
21179 {
21180 pcm = get_per_char_metric (it->f, font, &char2b);
21181 if (pcm->width == 0
21182 && pcm->rbearing == 0 && pcm->lbearing == 0)
21183 pcm = NULL;
21184 }
21185
21186 if (pcm)
21187 {
21188 it->phys_ascent = pcm->ascent + boff;
21189 it->phys_descent = pcm->descent - boff;
21190 it->pixel_width = pcm->width;
21191 }
21192 else
21193 {
21194 it->glyph_not_available_p = 1;
21195 it->phys_ascent = it->ascent;
21196 it->phys_descent = it->descent;
21197 it->pixel_width = font->space_width;
21198 }
21199
21200 if (it->constrain_row_ascent_descent_p)
21201 {
21202 if (it->descent > it->max_descent)
21203 {
21204 it->ascent += it->descent - it->max_descent;
21205 it->descent = it->max_descent;
21206 }
21207 if (it->ascent > it->max_ascent)
21208 {
21209 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21210 it->ascent = it->max_ascent;
21211 }
21212 it->phys_ascent = min (it->phys_ascent, it->ascent);
21213 it->phys_descent = min (it->phys_descent, it->descent);
21214 extra_line_spacing = 0;
21215 }
21216
21217 /* If this is a space inside a region of text with
21218 `space-width' property, change its width. */
21219 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21220 if (stretched_p)
21221 it->pixel_width *= XFLOATINT (it->space_width);
21222
21223 /* If face has a box, add the box thickness to the character
21224 height. If character has a box line to the left and/or
21225 right, add the box line width to the character's width. */
21226 if (face->box != FACE_NO_BOX)
21227 {
21228 int thick = face->box_line_width;
21229
21230 if (thick > 0)
21231 {
21232 it->ascent += thick;
21233 it->descent += thick;
21234 }
21235 else
21236 thick = -thick;
21237
21238 if (it->start_of_box_run_p)
21239 it->pixel_width += thick;
21240 if (it->end_of_box_run_p)
21241 it->pixel_width += thick;
21242 }
21243
21244 /* If face has an overline, add the height of the overline
21245 (1 pixel) and a 1 pixel margin to the character height. */
21246 if (face->overline_p)
21247 it->ascent += overline_margin;
21248
21249 if (it->constrain_row_ascent_descent_p)
21250 {
21251 if (it->ascent > it->max_ascent)
21252 it->ascent = it->max_ascent;
21253 if (it->descent > it->max_descent)
21254 it->descent = it->max_descent;
21255 }
21256
21257 take_vertical_position_into_account (it);
21258
21259 /* If we have to actually produce glyphs, do it. */
21260 if (it->glyph_row)
21261 {
21262 if (stretched_p)
21263 {
21264 /* Translate a space with a `space-width' property
21265 into a stretch glyph. */
21266 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21267 / FONT_HEIGHT (font));
21268 append_stretch_glyph (it, it->object, it->pixel_width,
21269 it->ascent + it->descent, ascent);
21270 }
21271 else
21272 append_glyph (it);
21273
21274 /* If characters with lbearing or rbearing are displayed
21275 in this line, record that fact in a flag of the
21276 glyph row. This is used to optimize X output code. */
21277 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21278 it->glyph_row->contains_overlapping_glyphs_p = 1;
21279 }
21280 if (! stretched_p && it->pixel_width == 0)
21281 /* We assure that all visible glyphs have at least 1-pixel
21282 width. */
21283 it->pixel_width = 1;
21284 }
21285 else if (it->char_to_display == '\n')
21286 {
21287 /* A newline has no width, but we need the height of the
21288 line. But if previous part of the line sets a height,
21289 don't increase that height */
21290
21291 Lisp_Object height;
21292 Lisp_Object total_height = Qnil;
21293
21294 it->override_ascent = -1;
21295 it->pixel_width = 0;
21296 it->nglyphs = 0;
21297
21298 height = get_it_property(it, Qline_height);
21299 /* Split (line-height total-height) list */
21300 if (CONSP (height)
21301 && CONSP (XCDR (height))
21302 && NILP (XCDR (XCDR (height))))
21303 {
21304 total_height = XCAR (XCDR (height));
21305 height = XCAR (height);
21306 }
21307 height = calc_line_height_property(it, height, font, boff, 1);
21308
21309 if (it->override_ascent >= 0)
21310 {
21311 it->ascent = it->override_ascent;
21312 it->descent = it->override_descent;
21313 boff = it->override_boff;
21314 }
21315 else
21316 {
21317 it->ascent = FONT_BASE (font) + boff;
21318 it->descent = FONT_DESCENT (font) - boff;
21319 }
21320
21321 if (EQ (height, Qt))
21322 {
21323 if (it->descent > it->max_descent)
21324 {
21325 it->ascent += it->descent - it->max_descent;
21326 it->descent = it->max_descent;
21327 }
21328 if (it->ascent > it->max_ascent)
21329 {
21330 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21331 it->ascent = it->max_ascent;
21332 }
21333 it->phys_ascent = min (it->phys_ascent, it->ascent);
21334 it->phys_descent = min (it->phys_descent, it->descent);
21335 it->constrain_row_ascent_descent_p = 1;
21336 extra_line_spacing = 0;
21337 }
21338 else
21339 {
21340 Lisp_Object spacing;
21341
21342 it->phys_ascent = it->ascent;
21343 it->phys_descent = it->descent;
21344
21345 if ((it->max_ascent > 0 || it->max_descent > 0)
21346 && face->box != FACE_NO_BOX
21347 && face->box_line_width > 0)
21348 {
21349 it->ascent += face->box_line_width;
21350 it->descent += face->box_line_width;
21351 }
21352 if (!NILP (height)
21353 && XINT (height) > it->ascent + it->descent)
21354 it->ascent = XINT (height) - it->descent;
21355
21356 if (!NILP (total_height))
21357 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21358 else
21359 {
21360 spacing = get_it_property(it, Qline_spacing);
21361 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21362 }
21363 if (INTEGERP (spacing))
21364 {
21365 extra_line_spacing = XINT (spacing);
21366 if (!NILP (total_height))
21367 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21368 }
21369 }
21370 }
21371 else /* i.e. (it->char_to_display == '\t') */
21372 {
21373 if (font->space_width > 0)
21374 {
21375 int tab_width = it->tab_width * font->space_width;
21376 int x = it->current_x + it->continuation_lines_width;
21377 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21378
21379 /* If the distance from the current position to the next tab
21380 stop is less than a space character width, use the
21381 tab stop after that. */
21382 if (next_tab_x - x < font->space_width)
21383 next_tab_x += tab_width;
21384
21385 it->pixel_width = next_tab_x - x;
21386 it->nglyphs = 1;
21387 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21388 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21389
21390 if (it->glyph_row)
21391 {
21392 append_stretch_glyph (it, it->object, it->pixel_width,
21393 it->ascent + it->descent, it->ascent);
21394 }
21395 }
21396 else
21397 {
21398 it->pixel_width = 0;
21399 it->nglyphs = 1;
21400 }
21401 }
21402 }
21403 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21404 {
21405 /* A static composition.
21406
21407 Note: A composition is represented as one glyph in the
21408 glyph matrix. There are no padding glyphs.
21409
21410 Important note: pixel_width, ascent, and descent are the
21411 values of what is drawn by draw_glyphs (i.e. the values of
21412 the overall glyphs composed). */
21413 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21414 int boff; /* baseline offset */
21415 struct composition *cmp = composition_table[it->cmp_it.id];
21416 int glyph_len = cmp->glyph_len;
21417 struct font *font = face->font;
21418
21419 it->nglyphs = 1;
21420
21421 /* If we have not yet calculated pixel size data of glyphs of
21422 the composition for the current face font, calculate them
21423 now. Theoretically, we have to check all fonts for the
21424 glyphs, but that requires much time and memory space. So,
21425 here we check only the font of the first glyph. This may
21426 lead to incorrect display, but it's very rare, and C-l
21427 (recenter-top-bottom) can correct the display anyway. */
21428 if (! cmp->font || cmp->font != font)
21429 {
21430 /* Ascent and descent of the font of the first character
21431 of this composition (adjusted by baseline offset).
21432 Ascent and descent of overall glyphs should not be less
21433 than these, respectively. */
21434 int font_ascent, font_descent, font_height;
21435 /* Bounding box of the overall glyphs. */
21436 int leftmost, rightmost, lowest, highest;
21437 int lbearing, rbearing;
21438 int i, width, ascent, descent;
21439 int left_padded = 0, right_padded = 0;
21440 int c;
21441 XChar2b char2b;
21442 struct font_metrics *pcm;
21443 int font_not_found_p;
21444 int pos;
21445
21446 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21447 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21448 break;
21449 if (glyph_len < cmp->glyph_len)
21450 right_padded = 1;
21451 for (i = 0; i < glyph_len; i++)
21452 {
21453 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21454 break;
21455 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21456 }
21457 if (i > 0)
21458 left_padded = 1;
21459
21460 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21461 : IT_CHARPOS (*it));
21462 /* If no suitable font is found, use the default font. */
21463 font_not_found_p = font == NULL;
21464 if (font_not_found_p)
21465 {
21466 face = face->ascii_face;
21467 font = face->font;
21468 }
21469 boff = font->baseline_offset;
21470 if (font->vertical_centering)
21471 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21472 font_ascent = FONT_BASE (font) + boff;
21473 font_descent = FONT_DESCENT (font) - boff;
21474 font_height = FONT_HEIGHT (font);
21475
21476 cmp->font = (void *) font;
21477
21478 pcm = NULL;
21479 if (! font_not_found_p)
21480 {
21481 get_char_face_and_encoding (it->f, c, it->face_id,
21482 &char2b, it->multibyte_p, 0);
21483 pcm = get_per_char_metric (it->f, font, &char2b);
21484 }
21485
21486 /* Initialize the bounding box. */
21487 if (pcm)
21488 {
21489 width = pcm->width;
21490 ascent = pcm->ascent;
21491 descent = pcm->descent;
21492 lbearing = pcm->lbearing;
21493 rbearing = pcm->rbearing;
21494 }
21495 else
21496 {
21497 width = font->space_width;
21498 ascent = FONT_BASE (font);
21499 descent = FONT_DESCENT (font);
21500 lbearing = 0;
21501 rbearing = width;
21502 }
21503
21504 rightmost = width;
21505 leftmost = 0;
21506 lowest = - descent + boff;
21507 highest = ascent + boff;
21508
21509 if (! font_not_found_p
21510 && font->default_ascent
21511 && CHAR_TABLE_P (Vuse_default_ascent)
21512 && !NILP (Faref (Vuse_default_ascent,
21513 make_number (it->char_to_display))))
21514 highest = font->default_ascent + boff;
21515
21516 /* Draw the first glyph at the normal position. It may be
21517 shifted to right later if some other glyphs are drawn
21518 at the left. */
21519 cmp->offsets[i * 2] = 0;
21520 cmp->offsets[i * 2 + 1] = boff;
21521 cmp->lbearing = lbearing;
21522 cmp->rbearing = rbearing;
21523
21524 /* Set cmp->offsets for the remaining glyphs. */
21525 for (i++; i < glyph_len; i++)
21526 {
21527 int left, right, btm, top;
21528 int ch = COMPOSITION_GLYPH (cmp, i);
21529 int face_id;
21530 struct face *this_face;
21531 int this_boff;
21532
21533 if (ch == '\t')
21534 ch = ' ';
21535 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21536 this_face = FACE_FROM_ID (it->f, face_id);
21537 font = this_face->font;
21538
21539 if (font == NULL)
21540 pcm = NULL;
21541 else
21542 {
21543 this_boff = font->baseline_offset;
21544 if (font->vertical_centering)
21545 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21546 get_char_face_and_encoding (it->f, ch, face_id,
21547 &char2b, it->multibyte_p, 0);
21548 pcm = get_per_char_metric (it->f, font, &char2b);
21549 }
21550 if (! pcm)
21551 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21552 else
21553 {
21554 width = pcm->width;
21555 ascent = pcm->ascent;
21556 descent = pcm->descent;
21557 lbearing = pcm->lbearing;
21558 rbearing = pcm->rbearing;
21559 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21560 {
21561 /* Relative composition with or without
21562 alternate chars. */
21563 left = (leftmost + rightmost - width) / 2;
21564 btm = - descent + boff;
21565 if (font->relative_compose
21566 && (! CHAR_TABLE_P (Vignore_relative_composition)
21567 || NILP (Faref (Vignore_relative_composition,
21568 make_number (ch)))))
21569 {
21570
21571 if (- descent >= font->relative_compose)
21572 /* One extra pixel between two glyphs. */
21573 btm = highest + 1;
21574 else if (ascent <= 0)
21575 /* One extra pixel between two glyphs. */
21576 btm = lowest - 1 - ascent - descent;
21577 }
21578 }
21579 else
21580 {
21581 /* A composition rule is specified by an integer
21582 value that encodes global and new reference
21583 points (GREF and NREF). GREF and NREF are
21584 specified by numbers as below:
21585
21586 0---1---2 -- ascent
21587 | |
21588 | |
21589 | |
21590 9--10--11 -- center
21591 | |
21592 ---3---4---5--- baseline
21593 | |
21594 6---7---8 -- descent
21595 */
21596 int rule = COMPOSITION_RULE (cmp, i);
21597 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21598
21599 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21600 grefx = gref % 3, nrefx = nref % 3;
21601 grefy = gref / 3, nrefy = nref / 3;
21602 if (xoff)
21603 xoff = font_height * (xoff - 128) / 256;
21604 if (yoff)
21605 yoff = font_height * (yoff - 128) / 256;
21606
21607 left = (leftmost
21608 + grefx * (rightmost - leftmost) / 2
21609 - nrefx * width / 2
21610 + xoff);
21611
21612 btm = ((grefy == 0 ? highest
21613 : grefy == 1 ? 0
21614 : grefy == 2 ? lowest
21615 : (highest + lowest) / 2)
21616 - (nrefy == 0 ? ascent + descent
21617 : nrefy == 1 ? descent - boff
21618 : nrefy == 2 ? 0
21619 : (ascent + descent) / 2)
21620 + yoff);
21621 }
21622
21623 cmp->offsets[i * 2] = left;
21624 cmp->offsets[i * 2 + 1] = btm + descent;
21625
21626 /* Update the bounding box of the overall glyphs. */
21627 if (width > 0)
21628 {
21629 right = left + width;
21630 if (left < leftmost)
21631 leftmost = left;
21632 if (right > rightmost)
21633 rightmost = right;
21634 }
21635 top = btm + descent + ascent;
21636 if (top > highest)
21637 highest = top;
21638 if (btm < lowest)
21639 lowest = btm;
21640
21641 if (cmp->lbearing > left + lbearing)
21642 cmp->lbearing = left + lbearing;
21643 if (cmp->rbearing < left + rbearing)
21644 cmp->rbearing = left + rbearing;
21645 }
21646 }
21647
21648 /* If there are glyphs whose x-offsets are negative,
21649 shift all glyphs to the right and make all x-offsets
21650 non-negative. */
21651 if (leftmost < 0)
21652 {
21653 for (i = 0; i < cmp->glyph_len; i++)
21654 cmp->offsets[i * 2] -= leftmost;
21655 rightmost -= leftmost;
21656 cmp->lbearing -= leftmost;
21657 cmp->rbearing -= leftmost;
21658 }
21659
21660 if (left_padded && cmp->lbearing < 0)
21661 {
21662 for (i = 0; i < cmp->glyph_len; i++)
21663 cmp->offsets[i * 2] -= cmp->lbearing;
21664 rightmost -= cmp->lbearing;
21665 cmp->rbearing -= cmp->lbearing;
21666 cmp->lbearing = 0;
21667 }
21668 if (right_padded && rightmost < cmp->rbearing)
21669 {
21670 rightmost = cmp->rbearing;
21671 }
21672
21673 cmp->pixel_width = rightmost;
21674 cmp->ascent = highest;
21675 cmp->descent = - lowest;
21676 if (cmp->ascent < font_ascent)
21677 cmp->ascent = font_ascent;
21678 if (cmp->descent < font_descent)
21679 cmp->descent = font_descent;
21680 }
21681
21682 if (it->glyph_row
21683 && (cmp->lbearing < 0
21684 || cmp->rbearing > cmp->pixel_width))
21685 it->glyph_row->contains_overlapping_glyphs_p = 1;
21686
21687 it->pixel_width = cmp->pixel_width;
21688 it->ascent = it->phys_ascent = cmp->ascent;
21689 it->descent = it->phys_descent = cmp->descent;
21690 if (face->box != FACE_NO_BOX)
21691 {
21692 int thick = face->box_line_width;
21693
21694 if (thick > 0)
21695 {
21696 it->ascent += thick;
21697 it->descent += thick;
21698 }
21699 else
21700 thick = - thick;
21701
21702 if (it->start_of_box_run_p)
21703 it->pixel_width += thick;
21704 if (it->end_of_box_run_p)
21705 it->pixel_width += thick;
21706 }
21707
21708 /* If face has an overline, add the height of the overline
21709 (1 pixel) and a 1 pixel margin to the character height. */
21710 if (face->overline_p)
21711 it->ascent += overline_margin;
21712
21713 take_vertical_position_into_account (it);
21714 if (it->ascent < 0)
21715 it->ascent = 0;
21716 if (it->descent < 0)
21717 it->descent = 0;
21718
21719 if (it->glyph_row)
21720 append_composite_glyph (it);
21721 }
21722 else if (it->what == IT_COMPOSITION)
21723 {
21724 /* A dynamic (automatic) composition. */
21725 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21726 Lisp_Object gstring;
21727 struct font_metrics metrics;
21728
21729 gstring = composition_gstring_from_id (it->cmp_it.id);
21730 it->pixel_width
21731 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21732 &metrics);
21733 if (it->glyph_row
21734 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21735 it->glyph_row->contains_overlapping_glyphs_p = 1;
21736 it->ascent = it->phys_ascent = metrics.ascent;
21737 it->descent = it->phys_descent = metrics.descent;
21738 if (face->box != FACE_NO_BOX)
21739 {
21740 int thick = face->box_line_width;
21741
21742 if (thick > 0)
21743 {
21744 it->ascent += thick;
21745 it->descent += thick;
21746 }
21747 else
21748 thick = - thick;
21749
21750 if (it->start_of_box_run_p)
21751 it->pixel_width += thick;
21752 if (it->end_of_box_run_p)
21753 it->pixel_width += thick;
21754 }
21755 /* If face has an overline, add the height of the overline
21756 (1 pixel) and a 1 pixel margin to the character height. */
21757 if (face->overline_p)
21758 it->ascent += overline_margin;
21759 take_vertical_position_into_account (it);
21760 if (it->ascent < 0)
21761 it->ascent = 0;
21762 if (it->descent < 0)
21763 it->descent = 0;
21764
21765 if (it->glyph_row)
21766 append_composite_glyph (it);
21767 }
21768 else if (it->what == IT_IMAGE)
21769 produce_image_glyph (it);
21770 else if (it->what == IT_STRETCH)
21771 produce_stretch_glyph (it);
21772
21773 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21774 because this isn't true for images with `:ascent 100'. */
21775 xassert (it->ascent >= 0 && it->descent >= 0);
21776 if (it->area == TEXT_AREA)
21777 it->current_x += it->pixel_width;
21778
21779 if (extra_line_spacing > 0)
21780 {
21781 it->descent += extra_line_spacing;
21782 if (extra_line_spacing > it->max_extra_line_spacing)
21783 it->max_extra_line_spacing = extra_line_spacing;
21784 }
21785
21786 it->max_ascent = max (it->max_ascent, it->ascent);
21787 it->max_descent = max (it->max_descent, it->descent);
21788 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21789 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21790 }
21791
21792 /* EXPORT for RIF:
21793 Output LEN glyphs starting at START at the nominal cursor position.
21794 Advance the nominal cursor over the text. The global variable
21795 updated_window contains the window being updated, updated_row is
21796 the glyph row being updated, and updated_area is the area of that
21797 row being updated. */
21798
21799 void
21800 x_write_glyphs (start, len)
21801 struct glyph *start;
21802 int len;
21803 {
21804 int x, hpos;
21805
21806 xassert (updated_window && updated_row);
21807 BLOCK_INPUT;
21808
21809 /* Write glyphs. */
21810
21811 hpos = start - updated_row->glyphs[updated_area];
21812 x = draw_glyphs (updated_window, output_cursor.x,
21813 updated_row, updated_area,
21814 hpos, hpos + len,
21815 DRAW_NORMAL_TEXT, 0);
21816
21817 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21818 if (updated_area == TEXT_AREA
21819 && updated_window->phys_cursor_on_p
21820 && updated_window->phys_cursor.vpos == output_cursor.vpos
21821 && updated_window->phys_cursor.hpos >= hpos
21822 && updated_window->phys_cursor.hpos < hpos + len)
21823 updated_window->phys_cursor_on_p = 0;
21824
21825 UNBLOCK_INPUT;
21826
21827 /* Advance the output cursor. */
21828 output_cursor.hpos += len;
21829 output_cursor.x = x;
21830 }
21831
21832
21833 /* EXPORT for RIF:
21834 Insert LEN glyphs from START at the nominal cursor position. */
21835
21836 void
21837 x_insert_glyphs (start, len)
21838 struct glyph *start;
21839 int len;
21840 {
21841 struct frame *f;
21842 struct window *w;
21843 int line_height, shift_by_width, shifted_region_width;
21844 struct glyph_row *row;
21845 struct glyph *glyph;
21846 int frame_x, frame_y;
21847 EMACS_INT hpos;
21848
21849 xassert (updated_window && updated_row);
21850 BLOCK_INPUT;
21851 w = updated_window;
21852 f = XFRAME (WINDOW_FRAME (w));
21853
21854 /* Get the height of the line we are in. */
21855 row = updated_row;
21856 line_height = row->height;
21857
21858 /* Get the width of the glyphs to insert. */
21859 shift_by_width = 0;
21860 for (glyph = start; glyph < start + len; ++glyph)
21861 shift_by_width += glyph->pixel_width;
21862
21863 /* Get the width of the region to shift right. */
21864 shifted_region_width = (window_box_width (w, updated_area)
21865 - output_cursor.x
21866 - shift_by_width);
21867
21868 /* Shift right. */
21869 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21870 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21871
21872 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21873 line_height, shift_by_width);
21874
21875 /* Write the glyphs. */
21876 hpos = start - row->glyphs[updated_area];
21877 draw_glyphs (w, output_cursor.x, row, updated_area,
21878 hpos, hpos + len,
21879 DRAW_NORMAL_TEXT, 0);
21880
21881 /* Advance the output cursor. */
21882 output_cursor.hpos += len;
21883 output_cursor.x += shift_by_width;
21884 UNBLOCK_INPUT;
21885 }
21886
21887
21888 /* EXPORT for RIF:
21889 Erase the current text line from the nominal cursor position
21890 (inclusive) to pixel column TO_X (exclusive). The idea is that
21891 everything from TO_X onward is already erased.
21892
21893 TO_X is a pixel position relative to updated_area of
21894 updated_window. TO_X == -1 means clear to the end of this area. */
21895
21896 void
21897 x_clear_end_of_line (to_x)
21898 int to_x;
21899 {
21900 struct frame *f;
21901 struct window *w = updated_window;
21902 int max_x, min_y, max_y;
21903 int from_x, from_y, to_y;
21904
21905 xassert (updated_window && updated_row);
21906 f = XFRAME (w->frame);
21907
21908 if (updated_row->full_width_p)
21909 max_x = WINDOW_TOTAL_WIDTH (w);
21910 else
21911 max_x = window_box_width (w, updated_area);
21912 max_y = window_text_bottom_y (w);
21913
21914 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21915 of window. For TO_X > 0, truncate to end of drawing area. */
21916 if (to_x == 0)
21917 return;
21918 else if (to_x < 0)
21919 to_x = max_x;
21920 else
21921 to_x = min (to_x, max_x);
21922
21923 to_y = min (max_y, output_cursor.y + updated_row->height);
21924
21925 /* Notice if the cursor will be cleared by this operation. */
21926 if (!updated_row->full_width_p)
21927 notice_overwritten_cursor (w, updated_area,
21928 output_cursor.x, -1,
21929 updated_row->y,
21930 MATRIX_ROW_BOTTOM_Y (updated_row));
21931
21932 from_x = output_cursor.x;
21933
21934 /* Translate to frame coordinates. */
21935 if (updated_row->full_width_p)
21936 {
21937 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21938 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21939 }
21940 else
21941 {
21942 int area_left = window_box_left (w, updated_area);
21943 from_x += area_left;
21944 to_x += area_left;
21945 }
21946
21947 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21948 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21949 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21950
21951 /* Prevent inadvertently clearing to end of the X window. */
21952 if (to_x > from_x && to_y > from_y)
21953 {
21954 BLOCK_INPUT;
21955 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21956 to_x - from_x, to_y - from_y);
21957 UNBLOCK_INPUT;
21958 }
21959 }
21960
21961 #endif /* HAVE_WINDOW_SYSTEM */
21962
21963
21964 \f
21965 /***********************************************************************
21966 Cursor types
21967 ***********************************************************************/
21968
21969 /* Value is the internal representation of the specified cursor type
21970 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21971 of the bar cursor. */
21972
21973 static enum text_cursor_kinds
21974 get_specified_cursor_type (arg, width)
21975 Lisp_Object arg;
21976 int *width;
21977 {
21978 enum text_cursor_kinds type;
21979
21980 if (NILP (arg))
21981 return NO_CURSOR;
21982
21983 if (EQ (arg, Qbox))
21984 return FILLED_BOX_CURSOR;
21985
21986 if (EQ (arg, Qhollow))
21987 return HOLLOW_BOX_CURSOR;
21988
21989 if (EQ (arg, Qbar))
21990 {
21991 *width = 2;
21992 return BAR_CURSOR;
21993 }
21994
21995 if (CONSP (arg)
21996 && EQ (XCAR (arg), Qbar)
21997 && INTEGERP (XCDR (arg))
21998 && XINT (XCDR (arg)) >= 0)
21999 {
22000 *width = XINT (XCDR (arg));
22001 return BAR_CURSOR;
22002 }
22003
22004 if (EQ (arg, Qhbar))
22005 {
22006 *width = 2;
22007 return HBAR_CURSOR;
22008 }
22009
22010 if (CONSP (arg)
22011 && EQ (XCAR (arg), Qhbar)
22012 && INTEGERP (XCDR (arg))
22013 && XINT (XCDR (arg)) >= 0)
22014 {
22015 *width = XINT (XCDR (arg));
22016 return HBAR_CURSOR;
22017 }
22018
22019 /* Treat anything unknown as "hollow box cursor".
22020 It was bad to signal an error; people have trouble fixing
22021 .Xdefaults with Emacs, when it has something bad in it. */
22022 type = HOLLOW_BOX_CURSOR;
22023
22024 return type;
22025 }
22026
22027 /* Set the default cursor types for specified frame. */
22028 void
22029 set_frame_cursor_types (f, arg)
22030 struct frame *f;
22031 Lisp_Object arg;
22032 {
22033 int width;
22034 Lisp_Object tem;
22035
22036 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22037 FRAME_CURSOR_WIDTH (f) = width;
22038
22039 /* By default, set up the blink-off state depending on the on-state. */
22040
22041 tem = Fassoc (arg, Vblink_cursor_alist);
22042 if (!NILP (tem))
22043 {
22044 FRAME_BLINK_OFF_CURSOR (f)
22045 = get_specified_cursor_type (XCDR (tem), &width);
22046 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22047 }
22048 else
22049 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22050 }
22051
22052
22053 /* Return the cursor we want to be displayed in window W. Return
22054 width of bar/hbar cursor through WIDTH arg. Return with
22055 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22056 (i.e. if the `system caret' should track this cursor).
22057
22058 In a mini-buffer window, we want the cursor only to appear if we
22059 are reading input from this window. For the selected window, we
22060 want the cursor type given by the frame parameter or buffer local
22061 setting of cursor-type. If explicitly marked off, draw no cursor.
22062 In all other cases, we want a hollow box cursor. */
22063
22064 static enum text_cursor_kinds
22065 get_window_cursor_type (w, glyph, width, active_cursor)
22066 struct window *w;
22067 struct glyph *glyph;
22068 int *width;
22069 int *active_cursor;
22070 {
22071 struct frame *f = XFRAME (w->frame);
22072 struct buffer *b = XBUFFER (w->buffer);
22073 int cursor_type = DEFAULT_CURSOR;
22074 Lisp_Object alt_cursor;
22075 int non_selected = 0;
22076
22077 *active_cursor = 1;
22078
22079 /* Echo area */
22080 if (cursor_in_echo_area
22081 && FRAME_HAS_MINIBUF_P (f)
22082 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22083 {
22084 if (w == XWINDOW (echo_area_window))
22085 {
22086 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22087 {
22088 *width = FRAME_CURSOR_WIDTH (f);
22089 return FRAME_DESIRED_CURSOR (f);
22090 }
22091 else
22092 return get_specified_cursor_type (b->cursor_type, width);
22093 }
22094
22095 *active_cursor = 0;
22096 non_selected = 1;
22097 }
22098
22099 /* Detect a nonselected window or nonselected frame. */
22100 else if (w != XWINDOW (f->selected_window)
22101 #ifdef HAVE_WINDOW_SYSTEM
22102 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22103 #endif
22104 )
22105 {
22106 *active_cursor = 0;
22107
22108 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22109 return NO_CURSOR;
22110
22111 non_selected = 1;
22112 }
22113
22114 /* Never display a cursor in a window in which cursor-type is nil. */
22115 if (NILP (b->cursor_type))
22116 return NO_CURSOR;
22117
22118 /* Get the normal cursor type for this window. */
22119 if (EQ (b->cursor_type, Qt))
22120 {
22121 cursor_type = FRAME_DESIRED_CURSOR (f);
22122 *width = FRAME_CURSOR_WIDTH (f);
22123 }
22124 else
22125 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22126
22127 /* Use cursor-in-non-selected-windows instead
22128 for non-selected window or frame. */
22129 if (non_selected)
22130 {
22131 alt_cursor = b->cursor_in_non_selected_windows;
22132 if (!EQ (Qt, alt_cursor))
22133 return get_specified_cursor_type (alt_cursor, width);
22134 /* t means modify the normal cursor type. */
22135 if (cursor_type == FILLED_BOX_CURSOR)
22136 cursor_type = HOLLOW_BOX_CURSOR;
22137 else if (cursor_type == BAR_CURSOR && *width > 1)
22138 --*width;
22139 return cursor_type;
22140 }
22141
22142 /* Use normal cursor if not blinked off. */
22143 if (!w->cursor_off_p)
22144 {
22145 #ifdef HAVE_WINDOW_SYSTEM
22146 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22147 {
22148 if (cursor_type == FILLED_BOX_CURSOR)
22149 {
22150 /* Using a block cursor on large images can be very annoying.
22151 So use a hollow cursor for "large" images.
22152 If image is not transparent (no mask), also use hollow cursor. */
22153 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22154 if (img != NULL && IMAGEP (img->spec))
22155 {
22156 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22157 where N = size of default frame font size.
22158 This should cover most of the "tiny" icons people may use. */
22159 if (!img->mask
22160 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22161 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22162 cursor_type = HOLLOW_BOX_CURSOR;
22163 }
22164 }
22165 else if (cursor_type != NO_CURSOR)
22166 {
22167 /* Display current only supports BOX and HOLLOW cursors for images.
22168 So for now, unconditionally use a HOLLOW cursor when cursor is
22169 not a solid box cursor. */
22170 cursor_type = HOLLOW_BOX_CURSOR;
22171 }
22172 }
22173 #endif
22174 return cursor_type;
22175 }
22176
22177 /* Cursor is blinked off, so determine how to "toggle" it. */
22178
22179 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22180 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22181 return get_specified_cursor_type (XCDR (alt_cursor), width);
22182
22183 /* Then see if frame has specified a specific blink off cursor type. */
22184 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22185 {
22186 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22187 return FRAME_BLINK_OFF_CURSOR (f);
22188 }
22189
22190 #if 0
22191 /* Some people liked having a permanently visible blinking cursor,
22192 while others had very strong opinions against it. So it was
22193 decided to remove it. KFS 2003-09-03 */
22194
22195 /* Finally perform built-in cursor blinking:
22196 filled box <-> hollow box
22197 wide [h]bar <-> narrow [h]bar
22198 narrow [h]bar <-> no cursor
22199 other type <-> no cursor */
22200
22201 if (cursor_type == FILLED_BOX_CURSOR)
22202 return HOLLOW_BOX_CURSOR;
22203
22204 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22205 {
22206 *width = 1;
22207 return cursor_type;
22208 }
22209 #endif
22210
22211 return NO_CURSOR;
22212 }
22213
22214
22215 #ifdef HAVE_WINDOW_SYSTEM
22216
22217 /* Notice when the text cursor of window W has been completely
22218 overwritten by a drawing operation that outputs glyphs in AREA
22219 starting at X0 and ending at X1 in the line starting at Y0 and
22220 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22221 the rest of the line after X0 has been written. Y coordinates
22222 are window-relative. */
22223
22224 static void
22225 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22226 struct window *w;
22227 enum glyph_row_area area;
22228 int x0, y0, x1, y1;
22229 {
22230 int cx0, cx1, cy0, cy1;
22231 struct glyph_row *row;
22232
22233 if (!w->phys_cursor_on_p)
22234 return;
22235 if (area != TEXT_AREA)
22236 return;
22237
22238 if (w->phys_cursor.vpos < 0
22239 || w->phys_cursor.vpos >= w->current_matrix->nrows
22240 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22241 !(row->enabled_p && row->displays_text_p)))
22242 return;
22243
22244 if (row->cursor_in_fringe_p)
22245 {
22246 row->cursor_in_fringe_p = 0;
22247 draw_fringe_bitmap (w, row, 0);
22248 w->phys_cursor_on_p = 0;
22249 return;
22250 }
22251
22252 cx0 = w->phys_cursor.x;
22253 cx1 = cx0 + w->phys_cursor_width;
22254 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22255 return;
22256
22257 /* The cursor image will be completely removed from the
22258 screen if the output area intersects the cursor area in
22259 y-direction. When we draw in [y0 y1[, and some part of
22260 the cursor is at y < y0, that part must have been drawn
22261 before. When scrolling, the cursor is erased before
22262 actually scrolling, so we don't come here. When not
22263 scrolling, the rows above the old cursor row must have
22264 changed, and in this case these rows must have written
22265 over the cursor image.
22266
22267 Likewise if part of the cursor is below y1, with the
22268 exception of the cursor being in the first blank row at
22269 the buffer and window end because update_text_area
22270 doesn't draw that row. (Except when it does, but
22271 that's handled in update_text_area.) */
22272
22273 cy0 = w->phys_cursor.y;
22274 cy1 = cy0 + w->phys_cursor_height;
22275 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22276 return;
22277
22278 w->phys_cursor_on_p = 0;
22279 }
22280
22281 #endif /* HAVE_WINDOW_SYSTEM */
22282
22283 \f
22284 /************************************************************************
22285 Mouse Face
22286 ************************************************************************/
22287
22288 #ifdef HAVE_WINDOW_SYSTEM
22289
22290 /* EXPORT for RIF:
22291 Fix the display of area AREA of overlapping row ROW in window W
22292 with respect to the overlapping part OVERLAPS. */
22293
22294 void
22295 x_fix_overlapping_area (w, row, area, overlaps)
22296 struct window *w;
22297 struct glyph_row *row;
22298 enum glyph_row_area area;
22299 int overlaps;
22300 {
22301 int i, x;
22302
22303 BLOCK_INPUT;
22304
22305 x = 0;
22306 for (i = 0; i < row->used[area];)
22307 {
22308 if (row->glyphs[area][i].overlaps_vertically_p)
22309 {
22310 int start = i, start_x = x;
22311
22312 do
22313 {
22314 x += row->glyphs[area][i].pixel_width;
22315 ++i;
22316 }
22317 while (i < row->used[area]
22318 && row->glyphs[area][i].overlaps_vertically_p);
22319
22320 draw_glyphs (w, start_x, row, area,
22321 start, i,
22322 DRAW_NORMAL_TEXT, overlaps);
22323 }
22324 else
22325 {
22326 x += row->glyphs[area][i].pixel_width;
22327 ++i;
22328 }
22329 }
22330
22331 UNBLOCK_INPUT;
22332 }
22333
22334
22335 /* EXPORT:
22336 Draw the cursor glyph of window W in glyph row ROW. See the
22337 comment of draw_glyphs for the meaning of HL. */
22338
22339 void
22340 draw_phys_cursor_glyph (w, row, hl)
22341 struct window *w;
22342 struct glyph_row *row;
22343 enum draw_glyphs_face hl;
22344 {
22345 /* If cursor hpos is out of bounds, don't draw garbage. This can
22346 happen in mini-buffer windows when switching between echo area
22347 glyphs and mini-buffer. */
22348 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22349 {
22350 int on_p = w->phys_cursor_on_p;
22351 int x1;
22352 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22353 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22354 hl, 0);
22355 w->phys_cursor_on_p = on_p;
22356
22357 if (hl == DRAW_CURSOR)
22358 w->phys_cursor_width = x1 - w->phys_cursor.x;
22359 /* When we erase the cursor, and ROW is overlapped by other
22360 rows, make sure that these overlapping parts of other rows
22361 are redrawn. */
22362 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22363 {
22364 w->phys_cursor_width = x1 - w->phys_cursor.x;
22365
22366 if (row > w->current_matrix->rows
22367 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22368 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22369 OVERLAPS_ERASED_CURSOR);
22370
22371 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22372 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22373 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22374 OVERLAPS_ERASED_CURSOR);
22375 }
22376 }
22377 }
22378
22379
22380 /* EXPORT:
22381 Erase the image of a cursor of window W from the screen. */
22382
22383 void
22384 erase_phys_cursor (w)
22385 struct window *w;
22386 {
22387 struct frame *f = XFRAME (w->frame);
22388 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22389 int hpos = w->phys_cursor.hpos;
22390 int vpos = w->phys_cursor.vpos;
22391 int mouse_face_here_p = 0;
22392 struct glyph_matrix *active_glyphs = w->current_matrix;
22393 struct glyph_row *cursor_row;
22394 struct glyph *cursor_glyph;
22395 enum draw_glyphs_face hl;
22396
22397 /* No cursor displayed or row invalidated => nothing to do on the
22398 screen. */
22399 if (w->phys_cursor_type == NO_CURSOR)
22400 goto mark_cursor_off;
22401
22402 /* VPOS >= active_glyphs->nrows means that window has been resized.
22403 Don't bother to erase the cursor. */
22404 if (vpos >= active_glyphs->nrows)
22405 goto mark_cursor_off;
22406
22407 /* If row containing cursor is marked invalid, there is nothing we
22408 can do. */
22409 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22410 if (!cursor_row->enabled_p)
22411 goto mark_cursor_off;
22412
22413 /* If line spacing is > 0, old cursor may only be partially visible in
22414 window after split-window. So adjust visible height. */
22415 cursor_row->visible_height = min (cursor_row->visible_height,
22416 window_text_bottom_y (w) - cursor_row->y);
22417
22418 /* If row is completely invisible, don't attempt to delete a cursor which
22419 isn't there. This can happen if cursor is at top of a window, and
22420 we switch to a buffer with a header line in that window. */
22421 if (cursor_row->visible_height <= 0)
22422 goto mark_cursor_off;
22423
22424 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22425 if (cursor_row->cursor_in_fringe_p)
22426 {
22427 cursor_row->cursor_in_fringe_p = 0;
22428 draw_fringe_bitmap (w, cursor_row, 0);
22429 goto mark_cursor_off;
22430 }
22431
22432 /* This can happen when the new row is shorter than the old one.
22433 In this case, either draw_glyphs or clear_end_of_line
22434 should have cleared the cursor. Note that we wouldn't be
22435 able to erase the cursor in this case because we don't have a
22436 cursor glyph at hand. */
22437 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22438 goto mark_cursor_off;
22439
22440 /* If the cursor is in the mouse face area, redisplay that when
22441 we clear the cursor. */
22442 if (! NILP (dpyinfo->mouse_face_window)
22443 && w == XWINDOW (dpyinfo->mouse_face_window)
22444 && (vpos > dpyinfo->mouse_face_beg_row
22445 || (vpos == dpyinfo->mouse_face_beg_row
22446 && hpos >= dpyinfo->mouse_face_beg_col))
22447 && (vpos < dpyinfo->mouse_face_end_row
22448 || (vpos == dpyinfo->mouse_face_end_row
22449 && hpos < dpyinfo->mouse_face_end_col))
22450 /* Don't redraw the cursor's spot in mouse face if it is at the
22451 end of a line (on a newline). The cursor appears there, but
22452 mouse highlighting does not. */
22453 && cursor_row->used[TEXT_AREA] > hpos)
22454 mouse_face_here_p = 1;
22455
22456 /* Maybe clear the display under the cursor. */
22457 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22458 {
22459 int x, y, left_x;
22460 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22461 int width;
22462
22463 cursor_glyph = get_phys_cursor_glyph (w);
22464 if (cursor_glyph == NULL)
22465 goto mark_cursor_off;
22466
22467 width = cursor_glyph->pixel_width;
22468 left_x = window_box_left_offset (w, TEXT_AREA);
22469 x = w->phys_cursor.x;
22470 if (x < left_x)
22471 width -= left_x - x;
22472 width = min (width, window_box_width (w, TEXT_AREA) - x);
22473 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22474 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22475
22476 if (width > 0)
22477 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22478 }
22479
22480 /* Erase the cursor by redrawing the character underneath it. */
22481 if (mouse_face_here_p)
22482 hl = DRAW_MOUSE_FACE;
22483 else
22484 hl = DRAW_NORMAL_TEXT;
22485 draw_phys_cursor_glyph (w, cursor_row, hl);
22486
22487 mark_cursor_off:
22488 w->phys_cursor_on_p = 0;
22489 w->phys_cursor_type = NO_CURSOR;
22490 }
22491
22492
22493 /* EXPORT:
22494 Display or clear cursor of window W. If ON is zero, clear the
22495 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22496 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22497
22498 void
22499 display_and_set_cursor (w, on, hpos, vpos, x, y)
22500 struct window *w;
22501 int on, hpos, vpos, x, y;
22502 {
22503 struct frame *f = XFRAME (w->frame);
22504 int new_cursor_type;
22505 int new_cursor_width;
22506 int active_cursor;
22507 struct glyph_row *glyph_row;
22508 struct glyph *glyph;
22509
22510 /* This is pointless on invisible frames, and dangerous on garbaged
22511 windows and frames; in the latter case, the frame or window may
22512 be in the midst of changing its size, and x and y may be off the
22513 window. */
22514 if (! FRAME_VISIBLE_P (f)
22515 || FRAME_GARBAGED_P (f)
22516 || vpos >= w->current_matrix->nrows
22517 || hpos >= w->current_matrix->matrix_w)
22518 return;
22519
22520 /* If cursor is off and we want it off, return quickly. */
22521 if (!on && !w->phys_cursor_on_p)
22522 return;
22523
22524 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22525 /* If cursor row is not enabled, we don't really know where to
22526 display the cursor. */
22527 if (!glyph_row->enabled_p)
22528 {
22529 w->phys_cursor_on_p = 0;
22530 return;
22531 }
22532
22533 glyph = NULL;
22534 if (!glyph_row->exact_window_width_line_p
22535 || hpos < glyph_row->used[TEXT_AREA])
22536 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22537
22538 xassert (interrupt_input_blocked);
22539
22540 /* Set new_cursor_type to the cursor we want to be displayed. */
22541 new_cursor_type = get_window_cursor_type (w, glyph,
22542 &new_cursor_width, &active_cursor);
22543
22544 /* If cursor is currently being shown and we don't want it to be or
22545 it is in the wrong place, or the cursor type is not what we want,
22546 erase it. */
22547 if (w->phys_cursor_on_p
22548 && (!on
22549 || w->phys_cursor.x != x
22550 || w->phys_cursor.y != y
22551 || new_cursor_type != w->phys_cursor_type
22552 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22553 && new_cursor_width != w->phys_cursor_width)))
22554 erase_phys_cursor (w);
22555
22556 /* Don't check phys_cursor_on_p here because that flag is only set
22557 to zero in some cases where we know that the cursor has been
22558 completely erased, to avoid the extra work of erasing the cursor
22559 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22560 still not be visible, or it has only been partly erased. */
22561 if (on)
22562 {
22563 w->phys_cursor_ascent = glyph_row->ascent;
22564 w->phys_cursor_height = glyph_row->height;
22565
22566 /* Set phys_cursor_.* before x_draw_.* is called because some
22567 of them may need the information. */
22568 w->phys_cursor.x = x;
22569 w->phys_cursor.y = glyph_row->y;
22570 w->phys_cursor.hpos = hpos;
22571 w->phys_cursor.vpos = vpos;
22572 }
22573
22574 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22575 new_cursor_type, new_cursor_width,
22576 on, active_cursor);
22577 }
22578
22579
22580 /* Switch the display of W's cursor on or off, according to the value
22581 of ON. */
22582
22583 void
22584 update_window_cursor (w, on)
22585 struct window *w;
22586 int on;
22587 {
22588 /* Don't update cursor in windows whose frame is in the process
22589 of being deleted. */
22590 if (w->current_matrix)
22591 {
22592 BLOCK_INPUT;
22593 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22594 w->phys_cursor.x, w->phys_cursor.y);
22595 UNBLOCK_INPUT;
22596 }
22597 }
22598
22599
22600 /* Call update_window_cursor with parameter ON_P on all leaf windows
22601 in the window tree rooted at W. */
22602
22603 static void
22604 update_cursor_in_window_tree (w, on_p)
22605 struct window *w;
22606 int on_p;
22607 {
22608 while (w)
22609 {
22610 if (!NILP (w->hchild))
22611 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22612 else if (!NILP (w->vchild))
22613 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22614 else
22615 update_window_cursor (w, on_p);
22616
22617 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22618 }
22619 }
22620
22621
22622 /* EXPORT:
22623 Display the cursor on window W, or clear it, according to ON_P.
22624 Don't change the cursor's position. */
22625
22626 void
22627 x_update_cursor (f, on_p)
22628 struct frame *f;
22629 int on_p;
22630 {
22631 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22632 }
22633
22634
22635 /* EXPORT:
22636 Clear the cursor of window W to background color, and mark the
22637 cursor as not shown. This is used when the text where the cursor
22638 is about to be rewritten. */
22639
22640 void
22641 x_clear_cursor (w)
22642 struct window *w;
22643 {
22644 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22645 update_window_cursor (w, 0);
22646 }
22647
22648
22649 /* EXPORT:
22650 Display the active region described by mouse_face_* according to DRAW. */
22651
22652 void
22653 show_mouse_face (dpyinfo, draw)
22654 Display_Info *dpyinfo;
22655 enum draw_glyphs_face draw;
22656 {
22657 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22658 struct frame *f = XFRAME (WINDOW_FRAME (w));
22659
22660 if (/* If window is in the process of being destroyed, don't bother
22661 to do anything. */
22662 w->current_matrix != NULL
22663 /* Don't update mouse highlight if hidden */
22664 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22665 /* Recognize when we are called to operate on rows that don't exist
22666 anymore. This can happen when a window is split. */
22667 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22668 {
22669 int phys_cursor_on_p = w->phys_cursor_on_p;
22670 struct glyph_row *row, *first, *last;
22671
22672 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22673 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22674
22675 for (row = first; row <= last && row->enabled_p; ++row)
22676 {
22677 int start_hpos, end_hpos, start_x;
22678
22679 /* For all but the first row, the highlight starts at column 0. */
22680 if (row == first)
22681 {
22682 start_hpos = dpyinfo->mouse_face_beg_col;
22683 start_x = dpyinfo->mouse_face_beg_x;
22684 }
22685 else
22686 {
22687 start_hpos = 0;
22688 start_x = 0;
22689 }
22690
22691 if (row == last)
22692 end_hpos = dpyinfo->mouse_face_end_col;
22693 else
22694 {
22695 end_hpos = row->used[TEXT_AREA];
22696 if (draw == DRAW_NORMAL_TEXT)
22697 row->fill_line_p = 1; /* Clear to end of line */
22698 }
22699
22700 if (end_hpos > start_hpos)
22701 {
22702 draw_glyphs (w, start_x, row, TEXT_AREA,
22703 start_hpos, end_hpos,
22704 draw, 0);
22705
22706 row->mouse_face_p
22707 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22708 }
22709 }
22710
22711 /* When we've written over the cursor, arrange for it to
22712 be displayed again. */
22713 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22714 {
22715 BLOCK_INPUT;
22716 display_and_set_cursor (w, 1,
22717 w->phys_cursor.hpos, w->phys_cursor.vpos,
22718 w->phys_cursor.x, w->phys_cursor.y);
22719 UNBLOCK_INPUT;
22720 }
22721 }
22722
22723 /* Change the mouse cursor. */
22724 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22725 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22726 else if (draw == DRAW_MOUSE_FACE)
22727 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22728 else
22729 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22730 }
22731
22732 /* EXPORT:
22733 Clear out the mouse-highlighted active region.
22734 Redraw it un-highlighted first. Value is non-zero if mouse
22735 face was actually drawn unhighlighted. */
22736
22737 int
22738 clear_mouse_face (dpyinfo)
22739 Display_Info *dpyinfo;
22740 {
22741 int cleared = 0;
22742
22743 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22744 {
22745 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22746 cleared = 1;
22747 }
22748
22749 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22750 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22751 dpyinfo->mouse_face_window = Qnil;
22752 dpyinfo->mouse_face_overlay = Qnil;
22753 return cleared;
22754 }
22755
22756
22757 /* EXPORT:
22758 Non-zero if physical cursor of window W is within mouse face. */
22759
22760 int
22761 cursor_in_mouse_face_p (w)
22762 struct window *w;
22763 {
22764 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22765 int in_mouse_face = 0;
22766
22767 if (WINDOWP (dpyinfo->mouse_face_window)
22768 && XWINDOW (dpyinfo->mouse_face_window) == w)
22769 {
22770 int hpos = w->phys_cursor.hpos;
22771 int vpos = w->phys_cursor.vpos;
22772
22773 if (vpos >= dpyinfo->mouse_face_beg_row
22774 && vpos <= dpyinfo->mouse_face_end_row
22775 && (vpos > dpyinfo->mouse_face_beg_row
22776 || hpos >= dpyinfo->mouse_face_beg_col)
22777 && (vpos < dpyinfo->mouse_face_end_row
22778 || hpos < dpyinfo->mouse_face_end_col
22779 || dpyinfo->mouse_face_past_end))
22780 in_mouse_face = 1;
22781 }
22782
22783 return in_mouse_face;
22784 }
22785
22786
22787
22788 \f
22789 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22790 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22791 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22792 for the overlay or run of text properties specifying the mouse
22793 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22794 before-string and after-string that must also be highlighted.
22795 DISPLAY_STRING, if non-nil, is a display string that may cover some
22796 or all of the highlighted text. */
22797
22798 static void
22799 mouse_face_from_buffer_pos (Lisp_Object window,
22800 Display_Info *dpyinfo,
22801 EMACS_INT mouse_charpos,
22802 EMACS_INT start_charpos,
22803 EMACS_INT end_charpos,
22804 Lisp_Object before_string,
22805 Lisp_Object after_string,
22806 Lisp_Object display_string)
22807 {
22808 struct window *w = XWINDOW (window);
22809 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22810 struct glyph_row *row;
22811 struct glyph *glyph, *end;
22812 EMACS_INT ignore;
22813 int x;
22814
22815 xassert (NILP (display_string) || STRINGP (display_string));
22816 xassert (NILP (before_string) || STRINGP (before_string));
22817 xassert (NILP (after_string) || STRINGP (after_string));
22818
22819 /* Find the first highlighted glyph. */
22820 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22821 {
22822 dpyinfo->mouse_face_beg_col = 0;
22823 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22824 dpyinfo->mouse_face_beg_x = first->x;
22825 dpyinfo->mouse_face_beg_y = first->y;
22826 }
22827 else
22828 {
22829 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22830 if (row == NULL)
22831 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22832
22833 /* If the before-string or display-string contains newlines,
22834 row_containing_pos skips to its last row. Move back. */
22835 if (!NILP (before_string) || !NILP (display_string))
22836 {
22837 struct glyph_row *prev;
22838 while ((prev = row - 1, prev >= first)
22839 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22840 && prev->used[TEXT_AREA] > 0)
22841 {
22842 struct glyph *beg = prev->glyphs[TEXT_AREA];
22843 glyph = beg + prev->used[TEXT_AREA];
22844 while (--glyph >= beg && INTEGERP (glyph->object));
22845 if (glyph < beg
22846 || !(EQ (glyph->object, before_string)
22847 || EQ (glyph->object, display_string)))
22848 break;
22849 row = prev;
22850 }
22851 }
22852
22853 glyph = row->glyphs[TEXT_AREA];
22854 end = glyph + row->used[TEXT_AREA];
22855 x = row->x;
22856 dpyinfo->mouse_face_beg_y = row->y;
22857 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22858
22859 /* Skip truncation glyphs at the start of the glyph row. */
22860 if (row->displays_text_p)
22861 for (; glyph < end
22862 && INTEGERP (glyph->object)
22863 && glyph->charpos < 0;
22864 ++glyph)
22865 x += glyph->pixel_width;
22866
22867 /* Scan the glyph row, stopping before BEFORE_STRING or
22868 DISPLAY_STRING or START_CHARPOS. */
22869 for (; glyph < end
22870 && !INTEGERP (glyph->object)
22871 && !EQ (glyph->object, before_string)
22872 && !EQ (glyph->object, display_string)
22873 && !(BUFFERP (glyph->object)
22874 && glyph->charpos >= start_charpos);
22875 ++glyph)
22876 x += glyph->pixel_width;
22877
22878 dpyinfo->mouse_face_beg_x = x;
22879 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22880 }
22881
22882 /* Find the last highlighted glyph. */
22883 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22884 if (row == NULL)
22885 {
22886 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22887 dpyinfo->mouse_face_past_end = 1;
22888 }
22889 else if (!NILP (after_string))
22890 {
22891 /* If the after-string has newlines, advance to its last row. */
22892 struct glyph_row *next;
22893 struct glyph_row *last
22894 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22895
22896 for (next = row + 1;
22897 next <= last
22898 && next->used[TEXT_AREA] > 0
22899 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22900 ++next)
22901 row = next;
22902 }
22903
22904 glyph = row->glyphs[TEXT_AREA];
22905 end = glyph + row->used[TEXT_AREA];
22906 x = row->x;
22907 dpyinfo->mouse_face_end_y = row->y;
22908 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22909
22910 /* Skip truncation glyphs at the start of the row. */
22911 if (row->displays_text_p)
22912 for (; glyph < end
22913 && INTEGERP (glyph->object)
22914 && glyph->charpos < 0;
22915 ++glyph)
22916 x += glyph->pixel_width;
22917
22918 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22919 AFTER_STRING. */
22920 for (; glyph < end
22921 && !INTEGERP (glyph->object)
22922 && !EQ (glyph->object, after_string)
22923 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22924 ++glyph)
22925 x += glyph->pixel_width;
22926
22927 /* If we found AFTER_STRING, consume it and stop. */
22928 if (EQ (glyph->object, after_string))
22929 {
22930 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22931 x += glyph->pixel_width;
22932 }
22933 else
22934 {
22935 /* If there's no after-string, we must check if we overshot,
22936 which might be the case if we stopped after a string glyph.
22937 That glyph may belong to a before-string or display-string
22938 associated with the end position, which must not be
22939 highlighted. */
22940 Lisp_Object prev_object;
22941 int pos;
22942
22943 while (glyph > row->glyphs[TEXT_AREA])
22944 {
22945 prev_object = (glyph - 1)->object;
22946 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
22947 break;
22948
22949 pos = string_buffer_position (w, prev_object, end_charpos);
22950 if (pos && pos < end_charpos)
22951 break;
22952
22953 for (; glyph > row->glyphs[TEXT_AREA]
22954 && EQ ((glyph - 1)->object, prev_object);
22955 --glyph)
22956 x -= (glyph - 1)->pixel_width;
22957 }
22958 }
22959
22960 dpyinfo->mouse_face_end_x = x;
22961 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
22962 dpyinfo->mouse_face_window = window;
22963 dpyinfo->mouse_face_face_id
22964 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
22965 mouse_charpos + 1,
22966 !dpyinfo->mouse_face_hidden, -1);
22967 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22968 }
22969
22970
22971 /* Find the position of the glyph for position POS in OBJECT in
22972 window W's current matrix, and return in *X, *Y the pixel
22973 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22974
22975 RIGHT_P non-zero means return the position of the right edge of the
22976 glyph, RIGHT_P zero means return the left edge position.
22977
22978 If no glyph for POS exists in the matrix, return the position of
22979 the glyph with the next smaller position that is in the matrix, if
22980 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22981 exists in the matrix, return the position of the glyph with the
22982 next larger position in OBJECT.
22983
22984 Value is non-zero if a glyph was found. */
22985
22986 static int
22987 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22988 struct window *w;
22989 EMACS_INT pos;
22990 Lisp_Object object;
22991 int *hpos, *vpos, *x, *y;
22992 int right_p;
22993 {
22994 int yb = window_text_bottom_y (w);
22995 struct glyph_row *r;
22996 struct glyph *best_glyph = NULL;
22997 struct glyph_row *best_row = NULL;
22998 int best_x = 0;
22999
23000 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23001 r->enabled_p && r->y < yb;
23002 ++r)
23003 {
23004 struct glyph *g = r->glyphs[TEXT_AREA];
23005 struct glyph *e = g + r->used[TEXT_AREA];
23006 int gx;
23007
23008 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23009 if (EQ (g->object, object))
23010 {
23011 if (g->charpos == pos)
23012 {
23013 best_glyph = g;
23014 best_x = gx;
23015 best_row = r;
23016 goto found;
23017 }
23018 else if (best_glyph == NULL
23019 || ((eabs (g->charpos - pos)
23020 < eabs (best_glyph->charpos - pos))
23021 && (right_p
23022 ? g->charpos < pos
23023 : g->charpos > pos)))
23024 {
23025 best_glyph = g;
23026 best_x = gx;
23027 best_row = r;
23028 }
23029 }
23030 }
23031
23032 found:
23033
23034 if (best_glyph)
23035 {
23036 *x = best_x;
23037 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23038
23039 if (right_p)
23040 {
23041 *x += best_glyph->pixel_width;
23042 ++*hpos;
23043 }
23044
23045 *y = best_row->y;
23046 *vpos = best_row - w->current_matrix->rows;
23047 }
23048
23049 return best_glyph != NULL;
23050 }
23051
23052
23053 /* See if position X, Y is within a hot-spot of an image. */
23054
23055 static int
23056 on_hot_spot_p (hot_spot, x, y)
23057 Lisp_Object hot_spot;
23058 int x, y;
23059 {
23060 if (!CONSP (hot_spot))
23061 return 0;
23062
23063 if (EQ (XCAR (hot_spot), Qrect))
23064 {
23065 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23066 Lisp_Object rect = XCDR (hot_spot);
23067 Lisp_Object tem;
23068 if (!CONSP (rect))
23069 return 0;
23070 if (!CONSP (XCAR (rect)))
23071 return 0;
23072 if (!CONSP (XCDR (rect)))
23073 return 0;
23074 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23075 return 0;
23076 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23077 return 0;
23078 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23079 return 0;
23080 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23081 return 0;
23082 return 1;
23083 }
23084 else if (EQ (XCAR (hot_spot), Qcircle))
23085 {
23086 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23087 Lisp_Object circ = XCDR (hot_spot);
23088 Lisp_Object lr, lx0, ly0;
23089 if (CONSP (circ)
23090 && CONSP (XCAR (circ))
23091 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23092 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23093 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23094 {
23095 double r = XFLOATINT (lr);
23096 double dx = XINT (lx0) - x;
23097 double dy = XINT (ly0) - y;
23098 return (dx * dx + dy * dy <= r * r);
23099 }
23100 }
23101 else if (EQ (XCAR (hot_spot), Qpoly))
23102 {
23103 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23104 if (VECTORP (XCDR (hot_spot)))
23105 {
23106 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23107 Lisp_Object *poly = v->contents;
23108 int n = v->header.size;
23109 int i;
23110 int inside = 0;
23111 Lisp_Object lx, ly;
23112 int x0, y0;
23113
23114 /* Need an even number of coordinates, and at least 3 edges. */
23115 if (n < 6 || n & 1)
23116 return 0;
23117
23118 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23119 If count is odd, we are inside polygon. Pixels on edges
23120 may or may not be included depending on actual geometry of the
23121 polygon. */
23122 if ((lx = poly[n-2], !INTEGERP (lx))
23123 || (ly = poly[n-1], !INTEGERP (lx)))
23124 return 0;
23125 x0 = XINT (lx), y0 = XINT (ly);
23126 for (i = 0; i < n; i += 2)
23127 {
23128 int x1 = x0, y1 = y0;
23129 if ((lx = poly[i], !INTEGERP (lx))
23130 || (ly = poly[i+1], !INTEGERP (ly)))
23131 return 0;
23132 x0 = XINT (lx), y0 = XINT (ly);
23133
23134 /* Does this segment cross the X line? */
23135 if (x0 >= x)
23136 {
23137 if (x1 >= x)
23138 continue;
23139 }
23140 else if (x1 < x)
23141 continue;
23142 if (y > y0 && y > y1)
23143 continue;
23144 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23145 inside = !inside;
23146 }
23147 return inside;
23148 }
23149 }
23150 return 0;
23151 }
23152
23153 Lisp_Object
23154 find_hot_spot (map, x, y)
23155 Lisp_Object map;
23156 int x, y;
23157 {
23158 while (CONSP (map))
23159 {
23160 if (CONSP (XCAR (map))
23161 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23162 return XCAR (map);
23163 map = XCDR (map);
23164 }
23165
23166 return Qnil;
23167 }
23168
23169 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23170 3, 3, 0,
23171 doc: /* Lookup in image map MAP coordinates X and Y.
23172 An image map is an alist where each element has the format (AREA ID PLIST).
23173 An AREA is specified as either a rectangle, a circle, or a polygon:
23174 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23175 pixel coordinates of the upper left and bottom right corners.
23176 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23177 and the radius of the circle; r may be a float or integer.
23178 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23179 vector describes one corner in the polygon.
23180 Returns the alist element for the first matching AREA in MAP. */)
23181 (map, x, y)
23182 Lisp_Object map;
23183 Lisp_Object x, y;
23184 {
23185 if (NILP (map))
23186 return Qnil;
23187
23188 CHECK_NUMBER (x);
23189 CHECK_NUMBER (y);
23190
23191 return find_hot_spot (map, XINT (x), XINT (y));
23192 }
23193
23194
23195 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23196 static void
23197 define_frame_cursor1 (f, cursor, pointer)
23198 struct frame *f;
23199 Cursor cursor;
23200 Lisp_Object pointer;
23201 {
23202 /* Do not change cursor shape while dragging mouse. */
23203 if (!NILP (do_mouse_tracking))
23204 return;
23205
23206 if (!NILP (pointer))
23207 {
23208 if (EQ (pointer, Qarrow))
23209 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23210 else if (EQ (pointer, Qhand))
23211 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23212 else if (EQ (pointer, Qtext))
23213 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23214 else if (EQ (pointer, intern ("hdrag")))
23215 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23216 #ifdef HAVE_X_WINDOWS
23217 else if (EQ (pointer, intern ("vdrag")))
23218 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23219 #endif
23220 else if (EQ (pointer, intern ("hourglass")))
23221 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23222 else if (EQ (pointer, Qmodeline))
23223 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23224 else
23225 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23226 }
23227
23228 if (cursor != No_Cursor)
23229 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23230 }
23231
23232 /* Take proper action when mouse has moved to the mode or header line
23233 or marginal area AREA of window W, x-position X and y-position Y.
23234 X is relative to the start of the text display area of W, so the
23235 width of bitmap areas and scroll bars must be subtracted to get a
23236 position relative to the start of the mode line. */
23237
23238 static void
23239 note_mode_line_or_margin_highlight (window, x, y, area)
23240 Lisp_Object window;
23241 int x, y;
23242 enum window_part area;
23243 {
23244 struct window *w = XWINDOW (window);
23245 struct frame *f = XFRAME (w->frame);
23246 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23247 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23248 Lisp_Object pointer = Qnil;
23249 int charpos, dx, dy, width, height;
23250 Lisp_Object string, object = Qnil;
23251 Lisp_Object pos, help;
23252
23253 Lisp_Object mouse_face;
23254 int original_x_pixel = x;
23255 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23256 struct glyph_row *row;
23257
23258 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23259 {
23260 int x0;
23261 struct glyph *end;
23262
23263 string = mode_line_string (w, area, &x, &y, &charpos,
23264 &object, &dx, &dy, &width, &height);
23265
23266 row = (area == ON_MODE_LINE
23267 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23268 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23269
23270 /* Find glyph */
23271 if (row->mode_line_p && row->enabled_p)
23272 {
23273 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23274 end = glyph + row->used[TEXT_AREA];
23275
23276 for (x0 = original_x_pixel;
23277 glyph < end && x0 >= glyph->pixel_width;
23278 ++glyph)
23279 x0 -= glyph->pixel_width;
23280
23281 if (glyph >= end)
23282 glyph = NULL;
23283 }
23284 }
23285 else
23286 {
23287 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23288 string = marginal_area_string (w, area, &x, &y, &charpos,
23289 &object, &dx, &dy, &width, &height);
23290 }
23291
23292 help = Qnil;
23293
23294 if (IMAGEP (object))
23295 {
23296 Lisp_Object image_map, hotspot;
23297 if ((image_map = Fplist_get (XCDR (object), QCmap),
23298 !NILP (image_map))
23299 && (hotspot = find_hot_spot (image_map, dx, dy),
23300 CONSP (hotspot))
23301 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23302 {
23303 Lisp_Object area_id, plist;
23304
23305 area_id = XCAR (hotspot);
23306 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23307 If so, we could look for mouse-enter, mouse-leave
23308 properties in PLIST (and do something...). */
23309 hotspot = XCDR (hotspot);
23310 if (CONSP (hotspot)
23311 && (plist = XCAR (hotspot), CONSP (plist)))
23312 {
23313 pointer = Fplist_get (plist, Qpointer);
23314 if (NILP (pointer))
23315 pointer = Qhand;
23316 help = Fplist_get (plist, Qhelp_echo);
23317 if (!NILP (help))
23318 {
23319 help_echo_string = help;
23320 /* Is this correct? ++kfs */
23321 XSETWINDOW (help_echo_window, w);
23322 help_echo_object = w->buffer;
23323 help_echo_pos = charpos;
23324 }
23325 }
23326 }
23327 if (NILP (pointer))
23328 pointer = Fplist_get (XCDR (object), QCpointer);
23329 }
23330
23331 if (STRINGP (string))
23332 {
23333 pos = make_number (charpos);
23334 /* If we're on a string with `help-echo' text property, arrange
23335 for the help to be displayed. This is done by setting the
23336 global variable help_echo_string to the help string. */
23337 if (NILP (help))
23338 {
23339 help = Fget_text_property (pos, Qhelp_echo, string);
23340 if (!NILP (help))
23341 {
23342 help_echo_string = help;
23343 XSETWINDOW (help_echo_window, w);
23344 help_echo_object = string;
23345 help_echo_pos = charpos;
23346 }
23347 }
23348
23349 if (NILP (pointer))
23350 pointer = Fget_text_property (pos, Qpointer, string);
23351
23352 /* Change the mouse pointer according to what is under X/Y. */
23353 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23354 {
23355 Lisp_Object map;
23356 map = Fget_text_property (pos, Qlocal_map, string);
23357 if (!KEYMAPP (map))
23358 map = Fget_text_property (pos, Qkeymap, string);
23359 if (!KEYMAPP (map))
23360 cursor = dpyinfo->vertical_scroll_bar_cursor;
23361 }
23362
23363 /* Change the mouse face according to what is under X/Y. */
23364 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23365 if (!NILP (mouse_face)
23366 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23367 && glyph)
23368 {
23369 Lisp_Object b, e;
23370
23371 struct glyph * tmp_glyph;
23372
23373 int gpos;
23374 int gseq_length;
23375 int total_pixel_width;
23376 EMACS_INT ignore;
23377
23378 int vpos, hpos;
23379
23380 b = Fprevious_single_property_change (make_number (charpos + 1),
23381 Qmouse_face, string, Qnil);
23382 if (NILP (b))
23383 b = make_number (0);
23384
23385 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23386 if (NILP (e))
23387 e = make_number (SCHARS (string));
23388
23389 /* Calculate the position(glyph position: GPOS) of GLYPH in
23390 displayed string. GPOS is different from CHARPOS.
23391
23392 CHARPOS is the position of glyph in internal string
23393 object. A mode line string format has structures which
23394 is converted to a flatten by emacs lisp interpreter.
23395 The internal string is an element of the structures.
23396 The displayed string is the flatten string. */
23397 gpos = 0;
23398 if (glyph > row_start_glyph)
23399 {
23400 tmp_glyph = glyph - 1;
23401 while (tmp_glyph >= row_start_glyph
23402 && tmp_glyph->charpos >= XINT (b)
23403 && EQ (tmp_glyph->object, glyph->object))
23404 {
23405 tmp_glyph--;
23406 gpos++;
23407 }
23408 }
23409
23410 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23411 displayed string holding GLYPH.
23412
23413 GSEQ_LENGTH is different from SCHARS (STRING).
23414 SCHARS (STRING) returns the length of the internal string. */
23415 for (tmp_glyph = glyph, gseq_length = gpos;
23416 tmp_glyph->charpos < XINT (e);
23417 tmp_glyph++, gseq_length++)
23418 {
23419 if (!EQ (tmp_glyph->object, glyph->object))
23420 break;
23421 }
23422
23423 total_pixel_width = 0;
23424 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23425 total_pixel_width += tmp_glyph->pixel_width;
23426
23427 /* Pre calculation of re-rendering position */
23428 vpos = (x - gpos);
23429 hpos = (area == ON_MODE_LINE
23430 ? (w->current_matrix)->nrows - 1
23431 : 0);
23432
23433 /* If the re-rendering position is included in the last
23434 re-rendering area, we should do nothing. */
23435 if ( EQ (window, dpyinfo->mouse_face_window)
23436 && dpyinfo->mouse_face_beg_col <= vpos
23437 && vpos < dpyinfo->mouse_face_end_col
23438 && dpyinfo->mouse_face_beg_row == hpos )
23439 return;
23440
23441 if (clear_mouse_face (dpyinfo))
23442 cursor = No_Cursor;
23443
23444 dpyinfo->mouse_face_beg_col = vpos;
23445 dpyinfo->mouse_face_beg_row = hpos;
23446
23447 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23448 dpyinfo->mouse_face_beg_y = 0;
23449
23450 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23451 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23452
23453 dpyinfo->mouse_face_end_x = 0;
23454 dpyinfo->mouse_face_end_y = 0;
23455
23456 dpyinfo->mouse_face_past_end = 0;
23457 dpyinfo->mouse_face_window = window;
23458
23459 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23460 charpos,
23461 0, 0, 0, &ignore,
23462 glyph->face_id, 1);
23463 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23464
23465 if (NILP (pointer))
23466 pointer = Qhand;
23467 }
23468 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23469 clear_mouse_face (dpyinfo);
23470 }
23471 define_frame_cursor1 (f, cursor, pointer);
23472 }
23473
23474
23475 /* EXPORT:
23476 Take proper action when the mouse has moved to position X, Y on
23477 frame F as regards highlighting characters that have mouse-face
23478 properties. Also de-highlighting chars where the mouse was before.
23479 X and Y can be negative or out of range. */
23480
23481 void
23482 note_mouse_highlight (f, x, y)
23483 struct frame *f;
23484 int x, y;
23485 {
23486 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23487 enum window_part part;
23488 Lisp_Object window;
23489 struct window *w;
23490 Cursor cursor = No_Cursor;
23491 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23492 struct buffer *b;
23493
23494 /* When a menu is active, don't highlight because this looks odd. */
23495 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23496 if (popup_activated ())
23497 return;
23498 #endif
23499
23500 if (NILP (Vmouse_highlight)
23501 || !f->glyphs_initialized_p
23502 || f->pointer_invisible)
23503 return;
23504
23505 dpyinfo->mouse_face_mouse_x = x;
23506 dpyinfo->mouse_face_mouse_y = y;
23507 dpyinfo->mouse_face_mouse_frame = f;
23508
23509 if (dpyinfo->mouse_face_defer)
23510 return;
23511
23512 if (gc_in_progress)
23513 {
23514 dpyinfo->mouse_face_deferred_gc = 1;
23515 return;
23516 }
23517
23518 /* Which window is that in? */
23519 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23520
23521 /* If we were displaying active text in another window, clear that.
23522 Also clear if we move out of text area in same window. */
23523 if (! EQ (window, dpyinfo->mouse_face_window)
23524 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23525 && !NILP (dpyinfo->mouse_face_window)))
23526 clear_mouse_face (dpyinfo);
23527
23528 /* Not on a window -> return. */
23529 if (!WINDOWP (window))
23530 return;
23531
23532 /* Reset help_echo_string. It will get recomputed below. */
23533 help_echo_string = Qnil;
23534
23535 /* Convert to window-relative pixel coordinates. */
23536 w = XWINDOW (window);
23537 frame_to_window_pixel_xy (w, &x, &y);
23538
23539 /* Handle tool-bar window differently since it doesn't display a
23540 buffer. */
23541 if (EQ (window, f->tool_bar_window))
23542 {
23543 note_tool_bar_highlight (f, x, y);
23544 return;
23545 }
23546
23547 /* Mouse is on the mode, header line or margin? */
23548 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23549 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23550 {
23551 note_mode_line_or_margin_highlight (window, x, y, part);
23552 return;
23553 }
23554
23555 if (part == ON_VERTICAL_BORDER)
23556 {
23557 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23558 help_echo_string = build_string ("drag-mouse-1: resize");
23559 }
23560 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23561 || part == ON_SCROLL_BAR)
23562 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23563 else
23564 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23565
23566 /* Are we in a window whose display is up to date?
23567 And verify the buffer's text has not changed. */
23568 b = XBUFFER (w->buffer);
23569 if (part == ON_TEXT
23570 && EQ (w->window_end_valid, w->buffer)
23571 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23572 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23573 {
23574 int hpos, vpos, pos, i, dx, dy, area;
23575 struct glyph *glyph;
23576 Lisp_Object object;
23577 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23578 Lisp_Object *overlay_vec = NULL;
23579 int noverlays;
23580 struct buffer *obuf;
23581 int obegv, ozv, same_region;
23582
23583 /* Find the glyph under X/Y. */
23584 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23585
23586 /* Look for :pointer property on image. */
23587 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23588 {
23589 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23590 if (img != NULL && IMAGEP (img->spec))
23591 {
23592 Lisp_Object image_map, hotspot;
23593 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23594 !NILP (image_map))
23595 && (hotspot = find_hot_spot (image_map,
23596 glyph->slice.x + dx,
23597 glyph->slice.y + dy),
23598 CONSP (hotspot))
23599 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23600 {
23601 Lisp_Object area_id, plist;
23602
23603 area_id = XCAR (hotspot);
23604 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23605 If so, we could look for mouse-enter, mouse-leave
23606 properties in PLIST (and do something...). */
23607 hotspot = XCDR (hotspot);
23608 if (CONSP (hotspot)
23609 && (plist = XCAR (hotspot), CONSP (plist)))
23610 {
23611 pointer = Fplist_get (plist, Qpointer);
23612 if (NILP (pointer))
23613 pointer = Qhand;
23614 help_echo_string = Fplist_get (plist, Qhelp_echo);
23615 if (!NILP (help_echo_string))
23616 {
23617 help_echo_window = window;
23618 help_echo_object = glyph->object;
23619 help_echo_pos = glyph->charpos;
23620 }
23621 }
23622 }
23623 if (NILP (pointer))
23624 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23625 }
23626 }
23627
23628 /* Clear mouse face if X/Y not over text. */
23629 if (glyph == NULL
23630 || area != TEXT_AREA
23631 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23632 {
23633 if (clear_mouse_face (dpyinfo))
23634 cursor = No_Cursor;
23635 if (NILP (pointer))
23636 {
23637 if (area != TEXT_AREA)
23638 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23639 else
23640 pointer = Vvoid_text_area_pointer;
23641 }
23642 goto set_cursor;
23643 }
23644
23645 pos = glyph->charpos;
23646 object = glyph->object;
23647 if (!STRINGP (object) && !BUFFERP (object))
23648 goto set_cursor;
23649
23650 /* If we get an out-of-range value, return now; avoid an error. */
23651 if (BUFFERP (object) && pos > BUF_Z (b))
23652 goto set_cursor;
23653
23654 /* Make the window's buffer temporarily current for
23655 overlays_at and compute_char_face. */
23656 obuf = current_buffer;
23657 current_buffer = b;
23658 obegv = BEGV;
23659 ozv = ZV;
23660 BEGV = BEG;
23661 ZV = Z;
23662
23663 /* Is this char mouse-active or does it have help-echo? */
23664 position = make_number (pos);
23665
23666 if (BUFFERP (object))
23667 {
23668 /* Put all the overlays we want in a vector in overlay_vec. */
23669 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23670 /* Sort overlays into increasing priority order. */
23671 noverlays = sort_overlays (overlay_vec, noverlays, w);
23672 }
23673 else
23674 noverlays = 0;
23675
23676 same_region = (EQ (window, dpyinfo->mouse_face_window)
23677 && vpos >= dpyinfo->mouse_face_beg_row
23678 && vpos <= dpyinfo->mouse_face_end_row
23679 && (vpos > dpyinfo->mouse_face_beg_row
23680 || hpos >= dpyinfo->mouse_face_beg_col)
23681 && (vpos < dpyinfo->mouse_face_end_row
23682 || hpos < dpyinfo->mouse_face_end_col
23683 || dpyinfo->mouse_face_past_end));
23684
23685 if (same_region)
23686 cursor = No_Cursor;
23687
23688 /* Check mouse-face highlighting. */
23689 if (! same_region
23690 /* If there exists an overlay with mouse-face overlapping
23691 the one we are currently highlighting, we have to
23692 check if we enter the overlapping overlay, and then
23693 highlight only that. */
23694 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23695 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23696 {
23697 /* Find the highest priority overlay with a mouse-face. */
23698 overlay = Qnil;
23699 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23700 {
23701 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23702 if (!NILP (mouse_face))
23703 overlay = overlay_vec[i];
23704 }
23705
23706 /* If we're highlighting the same overlay as before, there's
23707 no need to do that again. */
23708 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23709 goto check_help_echo;
23710 dpyinfo->mouse_face_overlay = overlay;
23711
23712 /* Clear the display of the old active region, if any. */
23713 if (clear_mouse_face (dpyinfo))
23714 cursor = No_Cursor;
23715
23716 /* If no overlay applies, get a text property. */
23717 if (NILP (overlay))
23718 mouse_face = Fget_text_property (position, Qmouse_face, object);
23719
23720 /* Next, compute the bounds of the mouse highlighting and
23721 display it. */
23722 if (!NILP (mouse_face) && STRINGP (object))
23723 {
23724 /* The mouse-highlighting comes from a display string
23725 with a mouse-face. */
23726 Lisp_Object b, e;
23727 EMACS_INT ignore;
23728
23729 b = Fprevious_single_property_change
23730 (make_number (pos + 1), Qmouse_face, object, Qnil);
23731 e = Fnext_single_property_change
23732 (position, Qmouse_face, object, Qnil);
23733 if (NILP (b))
23734 b = make_number (0);
23735 if (NILP (e))
23736 e = make_number (SCHARS (object) - 1);
23737
23738 fast_find_string_pos (w, XINT (b), object,
23739 &dpyinfo->mouse_face_beg_col,
23740 &dpyinfo->mouse_face_beg_row,
23741 &dpyinfo->mouse_face_beg_x,
23742 &dpyinfo->mouse_face_beg_y, 0);
23743 fast_find_string_pos (w, XINT (e), object,
23744 &dpyinfo->mouse_face_end_col,
23745 &dpyinfo->mouse_face_end_row,
23746 &dpyinfo->mouse_face_end_x,
23747 &dpyinfo->mouse_face_end_y, 1);
23748 dpyinfo->mouse_face_past_end = 0;
23749 dpyinfo->mouse_face_window = window;
23750 dpyinfo->mouse_face_face_id
23751 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23752 glyph->face_id, 1);
23753 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23754 cursor = No_Cursor;
23755 }
23756 else
23757 {
23758 /* The mouse-highlighting, if any, comes from an overlay
23759 or text property in the buffer. */
23760 Lisp_Object buffer, display_string;
23761
23762 if (STRINGP (object))
23763 {
23764 /* If we are on a display string with no mouse-face,
23765 check if the text under it has one. */
23766 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23767 int start = MATRIX_ROW_START_CHARPOS (r);
23768 pos = string_buffer_position (w, object, start);
23769 if (pos > 0)
23770 {
23771 mouse_face = get_char_property_and_overlay
23772 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23773 buffer = w->buffer;
23774 display_string = object;
23775 }
23776 }
23777 else
23778 {
23779 buffer = object;
23780 display_string = Qnil;
23781 }
23782
23783 if (!NILP (mouse_face))
23784 {
23785 Lisp_Object before, after;
23786 Lisp_Object before_string, after_string;
23787
23788 if (NILP (overlay))
23789 {
23790 /* Handle the text property case. */
23791 before = Fprevious_single_property_change
23792 (make_number (pos + 1), Qmouse_face, buffer,
23793 Fmarker_position (w->start));
23794 after = Fnext_single_property_change
23795 (make_number (pos), Qmouse_face, buffer,
23796 make_number (BUF_Z (XBUFFER (buffer))
23797 - XFASTINT (w->window_end_pos)));
23798 before_string = after_string = Qnil;
23799 }
23800 else
23801 {
23802 /* Handle the overlay case. */
23803 before = Foverlay_start (overlay);
23804 after = Foverlay_end (overlay);
23805 before_string = Foverlay_get (overlay, Qbefore_string);
23806 after_string = Foverlay_get (overlay, Qafter_string);
23807
23808 if (!STRINGP (before_string)) before_string = Qnil;
23809 if (!STRINGP (after_string)) after_string = Qnil;
23810 }
23811
23812 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23813 XFASTINT (before),
23814 XFASTINT (after),
23815 before_string, after_string,
23816 display_string);
23817 cursor = No_Cursor;
23818 }
23819 }
23820 }
23821
23822 check_help_echo:
23823
23824 /* Look for a `help-echo' property. */
23825 if (NILP (help_echo_string)) {
23826 Lisp_Object help, overlay;
23827
23828 /* Check overlays first. */
23829 help = overlay = Qnil;
23830 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23831 {
23832 overlay = overlay_vec[i];
23833 help = Foverlay_get (overlay, Qhelp_echo);
23834 }
23835
23836 if (!NILP (help))
23837 {
23838 help_echo_string = help;
23839 help_echo_window = window;
23840 help_echo_object = overlay;
23841 help_echo_pos = pos;
23842 }
23843 else
23844 {
23845 Lisp_Object object = glyph->object;
23846 int charpos = glyph->charpos;
23847
23848 /* Try text properties. */
23849 if (STRINGP (object)
23850 && charpos >= 0
23851 && charpos < SCHARS (object))
23852 {
23853 help = Fget_text_property (make_number (charpos),
23854 Qhelp_echo, object);
23855 if (NILP (help))
23856 {
23857 /* If the string itself doesn't specify a help-echo,
23858 see if the buffer text ``under'' it does. */
23859 struct glyph_row *r
23860 = MATRIX_ROW (w->current_matrix, vpos);
23861 int start = MATRIX_ROW_START_CHARPOS (r);
23862 int pos = string_buffer_position (w, object, start);
23863 if (pos > 0)
23864 {
23865 help = Fget_char_property (make_number (pos),
23866 Qhelp_echo, w->buffer);
23867 if (!NILP (help))
23868 {
23869 charpos = pos;
23870 object = w->buffer;
23871 }
23872 }
23873 }
23874 }
23875 else if (BUFFERP (object)
23876 && charpos >= BEGV
23877 && charpos < ZV)
23878 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23879 object);
23880
23881 if (!NILP (help))
23882 {
23883 help_echo_string = help;
23884 help_echo_window = window;
23885 help_echo_object = object;
23886 help_echo_pos = charpos;
23887 }
23888 }
23889 }
23890
23891 /* Look for a `pointer' property. */
23892 if (NILP (pointer))
23893 {
23894 /* Check overlays first. */
23895 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23896 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23897
23898 if (NILP (pointer))
23899 {
23900 Lisp_Object object = glyph->object;
23901 int charpos = glyph->charpos;
23902
23903 /* Try text properties. */
23904 if (STRINGP (object)
23905 && charpos >= 0
23906 && charpos < SCHARS (object))
23907 {
23908 pointer = Fget_text_property (make_number (charpos),
23909 Qpointer, object);
23910 if (NILP (pointer))
23911 {
23912 /* If the string itself doesn't specify a pointer,
23913 see if the buffer text ``under'' it does. */
23914 struct glyph_row *r
23915 = MATRIX_ROW (w->current_matrix, vpos);
23916 int start = MATRIX_ROW_START_CHARPOS (r);
23917 int pos = string_buffer_position (w, object, start);
23918 if (pos > 0)
23919 pointer = Fget_char_property (make_number (pos),
23920 Qpointer, w->buffer);
23921 }
23922 }
23923 else if (BUFFERP (object)
23924 && charpos >= BEGV
23925 && charpos < ZV)
23926 pointer = Fget_text_property (make_number (charpos),
23927 Qpointer, object);
23928 }
23929 }
23930
23931 BEGV = obegv;
23932 ZV = ozv;
23933 current_buffer = obuf;
23934 }
23935
23936 set_cursor:
23937
23938 define_frame_cursor1 (f, cursor, pointer);
23939 }
23940
23941
23942 /* EXPORT for RIF:
23943 Clear any mouse-face on window W. This function is part of the
23944 redisplay interface, and is called from try_window_id and similar
23945 functions to ensure the mouse-highlight is off. */
23946
23947 void
23948 x_clear_window_mouse_face (w)
23949 struct window *w;
23950 {
23951 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23952 Lisp_Object window;
23953
23954 BLOCK_INPUT;
23955 XSETWINDOW (window, w);
23956 if (EQ (window, dpyinfo->mouse_face_window))
23957 clear_mouse_face (dpyinfo);
23958 UNBLOCK_INPUT;
23959 }
23960
23961
23962 /* EXPORT:
23963 Just discard the mouse face information for frame F, if any.
23964 This is used when the size of F is changed. */
23965
23966 void
23967 cancel_mouse_face (f)
23968 struct frame *f;
23969 {
23970 Lisp_Object window;
23971 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23972
23973 window = dpyinfo->mouse_face_window;
23974 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23975 {
23976 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23977 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23978 dpyinfo->mouse_face_window = Qnil;
23979 }
23980 }
23981
23982
23983 #endif /* HAVE_WINDOW_SYSTEM */
23984
23985 \f
23986 /***********************************************************************
23987 Exposure Events
23988 ***********************************************************************/
23989
23990 #ifdef HAVE_WINDOW_SYSTEM
23991
23992 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23993 which intersects rectangle R. R is in window-relative coordinates. */
23994
23995 static void
23996 expose_area (w, row, r, area)
23997 struct window *w;
23998 struct glyph_row *row;
23999 XRectangle *r;
24000 enum glyph_row_area area;
24001 {
24002 struct glyph *first = row->glyphs[area];
24003 struct glyph *end = row->glyphs[area] + row->used[area];
24004 struct glyph *last;
24005 int first_x, start_x, x;
24006
24007 if (area == TEXT_AREA && row->fill_line_p)
24008 /* If row extends face to end of line write the whole line. */
24009 draw_glyphs (w, 0, row, area,
24010 0, row->used[area],
24011 DRAW_NORMAL_TEXT, 0);
24012 else
24013 {
24014 /* Set START_X to the window-relative start position for drawing glyphs of
24015 AREA. The first glyph of the text area can be partially visible.
24016 The first glyphs of other areas cannot. */
24017 start_x = window_box_left_offset (w, area);
24018 x = start_x;
24019 if (area == TEXT_AREA)
24020 x += row->x;
24021
24022 /* Find the first glyph that must be redrawn. */
24023 while (first < end
24024 && x + first->pixel_width < r->x)
24025 {
24026 x += first->pixel_width;
24027 ++first;
24028 }
24029
24030 /* Find the last one. */
24031 last = first;
24032 first_x = x;
24033 while (last < end
24034 && x < r->x + r->width)
24035 {
24036 x += last->pixel_width;
24037 ++last;
24038 }
24039
24040 /* Repaint. */
24041 if (last > first)
24042 draw_glyphs (w, first_x - start_x, row, area,
24043 first - row->glyphs[area], last - row->glyphs[area],
24044 DRAW_NORMAL_TEXT, 0);
24045 }
24046 }
24047
24048
24049 /* Redraw the parts of the glyph row ROW on window W intersecting
24050 rectangle R. R is in window-relative coordinates. Value is
24051 non-zero if mouse-face was overwritten. */
24052
24053 static int
24054 expose_line (w, row, r)
24055 struct window *w;
24056 struct glyph_row *row;
24057 XRectangle *r;
24058 {
24059 xassert (row->enabled_p);
24060
24061 if (row->mode_line_p || w->pseudo_window_p)
24062 draw_glyphs (w, 0, row, TEXT_AREA,
24063 0, row->used[TEXT_AREA],
24064 DRAW_NORMAL_TEXT, 0);
24065 else
24066 {
24067 if (row->used[LEFT_MARGIN_AREA])
24068 expose_area (w, row, r, LEFT_MARGIN_AREA);
24069 if (row->used[TEXT_AREA])
24070 expose_area (w, row, r, TEXT_AREA);
24071 if (row->used[RIGHT_MARGIN_AREA])
24072 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24073 draw_row_fringe_bitmaps (w, row);
24074 }
24075
24076 return row->mouse_face_p;
24077 }
24078
24079
24080 /* Redraw those parts of glyphs rows during expose event handling that
24081 overlap other rows. Redrawing of an exposed line writes over parts
24082 of lines overlapping that exposed line; this function fixes that.
24083
24084 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24085 row in W's current matrix that is exposed and overlaps other rows.
24086 LAST_OVERLAPPING_ROW is the last such row. */
24087
24088 static void
24089 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24090 struct window *w;
24091 struct glyph_row *first_overlapping_row;
24092 struct glyph_row *last_overlapping_row;
24093 XRectangle *r;
24094 {
24095 struct glyph_row *row;
24096
24097 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24098 if (row->overlapping_p)
24099 {
24100 xassert (row->enabled_p && !row->mode_line_p);
24101
24102 row->clip = r;
24103 if (row->used[LEFT_MARGIN_AREA])
24104 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24105
24106 if (row->used[TEXT_AREA])
24107 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24108
24109 if (row->used[RIGHT_MARGIN_AREA])
24110 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24111 row->clip = NULL;
24112 }
24113 }
24114
24115
24116 /* Return non-zero if W's cursor intersects rectangle R. */
24117
24118 static int
24119 phys_cursor_in_rect_p (w, r)
24120 struct window *w;
24121 XRectangle *r;
24122 {
24123 XRectangle cr, result;
24124 struct glyph *cursor_glyph;
24125 struct glyph_row *row;
24126
24127 if (w->phys_cursor.vpos >= 0
24128 && w->phys_cursor.vpos < w->current_matrix->nrows
24129 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24130 row->enabled_p)
24131 && row->cursor_in_fringe_p)
24132 {
24133 /* Cursor is in the fringe. */
24134 cr.x = window_box_right_offset (w,
24135 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24136 ? RIGHT_MARGIN_AREA
24137 : TEXT_AREA));
24138 cr.y = row->y;
24139 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24140 cr.height = row->height;
24141 return x_intersect_rectangles (&cr, r, &result);
24142 }
24143
24144 cursor_glyph = get_phys_cursor_glyph (w);
24145 if (cursor_glyph)
24146 {
24147 /* r is relative to W's box, but w->phys_cursor.x is relative
24148 to left edge of W's TEXT area. Adjust it. */
24149 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24150 cr.y = w->phys_cursor.y;
24151 cr.width = cursor_glyph->pixel_width;
24152 cr.height = w->phys_cursor_height;
24153 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24154 I assume the effect is the same -- and this is portable. */
24155 return x_intersect_rectangles (&cr, r, &result);
24156 }
24157 /* If we don't understand the format, pretend we're not in the hot-spot. */
24158 return 0;
24159 }
24160
24161
24162 /* EXPORT:
24163 Draw a vertical window border to the right of window W if W doesn't
24164 have vertical scroll bars. */
24165
24166 void
24167 x_draw_vertical_border (w)
24168 struct window *w;
24169 {
24170 struct frame *f = XFRAME (WINDOW_FRAME (w));
24171
24172 /* We could do better, if we knew what type of scroll-bar the adjacent
24173 windows (on either side) have... But we don't :-(
24174 However, I think this works ok. ++KFS 2003-04-25 */
24175
24176 /* Redraw borders between horizontally adjacent windows. Don't
24177 do it for frames with vertical scroll bars because either the
24178 right scroll bar of a window, or the left scroll bar of its
24179 neighbor will suffice as a border. */
24180 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24181 return;
24182
24183 if (!WINDOW_RIGHTMOST_P (w)
24184 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24185 {
24186 int x0, x1, y0, y1;
24187
24188 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24189 y1 -= 1;
24190
24191 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24192 x1 -= 1;
24193
24194 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24195 }
24196 else if (!WINDOW_LEFTMOST_P (w)
24197 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24198 {
24199 int x0, x1, y0, y1;
24200
24201 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24202 y1 -= 1;
24203
24204 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24205 x0 -= 1;
24206
24207 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24208 }
24209 }
24210
24211
24212 /* Redraw the part of window W intersection rectangle FR. Pixel
24213 coordinates in FR are frame-relative. Call this function with
24214 input blocked. Value is non-zero if the exposure overwrites
24215 mouse-face. */
24216
24217 static int
24218 expose_window (w, fr)
24219 struct window *w;
24220 XRectangle *fr;
24221 {
24222 struct frame *f = XFRAME (w->frame);
24223 XRectangle wr, r;
24224 int mouse_face_overwritten_p = 0;
24225
24226 /* If window is not yet fully initialized, do nothing. This can
24227 happen when toolkit scroll bars are used and a window is split.
24228 Reconfiguring the scroll bar will generate an expose for a newly
24229 created window. */
24230 if (w->current_matrix == NULL)
24231 return 0;
24232
24233 /* When we're currently updating the window, display and current
24234 matrix usually don't agree. Arrange for a thorough display
24235 later. */
24236 if (w == updated_window)
24237 {
24238 SET_FRAME_GARBAGED (f);
24239 return 0;
24240 }
24241
24242 /* Frame-relative pixel rectangle of W. */
24243 wr.x = WINDOW_LEFT_EDGE_X (w);
24244 wr.y = WINDOW_TOP_EDGE_Y (w);
24245 wr.width = WINDOW_TOTAL_WIDTH (w);
24246 wr.height = WINDOW_TOTAL_HEIGHT (w);
24247
24248 if (x_intersect_rectangles (fr, &wr, &r))
24249 {
24250 int yb = window_text_bottom_y (w);
24251 struct glyph_row *row;
24252 int cursor_cleared_p;
24253 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24254
24255 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24256 r.x, r.y, r.width, r.height));
24257
24258 /* Convert to window coordinates. */
24259 r.x -= WINDOW_LEFT_EDGE_X (w);
24260 r.y -= WINDOW_TOP_EDGE_Y (w);
24261
24262 /* Turn off the cursor. */
24263 if (!w->pseudo_window_p
24264 && phys_cursor_in_rect_p (w, &r))
24265 {
24266 x_clear_cursor (w);
24267 cursor_cleared_p = 1;
24268 }
24269 else
24270 cursor_cleared_p = 0;
24271
24272 /* Update lines intersecting rectangle R. */
24273 first_overlapping_row = last_overlapping_row = NULL;
24274 for (row = w->current_matrix->rows;
24275 row->enabled_p;
24276 ++row)
24277 {
24278 int y0 = row->y;
24279 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24280
24281 if ((y0 >= r.y && y0 < r.y + r.height)
24282 || (y1 > r.y && y1 < r.y + r.height)
24283 || (r.y >= y0 && r.y < y1)
24284 || (r.y + r.height > y0 && r.y + r.height < y1))
24285 {
24286 /* A header line may be overlapping, but there is no need
24287 to fix overlapping areas for them. KFS 2005-02-12 */
24288 if (row->overlapping_p && !row->mode_line_p)
24289 {
24290 if (first_overlapping_row == NULL)
24291 first_overlapping_row = row;
24292 last_overlapping_row = row;
24293 }
24294
24295 row->clip = fr;
24296 if (expose_line (w, row, &r))
24297 mouse_face_overwritten_p = 1;
24298 row->clip = NULL;
24299 }
24300 else if (row->overlapping_p)
24301 {
24302 /* We must redraw a row overlapping the exposed area. */
24303 if (y0 < r.y
24304 ? y0 + row->phys_height > r.y
24305 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24306 {
24307 if (first_overlapping_row == NULL)
24308 first_overlapping_row = row;
24309 last_overlapping_row = row;
24310 }
24311 }
24312
24313 if (y1 >= yb)
24314 break;
24315 }
24316
24317 /* Display the mode line if there is one. */
24318 if (WINDOW_WANTS_MODELINE_P (w)
24319 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24320 row->enabled_p)
24321 && row->y < r.y + r.height)
24322 {
24323 if (expose_line (w, row, &r))
24324 mouse_face_overwritten_p = 1;
24325 }
24326
24327 if (!w->pseudo_window_p)
24328 {
24329 /* Fix the display of overlapping rows. */
24330 if (first_overlapping_row)
24331 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24332 fr);
24333
24334 /* Draw border between windows. */
24335 x_draw_vertical_border (w);
24336
24337 /* Turn the cursor on again. */
24338 if (cursor_cleared_p)
24339 update_window_cursor (w, 1);
24340 }
24341 }
24342
24343 return mouse_face_overwritten_p;
24344 }
24345
24346
24347
24348 /* Redraw (parts) of all windows in the window tree rooted at W that
24349 intersect R. R contains frame pixel coordinates. Value is
24350 non-zero if the exposure overwrites mouse-face. */
24351
24352 static int
24353 expose_window_tree (w, r)
24354 struct window *w;
24355 XRectangle *r;
24356 {
24357 struct frame *f = XFRAME (w->frame);
24358 int mouse_face_overwritten_p = 0;
24359
24360 while (w && !FRAME_GARBAGED_P (f))
24361 {
24362 if (!NILP (w->hchild))
24363 mouse_face_overwritten_p
24364 |= expose_window_tree (XWINDOW (w->hchild), r);
24365 else if (!NILP (w->vchild))
24366 mouse_face_overwritten_p
24367 |= expose_window_tree (XWINDOW (w->vchild), r);
24368 else
24369 mouse_face_overwritten_p |= expose_window (w, r);
24370
24371 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24372 }
24373
24374 return mouse_face_overwritten_p;
24375 }
24376
24377
24378 /* EXPORT:
24379 Redisplay an exposed area of frame F. X and Y are the upper-left
24380 corner of the exposed rectangle. W and H are width and height of
24381 the exposed area. All are pixel values. W or H zero means redraw
24382 the entire frame. */
24383
24384 void
24385 expose_frame (f, x, y, w, h)
24386 struct frame *f;
24387 int x, y, w, h;
24388 {
24389 XRectangle r;
24390 int mouse_face_overwritten_p = 0;
24391
24392 TRACE ((stderr, "expose_frame "));
24393
24394 /* No need to redraw if frame will be redrawn soon. */
24395 if (FRAME_GARBAGED_P (f))
24396 {
24397 TRACE ((stderr, " garbaged\n"));
24398 return;
24399 }
24400
24401 /* If basic faces haven't been realized yet, there is no point in
24402 trying to redraw anything. This can happen when we get an expose
24403 event while Emacs is starting, e.g. by moving another window. */
24404 if (FRAME_FACE_CACHE (f) == NULL
24405 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24406 {
24407 TRACE ((stderr, " no faces\n"));
24408 return;
24409 }
24410
24411 if (w == 0 || h == 0)
24412 {
24413 r.x = r.y = 0;
24414 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24415 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24416 }
24417 else
24418 {
24419 r.x = x;
24420 r.y = y;
24421 r.width = w;
24422 r.height = h;
24423 }
24424
24425 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24426 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24427
24428 if (WINDOWP (f->tool_bar_window))
24429 mouse_face_overwritten_p
24430 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24431
24432 #ifdef HAVE_X_WINDOWS
24433 #ifndef MSDOS
24434 #ifndef USE_X_TOOLKIT
24435 if (WINDOWP (f->menu_bar_window))
24436 mouse_face_overwritten_p
24437 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24438 #endif /* not USE_X_TOOLKIT */
24439 #endif
24440 #endif
24441
24442 /* Some window managers support a focus-follows-mouse style with
24443 delayed raising of frames. Imagine a partially obscured frame,
24444 and moving the mouse into partially obscured mouse-face on that
24445 frame. The visible part of the mouse-face will be highlighted,
24446 then the WM raises the obscured frame. With at least one WM, KDE
24447 2.1, Emacs is not getting any event for the raising of the frame
24448 (even tried with SubstructureRedirectMask), only Expose events.
24449 These expose events will draw text normally, i.e. not
24450 highlighted. Which means we must redo the highlight here.
24451 Subsume it under ``we love X''. --gerd 2001-08-15 */
24452 /* Included in Windows version because Windows most likely does not
24453 do the right thing if any third party tool offers
24454 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24455 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24456 {
24457 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24458 if (f == dpyinfo->mouse_face_mouse_frame)
24459 {
24460 int x = dpyinfo->mouse_face_mouse_x;
24461 int y = dpyinfo->mouse_face_mouse_y;
24462 clear_mouse_face (dpyinfo);
24463 note_mouse_highlight (f, x, y);
24464 }
24465 }
24466 }
24467
24468
24469 /* EXPORT:
24470 Determine the intersection of two rectangles R1 and R2. Return
24471 the intersection in *RESULT. Value is non-zero if RESULT is not
24472 empty. */
24473
24474 int
24475 x_intersect_rectangles (r1, r2, result)
24476 XRectangle *r1, *r2, *result;
24477 {
24478 XRectangle *left, *right;
24479 XRectangle *upper, *lower;
24480 int intersection_p = 0;
24481
24482 /* Rearrange so that R1 is the left-most rectangle. */
24483 if (r1->x < r2->x)
24484 left = r1, right = r2;
24485 else
24486 left = r2, right = r1;
24487
24488 /* X0 of the intersection is right.x0, if this is inside R1,
24489 otherwise there is no intersection. */
24490 if (right->x <= left->x + left->width)
24491 {
24492 result->x = right->x;
24493
24494 /* The right end of the intersection is the minimum of the
24495 the right ends of left and right. */
24496 result->width = (min (left->x + left->width, right->x + right->width)
24497 - result->x);
24498
24499 /* Same game for Y. */
24500 if (r1->y < r2->y)
24501 upper = r1, lower = r2;
24502 else
24503 upper = r2, lower = r1;
24504
24505 /* The upper end of the intersection is lower.y0, if this is inside
24506 of upper. Otherwise, there is no intersection. */
24507 if (lower->y <= upper->y + upper->height)
24508 {
24509 result->y = lower->y;
24510
24511 /* The lower end of the intersection is the minimum of the lower
24512 ends of upper and lower. */
24513 result->height = (min (lower->y + lower->height,
24514 upper->y + upper->height)
24515 - result->y);
24516 intersection_p = 1;
24517 }
24518 }
24519
24520 return intersection_p;
24521 }
24522
24523 #endif /* HAVE_WINDOW_SYSTEM */
24524
24525 \f
24526 /***********************************************************************
24527 Initialization
24528 ***********************************************************************/
24529
24530 void
24531 syms_of_xdisp ()
24532 {
24533 Vwith_echo_area_save_vector = Qnil;
24534 staticpro (&Vwith_echo_area_save_vector);
24535
24536 Vmessage_stack = Qnil;
24537 staticpro (&Vmessage_stack);
24538
24539 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
24540 staticpro (&Qinhibit_redisplay);
24541
24542 message_dolog_marker1 = Fmake_marker ();
24543 staticpro (&message_dolog_marker1);
24544 message_dolog_marker2 = Fmake_marker ();
24545 staticpro (&message_dolog_marker2);
24546 message_dolog_marker3 = Fmake_marker ();
24547 staticpro (&message_dolog_marker3);
24548
24549 #if GLYPH_DEBUG
24550 defsubr (&Sdump_frame_glyph_matrix);
24551 defsubr (&Sdump_glyph_matrix);
24552 defsubr (&Sdump_glyph_row);
24553 defsubr (&Sdump_tool_bar_row);
24554 defsubr (&Strace_redisplay);
24555 defsubr (&Strace_to_stderr);
24556 #endif
24557 #ifdef HAVE_WINDOW_SYSTEM
24558 defsubr (&Stool_bar_lines_needed);
24559 defsubr (&Slookup_image_map);
24560 #endif
24561 defsubr (&Sformat_mode_line);
24562 defsubr (&Sinvisible_p);
24563
24564 staticpro (&Qmenu_bar_update_hook);
24565 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
24566
24567 staticpro (&Qoverriding_terminal_local_map);
24568 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
24569
24570 staticpro (&Qoverriding_local_map);
24571 Qoverriding_local_map = intern_c_string ("overriding-local-map");
24572
24573 staticpro (&Qwindow_scroll_functions);
24574 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
24575
24576 staticpro (&Qwindow_text_change_functions);
24577 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
24578
24579 staticpro (&Qredisplay_end_trigger_functions);
24580 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
24581
24582 staticpro (&Qinhibit_point_motion_hooks);
24583 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
24584
24585 Qeval = intern_c_string ("eval");
24586 staticpro (&Qeval);
24587
24588 QCdata = intern_c_string (":data");
24589 staticpro (&QCdata);
24590 Qdisplay = intern_c_string ("display");
24591 staticpro (&Qdisplay);
24592 Qspace_width = intern_c_string ("space-width");
24593 staticpro (&Qspace_width);
24594 Qraise = intern_c_string ("raise");
24595 staticpro (&Qraise);
24596 Qslice = intern_c_string ("slice");
24597 staticpro (&Qslice);
24598 Qspace = intern_c_string ("space");
24599 staticpro (&Qspace);
24600 Qmargin = intern_c_string ("margin");
24601 staticpro (&Qmargin);
24602 Qpointer = intern_c_string ("pointer");
24603 staticpro (&Qpointer);
24604 Qleft_margin = intern_c_string ("left-margin");
24605 staticpro (&Qleft_margin);
24606 Qright_margin = intern_c_string ("right-margin");
24607 staticpro (&Qright_margin);
24608 Qcenter = intern_c_string ("center");
24609 staticpro (&Qcenter);
24610 Qline_height = intern_c_string ("line-height");
24611 staticpro (&Qline_height);
24612 QCalign_to = intern_c_string (":align-to");
24613 staticpro (&QCalign_to);
24614 QCrelative_width = intern_c_string (":relative-width");
24615 staticpro (&QCrelative_width);
24616 QCrelative_height = intern_c_string (":relative-height");
24617 staticpro (&QCrelative_height);
24618 QCeval = intern_c_string (":eval");
24619 staticpro (&QCeval);
24620 QCpropertize = intern_c_string (":propertize");
24621 staticpro (&QCpropertize);
24622 QCfile = intern_c_string (":file");
24623 staticpro (&QCfile);
24624 Qfontified = intern_c_string ("fontified");
24625 staticpro (&Qfontified);
24626 Qfontification_functions = intern_c_string ("fontification-functions");
24627 staticpro (&Qfontification_functions);
24628 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
24629 staticpro (&Qtrailing_whitespace);
24630 Qescape_glyph = intern_c_string ("escape-glyph");
24631 staticpro (&Qescape_glyph);
24632 Qnobreak_space = intern_c_string ("nobreak-space");
24633 staticpro (&Qnobreak_space);
24634 Qimage = intern_c_string ("image");
24635 staticpro (&Qimage);
24636 QCmap = intern_c_string (":map");
24637 staticpro (&QCmap);
24638 QCpointer = intern_c_string (":pointer");
24639 staticpro (&QCpointer);
24640 Qrect = intern_c_string ("rect");
24641 staticpro (&Qrect);
24642 Qcircle = intern_c_string ("circle");
24643 staticpro (&Qcircle);
24644 Qpoly = intern_c_string ("poly");
24645 staticpro (&Qpoly);
24646 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
24647 staticpro (&Qmessage_truncate_lines);
24648 Qgrow_only = intern_c_string ("grow-only");
24649 staticpro (&Qgrow_only);
24650 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
24651 staticpro (&Qinhibit_menubar_update);
24652 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
24653 staticpro (&Qinhibit_eval_during_redisplay);
24654 Qposition = intern_c_string ("position");
24655 staticpro (&Qposition);
24656 Qbuffer_position = intern_c_string ("buffer-position");
24657 staticpro (&Qbuffer_position);
24658 Qobject = intern_c_string ("object");
24659 staticpro (&Qobject);
24660 Qbar = intern_c_string ("bar");
24661 staticpro (&Qbar);
24662 Qhbar = intern_c_string ("hbar");
24663 staticpro (&Qhbar);
24664 Qbox = intern_c_string ("box");
24665 staticpro (&Qbox);
24666 Qhollow = intern_c_string ("hollow");
24667 staticpro (&Qhollow);
24668 Qhand = intern_c_string ("hand");
24669 staticpro (&Qhand);
24670 Qarrow = intern_c_string ("arrow");
24671 staticpro (&Qarrow);
24672 Qtext = intern_c_string ("text");
24673 staticpro (&Qtext);
24674 Qrisky_local_variable = intern_c_string ("risky-local-variable");
24675 staticpro (&Qrisky_local_variable);
24676 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
24677 staticpro (&Qinhibit_free_realized_faces);
24678
24679 list_of_error = Fcons (Fcons (intern_c_string ("error"),
24680 Fcons (intern_c_string ("void-variable"), Qnil)),
24681 Qnil);
24682 staticpro (&list_of_error);
24683
24684 Qlast_arrow_position = intern_c_string ("last-arrow-position");
24685 staticpro (&Qlast_arrow_position);
24686 Qlast_arrow_string = intern_c_string ("last-arrow-string");
24687 staticpro (&Qlast_arrow_string);
24688
24689 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
24690 staticpro (&Qoverlay_arrow_string);
24691 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
24692 staticpro (&Qoverlay_arrow_bitmap);
24693
24694 echo_buffer[0] = echo_buffer[1] = Qnil;
24695 staticpro (&echo_buffer[0]);
24696 staticpro (&echo_buffer[1]);
24697
24698 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24699 staticpro (&echo_area_buffer[0]);
24700 staticpro (&echo_area_buffer[1]);
24701
24702 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
24703 staticpro (&Vmessages_buffer_name);
24704
24705 mode_line_proptrans_alist = Qnil;
24706 staticpro (&mode_line_proptrans_alist);
24707 mode_line_string_list = Qnil;
24708 staticpro (&mode_line_string_list);
24709 mode_line_string_face = Qnil;
24710 staticpro (&mode_line_string_face);
24711 mode_line_string_face_prop = Qnil;
24712 staticpro (&mode_line_string_face_prop);
24713 Vmode_line_unwind_vector = Qnil;
24714 staticpro (&Vmode_line_unwind_vector);
24715
24716 help_echo_string = Qnil;
24717 staticpro (&help_echo_string);
24718 help_echo_object = Qnil;
24719 staticpro (&help_echo_object);
24720 help_echo_window = Qnil;
24721 staticpro (&help_echo_window);
24722 previous_help_echo_string = Qnil;
24723 staticpro (&previous_help_echo_string);
24724 help_echo_pos = -1;
24725
24726 #ifdef HAVE_WINDOW_SYSTEM
24727 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24728 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24729 For example, if a block cursor is over a tab, it will be drawn as
24730 wide as that tab on the display. */);
24731 x_stretch_cursor_p = 0;
24732 #endif
24733
24734 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24735 doc: /* *Non-nil means highlight trailing whitespace.
24736 The face used for trailing whitespace is `trailing-whitespace'. */);
24737 Vshow_trailing_whitespace = Qnil;
24738
24739 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24740 doc: /* *Control highlighting of nobreak space and soft hyphen.
24741 A value of t means highlight the character itself (for nobreak space,
24742 use face `nobreak-space').
24743 A value of nil means no highlighting.
24744 Other values mean display the escape glyph followed by an ordinary
24745 space or ordinary hyphen. */);
24746 Vnobreak_char_display = Qt;
24747
24748 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24749 doc: /* *The pointer shape to show in void text areas.
24750 A value of nil means to show the text pointer. Other options are `arrow',
24751 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24752 Vvoid_text_area_pointer = Qarrow;
24753
24754 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24755 doc: /* Non-nil means don't actually do any redisplay.
24756 This is used for internal purposes. */);
24757 Vinhibit_redisplay = Qnil;
24758
24759 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24760 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24761 Vglobal_mode_string = Qnil;
24762
24763 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24764 doc: /* Marker for where to display an arrow on top of the buffer text.
24765 This must be the beginning of a line in order to work.
24766 See also `overlay-arrow-string'. */);
24767 Voverlay_arrow_position = Qnil;
24768
24769 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24770 doc: /* String to display as an arrow in non-window frames.
24771 See also `overlay-arrow-position'. */);
24772 Voverlay_arrow_string = make_pure_c_string ("=>");
24773
24774 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24775 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24776 The symbols on this list are examined during redisplay to determine
24777 where to display overlay arrows. */);
24778 Voverlay_arrow_variable_list
24779 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
24780
24781 DEFVAR_INT ("scroll-step", &scroll_step,
24782 doc: /* *The number of lines to try scrolling a window by when point moves out.
24783 If that fails to bring point back on frame, point is centered instead.
24784 If this is zero, point is always centered after it moves off frame.
24785 If you want scrolling to always be a line at a time, you should set
24786 `scroll-conservatively' to a large value rather than set this to 1. */);
24787
24788 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24789 doc: /* *Scroll up to this many lines, to bring point back on screen.
24790 If point moves off-screen, redisplay will scroll by up to
24791 `scroll-conservatively' lines in order to bring point just barely
24792 onto the screen again. If that cannot be done, then redisplay
24793 recenters point as usual.
24794
24795 A value of zero means always recenter point if it moves off screen. */);
24796 scroll_conservatively = 0;
24797
24798 DEFVAR_INT ("scroll-margin", &scroll_margin,
24799 doc: /* *Number of lines of margin at the top and bottom of a window.
24800 Recenter the window whenever point gets within this many lines
24801 of the top or bottom of the window. */);
24802 scroll_margin = 0;
24803
24804 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24805 doc: /* Pixels per inch value for non-window system displays.
24806 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24807 Vdisplay_pixels_per_inch = make_float (72.0);
24808
24809 #if GLYPH_DEBUG
24810 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24811 #endif
24812
24813 DEFVAR_LISP ("truncate-partial-width-windows",
24814 &Vtruncate_partial_width_windows,
24815 doc: /* Non-nil means truncate lines in windows narrower than the frame.
24816 For an integer value, truncate lines in each window narrower than the
24817 full frame width, provided the window width is less than that integer;
24818 otherwise, respect the value of `truncate-lines'.
24819
24820 For any other non-nil value, truncate lines in all windows that do
24821 not span the full frame width.
24822
24823 A value of nil means to respect the value of `truncate-lines'.
24824
24825 If `word-wrap' is enabled, you might want to reduce this. */);
24826 Vtruncate_partial_width_windows = make_number (50);
24827
24828 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24829 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24830 Any other value means to use the appropriate face, `mode-line',
24831 `header-line', or `menu' respectively. */);
24832 mode_line_inverse_video = 1;
24833
24834 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24835 doc: /* *Maximum buffer size for which line number should be displayed.
24836 If the buffer is bigger than this, the line number does not appear
24837 in the mode line. A value of nil means no limit. */);
24838 Vline_number_display_limit = Qnil;
24839
24840 DEFVAR_INT ("line-number-display-limit-width",
24841 &line_number_display_limit_width,
24842 doc: /* *Maximum line width (in characters) for line number display.
24843 If the average length of the lines near point is bigger than this, then the
24844 line number may be omitted from the mode line. */);
24845 line_number_display_limit_width = 200;
24846
24847 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24848 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24849 highlight_nonselected_windows = 0;
24850
24851 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24852 doc: /* Non-nil if more than one frame is visible on this display.
24853 Minibuffer-only frames don't count, but iconified frames do.
24854 This variable is not guaranteed to be accurate except while processing
24855 `frame-title-format' and `icon-title-format'. */);
24856
24857 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24858 doc: /* Template for displaying the title bar of visible frames.
24859 \(Assuming the window manager supports this feature.)
24860
24861 This variable has the same structure as `mode-line-format', except that
24862 the %c and %l constructs are ignored. It is used only on frames for
24863 which no explicit name has been set \(see `modify-frame-parameters'). */);
24864
24865 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24866 doc: /* Template for displaying the title bar of an iconified frame.
24867 \(Assuming the window manager supports this feature.)
24868 This variable has the same structure as `mode-line-format' (which see),
24869 and is used only on frames for which no explicit name has been set
24870 \(see `modify-frame-parameters'). */);
24871 Vicon_title_format
24872 = Vframe_title_format
24873 = pure_cons (intern_c_string ("multiple-frames"),
24874 pure_cons (make_pure_c_string ("%b"),
24875 pure_cons (pure_cons (empty_unibyte_string,
24876 pure_cons (intern_c_string ("invocation-name"),
24877 pure_cons (make_pure_c_string ("@"),
24878 pure_cons (intern_c_string ("system-name"),
24879 Qnil)))),
24880 Qnil)));
24881
24882 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24883 doc: /* Maximum number of lines to keep in the message log buffer.
24884 If nil, disable message logging. If t, log messages but don't truncate
24885 the buffer when it becomes large. */);
24886 Vmessage_log_max = make_number (100);
24887
24888 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24889 doc: /* Functions called before redisplay, if window sizes have changed.
24890 The value should be a list of functions that take one argument.
24891 Just before redisplay, for each frame, if any of its windows have changed
24892 size since the last redisplay, or have been split or deleted,
24893 all the functions in the list are called, with the frame as argument. */);
24894 Vwindow_size_change_functions = Qnil;
24895
24896 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24897 doc: /* List of functions to call before redisplaying a window with scrolling.
24898 Each function is called with two arguments, the window and its new
24899 display-start position. Note that these functions are also called by
24900 `set-window-buffer'. Also note that the value of `window-end' is not
24901 valid when these functions are called. */);
24902 Vwindow_scroll_functions = Qnil;
24903
24904 DEFVAR_LISP ("window-text-change-functions",
24905 &Vwindow_text_change_functions,
24906 doc: /* Functions to call in redisplay when text in the window might change. */);
24907 Vwindow_text_change_functions = Qnil;
24908
24909 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24910 doc: /* Functions called when redisplay of a window reaches the end trigger.
24911 Each function is called with two arguments, the window and the end trigger value.
24912 See `set-window-redisplay-end-trigger'. */);
24913 Vredisplay_end_trigger_functions = Qnil;
24914
24915 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24916 doc: /* *Non-nil means autoselect window with mouse pointer.
24917 If nil, do not autoselect windows.
24918 A positive number means delay autoselection by that many seconds: a
24919 window is autoselected only after the mouse has remained in that
24920 window for the duration of the delay.
24921 A negative number has a similar effect, but causes windows to be
24922 autoselected only after the mouse has stopped moving. \(Because of
24923 the way Emacs compares mouse events, you will occasionally wait twice
24924 that time before the window gets selected.\)
24925 Any other value means to autoselect window instantaneously when the
24926 mouse pointer enters it.
24927
24928 Autoselection selects the minibuffer only if it is active, and never
24929 unselects the minibuffer if it is active.
24930
24931 When customizing this variable make sure that the actual value of
24932 `focus-follows-mouse' matches the behavior of your window manager. */);
24933 Vmouse_autoselect_window = Qnil;
24934
24935 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24936 doc: /* *Non-nil means automatically resize tool-bars.
24937 This dynamically changes the tool-bar's height to the minimum height
24938 that is needed to make all tool-bar items visible.
24939 If value is `grow-only', the tool-bar's height is only increased
24940 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24941 Vauto_resize_tool_bars = Qt;
24942
24943 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24944 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24945 auto_raise_tool_bar_buttons_p = 1;
24946
24947 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24948 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24949 make_cursor_line_fully_visible_p = 1;
24950
24951 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24952 doc: /* *Border below tool-bar in pixels.
24953 If an integer, use it as the height of the border.
24954 If it is one of `internal-border-width' or `border-width', use the
24955 value of the corresponding frame parameter.
24956 Otherwise, no border is added below the tool-bar. */);
24957 Vtool_bar_border = Qinternal_border_width;
24958
24959 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24960 doc: /* *Margin around tool-bar buttons in pixels.
24961 If an integer, use that for both horizontal and vertical margins.
24962 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24963 HORZ specifying the horizontal margin, and VERT specifying the
24964 vertical margin. */);
24965 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24966
24967 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24968 doc: /* *Relief thickness of tool-bar buttons. */);
24969 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24970
24971 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24972 doc: /* List of functions to call to fontify regions of text.
24973 Each function is called with one argument POS. Functions must
24974 fontify a region starting at POS in the current buffer, and give
24975 fontified regions the property `fontified'. */);
24976 Vfontification_functions = Qnil;
24977 Fmake_variable_buffer_local (Qfontification_functions);
24978
24979 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24980 &unibyte_display_via_language_environment,
24981 doc: /* *Non-nil means display unibyte text according to language environment.
24982 Specifically, this means that raw bytes in the range 160-255 decimal
24983 are displayed by converting them to the equivalent multibyte characters
24984 according to the current language environment. As a result, they are
24985 displayed according to the current fontset.
24986
24987 Note that this variable affects only how these bytes are displayed,
24988 but does not change the fact they are interpreted as raw bytes. */);
24989 unibyte_display_via_language_environment = 0;
24990
24991 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24992 doc: /* *Maximum height for resizing mini-windows.
24993 If a float, it specifies a fraction of the mini-window frame's height.
24994 If an integer, it specifies a number of lines. */);
24995 Vmax_mini_window_height = make_float (0.25);
24996
24997 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24998 doc: /* *How to resize mini-windows.
24999 A value of nil means don't automatically resize mini-windows.
25000 A value of t means resize them to fit the text displayed in them.
25001 A value of `grow-only', the default, means let mini-windows grow
25002 only, until their display becomes empty, at which point the windows
25003 go back to their normal size. */);
25004 Vresize_mini_windows = Qgrow_only;
25005
25006 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25007 doc: /* Alist specifying how to blink the cursor off.
25008 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25009 `cursor-type' frame-parameter or variable equals ON-STATE,
25010 comparing using `equal', Emacs uses OFF-STATE to specify
25011 how to blink it off. ON-STATE and OFF-STATE are values for
25012 the `cursor-type' frame parameter.
25013
25014 If a frame's ON-STATE has no entry in this list,
25015 the frame's other specifications determine how to blink the cursor off. */);
25016 Vblink_cursor_alist = Qnil;
25017
25018 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25019 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25020 automatic_hscrolling_p = 1;
25021 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25022 staticpro (&Qauto_hscroll_mode);
25023
25024 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25025 doc: /* *How many columns away from the window edge point is allowed to get
25026 before automatic hscrolling will horizontally scroll the window. */);
25027 hscroll_margin = 5;
25028
25029 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25030 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25031 When point is less than `hscroll-margin' columns from the window
25032 edge, automatic hscrolling will scroll the window by the amount of columns
25033 determined by this variable. If its value is a positive integer, scroll that
25034 many columns. If it's a positive floating-point number, it specifies the
25035 fraction of the window's width to scroll. If it's nil or zero, point will be
25036 centered horizontally after the scroll. Any other value, including negative
25037 numbers, are treated as if the value were zero.
25038
25039 Automatic hscrolling always moves point outside the scroll margin, so if
25040 point was more than scroll step columns inside the margin, the window will
25041 scroll more than the value given by the scroll step.
25042
25043 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25044 and `scroll-right' overrides this variable's effect. */);
25045 Vhscroll_step = make_number (0);
25046
25047 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25048 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25049 Bind this around calls to `message' to let it take effect. */);
25050 message_truncate_lines = 0;
25051
25052 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25053 doc: /* Normal hook run to update the menu bar definitions.
25054 Redisplay runs this hook before it redisplays the menu bar.
25055 This is used to update submenus such as Buffers,
25056 whose contents depend on various data. */);
25057 Vmenu_bar_update_hook = Qnil;
25058
25059 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25060 doc: /* Frame for which we are updating a menu.
25061 The enable predicate for a menu binding should check this variable. */);
25062 Vmenu_updating_frame = Qnil;
25063
25064 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25065 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25066 inhibit_menubar_update = 0;
25067
25068 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25069 doc: /* Prefix prepended to all continuation lines at display time.
25070 The value may be a string, an image, or a stretch-glyph; it is
25071 interpreted in the same way as the value of a `display' text property.
25072
25073 This variable is overridden by any `wrap-prefix' text or overlay
25074 property.
25075
25076 To add a prefix to non-continuation lines, use `line-prefix'. */);
25077 Vwrap_prefix = Qnil;
25078 staticpro (&Qwrap_prefix);
25079 Qwrap_prefix = intern_c_string ("wrap-prefix");
25080 Fmake_variable_buffer_local (Qwrap_prefix);
25081
25082 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25083 doc: /* Prefix prepended to all non-continuation lines at display time.
25084 The value may be a string, an image, or a stretch-glyph; it is
25085 interpreted in the same way as the value of a `display' text property.
25086
25087 This variable is overridden by any `line-prefix' text or overlay
25088 property.
25089
25090 To add a prefix to continuation lines, use `wrap-prefix'. */);
25091 Vline_prefix = Qnil;
25092 staticpro (&Qline_prefix);
25093 Qline_prefix = intern_c_string ("line-prefix");
25094 Fmake_variable_buffer_local (Qline_prefix);
25095
25096 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25097 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25098 inhibit_eval_during_redisplay = 0;
25099
25100 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25101 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25102 inhibit_free_realized_faces = 0;
25103
25104 #if GLYPH_DEBUG
25105 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25106 doc: /* Inhibit try_window_id display optimization. */);
25107 inhibit_try_window_id = 0;
25108
25109 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25110 doc: /* Inhibit try_window_reusing display optimization. */);
25111 inhibit_try_window_reusing = 0;
25112
25113 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25114 doc: /* Inhibit try_cursor_movement display optimization. */);
25115 inhibit_try_cursor_movement = 0;
25116 #endif /* GLYPH_DEBUG */
25117
25118 DEFVAR_INT ("overline-margin", &overline_margin,
25119 doc: /* *Space between overline and text, in pixels.
25120 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25121 margin to the caracter height. */);
25122 overline_margin = 2;
25123
25124 DEFVAR_INT ("underline-minimum-offset",
25125 &underline_minimum_offset,
25126 doc: /* Minimum distance between baseline and underline.
25127 This can improve legibility of underlined text at small font sizes,
25128 particularly when using variable `x-use-underline-position-properties'
25129 with fonts that specify an UNDERLINE_POSITION relatively close to the
25130 baseline. The default value is 1. */);
25131 underline_minimum_offset = 1;
25132
25133 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25134 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25135 display_hourglass_p = 1;
25136
25137 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25138 doc: /* *Seconds to wait before displaying an hourglass pointer.
25139 Value must be an integer or float. */);
25140 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25141
25142 hourglass_atimer = NULL;
25143 hourglass_shown_p = 0;
25144 }
25145
25146
25147 /* Initialize this module when Emacs starts. */
25148
25149 void
25150 init_xdisp ()
25151 {
25152 Lisp_Object root_window;
25153 struct window *mini_w;
25154
25155 current_header_line_height = current_mode_line_height = -1;
25156
25157 CHARPOS (this_line_start_pos) = 0;
25158
25159 mini_w = XWINDOW (minibuf_window);
25160 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25161 echo_area_window = minibuf_window;
25162
25163 if (!noninteractive)
25164 {
25165 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25166 int i;
25167
25168 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25169 set_window_height (root_window,
25170 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25171 0);
25172 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25173 set_window_height (minibuf_window, 1, 0);
25174
25175 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25176 mini_w->total_cols = make_number (FRAME_COLS (f));
25177
25178 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25179 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25180 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25181
25182 /* The default ellipsis glyphs `...'. */
25183 for (i = 0; i < 3; ++i)
25184 default_invis_vector[i] = make_number ('.');
25185 }
25186
25187 {
25188 /* Allocate the buffer for frame titles.
25189 Also used for `format-mode-line'. */
25190 int size = 100;
25191 mode_line_noprop_buf = (char *) xmalloc (size);
25192 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25193 mode_line_noprop_ptr = mode_line_noprop_buf;
25194 mode_line_target = MODE_LINE_DISPLAY;
25195 }
25196
25197 help_echo_showing_p = 0;
25198 }
25199
25200 /* Since w32 does not support atimers, it defines its own implementation of
25201 the following three functions in w32fns.c. */
25202 #ifndef WINDOWSNT
25203
25204 /* Platform-independent portion of hourglass implementation. */
25205
25206 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25207 int
25208 hourglass_started ()
25209 {
25210 return hourglass_shown_p || hourglass_atimer != NULL;
25211 }
25212
25213 /* Cancel a currently active hourglass timer, and start a new one. */
25214 void
25215 start_hourglass ()
25216 {
25217 #if defined (HAVE_WINDOW_SYSTEM)
25218 EMACS_TIME delay;
25219 int secs, usecs = 0;
25220
25221 cancel_hourglass ();
25222
25223 if (INTEGERP (Vhourglass_delay)
25224 && XINT (Vhourglass_delay) > 0)
25225 secs = XFASTINT (Vhourglass_delay);
25226 else if (FLOATP (Vhourglass_delay)
25227 && XFLOAT_DATA (Vhourglass_delay) > 0)
25228 {
25229 Lisp_Object tem;
25230 tem = Ftruncate (Vhourglass_delay, Qnil);
25231 secs = XFASTINT (tem);
25232 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25233 }
25234 else
25235 secs = DEFAULT_HOURGLASS_DELAY;
25236
25237 EMACS_SET_SECS_USECS (delay, secs, usecs);
25238 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25239 show_hourglass, NULL);
25240 #endif
25241 }
25242
25243
25244 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25245 shown. */
25246 void
25247 cancel_hourglass ()
25248 {
25249 #if defined (HAVE_WINDOW_SYSTEM)
25250 if (hourglass_atimer)
25251 {
25252 cancel_atimer (hourglass_atimer);
25253 hourglass_atimer = NULL;
25254 }
25255
25256 if (hourglass_shown_p)
25257 hide_hourglass ();
25258 #endif
25259 }
25260 #endif /* ! WINDOWSNT */
25261
25262 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25263 (do not change this comment) */